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