WorkStruct: make allyesconfig
[safe/jmp/linux-2.6] / drivers / ieee1394 / sbp2.c
index 372a772..cd156d4 100644 (file)
  *       but the code needs additional debugging.
  */
 
-#include <linux/config.h>
+#include <linux/blkdev.h>
+#include <linux/compiler.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/dma-mapping.h>
+#include <linux/gfp.h>
+#include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/list.h>
-#include <linux/string.h>
-#include <linux/slab.h>
-#include <linux/interrupt.h>
-#include <linux/fs.h>
-#include <linux/poll.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
-#include <linux/types.h>
-#include <linux/delay.h>
-#include <linux/sched.h>
-#include <linux/blkdev.h>
-#include <linux/smp_lock.h>
-#include <linux/init.h>
 #include <linux/pci.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/stat.h>
+#include <linux/string.h>
+#include <linux/stringify.h>
+#include <linux/types.h>
+#include <linux/wait.h>
 
-#include <asm/current.h>
-#include <asm/uaccess.h>
-#include <asm/io.h>
 #include <asm/byteorder.h>
-#include <asm/atomic.h>
-#include <asm/system.h>
+#include <asm/errno.h>
+#include <asm/param.h>
 #include <asm/scatterlist.h>
+#include <asm/system.h>
+#include <asm/types.h>
+
+#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
+#include <asm/io.h> /* for bus_to_virt */
+#endif
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_host.h>
 
 #include "csr1212.h"
+#include "highlevel.h"
+#include "hosts.h"
 #include "ieee1394.h"
-#include "ieee1394_types.h"
 #include "ieee1394_core.h"
-#include "nodemgr.h"
-#include "hosts.h"
-#include "highlevel.h"
+#include "ieee1394_hotplug.h"
 #include "ieee1394_transactions.h"
+#include "ieee1394_types.h"
+#include "nodemgr.h"
 #include "sbp2.h"
 
 /*
@@ -117,7 +123,8 @@ MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default
  */
 static int max_sectors = SBP2_MAX_SECTORS;
 module_param(max_sectors, int, 0444);
-MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = 255)");
+MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = "
+                __stringify(SBP2_MAX_SECTORS) ")");
 
 /*
  * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
@@ -125,28 +132,52 @@ MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = 2
  * talking to a single sbp2 device at the same time (filesystem coherency,
  * etc.). If you're running an sbp2 device that supports multiple logins,
  * and you're either running read-only filesystems or some sort of special
- * filesystem supporting multiple hosts (one such filesystem is OpenGFS,
- * see opengfs.sourceforge.net for more info), then set exclusive_login
- * to zero. Note: The Oxsemi OXFW911 sbp2 chipset supports up to four
- * concurrent logins.
+ * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
+ * File System, or Lustre, then set exclusive_login to zero.
+ *
+ * So far only bridges from Oxford Semiconductor are known to support
+ * concurrent logins. Depending on firmware, four or two concurrent logins
+ * are possible on OXFW911 and newer Oxsemi bridges.
  */
 static int exclusive_login = 1;
 module_param(exclusive_login, int, 0644);
 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
 
 /*
- * SCSI inquiry hack for really badly behaved sbp2 devices. Turn this on
- * if your sbp2 device is not properly handling the SCSI inquiry command.
- * This hack makes the inquiry look more like a typical MS Windows
- * inquiry.
+ * If any of the following workarounds is required for your device to work,
+ * please submit the kernel messages logged by sbp2 to the linux1394-devel
+ * mailing list.
+ *
+ * - 128kB max transfer
+ *   Limit transfer size. Necessary for some old bridges.
+ *
+ * - 36 byte inquiry
+ *   When scsi_mod probes the device, let the inquiry command look like that
+ *   from MS Windows.
+ *
+ * - skip mode page 8
+ *   Suppress sending of mode_sense for mode page 8 if the device pretends to
+ *   support the SCSI Primary Block commands instead of Reduced Block Commands.
  *
- * If force_inquiry_hack=1 is required for your device to work,
- * please submit the logged sbp2_firmware_revision value of this device to
- * the linux1394-devel mailing list.
+ * - fix capacity
+ *   Tell sd_mod to correct the last sector number reported by read_capacity.
+ *   Avoids access beyond actual disk limits on devices with an off-by-one bug.
+ *   Don't use this with devices which don't have this bug.
+ *
+ * - override internal blacklist
+ *   Instead of adding to the built-in blacklist, use only the workarounds
+ *   specified in the module load parameter.
+ *   Useful if a blacklist entry interfered with a non-broken device.
  */
-static int force_inquiry_hack;
-module_param(force_inquiry_hack, int, 0444);
-MODULE_PARM_DESC(force_inquiry_hack, "Force SCSI inquiry hack (default = 0)");
+static int sbp2_default_workarounds;
+module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
+MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
+       ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
+       ", 36 byte inquiry = "    __stringify(SBP2_WORKAROUND_INQUIRY_36)
+       ", skip mode page 8 = "   __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
+       ", fix capacity = "       __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
+       ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
+       ", or a combination)");
 
 /*
  * Export information about protocols/devices supported by this driver.
@@ -178,9 +209,9 @@ static u32 global_outstanding_command_orbs = 0;
 #define outstanding_orb_incr global_outstanding_command_orbs++
 #define outstanding_orb_decr global_outstanding_command_orbs--
 #else
-#define SBP2_ORB_DEBUG(fmt, args...)
-#define outstanding_orb_incr
-#define outstanding_orb_decr
+#define SBP2_ORB_DEBUG(fmt, args...)   do {} while (0)
+#define outstanding_orb_incr           do {} while (0)
+#define outstanding_orb_decr           do {} while (0)
 #endif
 
 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
@@ -192,8 +223,8 @@ static u32 global_outstanding_command_orbs = 0;
                 --global_outstanding_dmas, ## args)
 static u32 global_outstanding_dmas = 0;
 #else
-#define SBP2_DMA_ALLOC(fmt, args...)
-#define SBP2_DMA_FREE(fmt, args...)
+#define SBP2_DMA_ALLOC(fmt, args...)   do {} while (0)
+#define SBP2_DMA_FREE(fmt, args...)    do {} while (0)
 #endif
 
 #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
@@ -207,13 +238,14 @@ static u32 global_outstanding_dmas = 0;
 #define SBP2_NOTICE(fmt, args...)      HPSB_NOTICE("sbp2: "fmt, ## args)
 #define SBP2_WARN(fmt, args...)                HPSB_WARN("sbp2: "fmt, ## args)
 #else
-#define SBP2_DEBUG(fmt, args...)
+#define SBP2_DEBUG(fmt, args...)       do {} while (0)
 #define SBP2_INFO(fmt, args...)                HPSB_INFO("sbp2: "fmt, ## args)
 #define SBP2_NOTICE(fmt, args...)       HPSB_NOTICE("sbp2: "fmt, ## args)
 #define SBP2_WARN(fmt, args...)         HPSB_WARN("sbp2: "fmt, ## args)
 #endif
 
 #define SBP2_ERR(fmt, args...)         HPSB_ERR("sbp2: "fmt, ## args)
+#define SBP2_DEBUG_ENTER()             SBP2_DEBUG("%s", __FUNCTION__)
 
 /*
  * Globals
@@ -264,18 +296,59 @@ static struct hpsb_protocol_driver sbp2_driver = {
        },
 };
 
-
-/* List of device firmware's that require a forced 36 byte inquiry.  */
-static u32 sbp2_broken_inquiry_list[] = {
-       0x00002800,     /* Stefan Richter <richtest@bauwesen.tu-cottbus.de> */
-                       /* DViCO Momobay CX-1 */
-       0x00000200      /* Andreas Plesch <plesch@fas.harvard.edu> */
-                       /* QPS Fire DVDBurner */
+/*
+ * List of devices with known bugs.
+ *
+ * The firmware_revision field, masked with 0xffff00, is the best indicator
+ * for the type of bridge chip of a device.  It yields a few false positives
+ * but this did not break correctly behaving devices so far.
+ */
+static const struct {
+       u32 firmware_revision;
+       u32 model_id;
+       unsigned workarounds;
+} sbp2_workarounds_table[] = {
+       /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
+               .firmware_revision      = 0x002800,
+               .model_id               = 0x001010,
+               .workarounds            = SBP2_WORKAROUND_INQUIRY_36 |
+                                         SBP2_WORKAROUND_MODE_SENSE_8,
+       },
+       /* Initio bridges, actually only needed for some older ones */ {
+               .firmware_revision      = 0x000200,
+               .workarounds            = SBP2_WORKAROUND_INQUIRY_36,
+       },
+       /* Symbios bridge */ {
+               .firmware_revision      = 0xa0b800,
+               .workarounds            = SBP2_WORKAROUND_128K_MAX_TRANS,
+       },
+       /*
+        * Note about the following Apple iPod blacklist entries:
+        *
+        * There are iPods (2nd gen, 3rd gen) with model_id==0.  Since our
+        * matching logic treats 0 as a wildcard, we cannot match this ID
+        * without rewriting the matching routine.  Fortunately these iPods
+        * do not feature the read_capacity bug according to one report.
+        * Read_capacity behaviour as well as model_id could change due to
+        * Apple-supplied firmware updates though.
+        */
+       /* iPod 4th generation */ {
+               .firmware_revision      = 0x0a2700,
+               .model_id               = 0x000021,
+               .workarounds            = SBP2_WORKAROUND_FIX_CAPACITY,
+       },
+       /* iPod mini */ {
+               .firmware_revision      = 0x0a2700,
+               .model_id               = 0x000023,
+               .workarounds            = SBP2_WORKAROUND_FIX_CAPACITY,
+       },
+       /* iPod Photo */ {
+               .firmware_revision      = 0x0a2700,
+               .model_id               = 0x00007e,
+               .workarounds            = SBP2_WORKAROUND_FIX_CAPACITY,
+       }
 };
 
