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