Merge branch 'linus' into cont_syslog
[safe/jmp/linux-2.6] / drivers / scsi / aic7xxx / aic79xx_osm.c
index cf8e0ca..4c41332 100644 (file)
 
 static struct scsi_transport_template *ahd_linux_transport_template = NULL;
 
-/*
- * Include aiclib.c as part of our
- * "module dependencies are hard" work around.
- */
-#include "aiclib.c"
-
 #include <linux/init.h>                /* __setup */
 #include <linux/mm.h>          /* For fetching system memory size */
 #include <linux/blkdev.h>              /* For block_size() */
 #include <linux/delay.h>       /* For ssleep/msleep */
-
-/*
- * Lock protecting manipulation of the ahd softc list.
- */
-spinlock_t ahd_list_spinlock;
+#include <linux/device.h>
+#include <linux/slab.h>
 
 /*
  * Bucket size for counting good commands in between bad ones.
@@ -203,7 +194,7 @@ struct ahd_linux_iocell_opts
 #define AIC79XX_PRECOMP_INDEX  0
 #define AIC79XX_SLEWRATE_INDEX 1
 #define AIC79XX_AMPLITUDE_INDEX        2
-static struct ahd_linux_iocell_opts aic79xx_iocell_info[] =
+static const struct ahd_linux_iocell_opts aic79xx_iocell_info[] =
 {
        AIC79XX_DEFAULT_IOOPTS,
        AIC79XX_DEFAULT_IOOPTS,
@@ -253,25 +244,6 @@ ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
 static uint32_t aic79xx_no_reset;
 
 /*
- * Certain PCI motherboards will scan PCI devices from highest to lowest,
- * others scan from lowest to highest, and they tend to do all kinds of
- * strange things when they come into contact with PCI bridge chips.  The
- * net result of all this is that the PCI card that is actually used to boot
- * the machine is very hard to detect.  Most motherboards go from lowest
- * PCI slot number to highest, and the first SCSI controller found is the
- * one you boot from.  The only exceptions to this are when a controller
- * has its BIOS disabled.  So, we by default sort all of our SCSI controllers
- * from lowest PCI slot number to highest PCI slot number.  We also force
- * all controllers with their BIOS disabled to the end of the list.  This
- * works on *almost* all computers.  Where it doesn't work, we have this
- * option.  Setting this option to non-0 will reverse the order of the sort
- * to highest first, then lowest, but will still leave cards with their BIOS
- * disabled at the very end.  That should fix everyone up unless there are
- * really strange cirumstances.
- */
-static uint32_t aic79xx_reverse_scan;
-
-/*
  * Should we force EXTENDED translation on a controller.
  *     0 == Use whatever is in the SEEPROM or default to off
  *     1 == Use whatever is in the SEEPROM or default to on
@@ -303,13 +275,6 @@ static uint32_t aic79xx_pci_parity = ~0;
 uint32_t aic79xx_allow_memio = ~0;
 
 /*
- * aic79xx_detect() has been run, so register all device arrivals
- * immediately with the system rather than deferring to the sorted
- * attachment performed by aic79xx_detect().
- */
-int aic79xx_detect_complete;
-
-/*
  * So that we can set how long each device is given as a selection timeout.
  * The table of values goes like this:
  *   0 - 256ms
@@ -329,30 +294,44 @@ static uint32_t aic79xx_seltime;
  * force all outstanding transactions to be serviced prior to a new
  * transaction.
  */
-uint32_t aic79xx_periodic_otag;
+static uint32_t aic79xx_periodic_otag;
+
+/* Some storage boxes are using an LSI chip which has a bug making it
+ * impossible to use aic79xx Rev B chip in 320 speeds.  The following
+ * storage boxes have been reported to be buggy:
+ * EonStor 3U 16-Bay: U16U-G3A3
+ * EonStor 2U 12-Bay: U12U-G3A3
+ * SentinelRAID: 2500F R5 / R6
+ * SentinelRAID: 2500F R1
+ * SentinelRAID: 2500F/1500F
+ * SentinelRAID: 150F
+ * 
+ * To get around this LSI bug, you can set your board to 160 mode
+ * or you can enable the SLOWCRC bit.
+ */
+uint32_t aic79xx_slowcrc;
 
 /*
  * Module information and settable options.
  */
 static char *aic79xx = NULL;
 
-MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
-MODULE_DESCRIPTION("Adaptec Aic790X U320 SCSI Host Bus Adapter driver");
+MODULE_AUTHOR("Maintainer: Hannes Reinecke <hare@suse.de>");
+MODULE_DESCRIPTION("Adaptec AIC790X U320 SCSI Host Bus Adapter driver");
 MODULE_LICENSE("Dual BSD/GPL");
 MODULE_VERSION(AIC79XX_DRIVER_VERSION);
 module_param(aic79xx, charp, 0444);
 MODULE_PARM_DESC(aic79xx,
-"period delimited, options string.\n"
+"period-delimited options string:\n"
 "      verbose                 Enable verbose/diagnostic logging\n"
 "      allow_memio             Allow device registers to be memory mapped\n"
 "      debug                   Bitmask of debug values to enable\n"
-"      no_reset                Supress initial bus resets\n"
+"      no_reset                Suppress initial bus resets\n"
 "      extended                Enable extended geometry on all controllers\n"
 "      periodic_otag           Send an ordered tagged transaction\n"
 "                              periodically to prevent tag starvation.\n"
 "                              This may be required by some older disk\n"
 "                              or drives/RAID arrays.\n"
-"      reverse_scan            Sort PCI devices highest Bus/Slot to lowest\n"
 "      tag_info:<tag_str>      Set per-target tag depth\n"
 "      global_tag_depth:<int>  Global tag depth for all targets on all buses\n"
 "      slewrate:<slewrate_list>Set the signal slew rate (0-15).\n"
@@ -360,6 +339,7 @@ MODULE_PARM_DESC(aic79xx,
 "      amplitude:<int>         Set the signal amplitude (0-7).\n"
 "      seltime:<int>           Selection Timeout:\n"
 "                              (0/256ms,1/128ms,2/64ms,3/32ms)\n"
+"      slowcrc                 Turn on the SLOWCRC bit (Rev B only)\n"          
 "\n"
 "      Sample /etc/modprobe.conf line:\n"
 "              Enable verbose logging\n"
@@ -367,15 +347,14 @@ MODULE_PARM_DESC(aic79xx,
 "              Shorten the selection timeout to 128ms\n"
 "\n"
 "      options aic79xx 'aic79xx=verbose.tag_info:{{}.{}.{..10}}.seltime:1'\n"
-"\n");
+);
 
 static void ahd_linux_handle_scsi_status(struct ahd_softc *,
                                         struct scsi_device *,
                                         struct scb *);
 static void ahd_linux_queue_cmd_complete(struct ahd_softc *ahd,
                                         struct scsi_cmnd *cmd);
-static void ahd_linux_sem_timeout(u_long arg);
-static int  ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
+static int ahd_linux_queue_abort_cmd(struct scsi_cmnd *cmd);
 static void ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd);
 static u_int ahd_linux_user_tagdepth(struct ahd_softc *ahd,
                                     struct ahd_devinfo *devinfo);
@@ -384,83 +363,187 @@ static int ahd_linux_run_command(struct ahd_softc*,
                                 struct ahd_linux_device *,
                                 struct scsi_cmnd *);
 static void ahd_linux_setup_tag_info_global(char *p);
-static aic_option_callback_t ahd_linux_setup_tag_info;
-static aic_option_callback_t ahd_linux_setup_iocell_info;
 static int  aic79xx_setup(char *c);
-static int  ahd_linux_next_unit(void);
+static void ahd_freeze_simq(struct ahd_softc *ahd);
+static void ahd_release_simq(struct ahd_softc *ahd);
 
-/****************************** Inlines ***************************************/
-static __inline void ahd_linux_unmap_scb(struct ahd_softc*, struct scb*);
+static int ahd_linux_unit;
 
-static __inline void
-ahd_linux_unmap_scb(struct ahd_softc *ahd, struct scb *scb)
+
+/************************** OS Utility Wrappers *******************************/
+void ahd_delay(long);
+void
+ahd_delay(long usec)
 {
-       struct scsi_cmnd *cmd;
-       int direction;
+       /*
+        * udelay on Linux can have problems for
+        * multi-millisecond waits.  Wait at most
+        * 1024us per call.
+        */
+       while (usec > 0) {
+               udelay(usec % 1024);
+               usec -= 1024;
+       }
+}
 
