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