-#define NUM_BROKEN_INQUIRY_DEVS \
-       (sizeof(sbp2_broken_inquiry_list)/sizeof(*sbp2_broken_inquiry_list))
-
 /**************************************
  * General utility functions
  **************************************/
@@ -284,7 +357,7 @@ static u32 sbp2_broken_inquiry_list[] = {
 /*
  * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
  */
-static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
+static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
 {
        u32 *temp = buffer;
 
@@ -297,7 +370,7 @@ static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
 /*
  * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
  */
-static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
+static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
 {
        u32 *temp = buffer;
 
@@ -308,8 +381,8 @@ static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
 }
 #else /* BIG_ENDIAN */
 /* Why waste the cpu cycles? */
-#define sbp2util_be32_to_cpu_buffer(x,y)
-#define sbp2util_cpu_to_be32_buffer(x,y)
+#define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
+#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
 #endif
 
 #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
@@ -345,24 +418,26 @@ static void sbp2util_packet_dump(void *buffer, int length, char *dump_name,
        return;
 }
 #else
-#define sbp2util_packet_dump(w,x,y,z)
+#define sbp2util_packet_dump(w,x,y,z) do {} while (0)
 #endif
 
+static DECLARE_WAIT_QUEUE_HEAD(access_wq);
+
 /*
- * Goofy routine that basically does a down_timeout function.
+ * Waits for completion of an SBP-2 access request.
+ * Returns nonzero if timed out or prematurely interrupted.
  */
-static int sbp2util_down_timeout(atomic_t *done, int timeout)
+static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
+                                  int timeout)
 {
-       int i;
+       long leftover = wait_event_interruptible_timeout(
+                               access_wq, scsi_id->access_complete, timeout);
 
-       for (i = timeout; (i > 0 && atomic_read(done) == 0); i-= HZ/10) {
-               if (msleep_interruptible(100))  /* 100ms */
-                       return 1;
-       }
-       return (i > 0) ? 0 : 1;
+       scsi_id->access_complete = 0;
+       return leftover <= 0;
 }
 
-/* Free's an allocated packet */
+/* Frees an allocated packet */
 static void sbp2_free_packet(struct hpsb_packet *packet)
 {
        hpsb_free_tlabel(packet);
@@ -396,6 +471,49 @@ static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
        return 0;
 }
 
+static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
+                                       u64 offset, quadlet_t *data, size_t len)
+{
+       /*
+        * There is a small window after a bus reset within which the node
+        * entry's generation is current but the reconnect wasn't completed.
+        */
+       if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
+               return;
+
+       if (hpsb_node_write(scsi_id->ne,
+                           scsi_id->sbp2_command_block_agent_addr + offset,
+                           data, len))
+               SBP2_ERR("sbp2util_notify_fetch_agent failed.");
+       /*
+        * Now accept new SCSI commands, unless a bus reset happended during
+        * hpsb_node_write.
+        */
+       if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET))
+               scsi_unblock_requests(scsi_id->scsi_host);
+}
+
+static void sbp2util_write_orb_pointer(struct work_struct *work)
+{
+       struct scsi_id_instance_data *scsi_id =
+               container_of(work, struct scsi_id_instance_data,
+                            protocol_work.work);
+       quadlet_t data[2];
+
+       data[0] = ORB_SET_NODE_ID(scsi_id->hi->host->node_id);
+       data[1] = scsi_id->last_orb_dma;
+       sbp2util_cpu_to_be32_buffer(data, 8);
+       sbp2util_notify_fetch_agent(scsi_id, SBP2_ORB_POINTER_OFFSET, data, 8);
+}
+
+static void sbp2util_write_doorbell(struct work_struct *work)
+{
+       struct scsi_id_instance_data *scsi_id =
+               container_of(work, struct scsi_id_instance_data,
+                            protocol_work.work);
+       sbp2util_notify_fetch_agent(scsi_id, SBP2_DOORBELL_OFFSET, NULL, 4);
+}
+
 /*
  * This function is called to create a pool of command orbs used for
  * command processing. It is called when a new sbp2 device is detected.
@@ -420,7 +538,7 @@ static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_i
                command->command_orb_dma =
                    pci_map_single(hi->host->pdev, &command->command_orb,
                                   sizeof(struct sbp2_command_orb),
-                                  PCI_DMA_BIDIRECTIONAL);
+                                  PCI_DMA_TODEVICE);
                SBP2_DMA_ALLOC("single command orb DMA");
                command->sge_dma =
                    pci_map_single(hi->host->pdev,
@@ -453,7 +571,7 @@ static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_
                        /* Release our generic DMA's */
                        pci_unmap_single(host->pdev, command->command_orb_dma,
                                         sizeof(struct sbp2_command_orb),
-                                        PCI_DMA_BIDIRECTIONAL);
+                                        PCI_DMA_TODEVICE);
                        SBP2_DMA_FREE("single command orb DMA");
                        pci_unmap_single(host->pdev, command->sge_dma,
                                         sizeof(command->scatter_gather_element),
@@ -496,22 +614,17 @@ static struct sbp2_command_info *sbp2util_find_command_for_orb(
 /*
  * This function finds the sbp2_command for a given outstanding SCpnt.
  * Only looks at the inuse list.
+ * Must be called with scsi_id->sbp2_command_orb_lock held.
  */
-static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(struct scsi_id_instance_data *scsi_id, void *SCpnt)
+static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
+               struct scsi_id_instance_data *scsi_id, void *SCpnt)
 {
        struct sbp2_command_info *command;
-       unsigned long flags;
 
-       spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
-       if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
-               list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
-                       if (command->Current_SCpnt == SCpnt) {
-                               spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
+       if (!list_empty(&scsi_id->sbp2_command_orb_inuse))
+               list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list)
+                       if (command->Current_SCpnt == SCpnt)
                                return command;
-                       }
-               }
-       }
-       spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
        return NULL;
 }
 
@@ -536,7 +649,7 @@ static struct sbp2_command_info *sbp2util_allocate_command_orb(
                command->Current_SCpnt = Current_SCpnt;
                list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
        } else {
-               SBP2_ERR("sbp2util_allocate_command_orb - No orbs available!");
+               SBP2_ERR("%s: no orbs available", __FUNCTION__);
        }
        spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
        return command;
@@ -550,7 +663,7 @@ static void sbp2util_free_command_dma(struct sbp2_command_info *command)
        struct hpsb_host *host;
 
        if (!scsi_id) {
-               printk(KERN_ERR "%s: scsi_id == NULL\n", __FUNCTION__);
+               SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
                return;
        }
 
@@ -580,17 +693,15 @@ static void sbp2util_free_command_dma(struct sbp2_command_info *command)
 
 /*
  * This function moves a command to the completed orb list.
+ * Must be called with scsi_id->sbp2_command_orb_lock held.
  */
-static void sbp2util_mark_command_completed(struct scsi_id_instance_data *scsi_id,
-                                           struct sbp2_command_info *command)
+static void sbp2util_mark_command_completed(
+               struct scsi_id_instance_data *scsi_id,
+               struct sbp2_command_info *command)
 {
-       unsigned long flags;
-
-       spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
        list_del(&command->list);
        sbp2util_free_command_dma(command);
        list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
-       spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
 }
 
 /*
@@ -611,7 +722,7 @@ static int sbp2_probe(struct device *dev)
        struct unit_directory *ud;
        struct scsi_id_instance_data *scsi_id;
 
-       SBP2_DEBUG("sbp2_probe");
+       SBP2_DEBUG_ENTER();
 
        ud = container_of(dev, struct unit_directory, device);
 
@@ -636,16 +747,23 @@ static int sbp2_remove(struct device *dev)
        struct scsi_id_instance_data *scsi_id;
        struct scsi_device *sdev;
 
-       SBP2_DEBUG("sbp2_remove");
+       SBP2_DEBUG_ENTER();
 
        ud = container_of(dev, struct unit_directory, device);
        scsi_id = ud->device.driver_data;
        if (!scsi_id)
                return 0;
 
-       /* Trigger shutdown functions in scsi's highlevel. */
-       if (scsi_id->scsi_host)
+       if (scsi_id->scsi_host) {
+               /* Get rid of enqueued commands if there is no chance to
+                * send them. */
+               if (!sbp2util_node_is_available(scsi_id))
+                       sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
+               /* scsi_remove_device() will trigger shutdown functions of SCSI
+                * highlevel drivers which would deadlock if blocked. */
+               atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
                scsi_unblock_requests(scsi_id->scsi_host);
+       }
        sdev = scsi_id->sdev;
        if (sdev) {
                scsi_id->sdev = NULL;
@@ -662,7 +780,7 @@ static int sbp2_update(struct unit_directory *ud)
 {
        struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
 
-       SBP2_DEBUG("sbp2_update");
+       SBP2_DEBUG_ENTER();
 
        if (sbp2_reconnect_device(scsi_id)) {
 
@@ -695,10 +813,12 @@ static int sbp2_update(struct unit_directory *ud)
         */
        sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
 
-       /* Make sure we unblock requests (since this is likely after a bus
-        * reset). */
-       scsi_unblock_requests(scsi_id->scsi_host);
-
+       /* Accept new commands unless there was another bus reset in the
+        * meantime. */
+       if (hpsb_node_entry_valid(scsi_id->ne)) {
+               atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
+               scsi_unblock_requests(scsi_id->scsi_host);
+       }
        return 0;
 }
 
@@ -710,7 +830,7 @@ static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud
        struct Scsi_Host *scsi_host = NULL;
        struct scsi_id_instance_data *scsi_id = NULL;
 
-       SBP2_DEBUG("sbp2_alloc_device");
+       SBP2_DEBUG_ENTER();
 
        scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
        if (!scsi_id) {
@@ -722,12 +842,13 @@ static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud
        scsi_id->ud = ud;
        scsi_id->speed_code = IEEE1394_SPEED_100;
        scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
-       atomic_set(&scsi_id->sbp2_login_complete, 0);
+       scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
        INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
        INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
        INIT_LIST_HEAD(&scsi_id->scsi_list);
        spin_lock_init(&scsi_id->sbp2_command_orb_lock);
-       scsi_id->sbp2_lun = 0;
+       atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
+       INIT_DELAYED_WORK(&scsi_id->protocol_work, NULL);
 
        ud->device.driver_data = scsi_id;
 
@@ -742,23 +863,45 @@ static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud
                hi->host = ud->ne->host;
                INIT_LIST_HEAD(&hi->scsi_ids);
 
-               /* Register our sbp2 status address space... */
-               hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host, &sbp2_ops,
-                                       SBP2_STATUS_FIFO_ADDRESS,
-                                       SBP2_STATUS_FIFO_ADDRESS +
-                                       SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(SBP2_MAX_UDS_PER_NODE+1));
 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
                /* Handle data movement if physical dma is not
-                * enabled/supportedon host controller */
-               hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host, &sbp2_physdma_ops,
-                                       0x0ULL, 0xfffffffcULL);
+                * enabled or not supported on host controller */
+               if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
+                                            &sbp2_physdma_ops,
+                                            0x0ULL, 0xfffffffcULL)) {
+                       SBP2_ERR("failed to register lower 4GB address range");
+                       goto failed_alloc;
+               }
 #endif
        }
 
