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