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