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/slab.h>
46 #include <linux/interrupt.h>
48 #include <linux/poll.h>
49 #include <linux/module.h>
50 #include <linux/moduleparam.h>
51 #include <linux/types.h>
52 #include <linux/delay.h>
53 #include <linux/sched.h>
54 #include <linux/blkdev.h>
55 #include <linux/smp_lock.h>
56 #include <linux/init.h>
57 #include <linux/version.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>
67 #include <asm/scatterlist.h>
69 #include "../scsi/scsi.h"
70 #include "../scsi/hosts.h"
73 #include "ieee1394_types.h"
74 #include "ieee1394_core.h"
78 #include "highlevel.h"
79 #include "ieee1394_transactions.h"
82 static char version[] __devinitdata =
83 "$Rev: 1010 $ Ben Collins <bcollins@debian.org>";
86 * Module load parameter definitions
90 * Change max_speed on module load if you have a bad IEEE-1394
91 * controller that has trouble running 2KB packets at 400mb.
93 * NOTE: On certain OHCI parts I have seen short packets on async transmit
94 * (probably due to PCI latency/throughput issues with the part). You can
95 * bump down the speed if you are running into problems.
97 static int max_speed = IEEE1394_SPEED_MAX;
98 module_param(max_speed, int, 0644);
99 MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb default, 1 = 200mb, 0 = 100mb)");
102 * Set serialize_io to 1 if you'd like only one scsi command sent
103 * down to us at a time (debugging). This might be necessary for very
104 * badly behaved sbp2 devices.
106 static int serialize_io = 0;
107 module_param(serialize_io, int, 0444);
108 MODULE_PARM_DESC(serialize_io, "Serialize all I/O coming down from the scsi drivers (default = 0)");
111 * Bump up max_sectors if you'd like to support very large sized
112 * transfers. Please note that some older sbp2 bridge chips are broken for
113 * transfers greater or equal to 128KB. Default is a value of 255
114 * sectors, or just under 128KB (at 512 byte sector size). I can note that
115 * the Oxsemi sbp2 chipsets have no problems supporting very large
118 static int max_sectors = SBP2_MAX_SECTORS;
119 module_param(max_sectors, int, 0444);
120 MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = 255)");
123 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
124 * do an exclusive login, as it's generally unsafe to have two hosts
125 * talking to a single sbp2 device at the same time (filesystem coherency,
126 * etc.). If you're running an sbp2 device that supports multiple logins,
127 * and you're either running read-only filesystems or some sort of special
128 * filesystem supporting multiple hosts (one such filesystem is OpenGFS,
129 * see opengfs.sourceforge.net for more info), then set exclusive_login
130 * to zero. Note: The Oxsemi OXFW911 sbp2 chipset supports up to four
133 static int exclusive_login = 1;
134 module_param(exclusive_login, int, 0644);
135 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
138 * SCSI inquiry hack for really badly behaved sbp2 devices. Turn this on
139 * if your sbp2 device is not properly handling the SCSI inquiry command.
140 * This hack makes the inquiry look more like a typical MS Windows
143 * If force_inquiry_hack=1 is required for your device to work,
144 * please submit the logged sbp2_firmware_revision value of this device to
145 * the linux1394-devel mailing list.
147 static int force_inquiry_hack = 0;
148 module_param(force_inquiry_hack, int, 0444);
149 MODULE_PARM_DESC(force_inquiry_hack, "Force SCSI inquiry hack (default = 0)");
153 * Export information about protocols/devices supported by this driver.
155 static struct ieee1394_device_id sbp2_id_table[] = {
157 .match_flags =IEEE1394_MATCH_SPECIFIER_ID |
158 IEEE1394_MATCH_VERSION,
159 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
160 .version = SBP2_SW_VERSION_ENTRY & 0xffffff
165 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
168 * Debug levels, configured via kernel config, or enable here.
171 /* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
172 /* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
173 /* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
174 /* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
175 /* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
177 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
178 #define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
179 static u32 global_outstanding_command_orbs = 0;
180 #define outstanding_orb_incr global_outstanding_command_orbs++
181 #define outstanding_orb_decr global_outstanding_command_orbs--
183 #define SBP2_ORB_DEBUG(fmt, args...)
184 #define outstanding_orb_incr
185 #define outstanding_orb_decr
188 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
189 #define SBP2_DMA_ALLOC(fmt, args...) \
190 HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
191 ++global_outstanding_dmas, ## args)
192 #define SBP2_DMA_FREE(fmt, args...) \
193 HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
194 --global_outstanding_dmas, ## args)
195 static u32 global_outstanding_dmas = 0;
197 #define SBP2_DMA_ALLOC(fmt, args...)
198 #define SBP2_DMA_FREE(fmt, args...)
201 #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
202 #define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
203 #define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
204 #define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
205 #define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
206 #elif CONFIG_IEEE1394_SBP2_DEBUG == 1
207 #define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
208 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
209 #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
210 #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
212 #define SBP2_DEBUG(fmt, args...)
213 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
214 #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
215 #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
218 #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
225 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
228 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
229 u32 scsi_status, Scsi_Cmnd *SCpnt,
230 void (*done)(Scsi_Cmnd *));
232 static Scsi_Host_Template scsi_driver_template;
234 const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
236 static struct hpsb_highlevel sbp2_highlevel = {
237 .name = SBP2_DEVICE_NAME,
238 .remove_host = sbp2_remove_host,
241 static struct hpsb_address_ops sbp2_ops = {
242 .write = sbp2_handle_status_write
245 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
246 static struct hpsb_address_ops sbp2_physdma_ops = {
247 .read = sbp2_handle_physdma_read,
248 .write = sbp2_handle_physdma_write,
252 static struct hpsb_protocol_driver sbp2_driver = {
253 .name = "SBP2 Driver",
254 .id_table = sbp2_id_table,
255 .update = sbp2_update,
257 .name = SBP2_DEVICE_NAME,
258 .bus = &ieee1394_bus_type,
260 .remove = sbp2_remove,
265 /* List of device firmware's that require a forced 36 byte inquiry. */
266 static u32 sbp2_broken_inquiry_list[] = {
267 0x00002800, /* Stefan Richter <richtest@bauwesen.tu-cottbus.de> */
268 /* DViCO Momobay CX-1 */
269 0x00000200 /* Andreas Plesch <plesch@fas.harvard.edu> */
270 /* QPS Fire DVDBurner */
273 #define NUM_BROKEN_INQUIRY_DEVS \
274 (sizeof(sbp2_broken_inquiry_list)/sizeof(*sbp2_broken_inquiry_list))
276 /**************************************
277 * General utility functions
278 **************************************/
283 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
285 static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
289 for (length = (length >> 2); length--; )
290 temp[length] = be32_to_cpu(temp[length]);
296 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
298 static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
302 for (length = (length >> 2); length--; )
303 temp[length] = cpu_to_be32(temp[length]);
307 #else /* BIG_ENDIAN */
308 /* Why waste the cpu cycles? */
309 #define sbp2util_be32_to_cpu_buffer(x,y)
310 #define sbp2util_cpu_to_be32_buffer(x,y)
313 #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
315 * Debug packet dump routine. Length is in bytes.
317 static void sbp2util_packet_dump(void *buffer, int length, char *dump_name, u32 dump_phys_addr)
320 unsigned char *dump = buffer;
322 if (!dump || !length || !dump_name)
326 printk("[%s, 0x%x]", dump_name, dump_phys_addr);
328 printk("[%s]", dump_name);
329 for (i = 0; i < length; i++) {
338 printk("%02x ", (int) dump[i]);
345 #define sbp2util_packet_dump(w,x,y,z)
349 * Goofy routine that basically does a down_timeout function.
351 static int sbp2util_down_timeout(atomic_t *done, int timeout)
355 for (i = timeout; (i > 0 && atomic_read(done) == 0); i-= HZ/10) {
356 set_current_state(TASK_INTERRUPTIBLE);
357 if (schedule_timeout(HZ/10)) /* 100ms */
360 return ((i > 0) ? 0:1);
363 /* Free's an allocated packet */
364 static void sbp2_free_packet(struct hpsb_packet *packet)
366 hpsb_free_tlabel(packet);
367 free_hpsb_packet(packet);
371 * This function is called to retrieve a block write packet from our
372 * packet pool. This function is used in place of calling
373 * alloc_hpsb_packet (which costs us three kmallocs). Instead we just pull
374 * out a free request packet and re-initialize values in it. I'm sure this
375 * can still stand some more optimization.
377 static struct hpsb_packet *
378 sbp2util_allocate_write_packet(struct sbp2scsi_host_info *hi,
379 struct node_entry *ne, u64 addr,
383 struct hpsb_packet *packet;
385 packet = hpsb_make_writepacket(hi->host, ne->nodeid,
386 addr, data, data_size);
391 hpsb_set_packet_complete_task(packet, (void (*)(void*))sbp2_free_packet,
394 hpsb_node_fill_packet(ne, packet);
401 * This function is called to create a pool of command orbs used for
402 * command processing. It is called when a new sbp2 device is detected.
404 static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
406 struct sbp2scsi_host_info *hi = scsi_id->hi;
408 unsigned long flags, orbs;
409 struct sbp2_command_info *command;
411 orbs = serialize_io ? 2 : SBP2_MAX_COMMAND_ORBS;
413 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
414 for (i = 0; i < orbs; i++) {
415 command = (struct sbp2_command_info *)
416 kmalloc(sizeof(struct sbp2_command_info), GFP_ATOMIC);
418 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
421 memset(command, '\0', sizeof(struct sbp2_command_info));
422 command->command_orb_dma =
423 pci_map_single (hi->host->pdev, &command->command_orb,
424 sizeof(struct sbp2_command_orb),
425 PCI_DMA_BIDIRECTIONAL);
426 SBP2_DMA_ALLOC("single command orb DMA");
428 pci_map_single (hi->host->pdev, &command->scatter_gather_element,
429 sizeof(command->scatter_gather_element),
430 PCI_DMA_BIDIRECTIONAL);
431 SBP2_DMA_ALLOC("scatter_gather_element");
432 INIT_LIST_HEAD(&command->list);
433 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
435 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
440 * This function is called to delete a pool of command orbs.
442 static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
444 struct hpsb_host *host = scsi_id->hi->host;
445 struct list_head *lh, *next;
446 struct sbp2_command_info *command;
449 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
450 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
451 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
452 command = list_entry(lh, struct sbp2_command_info, list);
454 /* Release our generic DMA's */
455 pci_unmap_single(host->pdev, command->command_orb_dma,
456 sizeof(struct sbp2_command_orb),
457 PCI_DMA_BIDIRECTIONAL);
458 SBP2_DMA_FREE("single command orb DMA");
459 pci_unmap_single(host->pdev, command->sge_dma,
460 sizeof(command->scatter_gather_element),
461 PCI_DMA_BIDIRECTIONAL);
462 SBP2_DMA_FREE("scatter_gather_element");
467 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
472 * This function finds the sbp2_command for a given outstanding command
473 * orb.Only looks at the inuse list.
475 static struct sbp2_command_info *sbp2util_find_command_for_orb(
476 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
478 struct list_head *lh;
479 struct sbp2_command_info *command;
482 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
483 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
484 list_for_each(lh, &scsi_id->sbp2_command_orb_inuse) {
485 command = list_entry(lh, struct sbp2_command_info, list);
486 if (command->command_orb_dma == orb) {
487 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
492 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
494 SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
500 * This function finds the sbp2_command for a given outstanding SCpnt.
501 * Only looks at the inuse list.
503 static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(struct scsi_id_instance_data *scsi_id, void *SCpnt)
505 struct list_head *lh;
506 struct sbp2_command_info *command;
509 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
510 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
511 list_for_each(lh, &scsi_id->sbp2_command_orb_inuse) {
512 command = list_entry(lh, struct sbp2_command_info, list);
513 if (command->Current_SCpnt == SCpnt) {
514 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
519 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
524 * This function allocates a command orb used to send a scsi command.
526 static struct sbp2_command_info *sbp2util_allocate_command_orb(
527 struct scsi_id_instance_data *scsi_id,
528 Scsi_Cmnd *Current_SCpnt,
529 void (*Current_done)(Scsi_Cmnd *))
531 struct list_head *lh;
532 struct sbp2_command_info *command = NULL;
535 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
536 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
537 lh = scsi_id->sbp2_command_orb_completed.next;
539 command = list_entry(lh, struct sbp2_command_info, list);
540 command->Current_done = Current_done;
541 command->Current_SCpnt = Current_SCpnt;
542 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
544 SBP2_ERR("sbp2util_allocate_command_orb - No orbs available!");
546 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
551 static void sbp2util_free_command_dma(struct sbp2_command_info *command)
553 struct hpsb_host *host;
555 host = hpsb_get_host_bykey(&sbp2_highlevel,
556 (unsigned long)command->Current_SCpnt->device->host);
558 printk(KERN_ERR "%s: host == NULL\n", __FUNCTION__);
562 if (command->cmd_dma) {
563 if (command->dma_type == CMD_DMA_SINGLE) {
564 pci_unmap_single(host->pdev, command->cmd_dma,
565 command->dma_size, command->dma_dir);
566 SBP2_DMA_FREE("single bulk");
567 } else if (command->dma_type == CMD_DMA_PAGE) {
568 pci_unmap_page(host->pdev, command->cmd_dma,
569 command->dma_size, command->dma_dir);
570 SBP2_DMA_FREE("single page");
571 } /* XXX: Check for CMD_DMA_NONE bug */
572 command->dma_type = CMD_DMA_NONE;
573 command->cmd_dma = 0;
576 if (command->sge_buffer) {
577 pci_unmap_sg(host->pdev, command->sge_buffer,
578 command->dma_size, command->dma_dir);
579 SBP2_DMA_FREE("scatter list");
580 command->sge_buffer = NULL;
585 * This function moves a command to the completed orb list.
587 static void sbp2util_mark_command_completed(struct scsi_id_instance_data *scsi_id, struct sbp2_command_info *command)
591 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
592 list_del(&command->list);
593 sbp2util_free_command_dma(command);
594 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
595 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
600 /*********************************************
601 * IEEE-1394 core driver stack related section
602 *********************************************/
604 static int sbp2_probe(struct device *dev)
606 struct unit_directory *ud;
607 struct sbp2scsi_host_info *hi;
609 SBP2_DEBUG(__FUNCTION__);
611 ud = container_of(dev, struct unit_directory, device);
613 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
615 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
618 /* This will only add it if it doesn't exist */
619 hi = sbp2_add_host(ud->ne->host);
624 return sbp2_start_ud(hi, ud);
627 static int sbp2_remove(struct device *dev)
629 struct scsi_id_group *scsi_group;
630 struct list_head *lh, *next;
631 struct unit_directory *ud;
632 struct scsi_id_instance_data *scsi_id;
634 SBP2_DEBUG(__FUNCTION__);
636 ud = container_of(dev, struct unit_directory, device);
637 scsi_group = ud->device.driver_data;
638 ud->device.driver_data = NULL;
640 list_for_each_safe (lh, next, &scsi_group->scsi_id_list) {
641 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
643 if (scsi_id != NULL) {
644 sbp2_logout_device(scsi_id);
645 sbp2_remove_device(scsi_id);
654 static void sbp2_update(struct unit_directory *ud)
656 struct sbp2scsi_host_info *hi;
657 struct scsi_id_group *scsi_group = ud->device.driver_data;
658 struct list_head *lh, *next;
659 struct scsi_id_instance_data *scsi_id;
662 SBP2_DEBUG("sbp2_update");
664 list_for_each_safe (lh, next, &scsi_group->scsi_id_list) {
665 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
669 if (sbp2_reconnect_device(scsi_id)) {
672 * Ok, reconnect has failed. Perhaps we didn't
673 * reconnect fast enough. Try doing a regular login.
675 if (sbp2_login_device(scsi_id)) {
676 /* Login failed too, just remove the device. */
677 SBP2_ERR("sbp2_reconnect_device failed!");
678 sbp2_remove_device(scsi_id);
683 /* Set max retries to something large on the device. */
684 sbp2_set_busy_timeout(scsi_id);
686 /* Do a SBP-2 fetch agent reset. */
687 sbp2_agent_reset(scsi_id, 1);
689 /* Get the max speed and packet size that we can use. */
690 sbp2_max_speed_and_size(scsi_id);
692 /* Complete any pending commands with busy (so they get
693 * retried) and remove them from our queue
695 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
696 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
697 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
701 /* This functions is called by the sbp2_probe, for each new device. If the
702 * host_info already exists, it will return it. If not, it allocated a new
703 * host_info entry and a corresponding scsi_host. */
704 static struct sbp2scsi_host_info *sbp2_add_host(struct hpsb_host *host)
706 struct sbp2scsi_host_info *hi;
707 struct Scsi_Host *scsi_host;
709 SBP2_DEBUG("sbp2_add_host");
711 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
715 /* Register our host with the SCSI stack. */
716 scsi_host = scsi_host_alloc(&scsi_driver_template, 0);
718 SBP2_ERR("failed to register scsi host");
722 hi = hpsb_create_hostinfo(&sbp2_highlevel, host, sizeof(*hi));
724 SBP2_ERR("failed to allocate hostinfo");
725 scsi_host_put(hi->scsi_host);
728 hpsb_set_hostinfo_key(&sbp2_highlevel, host, (unsigned long)scsi_host);
730 hi->scsi_host = scsi_host;
732 hi->sbp2_command_lock = SPIN_LOCK_UNLOCKED;
733 hi->scsi_host->max_id = SBP2SCSI_MAX_SCSI_IDS;
735 /* XXX We need a device to pass here as the scsi-host class. Can't
736 * use the PCI device, since it is already bound to the ieee1394
737 * host. Can't use the fw-host device since it is multi-class
738 * enabled (scsi-host uses classdata member of the device). */
739 if (scsi_add_host(hi->scsi_host, NULL)) {
740 SBP2_ERR("failed to add scsi host");
741 scsi_host_put(hi->scsi_host);
742 hpsb_destroy_hostinfo(&sbp2_highlevel, host);
750 * This function is called when a host is removed.
752 static void sbp2_remove_host(struct hpsb_host *host)
754 struct sbp2scsi_host_info *hi;
756 SBP2_DEBUG("sbp2_remove_host");
758 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
761 scsi_remove_host(hi->scsi_host);
762 scsi_host_put(hi->scsi_host);
766 static int sbp2_start_ud(struct sbp2scsi_host_info *hi, struct unit_directory *ud)
768 struct scsi_id_instance_data *scsi_id;
769 struct scsi_id_group *scsi_group;
770 struct list_head *lh, *next;
772 SBP2_DEBUG("sbp2_start_ud");
774 scsi_group = kmalloc(sizeof(*scsi_group), GFP_KERNEL);
776 SBP2_ERR ("Could not allocate memory for scsi_group");
780 INIT_LIST_HEAD(&scsi_group->scsi_id_list);
781 ud->device.driver_data = scsi_group;
782 sbp2_parse_unit_directory(scsi_group, ud);
784 list_for_each_safe (lh, next, &scsi_group->scsi_id_list) {
785 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
787 scsi_id->ne = ud->ne;
789 scsi_id->speed_code = IEEE1394_SPEED_100;
790 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
791 atomic_set(&scsi_id->sbp2_login_complete, 0);
792 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
793 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
794 scsi_id->sbp2_command_orb_lock = SPIN_LOCK_UNLOCKED;
796 sbp2_start_device(scsi_id);
799 /* Check to see if any of our devices survived the ordeal */
800 if (list_empty(&scsi_group->scsi_id_list)) {
810 * This function is where we first pull the node unique ids, and then
811 * allocate memory and register a SBP-2 device.
813 static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
815 struct sbp2scsi_host_info *hi = scsi_id->hi;
816 struct scsi_device *sdev;
819 SBP2_DEBUG("sbp2_start_device");
822 scsi_id->login_response =
823 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_response),
824 &scsi_id->login_response_dma);
825 if (!scsi_id->login_response)
827 SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
829 /* Query logins ORB DMA */
830 scsi_id->query_logins_orb =
831 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_query_logins_orb),
832 &scsi_id->query_logins_orb_dma);
833 if (!scsi_id->query_logins_orb)
835 SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
837 /* Query logins response DMA */
838 scsi_id->query_logins_response =
839 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_query_logins_response),
840 &scsi_id->query_logins_response_dma);
841 if (!scsi_id->query_logins_response)
843 SBP2_DMA_ALLOC("consistent DMA region for query logins response");
845 /* Reconnect ORB DMA */
846 scsi_id->reconnect_orb =
847 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_reconnect_orb),
848 &scsi_id->reconnect_orb_dma);
849 if (!scsi_id->reconnect_orb)
851 SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
854 scsi_id->logout_orb =
855 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_logout_orb),
856 &scsi_id->logout_orb_dma);
857 if (!scsi_id->logout_orb)
859 SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
863 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_orb),
864 &scsi_id->login_orb_dma);
865 if (!scsi_id->login_orb) {
867 if (scsi_id->query_logins_response) {
868 pci_free_consistent(hi->host->pdev,
869 sizeof(struct sbp2_query_logins_response),
870 scsi_id->query_logins_response,
871 scsi_id->query_logins_response_dma);
872 SBP2_DMA_FREE("query logins response DMA");
875 if (scsi_id->query_logins_orb) {
876 pci_free_consistent(hi->host->pdev,
877 sizeof(struct sbp2_query_logins_orb),
878 scsi_id->query_logins_orb,
879 scsi_id->query_logins_orb_dma);
880 SBP2_DMA_FREE("query logins ORB DMA");
883 if (scsi_id->logout_orb) {
884 pci_free_consistent(hi->host->pdev,
885 sizeof(struct sbp2_logout_orb),
887 scsi_id->logout_orb_dma);
888 SBP2_DMA_FREE("logout ORB DMA");
891 if (scsi_id->reconnect_orb) {
892 pci_free_consistent(hi->host->pdev,
893 sizeof(struct sbp2_reconnect_orb),
894 scsi_id->reconnect_orb,
895 scsi_id->reconnect_orb_dma);
896 SBP2_DMA_FREE("reconnect ORB DMA");
899 if (scsi_id->login_response) {
900 pci_free_consistent(hi->host->pdev,
901 sizeof(struct sbp2_login_response),
902 scsi_id->login_response,
903 scsi_id->login_response_dma);
904 SBP2_DMA_FREE("login FIFO DMA");
909 list_del(&scsi_id->list);
911 SBP2_ERR ("Could not allocate memory for scsi_id");
915 SBP2_DMA_ALLOC("consistent DMA region for login ORB");
918 * Find an empty spot to stick our scsi id instance data.
920 for (i = 0; i < hi->scsi_host->max_id; i++) {
921 if (!hi->scsi_id[i]) {
922 hi->scsi_id[i] = scsi_id;
924 SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", (unsigned int) i);
930 * Create our command orb pool
932 if (sbp2util_create_command_orb_pool(scsi_id)) {
933 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
934 sbp2_remove_device(scsi_id);
939 * Make sure we are not out of space
941 if (i == hi->scsi_host->max_id) {
942 SBP2_ERR("No slots left for SBP-2 device");
943 sbp2_remove_device(scsi_id);
948 * Login to the sbp-2 device
950 if (sbp2_login_device(scsi_id)) {
951 /* Login failed, just remove the device. */
952 sbp2_remove_device(scsi_id);
957 * Set max retries to something large on the device
959 sbp2_set_busy_timeout(scsi_id);
962 * Do a SBP-2 fetch agent reset
964 sbp2_agent_reset(scsi_id, 1);
967 * Get the max speed and packet size that we can use
969 sbp2_max_speed_and_size(scsi_id);
971 /* Add this device to the scsi layer now */
972 sdev = scsi_add_device(hi->scsi_host, 0, scsi_id->id, 0);
974 SBP2_ERR("scsi_add_device failed");
975 return PTR_ERR(sdev);
982 * This function removes an sbp2 device from the sbp2scsi_host_info struct.
984 static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
986 struct sbp2scsi_host_info *hi = scsi_id->hi;
987 struct scsi_device *sdev = scsi_find_device(hi->scsi_host, 0, scsi_id->id, 0);
989 SBP2_DEBUG("sbp2_remove_device");
991 /* Complete any pending commands with selection timeout */
992 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
994 /* Remove it from the scsi layer now */
995 if (sdev && scsi_remove_device(sdev))
996 SBP2_ERR("scsi_remove_device failed");
998 sbp2util_remove_command_orb_pool(scsi_id);
1000 hi->scsi_id[scsi_id->id] = NULL;
1002 if (scsi_id->login_response) {
1003 pci_free_consistent(hi->host->pdev,
1004 sizeof(struct sbp2_login_response),
1005 scsi_id->login_response,
1006 scsi_id->login_response_dma);
1007 SBP2_DMA_FREE("single login FIFO");
1010 if (scsi_id->login_orb) {
1011 pci_free_consistent(hi->host->pdev,
1012 sizeof(struct sbp2_login_orb),
1014 scsi_id->login_orb_dma);
1015 SBP2_DMA_FREE("single login ORB");
1018 if (scsi_id->reconnect_orb) {
1019 pci_free_consistent(hi->host->pdev,
1020 sizeof(struct sbp2_reconnect_orb),
1021 scsi_id->reconnect_orb,
1022 scsi_id->reconnect_orb_dma);
1023 SBP2_DMA_FREE("single reconnect orb");
1026 if (scsi_id->logout_orb) {
1027 pci_free_consistent(hi->host->pdev,
1028 sizeof(struct sbp2_logout_orb),
1029 scsi_id->logout_orb,
1030 scsi_id->logout_orb_dma);
1031 SBP2_DMA_FREE("single logout orb");
1034 if (scsi_id->query_logins_orb) {
1035 pci_free_consistent(hi->host->pdev,
1036 sizeof(struct sbp2_query_logins_orb),
1037 scsi_id->query_logins_orb,
1038 scsi_id->query_logins_orb_dma);
1039 SBP2_DMA_FREE("single query logins orb");
1042 if (scsi_id->query_logins_response) {
1043 pci_free_consistent(hi->host->pdev,
1044 sizeof(struct sbp2_query_logins_response),
1045 scsi_id->query_logins_response,
1046 scsi_id->query_logins_response_dma);
1047 SBP2_DMA_FREE("single query logins data");
1050 SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->id);
1052 list_del(&scsi_id->list);
1057 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1059 * This function deals with physical dma write requests (for adapters that do not support
1060 * physical dma in hardware). Mostly just here for debugging...
1062 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid, int destid, quadlet_t *data,
1063 u64 addr, unsigned int length, u16 flags)
1067 * Manually put the data in the right place.
1069 memcpy(bus_to_virt((u32)addr), data, length);
1070 sbp2util_packet_dump(data, length, "sbp2 phys dma write by device", (u32)addr);
1071 return(RCODE_COMPLETE);
1075 * This function deals with physical dma read requests (for adapters that do not support
1076 * physical dma in hardware). Mostly just here for debugging...
1078 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid, quadlet_t *data,
1079 u64 addr, unsigned int length, u16 flags)
1083 * Grab data from memory and send a read response.
1085 memcpy(data, bus_to_virt((u32)addr), length);
1086 sbp2util_packet_dump(data, length, "sbp2 phys dma read by device", (u32)addr);
1087 return(RCODE_COMPLETE);
1092 /**************************************
1093 * SBP-2 protocol related section
1094 **************************************/
1097 * This function determines if we should convert scsi commands for a particular sbp2 device type
1099 static __inline__ int sbp2_command_conversion_device_type(u8 device_type)
1101 return (((device_type == TYPE_DISK) ||
1102 (device_type == TYPE_SDAD) ||
1103 (device_type == TYPE_ROM)) ? 1:0);
1107 * This function queries the device for the maximum concurrent logins it
1110 static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1112 struct sbp2scsi_host_info *hi = scsi_id->hi;
1117 SBP2_DEBUG("sbp2_query_logins");
1119 scsi_id->query_logins_orb->reserved1 = 0x0;
1120 scsi_id->query_logins_orb->reserved2 = 0x0;
1122 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1123 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1124 SBP2_DEBUG("sbp2_query_logins: query_response_hi/lo initialized");
1126 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(QUERY_LOGINS_REQUEST);
1127 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1128 if (scsi_id->sbp2_device_type_and_lun != SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
1129 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
1130 SBP2_DEBUG("sbp2_query_logins: set lun to %d",
1131 ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun));
1133 SBP2_DEBUG("sbp2_query_logins: lun_misc initialized");
1135 scsi_id->query_logins_orb->reserved_resp_length =
1136 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
1137 SBP2_DEBUG("sbp2_query_logins: reserved_resp_length initialized");
1139 scsi_id->query_logins_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1140 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1141 scsi_id->query_logins_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1142 SBP2_STATUS_FIFO_ADDRESS_HI);
1143 SBP2_DEBUG("sbp2_query_logins: status FIFO initialized");
1145 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1147 SBP2_DEBUG("sbp2_query_logins: orb byte-swapped");
1149 sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
1150 "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
1152 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1153 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1155 SBP2_DEBUG("sbp2_query_logins: query_logins_response/status FIFO memset");
1157 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1158 data[1] = scsi_id->query_logins_orb_dma;
1159 sbp2util_cpu_to_be32_buffer(data, 8);
1161 atomic_set(&scsi_id->sbp2_login_complete, 0);
1163 SBP2_DEBUG("sbp2_query_logins: prepared to write");
1164 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1165 SBP2_DEBUG("sbp2_query_logins: written");
1167 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 2*HZ)) {
1168 SBP2_ERR("Error querying logins to SBP-2 device - timed out");
1172 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1173 SBP2_ERR("Error querying logins to SBP-2 device - timed out");
1177 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1178 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1179 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1181 SBP2_ERR("Error querying logins to SBP-2 device - timed out");
1185 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1187 SBP2_DEBUG("length_max_logins = %x",
1188 (unsigned int)scsi_id->query_logins_response->length_max_logins);
1190 SBP2_INFO("Query logins to SBP-2 device successful");
1192 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
1193 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1195 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
1196 SBP2_INFO("Number of active logins: %d", active_logins);
1198 if (active_logins >= max_logins) {
1206 * This function is called in order to login to a particular SBP-2 device,
1207 * after a bus reset.
1209 static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1211 struct sbp2scsi_host_info *hi = scsi_id->hi;
1214 SBP2_DEBUG("sbp2_login_device");
1216 if (!scsi_id->login_orb) {
1217 SBP2_DEBUG("sbp2_login_device: login_orb not alloc'd!");
1221 if (!exclusive_login) {
1222 if (sbp2_query_logins(scsi_id)) {
1223 SBP2_ERR("Device does not support any more concurrent logins");
1228 /* Set-up login ORB, assume no password */
1229 scsi_id->login_orb->password_hi = 0;
1230 scsi_id->login_orb->password_lo = 0;
1231 SBP2_DEBUG("sbp2_login_device: password_hi/lo initialized");
1233 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1234 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1235 SBP2_DEBUG("sbp2_login_device: login_response_hi/lo initialized");
1237 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(LOGIN_REQUEST);
1238 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
1239 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
1240 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
1241 /* Set the lun if we were able to pull it from the device's unit directory */
1242 if (scsi_id->sbp2_device_type_and_lun != SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
1243 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
1244 SBP2_DEBUG("sbp2_query_logins: set lun to %d",
1245 ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun));
1247 SBP2_DEBUG("sbp2_login_device: lun_misc initialized");
1249 scsi_id->login_orb->passwd_resp_lengths =
1250 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1251 SBP2_DEBUG("sbp2_login_device: passwd_resp_lengths initialized");
1253 scsi_id->login_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1254 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1255 scsi_id->login_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1256 SBP2_STATUS_FIFO_ADDRESS_HI);
1257 SBP2_DEBUG("sbp2_login_device: status FIFO initialized");
1260 * Byte swap ORB if necessary
1262 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1264 SBP2_DEBUG("sbp2_login_device: orb byte-swapped");
1266 sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
1267 "sbp2 login orb", scsi_id->login_orb_dma);
1270 * Initialize login response and status fifo
1272 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1273 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1275 SBP2_DEBUG("sbp2_login_device: login_response/status FIFO memset");
1278 * Ok, let's write to the target's management agent register
1280 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1281 data[1] = scsi_id->login_orb_dma;
1282 sbp2util_cpu_to_be32_buffer(data, 8);
1284 atomic_set(&scsi_id->sbp2_login_complete, 0);
1286 SBP2_DEBUG("sbp2_login_device: prepared to write to %08x",
1287 (unsigned int)scsi_id->sbp2_management_agent_addr);
1288 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1289 SBP2_DEBUG("sbp2_login_device: written");
1292 * Wait for login status (up to 20 seconds)...
1294 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 20*HZ)) {
1295 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1300 * Sanity. Make sure status returned matches login orb.
1302 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1303 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1310 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1311 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1312 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1314 SBP2_ERR("Error logging into SBP-2 device - login failed");
1319 * Byte swap the login response, for use when reconnecting or
1322 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1325 * Grab our command block agent address from the login response.
1327 SBP2_DEBUG("command_block_agent_hi = %x",
1328 (unsigned int)scsi_id->login_response->command_block_agent_hi);
1329 SBP2_DEBUG("command_block_agent_lo = %x",
1330 (unsigned int)scsi_id->login_response->command_block_agent_lo);
1332 scsi_id->sbp2_command_block_agent_addr =
1333 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1334 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1335 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1337 SBP2_INFO("Logged into SBP-2 device");
1344 * This function is called in order to logout from a particular SBP-2
1345 * device, usually called during driver unload.
1347 static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1349 struct sbp2scsi_host_info *hi = scsi_id->hi;
1352 SBP2_DEBUG("sbp2_logout_device");
1357 scsi_id->logout_orb->reserved1 = 0x0;
1358 scsi_id->logout_orb->reserved2 = 0x0;
1359 scsi_id->logout_orb->reserved3 = 0x0;
1360 scsi_id->logout_orb->reserved4 = 0x0;
1362 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(LOGOUT_REQUEST);
1363 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1365 /* Notify us when complete */
1366 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1368 scsi_id->logout_orb->reserved5 = 0x0;
1369 scsi_id->logout_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1370 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1371 scsi_id->logout_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1372 SBP2_STATUS_FIFO_ADDRESS_HI);
1375 * Byte swap ORB if necessary
1377 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1379 sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
1380 "sbp2 logout orb", scsi_id->logout_orb_dma);
1383 * Ok, let's write to the target's management agent register
1385 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1386 data[1] = scsi_id->logout_orb_dma;
1387 sbp2util_cpu_to_be32_buffer(data, 8);
1389 atomic_set(&scsi_id->sbp2_login_complete, 0);
1391 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1393 /* Wait for device to logout...1 second. */
1394 sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ);
1396 SBP2_INFO("Logged out of SBP-2 device");
1403 * This function is called in order to reconnect to a particular SBP-2
1404 * device, after a bus reset.
1406 static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1408 struct sbp2scsi_host_info *hi = scsi_id->hi;
1411 SBP2_DEBUG("sbp2_reconnect_device");
1414 * Set-up reconnect ORB
1416 scsi_id->reconnect_orb->reserved1 = 0x0;
1417 scsi_id->reconnect_orb->reserved2 = 0x0;
1418 scsi_id->reconnect_orb->reserved3 = 0x0;
1419 scsi_id->reconnect_orb->reserved4 = 0x0;
1421 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(RECONNECT_REQUEST);
1422 scsi_id->reconnect_orb->login_ID_misc |=
1423 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1425 /* Notify us when complete */
1426 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1428 scsi_id->reconnect_orb->reserved5 = 0x0;
1429 scsi_id->reconnect_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1430 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1431 scsi_id->reconnect_orb->status_FIFO_hi =
1432 (ORB_SET_NODE_ID(hi->host->node_id) | SBP2_STATUS_FIFO_ADDRESS_HI);
1435 * Byte swap ORB if necessary
1437 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1439 sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
1440 "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
1443 * Initialize status fifo
1445 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1448 * Ok, let's write to the target's management agent register
1450 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1451 data[1] = scsi_id->reconnect_orb_dma;
1452 sbp2util_cpu_to_be32_buffer(data, 8);
1454 atomic_set(&scsi_id->sbp2_login_complete, 0);
1456 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1459 * Wait for reconnect status (up to 1 second)...
1461 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) {
1462 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1467 * Sanity. Make sure status returned matches reconnect orb.
1469 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1470 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1477 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1478 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1479 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1481 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect failed");
1485 SBP2_INFO("Reconnected to SBP-2 device");
1492 * This function is called in order to set the busy timeout (number of
1493 * retries to attempt) on the sbp2 device.
1495 static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1499 SBP2_DEBUG("sbp2_set_busy_timeout");
1502 * Ok, let's write to the target's busy timeout register
1504 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1506 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4)) {
1507 SBP2_ERR("sbp2_set_busy_timeout error");
1514 * This function is called to parse sbp2 device's config rom unit
1515 * directory. Used to determine things like sbp2 management agent offset,
1516 * and command set used (SCSI or RBC).
1518 static void sbp2_parse_unit_directory(struct scsi_id_group *scsi_group,
1519 struct unit_directory *ud)
1521 struct scsi_id_instance_data *scsi_id;
1522 struct list_head *lh;
1523 u64 management_agent_addr;
1524 u32 command_set_spec_id, command_set, unit_characteristics,
1525 firmware_revision, workarounds;
1528 SBP2_DEBUG("sbp2_parse_unit_directory");
1530 management_agent_addr = 0x0;
1531 command_set_spec_id = 0x0;
1533 unit_characteristics = 0x0;
1534 firmware_revision = 0x0;
1536 /* Handle different fields in the unit directory, based on keys */
1537 for (i = 0; i < ud->length; i++) {
1538 switch (CONFIG_ROM_KEY(ud->quadlets[i])) {
1539 case SBP2_CSR_OFFSET_KEY:
1540 /* Save off the management agent address */
1541 management_agent_addr =
1543 (CONFIG_ROM_VALUE(ud->quadlets[i]) << 2);
1545 SBP2_DEBUG("sbp2_management_agent_addr = %x",
1546 (unsigned int) management_agent_addr);
1549 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1550 /* Command spec organization */
1552 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1553 SBP2_DEBUG("sbp2_command_set_spec_id = %x",
1554 (unsigned int) command_set_spec_id);
1557 case SBP2_COMMAND_SET_KEY:
1558 /* Command set used by sbp2 device */
1559 command_set = CONFIG_ROM_VALUE(ud->quadlets[i]);
1560 SBP2_DEBUG("sbp2_command_set = %x",
1561 (unsigned int) command_set);
1564 case SBP2_UNIT_CHARACTERISTICS_KEY:
1566 * Unit characterisitcs (orb related stuff
1567 * that I'm not yet paying attention to)
1569 unit_characteristics
1570 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1571 SBP2_DEBUG("sbp2_unit_characteristics = %x",
1572 (unsigned int) unit_characteristics);
1575 case SBP2_DEVICE_TYPE_AND_LUN_KEY:
1577 * Device type and lun (used for
1578 * detemining type of sbp2 device)
1580 scsi_id = kmalloc(sizeof(*scsi_id), GFP_KERNEL);
1582 SBP2_ERR("Out of memory adding scsi_id, not all LUN's will be added");
1585 memset(scsi_id, 0, sizeof(*scsi_id));
1587 scsi_id->sbp2_device_type_and_lun
1588 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1589 SBP2_DEBUG("sbp2_device_type_and_lun = %x",
1590 (unsigned int) scsi_id->sbp2_device_type_and_lun);
1591 list_add_tail(&scsi_id->list, &scsi_group->scsi_id_list);
1594 case SBP2_FIRMWARE_REVISION_KEY:
1595 /* Firmware revision */
1597 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1598 if (force_inquiry_hack)
1599 SBP2_INFO("sbp2_firmware_revision = %x",
1600 (unsigned int) firmware_revision);
1601 else SBP2_DEBUG("sbp2_firmware_revision = %x",
1602 (unsigned int) firmware_revision);
1610 /* This is the start of our broken device checking. We try to hack
1611 * around oddities and known defects. */
1614 /* If the vendor id is 0xa0b8 (Symbios vendor id), then we have a
1615 * bridge with 128KB max transfer size limitation. For sanity, we
1616 * only voice this when the current max_sectors setting
1617 * exceeds the 128k limit. By default, that is not the case.
1619 * It would be really nice if we could detect this before the scsi
1620 * host gets initialized. That way we can down-force the
1621 * max_sectors to account for it. That is not currently
1623 if ((firmware_revision & 0xffff00) ==
1624 SBP2_128KB_BROKEN_FIRMWARE &&
1625 (max_sectors * 512) > (128*1024)) {
1626 SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB max transfer size.",
1627 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid));
1628 SBP2_WARN("WARNING: Current max_sectors setting is larger than 128KB (%d sectors)!",
1630 workarounds |= SBP2_BREAKAGE_128K_MAX_TRANSFER;
1633 /* Check for a blacklisted set of devices that require us to force
1634 * a 36 byte host inquiry. This can be overriden as a module param
1635 * (to force all hosts). */
1636 for (i = 0; i < NUM_BROKEN_INQUIRY_DEVS; i++) {
1637 if ((firmware_revision & 0xffff00) ==
1638 sbp2_broken_inquiry_list[i]) {
1639 SBP2_WARN("Node " NODE_BUS_FMT ": Using 36byte inquiry workaround",
1640 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid));
1641 workarounds |= SBP2_BREAKAGE_INQUIRY_HACK;
1642 break; /* No need to continue. */
1646 /* If this is a logical unit directory entry, process the parent
1647 * to get the common values. */
1648 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1649 struct unit_directory *parent_ud =
1650 container_of(ud->device.parent, struct unit_directory, device);
1651 sbp2_parse_unit_directory(scsi_group, parent_ud);
1653 /* If our list is empty, add a base scsi_id (happens in a normal
1654 * case where there is no logical_unit_number entry */
1655 if (list_empty(&scsi_group->scsi_id_list)) {
1656 scsi_id = kmalloc(sizeof(*scsi_id), GFP_KERNEL);
1658 SBP2_ERR("Out of memory adding scsi_id");
1661 memset(scsi_id, 0, sizeof(*scsi_id));
1663 scsi_id->sbp2_device_type_and_lun = SBP2_DEVICE_TYPE_LUN_UNINITIALIZED;
1664 list_add_tail(&scsi_id->list, &scsi_group->scsi_id_list);
1667 /* Update the generic fields in all the LUN's */
1668 list_for_each (lh, &scsi_group->scsi_id_list) {
1669 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
1671 scsi_id->sbp2_management_agent_addr = management_agent_addr;
1672 scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
1673 scsi_id->sbp2_command_set = command_set;
1674 scsi_id->sbp2_unit_characteristics = unit_characteristics;
1675 scsi_id->sbp2_firmware_revision = firmware_revision;
1676 scsi_id->workarounds = workarounds;
1682 * This function is called in order to determine the max speed and packet
1683 * size we can use in our ORBs. Note, that we (the driver and host) only
1684 * initiate the transaction. The SBP-2 device actually transfers the data
1685 * (by reading from the DMA area we tell it). This means that the SBP-2
1686 * device decides the actual maximum data it can transfer. We just tell it
1687 * the speed that it needs to use, and the max_rec the host supports, and
1688 * it takes care of the rest.
1690 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1692 struct sbp2scsi_host_info *hi = scsi_id->hi;
1694 SBP2_DEBUG("sbp2_max_speed_and_size");
1696 /* Initial setting comes from the hosts speed map */
1697 scsi_id->speed_code = hi->host->speed_map[NODEID_TO_NODE(hi->host->node_id) * 64
1698 + NODEID_TO_NODE(scsi_id->ne->nodeid)];
1700 /* Bump down our speed if the user requested it */
1701 if (scsi_id->speed_code > max_speed) {
1702 scsi_id->speed_code = max_speed;
1703 SBP2_ERR("Forcing SBP-2 max speed down to %s",
1704 hpsb_speedto_str[scsi_id->speed_code]);
1707 /* Payload size is the lesser of what our speed supports and what
1708 * our host supports. */
1709 scsi_id->max_payload_size = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1710 (u8)(((be32_to_cpu(hi->host->csr.rom[2]) >> 12) & 0xf) - 1));
1712 SBP2_ERR("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1713 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1714 hpsb_speedto_str[scsi_id->speed_code],
1715 1 << ((u32)scsi_id->max_payload_size + 2));
1721 * This function is called in order to perform a SBP-2 agent reset.
1723 static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1725 struct sbp2scsi_host_info *hi = scsi_id->hi;
1726 struct hpsb_packet *packet;
1729 SBP2_DEBUG("sbp2_agent_reset");
1732 * Ok, let's write to the target's management agent register
1734 data = ntohl(SBP2_AGENT_RESET_DATA);
1735 packet = sbp2util_allocate_write_packet(hi, scsi_id->ne,
1736 scsi_id->sbp2_command_block_agent_addr +
1737 SBP2_AGENT_RESET_OFFSET,
1741 SBP2_ERR("sbp2util_allocate_write_packet failed");
1745 if (!hpsb_send_packet(packet)) {
1746 SBP2_ERR("hpsb_send_packet failed");
1747 sbp2_free_packet(packet);
1752 down(&packet->state_change);
1753 down(&packet->state_change);
1757 * Need to make sure orb pointer is written on next command
1759 scsi_id->last_orb = NULL;
1765 * This function is called to create the actual command orb and s/g list
1766 * out of the scsi command itself.
1768 static int sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1769 struct sbp2_command_info *command,
1771 unsigned int scsi_use_sg,
1772 unsigned int scsi_request_bufflen,
1773 void *scsi_request_buffer,
1774 unsigned char scsi_dir)
1776 struct sbp2scsi_host_info *hi = scsi_id->hi;
1777 struct scatterlist *sgpnt = (struct scatterlist *) scsi_request_buffer;
1778 struct sbp2_command_orb *command_orb = &command->command_orb;
1779 struct sbp2_unrestricted_page_table *scatter_gather_element =
1780 &command->scatter_gather_element[0];
1781 int dma_dir = scsi_to_pci_dma_dir (scsi_dir);
1782 u32 sg_count, sg_len, orb_direction;
1787 * Set-up our command ORB..
1789 * NOTE: We're doing unrestricted page tables (s/g), as this is
1790 * best performance (at least with the devices I have). This means
1791 * that data_size becomes the number of s/g elements, and
1792 * page_size should be zero (for unrestricted).
1794 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1795 command_orb->next_ORB_lo = 0x0;
1796 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1797 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
1798 command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
1801 * Get the direction of the transfer. If the direction is unknown, then use our
1802 * goofy table as a back-up.
1805 case SCSI_DATA_NONE:
1806 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1808 case SCSI_DATA_WRITE:
1809 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1811 case SCSI_DATA_READ:
1812 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1814 case SCSI_DATA_UNKNOWN:
1816 SBP2_ERR("SCSI data transfer direction not specified. "
1817 "Update the SBP2 direction table in sbp2.h if "
1818 "necessary for your application");
1819 print_command (scsi_cmd);
1820 orb_direction = sbp2scsi_direction_table[*scsi_cmd];
1825 * Set-up our pagetable stuff... unfortunately, this has become
1826 * messier than I'd like. Need to clean this up a bit. ;-)
1828 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1830 SBP2_DEBUG("No data transfer");
1833 * Handle no data transfer
1835 command_orb->data_descriptor_hi = 0x0;
1836 command_orb->data_descriptor_lo = 0x0;
1837 command_orb->misc |= ORB_SET_DIRECTION(1);
1839 } else if (scsi_use_sg) {
1841 SBP2_DEBUG("Use scatter/gather");
1844 * Special case if only one element (and less than 64KB in size)
1846 if ((scsi_use_sg == 1) && (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1848 SBP2_DEBUG("Only one s/g element");
1849 command->dma_dir = dma_dir;
1850 command->dma_size = sgpnt[0].length;
1851 command->dma_type = CMD_DMA_PAGE;
1852 command->cmd_dma = pci_map_page(hi->host->pdev,
1857 SBP2_DMA_ALLOC("single page scatter element");
1859 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1860 command_orb->data_descriptor_lo = command->cmd_dma;
1861 command_orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1862 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1865 int count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg, dma_dir);
1866 SBP2_DMA_ALLOC("scatter list");
1868 command->dma_size = scsi_use_sg;
1869 command->dma_dir = dma_dir;
1870 command->sge_buffer = sgpnt;
1872 /* use page tables (s/g) */
1873 command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1874 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1875 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1876 command_orb->data_descriptor_lo = command->sge_dma;
1879 * Loop through and fill out our sbp-2 page tables
1880 * (and split up anything too large)
1882 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1883 sg_len = sg_dma_len(sgpnt);
1884 sg_addr = sg_dma_address(sgpnt);
1886 scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1887 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1888 scatter_gather_element[sg_count].length_segment_base_hi =
1889 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1890 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1891 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1893 scatter_gather_element[sg_count].length_segment_base_hi =
1894 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1901 /* Number of page table (s/g) elements */
1902 command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1904 sbp2util_packet_dump(scatter_gather_element,
1905 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1906 "sbp2 s/g list", command->sge_dma);
1909 * Byte swap page tables if necessary
1911 sbp2util_cpu_to_be32_buffer(scatter_gather_element,
1912 (sizeof(struct sbp2_unrestricted_page_table)) *
1919 SBP2_DEBUG("No scatter/gather");
1921 command->dma_dir = dma_dir;
1922 command->dma_size = scsi_request_bufflen;
1923 command->dma_type = CMD_DMA_SINGLE;
1924 command->cmd_dma = pci_map_single (hi->host->pdev, scsi_request_buffer,
1927 SBP2_DMA_ALLOC("single bulk");
1930 * Handle case where we get a command w/o s/g enabled (but
1931 * check for transfers larger than 64K)
1933 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1935 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1936 command_orb->data_descriptor_lo = command->cmd_dma;
1937 command_orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1938 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1941 * Sanity, in case our direction table is not
1944 if (!scsi_request_bufflen) {
1945 command_orb->data_descriptor_hi = 0x0;
1946 command_orb->data_descriptor_lo = 0x0;
1947 command_orb->misc |= ORB_SET_DIRECTION(1);
1952 * Need to turn this into page tables, since the
1953 * buffer is too large.
1955 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1956 command_orb->data_descriptor_lo = command->sge_dma;
1958 /* Use page tables (s/g) */
1959 command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1960 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1963 * fill out our sbp-2 page tables (and split up
1967 sg_len = scsi_request_bufflen;
1968 sg_addr = command->cmd_dma;
1970 scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1971 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1972 scatter_gather_element[sg_count].length_segment_base_hi =
1973 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1974 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1975 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1977 scatter_gather_element[sg_count].length_segment_base_hi =
1978 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1984 /* Number of page table (s/g) elements */
1985 command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1987 sbp2util_packet_dump(scatter_gather_element,
1988 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1989 "sbp2 s/g list", command->sge_dma);
1992 * Byte swap page tables if necessary
1994 sbp2util_cpu_to_be32_buffer(scatter_gather_element,
1995 (sizeof(struct sbp2_unrestricted_page_table)) *
2003 * Byte swap command ORB if necessary
2005 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
2008 * Put our scsi command in the command ORB
2010 memset(command_orb->cdb, 0, 12);
2011 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
2017 * This function is called in order to begin a regular SBP-2 command.
2019 static int sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
2020 struct sbp2_command_info *command)
2022 struct sbp2scsi_host_info *hi = scsi_id->hi;
2023 struct hpsb_packet *packet;
2024 struct sbp2_command_orb *command_orb = &command->command_orb;
2026 outstanding_orb_incr;
2027 SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
2028 command_orb, global_outstanding_command_orbs);
2030 pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2031 sizeof(struct sbp2_command_orb),
2032 PCI_DMA_BIDIRECTIONAL);
2033 pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2034 sizeof(command->scatter_gather_element),
2035 PCI_DMA_BIDIRECTIONAL);
2037 * Check to see if there are any previous orbs to use
2039 if (scsi_id->last_orb == NULL) {
2042 * Ok, let's write to the target's management agent register
2044 if (hpsb_node_entry_valid(scsi_id->ne)) {
2046 packet = sbp2util_allocate_write_packet(hi, scsi_id->ne,
2047 scsi_id->sbp2_command_block_agent_addr +
2048 SBP2_ORB_POINTER_OFFSET, 8, NULL);
2051 SBP2_ERR("sbp2util_allocate_write_packet failed");
2055 packet->data[0] = ORB_SET_NODE_ID(hi->host->node_id);
2056 packet->data[1] = command->command_orb_dma;
2057 sbp2util_cpu_to_be32_buffer(packet->data, 8);
2059 SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb);
2061 if (!hpsb_send_packet(packet)) {
2062 SBP2_ERR("hpsb_send_packet failed");
2063 sbp2_free_packet(packet);
2067 SBP2_ORB_DEBUG("write command agent complete");
2070 scsi_id->last_orb = command_orb;
2071 scsi_id->last_orb_dma = command->command_orb_dma;
2076 * We have an orb already sent (maybe or maybe not
2077 * processed) that we can append this orb to. So do so,
2078 * and ring the doorbell. Have to be very careful
2079 * modifying these next orb pointers, as they are accessed
2080 * both by the sbp2 device and us.
2082 scsi_id->last_orb->next_ORB_lo =
2083 cpu_to_be32(command->command_orb_dma);
2084 /* Tells hardware that this pointer is valid */
2085 scsi_id->last_orb->next_ORB_hi = 0x0;
2086 pci_dma_sync_single(hi->host->pdev, scsi_id->last_orb_dma,
2087 sizeof(struct sbp2_command_orb),
2088 PCI_DMA_BIDIRECTIONAL);
2093 if (hpsb_node_entry_valid(scsi_id->ne)) {
2094 quadlet_t data = cpu_to_be32(command->command_orb_dma);
2096 packet = sbp2util_allocate_write_packet(hi, scsi_id->ne,
2097 scsi_id->sbp2_command_block_agent_addr +
2098 SBP2_DOORBELL_OFFSET, 4, &data);
2101 SBP2_ERR("sbp2util_allocate_write_packet failed");
2105 SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb);
2107 if (!hpsb_send_packet(packet)) {
2108 SBP2_ERR("hpsb_send_packet failed");
2109 sbp2_free_packet(packet);
2114 scsi_id->last_orb = command_orb;
2115 scsi_id->last_orb_dma = command->command_orb_dma;
2122 * This function is called in order to begin a regular SBP-2 command.
2124 static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
2125 Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2127 unchar *cmd = (unchar *) SCpnt->cmnd;
2128 unsigned int request_bufflen = SCpnt->request_bufflen;
2129 struct sbp2_command_info *command;
2131 SBP2_DEBUG("sbp2_send_command");
2132 #if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
2133 printk("[scsi command]\n ");
2134 print_command (cmd);
2136 SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
2137 SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2140 * Allocate a command orb and s/g structure
2142 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
2148 * The scsi stack sends down a request_bufflen which does not match the
2149 * length field in the scsi cdb. This causes some sbp2 devices to
2150 * reject this inquiry command. Fix the request_bufflen.
2152 if (*cmd == INQUIRY) {
2153 if (force_inquiry_hack || scsi_id->workarounds & SBP2_BREAKAGE_INQUIRY_HACK)
2154 request_bufflen = cmd[4] = 0x24;
2156 request_bufflen = cmd[4];
2160 * Now actually fill in the comamnd orb and sbp2 s/g list
2162 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
2163 request_bufflen, SCpnt->request_buffer,
2164 SCpnt->sc_data_direction);
2166 * Update our cdb if necessary (to handle sbp2 RBC command set
2167 * differences). This is where the command set hacks go! =)
2169 sbp2_check_sbp2_command(scsi_id, command->command_orb.cdb);
2171 sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
2172 "sbp2 command orb", command->command_orb_dma);
2175 * Initialize status fifo
2177 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2180 * Link up the orb, and ring the doorbell if needed
2182 sbp2_link_orb_command(scsi_id, command);
2189 * This function deals with command set differences between Linux scsi
2190 * command set and sbp2 RBC command set.
2192 static void sbp2_check_sbp2_command(struct scsi_id_instance_data *scsi_id, unchar *cmd)
2195 u8 device_type = SBP2_DEVICE_TYPE (scsi_id->sbp2_device_type_and_lun);
2197 SBP2_DEBUG("sbp2_check_sbp2_command");
2203 if (sbp2_command_conversion_device_type(device_type)) {
2205 SBP2_DEBUG("Convert READ_6 to READ_10");
2208 * Need to turn read_6 into read_10
2211 new_cmd[1] = (cmd[1] & 0xe0);
2213 new_cmd[3] = (cmd[1] & 0x1f);
2214 new_cmd[4] = cmd[2];
2215 new_cmd[5] = cmd[3];
2218 new_cmd[8] = cmd[4];
2219 new_cmd[9] = cmd[5];
2221 memcpy(cmd, new_cmd, 10);
2229 if (sbp2_command_conversion_device_type(device_type)) {
2231 SBP2_DEBUG("Convert WRITE_6 to WRITE_10");
2234 * Need to turn write_6 into write_10
2237 new_cmd[1] = (cmd[1] & 0xe0);
2239 new_cmd[3] = (cmd[1] & 0x1f);
2240 new_cmd[4] = cmd[2];
2241 new_cmd[5] = cmd[3];
2244 new_cmd[8] = cmd[4];
2245 new_cmd[9] = cmd[5];
2247 memcpy(cmd, new_cmd, 10);
2255 if (sbp2_command_conversion_device_type(device_type)) {
2257 SBP2_DEBUG("Convert MODE_SENSE_6 to MODE_SENSE_10");
2260 * Need to turn mode_sense_6 into mode_sense_10
2263 new_cmd[1] = cmd[1];
2264 new_cmd[2] = cmd[2];
2270 new_cmd[8] = cmd[4];
2271 new_cmd[9] = cmd[5];
2273 memcpy(cmd, new_cmd, 10);
2282 * TODO. Probably need to change mode select to 10 byte version
2293 * Translates SBP-2 status into SCSI sense data for check conditions
2295 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
2297 SBP2_DEBUG("sbp2_status_to_sense_data");
2300 * Ok, it's pretty ugly... ;-)
2302 sense_data[0] = 0x70;
2303 sense_data[1] = 0x0;
2304 sense_data[2] = sbp2_status[9];
2305 sense_data[3] = sbp2_status[12];
2306 sense_data[4] = sbp2_status[13];
2307 sense_data[5] = sbp2_status[14];
2308 sense_data[6] = sbp2_status[15];
2310 sense_data[8] = sbp2_status[16];
2311 sense_data[9] = sbp2_status[17];
2312 sense_data[10] = sbp2_status[18];
2313 sense_data[11] = sbp2_status[19];
2314 sense_data[12] = sbp2_status[10];
2315 sense_data[13] = sbp2_status[11];
2316 sense_data[14] = sbp2_status[20];
2317 sense_data[15] = sbp2_status[21];
2319 return(sbp2_status[8] & 0x3f); /* return scsi status */
2323 * This function is called after a command is completed, in order to do any necessary SBP-2
2324 * response data translations for the SCSI stack
2326 static void sbp2_check_sbp2_response(struct scsi_id_instance_data *scsi_id,
2329 u8 *scsi_buf = SCpnt->request_buffer;
2330 u8 device_type = SBP2_DEVICE_TYPE (scsi_id->sbp2_device_type_and_lun);
2332 SBP2_DEBUG("sbp2_check_sbp2_response");
2334 switch (SCpnt->cmnd[0]) {
2339 * If scsi_id->sbp2_device_type_and_lun is uninitialized, then fill
2340 * this information in from the inquiry response data. Lun is set to zero.
2342 if (scsi_id->sbp2_device_type_and_lun == SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
2343 SBP2_DEBUG("Creating sbp2_device_type_and_lun from scsi inquiry data");
2344 scsi_id->sbp2_device_type_and_lun = (scsi_buf[0] & 0x1f) << 16;
2348 * Make sure data length is ok. Minimum length is 36 bytes
2350 if (scsi_buf[4] == 0) {
2351 scsi_buf[4] = 36 - 5;
2355 * Check for Simple Direct Access Device and change it to TYPE_DISK
2357 if ((scsi_buf[0] & 0x1f) == TYPE_SDAD) {
2358 SBP2_DEBUG("Changing TYPE_SDAD to TYPE_DISK");
2359 scsi_buf[0] &= 0xe0;
2363 * Fix ansi revision and response data format
2366 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
2372 if (sbp2_command_conversion_device_type(device_type)) {
2374 SBP2_DEBUG("Modify mode sense response (10 byte version)");
2376 scsi_buf[0] = scsi_buf[1]; /* Mode data length */
2377 scsi_buf[1] = scsi_buf[2]; /* Medium type */
2378 scsi_buf[2] = scsi_buf[3]; /* Device specific parameter */
2379 scsi_buf[3] = scsi_buf[7]; /* Block descriptor length */
2380 memcpy(scsi_buf + 4, scsi_buf + 8, scsi_buf[0]);
2389 * TODO. Probably need to change mode select to 10 byte version
2399 * This function deals with status writes from the SBP-2 device
2401 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid,
2402 quadlet_t *data, u64 addr, unsigned int length, u16 fl)
2404 struct sbp2scsi_host_info *hi = NULL;
2405 struct scsi_id_instance_data *scsi_id = NULL;
2407 unsigned long flags;
2408 Scsi_Cmnd *SCpnt = NULL;
2409 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2410 struct sbp2_command_info *command;
2412 SBP2_DEBUG("sbp2_handle_status_write");
2414 sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
2417 SBP2_ERR("host is NULL - this is bad!");
2418 return(RCODE_ADDRESS_ERROR);
2421 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
2424 SBP2_ERR("host info is NULL - this is bad!");
2425 return(RCODE_ADDRESS_ERROR);
2428 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
2431 * Find our scsi_id structure by looking at the status fifo address written to by
2434 id = SBP2_STATUS_FIFO_OFFSET_TO_ENTRY((u32)(addr - SBP2_STATUS_FIFO_ADDRESS));
2435 scsi_id = hi->scsi_id[id];
2438 SBP2_ERR("scsi_id is NULL - device is gone?");
2439 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2440 return(RCODE_ADDRESS_ERROR);
2444 * Put response into scsi_id status fifo...
2446 memcpy(&scsi_id->status_block, data, length);
2449 * Byte swap first two quadlets (8 bytes) of status for processing
2451 sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8);
2454 * Handle command ORB status here if necessary. First, need to match status with command.
2456 command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo);
2459 SBP2_DEBUG("Found status for command ORB");
2460 pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2461 sizeof(struct sbp2_command_orb),
2462 PCI_DMA_BIDIRECTIONAL);
2463 pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2464 sizeof(command->scatter_gather_element),
2465 PCI_DMA_BIDIRECTIONAL);
2467 SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
2468 outstanding_orb_decr;
2471 * Matched status with command, now grab scsi command pointers and check status
2473 SCpnt = command->Current_SCpnt;
2474 sbp2util_mark_command_completed(scsi_id, command);
2479 * See if the target stored any scsi status information
2481 if (STATUS_GET_LENGTH(scsi_id->status_block.ORB_offset_hi_misc) > 1) {
2483 * Translate SBP-2 status to SCSI sense data
2485 SBP2_DEBUG("CHECK CONDITION");
2486 scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer);
2490 * Check to see if the dead bit is set. If so, we'll have to initiate
2491 * a fetch agent reset.
2493 if (STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc)) {
2496 * Initiate a fetch agent reset.
2498 SBP2_DEBUG("Dead bit set - initiating fetch agent reset");
2499 sbp2_agent_reset(scsi_id, 0);
2502 SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
2506 * Check here to see if there are no commands in-use. If there are none, we can
2507 * null out last orb so that next time around we write directly to the orb pointer...
2508 * Quick start saves one 1394 bus transaction.
2510 if (list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2511 scsi_id->last_orb = NULL;
2517 * It's probably a login/logout/reconnect status.
2519 if ((scsi_id->login_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2520 (scsi_id->query_logins_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2521 (scsi_id->reconnect_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2522 (scsi_id->logout_orb_dma == scsi_id->status_block.ORB_offset_lo)) {
2523 atomic_set(&scsi_id->sbp2_login_complete, 1);
2527 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2533 * Complete the SCSI command.
2535 * Only do it after we've released the sbp2_command_lock,
2536 * as it might otherwise deadlock with the
2537 * io_request_lock (in sbp2scsi_queuecommand).
2539 SBP2_DEBUG("Completing SCSI command");
2540 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
2541 command->Current_done);
2542 SBP2_ORB_DEBUG("command orb completed");
2545 return(RCODE_COMPLETE);
2549 /**************************************
2550 * SCSI interface related section
2551 **************************************/
2554 * This routine is the main request entry routine for doing I/O. It is
2555 * called from the scsi stack directly.
2557 static int sbp2scsi_queuecommand (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2559 struct sbp2scsi_host_info *hi = NULL;
2560 struct scsi_id_instance_data *scsi_id = NULL;
2561 unsigned long flags;
2563 SBP2_DEBUG("sbp2scsi_queuecommand");
2566 * Pull our host info and scsi id instance data from the scsi command
2568 hi = hpsb_get_hostinfo_bykey(&sbp2_highlevel, (unsigned long)SCpnt->device->host);
2571 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
2572 SCpnt->result = DID_NO_CONNECT << 16;
2577 scsi_id = hi->scsi_id[SCpnt->device->id];
2580 * If scsi_id is null, it means there is no device in this slot,
2581 * so we should return selection timeout.
2584 SCpnt->result = DID_NO_CONNECT << 16;
2590 * Until we handle multiple luns, just return selection time-out
2591 * to any IO directed at non-zero LUNs
2593 if (SCpnt->device->lun) {
2594 SCpnt->result = DID_NO_CONNECT << 16;
2600 * Check for request sense command, and handle it here
2601 * (autorequest sense)
2603 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
2604 SBP2_DEBUG("REQUEST_SENSE");
2605 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
2606 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2607 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
2612 * Check to see if we are in the middle of a bus reset.
2614 if (!hpsb_node_entry_valid(scsi_id->ne)) {
2615 SBP2_ERR("Bus reset in progress - rejecting command");
2616 SCpnt->result = DID_BUS_BUSY << 16;
2622 * Try and send our SCSI command
2624 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
2625 if (sbp2_send_command(scsi_id, SCpnt, done)) {
2626 SBP2_ERR("Error sending SCSI command");
2627 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
2630 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2636 * This function is called in order to complete all outstanding SBP-2
2637 * commands (in case of resets, etc.).
2639 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
2642 struct sbp2scsi_host_info *hi = scsi_id->hi;
2643 struct list_head *lh;
2644 struct sbp2_command_info *command;
2646 SBP2_DEBUG("sbp2_complete_all_commands");
2648 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2649 SBP2_DEBUG("Found pending command to complete");
2650 lh = scsi_id->sbp2_command_orb_inuse.next;
2651 command = list_entry(lh, struct sbp2_command_info, list);
2652 pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2653 sizeof(struct sbp2_command_orb),
2654 PCI_DMA_BIDIRECTIONAL);
2655 pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2656 sizeof(command->scatter_gather_element),
2657 PCI_DMA_BIDIRECTIONAL);
2658 sbp2util_mark_command_completed(scsi_id, command);
2659 if (command->Current_SCpnt) {
2660 void (*done)(Scsi_Cmnd *) = command->Current_done;
2661 command->Current_SCpnt->result = status << 16;
2662 done (command->Current_SCpnt);
2670 * This function is called in order to complete a regular SBP-2 command.
2672 * This can be called in interrupt context.
2674 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2675 u32 scsi_status, Scsi_Cmnd *SCpnt,
2676 void (*done)(Scsi_Cmnd *))
2678 unsigned long flags;
2680 SBP2_DEBUG("sbp2scsi_complete_command");
2686 SBP2_ERR("SCpnt is NULL");
2691 * If a bus reset is in progress and there was an error, don't
2692 * complete the command, just let it get retried at the end of the
2695 if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2696 SBP2_ERR("Bus reset in progress - retry command later");
2701 * Switch on scsi status
2703 switch (scsi_status) {
2704 case SBP2_SCSI_STATUS_GOOD:
2705 SCpnt->result = DID_OK;
2708 case SBP2_SCSI_STATUS_BUSY:
2709 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2710 SCpnt->result = DID_BUS_BUSY << 16;
2713 case SBP2_SCSI_STATUS_CHECK_CONDITION:
2714 SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
2715 SCpnt->result = CHECK_CONDITION << 1;
2720 #if CONFIG_IEEE1394_SBP2_DEBUG >= 1
2721 print_command (SCpnt->cmnd);
2722 print_sense("bh", SCpnt);
2727 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2728 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2729 SCpnt->result = DID_NO_CONNECT << 16;
2730 print_command (SCpnt->cmnd);
2733 case SBP2_SCSI_STATUS_CONDITION_MET:
2734 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2735 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2736 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2737 SCpnt->result = DID_ERROR << 16;
2738 print_command (SCpnt->cmnd);
2742 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2743 SCpnt->result = DID_ERROR << 16;
2747 * Take care of any sbp2 response data mucking here (RBC stuff, etc.)
2749 if (SCpnt->result == DID_OK) {
2750 sbp2_check_sbp2_response(scsi_id, SCpnt);
2754 * If a bus reset is in progress and there was an error, complete
2755 * the command as busy so that it will get retried.
2757 if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2758 SBP2_ERR("Completing command with busy (bus reset)");
2759 SCpnt->result = DID_BUS_BUSY << 16;
2763 * If a unit attention occurs, return busy status so it gets
2764 * retried... it could have happened because of a 1394 bus reset
2768 if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
2769 (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
2770 SBP2_DEBUG("UNIT ATTENTION - return busy");
2771 SCpnt->result = DID_BUS_BUSY << 16;
2776 * Tell scsi stack that we're done with this command
2778 spin_lock_irqsave(scsi_id->hi->scsi_host->host_lock,flags);
2780 spin_unlock_irqrestore(scsi_id->hi->scsi_host->host_lock,flags);
2786 * Called by scsi stack when something has really gone wrong. Usually
2787 * called when a command has timed-out for some reason.
2789 static int sbp2scsi_abort (Scsi_Cmnd *SCpnt)
2791 struct sbp2scsi_host_info *hi = hpsb_get_hostinfo_bykey(&sbp2_highlevel,
2792 (unsigned long)SCpnt->device->host);
2793 struct scsi_id_instance_data *scsi_id = hi->scsi_id[SCpnt->device->id];
2794 struct sbp2_command_info *command;
2795 unsigned long flags;
2797 SBP2_ERR("aborting sbp2 command");
2798 print_command (SCpnt->cmnd);
2803 * Right now, just return any matching command structures
2806 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
2807 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2809 SBP2_DEBUG("Found command to abort");
2810 pci_dma_sync_single(hi->host->pdev,
2811 command->command_orb_dma,
2812 sizeof(struct sbp2_command_orb),
2813 PCI_DMA_BIDIRECTIONAL);
2814 pci_dma_sync_single(hi->host->pdev,
2816 sizeof(command->scatter_gather_element),
2817 PCI_DMA_BIDIRECTIONAL);
2818 sbp2util_mark_command_completed(scsi_id, command);
2819 if (command->Current_SCpnt) {
2820 void (*done)(Scsi_Cmnd *) = command->Current_done;
2821 command->Current_SCpnt->result = DID_ABORT << 16;
2822 done (command->Current_SCpnt);
2827 * Initiate a fetch agent reset.
2829 sbp2_agent_reset(scsi_id, 0);
2830 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2831 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2838 * Called by scsi stack when something has really gone wrong.
2840 static int sbp2scsi_reset (Scsi_Cmnd *SCpnt)
2842 struct sbp2scsi_host_info *hi = hpsb_get_hostinfo_bykey(&sbp2_highlevel,
2843 (unsigned long)SCpnt->device->host);
2844 struct scsi_id_instance_data *scsi_id = hi->scsi_id[SCpnt->device->id];
2846 SBP2_ERR("reset requested");
2849 SBP2_ERR("Generating sbp2 fetch agent reset");
2850 sbp2_agent_reset(scsi_id, 0);
2856 static const char *sbp2scsi_info (struct Scsi_Host *host)
2858 return "SCSI emulation for IEEE-1394 SBP-2 Devices";
2861 MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2862 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2863 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2864 MODULE_LICENSE("GPL");
2866 /* SCSI host template */
2867 static Scsi_Host_Template scsi_driver_template = {
2868 .module = THIS_MODULE,
2869 .name = "SBP-2 IEEE-1394",
2870 .proc_name = SBP2_DEVICE_NAME,
2871 .info = sbp2scsi_info,
2872 .queuecommand = sbp2scsi_queuecommand,
2873 .eh_abort_handler = sbp2scsi_abort,
2874 .eh_device_reset_handler = sbp2scsi_reset,
2875 .eh_bus_reset_handler = sbp2scsi_reset,
2876 .eh_host_reset_handler = sbp2scsi_reset,
2878 .sg_tablesize = SG_ALL,
2879 .use_clustering = ENABLE_CLUSTERING,
2880 .cmd_per_lun = SBP2_MAX_CMDS_PER_LUN,
2881 .can_queue = SBP2_MAX_SCSI_QUEUE,
2886 static int sbp2_module_init(void)
2888 SBP2_DEBUG("sbp2_module_init");
2890 printk(KERN_INFO "sbp2: %s\n", version);
2892 /* Module load debug option to force one command at a time (serializing I/O) */
2894 SBP2_ERR("Driver forced to serialize I/O (serialize_io = 1)");
2895 scsi_driver_template.can_queue = 1;
2896 scsi_driver_template.cmd_per_lun = 1;
2899 /* Set max sectors (module load option). Default is 255 sectors. */
2900 scsi_driver_template.max_sectors = max_sectors;
2903 /* Register our high level driver with 1394 stack */
2904 hpsb_register_highlevel(&sbp2_highlevel);
2906 /* Register our sbp2 status address space... */
2907 hpsb_register_addrspace(&sbp2_highlevel, &sbp2_ops, SBP2_STATUS_FIFO_ADDRESS,
2908 SBP2_STATUS_FIFO_ADDRESS +
2909 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(SBP2SCSI_MAX_SCSI_IDS+1));
2911 /* Handle data movement if physical dma is not enabled/supported
2912 * on host controller */
2913 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
2914 hpsb_register_addrspace(&sbp2_highlevel, &sbp2_physdma_ops, 0x0ULL, 0xfffffffcULL);
2917 hpsb_register_protocol(&sbp2_driver);
2922 static void __exit sbp2_module_exit(void)
2924 SBP2_DEBUG("sbp2_module_exit");
2926 hpsb_unregister_protocol(&sbp2_driver);
2928 hpsb_unregister_highlevel(&sbp2_highlevel);
2931 module_init(sbp2_module_init);
2932 module_exit(sbp2_module_exit);