-       cmd = scb->io_ctx;
-       direction = cmd->sc_data_direction;
-       ahd_sync_sglist(ahd, scb, BUS_DMASYNC_POSTWRITE);
-       if (cmd->use_sg != 0) {
-               struct scatterlist *sg;
 
-               sg = (struct scatterlist *)cmd->request_buffer;
-               pci_unmap_sg(ahd->dev_softc, sg, cmd->use_sg, direction);
-       } else if (cmd->request_bufflen != 0) {
-               pci_unmap_single(ahd->dev_softc,
-                                scb->platform_data->buf_busaddr,
-                                cmd->request_bufflen, direction);
+/***************************** Low Level I/O **********************************/
+uint8_t ahd_inb(struct ahd_softc * ahd, long port);
+void ahd_outb(struct ahd_softc * ahd, long port, uint8_t val);
+void ahd_outw_atomic(struct ahd_softc * ahd,
+                                    long port, uint16_t val);
+void ahd_outsb(struct ahd_softc * ahd, long port,
+                              uint8_t *, int count);
+void ahd_insb(struct ahd_softc * ahd, long port,
+                              uint8_t *, int count);
+
+uint8_t
+ahd_inb(struct ahd_softc * ahd, long port)
+{
+       uint8_t x;
+
+       if (ahd->tags[0] == BUS_SPACE_MEMIO) {
+               x = readb(ahd->bshs[0].maddr + port);
+       } else {
+               x = inb(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
        }
+       mb();
+       return (x);
 }
 
-/******************************** Macros **************************************/
-#define BUILD_SCSIID(ahd, cmd)                                         \
-       ((((cmd)->device->id << TID_SHIFT) & TID) | (ahd)->our_id)
+#if 0 /* unused */
+static uint16_t
+ahd_inw_atomic(struct ahd_softc * ahd, long port)
+{
+       uint8_t x;
 
-/*
- * Try to detect an Adaptec 79XX controller.
- */
-static int
-ahd_linux_detect(struct scsi_host_template *template)
+       if (ahd->tags[0] == BUS_SPACE_MEMIO) {
+               x = readw(ahd->bshs[0].maddr + port);
+       } else {
+               x = inw(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
+       }
+       mb();
+       return (x);
+}
+#endif
+
+void
+ahd_outb(struct ahd_softc * ahd, long port, uint8_t val)
 {
-       struct  ahd_softc *ahd;
-       int     found;
-       int     error = 0;
+       if (ahd->tags[0] == BUS_SPACE_MEMIO) {
+               writeb(val, ahd->bshs[0].maddr + port);
+       } else {
+               outb(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
+       }
+       mb();
+}
 
-       /*
-        * If we've been passed any parameters, process them now.
-        */
-       if (aic79xx)
-               aic79xx_setup(aic79xx);
+void
+ahd_outw_atomic(struct ahd_softc * ahd, long port, uint16_t val)
+{
+       if (ahd->tags[0] == BUS_SPACE_MEMIO) {
+               writew(val, ahd->bshs[0].maddr + port);
+       } else {
+               outw(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
+       }
+       mb();
+}
 
-       template->proc_name = "aic79xx";
+void
+ahd_outsb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
+{
+       int i;
 
        /*
-        * Initialize our softc list lock prior to
-        * probing for any adapters.
+        * There is probably a more efficient way to do this on Linux
+        * but we don't use this for anything speed critical and this
+        * should work.
         */
-       ahd_list_lockinit();
+       for (i = 0; i < count; i++)
+               ahd_outb(ahd, port, *array++);
+}
 
-#ifdef CONFIG_PCI
-       error = ahd_linux_pci_init();
-       if (error)
-               return error;
-#endif
+void
+ahd_insb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
+{
+       int i;
 
        /*
-        * Register with the SCSI layer all
-        * controllers we've found.
+        * There is probably a more efficient way to do this on Linux
+        * but we don't use this for anything speed critical and this
+        * should work.
         */
-       found = 0;
-       TAILQ_FOREACH(ahd, &ahd_tailq, links) {
+       for (i = 0; i < count; i++)
+               *array++ = ahd_inb(ahd, port);
+}
+
+/******************************* PCI Routines *********************************/
+uint32_t
+ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
+{
+       switch (width) {
+       case 1:
+       {
+               uint8_t retval;
+
+               pci_read_config_byte(pci, reg, &retval);
+               return (retval);
+       }
+       case 2:
+       {
+               uint16_t retval;
+               pci_read_config_word(pci, reg, &retval);
+               return (retval);
+       }
+       case 4:
+       {
+               uint32_t retval;
+               pci_read_config_dword(pci, reg, &retval);
+               return (retval);
+       }
+       default:
+               panic("ahd_pci_read_config: Read size too big");
+               /* NOTREACHED */
+               return (0);
+       }
+}
 
-               if (ahd_linux_register_host(ahd, template) == 0)
-                       found++;
+void
+ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
+{
+       switch (width) {
+       case 1:
+               pci_write_config_byte(pci, reg, value);
+               break;
+       case 2:
+               pci_write_config_word(pci, reg, value);
+               break;
+       case 4:
+               pci_write_config_dword(pci, reg, value);
+               break;
+       default:
+               panic("ahd_pci_write_config: Write size too big");
+               /* NOTREACHED */
        }
-       aic79xx_detect_complete++;
-       return found;
 }
 
+/****************************** Inlines ***************************************/
+static void ahd_linux_unmap_scb(struct ahd_softc*, struct scb*);
+
+static void
+ahd_linux_unmap_scb(struct ahd_softc *ahd, struct scb *scb)
+{
+       struct scsi_cmnd *cmd;
+
+       cmd = scb->io_ctx;
+       ahd_sync_sglist(ahd, scb, BUS_DMASYNC_POSTWRITE);
+       scsi_dma_unmap(cmd);
+}
+
+/******************************** Macros **************************************/
+#define BUILD_SCSIID(ahd, cmd)                                         \
+       (((scmd_id(cmd) << TID_SHIFT) & TID) | (ahd)->our_id)
+
 /*
  * Return a string describing the driver.
  */
@@ -475,16 +558,13 @@ ahd_linux_info(struct Scsi_Host *host)
        bp = &buffer[0];
        ahd = *(struct ahd_softc **)host->hostdata;
        memset(bp, 0, sizeof(buffer));
-       strcpy(bp, "Adaptec AIC79XX PCI-X SCSI HBA DRIVER, Rev ");
-       strcat(bp, AIC79XX_DRIVER_VERSION);
-       strcat(bp, "\n");
-       strcat(bp, "        <");
+       strcpy(bp, "Adaptec AIC79XX PCI-X SCSI HBA DRIVER, Rev " AIC79XX_DRIVER_VERSION "\n"
+                       "        <");
        strcat(bp, ahd->description);
-       strcat(bp, ">\n");
-       strcat(bp, "        ");
+       strcat(bp, ">\n"
+                       "        ");
        ahd_controller_info(ahd, ahd_info);
        strcat(bp, ahd_info);
-       strcat(bp, "\n");
 
        return (bp);
 }
@@ -497,32 +577,18 @@ ahd_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
 {
        struct   ahd_softc *ahd;
        struct   ahd_linux_device *dev = scsi_transport_device_data(cmd->device);
+       int rtn = SCSI_MLQUEUE_HOST_BUSY;
 
        ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
 
-       /*
-        * Close the race of a command that was in the process of
-        * being queued to us just as our simq was frozen.  Let
-        * DV commands through so long as we are only frozen to
-        * perform DV.
-        */
-       if (ahd->platform_data->qfrozen != 0) {
-               printf("%s: queue frozen\n", ahd_name(ahd));
-
-               return SCSI_MLQUEUE_HOST_BUSY;
-       }
-
-       /*
-        * Save the callback on completion function.
-        */
        cmd->scsi_done = scsi_done;
-
        cmd->result = CAM_REQ_INPROG << 16;
+       rtn = ahd_linux_run_command(ahd, dev, cmd);
 
-       return ahd_linux_run_command(ahd, dev, cmd);
+       return rtn;
 }
 
-static inline struct scsi_target **
+static struct scsi_target **
 ahd_linux_target_in_softc(struct scsi_target *starget)
 {
        struct  ahd_softc *ahd =
@@ -541,9 +607,9 @@ ahd_linux_target_alloc(struct scsi_target *starget)
 {
        struct  ahd_softc *ahd =
                *((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
+       struct seeprom_config *sc = ahd->seep_config;
        unsigned long flags;
        struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
-       struct ahd_linux_target *targ = scsi_transport_target_data(starget);
        struct ahd_devinfo devinfo;
        struct ahd_initiator_tinfo *tinfo;
        struct ahd_tmode_tstate *tstate;
@@ -554,20 +620,35 @@ ahd_linux_target_alloc(struct scsi_target *starget)
        BUG_ON(*ahd_targp != NULL);
 
        *ahd_targp = starget;
-       memset(targ, 0, sizeof(*targ));
+
+       if (sc) {
+               int flags = sc->device_flags[starget->id];
+
+               tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
+                                           starget->id, &tstate);
+
+               if ((flags  & CFPACKETIZED) == 0) {
+                       /* don't negotiate packetized (IU) transfers */
+                       spi_max_iu(starget) = 0;
+               } else {
+                       if ((ahd->features & AHD_RTI) == 0)
+                               spi_rti(starget) = 0;
+               }
+
+               if ((flags & CFQAS) == 0)
+                       spi_max_qas(starget) = 0;
+
+               /* Transinfo values have been set to BIOS settings */
+               spi_max_width(starget) = (flags & CFWIDEB) ? 1 : 0;
+               spi_min_period(starget) = tinfo->user.period;
+               spi_max_offset(starget) = tinfo->user.offset;
+       }
 
        tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
                                    starget->id, &tstate);
        ahd_compile_devinfo(&devinfo, ahd->our_id, starget->id,
                            CAM_LUN_WILDCARD, channel,
                            ROLE_INITIATOR);
-       spi_min_period(starget) = AHD_SYNCRATE_MAX; /* We can do U320 */
-       if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
-               spi_max_offset(starget) = MAX_OFFSET_PACED_BUG;
-       else
-               spi_max_offset(starget) = MAX_OFFSET_PACED;
-       spi_max_width(starget) = ahd->features & AHD_WIDE;
-
        ahd_set_syncrate(ahd, &devinfo, 0, 0, 0,
                         AHD_TRANS_GOAL, /*paused*/FALSE);
        ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
@@ -590,15 +671,11 @@ ahd_linux_slave_alloc(struct scsi_device *sdev)
 {
        struct  ahd_softc *ahd =
                *((struct ahd_softc **)sdev->host->hostdata);
-       struct scsi_target *starget = sdev->sdev_target;
-       struct ahd_linux_target *targ = scsi_transport_target_data(starget);
        struct ahd_linux_device *dev;
 
        if (bootverbose)
                printf("%s: Slave Alloc %d\n", ahd_name(ahd), sdev->id);
 
-       BUG_ON(targ->sdev[sdev->lun] != NULL);
-
        dev = scsi_transport_device_data(sdev);
        memset(dev, 0, sizeof(*dev));
 
@@ -615,8 +692,6 @@ ahd_linux_slave_alloc(struct scsi_device *sdev)
         */
        dev->maxtags = 0;
        
-       targ->sdev[sdev->lun] = sdev;
-
        return (0);
 }
 
@@ -627,7 +702,7 @@ ahd_linux_slave_configure(struct scsi_device *sdev)
 
        ahd = *((struct ahd_softc **)sdev->host->hostdata);
        if (bootverbose)
-               printf("%s: Slave Configure %d\n", ahd_name(ahd), sdev->id);
+               sdev_printk(KERN_INFO, sdev, "Slave Configure\n");
 
        ahd_linux_device_queue_depth(sdev);
 
@@ -638,23 +713,6 @@ ahd_linux_slave_configure(struct scsi_device *sdev)
        return 0;
 }
 
-static void
-ahd_linux_slave_destroy(struct scsi_device *sdev)
-{
-       struct  ahd_softc *ahd;
-       struct  ahd_linux_device *dev = scsi_transport_device_data(sdev);
-       struct  ahd_linux_target *targ = scsi_transport_target_data(sdev->sdev_target);
-
-       ahd = *((struct ahd_softc **)sdev->host->hostdata);
-       if (bootverbose)
-               printf("%s: Slave Destroy %d\n", ahd_name(ahd), sdev->id);
-
-       BUG_ON(dev->active);
-
-       targ->sdev[sdev->lun] = NULL;
-
-}
-
 #if defined(__i386__)
 /*
  * Return the disk geometry for the given SCSI device.
@@ -708,10 +766,9 @@ static int
 ahd_linux_abort(struct scsi_cmnd *cmd)
 {
        int error;
+       
+       error = ahd_linux_queue_abort_cmd(cmd);
 
-       error = ahd_linux_queue_recovery_cmd(cmd, SCB_ABORT);
-       if (error != 0)
-               printf("aic79xx_abort returns 0x%x\n", error);
        return error;
 }
 
@@ -721,12 +778,97 @@ ahd_linux_abort(struct scsi_cmnd *cmd)
 static int
 ahd_linux_dev_reset(struct scsi_cmnd *cmd)
 {
-       int error;
+       struct ahd_softc *ahd;
+       struct ahd_linux_device *dev;
+       struct scb *reset_scb;
+       u_int  cdb_byte;
+       int    retval = SUCCESS;
+       int    paused;
+       int    wait;
+       struct  ahd_initiator_tinfo *tinfo;
+       struct  ahd_tmode_tstate *tstate;
+       unsigned long flags;
+       DECLARE_COMPLETION_ONSTACK(done);
 
-       error = ahd_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
-       if (error != 0)
-               printf("aic79xx_dev_reset returns 0x%x\n", error);
-       return error;
+       reset_scb = NULL;
+       paused = FALSE;
+       wait = FALSE;
+       ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
+
+       scmd_printk(KERN_INFO, cmd,
+                   "Attempting to queue a TARGET RESET message:");
+
+       printf("CDB:");
+       for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
+               printf(" 0x%x", cmd->cmnd[cdb_byte]);
+       printf("\n");
+
+       /*
+        * Determine if we currently own this command.
+        */
+       dev = scsi_transport_device_data(cmd->device);
+
+       if (dev == NULL) {
+               /*
+                * No target device for this command exists,
+                * so we must not still own the command.
+                */
+               scmd_printk(KERN_INFO, cmd, "Is not an active device\n");
+               return SUCCESS;
+       }
+
+       /*
+        * Generate us a new SCB
+        */
+       reset_scb = ahd_get_scb(ahd, AHD_NEVER_COL_IDX);
+       if (!reset_scb) {
+               scmd_printk(KERN_INFO, cmd, "No SCB available\n");
+               return FAILED;
+       }
+
+       tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
+                                   cmd->device->id, &tstate);
+       reset_scb->io_ctx = cmd;
+       reset_scb->platform_data->dev = dev;
+       reset_scb->sg_count = 0;
+       ahd_set_residual(reset_scb, 0);
+       ahd_set_sense_residual(reset_scb, 0);
+       reset_scb->platform_data->xfer_len = 0;
+       reset_scb->hscb->control = 0;
+       reset_scb->hscb->scsiid = BUILD_SCSIID(ahd,cmd);
+       reset_scb->hscb->lun = cmd->device->lun;
+       reset_scb->hscb->cdb_len = 0;
+       reset_scb->hscb->task_management = SIU_TASKMGMT_LUN_RESET;
+       reset_scb->flags |= SCB_DEVICE_RESET|SCB_RECOVERY_SCB|SCB_ACTIVE;
+       if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
+               reset_scb->flags |= SCB_PACKETIZED;
+       } else {
+               reset_scb->hscb->control |= MK_MESSAGE;
+       }
+       dev->openings--;
+       dev->active++;
+       dev->commands_issued++;
+
+       ahd_lock(ahd, &flags);
+
+       LIST_INSERT_HEAD(&ahd->pending_scbs, reset_scb, pending_links);
+       ahd_queue_scb(ahd, reset_scb);
+
+       ahd->platform_data->eh_done = &done;
+       ahd_unlock(ahd, &flags);
+
+       printf("%s: Device reset code sleeping\n", ahd_name(ahd));
+       if (!wait_for_completion_timeout(&done, 5 * HZ)) {
+               ahd_lock(ahd, &flags);
+               ahd->platform_data->eh_done = NULL;
+               ahd_unlock(ahd, &flags);
+               printf("%s: Device reset timer expired (active %d)\n",
+                      ahd_name(ahd), dev->active);
+               retval = FAILED;
+       }
+       printf("%s: Device reset returning 0x%x\n", ahd_name(ahd), retval);
+
+       return (retval);
 }
 
 /*
@@ -736,8 +878,8 @@ static int
 ahd_linux_bus_reset(struct scsi_cmnd *cmd)
 {
        struct ahd_softc *ahd;
-       u_long s;
        int    found;
+       unsigned long flags;
 
        ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
 #ifdef AHD_DEBUG
@@ -745,10 +887,11 @@ ahd_linux_bus_reset(struct scsi_cmnd *cmd)
                printf("%s: Bus reset called for cmd %p\n",
                       ahd_name(ahd), cmd);
 #endif
-       ahd_lock(ahd, &s);
-       found = ahd_reset_channel(ahd, cmd->device->channel + 'A',
+       ahd_lock(ahd, &flags);
+
+       found = ahd_reset_channel(ahd, scmd_channel(cmd) + 'A',
                                  /*initiate reset*/TRUE);
-       ahd_unlock(ahd, &s);
+       ahd_unlock(ahd, &flags);
 
        if (bootverbose)
                printf("%s: SCSI bus reset delivered. "
@@ -760,6 +903,7 @@ ahd_linux_bus_reset(struct scsi_cmnd *cmd)
 struct scsi_host_template aic79xx_driver_template = {
        .module                 = THIS_MODULE,
        .name                   = "aic79xx",
+       .proc_name              = "aic79xx",
        .proc_info              = ahd_linux_proc_info,
        .info                   = ahd_linux_info,
        .queuecommand           = ahd_linux_queue,
@@ -771,11 +915,11 @@ struct scsi_host_template aic79xx_driver_template = {
 #endif
        .can_queue              = AHD_MAX_QUEUE,
        .this_id                = -1,
+       .max_sectors            = 8192,
        .cmd_per_lun            = 2,
        .use_clustering         = ENABLE_CLUSTERING,
        .slave_alloc            = ahd_linux_slave_alloc,
        .slave_configure        = ahd_linux_slave_configure,
-       .slave_destroy          = ahd_linux_slave_destroy,
        .target_alloc           = ahd_linux_target_alloc,
        .target_destroy         = ahd_linux_target_destroy,
 };
@@ -864,65 +1008,12 @@ ahd_dmamap_unload(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
 }
 
 /********************* Platform Dependent Functions ***************************/
-/*
- * Compare "left hand" softc with "right hand" softc, returning:
- * < 0 - lahd has a lower priority than rahd
- *   0 - Softcs are equal
- * > 0 - lahd has a higher priority than rahd
- */
-int
-ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
-{
-       int     value;
-
-       /*
-        * Under Linux, cards are ordered as follows:
-        *      1) PCI devices that are marked as the boot controller.
-        *      2) PCI devices with BIOS enabled sorted by bus/slot/func.
-        *      3) All remaining PCI devices sorted by bus/slot/func.
-        */
-#if 0
-       value = (lahd->flags & AHD_BOOT_CHANNEL)
-             - (rahd->flags & AHD_BOOT_CHANNEL);
-       if (value != 0)
-               /* Controllers set for boot have a *higher* priority */
-               return (value);
-#endif
-
-       value = (lahd->flags & AHD_BIOS_ENABLED)
-             - (rahd->flags & AHD_BIOS_ENABLED);
-       if (value != 0)
-               /* Controllers with BIOS enabled have a *higher* priority */
-               return (value);
-
-       /* Still equal.  Sort by bus/slot/func. */
-       if (aic79xx_reverse_scan != 0)
-               value = ahd_get_pci_bus(lahd->dev_softc)
-                     - ahd_get_pci_bus(rahd->dev_softc);
-       else
-               value = ahd_get_pci_bus(rahd->dev_softc)
-                     - ahd_get_pci_bus(lahd->dev_softc);
-       if (value != 0)
-               return (value);
-       if (aic79xx_reverse_scan != 0)
-               value = ahd_get_pci_slot(lahd->dev_softc)
-                     - ahd_get_pci_slot(rahd->dev_softc);
-       else
-               value = ahd_get_pci_slot(rahd->dev_softc)
-                     - ahd_get_pci_slot(lahd->dev_softc);
-       if (value != 0)
-               return (value);
-
-       value = rahd->channel - lahd->channel;
-       return (value);
-}
-
 static void
 ahd_linux_setup_iocell_info(u_long index, int instance, int targ, int32_t value)
 {
 
        if ((instance >= 0)
-        && (instance < NUM_ELEMENTS(aic79xx_iocell_info))) {
+        && (instance < ARRAY_SIZE(aic79xx_iocell_info))) {
                uint8_t *iocell_info;
 
                iocell_info = (uint8_t*)&aic79xx_iocell_info[instance];
@@ -940,7 +1031,7 @@ ahd_linux_setup_tag_info_global(char *p)
        tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
        printf("Setting Global Tags= %d\n", tags);
 
-       for (i = 0; i < NUM_ELEMENTS(aic79xx_tag_info); i++) {
+       for (i = 0; i < ARRAY_SIZE(aic79xx_tag_info); i++) {
                for (j = 0; j < AHD_NUM_TARGETS; j++) {
                        aic79xx_tag_info[i].tag_commands[j] = tags;
                }
@@ -952,7 +1043,7 @@ ahd_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
 {
 
        if ((instance >= 0) && (targ >= 0)
-        && (instance < NUM_ELEMENTS(aic79xx_tag_info))
+        && (instance < ARRAY_SIZE(aic79xx_tag_info))
         && (targ < AHD_NUM_TARGETS)) {
                aic79xx_tag_info[instance].tag_commands[targ] = value & 0x1FF;
                if (bootverbose)
@@ -960,6 +1051,86 @@ ahd_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
        }
 }
 
+static char *
+ahd_parse_brace_option(char *opt_name, char *opt_arg, char *end, int depth,
+                      void (*callback)(u_long, int, int, int32_t),
+                      u_long callback_arg)
+{
+       char    *tok_end;
+       char    *tok_end2;
+       int      i;
+       int      instance;
+       int      targ;
+       int      done;
+       char     tok_list[] = {'.', ',', '{', '}', '\0'};
+
+       /* All options use a ':' name/arg separator */
+       if (*opt_arg != ':')
+               return (opt_arg);
+       opt_arg++;
+       instance = -1;
+       targ = -1;
+       done = FALSE;
+       /*
+        * Restore separator that may be in
+        * the middle of our option argument.
+        */
+       tok_end = strchr(opt_arg, '\0');
+       if (tok_end < end)
+               *tok_end = ',';
+       while (!done) {
+               switch (*opt_arg) {
+               case '{':
+                       if (instance == -1) {
+                               instance = 0;
+                       } else {
+                               if (depth > 1) {
+                                       if (targ == -1)
+                                               targ = 0;
+                               } else {
+                                       printf("Malformed Option %s\n",
+                                              opt_name);
+                                       done = TRUE;
+                               }
+                       }
+                       opt_arg++;
+                       break;
+               case '}':
+                       if (targ != -1)
+                               targ = -1;
+                       else if (instance != -1)
+                               instance = -1;
+                       opt_arg++;
+                       break;
+               case ',':
+               case '.':
+                       if (instance == -1)
+                               done = TRUE;
+                       else if (targ >= 0)
+                               targ++;
+                       else if (instance >= 0)
+                               instance++;
+                       opt_arg++;
+                       break;
+               case '\0':
+                       done = TRUE;
+                       break;
+               default:
+                       tok_end = end;
+                       for (i = 0; tok_list[i]; i++) {
+                               tok_end2 = strchr(opt_arg, tok_list[i]);
+                               if ((tok_end2) && (tok_end2 < tok_end))
+                                       tok_end = tok_end2;
+                       }
+                       callback(callback_arg, instance, targ,
+                                simple_strtol(opt_arg, NULL, 0));
+                       opt_arg = tok_end;
+                       break;
+               }
+       }
+       return (opt_arg);
+}
+
 /*
  * Handle Linux boot parameters. This routine allows for assigning a value
  * to a parameter with a ':' between the parameter and the value.
@@ -972,7 +1143,7 @@ aic79xx_setup(char *s)
        char   *p;
        char   *end;
 
-       static struct {
+       static const struct {
                const char *name;
                uint32_t *flag;
        } options[] = {
@@ -983,7 +1154,6 @@ aic79xx_setup(char *s)
 #ifdef AHD_DEBUG
                { "debug", &ahd_debug },
 #endif
-               { "reverse_scan", &aic79xx_reverse_scan },
                { "periodic_otag", &aic79xx_periodic_otag },
                { "pci_parity", &aic79xx_pci_parity },
                { "seltime", &aic79xx_seltime },
@@ -992,43 +1162,44 @@ aic79xx_setup(char *s)
                { "slewrate", NULL },
                { "precomp", NULL },
                { "amplitude", NULL },
+               { "slowcrc", &aic79xx_slowcrc },
        };
 
        end = strchr(s, '\0');
 
        /*
-        * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
+        * XXX ia64 gcc isn't smart enough to know that ARRAY_SIZE
         * will never be 0 in this case.
-        */      
-       n = 0;  
+        */
+       n = 0;
 
        while ((p = strsep(&s, ",.")) != NULL) {
                if (*p == '\0')
                        continue;
-               for (i = 0; i < NUM_ELEMENTS(options); i++) {
+               for (i = 0; i < ARRAY_SIZE(options); i++) {
 
                        n = strlen(options[i].name);
                        if (strncmp(options[i].name, p, n) == 0)
                                break;
                }
-               if (i == NUM_ELEMENTS(options))
+               if (i == ARRAY_SIZE(options))
                        continue;
 
                if (strncmp(p, "global_tag_depth", n) == 0) {
                        ahd_linux_setup_tag_info_global(p + n);
                } else if (strncmp(p, "tag_info", n) == 0) {
-                       s = aic_parse_brace_option("tag_info", p + n, end,
+                       s = ahd_parse_brace_option("tag_info", p + n, end,
                            2, ahd_linux_setup_tag_info, 0);
                } else if (strncmp(p, "slewrate", n) == 0) {
-                       s = aic_parse_brace_option("slewrate",
+                       s = ahd_parse_brace_option("slewrate",
                            p + n, end, 1, ahd_linux_setup_iocell_info,
                            AIC79XX_SLEWRATE_INDEX);
                } else if (strncmp(p, "precomp", n) == 0) {
-                       s = aic_parse_brace_option("precomp",
+                       s = ahd_parse_brace_option("precomp",
                            p + n, end, 1, ahd_linux_setup_iocell_info,
                            AIC79XX_PRECOMP_INDEX);
                } else if (strncmp(p, "amplitude", n) == 0) {
-                       s = aic_parse_brace_option("amplitude",
+                       s = ahd_parse_brace_option("amplitude",
                            p + n, end, 1, ahd_linux_setup_iocell_info,
                            AIC79XX_AMPLITUDE_INDEX);
                } else if (p[n] == ':') {
@@ -1053,6 +1224,7 @@ ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *templa
        struct  Scsi_Host *host;
        char    *new_name;
        u_long  s;
+       int     retval;
 
        template->name = ahd->description;
        host = scsi_host_alloc(template, sizeof(struct ahd_softc *));
@@ -1060,8 +1232,6 @@ ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *templa
                return (ENOMEM);
 
        *((struct ahd_softc **)host->hostdata) = ahd;
-       ahd_lock(ahd, &s);
-       scsi_assign_lock(host, &ahd->platform_data->spin_lock);
        ahd->platform_data->host = host;
        host->can_queue = AHD_MAX_QUEUE;
        host->cmd_per_lun = 2;
@@ -1072,7 +1242,9 @@ ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *templa
        host->max_lun = AHD_NUM_LUNS;
        host->max_channel = 0;
        host->sg_tablesize = AHD_NSEG;
-       ahd_set_unit(ahd, ahd_linux_next_unit());
+       ahd_lock(ahd, &s);
+       ahd_set_unit(ahd, ahd_linux_unit++);
+       ahd_unlock(ahd, &s);
        sprintf(buf, "scsi%d", host->host_no);
        new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
        if (new_name != NULL) {
@@ -1082,45 +1254,18 @@ ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *templa
        host->unique_id = ahd->unit;
        ahd_linux_initialize_scsi_bus(ahd);
        ahd_intr_enable(ahd, TRUE);
-       ahd_unlock(ahd, &s);
 
        host->transportt = ahd_linux_transport_template;
 
-       scsi_add_host(host, &ahd->dev_softc->dev); /* XXX handle failure */
-       scsi_scan_host(host);
-       return (0);
-}
-
-uint64_t
-ahd_linux_get_memsize(void)
-{
-       struct sysinfo si;
-
-       si_meminfo(&si);
-       return ((uint64_t)si.totalram << PAGE_SHIFT);
-}
-
-/*
- * Find the smallest available unit number to use
- * for a new device.  We don't just use a static
- * count to handle the "repeated hot-(un)plug"
- * scenario.
- */
-static int
-ahd_linux_next_unit(void)
-{
-       struct ahd_softc *ahd;
-       int unit;
-
-       unit = 0;
-retry:
-       TAILQ_FOREACH(ahd, &ahd_tailq, links) {
-               if (ahd->unit == unit) {
-                       unit++;
-                       goto retry;
-               }
+       retval = scsi_add_host(host, &ahd->dev_softc->dev);
+       if (retval) {
+               printk(KERN_WARNING "aic79xx: scsi_add_host failed\n");
+               scsi_host_put(host);
+               return retval;
        }
-       return (unit);
+
+       scsi_scan_host(host);
+       return 0;
 }
 
 /*
@@ -1133,6 +1278,7 @@ ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd)
 {
        u_int target_id;
        u_int numtarg;
+       unsigned long s;
 
        target_id = 0;
        numtarg = 0;
@@ -1145,6 +1291,8 @@ ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd)
        else
                numtarg = (ahd->features & AHD_WIDE) ? 16 : 8;
 
+       ahd_lock(ahd, &s);
+
        /*
         * Force negotiation to async for all targets that
         * will not see an initial bus reset.
@@ -1161,16 +1309,12 @@ ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd)
                ahd_update_neg_request(ahd, &devinfo, tstate,
                                       tinfo, AHD_NEG_ALWAYS);
        }
+       ahd_unlock(ahd, &s);
        /* Give the bus some time to recover */
        if ((ahd->flags & AHD_RESET_BUS_A) != 0) {
                ahd_freeze_simq(ahd);
-               init_timer(&ahd->platform_data->reset_timer);
-               ahd->platform_data->reset_timer.data = (u_long)ahd;
-               ahd->platform_data->reset_timer.expires =
-                   jiffies + (AIC79XX_RESET_DELAY * HZ)/1000;
-               ahd->platform_data->reset_timer.function =
-                   (ahd_linux_callback_t *)ahd_release_simq;
-               add_timer(&ahd->platform_data->reset_timer);
+               msleep(AIC79XX_RESET_DELAY);
+               ahd_release_simq(ahd);
        }
 }
 
@@ -1184,7 +1328,6 @@ ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
        memset(ahd->platform_data, 0, sizeof(struct ahd_platform_data));
        ahd->platform_data->irq = AHD_LINUX_NOIRQ;
        ahd_lockinit(ahd);
-       init_MUTEX_LOCKED(&ahd->platform_data->eh_sem);
        ahd->seltime = (aic79xx_seltime & 0x3) << 4;
        return (0);
 }
@@ -1193,25 +1336,13 @@ void
 ahd_platform_free(struct ahd_softc *ahd)
 {
        struct scsi_target *starget;
-       int i, j;
+       int i;
 
        if (ahd->platform_data != NULL) {
-               if (ahd->platform_data->host != NULL) {
-                       scsi_remove_host(ahd->platform_data->host);
-                       scsi_host_put(ahd->platform_data->host);
-               }
-
                /* destroy all of the device and target objects */
                for (i = 0; i < AHD_NUM_TARGETS; i++) {
                        starget = ahd->platform_data->starget[i];
                        if (starget != NULL) {
-                               for (j = 0; j < AHD_NUM_LUNS; j++) {
-                                       struct ahd_linux_target *targ =
-                                               scsi_transport_target_data(starget);
-                                       if (targ->sdev[j] == NULL)
-                                               continue;
-                                       targ->sdev[j] = NULL;
-                               }
                                ahd->platform_data->starget[i] = NULL;
                        }
                }
@@ -1230,6 +1361,9 @@ ahd_platform_free(struct ahd_softc *ahd)
                        release_mem_region(ahd->platform_data->mem_busaddr,
                                           0x1000);
                }
+               if (ahd->platform_data->host)
+                       scsi_host_put(ahd->platform_data->host);
+
                free(ahd->platform_data, M_DEVBUF);
        }
 }
@@ -1240,8 +1374,8 @@ ahd_platform_init(struct ahd_softc *ahd)
        /*
         * Lookup and commit any modified IO Cell options.
         */
-       if (ahd->unit < NUM_ELEMENTS(aic79xx_iocell_info)) {
-               struct ahd_linux_iocell_opts *iocell_opts;
+       if (ahd->unit < ARRAY_SIZE(aic79xx_iocell_info)) {
+               const struct ahd_linux_iocell_opts *iocell_opts;
 
                iocell_opts = &aic79xx_iocell_info[ahd->unit];
                if (iocell_opts->precomp != AIC79XX_DEFAULT_PRECOMP)
@@ -1264,20 +1398,13 @@ ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
 }
 
 void
-ahd_platform_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
-                     ahd_queue_alg alg)
+ahd_platform_set_tags(struct ahd_softc *ahd, struct scsi_device *sdev,
+                     struct ahd_devinfo *devinfo, ahd_queue_alg alg)
 {
-       struct scsi_target *starget;
-       struct ahd_linux_target *targ;
        struct ahd_linux_device *dev;
-       struct scsi_device *sdev;
        int was_queuing;
        int now_queuing;
 
-       starget = ahd->platform_data->starget[devinfo->target];
-       targ = scsi_transport_target_data(starget);
-       BUG_ON(targ == NULL);
-       sdev = targ->sdev[devinfo->lun];
        if (sdev == NULL)
                return;
 
@@ -1338,14 +1465,12 @@ ahd_platform_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
 
        switch ((dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED))) {
        case AHD_DEV_Q_BASIC:
-               scsi_adjust_queue_depth(sdev,
-                                       MSG_SIMPLE_TASK,
-                                       dev->openings + dev->active);
+               scsi_set_tag_type(sdev, MSG_SIMPLE_TASK);
+               scsi_activate_tcq(sdev, dev->openings + dev->active);
                break;
        case AHD_DEV_Q_TAGGED:
-               scsi_adjust_queue_depth(sdev,
-                                       MSG_ORDERED_TASK,
-                                       dev->openings + dev->active);
+               scsi_set_tag_type(sdev, MSG_ORDERED_TASK);
+               scsi_activate_tcq(sdev, dev->openings + dev->active);
                break;
        default:
                /*
@@ -1354,9 +1479,7 @@ ahd_platform_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
                 * serially on the controller/device.  This should
                 * remove some latency.
                 */
-               scsi_adjust_queue_depth(sdev,
-                                       /*NON-TAGGED*/0,
-                                       /*queue depth*/2);
+               scsi_deactivate_tcq(sdev, 1);
                break;
        }
 }
@@ -1376,7 +1499,7 @@ ahd_linux_user_tagdepth(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
 
        tags = 0;
        if ((ahd->user_discenable & devinfo->target_mask) != 0) {
-               if (ahd->unit >= NUM_ELEMENTS(aic79xx_tag_info)) {
+               if (ahd->unit >= ARRAY_SIZE(aic79xx_tag_info)) {
 
                        if (warned_user == 0) {
                                printf(KERN_WARNING
@@ -1417,11 +1540,15 @@ ahd_linux_device_queue_depth(struct scsi_device *sdev)
        tags = ahd_linux_user_tagdepth(ahd, &devinfo);
        if (tags != 0 && sdev->tagged_supported != 0) {
 
-               ahd_set_tags(ahd, &devinfo, AHD_QUEUE_TAGGED);
+               ahd_platform_set_tags(ahd, sdev, &devinfo, AHD_QUEUE_TAGGED);
+               ahd_send_async(ahd, devinfo.channel, devinfo.target,
+                              devinfo.lun, AC_TRANSFER_NEG);
                ahd_print_devinfo(ahd, &devinfo);
                printf("Tagged Queuing enabled.  Depth %d\n", tags);
        } else {
-               ahd_set_tags(ahd, &devinfo, AHD_QUEUE_NONE);
+               ahd_platform_set_tags(ahd, sdev, &devinfo, AHD_QUEUE_NONE);
+               ahd_send_async(ahd, devinfo.channel, devinfo.target,
+                              devinfo.lun, AC_TRANSFER_NEG);
        }
 }
 
@@ -1435,6 +1562,14 @@ ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
        struct   ahd_tmode_tstate *tstate;
        u_int    col_idx;
        uint16_t mask;
+       unsigned long flags;
+       int nseg;
+
+       nseg = scsi_dma_map(cmd);
+       if (nseg < 0)
+               return SCSI_MLQUEUE_HOST_BUSY;
+
+       ahd_lock(ahd, &flags);
 
        /*
         * Get an scb to use.
@@ -1450,6 +1585,8 @@ ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
        }
        if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
                ahd->flags |= AHD_RESOURCE_SHORTAGE;
+               ahd_unlock(ahd, &flags);
+               scsi_dma_unmap(cmd);
                return SCSI_MLQUEUE_HOST_BUSY;
        }
 
@@ -1504,18 +1641,15 @@ ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
        ahd_set_residual(scb, 0);
        ahd_set_sense_residual(scb, 0);
        scb->sg_count = 0;
-       if (cmd->use_sg != 0) {
-               void    *sg;
-               struct   scatterlist *cur_seg;
-               u_int    nseg;
-               int      dir;
-
-               cur_seg = (struct scatterlist *)cmd->request_buffer;
-               dir = cmd->sc_data_direction;
-               nseg = pci_map_sg(ahd->dev_softc, cur_seg,
-                                 cmd->use_sg, dir);
+
+       if (nseg > 0) {
+               void *sg = scb->sg_list;
+               struct scatterlist *cur_seg;
+               int i;
+
                scb->platform_data->xfer_len = 0;
-               for (sg = scb->sg_list; nseg > 0; nseg--, cur_seg++) {
+
+               scsi_for_each_sg(cmd, cur_seg, nseg, i) {
                        dma_addr_t addr;
                        bus_size_t len;
 
@@ -1523,22 +1657,8 @@ ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
                        len = sg_dma_len(cur_seg);
                        scb->platform_data->xfer_len += len;
                        sg = ahd_sg_setup(ahd, scb, sg, addr, len,
-                                         /*last*/nseg == 1);
+                                         i == (nseg - 1));
                }
-       } else if (cmd->request_bufflen != 0) {
-               void *sg;
-               dma_addr_t addr;
-               int dir;
-
-               sg = scb->sg_list;
-               dir = cmd->sc_data_direction;
-               addr = pci_map_single(ahd->dev_softc,
-                                     cmd->request_buffer,
-                                     cmd->request_bufflen, dir);
-               scb->platform_data->xfer_len = cmd->request_bufflen;
-               scb->platform_data->buf_busaddr = addr;
-               sg = ahd_sg_setup(ahd, scb, sg, addr,
-                                 cmd->request_bufflen, /*last*/TRUE);
        }
 
        LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
@@ -1551,6 +1671,8 @@ ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
        scb->flags |= SCB_ACTIVE;
        ahd_queue_scb(ahd, scb);
 
+       ahd_unlock(ahd, &flags);
+
        return 0;
 }
 
@@ -1558,7 +1680,7 @@ ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
  * SCSI controller interrupt handler.
  */
 irqreturn_t
-ahd_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
+ahd_linux_isr(int irq, void *dev_id)
 {
        struct  ahd_softc *ahd;
        u_long  flags;
@@ -1572,21 +1694,14 @@ ahd_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
 }
 
 void
-ahd_platform_flushwork(struct ahd_softc *ahd)
-{
-
-}
-
-void
 ahd_send_async(struct ahd_softc *ahd, char channel,
-              u_int target, u_int lun, ac_code code, void *arg)
+              u_int target, u_int lun, ac_code code)
 {
        switch (code) {
        case AC_TRANSFER_NEG:
        {
                char    buf[80];
                struct  scsi_target *starget;
-               struct  ahd_linux_target *targ;
                struct  info_str info;
                struct  ahd_initiator_tinfo *tinfo;
                struct  ahd_tmode_tstate *tstate;
@@ -1617,14 +1732,18 @@ ahd_send_async(struct ahd_softc *ahd, char channel,
                 * are identical to those last reported.
                 */
                starget = ahd->platform_data->starget[target];
-               targ = scsi_transport_target_data(starget);
-               if (targ == NULL)
+               if (starget == NULL)
                        break;
 
                target_ppr_options =
                        (spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
                        + (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
-                       + (spi_iu(starget) ?  MSG_EXT_PPR_IU_REQ : 0);
+                       + (spi_iu(starget) ?  MSG_EXT_PPR_IU_REQ : 0)
+                       + (spi_rd_strm(starget) ? MSG_EXT_PPR_RD_STRM : 0)
+                       + (spi_pcomp_en(starget) ? MSG_EXT_PPR_PCOMP_EN : 0)
+                       + (spi_rti(starget) ? MSG_EXT_PPR_RTI : 0)
+                       + (spi_wr_flow(starget) ? MSG_EXT_PPR_WR_FLOW : 0)
+                       + (spi_hold_mcs(starget) ? MSG_EXT_PPR_HOLD_MCS : 0);
 
                if (tinfo->curr.period == spi_period(starget)
                    && tinfo->curr.width == spi_width(starget)
@@ -1639,6 +1758,11 @@ ahd_send_async(struct ahd_softc *ahd, char channel,
                spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
                spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
                spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
+               spi_rd_strm(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_RD_STRM ? 1 : 0;
+               spi_pcomp_en(starget) =  tinfo->curr.ppr_options & MSG_EXT_PPR_PCOMP_EN ? 1 : 0;
+               spi_rti(starget) =  tinfo->curr.ppr_options &  MSG_EXT_PPR_RTI ? 1 : 0;
+               spi_wr_flow(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_WR_FLOW ? 1 : 0;
+               spi_hold_mcs(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_HOLD_MCS ? 1 : 0;
                spi_display_xfer_agreement(starget);
                break;
        }
@@ -1761,10 +1885,9 @@ ahd_done(struct ahd_softc *ahd, struct scb *scb)
                if (ahd_get_transaction_status(scb) == CAM_BDR_SENT
                 || ahd_get_transaction_status(scb) == CAM_REQ_ABORTED)
                        ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
-               if ((ahd->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
-                       ahd->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
-                       up(&ahd->platform_data->eh_sem);
-               }
+
+               if (ahd->platform_data->eh_done)
+                       complete(ahd->platform_data->eh_done);
        }
 
        ahd_free_scb(ahd, scb);
@@ -1813,9 +1936,9 @@ ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
                        u_int sense_offset;
 
                        if (scb->flags & SCB_SENSE) {
-                               sense_size = MIN(sizeof(struct scsi_sense_data)
+                               sense_size = min(sizeof(struct scsi_sense_data)
                                               - ahd_get_sense_residual(scb),
-                                                sizeof(cmd->sense_buffer));
+                                                (u_long)SCSI_SENSE_BUFFERSIZE);
                                sense_offset = 0;
                        } else {
                                /*
@@ -1824,12 +1947,13 @@ ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
                                 */
                                siu = (struct scsi_status_iu_header *)
                                    scb->sense_data;
-                               sense_size = MIN(scsi_4btoul(siu->sense_length),
-                                               sizeof(cmd->sense_buffer));
+                               sense_size = min_t(size_t,
+                                               scsi_4btoul(siu->sense_length),
+                                               SCSI_SENSE_BUFFERSIZE);
                                sense_offset = SIU_SENSE_OFFSET(siu);
                        }
 
-                       memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
+                       memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
                        memcpy(cmd->sense_buffer,
                               ahd_get_sense_buf(ahd, scb)
                               + sense_offset, sense_size);
@@ -1899,7 +2023,7 @@ ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
                        }
                        ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
                        ahd_set_scsi_status(scb, SCSI_STATUS_OK);
-                       ahd_platform_set_tags(ahd, &devinfo,
+                       ahd_platform_set_tags(ahd, sdev, &devinfo,
                                     (dev->flags & AHD_DEV_Q_BASIC)
                                   ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
                        break;
@@ -1909,7 +2033,7 @@ ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
                 * as if the target returned BUSY SCSI status.
                 */
                dev->openings = 1;
-               ahd_platform_set_tags(ahd, &devinfo,
+               ahd_platform_set_tags(ahd, sdev, &devinfo,
                             (dev->flags & AHD_DEV_Q_BASIC)
                           ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
                ahd_set_scsi_status(scb, SCSI_STATUS_BUSY);
@@ -1919,129 +2043,125 @@ ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
 static void
 ahd_linux_queue_cmd_complete(struct ahd_softc *ahd, struct scsi_cmnd *cmd)
 {
+       int status;
+       int new_status = DID_OK;
+       int do_fallback = 0;
+       int scsi_status;
+
        /*
         * Map CAM error codes into Linux Error codes.  We
         * avoid the conversion so that the DV code has the
         * full error information available when making
         * state change decisions.
         */
-       {
-               uint32_t status;
-               u_int new_status;
-
-               status = ahd_cmd_get_transaction_status(cmd);
-               switch (status) {
-               case CAM_REQ_INPROG:
-               case CAM_REQ_CMP:
-               case CAM_SCSI_STATUS_ERROR:
-                       new_status = DID_OK;
-                       break;
-               case CAM_REQ_ABORTED:
-                       new_status = DID_ABORT;
-                       break;
-               case CAM_BUSY:
-                       new_status = DID_BUS_BUSY;
-                       break;
-               case CAM_REQ_INVALID:
-               case CAM_PATH_INVALID:
-                       new_status = DID_BAD_TARGET;
-                       break;
-               case CAM_SEL_TIMEOUT:
-                       new_status = DID_NO_CONNECT;
-                       break;
-               case CAM_SCSI_BUS_RESET:
-               case CAM_BDR_SENT:
-                       new_status = DID_RESET;
-                       break;
-               case CAM_UNCOR_PARITY:
-                       new_status = DID_PARITY;
-                       break;
-               case CAM_CMD_TIMEOUT:
-                       new_status = DID_TIME_OUT;
-                       break;
-               case CAM_UA_ABORT:
-               case CAM_REQ_CMP_ERR:
-               case CAM_AUTOSENSE_FAIL:
-               case CAM_NO_HBA:
-               case CAM_DATA_RUN_ERR:
-               case CAM_UNEXP_BUSFREE:
-               case CAM_SEQUENCE_FAIL:
-               case CAM_CCB_LEN_ERR:
-               case CAM_PROVIDE_FAIL:
-               case CAM_REQ_TERMIO:
-               case CAM_UNREC_HBA_ERROR:
-               case CAM_REQ_TOO_BIG:
-                       new_status = DID_ERROR;
-                       break;
-               case CAM_REQUEUE_REQ:
-                       new_status = DID_REQUEUE;
+
+       status = ahd_cmd_get_transaction_status(cmd);
+       switch (status) {
+       case CAM_REQ_INPROG:
+       case CAM_REQ_CMP:
+               new_status = DID_OK;
+               break;
+       case CAM_AUTOSENSE_FAIL:
+               new_status = DID_ERROR;
+               /* Fallthrough */
+       case CAM_SCSI_STATUS_ERROR:
+               scsi_status = ahd_cmd_get_scsi_status(cmd);
+
+               switch(scsi_status) {
+               case SCSI_STATUS_CMD_TERMINATED:
+               case SCSI_STATUS_CHECK_COND:
+                       if ((cmd->result >> 24) != DRIVER_SENSE) {
+                               do_fallback = 1;
+                       } else {
+                               struct scsi_sense_data *sense;
+                               
+                               sense = (struct scsi_sense_data *)
+                                       cmd->sense_buffer;
+                               if (sense->extra_len >= 5 &&
+                                   (sense->add_sense_code == 0x47
+                                    || sense->add_sense_code == 0x48))
+                                       do_fallback = 1;
+                       }
                        break;
                default:
-                       /* We should never get here */
-                       new_status = DID_ERROR;
                        break;
                }
+               break;
+       case CAM_REQ_ABORTED:
+               new_status = DID_ABORT;
+               break;
+       case CAM_BUSY:
+               new_status = DID_BUS_BUSY;
+               break;
+       case CAM_REQ_INVALID:
+       case CAM_PATH_INVALID:
+               new_status = DID_BAD_TARGET;
+               break;
+       case CAM_SEL_TIMEOUT:
+               new_status = DID_NO_CONNECT;
+               break;
+       case CAM_SCSI_BUS_RESET:
+       case CAM_BDR_SENT:
+               new_status = DID_RESET;
+               break;
+       case CAM_UNCOR_PARITY:
+               new_status = DID_PARITY;
+               do_fallback = 1;
+               break;
+       case CAM_CMD_TIMEOUT:
+               new_status = DID_TIME_OUT;
+               do_fallback = 1;
+               break;
+       case CAM_REQ_CMP_ERR:
+       case CAM_UNEXP_BUSFREE:
+       case CAM_DATA_RUN_ERR:
+               new_status = DID_ERROR;
+               do_fallback = 1;
+               break;
+       case CAM_UA_ABORT:
+       case CAM_NO_HBA:
+       case CAM_SEQUENCE_FAIL:
+       case CAM_CCB_LEN_ERR:
+       case CAM_PROVIDE_FAIL:
+       case CAM_REQ_TERMIO:
+       case CAM_UNREC_HBA_ERROR:
+       case CAM_REQ_TOO_BIG:
+               new_status = DID_ERROR;
+               break;
+       case CAM_REQUEUE_REQ:
+               new_status = DID_REQUEUE;
+               break;
+       default:
+               /* We should never get here */
+               new_status = DID_ERROR;
+               break;
+       }
 
-               ahd_cmd_set_transaction_status(cmd, new_status);
+       if (do_fallback) {
+               printf("%s: device overrun (status %x) on %d:%d:%d\n",
+                      ahd_name(ahd), status, cmd->device->channel,
+                      cmd->device->id, cmd->device->lun);
        }
 
+       ahd_cmd_set_transaction_status(cmd, new_status);
+
        cmd->scsi_done(cmd);
 }
 
 static void
-ahd_linux_sem_timeout(u_long arg)
-{
-       struct  ahd_softc *ahd;
-       u_long  s;
-
-       ahd = (struct ahd_softc *)arg;
-
-       ahd_lock(ahd, &s);
-       if ((ahd->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
-               ahd->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
-               up(&ahd->platform_data->eh_sem);
-       }
-       ahd_unlock(ahd, &s);
-}
-
-void
 ahd_freeze_simq(struct ahd_softc *ahd)
 {
-       ahd->platform_data->qfrozen++;
-       if (ahd->platform_data->qfrozen == 1) {
-               scsi_block_requests(ahd->platform_data->host);
-               ahd_platform_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
-                                       CAM_LUN_WILDCARD, SCB_LIST_NULL,
-                                       ROLE_INITIATOR, CAM_REQUEUE_REQ);
-       }
+       scsi_block_requests(ahd->platform_data->host);
 }
 
-void
+static void
 ahd_release_simq(struct ahd_softc *ahd)
 {
-       u_long s;
-       int    unblock_reqs;
-
-       unblock_reqs = 0;
-       ahd_lock(ahd, &s);
-       if (ahd->platform_data->qfrozen > 0)
-               ahd->platform_data->qfrozen--;
-       if (ahd->platform_data->qfrozen == 0) {
-               unblock_reqs = 1;
-       }
-       ahd_unlock(ahd, &s);
-       /*
-        * There is still a race here.  The mid-layer
-        * should keep its own freeze count and use
-        * a bottom half handler to run the queues
-        * so we can unblock with our own lock held.
-        */
-       if (unblock_reqs)
-               scsi_unblock_requests(ahd->platform_data->host);
+       scsi_unblock_requests(ahd->platform_data->host);
 }
 
 static int
-ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
+ahd_linux_queue_abort_cmd(struct scsi_cmnd *cmd)
 {
        struct ahd_softc *ahd;
        struct ahd_linux_device *dev;
@@ -2057,23 +2177,22 @@ ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
        int    wait;
        int    disconnected;
        ahd_mode_state saved_modes;
+       unsigned long flags;
 
        pending_scb = NULL;
        paused = FALSE;
        wait = FALSE;
        ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
 
-       printf("%s:%d:%d:%d: Attempting to queue a%s message:",
-              ahd_name(ahd), cmd->device->channel,
-              cmd->device->id, cmd->device->lun,
-              flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
+       scmd_printk(KERN_INFO, cmd,
+                   "Attempting to queue an ABORT message:");
 
        printf("CDB:");
        for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
                printf(" 0x%x", cmd->cmnd[cdb_byte]);
        printf("\n");
 
-       spin_lock_irq(&ahd->platform_data->spin_lock);
+       ahd_lock(ahd, &flags);
 
        /*
         * First determine if we currently own this command.
@@ -2089,9 +2208,7 @@ ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
                 * No target device for this command exists,
                 * so we must not still own the command.
                 */
-               printf("%s:%d:%d:%d: Is not an active device\n",
-                      ahd_name(ahd), cmd->device->channel, cmd->device->id,
-                      cmd->device->lun);
+               scmd_printk(KERN_INFO, cmd, "Is not an active device\n");
                retval = SUCCESS;
                goto no_cmd;
        }
@@ -2104,22 +2221,8 @@ ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
                        break;
        }
 
-       if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
-
-               /* Any SCB for this device will do for a target reset */
-               LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
-                       if (ahd_match_scb(ahd, pending_scb, cmd->device->id,
-                                         cmd->device->channel + 'A',
-                                         CAM_LUN_WILDCARD,
-                                         SCB_LIST_NULL, ROLE_INITIATOR) == 0)
-                               break;
-               }
-       }
-
        if (pending_scb == NULL) {
-               printf("%s:%d:%d:%d: Command not found\n",
-                      ahd_name(ahd), cmd->device->channel, cmd->device->id,
-                      cmd->device->lun);
+               scmd_printk(KERN_INFO, cmd, "Command not found\n");
                goto no_cmd;
        }
 
@@ -2142,9 +2245,7 @@ ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
        paused = TRUE;
 
        if ((pending_scb->flags & SCB_ACTIVE) == 0) {
-               printf("%s:%d:%d:%d: Command already completed\n",
-                      ahd_name(ahd), cmd->device->channel, cmd->device->id,
-                      cmd->device->lun);
+               scmd_printk(KERN_INFO, cmd, "Command already completed\n");
                goto no_cmd;
        }
 
@@ -2153,25 +2254,17 @@ ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
        ahd_dump_card_state(ahd);
 
        disconnected = TRUE;
-       if (flag == SCB_ABORT) {
-               if (ahd_search_qinfifo(ahd, cmd->device->id, 
-                                      cmd->device->channel + 'A',
-                                      cmd->device->lun, 
-                                      pending_scb->hscb->tag,
-                                      ROLE_INITIATOR, CAM_REQ_ABORTED,
-                                      SEARCH_COMPLETE) > 0) {
-                       printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
-                              ahd_name(ahd), cmd->device->channel, 
-                              cmd->device->id, cmd->device->lun);
-                       retval = SUCCESS;
-                       goto done;
-               }
-       } else if (ahd_search_qinfifo(ahd, cmd->device->id,
-                                     cmd->device->channel + 'A',
-                                     cmd->device->lun, pending_scb->hscb->tag,
-                                     ROLE_INITIATOR, /*status*/0,
-                                     SEARCH_COUNT) > 0) {
-               disconnected = FALSE;
+       if (ahd_search_qinfifo(ahd, cmd->device->id, 
+                              cmd->device->channel + 'A',
+                              cmd->device->lun, 
+                              pending_scb->hscb->tag,
+                              ROLE_INITIATOR, CAM_REQ_ABORTED,
+                              SEARCH_COMPLETE) > 0) {
+               printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
+                      ahd_name(ahd), cmd->device->channel, 
+                      cmd->device->id, cmd->device->lun);
+               retval = SUCCESS;
+               goto done;
        }
 
        saved_modes = ahd_save_modes(ahd);
@@ -2185,10 +2278,6 @@ ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
                bus_scb = ahd_lookup_scb(ahd, active_scbptr);
                if (bus_scb == pending_scb)
                        disconnected = FALSE;
-               else if (flag != SCB_ABORT
-                        && ahd_inb(ahd, SAVED_SCSIID) == pending_scb->hscb->scsiid
-                        && ahd_inb(ahd, SAVED_LUN) == SCB_GET_LUN(pending_scb))
-                       disconnected = FALSE;
        }
 
        /*
@@ -2198,21 +2287,17 @@ ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
         */
        saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
        if (last_phase != P_BUSFREE
-        && (SCB_GET_TAG(pending_scb) == active_scbptr
-            || (flag == SCB_DEVICE_RESET
-                && SCSIID_TARGET(ahd, saved_scsiid) == cmd->device->id))) {
+           && SCB_GET_TAG(pending_scb) == active_scbptr) {
 
                /*
                 * We're active on the bus, so assert ATN
                 * and hope that the target responds.
                 */
                pending_scb = ahd_lookup_scb(ahd, active_scbptr);
-               pending_scb->flags |= SCB_RECOVERY_SCB|flag;
+               pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
                ahd_outb(ahd, MSG_OUT, HOST_MSG);
                ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
-               printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
-                      ahd_name(ahd), cmd->device->channel,
-                      cmd->device->id, cmd->device->lun);
+               scmd_printk(KERN_INFO, cmd, "Device is active, asserting ATN\n");
                wait = TRUE;
        } else if (disconnected) {
 
@@ -2251,7 +2336,7 @@ ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
                        /*
                         * The sequencer will never re-reference the
                         * in-core SCB.  To make sure we are notified
-                        * during reslection, set the MK_MESSAGE flag in
+                        * during reselection, set the MK_MESSAGE flag in
                         * the card's copy of the SCB.
                         */
                        ahd_outb(ahd, SCB_CONTROL,
@@ -2273,9 +2358,7 @@ ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
                printf("Device is disconnected, re-queuing SCB\n");
                wait = TRUE;
        } else {
-               printf("%s:%d:%d:%d: Unable to deliver message\n",
-                      ahd_name(ahd), cmd->device->channel,
-                      cmd->device->id, cmd->device->lun);
+               scmd_printk(KERN_INFO, cmd, "Unable to deliver message\n");
                retval = FAILED;
                goto done;
        }
@@ -2292,42 +2375,29 @@ done:
        if (paused)
                ahd_unpause(ahd);
        if (wait) {
-               struct timer_list timer;
-               int ret;
-
-               ahd->platform_data->flags |= AHD_SCB_UP_EH_SEM;
-               spin_unlock_irq(&ahd->platform_data->spin_lock);
-               init_timer(&timer);
-               timer.data = (u_long)ahd;
-               timer.expires = jiffies + (5 * HZ);
-               timer.function = ahd_linux_sem_timeout;
-               add_timer(&timer);
-               printf("Recovery code sleeping\n");
-               down(&ahd->platform_data->eh_sem);
-               printf("Recovery code awake\n");
-               ret = del_timer_sync(&timer);
-               if (ret == 0) {
-                       printf("Timer Expired\n");
+               DECLARE_COMPLETION_ONSTACK(done);
+
+               ahd->platform_data->eh_done = &done;
+               ahd_unlock(ahd, &flags);
+
+               printf("%s: Recovery code sleeping\n", ahd_name(ahd));
+               if (!wait_for_completion_timeout(&done, 5 * HZ)) {
+                       ahd_lock(ahd, &flags);
+                       ahd->platform_data->eh_done = NULL;
+                       ahd_unlock(ahd, &flags);
+                       printf("%s: Timer Expired (active %d)\n",
+                              ahd_name(ahd), dev->active);
                        retval = FAILED;
                }
-               spin_lock_irq(&ahd->platform_data->spin_lock);
-       }
-       spin_unlock_irq(&ahd->platform_data->spin_lock);
-       return (retval);
-}
+               printf("Recovery code awake\n");
+       } else
+               ahd_unlock(ahd, &flags);
 
-static void ahd_linux_exit(void);
+       if (retval != SUCCESS)
+               printf("%s: Command abort returning 0x%x\n",
+                      ahd_name(ahd), retval);
 
-static void ahd_linux_set_xferflags(struct scsi_target *starget, unsigned int ppr_options, unsigned int period)
-{
-       spi_qas(starget) = (ppr_options & MSG_EXT_PPR_QAS_REQ)? 1 : 0;
-       spi_dt(starget) = (ppr_options & MSG_EXT_PPR_DT_REQ)? 1 : 0;
-       spi_iu(starget) = (ppr_options & MSG_EXT_PPR_IU_REQ) ? 1 : 0;
-       spi_rd_strm(starget) = (ppr_options & MSG_EXT_PPR_RD_STRM) ? 1 : 0;
-       spi_wr_flow(starget) = (ppr_options & MSG_EXT_PPR_WR_FLOW) ? 1 : 0;
-       spi_pcomp_en(starget) = (ppr_options & MSG_EXT_PPR_PCOMP_EN) ? 1 : 0;
-       spi_rti(starget) = (ppr_options & MSG_EXT_PPR_RTI) ? 1 : 0;
-       spi_period(starget) = period;
+       return retval;
 }
 
 static void ahd_linux_set_width(struct scsi_target *starget, int width)
@@ -2369,9 +2439,12 @@ static void ahd_linux_set_period(struct scsi_target *starget, int period)
        if (period < 8)
                period = 8;
        if (period < 10) {
-               ppr_options |= MSG_EXT_PPR_DT_REQ;
-               if (period == 8)
-                       ppr_options |= MSG_EXT_PPR_IU_REQ;
+               if (spi_max_width(starget)) {
+                       ppr_options |= MSG_EXT_PPR_DT_REQ;
+                       if (period == 8)
+                               ppr_options |= MSG_EXT_PPR_IU_REQ;
+               } else
+                       period = 10;
        }
 
        dt = ppr_options & MSG_EXT_PPR_DT_REQ;
@@ -2388,8 +2461,6 @@ static void ahd_linux_set_period(struct scsi_target *starget, int period)
        ahd_find_syncrate(ahd, &period, &ppr_options,
                          dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
 
-       ahd_linux_set_xferflags(starget, ppr_options, period);
-
        ahd_lock(ahd, &flags);
        ahd_set_syncrate(ahd, &devinfo, period, offset,
                         ppr_options, AHD_TRANS_GOAL, FALSE);
@@ -2424,7 +2495,6 @@ static void ahd_linux_set_offset(struct scsi_target *starget, int offset)
                ahd_find_syncrate(ahd, &period, &ppr_options, 
                                  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
        }
-       ahd_linux_set_xferflags(starget, ppr_options, period);
 
        ahd_lock(ahd, &flags);
        ahd_set_syncrate(ahd, &devinfo, period, offset, ppr_options,
@@ -2445,6 +2515,7 @@ static void ahd_linux_set_dt(struct scsi_target *starget, int dt)
        unsigned int ppr_options = tinfo->goal.ppr_options
                & ~MSG_EXT_PPR_DT_REQ;
        unsigned int period = tinfo->goal.period;
+       unsigned int width = tinfo->goal.width;
        unsigned long flags;
 
 #ifdef AHD_DEBUG
@@ -2452,10 +2523,10 @@ static void ahd_linux_set_dt(struct scsi_target *starget, int dt)
                printf("%s: %s DT\n", ahd_name(ahd), 
                       dt ? "enabling" : "disabling");
 #endif
-       if (dt) {
+       if (dt && spi_max_width(starget)) {
                ppr_options |= MSG_EXT_PPR_DT_REQ;
-               if (period > 9)
-                       period = 9; /* at least 12.5ns for DT */
+               if (!width)
+                       ahd_linux_set_width(starget, 1);
        } else {
                if (period <= 9)
                        period = 10; /* If resetting DT, period must be >= 25ns */
@@ -2467,8 +2538,6 @@ static void ahd_linux_set_dt(struct scsi_target *starget, int dt)
        ahd_find_syncrate(ahd, &period, &ppr_options,
                          dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
 
-       ahd_linux_set_xferflags(starget, ppr_options, period);
-
        ahd_lock(ahd, &flags);
        ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
                         ppr_options, AHD_TRANS_GOAL, FALSE);
@@ -2508,8 +2577,6 @@ static void ahd_linux_set_qas(struct scsi_target *starget, int qas)
        ahd_find_syncrate(ahd, &period, &ppr_options,
                          dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
 
-       spi_qas(starget) = (ppr_options & MSG_EXT_PPR_QAS_REQ)? 1 : 0;
-
        ahd_lock(ahd, &flags);
        ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
                         ppr_options, AHD_TRANS_GOAL, FALSE);
@@ -2538,7 +2605,7 @@ static void ahd_linux_set_iu(struct scsi_target *starget, int iu)
                       iu ? "enabling" : "disabling");
 #endif
 
-       if (iu) {
+       if (iu && spi_max_width(starget)) {
                ppr_options |= MSG_EXT_PPR_IU_REQ;
                ppr_options |= MSG_EXT_PPR_DT_REQ; /* IU requires DT */
        }
@@ -2550,8 +2617,6 @@ static void ahd_linux_set_iu(struct scsi_target *starget, int iu)
        ahd_find_syncrate(ahd, &period, &ppr_options,
                          dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
 
-       ahd_linux_set_xferflags(starget, ppr_options, period);
-
        ahd_lock(ahd, &flags);
        ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
                         ppr_options, AHD_TRANS_GOAL, FALSE);
@@ -2580,7 +2645,7 @@ static void ahd_linux_set_rd_strm(struct scsi_target *starget, int rdstrm)
                       rdstrm  ? "enabling" : "disabling");
 #endif
 
-       if (rdstrm)
+       if (rdstrm && spi_max_width(starget))
                ppr_options |= MSG_EXT_PPR_RD_STRM;
 
        ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
@@ -2588,8 +2653,6 @@ static void ahd_linux_set_rd_strm(struct scsi_target *starget, int rdstrm)
        ahd_find_syncrate(ahd, &period, &ppr_options,
                          dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
 
-       spi_rd_strm(starget) = (ppr_options & MSG_EXT_PPR_RD_STRM) ? 1 : 0;
-
        ahd_lock(ahd, &flags);
        ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
                         ppr_options, AHD_TRANS_GOAL, FALSE);
@@ -2618,7 +2681,7 @@ static void ahd_linux_set_wr_flow(struct scsi_target *starget, int wrflow)
                       wrflow ? "enabling" : "disabling");
 #endif
 
-       if (wrflow)
+       if (wrflow && spi_max_width(starget))
                ppr_options |= MSG_EXT_PPR_WR_FLOW;
 
        ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
@@ -2626,8 +2689,6 @@ static void ahd_linux_set_wr_flow(struct scsi_target *starget, int wrflow)
        ahd_find_syncrate(ahd, &period, &ppr_options,
                          dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
 
-       spi_wr_flow(starget) = (ppr_options & MSG_EXT_PPR_WR_FLOW) ? 1 : 0;
-
        ahd_lock(ahd, &flags);
        ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
                         ppr_options, AHD_TRANS_GOAL, FALSE);
@@ -2664,7 +2725,7 @@ static void ahd_linux_set_rti(struct scsi_target *starget, int rti)
                       rti ? "enabling" : "disabling");
 #endif
 
-       if (rti)
+       if (rti && spi_max_width(starget))
                ppr_options |= MSG_EXT_PPR_RTI;
 
        ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
@@ -2672,8 +2733,6 @@ static void ahd_linux_set_rti(struct scsi_target *starget, int rti)
        ahd_find_syncrate(ahd, &period, &ppr_options,
                          dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
 
-       spi_rti(starget) = (ppr_options & MSG_EXT_PPR_RTI) ? 1 : 0;
-
        ahd_lock(ahd, &flags);
        ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
                         ppr_options, AHD_TRANS_GOAL, FALSE);
@@ -2702,15 +2761,57 @@ static void ahd_linux_set_pcomp_en(struct scsi_target *starget, int pcomp)
                       pcomp ? "Enable" : "Disable");
 #endif
 
-       if (pcomp)
+       if (pcomp && spi_max_width(starget)) {
+               uint8_t precomp;
+
+               if (ahd->unit < ARRAY_SIZE(aic79xx_iocell_info)) {
+                       const struct ahd_linux_iocell_opts *iocell_opts;
+
+                       iocell_opts = &aic79xx_iocell_info[ahd->unit];
+                       precomp = iocell_opts->precomp;
+               } else {
+                       precomp = AIC79XX_DEFAULT_PRECOMP;
+               }
                ppr_options |= MSG_EXT_PPR_PCOMP_EN;
+               AHD_SET_PRECOMP(ahd, precomp);
+       } else {
+               AHD_SET_PRECOMP(ahd, 0);
+       }
 
        ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
                            starget->channel + 'A', ROLE_INITIATOR);
        ahd_find_syncrate(ahd, &period, &ppr_options,
                          dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
 
-       spi_pcomp_en(starget) = (ppr_options & MSG_EXT_PPR_PCOMP_EN) ? 1 : 0;
+       ahd_lock(ahd, &flags);
+       ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
+                        ppr_options, AHD_TRANS_GOAL, FALSE);
+       ahd_unlock(ahd, &flags);
+}
+
+static void ahd_linux_set_hold_mcs(struct scsi_target *starget, int hold)
+{
+       struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
+       struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
+       struct ahd_tmode_tstate *tstate;
+       struct ahd_initiator_tinfo *tinfo 
+               = ahd_fetch_transinfo(ahd,
+                                     starget->channel + 'A',
+                                     shost->this_id, starget->id, &tstate);
+       struct ahd_devinfo devinfo;
+       unsigned int ppr_options = tinfo->goal.ppr_options
+               & ~MSG_EXT_PPR_HOLD_MCS;
+       unsigned int period = tinfo->goal.period;
+       unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
+       unsigned long flags;
+
+       if (hold && spi_max_width(starget))
+               ppr_options |= MSG_EXT_PPR_HOLD_MCS;
+
+       ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
+                           starget->channel + 'A', ROLE_INITIATOR);
+       ahd_find_syncrate(ahd, &period, &ppr_options,
+                         dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
 
        ahd_lock(ahd, &flags);
        ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
@@ -2718,6 +2819,26 @@ static void ahd_linux_set_pcomp_en(struct scsi_target *starget, int pcomp)
        ahd_unlock(ahd, &flags);
 }
 
+static void ahd_linux_get_signalling(struct Scsi_Host *shost)
+{
+       struct ahd_softc *ahd = *(struct ahd_softc **)shost->hostdata;
+       unsigned long flags;
+       u8 mode;
+
+       ahd_lock(ahd, &flags);
+       ahd_pause(ahd);
+       mode = ahd_inb(ahd, SBLKCTL);
+       ahd_unpause(ahd);
+       ahd_unlock(ahd, &flags);
+
+       if (mode & ENAB40)
+               spi_signalling(shost) = SPI_SIGNAL_LVD;
+       else if (mode & ENAB20)
+               spi_signalling(shost) = SPI_SIGNAL_SE;
+       else
+               spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
+}
+
 static struct spi_function_template ahd_linux_transport_functions = {
        .set_offset     = ahd_linux_set_offset,
        .show_offset    = 1,
@@ -2739,25 +2860,34 @@ static struct spi_function_template ahd_linux_transport_functions = {
        .show_rti       = 1,
        .set_pcomp_en   = ahd_linux_set_pcomp_en,
        .show_pcomp_en  = 1,
+       .set_hold_mcs   = ahd_linux_set_hold_mcs,
+       .show_hold_mcs  = 1,
+       .get_signalling = ahd_linux_get_signalling,
 };
 
-
-
 static int __init
 ahd_linux_init(void)
 {
-       ahd_linux_transport_template = spi_attach_transport(&ahd_linux_transport_functions);
+       int     error = 0;
+
+       /*
+        * If we've been passed any parameters, process them now.
+        */
+       if (aic79xx)
+               aic79xx_setup(aic79xx);
+
+       ahd_linux_transport_template =
+               spi_attach_transport(&ahd_linux_transport_functions);
        if (!ahd_linux_transport_template)
                return -ENODEV;
-       scsi_transport_reserve_target(ahd_linux_transport_template,
-                                     sizeof(struct ahd_linux_target));
+
        scsi_transport_reserve_device(ahd_linux_transport_template,
                                      sizeof(struct ahd_linux_device));
-       if (ahd_linux_detect(&aic79xx_driver_template) > 0)
-               return 0;
-       spi_release_transport(ahd_linux_transport_template);
-       ahd_linux_exit();
-       return -ENODEV;
+
+       error = ahd_linux_pci_init();
+       if (error)
+               spi_release_transport(ahd_linux_transport_template);
+       return error;
 }
 
 static void __exit