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