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