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