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