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