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