ieee1394: sbp2: remove unused struct members
[linux-2.6-block.git] / drivers / ieee1394 / sbp2.c
CommitLineData
1da177e4
LT
1/*
2 * sbp2.c - SBP-2 protocol driver for IEEE-1394
3 *
4 * Copyright (C) 2000 James Goodwin, Filanet Corporation (www.filanet.com)
5 * jamesg@filanet.com (JSG)
6 *
7 * Copyright (C) 2003 Ben Collins <bcollins@debian.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24/*
25 * Brief Description:
26 *
27 * This driver implements the Serial Bus Protocol 2 (SBP-2) over IEEE-1394
28 * under Linux. The SBP-2 driver is implemented as an IEEE-1394 high-level
29 * driver. It also registers as a SCSI lower-level driver in order to accept
30 * SCSI commands for transport using SBP-2.
31 *
32 * You may access any attached SBP-2 storage devices as if they were SCSI
33 * devices (e.g. mount /dev/sda1, fdisk, mkfs, etc.).
34 *
35 * Current Issues:
36 *
37 * - Error Handling: SCSI aborts and bus reset requests are handled somewhat
38 * but the code needs additional debugging.
39 */
40
902abed1
SR
41#include <linux/blkdev.h>
42#include <linux/compiler.h>
43#include <linux/delay.h>
44#include <linux/device.h>
45#include <linux/dma-mapping.h>
46#include <linux/gfp.h>
47#include <linux/init.h>
1da177e4
LT
48#include <linux/kernel.h>
49#include <linux/list.h>
1da177e4
LT
50#include <linux/module.h>
51#include <linux/moduleparam.h>
1da177e4 52#include <linux/pci.h>
902abed1
SR
53#include <linux/slab.h>
54#include <linux/spinlock.h>
55#include <linux/stat.h>
56#include <linux/string.h>
57#include <linux/stringify.h>
58#include <linux/types.h>
e8398bb7 59#include <linux/wait.h>
1da177e4 60
1da177e4 61#include <asm/byteorder.h>
902abed1
SR
62#include <asm/errno.h>
63#include <asm/param.h>
1da177e4 64#include <asm/scatterlist.h>
902abed1
SR
65#include <asm/system.h>
66#include <asm/types.h>
67
68#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
69#include <asm/io.h> /* for bus_to_virt */
70#endif
1da177e4
LT
71
72#include <scsi/scsi.h>
73#include <scsi/scsi_cmnd.h>
74#include <scsi/scsi_dbg.h>
75#include <scsi/scsi_device.h>
76#include <scsi/scsi_host.h>
77
78#include "csr1212.h"
902abed1
SR
79#include "highlevel.h"
80#include "hosts.h"
1da177e4 81#include "ieee1394.h"
1da177e4 82#include "ieee1394_core.h"
902abed1 83#include "ieee1394_hotplug.h"
1da177e4 84#include "ieee1394_transactions.h"
902abed1
SR
85#include "ieee1394_types.h"
86#include "nodemgr.h"
1da177e4
LT
87#include "sbp2.h"
88
1da177e4
LT
89/*
90 * Module load parameter definitions
91 */
92
93/*
94 * Change max_speed on module load if you have a bad IEEE-1394
95 * controller that has trouble running 2KB packets at 400mb.
96 *
97 * NOTE: On certain OHCI parts I have seen short packets on async transmit
98 * (probably due to PCI latency/throughput issues with the part). You can
99 * bump down the speed if you are running into problems.
100 */
ca0c7453
SR
101static int sbp2_max_speed = IEEE1394_SPEED_MAX;
102module_param_named(max_speed, sbp2_max_speed, int, 0644);
28b06679
SR
103MODULE_PARM_DESC(max_speed, "Force max speed "
104 "(3 = 800Mb/s, 2 = 400Mb/s, 1 = 200Mb/s, 0 = 100Mb/s)");
1da177e4
LT
105
106/*
107 * Set serialize_io to 1 if you'd like only one scsi command sent
108 * down to us at a time (debugging). This might be necessary for very
109 * badly behaved sbp2 devices.
2bab359a
JM
110 *
111 * TODO: Make this configurable per device.
1da177e4 112 */
ca0c7453
SR
113static int sbp2_serialize_io = 1;
114module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
115MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
116 "(default = 1, faster = 0)");
1da177e4
LT
117
118/*
119 * Bump up max_sectors if you'd like to support very large sized
120 * transfers. Please note that some older sbp2 bridge chips are broken for
121 * transfers greater or equal to 128KB. Default is a value of 255
122 * sectors, or just under 128KB (at 512 byte sector size). I can note that
123 * the Oxsemi sbp2 chipsets have no problems supporting very large
124 * transfer sizes.
125 */
ca0c7453
SR
126static int sbp2_max_sectors = SBP2_MAX_SECTORS;
127module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
128MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
129 "(default = " __stringify(SBP2_MAX_SECTORS) ")");
1da177e4
LT
130
131/*
132 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
133 * do an exclusive login, as it's generally unsafe to have two hosts
134 * talking to a single sbp2 device at the same time (filesystem coherency,
135 * etc.). If you're running an sbp2 device that supports multiple logins,
136 * and you're either running read-only filesystems or some sort of special
20f45781
BC
137 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
138 * File System, or Lustre, then set exclusive_login to zero.
139 *
140 * So far only bridges from Oxford Semiconductor are known to support
141 * concurrent logins. Depending on firmware, four or two concurrent logins
142 * are possible on OXFW911 and newer Oxsemi bridges.
1da177e4 143 */
ca0c7453
SR
144static int sbp2_exclusive_login = 1;
145module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
146MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
147 "(default = 1)");
1da177e4
LT
148
149/*
24d3bf88
SR
150 * If any of the following workarounds is required for your device to work,
151 * please submit the kernel messages logged by sbp2 to the linux1394-devel
152 * mailing list.
1da177e4 153 *
24d3bf88
SR
154 * - 128kB max transfer
155 * Limit transfer size. Necessary for some old bridges.
156 *
157 * - 36 byte inquiry
158 * When scsi_mod probes the device, let the inquiry command look like that
159 * from MS Windows.
160 *
161 * - skip mode page 8
162 * Suppress sending of mode_sense for mode page 8 if the device pretends to
163 * support the SCSI Primary Block commands instead of Reduced Block Commands.
e9a1c52c
SR
164 *
165 * - fix capacity
166 * Tell sd_mod to correct the last sector number reported by read_capacity.
167 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
168 * Don't use this with devices which don't have this bug.
679c0cd2
SR
169 *
170 * - override internal blacklist
171 * Instead of adding to the built-in blacklist, use only the workarounds
172 * specified in the module load parameter.
173 * Useful if a blacklist entry interfered with a non-broken device.
1da177e4 174 */
24d3bf88
SR
175static int sbp2_default_workarounds;
176module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
177MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
178 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
179 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
180 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
e9a1c52c 181 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
679c0cd2 182 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
24d3bf88
SR
183 ", or a combination)");
184
1da177e4 185
edf1fb21
SR
186#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
187#define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
1da177e4 188
1da177e4
LT
189/*
190 * Globals
191 */
ea42ea0f
SR
192static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *, u32);
193static void sbp2scsi_complete_command(struct scsi_id_instance_data *, u32,
194 struct scsi_cmnd *,
195 void (*)(struct scsi_cmnd *));
196static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *);
197static int sbp2_start_device(struct scsi_id_instance_data *);
198static void sbp2_remove_device(struct scsi_id_instance_data *);
199static int sbp2_login_device(struct scsi_id_instance_data *);
200static int sbp2_reconnect_device(struct scsi_id_instance_data *);
201static int sbp2_logout_device(struct scsi_id_instance_data *);
202static void sbp2_host_reset(struct hpsb_host *);
203static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
204 u64, size_t, u16);
205static int sbp2_agent_reset(struct scsi_id_instance_data *, int);
206static void sbp2_parse_unit_directory(struct scsi_id_instance_data *,
207 struct unit_directory *);
208static int sbp2_set_busy_timeout(struct scsi_id_instance_data *);
209static int sbp2_max_speed_and_size(struct scsi_id_instance_data *);
1da177e4 210
1da177e4
LT
211
212static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
213
1da177e4 214static struct hpsb_highlevel sbp2_highlevel = {
ea42ea0f
SR
215 .name = SBP2_DEVICE_NAME,
216 .host_reset = sbp2_host_reset,
1da177e4
LT
217};
218
219static struct hpsb_address_ops sbp2_ops = {
ea42ea0f 220 .write = sbp2_handle_status_write
1da177e4
LT
221};
222
223#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
ea42ea0f
SR
224static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
225 u64, size_t, u16);
226static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
227 size_t, u16);
228
1da177e4 229static struct hpsb_address_ops sbp2_physdma_ops = {
ea42ea0f
SR
230 .read = sbp2_handle_physdma_read,
231 .write = sbp2_handle_physdma_write,
1da177e4
LT
232};
233#endif
234
ea42ea0f
SR
235
236/*
237 * Interface to driver core and IEEE 1394 core
238 */
239static struct ieee1394_device_id sbp2_id_table[] = {
240 {
241 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
242 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
243 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
244 {}
245};
246MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
247
248static int sbp2_probe(struct device *);
249static int sbp2_remove(struct device *);
250static int sbp2_update(struct unit_directory *);
251
1da177e4
LT
252static struct hpsb_protocol_driver sbp2_driver = {
253 .name = "SBP2 Driver",
254 .id_table = sbp2_id_table,
255 .update = sbp2_update,
256 .driver = {
257 .name = SBP2_DEVICE_NAME,
258 .bus = &ieee1394_bus_type,
259 .probe = sbp2_probe,
260 .remove = sbp2_remove,
261 },
262};
263
ea42ea0f
SR
264
265/*
266 * Interface to SCSI core
267 */
268static int sbp2scsi_queuecommand(struct scsi_cmnd *,
269 void (*)(struct scsi_cmnd *));
270static int sbp2scsi_abort(struct scsi_cmnd *);
271static int sbp2scsi_reset(struct scsi_cmnd *);
272static int sbp2scsi_slave_alloc(struct scsi_device *);
273static int sbp2scsi_slave_configure(struct scsi_device *);
274static void sbp2scsi_slave_destroy(struct scsi_device *);
275static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
276 struct device_attribute *, char *);
277
278static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
279
280static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
281 &dev_attr_ieee1394_id,
282 NULL
283};
284
ca0c7453 285static struct scsi_host_template sbp2_shost_template = {
ea42ea0f
SR
286 .module = THIS_MODULE,
287 .name = "SBP-2 IEEE-1394",
288 .proc_name = SBP2_DEVICE_NAME,
289 .queuecommand = sbp2scsi_queuecommand,
290 .eh_abort_handler = sbp2scsi_abort,
291 .eh_device_reset_handler = sbp2scsi_reset,
292 .slave_alloc = sbp2scsi_slave_alloc,
293 .slave_configure = sbp2scsi_slave_configure,
294 .slave_destroy = sbp2scsi_slave_destroy,
295 .this_id = -1,
296 .sg_tablesize = SG_ALL,
297 .use_clustering = ENABLE_CLUSTERING,
298 .cmd_per_lun = SBP2_MAX_CMDS,
299 .can_queue = SBP2_MAX_CMDS,
300 .emulated = 1,
301 .sdev_attrs = sbp2_sysfs_sdev_attrs,
302};
303
304
a80614d1 305/*
24d3bf88
SR
306 * List of devices with known bugs.
307 *
308 * The firmware_revision field, masked with 0xffff00, is the best indicator
309 * for the type of bridge chip of a device. It yields a few false positives
310 * but this did not break correctly behaving devices so far.
a80614d1 311 */
24d3bf88
SR
312static const struct {
313 u32 firmware_revision;
e9a1c52c 314 u32 model_id;
24d3bf88
SR
315 unsigned workarounds;
316} sbp2_workarounds_table[] = {
4b9a3347 317 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
24d3bf88 318 .firmware_revision = 0x002800,
4b9a3347 319 .model_id = 0x001010,
24d3bf88
SR
320 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
321 SBP2_WORKAROUND_MODE_SENSE_8,
322 },
323 /* Initio bridges, actually only needed for some older ones */ {
324 .firmware_revision = 0x000200,
325 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
326 },
327 /* Symbios bridge */ {
328 .firmware_revision = 0xa0b800,
329 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
e9a1c52c
SR
330 },
331 /*
332 * Note about the following Apple iPod blacklist entries:
333 *
334 * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
335 * matching logic treats 0 as a wildcard, we cannot match this ID
336 * without rewriting the matching routine. Fortunately these iPods
337 * do not feature the read_capacity bug according to one report.
338 * Read_capacity behaviour as well as model_id could change due to
339 * Apple-supplied firmware updates though.
340 */
341 /* iPod 4th generation */ {
342 .firmware_revision = 0x0a2700,
343 .model_id = 0x000021,
344 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
345 },
346 /* iPod mini */ {
347 .firmware_revision = 0x0a2700,
348 .model_id = 0x000023,
349 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
350 },
351 /* iPod Photo */ {
352 .firmware_revision = 0x0a2700,
353 .model_id = 0x00007e,
354 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
24d3bf88 355 }
1da177e4
LT
356};
357
1da177e4
LT
358/**************************************
359 * General utility functions
360 **************************************/
361
1da177e4
LT
362#ifndef __BIG_ENDIAN
363/*
364 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
365 */
2b01b80b 366static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
1da177e4
LT
367{
368 u32 *temp = buffer;
369
370 for (length = (length >> 2); length--; )
371 temp[length] = be32_to_cpu(temp[length]);
372
373 return;
374}
375
376/*
377 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
378 */
2b01b80b 379static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
1da177e4
LT
380{
381 u32 *temp = buffer;
382
383 for (length = (length >> 2); length--; )
384 temp[length] = cpu_to_be32(temp[length]);
385
386 return;
387}
388#else /* BIG_ENDIAN */
389/* Why waste the cpu cycles? */
611aa19f
SR
390#define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
391#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
1da177e4
LT
392#endif
393
ca0c7453 394static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
e8398bb7 395
1da177e4 396/*
e8398bb7
SR
397 * Waits for completion of an SBP-2 access request.
398 * Returns nonzero if timed out or prematurely interrupted.
1da177e4 399 */
e8398bb7
SR
400static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
401 int timeout)
1da177e4 402{
ca0c7453 403 long leftover;
1da177e4 404
ca0c7453
SR
405 leftover = wait_event_interruptible_timeout(
406 sbp2_access_wq, scsi_id->access_complete, timeout);
e8398bb7
SR
407 scsi_id->access_complete = 0;
408 return leftover <= 0;
1da177e4
LT
409}
410
1da177e4
LT
411static void sbp2_free_packet(struct hpsb_packet *packet)
412{
413 hpsb_free_tlabel(packet);
414 hpsb_free_packet(packet);
415}
416
e8ca5668
SR
417/*
418 * This is much like hpsb_node_write(), except it ignores the response
419 * subaction and returns immediately. Can be used from atomic context.
1da177e4
LT
420 */
421static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
a237f35f 422 quadlet_t *buffer, size_t length)
1da177e4
LT
423{
424 struct hpsb_packet *packet;
425
426 packet = hpsb_make_writepacket(ne->host, ne->nodeid,
427 addr, buffer, length);
a237f35f
SR
428 if (!packet)
429 return -ENOMEM;
1da177e4 430
a237f35f
SR
431 hpsb_set_packet_complete_task(packet,
432 (void (*)(void *))sbp2_free_packet,
1da177e4
LT
433 packet);
434
435 hpsb_node_fill_packet(ne, packet);
436
a237f35f 437 if (hpsb_send_packet(packet) < 0) {
1da177e4
LT
438 sbp2_free_packet(packet);
439 return -EIO;
440 }
441
442 return 0;
443}
444
09ee67ab
SR
445static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
446 u64 offset, quadlet_t *data, size_t len)
447{
448 /*
449 * There is a small window after a bus reset within which the node
450 * entry's generation is current but the reconnect wasn't completed.
451 */
2cccbb55 452 if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
09ee67ab
SR
453 return;
454
455 if (hpsb_node_write(scsi_id->ne,
ca0c7453 456 scsi_id->command_block_agent_addr + offset,
09ee67ab
SR
457 data, len))
458 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
459 /*
460 * Now accept new SCSI commands, unless a bus reset happended during
461 * hpsb_node_write.
462 */
2cccbb55 463 if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET))
09ee67ab
SR
464 scsi_unblock_requests(scsi_id->scsi_host);
465}
466
c4028958 467static void sbp2util_write_orb_pointer(struct work_struct *work)
09ee67ab
SR
468{
469 quadlet_t data[2];
470
d19c7764
SR
471 data[0] = ORB_SET_NODE_ID(
472 (container_of(work, struct scsi_id_instance_data, protocol_work))->hi->host->node_id);
473 data[1] = (container_of(work, struct scsi_id_instance_data, protocol_work))->last_orb_dma;
09ee67ab 474 sbp2util_cpu_to_be32_buffer(data, 8);
d19c7764 475 sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_ORB_POINTER_OFFSET, data, 8);
09ee67ab
SR
476}
477
c4028958 478static void sbp2util_write_doorbell(struct work_struct *work)
09ee67ab 479{
d19c7764 480 sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_DOORBELL_OFFSET, NULL, 4);
09ee67ab
SR
481}
482
1da177e4
LT
483static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
484{
ca0c7453 485 struct sbp2_fwhost_info *hi = scsi_id->hi;
1da177e4
LT
486 int i;
487 unsigned long flags, orbs;
488 struct sbp2_command_info *command;
489
ca0c7453 490 orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
1da177e4 491
ca0c7453 492 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1da177e4 493 for (i = 0; i < orbs; i++) {
8551158a 494 command = kzalloc(sizeof(*command), GFP_ATOMIC);
1da177e4 495 if (!command) {
ca0c7453 496 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock,
a237f35f
SR
497 flags);
498 return -ENOMEM;
1da177e4 499 }
1da177e4 500 command->command_orb_dma =
a237f35f
SR
501 pci_map_single(hi->host->pdev, &command->command_orb,
502 sizeof(struct sbp2_command_orb),
d4018d7f 503 PCI_DMA_TODEVICE);
1da177e4 504 command->sge_dma =
a237f35f
SR
505 pci_map_single(hi->host->pdev,
506 &command->scatter_gather_element,
507 sizeof(command->scatter_gather_element),
508 PCI_DMA_BIDIRECTIONAL);
1da177e4 509 INIT_LIST_HEAD(&command->list);
ca0c7453 510 list_add_tail(&command->list, &scsi_id->cmd_orb_completed);
1da177e4 511 }
ca0c7453 512 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1da177e4
LT
513 return 0;
514}
515
1da177e4
LT
516static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
517{
518 struct hpsb_host *host = scsi_id->hi->host;
519 struct list_head *lh, *next;
520 struct sbp2_command_info *command;
521 unsigned long flags;
522
ca0c7453
SR
523 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
524 if (!list_empty(&scsi_id->cmd_orb_completed)) {
525 list_for_each_safe(lh, next, &scsi_id->cmd_orb_completed) {
1da177e4 526 command = list_entry(lh, struct sbp2_command_info, list);
1da177e4
LT
527 pci_unmap_single(host->pdev, command->command_orb_dma,
528 sizeof(struct sbp2_command_orb),
d4018d7f 529 PCI_DMA_TODEVICE);
1da177e4
LT
530 pci_unmap_single(host->pdev, command->sge_dma,
531 sizeof(command->scatter_gather_element),
532 PCI_DMA_BIDIRECTIONAL);
1da177e4
LT
533 kfree(command);
534 }
535 }
ca0c7453 536 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1da177e4
LT
537 return;
538}
539
540/*
e8ca5668
SR
541 * Finds the sbp2_command for a given outstanding command ORB.
542 * Only looks at the in-use list.
1da177e4
LT
543 */
544static struct sbp2_command_info *sbp2util_find_command_for_orb(
545 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
546{
547 struct sbp2_command_info *command;
548 unsigned long flags;
549
ca0c7453
SR
550 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
551 if (!list_empty(&scsi_id->cmd_orb_inuse)) {
552 list_for_each_entry(command, &scsi_id->cmd_orb_inuse, list) {
1da177e4 553 if (command->command_orb_dma == orb) {
ca0c7453 554 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
a237f35f 555 return command;
1da177e4
LT
556 }
557 }
558 }
ca0c7453 559 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
a237f35f 560 return NULL;
1da177e4
LT
561}
562
563/*
e8ca5668
SR
564 * Finds the sbp2_command for a given outstanding SCpnt.
565 * Only looks at the in-use list.
ca0c7453 566 * Must be called with scsi_id->cmd_orb_lock held.
1da177e4 567 */
24c7cd06
SR
568static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
569 struct scsi_id_instance_data *scsi_id, void *SCpnt)
1da177e4
LT
570{
571 struct sbp2_command_info *command;
1da177e4 572
ca0c7453
SR
573 if (!list_empty(&scsi_id->cmd_orb_inuse))
574 list_for_each_entry(command, &scsi_id->cmd_orb_inuse, list)
24c7cd06 575 if (command->Current_SCpnt == SCpnt)
a237f35f 576 return command;
a237f35f 577 return NULL;
1da177e4
LT
578}
579
1da177e4
LT
580static struct sbp2_command_info *sbp2util_allocate_command_orb(
581 struct scsi_id_instance_data *scsi_id,
582 struct scsi_cmnd *Current_SCpnt,
583 void (*Current_done)(struct scsi_cmnd *))
584{
585 struct list_head *lh;
586 struct sbp2_command_info *command = NULL;
587 unsigned long flags;
588
ca0c7453
SR
589 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
590 if (!list_empty(&scsi_id->cmd_orb_completed)) {
591 lh = scsi_id->cmd_orb_completed.next;
1da177e4
LT
592 list_del(lh);
593 command = list_entry(lh, struct sbp2_command_info, list);
594 command->Current_done = Current_done;
595 command->Current_SCpnt = Current_SCpnt;
ca0c7453 596 list_add_tail(&command->list, &scsi_id->cmd_orb_inuse);
1da177e4 597 } else {
d024ebc6 598 SBP2_ERR("%s: no orbs available", __FUNCTION__);
1da177e4 599 }
ca0c7453 600 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
a237f35f 601 return command;
1da177e4
LT
602}
603
1da177e4
LT
604static void sbp2util_free_command_dma(struct sbp2_command_info *command)
605{
606 struct scsi_id_instance_data *scsi_id =
607 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
608 struct hpsb_host *host;
609
610 if (!scsi_id) {
d024ebc6 611 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
1da177e4
LT
612 return;
613 }
614
615 host = scsi_id->ud->ne->host;
616
617 if (command->cmd_dma) {
edf1fb21 618 if (command->dma_type == CMD_DMA_SINGLE)
1da177e4
LT
619 pci_unmap_single(host->pdev, command->cmd_dma,
620 command->dma_size, command->dma_dir);
edf1fb21 621 else if (command->dma_type == CMD_DMA_PAGE)
1da177e4
LT
622 pci_unmap_page(host->pdev, command->cmd_dma,
623 command->dma_size, command->dma_dir);
edf1fb21 624 /* XXX: Check for CMD_DMA_NONE bug */
1da177e4
LT
625 command->dma_type = CMD_DMA_NONE;
626 command->cmd_dma = 0;
627 }
628
629 if (command->sge_buffer) {
630 pci_unmap_sg(host->pdev, command->sge_buffer,
631 command->dma_size, command->dma_dir);
1da177e4
LT
632 command->sge_buffer = NULL;
633 }
634}
635
636/*
637 * This function moves a command to the completed orb list.
ca0c7453 638 * Must be called with scsi_id->cmd_orb_lock held.
1da177e4 639 */
24c7cd06
SR
640static void sbp2util_mark_command_completed(
641 struct scsi_id_instance_data *scsi_id,
642 struct sbp2_command_info *command)
1da177e4 643{
1da177e4
LT
644 list_del(&command->list);
645 sbp2util_free_command_dma(command);
ca0c7453 646 list_add_tail(&command->list, &scsi_id->cmd_orb_completed);
1da177e4
LT
647}
648
abd559b1
JM
649/*
650 * Is scsi_id valid? Is the 1394 node still present?
651 */
652static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
653{
654 return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
655}
656
1da177e4
LT
657/*********************************************
658 * IEEE-1394 core driver stack related section
659 *********************************************/
1da177e4
LT
660
661static int sbp2_probe(struct device *dev)
662{
663 struct unit_directory *ud;
664 struct scsi_id_instance_data *scsi_id;
665
1da177e4
LT
666 ud = container_of(dev, struct unit_directory, device);
667
668 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
669 * instead. */
670 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
671 return -ENODEV;
672
a237f35f 673 scsi_id = sbp2_alloc_device(ud);
1da177e4 674
a237f35f
SR
675 if (!scsi_id)
676 return -ENOMEM;
1da177e4 677
a237f35f 678 sbp2_parse_unit_directory(scsi_id, ud);
1da177e4 679
a237f35f 680 return sbp2_start_device(scsi_id);
1da177e4
LT
681}
682
683static int sbp2_remove(struct device *dev)
684{
685 struct unit_directory *ud;
686 struct scsi_id_instance_data *scsi_id;
abd559b1 687 struct scsi_device *sdev;
1da177e4 688
1da177e4
LT
689 ud = container_of(dev, struct unit_directory, device);
690 scsi_id = ud->device.driver_data;
abd559b1
JM
691 if (!scsi_id)
692 return 0;
693
bf637ec3
SR
694 if (scsi_id->scsi_host) {
695 /* Get rid of enqueued commands if there is no chance to
696 * send them. */
697 if (!sbp2util_node_is_available(scsi_id))
698 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
e8ca5668 699 /* scsi_remove_device() may trigger shutdown functions of SCSI
bf637ec3 700 * highlevel drivers which would deadlock if blocked. */
2cccbb55 701 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
abd559b1 702 scsi_unblock_requests(scsi_id->scsi_host);
bf637ec3 703 }
abd559b1
JM
704 sdev = scsi_id->sdev;
705 if (sdev) {
706 scsi_id->sdev = NULL;
707 scsi_remove_device(sdev);
708 }
1da177e4
LT
709
710 sbp2_logout_device(scsi_id);
711 sbp2_remove_device(scsi_id);
712
713 return 0;
714}
715
716static int sbp2_update(struct unit_directory *ud)
717{
718 struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
719
1da177e4 720 if (sbp2_reconnect_device(scsi_id)) {
e8ca5668
SR
721 /* Reconnect has failed. Perhaps we didn't reconnect fast
722 * enough. Try a regular login, but first log out just in
723 * case of any weirdness. */
1da177e4
LT
724 sbp2_logout_device(scsi_id);
725
726 if (sbp2_login_device(scsi_id)) {
727 /* Login failed too, just fail, and the backend
728 * will call our sbp2_remove for us */
729 SBP2_ERR("Failed to reconnect to sbp2 device!");
730 return -EBUSY;
731 }
732 }
733
1da177e4 734 sbp2_set_busy_timeout(scsi_id);
1da177e4 735 sbp2_agent_reset(scsi_id, 1);
1da177e4
LT
736 sbp2_max_speed_and_size(scsi_id);
737
e8ca5668
SR
738 /* Complete any pending commands with busy (so they get retried)
739 * and remove them from our queue. */
1da177e4
LT
740 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
741
4fc383c0
SR
742 /* Accept new commands unless there was another bus reset in the
743 * meantime. */
744 if (hpsb_node_entry_valid(scsi_id->ne)) {
2cccbb55 745 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
4fc383c0
SR
746 scsi_unblock_requests(scsi_id->scsi_host);
747 }
1da177e4
LT
748 return 0;
749}
750
1da177e4
LT
751static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
752{
ca0c7453 753 struct sbp2_fwhost_info *hi;
1da177e4
LT
754 struct Scsi_Host *scsi_host = NULL;
755 struct scsi_id_instance_data *scsi_id = NULL;
756
8551158a 757 scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
1da177e4
LT
758 if (!scsi_id) {
759 SBP2_ERR("failed to create scsi_id");
760 goto failed_alloc;
761 }
1da177e4
LT
762
763 scsi_id->ne = ud->ne;
764 scsi_id->ud = ud;
765 scsi_id->speed_code = IEEE1394_SPEED_100;
766 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
6737231e 767 scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
ca0c7453
SR
768 INIT_LIST_HEAD(&scsi_id->cmd_orb_inuse);
769 INIT_LIST_HEAD(&scsi_id->cmd_orb_completed);
1da177e4 770 INIT_LIST_HEAD(&scsi_id->scsi_list);
ca0c7453 771 spin_lock_init(&scsi_id->cmd_orb_lock);
2cccbb55 772 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
d19c7764 773 INIT_WORK(&scsi_id->protocol_work, NULL);
1da177e4
LT
774
775 ud->device.driver_data = scsi_id;
776
777 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
778 if (!hi) {
779 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
780 if (!hi) {
781 SBP2_ERR("failed to allocate hostinfo");
782 goto failed_alloc;
783 }
1da177e4
LT
784 hi->host = ud->ne->host;
785 INIT_LIST_HEAD(&hi->scsi_ids);
786
1da177e4
LT
787#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
788 /* Handle data movement if physical dma is not
55664051
SR
789 * enabled or not supported on host controller */
790 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
791 &sbp2_physdma_ops,
792 0x0ULL, 0xfffffffcULL)) {
793 SBP2_ERR("failed to register lower 4GB address range");
794 goto failed_alloc;
795 }
1da177e4
LT
796#endif
797 }
798
147830f2
SR
799 /* Prevent unloading of the 1394 host */
800 if (!try_module_get(hi->host->driver->owner)) {
801 SBP2_ERR("failed to get a reference on 1394 host driver");
802 goto failed_alloc;
803 }
804
1da177e4
LT
805 scsi_id->hi = hi;
806
807 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
808
35bdddb8
SR
809 /* Register the status FIFO address range. We could use the same FIFO
810 * for targets at different nodes. However we need different FIFOs per
a54c9d30
SR
811 * target in order to support multi-unit devices.
812 * The FIFO is located out of the local host controller's physical range
813 * but, if possible, within the posted write area. Status writes will
814 * then be performed as unified transactions. This slightly reduces
815 * bandwidth usage, and some Prolific based devices seem to require it.
816 */
35bdddb8
SR
817 scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
818 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
819 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
40ae6c5e 820 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
6737231e 821 if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
35bdddb8
SR
822 SBP2_ERR("failed to allocate status FIFO address range");
823 goto failed_alloc;
824 }
825
ca0c7453 826 scsi_host = scsi_host_alloc(&sbp2_shost_template,
a237f35f 827 sizeof(unsigned long));
1da177e4
LT
828 if (!scsi_host) {
829 SBP2_ERR("failed to register scsi host");
830 goto failed_alloc;
831 }
832
833 scsi_host->hostdata[0] = (unsigned long)scsi_id;
834
835 if (!scsi_add_host(scsi_host, &ud->device)) {
836 scsi_id->scsi_host = scsi_host;
837 return scsi_id;
838 }
839
840 SBP2_ERR("failed to add scsi host");
841 scsi_host_put(scsi_host);
842
843failed_alloc:
844 sbp2_remove_device(scsi_id);
845 return NULL;
846}
847
1da177e4
LT
848static void sbp2_host_reset(struct hpsb_host *host)
849{
ca0c7453 850 struct sbp2_fwhost_info *hi;
1da177e4
LT
851 struct scsi_id_instance_data *scsi_id;
852
853 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
2cccbb55
SR
854 if (!hi)
855 return;
856 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
857 if (likely(atomic_read(&scsi_id->state) !=
858 SBP2LU_STATE_IN_SHUTDOWN)) {
859 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
1da177e4 860 scsi_block_requests(scsi_id->scsi_host);
09ee67ab 861 }
1da177e4
LT
862}
863
1da177e4
LT
864static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
865{
ca0c7453 866 struct sbp2_fwhost_info *hi = scsi_id->hi;
146f7262 867 int error;
1da177e4 868
1da177e4 869 scsi_id->login_response =
a237f35f
SR
870 pci_alloc_consistent(hi->host->pdev,
871 sizeof(struct sbp2_login_response),
1da177e4
LT
872 &scsi_id->login_response_dma);
873 if (!scsi_id->login_response)
874 goto alloc_fail;
1da177e4 875
1da177e4 876 scsi_id->query_logins_orb =
a237f35f
SR
877 pci_alloc_consistent(hi->host->pdev,
878 sizeof(struct sbp2_query_logins_orb),
1da177e4
LT
879 &scsi_id->query_logins_orb_dma);
880 if (!scsi_id->query_logins_orb)
881 goto alloc_fail;
1da177e4 882
1da177e4 883 scsi_id->query_logins_response =
a237f35f
SR
884 pci_alloc_consistent(hi->host->pdev,
885 sizeof(struct sbp2_query_logins_response),
1da177e4
LT
886 &scsi_id->query_logins_response_dma);
887 if (!scsi_id->query_logins_response)
888 goto alloc_fail;
1da177e4 889
1da177e4 890 scsi_id->reconnect_orb =
a237f35f
SR
891 pci_alloc_consistent(hi->host->pdev,
892 sizeof(struct sbp2_reconnect_orb),
1da177e4
LT
893 &scsi_id->reconnect_orb_dma);
894 if (!scsi_id->reconnect_orb)
895 goto alloc_fail;
1da177e4 896
1da177e4 897 scsi_id->logout_orb =
a237f35f
SR
898 pci_alloc_consistent(hi->host->pdev,
899 sizeof(struct sbp2_logout_orb),
1da177e4
LT
900 &scsi_id->logout_orb_dma);
901 if (!scsi_id->logout_orb)
902 goto alloc_fail;
1da177e4 903
1da177e4 904 scsi_id->login_orb =
a237f35f
SR
905 pci_alloc_consistent(hi->host->pdev,
906 sizeof(struct sbp2_login_orb),
1da177e4 907 &scsi_id->login_orb_dma);
eaceec7f
SR
908 if (!scsi_id->login_orb)
909 goto alloc_fail;
1da177e4 910
1da177e4
LT
911 if (sbp2util_create_command_orb_pool(scsi_id)) {
912 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
913 sbp2_remove_device(scsi_id);
914 return -ENOMEM;
915 }
916
e8ca5668
SR
917 /* Wait a second before trying to log in. Previously logged in
918 * initiators need a chance to reconnect. */
902abed1 919 if (msleep_interruptible(1000)) {
1da177e4
LT
920 sbp2_remove_device(scsi_id);
921 return -EINTR;
922 }
a237f35f 923
1da177e4 924 if (sbp2_login_device(scsi_id)) {
1da177e4
LT
925 sbp2_remove_device(scsi_id);
926 return -EBUSY;
927 }
928
1da177e4 929 sbp2_set_busy_timeout(scsi_id);
1da177e4 930 sbp2_agent_reset(scsi_id, 1);
1da177e4
LT
931 sbp2_max_speed_and_size(scsi_id);
932
146f7262
JB
933 error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
934 if (error) {
1da177e4 935 SBP2_ERR("scsi_add_device failed");
dc3edd54
SR
936 sbp2_logout_device(scsi_id);
937 sbp2_remove_device(scsi_id);
146f7262 938 return error;
1da177e4
LT
939 }
940
941 return 0;
eaceec7f
SR
942
943alloc_fail:
944 SBP2_ERR("Could not allocate memory for scsi_id");
945 sbp2_remove_device(scsi_id);
946 return -ENOMEM;
1da177e4
LT
947}
948
1da177e4
LT
949static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
950{
ca0c7453 951 struct sbp2_fwhost_info *hi;
1da177e4 952
1da177e4
LT
953 if (!scsi_id)
954 return;
955
956 hi = scsi_id->hi;
957
1da177e4
LT
958 if (scsi_id->scsi_host) {
959 scsi_remove_host(scsi_id->scsi_host);
960 scsi_host_put(scsi_id->scsi_host);
961 }
09ee67ab 962 flush_scheduled_work();
1da177e4
LT
963 sbp2util_remove_command_orb_pool(scsi_id);
964
965 list_del(&scsi_id->scsi_list);
966
edf1fb21 967 if (scsi_id->login_response)
1da177e4
LT
968 pci_free_consistent(hi->host->pdev,
969 sizeof(struct sbp2_login_response),
970 scsi_id->login_response,
971 scsi_id->login_response_dma);
edf1fb21 972 if (scsi_id->login_orb)
1da177e4
LT
973 pci_free_consistent(hi->host->pdev,
974 sizeof(struct sbp2_login_orb),
975 scsi_id->login_orb,
976 scsi_id->login_orb_dma);
edf1fb21 977 if (scsi_id->reconnect_orb)
1da177e4
LT
978 pci_free_consistent(hi->host->pdev,
979 sizeof(struct sbp2_reconnect_orb),
980 scsi_id->reconnect_orb,
981 scsi_id->reconnect_orb_dma);
edf1fb21 982 if (scsi_id->logout_orb)
1da177e4
LT
983 pci_free_consistent(hi->host->pdev,
984 sizeof(struct sbp2_logout_orb),
985 scsi_id->logout_orb,
986 scsi_id->logout_orb_dma);
edf1fb21 987 if (scsi_id->query_logins_orb)
1da177e4
LT
988 pci_free_consistent(hi->host->pdev,
989 sizeof(struct sbp2_query_logins_orb),
990 scsi_id->query_logins_orb,
991 scsi_id->query_logins_orb_dma);
edf1fb21 992 if (scsi_id->query_logins_response)
1da177e4
LT
993 pci_free_consistent(hi->host->pdev,
994 sizeof(struct sbp2_query_logins_response),
995 scsi_id->query_logins_response,
996 scsi_id->query_logins_response_dma);
1da177e4 997
6737231e 998 if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
35bdddb8 999 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
6737231e 1000 scsi_id->status_fifo_addr);
35bdddb8 1001
1da177e4
LT
1002 scsi_id->ud->device.driver_data = NULL;
1003
147830f2
SR
1004 if (hi)
1005 module_put(hi->host->driver->owner);
1006
1da177e4
LT
1007 kfree(scsi_id);
1008}
1009
1010#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1011/*
e8ca5668
SR
1012 * Deal with write requests on adapters which do not support physical DMA or
1013 * have it switched off.
1da177e4 1014 */
a237f35f
SR
1015static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1016 int destid, quadlet_t *data, u64 addr,
1017 size_t length, u16 flags)
1da177e4 1018{
a237f35f 1019 memcpy(bus_to_virt((u32) addr), data, length);
a237f35f 1020 return RCODE_COMPLETE;
1da177e4
LT
1021}
1022
1023/*
e8ca5668
SR
1024 * Deal with read requests on adapters which do not support physical DMA or
1025 * have it switched off.
1da177e4 1026 */
a237f35f
SR
1027static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1028 quadlet_t *data, u64 addr, size_t length,
1029 u16 flags)
1da177e4 1030{
a237f35f 1031 memcpy(data, bus_to_virt((u32) addr), length);
a237f35f 1032 return RCODE_COMPLETE;
1da177e4
LT
1033}
1034#endif
1035
1da177e4
LT
1036/**************************************
1037 * SBP-2 protocol related section
1038 **************************************/
1039
1da177e4
LT
1040static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1041{
ca0c7453 1042 struct sbp2_fwhost_info *hi = scsi_id->hi;
1da177e4
LT
1043 quadlet_t data[2];
1044 int max_logins;
1045 int active_logins;
1046
1da177e4
LT
1047 scsi_id->query_logins_orb->reserved1 = 0x0;
1048 scsi_id->query_logins_orb->reserved2 = 0x0;
1049
1050 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1051 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1da177e4
LT
1052
1053 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1054 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
ca0c7453 1055 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->lun);
1da177e4
LT
1056
1057 scsi_id->query_logins_orb->reserved_resp_length =
1058 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
1da177e4 1059
35bdddb8
SR
1060 scsi_id->query_logins_orb->status_fifo_hi =
1061 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1062 scsi_id->query_logins_orb->status_fifo_lo =
1063 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1da177e4
LT
1064
1065 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1066
1da177e4 1067 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1da177e4 1068
1da177e4
LT
1069 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1070 data[1] = scsi_id->query_logins_orb_dma;
1071 sbp2util_cpu_to_be32_buffer(data, 8);
1072
ca0c7453 1073 hpsb_node_write(scsi_id->ne, scsi_id->management_agent_addr, data, 8);
1da177e4 1074
e8398bb7 1075 if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
1da177e4 1076 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
a237f35f 1077 return -EIO;
1da177e4
LT
1078 }
1079
1080 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1081 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
a237f35f 1082 return -EIO;
1da177e4
LT
1083 }
1084
6065772d
SR
1085 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1086 SBP2_INFO("Error querying logins to SBP-2 device - failed");
a237f35f 1087 return -EIO;
1da177e4
LT
1088 }
1089
1090 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1091
1da177e4 1092 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
20f45781 1093 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1da177e4
LT
1094
1095 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
20f45781 1096 SBP2_INFO("Number of active logins: %d", active_logins);
1da177e4
LT
1097
1098 if (active_logins >= max_logins) {
a237f35f 1099 return -EIO;
1da177e4
LT
1100 }
1101
1102 return 0;
1103}
1104
1da177e4
LT
1105static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1106{
ca0c7453 1107 struct sbp2_fwhost_info *hi = scsi_id->hi;
1da177e4
LT
1108 quadlet_t data[2];
1109
edf1fb21 1110 if (!scsi_id->login_orb)
a237f35f 1111 return -EIO;
1da177e4 1112
ca0c7453
SR
1113 if (!sbp2_exclusive_login && sbp2_query_logins(scsi_id)) {
1114 SBP2_INFO("Device does not support any more concurrent logins");
1115 return -EIO;
1da177e4
LT
1116 }
1117
e8ca5668 1118 /* assume no password */
1da177e4
LT
1119 scsi_id->login_orb->password_hi = 0;
1120 scsi_id->login_orb->password_lo = 0;
1da177e4
LT
1121
1122 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1123 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1da177e4 1124 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
e8ca5668
SR
1125
1126 /* one second reconnect time */
1127 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
ca0c7453 1128 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
e8ca5668 1129 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
ca0c7453 1130 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->lun);
1da177e4
LT
1131
1132 scsi_id->login_orb->passwd_resp_lengths =
1133 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1da177e4 1134
35bdddb8
SR
1135 scsi_id->login_orb->status_fifo_hi =
1136 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1137 scsi_id->login_orb->status_fifo_lo =
1138 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1da177e4 1139
1da177e4
LT
1140 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1141
1da177e4 1142 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1da177e4 1143
1da177e4
LT
1144 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1145 data[1] = scsi_id->login_orb_dma;
1146 sbp2util_cpu_to_be32_buffer(data, 8);
1147
ca0c7453 1148 hpsb_node_write(scsi_id->ne, scsi_id->management_agent_addr, data, 8);
1da177e4 1149
e8ca5668 1150 /* wait up to 20 seconds for login status */
e8398bb7
SR
1151 if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
1152 SBP2_ERR("Error logging into SBP-2 device - timed out");
a237f35f 1153 return -EIO;
1da177e4
LT
1154 }
1155
e8ca5668 1156 /* make sure that the returned status matches the login ORB */
1da177e4 1157 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
6065772d 1158 SBP2_ERR("Error logging into SBP-2 device - timed out");
a237f35f 1159 return -EIO;
1da177e4
LT
1160 }
1161
6065772d
SR
1162 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1163 SBP2_ERR("Error logging into SBP-2 device - failed");
a237f35f 1164 return -EIO;
1da177e4
LT
1165 }
1166
1da177e4 1167 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
ca0c7453 1168 scsi_id->command_block_agent_addr =
1da177e4 1169 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
ca0c7453
SR
1170 scsi_id->command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1171 scsi_id->command_block_agent_addr &= 0x0000ffffffffffffULL;
1da177e4
LT
1172
1173 SBP2_INFO("Logged into SBP-2 device");
a237f35f 1174 return 0;
1da177e4
LT
1175}
1176
1da177e4
LT
1177static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1178{
ca0c7453 1179 struct sbp2_fwhost_info *hi = scsi_id->hi;
1da177e4
LT
1180 quadlet_t data[2];
1181 int error;
1182
1da177e4
LT
1183 scsi_id->logout_orb->reserved1 = 0x0;
1184 scsi_id->logout_orb->reserved2 = 0x0;
1185 scsi_id->logout_orb->reserved3 = 0x0;
1186 scsi_id->logout_orb->reserved4 = 0x0;
1187
1188 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1189 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1da177e4
LT
1190 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1191
1192 scsi_id->logout_orb->reserved5 = 0x0;
35bdddb8
SR
1193 scsi_id->logout_orb->status_fifo_hi =
1194 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1195 scsi_id->logout_orb->status_fifo_lo =
1196 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1da177e4 1197
1da177e4
LT
1198 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1199
1da177e4
LT
1200 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1201 data[1] = scsi_id->logout_orb_dma;
1202 sbp2util_cpu_to_be32_buffer(data, 8);
1203
1da177e4 1204 error = hpsb_node_write(scsi_id->ne,
ca0c7453 1205 scsi_id->management_agent_addr, data, 8);
1da177e4
LT
1206 if (error)
1207 return error;
1208
e8ca5668 1209 /* wait up to 1 second for the device to complete logout */
e8398bb7 1210 if (sbp2util_access_timeout(scsi_id, HZ))
1da177e4
LT
1211 return -EIO;
1212
1213 SBP2_INFO("Logged out of SBP-2 device");
a237f35f 1214 return 0;
1da177e4
LT
1215}
1216
1da177e4
LT
1217static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1218{
ca0c7453 1219 struct sbp2_fwhost_info *hi = scsi_id->hi;
1da177e4
LT
1220 quadlet_t data[2];
1221 int error;
1222
1da177e4
LT
1223 scsi_id->reconnect_orb->reserved1 = 0x0;
1224 scsi_id->reconnect_orb->reserved2 = 0x0;
1225 scsi_id->reconnect_orb->reserved3 = 0x0;
1226 scsi_id->reconnect_orb->reserved4 = 0x0;
1227
1228 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1229 scsi_id->reconnect_orb->login_ID_misc |=
1230 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1da177e4
LT
1231 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1232
1233 scsi_id->reconnect_orb->reserved5 = 0x0;
35bdddb8
SR
1234 scsi_id->reconnect_orb->status_fifo_hi =
1235 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1236 scsi_id->reconnect_orb->status_fifo_lo =
1237 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1da177e4 1238
1da177e4
LT
1239 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1240
1da177e4
LT
1241 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1242 data[1] = scsi_id->reconnect_orb_dma;
1243 sbp2util_cpu_to_be32_buffer(data, 8);
1244
1da177e4 1245 error = hpsb_node_write(scsi_id->ne,
ca0c7453 1246 scsi_id->management_agent_addr, data, 8);
1da177e4
LT
1247 if (error)
1248 return error;
1249
e8ca5668 1250 /* wait up to 1 second for reconnect status */
e8398bb7
SR
1251 if (sbp2util_access_timeout(scsi_id, HZ)) {
1252 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
a237f35f 1253 return -EIO;
1da177e4
LT
1254 }
1255
e8ca5668 1256 /* make sure that the returned status matches the reconnect ORB */
1da177e4 1257 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
6065772d 1258 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
a237f35f 1259 return -EIO;
1da177e4
LT
1260 }
1261
6065772d
SR
1262 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1263 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
a237f35f 1264 return -EIO;
1da177e4
LT
1265 }
1266
35644090 1267 SBP2_INFO("Reconnected to SBP-2 device");
a237f35f 1268 return 0;
1da177e4
LT
1269}
1270
1271/*
e8ca5668
SR
1272 * Set the target node's Single Phase Retry limit. Affects the target's retry
1273 * behaviour if our node is too busy to accept requests.
1da177e4
LT
1274 */
1275static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1276{
1277 quadlet_t data;
1278
1da177e4 1279 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
d024ebc6
SR
1280 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1281 SBP2_ERR("%s error", __FUNCTION__);
a237f35f 1282 return 0;
1da177e4
LT
1283}
1284
1da177e4
LT
1285static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1286 struct unit_directory *ud)
1287{
1288 struct csr1212_keyval *kv;
1289 struct csr1212_dentry *dentry;
1290 u64 management_agent_addr;
9117c6dc 1291 u32 unit_characteristics, firmware_revision;
24d3bf88 1292 unsigned workarounds;
1da177e4
LT
1293 int i;
1294
9117c6dc
SR
1295 management_agent_addr = 0;
1296 unit_characteristics = 0;
1297 firmware_revision = 0;
1da177e4 1298
1da177e4
LT
1299 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1300 switch (kv->key.id) {
1301 case CSR1212_KV_ID_DEPENDENT_INFO:
edf1fb21 1302 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
1da177e4 1303 management_agent_addr =
a237f35f
SR
1304 CSR1212_REGISTER_SPACE_BASE +
1305 (kv->value.csr_offset << 2);
1da177e4 1306
edf1fb21 1307 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
ca0c7453 1308 scsi_id->lun =
a237f35f 1309 ORB_SET_LUN(kv->value.immediate);
1da177e4
LT
1310 break;
1311
1da177e4 1312 case SBP2_UNIT_CHARACTERISTICS_KEY:
e8ca5668
SR
1313 /* FIXME: This is ignored so far.
1314 * See SBP-2 clause 7.4.8. */
1da177e4 1315 unit_characteristics = kv->value.immediate;
1da177e4
LT
1316 break;
1317
1318 case SBP2_FIRMWARE_REVISION_KEY:
1da177e4 1319 firmware_revision = kv->value.immediate;
1da177e4
LT
1320 break;
1321
1322 default:
e8ca5668
SR
1323 /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1324 * Its "ordered" bit has consequences for command ORB
1325 * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
1da177e4
LT
1326 break;
1327 }
1328 }
1329
24d3bf88 1330 workarounds = sbp2_default_workarounds;
1da177e4 1331
679c0cd2
SR
1332 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1333 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1334 if (sbp2_workarounds_table[i].firmware_revision &&
1335 sbp2_workarounds_table[i].firmware_revision !=
1336 (firmware_revision & 0xffff00))
1337 continue;
1338 if (sbp2_workarounds_table[i].model_id &&
1339 sbp2_workarounds_table[i].model_id != ud->model_id)
1340 continue;
1341 workarounds |= sbp2_workarounds_table[i].workarounds;
1342 break;
1343 }
1da177e4 1344
24d3bf88 1345 if (workarounds)
e9a1c52c
SR
1346 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1347 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1348 " model_id 0x%06x)",
24d3bf88 1349 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
e9a1c52c
SR
1350 workarounds, firmware_revision,
1351 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1352 ud->model_id);
24d3bf88
SR
1353
1354 /* We would need one SCSI host template for each target to adjust
1355 * max_sectors on the fly, therefore warn only. */
1356 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
ca0c7453 1357 (sbp2_max_sectors * 512) > (128 * 1024))
35644090 1358 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
24d3bf88
SR
1359 "max transfer size. WARNING: Current max_sectors "
1360 "setting is larger than 128KB (%d sectors)",
1361 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
ca0c7453 1362 sbp2_max_sectors);
24d3bf88 1363
1da177e4
LT
1364 /* If this is a logical unit directory entry, process the parent
1365 * to get the values. */
1366 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1367 struct unit_directory *parent_ud =
1368 container_of(ud->device.parent, struct unit_directory, device);
1369 sbp2_parse_unit_directory(scsi_id, parent_ud);
1370 } else {
ca0c7453 1371 scsi_id->management_agent_addr = management_agent_addr;
1da177e4
LT
1372 scsi_id->workarounds = workarounds;
1373 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
ca0c7453 1374 scsi_id->lun = ORB_SET_LUN(ud->lun);
1da177e4
LT
1375 }
1376}
1377
fd23ade8
BC
1378#define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1379
1da177e4
LT
1380/*
1381 * This function is called in order to determine the max speed and packet
1382 * size we can use in our ORBs. Note, that we (the driver and host) only
1383 * initiate the transaction. The SBP-2 device actually transfers the data
1384 * (by reading from the DMA area we tell it). This means that the SBP-2
1385 * device decides the actual maximum data it can transfer. We just tell it
1386 * the speed that it needs to use, and the max_rec the host supports, and
1387 * it takes care of the rest.
1388 */
1389static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1390{
ca0c7453 1391 struct sbp2_fwhost_info *hi = scsi_id->hi;
fd23ade8 1392 u8 payload;
1da177e4 1393
a237f35f 1394 scsi_id->speed_code =
647dcb5f 1395 hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
1da177e4 1396
ca0c7453
SR
1397 if (scsi_id->speed_code > sbp2_max_speed) {
1398 scsi_id->speed_code = sbp2_max_speed;
1399 SBP2_INFO("Reducing speed to %s",
1400 hpsb_speedto_str[sbp2_max_speed]);
1da177e4
LT
1401 }
1402
1403 /* Payload size is the lesser of what our speed supports and what
1404 * our host supports. */
fd23ade8
BC
1405 payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1406 (u8) (hi->host->csr.max_rec - 1));
1407
1408 /* If physical DMA is off, work around limitation in ohci1394:
1409 * packet size must not exceed PAGE_SIZE */
1410 if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
1411 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1412 payload)
1413 payload--;
1da177e4 1414
35644090
SR
1415 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1416 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1417 hpsb_speedto_str[scsi_id->speed_code],
1418 SBP2_PAYLOAD_TO_BYTES(payload));
1da177e4 1419
fd23ade8 1420 scsi_id->max_payload_size = payload;
a237f35f 1421 return 0;
1da177e4
LT
1422}
1423
1da177e4
LT
1424static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1425{
1426 quadlet_t data;
1427 u64 addr;
1428 int retval;
cc078189 1429 unsigned long flags;
1da177e4 1430
d19c7764 1431 /* cancel_delayed_work(&scsi_id->protocol_work); */
09ee67ab
SR
1432 if (wait)
1433 flush_scheduled_work();
1434
1da177e4 1435 data = ntohl(SBP2_AGENT_RESET_DATA);
ca0c7453 1436 addr = scsi_id->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1da177e4
LT
1437
1438 if (wait)
1439 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1440 else
1441 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1442
1443 if (retval < 0) {
1444 SBP2_ERR("hpsb_node_write failed.\n");
1445 return -EIO;
1446 }
1447
e8ca5668 1448 /* make sure that the ORB_POINTER is written on next command */
ca0c7453 1449 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1da177e4 1450 scsi_id->last_orb = NULL;
ca0c7453 1451 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1da177e4 1452
a237f35f 1453 return 0;
1da177e4
LT
1454}
1455
cf8d2c09 1456static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
ca0c7453 1457 struct sbp2_fwhost_info *hi,
cf8d2c09
SR
1458 struct sbp2_command_info *command,
1459 unsigned int scsi_use_sg,
1460 struct scatterlist *sgpnt,
1461 u32 orb_direction,
1462 enum dma_data_direction dma_dir)
1463{
1464 command->dma_dir = dma_dir;
1465 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1466 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1467
e8ca5668 1468 /* special case if only one element (and less than 64KB in size) */
cf8d2c09
SR
1469 if ((scsi_use_sg == 1) &&
1470 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1471
cf8d2c09
SR
1472 command->dma_size = sgpnt[0].length;
1473 command->dma_type = CMD_DMA_PAGE;
1474 command->cmd_dma = pci_map_page(hi->host->pdev,
1475 sgpnt[0].page,
1476 sgpnt[0].offset,
1477 command->dma_size,
1478 command->dma_dir);
cf8d2c09
SR
1479
1480 orb->data_descriptor_lo = command->cmd_dma;
1481 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1482
1483 } else {
1484 struct sbp2_unrestricted_page_table *sg_element =
1485 &command->scatter_gather_element[0];
1486 u32 sg_count, sg_len;
1487 dma_addr_t sg_addr;
1488 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
1489 dma_dir);
1490
cf8d2c09
SR
1491 command->dma_size = scsi_use_sg;
1492 command->sge_buffer = sgpnt;
1493
1494 /* use page tables (s/g) */
1495 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1496 orb->data_descriptor_lo = command->sge_dma;
1497
e8ca5668
SR
1498 /* loop through and fill out our SBP-2 page tables
1499 * (and split up anything too large) */
cf8d2c09
SR
1500 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1501 sg_len = sg_dma_len(sgpnt);
1502 sg_addr = sg_dma_address(sgpnt);
1503 while (sg_len) {
1504 sg_element[sg_count].segment_base_lo = sg_addr;
1505 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1506 sg_element[sg_count].length_segment_base_hi =
1507 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1508 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1509 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1510 } else {
1511 sg_element[sg_count].length_segment_base_hi =
1512 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1513 sg_len = 0;
1514 }
1515 sg_count++;
1516 }
1517 }
1518
cf8d2c09
SR
1519 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1520
cf8d2c09
SR
1521 sbp2util_cpu_to_be32_buffer(sg_element,
1522 (sizeof(struct sbp2_unrestricted_page_table)) *
1523 sg_count);
1524 }
1525}
1526
1527static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
ca0c7453 1528 struct sbp2_fwhost_info *hi,
cf8d2c09
SR
1529 struct sbp2_command_info *command,
1530 struct scatterlist *sgpnt,
1531 u32 orb_direction,
1532 unsigned int scsi_request_bufflen,
1533 void *scsi_request_buffer,
1534 enum dma_data_direction dma_dir)
1535{
1536 command->dma_dir = dma_dir;
1537 command->dma_size = scsi_request_bufflen;
1538 command->dma_type = CMD_DMA_SINGLE;
1539 command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
1540 command->dma_size, command->dma_dir);
1541 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1542 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1543
e8ca5668
SR
1544 /* handle case where we get a command w/o s/g enabled
1545 * (but check for transfers larger than 64K) */
cf8d2c09
SR
1546 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1547
1548 orb->data_descriptor_lo = command->cmd_dma;
1549 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1550
1551 } else {
e8ca5668
SR
1552 /* The buffer is too large. Turn this into page tables. */
1553
cf8d2c09
SR
1554 struct sbp2_unrestricted_page_table *sg_element =
1555 &command->scatter_gather_element[0];
1556 u32 sg_count, sg_len;
1557 dma_addr_t sg_addr;
1558
cf8d2c09 1559 orb->data_descriptor_lo = command->sge_dma;
cf8d2c09
SR
1560 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1561
e8ca5668 1562 /* fill out our SBP-2 page tables; split up the large buffer */
cf8d2c09
SR
1563 sg_count = 0;
1564 sg_len = scsi_request_bufflen;
1565 sg_addr = command->cmd_dma;
1566 while (sg_len) {
1567 sg_element[sg_count].segment_base_lo = sg_addr;
1568 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1569 sg_element[sg_count].length_segment_base_hi =
1570 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1571 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1572 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1573 } else {
1574 sg_element[sg_count].length_segment_base_hi =
1575 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1576 sg_len = 0;
1577 }
1578 sg_count++;
1579 }
1580
cf8d2c09
SR
1581 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1582
cf8d2c09
SR
1583 sbp2util_cpu_to_be32_buffer(sg_element,
1584 (sizeof(struct sbp2_unrestricted_page_table)) *
1585 sg_count);
1586 }
1587}
1588
cf8d2c09
SR
1589static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1590 struct sbp2_command_info *command,
1591 unchar *scsi_cmd,
1592 unsigned int scsi_use_sg,
1593 unsigned int scsi_request_bufflen,
1594 void *scsi_request_buffer,
1595 enum dma_data_direction dma_dir)
1da177e4 1596{
ca0c7453 1597 struct sbp2_fwhost_info *hi = scsi_id->hi;
a237f35f 1598 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
1da177e4 1599 struct sbp2_command_orb *command_orb = &command->command_orb;
cf8d2c09 1600 u32 orb_direction;
1da177e4
LT
1601
1602 /*
e8ca5668 1603 * Set-up our command ORB.
1da177e4
LT
1604 *
1605 * NOTE: We're doing unrestricted page tables (s/g), as this is
1606 * best performance (at least with the devices I have). This means
1607 * that data_size becomes the number of s/g elements, and
1608 * page_size should be zero (for unrestricted).
1609 */
1610 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1611 command_orb->next_ORB_lo = 0x0;
1612 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1613 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
e8ca5668 1614 command_orb->misc |= ORB_SET_NOTIFY(1);
1da177e4 1615
43863eba 1616 if (dma_dir == DMA_NONE)
a237f35f 1617 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
43863eba 1618 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
a237f35f 1619 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
43863eba 1620 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
a237f35f 1621 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
43863eba 1622 else {
35644090 1623 SBP2_INFO("Falling back to DMA_NONE");
43863eba 1624 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1da177e4
LT
1625 }
1626
e8ca5668 1627 /* set up our page table stuff */
1da177e4 1628 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1da177e4
LT
1629 command_orb->data_descriptor_hi = 0x0;
1630 command_orb->data_descriptor_lo = 0x0;
1631 command_orb->misc |= ORB_SET_DIRECTION(1);
edf1fb21 1632 } else if (scsi_use_sg)
cf8d2c09
SR
1633 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
1634 sgpnt, orb_direction, dma_dir);
edf1fb21 1635 else
cf8d2c09
SR
1636 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
1637 orb_direction, scsi_request_bufflen,
1638 scsi_request_buffer, dma_dir);
1da177e4 1639
1da177e4
LT
1640 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1641
1da177e4
LT
1642 memset(command_orb->cdb, 0, 12);
1643 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1da177e4
LT
1644}
1645
28212767 1646static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
1da177e4
LT
1647 struct sbp2_command_info *command)
1648{
ca0c7453 1649 struct sbp2_fwhost_info *hi = scsi_id->hi;
1da177e4 1650 struct sbp2_command_orb *command_orb = &command->command_orb;
cc078189
SR
1651 struct sbp2_command_orb *last_orb;
1652 dma_addr_t last_orb_dma;
ca0c7453 1653 u64 addr = scsi_id->command_block_agent_addr;
cc078189
SR
1654 quadlet_t data[2];
1655 size_t length;
1656 unsigned long flags;
1da177e4 1657
1da177e4
LT
1658 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
1659 sizeof(struct sbp2_command_orb),
d4018d7f 1660 PCI_DMA_TODEVICE);
1da177e4
LT
1661 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
1662 sizeof(command->scatter_gather_element),
1663 PCI_DMA_BIDIRECTIONAL);
e8ca5668
SR
1664
1665 /* check to see if there are any previous orbs to use */
ca0c7453 1666 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
cc078189
SR
1667 last_orb = scsi_id->last_orb;
1668 last_orb_dma = scsi_id->last_orb_dma;
1669 if (!last_orb) {
1da177e4 1670 /*
cc078189
SR
1671 * last_orb == NULL means: We know that the target's fetch agent
1672 * is not active right now.
1da177e4 1673 */
cc078189 1674 addr += SBP2_ORB_POINTER_OFFSET;
1da177e4
LT
1675 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1676 data[1] = command->command_orb_dma;
1677 sbp2util_cpu_to_be32_buffer(data, 8);
cc078189 1678 length = 8;
1da177e4 1679 } else {
1da177e4 1680 /*
cc078189
SR
1681 * last_orb != NULL means: We know that the target's fetch agent
1682 * is (very probably) not dead or in reset state right now.
1683 * We have an ORB already sent that we can append a new one to.
1684 * The target's fetch agent may or may not have read this
1685 * previous ORB yet.
1da177e4 1686 */
cc078189
SR
1687 pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
1688 sizeof(struct sbp2_command_orb),
d4018d7f 1689 PCI_DMA_TODEVICE);
cc078189
SR
1690 last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
1691 wmb();
1da177e4 1692 /* Tells hardware that this pointer is valid */
cc078189
SR
1693 last_orb->next_ORB_hi = 0;
1694 pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
1da177e4 1695 sizeof(struct sbp2_command_orb),
d4018d7f 1696 PCI_DMA_TODEVICE);
cc078189
SR
1697 addr += SBP2_DOORBELL_OFFSET;
1698 data[0] = 0;
1699 length = 4;
1700 }
1701 scsi_id->last_orb = command_orb;
1702 scsi_id->last_orb_dma = command->command_orb_dma;
ca0c7453 1703 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1da177e4 1704
09ee67ab
SR
1705 if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
1706 /*
1707 * sbp2util_node_write_no_wait failed. We certainly ran out
1708 * of transaction labels, perhaps just because there were no
1709 * context switches which gave khpsbpkt a chance to collect
1710 * free tlabels. Try again in non-atomic context. If necessary,
1711 * the workqueue job will sleep to guaranteedly get a tlabel.
1712 * We do not accept new commands until the job is over.
1713 */
1714 scsi_block_requests(scsi_id->scsi_host);
d19c7764 1715 PREPARE_WORK(&scsi_id->protocol_work,
09ee67ab 1716 last_orb ? sbp2util_write_doorbell:
d19c7764
SR
1717 sbp2util_write_orb_pointer
1718 /* */);
1719 schedule_work(&scsi_id->protocol_work);
09ee67ab 1720 }
1da177e4
LT
1721}
1722
1da177e4
LT
1723static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
1724 struct scsi_cmnd *SCpnt,
1725 void (*done)(struct scsi_cmnd *))
1726{
1727 unchar *cmd = (unchar *) SCpnt->cmnd;
1728 unsigned int request_bufflen = SCpnt->request_bufflen;
1729 struct sbp2_command_info *command;
1730
1da177e4 1731 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
edf1fb21 1732 if (!command)
a237f35f 1733 return -EIO;
1da177e4 1734
1da177e4
LT
1735 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
1736 request_bufflen, SCpnt->request_buffer,
1737 SCpnt->sc_data_direction);
1da177e4
LT
1738 sbp2_link_orb_command(scsi_id, command);
1739
a237f35f 1740 return 0;
1da177e4
LT
1741}
1742
1da177e4
LT
1743/*
1744 * Translates SBP-2 status into SCSI sense data for check conditions
1745 */
1746static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
1747{
e8ca5668 1748 /* OK, it's pretty ugly... ;-) */
1da177e4
LT
1749 sense_data[0] = 0x70;
1750 sense_data[1] = 0x0;
1751 sense_data[2] = sbp2_status[9];
1752 sense_data[3] = sbp2_status[12];
1753 sense_data[4] = sbp2_status[13];
1754 sense_data[5] = sbp2_status[14];
1755 sense_data[6] = sbp2_status[15];
1756 sense_data[7] = 10;
1757 sense_data[8] = sbp2_status[16];
1758 sense_data[9] = sbp2_status[17];
1759 sense_data[10] = sbp2_status[18];
1760 sense_data[11] = sbp2_status[19];
1761 sense_data[12] = sbp2_status[10];
1762 sense_data[13] = sbp2_status[11];
1763 sense_data[14] = sbp2_status[20];
1764 sense_data[15] = sbp2_status[21];
1765
e8ca5668 1766 return sbp2_status[8] & 0x3f;
1da177e4
LT
1767}
1768
3e98eab4
SR
1769static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1770 int destid, quadlet_t *data, u64 addr,
1771 size_t length, u16 fl)
1da177e4 1772{
ca0c7453 1773 struct sbp2_fwhost_info *hi;
1da177e4 1774 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
1da177e4 1775 struct scsi_cmnd *SCpnt = NULL;
3e98eab4 1776 struct sbp2_status_block *sb;
1da177e4
LT
1777 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
1778 struct sbp2_command_info *command;
79456197 1779 unsigned long flags;
1da177e4 1780
6065772d
SR
1781 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1782 SBP2_ERR("Wrong size of status block");
1783 return RCODE_ADDRESS_ERROR;
1784 }
1785 if (unlikely(!host)) {
1da177e4 1786 SBP2_ERR("host is NULL - this is bad!");
a237f35f 1787 return RCODE_ADDRESS_ERROR;
1da177e4 1788 }
1da177e4 1789 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
6065772d 1790 if (unlikely(!hi)) {
1da177e4 1791 SBP2_ERR("host info is NULL - this is bad!");
a237f35f 1792 return RCODE_ADDRESS_ERROR;
1da177e4 1793 }
e8ca5668
SR
1794
1795 /* Find the unit which wrote the status. */
1da177e4 1796 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
35bdddb8
SR
1797 if (scsi_id_tmp->ne->nodeid == nodeid &&
1798 scsi_id_tmp->status_fifo_addr == addr) {
1da177e4
LT
1799 scsi_id = scsi_id_tmp;
1800 break;
1801 }
1802 }
6065772d 1803 if (unlikely(!scsi_id)) {
1da177e4 1804 SBP2_ERR("scsi_id is NULL - device is gone?");
a237f35f 1805 return RCODE_ADDRESS_ERROR;
1da177e4
LT
1806 }
1807
e8ca5668 1808 /* Put response into scsi_id status fifo buffer. The first two bytes
3e98eab4
SR
1809 * come in big endian bit order. Often the target writes only a
1810 * truncated status block, minimally the first two quadlets. The rest
e8ca5668 1811 * is implied to be zeros. */
3e98eab4
SR
1812 sb = &scsi_id->status_block;
1813 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1814 memcpy(sb, data, length);
1815 sbp2util_be32_to_cpu_buffer(sb, 8);
1da177e4 1816
e8ca5668 1817 /* Ignore unsolicited status. Handle command ORB status. */
6065772d
SR
1818 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
1819 command = NULL;
1820 else
1821 command = sbp2util_find_command_for_orb(scsi_id,
1822 sb->ORB_offset_lo);
1da177e4 1823 if (command) {
1da177e4
LT
1824 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1825 sizeof(struct sbp2_command_orb),
d4018d7f 1826 PCI_DMA_TODEVICE);
1da177e4
LT
1827 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1828 sizeof(command->scatter_gather_element),
1829 PCI_DMA_BIDIRECTIONAL);
e8ca5668 1830 /* Grab SCSI command pointers and check status. */
6065772d
SR
1831 /*
1832 * FIXME: If the src field in the status is 1, the ORB DMA must
1833 * not be reused until status for a subsequent ORB is received.
1834 */
1da177e4 1835 SCpnt = command->Current_SCpnt;
ca0c7453 1836 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1da177e4 1837 sbp2util_mark_command_completed(scsi_id, command);
ca0c7453 1838 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1da177e4
LT
1839
1840 if (SCpnt) {
abbca103
SR
1841 u32 h = sb->ORB_offset_hi_misc;
1842 u32 r = STATUS_GET_RESP(h);
1843
1844 if (r != RESP_STATUS_REQUEST_COMPLETE) {
35644090 1845 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
abbca103 1846 r, STATUS_GET_SBP_STATUS(h));
6065772d 1847 scsi_status =
abbca103
SR
1848 r == RESP_STATUS_TRANSPORT_FAILURE ?
1849 SBP2_SCSI_STATUS_BUSY :
6065772d 1850 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
abbca103 1851 }
e8ca5668 1852
edf1fb21 1853 if (STATUS_GET_LEN(h) > 1)
3e98eab4
SR
1854 scsi_status = sbp2_status_to_sense_data(
1855 (unchar *)sb, SCpnt->sense_buffer);
e8ca5668 1856
edf1fb21 1857 if (STATUS_TEST_DEAD(h))
1da177e4 1858 sbp2_agent_reset(scsi_id, 0);
1da177e4
LT
1859 }
1860
e8ca5668 1861 /* Check here to see if there are no commands in-use. If there
cc078189
SR
1862 * are none, we know that the fetch agent left the active state
1863 * _and_ that we did not reactivate it yet. Therefore clear
1864 * last_orb so that next time we write directly to the
1865 * ORB_POINTER register. That way the fetch agent does not need
e8ca5668 1866 * to refetch the next_ORB. */
ca0c7453
SR
1867 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1868 if (list_empty(&scsi_id->cmd_orb_inuse))
1da177e4 1869 scsi_id->last_orb = NULL;
ca0c7453 1870 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1da177e4
LT
1871
1872 } else {
e8ca5668 1873 /* It's probably status after a management request. */
3e98eab4
SR
1874 if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
1875 (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
1876 (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
e8398bb7
SR
1877 (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
1878 scsi_id->access_complete = 1;
ca0c7453 1879 wake_up_interruptible(&sbp2_access_wq);
e8398bb7 1880 }
1da177e4
LT
1881 }
1882
edf1fb21 1883 if (SCpnt)
1da177e4
LT
1884 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
1885 command->Current_done);
a237f35f 1886 return RCODE_COMPLETE;
1da177e4
LT
1887}
1888
1da177e4
LT
1889/**************************************
1890 * SCSI interface related section
1891 **************************************/
1892
1da177e4
LT
1893static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1894 void (*done)(struct scsi_cmnd *))
1895{
1896 struct scsi_id_instance_data *scsi_id =
1897 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
ca0c7453 1898 struct sbp2_fwhost_info *hi;
abd559b1 1899 int result = DID_NO_CONNECT << 16;
1da177e4 1900
5796aa7b 1901 if (unlikely(!sbp2util_node_is_available(scsi_id)))
abd559b1 1902 goto done;
1da177e4
LT
1903
1904 hi = scsi_id->hi;
1905
5796aa7b 1906 if (unlikely(!hi)) {
ca0c7453 1907 SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
abd559b1 1908 goto done;
1da177e4
LT
1909 }
1910
e8ca5668
SR
1911 /* Multiple units are currently represented to the SCSI core as separate
1912 * targets, not as one target with multiple LUs. Therefore return
1913 * selection time-out to any IO directed at non-zero LUNs. */
5796aa7b 1914 if (unlikely(SCpnt->device->lun))
abd559b1 1915 goto done;
1da177e4 1916
e8ca5668 1917 /* handle the request sense command here (auto-request sense) */
1da177e4 1918 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
1da177e4
LT
1919 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
1920 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
1921 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
abd559b1 1922 return 0;
1da177e4
LT
1923 }
1924
5796aa7b 1925 if (unlikely(!hpsb_node_entry_valid(scsi_id->ne))) {
1da177e4 1926 SBP2_ERR("Bus reset in progress - rejecting command");
abd559b1
JM
1927 result = DID_BUS_BUSY << 16;
1928 goto done;
1da177e4
LT
1929 }
1930
e8ca5668
SR
1931 /* Bidirectional commands are not yet implemented,
1932 * and unknown transfer direction not handled. */
5796aa7b 1933 if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
43863eba
SR
1934 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1935 result = DID_ERROR << 16;
1936 goto done;
1937 }
1938
1da177e4
LT
1939 if (sbp2_send_command(scsi_id, SCpnt, done)) {
1940 SBP2_ERR("Error sending SCSI command");
1941 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1942 SCpnt, done);
1943 }
abd559b1 1944 return 0;
1da177e4 1945
abd559b1
JM
1946done:
1947 SCpnt->result = result;
1948 done(SCpnt);
1949 return 0;
1da177e4
LT
1950}
1951
1da177e4
LT
1952static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
1953 u32 status)
1954{
ca0c7453 1955 struct sbp2_fwhost_info *hi = scsi_id->hi;
1da177e4
LT
1956 struct list_head *lh;
1957 struct sbp2_command_info *command;
79456197 1958 unsigned long flags;
1da177e4 1959
ca0c7453
SR
1960 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1961 while (!list_empty(&scsi_id->cmd_orb_inuse)) {
1962 lh = scsi_id->cmd_orb_inuse.next;
1da177e4
LT
1963 command = list_entry(lh, struct sbp2_command_info, list);
1964 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1965 sizeof(struct sbp2_command_orb),
d4018d7f 1966 PCI_DMA_TODEVICE);
1da177e4
LT
1967 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1968 sizeof(command->scatter_gather_element),
1969 PCI_DMA_BIDIRECTIONAL);
1970 sbp2util_mark_command_completed(scsi_id, command);
1971 if (command->Current_SCpnt) {
1972 command->Current_SCpnt->result = status << 16;
1973 command->Current_done(command->Current_SCpnt);
1974 }
1975 }
ca0c7453 1976 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1da177e4
LT
1977
1978 return;
1979}
1980
1981/*
e8ca5668 1982 * Complete a regular SCSI command. Can be called in atomic context.
1da177e4
LT
1983 */
1984static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
1985 u32 scsi_status, struct scsi_cmnd *SCpnt,
1986 void (*done)(struct scsi_cmnd *))
1987{
1da177e4
LT
1988 if (!SCpnt) {
1989 SBP2_ERR("SCpnt is NULL");
1990 return;
1991 }
1992
1da177e4 1993 switch (scsi_status) {
a237f35f 1994 case SBP2_SCSI_STATUS_GOOD:
8f0525ff 1995 SCpnt->result = DID_OK << 16;
a237f35f 1996 break;
1da177e4 1997
a237f35f
SR
1998 case SBP2_SCSI_STATUS_BUSY:
1999 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2000 SCpnt->result = DID_BUS_BUSY << 16;
2001 break;
1da177e4 2002
a237f35f 2003 case SBP2_SCSI_STATUS_CHECK_CONDITION:
8f0525ff 2004 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
a237f35f 2005 break;
1da177e4 2006
a237f35f
SR
2007 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2008 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2009 SCpnt->result = DID_NO_CONNECT << 16;
2010 scsi_print_command(SCpnt);
2011 break;
1da177e4 2012
a237f35f
SR
2013 case SBP2_SCSI_STATUS_CONDITION_MET:
2014 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2015 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2016 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2017 SCpnt->result = DID_ERROR << 16;
2018 scsi_print_command(SCpnt);
2019 break;
1da177e4 2020
a237f35f
SR
2021 default:
2022 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2023 SCpnt->result = DID_ERROR << 16;
1da177e4
LT
2024 }
2025
e8ca5668
SR
2026 /* If a bus reset is in progress and there was an error, complete
2027 * the command as busy so that it will get retried. */
a237f35f
SR
2028 if (!hpsb_node_entry_valid(scsi_id->ne)
2029 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
1da177e4
LT
2030 SBP2_ERR("Completing command with busy (bus reset)");
2031 SCpnt->result = DID_BUS_BUSY << 16;
2032 }
2033
e8ca5668 2034 /* Tell the SCSI stack that we're done with this command. */
a237f35f 2035 done(SCpnt);
1da177e4
LT
2036}
2037
abd559b1 2038static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
1da177e4 2039{
a80614d1
SR
2040 struct scsi_id_instance_data *scsi_id =
2041 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2042
2043 scsi_id->sdev = sdev;
c394f1ea 2044 sdev->allow_restart = 1;
a80614d1 2045
24d3bf88 2046 if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
a80614d1 2047 sdev->inquiry_len = 36;
abd559b1
JM
2048 return 0;
2049}
2050
abd559b1
JM
2051static int sbp2scsi_slave_configure(struct scsi_device *sdev)
2052{
24d3bf88
SR
2053 struct scsi_id_instance_data *scsi_id =
2054 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2055
abd559b1 2056 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
365c786f 2057 sdev->use_10_for_rw = 1;
24d3bf88
SR
2058
2059 if (sdev->type == TYPE_DISK &&
2060 scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2061 sdev->skip_ms_page_8 = 1;
e9a1c52c
SR
2062 if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2063 sdev->fix_capacity = 1;
1da177e4
LT
2064 return 0;
2065}
2066
abd559b1
JM
2067static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2068{
2069 ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
2070 return;
2071}
2072
1da177e4 2073/*
e8ca5668
SR
2074 * Called by scsi stack when something has really gone wrong.
2075 * Usually called when a command has timed-out for some reason.
1da177e4
LT
2076 */
2077static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2078{
2079 struct scsi_id_instance_data *scsi_id =
2080 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
ca0c7453 2081 struct sbp2_fwhost_info *hi = scsi_id->hi;
1da177e4 2082 struct sbp2_command_info *command;
24c7cd06 2083 unsigned long flags;
1da177e4 2084
35644090 2085 SBP2_INFO("aborting sbp2 command");
1da177e4
LT
2086 scsi_print_command(SCpnt);
2087
abd559b1 2088 if (sbp2util_node_is_available(scsi_id)) {
23077f1d 2089 sbp2_agent_reset(scsi_id, 1);
1da177e4 2090
23077f1d 2091 /* Return a matching command structure to the free pool. */
ca0c7453 2092 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1da177e4
LT
2093 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2094 if (command) {
1da177e4
LT
2095 pci_dma_sync_single_for_cpu(hi->host->pdev,
2096 command->command_orb_dma,
2097 sizeof(struct sbp2_command_orb),
d4018d7f 2098 PCI_DMA_TODEVICE);
1da177e4
LT
2099 pci_dma_sync_single_for_cpu(hi->host->pdev,
2100 command->sge_dma,
2101 sizeof(command->scatter_gather_element),
2102 PCI_DMA_BIDIRECTIONAL);
2103 sbp2util_mark_command_completed(scsi_id, command);
2104 if (command->Current_SCpnt) {
2105 command->Current_SCpnt->result = DID_ABORT << 16;
2106 command->Current_done(command->Current_SCpnt);
2107 }
2108 }
ca0c7453 2109 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1da177e4 2110
1da177e4
LT
2111 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2112 }
2113
a237f35f 2114 return SUCCESS;
1da177e4
LT
2115}
2116
2117/*
2118 * Called by scsi stack when something has really gone wrong.
2119 */
abd559b1 2120static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
1da177e4
LT
2121{
2122 struct scsi_id_instance_data *scsi_id =
2123 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2124
35644090 2125 SBP2_INFO("reset requested");
1da177e4 2126
abd559b1 2127 if (sbp2util_node_is_available(scsi_id)) {
35644090 2128 SBP2_INFO("generating sbp2 fetch agent reset");
1f427e80 2129 sbp2_agent_reset(scsi_id, 1);
1da177e4
LT
2130 }
2131
abd559b1 2132 return SUCCESS;
94d0e7b8
JG
2133}
2134
a237f35f
SR
2135static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2136 struct device_attribute *attr,
2137 char *buf)
1da177e4
LT
2138{
2139 struct scsi_device *sdev;
2140 struct scsi_id_instance_data *scsi_id;
1da177e4
LT
2141
2142 if (!(sdev = to_scsi_device(dev)))
2143 return 0;
2144
2145 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2146 return 0;
2147
1da177e4 2148 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
ca0c7453 2149 scsi_id->ud->id, ORB_SET_LUN(scsi_id->lun));
1da177e4 2150}
1da177e4
LT
2151
2152MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2153MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2154MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2155MODULE_LICENSE("GPL");
2156
1da177e4
LT
2157static int sbp2_module_init(void)
2158{
2159 int ret;
2160
ca0c7453
SR
2161 if (sbp2_serialize_io) {
2162 sbp2_shost_template.can_queue = 1;
2163 sbp2_shost_template.cmd_per_lun = 1;
1da177e4
LT
2164 }
2165
24d3bf88 2166 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
ca0c7453
SR
2167 (sbp2_max_sectors * 512) > (128 * 1024))
2168 sbp2_max_sectors = 128 * 1024 / 512;
2169 sbp2_shost_template.max_sectors = sbp2_max_sectors;
1da177e4 2170
1da177e4 2171 hpsb_register_highlevel(&sbp2_highlevel);
1da177e4
LT
2172 ret = hpsb_register_protocol(&sbp2_driver);
2173 if (ret) {
2174 SBP2_ERR("Failed to register protocol");
2175 hpsb_unregister_highlevel(&sbp2_highlevel);
2176 return ret;
2177 }
1da177e4
LT
2178 return 0;
2179}
2180
2181static void __exit sbp2_module_exit(void)
2182{
1da177e4 2183 hpsb_unregister_protocol(&sbp2_driver);
1da177e4
LT
2184 hpsb_unregister_highlevel(&sbp2_highlevel);
2185}
2186
2187module_init(sbp2_module_init);
2188module_exit(sbp2_module_exit);