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