scsi: fix makefile for aic7(3*x)
[safe/jmp/linux-2.6] / drivers / scsi / aic7xxx / aic7xxx_osm.c
index 55e0b28..282aff6 100644 (file)
 
 static struct scsi_transport_template *ahc_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 ahc softc list.
- */
-spinlock_t ahc_list_spinlock;
 
 /*
  * Set this to the delay in seconds after SCSI bus reset.
@@ -292,25 +282,6 @@ ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
 static uint32_t aic7xxx_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 aic7xxx_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
@@ -338,13 +309,6 @@ static uint32_t aic7xxx_pci_parity = ~0;
 uint32_t aic7xxx_allow_memio = ~0;
 
 /*
- * aic7xxx_detect() has been run, so register all device arrivals
- * immediately with the system rather than deferring to the sorted
- * attachment performed by aic7xxx_detect().
- */
-int aic7xxx_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
@@ -364,32 +328,31 @@ static uint32_t aic7xxx_seltime;
  * force all outstanding transactions to be serviced prior to a new
  * transaction.
  */
-uint32_t aic7xxx_periodic_otag;
+static uint32_t aic7xxx_periodic_otag;
 
 /*
  * Module information and settable options.
  */
 static char *aic7xxx = NULL;
 
-MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
-MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
+MODULE_AUTHOR("Maintainer: Hannes Reinecke <hare@suse.de>");
+MODULE_DESCRIPTION("Adaptec AIC77XX/78XX SCSI Host Bus Adapter driver");
 MODULE_LICENSE("Dual BSD/GPL");
 MODULE_VERSION(AIC7XXX_DRIVER_VERSION);
 module_param(aic7xxx, charp, 0444);
 MODULE_PARM_DESC(aic7xxx,
-"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_probe                Toggle EISA/VLB controller probing\n"
 "      probe_eisa_vl           Toggle EISA/VLB controller probing\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"
 "                              drives or 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 every target\n"
 "                              on every bus\n"
@@ -409,9 +372,8 @@ static void ahc_linux_handle_scsi_status(struct ahc_softc *,
                                         struct scb *);
 static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
                                         struct scsi_cmnd *cmd);
-static void ahc_linux_sem_timeout(u_long arg);
 static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
-static void ahc_linux_release_simq(u_long arg);
+static void ahc_linux_release_simq(struct ahc_softc *ahc);
 static int  ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
 static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
 static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
@@ -421,9 +383,10 @@ static int ahc_linux_run_command(struct ahc_softc*,
                                 struct ahc_linux_device *,
                                 struct scsi_cmnd *);
 static void ahc_linux_setup_tag_info_global(char *p);
-static aic_option_callback_t ahc_linux_setup_tag_info;
 static int  aic7xxx_setup(char *s);
-static int  ahc_linux_next_unit(void);
+
+static int ahc_linux_unit;
+
 
 /********************************* Inlines ************************************/
 static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
@@ -439,18 +402,8 @@ ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
 
        cmd = scb->io_ctx;
        ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
-       if (cmd->use_sg != 0) {
-               struct scatterlist *sg;
-
-               sg = (struct scatterlist *)cmd->request_buffer;
-               pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg,
-                            cmd->sc_data_direction);
-       } else if (cmd->request_bufflen != 0) {
-               pci_unmap_single(ahc->dev_softc,
-                                scb->platform_data->buf_busaddr,
-                                cmd->request_bufflen,
-                                cmd->sc_data_direction);
-       }
+
+       scsi_dma_unmap(cmd);
 }
 
 static __inline int
@@ -476,48 +429,6 @@ ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
 }
 
 /*
- * Try to detect an Adaptec 7XXX controller.
- */
-static int
-ahc_linux_detect(struct scsi_host_template *template)
-{
-       struct  ahc_softc *ahc;
-       int     found = 0;
-
-       /*
-        * If we've been passed any parameters, process them now.
-        */
-       if (aic7xxx)
-               aic7xxx_setup(aic7xxx);
-
-       template->proc_name = "aic7xxx";
-
-       /*
-        * Initialize our softc list lock prior to
-        * probing for any adapters.
-        */
-       ahc_list_lockinit();
-
-       found = ahc_linux_pci_init();
-       if (!ahc_linux_eisa_init())
-               found++;
-       
-       /*
-        * Register with the SCSI layer all
-        * controllers we've found.
-        */
-       TAILQ_FOREACH(ahc, &ahc_tailq, links) {
-
-               if (ahc_linux_register_host(ahc, template) == 0)
-                       found++;
-       }
-
-       aic7xxx_detect_complete++;
-
-       return (found);
-}
-
-/*
  * Return a string describing the driver.
  */
 static const char *