+       /* Prevent unloading of the 1394 host */
+       if (!try_module_get(hi->host->driver->owner)) {
+               SBP2_ERR("failed to get a reference on 1394 host driver");
+               goto failed_alloc;
+       }
+
        scsi_id->hi = hi;
 
        list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
 
+       /* Register the status FIFO address range. We could use the same FIFO
+        * for targets at different nodes. However we need different FIFOs per
+        * target in order to support multi-unit devices.
+        * The FIFO is located out of the local host controller's physical range
+        * but, if possible, within the posted write area. Status writes will
+        * then be performed as unified transactions. This slightly reduces
+        * bandwidth usage, and some Prolific based devices seem to require it.
+        */
+       scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
+                       &sbp2_highlevel, ud->ne->host, &sbp2_ops,
+                       sizeof(struct sbp2_status_block), sizeof(quadlet_t),
+                       ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
+       if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
+               SBP2_ERR("failed to allocate status FIFO address range");
+               goto failed_alloc;
+       }
+
        /* Register our host with the SCSI stack. */
        scsi_host = scsi_host_alloc(&scsi_driver_template,
                                    sizeof(unsigned long));
@@ -788,11 +931,14 @@ static void sbp2_host_reset(struct hpsb_host *host)
        struct scsi_id_instance_data *scsi_id;
 
        hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
-
-       if (hi) {
-               list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
+       if (!hi)
+               return;
+       list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
+               if (likely(atomic_read(&scsi_id->state) !=
+                          SBP2LU_STATE_IN_SHUTDOWN)) {
+                       atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
                        scsi_block_requests(scsi_id->scsi_host);
-       }
+               }
 }
 
 /*
@@ -804,7 +950,7 @@ static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
        struct sbp2scsi_host_info *hi = scsi_id->hi;
        int error;
 
-       SBP2_DEBUG("sbp2_start_device");
+       SBP2_DEBUG_ENTER();
 
        /* Login FIFO DMA */
        scsi_id->login_response =
@@ -856,56 +1002,8 @@ static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
                pci_alloc_consistent(hi->host->pdev,
                                     sizeof(struct sbp2_login_orb),
                                     &scsi_id->login_orb_dma);
-       if (!scsi_id->login_orb) {
-alloc_fail:
-               if (scsi_id->query_logins_response) {
-                       pci_free_consistent(hi->host->pdev,
-                                           sizeof(struct sbp2_query_logins_response),
-                                           scsi_id->query_logins_response,
-                                           scsi_id->query_logins_response_dma);
-                       SBP2_DMA_FREE("query logins response DMA");
-               }
-
-               if (scsi_id->query_logins_orb) {
-                       pci_free_consistent(hi->host->pdev,
-                                           sizeof(struct sbp2_query_logins_orb),
-                                           scsi_id->query_logins_orb,
-                                           scsi_id->query_logins_orb_dma);
-                       SBP2_DMA_FREE("query logins ORB DMA");
-               }
-
-               if (scsi_id->logout_orb) {
-                       pci_free_consistent(hi->host->pdev,
-                                           sizeof(struct sbp2_logout_orb),
-                                           scsi_id->logout_orb,
-                                           scsi_id->logout_orb_dma);
-                       SBP2_DMA_FREE("logout ORB DMA");
-               }
-
-               if (scsi_id->reconnect_orb) {
-                       pci_free_consistent(hi->host->pdev,
-                                           sizeof(struct sbp2_reconnect_orb),
-                                           scsi_id->reconnect_orb,
-                                           scsi_id->reconnect_orb_dma);
-                       SBP2_DMA_FREE("reconnect ORB DMA");
-               }
-
-               if (scsi_id->login_response) {
-                       pci_free_consistent(hi->host->pdev,
-                                           sizeof(struct sbp2_login_response),
-                                           scsi_id->login_response,
-                                           scsi_id->login_response_dma);
-                       SBP2_DMA_FREE("login FIFO DMA");
-               }
-
-               list_del(&scsi_id->scsi_list);
-
-               kfree(scsi_id);
-
-               SBP2_ERR("Could not allocate memory for scsi_id");
-
-               return -ENOMEM;
-       }
+       if (!scsi_id->login_orb)
+               goto alloc_fail;
        SBP2_DMA_ALLOC("consistent DMA region for login ORB");
 
        SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id);
@@ -925,9 +1023,7 @@ alloc_fail:
         * connected to the sbp2 device being removed. That host would
         * have a certain amount of time to relogin before the sbp2 device
         * allows someone else to login instead. One second makes sense. */
-       msleep_interruptible(1000);
-       if (signal_pending(current)) {
-               SBP2_WARN("aborting sbp2_start_device due to event");
+       if (msleep_interruptible(1000)) {
                sbp2_remove_device(scsi_id);
                return -EINTR;
        }
@@ -960,10 +1056,17 @@ alloc_fail:
        error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
        if (error) {
                SBP2_ERR("scsi_add_device failed");
+               sbp2_logout_device(scsi_id);
+               sbp2_remove_device(scsi_id);
                return error;
        }
 
        return 0;
+
+alloc_fail:
+       SBP2_ERR("Could not allocate memory for scsi_id");
+       sbp2_remove_device(scsi_id);
+       return -ENOMEM;
 }
 
 /*
@@ -973,7 +1076,7 @@ static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
 {
        struct sbp2scsi_host_info *hi;
 
-       SBP2_DEBUG("sbp2_remove_device");
+       SBP2_DEBUG_ENTER();
 
        if (!scsi_id)
                return;
@@ -985,7 +1088,7 @@ static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
                scsi_remove_host(scsi_id->scsi_host);
                scsi_host_put(scsi_id->scsi_host);
        }
-
+       flush_scheduled_work();
        sbp2util_remove_command_orb_pool(scsi_id);
 
        list_del(&scsi_id->scsi_list);
@@ -1038,8 +1141,15 @@ static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
                SBP2_DMA_FREE("single query logins data");
        }
 
+       if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
+               hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
+                                         scsi_id->status_fifo_addr);
+
        scsi_id->ud->device.driver_data = NULL;
 
+       if (hi)
+               module_put(hi->host->driver->owner);
+
        SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id);
 
        kfree(scsi_id);
@@ -1098,53 +1208,40 @@ static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
        int max_logins;
        int active_logins;
 
-       SBP2_DEBUG("sbp2_query_logins");
+       SBP2_DEBUG_ENTER();
 
        scsi_id->query_logins_orb->reserved1 = 0x0;
        scsi_id->query_logins_orb->reserved2 = 0x0;
 
        scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
        scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
-       SBP2_DEBUG("sbp2_query_logins: query_response_hi/lo initialized");
 
        scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
        scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
        scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
-       SBP2_DEBUG("sbp2_query_logins: lun_misc initialized");
 
        scsi_id->query_logins_orb->reserved_resp_length =
                ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
-       SBP2_DEBUG("sbp2_query_logins: reserved_resp_length initialized");
 
-       scsi_id->query_logins_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
-                                                   SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->ud->id);
-       scsi_id->query_logins_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
-                                                    SBP2_STATUS_FIFO_ADDRESS_HI);
-       SBP2_DEBUG("sbp2_query_logins: status FIFO initialized");
+       scsi_id->query_logins_orb->status_fifo_hi =
+               ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
+       scsi_id->query_logins_orb->status_fifo_lo =
+               ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
 
        sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
 
-       SBP2_DEBUG("sbp2_query_logins: orb byte-swapped");
-
        sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
                             "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
 
        memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
-       memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
-
-       SBP2_DEBUG("sbp2_query_logins: query_logins_response/status FIFO memset");
 
        data[0] = ORB_SET_NODE_ID(hi->host->node_id);
        data[1] = scsi_id->query_logins_orb_dma;
        sbp2util_cpu_to_be32_buffer(data, 8);
 
-       atomic_set(&scsi_id->sbp2_login_complete, 0);
-
-       SBP2_DEBUG("sbp2_query_logins: prepared to write");
        hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
-       SBP2_DEBUG("sbp2_query_logins: written");
 
-       if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 2*HZ)) {
+       if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
                SBP2_INFO("Error querying logins to SBP-2 device - timed out");
                return -EIO;
        }
@@ -1154,11 +1251,8 @@ static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
                return -EIO;
        }
 
-       if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
-           STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
-           STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
-
-               SBP2_INFO("Error querying logins to SBP-2 device - timed out");
+       if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
+               SBP2_INFO("Error querying logins to SBP-2 device - failed");
                return -EIO;
        }
 
@@ -1167,13 +1261,11 @@ static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
        SBP2_DEBUG("length_max_logins = %x",
                   (unsigned int)scsi_id->query_logins_response->length_max_logins);
 
-       SBP2_DEBUG("Query logins to SBP-2 device successful");
-
        max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
-       SBP2_DEBUG("Maximum concurrent logins supported: %d", max_logins);
+       SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
 
        active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
-       SBP2_DEBUG("Number of active logins: %d", active_logins);
+       SBP2_INFO("Number of active logins: %d", active_logins);
 
        if (active_logins >= max_logins) {
                return -EIO;
@@ -1191,10 +1283,10 @@ static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
        struct sbp2scsi_host_info *hi = scsi_id->hi;
        quadlet_t data[2];
 
-       SBP2_DEBUG("sbp2_login_device");
+       SBP2_DEBUG_ENTER();
 
        if (!scsi_id->login_orb) {
-               SBP2_DEBUG("sbp2_login_device: login_orb not alloc'd!");
+               SBP2_DEBUG("%s: login_orb not alloc'd!", __FUNCTION__);
                return -EIO;
        }
 
@@ -1208,66 +1300,42 @@ static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
        /* Set-up login ORB, assume no password */
        scsi_id->login_orb->password_hi = 0;
        scsi_id->login_orb->password_lo = 0;
