Home | History | Annotate | Line # | Download | only in ic
aic7xxx.c revision 1.20
      1 /*	$NetBSD: aic7xxx.c,v 1.20 1997/02/20 05:15:50 mikel 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/aic7770.c	27/284X and aic7770 motherboard controllers
      7  * pci/aic7870.c	3940, 2940, aic7880, aic7870 and aic7850 controllers
      8  *
      9  * Copyright (c) 1994, 1995, 1996 Justin T. Gibbs.
     10  * All rights reserved.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice immediately at the beginning of the file, without modification,
     17  *    this list of conditions, and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  * from Id: aic7xxx.c,v 1.75 1996/06/23 20:02:37 gibbs Exp
     37  */
     38 /*
     39  * TODO:
     40  *	Implement Target Mode
     41  *
     42  * A few notes on how SCB paging works...
     43  *
     44  * SCB paging takes advantage of the fact that devices stay disconnected
     45  * from the bus a relatively long time and that while they're disconnected,
     46  * having the SCBs for that device down on the host adapter is of little use.
     47  * Instead we copy the SCB back up into kernel memory and reuse the SCB slot
     48  * on the card to schedule another transaction.  This can be a real payoff
     49  * when doing random I/O to tagged queueing devices since there are more
     50  * transactions active at once for the device to sort for optimal seek
     51  * reduction. The algorithm goes like this...
     52  *
     53  * At the sequencer level:
     54  * 1) Disconnected SCBs are threaded onto a doubly linked list, headed by
     55  *    DISCONNECTED_SCBH using the SCB_NEXT and SCB_PREV fields.  The most
     56  *    recently disconnected device is always at the head.
     57  *
     58  * 2) The SCB has an added field SCB_TAG that corresponds to the kernel
     59  *    SCB number (ie 0-254).
     60  *
     61  * 3) When a command is queued, the hardware index of the SCB it was downloaded
     62  *    into is placed into the QINFIFO for easy indexing by the sequencer.
     63  *
     64  * 4) The tag field is used as the tag for tagged-queueing, for determining
     65  *    the related kernel SCB, and is the value put into the QOUTFIFO
     66  *    so the kernel doesn't have to upload the SCB to determine the kernel SCB
     67  *    that completed on command completes.
     68  *
     69  * 5) When a reconnect occurs, the sequencer must scan the SCB array (even
     70  *    in the tag case) looking for the appropriate SCB and if it can't find
     71  *    it, it interrupts the kernel so it can page the SCB in.
     72  *
     73  * 6) If the sequencer is successful in finding the SCB, it removes it from
     74  *    the doubly linked list of disconnected SCBS.
     75  *
     76  * At the kernel level:
     77  * 1) There are four queues that a kernel SCB may reside on:
     78  *	free_scbs - SCBs that are not in use and have a hardware slot assigned
     79  *		    to them.
     80  *      page_scbs - SCBs that are not in use and need to have a hardware slot
     81  *		    assigned to them (i.e. they will most likely cause a page
     82  *		    out event).
     83  *	waiting_scbs - SCBs that are active, don't have an assigned hardware
     84  *		    slot assigned to them and are waiting for either a
     85  *		    disconnection or a command complete to free up a slot.
     86  *	assigned_scbs - SCBs that were in the waiting_scbs queue, but were
     87  *		    assigned a slot by ahc_free_scb.
     88  *
     89  * 2) When a new request comes in, an SCB is allocated from the free_scbs or
     90  *    page_scbs queue with preference to SCBs on the free_scbs queue.
     91  *
     92  * 3) If there are no free slots (we retrieved the SCB off of the page_scbs
     93  *    queue), the SCB is inserted onto the tail of the waiting_scbs list and
     94  *    we attempt to run this queue down.
     95  *
     96  * 4) ahc_run_waiing_queues() looks at both the assigned_scbs and waiting_scbs
     97  *    queues.  In the case of the assigned_scbs, the commands are immediately
     98  *    downloaded and started.  For waiting_scbs, we page in all that we can
     99  *    ensuring we don't create a resource deadlock (see comments in
    100  *    ahc_run_waing_queues()).
    101  *
    102  * 5) After we handle a bunch of command completes, we also try running the
    103  *    queues since many SCBs may have disconnected since the last command
    104  *    was started and we have at least one free slot on the card.
    105  *
    106  * 6) ahc_free_scb looks at the waiting_scbs queue for a transaction
    107  *    requiring a slot and moves it to the assigned_scbs queue if it
    108  *    finds one.  Otherwise it puts the current SCB onto the free_scbs
    109  *    queue for later use.
    110  *
    111  * 7) The driver handles page-in requests from the sequencer in response to
    112  *    the NO_MATCH sequencer interrupt.  For tagged commands, the approprite
    113  *    SCB is easily found since the tag is a direct index into our kernel SCB
    114  *    array.  For non-tagged commands, we keep a separate array of 16 pointers
    115  *    that point to the single possible SCB that was paged out for that target.
    116  */
    117 
    118 #include <sys/param.h>
    119 #include <sys/systm.h>
    120 #if defined(__NetBSD__)
    121 #include <sys/device.h>
    122 #include <machine/bus.h>
    123 #include <machine/intr.h>
    124 #endif /* defined(__NetBSD__) */
    125 
    126 #include <sys/malloc.h>
    127 #include <sys/buf.h>
    128 #include <sys/proc.h>
    129 
    130 #include <scsi/scsi_all.h>
    131 #include <scsi/scsi_message.h>
    132 #if defined(__NetBSD__)
    133 #include <scsi/scsi_debug.h>
    134 #endif
    135 #include <scsi/scsiconf.h>
    136 
    137 #if defined(__FreeBSD__)
    138 #include <machine/clock.h>
    139 #endif
    140 
    141 #include <vm/vm.h>
    142 #include <vm/vm_param.h>
    143 #include <vm/pmap.h>
    144 
    145 #if defined(__FreeBSD__)
    146 #include <i386/scsi/aic7xxx.h>
    147 
    148 #include <dev/aic7xxx/aic7xxx_reg.h>
    149 #endif /* defined(__FreeBSD__) */
    150 
    151 #if defined(__NetBSD__)
    152 #include <dev/ic/aic7xxxreg.h>
    153 #include <dev/ic/aic7xxxvar.h>
    154 
    155 #define bootverbose	1
    156 
    157 #define DEBUGTARG	DEBUGTARGET
    158 #if DEBUGTARG < 0	/* Negative numbrs for disabling cause warnings */
    159 #undef DEBUGTARG
    160 #define DEBUGTARG	17
    161 #endif
    162 #endif /* defined(__NetBSD__) */
    163 
    164 #include <sys/kernel.h>
    165 #define KVTOPHYS(x)   vtophys(x)
    166 
    167 #define MIN(a,b) ((a < b) ? a : b)
    168 #define ALL_TARGETS -1
    169 
    170 #if defined(__FreeBSD__)
    171 u_long ahc_unit = 0;
    172 #endif
    173 
    174 #ifdef AHC_DEBUG
    175 static int     ahc_debug = AHC_DEBUG;
    176 #endif
    177 
    178 #ifdef AHC_BROKEN_CACHE
    179 int ahc_broken_cache = 1;
    180 
    181 /*
    182  * "wbinvd" cause writing back whole cache (both CPU internal & external)
    183  * to memory, so that the instruction takes a lot of time.
    184  * This makes machine slow.
    185  */
    186 #define	INVALIDATE_CACHE()	__asm __volatile("wbinvd")
    187 #endif
    188 
    189 /**** bit definitions for SCSIDEF ****/
    190 #define	HSCSIID		0x07		/* our SCSI ID */
    191 #define HWSCSIID	0x0f		/* our SCSI ID if Wide Bus */
    192 
    193 static void	 ahcminphys __P((struct buf *bp));
    194 static int32_t	 ahc_scsi_cmd __P((struct scsi_xfer *xs));
    195 static inline void pause_sequencer __P((struct ahc_data *ahc));
    196 static inline void unpause_sequencer __P((struct ahc_data *ahc,
    197 					  int unpause_always));
    198 static inline void restart_sequencer __P((struct ahc_data *ahc));
    199 
    200 static struct scsi_adapter ahc_switch =
    201 {
    202         ahc_scsi_cmd,
    203         ahcminphys,
    204         0,
    205         0,
    206 #if defined(__FreeBSD__)
    207         0,
    208         "ahc",
    209         { 0, 0 }
    210 #endif
    211 };
    212 
    213 /* the below structure is so we have a default dev struct for our link struct */
    214 static struct scsi_device ahc_dev =
    215 {
    216     NULL,                       /* Use default error handler */
    217     NULL,                       /* have a queue, served by this */
    218     NULL,                       /* have no async handler */
    219     NULL,                       /* Use default 'done' routine */
    220 #if defined(__FreeBSD__)
    221     "ahc",
    222     0,
    223     { 0, 0 }
    224 #endif
    225 };
    226 
    227 static inline void
    228 pause_sequencer(ahc)
    229 	struct ahc_data *ahc;
    230 {
    231 	AHC_OUTB(ahc, HCNTRL, ahc->pause);
    232 
    233 	/*
    234 	 * Since the sequencer can disable pausing in a critical section, we
    235 	 * must loop until it actually stops.
    236 	 */
    237 	while ((AHC_INB(ahc, HCNTRL) & PAUSE) == 0)
    238 		;
    239 }
    240 
    241 static inline void
    242 unpause_sequencer(ahc, unpause_always)
    243 	struct ahc_data *ahc;
    244 	int unpause_always;
    245 {
    246 	if (unpause_always
    247 	 ||(AHC_INB(ahc, INTSTAT) & (SCSIINT | SEQINT | BRKADRINT)) == 0)
    248 		AHC_OUTB(ahc, HCNTRL, ahc->unpause);
    249 }
    250 
    251 /*
    252  * Restart the sequencer program from address zero
    253  */
    254 static inline void
    255 restart_sequencer(ahc)
    256 	struct ahc_data *ahc;
    257 {
    258 	do {
    259 		AHC_OUTB(ahc, SEQCTL, SEQRESET|FASTMODE);
    260 	} while((AHC_INB(ahc, SEQADDR0) != 0)
    261 		|| (AHC_INB(ahc, SEQADDR1) != 0));
    262 
    263 	unpause_sequencer(ahc, /*unpause_always*/TRUE);
    264 }
    265 
    266 #if defined(__NetBSD__)
    267 /*
    268  * Is device which is pointed by sc_link connected on second scsi bus ?
    269  */
    270 #define	IS_SCSIBUS_B(ahc, sc_link)	\
    271 	((sc_link)->scsibus == (ahc)->sc_link_b.scsibus)
    272 
    273 /*
    274  * convert FreeBSD's SCSI symbols to NetBSD's
    275  */
    276 #define	SCSI_NOMASK	SCSI_POLL
    277 #define	opennings	openings
    278 #endif
    279 
    280 static u_char	ahc_abort_wscb __P((struct ahc_data *ahc, struct scb *scbp,
    281 				    u_char prev,
    282 				    u_char timedout_scb, u_int32_t xs_error));
    283 static void	ahc_add_waiting_scb __P((struct ahc_data *ahc,
    284 					 struct scb *scb));
    285 static void	ahc_done __P((struct ahc_data *ahc, struct scb *scbp));
    286 static void	ahc_free_scb __P((struct ahc_data *ahc, struct scb *scb,
    287 				  int flags));
    288 static inline void ahc_send_scb __P((struct ahc_data *ahc, struct scb *scb));
    289 static inline void ahc_fetch_scb __P((struct ahc_data *ahc, struct scb *scb));
    290 static inline void ahc_page_scb __P((struct ahc_data *ahc, struct scb *out_scb,
    291 				struct scb *in_scb));
    292 static inline void ahc_run_waiting_queues __P((struct ahc_data *ahc));
    293 static void	ahc_handle_seqint __P((struct ahc_data *ahc, u_int8_t intstat));
    294 static struct scb *
    295 		ahc_get_scb __P((struct ahc_data *ahc, int flags));
    296 static void	ahc_loadseq __P((struct ahc_data *ahc));
    297 static int	ahc_match_scb __P((struct scb *scb, int target, char channel));
    298 static int	ahc_poll __P((struct ahc_data *ahc, int wait));
    299 #ifdef AHC_DEBUG
    300 static void	ahc_print_scb __P((struct scb *scb));
    301 #endif
    302 static int	ahc_reset_channel __P((struct ahc_data *ahc, char channel,
    303 				       u_char timedout_scb, u_int32_t xs_error,
    304 				       u_char initiate_reset));
    305 static int	ahc_reset_device __P((struct ahc_data *ahc, int target,
    306 				      char channel, u_char timedout_scb,
    307 				      u_int32_t xs_error));
    308 static void	ahc_reset_current_bus __P((struct ahc_data *ahc));
    309 static void	ahc_run_done_queue __P((struct ahc_data *ahc));
    310 static void	ahc_scsirate __P((struct ahc_data* ahc, u_int8_t *scsirate,
    311 				  u_int8_t *period, u_int8_t *offset,
    312 				  char channel, int target));
    313 #if defined(__FreeBSD__)
    314 static timeout_t
    315 		ahc_timeout;
    316 #elif defined(__NetBSD__)
    317 static void	ahc_timeout __P((void *));
    318 #endif
    319 static void	ahc_busy_target __P((struct ahc_data *ahc,
    320 				     int target, char channel));
    321 static void	ahc_unbusy_target __P((struct ahc_data *ahc,
    322 				       int target, char channel));
    323 static void	ahc_construct_sdtr __P((struct ahc_data *ahc, int start_byte,
    324 					u_int8_t period, u_int8_t offset));
    325 static void	ahc_construct_wdtr __P((struct ahc_data *ahc, int start_byte,
    326 					u_int8_t bus_width));
    327 
    328 #if defined(__NetBSD__)			/* XXX */
    329 static void	ahc_xxx_enqueue __P((struct ahc_data *ahc,
    330 		    struct scsi_xfer *xs, int infront));
    331 static struct scsi_xfer *ahc_xxx_dequeue __P((struct ahc_data *ahc));
    332 #endif
    333 
    334 #if defined(__FreeBSD__)
    335 
    336 char *ahc_name(ahc)
    337 	struct ahc_data *ahc;
    338 {
    339 	static char name[10];
    340 
    341 	sprintf(name, "ahc%d", ahc->unit);
    342 	return (name);
    343 }
    344 
    345 #elif defined(__NetBSD__)
    346 struct cfdriver ahc_cd = {
    347 	NULL, "ahc", DV_DULL
    348 };
    349 #endif
    350 
    351 #ifdef  AHC_DEBUG
    352 static void
    353 ahc_print_scb(scb)
    354         struct scb *scb;
    355 {
    356         printf("scb:%p control:0x%x tcl:0x%x cmdlen:%d cmdpointer:0x%lx\n"
    357 	    ,scb
    358 	    ,scb->control
    359 	    ,scb->tcl
    360 	    ,scb->cmdlen
    361 	    ,scb->cmdpointer );
    362         printf("        datlen:%d data:0x%lx segs:0x%x segp:0x%lx\n"
    363 	    ,scb->datalen
    364 	    ,scb->data
    365 	    ,scb->SG_segment_count
    366 	    ,scb->SG_list_pointer);
    367 	printf("	sg_addr:%lx sg_len:%ld\n"
    368 	    ,scb->ahc_dma[0].addr
    369 	    ,scb->ahc_dma[0].len);
    370 }
    371 
    372 #endif
    373 
    374 static struct {
    375         u_char errno;
    376 	char *errmesg;
    377 } hard_error[] = {
    378 	{ ILLHADDR,  "Illegal Host Access" },
    379 	{ ILLSADDR,  "Illegal Sequencer Address referrenced" },
    380 	{ ILLOPCODE, "Illegal Opcode in sequencer program" },
    381 	{ PARERR,    "Sequencer Ram Parity Error" }
    382 };
    383 
    384 
    385 /*
    386  * Valid SCSIRATE values.  (p. 3-17)
    387  * Provides a mapping of tranfer periods in ns to the proper value to
    388  * stick in the scsiscfr reg to use that transfer rate.
    389  */
    390 static struct {
    391 	short sxfr;
    392 	/* Rates in Ultra mode have bit 8 of sxfr set */
    393 #define		ULTRA_SXFR 0x100
    394 	int period; /* in ns/4 */
    395 	char *rate;
    396 } ahc_syncrates[] = {
    397 	{ 0x100, 12, "20.0"  },
    398 	{ 0x110, 15, "16.0"  },
    399 	{ 0x120, 18, "13.4"  },
    400 	{ 0x000, 25, "10.0"  },
    401 	{ 0x010, 31,  "8.0"  },
    402 	{ 0x020, 37,  "6.67" },
    403 	{ 0x030, 43,  "5.7"  },
    404 	{ 0x040, 50,  "5.0"  },
    405 	{ 0x050, 56,  "4.4"  },
    406 	{ 0x060, 62,  "4.0"  },
    407 	{ 0x070, 68,  "3.6"  }
    408 };
    409 
    410 static int ahc_num_syncrates =
    411 	sizeof(ahc_syncrates) / sizeof(ahc_syncrates[0]);
    412 
    413 /*
    414  * Allocate a controller structures for a new device and initialize it.
    415  * ahc_reset should be called before now since we assume that the card
    416  * is paused.
    417  */
    418 #if defined(__FreeBSD__)
    419 struct ahc_data *
    420 ahc_alloc(unit, iobase, type, flags)
    421 	int unit;
    422 	u_long iobase;
    423 #elif defined(__NetBSD__)
    424 void
    425 ahc_construct(ahc, iot, ioh, type, flags)
    426 	struct  ahc_data *ahc;
    427 	bus_space_tag_t iot;
    428 	bus_space_handle_t ioh;
    429 #endif
    430 	ahc_type type;
    431 	ahc_flag flags;
    432 {
    433 
    434 	/*
    435 	 * find unit and check we have that many defined
    436 	 */
    437 
    438 #if defined(__FreeBSD__)
    439 	struct  ahc_data *ahc;
    440 
    441 	/*
    442 	 * Allocate a storage area for us
    443 	 */
    444 
    445 	ahc = malloc(sizeof(struct ahc_data), M_TEMP, M_NOWAIT);
    446 	if (!ahc) {
    447 		printf("ahc%d: cannot malloc!\n", unit);
    448 		return NULL;
    449 	}
    450 	bzero(ahc, sizeof(struct ahc_data));
    451 #endif
    452 	STAILQ_INIT(&ahc->free_scbs);
    453 	STAILQ_INIT(&ahc->page_scbs);
    454 	STAILQ_INIT(&ahc->waiting_scbs);
    455 	STAILQ_INIT(&ahc->assigned_scbs);
    456 #if defined(__FreeBSD__)
    457 	ahc->unit = unit;
    458 #endif
    459 #if defined(__FreeBSD__)
    460 	ahc->baseport = iobase;
    461 #elif defined(__NetBSD__)
    462 	ahc->sc_iot = iot;
    463 	ahc->sc_ioh = ioh;
    464 #endif
    465 	ahc->type = type;
    466 	ahc->flags = flags;
    467 	ahc->unpause = (AHC_INB(ahc, HCNTRL) & IRQMS) | INTEN;
    468 	ahc->pause = ahc->unpause | PAUSE;
    469 
    470 #if defined(__FreeBSD__)
    471 	return (ahc);
    472 #endif
    473 }
    474 
    475 void
    476 ahc_free(ahc)
    477 	struct ahc_data *ahc;
    478 {
    479 #if defined(__FreeBSD__)
    480 	free(ahc, M_DEVBUF);
    481 	return;
    482 #endif
    483 }
    484 
    485 void
    486 #if defined(__FreeBSD__)
    487 ahc_reset(iobase)
    488 	u_long iobase;
    489 #elif defined(__NetBSD__)
    490 ahc_reset(devname, iot, ioh)
    491 	char *devname;
    492 	bus_space_tag_t iot;
    493 	bus_space_handle_t ioh;
    494 #endif
    495 {
    496         u_char hcntrl;
    497 	int wait;
    498 
    499 	/* Retain the IRQ type accross the chip reset */
    500 #if defined(__FreeBSD__)
    501 	hcntrl = (inb(HCNTRL + iobase) & IRQMS) | INTEN;
    502 
    503 	outb(HCNTRL + iobase, CHIPRST | PAUSE);
    504 #elif defined(__NetBSD__)
    505 	hcntrl = (bus_space_read_1(iot, ioh, HCNTRL) & IRQMS) | INTEN;
    506 
    507 	bus_space_write_1(iot, ioh, HCNTRL, CHIPRST | PAUSE);
    508 #endif
    509 	/*
    510 	 * Ensure that the reset has finished
    511 	 */
    512 	wait = 1000;
    513 #if defined(__FreeBSD__)
    514 	while (--wait && !(inb(HCNTRL + iobase) & CHIPRSTACK))
    515 #elif defined(__NetBSD__)
    516 	while (--wait && !(bus_space_read_1(iot, ioh, HCNTRL) & CHIPRSTACK))
    517 #endif
    518 		DELAY(1000);
    519 	if(wait == 0) {
    520 #if defined(__FreeBSD__)
    521 		printf("ahc at 0x%lx: WARNING - Failed chip reset!  "
    522 		       "Trying to initialize anyway.\n", iobase);
    523 #elif defined(__NetBSD__)
    524 		printf("%s: WARNING - Failed chip reset!  "
    525 		       "Trying to initialize anyway.\n", devname);
    526 #endif
    527 	}
    528 #if defined(__FreeBSD__)
    529 	outb(HCNTRL + iobase, hcntrl | PAUSE);
    530 #elif defined(__NetBSD__)
    531 	bus_space_write_1(iot, ioh, HCNTRL, hcntrl | PAUSE);
    532 #endif
    533 }
    534 
    535 /*
    536  * Look up the valid period to SCSIRATE conversion in our table.
    537  */
    538 static void
    539 ahc_scsirate(ahc, scsirate, period, offset, channel, target )
    540 	struct	 ahc_data *ahc;
    541 	u_int8_t *scsirate;
    542 	u_int8_t *period;
    543 	u_int8_t *offset;
    544 	char	 channel;
    545 	int	 target;
    546 {
    547 	int i;
    548 	u_int32_t ultra_enb_addr;
    549 	u_int8_t  sxfrctl0;
    550 	u_int8_t  ultra_enb;
    551 
    552 	i = ahc_num_syncrates; /* Default to async */
    553 
    554 	if (*period >= ahc_syncrates[0].period && *offset != 0) {
    555 		for (i = 0; i < ahc_num_syncrates; i++) {
    556 
    557 			if (*period <= ahc_syncrates[i].period) {
    558 				/*
    559 				 * Watch out for Ultra speeds when ultra is not
    560 				 * enabled and vice-versa.
    561 				 */
    562 				if(!(ahc->type & AHC_ULTRA)
    563 				 && (ahc_syncrates[i].sxfr & ULTRA_SXFR)) {
    564 					/*
    565 					 * This should only happen if the
    566 					 * drive is the first to negotiate
    567 					 * and chooses a high rate.  We'll
    568 					 * just move down the table util
    569 					 * we hit a non ultra speed.
    570 					 */
    571 					continue;
    572 				}
    573 				*scsirate = (ahc_syncrates[i].sxfr & 0xF0)
    574 					  | (*offset & 0x0f);
    575 				*period = ahc_syncrates[i].period;
    576 
    577 				if(bootverbose) {
    578 					printf("%s: target %d synchronous at %sMHz,"
    579 					       " offset = 0x%x\n",
    580 					        ahc_name(ahc), target,
    581 						ahc_syncrates[i].rate, *offset );
    582 				}
    583 				break;
    584 			}
    585 		}
    586 	}
    587 	if (i >= ahc_num_syncrates) {
    588 		/* Use asyncronous transfers. */
    589 		*scsirate = 0;
    590 		*period = 0;
    591 		*offset = 0;
    592 		if (bootverbose)
    593 			printf("%s: target %d using asyncronous transfers\n",
    594 			       ahc_name(ahc), target );
    595 	}
    596 	/*
    597 	 * Ensure Ultra mode is set properly for
    598 	 * this target.
    599 	 */
    600 	ultra_enb_addr = ULTRA_ENB;
    601 	if(channel == 'B' || target > 7)
    602 		ultra_enb_addr++;
    603 	ultra_enb = AHC_INB(ahc, ultra_enb_addr);
    604 	sxfrctl0 = AHC_INB(ahc, SXFRCTL0);
    605 	if (*scsirate != 0 && ahc_syncrates[i].sxfr & ULTRA_SXFR) {
    606 		ultra_enb |= 0x01 << (target & 0x07);
    607 		sxfrctl0 |= ULTRAEN;
    608 	}
    609 	else {
    610 		ultra_enb &= ~(0x01 << (target & 0x07));
    611 		sxfrctl0 &= ~ULTRAEN;
    612 	}
    613 	AHC_OUTB(ahc, ultra_enb_addr, ultra_enb);
    614 	AHC_OUTB(ahc, SXFRCTL0, sxfrctl0);
    615 }
    616 
    617 /*
    618  * Attach all the sub-devices we can find
    619  */
    620 int
    621 ahc_attach(ahc)
    622 	struct ahc_data *ahc;
    623 {
    624 #if defined(__FreeBSD__)
    625 	struct scsibus_data *scbus;
    626 #endif
    627 
    628 #if defined(__NetBSD__)			/* XXX */
    629 	/*
    630 	 * Initialize the software queue.
    631 	 */
    632 	LIST_INIT(&ahc->sc_xxxq);
    633 #endif
    634 
    635 #ifdef AHC_BROKEN_CACHE
    636 	if (cpu_class == CPUCLASS_386)	/* doesn't have "wbinvd" instruction */
    637 		ahc_broken_cache = 0;
    638 #endif
    639 	/*
    640 	 * fill in the prototype scsi_links.
    641 	 */
    642 #if defined(__FreeBSD__)
    643 	ahc->sc_link.adapter_unit = ahc->unit;
    644 	ahc->sc_link.adapter_targ = ahc->our_id;
    645 	ahc->sc_link.fordriver = 0;
    646 #elif defined(__NetBSD__)
    647 	ahc->sc_link.adapter_target = ahc->our_id;
    648 	ahc->sc_link.channel = 0;
    649 	/*
    650 	 * Set up max_target.
    651 	 */
    652 	ahc->sc_link.max_target = (ahc->type & AHC_WIDE) ? 15 : 7;
    653 #endif
    654 	ahc->sc_link.adapter_softc = ahc;
    655 	ahc->sc_link.adapter = &ahc_switch;
    656 	ahc->sc_link.opennings = 2;
    657 	ahc->sc_link.device = &ahc_dev;
    658 	ahc->sc_link.flags = DEBUGLEVEL;
    659 
    660 	if(ahc->type & AHC_TWIN) {
    661 		/* Configure the second scsi bus */
    662 		ahc->sc_link_b = ahc->sc_link;
    663 #if defined(__FreeBSD__)
    664 		ahc->sc_link_b.adapter_targ = ahc->our_id_b;
    665 		ahc->sc_link_b.adapter_bus = 1;
    666 		ahc->sc_link_b.fordriver = (void *)SELBUSB;
    667 #elif defined(__NetBSD__)
    668 		ahc->sc_link_b.adapter_target = ahc->our_id_b;
    669 		ahc->sc_link_b.channel = 1;
    670 #endif
    671 	}
    672 
    673 
    674 #if defined(__FreeBSD__)
    675 	/*
    676 	 * Prepare the scsibus_data area for the upperlevel
    677 	 * scsi code.
    678 	 */
    679 	scbus = scsi_alloc_bus();
    680 	if(!scbus)
    681 		return 0;
    682 	scbus->adapter_link = (ahc->flags & AHC_CHANNEL_B_PRIMARY) ?
    683 				&ahc->sc_link_b : &ahc->sc_link;
    684 	if(ahc->type & AHC_WIDE)
    685 		scbus->maxtarg = 15;
    686 
    687 	/*
    688 	 * ask the adapter what subunits are present
    689 	 */
    690 	if(bootverbose)
    691 		printf("ahc%d: Probing channel %c\n", ahc->unit,
    692 			(ahc->flags & AHC_CHANNEL_B_PRIMARY) ? 'B' : 'A');
    693 	scsi_attachdevs(scbus);
    694 	scbus = NULL;	/* Upper-level SCSI code owns this now */
    695 
    696 	if(ahc->type & AHC_TWIN) {
    697 		scbus =  scsi_alloc_bus();
    698 		if(!scbus)
    699 			return 0;
    700 		scbus->adapter_link = (ahc->flags & AHC_CHANNEL_B_PRIMARY) ?
    701 					&ahc->sc_link : &ahc->sc_link_b;
    702 		if(ahc->type & AHC_WIDE)
    703 			scbus->maxtarg = 15;
    704 		if(bootverbose)
    705 			printf("ahc%d: Probing Channel %c\n", ahc->unit,
    706 			       (ahc->flags & AHC_CHANNEL_B_PRIMARY) ? 'A': 'B');
    707 		scsi_attachdevs(scbus);
    708 		scbus = NULL;	/* Upper-level SCSI code owns this now */
    709 	}
    710 #elif defined(__NetBSD__)
    711 	/*
    712 	 * ask the adapter what subunits are present
    713 	 */
    714 	if ((ahc->flags & AHC_CHANNEL_B_PRIMARY) == 0) {
    715 		/* make IS_SCSIBUS_B() == false, while probing channel A */
    716 		ahc->sc_link_b.scsibus = 0xff;
    717 
    718 		config_found((void *)ahc, &ahc->sc_link, scsiprint);
    719 		if (ahc->type & AHC_TWIN)
    720 			config_found((void *)ahc, &ahc->sc_link_b, scsiprint);
    721 	} else {
    722 		/*
    723 		 * if implementation of IS_SCSIBUS_B() is changed to use
    724 		 * ahc->sc_link.scsibus, then "ahc->sc_link.scsibus = 0xff;"
    725 		 * is needed, here.
    726 		 */
    727 
    728 		/* assert(ahc->type & AHC_TWIN); */
    729 		config_found((void *)ahc, &ahc->sc_link_b, scsiprint);
    730 		config_found((void *)ahc, &ahc->sc_link, scsiprint);
    731 	}
    732 #endif
    733 	return 1;
    734 }
    735 
    736 /*
    737  * Send an SCB down to the card via PIO.
    738  * We assume that the proper SCB is already selected in SCBPTR.
    739  */
    740 static inline void
    741 ahc_send_scb(ahc, scb)
    742         struct	ahc_data *ahc;
    743         struct	scb *scb;
    744 {
    745 	AHC_OUTB(ahc, SCBCNT, SCBAUTO);
    746 	if( ahc->type == AHC_284 )
    747 		/* Can only do 8bit PIO */
    748 		AHC_OUTSB(ahc, SCBARRAY, scb, SCB_PIO_TRANSFER_SIZE);
    749 	else
    750 		AHC_OUTSL(ahc, SCBARRAY, scb,
    751 		      (SCB_PIO_TRANSFER_SIZE + 3) / 4);
    752 	AHC_OUTB(ahc, SCBCNT, 0);
    753 }
    754 
    755 /*
    756  * Retrieve an SCB from the card via PIO.
    757  * We assume that the proper SCB is already selected in SCBPTR.
    758  */
    759 static inline void
    760 ahc_fetch_scb(ahc, scb)
    761 	struct	ahc_data *ahc;
    762 	struct	scb *scb;
    763 {
    764 	AHC_OUTB(ahc, SCBCNT, 0x80);	/* SCBAUTO */
    765 
    766 	/* Can only do 8bit PIO for reads */
    767 	AHC_INSB(ahc, SCBARRAY, scb, SCB_PIO_TRANSFER_SIZE);
    768 
    769 	AHC_OUTB(ahc, SCBCNT, 0);
    770 }
    771 
    772 /*
    773  * Swap in_scbp for out_scbp down in the cards SCB array.
    774  * We assume that the SCB for out_scbp is already selected in SCBPTR.
    775  */
    776 static inline void
    777 ahc_page_scb(ahc, out_scbp, in_scbp)
    778 	struct ahc_data *ahc;
    779 	struct scb *out_scbp;
    780 	struct scb *in_scbp;
    781 {
    782 	/* Page-out */
    783 	ahc_fetch_scb(ahc, out_scbp);
    784 	out_scbp->flags |= SCB_PAGED_OUT;
    785 	if(!(out_scbp->control & TAG_ENB))
    786 	{
    787 		/* Stick in non-tagged array */
    788 		int index =  (out_scbp->tcl >> 4)
    789 			   | (out_scbp->tcl & SELBUSB);
    790 		ahc->pagedout_ntscbs[index] = out_scbp;
    791 	}
    792 
    793 	/* Page-in */
    794 	in_scbp->position = out_scbp->position;
    795 	out_scbp->position = SCB_LIST_NULL;
    796 	ahc_send_scb(ahc, in_scbp);
    797 	in_scbp->flags &= ~SCB_PAGED_OUT;
    798 }
    799 
    800 static inline void
    801 ahc_run_waiting_queues(ahc)
    802 	struct ahc_data *ahc;
    803 {
    804 	struct scb* scb;
    805 	u_char cur_scb;
    806 
    807 	if(!(ahc->assigned_scbs.stqh_first || ahc->waiting_scbs.stqh_first))
    808 		return;
    809 
    810 	pause_sequencer(ahc);
    811 	cur_scb = AHC_INB(ahc, SCBPTR);
    812 
    813 	/*
    814 	 * First handle SCBs that are waiting but have been
    815 	 * assigned a slot.
    816 	 */
    817 	while((scb = ahc->assigned_scbs.stqh_first) != NULL) {
    818 		STAILQ_REMOVE_HEAD(&ahc->assigned_scbs, links);
    819 		AHC_OUTB(ahc, SCBPTR, scb->position);
    820 		ahc_send_scb(ahc, scb);
    821 
    822 		/* Mark this as an active command */
    823 		scb->flags ^= SCB_ASSIGNEDQ|SCB_ACTIVE;
    824 
    825 		AHC_OUTB(ahc, QINFIFO, scb->position);
    826 		if (!(scb->xs->flags & SCSI_NOMASK)) {
    827 			timeout(ahc_timeout, (caddr_t)scb,
    828 				(scb->xs->timeout * hz) / 1000);
    829 		}
    830 		SC_DEBUG(scb->xs->sc_link, SDEV_DB3, ("cmd_sent\n"));
    831 	}
    832 	/* Now deal with SCBs that require paging */
    833 	if((scb = ahc->waiting_scbs.stqh_first) != NULL) {
    834 		u_char disc_scb = AHC_INB(ahc, DISCONNECTED_SCBH);
    835 		u_char active = AHC_INB(ahc, FLAGS) & (SELECTED|IDENTIFY_SEEN);
    836 		int count = 0;
    837 
    838 		do {
    839 			u_char next_scb;
    840 
    841 			/* Attempt to page this SCB in */
    842 			if(disc_scb == SCB_LIST_NULL)
    843 				break;
    844 
    845 			/*
    846 			 * Check the next SCB on in the list.
    847 			 */
    848 			AHC_OUTB(ahc, SCBPTR, disc_scb);
    849 			next_scb = AHC_INB(ahc, SCB_NEXT);
    850 
    851 			/*
    852 			 * We have to be careful about when we allow
    853 			 * an SCB to be paged out.  There must always
    854 			 * be at least one slot availible for a
    855 			 * reconnecting target in case it references
    856 			 * an SCB that has been paged out.  Our
    857 			 * heuristic is that either the disconnected
    858 			 * list has at least two entries in it or
    859 			 * there is one entry and the sequencer is
    860 			 * activily working on an SCB which implies that
    861 			 * it will either complete or disconnect before
    862 			 * another reconnection can occur.
    863 			 */
    864 			if((next_scb != SCB_LIST_NULL) || active)
    865 			{
    866 				u_char out_scbi;
    867 				struct scb* out_scbp;
    868 
    869 				STAILQ_REMOVE_HEAD(&ahc->waiting_scbs, links);
    870 
    871 				/*
    872 				 * Find the in-core SCB for the one
    873 				 * we're paging out.
    874 				 */
    875 				out_scbi = AHC_INB(ahc, SCB_TAG);
    876 				out_scbp = ahc->scbarray[out_scbi];
    877 
    878 				/* Do the page out */
    879 				ahc_page_scb(ahc, out_scbp, scb);
    880 
    881 				/* Mark this as an active command */
    882 				scb->flags ^= SCB_WAITINGQ|SCB_ACTIVE;
    883 
    884 				/* Queue the command */
    885 				AHC_OUTB(ahc, QINFIFO, scb->position);
    886 				if (!(scb->xs->flags & SCSI_NOMASK)) {
    887 					timeout(ahc_timeout, (caddr_t)scb,
    888 						(scb->xs->timeout * hz) / 1000);
    889 				}
    890 				SC_DEBUG(scb->xs->sc_link, SDEV_DB3,
    891 					("cmd_paged-in\n"));
    892 				count++;
    893 
    894 				/* Advance to the next disconnected SCB */
    895 				disc_scb = next_scb;
    896 			}
    897 			else
    898 				break;
    899 		} while((scb = ahc->waiting_scbs.stqh_first) != NULL);
    900 
    901 		if(count) {
    902 			/*
    903 			 * Update the head of the disconnected list.
    904 			 */
    905 			AHC_OUTB(ahc, DISCONNECTED_SCBH, disc_scb);
    906 			if(disc_scb != SCB_LIST_NULL) {
    907 				AHC_OUTB(ahc, SCBPTR, disc_scb);
    908 				AHC_OUTB(ahc, SCB_PREV, SCB_LIST_NULL);
    909 			}
    910 		}
    911 	}
    912 	/* Restore old position */
    913 	AHC_OUTB(ahc, SCBPTR, cur_scb);
    914 	unpause_sequencer(ahc, /*unpause_always*/FALSE);
    915 }
    916 
    917 /*
    918  * Add this SCB to the head of the "waiting for selection" list.
    919  */
    920 static
    921 void ahc_add_waiting_scb(ahc, scb)
    922 	struct ahc_data *ahc;
    923 	struct scb *scb;
    924 {
    925 	u_char next;
    926 	u_char curscb;
    927 
    928 	curscb = AHC_INB(ahc, SCBPTR);
    929 	next = AHC_INB(ahc, WAITING_SCBH);
    930 
    931 	AHC_OUTB(ahc, SCBPTR, scb->position);
    932 	AHC_OUTB(ahc, SCB_NEXT, next);
    933 	AHC_OUTB(ahc, WAITING_SCBH, scb->position);
    934 
    935 	AHC_OUTB(ahc, SCBPTR, curscb);
    936 }
    937 
    938 /*
    939  * Catch an interrupt from the adapter
    940  */
    941 #if defined(__FreeBSD__)
    942 void
    943 #elif defined (__NetBSD__)
    944 int
    945 #endif
    946 ahc_intr(arg)
    947         void *arg;
    948 {
    949 	int     intstat;
    950 	u_char	status;
    951 	struct	scb *scb;
    952 	struct	scsi_xfer *xs;
    953 	struct	ahc_data *ahc = (struct ahc_data *)arg;
    954 
    955 	intstat = AHC_INB(ahc, INTSTAT);
    956 	/*
    957 	 * Is this interrupt for me? or for
    958 	 * someone who is sharing my interrupt
    959 	 */
    960 	if (!(intstat & INT_PEND))
    961 #if defined(__FreeBSD__)
    962 		return;
    963 #elif defined(__NetBSD__)
    964 		return 0;
    965 #endif
    966 
    967         if (intstat & BRKADRINT) {
    968 		/* We upset the sequencer :-( */
    969 
    970 		/* Lookup the error message */
    971 		int i, error = AHC_INB(ahc, ERROR);
    972 		int num_errors =  sizeof(hard_error)/sizeof(hard_error[0]);
    973 		for(i = 0; error != 1 && i < num_errors; i++)
    974 			error >>= 1;
    975                 panic("%s: brkadrint, %s at seqaddr = 0x%x\n",
    976 		      ahc_name(ahc), hard_error[i].errmesg,
    977 		      (AHC_INB(ahc, SEQADDR1) << 8) |
    978 		      AHC_INB(ahc, SEQADDR0));
    979         }
    980         if (intstat & SEQINT)
    981 		ahc_handle_seqint(ahc, intstat);
    982 
    983 	if (intstat & SCSIINT) {
    984 
    985 		int scb_index = AHC_INB(ahc, SCB_TAG);
    986 		status = AHC_INB(ahc, SSTAT1);
    987 		scb = ahc->scbarray[scb_index];
    988 
    989 		if (status & SCSIRSTI) {
    990 			char channel;
    991 			channel = AHC_INB(ahc, SBLKCTL);
    992 			channel = channel & SELBUSB ? 'B' : 'A';
    993 			printf("%s: Someone reset channel %c\n",
    994 				ahc_name(ahc), channel);
    995 			ahc_reset_channel(ahc,
    996 					  channel,
    997 					  SCB_LIST_NULL,
    998 					  XS_BUSY,
    999 					  /* Initiate Reset */FALSE);
   1000 			scb = NULL;
   1001 		}
   1002 		else if (!(scb && (scb->flags & SCB_ACTIVE))){
   1003 			printf("%s: ahc_intr - referenced scb not "
   1004 			       "valid during scsiint 0x%x scb(%d)\n",
   1005 				ahc_name(ahc), status, scb_index);
   1006 			AHC_OUTB(ahc, CLRSINT1, status);
   1007 			unpause_sequencer(ahc, /*unpause_always*/TRUE);
   1008 			AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   1009 			scb = NULL;
   1010 		}
   1011 		else if (status & SCSIPERR) {
   1012 			/*
   1013 			 * Determine the bus phase and
   1014 			 * queue an appropriate message
   1015 			 */
   1016 			char	*phase;
   1017 			u_char	mesg_out = MSG_NOOP;
   1018 			u_char	lastphase = AHC_INB(ahc, LASTPHASE);
   1019 
   1020 			xs = scb->xs;
   1021 			sc_print_addr(xs->sc_link);
   1022 
   1023 			switch(lastphase) {
   1024 				case P_DATAOUT:
   1025 					phase = "Data-Out";
   1026 					break;
   1027 				case P_DATAIN:
   1028 					phase = "Data-In";
   1029 					mesg_out = MSG_INITIATOR_DET_ERR;
   1030 					break;
   1031 				case P_COMMAND:
   1032 					phase = "Command";
   1033 					break;
   1034 				case P_MESGOUT:
   1035 					phase = "Message-Out";
   1036 					break;
   1037 				case P_STATUS:
   1038 					phase = "Status";
   1039 					mesg_out = MSG_INITIATOR_DET_ERR;
   1040 					break;
   1041 				case P_MESGIN:
   1042 					phase = "Message-In";
   1043 					mesg_out = MSG_PARITY_ERROR;
   1044 					break;
   1045 				default:
   1046 					phase = "unknown";
   1047 					break;
   1048 			}
   1049 			printf("parity error during %s phase.\n", phase);
   1050 
   1051 			/*
   1052 			 * We've set the hardware to assert ATN if we
   1053 			 * get a parity error on "in" phases, so all we
   1054 			 * need to do is stuff the message buffer with
   1055 			 * the appropriate message.  "In" phases have set
   1056 			 * mesg_out to something other than MSG_NOP.
   1057 			 */
   1058 			if(mesg_out != MSG_NOOP) {
   1059 				AHC_OUTB(ahc, MSG0, mesg_out);
   1060 				AHC_OUTB(ahc, MSG_LEN, 1);
   1061 			}
   1062 			else
   1063 				/*
   1064 				 * Should we allow the target to make
   1065 				 * this decision for us?
   1066 				 */
   1067 				xs->error = XS_DRIVER_STUFFUP;
   1068 		}
   1069 		else if (status & SELTO) {
   1070 			u_char waiting;
   1071 			u_char flags;
   1072 
   1073 			xs = scb->xs;
   1074 			xs->error = XS_SELTIMEOUT;
   1075 			/*
   1076 			 * Clear any pending messages for the timed out
   1077 			 * target, and mark the target as free
   1078 			 */
   1079 			flags = AHC_INB(ahc, FLAGS);
   1080 			AHC_OUTB(ahc, MSG_LEN, 0);
   1081 			ahc_unbusy_target(ahc, xs->sc_link->target,
   1082 #if defined(__FreeBSD__)
   1083 			 	((long)xs->sc_link->fordriver & SELBUSB)
   1084 #elif defined(__NetBSD__)
   1085 				IS_SCSIBUS_B(ahc, xs->sc_link)
   1086 #endif
   1087 				 	? 'B' : 'A');
   1088 			/* Stop the selection */
   1089 			AHC_OUTB(ahc, SCSISEQ, 0);
   1090 
   1091 			AHC_OUTB(ahc, SCB_CONTROL, 0);
   1092 
   1093 			AHC_OUTB(ahc, CLRSINT1, CLRSELTIMEO);
   1094 
   1095 			AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   1096 
   1097 			/* Shift the waiting for selection queue forward */
   1098 			waiting = AHC_INB(ahc, WAITING_SCBH);
   1099 			AHC_OUTB(ahc, SCBPTR, waiting);
   1100 			waiting = AHC_INB(ahc, SCB_NEXT);
   1101 			AHC_OUTB(ahc, WAITING_SCBH, waiting);
   1102 
   1103 			restart_sequencer(ahc);
   1104 		}
   1105 		else if (!(status & BUSFREE)) {
   1106 		      sc_print_addr(scb->xs->sc_link);
   1107 		      printf("Unknown SCSIINT. Status = 0x%x\n", status);
   1108 		      AHC_OUTB(ahc, CLRSINT1, status);
   1109 		      unpause_sequencer(ahc, /*unpause_always*/TRUE);
   1110 		      AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   1111 		      scb = NULL;
   1112 		}
   1113 		if(scb != NULL) {
   1114 		    /* We want to process the command */
   1115 		    untimeout(ahc_timeout, (caddr_t)scb);
   1116 		    ahc_done(ahc, scb);
   1117 		}
   1118 	}
   1119 	if (intstat & CMDCMPLT) {
   1120 		int   scb_index;
   1121 
   1122 		do {
   1123 			scb_index = AHC_INB(ahc, QOUTFIFO);
   1124 			scb = ahc->scbarray[scb_index];
   1125 			if (!scb || !(scb->flags & SCB_ACTIVE)) {
   1126 				printf("%s: WARNING "
   1127 				       "no command for scb %d (cmdcmplt)\n"
   1128 				       "QOUTCNT == %d\n",
   1129 					ahc_name(ahc), scb_index,
   1130 					AHC_INB(ahc, QOUTCNT));
   1131 				AHC_OUTB(ahc, CLRINT, CLRCMDINT);
   1132 				continue;
   1133 			}
   1134 			AHC_OUTB(ahc, CLRINT, CLRCMDINT);
   1135 			untimeout(ahc_timeout, (caddr_t)scb);
   1136 			ahc_done(ahc, scb);
   1137 
   1138 		} while (AHC_INB(ahc, QOUTCNT) & ahc->qcntmask);
   1139 
   1140 		ahc_run_waiting_queues(ahc);
   1141 	}
   1142 #if defined(__NetBSD__)
   1143 	return 1;
   1144 #endif
   1145 }
   1146 
   1147 static void
   1148 ahc_handle_seqint(ahc, intstat)
   1149 	struct ahc_data *ahc;
   1150 	u_int8_t intstat;
   1151 {
   1152 	struct scb *scb;
   1153 	u_short targ_mask;
   1154 	u_char target = (AHC_INB(ahc, SCSIID) >> 4) & 0x0f;
   1155 	u_char scratch_offset = target;
   1156 	char channel = AHC_INB(ahc, SBLKCTL) & SELBUSB ? 'B': 'A';
   1157 
   1158 	if (channel == 'B')
   1159 		scratch_offset += 8;
   1160 	targ_mask = (0x01 << scratch_offset);
   1161 
   1162 	switch (intstat & SEQINT_MASK) {
   1163 	case NO_MATCH:
   1164 		if (ahc->flags & AHC_PAGESCBS) {
   1165 			/* SCB Page-in request */
   1166 			u_char tag;
   1167 			u_char next;
   1168 			u_char disc_scb;
   1169 			struct scb *outscb;
   1170 			u_char arg_1 = AHC_INB(ahc, ARG_1);
   1171 
   1172 			/*
   1173 			 * We should succeed, so set this now.
   1174 			 * If we don't, and one of the methods
   1175 			 * we use to aquire an SCB calls ahc_done,
   1176 			 * we may wind up in our start routine
   1177 			 * and unpause the adapter without giving
   1178 			 * it the correct return value, which will
   1179 			 * cause a hang.
   1180 			 */
   1181 			AHC_OUTB(ahc, RETURN_1, SCB_PAGEDIN);
   1182 
   1183 			if (arg_1 == SCB_LIST_NULL) {
   1184 				/* Non-tagged command */
   1185 				int index;
   1186 
   1187 				index = target|(channel == 'B' ? SELBUSB : 0);
   1188 				scb = ahc->pagedout_ntscbs[index];
   1189 			} else
   1190 				scb = ahc->scbarray[arg_1];
   1191 
   1192 			if (!(scb->flags & SCB_PAGED_OUT))
   1193 				panic("%s: Request to page in a non paged out "
   1194 				      "SCB.", ahc_name(ahc));
   1195 			/*
   1196 			 * Now to pick the SCB to page out.
   1197 			 * Either take a free SCB, an assigned SCB,
   1198 			 * an SCB that just completed, the first
   1199 			 * one on the disconnected SCB list, or
   1200 			 * as a last resort a queued SCB.
   1201 			 */
   1202 			if (ahc->free_scbs.stqh_first) {
   1203 				outscb = ahc->free_scbs.stqh_first;
   1204 				STAILQ_REMOVE_HEAD(&ahc->free_scbs, links);
   1205 				scb->position = outscb->position;
   1206 				outscb->position = SCB_LIST_NULL;
   1207 				STAILQ_INSERT_HEAD(&ahc->page_scbs, outscb,
   1208 						   links);
   1209 				AHC_OUTB(ahc, SCBPTR, scb->position);
   1210 				ahc_send_scb(ahc, scb);
   1211 				scb->flags &= ~SCB_PAGED_OUT;
   1212 				goto pagein_done;
   1213 			}
   1214 			if (intstat & CMDCMPLT) {
   1215 				int   scb_index;
   1216 
   1217 				AHC_OUTB(ahc, CLRINT, CLRCMDINT);
   1218 				scb_index = AHC_INB(ahc, QOUTFIFO);
   1219 				if (!(AHC_INB(ahc, QOUTCNT) & ahc->qcntmask))
   1220 					intstat &= ~CMDCMPLT;
   1221 
   1222 				outscb = ahc->scbarray[scb_index];
   1223 				if (!outscb || !(outscb->flags & SCB_ACTIVE)) {
   1224 					printf("%s: WARNING no command for "
   1225 					       "scb %d (cmdcmplt)\n",
   1226 					       ahc_name(ahc),
   1227 					       scb_index);
   1228 					/*
   1229 					 * Fall through in hopes of finding
   1230 					 * another SCB
   1231 					 */
   1232 				} else {
   1233 					scb->position = outscb->position;
   1234 					outscb->position = SCB_LIST_NULL;
   1235 					AHC_OUTB(ahc, SCBPTR, scb->position);
   1236 					ahc_send_scb(ahc, scb);
   1237 					scb->flags &= ~SCB_PAGED_OUT;
   1238 					untimeout(ahc_timeout,
   1239 						  (caddr_t)outscb);
   1240 					ahc_done(ahc, outscb);
   1241 					goto pagein_done;
   1242 				}
   1243 			}
   1244 			disc_scb = AHC_INB(ahc, DISCONNECTED_SCBH);
   1245 			if (disc_scb != SCB_LIST_NULL) {
   1246 				AHC_OUTB(ahc, SCBPTR, disc_scb);
   1247 				tag = AHC_INB(ahc, SCB_TAG);
   1248 				outscb = ahc->scbarray[tag];
   1249 				next = AHC_INB(ahc, SCB_NEXT);
   1250 				if (next != SCB_LIST_NULL) {
   1251 					AHC_OUTB(ahc, SCBPTR, next);
   1252 					AHC_OUTB(ahc, SCB_PREV,
   1253 						 SCB_LIST_NULL);
   1254 					AHC_OUTB(ahc, SCBPTR, disc_scb);
   1255 				}
   1256 				AHC_OUTB(ahc, DISCONNECTED_SCBH, next);
   1257 				ahc_page_scb(ahc, outscb, scb);
   1258 			} else if (AHC_INB(ahc, QINCNT) & ahc->qcntmask) {
   1259 				/*
   1260 				 * Pull one of our queued commands
   1261 				 * as a last resort
   1262 				 */
   1263 				disc_scb = AHC_INB(ahc, QINFIFO);
   1264 				AHC_OUTB(ahc, SCBPTR, disc_scb);
   1265 				tag = AHC_INB(ahc, SCB_TAG);
   1266 				outscb = ahc->scbarray[tag];
   1267 				if ((outscb->control & 0x23) != TAG_ENB) {
   1268 					/*
   1269 					 * This is not a simple tagged command
   1270 					 * so its position in the queue
   1271 					 * matters.  Take the command at the
   1272 					 * end of the queue instead.
   1273 					 */
   1274 					int i;
   1275 					u_char saved_queue[AHC_SCB_MAX];
   1276 					u_char queued = AHC_INB(ahc, QINCNT)
   1277 							& ahc->qcntmask;
   1278 
   1279 					/*
   1280 					 * Count the command we removed
   1281 					 * already
   1282 					 */
   1283 					saved_queue[0] = disc_scb;
   1284 					queued++;
   1285 
   1286 					/* Empty the input queue */
   1287 					for (i = 1; i < queued; i++)
   1288 						saved_queue[i] = AHC_INB(ahc, QINFIFO);
   1289 
   1290 					/*
   1291 					 * Put everyone back but the
   1292 					 * last entry
   1293 					 */
   1294 					queued--;
   1295 					for (i = 0; i < queued; i++)
   1296 						AHC_OUTB(ahc, QINFIFO,
   1297 							 saved_queue[i]);
   1298 
   1299 					AHC_OUTB(ahc, SCBPTR,
   1300 						 saved_queue[queued]);
   1301 					tag = AHC_INB(ahc, SCB_TAG);
   1302 					outscb = ahc->scbarray[tag];
   1303 				}
   1304 				untimeout(ahc_timeout, (caddr_t)outscb);
   1305 				scb->position = outscb->position;
   1306 				outscb->position = SCB_LIST_NULL;
   1307 				STAILQ_INSERT_HEAD(&ahc->waiting_scbs,
   1308 						   outscb, links);
   1309 				outscb->flags |= SCB_WAITINGQ;
   1310 				ahc_send_scb(ahc, scb);
   1311 				scb->flags &= ~SCB_PAGED_OUT;
   1312 			}
   1313 			else {
   1314 				panic("Page-in request with no candidates");
   1315 				AHC_OUTB(ahc, RETURN_1, 0);
   1316 			}
   1317 		  pagein_done:
   1318 		} else {
   1319 			printf("%s:%c:%d: no active SCB for reconnecting "
   1320 			       "target - issuing ABORT\n",
   1321 			       ahc_name(ahc), channel, target);
   1322 			printf("SAVED_TCL == 0x%x\n",
   1323 			       AHC_INB(ahc, SAVED_TCL));
   1324 			ahc_unbusy_target(ahc, target, channel);
   1325 			AHC_OUTB(ahc, SCB_CONTROL, 0);
   1326 			AHC_OUTB(ahc, CLRSINT1, CLRSELTIMEO);
   1327 			AHC_OUTB(ahc, RETURN_1, 0);
   1328 		}
   1329 		break;
   1330 	case SEND_REJECT:
   1331 	{
   1332 		u_char rejbyte = AHC_INB(ahc, REJBYTE);
   1333 		printf("%s:%c:%d: Warning - unknown message recieved from "
   1334 		       "target (0x%x).  Rejecting\n",
   1335 		       ahc_name(ahc), channel, target, rejbyte);
   1336 		break;
   1337 	}
   1338 	case NO_IDENT:
   1339 		panic("%s:%c:%d: Target did not send an IDENTIFY message. "
   1340 		      "SAVED_TCL == 0x%x\n",
   1341 		      ahc_name(ahc), channel, target,
   1342 		      AHC_INB(ahc, SAVED_TCL));
   1343 		break;
   1344 	case BAD_PHASE:
   1345 		printf("%s:%c:%d: unknown scsi bus phase.  Attempting to "
   1346 		       "continue\n", ahc_name(ahc), channel, target);
   1347 		break;
   1348 	case EXTENDED_MSG:
   1349 	{
   1350 		u_int8_t message_length;
   1351 		u_int8_t message_code;
   1352 
   1353 		message_length = AHC_INB(ahc, MSGIN_EXT_LEN);
   1354 		message_code = AHC_INB(ahc, MSGIN_EXT_OPCODE);
   1355 		switch(message_code) {
   1356 		case MSG_EXT_SDTR:
   1357 		{
   1358 			u_int8_t period;
   1359 			u_int8_t offset;
   1360 			u_int8_t saved_offset;
   1361 			u_int8_t targ_scratch;
   1362 			u_int8_t maxoffset;
   1363 			u_int8_t rate;
   1364 
   1365 			if (message_length != MSG_EXT_SDTR_LEN) {
   1366 				AHC_OUTB(ahc, RETURN_1, SEND_REJ);
   1367 				ahc->sdtrpending &= ~targ_mask;
   1368 				break;
   1369 			}
   1370 			period = AHC_INB(ahc, MSGIN_EXT_BYTE0);
   1371 			saved_offset = AHC_INB(ahc, MSGIN_EXT_BYTE1);
   1372 			targ_scratch = AHC_INB(ahc, TARG_SCRATCH
   1373 					       + scratch_offset);
   1374 			if (targ_scratch & WIDEXFER)
   1375 				maxoffset = MAX_OFFSET_16BIT;
   1376 			else
   1377 				maxoffset = MAX_OFFSET_8BIT;
   1378 			offset = MIN(saved_offset, maxoffset);
   1379 			ahc_scsirate(ahc, &rate, &period, &offset,
   1380 				     channel, target);
   1381 			/* Preserve the WideXfer flag */
   1382 			targ_scratch = rate | (targ_scratch & WIDEXFER);
   1383 
   1384 			/*
   1385 			 * Update both the target scratch area and the
   1386 			 * current SCSIRATE.
   1387 			 */
   1388 			AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset,
   1389 				 targ_scratch);
   1390 			AHC_OUTB(ahc, SCSIRATE, targ_scratch);
   1391 
   1392 			/*
   1393 			 * See if we initiated Sync Negotiation
   1394 			 * and didn't have to fall down to async
   1395 			 * transfers.
   1396 			 */
   1397 			if ((ahc->sdtrpending & targ_mask) != 0
   1398 			 && (saved_offset == offset)) {
   1399 				/*
   1400 				 * Don't send an SDTR back to
   1401 				 * the target
   1402 				 */
   1403 				AHC_OUTB(ahc, RETURN_1, 0);
   1404 				ahc->needsdtr &= ~targ_mask;
   1405 				ahc->sdtrpending &= ~targ_mask;
   1406 			} else {
   1407 				/*
   1408 				 * Send our own SDTR in reply
   1409 				 */
   1410 #ifdef AHC_DEBUG
   1411 				if(ahc_debug & AHC_SHOWMISC)
   1412 					printf("Sending SDTR!!\n");
   1413 #endif
   1414 				ahc_construct_sdtr(ahc, /*start_byte*/0,
   1415 						   period, offset);
   1416 				AHC_OUTB(ahc, RETURN_1, SEND_MSG);
   1417 
   1418 				/*
   1419 				 * If we aren't starting a re-negotiation
   1420 				 * because we had to go async in response
   1421 				 * to a "too low" response from the target
   1422 				 * clear the needsdtr flag for this target.
   1423 				 */
   1424 				if ((ahc->sdtrpending & targ_mask) == 0)
   1425 					ahc->needsdtr &= ~targ_mask;
   1426 				else
   1427 					ahc->sdtrpending |= targ_mask;
   1428 			}
   1429 			break;
   1430 		}
   1431 		case MSG_EXT_WDTR:
   1432 		{
   1433 			u_int8_t scratch, bus_width;
   1434 
   1435 			if (message_length != MSG_EXT_WDTR_LEN) {
   1436 				AHC_OUTB(ahc, RETURN_1, SEND_REJ);
   1437 				ahc->wdtrpending &= ~targ_mask;
   1438 				break;
   1439 			}
   1440 
   1441 			bus_width = AHC_INB(ahc, MSGIN_EXT_BYTE0);
   1442 			scratch = AHC_INB(ahc, TARG_SCRATCH
   1443 					  + scratch_offset);
   1444 
   1445 			if (ahc->wdtrpending & targ_mask) {
   1446 				/*
   1447 				 * Don't send a WDTR back to the
   1448 				 * target, since we asked first.
   1449 				 */
   1450 				AHC_OUTB(ahc, RETURN_1, 0);
   1451 				switch(bus_width){
   1452 				case BUS_8_BIT:
   1453 					scratch &= 0x7f;
   1454 					break;
   1455 				case BUS_16_BIT:
   1456 					if(bootverbose)
   1457 						printf("%s: target %d using "
   1458 						       "16Bit transfers\n",
   1459 						       ahc_name(ahc), target);
   1460 					scratch |= WIDEXFER;
   1461 					break;
   1462 				case BUS_32_BIT:
   1463 					/*
   1464 					 * How can we do 32bit transfers
   1465 					 * on a 16bit bus?
   1466 					 */
   1467 					AHC_OUTB(ahc, RETURN_1, SEND_REJ);
   1468 					printf("%s: target %d requested 32Bit "
   1469 					       "transfers.  Rejecting...\n",
   1470 					       ahc_name(ahc), target);
   1471 					break;
   1472 				default:
   1473 					break;
   1474 				}
   1475 			} else {
   1476 				/*
   1477 				 * Send our own WDTR in reply
   1478 				 */
   1479 				switch(bus_width) {
   1480 				case BUS_8_BIT:
   1481 					scratch &= 0x7f;
   1482 					break;
   1483 				case BUS_32_BIT:
   1484 				case BUS_16_BIT:
   1485 					if(ahc->type & AHC_WIDE) {
   1486 						/* Negotiate 16_BITS */
   1487 						bus_width = BUS_16_BIT;
   1488 						if(bootverbose)
   1489 							printf("%s: target %d "
   1490 							       "using 16Bit "
   1491 							       "transfers\n",
   1492 							       ahc_name(ahc),
   1493 							       target);
   1494 						scratch |= WIDEXFER;
   1495 					} else
   1496 						bus_width = BUS_8_BIT;
   1497 					break;
   1498 				default:
   1499 					break;
   1500 				}
   1501 				ahc_construct_wdtr(ahc, /*start_byte*/0,
   1502 						   bus_width);
   1503 				AHC_OUTB(ahc, RETURN_1, SEND_MSG);
   1504 			}
   1505 
   1506 			ahc->needwdtr &= ~targ_mask;
   1507 			ahc->wdtrpending &= ~targ_mask;
   1508 			AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset, scratch);
   1509 			AHC_OUTB(ahc, SCSIRATE, scratch);
   1510 			break;
   1511 		}
   1512 		default:
   1513 			/* Unknown extended message.  Reject it. */
   1514 			AHC_OUTB(ahc, RETURN_1, SEND_REJ);
   1515 		}
   1516 	}
   1517 	case REJECT_MSG:
   1518 	{
   1519 		/*
   1520 		 * What we care about here is if we had an
   1521 		 * outstanding SDTR or WDTR message for this
   1522 		 * target.  If we did, this is a signal that
   1523 		 * the target is refusing negotiation.
   1524 		 */
   1525 
   1526 		u_char targ_scratch;
   1527 
   1528 		targ_scratch = AHC_INB(ahc, TARG_SCRATCH
   1529 				       + scratch_offset);
   1530 
   1531 		if (ahc->wdtrpending & targ_mask){
   1532 			/* note 8bit xfers and clear flag */
   1533 			targ_scratch &= 0x7f;
   1534 			ahc->needwdtr &= ~targ_mask;
   1535 			ahc->wdtrpending &= ~targ_mask;
   1536 			printf("%s:%c:%d: refuses WIDE negotiation.  Using "
   1537 			       "8bit transfers\n", ahc_name(ahc),
   1538 			       channel, target);
   1539 		} else if(ahc->sdtrpending & targ_mask){
   1540 			/* note asynch xfers and clear flag */
   1541 			targ_scratch &= 0xf0;
   1542 			ahc->needsdtr &= ~targ_mask;
   1543 			ahc->sdtrpending &= ~targ_mask;
   1544 			printf("%s:%c:%d: refuses syncronous negotiation. "
   1545 			       "Using asyncronous transfers\n",
   1546 			       ahc_name(ahc),
   1547 			       channel, target);
   1548 		} else {
   1549 			/*
   1550 			 * Otherwise, we ignore it.
   1551 			 */
   1552 #ifdef AHC_DEBUG
   1553 			if(ahc_debug & AHC_SHOWMISC)
   1554 				printf("%s:%c:%d: Message reject -- ignored\n",
   1555 				       ahc_name(ahc), channel, target);
   1556 #endif
   1557 			break;
   1558 		}
   1559 		AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset, targ_scratch);
   1560 		AHC_OUTB(ahc, SCSIRATE, targ_scratch);
   1561 		break;
   1562 	}
   1563 	case BAD_STATUS:
   1564 	{
   1565 		int	scb_index;
   1566 		struct	scsi_xfer *xs;
   1567 
   1568 		/* The sequencer will notify us when a command
   1569 		 * has an error that would be of interest to
   1570 		 * the kernel.  This allows us to leave the sequencer
   1571 		 * running in the common case of command completes
   1572 		 * without error.
   1573 		 */
   1574 
   1575 		scb_index = AHC_INB(ahc, SCB_TAG);
   1576 		scb = ahc->scbarray[scb_index];
   1577 
   1578 		/*
   1579 		 * Set the default return value to 0 (don't
   1580 		 * send sense).  The sense code will change
   1581 		 * this if needed and this reduces code
   1582 		 * duplication.
   1583 		 */
   1584 		AHC_OUTB(ahc, RETURN_1, 0);
   1585 		if (!(scb && (scb->flags & SCB_ACTIVE))) {
   1586 			printf("%s:%c:%d: ahc_intr - referenced scb "
   1587 			       "not valid during seqint 0x%x scb(%d)\n",
   1588 			       ahc_name(ahc),
   1589 			       channel, target, intstat,
   1590 			       scb_index);
   1591 			goto clear;
   1592 		}
   1593 
   1594 		xs = scb->xs;
   1595 
   1596 		scb->status = AHC_INB(ahc, SCB_TARGET_STATUS);
   1597 
   1598 #ifdef AHC_DEBUG
   1599 		if((ahc_debug & AHC_SHOWSCBS)
   1600 		   && xs->sc_link->target == DEBUGTARGET)
   1601 			ahc_print_scb(scb);
   1602 #endif
   1603 		xs->status = scb->status;
   1604 		switch(scb->status){
   1605 		case SCSI_OK:
   1606 			printf("%s: Interrupted for staus of"
   1607 			       " 0???\n", ahc_name(ahc));
   1608 			break;
   1609 		case SCSI_CHECK:
   1610 #ifdef AHC_DEBUG
   1611 			if(ahc_debug & AHC_SHOWSENSE)
   1612 			{
   1613 				sc_print_addr(xs->sc_link);
   1614 				printf("requests Check Status\n");
   1615 			}
   1616 #endif
   1617 
   1618 			if ((xs->error == XS_NOERROR)
   1619 			    && !(scb->flags & SCB_SENSE)) {
   1620 				struct ahc_dma_seg *sg = scb->ahc_dma;
   1621 				struct scsi_sense *sc = &(scb->sense_cmd);
   1622 #ifdef AHC_DEBUG
   1623 				if (ahc_debug & AHC_SHOWSENSE)
   1624 				{
   1625 					sc_print_addr(xs->sc_link);
   1626 					printf("Sending Sense\n");
   1627 				}
   1628 #endif
   1629 #if defined(__FreeBSD__)
   1630 				sc->op_code = REQUEST_SENSE;
   1631 #elif defined(__NetBSD__)
   1632 				sc->opcode = REQUEST_SENSE;
   1633 #endif
   1634 				sc->byte2 =  xs->sc_link->lun << 5;
   1635 				sc->length = sizeof(struct scsi_sense_data);
   1636 				sc->control = 0;
   1637 
   1638 				sg->addr = KVTOPHYS(&xs->sense);
   1639 				sg->len = sizeof(struct scsi_sense_data);
   1640 
   1641 				scb->control &= DISCENB;
   1642 				scb->status = 0;
   1643 				scb->SG_segment_count = 1;
   1644 				scb->SG_list_pointer = KVTOPHYS(sg);
   1645 				scb->data = sg->addr;
   1646 				scb->datalen = sg->len;
   1647 #ifdef AHC_BROKEN_CACHE
   1648 				if (ahc_broken_cache)
   1649 					INVALIDATE_CACHE();
   1650 #endif
   1651 				scb->cmdpointer = KVTOPHYS(sc);
   1652 				scb->cmdlen = sizeof(*sc);
   1653 
   1654 				scb->flags |= SCB_SENSE;
   1655 				ahc_send_scb(ahc, scb);
   1656 				/*
   1657 				 * Ensure that the target is "BUSY"
   1658 				 * so we don't get overlapping
   1659 				 * commands if we happen to be doing
   1660 				 * tagged I/O.
   1661 				 */
   1662 				ahc_busy_target(ahc, target, channel);
   1663 
   1664 				/*
   1665 				 * Make us the next command to run
   1666 				 */
   1667 				ahc_add_waiting_scb(ahc, scb);
   1668 				AHC_OUTB(ahc, RETURN_1, SEND_SENSE);
   1669 				break;
   1670 			}
   1671 			/*
   1672 			 * Clear the SCB_SENSE Flag and have
   1673 			 * the sequencer do a normal command
   1674 			 * complete with either a "DRIVER_STUFFUP"
   1675 			 * error or whatever other error condition
   1676 			 * we already had.
   1677 			 */
   1678 			scb->flags &= ~SCB_SENSE;
   1679 			if (xs->error == XS_NOERROR)
   1680 				xs->error = XS_DRIVER_STUFFUP;
   1681 			break;
   1682 		case SCSI_BUSY:
   1683 			xs->error = XS_BUSY;
   1684 			sc_print_addr(xs->sc_link);
   1685 			printf("Target Busy\n");
   1686 			break;
   1687 		case SCSI_QUEUE_FULL:
   1688 			/*
   1689 			 * The upper level SCSI code will someday
   1690 			 * handle this properly.
   1691 			 */
   1692 			sc_print_addr(xs->sc_link);
   1693 			printf("Queue Full\n");
   1694 			scb->flags |= SCB_ASSIGNEDQ;
   1695 			STAILQ_INSERT_TAIL(&ahc->assigned_scbs,scb, links);
   1696 			AHC_OUTB(ahc, RETURN_1, SEND_SENSE);
   1697 			break;
   1698 		default:
   1699 			sc_print_addr(xs->sc_link);
   1700 			printf("unexpected targ_status: %x\n", scb->status);
   1701 			xs->error = XS_DRIVER_STUFFUP;
   1702 			break;
   1703 		}
   1704 		break;
   1705 	}
   1706 	case RESIDUAL:
   1707 	{
   1708 		int scb_index;
   1709 		struct scsi_xfer *xs;
   1710 
   1711 		scb_index = AHC_INB(ahc, SCB_TAG);
   1712 		scb = ahc->scbarray[scb_index];
   1713 		xs = scb->xs;
   1714 		/*
   1715 		 * Don't clobber valid resid info with
   1716 		 * a resid coming from a check sense
   1717 		 * operation.
   1718 		 */
   1719 		if (!(scb->flags & SCB_SENSE)) {
   1720 			int resid_sgs;
   1721 
   1722 			/*
   1723 			 * Remainder of the SG where the transfer
   1724 			 * stopped.
   1725 			 */
   1726 			xs->resid = (AHC_INB(ahc, SCB_RESID_DCNT2)<<16) |
   1727 				    (AHC_INB(ahc, SCB_RESID_DCNT1)<<8)  |
   1728 				    AHC_INB(ahc, SCB_RESID_DCNT0);
   1729 
   1730 			/*
   1731 			 * Add up the contents of all residual
   1732 			 * SG segments that are after the SG where
   1733 			 * the transfer stopped.
   1734 			 */
   1735 			resid_sgs = AHC_INB(ahc, SCB_RESID_SGCNT) - 1;
   1736 			while (resid_sgs > 0) {
   1737 				int sg;
   1738 
   1739 				sg = scb->SG_segment_count - resid_sgs;
   1740 				xs->resid += scb->ahc_dma[sg].len;
   1741 				resid_sgs--;
   1742 			}
   1743 
   1744 #if defined(__FreeBSD__)
   1745 			xs->flags |= SCSI_RESID_VALID;
   1746 #elif defined(__NetBSD__)
   1747 			/* XXX - Update to do this right */
   1748 #endif
   1749 #ifdef AHC_DEBUG
   1750 			if (ahc_debug & AHC_SHOWMISC) {
   1751 				sc_print_addr(xs->sc_link);
   1752 				printf("Handled Residual of %ld bytes\n"
   1753 				       ,xs->resid);
   1754 			}
   1755 #endif
   1756 		}
   1757 		break;
   1758 	}
   1759 	case ABORT_TAG:
   1760 	{
   1761 		int   scb_index;
   1762 		struct scsi_xfer *xs;
   1763 
   1764 		scb_index = AHC_INB(ahc, SCB_TAG);
   1765 		scb = ahc->scbarray[scb_index];
   1766 		xs = scb->xs;
   1767 		/*
   1768 		 * We didn't recieve a valid tag back from
   1769 		 * the target on a reconnect.
   1770 		 */
   1771 		sc_print_addr(xs->sc_link);
   1772 		printf("invalid tag recieved -- sending ABORT_TAG\n");
   1773 		xs->error = XS_DRIVER_STUFFUP;
   1774 		untimeout(ahc_timeout, (caddr_t)scb);
   1775 		ahc_done(ahc, scb);
   1776 		break;
   1777 	}
   1778 	case AWAITING_MSG:
   1779 	{
   1780 		int   scb_index;
   1781 		scb_index = AHC_INB(ahc, SCB_TAG);
   1782 		scb = ahc->scbarray[scb_index];
   1783 		/*
   1784 		 * This SCB had a zero length command, informing
   1785 		 * the sequencer that we wanted to send a special
   1786 		 * message to this target.  We only do this for
   1787 		 * BUS_DEVICE_RESET messages currently.
   1788 		 */
   1789 		if (scb->flags & SCB_DEVICE_RESET) {
   1790 			AHC_OUTB(ahc, MSG0,
   1791 				 MSG_BUS_DEV_RESET);
   1792 			AHC_OUTB(ahc, MSG_LEN, 1);
   1793 			printf("Bus Device Reset Message Sent\n");
   1794 		} else if (scb->flags & SCB_MSGOUT_WDTR) {
   1795 			ahc_construct_wdtr(ahc, AHC_INB(ahc, MSG_LEN),
   1796 					   BUS_16_BIT);
   1797 		} else if (scb->flags & SCB_MSGOUT_SDTR) {
   1798 			u_int8_t target_scratch;
   1799 			u_int8_t ultraenable;
   1800 			int sxfr;
   1801 			int i;
   1802 
   1803 			/* Pull the user defined setting */
   1804 			target_scratch = AHC_INB(ahc, TARG_SCRATCH
   1805 						 + scratch_offset);
   1806 
   1807 			sxfr = target_scratch & SXFR;
   1808 			if (scratch_offset < 8)
   1809 				ultraenable = AHC_INB(ahc, ULTRA_ENB);
   1810 			else
   1811 				ultraenable = AHC_INB(ahc, ULTRA_ENB + 1);
   1812 
   1813 			if (ultraenable & targ_mask)
   1814 				/* Want an ultra speed in the table */
   1815 				sxfr |= 0x100;
   1816 
   1817 			for (i = 0; i < ahc_num_syncrates; i++)
   1818 				if (sxfr == ahc_syncrates[i].sxfr)
   1819 					break;
   1820 
   1821 			ahc_construct_sdtr(ahc, AHC_INB(ahc, MSG_LEN),
   1822 					   ahc_syncrates[i].period,
   1823 					   target_scratch & WIDEXFER ?
   1824 					   MAX_OFFSET_16BIT : MAX_OFFSET_8BIT);
   1825 		} else
   1826 			panic("ahc_intr: AWAITING_MSG for an SCB that "
   1827 			      "does not have a waiting message");
   1828 		break;
   1829 	}
   1830 	case IMMEDDONE:
   1831 	{
   1832 		/*
   1833 		 * Take care of device reset messages
   1834 		 */
   1835 		u_char scbindex = AHC_INB(ahc, SCB_TAG);
   1836 		scb = ahc->scbarray[scbindex];
   1837 		if (scb->flags & SCB_DEVICE_RESET) {
   1838 			u_char targ_scratch;
   1839 			int found;
   1840 			/*
   1841 			 * Go back to async/narrow transfers and
   1842 			 * renegotiate.
   1843 			 */
   1844 			ahc_unbusy_target(ahc, target, channel);
   1845 			ahc->needsdtr |= ahc->needsdtr_orig & targ_mask;
   1846 			ahc->needwdtr |= ahc->needwdtr_orig & targ_mask;
   1847 			ahc->sdtrpending &= ~targ_mask;
   1848 			ahc->wdtrpending &= ~targ_mask;
   1849 			targ_scratch = AHC_INB(ahc, TARG_SCRATCH
   1850 					       + scratch_offset);
   1851 			targ_scratch &= SXFR;
   1852 			AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset,
   1853 				 targ_scratch);
   1854 			found = ahc_reset_device(ahc, target,
   1855 						 channel, SCB_LIST_NULL,
   1856 						 XS_NOERROR);
   1857 			sc_print_addr(scb->xs->sc_link);
   1858 			printf("Bus Device Reset delivered. "
   1859 			       "%d SCBs aborted\n", found);
   1860 			ahc->in_timeout = FALSE;
   1861 			ahc_run_done_queue(ahc);
   1862 		} else
   1863 			panic("ahc_intr: Immediate complete for "
   1864 			      "unknown operation.");
   1865 		break;
   1866 	}
   1867 	case DATA_OVERRUN:
   1868 	{
   1869 		/*
   1870 		 * When the sequencer detects an overrun, it
   1871 		 * sets STCNT to 0x00ffffff and allows the
   1872 		 * target to complete its transfer in
   1873 		 * BITBUCKET mode.
   1874 		 */
   1875 		u_char scbindex = AHC_INB(ahc, SCB_TAG);
   1876 		u_int32_t overrun;
   1877 		scb = ahc->scbarray[scbindex];
   1878 		overrun = AHC_INB(ahc, STCNT0)
   1879 			| (AHC_INB(ahc, STCNT1) << 8)
   1880 			| (AHC_INB(ahc, STCNT2) << 16);
   1881 		overrun = 0x00ffffff - overrun;
   1882 		sc_print_addr(scb->xs->sc_link);
   1883 		printf("data overrun of %d bytes detected."
   1884 		       "  Forcing a retry.\n", overrun);
   1885 		/*
   1886 		 * Set this and it will take affect when the
   1887 		 * target does a command complete.
   1888 		 */
   1889 		scb->xs->error = XS_DRIVER_STUFFUP;
   1890 		break;
   1891 	}
   1892 #if NOT_YET
   1893 	/* XXX Fill these in later */
   1894 	case MESG_BUFFER_BUSY:
   1895 		break;
   1896 	case MSGIN_PHASEMIS:
   1897 		break;
   1898 #endif
   1899 	default:
   1900 		printf("ahc_intr: seqint, "
   1901 		       "intstat == 0x%x, scsisigi = 0x%x\n",
   1902 		       intstat, AHC_INB(ahc, SCSISIGI));
   1903 		break;
   1904 	}
   1905 
   1906 clear:
   1907 	/*
   1908 	 * Clear the upper byte that holds SEQINT status
   1909 	 * codes and clear the SEQINT bit.
   1910 	 */
   1911 	AHC_OUTB(ahc, CLRINT, CLRSEQINT);
   1912 
   1913 	/*
   1914 	 *  The sequencer is paused immediately on
   1915 	 *  a SEQINT, so we should restart it when
   1916 	 *  we're done.
   1917 	 */
   1918 	unpause_sequencer(ahc, /*unpause_always*/TRUE);
   1919 }
   1920 
   1921 /*
   1922  * We have a scb which has been processed by the
   1923  * adaptor, now we look to see how the operation
   1924  * went.
   1925  */
   1926 static void
   1927 ahc_done(ahc, scb)
   1928 	struct ahc_data *ahc;
   1929 	struct scb *scb;
   1930 {
   1931 	struct scsi_xfer *xs = scb->xs;
   1932 
   1933 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahc_done\n"));
   1934 	/*
   1935 	 * Put the results of the operation
   1936 	 * into the xfer and call whoever started it
   1937 	 */
   1938 #if defined(__NetBSD__)
   1939 	if (xs->error != XS_NOERROR) {
   1940 		/* Don't override the error value. */
   1941 	} else if (scb->flags & SCB_ABORTED) {
   1942 		xs->error = XS_DRIVER_STUFFUP;
   1943 	} else
   1944 #endif
   1945 	if(scb->flags & SCB_SENSE)
   1946 		xs->error = XS_SENSE;
   1947 	if(scb->flags & SCB_SENTORDEREDTAG)
   1948 		ahc->in_timeout = FALSE;
   1949 #if defined(__FreeBSD__)
   1950 	if ((xs->flags & SCSI_ERR_OK) && !(xs->error == XS_SENSE)) {
   1951 		/* All went correctly  OR errors expected */
   1952 		xs->error = XS_NOERROR;
   1953 	}
   1954 #elif defined(__NetBSD__)
   1955 	/*
   1956 	 * Since NetBSD doesn't have error ignoring operation mode
   1957 	 * (SCSI_ERR_OK in FreeBSD), we don't have to care this case.
   1958 	 */
   1959 #endif
   1960 	xs->flags |= ITSDONE;
   1961 #ifdef AHC_TAGENABLE
   1962 	if(xs->cmd->opcode == INQUIRY && xs->error == XS_NOERROR)
   1963 	{
   1964 		struct scsi_inquiry_data *inq_data;
   1965 		u_short mask = 0x01 << (xs->sc_link->target |
   1966 				(scb->tcl & 0x08));
   1967 		/*
   1968 		 * Sneak a look at the results of the SCSI Inquiry
   1969 		 * command and see if we can do Tagged queing.  This
   1970 		 * should really be done by the higher level drivers.
   1971 		 */
   1972 		inq_data = (struct scsi_inquiry_data *)xs->data;
   1973 		if((inq_data->flags & SID_CmdQue) && !(ahc->tagenable & mask))
   1974 		{
   1975 		        printf("%s: target %d Tagged Queuing Device\n",
   1976 				ahc_name(ahc), xs->sc_link->target);
   1977 			ahc->tagenable |= mask;
   1978 			if(ahc->maxhscbs >= 16 || (ahc->flags & AHC_PAGESCBS)) {
   1979 				/* Default to 8 tags */
   1980 				xs->sc_link->opennings += 6;
   1981 			}
   1982 			else
   1983 			{
   1984 				/*
   1985 				 * Default to 4 tags on whimpy
   1986 				 * cards that don't have much SCB
   1987 				 * space and can't page.  This prevents
   1988 				 * a single device from hogging all
   1989 				 * slots.  We should really have a better
   1990 				 * way of providing fairness.
   1991 				 */
   1992 				xs->sc_link->opennings += 2;
   1993 			}
   1994 		}
   1995 	}
   1996 #endif
   1997 	ahc_free_scb(ahc, scb, xs->flags);
   1998 	scsi_done(xs);
   1999 
   2000 #if defined(__NetBSD__)			/* XXX */
   2001 	/*
   2002 	 * If there are entries in the software queue, try to
   2003 	 * run the first one.  We should be more or less guaranteed
   2004 	 * to succeed, since we just freed an SCB.
   2005 	 *
   2006 	 * NOTE: ahc_scsi_cmd() relies on our calling it with
   2007 	 * the first entry in the queue.
   2008 	 */
   2009 	if (ahc->sc_xxxq.lh_first != NULL)
   2010 		(void) ahc_scsi_cmd(ahc->sc_xxxq.lh_first);
   2011 #endif /* __NetBSD__ */
   2012 }
   2013 
   2014 /*
   2015  * Start the board, ready for normal operation
   2016  */
   2017 int
   2018 ahc_init(ahc)
   2019 	struct  ahc_data *ahc;
   2020 {
   2021 	u_int8_t  scsi_conf, sblkctl, i;
   2022 	u_int16_t ultraenable = 0;
   2023 	int	  max_targ = 15;
   2024 	/*
   2025 	 * Assume we have a board at this stage and it has been reset.
   2026 	 */
   2027 
   2028 	/* Handle the SCBPAGING option */
   2029 #ifndef AHC_SCBPAGING_ENABLE
   2030 	ahc->flags &= ~AHC_PAGESCBS;
   2031 #endif
   2032 
   2033 	/* Determine channel configuration and who we are on the scsi bus. */
   2034 	switch ( (sblkctl = AHC_INB(ahc, SBLKCTL) & 0x0a) ) {
   2035 	    case 0:
   2036 		ahc->our_id = (AHC_INB(ahc, SCSICONF) & HSCSIID);
   2037 		ahc->flags &= ~AHC_CHANNEL_B_PRIMARY;
   2038 		if(ahc->type == AHC_394)
   2039 			printf("Channel %c, SCSI Id=%d, ",
   2040 				ahc->flags & AHC_CHNLB ? 'B' : 'A',
   2041 				ahc->our_id);
   2042 		else
   2043 			printf("Single Channel, SCSI Id=%d, ", ahc->our_id);
   2044 		AHC_OUTB(ahc, FLAGS, SINGLE_BUS | (ahc->flags & AHC_PAGESCBS));
   2045 		break;
   2046 	    case 2:
   2047 		ahc->our_id = (AHC_INB(ahc, SCSICONF + 1) & HWSCSIID);
   2048 		ahc->flags &= ~AHC_CHANNEL_B_PRIMARY;
   2049 		if(ahc->type == AHC_394)
   2050 			printf("Wide Channel %c, SCSI Id=%d, ",
   2051 				ahc->flags & AHC_CHNLB ? 'B' : 'A',
   2052 				ahc->our_id);
   2053 		else
   2054 			printf("Wide Channel, SCSI Id=%d, ", ahc->our_id);
   2055 		ahc->type |= AHC_WIDE;
   2056 		AHC_OUTB(ahc, FLAGS, WIDE_BUS | (ahc->flags & AHC_PAGESCBS));
   2057 		break;
   2058 	    case 8:
   2059 		ahc->our_id = (AHC_INB(ahc, SCSICONF) & HSCSIID);
   2060 		ahc->our_id_b = (AHC_INB(ahc, SCSICONF + 1) & HSCSIID);
   2061 		printf("Twin Channel, A SCSI Id=%d, B SCSI Id=%d, ",
   2062 			ahc->our_id, ahc->our_id_b);
   2063 		ahc->type |= AHC_TWIN;
   2064 		AHC_OUTB(ahc, FLAGS, TWIN_BUS | (ahc->flags & AHC_PAGESCBS));
   2065 		break;
   2066 	    default:
   2067 		printf(" Unsupported adapter type.  Ignoring\n");
   2068 		return(-1);
   2069 	}
   2070 
   2071 	/* Determine the number of SCBs */
   2072 
   2073 	{
   2074 		AHC_OUTB(ahc, SCBPTR, 0);
   2075 		AHC_OUTB(ahc, SCB_CONTROL, 0);
   2076 		for(i = 1; i < AHC_SCB_MAX; i++) {
   2077 			AHC_OUTB(ahc, SCBPTR, i);
   2078 			AHC_OUTB(ahc, SCB_CONTROL, i);
   2079 			if(AHC_INB(ahc, SCB_CONTROL) != i)
   2080 				break;
   2081 			AHC_OUTB(ahc, SCBPTR, 0);
   2082 			if(AHC_INB(ahc, SCB_CONTROL) != 0)
   2083 				break;
   2084 			/* Clear the control byte. */
   2085 			AHC_OUTB(ahc, SCBPTR, i);
   2086 			AHC_OUTB(ahc, SCB_CONTROL, 0);
   2087 
   2088 			ahc->qcntmask |= i;     /* Update the count mask. */
   2089 		}
   2090 
   2091 		/* Ensure we clear the 0 SCB's control byte. */
   2092 		AHC_OUTB(ahc, SCBPTR, 0);
   2093 		AHC_OUTB(ahc, SCB_CONTROL, 0);
   2094 
   2095 		ahc->qcntmask |= i;
   2096 		ahc->maxhscbs = i;
   2097 	}
   2098 
   2099 	if((ahc->maxhscbs < AHC_SCB_MAX) && (ahc->flags & AHC_PAGESCBS))
   2100 		ahc->maxscbs = AHC_SCB_MAX;
   2101 	else {
   2102 		ahc->maxscbs = ahc->maxhscbs;
   2103 		ahc->flags &= ~AHC_PAGESCBS;
   2104 	}
   2105 
   2106 	printf("%d SCBs\n", ahc->maxhscbs);
   2107 
   2108 #ifdef AHC_DEBUG
   2109 	if(ahc_debug & AHC_SHOWMISC) {
   2110 		struct scb	test;
   2111 		printf("%s: hardware scb %ld bytes; kernel scb; "
   2112 		       "ahc_dma %d bytes\n",
   2113 			ahc_name(ahc),
   2114 		        (u_long)&(test.next) - (u_long)(&test),
   2115 			sizeof(test),
   2116 			sizeof(struct ahc_dma_seg));
   2117 	}
   2118 #endif /* AHC_DEBUG */
   2119 
   2120 	/* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
   2121 	if(ahc->type & AHC_TWIN)
   2122 	{
   2123 		/*
   2124 		 * The device is gated to channel B after a chip reset,
   2125 		 * so set those values first
   2126 		 */
   2127 		AHC_OUTB(ahc, SCSIID, ahc->our_id_b);
   2128 		scsi_conf = AHC_INB(ahc, SCSICONF + 1);
   2129 		AHC_OUTB(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
   2130 					| ENSTIMER|ACTNEGEN|STPWEN);
   2131 		AHC_OUTB(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
   2132 		if(ahc->type & AHC_ULTRA)
   2133 			AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN|ULTRAEN);
   2134 		else
   2135 			AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN);
   2136 
   2137 		if(scsi_conf & RESET_SCSI) {
   2138 			/* Reset the bus */
   2139 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
   2140 			if(bootverbose)
   2141 				printf("%s: Reseting Channel B\n",
   2142 				       ahc_name(ahc));
   2143 #endif
   2144 			AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
   2145 			DELAY(1000);
   2146 			AHC_OUTB(ahc, SCSISEQ, 0);
   2147 
   2148 			/* Ensure we don't get a RSTI interrupt from this */
   2149 			AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI);
   2150 			AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   2151 		}
   2152 
   2153 		/* Select Channel A */
   2154 		AHC_OUTB(ahc, SBLKCTL, 0);
   2155 	}
   2156 	AHC_OUTB(ahc, SCSIID, ahc->our_id);
   2157 	scsi_conf = AHC_INB(ahc, SCSICONF);
   2158 	AHC_OUTB(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
   2159 				| ENSTIMER|ACTNEGEN|STPWEN);
   2160 	AHC_OUTB(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
   2161 	if(ahc->type & AHC_ULTRA)
   2162 		AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN|ULTRAEN);
   2163 	else
   2164 		AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN);
   2165 
   2166 	if(scsi_conf & RESET_SCSI) {
   2167 		/* Reset the bus */
   2168 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
   2169 		if(bootverbose)
   2170 			printf("%s: Reseting Channel A\n", ahc_name(ahc));
   2171 #endif
   2172 
   2173 		AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
   2174 		DELAY(1000);
   2175 		AHC_OUTB(ahc, SCSISEQ, 0);
   2176 
   2177 		/* Ensure we don't get a RSTI interrupt from this */
   2178 		AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI);
   2179 		AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   2180 	}
   2181 
   2182 	/*
   2183 	 * Look at the information that board initialization or
   2184 	 * the board bios has left us.  In the lower four bits of each
   2185 	 * target's scratch space any value other than 0 indicates
   2186 	 * that we should initiate syncronous transfers.  If it's zero,
   2187 	 * the user or the BIOS has decided to disable syncronous
   2188 	 * negotiation to that target so we don't activate the needsdtr
   2189 	 * flag.
   2190 	 */
   2191 	ahc->needsdtr_orig = 0;
   2192 	ahc->needwdtr_orig = 0;
   2193 
   2194 	/* Grab the disconnection disable table and invert it for our needs */
   2195 	if(ahc->flags & AHC_USEDEFAULTS) {
   2196 		printf("%s: Host Adapter Bios disabled.  Using default SCSI "
   2197 			"device parameters\n", ahc_name(ahc));
   2198 		ahc->discenable = 0xff;
   2199 	}
   2200 	else
   2201 		ahc->discenable = ~((AHC_INB(ahc, DISC_DSB + 1) << 8)
   2202 				   | AHC_INB(ahc, DISC_DSB));
   2203 
   2204 	if(!(ahc->type & (AHC_WIDE|AHC_TWIN)))
   2205 		max_targ = 7;
   2206 
   2207 	for(i = 0; i <= max_targ; i++){
   2208 		u_char target_settings;
   2209 		if (ahc->flags & AHC_USEDEFAULTS) {
   2210 			target_settings = 0; /* 10MHz */
   2211 			ahc->needsdtr_orig |= (0x01 << i);
   2212 			ahc->needwdtr_orig |= (0x01 << i);
   2213 		}
   2214 		else {
   2215 			/* Take the settings leftover in scratch RAM. */
   2216 			target_settings = AHC_INB(ahc, TARG_SCRATCH + i);
   2217 
   2218 			if(target_settings & 0x0f){
   2219 				ahc->needsdtr_orig |= (0x01 << i);
   2220 				/*Default to a asyncronous transfers(0 offset)*/
   2221 				target_settings &= 0xf0;
   2222 			}
   2223 			if(target_settings & 0x80){
   2224 				ahc->needwdtr_orig |= (0x01 << i);
   2225 				/*
   2226 				 * We'll set the Wide flag when we
   2227 				 * are successful with Wide negotiation.
   2228 				 * Turn it off for now so we aren't
   2229 				 * confused.
   2230 				 */
   2231 				target_settings &= 0x7f;
   2232 			}
   2233 			if(ahc->type & AHC_ULTRA) {
   2234 				/*
   2235 				 * Enable Ultra for any target that
   2236 				 * has a valid ultra syncrate setting.
   2237 				 */
   2238 				u_char rate = target_settings & 0x70;
   2239 				if(rate == 0x00 || rate == 0x10 ||
   2240 				   rate == 0x20 || rate == 0x40) {
   2241 					if(rate == 0x40) {
   2242 						/* Treat 10MHz specially */
   2243 						target_settings &= ~0x70;
   2244 					}
   2245 					else
   2246 						ultraenable |= (0x01 << i);
   2247 				}
   2248 			}
   2249 		}
   2250 		AHC_OUTB(ahc, TARG_SCRATCH+i,target_settings);
   2251 	}
   2252 	/*
   2253 	 * If we are not a WIDE device, forget WDTR.  This
   2254 	 * makes the driver work on some cards that don't
   2255 	 * leave these fields cleared when the BIOS is not
   2256 	 * installed.
   2257 	 */
   2258 	if(!(ahc->type & AHC_WIDE))
   2259 		ahc->needwdtr_orig = 0;
   2260 	ahc->needsdtr = ahc->needsdtr_orig;
   2261 	ahc->needwdtr = ahc->needwdtr_orig;
   2262 	ahc->sdtrpending = 0;
   2263 	ahc->wdtrpending = 0;
   2264 	ahc->tagenable = 0;
   2265 	ahc->orderedtag = 0;
   2266 
   2267 	AHC_OUTB(ahc, ULTRA_ENB, ultraenable & 0xff);
   2268 	AHC_OUTB(ahc, ULTRA_ENB + 1, (ultraenable >> 8) & 0xff);
   2269 
   2270 #ifdef AHC_DEBUG
   2271 	/* How did we do? */
   2272 	if(ahc_debug & AHC_SHOWMISC)
   2273 		printf("NEEDSDTR == 0x%x\nNEEDWDTR == 0x%x\n"
   2274 			"DISCENABLE == 0x%x\n", ahc->needsdtr,
   2275 			ahc->needwdtr, ahc->discenable);
   2276 #endif
   2277 	/*
   2278 	 * Set the number of availible SCBs
   2279 	 */
   2280 	AHC_OUTB(ahc, SCBCOUNT, ahc->maxhscbs);
   2281 
   2282 	/*
   2283 	 * 2's compliment of maximum tag value
   2284 	 */
   2285 	i = ahc->maxscbs;
   2286 	AHC_OUTB(ahc, COMP_SCBCOUNT, -i & 0xff);
   2287 
   2288 	/*
   2289 	 * QCount mask to deal with broken aic7850s that
   2290 	 * sporatically get garbage in the upper bits of
   2291 	 * their QCount registers.
   2292 	 */
   2293 	AHC_OUTB(ahc, QCNTMASK, ahc->qcntmask);
   2294 
   2295 	/* We don't have any busy targets right now */
   2296 	AHC_OUTB(ahc, ACTIVE_A, 0);
   2297 	AHC_OUTB(ahc, ACTIVE_B, 0);
   2298 
   2299 	/* We don't have any waiting selections */
   2300 	AHC_OUTB(ahc, WAITING_SCBH, SCB_LIST_NULL);
   2301 
   2302 	/* Our disconnection list is empty too */
   2303 	AHC_OUTB(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
   2304 
   2305 	/* Message out buffer starts empty */
   2306 	AHC_OUTB(ahc, MSG_LEN, 0x00);
   2307 
   2308 	/*
   2309 	 * Load the Sequencer program and Enable the adapter
   2310 	 * in "fast" mode.
   2311          */
   2312 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
   2313 	if(bootverbose)
   2314 		printf("%s: Downloading Sequencer Program...",
   2315 		       ahc_name(ahc));
   2316 #endif
   2317 
   2318 	ahc_loadseq(ahc);
   2319 
   2320 #if !defined(__NetBSD__) || (defined(__NetBSD__) && defined(DEBUG))
   2321 	if(bootverbose)
   2322 		printf("Done\n");
   2323 #endif
   2324 
   2325 	AHC_OUTB(ahc, SEQCTL, FASTMODE);
   2326 
   2327 	unpause_sequencer(ahc, /*unpause_always*/TRUE);
   2328 
   2329 	/*
   2330 	 * Note that we are going and return (to probe)
   2331 	 */
   2332 	ahc->flags |= AHC_INIT;
   2333 	return (0);
   2334 }
   2335 
   2336 static void
   2337 ahcminphys(bp)
   2338         struct buf *bp;
   2339 {
   2340 /*
   2341  * Even though the card can transfer up to 16megs per command
   2342  * we are limited by the number of segments in the dma segment
   2343  * list that we can hold.  The worst case is that all pages are
   2344  * discontinuous physically, hense the "page per segment" limit
   2345  * enforced here.
   2346  */
   2347         if (bp->b_bcount > ((AHC_NSEG - 1) * PAGE_SIZE)) {
   2348                 bp->b_bcount = ((AHC_NSEG - 1) * PAGE_SIZE);
   2349         }
   2350 #if defined(__NetBSD__)
   2351 	minphys(bp);
   2352 #endif
   2353 }
   2354 
   2355 #if defined(__NetBSD__)			/* XXX */
   2356 /*
   2357  * Insert a scsi_xfer into the software queue.  We overload xs->free_list
   2358  * to to ensure we don't run into a queue resource shortage, and keep
   2359  * a pointer to the last entry around to make insertion O(C).
   2360  */
   2361 static void
   2362 ahc_xxx_enqueue(ahc, xs, infront)
   2363 	struct ahc_data *ahc;
   2364 	struct scsi_xfer *xs;
   2365 	int infront;
   2366 {
   2367 
   2368 	if (infront || ahc->sc_xxxq.lh_first == NULL) {
   2369 		if (ahc->sc_xxxq.lh_first == NULL)
   2370 			ahc->sc_xxxqlast = xs;
   2371 		LIST_INSERT_HEAD(&ahc->sc_xxxq, xs, free_list);
   2372 		return;
   2373 	}
   2374 
   2375 	LIST_INSERT_AFTER(ahc->sc_xxxqlast, xs, free_list);
   2376 	ahc->sc_xxxqlast = xs;
   2377 }
   2378 
   2379 /*
   2380  * Pull a scsi_xfer off the front of the software queue.  When we
   2381  * pull the last one off, we need to clear the pointer to the last
   2382  * entry.
   2383  */
   2384 static struct scsi_xfer *
   2385 ahc_xxx_dequeue(ahc)
   2386 	struct ahc_data *ahc;
   2387 {
   2388 	struct scsi_xfer *xs;
   2389 
   2390 	xs = ahc->sc_xxxq.lh_first;
   2391 	LIST_REMOVE(xs, free_list);
   2392 
   2393 	if (ahc->sc_xxxq.lh_first == NULL)
   2394 		ahc->sc_xxxqlast = NULL;
   2395 
   2396 	return (xs);
   2397 }
   2398 #endif
   2399 
   2400 /*
   2401  * start a scsi operation given the command and
   2402  * the data address, target, and lun all of which
   2403  * are stored in the scsi_xfer struct
   2404  */
   2405 static int32_t
   2406 ahc_scsi_cmd(xs)
   2407         struct scsi_xfer *xs;
   2408 {
   2409 	struct	scb *scb;
   2410 	struct	ahc_dma_seg *sg;
   2411 	int	seg;		/* scatter gather seg being worked on */
   2412 	int	thiskv;
   2413 	physaddr thisphys, nextphys;
   2414 	int	bytes_this_seg, bytes_this_page, datalen, flags;
   2415 	struct	ahc_data *ahc;
   2416 	u_short	mask;
   2417 	int	s;
   2418 #if defined(__NetBSD__)			/* XXX */
   2419 	int	dontqueue = 0, fromqueue = 0;
   2420 #endif
   2421 
   2422 	ahc = (struct ahc_data *)xs->sc_link->adapter_softc;
   2423 	mask = (0x01 << (xs->sc_link->target
   2424 #if defined(__FreeBSD__)
   2425 				| ((u_long)xs->sc_link->fordriver & 0x08)));
   2426 #elif defined(__NetBSD__)
   2427 			| (IS_SCSIBUS_B(ahc, xs->sc_link) ? SELBUSB : 0) ));
   2428 #endif
   2429 
   2430 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahc_scsi_cmd\n"));
   2431 
   2432 #if defined(__NetBSD__)			/* XXX */
   2433 	/* must protect the queue */
   2434 	s = splbio();
   2435 
   2436 	/*
   2437 	 * If we're running the queue from ahc_done(), we're called
   2438 	 * with the first entry in the queue as our argument.
   2439 	 * Pull it off; if we can't run the job, it will get placed
   2440 	 * back at the front.
   2441 	 */
   2442 	if (xs == ahc->sc_xxxq.lh_first) {
   2443 		xs = ahc_xxx_dequeue(ahc);
   2444 		fromqueue = 1;
   2445 		goto get_scb;
   2446 	}
   2447 
   2448 	/* determine safey of software queueing */
   2449 	dontqueue = xs->flags & SCSI_POLL;
   2450 
   2451 	/*
   2452 	 * Handle situations where there's already entries in the
   2453 	 * queue.
   2454 	 */
   2455 	if (ahc->sc_xxxq.lh_first != NULL) {
   2456 		/*
   2457 		 * If we can't queue, we have to abort, since
   2458 		 * we have to preserve order.
   2459 		 */
   2460 		if (dontqueue) {
   2461 			splx(s);
   2462 			xs->error = XS_DRIVER_STUFFUP;
   2463 			return (TRY_AGAIN_LATER);
   2464 		}
   2465 
   2466 		/*
   2467 		 * Swap with the first queue entry.
   2468 		 */
   2469 		ahc_xxx_enqueue(ahc, xs, 0);
   2470 		xs = ahc_xxx_dequeue(ahc);
   2471 		fromqueue = 1;
   2472 	}
   2473 
   2474  get_scb:
   2475 #endif /* __NetBSD__ */
   2476 	/*
   2477 	 * get an scb to use. If the transfer
   2478 	 * is from a buf (possibly from interrupt time)
   2479 	 * then we can't allow it to sleep
   2480 	 */
   2481 	flags = xs->flags;
   2482 	if (flags & ITSDONE) {
   2483 		printf("%s: Already done?", ahc_name(ahc));
   2484 		xs->flags &= ~ITSDONE;
   2485 	}
   2486 	if (!(flags & INUSE)) {
   2487 		printf("%s: Not in use?", ahc_name(ahc));
   2488 		xs->flags |= INUSE;
   2489 	}
   2490 	if (!(scb = ahc_get_scb(ahc, flags))) {
   2491 #if defined(__NetBSD__)			/* XXX */
   2492 		/*
   2493 		 * If we can't queue, we lose.
   2494 		 */
   2495 		if (dontqueue) {
   2496 			splx(s);
   2497 			xs->error = XS_DRIVER_STUFFUP;
   2498 			return (TRY_AGAIN_LATER);
   2499 		}
   2500 
   2501 		/*
   2502 		 * If we were pulled off the queue, put ourselves
   2503 		 * back in the front, otherwise tack ourselves onto
   2504 		 * the end.
   2505 		 */
   2506 		ahc_xxx_enqueue(ahc, xs, fromqueue);
   2507 
   2508 		splx(s);
   2509 		return (SUCCESSFULLY_QUEUED);
   2510 #else
   2511 		xs->error = XS_DRIVER_STUFFUP;
   2512 		return (TRY_AGAIN_LATER);
   2513 #endif /* __NetBSD__ */
   2514 	}
   2515 
   2516 #if defined(__NetBSD__)
   2517 	/* we're done playing with the queue */
   2518 	splx(s);
   2519 #endif
   2520 
   2521 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("start scb(%p)\n", scb));
   2522 	scb->xs = xs;
   2523 	if (flags & SCSI_RESET) {
   2524 		scb->flags |= SCB_DEVICE_RESET|SCB_IMMED;
   2525 		scb->control |= MK_MESSAGE;
   2526 	}
   2527 	/*
   2528 	 * Put all the arguments for the xfer in the scb
   2529 	 */
   2530 
   2531 	if(ahc->tagenable & mask) {
   2532 		scb->control |= TAG_ENB;
   2533 		if(ahc->orderedtag & mask) {
   2534 			printf("Ordered Tag sent\n");
   2535 			scb->control |= 0x02;
   2536 			ahc->orderedtag &= ~mask;
   2537 		}
   2538 	}
   2539 	if(ahc->discenable & mask)
   2540 		scb->control |= DISCENB;
   2541 	if((ahc->needwdtr & mask) && !(ahc->wdtrpending & mask))
   2542 	{
   2543 		scb->control |= MK_MESSAGE;
   2544 		scb->flags |= SCB_MSGOUT_WDTR;
   2545 		ahc->wdtrpending |= mask;
   2546 	}
   2547 	else if((ahc->needsdtr & mask) && !(ahc->sdtrpending & mask))
   2548 	{
   2549 		scb->control |= MK_MESSAGE;
   2550 		scb->flags |= SCB_MSGOUT_SDTR;
   2551 		ahc->sdtrpending |= mask;
   2552 	}
   2553 	scb->tcl = ((xs->sc_link->target << 4) & 0xF0) |
   2554 #if defined(__FreeBSD__)
   2555 				  ((u_long)xs->sc_link->fordriver & 0x08) |
   2556 #elif defined(__NetBSD__)
   2557 				  (IS_SCSIBUS_B(ahc,xs->sc_link)? SELBUSB : 0)|
   2558 #endif
   2559 				  (xs->sc_link->lun & 0x07);
   2560 	scb->cmdlen = xs->cmdlen;
   2561 	scb->cmdpointer = KVTOPHYS(xs->cmd);
   2562 	xs->resid = 0;
   2563 	xs->status = 0;
   2564 	if (xs->datalen) {      /* should use S/G only if not zero length */
   2565 		scb->SG_list_pointer = KVTOPHYS(scb->ahc_dma);
   2566 		sg = scb->ahc_dma;
   2567 		seg = 0;
   2568 		/*
   2569 		 * Set up the scatter gather block
   2570 		 */
   2571 		SC_DEBUG(xs->sc_link, SDEV_DB4,
   2572 			 ("%ld @%p:- ", xs->datalen, xs->data));
   2573 		datalen = xs->datalen;
   2574 		thiskv = (int) xs->data;
   2575 		thisphys = KVTOPHYS(thiskv);
   2576 
   2577 		while ((datalen) && (seg < AHC_NSEG)) {
   2578 			bytes_this_seg = 0;
   2579 
   2580 			/* put in the base address */
   2581 			sg->addr = thisphys;
   2582 
   2583 			SC_DEBUGN(xs->sc_link, SDEV_DB4, ("0x%lx", thisphys));
   2584 
   2585 			/* do it at least once */
   2586 			nextphys = thisphys;
   2587 			while ((datalen) && (thisphys == nextphys)) {
   2588 				/*
   2589 				 * This page is contiguous (physically)
   2590 				 * with the the last, just extend the
   2591 				 * length
   2592 				 */
   2593 				/* how far to the end of the page */
   2594 				nextphys = (thisphys & (~(PAGE_SIZE- 1)))
   2595 					   + PAGE_SIZE;
   2596 				bytes_this_page = nextphys - thisphys;
   2597 				/**** or the data ****/
   2598 				bytes_this_page = min(bytes_this_page ,datalen);
   2599 				bytes_this_seg += bytes_this_page;
   2600 				datalen -= bytes_this_page;
   2601 
   2602 				/* get more ready for the next page */
   2603 				thiskv = (thiskv & (~(PAGE_SIZE - 1)))
   2604 					 + PAGE_SIZE;
   2605 				if (datalen)
   2606 					thisphys = KVTOPHYS(thiskv);
   2607 			}
   2608 			/*
   2609 			 * next page isn't contiguous, finish the seg
   2610 			 */
   2611 			SC_DEBUGN(xs->sc_link, SDEV_DB4,
   2612 					("(0x%x)", bytes_this_seg));
   2613 			sg->len = bytes_this_seg;
   2614 			sg++;
   2615 			seg++;
   2616 		}
   2617 		scb->SG_segment_count = seg;
   2618 
   2619 		/* Copy the first SG into the data pointer area */
   2620 		scb->data = scb->ahc_dma->addr;
   2621 		scb->datalen = scb->ahc_dma->len;
   2622 		SC_DEBUGN(xs->sc_link, SDEV_DB4, ("\n"));
   2623 		if (datalen) {
   2624 			/* there's still data, must have run out of segs! */
   2625 			printf("%s: ahc_scsi_cmd: more than %d DMA segs\n",
   2626 				ahc_name(ahc), AHC_NSEG);
   2627 			xs->error = XS_DRIVER_STUFFUP;
   2628 			ahc_free_scb(ahc, scb, flags);
   2629 			return (COMPLETE);
   2630 		}
   2631 #ifdef AHC_BROKEN_CACHE
   2632 		if (ahc_broken_cache)
   2633 			INVALIDATE_CACHE();
   2634 #endif
   2635 	}
   2636 	else {
   2637 		/*
   2638 		 * No data xfer, use non S/G values
   2639 	 	 */
   2640 		scb->SG_segment_count = 0;
   2641 		scb->SG_list_pointer = 0;
   2642 		scb->data = 0;
   2643 		scb->datalen = 0;
   2644 	}
   2645 
   2646 #ifdef AHC_DEBUG
   2647 	if((ahc_debug & AHC_SHOWSCBS) && (xs->sc_link->target == DEBUGTARG))
   2648 		ahc_print_scb(scb);
   2649 #endif
   2650 	s = splbio();
   2651 
   2652 	if( scb->position != SCB_LIST_NULL )
   2653 	{
   2654 		/* We already have a valid slot */
   2655 		u_char curscb;
   2656 
   2657 		pause_sequencer(ahc);
   2658 		curscb = AHC_INB(ahc, SCBPTR);
   2659 		AHC_OUTB(ahc, SCBPTR, scb->position);
   2660 		ahc_send_scb(ahc, scb);
   2661 		AHC_OUTB(ahc, SCBPTR, curscb);
   2662 		AHC_OUTB(ahc, QINFIFO, scb->position);
   2663 		unpause_sequencer(ahc, /*unpause_always*/FALSE);
   2664 		scb->flags |= SCB_ACTIVE;
   2665 		if (!(flags & SCSI_NOMASK)) {
   2666 			timeout(ahc_timeout, (caddr_t)scb,
   2667 				(xs->timeout * hz) / 1000);
   2668 		}
   2669 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_sent\n"));
   2670 	}
   2671 	else {
   2672 		scb->flags |= SCB_WAITINGQ;
   2673 		STAILQ_INSERT_TAIL(&ahc->waiting_scbs, scb, links);
   2674 		ahc_run_waiting_queues(ahc);
   2675 	}
   2676 	if (!(flags & SCSI_NOMASK)) {
   2677 		splx(s);
   2678 		return (SUCCESSFULLY_QUEUED);
   2679 	}
   2680 	/*
   2681 	 * If we can't use interrupts, poll for completion
   2682 	 */
   2683 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_poll\n"));
   2684 	do {
   2685 		if (ahc_poll(ahc, xs->timeout)) {
   2686 			if (!(xs->flags & SCSI_SILENT))
   2687 				printf("cmd fail\n");
   2688 			ahc_timeout(scb);
   2689 			break;
   2690 		}
   2691 	} while (!(xs->flags & ITSDONE));  /* a non command complete intr */
   2692 	splx(s);
   2693 	return (COMPLETE);
   2694 }
   2695 
   2696 
   2697 /*
   2698  * A scb (and hence an scb entry on the board is put onto the
   2699  * free list.
   2700  */
   2701 static void
   2702 ahc_free_scb(ahc, scb, flags)
   2703         struct	ahc_data *ahc;
   2704         int     flags;
   2705         struct  scb *scb;
   2706 {
   2707 	struct scb *wscb;
   2708 	unsigned int opri;
   2709 
   2710 	opri = splbio();
   2711 
   2712 	/* Clean up for the next user */
   2713 	scb->flags = SCB_FREE;
   2714 	scb->control = 0;
   2715 	scb->status = 0;
   2716 
   2717 	if(scb->position == SCB_LIST_NULL) {
   2718 		STAILQ_INSERT_HEAD(&ahc->page_scbs, scb, links);
   2719 		if(!scb->links.stqe_next && !ahc->free_scbs.stqh_first)
   2720 			/*
   2721 			 * If there were no SCBs availible, wake anybody waiting
   2722 			 * for one to come free.
   2723 			 */
   2724 			wakeup((caddr_t)&ahc->free_scbs);
   2725 	}
   2726 	/*
   2727 	 * If there are any SCBS on the waiting queue,
   2728 	 * assign the slot of this "freed" SCB to the first
   2729 	 * one.  We'll run the waiting queues after all command
   2730 	 * completes for a particular interrupt are completed
   2731 	 * or when we start another command.
   2732 	 */
   2733 	else if((wscb = ahc->waiting_scbs.stqh_first) != NULL) {
   2734 		STAILQ_REMOVE_HEAD(&ahc->waiting_scbs, links);
   2735 		wscb->position = scb->position;
   2736 		STAILQ_INSERT_TAIL(&ahc->assigned_scbs, wscb, links);
   2737 		wscb->flags ^= SCB_WAITINGQ|SCB_ASSIGNEDQ;
   2738 
   2739 		/*
   2740 		 * The "freed" SCB will need to be assigned a slot
   2741 		 * before being used, so put it in the page_scbs
   2742 		 * queue.
   2743 		 */
   2744 		scb->position = SCB_LIST_NULL;
   2745 		STAILQ_INSERT_HEAD(&ahc->page_scbs, scb, links);
   2746 		if(!scb->links.stqe_next && !ahc->free_scbs.stqh_first)
   2747 			/*
   2748 			 * If there were no SCBs availible, wake anybody waiting
   2749 			 * for one to come free.
   2750 			 */
   2751 			wakeup((caddr_t)&ahc->free_scbs);
   2752 	}
   2753 	else {
   2754 		STAILQ_INSERT_HEAD(&ahc->free_scbs, scb, links);
   2755 		if(!scb->links.stqe_next && !ahc->page_scbs.stqh_first)
   2756 			/*
   2757 			 * If there were no SCBs availible, wake anybody waiting
   2758 			 * for one to come free.
   2759 			 */
   2760 			wakeup((caddr_t)&ahc->free_scbs);
   2761 	}
   2762 #ifdef AHC_DEBUG
   2763 	ahc->activescbs--;
   2764 #endif
   2765 	splx(opri);
   2766 }
   2767 
   2768 /*
   2769  * Get a free scb, either one already assigned to a hardware slot
   2770  * on the adapter or one that will require an SCB to be paged out before
   2771  * use. If there are none, see if we can allocate a new SCB.  Otherwise
   2772  * either return an error or sleep.
   2773  */
   2774 static struct scb *
   2775 ahc_get_scb(ahc, flags)
   2776         struct	ahc_data *ahc;
   2777         int	flags;
   2778 {
   2779 	unsigned opri;
   2780 	struct scb *scbp;
   2781 
   2782 	opri = splbio();
   2783 	/*
   2784 	 * If we can and have to, sleep waiting for one to come free
   2785 	 * but only if we can't allocate a new one.
   2786 	 */
   2787 	while (1) {
   2788 		if((scbp = ahc->free_scbs.stqh_first)) {
   2789 			STAILQ_REMOVE_HEAD(&ahc->free_scbs, links);
   2790 		}
   2791 		else if((scbp = ahc->page_scbs.stqh_first)) {
   2792 			STAILQ_REMOVE_HEAD(&ahc->page_scbs, links);
   2793 		}
   2794 		else if(ahc->numscbs < ahc->maxscbs) {
   2795 			scbp = (struct scb *) malloc(sizeof(struct scb),
   2796 				M_TEMP, M_NOWAIT);
   2797 			if (scbp) {
   2798 				bzero(scbp, sizeof(struct scb));
   2799 				scbp->tag = ahc->numscbs;
   2800 				if( ahc->numscbs < ahc->maxhscbs )
   2801 					scbp->position = ahc->numscbs;
   2802 				else
   2803 					scbp->position = SCB_LIST_NULL;
   2804 				ahc->numscbs++;
   2805 				/*
   2806 				 * Place in the scbarray
   2807 				 * Never is removed.
   2808 				 */
   2809 				ahc->scbarray[scbp->tag] = scbp;
   2810 			}
   2811 			else {
   2812 				printf("%s: Can't malloc SCB\n",
   2813 				       ahc_name(ahc));
   2814 			}
   2815 		}
   2816 		else {
   2817 			if (!(flags & SCSI_NOSLEEP)) {
   2818 				tsleep((caddr_t)&ahc->free_scbs, PRIBIO,
   2819 					"ahcscb", 0);
   2820 				continue;
   2821 			}
   2822 		}
   2823 		break;
   2824 	}
   2825 
   2826 #ifdef AHC_DEBUG
   2827 	if (scbp) {
   2828 		ahc->activescbs++;
   2829 		if((ahc_debug & AHC_SHOWSCBCNT)
   2830 		  && (ahc->activescbs == ahc->maxhscbs))
   2831 			printf("%s: Max SCBs active\n", ahc_name(ahc));
   2832 	}
   2833 #endif
   2834 
   2835 	splx(opri);
   2836 
   2837 	return (scbp);
   2838 }
   2839 
   2840 static void ahc_loadseq(ahc)
   2841 	struct ahc_data *ahc;
   2842 {
   2843         static u_char seqprog[] = {
   2844 #if defined(__FreeBSD__)
   2845 #               include "aic7xxx_seq.h"
   2846 #endif
   2847 #if defined(__NetBSD__)
   2848 #		include <dev/microcode/aic7xxx/aic7xxx_seq.h>
   2849 #endif
   2850 	};
   2851 
   2852 	AHC_OUTB(ahc, SEQCTL, PERRORDIS|SEQRESET|LOADRAM);
   2853 
   2854 	AHC_OUTSB(ahc, SEQRAM, seqprog, sizeof(seqprog));
   2855 
   2856 	do {
   2857 		AHC_OUTB(ahc, SEQCTL, SEQRESET|FASTMODE);
   2858 	} while((AHC_INB(ahc, SEQADDR0) != 0)
   2859 		|| (AHC_INB(ahc, SEQADDR1) != 0));
   2860 }
   2861 
   2862 /*
   2863  * Function to poll for command completion when
   2864  * interrupts are disabled (crash dumps)
   2865  */
   2866 static int
   2867 ahc_poll(ahc, wait)
   2868 	struct	ahc_data *ahc;
   2869 	int	wait; /* in msec */
   2870 {
   2871 	while (--wait) {
   2872 		DELAY(1000);
   2873 		if (AHC_INB(ahc, INTSTAT) & INT_PEND)
   2874 			break;
   2875 	} if (wait == 0) {
   2876 		printf("%s: board is not responding\n", ahc_name(ahc));
   2877 		return (EIO);
   2878 	}
   2879 	ahc_intr((void *)ahc);
   2880 	return (0);
   2881 }
   2882 
   2883 static void
   2884 ahc_timeout(arg)
   2885 	void	*arg;
   2886 {
   2887 	struct	scb *scb = (struct scb *)arg;
   2888 	struct	ahc_data *ahc;
   2889 	int	s, found;
   2890 	u_char	bus_state;
   2891 	char	channel;
   2892 
   2893 	s = splbio();
   2894 
   2895 	if (!(scb->flags & SCB_ACTIVE)) {
   2896 		/* Previous timeout took care of me already */
   2897 		splx(s);
   2898 		return;
   2899 	}
   2900 
   2901 	ahc = (struct ahc_data *)scb->xs->sc_link->adapter_softc;
   2902 
   2903 	if (ahc->in_timeout) {
   2904 		/*
   2905 		 * Some other SCB has started a recovery operation
   2906 		 * and is still working on cleaning things up.
   2907 		 */
   2908 		if (scb->flags & SCB_TIMEDOUT) {
   2909 			/*
   2910 			 * This SCB has been here before and is not the
   2911 			 * recovery SCB. Cut our losses and panic.  Its
   2912 			 * better to do this than trash a filesystem.
   2913 			 */
   2914 			panic("%s: Timed-out command times out "
   2915 				"again\n", ahc_name(ahc));
   2916 		}
   2917 		else if (!(scb->flags & SCB_ABORTED))
   2918 		{
   2919 			/*
   2920 			 * This is not the SCB that started this timeout
   2921 			 * processing.  Give this scb another lifetime so
   2922 			 * that it can continue once we deal with the
   2923 			 * timeout.
   2924 			 */
   2925 			scb->flags |= SCB_TIMEDOUT;
   2926 			timeout(ahc_timeout, (caddr_t)scb,
   2927 				(scb->xs->timeout * hz) / 1000);
   2928 			splx(s);
   2929 			return;
   2930 		}
   2931 	}
   2932 	ahc->in_timeout = TRUE;
   2933 
   2934 	/*
   2935 	 * Ensure that the card doesn't do anything
   2936 	 * behind our back.
   2937 	 */
   2938 	pause_sequencer(ahc);
   2939 
   2940 	sc_print_addr(scb->xs->sc_link);
   2941 	printf("timed out ");
   2942 	/*
   2943 	 * Take a snapshot of the bus state and print out
   2944 	 * some information so we can track down driver bugs.
   2945 	 */
   2946 	bus_state = AHC_INB(ahc, LASTPHASE);
   2947 
   2948 	switch(bus_state & PHASE_MASK)
   2949 	{
   2950 		case P_DATAOUT:
   2951 			printf("in dataout phase");
   2952 			break;
   2953 		case P_DATAIN:
   2954 			printf("in datain phase");
   2955 			break;
   2956 		case P_COMMAND:
   2957 			printf("in command phase");
   2958 			break;
   2959 		case P_MESGOUT:
   2960 			printf("in message out phase");
   2961 			break;
   2962 		case P_STATUS:
   2963 			printf("in status phase");
   2964 			break;
   2965 		case P_MESGIN:
   2966 			printf("in message in phase");
   2967 			break;
   2968 		default:
   2969 			printf("while idle, LASTPHASE == 0x%x",
   2970 				bus_state);
   2971 			/*
   2972 			 * We aren't in a valid phase, so assume we're
   2973 			 * idle.
   2974 			 */
   2975 			bus_state = 0;
   2976 			break;
   2977 	}
   2978 
   2979 	printf(", SCSISIGI == 0x%x\n", AHC_INB(ahc, SCSISIGI));
   2980 
   2981 	/* Decide our course of action */
   2982 
   2983 	if(scb->flags & SCB_ABORTED)
   2984 	{
   2985 		/*
   2986 		 * Been down this road before.
   2987 		 * Do a full bus reset.
   2988 		 */
   2989 		char channel = (scb->tcl & SELBUSB)
   2990 			   ? 'B': 'A';
   2991 		found = ahc_reset_channel(ahc, channel, scb->tag,
   2992 					  XS_TIMEOUT, /*Initiate Reset*/TRUE);
   2993 		printf("%s: Issued Channel %c Bus Reset #1. "
   2994 		       "%d SCBs aborted\n", ahc_name(ahc), channel, found);
   2995 		ahc->in_timeout = FALSE;
   2996 	}
   2997 	else if(scb->control & TAG_ENB) {
   2998 		/*
   2999 		 * We could be starving this command
   3000 		 * try sending an ordered tag command
   3001 		 * to the target we come from.
   3002 		 */
   3003 		scb->flags |= SCB_ABORTED|SCB_SENTORDEREDTAG;
   3004 		ahc->orderedtag |= 0xFF;
   3005 		timeout(ahc_timeout, (caddr_t)scb, (5 * hz));
   3006 		unpause_sequencer(ahc, /*unpause_always*/FALSE);
   3007 		printf("Ordered Tag queued\n");
   3008 		goto done;
   3009 	}
   3010 	else {
   3011 		/*
   3012 		 * Send a Bus Device Reset Message:
   3013 		 * The target that is holding up the bus may not
   3014 		 * be the same as the one that triggered this timeout
   3015 		 * (different commands have different timeout lengths).
   3016 		 * It is also impossible to get a message to a target
   3017 		 * if we are in a "frozen" data transfer phase.  Our
   3018 		 * strategy here is to queue a bus device reset message
   3019 		 * to the timed out target if it is disconnected.
   3020 		 * Otherwise, if we have an active target we stuff the
   3021 		 * message buffer with a bus device reset message and
   3022 		 * assert ATN in the hopes that the target will let go
   3023 		 * of the bus and finally disconnect.  If this fails,
   3024 		 * we'll get another timeout 2 seconds later which will
   3025 		 * cause a bus reset.
   3026 		 *
   3027 		 * XXX If the SCB is paged out, we simply reset the
   3028 		 *     bus.  We should probably queue a new command
   3029 		 *     instead.
   3030 		 */
   3031 
   3032 		/* Test to see if scb is disconnected */
   3033 		if( !(scb->flags & SCB_PAGED_OUT ) ){
   3034 			u_char active_scb;
   3035 			struct scb *active_scbp;
   3036 
   3037 			active_scb = AHC_INB(ahc, SCBPTR);
   3038 			active_scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
   3039 			AHC_OUTB(ahc, SCBPTR, scb->position);
   3040 
   3041 			if(AHC_INB(ahc, SCB_CONTROL) & DISCONNECTED) {
   3042 				if(ahc->flags & AHC_PAGESCBS) {
   3043 					/*
   3044 					 * Pull this SCB out of the
   3045 					 * disconnected list.
   3046 					 */
   3047 					u_char prev = AHC_INB(ahc, SCB_PREV);
   3048 					u_char next = AHC_INB(ahc, SCB_NEXT);
   3049 					if(prev == SCB_LIST_NULL) {
   3050 						/* At the head */
   3051 						AHC_OUTB(ahc, DISCONNECTED_SCBH,
   3052 						     next );
   3053 					}
   3054 					else {
   3055 						AHC_OUTB(ahc, SCBPTR, prev);
   3056 						AHC_OUTB(ahc, SCB_NEXT, next);
   3057 						if(next != SCB_LIST_NULL) {
   3058 							AHC_OUTB(ahc, SCBPTR,
   3059 							     next);
   3060 							AHC_OUTB(ahc, SCB_PREV,
   3061 							     prev);
   3062 						}
   3063 						AHC_OUTB(ahc, SCBPTR,
   3064 						     scb->position);
   3065 					}
   3066 				}
   3067 				scb->flags |= SCB_DEVICE_RESET|SCB_ABORTED;
   3068 				scb->control &= DISCENB;
   3069 				scb->control |= MK_MESSAGE;
   3070 				scb->cmdlen = 0;
   3071 				scb->SG_segment_count = 0;
   3072 				scb->SG_list_pointer = 0;
   3073 				scb->data = 0;
   3074 				scb->datalen = 0;
   3075 				ahc_send_scb(ahc, scb);
   3076 				ahc_add_waiting_scb(ahc, scb);
   3077 				timeout(ahc_timeout, (caddr_t)scb, (2 * hz));
   3078 				sc_print_addr(scb->xs->sc_link);
   3079 				printf("BUS DEVICE RESET message queued.\n");
   3080 				AHC_OUTB(ahc, SCBPTR, active_scb);
   3081 				unpause_sequencer(ahc, /*unpause_always*/FALSE);
   3082 				goto done;
   3083 			}
   3084 			/* Is the active SCB really active? */
   3085 			else if((active_scbp->flags & SCB_ACTIVE) && bus_state){
   3086 				AHC_OUTB(ahc, MSG_LEN, 1);
   3087 				AHC_OUTB(ahc, MSG0, MSG_BUS_DEV_RESET);
   3088 				AHC_OUTB(ahc, SCSISIGO, bus_state|ATNO);
   3089 				sc_print_addr(active_scbp->xs->sc_link);
   3090 				printf("asserted ATN - device reset in "
   3091 				       "message buffer\n");
   3092 				active_scbp->flags |=   SCB_DEVICE_RESET
   3093 						      | SCB_ABORTED;
   3094 				if(active_scbp != scb) {
   3095 					untimeout(ahc_timeout,
   3096 						  (caddr_t)active_scbp);
   3097 					/* Give scb a new lease on life */
   3098 					timeout(ahc_timeout, (caddr_t)scb,
   3099 						(scb->xs->timeout * hz) / 1000);
   3100 				}
   3101 				timeout(ahc_timeout, (caddr_t)active_scbp,
   3102 					(2 * hz));
   3103 				AHC_OUTB(ahc, SCBPTR, active_scb);
   3104 				unpause_sequencer(ahc, /*unpause_always*/FALSE);
   3105 				goto done;
   3106 			}
   3107 		}
   3108 		/*
   3109 		 * No active target or a paged out SCB.
   3110 		 * Try reseting the bus.
   3111 		 */
   3112 		channel = (scb->tcl & SELBUSB) ? 'B': 'A';
   3113 		found = ahc_reset_channel(ahc, channel, scb->tag,
   3114 					  XS_TIMEOUT,
   3115 					  /*Initiate Reset*/TRUE);
   3116 		printf("%s: Issued Channel %c Bus Reset #2. "
   3117 			"%d SCBs aborted\n", ahc_name(ahc), channel,
   3118 			found);
   3119 		ahc->in_timeout = FALSE;
   3120 	}
   3121 done:
   3122 	splx(s);
   3123 }
   3124 
   3125 
   3126 /*
   3127  * The device at the given target/channel has been reset.  Abort
   3128  * all active and queued scbs for that target/channel.
   3129  */
   3130 static int
   3131 ahc_reset_device(ahc, target, channel, timedout_scb, xs_error)
   3132 	struct ahc_data *ahc;
   3133 	int target;
   3134 	char channel;
   3135 	u_char timedout_scb;
   3136 	u_int32_t xs_error;
   3137 {
   3138         struct scb *scbp;
   3139 	u_char active_scb;
   3140 	int i = 0;
   3141 	int found = 0;
   3142 
   3143 	/* restore this when we're done */
   3144 	active_scb = AHC_INB(ahc, SCBPTR);
   3145 
   3146 	/*
   3147 	 * Search the QINFIFO.
   3148 	 */
   3149 	{
   3150 		u_char saved_queue[AHC_SCB_MAX];
   3151 		u_char queued = AHC_INB(ahc, QINCNT) & ahc->qcntmask;
   3152 
   3153 		for (i = 0; i < (queued - found); i++) {
   3154 			saved_queue[i] = AHC_INB(ahc, QINFIFO);
   3155 			AHC_OUTB(ahc, SCBPTR, saved_queue[i]);
   3156 			scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
   3157 			if (ahc_match_scb (scbp, target, channel)){
   3158 				/*
   3159 				 * We found an scb that needs to be aborted.
   3160 				 */
   3161 				scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
   3162 				scbp->xs->error |= xs_error;
   3163 				if(scbp->position != timedout_scb)
   3164 					untimeout(ahc_timeout, (caddr_t)scbp);
   3165 				AHC_OUTB(ahc, SCB_CONTROL, 0);
   3166 				i--;
   3167 				found++;
   3168 			}
   3169 		}
   3170 		/* Now put the saved scbs back. */
   3171 		for (queued = 0; queued < i; queued++) {
   3172 			AHC_OUTB(ahc, QINFIFO, saved_queue[queued]);
   3173 		}
   3174 	}
   3175 
   3176 	/*
   3177 	 * Search waiting for selection list.
   3178 	 */
   3179 	{
   3180 		u_char next, prev;
   3181 
   3182 		next = AHC_INB(ahc, WAITING_SCBH);  /* Start at head of list. */
   3183 		prev = SCB_LIST_NULL;
   3184 
   3185 		while (next != SCB_LIST_NULL) {
   3186 			AHC_OUTB(ahc, SCBPTR, next);
   3187 			scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
   3188 			/*
   3189 			 * Select the SCB.
   3190 			 */
   3191 			if (ahc_match_scb(scbp, target, channel)) {
   3192 				next = ahc_abort_wscb(ahc, scbp, prev,
   3193 						timedout_scb, xs_error);
   3194 				found++;
   3195 			}
   3196 			else {
   3197 				prev = next;
   3198 				next = AHC_INB(ahc, SCB_NEXT);
   3199 			}
   3200 		}
   3201 	}
   3202 	/*
   3203 	 * Go through the entire SCB array now and look for
   3204 	 * commands for this target that are active.  These
   3205 	 * are other (most likely tagged) commands that
   3206 	 * were disconnected when the reset occured.
   3207 	 */
   3208 	for(i = 0; i < ahc->numscbs; i++) {
   3209 		scbp = ahc->scbarray[i];
   3210 		if((scbp->flags & SCB_ACTIVE)
   3211 		  && ahc_match_scb(scbp, target, channel)) {
   3212 			/* Ensure the target is "free" */
   3213 			ahc_unbusy_target(ahc, target, channel);
   3214 			if( !(scbp->flags & SCB_PAGED_OUT) )
   3215 			{
   3216 				AHC_OUTB(ahc, SCBPTR, scbp->position);
   3217 				AHC_OUTB(ahc, SCB_CONTROL, 0);
   3218 			}
   3219 			scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
   3220 			scbp->xs->error |= xs_error;
   3221 			if(scbp->tag != timedout_scb)
   3222 				untimeout(ahc_timeout, (caddr_t)scbp);
   3223 			found++;
   3224 		}
   3225 	}
   3226 	AHC_OUTB(ahc, SCBPTR, active_scb);
   3227 	return found;
   3228 }
   3229 
   3230 /*
   3231  * Manipulate the waiting for selection list and return the
   3232  * scb that follows the one that we remove.
   3233  */
   3234 static u_char
   3235 ahc_abort_wscb (ahc, scbp, prev, timedout_scb, xs_error)
   3236 	struct ahc_data *ahc;
   3237         struct scb *scbp;
   3238 	u_char prev;
   3239 	u_char timedout_scb;
   3240 	u_int32_t xs_error;
   3241 {
   3242 	u_char curscbp, next;
   3243 	int target = ((scbp->tcl >> 4) & 0x0f);
   3244 	char channel = (scbp->tcl & SELBUSB) ? 'B' : 'A';
   3245 	/*
   3246 	 * Select the SCB we want to abort and
   3247 	 * pull the next pointer out of it.
   3248 	 */
   3249 	curscbp = AHC_INB(ahc, SCBPTR);
   3250 	AHC_OUTB(ahc, SCBPTR, scbp->position);
   3251 	next = AHC_INB(ahc, SCB_NEXT);
   3252 
   3253 	/* Clear the necessary fields */
   3254 	AHC_OUTB(ahc, SCB_CONTROL, 0);
   3255 	AHC_OUTB(ahc, SCB_NEXT, SCB_LIST_NULL);
   3256 	ahc_unbusy_target(ahc, target, channel);
   3257 
   3258 	/* update the waiting list */
   3259 	if( prev == SCB_LIST_NULL )
   3260 		/* First in the list */
   3261 		AHC_OUTB(ahc, WAITING_SCBH, next);
   3262 	else {
   3263 		/*
   3264 		 * Select the scb that pointed to us
   3265 		 * and update its next pointer.
   3266 		 */
   3267 		AHC_OUTB(ahc, SCBPTR, prev);
   3268 		AHC_OUTB(ahc, SCB_NEXT, next);
   3269 	}
   3270 	/*
   3271 	 * Point us back at the original scb position
   3272 	 * and inform the SCSI system that the command
   3273 	 * has been aborted.
   3274 	 */
   3275 	AHC_OUTB(ahc, SCBPTR, curscbp);
   3276 	scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
   3277 	scbp->xs->error |= xs_error;
   3278 	if(scbp->tag != timedout_scb)
   3279 		untimeout(ahc_timeout, (caddr_t)scbp);
   3280 	return next;
   3281 }
   3282 
   3283 static void
   3284 ahc_busy_target(ahc, target, channel)
   3285 	struct ahc_data *ahc;
   3286 	u_char target;
   3287 	char   channel;
   3288 {
   3289 	u_char active;
   3290 	u_long active_port = ACTIVE_A;
   3291 
   3292 	if(target > 0x07 || channel == 'B') {
   3293 		/*
   3294 		 * targets on the Second channel or
   3295 		 * above id 7 store info in byte two
   3296 		 * of HA_ACTIVE
   3297 		 */
   3298 		active_port++;
   3299 	}
   3300 	active = AHC_INB(ahc, active_port);
   3301 	active |= (0x01 << (target & 0x07));
   3302 	AHC_OUTB(ahc, active_port, active);
   3303 }
   3304 
   3305 static void
   3306 ahc_unbusy_target(ahc, target, channel)
   3307 	struct ahc_data *ahc;
   3308 	u_char target;
   3309 	char   channel;
   3310 {
   3311 	u_char active;
   3312 	u_long active_port = ACTIVE_A;
   3313 
   3314 	if(target > 0x07 || channel == 'B') {
   3315 		/*
   3316 		 * targets on the Second channel or
   3317 		 * above id 7 store info in byte two
   3318 		 * of HA_ACTIVE
   3319 		 */
   3320 		active_port++;
   3321 	}
   3322 	active = AHC_INB(ahc, active_port);
   3323 	active &= ~(0x01 << (target & 0x07));
   3324 	AHC_OUTB(ahc, active_port, active);
   3325 }
   3326 
   3327 static void
   3328 ahc_reset_current_bus(ahc)
   3329 	struct ahc_data *ahc;
   3330 {
   3331 	AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
   3332 	DELAY(1000);
   3333 	AHC_OUTB(ahc, SCSISEQ, 0);
   3334 }
   3335 
   3336 static int
   3337 ahc_reset_channel(ahc, channel, timedout_scb, xs_error, initiate_reset)
   3338 	struct ahc_data *ahc;
   3339 	char   channel;
   3340 	u_char timedout_scb;
   3341 	u_int32_t xs_error;
   3342 	u_char initiate_reset;
   3343 {
   3344 	u_char sblkctl;
   3345 	char cur_channel;
   3346 	u_long offset, offset_max;
   3347 	int found;
   3348 
   3349 	/*
   3350 	 * Clean up all the state information for the
   3351 	 * pending transactions on this bus.
   3352 	 */
   3353 	found = ahc_reset_device(ahc, ALL_TARGETS, channel,
   3354 				 timedout_scb, xs_error);
   3355 	if(channel == 'B'){
   3356 		ahc->needsdtr |= (ahc->needsdtr_orig & 0xff00);
   3357 		ahc->sdtrpending &= 0x00ff;
   3358 		AHC_OUTB(ahc, ACTIVE_B, 0);
   3359 		offset = TARG_SCRATCH + 8;
   3360 		offset_max = TARG_SCRATCH + 16;
   3361 	}
   3362 	else if (ahc->type & AHC_WIDE){
   3363 		ahc->needsdtr = ahc->needsdtr_orig;
   3364 		ahc->needwdtr = ahc->needwdtr_orig;
   3365 		ahc->sdtrpending = 0;
   3366 		ahc->wdtrpending = 0;
   3367 		AHC_OUTB(ahc, ACTIVE_A, 0);
   3368 		AHC_OUTB(ahc, ACTIVE_B, 0);
   3369 		offset = TARG_SCRATCH;
   3370 		offset_max = TARG_SCRATCH + 16;
   3371 	}
   3372 	else{
   3373 		ahc->needsdtr |= (ahc->needsdtr_orig & 0x00ff);
   3374 		ahc->sdtrpending &= 0xff00;
   3375 		AHC_OUTB(ahc, ACTIVE_A, 0);
   3376 		offset = TARG_SCRATCH;
   3377 		offset_max = TARG_SCRATCH + 8;
   3378 	}
   3379 	for(;offset < offset_max;offset++) {
   3380 		/*
   3381 		 * Revert to async/narrow transfers
   3382 		 * until we renegotiate.
   3383 		 */
   3384 		u_char targ_scratch;
   3385 
   3386 		targ_scratch = AHC_INB(ahc, offset);
   3387 		targ_scratch &= SXFR;
   3388 		AHC_OUTB(ahc, offset, targ_scratch);
   3389 	}
   3390 
   3391 	/*
   3392 	 * Reset the bus if we are initiating this reset and
   3393 	 * restart/unpause the sequencer
   3394 	 */
   3395 	/* Case 1: Command for another bus is active */
   3396 	sblkctl = AHC_INB(ahc, SBLKCTL);
   3397 	cur_channel = (sblkctl & SELBUSB) ? 'B' : 'A';
   3398 	if(cur_channel != channel)
   3399 	{
   3400 		/*
   3401 		 * Stealthily reset the other bus
   3402 		 * without upsetting the current bus
   3403 		 */
   3404 		AHC_OUTB(ahc, SBLKCTL, sblkctl ^ SELBUSB);
   3405 		if( initiate_reset )
   3406 		{
   3407 			ahc_reset_current_bus(ahc);
   3408 		}
   3409 		AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI|CLRSELTIMEO);
   3410 		AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   3411 		AHC_OUTB(ahc, SBLKCTL, sblkctl);
   3412 		unpause_sequencer(ahc, /*unpause_always*/TRUE);
   3413 	}
   3414 	/* Case 2: A command from this bus is active or we're idle */
   3415 	else {
   3416 		if( initiate_reset )
   3417 		{
   3418 			ahc_reset_current_bus(ahc);
   3419 		}
   3420 		AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI|CLRSELTIMEO);
   3421 		AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   3422 		restart_sequencer(ahc);
   3423 	}
   3424 	ahc_run_done_queue(ahc);
   3425 	return found;
   3426 }
   3427 
   3428 void
   3429 ahc_run_done_queue(ahc)
   3430 	struct ahc_data *ahc;
   3431 {
   3432 	int i;
   3433 	struct scb *scbp;
   3434 
   3435 	for(i = 0; i < ahc->numscbs; i++) {
   3436 		scbp = ahc->scbarray[i];
   3437 		if(scbp->flags & SCB_QUEUED_FOR_DONE)
   3438 			ahc_done(ahc, scbp);
   3439 	}
   3440 }
   3441 
   3442 static int
   3443 ahc_match_scb (scb, target, channel)
   3444         struct scb *scb;
   3445         int target;
   3446 	char channel;
   3447 {
   3448 	int targ = (scb->tcl >> 4) & 0x0f;
   3449 	char chan = (scb->tcl & SELBUSB) ? 'B' : 'A';
   3450 
   3451 	if (target == ALL_TARGETS)
   3452 		return (chan == channel);
   3453 	else
   3454 		return ((chan == channel) && (targ == target));
   3455 }
   3456 
   3457 
   3458 static void
   3459 ahc_construct_sdtr(ahc, start_byte, period, offset)
   3460 	struct ahc_data *ahc;
   3461 	int start_byte;
   3462 	u_int8_t period;
   3463 	u_int8_t offset;
   3464 {
   3465 	AHC_OUTB(ahc, MSG0 + start_byte, MSG_EXTENDED);
   3466 	AHC_OUTB(ahc, MSG1 + start_byte, MSG_EXT_SDTR_LEN);
   3467 	AHC_OUTB(ahc, MSG2 + start_byte, MSG_EXT_SDTR);
   3468 	AHC_OUTB(ahc, MSG3 + start_byte, period);
   3469 	AHC_OUTB(ahc, MSG4 + start_byte, offset);
   3470 	AHC_OUTB(ahc, MSG_LEN, start_byte + 5);
   3471 }
   3472 
   3473 static void
   3474 ahc_construct_wdtr(ahc, start_byte, bus_width)
   3475 	struct ahc_data *ahc;
   3476 	int start_byte;
   3477 	u_int8_t bus_width;
   3478 {
   3479 	AHC_OUTB(ahc, MSG0 + start_byte, MSG_EXTENDED);
   3480 	AHC_OUTB(ahc, MSG1 + start_byte, MSG_EXT_WDTR_LEN);
   3481 	AHC_OUTB(ahc, MSG2 + start_byte, MSG_EXT_WDTR);
   3482 	AHC_OUTB(ahc, MSG3 + start_byte, bus_width);
   3483 	AHC_OUTB(ahc, MSG_LEN, start_byte + 4);
   3484 }
   3485