@@ -553,26 +464,20 @@ ahc_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
 {
        struct   ahc_softc *ahc;
        struct   ahc_linux_device *dev = scsi_transport_device_data(cmd->device);
+       int rtn = SCSI_MLQUEUE_HOST_BUSY;
+       unsigned long flags;
 
        ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
 
-       /*
-        * Save the callback on completion function.
-        */
-       cmd->scsi_done = scsi_done;
-
-       /*
-        * 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 (ahc->platform_data->qfrozen != 0)
-               return SCSI_MLQUEUE_HOST_BUSY;
-
-       cmd->result = CAM_REQ_INPROG << 16;
+       ahc_lock(ahc, &flags);
+       if (ahc->platform_data->qfrozen == 0) {
+               cmd->scsi_done = scsi_done;
+               cmd->result = CAM_REQ_INPROG << 16;
+               rtn = ahc_linux_run_command(ahc, dev, cmd);
+       }
+       ahc_unlock(ahc, &flags);
 
-       return ahc_linux_run_command(ahc, dev, cmd);
+       return rtn;
 }
 
 static inline struct scsi_target **
@@ -597,7 +502,6 @@ ahc_linux_target_alloc(struct scsi_target *starget)
        struct seeprom_config *sc = ahc->seep_config;
        unsigned long flags;
        struct scsi_target **ahc_targp = ahc_linux_target_in_softc(starget);
-       struct ahc_linux_target *targ = scsi_transport_target_data(starget);
        unsigned short scsirate;
        struct ahc_devinfo devinfo;
        struct ahc_initiator_tinfo *tinfo;
@@ -618,23 +522,37 @@ ahc_linux_target_alloc(struct scsi_target *starget)
        BUG_ON(*ahc_targp != NULL);
 
        *ahc_targp = starget;
-       memset(targ, 0, sizeof(*targ));
 
        if (sc) {
+               int maxsync = AHC_SYNCRATE_DT;
+               int ultra = 0;
+               int flags = sc->device_flags[target_offset];
+
+               if (ahc->flags & AHC_NEWEEPROM_FMT) {
+                   if (flags & CFSYNCHISULTRA)
+                       ultra = 1;
+               } else if (flags & CFULTRAEN)
+                       ultra = 1;
+               /* AIC nutcase; 10MHz appears as ultra = 1, CFXFER = 0x04
+                * change it to ultra=0, CFXFER = 0 */
+               if(ultra && (flags & CFXFER) == 0x04) {
+                       ultra = 0;
+                       flags &= ~CFXFER;
+               }
+           
                if ((ahc->features & AHC_ULTRA2) != 0) {
-                       scsirate = sc->device_flags[target_offset] & CFXFER;
+                       scsirate = (flags & CFXFER) | (ultra ? 0x8 : 0);
                } else {
-                       scsirate = (sc->device_flags[target_offset] & CFXFER) << 4;
-                       if (sc->device_flags[target_offset] & CFSYNCH)
-                               scsirate |= SOFS;
+                       scsirate = (flags & CFXFER) << 4;
+                       maxsync = ultra ? AHC_SYNCRATE_ULTRA : 
+                               AHC_SYNCRATE_FAST;
                }
-               if (sc->device_flags[target_offset] & CFWIDEB) {
-                       scsirate |= WIDEXFER;
-                       spi_max_width(starget) = 1;
-               } else
-                       spi_max_width(starget) = 0;
+               spi_max_width(starget) = (flags & CFWIDEB) ? 1 : 0;
+               if (!(flags & CFSYNCH))
+                       spi_max_offset(starget) = 0;
                spi_min_period(starget) = 
-                       ahc_find_period(ahc, scsirate, AHC_SYNCRATE_DT);
+                       ahc_find_period(ahc, scsirate, maxsync);
+
                tinfo = ahc_fetch_transinfo(ahc, channel, ahc->our_id,
                                            starget->id, &tstate);
        }
@@ -664,14 +582,11 @@ ahc_linux_slave_alloc(struct scsi_device *sdev)
        struct  ahc_softc *ahc =
                *((struct ahc_softc **)sdev->host->hostdata);
        struct scsi_target *starget = sdev->sdev_target;
-       struct ahc_linux_target *targ = scsi_transport_target_data(starget);
        struct ahc_linux_device *dev;
 
        if (bootverbose)
                printf("%s: Slave Alloc %d\n", ahc_name(ahc), sdev->id);
 
-       BUG_ON(targ->sdev[sdev->lun] != NULL);
-
        dev = scsi_transport_device_data(sdev);
        memset(dev, 0, sizeof(*dev));
 
@@ -688,7 +603,7 @@ ahc_linux_slave_alloc(struct scsi_device *sdev)
         */
        dev->maxtags = 0;
        
-       targ->sdev[sdev->lun] = sdev;
+       spi_period(starget) = 0;
 
        return 0;
 }
@@ -701,7 +616,7 @@ ahc_linux_slave_configure(struct scsi_device *sdev)
        ahc = *((struct ahc_softc **)sdev->host->hostdata);
 
        if (bootverbose)
-               printf("%s: Slave Configure %d\n", ahc_name(ahc), sdev->id);
+               sdev_printk(KERN_INFO, sdev, "Slave Configure\n");
 
        ahc_linux_device_queue_depth(sdev);
 
@@ -712,22 +627,6 @@ ahc_linux_slave_configure(struct scsi_device *sdev)
        return 0;
 }
 
-static void
-ahc_linux_slave_destroy(struct scsi_device *sdev)
-{
-       struct  ahc_softc *ahc;
-       struct  ahc_linux_device *dev = scsi_transport_device_data(sdev);
-       struct  ahc_linux_target *targ = scsi_transport_target_data(sdev->sdev_target);
-
-       ahc = *((struct ahc_softc **)sdev->host->hostdata);
-       if (bootverbose)
-               printf("%s: Slave Destroy %d\n", ahc_name(ahc), sdev->id);
-
-       BUG_ON(dev->active);
-
-       targ->sdev[sdev->lun] = NULL;
-}
-
 #if defined(__i386__)
 /*
  * Return the disk geometry for the given SCSI device.
@@ -746,7 +645,7 @@ ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
        u_int    channel;
 
        ahc = *((struct ahc_softc **)sdev->host->hostdata);
-       channel = sdev->channel;
+       channel = sdev_channel(sdev);
 
        bh = scsi_bios_ptable(bdev);
        if (bh) {
@@ -814,10 +713,14 @@ ahc_linux_bus_reset(struct scsi_cmnd *cmd)
 {
        struct ahc_softc *ahc;
        int    found;
+       unsigned long flags;
 
        ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
-       found = ahc_reset_channel(ahc, cmd->device->channel + 'A',
+
+       ahc_lock(ahc, &flags);
+       found = ahc_reset_channel(ahc, scmd_channel(cmd) + 'A',
                                  /*initiate reset*/TRUE);