-       SBP2_DEBUG("sbp2_login_device: password_hi/lo initialized");
 
        scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
        scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
-       SBP2_DEBUG("sbp2_login_device: login_response_hi/lo initialized");
 
        scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
        scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0);   /* One second reconnect time */
        scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login);     /* Exclusive access to device */
        scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1);      /* Notify us of login complete */
        scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
-       SBP2_DEBUG("sbp2_login_device: lun_misc initialized");
 
        scsi_id->login_orb->passwd_resp_lengths =
                ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
-       SBP2_DEBUG("sbp2_login_device: passwd_resp_lengths initialized");
 
-       scsi_id->login_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
-                                            SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->ud->id);
-       scsi_id->login_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
-                                             SBP2_STATUS_FIFO_ADDRESS_HI);
-       SBP2_DEBUG("sbp2_login_device: status FIFO initialized");
+       scsi_id->login_orb->status_fifo_hi =
+               ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
+       scsi_id->login_orb->status_fifo_lo =
+               ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
 
-       /*
-        * Byte swap ORB if necessary
-        */
        sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
 
-       SBP2_DEBUG("sbp2_login_device: orb byte-swapped");
-
        sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
                             "sbp2 login orb", scsi_id->login_orb_dma);
 
-       /*
-        * Initialize login response and status fifo
-        */
        memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
-       memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
-
-       SBP2_DEBUG("sbp2_login_device: login_response/status FIFO memset");
 
-       /*
-        * Ok, let's write to the target's management agent register
-        */
        data[0] = ORB_SET_NODE_ID(hi->host->node_id);
        data[1] = scsi_id->login_orb_dma;
        sbp2util_cpu_to_be32_buffer(data, 8);
 
-       atomic_set(&scsi_id->sbp2_login_complete, 0);
-
-       SBP2_DEBUG("sbp2_login_device: prepared to write to %08x",
-                  (unsigned int)scsi_id->sbp2_management_agent_addr);
        hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
-       SBP2_DEBUG("sbp2_login_device: written");
 
        /*
         * Wait for login status (up to 20 seconds)...
         */
-       if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 20*HZ)) {
-               SBP2_ERR("Error logging into SBP-2 device - login timed-out");
+       if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
+               SBP2_ERR("Error logging into SBP-2 device - timed out");
                return -EIO;
        }
 
@@ -1275,18 +1343,12 @@ static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
         * Sanity. Make sure status returned matches login orb.
         */
        if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
-               SBP2_ERR("Error logging into SBP-2 device - login timed-out");
+               SBP2_ERR("Error logging into SBP-2 device - timed out");
                return -EIO;
        }
 
-       /*
-        * Check status
-        */
-       if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
-           STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
-           STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
-
-               SBP2_ERR("Error logging into SBP-2 device - login failed");
+       if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
+               SBP2_ERR("Error logging into SBP-2 device - failed");
                return -EIO;
        }
 
@@ -1310,9 +1372,7 @@ static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
        scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
 
        SBP2_INFO("Logged into SBP-2 device");
-
        return 0;
-
 }
 
 /*
@@ -1325,7 +1385,7 @@ static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
        quadlet_t data[2];
        int error;
 
-       SBP2_DEBUG("sbp2_logout_device");
+       SBP2_DEBUG_ENTER();
 
        /*
         * Set-up logout ORB
@@ -1342,10 +1402,10 @@ static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
        scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
 
        scsi_id->logout_orb->reserved5 = 0x0;
-       scsi_id->logout_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
-                                             SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->ud->id);
-       scsi_id->logout_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
-                                              SBP2_STATUS_FIFO_ADDRESS_HI);
+       scsi_id->logout_orb->status_fifo_hi =
+               ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
+       scsi_id->logout_orb->status_fifo_lo =
+               ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
 
        /*
         * Byte swap ORB if necessary
@@ -1362,21 +1422,17 @@ static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
        data[1] = scsi_id->logout_orb_dma;
        sbp2util_cpu_to_be32_buffer(data, 8);
 
-       atomic_set(&scsi_id->sbp2_login_complete, 0);
-
        error = hpsb_node_write(scsi_id->ne,
                                scsi_id->sbp2_management_agent_addr, data, 8);
        if (error)
                return error;
 
        /* Wait for device to logout...1 second. */
-       if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ))
+       if (sbp2util_access_timeout(scsi_id, HZ))
                return -EIO;
 
        SBP2_INFO("Logged out of SBP-2 device");
-
        return 0;
-
 }
 
 /*
@@ -1389,7 +1445,7 @@ static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
        quadlet_t data[2];
        int error;
 
-       SBP2_DEBUG("sbp2_reconnect_device");
+       SBP2_DEBUG_ENTER();
 
        /*
         * Set-up reconnect ORB
@@ -1407,10 +1463,10 @@ static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
        scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
 
        scsi_id->reconnect_orb->reserved5 = 0x0;
-       scsi_id->reconnect_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
-                                                SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->ud->id);
-       scsi_id->reconnect_orb->status_FIFO_hi =
-               (ORB_SET_NODE_ID(hi->host->node_id) | SBP2_STATUS_FIFO_ADDRESS_HI);
+       scsi_id->reconnect_orb->status_fifo_hi =
+               ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
+       scsi_id->reconnect_orb->status_fifo_lo =
+               ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
 
        /*
         * Byte swap ORB if necessary
@@ -1420,20 +1476,10 @@ static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
        sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
                             "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
 
-       /*
-        * Initialize status fifo
-        */
-       memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
-
-       /*
-        * Ok, let's write to the target's management agent register
-        */
        data[0] = ORB_SET_NODE_ID(hi->host->node_id);
        data[1] = scsi_id->reconnect_orb_dma;
        sbp2util_cpu_to_be32_buffer(data, 8);
 
-       atomic_set(&scsi_id->sbp2_login_complete, 0);
-
        error = hpsb_node_write(scsi_id->ne,
                                scsi_id->sbp2_management_agent_addr, data, 8);
        if (error)
@@ -1442,8 +1488,8 @@ static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
        /*
         * Wait for reconnect status (up to 1 second)...
         */
-       if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) {
-               SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
+       if (sbp2util_access_timeout(scsi_id, HZ)) {
+               SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
                return -EIO;
        }
 
@@ -1451,25 +1497,17 @@ static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
         * Sanity. Make sure status returned matches reconnect orb.
         */
        if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
-               SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
+               SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
                return -EIO;
        }
 
-       /*
-        * Check status
-        */
-       if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
-           STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
-           STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
-
-               SBP2_ERR("Error reconnecting to SBP-2 device - reconnect failed");
+       if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
+               SBP2_ERR("Error reconnecting to SBP-2 device - failed");
                return -EIO;
        }
 
        HPSB_DEBUG("Reconnected to SBP-2 device");
-
        return 0;
-
 }
 
 /*
@@ -1480,17 +1518,11 @@ static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
 {
        quadlet_t data;
 
-       SBP2_DEBUG("sbp2_set_busy_timeout");
+       SBP2_DEBUG_ENTER();
 
-       /*
-        * Ok, let's write to the target's busy timeout register
-        */
        data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
-
-       if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4)) {
-               SBP2_ERR("sbp2_set_busy_timeout error");
-       }
-
+       if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
+               SBP2_ERR("%s error", __FUNCTION__);
        return 0;
 }
 
@@ -1506,10 +1538,11 @@ static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
        struct csr1212_dentry *dentry;
        u64 management_agent_addr;
        u32 command_set_spec_id, command_set, unit_characteristics,
-           firmware_revision, workarounds;
+           firmware_revision;
+       unsigned workarounds;
        int i;
 
-       SBP2_DEBUG("sbp2_parse_unit_directory");
+       SBP2_DEBUG_ENTER();
 
        management_agent_addr = 0x0;
        command_set_spec_id = 0x0;
@@ -1562,12 +1595,8 @@ static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
                case SBP2_FIRMWARE_REVISION_KEY:
                        /* Firmware revision */
                        firmware_revision = kv->value.immediate;
-                       if (force_inquiry_hack)
-                               SBP2_INFO("sbp2_firmware_revision = %x",
-                                         (unsigned int)firmware_revision);
-                       else
-                               SBP2_DEBUG("sbp2_firmware_revision = %x",
-                                          (unsigned int)firmware_revision);
+                       SBP2_DEBUG("sbp2_firmware_revision = %x",
+                                  (unsigned int)firmware_revision);
                        break;
 
                default:
