Home | History | Annotate | Line # | Download | only in dev
spc.c revision 1.15
      1 /*	$NetBSD: spc.c,v 1.15 1998/07/04 22:18:46 jonathan 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 "opt_ddb.h"
    113 
    114 #include <sys/types.h>
    115 #include <sys/param.h>
    116 #include <sys/systm.h>
    117 #include <sys/kernel.h>
    118 #include <sys/errno.h>
    119 #include <sys/ioctl.h>
    120 #include <sys/device.h>
    121 #include <sys/buf.h>
    122 #include <sys/proc.h>
    123 #include <sys/user.h>
    124 #include <sys/queue.h>
    125 
    126 #include <dev/scsipi/scsi_all.h>
    127 #include <dev/scsipi/scsipi_all.h>
    128 #include <dev/scsipi/scsi_message.h>
    129 #include <dev/scsipi/scsiconf.h>
    130 
    131 #include <x68k/x68k/iodevice.h>
    132 #include <x68k/dev/mb89352reg.h>
    133 
    134 /*
    135  * Definitions, most of them has turned out to be unneccesary, but here they
    136  * are anyway.
    137  */
    138 
    139 #define IOBASE		sc->sc_iobase
    140 #define BDID		(IOBASE->scsi_bdid)
    141 #define SCTL		(IOBASE->scsi_sctl)
    142 #define SCMD		(IOBASE->scsi_scmd)
    143 #define TMOD		(IOBASE->scsi_tmod)
    144 #define INTS		(IOBASE->scsi_ints)
    145 #define PSNS		(IOBASE->scsi_psns)
    146 #define SSTS		(IOBASE->scsi_ssts)
    147 #define SERR		(IOBASE->scsi_serr)
    148 #define PCTL		(IOBASE->scsi_pctl)
    149 #define MBC		(IOBASE->scsi_mbc)
    150 #define DREG		(IOBASE->scsi_dreg)
    151 #define TEMP		(IOBASE->scsi_temp)
    152 #define TCH		(IOBASE->scsi_tch)
    153 #define TCM		(IOBASE->scsi_tcm)
    154 #define TCL		(IOBASE->scsi_tcl)
    155 #define EXBF		(IOBASE->scsi_exbf)
    156 
    157 /* PSNS */
    158 #define REQI		0x80
    159 #define ACKI		0x40
    160 #define ATNI		0x20
    161 #define SELI		0x10
    162 #define BSYI		0x08
    163 #define MSGI		0x04
    164 #define CDI		0x02
    165 #define IOI		0x01
    166 
    167 /* Important! The 3 most significant bits of this register, in initiator mode,
    168  * represents the "expected" SCSI bus phase and can be used to trigger phase
    169  * mismatch and phase change interrupts.  But more important:  If there is a
    170  * phase mismatch the chip will not transfer any data!  This is actually a nice
    171  * feature as it gives us a bit more control over what is happening when we are
    172  * bursting data (in) through the FIFOs and the phase suddenly changes from
    173  * DATA IN to STATUS or MESSAGE IN.  The transfer will stop and wait for the
    174  * proper phase to be set in this register instead of dumping the bits into the
    175  * FIFOs.
    176  */
    177 #if 0
    178 #define REQO		0x80
    179 #define ACKO		0x40
    180 #define ATNO		0x20
    181 #define SELO		0x10
    182 #define BSYO		0x08
    183 #endif
    184 /* PCTL */
    185 #define MSGO		0x04
    186 #define CDO		0x02
    187 #define IOO		0x01
    188 
    189 /* Information transfer phases */
    190 #define PH_DATAOUT	(0)
    191 #define PH_DATAIN	(IOI)
    192 #define PH_CMD		(CDI)
    193 #define PH_STAT		(CDI | IOI)
    194 #define PH_MSGOUT	(MSGI | CDI)
    195 #define PH_MSGIN	(MSGI | CDI | IOI)
    196 
    197 #define PH_MASK		(MSGI | CDI | IOI)
    198 
    199 #define	PH_INVALID	0xff
    200 
    201 /* SCSI selection/reselection ID (both target *and* initiator) */
    202 #define SELID7		0x80
    203 #define SELID6		0x40
    204 #define SELID5		0x20
    205 #define SELID4		0x10
    206 #define SELID3		0x08
    207 #define SELID2		0x04
    208 #define SELID1		0x02
    209 #define SELID0		0x01
    210 
    211 #ifndef DDB
    213 #define	Debugger() panic("should call debugger here (spc.c)")
    214 #endif /* ! DDB */
    215 
    216 /*
    217  * ACB. Holds additional information for each SCSI command Comments: We
    218  * need a separate scsi command block because we may need to overwrite it
    219  * with a request sense command.  Basicly, we refrain from fiddling with
    220  * the scsi_xfer struct (except do the expected updating of return values).
    221  * We'll generally update: xs->{flags,resid,error,sense,status} and
    222  * occasionally xs->retries.
    223  */
    224 struct spc_acb {
    225 	struct scsi_generic scsi_cmd;
    226 	int scsi_cmd_length;
    227 	u_char *data_addr;		/* Saved data pointer */
    228 	int data_length;		/* Residue */
    229 
    230 	u_char target_stat;		/* SCSI status byte */
    231 
    232 /*	struct spc_dma_seg dma[SPC_NSEG];*/ /* Physical addresses+len */
    233 
    234 	TAILQ_ENTRY(spc_acb) chain;
    235 	struct scsipi_xfer *xs;	/* SCSI xfer ctrl block from above */
    236 	int flags;
    237 #define ACB_ALLOC	0x01
    238 #define	ACB_NEXUS	0x02
    239 #define ACB_SENSE	0x04
    240 #define	ACB_ABORT	0x40
    241 #define	ACB_RESET	0x80
    242 	int timeout;
    243 };
    244 
    245 /*
    246  * Some info about each (possible) target on the SCSI bus.  This should
    247  * probably have been a "per target+lunit" structure, but we'll leave it at
    248  * this for now.
    249  */
    250 struct spc_tinfo {
    251 	int	cmds;		/* #commands processed */
    252 	int	dconns;		/* #disconnects */
    253 	int	touts;		/* #timeouts */
    254 	int	perrs;		/* #parity errors */
    255 	int	senses;		/* #request sense commands sent */
    256 	ushort	lubusy;		/* What local units/subr. are busy? */
    257 	u_char  flags;
    258 #define DO_SYNC		0x01	/* (Re)Negotiate synchronous options */
    259 #define	DO_WIDE		0x02	/* (Re)Negotiate wide options */
    260 	u_char  period;		/* Period suggestion */
    261 	u_char  offset;		/* Offset suggestion */
    262 	u_char	width;		/* Width suggestion */
    263 } tinfo_t;
    264 
    265 struct spc_softc {
    266 	struct device sc_dev;
    267 	volatile struct mb89352 *sc_iobase;
    268 
    269 	struct scsipi_link sc_link;	/* prototype for subdevs */
    270 
    271 	TAILQ_HEAD(, spc_acb) free_list, ready_list, nexus_list;
    272 	struct spc_acb *sc_nexus;	/* current command */
    273 	struct spc_acb sc_acb[8];
    274 	struct spc_tinfo sc_tinfo[8];
    275 
    276 	/* Data about the current nexus (updated for every cmd switch) */
    277 	u_char	*sc_dp;		/* Current data pointer */
    278 	size_t	sc_dleft;	/* Data bytes left to transfer */
    279 	u_char	*sc_cp;		/* Current command pointer */
    280 	size_t	sc_cleft;	/* Command bytes left to transfer */
    281 
    282 	/* Adapter state */
    283 	u_char	 sc_phase;	/* Current bus phase */
    284 	u_char	 sc_prevphase;	/* Previous bus phase */
    285 	u_char	 sc_state;	/* State applicable to the adapter */
    286 #define	SPC_INIT	0
    287 #define SPC_IDLE	1
    288 #define SPC_SELECTING	2	/* SCSI command is arbiting  */
    289 #define SPC_RESELECTED	3	/* Has been reselected */
    290 #define SPC_CONNECTED	4	/* Actively using the SCSI bus */
    291 #define	SPC_DISCONNECT	5	/* MSG_DISCONNECT received */
    292 #define	SPC_CMDCOMPLETE	6	/* MSG_CMDCOMPLETE received */
    293 #define SPC_CLEANING	7
    294 	u_char	 sc_flags;
    295 #define SPC_DROP_MSGIN	0x01	/* Discard all msgs (parity err detected) */
    296 #define	SPC_ABORTING	0x02	/* Bailing out */
    297 #define SPC_DOINGDMA	0x04	/* The FIFO data path is active! */
    298 	u_char	sc_selid;	/* Reselection ID */
    299 
    300 	/* Message stuff */
    301 	u_char	sc_msgpriq;	/* Messages we want to send */
    302 	u_char	sc_msgoutq;	/* Messages sent during last MESSAGE OUT */
    303 	u_char	sc_lastmsg;	/* Message last transmitted */
    304 	u_char	sc_currmsg;	/* Message currently ready to transmit */
    305 #define SEND_DEV_RESET		0x01
    306 #define SEND_PARITY_ERROR	0x02
    307 #define SEND_INIT_DET_ERR	0x04
    308 #define SEND_REJECT		0x08
    309 #define SEND_IDENTIFY  		0x10
    310 #define SEND_ABORT		0x20
    311 #define SEND_SDTR		0x40
    312 #define	SEND_WDTR		0x80
    313 #define SPC_MAX_MSG_LEN 8
    314 	u_char  sc_omess[SPC_MAX_MSG_LEN];
    315 	u_char	*sc_omp;		/* Outgoing message pointer */
    316 	u_char	sc_imess[SPC_MAX_MSG_LEN];
    317 	u_char	*sc_imp;		/* Incoming message pointer */
    318 
    319 	/* Hardware stuff */
    320 	int	sc_initiator;		/* Our scsi id */
    321 	int	sc_freq;		/* Clock frequency in MHz */
    322 	int	sc_minsync;		/* Minimum sync period / 4 */
    323 	int	sc_maxsync;		/* Maximum sync period / 4 */
    324 };
    325 
    326 #if SPC_DEBUG
    327 #define SPC_SHOWACBS	0x01
    328 #define SPC_SHOWINTS	0x02
    329 #define SPC_SHOWCMDS	0x04
    330 #define SPC_SHOWMISC	0x08
    331 #define SPC_SHOWTRACE	0x10
    332 #define SPC_SHOWSTART	0x20
    333 #define SPC_DOBREAK	0x40
    334 int spc_debug = 0x00; /* SPC_SHOWSTART|SPC_SHOWMISC|SPC_SHOWTRACE; */
    335 #define	SPC_PRINT(b, s)	do {if ((spc_debug & (b)) != 0) printf s;} while (0)
    336 #define	SPC_BREAK()	do {if ((spc_debug & SPC_DOBREAK) != 0) Debugger();} while (0)
    337 #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)
    338 #else
    339 #define	SPC_PRINT(b, s)
    340 #define	SPC_BREAK()
    341 #define	SPC_ASSERT(x)
    342 #endif
    343 
    344 #define SPC_ACBS(s)	SPC_PRINT(SPC_SHOWACBS, s)
    345 #define SPC_INTS(s)	SPC_PRINT(SPC_SHOWINTS, s)
    346 #define SPC_CMDS(s)	SPC_PRINT(SPC_SHOWCMDS, s)
    347 #define SPC_MISC(s)	SPC_PRINT(SPC_SHOWMISC, s)
    348 #define SPC_TRACE(s)	SPC_PRINT(SPC_SHOWTRACE, s)
    349 #define SPC_START(s)	SPC_PRINT(SPC_SHOWSTART, s)
    350 
    351 int	spcmatch	__P((struct device *, void *, void *));
    352 void	spcattach	__P((struct device *, struct device *, void *));
    353 void	spc_minphys	__P((struct buf *));
    354 int	spcintr		__P((int));
    355 void 	spc_init	__P((struct spc_softc *));
    356 void	spc_done	__P((struct spc_softc *, struct spc_acb *));
    357 void	spc_dequeue	__P((struct spc_softc *, struct spc_acb *));
    358 int	spc_scsi_cmd	__P((struct scsipi_xfer *));
    359 int	spc_poll	__P((struct spc_softc *, struct scsipi_xfer *, int));
    360 integrate void	spc_sched_msgout __P((struct spc_softc *, u_char));
    361 integrate void	spc_setsync	__P((struct spc_softc *, struct spc_tinfo *));
    362 void	spc_select	__P((struct spc_softc *, struct spc_acb *));
    363 void	spc_timeout	__P((void *));
    364 void	spc_sched	__P((struct spc_softc *));
    365 void	spc_scsi_reset	__P((struct spc_softc *));
    366 void	spc_reset	__P((struct spc_softc *));
    367 void	spc_free_acb	__P((struct spc_softc *, struct spc_acb *, int));
    368 struct spc_acb * spc_get_acb __P((struct spc_softc *, int));
    369 int	spc_reselect	__P((struct spc_softc *, u_char));
    370 void	spc_sense	__P((struct spc_softc *, struct spc_acb *));
    371 void	spc_msgin	__P((struct spc_softc *));
    372 void	spc_msgout	__P((struct spc_softc *));
    373 int	spc_dataout_pio	__P((struct spc_softc *, u_char *, int));
    374 int	spc_datain_pio	__P((struct spc_softc *, u_char *, int));
    375 void	spc_abort	__P((struct spc_softc *, struct spc_acb *));
    376 #if SPC_DEBUG
    377 void	spc_print_acb	__P((struct spc_acb *));
    378 void	spc_dump_driver __P((struct spc_softc *));
    379 void	spc_show_scsi_cmd __P((struct spc_acb *));
    380 void	spc_print_active_acb __P((void));
    381 #endif
    382 volatile void *	spc_find	__P((int));
    383 
    384 struct cfattach spc_ca = {
    385 	sizeof(struct spc_softc), spcmatch, spcattach
    386 };
    387 
    388 extern struct cfdriver spc_cd;
    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 {
   1018 			switch (acb->target_stat) {
   1019 			case SCSI_CHECK:
   1020 				/* First, save the return values */
   1021 				xs->resid = acb->data_length;
   1022 				xs->status = acb->target_stat;
   1023 				spc_sense(sc, acb);
   1024 				return;
   1025 			case SCSI_BUSY:
   1026 				xs->error = XS_BUSY;
   1027 				break;
   1028 			case SCSI_OK:
   1029 				xs->resid = acb->data_length;
   1030 				break;
   1031 			default:
   1032 				xs->error = XS_DRIVER_STUFFUP;
   1033 #if SPC_DEBUG
   1034 				printf("%s: spc_done: bad stat 0x%x\n",
   1035 					sc->sc_dev.dv_xname, acb->target_stat);
   1036 #endif
   1037 				break;
   1038 			}
   1039 		}
   1040 	}
   1041 
   1042 	xs->flags |= ITSDONE;
   1043 
   1044 #if SPC_DEBUG
   1045 	if ((spc_debug & SPC_SHOWMISC) != 0) {
   1046 		if (xs->resid != 0)
   1047 			printf("resid=%d ", xs->resid);
   1048 		if (xs->error == XS_SENSE)
   1049 			printf("sense=0x%02x\n", xs->sense.scsi_sense.error_code);
   1050 		else
   1051 			printf("error=%d\n", xs->error);
   1052 	}
   1053 #endif
   1054 
   1055 	/*
   1056 	 * Remove the ACB from whatever queue it happens to be on.
   1057 	 */
   1058 	if (acb->flags & ACB_NEXUS)
   1059 		ti->lubusy &= ~(1 << sc_link->scsipi_scsi.lun);
   1060 	if (acb == sc->sc_nexus) {
   1061 		sc->sc_nexus = NULL;
   1062 		sc->sc_state = SPC_IDLE;
   1063 		spc_sched(sc);
   1064 	} else
   1065 		spc_dequeue(sc, acb);
   1066 
   1067 	spc_free_acb(sc, acb, xs->flags);
   1068 	ti->cmds++;
   1069 	scsipi_done(xs);
   1070 }
   1071 
   1072 void
   1073 spc_dequeue(sc, acb)
   1074 	struct spc_softc *sc;
   1075 	struct spc_acb *acb;
   1076 {
   1077 
   1078 	if (acb->flags & ACB_NEXUS) {
   1079 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1080 	} else {
   1081 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
   1082 	}
   1083 }
   1084 
   1085 /*
   1087  * INTERRUPT/PROTOCOL ENGINE
   1088  */
   1089 
   1090 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
   1091 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
   1092 #define ISEXTMSG(m) ((m) == 0x01)
   1093 
   1094 /*
   1095  * Precondition:
   1096  * The SCSI bus is already in the MSGI phase and there is a message byte
   1097  * on the bus, along with an asserted REQ signal.
   1098  */
   1099 void
   1100 spc_msgin(sc)
   1101 	register struct spc_softc *sc;
   1102 {
   1103 	int n;
   1104 
   1105 	SPC_TRACE(("spc_msgin  "));
   1106 
   1107 	if (sc->sc_prevphase == PH_MSGIN) {
   1108 		/* This is a continuation of the previous message. */
   1109 		n = sc->sc_imp - sc->sc_imess;
   1110 		goto nextbyte;
   1111 	}
   1112 
   1113 	/* This is a new MESSAGE IN phase.  Clean up our state. */
   1114 	sc->sc_flags &= ~SPC_DROP_MSGIN;
   1115 
   1116 nextmsg:
   1117 	n = 0;
   1118 	sc->sc_imp = &sc->sc_imess[n];
   1119 
   1120 nextbyte:
   1121 	/*
   1122 	 * Read a whole message, but don't ack the last byte.  If we reject the
   1123 	 * message, we have to assert ATN during the message transfer phase
   1124 	 * itself.
   1125 	 */
   1126 	for (;;) {
   1127 #if 0
   1128 		for (;;) {
   1129 			if ((PSNS & PSNS_REQ) != 0)
   1130 				break;
   1131 			/* Wait for REQINIT.  XXX Need timeout. */
   1132 		}
   1133 #endif
   1134 		if (INTS != 0) {
   1135 			/*
   1136 			 * Target left MESSAGE IN, probably because it
   1137 			 * a) noticed our ATN signal, or
   1138 			 * b) ran out of messages.
   1139 			 */
   1140 			goto out;
   1141 		}
   1142 
   1143 		/* If parity error, just dump everything on the floor. */
   1144 		if ((SERR & (SERR_SCSI_PAR|SERR_SPC_PAR)) != 0) {
   1145 			sc->sc_flags |= SPC_DROP_MSGIN;
   1146 			spc_sched_msgout(sc, SEND_PARITY_ERROR);
   1147 		}
   1148 
   1149 		/* send TRANSFER command. */
   1150 		TCH = 0;
   1151 		TCM = 0;
   1152 		TCL = 1;
   1153 		PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
   1154 		SCMD = SCMD_XFR; /* | SCMD_PROG_XFR */
   1155 		for (;;) {
   1156 			/*if ((SSTS & SSTS_BUSY) != 0 && (SSTS & SSTS_DREG_EMPTY) != 0)*/
   1157 			if ((SSTS & SSTS_DREG_EMPTY) == 0)
   1158 				break;
   1159 			if (INTS != 0)
   1160 				goto out;
   1161 		}
   1162 
   1163 		/* Gather incoming message bytes if needed. */
   1164 		if ((sc->sc_flags & SPC_DROP_MSGIN) == 0) {
   1165 			if (n >= SPC_MAX_MSG_LEN) {
   1166 				(void) DREG;
   1167 				sc->sc_flags |= SPC_DROP_MSGIN;
   1168 				spc_sched_msgout(sc, SEND_REJECT);
   1169 			} else {
   1170 				*sc->sc_imp++ = DREG;
   1171 				n++;
   1172 				/*
   1173 				 * This testing is suboptimal, but most
   1174 				 * messages will be of the one byte variety, so
   1175 				 * it should not affect performance
   1176 				 * significantly.
   1177 				 */
   1178 				if (n == 1 && IS1BYTEMSG(sc->sc_imess[0]))
   1179 					break;
   1180 				if (n == 2 && IS2BYTEMSG(sc->sc_imess[0]))
   1181 					break;
   1182 				if (n >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
   1183 				    n == sc->sc_imess[1] + 2)
   1184 					break;
   1185 			}
   1186 		} else
   1187 			(void) DREG;
   1188 
   1189 		/*
   1190 		 * If we reach this spot we're either:
   1191 		 * a) in the middle of a multi-byte message, or
   1192 		 * b) dropping bytes.
   1193 		 */
   1194 
   1195 #if 0
   1196 		/* Ack the last byte read. */
   1197 		/*(void) DREG;*/
   1198 		while ((PSNS & ACKI) != 0)
   1199 			;
   1200 #endif
   1201 	}
   1202 
   1203 	SPC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1204 
   1205 	/* We now have a complete message.  Parse it. */
   1206 	switch (sc->sc_state) {
   1207 		struct spc_acb *acb;
   1208 		struct scsipi_link *sc_link;
   1209 		struct spc_tinfo *ti;
   1210 
   1211 	case SPC_CONNECTED:
   1212 		SPC_ASSERT(sc->sc_nexus != NULL);
   1213 		acb = sc->sc_nexus;
   1214 		ti = &sc->sc_tinfo[acb->xs->sc_link->scsipi_scsi.target];
   1215 
   1216 		switch (sc->sc_imess[0]) {
   1217 		case MSG_CMDCOMPLETE:
   1218 			if (sc->sc_dleft < 0) {
   1219 				sc_link = acb->xs->sc_link;
   1220 				printf("%s: %d extra bytes from %d:%d\n",
   1221 				    sc->sc_dev.dv_xname, -sc->sc_dleft,
   1222 				    sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun);
   1223 				acb->data_length = 0;
   1224 			}
   1225 			acb->xs->resid = acb->data_length = sc->sc_dleft;
   1226 			sc->sc_state = SPC_CMDCOMPLETE;
   1227 			break;
   1228 
   1229 		case MSG_PARITY_ERROR:
   1230 			/* Resend the last message. */
   1231 			spc_sched_msgout(sc, sc->sc_lastmsg);
   1232 			break;
   1233 
   1234 		case MSG_MESSAGE_REJECT:
   1235 			SPC_MISC(("message rejected %02x  ", sc->sc_lastmsg));
   1236 			switch (sc->sc_lastmsg) {
   1237 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
   1238 			case SEND_IDENTIFY:
   1239 				ti->flags &= ~(DO_SYNC | DO_WIDE);
   1240 				ti->period = ti->offset = 0;
   1241 				spc_setsync(sc, ti);
   1242 				ti->width = 0;
   1243 				break;
   1244 #endif
   1245 #if SPC_USE_SYNCHRONOUS
   1246 			case SEND_SDTR:
   1247 				ti->flags &= ~DO_SYNC;
   1248 				ti->period = ti->offset = 0;
   1249 				spc_setsync(sc, ti);
   1250 				break;
   1251 #endif
   1252 #if SPC_USE_WIDE
   1253 			case SEND_WDTR:
   1254 				ti->flags &= ~DO_WIDE;
   1255 				ti->width = 0;
   1256 				break;
   1257 #endif
   1258 			case SEND_INIT_DET_ERR:
   1259 				spc_sched_msgout(sc, SEND_ABORT);
   1260 				break;
   1261 			}
   1262 			break;
   1263 
   1264 		case MSG_NOOP:
   1265 			break;
   1266 
   1267 		case MSG_DISCONNECT:
   1268 			ti->dconns++;
   1269 			sc->sc_state = SPC_DISCONNECT;
   1270 			break;
   1271 
   1272 		case MSG_SAVEDATAPOINTER:
   1273 			acb->data_addr = sc->sc_dp;
   1274 			acb->data_length = sc->sc_dleft;
   1275 			break;
   1276 
   1277 		case MSG_RESTOREPOINTERS:
   1278 			sc->sc_dp = acb->data_addr;
   1279 			sc->sc_dleft = acb->data_length;
   1280 			sc->sc_cp = (u_char *)&acb->scsi_cmd;
   1281 			sc->sc_cleft = acb->scsi_cmd_length;
   1282 			break;
   1283 
   1284 		case MSG_EXTENDED:
   1285 			switch (sc->sc_imess[2]) {
   1286 #if SPC_USE_SYNCHRONOUS
   1287 			case MSG_EXT_SDTR:
   1288 				if (sc->sc_imess[1] != 3)
   1289 					goto reject;
   1290 				ti->period = sc->sc_imess[3];
   1291 				ti->offset = sc->sc_imess[4];
   1292 				ti->flags &= ~DO_SYNC;
   1293 				if (ti->offset == 0) {
   1294 				} else if (ti->period < sc->sc_minsync ||
   1295 					   ti->period > sc->sc_maxsync ||
   1296 					   ti->offset > 8) {
   1297 					ti->period = ti->offset = 0;
   1298 					spc_sched_msgout(sc, SEND_SDTR);
   1299 				} else {
   1300 					scsi_print_addr(acb->xs->sc_link);
   1301 					printf("sync, offset %d, period %dnsec\n",
   1302 					    ti->offset, ti->period * 4);
   1303 				}
   1304 				spc_setsync(sc, ti);
   1305 				break;
   1306 #endif
   1307 
   1308 #if SPC_USE_WIDE
   1309 			case MSG_EXT_WDTR:
   1310 				if (sc->sc_imess[1] != 2)
   1311 					goto reject;
   1312 				ti->width = sc->sc_imess[3];
   1313 				ti->flags &= ~DO_WIDE;
   1314 				if (ti->width == 0) {
   1315 				} else if (ti->width > SPC_MAX_WIDTH) {
   1316 					ti->width = 0;
   1317 					spc_sched_msgout(sc, SEND_WDTR);
   1318 				} else {
   1319 					scsi_print_addr(acb->xs->sc_link);
   1320 					printf("wide, width %d\n",
   1321 					    1 << (3 + ti->width));
   1322 				}
   1323 				break;
   1324 #endif
   1325 
   1326 			default:
   1327 				printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
   1328 				    sc->sc_dev.dv_xname);
   1329 				SPC_BREAK();
   1330 				goto reject;
   1331 			}
   1332 			break;
   1333 
   1334 		default:
   1335 			printf("%s: unrecognized MESSAGE; sending REJECT\n",
   1336 			    sc->sc_dev.dv_xname);
   1337 			SPC_BREAK();
   1338 		reject:
   1339 			spc_sched_msgout(sc, SEND_REJECT);
   1340 			break;
   1341 		}
   1342 		break;
   1343 
   1344 	case SPC_RESELECTED:
   1345 		if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
   1346 			printf("%s: reselect without IDENTIFY; sending DEVICE RESET\n",
   1347 			    sc->sc_dev.dv_xname);
   1348 			SPC_BREAK();
   1349 			goto reset;
   1350 		}
   1351 
   1352 		(void) spc_reselect(sc, sc->sc_imess[0]);
   1353 		break;
   1354 
   1355 	default:
   1356 		printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
   1357 		    sc->sc_dev.dv_xname);
   1358 		SPC_BREAK();
   1359 	reset:
   1360 		spc_sched_msgout(sc, SEND_DEV_RESET);
   1361 		break;
   1362 
   1363 	abort:
   1364 		spc_sched_msgout(sc, SEND_ABORT);
   1365 		break;
   1366 	}
   1367 
   1368 	/* Ack the last message byte. */
   1369 #if 0 /* XXX? */
   1370 	(void) DREG;
   1371 	while ((PSNS & ACKI) != 0)
   1372 		;
   1373 #endif
   1374 
   1375 	/* Go get the next message, if any. */
   1376 	goto nextmsg;
   1377 
   1378 out:
   1379 	SCMD = SCMD_RST_ACK;
   1380 	SPC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1381 }
   1382 
   1383 /*
   1384  * Send the highest priority, scheduled message.
   1385  */
   1386 void
   1387 spc_msgout(sc)
   1388 	register struct spc_softc *sc;
   1389 {
   1390 	struct spc_tinfo *ti;
   1391 	int n;
   1392 
   1393 	SPC_TRACE(("spc_msgout  "));
   1394 
   1395 	if (sc->sc_prevphase == PH_MSGOUT) {
   1396 		if (sc->sc_omp == sc->sc_omess) {
   1397 			/*
   1398 			 * This is a retransmission.
   1399 			 *
   1400 			 * We get here if the target stayed in MESSAGE OUT
   1401 			 * phase.  Section 5.1.9.2 of the SCSI 2 spec indicates
   1402 			 * that all of the previously transmitted messages must
   1403 			 * be sent again, in the same order.  Therefore, we
   1404 			 * requeue all the previously transmitted messages, and
   1405 			 * start again from the top.  Our simple priority
   1406 			 * scheme keeps the messages in the right order.
   1407 			 */
   1408 			SPC_MISC(("retransmitting  "));
   1409 			sc->sc_msgpriq |= sc->sc_msgoutq;
   1410 			/*
   1411 			 * Set ATN.  If we're just sending a trivial 1-byte
   1412 			 * message, we'll clear ATN later on anyway.
   1413 			 */
   1414 			SCMD = SCMD_SET_ATN; /* XXX? */
   1415 		} else {
   1416 			/* This is a continuation of the previous message. */
   1417 			n = sc->sc_omp - sc->sc_omess;
   1418 			goto nextbyte;
   1419 		}
   1420 	}
   1421 
   1422 	/* No messages transmitted so far. */
   1423 	sc->sc_msgoutq = 0;
   1424 	sc->sc_lastmsg = 0;
   1425 
   1426 nextmsg:
   1427 	/* Pick up highest priority message. */
   1428 	sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
   1429 	sc->sc_msgpriq &= ~sc->sc_currmsg;
   1430 	sc->sc_msgoutq |= sc->sc_currmsg;
   1431 
   1432 	/* Build the outgoing message data. */
   1433 	switch (sc->sc_currmsg) {
   1434 	case SEND_IDENTIFY:
   1435 		SPC_ASSERT(sc->sc_nexus != NULL);
   1436 		sc->sc_omess[0] =
   1437 		    MSG_IDENTIFY(sc->sc_nexus->xs->sc_link->scsipi_scsi.lun, 1);
   1438 		n = 1;
   1439 		break;
   1440 
   1441 #if SPC_USE_SYNCHRONOUS
   1442 	case SEND_SDTR:
   1443 		SPC_ASSERT(sc->sc_nexus != NULL);
   1444 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->scsipi_scsi.target];
   1445 		sc->sc_omess[4] = MSG_EXTENDED;
   1446 		sc->sc_omess[3] = 3;
   1447 		sc->sc_omess[2] = MSG_EXT_SDTR;
   1448 		sc->sc_omess[1] = ti->period >> 2;
   1449 		sc->sc_omess[0] = ti->offset;
   1450 		n = 5;
   1451 		break;
   1452 #endif
   1453 
   1454 #if SPC_USE_WIDE
   1455 	case SEND_WDTR:
   1456 		SPC_ASSERT(sc->sc_nexus != NULL);
   1457 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->scsipi_scsi.target];
   1458 		sc->sc_omess[3] = MSG_EXTENDED;
   1459 		sc->sc_omess[2] = 2;
   1460 		sc->sc_omess[1] = MSG_EXT_WDTR;
   1461 		sc->sc_omess[0] = ti->width;
   1462 		n = 4;
   1463 		break;
   1464 #endif
   1465 
   1466 	case SEND_DEV_RESET:
   1467 		sc->sc_flags |= SPC_ABORTING;
   1468 		sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1469 		n = 1;
   1470 		break;
   1471 
   1472 	case SEND_REJECT:
   1473 		sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1474 		n = 1;
   1475 		break;
   1476 
   1477 	case SEND_PARITY_ERROR:
   1478 		sc->sc_omess[0] = MSG_PARITY_ERROR;
   1479 		n = 1;
   1480 		break;
   1481 
   1482 	case SEND_INIT_DET_ERR:
   1483 		sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1484 		n = 1;
   1485 		break;
   1486 
   1487 	case SEND_ABORT:
   1488 		sc->sc_flags |= SPC_ABORTING;
   1489 		sc->sc_omess[0] = MSG_ABORT;
   1490 		n = 1;
   1491 		break;
   1492 
   1493 	default:
   1494 		printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
   1495 		    sc->sc_dev.dv_xname);
   1496 		SPC_BREAK();
   1497 		sc->sc_omess[0] = MSG_NOOP;
   1498 		n = 1;
   1499 		break;
   1500 	}
   1501 	sc->sc_omp = &sc->sc_omess[n];
   1502 
   1503 nextbyte:
   1504 	/* Send message bytes. */
   1505 	/* send TRANSFER command. */
   1506 	TCH = n >> 16;
   1507 	TCM = n >> 8;
   1508 	TCL = n;
   1509 	PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
   1510 	SCMD = SCMD_XFR; /* | SCMD_PROG_XFR */
   1511 	for (;;) {
   1512 		if ((SSTS & SSTS_BUSY) != 0)
   1513 			break;
   1514 		if (INTS != 0)
   1515 			goto out;
   1516 	}
   1517 	for (;;) {
   1518 #if 0
   1519 		for (;;) {
   1520 			if ((PSNS & PSNS_REQ) != 0)
   1521 				break;
   1522 			/* Wait for REQINIT.  XXX Need timeout. */
   1523 		}
   1524 #endif
   1525 		if (INTS != 0) {
   1526 			/*
   1527 			 * Target left MESSAGE OUT, possibly to reject
   1528 			 * our message.
   1529 			 *
   1530 			 * If this is the last message being sent, then we
   1531 			 * deassert ATN, since either the target is going to
   1532 			 * ignore this message, or it's going to ask for a
   1533 			 * retransmission via MESSAGE PARITY ERROR (in which
   1534 			 * case we reassert ATN anyway).
   1535 			 */
   1536 #if 0
   1537 			if (sc->sc_msgpriq == 0)
   1538 				SCMD = SCMD_RST_ATN;
   1539 #endif
   1540 			goto out;
   1541 		}
   1542 
   1543 #if 0
   1544 		/* Clear ATN before last byte if this is the last message. */
   1545 		if (n == 1 && sc->sc_msgpriq == 0)
   1546 			SCMD = SCMD_RST_ATN;
   1547 #endif
   1548 
   1549 		while ((SSTS & SSTS_DREG_FULL) != 0)
   1550 			;
   1551 		/* Send message byte. */
   1552 		DREG = *--sc->sc_omp;
   1553 		--n;
   1554 		/* Keep track of the last message we've sent any bytes of. */
   1555 		sc->sc_lastmsg = sc->sc_currmsg;
   1556 #if 0
   1557 		/* Wait for ACK to be negated.  XXX Need timeout. */
   1558 		while ((PSNS & ACKI) != 0)
   1559 			;
   1560 #endif
   1561 
   1562 		if (n == 0)
   1563 			break;
   1564 	}
   1565 
   1566 	/* We get here only if the entire message has been transmitted. */
   1567 	if (sc->sc_msgpriq != 0) {
   1568 		/* There are more outgoing messages. */
   1569 		goto nextmsg;
   1570 	}
   1571 
   1572 	/*
   1573 	 * The last message has been transmitted.  We need to remember the last
   1574 	 * message transmitted (in case the target switches to MESSAGE IN phase
   1575 	 * and sends a MESSAGE REJECT), and the list of messages transmitted
   1576 	 * this time around (in case the target stays in MESSAGE OUT phase to
   1577 	 * request a retransmit).
   1578 	 */
   1579 
   1580 out:
   1581 	/* Disable REQ/ACK protocol. */
   1582 }
   1583 
   1584 /*
   1586  * This new revision has been optimized (I tried) to make the common case fast,
   1587  * and the rarer cases (as a result) somewhat more comlex
   1588  */
   1589 int
   1590 spc_dataout_pio(sc, p, n)
   1591 	register struct spc_softc *sc;
   1592 	u_char *p;
   1593 	int n;
   1594 {
   1595 	register u_char intstat = 0;
   1596 	int out = 0;
   1597 #define DOUTAMOUNT 8		/* Full FIFO */
   1598 
   1599 	/* send TRANSFER command. */
   1600 	TCH = n >> 16;
   1601 	TCM = n >> 8;
   1602 	TCL = n;
   1603 	PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
   1604 	SCMD = SCMD_XFR;
   1605 	for (;;) {
   1606 		if ((SSTS & SSTS_BUSY) != 0)
   1607 			break;
   1608 		if (INTS != 0)
   1609 			break;
   1610 	}
   1611 
   1612 	/*
   1613 	 * I have tried to make the main loop as tight as possible.  This
   1614 	 * means that some of the code following the loop is a bit more
   1615 	 * complex than otherwise.
   1616 	 */
   1617 	while (n > 0) {
   1618 		int xfer;
   1619 
   1620 		for (;;) {
   1621 			intstat = INTS;
   1622 			/* $B%P%C%U%!$,6u$K$J$k$^$GBT$D(B */
   1623 			if ((SSTS & SSTS_DREG_EMPTY) != 0)
   1624 				break;
   1625 			/* $B$?$@$73d$j9~$_$,F~$C$F$-$?$iH4$1$k(B */
   1626 			if (intstat != 0)
   1627 				goto phasechange;
   1628 		}
   1629 
   1630 		xfer = min(DOUTAMOUNT, n);
   1631 
   1632 		SPC_MISC(("%d> ", xfer));
   1633 
   1634 		n -= xfer;
   1635 		out += xfer;
   1636 
   1637 		while (xfer-- > 0) {
   1638 			DREG = *p++;
   1639 		}
   1640 	}
   1641 
   1642 	if (out == 0) {
   1643 		for (;;) {
   1644 			if (INTS != 0)
   1645 				break;
   1646 		}
   1647 		SPC_MISC(("extra data  "));
   1648 	} else {
   1649 		/* See the bytes off chip */
   1650 		for (;;) {
   1651 			/* $B%P%C%U%!$,6u$K$J$k$^$GBT$D(B */
   1652 			if ((SSTS & SSTS_DREG_EMPTY) != 0)
   1653 				break;
   1654 			intstat = INTS;
   1655 			/* $B$?$@$73d$j9~$_$,F~$C$F$-$?$iH4$1$k(B */
   1656 			if (intstat != 0)
   1657 				goto phasechange;
   1658 		}
   1659 	}
   1660 
   1661 phasechange:
   1662 	/* Stop the FIFO data path. */
   1663 
   1664 	if (intstat != 0) {
   1665 		/* Some sort of phase change. */
   1666 		int amount;
   1667 
   1668 		amount = (TCH << 16) | (TCM << 8) | TCL;
   1669 		if (amount > 0) {
   1670 			out -= amount;
   1671 			SPC_MISC(("+%d ", amount));
   1672 		}
   1673 	}
   1674 	/* Turn on ENREQINIT again. */
   1675 
   1676 	return out;
   1677 }
   1678 
   1679 /*
   1681  * For now, uses a pretty dumb algorithm, hangs around until all data has been
   1682  * transferred.  This, is OK for fast targets, but not so smart for slow
   1683  * targets which don't disconnect or for huge transfers.
   1684  */
   1685 int
   1686 spc_datain_pio(sc, p, n)
   1687 	register struct spc_softc *sc;
   1688 	u_char *p;
   1689 	int n;
   1690 {
   1691 	register u_short intstat;
   1692 	int in = 0;
   1693 #define DINAMOUNT 8		/* Full FIFO */
   1694 
   1695 	/* send TRANSFER command. */
   1696 	TCH = n >> 16;
   1697 	TCM = n >> 8;
   1698 	TCL = n;
   1699 	PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
   1700 	SCMD = SCMD_XFR;
   1701 	for (;;) {
   1702 		if ((SSTS & SSTS_BUSY) != 0)
   1703 			break;
   1704 		if (INTS != 0)
   1705 			goto phasechange;
   1706 	}
   1707 
   1708 	/*
   1709 	 * We leave this loop if one or more of the following is true:
   1710 	 * a) phase != PH_DATAIN && FIFOs are empty
   1711 	 * b) reset has occurred or busfree is detected.
   1712 	 */
   1713 	while (n > 0) {
   1714 		int xfer;
   1715 
   1716 #define INTSMASK 0xff
   1717 		/* Wait for fifo half full or phase mismatch */
   1718 		for (;;) {
   1719 			intstat = (SSTS << 8) | INTS;
   1720 			if ((intstat & (INTSMASK | (SSTS_DREG_FULL << 8))) != 0)
   1721 				break;
   1722 			if ((intstat & (SSTS_DREG_EMPTY << 8)) == 0)
   1723 				break;
   1724 		}
   1725 
   1726 #if 1
   1727 		if ((intstat & INTSMASK) != 0)
   1728 			goto phasechange;
   1729 #else
   1730 		if ((intstat & INTSMASK) != 0 &&
   1731 		    (intstat & (SSTS_DREG_EMPTY << 8)))
   1732 			goto phasechange;
   1733 #endif
   1734 		if ((intstat & (SSTS_DREG_FULL << 8)) != 0)
   1735 			xfer = min(DINAMOUNT, n);
   1736 		else
   1737 			xfer = min(1, n);
   1738 
   1739 		SPC_MISC((">%d ", xfer));
   1740 
   1741 		n -= xfer;
   1742 		in += xfer;
   1743 
   1744 		while (xfer-- > 0) {
   1745 			*p++ = DREG;
   1746 		}
   1747 
   1748 		if ((intstat & INTSMASK) != 0)
   1749 			goto phasechange;
   1750 	}
   1751 
   1752 	/*
   1753 	 * Some SCSI-devices are rude enough to transfer more data than what
   1754 	 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
   1755 	 * requested 512.  Test for progress, i.e. real transfers.  If no real
   1756 	 * transfers have been performed (n is probably already zero) and the
   1757 	 * FIFO is not empty, waste some bytes....
   1758 	 */
   1759 	if (in == 0) {
   1760 		for (;;) {
   1761 			if (INTS != 0)
   1762 				break;
   1763 		}
   1764 		SPC_MISC(("extra data  "));
   1765 	}
   1766 
   1767 phasechange:
   1768 	/* Stop the FIFO data path. */
   1769 
   1770 	/* Turn on ENREQINIT again. */
   1771 
   1772 	return in;
   1773 }
   1774 
   1775 /*
   1777  * Catch an interrupt from the adaptor
   1778  */
   1779 /*
   1780  * This is the workhorse routine of the driver.
   1781  * Deficiencies (for now):
   1782  * 1) always uses programmed I/O
   1783  */
   1784 int
   1785 spcintr(unit)
   1786 	int unit;
   1787 {
   1788 	struct spc_softc *sc = spc_cd.cd_devs[unit]; /* XXX */
   1789 	u_char ints;
   1790 	struct spc_acb *acb;
   1791 	struct scsipi_link *sc_link;
   1792 	struct spc_tinfo *ti;
   1793 	int n;
   1794 
   1795 	/* return if not configured */
   1796 	if (sc == NULL)
   1797 		return;
   1798 	/*
   1799 	 * $B3d$j9~$_6X;_$K$9$k(B
   1800 	 */
   1801 	SCTL &= ~SCTL_INTR_ENAB;
   1802 
   1803 	SPC_TRACE(("spcintr  "));
   1804 
   1805 loop:
   1806 	/*
   1807 	 * $BA4E>Aw$,40A4$K=*N;$9$k$^$G%k!<%W$9$k(B
   1808 	 */
   1809 	/*
   1810 	 * First check for abnormal conditions, such as reset.
   1811 	 */
   1812 #if 1 /* XXX? */
   1813 	while ((ints = INTS) == 0)
   1814 		delay(1);
   1815 	SPC_MISC(("ints = 0x%x  ", ints));
   1816 #else /* usually? */
   1817 	ints = INTS;
   1818 #endif
   1819 	if ((ints & INTS_RST) != 0) {
   1820 		printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
   1821 		goto reset;
   1822 	}
   1823 
   1824 	/*
   1825 	 * Check for less serious errors.
   1826 	 */
   1827 	if ((SERR & (SERR_SCSI_PAR|SERR_SPC_PAR)) != 0) {
   1828 		printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
   1829 		if (sc->sc_prevphase == PH_MSGIN) {
   1830 			sc->sc_flags |= SPC_DROP_MSGIN;
   1831 			spc_sched_msgout(sc, SEND_PARITY_ERROR);
   1832 		} else
   1833 			spc_sched_msgout(sc, SEND_INIT_DET_ERR);
   1834 	}
   1835 
   1836 	/*
   1837 	 * If we're not already busy doing something test for the following
   1838 	 * conditions:
   1839 	 * 1) We have been reselected by something
   1840 	 * 2) We have selected something successfully
   1841 	 * 3) Our selection process has timed out
   1842 	 * 4) This is really a bus free interrupt just to get a new command
   1843 	 *    going?
   1844 	 * 5) Spurious interrupt?
   1845 	 */
   1846 	switch (sc->sc_state) {
   1847 	case SPC_IDLE:
   1848 	case SPC_SELECTING:
   1849 
   1850 		if ((ints & INTS_SEL) != 0) {
   1851 			/*
   1852 			 * We don't currently support target mode.
   1853 			 */
   1854 			printf("%s: target mode selected; going to BUS FREE\n",
   1855 			    sc->sc_dev.dv_xname);
   1856 
   1857 			goto sched;
   1858 		} else if ((ints & INTS_RESEL) != 0) {
   1859 			SPC_MISC(("reselected  "));
   1860 
   1861 			/*
   1862 			 * If we're trying to select a target ourselves,
   1863 			 * push our command back into the ready list.
   1864 			 */
   1865 			if (sc->sc_state == SPC_SELECTING) {
   1866 				SPC_MISC(("backoff selector  "));
   1867 				SPC_ASSERT(sc->sc_nexus != NULL);
   1868 				acb = sc->sc_nexus;
   1869 				sc->sc_nexus = NULL;
   1870 				TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   1871 			}
   1872 
   1873 			/* Save reselection ID. */
   1874 			sc->sc_selid = TEMP;
   1875 
   1876 			sc->sc_state = SPC_RESELECTED;
   1877 		} else if ((ints & INTS_CMD_DONE) != 0) {
   1878 			SPC_MISC(("selected  "));
   1879 
   1880 			/*
   1881 			 * We have selected a target. Things to do:
   1882 			 * a) Determine what message(s) to send.
   1883 			 * b) Verify that we're still selecting the target.
   1884 			 * c) Mark device as busy.
   1885 			 */
   1886 			if (sc->sc_state != SPC_SELECTING) {
   1887 				printf("%s: selection out while idle; resetting\n",
   1888 				    sc->sc_dev.dv_xname);
   1889 				SPC_BREAK();
   1890 				goto reset;
   1891 			}
   1892 			SPC_ASSERT(sc->sc_nexus != NULL);
   1893 			acb = sc->sc_nexus;
   1894 			sc_link = acb->xs->sc_link;
   1895 			ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
   1896 
   1897 			sc->sc_msgpriq = SEND_IDENTIFY;
   1898 			if (acb->flags & ACB_RESET)
   1899 				sc->sc_msgpriq |= SEND_DEV_RESET;
   1900 			else if (acb->flags & ACB_ABORT)
   1901 				sc->sc_msgpriq |= SEND_ABORT;
   1902 			else {
   1903 #if SPC_USE_SYNCHRONOUS
   1904 				if ((ti->flags & DO_SYNC) != 0)
   1905 					sc->sc_msgpriq |= SEND_SDTR;
   1906 #endif
   1907 #if SPC_USE_WIDE
   1908 				if ((ti->flags & DO_WIDE) != 0)
   1909 					sc->sc_msgpriq |= SEND_WDTR;
   1910 #endif
   1911 			}
   1912 
   1913 			acb->flags |= ACB_NEXUS;
   1914 			ti->lubusy |= (1 << sc_link->scsipi_scsi.lun);
   1915 
   1916 			/* Do an implicit RESTORE POINTERS. */
   1917 			sc->sc_dp = acb->data_addr;
   1918 			sc->sc_dleft = acb->data_length;
   1919 			sc->sc_cp = (u_char *)&acb->scsi_cmd;
   1920 			sc->sc_cleft = acb->scsi_cmd_length;
   1921 
   1922 			/* On our first connection, schedule a timeout. */
   1923 			if ((acb->xs->flags & SCSI_POLL) == 0)
   1924 				timeout(spc_timeout, acb, (acb->timeout * hz) / 1000);
   1925 
   1926 			sc->sc_state = SPC_CONNECTED;
   1927 		} else if ((ints & INTS_TIMEOUT) != 0) {
   1928 			SPC_MISC(("selection timeout  "));
   1929 
   1930 			if (sc->sc_state != SPC_SELECTING) {
   1931 				printf("%s: selection timeout while idle; resetting\n",
   1932 				    sc->sc_dev.dv_xname);
   1933 				SPC_BREAK();
   1934 				goto reset;
   1935 			}
   1936 			SPC_ASSERT(sc->sc_nexus != NULL);
   1937 			acb = sc->sc_nexus;
   1938 
   1939 			delay(250);
   1940 
   1941 			acb->xs->error = XS_SELTIMEOUT;
   1942 			goto finish;
   1943 		} else {
   1944 			if (sc->sc_state != SPC_IDLE) {
   1945 				printf("%s: BUS FREE while not idle; state=%d\n",
   1946 				    sc->sc_dev.dv_xname, sc->sc_state);
   1947 				SPC_BREAK();
   1948 				goto out;
   1949 			}
   1950 
   1951 			goto sched;
   1952 		}
   1953 
   1954 		/*
   1955 		 * Turn off selection stuff, and prepare to catch bus free
   1956 		 * interrupts, parity errors, and phase changes.
   1957 		 */
   1958 
   1959 		sc->sc_flags = 0;
   1960 		sc->sc_prevphase = PH_INVALID;
   1961 		goto dophase;
   1962 	}
   1963 
   1964 	if ((ints & INTS_DISCON) != 0) {
   1965 		/* We've gone to BUS FREE phase. */
   1966 		PCTL &= ~PCTL_BFINT_ENAB; /* disable disconnect interrupt */
   1967 		INTS = ints; /* XXX reset interrput */
   1968 
   1969 		switch (sc->sc_state) {
   1970 		case SPC_RESELECTED:
   1971 			goto sched;
   1972 
   1973 		case SPC_CONNECTED:
   1974 			SPC_ASSERT(sc->sc_nexus != NULL);
   1975 			acb = sc->sc_nexus;
   1976 
   1977 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
   1978 			if (sc->sc_prevphase == PH_MSGOUT) {
   1979 				/*
   1980 				 * If the target went to BUS FREE phase during
   1981 				 * or immediately after sending a SDTR or WDTR
   1982 				 * message, disable negotiation.
   1983 				 */
   1984 				sc_link = acb->xs->sc_link;
   1985 				ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
   1986 				switch (sc->sc_lastmsg) {
   1987 #if SPC_USE_SYNCHRONOUS
   1988 				case SEND_SDTR:
   1989 					ti->flags &= ~DO_SYNC;
   1990 					ti->period = ti->offset = 0;
   1991 					break;
   1992 #endif
   1993 #if SPC_USE_WIDE
   1994 				case SEND_WDTR:
   1995 					ti->flags &= ~DO_WIDE;
   1996 					ti->width = 0;
   1997 					break;
   1998 #endif
   1999 				}
   2000 			}
   2001 #endif
   2002 
   2003 			if ((sc->sc_flags & SPC_ABORTING) == 0) {
   2004 				/*
   2005 				 * Section 5.1.1 of the SCSI 2 spec suggests
   2006 				 * issuing a REQUEST SENSE following an
   2007 				 * unexpected disconnect.  Some devices go into
   2008 				 * a contingent allegiance condition when
   2009 				 * disconnecting, and this is necessary to
   2010 				 * clean up their state.
   2011 				 */
   2012 				printf("%s: unexpected disconnect; sending REQUEST SENSE\n",
   2013 				    sc->sc_dev.dv_xname);
   2014 				SPC_BREAK();
   2015 				spc_sense(sc, acb);
   2016 				goto out;
   2017 			}
   2018 
   2019 			acb->xs->error = XS_DRIVER_STUFFUP;
   2020 			goto finish;
   2021 
   2022 		case SPC_DISCONNECT:
   2023 			SPC_ASSERT(sc->sc_nexus != NULL);
   2024 			acb = sc->sc_nexus;
   2025 			TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   2026 			sc->sc_nexus = NULL;
   2027 			goto sched;
   2028 
   2029 		case SPC_CMDCOMPLETE:
   2030 			SPC_ASSERT(sc->sc_nexus != NULL);
   2031 			acb = sc->sc_nexus;
   2032 			goto finish;
   2033 		}
   2034 	}
   2035 	else if ((ints & INTS_CMD_DONE) != 0 &&
   2036 		 sc->sc_prevphase == PH_MSGIN && sc->sc_state != SPC_CONNECTED)
   2037 		goto out;
   2038 
   2039 dophase:
   2040 #if 0
   2041 	if ((PSNS & PSNS_REQ) == 0) {
   2042 		/* Wait for REQINIT. */
   2043 		goto out;
   2044 	}
   2045 #else
   2046 	INTS = ints;
   2047 	ints = 0;
   2048 	while ((PSNS & PSNS_REQ) == 0)
   2049 		delay(1);	/* need timeout XXX */
   2050 #endif
   2051 
   2052 	/*
   2053 	 * $B%U%'!<%:$K$h$C$F>uBVA+0\$9$k(B
   2054 	 */
   2055 	sc->sc_phase = PSNS & PH_MASK;
   2056 /*	PCTL = sc->sc_phase;*/
   2057 
   2058 	switch (sc->sc_phase) {
   2059 	case PH_MSGOUT:
   2060 		if (sc->sc_state != SPC_CONNECTED &&
   2061 		    sc->sc_state != SPC_RESELECTED)
   2062 			break;
   2063 		spc_msgout(sc);
   2064 		sc->sc_prevphase = PH_MSGOUT;
   2065 		goto loop;
   2066 
   2067 	case PH_MSGIN:
   2068 		if (sc->sc_state != SPC_CONNECTED &&
   2069 		    sc->sc_state != SPC_RESELECTED)
   2070 			break;
   2071 		spc_msgin(sc);
   2072 		sc->sc_prevphase = PH_MSGIN;
   2073 		goto loop;
   2074 
   2075 	case PH_CMD:
   2076 		if (sc->sc_state != SPC_CONNECTED)
   2077 			break;
   2078 #if SPC_DEBUG
   2079 		if ((spc_debug & SPC_SHOWMISC) != 0) {
   2080 			SPC_ASSERT(sc->sc_nexus != NULL);
   2081 			acb = sc->sc_nexus;
   2082 			printf("cmd=0x%02x+%d  ",
   2083 			    acb->scsi_cmd.opcode, acb->scsi_cmd_length-1);
   2084 		}
   2085 #endif
   2086 		n = spc_dataout_pio(sc, sc->sc_cp, sc->sc_cleft);
   2087 		sc->sc_cp += n;
   2088 		sc->sc_cleft -= n;
   2089 		sc->sc_prevphase = PH_CMD;
   2090 		goto loop;
   2091 
   2092 	case PH_DATAOUT:
   2093 		if (sc->sc_state != SPC_CONNECTED)
   2094 			break;
   2095 		SPC_MISC(("dataout dleft=%d  ", sc->sc_dleft));
   2096 		n = spc_dataout_pio(sc, sc->sc_dp, sc->sc_dleft);
   2097 		sc->sc_dp += n;
   2098 		sc->sc_dleft -= n;
   2099 		sc->sc_prevphase = PH_DATAOUT;
   2100 		goto loop;
   2101 
   2102 	case PH_DATAIN:
   2103 		if (sc->sc_state != SPC_CONNECTED)
   2104 			break;
   2105 		SPC_MISC(("datain  "));
   2106 		n = spc_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
   2107 		sc->sc_dp += n;
   2108 		sc->sc_dleft -= n;
   2109 		sc->sc_prevphase = PH_DATAIN;
   2110 		goto loop;
   2111 
   2112 	case PH_STAT:
   2113 		if (sc->sc_state != SPC_CONNECTED)
   2114 			break;
   2115 		SPC_ASSERT(sc->sc_nexus != NULL);
   2116 		acb = sc->sc_nexus;
   2117 		/*acb->target_stat = DREG;*/
   2118 		spc_datain_pio(sc, &acb->target_stat, 1);
   2119 		SPC_MISC(("target_stat=0x%02x  ", acb->target_stat));
   2120 		sc->sc_prevphase = PH_STAT;
   2121 		goto loop;
   2122 	}
   2123 
   2124 	printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
   2125 	SPC_BREAK();
   2126 reset:
   2127 	spc_init(sc);
   2128 	return 1;
   2129 
   2130 finish:
   2131 	untimeout(spc_timeout, acb);
   2132 	INTS = ints;
   2133 	ints = 0;
   2134 	spc_done(sc, acb);
   2135 	goto out;
   2136 
   2137 sched:
   2138 	sc->sc_state = SPC_IDLE;
   2139 	spc_sched(sc);
   2140 	goto out;
   2141 
   2142 out:
   2143 	if (ints)
   2144 		INTS = ints;
   2145 	SCTL |= SCTL_INTR_ENAB;
   2146 	return 1;
   2147 }
   2148 
   2149 void
   2150 spc_abort(sc, acb)
   2151 	struct spc_softc *sc;
   2152 	struct spc_acb *acb;
   2153 {
   2154 
   2155 	/* 2 secs for the abort */
   2156 	acb->timeout = SPC_ABORT_TIMEOUT;
   2157 	acb->flags |= ACB_ABORT;
   2158 
   2159 	if (acb == sc->sc_nexus) {
   2160 		/*
   2161 		 * If we're still selecting, the message will be scheduled
   2162 		 * after selection is complete.
   2163 		 */
   2164 		if (sc->sc_state == SPC_CONNECTED)
   2165 			spc_sched_msgout(sc, SEND_ABORT);
   2166 	} else {
   2167 		spc_dequeue(sc, acb);
   2168 		TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   2169 		if (sc->sc_state == SPC_IDLE)
   2170 			spc_sched(sc);
   2171 	}
   2172 }
   2173 
   2174 void
   2175 spc_timeout(arg)
   2176 	void *arg;
   2177 {
   2178 	struct spc_acb *acb = arg;
   2179 	struct scsipi_xfer *xs = acb->xs;
   2180 	struct scsipi_link *sc_link = xs->sc_link;
   2181 	struct spc_softc *sc = sc_link->adapter_softc;
   2182 	int s;
   2183 
   2184 	scsi_print_addr(sc_link);
   2185 	printf("timed out");
   2186 
   2187 	s = splbio();
   2188 
   2189 	if (acb->flags & ACB_ABORT) {
   2190 		/* abort timed out */
   2191 		printf(" AGAIN\n");
   2192 		/* XXX Must reset! */
   2193 	} else {
   2194 		/* abort the operation that has timed out */
   2195 		printf("\n");
   2196 		acb->xs->error = XS_TIMEOUT;
   2197 		spc_abort(sc, acb);
   2198 	}
   2199 
   2200 	splx(s);
   2201 }
   2202 
   2203 #if SPC_DEBUG
   2205 /*
   2206  * The following functions are mostly used for debugging purposes, either
   2207  * directly called from the driver or from the kernel debugger.
   2208  */
   2209 
   2210 void
   2211 spc_show_scsi_cmd(acb)
   2212 	struct spc_acb *acb;
   2213 {
   2214 	u_char  *b = (u_char *)&acb->scsi_cmd;
   2215 	struct scsipi_link *sc_link = acb->xs->sc_link;
   2216 	int i;
   2217 
   2218 	scsi_print_addr(sc_link);
   2219 	if ((acb->xs->flags & SCSI_RESET) == 0) {
   2220 		for (i = 0; i < acb->scsi_cmd_length; i++) {
   2221 			if (i)
   2222 				printf(",");
   2223 			printf("%x", b[i]);
   2224 		}
   2225 		printf("\n");
   2226 	} else
   2227 		printf("RESET\n");
   2228 }
   2229 
   2230 void
   2231 spc_print_acb(acb)
   2232 	struct spc_acb *acb;
   2233 {
   2234 
   2235 	printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
   2236 	printf(" dp=%x dleft=%d target_stat=%x\n",
   2237 	    (long)acb->data_addr, acb->data_length, acb->target_stat);
   2238 	spc_show_scsi_cmd(acb);
   2239 }
   2240 
   2241 void
   2242 spc_print_active_acb()
   2243 {
   2244 	struct spc_acb *acb;
   2245 	struct spc_softc *sc = spc_cd.cd_devs[0]; /* XXX */
   2246 
   2247 	printf("ready list:\n");
   2248 	for (acb = sc->ready_list.tqh_first; acb != NULL;
   2249 	    acb = acb->chain.tqe_next)
   2250 		spc_print_acb(acb);
   2251 	printf("nexus:\n");
   2252 	if (sc->sc_nexus != NULL)
   2253 		spc_print_acb(sc->sc_nexus);
   2254 	printf("nexus list:\n");
   2255 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
   2256 	    acb = acb->chain.tqe_next)
   2257 		spc_print_acb(acb);
   2258 }
   2259 
   2260 void
   2261 spc_dump_driver(sc)
   2262 	struct spc_softc *sc;
   2263 {
   2264 	struct spc_tinfo *ti;
   2265 	int i;
   2266 
   2267 	printf("nexus=%x prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
   2268 	printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
   2269 	    sc->sc_state, sc->sc_imess[0],
   2270 	    sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
   2271 	for (i = 0; i < 7; i++) {
   2272 		ti = &sc->sc_tinfo[i];
   2273 		printf("tinfo%d: %d cmds %d disconnects %d timeouts",
   2274 		    i, ti->cmds, ti->dconns, ti->touts);
   2275 		printf(" %d senses flags=%x\n", ti->senses, ti->flags);
   2276 	}
   2277 }
   2278 #endif
   2279