Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / scsi / aic7xxx / aic79xx_core.c
index 137fb1a..08771f6 100644 (file)
@@ -37,9 +37,7 @@
  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGES.
  *
- * $Id: //depot/aic7xxx/aic7xxx/aic79xx.c#202 $
- *
- * $FreeBSD$
+ * $Id: //depot/aic7xxx/aic7xxx/aic79xx.c#250 $
  */
 
 #ifdef __linux__
@@ -52,8 +50,6 @@
 #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
 #endif
 
-/******************************** Globals *************************************/
-struct ahd_softc_tailq ahd_tailq = TAILQ_HEAD_INITIALIZER(ahd_tailq);
 
 /***************************** Lookup Tables **********************************/
 char *ahd_chip_names[] =
@@ -201,7 +197,8 @@ static int          ahd_search_scb_list(struct ahd_softc *ahd, int target,
                                            char channel, int lun, u_int tag,
                                            role_t role, uint32_t status,
                                            ahd_search_action action,
-                                           u_int *list_head, u_int tid);
+                                           u_int *list_head, u_int *list_tail,
+                                           u_int tid);
 static void            ahd_stitch_tid_list(struct ahd_softc *ahd,
                                            u_int tid_prev, u_int tid_cur,
                                            u_int tid_next);
@@ -210,7 +207,6 @@ static void         ahd_add_scb_to_free_list(struct ahd_softc *ahd,
 static u_int           ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
                                     u_int prev, u_int next, u_int tid);
 static void            ahd_reset_current_bus(struct ahd_softc *ahd);
-static ahd_callback_t  ahd_reset_poll;
 static ahd_callback_t  ahd_stat_timer;
 #ifdef AHD_DUMP_SEQ
 static void            ahd_dumpseq(struct ahd_softc *ahd);
@@ -334,6 +330,14 @@ ahd_restart(struct ahd_softc *ahd)
        ahd_outb(ahd, SCSISEQ1,
                 ahd_inb(ahd, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
        ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
+
+       /*
+        * Clear any pending sequencer interrupt.  It is no
+        * longer relevant since we're resetting the Program
+        * Counter.
+        */
+       ahd_outb(ahd, CLRINT, CLRSEQINT);
+
        ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
        ahd_unpause(ahd);
 }
@@ -375,13 +379,7 @@ ahd_flush_qoutfifo(struct ahd_softc *ahd)
        saved_modes = ahd_save_modes(ahd);
 
        /*
-        * Complete any SCBs that just finished being
-        * DMA'ed into the qoutfifo.
-        */
-       ahd_run_qoutfifo(ahd);
-
-       /*
-        * Flush the good status FIFO for compelted packetized commands.
+        * Flush the good status FIFO for completed packetized commands.
         */
        ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
        saved_scbptr = ahd_get_scbptr(ahd);
@@ -389,8 +387,7 @@ ahd_flush_qoutfifo(struct ahd_softc *ahd)
                u_int fifo_mode;
                u_int i;
                
-               scbid = (ahd_inb(ahd, GSFIFO+1) << 8)
-                     | ahd_inb(ahd, GSFIFO);
+               scbid = ahd_inw(ahd, GSFIFO);
                scb = ahd_lookup_scb(ahd, scbid);
                if (scb == NULL) {
                        printf("%s: Warning - GSFIFO SCB %d invalid\n",
@@ -403,22 +400,33 @@ ahd_flush_qoutfifo(struct ahd_softc *ahd)
                 * the host before completing the  command.
                 */
                fifo_mode = 0;
+rescan_fifos:
                for (i = 0; i < 2; i++) {
                        /* Toggle to the other mode. */
                        fifo_mode ^= 1;
                        ahd_set_modes(ahd, fifo_mode, fifo_mode);
+
                        if (ahd_scb_active_in_fifo(ahd, scb) == 0)
                                continue;
 
                        ahd_run_data_fifo(ahd, scb);
 
                        /*
-                        * Clearing this transaction in this FIFO may
-                        * cause a CFG4DATA for this same transaction
-                        * to assert in the other FIFO.  Make sure we
-                        * loop one more time and check the other FIFO.
+                        * Running this FIFO may cause a CFG4DATA for
+                        * this same transaction to assert in the other
+                        * FIFO or a new snapshot SAVEPTRS interrupt
+                        * in this FIFO.  Even running a FIFO may not
+                        * clear the transaction if we are still waiting
+                        * for data to drain to the host. We must loop
+                        * until the transaction is not active in either
+                        * FIFO just to be sure.  Reset our loop counter
+                        * so we will visit both FIFOs again before
+                        * declaring this transaction finished.  We
+                        * also delay a bit so that status has a chance
+                        * to change before we look at this FIFO again.
                         */
-                       i = 0;
+                       ahd_delay(200);
+                       goto rescan_fifos;
                }
                ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
                ahd_set_scbptr(ahd, scbid);
@@ -431,19 +439,28 @@ ahd_flush_qoutfifo(struct ahd_softc *ahd)
                        /*
                         * The transfer completed with a residual.
                         * Place this SCB on the complete DMA list
-                        * so that we Update our in-core copy of the
+                        * so that we update our in-core copy of the
                         * SCB before completing the command.
                         */
                        ahd_outb(ahd, SCB_SCSI_STATUS, 0);
                        ahd_outb(ahd, SCB_SGPTR,
                                 ahd_inb_scbram(ahd, SCB_SGPTR)
                                 | SG_STATUS_VALID);
-                       ahd_outw(ahd, SCB_TAG, SCB_GET_TAG(scb));
+                       ahd_outw(ahd, SCB_TAG, scbid);
+                       ahd_outw(ahd, SCB_NEXT_COMPLETE, SCB_LIST_NULL);
                        comp_head = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
-                       ahd_outw(ahd, SCB_NEXT_COMPLETE, comp_head);
-                       if (SCBID_IS_NULL(comp_head))
-                               ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD,
-                                        SCB_GET_TAG(scb));
+                       if (SCBID_IS_NULL(comp_head)) {
+                               ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, scbid);
+                               ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
+                       } else {
+                               u_int tail;
+
+                               tail = ahd_inw(ahd, COMPLETE_DMA_SCB_TAIL);
+                               ahd_set_scbptr(ahd, tail);
+                               ahd_outw(ahd, SCB_NEXT_COMPLETE, scbid);
+                               ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
+                               ahd_set_scbptr(ahd, scbid);
+                       }
                } else
                        ahd_complete_scb(ahd, scb);
        }
@@ -467,9 +484,22 @@ ahd_flush_qoutfifo(struct ahd_softc *ahd)
                        break;
                ahd_delay(200);
        }
-       if ((ccscbctl & CCSCBDIR) != 0)
+       /*
+        * We leave the sequencer to cleanup in the case of DMA's to
+        * update the qoutfifo.  In all other cases (DMA's to the
+        * chip or a push of an SCB from the COMPLETE_DMA_SCB list),
+        * we disable the DMA engine so that the sequencer will not
+        * attempt to handle the DMA completion.
+        */
+       if ((ccscbctl & CCSCBDIR) != 0 || (ccscbctl & ARRDONE) != 0)
                ahd_outb(ahd, CCSCBCTL, ccscbctl & ~(CCARREN|CCSCBEN));
 
+       /*
+        * Complete any SCBs that just finished
+        * being DMA'ed into the qoutfifo.
+        */
+       ahd_run_qoutfifo(ahd);
+
        saved_scbptr = ahd_get_scbptr(ahd);
        /*
         * Manually update/complete any completed SCBs that are waiting to be
@@ -496,6 +526,24 @@ ahd_flush_qoutfifo(struct ahd_softc *ahd)
                scbid = next_scbid;
        }
        ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
+       ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
+
+       scbid = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
+       while (!SCBID_IS_NULL(scbid)) {
+
+               ahd_set_scbptr(ahd, scbid);
+               next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
+               scb = ahd_lookup_scb(ahd, scbid);
+               if (scb == NULL) {
+                       printf("%s: Warning - Complete Qfrz SCB %d invalid\n",
+                              ahd_name(ahd), scbid);
+                       continue;
+               }
+
+               ahd_complete_scb(ahd, scb);
+               scbid = next_scbid;
+       }
+       ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
 
        scbid = ahd_inw(ahd, COMPLETE_SCB_HEAD);
        while (!SCBID_IS_NULL(scbid)) {
@@ -560,150 +608,146 @@ ahd_run_data_fifo(struct ahd_softc *ahd, struct scb *scb)
 {
        u_int seqintsrc;
 
-       while (1) {
-               seqintsrc = ahd_inb(ahd, SEQINTSRC);
-               if ((seqintsrc & CFG4DATA) != 0) {
-                       uint32_t datacnt;
-                       uint32_t sgptr;
-
-                       /*
-                        * Clear full residual flag.
-                        */
-                       sgptr = ahd_inl_scbram(ahd, SCB_SGPTR) & ~SG_FULL_RESID;
-                       ahd_outb(ahd, SCB_SGPTR, sgptr);
+       seqintsrc = ahd_inb(ahd, SEQINTSRC);
+       if ((seqintsrc & CFG4DATA) != 0) {
+               uint32_t datacnt;
+               uint32_t sgptr;
 
-                       /*
-                        * Load datacnt and address.
-                        */
-                       datacnt = ahd_inl_scbram(ahd, SCB_DATACNT);
-                       if ((datacnt & AHD_DMA_LAST_SEG) != 0) {
-                               sgptr |= LAST_SEG;
-                               ahd_outb(ahd, SG_STATE, 0);
-                       } else
-                               ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
-                       ahd_outq(ahd, HADDR, ahd_inq_scbram(ahd, SCB_DATAPTR));
-                       ahd_outl(ahd, HCNT, datacnt & AHD_SG_LEN_MASK);
-                       ahd_outb(ahd, SG_CACHE_PRE, sgptr);
-                       ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
+               /*
+                * Clear full residual flag.
+                */
+               sgptr = ahd_inl_scbram(ahd, SCB_SGPTR) & ~SG_FULL_RESID;
+               ahd_outb(ahd, SCB_SGPTR, sgptr);
 
-                       /*
-                        * Initialize Residual Fields.
-                        */
-                       ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, datacnt >> 24);
-                       ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr & SG_PTR_MASK);
+               /*
+                * Load datacnt and address.
+                */
+               datacnt = ahd_inl_scbram(ahd, SCB_DATACNT);
+               if ((datacnt & AHD_DMA_LAST_SEG) != 0) {
+                       sgptr |= LAST_SEG;
+                       ahd_outb(ahd, SG_STATE, 0);
+               } else
+                       ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
+               ahd_outq(ahd, HADDR, ahd_inq_scbram(ahd, SCB_DATAPTR));
+               ahd_outl(ahd, HCNT, datacnt & AHD_SG_LEN_MASK);
+               ahd_outb(ahd, SG_CACHE_PRE, sgptr);
+               ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
 
-                       /*
-                        * Mark the SCB as having a FIFO in use.
-                        */
-                       ahd_outb(ahd, SCB_FIFO_USE_COUNT,
-                                ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) + 1);
+               /*
+                * Initialize Residual Fields.
+                */
+               ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, datacnt >> 24);
+               ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr & SG_PTR_MASK);
 
-                       /*
-                        * Install a "fake" handler for this FIFO.
-                        */
-                       ahd_outw(ahd, LONGJMP_ADDR, 0);
+               /*
+                * Mark the SCB as having a FIFO in use.
+                */
+               ahd_outb(ahd, SCB_FIFO_USE_COUNT,
+                        ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) + 1);
 