+       ahc_unlock(ahc, &flags);
 
        if (bootverbose)
                printf("%s: SCSI bus reset delivered. "
@@ -829,6 +732,7 @@ ahc_linux_bus_reset(struct scsi_cmnd *cmd)
 struct scsi_host_template aic7xxx_driver_template = {
        .module                 = THIS_MODULE,
        .name                   = "aic7xxx",
+       .proc_name              = "aic7xxx",
        .proc_info              = ahc_linux_proc_info,
        .info                   = ahc_linux_info,
        .queuecommand           = ahc_linux_queue,
@@ -840,11 +744,11 @@ struct scsi_host_template aic7xxx_driver_template = {
 #endif
        .can_queue              = AHC_MAX_QUEUE,
        .this_id                = -1,
+       .max_sectors            = 8192,
        .cmd_per_lun            = 2,
        .use_clustering         = ENABLE_CLUSTERING,
        .slave_alloc            = ahc_linux_slave_alloc,
        .slave_configure        = ahc_linux_slave_configure,
-       .slave_destroy          = ahc_linux_slave_destroy,
        .target_alloc           = ahc_linux_target_alloc,
        .target_destroy         = ahc_linux_target_destroy,
 };
@@ -940,99 +844,6 @@ ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
        return (0);
 }
 
-/********************* Platform Dependent Functions ***************************/
-/*
- * Compare "left hand" softc with "right hand" softc, returning:
- * < 0 - lahc has a lower priority than rahc
- *   0 - Softcs are equal
- * > 0 - lahc has a higher priority than rahc
- */
-int
-ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
-{
-       int     value;
-       int     rvalue;
-       int     lvalue;
-
-       /*
-        * Under Linux, cards are ordered as follows:
-        *      1) VLB/EISA BIOS enabled devices sorted by BIOS address.
-        *      2) PCI devices with BIOS enabled sorted by bus/slot/func.
-        *      3) All remaining VLB/EISA devices sorted by ioport.
-        *      4) All remaining PCI devices sorted by bus/slot/func.
-        */
-       value = (lahc->flags & AHC_BIOS_ENABLED)
-             - (rahc->flags & AHC_BIOS_ENABLED);
-       if (value != 0)
-               /* Controllers with BIOS enabled have a *higher* priority */
-               return (value);
-
-       /*
-        * Same BIOS setting, now sort based on bus type.
-        * EISA and VL controllers sort together.  EISA/VL
-        * have higher priority than PCI.
-        */
-       rvalue = (rahc->chip & AHC_BUS_MASK);
-       if (rvalue == AHC_VL)
-               rvalue = AHC_EISA;
-       lvalue = (lahc->chip & AHC_BUS_MASK);
-       if (lvalue == AHC_VL)
-               lvalue = AHC_EISA;
-       value = rvalue - lvalue;
-       if (value != 0)
-               return (value);
-
-       /* Still equal.  Sort by BIOS address, ioport, or bus/slot/func. */
-       switch (rvalue) {
-#ifdef CONFIG_PCI
-       case AHC_PCI:
-       {
-               char primary_channel;
-
-               if (aic7xxx_reverse_scan != 0)
-                       value = ahc_get_pci_bus(lahc->dev_softc)
-                             - ahc_get_pci_bus(rahc->dev_softc);
-               else
-                       value = ahc_get_pci_bus(rahc->dev_softc)
-                             - ahc_get_pci_bus(lahc->dev_softc);
-               if (value != 0)
-                       break;
-               if (aic7xxx_reverse_scan != 0)
-                       value = ahc_get_pci_slot(lahc->dev_softc)
-                             - ahc_get_pci_slot(rahc->dev_softc);
-               else
-                       value = ahc_get_pci_slot(rahc->dev_softc)
-                             - ahc_get_pci_slot(lahc->dev_softc);
-               if (value != 0)
-                       break;
-               /*
-                * On multi-function devices, the user can choose
-                * to have function 1 probed before function 0.
-                * Give whichever channel is the primary channel
-                * the highest priority.
-                */
-               primary_channel = (lahc->flags & AHC_PRIMARY_CHANNEL) + 'A';
-               value = -1;
-               if (lahc->channel == primary_channel)
-                       value = 1;
-               break;
-       }
-#endif
-       case AHC_EISA:
-               if ((rahc->flags & AHC_BIOS_ENABLED) != 0) {
-                       value = rahc->platform_data->bios_address
-                             - lahc->platform_data->bios_address; 
-               } else {
-                       value = rahc->bsh.ioport
-                             - lahc->bsh.ioport; 
-               }
-               break;
-       default:
-               panic("ahc_softc_sort: invalid bus type");
-       }
-       return (value);
-}
-
 static void
 ahc_linux_setup_tag_info_global(char *p)
 {
@@ -1041,7 +852,7 @@ ahc_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(aic7xxx_tag_info); i++) {
+       for (i = 0; i < ARRAY_SIZE(aic7xxx_tag_info); i++) {
                for (j = 0; j < AHC_NUM_TARGETS; j++) {
                        aic7xxx_tag_info[i].tag_commands[j] = tags;
                }
@@ -1053,7 +864,7 @@ ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
 {
 
        if ((instance >= 0) && (targ >= 0)
-        && (instance < NUM_ELEMENTS(aic7xxx_tag_info))
+        && (instance < ARRAY_SIZE(aic7xxx_tag_info))
         && (targ < AHC_NUM_TARGETS)) {
                aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff;
                if (bootverbose)
@@ -1061,6 +872,86 @@ ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
        }
 }
 
+static char *
+ahc_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.
@@ -1084,7 +975,6 @@ aic7xxx_setup(char *s)
 #ifdef AHC_DEBUG
                { "debug", &ahc_debug },
 #endif
-               { "reverse_scan", &aic7xxx_reverse_scan },
                { "periodic_otag", &aic7xxx_periodic_otag },
                { "pci_parity", &aic7xxx_pci_parity },
                { "seltime", &aic7xxx_seltime },
@@ -1096,7 +986,7 @@ aic7xxx_setup(char *s)
        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;
@@ -1104,19 +994,19 @@ aic7xxx_setup(char *s)
        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) {
                        ahc_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 = ahc_parse_brace_option("tag_info", p + n, end,
                            2, ahc_linux_setup_tag_info, 0);
                } else if (p[n] == ':') {
                        *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
@@ -1136,10 +1026,11 @@ uint32_t aic7xxx_verbose;
 int
 ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *template)
 {
-       char     buf[80];
-       struct   Scsi_Host *host;
+       char    buf[80];
+       struct  Scsi_Host *host;
        char    *new_name;
-       u_long   s;
+       u_long  s;
+       int     retval;
 
        template->name = ahc->description;
        host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
@@ -1147,8 +1038,6 @@ ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *templa
                return (ENOMEM);
 
        *((struct ahc_softc **)host->hostdata) = ahc;
-       ahc_lock(ahc, &s);
-       scsi_assign_lock(host, &ahc->platform_data->spin_lock);
        ahc->platform_data->host = host;
        host->can_queue = AHC_MAX_QUEUE;
        host->cmd_per_lun = 2;
@@ -1159,7 +1048,9 @@ ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *templa
        host->max_lun = AHC_NUM_LUNS;
        host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
        host->sg_tablesize = AHC_NSEG;
-       ahc_set_unit(ahc, ahc_linux_next_unit());
+       ahc_lock(ahc, &s);
+       ahc_set_unit(ahc, ahc_linux_unit++);
+       ahc_unlock(ahc, &s);
        sprintf(buf, "scsi%d", host->host_no);
        new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
        if (new_name != NULL) {
@@ -1169,45 +1060,19 @@ ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *templa
        host->unique_id = ahc->unit;
        ahc_linux_initialize_scsi_bus(ahc);
        ahc_intr_enable(ahc, TRUE);
-       ahc_unlock(ahc, &s);
 
        host->transportt = ahc_linux_transport_template;
 
-       scsi_add_host(host, (ahc->dev_softc ? &ahc->dev_softc->dev : NULL)); /* XXX handle failure */
-       scsi_scan_host(host);
-       return (0);
-}
-
-uint64_t
-ahc_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
-ahc_linux_next_unit(void)
-{
-       struct ahc_softc *ahc;
-       int unit;
-
-       unit = 0;
-retry:
-       TAILQ_FOREACH(ahc, &ahc_tailq, links) {
-               if (ahc->unit == unit) {
-                       unit++;
-                       goto retry;
-               }
+       retval = scsi_add_host(host,
+                       (ahc->dev_softc ? &ahc->dev_softc->dev : NULL));
+       if (retval) {
+               printk(KERN_WARNING "aic7xxx: scsi_add_host failed\n");
+               scsi_host_put(host);
+               return retval;
        }
-       return (unit);
+
+       scsi_scan_host(host);
+       return 0;
 }
 
 /*
@@ -1220,10 +1085,13 @@ ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
 {
        int i;
        int numtarg;
+       unsigned long s;
 
        i = 0;
        numtarg = 0;
 
+       ahc_lock(ahc, &s);
+
        if (aic7xxx_no_reset != 0)
                ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
 
@@ -1270,16 +1138,12 @@ ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
                ahc_update_neg_request(ahc, &devinfo, tstate,
                                       tinfo, AHC_NEG_ALWAYS);
        }
+       ahc_unlock(ahc, &s);
        /* Give the bus some time to recover */
        if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
                ahc_linux_freeze_simq(ahc);
-               init_timer(&ahc->platform_data->reset_timer);
-               ahc->platform_data->reset_timer.data = (u_long)ahc;
-               ahc->platform_data->reset_timer.expires =
-                   jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000;
-               ahc->platform_data->reset_timer.function =
-                   ahc_linux_release_simq;
-               add_timer(&ahc->platform_data->reset_timer);
+               msleep(AIC7XXX_RESET_DELAY);
+               ahc_linux_release_simq(ahc);
        }
 }
 
@@ -1294,7 +1158,6 @@ ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
        memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
        ahc->platform_data->irq = AHC_LINUX_NOIRQ;
        ahc_lockinit(ahc);
-       init_MUTEX_LOCKED(&ahc->platform_data->eh_sem);
        ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
        ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
        if (aic7xxx_pci_parity == 0)
@@ -1307,26 +1170,13 @@ void
 ahc_platform_free(struct ahc_softc *ahc)
 {
        struct scsi_target *starget;
-       int i, j;
+       int i;
 
        if (ahc->platform_data != NULL) {
-               if (ahc->platform_data->host != NULL) {
-                       scsi_remove_host(ahc->platform_data->host);
-                       scsi_host_put(ahc->platform_data->host);
-               }
-
                /* destroy all of the device and target objects */
                for (i = 0; i < AHC_NUM_TARGETS; i++) {
                        starget = ahc->platform_data->starget[i];
                        if (starget != NULL) {
-                               for (j = 0; j < AHC_NUM_LUNS; j++) {
-                                       struct ahc_linux_target *targ =
-                                               scsi_transport_target_data(starget);
-
-                                       if (targ->sdev[j] == NULL)
-                                               continue;
-                                       targ->sdev[j] = NULL;
-                               }
                                ahc->platform_data->starget[i] = NULL;
                        }
                }
@@ -1343,6 +1193,9 @@ ahc_platform_free(struct ahc_softc *ahc)
                                           0x1000);
                }
 
+               if (ahc->platform_data->host)
+                       scsi_host_put(ahc->platform_data->host);
+
                free(ahc->platform_data, M_DEVBUF);
        }
 }
