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