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