@@ -1357,24 +1210,13 @@ ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
 }
 
 void
-ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
-                     ahc_queue_alg alg)
+ahc_platform_set_tags(struct ahc_softc *ahc, struct scsi_device *sdev,
+                     struct ahc_devinfo *devinfo, ahc_queue_alg alg)
 {
-       struct scsi_target *starget;
-       struct ahc_linux_target *targ;
        struct ahc_linux_device *dev;
-       struct scsi_device *sdev;
-       u_int target_offset;
        int was_queuing;
        int now_queuing;
 
-       target_offset = devinfo->target;
-       if (devinfo->channel != 'A')
-               target_offset += 8;
-       starget = ahc->platform_data->starget[target_offset];
-       targ = scsi_transport_target_data(starget);
-       BUG_ON(targ == NULL);
-       sdev = targ->sdev[devinfo->lun];
        if (sdev == NULL)
                return;
        dev = scsi_transport_device_data(sdev);
@@ -1431,14 +1273,12 @@ ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
        }
        switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
        case AHC_DEV_Q_BASIC:
-               scsi_adjust_queue_depth(sdev,
-                                       MSG_SIMPLE_TASK,
-                                       dev->openings + dev->active);
+               scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
+               scsi_activate_tcq(sdev, dev->openings + dev->active);
                break;
        case AHC_DEV_Q_TAGGED:
-               scsi_adjust_queue_depth(sdev,
-                                       MSG_ORDERED_TASK,
-                                       dev->openings + dev->active);
+               scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
+               scsi_activate_tcq(sdev, dev->openings + dev->active);
                break;
        default:
                /*
@@ -1447,9 +1287,7 @@ ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_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, 2);
                break;
        }
 }
@@ -1469,7 +1307,7 @@ ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
 
        tags = 0;
        if ((ahc->user_discenable & devinfo->target_mask) != 0) {
-               if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
+               if (ahc->unit >= ARRAY_SIZE(aic7xxx_tag_info)) {
                        if (warned_user == 0) {
 
                                printf(KERN_WARNING
@@ -1511,11 +1349,15 @@ ahc_linux_device_queue_depth(struct scsi_device *sdev)
        tags = ahc_linux_user_tagdepth(ahc, &devinfo);
        if (tags != 0 && sdev->tagged_supported != 0) {
 
-               ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
+               ahc_platform_set_tags(ahc, sdev, &devinfo, AHC_QUEUE_TAGGED);
+               ahc_send_async(ahc, devinfo.channel, devinfo.target,
+                              devinfo.lun, AC_TRANSFER_NEG);
                ahc_print_devinfo(ahc, &devinfo);
                printf("Tagged Queuing enabled.  Depth %d\n", tags);
        } else {
-               ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE);
+               ahc_platform_set_tags(ahc, sdev, &devinfo, AHC_QUEUE_NONE);
+               ahc_send_async(ahc, devinfo.channel, devinfo.target,
+                              devinfo.lun, AC_TRANSFER_NEG);
        }
 }
 
@@ -1529,6 +1371,7 @@ ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
        struct   ahc_tmode_tstate *tstate;
        uint16_t mask;
        struct scb_tailq *untagged_q = NULL;
+       int nseg;
 
        /*
         * Schedule us to run later.  The only reason we are not
@@ -1620,23 +1463,21 @@ ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
        ahc_set_residual(scb, 0);
        ahc_set_sense_residual(scb, 0);
        scb->sg_count = 0;
-       if (cmd->use_sg != 0) {
+
+       nseg = scsi_dma_map(cmd);
+       BUG_ON(nseg < 0);
+       if (nseg > 0) {
                struct  ahc_dma_seg *sg;
                struct  scatterlist *cur_seg;
-               struct  scatterlist *end_seg;
-               int     nseg;
+               int i;
 
-               cur_seg = (struct scatterlist *)cmd->request_buffer;
-               nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
-                                 cmd->sc_data_direction);
-               end_seg = cur_seg + nseg;
                /* Copy the segments into the SG list. */
                sg = scb->sg_list;
                /*
                 * The sg_count may be larger than nseg if
                 * a transfer crosses a 32bit page.
-                */ 
-               while (cur_seg < end_seg) {
+                */
+               scsi_for_each_sg(cmd, cur_seg, nseg, i) {
                        dma_addr_t addr;
                        bus_size_t len;
                        int consumed;
@@ -1647,7 +1488,6 @@ ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
                                                     sg, addr, len);
                        sg += consumed;
                        scb->sg_count += consumed;
-                       cur_seg++;
                }
                sg--;
                sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