@@ -1575,41 +1604,39 @@ static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
                }
        }
 
-       /* This is the start of our broken device checking. We try to hack
-        * around oddities and known defects.  */
-       workarounds = 0x0;
+       workarounds = sbp2_default_workarounds;
 
-       /* If the vendor id is 0xa0b8 (Symbios vendor id), then we have a
-        * bridge with 128KB max transfer size limitation. For sanity, we
-        * only voice this when the current max_sectors setting
-        * exceeds the 128k limit. By default, that is not the case.
-        *
-        * It would be really nice if we could detect this before the scsi
-        * host gets initialized. That way we can down-force the
-        * max_sectors to account for it. That is not currently
-        * possible.  */
-       if ((firmware_revision & 0xffff00) ==
-                       SBP2_128KB_BROKEN_FIRMWARE &&
-                       (max_sectors * 512) > (128*1024)) {
-               SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB max transfer size.",
-                               NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid));
-               SBP2_WARN("WARNING: Current max_sectors setting is larger than 128KB (%d sectors)!",
-                               max_sectors);
-               workarounds |= SBP2_BREAKAGE_128K_MAX_TRANSFER;
-       }
-
-       /* Check for a blacklisted set of devices that require us to force
-        * a 36 byte host inquiry. This can be overriden as a module param
-        * (to force all hosts).  */
-       for (i = 0; i < NUM_BROKEN_INQUIRY_DEVS; i++) {
-               if ((firmware_revision & 0xffff00) ==
-                               sbp2_broken_inquiry_list[i]) {
-                       SBP2_WARN("Node " NODE_BUS_FMT ": Using 36byte inquiry workaround",
-                                       NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid));
-                       workarounds |= SBP2_BREAKAGE_INQUIRY_HACK;
-                       break; /* No need to continue. */
+       if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
+               for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
+                       if (sbp2_workarounds_table[i].firmware_revision &&
+                           sbp2_workarounds_table[i].firmware_revision !=
+                           (firmware_revision & 0xffff00))
+                               continue;
+                       if (sbp2_workarounds_table[i].model_id &&
+                           sbp2_workarounds_table[i].model_id != ud->model_id)
+                               continue;
+                       workarounds |= sbp2_workarounds_table[i].workarounds;
+                       break;
                }
-       }
+
+       if (workarounds)
+               SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
+                         "(firmware_revision 0x%06x, vendor_id 0x%06x,"
+                         " model_id 0x%06x)",
+                         NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
+                         workarounds, firmware_revision,
+                         ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
+                         ud->model_id);
+
+       /* We would need one SCSI host template for each target to adjust
+        * max_sectors on the fly, therefore warn only. */
+       if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
+           (max_sectors * 512) > (128 * 1024))
+               SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
+                         "max transfer size. WARNING: Current max_sectors "
+                         "setting is larger than 128KB (%d sectors)",
+                         NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
+                         max_sectors);
 
        /* If this is a logical unit directory entry, process the parent
         * to get the values. */
@@ -1629,6 +1656,8 @@ static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
        }
 }
 
+#define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
+
 /*
  * This function is called in order to determine the max speed and packet
  * size we can use in our ORBs. Note, that we (the driver and host) only
@@ -1641,13 +1670,12 @@ static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
 static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
 {
        struct sbp2scsi_host_info *hi = scsi_id->hi;
+       u8 payload;
 
-       SBP2_DEBUG("sbp2_max_speed_and_size");
+       SBP2_DEBUG_ENTER();
 
-       /* Initial setting comes from the hosts speed map */
        scsi_id->speed_code =
-           hi->host->speed_map[NODEID_TO_NODE(hi->host->node_id) * 64 +
-                               NODEID_TO_NODE(scsi_id->ne->nodeid)];
+           hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
 
        /* Bump down our speed if the user requested it */
        if (scsi_id->speed_code > max_speed) {
@@ -1658,15 +1686,22 @@ static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
 
        /* Payload size is the lesser of what our speed supports and what
         * our host supports.  */
-       scsi_id->max_payload_size =
-           min(sbp2_speedto_max_payload[scsi_id->speed_code],
-               (u8) (hi->host->csr.max_rec - 1));
+       payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
+                     (u8) (hi->host->csr.max_rec - 1));
+
+       /* If physical DMA is off, work around limitation in ohci1394:
+        * packet size must not exceed PAGE_SIZE */
+       if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
+               while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
+                      payload)
+                       payload--;
 
        HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
                   NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
                   hpsb_speedto_str[scsi_id->speed_code],
-                  1 << ((u32) scsi_id->max_payload_size + 2));
+                  SBP2_PAYLOAD_TO_BYTES(payload));
 
+       scsi_id->max_payload_size = payload;
        return 0;
 }
 
@@ -1678,12 +1713,14 @@ static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
        quadlet_t data;
        u64 addr;
        int retval;
+       unsigned long flags;
 
-       SBP2_DEBUG("sbp2_agent_reset");
+       SBP2_DEBUG_ENTER();
+
+       cancel_delayed_work(&scsi_id->protocol_work);
+       if (wait)
+               flush_scheduled_work();
 
-       /*
-        * Ok, let's write to the target's management agent register
-        */
        data = ntohl(SBP2_AGENT_RESET_DATA);
        addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
 
@@ -1700,31 +1737,191 @@ static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
        /*
         * Need to make sure orb pointer is written on next command
         */
+       spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
        scsi_id->last_orb = NULL;
+       spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
 
        return 0;
 }
 
+static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
+                                    struct sbp2scsi_host_info *hi,
+                                    struct sbp2_command_info *command,
+                                    unsigned int scsi_use_sg,
+                                    struct scatterlist *sgpnt,
+                                    u32 orb_direction,
+                                    enum dma_data_direction dma_dir)
+{
+       command->dma_dir = dma_dir;
+       orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
+       orb->misc |= ORB_SET_DIRECTION(orb_direction);
+
+       /* Special case if only one element (and less than 64KB in size) */
+       if ((scsi_use_sg == 1) &&
+           (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
+
+               SBP2_DEBUG("Only one s/g element");
+               command->dma_size = sgpnt[0].length;
+               command->dma_type = CMD_DMA_PAGE;
+               command->cmd_dma = pci_map_page(hi->host->pdev,
+                                               sgpnt[0].page,
+                                               sgpnt[0].offset,
+                                               command->dma_size,
+                                               command->dma_dir);
+               SBP2_DMA_ALLOC("single page scatter element");
+
+               orb->data_descriptor_lo = command->cmd_dma;
+               orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
+
+       } else {
+               struct sbp2_unrestricted_page_table *sg_element =
+                                       &command->scatter_gather_element[0];
+               u32 sg_count, sg_len;
+               dma_addr_t sg_addr;
+               int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
+                                         dma_dir);
+
+               SBP2_DMA_ALLOC("scatter list");
+
+               command->dma_size = scsi_use_sg;
+               command->sge_buffer = sgpnt;
+
+               /* use page tables (s/g) */
+               orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
+               orb->data_descriptor_lo = command->sge_dma;
+
+               /*
+                * Loop through and fill out our sbp-2 page tables
+                * (and split up anything too large)
+                */
+               for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
+                       sg_len = sg_dma_len(sgpnt);
+                       sg_addr = sg_dma_address(sgpnt);
+                       while (sg_len) {
+                               sg_element[sg_count].segment_base_lo = sg_addr;
+                               if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
+                                       sg_element[sg_count].length_segment_base_hi =
+                                               PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
+                                       sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
+                                       sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
+                               } else {
+                                       sg_element[sg_count].length_segment_base_hi =
+                                               PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
+                                       sg_len = 0;
+                               }
+                               sg_count++;
+                       }
+               }
+
+               /* Number of page table (s/g) elements */
+               orb->misc |= ORB_SET_DATA_SIZE(sg_count);
+
+               sbp2util_packet_dump(sg_element,
+                                    (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
+                                    "sbp2 s/g list", command->sge_dma);
+
+               /* Byte swap page tables if necessary */
+               sbp2util_cpu_to_be32_buffer(sg_element,
+                                           (sizeof(struct sbp2_unrestricted_page_table)) *
+                                           sg_count);
+       }
+}
+
+static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
+                                       struct sbp2scsi_host_info *hi,
+                                       struct sbp2_command_info *command,
+                                       struct scatterlist *sgpnt,
+                                       u32 orb_direction,
+                                       unsigned int scsi_request_bufflen,
+                                       void *scsi_request_buffer,
+                                       enum dma_data_direction dma_dir)
+{
+       command->dma_dir = dma_dir;
+       command->dma_size = scsi_request_bufflen;
+       command->dma_type = CMD_DMA_SINGLE;
+       command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
+                                         command->dma_size, command->dma_dir);
+       orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
+       orb->misc |= ORB_SET_DIRECTION(orb_direction);
+
+       SBP2_DMA_ALLOC("single bulk");
+
+       /*
+        * Handle case where we get a command w/o s/g enabled (but
+        * check for transfers larger than 64K)
+        */
+       if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
+
+               orb->data_descriptor_lo = command->cmd_dma;
+               orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
+
+       } else {
+               struct sbp2_unrestricted_page_table *sg_element =
+                       &command->scatter_gather_element[0];
+               u32 sg_count, sg_len;
+               dma_addr_t sg_addr;
+
+               /*
+                * Need to turn this into page tables, since the
+                * buffer is too large.
+                */
+               orb->data_descriptor_lo = command->sge_dma;
+
+               /* Use page tables (s/g) */
+               orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
+
+               /*
+                * fill out our sbp-2 page tables (and split up
+                * the large buffer)
+                */
+               sg_count = 0;
+               sg_len = scsi_request_bufflen;
+               sg_addr = command->cmd_dma;
+               while (sg_len) {
+                       sg_element[sg_count].segment_base_lo = sg_addr;
+                       if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
+                               sg_element[sg_count].length_segment_base_hi =
+                                       PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
+                               sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
+                               sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
+                       } else {
+                               sg_element[sg_count].length_segment_base_hi =
+                                       PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
+                               sg_len = 0;
+                       }
+                       sg_count++;
+               }
+
+               /* Number of page table (s/g) elements */
+               orb->misc |= ORB_SET_DATA_SIZE(sg_count);
+
+               sbp2util_packet_dump(sg_element,
+                                    (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
+                                    "sbp2 s/g list", command->sge_dma);
+
+               /* Byte swap page tables if necessary */
+               sbp2util_cpu_to_be32_buffer(sg_element,
+                                           (sizeof(struct sbp2_unrestricted_page_table)) *
+                                            sg_count);
+       }
+}
+
 /*
  * This function is called to create the actual command orb and s/g list
  * out of the scsi command itself.
  */