-                       /*
-                        * Notify the hardware that we have satisfied
-                        * this sequencer interrupt.
-                        */
-                       ahd_outb(ahd, CLRSEQINTSRC, CLRCFG4DATA);
-               } else if ((seqintsrc & SAVEPTRS) != 0) {
-                       uint32_t sgptr;
-                       uint32_t resid;
+               /*
+                * Install a "fake" handler for this FIFO.
+                */
+               ahd_outw(ahd, LONGJMP_ADDR, 0);
 
-                       if ((ahd_inb(ahd, LONGJMP_ADDR+1)&INVALID_ADDR) != 0) {
-                               /*
-                                * Snapshot Save Pointers.  Clear
-                                * the snapshot and continue.
-                                */
-                               ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
-                               continue;
-                       }
+               /*
+                * Notify the hardware that we have satisfied
+                * this sequencer interrupt.
+                */
+               ahd_outb(ahd, CLRSEQINTSRC, CLRCFG4DATA);
+       } else if ((seqintsrc & SAVEPTRS) != 0) {
+               uint32_t sgptr;
+               uint32_t resid;
 
+               if ((ahd_inb(ahd, LONGJMP_ADDR+1)&INVALID_ADDR) != 0) {
                        /*
-                        * Disable S/G fetch so the DMA engine
-                        * is available to future users.
+                        * Snapshot Save Pointers.  All that
+                        * is necessary to clear the snapshot
+                        * is a CLRCHN.
                         */
-                       if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
-                               ahd_outb(ahd, CCSGCTL, 0);
-                       ahd_outb(ahd, SG_STATE, 0);
+                       goto clrchn;
+               }
 
-                       /*
-                        * Flush the data FIFO.  Strickly only
-                        * necessary for Rev A parts.
-                        */
-                       ahd_outb(ahd, DFCNTRL,
-                                ahd_inb(ahd, DFCNTRL) | FIFOFLUSH);
+               /*
+                * Disable S/G fetch so the DMA engine
+                * is available to future users.
+                */
+               if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
+                       ahd_outb(ahd, CCSGCTL, 0);
+               ahd_outb(ahd, SG_STATE, 0);
 
-                       /*
-                        * Calculate residual.
-                        */
-                       sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
-                       resid = ahd_inl(ahd, SHCNT);
-                       resid |=
-                           ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+3) << 24;
-                       ahd_outl(ahd, SCB_RESIDUAL_DATACNT, resid);
-                       if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG) == 0) {
-                               /*
-                                * Must back up to the correct S/G element.
-                                * Typically this just means resetting our
-                                * low byte to the offset in the SG_CACHE,
-                                * but if we wrapped, we have to correct
-                                * the other bytes of the sgptr too.
-                                */
-                               if ((ahd_inb(ahd, SG_CACHE_SHADOW) & 0x80) != 0
-                                && (sgptr & 0x80) == 0)
-                                       sgptr -= 0x100;
-                               sgptr &= ~0xFF;
-                               sgptr |= ahd_inb(ahd, SG_CACHE_SHADOW)
-                                      & SG_ADDR_MASK;
-                               ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
-                               ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 3, 0);
-                       } else if ((resid & AHD_SG_LEN_MASK) == 0) {
-                               ahd_outb(ahd, SCB_RESIDUAL_SGPTR,
-                                        sgptr | SG_LIST_NULL);
-                       }
-                       /*
-                        * Save Pointers.
-                        */
-                       ahd_outq(ahd, SCB_DATAPTR, ahd_inq(ahd, SHADDR));
-                       ahd_outl(ahd, SCB_DATACNT, resid);
-                       ahd_outl(ahd, SCB_SGPTR, sgptr);
-                       ahd_outb(ahd, CLRSEQINTSRC, CLRSAVEPTRS);
-                       ahd_outb(ahd, SEQIMODE,
-                                ahd_inb(ahd, SEQIMODE) | ENSAVEPTRS);
-                       /*
-                        * If the data is to the SCSI bus, we are
-                        * done, otherwise wait for FIFOEMP.
-                        */
-                       if ((ahd_inb(ahd, DFCNTRL) & DIRECTION) != 0)
-                               break;
-               } else if ((ahd_inb(ahd, SG_STATE) & LOADING_NEEDED) != 0) {
-                       uint32_t sgptr;
-                       uint64_t data_addr;
-                       uint32_t data_len;
-                       u_int    dfcntrl;
+               /*
+                * Flush the data FIFO.  Strickly only
+                * necessary for Rev A parts.
+                */
+               ahd_outb(ahd, DFCNTRL, ahd_inb(ahd, DFCNTRL) | FIFOFLUSH);
 
+               /*
+                * Calculate residual.
+                */
+               sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
+               resid = ahd_inl(ahd, SHCNT);
+               resid |= ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+3) << 24;
+               ahd_outl(ahd, SCB_RESIDUAL_DATACNT, resid);
+               if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG) == 0) {
                        /*
-                        * Disable S/G fetch so the DMA engine
-                        * is available to future users.
+                        * Must back up to the correct S/G element.
+                        * Typically this just means resetting our
+                        * low byte to the offset in the SG_CACHE,
+                        * but if we wrapped, we have to correct
+                        * the other bytes of the sgptr too.
                         */
-                       if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) {
-                               ahd_outb(ahd, CCSGCTL, 0);
-                               ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
-                       }
+                       if ((ahd_inb(ahd, SG_CACHE_SHADOW) & 0x80) != 0
+                        && (sgptr & 0x80) == 0)
+                               sgptr -= 0x100;
+                       sgptr &= ~0xFF;
+                       sgptr |= ahd_inb(ahd, SG_CACHE_SHADOW)
+                              & SG_ADDR_MASK;
+                       ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
+                       ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 3, 0);
+               } else if ((resid & AHD_SG_LEN_MASK) == 0) {
+                       ahd_outb(ahd, SCB_RESIDUAL_SGPTR,
+                                sgptr | SG_LIST_NULL);
+               }
+               /*
+                * Save Pointers.
+                */
+               ahd_outq(ahd, SCB_DATAPTR, ahd_inq(ahd, SHADDR));
+               ahd_outl(ahd, SCB_DATACNT, resid);
+               ahd_outl(ahd, SCB_SGPTR, sgptr);
+               ahd_outb(ahd, CLRSEQINTSRC, CLRSAVEPTRS);
+               ahd_outb(ahd, SEQIMODE,
+                        ahd_inb(ahd, SEQIMODE) | ENSAVEPTRS);
+               /*
+                * If the data is to the SCSI bus, we are
+                * done, otherwise wait for FIFOEMP.
+                */
+               if ((ahd_inb(ahd, DFCNTRL) & DIRECTION) != 0)
+                       goto clrchn;
+       } else if ((ahd_inb(ahd, SG_STATE) & LOADING_NEEDED) != 0) {
+               uint32_t sgptr;
+               uint64_t data_addr;
+               uint32_t data_len;
+               u_int    dfcntrl;
 
-                       /*
-                        * Wait for the DMA engine to notice that the
-                        * host transfer is enabled and that there is
-                        * space in the S/G FIFO for new segments before
-                        * loading more segments.
-                        */
-                       if ((ahd_inb(ahd, DFSTATUS) & PRELOAD_AVAIL) == 0)
-                               continue;
-                       if ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) == 0)
-                               continue;
+               /*
+                * Disable S/G fetch so the DMA engine
+                * is available to future users.  We won't
+                * be using the DMA engine to load segments.
+                */
+               if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) {
+                       ahd_outb(ahd, CCSGCTL, 0);
+                       ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
+               }
+
+               /*
+                * Wait for the DMA engine to notice that the
+                * host transfer is enabled and that there is
+                * space in the S/G FIFO for new segments before
+                * loading more segments.
+                */
+               if ((ahd_inb(ahd, DFSTATUS) & PRELOAD_AVAIL) != 0
+                && (ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0) {
 
                        /*
                         * Determine the offset of the next S/G
@@ -750,7 +794,7 @@ ahd_run_data_fifo(struct ahd_softc *ahd, struct scb *scb)
                         * Advertise the segment to the hardware.
                         */
                        dfcntrl = ahd_inb(ahd, DFCNTRL)|PRELOADEN|HDMAEN;
-                       if ((ahd->features & AHD_NEW_DFCNTRL_OPTS)!=0) {
+                       if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
                                /*
                                 * Use SCSIENWRDIS so that SCSIEN
                                 * is never modified by this
@@ -759,35 +803,44 @@ ahd_run_data_fifo(struct ahd_softc *ahd, struct scb *scb)
                                dfcntrl |= SCSIENWRDIS;
                        }
                        ahd_outb(ahd, DFCNTRL, dfcntrl);
-               } else if ((ahd_inb(ahd, SG_CACHE_SHADOW)
-                        & LAST_SEG_DONE) != 0) {
-
-                       /*
-                        * Transfer completed to the end of SG list
-                        * and has flushed to the host.
-                        */
-                       ahd_outb(ahd, SCB_SGPTR,
-                                ahd_inb_scbram(ahd, SCB_SGPTR) | SG_LIST_NULL);
-                       break;
-               } else if ((ahd_inb(ahd, DFSTATUS) & FIFOEMP) != 0) {
-                       break;
                }
-               ahd_delay(200);
+       } else if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG_DONE) != 0) {
+
+               /*
+                * Transfer completed to the end of SG list
+                * and has flushed to the host.
+                */
+               ahd_outb(ahd, SCB_SGPTR,
+                        ahd_inb_scbram(ahd, SCB_SGPTR) | SG_LIST_NULL);
+               goto clrchn;
+       } else if ((ahd_inb(ahd, DFSTATUS) & FIFOEMP) != 0) {
+clrchn:
+               /*
+                * Clear any handler for this FIFO, decrement
+                * the FIFO use count for the SCB, and release
+                * the FIFO.
+                */
+               ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
+               ahd_outb(ahd, SCB_FIFO_USE_COUNT,
+                        ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) - 1);
+               ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
        }
-       /*
-        * Clear any handler for this FIFO, decrement
-        * the FIFO use count for the SCB, and release
-        * the FIFO.
-        */
-       ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
-       ahd_outb(ahd, SCB_FIFO_USE_COUNT,
-                ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) - 1);
-       ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
 }
 
+/*
+ * Look for entries in the QoutFIFO that have completed.
+ * The valid_tag completion field indicates the validity
+ * of the entry - the valid value toggles each time through
+ * the queue. We use the sg_status field in the completion
+ * entry to avoid referencing the hscb if the completion
+ * occurred with no errors and no residual.  sg_status is
+ * a copy of the first byte (little endian) of the sgptr
+ * hscb field.
+ */
 void
 ahd_run_qoutfifo(struct ahd_softc *ahd)
 {
+       struct ahd_completion *completion;
        struct scb *scb;
        u_int  scb_index;
 
@@ -795,11 +848,13 @@ ahd_run_qoutfifo(struct ahd_softc *ahd)
                panic("ahd_run_qoutfifo recursion");
        ahd->flags |= AHD_RUNNING_QOUTFIFO;
        ahd_sync_qoutfifo(ahd, BUS_DMASYNC_POSTREAD);
-       while ((ahd->qoutfifo[ahd->qoutfifonext]
-            & QOUTFIFO_ENTRY_VALID_LE) == ahd->qoutfifonext_valid_tag) {
+       for (;;) {
+               completion = &ahd->qoutfifo[ahd->qoutfifonext];
+
+               if (completion->valid_tag != ahd->qoutfifonext_valid_tag)
+                       break;
 
-               scb_index = ahd_le16toh(ahd->qoutfifo[ahd->qoutfifonext]
-                                     & ~QOUTFIFO_ENTRY_VALID_LE);
+               scb_index = ahd_le16toh(completion->tag);
                scb = ahd_lookup_scb(ahd, scb_index);
                if (scb == NULL) {
                        printf("%s: WARNING no command for scb %d "
@@ -807,12 +862,15 @@ ahd_run_qoutfifo(struct ahd_softc *ahd)
                               ahd_name(ahd), scb_index,
                               ahd->qoutfifonext);
                        ahd_dump_card_state(ahd);
-               } else
-                       ahd_complete_scb(ahd, scb);
+               } else if ((completion->sg_status & SG_STATUS_VALID) != 0) {
+                       ahd_handle_scb_status(ahd, scb);
+               } else {
+                       ahd_done(ahd, scb);
+               }
 
                ahd->qoutfifonext = (ahd->qoutfifonext+1) & (AHD_QOUT_SIZE-1);
                if (ahd->qoutfifonext == 0)
-                       ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID_LE;
+                       ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID;
        }
        ahd->flags &= ~AHD_RUNNING_QOUTFIFO;
 }
