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