-static int sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
-                                  struct sbp2_command_info *command,
-                                  unchar *scsi_cmd,
-                                  unsigned int scsi_use_sg,
-                                  unsigned int scsi_request_bufflen,
-                                  void *scsi_request_buffer,
-                                  enum dma_data_direction dma_dir)
+static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
+                                   struct sbp2_command_info *command,
+                                   unchar *scsi_cmd,
+                                   unsigned int scsi_use_sg,
+                                   unsigned int scsi_request_bufflen,
+                                   void *scsi_request_buffer,
+                                   enum dma_data_direction dma_dir)
 {
        struct sbp2scsi_host_info *hi = scsi_id->hi;
        struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
        struct sbp2_command_orb *command_orb = &command->command_orb;
-       struct sbp2_unrestricted_page_table *scatter_gather_element =
-               &command->scatter_gather_element[0];
-       u32 sg_count, sg_len, orb_direction;
-       dma_addr_t sg_addr;
-       int i;
+       u32 orb_direction;
 
        /*
         * Set-up our command ORB..
@@ -1751,198 +1948,45 @@ static int sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
                orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
        }
 
-       /*
-        * Set-up our pagetable stuff... unfortunately, this has become
-        * messier than I'd like. Need to clean this up a bit.   ;-)
-        */
+       /* Set-up our pagetable stuff */
        if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
-
                SBP2_DEBUG("No data transfer");
-
-               /*
-                * Handle no data transfer
-                */
                command_orb->data_descriptor_hi = 0x0;
                command_orb->data_descriptor_lo = 0x0;
                command_orb->misc |= ORB_SET_DIRECTION(1);
-
        } else if (scsi_use_sg) {
-
                SBP2_DEBUG("Use scatter/gather");
-
-               /*
-                * Special case if only one element (and less than 64KB in size)
-                */
-               if ((scsi_use_sg == 1) && (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
-
-                       SBP2_DEBUG("Only one s/g element");
-                       command->dma_dir = dma_dir;
-                       command->dma_size = sgpnt[0].length;
-                       command->dma_type = CMD_DMA_PAGE;
-                       command->cmd_dma = pci_map_page(hi->host->pdev,
-                                                       sgpnt[0].page,
-                                                       sgpnt[0].offset,
-                                                       command->dma_size,
-                                                       command->dma_dir);
-                       SBP2_DMA_ALLOC("single page scatter element");
-
-                       command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
-                       command_orb->data_descriptor_lo = command->cmd_dma;
-                       command_orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
-                       command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
-
-               } else {
-                       int count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg, dma_dir);
-                       SBP2_DMA_ALLOC("scatter list");
-
-                       command->dma_size = scsi_use_sg;
-                       command->dma_dir = dma_dir;
-                       command->sge_buffer = sgpnt;
-
-                       /* use page tables (s/g) */
-                       command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
-                       command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
-                       command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
-                       command_orb->data_descriptor_lo = command->sge_dma;
-
-                       /*
-                        * Loop through and fill out our sbp-2 page tables
-                        * (and split up anything too large)
-                        */
-                       for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
-                               sg_len = sg_dma_len(sgpnt);
-                               sg_addr = sg_dma_address(sgpnt);
-                               while (sg_len) {
-                                       scatter_gather_element[sg_count].segment_base_lo = sg_addr;
-                                       if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
-                                               scatter_gather_element[sg_count].length_segment_base_hi =
-                                                       PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
-                                               sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
-                                               sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
-                                       } else {
-                                               scatter_gather_element[sg_count].length_segment_base_hi =
-                                                       PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
-                                               sg_len = 0;
-                                       }
-                                       sg_count++;
-                               }
-                       }
-
-                       /* Number of page table (s/g) elements */
-                       command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
-
-                       sbp2util_packet_dump(scatter_gather_element,
-                                            (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
-                                            "sbp2 s/g list", command->sge_dma);
-
-                       /*
-                        * Byte swap page tables if necessary
-                        */
-                       sbp2util_cpu_to_be32_buffer(scatter_gather_element,
-                                                   (sizeof(struct sbp2_unrestricted_page_table)) *
-                                                   sg_count);
-
-               }
-
+               sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
+                                        sgpnt, orb_direction, dma_dir);
        } else {
-
                SBP2_DEBUG("No scatter/gather");
-
-               command->dma_dir = dma_dir;
-               command->dma_size = scsi_request_bufflen;
-               command->dma_type = CMD_DMA_SINGLE;
-               command->cmd_dma =
-                   pci_map_single(hi->host->pdev, scsi_request_buffer,
-                                  command->dma_size, command->dma_dir);
-               SBP2_DMA_ALLOC("single bulk");
-
-               /*
-                * Handle case where we get a command w/o s/g enabled (but
-                * check for transfers larger than 64K)
-                */
-               if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
-
-                       command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
-                       command_orb->data_descriptor_lo = command->cmd_dma;
-                       command_orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
-                       command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
-
-               } else {
-                       /*
-                        * Need to turn this into page tables, since the
-                        * buffer is too large.
-                        */
-                       command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
-                       command_orb->data_descriptor_lo = command->sge_dma;
-
-                       /* Use page tables (s/g) */
-                       command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
-                       command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
-
-                       /*
-                        * fill out our sbp-2 page tables (and split up
-                        * the large buffer)
-                        */
-                       sg_count = 0;
-                       sg_len = scsi_request_bufflen;
-                       sg_addr = command->cmd_dma;
-                       while (sg_len) {
-                               scatter_gather_element[sg_count].segment_base_lo = sg_addr;
-                               if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
-                                       scatter_gather_element[sg_count].length_segment_base_hi =
-                                               PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
-                                       sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
-                                       sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
-                               } else {
-                                       scatter_gather_element[sg_count].length_segment_base_hi =
-                                               PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
-                                       sg_len = 0;
-                               }
-                               sg_count++;
-                       }
-
-                       /* Number of page table (s/g) elements */
-                       command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
-
-                       sbp2util_packet_dump(scatter_gather_element,
-                                            (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
-                                            "sbp2 s/g list", command->sge_dma);
-
-                       /*
-                        * Byte swap page tables if necessary
-                        */
-                       sbp2util_cpu_to_be32_buffer(scatter_gather_element,
-                                                   (sizeof(struct sbp2_unrestricted_page_table)) *
-                                                    sg_count);
-
-               }
-
+               sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
+                                           orb_direction, scsi_request_bufflen,
+                                           scsi_request_buffer, dma_dir);
        }
 
-       /*
-        * Byte swap command ORB if necessary
-        */
+       /* Byte swap command ORB if necessary */
        sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
 
-       /*
-        * Put our scsi command in the command ORB
-        */
+       /* Put our scsi command in the command ORB */
        memset(command_orb->cdb, 0, 12);
        memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
-
-       return 0;
 }
 
 /*
  * This function is called in order to begin a regular SBP-2 command.
  */
-static int sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
+static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
                                 struct sbp2_command_info *command)
 {
        struct sbp2scsi_host_info *hi = scsi_id->hi;
        struct sbp2_command_orb *command_orb = &command->command_orb;
-       struct node_entry *ne = scsi_id->ne;
-       u64 addr;
+       struct sbp2_command_orb *last_orb;
+       dma_addr_t last_orb_dma;
+       u64 addr = scsi_id->sbp2_command_block_agent_addr;
+       quadlet_t data[2];
+       size_t length;
+       unsigned long flags;
 
        outstanding_orb_incr;
        SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
@@ -1950,73 +1994,69 @@ static int sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
 
        pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
                                       sizeof(struct sbp2_command_orb),
-                                      PCI_DMA_BIDIRECTIONAL);
+                                      PCI_DMA_TODEVICE);
        pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
                                       sizeof(command->scatter_gather_element),
                                       PCI_DMA_BIDIRECTIONAL);
        /*
         * Check to see if there are any previous orbs to use
         */
-       if (scsi_id->last_orb == NULL) {
-               quadlet_t data[2];
-
+       spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
+       last_orb = scsi_id->last_orb;
+       last_orb_dma = scsi_id->last_orb_dma;
+       if (!last_orb) {
                /*
-                * Ok, let's write to the target's management agent register
+                * last_orb == NULL means: We know that the target's fetch agent
+                * is not active right now.
                 */
-               addr = scsi_id->sbp2_command_block_agent_addr + SBP2_ORB_POINTER_OFFSET;
+               addr += SBP2_ORB_POINTER_OFFSET;
                data[0] = ORB_SET_NODE_ID(hi->host->node_id);
                data[1] = command->command_orb_dma;
                sbp2util_cpu_to_be32_buffer(data, 8);
-
-               SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb);
-
-               if (sbp2util_node_write_no_wait(ne, addr, data, 8) < 0) {
-                       SBP2_ERR("sbp2util_node_write_no_wait failed.\n");
-                       return -EIO;
-               }
-
-               SBP2_ORB_DEBUG("write command agent complete");
-
-               scsi_id->last_orb = command_orb;
-               scsi_id->last_orb_dma = command->command_orb_dma;
-
+               length = 8;
        } else {
-               quadlet_t data;
-
                /*
-                * We have an orb already sent (maybe or maybe not
-                * processed) that we can append this orb to. So do so,
-                * and ring the doorbell. Have to be very careful
-                * modifying these next orb pointers, as they are accessed
-                * both by the sbp2 device and us.
+                * last_orb != NULL means: We know that the target's fetch agent
+                * is (very probably) not dead or in reset state right now.
+                * We have an ORB already sent that we can append a new one to.
+                * The target's fetch agent may or may not have read this
+                * previous ORB yet.
                 */
-               scsi_id->last_orb->next_ORB_lo =
-                   cpu_to_be32(command->command_orb_dma);
+               pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
+                                           sizeof(struct sbp2_command_orb),
+                                           PCI_DMA_TODEVICE);
+               last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
+               wmb();
                /* Tells hardware that this pointer is valid */
