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/config.h>
42 #include <linux/kernel.h>
43 #include <linux/list.h>
44 #include <linux/string.h>
45 #include <linux/stringify.h>
46 #include <linux/slab.h>
47 #include <linux/interrupt.h>
49 #include <linux/poll.h>
50 #include <linux/module.h>
51 #include <linux/moduleparam.h>
52 #include <linux/types.h>
53 #include <linux/delay.h>
54 #include <linux/sched.h>
55 #include <linux/blkdev.h>
56 #include <linux/smp_lock.h>
57 #include <linux/init.h>
58 #include <linux/pci.h>
60 #include <asm/current.h>
61 #include <asm/uaccess.h>
63 #include <asm/byteorder.h>
64 #include <asm/atomic.h>
65 #include <asm/system.h>
66 #include <asm/scatterlist.h>
68 #include <scsi/scsi.h>
69 #include <scsi/scsi_cmnd.h>
70 #include <scsi/scsi_dbg.h>
71 #include <scsi/scsi_device.h>
72 #include <scsi/scsi_host.h>
76 #include "ieee1394_types.h"
77 #include "ieee1394_core.h"
80 #include "highlevel.h"
81 #include "ieee1394_transactions.h"
85 * Module load parameter definitions
89 * Change max_speed on module load if you have a bad IEEE-1394
90 * controller that has trouble running 2KB packets at 400mb.
92 * NOTE: On certain OHCI parts I have seen short packets on async transmit
93 * (probably due to PCI latency/throughput issues with the part). You can
94 * bump down the speed if you are running into problems.
96 static int max_speed = IEEE1394_SPEED_MAX;
97 module_param(max_speed, int, 0644);
98 MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)");
101 * Set serialize_io to 1 if you'd like only one scsi command sent
102 * down to us at a time (debugging). This might be necessary for very
103 * badly behaved sbp2 devices.
105 * TODO: Make this configurable per device.
107 static int serialize_io = 1;
108 module_param(serialize_io, int, 0444);
109 MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default = 1, faster = 0)");
112 * Bump up max_sectors if you'd like to support very large sized
113 * transfers. Please note that some older sbp2 bridge chips are broken for
114 * transfers greater or equal to 128KB. Default is a value of 255
115 * sectors, or just under 128KB (at 512 byte sector size). I can note that
116 * the Oxsemi sbp2 chipsets have no problems supporting very large
119 static int max_sectors = SBP2_MAX_SECTORS;
120 module_param(max_sectors, int, 0444);
121 MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = "
122 __stringify(SBP2_MAX_SECTORS) ")");
125 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
126 * do an exclusive login, as it's generally unsafe to have two hosts
127 * talking to a single sbp2 device at the same time (filesystem coherency,
128 * etc.). If you're running an sbp2 device that supports multiple logins,
129 * and you're either running read-only filesystems or some sort of special
130 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
131 * File System, or Lustre, then set exclusive_login to zero.
133 * So far only bridges from Oxford Semiconductor are known to support
134 * concurrent logins. Depending on firmware, four or two concurrent logins
135 * are possible on OXFW911 and newer Oxsemi bridges.
137 static int exclusive_login = 1;
138 module_param(exclusive_login, int, 0644);
139 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
142 * If any of the following workarounds is required for your device to work,
143 * please submit the kernel messages logged by sbp2 to the linux1394-devel
146 * - 128kB max transfer
147 * Limit transfer size. Necessary for some old bridges.
150 * When scsi_mod probes the device, let the inquiry command look like that
154 * Suppress sending of mode_sense for mode page 8 if the device pretends to
155 * support the SCSI Primary Block commands instead of Reduced Block Commands.
158 * Tell sd_mod to correct the last sector number reported by read_capacity.
159 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
160 * Don't use this with devices which don't have this bug.
162 * - override internal blacklist
163 * Instead of adding to the built-in blacklist, use only the workarounds
164 * specified in the module load parameter.
165 * Useful if a blacklist entry interfered with a non-broken device.
167 static int sbp2_default_workarounds;
168 module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
169 MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
170 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
171 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
172 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
173 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
174 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
175 ", or a combination)");
177 /* legacy parameter */
178 static int force_inquiry_hack;
179 module_param(force_inquiry_hack, int, 0644);
180 MODULE_PARM_DESC(force_inquiry_hack, "Deprecated, use 'workarounds'");
183 * Export information about protocols/devices supported by this driver.
185 static struct ieee1394_device_id sbp2_id_table[] = {
187 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
188 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
189 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
193 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
196 * Debug levels, configured via kernel config, or enable here.
199 #define CONFIG_IEEE1394_SBP2_DEBUG 0
200 /* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
201 /* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
202 /* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
203 /* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
204 /* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
206 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
207 #define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
208 static u32 global_outstanding_command_orbs = 0;
209 #define outstanding_orb_incr global_outstanding_command_orbs++
210 #define outstanding_orb_decr global_outstanding_command_orbs--
212 #define SBP2_ORB_DEBUG(fmt, args...)
213 #define outstanding_orb_incr
214 #define outstanding_orb_decr
217 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
218 #define SBP2_DMA_ALLOC(fmt, args...) \
219 HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
220 ++global_outstanding_dmas, ## args)
221 #define SBP2_DMA_FREE(fmt, args...) \
222 HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
223 --global_outstanding_dmas, ## args)
224 static u32 global_outstanding_dmas = 0;
226 #define SBP2_DMA_ALLOC(fmt, args...)
227 #define SBP2_DMA_FREE(fmt, args...)
230 #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
231 #define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
232 #define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
233 #define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
234 #define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
235 #elif CONFIG_IEEE1394_SBP2_DEBUG == 1
236 #define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
237 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
238 #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
239 #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
241 #define SBP2_DEBUG(fmt, args...)
242 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
243 #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
244 #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
247 #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
248 #define SBP2_DEBUG_ENTER() SBP2_DEBUG("%s", __FUNCTION__)
254 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
257 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
258 u32 scsi_status, struct scsi_cmnd *SCpnt,
259 void (*done)(struct scsi_cmnd *));
261 static struct scsi_host_template scsi_driver_template;
263 static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
265 static void sbp2_host_reset(struct hpsb_host *host);
267 static int sbp2_probe(struct device *dev);
268 static int sbp2_remove(struct device *dev);
269 static int sbp2_update(struct unit_directory *ud);
271 static struct hpsb_highlevel sbp2_highlevel = {
272 .name = SBP2_DEVICE_NAME,
273 .host_reset = sbp2_host_reset,
276 static struct hpsb_address_ops sbp2_ops = {
277 .write = sbp2_handle_status_write
280 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
281 static struct hpsb_address_ops sbp2_physdma_ops = {
282 .read = sbp2_handle_physdma_read,
283 .write = sbp2_handle_physdma_write,
287 static struct hpsb_protocol_driver sbp2_driver = {
288 .name = "SBP2 Driver",
289 .id_table = sbp2_id_table,
290 .update = sbp2_update,
292 .name = SBP2_DEVICE_NAME,
293 .bus = &ieee1394_bus_type,
295 .remove = sbp2_remove,
300 * List of devices with known bugs.
302 * The firmware_revision field, masked with 0xffff00, is the best indicator
303 * for the type of bridge chip of a device. It yields a few false positives
304 * but this did not break correctly behaving devices so far.
306 static const struct {
307 u32 firmware_revision;
309 unsigned workarounds;
310 } sbp2_workarounds_table[] = {
311 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
312 .firmware_revision = 0x002800,
313 .model_id = 0x001010,
314 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
315 SBP2_WORKAROUND_MODE_SENSE_8,
317 /* Initio bridges, actually only needed for some older ones */ {
318 .firmware_revision = 0x000200,
319 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
321 /* Symbios bridge */ {
322 .firmware_revision = 0xa0b800,
323 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
326 * Note about the following Apple iPod blacklist entries:
328 * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
329 * matching logic treats 0 as a wildcard, we cannot match this ID
330 * without rewriting the matching routine. Fortunately these iPods
331 * do not feature the read_capacity bug according to one report.
332 * Read_capacity behaviour as well as model_id could change due to
333 * Apple-supplied firmware updates though.
335 /* iPod 4th generation */ {
336 .firmware_revision = 0x0a2700,
337 .model_id = 0x000021,
338 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
341 .firmware_revision = 0x0a2700,
342 .model_id = 0x000023,
343 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
346 .firmware_revision = 0x0a2700,
347 .model_id = 0x00007e,
348 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
352 /**************************************
353 * General utility functions
354 **************************************/
358 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
360 static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
364 for (length = (length >> 2); length--; )
365 temp[length] = be32_to_cpu(temp[length]);
371 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
373 static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
377 for (length = (length >> 2); length--; )
378 temp[length] = cpu_to_be32(temp[length]);
382 #else /* BIG_ENDIAN */
383 /* Why waste the cpu cycles? */
384 #define sbp2util_be32_to_cpu_buffer(x,y)
385 #define sbp2util_cpu_to_be32_buffer(x,y)
388 #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
390 * Debug packet dump routine. Length is in bytes.
392 static void sbp2util_packet_dump(void *buffer, int length, char *dump_name,
396 unsigned char *dump = buffer;
398 if (!dump || !length || !dump_name)
402 printk("[%s, 0x%x]", dump_name, dump_phys_addr);
404 printk("[%s]", dump_name);
405 for (i = 0; i < length; i++) {
414 printk("%02x ", (int)dump[i]);
421 #define sbp2util_packet_dump(w,x,y,z)
425 * Goofy routine that basically does a down_timeout function.
427 static int sbp2util_down_timeout(atomic_t *done, int timeout)
431 for (i = timeout; (i > 0 && atomic_read(done) == 0); i-= HZ/10) {
432 if (msleep_interruptible(100)) /* 100ms */
435 return (i > 0) ? 0 : 1;
438 /* Free's an allocated packet */
439 static void sbp2_free_packet(struct hpsb_packet *packet)
441 hpsb_free_tlabel(packet);
442 hpsb_free_packet(packet);
445 /* This is much like hpsb_node_write(), except it ignores the response
446 * subaction and returns immediately. Can be used from interrupts.
448 static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
449 quadlet_t *buffer, size_t length)
451 struct hpsb_packet *packet;
453 packet = hpsb_make_writepacket(ne->host, ne->nodeid,
454 addr, buffer, length);
458 hpsb_set_packet_complete_task(packet,
459 (void (*)(void *))sbp2_free_packet,
462 hpsb_node_fill_packet(ne, packet);
464 if (hpsb_send_packet(packet) < 0) {
465 sbp2_free_packet(packet);
473 * This function is called to create a pool of command orbs used for
474 * command processing. It is called when a new sbp2 device is detected.
476 static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
478 struct sbp2scsi_host_info *hi = scsi_id->hi;
480 unsigned long flags, orbs;
481 struct sbp2_command_info *command;
483 orbs = serialize_io ? 2 : SBP2_MAX_CMDS;
485 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
486 for (i = 0; i < orbs; i++) {
487 command = kzalloc(sizeof(*command), GFP_ATOMIC);
489 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock,
493 command->command_orb_dma =
494 pci_map_single(hi->host->pdev, &command->command_orb,
495 sizeof(struct sbp2_command_orb),
496 PCI_DMA_BIDIRECTIONAL);
497 SBP2_DMA_ALLOC("single command orb DMA");
499 pci_map_single(hi->host->pdev,
500 &command->scatter_gather_element,
501 sizeof(command->scatter_gather_element),
502 PCI_DMA_BIDIRECTIONAL);
503 SBP2_DMA_ALLOC("scatter_gather_element");
504 INIT_LIST_HEAD(&command->list);
505 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
507 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
512 * This function is called to delete a pool of command orbs.
514 static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
516 struct hpsb_host *host = scsi_id->hi->host;
517 struct list_head *lh, *next;
518 struct sbp2_command_info *command;
521 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
522 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
523 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
524 command = list_entry(lh, struct sbp2_command_info, list);
526 /* Release our generic DMA's */
527 pci_unmap_single(host->pdev, command->command_orb_dma,
528 sizeof(struct sbp2_command_orb),
529 PCI_DMA_BIDIRECTIONAL);
530 SBP2_DMA_FREE("single command orb DMA");
531 pci_unmap_single(host->pdev, command->sge_dma,
532 sizeof(command->scatter_gather_element),
533 PCI_DMA_BIDIRECTIONAL);
534 SBP2_DMA_FREE("scatter_gather_element");
539 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
544 * This function finds the sbp2_command for a given outstanding command
545 * orb.Only looks at the inuse list.
547 static struct sbp2_command_info *sbp2util_find_command_for_orb(
548 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
550 struct sbp2_command_info *command;
553 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
554 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
555 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
556 if (command->command_orb_dma == orb) {
557 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
562 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
564 SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
570 * This function finds the sbp2_command for a given outstanding SCpnt.
571 * Only looks at the inuse list.
572 * Must be called with scsi_id->sbp2_command_orb_lock held.
574 static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
575 struct scsi_id_instance_data *scsi_id, void *SCpnt)
577 struct sbp2_command_info *command;
579 if (!list_empty(&scsi_id->sbp2_command_orb_inuse))
580 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list)
581 if (command->Current_SCpnt == SCpnt)
587 * This function allocates a command orb used to send a scsi command.
589 static struct sbp2_command_info *sbp2util_allocate_command_orb(
590 struct scsi_id_instance_data *scsi_id,
591 struct scsi_cmnd *Current_SCpnt,
592 void (*Current_done)(struct scsi_cmnd *))
594 struct list_head *lh;
595 struct sbp2_command_info *command = NULL;
598 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
599 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
600 lh = scsi_id->sbp2_command_orb_completed.next;
602 command = list_entry(lh, struct sbp2_command_info, list);
603 command->Current_done = Current_done;
604 command->Current_SCpnt = Current_SCpnt;
605 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
607 SBP2_ERR("%s: no orbs available", __FUNCTION__);
609 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
614 static void sbp2util_free_command_dma(struct sbp2_command_info *command)
616 struct scsi_id_instance_data *scsi_id =
617 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
618 struct hpsb_host *host;
621 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
625 host = scsi_id->ud->ne->host;
627 if (command->cmd_dma) {
628 if (command->dma_type == CMD_DMA_SINGLE) {
629 pci_unmap_single(host->pdev, command->cmd_dma,
630 command->dma_size, command->dma_dir);
631 SBP2_DMA_FREE("single bulk");
632 } else if (command->dma_type == CMD_DMA_PAGE) {
633 pci_unmap_page(host->pdev, command->cmd_dma,
634 command->dma_size, command->dma_dir);
635 SBP2_DMA_FREE("single page");
636 } /* XXX: Check for CMD_DMA_NONE bug */
637 command->dma_type = CMD_DMA_NONE;
638 command->cmd_dma = 0;
641 if (command->sge_buffer) {
642 pci_unmap_sg(host->pdev, command->sge_buffer,
643 command->dma_size, command->dma_dir);
644 SBP2_DMA_FREE("scatter list");
645 command->sge_buffer = NULL;
650 * This function moves a command to the completed orb list.
651 * Must be called with scsi_id->sbp2_command_orb_lock held.
653 static void sbp2util_mark_command_completed(
654 struct scsi_id_instance_data *scsi_id,
655 struct sbp2_command_info *command)
657 list_del(&command->list);
658 sbp2util_free_command_dma(command);
659 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
663 * Is scsi_id valid? Is the 1394 node still present?
665 static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
667 return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
670 /*********************************************
671 * IEEE-1394 core driver stack related section
672 *********************************************/
673 static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud);
675 static int sbp2_probe(struct device *dev)
677 struct unit_directory *ud;
678 struct scsi_id_instance_data *scsi_id;
682 ud = container_of(dev, struct unit_directory, device);
684 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
686 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
689 scsi_id = sbp2_alloc_device(ud);
694 sbp2_parse_unit_directory(scsi_id, ud);
696 return sbp2_start_device(scsi_id);
699 static int sbp2_remove(struct device *dev)
701 struct unit_directory *ud;
702 struct scsi_id_instance_data *scsi_id;
703 struct scsi_device *sdev;
707 ud = container_of(dev, struct unit_directory, device);
708 scsi_id = ud->device.driver_data;
712 if (scsi_id->scsi_host) {
713 /* Get rid of enqueued commands if there is no chance to
715 if (!sbp2util_node_is_available(scsi_id))
716 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
717 /* scsi_remove_device() will trigger shutdown functions of SCSI
718 * highlevel drivers which would deadlock if blocked. */
719 scsi_unblock_requests(scsi_id->scsi_host);
721 sdev = scsi_id->sdev;
723 scsi_id->sdev = NULL;
724 scsi_remove_device(sdev);
727 sbp2_logout_device(scsi_id);
728 sbp2_remove_device(scsi_id);
733 static int sbp2_update(struct unit_directory *ud)
735 struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
739 if (sbp2_reconnect_device(scsi_id)) {
742 * Ok, reconnect has failed. Perhaps we didn't
743 * reconnect fast enough. Try doing a regular login, but
744 * first do a logout just in case of any weirdness.
746 sbp2_logout_device(scsi_id);
748 if (sbp2_login_device(scsi_id)) {
749 /* Login failed too, just fail, and the backend
750 * will call our sbp2_remove for us */
751 SBP2_ERR("Failed to reconnect to sbp2 device!");
756 /* Set max retries to something large on the device. */
757 sbp2_set_busy_timeout(scsi_id);
759 /* Do a SBP-2 fetch agent reset. */
760 sbp2_agent_reset(scsi_id, 1);
762 /* Get the max speed and packet size that we can use. */
763 sbp2_max_speed_and_size(scsi_id);
765 /* Complete any pending commands with busy (so they get
766 * retried) and remove them from our queue
768 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
770 /* Make sure we unblock requests (since this is likely after a bus
772 scsi_unblock_requests(scsi_id->scsi_host);
777 /* This functions is called by the sbp2_probe, for each new device. We now
778 * allocate one scsi host for each scsi_id (unit directory). */
779 static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
781 struct sbp2scsi_host_info *hi;
782 struct Scsi_Host *scsi_host = NULL;
783 struct scsi_id_instance_data *scsi_id = NULL;
787 scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
789 SBP2_ERR("failed to create scsi_id");
793 scsi_id->ne = ud->ne;
795 scsi_id->speed_code = IEEE1394_SPEED_100;
796 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
797 scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
798 atomic_set(&scsi_id->sbp2_login_complete, 0);
799 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
800 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
801 INIT_LIST_HEAD(&scsi_id->scsi_list);
802 spin_lock_init(&scsi_id->sbp2_command_orb_lock);
804 ud->device.driver_data = scsi_id;
806 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
808 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
810 SBP2_ERR("failed to allocate hostinfo");
813 SBP2_DEBUG("sbp2_alloc_device: allocated hostinfo");
814 hi->host = ud->ne->host;
815 INIT_LIST_HEAD(&hi->scsi_ids);
817 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
818 /* Handle data movement if physical dma is not
819 * enabled or not supported on host controller */
820 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
822 0x0ULL, 0xfffffffcULL)) {
823 SBP2_ERR("failed to register lower 4GB address range");
829 /* Prevent unloading of the 1394 host */
830 if (!try_module_get(hi->host->driver->owner)) {
831 SBP2_ERR("failed to get a reference on 1394 host driver");
837 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
839 /* Register the status FIFO address range. We could use the same FIFO
840 * for targets at different nodes. However we need different FIFOs per
841 * target in order to support multi-unit devices.
842 * The FIFO is located out of the local host controller's physical range
843 * but, if possible, within the posted write area. Status writes will
844 * then be performed as unified transactions. This slightly reduces
845 * bandwidth usage, and some Prolific based devices seem to require it.
847 scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
848 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
849 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
850 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
851 if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
852 SBP2_ERR("failed to allocate status FIFO address range");
856 /* Register our host with the SCSI stack. */
857 scsi_host = scsi_host_alloc(&scsi_driver_template,
858 sizeof(unsigned long));
860 SBP2_ERR("failed to register scsi host");
864 scsi_host->hostdata[0] = (unsigned long)scsi_id;
866 if (!scsi_add_host(scsi_host, &ud->device)) {
867 scsi_id->scsi_host = scsi_host;
871 SBP2_ERR("failed to add scsi host");
872 scsi_host_put(scsi_host);
875 sbp2_remove_device(scsi_id);
879 static void sbp2_host_reset(struct hpsb_host *host)
881 struct sbp2scsi_host_info *hi;
882 struct scsi_id_instance_data *scsi_id;
884 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
887 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
888 scsi_block_requests(scsi_id->scsi_host);
893 * This function is where we first pull the node unique ids, and then
894 * allocate memory and register a SBP-2 device.
896 static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
898 struct sbp2scsi_host_info *hi = scsi_id->hi;
904 scsi_id->login_response =
905 pci_alloc_consistent(hi->host->pdev,
906 sizeof(struct sbp2_login_response),
907 &scsi_id->login_response_dma);
908 if (!scsi_id->login_response)
910 SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
912 /* Query logins ORB DMA */
913 scsi_id->query_logins_orb =
914 pci_alloc_consistent(hi->host->pdev,
915 sizeof(struct sbp2_query_logins_orb),
916 &scsi_id->query_logins_orb_dma);
917 if (!scsi_id->query_logins_orb)
919 SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
921 /* Query logins response DMA */
922 scsi_id->query_logins_response =
923 pci_alloc_consistent(hi->host->pdev,
924 sizeof(struct sbp2_query_logins_response),
925 &scsi_id->query_logins_response_dma);
926 if (!scsi_id->query_logins_response)
928 SBP2_DMA_ALLOC("consistent DMA region for query logins response");
930 /* Reconnect ORB DMA */
931 scsi_id->reconnect_orb =
932 pci_alloc_consistent(hi->host->pdev,
933 sizeof(struct sbp2_reconnect_orb),
934 &scsi_id->reconnect_orb_dma);
935 if (!scsi_id->reconnect_orb)
937 SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
940 scsi_id->logout_orb =
941 pci_alloc_consistent(hi->host->pdev,
942 sizeof(struct sbp2_logout_orb),
943 &scsi_id->logout_orb_dma);
944 if (!scsi_id->logout_orb)
946 SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
950 pci_alloc_consistent(hi->host->pdev,
951 sizeof(struct sbp2_login_orb),
952 &scsi_id->login_orb_dma);
953 if (!scsi_id->login_orb)
955 SBP2_DMA_ALLOC("consistent DMA region for login ORB");
957 SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id);
960 * Create our command orb pool
962 if (sbp2util_create_command_orb_pool(scsi_id)) {
963 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
964 sbp2_remove_device(scsi_id);
968 /* Schedule a timeout here. The reason is that we may be so close
969 * to a bus reset, that the device is not available for logins.
970 * This can happen when the bus reset is caused by the host
971 * connected to the sbp2 device being removed. That host would
972 * have a certain amount of time to relogin before the sbp2 device
973 * allows someone else to login instead. One second makes sense. */
974 msleep_interruptible(1000);
975 if (signal_pending(current)) {
976 sbp2_remove_device(scsi_id);
981 * Login to the sbp-2 device
983 if (sbp2_login_device(scsi_id)) {
984 /* Login failed, just remove the device. */
985 sbp2_remove_device(scsi_id);
990 * Set max retries to something large on the device
992 sbp2_set_busy_timeout(scsi_id);
995 * Do a SBP-2 fetch agent reset
997 sbp2_agent_reset(scsi_id, 1);
1000 * Get the max speed and packet size that we can use
1002 sbp2_max_speed_and_size(scsi_id);
1004 /* Add this device to the scsi layer now */
1005 error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
1007 SBP2_ERR("scsi_add_device failed");
1008 sbp2_logout_device(scsi_id);
1009 sbp2_remove_device(scsi_id);
1016 SBP2_ERR("Could not allocate memory for scsi_id");
1017 sbp2_remove_device(scsi_id);
1022 * This function removes an sbp2 device from the sbp2scsi_host_info struct.
1024 static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
1026 struct sbp2scsi_host_info *hi;
1035 /* This will remove our scsi device aswell */
1036 if (scsi_id->scsi_host) {
1037 scsi_remove_host(scsi_id->scsi_host);
1038 scsi_host_put(scsi_id->scsi_host);
1041 sbp2util_remove_command_orb_pool(scsi_id);
1043 list_del(&scsi_id->scsi_list);
1045 if (scsi_id->login_response) {
1046 pci_free_consistent(hi->host->pdev,
1047 sizeof(struct sbp2_login_response),
1048 scsi_id->login_response,
1049 scsi_id->login_response_dma);
1050 SBP2_DMA_FREE("single login FIFO");
1053 if (scsi_id->login_orb) {
1054 pci_free_consistent(hi->host->pdev,
1055 sizeof(struct sbp2_login_orb),
1057 scsi_id->login_orb_dma);
1058 SBP2_DMA_FREE("single login ORB");
1061 if (scsi_id->reconnect_orb) {
1062 pci_free_consistent(hi->host->pdev,
1063 sizeof(struct sbp2_reconnect_orb),
1064 scsi_id->reconnect_orb,
1065 scsi_id->reconnect_orb_dma);
1066 SBP2_DMA_FREE("single reconnect orb");
1069 if (scsi_id->logout_orb) {
1070 pci_free_consistent(hi->host->pdev,
1071 sizeof(struct sbp2_logout_orb),
1072 scsi_id->logout_orb,
1073 scsi_id->logout_orb_dma);
1074 SBP2_DMA_FREE("single logout orb");
1077 if (scsi_id->query_logins_orb) {
1078 pci_free_consistent(hi->host->pdev,
1079 sizeof(struct sbp2_query_logins_orb),
1080 scsi_id->query_logins_orb,
1081 scsi_id->query_logins_orb_dma);
1082 SBP2_DMA_FREE("single query logins orb");
1085 if (scsi_id->query_logins_response) {
1086 pci_free_consistent(hi->host->pdev,
1087 sizeof(struct sbp2_query_logins_response),
1088 scsi_id->query_logins_response,
1089 scsi_id->query_logins_response_dma);
1090 SBP2_DMA_FREE("single query logins data");
1093 if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
1094 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
1095 scsi_id->status_fifo_addr);
1097 scsi_id->ud->device.driver_data = NULL;
1100 module_put(hi->host->driver->owner);
1102 SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id);
1107 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1109 * This function deals with physical dma write requests (for adapters that do not support
1110 * physical dma in hardware). Mostly just here for debugging...
1112 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1113 int destid, quadlet_t *data, u64 addr,
1114 size_t length, u16 flags)
1118 * Manually put the data in the right place.
1120 memcpy(bus_to_virt((u32) addr), data, length);
1121 sbp2util_packet_dump(data, length, "sbp2 phys dma write by device",
1123 return RCODE_COMPLETE;
1127 * This function deals with physical dma read requests (for adapters that do not support
1128 * physical dma in hardware). Mostly just here for debugging...
1130 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1131 quadlet_t *data, u64 addr, size_t length,
1136 * Grab data from memory and send a read response.
1138 memcpy(data, bus_to_virt((u32) addr), length);
1139 sbp2util_packet_dump(data, length, "sbp2 phys dma read by device",
1141 return RCODE_COMPLETE;
1145 /**************************************
1146 * SBP-2 protocol related section
1147 **************************************/
1150 * This function queries the device for the maximum concurrent logins it
1153 static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1155 struct sbp2scsi_host_info *hi = scsi_id->hi;
1162 scsi_id->query_logins_orb->reserved1 = 0x0;
1163 scsi_id->query_logins_orb->reserved2 = 0x0;
1165 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1166 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1168 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1169 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1170 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
1172 scsi_id->query_logins_orb->reserved_resp_length =
1173 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
1175 scsi_id->query_logins_orb->status_fifo_hi =
1176 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1177 scsi_id->query_logins_orb->status_fifo_lo =
1178 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1180 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1182 sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
1183 "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
1185 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1186 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1188 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1189 data[1] = scsi_id->query_logins_orb_dma;
1190 sbp2util_cpu_to_be32_buffer(data, 8);
1192 atomic_set(&scsi_id->sbp2_login_complete, 0);
1194 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1196 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 2*HZ)) {
1197 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1201 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1202 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1206 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1207 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1208 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1210 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1214 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1216 SBP2_DEBUG("length_max_logins = %x",
1217 (unsigned int)scsi_id->query_logins_response->length_max_logins);
1219 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
1220 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1222 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
1223 SBP2_INFO("Number of active logins: %d", active_logins);
1225 if (active_logins >= max_logins) {
1233 * This function is called in order to login to a particular SBP-2 device,
1234 * after a bus reset.
1236 static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1238 struct sbp2scsi_host_info *hi = scsi_id->hi;
1243 if (!scsi_id->login_orb) {
1244 SBP2_DEBUG("%s: login_orb not alloc'd!", __FUNCTION__);
1248 if (!exclusive_login) {
1249 if (sbp2_query_logins(scsi_id)) {
1250 SBP2_INFO("Device does not support any more concurrent logins");
1255 /* Set-up login ORB, assume no password */
1256 scsi_id->login_orb->password_hi = 0;
1257 scsi_id->login_orb->password_lo = 0;
1259 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1260 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1262 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1263 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
1264 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
1265 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
1266 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
1268 scsi_id->login_orb->passwd_resp_lengths =
1269 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1271 scsi_id->login_orb->status_fifo_hi =
1272 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1273 scsi_id->login_orb->status_fifo_lo =
1274 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1276 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1278 sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
1279 "sbp2 login orb", scsi_id->login_orb_dma);
1281 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1282 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1284 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1285 data[1] = scsi_id->login_orb_dma;
1286 sbp2util_cpu_to_be32_buffer(data, 8);
1288 atomic_set(&scsi_id->sbp2_login_complete, 0);
1290 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1293 * Wait for login status (up to 20 seconds)...
1295 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 20*HZ)) {
1296 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1301 * Sanity. Make sure status returned matches login orb.
1303 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1304 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1311 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1312 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1313 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1315 SBP2_ERR("Error logging into SBP-2 device - login failed");
1320 * Byte swap the login response, for use when reconnecting or
1323 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1326 * Grab our command block agent address from the login response.
1328 SBP2_DEBUG("command_block_agent_hi = %x",
1329 (unsigned int)scsi_id->login_response->command_block_agent_hi);
1330 SBP2_DEBUG("command_block_agent_lo = %x",
1331 (unsigned int)scsi_id->login_response->command_block_agent_lo);
1333 scsi_id->sbp2_command_block_agent_addr =
1334 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1335 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1336 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1338 SBP2_INFO("Logged into SBP-2 device");
1345 * This function is called in order to logout from a particular SBP-2
1346 * device, usually called during driver unload.
1348 static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1350 struct sbp2scsi_host_info *hi = scsi_id->hi;
1359 scsi_id->logout_orb->reserved1 = 0x0;
1360 scsi_id->logout_orb->reserved2 = 0x0;
1361 scsi_id->logout_orb->reserved3 = 0x0;
1362 scsi_id->logout_orb->reserved4 = 0x0;
1364 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1365 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1367 /* Notify us when complete */
1368 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1370 scsi_id->logout_orb->reserved5 = 0x0;
1371 scsi_id->logout_orb->status_fifo_hi =
1372 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1373 scsi_id->logout_orb->status_fifo_lo =
1374 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1377 * Byte swap ORB if necessary
1379 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1381 sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
1382 "sbp2 logout orb", scsi_id->logout_orb_dma);
1385 * Ok, let's write to the target's management agent register
1387 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1388 data[1] = scsi_id->logout_orb_dma;
1389 sbp2util_cpu_to_be32_buffer(data, 8);
1391 atomic_set(&scsi_id->sbp2_login_complete, 0);
1393 error = hpsb_node_write(scsi_id->ne,
1394 scsi_id->sbp2_management_agent_addr, data, 8);
1398 /* Wait for device to logout...1 second. */
1399 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ))
1402 SBP2_INFO("Logged out of SBP-2 device");
1409 * This function is called in order to reconnect to a particular SBP-2
1410 * device, after a bus reset.
1412 static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1414 struct sbp2scsi_host_info *hi = scsi_id->hi;
1421 * Set-up reconnect ORB
1423 scsi_id->reconnect_orb->reserved1 = 0x0;
1424 scsi_id->reconnect_orb->reserved2 = 0x0;
1425 scsi_id->reconnect_orb->reserved3 = 0x0;
1426 scsi_id->reconnect_orb->reserved4 = 0x0;
1428 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1429 scsi_id->reconnect_orb->login_ID_misc |=
1430 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1432 /* Notify us when complete */
1433 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1435 scsi_id->reconnect_orb->reserved5 = 0x0;
1436 scsi_id->reconnect_orb->status_fifo_hi =
1437 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1438 scsi_id->reconnect_orb->status_fifo_lo =
1439 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1442 * Byte swap ORB if necessary
1444 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1446 sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
1447 "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
1450 * Initialize status fifo
1452 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1455 * Ok, let's write to the target's management agent register
1457 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1458 data[1] = scsi_id->reconnect_orb_dma;
1459 sbp2util_cpu_to_be32_buffer(data, 8);
1461 atomic_set(&scsi_id->sbp2_login_complete, 0);
1463 error = hpsb_node_write(scsi_id->ne,
1464 scsi_id->sbp2_management_agent_addr, data, 8);
1469 * Wait for reconnect status (up to 1 second)...
1471 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) {
1472 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1477 * Sanity. Make sure status returned matches reconnect orb.
1479 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1480 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1487 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1488 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1489 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1491 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect failed");
1495 HPSB_DEBUG("Reconnected to SBP-2 device");
1502 * This function is called in order to set the busy timeout (number of
1503 * retries to attempt) on the sbp2 device.
1505 static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1511 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1512 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1513 SBP2_ERR("%s error", __FUNCTION__);
1518 * This function is called to parse sbp2 device's config rom unit
1519 * directory. Used to determine things like sbp2 management agent offset,
1520 * and command set used (SCSI or RBC).
1522 static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1523 struct unit_directory *ud)
1525 struct csr1212_keyval *kv;
1526 struct csr1212_dentry *dentry;
1527 u64 management_agent_addr;
1528 u32 command_set_spec_id, command_set, unit_characteristics,
1530 unsigned workarounds;
1535 management_agent_addr = 0x0;
1536 command_set_spec_id = 0x0;
1538 unit_characteristics = 0x0;
1539 firmware_revision = 0x0;
1541 /* Handle different fields in the unit directory, based on keys */
1542 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1543 switch (kv->key.id) {
1544 case CSR1212_KV_ID_DEPENDENT_INFO:
1545 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET) {
1546 /* Save off the management agent address */
1547 management_agent_addr =
1548 CSR1212_REGISTER_SPACE_BASE +
1549 (kv->value.csr_offset << 2);
1551 SBP2_DEBUG("sbp2_management_agent_addr = %x",
1552 (unsigned int)management_agent_addr);
1553 } else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
1555 ORB_SET_LUN(kv->value.immediate);
1559 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1560 /* Command spec organization */
1561 command_set_spec_id = kv->value.immediate;
1562 SBP2_DEBUG("sbp2_command_set_spec_id = %x",
1563 (unsigned int)command_set_spec_id);
1566 case SBP2_COMMAND_SET_KEY:
1567 /* Command set used by sbp2 device */
1568 command_set = kv->value.immediate;
1569 SBP2_DEBUG("sbp2_command_set = %x",
1570 (unsigned int)command_set);
1573 case SBP2_UNIT_CHARACTERISTICS_KEY:
1575 * Unit characterisitcs (orb related stuff
1576 * that I'm not yet paying attention to)
1578 unit_characteristics = kv->value.immediate;
1579 SBP2_DEBUG("sbp2_unit_characteristics = %x",
1580 (unsigned int)unit_characteristics);
1583 case SBP2_FIRMWARE_REVISION_KEY:
1584 /* Firmware revision */
1585 firmware_revision = kv->value.immediate;
1586 SBP2_DEBUG("sbp2_firmware_revision = %x",
1587 (unsigned int)firmware_revision);
1595 workarounds = sbp2_default_workarounds;
1596 if (force_inquiry_hack) {
1597 SBP2_WARN("force_inquiry_hack is deprecated. "
1598 "Use parameter 'workarounds' instead.");
1599 workarounds |= SBP2_WORKAROUND_INQUIRY_36;
1602 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1603 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1604 if (sbp2_workarounds_table[i].firmware_revision &&
1605 sbp2_workarounds_table[i].firmware_revision !=
1606 (firmware_revision & 0xffff00))
1608 if (sbp2_workarounds_table[i].model_id &&
1609 sbp2_workarounds_table[i].model_id != ud->model_id)
1611 workarounds |= sbp2_workarounds_table[i].workarounds;
1616 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1617 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1618 " model_id 0x%06x)",
1619 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1620 workarounds, firmware_revision,
1621 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1624 /* We would need one SCSI host template for each target to adjust
1625 * max_sectors on the fly, therefore warn only. */
1626 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1627 (max_sectors * 512) > (128 * 1024))
1628 SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
1629 "max transfer size. WARNING: Current max_sectors "
1630 "setting is larger than 128KB (%d sectors)",
1631 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1634 /* If this is a logical unit directory entry, process the parent
1635 * to get the values. */
1636 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1637 struct unit_directory *parent_ud =
1638 container_of(ud->device.parent, struct unit_directory, device);
1639 sbp2_parse_unit_directory(scsi_id, parent_ud);
1641 scsi_id->sbp2_management_agent_addr = management_agent_addr;
1642 scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
1643 scsi_id->sbp2_command_set = command_set;
1644 scsi_id->sbp2_unit_characteristics = unit_characteristics;
1645 scsi_id->sbp2_firmware_revision = firmware_revision;
1646 scsi_id->workarounds = workarounds;
1647 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
1648 scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun);
1652 #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1655 * This function is called in order to determine the max speed and packet
1656 * size we can use in our ORBs. Note, that we (the driver and host) only
1657 * initiate the transaction. The SBP-2 device actually transfers the data
1658 * (by reading from the DMA area we tell it). This means that the SBP-2
1659 * device decides the actual maximum data it can transfer. We just tell it
1660 * the speed that it needs to use, and the max_rec the host supports, and
1661 * it takes care of the rest.
1663 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1665 struct sbp2scsi_host_info *hi = scsi_id->hi;
1670 scsi_id->speed_code =
1671 hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
1673 /* Bump down our speed if the user requested it */
1674 if (scsi_id->speed_code > max_speed) {
1675 scsi_id->speed_code = max_speed;
1676 SBP2_ERR("Forcing SBP-2 max speed down to %s",
1677 hpsb_speedto_str[scsi_id->speed_code]);
1680 /* Payload size is the lesser of what our speed supports and what
1681 * our host supports. */
1682 payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1683 (u8) (hi->host->csr.max_rec - 1));
1685 /* If physical DMA is off, work around limitation in ohci1394:
1686 * packet size must not exceed PAGE_SIZE */
1687 if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
1688 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1692 HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1693 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1694 hpsb_speedto_str[scsi_id->speed_code],
1695 SBP2_PAYLOAD_TO_BYTES(payload));
1697 scsi_id->max_payload_size = payload;
1702 * This function is called in order to perform a SBP-2 agent reset.
1704 static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1712 data = ntohl(SBP2_AGENT_RESET_DATA);
1713 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1716 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1718 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1721 SBP2_ERR("hpsb_node_write failed.\n");
1726 * Need to make sure orb pointer is written on next command
1728 scsi_id->last_orb = NULL;
1733 static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1734 struct sbp2scsi_host_info *hi,
1735 struct sbp2_command_info *command,
1736 unsigned int scsi_use_sg,
1737 struct scatterlist *sgpnt,
1739 enum dma_data_direction dma_dir)
1741 command->dma_dir = dma_dir;
1742 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1743 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1745 /* Special case if only one element (and less than 64KB in size) */
1746 if ((scsi_use_sg == 1) &&
1747 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1749 SBP2_DEBUG("Only one s/g element");
1750 command->dma_size = sgpnt[0].length;
1751 command->dma_type = CMD_DMA_PAGE;
1752 command->cmd_dma = pci_map_page(hi->host->pdev,
1757 SBP2_DMA_ALLOC("single page scatter element");
1759 orb->data_descriptor_lo = command->cmd_dma;
1760 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1763 struct sbp2_unrestricted_page_table *sg_element =
1764 &command->scatter_gather_element[0];
1765 u32 sg_count, sg_len;
1767 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
1770 SBP2_DMA_ALLOC("scatter list");
1772 command->dma_size = scsi_use_sg;
1773 command->sge_buffer = sgpnt;
1775 /* use page tables (s/g) */
1776 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1777 orb->data_descriptor_lo = command->sge_dma;
1780 * Loop through and fill out our sbp-2 page tables
1781 * (and split up anything too large)
1783 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1784 sg_len = sg_dma_len(sgpnt);
1785 sg_addr = sg_dma_address(sgpnt);
1787 sg_element[sg_count].segment_base_lo = sg_addr;
1788 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1789 sg_element[sg_count].length_segment_base_hi =
1790 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1791 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1792 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1794 sg_element[sg_count].length_segment_base_hi =
1795 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1802 /* Number of page table (s/g) elements */
1803 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1805 sbp2util_packet_dump(sg_element,
1806 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1807 "sbp2 s/g list", command->sge_dma);
1809 /* Byte swap page tables if necessary */
1810 sbp2util_cpu_to_be32_buffer(sg_element,
1811 (sizeof(struct sbp2_unrestricted_page_table)) *
1816 static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1817 struct sbp2scsi_host_info *hi,
1818 struct sbp2_command_info *command,
1819 struct scatterlist *sgpnt,
1821 unsigned int scsi_request_bufflen,
1822 void *scsi_request_buffer,
1823 enum dma_data_direction dma_dir)
1825 command->dma_dir = dma_dir;
1826 command->dma_size = scsi_request_bufflen;
1827 command->dma_type = CMD_DMA_SINGLE;
1828 command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
1829 command->dma_size, command->dma_dir);
1830 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1831 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1833 SBP2_DMA_ALLOC("single bulk");
1836 * Handle case where we get a command w/o s/g enabled (but
1837 * check for transfers larger than 64K)
1839 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1841 orb->data_descriptor_lo = command->cmd_dma;
1842 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1845 struct sbp2_unrestricted_page_table *sg_element =
1846 &command->scatter_gather_element[0];
1847 u32 sg_count, sg_len;
1851 * Need to turn this into page tables, since the
1852 * buffer is too large.
1854 orb->data_descriptor_lo = command->sge_dma;
1856 /* Use page tables (s/g) */
1857 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1860 * fill out our sbp-2 page tables (and split up
1864 sg_len = scsi_request_bufflen;
1865 sg_addr = command->cmd_dma;
1867 sg_element[sg_count].segment_base_lo = sg_addr;
1868 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1869 sg_element[sg_count].length_segment_base_hi =
1870 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1871 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1872 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1874 sg_element[sg_count].length_segment_base_hi =
1875 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1881 /* Number of page table (s/g) elements */
1882 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1884 sbp2util_packet_dump(sg_element,
1885 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1886 "sbp2 s/g list", command->sge_dma);
1888 /* Byte swap page tables if necessary */
1889 sbp2util_cpu_to_be32_buffer(sg_element,
1890 (sizeof(struct sbp2_unrestricted_page_table)) *
1896 * This function is called to create the actual command orb and s/g list
1897 * out of the scsi command itself.
1899 static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1900 struct sbp2_command_info *command,
1902 unsigned int scsi_use_sg,
1903 unsigned int scsi_request_bufflen,
1904 void *scsi_request_buffer,
1905 enum dma_data_direction dma_dir)
1907 struct sbp2scsi_host_info *hi = scsi_id->hi;
1908 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
1909 struct sbp2_command_orb *command_orb = &command->command_orb;
1913 * Set-up our command ORB..
1915 * NOTE: We're doing unrestricted page tables (s/g), as this is
1916 * best performance (at least with the devices I have). This means
1917 * that data_size becomes the number of s/g elements, and
1918 * page_size should be zero (for unrestricted).
1920 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1921 command_orb->next_ORB_lo = 0x0;
1922 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1923 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
1924 command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
1926 if (dma_dir == DMA_NONE)
1927 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1928 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
1929 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1930 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
1931 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1933 SBP2_WARN("Falling back to DMA_NONE");
1934 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1937 /* Set-up our pagetable stuff */
1938 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1939 SBP2_DEBUG("No data transfer");
1940 command_orb->data_descriptor_hi = 0x0;
1941 command_orb->data_descriptor_lo = 0x0;
1942 command_orb->misc |= ORB_SET_DIRECTION(1);
1943 } else if (scsi_use_sg) {
1944 SBP2_DEBUG("Use scatter/gather");
1945 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
1946 sgpnt, orb_direction, dma_dir);
1948 SBP2_DEBUG("No scatter/gather");
1949 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
1950 orb_direction, scsi_request_bufflen,
1951 scsi_request_buffer, dma_dir);
1954 /* Byte swap command ORB if necessary */
1955 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1957 /* Put our scsi command in the command ORB */
1958 memset(command_orb->cdb, 0, 12);
1959 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1963 * This function is called in order to begin a regular SBP-2 command.
1965 static int sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
1966 struct sbp2_command_info *command)
1968 struct sbp2scsi_host_info *hi = scsi_id->hi;
1969 struct sbp2_command_orb *command_orb = &command->command_orb;
1970 struct node_entry *ne = scsi_id->ne;
1973 outstanding_orb_incr;
1974 SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
1975 command_orb, global_outstanding_command_orbs);
1977 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
1978 sizeof(struct sbp2_command_orb),
1979 PCI_DMA_BIDIRECTIONAL);
1980 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
1981 sizeof(command->scatter_gather_element),
1982 PCI_DMA_BIDIRECTIONAL);
1984 * Check to see if there are any previous orbs to use
1986 if (scsi_id->last_orb == NULL) {
1990 * Ok, let's write to the target's management agent register
1992 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_ORB_POINTER_OFFSET;
1993 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1994 data[1] = command->command_orb_dma;
1995 sbp2util_cpu_to_be32_buffer(data, 8);
1997 SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb);
1999 if (sbp2util_node_write_no_wait(ne, addr, data, 8) < 0) {
2000 SBP2_ERR("sbp2util_node_write_no_wait failed.\n");
2004 SBP2_ORB_DEBUG("write command agent complete");
2006 scsi_id->last_orb = command_orb;
2007 scsi_id->last_orb_dma = command->command_orb_dma;
2013 * We have an orb already sent (maybe or maybe not
2014 * processed) that we can append this orb to. So do so,
2015 * and ring the doorbell. Have to be very careful
2016 * modifying these next orb pointers, as they are accessed
2017 * both by the sbp2 device and us.
2019 scsi_id->last_orb->next_ORB_lo =
2020 cpu_to_be32(command->command_orb_dma);
2021 /* Tells hardware that this pointer is valid */
2022 scsi_id->last_orb->next_ORB_hi = 0x0;
2023 pci_dma_sync_single_for_device(hi->host->pdev,
2024 scsi_id->last_orb_dma,
2025 sizeof(struct sbp2_command_orb),
2026 PCI_DMA_BIDIRECTIONAL);
2031 data = cpu_to_be32(command->command_orb_dma);
2032 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_DOORBELL_OFFSET;
2034 SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb);
2036 if (sbp2util_node_write_no_wait(ne, addr, &data, 4) < 0) {
2037 SBP2_ERR("sbp2util_node_write_no_wait failed");
2041 scsi_id->last_orb = command_orb;
2042 scsi_id->last_orb_dma = command->command_orb_dma;
2049 * This function is called in order to begin a regular SBP-2 command.
2051 static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
2052 struct scsi_cmnd *SCpnt,
2053 void (*done)(struct scsi_cmnd *))
2055 unchar *cmd = (unchar *) SCpnt->cmnd;
2056 unsigned int request_bufflen = SCpnt->request_bufflen;
2057 struct sbp2_command_info *command;
2060 SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
2061 SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2064 * Allocate a command orb and s/g structure
2066 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
2072 * Now actually fill in the comamnd orb and sbp2 s/g list
2074 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
2075 request_bufflen, SCpnt->request_buffer,
2076 SCpnt->sc_data_direction);
2078 sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
2079 "sbp2 command orb", command->command_orb_dma);
2082 * Initialize status fifo
2084 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2087 * Link up the orb, and ring the doorbell if needed
2089 sbp2_link_orb_command(scsi_id, command);
2095 * Translates SBP-2 status into SCSI sense data for check conditions
2097 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
2102 * Ok, it's pretty ugly... ;-)
2104 sense_data[0] = 0x70;
2105 sense_data[1] = 0x0;
2106 sense_data[2] = sbp2_status[9];
2107 sense_data[3] = sbp2_status[12];
2108 sense_data[4] = sbp2_status[13];
2109 sense_data[5] = sbp2_status[14];
2110 sense_data[6] = sbp2_status[15];
2112 sense_data[8] = sbp2_status[16];
2113 sense_data[9] = sbp2_status[17];
2114 sense_data[10] = sbp2_status[18];
2115 sense_data[11] = sbp2_status[19];
2116 sense_data[12] = sbp2_status[10];
2117 sense_data[13] = sbp2_status[11];
2118 sense_data[14] = sbp2_status[20];
2119 sense_data[15] = sbp2_status[21];
2121 return sbp2_status[8] & 0x3f; /* return scsi status */
2125 * This function deals with status writes from the SBP-2 device
2127 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid,
2128 quadlet_t *data, u64 addr, size_t length, u16 fl)
2130 struct sbp2scsi_host_info *hi;
2131 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
2132 struct scsi_cmnd *SCpnt = NULL;
2133 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2134 struct sbp2_command_info *command;
2135 unsigned long flags;
2139 sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
2142 SBP2_ERR("host is NULL - this is bad!");
2143 return RCODE_ADDRESS_ERROR;
2146 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
2149 SBP2_ERR("host info is NULL - this is bad!");
2150 return RCODE_ADDRESS_ERROR;
2154 * Find our scsi_id structure by looking at the status fifo address
2155 * written to by the sbp2 device.
2157 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
2158 if (scsi_id_tmp->ne->nodeid == nodeid &&
2159 scsi_id_tmp->status_fifo_addr == addr) {
2160 scsi_id = scsi_id_tmp;
2166 SBP2_ERR("scsi_id is NULL - device is gone?");
2167 return RCODE_ADDRESS_ERROR;
2171 * Put response into scsi_id status fifo...
2173 memcpy(&scsi_id->status_block, data, length);
2176 * Byte swap first two quadlets (8 bytes) of status for processing
2178 sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8);
2181 * Handle command ORB status here if necessary. First, need to match status with command.
2183 command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo);
2186 SBP2_DEBUG("Found status for command ORB");
2187 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2188 sizeof(struct sbp2_command_orb),
2189 PCI_DMA_BIDIRECTIONAL);
2190 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2191 sizeof(command->scatter_gather_element),
2192 PCI_DMA_BIDIRECTIONAL);
2194 SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
2195 outstanding_orb_decr;
2198 * Matched status with command, now grab scsi command pointers and check status
2200 SCpnt = command->Current_SCpnt;
2201 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
2202 sbp2util_mark_command_completed(scsi_id, command);
2203 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
2208 * See if the target stored any scsi status information
2210 if (STATUS_GET_LENGTH(scsi_id->status_block.ORB_offset_hi_misc) > 1) {
2212 * Translate SBP-2 status to SCSI sense data
2214 SBP2_DEBUG("CHECK CONDITION");
2215 scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer);
2219 * Check to see if the dead bit is set. If so, we'll have to initiate
2220 * a fetch agent reset.
2222 if (STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc)) {
2225 * Initiate a fetch agent reset.
2227 SBP2_DEBUG("Dead bit set - initiating fetch agent reset");
2228 sbp2_agent_reset(scsi_id, 0);
2231 SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
2235 * Check here to see if there are no commands in-use. If there are none, we can
2236 * null out last orb so that next time around we write directly to the orb pointer...
2237 * Quick start saves one 1394 bus transaction.
2239 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
2240 if (list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2241 scsi_id->last_orb = NULL;
2243 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
2248 * It's probably a login/logout/reconnect status.
2250 if ((scsi_id->login_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2251 (scsi_id->query_logins_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2252 (scsi_id->reconnect_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2253 (scsi_id->logout_orb_dma == scsi_id->status_block.ORB_offset_lo)) {
2254 atomic_set(&scsi_id->sbp2_login_complete, 1);
2260 /* Complete the SCSI command. */
2261 SBP2_DEBUG("Completing SCSI command");
2262 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
2263 command->Current_done);
2264 SBP2_ORB_DEBUG("command orb completed");
2267 return RCODE_COMPLETE;
2270 /**************************************
2271 * SCSI interface related section
2272 **************************************/
2275 * This routine is the main request entry routine for doing I/O. It is
2276 * called from the scsi stack directly.
2278 static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
2279 void (*done)(struct scsi_cmnd *))
2281 struct scsi_id_instance_data *scsi_id =
2282 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2283 struct sbp2scsi_host_info *hi;
2284 int result = DID_NO_CONNECT << 16;
2287 #if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
2288 scsi_print_command(SCpnt);
2291 if (!sbp2util_node_is_available(scsi_id))
2297 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
2302 * Until we handle multiple luns, just return selection time-out
2303 * to any IO directed at non-zero LUNs
2305 if (SCpnt->device->lun)
2309 * Check for request sense command, and handle it here
2310 * (autorequest sense)
2312 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
2313 SBP2_DEBUG("REQUEST_SENSE");
2314 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
2315 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2316 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
2321 * Check to see if we are in the middle of a bus reset.
2323 if (!hpsb_node_entry_valid(scsi_id->ne)) {
2324 SBP2_ERR("Bus reset in progress - rejecting command");
2325 result = DID_BUS_BUSY << 16;
2330 * Bidirectional commands are not yet implemented,
2331 * and unknown transfer direction not handled.
2333 if (SCpnt->sc_data_direction == DMA_BIDIRECTIONAL) {
2334 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
2335 result = DID_ERROR << 16;
2340 * Try and send our SCSI command
2342 if (sbp2_send_command(scsi_id, SCpnt, done)) {
2343 SBP2_ERR("Error sending SCSI command");
2344 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
2350 SCpnt->result = result;
2356 * This function is called in order to complete all outstanding SBP-2
2357 * commands (in case of resets, etc.).
2359 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
2362 struct sbp2scsi_host_info *hi = scsi_id->hi;
2363 struct list_head *lh;
2364 struct sbp2_command_info *command;
2365 unsigned long flags;
2369 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
2370 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2371 SBP2_DEBUG("Found pending command to complete");
2372 lh = scsi_id->sbp2_command_orb_inuse.next;
2373 command = list_entry(lh, struct sbp2_command_info, list);
2374 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2375 sizeof(struct sbp2_command_orb),
2376 PCI_DMA_BIDIRECTIONAL);
2377 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2378 sizeof(command->scatter_gather_element),
2379 PCI_DMA_BIDIRECTIONAL);
2380 sbp2util_mark_command_completed(scsi_id, command);
2381 if (command->Current_SCpnt) {
2382 command->Current_SCpnt->result = status << 16;
2383 command->Current_done(command->Current_SCpnt);
2386 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
2392 * This function is called in order to complete a regular SBP-2 command.
2394 * This can be called in interrupt context.
2396 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2397 u32 scsi_status, struct scsi_cmnd *SCpnt,
2398 void (*done)(struct scsi_cmnd *))
2406 SBP2_ERR("SCpnt is NULL");
2411 * If a bus reset is in progress and there was an error, don't
2412 * complete the command, just let it get retried at the end of the
2415 if (!hpsb_node_entry_valid(scsi_id->ne)
2416 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2417 SBP2_ERR("Bus reset in progress - retry command later");
2422 * Switch on scsi status
2424 switch (scsi_status) {
2425 case SBP2_SCSI_STATUS_GOOD:
2426 SCpnt->result = DID_OK << 16;
2429 case SBP2_SCSI_STATUS_BUSY:
2430 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2431 SCpnt->result = DID_BUS_BUSY << 16;
2434 case SBP2_SCSI_STATUS_CHECK_CONDITION:
2435 SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
2436 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
2437 #if CONFIG_IEEE1394_SBP2_DEBUG >= 1
2438 scsi_print_command(SCpnt);
2439 scsi_print_sense(SBP2_DEVICE_NAME, SCpnt);
2443 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2444 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2445 SCpnt->result = DID_NO_CONNECT << 16;
2446 scsi_print_command(SCpnt);
2449 case SBP2_SCSI_STATUS_CONDITION_MET:
2450 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2451 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2452 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2453 SCpnt->result = DID_ERROR << 16;
2454 scsi_print_command(SCpnt);
2458 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2459 SCpnt->result = DID_ERROR << 16;
2463 * If a bus reset is in progress and there was an error, complete
2464 * the command as busy so that it will get retried.
2466 if (!hpsb_node_entry_valid(scsi_id->ne)
2467 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2468 SBP2_ERR("Completing command with busy (bus reset)");
2469 SCpnt->result = DID_BUS_BUSY << 16;
2473 * If a unit attention occurs, return busy status so it gets
2474 * retried... it could have happened because of a 1394 bus reset
2476 * XXX DID_BUS_BUSY is actually a bad idea because it will defy
2477 * the scsi layer's retry logic.
2480 if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
2481 (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
2482 SBP2_DEBUG("UNIT ATTENTION - return busy");
2483 SCpnt->result = DID_BUS_BUSY << 16;
2488 * Tell scsi stack that we're done with this command
2493 static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2495 struct scsi_id_instance_data *scsi_id =
2496 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2498 scsi_id->sdev = sdev;
2500 if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
2501 sdev->inquiry_len = 36;
2505 static int sbp2scsi_slave_configure(struct scsi_device *sdev)
2507 struct scsi_id_instance_data *scsi_id =
2508 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2510 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
2511 sdev->use_10_for_rw = 1;
2512 sdev->use_10_for_ms = 1;
2514 if (sdev->type == TYPE_DISK &&
2515 scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2516 sdev->skip_ms_page_8 = 1;
2517 if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2518 sdev->fix_capacity = 1;
2522 static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2524 ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
2529 * Called by scsi stack when something has really gone wrong. Usually
2530 * called when a command has timed-out for some reason.
2532 static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2534 struct scsi_id_instance_data *scsi_id =
2535 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2536 struct sbp2scsi_host_info *hi = scsi_id->hi;
2537 struct sbp2_command_info *command;
2538 unsigned long flags;
2540 SBP2_ERR("aborting sbp2 command");
2541 scsi_print_command(SCpnt);
2543 if (sbp2util_node_is_available(scsi_id)) {
2546 * Right now, just return any matching command structures
2549 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
2550 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2552 SBP2_DEBUG("Found command to abort");
2553 pci_dma_sync_single_for_cpu(hi->host->pdev,
2554 command->command_orb_dma,
2555 sizeof(struct sbp2_command_orb),
2556 PCI_DMA_BIDIRECTIONAL);
2557 pci_dma_sync_single_for_cpu(hi->host->pdev,
2559 sizeof(command->scatter_gather_element),
2560 PCI_DMA_BIDIRECTIONAL);
2561 sbp2util_mark_command_completed(scsi_id, command);
2562 if (command->Current_SCpnt) {
2563 command->Current_SCpnt->result = DID_ABORT << 16;
2564 command->Current_done(command->Current_SCpnt);
2567 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
2570 * Initiate a fetch agent reset.
2572 sbp2_agent_reset(scsi_id, 0);
2573 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2580 * Called by scsi stack when something has really gone wrong.
2582 static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
2584 struct scsi_id_instance_data *scsi_id =
2585 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2587 SBP2_ERR("reset requested");
2589 if (sbp2util_node_is_available(scsi_id)) {
2590 SBP2_ERR("Generating sbp2 fetch agent reset");
2591 sbp2_agent_reset(scsi_id, 0);
2597 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2598 struct device_attribute *attr,
2601 struct scsi_device *sdev;
2602 struct scsi_id_instance_data *scsi_id;
2605 if (!(sdev = to_scsi_device(dev)))
2608 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2611 lun = ORB_SET_LUN(scsi_id->sbp2_lun);
2613 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
2614 scsi_id->ud->id, lun);
2616 static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
2618 static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
2619 &dev_attr_ieee1394_id,
2623 MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2624 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2625 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2626 MODULE_LICENSE("GPL");
2628 /* SCSI host template */
2629 static struct scsi_host_template scsi_driver_template = {
2630 .module = THIS_MODULE,
2631 .name = "SBP-2 IEEE-1394",
2632 .proc_name = SBP2_DEVICE_NAME,
2633 .queuecommand = sbp2scsi_queuecommand,
2634 .eh_abort_handler = sbp2scsi_abort,
2635 .eh_device_reset_handler = sbp2scsi_reset,
2636 .slave_alloc = sbp2scsi_slave_alloc,
2637 .slave_configure = sbp2scsi_slave_configure,
2638 .slave_destroy = sbp2scsi_slave_destroy,
2640 .sg_tablesize = SG_ALL,
2641 .use_clustering = ENABLE_CLUSTERING,
2642 .cmd_per_lun = SBP2_MAX_CMDS,
2643 .can_queue = SBP2_MAX_CMDS,
2645 .sdev_attrs = sbp2_sysfs_sdev_attrs,
2648 static int sbp2_module_init(void)
2654 /* Module load debug option to force one command at a time (serializing I/O) */
2656 SBP2_INFO("Driver forced to serialize I/O (serialize_io=1)");
2657 SBP2_INFO("Try serialize_io=0 for better performance");
2658 scsi_driver_template.can_queue = 1;
2659 scsi_driver_template.cmd_per_lun = 1;
2662 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2663 (max_sectors * 512) > (128 * 1024))
2664 max_sectors = 128 * 1024 / 512;
2665 scsi_driver_template.max_sectors = max_sectors;
2667 /* Register our high level driver with 1394 stack */
2668 hpsb_register_highlevel(&sbp2_highlevel);
2670 ret = hpsb_register_protocol(&sbp2_driver);
2672 SBP2_ERR("Failed to register protocol");
2673 hpsb_unregister_highlevel(&sbp2_highlevel);
2680 static void __exit sbp2_module_exit(void)
2684 hpsb_unregister_protocol(&sbp2_driver);
2686 hpsb_unregister_highlevel(&sbp2_highlevel);
2689 module_init(sbp2_module_init);
2690 module_exit(sbp2_module_exit);