Home | History | Annotate | Line # | Download | only in ic
aic6360.c revision 1.65
      1 /*	$NetBSD: aic6360.c,v 1.65 1999/10/30 00:58:32 enami Exp $	*/
      2 
      3 #include "opt_ddb.h"
      4 #ifdef DDB
      5 #define	integrate
      6 #else
      7 #define	integrate	static inline
      8 #endif
      9 
     10 /*
     11  * Copyright (c) 1994, 1995, 1996 Charles M. Hannum.  All rights reserved.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, 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. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by Charles M. Hannum.
     24  * 4. The name of the author may not be used to endorse or promote products
     25  *    derived from this software without specific prior written permission.
     26  *
     27  * Copyright (c) 1994 Jarle Greipsland
     28  * All rights reserved.
     29  *
     30  * Redistribution and use in source and binary forms, with or without
     31  * modification, are permitted provided that the following conditions
     32  * are met:
     33  * 1. Redistributions of source code must retain the above copyright
     34  *    notice, this list of conditions and the following disclaimer.
     35  * 2. Redistributions in binary form must reproduce the above copyright
     36  *    notice, this list of conditions and the following disclaimer in the
     37  *    documentation and/or other materials provided with the distribution.
     38  * 3. The name of the author may not be used to endorse or promote products
     39  *    derived from this software without specific prior written permission.
     40  *
     41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     42  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     43  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     44  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     45  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     46  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     47  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     50  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     51  * POSSIBILITY OF SUCH DAMAGE.
     52  */
     53 
     54 /*
     55  * Acknowledgements: Many of the algorithms used in this driver are
     56  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     57  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     58  */
     59 
     60 /* TODO list:
     61  * 1) Get the DMA stuff working.
     62  * 2) Get the iov/uio stuff working. Is this a good thing ???
     63  * 3) Get the synch stuff working.
     64  * 4) Rewrite it to use malloc for the acb structs instead of static alloc.?
     65  */
     66 
     67 /*
     68  * A few customizable items:
     69  */
     70 
     71 /* Use doubleword transfers to/from SCSI chip.  Note: This requires
     72  * motherboard support.  Basicly, some motherboard chipsets are able to
     73  * split a 32 bit I/O operation into two 16 bit I/O operations,
     74  * transparently to the processor.  This speeds up some things, notably long
     75  * data transfers.
     76  */
     77 #define AIC_USE_DWORDS		0
     78 
     79 /* Synchronous data transfers? */
     80 #define AIC_USE_SYNCHRONOUS	0
     81 #define AIC_SYNC_REQ_ACK_OFS 	8
     82 
     83 /* Wide data transfers? */
     84 #define	AIC_USE_WIDE		0
     85 #define	AIC_MAX_WIDTH		0
     86 
     87 /* Max attempts made to transmit a message */
     88 #define AIC_MSG_MAX_ATTEMPT	3 /* Not used now XXX */
     89 
     90 /* Use DMA (else we do programmed I/O using string instructions) (not yet!)*/
     91 #define AIC_USE_EISA_DMA	0
     92 #define AIC_USE_ISA_DMA		0
     93 
     94 /* How to behave on the (E)ISA bus when/if DMAing (on<<4) + off in us */
     95 #define EISA_BRST_TIM ((15<<4) + 1)	/* 15us on, 1us off */
     96 
     97 /* Some spin loop parameters (essentially how long to wait some places)
     98  * The problem(?) is that sometimes we expect either to be able to transmit a
     99  * byte or to get a new one from the SCSI bus pretty soon.  In order to avoid
    100  * returning from the interrupt just to get yanked back for the next byte we
    101  * may spin in the interrupt routine waiting for this byte to come.  How long?
    102  * This is really (SCSI) device and processor dependent.  Tuneable, I guess.
    103  */
    104 #define AIC_MSGIN_SPIN		1 	/* Will spinwait upto ?ms for a new msg byte */
    105 #define AIC_MSGOUT_SPIN		1
    106 
    107 /* Include debug functions?  At the end of this file there are a bunch of
    108  * functions that will print out various information regarding queued SCSI
    109  * commands, driver state and chip contents.  You can call them from the
    110  * kernel debugger.  If you set AIC_DEBUG to 0 they are not included (the
    111  * kernel uses less memory) but you lose the debugging facilities.
    112  */
    113 #define AIC_DEBUG		1
    114 
    115 #define	AIC_ABORT_TIMEOUT	2000	/* time to wait for abort */
    116 
    117 /* End of customizable parameters */
    118 
    119 #if AIC_USE_EISA_DMA || AIC_USE_ISA_DMA
    120 #error "I said not yet! Start paying attention... grumble"
    121 #endif
    122 
    123 #include <sys/types.h>
    124 #include <sys/param.h>
    125 #include <sys/systm.h>
    126 #include <sys/kernel.h>
    127 #include <sys/errno.h>
    128 #include <sys/ioctl.h>
    129 #include <sys/device.h>
    130 #include <sys/buf.h>
    131 #include <sys/proc.h>
    132 #include <sys/user.h>
    133 #include <sys/queue.h>
    134 
    135 #include <machine/bus.h>
    136 #include <machine/intr.h>
    137 
    138 #include <dev/scsipi/scsi_all.h>
    139 #include <dev/scsipi/scsipi_all.h>
    140 #include <dev/scsipi/scsi_message.h>
    141 #include <dev/scsipi/scsiconf.h>
    142 
    143 #include <dev/ic/aic6360reg.h>
    144 #include <dev/ic/aic6360var.h>
    145 
    146 
    147 #ifndef DDB
    149 #define	Debugger() panic("should call debugger here (aic6360.c)")
    150 #endif /* ! DDB */
    151 
    152 #if AIC_DEBUG
    153 int aic_debug = 0x00; /* AIC_SHOWSTART|AIC_SHOWMISC|AIC_SHOWTRACE; */
    154 #endif
    155 
    156 void	aicattach	__P((struct aic_softc *));
    157 void	aic_minphys	__P((struct buf *));
    158 void	aic_done	__P((struct aic_softc *, struct aic_acb *));
    159 void	aic_dequeue	__P((struct aic_softc *, struct aic_acb *));
    160 int	aic_scsi_cmd	__P((struct scsipi_xfer *));
    161 int	aic_poll	__P((struct aic_softc *, struct scsipi_xfer *, int));
    162 integrate void	aic_sched_msgout __P((struct aic_softc *, u_char));
    163 integrate void	aic_setsync	__P((struct aic_softc *, struct aic_tinfo *));
    164 void	aic_select	__P((struct aic_softc *, struct aic_acb *));
    165 void	aic_timeout	__P((void *));
    166 void	aic_sched	__P((struct aic_softc *));
    167 void	aic_scsi_reset	__P((struct aic_softc *));
    168 void	aic_reset	__P((struct aic_softc *));
    169 void	aic_free_acb	__P((struct aic_softc *, struct aic_acb *, int));
    170 struct aic_acb* aic_get_acb __P((struct aic_softc *, int));
    171 int	aic_reselect	__P((struct aic_softc *, int));
    172 void	aic_sense	__P((struct aic_softc *, struct aic_acb *));
    173 void	aic_msgin	__P((struct aic_softc *));
    174 void	aic_abort	__P((struct aic_softc *, struct aic_acb *));
    175 void	aic_msgout	__P((struct aic_softc *));
    176 int	aic_dataout_pio	__P((struct aic_softc *, u_char *, int));
    177 int	aic_datain_pio	__P((struct aic_softc *, u_char *, int));
    178 #if AIC_DEBUG
    179 void	aic_print_acb	__P((struct aic_acb *));
    180 void	aic_dump_driver __P((struct aic_softc *));
    181 void	aic_dump6360	__P((struct aic_softc *));
    182 void	aic_show_scsi_cmd __P((struct aic_acb *));
    183 void	aic_print_active_acb __P((void));
    184 #endif
    185 
    186 struct scsipi_device aic_dev = {
    187 	NULL,			/* Use default error handler */
    188 	NULL,			/* have a queue, served by this */
    189 	NULL,			/* have no async handler */
    190 	NULL,			/* Use default 'done' routine */
    191 };
    192 
    193 /*
    195  * INITIALIZATION ROUTINES (probe, attach ++)
    196  */
    197 
    198 /* Do the real search-for-device.
    199  * Prerequisite: sc->sc_iobase should be set to the proper value
    200  */
    201 int
    202 aic_find(iot, ioh)
    203 	bus_space_tag_t iot;
    204 	bus_space_handle_t ioh;
    205 {
    206 	char chip_id[sizeof(IDSTRING)];	/* For chips that support it */
    207 	int i;
    208 
    209 	/* Remove aic6360 from possible powerdown mode */
    210 	bus_space_write_1(iot, ioh, DMACNTRL0, 0);
    211 
    212 	/* Thanks to mark (at) aggregate.com for the new method for detecting
    213 	 * whether the chip is present or not.  Bonus: may also work for
    214 	 * the AIC-6260!
    215  	 */
    216 	AIC_TRACE(("aic: probing for aic-chip\n"));
    217  	/*
    218  	 * Linux also init's the stack to 1-16 and then clears it,
    219      	 *  6260's don't appear to have an ID reg - mpg
    220  	 */
    221 	/* Push the sequence 0,1,..,15 on the stack */
    222 #define STSIZE 16
    223 	bus_space_write_1(iot, ioh, DMACNTRL1, 0); /* Reset stack pointer */
    224 	for (i = 0; i < STSIZE; i++)
    225 		bus_space_write_1(iot, ioh, STACK, i);
    226 
    227 	/* See if we can pull out the same sequence */
    228 	bus_space_write_1(iot, ioh, DMACNTRL1, 0);
    229  	for (i = 0; i < STSIZE && bus_space_read_1(iot, ioh, STACK) == i; i++)
    230 		;
    231 	if (i != STSIZE) {
    232 		AIC_START(("STACK futzed at %d.\n", i));
    233 		return 0;
    234 	}
    235 
    236 	/* See if we can pull the id string out of the ID register,
    237 	 * now only used for informational purposes.
    238 	 */
    239 	bzero(chip_id, sizeof(chip_id));
    240 	bus_space_read_multi_1(iot, ioh, ID, chip_id, sizeof(IDSTRING) - 1);
    241 	AIC_START(("AIC found ID: %s ",chip_id));
    242 	AIC_START(("chip revision %d\n",
    243 	    (int)bus_space_read_1(iot, ioh, REV)));
    244 
    245 	return 1;
    246 }
    247 
    248 /*
    249  * Attach the AIC6360, fill out some high and low level data structures
    250  */
    251 void
    252 aicattach(sc)
    253 	struct aic_softc *sc;
    254 {
    255 
    256 	AIC_TRACE(("aicattach  "));
    257 	sc->sc_state = AIC_INIT;
    258 
    259 	sc->sc_initiator = 7;
    260 	sc->sc_freq = 20;	/* XXXX Assume 20 MHz. */
    261 
    262 	/*
    263 	 * These are the bounds of the sync period, based on the frequency of
    264 	 * the chip's clock input and the size and offset of the sync period
    265 	 * register.
    266 	 *
    267 	 * For a 20Mhz clock, this gives us 25, or 100nS, or 10MB/s, as a
    268 	 * maximum transfer rate, and 112.5, or 450nS, or 2.22MB/s, as a
    269 	 * minimum transfer rate.
    270 	 */
    271 	sc->sc_minsync = (2 * 250) / sc->sc_freq;
    272 	sc->sc_maxsync = (9 * 250) / sc->sc_freq;
    273 
    274 	/*
    275 	 * Fill in the adapter.
    276 	 */
    277 	sc->sc_adapter.scsipi_cmd = aic_scsi_cmd;
    278 	sc->sc_adapter.scsipi_minphys = aic_minphys;
    279 
    280 	/*
    281 	 * Fill in the prototype scsipi_link
    282 	 */
    283 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    284 	sc->sc_link.adapter_softc = sc;
    285 	sc->sc_link.scsipi_scsi.adapter_target = sc->sc_initiator;
    286 	sc->sc_link.adapter = &sc->sc_adapter;
    287 	sc->sc_link.device = &aic_dev;
    288 	sc->sc_link.openings = 2;
    289 	sc->sc_link.scsipi_scsi.max_target = 7;
    290 	sc->sc_link.scsipi_scsi.max_lun = 7;
    291 	sc->sc_link.type = BUS_SCSI;
    292 
    293 	/*
    294 	 * Add reference to adapter so that we drop the reference after
    295 	 * config_found() to make sure the adatper is disabled.
    296 	 */
    297 	if (scsipi_adapter_addref(&sc->sc_link) != 0) {
    298 		printf("%s: unable to enable controller\n",
    299 		    sc->sc_dev.dv_xname);
    300 		return;
    301 	}
    302 
    303 	aic_init(sc, 1);	/* Init chip and driver */
    304 
    305 	/*
    306 	 * Ask the adapter what subunits are present
    307 	 */
    308 	sc->sc_child = config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    309 	scsipi_adapter_delref(&sc->sc_link);
    310 }
    311 
    312 int
    313 aic_activate(self, act)
    314 	struct device *self;
    315 	enum devact act;
    316 {
    317 	struct aic_softc *sc = (struct aic_softc *) self;
    318 	int s, rv = 0;
    319 
    320 	s = splhigh();
    321 	switch (act) {
    322 	case DVACT_ACTIVATE:
    323 		rv = EOPNOTSUPP;
    324 		break;
    325 
    326 	case DVACT_DEACTIVATE:
    327 		if (sc->sc_child != NULL && !sc->sc_dying) {
    328 			rv = config_deactivate(sc->sc_child);
    329 			if (rv == 0)
    330 				sc->sc_dying = 1;
    331 		}
    332 		break;
    333 	}
    334 	splx(s);
    335 
    336 	return (rv);
    337 }
    338 
    339 int
    340 aic_detach(self, flags)
    341 	struct device *self;
    342 	int flags;
    343 {
    344 	struct aic_softc *sc = (struct aic_softc *) self;
    345 	int rv = 0;
    346 
    347 	if (sc->sc_child != NULL)
    348 		rv = config_detach(sc->sc_child, flags);
    349 
    350 	return (rv);
    351 }
    352 
    353 /* Initialize AIC6360 chip itself
    354  * The following conditions should hold:
    355  * aic_isa_probe should have succeeded, i.e. the iobase address in aic_softc
    356  * must be valid.
    357  */
    358 void
    359 aic_reset(sc)
    360 	struct aic_softc *sc;
    361 {
    362 	bus_space_tag_t iot = sc->sc_iot;
    363 	bus_space_handle_t ioh = sc->sc_ioh;
    364 
    365 	/*
    366 	 * Doc. recommends to clear these two registers before
    367 	 * operations commence
    368 	 */
    369 	bus_space_write_1(iot, ioh, SCSITEST, 0);
    370 	bus_space_write_1(iot, ioh, TEST, 0);
    371 
    372 	/* Reset SCSI-FIFO and abort any transfers */
    373 	bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | CLRCH | CLRSTCNT);
    374 
    375 	/* Reset DMA-FIFO */
    376 	bus_space_write_1(iot, ioh, DMACNTRL0, RSTFIFO);
    377 	bus_space_write_1(iot, ioh, DMACNTRL1, 0);
    378 
    379 	/* Disable all selection features */
    380 	bus_space_write_1(iot, ioh, SCSISEQ, 0);
    381 	bus_space_write_1(iot, ioh, SXFRCTL1, 0);
    382 
    383 	/* Disable some interrupts */
    384 	bus_space_write_1(iot, ioh, SIMODE0, 0x00);
    385 	/* Clear a slew of interrupts */
    386 	bus_space_write_1(iot, ioh, CLRSINT0, 0x7f);
    387 
    388 	/* Disable some more interrupts */
    389 	bus_space_write_1(iot, ioh, SIMODE1, 0x00);
    390 	/* Clear another slew of interrupts */
    391 	bus_space_write_1(iot, ioh, CLRSINT1, 0xef);
    392 
    393 	/* Disable synchronous transfers */
    394 	bus_space_write_1(iot, ioh, SCSIRATE, 0);
    395 
    396 	/* Haven't seen ant errors (yet) */
    397 	bus_space_write_1(iot, ioh, CLRSERR, 0x07);
    398 
    399 	/* Set our SCSI-ID */
    400 	bus_space_write_1(iot, ioh, SCSIID, sc->sc_initiator << OID_S);
    401 	bus_space_write_1(iot, ioh, BRSTCNTRL, EISA_BRST_TIM);
    402 }
    403 
    404 /* Pull the SCSI RST line for 500 us */
    405 void
    406 aic_scsi_reset(sc)
    407 	struct aic_softc *sc;
    408 {
    409 	bus_space_tag_t iot = sc->sc_iot;
    410 	bus_space_handle_t ioh = sc->sc_ioh;
    411 
    412 	bus_space_write_1(iot, ioh, SCSISEQ, SCSIRSTO);
    413 	delay(500);
    414 	bus_space_write_1(iot, ioh, SCSISEQ, 0);
    415 	delay(50);
    416 }
    417 
    418 /*
    419  * Initialize aic SCSI driver.
    420  */
    421 void
    422 aic_init(sc, bus_reset)
    423 	struct aic_softc *sc;
    424 	int bus_reset;
    425 {
    426 	struct aic_acb *acb;
    427 	int r;
    428 
    429 	if (bus_reset) {
    430 		aic_reset(sc);
    431 		aic_scsi_reset(sc);
    432 	}
    433 	aic_reset(sc);
    434 
    435 	if (sc->sc_state == AIC_INIT) {
    436 		/* First time through; initialize. */
    437 		TAILQ_INIT(&sc->ready_list);
    438 		TAILQ_INIT(&sc->nexus_list);
    439 		TAILQ_INIT(&sc->free_list);
    440 		sc->sc_nexus = NULL;
    441 		acb = sc->sc_acb;
    442 		bzero(acb, sizeof(sc->sc_acb));
    443 		for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
    444 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    445 			acb++;
    446 		}
    447 		bzero(&sc->sc_tinfo, sizeof(sc->sc_tinfo));
    448 	} else {
    449 		/* Cancel any active commands. */
    450 		sc->sc_state = AIC_CLEANING;
    451 		if ((acb = sc->sc_nexus) != NULL) {
    452 			acb->xs->error = XS_DRIVER_STUFFUP;
    453 			untimeout(aic_timeout, acb);
    454 			aic_done(sc, acb);
    455 		}
    456 		while ((acb = sc->nexus_list.tqh_first) != NULL) {
    457 			acb->xs->error = XS_DRIVER_STUFFUP;
    458 			untimeout(aic_timeout, acb);
    459 			aic_done(sc, acb);
    460 		}
    461 	}
    462 
    463 	sc->sc_prevphase = PH_INVALID;
    464 	for (r = 0; r < 8; r++) {
    465 		struct aic_tinfo *ti = &sc->sc_tinfo[r];
    466 
    467 		ti->flags = 0;
    468 #if AIC_USE_SYNCHRONOUS
    469 		ti->flags |= DO_SYNC;
    470 		ti->period = sc->sc_minsync;
    471 		ti->offset = AIC_SYNC_REQ_ACK_OFS;
    472 #else
    473 		ti->period = ti->offset = 0;
    474 #endif
    475 #if AIC_USE_WIDE
    476 		ti->flags |= DO_WIDE;
    477 		ti->width = AIC_MAX_WIDTH;
    478 #else
    479 		ti->width = 0;
    480 #endif
    481 	}
    482 
    483 	sc->sc_state = AIC_IDLE;
    484 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, DMACNTRL0, INTEN);
    485 }
    486 
    487 void
    488 aic_free_acb(sc, acb, flags)
    489 	struct aic_softc *sc;
    490 	struct aic_acb *acb;
    491 	int flags;
    492 {
    493 	int s;
    494 
    495 	s = splbio();
    496 
    497 	acb->flags = 0;
    498 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
    499 
    500 	/*
    501 	 * If there were none, wake anybody waiting for one to come free,
    502 	 * starting with queued entries.
    503 	 */
    504 	if (acb->chain.tqe_next == 0)
    505 		wakeup(&sc->free_list);
    506 
    507 	splx(s);
    508 }
    509 
    510 struct aic_acb *
    511 aic_get_acb(sc, flags)
    512 	struct aic_softc *sc;
    513 	int flags;
    514 {
    515 	struct aic_acb *acb;
    516 	int s;
    517 
    518 	s = splbio();
    519 
    520 	while ((acb = sc->free_list.tqh_first) == NULL &&
    521 	       (flags & XS_CTL_NOSLEEP) == 0)
    522 		tsleep(&sc->free_list, PRIBIO, "aicacb", 0);
    523 	if (acb) {
    524 		TAILQ_REMOVE(&sc->free_list, acb, chain);
    525 		acb->flags |= ACB_ALLOC;
    526 	}
    527 
    528 	splx(s);
    529 	return acb;
    530 }
    531 
    532 /*
    534  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
    535  */
    536 
    537 /*
    538  * Expected sequence:
    539  * 1) Command inserted into ready list
    540  * 2) Command selected for execution
    541  * 3) Command won arbitration and has selected target device
    542  * 4) Send message out (identify message, eventually also sync.negotiations)
    543  * 5) Send command
    544  * 5a) Receive disconnect message, disconnect.
    545  * 5b) Reselected by target
    546  * 5c) Receive identify message from target.
    547  * 6) Send or receive data
    548  * 7) Receive status
    549  * 8) Receive message (command complete etc.)
    550  * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
    551  *    Repeat 2-8 (no disconnects please...)
    552  */
    553 
    554 /*
    555  * Start a SCSI-command
    556  * This function is called by the higher level SCSI-driver to queue/run
    557  * SCSI-commands.
    558  */
    559 int
    560 aic_scsi_cmd(xs)
    561 	struct scsipi_xfer *xs;
    562 {
    563 	struct scsipi_link *sc_link = xs->sc_link;
    564 	struct aic_softc *sc = sc_link->adapter_softc;
    565 	struct aic_acb *acb;
    566 	int s, flags;
    567 
    568 	AIC_TRACE(("aic_scsi_cmd  "));
    569 	AIC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
    570 	    sc_link->scsipi_scsi.target));
    571 
    572 	if (sc->sc_dying) {
    573 		xs->xs_status |= XS_STS_DONE;
    574 		xs->error = XS_DRIVER_STUFFUP;
    575 		scsipi_done(xs);
    576 		if ((xs->xs_control & XS_CTL_POLL) == 0)
    577 			return (SUCCESSFULLY_QUEUED);
    578 		else
    579 			return (COMPLETE);
    580 	}
    581 
    582 	flags = xs->xs_control;
    583 	if ((acb = aic_get_acb(sc, flags)) == NULL) {
    584 		xs->error = XS_DRIVER_STUFFUP;
    585 		return TRY_AGAIN_LATER;
    586 	}
    587 
    588 	/* Initialize acb */
    589 	acb->xs = xs;
    590 	acb->timeout = xs->timeout;
    591 
    592 	if (xs->xs_control & XS_CTL_RESET) {
    593 		acb->flags |= ACB_RESET;
    594 		acb->scsipi_cmd_length = 0;
    595 		acb->data_length = 0;
    596 	} else {
    597 		bcopy(xs->cmd, &acb->scsipi_cmd, xs->cmdlen);
    598 		acb->scsipi_cmd_length = xs->cmdlen;
    599 		acb->data_addr = xs->data;
    600 		acb->data_length = xs->datalen;
    601 	}
    602 	acb->target_stat = 0;
    603 
    604 	s = splbio();
    605 
    606 	TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
    607 	if (sc->sc_state == AIC_IDLE)
    608 		aic_sched(sc);
    609 
    610 	splx(s);
    611 
    612 	if ((flags & XS_CTL_POLL) == 0)
    613 		return SUCCESSFULLY_QUEUED;
    614 
    615 	/* Not allowed to use interrupts, use polling instead */
    616 	if (aic_poll(sc, xs, acb->timeout)) {
    617 		aic_timeout(acb);
    618 		if (aic_poll(sc, xs, acb->timeout))
    619 			aic_timeout(acb);
    620 	}
    621 	return COMPLETE;
    622 }
    623 
    624 /*
    625  * Adjust transfer size in buffer structure
    626  */
    627 void
    628 aic_minphys(bp)
    629 	struct buf *bp;
    630 {
    631 
    632 	AIC_TRACE(("aic_minphys  "));
    633 	if (bp->b_bcount > (AIC_NSEG << PGSHIFT))
    634 		bp->b_bcount = (AIC_NSEG << PGSHIFT);
    635 	minphys(bp);
    636 }
    637 
    638 /*
    639  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
    640  */
    641 int
    642 aic_poll(sc, xs, count)
    643 	struct aic_softc *sc;
    644 	struct scsipi_xfer *xs;
    645 	int count;
    646 {
    647 	bus_space_tag_t iot = sc->sc_iot;
    648 	bus_space_handle_t ioh = sc->sc_ioh;
    649 
    650 	AIC_TRACE(("aic_poll  "));
    651 	while (count) {
    652 		/*
    653 		 * If we had interrupts enabled, would we
    654 		 * have got an interrupt?
    655 		 */
    656 		if ((bus_space_read_1(iot, ioh, DMASTAT) & INTSTAT) != 0)
    657 			aicintr(sc);
    658 		if ((xs->xs_status & XS_STS_DONE) != 0)
    659 			return 0;
    660 		delay(1000);
    661 		count--;
    662 	}
    663 	return 1;
    664 }
    665 
    666 /*
    668  * LOW LEVEL SCSI UTILITIES
    669  */
    670 
    671 integrate void
    672 aic_sched_msgout(sc, m)
    673 	struct aic_softc *sc;
    674 	u_char m;
    675 {
    676 	bus_space_tag_t iot = sc->sc_iot;
    677 	bus_space_handle_t ioh = sc->sc_ioh;
    678 
    679 	if (sc->sc_msgpriq == 0)
    680 		bus_space_write_1(iot, ioh, SCSISIG, sc->sc_phase | ATNO);
    681 	sc->sc_msgpriq |= m;
    682 }
    683 
    684 /*
    685  * Set synchronous transfer offset and period.
    686  */
    687 integrate void
    688 aic_setsync(sc, ti)
    689 	struct aic_softc *sc;
    690 	struct aic_tinfo *ti;
    691 {
    692 #if AIC_USE_SYNCHRONOUS
    693 	bus_space_tag_t iot = sc->sc_iot;
    694 	bus_space_handle_t ioh = sc->sc_ioh;
    695 
    696 	if (ti->offset != 0)
    697 		bus_space_write_1(iot, ioh, SCSIRATE,
    698 		    ((ti->period * sc->sc_freq) / 250 - 2) << 4 | ti->offset);
    699 	else
    700 		bus_space_write_1(iot, ioh, SCSIRATE, 0);
    701 #endif
    702 }
    703 
    704 /*
    705  * Start a selection.  This is used by aic_sched() to select an idle target,
    706  * and by aic_done() to immediately reselect a target to get sense information.
    707  */
    708 void
    709 aic_select(sc, acb)
    710 	struct aic_softc *sc;
    711 	struct aic_acb *acb;
    712 {
    713 	struct scsipi_link *sc_link = acb->xs->sc_link;
    714 	int target = sc_link->scsipi_scsi.target;
    715 	struct aic_tinfo *ti = &sc->sc_tinfo[target];
    716 	bus_space_tag_t iot = sc->sc_iot;
    717 	bus_space_handle_t ioh = sc->sc_ioh;
    718 
    719 	bus_space_write_1(iot, ioh, SCSIID,
    720 	    sc->sc_initiator << OID_S | target);
    721 	aic_setsync(sc, ti);
    722 	bus_space_write_1(iot, ioh, SXFRCTL1, STIMO_256ms | ENSTIMER);
    723 
    724 	/* Always enable reselections. */
    725 	bus_space_write_1(iot, ioh, SIMODE0, ENSELDI | ENSELDO);
    726 	bus_space_write_1(iot, ioh, SIMODE1, ENSCSIRST | ENSELTIMO);
    727 	bus_space_write_1(iot, ioh, SCSISEQ, ENRESELI | ENSELO | ENAUTOATNO);
    728 
    729 	sc->sc_state = AIC_SELECTING;
    730 }
    731 
    732 int
    733 aic_reselect(sc, message)
    734 	struct aic_softc *sc;
    735 	int message;
    736 {
    737 	u_char selid, target, lun;
    738 	struct aic_acb *acb;
    739 	struct scsipi_link *sc_link;
    740 	struct aic_tinfo *ti;
    741 
    742 	/*
    743 	 * The SCSI chip made a snapshot of the data bus while the reselection
    744 	 * was being negotiated.  This enables us to determine which target did
    745 	 * the reselect.
    746 	 */
    747 	selid = sc->sc_selid & ~(1 << sc->sc_initiator);
    748 	if (selid & (selid - 1)) {
    749 		printf("%s: reselect with invalid selid %02x; "
    750 		    "sending DEVICE RESET\n", sc->sc_dev.dv_xname, selid);
    751 		AIC_BREAK();
    752 		goto reset;
    753 	}
    754 
    755 	/* Search wait queue for disconnected cmd
    756 	 * The list should be short, so I haven't bothered with
    757 	 * any more sophisticated structures than a simple
    758 	 * singly linked list.
    759 	 */
    760 	target = ffs(selid) - 1;
    761 	lun = message & 0x07;
    762 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
    763 	     acb = acb->chain.tqe_next) {
    764 		sc_link = acb->xs->sc_link;
    765 		if (sc_link->scsipi_scsi.target == target && sc_link->scsipi_scsi.lun == lun)
    766 			break;
    767 	}
    768 	if (acb == NULL) {
    769 		printf("%s: reselect from target %d lun %d with no nexus; "
    770 		    "sending ABORT\n", sc->sc_dev.dv_xname, target, lun);
    771 		AIC_BREAK();
    772 		goto abort;
    773 	}
    774 
    775 	/* Make this nexus active again. */
    776 	TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    777 	sc->sc_state = AIC_CONNECTED;
    778 	sc->sc_nexus = acb;
    779 	ti = &sc->sc_tinfo[target];
    780 	ti->lubusy |= (1 << lun);
    781 	aic_setsync(sc, ti);
    782 
    783 	if (acb->flags & ACB_RESET)
    784 		aic_sched_msgout(sc, SEND_DEV_RESET);
    785 	else if (acb->flags & ACB_ABORT)
    786 		aic_sched_msgout(sc, SEND_ABORT);
    787 
    788 	/* Do an implicit RESTORE POINTERS. */
    789 	sc->sc_dp = acb->data_addr;
    790 	sc->sc_dleft = acb->data_length;
    791 	sc->sc_cp = (u_char *)&acb->scsipi_cmd;
    792 	sc->sc_cleft = acb->scsipi_cmd_length;
    793 
    794 	return (0);
    795 
    796 reset:
    797 	aic_sched_msgout(sc, SEND_DEV_RESET);
    798 	return (1);
    799 
    800 abort:
    801 	aic_sched_msgout(sc, SEND_ABORT);
    802 	return (1);
    803 }
    804 
    805 /*
    807  * Schedule a SCSI operation.  This has now been pulled out of the interrupt
    808  * handler so that we may call it from aic_scsi_cmd and aic_done.  This may
    809  * save us an unecessary interrupt just to get things going.  Should only be
    810  * called when state == AIC_IDLE and at bio pl.
    811  */
    812 void
    813 aic_sched(sc)
    814 	register struct aic_softc *sc;
    815 {
    816 	struct aic_acb *acb;
    817 	struct scsipi_link *sc_link;
    818 	struct aic_tinfo *ti;
    819 	bus_space_tag_t iot = sc->sc_iot;
    820 	bus_space_handle_t ioh = sc->sc_ioh;
    821 
    822 	if (sc->sc_dying)
    823 		return;
    824 
    825 	/*
    826 	 * Find first acb in ready queue that is for a target/lunit pair that
    827 	 * is not busy.
    828 	 */
    829 	bus_space_write_1(iot, ioh, CLRSINT1,
    830 	    CLRSELTIMO | CLRBUSFREE | CLRSCSIPERR);
    831 	for (acb = sc->ready_list.tqh_first; acb != NULL;
    832 	    acb = acb->chain.tqe_next) {
    833 		sc_link = acb->xs->sc_link;
    834 		ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
    835 		if ((ti->lubusy & (1 << sc_link->scsipi_scsi.lun)) == 0) {
    836 			AIC_MISC(("selecting %d:%d  ",
    837 			    sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun));
    838 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    839 			sc->sc_nexus = acb;
    840 			aic_select(sc, acb);
    841 			return;
    842 		} else
    843 			AIC_MISC(("%d:%d busy\n",
    844 			    sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun));
    845 	}
    846 	AIC_MISC(("idle  "));
    847 	/* Nothing to start; just enable reselections and wait. */
    848 	bus_space_write_1(iot, ioh, SIMODE0, ENSELDI);
    849 	bus_space_write_1(iot, ioh, SIMODE1, ENSCSIRST);
    850 	bus_space_write_1(iot, ioh, SCSISEQ, ENRESELI);
    851 }
    852 
    853 void
    855 aic_sense(sc, acb)
    856 	struct aic_softc *sc;
    857 	struct aic_acb *acb;
    858 {
    859 	struct scsipi_xfer *xs = acb->xs;
    860 	struct scsipi_link *sc_link = xs->sc_link;
    861 	struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
    862 	struct scsipi_sense *ss = (void *)&acb->scsipi_cmd;
    863 
    864 	AIC_MISC(("requesting sense  "));
    865 	/* Next, setup a request sense command block */
    866 	bzero(ss, sizeof(*ss));
    867 	ss->opcode = REQUEST_SENSE;
    868 	ss->byte2 = sc_link->scsipi_scsi.lun << 5;
    869 	ss->length = sizeof(struct scsipi_sense_data);
    870 	acb->scsipi_cmd_length = sizeof(*ss);
    871 	acb->data_addr = (char *)&xs->sense.scsi_sense;
    872 	acb->data_length = sizeof(struct scsipi_sense_data);
    873 	acb->flags |= ACB_SENSE;
    874 	ti->senses++;
    875 	if (acb->flags & ACB_NEXUS)
    876 		ti->lubusy &= ~(1 << sc_link->scsipi_scsi.lun);
    877 	if (acb == sc->sc_nexus) {
    878 		aic_select(sc, acb);
    879 	} else {
    880 		aic_dequeue(sc, acb);
    881 		TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
    882 		if (sc->sc_state == AIC_IDLE)
    883 			aic_sched(sc);
    884 	}
    885 }
    886 
    887 /*
    888  * POST PROCESSING OF SCSI_CMD (usually current)
    889  */
    890 void
    891 aic_done(sc, acb)
    892 	struct aic_softc *sc;
    893 	struct aic_acb *acb;
    894 {
    895 	struct scsipi_xfer *xs = acb->xs;
    896 	struct scsipi_link *sc_link = xs->sc_link;
    897 	struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
    898 
    899 	AIC_TRACE(("aic_done  "));
    900 
    901 	/*
    902 	 * Now, if we've come here with no error code, i.e. we've kept the
    903 	 * initial XS_NOERROR, and the status code signals that we should
    904 	 * check sense, we'll need to set up a request sense cmd block and
    905 	 * push the command back into the ready queue *before* any other
    906 	 * commands for this target/lunit, else we lose the sense info.
    907 	 * We don't support chk sense conditions for the request sense cmd.
    908 	 */
    909 	if (xs->error == XS_NOERROR) {
    910 		if (acb->flags & ACB_ABORT) {
    911 			xs->error = XS_DRIVER_STUFFUP;
    912 		} else if (acb->flags & ACB_SENSE) {
    913 			xs->error = XS_SENSE;
    914 		} else if (acb->target_stat == SCSI_CHECK) {
    915 			/* First, save the return values */
    916 			xs->resid = acb->data_length;
    917 			xs->status = acb->target_stat;
    918 			aic_sense(sc, acb);
    919 			return;
    920 		} else {
    921 			xs->resid = acb->data_length;
    922 		}
    923 	}
    924 
    925 	xs->xs_status |= XS_STS_DONE;
    926 
    927 #if AIC_DEBUG
    928 	if ((aic_debug & AIC_SHOWMISC) != 0) {
    929 		if (xs->resid != 0)
    930 			printf("resid=%d ", xs->resid);
    931 		if (xs->error == XS_SENSE)
    932 			printf("sense=0x%02x\n", xs->sense.scsi_sense.error_code);
    933 		else
    934 			printf("error=%d\n", xs->error);
    935 	}
    936 #endif
    937 
    938 	/*
    939 	 * Remove the ACB from whatever queue it happens to be on.
    940 	 */
    941 	if (acb->flags & ACB_NEXUS)
    942 		ti->lubusy &= ~(1 << sc_link->scsipi_scsi.lun);
    943 	if (acb == sc->sc_nexus) {
    944 		sc->sc_nexus = NULL;
    945 		sc->sc_state = AIC_IDLE;
    946 		aic_sched(sc);
    947 	} else
    948 		aic_dequeue(sc, acb);
    949 
    950 	aic_free_acb(sc, acb, xs->xs_control);
    951 	ti->cmds++;
    952 	scsipi_done(xs);
    953 }
    954 
    955 void
    956 aic_dequeue(sc, acb)
    957 	struct aic_softc *sc;
    958 	struct aic_acb *acb;
    959 {
    960 
    961 	if (acb->flags & ACB_NEXUS) {
    962 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    963 	} else {
    964 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
    965 	}
    966 }
    967 
    968 /*
    970  * INTERRUPT/PROTOCOL ENGINE
    971  */
    972 
    973 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
    974 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
    975 #define ISEXTMSG(m) ((m) == 0x01)
    976 
    977 /*
    978  * Precondition:
    979  * The SCSI bus is already in the MSGI phase and there is a message byte
    980  * on the bus, along with an asserted REQ signal.
    981  */
    982 void
    983 aic_msgin(sc)
    984 	register struct aic_softc *sc;
    985 {
    986 	bus_space_tag_t iot = sc->sc_iot;
    987 	bus_space_handle_t ioh = sc->sc_ioh;
    988 	u_char sstat1;
    989 	int n;
    990 
    991 	AIC_TRACE(("aic_msgin  "));
    992 
    993 	if (sc->sc_prevphase == PH_MSGIN) {
    994 		/* This is a continuation of the previous message. */
    995 		n = sc->sc_imp - sc->sc_imess;
    996 		goto nextbyte;
    997 	}
    998 
    999 	/* This is a new MESSAGE IN phase.  Clean up our state. */
   1000 	sc->sc_flags &= ~AIC_DROP_MSGIN;
   1001 
   1002 nextmsg:
   1003 	n = 0;
   1004 	sc->sc_imp = &sc->sc_imess[n];
   1005 
   1006 nextbyte:
   1007 	/*
   1008 	 * Read a whole message, but don't ack the last byte.  If we reject the
   1009 	 * message, we have to assert ATN during the message transfer phase
   1010 	 * itself.
   1011 	 */
   1012 	for (;;) {
   1013 		for (;;) {
   1014 			sstat1 = bus_space_read_1(iot, ioh, SSTAT1);
   1015 			if ((sstat1 & (REQINIT | PHASECHG | BUSFREE)) != 0)
   1016 				break;
   1017 			/* Wait for REQINIT.  XXX Need timeout. */
   1018 		}
   1019 		if ((sstat1 & (PHASECHG | BUSFREE)) != 0) {
   1020 			/*
   1021 			 * Target left MESSAGE IN, probably because it
   1022 			 * a) noticed our ATN signal, or
   1023 			 * b) ran out of messages.
   1024 			 */
   1025 			goto out;
   1026 		}
   1027 
   1028 		/* If parity error, just dump everything on the floor. */
   1029 		if ((sstat1 & SCSIPERR) != 0) {
   1030 			sc->sc_flags |= AIC_DROP_MSGIN;
   1031 			aic_sched_msgout(sc, SEND_PARITY_ERROR);
   1032 		}
   1033 
   1034 		/* Gather incoming message bytes if needed. */
   1035 		if ((sc->sc_flags & AIC_DROP_MSGIN) == 0) {
   1036 			if (n >= AIC_MAX_MSG_LEN) {
   1037 				(void) bus_space_read_1(iot, ioh, SCSIDAT);
   1038 				sc->sc_flags |= AIC_DROP_MSGIN;
   1039 				aic_sched_msgout(sc, SEND_REJECT);
   1040 			} else {
   1041 				*sc->sc_imp++ = bus_space_read_1(iot, ioh,
   1042 				    SCSIDAT);
   1043 				n++;
   1044 				/*
   1045 				 * This testing is suboptimal, but most
   1046 				 * messages will be of the one byte variety, so
   1047 				 * it should not affect performance
   1048 				 * significantly.
   1049 				 */
   1050 				if (n == 1 && IS1BYTEMSG(sc->sc_imess[0]))
   1051 					break;
   1052 				if (n == 2 && IS2BYTEMSG(sc->sc_imess[0]))
   1053 					break;
   1054 				if (n >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
   1055 				    n == sc->sc_imess[1] + 2)
   1056 					break;
   1057 			}
   1058 		} else
   1059 			(void) bus_space_read_1(iot, ioh, SCSIDAT);
   1060 
   1061 		/*
   1062 		 * If we reach this spot we're either:
   1063 		 * a) in the middle of a multi-byte message, or
   1064 		 * b) dropping bytes.
   1065 		 */
   1066 		bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | SPIOEN);
   1067 		/* Ack the last byte read. */
   1068 		(void) bus_space_read_1(iot, ioh, SCSIDAT);
   1069 		bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
   1070 		while ((bus_space_read_1(iot, ioh, SCSISIG) & ACKI) != 0)
   1071 			;
   1072 	}
   1073 
   1074 	AIC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1075 
   1076 	/* We now have a complete message.  Parse it. */
   1077 	switch (sc->sc_state) {
   1078 		struct aic_acb *acb;
   1079 		struct scsipi_link *sc_link;
   1080 		struct aic_tinfo *ti;
   1081 
   1082 	case AIC_CONNECTED:
   1083 		AIC_ASSERT(sc->sc_nexus != NULL);
   1084 		acb = sc->sc_nexus;
   1085 		ti = &sc->sc_tinfo[acb->xs->sc_link->scsipi_scsi.target];
   1086 
   1087 		switch (sc->sc_imess[0]) {
   1088 		case MSG_CMDCOMPLETE:
   1089 			if (sc->sc_dleft < 0) {
   1090 				sc_link = acb->xs->sc_link;
   1091 				printf("%s: %d extra bytes from %d:%d\n",
   1092 				    sc->sc_dev.dv_xname, -sc->sc_dleft,
   1093 				    sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun);
   1094 				acb->data_length = 0;
   1095 			}
   1096 			acb->xs->resid = acb->data_length = sc->sc_dleft;
   1097 			sc->sc_state = AIC_CMDCOMPLETE;
   1098 			break;
   1099 
   1100 		case MSG_PARITY_ERROR:
   1101 			/* Resend the last message. */
   1102 			aic_sched_msgout(sc, sc->sc_lastmsg);
   1103 			break;
   1104 
   1105 		case MSG_MESSAGE_REJECT:
   1106 			AIC_MISC(("message rejected %02x  ", sc->sc_lastmsg));
   1107 			switch (sc->sc_lastmsg) {
   1108 #if AIC_USE_SYNCHRONOUS + AIC_USE_WIDE
   1109 			case SEND_IDENTIFY:
   1110 				ti->flags &= ~(DO_SYNC | DO_WIDE);
   1111 				ti->period = ti->offset = 0;
   1112 				aic_setsync(sc, ti);
   1113 				ti->width = 0;
   1114 				break;
   1115 #endif
   1116 #if AIC_USE_SYNCHRONOUS
   1117 			case SEND_SDTR:
   1118 				ti->flags &= ~DO_SYNC;
   1119 				ti->period = ti->offset = 0;
   1120 				aic_setsync(sc, ti);
   1121 				break;
   1122 #endif
   1123 #if AIC_USE_WIDE
   1124 			case SEND_WDTR:
   1125 				ti->flags &= ~DO_WIDE;
   1126 				ti->width = 0;
   1127 				break;
   1128 #endif
   1129 			case SEND_INIT_DET_ERR:
   1130 				aic_sched_msgout(sc, SEND_ABORT);
   1131 				break;
   1132 			}
   1133 			break;
   1134 
   1135 		case MSG_NOOP:
   1136 			break;
   1137 
   1138 		case MSG_DISCONNECT:
   1139 			ti->dconns++;
   1140 			sc->sc_state = AIC_DISCONNECT;
   1141 			break;
   1142 
   1143 		case MSG_SAVEDATAPOINTER:
   1144 			acb->data_addr = sc->sc_dp;
   1145 			acb->data_length = sc->sc_dleft;
   1146 			break;
   1147 
   1148 		case MSG_RESTOREPOINTERS:
   1149 			sc->sc_dp = acb->data_addr;
   1150 			sc->sc_dleft = acb->data_length;
   1151 			sc->sc_cp = (u_char *)&acb->scsipi_cmd;
   1152 			sc->sc_cleft = acb->scsipi_cmd_length;
   1153 			break;
   1154 
   1155 		case MSG_EXTENDED:
   1156 			switch (sc->sc_imess[2]) {
   1157 #if AIC_USE_SYNCHRONOUS
   1158 			case MSG_EXT_SDTR:
   1159 				if (sc->sc_imess[1] != 3)
   1160 					goto reject;
   1161 				ti->period = sc->sc_imess[3];
   1162 				ti->offset = sc->sc_imess[4];
   1163 				ti->flags &= ~DO_SYNC;
   1164 				if (ti->offset == 0) {
   1165 				} else if (ti->period < sc->sc_minsync ||
   1166 					   ti->period > sc->sc_maxsync ||
   1167 					   ti->offset > 8) {
   1168 					ti->period = ti->offset = 0;
   1169 					aic_sched_msgout(sc, SEND_SDTR);
   1170 				} else {
   1171 					scsi_print_addr(acb->xs->sc_link);
   1172 					printf("sync, offset %d, "
   1173 					    "period %dnsec\n",
   1174 					    ti->offset, ti->period * 4);
   1175 				}
   1176 				aic_setsync(sc, ti);
   1177 				break;
   1178 #endif
   1179 
   1180 #if AIC_USE_WIDE
   1181 			case MSG_EXT_WDTR:
   1182 				if (sc->sc_imess[1] != 2)
   1183 					goto reject;
   1184 				ti->width = sc->sc_imess[3];
   1185 				ti->flags &= ~DO_WIDE;
   1186 				if (ti->width == 0) {
   1187 				} else if (ti->width > AIC_MAX_WIDTH) {
   1188 					ti->width = 0;
   1189 					aic_sched_msgout(sc, SEND_WDTR);
   1190 				} else {
   1191 					scsi_print_addr(acb->xs->sc_link);
   1192 					printf("wide, width %d\n",
   1193 					    1 << (3 + ti->width));
   1194 				}
   1195 				break;
   1196 #endif
   1197 
   1198 			default:
   1199 				printf("%s: unrecognized MESSAGE EXTENDED; "
   1200 				    "sending REJECT\n", sc->sc_dev.dv_xname);
   1201 				AIC_BREAK();
   1202 				goto reject;
   1203 			}
   1204 			break;
   1205 
   1206 		default:
   1207 			printf("%s: unrecognized MESSAGE; sending REJECT\n",
   1208 			    sc->sc_dev.dv_xname);
   1209 			AIC_BREAK();
   1210 		reject:
   1211 			aic_sched_msgout(sc, SEND_REJECT);
   1212 			break;
   1213 		}
   1214 		break;
   1215 
   1216 	case AIC_RESELECTED:
   1217 		if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
   1218 			printf("%s: reselect without IDENTIFY; "
   1219 			    "sending DEVICE RESET\n", sc->sc_dev.dv_xname);
   1220 			AIC_BREAK();
   1221 			goto reset;
   1222 		}
   1223 
   1224 		(void) aic_reselect(sc, sc->sc_imess[0]);
   1225 		break;
   1226 
   1227 	default:
   1228 		printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
   1229 		    sc->sc_dev.dv_xname);
   1230 		AIC_BREAK();
   1231 	reset:
   1232 		aic_sched_msgout(sc, SEND_DEV_RESET);
   1233 		break;
   1234 
   1235 #ifdef notdef
   1236 	abort:
   1237 		aic_sched_msgout(sc, SEND_ABORT);
   1238 		break;
   1239 #endif
   1240 	}
   1241 
   1242 	bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | SPIOEN);
   1243 	/* Ack the last message byte. */
   1244 	(void) bus_space_read_1(iot, ioh, SCSIDAT);
   1245 	bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
   1246 	while ((bus_space_read_1(iot, ioh, SCSISIG) & ACKI) != 0)
   1247 		;
   1248 
   1249 	/* Go get the next message, if any. */
   1250 	goto nextmsg;
   1251 
   1252 out:
   1253 	AIC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1254 }
   1255 
   1256 /*
   1257  * Send the highest priority, scheduled message.
   1258  */
   1259 void
   1260 aic_msgout(sc)
   1261 	register struct aic_softc *sc;
   1262 {
   1263 	bus_space_tag_t iot = sc->sc_iot;
   1264 	bus_space_handle_t ioh = sc->sc_ioh;
   1265 #if AIC_USE_SYNCHRONOUS
   1266 	struct aic_tinfo *ti;
   1267 #endif
   1268 	u_char sstat1;
   1269 	int n;
   1270 
   1271 	AIC_TRACE(("aic_msgout  "));
   1272 
   1273 	/* Reset the FIFO. */
   1274 	bus_space_write_1(iot, ioh, DMACNTRL0, RSTFIFO);
   1275 	/* Enable REQ/ACK protocol. */
   1276 	bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | SPIOEN);
   1277 
   1278 	if (sc->sc_prevphase == PH_MSGOUT) {
   1279 		if (sc->sc_omp == sc->sc_omess) {
   1280 			/*
   1281 			 * This is a retransmission.
   1282 			 *
   1283 			 * We get here if the target stayed in MESSAGE OUT
   1284 			 * phase.  Section 5.1.9.2 of the SCSI 2 spec indicates
   1285 			 * that all of the previously transmitted messages must
   1286 			 * be sent again, in the same order.  Therefore, we
   1287 			 * requeue all the previously transmitted messages, and
   1288 			 * start again from the top.  Our simple priority
   1289 			 * scheme keeps the messages in the right order.
   1290 			 */
   1291 			AIC_MISC(("retransmitting  "));
   1292 			sc->sc_msgpriq |= sc->sc_msgoutq;
   1293 			/*
   1294 			 * Set ATN.  If we're just sending a trivial 1-byte
   1295 			 * message, we'll clear ATN later on anyway.
   1296 			 */
   1297 			bus_space_write_1(iot, ioh, SCSISIG, PH_MSGOUT | ATNO);
   1298 		} else {
   1299 			/* This is a continuation of the previous message. */
   1300 			n = sc->sc_omp - sc->sc_omess;
   1301 			goto nextbyte;
   1302 		}
   1303 	}
   1304 
   1305 	/* No messages transmitted so far. */
   1306 	sc->sc_msgoutq = 0;
   1307 	sc->sc_lastmsg = 0;
   1308 
   1309 nextmsg:
   1310 	/* Pick up highest priority message. */
   1311 	sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
   1312 	sc->sc_msgpriq &= ~sc->sc_currmsg;
   1313 	sc->sc_msgoutq |= sc->sc_currmsg;
   1314 
   1315 	/* Build the outgoing message data. */
   1316 	switch (sc->sc_currmsg) {
   1317 	case SEND_IDENTIFY:
   1318 		AIC_ASSERT(sc->sc_nexus != NULL);
   1319 		sc->sc_omess[0] =
   1320 		    MSG_IDENTIFY(sc->sc_nexus->xs->sc_link->scsipi_scsi.lun, 1);
   1321 		n = 1;
   1322 		break;
   1323 
   1324 #if AIC_USE_SYNCHRONOUS
   1325 	case SEND_SDTR:
   1326 		AIC_ASSERT(sc->sc_nexus != NULL);
   1327 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->scsipi_scsi.target];
   1328 		sc->sc_omess[4] = MSG_EXTENDED;
   1329 		sc->sc_omess[3] = 3;
   1330 		sc->sc_omess[2] = MSG_EXT_SDTR;
   1331 		sc->sc_omess[1] = ti->period >> 2;
   1332 		sc->sc_omess[0] = ti->offset;
   1333 		n = 5;
   1334 		break;
   1335 #endif
   1336 
   1337 #if AIC_USE_WIDE
   1338 	case SEND_WDTR:
   1339 		AIC_ASSERT(sc->sc_nexus != NULL);
   1340 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->scsipi_scsi.target];
   1341 		sc->sc_omess[3] = MSG_EXTENDED;
   1342 		sc->sc_omess[2] = 2;
   1343 		sc->sc_omess[1] = MSG_EXT_WDTR;
   1344 		sc->sc_omess[0] = ti->width;
   1345 		n = 4;
   1346 		break;
   1347 #endif
   1348 
   1349 	case SEND_DEV_RESET:
   1350 		sc->sc_flags |= AIC_ABORTING;
   1351 		sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1352 		n = 1;
   1353 		break;
   1354 
   1355 	case SEND_REJECT:
   1356 		sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1357 		n = 1;
   1358 		break;
   1359 
   1360 	case SEND_PARITY_ERROR:
   1361 		sc->sc_omess[0] = MSG_PARITY_ERROR;
   1362 		n = 1;
   1363 		break;
   1364 
   1365 	case SEND_INIT_DET_ERR:
   1366 		sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1367 		n = 1;
   1368 		break;
   1369 
   1370 	case SEND_ABORT:
   1371 		sc->sc_flags |= AIC_ABORTING;
   1372 		sc->sc_omess[0] = MSG_ABORT;
   1373 		n = 1;
   1374 		break;
   1375 
   1376 	default:
   1377 		printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
   1378 		    sc->sc_dev.dv_xname);
   1379 		AIC_BREAK();
   1380 		sc->sc_omess[0] = MSG_NOOP;
   1381 		n = 1;
   1382 		break;
   1383 	}
   1384 	sc->sc_omp = &sc->sc_omess[n];
   1385 
   1386 nextbyte:
   1387 	/* Send message bytes. */
   1388 	for (;;) {
   1389 		for (;;) {
   1390 			sstat1 = bus_space_read_1(iot, ioh, SSTAT1);
   1391 			if ((sstat1 & (REQINIT | PHASECHG | BUSFREE)) != 0)
   1392 				break;
   1393 			/* Wait for REQINIT.  XXX Need timeout. */
   1394 		}
   1395 		if ((sstat1 & (PHASECHG | BUSFREE)) != 0) {
   1396 			/*
   1397 			 * Target left MESSAGE OUT, possibly to reject
   1398 			 * our message.
   1399 			 *
   1400 			 * If this is the last message being sent, then we
   1401 			 * deassert ATN, since either the target is going to
   1402 			 * ignore this message, or it's going to ask for a
   1403 			 * retransmission via MESSAGE PARITY ERROR (in which
   1404 			 * case we reassert ATN anyway).
   1405 			 */
   1406 			if (sc->sc_msgpriq == 0)
   1407 				bus_space_write_1(iot, ioh, CLRSINT1, CLRATNO);
   1408 			goto out;
   1409 		}
   1410 
   1411 		/* Clear ATN before last byte if this is the last message. */
   1412 		if (n == 1 && sc->sc_msgpriq == 0)
   1413 			bus_space_write_1(iot, ioh, CLRSINT1, CLRATNO);
   1414 		/* Send message byte. */
   1415 		bus_space_write_1(iot, ioh, SCSIDAT, *--sc->sc_omp);
   1416 		--n;
   1417 		/* Keep track of the last message we've sent any bytes of. */
   1418 		sc->sc_lastmsg = sc->sc_currmsg;
   1419 		/* Wait for ACK to be negated.  XXX Need timeout. */
   1420 		while ((bus_space_read_1(iot, ioh, SCSISIG) & ACKI) != 0)
   1421 			;
   1422 
   1423 		if (n == 0)
   1424 			break;
   1425 	}
   1426 
   1427 	/* We get here only if the entire message has been transmitted. */
   1428 	if (sc->sc_msgpriq != 0) {
   1429 		/* There are more outgoing messages. */
   1430 		goto nextmsg;
   1431 	}
   1432 
   1433 	/*
   1434 	 * The last message has been transmitted.  We need to remember the last
   1435 	 * message transmitted (in case the target switches to MESSAGE IN phase
   1436 	 * and sends a MESSAGE REJECT), and the list of messages transmitted
   1437 	 * this time around (in case the target stays in MESSAGE OUT phase to
   1438 	 * request a retransmit).
   1439 	 */
   1440 
   1441 out:
   1442 	/* Disable REQ/ACK protocol. */
   1443 	bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
   1444 }
   1445 
   1446 /* aic_dataout_pio: perform a data transfer using the FIFO datapath in the
   1448  * aic6360
   1449  * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted
   1450  * and ACK deasserted (i.e. waiting for a data byte)
   1451  * This new revision has been optimized (I tried) to make the common case fast,
   1452  * and the rarer cases (as a result) somewhat more comlex
   1453  */
   1454 int
   1455 aic_dataout_pio(sc, p, n)
   1456 	register struct aic_softc *sc;
   1457 	u_char *p;
   1458 	int n;
   1459 {
   1460 	bus_space_tag_t iot = sc->sc_iot;
   1461 	bus_space_handle_t ioh = sc->sc_ioh;
   1462 	register u_char dmastat = 0;
   1463 	int out = 0;
   1464 #define DOUTAMOUNT 128		/* Full FIFO */
   1465 
   1466 	AIC_MISC(("%02x%02x  ", bus_space_read_1(iot, ioh, FIFOSTAT),
   1467 	    bus_space_read_1(iot, ioh, SSTAT2)));
   1468 
   1469 	/* Clear host FIFO and counter. */
   1470 	bus_space_write_1(iot, ioh, DMACNTRL0, RSTFIFO | WRITE);
   1471 	/* Enable FIFOs. */
   1472 	bus_space_write_1(iot, ioh, DMACNTRL0, ENDMA | DWORDPIO | WRITE);
   1473 	bus_space_write_1(iot, ioh, SXFRCTL0, SCSIEN | DMAEN | CHEN);
   1474 
   1475 	/* Turn off ENREQINIT for now. */
   1476 	bus_space_write_1(iot, ioh, SIMODE1,
   1477 	    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENPHASECHG);
   1478 
   1479 	/* I have tried to make the main loop as tight as possible.  This
   1480 	 * means that some of the code following the loop is a bit more
   1481 	 * complex than otherwise.
   1482 	 */
   1483 	while (n > 0) {
   1484 		for (;;) {
   1485 			dmastat = bus_space_read_1(iot, ioh, DMASTAT);
   1486 			if ((dmastat & (DFIFOEMP | INTSTAT)) != 0)
   1487 				break;
   1488 		}
   1489 
   1490 		if ((dmastat & INTSTAT) != 0)
   1491 			goto phasechange;
   1492 
   1493 		if (n >= DOUTAMOUNT) {
   1494 			n -= DOUTAMOUNT;
   1495 			out += DOUTAMOUNT;
   1496 
   1497 #if AIC_USE_DWORDS
   1498 			bus_space_write_multi_4(iot, ioh, DMADATALONG,
   1499 			    (u_int32_t *) p, DOUTAMOUNT >> 2);
   1500 #else
   1501 			bus_space_write_multi_2(iot, ioh, DMADATA,
   1502 			    (u_int16_t *) p, DOUTAMOUNT >> 1);
   1503 #endif
   1504 
   1505 			p += DOUTAMOUNT;
   1506 		} else {
   1507 			register int xfer;
   1508 
   1509 			xfer = n;
   1510 			AIC_MISC(("%d> ", xfer));
   1511 
   1512 			n -= xfer;
   1513 			out += xfer;
   1514 
   1515 #if AIC_USE_DWORDS
   1516 			if (xfer >= 12) {
   1517 				bus_space_write_multi_4(iot, ioh, DMADATALONG,
   1518 				    (u_int32_t *) p, xfer >> 2);
   1519 				p += xfer & ~3;
   1520 				xfer &= 3;
   1521 			}
   1522 #else
   1523 			if (xfer >= 8) {
   1524 				bus_space_write_multi_2(iot, ioh, DMADATA,
   1525 				    (u_int16_t *) p, xfer >> 1);
   1526 				p += xfer & ~1;
   1527 				xfer &= 1;
   1528 			}
   1529 #endif
   1530 
   1531 			if (xfer > 0) {
   1532 				bus_space_write_1(iot, ioh, DMACNTRL0,
   1533 				    ENDMA | B8MODE | WRITE);
   1534 				bus_space_write_multi_1(iot, ioh, DMADATA,
   1535 				    p, xfer);
   1536 				p += xfer;
   1537 				bus_space_write_1(iot, ioh, DMACNTRL0,
   1538 				    ENDMA | DWORDPIO | WRITE);
   1539 			}
   1540 		}
   1541 	}
   1542 
   1543 	if (out == 0) {
   1544 		bus_space_write_1(iot, ioh, SXFRCTL1, BITBUCKET);
   1545 		for (;;) {
   1546 			if ((bus_space_read_1(iot, ioh, DMASTAT) & INTSTAT)
   1547 			    != 0)
   1548 				break;
   1549 		}
   1550 		bus_space_write_1(iot, ioh, SXFRCTL1, 0);
   1551 		AIC_MISC(("extra data  "));
   1552 	} else {
   1553 		/* See the bytes off chip */
   1554 		for (;;) {
   1555 			dmastat = bus_space_read_1(iot, ioh, DMASTAT);
   1556 			if ((dmastat & INTSTAT) != 0)
   1557 				goto phasechange;
   1558 			if ((dmastat & DFIFOEMP) != 0 &&
   1559 			    (bus_space_read_1(iot, ioh, SSTAT2) & SEMPTY) != 0)
   1560 				break;
   1561 		}
   1562 	}
   1563 
   1564 phasechange:
   1565 	if ((dmastat & INTSTAT) != 0) {
   1566 		/* Some sort of phase change. */
   1567 		int amount;
   1568 
   1569 		/* Stop transfers, do some accounting */
   1570 		amount = bus_space_read_1(iot, ioh, FIFOSTAT)
   1571 		    + (bus_space_read_1(iot, ioh, SSTAT2) & 15);
   1572 		if (amount > 0) {
   1573 			out -= amount;
   1574 			bus_space_write_1(iot, ioh, DMACNTRL0,
   1575 			    RSTFIFO | WRITE);
   1576 			bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | CLRCH);
   1577 			AIC_MISC(("+%d ", amount));
   1578 		}
   1579 	}
   1580 
   1581 	/* Turn on ENREQINIT again. */
   1582 	bus_space_write_1(iot, ioh, SIMODE1,
   1583 	    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENREQINIT | ENPHASECHG);
   1584 
   1585 	/* Stop the FIFO data path. */
   1586 	bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
   1587 	bus_space_write_1(iot, ioh, DMACNTRL0, 0);
   1588 
   1589 	return out;
   1590 }
   1591 
   1592 /* aic_datain_pio: perform data transfers using the FIFO datapath in the
   1594  * aic6360
   1595  * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted
   1596  * and ACK deasserted (i.e. at least one byte is ready).
   1597  * For now, uses a pretty dumb algorithm, hangs around until all data has been
   1598  * transferred.  This, is OK for fast targets, but not so smart for slow
   1599  * targets which don't disconnect or for huge transfers.
   1600  */
   1601 int
   1602 aic_datain_pio(sc, p, n)
   1603 	register struct aic_softc *sc;
   1604 	u_char *p;
   1605 	int n;
   1606 {
   1607 	bus_space_tag_t iot = sc->sc_iot;
   1608 	bus_space_handle_t ioh = sc->sc_ioh;
   1609 	register u_char dmastat;
   1610 	int in = 0;
   1611 #define DINAMOUNT 128		/* Full FIFO */
   1612 
   1613 	AIC_MISC(("%02x%02x  ", bus_space_read_1(iot, ioh, FIFOSTAT),
   1614 	    bus_space_read_1(iot, ioh, SSTAT2)));
   1615 
   1616 	/* Clear host FIFO and counter. */
   1617 	bus_space_write_1(iot, ioh, DMACNTRL0, RSTFIFO);
   1618 	/* Enable FIFOs. */
   1619 	bus_space_write_1(iot, ioh, DMACNTRL0, ENDMA | DWORDPIO);
   1620 	bus_space_write_1(iot, ioh, SXFRCTL0, SCSIEN | DMAEN | CHEN);
   1621 
   1622 	/* Turn off ENREQINIT for now. */
   1623 	bus_space_write_1(iot, ioh, SIMODE1,
   1624 	    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENPHASECHG);
   1625 
   1626 	/* We leave this loop if one or more of the following is true:
   1627 	 * a) phase != PH_DATAIN && FIFOs are empty
   1628 	 * b) SCSIRSTI is set (a reset has occurred) or busfree is detected.
   1629 	 */
   1630 	while (n > 0) {
   1631 		/* Wait for fifo half full or phase mismatch */
   1632 		for (;;) {
   1633 			dmastat = bus_space_read_1(iot, ioh, DMASTAT);
   1634 			if ((dmastat & (DFIFOFULL | INTSTAT)) != 0)
   1635 				break;
   1636 		}
   1637 
   1638 		if ((dmastat & DFIFOFULL) != 0) {
   1639 			n -= DINAMOUNT;
   1640 			in += DINAMOUNT;
   1641 
   1642 #if AIC_USE_DWORDS
   1643 			bus_space_read_multi_4(iot, ioh, DMADATALONG,
   1644 			    (u_int32_t *) p, DINAMOUNT >> 2);
   1645 #else
   1646 			bus_space_read_multi_2(iot, ioh, DMADATA,
   1647 			    (u_int16_t *) p, DINAMOUNT >> 1);
   1648 #endif
   1649 
   1650 			p += DINAMOUNT;
   1651 		} else {
   1652 			register int xfer;
   1653 
   1654 			xfer = min(bus_space_read_1(iot, ioh, FIFOSTAT), n);
   1655 			AIC_MISC((">%d ", xfer));
   1656 
   1657 			n -= xfer;
   1658 			in += xfer;
   1659 
   1660 #if AIC_USE_DWORDS
   1661 			if (xfer >= 12) {
   1662 				bus_space_read_multi_4(iot, ioh, DMADATALONG,
   1663 				    (u_int32_t *) p, xfer >> 2);
   1664 				p += xfer & ~3;
   1665 				xfer &= 3;
   1666 			}
   1667 #else
   1668 			if (xfer >= 8) {
   1669 				bus_space_read_multi_2(iot, ioh, DMADATA,
   1670 				    (u_int16_t *) p, xfer >> 1);
   1671 				p += xfer & ~1;
   1672 				xfer &= 1;
   1673 			}
   1674 #endif
   1675 
   1676 			if (xfer > 0) {
   1677 				bus_space_write_1(iot, ioh, DMACNTRL0,
   1678 				    ENDMA | B8MODE);
   1679 				bus_space_read_multi_1(iot, ioh, DMADATA,
   1680 				    p, xfer);
   1681 				p += xfer;
   1682 				bus_space_write_1(iot, ioh, DMACNTRL0,
   1683 				    ENDMA | DWORDPIO);
   1684 			}
   1685 		}
   1686 
   1687 		if ((dmastat & INTSTAT) != 0)
   1688 			goto phasechange;
   1689 	}
   1690 
   1691 	/* Some SCSI-devices are rude enough to transfer more data than what
   1692 	 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
   1693 	 * requested 512.  Test for progress, i.e. real transfers.  If no real
   1694 	 * transfers have been performed (n is probably already zero) and the
   1695 	 * FIFO is not empty, waste some bytes....
   1696 	 */
   1697 	if (in == 0) {
   1698 		bus_space_write_1(iot, ioh, SXFRCTL1, BITBUCKET);
   1699 		for (;;) {
   1700 			if ((bus_space_read_1(iot, ioh, DMASTAT) & INTSTAT)
   1701 			    != 0)
   1702 				break;
   1703 		}
   1704 		bus_space_write_1(iot, ioh, SXFRCTL1, 0);
   1705 		AIC_MISC(("extra data  "));
   1706 	}
   1707 
   1708 phasechange:
   1709 	/* Turn on ENREQINIT again. */
   1710 	bus_space_write_1(iot, ioh, SIMODE1,
   1711 	    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENREQINIT | ENPHASECHG);
   1712 
   1713 	/* Stop the FIFO data path. */
   1714 	bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
   1715 	bus_space_write_1(iot, ioh, DMACNTRL0, 0);
   1716 
   1717 	return in;
   1718 }
   1719 
   1720 /*
   1722  * This is the workhorse routine of the driver.
   1723  * Deficiencies (for now):
   1724  * 1) always uses programmed I/O
   1725  */
   1726 int
   1727 aicintr(arg)
   1728 	void *arg;
   1729 {
   1730 	register struct aic_softc *sc = arg;
   1731 	bus_space_tag_t iot = sc->sc_iot;
   1732 	bus_space_handle_t ioh = sc->sc_ioh;
   1733 	u_char sstat0, sstat1;
   1734 	register struct aic_acb *acb;
   1735 	register struct scsipi_link *sc_link;
   1736 	struct aic_tinfo *ti;
   1737 	int n;
   1738 
   1739 	if (sc->sc_dying)
   1740 		return (0);
   1741 
   1742 	/*
   1743 	 * Clear INTEN.  We enable it again before returning.  This makes the
   1744 	 * interrupt esssentially level-triggered.
   1745 	 */
   1746 	bus_space_write_1(iot, ioh, DMACNTRL0, 0);
   1747 
   1748 	AIC_TRACE(("aicintr  "));
   1749 
   1750 loop:
   1751 	/*
   1752 	 * First check for abnormal conditions, such as reset.
   1753 	 */
   1754 	sstat1 = bus_space_read_1(iot, ioh, SSTAT1);
   1755 	AIC_MISC(("sstat1:0x%02x ", sstat1));
   1756 
   1757 	if ((sstat1 & SCSIRSTI) != 0) {
   1758 		printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
   1759 		goto reset;
   1760 	}
   1761 
   1762 	/*
   1763 	 * Check for less serious errors.
   1764 	 */
   1765 	if ((sstat1 & SCSIPERR) != 0) {
   1766 		printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
   1767 		bus_space_write_1(iot, ioh, CLRSINT1, CLRSCSIPERR);
   1768 		if (sc->sc_prevphase == PH_MSGIN) {
   1769 			sc->sc_flags |= AIC_DROP_MSGIN;
   1770 			aic_sched_msgout(sc, SEND_PARITY_ERROR);
   1771 		} else
   1772 			aic_sched_msgout(sc, SEND_INIT_DET_ERR);
   1773 	}
   1774 
   1775 	/*
   1776 	 * If we're not already busy doing something test for the following
   1777 	 * conditions:
   1778 	 * 1) We have been reselected by something
   1779 	 * 2) We have selected something successfully
   1780 	 * 3) Our selection process has timed out
   1781 	 * 4) This is really a bus free interrupt just to get a new command
   1782 	 *    going?
   1783 	 * 5) Spurious interrupt?
   1784 	 */
   1785 	switch (sc->sc_state) {
   1786 	case AIC_IDLE:
   1787 	case AIC_SELECTING:
   1788 		sstat0 = bus_space_read_1(iot, ioh, SSTAT0);
   1789 		AIC_MISC(("sstat0:0x%02x ", sstat0));
   1790 
   1791 		if ((sstat0 & TARGET) != 0) {
   1792 			/*
   1793 			 * We don't currently support target mode.
   1794 			 */
   1795 			printf("%s: target mode selected; going to BUS FREE\n",
   1796 			    sc->sc_dev.dv_xname);
   1797 			bus_space_write_1(iot, ioh, SCSISIG, 0);
   1798 
   1799 			goto sched;
   1800 		} else if ((sstat0 & SELDI) != 0) {
   1801 			AIC_MISC(("reselected  "));
   1802 
   1803 			/*
   1804 			 * If we're trying to select a target ourselves,
   1805 			 * push our command back into the ready list.
   1806 			 */
   1807 			if (sc->sc_state == AIC_SELECTING) {
   1808 				AIC_MISC(("backoff selector  "));
   1809 				AIC_ASSERT(sc->sc_nexus != NULL);
   1810 				acb = sc->sc_nexus;
   1811 				sc->sc_nexus = NULL;
   1812 				TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   1813 			}
   1814 
   1815 			/* Save reselection ID. */
   1816 			sc->sc_selid = bus_space_read_1(iot, ioh, SELID);
   1817 
   1818 			sc->sc_state = AIC_RESELECTED;
   1819 		} else if ((sstat0 & SELDO) != 0) {
   1820 			AIC_MISC(("selected  "));
   1821 
   1822 			/* We have selected a target. Things to do:
   1823 			 * a) Determine what message(s) to send.
   1824 			 * b) Verify that we're still selecting the target.
   1825 			 * c) Mark device as busy.
   1826 			 */
   1827 			if (sc->sc_state != AIC_SELECTING) {
   1828 				printf("%s: selection out while idle; "
   1829 				    "resetting\n", sc->sc_dev.dv_xname);
   1830 				AIC_BREAK();
   1831 				goto reset;
   1832 			}
   1833 			AIC_ASSERT(sc->sc_nexus != NULL);
   1834 			acb = sc->sc_nexus;
   1835 			sc_link = acb->xs->sc_link;
   1836 			ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
   1837 
   1838 			sc->sc_msgpriq = SEND_IDENTIFY;
   1839 			if (acb->flags & ACB_RESET)
   1840 				sc->sc_msgpriq |= SEND_DEV_RESET;
   1841 			else if (acb->flags & ACB_ABORT)
   1842 				sc->sc_msgpriq |= SEND_ABORT;
   1843 			else {
   1844 #if AIC_USE_SYNCHRONOUS
   1845 				if ((ti->flags & DO_SYNC) != 0)
   1846 					sc->sc_msgpriq |= SEND_SDTR;
   1847 #endif
   1848 #if AIC_USE_WIDE
   1849 				if ((ti->flags & DO_WIDE) != 0)
   1850 					sc->sc_msgpriq |= SEND_WDTR;
   1851 #endif
   1852 			}
   1853 
   1854 			acb->flags |= ACB_NEXUS;
   1855 			ti->lubusy |= (1 << sc_link->scsipi_scsi.lun);
   1856 
   1857 			/* Do an implicit RESTORE POINTERS. */
   1858 			sc->sc_dp = acb->data_addr;
   1859 			sc->sc_dleft = acb->data_length;
   1860 			sc->sc_cp = (u_char *)&acb->scsipi_cmd;
   1861 			sc->sc_cleft = acb->scsipi_cmd_length;
   1862 
   1863 			/* On our first connection, schedule a timeout. */
   1864 			if ((acb->xs->xs_control & XS_CTL_POLL) == 0)
   1865 				timeout(aic_timeout, acb,
   1866 				    (acb->timeout * hz) / 1000);
   1867 
   1868 			sc->sc_state = AIC_CONNECTED;
   1869 		} else if ((sstat1 & SELTO) != 0) {
   1870 			AIC_MISC(("selection timeout  "));
   1871 
   1872 			if (sc->sc_state != AIC_SELECTING) {
   1873 				printf("%s: selection timeout while idle; "
   1874 				    "resetting\n", sc->sc_dev.dv_xname);
   1875 				AIC_BREAK();
   1876 				goto reset;
   1877 			}
   1878 			AIC_ASSERT(sc->sc_nexus != NULL);
   1879 			acb = sc->sc_nexus;
   1880 
   1881 			bus_space_write_1(iot, ioh, SXFRCTL1, 0);
   1882 			bus_space_write_1(iot, ioh, SCSISEQ, ENRESELI);
   1883 			bus_space_write_1(iot, ioh, CLRSINT1, CLRSELTIMO);
   1884 			delay(250);
   1885 
   1886 			acb->xs->error = XS_SELTIMEOUT;
   1887 			goto finish;
   1888 		} else {
   1889 			if (sc->sc_state != AIC_IDLE) {
   1890 				printf("%s: BUS FREE while not idle; "
   1891 				    "state=%d\n",
   1892 				    sc->sc_dev.dv_xname, sc->sc_state);
   1893 				AIC_BREAK();
   1894 				goto out;
   1895 			}
   1896 
   1897 			goto sched;
   1898 		}
   1899 
   1900 		/*
   1901 		 * Turn off selection stuff, and prepare to catch bus free
   1902 		 * interrupts, parity errors, and phase changes.
   1903 		 */
   1904 		bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | CLRSTCNT | CLRCH);
   1905 		bus_space_write_1(iot, ioh, SXFRCTL1, 0);
   1906 		bus_space_write_1(iot, ioh, SCSISEQ, ENAUTOATNP);
   1907 		bus_space_write_1(iot, ioh, CLRSINT0, CLRSELDI | CLRSELDO);
   1908 		bus_space_write_1(iot, ioh, CLRSINT1,
   1909 		    CLRBUSFREE | CLRPHASECHG);
   1910 		bus_space_write_1(iot, ioh, SIMODE0, 0);
   1911 		bus_space_write_1(iot, ioh, SIMODE1,
   1912 		    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENREQINIT |
   1913 		    ENPHASECHG);
   1914 
   1915 		sc->sc_flags = 0;
   1916 		sc->sc_prevphase = PH_INVALID;
   1917 		goto dophase;
   1918 	}
   1919 
   1920 	if ((sstat1 & BUSFREE) != 0) {
   1921 		/* We've gone to BUS FREE phase. */
   1922 		bus_space_write_1(iot, ioh, CLRSINT1,
   1923 		    CLRBUSFREE | CLRPHASECHG);
   1924 
   1925 		switch (sc->sc_state) {
   1926 		case AIC_RESELECTED:
   1927 			goto sched;
   1928 
   1929 		case AIC_CONNECTED:
   1930 			AIC_ASSERT(sc->sc_nexus != NULL);
   1931 			acb = sc->sc_nexus;
   1932 
   1933 #if AIC_USE_SYNCHRONOUS + AIC_USE_WIDE
   1934 			if (sc->sc_prevphase == PH_MSGOUT) {
   1935 				/*
   1936 				 * If the target went to BUS FREE phase during
   1937 				 * or immediately after sending a SDTR or WDTR
   1938 				 * message, disable negotiation.
   1939 				 */
   1940 				sc_link = acb->xs->sc_link;
   1941 				ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
   1942 				switch (sc->sc_lastmsg) {
   1943 #if AIC_USE_SYNCHRONOUS
   1944 				case SEND_SDTR:
   1945 					ti->flags &= ~DO_SYNC;
   1946 					ti->period = ti->offset = 0;
   1947 					break;
   1948 #endif
   1949 #if AIC_USE_WIDE
   1950 				case SEND_WDTR:
   1951 					ti->flags &= ~DO_WIDE;
   1952 					ti->width = 0;
   1953 					break;
   1954 #endif
   1955 				}
   1956 			}
   1957 #endif
   1958 
   1959 			if ((sc->sc_flags & AIC_ABORTING) == 0) {
   1960 				/*
   1961 				 * Section 5.1.1 of the SCSI 2 spec suggests
   1962 				 * issuing a REQUEST SENSE following an
   1963 				 * unexpected disconnect.  Some devices go into
   1964 				 * a contingent allegiance condition when
   1965 				 * disconnecting, and this is necessary to
   1966 				 * clean up their state.
   1967 				 */
   1968 				printf("%s: unexpected disconnect; "
   1969 				    "sending REQUEST SENSE\n",
   1970 				    sc->sc_dev.dv_xname);
   1971 				AIC_BREAK();
   1972 				aic_sense(sc, acb);
   1973 				goto out;
   1974 			}
   1975 
   1976 			acb->xs->error = XS_DRIVER_STUFFUP;
   1977 			goto finish;
   1978 
   1979 		case AIC_DISCONNECT:
   1980 			AIC_ASSERT(sc->sc_nexus != NULL);
   1981 			acb = sc->sc_nexus;
   1982 #if 1 /* XXXX */
   1983 			acb->data_addr = sc->sc_dp;
   1984 			acb->data_length = sc->sc_dleft;
   1985 #endif
   1986 			TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   1987 			sc->sc_nexus = NULL;
   1988 			goto sched;
   1989 
   1990 		case AIC_CMDCOMPLETE:
   1991 			AIC_ASSERT(sc->sc_nexus != NULL);
   1992 			acb = sc->sc_nexus;
   1993 			goto finish;
   1994 		}
   1995 	}
   1996 
   1997 	bus_space_write_1(iot, ioh, CLRSINT1, CLRPHASECHG);
   1998 
   1999 dophase:
   2000 	if ((sstat1 & REQINIT) == 0) {
   2001 		/* Wait for REQINIT. */
   2002 		goto out;
   2003 	}
   2004 
   2005 	sc->sc_phase = bus_space_read_1(iot, ioh, SCSISIG) & PH_MASK;
   2006 	bus_space_write_1(iot, ioh, SCSISIG, sc->sc_phase);
   2007 
   2008 	switch (sc->sc_phase) {
   2009 	case PH_MSGOUT:
   2010 		if (sc->sc_state != AIC_CONNECTED &&
   2011 		    sc->sc_state != AIC_RESELECTED)
   2012 			break;
   2013 		aic_msgout(sc);
   2014 		sc->sc_prevphase = PH_MSGOUT;
   2015 		goto loop;
   2016 
   2017 	case PH_MSGIN:
   2018 		if (sc->sc_state != AIC_CONNECTED &&
   2019 		    sc->sc_state != AIC_RESELECTED)
   2020 			break;
   2021 		aic_msgin(sc);
   2022 		sc->sc_prevphase = PH_MSGIN;
   2023 		goto loop;
   2024 
   2025 	case PH_CMD:
   2026 		if (sc->sc_state != AIC_CONNECTED)
   2027 			break;
   2028 #if AIC_DEBUG
   2029 		if ((aic_debug & AIC_SHOWMISC) != 0) {
   2030 			AIC_ASSERT(sc->sc_nexus != NULL);
   2031 			acb = sc->sc_nexus;
   2032 			printf("cmd=0x%02x+%d ",
   2033 			    acb->scsipi_cmd.opcode, acb->scsipi_cmd_length-1);
   2034 		}
   2035 #endif
   2036 		n = aic_dataout_pio(sc, sc->sc_cp, sc->sc_cleft);
   2037 		sc->sc_cp += n;
   2038 		sc->sc_cleft -= n;
   2039 		sc->sc_prevphase = PH_CMD;
   2040 		goto loop;
   2041 
   2042 	case PH_DATAOUT:
   2043 		if (sc->sc_state != AIC_CONNECTED)
   2044 			break;
   2045 		AIC_MISC(("dataout %d ", sc->sc_dleft));
   2046 		n = aic_dataout_pio(sc, sc->sc_dp, sc->sc_dleft);
   2047 		sc->sc_dp += n;
   2048 		sc->sc_dleft -= n;
   2049 		sc->sc_prevphase = PH_DATAOUT;
   2050 		goto loop;
   2051 
   2052 	case PH_DATAIN:
   2053 		if (sc->sc_state != AIC_CONNECTED)
   2054 			break;
   2055 		AIC_MISC(("datain %d ", sc->sc_dleft));
   2056 		n = aic_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
   2057 		sc->sc_dp += n;
   2058 		sc->sc_dleft -= n;
   2059 		sc->sc_prevphase = PH_DATAIN;
   2060 		goto loop;
   2061 
   2062 	case PH_STAT:
   2063 		if (sc->sc_state != AIC_CONNECTED)
   2064 			break;
   2065 		AIC_ASSERT(sc->sc_nexus != NULL);
   2066 		acb = sc->sc_nexus;
   2067 		bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | SPIOEN);
   2068 		acb->target_stat = bus_space_read_1(iot, ioh, SCSIDAT);
   2069 		bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
   2070 		AIC_MISC(("target_stat=0x%02x  ", acb->target_stat));
   2071 		sc->sc_prevphase = PH_STAT;
   2072 		goto loop;
   2073 	}
   2074 
   2075 	printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
   2076 	AIC_BREAK();
   2077 reset:
   2078 	aic_init(sc, 1);
   2079 	return 1;
   2080 
   2081 finish:
   2082 	untimeout(aic_timeout, acb);
   2083 	aic_done(sc, acb);
   2084 	goto out;
   2085 
   2086 sched:
   2087 	sc->sc_state = AIC_IDLE;
   2088 	aic_sched(sc);
   2089 	goto out;
   2090 
   2091 out:
   2092 	bus_space_write_1(iot, ioh, DMACNTRL0, INTEN);
   2093 	return 1;
   2094 }
   2095 
   2096 void
   2097 aic_abort(sc, acb)
   2098 	struct aic_softc *sc;
   2099 	struct aic_acb *acb;
   2100 {
   2101 
   2102 	/* 2 secs for the abort */
   2103 	acb->timeout = AIC_ABORT_TIMEOUT;
   2104 	acb->flags |= ACB_ABORT;
   2105 
   2106 	if (acb == sc->sc_nexus) {
   2107 		/*
   2108 		 * If we're still selecting, the message will be scheduled
   2109 		 * after selection is complete.
   2110 		 */
   2111 		if (sc->sc_state == AIC_CONNECTED)
   2112 			aic_sched_msgout(sc, SEND_ABORT);
   2113 	} else {
   2114 		aic_dequeue(sc, acb);
   2115 		TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   2116 		if (sc->sc_state == AIC_IDLE)
   2117 			aic_sched(sc);
   2118 	}
   2119 }
   2120 
   2121 void
   2122 aic_timeout(arg)
   2123 	void *arg;
   2124 {
   2125 	struct aic_acb *acb = arg;
   2126 	struct scsipi_xfer *xs = acb->xs;
   2127 	struct scsipi_link *sc_link = xs->sc_link;
   2128 	struct aic_softc *sc = sc_link->adapter_softc;
   2129 	int s;
   2130 
   2131 	scsi_print_addr(sc_link);
   2132 	printf("timed out");
   2133 
   2134 	s = splbio();
   2135 
   2136 	if (acb->flags & ACB_ABORT) {
   2137 		/* abort timed out */
   2138 		printf(" AGAIN\n");
   2139 		/* XXX Must reset! */
   2140 	} else {
   2141 		/* abort the operation that has timed out */
   2142 		printf("\n");
   2143 		acb->xs->error = XS_TIMEOUT;
   2144 		aic_abort(sc, acb);
   2145 	}
   2146 
   2147 	splx(s);
   2148 }
   2149 
   2150 #ifdef AIC_DEBUG
   2152 /*
   2153  * The following functions are mostly used for debugging purposes, either
   2154  * directly called from the driver or from the kernel debugger.
   2155  */
   2156 
   2157 void
   2158 aic_show_scsi_cmd(acb)
   2159 	struct aic_acb *acb;
   2160 {
   2161 	u_char  *b = (u_char *)&acb->scsipi_cmd;
   2162 	struct scsipi_link *sc_link = acb->xs->sc_link;
   2163 	int i;
   2164 
   2165 	scsi_print_addr(sc_link);
   2166 	if ((acb->xs->xs_control & XS_CTL_RESET) == 0) {
   2167 		for (i = 0; i < acb->scsipi_cmd_length; i++) {
   2168 			if (i)
   2169 				printf(",");
   2170 			printf("%x", b[i]);
   2171 		}
   2172 		printf("\n");
   2173 	} else
   2174 		printf("RESET\n");
   2175 }
   2176 
   2177 void
   2178 aic_print_acb(acb)
   2179 	struct aic_acb *acb;
   2180 {
   2181 
   2182 	printf("acb@%p xs=%p flags=%x", acb, acb->xs, acb->flags);
   2183 	printf(" dp=%p dleft=%d target_stat=%x\n",
   2184 	       acb->data_addr, acb->data_length, acb->target_stat);
   2185 	aic_show_scsi_cmd(acb);
   2186 }
   2187 
   2188 void
   2189 aic_print_active_acb()
   2190 {
   2191 	extern struct cfdriver aic_cd;
   2192 	struct aic_acb *acb;
   2193 	struct aic_softc *sc = aic_cd.cd_devs[0];
   2194 
   2195 	printf("ready list:\n");
   2196 	for (acb = sc->ready_list.tqh_first; acb != NULL;
   2197 	    acb = acb->chain.tqe_next)
   2198 		aic_print_acb(acb);
   2199 	printf("nexus:\n");
   2200 	if (sc->sc_nexus != NULL)
   2201 		aic_print_acb(sc->sc_nexus);
   2202 	printf("nexus list:\n");
   2203 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
   2204 	    acb = acb->chain.tqe_next)
   2205 		aic_print_acb(acb);
   2206 }
   2207 
   2208 void
   2209 aic_dump6360(sc)
   2210 	struct aic_softc *sc;
   2211 {
   2212 	bus_space_tag_t iot = sc->sc_iot;
   2213 	bus_space_handle_t ioh = sc->sc_ioh;
   2214 
   2215 	printf("aic6360: SCSISEQ=%x SXFRCTL0=%x SXFRCTL1=%x SCSISIG=%x\n",
   2216 	    bus_space_read_1(iot, ioh, SCSISEQ),
   2217 	    bus_space_read_1(iot, ioh, SXFRCTL0),
   2218 	    bus_space_read_1(iot, ioh, SXFRCTL1),
   2219 	    bus_space_read_1(iot, ioh, SCSISIG));
   2220 	printf("         SSTAT0=%x SSTAT1=%x SSTAT2=%x SSTAT3=%x SSTAT4=%x\n",
   2221 	    bus_space_read_1(iot, ioh, SSTAT0),
   2222 	    bus_space_read_1(iot, ioh, SSTAT1),
   2223 	    bus_space_read_1(iot, ioh, SSTAT2),
   2224 	    bus_space_read_1(iot, ioh, SSTAT3),
   2225 	    bus_space_read_1(iot, ioh, SSTAT4));
   2226 	printf("         SIMODE0=%x SIMODE1=%x DMACNTRL0=%x DMACNTRL1=%x "
   2227 	    "DMASTAT=%x\n",
   2228 	    bus_space_read_1(iot, ioh, SIMODE0),
   2229 	    bus_space_read_1(iot, ioh, SIMODE1),
   2230 	    bus_space_read_1(iot, ioh, DMACNTRL0),
   2231 	    bus_space_read_1(iot, ioh, DMACNTRL1),
   2232 	    bus_space_read_1(iot, ioh, DMASTAT));
   2233 	printf("         FIFOSTAT=%d SCSIBUS=0x%x\n",
   2234 	    bus_space_read_1(iot, ioh, FIFOSTAT),
   2235 	    bus_space_read_1(iot, ioh, SCSIBUS));
   2236 }
   2237 
   2238 void
   2239 aic_dump_driver(sc)
   2240 	struct aic_softc *sc;
   2241 {
   2242 	struct aic_tinfo *ti;
   2243 	int i;
   2244 
   2245 	printf("nexus=%p prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
   2246 	printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x "
   2247 	    "currmsg=%x\n",
   2248 	    sc->sc_state, sc->sc_imess[0],
   2249 	    sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
   2250 	for (i = 0; i < 7; i++) {
   2251 		ti = &sc->sc_tinfo[i];
   2252 		printf("tinfo%d: %d cmds %d disconnects %d timeouts",
   2253 		    i, ti->cmds, ti->dconns, ti->touts);
   2254 		printf(" %d senses flags=%x\n", ti->senses, ti->flags);
   2255 	}
   2256 }
   2257 #endif
   2258