-               scsi_id->last_orb->next_ORB_hi = 0x0;
-               pci_dma_sync_single_for_device(hi->host->pdev,
-                                              scsi_id->last_orb_dma,
+               last_orb->next_ORB_hi = 0;
+               pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
                                               sizeof(struct sbp2_command_orb),
-                                              PCI_DMA_BIDIRECTIONAL);
+                                              PCI_DMA_TODEVICE);
+               addr += SBP2_DOORBELL_OFFSET;
+               data[0] = 0;
+               length = 4;
+       }
+       scsi_id->last_orb = command_orb;
+       scsi_id->last_orb_dma = command->command_orb_dma;
+       spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
 
+       SBP2_ORB_DEBUG("write to %s register, command orb %p",
+                       last_orb ? "DOORBELL" : "ORB_POINTER", command_orb);
+       if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
                /*
-                * Ring the doorbell
+                * sbp2util_node_write_no_wait failed. We certainly ran out
+                * of transaction labels, perhaps just because there were no
+                * context switches which gave khpsbpkt a chance to collect
+                * free tlabels. Try again in non-atomic context. If necessary,
+                * the workqueue job will sleep to guaranteedly get a tlabel.
+                * We do not accept new commands until the job is over.
                 */
-               data = cpu_to_be32(command->command_orb_dma);
-               addr = scsi_id->sbp2_command_block_agent_addr + SBP2_DOORBELL_OFFSET;
-
-               SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb);
-
-               if (sbp2util_node_write_no_wait(ne, addr, &data, 4) < 0) {
-                       SBP2_ERR("sbp2util_node_write_no_wait failed");
-                       return -EIO;
-               }
-
-               scsi_id->last_orb = command_orb;
-               scsi_id->last_orb_dma = command->command_orb_dma;
-
+               scsi_block_requests(scsi_id->scsi_host);
+               PREPARE_DELAYED_WORK(&scsi_id->protocol_work,
+                            last_orb ? sbp2util_write_doorbell:
+                                       sbp2util_write_orb_pointer);
+               schedule_delayed_work(&scsi_id->protocol_work, 0);
        }
-       return 0;
 }
 
 /*
@@ -2030,11 +2070,7 @@ static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
        unsigned int request_bufflen = SCpnt->request_bufflen;
        struct sbp2_command_info *command;
 
-       SBP2_DEBUG("sbp2_send_command");
-#if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
-       printk("[scsi command]\n   ");
-       scsi_print_command(SCpnt);
-#endif
+       SBP2_DEBUG_ENTER();
        SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
        SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
 
@@ -2047,18 +2083,6 @@ static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
        }
 
        /*
-        * The scsi stack sends down a request_bufflen which does not match the
-        * length field in the scsi cdb. This causes some sbp2 devices to
-        * reject this inquiry command. Fix the request_bufflen.
-        */
-       if (*cmd == INQUIRY) {
-               if (force_inquiry_hack || scsi_id->workarounds & SBP2_BREAKAGE_INQUIRY_HACK)
-                       request_bufflen = cmd[4] = 0x24;
-               else
-                       request_bufflen = cmd[4];
-       }
-
-       /*
         * Now actually fill in the comamnd orb and sbp2 s/g list
         */
        sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
@@ -2069,11 +2093,6 @@ static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
                             "sbp2 command orb", command->command_orb_dma);
 
        /*
-        * Initialize status fifo
-        */
-       memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
-
-       /*
         * Link up the orb, and ring the doorbell if needed
         */
        sbp2_link_orb_command(scsi_id, command);
@@ -2086,7 +2105,7 @@ static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
  */
 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
 {
-       SBP2_DEBUG("sbp2_status_to_sense_data");
+       SBP2_DEBUG_ENTER();
 
        /*
         * Ok, it's pretty ugly...   ;-)
@@ -2112,107 +2131,77 @@ static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense
 }
 
 /*
- * This function is called after a command is completed, in order to do any necessary SBP-2
- * response data translations for the SCSI stack
- */
-static void sbp2_check_sbp2_response(struct scsi_id_instance_data *scsi_id,
-                                    struct scsi_cmnd *SCpnt)
-{
-       u8 *scsi_buf = SCpnt->request_buffer;
-
-       SBP2_DEBUG("sbp2_check_sbp2_response");
-
-       switch (SCpnt->cmnd[0]) {
-
-       case INQUIRY:
-               /*
-                * Make sure data length is ok. Minimum length is 36 bytes
-                */
-               if (scsi_buf[4] == 0) {
-                       scsi_buf[4] = 36 - 5;
-               }
-
-               /*
-                * Fix ansi revision and response data format
-                */
-               scsi_buf[2] |= 2;
-               scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
-
-               break;
-
-       default:
-               break;
-       }
-       return;
-}
-
-/*
  * This function deals with status writes from the SBP-2 device
  */
-static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid,
-                                   quadlet_t *data, u64 addr, size_t length, u16 fl)
+static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
+                                   int destid, quadlet_t *data, u64 addr,
+                                   size_t length, u16 fl)
 {
        struct sbp2scsi_host_info *hi;
        struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
-       u32 id;
        struct scsi_cmnd *SCpnt = NULL;
+       struct sbp2_status_block *sb;
        u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
        struct sbp2_command_info *command;
        unsigned long flags;
 
-       SBP2_DEBUG("sbp2_handle_status_write");
+       SBP2_DEBUG_ENTER();
 
        sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
 
-       if (!host) {
+       if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
+               SBP2_ERR("Wrong size of status block");
+               return RCODE_ADDRESS_ERROR;
+       }
+       if (unlikely(!host)) {
                SBP2_ERR("host is NULL - this is bad!");
                return RCODE_ADDRESS_ERROR;
        }
-
        hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
-
-       if (!hi) {
+       if (unlikely(!hi)) {
                SBP2_ERR("host info is NULL - this is bad!");
                return RCODE_ADDRESS_ERROR;
        }
-
        /*
-        * Find our scsi_id structure by looking at the status fifo address written to by
-        * the sbp2 device.
+        * Find our scsi_id structure by looking at the status fifo address
+        * written to by the sbp2 device.
         */
-       id = SBP2_STATUS_FIFO_OFFSET_TO_ENTRY((u32)(addr - SBP2_STATUS_FIFO_ADDRESS));
        list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
-               if (scsi_id_tmp->ne->nodeid == nodeid && scsi_id_tmp->ud->id == id) {
+               if (scsi_id_tmp->ne->nodeid == nodeid &&
+                   scsi_id_tmp->status_fifo_addr == addr) {
                        scsi_id = scsi_id_tmp;
                        break;
                }
        }
-
-       if (!scsi_id) {
+       if (unlikely(!scsi_id)) {
                SBP2_ERR("scsi_id is NULL - device is gone?");
                return RCODE_ADDRESS_ERROR;
        }
 
        /*
-        * Put response into scsi_id status fifo...
-        */
-       memcpy(&scsi_id->status_block, data, length);
-
-       /*
-        * Byte swap first two quadlets (8 bytes) of status for processing
+        * Put response into scsi_id status fifo buffer. The first two bytes
+        * come in big endian bit order. Often the target writes only a
+        * truncated status block, minimally the first two quadlets. The rest
+        * is implied to be zeros.
         */
-       sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8);
+       sb = &scsi_id->status_block;
+       memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
+       memcpy(sb, data, length);
+       sbp2util_be32_to_cpu_buffer(sb, 8);
 
        /*
-        * Handle command ORB status here if necessary. First, need to match status with command.
+        * Ignore unsolicited status. Handle command ORB status.
         */
-       command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo);
+       if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
+               command = NULL;
+       else
+               command = sbp2util_find_command_for_orb(scsi_id,
+                                                       sb->ORB_offset_lo);
        if (command) {
-
                SBP2_DEBUG("Found status for command ORB");
                pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
                                            sizeof(struct sbp2_command_orb),
-                                           PCI_DMA_BIDIRECTIONAL);
+                                           PCI_DMA_TODEVICE);
                pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
                                            sizeof(command->scatter_gather_element),
                                            PCI_DMA_BIDIRECTIONAL);
@@ -2221,67 +2210,77 @@ static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int dest
                outstanding_orb_decr;
 
                /*
-                * Matched status with command, now grab scsi command pointers and check status
+                * Matched status with command, now grab scsi command pointers
+                * and check status.
+                */
+               /*
+                * FIXME: If the src field in the status is 1, the ORB DMA must
+                * not be reused until status for a subsequent ORB is received.
                 */
                SCpnt = command->Current_SCpnt;
+               spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
                sbp2util_mark_command_completed(scsi_id, command);
