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