Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / drivers / ieee1394 / sbp2.c
blob3fd94842cfb981caea94191b5dac90e7f71f5018
1 /*
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.
25 * Brief Description:
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.).
35 * Current Issues:
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>
47 #include <linux/fs.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/proc_fs.h>
55 #include <linux/blk.h>
56 #include <linux/smp_lock.h>
57 #include <linux/init.h>
58 #include <linux/version.h>
59 #include <asm/current.h>
60 #include <asm/uaccess.h>
61 #include <asm/io.h>
62 #include <asm/byteorder.h>
63 #include <asm/atomic.h>
64 #include <asm/system.h>
65 #include <asm/io.h>
66 #include <asm/scatterlist.h>
68 #include "../scsi/scsi.h"
69 #include "../scsi/hosts.h"
71 #include "ieee1394.h"
72 #include "ieee1394_types.h"
73 #include "ieee1394_core.h"
74 #include "nodemgr.h"
75 #include "hosts.h"
76 #include "nodemgr.h"
77 #include "highlevel.h"
78 #include "ieee1394_transactions.h"
79 #include "sbp2.h"
81 static char version[] __devinitdata =
82 "$Rev: 967 $ Ben Collins <bcollins@debian.org>";
85 * Module load parameter definitions
89 * Change max_speed on module load if you have a bad IEEE-1394
90 * controller that has trouble running 2KB packets at 400mb.
92 * NOTE: On certain OHCI parts I have seen short packets on async transmit
93 * (probably due to PCI latency/throughput issues with the part). You can
94 * bump down the speed if you are running into problems.
96 static int max_speed = IEEE1394_SPEED_MAX;
97 module_param(max_speed, int, 0644);
98 MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb default, 1 = 200mb, 0 = 100mb)");
101 * Set serialize_io to 1 if you'd like only one scsi command sent
102 * down to us at a time (debugging). This might be necessary for very
103 * badly behaved sbp2 devices.
105 static int serialize_io = 0;
106 module_param(serialize_io, int, 0444);
107 MODULE_PARM_DESC(serialize_io, "Serialize all I/O coming down from the scsi drivers (default = 0)");
110 * Bump up max_sectors if you'd like to support very large sized
111 * transfers. Please note that some older sbp2 bridge chips are broken for
112 * transfers greater or equal to 128KB. Default is a value of 255
113 * sectors, or just under 128KB (at 512 byte sector size). I can note that
114 * the Oxsemi sbp2 chipsets have no problems supporting very large
115 * transfer sizes.
117 static int max_sectors = SBP2_MAX_SECTORS;
118 module_param(max_sectors, int, 0444);
119 MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = 255)");
122 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
123 * do an exclusive login, as it's generally unsafe to have two hosts
124 * talking to a single sbp2 device at the same time (filesystem coherency,
125 * etc.). If you're running an sbp2 device that supports multiple logins,
126 * and you're either running read-only filesystems or some sort of special
127 * filesystem supporting multiple hosts (one such filesystem is OpenGFS,
128 * see opengfs.sourceforge.net for more info), then set exclusive_login
129 * to zero. Note: The Oxsemi OXFW911 sbp2 chipset supports up to four
130 * concurrent logins.
132 static int exclusive_login = 1;
133 module_param(exclusive_login, int, 0644);
134 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
137 * SCSI inquiry hack for really badly behaved sbp2 devices. Turn this on
138 * if your sbp2 device is not properly handling the SCSI inquiry command.
139 * This hack makes the inquiry look more like a typical MS Windows
140 * inquiry.
142 * If force_inquiry_hack=1 is required for your device to work,
143 * please submit the logged sbp2_firmware_revision value of this device to
144 * the linux1394-devel mailing list.
146 static int force_inquiry_hack = 0;
147 module_param(force_inquiry_hack, int, 0444);
148 MODULE_PARM_DESC(force_inquiry_hack, "Force SCSI inquiry hack (default = 0)");
152 * Export information about protocols/devices supported by this driver.
154 static struct ieee1394_device_id sbp2_id_table[] = {
156 .match_flags =IEEE1394_MATCH_SPECIFIER_ID |
157 IEEE1394_MATCH_VERSION,
158 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
159 .version = SBP2_SW_VERSION_ENTRY & 0xffffff
164 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
167 * Debug levels, configured via kernel config, or enable here.
170 /* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
171 /* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
172 /* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
173 /* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
174 /* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
176 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
177 #define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
178 static u32 global_outstanding_command_orbs = 0;
179 #define outstanding_orb_incr global_outstanding_command_orbs++
180 #define outstanding_orb_decr global_outstanding_command_orbs--
181 #else
182 #define SBP2_ORB_DEBUG(fmt, args...)
183 #define outstanding_orb_incr
184 #define outstanding_orb_decr
185 #endif
187 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
188 #define SBP2_DMA_ALLOC(fmt, args...) \
189 HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
190 ++global_outstanding_dmas, ## args)
191 #define SBP2_DMA_FREE(fmt, args...) \
192 HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
193 --global_outstanding_dmas, ## args)
194 static u32 global_outstanding_dmas = 0;
195 #else
196 #define SBP2_DMA_ALLOC(fmt, args...)
197 #define SBP2_DMA_FREE(fmt, args...)
198 #endif
200 #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
201 #define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
202 #define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
203 #define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
204 #define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
205 #elif CONFIG_IEEE1394_SBP2_DEBUG == 1
206 #define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
207 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
208 #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
209 #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
210 #else
211 #define SBP2_DEBUG(fmt, args...)
212 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
213 #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
214 #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
215 #endif
217 #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
221 * Globals
224 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
225 u32 status);
227 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
228 u32 scsi_status, Scsi_Cmnd *SCpnt,
229 void (*done)(Scsi_Cmnd *));
231 static Scsi_Host_Template scsi_driver_template;
233 const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
235 static struct hpsb_highlevel sbp2_highlevel = {
236 .name = SBP2_DEVICE_NAME,
237 .remove_host = sbp2_remove_host,
240 static struct hpsb_address_ops sbp2_ops = {
241 .write = sbp2_handle_status_write
244 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
245 static struct hpsb_address_ops sbp2_physdma_ops = {
246 .read = sbp2_handle_physdma_read,
247 .write = sbp2_handle_physdma_write,
249 #endif
251 static struct hpsb_protocol_driver sbp2_driver = {
252 .name = "SBP2 Driver",
253 .id_table = sbp2_id_table,
254 .update = sbp2_update,
255 .driver = {
256 .name = SBP2_DEVICE_NAME,
257 .bus = &ieee1394_bus_type,
258 .probe = sbp2_probe,
259 .remove = sbp2_remove,
264 /* List of device firmware's that require a forced 36 byte inquiry. */
265 static u32 sbp2_broken_inquiry_list[] = {
266 0x00002800, /* Stefan Richter <richtest@bauwesen.tu-cottbus.de> */
267 /* DViCO Momobay CX-1 */
268 0x00000200 /* Andreas Plesch <plesch@fas.harvard.edu> */
269 /* QPS Fire DVDBurner */
272 #define NUM_BROKEN_INQUIRY_DEVS \
273 (sizeof(sbp2_broken_inquiry_list)/sizeof(*sbp2_broken_inquiry_list))
275 /**************************************
276 * General utility functions
277 **************************************/
280 #ifndef __BIG_ENDIAN
282 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
284 static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
286 u32 *temp = buffer;
288 for (length = (length >> 2); length--; )
289 temp[length] = be32_to_cpu(temp[length]);
291 return;
295 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
297 static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
299 u32 *temp = buffer;
301 for (length = (length >> 2); length--; )
302 temp[length] = cpu_to_be32(temp[length]);
304 return;
306 #else /* BIG_ENDIAN */
307 /* Why waste the cpu cycles? */
308 #define sbp2util_be32_to_cpu_buffer(x,y)
309 #define sbp2util_cpu_to_be32_buffer(x,y)
310 #endif
312 #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
314 * Debug packet dump routine. Length is in bytes.
316 static void sbp2util_packet_dump(void *buffer, int length, char *dump_name, u32 dump_phys_addr)
318 int i;
319 unsigned char *dump = buffer;
321 if (!dump || !length || !dump_name)
322 return;
324 if (dump_phys_addr)
325 printk("[%s, 0x%x]", dump_name, dump_phys_addr);
326 else
327 printk("[%s]", dump_name);
328 for (i = 0; i < length; i++) {
329 if (i > 0x3f) {
330 printk("\n ...");
331 break;
333 if ((i & 0x3) == 0)
334 printk(" ");
335 if ((i & 0xf) == 0)
336 printk("\n ");
337 printk("%02x ", (int) dump[i]);
339 printk("\n");
341 return;
343 #else
344 #define sbp2util_packet_dump(w,x,y,z)
345 #endif
348 * Goofy routine that basically does a down_timeout function.
350 static int sbp2util_down_timeout(atomic_t *done, int timeout)
352 int i;
354 for (i = timeout; (i > 0 && atomic_read(done) == 0); i-= HZ/10) {
355 set_current_state(TASK_INTERRUPTIBLE);
356 if (schedule_timeout(HZ/10)) /* 100ms */
357 return(1);
359 return ((i > 0) ? 0:1);
362 /* Free's an allocated packet */
363 static void sbp2_free_packet(struct hpsb_packet *packet)
365 hpsb_free_tlabel(packet);
366 free_hpsb_packet(packet);
370 * This function is called to retrieve a block write packet from our
371 * packet pool. This function is used in place of calling
372 * alloc_hpsb_packet (which costs us three kmallocs). Instead we just pull
373 * out a free request packet and re-initialize values in it. I'm sure this
374 * can still stand some more optimization.
376 static struct hpsb_packet *
377 sbp2util_allocate_write_packet(struct sbp2scsi_host_info *hi,
378 struct node_entry *ne, u64 addr,
379 size_t data_size,
380 quadlet_t *data)
382 struct hpsb_packet *packet;
384 packet = hpsb_make_writepacket(hi->host, ne->nodeid,
385 addr, data, data_size);
387 if (!packet)
388 return NULL;
390 hpsb_set_packet_complete_task(packet, (void (*)(void*))sbp2_free_packet,
391 packet);
393 hpsb_node_fill_packet(ne, packet);
395 return packet;
400 * This function is called to create a pool of command orbs used for
401 * command processing. It is called when a new sbp2 device is detected.
403 static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
405 struct sbp2scsi_host_info *hi = scsi_id->hi;
406 int i;
407 unsigned long flags, orbs;
408 struct sbp2_command_info *command;
410 orbs = serialize_io ? 2 : SBP2_MAX_COMMAND_ORBS;
412 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
413 for (i = 0; i < orbs; i++) {
414 command = (struct sbp2_command_info *)
415 kmalloc(sizeof(struct sbp2_command_info), GFP_ATOMIC);
416 if (!command) {
417 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
418 return(-ENOMEM);
420 memset(command, '\0', sizeof(struct sbp2_command_info));
421 command->command_orb_dma =
422 pci_map_single (hi->host->pdev, &command->command_orb,
423 sizeof(struct sbp2_command_orb),
424 PCI_DMA_BIDIRECTIONAL);
425 SBP2_DMA_ALLOC("single command orb DMA");
426 command->sge_dma =
427 pci_map_single (hi->host->pdev, &command->scatter_gather_element,
428 sizeof(command->scatter_gather_element),
429 PCI_DMA_BIDIRECTIONAL);
430 SBP2_DMA_ALLOC("scatter_gather_element");
431 INIT_LIST_HEAD(&command->list);
432 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
434 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
435 return 0;
439 * This function is called to delete a pool of command orbs.
441 static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
443 struct hpsb_host *host = scsi_id->hi->host;
444 struct list_head *lh, *next;
445 struct sbp2_command_info *command;
446 unsigned long flags;
448 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
449 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
450 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
451 command = list_entry(lh, struct sbp2_command_info, list);
453 /* Release our generic DMA's */
454 pci_unmap_single(host->pdev, command->command_orb_dma,
455 sizeof(struct sbp2_command_orb),
456 PCI_DMA_BIDIRECTIONAL);
457 SBP2_DMA_FREE("single command orb DMA");
458 pci_unmap_single(host->pdev, command->sge_dma,
459 sizeof(command->scatter_gather_element),
460 PCI_DMA_BIDIRECTIONAL);
461 SBP2_DMA_FREE("scatter_gather_element");
463 kfree(command);
466 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
467 return;
471 * This function finds the sbp2_command for a given outstanding command
472 * orb.Only looks at the inuse list.
474 static struct sbp2_command_info *sbp2util_find_command_for_orb(
475 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
477 struct list_head *lh;
478 struct sbp2_command_info *command;
479 unsigned long flags;
481 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
482 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
483 list_for_each(lh, &scsi_id->sbp2_command_orb_inuse) {
484 command = list_entry(lh, struct sbp2_command_info, list);
485 if (command->command_orb_dma == orb) {
486 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
487 return (command);
491 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
493 SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
495 return(NULL);
499 * This function finds the sbp2_command for a given outstanding SCpnt.
500 * Only looks at the inuse list.
502 static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(struct scsi_id_instance_data *scsi_id, void *SCpnt)
504 struct list_head *lh;
505 struct sbp2_command_info *command;
506 unsigned long flags;
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(lh, &scsi_id->sbp2_command_orb_inuse) {
511 command = list_entry(lh, struct sbp2_command_info, list);
512 if (command->Current_SCpnt == SCpnt) {
513 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
514 return (command);
518 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
519 return(NULL);
523 * This function allocates a command orb used to send a scsi command.
525 static struct sbp2_command_info *sbp2util_allocate_command_orb(
526 struct scsi_id_instance_data *scsi_id,
527 Scsi_Cmnd *Current_SCpnt,
528 void (*Current_done)(Scsi_Cmnd *))
530 struct list_head *lh;
531 struct sbp2_command_info *command = NULL;
532 unsigned long flags;
534 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
535 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
536 lh = scsi_id->sbp2_command_orb_completed.next;
537 list_del(lh);
538 command = list_entry(lh, struct sbp2_command_info, list);
539 command->Current_done = Current_done;
540 command->Current_SCpnt = Current_SCpnt;
541 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
542 } else {
543 SBP2_ERR("sbp2util_allocate_command_orb - No orbs available!");
545 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
546 return (command);
549 /* Free our DMA's */
550 static void sbp2util_free_command_dma(struct sbp2_command_info *command)
552 struct hpsb_host *host;
554 host = hpsb_get_host_bykey(&sbp2_highlevel,
555 (unsigned long)command->Current_SCpnt->device->host);
556 if (!host) {
557 printk(KERN_ERR "%s: host == NULL\n", __FUNCTION__);
558 return;
561 if (command->cmd_dma) {
562 if (command->dma_type == CMD_DMA_SINGLE) {
563 pci_unmap_single(host->pdev, command->cmd_dma,
564 command->dma_size, command->dma_dir);
565 SBP2_DMA_FREE("single bulk");
566 } else if (command->dma_type == CMD_DMA_PAGE) {
567 pci_unmap_page(host->pdev, command->cmd_dma,
568 command->dma_size, command->dma_dir);
569 SBP2_DMA_FREE("single page");
570 } /* XXX: Check for CMD_DMA_NONE bug */
571 command->dma_type = CMD_DMA_NONE;
572 command->cmd_dma = 0;
575 if (command->sge_buffer) {
576 pci_unmap_sg(host->pdev, command->sge_buffer,
577 command->dma_size, command->dma_dir);
578 SBP2_DMA_FREE("scatter list");
579 command->sge_buffer = NULL;
584 * This function moves a command to the completed orb list.
586 static void sbp2util_mark_command_completed(struct scsi_id_instance_data *scsi_id, struct sbp2_command_info *command)
588 unsigned long flags;
590 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
591 list_del(&command->list);
592 sbp2util_free_command_dma(command);
593 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
594 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
599 /*********************************************
600 * IEEE-1394 core driver stack related section
601 *********************************************/
603 static int sbp2_probe(struct device *dev)
605 struct unit_directory *ud;
606 struct sbp2scsi_host_info *hi;
608 SBP2_DEBUG(__FUNCTION__);
610 ud = container_of(dev, struct unit_directory, device);
612 /* This will only add it if it doesn't exist */
613 hi = sbp2_add_host(ud->ne->host);
615 if (!hi)
616 return -ENODEV;
618 return sbp2_start_ud(hi, ud);
621 static int sbp2_remove(struct device *dev)
623 struct scsi_id_group *scsi_group;
624 struct list_head *lh, *next;
625 struct unit_directory *ud;
626 struct scsi_id_instance_data *scsi_id;
628 SBP2_DEBUG(__FUNCTION__);
630 ud = container_of(dev, struct unit_directory, device);
631 scsi_group = ud->device.driver_data;
632 ud->device.driver_data = NULL;
634 list_for_each_safe (lh, next, &scsi_group->scsi_id_list) {
635 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
637 if (scsi_id != NULL) {
638 sbp2_logout_device(scsi_id);
639 sbp2_remove_device(scsi_id);
643 kfree(scsi_group);
645 return 0;
648 static void sbp2_update(struct unit_directory *ud)
650 struct sbp2scsi_host_info *hi;
651 struct scsi_id_group *scsi_group = ud->device.driver_data;
652 struct list_head *lh, *next;
653 struct scsi_id_instance_data *scsi_id;
654 unsigned long flags;
656 SBP2_DEBUG("sbp2_update");
658 list_for_each_safe (lh, next, &scsi_group->scsi_id_list) {
659 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
661 hi = scsi_id->hi;
663 if (sbp2_reconnect_device(scsi_id)) {
666 * Ok, reconnect has failed. Perhaps we didn't
667 * reconnect fast enough. Try doing a regular login.
669 if (sbp2_login_device(scsi_id)) {
670 /* Login failed too, just remove the device. */
671 SBP2_ERR("sbp2_reconnect_device failed!");
672 sbp2_remove_device(scsi_id);
673 return;
677 /* Set max retries to something large on the device. */
678 sbp2_set_busy_timeout(scsi_id);
680 /* Do a SBP-2 fetch agent reset. */
681 sbp2_agent_reset(scsi_id, 1);
683 /* Get the max speed and packet size that we can use. */
684 sbp2_max_speed_and_size(scsi_id);
686 /* Complete any pending commands with busy (so they get
687 * retried) and remove them from our queue
689 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
690 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
691 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
695 /* This functions is called by the sbp2_probe, for each new device. If the
696 * host_info already exists, it will return it. If not, it allocated a new
697 * host_info entry and a corresponding scsi_host. */
698 static struct sbp2scsi_host_info *sbp2_add_host(struct hpsb_host *host)
700 struct sbp2scsi_host_info *hi;
701 struct Scsi_Host *scsi_host;
703 SBP2_DEBUG("sbp2_add_host");
705 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
706 if (hi)
707 return hi;
709 /* Register our host with the SCSI stack. */
710 scsi_host = scsi_host_alloc(&scsi_driver_template, 0);
711 if (!scsi_host) {
712 SBP2_ERR("failed to register scsi host");
713 return NULL;
716 hi = hpsb_create_hostinfo(&sbp2_highlevel, host, sizeof(*hi));
717 if (!hi) {
718 SBP2_ERR("failed to allocate hostinfo");
719 scsi_host_put(hi->scsi_host);
722 hpsb_set_hostinfo_key(&sbp2_highlevel, host, (unsigned long)scsi_host);
724 hi->scsi_host = scsi_host;
725 hi->host = host;
726 hi->sbp2_command_lock = SPIN_LOCK_UNLOCKED;
727 hi->scsi_host->max_id = SBP2SCSI_MAX_SCSI_IDS;
729 /* XXX We need a device to pass here as the scsi-host class. Can't
730 * use the PCI device, since it is already bound to the ieee1394
731 * host. Can't use the fw-host device since it is multi-class
732 * enabled (scsi-host uses classdata member of the device). */
733 if (scsi_add_host(hi->scsi_host, NULL)) {
734 SBP2_ERR("failed to add scsi host");
735 scsi_host_put(hi->scsi_host);
736 hpsb_destroy_hostinfo(&sbp2_highlevel, host);
739 return hi;
744 * This function is called when a host is removed.
746 static void sbp2_remove_host(struct hpsb_host *host)
748 struct sbp2scsi_host_info *hi;
750 SBP2_DEBUG("sbp2_remove_host");
752 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
754 if (hi) {
755 scsi_remove_host(hi->scsi_host);
756 scsi_host_put(hi->scsi_host);
760 static int sbp2_start_ud(struct sbp2scsi_host_info *hi, struct unit_directory *ud)
762 struct scsi_id_instance_data *scsi_id;
763 struct scsi_id_group *scsi_group;
764 struct list_head *lh, *next;
766 SBP2_DEBUG("sbp2_start_ud");
768 scsi_group = kmalloc(sizeof(*scsi_group), GFP_KERNEL);
769 if (!scsi_group) {
770 SBP2_ERR ("Could not allocate memory for scsi_group");
771 return -ENOMEM;
774 INIT_LIST_HEAD(&scsi_group->scsi_id_list);
775 ud->device.driver_data = scsi_group;
776 sbp2_parse_unit_directory(scsi_group, ud);
778 list_for_each_safe (lh, next, &scsi_group->scsi_id_list) {
779 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
781 scsi_id->ne = ud->ne;
782 scsi_id->hi = hi;
783 scsi_id->speed_code = IEEE1394_SPEED_100;
784 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
785 atomic_set(&scsi_id->sbp2_login_complete, 0);
786 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
787 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
788 scsi_id->sbp2_command_orb_lock = SPIN_LOCK_UNLOCKED;
790 sbp2_start_device(scsi_id);
793 /* Check to see if any of our devices survived the ordeal */
794 if (list_empty(&scsi_group->scsi_id_list)) {
795 kfree(scsi_group);
796 return -ENODEV;
799 return 0;
804 * This function is where we first pull the node unique ids, and then
805 * allocate memory and register a SBP-2 device.
807 static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
809 struct sbp2scsi_host_info *hi = scsi_id->hi;
810 struct scsi_device *sdev;
811 int i;
813 SBP2_DEBUG("sbp2_start_device");
815 /* Login FIFO DMA */
816 scsi_id->login_response =
817 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_response),
818 &scsi_id->login_response_dma);
819 if (!scsi_id->login_response)
820 goto alloc_fail;
821 SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
823 /* Query logins ORB DMA */
824 scsi_id->query_logins_orb =
825 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_query_logins_orb),
826 &scsi_id->query_logins_orb_dma);
827 if (!scsi_id->query_logins_orb)
828 goto alloc_fail;
829 SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
831 /* Query logins response DMA */
832 scsi_id->query_logins_response =
833 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_query_logins_response),
834 &scsi_id->query_logins_response_dma);
835 if (!scsi_id->query_logins_response)
836 goto alloc_fail;
837 SBP2_DMA_ALLOC("consistent DMA region for query logins response");
839 /* Reconnect ORB DMA */
840 scsi_id->reconnect_orb =
841 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_reconnect_orb),
842 &scsi_id->reconnect_orb_dma);
843 if (!scsi_id->reconnect_orb)
844 goto alloc_fail;
845 SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
847 /* Logout ORB DMA */
848 scsi_id->logout_orb =
849 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_logout_orb),
850 &scsi_id->logout_orb_dma);
851 if (!scsi_id->logout_orb)
852 goto alloc_fail;
853 SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
855 /* Login ORB DMA */
856 scsi_id->login_orb =
857 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_orb),
858 &scsi_id->login_orb_dma);
859 if (!scsi_id->login_orb) {
860 alloc_fail:
861 if (scsi_id->query_logins_response) {
862 pci_free_consistent(hi->host->pdev,
863 sizeof(struct sbp2_query_logins_response),
864 scsi_id->query_logins_response,
865 scsi_id->query_logins_response_dma);
866 SBP2_DMA_FREE("query logins response DMA");
869 if (scsi_id->query_logins_orb) {
870 pci_free_consistent(hi->host->pdev,
871 sizeof(struct sbp2_query_logins_orb),
872 scsi_id->query_logins_orb,
873 scsi_id->query_logins_orb_dma);
874 SBP2_DMA_FREE("query logins ORB DMA");
877 if (scsi_id->logout_orb) {
878 pci_free_consistent(hi->host->pdev,
879 sizeof(struct sbp2_logout_orb),
880 scsi_id->logout_orb,
881 scsi_id->logout_orb_dma);
882 SBP2_DMA_FREE("logout ORB DMA");
885 if (scsi_id->reconnect_orb) {
886 pci_free_consistent(hi->host->pdev,
887 sizeof(struct sbp2_reconnect_orb),
888 scsi_id->reconnect_orb,
889 scsi_id->reconnect_orb_dma);
890 SBP2_DMA_FREE("reconnect ORB DMA");
893 if (scsi_id->login_response) {
894 pci_free_consistent(hi->host->pdev,
895 sizeof(struct sbp2_login_response),
896 scsi_id->login_response,
897 scsi_id->login_response_dma);
898 SBP2_DMA_FREE("login FIFO DMA");
901 kfree(scsi_id);
903 list_del(&scsi_id->list);
905 SBP2_ERR ("Could not allocate memory for scsi_id");
907 return -ENOMEM;
909 SBP2_DMA_ALLOC("consistent DMA region for login ORB");
912 * Find an empty spot to stick our scsi id instance data.
914 for (i = 0; i < hi->scsi_host->max_id; i++) {
915 if (!hi->scsi_id[i]) {
916 hi->scsi_id[i] = scsi_id;
917 scsi_id->id = i;
918 SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", (unsigned int) i);
919 break;
924 * Create our command orb pool
926 if (sbp2util_create_command_orb_pool(scsi_id)) {
927 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
928 sbp2_remove_device(scsi_id);
929 return -ENOMEM;
933 * Make sure we are not out of space
935 if (i == hi->scsi_host->max_id) {
936 SBP2_ERR("No slots left for SBP-2 device");
937 sbp2_remove_device(scsi_id);
938 return -EBUSY;
942 * Login to the sbp-2 device
944 if (sbp2_login_device(scsi_id)) {
945 /* Login failed, just remove the device. */
946 sbp2_remove_device(scsi_id);
947 return -EBUSY;
951 * Set max retries to something large on the device
953 sbp2_set_busy_timeout(scsi_id);
956 * Do a SBP-2 fetch agent reset
958 sbp2_agent_reset(scsi_id, 1);
961 * Get the max speed and packet size that we can use
963 sbp2_max_speed_and_size(scsi_id);
965 /* Add this device to the scsi layer now */
966 sdev = scsi_add_device(hi->scsi_host, 0, scsi_id->id, 0);
967 if (IS_ERR(sdev)) {
968 SBP2_ERR("scsi_add_device failed");
969 return PTR_ERR(sdev);
972 return 0;
976 * This function removes an sbp2 device from the sbp2scsi_host_info struct.
978 static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
980 struct sbp2scsi_host_info *hi = scsi_id->hi;
981 struct scsi_device *sdev = scsi_find_device(hi->scsi_host, 0, scsi_id->id, 0);
983 SBP2_DEBUG("sbp2_remove_device");
985 /* Complete any pending commands with selection timeout */
986 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
988 /* Remove it from the scsi layer now */
989 if (sdev && scsi_remove_device(sdev))
990 SBP2_ERR("scsi_remove_device failed");
992 sbp2util_remove_command_orb_pool(scsi_id);
994 hi->scsi_id[scsi_id->id] = NULL;
996 if (scsi_id->login_response) {
997 pci_free_consistent(hi->host->pdev,
998 sizeof(struct sbp2_login_response),
999 scsi_id->login_response,
1000 scsi_id->login_response_dma);
1001 SBP2_DMA_FREE("single login FIFO");
1004 if (scsi_id->login_orb) {
1005 pci_free_consistent(hi->host->pdev,
1006 sizeof(struct sbp2_login_orb),
1007 scsi_id->login_orb,
1008 scsi_id->login_orb_dma);
1009 SBP2_DMA_FREE("single login ORB");
1012 if (scsi_id->reconnect_orb) {
1013 pci_free_consistent(hi->host->pdev,
1014 sizeof(struct sbp2_reconnect_orb),
1015 scsi_id->reconnect_orb,
1016 scsi_id->reconnect_orb_dma);
1017 SBP2_DMA_FREE("single reconnect orb");
1020 if (scsi_id->logout_orb) {
1021 pci_free_consistent(hi->host->pdev,
1022 sizeof(struct sbp2_logout_orb),
1023 scsi_id->logout_orb,
1024 scsi_id->logout_orb_dma);
1025 SBP2_DMA_FREE("single logout orb");
1028 if (scsi_id->query_logins_orb) {
1029 pci_free_consistent(hi->host->pdev,
1030 sizeof(struct sbp2_query_logins_orb),
1031 scsi_id->query_logins_orb,
1032 scsi_id->query_logins_orb_dma);
1033 SBP2_DMA_FREE("single query logins orb");
1036 if (scsi_id->query_logins_response) {
1037 pci_free_consistent(hi->host->pdev,
1038 sizeof(struct sbp2_query_logins_response),
1039 scsi_id->query_logins_response,
1040 scsi_id->query_logins_response_dma);
1041 SBP2_DMA_FREE("single query logins data");
1044 SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->id);
1046 list_del(&scsi_id->list);
1048 kfree(scsi_id);
1051 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1053 * This function deals with physical dma write requests (for adapters that do not support
1054 * physical dma in hardware). Mostly just here for debugging...
1056 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid, int destid, quadlet_t *data,
1057 u64 addr, unsigned int length, u16 flags)
1061 * Manually put the data in the right place.
1063 memcpy(bus_to_virt((u32)addr), data, length);
1064 sbp2util_packet_dump(data, length, "sbp2 phys dma write by device", (u32)addr);
1065 return(RCODE_COMPLETE);
1069 * This function deals with physical dma read requests (for adapters that do not support
1070 * physical dma in hardware). Mostly just here for debugging...
1072 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid, quadlet_t *data,
1073 u64 addr, unsigned int length, u16 flags)
1077 * Grab data from memory and send a read response.
1079 memcpy(data, bus_to_virt((u32)addr), length);
1080 sbp2util_packet_dump(data, length, "sbp2 phys dma read by device", (u32)addr);
1081 return(RCODE_COMPLETE);
1083 #endif
1086 /**************************************
1087 * SBP-2 protocol related section
1088 **************************************/
1091 * This function determines if we should convert scsi commands for a particular sbp2 device type
1093 static __inline__ int sbp2_command_conversion_device_type(u8 device_type)
1095 return (((device_type == TYPE_DISK) ||
1096 (device_type == TYPE_SDAD) ||
1097 (device_type == TYPE_ROM)) ? 1:0);
1101 * This function queries the device for the maximum concurrent logins it
1102 * supports.
1104 static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1106 struct sbp2scsi_host_info *hi = scsi_id->hi;
1107 quadlet_t data[2];
1108 int max_logins;
1109 int active_logins;
1111 SBP2_DEBUG("sbp2_query_logins");
1113 scsi_id->query_logins_orb->reserved1 = 0x0;
1114 scsi_id->query_logins_orb->reserved2 = 0x0;
1116 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1117 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1118 SBP2_DEBUG("sbp2_query_logins: query_response_hi/lo initialized");
1120 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(QUERY_LOGINS_REQUEST);
1121 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1122 if (scsi_id->sbp2_device_type_and_lun != SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
1123 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
1124 SBP2_DEBUG("sbp2_query_logins: set lun to %d",
1125 ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun));
1127 SBP2_DEBUG("sbp2_query_logins: lun_misc initialized");
1129 scsi_id->query_logins_orb->reserved_resp_length =
1130 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
1131 SBP2_DEBUG("sbp2_query_logins: reserved_resp_length initialized");
1133 scsi_id->query_logins_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1134 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1135 scsi_id->query_logins_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1136 SBP2_STATUS_FIFO_ADDRESS_HI);
1137 SBP2_DEBUG("sbp2_query_logins: status FIFO initialized");
1139 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1141 SBP2_DEBUG("sbp2_query_logins: orb byte-swapped");
1143 sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(stuct sbp2_query_logins_orb),
1144 "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
1146 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1147 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1149 SBP2_DEBUG("sbp2_query_logins: query_logins_response/status FIFO memset");
1151 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1152 data[1] = scsi_id->query_logins_orb_dma;
1153 sbp2util_cpu_to_be32_buffer(data, 8);
1155 atomic_set(&scsi_id->sbp2_login_complete, 0);
1157 SBP2_DEBUG("sbp2_query_logins: prepared to write");
1158 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1159 SBP2_DEBUG("sbp2_query_logins: written");
1161 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 2*HZ)) {
1162 SBP2_ERR("Error querying logins to SBP-2 device - timed out");
1163 return(-EIO);
1166 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1167 SBP2_ERR("Error querying logins to SBP-2 device - timed out");
1168 return(-EIO);
1171 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1172 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1173 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1175 SBP2_ERR("Error querying logins to SBP-2 device - timed out");
1176 return(-EIO);
1179 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1181 SBP2_DEBUG("length_max_logins = %x",
1182 (unsigned int)scsi_id->query_logins_response->length_max_logins);
1184 SBP2_INFO("Query logins to SBP-2 device successful");
1186 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
1187 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1189 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
1190 SBP2_INFO("Number of active logins: %d", active_logins);
1192 if (active_logins >= max_logins) {
1193 return(-EIO);
1196 return 0;
1200 * This function is called in order to login to a particular SBP-2 device,
1201 * after a bus reset.
1203 static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1205 struct sbp2scsi_host_info *hi = scsi_id->hi;
1206 quadlet_t data[2];
1208 SBP2_DEBUG("sbp2_login_device");
1210 if (!scsi_id->login_orb) {
1211 SBP2_DEBUG("sbp2_login_device: login_orb not alloc'd!");
1212 return(-EIO);
1215 if (!exclusive_login) {
1216 if (sbp2_query_logins(scsi_id)) {
1217 SBP2_ERR("Device does not support any more concurrent logins");
1218 return(-EIO);
1222 /* Set-up login ORB, assume no password */
1223 scsi_id->login_orb->password_hi = 0;
1224 scsi_id->login_orb->password_lo = 0;
1225 SBP2_DEBUG("sbp2_login_device: password_hi/lo initialized");
1227 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1228 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1229 SBP2_DEBUG("sbp2_login_device: login_response_hi/lo initialized");
1231 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(LOGIN_REQUEST);
1232 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
1233 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
1234 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
1235 /* Set the lun if we were able to pull it from the device's unit directory */
1236 if (scsi_id->sbp2_device_type_and_lun != SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
1237 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
1238 SBP2_DEBUG("sbp2_query_logins: set lun to %d",
1239 ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun));
1241 SBP2_DEBUG("sbp2_login_device: lun_misc initialized");
1243 scsi_id->login_orb->passwd_resp_lengths =
1244 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1245 SBP2_DEBUG("sbp2_login_device: passwd_resp_lengths initialized");
1247 scsi_id->login_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1248 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1249 scsi_id->login_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1250 SBP2_STATUS_FIFO_ADDRESS_HI);
1251 SBP2_DEBUG("sbp2_login_device: status FIFO initialized");
1254 * Byte swap ORB if necessary
1256 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1258 SBP2_DEBUG("sbp2_login_device: orb byte-swapped");
1260 sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
1261 "sbp2 login orb", scsi_id->login_orb_dma);
1264 * Initialize login response and status fifo
1266 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1267 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1269 SBP2_DEBUG("sbp2_login_device: login_response/status FIFO memset");
1272 * Ok, let's write to the target's management agent register
1274 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1275 data[1] = scsi_id->login_orb_dma;
1276 sbp2util_cpu_to_be32_buffer(data, 8);
1278 atomic_set(&scsi_id->sbp2_login_complete, 0);
1280 SBP2_DEBUG("sbp2_login_device: prepared to write to %08x",
1281 (unsigned int)scsi_id->sbp2_management_agent_addr);
1282 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1283 SBP2_DEBUG("sbp2_login_device: written");
1286 * Wait for login status (up to 20 seconds)...
1288 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 20*HZ)) {
1289 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1290 return(-EIO);
1294 * Sanity. Make sure status returned matches login orb.
1296 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1297 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1298 return(-EIO);
1302 * Check status
1304 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1305 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1306 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1308 SBP2_ERR("Error logging into SBP-2 device - login failed");
1309 return(-EIO);
1313 * Byte swap the login response, for use when reconnecting or
1314 * logging out.
1316 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1319 * Grab our command block agent address from the login response.
1321 SBP2_DEBUG("command_block_agent_hi = %x",
1322 (unsigned int)scsi_id->login_response->command_block_agent_hi);
1323 SBP2_DEBUG("command_block_agent_lo = %x",
1324 (unsigned int)scsi_id->login_response->command_block_agent_lo);
1326 scsi_id->sbp2_command_block_agent_addr =
1327 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1328 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1329 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1331 SBP2_INFO("Logged into SBP-2 device");
1333 return(0);
1338 * This function is called in order to logout from a particular SBP-2
1339 * device, usually called during driver unload.
1341 static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1343 struct sbp2scsi_host_info *hi = scsi_id->hi;
1344 quadlet_t data[2];
1346 SBP2_DEBUG("sbp2_logout_device");
1349 * Set-up logout ORB
1351 scsi_id->logout_orb->reserved1 = 0x0;
1352 scsi_id->logout_orb->reserved2 = 0x0;
1353 scsi_id->logout_orb->reserved3 = 0x0;
1354 scsi_id->logout_orb->reserved4 = 0x0;
1356 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(LOGOUT_REQUEST);
1357 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1359 /* Notify us when complete */
1360 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1362 scsi_id->logout_orb->reserved5 = 0x0;
1363 scsi_id->logout_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1364 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1365 scsi_id->logout_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1366 SBP2_STATUS_FIFO_ADDRESS_HI);
1369 * Byte swap ORB if necessary
1371 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1373 sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
1374 "sbp2 logout orb", scsi_id->logout_orb_dma);
1377 * Ok, let's write to the target's management agent register
1379 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1380 data[1] = scsi_id->logout_orb_dma;
1381 sbp2util_cpu_to_be32_buffer(data, 8);
1383 atomic_set(&scsi_id->sbp2_login_complete, 0);
1385 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1387 /* Wait for device to logout...1 second. */
1388 sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ);
1390 SBP2_INFO("Logged out of SBP-2 device");
1392 return(0);
1397 * This function is called in order to reconnect to a particular SBP-2
1398 * device, after a bus reset.
1400 static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1402 struct sbp2scsi_host_info *hi = scsi_id->hi;
1403 quadlet_t data[2];
1405 SBP2_DEBUG("sbp2_reconnect_device");
1408 * Set-up reconnect ORB
1410 scsi_id->reconnect_orb->reserved1 = 0x0;
1411 scsi_id->reconnect_orb->reserved2 = 0x0;
1412 scsi_id->reconnect_orb->reserved3 = 0x0;
1413 scsi_id->reconnect_orb->reserved4 = 0x0;
1415 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(RECONNECT_REQUEST);
1416 scsi_id->reconnect_orb->login_ID_misc |=
1417 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1419 /* Notify us when complete */
1420 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1422 scsi_id->reconnect_orb->reserved5 = 0x0;
1423 scsi_id->reconnect_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1424 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1425 scsi_id->reconnect_orb->status_FIFO_hi =
1426 (ORB_SET_NODE_ID(hi->host->node_id) | SBP2_STATUS_FIFO_ADDRESS_HI);
1429 * Byte swap ORB if necessary
1431 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1433 sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
1434 "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
1437 * Initialize status fifo
1439 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1442 * Ok, let's write to the target's management agent register
1444 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1445 data[1] = scsi_id->reconnect_orb_dma;
1446 sbp2util_cpu_to_be32_buffer(data, 8);
1448 atomic_set(&scsi_id->sbp2_login_complete, 0);
1450 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1453 * Wait for reconnect status (up to 1 second)...
1455 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) {
1456 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1457 return(-EIO);
1461 * Sanity. Make sure status returned matches reconnect orb.
1463 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1464 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1465 return(-EIO);
1469 * Check status
1471 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1472 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1473 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1475 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect failed");
1476 return(-EIO);
1479 SBP2_INFO("Reconnected to SBP-2 device");
1481 return(0);
1486 * This function is called in order to set the busy timeout (number of
1487 * retries to attempt) on the sbp2 device.
1489 static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1491 quadlet_t data;
1493 SBP2_DEBUG("sbp2_set_busy_timeout");
1496 * Ok, let's write to the target's busy timeout register
1498 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1500 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4)) {
1501 SBP2_ERR("sbp2_set_busy_timeout error");
1504 return(0);
1508 * This function is called to parse sbp2 device's config rom unit
1509 * directory. Used to determine things like sbp2 management agent offset,
1510 * and command set used (SCSI or RBC).
1512 static void sbp2_parse_unit_directory(struct scsi_id_group *scsi_group,
1513 struct unit_directory *ud)
1515 struct scsi_id_instance_data *scsi_id;
1516 struct list_head *lh;
1517 u64 management_agent_addr;
1518 u32 command_set_spec_id, command_set, unit_characteristics,
1519 firmware_revision, workarounds;
1520 int i;
1522 SBP2_DEBUG("sbp2_parse_unit_directory");
1524 management_agent_addr = 0x0;
1525 command_set_spec_id = 0x0;
1526 command_set = 0x0;
1527 unit_characteristics = 0x0;
1528 firmware_revision = 0x0;
1530 /* Handle different fields in the unit directory, based on keys */
1531 for (i = 0; i < ud->length; i++) {
1532 switch (CONFIG_ROM_KEY(ud->quadlets[i])) {
1533 case SBP2_CSR_OFFSET_KEY:
1534 /* Save off the management agent address */
1535 management_agent_addr =
1536 CSR_REGISTER_BASE +
1537 (CONFIG_ROM_VALUE(ud->quadlets[i]) << 2);
1539 SBP2_DEBUG("sbp2_management_agent_addr = %x",
1540 (unsigned int) management_agent_addr);
1541 break;
1543 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1544 /* Command spec organization */
1545 command_set_spec_id
1546 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1547 SBP2_DEBUG("sbp2_command_set_spec_id = %x",
1548 (unsigned int) command_set_spec_id);
1549 break;
1551 case SBP2_COMMAND_SET_KEY:
1552 /* Command set used by sbp2 device */
1553 command_set = CONFIG_ROM_VALUE(ud->quadlets[i]);
1554 SBP2_DEBUG("sbp2_command_set = %x",
1555 (unsigned int) command_set);
1556 break;
1558 case SBP2_UNIT_CHARACTERISTICS_KEY:
1560 * Unit characterisitcs (orb related stuff
1561 * that I'm not yet paying attention to)
1563 unit_characteristics
1564 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1565 SBP2_DEBUG("sbp2_unit_characteristics = %x",
1566 (unsigned int) unit_characteristics);
1567 break;
1569 case SBP2_DEVICE_TYPE_AND_LUN_KEY:
1571 * Device type and lun (used for
1572 * detemining type of sbp2 device)
1574 scsi_id = kmalloc(sizeof(*scsi_id), GFP_KERNEL);
1575 if (!scsi_id) {
1576 SBP2_ERR("Out of memory adding scsi_id, not all LUN's will be added");
1577 break;
1579 memset(scsi_id, 0, sizeof(*scsi_id));
1581 scsi_id->sbp2_device_type_and_lun
1582 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1583 SBP2_DEBUG("sbp2_device_type_and_lun = %x",
1584 (unsigned int) scsi_id->sbp2_device_type_and_lun);
1585 list_add_tail(&scsi_id->list, &scsi_group->scsi_id_list);
1586 break;
1588 case SBP2_FIRMWARE_REVISION_KEY:
1589 /* Firmware revision */
1590 firmware_revision
1591 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1592 if (force_inquiry_hack)
1593 SBP2_INFO("sbp2_firmware_revision = %x",
1594 (unsigned int) firmware_revision);
1595 else SBP2_DEBUG("sbp2_firmware_revision = %x",
1596 (unsigned int) firmware_revision);
1597 break;
1599 default:
1600 break;
1604 /* This is the start of our broken device checking. We try to hack
1605 * around oddities and known defects. */
1606 workarounds = 0x0;
1608 /* If the vendor id is 0xa0b8 (Symbios vendor id), then we have a
1609 * bridge with 128KB max transfer size limitation. For sanity, we
1610 * only voice this when the current max_sectors setting
1611 * exceeds the 128k limit. By default, that is not the case.
1613 * It would be really nice if we could detect this before the scsi
1614 * host gets initialized. That way we can down-force the
1615 * max_sectors to account for it. That is not currently
1616 * possible. */
1617 if ((firmware_revision & 0xffff00) ==
1618 SBP2_128KB_BROKEN_FIRMWARE &&
1619 (max_sectors * 512) > (128*1024)) {
1620 SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB max transfer size.",
1621 NODE_BUS_ARGS(ud->ne->nodeid));
1622 SBP2_WARN("WARNING: Current max_sectors setting is larger than 128KB (%d sectors)!",
1623 max_sectors);
1624 workarounds |= SBP2_BREAKAGE_128K_MAX_TRANSFER;
1627 /* Check for a blacklisted set of devices that require us to force
1628 * a 36 byte host inquiry. This can be overriden as a module param
1629 * (to force all hosts). */
1630 for (i = 0; i < NUM_BROKEN_INQUIRY_DEVS; i++) {
1631 if ((firmware_revision & 0xffff00) ==
1632 sbp2_broken_inquiry_list[i]) {
1633 SBP2_WARN("Node " NODE_BUS_FMT ": Using 36byte inquiry workaround",
1634 NODE_BUS_ARGS(ud->ne->nodeid));
1635 workarounds |= SBP2_BREAKAGE_INQUIRY_HACK;
1636 break; /* No need to continue. */
1640 /* If our list is empty, add a base scsi_id (happens in a normal
1641 * case where there is no logical_unit_number entry */
1642 if (list_empty(&scsi_group->scsi_id_list)) {
1643 scsi_id = kmalloc(sizeof(*scsi_id), GFP_KERNEL);
1644 if (!scsi_id) {
1645 SBP2_ERR("Out of memory adding scsi_id");
1646 return;
1648 memset(scsi_id, 0, sizeof(*scsi_id));
1650 scsi_id->sbp2_device_type_and_lun = SBP2_DEVICE_TYPE_LUN_UNINITIALIZED;
1651 list_add_tail(&scsi_id->list, &scsi_group->scsi_id_list);
1654 /* Update the generic fields in all the LUN's */
1655 list_for_each (lh, &scsi_group->scsi_id_list) {
1656 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
1658 scsi_id->sbp2_management_agent_addr = management_agent_addr;
1659 scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
1660 scsi_id->sbp2_command_set = command_set;
1661 scsi_id->sbp2_unit_characteristics = unit_characteristics;
1662 scsi_id->sbp2_firmware_revision = firmware_revision;
1663 scsi_id->workarounds = workarounds;
1668 * This function is called in order to determine the max speed and packet
1669 * size we can use in our ORBs. Note, that we (the driver and host) only
1670 * initiate the transaction. The SBP-2 device actually transfers the data
1671 * (by reading from the DMA area we tell it). This means that the SBP-2
1672 * device decides the actual maximum data it can transfer. We just tell it
1673 * the speed that it needs to use, and the max_rec the host supports, and
1674 * it takes care of the rest.
1676 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1678 struct sbp2scsi_host_info *hi = scsi_id->hi;
1680 SBP2_DEBUG("sbp2_max_speed_and_size");
1682 /* Initial setting comes from the hosts speed map */
1683 scsi_id->speed_code = hi->host->speed_map[NODEID_TO_NODE(hi->host->node_id) * 64
1684 + NODEID_TO_NODE(scsi_id->ne->nodeid)];
1686 /* Bump down our speed if the user requested it */
1687 if (scsi_id->speed_code > max_speed) {
1688 scsi_id->speed_code = max_speed;
1689 SBP2_ERR("Forcing SBP-2 max speed down to %s",
1690 hpsb_speedto_str[scsi_id->speed_code]);
1693 /* Payload size is the lesser of what our speed supports and what
1694 * our host supports. */
1695 scsi_id->max_payload_size = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1696 (u8)(((be32_to_cpu(hi->host->csr.rom[2]) >> 12) & 0xf) - 1));
1698 SBP2_ERR("Node[" NODE_BUS_FMT "]: Max speed [%s] - Max payload [%u]",
1699 NODE_BUS_ARGS(scsi_id->ne->nodeid), hpsb_speedto_str[scsi_id->speed_code],
1700 1 << ((u32)scsi_id->max_payload_size + 2));
1702 return(0);
1706 * This function is called in order to perform a SBP-2 agent reset.
1708 static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1710 struct sbp2scsi_host_info *hi = scsi_id->hi;
1711 struct hpsb_packet *packet;
1712 quadlet_t data;
1714 SBP2_DEBUG("sbp2_agent_reset");
1717 * Ok, let's write to the target's management agent register
1719 data = ntohl(SBP2_AGENT_RESET_DATA);
1720 packet = sbp2util_allocate_write_packet(hi, scsi_id->ne,
1721 scsi_id->sbp2_command_block_agent_addr +
1722 SBP2_AGENT_RESET_OFFSET,
1723 4, &data);
1725 if (!packet) {
1726 SBP2_ERR("sbp2util_allocate_write_packet failed");
1727 return(-ENOMEM);
1730 if (!hpsb_send_packet(packet)) {
1731 SBP2_ERR("hpsb_send_packet failed");
1732 sbp2_free_packet(packet);
1733 return(-EIO);
1736 if (wait) {
1737 down(&packet->state_change);
1738 down(&packet->state_change);
1742 * Need to make sure orb pointer is written on next command
1744 scsi_id->last_orb = NULL;
1746 return(0);
1750 * This function is called to create the actual command orb and s/g list
1751 * out of the scsi command itself.
1753 static int sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1754 struct sbp2_command_info *command,
1755 unchar *scsi_cmd,
1756 unsigned int scsi_use_sg,
1757 unsigned int scsi_request_bufflen,
1758 void *scsi_request_buffer,
1759 unsigned char scsi_dir)
1761 struct sbp2scsi_host_info *hi = scsi_id->hi;
1762 struct scatterlist *sgpnt = (struct scatterlist *) scsi_request_buffer;
1763 struct sbp2_command_orb *command_orb = &command->command_orb;
1764 struct sbp2_unrestricted_page_table *scatter_gather_element =
1765 &command->scatter_gather_element[0];
1766 int dma_dir = scsi_to_pci_dma_dir (scsi_dir);
1767 u32 sg_count, sg_len, orb_direction;
1768 dma_addr_t sg_addr;
1769 int i;
1772 * Set-up our command ORB..
1774 * NOTE: We're doing unrestricted page tables (s/g), as this is
1775 * best performance (at least with the devices I have). This means
1776 * that data_size becomes the number of s/g elements, and
1777 * page_size should be zero (for unrestricted).
1779 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1780 command_orb->next_ORB_lo = 0x0;
1781 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1782 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
1783 command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
1786 * Get the direction of the transfer. If the direction is unknown, then use our
1787 * goofy table as a back-up.
1789 switch (scsi_dir) {
1790 case SCSI_DATA_NONE:
1791 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1792 break;
1793 case SCSI_DATA_WRITE:
1794 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1795 break;
1796 case SCSI_DATA_READ:
1797 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1798 break;
1799 case SCSI_DATA_UNKNOWN:
1800 default:
1801 SBP2_ERR("SCSI data transfer direction not specified. "
1802 "Update the SBP2 direction table in sbp2.h if "
1803 "necessary for your application");
1804 print_command (scsi_cmd);
1805 orb_direction = sbp2scsi_direction_table[*scsi_cmd];
1806 break;
1810 * Set-up our pagetable stuff... unfortunately, this has become
1811 * messier than I'd like. Need to clean this up a bit. ;-)
1813 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1815 SBP2_DEBUG("No data transfer");
1818 * Handle no data transfer
1820 command_orb->data_descriptor_hi = 0x0;
1821 command_orb->data_descriptor_lo = 0x0;
1822 command_orb->misc |= ORB_SET_DIRECTION(1);
1824 } else if (scsi_use_sg) {
1826 SBP2_DEBUG("Use scatter/gather");
1829 * Special case if only one element (and less than 64KB in size)
1831 if ((scsi_use_sg == 1) && (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1833 SBP2_DEBUG("Only one s/g element");
1834 command->dma_dir = dma_dir;
1835 command->dma_size = sgpnt[0].length;
1836 command->dma_type = CMD_DMA_PAGE;
1837 command->cmd_dma = pci_map_page(hi->host->pdev,
1838 sgpnt[0].page,
1839 sgpnt[0].offset,
1840 command->dma_size,
1841 command->dma_dir);
1842 SBP2_DMA_ALLOC("single page scatter element");
1844 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1845 command_orb->data_descriptor_lo = command->cmd_dma;
1846 command_orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1847 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1849 } else {
1850 int count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg, dma_dir);
1851 SBP2_DMA_ALLOC("scatter list");
1853 command->dma_size = scsi_use_sg;
1854 command->dma_dir = dma_dir;
1855 command->sge_buffer = sgpnt;
1857 /* use page tables (s/g) */
1858 command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1859 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1860 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1861 command_orb->data_descriptor_lo = command->sge_dma;
1864 * Loop through and fill out our sbp-2 page tables
1865 * (and split up anything too large)
1867 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1868 sg_len = sg_dma_len(sgpnt);
1869 sg_addr = sg_dma_address(sgpnt);
1870 while (sg_len) {
1871 scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1872 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1873 scatter_gather_element[sg_count].length_segment_base_hi =
1874 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1875 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1876 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1877 } else {
1878 scatter_gather_element[sg_count].length_segment_base_hi =
1879 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1880 sg_len = 0;
1882 sg_count++;
1886 /* Number of page table (s/g) elements */
1887 command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1889 sbp2util_packet_dump(scatter_gather_element,
1890 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1891 "sbp2 s/g list", command->sge_dma);
1894 * Byte swap page tables if necessary
1896 sbp2util_cpu_to_be32_buffer(scatter_gather_element,
1897 (sizeof(struct sbp2_unrestricted_page_table)) *
1898 sg_count);
1902 } else {
1904 SBP2_DEBUG("No scatter/gather");
1906 command->dma_dir = dma_dir;
1907 command->dma_size = scsi_request_bufflen;
1908 command->dma_type = CMD_DMA_SINGLE;
1909 command->cmd_dma = pci_map_single (hi->host->pdev, scsi_request_buffer,
1910 command->dma_size,
1911 command->dma_dir);
1912 SBP2_DMA_ALLOC("single bulk");
1915 * Handle case where we get a command w/o s/g enabled (but
1916 * check for transfers larger than 64K)
1918 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1920 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1921 command_orb->data_descriptor_lo = command->cmd_dma;
1922 command_orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1923 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1926 * Sanity, in case our direction table is not
1927 * up-to-date
1929 if (!scsi_request_bufflen) {
1930 command_orb->data_descriptor_hi = 0x0;
1931 command_orb->data_descriptor_lo = 0x0;
1932 command_orb->misc |= ORB_SET_DIRECTION(1);
1935 } else {
1937 * Need to turn this into page tables, since the
1938 * buffer is too large.
1940 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1941 command_orb->data_descriptor_lo = command->sge_dma;
1943 /* Use page tables (s/g) */
1944 command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1945 command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1948 * fill out our sbp-2 page tables (and split up
1949 * the large buffer)
1951 sg_count = 0;
1952 sg_len = scsi_request_bufflen;
1953 sg_addr = command->cmd_dma;
1954 while (sg_len) {
1955 scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1956 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1957 scatter_gather_element[sg_count].length_segment_base_hi =
1958 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1959 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1960 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1961 } else {
1962 scatter_gather_element[sg_count].length_segment_base_hi =
1963 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1964 sg_len = 0;
1966 sg_count++;
1969 /* Number of page table (s/g) elements */
1970 command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1972 sbp2util_packet_dump(scatter_gather_element,
1973 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1974 "sbp2 s/g list", command->sge_dma);
1977 * Byte swap page tables if necessary
1979 sbp2util_cpu_to_be32_buffer(scatter_gather_element,
1980 (sizeof(struct sbp2_unrestricted_page_table)) *
1981 sg_count);
1988 * Byte swap command ORB if necessary
1990 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1993 * Put our scsi command in the command ORB
1995 memset(command_orb->cdb, 0, 12);
1996 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1998 return(0);
2002 * This function is called in order to begin a regular SBP-2 command.
2004 static int sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
2005 struct sbp2_command_info *command)
2007 struct sbp2scsi_host_info *hi = scsi_id->hi;
2008 struct hpsb_packet *packet;
2009 struct sbp2_command_orb *command_orb = &command->command_orb;
2011 outstanding_orb_incr;
2012 SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
2013 command_orb, global_outstanding_command_orbs);
2015 pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2016 sizeof(struct sbp2_command_orb),
2017 PCI_DMA_BIDIRECTIONAL);
2018 pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2019 sizeof(command->scatter_gather_element),
2020 PCI_DMA_BIDIRECTIONAL);
2022 * Check to see if there are any previous orbs to use
2024 if (scsi_id->last_orb == NULL) {
2027 * Ok, let's write to the target's management agent register
2029 if (hpsb_node_entry_valid(scsi_id->ne)) {
2031 packet = sbp2util_allocate_write_packet(hi, scsi_id->ne,
2032 scsi_id->sbp2_command_block_agent_addr +
2033 SBP2_ORB_POINTER_OFFSET, 8, NULL);
2035 if (!packet) {
2036 SBP2_ERR("sbp2util_allocate_write_packet failed");
2037 return(-ENOMEM);
2040 packet->data[0] = ORB_SET_NODE_ID(hi->host->node_id);
2041 packet->data[1] = command->command_orb_dma;
2042 sbp2util_cpu_to_be32_buffer(packet->data, 8);
2044 SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb);
2046 if (!hpsb_send_packet(packet)) {
2047 SBP2_ERR("hpsb_send_packet failed");
2048 sbp2_free_packet(packet);
2049 return(-EIO);
2052 SBP2_ORB_DEBUG("write command agent complete");
2055 scsi_id->last_orb = command_orb;
2056 scsi_id->last_orb_dma = command->command_orb_dma;
2058 } else {
2061 * We have an orb already sent (maybe or maybe not
2062 * processed) that we can append this orb to. So do so,
2063 * and ring the doorbell. Have to be very careful
2064 * modifying these next orb pointers, as they are accessed
2065 * both by the sbp2 device and us.
2067 scsi_id->last_orb->next_ORB_lo =
2068 cpu_to_be32(command->command_orb_dma);
2069 /* Tells hardware that this pointer is valid */
2070 scsi_id->last_orb->next_ORB_hi = 0x0;
2071 pci_dma_sync_single(hi->host->pdev, scsi_id->last_orb_dma,
2072 sizeof(struct sbp2_command_orb),
2073 PCI_DMA_BIDIRECTIONAL);
2076 * Ring the doorbell
2078 if (hpsb_node_entry_valid(scsi_id->ne)) {
2079 quadlet_t data = cpu_to_be32(command->command_orb_dma);
2081 packet = sbp2util_allocate_write_packet(hi, scsi_id->ne,
2082 scsi_id->sbp2_command_block_agent_addr +
2083 SBP2_DOORBELL_OFFSET, 4, &data);
2085 if (!packet) {
2086 SBP2_ERR("sbp2util_allocate_write_packet failed");
2087 return(-ENOMEM);
2090 SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb);
2092 if (!hpsb_send_packet(packet)) {
2093 SBP2_ERR("hpsb_send_packet failed");
2094 sbp2_free_packet(packet);
2095 return(-EIO);
2099 scsi_id->last_orb = command_orb;
2100 scsi_id->last_orb_dma = command->command_orb_dma;
2103 return(0);
2107 * This function is called in order to begin a regular SBP-2 command.
2109 static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
2110 Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2112 unchar *cmd = (unchar *) SCpnt->cmnd;
2113 unsigned int request_bufflen = SCpnt->request_bufflen;
2114 struct sbp2_command_info *command;
2116 SBP2_DEBUG("sbp2_send_command");
2117 #if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
2118 printk("[scsi command]\n ");
2119 print_command (cmd);
2120 #endif
2121 SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
2122 SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2125 * Allocate a command orb and s/g structure
2127 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
2128 if (!command) {
2129 return(-EIO);
2133 * The scsi stack sends down a request_bufflen which does not match the
2134 * length field in the scsi cdb. This causes some sbp2 devices to
2135 * reject this inquiry command. Fix the request_bufflen.
2137 if (*cmd == INQUIRY) {
2138 if (force_inquiry_hack || scsi_id->workarounds & SBP2_BREAKAGE_INQUIRY_HACK)
2139 request_bufflen = cmd[4] = 0x24;
2140 else
2141 request_bufflen = cmd[4];
2145 * Now actually fill in the comamnd orb and sbp2 s/g list
2147 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
2148 request_bufflen, SCpnt->request_buffer,
2149 SCpnt->sc_data_direction);
2151 * Update our cdb if necessary (to handle sbp2 RBC command set
2152 * differences). This is where the command set hacks go! =)
2154 sbp2_check_sbp2_command(scsi_id, command->command_orb.cdb);
2156 sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
2157 "sbp2 command orb", command->command_orb_dma);
2160 * Initialize status fifo
2162 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2165 * Link up the orb, and ring the doorbell if needed
2167 sbp2_link_orb_command(scsi_id, command);
2169 return(0);
2174 * This function deals with command set differences between Linux scsi
2175 * command set and sbp2 RBC command set.
2177 static void sbp2_check_sbp2_command(struct scsi_id_instance_data *scsi_id, unchar *cmd)
2179 unchar new_cmd[16];
2180 u8 device_type = SBP2_DEVICE_TYPE (scsi_id->sbp2_device_type_and_lun);
2182 SBP2_DEBUG("sbp2_check_sbp2_command");
2184 switch (*cmd) {
2186 case READ_6:
2188 if (sbp2_command_conversion_device_type(device_type)) {
2190 SBP2_DEBUG("Convert READ_6 to READ_10");
2193 * Need to turn read_6 into read_10
2195 new_cmd[0] = 0x28;
2196 new_cmd[1] = (cmd[1] & 0xe0);
2197 new_cmd[2] = 0x0;
2198 new_cmd[3] = (cmd[1] & 0x1f);
2199 new_cmd[4] = cmd[2];
2200 new_cmd[5] = cmd[3];
2201 new_cmd[6] = 0x0;
2202 new_cmd[7] = 0x0;
2203 new_cmd[8] = cmd[4];
2204 new_cmd[9] = cmd[5];
2206 memcpy(cmd, new_cmd, 10);
2210 break;
2212 case WRITE_6:
2214 if (sbp2_command_conversion_device_type(device_type)) {
2216 SBP2_DEBUG("Convert WRITE_6 to WRITE_10");
2219 * Need to turn write_6 into write_10
2221 new_cmd[0] = 0x2a;
2222 new_cmd[1] = (cmd[1] & 0xe0);
2223 new_cmd[2] = 0x0;
2224 new_cmd[3] = (cmd[1] & 0x1f);
2225 new_cmd[4] = cmd[2];
2226 new_cmd[5] = cmd[3];
2227 new_cmd[6] = 0x0;
2228 new_cmd[7] = 0x0;
2229 new_cmd[8] = cmd[4];
2230 new_cmd[9] = cmd[5];
2232 memcpy(cmd, new_cmd, 10);
2236 break;
2238 case MODE_SENSE:
2240 if (sbp2_command_conversion_device_type(device_type)) {
2242 SBP2_DEBUG("Convert MODE_SENSE_6 to MODE_SENSE_10");
2245 * Need to turn mode_sense_6 into mode_sense_10
2247 new_cmd[0] = 0x5a;
2248 new_cmd[1] = cmd[1];
2249 new_cmd[2] = cmd[2];
2250 new_cmd[3] = 0x0;
2251 new_cmd[4] = 0x0;
2252 new_cmd[5] = 0x0;
2253 new_cmd[6] = 0x0;
2254 new_cmd[7] = 0x0;
2255 new_cmd[8] = cmd[4];
2256 new_cmd[9] = cmd[5];
2258 memcpy(cmd, new_cmd, 10);
2262 break;
2264 case MODE_SELECT:
2267 * TODO. Probably need to change mode select to 10 byte version
2270 default:
2271 break;
2274 return;
2278 * Translates SBP-2 status into SCSI sense data for check conditions
2280 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
2282 SBP2_DEBUG("sbp2_status_to_sense_data");
2285 * Ok, it's pretty ugly... ;-)
2287 sense_data[0] = 0x70;
2288 sense_data[1] = 0x0;
2289 sense_data[2] = sbp2_status[9];
2290 sense_data[3] = sbp2_status[12];
2291 sense_data[4] = sbp2_status[13];
2292 sense_data[5] = sbp2_status[14];
2293 sense_data[6] = sbp2_status[15];
2294 sense_data[7] = 10;
2295 sense_data[8] = sbp2_status[16];
2296 sense_data[9] = sbp2_status[17];
2297 sense_data[10] = sbp2_status[18];
2298 sense_data[11] = sbp2_status[19];
2299 sense_data[12] = sbp2_status[10];
2300 sense_data[13] = sbp2_status[11];
2301 sense_data[14] = sbp2_status[20];
2302 sense_data[15] = sbp2_status[21];
2304 return(sbp2_status[8] & 0x3f); /* return scsi status */
2308 * This function is called after a command is completed, in order to do any necessary SBP-2
2309 * response data translations for the SCSI stack
2311 static void sbp2_check_sbp2_response(struct scsi_id_instance_data *scsi_id,
2312 Scsi_Cmnd *SCpnt)
2314 u8 *scsi_buf = SCpnt->request_buffer;
2315 u8 device_type = SBP2_DEVICE_TYPE (scsi_id->sbp2_device_type_and_lun);
2317 SBP2_DEBUG("sbp2_check_sbp2_response");
2319 switch (SCpnt->cmnd[0]) {
2321 case INQUIRY:
2324 * If scsi_id->sbp2_device_type_and_lun is uninitialized, then fill
2325 * this information in from the inquiry response data. Lun is set to zero.
2327 if (scsi_id->sbp2_device_type_and_lun == SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
2328 SBP2_DEBUG("Creating sbp2_device_type_and_lun from scsi inquiry data");
2329 scsi_id->sbp2_device_type_and_lun = (scsi_buf[0] & 0x1f) << 16;
2333 * Make sure data length is ok. Minimum length is 36 bytes
2335 if (scsi_buf[4] == 0) {
2336 scsi_buf[4] = 36 - 5;
2340 * Check for Simple Direct Access Device and change it to TYPE_DISK
2342 if ((scsi_buf[0] & 0x1f) == TYPE_SDAD) {
2343 SBP2_DEBUG("Changing TYPE_SDAD to TYPE_DISK");
2344 scsi_buf[0] &= 0xe0;
2348 * Fix ansi revision and response data format
2350 scsi_buf[2] |= 2;
2351 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
2353 break;
2355 case MODE_SENSE:
2357 if (sbp2_command_conversion_device_type(device_type)) {
2359 SBP2_DEBUG("Modify mode sense response (10 byte version)");
2361 scsi_buf[0] = scsi_buf[1]; /* Mode data length */
2362 scsi_buf[1] = scsi_buf[2]; /* Medium type */
2363 scsi_buf[2] = scsi_buf[3]; /* Device specific parameter */
2364 scsi_buf[3] = scsi_buf[7]; /* Block descriptor length */
2365 memcpy(scsi_buf + 4, scsi_buf + 8, scsi_buf[0]);
2369 break;
2371 case MODE_SELECT:
2374 * TODO. Probably need to change mode select to 10 byte version
2377 default:
2378 break;
2380 return;
2384 * This function deals with status writes from the SBP-2 device
2386 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid,
2387 quadlet_t *data, u64 addr, unsigned int length, u16 fl)
2389 struct sbp2scsi_host_info *hi = NULL;
2390 struct scsi_id_instance_data *scsi_id = NULL;
2391 u32 id;
2392 unsigned long flags;
2393 Scsi_Cmnd *SCpnt = NULL;
2394 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2395 struct sbp2_command_info *command;
2397 SBP2_DEBUG("sbp2_handle_status_write");
2399 sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
2401 if (!host) {
2402 SBP2_ERR("host is NULL - this is bad!");
2403 return(RCODE_ADDRESS_ERROR);
2406 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
2408 if (!hi) {
2409 SBP2_ERR("host info is NULL - this is bad!");
2410 return(RCODE_ADDRESS_ERROR);
2413 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
2416 * Find our scsi_id structure by looking at the status fifo address written to by
2417 * the sbp2 device.
2419 id = SBP2_STATUS_FIFO_OFFSET_TO_ENTRY((u32)(addr - SBP2_STATUS_FIFO_ADDRESS));
2420 scsi_id = hi->scsi_id[id];
2422 if (!scsi_id) {
2423 SBP2_ERR("scsi_id is NULL - device is gone?");
2424 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2425 return(RCODE_ADDRESS_ERROR);
2429 * Put response into scsi_id status fifo...
2431 memcpy(&scsi_id->status_block, data, length);
2434 * Byte swap first two quadlets (8 bytes) of status for processing
2436 sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8);
2439 * Handle command ORB status here if necessary. First, need to match status with command.
2441 command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo);
2442 if (command) {
2444 SBP2_DEBUG("Found status for command ORB");
2445 pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2446 sizeof(struct sbp2_command_orb),
2447 PCI_DMA_BIDIRECTIONAL);
2448 pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2449 sizeof(command->scatter_gather_element),
2450 PCI_DMA_BIDIRECTIONAL);
2452 SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
2453 outstanding_orb_decr;
2456 * Matched status with command, now grab scsi command pointers and check status
2458 SCpnt = command->Current_SCpnt;
2459 sbp2util_mark_command_completed(scsi_id, command);
2461 if (SCpnt) {
2464 * See if the target stored any scsi status information
2466 if (STATUS_GET_LENGTH(scsi_id->status_block.ORB_offset_hi_misc) > 1) {
2468 * Translate SBP-2 status to SCSI sense data
2470 SBP2_DEBUG("CHECK CONDITION");
2471 scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer);
2475 * Check to see if the dead bit is set. If so, we'll have to initiate
2476 * a fetch agent reset.
2478 if (STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc)) {
2481 * Initiate a fetch agent reset.
2483 SBP2_DEBUG("Dead bit set - initiating fetch agent reset");
2484 sbp2_agent_reset(scsi_id, 0);
2487 SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
2491 * Check here to see if there are no commands in-use. If there are none, we can
2492 * null out last orb so that next time around we write directly to the orb pointer...
2493 * Quick start saves one 1394 bus transaction.
2495 if (list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2496 scsi_id->last_orb = NULL;
2499 } else {
2502 * It's probably a login/logout/reconnect status.
2504 if ((scsi_id->login_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2505 (scsi_id->query_logins_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2506 (scsi_id->reconnect_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2507 (scsi_id->logout_orb_dma == scsi_id->status_block.ORB_offset_lo)) {
2508 atomic_set(&scsi_id->sbp2_login_complete, 1);
2512 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2515 if (SCpnt) {
2518 * Complete the SCSI command.
2520 * Only do it after we've released the sbp2_command_lock,
2521 * as it might otherwise deadlock with the
2522 * io_request_lock (in sbp2scsi_queuecommand).
2524 SBP2_DEBUG("Completing SCSI command");
2525 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
2526 command->Current_done);
2527 SBP2_ORB_DEBUG("command orb completed");
2530 return(RCODE_COMPLETE);
2534 /**************************************
2535 * SCSI interface related section
2536 **************************************/
2539 * This routine is the main request entry routine for doing I/O. It is
2540 * called from the scsi stack directly.
2542 static int sbp2scsi_queuecommand (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2544 struct sbp2scsi_host_info *hi = NULL;
2545 struct scsi_id_instance_data *scsi_id = NULL;
2546 unsigned long flags;
2548 SBP2_DEBUG("sbp2scsi_queuecommand");
2551 * Pull our host info and scsi id instance data from the scsi command
2553 hi = hpsb_get_hostinfo_bykey(&sbp2_highlevel, (unsigned long)SCpnt->device->host);
2555 if (!hi) {
2556 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
2557 SCpnt->result = DID_NO_CONNECT << 16;
2558 done (SCpnt);
2559 return(0);
2562 scsi_id = hi->scsi_id[SCpnt->device->id];
2565 * If scsi_id is null, it means there is no device in this slot,
2566 * so we should return selection timeout.
2568 if (!scsi_id) {
2569 SCpnt->result = DID_NO_CONNECT << 16;
2570 done (SCpnt);
2571 return(0);
2575 * Until we handle multiple luns, just return selection time-out
2576 * to any IO directed at non-zero LUNs
2578 if (SCpnt->device->lun) {
2579 SCpnt->result = DID_NO_CONNECT << 16;
2580 done (SCpnt);
2581 return(0);
2585 * Check for request sense command, and handle it here
2586 * (autorequest sense)
2588 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
2589 SBP2_DEBUG("REQUEST_SENSE");
2590 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
2591 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2592 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
2593 return(0);
2597 * Check to see if we are in the middle of a bus reset.
2599 if (!hpsb_node_entry_valid(scsi_id->ne)) {
2600 SBP2_ERR("Bus reset in progress - rejecting command");
2601 SCpnt->result = DID_BUS_BUSY << 16;
2602 done (SCpnt);
2603 return(0);
2607 * Try and send our SCSI command
2609 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
2610 if (sbp2_send_command(scsi_id, SCpnt, done)) {
2611 SBP2_ERR("Error sending SCSI command");
2612 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
2613 SCpnt, done);
2615 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2617 return(0);
2621 * This function is called in order to complete all outstanding SBP-2
2622 * commands (in case of resets, etc.).
2624 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
2625 u32 status)
2627 struct sbp2scsi_host_info *hi = scsi_id->hi;
2628 struct list_head *lh;
2629 struct sbp2_command_info *command;
2631 SBP2_DEBUG("sbp2_complete_all_commands");
2633 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2634 SBP2_DEBUG("Found pending command to complete");
2635 lh = scsi_id->sbp2_command_orb_inuse.next;
2636 command = list_entry(lh, struct sbp2_command_info, list);
2637 pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2638 sizeof(struct sbp2_command_orb),
2639 PCI_DMA_BIDIRECTIONAL);
2640 pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2641 sizeof(command->scatter_gather_element),
2642 PCI_DMA_BIDIRECTIONAL);
2643 sbp2util_mark_command_completed(scsi_id, command);
2644 if (command->Current_SCpnt) {
2645 void (*done)(Scsi_Cmnd *) = command->Current_done;
2646 command->Current_SCpnt->result = status << 16;
2647 done (command->Current_SCpnt);
2651 return;
2655 * This function is called in order to complete a regular SBP-2 command.
2657 * This can be called in interrupt context.
2659 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2660 u32 scsi_status, Scsi_Cmnd *SCpnt,
2661 void (*done)(Scsi_Cmnd *))
2663 unsigned long flags;
2665 SBP2_DEBUG("sbp2scsi_complete_command");
2668 * Sanity
2670 if (!SCpnt) {
2671 SBP2_ERR("SCpnt is NULL");
2672 return;
2676 * If a bus reset is in progress and there was an error, don't
2677 * complete the command, just let it get retried at the end of the
2678 * bus reset.
2680 if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2681 SBP2_ERR("Bus reset in progress - retry command later");
2682 return;
2686 * Switch on scsi status
2688 switch (scsi_status) {
2689 case SBP2_SCSI_STATUS_GOOD:
2690 SCpnt->result = DID_OK;
2691 break;
2693 case SBP2_SCSI_STATUS_BUSY:
2694 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2695 SCpnt->result = DID_BUS_BUSY << 16;
2696 break;
2698 case SBP2_SCSI_STATUS_CHECK_CONDITION:
2699 SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
2700 SCpnt->result = CHECK_CONDITION << 1;
2703 * Debug stuff
2705 #if CONFIG_IEEE1394_SBP2_DEBUG >= 1
2706 print_command (SCpnt->cmnd);
2707 print_sense("bh", SCpnt);
2708 #endif
2710 break;
2712 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2713 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2714 SCpnt->result = DID_NO_CONNECT << 16;
2715 print_command (SCpnt->cmnd);
2716 break;
2718 case SBP2_SCSI_STATUS_CONDITION_MET:
2719 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2720 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2721 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2722 SCpnt->result = DID_ERROR << 16;
2723 print_command (SCpnt->cmnd);
2724 break;
2726 default:
2727 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2728 SCpnt->result = DID_ERROR << 16;
2732 * Take care of any sbp2 response data mucking here (RBC stuff, etc.)
2734 if (SCpnt->result == DID_OK) {
2735 sbp2_check_sbp2_response(scsi_id, SCpnt);
2739 * If a bus reset is in progress and there was an error, complete
2740 * the command as busy so that it will get retried.
2742 if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2743 SBP2_ERR("Completing command with busy (bus reset)");
2744 SCpnt->result = DID_BUS_BUSY << 16;
2748 * If a unit attention occurs, return busy status so it gets
2749 * retried... it could have happened because of a 1394 bus reset
2750 * or hot-plug...
2752 #if 0
2753 if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
2754 (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
2755 SBP2_DEBUG("UNIT ATTENTION - return busy");
2756 SCpnt->result = DID_BUS_BUSY << 16;
2758 #endif
2761 * Tell scsi stack that we're done with this command
2763 spin_lock_irqsave(scsi_id->hi->scsi_host->host_lock,flags);
2764 done (SCpnt);
2765 spin_unlock_irqrestore(scsi_id->hi->scsi_host->host_lock,flags);
2767 return;
2771 * Called by scsi stack when something has really gone wrong. Usually
2772 * called when a command has timed-out for some reason.
2774 static int sbp2scsi_abort (Scsi_Cmnd *SCpnt)
2776 struct sbp2scsi_host_info *hi = hpsb_get_hostinfo_bykey(&sbp2_highlevel,
2777 (unsigned long)SCpnt->device->host);
2778 struct scsi_id_instance_data *scsi_id = hi->scsi_id[SCpnt->device->id];
2779 struct sbp2_command_info *command;
2780 unsigned long flags;
2782 SBP2_ERR("aborting sbp2 command");
2783 print_command (SCpnt->cmnd);
2785 if (scsi_id) {
2788 * Right now, just return any matching command structures
2789 * to the free pool.
2791 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
2792 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2793 if (command) {
2794 SBP2_DEBUG("Found command to abort");
2795 pci_dma_sync_single(hi->host->pdev,
2796 command->command_orb_dma,
2797 sizeof(struct sbp2_command_orb),
2798 PCI_DMA_BIDIRECTIONAL);
2799 pci_dma_sync_single(hi->host->pdev,
2800 command->sge_dma,
2801 sizeof(command->scatter_gather_element),
2802 PCI_DMA_BIDIRECTIONAL);
2803 sbp2util_mark_command_completed(scsi_id, command);
2804 if (command->Current_SCpnt) {
2805 void (*done)(Scsi_Cmnd *) = command->Current_done;
2806 command->Current_SCpnt->result = DID_ABORT << 16;
2807 done (command->Current_SCpnt);
2812 * Initiate a fetch agent reset.
2814 sbp2_agent_reset(scsi_id, 0);
2815 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2816 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2819 return(SUCCESS);
2823 * Called by scsi stack when something has really gone wrong.
2825 static int sbp2scsi_reset (Scsi_Cmnd *SCpnt)
2827 struct sbp2scsi_host_info *hi = hpsb_get_hostinfo_bykey(&sbp2_highlevel,
2828 (unsigned long)SCpnt->device->host);
2829 struct scsi_id_instance_data *scsi_id = hi->scsi_id[SCpnt->device->id];
2831 SBP2_ERR("reset requested");
2833 if (scsi_id) {
2834 SBP2_ERR("Generating sbp2 fetch agent reset");
2835 sbp2_agent_reset(scsi_id, 0);
2838 return(SUCCESS);
2841 static const char *sbp2scsi_info (struct Scsi_Host *host)
2843 return "SCSI emulation for IEEE-1394 SBP-2 Devices";
2846 /* Called for contents of procfs */
2847 #define SPRINTF(args...) \
2848 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
2850 static int sbp2scsi_proc_info(struct Scsi_Host *scsi_host, char *buffer, char **start, off_t offset,
2851 int length, int inout)
2853 Scsi_Device *scd;
2854 struct hpsb_host *host;
2855 char *pos = buffer;
2857 /* if someone is sending us data, just throw it away */
2858 if (inout)
2859 return length;
2861 host = hpsb_get_host_bykey(&sbp2_highlevel, (unsigned long)scsi_host);
2862 if (!host) /* shouldn't happen, but... */
2863 return -ESRCH;
2865 SPRINTF("Host scsi%d : SBP-2 IEEE-1394 (%s)\n",
2866 scsi_host->host_no, host->driver->name);
2867 SPRINTF("Driver version : %s\n", version);
2869 SPRINTF("\nModule options :\n");
2870 SPRINTF(" max_speed : %s\n", hpsb_speedto_str[max_speed]);
2871 SPRINTF(" max_sectors : %d\n", max_sectors);
2872 SPRINTF(" serialize_io : %s\n", serialize_io ? "yes" : "no");
2873 SPRINTF(" exclusive_login : %s\n", exclusive_login ? "yes" : "no");
2875 SPRINTF("\nAttached devices : %s\n", !list_empty(&scsi_host->my_devices) ?
2876 "" : "none");
2878 list_for_each_entry (scd, &scsi_host->my_devices, siblings) {
2879 int i;
2881 SPRINTF(" [Channel: %02d, Id: %02d, Lun: %02d] ", scd->channel,
2882 scd->id, scd->lun);
2883 SPRINTF("%s ", (scd->type < MAX_SCSI_DEVICE_CODE) ?
2884 scsi_device_types[(short) scd->type] : "Unknown device");
2886 for (i = 0; (i < 8) && (scd->vendor[i] >= 0x20); i++)
2887 SPRINTF("%c", scd->vendor[i]);
2889 SPRINTF(" ");
2891 for (i = 0; (i < 16) && (scd->model[i] >= 0x20); i++)
2892 SPRINTF("%c", scd->model[i]);
2894 SPRINTF("\n");
2897 SPRINTF("\n");
2899 /* Calculate start of next buffer, and return value. */
2900 *start = buffer + offset;
2902 if ((pos - buffer) < offset)
2903 return (0);
2904 else if ((pos - buffer - offset) < length)
2905 return (pos - buffer - offset);
2906 else
2907 return (length);
2910 MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2911 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2912 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2913 MODULE_LICENSE("GPL");
2915 /* SCSI host template */
2916 static Scsi_Host_Template scsi_driver_template = {
2917 .module = THIS_MODULE,
2918 .name = "SBP-2 IEEE-1394",
2919 .proc_name = SBP2_DEVICE_NAME,
2920 .info = sbp2scsi_info,
2921 .proc_info = sbp2scsi_proc_info,
2922 .queuecommand = sbp2scsi_queuecommand,
2923 .eh_abort_handler = sbp2scsi_abort,
2924 .eh_device_reset_handler = sbp2scsi_reset,
2925 .eh_bus_reset_handler = sbp2scsi_reset,
2926 .eh_host_reset_handler = sbp2scsi_reset,
2927 .this_id = -1,
2928 .sg_tablesize = SG_ALL,
2929 .use_clustering = ENABLE_CLUSTERING,
2930 .cmd_per_lun = SBP2_MAX_CMDS_PER_LUN,
2931 .can_queue = SBP2_MAX_SCSI_QUEUE,
2932 .emulated = 1,
2933 .highmem_io = 1,
2936 static int sbp2_module_init(void)
2938 SBP2_DEBUG("sbp2_module_init");
2940 /* Module load debug option to force one command at a time (serializing I/O) */
2941 if (serialize_io) {
2942 SBP2_ERR("Driver forced to serialize I/O (serialize_io = 1)");
2943 scsi_driver_template.can_queue = 1;
2944 scsi_driver_template.cmd_per_lun = 1;
2947 /* Set max sectors (module load option). Default is 255 sectors. */
2948 scsi_driver_template.max_sectors = max_sectors;
2951 /* Register our high level driver with 1394 stack */
2952 hpsb_register_highlevel(&sbp2_highlevel);
2954 /* Register our sbp2 status address space... */
2955 hpsb_register_addrspace(&sbp2_highlevel, &sbp2_ops, SBP2_STATUS_FIFO_ADDRESS,
2956 SBP2_STATUS_FIFO_ADDRESS +
2957 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(SBP2SCSI_MAX_SCSI_IDS+1));
2959 /* Handle data movement if physical dma is not enabled/supported
2960 * on host controller */
2961 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
2962 hpsb_register_addrspace(&sbp2_highlevel, &sbp2_physdma_ops, 0x0ULL, 0xfffffffcULL);
2963 #endif
2965 hpsb_register_protocol(&sbp2_driver);
2967 return 0;
2970 static void __exit sbp2_module_exit(void)
2972 SBP2_DEBUG("sbp2_module_exit");
2974 hpsb_unregister_protocol(&sbp2_driver);
2976 hpsb_unregister_highlevel(&sbp2_highlevel);
2979 module_init(sbp2_module_init);
2980 module_exit(sbp2_module_exit);