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