ieee1394: sbp2: clean up sbp2_ namespace
[safe/jmp/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/blkdev.h>
42 #include <linux/compiler.h>
43 #include <linux/delay.h>
44 #include <linux/device.h>
45 #include <linux/dma-mapping.h>
46 #include <linux/gfp.h>
47 #include <linux/init.h>
48 #include <linux/kernel.h>
49 #include <linux/list.h>
50 #include <linux/module.h>
51 #include <linux/moduleparam.h>
52 #include <linux/pci.h>
53 #include <linux/slab.h>
54 #include <linux/spinlock.h>
55 #include <linux/stat.h>
56 #include <linux/string.h>
57 #include <linux/stringify.h>
58 #include <linux/types.h>
59 #include <linux/wait.h>
60
61 #include <asm/byteorder.h>
62 #include <asm/errno.h>
63 #include <asm/param.h>
64 #include <asm/scatterlist.h>
65 #include <asm/system.h>
66 #include <asm/types.h>
67
68 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
69 #include <asm/io.h> /* for bus_to_virt */
70 #endif
71
72 #include <scsi/scsi.h>
73 #include <scsi/scsi_cmnd.h>
74 #include <scsi/scsi_dbg.h>
75 #include <scsi/scsi_device.h>
76 #include <scsi/scsi_host.h>
77
78 #include "csr1212.h"
79 #include "highlevel.h"
80 #include "hosts.h"
81 #include "ieee1394.h"
82 #include "ieee1394_core.h"
83 #include "ieee1394_hotplug.h"
84 #include "ieee1394_transactions.h"
85 #include "ieee1394_types.h"
86 #include "nodemgr.h"
87 #include "sbp2.h"
88
89 /*
90  * Module load parameter definitions
91  */
92
93 /*
94  * Change max_speed on module load if you have a bad IEEE-1394
95  * controller that has trouble running 2KB packets at 400mb.
96  *
97  * NOTE: On certain OHCI parts I have seen short packets on async transmit
98  * (probably due to PCI latency/throughput issues with the part). You can
99  * bump down the speed if you are running into problems.
100  */
101 static int sbp2_max_speed = IEEE1394_SPEED_MAX;
102 module_param_named(max_speed, sbp2_max_speed, int, 0644);
103 MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)");
104
105 /*
106  * Set serialize_io to 1 if you'd like only one scsi command sent
107  * down to us at a time (debugging). This might be necessary for very
108  * badly behaved sbp2 devices.
109  *
110  * TODO: Make this configurable per device.
111  */
112 static int sbp2_serialize_io = 1;
113 module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
114 MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
115                  "(default = 1, faster = 0)");
116
117 /*
118  * Bump up max_sectors if you'd like to support very large sized
119  * transfers. Please note that some older sbp2 bridge chips are broken for
120  * transfers greater or equal to 128KB.  Default is a value of 255
121  * sectors, or just under 128KB (at 512 byte sector size). I can note that
122  * the Oxsemi sbp2 chipsets have no problems supporting very large
123  * transfer sizes.
124  */
125 static int sbp2_max_sectors = SBP2_MAX_SECTORS;
126 module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
127 MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
128                  "(default = " __stringify(SBP2_MAX_SECTORS) ")");
129
130 /*
131  * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
132  * do an exclusive login, as it's generally unsafe to have two hosts
133  * talking to a single sbp2 device at the same time (filesystem coherency,
134  * etc.). If you're running an sbp2 device that supports multiple logins,
135  * and you're either running read-only filesystems or some sort of special
136  * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
137  * File System, or Lustre, then set exclusive_login to zero.
138  *
139  * So far only bridges from Oxford Semiconductor are known to support
140  * concurrent logins. Depending on firmware, four or two concurrent logins
141  * are possible on OXFW911 and newer Oxsemi bridges.
142  */
143 static int sbp2_exclusive_login = 1;
144 module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
145 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
146                  "(default = 1)");
147
148 /*
149  * If any of the following workarounds is required for your device to work,
150  * please submit the kernel messages logged by sbp2 to the linux1394-devel
151  * mailing list.
152  *
153  * - 128kB max transfer
154  *   Limit transfer size. Necessary for some old bridges.
155  *
156  * - 36 byte inquiry
157  *   When scsi_mod probes the device, let the inquiry command look like that
158  *   from MS Windows.
159  *
160  * - skip mode page 8
161  *   Suppress sending of mode_sense for mode page 8 if the device pretends to
162  *   support the SCSI Primary Block commands instead of Reduced Block Commands.
163  *
164  * - fix capacity
165  *   Tell sd_mod to correct the last sector number reported by read_capacity.
166  *   Avoids access beyond actual disk limits on devices with an off-by-one bug.
167  *   Don't use this with devices which don't have this bug.
168  *
169  * - override internal blacklist
170  *   Instead of adding to the built-in blacklist, use only the workarounds
171  *   specified in the module load parameter.
172  *   Useful if a blacklist entry interfered with a non-broken device.
173  */
174 static int sbp2_default_workarounds;
175 module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
176 MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
177         ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
178         ", 36 byte inquiry = "    __stringify(SBP2_WORKAROUND_INQUIRY_36)
179         ", skip mode page 8 = "   __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
180         ", fix capacity = "       __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
181         ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
182         ", or a combination)");
183
184
185 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
186 #define SBP2_ERR(fmt, args...)  HPSB_ERR("sbp2: "fmt, ## args)
187
188 /*
189  * Globals
190  */
191 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *, u32);
192 static void sbp2scsi_complete_command(struct scsi_id_instance_data *, u32,
193                                       struct scsi_cmnd *,
194                                       void (*)(struct scsi_cmnd *));
195 static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *);
196 static int sbp2_start_device(struct scsi_id_instance_data *);
197 static void sbp2_remove_device(struct scsi_id_instance_data *);
198 static int sbp2_login_device(struct scsi_id_instance_data *);
199 static int sbp2_reconnect_device(struct scsi_id_instance_data *);
200 static int sbp2_logout_device(struct scsi_id_instance_data *);
201 static void sbp2_host_reset(struct hpsb_host *);
202 static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
203                                     u64, size_t, u16);
204 static int sbp2_agent_reset(struct scsi_id_instance_data *, int);
205 static void sbp2_parse_unit_directory(struct scsi_id_instance_data *,
206                                       struct unit_directory *);
207 static int sbp2_set_busy_timeout(struct scsi_id_instance_data *);
208 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *);
209
210
211 static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
212
213 static struct hpsb_highlevel sbp2_highlevel = {
214         .name           = SBP2_DEVICE_NAME,
215         .host_reset     = sbp2_host_reset,
216 };
217
218 static struct hpsb_address_ops sbp2_ops = {
219         .write          = sbp2_handle_status_write
220 };
221
222 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
223 static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
224                                      u64, size_t, u16);
225 static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
226                                     size_t, u16);
227
228 static struct hpsb_address_ops sbp2_physdma_ops = {
229         .read           = sbp2_handle_physdma_read,
230         .write          = sbp2_handle_physdma_write,
231 };
232 #endif
233
234
235 /*
236  * Interface to driver core and IEEE 1394 core
237  */
238 static struct ieee1394_device_id sbp2_id_table[] = {
239         {
240          .match_flags   = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
241          .specifier_id  = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
242          .version       = SBP2_SW_VERSION_ENTRY & 0xffffff},
243         {}
244 };
245 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
246
247 static int sbp2_probe(struct device *);
248 static int sbp2_remove(struct device *);
249 static int sbp2_update(struct unit_directory *);
250
251 static struct hpsb_protocol_driver sbp2_driver = {
252         .name           = "SBP2 Driver",
253         .id_table       = sbp2_id_table,
254         .update         = sbp2_update,
255         .driver         = {
256                 .name           = SBP2_DEVICE_NAME,
257                 .bus            = &ieee1394_bus_type,
258                 .probe          = sbp2_probe,
259                 .remove         = sbp2_remove,
260         },
261 };
262
263
264 /*
265  * Interface to SCSI core
266  */
267 static int sbp2scsi_queuecommand(struct scsi_cmnd *,
268                                  void (*)(struct scsi_cmnd *));
269 static int sbp2scsi_abort(struct scsi_cmnd *);
270 static int sbp2scsi_reset(struct scsi_cmnd *);
271 static int sbp2scsi_slave_alloc(struct scsi_device *);
272 static int sbp2scsi_slave_configure(struct scsi_device *);
273 static void sbp2scsi_slave_destroy(struct scsi_device *);
274 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
275                                            struct device_attribute *, char *);
276
277 static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
278
279 static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
280         &dev_attr_ieee1394_id,
281         NULL
282 };
283
284 static struct scsi_host_template sbp2_shost_template = {
285         .module                  = THIS_MODULE,
286         .name                    = "SBP-2 IEEE-1394",
287         .proc_name               = SBP2_DEVICE_NAME,
288         .queuecommand            = sbp2scsi_queuecommand,
289         .eh_abort_handler        = sbp2scsi_abort,
290         .eh_device_reset_handler = sbp2scsi_reset,
291         .slave_alloc             = sbp2scsi_slave_alloc,
292         .slave_configure         = sbp2scsi_slave_configure,
293         .slave_destroy           = sbp2scsi_slave_destroy,
294         .this_id                 = -1,
295         .sg_tablesize            = SG_ALL,
296         .use_clustering          = ENABLE_CLUSTERING,
297         .cmd_per_lun             = SBP2_MAX_CMDS,
298         .can_queue               = SBP2_MAX_CMDS,
299         .emulated                = 1,
300         .sdev_attrs              = sbp2_sysfs_sdev_attrs,
301 };
302
303
304 /*
305  * List of devices with known bugs.
306  *
307  * The firmware_revision field, masked with 0xffff00, is the best indicator
308  * for the type of bridge chip of a device.  It yields a few false positives
309  * but this did not break correctly behaving devices so far.
310  */
311 static const struct {
312         u32 firmware_revision;
313         u32 model_id;
314         unsigned workarounds;
315 } sbp2_workarounds_table[] = {
316         /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
317                 .firmware_revision      = 0x002800,
318                 .model_id               = 0x001010,
319                 .workarounds            = SBP2_WORKAROUND_INQUIRY_36 |
320                                           SBP2_WORKAROUND_MODE_SENSE_8,
321         },
322         /* Initio bridges, actually only needed for some older ones */ {
323                 .firmware_revision      = 0x000200,
324                 .workarounds            = SBP2_WORKAROUND_INQUIRY_36,
325         },
326         /* Symbios bridge */ {
327                 .firmware_revision      = 0xa0b800,
328                 .workarounds            = SBP2_WORKAROUND_128K_MAX_TRANS,
329         },
330         /*
331          * Note about the following Apple iPod blacklist entries:
332          *
333          * There are iPods (2nd gen, 3rd gen) with model_id==0.  Since our
334          * matching logic treats 0 as a wildcard, we cannot match this ID
335          * without rewriting the matching routine.  Fortunately these iPods
336          * do not feature the read_capacity bug according to one report.
337          * Read_capacity behaviour as well as model_id could change due to
338          * Apple-supplied firmware updates though.
339          */
340         /* iPod 4th generation */ {
341                 .firmware_revision      = 0x0a2700,
342                 .model_id               = 0x000021,
343                 .workarounds            = SBP2_WORKAROUND_FIX_CAPACITY,
344         },
345         /* iPod mini */ {
346                 .firmware_revision      = 0x0a2700,
347                 .model_id               = 0x000023,
348                 .workarounds            = SBP2_WORKAROUND_FIX_CAPACITY,
349         },
350         /* iPod Photo */ {
351                 .firmware_revision      = 0x0a2700,
352                 .model_id               = 0x00007e,
353                 .workarounds            = SBP2_WORKAROUND_FIX_CAPACITY,
354         }
355 };
356
357 /**************************************
358  * General utility functions
359  **************************************/
360
361 #ifndef __BIG_ENDIAN
362 /*
363  * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
364  */
365 static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
366 {
367         u32 *temp = buffer;
368
369         for (length = (length >> 2); length--; )
370                 temp[length] = be32_to_cpu(temp[length]);
371
372         return;
373 }
374
375 /*
376  * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
377  */
378 static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
379 {
380         u32 *temp = buffer;
381
382         for (length = (length >> 2); length--; )
383                 temp[length] = cpu_to_be32(temp[length]);
384
385         return;
386 }
387 #else /* BIG_ENDIAN */
388 /* Why waste the cpu cycles? */
389 #define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
390 #define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
391 #endif
392
393 static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
394
395 /*
396  * Waits for completion of an SBP-2 access request.
397  * Returns nonzero if timed out or prematurely interrupted.
398  */
399 static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
400                                    int timeout)
401 {
402         long leftover;
403
404         leftover = wait_event_interruptible_timeout(
405                         sbp2_access_wq, scsi_id->access_complete, timeout);
406         scsi_id->access_complete = 0;
407         return leftover <= 0;
408 }
409
410 static void sbp2_free_packet(struct hpsb_packet *packet)
411 {
412         hpsb_free_tlabel(packet);
413         hpsb_free_packet(packet);
414 }
415
416 /*
417  * This is much like hpsb_node_write(), except it ignores the response
418  * subaction and returns immediately. Can be used from atomic context.
419  */
420 static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
421                                        quadlet_t *buffer, size_t length)
422 {
423         struct hpsb_packet *packet;
424
425         packet = hpsb_make_writepacket(ne->host, ne->nodeid,
426                                        addr, buffer, length);
427         if (!packet)
428                 return -ENOMEM;
429
430         hpsb_set_packet_complete_task(packet,
431                                       (void (*)(void *))sbp2_free_packet,
432                                       packet);
433
434         hpsb_node_fill_packet(ne, packet);
435
436         if (hpsb_send_packet(packet) < 0) {
437                 sbp2_free_packet(packet);
438                 return -EIO;
439         }
440
441         return 0;
442 }
443
444 static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
445                                         u64 offset, quadlet_t *data, size_t len)
446 {
447         /*
448          * There is a small window after a bus reset within which the node
449          * entry's generation is current but the reconnect wasn't completed.
450          */
451         if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
452                 return;
453
454         if (hpsb_node_write(scsi_id->ne,
455                             scsi_id->command_block_agent_addr + offset,
456                             data, len))
457                 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
458         /*
459          * Now accept new SCSI commands, unless a bus reset happended during
460          * hpsb_node_write.
461          */
462         if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET))
463                 scsi_unblock_requests(scsi_id->scsi_host);
464 }
465
466 static void sbp2util_write_orb_pointer(struct work_struct *work)
467 {
468         quadlet_t data[2];
469
470         data[0] = ORB_SET_NODE_ID(
471                         (container_of(work, struct scsi_id_instance_data, protocol_work))->hi->host->node_id);
472         data[1] = (container_of(work, struct scsi_id_instance_data, protocol_work))->last_orb_dma;
473         sbp2util_cpu_to_be32_buffer(data, 8);
474         sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_ORB_POINTER_OFFSET, data, 8);
475 }
476
477 static void sbp2util_write_doorbell(struct work_struct *work)
478 {
479         sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_DOORBELL_OFFSET, NULL, 4);
480 }
481
482 static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
483 {
484         struct sbp2_fwhost_info *hi = scsi_id->hi;
485         int i;
486         unsigned long flags, orbs;
487         struct sbp2_command_info *command;
488
489         orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
490
491         spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
492         for (i = 0; i < orbs; i++) {
493                 command = kzalloc(sizeof(*command), GFP_ATOMIC);
494                 if (!command) {
495                         spin_unlock_irqrestore(&scsi_id->cmd_orb_lock,
496                                                flags);
497                         return -ENOMEM;
498                 }
499                 command->command_orb_dma =
500                     pci_map_single(hi->host->pdev, &command->command_orb,
501                                    sizeof(struct sbp2_command_orb),
502                                    PCI_DMA_TODEVICE);
503                 command->sge_dma =
504                     pci_map_single(hi->host->pdev,
505                                    &command->scatter_gather_element,
506                                    sizeof(command->scatter_gather_element),
507                                    PCI_DMA_BIDIRECTIONAL);
508                 INIT_LIST_HEAD(&command->list);
509                 list_add_tail(&command->list, &scsi_id->cmd_orb_completed);
510         }
511         spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
512         return 0;
513 }
514
515 static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
516 {
517         struct hpsb_host *host = scsi_id->hi->host;
518         struct list_head *lh, *next;
519         struct sbp2_command_info *command;
520         unsigned long flags;
521
522         spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
523         if (!list_empty(&scsi_id->cmd_orb_completed)) {
524                 list_for_each_safe(lh, next, &scsi_id->cmd_orb_completed) {
525                         command = list_entry(lh, struct sbp2_command_info, list);
526                         pci_unmap_single(host->pdev, command->command_orb_dma,
527                                          sizeof(struct sbp2_command_orb),
528                                          PCI_DMA_TODEVICE);
529                         pci_unmap_single(host->pdev, command->sge_dma,
530                                          sizeof(command->scatter_gather_element),
531                                          PCI_DMA_BIDIRECTIONAL);
532                         kfree(command);
533                 }
534         }
535         spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
536         return;
537 }
538
539 /*
540  * Finds the sbp2_command for a given outstanding command ORB.
541  * Only looks at the in-use list.
542  */
543 static struct sbp2_command_info *sbp2util_find_command_for_orb(
544                 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
545 {
546         struct sbp2_command_info *command;
547         unsigned long flags;
548
549         spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
550         if (!list_empty(&scsi_id->cmd_orb_inuse)) {
551                 list_for_each_entry(command, &scsi_id->cmd_orb_inuse, list) {
552                         if (command->command_orb_dma == orb) {
553                                 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
554                                 return command;
555                         }
556                 }
557         }
558         spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
559         return NULL;
560 }
561
562 /*
563  * Finds the sbp2_command for a given outstanding SCpnt.
564  * Only looks at the in-use list.
565  * Must be called with scsi_id->cmd_orb_lock held.
566  */
567 static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
568                 struct scsi_id_instance_data *scsi_id, void *SCpnt)
569 {
570         struct sbp2_command_info *command;
571
572         if (!list_empty(&scsi_id->cmd_orb_inuse))
573                 list_for_each_entry(command, &scsi_id->cmd_orb_inuse, list)
574                         if (command->Current_SCpnt == SCpnt)
575                                 return command;
576         return NULL;
577 }
578
579 static struct sbp2_command_info *sbp2util_allocate_command_orb(
580                 struct scsi_id_instance_data *scsi_id,
581                 struct scsi_cmnd *Current_SCpnt,
582                 void (*Current_done)(struct scsi_cmnd *))
583 {
584         struct list_head *lh;
585         struct sbp2_command_info *command = NULL;
586         unsigned long flags;
587
588         spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
589         if (!list_empty(&scsi_id->cmd_orb_completed)) {
590                 lh = scsi_id->cmd_orb_completed.next;
591                 list_del(lh);
592                 command = list_entry(lh, struct sbp2_command_info, list);
593                 command->Current_done = Current_done;
594                 command->Current_SCpnt = Current_SCpnt;
595                 list_add_tail(&command->list, &scsi_id->cmd_orb_inuse);
596         } else {
597                 SBP2_ERR("%s: no orbs available", __FUNCTION__);
598         }
599         spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
600         return command;
601 }
602
603 static void sbp2util_free_command_dma(struct sbp2_command_info *command)
604 {
605         struct scsi_id_instance_data *scsi_id =
606                 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
607         struct hpsb_host *host;
608
609         if (!scsi_id) {
610                 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
611                 return;
612         }
613
614         host = scsi_id->ud->ne->host;
615
616         if (command->cmd_dma) {
617                 if (command->dma_type == CMD_DMA_SINGLE)
618                         pci_unmap_single(host->pdev, command->cmd_dma,
619                                          command->dma_size, command->dma_dir);
620                 else if (command->dma_type == CMD_DMA_PAGE)
621                         pci_unmap_page(host->pdev, command->cmd_dma,
622                                        command->dma_size, command->dma_dir);
623                 /* XXX: Check for CMD_DMA_NONE bug */
624                 command->dma_type = CMD_DMA_NONE;
625                 command->cmd_dma = 0;
626         }
627
628         if (command->sge_buffer) {
629                 pci_unmap_sg(host->pdev, command->sge_buffer,
630                              command->dma_size, command->dma_dir);
631                 command->sge_buffer = NULL;
632         }
633 }
634
635 /*
636  * This function moves a command to the completed orb list.
637  * Must be called with scsi_id->cmd_orb_lock held.
638  */
639 static void sbp2util_mark_command_completed(
640                 struct scsi_id_instance_data *scsi_id,
641                 struct sbp2_command_info *command)
642 {
643         list_del(&command->list);
644         sbp2util_free_command_dma(command);
645         list_add_tail(&command->list, &scsi_id->cmd_orb_completed);
646 }
647
648 /*
649  * Is scsi_id valid? Is the 1394 node still present?
650  */
651 static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
652 {
653         return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
654 }
655
656 /*********************************************
657  * IEEE-1394 core driver stack related section
658  *********************************************/
659
660 static int sbp2_probe(struct device *dev)
661 {
662         struct unit_directory *ud;
663         struct scsi_id_instance_data *scsi_id;
664
665         ud = container_of(dev, struct unit_directory, device);
666
667         /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
668          * instead. */
669         if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
670                 return -ENODEV;
671
672         scsi_id = sbp2_alloc_device(ud);
673
674         if (!scsi_id)
675                 return -ENOMEM;
676
677         sbp2_parse_unit_directory(scsi_id, ud);
678
679         return sbp2_start_device(scsi_id);
680 }
681
682 static int sbp2_remove(struct device *dev)
683 {
684         struct unit_directory *ud;
685         struct scsi_id_instance_data *scsi_id;
686         struct scsi_device *sdev;
687
688         ud = container_of(dev, struct unit_directory, device);
689         scsi_id = ud->device.driver_data;
690         if (!scsi_id)
691                 return 0;
692
693         if (scsi_id->scsi_host) {
694                 /* Get rid of enqueued commands if there is no chance to
695                  * send them. */
696                 if (!sbp2util_node_is_available(scsi_id))
697                         sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
698                 /* scsi_remove_device() may trigger shutdown functions of SCSI
699                  * highlevel drivers which would deadlock if blocked. */
700                 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
701                 scsi_unblock_requests(scsi_id->scsi_host);
702         }
703         sdev = scsi_id->sdev;
704         if (sdev) {
705                 scsi_id->sdev = NULL;
706                 scsi_remove_device(sdev);
707         }
708
709         sbp2_logout_device(scsi_id);
710         sbp2_remove_device(scsi_id);
711
712         return 0;
713 }
714
715 static int sbp2_update(struct unit_directory *ud)
716 {
717         struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
718
719         if (sbp2_reconnect_device(scsi_id)) {
720                 /* Reconnect has failed. Perhaps we didn't reconnect fast
721                  * enough. Try a regular login, but first log out just in
722                  * case of any weirdness. */
723                 sbp2_logout_device(scsi_id);
724
725                 if (sbp2_login_device(scsi_id)) {
726                         /* Login failed too, just fail, and the backend
727                          * will call our sbp2_remove for us */
728                         SBP2_ERR("Failed to reconnect to sbp2 device!");
729                         return -EBUSY;
730                 }
731         }
732
733         sbp2_set_busy_timeout(scsi_id);
734         sbp2_agent_reset(scsi_id, 1);
735         sbp2_max_speed_and_size(scsi_id);
736
737         /* Complete any pending commands with busy (so they get retried)
738          * and remove them from our queue. */
739         sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
740
741         /* Accept new commands unless there was another bus reset in the
742          * meantime. */
743         if (hpsb_node_entry_valid(scsi_id->ne)) {
744                 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
745                 scsi_unblock_requests(scsi_id->scsi_host);
746         }
747         return 0;
748 }
749
750 static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
751 {
752         struct sbp2_fwhost_info *hi;
753         struct Scsi_Host *scsi_host = NULL;
754         struct scsi_id_instance_data *scsi_id = NULL;
755
756         scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
757         if (!scsi_id) {
758                 SBP2_ERR("failed to create scsi_id");
759                 goto failed_alloc;
760         }
761
762         scsi_id->ne = ud->ne;
763         scsi_id->ud = ud;
764         scsi_id->speed_code = IEEE1394_SPEED_100;
765         scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
766         scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
767         INIT_LIST_HEAD(&scsi_id->cmd_orb_inuse);
768         INIT_LIST_HEAD(&scsi_id->cmd_orb_completed);
769         INIT_LIST_HEAD(&scsi_id->scsi_list);
770         spin_lock_init(&scsi_id->cmd_orb_lock);
771         atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
772         INIT_WORK(&scsi_id->protocol_work, NULL);
773
774         ud->device.driver_data = scsi_id;
775
776         hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
777         if (!hi) {
778                 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
779                 if (!hi) {
780                         SBP2_ERR("failed to allocate hostinfo");
781                         goto failed_alloc;
782                 }
783                 hi->host = ud->ne->host;
784                 INIT_LIST_HEAD(&hi->scsi_ids);
785
786 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
787                 /* Handle data movement if physical dma is not
788                  * enabled or not supported on host controller */
789                 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
790                                              &sbp2_physdma_ops,
791                                              0x0ULL, 0xfffffffcULL)) {
792                         SBP2_ERR("failed to register lower 4GB address range");
793                         goto failed_alloc;
794                 }
795 #endif
796         }
797
798         /* Prevent unloading of the 1394 host */
799         if (!try_module_get(hi->host->driver->owner)) {
800                 SBP2_ERR("failed to get a reference on 1394 host driver");
801                 goto failed_alloc;
802         }
803
804         scsi_id->hi = hi;
805
806         list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
807
808         /* Register the status FIFO address range. We could use the same FIFO
809          * for targets at different nodes. However we need different FIFOs per
810          * target in order to support multi-unit devices.
811          * The FIFO is located out of the local host controller's physical range
812          * but, if possible, within the posted write area. Status writes will
813          * then be performed as unified transactions. This slightly reduces
814          * bandwidth usage, and some Prolific based devices seem to require it.
815          */
816         scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
817                         &sbp2_highlevel, ud->ne->host, &sbp2_ops,
818                         sizeof(struct sbp2_status_block), sizeof(quadlet_t),
819                         ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
820         if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
821                 SBP2_ERR("failed to allocate status FIFO address range");
822                 goto failed_alloc;
823         }
824
825         scsi_host = scsi_host_alloc(&sbp2_shost_template,
826                                     sizeof(unsigned long));
827         if (!scsi_host) {
828                 SBP2_ERR("failed to register scsi host");
829                 goto failed_alloc;
830         }
831
832         scsi_host->hostdata[0] = (unsigned long)scsi_id;
833
834         if (!scsi_add_host(scsi_host, &ud->device)) {
835                 scsi_id->scsi_host = scsi_host;
836                 return scsi_id;
837         }
838
839         SBP2_ERR("failed to add scsi host");
840         scsi_host_put(scsi_host);
841
842 failed_alloc:
843         sbp2_remove_device(scsi_id);
844         return NULL;
845 }
846
847 static void sbp2_host_reset(struct hpsb_host *host)
848 {
849         struct sbp2_fwhost_info *hi;
850         struct scsi_id_instance_data *scsi_id;
851
852         hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
853         if (!hi)
854                 return;
855         list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
856                 if (likely(atomic_read(&scsi_id->state) !=
857                            SBP2LU_STATE_IN_SHUTDOWN)) {
858                         atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
859                         scsi_block_requests(scsi_id->scsi_host);
860                 }
861 }
862
863 static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
864 {
865         struct sbp2_fwhost_info *hi = scsi_id->hi;
866         int error;
867
868         scsi_id->login_response =
869                 pci_alloc_consistent(hi->host->pdev,
870                                      sizeof(struct sbp2_login_response),
871                                      &scsi_id->login_response_dma);
872         if (!scsi_id->login_response)
873                 goto alloc_fail;
874
875         scsi_id->query_logins_orb =
876                 pci_alloc_consistent(hi->host->pdev,
877                                      sizeof(struct sbp2_query_logins_orb),
878                                      &scsi_id->query_logins_orb_dma);
879         if (!scsi_id->query_logins_orb)
880                 goto alloc_fail;
881
882         scsi_id->query_logins_response =
883                 pci_alloc_consistent(hi->host->pdev,
884                                      sizeof(struct sbp2_query_logins_response),
885                                      &scsi_id->query_logins_response_dma);
886         if (!scsi_id->query_logins_response)
887                 goto alloc_fail;
888
889         scsi_id->reconnect_orb =
890                 pci_alloc_consistent(hi->host->pdev,
891                                      sizeof(struct sbp2_reconnect_orb),
892                                      &scsi_id->reconnect_orb_dma);
893         if (!scsi_id->reconnect_orb)
894                 goto alloc_fail;
895
896         scsi_id->logout_orb =
897                 pci_alloc_consistent(hi->host->pdev,
898                                      sizeof(struct sbp2_logout_orb),
899                                      &scsi_id->logout_orb_dma);
900         if (!scsi_id->logout_orb)
901                 goto alloc_fail;
902
903         scsi_id->login_orb =
904                 pci_alloc_consistent(hi->host->pdev,
905                                      sizeof(struct sbp2_login_orb),
906                                      &scsi_id->login_orb_dma);
907         if (!scsi_id->login_orb)
908                 goto alloc_fail;
909
910         if (sbp2util_create_command_orb_pool(scsi_id)) {
911                 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
912                 sbp2_remove_device(scsi_id);
913                 return -ENOMEM;
914         }
915
916         /* Wait a second before trying to log in. Previously logged in
917          * initiators need a chance to reconnect. */
918         if (msleep_interruptible(1000)) {
919                 sbp2_remove_device(scsi_id);
920                 return -EINTR;
921         }
922
923         if (sbp2_login_device(scsi_id)) {
924                 sbp2_remove_device(scsi_id);
925                 return -EBUSY;
926         }
927
928         sbp2_set_busy_timeout(scsi_id);
929         sbp2_agent_reset(scsi_id, 1);
930         sbp2_max_speed_and_size(scsi_id);
931
932         error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
933         if (error) {
934                 SBP2_ERR("scsi_add_device failed");
935                 sbp2_logout_device(scsi_id);
936                 sbp2_remove_device(scsi_id);
937                 return error;
938         }
939
940         return 0;
941
942 alloc_fail:
943         SBP2_ERR("Could not allocate memory for scsi_id");
944         sbp2_remove_device(scsi_id);
945         return -ENOMEM;
946 }
947
948 static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
949 {
950         struct sbp2_fwhost_info *hi;
951
952         if (!scsi_id)
953                 return;
954
955         hi = scsi_id->hi;
956
957         if (scsi_id->scsi_host) {
958                 scsi_remove_host(scsi_id->scsi_host);
959                 scsi_host_put(scsi_id->scsi_host);
960         }
961         flush_scheduled_work();
962         sbp2util_remove_command_orb_pool(scsi_id);
963
964         list_del(&scsi_id->scsi_list);
965
966         if (scsi_id->login_response)
967                 pci_free_consistent(hi->host->pdev,
968                                     sizeof(struct sbp2_login_response),
969                                     scsi_id->login_response,
970                                     scsi_id->login_response_dma);
971         if (scsi_id->login_orb)
972                 pci_free_consistent(hi->host->pdev,
973                                     sizeof(struct sbp2_login_orb),
974                                     scsi_id->login_orb,
975                                     scsi_id->login_orb_dma);
976         if (scsi_id->reconnect_orb)
977                 pci_free_consistent(hi->host->pdev,
978                                     sizeof(struct sbp2_reconnect_orb),
979                                     scsi_id->reconnect_orb,
980                                     scsi_id->reconnect_orb_dma);
981         if (scsi_id->logout_orb)
982                 pci_free_consistent(hi->host->pdev,
983                                     sizeof(struct sbp2_logout_orb),
984                                     scsi_id->logout_orb,
985                                     scsi_id->logout_orb_dma);
986         if (scsi_id->query_logins_orb)
987                 pci_free_consistent(hi->host->pdev,
988                                     sizeof(struct sbp2_query_logins_orb),
989                                     scsi_id->query_logins_orb,
990                                     scsi_id->query_logins_orb_dma);
991         if (scsi_id->query_logins_response)
992                 pci_free_consistent(hi->host->pdev,
993                                     sizeof(struct sbp2_query_logins_response),
994                                     scsi_id->query_logins_response,
995                                     scsi_id->query_logins_response_dma);
996
997         if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
998                 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
999                                           scsi_id->status_fifo_addr);
1000
1001         scsi_id->ud->device.driver_data = NULL;
1002
1003         if (hi)
1004                 module_put(hi->host->driver->owner);
1005
1006         kfree(scsi_id);
1007 }
1008
1009 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1010 /*
1011  * Deal with write requests on adapters which do not support physical DMA or
1012  * have it switched off.
1013  */
1014 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1015                                      int destid, quadlet_t *data, u64 addr,
1016                                      size_t length, u16 flags)
1017 {
1018         memcpy(bus_to_virt((u32) addr), data, length);
1019         return RCODE_COMPLETE;
1020 }
1021
1022 /*
1023  * Deal with read requests on adapters which do not support physical DMA or
1024  * have it switched off.
1025  */
1026 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1027                                     quadlet_t *data, u64 addr, size_t length,
1028                                     u16 flags)
1029 {
1030         memcpy(data, bus_to_virt((u32) addr), length);
1031         return RCODE_COMPLETE;
1032 }
1033 #endif
1034
1035 /**************************************
1036  * SBP-2 protocol related section
1037  **************************************/
1038
1039 static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1040 {
1041         struct sbp2_fwhost_info *hi = scsi_id->hi;
1042         quadlet_t data[2];
1043         int max_logins;
1044         int active_logins;
1045
1046         scsi_id->query_logins_orb->reserved1 = 0x0;
1047         scsi_id->query_logins_orb->reserved2 = 0x0;
1048
1049         scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1050         scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1051
1052         scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1053         scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1054         scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->lun);
1055
1056         scsi_id->query_logins_orb->reserved_resp_length =
1057                 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
1058
1059         scsi_id->query_logins_orb->status_fifo_hi =
1060                 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1061         scsi_id->query_logins_orb->status_fifo_lo =
1062                 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1063
1064         sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1065
1066         memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1067
1068         data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1069         data[1] = scsi_id->query_logins_orb_dma;
1070         sbp2util_cpu_to_be32_buffer(data, 8);
1071
1072         hpsb_node_write(scsi_id->ne, scsi_id->management_agent_addr, data, 8);
1073
1074         if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
1075                 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1076                 return -EIO;
1077         }
1078
1079         if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1080                 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1081                 return -EIO;
1082         }
1083
1084         if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1085                 SBP2_INFO("Error querying logins to SBP-2 device - failed");
1086                 return -EIO;
1087         }
1088
1089         sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1090
1091         max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
1092         SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1093
1094         active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
1095         SBP2_INFO("Number of active logins: %d", active_logins);
1096
1097         if (active_logins >= max_logins) {
1098                 return -EIO;
1099         }
1100
1101         return 0;
1102 }
1103
1104 static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1105 {
1106         struct sbp2_fwhost_info *hi = scsi_id->hi;
1107         quadlet_t data[2];
1108
1109         if (!scsi_id->login_orb)
1110                 return -EIO;
1111
1112         if (!sbp2_exclusive_login && sbp2_query_logins(scsi_id)) {
1113                 SBP2_INFO("Device does not support any more concurrent logins");
1114                 return -EIO;
1115         }
1116
1117         /* assume no password */
1118         scsi_id->login_orb->password_hi = 0;
1119         scsi_id->login_orb->password_lo = 0;
1120
1121         scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1122         scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1123         scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1124
1125         /* one second reconnect time */
1126         scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
1127         scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
1128         scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
1129         scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->lun);
1130
1131         scsi_id->login_orb->passwd_resp_lengths =
1132                 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1133
1134         scsi_id->login_orb->status_fifo_hi =
1135                 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1136         scsi_id->login_orb->status_fifo_lo =
1137                 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1138
1139         sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1140
1141         memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1142
1143         data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1144         data[1] = scsi_id->login_orb_dma;
1145         sbp2util_cpu_to_be32_buffer(data, 8);
1146
1147         hpsb_node_write(scsi_id->ne, scsi_id->management_agent_addr, data, 8);
1148
1149         /* wait up to 20 seconds for login status */
1150         if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
1151                 SBP2_ERR("Error logging into SBP-2 device - timed out");
1152                 return -EIO;
1153         }
1154
1155         /* make sure that the returned status matches the login ORB */
1156         if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1157                 SBP2_ERR("Error logging into SBP-2 device - timed out");
1158                 return -EIO;
1159         }
1160
1161         if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1162                 SBP2_ERR("Error logging into SBP-2 device - failed");
1163                 return -EIO;
1164         }
1165
1166         sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1167         scsi_id->command_block_agent_addr =
1168                 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1169         scsi_id->command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1170         scsi_id->command_block_agent_addr &= 0x0000ffffffffffffULL;
1171
1172         SBP2_INFO("Logged into SBP-2 device");
1173         return 0;
1174 }
1175
1176 static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1177 {
1178         struct sbp2_fwhost_info *hi = scsi_id->hi;
1179         quadlet_t data[2];
1180         int error;
1181
1182         scsi_id->logout_orb->reserved1 = 0x0;
1183         scsi_id->logout_orb->reserved2 = 0x0;
1184         scsi_id->logout_orb->reserved3 = 0x0;
1185         scsi_id->logout_orb->reserved4 = 0x0;
1186
1187         scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1188         scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1189         scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1190
1191         scsi_id->logout_orb->reserved5 = 0x0;
1192         scsi_id->logout_orb->status_fifo_hi =
1193                 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1194         scsi_id->logout_orb->status_fifo_lo =
1195                 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1196
1197         sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1198
1199         data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1200         data[1] = scsi_id->logout_orb_dma;
1201         sbp2util_cpu_to_be32_buffer(data, 8);
1202
1203         error = hpsb_node_write(scsi_id->ne,
1204                                 scsi_id->management_agent_addr, data, 8);
1205         if (error)
1206                 return error;
1207
1208         /* wait up to 1 second for the device to complete logout */
1209         if (sbp2util_access_timeout(scsi_id, HZ))
1210                 return -EIO;
1211
1212         SBP2_INFO("Logged out of SBP-2 device");
1213         return 0;
1214 }
1215
1216 static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1217 {
1218         struct sbp2_fwhost_info *hi = scsi_id->hi;
1219         quadlet_t data[2];
1220         int error;
1221
1222         scsi_id->reconnect_orb->reserved1 = 0x0;
1223         scsi_id->reconnect_orb->reserved2 = 0x0;
1224         scsi_id->reconnect_orb->reserved3 = 0x0;
1225         scsi_id->reconnect_orb->reserved4 = 0x0;
1226
1227         scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1228         scsi_id->reconnect_orb->login_ID_misc |=
1229                 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1230         scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1231
1232         scsi_id->reconnect_orb->reserved5 = 0x0;
1233         scsi_id->reconnect_orb->status_fifo_hi =
1234                 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1235         scsi_id->reconnect_orb->status_fifo_lo =
1236                 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1237
1238         sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1239
1240         data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1241         data[1] = scsi_id->reconnect_orb_dma;
1242         sbp2util_cpu_to_be32_buffer(data, 8);
1243
1244         error = hpsb_node_write(scsi_id->ne,
1245                                 scsi_id->management_agent_addr, data, 8);
1246         if (error)
1247                 return error;
1248
1249         /* wait up to 1 second for reconnect status */
1250         if (sbp2util_access_timeout(scsi_id, HZ)) {
1251                 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1252                 return -EIO;
1253         }
1254
1255         /* make sure that the returned status matches the reconnect ORB */
1256         if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1257                 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1258                 return -EIO;
1259         }
1260
1261         if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1262                 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
1263                 return -EIO;
1264         }
1265
1266         SBP2_INFO("Reconnected to SBP-2 device");
1267         return 0;
1268 }
1269
1270 /*
1271  * Set the target node's Single Phase Retry limit. Affects the target's retry
1272  * behaviour if our node is too busy to accept requests.
1273  */
1274 static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1275 {
1276         quadlet_t data;
1277
1278         data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1279         if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1280                 SBP2_ERR("%s error", __FUNCTION__);
1281         return 0;
1282 }
1283
1284 static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1285                                       struct unit_directory *ud)
1286 {
1287         struct csr1212_keyval *kv;
1288         struct csr1212_dentry *dentry;
1289         u64 management_agent_addr;
1290         u32 command_set_spec_id, command_set, unit_characteristics,
1291             firmware_revision;
1292         unsigned workarounds;
1293         int i;
1294
1295         management_agent_addr = 0x0;
1296         command_set_spec_id = 0x0;
1297         command_set = 0x0;
1298         unit_characteristics = 0x0;
1299         firmware_revision = 0x0;
1300
1301         csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1302                 switch (kv->key.id) {
1303                 case CSR1212_KV_ID_DEPENDENT_INFO:
1304                         if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
1305                                 management_agent_addr =
1306                                     CSR1212_REGISTER_SPACE_BASE +
1307                                     (kv->value.csr_offset << 2);
1308
1309                         else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
1310                                 scsi_id->lun =
1311                                     ORB_SET_LUN(kv->value.immediate);
1312                         break;
1313
1314                 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1315                         command_set_spec_id = kv->value.immediate;
1316                         break;
1317
1318                 case SBP2_COMMAND_SET_KEY:
1319                         command_set = kv->value.immediate;
1320                         break;
1321
1322                 case SBP2_UNIT_CHARACTERISTICS_KEY:
1323                         /* FIXME: This is ignored so far.
1324                          * See SBP-2 clause 7.4.8. */
1325                         unit_characteristics = kv->value.immediate;
1326                         break;
1327
1328                 case SBP2_FIRMWARE_REVISION_KEY:
1329                         firmware_revision = kv->value.immediate;
1330                         break;
1331
1332                 default:
1333                         /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1334                          * Its "ordered" bit has consequences for command ORB
1335                          * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
1336                         break;
1337                 }
1338         }
1339
1340         workarounds = sbp2_default_workarounds;
1341
1342         if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1343                 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1344                         if (sbp2_workarounds_table[i].firmware_revision &&
1345                             sbp2_workarounds_table[i].firmware_revision !=
1346                             (firmware_revision & 0xffff00))
1347                                 continue;
1348                         if (sbp2_workarounds_table[i].model_id &&
1349                             sbp2_workarounds_table[i].model_id != ud->model_id)
1350                                 continue;
1351                         workarounds |= sbp2_workarounds_table[i].workarounds;
1352                         break;
1353                 }
1354
1355         if (workarounds)
1356                 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1357                           "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1358                           " model_id 0x%06x)",
1359                           NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1360                           workarounds, firmware_revision,
1361                           ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1362                           ud->model_id);
1363
1364         /* We would need one SCSI host template for each target to adjust
1365          * max_sectors on the fly, therefore warn only. */
1366         if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1367             (sbp2_max_sectors * 512) > (128 * 1024))
1368                 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
1369                           "max transfer size. WARNING: Current max_sectors "
1370                           "setting is larger than 128KB (%d sectors)",
1371                           NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1372                           sbp2_max_sectors);
1373
1374         /* If this is a logical unit directory entry, process the parent
1375          * to get the values. */
1376         if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1377                 struct unit_directory *parent_ud =
1378                         container_of(ud->device.parent, struct unit_directory, device);
1379                 sbp2_parse_unit_directory(scsi_id, parent_ud);
1380         } else {
1381                 scsi_id->management_agent_addr = management_agent_addr;
1382                 scsi_id->command_set_spec_id = command_set_spec_id;
1383                 scsi_id->command_set = command_set;
1384                 scsi_id->unit_characteristics = unit_characteristics;
1385                 scsi_id->firmware_revision = firmware_revision;
1386                 scsi_id->workarounds = workarounds;
1387                 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
1388                         scsi_id->lun = ORB_SET_LUN(ud->lun);
1389         }
1390 }
1391
1392 #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1393
1394 /*
1395  * This function is called in order to determine the max speed and packet
1396  * size we can use in our ORBs. Note, that we (the driver and host) only
1397  * initiate the transaction. The SBP-2 device actually transfers the data
1398  * (by reading from the DMA area we tell it). This means that the SBP-2
1399  * device decides the actual maximum data it can transfer. We just tell it
1400  * the speed that it needs to use, and the max_rec the host supports, and
1401  * it takes care of the rest.
1402  */
1403 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1404 {
1405         struct sbp2_fwhost_info *hi = scsi_id->hi;
1406         u8 payload;
1407
1408         scsi_id->speed_code =
1409             hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
1410
1411         if (scsi_id->speed_code > sbp2_max_speed) {
1412                 scsi_id->speed_code = sbp2_max_speed;
1413                 SBP2_INFO("Reducing speed to %s",
1414                           hpsb_speedto_str[sbp2_max_speed]);
1415         }
1416
1417         /* Payload size is the lesser of what our speed supports and what
1418          * our host supports.  */
1419         payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1420                       (u8) (hi->host->csr.max_rec - 1));
1421
1422         /* If physical DMA is off, work around limitation in ohci1394:
1423          * packet size must not exceed PAGE_SIZE */
1424         if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
1425                 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1426                        payload)
1427                         payload--;
1428
1429         SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1430                   NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1431                   hpsb_speedto_str[scsi_id->speed_code],
1432                   SBP2_PAYLOAD_TO_BYTES(payload));
1433
1434         scsi_id->max_payload_size = payload;
1435         return 0;
1436 }
1437
1438 static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1439 {
1440         quadlet_t data;
1441         u64 addr;
1442         int retval;
1443         unsigned long flags;
1444
1445         /* cancel_delayed_work(&scsi_id->protocol_work); */
1446         if (wait)
1447                 flush_scheduled_work();
1448
1449         data = ntohl(SBP2_AGENT_RESET_DATA);
1450         addr = scsi_id->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1451
1452         if (wait)
1453                 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1454         else
1455                 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1456
1457         if (retval < 0) {
1458                 SBP2_ERR("hpsb_node_write failed.\n");
1459                 return -EIO;
1460         }
1461
1462         /* make sure that the ORB_POINTER is written on next command */
1463         spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1464         scsi_id->last_orb = NULL;
1465         spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1466
1467         return 0;
1468 }
1469
1470 static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1471                                      struct sbp2_fwhost_info *hi,
1472                                      struct sbp2_command_info *command,
1473                                      unsigned int scsi_use_sg,
1474                                      struct scatterlist *sgpnt,
1475                                      u32 orb_direction,
1476                                      enum dma_data_direction dma_dir)
1477 {
1478         command->dma_dir = dma_dir;
1479         orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1480         orb->misc |= ORB_SET_DIRECTION(orb_direction);
1481
1482         /* special case if only one element (and less than 64KB in size) */
1483         if ((scsi_use_sg == 1) &&
1484             (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1485
1486                 command->dma_size = sgpnt[0].length;
1487                 command->dma_type = CMD_DMA_PAGE;
1488                 command->cmd_dma = pci_map_page(hi->host->pdev,
1489                                                 sgpnt[0].page,
1490                                                 sgpnt[0].offset,
1491                                                 command->dma_size,
1492                                                 command->dma_dir);
1493
1494                 orb->data_descriptor_lo = command->cmd_dma;
1495                 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1496
1497         } else {
1498                 struct sbp2_unrestricted_page_table *sg_element =
1499                                         &command->scatter_gather_element[0];
1500                 u32 sg_count, sg_len;
1501                 dma_addr_t sg_addr;
1502                 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
1503                                           dma_dir);
1504
1505                 command->dma_size = scsi_use_sg;
1506                 command->sge_buffer = sgpnt;
1507
1508                 /* use page tables (s/g) */
1509                 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1510                 orb->data_descriptor_lo = command->sge_dma;
1511
1512                 /* loop through and fill out our SBP-2 page tables
1513                  * (and split up anything too large) */
1514                 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1515                         sg_len = sg_dma_len(sgpnt);
1516                         sg_addr = sg_dma_address(sgpnt);
1517                         while (sg_len) {
1518                                 sg_element[sg_count].segment_base_lo = sg_addr;
1519                                 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1520                                         sg_element[sg_count].length_segment_base_hi =
1521                                                 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1522                                         sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1523                                         sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1524                                 } else {
1525                                         sg_element[sg_count].length_segment_base_hi =
1526                                                 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1527                                         sg_len = 0;
1528                                 }
1529                                 sg_count++;
1530                         }
1531                 }
1532
1533                 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1534
1535                 sbp2util_cpu_to_be32_buffer(sg_element,
1536                                             (sizeof(struct sbp2_unrestricted_page_table)) *
1537                                             sg_count);
1538         }
1539 }
1540
1541 static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1542                                         struct sbp2_fwhost_info *hi,
1543                                         struct sbp2_command_info *command,
1544                                         struct scatterlist *sgpnt,
1545                                         u32 orb_direction,
1546                                         unsigned int scsi_request_bufflen,
1547                                         void *scsi_request_buffer,
1548                                         enum dma_data_direction dma_dir)
1549 {
1550         command->dma_dir = dma_dir;
1551         command->dma_size = scsi_request_bufflen;
1552         command->dma_type = CMD_DMA_SINGLE;
1553         command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
1554                                           command->dma_size, command->dma_dir);
1555         orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1556         orb->misc |= ORB_SET_DIRECTION(orb_direction);
1557
1558         /* handle case where we get a command w/o s/g enabled
1559          * (but check for transfers larger than 64K) */
1560         if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1561
1562                 orb->data_descriptor_lo = command->cmd_dma;
1563                 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1564
1565         } else {
1566                 /* The buffer is too large. Turn this into page tables. */
1567
1568                 struct sbp2_unrestricted_page_table *sg_element =
1569                         &command->scatter_gather_element[0];
1570                 u32 sg_count, sg_len;
1571                 dma_addr_t sg_addr;
1572
1573                 orb->data_descriptor_lo = command->sge_dma;
1574                 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1575
1576                 /* fill out our SBP-2 page tables; split up the large buffer */
1577                 sg_count = 0;
1578                 sg_len = scsi_request_bufflen;
1579                 sg_addr = command->cmd_dma;
1580                 while (sg_len) {
1581                         sg_element[sg_count].segment_base_lo = sg_addr;
1582                         if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1583                                 sg_element[sg_count].length_segment_base_hi =
1584                                         PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1585                                 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1586                                 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1587                         } else {
1588                                 sg_element[sg_count].length_segment_base_hi =
1589                                         PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1590                                 sg_len = 0;
1591                         }
1592                         sg_count++;
1593                 }
1594
1595                 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1596
1597                 sbp2util_cpu_to_be32_buffer(sg_element,
1598                                             (sizeof(struct sbp2_unrestricted_page_table)) *
1599                                              sg_count);
1600         }
1601 }
1602
1603 static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1604                                     struct sbp2_command_info *command,
1605                                     unchar *scsi_cmd,
1606                                     unsigned int scsi_use_sg,
1607                                     unsigned int scsi_request_bufflen,
1608                                     void *scsi_request_buffer,
1609                                     enum dma_data_direction dma_dir)
1610 {
1611         struct sbp2_fwhost_info *hi = scsi_id->hi;
1612         struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
1613         struct sbp2_command_orb *command_orb = &command->command_orb;
1614         u32 orb_direction;
1615
1616         /*
1617          * Set-up our command ORB.
1618          *
1619          * NOTE: We're doing unrestricted page tables (s/g), as this is
1620          * best performance (at least with the devices I have). This means
1621          * that data_size becomes the number of s/g elements, and
1622          * page_size should be zero (for unrestricted).
1623          */
1624         command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1625         command_orb->next_ORB_lo = 0x0;
1626         command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1627         command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
1628         command_orb->misc |= ORB_SET_NOTIFY(1);
1629
1630         if (dma_dir == DMA_NONE)
1631                 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1632         else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
1633                 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1634         else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
1635                 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1636         else {
1637                 SBP2_INFO("Falling back to DMA_NONE");
1638                 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1639         }
1640
1641         /* set up our page table stuff */
1642         if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1643                 command_orb->data_descriptor_hi = 0x0;
1644                 command_orb->data_descriptor_lo = 0x0;
1645                 command_orb->misc |= ORB_SET_DIRECTION(1);
1646         } else if (scsi_use_sg)
1647                 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
1648                                          sgpnt, orb_direction, dma_dir);
1649         else
1650                 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
1651                                             orb_direction, scsi_request_bufflen,
1652                                             scsi_request_buffer, dma_dir);
1653
1654         sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1655
1656         memset(command_orb->cdb, 0, 12);
1657         memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1658 }
1659
1660 static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
1661                                  struct sbp2_command_info *command)
1662 {
1663         struct sbp2_fwhost_info *hi = scsi_id->hi;
1664         struct sbp2_command_orb *command_orb = &command->command_orb;
1665         struct sbp2_command_orb *last_orb;
1666         dma_addr_t last_orb_dma;
1667         u64 addr = scsi_id->command_block_agent_addr;
1668         quadlet_t data[2];
1669         size_t length;
1670         unsigned long flags;
1671
1672         pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
1673                                        sizeof(struct sbp2_command_orb),
1674                                        PCI_DMA_TODEVICE);
1675         pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
1676                                        sizeof(command->scatter_gather_element),
1677                                        PCI_DMA_BIDIRECTIONAL);
1678
1679         /* check to see if there are any previous orbs to use */
1680         spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1681         last_orb = scsi_id->last_orb;
1682         last_orb_dma = scsi_id->last_orb_dma;
1683         if (!last_orb) {
1684                 /*
1685                  * last_orb == NULL means: We know that the target's fetch agent
1686                  * is not active right now.
1687                  */
1688                 addr += SBP2_ORB_POINTER_OFFSET;
1689                 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1690                 data[1] = command->command_orb_dma;
1691                 sbp2util_cpu_to_be32_buffer(data, 8);
1692                 length = 8;
1693         } else {
1694                 /*
1695                  * last_orb != NULL means: We know that the target's fetch agent
1696                  * is (very probably) not dead or in reset state right now.
1697                  * We have an ORB already sent that we can append a new one to.
1698                  * The target's fetch agent may or may not have read this
1699                  * previous ORB yet.
1700                  */
1701                 pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
1702                                             sizeof(struct sbp2_command_orb),
1703                                             PCI_DMA_TODEVICE);
1704                 last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
1705                 wmb();
1706                 /* Tells hardware that this pointer is valid */
1707                 last_orb->next_ORB_hi = 0;
1708                 pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
1709                                                sizeof(struct sbp2_command_orb),
1710                                                PCI_DMA_TODEVICE);
1711                 addr += SBP2_DOORBELL_OFFSET;
1712                 data[0] = 0;
1713                 length = 4;
1714         }
1715         scsi_id->last_orb = command_orb;
1716         scsi_id->last_orb_dma = command->command_orb_dma;
1717         spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1718
1719         if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
1720                 /*
1721                  * sbp2util_node_write_no_wait failed. We certainly ran out
1722                  * of transaction labels, perhaps just because there were no
1723                  * context switches which gave khpsbpkt a chance to collect
1724                  * free tlabels. Try again in non-atomic context. If necessary,
1725                  * the workqueue job will sleep to guaranteedly get a tlabel.
1726                  * We do not accept new commands until the job is over.
1727                  */
1728                 scsi_block_requests(scsi_id->scsi_host);
1729                 PREPARE_WORK(&scsi_id->protocol_work,
1730                              last_orb ? sbp2util_write_doorbell:
1731                                         sbp2util_write_orb_pointer
1732                              /* */);
1733                 schedule_work(&scsi_id->protocol_work);
1734         }
1735 }
1736
1737 static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
1738                              struct scsi_cmnd *SCpnt,
1739                              void (*done)(struct scsi_cmnd *))
1740 {
1741         unchar *cmd = (unchar *) SCpnt->cmnd;
1742         unsigned int request_bufflen = SCpnt->request_bufflen;
1743         struct sbp2_command_info *command;
1744
1745         command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
1746         if (!command)
1747                 return -EIO;
1748
1749         sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
1750                                 request_bufflen, SCpnt->request_buffer,
1751                                 SCpnt->sc_data_direction);
1752         sbp2_link_orb_command(scsi_id, command);
1753
1754         return 0;
1755 }
1756
1757 /*
1758  * Translates SBP-2 status into SCSI sense data for check conditions
1759  */
1760 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
1761 {
1762         /* OK, it's pretty ugly... ;-) */
1763         sense_data[0] = 0x70;
1764         sense_data[1] = 0x0;
1765         sense_data[2] = sbp2_status[9];
1766         sense_data[3] = sbp2_status[12];
1767         sense_data[4] = sbp2_status[13];
1768         sense_data[5] = sbp2_status[14];
1769         sense_data[6] = sbp2_status[15];
1770         sense_data[7] = 10;
1771         sense_data[8] = sbp2_status[16];
1772         sense_data[9] = sbp2_status[17];
1773         sense_data[10] = sbp2_status[18];
1774         sense_data[11] = sbp2_status[19];
1775         sense_data[12] = sbp2_status[10];
1776         sense_data[13] = sbp2_status[11];
1777         sense_data[14] = sbp2_status[20];
1778         sense_data[15] = sbp2_status[21];
1779
1780         return sbp2_status[8] & 0x3f;
1781 }
1782
1783 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1784                                     int destid, quadlet_t *data, u64 addr,
1785                                     size_t length, u16 fl)
1786 {
1787         struct sbp2_fwhost_info *hi;
1788         struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
1789         struct scsi_cmnd *SCpnt = NULL;
1790         struct sbp2_status_block *sb;
1791         u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
1792         struct sbp2_command_info *command;
1793         unsigned long flags;
1794
1795         if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1796                 SBP2_ERR("Wrong size of status block");
1797                 return RCODE_ADDRESS_ERROR;
1798         }
1799         if (unlikely(!host)) {
1800                 SBP2_ERR("host is NULL - this is bad!");
1801                 return RCODE_ADDRESS_ERROR;
1802         }
1803         hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
1804         if (unlikely(!hi)) {
1805                 SBP2_ERR("host info is NULL - this is bad!");
1806                 return RCODE_ADDRESS_ERROR;
1807         }
1808
1809         /* Find the unit which wrote the status. */
1810         list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
1811                 if (scsi_id_tmp->ne->nodeid == nodeid &&
1812                     scsi_id_tmp->status_fifo_addr == addr) {
1813                         scsi_id = scsi_id_tmp;
1814                         break;
1815                 }
1816         }
1817         if (unlikely(!scsi_id)) {
1818                 SBP2_ERR("scsi_id is NULL - device is gone?");
1819                 return RCODE_ADDRESS_ERROR;
1820         }
1821
1822         /* Put response into scsi_id status fifo buffer. The first two bytes
1823          * come in big endian bit order. Often the target writes only a
1824          * truncated status block, minimally the first two quadlets. The rest
1825          * is implied to be zeros. */
1826         sb = &scsi_id->status_block;
1827         memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1828         memcpy(sb, data, length);
1829         sbp2util_be32_to_cpu_buffer(sb, 8);
1830
1831         /* Ignore unsolicited status. Handle command ORB status. */
1832         if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
1833                 command = NULL;
1834         else
1835                 command = sbp2util_find_command_for_orb(scsi_id,
1836                                                         sb->ORB_offset_lo);
1837         if (command) {
1838                 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1839                                             sizeof(struct sbp2_command_orb),
1840                                             PCI_DMA_TODEVICE);
1841                 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1842                                             sizeof(command->scatter_gather_element),
1843                                             PCI_DMA_BIDIRECTIONAL);
1844                 /* Grab SCSI command pointers and check status. */
1845                 /*
1846                  * FIXME: If the src field in the status is 1, the ORB DMA must
1847                  * not be reused until status for a subsequent ORB is received.
1848                  */
1849                 SCpnt = command->Current_SCpnt;
1850                 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1851                 sbp2util_mark_command_completed(scsi_id, command);
1852                 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1853
1854                 if (SCpnt) {
1855                         u32 h = sb->ORB_offset_hi_misc;
1856                         u32 r = STATUS_GET_RESP(h);
1857
1858                         if (r != RESP_STATUS_REQUEST_COMPLETE) {
1859                                 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
1860                                           r, STATUS_GET_SBP_STATUS(h));
1861                                 scsi_status =
1862                                         r == RESP_STATUS_TRANSPORT_FAILURE ?
1863                                         SBP2_SCSI_STATUS_BUSY :
1864                                         SBP2_SCSI_STATUS_COMMAND_TERMINATED;
1865                         }
1866
1867                         if (STATUS_GET_LEN(h) > 1)
1868                                 scsi_status = sbp2_status_to_sense_data(
1869                                         (unchar *)sb, SCpnt->sense_buffer);
1870
1871                         if (STATUS_TEST_DEAD(h))
1872                                 sbp2_agent_reset(scsi_id, 0);
1873                 }
1874
1875                 /* Check here to see if there are no commands in-use. If there
1876                  * are none, we know that the fetch agent left the active state
1877                  * _and_ that we did not reactivate it yet. Therefore clear
1878                  * last_orb so that next time we write directly to the
1879                  * ORB_POINTER register. That way the fetch agent does not need
1880                  * to refetch the next_ORB. */
1881                 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1882                 if (list_empty(&scsi_id->cmd_orb_inuse))
1883                         scsi_id->last_orb = NULL;
1884                 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1885
1886         } else {
1887                 /* It's probably status after a management request. */
1888                 if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
1889                     (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
1890                     (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
1891                     (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
1892                         scsi_id->access_complete = 1;
1893                         wake_up_interruptible(&sbp2_access_wq);
1894                 }
1895         }
1896
1897         if (SCpnt)
1898                 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
1899                                           command->Current_done);
1900         return RCODE_COMPLETE;
1901 }
1902
1903 /**************************************
1904  * SCSI interface related section
1905  **************************************/
1906
1907 static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1908                                  void (*done)(struct scsi_cmnd *))
1909 {
1910         struct scsi_id_instance_data *scsi_id =
1911                 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
1912         struct sbp2_fwhost_info *hi;
1913         int result = DID_NO_CONNECT << 16;
1914
1915         if (unlikely(!sbp2util_node_is_available(scsi_id)))
1916                 goto done;
1917
1918         hi = scsi_id->hi;
1919
1920         if (unlikely(!hi)) {
1921                 SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
1922                 goto done;
1923         }
1924
1925         /* Multiple units are currently represented to the SCSI core as separate
1926          * targets, not as one target with multiple LUs. Therefore return
1927          * selection time-out to any IO directed at non-zero LUNs. */
1928         if (unlikely(SCpnt->device->lun))
1929                 goto done;
1930
1931         /* handle the request sense command here (auto-request sense) */
1932         if (SCpnt->cmnd[0] == REQUEST_SENSE) {
1933                 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
1934                 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
1935                 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
1936                 return 0;
1937         }
1938
1939         if (unlikely(!hpsb_node_entry_valid(scsi_id->ne))) {
1940                 SBP2_ERR("Bus reset in progress - rejecting command");
1941                 result = DID_BUS_BUSY << 16;
1942                 goto done;
1943         }
1944
1945         /* Bidirectional commands are not yet implemented,
1946          * and unknown transfer direction not handled. */
1947         if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
1948                 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1949                 result = DID_ERROR << 16;
1950                 goto done;
1951         }
1952
1953         if (sbp2_send_command(scsi_id, SCpnt, done)) {
1954                 SBP2_ERR("Error sending SCSI command");
1955                 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1956                                           SCpnt, done);
1957         }
1958         return 0;
1959
1960 done:
1961         SCpnt->result = result;
1962         done(SCpnt);
1963         return 0;
1964 }
1965
1966 static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
1967                                            u32 status)
1968 {
1969         struct sbp2_fwhost_info *hi = scsi_id->hi;
1970         struct list_head *lh;
1971         struct sbp2_command_info *command;
1972         unsigned long flags;
1973
1974         spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1975         while (!list_empty(&scsi_id->cmd_orb_inuse)) {
1976                 lh = scsi_id->cmd_orb_inuse.next;
1977                 command = list_entry(lh, struct sbp2_command_info, list);
1978                 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1979                                             sizeof(struct sbp2_command_orb),
1980                                             PCI_DMA_TODEVICE);
1981                 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1982                                             sizeof(command->scatter_gather_element),
1983                                             PCI_DMA_BIDIRECTIONAL);
1984                 sbp2util_mark_command_completed(scsi_id, command);
1985                 if (command->Current_SCpnt) {
1986                         command->Current_SCpnt->result = status << 16;
1987                         command->Current_done(command->Current_SCpnt);
1988                 }
1989         }
1990         spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
1991
1992         return;
1993 }
1994
1995 /*
1996  * Complete a regular SCSI command. Can be called in atomic context.
1997  */
1998 static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
1999                                       u32 scsi_status, struct scsi_cmnd *SCpnt,
2000                                       void (*done)(struct scsi_cmnd *))
2001 {
2002         if (!SCpnt) {
2003                 SBP2_ERR("SCpnt is NULL");
2004                 return;
2005         }
2006
2007         switch (scsi_status) {
2008         case SBP2_SCSI_STATUS_GOOD:
2009                 SCpnt->result = DID_OK << 16;
2010                 break;
2011
2012         case SBP2_SCSI_STATUS_BUSY:
2013                 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2014                 SCpnt->result = DID_BUS_BUSY << 16;
2015                 break;
2016
2017         case SBP2_SCSI_STATUS_CHECK_CONDITION:
2018                 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
2019                 break;
2020
2021         case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2022                 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2023                 SCpnt->result = DID_NO_CONNECT << 16;
2024                 scsi_print_command(SCpnt);
2025                 break;
2026
2027         case SBP2_SCSI_STATUS_CONDITION_MET:
2028         case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2029         case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2030                 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2031                 SCpnt->result = DID_ERROR << 16;
2032                 scsi_print_command(SCpnt);
2033                 break;
2034
2035         default:
2036                 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2037                 SCpnt->result = DID_ERROR << 16;
2038         }
2039
2040         /* If a bus reset is in progress and there was an error, complete
2041          * the command as busy so that it will get retried. */
2042         if (!hpsb_node_entry_valid(scsi_id->ne)
2043             && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2044                 SBP2_ERR("Completing command with busy (bus reset)");
2045                 SCpnt->result = DID_BUS_BUSY << 16;
2046         }
2047
2048         /* Tell the SCSI stack that we're done with this command. */
2049         done(SCpnt);
2050 }
2051
2052 static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2053 {
2054         struct scsi_id_instance_data *scsi_id =
2055                 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2056
2057         scsi_id->sdev = sdev;
2058         sdev->allow_restart = 1;
2059
2060         if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
2061                 sdev->inquiry_len = 36;
2062         return 0;
2063 }
2064
2065 static int sbp2scsi_slave_configure(struct scsi_device *sdev)
2066 {
2067         struct scsi_id_instance_data *scsi_id =
2068                 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2069
2070         blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
2071         sdev->use_10_for_rw = 1;
2072
2073         if (sdev->type == TYPE_DISK &&
2074             scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2075                 sdev->skip_ms_page_8 = 1;
2076         if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2077                 sdev->fix_capacity = 1;
2078         return 0;
2079 }
2080
2081 static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2082 {
2083         ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
2084         return;
2085 }
2086
2087 /*
2088  * Called by scsi stack when something has really gone wrong.
2089  * Usually called when a command has timed-out for some reason.
2090  */
2091 static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2092 {
2093         struct scsi_id_instance_data *scsi_id =
2094                 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2095         struct sbp2_fwhost_info *hi = scsi_id->hi;
2096         struct sbp2_command_info *command;
2097         unsigned long flags;
2098
2099         SBP2_INFO("aborting sbp2 command");
2100         scsi_print_command(SCpnt);
2101
2102         if (sbp2util_node_is_available(scsi_id)) {
2103                 sbp2_agent_reset(scsi_id, 1);
2104
2105                 /* Return a matching command structure to the free pool. */
2106                 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
2107                 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2108                 if (command) {
2109                         pci_dma_sync_single_for_cpu(hi->host->pdev,
2110                                                     command->command_orb_dma,
2111                                                     sizeof(struct sbp2_command_orb),
2112                                                     PCI_DMA_TODEVICE);
2113                         pci_dma_sync_single_for_cpu(hi->host->pdev,
2114                                                     command->sge_dma,
2115                                                     sizeof(command->scatter_gather_element),
2116                                                     PCI_DMA_BIDIRECTIONAL);
2117                         sbp2util_mark_command_completed(scsi_id, command);
2118                         if (command->Current_SCpnt) {
2119                                 command->Current_SCpnt->result = DID_ABORT << 16;
2120                                 command->Current_done(command->Current_SCpnt);
2121                         }
2122                 }
2123                 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
2124
2125                 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2126         }
2127
2128         return SUCCESS;
2129 }
2130
2131 /*
2132  * Called by scsi stack when something has really gone wrong.
2133  */
2134 static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
2135 {
2136         struct scsi_id_instance_data *scsi_id =
2137                 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2138
2139         SBP2_INFO("reset requested");
2140
2141         if (sbp2util_node_is_available(scsi_id)) {
2142                 SBP2_INFO("generating sbp2 fetch agent reset");
2143                 sbp2_agent_reset(scsi_id, 1);
2144         }
2145
2146         return SUCCESS;
2147 }
2148
2149 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2150                                            struct device_attribute *attr,
2151                                            char *buf)
2152 {
2153         struct scsi_device *sdev;
2154         struct scsi_id_instance_data *scsi_id;
2155
2156         if (!(sdev = to_scsi_device(dev)))
2157                 return 0;
2158
2159         if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2160                 return 0;
2161
2162         return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
2163                        scsi_id->ud->id, ORB_SET_LUN(scsi_id->lun));
2164 }
2165
2166 MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2167 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2168 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2169 MODULE_LICENSE("GPL");
2170
2171 static int sbp2_module_init(void)
2172 {
2173         int ret;
2174
2175         if (sbp2_serialize_io) {
2176                 sbp2_shost_template.can_queue = 1;
2177                 sbp2_shost_template.cmd_per_lun = 1;
2178         }
2179
2180         if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2181             (sbp2_max_sectors * 512) > (128 * 1024))
2182                 sbp2_max_sectors = 128 * 1024 / 512;
2183         sbp2_shost_template.max_sectors = sbp2_max_sectors;
2184
2185         hpsb_register_highlevel(&sbp2_highlevel);
2186         ret = hpsb_register_protocol(&sbp2_driver);
2187         if (ret) {
2188                 SBP2_ERR("Failed to register protocol");
2189                 hpsb_unregister_highlevel(&sbp2_highlevel);
2190                 return ret;
2191         }
2192         return 0;
2193 }
2194
2195 static void __exit sbp2_module_exit(void)
2196 {
2197         hpsb_unregister_protocol(&sbp2_driver);
2198         hpsb_unregister_highlevel(&sbp2_highlevel);
2199 }
2200
2201 module_init(sbp2_module_init);
2202 module_exit(sbp2_module_exit);