Home | History | Annotate | Line # | Download | only in ic
aic7xxx.c revision 1.52
      1 /*	$NetBSD: aic7xxx.c,v 1.52 2000/05/27 21:58:16 fvdl Exp $	*/
      2 
      3 /*
      4  * Generic driver for the aic7xxx based adaptec SCSI controllers
      5  * Product specific probe and attach routines can be found in:
      6  * i386/eisa/ahc_eisa.c	27/284X and aic7770 motherboard controllers
      7  * pci/ahc_pci.c	3985, 3980, 3940, 2940, aic7895, aic7890,
      8  *			aic7880, aic7870, aic7860, and aic7850 controllers
      9  *
     10  * Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000 Justin T. Gibbs.
     11  * All rights reserved.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions, and the following disclaimer,
     18  *    without modification.
     19  * 2. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * Alternatively, this software may be distributed under the terms of the
     23  * the GNU Public License ("GPL").
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     29  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  * $FreeBSD: src/sys/dev/aic7xxx/aic7xxx.c,v 1.42 2000/03/18 22:28:18 gibbs Exp $
     38  */
     39 /*
     40  * A few notes on features of the driver.
     41  *
     42  * SCB paging takes advantage of the fact that devices stay disconnected
     43  * from the bus a relatively long time and that while they're disconnected,
     44  * having the SCBs for these transactions down on the host adapter is of
     45  * little use.  Instead of leaving this idle SCB down on the card we copy
     46  * it back up into kernel memory and reuse the SCB slot on the card to
     47  * schedule another transaction.  This can be a real payoff when doing random
     48  * I/O to tagged queueing devices since there are more transactions active at
     49  * once for the device to sort for optimal seek reduction. The algorithm goes
     50  * like this...
     51  *
     52  * The sequencer maintains two lists of its hardware SCBs.  The first is the
     53  * singly linked free list which tracks all SCBs that are not currently in
     54  * use.  The second is the doubly linked disconnected list which holds the
     55  * SCBs of transactions that are in the disconnected state sorted most
     56  * recently disconnected first.  When the kernel queues a transaction to
     57  * the card, a hardware SCB to "house" this transaction is retrieved from
     58  * either of these two lists.  If the SCB came from the disconnected list,
     59  * a check is made to see if any data transfer or SCB linking (more on linking
     60  * in a bit) information has been changed since it was copied from the host
     61  * and if so, DMAs the SCB back up before it can be used.  Once a hardware
     62  * SCB has been obtained, the SCB is DMAed from the host.  Before any work
     63  * can begin on this SCB, the sequencer must ensure that either the SCB is
     64  * for a tagged transaction or the target is not already working on another
     65  * non-tagged transaction.  If a conflict arises in the non-tagged case, the
     66  * sequencer finds the SCB for the active transactions and sets the SCB_LINKED
     67  * field in that SCB to this next SCB to execute.  To facilitate finding
     68  * active non-tagged SCBs, the last four bytes of up to the first four hardware
     69  * SCBs serve as a storage area for the currently active SCB ID for each
     70  * target.
     71  *
     72  * When a device reconnects, a search is made of the hardware SCBs to find
     73  * the SCB for this transaction.  If the search fails, a hardware SCB is
     74  * pulled from either the free or disconnected SCB list and the proper
     75  * SCB is DMAed from the host.  If the MK_MESSAGE control bit is set
     76  * in the control byte of the SCB while it was disconnected, the sequencer
     77  * will assert ATN and attempt to issue a message to the host.
     78  *
     79  * When a command completes, a check for non-zero status and residuals is
     80  * made.  If either of these conditions exists, the SCB is DMAed back up to
     81  * the host so that it can interpret this information.  Additionally, in the
     82  * case of bad status, the sequencer generates a special interrupt and pauses
     83  * itself.  This allows the host to setup a request sense command if it
     84  * chooses for this target synchronously with the error so that sense
     85  * information isn't lost.
     86  *
     87  */
     88 
     89 #include "opt_ddb.h"
     90 
     91 #include <sys/param.h>
     92 #include <sys/kernel.h>
     93 #include <sys/systm.h>
     94 #include <sys/device.h>
     95 #include <sys/malloc.h>
     96 #include <sys/buf.h>
     97 #include <sys/proc.h>
     98 #include <sys/scsiio.h>
     99 
    100 #include <machine/bus.h>
    101 #include <machine/intr.h>
    102 
    103 #include <dev/scsipi/scsi_all.h>
    104 #include <dev/scsipi/scsipi_all.h>
    105 #include <dev/scsipi/scsi_message.h>
    106 #include <dev/scsipi/scsipi_debug.h>
    107 #include <dev/scsipi/scsiconf.h>
    108 
    109 #include <vm/vm.h>
    110 #include <vm/vm_param.h>
    111 #include <vm/pmap.h>
    112 
    113 #include <dev/ic/aic7xxxvar.h>
    114 #include <dev/microcode/aic7xxx/sequencer.h>
    115 #include <dev/microcode/aic7xxx/aic7xxx_reg.h>
    116 #include <dev/microcode/aic7xxx/aic7xxx_seq.h>
    117 
    118 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
    119 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
    120 #define ALL_CHANNELS '\0'
    121 #define ALL_TARGETS_MASK 0xFFFF
    122 #define INITIATOR_WILDCARD	(~0)
    123 
    124 #define	SIM_IS_SCSIBUS_B(ahc, sc_link)	\
    125 	((sc_link)->scsipi_scsi.scsibus == (ahc)->sc_link_b.scsipi_scsi.scsibus)
    126 #define	SIM_CHANNEL(ahc, sc_link)	\
    127 	(SIM_IS_SCSIBUS_B(ahc, sc_link) ? 'B' : 'A')
    128 #define	SIM_SCSI_ID(ahc, sc_link)	\
    129 	(SIM_IS_SCSIBUS_B(ahc, sc_link) ? ahc->our_id_b : ahc->our_id)
    130 #define	SCB_IS_SCSIBUS_B(scb)	\
    131 	(((scb)->hscb->tcl & SELBUSB) != 0)
    132 #define	SCB_TARGET(scb)	\
    133 	(((scb)->hscb->tcl & TID) >> 4)
    134 #define	SCB_CHANNEL(scb) \
    135 	(SCB_IS_SCSIBUS_B(scb) ? 'B' : 'A')
    136 #define	SCB_LUN(scb)	\
    137 	((scb)->hscb->tcl & LID)
    138 #define SCB_TARGET_OFFSET(scb)		\
    139 	(SCB_TARGET(scb) + (SCB_IS_SCSIBUS_B(scb) ? 8 : 0))
    140 #define SCB_TARGET_MASK(scb)		\
    141 	(0x01 << (SCB_TARGET_OFFSET(scb)))
    142 #define TCL_CHANNEL(ahc, tcl)		\
    143 	((((ahc)->features & AHC_TWIN) && ((tcl) & SELBUSB)) ? 'B' : 'A')
    144 #define TCL_SCSI_ID(ahc, tcl)		\
    145 	(TCL_CHANNEL((ahc), (tcl)) == 'B' ? (ahc)->our_id_b : (ahc)->our_id)
    146 #define TCL_TARGET(tcl) (((tcl) & TID) >> TCL_TARGET_SHIFT)
    147 #define TCL_LUN(tcl) ((tcl) & LID)
    148 
    149 #define XS_TCL(ahc, xs) \
    150 	((((xs)->sc_link->scsipi_scsi.target << 4) & 0xF0) \
    151 	    | (SIM_IS_SCSIBUS_B((ahc), (xs)->sc_link) ? SELBUSB : 0) \
    152 	    | ((xs)->sc_link->scsipi_scsi.lun & 0x07))
    153 
    154 char *ahc_chip_names[] =
    155 {
    156 	"NONE",
    157 	"aic7770",
    158 	"aic7850",
    159 	"aic7855",
    160 	"aic7859",
    161 	"aic7860",
    162 	"aic7870",
    163 	"aic7880",
    164 	"aic7890/91",
    165 	"aic7892",
    166 	"aic7895",
    167 	"aic7896/97",
    168 	"aic7899"
    169 };
    170 
    171 typedef enum {
    172 	ROLE_UNKNOWN,
    173 	ROLE_INITIATOR,
    174 	ROLE_TARGET
    175 } role_t;
    176 
    177 struct ahc_devinfo {
    178 	int	  our_scsiid;
    179 	int	  target_offset;
    180 	u_int16_t target_mask;
    181 	u_int8_t  target;
    182 	u_int8_t  lun;
    183 	char	  channel;
    184 	role_t	  role;		/*
    185 				 * Only guaranteed to be correct if not
    186 				 * in the busfree state.
    187 				 */
    188 };
    189 
    190 typedef enum {
    191 	SEARCH_COMPLETE,
    192 	SEARCH_COUNT,
    193 	SEARCH_REMOVE
    194 } ahc_search_action;
    195 
    196 #ifdef AHC_DEBUG
    197 static int     ahc_debug = AHC_DEBUG;
    198 #endif
    199 
    200 static int	ahcinitscbdata(struct ahc_softc *);
    201 static void	ahcfiniscbdata(struct ahc_softc *);
    202 
    203 #if UNUSED
    204 static void	ahc_dump_targcmd(struct target_cmd *);
    205 #endif
    206 static void	ahc_shutdown(void *arg);
    207 static int32_t	ahc_action(struct scsipi_xfer *);
    208 static int	ahc_ioctl(struct scsipi_link *, u_long, caddr_t, int,
    209 			  struct proc *);
    210 static int	ahc_execute_scb(void *, bus_dma_segment_t *, int);
    211 static int	ahc_poll(struct ahc_softc *, int);
    212 static int	ahc_setup_data(struct ahc_softc *, struct scsipi_xfer *,
    213 			       struct scb *);
    214 static void	ahc_freeze_devq(struct ahc_softc *, struct scsipi_link *);
    215 static void	ahcallocscbs(struct ahc_softc *);
    216 #if UNUSED
    217 static void	ahc_scb_devinfo(struct ahc_softc *, struct ahc_devinfo *,
    218 				struct scb *);
    219 #endif
    220 static void	ahc_fetch_devinfo(struct ahc_softc *, struct ahc_devinfo *);
    221 static void	ahc_compile_devinfo(struct ahc_devinfo *, u_int, u_int, u_int,
    222 				    char, role_t);
    223 static u_int	ahc_abort_wscb(struct ahc_softc *, u_int, u_int);
    224 static void	ahc_done(struct ahc_softc *, struct scb *);
    225 static struct tmode_tstate *
    226 		ahc_alloc_tstate(struct ahc_softc *, u_int, char);
    227 #if UNUSED
    228 static void	ahc_free_tstate(struct ahc_softc *, u_int, char, int);
    229 #endif
    230 static void 	ahc_handle_seqint(struct ahc_softc *, u_int);
    231 static void	ahc_handle_scsiint(struct ahc_softc *, u_int);
    232 static void	ahc_build_transfer_msg(struct ahc_softc *,
    233 				       struct ahc_devinfo *);
    234 static void	ahc_setup_initiator_msgout(struct ahc_softc *,
    235 					   struct ahc_devinfo *,
    236 					   struct scb *);
    237 static void	ahc_setup_target_msgin(struct ahc_softc *,
    238 				       struct ahc_devinfo *);
    239 static void	ahc_clear_msg_state(struct ahc_softc *);
    240 static void	ahc_handle_message_phase(struct ahc_softc *,
    241 					 struct scsipi_link *);
    242 static int	ahc_sent_msg(struct ahc_softc *, u_int, int);
    243 
    244 static int	ahc_parse_msg(struct ahc_softc *, struct scsipi_link *,
    245 			      struct ahc_devinfo *);
    246 static void	ahc_handle_ign_wide_residue(struct ahc_softc *,
    247 					    struct ahc_devinfo *);
    248 static void	ahc_handle_devreset(struct ahc_softc *, struct ahc_devinfo *,
    249 				    int, char *, int);
    250 #ifdef AHC_DUMP_SEQ
    251 static void	ahc_dumpseq(struct ahc_softc *);
    252 #endif
    253 static void	ahc_loadseq(struct ahc_softc *);
    254 static int	ahc_check_patch(struct ahc_softc *, struct patch **,
    255 				int, int *);
    256 static void	ahc_download_instr(struct ahc_softc *, int, u_int8_t *);
    257 static int	ahc_match_scb(struct scb *, int, char, int, u_int, role_t);
    258 #if defined(AHC_DEBUG)
    259 static void	ahc_print_scb(struct scb *);
    260 #endif
    261 static int	ahc_search_qinfifo(struct ahc_softc *, int, char, int, u_int,
    262 				   role_t, scb_flag, ahc_search_action);
    263 static int	ahc_reset_channel(struct ahc_softc *, char, int);
    264 static int	ahc_abort_scbs(struct ahc_softc *, int, char, int, u_int,
    265 			       role_t, int);
    266 static int	ahc_search_disc_list(struct ahc_softc *, int,
    267 				     char, int, u_int, int, int, int);
    268 static u_int	ahc_rem_scb_from_disc_list(struct ahc_softc *, u_int, u_int);
    269 static void	ahc_add_curscb_to_free_list(struct ahc_softc *);
    270 static void	ahc_clear_intstat(struct ahc_softc *);
    271 static void	ahc_reset_current_bus(struct ahc_softc *);
    272 static struct ahc_syncrate *
    273 		ahc_devlimited_syncrate(struct ahc_softc *, u_int *);
    274 static struct ahc_syncrate *
    275 		ahc_find_syncrate(struct ahc_softc *, u_int *, u_int);
    276 static u_int	ahc_find_period(struct ahc_softc *, u_int, u_int);
    277 static void	ahc_validate_offset(struct ahc_softc *, struct ahc_syncrate *,
    278 				    u_int *, int);
    279 static void	ahc_update_target_msg_request(struct ahc_softc *,
    280 					      struct ahc_devinfo *,
    281 					      struct ahc_initiator_tinfo *,
    282 					      int, int);
    283 static void	ahc_set_syncrate(struct ahc_softc *, struct ahc_devinfo *,
    284 				 struct ahc_syncrate *, u_int, u_int, u_int,
    285 				 int, int);
    286 static void	ahc_set_width(struct ahc_softc *, struct ahc_devinfo *,
    287 			      u_int, u_int, int, int);
    288 static void	ahc_set_tags(struct ahc_softc *, struct ahc_devinfo *,
    289 			     int);
    290 static void	ahc_construct_sdtr(struct ahc_softc *, u_int, u_int);
    291 
    292 static void	ahc_construct_wdtr(struct ahc_softc *, u_int);
    293 
    294 static void	ahc_calc_residual(struct scb *);
    295 
    296 static void	ahc_update_pending_syncrates(struct ahc_softc *);
    297 
    298 static void	ahc_set_recoveryscb(struct ahc_softc *, struct scb *);
    299 
    300 static void     ahc_timeout (void *);
    301 static __inline int  sequencer_paused(struct ahc_softc *);
    302 static __inline void pause_sequencer(struct ahc_softc *);
    303 static __inline void unpause_sequencer(struct ahc_softc *);
    304 static 		void restart_sequencer(struct ahc_softc *);
    305 static __inline u_int ahc_index_busy_tcl(struct ahc_softc *, u_int, int);
    306 
    307 static __inline void	 ahc_busy_tcl(struct ahc_softc *, struct scb *);
    308 static __inline int	ahc_isbusy_tcl(struct ahc_softc *, struct scb *);
    309 
    310 static __inline void	   ahc_freeze_ccb(struct scb *);
    311 static __inline void	   ahcsetccbstatus(struct scsipi_xfer *, int);
    312 static void		   ahc_run_qoutfifo(struct ahc_softc *);
    313 
    314 static __inline struct ahc_initiator_tinfo *
    315 			   ahc_fetch_transinfo(struct ahc_softc *,
    316 					       char, u_int, u_int,
    317 					       struct tmode_tstate **);
    318 static void	   ahcfreescb(struct ahc_softc *, struct scb *);
    319 static __inline	struct scb *ahcgetscb(struct ahc_softc *);
    320 
    321 static int ahc_createdmamem(bus_dma_tag_t, int, int, bus_dmamap_t *,
    322 			    caddr_t *, bus_addr_t *, bus_dma_segment_t *,
    323 			    int *, const char *, const char *);
    324 static void ahc_freedmamem(bus_dma_tag_t, int, bus_dmamap_t,
    325 			   caddr_t, bus_dma_segment_t *, int);
    326 static void ahcminphys(struct buf *);
    327 
    328 static __inline struct scsipi_xfer *ahc_first_xs(struct ahc_softc *);
    329 static __inline void ahc_swap_hscb(struct hardware_scb *);
    330 static __inline void ahc_swap_sg(struct ahc_dma_seg *);
    331 static void ahc_check_tags(struct ahc_softc *, struct scsipi_xfer *);
    332 static int ahc_istagged_device(struct ahc_softc *, struct scsipi_xfer *, int);
    333 
    334 #if defined(AHC_DEBUG) && 0
    335 static void ahc_dumptinfo(struct ahc_softc *, struct ahc_initiator_tinfo *);
    336 #endif
    337 
    338 static struct scsipi_device ahc_dev =
    339 {
    340     NULL,                       /* Use default error handler */
    341     NULL,                       /* have a queue, served by this */
    342     NULL,                       /* have no async handler */
    343     NULL,                       /* Use default 'done' routine */
    344 };
    345 
    346 /*
    347  * Pick the first xs for a non-blocked target.
    348  */
    349 static __inline struct scsipi_xfer *
    350 ahc_first_xs(struct ahc_softc *ahc)
    351 {
    352 	int target;
    353 	struct scsipi_xfer *xs = TAILQ_FIRST(&ahc->sc_q);
    354 
    355 	if (ahc->queue_blocked)
    356 		return NULL;
    357 
    358 	while (xs != NULL) {
    359 		target = xs->sc_link->scsipi_scsi.target;
    360 		if (ahc->devqueue_blocked[target] == 0 &&
    361 		    (!ahc_istagged_device(ahc, xs, 0) &&
    362 		     ahc_index_busy_tcl(ahc, XS_TCL(ahc, xs), FALSE) ==
    363 		    SCB_LIST_NULL))
    364 			break;
    365 		xs = TAILQ_NEXT(xs, adapter_q);
    366 	}
    367 
    368 	return xs;
    369 }
    370 
    371 static __inline void
    372 ahc_swap_hscb(struct hardware_scb *hscb)
    373 {
    374 	hscb->SG_pointer = htole32(hscb->SG_pointer);
    375 	hscb->data = htole32(hscb->data);
    376 	hscb->datalen = htole32(hscb->datalen);
    377 	/*
    378 	 * No need to swap cmdpointer; it's either 0 or set to
    379 	 * cmdstore_busaddr, which is already swapped.
    380 	 */
    381 }
    382 
    383 static __inline void
    384 ahc_swap_sg(struct ahc_dma_seg *sg)
    385 {
    386 	sg->addr = htole32(sg->addr);
    387 	sg->len = htole32(sg->len);
    388 }
    389 
    390 static void
    391 ahcminphys(bp)
    392 	struct buf *bp;
    393 {
    394 /*
    395  * Even though the card can transfer up to 16megs per command
    396  * we are limited by the number of segments in the dma segment
    397  * list that we can hold.  The worst case is that all pages are
    398  * discontinuous physically, hense the "page per segment" limit
    399  * enforced here.
    400  */
    401 	if (bp->b_bcount > AHC_MAXTRANSFER_SIZE) {
    402 		bp->b_bcount = AHC_MAXTRANSFER_SIZE;
    403 	}
    404 	minphys(bp);
    405 }
    406 
    407 
    408 static __inline u_int32_t
    409 ahc_hscb_busaddr(struct ahc_softc *ahc, u_int index)
    410 {
    411 	return (ahc->scb_data->hscb_busaddr
    412 		+ (sizeof(struct hardware_scb) * index));
    413 }
    414 
    415 #define AHC_BUSRESET_DELAY	25	/* Reset delay in us */
    416 
    417 static __inline int
    418 sequencer_paused(struct ahc_softc *ahc)
    419 {
    420 	return ((ahc_inb(ahc, HCNTRL) & PAUSE) != 0);
    421 }
    422 
    423 static __inline void
    424 pause_sequencer(struct ahc_softc *ahc)
    425 {
    426 	ahc_outb(ahc, HCNTRL, ahc->pause);
    427 
    428 	/*
    429 	 * Since the sequencer can disable pausing in a critical section, we
    430 	 * must loop until it actually stops.
    431 	 */
    432 	while (sequencer_paused(ahc) == 0)
    433 		;
    434 }
    435 
    436 static __inline void
    437 unpause_sequencer(struct ahc_softc *ahc)
    438 {
    439 	if ((ahc_inb(ahc, INTSTAT) & (SCSIINT | SEQINT | BRKADRINT)) == 0)
    440 		ahc_outb(ahc, HCNTRL, ahc->unpause);
    441 }
    442 
    443 /*
    444  * Restart the sequencer program from address zero
    445  */
    446 static void
    447 restart_sequencer(struct ahc_softc *ahc)
    448 {
    449 	u_int i;
    450 
    451 	pause_sequencer(ahc);
    452 
    453 	/*
    454 	 * Everytime we restart the sequencer, there
    455 	 * is the possiblitity that we have restarted
    456 	 * within a three instruction window where an
    457 	 * SCB has been marked free but has not made it
    458 	 * onto the free list.  Since SCSI events(bus reset,
    459 	 * unexpected bus free) will always freeze the
    460 	 * sequencer, we cannot close this window.  To
    461 	 * avoid losing an SCB, we reconsitute the free
    462 	 * list every time we restart the sequencer.
    463 	 */
    464 	ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
    465 	for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
    466 
    467 		ahc_outb(ahc, SCBPTR, i);
    468 		if (ahc_inb(ahc, SCB_TAG) == SCB_LIST_NULL)
    469 			ahc_add_curscb_to_free_list(ahc);
    470 	}
    471 	ahc_outb(ahc, SEQCTL, FASTMODE|SEQRESET);
    472 	unpause_sequencer(ahc);
    473 }
    474 
    475 static __inline u_int
    476 ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl, int unbusy)
    477 {
    478 	u_int scbid;
    479 
    480 	scbid = ahc->untagged_scbs[tcl];
    481 	if (unbusy) {
    482 		ahc->untagged_scbs[tcl] = SCB_LIST_NULL;
    483 		bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
    484 		    UNTAGGEDSCB_OFFSET * 256, 256, BUS_DMASYNC_PREWRITE);
    485 	}
    486 
    487 	return (scbid);
    488 }
    489 
    490 static __inline void
    491 ahc_busy_tcl(struct ahc_softc *ahc, struct scb *scb)
    492 {
    493 	ahc->untagged_scbs[scb->hscb->tcl] = scb->hscb->tag;
    494 	bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
    495 	    UNTAGGEDSCB_OFFSET * 256, 256, BUS_DMASYNC_PREWRITE);
    496 }
    497 
    498 static __inline int
    499 ahc_isbusy_tcl(struct ahc_softc *ahc, struct scb *scb)
    500 {
    501 	return ahc->untagged_scbs[scb->hscb->tcl] != SCB_LIST_NULL;
    502 }
    503 
    504 static __inline void
    505 ahc_freeze_ccb(struct scb *scb)
    506 {
    507 	struct scsipi_xfer *xs = scb->xs;
    508 	struct ahc_softc *ahc = (struct ahc_softc *)xs->sc_link->adapter_softc;
    509 	int target;
    510 
    511 	target = xs->sc_link->scsipi_scsi.target;
    512 	if (!(scb->flags & SCB_FREEZE_QUEUE)) {
    513 		ahc->devqueue_blocked[target]++;
    514 		scb->flags |= SCB_FREEZE_QUEUE;
    515 	}
    516 }
    517 
    518 static __inline void
    519 ahcsetccbstatus(struct scsipi_xfer *xs, int status)
    520 {
    521 	xs->error = status;
    522 }
    523 
    524 static __inline struct ahc_initiator_tinfo *
    525 ahc_fetch_transinfo(struct ahc_softc *ahc, char channel, u_int our_id,
    526 		    u_int remote_id, struct tmode_tstate **tstate)
    527 {
    528 	/*
    529 	 * Transfer data structures are stored from the perspective
    530 	 * of the target role.  Since the parameters for a connection
    531 	 * in the initiator role to a given target are the same as
    532 	 * when the roles are reversed, we pretend we are the target.
    533 	 */
    534 	if (channel == 'B')
    535 		our_id += 8;
    536 	*tstate = ahc->enabled_targets[our_id];
    537 	return (&(*tstate)->transinfo[remote_id]);
    538 }
    539 
    540 static void
    541 ahc_run_qoutfifo(struct ahc_softc *ahc)
    542 {
    543 	struct scb *scb;
    544 	u_int  scb_index;
    545 
    546 	bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap, 0,
    547 	     256, BUS_DMASYNC_POSTREAD);
    548 
    549 	while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) {
    550 		scb_index = ahc->qoutfifo[ahc->qoutfifonext];
    551 		ahc->qoutfifo[ahc->qoutfifonext++] = SCB_LIST_NULL;
    552 
    553 		scb = &ahc->scb_data->scbarray[scb_index];
    554 		if (scb_index >= ahc->scb_data->numscbs
    555 		  || (scb->flags & SCB_ACTIVE) == 0) {
    556 			printf("%s: WARNING no command for scb %d "
    557 			       "(cmdcmplt)\nQOUTPOS = %d\n",
    558 			       ahc_name(ahc), scb_index,
    559 			       ahc->qoutfifonext - 1);
    560 			continue;
    561 		}
    562 
    563 		/*
    564 		 * Save off the residual
    565 		 * if there is one.
    566 		 */
    567 		if (scb->hscb->residual_SG_count != 0)
    568 			ahc_calc_residual(scb);
    569 		else
    570 			scb->xs->resid = 0;
    571 #ifdef AHC_DEBUG
    572 		if (ahc_debug & AHC_SHOWSCBS) {
    573 			scsi_print_addr(scb->xs->sc_link);
    574 			printf("run_qoutfifo: SCB %x complete\n",
    575 			    scb->hscb->tag);
    576 		}
    577 #endif
    578 		ahc_done(ahc, scb);
    579 	}
    580 }
    581 
    582 
    583 /*
    584  * An scb (and hence an scb entry on the board) is put onto the
    585  * free list.
    586  */
    587 static void
    588 ahcfreescb(struct ahc_softc *ahc, struct scb *scb)
    589 {
    590 	struct hardware_scb *hscb;
    591 	int opri;
    592 
    593 	hscb = scb->hscb;
    594 
    595 #ifdef AHC_DEBUG
    596 	if (ahc_debug & AHC_SHOWSCBALLOC)
    597 		printf("%s: free SCB tag %x\n", ahc_name(ahc), hscb->tag);
    598 #endif
    599 
    600 	opri = splbio();
    601 
    602 	if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0 ||
    603 	    (scb->flags & SCB_RECOVERY_SCB) != 0) {
    604 		ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
    605 		ahc->queue_blocked = 0;
    606 	}
    607 
    608 	/* Clean up for the next user */
    609 	scb->flags = SCB_FREE;
    610 	hscb->control = 0;
    611 	hscb->status = 0;
    612 
    613 	SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs, scb, links);
    614 
    615 	splx(opri);
    616 }
    617 
    618 /*
    619  * Get a free scb, either one already assigned to a hardware slot
    620  * on the adapter or one that will require an SCB to be paged out before
    621  * use. If there are none, see if we can allocate a new SCB.  Otherwise
    622  * either return an error or sleep.
    623  */
    624 static __inline struct scb *
    625 ahcgetscb(struct ahc_softc *ahc)
    626 {
    627 	struct scb *scbp;
    628 	int opri;;
    629 
    630 	opri = splbio();
    631 	if ((scbp = SLIST_FIRST(&ahc->scb_data->free_scbs))) {
    632 		SLIST_REMOVE_HEAD(&ahc->scb_data->free_scbs, links);
    633 	} else {
    634 		ahcallocscbs(ahc);
    635 		scbp = SLIST_FIRST(&ahc->scb_data->free_scbs);
    636 		if (scbp != NULL)
    637 			SLIST_REMOVE_HEAD(&ahc->scb_data->free_scbs, links);
    638 	}
    639 
    640 	splx(opri);
    641 
    642 #ifdef AHC_DEBUG
    643 	if (ahc_debug & AHC_SHOWSCBALLOC) {
    644 		if (scbp != NULL)
    645 			printf("%s: new SCB, tag %x\n", ahc_name(ahc),
    646 			    scbp->hscb->tag);
    647 		else
    648 			printf("%s: failed to allocate new SCB\n",
    649 			    ahc_name(ahc));
    650 	}
    651 #endif
    652 
    653 	return (scbp);
    654 }
    655 
    656 static int
    657 ahc_createdmamem(tag, size, flags, mapp, vaddr, baddr, seg, nseg, myname, what)
    658 	bus_dma_tag_t tag;
    659 	int size;
    660 	int flags;
    661 	bus_dmamap_t *mapp;
    662 	caddr_t *vaddr;
    663 	bus_addr_t *baddr;
    664 	bus_dma_segment_t *seg;
    665 	int *nseg;
    666 	const char *myname, *what;
    667 {
    668 	int error, level = 0;
    669 
    670 	if ((error = bus_dmamem_alloc(tag, size, NBPG, 0,
    671 			seg, 1, nseg, BUS_DMA_NOWAIT)) != 0) {
    672 		printf("%s: failed to allocate DMA mem for %s, error = %d\n",
    673 			myname, what, error);
    674 		goto out;
    675 	}
    676 	level++;
    677 
    678 	if ((error = bus_dmamem_map(tag, seg, *nseg, size, vaddr,
    679 			BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    680 		printf("%s: failed to map DMA mem for %s, error = %d\n",
    681 			myname, what, error);
    682 		goto out;
    683 	}
    684 	level++;
    685 
    686 	if ((error = bus_dmamap_create(tag, size, 1, size, 0,
    687 			BUS_DMA_NOWAIT | flags, mapp)) != 0) {
    688                 printf("%s: failed to create DMA map for %s, error = %d\n",
    689 			myname, what, error);
    690 		goto out;
    691         }
    692 	level++;
    693 
    694 	if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
    695 			BUS_DMA_NOWAIT)) != 0) {
    696                 printf("%s: failed to load DMA map for %s, error = %d\n",
    697 			myname, what, error);
    698 		goto out;
    699         }
    700 
    701 	*baddr = (*mapp)->dm_segs[0].ds_addr;
    702 
    703 #ifdef AHC_DEBUG
    704 	printf("%s: dmamem for %s at busaddr %lx virt %lx nseg %d size %d\n",
    705 	    myname, what, (unsigned long)*baddr, (unsigned long)*vaddr,
    706 	    *nseg, size);
    707 #endif
    708 
    709 	return 0;
    710 out:
    711 	switch (level) {
    712 	case 3:
    713 		bus_dmamap_destroy(tag, *mapp);
    714 		/* FALLTHROUGH */
    715 	case 2:
    716 		bus_dmamem_unmap(tag, *vaddr, size);
    717 		/* FALLTHROUGH */
    718 	case 1:
    719 		bus_dmamem_free(tag, seg, *nseg);
    720 		break;
    721 	default:
    722 		break;
    723 	}
    724 
    725 	return error;
    726 }
    727 
    728 static void
    729 ahc_freedmamem(tag, size, map, vaddr, seg, nseg)
    730 	bus_dma_tag_t tag;
    731 	int size;
    732 	bus_dmamap_t map;
    733 	caddr_t vaddr;
    734 	bus_dma_segment_t *seg;
    735 	int nseg;
    736 {
    737 
    738 	bus_dmamap_unload(tag, map);
    739 	bus_dmamap_destroy(tag, map);
    740 	bus_dmamem_unmap(tag, vaddr, size);
    741 	bus_dmamem_free(tag, seg, nseg);
    742 }
    743 
    744 char *
    745 ahc_name(struct ahc_softc *ahc)
    746 {
    747 	return (ahc->sc_dev.dv_xname);
    748 }
    749 
    750 #ifdef AHC_DEBUG
    751 static void
    752 ahc_print_scb(struct scb *scb)
    753 {
    754 	struct hardware_scb *hscb = scb->hscb;
    755 
    756 	printf("scb:%p tag %x control:0x%x tcl:0x%x cmdlen:%d cmdpointer:0x%lx\n",
    757 		scb,
    758 		hscb->tag,
    759 		hscb->control,
    760 		hscb->tcl,
    761 		hscb->cmdlen,
    762 		(unsigned long)le32toh(hscb->cmdpointer));
    763 	printf("        datlen:%u data:0x%lx segs:0x%x segp:0x%lx\n",
    764 		le32toh(hscb->datalen),
    765 		(unsigned long)(le32toh(hscb->data)),
    766 		hscb->SG_count,
    767 		(unsigned long)(le32toh(hscb->SG_pointer)));
    768 	printf("	sg_addr:%lx sg_len:%lu\n",
    769 		(unsigned long)(le32toh(scb->sg_list[0].addr)),
    770 		(unsigned long)(le32toh(scb->sg_list[0].len)));
    771 	printf("	cdb:%x %x %x %x %x %x %x %x %x %x %x %x\n",
    772 		hscb->cmdstore[0], hscb->cmdstore[1], hscb->cmdstore[2],
    773 		hscb->cmdstore[3], hscb->cmdstore[4], hscb->cmdstore[5],
    774 		hscb->cmdstore[6], hscb->cmdstore[7], hscb->cmdstore[8],
    775 		hscb->cmdstore[9], hscb->cmdstore[10], hscb->cmdstore[11]);
    776 }
    777 #endif
    778 
    779 static struct {
    780         u_int8_t errno;
    781 	char *errmesg;
    782 } hard_error[] = {
    783 	{ ILLHADDR,	"Illegal Host Access" },
    784 	{ ILLSADDR,	"Illegal Sequencer Address referrenced" },
    785 	{ ILLOPCODE,	"Illegal Opcode in sequencer program" },
    786 	{ SQPARERR,	"Sequencer Parity Error" },
    787 	{ DPARERR,	"Data-path Parity Error" },
    788 	{ MPARERR,	"Scratch or SCB Memory Parity Error" },
    789 	{ PCIERRSTAT,	"PCI Error detected" },
    790 	{ CIOPARERR,	"CIOBUS Parity Error" },
    791 };
    792 static const int num_errors = sizeof(hard_error)/sizeof(hard_error[0]);
    793 
    794 static struct {
    795         u_int8_t phase;
    796         u_int8_t mesg_out; /* Message response to parity errors */
    797 	char *phasemsg;
    798 } phase_table[] = {
    799 	{ P_DATAOUT,	MSG_NOOP,		"in Data-out phase"	},
    800 	{ P_DATAIN,	MSG_INITIATOR_DET_ERR,	"in Data-in phase"	},
    801 	{ P_COMMAND,	MSG_NOOP,		"in Command phase"	},
    802 	{ P_MESGOUT,	MSG_NOOP,		"in Message-out phase"	},
    803 	{ P_STATUS,	MSG_INITIATOR_DET_ERR,	"in Status phase"	},
    804 	{ P_MESGIN,	MSG_PARITY_ERROR,	"in Message-in phase"	},
    805 	{ P_BUSFREE,	MSG_NOOP,		"while idle"		},
    806 	{ 0,		MSG_NOOP,		"in unknown phase"	}
    807 };
    808 static const int num_phases = (sizeof(phase_table)/sizeof(phase_table[0])) - 1;
    809 
    810 /*
    811  * Valid SCSIRATE values.  (p. 3-17)
    812  * Provides a mapping of tranfer periods in ns to the proper value to
    813  * stick in the scsiscfr reg to use that transfer rate.
    814  */
    815 #define AHC_SYNCRATE_DT		0
    816 #define AHC_SYNCRATE_ULTRA2	1
    817 #define AHC_SYNCRATE_ULTRA	3
    818 #define AHC_SYNCRATE_FAST	6
    819 static struct ahc_syncrate ahc_syncrates[] = {
    820       /* ultra2    fast/ultra  period     rate */
    821 	{ 0x42,      0x000,      9,      "80.0" },
    822 	{ 0x03,      0x000,     10,      "40.0" },
    823 	{ 0x04,      0x000,     11,      "33.0" },
    824 	{ 0x05,      0x100,     12,      "20.0" },
    825 	{ 0x06,      0x110,     15,      "16.0" },
    826 	{ 0x07,      0x120,     18,      "13.4" },
    827 	{ 0x08,      0x000,     25,      "10.0" },
    828 	{ 0x19,      0x010,     31,      "8.0"  },
    829 	{ 0x1a,      0x020,     37,      "6.67" },
    830 	{ 0x1b,      0x030,     43,      "5.7"  },
    831 	{ 0x1c,      0x040,     50,      "5.0"  },
    832 	{ 0x00,      0x050,     56,      "4.4"  },
    833 	{ 0x00,      0x060,     62,      "4.0"  },
    834 	{ 0x00,      0x070,     68,      "3.6"  },
    835 	{ 0x00,      0x000,      0,      NULL   }
    836 };
    837 
    838 /*
    839  * Allocate a controller structure for a new device and initialize it.
    840  */
    841 int
    842 ahc_alloc(struct ahc_softc *ahc, bus_space_handle_t sh, bus_space_tag_t st,
    843 	  bus_dma_tag_t parent_dmat, ahc_chip chip, ahc_feature features,
    844 	  ahc_flag flags)
    845 {
    846 	struct scb_data *scb_data;
    847 
    848 	scb_data = malloc(sizeof (struct scb_data), M_DEVBUF, M_NOWAIT);
    849 	if (scb_data == NULL) {
    850 		printf("%s: cannot malloc softc!\n", ahc_name(ahc));
    851 		return -1;
    852 	}
    853 	bzero(scb_data, sizeof (struct scb_data));
    854 	LIST_INIT(&ahc->pending_ccbs);
    855 	ahc->tag = st;
    856 	ahc->bsh = sh;
    857 	ahc->parent_dmat = parent_dmat;
    858 	ahc->chip = chip;
    859 	ahc->features = features;
    860 	ahc->flags = flags;
    861 	ahc->scb_data = scb_data;
    862 
    863 	ahc->unpause = (ahc_inb(ahc, HCNTRL) & IRQMS) | INTEN;
    864 	/* The IRQMS bit is only valid on VL and EISA chips */
    865 	if ((ahc->chip & AHC_PCI) != 0)
    866 		ahc->unpause &= ~IRQMS;
    867 	ahc->pause = ahc->unpause | PAUSE;
    868 	return (0);
    869 }
    870 
    871 void
    872 ahc_free(ahc)
    873 	struct ahc_softc *ahc;
    874 {
    875 	ahcfiniscbdata(ahc);
    876 	if (ahc->init_level != 0)
    877 		ahc_freedmamem(ahc->parent_dmat, ahc->shared_data_size,
    878 		    ahc->shared_data_dmamap, ahc->qoutfifo,
    879 		    &ahc->shared_data_seg, ahc->shared_data_nseg);
    880 
    881 	if (ahc->scb_data != NULL)
    882 		free(ahc->scb_data, M_DEVBUF);
    883 	if (ahc->bus_data != NULL)
    884 		free(ahc->bus_data, M_DEVBUF);
    885 	return;
    886 }
    887 
    888 static int
    889 ahcinitscbdata(struct ahc_softc *ahc)
    890 {
    891 	struct scb_data *scb_data;
    892 	int i;
    893 
    894 	scb_data = ahc->scb_data;
    895 	SLIST_INIT(&scb_data->free_scbs);
    896 	SLIST_INIT(&scb_data->sg_maps);
    897 
    898 	/* Allocate SCB resources */
    899 	scb_data->scbarray =
    900 	    (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX,
    901 				 M_DEVBUF, M_NOWAIT);
    902 	if (scb_data->scbarray == NULL)
    903 		return (ENOMEM);
    904 	bzero(scb_data->scbarray, sizeof(struct scb) * AHC_SCB_MAX);
    905 
    906 	/* Determine the number of hardware SCBs and initialize them */
    907 
    908 	scb_data->maxhscbs = ahc_probe_scbs(ahc);
    909 	/* SCB 0 heads the free list */
    910 	ahc_outb(ahc, FREE_SCBH, 0);
    911 	for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
    912 		ahc_outb(ahc, SCBPTR, i);
    913 
    914 		/* Clear the control byte. */
    915 		ahc_outb(ahc, SCB_CONTROL, 0);
    916 
    917 		/* Set the next pointer */
    918 		ahc_outb(ahc, SCB_NEXT, i+1);
    919 
    920 		/* Make the tag number invalid */
    921 		ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
    922 	}
    923 
    924 	/* Make sure that the last SCB terminates the free list */
    925 	ahc_outb(ahc, SCBPTR, i-1);
    926 	ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
    927 
    928 	/* Ensure we clear the 0 SCB's control byte. */
    929 	ahc_outb(ahc, SCBPTR, 0);
    930 	ahc_outb(ahc, SCB_CONTROL, 0);
    931 
    932 	scb_data->maxhscbs = i;
    933 
    934 	if (ahc->scb_data->maxhscbs == 0)
    935 		panic("%s: No SCB space found", ahc_name(ahc));
    936 
    937 	/*
    938 	 * Create our DMA tags.  These tags define the kinds of device
    939 	 * accessable memory allocations and memory mappings we will
    940 	 * need to perform during normal operation.
    941 	 *
    942 	 * Unless we need to further restrict the allocation, we rely
    943 	 * on the restrictions of the parent dmat, hence the common
    944 	 * use of MAXADDR and MAXSIZE.
    945 	 */
    946 
    947 	if (ahc_createdmamem(ahc->parent_dmat,
    948 	    AHC_SCB_MAX * sizeof(struct hardware_scb), ahc->sc_dmaflags,
    949 	    &scb_data->hscb_dmamap,
    950 	    (caddr_t *)&scb_data->hscbs, &scb_data->hscb_busaddr,
    951 	    &scb_data->hscb_seg, &scb_data->hscb_nseg, ahc_name(ahc),
    952 	    "hardware SCB structures") < 0)
    953 		goto error_exit;
    954 
    955 	scb_data->init_level++;
    956 
    957 	if (ahc_createdmamem(ahc->parent_dmat,
    958 	    AHC_SCB_MAX * sizeof(struct scsipi_sense_data), ahc->sc_dmaflags,
    959 	    &scb_data->sense_dmamap, (caddr_t *)&scb_data->sense,
    960 	    &scb_data->sense_busaddr, &scb_data->sense_seg,
    961 	    &scb_data->sense_nseg, ahc_name(ahc), "sense buffers") < 0)
    962 		goto error_exit;
    963 
    964 	scb_data->init_level++;
    965 
    966 	/* Perform initial CCB allocation */
    967 	bzero(scb_data->hscbs, AHC_SCB_MAX * sizeof(struct hardware_scb));
    968 	ahcallocscbs(ahc);
    969 
    970 	if (scb_data->numscbs == 0) {
    971 		printf("%s: ahc_init_scb_data - "
    972 		       "Unable to allocate initial scbs\n",
    973 		       ahc_name(ahc));
    974 		goto error_exit;
    975 	}
    976 
    977 	scb_data->init_level++;
    978 
    979 	/*
    980          * Note that we were successfull
    981          */
    982         return 0;
    983 
    984 error_exit:
    985 
    986 	return ENOMEM;
    987 }
    988 
    989 static void
    990 ahcfiniscbdata(struct ahc_softc *ahc)
    991 {
    992 	struct scb_data *scb_data;
    993 
    994 	scb_data = ahc->scb_data;
    995 
    996 	switch (scb_data->init_level) {
    997 	default:
    998 	case 3:
    999 	{
   1000 		struct sg_map_node *sg_map;
   1001 
   1002 		while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
   1003 			SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
   1004 			ahc_freedmamem(ahc->parent_dmat, PAGE_SIZE,
   1005 			    sg_map->sg_dmamap, (caddr_t)sg_map->sg_vaddr,
   1006 			    &sg_map->sg_dmasegs, sg_map->sg_nseg);
   1007 			free(sg_map, M_DEVBUF);
   1008 		}
   1009 	}
   1010 	/*FALLTHROUGH*/
   1011 	case 2:
   1012 		ahc_freedmamem(ahc->parent_dmat,
   1013 		    AHC_SCB_MAX * sizeof(struct scsipi_sense_data),
   1014 		    scb_data->sense_dmamap, (caddr_t)scb_data->sense,
   1015 		    &scb_data->sense_seg, scb_data->sense_nseg);
   1016 	/*FALLTHROUGH*/
   1017 	case 1:
   1018 		ahc_freedmamem(ahc->parent_dmat,
   1019 		    AHC_SCB_MAX * sizeof(struct hardware_scb),
   1020 		    scb_data->hscb_dmamap, (caddr_t)scb_data->hscbs,
   1021 		    &scb_data->hscb_seg, scb_data->hscb_nseg);
   1022 	/*FALLTHROUGH*/
   1023 	}
   1024 	if (scb_data->scbarray != NULL)
   1025 		free(scb_data->scbarray, M_DEVBUF);
   1026 }
   1027 
   1028 int
   1029 ahc_reset(struct ahc_softc *ahc)
   1030 {
   1031 	u_int	sblkctl;
   1032 	int	wait;
   1033 
   1034 #ifdef AHC_DUMP_SEQ
   1035 	if (ahc->init_level == 0)
   1036 		ahc_dumpseq(ahc);
   1037 #endif
   1038 	ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
   1039 	/*
   1040 	 * Ensure that the reset has finished
   1041 	 */
   1042 	wait = 1000;
   1043 	do {
   1044 		DELAY(1000);
   1045 	} while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
   1046 
   1047 	if (wait == 0) {
   1048 		printf("%s: WARNING - Failed chip reset!  "
   1049 		       "Trying to initialize anyway.\n", ahc_name(ahc));
   1050 	}
   1051 	ahc_outb(ahc, HCNTRL, ahc->pause);
   1052 
   1053 	/* Determine channel configuration */
   1054 	sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
   1055 	/* No Twin Channel PCI cards */
   1056 	if ((ahc->chip & AHC_PCI) != 0)
   1057 		sblkctl &= ~SELBUSB;
   1058 	switch (sblkctl) {
   1059 	case 0:
   1060 		/* Single Narrow Channel */
   1061 		break;
   1062 	case 2:
   1063 		/* Wide Channel */
   1064 		ahc->features |= AHC_WIDE;
   1065 		break;
   1066 	case 8:
   1067 		/* Twin Channel */
   1068 		ahc->features |= AHC_TWIN;
   1069 		break;
   1070 	default:
   1071 		printf(" Unsupported adapter type.  Ignoring\n");
   1072 		return(-1);
   1073 	}
   1074 
   1075 	return (0);
   1076 }
   1077 
   1078 /*
   1079  * Called when we have an active connection to a target on the bus,
   1080  * this function finds the nearest syncrate to the input period limited
   1081  * by the capabilities of the bus connectivity of the target.
   1082  */
   1083 static struct ahc_syncrate *
   1084 ahc_devlimited_syncrate(struct ahc_softc *ahc, u_int *period) {
   1085 	u_int	maxsync;
   1086 
   1087 	if ((ahc->features & AHC_ULTRA2) != 0) {
   1088 		if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0
   1089 		 && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) {
   1090 			maxsync = AHC_SYNCRATE_ULTRA2;
   1091 		} else {
   1092 			maxsync = AHC_SYNCRATE_ULTRA;
   1093 		}
   1094 	} else if ((ahc->features & AHC_ULTRA) != 0) {
   1095 		maxsync = AHC_SYNCRATE_ULTRA;
   1096 	} else {
   1097 		maxsync = AHC_SYNCRATE_FAST;
   1098 	}
   1099 	return (ahc_find_syncrate(ahc, period, maxsync));
   1100 }
   1101 
   1102 /*
   1103  * Look up the valid period to SCSIRATE conversion in our table.
   1104  * Return the period and offset that should be sent to the target
   1105  * if this was the beginning of an SDTR.
   1106  */
   1107 static struct ahc_syncrate *
   1108 ahc_find_syncrate(struct ahc_softc *ahc, u_int *period, u_int maxsync)
   1109 {
   1110 	struct ahc_syncrate *syncrate;
   1111 
   1112 	syncrate = &ahc_syncrates[maxsync];
   1113 	while ((syncrate->rate != NULL)
   1114 	    && ((ahc->features & AHC_ULTRA2) == 0
   1115 	     || (syncrate->sxfr_u2 != 0))) {
   1116 
   1117 		if (*period <= syncrate->period) {
   1118 			/*
   1119 			 * When responding to a target that requests
   1120 			 * sync, the requested rate may fall between
   1121 			 * two rates that we can output, but still be
   1122 			 * a rate that we can receive.  Because of this,
   1123 			 * we want to respond to the target with
   1124 			 * the same rate that it sent to us even
   1125 			 * if the period we use to send data to it
   1126 			 * is lower.  Only lower the response period
   1127 			 * if we must.
   1128 			 */
   1129 			if (syncrate == &ahc_syncrates[maxsync])
   1130 				*period = syncrate->period;
   1131 			break;
   1132 		}
   1133 		syncrate++;
   1134 	}
   1135 
   1136 	if ((*period == 0)
   1137 	 || (syncrate->rate == NULL)
   1138 	 || ((ahc->features & AHC_ULTRA2) != 0
   1139 	  && (syncrate->sxfr_u2 == 0))) {
   1140 		/* Use asynchronous transfers. */
   1141 		*period = 0;
   1142 		syncrate = NULL;
   1143 	}
   1144 	return (syncrate);
   1145 }
   1146 
   1147 static u_int
   1148 ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
   1149 {
   1150 	struct ahc_syncrate *syncrate;
   1151 
   1152 	if ((ahc->features & AHC_ULTRA2) != 0)
   1153 		scsirate &= SXFR_ULTRA2;
   1154 	else
   1155 		scsirate &= SXFR;
   1156 
   1157 	syncrate = &ahc_syncrates[maxsync];
   1158 	while (syncrate->rate != NULL) {
   1159 
   1160 		if ((ahc->features & AHC_ULTRA2) != 0) {
   1161 			if (syncrate->sxfr_u2 == 0)
   1162 				break;
   1163 			else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
   1164 				return (syncrate->period);
   1165 		} else if (scsirate == (syncrate->sxfr & SXFR)) {
   1166 				return (syncrate->period);
   1167 		}
   1168 		syncrate++;
   1169 	}
   1170 	return (0); /* async */
   1171 }
   1172 
   1173 static void
   1174 ahc_validate_offset(struct ahc_softc *ahc, struct ahc_syncrate *syncrate,
   1175 		    u_int *offset, int wide)
   1176 {
   1177 	u_int maxoffset;
   1178 
   1179 	/* Limit offset to what we can do */
   1180 	if (syncrate == NULL) {
   1181 		maxoffset = 0;
   1182 	} else if ((ahc->features & AHC_ULTRA2) != 0) {
   1183 		maxoffset = MAX_OFFSET_ULTRA2;
   1184 	} else {
   1185 		if (wide)
   1186 			maxoffset = MAX_OFFSET_16BIT;
   1187 		else
   1188 			maxoffset = MAX_OFFSET_8BIT;
   1189 	}
   1190 	*offset = MIN(*offset, maxoffset);
   1191 }
   1192 
   1193 static void
   1194 ahc_update_target_msg_request(struct ahc_softc *ahc,
   1195 			      struct ahc_devinfo *devinfo,
   1196 			      struct ahc_initiator_tinfo *tinfo,
   1197 			      int force, int paused)
   1198 {
   1199 	u_int targ_msg_req_orig;
   1200 
   1201 	targ_msg_req_orig = ahc->targ_msg_req;
   1202 	if (tinfo->current.period != tinfo->goal.period
   1203 	 || tinfo->current.width != tinfo->goal.width
   1204 	 || tinfo->current.offset != tinfo->goal.offset
   1205 	 || (force
   1206 	  && (tinfo->goal.period != 0
   1207 	   || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT))) {
   1208 		ahc->targ_msg_req |= devinfo->target_mask;
   1209 	} else {
   1210 		ahc->targ_msg_req &= ~devinfo->target_mask;
   1211 	}
   1212 
   1213 	if (ahc->targ_msg_req != targ_msg_req_orig) {
   1214 		/* Update the message request bit for this target */
   1215 		if ((ahc->features & AHC_HS_MAILBOX) != 0) {
   1216 			if (paused) {
   1217 				ahc_outb(ahc, TARGET_MSG_REQUEST,
   1218 					 ahc->targ_msg_req & 0xFF);
   1219 				ahc_outb(ahc, TARGET_MSG_REQUEST + 1,
   1220 					 (ahc->targ_msg_req >> 8) & 0xFF);
   1221 			} else {
   1222 				ahc_outb(ahc, HS_MAILBOX,
   1223 					 0x01 << HOST_MAILBOX_SHIFT);
   1224 			}
   1225 		} else {
   1226 			if (!paused)
   1227 				pause_sequencer(ahc);
   1228 
   1229 			ahc_outb(ahc, TARGET_MSG_REQUEST,
   1230 				 ahc->targ_msg_req & 0xFF);
   1231 			ahc_outb(ahc, TARGET_MSG_REQUEST + 1,
   1232 				 (ahc->targ_msg_req >> 8) & 0xFF);
   1233 
   1234 			if (!paused)
   1235 				unpause_sequencer(ahc);
   1236 		}
   1237 	}
   1238 }
   1239 
   1240 static void
   1241 ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
   1242 		 struct ahc_syncrate *syncrate,
   1243 		 u_int period, u_int offset, u_int type, int paused, int done)
   1244 {
   1245 	struct	ahc_initiator_tinfo *tinfo;
   1246 	struct	tmode_tstate *tstate;
   1247 	u_int	old_period;
   1248 	u_int	old_offset;
   1249 	int	active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
   1250 
   1251 	if (syncrate == NULL) {
   1252 		period = 0;
   1253 		offset = 0;
   1254 	}
   1255 
   1256 	tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
   1257 				    devinfo->target, &tstate);
   1258 	old_period = tinfo->current.period;
   1259 	old_offset = tinfo->current.offset;
   1260 
   1261 	if ((type & AHC_TRANS_CUR) != 0
   1262 	 && (old_period != period || old_offset != offset)) {
   1263 		u_int	scsirate;
   1264 
   1265 		scsirate = tinfo->scsirate;
   1266 		if ((ahc->features & AHC_ULTRA2) != 0) {
   1267 
   1268 			/* XXX */
   1269 			/* Force single edge until DT is fully implemented */
   1270 			scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
   1271 			if (syncrate != NULL)
   1272 				scsirate |= syncrate->sxfr_u2|SINGLE_EDGE;
   1273 
   1274 			if (active)
   1275 				ahc_outb(ahc, SCSIOFFSET, offset);
   1276 		} else {
   1277 
   1278 			scsirate &= ~(SXFR|SOFS);
   1279 			/*
   1280 			 * Ensure Ultra mode is set properly for
   1281 			 * this target.
   1282 			 */
   1283 			tstate->ultraenb &= ~devinfo->target_mask;
   1284 			if (syncrate != NULL) {
   1285 				if (syncrate->sxfr & ULTRA_SXFR) {
   1286 					tstate->ultraenb |=
   1287 						devinfo->target_mask;
   1288 				}
   1289 				scsirate |= syncrate->sxfr & SXFR;
   1290 				scsirate |= offset & SOFS;
   1291 			}
   1292 			if (active) {
   1293 				u_int sxfrctl0;
   1294 
   1295 				sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
   1296 				sxfrctl0 &= ~FAST20;
   1297 				if (tstate->ultraenb & devinfo->target_mask)
   1298 					sxfrctl0 |= FAST20;
   1299 				ahc_outb(ahc, SXFRCTL0, sxfrctl0);
   1300 			}
   1301 		}
   1302 		if (active)
   1303 			ahc_outb(ahc, SCSIRATE, scsirate);
   1304 
   1305 		tinfo->scsirate = scsirate;
   1306 		tinfo->current.period = period;
   1307 		tinfo->current.offset = offset;
   1308 
   1309 		/* Update the syncrates in any pending scbs */
   1310 		ahc_update_pending_syncrates(ahc);
   1311 	}
   1312 
   1313 	/*
   1314 	 * Print messages if we're verbose and at the end of a negotiation
   1315 	 * cycle.
   1316 	 */
   1317 	if (done) {
   1318 		if (offset != 0) {
   1319 			printf("%s: target %d synchronous at %sMHz, "
   1320 			       "offset = 0x%x\n", ahc_name(ahc),
   1321 			       devinfo->target, syncrate->rate, offset);
   1322 		} else {
   1323 			printf("%s: target %d using "
   1324 			       "asynchronous transfers\n",
   1325 			       ahc_name(ahc), devinfo->target);
   1326 		}
   1327 	}
   1328 
   1329 	if ((type & AHC_TRANS_GOAL) != 0) {
   1330 		tinfo->goal.period = period;
   1331 		tinfo->goal.offset = offset;
   1332 	}
   1333 
   1334 	if ((type & AHC_TRANS_USER) != 0) {
   1335 		tinfo->user.period = period;
   1336 		tinfo->user.offset = offset;
   1337 	}
   1338 
   1339 	ahc_update_target_msg_request(ahc, devinfo, tinfo,
   1340 				      /*force*/FALSE,
   1341 				      paused);
   1342 }
   1343 
   1344 static void
   1345 ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
   1346 	      u_int width, u_int type, int paused, int done)
   1347 {
   1348 	struct ahc_initiator_tinfo *tinfo;
   1349 	struct tmode_tstate *tstate;
   1350 	u_int  oldwidth;
   1351 	int    active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
   1352 
   1353 	tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
   1354 				    devinfo->target, &tstate);
   1355 	oldwidth = tinfo->current.width;
   1356 
   1357 	if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
   1358 		u_int	scsirate;
   1359 
   1360 		scsirate =  tinfo->scsirate;
   1361 		scsirate &= ~WIDEXFER;
   1362 		if (width == MSG_EXT_WDTR_BUS_16_BIT)
   1363 			scsirate |= WIDEXFER;
   1364 
   1365 		tinfo->scsirate = scsirate;
   1366 
   1367 		if (active)
   1368 			ahc_outb(ahc, SCSIRATE, scsirate);
   1369 
   1370 		tinfo->current.width = width;
   1371 	}
   1372 
   1373 	if (done) {
   1374 		printf("%s: target %d using %dbit transfers\n",
   1375 		       ahc_name(ahc), devinfo->target,
   1376 		       8 * (0x01 << width));
   1377 	}
   1378 
   1379 	if ((type & AHC_TRANS_GOAL) != 0)
   1380 		tinfo->goal.width = width;
   1381 	if ((type & AHC_TRANS_USER) != 0)
   1382 		tinfo->user.width = width;
   1383 
   1384 	ahc_update_target_msg_request(ahc, devinfo, tinfo,
   1385 				      /*force*/FALSE, paused);
   1386 }
   1387 
   1388 static void
   1389 ahc_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, int enable)
   1390 {
   1391 	struct ahc_initiator_tinfo *tinfo;
   1392 	struct tmode_tstate *tstate;
   1393 
   1394 	tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
   1395 				    devinfo->target, &tstate);
   1396 
   1397 	if (enable)
   1398 		tstate->tagenable |= devinfo->target_mask;
   1399 	else {
   1400 		tstate->tagenable &= ~devinfo->target_mask;
   1401 		tstate->tagdisable |= devinfo->target_mask;
   1402 	}
   1403 }
   1404 
   1405 /*
   1406  * Attach all the sub-devices we can find
   1407  */
   1408 int
   1409 ahc_attach(struct ahc_softc *ahc)
   1410 {
   1411 	TAILQ_INIT(&ahc->sc_q);
   1412 
   1413 	ahc->sc_adapter.scsipi_cmd = ahc_action;
   1414 	ahc->sc_adapter.scsipi_minphys = ahcminphys;
   1415 	ahc->sc_adapter.scsipi_ioctl = ahc_ioctl;
   1416 	ahc->sc_link.type = BUS_SCSI;
   1417 	ahc->sc_link.scsipi_scsi.adapter_target = ahc->our_id;
   1418 	ahc->sc_link.scsipi_scsi.channel = 0;
   1419 	ahc->sc_link.scsipi_scsi.max_target =
   1420 	    (ahc->features & AHC_WIDE) ? 15 : 7;
   1421 	ahc->sc_link.scsipi_scsi.max_lun = 7;
   1422 	ahc->sc_link.adapter_softc = ahc;
   1423 	ahc->sc_link.adapter = &ahc->sc_adapter;
   1424 	ahc->sc_link.openings = 2;
   1425 	ahc->sc_link.device = &ahc_dev;
   1426 
   1427 	if (ahc->features & AHC_TWIN) {
   1428 		ahc->sc_link_b = ahc->sc_link;
   1429 		ahc->sc_link_b.scsipi_scsi.adapter_target = ahc->our_id_b;
   1430 		ahc->sc_link_b.scsipi_scsi.channel = 1;
   1431 	}
   1432 
   1433 	if ((ahc->flags & AHC_CHANNEL_B_PRIMARY) == 0) {
   1434 		ahc->sc_link_b.scsipi_scsi.scsibus = 0xff;
   1435 		config_found((void *)ahc, &ahc->sc_link, scsiprint);
   1436 		if (ahc->features & AHC_TWIN)
   1437 			config_found((void *)ahc, &ahc->sc_link_b, scsiprint);
   1438 	} else {
   1439 		config_found((void *)ahc, &ahc->sc_link_b, scsiprint);
   1440 		config_found((void *)ahc, &ahc->sc_link, scsiprint);
   1441 	}
   1442 	return 1;
   1443 }
   1444 
   1445 static void
   1446 ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
   1447 {
   1448 	u_int	saved_tcl;
   1449 	role_t	role;
   1450 	int	our_id;
   1451 
   1452 	if (ahc_inb(ahc, SSTAT0) & TARGET)
   1453 		role = ROLE_TARGET;
   1454 	else
   1455 		role = ROLE_INITIATOR;
   1456 
   1457 	if (role == ROLE_TARGET
   1458 	 && (ahc->features & AHC_MULTI_TID) != 0
   1459 	 && (ahc_inb(ahc, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
   1460 		/* We were selected, so pull our id from TARGIDIN */
   1461 		our_id = ahc_inb(ahc, TARGIDIN) & OID;
   1462 	} else if ((ahc->features & AHC_ULTRA2) != 0)
   1463 		our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
   1464 	else
   1465 		our_id = ahc_inb(ahc, SCSIID) & OID;
   1466 
   1467 	saved_tcl = ahc_inb(ahc, SAVED_TCL);
   1468 	ahc_compile_devinfo(devinfo, our_id, TCL_TARGET(saved_tcl),
   1469 			    TCL_LUN(saved_tcl), TCL_CHANNEL(ahc, saved_tcl),
   1470 			    role);
   1471 }
   1472 
   1473 static void
   1474 ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
   1475 		    u_int lun, char channel, role_t role)
   1476 {
   1477 	devinfo->our_scsiid = our_id;
   1478 	devinfo->target = target;
   1479 	devinfo->lun = lun;
   1480 	devinfo->target_offset = target;
   1481 	devinfo->channel = channel;
   1482 	devinfo->role = role;
   1483 	if (channel == 'B')
   1484 		devinfo->target_offset += 8;
   1485 	devinfo->target_mask = (0x01 << devinfo->target_offset);
   1486 }
   1487 
   1488 /*
   1489  * Catch an interrupt from the adapter
   1490  */
   1491 int
   1492 ahc_intr(void *arg)
   1493 {
   1494 	struct	ahc_softc *ahc;
   1495 	u_int	intstat;
   1496 
   1497 	ahc = (struct ahc_softc *)arg;
   1498 
   1499 	intstat = ahc_inb(ahc, INTSTAT);
   1500 
   1501 	/*
   1502 	 * Any interrupts to process?
   1503 	 */
   1504 	if ((intstat & INT_PEND) == 0) {
   1505 		if (ahc->bus_intr && ahc->bus_intr(ahc)) {
   1506 #ifdef AHC_DEBUG
   1507 			printf("%s: bus intr: CCHADDR %x HADDR %x SEQADDR %x\n",
   1508 			    ahc_name(ahc),
   1509 			    ahc_inb(ahc, CCHADDR) |
   1510 			    (ahc_inb(ahc, CCHADDR+1) << 8)
   1511 			    | (ahc_inb(ahc, CCHADDR+2) << 16)
   1512 			    | (ahc_inb(ahc, CCHADDR+3) << 24),
   1513 			    ahc_inb(ahc, HADDR) | (ahc_inb(ahc, HADDR+1) << 8)
   1514 			    | (ahc_inb(ahc, HADDR+2) << 16)
   1515 			    | (ahc_inb(ahc, HADDR+3) << 24),
   1516 			    ahc_inb(ahc, SEQADDR0) |
   1517 			    (ahc_inb(ahc, SEQADDR1) << 8));
   1518 #endif
   1519 			return 1;
   1520 		}
   1521 		return 0;
   1522 	}
   1523 
   1524 #ifdef AHC_DEBUG
   1525 	if (ahc_debug & AHC_SHOWINTR) {
   1526 		printf("%s: intstat %x\n", ahc_name(ahc), intstat);
   1527 	}
   1528 #endif
   1529 
   1530 	if (intstat & CMDCMPLT) {
   1531 		ahc_outb(ahc, CLRINT, CLRCMDINT);
   1532 		ahc_run_qoutfifo(ahc);
   1533 	}
   1534 	if (intstat & BRKADRINT) {
   1535 		/*
   1536 		 * We upset the sequencer :-(
   1537 		 * Lookup the error message
   1538 		 */
   1539 		int i, error, num_errors;
   1540 
   1541 		error = ahc_inb(ahc, ERROR);
   1542 		num_errors =  sizeof(hard_error)/sizeof(hard_error[0]);
   1543 		for (i = 0; error != 1 && i < num_errors; i++)
   1544 			error >>= 1;
   1545 		panic("%s: brkadrint, %s at seqaddr = 0x%x\n",
   1546 		      ahc_name(ahc), hard_error[i].errmesg,
   1547 		      ahc_inb(ahc, SEQADDR0) |
   1548 		      (ahc_inb(ahc, SEQADDR1) << 8));
   1549 
   1550 		/* Tell everyone that this HBA is no longer availible */
   1551 		ahc_abort_scbs(ahc, AHC_TARGET_WILDCARD, ALL_CHANNELS,
   1552 			       AHC_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
   1553 			       XS_DRIVER_STUFFUP);
   1554 	}
   1555 	if (intstat & SEQINT)
   1556 		ahc_handle_seqint(ahc, intstat);
   1557 
   1558 	if (intstat & SCSIINT)
   1559 		ahc_handle_scsiint(ahc, intstat);
   1560 
   1561 	return 1;
   1562 }
   1563 
   1564 static struct tmode_tstate *
   1565 ahc_alloc_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel)
   1566 {
   1567 	struct tmode_tstate *master_tstate;
   1568 	struct tmode_tstate *tstate;
   1569 	int i, s;
   1570 
   1571 	master_tstate = ahc->enabled_targets[ahc->our_id];
   1572 	if (channel == 'B') {
   1573 		scsi_id += 8;
   1574 		master_tstate = ahc->enabled_targets[ahc->our_id_b + 8];
   1575 	}
   1576 	if (ahc->enabled_targets[scsi_id] != NULL
   1577 	 && ahc->enabled_targets[scsi_id] != master_tstate)
   1578 		panic("%s: ahc_alloc_tstate - Target already allocated",
   1579 		      ahc_name(ahc));
   1580 	tstate = malloc(sizeof(*tstate), M_DEVBUF, M_NOWAIT);
   1581 	if (tstate == NULL)
   1582 		return (NULL);
   1583 
   1584 	/*
   1585 	 * If we have allocated a master tstate, copy user settings from
   1586 	 * the master tstate (taken from SRAM or the EEPROM) for this
   1587 	 * channel, but reset our current and goal settings to async/narrow
   1588 	 * until an initiator talks to us.
   1589 	 */
   1590 	if (master_tstate != NULL) {
   1591 		bcopy(master_tstate, tstate, sizeof(*tstate));
   1592 		tstate->ultraenb = 0;
   1593 		for (i = 0; i < 16; i++) {
   1594 			bzero(&tstate->transinfo[i].current,
   1595 			      sizeof(tstate->transinfo[i].current));
   1596 			bzero(&tstate->transinfo[i].goal,
   1597 			      sizeof(tstate->transinfo[i].goal));
   1598 		}
   1599 	} else
   1600 		bzero(tstate, sizeof(*tstate));
   1601 	s = splbio();
   1602 	ahc->enabled_targets[scsi_id] = tstate;
   1603 	splx(s);
   1604 	return (tstate);
   1605 }
   1606 
   1607 #if UNUSED
   1608 static void
   1609 ahc_free_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel, int force)
   1610 {
   1611 	struct tmode_tstate *tstate;
   1612 
   1613 	/* Don't clean up the entry for our initiator role */
   1614 	if ((ahc->flags & AHC_INITIATORMODE) != 0
   1615 	 && ((channel == 'B' && scsi_id == ahc->our_id_b)
   1616 	  || (channel == 'A' && scsi_id == ahc->our_id))
   1617 	 && force == FALSE)
   1618 		return;
   1619 
   1620 	if (channel == 'B')
   1621 		scsi_id += 8;
   1622 	tstate = ahc->enabled_targets[scsi_id];
   1623 	if (tstate != NULL)
   1624 		free(tstate, M_DEVBUF);
   1625 	ahc->enabled_targets[scsi_id] = NULL;
   1626 }
   1627 #endif
   1628 
   1629 static void
   1630 ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat)
   1631 {
   1632 	struct scb *scb;
   1633 	struct ahc_devinfo devinfo;
   1634 
   1635 	ahc_fetch_devinfo(ahc, &devinfo);
   1636 
   1637 	/*
   1638 	 * Clear the upper byte that holds SEQINT status
   1639 	 * codes and clear the SEQINT bit. We will unpause
   1640 	 * the sequencer, if appropriate, after servicing
   1641 	 * the request.
   1642 	 */
   1643 	ahc_outb(ahc, CLRINT, CLRSEQINT);
   1644 	switch (intstat & SEQINT_MASK) {
   1645 	case NO_MATCH:
   1646 	{
   1647 		/* Ensure we don't leave the selection hardware on */
   1648 		ahc_outb(ahc, SCSISEQ,
   1649 			 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
   1650 
   1651 		printf("%s:%c:%d: no active SCB for reconnecting "
   1652 		       "target - issuing BUS DEVICE RESET\n",
   1653 		       ahc_name(ahc), devinfo.channel, devinfo.target);
   1654 		printf("SAVED_TCL == 0x%x, ARG_1 == 0x%x, SEQ_FLAGS == 0x%x\n",
   1655 		       ahc_inb(ahc, SAVED_TCL), ahc_inb(ahc, ARG_1),
   1656 		       ahc_inb(ahc, SEQ_FLAGS));
   1657 		ahc->msgout_buf[0] = MSG_BUS_DEV_RESET;
   1658 		ahc->msgout_len = 1;
   1659 		ahc->msgout_index = 0;
   1660 		ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
   1661 		ahc_outb(ahc, MSG_OUT, HOST_MSG);
   1662 		ahc_outb(ahc, SCSISIGO, ahc_inb(ahc, LASTPHASE) | ATNO);
   1663 		break;
   1664 	}
   1665 	case UPDATE_TMSG_REQ:
   1666 		ahc_outb(ahc, TARGET_MSG_REQUEST, ahc->targ_msg_req & 0xFF);
   1667 		ahc_outb(ahc, TARGET_MSG_REQUEST + 1,
   1668 			 (ahc->targ_msg_req >> 8) & 0xFF);
   1669 		ahc_outb(ahc, HS_MAILBOX, 0);
   1670 		break;
   1671 	case SEND_REJECT:
   1672 	{
   1673 		u_int rejbyte = ahc_inb(ahc, ACCUM);
   1674 		printf("%s:%c:%d: Warning - unknown message received from "
   1675 		       "target (0x%x).  Rejecting\n",
   1676 		       ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte);
   1677 		break;
   1678 	}
   1679 	case NO_IDENT:
   1680 	{
   1681 		/*
   1682 		 * The reconnecting target either did not send an identify
   1683 		 * message, or did, but we didn't find and SCB to match and
   1684 		 * before it could respond to our ATN/abort, it hit a dataphase.
   1685 		 * The only safe thing to do is to blow it away with a bus
   1686 		 * reset.
   1687 		 */
   1688 		int found;
   1689 
   1690 		printf("%s:%c:%d: Target did not send an IDENTIFY message. "
   1691 		       "LASTPHASE = 0x%x, SAVED_TCL == 0x%x\n",
   1692 		       ahc_name(ahc), devinfo.channel, devinfo.target,
   1693 		       ahc_inb(ahc, LASTPHASE), ahc_inb(ahc, SAVED_TCL));
   1694 		found = ahc_reset_channel(ahc, devinfo.channel,
   1695 					  /*initiate reset*/TRUE);
   1696 		printf("%s: Issued Channel %c Bus Reset. "
   1697 		       "%d SCBs aborted\n", ahc_name(ahc), devinfo.channel,
   1698 		       found);
   1699 		return;
   1700 	}
   1701 	case BAD_PHASE:
   1702 	{
   1703 		u_int lastphase;
   1704 
   1705 		lastphase = ahc_inb(ahc, LASTPHASE);
   1706 		if (lastphase == P_BUSFREE) {
   1707 			printf("%s:%c:%d: Missed busfree.  Curphase = 0x%x\n",
   1708 			       ahc_name(ahc), devinfo.channel, devinfo.target,
   1709 			       ahc_inb(ahc, SCSISIGI));
   1710 			restart_sequencer(ahc);
   1711 			return;
   1712 		} else {
   1713 			printf("%s:%c:%d: unknown scsi bus phase %x.  "
   1714 			       "Attempting to continue\n",
   1715 			       ahc_name(ahc), devinfo.channel, devinfo.target,
   1716 			       ahc_inb(ahc, SCSISIGI));
   1717 		}
   1718 		break;
   1719 	}
   1720 	case BAD_STATUS:
   1721 	{
   1722 		u_int  scb_index;
   1723 		struct hardware_scb *hscb;
   1724 		struct scsipi_xfer *xs;
   1725 		/*
   1726 		 * The sequencer will notify us when a command
   1727 		 * has an error that would be of interest to
   1728 		 * the kernel.  This allows us to leave the sequencer
   1729 		 * running in the common case of command completes
   1730 		 * without error.  The sequencer will already have
   1731 		 * dma'd the SCB back up to us, so we can reference
   1732 		 * the in kernel copy directly.
   1733 		 */
   1734 		scb_index = ahc_inb(ahc, SCB_TAG);
   1735 		scb = &ahc->scb_data->scbarray[scb_index];
   1736 
   1737 		/* ahc_print_scb(scb); */
   1738 
   1739 		/*
   1740 		 * Set the default return value to 0 (don't
   1741 		 * send sense).  The sense code will change
   1742 		 * this if needed.
   1743 		 */
   1744 		ahc_outb(ahc, RETURN_1, 0);
   1745 		if (!(scb_index < ahc->scb_data->numscbs
   1746 		   && (scb->flags & SCB_ACTIVE) != 0)) {
   1747 			printf("%s:%c:%d: ahc_intr - referenced scb "
   1748 			       "not valid during seqint 0x%x scb(%d)\n",
   1749 			       ahc_name(ahc), devinfo.channel,
   1750 			       devinfo.target, intstat, scb_index);
   1751 			goto unpause;
   1752 		}
   1753 
   1754 		hscb = scb->hscb;
   1755 		xs = scb->xs;
   1756 
   1757 		/* Don't want to clobber the original sense code */
   1758 		if ((scb->flags & SCB_SENSE) != 0) {
   1759 			/*
   1760 			 * Clear the SCB_SENSE Flag and have
   1761 			 * the sequencer do a normal command
   1762 			 * complete.
   1763 			 */
   1764 			scb->flags &= ~SCB_SENSE;
   1765 			ahcsetccbstatus(xs, XS_DRIVER_STUFFUP);
   1766 			break;
   1767 		}
   1768 		/* Freeze the queue unit the client sees the error. */
   1769 		ahc_freeze_devq(ahc, xs->sc_link);
   1770 		ahc_freeze_ccb(scb);
   1771 		xs->status = hscb->status;
   1772 		switch (hscb->status) {
   1773 		case SCSI_STATUS_OK:
   1774 			printf("%s: Interrupted for status of 0???\n",
   1775 			       ahc_name(ahc));
   1776 			break;
   1777 		case SCSI_STATUS_CMD_TERMINATED:
   1778 		case SCSI_STATUS_CHECK_COND:
   1779 #if defined(AHC_DEBUG)
   1780 			if (ahc_debug & AHC_SHOWSENSE) {
   1781 				scsi_print_addr(xs->sc_link);
   1782 				printf("Check Status, resid %d datalen %d\n",
   1783 				    xs->resid, xs->datalen);
   1784 			}
   1785 #endif
   1786 
   1787 			if (xs->error == XS_NOERROR &&
   1788 			    !(scb->flags & SCB_SENSE)) {
   1789 				struct ahc_dma_seg *sg;
   1790 				struct scsipi_sense *sc;
   1791 				struct ahc_initiator_tinfo *tinfo;
   1792 				struct tmode_tstate *tstate;
   1793 
   1794 				sg = scb->sg_list;
   1795 				sc = (struct scsipi_sense *)(&hscb->cmdstore);
   1796 				/*
   1797 				 * Save off the residual if there is one.
   1798 				 */
   1799 				if (hscb->residual_SG_count != 0)
   1800 					ahc_calc_residual(scb);
   1801 				else
   1802 					xs->resid = 0;
   1803 
   1804 #ifdef AHC_DEBUG
   1805 				if (ahc_debug & AHC_SHOWSENSE) {
   1806 					scsi_print_addr(xs->sc_link);
   1807 					printf("Sending Sense\n");
   1808 				}
   1809 #endif
   1810 				sg->addr = ahc->scb_data->sense_busaddr +
   1811 				   (hscb->tag*sizeof(struct scsipi_sense_data));
   1812 				sg->len = sizeof (struct scsipi_sense_data);
   1813 
   1814 				sc->opcode = REQUEST_SENSE;
   1815 				sc->byte2 =  SCB_LUN(scb) << 5;
   1816 				sc->unused[0] = 0;
   1817 				sc->unused[1] = 0;
   1818 				sc->length = sg->len;
   1819 				sc->control = 0;
   1820 
   1821 				/*
   1822 				 * Would be nice to preserve DISCENB here,
   1823 				 * but due to the way we page SCBs, we can't.
   1824 				 */
   1825 				hscb->control = 0;
   1826 
   1827 				/*
   1828 				 * This request sense could be because the
   1829 				 * the device lost power or in some other
   1830 				 * way has lost our transfer negotiations.
   1831 				 * Renegotiate if appropriate.  Unit attention
   1832 				 * errors will be reported before any data
   1833 				 * phases occur.
   1834 				 */
   1835 				ahc_calc_residual(scb);
   1836 #if defined(AHC_DEBUG)
   1837 				if (ahc_debug & AHC_SHOWSENSE) {
   1838 					scsi_print_addr(xs->sc_link);
   1839 					printf("Sense: datalen %d resid %d"
   1840 					       "chan %d id %d targ %d\n",
   1841 					    xs->datalen, xs->resid,
   1842 					    devinfo.channel, devinfo.our_scsiid,
   1843 					    devinfo.target);
   1844 				}
   1845 #endif
   1846 				if (xs->datalen > 0 &&
   1847 				    xs->resid == xs->datalen) {
   1848 					tinfo = ahc_fetch_transinfo(ahc,
   1849 							    devinfo.channel,
   1850 							    devinfo.our_scsiid,
   1851 							    devinfo.target,
   1852 							    &tstate);
   1853 					ahc_update_target_msg_request(ahc,
   1854 							      &devinfo,
   1855 							      tinfo,
   1856 							      /*force*/TRUE,
   1857 							      /*paused*/TRUE);
   1858 				}
   1859 				hscb->status = 0;
   1860 				hscb->SG_count = 1;
   1861 				hscb->SG_pointer = scb->sg_list_phys;
   1862 				hscb->data = sg->addr;
   1863 				hscb->datalen = sg->len;
   1864 				hscb->cmdpointer = hscb->cmdstore_busaddr;
   1865 				hscb->cmdlen = sizeof(*sc);
   1866 				scb->sg_count = hscb->SG_count;
   1867 				ahc_swap_hscb(hscb);
   1868 				ahc_swap_sg(scb->sg_list);
   1869 				scb->flags |= SCB_SENSE;
   1870 				/*
   1871 				 * Ensure the target is busy since this
   1872 				 * will be an untagged request.
   1873 				 */
   1874 				ahc_busy_tcl(ahc, scb);
   1875 				ahc_outb(ahc, RETURN_1, SEND_SENSE);
   1876 
   1877 				/*
   1878 				 * Ensure we have enough time to actually
   1879 				 * retrieve the sense.
   1880 				 */
   1881 				if (!(scb->xs->xs_control & XS_CTL_POLL)) {
   1882 					callout_reset(&scb->xs->xs_callout,
   1883 					    5 * hz, ahc_timeout, scb);
   1884 				}
   1885 			}
   1886 			break;
   1887 		case SCSI_STATUS_QUEUE_FULL:
   1888 			scsi_print_addr(xs->sc_link);
   1889 			printf("queue full\n");
   1890 		case SCSI_STATUS_BUSY:
   1891 			/*
   1892 			 * Requeue any transactions that haven't been
   1893 			 * sent yet.
   1894 			 */
   1895 			ahc_freeze_devq(ahc, xs->sc_link);
   1896 			ahc_freeze_ccb(scb);
   1897 			break;
   1898 		}
   1899 		break;
   1900 	}
   1901 	case TRACE_POINT:
   1902 	{
   1903 		printf("SSTAT2 = 0x%x DFCNTRL = 0x%x\n", ahc_inb(ahc, SSTAT2),
   1904 		       ahc_inb(ahc, DFCNTRL));
   1905 		printf("SSTAT3 = 0x%x DSTATUS = 0x%x\n", ahc_inb(ahc, SSTAT3),
   1906 		       ahc_inb(ahc, DFSTATUS));
   1907 		printf("SSTAT0 = 0x%x, SCB_DATACNT = 0x%x\n",
   1908 		       ahc_inb(ahc, SSTAT0),
   1909 		       ahc_inb(ahc, SCB_DATACNT));
   1910 		break;
   1911 	}
   1912 	case HOST_MSG_LOOP:
   1913 	{
   1914 		/*
   1915 		 * The sequencer has encountered a message phase
   1916 		 * that requires host assistance for completion.
   1917 		 * While handling the message phase(s), we will be
   1918 		 * notified by the sequencer after each byte is
   1919 		 * transfered so we can track bus phases.
   1920 		 *
   1921 		 * If this is the first time we've seen a HOST_MSG_LOOP,
   1922 		 * initialize the state of the host message loop.
   1923 		 */
   1924 		if (ahc->msg_type == MSG_TYPE_NONE) {
   1925 			u_int bus_phase;
   1926 
   1927 			bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
   1928 			if (bus_phase != P_MESGIN
   1929 			 && bus_phase != P_MESGOUT) {
   1930 				printf("ahc_intr: HOST_MSG_LOOP bad "
   1931 				       "phase 0x%x\n",
   1932 				      bus_phase);
   1933 				/*
   1934 				 * Probably transitioned to bus free before
   1935 				 * we got here.  Just punt the message.
   1936 				 */
   1937 				ahc_clear_intstat(ahc);
   1938 				restart_sequencer(ahc);
   1939 			}
   1940 
   1941 			if (devinfo.role == ROLE_INITIATOR) {
   1942 				struct scb *scb;
   1943 				u_int scb_index;
   1944 
   1945 				scb_index = ahc_inb(ahc, SCB_TAG);
   1946 				scb = &ahc->scb_data->scbarray[scb_index];
   1947 
   1948 				if (bus_phase == P_MESGOUT)
   1949 					ahc_setup_initiator_msgout(ahc,
   1950 								   &devinfo,
   1951 								   scb);
   1952 				else {
   1953 					ahc->msg_type =
   1954 					    MSG_TYPE_INITIATOR_MSGIN;
   1955 					ahc->msgin_index = 0;
   1956 				}
   1957 			} else {
   1958 				if (bus_phase == P_MESGOUT) {
   1959 					ahc->msg_type =
   1960 					    MSG_TYPE_TARGET_MSGOUT;
   1961 					ahc->msgin_index = 0;
   1962 				} else
   1963 					/* XXX Ever executed??? */
   1964 					ahc_setup_target_msgin(ahc, &devinfo);
   1965 			}
   1966 		}
   1967 
   1968 		/* Pass a NULL path so that handlers generate their own */
   1969 		ahc_handle_message_phase(ahc, /*path*/NULL);
   1970 		break;
   1971 	}
   1972 	case PERR_DETECTED:
   1973 	{
   1974 		/*
   1975 		 * If we've cleared the parity error interrupt
   1976 		 * but the sequencer still believes that SCSIPERR
   1977 		 * is true, it must be that the parity error is
   1978 		 * for the currently presented byte on the bus,
   1979 		 * and we are not in a phase (data-in) where we will
   1980 		 * eventually ack this byte.  Ack the byte and
   1981 		 * throw it away in the hope that the target will
   1982 		 * take us to message out to deliver the appropriate
   1983 		 * error message.
   1984 		 */
   1985 		if ((intstat & SCSIINT) == 0
   1986 		 && (ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0) {
   1987 			u_int curphase;
   1988 
   1989 			/*
   1990 			 * The hardware will only let you ack bytes
   1991 			 * if the expected phase in SCSISIGO matches
   1992 			 * the current phase.  Make sure this is
   1993 			 * currently the case.
   1994 			 */
   1995 			curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
   1996 			ahc_outb(ahc, LASTPHASE, curphase);
   1997 			ahc_outb(ahc, SCSISIGO, curphase);
   1998 			ahc_inb(ahc, SCSIDATL);
   1999 		}
   2000 		break;
   2001 	}
   2002 	case DATA_OVERRUN:
   2003 	{
   2004 		/*
   2005 		 * When the sequencer detects an overrun, it
   2006 		 * places the controller in "BITBUCKET" mode
   2007 		 * and allows the target to complete its transfer.
   2008 		 * Unfortunately, none of the counters get updated
   2009 		 * when the controller is in this mode, so we have
   2010 		 * no way of knowing how large the overrun was.
   2011 		 */
   2012 		u_int scbindex = ahc_inb(ahc, SCB_TAG);
   2013 		u_int lastphase = ahc_inb(ahc, LASTPHASE);
   2014 		int i;
   2015 
   2016 		scb = &ahc->scb_data->scbarray[scbindex];
   2017 		for (i = 0; i < num_phases; i++) {
   2018 			if (lastphase == phase_table[i].phase)
   2019 				break;
   2020 		}
   2021 		scsi_print_addr(scb->xs->sc_link);
   2022 		printf("data overrun detected %s."
   2023 		       "  Tag == 0x%x.\n",
   2024 		       phase_table[i].phasemsg,
   2025   		       scb->hscb->tag);
   2026 		scsi_print_addr(scb->xs->sc_link);
   2027 		printf("%s seen Data Phase.  Length = %d.  NumSGs = %d.\n",
   2028 		       ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't",
   2029 		       scb->xs->datalen, scb->sg_count);
   2030 		if (scb->sg_count > 0) {
   2031 			for (i = 0; i < scb->sg_count; i++) {
   2032 				printf("sg[%d] - Addr 0x%x : Length %d\n",
   2033 				       i,
   2034 				       le32toh(scb->sg_list[i].addr),
   2035 				       le32toh(scb->sg_list[i].len));
   2036 			}
   2037 		}
   2038 		/*
   2039 		 * Set this and it will take affect when the
   2040 		 * target does a command complete.
   2041 		 */
   2042 		ahc_freeze_devq(ahc, scb->xs->sc_link);
   2043 		ahcsetccbstatus(scb->xs, XS_DRIVER_STUFFUP);
   2044 		ahc_freeze_ccb(scb);
   2045 		break;
   2046 	}
   2047 	case TRACEPOINT:
   2048 	{
   2049 		printf("TRACEPOINT: RETURN_1 = %d\n", ahc_inb(ahc, RETURN_1));
   2050 		printf("TRACEPOINT: RETURN_2 = %d\n", ahc_inb(ahc, RETURN_2));
   2051 		printf("TRACEPOINT: ARG_1    = %d\n", ahc_inb(ahc, ARG_1));
   2052 		printf("TRACEPOINT: ARG_2    = %d\n", ahc_inb(ahc, ARG_2));
   2053 		printf("TRACEPOINT: CCHADDR =  %x\n",
   2054 		    ahc_inb(ahc, CCHADDR) | (ahc_inb(ahc, CCHADDR+1) << 8)
   2055 		    | (ahc_inb(ahc, CCHADDR+2) << 16)
   2056 		    | (ahc_inb(ahc, CCHADDR+3) << 24));
   2057 #if 0
   2058 		printf("SSTAT1 == 0x%x\n", ahc_inb(ahc, SSTAT1));
   2059 		printf("SSTAT0 == 0x%x\n", ahc_inb(ahc, SSTAT0));
   2060 		printf(", SCSISIGI == 0x%x\n", ahc_inb(ahc, SCSISIGI));
   2061 		printf("TRACEPOINT: CCHCNT = %d, SG_COUNT = %d\n",
   2062 		       ahc_inb(ahc, CCHCNT), ahc_inb(ahc, SG_COUNT));
   2063 		printf("TRACEPOINT: SCB_TAG = %d\n", ahc_inb(ahc, SCB_TAG));
   2064 		printf("TRACEPOINT1: CCHADDR = %d, CCHCNT = %d, SCBPTR = %d\n",
   2065 		       ahc_inb(ahc, CCHADDR)
   2066 		    | (ahc_inb(ahc, CCHADDR+1) << 8)
   2067 		    | (ahc_inb(ahc, CCHADDR+2) << 16)
   2068 		    | (ahc_inb(ahc, CCHADDR+3) << 24),
   2069 		       ahc_inb(ahc, CCHCNT)
   2070 		    | (ahc_inb(ahc, CCHCNT+1) << 8)
   2071 		    | (ahc_inb(ahc, CCHCNT+2) << 16),
   2072 		       ahc_inb(ahc, SCBPTR));
   2073 		printf("TRACEPOINT: WAITING_SCBH = %d\n", ahc_inb(ahc, WAITING_SCBH));
   2074 		printf("TRACEPOINT: SCB_TAG = %d\n", ahc_inb(ahc, SCB_TAG));
   2075 #if DDB > 0
   2076 		cpu_Debugger();
   2077 #endif
   2078 #endif
   2079 		break;
   2080 	}
   2081 #if NOT_YET
   2082 	/* XXX Fill these in later */
   2083 	case MESG_BUFFER_BUSY:
   2084 		break;
   2085 	case MSGIN_PHASEMIS:
   2086 		break;
   2087 #endif
   2088 	default:
   2089 		printf("ahc_intr: seqint, "
   2090 		       "intstat == 0x%x, scsisigi = 0x%x\n",
   2091 		       intstat, ahc_inb(ahc, SCSISIGI));
   2092 		break;
   2093 	}
   2094 
   2095 unpause:
   2096 	/*
   2097 	 *  The sequencer is paused immediately on
   2098 	 *  a SEQINT, so we should restart it when
   2099 	 *  we're done.
   2100 	 */
   2101 	unpause_sequencer(ahc);
   2102 }
   2103 
   2104 static void
   2105 ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat)
   2106 {
   2107 	u_int	scb_index;
   2108 	u_int	status;
   2109 	struct	scb *scb;
   2110 	char	cur_channel;
   2111 	char	intr_channel;
   2112 
   2113 	if ((ahc->features & AHC_TWIN) != 0
   2114 	 && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0))
   2115 		cur_channel = 'B';
   2116 	else
   2117 		cur_channel = 'A';
   2118 	intr_channel = cur_channel;
   2119 
   2120 	status = ahc_inb(ahc, SSTAT1);
   2121 	if (status == 0) {
   2122 		if ((ahc->features & AHC_TWIN) != 0) {
   2123 			/* Try the other channel */
   2124 		 	ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
   2125 			status = ahc_inb(ahc, SSTAT1);
   2126 		 	ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
   2127 			intr_channel = (cur_channel == 'A') ? 'B' : 'A';
   2128 		}
   2129 		if (status == 0) {
   2130 			printf("%s: Spurious SCSI interrupt\n", ahc_name(ahc));
   2131 			return;
   2132 		}
   2133 	}
   2134 
   2135 	scb_index = ahc_inb(ahc, SCB_TAG);
   2136 	if (scb_index < ahc->scb_data->numscbs) {
   2137 		scb = &ahc->scb_data->scbarray[scb_index];
   2138 		if ((scb->flags & SCB_ACTIVE) == 0
   2139 		 || (ahc_inb(ahc, SEQ_FLAGS) & IDENTIFY_SEEN) == 0)
   2140 			scb = NULL;
   2141 	} else
   2142 		scb = NULL;
   2143 
   2144 	if ((status & SCSIRSTI) != 0) {
   2145 		printf("%s: Someone reset channel %c\n",
   2146 			ahc_name(ahc), intr_channel);
   2147 		ahc_reset_channel(ahc, intr_channel, /* Initiate Reset */FALSE);
   2148 	} else if ((status & SCSIPERR) != 0) {
   2149 		/*
   2150 		 * Determine the bus phase and queue an appropriate message.
   2151 		 * SCSIPERR is latched true as soon as a parity error
   2152 		 * occurs.  If the sequencer acked the transfer that
   2153 		 * caused the parity error and the currently presented
   2154 		 * transfer on the bus has correct parity, SCSIPERR will
   2155 		 * be cleared by CLRSCSIPERR.  Use this to determine if
   2156 		 * we should look at the last phase the sequencer recorded,
   2157 		 * or the current phase presented on the bus.
   2158 		 */
   2159 		u_int mesg_out;
   2160 		u_int curphase;
   2161 		u_int errorphase;
   2162 		u_int lastphase;
   2163 		int   i;
   2164 
   2165 		lastphase = ahc_inb(ahc, LASTPHASE);
   2166 		curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
   2167 		ahc_outb(ahc, CLRSINT1, CLRSCSIPERR);
   2168 		/*
   2169 		 * For all phases save DATA, the sequencer won't
   2170 		 * automatically ack a byte that has a parity error
   2171 		 * in it.  So the only way that the current phase
   2172 		 * could be 'data-in' is if the parity error is for
   2173 		 * an already acked byte in the data phase.  During
   2174 		 * synchronous data-in transfers, we may actually
   2175 		 * ack bytes before latching the current phase in
   2176 		 * LASTPHASE, leading to the discrepancy between
   2177 		 * curphase and lastphase.
   2178 		 */
   2179 		if ((ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0
   2180 		 || curphase == P_DATAIN)
   2181 			errorphase = curphase;
   2182 		else
   2183 			errorphase = lastphase;
   2184 
   2185 		for (i = 0; i < num_phases; i++) {
   2186 			if (errorphase == phase_table[i].phase)
   2187 				break;
   2188 		}
   2189 		mesg_out = phase_table[i].mesg_out;
   2190 		if (scb != NULL)
   2191 			scsi_print_addr(scb->xs->sc_link);
   2192 		else
   2193 			printf("%s:%c:%d: ", ahc_name(ahc),
   2194 			       intr_channel,
   2195 			       TCL_TARGET(ahc_inb(ahc, SAVED_TCL)));
   2196 
   2197 		printf("parity error detected %s. "
   2198 		       "SEQADDR(0x%x) SCSIRATE(0x%x)\n",
   2199 		       phase_table[i].phasemsg,
   2200 		       ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8),
   2201 		       ahc_inb(ahc, SCSIRATE));
   2202 
   2203 		/*
   2204 		 * We've set the hardware to assert ATN if we
   2205 		 * get a parity error on "in" phases, so all we
   2206 		 * need to do is stuff the message buffer with
   2207 		 * the appropriate message.  "In" phases have set
   2208 		 * mesg_out to something other than MSG_NOP.
   2209 		 */
   2210 		if (mesg_out != MSG_NOOP) {
   2211 			if (ahc->msg_type != MSG_TYPE_NONE)
   2212 				ahc->send_msg_perror = TRUE;
   2213 			else
   2214 				ahc_outb(ahc, MSG_OUT, mesg_out);
   2215 		}
   2216 		ahc_outb(ahc, CLRINT, CLRSCSIINT);
   2217 		unpause_sequencer(ahc);
   2218 	} else if ((status & BUSFREE) != 0
   2219 		&& (ahc_inb(ahc, SIMODE1) & ENBUSFREE) != 0) {
   2220 		/*
   2221 		 * First look at what phase we were last in.
   2222 		 * If its message out, chances are pretty good
   2223 		 * that the busfree was in response to one of
   2224 		 * our abort requests.
   2225 		 */
   2226 		u_int lastphase = ahc_inb(ahc, LASTPHASE);
   2227 		u_int saved_tcl = ahc_inb(ahc, SAVED_TCL);
   2228 		u_int target = TCL_TARGET(saved_tcl);
   2229 		u_int initiator_role_id = TCL_SCSI_ID(ahc, saved_tcl);
   2230 		char channel = TCL_CHANNEL(ahc, saved_tcl);
   2231 		int printerror = 1;
   2232 
   2233 		ahc_outb(ahc, SCSISEQ,
   2234 			 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
   2235 		if (lastphase == P_MESGOUT) {
   2236 			u_int message;
   2237 			u_int tag;
   2238 
   2239 			message = ahc->msgout_buf[ahc->msgout_index - 1];
   2240 			tag = SCB_LIST_NULL;
   2241 			switch (message) {
   2242 			case MSG_ABORT_TAG:
   2243 				tag = scb->hscb->tag;
   2244 				/* FALLTRHOUGH */
   2245 			case MSG_ABORT:
   2246 				scsi_print_addr(scb->xs->sc_link);
   2247 				printf("SCB %x - Abort %s Completed.\n",
   2248 				       scb->hscb->tag, tag == SCB_LIST_NULL ?
   2249 				       "" : "Tag");
   2250 				ahc_abort_scbs(ahc, target, channel,
   2251 					       TCL_LUN(saved_tcl), tag,
   2252 					       ROLE_INITIATOR,
   2253 					       XS_DRIVER_STUFFUP);
   2254 				printerror = 0;
   2255 				break;
   2256 			case MSG_BUS_DEV_RESET:
   2257 			{
   2258 				struct ahc_devinfo devinfo;
   2259 
   2260 				if (scb != NULL &&
   2261 				    (scb->xs->xs_control & XS_CTL_RESET)
   2262 				 && ahc_match_scb(scb, target, channel,
   2263 						  TCL_LUN(saved_tcl),
   2264 						  SCB_LIST_NULL,
   2265 						  ROLE_INITIATOR)) {
   2266 					ahcsetccbstatus(scb->xs, XS_NOERROR);
   2267 				}
   2268 				ahc_compile_devinfo(&devinfo,
   2269 						    initiator_role_id,
   2270 						    target,
   2271 						    TCL_LUN(saved_tcl),
   2272 						    channel,
   2273 						    ROLE_INITIATOR);
   2274 				ahc_handle_devreset(ahc, &devinfo,
   2275 						    XS_RESET,
   2276 						    "Bus Device Reset",
   2277 						    /*verbose_level*/0);
   2278 				printerror = 0;
   2279 				break;
   2280 			}
   2281 			default:
   2282 				break;
   2283 			}
   2284 		}
   2285 		if (printerror != 0) {
   2286 			int i;
   2287 
   2288 			if (scb != NULL) {
   2289 				u_int tag;
   2290 
   2291 				if ((scb->hscb->control & TAG_ENB) != 0)
   2292 					tag = scb->hscb->tag;
   2293 				else
   2294 					tag = SCB_LIST_NULL;
   2295 				ahc_abort_scbs(ahc, target, channel,
   2296 					       SCB_LUN(scb), tag,
   2297 					       ROLE_INITIATOR,
   2298 					       XS_DRIVER_STUFFUP);
   2299 				scsi_print_addr(scb->xs->sc_link);
   2300 			} else {
   2301 				/*
   2302 				 * We had not fully identified this connection,
   2303 				 * so we cannot abort anything.
   2304 				 */
   2305 				printf("%s: ", ahc_name(ahc));
   2306 			}
   2307 			for (i = 0; i < num_phases; i++) {
   2308 				if (lastphase == phase_table[i].phase)
   2309 					break;
   2310 			}
   2311 			printf("Unexpected busfree %s\n"
   2312 			       "SEQADDR == 0x%x\n",
   2313 			       phase_table[i].phasemsg, ahc_inb(ahc, SEQADDR0)
   2314 				| (ahc_inb(ahc, SEQADDR1) << 8));
   2315 		}
   2316 		ahc_clear_msg_state(ahc);
   2317 		ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
   2318 		ahc_outb(ahc, CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
   2319 		ahc_outb(ahc, CLRINT, CLRSCSIINT);
   2320 		restart_sequencer(ahc);
   2321 	} else if ((status & SELTO) != 0) {
   2322 		u_int scbptr;
   2323 
   2324 		scbptr = ahc_inb(ahc, WAITING_SCBH);
   2325 		ahc_outb(ahc, SCBPTR, scbptr);
   2326 		scb_index = ahc_inb(ahc, SCB_TAG);
   2327 
   2328 		if (scb_index < ahc->scb_data->numscbs) {
   2329 			scb = &ahc->scb_data->scbarray[scb_index];
   2330 			if ((scb->flags & SCB_ACTIVE) == 0)
   2331 				scb = NULL;
   2332 		} else
   2333 			scb = NULL;
   2334 
   2335 		if (scb == NULL) {
   2336 			printf("%s: ahc_intr - referenced scb not "
   2337 			       "valid during SELTO scb(%d, %d)\n",
   2338 			       ahc_name(ahc), scbptr, scb_index);
   2339 		} else {
   2340 			u_int tag;
   2341 
   2342 			tag = SCB_LIST_NULL;
   2343 			if ((scb->hscb->control & MSG_SIMPLE_Q_TAG) != 0)
   2344 				tag = scb->hscb->tag;
   2345 
   2346 			ahc_abort_scbs(ahc, SCB_TARGET(scb), SCB_CHANNEL(scb),
   2347 				       SCB_LUN(scb), tag,
   2348 				       ROLE_INITIATOR, XS_SELTIMEOUT);
   2349 		}
   2350 		/* Stop the selection */
   2351 		ahc_outb(ahc, SCSISEQ, 0);
   2352 
   2353 		/* No more pending messages */
   2354 		ahc_clear_msg_state(ahc);
   2355 
   2356 		/*
   2357 		 * Although the driver does not care about the
   2358 		 * 'Selection in Progress' status bit, the busy
   2359 		 * LED does.  SELINGO is only cleared by a sucessful
   2360 		 * selection, so we must manually clear it to ensure
   2361 		 * the LED turns off just incase no future successful
   2362 		 * selections occur (e.g. no devices on the bus).
   2363 		 */
   2364 		ahc_outb(ahc, CLRSINT0, CLRSELINGO);
   2365 
   2366 		/* Clear interrupt state */
   2367 		ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
   2368 		ahc_outb(ahc, CLRINT, CLRSCSIINT);
   2369 		restart_sequencer(ahc);
   2370 	} else {
   2371 		scsi_print_addr(scb->xs->sc_link);
   2372 		printf("Unknown SCSIINT. Status = 0x%x\n", status);
   2373 		ahc_outb(ahc, CLRSINT1, status);
   2374 		ahc_outb(ahc, CLRINT, CLRSCSIINT);
   2375 		unpause_sequencer(ahc);
   2376 	}
   2377 }
   2378 
   2379 static void
   2380 ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
   2381 {
   2382 	/*
   2383 	 * We need to initiate transfer negotiations.
   2384 	 * If our current and goal settings are identical,
   2385 	 * we want to renegotiate due to a check condition.
   2386 	 */
   2387 	struct	ahc_initiator_tinfo *tinfo;
   2388 	struct	tmode_tstate *tstate;
   2389 	int	dowide;
   2390 	int	dosync;
   2391 
   2392 	tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
   2393 				    devinfo->target, &tstate);
   2394 	dowide = tinfo->current.width != tinfo->goal.width;
   2395 	dosync = tinfo->current.period != tinfo->goal.period;
   2396 
   2397 	if (!dowide && !dosync) {
   2398 		dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
   2399 		dosync = tinfo->goal.period != 0;
   2400 	}
   2401 
   2402 	if (dowide) {
   2403 		ahc_construct_wdtr(ahc, tinfo->goal.width);
   2404 	} else if (dosync) {
   2405 		struct	ahc_syncrate *rate;
   2406 		u_int	period;
   2407 		u_int	offset;
   2408 
   2409 		period = tinfo->goal.period;
   2410 		rate = ahc_devlimited_syncrate(ahc, &period);
   2411 		offset = tinfo->goal.offset;
   2412 		ahc_validate_offset(ahc, rate, &offset,
   2413 				    tinfo->current.width);
   2414 		ahc_construct_sdtr(ahc, period, offset);
   2415 	} else {
   2416 		panic("ahc_intr: AWAITING_MSG for negotiation, "
   2417 		      "but no negotiation needed\n");
   2418 	}
   2419 }
   2420 
   2421 static void
   2422 ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
   2423 			   struct scb *scb)
   2424 {
   2425 	/*
   2426 	 * To facilitate adding multiple messages together,
   2427 	 * each routine should increment the index and len
   2428 	 * variables instead of setting them explicitly.
   2429 	 */
   2430 	ahc->msgout_index = 0;
   2431 	ahc->msgout_len = 0;
   2432 
   2433 	if ((scb->flags & SCB_DEVICE_RESET) == 0
   2434 	 && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
   2435 		u_int identify_msg;
   2436 
   2437 		identify_msg = MSG_IDENTIFYFLAG | SCB_LUN(scb);
   2438 		if ((scb->hscb->control & DISCENB) != 0)
   2439 			identify_msg |= MSG_IDENTIFY_DISCFLAG;
   2440 		ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
   2441 		ahc->msgout_len++;
   2442 
   2443 		if ((scb->hscb->control & TAG_ENB) != 0) {
   2444 			/* XXX fvdl FreeBSD has tag action passed down */
   2445 			ahc->msgout_buf[ahc->msgout_index++] = MSG_SIMPLE_Q_TAG;
   2446 			ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
   2447 			ahc->msgout_len += 2;
   2448 		}
   2449 	}
   2450 
   2451 	if (scb->flags & SCB_DEVICE_RESET) {
   2452 		ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
   2453 		ahc->msgout_len++;
   2454 		scsi_print_addr(scb->xs->sc_link);
   2455 		printf("Bus Device Reset Message Sent\n");
   2456 	} else if (scb->flags & SCB_ABORT) {
   2457 		if ((scb->hscb->control & TAG_ENB) != 0)
   2458 			ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
   2459 		else
   2460 			ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
   2461 		ahc->msgout_len++;
   2462 		scsi_print_addr(scb->xs->sc_link);
   2463 		printf("Abort Message Sent\n");
   2464 	} else if ((ahc->targ_msg_req & devinfo->target_mask) != 0) {
   2465 		ahc_build_transfer_msg(ahc, devinfo);
   2466 	} else {
   2467 		printf("ahc_intr: AWAITING_MSG for an SCB that "
   2468 		       "does not have a waiting message");
   2469 		panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
   2470 		      "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
   2471 		      ahc_inb(ahc, MSG_OUT), scb->flags);
   2472 	}
   2473 
   2474 	/*
   2475 	 * Clear the MK_MESSAGE flag from the SCB so we aren't
   2476 	 * asked to send this message again.
   2477 	 */
   2478 	ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
   2479 	ahc->msgout_index = 0;
   2480 	ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
   2481 }
   2482 
   2483 static void
   2484 ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
   2485 {
   2486 	/*
   2487 	 * To facilitate adding multiple messages together,
   2488 	 * each routine should increment the index and len
   2489 	 * variables instead of setting them explicitly.
   2490 	 */
   2491 	ahc->msgout_index = 0;
   2492 	ahc->msgout_len = 0;
   2493 
   2494 	if ((ahc->targ_msg_req & devinfo->target_mask) != 0)
   2495 		ahc_build_transfer_msg(ahc, devinfo);
   2496 	else
   2497 		panic("ahc_intr: AWAITING target message with no message");
   2498 
   2499 	ahc->msgout_index = 0;
   2500 	ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
   2501 }
   2502 
   2503 static int
   2504 ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
   2505 {
   2506 	/*
   2507 	 * What we care about here is if we had an
   2508 	 * outstanding SDTR or WDTR message for this
   2509 	 * target.  If we did, this is a signal that
   2510 	 * the target is refusing negotiation.
   2511 	 */
   2512 	struct scb *scb;
   2513 	u_int scb_index;
   2514 	u_int last_msg;
   2515 	int   response = 0;
   2516 
   2517 	scb_index = ahc_inb(ahc, SCB_TAG);
   2518 	scb = &ahc->scb_data->scbarray[scb_index];
   2519 
   2520 	/* Might be necessary */
   2521 	last_msg = ahc_inb(ahc, LAST_MSG);
   2522 
   2523 	if (ahc_sent_msg(ahc, MSG_EXT_WDTR, /*full*/FALSE)) {
   2524 		struct ahc_initiator_tinfo *tinfo;
   2525 		struct tmode_tstate *tstate;
   2526 
   2527 #ifdef AHC_DEBUG_NEG
   2528 		/* note 8bit xfers */
   2529 		printf("%s:%c:%d: refuses WIDE negotiation.  Using "
   2530 		       "8bit transfers\n", ahc_name(ahc),
   2531 		       devinfo->channel, devinfo->target);
   2532 #endif
   2533 		ahc_set_width(ahc, devinfo,
   2534 			      MSG_EXT_WDTR_BUS_8_BIT,
   2535 			      AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
   2536 			      /*paused*/TRUE, /*done*/TRUE);
   2537 		/*
   2538 		 * No need to clear the sync rate.  If the target
   2539 		 * did not accept the command, our syncrate is
   2540 		 * unaffected.  If the target started the negotiation,
   2541 		 * but rejected our response, we already cleared the
   2542 		 * sync rate before sending our WDTR.
   2543 		 */
   2544 		tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
   2545 					    devinfo->our_scsiid,
   2546 					    devinfo->target, &tstate);
   2547 		if (tinfo->goal.period) {
   2548 			u_int period;
   2549 
   2550 			/* Start the sync negotiation */
   2551 			period = tinfo->goal.period;
   2552 			ahc_devlimited_syncrate(ahc, &period);
   2553 			ahc->msgout_index = 0;
   2554 			ahc->msgout_len = 0;
   2555 			ahc_construct_sdtr(ahc, period, tinfo->goal.offset);
   2556 			ahc->msgout_index = 0;
   2557 			response = 1;
   2558 		}
   2559 	} else if (ahc_sent_msg(ahc, MSG_EXT_SDTR, /*full*/FALSE)) {
   2560 		/* note asynch xfers and clear flag */
   2561 		ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
   2562 				 /*offset*/0, AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
   2563 				 /*paused*/TRUE, /*done*/TRUE);
   2564 #ifdef AHC_DEBUG_NEG
   2565 		printf("%s:%c:%d: refuses synchronous negotiation. "
   2566 		       "Using asynchronous transfers\n",
   2567 		       ahc_name(ahc),
   2568 		       devinfo->channel, devinfo->target);
   2569 #endif
   2570 	} else if ((scb->hscb->control & MSG_SIMPLE_Q_TAG) != 0) {
   2571 		printf("%s:%c:%d: refuses tagged commands.  Performing "
   2572 		       "non-tagged I/O\n", ahc_name(ahc),
   2573 		       devinfo->channel, devinfo->target);
   2574 
   2575 		ahc_set_tags(ahc, devinfo, FALSE);
   2576 
   2577 		/*
   2578 		 * Resend the identify for this CCB as the target
   2579 		 * may believe that the selection is invalid otherwise.
   2580 		 */
   2581 		ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL)
   2582 					  & ~MSG_SIMPLE_Q_TAG);
   2583 	 	scb->hscb->control &= ~MSG_SIMPLE_Q_TAG;
   2584 		ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
   2585 		ahc_outb(ahc, SCSISIGO, ahc_inb(ahc, SCSISIGO) | ATNO);
   2586 
   2587 		/*
   2588 		 * Requeue all tagged commands for this target
   2589 		 * currently in our posession so they can be
   2590 		 * converted to untagged commands.
   2591 		 */
   2592 		ahc_search_qinfifo(ahc, SCB_TARGET(scb), SCB_CHANNEL(scb),
   2593 				   SCB_LUN(scb), /*tag*/SCB_LIST_NULL,
   2594 				   ROLE_INITIATOR, SCB_REQUEUE,
   2595 				   SEARCH_COMPLETE);
   2596 	} else {
   2597 		/*
   2598 		 * Otherwise, we ignore it.
   2599 		 */
   2600 		printf("%s:%c:%d: Message reject for %x -- ignored\n",
   2601 		       ahc_name(ahc), devinfo->channel, devinfo->target,
   2602 		       last_msg);
   2603 	}
   2604 	return (response);
   2605 }
   2606 
   2607 static void
   2608 ahc_clear_msg_state(struct ahc_softc *ahc)
   2609 {
   2610 	ahc->msgout_len = 0;
   2611 	ahc->msgin_index = 0;
   2612 	ahc->msg_type = MSG_TYPE_NONE;
   2613 	ahc_outb(ahc, MSG_OUT, MSG_NOOP);
   2614 }
   2615 
   2616 static void
   2617 ahc_handle_message_phase(struct ahc_softc *ahc, struct scsipi_link *sc_link)
   2618 {
   2619 	struct	ahc_devinfo devinfo;
   2620 	u_int	bus_phase;
   2621 	int	end_session;
   2622 
   2623 	ahc_fetch_devinfo(ahc, &devinfo);
   2624 	end_session = FALSE;
   2625 	bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
   2626 
   2627 reswitch:
   2628 	switch (ahc->msg_type) {
   2629 	case MSG_TYPE_INITIATOR_MSGOUT:
   2630 	{
   2631 		int lastbyte;
   2632 		int phasemis;
   2633 		int msgdone;
   2634 
   2635 		if (ahc->msgout_len == 0)
   2636 			panic("REQINIT interrupt with no active message");
   2637 
   2638 		phasemis = bus_phase != P_MESGOUT;
   2639 		if (phasemis) {
   2640 			if (bus_phase == P_MESGIN) {
   2641 				/*
   2642 				 * Change gears and see if
   2643 				 * this messages is of interest to
   2644 				 * us or should be passed back to
   2645 				 * the sequencer.
   2646 				 */
   2647 				ahc_outb(ahc, CLRSINT1, CLRATNO);
   2648 				ahc->send_msg_perror = FALSE;
   2649 				ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
   2650 				ahc->msgin_index = 0;
   2651 				goto reswitch;
   2652 			}
   2653 			end_session = TRUE;
   2654 			break;
   2655 		}
   2656 
   2657 		if (ahc->send_msg_perror) {
   2658 			ahc_outb(ahc, CLRSINT1, CLRATNO);
   2659 			ahc_outb(ahc, CLRSINT1, CLRREQINIT);
   2660 			ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
   2661 			break;
   2662 		}
   2663 
   2664 		msgdone	= ahc->msgout_index == ahc->msgout_len;
   2665 		if (msgdone) {
   2666 			/*
   2667 			 * The target has requested a retry.
   2668 			 * Re-assert ATN, reset our message index to
   2669 			 * 0, and try again.
   2670 			 */
   2671 			ahc->msgout_index = 0;
   2672 			ahc_outb(ahc, SCSISIGO, ahc_inb(ahc, SCSISIGO) | ATNO);
   2673 		}
   2674 
   2675 		lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
   2676 		if (lastbyte) {
   2677 			/* Last byte is signified by dropping ATN */
   2678 			ahc_outb(ahc, CLRSINT1, CLRATNO);
   2679 		}
   2680 
   2681 		/*
   2682 		 * Clear our interrupt status and present
   2683 		 * the next byte on the bus.
   2684 		 */
   2685 		ahc_outb(ahc, CLRSINT1, CLRREQINIT);
   2686 		ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
   2687 		break;
   2688 	}
   2689 	case MSG_TYPE_INITIATOR_MSGIN:
   2690 	{
   2691 		int phasemis;
   2692 		int message_done;
   2693 
   2694 		phasemis = bus_phase != P_MESGIN;
   2695 
   2696 		if (phasemis) {
   2697 			ahc->msgin_index = 0;
   2698 			if (bus_phase == P_MESGOUT
   2699 			 && (ahc->send_msg_perror == TRUE
   2700 			  || (ahc->msgout_len != 0
   2701 			   && ahc->msgout_index == 0))) {
   2702 				ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
   2703 				goto reswitch;
   2704 			}
   2705 			end_session = TRUE;
   2706 			break;
   2707 		}
   2708 
   2709 		/* Pull the byte in without acking it */
   2710 		ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
   2711 
   2712 		message_done = ahc_parse_msg(ahc, sc_link, &devinfo);
   2713 
   2714 		if (message_done) {
   2715 			/*
   2716 			 * Clear our incoming message buffer in case there
   2717 			 * is another message following this one.
   2718 			 */
   2719 			ahc->msgin_index = 0;
   2720 
   2721 			/*
   2722 			 * If this message illicited a response,
   2723 			 * assert ATN so the target takes us to the
   2724 			 * message out phase.
   2725 			 */
   2726 			if (ahc->msgout_len != 0)
   2727 				ahc_outb(ahc, SCSISIGO,
   2728 					 ahc_inb(ahc, SCSISIGO) | ATNO);
   2729 		} else
   2730 			ahc->msgin_index++;
   2731 
   2732 		/* Ack the byte */
   2733 		ahc_outb(ahc, CLRSINT1, CLRREQINIT);
   2734 		ahc_inb(ahc, SCSIDATL);
   2735 		break;
   2736 	}
   2737 	case MSG_TYPE_TARGET_MSGIN:
   2738 	{
   2739 		int msgdone;
   2740 		int msgout_request;
   2741 
   2742 		if (ahc->msgout_len == 0)
   2743 			panic("Target MSGIN with no active message");
   2744 
   2745 		/*
   2746 		 * If we interrupted a mesgout session, the initiator
   2747 		 * will not know this until our first REQ.  So, we
   2748 		 * only honor mesgout requests after we've sent our
   2749 		 * first byte.
   2750 		 */
   2751 		if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
   2752 		 && ahc->msgout_index > 0)
   2753 			msgout_request = TRUE;
   2754 		else
   2755 			msgout_request = FALSE;
   2756 
   2757 		if (msgout_request) {
   2758 
   2759 			/*
   2760 			 * Change gears and see if
   2761 			 * this messages is of interest to
   2762 			 * us or should be passed back to
   2763 			 * the sequencer.
   2764 			 */
   2765 			ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
   2766 			ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
   2767 			ahc->msgin_index = 0;
   2768 			/* Dummy read to REQ for first byte */
   2769 			ahc_inb(ahc, SCSIDATL);
   2770 			ahc_outb(ahc, SXFRCTL0,
   2771 				 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
   2772 			break;
   2773 		}
   2774 
   2775 		msgdone = ahc->msgout_index == ahc->msgout_len;
   2776 		if (msgdone) {
   2777 			ahc_outb(ahc, SXFRCTL0,
   2778 				 ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
   2779 			end_session = TRUE;
   2780 			break;
   2781 		}
   2782 
   2783 		/*
   2784 		 * Present the next byte on the bus.
   2785 		 */
   2786 		ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
   2787 		ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
   2788 		break;
   2789 	}
   2790 	case MSG_TYPE_TARGET_MSGOUT:
   2791 	{
   2792 		int lastbyte;
   2793 		int msgdone;
   2794 
   2795 		/*
   2796 		 * The initiator signals that this is
   2797 		 * the last byte by dropping ATN.
   2798 		 */
   2799 		lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
   2800 
   2801 		/*
   2802 		 * Read the latched byte, but turn off SPIOEN first
   2803 		 * so that we don't inadvertantly cause a REQ for the
   2804 		 * next byte.
   2805 		 */
   2806 		ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
   2807 		ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
   2808 		msgdone = ahc_parse_msg(ahc, sc_link, &devinfo);
   2809 		if (msgdone == MSGLOOP_TERMINATED) {
   2810 			/*
   2811 			 * The message is *really* done in that it caused
   2812 			 * us to go to bus free.  The sequencer has already
   2813 			 * been reset at this point, so pull the ejection
   2814 			 * handle.
   2815 			 */
   2816 			return;
   2817 		}
   2818 
   2819 		ahc->msgin_index++;
   2820 
   2821 		/*
   2822 		 * XXX Read spec about initiator dropping ATN too soon
   2823 		 *     and use msgdone to detect it.
   2824 		 */
   2825 		if (msgdone == MSGLOOP_MSGCOMPLETE) {
   2826 			ahc->msgin_index = 0;
   2827 
   2828 			/*
   2829 			 * If this message illicited a response, transition
   2830 			 * to the Message in phase and send it.
   2831 			 */
   2832 			if (ahc->msgout_len != 0) {
   2833 				ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
   2834 				ahc_outb(ahc, SXFRCTL0,
   2835 					 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
   2836 				ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
   2837 				ahc->msgin_index = 0;
   2838 				break;
   2839 			}
   2840 		}
   2841 
   2842 		if (lastbyte)
   2843 			end_session = TRUE;
   2844 		else {
   2845 			/* Ask for the next byte. */
   2846 			ahc_outb(ahc, SXFRCTL0,
   2847 				 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
   2848 		}
   2849 
   2850 		break;
   2851 	}
   2852 	default:
   2853 		panic("Unknown REQINIT message type");
   2854 	}
   2855 
   2856 	if (end_session) {
   2857 		ahc_clear_msg_state(ahc);
   2858 		ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
   2859 	} else
   2860 		ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
   2861 }
   2862 
   2863 /*
   2864  * See if we sent a particular extended message to the target.
   2865  * If "full" is true, the target saw the full message.
   2866  * If "full" is false, the target saw at least the first
   2867  * byte of the message.
   2868  */
   2869 static int
   2870 ahc_sent_msg(struct ahc_softc *ahc, u_int msgtype, int full)
   2871 {
   2872 	int found;
   2873 	int index;
   2874 
   2875 	found = FALSE;
   2876 	index = 0;
   2877 
   2878 	while (index < ahc->msgout_len) {
   2879 		if (ahc->msgout_buf[index] == MSG_EXTENDED) {
   2880 
   2881 			/* Found a candidate */
   2882 			if (ahc->msgout_buf[index+2] == msgtype) {
   2883 				u_int end_index;
   2884 
   2885 				end_index = index + 1
   2886 					  + ahc->msgout_buf[index + 1];
   2887 				if (full) {
   2888 					if (ahc->msgout_index > end_index)
   2889 						found = TRUE;
   2890 				} else if (ahc->msgout_index > index)
   2891 					found = TRUE;
   2892 			}
   2893 			break;
   2894 		} else if (ahc->msgout_buf[index] >= MSG_SIMPLE_Q_TAG
   2895 			&& ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
   2896 
   2897 			/* Skip tag type and tag id or residue param*/
   2898 			index += 2;
   2899 		} else {
   2900 			/* Single byte message */
   2901 			index++;
   2902 		}
   2903 	}
   2904 	return (found);
   2905 }
   2906 
   2907 static int
   2908 ahc_parse_msg(struct ahc_softc *ahc, struct scsipi_link *sc_link,
   2909 	      struct ahc_devinfo *devinfo)
   2910 {
   2911 	struct	ahc_initiator_tinfo *tinfo;
   2912 	struct	tmode_tstate *tstate;
   2913 	int	reject;
   2914 	int	done;
   2915 	int	response;
   2916 	u_int	targ_scsirate;
   2917 
   2918 	done = MSGLOOP_IN_PROG;
   2919 	response = FALSE;
   2920 	reject = FALSE;
   2921 	tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
   2922 				    devinfo->target, &tstate);
   2923 	targ_scsirate = tinfo->scsirate;
   2924 
   2925 	/*
   2926 	 * Parse as much of the message as is availible,
   2927 	 * rejecting it if we don't support it.  When
   2928 	 * the entire message is availible and has been
   2929 	 * handled, return MSGLOOP_MSGCOMPLETE, indicating
   2930 	 * that we have parsed an entire message.
   2931 	 *
   2932 	 * In the case of extended messages, we accept the length
   2933 	 * byte outright and perform more checking once we know the
   2934 	 * extended message type.
   2935 	 */
   2936 	switch (ahc->msgin_buf[0]) {
   2937 	case MSG_MESSAGE_REJECT:
   2938 		response = ahc_handle_msg_reject(ahc, devinfo);
   2939 		/* FALLTHROUGH */
   2940 	case MSG_NOOP:
   2941 		done = MSGLOOP_MSGCOMPLETE;
   2942 		break;
   2943 	case MSG_IGN_WIDE_RESIDUE:
   2944 	{
   2945 		/* Wait for the whole message */
   2946 		if (ahc->msgin_index >= 1) {
   2947 			if (ahc->msgin_buf[1] != 1
   2948 			 || tinfo->current.width == MSG_EXT_WDTR_BUS_8_BIT) {
   2949 				reject = TRUE;
   2950 				done = MSGLOOP_MSGCOMPLETE;
   2951 			} else
   2952 				ahc_handle_ign_wide_residue(ahc, devinfo);
   2953 		}
   2954 		break;
   2955 	}
   2956 	case MSG_EXTENDED:
   2957 	{
   2958 		/* Wait for enough of the message to begin validation */
   2959 		if (ahc->msgin_index < 2)
   2960 			break;
   2961 		switch (ahc->msgin_buf[2]) {
   2962 		case MSG_EXT_SDTR:
   2963 		{
   2964 			struct	 ahc_syncrate *syncrate;
   2965 			u_int	 period;
   2966 			u_int	 offset;
   2967 			u_int	 saved_offset;
   2968 
   2969 			if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
   2970 				reject = TRUE;
   2971 				break;
   2972 			}
   2973 
   2974 			/*
   2975 			 * Wait until we have both args before validating
   2976 			 * and acting on this message.
   2977 			 *
   2978 			 * Add one to MSG_EXT_SDTR_LEN to account for
   2979 			 * the extended message preamble.
   2980 			 */
   2981 			if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
   2982 				break;
   2983 
   2984 			period = ahc->msgin_buf[3];
   2985 			saved_offset = offset = ahc->msgin_buf[4];
   2986 			syncrate = ahc_devlimited_syncrate(ahc, &period);
   2987 			ahc_validate_offset(ahc, syncrate, &offset,
   2988 					    targ_scsirate & WIDEXFER);
   2989 			ahc_set_syncrate(ahc, devinfo,
   2990 					 syncrate, period, offset,
   2991 					 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
   2992 					 /*paused*/TRUE, /*done*/TRUE);
   2993 
   2994 			/*
   2995 			 * See if we initiated Sync Negotiation
   2996 			 * and didn't have to fall down to async
   2997 			 * transfers.
   2998 			 */
   2999 			if (ahc_sent_msg(ahc, MSG_EXT_SDTR, /*full*/TRUE)) {
   3000 				/* We started it */
   3001 				if (saved_offset != offset) {
   3002 					/* Went too low - force async */
   3003 					reject = TRUE;
   3004 				}
   3005 			} else {
   3006 				/*
   3007 				 * Send our own SDTR in reply
   3008 				 */
   3009 				ahc->msgout_index = 0;
   3010 				ahc->msgout_len = 0;
   3011 				ahc_construct_sdtr(ahc, period, offset);
   3012 				ahc->msgout_index = 0;
   3013 				response = TRUE;
   3014 			}
   3015 			done = MSGLOOP_MSGCOMPLETE;
   3016 			break;
   3017 		}
   3018 		case MSG_EXT_WDTR:
   3019 		{
   3020 			u_int	bus_width;
   3021 			u_int	sending_reply;
   3022 
   3023 			sending_reply = FALSE;
   3024 			if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
   3025 				reject = TRUE;
   3026 				break;
   3027 			}
   3028 
   3029 			/*
   3030 			 * Wait until we have our arg before validating
   3031 			 * and acting on this message.
   3032 			 *
   3033 			 * Add one to MSG_EXT_WDTR_LEN to account for
   3034 			 * the extended message preamble.
   3035 			 */
   3036 			if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
   3037 				break;
   3038 
   3039 			bus_width = ahc->msgin_buf[3];
   3040 			if (ahc_sent_msg(ahc, MSG_EXT_WDTR, /*full*/TRUE)) {
   3041 				/*
   3042 				 * Don't send a WDTR back to the
   3043 				 * target, since we asked first.
   3044 				 */
   3045 				switch (bus_width){
   3046 				default:
   3047 					/*
   3048 					 * How can we do anything greater
   3049 					 * than 16bit transfers on a 16bit
   3050 					 * bus?
   3051 					 */
   3052 					reject = TRUE;
   3053 					printf("%s: target %d requested %dBit "
   3054 					       "transfers.  Rejecting...\n",
   3055 					       ahc_name(ahc), devinfo->target,
   3056 					       8 * (0x01 << bus_width));
   3057 					/* FALLTHROUGH */
   3058 				case MSG_EXT_WDTR_BUS_8_BIT:
   3059 					bus_width = MSG_EXT_WDTR_BUS_8_BIT;
   3060 					break;
   3061 				case MSG_EXT_WDTR_BUS_16_BIT:
   3062 					break;
   3063 				}
   3064 			} else {
   3065 				/*
   3066 				 * Send our own WDTR in reply
   3067 				 */
   3068 				switch (bus_width) {
   3069 				default:
   3070 					if (ahc->features & AHC_WIDE) {
   3071 						/* Respond Wide */
   3072 						bus_width =
   3073 						    MSG_EXT_WDTR_BUS_16_BIT;
   3074 						break;
   3075 					}
   3076 					/* FALLTHROUGH */
   3077 				case MSG_EXT_WDTR_BUS_8_BIT:
   3078 					bus_width = MSG_EXT_WDTR_BUS_8_BIT;
   3079 					break;
   3080 				}
   3081 				ahc->msgout_index = 0;
   3082 				ahc->msgout_len = 0;
   3083 				ahc_construct_wdtr(ahc, bus_width);
   3084 				ahc->msgout_index = 0;
   3085 				response = TRUE;
   3086 				sending_reply = TRUE;
   3087 			}
   3088 			ahc_set_width(ahc, devinfo, bus_width,
   3089 				      AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
   3090 				      /*paused*/TRUE, /*done*/TRUE);
   3091 
   3092 			/* After a wide message, we are async */
   3093 			ahc_set_syncrate(ahc, devinfo,
   3094 					 /*syncrate*/NULL, /*period*/0,
   3095 					 /*offset*/0, AHC_TRANS_ACTIVE,
   3096 					 /*paused*/TRUE, /*done*/FALSE);
   3097 			if (sending_reply == FALSE && reject == FALSE) {
   3098 
   3099 				if (tinfo->goal.period) {
   3100 					struct	ahc_syncrate *rate;
   3101 					u_int	period;
   3102 					u_int	offset;
   3103 
   3104 					/* Start the sync negotiation */
   3105 					period = tinfo->goal.period;
   3106 					rate = ahc_devlimited_syncrate(ahc,
   3107 								       &period);
   3108 					offset = tinfo->goal.offset;
   3109 					ahc_validate_offset(ahc, rate, &offset,
   3110 							  tinfo->current.width);
   3111 					ahc->msgout_index = 0;
   3112 					ahc->msgout_len = 0;
   3113 					ahc_construct_sdtr(ahc, period, offset);
   3114 					ahc->msgout_index = 0;
   3115 					response = TRUE;
   3116 				}
   3117 			}
   3118 			done = MSGLOOP_MSGCOMPLETE;
   3119 			break;
   3120 		}
   3121 		default:
   3122 			/* Unknown extended message.  Reject it. */
   3123 			reject = TRUE;
   3124 			break;
   3125 		}
   3126 		break;
   3127 	}
   3128 	case MSG_BUS_DEV_RESET:
   3129 		ahc_handle_devreset(ahc, devinfo,
   3130 				    XS_RESET, "Bus Device Reset Received",
   3131 				    /*verbose_level*/0);
   3132 		restart_sequencer(ahc);
   3133 		done = MSGLOOP_TERMINATED;
   3134 		break;
   3135 	case MSG_ABORT_TAG:
   3136 	case MSG_ABORT:
   3137 	case MSG_CLEAR_QUEUE:
   3138 		/* Target mode messages */
   3139 		if (devinfo->role != ROLE_TARGET) {
   3140 			reject = TRUE;
   3141 			break;
   3142 		}
   3143 #if AHC_TARGET_MODE
   3144 		ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
   3145 			       devinfo->lun,
   3146 			       ahc->msgin_buf[0] == MSG_ABORT_TAG
   3147 						  ? SCB_LIST_NULL
   3148 						  : ahc_inb(ahc, INITIATOR_TAG),
   3149 			       ROLE_TARGET, XS_DRIVER_STUFFUP);
   3150 
   3151 		tstate = ahc->enabled_targets[devinfo->our_scsiid];
   3152 		if (tstate != NULL) {
   3153 			struct tmode_lstate* lstate;
   3154 
   3155 			lstate = tstate->enabled_luns[devinfo->lun];
   3156 			if (lstate != NULL) {
   3157 				ahc_queue_lstate_event(ahc, lstate,
   3158 						       devinfo->our_scsiid,
   3159 						       ahc->msgin_buf[0],
   3160 						       /*arg*/0);
   3161 				ahc_send_lstate_events(ahc, lstate);
   3162 			}
   3163 		}
   3164 		done = MSGLOOP_MSGCOMPLETE;
   3165 #else
   3166 		panic("ahc: got target mode message");
   3167 #endif
   3168 		break;
   3169 	case MSG_TERM_IO_PROC:
   3170 	default:
   3171 		reject = TRUE;
   3172 		break;
   3173 	}
   3174 
   3175 	if (reject) {
   3176 		/*
   3177 		 * Setup to reject the message.
   3178 		 */
   3179 		ahc->msgout_index = 0;
   3180 		ahc->msgout_len = 1;
   3181 		ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
   3182 		done = MSGLOOP_MSGCOMPLETE;
   3183 		response = TRUE;
   3184 	}
   3185 
   3186 	if (done != MSGLOOP_IN_PROG && !response)
   3187 		/* Clear the outgoing message buffer */
   3188 		ahc->msgout_len = 0;
   3189 
   3190 	return (done);
   3191 }
   3192 
   3193 static void
   3194 ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
   3195 {
   3196 	u_int scb_index;
   3197 	struct scb *scb;
   3198 
   3199 	scb_index = ahc_inb(ahc, SCB_TAG);
   3200 	scb = &ahc->scb_data->scbarray[scb_index];
   3201 	if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
   3202 	 || !(scb->xs->xs_control & XS_CTL_DATA_IN)) {
   3203 		/*
   3204 		 * Ignore the message if we haven't
   3205 		 * seen an appropriate data phase yet.
   3206 		 */
   3207 	} else {
   3208 		/*
   3209 		 * If the residual occurred on the last
   3210 		 * transfer and the transfer request was
   3211 		 * expected to end on an odd count, do
   3212 		 * nothing.  Otherwise, subtract a byte
   3213 		 * and update the residual count accordingly.
   3214 		 */
   3215 		u_int resid_sgcnt;
   3216 
   3217 		resid_sgcnt = ahc_inb(ahc, SCB_RESID_SGCNT);
   3218 		if (resid_sgcnt == 0
   3219 		 && ahc_inb(ahc, DATA_COUNT_ODD) == 1) {
   3220 			/*
   3221 			 * If the residual occurred on the last
   3222 			 * transfer and the transfer request was
   3223 			 * expected to end on an odd count, do
   3224 			 * nothing.
   3225 			 */
   3226 		} else {
   3227 			u_int data_cnt;
   3228 			u_int32_t data_addr;
   3229 			u_int sg_index;
   3230 
   3231 			data_cnt = (ahc_inb(ahc, SCB_RESID_DCNT + 2) << 16)
   3232 				 | (ahc_inb(ahc, SCB_RESID_DCNT + 1) << 8)
   3233 				 | (ahc_inb(ahc, SCB_RESID_DCNT));
   3234 
   3235 			data_addr = (ahc_inb(ahc, SHADDR + 3) << 24)
   3236 				  | (ahc_inb(ahc, SHADDR + 2) << 16)
   3237 				  | (ahc_inb(ahc, SHADDR + 1) << 8)
   3238 				  | (ahc_inb(ahc, SHADDR));
   3239 
   3240 			data_cnt += 1;
   3241 			data_addr -= 1;
   3242 
   3243 			sg_index = scb->sg_count - resid_sgcnt;
   3244 
   3245 			if (sg_index != 0
   3246 			 && (le32toh(scb->sg_list[sg_index].len) < data_cnt)) {
   3247 				u_int32_t sg_addr;
   3248 
   3249 				sg_index--;
   3250 				data_cnt = 1;
   3251 				data_addr = le32toh(scb->sg_list[sg_index].addr)
   3252 					  + le32toh(scb->sg_list[sg_index].len)
   3253 					  - 1;
   3254 
   3255 				/*
   3256 				 * The physical address base points to the
   3257 				 * second entry as it is always used for
   3258 				 * calculating the "next S/G pointer".
   3259 				 */
   3260 				sg_addr = scb->sg_list_phys
   3261 					+ (sg_index* sizeof(*scb->sg_list));
   3262 				ahc_outb(ahc, SG_NEXT + 3, sg_addr >> 24);
   3263 				ahc_outb(ahc, SG_NEXT + 2, sg_addr >> 16);
   3264 				ahc_outb(ahc, SG_NEXT + 1, sg_addr >> 8);
   3265 				ahc_outb(ahc, SG_NEXT, sg_addr);
   3266 			}
   3267 
   3268 			ahc_outb(ahc, SCB_RESID_DCNT + 2, data_cnt >> 16);
   3269 			ahc_outb(ahc, SCB_RESID_DCNT + 1, data_cnt >> 8);
   3270 			ahc_outb(ahc, SCB_RESID_DCNT, data_cnt);
   3271 
   3272 			ahc_outb(ahc, SHADDR + 3, data_addr >> 24);
   3273 			ahc_outb(ahc, SHADDR + 2, data_addr >> 16);
   3274 			ahc_outb(ahc, SHADDR + 1, data_addr >> 8);
   3275 			ahc_outb(ahc, SHADDR, data_addr);
   3276 		}
   3277 	}
   3278 }
   3279 
   3280 static void
   3281 ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
   3282 		    int status, char *message,
   3283 		    int verbose_level)
   3284 {
   3285 	int found;
   3286 
   3287 	found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
   3288 			       AHC_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
   3289 			       status);
   3290 
   3291 	/*
   3292 	 * Go back to async/narrow transfers and renegotiate.
   3293 	 * ahc_set_width and ahc_set_syncrate can cope with NULL
   3294 	 * paths.
   3295 	 */
   3296 	ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
   3297 		      AHC_TRANS_CUR, /*paused*/TRUE, /*done*/FALSE);
   3298 	ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
   3299 			 /*period*/0, /*offset*/0, AHC_TRANS_CUR,
   3300 			 /*paused*/TRUE, /*done*/FALSE);
   3301 
   3302 	if (message != NULL && (verbose_level <= 0))
   3303 		printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc),
   3304 		       message, devinfo->channel, devinfo->target, found);
   3305 }
   3306 
   3307 /*
   3308  * We have an scb which has been processed by the
   3309  * adaptor, now we look to see how the operation
   3310  * went.
   3311  */
   3312 static void
   3313 ahc_done(struct ahc_softc *ahc, struct scb *scb)
   3314 {
   3315 	struct scsipi_xfer *xs;
   3316 	struct scsipi_link *sc_link;
   3317 	int requeue = 0;
   3318 	int target;
   3319 
   3320 
   3321 	xs = scb->xs;
   3322 	sc_link = xs->sc_link;
   3323 	LIST_REMOVE(scb, plinks);
   3324 
   3325 	callout_stop(&scb->xs->xs_callout);
   3326 
   3327 #ifdef AHC_DEBUG
   3328 	if (ahc_debug & AHC_SHOWCMDS) {
   3329 		scsi_print_addr(sc_link);
   3330 		printf("ahc_done opcode %d tag %x\n", xs->cmdstore.opcode,
   3331 		    scb->hscb->tag);
   3332 	}
   3333 #endif
   3334 
   3335 	target = sc_link->scsipi_scsi.target;
   3336 
   3337 	if (xs->datalen) {
   3338 		int op;
   3339 
   3340 		if (xs->xs_control & XS_CTL_DATA_IN)
   3341 			op = BUS_DMASYNC_POSTREAD;
   3342 		else
   3343 			op = BUS_DMASYNC_POSTWRITE;
   3344 		bus_dmamap_sync(ahc->parent_dmat, scb->dmamap, 0,
   3345 		    scb->dmamap->dm_mapsize, op);
   3346 		bus_dmamap_unload(ahc->parent_dmat, scb->dmamap);
   3347 	}
   3348 
   3349 	/*
   3350 	 * Unbusy this target/channel/lun.
   3351 	 * XXX if we are holding two commands per lun,
   3352 	 *     send the next command.
   3353 	 */
   3354 	if (!(scb->hscb->control & TAG_ENB))
   3355 		ahc_index_busy_tcl(ahc, scb->hscb->tcl, /*unbusy*/TRUE);
   3356 
   3357 	/*
   3358 	 * If the recovery SCB completes, we have to be
   3359 	 * out of our timeout.
   3360 	 */
   3361 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
   3362 
   3363 		struct	scb *scbp;
   3364 
   3365 		/*
   3366 		 * We were able to complete the command successfully,
   3367 		 * so reinstate the timeouts for all other pending
   3368 		 * commands.
   3369 		 */
   3370 		scbp = ahc->pending_ccbs.lh_first;
   3371 		while (scbp != NULL) {
   3372 			struct scsipi_xfer *txs = scbp->xs;
   3373 
   3374 			if (!(txs->xs_control & XS_CTL_POLL)) {
   3375 				callout_reset(&scbp->xs->xs_callout,
   3376 				    (scbp->xs->timeout * hz) / 1000,
   3377 				    ahc_timeout, scbp);
   3378 			}
   3379 			scbp = LIST_NEXT(scbp, plinks);
   3380 		}
   3381 
   3382 		/*
   3383 		 * Ensure that we didn't put a second instance of this
   3384 		 * SCB into the QINFIFO.
   3385 		 */
   3386 		ahc_search_qinfifo(ahc, SCB_TARGET(scb), SCB_CHANNEL(scb),
   3387 				   SCB_LUN(scb), scb->hscb->tag,
   3388 				   ROLE_INITIATOR, /*status*/0,
   3389 				   SEARCH_REMOVE);
   3390 		if (xs->error != XS_NOERROR)
   3391 			ahcsetccbstatus(xs, XS_TIMEOUT);
   3392 		scsi_print_addr(xs->sc_link);
   3393 		printf("no longer in timeout, status = %x\n", xs->status);
   3394 	}
   3395 
   3396 	if (xs->error != XS_NOERROR) {
   3397 		/* Don't clobber any existing error state */
   3398 	} else if ((scb->flags & SCB_SENSE) != 0) {
   3399 		/*
   3400 		 * We performed autosense retrieval.
   3401 		 *
   3402 		 * bzero the sense data before having
   3403 		 * the drive fill it.  The SCSI spec mandates
   3404 		 * that any untransfered data should be
   3405 		 * assumed to be zero.  Complete the 'bounce'
   3406 		 * of sense information through buffers accessible
   3407 		 * via bus-space by copying it into the clients
   3408 		 * csio.
   3409 		 */
   3410 		bzero(&xs->sense.scsi_sense, sizeof(xs->sense.scsi_sense));
   3411 		bcopy(&ahc->scb_data->sense[scb->hscb->tag],
   3412 		      &xs->sense.scsi_sense, le32toh(scb->sg_list->len));
   3413 		xs->error = XS_SENSE;
   3414 	}
   3415 	if (scb->flags & SCB_FREEZE_QUEUE) {
   3416 		ahc->devqueue_blocked[target]--;
   3417 		scb->flags &= ~SCB_FREEZE_QUEUE;
   3418 	}
   3419 
   3420 	requeue = scb->flags & SCB_REQUEUE;
   3421 	ahcfreescb(ahc, scb);
   3422 
   3423 	if (requeue) {
   3424 		/*
   3425 		 * Re-insert at the front of the private queue to
   3426 		 * preserve order.
   3427 		 */
   3428 		int s;
   3429 
   3430 		s = splbio();
   3431 		TAILQ_INSERT_HEAD(&ahc->sc_q, xs, adapter_q);
   3432 		splx(s);
   3433 	} else {
   3434 		xs->xs_status |= XS_STS_DONE;
   3435 		ahc_check_tags(ahc, xs);
   3436 		scsipi_done(xs);
   3437 	}
   3438 
   3439 	if ((xs = TAILQ_FIRST(&ahc->sc_q)) != NULL)
   3440 		ahc_action(xs);
   3441 }
   3442 
   3443 /*
   3444  * Determine the number of SCBs available on the controller
   3445  */
   3446 int
   3447 ahc_probe_scbs(struct ahc_softc *ahc) {
   3448 	int i;
   3449 
   3450 	for (i = 0; i < AHC_SCB_MAX; i++) {
   3451 		ahc_outb(ahc, SCBPTR, i);
   3452 		ahc_outb(ahc, SCB_CONTROL, i);
   3453 		if (ahc_inb(ahc, SCB_CONTROL) != i)
   3454 			break;
   3455 		ahc_outb(ahc, SCBPTR, 0);
   3456 		if (ahc_inb(ahc, SCB_CONTROL) != 0)
   3457 			break;
   3458 	}
   3459 	return (i);
   3460 }
   3461 
   3462 /*
   3463  * Start the board, ready for normal operation
   3464  */
   3465 int
   3466 ahc_init(struct ahc_softc *ahc)
   3467 {
   3468 	int	  max_targ = 15;
   3469 	int	  i;
   3470 	int	  term;
   3471 	u_int	  scsi_conf;
   3472 	u_int	  scsiseq_template;
   3473 	u_int	  ultraenb;
   3474 	u_int	  discenable;
   3475 	u_int	  tagenable;
   3476 	size_t	  driver_data_size;
   3477 	u_int32_t physaddr;
   3478 
   3479 #ifdef AHC_PRINT_SRAM
   3480 	printf("Scratch Ram:");
   3481 	for (i = 0x20; i < 0x5f; i++) {
   3482 		if (((i % 8) == 0) && (i != 0)) {
   3483 			printf ("\n              ");
   3484 		}
   3485 		printf (" 0x%x", ahc_inb(ahc, i));
   3486 	}
   3487 	if ((ahc->features & AHC_MORE_SRAM) != 0) {
   3488 		for (i = 0x70; i < 0x7f; i++) {
   3489 			if (((i % 8) == 0) && (i != 0)) {
   3490 				printf ("\n              ");
   3491 			}
   3492 			printf (" 0x%x", ahc_inb(ahc, i));
   3493 		}
   3494 	}
   3495 	printf ("\n");
   3496 #endif
   3497 
   3498 	/*
   3499 	 * Assume we have a board at this stage and it has been reset.
   3500 	 */
   3501 	if ((ahc->flags & AHC_USEDEFAULTS) != 0)
   3502 		ahc->our_id = ahc->our_id_b = 7;
   3503 
   3504 	/*
   3505 	 * Default to allowing initiator operations.
   3506 	 */
   3507 	ahc->flags |= AHC_INITIATORMODE;
   3508 
   3509 	/*
   3510 	 * DMA tag for our command fifos and other data in system memory
   3511 	 * the card's sequencer must be able to access.  For initiator
   3512 	 * roles, we need to allocate space for the qinfifo, qoutfifo,
   3513 	 * and untagged_scb arrays each of which are composed of 256
   3514 	 * 1 byte elements.  When providing for the target mode role,
   3515 	 * we additionally must provide space for the incoming target
   3516 	 * command fifo.
   3517 	 */
   3518 	driver_data_size = 3 * 256 * sizeof(u_int8_t);
   3519 
   3520 	if (ahc_createdmamem(ahc->parent_dmat, driver_data_size,
   3521 	    ahc->sc_dmaflags,
   3522 	    &ahc->shared_data_dmamap, (caddr_t *)&ahc->qoutfifo,
   3523 	    &ahc->shared_data_busaddr, &ahc->shared_data_seg,
   3524 	    &ahc->shared_data_nseg, ahc_name(ahc), "shared data") < 0)
   3525 		return (ENOMEM);
   3526 
   3527 	ahc->init_level++;
   3528 
   3529 	/* Allocate SCB data now that parent_dmat is initialized */
   3530 	if (ahc->scb_data->maxhscbs == 0)
   3531 		if (ahcinitscbdata(ahc) != 0)
   3532 			return (ENOMEM);
   3533 
   3534 	ahc->qinfifo = &ahc->qoutfifo[256];
   3535 	ahc->untagged_scbs = &ahc->qinfifo[256];
   3536 	/* There are no untagged SCBs active yet. */
   3537 	for (i = 0; i < 256; i++)
   3538 		ahc->untagged_scbs[i] = SCB_LIST_NULL;
   3539 
   3540 	/* All of our queues are empty */
   3541 	for (i = 0; i < 256; i++)
   3542 		ahc->qoutfifo[i] = SCB_LIST_NULL;
   3543 
   3544 	bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap, 0,
   3545 	    driver_data_size, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3546 
   3547 	/*
   3548 	 * Allocate a tstate to house information for our
   3549 	 * initiator presence on the bus as well as the user
   3550 	 * data for any target mode initiator.
   3551 	 */
   3552 	if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
   3553 		printf("%s: unable to allocate tmode_tstate.  "
   3554 		       "Failing attach\n", ahc_name(ahc));
   3555 		return (-1);
   3556 	}
   3557 
   3558 	if ((ahc->features & AHC_TWIN) != 0) {
   3559 		if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
   3560 			printf("%s: unable to allocate tmode_tstate.  "
   3561 			       "Failing attach\n", ahc_name(ahc));
   3562 			return (-1);
   3563 		}
   3564  		printf("Twin Channel, A SCSI Id=%d, B SCSI Id=%d, primary %c, ",
   3565 		       ahc->our_id, ahc->our_id_b,
   3566 		       ahc->flags & AHC_CHANNEL_B_PRIMARY? 'B': 'A');
   3567 	} else {
   3568 		if ((ahc->features & AHC_WIDE) != 0) {
   3569 			printf("Wide ");
   3570 		} else {
   3571 			printf("Single ");
   3572 		}
   3573 		printf("Channel %c, SCSI Id=%d, ", ahc->channel, ahc->our_id);
   3574 	}
   3575 
   3576 	ahc_outb(ahc, SEQ_FLAGS, 0);
   3577 
   3578 	if (ahc->scb_data->maxhscbs < AHC_SCB_MAX) {
   3579 		ahc->flags |= AHC_PAGESCBS;
   3580 		printf("%d/%d SCBs\n", ahc->scb_data->maxhscbs, AHC_SCB_MAX);
   3581 	} else {
   3582 		ahc->flags &= ~AHC_PAGESCBS;
   3583 		printf("%d SCBs\n", ahc->scb_data->maxhscbs);
   3584 	}
   3585 
   3586 #ifdef AHC_DEBUG
   3587 	if (ahc_debug & AHC_SHOWMISC) {
   3588 		printf("%s: hardware scb %d bytes; kernel scb %d bytes; "
   3589 		       "ahc_dma %d bytes\n",
   3590 			ahc_name(ahc),
   3591 		        sizeof(struct hardware_scb),
   3592 			sizeof(struct scb),
   3593 			sizeof(struct ahc_dma_seg));
   3594 	}
   3595 #endif /* AHC_DEBUG */
   3596 
   3597 	/* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
   3598 	if (ahc->features & AHC_TWIN) {
   3599 
   3600 		/*
   3601 		 * The device is gated to channel B after a chip reset,
   3602 		 * so set those values first
   3603 		 */
   3604 		term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
   3605 		if ((ahc->features & AHC_ULTRA2) != 0)
   3606 			ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id_b);
   3607 		else
   3608 			ahc_outb(ahc, SCSIID, ahc->our_id_b);
   3609 		scsi_conf = ahc_inb(ahc, SCSICONF + 1);
   3610 		ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
   3611 					|term|ENSTIMER|ACTNEGEN);
   3612 		ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
   3613 		ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
   3614 
   3615 		if ((scsi_conf & RESET_SCSI) != 0
   3616 		 && (ahc->flags & AHC_INITIATORMODE) != 0)
   3617 			ahc->flags |= AHC_RESET_BUS_B;
   3618 
   3619 		/* Select Channel A */
   3620 		ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
   3621 	}
   3622 	term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
   3623 	if ((ahc->features & AHC_ULTRA2) != 0)
   3624 		ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
   3625 	else
   3626 		ahc_outb(ahc, SCSIID, ahc->our_id);
   3627 	scsi_conf = ahc_inb(ahc, SCSICONF);
   3628 	ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
   3629 				|term
   3630 				|ENSTIMER|ACTNEGEN);
   3631 	ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
   3632 	ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
   3633 
   3634 	if ((scsi_conf & RESET_SCSI) != 0
   3635 	 && (ahc->flags & AHC_INITIATORMODE) != 0)
   3636 		ahc->flags |= AHC_RESET_BUS_A;
   3637 
   3638 	/*
   3639 	 * Look at the information that board initialization or
   3640 	 * the board bios has left us.
   3641 	 */
   3642 	ultraenb = 0;
   3643 	tagenable = ALL_TARGETS_MASK;
   3644 
   3645 	/* Grab the disconnection disable table and invert it for our needs */
   3646 	if (ahc->flags & AHC_USEDEFAULTS) {
   3647 		printf("%s: Host Adapter Bios disabled.  Using default SCSI "
   3648 			"device parameters\n", ahc_name(ahc));
   3649 		ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
   3650 			      AHC_TERM_ENB_A|AHC_TERM_ENB_B;
   3651 		discenable = ALL_TARGETS_MASK;
   3652 		if ((ahc->features & AHC_ULTRA) != 0)
   3653 			ultraenb = ALL_TARGETS_MASK;
   3654 	} else {
   3655 		discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
   3656 			   | ahc_inb(ahc, DISC_DSB));
   3657 		if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
   3658 			ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
   3659 				      | ahc_inb(ahc, ULTRA_ENB);
   3660 	}
   3661 
   3662 	if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
   3663 		max_targ = 7;
   3664 
   3665 	for (i = 0; i <= max_targ; i++) {
   3666 		struct ahc_initiator_tinfo *tinfo;
   3667 		struct tmode_tstate *tstate;
   3668 		u_int our_id;
   3669 		u_int target_id;
   3670 		char channel;
   3671 
   3672 		channel = 'A';
   3673 		our_id = ahc->our_id;
   3674 		target_id = i;
   3675 		if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
   3676 			channel = 'B';
   3677 			our_id = ahc->our_id_b;
   3678 			target_id = i % 8;
   3679 		}
   3680 		tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
   3681 					    target_id, &tstate);
   3682 		/* Default to async narrow across the board */
   3683 		bzero(tinfo, sizeof(*tinfo));
   3684 		if (ahc->flags & AHC_USEDEFAULTS) {
   3685 			if ((ahc->features & AHC_WIDE) != 0)
   3686 				tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
   3687 
   3688 			/*
   3689 			 * These will be truncated when we determine the
   3690 			 * connection type we have with the target.
   3691 			 */
   3692 			tinfo->user.period = ahc_syncrates->period;
   3693 			tinfo->user.offset = ~0;
   3694 		} else {
   3695 			u_int scsirate;
   3696 			u_int16_t mask;
   3697 
   3698 			/* Take the settings leftover in scratch RAM. */
   3699 			scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
   3700 			mask = (0x01 << i);
   3701 			if ((ahc->features & AHC_ULTRA2) != 0) {
   3702 				u_int offset;
   3703 				u_int maxsync;
   3704 
   3705 				if ((scsirate & SOFS) == 0x0F) {
   3706 					/*
   3707 					 * Haven't negotiated yet,
   3708 					 * so the format is different.
   3709 					 */
   3710 					scsirate = (scsirate & SXFR) >> 4
   3711 						 | (ultraenb & mask)
   3712 						  ? 0x08 : 0x0
   3713 						 | (scsirate & WIDEXFER);
   3714 					offset = MAX_OFFSET_ULTRA2;
   3715 				} else
   3716 					offset = ahc_inb(ahc, TARG_OFFSET + i);
   3717 				maxsync = AHC_SYNCRATE_ULTRA2;
   3718 				if ((ahc->features & AHC_DT) != 0)
   3719 					maxsync = AHC_SYNCRATE_DT;
   3720 				tinfo->user.period =
   3721 				    ahc_find_period(ahc, scsirate, maxsync);
   3722 				if (offset == 0)
   3723 					tinfo->user.period = 0;
   3724 				else
   3725 					tinfo->user.offset = ~0;
   3726 			} else if ((scsirate & SOFS) != 0) {
   3727 				tinfo->user.period =
   3728 				    ahc_find_period(ahc, scsirate,
   3729 						    (ultraenb & mask)
   3730 						   ? AHC_SYNCRATE_ULTRA
   3731 						   : AHC_SYNCRATE_FAST);
   3732 				if (tinfo->user.period != 0)
   3733 					tinfo->user.offset = ~0;
   3734 			}
   3735 			if ((scsirate & WIDEXFER) != 0
   3736 			 && (ahc->features & AHC_WIDE) != 0)
   3737 				tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
   3738 		}
   3739 		tinfo->goal = tinfo->user; /* force negotiation */
   3740 		tstate->ultraenb = ultraenb;
   3741 		tstate->discenable = discenable;
   3742 		tstate->tagenable = 0; /* Wait until the XPT says its okay */
   3743 		tstate->tagdisable = 0;
   3744 	}
   3745 	ahc->user_discenable = discenable;
   3746 	ahc->user_tagenable = tagenable;
   3747 
   3748 	/*
   3749 	 * Tell the sequencer where it can find our arrays in memory.
   3750 	 */
   3751 	physaddr = ahc->scb_data->hscb_busaddr;
   3752 	ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
   3753 	ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
   3754 	ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
   3755 	ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
   3756 
   3757 	physaddr = ahc->shared_data_busaddr;
   3758 	ahc_outb(ahc, SCBID_ADDR, physaddr & 0xFF);
   3759 	ahc_outb(ahc, SCBID_ADDR + 1, (physaddr >> 8) & 0xFF);
   3760 	ahc_outb(ahc, SCBID_ADDR + 2, (physaddr >> 16) & 0xFF);
   3761 	ahc_outb(ahc, SCBID_ADDR + 3, (physaddr >> 24) & 0xFF);
   3762 
   3763 	/* Target mode incomding command fifo */
   3764 	physaddr += 3 * 256 * sizeof(u_int8_t);
   3765 	ahc_outb(ahc, TMODE_CMDADDR, physaddr & 0xFF);
   3766 	ahc_outb(ahc, TMODE_CMDADDR + 1, (physaddr >> 8) & 0xFF);
   3767 	ahc_outb(ahc, TMODE_CMDADDR + 2, (physaddr >> 16) & 0xFF);
   3768 	ahc_outb(ahc, TMODE_CMDADDR + 3, (physaddr >> 24) & 0xFF);
   3769 
   3770 	/*
   3771 	 * Initialize the group code to command length table.
   3772 	 * This overrides the values in TARG_SCSIRATE, so only
   3773 	 * setup the table after we have processed that information.
   3774 	 */
   3775 	ahc_outb(ahc, CMDSIZE_TABLE, 5);
   3776 	ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
   3777 	ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
   3778 	ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
   3779 	ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
   3780 	ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
   3781 	ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
   3782 	ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
   3783 
   3784 	/* Tell the sequencer of our initial queue positions */
   3785 	ahc_outb(ahc, KERNEL_QINPOS, 0);
   3786 	ahc_outb(ahc, QINPOS, 0);
   3787 	ahc_outb(ahc, QOUTPOS, 0);
   3788 
   3789 #ifdef AHC_DEBUG
   3790 	if (ahc_debug & AHC_SHOWMISC)
   3791 		printf("DISCENABLE == 0x%x\nULTRAENB == 0x%x\n",
   3792 		       discenable, ultraenb);
   3793 #endif
   3794 
   3795 	/* Don't have any special messages to send to targets */
   3796 	ahc_outb(ahc, TARGET_MSG_REQUEST, 0);
   3797 	ahc_outb(ahc, TARGET_MSG_REQUEST + 1, 0);
   3798 
   3799 	/*
   3800 	 * Use the built in queue management registers
   3801 	 * if they are available.
   3802 	 */
   3803 	if ((ahc->features & AHC_QUEUE_REGS) != 0) {
   3804 		ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
   3805 		ahc_outb(ahc, SDSCB_QOFF, 0);
   3806 		ahc_outb(ahc, SNSCB_QOFF, 0);
   3807 		ahc_outb(ahc, HNSCB_QOFF, 0);
   3808 	}
   3809 
   3810 
   3811 	/* We don't have any waiting selections */
   3812 	ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
   3813 
   3814 	/* Our disconnection list is empty too */
   3815 	ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
   3816 
   3817 	/* Message out buffer starts empty */
   3818 	ahc_outb(ahc, MSG_OUT, MSG_NOOP);
   3819 
   3820 	/*
   3821 	 * Setup the allowed SCSI Sequences based on operational mode.
   3822 	 * If we are a target, we'll enalbe select in operations once
   3823 	 * we've had a lun enabled.
   3824 	 */
   3825 	scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
   3826 	if ((ahc->flags & AHC_INITIATORMODE) != 0)
   3827 		scsiseq_template |= ENRSELI;
   3828 	ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
   3829 
   3830 	/*
   3831 	 * Load the Sequencer program and Enable the adapter
   3832 	 * in "fast" mode.
   3833          */
   3834 #ifdef AHC_DEBUG
   3835 	printf("%s: Downloading Sequencer Program...",
   3836 	       ahc_name(ahc));
   3837 #endif
   3838 
   3839 	ahc_loadseq(ahc);
   3840 
   3841 	/* We have to wait until after any system dumps... */
   3842 	shutdownhook_establish(ahc_shutdown, ahc);
   3843 
   3844 	return (0);
   3845 }
   3846 
   3847 static int
   3848 ahc_ioctl(struct scsipi_link *sc_link, u_long cmd, caddr_t addr, int flag,
   3849 	  struct proc *p)
   3850 {
   3851 	struct ahc_softc *ahc = sc_link->adapter_softc;
   3852 	char channel;
   3853 	int s, ret = ENOTTY;
   3854 
   3855 	switch (cmd) {
   3856 	case SCBUSIORESET:
   3857 		channel = SIM_CHANNEL(ahc, sc_link);
   3858 		s = splbio();
   3859 		ahc_reset_channel(ahc, channel, TRUE);
   3860 		splx(s);
   3861 		ret = 0;
   3862 		break;
   3863 	default:
   3864 	}
   3865 
   3866 	return ret;
   3867 }
   3868 
   3869 
   3870 /*
   3871  * XXX fvdl the busy_tcl checks and settings should only be done
   3872  * for the non-tagged queueing case, but we don't do tagged queueing
   3873  * yet, so..
   3874  */
   3875 static int32_t
   3876 ahc_action(struct scsipi_xfer *xs)
   3877 {
   3878 	struct scsipi_xfer *first_xs, *next_xs = NULL;
   3879 	struct ahc_softc *ahc;
   3880 	struct scb *scb;
   3881 	struct hardware_scb *hscb;
   3882 	struct ahc_initiator_tinfo *tinfo;
   3883 	struct tmode_tstate *tstate;
   3884 	u_int target_id;
   3885 	u_int our_id;
   3886 	int s, tcl;
   3887 	u_int16_t mask;
   3888 	char channel;
   3889 	int dontqueue = 0, fromqueue = 0;
   3890 
   3891 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("ahc_action\n"));
   3892 
   3893 	ahc = (struct ahc_softc *)xs->sc_link->adapter_softc;
   3894 
   3895 	/* must protect the queue */
   3896 	s = splbio();
   3897 
   3898 	if (xs == TAILQ_FIRST(&ahc->sc_q)) {
   3899 		/*
   3900 		 * Called from ahc_done. Calling with the first entry in
   3901 		 * the queue is really just a way of seeing where we're
   3902 		 * called from. Now, find the first eligible SCB to send,
   3903 		 * e.g. one which will be accepted immediately.
   3904 		 */
   3905 
   3906 		if (ahc->queue_blocked) {
   3907 			splx(s);
   3908 			return (TRY_AGAIN_LATER);
   3909 		}
   3910 
   3911 		xs = ahc_first_xs(ahc);
   3912 		if (xs == NULL) {
   3913 			splx(s);
   3914 			return (TRY_AGAIN_LATER);
   3915 		}
   3916 
   3917 		next_xs = TAILQ_NEXT(xs, adapter_q);
   3918 		TAILQ_REMOVE(&ahc->sc_q, xs, adapter_q);
   3919 		fromqueue = 1;
   3920 		goto get_scb;
   3921 	}
   3922 
   3923 	/*
   3924 	 * If no new requests are accepted, just insert into the
   3925 	 * private queue to wait for our turn.
   3926 	 */
   3927 	tcl = XS_TCL(ahc, xs);
   3928 
   3929 	if (ahc->queue_blocked ||
   3930 	    ahc->devqueue_blocked[xs->sc_link->scsipi_scsi.target] ||
   3931 	    (!ahc_istagged_device(ahc, xs, 0) &&
   3932 	     ahc_index_busy_tcl(ahc, tcl, FALSE) != SCB_LIST_NULL)) {
   3933 		if (dontqueue) {
   3934 			splx(s);
   3935 			xs->error = XS_DRIVER_STUFFUP;
   3936 			return TRY_AGAIN_LATER;
   3937 		}
   3938 		TAILQ_INSERT_TAIL(&ahc->sc_q, xs, adapter_q);
   3939 		splx(s);
   3940 		return SUCCESSFULLY_QUEUED;
   3941 	}
   3942 
   3943 	first_xs = ahc_first_xs(ahc);
   3944 
   3945 	/* determine safety of software queueing */
   3946 	dontqueue = xs->xs_control & XS_CTL_POLL;
   3947 
   3948 	/*
   3949 	 * Handle situations where there's already entries in the
   3950 	 * queue.
   3951 	 */
   3952 	if (first_xs != NULL) {
   3953 		/*
   3954 		 * If we can't queue, we have to abort, since
   3955 		 * we have to preserve order.
   3956 		 */
   3957 		if (dontqueue) {
   3958 			splx(s);
   3959 			xs->error = XS_DRIVER_STUFFUP;
   3960 			return (TRY_AGAIN_LATER);
   3961 		}
   3962 
   3963 		/*
   3964 		 * Swap with the first queue entry.
   3965 		 */
   3966 		TAILQ_INSERT_TAIL(&ahc->sc_q, xs, adapter_q);
   3967 		xs = first_xs;
   3968 		next_xs = TAILQ_NEXT(xs, adapter_q);
   3969 		TAILQ_REMOVE(&ahc->sc_q, xs, adapter_q);
   3970 		fromqueue = 1;
   3971 
   3972 	}
   3973 
   3974 get_scb:
   3975 
   3976 	target_id = xs->sc_link->scsipi_scsi.target;
   3977 	our_id = SIM_SCSI_ID(ahc, xs->sc_link);
   3978 
   3979 	/*
   3980 	 * get an scb to use.
   3981 	 */
   3982 	if ((scb = ahcgetscb(ahc)) == NULL) {
   3983 
   3984 		if (dontqueue) {
   3985 			splx(s);
   3986 			xs->error = XS_DRIVER_STUFFUP;
   3987 			return (TRY_AGAIN_LATER);
   3988 		}
   3989 
   3990 		/*
   3991 		 * If we were pulled off the queue, put ourselves
   3992 		 * back to where we came from, otherwise tack ourselves
   3993 		 * onto the end.
   3994 		 */
   3995 		if (fromqueue && next_xs != NULL)
   3996 			TAILQ_INSERT_BEFORE(xs, next_xs, adapter_q);
   3997 		else
   3998 			TAILQ_INSERT_TAIL(&ahc->sc_q, xs, adapter_q);
   3999 
   4000 		splx(s);
   4001 		return (SUCCESSFULLY_QUEUED);
   4002 	}
   4003 
   4004 	tcl = XS_TCL(ahc, xs);
   4005 
   4006 #ifdef DIAGNOSTIC
   4007 	if (!ahc_istagged_device(ahc, xs, 0) &&
   4008 	    ahc_index_busy_tcl(ahc, tcl, FALSE) != SCB_LIST_NULL)
   4009 		panic("ahc: queuing for busy target");
   4010 #endif
   4011 
   4012 	scb->xs = xs;
   4013 	hscb = scb->hscb;
   4014 	hscb->tcl = tcl;
   4015 
   4016 	if (ahc_istagged_device(ahc, xs, 0))
   4017 		scb->hscb->control |= MSG_SIMPLE_Q_TAG;
   4018 	else
   4019 		ahc_busy_tcl(ahc, scb);
   4020 
   4021 	splx(s);
   4022 
   4023 	channel = SIM_CHANNEL(ahc, xs->sc_link);
   4024 	if (ahc->inited_channels[channel - 'A'] == 0) {
   4025 		s = splbio();
   4026 		ahc_reset_channel(ahc, channel, TRUE);
   4027 		splx(s);
   4028 		ahc->inited_channels[channel - 'A'] = 1;
   4029 	}
   4030 
   4031 	/*
   4032 	 * Put all the arguments for the xfer in the scb
   4033 	 */
   4034 
   4035 	mask = SCB_TARGET_MASK(scb);
   4036 	tinfo = ahc_fetch_transinfo(ahc, SIM_CHANNEL(ahc, xs->sc_link), our_id,
   4037 				    target_id, &tstate);
   4038 	if (ahc->inited_targets[target_id] == 0) {
   4039 		struct ahc_devinfo devinfo;
   4040 
   4041 		s = splbio();
   4042 		ahc_compile_devinfo(&devinfo, our_id, target_id,
   4043 		    xs->sc_link->scsipi_scsi.lun, SIM_CHANNEL(ahc, xs->sc_link),
   4044 		    ROLE_INITIATOR);
   4045 		ahc_update_target_msg_request(ahc, &devinfo, tinfo, TRUE,
   4046 		    FALSE);
   4047 		ahc->inited_targets[target_id] = 1;
   4048 		splx(s);
   4049 	}
   4050 
   4051 	hscb->scsirate = tinfo->scsirate;
   4052 	hscb->scsioffset = tinfo->current.offset;
   4053 	if ((tstate->ultraenb & mask) != 0)
   4054 		hscb->control |= ULTRAENB;
   4055 
   4056 	if ((tstate->discenable & mask) != 0)
   4057 		hscb->control |= DISCENB;
   4058 
   4059 	if (xs->xs_control & XS_CTL_RESET) {
   4060 		hscb->cmdpointer = 0;
   4061 		scb->flags |= SCB_DEVICE_RESET;
   4062 		hscb->control |= MK_MESSAGE;
   4063 		return ahc_execute_scb(scb, NULL, 0);
   4064 	}
   4065 
   4066 	return ahc_setup_data(ahc, xs, scb);
   4067 }
   4068 
   4069 static int
   4070 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments)
   4071 {
   4072 	struct	 scb *scb;
   4073 	struct scsipi_xfer *xs;
   4074 	struct	 ahc_softc *ahc;
   4075 	int	 s;
   4076 
   4077 	scb = (struct scb *)arg;
   4078 	xs = scb->xs;
   4079 	ahc = (struct ahc_softc *)xs->sc_link->adapter_softc;
   4080 
   4081 
   4082 	if (nsegments != 0) {
   4083 		struct	  ahc_dma_seg *sg;
   4084 		bus_dma_segment_t *end_seg;
   4085 		int op;
   4086 
   4087 		end_seg = dm_segs + nsegments;
   4088 
   4089 		/* Copy the first SG into the data pointer area */
   4090 		scb->hscb->data = dm_segs->ds_addr;
   4091 		scb->hscb->datalen = dm_segs->ds_len;
   4092 
   4093 		/* Copy the segments into our SG list */
   4094 		sg = scb->sg_list;
   4095 		while (dm_segs < end_seg) {
   4096 			sg->addr = dm_segs->ds_addr;
   4097 			sg->len = dm_segs->ds_len;
   4098 			ahc_swap_sg(sg);
   4099 			sg++;
   4100 			dm_segs++;
   4101 		}
   4102 
   4103 		/* Note where to find the SG entries in bus space */
   4104 		scb->hscb->SG_pointer = scb->sg_list_phys;
   4105 
   4106 		if (xs->xs_control & XS_CTL_DATA_IN)
   4107 			op = BUS_DMASYNC_PREREAD;
   4108 		else
   4109 			op = BUS_DMASYNC_PREWRITE;
   4110 
   4111 		bus_dmamap_sync(ahc->parent_dmat, scb->dmamap, 0,
   4112 		    scb->dmamap->dm_mapsize, op);
   4113 
   4114 	} else {
   4115 		scb->hscb->SG_pointer = 0;
   4116 		scb->hscb->data = 0;
   4117 		scb->hscb->datalen = 0;
   4118 	}
   4119 
   4120 	scb->sg_count = scb->hscb->SG_count = nsegments;
   4121 
   4122 	s = splbio();
   4123 
   4124 	/*
   4125 	 * Last time we need to check if this SCB needs to
   4126 	 * be aborted.
   4127 	 */
   4128 	if (xs->xs_status & XS_STS_DONE) {
   4129 		if (!ahc_istagged_device(ahc, xs, 0))
   4130 			ahc_index_busy_tcl(ahc, scb->hscb->tcl, TRUE);
   4131 		if (nsegments != 0)
   4132 			bus_dmamap_unload(ahc->parent_dmat, scb->dmamap);
   4133 		ahcfreescb(ahc, scb);
   4134 		splx(s);
   4135 		return (COMPLETE);
   4136 	}
   4137 
   4138 #ifdef DIAGNOSTIC
   4139 	if (scb->sg_count > 255)
   4140 		panic("ahc bad sg_count");
   4141 #endif
   4142 
   4143 	ahc_swap_hscb(scb->hscb);
   4144 
   4145 	LIST_INSERT_HEAD(&ahc->pending_ccbs, scb, plinks);
   4146 
   4147 	scb->flags |= SCB_ACTIVE;
   4148 
   4149 	if (!(xs->xs_control & XS_CTL_POLL))
   4150 		callout_reset(&scb->xs->xs_callout, (xs->timeout * hz) / 1000,
   4151 		    ahc_timeout, scb);
   4152 
   4153 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
   4154 #if 0
   4155 		printf("Continueing Immediate Command %d:%d\n",
   4156 		       xs->sc_link->scsipi_scsi.target,
   4157 		       xs->sc_link->scsipi_scsi.lun);
   4158 #endif
   4159 		pause_sequencer(ahc);
   4160 		if ((ahc->flags & AHC_PAGESCBS) == 0)
   4161 			ahc_outb(ahc, SCBPTR, scb->hscb->tag);
   4162 		ahc_outb(ahc, SCB_TAG, scb->hscb->tag);
   4163 		ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
   4164 		unpause_sequencer(ahc);
   4165 	} else {
   4166 
   4167 #if 0
   4168 		printf("tag %x at qpos %u vaddr %p paddr 0x%lx\n",
   4169 		    scb->hscb->tag, ahc->qinfifonext,
   4170 		    &ahc->qinfifo[ahc->qinfifonext],
   4171 		    ahc->shared_data_busaddr + 1024 + ahc->qinfifonext);
   4172 #endif
   4173 
   4174 		ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
   4175 
   4176 		bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
   4177 		    QINFIFO_OFFSET * 256, 256, BUS_DMASYNC_PREWRITE);
   4178 
   4179 		if ((ahc->features & AHC_QUEUE_REGS) != 0) {
   4180 			ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
   4181 		} else {
   4182 			pause_sequencer(ahc);
   4183 			ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
   4184 			unpause_sequencer(ahc);
   4185 		}
   4186 	}
   4187 
   4188 #ifdef AHC_DEBUG
   4189 	if (ahc_debug & AHC_SHOWCMDS) {
   4190 		scsi_print_addr(xs->sc_link);
   4191 		printf("opcode %d tag %x len %d flags %x control %x fpos %u"
   4192 		    " rate %x\n",
   4193 		    xs->cmdstore.opcode, scb->hscb->tag, scb->hscb->datalen,
   4194 		    scb->flags, scb->hscb->control, ahc->qinfifonext,
   4195 		    scb->hscb->scsirate);
   4196 	}
   4197 #endif
   4198 
   4199 	if (!(xs->xs_control & XS_CTL_POLL)) {
   4200 		splx(s);
   4201 		return (SUCCESSFULLY_QUEUED);
   4202 	}
   4203 	/*
   4204 	 * If we can't use interrupts, poll for completion
   4205 	 */
   4206 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_poll\n"));
   4207 	do {
   4208 		if (ahc_poll(ahc, xs->timeout)) {
   4209 			if (!(xs->xs_control & XS_CTL_SILENT))
   4210 				printf("cmd fail\n");
   4211 			ahc_timeout(scb);
   4212 			break;
   4213 		}
   4214 	} while (!(xs->xs_status & XS_STS_DONE));
   4215 	splx(s);
   4216 	return (COMPLETE);
   4217 }
   4218 
   4219 static int
   4220 ahc_poll(struct ahc_softc *ahc, int wait)
   4221 {
   4222 	while (--wait) {
   4223 		DELAY(1000);
   4224 		if (ahc_inb(ahc, INTSTAT) & INT_PEND)
   4225 			break;
   4226 	}
   4227 
   4228 	if (wait == 0) {
   4229 		printf("%s: board is not responding\n", ahc_name(ahc));
   4230 		return (EIO);
   4231 	}
   4232 
   4233 	ahc_intr((void *)ahc);
   4234 	return (0);
   4235 }
   4236 
   4237 static int
   4238 ahc_setup_data(struct ahc_softc *ahc, struct scsipi_xfer *xs,
   4239 	       struct scb *scb)
   4240 {
   4241 	struct hardware_scb *hscb;
   4242 
   4243 	hscb = scb->hscb;
   4244 	xs->resid = xs->status = 0;
   4245 
   4246 	hscb->cmdlen = xs->cmdlen;
   4247 	memcpy(hscb->cmdstore, xs->cmd, xs->cmdlen);
   4248 	hscb->cmdpointer = hscb->cmdstore_busaddr;
   4249 
   4250 	/* Only use S/G if there is a transfer */
   4251 	if (xs->datalen) {
   4252 		int error;
   4253 
   4254 		error = bus_dmamap_load(ahc->parent_dmat,
   4255 			    scb->dmamap, xs->data,
   4256 			    xs->datalen, NULL,
   4257 			    (xs->xs_control & XS_CTL_NOSLEEP) ?
   4258 			    BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
   4259 		if (error) {
   4260 			if (!ahc_istagged_device(ahc, xs, 0))
   4261 				ahc_index_busy_tcl(ahc, hscb->tcl, TRUE);
   4262 			return (TRY_AGAIN_LATER);	/* XXX fvdl */
   4263 		}
   4264 		error = ahc_execute_scb(scb,
   4265 		    scb->dmamap->dm_segs,
   4266 		    scb->dmamap->dm_nsegs);
   4267 		return error;
   4268 	} else {
   4269 		return ahc_execute_scb(scb, NULL, 0);
   4270 	}
   4271 }
   4272 
   4273 static void
   4274 ahc_freeze_devq(struct ahc_softc *ahc, struct scsipi_link *sc_link)
   4275 {
   4276 	int	target;
   4277 	char	channel;
   4278 	int	lun;
   4279 
   4280 	target = sc_link->scsipi_scsi.target;
   4281 	lun = sc_link->scsipi_scsi.lun;
   4282 	channel = sc_link->scsipi_scsi.channel;
   4283 
   4284 	ahc_search_qinfifo(ahc, target, channel, lun,
   4285 			   /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
   4286 			   SCB_REQUEUE, SEARCH_COMPLETE);
   4287 }
   4288 
   4289 static void
   4290 ahcallocscbs(struct ahc_softc *ahc)
   4291 {
   4292 	struct scb_data *scb_data;
   4293 	struct scb *next_scb;
   4294 	struct sg_map_node *sg_map;
   4295 	bus_addr_t physaddr;
   4296 	struct ahc_dma_seg *segs;
   4297 	int newcount;
   4298 	int i;
   4299 
   4300 	scb_data = ahc->scb_data;
   4301 	if (scb_data->numscbs >= AHC_SCB_MAX)
   4302 		/* Can't allocate any more */
   4303 		return;
   4304 
   4305 	next_scb = &scb_data->scbarray[scb_data->numscbs];
   4306 
   4307 	sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
   4308 
   4309 	if (sg_map == NULL)
   4310 		return;
   4311 
   4312 	if (ahc_createdmamem(ahc->parent_dmat, PAGE_SIZE, ahc->sc_dmaflags,
   4313 	    &sg_map->sg_dmamap,
   4314 	    (caddr_t *)&sg_map->sg_vaddr, &sg_map->sg_physaddr,
   4315 	    &sg_map->sg_dmasegs, &sg_map->sg_nseg, ahc_name(ahc),
   4316 	    "SG space") < 0) {
   4317 		free(sg_map, M_DEVBUF);
   4318 		return;
   4319 	}
   4320 
   4321 	SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
   4322 
   4323 	segs = sg_map->sg_vaddr;
   4324 	physaddr = sg_map->sg_physaddr;
   4325 
   4326 	newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
   4327 	for (i = 0; scb_data->numscbs < AHC_SCB_MAX && i < newcount; i++) {
   4328 		int error;
   4329 
   4330 		next_scb->sg_list = segs;
   4331 		/*
   4332 		 * The sequencer always starts with the second entry.
   4333 		 * The first entry is embedded in the scb.
   4334 		 */
   4335 		next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
   4336 		next_scb->flags = SCB_FREE;
   4337 		error = bus_dmamap_create(ahc->parent_dmat,
   4338 			    AHC_MAXTRANSFER_SIZE, AHC_NSEG, MAXBSIZE, 0,
   4339 			    BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW|ahc->sc_dmaflags,
   4340 			    &next_scb->dmamap);
   4341 		if (error != 0)
   4342 			break;
   4343 		next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
   4344 		next_scb->hscb->tag = ahc->scb_data->numscbs;
   4345 		next_scb->hscb->cmdstore_busaddr =
   4346 		    ahc_hscb_busaddr(ahc, next_scb->hscb->tag)
   4347 		  + offsetof(struct hardware_scb, cmdstore);
   4348 		next_scb->hscb->cmdstore_busaddr =
   4349 		    htole32(next_scb->hscb->cmdstore_busaddr);
   4350 		SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs, next_scb, links);
   4351 		segs += AHC_NSEG;
   4352 		physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
   4353 		next_scb++;
   4354 		ahc->scb_data->numscbs++;
   4355 	}
   4356 #ifdef AHC_DEBUG
   4357 	if (ahc_debug & AHC_SHOWSCBALLOC)
   4358 		printf("%s: allocated %d new SCBs count now %d\n",
   4359 		    ahc_name(ahc), i - 1, ahc->scb_data->numscbs);
   4360 #endif
   4361 }
   4362 
   4363 #ifdef AHC_DUMP_SEQ
   4364 static void
   4365 ahc_dumpseq(struct ahc_softc* ahc)
   4366 {
   4367 	int i;
   4368 	int max_prog;
   4369 
   4370 	if ((ahc->chip & AHC_BUS_MASK) < AHC_PCI)
   4371 		max_prog = 448;
   4372 	else if ((ahc->features & AHC_ULTRA2) != 0)
   4373 		max_prog = 768;
   4374 	else
   4375 		max_prog = 512;
   4376 
   4377 	ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
   4378 	ahc_outb(ahc, SEQADDR0, 0);
   4379 	ahc_outb(ahc, SEQADDR1, 0);
   4380 	for (i = 0; i < max_prog; i++) {
   4381 		u_int8_t ins_bytes[4];
   4382 
   4383 		ahc_insb(ahc, SEQRAM, ins_bytes, 4);
   4384 		printf("0x%08x\n", ins_bytes[0] << 24
   4385 				 | ins_bytes[1] << 16
   4386 				 | ins_bytes[2] << 8
   4387 				 | ins_bytes[3]);
   4388 	}
   4389 }
   4390 #endif
   4391 
   4392 static void
   4393 ahc_loadseq(struct ahc_softc *ahc)
   4394 {
   4395 	struct patch *cur_patch;
   4396 	int i;
   4397 	int downloaded;
   4398 	int skip_addr;
   4399 	u_int8_t download_consts[4];
   4400 
   4401 	/* Setup downloadable constant table */
   4402 #if 0
   4403 	/* No downloaded constants are currently defined. */
   4404 	download_consts[TMODE_NUMCMDS] = ahc->num_targetcmds;
   4405 #endif
   4406 
   4407 	cur_patch = patches;
   4408 	downloaded = 0;
   4409 	skip_addr = 0;
   4410 	ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
   4411 	ahc_outb(ahc, SEQADDR0, 0);
   4412 	ahc_outb(ahc, SEQADDR1, 0);
   4413 
   4414 	for (i = 0; i < sizeof(seqprog)/4; i++) {
   4415 		if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
   4416 			/*
   4417 			 * Don't download this instruction as it
   4418 			 * is in a patch that was removed.
   4419 			 */
   4420                         continue;
   4421 		}
   4422 		ahc_download_instr(ahc, i, download_consts);
   4423 		downloaded++;
   4424 	}
   4425 	ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
   4426 	restart_sequencer(ahc);
   4427 
   4428 #ifdef AHC_DEBUG
   4429 	printf(" %d instructions downloaded\n", downloaded);
   4430 #endif
   4431 }
   4432 
   4433 static int
   4434 ahc_check_patch(struct ahc_softc *ahc, struct patch **start_patch,
   4435 		int start_instr, int *skip_addr)
   4436 {
   4437 	struct	patch *cur_patch;
   4438 	struct	patch *last_patch;
   4439 	int	num_patches;
   4440 
   4441 	num_patches = sizeof(patches)/sizeof(struct patch);
   4442 	last_patch = &patches[num_patches];
   4443 	cur_patch = *start_patch;
   4444 
   4445 	while (cur_patch < last_patch && start_instr == cur_patch->begin) {
   4446 
   4447 		if (cur_patch->patch_func(ahc) == 0) {
   4448 
   4449 			/* Start rejecting code */
   4450 			*skip_addr = start_instr + cur_patch->skip_instr;
   4451 			cur_patch += cur_patch->skip_patch;
   4452 		} else {
   4453 			/* Accepted this patch.  Advance to the next
   4454 			 * one and wait for our intruction pointer to
   4455 			 * hit this point.
   4456 			 */
   4457 			cur_patch++;
   4458 		}
   4459 	}
   4460 
   4461 	*start_patch = cur_patch;
   4462 	if (start_instr < *skip_addr)
   4463 		/* Still skipping */
   4464 		return (0);
   4465 
   4466 	return (1);
   4467 }
   4468 
   4469 static void
   4470 ahc_download_instr(struct ahc_softc *ahc, int instrptr, u_int8_t *dconsts)
   4471 {
   4472 	union	ins_formats instr;
   4473 	struct	ins_format1 *fmt1_ins;
   4474 	struct	ins_format3 *fmt3_ins;
   4475 	u_int	opcode;
   4476 
   4477 	/* Structure copy */
   4478 	instr = *(union ins_formats*)&seqprog[instrptr * 4];
   4479 
   4480 	instr.integer = le32toh(instr.integer);
   4481 
   4482 	fmt1_ins = &instr.format1;
   4483 	fmt3_ins = NULL;
   4484 
   4485 	/* Pull the opcode */
   4486 	opcode = instr.format1.opcode;
   4487 	switch (opcode) {
   4488 	case AIC_OP_JMP:
   4489 	case AIC_OP_JC:
   4490 	case AIC_OP_JNC:
   4491 	case AIC_OP_CALL:
   4492 	case AIC_OP_JNE:
   4493 	case AIC_OP_JNZ:
   4494 	case AIC_OP_JE:
   4495 	case AIC_OP_JZ:
   4496 	{
   4497 		struct patch *cur_patch;
   4498 		int address_offset;
   4499 		u_int address;
   4500 		int skip_addr;
   4501 		int i;
   4502 
   4503 		fmt3_ins = &instr.format3;
   4504 		address_offset = 0;
   4505 		address = fmt3_ins->address;
   4506 		cur_patch = patches;
   4507 		skip_addr = 0;
   4508 
   4509 		for (i = 0; i < address;) {
   4510 
   4511 			ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
   4512 
   4513 			if (skip_addr > i) {
   4514 				int end_addr;
   4515 
   4516 				end_addr = MIN(address, skip_addr);
   4517 				address_offset += end_addr - i;
   4518 				i = skip_addr;
   4519 			} else {
   4520 				i++;
   4521 			}
   4522 		}
   4523 		address -= address_offset;
   4524 		fmt3_ins->address = address;
   4525 		/* FALLTHROUGH */
   4526 	}
   4527 	case AIC_OP_OR:
   4528 	case AIC_OP_AND:
   4529 	case AIC_OP_XOR:
   4530 	case AIC_OP_ADD:
   4531 	case AIC_OP_ADC:
   4532 	case AIC_OP_BMOV:
   4533 		if (fmt1_ins->parity != 0) {
   4534 			fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
   4535 		}
   4536 		fmt1_ins->parity = 0;
   4537 		/* FALLTHROUGH */
   4538 	case AIC_OP_ROL:
   4539 		if ((ahc->features & AHC_ULTRA2) != 0) {
   4540 			int i, count;
   4541 
   4542 			/* Calculate odd parity for the instruction */
   4543 			for (i = 0, count = 0; i < 31; i++) {
   4544 				u_int32_t mask;
   4545 
   4546 				mask = 0x01 << i;
   4547 				if ((instr.integer & mask) != 0)
   4548 					count++;
   4549 			}
   4550 			if ((count & 0x01) == 0)
   4551 				instr.format1.parity = 1;
   4552 		} else {
   4553 			/* Compress the instruction for older sequencers */
   4554 			if (fmt3_ins != NULL) {
   4555 				instr.integer =
   4556 					fmt3_ins->immediate
   4557 				      | (fmt3_ins->source << 8)
   4558 				      | (fmt3_ins->address << 16)
   4559 				      |	(fmt3_ins->opcode << 25);
   4560 			} else {
   4561 				instr.integer =
   4562 					fmt1_ins->immediate
   4563 				      | (fmt1_ins->source << 8)
   4564 				      | (fmt1_ins->destination << 16)
   4565 				      |	(fmt1_ins->ret << 24)
   4566 				      |	(fmt1_ins->opcode << 25);
   4567 			}
   4568 		}
   4569 		instr.integer = htole32(instr.integer);
   4570 		ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
   4571 		break;
   4572 	default:
   4573 		panic("Unknown opcode encountered in seq program");
   4574 		break;
   4575 	}
   4576 }
   4577 
   4578 static void
   4579 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb)
   4580 {
   4581 
   4582 	if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
   4583 		struct scb *scbp;
   4584 
   4585 		scb->flags |= SCB_RECOVERY_SCB;
   4586 
   4587 		/*
   4588 		 * Take all queued, but not sent SCBs out of the equation.
   4589 		 * Also ensure that no new CCBs are queued to us while we
   4590 		 * try to fix this problem.
   4591 		 */
   4592 		ahc->queue_blocked = 1;
   4593 
   4594 		/*
   4595 		 * Go through all of our pending SCBs and remove
   4596 		 * any scheduled timeouts for them.  We will reschedule
   4597 		 * them after we've successfully fixed this problem.
   4598 		 */
   4599 		scbp = ahc->pending_ccbs.lh_first;
   4600 		while (scbp != NULL) {
   4601 			callout_stop(&scbp->xs->xs_callout);
   4602 			scbp = scbp->plinks.le_next;
   4603 		}
   4604 	}
   4605 }
   4606 
   4607 static void
   4608 ahc_timeout(void *arg)
   4609 {
   4610 	struct	scb *scb;
   4611 	struct	ahc_softc *ahc;
   4612 	int	s, found;
   4613 	u_int	last_phase;
   4614 	int	target;
   4615 	int	lun;
   4616 	int	i;
   4617 	char	channel;
   4618 
   4619 	scb = (struct scb *)arg;
   4620 	ahc = (struct ahc_softc *)scb->xs->sc_link->adapter_softc;
   4621 
   4622 	s = splbio();
   4623 
   4624 	/*
   4625 	 * Ensure that the card doesn't do anything
   4626 	 * behind our back.  Also make sure that we
   4627 	 * didn't "just" miss an interrupt that would
   4628 	 * affect this timeout.
   4629 	 */
   4630 	do {
   4631 		ahc_intr(ahc);
   4632 		pause_sequencer(ahc);
   4633 	} while (ahc_inb(ahc, INTSTAT) & INT_PEND);
   4634 
   4635 	if ((scb->flags & SCB_ACTIVE) == 0) {
   4636 		/* Previous timeout took care of me already */
   4637 		printf("Timedout SCB handled by another timeout\n");
   4638 		unpause_sequencer(ahc);
   4639 		splx(s);
   4640 		return;
   4641 	}
   4642 
   4643 	target = SCB_TARGET(scb);
   4644 	channel = SCB_CHANNEL(scb);
   4645 	lun = SCB_LUN(scb);
   4646 
   4647 	scsi_print_addr(scb->xs->sc_link);
   4648 	printf("SCB %x - timed out ", scb->hscb->tag);
   4649 	/*
   4650 	 * Take a snapshot of the bus state and print out
   4651 	 * some information so we can track down driver bugs.
   4652 	 */
   4653 	last_phase = ahc_inb(ahc, LASTPHASE);
   4654 
   4655 	for (i = 0; i < num_phases; i++) {
   4656 		if (last_phase == phase_table[i].phase)
   4657 			break;
   4658 	}
   4659 	printf("%s", phase_table[i].phasemsg);
   4660 
   4661 	printf(", SEQADDR == 0x%x\n",
   4662 	       ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
   4663 	printf("SCSIRATE == 0x%x\n", ahc_inb(ahc, SCSIRATE));
   4664 
   4665 #ifdef AHC_DEBUG
   4666 	ahc_print_scb(scb);
   4667 #endif
   4668 
   4669 #if 0
   4670 	printf("SSTAT1 == 0x%x\n", ahc_inb(ahc, SSTAT1));
   4671 	printf("SSTAT3 == 0x%x\n", ahc_inb(ahc, SSTAT3));
   4672 	printf("SCSIPHASE == 0x%x\n", ahc_inb(ahc, SCSIPHASE));
   4673 	printf("SCSIOFFSET == 0x%x\n", ahc_inb(ahc, SCSIOFFSET));
   4674 	printf("SEQ_FLAGS == 0x%x\n", ahc_inb(ahc, SEQ_FLAGS));
   4675 	printf("SCB_DATAPTR == 0x%x\n", ahc_inb(ahc, SCB_DATAPTR)
   4676 				      | ahc_inb(ahc, SCB_DATAPTR + 1) << 8
   4677 				      | ahc_inb(ahc, SCB_DATAPTR + 2) << 16
   4678 				      | ahc_inb(ahc, SCB_DATAPTR + 3) << 24);
   4679 	printf("SCB_DATACNT == 0x%x\n", ahc_inb(ahc, SCB_DATACNT)
   4680 				      | ahc_inb(ahc, SCB_DATACNT + 1) << 8
   4681 				      | ahc_inb(ahc, SCB_DATACNT + 2) << 16);
   4682 	printf("SCB_SGCOUNT == 0x%x\n", ahc_inb(ahc, SCB_SGCOUNT));
   4683 	printf("CCSCBCTL == 0x%x\n", ahc_inb(ahc, CCSCBCTL));
   4684 	printf("CCSCBCNT == 0x%x\n", ahc_inb(ahc, CCSCBCNT));
   4685 	printf("DFCNTRL == 0x%x\n", ahc_inb(ahc, DFCNTRL));
   4686 	printf("DFSTATUS == 0x%x\n", ahc_inb(ahc, DFSTATUS));
   4687 	printf("CCHCNT == 0x%x\n", ahc_inb(ahc, CCHCNT));
   4688 	if (scb->sg_count > 0) {
   4689 		for (i = 0; i < scb->sg_count; i++) {
   4690 			printf("sg[%d] - Addr 0x%x : Length %d\n",
   4691 			       i,
   4692 			       le32toh(scb->sg_list[i].addr),
   4693 			       le32toh(scb->sg_list[i].len));
   4694 		}
   4695 	}
   4696 #endif
   4697 	if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
   4698 		/*
   4699 		 * Been down this road before.
   4700 		 * Do a full bus reset.
   4701 		 */
   4702 bus_reset:
   4703 		ahcsetccbstatus(scb->xs, XS_TIMEOUT);
   4704 		found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
   4705 		printf("%s: Issued Channel %c Bus Reset. "
   4706 		       "%d SCBs aborted\n", ahc_name(ahc), channel, found);
   4707 	} else {
   4708 		/*
   4709 		 * If we are a target, transition to bus free and report
   4710 		 * the timeout.
   4711 		 *
   4712 		 * The target/initiator that is holding up the bus may not
   4713 		 * be the same as the one that triggered this timeout
   4714 		 * (different commands have different timeout lengths).
   4715 		 * If the bus is idle and we are actiing as the initiator
   4716 		 * for this request, queue a BDR message to the timed out
   4717 		 * target.  Otherwise, if the timed out transaction is
   4718 		 * active:
   4719 		 *   Initiator transaction:
   4720 		 *	Stuff the message buffer with a BDR message and assert
   4721 		 *	ATN in the hopes that the target will let go of the bus
   4722 		 *	and go to the mesgout phase.  If this fails, we'll
   4723 		 *	get another timeout 2 seconds later which will attempt
   4724 		 *	a bus reset.
   4725 		 *
   4726 		 *   Target transaction:
   4727 		 *	Transition to BUS FREE and report the error.
   4728 		 *	It's good to be the target!
   4729 		 */
   4730 		u_int active_scb_index;
   4731 
   4732 		active_scb_index = ahc_inb(ahc, SCB_TAG);
   4733 
   4734 		if (last_phase != P_BUSFREE
   4735 		  && (active_scb_index < ahc->scb_data->numscbs)) {
   4736 			struct scb *active_scb;
   4737 
   4738 			/*
   4739 			 * If the active SCB is not from our device,
   4740 			 * assume that another device is hogging the bus
   4741 			 * and wait for it's timeout to expire before
   4742 			 * taking additional action.
   4743 			 */
   4744 			active_scb = &ahc->scb_data->scbarray[active_scb_index];
   4745 			if (active_scb->hscb->tcl != scb->hscb->tcl) {
   4746 				u_int	newtimeout;
   4747 
   4748 				scsi_print_addr(scb->xs->sc_link);
   4749 				printf("Other SCB Timeout%s",
   4750 			 	       (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
   4751 				       ? " again\n" : "\n");
   4752 				scb->flags |= SCB_OTHERTCL_TIMEOUT;
   4753 				newtimeout = MAX(active_scb->xs->timeout,
   4754 						 scb->xs->timeout);
   4755 				callout_reset(&scb->xs->xs_callout,
   4756 				    (newtimeout * hz) / 1000,
   4757 				    ahc_timeout, scb);
   4758 				splx(s);
   4759 				return;
   4760 			}
   4761 
   4762 			/* It's us */
   4763 			if ((scb->hscb->control & TARGET_SCB) != 0) {
   4764 
   4765 				/*
   4766 				 * Send back any queued up transactions
   4767 				 * and properly record the error condition.
   4768 				 */
   4769 				ahc_freeze_devq(ahc, scb->xs->sc_link);
   4770 				ahcsetccbstatus(scb->xs, XS_TIMEOUT);
   4771 				ahc_freeze_ccb(scb);
   4772 				ahc_done(ahc, scb);
   4773 
   4774 				/* Will clear us from the bus */
   4775 				restart_sequencer(ahc);
   4776 				return;
   4777 			}
   4778 
   4779 			ahc_set_recoveryscb(ahc, active_scb);
   4780 			ahc_outb(ahc, MSG_OUT, MSG_BUS_DEV_RESET);
   4781 			ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
   4782 			scsi_print_addr(active_scb->xs->sc_link);
   4783 			printf("BDR message in message buffer\n");
   4784 			active_scb->flags |=  SCB_DEVICE_RESET;
   4785 			callout_reset(&active_scb->xs->xs_callout,
   4786 			    2 * hz, ahc_timeout, active_scb);
   4787 			unpause_sequencer(ahc);
   4788 		} else {
   4789 			int	 disconnected;
   4790 
   4791 			/* XXX Shouldn't panic.  Just punt instead */
   4792 			if ((scb->hscb->control & TARGET_SCB) != 0)
   4793 				panic("Timed-out target SCB but bus idle");
   4794 
   4795 			if (last_phase != P_BUSFREE
   4796 			 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
   4797 				/* XXX What happened to the SCB? */
   4798 				/* Hung target selection.  Goto busfree */
   4799 				printf("%s: Hung target selection\n",
   4800 				       ahc_name(ahc));
   4801 				restart_sequencer(ahc);
   4802 				return;
   4803 			}
   4804 
   4805 			if (ahc_search_qinfifo(ahc, target, channel, lun,
   4806 					       scb->hscb->tag, ROLE_INITIATOR,
   4807 					       /*status*/0, SEARCH_COUNT) > 0) {
   4808 				disconnected = FALSE;
   4809 			} else {
   4810 				disconnected = TRUE;
   4811 			}
   4812 
   4813 			if (disconnected) {
   4814 				u_int active_scb;
   4815 
   4816 				ahc_set_recoveryscb(ahc, scb);
   4817 				/*
   4818 				 * Simply set the MK_MESSAGE control bit.
   4819 				 */
   4820 				scb->hscb->control |= MK_MESSAGE;
   4821 				scb->flags |= SCB_QUEUED_MSG
   4822 					   |  SCB_DEVICE_RESET;
   4823 
   4824 				/*
   4825 				 * Mark the cached copy of this SCB in the
   4826 				 * disconnected list too, so that a reconnect
   4827 				 * at this point causes a BDR or abort.
   4828 				 */
   4829 				active_scb = ahc_inb(ahc, SCBPTR);
   4830 				if (ahc_search_disc_list(ahc, target,
   4831 							 channel, lun,
   4832 							 scb->hscb->tag,
   4833 							 /*stop_on_first*/TRUE,
   4834 							 /*remove*/FALSE,
   4835 							 /*save_state*/FALSE)) {
   4836 					u_int scb_control;
   4837 
   4838 					scb_control = ahc_inb(ahc, SCB_CONTROL);
   4839 					scb_control |= MK_MESSAGE;
   4840 					ahc_outb(ahc, SCB_CONTROL, scb_control);
   4841 				}
   4842 				ahc_outb(ahc, SCBPTR, active_scb);
   4843 				ahc_index_busy_tcl(ahc, scb->hscb->tcl,
   4844 						   /*unbusy*/TRUE);
   4845 
   4846 				/*
   4847 				 * Actually re-queue this SCB in case we can
   4848 				 * select the device before it reconnects.
   4849 				 * Clear out any entries in the QINFIFO first
   4850 				 * so we are the next SCB for this target
   4851 				 * to run.
   4852 				 */
   4853 				ahc_search_qinfifo(ahc, SCB_TARGET(scb),
   4854 						   channel, SCB_LUN(scb),
   4855 						   SCB_LIST_NULL,
   4856 						   ROLE_INITIATOR,
   4857 						   SCB_REQUEUE,
   4858 						   SEARCH_COMPLETE);
   4859 				scsi_print_addr(scb->xs->sc_link);
   4860 				printf("Queuing a BDR SCB\n");
   4861 				ahc->qinfifo[ahc->qinfifonext++] =
   4862 				    scb->hscb->tag;
   4863 
   4864 				bus_dmamap_sync(ahc->parent_dmat,
   4865 				    ahc->shared_data_dmamap,
   4866 				    QINFIFO_OFFSET * 256, 256,
   4867 				    BUS_DMASYNC_PREWRITE);
   4868 
   4869 				if ((ahc->features & AHC_QUEUE_REGS) != 0) {
   4870 					ahc_outb(ahc, HNSCB_QOFF,
   4871 						 ahc->qinfifonext);
   4872 				} else {
   4873 					ahc_outb(ahc, KERNEL_QINPOS,
   4874 						 ahc->qinfifonext);
   4875 				}
   4876 				callout_reset(&scb->xs->xs_callout, 2 * hz,
   4877 				    ahc_timeout, scb);
   4878 				unpause_sequencer(ahc);
   4879 			} else {
   4880 				/* Go "immediatly" to the bus reset */
   4881 				/* This shouldn't happen */
   4882 				ahc_set_recoveryscb(ahc, scb);
   4883 				scsi_print_addr(scb->xs->sc_link);
   4884 				printf("SCB %x: Immediate reset.  "
   4885 					"Flags = 0x%x\n", scb->hscb->tag,
   4886 					scb->flags);
   4887 				goto bus_reset;
   4888 			}
   4889 		}
   4890 	}
   4891 	splx(s);
   4892 }
   4893 
   4894 static int
   4895 ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
   4896 		   int lun, u_int tag, role_t role, scb_flag status,
   4897 		   ahc_search_action action)
   4898 {
   4899 	struct	 scb *scbp;
   4900 	u_int8_t qinpos;
   4901 	u_int8_t qintail;
   4902 	int	 found;
   4903 
   4904 	qinpos = ahc_inb(ahc, QINPOS);
   4905 	qintail = ahc->qinfifonext;
   4906 	found = 0;
   4907 
   4908 	/*
   4909 	 * Start with an empty queue.  Entries that are not chosen
   4910 	 * for removal will be re-added to the queue as we go.
   4911 	 */
   4912 	ahc->qinfifonext = qinpos;
   4913 
   4914 	bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
   4915 	    QINFIFO_OFFSET * 256, 256, BUS_DMASYNC_POSTREAD);
   4916 
   4917 	while (qinpos != qintail) {
   4918 		scbp = &ahc->scb_data->scbarray[ahc->qinfifo[qinpos]];
   4919 		if (ahc_match_scb(scbp, target, channel, lun, tag, role)) {
   4920 			/*
   4921 			 * We found an scb that needs to be removed.
   4922 			 */
   4923 			switch (action) {
   4924 			case SEARCH_COMPLETE:
   4925 				if (!(scbp->xs->xs_status & XS_STS_DONE)) {
   4926 					scbp->flags |= status;
   4927 					scbp->xs->error = XS_NOERROR;
   4928 				}
   4929 				ahc_freeze_ccb(scbp);
   4930 				ahc_done(ahc, scbp);
   4931 				break;
   4932 			case SEARCH_COUNT:
   4933 				ahc->qinfifo[ahc->qinfifonext++] =
   4934 				    scbp->hscb->tag;
   4935 				break;
   4936 			case SEARCH_REMOVE:
   4937 				break;
   4938 			}
   4939 			found++;
   4940 		} else {
   4941 			ahc->qinfifo[ahc->qinfifonext++] = scbp->hscb->tag;
   4942 		}
   4943 		qinpos++;
   4944 	}
   4945 
   4946 	bus_dmamap_sync(ahc->parent_dmat, ahc->shared_data_dmamap,
   4947 	    QINFIFO_OFFSET * 256, 256, BUS_DMASYNC_PREWRITE);
   4948 
   4949 	if ((ahc->features & AHC_QUEUE_REGS) != 0) {
   4950 		ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
   4951 	} else {
   4952 		ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
   4953 	}
   4954 
   4955 	return (found);
   4956 }
   4957 
   4958 /*
   4959  * Abort all SCBs that match the given description (target/channel/lun/tag),
   4960  * setting their status to the passed in status if the status has not already
   4961  * been modified from CAM_REQ_INPROG.  This routine assumes that the sequencer
   4962  * is paused before it is called.
   4963  */
   4964 static int
   4965 ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
   4966 	       int lun, u_int tag, role_t role, int status)
   4967 {
   4968 	struct	scb *scbp;
   4969 	u_int	active_scb;
   4970 	int	i;
   4971 	int	found;
   4972 
   4973 	/* restore this when we're done */
   4974 	active_scb = ahc_inb(ahc, SCBPTR);
   4975 
   4976 	found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
   4977 				   role, SCB_REQUEUE, SEARCH_COMPLETE);
   4978 
   4979 	/*
   4980 	 * Search waiting for selection list.
   4981 	 */
   4982 	{
   4983 		u_int8_t next, prev;
   4984 
   4985 		next = ahc_inb(ahc, WAITING_SCBH);  /* Start at head of list. */
   4986 		prev = SCB_LIST_NULL;
   4987 
   4988 		while (next != SCB_LIST_NULL) {
   4989 			u_int8_t scb_index;
   4990 
   4991 			ahc_outb(ahc, SCBPTR, next);
   4992 			scb_index = ahc_inb(ahc, SCB_TAG);
   4993 			if (scb_index >= ahc->scb_data->numscbs) {
   4994 				panic("Waiting List inconsistency. "
   4995 				      "SCB index == %d, yet numscbs == %d.",
   4996 				      scb_index, ahc->scb_data->numscbs);
   4997 			}
   4998 			scbp = &ahc->scb_data->scbarray[scb_index];
   4999 			if (ahc_match_scb(scbp, target, channel,
   5000 					  lun, SCB_LIST_NULL, role)) {
   5001 
   5002 				next = ahc_abort_wscb(ahc, next, prev);
   5003 			} else {
   5004 
   5005 				prev = next;
   5006 				next = ahc_inb(ahc, SCB_NEXT);
   5007 			}
   5008 		}
   5009 	}
   5010 	/*
   5011 	 * Go through the disconnected list and remove any entries we
   5012 	 * have queued for completion, 0'ing their control byte too.
   5013 	 * We save the active SCB and restore it ourselves, so there
   5014 	 * is no reason for this search to restore it too.
   5015 	 */
   5016 	ahc_search_disc_list(ahc, target, channel, lun, tag,
   5017 			     /*stop_on_first*/FALSE, /*remove*/TRUE,
   5018 			     /*save_state*/FALSE);
   5019 
   5020 	/*
   5021 	 * Go through the hardware SCB array looking for commands that
   5022 	 * were active but not on any list.
   5023 	 */
   5024 	for(i = 0; i < ahc->scb_data->maxhscbs; i++) {
   5025 		u_int scbid;
   5026 
   5027 		ahc_outb(ahc, SCBPTR, i);
   5028 		scbid = ahc_inb(ahc, SCB_TAG);
   5029 		scbp = &ahc->scb_data->scbarray[scbid];
   5030 		if (scbid < ahc->scb_data->numscbs
   5031 		 && ahc_match_scb(scbp, target, channel, lun, tag, role))
   5032 			ahc_add_curscb_to_free_list(ahc);
   5033 	}
   5034 
   5035 	/*
   5036 	 * Go through the pending CCB list and look for
   5037 	 * commands for this target that are still active.
   5038 	 * These are other tagged commands that were
   5039 	 * disconnected when the reset occured.
   5040 	 */
   5041 	{
   5042 		struct scb *scb;
   5043 
   5044 		scb = ahc->pending_ccbs.lh_first;
   5045 		while (scb != NULL) {
   5046 			scbp = scb;
   5047 			scb = scb->plinks.le_next;
   5048 			if (ahc_match_scb(scbp, target, channel,
   5049 					  lun, tag, role)) {
   5050 				if (!(scbp->xs->xs_status & XS_STS_DONE))
   5051 					ahcsetccbstatus(scbp->xs, status);
   5052 				ahc_freeze_ccb(scbp);
   5053 				ahc_done(ahc, scbp);
   5054 				found++;
   5055 			}
   5056 		}
   5057 	}
   5058 	ahc_outb(ahc, SCBPTR, active_scb);
   5059 	return found;
   5060 }
   5061 
   5062 static int
   5063 ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
   5064 		     int lun, u_int tag, int stop_on_first, int remove,
   5065 		     int save_state)
   5066 {
   5067 	struct	scb *scbp;
   5068 	u_int	next;
   5069 	u_int	prev;
   5070 	u_int	count;
   5071 	u_int	active_scb;
   5072 
   5073 	count = 0;
   5074 	next = ahc_inb(ahc, DISCONNECTED_SCBH);
   5075 	prev = SCB_LIST_NULL;
   5076 
   5077 	if (save_state) {
   5078 		/* restore this when we're done */
   5079 		active_scb = ahc_inb(ahc, SCBPTR);
   5080 	} else
   5081 		/* Silence compiler */
   5082 		active_scb = SCB_LIST_NULL;
   5083 
   5084 	while (next != SCB_LIST_NULL) {
   5085 		u_int scb_index;
   5086 
   5087 		ahc_outb(ahc, SCBPTR, next);
   5088 		scb_index = ahc_inb(ahc, SCB_TAG);
   5089 		if (scb_index >= ahc->scb_data->numscbs) {
   5090 			panic("Disconnected List inconsistency. "
   5091 			      "SCB index == %d, yet numscbs == %d.",
   5092 			      scb_index, ahc->scb_data->numscbs);
   5093 		}
   5094 		scbp = &ahc->scb_data->scbarray[scb_index];
   5095 		if (ahc_match_scb(scbp, target, channel, lun,
   5096 				  tag, ROLE_INITIATOR)) {
   5097 			count++;
   5098 			if (remove) {
   5099 				next =
   5100 				    ahc_rem_scb_from_disc_list(ahc, prev, next);
   5101 			} else {
   5102 				prev = next;
   5103 				next = ahc_inb(ahc, SCB_NEXT);
   5104 			}
   5105 			if (stop_on_first)
   5106 				break;
   5107 		} else {
   5108 			prev = next;
   5109 			next = ahc_inb(ahc, SCB_NEXT);
   5110 		}
   5111 	}
   5112 	if (save_state)
   5113 		ahc_outb(ahc, SCBPTR, active_scb);
   5114 	return (count);
   5115 }
   5116 
   5117 static u_int
   5118 ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
   5119 {
   5120 	u_int next;
   5121 
   5122 	ahc_outb(ahc, SCBPTR, scbptr);
   5123 	next = ahc_inb(ahc, SCB_NEXT);
   5124 
   5125 	ahc_outb(ahc, SCB_CONTROL, 0);
   5126 
   5127 	ahc_add_curscb_to_free_list(ahc);
   5128 
   5129 	if (prev != SCB_LIST_NULL) {
   5130 		ahc_outb(ahc, SCBPTR, prev);
   5131 		ahc_outb(ahc, SCB_NEXT, next);
   5132 	} else
   5133 		ahc_outb(ahc, DISCONNECTED_SCBH, next);
   5134 
   5135 	return (next);
   5136 }
   5137 
   5138 static void
   5139 ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
   5140 {
   5141 	/* Invalidate the tag so that ahc_find_scb doesn't think it's active */
   5142 	ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
   5143 
   5144 	ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
   5145 	ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
   5146 }
   5147 
   5148 /*
   5149  * Manipulate the waiting for selection list and return the
   5150  * scb that follows the one that we remove.
   5151  */
   5152 static u_int
   5153 ahc_abort_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
   5154 {
   5155 	u_int curscb, next;
   5156 
   5157 	/*
   5158 	 * Select the SCB we want to abort and
   5159 	 * pull the next pointer out of it.
   5160 	 */
   5161 	curscb = ahc_inb(ahc, SCBPTR);
   5162 	ahc_outb(ahc, SCBPTR, scbpos);
   5163 	next = ahc_inb(ahc, SCB_NEXT);
   5164 
   5165 	/* Clear the necessary fields */
   5166 	ahc_outb(ahc, SCB_CONTROL, 0);
   5167 
   5168 	ahc_add_curscb_to_free_list(ahc);
   5169 
   5170 	/* update the waiting list */
   5171 	if (prev == SCB_LIST_NULL) {
   5172 		/* First in the list */
   5173 		ahc_outb(ahc, WAITING_SCBH, next);
   5174 
   5175 		/*
   5176 		 * Ensure we aren't attempting to perform
   5177 		 * selection for this entry.
   5178 		 */
   5179 		ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
   5180 	} else {
   5181 		/*
   5182 		 * Select the scb that pointed to us
   5183 		 * and update its next pointer.
   5184 		 */
   5185 		ahc_outb(ahc, SCBPTR, prev);
   5186 		ahc_outb(ahc, SCB_NEXT, next);
   5187 	}
   5188 
   5189 	/*
   5190 	 * Point us back at the original scb position.
   5191 	 */
   5192 	ahc_outb(ahc, SCBPTR, curscb);
   5193 	return next;
   5194 }
   5195 
   5196 static void
   5197 ahc_clear_intstat(struct ahc_softc *ahc)
   5198 {
   5199 	/* Clear any interrupt conditions this may have caused */
   5200 	ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO);
   5201 	ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
   5202 				|CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG|
   5203 				CLRREQINIT);
   5204 	ahc_outb(ahc, CLRINT, CLRSCSIINT);
   5205 }
   5206 
   5207 static void
   5208 ahc_reset_current_bus(struct ahc_softc *ahc)
   5209 {
   5210 	u_int8_t scsiseq;
   5211 
   5212 	ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
   5213 	scsiseq = ahc_inb(ahc, SCSISEQ);
   5214 	ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
   5215 	DELAY(AHC_BUSRESET_DELAY);
   5216 	/* Turn off the bus reset */
   5217 	ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
   5218 
   5219 	ahc_clear_intstat(ahc);
   5220 
   5221 	/* Re-enable reset interrupts */
   5222 	ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
   5223 }
   5224 
   5225 static int
   5226 ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
   5227 {
   5228 	u_int	initiator, target, max_scsiid;
   5229 	u_int	sblkctl;
   5230 	u_int	our_id;
   5231 	int	found;
   5232 	int	restart_needed;
   5233 	char	cur_channel;
   5234 
   5235 	ahc->pending_device = NULL;
   5236 
   5237 	pause_sequencer(ahc);
   5238 
   5239 	/*
   5240 	 * Run our command complete fifos to ensure that we perform
   5241 	 * completion processing on any commands that 'completed'
   5242 	 * before the reset occurred.
   5243 	 */
   5244 	ahc_run_qoutfifo(ahc);
   5245 
   5246 	/*
   5247 	 * Reset the bus if we are initiating this reset
   5248 	 */
   5249 	sblkctl = ahc_inb(ahc, SBLKCTL);
   5250 	cur_channel = 'A';
   5251 	if ((ahc->features & AHC_TWIN) != 0
   5252 	 && ((sblkctl & SELBUSB) != 0))
   5253 	    cur_channel = 'B';
   5254 	if (cur_channel != channel) {
   5255 		/* Case 1: Command for another bus is active
   5256 		 * Stealthily reset the other bus without
   5257 		 * upsetting the current bus.
   5258 		 */
   5259 		ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
   5260 		ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
   5261 		ahc_outb(ahc, SCSISEQ,
   5262 			 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
   5263 		if (initiate_reset)
   5264 			ahc_reset_current_bus(ahc);
   5265 		ahc_clear_intstat(ahc);
   5266 		ahc_outb(ahc, SBLKCTL, sblkctl);
   5267 		restart_needed = FALSE;
   5268 	} else {
   5269 		/* Case 2: A command from this bus is active or we're idle */
   5270 		ahc_clear_msg_state(ahc);
   5271 		ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
   5272 		ahc_outb(ahc, SCSISEQ,
   5273 			 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
   5274 		if (initiate_reset)
   5275 			ahc_reset_current_bus(ahc);
   5276 		ahc_clear_intstat(ahc);
   5277 
   5278 		/*
   5279 		 * Since we are going to restart the sequencer, avoid
   5280 		 * a race in the sequencer that could cause corruption
   5281 		 * of our Q pointers by starting over from index 0.
   5282 		 */
   5283 		ahc->qoutfifonext = 0;
   5284 		if ((ahc->features & AHC_QUEUE_REGS) != 0)
   5285 			ahc_outb(ahc, SDSCB_QOFF, 0);
   5286 		else
   5287 			ahc_outb(ahc, QOUTPOS, 0);
   5288 		restart_needed = TRUE;
   5289 	}
   5290 
   5291 	/*
   5292 	 * Clean up all the state information for the
   5293 	 * pending transactions on this bus.
   5294 	 */
   5295 	found = ahc_abort_scbs(ahc, AHC_TARGET_WILDCARD, channel,
   5296 			       AHC_LUN_WILDCARD, SCB_LIST_NULL,
   5297 			       ROLE_UNKNOWN, XS_RESET);
   5298 	if (channel == 'B') {
   5299 		our_id = ahc->our_id_b;
   5300 	} else {
   5301 		our_id = ahc->our_id;
   5302 	}
   5303 
   5304 	max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
   5305 
   5306 	/*
   5307 	 * Revert to async/narrow transfers until we renegotiate.
   5308 	 */
   5309 	for (target = 0; target <= max_scsiid; target++) {
   5310 
   5311 		if (ahc->enabled_targets[target] == NULL)
   5312 			continue;
   5313 		for (initiator = 0; initiator <= max_scsiid; initiator++) {
   5314 			struct ahc_devinfo devinfo;
   5315 
   5316 			ahc_compile_devinfo(&devinfo, target, initiator,
   5317 					    AHC_LUN_WILDCARD,
   5318 					    channel, ROLE_UNKNOWN);
   5319 			ahc_set_width(ahc, &devinfo,
   5320 				      MSG_EXT_WDTR_BUS_8_BIT,
   5321 				      AHC_TRANS_CUR, /*paused*/TRUE, FALSE);
   5322 			ahc_set_syncrate(ahc, &devinfo,
   5323 					 /*syncrate*/NULL, /*period*/0,
   5324 					 /*offset*/0, AHC_TRANS_CUR,
   5325 					 /*paused*/TRUE, FALSE);
   5326 		}
   5327 	}
   5328 
   5329 	if (restart_needed)
   5330 		restart_sequencer(ahc);
   5331 	else
   5332 		unpause_sequencer(ahc);
   5333 	return found;
   5334 }
   5335 
   5336 static int
   5337 ahc_match_scb(struct scb *scb, int target, char channel,
   5338 	      int lun, u_int tag, role_t role)
   5339 {
   5340 	int targ = SCB_TARGET(scb);
   5341 	char chan = SCB_CHANNEL(scb);
   5342 	int slun = SCB_LUN(scb);
   5343 	int match;
   5344 
   5345 	match = ((chan == channel) || (channel == ALL_CHANNELS));
   5346 	if (match != 0)
   5347 		match = ((targ == target) || (target == AHC_TARGET_WILDCARD));
   5348 	if (match != 0)
   5349 		match = ((lun == slun) || (lun == AHC_LUN_WILDCARD));
   5350 
   5351 	return match;
   5352 }
   5353 
   5354 static void
   5355 ahc_construct_sdtr(struct ahc_softc *ahc, u_int period, u_int offset)
   5356 {
   5357 	ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
   5358 	ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR_LEN;
   5359 	ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR;
   5360 	ahc->msgout_buf[ahc->msgout_index++] = period;
   5361 	ahc->msgout_buf[ahc->msgout_index++] = offset;
   5362 	ahc->msgout_len += 5;
   5363 }
   5364 
   5365 static void
   5366 ahc_construct_wdtr(struct ahc_softc *ahc, u_int bus_width)
   5367 {
   5368 	ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
   5369 	ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR_LEN;
   5370 	ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR;
   5371 	ahc->msgout_buf[ahc->msgout_index++] = bus_width;
   5372 	ahc->msgout_len += 4;
   5373 }
   5374 
   5375 static void
   5376 ahc_calc_residual(struct scb *scb)
   5377 {
   5378 	struct	hardware_scb *hscb;
   5379 
   5380 	hscb = scb->hscb;
   5381 
   5382 	/*
   5383 	 * If the disconnected flag is still set, this is bogus
   5384 	 * residual information left over from a sequencer
   5385 	 * pagin/pageout, so ignore this case.
   5386 	 */
   5387 	if ((scb->hscb->control & DISCONNECTED) == 0) {
   5388 		u_int32_t resid;
   5389 		int	  resid_sgs;
   5390 		int	  sg;
   5391 
   5392 		/*
   5393 		 * Remainder of the SG where the transfer
   5394 		 * stopped.
   5395 		 */
   5396 		resid = (hscb->residual_data_count[2] << 16)
   5397 		      |	(hscb->residual_data_count[1] <<8)
   5398 		      |	(hscb->residual_data_count[0]);
   5399 
   5400 		/*
   5401 		 * Add up the contents of all residual
   5402 		 * SG segments that are after the SG where
   5403 		 * the transfer stopped.
   5404 		 */
   5405 		resid_sgs = scb->hscb->residual_SG_count - 1/*current*/;
   5406 		sg = scb->sg_count - resid_sgs;
   5407 		while (resid_sgs > 0) {
   5408 
   5409 			resid += le32toh(scb->sg_list[sg].len);
   5410 			sg++;
   5411 			resid_sgs--;
   5412 		}
   5413 		scb->xs->resid = resid;
   5414 	}
   5415 
   5416 	/*
   5417 	 * Clean out the residual information in this SCB for its
   5418 	 * next consumer.
   5419 	 */
   5420 	hscb->residual_SG_count = 0;
   5421 
   5422 #ifdef AHC_DEBUG
   5423 	if (ahc_debug & AHC_SHOWMISC) {
   5424 		scsi_print_addr(scb->xs->sc_link);
   5425 		printf("Handled Residual of %ld bytes\n" ,(long)scb->xs->resid);
   5426 	}
   5427 #endif
   5428 }
   5429 
   5430 static void
   5431 ahc_update_pending_syncrates(struct ahc_softc *ahc)
   5432 {
   5433 	struct	scb *scb;
   5434 	int	pending_ccb_count;
   5435 	int	i;
   5436 	u_int	saved_scbptr;
   5437 
   5438 	/*
   5439 	 * Traverse the pending SCB list and ensure that all of the
   5440 	 * SCBs there have the proper settings.
   5441 	 */
   5442 	scb = LIST_FIRST(&ahc->pending_ccbs);
   5443 	pending_ccb_count = 0;
   5444 	while (scb != NULL) {
   5445 		struct ahc_devinfo devinfo;
   5446 		struct scsipi_xfer *xs;
   5447 		struct scb *pending_scb;
   5448 		struct hardware_scb *pending_hscb;
   5449 		struct ahc_initiator_tinfo *tinfo;
   5450 		struct tmode_tstate *tstate;
   5451 		u_int  our_id, remote_id;
   5452 
   5453 		xs = scb->xs;
   5454 		pending_scb = scb;
   5455 		pending_hscb = pending_scb->hscb;
   5456 		our_id = SCB_IS_SCSIBUS_B(pending_scb)
   5457 		       ? ahc->our_id_b : ahc->our_id;
   5458 		remote_id = xs->sc_link->scsipi_scsi.target;
   5459 		ahc_compile_devinfo(&devinfo, our_id, remote_id,
   5460 				    SCB_LUN(pending_scb),
   5461 				    SCB_CHANNEL(pending_scb),
   5462 				    ROLE_UNKNOWN);
   5463 		tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
   5464 					    our_id, remote_id, &tstate);
   5465 		pending_hscb->control &= ~ULTRAENB;
   5466 		if ((tstate->ultraenb & devinfo.target_mask) != 0)
   5467 			pending_hscb->control |= ULTRAENB;
   5468 		pending_hscb->scsirate = tinfo->scsirate;
   5469 		pending_hscb->scsioffset = tinfo->current.offset;
   5470 		pending_ccb_count++;
   5471 		scb = LIST_NEXT(scb, plinks);
   5472 	}
   5473 
   5474 	if (pending_ccb_count == 0)
   5475 		return;
   5476 
   5477 	saved_scbptr = ahc_inb(ahc, SCBPTR);
   5478 	/* Ensure that the hscbs down on the card match the new information */
   5479 	for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
   5480 		u_int scb_tag;
   5481 
   5482 		ahc_outb(ahc, SCBPTR, i);
   5483 		scb_tag = ahc_inb(ahc, SCB_TAG);
   5484 		if (scb_tag != SCB_LIST_NULL) {
   5485 			struct	ahc_devinfo devinfo;
   5486 			struct	scb *pending_scb;
   5487 			struct scsipi_xfer *xs;
   5488 			struct	hardware_scb *pending_hscb;
   5489 			struct	ahc_initiator_tinfo *tinfo;
   5490 			struct	tmode_tstate *tstate;
   5491 			u_int	our_id, remote_id;
   5492 			u_int	control;
   5493 
   5494 			pending_scb = &ahc->scb_data->scbarray[scb_tag];
   5495 			if (pending_scb->flags == SCB_FREE)
   5496 				continue;
   5497 			pending_hscb = pending_scb->hscb;
   5498 			xs = pending_scb->xs;
   5499 			our_id = SCB_IS_SCSIBUS_B(pending_scb)
   5500 			       ? ahc->our_id_b : ahc->our_id;
   5501 			remote_id = xs->sc_link->scsipi_scsi.target;
   5502 			ahc_compile_devinfo(&devinfo, our_id, remote_id,
   5503 					    SCB_LUN(pending_scb),
   5504 					    SCB_CHANNEL(pending_scb),
   5505 					    ROLE_UNKNOWN);
   5506 			tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
   5507 						    our_id, remote_id, &tstate);
   5508 			control = ahc_inb(ahc, SCB_CONTROL);
   5509 			control &= ~ULTRAENB;
   5510 			if ((tstate->ultraenb & devinfo.target_mask) != 0)
   5511 				control |= ULTRAENB;
   5512 			ahc_outb(ahc, SCB_CONTROL, control);
   5513 			ahc_outb(ahc, SCB_SCSIRATE, tinfo->scsirate);
   5514 			ahc_outb(ahc, SCB_SCSIOFFSET, tinfo->current.offset);
   5515 		}
   5516 	}
   5517 	ahc_outb(ahc, SCBPTR, saved_scbptr);
   5518 }
   5519 
   5520 #if UNUSED
   5521 static void
   5522 ahc_dump_targcmd(struct target_cmd *cmd)
   5523 {
   5524 	u_int8_t *byte;
   5525 	u_int8_t *last_byte;
   5526 	int i;
   5527 
   5528 	byte = &cmd->initiator_channel;
   5529 	/* Debugging info for received commands */
   5530 	last_byte = &cmd[1].initiator_channel;
   5531 
   5532 	i = 0;
   5533 	while (byte < last_byte) {
   5534 		if (i == 0)
   5535 			printf("\t");
   5536 		printf("%#x", *byte++);
   5537 		i++;
   5538 		if (i == 8) {
   5539 			printf("\n");
   5540 			i = 0;
   5541 		} else {
   5542 			printf(", ");
   5543 		}
   5544 	}
   5545 }
   5546 #endif
   5547 
   5548 static void
   5549 ahc_shutdown(void *arg)
   5550 {
   5551 	struct	ahc_softc *ahc;
   5552 	int	i;
   5553 	u_int	sxfrctl1_a, sxfrctl1_b;
   5554 
   5555 	ahc = (struct ahc_softc *)arg;
   5556 
   5557 	pause_sequencer(ahc);
   5558 
   5559 	/*
   5560 	 * Preserve the value of the SXFRCTL1 register for all channels.
   5561 	 * It contains settings that affect termination and we don't want
   5562 	 * to disturb the integrity of the bus during shutdown in case
   5563 	 * we are in a multi-initiator setup.
   5564 	 */
   5565 	sxfrctl1_b = 0;
   5566 	if ((ahc->features & AHC_TWIN) != 0) {
   5567 		u_int sblkctl;
   5568 
   5569 		sblkctl = ahc_inb(ahc, SBLKCTL);
   5570 		ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
   5571 		sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
   5572 		ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
   5573 	}
   5574 
   5575 	sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
   5576 
   5577 	/* This will reset most registers to 0, but not all */
   5578 	ahc_reset(ahc);
   5579 
   5580 	if ((ahc->features & AHC_TWIN) != 0) {
   5581 		u_int sblkctl;
   5582 
   5583 		sblkctl = ahc_inb(ahc, SBLKCTL);
   5584 		ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
   5585 		ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
   5586 		ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
   5587 	}
   5588 	ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
   5589 
   5590 	ahc_outb(ahc, SCSISEQ, 0);
   5591 	ahc_outb(ahc, SXFRCTL0, 0);
   5592 	ahc_outb(ahc, DSPCISTATUS, 0);
   5593 
   5594 	for (i = TARG_SCSIRATE; i < HA_274_BIOSCTRL; i++)
   5595 		ahc_outb(ahc, i, 0);
   5596 }
   5597 
   5598 #if defined(AHC_DEBUG) && 0
   5599 static void
   5600 ahc_dumptinfo(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo)
   5601 {
   5602 	printf("%s: tinfo: rate %u\n", ahc_name(ahc), tinfo->scsirate);
   5603 
   5604 	printf("\tcurrent:\n");
   5605 	printf("\t\twidth %u period %u offset %u flags %x\n",
   5606 	    tinfo->current.width, tinfo->current.period,
   5607 	    tinfo->current.offset, tinfo->current.ppr_flags);
   5608 
   5609 	printf("\tgoal:\n");
   5610 	printf("\t\twidth %u period %u offset %u flags %x\n",
   5611 	    tinfo->goal.width, tinfo->goal.period,
   5612 	    tinfo->goal.offset, tinfo->goal.ppr_flags);
   5613 
   5614 	printf("\tuser:\n");
   5615 	printf("\t\twidth %u period %u offset %u flags %x\n",
   5616 	    tinfo->user.width, tinfo->user.period,
   5617 	    tinfo->user.offset, tinfo->user.ppr_flags);
   5618 }
   5619 #endif
   5620 
   5621 static void
   5622 ahc_check_tags(struct ahc_softc *ahc, struct scsipi_xfer *xs)
   5623 {
   5624 	struct scsipi_inquiry_data *inq;
   5625 	struct ahc_devinfo devinfo;
   5626 	struct tmode_tstate *tstate;
   5627 	int target_id, our_id;
   5628 	char channel;
   5629 
   5630 	if (xs->cmd->opcode != INQUIRY || xs->error != XS_NOERROR)
   5631 		return;
   5632 
   5633 	if (xs->sc_link->quirks & SDEV_NOTAG)
   5634 		return;
   5635 
   5636 	target_id = xs->sc_link->scsipi_scsi.target;
   5637 	our_id = SIM_SCSI_ID(ahc, xs->sc_link);
   5638 	channel = SIM_CHANNEL(ahc, xs->sc_link);
   5639 
   5640 	(void)ahc_fetch_transinfo(ahc, channel, our_id, target_id, &tstate);
   5641 	ahc_compile_devinfo(&devinfo, our_id, target_id,
   5642 	    xs->sc_link->scsipi_scsi.lun, channel, ROLE_INITIATOR);
   5643 
   5644 	if (tstate->tagdisable & devinfo.target_mask)
   5645 		return;
   5646 
   5647 	/*
   5648 	 * Sneak a look at the results of the SCSI Inquiry
   5649 	 * command and see if we can do Tagged queing.  This
   5650 	 * should really be done by the higher level drivers.
   5651 	 */
   5652 	inq = (struct scsipi_inquiry_data *)xs->data;
   5653 	if ((inq->flags3 & SID_CmdQue) && !(ahc_istagged_device(ahc, xs, 1))) {
   5654 	        printf("%s: target %d using tagged queuing\n",
   5655 			ahc_name(ahc), xs->sc_link->scsipi_scsi.target);
   5656 
   5657 		ahc_set_tags(ahc, &devinfo, TRUE);
   5658 
   5659 		if (ahc->scb_data->maxhscbs >= 16 ||
   5660 		    (ahc->flags & AHC_PAGESCBS)) {
   5661 			/* Default to 16 tags */
   5662 			xs->sc_link->openings = 16;
   5663 		} else {
   5664 			/*
   5665 			 * Default to 4 tags on whimpy
   5666 			 * cards that don't have much SCB
   5667 			 * space and can't page.  This prevents
   5668 			 * a single device from hogging all
   5669 			 * slots.  We should really have a better
   5670 			 * way of providing fairness.
   5671 			 */
   5672 			xs->sc_link->openings = 4;
   5673 		}
   5674 	}
   5675 }
   5676 
   5677 static int
   5678 ahc_istagged_device(struct ahc_softc *ahc, struct scsipi_xfer *xs,
   5679 		    int nocmdcheck)
   5680 {
   5681 	char channel;
   5682 	u_int our_id, target;
   5683 	struct tmode_tstate *tstate;
   5684 	struct ahc_devinfo devinfo;
   5685 
   5686 	if (xs->sc_link->quirks & SDEV_NOTAG)
   5687 		return 0;
   5688 
   5689 	/*
   5690 	 * XXX never do these commands with tags. Should really be
   5691 	 * in a higher layer.
   5692 	 */
   5693 	if (!nocmdcheck && (xs->cmd->opcode == INQUIRY ||
   5694 	     xs->cmd->opcode == TEST_UNIT_READY ||
   5695 	     xs->cmd->opcode == REQUEST_SENSE))
   5696 		return 0;
   5697 
   5698 	channel = SIM_CHANNEL(ahc, xs->sc_link);
   5699 	our_id = SIM_SCSI_ID(ahc, xs->sc_link);
   5700 	target = xs->sc_link->scsipi_scsi.target;
   5701 	(void)ahc_fetch_transinfo(ahc, channel, our_id, target, &tstate);
   5702 
   5703 	ahc_compile_devinfo(&devinfo, our_id, target,
   5704 	    xs->sc_link->scsipi_scsi.lun, channel, ROLE_INITIATOR);
   5705 
   5706 	return (tstate->tagenable & devinfo.target_mask);
   5707 }
   5708