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