@@ -1664,33 +1504,6 @@ ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
                 */
                scb->hscb->dataptr = scb->sg_list->addr;
                scb->hscb->datacnt = scb->sg_list->len;
-       } else if (cmd->request_bufflen != 0) {
-               struct   ahc_dma_seg *sg;
-               dma_addr_t addr;
-
-               sg = scb->sg_list;
-               addr = pci_map_single(ahc->dev_softc,
-                                     cmd->request_buffer,
-                                     cmd->request_bufflen,
-                                     cmd->sc_data_direction);
-               scb->platform_data->buf_busaddr = addr;
-               scb->sg_count = ahc_linux_map_seg(ahc, scb,
-                                                 sg, addr,
-                                                 cmd->request_bufflen);
-               sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
-
-               /*
-                * Reset the sg list pointer.
-                */
-               scb->hscb->sgptr =
-                       ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
-
-               /*
-                * Copy the first SG into the "current"
-                * data pointer area.
-                */
-               scb->hscb->dataptr = sg->addr;
-               scb->hscb->datacnt = sg->len;
        } else {
                scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
                scb->hscb->dataptr = 0;
@@ -1718,7 +1531,7 @@ ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
  * SCSI controller interrupt handler.
  */
 irqreturn_t
-ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
+ahc_linux_isr(int irq, void *dev_id)
 {
        struct  ahc_softc *ahc;
        u_long  flags;
@@ -1739,7 +1552,7 @@ ahc_platform_flushwork(struct ahc_softc *ahc)
 
 void
 ahc_send_async(struct ahc_softc *ahc, 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:
@@ -1783,9 +1596,9 @@ ahc_send_async(struct ahc_softc *ahc, char channel,
                if (channel == 'B')
                        target_offset += 8;
                starget = ahc->platform_data->starget[target_offset];
-               targ = scsi_transport_target_data(starget);
-               if (targ == NULL)
+               if (starget == NULL)
                        break;
+               targ = scsi_transport_target_data(starget);
 
                target_ppr_options =
                        (spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
@@ -1802,9 +1615,9 @@ ahc_send_async(struct ahc_softc *ahc, char channel,
                spi_period(starget) = tinfo->curr.period;
                spi_width(starget) = tinfo->curr.width;
                spi_offset(starget) = tinfo->curr.offset;
-               spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ;
-               spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ;
-               spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ;
+               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_display_xfer_agreement(starget);
                break;
        }
@@ -1844,9 +1657,12 @@ ahc_done(struct ahc_softc *ahc, struct scb *scb)
                untagged_q = &(ahc->untagged_queues[target_offset]);
                TAILQ_REMOVE(untagged_q, scb, links.tqe);
                BUG_ON(!TAILQ_EMPTY(untagged_q));
-       }
-
-       if ((scb->flags & SCB_ACTIVE) == 0) {
+       } else if ((scb->flags & SCB_ACTIVE) == 0) {
+               /*
+                * Transactions aborted from the untagged queue may
+                * not have been dispatched to the controller, so
+                * only check the SCB_ACTIVE flag for tagged transactions.
+                */
                printf("SCB %d done'd twice\n", scb->hscb->tag);
                ahc_dump_card_state(ahc);
                panic("Stopping for safety");
@@ -1937,10 +1753,9 @@ ahc_done(struct ahc_softc *ahc, struct scb *scb)
                if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
                 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
                        ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
-               if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
-                       ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
-                       up(&ahc->platform_data->eh_sem);
-               }
+
+               if (ahc->platform_data->eh_done)
+                       complete(ahc->platform_data->eh_done);
        }
 
        ahc_free_scb(ahc, scb);
@@ -1986,14 +1801,14 @@ ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
                if (scb->flags & SCB_SENSE) {
                        u_int sense_size;
 
-                       sense_size = MIN(sizeof(struct scsi_sense_data)
+                       sense_size = min(sizeof(struct scsi_sense_data)
                                       - ahc_get_sense_residual(scb),
-                                        sizeof(cmd->sense_buffer));
+                                        (u_long)SCSI_SENSE_BUFFERSIZE);
                        memcpy(cmd->sense_buffer,
                               ahc_get_sense_buf(ahc, scb), sense_size);
-                       if (sense_size < sizeof(cmd->sense_buffer))
+                       if (sense_size < SCSI_SENSE_BUFFERSIZE)
                                memset(&cmd->sense_buffer[sense_size], 0,
-                                      sizeof(cmd->sense_buffer) - sense_size);
+                                      SCSI_SENSE_BUFFERSIZE - sense_size);
                        cmd->result |= (DRIVER_SENSE << 24);
 #ifdef AHC_DEBUG
                        if (ahc_debug & AHC_SHOW_SENSE) {
@@ -2057,7 +1872,7 @@ ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
                        }
                        ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
                        ahc_set_scsi_status(scb, SCSI_STATUS_OK);
-                       ahc_platform_set_tags(ahc, &devinfo,
+                       ahc_platform_set_tags(ahc, sdev, &devinfo,
                                     (dev->flags & AHC_DEV_Q_BASIC)
                                   ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
                        break;
@@ -2068,7 +1883,7 @@ ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
                 */
                dev->openings = 1;
                ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
-               ahc_platform_set_tags(ahc, &devinfo,
+               ahc_platform_set_tags(ahc, sdev, &devinfo,
                             (dev->flags & AHC_DEV_Q_BASIC)
                           ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
                break;
@@ -2147,24 +1962,11 @@ ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, struct scsi_cmnd *cmd)
 }
 
 static void
-ahc_linux_sem_timeout(u_long arg)
+ahc_linux_freeze_simq(struct ahc_softc *ahc)
 {
-       struct  ahc_softc *ahc;
-       u_long  s;
-
-       ahc = (struct ahc_softc *)arg;
+       unsigned long s;
 
        ahc_lock(ahc, &s);
-       if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
-               ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
-               up(&ahc->platform_data->eh_sem);
-       }
-       ahc_unlock(ahc, &s);
-}
-
-static void
-ahc_linux_freeze_simq(struct ahc_softc *ahc)
-{
        ahc->platform_data->qfrozen++;
        if (ahc->platform_data->qfrozen == 1) {
                scsi_block_requests(ahc->platform_data->host);
@@ -2174,17 +1976,15 @@ ahc_linux_freeze_simq(struct ahc_softc *ahc)
                                        CAM_LUN_WILDCARD, SCB_LIST_NULL,
                                        ROLE_INITIATOR, CAM_REQUEUE_REQ);
        }
+       ahc_unlock(ahc, &s);
 }
 
 static void
-ahc_linux_release_simq(u_long arg)
+ahc_linux_release_simq(struct ahc_softc *ahc)
 {
-       struct ahc_softc *ahc;
        u_long s;
        int    unblock_reqs;
 
-       ahc = (struct ahc_softc *)arg;
-
        unblock_reqs = 0;
        ahc_lock(ahc, &s);
        if (ahc->platform_data->qfrozen > 0)
@@ -2218,15 +2018,14 @@ ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
        int    paused;
        int    wait;
        int    disconnected;
+       unsigned long flags;
 
        pending_scb = NULL;
        paused = FALSE;
        wait = FALSE;
        ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
 
-       printf("%s:%d:%d:%d: Attempting to queue a%s message\n",
-              ahc_name(ahc), cmd->device->channel,
-              cmd->device->id, cmd->device->lun,
+       scmd_printk(KERN_INFO, cmd, "Attempting to queue a%s message\n",
               flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
 
        printf("CDB:");
@@ -2234,6 +2033,8 @@ ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
                printf(" 0x%x", cmd->cmnd[cdb_byte]);
        printf("\n");
 
+       ahc_lock(ahc, &flags);
+
        /*
         * First determine if we currently own this command.
         * Start by searching the device queue.  If not found
@@ -2279,18 +2080,16 @@ ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
 
                /* Any SCB for this device will do for a target reset */
                LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
-                       if (ahc_match_scb(ahc, pending_scb, cmd->device->id,
-                                         cmd->device->channel + 'A',
+                       if (ahc_match_scb(ahc, pending_scb, scmd_id(cmd),
+                                         scmd_channel(cmd) + 'A',
                                          CAM_LUN_WILDCARD,
-                                         SCB_LIST_NULL, ROLE_INITIATOR) == 0)
+                                         SCB_LIST_NULL, ROLE_INITIATOR))
                                break;
                }
        }
 
        if (pending_scb == NULL) {
-               printf("%s:%d:%d:%d: Command not found\n",
-                      ahc_name(ahc), cmd->device->channel, cmd->device->id,
-                      cmd->device->lun);
+               scmd_printk(KERN_INFO, cmd, "Command not found\n");
                goto no_cmd;
        }
 
@@ -2312,9 +2111,7 @@ ahc_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",
-                      ahc_name(ahc), cmd->device->channel, cmd->device->id,
-                      cmd->device->lun);
+               scmd_printk(KERN_INFO, cmd, "Command already completed\n");
                goto no_cmd;
        }
 
@@ -2371,7 +2168,7 @@ ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
        if (last_phase != P_BUSFREE
         && (pending_scb->hscb->tag == active_scb_index
          || (flag == SCB_DEVICE_RESET
-          && SCSIID_TARGET(ahc, saved_scsiid) == cmd->device->id))) {
+          && SCSIID_TARGET(ahc, saved_scsiid) == scmd_id(cmd)))) {
 
                /*
                 * We're active on the bus, so assert ATN
@@ -2381,9 +2178,7 @@ ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
                pending_scb->flags |= SCB_RECOVERY_SCB|flag;
                ahc_outb(ahc, MSG_OUT, HOST_MSG);
                ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
-               printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
-                      ahc_name(ahc), 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) {
 
@@ -2449,9 +2244,7 @@ ahc_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",
-                      ahc_name(ahc), cmd->device->channel, cmd->device->id,
-                      cmd->device->lun);
+               scmd_printk(KERN_INFO, cmd, "Unable to deliver message\n");
                retval = FAILED;
                goto done;
        }
@@ -2468,26 +2261,23 @@ done:
        if (paused)
                ahc_unpause(ahc);
        if (wait) {
-               struct timer_list timer;
-               int ret;
-
-               ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE;
-               spin_unlock_irq(&ahc->platform_data->spin_lock);
-               init_timer(&timer);
-               timer.data = (u_long)ahc;
-               timer.expires = jiffies + (5 * HZ);
-               timer.function = ahc_linux_sem_timeout;
-               add_timer(&timer);
+               DECLARE_COMPLETION_ONSTACK(done);
+
+               ahc->platform_data->eh_done = &done;
+               ahc_unlock(ahc, &flags);
+
                printf("Recovery code sleeping\n");
-               down(&ahc->platform_data->eh_sem);
-               printf("Recovery code awake\n");
-               ret = del_timer_sync(&timer);
-               if (ret == 0) {
+               if (!wait_for_completion_timeout(&done, 5 * HZ)) {
+                       ahc_lock(ahc, &flags);
+                       ahc->platform_data->eh_done = NULL;
+                       ahc_unlock(ahc, &flags);
+
                        printf("Timer Expired\n");
                        retval = FAILED;
                }
-               spin_lock_irq(&ahc->platform_data->spin_lock);
-       }
+               printf("Recovery code awake\n");
+       } else
+               ahc_unlock(ahc, &flags);
        return (retval);
 }
 
@@ -2496,8 +2286,6 @@ ahc_platform_dump_card_state(struct ahc_softc *ahc)
 {
 }
 
-static void ahc_linux_exit(void);
-
 static void ahc_linux_set_width(struct scsi_target *starget, int width)
 {
        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
@@ -2532,8 +2320,13 @@ static void ahc_linux_set_period(struct scsi_target *starget, int period)
 
        if (period < 9)
                period = 9;     /* 12.5ns is our minimum */
-       if (period == 9)
-               ppr_options |= MSG_EXT_PPR_DT_REQ;
+       if (period == 9) {
+               if (spi_max_width(starget))
+                       ppr_options |= MSG_EXT_PPR_DT_REQ;
+               else
+                       /* need wide for DT and need DT for 12.5 ns */
+                       period = 10;
+       }
 
        ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
                            starget->channel + 'A', ROLE_INITIATOR);
