2 * sbp2.c - SBP-2 protocol driver for IEEE-1394
4 * Copyright (C) 2000 James Goodwin, Filanet Corporation (www.filanet.com)
5 * jamesg@filanet.com (JSG)
7 * Copyright (C) 2003 Ben Collins <bcollins@debian.org>
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.
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.
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.
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.
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.).
37 * - Error Handling: SCSI aborts and bus reset requests are handled somewhat
38 * but the code needs additional debugging.
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>
48 #include <linux/kernel.h>
49 #include <linux/list.h>
50 #include <linux/module.h>
51 #include <linux/moduleparam.h>
52 #include <linux/pci.h>
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>
59 #include <linux/wait.h>
61 #include <asm/byteorder.h>
62 #include <asm/errno.h>
63 #include <asm/param.h>
64 #include <asm/scatterlist.h>
65 #include <asm/system.h>
66 #include <asm/types.h>
68 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
69 #include <asm/io.h> /* for bus_to_virt */
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>
79 #include "highlevel.h"
82 #include "ieee1394_core.h"
83 #include "ieee1394_hotplug.h"
84 #include "ieee1394_transactions.h"
85 #include "ieee1394_types.h"
90 * Module load parameter definitions
94 * Change max_speed on module load if you have a bad IEEE-1394
95 * controller that has trouble running 2KB packets at 400mb.
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.
101 static int sbp2_max_speed = IEEE1394_SPEED_MAX;
102 module_param_named(max_speed, sbp2_max_speed, int, 0644);
103 MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)");
106 * Set serialize_io to 1 if you'd like only one scsi command sent
107 * down to us at a time (debugging). This might be necessary for very
108 * badly behaved sbp2 devices.
110 * TODO: Make this configurable per device.
112 static int sbp2_serialize_io = 1;
113 module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
114 MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
115 "(default = 1, faster = 0)");
118 * Bump up max_sectors if you'd like to support very large sized
119 * transfers. Please note that some older sbp2 bridge chips are broken for
120 * transfers greater or equal to 128KB. Default is a value of 255
121 * sectors, or just under 128KB (at 512 byte sector size). I can note that
122 * the Oxsemi sbp2 chipsets have no problems supporting very large
125 static int sbp2_max_sectors = SBP2_MAX_SECTORS;
126 module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
127 MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
128 "(default = " __stringify(SBP2_MAX_SECTORS) ")");
131 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
132 * do an exclusive login, as it's generally unsafe to have two hosts
133 * talking to a single sbp2 device at the same time (filesystem coherency,
134 * etc.). If you're running an sbp2 device that supports multiple logins,
135 * and you're either running read-only filesystems or some sort of special
136 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
137 * File System, or Lustre, then set exclusive_login to zero.
139 * So far only bridges from Oxford Semiconductor are known to support
140 * concurrent logins. Depending on firmware, four or two concurrent logins
141 * are possible on OXFW911 and newer Oxsemi bridges.
143 static int sbp2_exclusive_login = 1;
144 module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
145 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
149 * If any of the following workarounds is required for your device to work,
150 * please submit the kernel messages logged by sbp2 to the linux1394-devel
153 * - 128kB max transfer
154 * Limit transfer size. Necessary for some old bridges.
157 * When scsi_mod probes the device, let the inquiry command look like that
161 * Suppress sending of mode_sense for mode page 8 if the device pretends to
162 * support the SCSI Primary Block commands instead of Reduced Block Commands.
165 * Tell sd_mod to correct the last sector number reported by read_capacity.
166 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
167 * Don't use this with devices which don't have this bug.
169 * - override internal blacklist
170 * Instead of adding to the built-in blacklist, use only the workarounds
171 * specified in the module load parameter.
172 * Useful if a blacklist entry interfered with a non-broken device.
174 static int sbp2_default_workarounds;
175 module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
176 MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
177 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
178 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
179 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
180 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
181 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
182 ", or a combination)");
185 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
186 #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
191 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *, u32);
192 static void sbp2scsi_complete_command(struct scsi_id_instance_data *, u32,
194 void (*)(struct scsi_cmnd *));
195 static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *);
196 static int sbp2_start_device(struct scsi_id_instance_data *);
197 static void sbp2_remove_device(struct scsi_id_instance_data *);
198 static int sbp2_login_device(struct scsi_id_instance_data *);
199 static int sbp2_reconnect_device(struct scsi_id_instance_data *);
200 static int sbp2_logout_device(struct scsi_id_instance_data *);
201 static void sbp2_host_reset(struct hpsb_host *);
202 static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
204 static int sbp2_agent_reset(struct scsi_id_instance_data *, int);
205 static void sbp2_parse_unit_directory(struct scsi_id_instance_data *,
206 struct unit_directory *);
207 static int sbp2_set_busy_timeout(struct scsi_id_instance_data *);
208 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *);
211 static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
213 static struct hpsb_highlevel sbp2_highlevel = {
214 .name = SBP2_DEVICE_NAME,
215 .host_reset = sbp2_host_reset,
218 static struct hpsb_address_ops sbp2_ops = {
219 .write = sbp2_handle_status_write
222 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
223 static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
225 static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
228 static struct hpsb_address_ops sbp2_physdma_ops = {
229 .read = sbp2_handle_physdma_read,
230 .write = sbp2_handle_physdma_write,
236 * Interface to driver core and IEEE 1394 core
238 static struct ieee1394_device_id sbp2_id_table[] = {
240 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
241 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
242 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
245 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
247 static int sbp2_probe(struct device *);
248 static int sbp2_remove(struct device *);
249 static int sbp2_update(struct unit_directory *);
251 static struct hpsb_protocol_driver sbp2_driver = {
252 .name = "SBP2 Driver",
253 .id_table = sbp2_id_table,
254 .update = sbp2_update,
256 .name = SBP2_DEVICE_NAME,
257 .bus = &ieee1394_bus_type,
259 .remove = sbp2_remove,
265 * Interface to SCSI core
267 static int sbp2scsi_queuecommand(struct scsi_cmnd *,
268 void (*)(struct scsi_cmnd *));
269 static int sbp2scsi_abort(struct scsi_cmnd *);
270 static int sbp2scsi_reset(struct scsi_cmnd *);
271 static int sbp2scsi_slave_alloc(struct scsi_device *);
272 static int sbp2scsi_slave_configure(struct scsi_device *);
273 static void sbp2scsi_slave_destroy(struct scsi_device *);
274 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
275 struct device_attribute *, char *);
277 static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
279 static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
280 &dev_attr_ieee1394_id,
284 static struct scsi_host_template sbp2_shost_template = {
285 .module = THIS_MODULE,
286 .name = "SBP-2 IEEE-1394",
287 .proc_name = SBP2_DEVICE_NAME,
288 .queuecommand = sbp2scsi_queuecommand,
289 .eh_abort_handler = sbp2scsi_abort,
290 .eh_device_reset_handler = sbp2scsi_reset,
291 .slave_alloc = sbp2scsi_slave_alloc,
292 .slave_configure = sbp2scsi_slave_configure,
293 .slave_destroy = sbp2scsi_slave_destroy,
295 .sg_tablesize = SG_ALL,
296 .use_clustering = ENABLE_CLUSTERING,
297 .cmd_per_lun = SBP2_MAX_CMDS,
298 .can_queue = SBP2_MAX_CMDS,
300 .sdev_attrs = sbp2_sysfs_sdev_attrs,
305 * List of devices with known bugs.
307 * The firmware_revision field, masked with 0xffff00, is the best indicator
308 * for the type of bridge chip of a device. It yields a few false positives
309 * but this did not break correctly behaving devices so far.
311 static const struct {
312 u32 firmware_revision;
314 unsigned workarounds;
315 } sbp2_workarounds_table[] = {
316 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
317 .firmware_revision = 0x002800,
318 .model_id = 0x001010,
319 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
320 SBP2_WORKAROUND_MODE_SENSE_8,
322 /* Initio bridges, actually only needed for some older ones */ {
323 .firmware_revision = 0x000200,
324 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
326 /* Symbios bridge */ {
327 .firmware_revision = 0xa0b800,
328 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
331 * Note about the following Apple iPod blacklist entries:
333 * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
334 * matching logic treats 0 as a wildcard, we cannot match this ID
335 * without rewriting the matching routine. Fortunately these iPods
336 * do not feature the read_capacity bug according to one report.
337 * Read_capacity behaviour as well as model_id could change due to
338 * Apple-supplied firmware updates though.
340 /* iPod 4th generation */ {
341 .firmware_revision = 0x0a2700,
342 .model_id = 0x000021,
343 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
346 .firmware_revision = 0x0a2700,
347 .model_id = 0x000023,
348 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
351 .firmware_revision = 0x0a2700,
352 .model_id = 0x00007e,
353 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
357 /**************************************
358 * General utility functions
359 **************************************/
363 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
365 static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
369 for (length = (length >> 2); length--; )
370 temp[length] = be32_to_cpu(temp[length]);
376 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
378 static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
382 for (length = (length >> 2); length--; )
383 temp[length] = cpu_to_be32(temp[length]);
387 #else /* BIG_ENDIAN */
388 /* Why waste the cpu cycles? */
389 #define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
390 #define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
393 static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
396 * Waits for completion of an SBP-2 access request.
397 * Returns nonzero if timed out or prematurely interrupted.
399 static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
404 leftover = wait_event_interruptible_timeout(
405 sbp2_access_wq, scsi_id->access_complete, timeout);
406 scsi_id->access_complete = 0;
407 return leftover <= 0;
410 static void sbp2_free_packet(struct hpsb_packet *packet)
412 hpsb_free_tlabel(packet);
413 hpsb_free_packet(packet);
417 * This is much like hpsb_node_write(), except it ignores the response
418 * subaction and returns immediately. Can be used from atomic context.
420 static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
421 quadlet_t *buffer, size_t length)
423 struct hpsb_packet *packet;
425 packet = hpsb_make_writepacket(ne->host, ne->nodeid,
426 addr, buffer, length);
430 hpsb_set_packet_complete_task(packet,
431 (void (*)(void *))sbp2_free_packet,
434 hpsb_node_fill_packet(ne, packet);
436 if (hpsb_send_packet(packet) < 0) {
437 sbp2_free_packet(packet);
444 static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
445 u64 offset, quadlet_t *data, size_t len)
448 * There is a small window after a bus reset within which the node
449 * entry's generation is current but the reconnect wasn't completed.
451 if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
454 if (hpsb_node_write(scsi_id->ne,
455 scsi_id->command_block_agent_addr + offset,
457 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
459 * Now accept new SCSI commands, unless a bus reset happended during
462 if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET))
463 scsi_unblock_requests(scsi_id->scsi_host);
466 static void sbp2util_write_orb_pointer(struct work_struct *work)
470 data[0] = ORB_SET_NODE_ID(
471 (container_of(work, struct scsi_id_instance_data, protocol_work))->hi->host->node_id);
472 data[1] = (container_of(work, struct scsi_id_instance_data, protocol_work))->last_orb_dma;
473 sbp2util_cpu_to_be32_buffer(data, 8);
474 sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_ORB_POINTER_OFFSET, data, 8);
477 static void sbp2util_write_doorbell(struct work_struct *work)
479 sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_DOORBELL_OFFSET, NULL, 4);
482 static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
484 struct sbp2_fwhost_info *hi = scsi_id->hi;
486 unsigned long flags, orbs;
487 struct sbp2_command_info *command;
489 orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
491 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
492 for (i = 0; i < orbs; i++) {
493 command = kzalloc(sizeof(*command), GFP_ATOMIC);
495 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock,
499 command->command_orb_dma =
500 pci_map_single(hi->host->pdev, &command->command_orb,
501 sizeof(struct sbp2_command_orb),
504 pci_map_single(hi->host->pdev,
505 &command->scatter_gather_element,
506 sizeof(command->scatter_gather_element),
507 PCI_DMA_BIDIRECTIONAL);
508 INIT_LIST_HEAD(&command->list);
509 list_add_tail(&command->list, &scsi_id->cmd_orb_completed);
511 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
515 static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
517 struct hpsb_host *host = scsi_id->hi->host;
518 struct list_head *lh, *next;
519 struct sbp2_command_info *command;
522 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
523 if (!list_empty(&scsi_id->cmd_orb_completed)) {
524 list_for_each_safe(lh, next, &scsi_id->cmd_orb_completed) {
525 command = list_entry(lh, struct sbp2_command_info, list);
526 pci_unmap_single(host->pdev, command->command_orb_dma,
527 sizeof(struct sbp2_command_orb),
529 pci_unmap_single(host->pdev, command->sge_dma,
530 sizeof(command->scatter_gather_element),
531 PCI_DMA_BIDIRECTIONAL);
535 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
540 * Finds the sbp2_command for a given outstanding command ORB.
541 * Only looks at the in-use list.
543 static struct sbp2_command_info *sbp2util_find_command_for_orb(
544 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
546 struct sbp2_command_info *command;
549 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
550 if (!list_empty(&scsi_id->cmd_orb_inuse)) {
551 list_for_each_entry(command, &scsi_id->cmd_orb_inuse, list) {
552 if (command->command_orb_dma == orb) {
553 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
558 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
563 * Finds the sbp2_command for a given outstanding SCpnt.
564 * Only looks at the in-use list.
565 * Must be called with scsi_id->cmd_orb_lock held.
567 static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
568 struct scsi_id_instance_data *scsi_id, void *SCpnt)
570 struct sbp2_command_info *command;
572 if (!list_empty(&scsi_id->cmd_orb_inuse))
573 list_for_each_entry(command, &scsi_id->cmd_orb_inuse, list)
574 if (command->Current_SCpnt == SCpnt)
579 static struct sbp2_command_info *sbp2util_allocate_command_orb(
580 struct scsi_id_instance_data *scsi_id,
581 struct scsi_cmnd *Current_SCpnt,
582 void (*Current_done)(struct scsi_cmnd *))
584 struct list_head *lh;
585 struct sbp2_command_info *command = NULL;
588 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
589 if (!list_empty(&scsi_id->cmd_orb_completed)) {
590 lh = scsi_id->cmd_orb_completed.next;
592 command = list_entry(lh, struct sbp2_command_info, list);
593 command->Current_done = Current_done;
594 command->Current_SCpnt = Current_SCpnt;
595 list_add_tail(&command->list, &scsi_id->cmd_orb_inuse);
597 SBP2_ERR("%s: no orbs available", __FUNCTION__);
599 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
603 static void sbp2util_free_command_dma(struct sbp2_command_info *command)
605 struct scsi_id_instance_data *scsi_id =
606 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
607 struct hpsb_host *host;
610 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
614 host = scsi_id->ud->ne->host;
616 if (command->cmd_dma) {
617 if (command->dma_type == CMD_DMA_SINGLE)
618 pci_unmap_single(host->pdev, command->cmd_dma,
619 command->dma_size, command->dma_dir);
620 else if (command->dma_type == CMD_DMA_PAGE)
621 pci_unmap_page(host->pdev, command->cmd_dma,
622 command->dma_size, command->dma_dir);
623 /* XXX: Check for CMD_DMA_NONE bug */
624 command->dma_type = CMD_DMA_NONE;
625 command->cmd_dma = 0;
628 if (command->sge_buffer) {
629 pci_unmap_sg(host->pdev, command->sge_buffer,
630 command->dma_size, command->dma_dir);
631 command->sge_buffer = NULL;
636 * This function moves a command to the completed orb list.
637 * Must be called with scsi_id->cmd_orb_lock held.
639 static void sbp2util_mark_command_completed(
640 struct scsi_id_instance_data *scsi_id,
641 struct sbp2_command_info *command)
643 list_del(&command->list);
644 sbp2util_free_command_dma(command);
645 list_add_tail(&command->list, &scsi_id->cmd_orb_completed);
649 * Is scsi_id valid? Is the 1394 node still present?
651 static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
653 return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
656 /*********************************************
657 * IEEE-1394 core driver stack related section
658 *********************************************/
660 static int sbp2_probe(struct device *dev)
662 struct unit_directory *ud;
663 struct scsi_id_instance_data *scsi_id;
665 ud = container_of(dev, struct unit_directory, device);
667 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
669 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
672 scsi_id = sbp2_alloc_device(ud);
677 sbp2_parse_unit_directory(scsi_id, ud);
679 return sbp2_start_device(scsi_id);
682 static int sbp2_remove(struct device *dev)
684 struct unit_directory *ud;
685 struct scsi_id_instance_data *scsi_id;
686 struct scsi_device *sdev;
688 ud = container_of(dev, struct unit_directory, device);
689 scsi_id = ud->device.driver_data;
693 if (scsi_id->scsi_host) {
694 /* Get rid of enqueued commands if there is no chance to
696 if (!sbp2util_node_is_available(scsi_id))
697 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
698 /* scsi_remove_device() may trigger shutdown functions of SCSI
699 * highlevel drivers which would deadlock if blocked. */
700 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
701 scsi_unblock_requests(scsi_id->scsi_host);
703 sdev = scsi_id->sdev;
705 scsi_id->sdev = NULL;
706 scsi_remove_device(sdev);
709 sbp2_logout_device(scsi_id);
710 sbp2_remove_device(scsi_id);
715 static int sbp2_update(struct unit_directory *ud)
717 struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
719 if (sbp2_reconnect_device(scsi_id)) {
720 /* Reconnect has failed. Perhaps we didn't reconnect fast
721 * enough. Try a regular login, but first log out just in
722 * case of any weirdness. */
723 sbp2_logout_device(scsi_id);
725 if (sbp2_login_device(scsi_id)) {
726 /* Login failed too, just fail, and the backend
727 * will call our sbp2_remove for us */
728 SBP2_ERR("Failed to reconnect to sbp2 device!");
733 sbp2_set_busy_timeout(scsi_id);
734 sbp2_agent_reset(scsi_id, 1);
735 sbp2_max_speed_and_size(scsi_id);
737 /* Complete any pending commands with busy (so they get retried)
738 * and remove them from our queue. */
739 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
741 /* Accept new commands unless there was another bus reset in the
743 if (hpsb_node_entry_valid(scsi_id->ne)) {
744 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
745 scsi_unblock_requests(scsi_id->scsi_host);
750 static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
752 struct sbp2_fwhost_info *hi;
753 struct Scsi_Host *scsi_host = NULL;
754 struct scsi_id_instance_data *scsi_id = NULL;
756 scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
758 SBP2_ERR("failed to create scsi_id");
762 scsi_id->ne = ud->ne;
764 scsi_id->speed_code = IEEE1394_SPEED_100;
765 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
766 scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
767 INIT_LIST_HEAD(&scsi_id->cmd_orb_inuse);
768 INIT_LIST_HEAD(&scsi_id->cmd_orb_completed);
769 INIT_LIST_HEAD(&scsi_id->scsi_list);
770 spin_lock_init(&scsi_id->cmd_orb_lock);
771 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
772 INIT_WORK(&scsi_id->protocol_work, NULL);
774 ud->device.driver_data = scsi_id;
776 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
778 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
780 SBP2_ERR("failed to allocate hostinfo");
783 hi->host = ud->ne->host;
784 INIT_LIST_HEAD(&hi->scsi_ids);
786 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
787 /* Handle data movement if physical dma is not
788 * enabled or not supported on host controller */
789 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
791 0x0ULL, 0xfffffffcULL)) {
792 SBP2_ERR("failed to register lower 4GB address range");
798 /* Prevent unloading of the 1394 host */
799 if (!try_module_get(hi->host->driver->owner)) {
800 SBP2_ERR("failed to get a reference on 1394 host driver");
806 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
808 /* Register the status FIFO address range. We could use the same FIFO
809 * for targets at different nodes. However we need different FIFOs per
810 * target in order to support multi-unit devices.
811 * The FIFO is located out of the local host controller's physical range
812 * but, if possible, within the posted write area. Status writes will
813 * then be performed as unified transactions. This slightly reduces
814 * bandwidth usage, and some Prolific based devices seem to require it.
816 scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
817 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
818 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
819 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
820 if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
821 SBP2_ERR("failed to allocate status FIFO address range");
825 scsi_host = scsi_host_alloc(&sbp2_shost_template,
826 sizeof(unsigned long));
828 SBP2_ERR("failed to register scsi host");
832 scsi_host->hostdata[0] = (unsigned long)scsi_id;
834 if (!scsi_add_host(scsi_host, &ud->device)) {
835 scsi_id->scsi_host = scsi_host;
839 SBP2_ERR("failed to add scsi host");
840 scsi_host_put(scsi_host);
843 sbp2_remove_device(scsi_id);
847 static void sbp2_host_reset(struct hpsb_host *host)
849 struct sbp2_fwhost_info *hi;
850 struct scsi_id_instance_data *scsi_id;
852 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
855 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
856 if (likely(atomic_read(&scsi_id->state) !=
857 SBP2LU_STATE_IN_SHUTDOWN)) {
858 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
859 scsi_block_requests(scsi_id->scsi_host);
863 static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
865 struct sbp2_fwhost_info *hi = scsi_id->hi;
868 scsi_id->login_response =
869 pci_alloc_consistent(hi->host->pdev,
870 sizeof(struct sbp2_login_response),
871 &scsi_id->login_response_dma);
872 if (!scsi_id->login_response)
875 scsi_id->query_logins_orb =
876 pci_alloc_consistent(hi->host->pdev,
877 sizeof(struct sbp2_query_logins_orb),
878 &scsi_id->query_logins_orb_dma);
879 if (!scsi_id->query_logins_orb)
882 scsi_id->query_logins_response =
883 pci_alloc_consistent(hi->host->pdev,
884 sizeof(struct sbp2_query_logins_response),
885 &scsi_id->query_logins_response_dma);
886 if (!scsi_id->query_logins_response)
889 scsi_id->reconnect_orb =
890 pci_alloc_consistent(hi->host->pdev,
891 sizeof(struct sbp2_reconnect_orb),
892 &scsi_id->reconnect_orb_dma);
893 if (!scsi_id->reconnect_orb)
896 scsi_id->logout_orb =
897 pci_alloc_consistent(hi->host->pdev,
898 sizeof(struct sbp2_logout_orb),
899 &scsi_id->logout_orb_dma);
900 if (!scsi_id->logout_orb)
904 pci_alloc_consistent(hi->host->pdev,
905 sizeof(struct sbp2_login_orb),
906 &scsi_id->login_orb_dma);
907 if (!scsi_id->login_orb)
910 if (sbp2util_create_command_orb_pool(scsi_id)) {
911 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
912 sbp2_remove_device(scsi_id);
916 /* Wait a second before trying to log in. Previously logged in
917 * initiators need a chance to reconnect. */
918 if (msleep_interruptible(1000)) {
919 sbp2_remove_device(scsi_id);
923 if (sbp2_login_device(scsi_id)) {
924 sbp2_remove_device(scsi_id);
928 sbp2_set_busy_timeout(scsi_id);
929 sbp2_agent_reset(scsi_id, 1);
930 sbp2_max_speed_and_size(scsi_id);
932 error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
934 SBP2_ERR("scsi_add_device failed");
935 sbp2_logout_device(scsi_id);
936 sbp2_remove_device(scsi_id);
943 SBP2_ERR("Could not allocate memory for scsi_id");
944 sbp2_remove_device(scsi_id);
948 static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
950 struct sbp2_fwhost_info *hi;
957 if (scsi_id->scsi_host) {
958 scsi_remove_host(scsi_id->scsi_host);
959 scsi_host_put(scsi_id->scsi_host);
961 flush_scheduled_work();
962 sbp2util_remove_command_orb_pool(scsi_id);
964 list_del(&scsi_id->scsi_list);
966 if (scsi_id->login_response)
967 pci_free_consistent(hi->host->pdev,
968 sizeof(struct sbp2_login_response),
969 scsi_id->login_response,
970 scsi_id->login_response_dma);
971 if (scsi_id->login_orb)
972 pci_free_consistent(hi->host->pdev,
973 sizeof(struct sbp2_login_orb),
975 scsi_id->login_orb_dma);
976 if (scsi_id->reconnect_orb)
977 pci_free_consistent(hi->host->pdev,
978 sizeof(struct sbp2_reconnect_orb),
979 scsi_id->reconnect_orb,
980 scsi_id->reconnect_orb_dma);
981 if (scsi_id->logout_orb)
982 pci_free_consistent(hi->host->pdev,
983 sizeof(struct sbp2_logout_orb),
985 scsi_id->logout_orb_dma);
986 if (scsi_id->query_logins_orb)
987 pci_free_consistent(hi->host->pdev,
988 sizeof(struct sbp2_query_logins_orb),
989 scsi_id->query_logins_orb,
990 scsi_id->query_logins_orb_dma);
991 if (scsi_id->query_logins_response)
992 pci_free_consistent(hi->host->pdev,
993 sizeof(struct sbp2_query_logins_response),
994 scsi_id->query_logins_response,
995 scsi_id->query_logins_response_dma);
997 if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
998 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
999 scsi_id->status_fifo_addr);
1001 scsi_id->ud->device.driver_data = NULL;
1004 module_put(hi->host->driver->owner);
1009 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1011 * Deal with write requests on adapters which do not support physical DMA or
1012 * have it switched off.
1014 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1015 int destid, quadlet_t *data, u64 addr,
1016 size_t length, u16 flags)
1018 memcpy(bus_to_virt((u32) addr), data, length);
1019 return RCODE_COMPLETE;
1023 * Deal with read requests on adapters which do not support physical DMA or
1024 * have it switched off.
1026 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1027 quadlet_t *data, u64 addr, size_t length,
1030 memcpy(data, bus_to_virt((u32) addr), length);
1031 return RCODE_COMPLETE;
1035 /**************************************
1036 * SBP-2 protocol related section
1037 **************************************/
1039 static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1041 struct sbp2_fwhost_info *hi = scsi_id->hi;
1046 scsi_id->query_logins_orb->reserved1 = 0x0;
1047 scsi_id->query_logins_orb->reserved2 = 0x0;
1049 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1050 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1052 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1053 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1054 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->lun);
1056 scsi_id->query_logins_orb->reserved_resp_length =
1057 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
1059 scsi_id->query_logins_orb->status_fifo_hi =
1060 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1061 scsi_id->query_logins_orb->status_fifo_lo =
1062 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1064 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1066 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1068 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1069 data[1] = scsi_id->query_logins_orb_dma;
1070 sbp2util_cpu_to_be32_buffer(data, 8);
1072 hpsb_node_write(scsi_id->ne, scsi_id->management_agent_addr, data, 8);
1074 if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
1075 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1079 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1080 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1084 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1085 SBP2_INFO("Error querying logins to SBP-2 device - failed");
1089 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1091 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
1092 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1094 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
1095 SBP2_INFO("Number of active logins: %d", active_logins);
1097 if (active_logins >= max_logins) {
1104 static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1106 struct sbp2_fwhost_info *hi = scsi_id->hi;
1109 if (!scsi_id->login_orb)
1112 if (!sbp2_exclusive_login && sbp2_query_logins(scsi_id)) {
1113 SBP2_INFO("Device does not support any more concurrent logins");
1117 /* assume no password */
1118 scsi_id->login_orb->password_hi = 0;
1119 scsi_id->login_orb->password_lo = 0;
1121 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1122 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1123 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1125 /* one second reconnect time */
1126 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
1127 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
1128 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
1129 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->lun);
1131 scsi_id->login_orb->passwd_resp_lengths =
1132 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1134 scsi_id->login_orb->status_fifo_hi =
1135 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1136 scsi_id->login_orb->status_fifo_lo =
1137 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1139 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1141 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1143 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1144 data[1] = scsi_id->login_orb_dma;
1145 sbp2util_cpu_to_be32_buffer(data, 8);
1147 hpsb_node_write(scsi_id->ne, scsi_id->management_agent_addr, data, 8);
1149 /* wait up to 20 seconds for login status */
1150 if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
1151 SBP2_ERR("Error logging into SBP-2 device - timed out");
1155 /* make sure that the returned status matches the login ORB */
1156 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1157 SBP2_ERR("Error logging into SBP-2 device - timed out");
1161 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1162 SBP2_ERR("Error logging into SBP-2 device - failed");
1166 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1167 scsi_id->command_block_agent_addr =
1168 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1169 scsi_id->command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1170 scsi_id->command_block_agent_addr &= 0x0000ffffffffffffULL;
1172 SBP2_INFO("Logged into SBP-2 device");
1176 static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1178 struct sbp2_fwhost_info *hi = scsi_id->hi;
1182 scsi_id->logout_orb->reserved1 = 0x0;
1183 scsi_id->logout_orb->reserved2 = 0x0;
1184 scsi_id->logout_orb->reserved3 = 0x0;
1185 scsi_id->logout_orb->reserved4 = 0x0;
1187 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1188 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1189 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1191 scsi_id->logout_orb->reserved5 = 0x0;
1192 scsi_id->logout_orb->status_fifo_hi =
1193 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1194 scsi_id->logout_orb->status_fifo_lo =
1195 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1197 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1199 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1200 data[1] = scsi_id->logout_orb_dma;
1201 sbp2util_cpu_to_be32_buffer(data, 8);
1203 error = hpsb_node_write(scsi_id->ne,
1204 scsi_id->management_agent_addr, data, 8);
1208 /* wait up to 1 second for the device to complete logout */
1209 if (sbp2util_access_timeout(scsi_id, HZ))
1212 SBP2_INFO("Logged out of SBP-2 device");
1216 static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1218 struct sbp2_fwhost_info *hi = scsi_id->hi;
1222 scsi_id->reconnect_orb->reserved1 = 0x0;
1223 scsi_id->reconnect_orb->reserved2 = 0x0;
1224 scsi_id->reconnect_orb->reserved3 = 0x0;
1225 scsi_id->reconnect_orb->reserved4 = 0x0;
1227 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1228 scsi_id->reconnect_orb->login_ID_misc |=
1229 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1230 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1232 scsi_id->reconnect_orb->reserved5 = 0x0;
1233 scsi_id->reconnect_orb->status_fifo_hi =
1234 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1235 scsi_id->reconnect_orb->status_fifo_lo =
1236 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1238 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1240 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1241 data[1] = scsi_id->reconnect_orb_dma;
1242 sbp2util_cpu_to_be32_buffer(data, 8);
1244 error = hpsb_node_write(scsi_id->ne,
1245 scsi_id->management_agent_addr, data, 8);
1249 /* wait up to 1 second for reconnect status */
1250 if (sbp2util_access_timeout(scsi_id, HZ)) {
1251 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1255 /* make sure that the returned status matches the reconnect ORB */
1256 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1257 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1261 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1262 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
1266 SBP2_INFO("Reconnected to SBP-2 device");
1271 * Set the target node's Single Phase Retry limit. Affects the target's retry
1272 * behaviour if our node is too busy to accept requests.
1274 static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1278 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1279 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1280 SBP2_ERR("%s error", __FUNCTION__);
1284 static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1285 struct unit_directory *ud)
1287 struct csr1212_keyval *kv;
1288 struct csr1212_dentry *dentry;
1289 u64 management_agent_addr;
1290 u32 command_set_spec_id, command_set, unit_characteristics,
1292 unsigned workarounds;
1295 management_agent_addr = 0x0;
1296 command_set_spec_id = 0x0;
1298 unit_characteristics = 0x0;
1299 firmware_revision = 0x0;
1301 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1302 switch (kv->key.id) {
1303 case CSR1212_KV_ID_DEPENDENT_INFO:
1304 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
1305 management_agent_addr =
1306 CSR1212_REGISTER_SPACE_BASE +
1307 (kv->value.csr_offset << 2);
1309 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
1311 ORB_SET_LUN(kv->value.immediate);
1314 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1315 command_set_spec_id = kv->value.immediate;
1318 case SBP2_COMMAND_SET_KEY:
1319 command_set = kv->value.immediate;
1322 case SBP2_UNIT_CHARACTERISTICS_KEY:
1323 /* FIXME: This is ignored so far.
1324 * See SBP-2 clause 7.4.8. */
1325 unit_characteristics = kv->value.immediate;
1328 case SBP2_FIRMWARE_REVISION_KEY:
1329 firmware_revision = kv->value.immediate;
1333 /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1334 * Its "ordered" bit has consequences for command ORB
1335 * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
1340 workarounds = sbp2_default_workarounds;
1342 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1343 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1344 if (sbp2_workarounds_table[i].firmware_revision &&
1345 sbp2_workarounds_table[i].firmware_revision !=
1346 (firmware_revision & 0xffff00))
1348 if (sbp2_workarounds_table[i].model_id &&
1349 sbp2_workarounds_table[i].model_id != ud->model_id)
1351 workarounds |= sbp2_workarounds_table[i].workarounds;
1356 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1357 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1358 " model_id 0x%06x)",
1359 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1360 workarounds, firmware_revision,
1361 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1364 /* We would need one SCSI host template for each target to adjust
1365 * max_sectors on the fly, therefore warn only. */
1366 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1367 (sbp2_max_sectors * 512) > (128 * 1024))
1368 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
1369 "max transfer size. WARNING: Current max_sectors "
1370 "setting is larger than 128KB (%d sectors)",
1371 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1374 /* If this is a logical unit directory entry, process the parent
1375 * to get the values. */
1376 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1377 struct unit_directory *parent_ud =
1378 container_of(ud->device.parent, struct unit_directory, device);
1379 sbp2_parse_unit_directory(scsi_id, parent_ud);
1381 scsi_id->management_agent_addr = management_agent_addr;
1382 scsi_id->command_set_spec_id = command_set_spec_id;
1383 scsi_id->command_set = command_set;
1384 scsi_id->unit_characteristics = unit_characteristics;
1385 scsi_id->firmware_revision = firmware_revision;
1386 scsi_id->workarounds = workarounds;
1387 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
1388 scsi_id->lun = ORB_SET_LUN(ud->lun);
1392 #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1395 * This function is called in order to determine the max speed and packet
1396 * size we can use in our ORBs. Note, that we (the driver and host) only
1397 * initiate the transaction. The SBP-2 device actually transfers the data
1398 * (by reading from the DMA area we tell it). This means that the SBP-2
1399 * device decides the actual maximum data it can transfer. We just tell it
1400 * the speed that it needs to use, and the max_rec the host supports, and
1401 * it takes care of the rest.
1403 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1405 struct sbp2_fwhost_info *hi = scsi_id->hi;
1408 scsi_id->speed_code =
1409 hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
1411 if (scsi_id->speed_code > sbp2_max_speed) {
1412 scsi_id->speed_code = sbp2_max_speed;
1413 SBP2_INFO("Reducing speed to %s",
1414 hpsb_speedto_str[sbp2_max_speed]);
1417 /* Payload size is the lesser of what our speed supports and what
1418 * our host supports. */
1419 payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1420 (u8) (hi->host->csr.max_rec - 1));
1422 /* If physical DMA is off, work around limitation in ohci1394:
1423 * packet size must not exceed PAGE_SIZE */
1424 if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
1425 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1429 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1430 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1431 hpsb_speedto_str[scsi_id->speed_code],
1432 SBP2_PAYLOAD_TO_BYTES(payload));
1434 scsi_id->max_payload_size = payload;
1438 static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1443 unsigned long flags;
1445 /* cancel_delayed_work(&scsi_id->protocol_work); */
1447 flush_scheduled_work();
1449 data = ntohl(SBP2_AGENT_RESET_DATA);
1450 addr = scsi_id->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1453 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1455 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1458 SBP2_ERR("hpsb_node_write failed.\n");
1462 /* make sure that the ORB_POINTER is written on next command */
1463 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1464 scsi_id->last_orb = NULL;
1465 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1470 static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1471 struct sbp2_fwhost_info *hi,
1472 struct sbp2_command_info *command,
1473 unsigned int scsi_use_sg,
1474 struct scatterlist *sgpnt,
1476 enum dma_data_direction dma_dir)
1478 command->dma_dir = dma_dir;
1479 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1480 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1482 /* special case if only one element (and less than 64KB in size) */
1483 if ((scsi_use_sg == 1) &&
1484 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1486 command->dma_size = sgpnt[0].length;
1487 command->dma_type = CMD_DMA_PAGE;
1488 command->cmd_dma = pci_map_page(hi->host->pdev,
1494 orb->data_descriptor_lo = command->cmd_dma;
1495 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1498 struct sbp2_unrestricted_page_table *sg_element =
1499 &command->scatter_gather_element[0];
1500 u32 sg_count, sg_len;
1502 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
1505 command->dma_size = scsi_use_sg;
1506 command->sge_buffer = sgpnt;
1508 /* use page tables (s/g) */
1509 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1510 orb->data_descriptor_lo = command->sge_dma;
1512 /* loop through and fill out our SBP-2 page tables
1513 * (and split up anything too large) */
1514 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1515 sg_len = sg_dma_len(sgpnt);
1516 sg_addr = sg_dma_address(sgpnt);
1518 sg_element[sg_count].segment_base_lo = sg_addr;
1519 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1520 sg_element[sg_count].length_segment_base_hi =
1521 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1522 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1523 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1525 sg_element[sg_count].length_segment_base_hi =
1526 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1533 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1535 sbp2util_cpu_to_be32_buffer(sg_element,
1536 (sizeof(struct sbp2_unrestricted_page_table)) *
1541 static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1542 struct sbp2_fwhost_info *hi,
1543 struct sbp2_command_info *command,
1544 struct scatterlist *sgpnt,
1546 unsigned int scsi_request_bufflen,
1547 void *scsi_request_buffer,
1548 enum dma_data_direction dma_dir)
1550 command->dma_dir = dma_dir;
1551 command->dma_size = scsi_request_bufflen;
1552 command->dma_type = CMD_DMA_SINGLE;
1553 command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
1554 command->dma_size, command->dma_dir);
1555 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1556 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1558 /* handle case where we get a command w/o s/g enabled
1559 * (but check for transfers larger than 64K) */
1560 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1562 orb->data_descriptor_lo = command->cmd_dma;
1563 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1566 /* The buffer is too large. Turn this into page tables. */
1568 struct sbp2_unrestricted_page_table *sg_element =
1569 &command->scatter_gather_element[0];
1570 u32 sg_count, sg_len;
1573 orb->data_descriptor_lo = command->sge_dma;
1574 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1576 /* fill out our SBP-2 page tables; split up the large buffer */
1578 sg_len = scsi_request_bufflen;
1579 sg_addr = command->cmd_dma;
1581 sg_element[sg_count].segment_base_lo = sg_addr;
1582 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1583 sg_element[sg_count].length_segment_base_hi =
1584 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1585 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1586 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1588 sg_element[sg_count].length_segment_base_hi =
1589 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1595 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1597 sbp2util_cpu_to_be32_buffer(sg_element,
1598 (sizeof(struct sbp2_unrestricted_page_table)) *
1603 static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1604 struct sbp2_command_info *command,
1606 unsigned int scsi_use_sg,
1607 unsigned int scsi_request_bufflen,
1608 void *scsi_request_buffer,
1609 enum dma_data_direction dma_dir)
1611 struct sbp2_fwhost_info *hi = scsi_id->hi;
1612 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
1613 struct sbp2_command_orb *command_orb = &command->command_orb;
1617 * Set-up our command ORB.
1619 * NOTE: We're doing unrestricted page tables (s/g), as this is
1620 * best performance (at least with the devices I have). This means
1621 * that data_size becomes the number of s/g elements, and
1622 * page_size should be zero (for unrestricted).
1624 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1625 command_orb->next_ORB_lo = 0x0;
1626 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1627 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
1628 command_orb->misc |= ORB_SET_NOTIFY(1);
1630 if (dma_dir == DMA_NONE)
1631 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1632 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
1633 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1634 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
1635 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1637 SBP2_INFO("Falling back to DMA_NONE");
1638 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1641 /* set up our page table stuff */
1642 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1643 command_orb->data_descriptor_hi = 0x0;
1644 command_orb->data_descriptor_lo = 0x0;
1645 command_orb->misc |= ORB_SET_DIRECTION(1);
1646 } else if (scsi_use_sg)
1647 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
1648 sgpnt, orb_direction, dma_dir);
1650 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
1651 orb_direction, scsi_request_bufflen,
1652 scsi_request_buffer, dma_dir);
1654 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1656 memset(command_orb->cdb, 0, 12);
1657 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1660 static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
1661 struct sbp2_command_info *command)
1663 struct sbp2_fwhost_info *hi = scsi_id->hi;
1664 struct sbp2_command_orb *command_orb = &command->command_orb;
1665 struct sbp2_command_orb *last_orb;
1666 dma_addr_t last_orb_dma;
1667 u64 addr = scsi_id->command_block_agent_addr;
1670 unsigned long flags;
1672 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
1673 sizeof(struct sbp2_command_orb),
1675 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
1676 sizeof(command->scatter_gather_element),
1677 PCI_DMA_BIDIRECTIONAL);
1679 /* check to see if there are any previous orbs to use */
1680 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1681 last_orb = scsi_id->last_orb;
1682 last_orb_dma = scsi_id->last_orb_dma;
1685 * last_orb == NULL means: We know that the target's fetch agent
1686 * is not active right now.
1688 addr += SBP2_ORB_POINTER_OFFSET;
1689 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1690 data[1] = command->command_orb_dma;
1691 sbp2util_cpu_to_be32_buffer(data, 8);
1695 * last_orb != NULL means: We know that the target's fetch agent
1696 * is (very probably) not dead or in reset state right now.
1697 * We have an ORB already sent that we can append a new one to.
1698 * The target's fetch agent may or may not have read this
1701 pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
1702 sizeof(struct sbp2_command_orb),
1704 last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
1706 /* Tells hardware that this pointer is valid */
1707 last_orb->next_ORB_hi = 0;
1708 pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
1709 sizeof(struct sbp2_command_orb),
1711 addr += SBP2_DOORBELL_OFFSET;
1715 scsi_id->last_orb = command_orb;
1716 scsi_id->last_orb_dma = command->command_orb_dma;
1717 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1719 if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
1721 * sbp2util_node_write_no_wait failed. We certainly ran out
1722 * of transaction labels, perhaps just because there were no
1723 * context switches which gave khpsbpkt a chance to collect
1724 * free tlabels. Try again in non-atomic context. If necessary,
1725 * the workqueue job will sleep to guaranteedly get a tlabel.
1726 * We do not accept new commands until the job is over.
1728 scsi_block_requests(scsi_id->scsi_host);
1729 PREPARE_WORK(&scsi_id->protocol_work,
1730 last_orb ? sbp2util_write_doorbell:
1731 sbp2util_write_orb_pointer
1733 schedule_work(&scsi_id->protocol_work);
1737 static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
1738 struct scsi_cmnd *SCpnt,
1739 void (*done)(struct scsi_cmnd *))
1741 unchar *cmd = (unchar *) SCpnt->cmnd;
1742 unsigned int request_bufflen = SCpnt->request_bufflen;
1743 struct sbp2_command_info *command;
1745 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
1749 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
1750 request_bufflen, SCpnt->request_buffer,
1751 SCpnt->sc_data_direction);
1752 sbp2_link_orb_command(scsi_id, command);
1758 * Translates SBP-2 status into SCSI sense data for check conditions
1760 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
1762 /* OK, it's pretty ugly... ;-) */
1763 sense_data[0] = 0x70;
1764 sense_data[1] = 0x0;
1765 sense_data[2] = sbp2_status[9];
1766 sense_data[3] = sbp2_status[12];
1767 sense_data[4] = sbp2_status[13];
1768 sense_data[5] = sbp2_status[14];
1769 sense_data[6] = sbp2_status[15];
1771 sense_data[8] = sbp2_status[16];
1772 sense_data[9] = sbp2_status[17];
1773 sense_data[10] = sbp2_status[18];
1774 sense_data[11] = sbp2_status[19];
1775 sense_data[12] = sbp2_status[10];
1776 sense_data[13] = sbp2_status[11];
1777 sense_data[14] = sbp2_status[20];
1778 sense_data[15] = sbp2_status[21];
1780 return sbp2_status[8] & 0x3f;
1783 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1784 int destid, quadlet_t *data, u64 addr,
1785 size_t length, u16 fl)
1787 struct sbp2_fwhost_info *hi;
1788 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
1789 struct scsi_cmnd *SCpnt = NULL;
1790 struct sbp2_status_block *sb;
1791 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
1792 struct sbp2_command_info *command;
1793 unsigned long flags;
1795 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1796 SBP2_ERR("Wrong size of status block");
1797 return RCODE_ADDRESS_ERROR;
1799 if (unlikely(!host)) {
1800 SBP2_ERR("host is NULL - this is bad!");
1801 return RCODE_ADDRESS_ERROR;
1803 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
1804 if (unlikely(!hi)) {
1805 SBP2_ERR("host info is NULL - this is bad!");
1806 return RCODE_ADDRESS_ERROR;
1809 /* Find the unit which wrote the status. */
1810 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
1811 if (scsi_id_tmp->ne->nodeid == nodeid &&
1812 scsi_id_tmp->status_fifo_addr == addr) {
1813 scsi_id = scsi_id_tmp;
1817 if (unlikely(!scsi_id)) {
1818 SBP2_ERR("scsi_id is NULL - device is gone?");
1819 return RCODE_ADDRESS_ERROR;
1822 /* Put response into scsi_id status fifo buffer. The first two bytes
1823 * come in big endian bit order. Often the target writes only a
1824 * truncated status block, minimally the first two quadlets. The rest
1825 * is implied to be zeros. */
1826 sb = &scsi_id->status_block;
1827 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1828 memcpy(sb, data, length);
1829 sbp2util_be32_to_cpu_buffer(sb, 8);
1831 /* Ignore unsolicited status. Handle command ORB status. */
1832 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
1835 command = sbp2util_find_command_for_orb(scsi_id,
1838 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1839 sizeof(struct sbp2_command_orb),
1841 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1842 sizeof(command->scatter_gather_element),
1843 PCI_DMA_BIDIRECTIONAL);
1844 /* Grab SCSI command pointers and check status. */
1846 * FIXME: If the src field in the status is 1, the ORB DMA must
1847 * not be reused until status for a subsequent ORB is received.
1849 SCpnt = command->Current_SCpnt;
1850 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1851 sbp2util_mark_command_completed(scsi_id, command);
1852 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1855 u32 h = sb->ORB_offset_hi_misc;
1856 u32 r = STATUS_GET_RESP(h);
1858 if (r != RESP_STATUS_REQUEST_COMPLETE) {
1859 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
1860 r, STATUS_GET_SBP_STATUS(h));
1862 r == RESP_STATUS_TRANSPORT_FAILURE ?
1863 SBP2_SCSI_STATUS_BUSY :
1864 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
1867 if (STATUS_GET_LEN(h) > 1)
1868 scsi_status = sbp2_status_to_sense_data(
1869 (unchar *)sb, SCpnt->sense_buffer);
1871 if (STATUS_TEST_DEAD(h))
1872 sbp2_agent_reset(scsi_id, 0);
1875 /* Check here to see if there are no commands in-use. If there
1876 * are none, we know that the fetch agent left the active state
1877 * _and_ that we did not reactivate it yet. Therefore clear
1878 * last_orb so that next time we write directly to the
1879 * ORB_POINTER register. That way the fetch agent does not need
1880 * to refetch the next_ORB. */
1881 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1882 if (list_empty(&scsi_id->cmd_orb_inuse))
1883 scsi_id->last_orb = NULL;
1884 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1887 /* It's probably status after a management request. */
1888 if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
1889 (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
1890 (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
1891 (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
1892 scsi_id->access_complete = 1;
1893 wake_up_interruptible(&sbp2_access_wq);
1898 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
1899 command->Current_done);
1900 return RCODE_COMPLETE;
1903 /**************************************
1904 * SCSI interface related section
1905 **************************************/
1907 static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1908 void (*done)(struct scsi_cmnd *))
1910 struct scsi_id_instance_data *scsi_id =
1911 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
1912 struct sbp2_fwhost_info *hi;
1913 int result = DID_NO_CONNECT << 16;
1915 if (unlikely(!sbp2util_node_is_available(scsi_id)))
1920 if (unlikely(!hi)) {
1921 SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
1925 /* Multiple units are currently represented to the SCSI core as separate
1926 * targets, not as one target with multiple LUs. Therefore return
1927 * selection time-out to any IO directed at non-zero LUNs. */
1928 if (unlikely(SCpnt->device->lun))
1931 /* handle the request sense command here (auto-request sense) */
1932 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
1933 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
1934 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
1935 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
1939 if (unlikely(!hpsb_node_entry_valid(scsi_id->ne))) {
1940 SBP2_ERR("Bus reset in progress - rejecting command");
1941 result = DID_BUS_BUSY << 16;
1945 /* Bidirectional commands are not yet implemented,
1946 * and unknown transfer direction not handled. */
1947 if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
1948 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1949 result = DID_ERROR << 16;
1953 if (sbp2_send_command(scsi_id, SCpnt, done)) {
1954 SBP2_ERR("Error sending SCSI command");
1955 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1961 SCpnt->result = result;
1966 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
1969 struct sbp2_fwhost_info *hi = scsi_id->hi;
1970 struct list_head *lh;
1971 struct sbp2_command_info *command;
1972 unsigned long flags;
1974 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1975 while (!list_empty(&scsi_id->cmd_orb_inuse)) {
1976 lh = scsi_id->cmd_orb_inuse.next;
1977 command = list_entry(lh, struct sbp2_command_info, list);
1978 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1979 sizeof(struct sbp2_command_orb),
1981 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1982 sizeof(command->scatter_gather_element),
1983 PCI_DMA_BIDIRECTIONAL);
1984 sbp2util_mark_command_completed(scsi_id, command);
1985 if (command->Current_SCpnt) {
1986 command->Current_SCpnt->result = status << 16;
1987 command->Current_done(command->Current_SCpnt);
1990 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1996 * Complete a regular SCSI command. Can be called in atomic context.
1998 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
1999 u32 scsi_status, struct scsi_cmnd *SCpnt,
2000 void (*done)(struct scsi_cmnd *))
2003 SBP2_ERR("SCpnt is NULL");
2007 switch (scsi_status) {
2008 case SBP2_SCSI_STATUS_GOOD:
2009 SCpnt->result = DID_OK << 16;
2012 case SBP2_SCSI_STATUS_BUSY:
2013 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2014 SCpnt->result = DID_BUS_BUSY << 16;
2017 case SBP2_SCSI_STATUS_CHECK_CONDITION:
2018 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
2021 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2022 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2023 SCpnt->result = DID_NO_CONNECT << 16;
2024 scsi_print_command(SCpnt);
2027 case SBP2_SCSI_STATUS_CONDITION_MET:
2028 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2029 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2030 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2031 SCpnt->result = DID_ERROR << 16;
2032 scsi_print_command(SCpnt);
2036 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2037 SCpnt->result = DID_ERROR << 16;
2040 /* If a bus reset is in progress and there was an error, complete
2041 * the command as busy so that it will get retried. */
2042 if (!hpsb_node_entry_valid(scsi_id->ne)
2043 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2044 SBP2_ERR("Completing command with busy (bus reset)");
2045 SCpnt->result = DID_BUS_BUSY << 16;
2048 /* Tell the SCSI stack that we're done with this command. */
2052 static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2054 struct scsi_id_instance_data *scsi_id =
2055 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2057 scsi_id->sdev = sdev;
2058 sdev->allow_restart = 1;
2060 if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
2061 sdev->inquiry_len = 36;
2065 static int sbp2scsi_slave_configure(struct scsi_device *sdev)
2067 struct scsi_id_instance_data *scsi_id =
2068 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2070 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
2071 sdev->use_10_for_rw = 1;
2073 if (sdev->type == TYPE_DISK &&
2074 scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2075 sdev->skip_ms_page_8 = 1;
2076 if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2077 sdev->fix_capacity = 1;
2081 static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2083 ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
2088 * Called by scsi stack when something has really gone wrong.
2089 * Usually called when a command has timed-out for some reason.
2091 static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2093 struct scsi_id_instance_data *scsi_id =
2094 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2095 struct sbp2_fwhost_info *hi = scsi_id->hi;
2096 struct sbp2_command_info *command;
2097 unsigned long flags;
2099 SBP2_INFO("aborting sbp2 command");
2100 scsi_print_command(SCpnt);
2102 if (sbp2util_node_is_available(scsi_id)) {
2103 sbp2_agent_reset(scsi_id, 1);
2105 /* Return a matching command structure to the free pool. */
2106 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
2107 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2109 pci_dma_sync_single_for_cpu(hi->host->pdev,
2110 command->command_orb_dma,
2111 sizeof(struct sbp2_command_orb),
2113 pci_dma_sync_single_for_cpu(hi->host->pdev,
2115 sizeof(command->scatter_gather_element),
2116 PCI_DMA_BIDIRECTIONAL);
2117 sbp2util_mark_command_completed(scsi_id, command);
2118 if (command->Current_SCpnt) {
2119 command->Current_SCpnt->result = DID_ABORT << 16;
2120 command->Current_done(command->Current_SCpnt);
2123 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
2125 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2132 * Called by scsi stack when something has really gone wrong.
2134 static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
2136 struct scsi_id_instance_data *scsi_id =
2137 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2139 SBP2_INFO("reset requested");
2141 if (sbp2util_node_is_available(scsi_id)) {
2142 SBP2_INFO("generating sbp2 fetch agent reset");
2143 sbp2_agent_reset(scsi_id, 1);
2149 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2150 struct device_attribute *attr,
2153 struct scsi_device *sdev;
2154 struct scsi_id_instance_data *scsi_id;
2156 if (!(sdev = to_scsi_device(dev)))
2159 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2162 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
2163 scsi_id->ud->id, ORB_SET_LUN(scsi_id->lun));
2166 MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2167 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2168 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2169 MODULE_LICENSE("GPL");
2171 static int sbp2_module_init(void)
2175 if (sbp2_serialize_io) {
2176 sbp2_shost_template.can_queue = 1;
2177 sbp2_shost_template.cmd_per_lun = 1;
2180 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2181 (sbp2_max_sectors * 512) > (128 * 1024))
2182 sbp2_max_sectors = 128 * 1024 / 512;
2183 sbp2_shost_template.max_sectors = sbp2_max_sectors;
2185 hpsb_register_highlevel(&sbp2_highlevel);
2186 ret = hpsb_register_protocol(&sbp2_driver);
2188 SBP2_ERR("Failed to register protocol");
2189 hpsb_unregister_highlevel(&sbp2_highlevel);
2195 static void __exit sbp2_module_exit(void)
2197 hpsb_unregister_protocol(&sbp2_driver);
2198 hpsb_unregister_highlevel(&sbp2_highlevel);
2201 module_init(sbp2_module_init);
2202 module_exit(sbp2_module_exit);