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