@@ -2592,12 +2385,14 @@ static void ahc_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;
        struct ahc_syncrate *syncrate;
 
-       if (dt) {
-               period = 9;     /* 12.5ns is the only period valid for DT */
+       if (dt && spi_max_width(starget)) {
                ppr_options |= MSG_EXT_PPR_DT_REQ;
+               if (!width)
+                       ahc_linux_set_width(starget, 1);
        } else if (period == 9)
                period = 10;    /* if resetting DT, period must be >= 25ns */
 
@@ -2610,6 +2405,11 @@ static void ahc_linux_set_dt(struct scsi_target *starget, int dt)
        ahc_unlock(ahc, &flags);
 }
 
+#if 0
+/* FIXME: This code claims to support IU and QAS.  However, the actual
+ * sequencer code and aic7xxx_core have no support for these parameters and
+ * will get into a bad state if they're negotiated.  Do not enable this
+ * unless you know what you're doing */
 static void ahc_linux_set_qas(struct scsi_target *starget, int qas)
 {
        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
@@ -2665,6 +2465,36 @@ static void ahc_linux_set_iu(struct scsi_target *starget, int iu)
                         ppr_options, AHC_TRANS_GOAL, FALSE);
        ahc_unlock(ahc, &flags);
 }
+#endif
+
+static void ahc_linux_get_signalling(struct Scsi_Host *shost)
+{
+       struct ahc_softc *ahc = *(struct ahc_softc **)shost->hostdata;
+       unsigned long flags;
+       u8 mode;
+
+       if (!(ahc->features & AHC_ULTRA2)) {
+               /* non-LVD chipset, may not have SBLKCTL reg */
+               spi_signalling(shost) = 
+                       ahc->features & AHC_HVD ?
+                       SPI_SIGNAL_HVD :
+                       SPI_SIGNAL_SE;
+               return;
+       }
+
+       ahc_lock(ahc, &flags);
+       ahc_pause(ahc);
+       mode = ahc_inb(ahc, SBLKCTL);
+       ahc_unpause(ahc);
+       ahc_unlock(ahc, &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 ahc_linux_transport_functions = {
        .set_offset     = ahc_linux_set_offset,
@@ -2675,10 +2505,13 @@ static struct spi_function_template ahc_linux_transport_functions = {
        .show_width     = 1,
        .set_dt         = ahc_linux_set_dt,
        .show_dt        = 1,
+#if 0
        .set_iu         = ahc_linux_set_iu,
        .show_iu        = 1,
        .set_qas        = ahc_linux_set_qas,
        .show_qas       = 1,
+#endif
+       .get_signalling = ahc_linux_get_signalling,
 };
 
 
@@ -2686,18 +2519,23 @@ static struct spi_function_template ahc_linux_transport_functions = {
 static int __init
 ahc_linux_init(void)
 {
-       ahc_linux_transport_template = spi_attach_transport(&ahc_linux_transport_functions);
+       /*
+        * If we've been passed any parameters, process them now.
+        */
+       if (aic7xxx)
+               aic7xxx_setup(aic7xxx);
+
+       ahc_linux_transport_template =
+               spi_attach_transport(&ahc_linux_transport_functions);
        if (!ahc_linux_transport_template)
                return -ENODEV;
-       scsi_transport_reserve_target(ahc_linux_transport_template,
-                                     sizeof(struct ahc_linux_target));
+
        scsi_transport_reserve_device(ahc_linux_transport_template,
                                      sizeof(struct ahc_linux_device));
-       if (ahc_linux_detect(&aic7xxx_driver_template))
-               return 0;
-       spi_release_transport(ahc_linux_transport_template);
-       ahc_linux_exit();
-       return -ENODEV;
+
+       ahc_linux_pci_init();
+       ahc_linux_eisa_init();
+       return 0;
 }
 
 static void