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