@@ -878,26 +936,6 @@ ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
                       ahd_name(ahd), seqintcode);
 #endif
        switch (seqintcode) {
-       case BAD_SCB_STATUS:
-       {
-               struct  scb *scb;
-               u_int   scbid;
-               int     cmds_pending;
-
-               scbid = ahd_get_scbptr(ahd);
-               scb = ahd_lookup_scb(ahd, scbid);
-               if (scb != NULL) {
-                       ahd_complete_scb(ahd, scb);
-               } else {
-                       printf("%s: WARNING no command for scb %d "
-                              "(bad status)\n", ahd_name(ahd), scbid);
-                       ahd_dump_card_state(ahd);
-               }
-               cmds_pending = ahd_inw(ahd, CMDS_PENDING);
-               if (cmds_pending > 0)
-                       ahd_outw(ahd, CMDS_PENDING, cmds_pending - 1);
-               break;
-       }
        case ENTERING_NONPACK:
        {
                struct  scb *scb;
@@ -939,9 +977,13 @@ ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
                break;
        }
        case INVALID_SEQINT:
-               printf("%s: Invalid Sequencer interrupt occurred.\n",
+               printf("%s: Invalid Sequencer interrupt occurred, "
+                      "resetting channel.\n",
                       ahd_name(ahd));
-               ahd_dump_card_state(ahd);
+#ifdef AHD_DEBUG
+               if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
+                       ahd_dump_card_state(ahd);
+#endif
                ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
                break;
        case STATUS_OVERRUN:
@@ -1011,12 +1053,10 @@ ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
                         * If a target takes us into the command phase
                         * assume that it has been externally reset and
                         * has thus lost our previous packetized negotiation
-                        * agreement.  Since we have not sent an identify
-                        * message and may not have fully qualified the
-                        * connection, we change our command to TUR, assert
-                        * ATN and ABORT the task when we go to message in
-                        * phase.  The OSM will see the REQUEUE_REQUEST
-                        * status and retry the command.
+                        * agreement.
+                        * Revert to async/narrow transfers until we
+                        * can renegotiate with the device and notify
+                        * the OSM about the reset.
                         */
                        scbid = ahd_get_scbptr(ahd);
                        scb = ahd_lookup_scb(ahd, scbid);
@@ -1043,31 +1083,15 @@ ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
                        ahd_set_syncrate(ahd, &devinfo, /*period*/0,
                                         /*offset*/0, /*ppr_options*/0,
                                         AHD_TRANS_ACTIVE, /*paused*/TRUE);
-                       ahd_outb(ahd, SCB_CDB_STORE, 0);
-                       ahd_outb(ahd, SCB_CDB_STORE+1, 0);
-                       ahd_outb(ahd, SCB_CDB_STORE+2, 0);
-                       ahd_outb(ahd, SCB_CDB_STORE+3, 0);
-                       ahd_outb(ahd, SCB_CDB_STORE+4, 0);
-                       ahd_outb(ahd, SCB_CDB_STORE+5, 0);
-                       ahd_outb(ahd, SCB_CDB_LEN, 6);
-                       scb->hscb->control &= ~(TAG_ENB|SCB_TAG_TYPE);
-                       scb->hscb->control |= MK_MESSAGE;
-                       ahd_outb(ahd, SCB_CONTROL, scb->hscb->control);
-                       ahd_outb(ahd, MSG_OUT, HOST_MSG);
-                       ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
-                       /*
-                        * The lun is 0, regardless of the SCB's lun
-                        * as we have not sent an identify message.
-                        */
-                       ahd_outb(ahd, SAVED_LUN, 0);
-                       ahd_outb(ahd, SEQ_FLAGS, 0);
-                       ahd_assert_atn(ahd);
-                       scb->flags &= ~(SCB_PACKETIZED);
-                       scb->flags |= SCB_ABORT|SCB_CMDPHASE_ABORT;
+                       scb->flags |= SCB_EXTERNAL_RESET;
                        ahd_freeze_devq(ahd, scb);
                        ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
                        ahd_freeze_scb(scb);
 
+                       /* Notify XPT */
+                       ahd_send_async(ahd, devinfo.channel, devinfo.target,
+                                      CAM_LUN_WILDCARD, AC_SENT_BDR, NULL);
+
                        /*
                         * Allow the sequencer to continue with
                         * non-pack processing.
@@ -1491,6 +1515,18 @@ ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
        lqistat1 = ahd_inb(ahd, LQISTAT1);
        lqostat0 = ahd_inb(ahd, LQOSTAT0);
        busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
+
+       /*
+        * Ignore external resets after a bus reset.
+        */
+       if (((status & SCSIRSTI) != 0) && (ahd->flags & AHD_BUS_RESET_ACTIVE))
+               return;
+
+       /*
+        * Clear bus reset flag
+        */
+       ahd->flags &= ~AHD_BUS_RESET_ACTIVE;
+
        if ((status0 & (SELDI|SELDO)) != 0) {
                u_int simode0;
 
@@ -1505,9 +1541,6 @@ ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
         && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
                scb = NULL;
 
-       /* Make sure the sequencer is in a safe location. */
-       ahd_clear_critical_section(ahd);
-
        if ((status0 & IOERR) != 0) {
                u_int now_lvd;
 
@@ -1523,26 +1556,35 @@ ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
                ahd_setup_iocell_workaround(ahd);
                ahd_unpause(ahd);
        } else if ((status0 & OVERRUN) != 0) {
+
                printf("%s: SCSI offset overrun detected.  Resetting bus.\n",
                       ahd_name(ahd));
                ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
        } else if ((status & SCSIRSTI) != 0) {
+
                printf("%s: Someone reset channel A\n", ahd_name(ahd));
                ahd_reset_channel(ahd, 'A', /*Initiate Reset*/FALSE);
        } else if ((status & SCSIPERR) != 0) {
+
+               /* Make sure the sequencer is in a safe location. */
+               ahd_clear_critical_section(ahd);
+
                ahd_handle_transmission_error(ahd);
        } else if (lqostat0 != 0) {
+
                printf("%s: lqostat0 == 0x%x!\n", ahd_name(ahd), lqostat0);
                ahd_outb(ahd, CLRLQOINT0, lqostat0);
-               if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
+               if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
                        ahd_outb(ahd, CLRLQOINT1, 0);
-               }
        } else if ((status & SELTO) != 0) {
                u_int  scbid;
 
                /* Stop the selection */
                ahd_outb(ahd, SCSISEQ0, 0);
 
+               /* Make sure the sequencer is in a safe location. */
+               ahd_clear_critical_section(ahd);
+
                /* No more pending messages */
                ahd_clear_msg_state(ahd);
 
@@ -1575,24 +1617,27 @@ ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
                                       scbid);
                        }
 #endif
-                       /*
-                        * Force a renegotiation with this target just in
-                        * case the cable was pulled and will later be
-                        * re-attached.  The target may forget its negotiation
-                        * settings with us should it attempt to reselect
-                        * during the interruption.  The target will not issue
-                        * a unit attention in this case, so we must always
-                        * renegotiate.
-                        */
                        ahd_scb_devinfo(ahd, &devinfo, scb);
-                       ahd_force_renegotiation(ahd, &devinfo);
                        ahd_set_transaction_status(scb, CAM_SEL_TIMEOUT);
                        ahd_freeze_devq(ahd, scb);
+
+                       /*
+                        * Cancel any pending transactions on the device
+                        * now that it seems to be missing.  This will
+                        * also revert us to async/narrow transfers until
+                        * we can renegotiate with the device.
+                        */
+                       ahd_handle_devreset(ahd, &devinfo,
+                                           CAM_LUN_WILDCARD,
+                                           CAM_SEL_TIMEOUT,
+                                           "Selection Timeout",
+                                           /*verbose_level*/1);
                }
                ahd_outb(ahd, CLRINT, CLRSCSIINT);
                ahd_iocell_first_selection(ahd);
                ahd_unpause(ahd);
        } else if ((status0 & (SELDI|SELDO)) != 0) {
+
                ahd_iocell_first_selection(ahd);
                ahd_unpause(ahd);
        } else if (status3 != 0) {
@@ -1600,6 +1645,10 @@ ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
                       ahd_name(ahd), status3);
                ahd_outb(ahd, CLRSINT3, status3);
        } else if ((lqistat1 & (LQIPHASE_LQ|LQIPHASE_NLQ)) != 0) {
+
+               /* Make sure the sequencer is in a safe location. */
+               ahd_clear_critical_section(ahd);
+
                ahd_handle_lqiphase_error(ahd, lqistat1);
        } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
                /*
@@ -1609,7 +1658,8 @@ ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
                 * so just clear the error.
                 */
                ahd_outb(ahd, CLRLQIINT1, CLRLQICRCI_NLQ);
-       } else if ((status & BUSFREE) != 0) {
+       } else if ((status & BUSFREE) != 0
+               || (lqistat1 & LQOBUSFREE) != 0) {
                u_int lqostat1;
                int   restart;
                int   clear_fifo;
@@ -1624,6 +1674,9 @@ ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
                 */
                ahd_outb(ahd, SCSISEQ0, 0);
 
+               /* Make sure the sequencer is in a safe location. */
+               ahd_clear_critical_section(ahd);
+
                /*
                 * Determine what we were up to at the time of
                 * the busfree.
@@ -1661,7 +1714,16 @@ ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
                        clear_fifo = 0;
                        packetized =  (lqostat1 & LQOBUSFREE) != 0;
                        if (!packetized
-                        && ahd_inb(ahd, LASTPHASE) == P_BUSFREE)
+                        && ahd_inb(ahd, LASTPHASE) == P_BUSFREE
+                        && (ahd_inb(ahd, SSTAT0) & SELDI) == 0
+                        && ((ahd_inb(ahd, SSTAT0) & SELDO) == 0
+                         || (ahd_inb(ahd, SCSISEQ0) & ENSELO) == 0))
+                               /*
+                                * Assume packetized if we are not
+                                * on the bus in a non-packetized
+                                * capacity and any pending selection
+                                * was a packetized selection.
+                                */
                                packetized = 1;
                        break;
                }
@@ -1962,10 +2024,6 @@ ahd_handle_pkt_busfree(struct ahd_softc *ahd, u_int busfreetime)
                u_int waiting_t;
                u_int next;
 
-               if ((busfreetime & BUSFREE_LQO) == 0)
-                       printf("%s: Warning, BUSFREE time is 0x%x.  "
-                              "Expected BUSFREE_LQO.\n",
-                              ahd_name(ahd), busfreetime);
                /*
                 * The LQO manager detected an unexpected busfree
                 * either:
@@ -2142,22 +2200,6 @@ ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
                        if (sent_msg == MSG_ABORT_TAG)
                                tag = SCB_GET_TAG(scb);
 
-                       if ((scb->flags & SCB_CMDPHASE_ABORT) != 0) {
-                               /*
-                                * This abort is in response to an
-                                * unexpected switch to command phase
-                                * for a packetized connection.  Since
-                                * the identify message was never sent,
-                                * "saved lun" is 0.  We really want to
-                                * abort only the SCB that encountered
-                                * this error, which could have a different
-                                * lun.  The SCB will be retried so the OS
-                                * will see the UA after renegotiating to
-                                * packetized.
-                                */
-                               tag = SCB_GET_TAG(scb);
-                               saved_lun = scb->hscb->lun;
-                       }
                        found = ahd_abort_scbs(ahd, target, 'A', saved_lun,
                                               tag, ROLE_INITIATOR,
                                               CAM_REQ_ABORTED);
