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