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