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