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