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