@@ -2188,8 +2230,14 @@ ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
                        struct ahd_tmode_tstate *tstate;
 
                        /*
-                        * PPR Rejected.  Try non-ppr negotiation
-                        * and retry command.
+                        * PPR Rejected.
+                        *
+                        * If the previous negotiation was packetized,
+                        * this could be because the device has been
+                        * reset without our knowledge.  Force our
+                        * current negotiation to async and retry the
+                        * negotiation.  Otherwise retry the command
+                        * with non-ppr negotiation.
                         */
 #ifdef AHD_DEBUG
                        if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
@@ -2198,11 +2246,34 @@ ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
                        tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
                                                    devinfo.our_scsiid,
                                                    devinfo.target, &tstate);
-                       tinfo->curr.transport_version = 2;
-                       tinfo->goal.transport_version = 2;
-                       tinfo->goal.ppr_options = 0;
-                       ahd_qinfifo_requeue_tail(ahd, scb);
-                       printerror = 0;
+                       if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ)!=0) {
+                               ahd_set_width(ahd, &devinfo,
+                                             MSG_EXT_WDTR_BUS_8_BIT,
+                                             AHD_TRANS_CUR,
+                                             /*paused*/TRUE);
+                               ahd_set_syncrate(ahd, &devinfo,
+                                               /*period*/0, /*offset*/0,
+                                               /*ppr_options*/0,
+                                               AHD_TRANS_CUR,
+                                               /*paused*/TRUE);
+                               /*
+                                * The expect PPR busfree handler below
+                                * will effect the retry and necessary
+                                * abort.
+                                */
+                       } else {
+                               tinfo->curr.transport_version = 2;
+                               tinfo->goal.transport_version = 2;
+                               tinfo->goal.ppr_options = 0;
+                               /*
+                                * Remove any SCBs in the waiting for selection
+                                * queue that may also be for this target so
+                                * that command ordering is preserved.
+                                */
+                               ahd_freeze_devq(ahd, scb);
+                               ahd_qinfifo_requeue_tail(ahd, scb);
+                               printerror = 0;
+                       }
                } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, FALSE)
                        && ppr_busfree == 0) {
                        /*
@@ -2217,6 +2288,12 @@ ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
                                      MSG_EXT_WDTR_BUS_8_BIT,
                                      AHD_TRANS_CUR|AHD_TRANS_GOAL,
                                      /*paused*/TRUE);
+                       /*
+                        * Remove any SCBs in the waiting for selection
+                        * queue that may also be for this target so that
+                        * command ordering is preserved.
+                        */
+                       ahd_freeze_devq(ahd, scb);
                        ahd_qinfifo_requeue_tail(ahd, scb);
                        printerror = 0;
                } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, FALSE)
@@ -2234,6 +2311,12 @@ ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
                                        /*ppr_options*/0,
                                        AHD_TRANS_CUR|AHD_TRANS_GOAL,
                                        /*paused*/TRUE);
+                       /*
+                        * Remove any SCBs in the waiting for selection
+                        * queue that may also be for this target so that
+                        * command ordering is preserved.
+                        */
+                       ahd_freeze_devq(ahd, scb);
                        ahd_qinfifo_requeue_tail(ahd, scb);
                        printerror = 0;
                } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_IDE_BUSFREE) != 0
@@ -2306,15 +2389,14 @@ ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
                         */
                        printf("%s: ", ahd_name(ahd));
                }
-               if (lastphase != P_BUSFREE)
-                       ahd_force_renegotiation(ahd, &devinfo);
                printf("Unexpected busfree %s, %d SCBs aborted, "
                       "PRGMCNT == 0x%x\n",
                       ahd_lookup_phase_entry(lastphase)->phasemsg,
                       aborted,
-                      ahd_inb(ahd, PRGMCNT)
-                       | (ahd_inb(ahd, PRGMCNT+1) << 8));
+                      ahd_inw(ahd, PRGMCNT));
                ahd_dump_card_state(ahd);
+               if (lastphase != P_BUSFREE)
+                       ahd_force_renegotiation(ahd, &devinfo);
        }
        /* Always restart the sequencer. */
        return (1);
@@ -2476,8 +2558,7 @@ ahd_clear_critical_section(struct ahd_softc *ahd)
                u_int   i;
 
                ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
-               seqaddr = ahd_inb(ahd, CURADDR)
-                       | (ahd_inb(ahd, CURADDR+1) << 8);
+               seqaddr = ahd_inw(ahd, CURADDR);
 
                cs = ahd->critical_sections;
                for (i = 0; i < ahd->num_critical_sections; i++, cs++) {
@@ -3198,14 +3279,25 @@ ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
                iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;
 
                if ((ahd->features & AHD_NEW_IOCELL_OPTS) != 0
-                && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0) {
+                && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0
+                && (ppr_opts & MSG_EXT_PPR_IU_REQ) == 0) {
                        /*
                         * Slow down our CRC interval to be
-                        * compatible with devices that can't
-                        * handle a CRC at full speed.
+                        * compatible with non-packetized
+                        * U160 devices that can't handle a
+                        * CRC at full speed.
                         */
                        con_opts |= ENSLOWCRC;
                }
+
+               if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
+                       /*
+                        * On H2A4, revert to a slower slewrate
+                        * on non-paced transfers.
+                        */
+                       iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
+                           ~AHD_SLEWRATE_MASK;
+               }
        }
 
        ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PRECOMP_SLEW);
@@ -3220,6 +3312,15 @@ ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
        if (tinfo->width == MSG_EXT_WDTR_BUS_16_BIT)
                con_opts |= WIDEXFER;
 
+       /*
+        * Slow down our CRC interval to be
+        * compatible with packetized U320 devices
+        * that can't handle a CRC at full speed
+        */
+       if (ahd->features & AHD_AIC79XXB_SLOWCRC) {
+               con_opts |= ENSLOWCRC;
+       }
+
        /*
         * During packetized transfers, the target will
         * give us the oportunity to send command packets
@@ -3243,7 +3344,6 @@ ahd_update_pending_scbs(struct ahd_softc *ahd)
 {
        struct          scb *pending_scb;
        int             pending_scb_count;
-       u_int           scb_tag;
        int             paused;
        u_int           saved_scbptr;
        ahd_mode_state  saved_modes;
@@ -3261,7 +3361,6 @@ ahd_update_pending_scbs(struct ahd_softc *ahd)
        pending_scb_count = 0;
        LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
                struct ahd_devinfo devinfo;
-               struct hardware_scb *pending_hscb;
                struct ahd_initiator_tinfo *tinfo;
                struct ahd_tmode_tstate *tstate;
 
@@ -3269,11 +3368,10 @@ ahd_update_pending_scbs(struct ahd_softc *ahd)
                tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
                                            devinfo.our_scsiid,
                                            devinfo.target, &tstate);
-               pending_hscb = pending_scb->hscb;
                if ((tstate->auto_negotiate & devinfo.target_mask) == 0
                 && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
                        pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
-                       pending_hscb->control &= ~MK_MESSAGE;
+                       pending_scb->hscb->control &= ~MK_MESSAGE;
                }
                ahd_sync_scb(ahd, pending_scb,
                             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
@@ -3294,25 +3392,26 @@ ahd_update_pending_scbs(struct ahd_softc *ahd)
         * Force the sequencer to reinitialize the selection for
         * the command at the head of the execution queue if it
         * has already been setup.  The negotiation changes may
-        * effect whether we select-out with ATN.
+        * effect whether we select-out with ATN.  It is only
+        * safe to clear ENSELO when the bus is not free and no
+        * selection is in progres or completed.
         */
        saved_modes = ahd_save_modes(ahd);
        ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
-       ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
+       if ((ahd_inb(ahd, SCSISIGI) & BSYI) != 0
+        && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0)
+               ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
        saved_scbptr = ahd_get_scbptr(ahd);
        /* Ensure that the hscbs down on the card match the new information */
-       for (scb_tag = 0; scb_tag < ahd->scb_data.maxhscbs; scb_tag++) {
-               struct  hardware_scb *pending_hscb;
+       LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
+               u_int   scb_tag;
                u_int   control;
 
-               pending_scb = ahd_lookup_scb(ahd, scb_tag);
-               if (pending_scb == NULL)
-                       continue;
+               scb_tag = SCB_GET_TAG(pending_scb);
                ahd_set_scbptr(ahd, scb_tag);
-               pending_hscb = pending_scb->hscb;
                control = ahd_inb_scbram(ahd, SCB_CONTROL);
                control &= ~MK_MESSAGE;
-               control |= pending_hscb->control & MK_MESSAGE;
+               control |= pending_scb->hscb->control & MK_MESSAGE;
                ahd_outb(ahd, SCB_CONTROL, control);
        }
        ahd_set_scbptr(ahd, saved_scbptr);
@@ -3644,11 +3743,8 @@ ahd_construct_sdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
 {
        if (offset == 0)
                period = AHD_ASYNC_XFER_PERIOD;
-       ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
-       ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR_LEN;
-       ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR;
-       ahd->msgout_buf[ahd->msgout_index++] = period;
-       ahd->msgout_buf[ahd->msgout_index++] = offset;
+       ahd->msgout_index += spi_populate_sync_msg(
+                       ahd->msgout_buf + ahd->msgout_index, period, offset);
        ahd->msgout_len += 5;
        if (bootverbose) {
                printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
@@ -3665,10 +3761,8 @@ static void
 ahd_construct_wdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
                   u_int bus_width)
 {
-       ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
-       ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR_LEN;
-       ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR;
-       ahd->msgout_buf[ahd->msgout_index++] = bus_width;
+       ahd->msgout_index += spi_populate_width_msg(
+                       ahd->msgout_buf + ahd->msgout_index, bus_width);
        ahd->msgout_len += 4;
        if (bootverbose) {
                printf("(%s:%c:%d:%d): Sending WDTR %x\n",
@@ -3695,14 +3789,9 @@ ahd_construct_ppr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
                ppr_options |= MSG_EXT_PPR_PCOMP_EN;
        if (offset == 0)
                period = AHD_ASYNC_XFER_PERIOD;
-       ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
-       ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR_LEN;
-       ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR;
-       ahd->msgout_buf[ahd->msgout_index++] = period;
-       ahd->msgout_buf[ahd->msgout_index++] = 0;
-       ahd->msgout_buf[ahd->msgout_index++] = offset;
-       ahd->msgout_buf[ahd->msgout_index++] = bus_width;
-       ahd->msgout_buf[ahd->msgout_index++] = ppr_options;
+       ahd->msgout_index += spi_populate_ppr_msg(
+                       ahd->msgout_buf + ahd->msgout_index, period, offset,
+                       bus_width, ppr_options);
        ahd->msgout_len += 8;
        if (bootverbose) {
                printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
@@ -4911,10 +5000,7 @@ ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
         * Determine initial values for data_addr and data_cnt
         * for resuming the data phase.
         */
-       sgptr = (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 3) << 24)
-             | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 2) << 16)
-             | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 1) << 8)
-             | ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
+       sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
        sgptr &= SG_PTR_MASK;
 
        resid = (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 2) << 16)
@@ -4932,10 +5018,7 @@ ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
                dataptr = ahd_le64toh(sg->addr)
                        + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
                        - resid;
-               ahd_outb(ahd, HADDR + 7, dataptr >> 56);
-               ahd_outb(ahd, HADDR + 6, dataptr >> 48);
-               ahd_outb(ahd, HADDR + 5, dataptr >> 40);
-               ahd_outb(ahd, HADDR + 4, dataptr >> 32);
+               ahd_outl(ahd, HADDR + 4, dataptr >> 32);
        } else {
                struct   ahd_dma_seg *sg;
 
@@ -4950,10 +5033,7 @@ ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
                ahd_outb(ahd, HADDR + 4,
                         (ahd_le32toh(sg->len) & ~AHD_SG_LEN_MASK) >> 24);
        }
