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