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/pci.h>
59 #include <asm/current.h>
60 #include <asm/uaccess.h>
62 #include <asm/byteorder.h>
63 #include <asm/atomic.h>
64 #include <asm/system.h>
65 #include <asm/scatterlist.h>
67 #include <scsi/scsi.h>
68 #include <scsi/scsi_cmnd.h>
69 #include <scsi/scsi_dbg.h>
70 #include <scsi/scsi_device.h>
71 #include <scsi/scsi_host.h>
75 #include "ieee1394_types.h"
76 #include "ieee1394_core.h"
79 #include "highlevel.h"
80 #include "ieee1394_transactions.h"
83 static char version[] __devinitdata =
84 "$Rev: 1219 $ Ben Collins <bcollins@debian.org>";
87 * Module load parameter definitions
91 * Change max_speed on module load if you have a bad IEEE-1394
92 * controller that has trouble running 2KB packets at 400mb.
94 * NOTE: On certain OHCI parts I have seen short packets on async transmit
95 * (probably due to PCI latency/throughput issues with the part). You can
96 * bump down the speed if you are running into problems.
98 static int max_speed = IEEE1394_SPEED_MAX;
99 module_param(max_speed, int, 0644);
100 MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb default, 1 = 200mb, 0 = 100mb)");
103 * Set serialize_io to 1 if you'd like only one scsi command sent
104 * down to us at a time (debugging). This might be necessary for very
105 * badly behaved sbp2 devices.
107 static int serialize_io = 0;
108 module_param(serialize_io, int, 0444);
109 MODULE_PARM_DESC(serialize_io, "Serialize all I/O coming down from the scsi drivers (default = 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 = 255)");
124 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
125 * do an exclusive login, as it's generally unsafe to have two hosts
126 * talking to a single sbp2 device at the same time (filesystem coherency,
127 * etc.). If you're running an sbp2 device that supports multiple logins,
128 * and you're either running read-only filesystems or some sort of special
129 * filesystem supporting multiple hosts (one such filesystem is OpenGFS,
130 * see opengfs.sourceforge.net for more info), then set exclusive_login
131 * to zero. Note: The Oxsemi OXFW911 sbp2 chipset supports up to four
134 static int exclusive_login = 1;
135 module_param(exclusive_login, int, 0644);
136 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
139 * SCSI inquiry hack for really badly behaved sbp2 devices. Turn this on
140 * if your sbp2 device is not properly handling the SCSI inquiry command.
141 * This hack makes the inquiry look more like a typical MS Windows
144 * If force_inquiry_hack=1 is required for your device to work,
145 * please submit the logged sbp2_firmware_revision value of this device to
146 * the linux1394-devel mailing list.
148 static int force_inquiry_hack = 0;
149 module_param(force_inquiry_hack, int, 0444);
150 MODULE_PARM_DESC(force_inquiry_hack, "Force SCSI inquiry hack (default = 0)");
154 * Export information about protocols/devices supported by this driver.
156 static struct ieee1394_device_id sbp2_id_table[] = {
158 .match_flags =IEEE1394_MATCH_SPECIFIER_ID |
159 IEEE1394_MATCH_VERSION,
160 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
161 .version = SBP2_SW_VERSION_ENTRY & 0xffffff
166 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
169 * Debug levels, configured via kernel config, or enable here.
172 /* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
173 /* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
174 /* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
175 /* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
176 /* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
178 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
179 #define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
180 static u32 global_outstanding_command_orbs = 0;
181 #define outstanding_orb_incr global_outstanding_command_orbs++
182 #define outstanding_orb_decr global_outstanding_command_orbs--
184 #define SBP2_ORB_DEBUG(fmt, args...)
185 #define outstanding_orb_incr
186 #define outstanding_orb_decr
189 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
190 #define SBP2_DMA_ALLOC(fmt, args...) \
191 HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
192 ++global_outstanding_dmas, ## args)
193 #define SBP2_DMA_FREE(fmt, args...) \
194 HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
195 --global_outstanding_dmas, ## args)
196 static u32 global_outstanding_dmas = 0;
198 #define SBP2_DMA_ALLOC(fmt, args...)
199 #define SBP2_DMA_FREE(fmt, args...)
202 #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
203 #define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
204 #define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
205 #define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
206 #define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
207 #elif CONFIG_IEEE1394_SBP2_DEBUG == 1
208 #define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
209 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
210 #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
211 #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
213 #define SBP2_DEBUG(fmt, args...)
214 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
215 #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
216 #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
219 #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
226 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
229 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
230 u32 scsi_status, struct scsi_cmnd *SCpnt,
231 void (*done)(struct scsi_cmnd *));
233 static struct scsi_host_template scsi_driver_template;
235 static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
237 static void sbp2_host_reset(struct hpsb_host *host);
239 static int sbp2_probe(struct device *dev);
240 static int sbp2_remove(struct device *dev);
241 static int sbp2_update(struct unit_directory *ud);
243 static struct hpsb_highlevel sbp2_highlevel = {
244 .name = SBP2_DEVICE_NAME,
245 .host_reset = sbp2_host_reset,
248 static struct hpsb_address_ops sbp2_ops = {
249 .write = sbp2_handle_status_write
252 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
253 static struct hpsb_address_ops sbp2_physdma_ops = {
254 .read = sbp2_handle_physdma_read,
255 .write = sbp2_handle_physdma_write,
259 static struct hpsb_protocol_driver sbp2_driver = {
260 .name = "SBP2 Driver",
261 .id_table = sbp2_id_table,
262 .update = sbp2_update,
264 .name = SBP2_DEVICE_NAME,
265 .bus = &ieee1394_bus_type,
267 .remove = sbp2_remove,
272 /* List of device firmware's that require a forced 36 byte inquiry. */
273 static u32 sbp2_broken_inquiry_list[] = {
274 0x00002800, /* Stefan Richter <richtest@bauwesen.tu-cottbus.de> */
275 /* DViCO Momobay CX-1 */
276 0x00000200 /* Andreas Plesch <plesch@fas.harvard.edu> */
277 /* QPS Fire DVDBurner */
280 #define NUM_BROKEN_INQUIRY_DEVS \
281 (sizeof(sbp2_broken_inquiry_list)/sizeof(*sbp2_broken_inquiry_list))
283 /**************************************
284 * General utility functions
285 **************************************/
290 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
292 static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
296 for (length = (length >> 2); length--; )
297 temp[length] = be32_to_cpu(temp[length]);
303 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
305 static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
309 for (length = (length >> 2); length--; )
310 temp[length] = cpu_to_be32(temp[length]);
314 #else /* BIG_ENDIAN */
315 /* Why waste the cpu cycles? */
316 #define sbp2util_be32_to_cpu_buffer(x,y)
317 #define sbp2util_cpu_to_be32_buffer(x,y)
320 #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
322 * Debug packet dump routine. Length is in bytes.
324 static void sbp2util_packet_dump(void *buffer, int length, char *dump_name, u32 dump_phys_addr)
327 unsigned char *dump = buffer;
329 if (!dump || !length || !dump_name)
333 printk("[%s, 0x%x]", dump_name, dump_phys_addr);
335 printk("[%s]", dump_name);
336 for (i = 0; i < length; i++) {
345 printk("%02x ", (int) dump[i]);
352 #define sbp2util_packet_dump(w,x,y,z)
356 * Goofy routine that basically does a down_timeout function.
358 static int sbp2util_down_timeout(atomic_t *done, int timeout)
362 for (i = timeout; (i > 0 && atomic_read(done) == 0); i-= HZ/10) {
363 if (msleep_interruptible(100)) /* 100ms */
366 return ((i > 0) ? 0:1);
369 /* Free's an allocated packet */
370 static void sbp2_free_packet(struct hpsb_packet *packet)
372 hpsb_free_tlabel(packet);
373 hpsb_free_packet(packet);
376 /* This is much like hpsb_node_write(), except it ignores the response
377 * subaction and returns immediately. Can be used from interrupts.
379 static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
380 quadlet_t *buffer, size_t length)
382 struct hpsb_packet *packet;
384 packet = hpsb_make_writepacket(ne->host, ne->nodeid,
385 addr, buffer, length);
389 hpsb_set_packet_complete_task(packet, (void (*)(void*))sbp2_free_packet,
392 hpsb_node_fill_packet(ne, packet);
394 if (hpsb_send_packet(packet) < 0) {
395 sbp2_free_packet(packet);
403 * This function is called to create a pool of command orbs used for
404 * command processing. It is called when a new sbp2 device is detected.
406 static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
408 struct sbp2scsi_host_info *hi = scsi_id->hi;
410 unsigned long flags, orbs;
411 struct sbp2_command_info *command;
413 orbs = serialize_io ? 2 : SBP2_MAX_CMDS;
415 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
416 for (i = 0; i < orbs; i++) {
417 command = (struct sbp2_command_info *)
418 kmalloc(sizeof(struct sbp2_command_info), GFP_ATOMIC);
420 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
423 memset(command, '\0', sizeof(struct sbp2_command_info));
424 command->command_orb_dma =
425 pci_map_single (hi->host->pdev, &command->command_orb,
426 sizeof(struct sbp2_command_orb),
427 PCI_DMA_BIDIRECTIONAL);
428 SBP2_DMA_ALLOC("single command orb DMA");
430 pci_map_single (hi->host->pdev, &command->scatter_gather_element,
431 sizeof(command->scatter_gather_element),
432 PCI_DMA_BIDIRECTIONAL);
433 SBP2_DMA_ALLOC("scatter_gather_element");
434 INIT_LIST_HEAD(&command->list);
435 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
437 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
442 * This function is called to delete a pool of command orbs.
444 static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
446 struct hpsb_host *host = scsi_id->hi->host;
447 struct list_head *lh, *next;
448 struct sbp2_command_info *command;
451 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
452 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
453 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
454 command = list_entry(lh, struct sbp2_command_info, list);
456 /* Release our generic DMA's */
457 pci_unmap_single(host->pdev, command->command_orb_dma,
458 sizeof(struct sbp2_command_orb),
459 PCI_DMA_BIDIRECTIONAL);
460 SBP2_DMA_FREE("single command orb DMA");
461 pci_unmap_single(host->pdev, command->sge_dma,
462 sizeof(command->scatter_gather_element),
463 PCI_DMA_BIDIRECTIONAL);
464 SBP2_DMA_FREE("scatter_gather_element");
469 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
474 * This function finds the sbp2_command for a given outstanding command
475 * orb.Only looks at the inuse list.
477 static struct sbp2_command_info *sbp2util_find_command_for_orb(
478 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
480 struct sbp2_command_info *command;
483 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
484 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
485 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, 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 sbp2_command_info *command;
508 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
509 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
510 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
511 if (command->Current_SCpnt == SCpnt) {
512 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
517 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
522 * This function allocates a command orb used to send a scsi command.
524 static struct sbp2_command_info *sbp2util_allocate_command_orb(
525 struct scsi_id_instance_data *scsi_id,
526 struct scsi_cmnd *Current_SCpnt,
527 void (*Current_done)(struct scsi_cmnd *))
529 struct list_head *lh;
530 struct sbp2_command_info *command = NULL;
533 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
534 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
535 lh = scsi_id->sbp2_command_orb_completed.next;
537 command = list_entry(lh, struct sbp2_command_info, list);
538 command->Current_done = Current_done;
539 command->Current_SCpnt = Current_SCpnt;
540 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
542 SBP2_ERR("sbp2util_allocate_command_orb - No orbs available!");
544 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
549 static void sbp2util_free_command_dma(struct sbp2_command_info *command)
551 struct scsi_id_instance_data *scsi_id =
552 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
553 struct hpsb_host *host;
556 printk(KERN_ERR "%s: scsi_id == NULL\n", __FUNCTION__);
560 host = scsi_id->ud->ne->host;
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 *********************************************/
603 static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud);
605 static int sbp2_probe(struct device *dev)
607 struct unit_directory *ud;
608 struct scsi_id_instance_data *scsi_id;
610 SBP2_DEBUG("sbp2_probe");
612 ud = container_of(dev, struct unit_directory, device);
614 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
616 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
619 scsi_id = sbp2_alloc_device(ud);
624 sbp2_parse_unit_directory(scsi_id, ud);
626 return sbp2_start_device(scsi_id);
629 static int sbp2_remove(struct device *dev)
631 struct unit_directory *ud;
632 struct scsi_id_instance_data *scsi_id;
634 SBP2_DEBUG("sbp2_remove");
636 ud = container_of(dev, struct unit_directory, device);
637 scsi_id = ud->device.driver_data;
639 sbp2_logout_device(scsi_id);
640 sbp2_remove_device(scsi_id);
645 static int sbp2_update(struct unit_directory *ud)
647 struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
649 SBP2_DEBUG("sbp2_update");
651 if (sbp2_reconnect_device(scsi_id)) {
654 * Ok, reconnect has failed. Perhaps we didn't
655 * reconnect fast enough. Try doing a regular login, but
656 * first do a logout just in case of any weirdness.
658 sbp2_logout_device(scsi_id);
660 if (sbp2_login_device(scsi_id)) {
661 /* Login failed too, just fail, and the backend
662 * will call our sbp2_remove for us */
663 SBP2_ERR("Failed to reconnect to sbp2 device!");
668 /* Set max retries to something large on the device. */
669 sbp2_set_busy_timeout(scsi_id);
671 /* Do a SBP-2 fetch agent reset. */
672 sbp2_agent_reset(scsi_id, 1);
674 /* Get the max speed and packet size that we can use. */
675 sbp2_max_speed_and_size(scsi_id);
677 /* Complete any pending commands with busy (so they get
678 * retried) and remove them from our queue
680 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
682 /* Make sure we unblock requests (since this is likely after a bus
684 scsi_unblock_requests(scsi_id->scsi_host);
689 /* This functions is called by the sbp2_probe, for each new device. We now
690 * allocate one scsi host for each scsi_id (unit directory). */
691 static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
693 struct sbp2scsi_host_info *hi;
694 struct Scsi_Host *scsi_host = NULL;
695 struct scsi_id_instance_data *scsi_id = NULL;
697 SBP2_DEBUG("sbp2_alloc_device");
699 scsi_id = kmalloc(sizeof(*scsi_id), GFP_KERNEL);
701 SBP2_ERR("failed to create scsi_id");
704 memset(scsi_id, 0, sizeof(*scsi_id));
706 scsi_id->ne = ud->ne;
708 scsi_id->speed_code = IEEE1394_SPEED_100;
709 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
710 atomic_set(&scsi_id->sbp2_login_complete, 0);
711 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
712 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
713 INIT_LIST_HEAD(&scsi_id->scsi_list);
714 spin_lock_init(&scsi_id->sbp2_command_orb_lock);
715 scsi_id->sbp2_device_type_and_lun = SBP2_DEVICE_TYPE_LUN_UNINITIALIZED;
717 ud->device.driver_data = scsi_id;
719 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
721 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
723 SBP2_ERR("failed to allocate hostinfo");
726 SBP2_DEBUG("sbp2_alloc_device: allocated hostinfo");
727 hi->host = ud->ne->host;
728 INIT_LIST_HEAD(&hi->scsi_ids);
730 /* Register our sbp2 status address space... */
731 hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host, &sbp2_ops,
732 SBP2_STATUS_FIFO_ADDRESS,
733 SBP2_STATUS_FIFO_ADDRESS +
734 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(SBP2_MAX_UDS_PER_NODE+1));
735 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
736 /* Handle data movement if physical dma is not
737 * enabled/supportedon host controller */
738 hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host, &sbp2_physdma_ops,
739 0x0ULL, 0xfffffffcULL);
745 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
747 /* Register our host with the SCSI stack. */
748 scsi_host = scsi_host_alloc(&scsi_driver_template,
749 sizeof (unsigned long));
751 SBP2_ERR("failed to register scsi host");
755 scsi_host->hostdata[0] = (unsigned long)scsi_id;
757 if (!scsi_add_host(scsi_host, &ud->device)) {
758 scsi_id->scsi_host = scsi_host;
762 SBP2_ERR("failed to add scsi host");
763 scsi_host_put(scsi_host);
766 sbp2_remove_device(scsi_id);
771 static void sbp2_host_reset(struct hpsb_host *host)
773 struct sbp2scsi_host_info *hi;
774 struct scsi_id_instance_data *scsi_id;
776 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
779 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
780 scsi_block_requests(scsi_id->scsi_host);
786 * This function is where we first pull the node unique ids, and then
787 * allocate memory and register a SBP-2 device.
789 static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
791 struct sbp2scsi_host_info *hi = scsi_id->hi;
792 struct scsi_device *sdev;
794 SBP2_DEBUG("sbp2_start_device");
797 scsi_id->login_response =
798 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_response),
799 &scsi_id->login_response_dma);
800 if (!scsi_id->login_response)
802 SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
804 /* Query logins ORB DMA */
805 scsi_id->query_logins_orb =
806 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_query_logins_orb),
807 &scsi_id->query_logins_orb_dma);
808 if (!scsi_id->query_logins_orb)
810 SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
812 /* Query logins response DMA */
813 scsi_id->query_logins_response =
814 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_query_logins_response),
815 &scsi_id->query_logins_response_dma);
816 if (!scsi_id->query_logins_response)
818 SBP2_DMA_ALLOC("consistent DMA region for query logins response");
820 /* Reconnect ORB DMA */
821 scsi_id->reconnect_orb =
822 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_reconnect_orb),
823 &scsi_id->reconnect_orb_dma);
824 if (!scsi_id->reconnect_orb)
826 SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
829 scsi_id->logout_orb =
830 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_logout_orb),
831 &scsi_id->logout_orb_dma);
832 if (!scsi_id->logout_orb)
834 SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
838 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_orb),
839 &scsi_id->login_orb_dma);
840 if (!scsi_id->login_orb) {
842 if (scsi_id->query_logins_response) {
843 pci_free_consistent(hi->host->pdev,
844 sizeof(struct sbp2_query_logins_response),
845 scsi_id->query_logins_response,
846 scsi_id->query_logins_response_dma);
847 SBP2_DMA_FREE("query logins response DMA");
850 if (scsi_id->query_logins_orb) {
851 pci_free_consistent(hi->host->pdev,
852 sizeof(struct sbp2_query_logins_orb),
853 scsi_id->query_logins_orb,
854 scsi_id->query_logins_orb_dma);
855 SBP2_DMA_FREE("query logins ORB DMA");
858 if (scsi_id->logout_orb) {
859 pci_free_consistent(hi->host->pdev,
860 sizeof(struct sbp2_logout_orb),
862 scsi_id->logout_orb_dma);
863 SBP2_DMA_FREE("logout ORB DMA");
866 if (scsi_id->reconnect_orb) {
867 pci_free_consistent(hi->host->pdev,
868 sizeof(struct sbp2_reconnect_orb),
869 scsi_id->reconnect_orb,
870 scsi_id->reconnect_orb_dma);
871 SBP2_DMA_FREE("reconnect ORB DMA");
874 if (scsi_id->login_response) {
875 pci_free_consistent(hi->host->pdev,
876 sizeof(struct sbp2_login_response),
877 scsi_id->login_response,
878 scsi_id->login_response_dma);
879 SBP2_DMA_FREE("login FIFO DMA");
882 list_del(&scsi_id->scsi_list);
886 SBP2_ERR ("Could not allocate memory for scsi_id");
890 SBP2_DMA_ALLOC("consistent DMA region for login ORB");
892 SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id);
895 * Create our command orb pool
897 if (sbp2util_create_command_orb_pool(scsi_id)) {
898 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
899 sbp2_remove_device(scsi_id);
903 /* Schedule a timeout here. The reason is that we may be so close
904 * to a bus reset, that the device is not available for logins.
905 * This can happen when the bus reset is caused by the host
906 * connected to the sbp2 device being removed. That host would
907 * have a certain amount of time to relogin before the sbp2 device
908 * allows someone else to login instead. One second makes sense. */
909 msleep_interruptible(1000);
910 if (signal_pending(current)) {
911 SBP2_WARN("aborting sbp2_start_device due to event");
912 sbp2_remove_device(scsi_id);
917 * Login to the sbp-2 device
919 if (sbp2_login_device(scsi_id)) {
920 /* Login failed, just remove the device. */
921 sbp2_remove_device(scsi_id);
926 * Set max retries to something large on the device
928 sbp2_set_busy_timeout(scsi_id);
931 * Do a SBP-2 fetch agent reset
933 sbp2_agent_reset(scsi_id, 1);
936 * Get the max speed and packet size that we can use
938 sbp2_max_speed_and_size(scsi_id);
940 /* Add this device to the scsi layer now */
941 sdev = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
943 SBP2_ERR("scsi_add_device failed");
944 return PTR_ERR(sdev);
951 * This function removes an sbp2 device from the sbp2scsi_host_info struct.
953 static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
955 struct sbp2scsi_host_info *hi;
957 SBP2_DEBUG("sbp2_remove_device");
964 /* This will remove our scsi device aswell */
965 if (scsi_id->scsi_host) {
966 scsi_remove_host(scsi_id->scsi_host);
967 scsi_host_put(scsi_id->scsi_host);
970 sbp2util_remove_command_orb_pool(scsi_id);
972 list_del(&scsi_id->scsi_list);
974 if (scsi_id->login_response) {
975 pci_free_consistent(hi->host->pdev,
976 sizeof(struct sbp2_login_response),
977 scsi_id->login_response,
978 scsi_id->login_response_dma);
979 SBP2_DMA_FREE("single login FIFO");
982 if (scsi_id->login_orb) {
983 pci_free_consistent(hi->host->pdev,
984 sizeof(struct sbp2_login_orb),
986 scsi_id->login_orb_dma);
987 SBP2_DMA_FREE("single login ORB");
990 if (scsi_id->reconnect_orb) {
991 pci_free_consistent(hi->host->pdev,
992 sizeof(struct sbp2_reconnect_orb),
993 scsi_id->reconnect_orb,
994 scsi_id->reconnect_orb_dma);
995 SBP2_DMA_FREE("single reconnect orb");
998 if (scsi_id->logout_orb) {
999 pci_free_consistent(hi->host->pdev,
1000 sizeof(struct sbp2_logout_orb),
1001 scsi_id->logout_orb,
1002 scsi_id->logout_orb_dma);
1003 SBP2_DMA_FREE("single logout orb");
1006 if (scsi_id->query_logins_orb) {
1007 pci_free_consistent(hi->host->pdev,
1008 sizeof(struct sbp2_query_logins_orb),
1009 scsi_id->query_logins_orb,
1010 scsi_id->query_logins_orb_dma);
1011 SBP2_DMA_FREE("single query logins orb");
1014 if (scsi_id->query_logins_response) {
1015 pci_free_consistent(hi->host->pdev,
1016 sizeof(struct sbp2_query_logins_response),
1017 scsi_id->query_logins_response,
1018 scsi_id->query_logins_response_dma);
1019 SBP2_DMA_FREE("single query logins data");
1022 scsi_id->ud->device.driver_data = NULL;
1024 SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id);
1029 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1031 * This function deals with physical dma write requests (for adapters that do not support
1032 * physical dma in hardware). Mostly just here for debugging...
1034 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid, int destid, quadlet_t *data,
1035 u64 addr, size_t length, u16 flags)
1039 * Manually put the data in the right place.
1041 memcpy(bus_to_virt((u32)addr), data, length);
1042 sbp2util_packet_dump(data, length, "sbp2 phys dma write by device", (u32)addr);
1043 return(RCODE_COMPLETE);
1047 * This function deals with physical dma read requests (for adapters that do not support
1048 * physical dma in hardware). Mostly just here for debugging...
1050 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid, quadlet_t *data,
1051 u64 addr, size_t length, u16 flags)
1055 * Grab data from memory and send a read response.
1057 memcpy(data, bus_to_virt((u32)addr), length);
1058 sbp2util_packet_dump(data, length, "sbp2 phys dma read by device", (u32)addr);
1059 return(RCODE_COMPLETE);
1064 /**************************************
1065 * SBP-2 protocol related section
1066 **************************************/
1069 * This function determines if we should convert scsi commands for a particular sbp2 device type
1071 static __inline__ int sbp2_command_conversion_device_type(u8 device_type)
1073 return (((device_type == TYPE_DISK) ||
1074 (device_type == TYPE_RBC) ||
1075 (device_type == TYPE_ROM)) ? 1:0);
1079 * This function queries the device for the maximum concurrent logins it
1082 static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1084 struct sbp2scsi_host_info *hi = scsi_id->hi;
1089 SBP2_DEBUG("sbp2_query_logins");
1091 scsi_id->query_logins_orb->reserved1 = 0x0;
1092 scsi_id->query_logins_orb->reserved2 = 0x0;
1094 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1095 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1096 SBP2_DEBUG("sbp2_query_logins: query_response_hi/lo initialized");
1098 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1099 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1100 if (scsi_id->sbp2_device_type_and_lun != SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
1101 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
1102 SBP2_DEBUG("sbp2_query_logins: set lun to %d",
1103 ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun));
1105 SBP2_DEBUG("sbp2_query_logins: lun_misc initialized");
1107 scsi_id->query_logins_orb->reserved_resp_length =
1108 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
1109 SBP2_DEBUG("sbp2_query_logins: reserved_resp_length initialized");
1111 scsi_id->query_logins_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1112 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->ud->id);
1113 scsi_id->query_logins_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1114 SBP2_STATUS_FIFO_ADDRESS_HI);
1115 SBP2_DEBUG("sbp2_query_logins: status FIFO initialized");
1117 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1119 SBP2_DEBUG("sbp2_query_logins: orb byte-swapped");
1121 sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
1122 "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
1124 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1125 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1127 SBP2_DEBUG("sbp2_query_logins: query_logins_response/status FIFO memset");
1129 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1130 data[1] = scsi_id->query_logins_orb_dma;
1131 sbp2util_cpu_to_be32_buffer(data, 8);
1133 atomic_set(&scsi_id->sbp2_login_complete, 0);
1135 SBP2_DEBUG("sbp2_query_logins: prepared to write");
1136 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1137 SBP2_DEBUG("sbp2_query_logins: written");
1139 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 2*HZ)) {
1140 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1144 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1145 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1149 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1150 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1151 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1153 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1157 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1159 SBP2_DEBUG("length_max_logins = %x",
1160 (unsigned int)scsi_id->query_logins_response->length_max_logins);
1162 SBP2_DEBUG("Query logins to SBP-2 device successful");
1164 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
1165 SBP2_DEBUG("Maximum concurrent logins supported: %d", max_logins);
1167 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
1168 SBP2_DEBUG("Number of active logins: %d", active_logins);
1170 if (active_logins >= max_logins) {
1178 * This function is called in order to login to a particular SBP-2 device,
1179 * after a bus reset.
1181 static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1183 struct sbp2scsi_host_info *hi = scsi_id->hi;
1186 SBP2_DEBUG("sbp2_login_device");
1188 if (!scsi_id->login_orb) {
1189 SBP2_DEBUG("sbp2_login_device: login_orb not alloc'd!");
1193 if (!exclusive_login) {
1194 if (sbp2_query_logins(scsi_id)) {
1195 SBP2_INFO("Device does not support any more concurrent logins");
1200 /* Set-up login ORB, assume no password */
1201 scsi_id->login_orb->password_hi = 0;
1202 scsi_id->login_orb->password_lo = 0;
1203 SBP2_DEBUG("sbp2_login_device: password_hi/lo initialized");
1205 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1206 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1207 SBP2_DEBUG("sbp2_login_device: login_response_hi/lo initialized");
1209 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1210 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
1211 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
1212 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
1213 /* Set the lun if we were able to pull it from the device's unit directory */
1214 if (scsi_id->sbp2_device_type_and_lun != SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
1215 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
1216 SBP2_DEBUG("sbp2_query_logins: set lun to %d",
1217 ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun));
1219 SBP2_DEBUG("sbp2_login_device: lun_misc initialized");
1221 scsi_id->login_orb->passwd_resp_lengths =
1222 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1223 SBP2_DEBUG("sbp2_login_device: passwd_resp_lengths initialized");
1225 scsi_id->login_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1226 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->ud->id);
1227 scsi_id->login_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1228 SBP2_STATUS_FIFO_ADDRESS_HI);
1229 SBP2_DEBUG("sbp2_login_device: status FIFO initialized");
1232 * Byte swap ORB if necessary
1234 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1236 SBP2_DEBUG("sbp2_login_device: orb byte-swapped");
1238 sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
1239 "sbp2 login orb", scsi_id->login_orb_dma);
1242 * Initialize login response and status fifo
1244 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1245 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1247 SBP2_DEBUG("sbp2_login_device: login_response/status FIFO memset");
1250 * Ok, let's write to the target's management agent register
1252 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1253 data[1] = scsi_id->login_orb_dma;
1254 sbp2util_cpu_to_be32_buffer(data, 8);
1256 atomic_set(&scsi_id->sbp2_login_complete, 0);
1258 SBP2_DEBUG("sbp2_login_device: prepared to write to %08x",
1259 (unsigned int)scsi_id->sbp2_management_agent_addr);
1260 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1261 SBP2_DEBUG("sbp2_login_device: written");
1264 * Wait for login status (up to 20 seconds)...
1266 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 20*HZ)) {
1267 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1272 * Sanity. Make sure status returned matches login orb.
1274 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1275 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1282 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1283 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1284 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1286 SBP2_ERR("Error logging into SBP-2 device - login failed");
1291 * Byte swap the login response, for use when reconnecting or
1294 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1297 * Grab our command block agent address from the login response.
1299 SBP2_DEBUG("command_block_agent_hi = %x",
1300 (unsigned int)scsi_id->login_response->command_block_agent_hi);
1301 SBP2_DEBUG("command_block_agent_lo = %x",
1302 (unsigned int)scsi_id->login_response->command_block_agent_lo);
1304 scsi_id->sbp2_command_block_agent_addr =
1305 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1306 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1307 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1309 SBP2_INFO("Logged into SBP-2 device");
1316 * This function is called in order to logout from a particular SBP-2
1317 * device, usually called during driver unload.
1319 static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1321 struct sbp2scsi_host_info *hi = scsi_id->hi;
1325 SBP2_DEBUG("sbp2_logout_device");
1330 scsi_id->logout_orb->reserved1 = 0x0;
1331 scsi_id->logout_orb->reserved2 = 0x0;
1332 scsi_id->logout_orb->reserved3 = 0x0;
1333 scsi_id->logout_orb->reserved4 = 0x0;
1335 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1336 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1338 /* Notify us when complete */
1339 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1341 scsi_id->logout_orb->reserved5 = 0x0;
1342 scsi_id->logout_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1343 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->ud->id);
1344 scsi_id->logout_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1345 SBP2_STATUS_FIFO_ADDRESS_HI);
1348 * Byte swap ORB if necessary
1350 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1352 sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
1353 "sbp2 logout orb", scsi_id->logout_orb_dma);
1356 * Ok, let's write to the target's management agent register
1358 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1359 data[1] = scsi_id->logout_orb_dma;
1360 sbp2util_cpu_to_be32_buffer(data, 8);
1362 atomic_set(&scsi_id->sbp2_login_complete, 0);
1364 error = hpsb_node_write(scsi_id->ne,
1365 scsi_id->sbp2_management_agent_addr,
1370 /* Wait for device to logout...1 second. */
1371 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ))
1374 SBP2_INFO("Logged out of SBP-2 device");
1381 * This function is called in order to reconnect to a particular SBP-2
1382 * device, after a bus reset.
1384 static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1386 struct sbp2scsi_host_info *hi = scsi_id->hi;
1390 SBP2_DEBUG("sbp2_reconnect_device");
1393 * Set-up reconnect ORB
1395 scsi_id->reconnect_orb->reserved1 = 0x0;
1396 scsi_id->reconnect_orb->reserved2 = 0x0;
1397 scsi_id->reconnect_orb->reserved3 = 0x0;
1398 scsi_id->reconnect_orb->reserved4 = 0x0;
1400 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1401 scsi_id->reconnect_orb->login_ID_misc |=
1402 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1404 /* Notify us when complete */
1405 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1407 scsi_id->reconnect_orb->reserved5 = 0x0;
1408 scsi_id->reconnect_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1409 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->ud->id);
1410 scsi_id->reconnect_orb->status_FIFO_hi =
1411 (ORB_SET_NODE_ID(hi->host->node_id) | SBP2_STATUS_FIFO_ADDRESS_HI);
1414 * Byte swap ORB if necessary
1416 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1418 sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
1419 "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
1422 * Initialize status fifo
1424 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1427 * Ok, let's write to the target's management agent register
1429 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1430 data[1] = scsi_id->reconnect_orb_dma;
1431 sbp2util_cpu_to_be32_buffer(data, 8);
1433 atomic_set(&scsi_id->sbp2_login_complete, 0);
1435 error = hpsb_node_write(scsi_id->ne,
1436 scsi_id->sbp2_management_agent_addr,
1442 * Wait for reconnect status (up to 1 second)...
1444 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) {
1445 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1450 * Sanity. Make sure status returned matches reconnect orb.
1452 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1453 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1460 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1461 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1462 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1464 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect failed");
1468 HPSB_DEBUG("Reconnected to SBP-2 device");
1475 * This function is called in order to set the busy timeout (number of
1476 * retries to attempt) on the sbp2 device.
1478 static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1482 SBP2_DEBUG("sbp2_set_busy_timeout");
1485 * Ok, let's write to the target's busy timeout register
1487 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1489 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4)) {
1490 SBP2_ERR("sbp2_set_busy_timeout error");
1498 * This function is called to parse sbp2 device's config rom unit
1499 * directory. Used to determine things like sbp2 management agent offset,
1500 * and command set used (SCSI or RBC).
1502 static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1503 struct unit_directory *ud)
1505 struct csr1212_keyval *kv;
1506 struct csr1212_dentry *dentry;
1507 u64 management_agent_addr;
1508 u32 command_set_spec_id, command_set, unit_characteristics,
1509 firmware_revision, workarounds;
1512 SBP2_DEBUG("sbp2_parse_unit_directory");
1514 management_agent_addr = 0x0;
1515 command_set_spec_id = 0x0;
1517 unit_characteristics = 0x0;
1518 firmware_revision = 0x0;
1520 /* Handle different fields in the unit directory, based on keys */
1521 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1522 switch (kv->key.id) {
1523 case CSR1212_KV_ID_DEPENDENT_INFO:
1524 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET) {
1525 /* Save off the management agent address */
1526 management_agent_addr =
1527 CSR1212_REGISTER_SPACE_BASE +
1528 (kv->value.csr_offset << 2);
1530 SBP2_DEBUG("sbp2_management_agent_addr = %x",
1531 (unsigned int) management_agent_addr);
1532 } else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
1533 scsi_id->sbp2_device_type_and_lun = kv->value.immediate;
1537 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1538 /* Command spec organization */
1539 command_set_spec_id = kv->value.immediate;
1540 SBP2_DEBUG("sbp2_command_set_spec_id = %x",
1541 (unsigned int) command_set_spec_id);
1544 case SBP2_COMMAND_SET_KEY:
1545 /* Command set used by sbp2 device */
1546 command_set = kv->value.immediate;
1547 SBP2_DEBUG("sbp2_command_set = %x",
1548 (unsigned int) command_set);
1551 case SBP2_UNIT_CHARACTERISTICS_KEY:
1553 * Unit characterisitcs (orb related stuff
1554 * that I'm not yet paying attention to)
1556 unit_characteristics = kv->value.immediate;
1557 SBP2_DEBUG("sbp2_unit_characteristics = %x",
1558 (unsigned int) unit_characteristics);
1561 case SBP2_FIRMWARE_REVISION_KEY:
1562 /* Firmware revision */
1563 firmware_revision = kv->value.immediate;
1564 if (force_inquiry_hack)
1565 SBP2_INFO("sbp2_firmware_revision = %x",
1566 (unsigned int) firmware_revision);
1567 else SBP2_DEBUG("sbp2_firmware_revision = %x",
1568 (unsigned int) firmware_revision);
1576 /* This is the start of our broken device checking. We try to hack
1577 * around oddities and known defects. */
1580 /* If the vendor id is 0xa0b8 (Symbios vendor id), then we have a
1581 * bridge with 128KB max transfer size limitation. For sanity, we
1582 * only voice this when the current max_sectors setting
1583 * exceeds the 128k limit. By default, that is not the case.
1585 * It would be really nice if we could detect this before the scsi
1586 * host gets initialized. That way we can down-force the
1587 * max_sectors to account for it. That is not currently
1589 if ((firmware_revision & 0xffff00) ==
1590 SBP2_128KB_BROKEN_FIRMWARE &&
1591 (max_sectors * 512) > (128*1024)) {
1592 SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB max transfer size.",
1593 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid));
1594 SBP2_WARN("WARNING: Current max_sectors setting is larger than 128KB (%d sectors)!",
1596 workarounds |= SBP2_BREAKAGE_128K_MAX_TRANSFER;
1599 /* Check for a blacklisted set of devices that require us to force
1600 * a 36 byte host inquiry. This can be overriden as a module param
1601 * (to force all hosts). */
1602 for (i = 0; i < NUM_BROKEN_INQUIRY_DEVS; i++) {
1603 if ((firmware_revision & 0xffff00) ==
1604 sbp2_broken_inquiry_list[i]) {
1605 SBP2_WARN("Node " NODE_BUS_FMT ": Using 36byte inquiry workaround",
1606 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid));
1607 workarounds |= SBP2_BREAKAGE_INQUIRY_HACK;
1608 break; /* No need to continue. */
1612 /* If this is a logical unit directory entry, process the parent
1613 * to get the values. */
1614 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1615 struct unit_directory *parent_ud =
1616 container_of(ud->device.parent, struct unit_directory, device);
1617 sbp2_parse_unit_directory(scsi_id, parent_ud);
1619 scsi_id->sbp2_management_agent_addr = management_agent_addr;
1620 scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
1621 scsi_id->sbp2_command_set = command_set;
1622 scsi_id->sbp2_unit_characteristics = unit_characteristics;
1623 scsi_id->sbp2_firmware_revision = firmware_revision;
1624 scsi_id->workarounds = workarounds;
1625 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
1626 scsi_id->sbp2_device_type_and_lun = ud->lun;
1631 * This function is called in order to determine the max speed and packet
1632 * size we can use in our ORBs. Note, that we (the driver and host) only
1633 * initiate the transaction. The SBP-2 device actually transfers the data
1634 * (by reading from the DMA area we tell it). This means that the SBP-2
1635 * device decides the actual maximum data it can transfer. We just tell it
1636 * the speed that it needs to use, and the max_rec the host supports, and
1637 * it takes care of the rest.
1639 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1641 struct sbp2scsi_host_info *hi = scsi_id->hi;
1643 SBP2_DEBUG("sbp2_max_speed_and_size");
1645 /* Initial setting comes from the hosts speed map */
1646 scsi_id->speed_code = hi->host->speed_map[NODEID_TO_NODE(hi->host->node_id) * 64
1647 + NODEID_TO_NODE(scsi_id->ne->nodeid)];
1649 /* Bump down our speed if the user requested it */
1650 if (scsi_id->speed_code > max_speed) {
1651 scsi_id->speed_code = max_speed;
1652 SBP2_ERR("Forcing SBP-2 max speed down to %s",
1653 hpsb_speedto_str[scsi_id->speed_code]);
1656 /* Payload size is the lesser of what our speed supports and what
1657 * our host supports. */
1658 scsi_id->max_payload_size = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1659 (u8)(hi->host->csr.max_rec - 1));
1661 HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1662 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1663 hpsb_speedto_str[scsi_id->speed_code],
1664 1 << ((u32)scsi_id->max_payload_size + 2));
1670 * This function is called in order to perform a SBP-2 agent reset.
1672 static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1678 SBP2_DEBUG("sbp2_agent_reset");
1681 * Ok, let's write to the target's management agent register
1683 data = ntohl(SBP2_AGENT_RESET_DATA);
1684 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1687 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1689 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1692 SBP2_ERR("hpsb_node_write failed.\n");
1697 * Need to make sure orb pointer is written on next command
1699 scsi_id->last_orb = NULL;
1705 * This function is called to create the actual command orb and s/g list
1706 * out of the scsi command itself.
1708 static int sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1709 struct sbp2_command_info *command,
1711 unsigned int scsi_use_sg,
1712 unsigned int scsi_request_bufflen,
1713 void *scsi_request_buffer,
1714 enum dma_data_direction dma_dir)
1717 struct sbp2scsi_host_info *hi = scsi_id->hi;
1718 struct scatterlist *sgpnt = (struct scatterlist *) scsi_request_buffer;
1719 struct sbp2_command_orb *command_orb = &command->command_orb;
1720 struct sbp2_unrestricted_page_table *scatter_gather_element =
1721 &command->scatter_gather_element[0];
1722 u32 sg_count, sg_len, orb_direction;
1727 * Set-up our command ORB..
1729 * NOTE: We're doing unrestricted page tables (s/g), as this is
1730 * best performance (at least with the devices I have). This means
1731 * that data_size becomes the number of s/g elements, and
1732 * page_size should be zero (for unrestricted).
1734 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1735 command_orb->next_ORB_lo = 0x0;
1736 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1737 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
1738 command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
1741 * Get the direction of the transfer. If the direction is unknown, then use our
1742 * goofy table as a back-up.
1746 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1749 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1751 case DMA_FROM_DEVICE:
1752 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1754 case DMA_BIDIRECTIONAL:
1756 SBP2_ERR("SCSI data transfer direction not specified. "
1757 "Update the SBP2 direction table in sbp2.h if "
1758 "necessary for your application");
1759 __scsi_print_command(scsi_cmd);
1760 orb_direction = sbp2scsi_direction_table[*scsi_cmd];
1765 * Set-up our pagetable stuff... unfortunately, this has become
1766 * messier than I'd like. Need to clean this up a bit. ;-)
1768 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1770 SBP2_DEBUG("No data transfer");
1773 * Handle no data transfer
1775 command_orb->data_descriptor_hi = 0x0;
1776 command_orb->data_descriptor_lo = 0x0;
1777 command_orb->misc |= ORB_SET_DIRECTION(1);
1779 } else if (scsi_use_sg) {
1781 SBP2_DEBUG("Use scatter/gather");
1784 * Special case if only one element (and less than 64KB in size)
1786 if ((scsi_use_sg == 1) && (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1788 SBP2_DEBUG("Only one s/g element");
1789 command->dma_dir = dma_dir;
1790 command->dma_size = sgpnt[0].length;
1791 command->dma_type = CMD_DMA_PAGE;
1792 command->cmd_dma = pci_map_page(hi->host->pdev,
1797 SBP2_DMA_ALLOC("single page scatter element");
1799 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1800 command_orb->data_descriptor_lo = command->cmd_dma;
1801 command_orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1802 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1805 int count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg, dma_dir);
1806 SBP2_DMA_ALLOC("scatter list");
1808 command->dma_size = scsi_use_sg;
1809 command->dma_dir = dma_dir;
1810 command->sge_buffer = sgpnt;
1812 /* use page tables (s/g) */
1813 command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1814 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1815 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1816 command_orb->data_descriptor_lo = command->sge_dma;
1819 * Loop through and fill out our sbp-2 page tables
1820 * (and split up anything too large)
1822 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1823 sg_len = sg_dma_len(sgpnt);
1824 sg_addr = sg_dma_address(sgpnt);
1826 scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1827 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1828 scatter_gather_element[sg_count].length_segment_base_hi =
1829 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1830 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1831 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1833 scatter_gather_element[sg_count].length_segment_base_hi =
1834 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1841 /* Number of page table (s/g) elements */
1842 command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1844 sbp2util_packet_dump(scatter_gather_element,
1845 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1846 "sbp2 s/g list", command->sge_dma);
1849 * Byte swap page tables if necessary
1851 sbp2util_cpu_to_be32_buffer(scatter_gather_element,
1852 (sizeof(struct sbp2_unrestricted_page_table)) *
1859 SBP2_DEBUG("No scatter/gather");
1861 command->dma_dir = dma_dir;
1862 command->dma_size = scsi_request_bufflen;
1863 command->dma_type = CMD_DMA_SINGLE;
1864 command->cmd_dma = pci_map_single (hi->host->pdev, scsi_request_buffer,
1867 SBP2_DMA_ALLOC("single bulk");
1870 * Handle case where we get a command w/o s/g enabled (but
1871 * check for transfers larger than 64K)
1873 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1875 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1876 command_orb->data_descriptor_lo = command->cmd_dma;
1877 command_orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1878 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1881 * Sanity, in case our direction table is not
1884 if (!scsi_request_bufflen) {
1885 command_orb->data_descriptor_hi = 0x0;
1886 command_orb->data_descriptor_lo = 0x0;
1887 command_orb->misc |= ORB_SET_DIRECTION(1);
1892 * Need to turn this into page tables, since the
1893 * buffer is too large.
1895 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1896 command_orb->data_descriptor_lo = command->sge_dma;
1898 /* Use page tables (s/g) */
1899 command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1900 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1903 * fill out our sbp-2 page tables (and split up
1907 sg_len = scsi_request_bufflen;
1908 sg_addr = command->cmd_dma;
1910 scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1911 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1912 scatter_gather_element[sg_count].length_segment_base_hi =
1913 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1914 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1915 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1917 scatter_gather_element[sg_count].length_segment_base_hi =
1918 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1924 /* Number of page table (s/g) elements */
1925 command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1927 sbp2util_packet_dump(scatter_gather_element,
1928 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1929 "sbp2 s/g list", command->sge_dma);
1932 * Byte swap page tables if necessary
1934 sbp2util_cpu_to_be32_buffer(scatter_gather_element,
1935 (sizeof(struct sbp2_unrestricted_page_table)) *
1943 * Byte swap command ORB if necessary
1945 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1948 * Put our scsi command in the command ORB
1950 memset(command_orb->cdb, 0, 12);
1951 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1957 * This function is called in order to begin a regular SBP-2 command.
1959 static int sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
1960 struct sbp2_command_info *command)
1962 struct sbp2scsi_host_info *hi = scsi_id->hi;
1963 struct sbp2_command_orb *command_orb = &command->command_orb;
1964 struct node_entry *ne = scsi_id->ne;
1967 outstanding_orb_incr;
1968 SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
1969 command_orb, global_outstanding_command_orbs);
1971 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
1972 sizeof(struct sbp2_command_orb),
1973 PCI_DMA_BIDIRECTIONAL);
1974 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
1975 sizeof(command->scatter_gather_element),
1976 PCI_DMA_BIDIRECTIONAL);
1978 * Check to see if there are any previous orbs to use
1980 if (scsi_id->last_orb == NULL) {
1984 * Ok, let's write to the target's management agent register
1986 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_ORB_POINTER_OFFSET;
1987 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1988 data[1] = command->command_orb_dma;
1989 sbp2util_cpu_to_be32_buffer(data, 8);
1991 SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb);
1993 if (sbp2util_node_write_no_wait(ne, addr, data, 8) < 0) {
1994 SBP2_ERR("sbp2util_node_write_no_wait failed.\n");
1998 SBP2_ORB_DEBUG("write command agent complete");
2000 scsi_id->last_orb = command_orb;
2001 scsi_id->last_orb_dma = command->command_orb_dma;
2007 * We have an orb already sent (maybe or maybe not
2008 * processed) that we can append this orb to. So do so,
2009 * and ring the doorbell. Have to be very careful
2010 * modifying these next orb pointers, as they are accessed
2011 * both by the sbp2 device and us.
2013 scsi_id->last_orb->next_ORB_lo =
2014 cpu_to_be32(command->command_orb_dma);
2015 /* Tells hardware that this pointer is valid */
2016 scsi_id->last_orb->next_ORB_hi = 0x0;
2017 pci_dma_sync_single_for_device(hi->host->pdev, scsi_id->last_orb_dma,
2018 sizeof(struct sbp2_command_orb),
2019 PCI_DMA_BIDIRECTIONAL);
2024 data = cpu_to_be32(command->command_orb_dma);
2025 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_DOORBELL_OFFSET;
2027 SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb);
2029 if (sbp2util_node_write_no_wait(ne, addr, &data, 4) < 0) {
2030 SBP2_ERR("sbp2util_node_write_no_wait failed");
2034 scsi_id->last_orb = command_orb;
2035 scsi_id->last_orb_dma = command->command_orb_dma;
2042 * This function is called in order to begin a regular SBP-2 command.
2044 static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
2045 struct scsi_cmnd *SCpnt,
2046 void (*done)(struct scsi_cmnd *))
2048 unchar *cmd = (unchar *) SCpnt->cmnd;
2049 unsigned int request_bufflen = SCpnt->request_bufflen;
2050 struct sbp2_command_info *command;
2052 SBP2_DEBUG("sbp2_send_command");
2053 #if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
2054 printk("[scsi command]\n ");
2055 scsi_print_command(SCpnt);
2057 SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
2058 SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2061 * Allocate a command orb and s/g structure
2063 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
2069 * The scsi stack sends down a request_bufflen which does not match the
2070 * length field in the scsi cdb. This causes some sbp2 devices to
2071 * reject this inquiry command. Fix the request_bufflen.
2073 if (*cmd == INQUIRY) {
2074 if (force_inquiry_hack || scsi_id->workarounds & SBP2_BREAKAGE_INQUIRY_HACK)
2075 request_bufflen = cmd[4] = 0x24;
2077 request_bufflen = cmd[4];
2081 * Now actually fill in the comamnd orb and sbp2 s/g list
2083 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
2084 request_bufflen, SCpnt->request_buffer,
2085 SCpnt->sc_data_direction);
2087 * Update our cdb if necessary (to handle sbp2 RBC command set
2088 * differences). This is where the command set hacks go! =)
2090 sbp2_check_sbp2_command(scsi_id, command->command_orb.cdb);
2092 sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
2093 "sbp2 command orb", command->command_orb_dma);
2096 * Initialize status fifo
2098 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2101 * Link up the orb, and ring the doorbell if needed
2103 sbp2_link_orb_command(scsi_id, command);
2110 * This function deals with command set differences between Linux scsi
2111 * command set and sbp2 RBC command set.
2113 static void sbp2_check_sbp2_command(struct scsi_id_instance_data *scsi_id, unchar *cmd)
2118 * Translates SBP-2 status into SCSI sense data for check conditions
2120 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
2122 SBP2_DEBUG("sbp2_status_to_sense_data");
2125 * Ok, it's pretty ugly... ;-)
2127 sense_data[0] = 0x70;
2128 sense_data[1] = 0x0;
2129 sense_data[2] = sbp2_status[9];
2130 sense_data[3] = sbp2_status[12];
2131 sense_data[4] = sbp2_status[13];
2132 sense_data[5] = sbp2_status[14];
2133 sense_data[6] = sbp2_status[15];
2135 sense_data[8] = sbp2_status[16];
2136 sense_data[9] = sbp2_status[17];
2137 sense_data[10] = sbp2_status[18];
2138 sense_data[11] = sbp2_status[19];
2139 sense_data[12] = sbp2_status[10];
2140 sense_data[13] = sbp2_status[11];
2141 sense_data[14] = sbp2_status[20];
2142 sense_data[15] = sbp2_status[21];
2144 return(sbp2_status[8] & 0x3f); /* return scsi status */
2148 * This function is called after a command is completed, in order to do any necessary SBP-2
2149 * response data translations for the SCSI stack
2151 static void sbp2_check_sbp2_response(struct scsi_id_instance_data *scsi_id,
2152 struct scsi_cmnd *SCpnt)
2154 u8 *scsi_buf = SCpnt->request_buffer;
2156 SBP2_DEBUG("sbp2_check_sbp2_response");
2158 switch (SCpnt->cmnd[0]) {
2163 * If scsi_id->sbp2_device_type_and_lun is uninitialized, then fill
2164 * this information in from the inquiry response data. Lun is set to zero.
2166 if (scsi_id->sbp2_device_type_and_lun == SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
2167 SBP2_DEBUG("Creating sbp2_device_type_and_lun from scsi inquiry data");
2168 scsi_id->sbp2_device_type_and_lun = (scsi_buf[0] & 0x1f) << 16;
2172 * Make sure data length is ok. Minimum length is 36 bytes
2174 if (scsi_buf[4] == 0) {
2175 scsi_buf[4] = 36 - 5;
2179 * Fix ansi revision and response data format
2182 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
2193 * This function deals with status writes from the SBP-2 device
2195 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid,
2196 quadlet_t *data, u64 addr, size_t length, u16 fl)
2198 struct sbp2scsi_host_info *hi;
2199 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
2201 struct scsi_cmnd *SCpnt = NULL;
2202 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2203 struct sbp2_command_info *command;
2205 SBP2_DEBUG("sbp2_handle_status_write");
2207 sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
2210 SBP2_ERR("host is NULL - this is bad!");
2211 return(RCODE_ADDRESS_ERROR);
2214 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
2217 SBP2_ERR("host info is NULL - this is bad!");
2218 return(RCODE_ADDRESS_ERROR);
2222 * Find our scsi_id structure by looking at the status fifo address written to by
2225 id = SBP2_STATUS_FIFO_OFFSET_TO_ENTRY((u32)(addr - SBP2_STATUS_FIFO_ADDRESS));
2226 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
2227 if (scsi_id_tmp->ne->nodeid == nodeid && scsi_id_tmp->ud->id == id) {
2228 scsi_id = scsi_id_tmp;
2234 SBP2_ERR("scsi_id is NULL - device is gone?");
2235 return(RCODE_ADDRESS_ERROR);
2239 * Put response into scsi_id status fifo...
2241 memcpy(&scsi_id->status_block, data, length);
2244 * Byte swap first two quadlets (8 bytes) of status for processing
2246 sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8);
2249 * Handle command ORB status here if necessary. First, need to match status with command.
2251 command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo);
2254 SBP2_DEBUG("Found status for command ORB");
2255 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2256 sizeof(struct sbp2_command_orb),
2257 PCI_DMA_BIDIRECTIONAL);
2258 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2259 sizeof(command->scatter_gather_element),
2260 PCI_DMA_BIDIRECTIONAL);
2262 SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
2263 outstanding_orb_decr;
2266 * Matched status with command, now grab scsi command pointers and check status
2268 SCpnt = command->Current_SCpnt;
2269 sbp2util_mark_command_completed(scsi_id, command);
2274 * See if the target stored any scsi status information
2276 if (STATUS_GET_LENGTH(scsi_id->status_block.ORB_offset_hi_misc) > 1) {
2278 * Translate SBP-2 status to SCSI sense data
2280 SBP2_DEBUG("CHECK CONDITION");
2281 scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer);
2285 * Check to see if the dead bit is set. If so, we'll have to initiate
2286 * a fetch agent reset.
2288 if (STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc)) {
2291 * Initiate a fetch agent reset.
2293 SBP2_DEBUG("Dead bit set - initiating fetch agent reset");
2294 sbp2_agent_reset(scsi_id, 0);
2297 SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
2301 * Check here to see if there are no commands in-use. If there are none, we can
2302 * null out last orb so that next time around we write directly to the orb pointer...
2303 * Quick start saves one 1394 bus transaction.
2305 if (list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2306 scsi_id->last_orb = NULL;
2312 * It's probably a login/logout/reconnect status.
2314 if ((scsi_id->login_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2315 (scsi_id->query_logins_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2316 (scsi_id->reconnect_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2317 (scsi_id->logout_orb_dma == scsi_id->status_block.ORB_offset_lo)) {
2318 atomic_set(&scsi_id->sbp2_login_complete, 1);
2324 /* Complete the SCSI command. */
2325 SBP2_DEBUG("Completing SCSI command");
2326 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
2327 command->Current_done);
2328 SBP2_ORB_DEBUG("command orb completed");
2331 return(RCODE_COMPLETE);
2335 /**************************************
2336 * SCSI interface related section
2337 **************************************/
2340 * This routine is the main request entry routine for doing I/O. It is
2341 * called from the scsi stack directly.
2343 static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
2344 void (*done)(struct scsi_cmnd *))
2346 struct scsi_id_instance_data *scsi_id =
2347 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2348 struct sbp2scsi_host_info *hi;
2350 SBP2_DEBUG("sbp2scsi_queuecommand");
2353 * If scsi_id is null, it means there is no device in this slot,
2354 * so we should return selection timeout.
2357 SCpnt->result = DID_NO_CONNECT << 16;
2365 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
2366 SCpnt->result = DID_NO_CONNECT << 16;
2372 * Until we handle multiple luns, just return selection time-out
2373 * to any IO directed at non-zero LUNs
2375 if (SCpnt->device->lun) {
2376 SCpnt->result = DID_NO_CONNECT << 16;
2382 * Check for request sense command, and handle it here
2383 * (autorequest sense)
2385 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
2386 SBP2_DEBUG("REQUEST_SENSE");
2387 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
2388 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2389 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
2394 * Check to see if we are in the middle of a bus reset.
2396 if (!hpsb_node_entry_valid(scsi_id->ne)) {
2397 SBP2_ERR("Bus reset in progress - rejecting command");
2398 SCpnt->result = DID_BUS_BUSY << 16;
2404 * Try and send our SCSI command
2406 if (sbp2_send_command(scsi_id, SCpnt, done)) {
2407 SBP2_ERR("Error sending SCSI command");
2408 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
2416 * This function is called in order to complete all outstanding SBP-2
2417 * commands (in case of resets, etc.).
2419 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
2422 struct sbp2scsi_host_info *hi = scsi_id->hi;
2423 struct list_head *lh;
2424 struct sbp2_command_info *command;
2426 SBP2_DEBUG("sbp2scsi_complete_all_commands");
2428 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2429 SBP2_DEBUG("Found pending command to complete");
2430 lh = scsi_id->sbp2_command_orb_inuse.next;
2431 command = list_entry(lh, struct sbp2_command_info, list);
2432 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2433 sizeof(struct sbp2_command_orb),
2434 PCI_DMA_BIDIRECTIONAL);
2435 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2436 sizeof(command->scatter_gather_element),
2437 PCI_DMA_BIDIRECTIONAL);
2438 sbp2util_mark_command_completed(scsi_id, command);
2439 if (command->Current_SCpnt) {
2440 command->Current_SCpnt->result = status << 16;
2441 command->Current_done(command->Current_SCpnt);
2449 * This function is called in order to complete a regular SBP-2 command.
2451 * This can be called in interrupt context.
2453 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2454 u32 scsi_status, struct scsi_cmnd *SCpnt,
2455 void (*done)(struct scsi_cmnd *))
2457 SBP2_DEBUG("sbp2scsi_complete_command");
2463 SBP2_ERR("SCpnt is NULL");
2468 * If a bus reset is in progress and there was an error, don't
2469 * complete the command, just let it get retried at the end of the
2472 if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2473 SBP2_ERR("Bus reset in progress - retry command later");
2478 * Switch on scsi status
2480 switch (scsi_status) {
2481 case SBP2_SCSI_STATUS_GOOD:
2482 SCpnt->result = DID_OK;
2485 case SBP2_SCSI_STATUS_BUSY:
2486 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2487 SCpnt->result = DID_BUS_BUSY << 16;
2490 case SBP2_SCSI_STATUS_CHECK_CONDITION:
2491 SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
2492 SCpnt->result = CHECK_CONDITION << 1;
2497 #if CONFIG_IEEE1394_SBP2_DEBUG >= 1
2498 scsi_print_command(SCpnt);
2499 scsi_print_sense("bh", SCpnt);
2504 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2505 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2506 SCpnt->result = DID_NO_CONNECT << 16;
2507 scsi_print_command(SCpnt);
2510 case SBP2_SCSI_STATUS_CONDITION_MET:
2511 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2512 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2513 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2514 SCpnt->result = DID_ERROR << 16;
2515 scsi_print_command(SCpnt);
2519 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2520 SCpnt->result = DID_ERROR << 16;
2524 * Take care of any sbp2 response data mucking here (RBC stuff, etc.)
2526 if (SCpnt->result == DID_OK) {
2527 sbp2_check_sbp2_response(scsi_id, SCpnt);
2531 * If a bus reset is in progress and there was an error, complete
2532 * the command as busy so that it will get retried.
2534 if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2535 SBP2_ERR("Completing command with busy (bus reset)");
2536 SCpnt->result = DID_BUS_BUSY << 16;
2540 * If a unit attention occurs, return busy status so it gets
2541 * retried... it could have happened because of a 1394 bus reset
2545 if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
2546 (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
2547 SBP2_DEBUG("UNIT ATTENTION - return busy");
2548 SCpnt->result = DID_BUS_BUSY << 16;
2553 * Tell scsi stack that we're done with this command
2559 static int sbp2scsi_slave_configure (struct scsi_device *sdev)
2561 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
2562 sdev->use_10_for_rw = 1;
2563 sdev->use_10_for_ms = 1;
2569 * Called by scsi stack when something has really gone wrong. Usually
2570 * called when a command has timed-out for some reason.
2572 static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2574 struct scsi_id_instance_data *scsi_id =
2575 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2576 struct sbp2scsi_host_info *hi = scsi_id->hi;
2577 struct sbp2_command_info *command;
2579 SBP2_ERR("aborting sbp2 command");
2580 scsi_print_command(SCpnt);
2585 * Right now, just return any matching command structures
2588 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2590 SBP2_DEBUG("Found command to abort");
2591 pci_dma_sync_single_for_cpu(hi->host->pdev,
2592 command->command_orb_dma,
2593 sizeof(struct sbp2_command_orb),
2594 PCI_DMA_BIDIRECTIONAL);
2595 pci_dma_sync_single_for_cpu(hi->host->pdev,
2597 sizeof(command->scatter_gather_element),
2598 PCI_DMA_BIDIRECTIONAL);
2599 sbp2util_mark_command_completed(scsi_id, command);
2600 if (command->Current_SCpnt) {
2601 command->Current_SCpnt->result = DID_ABORT << 16;
2602 command->Current_done(command->Current_SCpnt);
2607 * Initiate a fetch agent reset.
2609 sbp2_agent_reset(scsi_id, 0);
2610 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2617 * Called by scsi stack when something has really gone wrong.
2619 static int __sbp2scsi_reset(struct scsi_cmnd *SCpnt)
2621 struct scsi_id_instance_data *scsi_id =
2622 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2624 SBP2_ERR("reset requested");
2627 SBP2_ERR("Generating sbp2 fetch agent reset");
2628 sbp2_agent_reset(scsi_id, 0);
2634 static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
2636 unsigned long flags;
2639 spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
2640 rc = __sbp2scsi_reset(SCpnt);
2641 spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
2646 static const char *sbp2scsi_info (struct Scsi_Host *host)
2648 return "SCSI emulation for IEEE-1394 SBP-2 Devices";
2651 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr, char *buf)
2653 struct scsi_device *sdev;
2654 struct scsi_id_instance_data *scsi_id;
2657 if (!(sdev = to_scsi_device(dev)))
2660 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2663 if (scsi_id->sbp2_device_type_and_lun == SBP2_DEVICE_TYPE_LUN_UNINITIALIZED)
2666 lun = ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
2668 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
2669 scsi_id->ud->id, lun);
2671 static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
2673 static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
2674 &dev_attr_ieee1394_id,
2678 MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2679 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2680 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2681 MODULE_LICENSE("GPL");
2683 /* SCSI host template */
2684 static struct scsi_host_template scsi_driver_template = {
2685 .module = THIS_MODULE,
2686 .name = "SBP-2 IEEE-1394",
2687 .proc_name = SBP2_DEVICE_NAME,
2688 .info = sbp2scsi_info,
2689 .queuecommand = sbp2scsi_queuecommand,
2690 .eh_abort_handler = sbp2scsi_abort,
2691 .eh_device_reset_handler = sbp2scsi_reset,
2692 .eh_bus_reset_handler = sbp2scsi_reset,
2693 .eh_host_reset_handler = sbp2scsi_reset,
2694 .slave_configure = sbp2scsi_slave_configure,
2696 .sg_tablesize = SG_ALL,
2697 .use_clustering = ENABLE_CLUSTERING,
2698 .cmd_per_lun = SBP2_MAX_CMDS,
2699 .can_queue = SBP2_MAX_CMDS,
2701 .sdev_attrs = sbp2_sysfs_sdev_attrs,
2704 static int sbp2_module_init(void)
2708 SBP2_DEBUG("sbp2_module_init");
2710 printk(KERN_INFO "sbp2: %s\n", version);
2712 /* Module load debug option to force one command at a time (serializing I/O) */
2714 SBP2_ERR("Driver forced to serialize I/O (serialize_io = 1)");
2715 scsi_driver_template.can_queue = 1;
2716 scsi_driver_template.cmd_per_lun = 1;
2719 /* Set max sectors (module load option). Default is 255 sectors. */
2720 scsi_driver_template.max_sectors = max_sectors;
2723 /* Register our high level driver with 1394 stack */
2724 hpsb_register_highlevel(&sbp2_highlevel);
2726 ret = hpsb_register_protocol(&sbp2_driver);
2728 SBP2_ERR("Failed to register protocol");
2729 hpsb_unregister_highlevel(&sbp2_highlevel);
2736 static void __exit sbp2_module_exit(void)
2738 SBP2_DEBUG("sbp2_module_exit");
2740 hpsb_unregister_protocol(&sbp2_driver);
2742 hpsb_unregister_highlevel(&sbp2_highlevel);
2745 module_init(sbp2_module_init);
2746 module_exit(sbp2_module_exit);