-       ahd_outb(ahd, HADDR + 3, dataptr >> 24);
-       ahd_outb(ahd, HADDR + 2, dataptr >> 16);
-       ahd_outb(ahd, HADDR + 1, dataptr >> 8);
-       ahd_outb(ahd, HADDR, dataptr);
+       ahd_outl(ahd, HADDR, dataptr);
        ahd_outb(ahd, HCNT + 2, resid >> 16);
        ahd_outb(ahd, HCNT + 1, resid >> 8);
        ahd_outb(ahd, HCNT, resid);
@@ -5013,13 +5093,14 @@ ahd_handle_devreset(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
        ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
                      AHD_TRANS_CUR, /*paused*/TRUE);
        ahd_set_syncrate(ahd, devinfo, /*period*/0, /*offset*/0,
-                        /*ppr_options*/0, AHD_TRANS_CUR, /*paused*/TRUE);
+                        /*ppr_options*/0, AHD_TRANS_CUR,
+                        /*paused*/TRUE);
        
-       ahd_send_async(ahd, devinfo->channel, devinfo->target,
-                      lun, AC_SENT_BDR, NULL);
+       if (status != CAM_SEL_TIMEOUT)
+               ahd_send_async(ahd, devinfo->channel, devinfo->target,
+                              CAM_LUN_WILDCARD, AC_SENT_BDR, NULL);
 
-       if (message != NULL
-        && (verbose_level <= bootverbose))
+       if (message != NULL && bootverbose)
                printf("%s: %s on %c:%d. %d SCBs aborted\n", ahd_name(ahd),
                       message, devinfo->channel, devinfo->target, found);
 }
@@ -5179,74 +5260,6 @@ ahd_softc_init(struct ahd_softc *ahd)
        return (0);
 }
 
-void
-ahd_softc_insert(struct ahd_softc *ahd)
-{
-       struct ahd_softc *list_ahd;
-
-#if AHD_PCI_CONFIG > 0
-       /*
-        * Second Function PCI devices need to inherit some
-        * settings from function 0.
-        */
-       if ((ahd->features & AHD_MULTI_FUNC) != 0) {
-               TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
-                       ahd_dev_softc_t list_pci;
-                       ahd_dev_softc_t pci;
-
-                       list_pci = list_ahd->dev_softc;
-                       pci = ahd->dev_softc;
-                       if (ahd_get_pci_slot(list_pci) == ahd_get_pci_slot(pci)
-                        && ahd_get_pci_bus(list_pci) == ahd_get_pci_bus(pci)) {
-                               struct ahd_softc *master;
-                               struct ahd_softc *slave;
-
-                               if (ahd_get_pci_function(list_pci) == 0) {
-                                       master = list_ahd;
-                                       slave = ahd;
-                               } else {
-                                       master = ahd;
-                                       slave = list_ahd;
-                               }
-                               slave->flags &= ~AHD_BIOS_ENABLED; 
-                               slave->flags |=
-                                   master->flags & AHD_BIOS_ENABLED;
-                               break;
-                       }
-               }
-       }
-#endif
-
-       /*
-        * Insertion sort into our list of softcs.
-        */
-       list_ahd = TAILQ_FIRST(&ahd_tailq);
-       while (list_ahd != NULL
-           && ahd_softc_comp(ahd, list_ahd) <= 0)
-               list_ahd = TAILQ_NEXT(list_ahd, links);
-       if (list_ahd != NULL)
-               TAILQ_INSERT_BEFORE(list_ahd, ahd, links);
-       else
-               TAILQ_INSERT_TAIL(&ahd_tailq, ahd, links);
-       ahd->init_level++;
-}
-
-/*
- * Verify that the passed in softc pointer is for a
- * controller that is still configured.
- */
-struct ahd_softc *
-ahd_find_softc(struct ahd_softc *ahd)
-{
-       struct ahd_softc *list_ahd;
-
-       TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
-               if (list_ahd == ahd)
-                       return (ahd);
-       }
-       return (NULL);
-}
-
 void
 ahd_set_unit(struct ahd_softc *ahd, int unit)
 {
@@ -5273,13 +5286,13 @@ ahd_free(struct ahd_softc *ahd)
                /* FALLTHROUGH */
        case 4:
                ahd_dmamap_unload(ahd, ahd->shared_data_dmat,
-                                 ahd->shared_data_dmamap);
+                                 ahd->shared_data_map.dmamap);
                /* FALLTHROUGH */
        case 3:
                ahd_dmamem_free(ahd, ahd->shared_data_dmat, ahd->qoutfifo,
-                               ahd->shared_data_dmamap);
+                               ahd->shared_data_map.dmamap);
                ahd_dmamap_destroy(ahd, ahd->shared_data_dmat,
-                                  ahd->shared_data_dmamap);
+                                  ahd->shared_data_map.dmamap);
                /* FALLTHROUGH */
        case 2:
                ahd_dma_tag_destroy(ahd, ahd->shared_data_dmat);
@@ -6045,16 +6058,13 @@ ahd_alloc_scbs(struct ahd_softc *ahd)
        newcount = MIN(scb_data->sense_left, scb_data->scbs_left);
        newcount = MIN(newcount, scb_data->sgs_left);
        newcount = MIN(newcount, (AHD_SCB_MAX_ALLOC - scb_data->numscbs));
-       scb_data->sense_left -= newcount;
-       scb_data->scbs_left -= newcount;
-       scb_data->sgs_left -= newcount;
        for (i = 0; i < newcount; i++) {
-               u_int col_tag;
-
                struct scb_platform_data *pdata;
+               u_int col_tag;
 #ifndef __linux__
                int error;
 #endif
+
                next_scb = (struct scb *)malloc(sizeof(*next_scb),
                                                M_DEVBUF, M_NOWAIT);
                if (next_scb == NULL)
@@ -6111,6 +6121,9 @@ ahd_alloc_scbs(struct ahd_softc *ahd)
                sense_data += AHD_SENSE_BUFSIZE;
                sense_busaddr += AHD_SENSE_BUFSIZE;
                scb_data->numscbs++;
+               scb_data->sense_left--;
+               scb_data->scbs_left--;
+               scb_data->sgs_left--;
        }
 }
 
@@ -6158,7 +6171,6 @@ static const char *termstat_strings[] = {
 int
 ahd_init(struct ahd_softc *ahd)
 {
-       uint8_t         *base_vaddr;
        uint8_t         *next_vaddr;
        dma_addr_t       next_baddr;
        size_t           driver_data_size;
@@ -6226,7 +6238,7 @@ ahd_init(struct ahd_softc *ahd)
         * for the target mode role, we must additionally provide space for
         * the incoming target command fifo.
         */
-       driver_data_size = AHD_SCB_MAX * sizeof(uint16_t)
+       driver_data_size = AHD_SCB_MAX * sizeof(*ahd->qoutfifo)
                         + sizeof(struct hardware_scb);
        if ((ahd->features & AHD_TARGETMODE) != 0)
                driver_data_size += AHD_TMODE_CMDS * sizeof(struct target_cmd);
@@ -6248,20 +6260,23 @@ ahd_init(struct ahd_softc *ahd)
 
        /* Allocation of driver data */
        if (ahd_dmamem_alloc(ahd, ahd->shared_data_dmat,
-                            (void **)&base_vaddr,
-                            BUS_DMA_NOWAIT, &ahd->shared_data_dmamap) != 0) {
+                            (void **)&ahd->shared_data_map.vaddr,
+                            BUS_DMA_NOWAIT,
+                            &ahd->shared_data_map.dmamap) != 0) {
                return (ENOMEM);
        }
 
        ahd->init_level++;
 
        /* And permanently map it in */
-       ahd_dmamap_load(ahd, ahd->shared_data_dmat, ahd->shared_data_dmamap,
-                       base_vaddr, driver_data_size, ahd_dmamap_cb,
-                       &ahd->shared_data_busaddr, /*flags*/0);
-       ahd->qoutfifo = (uint16_t *)base_vaddr;
+       ahd_dmamap_load(ahd, ahd->shared_data_dmat, ahd->shared_data_map.dmamap,
+                       ahd->shared_data_map.vaddr, driver_data_size,
+                       ahd_dmamap_cb, &ahd->shared_data_map.physaddr,
+                       /*flags*/0);
+       ahd->qoutfifo = (struct ahd_completion *)ahd->shared_data_map.vaddr;
        next_vaddr = (uint8_t *)&ahd->qoutfifo[AHD_QOUT_SIZE];
-       next_baddr = ahd->shared_data_busaddr + AHD_QOUT_SIZE*sizeof(uint16_t);
+       next_baddr = ahd->shared_data_map.physaddr
+                  + AHD_QOUT_SIZE*sizeof(struct ahd_completion);
        if ((ahd->features & AHD_TARGETMODE) != 0) {
                ahd->targetcmds = (struct target_cmd *)next_vaddr;
                next_vaddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
@@ -6282,6 +6297,7 @@ ahd_init(struct ahd_softc *ahd)
         * specially from the DMA safe memory chunk used for the QOUTFIFO.
         */
        ahd->next_queued_hscb = (struct hardware_scb *)next_vaddr;
+       ahd->next_queued_hscb_map = &ahd->shared_data_map;
        ahd->next_queued_hscb->hscb_busaddr = ahd_htole32(next_baddr);
 
        ahd->init_level++;
@@ -6497,13 +6513,14 @@ ahd_chip_init(struct ahd_softc *ahd)
                              | ENLQIOVERI_LQ|ENLQIOVERI_NLQ);
        ahd_outb(ahd, LQOMODE0, ENLQOATNLQ|ENLQOATNPKT|ENLQOTCRC);
        /*
-        * An interrupt from LQOBUSFREE is made redundant by the
-        * BUSFREE interrupt.  We choose to have the sequencer catch
-        * LQOPHCHGINPKT errors manually for the command phase at the
-        * start of a packetized selection case.
-       ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE|ENLQOPHACHGINPKT);
+        * We choose to have the sequencer catch LQOPHCHGINPKT errors
+        * manually for the command phase at the start of a packetized
+        * selection case.  ENLQOBUSFREE should be made redundant by
+        * the BUSFREE interrupt, but it seems that some LQOBUSFREE
+        * events fail to assert the BUSFREE interrupt so we must
+        * also enable LQOBUSFREE interrupts.
         */
-       ahd_outb(ahd, LQOMODE1, 0);
+       ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE);
 
        /*
         * Setup sequencer interrupt handlers.
@@ -6587,10 +6604,10 @@ ahd_chip_init(struct ahd_softc *ahd)
 
        /* All of our queues are empty */
        ahd->qoutfifonext = 0;
-       ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID_LE;
-       ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID >> 8);
+       ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID;
+       ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID);
        for (i = 0; i < AHD_QOUT_SIZE; i++)
-               ahd->qoutfifo[i] = 0;
+               ahd->qoutfifo[i].valid_tag = 0;
        ahd_sync_qoutfifo(ahd, BUS_DMASYNC_PREREAD);
 
        ahd->qinfifonext = 0;
@@ -6614,6 +6631,8 @@ ahd_chip_init(struct ahd_softc *ahd)
        /* We don't have any waiting selections */
        ahd_outw(ahd, WAITING_TID_HEAD, SCB_LIST_NULL);
        ahd_outw(ahd, WAITING_TID_TAIL, SCB_LIST_NULL);
+       ahd_outw(ahd, MK_MESSAGE_SCB, SCB_LIST_NULL);
+       ahd_outw(ahd, MK_MESSAGE_SCSIID, 0xFF);
        for (i = 0; i < AHD_NUM_TARGETS; i++)
                ahd_outw(ahd, WAITING_SCB_TAILS + (2 * i), SCB_LIST_NULL);
 
@@ -6623,24 +6642,22 @@ ahd_chip_init(struct ahd_softc *ahd)
        ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
        ahd_outw(ahd, COMPLETE_SCB_DMAINPROG_HEAD, SCB_LIST_NULL);
        ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
+       ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
+       ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
 
        /*
         * The Freeze Count is 0.
         */
