a17bf9e96c80fa1ebbc2bb0bbd7f7f8e2169e0b9
[linux-flexiantxendom0-3.2.10.git] / drivers / ieee1394 / sbp2.c
1 /*
2  * sbp2.c - SBP-2 protocol driver for IEEE-1394
3  *
4  * Copyright (C) 2000 James Goodwin, Filanet Corporation (www.filanet.com)
5  * jamesg@filanet.com (JSG)
6  *
7  * Copyright (C) 2003 Ben Collins <bcollins@debian.org>
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 /*
25  * Brief Description:
26  *
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.
31  *
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.).
34  *
35  * Current Issues:
36  *
37  *      - Error Handling: SCSI aborts and bus reset requests are handled somewhat
38  *        but the code needs additional debugging.
39  */
40
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/blkdev.h>
55 #include <linux/smp_lock.h>
56 #include <linux/init.h>
57 #include <linux/version.h>
58 #include <linux/pci.h>
59
60 #include <asm/current.h>
61 #include <asm/uaccess.h>
62 #include <asm/io.h>
63 #include <asm/byteorder.h>
64 #include <asm/atomic.h>
65 #include <asm/system.h>
66 #include <asm/io.h>
67 #include <asm/scatterlist.h>
68
69 #include "../scsi/scsi.h"
70 #include "../scsi/hosts.h"
71
72 #include "ieee1394.h"
73 #include "ieee1394_types.h"
74 #include "ieee1394_core.h"
75 #include "nodemgr.h"
76 #include "hosts.h"
77 #include "nodemgr.h"
78 #include "highlevel.h"
79 #include "ieee1394_transactions.h"
80 #include "sbp2.h"
81
82 static char version[] __devinitdata =
83         "$Rev: 1010 $ Ben Collins <bcollins@debian.org>";
84
85 /*
86  * Module load parameter definitions
87  */
88
89 /*
90  * Change max_speed on module load if you have a bad IEEE-1394
91  * controller that has trouble running 2KB packets at 400mb.
92  *
93  * NOTE: On certain OHCI parts I have seen short packets on async transmit
94  * (probably due to PCI latency/throughput issues with the part). You can
95  * bump down the speed if you are running into problems.
96  */
97 static int max_speed = IEEE1394_SPEED_MAX;
98 module_param(max_speed, int, 0644);
99 MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb default, 1 = 200mb, 0 = 100mb)");
100
101 /*
102  * Set serialize_io to 1 if you'd like only one scsi command sent
103  * down to us at a time (debugging). This might be necessary for very
104  * badly behaved sbp2 devices.
105  */
106 static int serialize_io = 0;
107 module_param(serialize_io, int, 0444);
108 MODULE_PARM_DESC(serialize_io, "Serialize all I/O coming down from the scsi drivers (default = 0)");
109
110 /*
111  * Bump up max_sectors if you'd like to support very large sized
112  * transfers. Please note that some older sbp2 bridge chips are broken for
113  * transfers greater or equal to 128KB.  Default is a value of 255
114  * sectors, or just under 128KB (at 512 byte sector size). I can note that
115  * the Oxsemi sbp2 chipsets have no problems supporting very large
116  * transfer sizes.
117  */
118 static int max_sectors = SBP2_MAX_SECTORS;
119 module_param(max_sectors, int, 0444);
120 MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = 255)");
121
122 /*
123  * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
124  * do an exclusive login, as it's generally unsafe to have two hosts
125  * talking to a single sbp2 device at the same time (filesystem coherency,
126  * etc.). If you're running an sbp2 device that supports multiple logins,
127  * and you're either running read-only filesystems or some sort of special
128  * filesystem supporting multiple hosts (one such filesystem is OpenGFS,
129  * see opengfs.sourceforge.net for more info), then set exclusive_login
130  * to zero. Note: The Oxsemi OXFW911 sbp2 chipset supports up to four
131  * concurrent logins.
132  */
133 static int exclusive_login = 1;
134 module_param(exclusive_login, int, 0644);
135 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
136
137 /*
138  * SCSI inquiry hack for really badly behaved sbp2 devices. Turn this on
139  * if your sbp2 device is not properly handling the SCSI inquiry command.
140  * This hack makes the inquiry look more like a typical MS Windows
141  * inquiry.
142  * 
143  * If force_inquiry_hack=1 is required for your device to work,
144  * please submit the logged sbp2_firmware_revision value of this device to
145  * the linux1394-devel mailing list.
146  */
147 static int force_inquiry_hack = 0;
148 module_param(force_inquiry_hack, int, 0444);
149 MODULE_PARM_DESC(force_inquiry_hack, "Force SCSI inquiry hack (default = 0)");
150
151
152 /*
153  * Export information about protocols/devices supported by this driver.
154  */
155 static struct ieee1394_device_id sbp2_id_table[] = {
156         {
157                 .match_flags =IEEE1394_MATCH_SPECIFIER_ID |
158                               IEEE1394_MATCH_VERSION,
159                 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
160                 .version =    SBP2_SW_VERSION_ENTRY & 0xffffff
161         },
162         { }
163 };
164
165 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
166
167 /*
168  * Debug levels, configured via kernel config, or enable here.
169  */
170
171 /* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
172 /* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
173 /* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
174 /* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
175 /* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
176
177 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
178 #define SBP2_ORB_DEBUG(fmt, args...)    HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
179 static u32 global_outstanding_command_orbs = 0;
180 #define outstanding_orb_incr global_outstanding_command_orbs++
181 #define outstanding_orb_decr global_outstanding_command_orbs--
182 #else
183 #define SBP2_ORB_DEBUG(fmt, args...)
184 #define outstanding_orb_incr
185 #define outstanding_orb_decr
186 #endif
187
188 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
189 #define SBP2_DMA_ALLOC(fmt, args...) \
190         HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
191                  ++global_outstanding_dmas, ## args)
192 #define SBP2_DMA_FREE(fmt, args...) \
193         HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
194                  --global_outstanding_dmas, ## args)
195 static u32 global_outstanding_dmas = 0;
196 #else
197 #define SBP2_DMA_ALLOC(fmt, args...)
198 #define SBP2_DMA_FREE(fmt, args...)
199 #endif
200
201 #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
202 #define SBP2_DEBUG(fmt, args...)        HPSB_ERR("sbp2: "fmt, ## args)
203 #define SBP2_INFO(fmt, args...)         HPSB_ERR("sbp2: "fmt, ## args)
204 #define SBP2_NOTICE(fmt, args...)       HPSB_ERR("sbp2: "fmt, ## args)
205 #define SBP2_WARN(fmt, args...)         HPSB_ERR("sbp2: "fmt, ## args)
206 #elif CONFIG_IEEE1394_SBP2_DEBUG == 1
207 #define SBP2_DEBUG(fmt, args...)        HPSB_DEBUG("sbp2: "fmt, ## args)
208 #define SBP2_INFO(fmt, args...)         HPSB_INFO("sbp2: "fmt, ## args)
209 #define SBP2_NOTICE(fmt, args...)       HPSB_NOTICE("sbp2: "fmt, ## args)
210 #define SBP2_WARN(fmt, args...)         HPSB_WARN("sbp2: "fmt, ## args)
211 #else 
212 #define SBP2_DEBUG(fmt, args...)
213 #define SBP2_INFO(fmt, args...)         HPSB_INFO("sbp2: "fmt, ## args)
214 #define SBP2_NOTICE(fmt, args...)       HPSB_NOTICE("sbp2: "fmt, ## args)
215 #define SBP2_WARN(fmt, args...)         HPSB_WARN("sbp2: "fmt, ## args)
216 #endif
217
218 #define SBP2_ERR(fmt, args...)          HPSB_ERR("sbp2: "fmt, ## args)
219
220
221 /*
222  * Globals
223  */
224
225 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
226                                            u32 status);
227
228 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
229                                       u32 scsi_status, Scsi_Cmnd *SCpnt,
230                                       void (*done)(Scsi_Cmnd *));
231         
232 static Scsi_Host_Template scsi_driver_template;
233
234 const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
235
236 static struct hpsb_highlevel sbp2_highlevel = {
237         .name =         SBP2_DEVICE_NAME,
238         .remove_host =  sbp2_remove_host,
239 };
240
241 static struct hpsb_address_ops sbp2_ops = {
242         .write = sbp2_handle_status_write
243 };
244
245 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
246 static struct hpsb_address_ops sbp2_physdma_ops = {
247         .read = sbp2_handle_physdma_read,
248         .write = sbp2_handle_physdma_write,
249 };
250 #endif
251
252 static struct hpsb_protocol_driver sbp2_driver = {
253         .name           = "SBP2 Driver",
254         .id_table       = sbp2_id_table,
255         .update         = sbp2_update,
256         .driver         = {
257                 .name           = SBP2_DEVICE_NAME,
258                 .bus            = &ieee1394_bus_type,
259                 .probe          = sbp2_probe,
260                 .remove         = sbp2_remove,
261         },
262 };
263
264
265 /* List of device firmware's that require a forced 36 byte inquiry.  */
266 static u32 sbp2_broken_inquiry_list[] = {
267         0x00002800,     /* Stefan Richter <richtest@bauwesen.tu-cottbus.de> */
268                         /* DViCO Momobay CX-1 */
269         0x00000200      /* Andreas Plesch <plesch@fas.harvard.edu> */
270                         /* QPS Fire DVDBurner */
271 };
272
273 #define NUM_BROKEN_INQUIRY_DEVS \
274         (sizeof(sbp2_broken_inquiry_list)/sizeof(*sbp2_broken_inquiry_list))
275
276 /**************************************
277  * General utility functions
278  **************************************/
279
280
281 #ifndef __BIG_ENDIAN
282 /*
283  * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
284  */
285 static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
286 {
287         u32 *temp = buffer;
288
289         for (length = (length >> 2); length--; )
290                 temp[length] = be32_to_cpu(temp[length]);
291
292         return;
293 }
294
295 /*
296  * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
297  */
298 static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
299 {
300         u32 *temp = buffer;
301
302         for (length = (length >> 2); length--; )
303                 temp[length] = cpu_to_be32(temp[length]);
304
305         return;
306 }
307 #else /* BIG_ENDIAN */
308 /* Why waste the cpu cycles? */
309 #define sbp2util_be32_to_cpu_buffer(x,y)
310 #define sbp2util_cpu_to_be32_buffer(x,y)
311 #endif
312
313 #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
314 /*
315  * Debug packet dump routine. Length is in bytes.
316  */
317 static void sbp2util_packet_dump(void *buffer, int length, char *dump_name, u32 dump_phys_addr)
318 {
319         int i;
320         unsigned char *dump = buffer;
321
322         if (!dump || !length || !dump_name)
323                 return;
324
325         if (dump_phys_addr)
326                 printk("[%s, 0x%x]", dump_name, dump_phys_addr);
327         else
328                 printk("[%s]", dump_name);
329         for (i = 0; i < length; i++) {
330                 if (i > 0x3f) {
331                         printk("\n   ...");
332                         break;
333                 }
334                 if ((i & 0x3) == 0)
335                         printk("  ");
336                 if ((i & 0xf) == 0)
337                         printk("\n   ");
338                 printk("%02x ", (int) dump[i]);
339         }
340         printk("\n");
341
342         return;
343 }
344 #else
345 #define sbp2util_packet_dump(w,x,y,z)
346 #endif
347
348 /*
349  * Goofy routine that basically does a down_timeout function.
350  */
351 static int sbp2util_down_timeout(atomic_t *done, int timeout)
352 {
353         int i;
354
355         for (i = timeout; (i > 0 && atomic_read(done) == 0); i-= HZ/10) {
356                 set_current_state(TASK_INTERRUPTIBLE);
357                 if (schedule_timeout(HZ/10))    /* 100ms */
358                         return(1);
359         }
360         return ((i > 0) ? 0:1);
361 }
362
363 /* Free's an allocated packet */
364 static void sbp2_free_packet(struct hpsb_packet *packet)
365 {
366         hpsb_free_tlabel(packet);
367         free_hpsb_packet(packet);
368 }
369
370 /*
371  * This function is called to retrieve a block write packet from our
372  * packet pool. This function is used in place of calling
373  * alloc_hpsb_packet (which costs us three kmallocs). Instead we just pull
374  * out a free request packet and re-initialize values in it. I'm sure this
375  * can still stand some more optimization.
376  */
377 static struct hpsb_packet *
378 sbp2util_allocate_write_packet(struct sbp2scsi_host_info *hi,
379                                struct node_entry *ne, u64 addr,
380                                size_t data_size,
381                                quadlet_t *data)
382 {
383         struct hpsb_packet *packet;
384
385         packet = hpsb_make_writepacket(hi->host, ne->nodeid,
386                                        addr, data, data_size);
387
388         if (!packet)
389                 return NULL;
390
391         hpsb_set_packet_complete_task(packet, (void (*)(void*))sbp2_free_packet,
392                                       packet);
393
394         hpsb_node_fill_packet(ne, packet);
395
396         return packet;
397 }
398
399
400 /*
401  * This function is called to create a pool of command orbs used for
402  * command processing. It is called when a new sbp2 device is detected.
403  */
404 static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
405 {
406         struct sbp2scsi_host_info *hi = scsi_id->hi;
407         int i;
408         unsigned long flags, orbs;
409         struct sbp2_command_info *command;
410
411         orbs = serialize_io ? 2 : SBP2_MAX_COMMAND_ORBS;
412         
413         spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
414         for (i = 0; i < orbs; i++) {
415                 command = (struct sbp2_command_info *)
416                     kmalloc(sizeof(struct sbp2_command_info), GFP_ATOMIC);
417                 if (!command) {
418                         spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
419                         return(-ENOMEM);
420                 }
421                 memset(command, '\0', sizeof(struct sbp2_command_info));
422                 command->command_orb_dma =
423                         pci_map_single (hi->host->pdev, &command->command_orb,
424                                         sizeof(struct sbp2_command_orb),
425                                         PCI_DMA_BIDIRECTIONAL);
426                 SBP2_DMA_ALLOC("single command orb DMA");
427                 command->sge_dma =
428                         pci_map_single (hi->host->pdev, &command->scatter_gather_element,
429                                         sizeof(command->scatter_gather_element),
430                                         PCI_DMA_BIDIRECTIONAL);
431                 SBP2_DMA_ALLOC("scatter_gather_element");
432                 INIT_LIST_HEAD(&command->list);
433                 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
434         }
435         spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
436         return 0;
437 }
438
439 /*
440  * This function is called to delete a pool of command orbs.
441  */
442 static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
443 {
444         struct hpsb_host *host = scsi_id->hi->host;
445         struct list_head *lh, *next;
446         struct sbp2_command_info *command;
447         unsigned long flags;
448         
449         spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
450         if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
451                 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
452                         command = list_entry(lh, struct sbp2_command_info, list);
453
454                         /* Release our generic DMA's */
455                         pci_unmap_single(host->pdev, command->command_orb_dma,
456                                          sizeof(struct sbp2_command_orb),
457                                          PCI_DMA_BIDIRECTIONAL);
458                         SBP2_DMA_FREE("single command orb DMA");
459                         pci_unmap_single(host->pdev, command->sge_dma,
460                                          sizeof(command->scatter_gather_element),
461                                          PCI_DMA_BIDIRECTIONAL);
462                         SBP2_DMA_FREE("scatter_gather_element");
463
464                         kfree(command);
465                 }
466         }
467         spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
468         return;
469 }
470
471 /* 
472  * This function finds the sbp2_command for a given outstanding command
473  * orb.Only looks at the inuse list.
474  */
475 static struct sbp2_command_info *sbp2util_find_command_for_orb(
476                 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
477 {
478         struct list_head *lh;
479         struct sbp2_command_info *command;
480         unsigned long flags;
481
482         spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
483         if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
484                 list_for_each(lh, &scsi_id->sbp2_command_orb_inuse) {
485                         command = list_entry(lh, struct sbp2_command_info, list);
486                         if (command->command_orb_dma == orb) {
487                                 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
488                                 return (command);
489                         }
490                 }
491         }
492         spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
493
494         SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
495
496         return(NULL);
497 }
498
499 /* 
500  * This function finds the sbp2_command for a given outstanding SCpnt.
501  * Only looks at the inuse list.
502  */
503 static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(struct scsi_id_instance_data *scsi_id, void *SCpnt)
504 {
505         struct list_head *lh;
506         struct sbp2_command_info *command;
507         unsigned long flags;
508
509         spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
510         if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
511                 list_for_each(lh, &scsi_id->sbp2_command_orb_inuse) {
512                         command = list_entry(lh, struct sbp2_command_info, list);
513                         if (command->Current_SCpnt == SCpnt) {
514                                 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
515                                 return (command);
516                         }
517                 }
518         }
519         spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
520         return(NULL);
521 }
522
523 /*
524  * This function allocates a command orb used to send a scsi command.
525  */
526 static struct sbp2_command_info *sbp2util_allocate_command_orb(
527                 struct scsi_id_instance_data *scsi_id, 
528                 Scsi_Cmnd *Current_SCpnt, 
529                 void (*Current_done)(Scsi_Cmnd *))
530 {
531         struct list_head *lh;
532         struct sbp2_command_info *command = NULL;
533         unsigned long flags;
534
535         spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
536         if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
537                 lh = scsi_id->sbp2_command_orb_completed.next;
538                 list_del(lh);
539                 command = list_entry(lh, struct sbp2_command_info, list);
540                 command->Current_done = Current_done;
541                 command->Current_SCpnt = Current_SCpnt;
542                 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
543         } else {
544                 SBP2_ERR("sbp2util_allocate_command_orb - No orbs available!");
545         }
546         spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
547         return (command);
548 }
549
550 /* Free our DMA's */
551 static void sbp2util_free_command_dma(struct sbp2_command_info *command)
552 {
553         struct hpsb_host *host;
554
555         host = hpsb_get_host_bykey(&sbp2_highlevel,
556                         (unsigned long)command->Current_SCpnt->device->host);
557         if (!host) {
558                 printk(KERN_ERR "%s: host == NULL\n", __FUNCTION__);
559                 return;
560         }
561
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;
574         }
575
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;
581         }
582 }
583
584 /*
585  * This function moves a command to the completed orb list.
586  */
587 static void sbp2util_mark_command_completed(struct scsi_id_instance_data *scsi_id, struct sbp2_command_info *command)
588 {
589         unsigned long flags;
590
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);
596 }
597
598 \f
599
600 /*********************************************
601  * IEEE-1394 core driver stack related section
602  *********************************************/
603
604 static int sbp2_probe(struct device *dev)
605 {
606         struct unit_directory *ud;
607         struct sbp2scsi_host_info *hi;
608
609         SBP2_DEBUG(__FUNCTION__);
610
611         ud = container_of(dev, struct unit_directory, device);
612
613         /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
614          * instead. */
615         if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
616                 return -ENODEV;
617
618         /* This will only add it if it doesn't exist */
619         hi = sbp2_add_host(ud->ne->host);
620
621         if (!hi)
622                 return -ENODEV;
623
624         return sbp2_start_ud(hi, ud);
625 }
626
627 static int sbp2_remove(struct device *dev)
628 {
629         struct scsi_id_group *scsi_group;
630         struct list_head *lh, *next;
631         struct unit_directory *ud;
632         struct scsi_id_instance_data *scsi_id;
633
634         SBP2_DEBUG(__FUNCTION__);
635
636         ud = container_of(dev, struct unit_directory, device);
637         scsi_group = ud->device.driver_data;
638         ud->device.driver_data = NULL;
639
640         list_for_each_safe (lh, next, &scsi_group->scsi_id_list) {
641                 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
642
643                 if (scsi_id != NULL) {
644                         sbp2_logout_device(scsi_id);
645                         sbp2_remove_device(scsi_id);
646                 }
647         }
648
649         kfree(scsi_group);
650
651         return 0;
652 }
653
654 static void sbp2_update(struct unit_directory *ud)
655 {
656         struct sbp2scsi_host_info *hi;
657         struct scsi_id_group *scsi_group = ud->device.driver_data;
658         struct list_head *lh, *next;
659         struct scsi_id_instance_data *scsi_id;
660         unsigned long flags;
661
662         SBP2_DEBUG("sbp2_update");
663
664         list_for_each_safe (lh, next, &scsi_group->scsi_id_list) {
665                 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
666
667                 hi = scsi_id->hi;
668
669                 if (sbp2_reconnect_device(scsi_id)) {
670                 
671                         /* 
672                          * Ok, reconnect has failed. Perhaps we didn't
673                          * reconnect fast enough. Try doing a regular login.
674                          */
675                         if (sbp2_login_device(scsi_id)) {
676                                 /* Login failed too, just remove the device. */
677                                 SBP2_ERR("sbp2_reconnect_device failed!");
678                                 sbp2_remove_device(scsi_id);
679                                 return;
680                         }
681                 }
682
683                 /* Set max retries to something large on the device. */
684                 sbp2_set_busy_timeout(scsi_id);
685
686                 /* Do a SBP-2 fetch agent reset. */
687                 sbp2_agent_reset(scsi_id, 1);
688         
689                 /* Get the max speed and packet size that we can use. */
690                 sbp2_max_speed_and_size(scsi_id);
691
692                 /* Complete any pending commands with busy (so they get
693                  * retried) and remove them from our queue
694                  */
695                 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
696                 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
697                 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
698         }
699 }
700
701 /* This functions is called by the sbp2_probe, for each new device. If the
702  * host_info already exists, it will return it. If not, it allocated a new
703  * host_info entry and a corresponding scsi_host. */
704 static struct sbp2scsi_host_info *sbp2_add_host(struct hpsb_host *host)
705 {
706         struct sbp2scsi_host_info *hi;
707         struct Scsi_Host *scsi_host;
708
709         SBP2_DEBUG("sbp2_add_host");
710
711         hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
712         if (hi)
713                 return hi;
714
715         /* Register our host with the SCSI stack. */
716         scsi_host = scsi_host_alloc(&scsi_driver_template, 0);
717         if (!scsi_host) {
718                 SBP2_ERR("failed to register scsi host");
719                 return NULL;
720         }
721
722         hi = hpsb_create_hostinfo(&sbp2_highlevel, host, sizeof(*hi));
723         if (!hi) {
724                 SBP2_ERR("failed to allocate hostinfo");
725                 scsi_host_put(hi->scsi_host);
726         }
727
728         hpsb_set_hostinfo_key(&sbp2_highlevel, host, (unsigned long)scsi_host);
729
730         hi->scsi_host = scsi_host;
731         hi->host = host;
732         hi->sbp2_command_lock = SPIN_LOCK_UNLOCKED;
733         hi->scsi_host->max_id = SBP2SCSI_MAX_SCSI_IDS;
734
735         /* XXX We need a device to pass here as the scsi-host class. Can't
736          * use the PCI device, since it is already bound to the ieee1394
737          * host. Can't use the fw-host device since it is multi-class
738          * enabled (scsi-host uses classdata member of the device). */
739         if (scsi_add_host(hi->scsi_host, NULL)) {
740                 SBP2_ERR("failed to add scsi host");
741                 scsi_host_put(hi->scsi_host);
742                 hpsb_destroy_hostinfo(&sbp2_highlevel, host);
743         }
744
745         return hi;
746 }
747
748
749 /*
750  * This function is called when a host is removed.
751  */
752 static void sbp2_remove_host(struct hpsb_host *host)
753 {
754         struct sbp2scsi_host_info *hi;
755
756         SBP2_DEBUG("sbp2_remove_host");
757
758         hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
759
760         if (hi) {
761                 scsi_remove_host(hi->scsi_host);
762                 scsi_host_put(hi->scsi_host);
763         }
764 }
765
766 static int sbp2_start_ud(struct sbp2scsi_host_info *hi, struct unit_directory *ud)
767 {
768         struct scsi_id_instance_data *scsi_id;
769         struct scsi_id_group *scsi_group;
770         struct list_head *lh, *next;
771
772         SBP2_DEBUG("sbp2_start_ud");
773
774         scsi_group = kmalloc(sizeof(*scsi_group), GFP_KERNEL);
775         if (!scsi_group) {
776                 SBP2_ERR ("Could not allocate memory for scsi_group");
777                 return -ENOMEM;
778         }
779
780         INIT_LIST_HEAD(&scsi_group->scsi_id_list);
781         ud->device.driver_data = scsi_group;
782         sbp2_parse_unit_directory(scsi_group, ud);
783
784         list_for_each_safe (lh, next, &scsi_group->scsi_id_list) {
785                 scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
786
787                 scsi_id->ne = ud->ne;
788                 scsi_id->hi = hi;
789                 scsi_id->speed_code = IEEE1394_SPEED_100;
790                 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
791                 atomic_set(&scsi_id->sbp2_login_complete, 0);
792                 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
793                 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
794                 scsi_id->sbp2_command_orb_lock = SPIN_LOCK_UNLOCKED;
795
796                 sbp2_start_device(scsi_id);
797         }
798
799         /* Check to see if any of our devices survived the ordeal */
800         if (list_empty(&scsi_group->scsi_id_list)) {
801                 kfree(scsi_group);
802                 return -ENODEV;
803         }
804
805         return 0;
806 }
807
808
809 /*
810  * This function is where we first pull the node unique ids, and then
811  * allocate memory and register a SBP-2 device.
812  */
813 static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
814 {
815         struct sbp2scsi_host_info *hi = scsi_id->hi;
816         struct scsi_device *sdev;
817         int i;
818
819         SBP2_DEBUG("sbp2_start_device");
820
821         /* Login FIFO DMA */
822         scsi_id->login_response =
823                 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_response),
824                                      &scsi_id->login_response_dma);
825         if (!scsi_id->login_response)
826                 goto alloc_fail;
827         SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
828
829         /* Query logins ORB DMA */
830         scsi_id->query_logins_orb =
831                 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_query_logins_orb),
832                                      &scsi_id->query_logins_orb_dma);
833         if (!scsi_id->query_logins_orb)
834                 goto alloc_fail;
835         SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
836
837         /* Query logins response DMA */
838         scsi_id->query_logins_response =
839                 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_query_logins_response),
840                                      &scsi_id->query_logins_response_dma);
841         if (!scsi_id->query_logins_response)
842                 goto alloc_fail;
843         SBP2_DMA_ALLOC("consistent DMA region for query logins response");
844
845         /* Reconnect ORB DMA */
846         scsi_id->reconnect_orb =
847                 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_reconnect_orb),
848                                      &scsi_id->reconnect_orb_dma);
849         if (!scsi_id->reconnect_orb)
850                 goto alloc_fail;
851         SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
852
853         /* Logout ORB DMA */
854         scsi_id->logout_orb =
855                 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_logout_orb),
856                                      &scsi_id->logout_orb_dma);
857         if (!scsi_id->logout_orb)
858                 goto alloc_fail;
859         SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
860
861         /* Login ORB DMA */
862         scsi_id->login_orb =
863                 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_orb),
864                                      &scsi_id->login_orb_dma);
865         if (!scsi_id->login_orb) {
866 alloc_fail:
867                 if (scsi_id->query_logins_response) {
868                         pci_free_consistent(hi->host->pdev,
869                                             sizeof(struct sbp2_query_logins_response),
870                                             scsi_id->query_logins_response,
871                                             scsi_id->query_logins_response_dma);
872                         SBP2_DMA_FREE("query logins response DMA");
873                 }
874
875                 if (scsi_id->query_logins_orb) {
876                         pci_free_consistent(hi->host->pdev,
877                                             sizeof(struct sbp2_query_logins_orb),
878                                             scsi_id->query_logins_orb,
879                                             scsi_id->query_logins_orb_dma);
880                         SBP2_DMA_FREE("query logins ORB DMA");
881                 }
882         
883                 if (scsi_id->logout_orb) {
884                         pci_free_consistent(hi->host->pdev,
885                                         sizeof(struct sbp2_logout_orb),
886                                         scsi_id->logout_orb,
887                                         scsi_id->logout_orb_dma);
888                         SBP2_DMA_FREE("logout ORB DMA");
889                 }
890
891                 if (scsi_id->reconnect_orb) {
892                         pci_free_consistent(hi->host->pdev,
893                                         sizeof(struct sbp2_reconnect_orb),
894                                         scsi_id->reconnect_orb,
895                                         scsi_id->reconnect_orb_dma);
896                         SBP2_DMA_FREE("reconnect ORB DMA");
897                 }
898
899                 if (scsi_id->login_response) {
900                         pci_free_consistent(hi->host->pdev,
901                                         sizeof(struct sbp2_login_response),
902                                         scsi_id->login_response,
903                                         scsi_id->login_response_dma);
904                         SBP2_DMA_FREE("login FIFO DMA");
905                 }
906
907                 kfree(scsi_id);
908
909                 list_del(&scsi_id->list);
910
911                 SBP2_ERR ("Could not allocate memory for scsi_id");
912
913                 return -ENOMEM;
914         }
915         SBP2_DMA_ALLOC("consistent DMA region for login ORB");
916
917         /*
918          * Find an empty spot to stick our scsi id instance data. 
919          */
920         for (i = 0; i < hi->scsi_host->max_id; i++) {
921                 if (!hi->scsi_id[i]) {
922                         hi->scsi_id[i] = scsi_id;
923                         scsi_id->id = i;
924                         SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", (unsigned int) i);
925                         break;
926                 }
927         }
928
929         /*
930          * Create our command orb pool
931          */
932         if (sbp2util_create_command_orb_pool(scsi_id)) {
933                 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
934                 sbp2_remove_device(scsi_id);
935                 return -ENOMEM;
936         }
937
938         /*
939          * Make sure we are not out of space
940          */
941         if (i == hi->scsi_host->max_id) {
942                 SBP2_ERR("No slots left for SBP-2 device");
943                 sbp2_remove_device(scsi_id);
944                 return -EBUSY;
945         }
946
947         /*
948          * Login to the sbp-2 device
949          */
950         if (sbp2_login_device(scsi_id)) {
951                 /* Login failed, just remove the device. */
952                 sbp2_remove_device(scsi_id);
953                 return -EBUSY;
954         }
955
956         /*
957          * Set max retries to something large on the device
958          */
959         sbp2_set_busy_timeout(scsi_id);
960         
961         /*
962          * Do a SBP-2 fetch agent reset
963          */
964         sbp2_agent_reset(scsi_id, 1);
965         
966         /*
967          * Get the max speed and packet size that we can use
968          */
969         sbp2_max_speed_and_size(scsi_id);
970
971         /* Add this device to the scsi layer now */
972         sdev = scsi_add_device(hi->scsi_host, 0, scsi_id->id, 0);
973         if (IS_ERR(sdev)) {
974                 SBP2_ERR("scsi_add_device failed");
975                 return PTR_ERR(sdev);
976         }
977
978         return 0;
979 }
980
981 /*
982  * This function removes an sbp2 device from the sbp2scsi_host_info struct.
983  */
984 static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
985 {
986         struct sbp2scsi_host_info *hi = scsi_id->hi;
987         struct scsi_device *sdev = scsi_find_device(hi->scsi_host, 0, scsi_id->id, 0);
988
989         SBP2_DEBUG("sbp2_remove_device");
990
991         /* Complete any pending commands with selection timeout */
992         sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
993
994         /* Remove it from the scsi layer now */
995         if (sdev && scsi_remove_device(sdev))
996                 SBP2_ERR("scsi_remove_device failed");
997
998         sbp2util_remove_command_orb_pool(scsi_id);
999
1000         hi->scsi_id[scsi_id->id] = NULL;
1001
1002         if (scsi_id->login_response) {
1003                 pci_free_consistent(hi->host->pdev,
1004                                     sizeof(struct sbp2_login_response),
1005                                     scsi_id->login_response,
1006                                     scsi_id->login_response_dma);
1007                 SBP2_DMA_FREE("single login FIFO");
1008         }
1009
1010         if (scsi_id->login_orb) {
1011                 pci_free_consistent(hi->host->pdev,
1012                                     sizeof(struct sbp2_login_orb),
1013                                     scsi_id->login_orb,
1014                                     scsi_id->login_orb_dma);
1015                 SBP2_DMA_FREE("single login ORB");
1016         }
1017
1018         if (scsi_id->reconnect_orb) {
1019                 pci_free_consistent(hi->host->pdev,
1020                                     sizeof(struct sbp2_reconnect_orb),
1021                                     scsi_id->reconnect_orb,
1022                                     scsi_id->reconnect_orb_dma);
1023                 SBP2_DMA_FREE("single reconnect orb");
1024         }
1025
1026         if (scsi_id->logout_orb) {
1027                 pci_free_consistent(hi->host->pdev,
1028                                     sizeof(struct sbp2_logout_orb),
1029                                     scsi_id->logout_orb,
1030                                     scsi_id->logout_orb_dma);
1031                 SBP2_DMA_FREE("single logout orb");
1032         }
1033
1034         if (scsi_id->query_logins_orb) {
1035                 pci_free_consistent(hi->host->pdev,
1036                                     sizeof(struct sbp2_query_logins_orb),
1037                                     scsi_id->query_logins_orb,
1038                                     scsi_id->query_logins_orb_dma);
1039                 SBP2_DMA_FREE("single query logins orb");
1040         }
1041
1042         if (scsi_id->query_logins_response) {
1043                 pci_free_consistent(hi->host->pdev,
1044                                     sizeof(struct sbp2_query_logins_response),
1045                                     scsi_id->query_logins_response,
1046                                     scsi_id->query_logins_response_dma);
1047                 SBP2_DMA_FREE("single query logins data");
1048         }
1049
1050         SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->id);
1051
1052         list_del(&scsi_id->list);
1053
1054         kfree(scsi_id);
1055 }
1056
1057 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1058 /*
1059  * This function deals with physical dma write requests (for adapters that do not support
1060  * physical dma in hardware). Mostly just here for debugging...
1061  */
1062 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid, int destid, quadlet_t *data,
1063                                      u64 addr, unsigned int length, u16 flags)
1064 {
1065
1066         /*
1067          * Manually put the data in the right place.
1068          */
1069         memcpy(bus_to_virt((u32)addr), data, length);
1070         sbp2util_packet_dump(data, length, "sbp2 phys dma write by device", (u32)addr);
1071         return(RCODE_COMPLETE);
1072 }
1073
1074 /*
1075  * This function deals with physical dma read requests (for adapters that do not support
1076  * physical dma in hardware). Mostly just here for debugging...
1077  */
1078 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid, quadlet_t *data,
1079                                     u64 addr, unsigned int length, u16 flags)
1080 {
1081
1082         /*
1083          * Grab data from memory and send a read response.
1084          */
1085         memcpy(data, bus_to_virt((u32)addr), length);
1086         sbp2util_packet_dump(data, length, "sbp2 phys dma read by device", (u32)addr);
1087         return(RCODE_COMPLETE);
1088 }
1089 #endif
1090
1091
1092 /**************************************
1093  * SBP-2 protocol related section
1094  **************************************/
1095
1096 /*
1097  * This function determines if we should convert scsi commands for a particular sbp2 device type
1098  */
1099 static __inline__ int sbp2_command_conversion_device_type(u8 device_type)
1100 {
1101         return (((device_type == TYPE_DISK) ||
1102                  (device_type == TYPE_SDAD) ||
1103                  (device_type == TYPE_ROM)) ? 1:0);
1104 }
1105
1106 /*
1107  * This function queries the device for the maximum concurrent logins it
1108  * supports.
1109  */
1110 static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1111 {
1112         struct sbp2scsi_host_info *hi = scsi_id->hi;
1113         quadlet_t data[2];
1114         int max_logins;
1115         int active_logins;
1116
1117         SBP2_DEBUG("sbp2_query_logins");
1118
1119         scsi_id->query_logins_orb->reserved1 = 0x0;
1120         scsi_id->query_logins_orb->reserved2 = 0x0;
1121
1122         scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1123         scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1124         SBP2_DEBUG("sbp2_query_logins: query_response_hi/lo initialized");
1125
1126         scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(QUERY_LOGINS_REQUEST);
1127         scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1128         if (scsi_id->sbp2_device_type_and_lun != SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
1129                 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
1130                 SBP2_DEBUG("sbp2_query_logins: set lun to %d",
1131                            ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun));
1132         }
1133         SBP2_DEBUG("sbp2_query_logins: lun_misc initialized");
1134
1135         scsi_id->query_logins_orb->reserved_resp_length =
1136                 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
1137         SBP2_DEBUG("sbp2_query_logins: reserved_resp_length initialized");
1138
1139         scsi_id->query_logins_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1140                                                     SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1141         scsi_id->query_logins_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1142                                                      SBP2_STATUS_FIFO_ADDRESS_HI);
1143         SBP2_DEBUG("sbp2_query_logins: status FIFO initialized");
1144
1145         sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1146
1147         SBP2_DEBUG("sbp2_query_logins: orb byte-swapped");
1148
1149         sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
1150                              "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
1151
1152         memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1153         memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1154
1155         SBP2_DEBUG("sbp2_query_logins: query_logins_response/status FIFO memset");
1156
1157         data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1158         data[1] = scsi_id->query_logins_orb_dma;
1159         sbp2util_cpu_to_be32_buffer(data, 8);
1160
1161         atomic_set(&scsi_id->sbp2_login_complete, 0);
1162
1163         SBP2_DEBUG("sbp2_query_logins: prepared to write");
1164         hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1165         SBP2_DEBUG("sbp2_query_logins: written");
1166
1167         if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 2*HZ)) {
1168                 SBP2_ERR("Error querying logins to SBP-2 device - timed out");
1169                 return(-EIO);
1170         }
1171
1172         if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1173                 SBP2_ERR("Error querying logins to SBP-2 device - timed out");
1174                 return(-EIO);
1175         }
1176
1177         if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1178             STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1179             STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1180
1181                 SBP2_ERR("Error querying logins to SBP-2 device - timed out");
1182                 return(-EIO);
1183         }
1184
1185         sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1186
1187         SBP2_DEBUG("length_max_logins = %x",
1188                    (unsigned int)scsi_id->query_logins_response->length_max_logins);
1189
1190         SBP2_INFO("Query logins to SBP-2 device successful");
1191
1192         max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
1193         SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1194                                                                                 
1195         active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
1196         SBP2_INFO("Number of active logins: %d", active_logins);
1197                                                                                 
1198         if (active_logins >= max_logins) {
1199                 return(-EIO);
1200         }
1201                                                                                 
1202         return 0;
1203 }
1204
1205 /*
1206  * This function is called in order to login to a particular SBP-2 device,
1207  * after a bus reset.
1208  */
1209 static int sbp2_login_device(struct scsi_id_instance_data *scsi_id) 
1210 {
1211         struct sbp2scsi_host_info *hi = scsi_id->hi;
1212         quadlet_t data[2];
1213
1214         SBP2_DEBUG("sbp2_login_device");
1215
1216         if (!scsi_id->login_orb) {
1217                 SBP2_DEBUG("sbp2_login_device: login_orb not alloc'd!");
1218                 return(-EIO);
1219         }
1220
1221         if (!exclusive_login) {
1222                 if (sbp2_query_logins(scsi_id)) {
1223                         SBP2_ERR("Device does not support any more concurrent logins");
1224                         return(-EIO);
1225                 }
1226         }
1227
1228         /* Set-up login ORB, assume no password */
1229         scsi_id->login_orb->password_hi = 0; 
1230         scsi_id->login_orb->password_lo = 0;
1231         SBP2_DEBUG("sbp2_login_device: password_hi/lo initialized");
1232
1233         scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1234         scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1235         SBP2_DEBUG("sbp2_login_device: login_response_hi/lo initialized");
1236
1237         scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(LOGIN_REQUEST);
1238         scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0);   /* One second reconnect time */
1239         scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login);     /* Exclusive access to device */
1240         scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1);      /* Notify us of login complete */
1241         /* Set the lun if we were able to pull it from the device's unit directory */
1242         if (scsi_id->sbp2_device_type_and_lun != SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
1243                 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
1244                 SBP2_DEBUG("sbp2_query_logins: set lun to %d",
1245                            ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun));
1246         }
1247         SBP2_DEBUG("sbp2_login_device: lun_misc initialized");
1248
1249         scsi_id->login_orb->passwd_resp_lengths =
1250                 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1251         SBP2_DEBUG("sbp2_login_device: passwd_resp_lengths initialized");
1252
1253         scsi_id->login_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO + 
1254                                              SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1255         scsi_id->login_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1256                                               SBP2_STATUS_FIFO_ADDRESS_HI);
1257         SBP2_DEBUG("sbp2_login_device: status FIFO initialized");
1258
1259         /*
1260          * Byte swap ORB if necessary
1261          */
1262         sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1263
1264         SBP2_DEBUG("sbp2_login_device: orb byte-swapped");
1265
1266         sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb), 
1267                              "sbp2 login orb", scsi_id->login_orb_dma);
1268
1269         /*
1270          * Initialize login response and status fifo
1271          */
1272         memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1273         memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1274
1275         SBP2_DEBUG("sbp2_login_device: login_response/status FIFO memset");
1276
1277         /*
1278          * Ok, let's write to the target's management agent register
1279          */
1280         data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1281         data[1] = scsi_id->login_orb_dma;
1282         sbp2util_cpu_to_be32_buffer(data, 8);
1283
1284         atomic_set(&scsi_id->sbp2_login_complete, 0);
1285
1286         SBP2_DEBUG("sbp2_login_device: prepared to write to %08x",
1287                    (unsigned int)scsi_id->sbp2_management_agent_addr);
1288         hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1289         SBP2_DEBUG("sbp2_login_device: written");
1290
1291         /*
1292          * Wait for login status (up to 20 seconds)... 
1293          */
1294         if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 20*HZ)) {
1295                 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1296                 return(-EIO);
1297         }
1298
1299         /*
1300          * Sanity. Make sure status returned matches login orb.
1301          */
1302         if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1303                 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1304                 return(-EIO);
1305         }
1306
1307         /*
1308          * Check status
1309          */
1310         if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1311             STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1312             STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1313
1314                 SBP2_ERR("Error logging into SBP-2 device - login failed");
1315                 return(-EIO);
1316         }
1317
1318         /*
1319          * Byte swap the login response, for use when reconnecting or
1320          * logging out.
1321          */
1322         sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1323
1324         /*
1325          * Grab our command block agent address from the login response.
1326          */
1327         SBP2_DEBUG("command_block_agent_hi = %x",
1328                    (unsigned int)scsi_id->login_response->command_block_agent_hi);
1329         SBP2_DEBUG("command_block_agent_lo = %x",
1330                    (unsigned int)scsi_id->login_response->command_block_agent_lo);
1331
1332         scsi_id->sbp2_command_block_agent_addr =
1333                 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1334         scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1335         scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1336
1337         SBP2_INFO("Logged into SBP-2 device");
1338
1339         return(0);
1340
1341 }
1342
1343 /*
1344  * This function is called in order to logout from a particular SBP-2
1345  * device, usually called during driver unload.
1346  */
1347 static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id) 
1348 {
1349         struct sbp2scsi_host_info *hi = scsi_id->hi;
1350         quadlet_t data[2];
1351
1352         SBP2_DEBUG("sbp2_logout_device");
1353
1354         /*
1355          * Set-up logout ORB
1356          */
1357         scsi_id->logout_orb->reserved1 = 0x0;
1358         scsi_id->logout_orb->reserved2 = 0x0;
1359         scsi_id->logout_orb->reserved3 = 0x0;
1360         scsi_id->logout_orb->reserved4 = 0x0;
1361
1362         scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(LOGOUT_REQUEST);
1363         scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1364
1365         /* Notify us when complete */
1366         scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1367
1368         scsi_id->logout_orb->reserved5 = 0x0;
1369         scsi_id->logout_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO + 
1370                                               SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1371         scsi_id->logout_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1372                                                SBP2_STATUS_FIFO_ADDRESS_HI);
1373
1374         /*
1375          * Byte swap ORB if necessary
1376          */
1377         sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1378
1379         sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb), 
1380                              "sbp2 logout orb", scsi_id->logout_orb_dma);
1381
1382         /*
1383          * Ok, let's write to the target's management agent register
1384          */
1385         data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1386         data[1] = scsi_id->logout_orb_dma;
1387         sbp2util_cpu_to_be32_buffer(data, 8);
1388
1389         atomic_set(&scsi_id->sbp2_login_complete, 0);
1390
1391         hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1392
1393         /* Wait for device to logout...1 second. */
1394         sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ);
1395
1396         SBP2_INFO("Logged out of SBP-2 device");
1397
1398         return(0);
1399
1400 }
1401
1402 /*
1403  * This function is called in order to reconnect to a particular SBP-2
1404  * device, after a bus reset.
1405  */
1406 static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id) 
1407 {
1408         struct sbp2scsi_host_info *hi = scsi_id->hi;
1409         quadlet_t data[2];
1410
1411         SBP2_DEBUG("sbp2_reconnect_device");
1412
1413         /*
1414          * Set-up reconnect ORB
1415          */
1416         scsi_id->reconnect_orb->reserved1 = 0x0;
1417         scsi_id->reconnect_orb->reserved2 = 0x0;
1418         scsi_id->reconnect_orb->reserved3 = 0x0;
1419         scsi_id->reconnect_orb->reserved4 = 0x0;
1420
1421         scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(RECONNECT_REQUEST);
1422         scsi_id->reconnect_orb->login_ID_misc |=
1423                 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1424
1425         /* Notify us when complete */
1426         scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1427
1428         scsi_id->reconnect_orb->reserved5 = 0x0;
1429         scsi_id->reconnect_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO + 
1430                                                  SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1431         scsi_id->reconnect_orb->status_FIFO_hi =
1432                 (ORB_SET_NODE_ID(hi->host->node_id) | SBP2_STATUS_FIFO_ADDRESS_HI);
1433
1434         /*
1435          * Byte swap ORB if necessary
1436          */
1437         sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1438
1439         sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb), 
1440                              "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
1441
1442         /*
1443          * Initialize status fifo
1444          */
1445         memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1446
1447         /*
1448          * Ok, let's write to the target's management agent register
1449          */
1450         data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1451         data[1] = scsi_id->reconnect_orb_dma;
1452         sbp2util_cpu_to_be32_buffer(data, 8);
1453
1454         atomic_set(&scsi_id->sbp2_login_complete, 0);
1455
1456         hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1457
1458         /*
1459          * Wait for reconnect status (up to 1 second)...
1460          */
1461         if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) {
1462                 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1463                 return(-EIO);
1464         }
1465
1466         /*
1467          * Sanity. Make sure status returned matches reconnect orb.
1468          */
1469         if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1470                 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1471                 return(-EIO);
1472         }
1473
1474         /*
1475          * Check status
1476          */
1477         if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1478             STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1479             STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1480
1481                 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect failed");
1482                 return(-EIO);
1483         }
1484
1485         SBP2_INFO("Reconnected to SBP-2 device");
1486
1487         return(0);
1488
1489 }
1490
1491 /*
1492  * This function is called in order to set the busy timeout (number of
1493  * retries to attempt) on the sbp2 device. 
1494  */
1495 static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1496 {
1497         quadlet_t data;
1498
1499         SBP2_DEBUG("sbp2_set_busy_timeout");
1500
1501         /*
1502          * Ok, let's write to the target's busy timeout register
1503          */
1504         data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1505
1506         if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4)) {
1507                 SBP2_ERR("sbp2_set_busy_timeout error");
1508         }
1509
1510         return(0);
1511 }
1512
1513 /*
1514  * This function is called to parse sbp2 device's config rom unit
1515  * directory. Used to determine things like sbp2 management agent offset,
1516  * and command set used (SCSI or RBC). 
1517  */
1518 static void sbp2_parse_unit_directory(struct scsi_id_group *scsi_group,
1519                                       struct unit_directory *ud)
1520 {
1521         struct scsi_id_instance_data *scsi_id;
1522         struct list_head *lh;
1523         u64 management_agent_addr;
1524         u32 command_set_spec_id, command_set, unit_characteristics,
1525                 firmware_revision, workarounds;
1526         int i;
1527
1528         SBP2_DEBUG("sbp2_parse_unit_directory");
1529
1530         management_agent_addr = 0x0;
1531         command_set_spec_id = 0x0;
1532         command_set = 0x0;
1533         unit_characteristics = 0x0;
1534         firmware_revision = 0x0;
1535
1536         /* Handle different fields in the unit directory, based on keys */
1537         for (i = 0; i < ud->length; i++) {
1538                 switch (CONFIG_ROM_KEY(ud->quadlets[i])) {
1539                 case SBP2_CSR_OFFSET_KEY:
1540                         /* Save off the management agent address */
1541                         management_agent_addr =
1542                                 CSR_REGISTER_BASE + 
1543                                 (CONFIG_ROM_VALUE(ud->quadlets[i]) << 2);
1544
1545                         SBP2_DEBUG("sbp2_management_agent_addr = %x",
1546                                    (unsigned int) management_agent_addr);
1547                         break;
1548
1549                 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1550                         /* Command spec organization */
1551                         command_set_spec_id
1552                                 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1553                         SBP2_DEBUG("sbp2_command_set_spec_id = %x",
1554                                    (unsigned int) command_set_spec_id);
1555                         break;
1556
1557                 case SBP2_COMMAND_SET_KEY:
1558                         /* Command set used by sbp2 device */
1559                         command_set = CONFIG_ROM_VALUE(ud->quadlets[i]);
1560                         SBP2_DEBUG("sbp2_command_set = %x",
1561                                    (unsigned int) command_set);
1562                         break;
1563
1564                 case SBP2_UNIT_CHARACTERISTICS_KEY:
1565                         /*
1566                          * Unit characterisitcs (orb related stuff
1567                          * that I'm not yet paying attention to)
1568                          */
1569                         unit_characteristics
1570                                 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1571                         SBP2_DEBUG("sbp2_unit_characteristics = %x",
1572                                    (unsigned int) unit_characteristics);
1573                         break;
1574
1575                 case SBP2_DEVICE_TYPE_AND_LUN_KEY:
1576                         /*
1577                          * Device type and lun (used for
1578                          * detemining type of sbp2 device)
1579                          */
1580                         scsi_id = kmalloc(sizeof(*scsi_id), GFP_KERNEL);
1581                         if (!scsi_id) {
1582                                 SBP2_ERR("Out of memory adding scsi_id, not all LUN's will be added");
1583                                 break;
1584                         }
1585                         memset(scsi_id, 0, sizeof(*scsi_id));
1586
1587                         scsi_id->sbp2_device_type_and_lun
1588                                 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1589                         SBP2_DEBUG("sbp2_device_type_and_lun = %x",
1590                                    (unsigned int) scsi_id->sbp2_device_type_and_lun);
1591                         list_add_tail(&scsi_id->list, &scsi_group->scsi_id_list);
1592                         break;
1593
1594                 case SBP2_FIRMWARE_REVISION_KEY:
1595                         /* Firmware revision */
1596                         firmware_revision
1597                                 = CONFIG_ROM_VALUE(ud->quadlets[i]);
1598                         if (force_inquiry_hack)
1599                                 SBP2_INFO("sbp2_firmware_revision = %x",
1600                                    (unsigned int) firmware_revision);
1601                         else    SBP2_DEBUG("sbp2_firmware_revision = %x",
1602                                    (unsigned int) firmware_revision);
1603                         break;
1604
1605                 default:
1606                         break;
1607                 }
1608         }
1609
1610         /* This is the start of our broken device checking. We try to hack
1611          * around oddities and known defects.  */
1612         workarounds = 0x0;
1613
1614         /* If the vendor id is 0xa0b8 (Symbios vendor id), then we have a
1615          * bridge with 128KB max transfer size limitation. For sanity, we
1616          * only voice this when the current max_sectors setting
1617          * exceeds the 128k limit. By default, that is not the case.
1618          *
1619          * It would be really nice if we could detect this before the scsi
1620          * host gets initialized. That way we can down-force the
1621          * max_sectors to account for it. That is not currently
1622          * possible.  */
1623         if ((firmware_revision & 0xffff00) ==
1624                         SBP2_128KB_BROKEN_FIRMWARE &&
1625                         (max_sectors * 512) > (128*1024)) {
1626                 SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB max transfer size.",
1627                                 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid));
1628                 SBP2_WARN("WARNING: Current max_sectors setting is larger than 128KB (%d sectors)!",
1629                                 max_sectors);
1630                 workarounds |= SBP2_BREAKAGE_128K_MAX_TRANSFER;
1631         }
1632
1633         /* Check for a blacklisted set of devices that require us to force
1634          * a 36 byte host inquiry. This can be overriden as a module param
1635          * (to force all hosts).  */
1636         for (i = 0; i < NUM_BROKEN_INQUIRY_DEVS; i++) {
1637                 if ((firmware_revision & 0xffff00) ==
1638                                 sbp2_broken_inquiry_list[i]) {
1639                         SBP2_WARN("Node " NODE_BUS_FMT ": Using 36byte inquiry workaround",
1640                                         NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid));
1641                         workarounds |= SBP2_BREAKAGE_INQUIRY_HACK;
1642                         break; /* No need to continue. */
1643                 }
1644         }
1645
1646         /* If this is a logical unit directory entry, process the parent
1647          * to get the common values. */
1648         if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1649                 struct unit_directory *parent_ud =
1650                         container_of(ud->device.parent, struct unit_directory, device);
1651                 sbp2_parse_unit_directory(scsi_group, parent_ud);
1652         } else {
1653                 /* If our list is empty, add a base scsi_id (happens in a normal
1654                  * case where there is no logical_unit_number entry */
1655                 if (list_empty(&scsi_group->scsi_id_list)) {
1656                         scsi_id = kmalloc(sizeof(*scsi_id), GFP_KERNEL);
1657                         if (!scsi_id) {
1658                                 SBP2_ERR("Out of memory adding scsi_id");
1659                                 return;
1660                         }
1661                         memset(scsi_id, 0, sizeof(*scsi_id));
1662
1663                         scsi_id->sbp2_device_type_and_lun = SBP2_DEVICE_TYPE_LUN_UNINITIALIZED;
1664                         list_add_tail(&scsi_id->list, &scsi_group->scsi_id_list);
1665                 }
1666
1667                 /* Update the generic fields in all the LUN's */
1668                 list_for_each (lh, &scsi_group->scsi_id_list) {
1669                         scsi_id = list_entry(lh, struct scsi_id_instance_data, list);
1670
1671                         scsi_id->sbp2_management_agent_addr = management_agent_addr;
1672                         scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
1673                         scsi_id->sbp2_command_set = command_set;
1674                         scsi_id->sbp2_unit_characteristics = unit_characteristics;
1675                         scsi_id->sbp2_firmware_revision = firmware_revision;
1676                         scsi_id->workarounds = workarounds;
1677                 }
1678         }
1679 }
1680
1681 /*
1682  * This function is called in order to determine the max speed and packet
1683  * size we can use in our ORBs. Note, that we (the driver and host) only
1684  * initiate the transaction. The SBP-2 device actually transfers the data
1685  * (by reading from the DMA area we tell it). This means that the SBP-2
1686  * device decides the actual maximum data it can transfer. We just tell it
1687  * the speed that it needs to use, and the max_rec the host supports, and
1688  * it takes care of the rest.
1689  */
1690 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1691 {
1692         struct sbp2scsi_host_info *hi = scsi_id->hi;
1693
1694         SBP2_DEBUG("sbp2_max_speed_and_size");
1695
1696         /* Initial setting comes from the hosts speed map */
1697         scsi_id->speed_code = hi->host->speed_map[NODEID_TO_NODE(hi->host->node_id) * 64
1698                                                   + NODEID_TO_NODE(scsi_id->ne->nodeid)];
1699
1700         /* Bump down our speed if the user requested it */
1701         if (scsi_id->speed_code > max_speed) {
1702                 scsi_id->speed_code = max_speed;
1703                 SBP2_ERR("Forcing SBP-2 max speed down to %s",
1704                          hpsb_speedto_str[scsi_id->speed_code]);
1705         }
1706
1707         /* Payload size is the lesser of what our speed supports and what
1708          * our host supports.  */
1709         scsi_id->max_payload_size = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1710                                         (u8)(((be32_to_cpu(hi->host->csr.rom[2]) >> 12) & 0xf) - 1));
1711
1712         SBP2_ERR("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1713                  NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1714                  hpsb_speedto_str[scsi_id->speed_code],
1715                  1 << ((u32)scsi_id->max_payload_size + 2));
1716
1717         return(0);
1718 }
1719
1720 /*
1721  * This function is called in order to perform a SBP-2 agent reset. 
1722  */
1723 static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait) 
1724 {
1725         struct sbp2scsi_host_info *hi = scsi_id->hi;
1726         struct hpsb_packet *packet;
1727         quadlet_t data;
1728         
1729         SBP2_DEBUG("sbp2_agent_reset");
1730
1731         /*
1732          * Ok, let's write to the target's management agent register
1733          */
1734         data = ntohl(SBP2_AGENT_RESET_DATA);
1735         packet = sbp2util_allocate_write_packet(hi, scsi_id->ne,
1736                                                 scsi_id->sbp2_command_block_agent_addr +
1737                                                 SBP2_AGENT_RESET_OFFSET,
1738                                                 4, &data);
1739
1740         if (!packet) {
1741                 SBP2_ERR("sbp2util_allocate_write_packet failed");
1742                 return(-ENOMEM);
1743         }
1744
1745         if (!hpsb_send_packet(packet)) {
1746                 SBP2_ERR("hpsb_send_packet failed");
1747                 sbp2_free_packet(packet); 
1748                 return(-EIO);
1749         }
1750
1751         if (wait) {
1752                 down(&packet->state_change);
1753                 down(&packet->state_change);
1754         }
1755
1756         /*
1757          * Need to make sure orb pointer is written on next command
1758          */
1759         scsi_id->last_orb = NULL;
1760
1761         return(0);
1762 }
1763
1764 /*
1765  * This function is called to create the actual command orb and s/g list
1766  * out of the scsi command itself.
1767  */
1768 static int sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1769                                    struct sbp2_command_info *command,
1770                                    unchar *scsi_cmd,
1771                                    unsigned int scsi_use_sg,
1772                                    unsigned int scsi_request_bufflen,
1773                                    void *scsi_request_buffer, 
1774                                    unsigned char scsi_dir)
1775 {
1776         struct sbp2scsi_host_info *hi = scsi_id->hi;
1777         struct scatterlist *sgpnt = (struct scatterlist *) scsi_request_buffer;
1778         struct sbp2_command_orb *command_orb = &command->command_orb;
1779         struct sbp2_unrestricted_page_table *scatter_gather_element =
1780                 &command->scatter_gather_element[0];
1781         int dma_dir = scsi_to_pci_dma_dir (scsi_dir);
1782         u32 sg_count, sg_len, orb_direction;
1783         dma_addr_t sg_addr;
1784         int i;
1785
1786         /*
1787          * Set-up our command ORB..
1788          *
1789          * NOTE: We're doing unrestricted page tables (s/g), as this is
1790          * best performance (at least with the devices I have). This means
1791          * that data_size becomes the number of s/g elements, and
1792          * page_size should be zero (for unrestricted).
1793          */
1794         command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1795         command_orb->next_ORB_lo = 0x0;
1796         command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1797         command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
1798         command_orb->misc |= ORB_SET_NOTIFY(1);         /* Notify us when complete */
1799
1800         /*
1801          * Get the direction of the transfer. If the direction is unknown, then use our
1802          * goofy table as a back-up.
1803          */
1804         switch (scsi_dir) {
1805                 case SCSI_DATA_NONE:
1806                         orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1807                         break;
1808                 case SCSI_DATA_WRITE:
1809                         orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1810                         break;
1811                 case SCSI_DATA_READ:
1812                         orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1813                         break;
1814                 case SCSI_DATA_UNKNOWN:
1815                 default:
1816                         SBP2_ERR("SCSI data transfer direction not specified. "
1817                                  "Update the SBP2 direction table in sbp2.h if " 
1818                                  "necessary for your application");
1819                         print_command (scsi_cmd);
1820                         orb_direction = sbp2scsi_direction_table[*scsi_cmd];
1821                         break;
1822         }
1823
1824         /*
1825          * Set-up our pagetable stuff... unfortunately, this has become
1826          * messier than I'd like. Need to clean this up a bit.   ;-)
1827          */
1828         if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1829
1830                 SBP2_DEBUG("No data transfer");
1831
1832                 /*
1833                  * Handle no data transfer
1834                  */
1835                 command_orb->data_descriptor_hi = 0x0;
1836                 command_orb->data_descriptor_lo = 0x0;
1837                 command_orb->misc |= ORB_SET_DIRECTION(1);
1838
1839         } else if (scsi_use_sg) {
1840
1841                 SBP2_DEBUG("Use scatter/gather");
1842
1843                 /*
1844                  * Special case if only one element (and less than 64KB in size)
1845                  */
1846                 if ((scsi_use_sg == 1) && (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1847
1848                         SBP2_DEBUG("Only one s/g element");
1849                         command->dma_dir = dma_dir;
1850                         command->dma_size = sgpnt[0].length;
1851                         command->dma_type = CMD_DMA_PAGE;
1852                         command->cmd_dma = pci_map_page(hi->host->pdev,
1853                                                         sgpnt[0].page,
1854                                                         sgpnt[0].offset,
1855                                                         command->dma_size,
1856                                                         command->dma_dir);
1857                         SBP2_DMA_ALLOC("single page scatter element");
1858
1859                         command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1860                         command_orb->data_descriptor_lo = command->cmd_dma;
1861                         command_orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1862                         command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1863
1864                 } else {
1865                         int count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg, dma_dir);
1866                         SBP2_DMA_ALLOC("scatter list");
1867
1868                         command->dma_size = scsi_use_sg;
1869                         command->dma_dir = dma_dir;
1870                         command->sge_buffer = sgpnt;
1871
1872                         /* use page tables (s/g) */
1873                         command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1874                         command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1875                         command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1876                         command_orb->data_descriptor_lo = command->sge_dma;
1877
1878                         /*
1879                          * Loop through and fill out our sbp-2 page tables
1880                          * (and split up anything too large)
1881                          */
1882                         for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1883                                 sg_len = sg_dma_len(sgpnt);
1884                                 sg_addr = sg_dma_address(sgpnt);
1885                                 while (sg_len) {
1886                                         scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1887                                         if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1888                                                 scatter_gather_element[sg_count].length_segment_base_hi =  
1889                                                         PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1890                                                 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1891                                                 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1892                                         } else {
1893                                                 scatter_gather_element[sg_count].length_segment_base_hi = 
1894                                                         PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1895                                                 sg_len = 0;
1896                                         }
1897                                         sg_count++;
1898                                 }
1899                         }
1900
1901                         /* Number of page table (s/g) elements */
1902                         command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1903
1904                         sbp2util_packet_dump(scatter_gather_element, 
1905                                              (sizeof(struct sbp2_unrestricted_page_table)) * sg_count, 
1906                                              "sbp2 s/g list", command->sge_dma);
1907
1908                         /*
1909                          * Byte swap page tables if necessary
1910                          */
1911                         sbp2util_cpu_to_be32_buffer(scatter_gather_element, 
1912                                                     (sizeof(struct sbp2_unrestricted_page_table)) *
1913                                                     sg_count);
1914
1915                 }
1916
1917         } else {
1918
1919                 SBP2_DEBUG("No scatter/gather");
1920
1921                 command->dma_dir = dma_dir;
1922                 command->dma_size = scsi_request_bufflen;
1923                 command->dma_type = CMD_DMA_SINGLE;
1924                 command->cmd_dma = pci_map_single (hi->host->pdev, scsi_request_buffer,
1925                                                    command->dma_size,
1926                                                    command->dma_dir);
1927                 SBP2_DMA_ALLOC("single bulk");
1928
1929                 /*
1930                  * Handle case where we get a command w/o s/g enabled (but
1931                  * check for transfers larger than 64K)
1932                  */
1933                 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1934
1935                         command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1936                         command_orb->data_descriptor_lo = command->cmd_dma;
1937                         command_orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1938                         command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1939
1940                         /*
1941                          * Sanity, in case our direction table is not
1942                          * up-to-date
1943                          */
1944                         if (!scsi_request_bufflen) {
1945                                 command_orb->data_descriptor_hi = 0x0;
1946                                 command_orb->data_descriptor_lo = 0x0;
1947                                 command_orb->misc |= ORB_SET_DIRECTION(1);
1948                         }
1949
1950                 } else {
1951                         /*
1952                          * Need to turn this into page tables, since the
1953                          * buffer is too large.
1954                          */                     
1955                         command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1956                         command_orb->data_descriptor_lo = command->sge_dma;
1957
1958                         /* Use page tables (s/g) */
1959                         command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1960                         command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1961
1962                         /*
1963                          * fill out our sbp-2 page tables (and split up
1964                          * the large buffer)
1965                          */
1966                         sg_count = 0;
1967                         sg_len = scsi_request_bufflen;
1968                         sg_addr = command->cmd_dma;
1969                         while (sg_len) {
1970                                 scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1971                                 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1972                                         scatter_gather_element[sg_count].length_segment_base_hi = 
1973                                                 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1974                                         sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1975                                         sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1976                                 } else {
1977                                         scatter_gather_element[sg_count].length_segment_base_hi = 
1978                                                 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1979                                         sg_len = 0;
1980                                 }
1981                                 sg_count++;
1982                         }
1983
1984                         /* Number of page table (s/g) elements */
1985                         command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1986
1987                         sbp2util_packet_dump(scatter_gather_element, 
1988                                              (sizeof(struct sbp2_unrestricted_page_table)) * sg_count, 
1989                                              "sbp2 s/g list", command->sge_dma);
1990
1991                         /*
1992                          * Byte swap page tables if necessary
1993                          */
1994                         sbp2util_cpu_to_be32_buffer(scatter_gather_element, 
1995                                                     (sizeof(struct sbp2_unrestricted_page_table)) *
1996                                                      sg_count);
1997
1998                 }
1999
2000         }
2001
2002         /*
2003          * Byte swap command ORB if necessary
2004          */
2005         sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
2006
2007         /*
2008          * Put our scsi command in the command ORB
2009          */
2010         memset(command_orb->cdb, 0, 12);
2011         memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
2012
2013         return(0);
2014 }
2015  
2016 /*
2017  * This function is called in order to begin a regular SBP-2 command. 
2018  */
2019 static int sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
2020                                  struct sbp2_command_info *command)
2021 {
2022         struct sbp2scsi_host_info *hi = scsi_id->hi;
2023         struct hpsb_packet *packet;
2024         struct sbp2_command_orb *command_orb = &command->command_orb;
2025
2026         outstanding_orb_incr;
2027         SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
2028                         command_orb, global_outstanding_command_orbs);
2029
2030         pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2031                             sizeof(struct sbp2_command_orb),
2032                             PCI_DMA_BIDIRECTIONAL);
2033         pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2034                             sizeof(command->scatter_gather_element),
2035                             PCI_DMA_BIDIRECTIONAL);
2036         /*
2037          * Check to see if there are any previous orbs to use
2038          */
2039         if (scsi_id->last_orb == NULL) {
2040         
2041                 /*
2042                  * Ok, let's write to the target's management agent register
2043                  */
2044                 if (hpsb_node_entry_valid(scsi_id->ne)) {
2045
2046                         packet = sbp2util_allocate_write_packet(hi, scsi_id->ne,
2047                                                                 scsi_id->sbp2_command_block_agent_addr +
2048                                                                 SBP2_ORB_POINTER_OFFSET, 8, NULL);
2049                 
2050                         if (!packet) {
2051                                 SBP2_ERR("sbp2util_allocate_write_packet failed");
2052                                 return(-ENOMEM);
2053                         }
2054                 
2055                         packet->data[0] = ORB_SET_NODE_ID(hi->host->node_id);
2056                         packet->data[1] = command->command_orb_dma;
2057                         sbp2util_cpu_to_be32_buffer(packet->data, 8);
2058                 
2059                         SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb);
2060
2061                         if (!hpsb_send_packet(packet)) {
2062                                 SBP2_ERR("hpsb_send_packet failed");
2063                                 sbp2_free_packet(packet); 
2064                                 return(-EIO);
2065                         }
2066
2067                         SBP2_ORB_DEBUG("write command agent complete");
2068                 }
2069
2070                 scsi_id->last_orb = command_orb;
2071                 scsi_id->last_orb_dma = command->command_orb_dma;
2072
2073         } else {
2074
2075                 /*
2076                  * We have an orb already sent (maybe or maybe not
2077                  * processed) that we can append this orb to. So do so,
2078                  * and ring the doorbell. Have to be very careful
2079                  * modifying these next orb pointers, as they are accessed
2080                  * both by the sbp2 device and us.
2081                  */
2082                 scsi_id->last_orb->next_ORB_lo =
2083                         cpu_to_be32(command->command_orb_dma);
2084                 /* Tells hardware that this pointer is valid */
2085                 scsi_id->last_orb->next_ORB_hi = 0x0;
2086                 pci_dma_sync_single(hi->host->pdev, scsi_id->last_orb_dma,
2087                                     sizeof(struct sbp2_command_orb),
2088                                     PCI_DMA_BIDIRECTIONAL);
2089
2090                 /*
2091                  * Ring the doorbell
2092                  */
2093                 if (hpsb_node_entry_valid(scsi_id->ne)) {
2094                         quadlet_t data = cpu_to_be32(command->command_orb_dma);
2095
2096                         packet = sbp2util_allocate_write_packet(hi, scsi_id->ne,
2097                                         scsi_id->sbp2_command_block_agent_addr +
2098                                         SBP2_DOORBELL_OFFSET, 4, &data);
2099         
2100                         if (!packet) {
2101                                 SBP2_ERR("sbp2util_allocate_write_packet failed");
2102                                 return(-ENOMEM);
2103                         }
2104
2105                         SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb);
2106
2107                         if (!hpsb_send_packet(packet)) {
2108                                 SBP2_ERR("hpsb_send_packet failed");
2109                                 sbp2_free_packet(packet);
2110                                 return(-EIO);
2111                         }
2112                 }
2113
2114                 scsi_id->last_orb = command_orb;
2115                 scsi_id->last_orb_dma = command->command_orb_dma;
2116
2117         }
2118         return(0);
2119 }
2120
2121 /*
2122  * This function is called in order to begin a regular SBP-2 command. 
2123  */
2124 static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
2125                              Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2126 {
2127         unchar *cmd = (unchar *) SCpnt->cmnd;
2128         unsigned int request_bufflen = SCpnt->request_bufflen;
2129         struct sbp2_command_info *command;
2130
2131         SBP2_DEBUG("sbp2_send_command");
2132 #if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
2133         printk("[scsi command]\n   ");
2134         print_command (cmd);
2135 #endif
2136         SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
2137         SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2138
2139         /*
2140          * Allocate a command orb and s/g structure
2141          */
2142         command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
2143         if (!command) {
2144                 return(-EIO);
2145         }
2146
2147         /*
2148          * The scsi stack sends down a request_bufflen which does not match the
2149          * length field in the scsi cdb. This causes some sbp2 devices to 
2150          * reject this inquiry command. Fix the request_bufflen. 
2151          */
2152         if (*cmd == INQUIRY) {
2153                 if (force_inquiry_hack || scsi_id->workarounds & SBP2_BREAKAGE_INQUIRY_HACK)
2154                         request_bufflen = cmd[4] = 0x24;
2155                 else
2156                         request_bufflen = cmd[4];
2157         }
2158
2159         /*
2160          * Now actually fill in the comamnd orb and sbp2 s/g list
2161          */
2162         sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
2163                                 request_bufflen, SCpnt->request_buffer,
2164                                 SCpnt->sc_data_direction); 
2165         /*
2166          * Update our cdb if necessary (to handle sbp2 RBC command set
2167          * differences). This is where the command set hacks go!   =)
2168          */
2169         sbp2_check_sbp2_command(scsi_id, command->command_orb.cdb);
2170
2171         sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb), 
2172                              "sbp2 command orb", command->command_orb_dma);
2173
2174         /*
2175          * Initialize status fifo
2176          */
2177         memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2178
2179         /*
2180          * Link up the orb, and ring the doorbell if needed
2181          */
2182         sbp2_link_orb_command(scsi_id, command);
2183         
2184         return(0);
2185 }
2186
2187
2188 /*
2189  * This function deals with command set differences between Linux scsi
2190  * command set and sbp2 RBC command set.
2191  */
2192 static void sbp2_check_sbp2_command(struct scsi_id_instance_data *scsi_id, unchar *cmd)
2193 {
2194         unchar new_cmd[16];
2195         u8 device_type = SBP2_DEVICE_TYPE (scsi_id->sbp2_device_type_and_lun);
2196
2197         SBP2_DEBUG("sbp2_check_sbp2_command");
2198
2199         switch (*cmd) {
2200                 
2201                 case READ_6:
2202
2203                         if (sbp2_command_conversion_device_type(device_type)) {
2204
2205                                 SBP2_DEBUG("Convert READ_6 to READ_10");
2206                                             
2207                                 /*
2208                                  * Need to turn read_6 into read_10
2209                                  */
2210                                 new_cmd[0] = 0x28;
2211                                 new_cmd[1] = (cmd[1] & 0xe0);
2212                                 new_cmd[2] = 0x0;
2213                                 new_cmd[3] = (cmd[1] & 0x1f);
2214                                 new_cmd[4] = cmd[2];
2215                                 new_cmd[5] = cmd[3];
2216                                 new_cmd[6] = 0x0;
2217                                 new_cmd[7] = 0x0;
2218                                 new_cmd[8] = cmd[4];
2219                                 new_cmd[9] = cmd[5];
2220         
2221                                 memcpy(cmd, new_cmd, 10);
2222
2223                         }
2224
2225                         break;
2226
2227                 case WRITE_6:
2228
2229                         if (sbp2_command_conversion_device_type(device_type)) {
2230
2231                                 SBP2_DEBUG("Convert WRITE_6 to WRITE_10");
2232         
2233                                 /*
2234                                  * Need to turn write_6 into write_10
2235                                  */
2236                                 new_cmd[0] = 0x2a;
2237                                 new_cmd[1] = (cmd[1] & 0xe0);
2238                                 new_cmd[2] = 0x0;
2239                                 new_cmd[3] = (cmd[1] & 0x1f);
2240                                 new_cmd[4] = cmd[2];
2241                                 new_cmd[5] = cmd[3];
2242                                 new_cmd[6] = 0x0;
2243                                 new_cmd[7] = 0x0;
2244                                 new_cmd[8] = cmd[4];
2245                                 new_cmd[9] = cmd[5];
2246         
2247                                 memcpy(cmd, new_cmd, 10);
2248
2249                         }
2250
2251                         break;
2252
2253                 case MODE_SENSE:
2254
2255                         if (sbp2_command_conversion_device_type(device_type)) {
2256
2257                                 SBP2_DEBUG("Convert MODE_SENSE_6 to MODE_SENSE_10");
2258
2259                                 /*
2260                                  * Need to turn mode_sense_6 into mode_sense_10
2261                                  */
2262                                 new_cmd[0] = 0x5a;
2263                                 new_cmd[1] = cmd[1];
2264                                 new_cmd[2] = cmd[2];
2265                                 new_cmd[3] = 0x0;
2266                                 new_cmd[4] = 0x0;
2267                                 new_cmd[5] = 0x0;
2268                                 new_cmd[6] = 0x0;
2269                                 new_cmd[7] = 0x0;
2270                                 new_cmd[8] = cmd[4];
2271                                 new_cmd[9] = cmd[5];
2272         
2273                                 memcpy(cmd, new_cmd, 10);
2274
2275                         }
2276
2277                         break;
2278
2279                 case MODE_SELECT:
2280
2281                         /*
2282                          * TODO. Probably need to change mode select to 10 byte version
2283                          */
2284
2285                 default:
2286                         break;
2287         }
2288
2289         return;
2290 }
2291
2292 /*
2293  * Translates SBP-2 status into SCSI sense data for check conditions
2294  */
2295 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
2296 {
2297         SBP2_DEBUG("sbp2_status_to_sense_data");
2298
2299         /*
2300          * Ok, it's pretty ugly...   ;-)
2301          */
2302         sense_data[0] = 0x70;
2303         sense_data[1] = 0x0;
2304         sense_data[2] = sbp2_status[9];
2305         sense_data[3] = sbp2_status[12];
2306         sense_data[4] = sbp2_status[13];
2307         sense_data[5] = sbp2_status[14];
2308         sense_data[6] = sbp2_status[15];
2309         sense_data[7] = 10;
2310         sense_data[8] = sbp2_status[16];
2311         sense_data[9] = sbp2_status[17];
2312         sense_data[10] = sbp2_status[18];
2313         sense_data[11] = sbp2_status[19];
2314         sense_data[12] = sbp2_status[10];
2315         sense_data[13] = sbp2_status[11];
2316         sense_data[14] = sbp2_status[20];
2317         sense_data[15] = sbp2_status[21];
2318
2319         return(sbp2_status[8] & 0x3f);  /* return scsi status */
2320 }
2321
2322 /*
2323  * This function is called after a command is completed, in order to do any necessary SBP-2
2324  * response data translations for the SCSI stack
2325  */
2326 static void sbp2_check_sbp2_response(struct scsi_id_instance_data *scsi_id, 
2327                                      Scsi_Cmnd *SCpnt)
2328 {
2329         u8 *scsi_buf = SCpnt->request_buffer;
2330         u8 device_type = SBP2_DEVICE_TYPE (scsi_id->sbp2_device_type_and_lun);
2331
2332         SBP2_DEBUG("sbp2_check_sbp2_response");
2333
2334         switch (SCpnt->cmnd[0]) {
2335                 
2336                 case INQUIRY:
2337
2338                         /*
2339                          * If scsi_id->sbp2_device_type_and_lun is uninitialized, then fill 
2340                          * this information in from the inquiry response data. Lun is set to zero.
2341                          */
2342                         if (scsi_id->sbp2_device_type_and_lun == SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
2343                                 SBP2_DEBUG("Creating sbp2_device_type_and_lun from scsi inquiry data");
2344                                 scsi_id->sbp2_device_type_and_lun = (scsi_buf[0] & 0x1f) << 16;
2345                         }
2346
2347                         /*
2348                          * Make sure data length is ok. Minimum length is 36 bytes
2349                          */
2350                         if (scsi_buf[4] == 0) {
2351                                 scsi_buf[4] = 36 - 5;
2352                         }
2353
2354                         /*
2355                          * Check for Simple Direct Access Device and change it to TYPE_DISK
2356                          */
2357                         if ((scsi_buf[0] & 0x1f) == TYPE_SDAD) {
2358                                 SBP2_DEBUG("Changing TYPE_SDAD to TYPE_DISK");
2359                                 scsi_buf[0] &= 0xe0;
2360                         }
2361
2362                         /*
2363                          * Fix ansi revision and response data format
2364                          */
2365                         scsi_buf[2] |= 2;
2366                         scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
2367
2368                         break;
2369
2370                 case MODE_SENSE:
2371
2372                         if (sbp2_command_conversion_device_type(device_type)) {
2373                         
2374                                 SBP2_DEBUG("Modify mode sense response (10 byte version)");
2375
2376                                 scsi_buf[0] = scsi_buf[1];      /* Mode data length */
2377                                 scsi_buf[1] = scsi_buf[2];      /* Medium type */
2378                                 scsi_buf[2] = scsi_buf[3];      /* Device specific parameter */
2379                                 scsi_buf[3] = scsi_buf[7];      /* Block descriptor length */
2380                                 memcpy(scsi_buf + 4, scsi_buf + 8, scsi_buf[0]);
2381         
2382                         }
2383
2384                         break;
2385
2386                 case MODE_SELECT:
2387
2388                         /*
2389                          * TODO. Probably need to change mode select to 10 byte version
2390                          */
2391
2392                 default:
2393                         break;
2394         }
2395         return;
2396 }
2397
2398 /*
2399  * This function deals with status writes from the SBP-2 device
2400  */
2401 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid,
2402                                     quadlet_t *data, u64 addr, unsigned int length, u16 fl)
2403 {
2404         struct sbp2scsi_host_info *hi = NULL;
2405         struct scsi_id_instance_data *scsi_id = NULL;
2406         u32 id;
2407         unsigned long flags;
2408         Scsi_Cmnd *SCpnt = NULL;
2409         u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2410         struct sbp2_command_info *command;
2411
2412         SBP2_DEBUG("sbp2_handle_status_write");
2413
2414         sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
2415
2416         if (!host) {
2417                 SBP2_ERR("host is NULL - this is bad!");
2418                 return(RCODE_ADDRESS_ERROR);
2419         }
2420
2421         hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
2422
2423         if (!hi) {
2424                 SBP2_ERR("host info is NULL - this is bad!");
2425                 return(RCODE_ADDRESS_ERROR);
2426         }
2427
2428         spin_lock_irqsave(&hi->sbp2_command_lock, flags);
2429
2430         /*
2431          * Find our scsi_id structure by looking at the status fifo address written to by
2432          * the sbp2 device.
2433          */
2434         id = SBP2_STATUS_FIFO_OFFSET_TO_ENTRY((u32)(addr - SBP2_STATUS_FIFO_ADDRESS)); 
2435         scsi_id = hi->scsi_id[id];
2436
2437         if (!scsi_id) {
2438                 SBP2_ERR("scsi_id is NULL - device is gone?");
2439                 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2440                 return(RCODE_ADDRESS_ERROR);
2441         }
2442
2443         /*
2444          * Put response into scsi_id status fifo... 
2445          */
2446         memcpy(&scsi_id->status_block, data, length);
2447
2448         /*
2449          * Byte swap first two quadlets (8 bytes) of status for processing
2450          */
2451         sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8);
2452
2453         /*
2454          * Handle command ORB status here if necessary. First, need to match status with command.
2455          */
2456         command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo);
2457         if (command) {
2458
2459                 SBP2_DEBUG("Found status for command ORB");
2460                 pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2461                                     sizeof(struct sbp2_command_orb),
2462                                     PCI_DMA_BIDIRECTIONAL);
2463                 pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2464                                     sizeof(command->scatter_gather_element),
2465                                     PCI_DMA_BIDIRECTIONAL);
2466
2467                 SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
2468                 outstanding_orb_decr;
2469
2470                 /*
2471                  * Matched status with command, now grab scsi command pointers and check status
2472                  */
2473                 SCpnt = command->Current_SCpnt;
2474                 sbp2util_mark_command_completed(scsi_id, command);
2475
2476                 if (SCpnt) {
2477
2478                         /*
2479                          * See if the target stored any scsi status information
2480                          */
2481                         if (STATUS_GET_LENGTH(scsi_id->status_block.ORB_offset_hi_misc) > 1) {
2482                                 /*
2483                                  * Translate SBP-2 status to SCSI sense data
2484                                  */
2485                                 SBP2_DEBUG("CHECK CONDITION");
2486                                 scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer);
2487                         }
2488
2489                         /*
2490                          * Check to see if the dead bit is set. If so, we'll have to initiate
2491                          * a fetch agent reset.
2492                          */
2493                         if (STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc)) {
2494
2495                                 /*
2496                                  * Initiate a fetch agent reset. 
2497                                  */
2498                                 SBP2_DEBUG("Dead bit set - initiating fetch agent reset");
2499                                 sbp2_agent_reset(scsi_id, 0);
2500                         }
2501
2502                         SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
2503                 }
2504
2505                 /*
2506                  * Check here to see if there are no commands in-use. If there are none, we can
2507                  * null out last orb so that next time around we write directly to the orb pointer... 
2508                  * Quick start saves one 1394 bus transaction.
2509                  */
2510                 if (list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2511                         scsi_id->last_orb = NULL;
2512                 }
2513
2514         } else {
2515                 
2516                 /* 
2517                  * It's probably a login/logout/reconnect status.
2518                  */
2519                 if ((scsi_id->login_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2520                     (scsi_id->query_logins_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2521                     (scsi_id->reconnect_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2522                     (scsi_id->logout_orb_dma == scsi_id->status_block.ORB_offset_lo)) {
2523                         atomic_set(&scsi_id->sbp2_login_complete, 1);
2524                 }
2525         }
2526
2527         spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2528
2529
2530         if (SCpnt) {
2531
2532                 /*
2533                  * Complete the SCSI command.
2534                  *
2535                  * Only do it after we've released the sbp2_command_lock,
2536                  * as it might otherwise deadlock with the 
2537                  * io_request_lock (in sbp2scsi_queuecommand).
2538                  */
2539                 SBP2_DEBUG("Completing SCSI command");
2540                 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
2541                                           command->Current_done);
2542                 SBP2_ORB_DEBUG("command orb completed");
2543         }
2544
2545         return(RCODE_COMPLETE);
2546 }
2547
2548
2549 /**************************************
2550  * SCSI interface related section
2551  **************************************/
2552
2553 /*
2554  * This routine is the main request entry routine for doing I/O. It is 
2555  * called from the scsi stack directly.
2556  */
2557 static int sbp2scsi_queuecommand (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) 
2558 {
2559         struct sbp2scsi_host_info *hi = NULL;
2560         struct scsi_id_instance_data *scsi_id = NULL;
2561         unsigned long flags;
2562
2563         SBP2_DEBUG("sbp2scsi_queuecommand");
2564
2565         /*
2566          * Pull our host info and scsi id instance data from the scsi command
2567          */
2568         hi = hpsb_get_hostinfo_bykey(&sbp2_highlevel, (unsigned long)SCpnt->device->host);
2569
2570         if (!hi) {
2571                 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
2572                 SCpnt->result = DID_NO_CONNECT << 16;
2573                 done (SCpnt);
2574                 return(0);
2575         }
2576
2577         scsi_id = hi->scsi_id[SCpnt->device->id];
2578
2579         /*
2580          * If scsi_id is null, it means there is no device in this slot,
2581          * so we should return selection timeout.
2582          */
2583         if (!scsi_id) {
2584                 SCpnt->result = DID_NO_CONNECT << 16;
2585                 done (SCpnt);
2586                 return(0);
2587         }
2588
2589         /*
2590          * Until we handle multiple luns, just return selection time-out
2591          * to any IO directed at non-zero LUNs
2592          */
2593         if (SCpnt->device->lun) {
2594                 SCpnt->result = DID_NO_CONNECT << 16;
2595                 done (SCpnt);
2596                 return(0);
2597         }
2598
2599         /*
2600          * Check for request sense command, and handle it here
2601          * (autorequest sense)
2602          */
2603         if (SCpnt->cmnd[0] == REQUEST_SENSE) {
2604                 SBP2_DEBUG("REQUEST_SENSE");
2605                 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
2606                 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2607                 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
2608                 return(0);
2609         }
2610
2611         /*
2612          * Check to see if we are in the middle of a bus reset.
2613          */
2614         if (!hpsb_node_entry_valid(scsi_id->ne)) {
2615                 SBP2_ERR("Bus reset in progress - rejecting command");
2616                 SCpnt->result = DID_BUS_BUSY << 16;
2617                 done (SCpnt);
2618                 return(0);
2619         }
2620
2621         /*
2622          * Try and send our SCSI command
2623          */
2624         spin_lock_irqsave(&hi->sbp2_command_lock, flags);
2625         if (sbp2_send_command(scsi_id, SCpnt, done)) {
2626                 SBP2_ERR("Error sending SCSI command");
2627                 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
2628                                           SCpnt, done);
2629         }
2630         spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2631
2632         return(0);
2633 }
2634
2635 /*
2636  * This function is called in order to complete all outstanding SBP-2
2637  * commands (in case of resets, etc.).
2638  */
2639 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id, 
2640                                            u32 status)
2641 {
2642         struct sbp2scsi_host_info *hi = scsi_id->hi;
2643         struct list_head *lh;
2644         struct sbp2_command_info *command;
2645
2646         SBP2_DEBUG("sbp2_complete_all_commands");
2647
2648         while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2649                 SBP2_DEBUG("Found pending command to complete");
2650                 lh = scsi_id->sbp2_command_orb_inuse.next;
2651                 command = list_entry(lh, struct sbp2_command_info, list);
2652                 pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2653                                     sizeof(struct sbp2_command_orb),
2654                                     PCI_DMA_BIDIRECTIONAL);
2655                 pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2656                                     sizeof(command->scatter_gather_element),
2657                                     PCI_DMA_BIDIRECTIONAL);
2658                 sbp2util_mark_command_completed(scsi_id, command);
2659                 if (command->Current_SCpnt) {
2660                         void (*done)(Scsi_Cmnd *) = command->Current_done;
2661                         command->Current_SCpnt->result = status << 16;
2662                         done (command->Current_SCpnt);
2663                 }
2664         }
2665
2666         return;
2667 }
2668
2669 /*
2670  * This function is called in order to complete a regular SBP-2 command.
2671  *
2672  * This can be called in interrupt context.
2673  */
2674 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2675                                       u32 scsi_status, Scsi_Cmnd *SCpnt,
2676                                       void (*done)(Scsi_Cmnd *))
2677 {
2678         unsigned long flags;
2679
2680         SBP2_DEBUG("sbp2scsi_complete_command");
2681
2682         /*
2683          * Sanity
2684          */
2685         if (!SCpnt) {
2686                 SBP2_ERR("SCpnt is NULL");
2687                 return;
2688         }
2689
2690         /*
2691          * If a bus reset is in progress and there was an error, don't
2692          * complete the command, just let it get retried at the end of the
2693          * bus reset.
2694          */
2695         if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2696                 SBP2_ERR("Bus reset in progress - retry command later");
2697                 return;
2698         }
2699         
2700         /*
2701          * Switch on scsi status
2702          */
2703         switch (scsi_status) {
2704                 case SBP2_SCSI_STATUS_GOOD:
2705                         SCpnt->result = DID_OK;
2706                         break;
2707
2708                 case SBP2_SCSI_STATUS_BUSY:
2709                         SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2710                         SCpnt->result = DID_BUS_BUSY << 16;
2711                         break;
2712
2713                 case SBP2_SCSI_STATUS_CHECK_CONDITION:
2714                         SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
2715                         SCpnt->result = CHECK_CONDITION << 1;
2716
2717                         /*
2718                          * Debug stuff
2719                          */
2720 #if CONFIG_IEEE1394_SBP2_DEBUG >= 1
2721                         print_command (SCpnt->cmnd);
2722                         print_sense("bh", SCpnt);
2723 #endif
2724
2725                         break;
2726
2727                 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2728                         SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2729                         SCpnt->result = DID_NO_CONNECT << 16;
2730                         print_command (SCpnt->cmnd);
2731                         break;
2732
2733                 case SBP2_SCSI_STATUS_CONDITION_MET:
2734                 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2735                 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2736                         SBP2_ERR("Bad SCSI status = %x", scsi_status);
2737                         SCpnt->result = DID_ERROR << 16;
2738                         print_command (SCpnt->cmnd);
2739                         break;
2740
2741                 default:
2742                         SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2743                         SCpnt->result = DID_ERROR << 16;
2744         }
2745
2746         /*
2747          * Take care of any sbp2 response data mucking here (RBC stuff, etc.)
2748          */
2749         if (SCpnt->result == DID_OK) {
2750                 sbp2_check_sbp2_response(scsi_id, SCpnt);
2751         }
2752
2753         /*
2754          * If a bus reset is in progress and there was an error, complete
2755          * the command as busy so that it will get retried.
2756          */
2757         if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2758                 SBP2_ERR("Completing command with busy (bus reset)");
2759                 SCpnt->result = DID_BUS_BUSY << 16;
2760         }
2761
2762         /*
2763          * If a unit attention occurs, return busy status so it gets
2764          * retried... it could have happened because of a 1394 bus reset
2765          * or hot-plug...
2766          */
2767 #if 0
2768         if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) && 
2769             (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
2770                 SBP2_DEBUG("UNIT ATTENTION - return busy");
2771                 SCpnt->result = DID_BUS_BUSY << 16;
2772         }
2773 #endif
2774
2775         /*
2776          * Tell scsi stack that we're done with this command
2777          */
2778         spin_lock_irqsave(scsi_id->hi->scsi_host->host_lock,flags);
2779         done (SCpnt);
2780         spin_unlock_irqrestore(scsi_id->hi->scsi_host->host_lock,flags);
2781
2782         return;
2783 }
2784
2785 /*
2786  * Called by scsi stack when something has really gone wrong.  Usually
2787  * called when a command has timed-out for some reason.
2788  */
2789 static int sbp2scsi_abort (Scsi_Cmnd *SCpnt) 
2790 {
2791         struct sbp2scsi_host_info *hi = hpsb_get_hostinfo_bykey(&sbp2_highlevel,
2792                                                                 (unsigned long)SCpnt->device->host);
2793         struct scsi_id_instance_data *scsi_id = hi->scsi_id[SCpnt->device->id];
2794         struct sbp2_command_info *command;
2795         unsigned long flags;
2796
2797         SBP2_ERR("aborting sbp2 command");
2798         print_command (SCpnt->cmnd);
2799         
2800         if (scsi_id) {
2801
2802                 /*
2803                  * Right now, just return any matching command structures
2804                  * to the free pool.
2805                  */
2806                 spin_lock_irqsave(&hi->sbp2_command_lock, flags);
2807                 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2808                 if (command) {
2809                         SBP2_DEBUG("Found command to abort");
2810                         pci_dma_sync_single(hi->host->pdev,
2811                                             command->command_orb_dma,
2812                                             sizeof(struct sbp2_command_orb),
2813                                             PCI_DMA_BIDIRECTIONAL);
2814                         pci_dma_sync_single(hi->host->pdev,
2815                                             command->sge_dma,
2816                                             sizeof(command->scatter_gather_element),
2817                                             PCI_DMA_BIDIRECTIONAL);
2818                         sbp2util_mark_command_completed(scsi_id, command);
2819                         if (command->Current_SCpnt) {
2820                                 void (*done)(Scsi_Cmnd *) = command->Current_done;
2821                                 command->Current_SCpnt->result = DID_ABORT << 16;
2822                                 done (command->Current_SCpnt);
2823                         }
2824                 }
2825
2826                 /*
2827                  * Initiate a fetch agent reset. 
2828                  */
2829                 sbp2_agent_reset(scsi_id, 0);
2830                 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);          
2831                 spin_unlock_irqrestore(&hi->sbp2_command_lock, flags);
2832         }
2833
2834         return(SUCCESS);
2835 }
2836
2837 /*
2838  * Called by scsi stack when something has really gone wrong.
2839  */
2840 static int sbp2scsi_reset (Scsi_Cmnd *SCpnt) 
2841 {
2842         struct sbp2scsi_host_info *hi = hpsb_get_hostinfo_bykey(&sbp2_highlevel,
2843                                                                 (unsigned long)SCpnt->device->host);
2844         struct scsi_id_instance_data *scsi_id = hi->scsi_id[SCpnt->device->id];
2845
2846         SBP2_ERR("reset requested");
2847
2848         if (scsi_id) {
2849                 SBP2_ERR("Generating sbp2 fetch agent reset");
2850                 sbp2_agent_reset(scsi_id, 0);
2851         }
2852
2853         return(SUCCESS);
2854 }
2855
2856 static const char *sbp2scsi_info (struct Scsi_Host *host)
2857 {
2858         return "SCSI emulation for IEEE-1394 SBP-2 Devices";
2859 }
2860
2861 MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2862 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2863 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2864 MODULE_LICENSE("GPL");
2865
2866 /* SCSI host template */
2867 static Scsi_Host_Template scsi_driver_template = {
2868         .module =                       THIS_MODULE,
2869         .name =                         "SBP-2 IEEE-1394",
2870         .proc_name =                    SBP2_DEVICE_NAME,
2871         .info =                         sbp2scsi_info,
2872         .queuecommand =                 sbp2scsi_queuecommand,
2873         .eh_abort_handler =             sbp2scsi_abort,
2874         .eh_device_reset_handler =      sbp2scsi_reset,
2875         .eh_bus_reset_handler =         sbp2scsi_reset,
2876         .eh_host_reset_handler =        sbp2scsi_reset,
2877         .this_id =                      -1,
2878         .sg_tablesize =                 SG_ALL,
2879         .use_clustering =               ENABLE_CLUSTERING,
2880         .cmd_per_lun =                  SBP2_MAX_CMDS_PER_LUN,
2881         .can_queue =                    SBP2_MAX_SCSI_QUEUE,
2882         .emulated =                     1,
2883         .highmem_io =                   1,
2884 };
2885
2886 static int sbp2_module_init(void)
2887 {
2888         SBP2_DEBUG("sbp2_module_init");
2889
2890         printk(KERN_INFO "sbp2: %s\n", version);
2891
2892         /* Module load debug option to force one command at a time (serializing I/O) */
2893         if (serialize_io) {
2894                 SBP2_ERR("Driver forced to serialize I/O (serialize_io = 1)");
2895                 scsi_driver_template.can_queue = 1;
2896                 scsi_driver_template.cmd_per_lun = 1;
2897         }
2898
2899         /* Set max sectors (module load option). Default is 255 sectors. */
2900         scsi_driver_template.max_sectors = max_sectors;
2901
2902
2903         /* Register our high level driver with 1394 stack */
2904         hpsb_register_highlevel(&sbp2_highlevel);
2905
2906         /* Register our sbp2 status address space... */
2907         hpsb_register_addrspace(&sbp2_highlevel, &sbp2_ops, SBP2_STATUS_FIFO_ADDRESS,
2908                                 SBP2_STATUS_FIFO_ADDRESS + 
2909                                 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(SBP2SCSI_MAX_SCSI_IDS+1));
2910
2911         /* Handle data movement if physical dma is not enabled/supported
2912          * on host controller */
2913 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
2914         hpsb_register_addrspace(&sbp2_highlevel, &sbp2_physdma_ops, 0x0ULL, 0xfffffffcULL);
2915 #endif
2916
2917         hpsb_register_protocol(&sbp2_driver);
2918
2919         return 0;
2920 }
2921
2922 static void __exit sbp2_module_exit(void)
2923 {
2924         SBP2_DEBUG("sbp2_module_exit");
2925
2926         hpsb_unregister_protocol(&sbp2_driver);
2927
2928         hpsb_unregister_highlevel(&sbp2_highlevel);
2929 }
2930
2931 module_init(sbp2_module_init);
2932 module_exit(sbp2_module_exit);