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