+               spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
 
                if (SCpnt) {
-
+                       u32 h = sb->ORB_offset_hi_misc;
+                       u32 r = STATUS_GET_RESP(h);
+
+                       if (r != RESP_STATUS_REQUEST_COMPLETE) {
+                               SBP2_WARN("resp 0x%x, sbp_status 0x%x",
+                                         r, STATUS_GET_SBP_STATUS(h));
+                               scsi_status =
+                                       r == RESP_STATUS_TRANSPORT_FAILURE ?
+                                       SBP2_SCSI_STATUS_BUSY :
+                                       SBP2_SCSI_STATUS_COMMAND_TERMINATED;
+                       }
                        /*
-                        * See if the target stored any scsi status information
+                        * See if the target stored any scsi status information.
                         */
-                       if (STATUS_GET_LENGTH(scsi_id->status_block.ORB_offset_hi_misc) > 1) {
-                               /*
-                                * Translate SBP-2 status to SCSI sense data
-                                */
+                       if (STATUS_GET_LEN(h) > 1) {
                                SBP2_DEBUG("CHECK CONDITION");
-                               scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer);
+                               scsi_status = sbp2_status_to_sense_data(
+                                       (unchar *)sb, SCpnt->sense_buffer);
                        }
-
                        /*
-                        * Check to see if the dead bit is set. If so, we'll have to initiate
-                        * a fetch agent reset.
+                        * Check to see if the dead bit is set. If so, we'll
+                        * have to initiate a fetch agent reset.
                         */
-                       if (STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc)) {
-
-                               /*
-                                * Initiate a fetch agent reset.
-                                */
-                               SBP2_DEBUG("Dead bit set - initiating fetch agent reset");
+                       if (STATUS_TEST_DEAD(h)) {
+                               SBP2_DEBUG("Dead bit set - "
+                                          "initiating fetch agent reset");
                                 sbp2_agent_reset(scsi_id, 0);
                        }
-
                        SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
                }
 
                /*
-                * Check here to see if there are no commands in-use. If there are none, we can
-                * null out last orb so that next time around we write directly to the orb pointer...
-                * Quick start saves one 1394 bus transaction.
+                * Check here to see if there are no commands in-use. If there
+                * are none, we know that the fetch agent left the active state
+                * _and_ that we did not reactivate it yet. Therefore clear
+                * last_orb so that next time we write directly to the
+                * ORB_POINTER register. That way the fetch agent does not need
+                * to refetch the next_ORB.
                 */
                spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
-               if (list_empty(&scsi_id->sbp2_command_orb_inuse)) {
+               if (list_empty(&scsi_id->sbp2_command_orb_inuse))
                        scsi_id->last_orb = NULL;
-               }
                spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
 
        } else {
-
                /*
                 * It's probably a login/logout/reconnect status.
                 */
-               if ((scsi_id->login_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
-                   (scsi_id->query_logins_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
-                   (scsi_id->reconnect_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
-                   (scsi_id->logout_orb_dma == scsi_id->status_block.ORB_offset_lo)) {
-                       atomic_set(&scsi_id->sbp2_login_complete, 1);
+               if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
+                   (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
+                   (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
+                   (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
+                       scsi_id->access_complete = 1;
+                       wake_up_interruptible(&access_wq);
                }
        }
 
        if (SCpnt) {
-
-               /* Complete the SCSI command. */
                SBP2_DEBUG("Completing SCSI command");
                sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
                                          command->Current_done);
@@ -2307,7 +2306,10 @@ static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
        struct sbp2scsi_host_info *hi;
        int result = DID_NO_CONNECT << 16;
 
-       SBP2_DEBUG("sbp2scsi_queuecommand");
+       SBP2_DEBUG_ENTER();
+#if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
+       scsi_print_command(SCpnt);
+#endif
 
        if (!sbp2util_node_is_available(scsi_id))
                goto done;
@@ -2385,7 +2387,7 @@ static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id
        struct sbp2_command_info *command;
        unsigned long flags;
 
-       SBP2_DEBUG("sbp2scsi_complete_all_commands");
+       SBP2_DEBUG_ENTER();
 
        spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
        while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
@@ -2394,7 +2396,7 @@ static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id
                command = list_entry(lh, struct sbp2_command_info, list);
                pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
                                            sizeof(struct sbp2_command_orb),
-                                           PCI_DMA_BIDIRECTIONAL);
+                                           PCI_DMA_TODEVICE);
                pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
                                            sizeof(command->scatter_gather_element),
                                            PCI_DMA_BIDIRECTIONAL);
@@ -2418,7 +2420,7 @@ static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
                                      u32 scsi_status, struct scsi_cmnd *SCpnt,
                                      void (*done)(struct scsi_cmnd *))
 {
-       SBP2_DEBUG("sbp2scsi_complete_command");
+       SBP2_DEBUG_ENTER();
 
        /*
         * Sanity
@@ -2444,7 +2446,7 @@ static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
         */
        switch (scsi_status) {
        case SBP2_SCSI_STATUS_GOOD:
-               SCpnt->result = DID_OK;
+               SCpnt->result = DID_OK << 16;
                break;
 
        case SBP2_SCSI_STATUS_BUSY:
@@ -2454,16 +2456,11 @@ static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
 
        case SBP2_SCSI_STATUS_CHECK_CONDITION:
                SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
-               SCpnt->result = CHECK_CONDITION << 1;
-
-               /*
-                * Debug stuff
-                */
+               SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
 #if CONFIG_IEEE1394_SBP2_DEBUG >= 1
                scsi_print_command(SCpnt);
-               scsi_print_sense("bh", SCpnt);
+               scsi_print_sense(SBP2_DEVICE_NAME, SCpnt);
 #endif
-
                break;
 
        case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
@@ -2486,13 +2483,6 @@ static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
        }
 
        /*
-        * Take care of any sbp2 response data mucking here (RBC stuff, etc.)
-        */
-       if (SCpnt->result == DID_OK) {
-               sbp2_check_sbp2_response(scsi_id, SCpnt);
-       }
-
-       /*
         * If a bus reset is in progress and there was an error, complete
         * the command as busy so that it will get retried.
         */
@@ -2506,6 +2496,8 @@ static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
         * If a unit attention occurs, return busy status so it gets
         * retried... it could have happened because of a 1394 bus reset
         * or hot-plug...
+        * XXX  DID_BUS_BUSY is actually a bad idea because it will defy
+        * the scsi layer's retry logic.
         */
 #if 0
        if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
@@ -2523,15 +2515,30 @@ static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
 
 static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
 {
-       ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = sdev;
+       struct scsi_id_instance_data *scsi_id =
+               (struct scsi_id_instance_data *)sdev->host->hostdata[0];
+
+       scsi_id->sdev = sdev;
+       sdev->allow_restart = 1;
+
+       if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
+               sdev->inquiry_len = 36;
        return 0;
 }
 
 static int sbp2scsi_slave_configure(struct scsi_device *sdev)
 {
+       struct scsi_id_instance_data *scsi_id =
+               (struct scsi_id_instance_data *)sdev->host->hostdata[0];
+
        blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
        sdev->use_10_for_rw = 1;
-       sdev->use_10_for_ms = 1;
+
+       if (sdev->type == TYPE_DISK &&
+           scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
+               sdev->skip_ms_page_8 = 1;
+       if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
+               sdev->fix_capacity = 1;
        return 0;
 }
 
@@ -2551,6 +2558,7 @@ static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
                (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
        struct sbp2scsi_host_info *hi = scsi_id->hi;
        struct sbp2_command_info *command;
+       unsigned long flags;
 
        SBP2_ERR("aborting sbp2 command");
        scsi_print_command(SCpnt);
@@ -2561,13 +2569,14 @@ static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
                 * Right now, just return any matching command structures
                 * to the free pool.
                 */
+               spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
                command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
                if (command) {
                        SBP2_DEBUG("Found command to abort");
                        pci_dma_sync_single_for_cpu(hi->host->pdev,
                                                    command->command_orb_dma,
                                                    sizeof(struct sbp2_command_orb),
-                                                   PCI_DMA_BIDIRECTIONAL);
+                                                   PCI_DMA_TODEVICE);
                        pci_dma_sync_single_for_cpu(hi->host->pdev,
                                                    command->sge_dma,
                                                    sizeof(command->scatter_gather_element),
@@ -2578,11 +2587,12 @@ static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
                                command->Current_done(command->Current_SCpnt);
                        }
                }
+               spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
 
                /*
                 * Initiate a fetch agent reset.
                 */
-               sbp2_agent_reset(scsi_id, 0);
+               sbp2_agent_reset(scsi_id, 1);
                sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
        }
 
@@ -2601,7 +2611,7 @@ static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
 
        if (sbp2util_node_is_available(scsi_id)) {
                SBP2_ERR("Generating sbp2 fetch agent reset");
-               sbp2_agent_reset(scsi_id, 0);
+               sbp2_agent_reset(scsi_id, 1);
        }
 
        return SUCCESS;
@@ -2662,7 +2672,7 @@ static int sbp2_module_init(void)
 {
        int ret;
 
-       SBP2_DEBUG("sbp2_module_init");
+       SBP2_DEBUG_ENTER();
 
        /* Module load debug option to force one command at a time (serializing I/O) */
        if (serialize_io) {
@@ -2672,7 +2682,9 @@ static int sbp2_module_init(void)
                scsi_driver_template.cmd_per_lun = 1;
        }
 
-       /* Set max sectors (module load option). Default is 255 sectors. */
+       if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
+           (max_sectors * 512) > (128 * 1024))
+               max_sectors = 128 * 1024 / 512;
        scsi_driver_template.max_sectors = max_sectors;
 
        /* Register our high level driver with 1394 stack */
@@ -2690,7 +2702,7 @@ static int sbp2_module_init(void)
 
 static void __exit sbp2_module_exit(void)
 {
-       SBP2_DEBUG("sbp2_module_exit");
+       SBP2_DEBUG_ENTER();
 
        hpsb_unregister_protocol(&sbp2_driver);