+       ahd->qfreeze_cnt = 0;
        ahd_outw(ahd, QFREEZE_COUNT, 0);
+       ahd_outw(ahd, KERNEL_QFREEZE_COUNT, 0);
 
        /*
         * Tell the sequencer where it can find our arrays in memory.
         */
-       busaddr = ahd->shared_data_busaddr;
-       ahd_outb(ahd, SHARED_DATA_ADDR, busaddr & 0xFF);
-       ahd_outb(ahd, SHARED_DATA_ADDR + 1, (busaddr >> 8) & 0xFF);
-       ahd_outb(ahd, SHARED_DATA_ADDR + 2, (busaddr >> 16) & 0xFF);
-       ahd_outb(ahd, SHARED_DATA_ADDR + 3, (busaddr >> 24) & 0xFF);
-       ahd_outb(ahd, QOUTFIFO_NEXT_ADDR, busaddr & 0xFF);
-       ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 1, (busaddr >> 8) & 0xFF);
-       ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 2, (busaddr >> 16) & 0xFF);
-       ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 3, (busaddr >> 24) & 0xFF);
+       busaddr = ahd->shared_data_map.physaddr;
+       ahd_outl(ahd, SHARED_DATA_ADDR, busaddr);
+       ahd_outl(ahd, QOUTFIFO_NEXT_ADDR, busaddr);
 
        /*
         * Setup the allowed SCSI Sequences based on operational mode.
@@ -6689,10 +6706,7 @@ ahd_chip_init(struct ahd_softc *ahd)
         * Tell the sequencer which SCB will be the next one it receives.
         */
        busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
-       ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
-       ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
-       ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
-       ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
+       ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
 
        /*
         * Default to coalescing disabled.
@@ -6706,6 +6720,18 @@ ahd_chip_init(struct ahd_softc *ahd)
 
        ahd_loadseq(ahd);
        ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
+
+       if (ahd->features & AHD_AIC79XXB_SLOWCRC) {
+               u_int negodat3 = ahd_inb(ahd, NEGCONOPTS);
+
+               negodat3 |= ENSLOWCRC;
+               ahd_outb(ahd, NEGCONOPTS, negodat3);
+               negodat3 = ahd_inb(ahd, NEGCONOPTS);
+               if (!(negodat3 & ENSLOWCRC))
+                       printf("aic79xx: failed to set the SLOWCRC bit\n");
+               else
+                       printf("aic79xx: SLOWCRC bit set\n");
+       }
 }
 
 /*
@@ -6996,43 +7022,34 @@ ahd_pause_and_flushwork(struct ahd_softc *ahd)
 {
        u_int intstat;
        u_int maxloops;
-       u_int qfreeze_cnt;
 
        maxloops = 1000;
        ahd->flags |= AHD_ALL_INTERRUPTS;
        ahd_pause(ahd);
        /*
-        * Increment the QFreeze Count so that the sequencer
-        * will not start new selections.  We do this only
+        * Freeze the outgoing selections.  We do this only
         * until we are safely paused without further selections
         * pending.
         */
