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