-       ahd_outw(ahd, QFREEZE_COUNT, ahd_inw(ahd, QFREEZE_COUNT) + 1);
+       ahd->qfreeze_cnt--;
+       ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
        ahd_outb(ahd, SEQ_FLAGS2, ahd_inb(ahd, SEQ_FLAGS2) | SELECTOUT_QFROZEN);
        do {
-               struct scb *waiting_scb;
 
                ahd_unpause(ahd);
+               /*
+                * Give the sequencer some time to service
+                * any active selections.
+                */
+               ahd_delay(500);
+
                ahd_intr(ahd);
                ahd_pause(ahd);
-               ahd_clear_critical_section(ahd);
                intstat = ahd_inb(ahd, INTSTAT);
-               ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
-               if ((ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0)
-                       ahd_outb(ahd, SCSISEQ0,
-                                ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
-               /*
-                * In the non-packetized case, the sequencer (for Rev A),
-                * relies on ENSELO remaining set after SELDO.  The hardware
-                * auto-clears ENSELO in the packetized case.
-                */
-               waiting_scb = ahd_lookup_scb(ahd,
-                                            ahd_inw(ahd, WAITING_TID_HEAD));
-               if (waiting_scb != NULL
-                && (waiting_scb->flags & SCB_PACKETIZED) == 0
-                && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0)
-                       ahd_outb(ahd, SCSISEQ0,
-                                ahd_inb(ahd, SCSISEQ0) | ENSELO);
+               if ((intstat & INT_PEND) == 0) {
+                       ahd_clear_critical_section(ahd);
+                       intstat = ahd_inb(ahd, INTSTAT);
+               }
        } while (--maxloops
              && (intstat != 0xFF || (ahd->features & AHD_REMOVABLE) == 0)
              && ((intstat & INT_PEND) != 0
@@ -7043,21 +7060,11 @@ ahd_pause_and_flushwork(struct ahd_softc *ahd)
                printf("Infinite interrupt loop, INTSTAT = %x",
                      ahd_inb(ahd, INTSTAT));
        }
-       qfreeze_cnt = ahd_inw(ahd, QFREEZE_COUNT);
-       if (qfreeze_cnt == 0) {
-               printf("%s: ahd_pause_and_flushwork with 0 qfreeze count!\n",
-                      ahd_name(ahd));
-       } else {
-               qfreeze_cnt--;
-       }
-       ahd_outw(ahd, QFREEZE_COUNT, qfreeze_cnt);
-       if (qfreeze_cnt == 0)
-               ahd_outb(ahd, SEQ_FLAGS2,
-                        ahd_inb(ahd, SEQ_FLAGS2) & ~SELECTOUT_QFROZEN);
+       ahd->qfreeze_cnt++;
+       ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
 
        ahd_flush_qoutfifo(ahd);
 
-       ahd_platform_flushwork(ahd);
        ahd->flags &= ~AHD_ALL_INTERRUPTS;
 }
 
@@ -7225,10 +7232,7 @@ ahd_qinfifo_requeue(struct ahd_softc *ahd, struct scb *prev_scb,
                uint32_t busaddr;
 
                busaddr = ahd_le32toh(scb->hscb->hscb_busaddr);
-               ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
-               ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
-               ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
-               ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
+               ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
        } else {
                prev_scb->hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
                ahd_sync_scb(ahd, prev_scb, 
@@ -7283,12 +7287,28 @@ ahd_reset_cmds_pending(struct ahd_softc *ahd)
        ahd->flags &= ~AHD_UPDATE_PEND_CMDS;
 }
 
+void
+ahd_done_with_status(struct ahd_softc *ahd, struct scb *scb, uint32_t status)
+{
+       cam_status ostat;
+       cam_status cstat;
+
+       ostat = ahd_get_transaction_status(scb);
+       if (ostat == CAM_REQ_INPROG)
+               ahd_set_transaction_status(scb, status);
+       cstat = ahd_get_transaction_status(scb);
+       if (cstat != CAM_REQ_CMP)
+               ahd_freeze_scb(scb);
+       ahd_done(ahd, scb);
+}
+
 int
 ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
                   int lun, u_int tag, role_t role, uint32_t status,
                   ahd_search_action action)
 {
        struct scb      *scb;
+       struct scb      *mk_msg_scb;
        struct scb      *prev_scb;
        ahd_mode_state   saved_modes;
        u_int            qinstart;
@@ -7297,6 +7317,7 @@ ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
        u_int            tid_next;
        u_int            tid_prev;
        u_int            scbid;
+       u_int            seq_flags2;
        u_int            savedscbptr;
        uint32_t         busaddr;
        int              found;
@@ -7335,10 +7356,7 @@ ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
         */
        ahd->qinfifonext = qinstart;
        busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
-       ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
-       ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
-       ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
-       ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
+       ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
 
        while (qinpos != qintail) {
                scb = ahd_lookup_scb(ahd, ahd->qinfifo[qinpos]);
@@ -7355,23 +7373,10 @@ ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
                        found++;
                        switch (action) {
                        case SEARCH_COMPLETE:
-                       {
-                               cam_status ostat;
-                               cam_status cstat;
-
-                               ostat = ahd_get_transaction_status(scb);
-                               if (ostat == CAM_REQ_INPROG)
-                                       ahd_set_transaction_status(scb,
-                                                                  status);
-                               cstat = ahd_get_transaction_status(scb);
-                               if (cstat != CAM_REQ_CMP)
-                                       ahd_freeze_scb(scb);
                                if ((scb->flags & SCB_ACTIVE) == 0)
                                        printf("Inactive SCB in qinfifo\n");
-                               ahd_done(ahd, scb);
-
+                               ahd_done_with_status(ahd, scb, status);
                                /* FALLTHROUGH */
-                       }
                        case SEARCH_REMOVE:
                                break;
                        case SEARCH_PRINT:
@@ -7400,21 +7405,25 @@ ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
         * appropriate, traverse the SCBs of each "their id"
         * looking for matches.
         */
+       ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
+       seq_flags2 = ahd_inb(ahd, SEQ_FLAGS2);
+       if ((seq_flags2 & PENDING_MK_MESSAGE) != 0) {
+               scbid = ahd_inw(ahd, MK_MESSAGE_SCB);
+               mk_msg_scb = ahd_lookup_scb(ahd, scbid);
+       } else
+               mk_msg_scb = NULL;
        savedscbptr = ahd_get_scbptr(ahd);
        tid_next = ahd_inw(ahd, WAITING_TID_HEAD);
        tid_prev = SCB_LIST_NULL;
        targets = 0;
        for (scbid = tid_next; !SCBID_IS_NULL(scbid); scbid = tid_next) {
                u_int tid_head;
+               u_int tid_tail;
 
-               /*
-                * We limit based on the number of SCBs since
-                * MK_MESSAGE SCBs are not in the per-tid lists.
-                */
                targets++;
-               if (targets > AHD_SCB_MAX) {
+               if (targets > AHD_NUM_TARGETS)
                        panic("TID LIST LOOP");
-               }
+
                if (scbid >= ahd->scb_data.numscbs) {
                        printf("%s: Waiting TID List inconsistency. "
                               "SCB index == 0x%x, yet numscbs == 0x%x.",
@@ -7444,8 +7453,71 @@ ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
                tid_head = scbid;
                found += ahd_search_scb_list(ahd, target, channel,
                                             lun, tag, role, status,
-                                            action, &tid_head,
+                                            action, &tid_head, &tid_tail,
                                             SCB_GET_TARGET(ahd, scb));
+               /*
+                * Check any MK_MESSAGE SCB that is still waiting to
+                * enter this target's waiting for selection queue.
+                */
+               if (mk_msg_scb != NULL
+                && ahd_match_scb(ahd, mk_msg_scb, target, channel,
+                                 lun, tag, role)) {
+
+                       /*
+                        * We found an scb that needs to be acted on.
+                        */
+                       found++;
+                       switch (action) {
+                       case SEARCH_COMPLETE:
+                               if ((mk_msg_scb->flags & SCB_ACTIVE) == 0)
+                                       printf("Inactive SCB pending MK_MSG\n");
+                               ahd_done_with_status(ahd, mk_msg_scb, status);
+                               /* FALLTHROUGH */
+                       case SEARCH_REMOVE:
+                       {
+                               u_int tail_offset;
+
+                               printf("Removing MK_MSG scb\n");
+
+                               /*
+                                * Reset our tail to the tail of the
+                                * main per-target list.
+                                */
+                               tail_offset = WAITING_SCB_TAILS
+                                   + (2 * SCB_GET_TARGET(ahd, mk_msg_scb));
+                               ahd_outw(ahd, tail_offset, tid_tail);
+
+                               seq_flags2 &= ~PENDING_MK_MESSAGE;
+                               ahd_outb(ahd, SEQ_FLAGS2, seq_flags2);
+                               ahd_outw(ahd, CMDS_PENDING,
+                                        ahd_inw(ahd, CMDS_PENDING)-1);
+                               mk_msg_scb = NULL;
+                               break;
+                       }
+                       case SEARCH_PRINT:
+                               printf(" 0x%x", SCB_GET_TAG(scb));
+                               /* FALLTHROUGH */
+                       case SEARCH_COUNT:
+                               break;
+                       }
+               }
+
+               if (mk_msg_scb != NULL
+                && SCBID_IS_NULL(tid_head)
+                && ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
+                                 SCB_LIST_NULL, ROLE_UNKNOWN)) {
+
+                       /*
+                        * When removing the last SCB for a target
+                        * queue with a pending MK_MESSAGE scb, we
+                        * must queue the MK_MESSAGE scb.
+                        */
+                       printf("Queueing mk_msg_scb\n");
+                       tid_head = ahd_inw(ahd, MK_MESSAGE_SCB);
+                       seq_flags2 &= ~PENDING_MK_MESSAGE;
+                       ahd_outb(ahd, SEQ_FLAGS2, seq_flags2);
+                       mk_msg_scb = NULL;
+               }
                if (tid_head != scbid)
                        ahd_stitch_tid_list(ahd, tid_prev, tid_head, tid_next);
                if (!SCBID_IS_NULL(tid_head))
@@ -7453,6 +7525,8 @@ ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
                if (action == SEARCH_PRINT)
                        printf(")\n");
        }
+
+       /* Restore saved state. */
        ahd_set_scbptr(ahd, savedscbptr);
        ahd_restore_modes(ahd, saved_modes);
        return (found);
@@ -7461,7 +7535,8 @@ ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
 static int
 ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
                    int lun, u_int tag, role_t role, uint32_t status,
-                   ahd_search_action action, u_int *list_head, u_int tid)
+                   ahd_search_action action, u_int *list_head, 
+                   u_int *list_tail, u_int tid)
 {
        struct  scb *scb;
        u_int   scbid;
@@ -7469,10 +7544,11 @@ ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
        u_int   prev;
        int     found;
 
-       AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
+       AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
        found = 0;
        prev = SCB_LIST_NULL;
        next = *list_head;
+       *list_tail = SCB_LIST_NULL;
        for (scbid = next; !SCBID_IS_NULL(scbid); scbid = next) {
                if (scbid >= ahd->scb_data.numscbs) {
                        printf("%s:SCB List inconsistency. "
@@ -7488,6 +7564,7 @@ ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
                        panic("Waiting List traversal\n");
                }
                ahd_set_scbptr(ahd, scbid);
+               *list_tail = scbid;
                next = ahd_inw_scbram(ahd, SCB_NEXT);
                if (ahd_match_scb(ahd, scb, target, channel,
                                  lun, SCB_LIST_NULL, role) == 0) {
@@ -7497,24 +7574,14 @@ ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
                found++;
                switch (action) {
                case SEARCH_COMPLETE:
-               {
-                       cam_status ostat;
-                       cam_status cstat;
-
-                       ostat = ahd_get_transaction_status(scb);
-                       if (ostat == CAM_REQ_INPROG)
-                               ahd_set_transaction_status(scb, status);
-                       cstat = ahd_get_transaction_status(scb);
-                       if (cstat != CAM_REQ_CMP)
-                               ahd_freeze_scb(scb);
                        if ((scb->flags & SCB_ACTIVE) == 0)
                                printf("Inactive SCB in Waiting List\n");
-                       ahd_done(ahd, scb);
+                       ahd_done_with_status(ahd, scb, status);
                        /* FALLTHROUGH */
-               }
                case SEARCH_REMOVE:
                        ahd_rem_wscb(ahd, scbid, prev, next, tid);
-                       if (prev == SCB_LIST_NULL)
+                       *list_tail = prev;
+                       if (SCBID_IS_NULL(prev))
                                *list_head = next;
                        break;
                case SEARCH_PRINT:
@@ -7536,7 +7603,7 @@ static void
 ahd_stitch_tid_list(struct ahd_softc *ahd, u_int tid_prev,
                    u_int tid_cur, u_int tid_next)
 {
-       AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
+       AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
 
        if (SCBID_IS_NULL(tid_cur)) {
 
@@ -7576,21 +7643,24 @@ ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
 {
        u_int tail_offset;
 
-       AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
+       AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
        if (!SCBID_IS_NULL(prev)) {
                ahd_set_scbptr(ahd, prev);
                ahd_outw(ahd, SCB_NEXT, next);
        }
 
        /*
-        * SCBs that had MK_MESSAGE set in them will not
-        * be queued to the per-target lists, so don't
-        * blindly clear the tail pointer.
+        * SCBs that have MK_MESSAGE set in them may
+        * cause the tail pointer to be updated without
+        * setting the next pointer of the previous tail.
+        * Only clear the tail if the removed SCB was
+        * the tail.
         */
        tail_offset = WAITING_SCB_TAILS + (2 * tid);
        if (SCBID_IS_NULL(next)
         && ahd_inw(ahd, tail_offset) == scbid)
                ahd_outw(ahd, tail_offset, prev);
+
        ahd_add_scb_to_free_list(ahd, scbid);
        return (next);
 }
@@ -7754,6 +7824,17 @@ ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
        int     found;
        u_int   fifo;
        u_int   next_fifo;
+       uint8_t scsiseq;
+
+       /*
+        * Check if the last bus reset is cleared
+        */
+       if (ahd->flags & AHD_BUS_RESET_ACTIVE) {
+               printf("%s: bus reset still active\n",
+                      ahd_name(ahd));
+               return 0;
+       }
+       ahd->flags |= AHD_BUS_RESET_ACTIVE;
 
        ahd->pending_device = NULL;
 
@@ -7767,6 +7848,12 @@ ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
        /* Make sure the sequencer is in a safe location. */
        ahd_clear_critical_section(ahd);
 
+       /*
+        * Run our command complete fifos to ensure that we perform
+        * completion processing on any commands that 'completed'
+        * before the reset occurred.
+        */
+       ahd_run_qoutfifo(ahd);
 #ifdef AHD_TARGET_MODE
        if ((ahd->flags & AHD_TARGETROLE) != 0) {
                ahd_run_tqinfifo(ahd, /*paused*/TRUE);
@@ -7809,7 +7896,7 @@ ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
         */
        ahd_clear_msg_state(ahd);
        ahd_outb(ahd, SIMODE1,
-                ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST|ENBUSFREE));
+                ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST));
 
        if (initiate_reset)
                ahd_reset_current_bus(ahd);
@@ -7831,30 +7918,14 @@ ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
        ahd_clear_fifo(ahd, 1);
 
        /*
-        * Revert to async/narrow transfers until we renegotiate.
+        * Reenable selections
         */
-       max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
-       for (target = 0; target <= max_scsiid; target++) {
-
-               if (ahd->enabled_targets[target] == NULL)
-                       continue;
-               for (initiator = 0; initiator <= max_scsiid; initiator++) {
-                       struct ahd_devinfo devinfo;
-
-                       ahd_compile_devinfo(&devinfo, target, initiator,
-                                           CAM_LUN_WILDCARD,
-                                           'A', ROLE_UNKNOWN);
-                       ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
-                                     AHD_TRANS_CUR, /*paused*/TRUE);
-                       ahd_set_syncrate(ahd, &devinfo, /*period*/0,
-                                        /*offset*/0, /*ppr_options*/0,
-                                        AHD_TRANS_CUR, /*paused*/TRUE);
-               }
-       }
+       ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST);
+       scsiseq = ahd_inb(ahd, SCSISEQ_TEMPLATE);
+       ahd_outb(ahd, SCSISEQ1, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
 
-#ifdef AHD_TARGET_MODE
        max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
-
+#ifdef AHD_TARGET_MODE
        /*
         * Send an immediate notify ccb to all target more peripheral
         * drivers affected by this action.
@@ -7882,79 +7953,41 @@ ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
        /* Notify the XPT that a bus reset occurred */
        ahd_send_async(ahd, devinfo.channel, CAM_TARGET_WILDCARD,
                       CAM_LUN_WILDCARD, AC_BUS_RESET, NULL);
-       ahd_restart(ahd);
+
        /*
-        * Freeze the SIMQ until our poller can determine that
-        * the bus reset has really gone away.  We set the initial
-        * timer to 0 to have the check performed as soon as possible
-        * from the timer context.
+        * Revert to async/narrow transfers until we renegotiate.
         */
-       if ((ahd->flags & AHD_RESET_POLL_ACTIVE) == 0) {
-               ahd->flags |= AHD_RESET_POLL_ACTIVE;
-               ahd_freeze_simq(ahd);
-               ahd_timer_reset(&ahd->reset_timer, 0, ahd_reset_poll, ahd);
-       }
-       return (found);
-}
+       for (target = 0; target <= max_scsiid; target++) {
 
+               if (ahd->enabled_targets[target] == NULL)
+                       continue;
+               for (initiator = 0; initiator <= max_scsiid; initiator++) {
+                       struct ahd_devinfo devinfo;
 
-#define AHD_RESET_POLL_US 1000
-static void
-ahd_reset_poll(void *arg)
-{
-       struct  ahd_softc *ahd;
-       u_int   scsiseq1;
-       u_long  l;
-       u_long  s;
-       
-       ahd_list_lock(&l);
-       ahd = ahd_find_softc((struct ahd_softc *)arg);
-       if (ahd == NULL) {
-               printf("ahd_reset_poll: Instance %p no longer exists\n", arg);
-               ahd_list_unlock(&l);
-               return;
-       }
-       ahd_lock(ahd, &s);
-       ahd_pause(ahd);
-       ahd_update_modes(ahd);
-       ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
-       ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
-       if ((ahd_inb(ahd, SSTAT1) & SCSIRSTI) != 0) {
-               ahd_timer_reset(&ahd->reset_timer, AHD_RESET_POLL_US,
-                               ahd_reset_poll, ahd);
-               ahd_unpause(ahd);
-               ahd_unlock(ahd, &s);
-               ahd_list_unlock(&l);
-               return;
+                       ahd_compile_devinfo(&devinfo, target, initiator,
+                                           CAM_LUN_WILDCARD,
+                                           'A', ROLE_UNKNOWN);
+                       ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
+                                     AHD_TRANS_CUR, /*paused*/TRUE);
+                       ahd_set_syncrate(ahd, &devinfo, /*period*/0,
+                                        /*offset*/0, /*ppr_options*/0,
+                                        AHD_TRANS_CUR, /*paused*/TRUE);
+               }
        }
 
-       /* Reset is now low.  Complete chip reinitialization. */
-       ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST);
-       scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
-       ahd_outb(ahd, SCSISEQ1, scsiseq1 & (ENSELI|ENRSELI|ENAUTOATNP));
-       ahd_unpause(ahd);
-       ahd->flags &= ~AHD_RESET_POLL_ACTIVE;
-       ahd_unlock(ahd, &s);
-       ahd_release_simq(ahd);
-       ahd_list_unlock(&l);
+       ahd_restart(ahd);
+
+       return (found);
 }
 
 /**************************** Statistics Processing ***************************/
 static void
 ahd_stat_timer(void *arg)
 {
-       struct  ahd_softc *ahd;
-       u_long  l;
+       struct  ahd_softc *ahd = arg;
        u_long  s;
        int     enint_coal;
        
-       ahd_list_lock(&l);
-       ahd = ahd_find_softc((struct ahd_softc *)arg);
-       if (ahd == NULL) {
-               printf("ahd_stat_timer: Instance %p no longer exists\n", arg);
-               ahd_list_unlock(&l);
-               return;
-       }
        ahd_lock(ahd, &s);
 
        enint_coal = ahd->hs_mailbox & ENINT_COALESCE;
@@ -7981,7 +8014,6 @@ ahd_stat_timer(void *arg)
        ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
                        ahd_stat_timer, ahd);
        ahd_unlock(ahd, &s);
-       ahd_list_unlock(&l);
 }
 
 /****************************** Status Processing *****************************/
@@ -7999,30 +8031,35 @@ ahd_handle_scb_status(struct ahd_softc *ahd, struct scb *scb)
 void
 ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb)
 {
-       struct hardware_scb *hscb;
-       u_int  qfreeze_cnt;
+       struct  hardware_scb *hscb;
+       int     paused;
 
        /*
         * The sequencer freezes its select-out queue
         * anytime a SCSI status error occurs.  We must
-        * handle the error and decrement the QFREEZE count
-        * to allow the sequencer to continue.
+        * handle the error and increment our qfreeze count
+        * to allow the sequencer to continue.  We don't
+        * bother clearing critical sections here since all
+        * operations are on data structures that the sequencer
+        * is not touching once the queue is frozen.
         */
        hscb = scb->hscb; 
 
+       if (ahd_is_paused(ahd)) {
+               paused = 1;
+       } else {
+               paused = 0;
+               ahd_pause(ahd);
+       }
+
        /* Freeze the queue until the client sees the error. */
        ahd_freeze_devq(ahd, scb);
        ahd_freeze_scb(scb);
-       qfreeze_cnt = ahd_inw(ahd, QFREEZE_COUNT);
-       if (qfreeze_cnt == 0) {
-               printf("%s: Bad status with 0 qfreeze count!\n", ahd_name(ahd));
-       } else {
-               qfreeze_cnt--;
-               ahd_outw(ahd, QFREEZE_COUNT, qfreeze_cnt);
-       }
-       if (qfreeze_cnt == 0)
-               ahd_outb(ahd, SEQ_FLAGS2,
-                        ahd_inb(ahd, SEQ_FLAGS2) & ~SELECTOUT_QFROZEN);
+       ahd->qfreeze_cnt++;
+       ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
+
+       if (paused == 0)
+               ahd_unpause(ahd);
 
        /* Don't want to clobber the original sense code */
        if ((scb->flags & SCB_SENSE) != 0) {
@@ -8187,11 +8224,6 @@ ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb)
                ahd_setup_data_scb(ahd, scb);
                scb->flags |= SCB_SENSE;
                ahd_queue_scb(ahd, scb);
-               /*
-                * Ensure we have enough time to actually
-                * retrieve the sense.
-                */
-               ahd_scb_timer_reset(scb, 5 * 1000000);
                break;
        }
        case SCSI_STATUS_OK:
@@ -8406,8 +8438,7 @@ ahd_dumpseq(struct ahd_softc* ahd)
        max_prog = 2048;
 
        ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
-       ahd_outb(ahd, PRGMCNT, 0);
-       ahd_outb(ahd, PRGMCNT+1, 0);
+       ahd_outw(ahd, PRGMCNT, 0);
        for (i = 0; i < max_prog; i++) {
                uint8_t ins_bytes[4];
 
@@ -8436,13 +8467,14 @@ ahd_loadseq(struct ahd_softc *ahd)
        u_int   sg_prefetch_cnt_limit;
        u_int   sg_prefetch_align;
        u_int   sg_size;
+       u_int   cacheline_mask;
        uint8_t download_consts[DOWNLOAD_CONST_COUNT];
 
        if (bootverbose)
                printf("%s: Downloading Sequencer Program...",
                       ahd_name(ahd));
 
-#if DOWNLOAD_CONST_COUNT != 7
+#if DOWNLOAD_CONST_COUNT != 8
 #error "Download Const Mismatch"
 #endif
        /*
@@ -8478,6 +8510,9 @@ ahd_loadseq(struct ahd_softc *ahd)
        /* Round down to the nearest power of 2. */
        while (powerof2(sg_prefetch_align) == 0)
                sg_prefetch_align--;
+
+       cacheline_mask = sg_prefetch_align - 1;
+
        /*
         * If the cacheline boundary is greater than half our prefetch RAM
         * we risk not being able to fetch even a single complete S/G
@@ -8518,12 +8553,12 @@ ahd_loadseq(struct ahd_softc *ahd)
        download_consts[PKT_OVERRUN_BUFOFFSET] =
                (ahd->overrun_buf - (uint8_t *)ahd->qoutfifo) / 256;
        download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_1BYTE_LUN;
+       download_consts[CACHELINE_MASK] = cacheline_mask;
        cur_patch = patches;
        downloaded = 0;
        skip_addr = 0;
        ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
-       ahd_outb(ahd, PRGMCNT, 0);
-       ahd_outb(ahd, PRGMCNT+1, 0);
+       ahd_outw(ahd, PRGMCNT, 0);
 
        for (i = 0; i < sizeof(seqprog)/4; i++) {
                if (ahd_check_patch(ahd, &cur_patch, i, &skip_addr) == 0) {
@@ -8745,16 +8780,6 @@ sized:
        return (last_probe);
 }
 
-void
-ahd_dump_all_cards_state(void)
-{
-       struct ahd_softc *list_ahd;
-
-       TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
-               ahd_dump_card_state(list_ahd);
-       }
-}
-
 int
 ahd_print_register(ahd_reg_parse_entry_t *table, u_int num_entries,
                   const char *name, u_int address, u_int value,
@@ -8826,7 +8851,7 @@ ahd_dump_card_state(struct ahd_softc *ahd)
        printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
               "%s: Dumping Card State at program address 0x%x Mode 0x%x\n",
               ahd_name(ahd), 
-              ahd_inb(ahd, CURADDR) | (ahd_inb(ahd, CURADDR+1) << 8),
+              ahd_inw(ahd, CURADDR),
               ahd_build_mode_state(ahd, ahd->saved_src_mode,
                                    ahd->saved_dst_mode));
        if (paused)
@@ -8839,6 +8864,9 @@ ahd_dump_card_state(struct ahd_softc *ahd)
         * Mode independent registers.
         */
        cur_col = 0;
+       ahd_intstat_print(ahd_inb(ahd, INTSTAT), &cur_col, 50);
+       ahd_seloid_print(ahd_inb(ahd, SELOID), &cur_col, 50);
+       ahd_selid_print(ahd_inb(ahd, SELID), &cur_col, 50);
        ahd_hs_mailbox_print(ahd_inb(ahd, LOCAL_HS_MAILBOX), &cur_col, 50);
        ahd_intctl_print(ahd_inb(ahd, INTCTL), &cur_col, 50);
        ahd_seqintstat_print(ahd_inb(ahd, SEQINTSTAT), &cur_col, 50);
@@ -8854,6 +8882,12 @@ ahd_dump_card_state(struct ahd_softc *ahd)
        ahd_seqintctl_print(ahd_inb(ahd, SEQINTCTL), &cur_col, 50);
        ahd_seq_flags_print(ahd_inb(ahd, SEQ_FLAGS), &cur_col, 50);
        ahd_seq_flags2_print(ahd_inb(ahd, SEQ_FLAGS2), &cur_col, 50);
+       ahd_qfreeze_count_print(ahd_inw(ahd, QFREEZE_COUNT), &cur_col, 50);
+       ahd_kernel_qfreeze_count_print(ahd_inw(ahd, KERNEL_QFREEZE_COUNT),
+                                      &cur_col, 50);
+       ahd_mk_message_scb_print(ahd_inw(ahd, MK_MESSAGE_SCB), &cur_col, 50);
+       ahd_mk_message_scsiid_print(ahd_inb(ahd, MK_MESSAGE_SCSIID),
+                                   &cur_col, 50);
        ahd_sstat0_print(ahd_inb(ahd, SSTAT0), &cur_col, 50);
        ahd_sstat1_print(ahd_inb(ahd, SSTAT1), &cur_col, 50);
        ahd_sstat2_print(ahd_inb(ahd, SSTAT2), &cur_col, 50);
@@ -8942,6 +8976,15 @@ ahd_dump_card_state(struct ahd_softc *ahd)
                scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
        }
        printf("\n");
+       printf("Sequencer On QFreeze and Complete list: ");
+       scb_index = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
+       i = 0;
+       while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
+               ahd_set_scbptr(ahd, scb_index);
+               printf("%d ", scb_index);
+               scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
+       }
+       printf("\n");
        ahd_set_scbptr(ahd, saved_scb_index);
        dffstat = ahd_inb(ahd, DFFSTAT);
        for (i = 0; i < 2; i++) {
@@ -8952,7 +8995,7 @@ ahd_dump_card_state(struct ahd_softc *ahd)
 
                ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
                fifo_scbptr = ahd_get_scbptr(ahd);
-               printf("\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n",
+               printf("\n\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n",
                       ahd_name(ahd), i,
                       (dffstat & (FIFO0FREE << i)) ? "Free" : "Active",
                       ahd_inw(ahd, LONGJMP_ADDR), fifo_scbptr);
@@ -9007,6 +9050,9 @@ ahd_dump_card_state(struct ahd_softc *ahd)
        printf("%s: OS_SPACE_CNT = 0x%x MAXCMDCNT = 0x%x\n",
               ahd_name(ahd), ahd_inb(ahd, OS_SPACE_CNT),
               ahd_inb(ahd, MAXCMDCNT));
+       printf("%s: SAVED_SCSIID = 0x%x SAVED_LUN = 0x%x\n",
+              ahd_name(ahd), ahd_inb(ahd, SAVED_SCSIID),
+              ahd_inb(ahd, SAVED_LUN));
        ahd_simode0_print(ahd_inb(ahd, SIMODE0), &cur_col, 50);
        printf("\n");
        ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
@@ -9039,7 +9085,6 @@ ahd_dump_card_state(struct ahd_softc *ahd)
                ahd_outb(ahd, STACK, (ahd->saved_stack[i] >> 8) & 0xFF);
        }
        printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
-       ahd_platform_dump_card_state(ahd);
        ahd_restore_modes(ahd, saved_modes);
        if (paused == 0)
                ahd_unpause(ahd);
@@ -9177,7 +9222,7 @@ ahd_wait_seeprom(struct ahd_softc *ahd)
 {
        int cnt;
 
-       cnt = 20;
+       cnt = 5000;
        while ((ahd_inb(ahd, SEESTAT) & (SEEARBACK|SEEBUSY)) != 0 && --cnt)
                ahd_delay(5);
 
@@ -9523,13 +9568,9 @@ ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
                        if ((ahd->features & AHD_MULTI_TID) != 0) {
                                u_int targid_mask;
 
-                               targid_mask = ahd_inb(ahd, TARGID)
-                                           | (ahd_inb(ahd, TARGID + 1) << 8);
-
+                               targid_mask = ahd_inw(ahd, TARGID);
                                targid_mask |= target_mask;
-                               ahd_outb(ahd, TARGID, targid_mask);
-                               ahd_outb(ahd, TARGID+1, (targid_mask >> 8));
-                               
+                               ahd_outw(ahd, TARGID, targid_mask);
                                ahd_update_scsiid(ahd, targid_mask);
                        } else {
                                u_int our_id;
@@ -9643,14 +9684,9 @@ ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
                                if (ahd->features & AHD_MULTI_TID) {
                                        u_int targid_mask;
 
-                                       targid_mask = ahd_inb(ahd, TARGID)
-                                                   | (ahd_inb(ahd, TARGID + 1)
-                                                      << 8);
-
+                                       targid_mask = ahd_inw(ahd, TARGID);
                                        targid_mask &= ~target_mask;
-                                       ahd_outb(ahd, TARGID, targid_mask);
-                                       ahd_outb(ahd, TARGID+1,
-                                                (targid_mask >> 8));
+                                       ahd_outw(ahd, TARGID, targid_mask);
                                        ahd_update_scsiid(ahd, targid_mask);
                                }
                        }
@@ -9751,7 +9787,7 @@ ahd_run_tqinfifo(struct ahd_softc *ahd, int paused)
 
                cmd->cmd_valid = 0;
                ahd_dmamap_sync(ahd, ahd->shared_data_dmat,
-                               ahd->shared_data_dmamap,
+                               ahd->shared_data_map.dmamap,
                                ahd_targetcmd_offset(ahd, ahd->tqinfifonext),
                                sizeof(struct target_cmd),
                                BUS_DMASYNC_PREREAD);