xref: /netbsd-src/sys/dev/ic/sunscpal.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: sunscpal.c,v 1.25 2009/11/23 02:13:46 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Matthew Fredette
5  * Copyright (c) 1995 David Jones, Gordon W. Ross
6  * Copyright (c) 1994 Jarle Greipsland
7  * 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. The name of the authors may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  * 4. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by
22  *      David Jones and Gordon Ross
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * This is a machine-independent driver for the Sun "sc"
38  * SCSI Bus Controller (SBC).
39  *
40  * This code should work with any memory-mapped card,
41  * and can be shared by multiple adapters that address
42  * the card with different register offset spacings.
43  * (This can happen on the atari, for example.)
44  *
45  * Credits, history:
46  *
47  * Matthew Fredette completely copied revision 1.38 of
48  * ncr5380sbc.c, and then heavily modified it to match
49  * the Sun sc PAL.  The remaining credits are for
50  * ncr5380sbc.c:
51  *
52  * David Jones is the author of most of the code that now
53  * appears in this file, and was the architect of the
54  * current overall structure (MI/MD code separation, etc.)
55  *
56  * Gordon Ross integrated the message phase code, added lots of
57  * comments about what happens when and why (re. SCSI spec.),
58  * debugged some reentrance problems, and added several new
59  * "hooks" needed for the Sun3 "si" adapters.
60  *
61  * The message in/out code was taken nearly verbatim from
62  * the aic6360 driver by Jarle Greipsland.
63  *
64  * Several other NCR5380 drivers were used for reference
65  * while developing this driver, including work by:
66  *   The Alice Group (mac68k port) namely:
67  *       Allen K. Briggs, Chris P. Caputo, Michael L. Finch,
68  *       Bradley A. Grantham, and Lawrence A. Kesteloot
69  *   Michael L. Hitch (amiga drivers: sci.c)
70  *   Leo Weppelman (atari driver: ncr5380.c)
71  * There are others too.  Thanks, everyone.
72  *
73  * Transliteration to bus_space() performed 9/17/98 by
74  * John Ruschmeyer (jruschme@exit109.com) for i386 'nca' driver.
75  * Thank you all.
76  */
77 
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: sunscpal.c,v 1.25 2009/11/23 02:13:46 rmind Exp $");
80 
81 #include "opt_ddb.h"
82 
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/errno.h>
87 #include <sys/malloc.h>
88 #include <sys/device.h>
89 #include <sys/buf.h>
90 #include <sys/proc.h>
91 
92 #include <dev/scsipi/scsi_all.h>
93 #include <dev/scsipi/scsipi_all.h>
94 #include <dev/scsipi/scsipi_debug.h>
95 #include <dev/scsipi/scsi_message.h>
96 #include <dev/scsipi/scsiconf.h>
97 
98 #ifdef DDB
99 #include <ddb/db_output.h>
100 #endif
101 
102 #include <dev/ic/sunscpalreg.h>
103 #include <dev/ic/sunscpalvar.h>
104 
105 static void	sunscpal_reset_scsibus(struct sunscpal_softc *);
106 static void	sunscpal_sched(struct sunscpal_softc *);
107 static void	sunscpal_done(struct sunscpal_softc *);
108 
109 static int	sunscpal_select(struct sunscpal_softc *, struct sunscpal_req *);
110 static void	sunscpal_reselect(struct sunscpal_softc *);
111 
112 static int	sunscpal_msg_in(struct sunscpal_softc *);
113 static int	sunscpal_msg_out(struct sunscpal_softc *);
114 static int	sunscpal_data_xfer(struct sunscpal_softc *, int);
115 static int	sunscpal_command(struct sunscpal_softc *);
116 static int	sunscpal_status(struct sunscpal_softc *);
117 static void	sunscpal_machine(struct sunscpal_softc *);
118 
119 void	sunscpal_abort(struct sunscpal_softc *);
120 void	sunscpal_cmd_timeout(void *);
121 /*
122  * Action flags returned by the info_transfer functions:
123  * (These determine what happens next.)
124  */
125 #define ACT_CONTINUE	0x00	/* No flags: expect another phase */
126 #define ACT_DISCONNECT	0x01	/* Target is disconnecting */
127 #define ACT_CMD_DONE	0x02	/* Need to call scsipi_done() */
128 #define ACT_RESET_BUS	0x04	/* Need bus reset (cmd timeout) */
129 #define ACT_WAIT_DMA	0x10	/* Wait for DMA to complete */
130 
131 /*****************************************************************
132  * Debugging stuff
133  *****************************************************************/
134 
135 #ifndef DDB
136 /* This is used only in recoverable places. */
137 #ifndef Debugger
138 #define Debugger() printf("Debug: sunscpal.c:%d\n", __LINE__)
139 #endif
140 #endif
141 
142 #ifdef	SUNSCPAL_DEBUG
143 
144 #define	SUNSCPAL_DBG_BREAK	1
145 #define	SUNSCPAL_DBG_CMDS	2
146 #define	SUNSCPAL_DBG_DMA	4
147 int sunscpal_debug = 0;
148 #define	SUNSCPAL_BREAK() \
149 	do { if (sunscpal_debug & SUNSCPAL_DBG_BREAK) Debugger(); } while (0)
150 static void sunscpal_show_scsi_cmd(struct scsipi_xfer *);
151 #ifdef DDB
152 void	sunscpal_clear_trace(void);
153 void	sunscpal_show_trace(void);
154 void	sunscpal_show_req(struct sunscpal_req *);
155 void	sunscpal_show_state(void);
156 #endif	/* DDB */
157 #else	/* SUNSCPAL_DEBUG */
158 
159 #define	SUNSCPAL_BREAK() 		/* nada */
160 #define sunscpal_show_scsi_cmd(xs) /* nada */
161 
162 #endif	/* SUNSCPAL_DEBUG */
163 
164 static const char *
165 phase_names[8] = {
166 	"DATA_OUT",
167 	"DATA_IN",
168 	"COMMAND",
169 	"STATUS",
170 	"UNSPEC1",
171 	"UNSPEC2",
172 	"MSG_OUT",
173 	"MSG_IN",
174 };
175 
176 #ifdef SUNSCPAL_USE_BUS_DMA
177 static void sunscpal_dma_alloc(struct sunscpal_softc *);
178 static void sunscpal_dma_free(struct sunscpal_softc *);
179 static void sunscpal_dma_setup(struct sunscpal_softc *);
180 #else
181 #define sunscpal_dma_alloc(sc) (*sc->sc_dma_alloc)(sc)
182 #define sunscpal_dma_free(sc) (*sc->sc_dma_free)(sc)
183 #define sunscpal_dma_setup(sc) (*sc->sc_dma_setup)(sc)
184 #endif
185 static void sunscpal_minphys(struct buf *);
186 
187 /*****************************************************************
188  * Actual chip control
189  *****************************************************************/
190 
191 /*
192  * XXX: These timeouts might need to be tuned...
193  */
194 
195 /* This one is used when waiting for a phase change. (X100uS.) */
196 int sunscpal_wait_phase_timo = 1000 * 10 * 300;	/* 5 min. */
197 
198 /* These are used in the following inline functions. */
199 int sunscpal_wait_req_timo = 1000 * 50;	/* X2 = 100 mS. */
200 int sunscpal_wait_nrq_timo = 1000 * 25;	/* X2 =  50 mS. */
201 
202 static inline int sunscpal_wait_req(struct sunscpal_softc *);
203 static inline int sunscpal_wait_not_req(struct sunscpal_softc *);
204 static inline void sunscpal_sched_msgout(struct sunscpal_softc *, int);
205 
206 /* Return zero on success. */
207 static inline int sunscpal_wait_req(struct sunscpal_softc *sc)
208 {
209 	int timo = sunscpal_wait_req_timo;
210 
211 	for (;;) {
212 		if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_REQUEST) {
213 			timo = 0;	/* return 0 */
214 			break;
215 		}
216 		if (--timo < 0)
217 			break;	/* return -1 */
218 		delay(2);
219 	}
220 	return timo;
221 }
222 
223 /* Return zero on success. */
224 static inline int sunscpal_wait_not_req(struct sunscpal_softc *sc)
225 {
226 	int timo = sunscpal_wait_nrq_timo;
227 
228 	for (;;) {
229 		if ((SUNSCPAL_READ_2(sc, sunscpal_icr) &
230 		    SUNSCPAL_ICR_REQUEST) == 0) {
231 			timo = 0;	/* return 0 */
232 			break;
233 		}
234 		if (--timo < 0)
235 			break;	/* return -1 */
236 		delay(2);
237 	}
238 	return timo;
239 }
240 
241 /*
242  * These functions control DMA functions in the chipset independent of
243  * the host DMA implementation.
244  */
245 static void sunscpal_dma_start(struct sunscpal_softc *);
246 static void sunscpal_dma_poll(struct sunscpal_softc *);
247 static void sunscpal_dma_stop(struct sunscpal_softc *);
248 
249 static void
250 sunscpal_dma_start(struct sunscpal_softc *sc)
251 {
252 	struct sunscpal_req *sr = sc->sc_current;
253 	int xlen;
254 	uint16_t icr;
255 
256 	xlen = sc->sc_reqlen;
257 
258 	/* Let'er rip! */
259 	icr = SUNSCPAL_READ_2(sc, sunscpal_icr);
260 	icr |= SUNSCPAL_ICR_DMA_ENABLE |
261 	    ((xlen & 1) ? 0 : SUNSCPAL_ICR_WORD_MODE) |
262 	    ((sr->sr_flags & SR_IMMED) ? 0 : SUNSCPAL_ICR_INTERRUPT_ENABLE);
263 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, icr);
264 
265 	sc->sc_state |= SUNSCPAL_DOINGDMA;
266 
267 #ifdef	SUNSCPAL_DEBUG
268 	if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
269 		printf("%s: started, flags=0x%x\n",
270 		    __func__, sc->sc_state);
271 	}
272 #endif
273 }
274 
275 #define	ICR_MASK (SUNSCPAL_ICR_PARITY_ERROR | SUNSCPAL_ICR_BUS_ERROR | SUNSCPAL_ICR_INTERRUPT_REQUEST)
276 #define	POLL_TIMO	50000	/* X100 = 5 sec. */
277 
278 /*
279  * Poll (spin-wait) for DMA completion.
280  * Called right after xx_dma_start(), and
281  * xx_dma_stop() will be called next.
282  */
283 static void
284 sunscpal_dma_poll(struct sunscpal_softc *sc)
285 {
286 	struct sunscpal_req *sr = sc->sc_current;
287 	int tmo;
288 
289 	/* Make sure DMA started successfully. */
290 	if (sc->sc_state & SUNSCPAL_ABORTING)
291 		return;
292 
293 	/* Wait for any "DMA complete" or error bits. */
294 	tmo = POLL_TIMO;
295 	for (;;) {
296 		if (SUNSCPAL_READ_2(sc, sunscpal_icr) & ICR_MASK)
297 			break;
298 		if (--tmo <= 0) {
299 			printf("sc: DMA timeout (while polling)\n");
300 			/* Indicate timeout as MI code would. */
301 			sr->sr_flags |= SR_OVERDUE;
302 			break;
303 		}
304 		delay(100);
305 	}
306 	SUNSCPAL_TRACE("sunscpal_dma_poll: waited %d\n", POLL_TIMO - tmo);
307 
308 #ifdef	SUNSCPAL_DEBUG
309 	if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
310 		char buffer[64];
311 		snprintb(buffer, sizeof(buffer),
312 		    SUNSCPAL_READ_2(sc, sunscpal_icr), SUNSCPAL_ICR_BITS);
313 		printf("%s: done, icr=%s\n", __func__, buffer);
314 	}
315 #endif
316 }
317 
318 static void
319 sunscpal_dma_stop(struct sunscpal_softc *sc)
320 {
321 	struct sunscpal_req *sr = sc->sc_current;
322 	struct scsipi_xfer *xs = sr->sr_xs;
323 	int resid, ntrans;
324 	uint16_t icr;
325 
326 	if ((sc->sc_state & SUNSCPAL_DOINGDMA) == 0) {
327 #ifdef	DEBUG
328 		printf("%s: DMA not running\n", __func__);
329 #endif
330 		return;
331 	}
332 	sc->sc_state &= ~SUNSCPAL_DOINGDMA;
333 
334 	/* First, halt the DMA engine. */
335 	icr = SUNSCPAL_READ_2(sc, sunscpal_icr);
336 	icr &= ~(SUNSCPAL_ICR_DMA_ENABLE | SUNSCPAL_ICR_WORD_MODE |
337 	    SUNSCPAL_ICR_INTERRUPT_ENABLE);
338 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, icr);
339 
340 #ifdef	SUNSCPAL_USE_BUS_DMA
341 	/*
342 	 * XXX - this function is supposed to be independent of
343 	 * the host's DMA implementation.
344 	 */
345  {
346 	 sunscpal_dma_handle_t dh = sr->sr_dma_hand;
347 
348 	 /* sync the DMA map: */
349 	 bus_dmamap_sync(sc->sunscpal_dmat, dh->dh_dmamap, 0, dh->dh_maplen,
350 	     ((xs->xs_control & XS_CTL_DATA_OUT) == 0 ?
351 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
352  }
353 #endif /* SUNSCPAL_USE_BUS_DMA */
354 
355 
356 	if (icr & (SUNSCPAL_ICR_BUS_ERROR)) {
357 		char buffer[64];
358 		snprintb(buffer, sizeof(buffer), SUNSCPAL_ICR_BITS, icr);
359 		printf("sc: DMA error, icr=%s, reset\n", buffer);
360 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
361 		sc->sc_state |= SUNSCPAL_ABORTING;
362 		goto out;
363 	}
364 
365 	/* Note that timeout may have set the error flag. */
366 	if (sc->sc_state & SUNSCPAL_ABORTING)
367 		goto out;
368 
369 	/* XXX: Wait for DMA to actually finish? */
370 
371 	/*
372 	 * Now try to figure out how much actually transferred
373 	 */
374 
375 	resid = SUNSCPAL_DMA_COUNT_FLIP(SUNSCPAL_READ_2(sc,
376 	    sunscpal_dma_count));
377 	ntrans = sc->sc_reqlen - resid;
378 
379 #ifdef	SUNSCPAL_DEBUG
380 	if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
381 		printf("%s: resid=0x%x ntrans=0x%x\n",
382 		    __func__, resid, ntrans);
383 	}
384 #endif
385 
386 	if (ntrans < sc->sc_min_dma_len) {
387 		printf("sc: DMA count: 0x%x\n", resid);
388 		sc->sc_state |= SUNSCPAL_ABORTING;
389 		goto out;
390 	}
391 	if (ntrans > sc->sc_datalen)
392 		panic("%s: excess transfer", __func__);
393 
394 	/* Adjust data pointer */
395 	sc->sc_dataptr += ntrans;
396 	sc->sc_datalen -= ntrans;
397 
398 	/*
399 	 * After a read, we may need to clean-up
400 	 * "Left-over bytes" (yuck!)
401 	 */
402 	if (((xs->xs_control & XS_CTL_DATA_OUT) == 0) &&
403 	    ((icr & SUNSCPAL_ICR_ODD_LENGTH) != 0)) {
404 #ifdef DEBUG
405 		printf("sc: Got Left-over bytes!\n");
406 #endif
407 		*(sc->sc_dataptr++) = SUNSCPAL_READ_1(sc, sunscpal_data);
408 		sc->sc_datalen--;
409 	}
410 
411  out:
412 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(0));
413 
414 }
415 
416 /* Ask the target for a MSG_OUT phase. */
417 static inline void
418 sunscpal_sched_msgout(struct sunscpal_softc *sc, int msg_code)
419 {
420 	/*
421 	 * This controller does not allow you to assert ATN, which
422 	 * will eventually leave us with no option other than to reset
423 	 * the bus.  We keep this function as a placeholder, though,
424 	 * and this printf will eventually go away or get #ifdef'ed:
425 	 */
426 	printf("%s: trying to schedule 0x%0x\n", __func__, msg_code);
427 	sc->sc_msgpriq |= msg_code;
428 }
429 
430 int
431 sunscpal_pio_out(struct sunscpal_softc *sc, int phase, int count, uint8_t *data)
432 {
433 	int resid;
434 
435 	resid = count;
436 	while (resid > 0) {
437 		if (!SUNSCPAL_BUSY(sc)) {
438 			SUNSCPAL_TRACE("pio_out: lost BSY, resid=%d\n", resid);
439 			break;
440 		}
441 		if (sunscpal_wait_req(sc)) {
442 			SUNSCPAL_TRACE("pio_out: no REQ, resid=%d\n", resid);
443 			break;
444 		}
445 		if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) !=
446 		    phase)
447 			break;
448 
449 		/* Put the data on the bus. */
450 		if (data) {
451 			SUNSCPAL_BYTE_WRITE(sc, phase, *data++);
452 		} else {
453 			SUNSCPAL_BYTE_WRITE(sc, phase, 0);
454 		}
455 
456 		--resid;
457 	}
458 
459 	return count - resid;
460 }
461 
462 
463 int
464 sunscpal_pio_in(struct sunscpal_softc *sc, int phase, int count, uint8_t *data)
465 {
466 	int resid;
467 
468 	resid = count;
469 	while (resid > 0) {
470 		if (!SUNSCPAL_BUSY(sc)) {
471 			SUNSCPAL_TRACE("pio_in: lost BSY, resid=%d\n", resid);
472 			break;
473 		}
474 		if (sunscpal_wait_req(sc)) {
475 			SUNSCPAL_TRACE("pio_in: no REQ, resid=%d\n", resid);
476 			break;
477 		}
478 		/* A phase change is not valid until AFTER REQ rises! */
479 		if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) !=
480 		    phase)
481 			break;
482 
483 		/* Read the data bus. */
484 		if (data)
485 			*data++ = SUNSCPAL_BYTE_READ(sc, phase);
486 		else
487 			(void)SUNSCPAL_BYTE_READ(sc, phase);
488 
489 		--resid;
490 	}
491 
492 	return count - resid;
493 }
494 
495 
496 void
497 sunscpal_init(struct sunscpal_softc *sc)
498 {
499 	int i, j;
500 
501 #ifdef	SUNSCPAL_DEBUG
502 	sunscpal_debug_sc = sc;
503 #endif
504 
505 	for (i = 0; i < SUNSCPAL_OPENINGS; i++)
506 		sc->sc_ring[i].sr_xs = NULL;
507 	for (i = 0; i < 8; i++)
508 		for (j = 0; j < 8; j++)
509 			sc->sc_matrix[i][j] = NULL;
510 
511 	sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
512 	sc->sc_state = SUNSCPAL_IDLE;
513 
514 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
515 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_h, 0);
516 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_l, 0);
517 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(0));
518 
519 	SUNSCPAL_CLR_INTR(sc);
520 
521 	/* Another hack (Er.. hook!) for anything that needs it: */
522 	if (sc->sc_intr_on) {
523 		SUNSCPAL_TRACE("init: intr ON\n", 0);
524 		sc->sc_intr_on(sc);
525 	}
526 }
527 
528 
529 static void
530 sunscpal_reset_scsibus(struct sunscpal_softc *sc)
531 {
532 
533 	SUNSCPAL_TRACE("reset_scsibus, cur=0x%x\n", (long)sc->sc_current);
534 
535 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, SUNSCPAL_ICR_RESET);
536 	delay(500);
537 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
538 
539 	SUNSCPAL_CLR_INTR(sc);
540 	/* XXX - Need long delay here! */
541 	delay(100000);
542 
543 	/* XXX - Need to cancel disconnected requests. */
544 }
545 
546 
547 /*
548  * Interrupt handler for the SCSI Bus Controller (SBC)
549  * This may also called for a DMA timeout (at splbio).
550  */
551 int
552 sunscpal_intr(void *arg)
553 {
554 	struct sunscpal_softc *sc = arg;
555 	int claimed = 0;
556 
557 	/*
558 	 * Do not touch SBC regs here unless sc_current == NULL
559 	 * or it will complain about "register conflict" errors.
560 	 * Instead, just let sunscpal_machine() deal with it.
561 	 */
562 	SUNSCPAL_TRACE("intr: top, state=%d\n", sc->sc_state);
563 
564 	if (sc->sc_state == SUNSCPAL_IDLE) {
565 		/*
566 		 * Might be reselect.  sunscpal_reselect() will check,
567 		 * and set up the connection if so.  This will verify
568 		 * that sc_current == NULL at the beginning...
569 		 */
570 
571 		/* Another hack (Er.. hook!) for anything that needs it: */
572 		if (sc->sc_intr_off) {
573 			SUNSCPAL_TRACE("intr: for reselect, intr off\n", 0);
574 		    sc->sc_intr_off(sc);
575 		}
576 
577 		sunscpal_reselect(sc);
578 	}
579 
580 	/*
581 	 * The remaining documented interrupt causes are a DMA complete
582 	 * condition.
583 	 *
584 	 * The procedure is to let sunscpal_machine() figure out what
585 	 * to do next.
586 	 */
587 	if (sc->sc_state & SUNSCPAL_WORKING) {
588 		SUNSCPAL_TRACE("intr: call machine, cur=0x%x\n",
589 		    (long)sc->sc_current);
590 		/* This will usually free-up the nexus. */
591 		sunscpal_machine(sc);
592 		SUNSCPAL_TRACE("intr: machine done, cur=0x%x\n",
593 		    (long)sc->sc_current);
594 		claimed = 1;
595 	}
596 
597 	/* Maybe we can run some commands now... */
598 	if (sc->sc_state == SUNSCPAL_IDLE) {
599 		SUNSCPAL_TRACE("intr: call sched, cur=0x%x\n",
600 		    (long)sc->sc_current);
601 		sunscpal_sched(sc);
602 		SUNSCPAL_TRACE("intr: sched done, cur=0x%x\n",
603 		    (long)sc->sc_current);
604 	}
605 
606 	return claimed;
607 }
608 
609 
610 /*
611  * Abort the current command (i.e. due to timeout)
612  */
613 void
614 sunscpal_abort(struct sunscpal_softc *sc)
615 {
616 
617 	/*
618 	 * Finish it now.  If DMA is in progress, we
619 	 * can not call sunscpal_sched_msgout() because
620 	 * that hits the SBC (avoid DMA conflict).
621 	 */
622 
623 	/* Another hack (Er.. hook!) for anything that needs it: */
624 	if (sc->sc_intr_off) {
625 		SUNSCPAL_TRACE("abort: intr off\n", 0);
626 		sc->sc_intr_off(sc);
627 	}
628 
629 	sc->sc_state |= SUNSCPAL_ABORTING;
630 	if ((sc->sc_state & SUNSCPAL_DOINGDMA) == 0) {
631 		sunscpal_sched_msgout(sc, SEND_ABORT);
632 	}
633 	SUNSCPAL_TRACE("abort: call machine, cur=0x%x\n",
634 	    (long)sc->sc_current);
635 	sunscpal_machine(sc);
636 	SUNSCPAL_TRACE("abort: machine done, cur=0x%x\n",
637 	    (long)sc->sc_current);
638 
639 	/* Another hack (Er.. hook!) for anything that needs it: */
640 	if (sc->sc_intr_on) {
641 		SUNSCPAL_TRACE("abort: intr ON\n", 0);
642 		sc->sc_intr_on(sc);
643 	}
644 }
645 
646 /*
647  * Timeout handler, scheduled for each SCSI command.
648  */
649 void
650 sunscpal_cmd_timeout(void *arg)
651 {
652 	struct sunscpal_req *sr = arg;
653 	struct scsipi_xfer *xs;
654 	struct scsipi_periph *periph;
655 	struct sunscpal_softc *sc;
656 	int s;
657 
658 	s = splbio();
659 
660 	/* Get all our variables... */
661 	xs = sr->sr_xs;
662 	if (xs == NULL) {
663 		printf("%s: no scsipi_xfer\n", __func__);
664 		goto out;
665 	}
666 	periph = xs->xs_periph;
667 	sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
668 
669 	printf("%s: cmd timeout, targ=%d, lun=%d\n",
670 	    device_xname(sc->sc_dev),
671 	    sr->sr_target, sr->sr_lun);
672 
673 	/*
674 	 * Mark the overdue job as failed, and arrange for
675 	 * sunscpal_machine to terminate it.  If the victim
676 	 * is the current job, call sunscpal_machine() now.
677 	 * Otherwise arrange for sunscpal_sched() to do it.
678 	 */
679 	sr->sr_flags |= SR_OVERDUE;
680 	if (sc->sc_current == sr) {
681 		SUNSCPAL_TRACE("cmd_tmo: call abort, sr=0x%x\n", (long)sr);
682 		sunscpal_abort(sc);
683 	} else {
684 		/*
685 		 * The driver may be idle, or busy with another job.
686 		 * Arrange for sunscpal_sched() to do the deed.
687 		 */
688 		SUNSCPAL_TRACE("cmd_tmo: clear matrix, t/l=0x%02x\n",
689 		    (sr->sr_target << 4) | sr->sr_lun);
690 		sc->sc_matrix[sr->sr_target][sr->sr_lun] = NULL;
691 	}
692 
693 	/*
694 	 * We may have aborted the current job, or may have
695 	 * already been idle. In either case, we should now
696 	 * be idle, so try to start another job.
697 	 */
698 	if (sc->sc_state == SUNSCPAL_IDLE) {
699 		SUNSCPAL_TRACE("cmd_tmo: call sched, cur=0x%x\n",
700 		    (long)sc->sc_current);
701 		sunscpal_sched(sc);
702 		SUNSCPAL_TRACE("cmd_tmo: sched done, cur=0x%x\n",
703 		    (long)sc->sc_current);
704 	}
705 
706  out:
707 	splx(s);
708 }
709 
710 
711 /*****************************************************************
712  * Interface to higher level
713  *****************************************************************/
714 
715 
716 /*
717  * Enter a new SCSI command into the "issue" queue, and
718  * if there is work to do, start it going.
719  *
720  * WARNING:  This can be called recursively!
721  * (see comment in sunscpal_done)
722  */
723 void
724 sunscpal_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
725     void *arg)
726 {
727 	struct scsipi_xfer *xs;
728 	struct scsipi_periph *periph;
729 	struct sunscpal_softc *sc;
730 	struct sunscpal_req *sr;
731 	int s, i, flags;
732 
733 	sc = device_private(chan->chan_adapter->adapt_dev);
734 
735 	switch (req) {
736 	case ADAPTER_REQ_RUN_XFER:
737 		xs = arg;
738 		periph = xs->xs_periph;
739 		flags = xs->xs_control;
740 
741 		if (flags & XS_CTL_DATA_UIO)
742 			panic("sunscpal: scsi data uio requested");
743 
744 		s = splbio();
745 
746 		if (flags & XS_CTL_POLL) {
747 			/* Terminate any current command. */
748 			sr = sc->sc_current;
749 			if (sr != NULL) {
750 				printf("%s: polled request aborting %d/%d\n",
751 				    device_xname(sc->sc_dev), sr->sr_target,
752 				    sr->sr_lun);
753 				sunscpal_abort(sc);
754 			}
755 			if (sc->sc_state != SUNSCPAL_IDLE) {
756 				panic("%s: polled request, abort failed",
757 				    __func__);
758 			}
759 		}
760 
761 		/*
762 		 * Find lowest empty slot in ring buffer.
763 		 * XXX: What about "fairness" and cmd order?
764 		 */
765 		for (i = 0; i < SUNSCPAL_OPENINGS; i++)
766 			if (sc->sc_ring[i].sr_xs == NULL)
767 				goto new;
768 
769 		xs->error = XS_RESOURCE_SHORTAGE;
770 		SUNSCPAL_TRACE("scsipi_cmd: no openings, rv=%d\n", rv);
771 		goto out;
772 
773  new:
774 		/* Create queue entry */
775 		sr = &sc->sc_ring[i];
776 		sr->sr_xs = xs;
777 		sr->sr_target = xs->xs_periph->periph_target;
778 		sr->sr_lun = xs->xs_periph->periph_lun;
779 		sr->sr_dma_hand = NULL;
780 		sr->sr_dataptr = xs->data;
781 		sr->sr_datalen = xs->datalen;
782 		sr->sr_flags = (flags & XS_CTL_POLL) ? SR_IMMED : 0;
783 		sr->sr_status = -1;	/* no value */
784 		sc->sc_ncmds++;
785 
786 		SUNSCPAL_TRACE("scsipi_cmd: new sr=0x%x\n", (long)sr);
787 
788 		if (flags & XS_CTL_POLL) {
789 			/* Force this new command to be next. */
790 			sc->sc_rr = i;
791 		}
792 
793 		/*
794 		 * If we were idle, run some commands...
795 		 */
796 		if (sc->sc_state == SUNSCPAL_IDLE) {
797 			SUNSCPAL_TRACE("scsipi_cmd: call sched, cur=0x%x\n",
798 			    (long)sc->sc_current);
799 			sunscpal_sched(sc);
800 			SUNSCPAL_TRACE("scsipi_cmd: sched done, cur=0x%x\n",
801 			    (long)sc->sc_current);
802 		}
803 
804 		if (flags & XS_CTL_POLL) {
805 			/* Make sure sunscpal_sched() finished it. */
806 			if ((xs->xs_status & XS_STS_DONE) == 0)
807 				panic("%s: poll didn't finish", __func__);
808 		}
809 
810  out:
811 		splx(s);
812 		return;
813 
814 	case ADAPTER_REQ_GROW_RESOURCES:
815 		/* XXX Not supported. */
816 		return;
817 
818 	case ADAPTER_REQ_SET_XFER_MODE:
819 	    {
820 		/*
821 		 * We don't support Sync, Wide, or Tagged Queueing.
822 		 * Just callback now, to report this.
823 		 */
824 		struct scsipi_xfer_mode *xm = arg;
825 
826 		xm->xm_mode = 0;
827 		xm->xm_period = 0;
828 		xm->xm_offset = 0;
829 		scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
830 		return;
831 	    }
832 	}
833 }
834 
835 
836 /*
837  * POST PROCESSING OF SCSI_CMD (usually current)
838  * Called by sunscpal_sched(), sunscpal_machine()
839  */
840 static void
841 sunscpal_done(struct sunscpal_softc *sc)
842 {
843 	struct	sunscpal_req *sr;
844 	struct	scsipi_xfer *xs;
845 
846 #ifdef	DIAGNOSTIC
847 	if (sc->sc_state == SUNSCPAL_IDLE)
848 		panic("%s: state=idle", __func__);
849 	if (sc->sc_current == NULL)
850 		panic("%s: current=0", __func__);
851 #endif
852 
853 	sr = sc->sc_current;
854 	xs = sr->sr_xs;
855 
856 	SUNSCPAL_TRACE("done: top, cur=0x%x\n", (long)sc->sc_current);
857 
858 	/*
859 	 * Clean up DMA resources for this command.
860 	 */
861 	if (sr->sr_dma_hand) {
862 		SUNSCPAL_TRACE("done: dma_free, dh=0x%x\n",
863 		    (long)sr->sr_dma_hand);
864 		sunscpal_dma_free(sc);
865 	}
866 #ifdef	DIAGNOSTIC
867 	if (sr->sr_dma_hand)
868 		panic("%s: DMA free did not", __func__);
869 #endif
870 
871 	if (sc->sc_state & SUNSCPAL_ABORTING) {
872 		SUNSCPAL_TRACE("done: aborting, error=%d\n", xs->error);
873 		if (xs->error == XS_NOERROR)
874 			xs->error = XS_TIMEOUT;
875 	}
876 
877 	SUNSCPAL_TRACE("done: check error=%d\n", (long)xs->error);
878 
879 	/* If error is already set, ignore sr_status value. */
880 	if (xs->error != XS_NOERROR)
881 		goto finish;
882 
883 	SUNSCPAL_TRACE("done: check status=%d\n", sr->sr_status);
884 
885 	xs->status = sr->sr_status;
886 	switch (sr->sr_status) {
887 	case SCSI_OK:	/* 0 */
888 		break;
889 
890 	case SCSI_CHECK:
891 	case SCSI_BUSY:
892 		xs->error = XS_BUSY;
893 		break;
894 
895 	case -1:
896 		/* This is our "impossible" initial value. */
897 		/* fallthrough */
898 	default:
899 		printf("%s: target %d, bad status=%d\n",
900 		    device_xname(sc->sc_dev), sr->sr_target, sr->sr_status);
901 		xs->error = XS_DRIVER_STUFFUP;
902 		break;
903 	}
904 
905  finish:
906 
907 	SUNSCPAL_TRACE("done: finish, error=%d\n", xs->error);
908 
909 	/*
910 	 * Dequeue the finished command, but don't clear sc_state until
911 	 * after the call to scsipi_done(), because that may call back to
912 	 * sunscpal_scsi_cmd() - unwanted recursion!
913 	 *
914 	 * Keeping sc->sc_state != idle terminates the recursion.
915 	 */
916 #ifdef	DIAGNOSTIC
917 	if ((sc->sc_state & SUNSCPAL_WORKING) == 0)
918 		panic("%s: bad state", __func__);
919 #endif
920 
921 	/* Clear our pointers to the request. */
922 	sc->sc_current = NULL;
923 	sc->sc_matrix[sr->sr_target][sr->sr_lun] = NULL;
924 	callout_stop(&sr->sr_xs->xs_callout);
925 
926 	/* Make the request free. */
927 	sr->sr_xs = NULL;
928 	sc->sc_ncmds--;
929 
930 	/* Tell common SCSI code it is done. */
931 	scsipi_done(xs);
932 
933 	sc->sc_state = SUNSCPAL_IDLE;
934 	/* Now sunscpal_sched() may be called again. */
935 }
936 
937 
938 /*
939  * Schedule a SCSI operation.  This routine should return
940  * only after it achieves one of the following conditions:
941  *  	Busy (sc->sc_state != SUNSCPAL_IDLE)
942  *  	No more work can be started.
943  */
944 static void
945 sunscpal_sched(struct sunscpal_softc *sc)
946 {
947 	struct sunscpal_req	*sr;
948 	struct scsipi_xfer *xs;
949 	int	target = 0, lun = 0;
950 	int	error, i;
951 
952 	/* Another hack (Er.. hook!) for anything that needs it: */
953 	if (sc->sc_intr_off) {
954 		SUNSCPAL_TRACE("sched: top, intr off\n", 0);
955 		sc->sc_intr_off(sc);
956 	}
957 
958  next_job:
959 	/*
960 	 * Grab the next job from queue.  Must be idle.
961 	 */
962 #ifdef	DIAGNOSTIC
963 	if (sc->sc_state != SUNSCPAL_IDLE)
964 		panic("%s: not idle", __func__);
965 	if (sc->sc_current)
966 		panic("%s: current set", __func__);
967 #endif
968 
969 	/*
970 	 * Always start the search where we last looked.
971 	 */
972 	i = sc->sc_rr;
973 	sr = NULL;
974 	do {
975 		if (sc->sc_ring[i].sr_xs) {
976 			target = sc->sc_ring[i].sr_target;
977 			lun = sc->sc_ring[i].sr_lun;
978 			if (sc->sc_matrix[target][lun] == NULL) {
979 				/*
980 				 * Do not mark the  target/LUN busy yet,
981 				 * because reselect may cause some other
982 				 * job to become the current one, so we
983 				 * might not actually start this job.
984 				 * Instead, set sc_matrix later on.
985 				 */
986 				sc->sc_rr = i;
987 				sr = &sc->sc_ring[i];
988 				break;
989 			}
990 		}
991 		i++;
992 		if (i == SUNSCPAL_OPENINGS)
993 			i = 0;
994 	} while (i != sc->sc_rr);
995 
996 	if (sr == NULL) {
997 		SUNSCPAL_TRACE("sched: no work, cur=0x%x\n",
998 		    (long)sc->sc_current);
999 
1000 		/* Another hack (Er.. hook!) for anything that needs it: */
1001 		if (sc->sc_intr_on) {
1002 			SUNSCPAL_TRACE("sched: ret, intr ON\n", 0);
1003 			sc->sc_intr_on(sc);
1004 		}
1005 
1006 		return;		/* No more work to do. */
1007 	}
1008 
1009 	SUNSCPAL_TRACE("sched: select for t/l=0x%02x\n",
1010 	    (sr->sr_target << 4) | sr->sr_lun);
1011 
1012 	sc->sc_state = SUNSCPAL_WORKING;
1013 	error = sunscpal_select(sc, sr);
1014 	if (sc->sc_current) {
1015 		/* Lost the race!  reselected out from under us! */
1016 		/* Work with the reselected job. */
1017 		if (sr->sr_flags & SR_IMMED) {
1018 			printf("%s: reselected while polling (abort)\n",
1019 			    device_xname(sc->sc_dev));
1020 			/* Abort the reselected job. */
1021 			sc->sc_state |= SUNSCPAL_ABORTING;
1022 			sc->sc_msgpriq |= SEND_ABORT;
1023 		}
1024 		sr = sc->sc_current;
1025 		xs = sr->sr_xs;
1026 		SUNSCPAL_TRACE("sched: reselect, new sr=0x%x\n", (long)sr);
1027 		goto have_nexus;
1028 	}
1029 
1030 	/* Normal selection result.  Target/LUN is now busy. */
1031 	sc->sc_matrix[target][lun] = sr;
1032 	sc->sc_current = sr;	/* connected */
1033 	xs = sr->sr_xs;
1034 
1035 	/*
1036 	 * Initialize pointers, etc. for this job
1037 	 */
1038 	sc->sc_dataptr  = sr->sr_dataptr;
1039 	sc->sc_datalen  = sr->sr_datalen;
1040 	sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
1041 	sc->sc_msgpriq = SEND_IDENTIFY;
1042 	sc->sc_msgoutq = 0;
1043 	sc->sc_msgout  = 0;
1044 
1045 	SUNSCPAL_TRACE("sched: select rv=%d\n", error);
1046 
1047 	switch (error) {
1048 	case XS_NOERROR:
1049 		break;
1050 
1051 	case XS_BUSY:
1052 		/* XXX - Reset and try again. */
1053 		printf("%s: select found SCSI bus busy, resetting...\n",
1054 		    device_xname(sc->sc_dev));
1055 		sunscpal_reset_scsibus(sc);
1056 		/* fallthrough */
1057 	case XS_SELTIMEOUT:
1058 	default:
1059 		xs->error = error;	/* from select */
1060 		SUNSCPAL_TRACE("sched: call done, sr=0x%x\n", (long)sr);
1061 		sunscpal_done(sc);
1062 
1063 		/* Paranoia: clear everything. */
1064 		sc->sc_dataptr = NULL;
1065 		sc->sc_datalen = 0;
1066 		sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
1067 		sc->sc_msgpriq = 0;
1068 		sc->sc_msgoutq = 0;
1069 		sc->sc_msgout  = 0;
1070 
1071 		goto next_job;
1072 	}
1073 
1074 	/*
1075 	 * Selection was successful.  Normally, this means
1076 	 * we are starting a new command.  However, this
1077 	 * might be the termination of an overdue job.
1078 	 */
1079 	if (sr->sr_flags & SR_OVERDUE) {
1080 		SUNSCPAL_TRACE("sched: overdue, sr=0x%x\n", (long)sr);
1081 		sc->sc_state |= SUNSCPAL_ABORTING;
1082 		sc->sc_msgpriq |= SEND_ABORT;
1083 		goto have_nexus;
1084 	}
1085 
1086 	/*
1087 	 * OK, we are starting a new command.
1088 	 * Initialize and allocate resources for the new command.
1089 	 * Device reset is special (only uses MSG_OUT phase).
1090 	 * Normal commands start in MSG_OUT phase where we will
1091 	 * send and IDENDIFY message, and then expect CMD phase.
1092 	 */
1093 #ifdef	SUNSCPAL_DEBUG
1094 	if (sunscpal_debug & SUNSCPAL_DBG_CMDS) {
1095 		printf("%s: begin, target=%d, LUN=%d\n", __func__,
1096 		    xs->xs_periph->periph_target, xs->xs_periph->periph_lun);
1097 		sunscpal_show_scsi_cmd(xs);
1098 	}
1099 #endif
1100 	if (xs->xs_control & XS_CTL_RESET) {
1101 		SUNSCPAL_TRACE("sched: cmd=reset, sr=0x%x\n", (long)sr);
1102 		/* Not an error, so do not set SUNSCPAL_ABORTING */
1103 		sc->sc_msgpriq |= SEND_DEV_RESET;
1104 		goto have_nexus;
1105 	}
1106 
1107 #ifdef	DIAGNOSTIC
1108 	if ((xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) == 0) {
1109 		if (sc->sc_dataptr) {
1110 			printf("%s: ptr but no data in/out flags?\n",
1111 			    device_xname(sc->sc_dev));
1112 			SUNSCPAL_BREAK();
1113 			sc->sc_dataptr = NULL;
1114 		}
1115 	}
1116 #endif
1117 
1118 	/* Allocate DMA space (maybe) */
1119 	if (sc->sc_dataptr && (sc->sc_flags & SUNSCPAL_DISABLE_DMA) == 0 &&
1120 		(sc->sc_datalen >= sc->sc_min_dma_len))
1121 	{
1122 		SUNSCPAL_TRACE("sched: dma_alloc, len=%d\n", sc->sc_datalen);
1123 		sunscpal_dma_alloc(sc);
1124 	}
1125 
1126 	/*
1127 	 * Initialization hook called just after select,
1128 	 * at the beginning of COMMAND phase.
1129 	 * (but AFTER the DMA allocation is done)
1130 	 *
1131 	 * We need to set up the DMA engine BEFORE the target puts
1132 	 * the SCSI bus into any DATA phase.
1133 	 */
1134 	if (sr->sr_dma_hand) {
1135 		SUNSCPAL_TRACE("sched: dma_setup, dh=0x%x\n",
1136 		    (long) sr->sr_dma_hand);
1137 	    sunscpal_dma_setup(sc);
1138 	}
1139 
1140 	/*
1141 	 * Schedule a timeout for the job we are starting.
1142 	 */
1143 	if ((sr->sr_flags & SR_IMMED) == 0) {
1144 		i = mstohz(xs->timeout);
1145 		SUNSCPAL_TRACE("sched: set timeout=%d\n", i);
1146 		callout_reset(&sr->sr_xs->xs_callout, i,
1147 		    sunscpal_cmd_timeout, sr);
1148 	}
1149 
1150  have_nexus:
1151 
1152 	SUNSCPAL_TRACE("sched: call machine, cur=0x%x\n",
1153 	    (long)sc->sc_current);
1154 	sunscpal_machine(sc);
1155 	SUNSCPAL_TRACE("sched: machine done, cur=0x%x\n",
1156 	    (long)sc->sc_current);
1157 
1158 	/*
1159 	 * What state did sunscpal_machine() leave us in?
1160 	 * Hopefully it sometimes completes a job...
1161 	 */
1162 	if (sc->sc_state == SUNSCPAL_IDLE)
1163 		goto next_job;
1164 
1165 	return; 	/* Have work in progress. */
1166 }
1167 
1168 
1169 /*
1170  *  Reselect handler: checks for reselection, and if we are being
1171  *	reselected, it sets up sc->sc_current.
1172  *
1173  *  We are reselected when:
1174  *	SEL is TRUE
1175  *	IO  is TRUE
1176  *	BSY is FALSE
1177  */
1178 void
1179 sunscpal_reselect(struct sunscpal_softc *sc)
1180 {
1181 
1182 	/*
1183 	 * This controller does not implement disconnect/reselect, so
1184 	 * we really don't have anything to do here.  We keep this
1185 	 * function as a placeholder, though.
1186 	 */
1187 }
1188 
1189 /*
1190  *  Select target: xs is the transfer that we are selecting for.
1191  *  sc->sc_current should be NULL.
1192  *
1193  *  Returns:
1194  *	sc->sc_current != NULL  ==> we were reselected (race!)
1195  *	XS_NOERROR		==> selection worked
1196  *	XS_BUSY 		==> lost arbitration
1197  *	XS_SELTIMEOUT   	==> no response to selection
1198  */
1199 static int
1200 sunscpal_select(struct sunscpal_softc *sc, struct sunscpal_req *sr)
1201 {
1202 	int timo, target_mask;
1203 	u_short	mode;
1204 
1205 	/* Check for reselect */
1206 	sunscpal_reselect(sc);
1207 	if (sc->sc_current) {
1208 		SUNSCPAL_TRACE("select: reselect, cur=0x%x\n",
1209 		    (long)sc->sc_current);
1210 		return XS_BUSY;	/* reselected */
1211 	}
1212 
1213 	/*
1214 	 * Select the target.
1215 	 */
1216 	target_mask = (1 << sr->sr_target);
1217 	SUNSCPAL_WRITE_1(sc, sunscpal_data, target_mask);
1218 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, SUNSCPAL_ICR_SELECT);
1219 
1220 	/*
1221 	 * Wait for the target to assert BSY.
1222 	 * SCSI spec. says wait for 250 mS.
1223 	 */
1224 	for (timo = 25000;;) {
1225 		if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_BUSY)
1226 			goto success;
1227 		if (--timo <= 0)
1228 			break;
1229 		delay(10);
1230 	}
1231 
1232 	SUNSCPAL_WRITE_1(sc, sunscpal_data, 0);
1233 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
1234 
1235 	SUNSCPAL_TRACE("select: device down, rc=%d\n", XS_SELTIMEOUT);
1236 	return XS_SELTIMEOUT;
1237 
1238  success:
1239 
1240 	/*
1241 	 * The target is now driving BSY, so we can stop
1242 	 * driving SEL and the data bus.  We do set up
1243 	 * whether or not this target needs parity.
1244 	 */
1245 	mode = 0;
1246 	if ((sc->sc_parity_disable & target_mask) == 0)
1247 		mode |= SUNSCPAL_ICR_PARITY_ENABLE;
1248 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, mode);
1249 
1250 	return XS_NOERROR;
1251 }
1252 
1253 /*****************************************************************
1254  * Functions to handle each info. transfer phase:
1255  *****************************************************************/
1256 
1257 /*
1258  * The message system:
1259  *
1260  * This is a revamped message system that now should easier accommodate
1261  * new messages, if necessary.
1262  *
1263  * Currently we accept these messages:
1264  * IDENTIFY (when reselecting)
1265  * COMMAND COMPLETE # (expect bus free after messages marked #)
1266  * NOOP
1267  * MESSAGE REJECT
1268  * SYNCHRONOUS DATA TRANSFER REQUEST
1269  * SAVE DATA POINTER
1270  * RESTORE POINTERS
1271  * DISCONNECT #
1272  *
1273  * We may send these messages in prioritized order:
1274  * BUS DEVICE RESET #		if XS_CTL_RESET & xs->xs_control (or in
1275  *				weird sits.)
1276  * MESSAGE PARITY ERROR		par. err. during MSGI
1277  * MESSAGE REJECT		If we get a message we don't know how to handle
1278  * ABORT #			send on errors
1279  * INITIATOR DETECTED ERROR	also on errors (SCSI2) (during info xfer)
1280  * IDENTIFY			At the start of each transfer
1281  * SYNCHRONOUS DATA TRANSFER REQUEST	if appropriate
1282  * NOOP				if nothing else fits the bill ...
1283  */
1284 
1285 /*
1286  * Precondition:
1287  * The SCSI bus is already in the MSGI phase and there is a message byte
1288  * on the bus, along with an asserted REQ signal.
1289  *
1290  * Our return value determines whether our caller, sunscpal_machine()
1291  * will expect to see another REQ (and possibly phase change).
1292  */
1293 static int
1294 sunscpal_msg_in(struct sunscpal_softc *sc)
1295 {
1296 	struct sunscpal_req *sr = sc->sc_current;
1297 	struct scsipi_xfer *xs = sr->sr_xs;
1298 	int n, phase;
1299 	int act_flags;
1300 
1301 	act_flags = ACT_CONTINUE;
1302 
1303 	if (sc->sc_prevphase == SUNSCPAL_PHASE_MSG_IN) {
1304 		/* This is a continuation of the previous message. */
1305 		n = sc->sc_imp - sc->sc_imess;
1306 		SUNSCPAL_TRACE("msg_in: continuation, n=%d\n", n);
1307 		goto nextbyte;
1308 	}
1309 
1310 	/* This is a new MESSAGE IN phase.  Clean up our state. */
1311 	sc->sc_state &= ~SUNSCPAL_DROP_MSGIN;
1312 
1313  nextmsg:
1314 	n = 0;
1315 	sc->sc_imp = &sc->sc_imess[n];
1316 
1317  nextbyte:
1318 	/*
1319 	 * Read a whole message, but don't ack the last byte.  If we reject the
1320 	 * message, we have to assert ATN during the message transfer phase
1321 	 * itself.
1322 	 */
1323 	for (;;) {
1324 		/*
1325 		 * Read a message byte.
1326 		 * First, check BSY, REQ, phase...
1327 		 */
1328 		if (!SUNSCPAL_BUSY(sc)) {
1329 			SUNSCPAL_TRACE("msg_in: lost BSY, n=%d\n", n);
1330 			/* XXX - Assume the command completed? */
1331 			act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
1332 			return act_flags;
1333 		}
1334 		if (sunscpal_wait_req(sc)) {
1335 			SUNSCPAL_TRACE("msg_in: BSY but no REQ, n=%d\n", n);
1336 			/* Just let sunscpal_machine() handle it... */
1337 			return act_flags;
1338 		}
1339 		phase = SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr));
1340 		if (phase != SUNSCPAL_PHASE_MSG_IN) {
1341 			/*
1342 			 * Target left MESSAGE IN, probably because it
1343 			 * a) noticed our ATN signal, or
1344 			 * b) ran out of messages.
1345 			 */
1346 			return act_flags;
1347 		}
1348 		/* Still in MESSAGE IN phase, and REQ is asserted. */
1349 		if ((SUNSCPAL_READ_2(sc, sunscpal_icr) &
1350 		    SUNSCPAL_ICR_PARITY_ERROR) != 0) {
1351 			sunscpal_sched_msgout(sc, SEND_PARITY_ERROR);
1352 			sc->sc_state |= SUNSCPAL_DROP_MSGIN;
1353 		}
1354 
1355 		/* Gather incoming message bytes if needed. */
1356 		if ((sc->sc_state & SUNSCPAL_DROP_MSGIN) == 0) {
1357 			if (n >= SUNSCPAL_MAX_MSG_LEN) {
1358 				sunscpal_sched_msgout(sc, SEND_REJECT);
1359 				sc->sc_state |= SUNSCPAL_DROP_MSGIN;
1360 			} else {
1361 				*sc->sc_imp++ =
1362 				    SUNSCPAL_READ_1(sc, sunscpal_cmd_stat);
1363 				n++;
1364 				/*
1365 				 * This testing is suboptimal, but most
1366 				 * messages will be of the one byte variety, so
1367 				 * it should not affect performance
1368 				 * significantly.
1369 				 */
1370 				if (n == 1 && MSG_IS1BYTE(sc->sc_imess[0]))
1371 					goto have_msg;
1372 				if (n == 2 && MSG_IS2BYTE(sc->sc_imess[0]))
1373 					goto have_msg;
1374 				if (n >= 3 && MSG_ISEXTENDED(sc->sc_imess[0]) &&
1375 					n == sc->sc_imess[1] + 2)
1376 					goto have_msg;
1377 			}
1378 		}
1379 
1380 		/*
1381 		 * If we reach this spot we're either:
1382 		 * a) in the middle of a multi-byte message, or
1383 		 * b) dropping bytes.
1384 		 */
1385 
1386 		if (act_flags != ACT_CONTINUE)
1387 			return act_flags;
1388 
1389 		/* back to nextbyte */
1390 	}
1391 
1392  have_msg:
1393 	/* We now have a complete message.  Parse it. */
1394 
1395 	switch (sc->sc_imess[0]) {
1396 	case MSG_CMDCOMPLETE:
1397 		SUNSCPAL_TRACE("msg_in: CMDCOMPLETE\n", 0);
1398 		/* Target is about to disconnect. */
1399 		act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
1400 		break;
1401 
1402 	case MSG_PARITY_ERROR:
1403 		SUNSCPAL_TRACE("msg_in: PARITY_ERROR\n", 0);
1404 		/* Resend the last message. */
1405 		sunscpal_sched_msgout(sc, sc->sc_msgout);
1406 		break;
1407 
1408 	case MSG_MESSAGE_REJECT:
1409 		/* The target rejects the last message we sent. */
1410 		SUNSCPAL_TRACE("msg_in: got reject for 0x%x\n", sc->sc_msgout);
1411 		switch (sc->sc_msgout) {
1412 		case SEND_IDENTIFY:
1413 			/* Really old target controller? */
1414 			/* XXX ... */
1415 			break;
1416 		case SEND_INIT_DET_ERR:
1417 			goto abort;
1418 		}
1419 		break;
1420 
1421 	case MSG_NOOP:
1422 		SUNSCPAL_TRACE("msg_in: NOOP\n", 0);
1423 		break;
1424 
1425 	case MSG_DISCONNECT:
1426 		SUNSCPAL_TRACE("msg_in: DISCONNECT\n", 0);
1427 		/* Target is about to disconnect. */
1428 		act_flags |= ACT_DISCONNECT;
1429 		if ((xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0)
1430 			break;
1431 		/*FALLTHROUGH*/
1432 
1433 	case MSG_SAVEDATAPOINTER:
1434 		SUNSCPAL_TRACE("msg_in: SAVE_PTRS\n", 0);
1435 		sr->sr_dataptr = sc->sc_dataptr;
1436 		sr->sr_datalen = sc->sc_datalen;
1437 		break;
1438 
1439 	case MSG_RESTOREPOINTERS:
1440 		SUNSCPAL_TRACE("msg_in: RESTORE_PTRS\n", 0);
1441 		sc->sc_dataptr = sr->sr_dataptr;
1442 		sc->sc_datalen = sr->sr_datalen;
1443 		break;
1444 
1445 	case MSG_EXTENDED:
1446 		switch (sc->sc_imess[2]) {
1447 		case MSG_EXT_SDTR:
1448 		case MSG_EXT_WDTR:
1449 			/* This controller can not do synchronous mode. */
1450 			goto reject;
1451 		default:
1452 			printf("%s: unrecognized MESSAGE EXTENDED; "
1453 			    "sending REJECT\n",
1454 			    device_xname(sc->sc_dev));
1455 			SUNSCPAL_BREAK();
1456 			goto reject;
1457 		}
1458 		break;
1459 
1460 	default:
1461 		SUNSCPAL_TRACE("msg_in: eh? imsg=0x%x\n", sc->sc_imess[0]);
1462 		printf("%s: unrecognized MESSAGE; sending REJECT\n",
1463 		    device_xname(sc->sc_dev));
1464 		SUNSCPAL_BREAK();
1465 		/* FALLTHROUGH */
1466 	reject:
1467 		sunscpal_sched_msgout(sc, SEND_REJECT);
1468 		break;
1469 
1470 	abort:
1471 		sc->sc_state |= SUNSCPAL_ABORTING;
1472 		sunscpal_sched_msgout(sc, SEND_ABORT);
1473 		break;
1474 	}
1475 
1476 	/* Go get the next message, if any. */
1477 	if (act_flags == ACT_CONTINUE)
1478 		goto nextmsg;
1479 
1480 	return act_flags;
1481 }
1482 
1483 
1484 /*
1485  * The message out (and in) stuff is a bit complicated:
1486  * If the target requests another message (sequence) without
1487  * having changed phase in between it really asks for a
1488  * retransmit, probably due to parity error(s).
1489  * The following messages can be sent:
1490  * IDENTIFY	   @ These 4 stem from SCSI command activity
1491  * SDTR		   @
1492  * WDTR		   @
1493  * DEV_RESET	   @
1494  * REJECT if MSGI doesn't make sense
1495  * PARITY_ERROR if parity error while in MSGI
1496  * INIT_DET_ERR if parity error while not in MSGI
1497  * ABORT if INIT_DET_ERR rejected
1498  * NOOP if asked for a message and there's nothing to send
1499  *
1500  * Note that we call this one with (sc_current == NULL)
1501  * when sending ABORT for unwanted reselections.
1502  */
1503 static int
1504 sunscpal_msg_out(struct sunscpal_softc *sc)
1505 {
1506 	/*
1507 	 * This controller does not allow you to assert ATN, which
1508 	 * means we will never get the opportunity to send messages to
1509 	 * the target (the bus will never enter this MSG_OUT phase).
1510 	 * This will eventually leave us with no option other than to
1511 	 * reset the bus.  We keep this function as a placeholder,
1512 	 * though, and this printf will eventually go away or get
1513 	 * #ifdef'ed:
1514 	 */
1515 	printf("%s: bus is in MSG_OUT phase?\n", __func__);
1516 	return ACT_CONTINUE | ACT_RESET_BUS;
1517 }
1518 
1519 /*
1520  * Handle command phase.
1521  */
1522 static int
1523 sunscpal_command(struct sunscpal_softc *sc)
1524 {
1525 	struct sunscpal_req *sr = sc->sc_current;
1526 	struct scsipi_xfer *xs = sr->sr_xs;
1527 	int len;
1528 
1529 	/* Assume command can be sent in one go. */
1530 	/* XXX: Do this using DMA, and get a phase change intr? */
1531 	len = sunscpal_pio_out(sc, SUNSCPAL_PHASE_COMMAND, xs->cmdlen,
1532 	    (uint8_t *)xs->cmd);
1533 
1534 	if (len != xs->cmdlen) {
1535 #ifdef	SUNSCPAL_DEBUG
1536 		printf("%s: short transfer: wanted %d got %d.\n",
1537 		    __func__, xs->cmdlen, len);
1538 		sunscpal_show_scsi_cmd(xs);
1539 		SUNSCPAL_BREAK();
1540 #endif
1541 		if (len < 6) {
1542 			xs->error = XS_DRIVER_STUFFUP;
1543 			sc->sc_state |= SUNSCPAL_ABORTING;
1544 			sunscpal_sched_msgout(sc, SEND_ABORT);
1545 		}
1546 
1547 	}
1548 
1549 	return ACT_CONTINUE;
1550 }
1551 
1552 
1553 /*
1554  * Handle either data_in or data_out
1555  */
1556 static int
1557 sunscpal_data_xfer(struct sunscpal_softc *sc, int phase)
1558 {
1559 	struct sunscpal_req *sr = sc->sc_current;
1560 	struct scsipi_xfer *xs = sr->sr_xs;
1561 	int expected_phase;
1562 	int len;
1563 
1564 	/*
1565 	 * When aborting a command, disallow any data phase.
1566 	 */
1567 	if (sc->sc_state & SUNSCPAL_ABORTING) {
1568 		printf("%s: aborting, bus phase=%s (reset)\n",
1569 		    device_xname(sc->sc_dev), phase_names[(phase >> 8) & 7]);
1570 		return ACT_RESET_BUS;	/* XXX */
1571 	}
1572 
1573 	/* Validate expected phase (data_in or data_out) */
1574 	expected_phase = (xs->xs_control & XS_CTL_DATA_OUT) ?
1575 	    SUNSCPAL_PHASE_DATA_OUT : SUNSCPAL_PHASE_DATA_IN;
1576 	if (phase != expected_phase) {
1577 		printf("%s: data phase error\n", device_xname(sc->sc_dev));
1578 		goto abort;
1579 	}
1580 
1581 	/* Make sure we have some data to move. */
1582 	if (sc->sc_datalen <= 0) {
1583 		/* Device needs padding. */
1584 		if (phase == SUNSCPAL_PHASE_DATA_IN)
1585 			sunscpal_pio_in(sc, phase, 4096, NULL);
1586 		else
1587 			sunscpal_pio_out(sc, phase, 4096, NULL);
1588 		/* Make sure that caused a phase change. */
1589 		if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) ==
1590 		    phase) {
1591 			/* More than 4k is just too much! */
1592 			printf("%s: too much data padding\n",
1593 			    device_xname(sc->sc_dev));
1594 			goto abort;
1595 		}
1596 		return ACT_CONTINUE;
1597 	}
1598 
1599 	/*
1600 	 * Attempt DMA only if dma_alloc gave us a DMA handle AND
1601 	 * there is enough left to transfer so DMA is worth while.
1602 	 */
1603 	if (sr->sr_dma_hand && (sc->sc_datalen >= sc->sc_min_dma_len)) {
1604 		/*
1605 		 * OK, really start DMA.  Note, the MD start function
1606 		 * is responsible for setting the TCMD register, etc.
1607 		 * (Acknowledge the phase change there, not here.)
1608 		 */
1609 		SUNSCPAL_TRACE("data_xfer: dma_start, dh=0x%x\n",
1610 		    (long)sr->sr_dma_hand);
1611 		sunscpal_dma_start(sc);
1612 		return ACT_WAIT_DMA;
1613 	}
1614 
1615 	/*
1616 	 * Doing PIO for data transfer.  (Possibly "Pseudo DMA")
1617 	 * XXX:  Do PDMA functions need to set tcmd later?
1618 	 */
1619 	SUNSCPAL_TRACE("data_xfer: doing PIO, len=%d\n", sc->sc_datalen);
1620 	if (phase == SUNSCPAL_PHASE_DATA_OUT) {
1621 		len = sunscpal_pio_out(sc, phase,
1622 		    sc->sc_datalen, sc->sc_dataptr);
1623 	} else {
1624 		len = sunscpal_pio_in(sc, phase,
1625 		    sc->sc_datalen, sc->sc_dataptr);
1626 	}
1627 	sc->sc_dataptr += len;
1628 	sc->sc_datalen -= len;
1629 
1630 	SUNSCPAL_TRACE("data_xfer: did PIO, resid=%d\n", sc->sc_datalen);
1631 	return ACT_CONTINUE;
1632 
1633  abort:
1634 	sc->sc_state |= SUNSCPAL_ABORTING;
1635 	sunscpal_sched_msgout(sc, SEND_ABORT);
1636 	return ACT_CONTINUE;
1637 }
1638 
1639 
1640 static int
1641 sunscpal_status(struct sunscpal_softc *sc)
1642 {
1643 	int len;
1644 	uint8_t status;
1645 	struct sunscpal_req *sr = sc->sc_current;
1646 
1647 	len = sunscpal_pio_in(sc, SUNSCPAL_PHASE_STATUS, 1, &status);
1648 	if (len) {
1649 		sr->sr_status = status;
1650 	} else {
1651 		printf("%s: none?\n", __func__);
1652 	}
1653 
1654 	return ACT_CONTINUE;
1655 }
1656 
1657 
1658 /*
1659  * This is the big state machine that follows SCSI phase changes.
1660  * This is somewhat like a co-routine.  It will do a SCSI command,
1661  * and exit if the command is complete, or if it must wait, i.e.
1662  * for DMA to complete or for reselect to resume the job.
1663  *
1664  * The bus must be selected, and we need to know which command is
1665  * being undertaken.
1666  */
1667 static void
1668 sunscpal_machine(struct sunscpal_softc *sc)
1669 {
1670 	struct sunscpal_req *sr;
1671 	struct scsipi_xfer *xs;
1672 	int act_flags, phase, timo;
1673 
1674 #ifdef	DIAGNOSTIC
1675 	if (sc->sc_state == SUNSCPAL_IDLE)
1676 		panic("%s: state=idle", __func__);
1677 	if (sc->sc_current == NULL)
1678 		panic("%s: no current cmd", __func__);
1679 #endif
1680 
1681 	sr = sc->sc_current;
1682 	xs = sr->sr_xs;
1683 	act_flags = ACT_CONTINUE;
1684 
1685 	/*
1686 	 * This will be called by sunscpal_intr() when DMA is
1687 	 * complete.  Must stop DMA before touching the PAL or
1688 	 * there will be "register conflict" errors.
1689 	 */
1690 	if ((sc->sc_state & SUNSCPAL_DOINGDMA) != 0) {
1691 		/* Pick-up where where we left off... */
1692 		goto dma_done;
1693 	}
1694 
1695  next_phase:
1696 
1697 	if (!SUNSCPAL_BUSY(sc)) {
1698 		/* Unexpected disconnect */
1699 		printf("%s: unexpected disconnect.\n", __func__);
1700 		xs->error = XS_DRIVER_STUFFUP;
1701 		act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
1702 		goto do_actions;
1703 	}
1704 
1705 	/*
1706 	 * Wait for REQ before reading the phase.
1707 	 * Need to wait longer than usual here, because
1708 	 * some devices are just plain slow...
1709 	 */
1710 	timo = sunscpal_wait_phase_timo;
1711 	for (;;) {
1712 		if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_REQUEST)
1713 			break;
1714 		if (--timo <= 0) {
1715 			if (sc->sc_state & SUNSCPAL_ABORTING) {
1716 				printf("%s: no REQ while aborting, reset\n",
1717 				    device_xname(sc->sc_dev));
1718 				act_flags |= ACT_RESET_BUS;
1719 				goto do_actions;
1720 			}
1721 			printf("%s: no REQ for next phase, abort\n",
1722 			    device_xname(sc->sc_dev));
1723 			sc->sc_state |= SUNSCPAL_ABORTING;
1724 			sunscpal_sched_msgout(sc, SEND_ABORT);
1725 			goto next_phase;
1726 		}
1727 		delay(100);
1728 	}
1729 
1730 	phase = SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr));
1731 	SUNSCPAL_TRACE("machine: phase=%s\n",
1732 	    (long)phase_names[(phase >> 8) & 7]);
1733 
1734 	/*
1735 	 * We assume that the device knows what it's doing,
1736 	 * so any phase is good.
1737 	 */
1738 
1739 	switch (phase) {
1740 
1741 	case SUNSCPAL_PHASE_DATA_OUT:
1742 	case SUNSCPAL_PHASE_DATA_IN:
1743 		act_flags = sunscpal_data_xfer(sc, phase);
1744 		break;
1745 
1746 	case SUNSCPAL_PHASE_COMMAND:
1747 		act_flags = sunscpal_command(sc);
1748 		break;
1749 
1750 	case SUNSCPAL_PHASE_STATUS:
1751 		act_flags = sunscpal_status(sc);
1752 		break;
1753 
1754 	case SUNSCPAL_PHASE_MSG_OUT:
1755 		act_flags = sunscpal_msg_out(sc);
1756 		break;
1757 
1758 	case SUNSCPAL_PHASE_MSG_IN:
1759 		act_flags = sunscpal_msg_in(sc);
1760 		break;
1761 
1762 	default:
1763 		printf("%s: Unexpected phase 0x%x\n", __func__, phase);
1764 		sc->sc_state |= SUNSCPAL_ABORTING;
1765 		sunscpal_sched_msgout(sc, SEND_ABORT);
1766 		goto next_phase;
1767 
1768 	} /* switch */
1769 	sc->sc_prevphase = phase;
1770 
1771  do_actions:
1772 
1773 	if (act_flags & ACT_WAIT_DMA) {
1774 		act_flags &= ~ACT_WAIT_DMA;
1775 		/* Wait for DMA to complete (polling, or interrupt). */
1776 		if ((sr->sr_flags & SR_IMMED) == 0) {
1777 			SUNSCPAL_TRACE("machine: wait for DMA intr.\n", 0);
1778 			return; 	/* will resume at dma_done */
1779 		}
1780 		/* Busy-wait for it to finish. */
1781 		SUNSCPAL_TRACE("machine: dma_poll, dh=0x%x\n",
1782 		    (long)sr->sr_dma_hand);
1783 		sunscpal_dma_poll(sc);
1784  dma_done:
1785 		/* Return here after interrupt. */
1786 		if (sr->sr_flags & SR_OVERDUE)
1787 			sc->sc_state |= SUNSCPAL_ABORTING;
1788 		SUNSCPAL_TRACE("machine: dma_stop, dh=0x%x\n",
1789 		    (long)sr->sr_dma_hand);
1790 		sunscpal_dma_stop(sc);
1791 		SUNSCPAL_CLR_INTR(sc);	/* XXX */
1792 		/*
1793 		 * While DMA is running we can not touch the SBC,
1794 		 * so various places just set SUNSCPAL_ABORTING and
1795 		 * expect us the "kick it" when DMA is done.
1796 		 */
1797 		if (sc->sc_state & SUNSCPAL_ABORTING) {
1798 			sunscpal_sched_msgout(sc, SEND_ABORT);
1799 		}
1800 	}
1801 
1802 	/*
1803 	 * Check for parity error.
1804 	 * XXX - better place to check?
1805 	 */
1806 	if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_PARITY_ERROR) {
1807 		printf("%s: parity error!\n", device_xname(sc->sc_dev));
1808 		/* XXX: sc->sc_state |= SUNSCPAL_ABORTING; */
1809 		sunscpal_sched_msgout(sc, SEND_PARITY_ERROR);
1810 	}
1811 
1812 	if (act_flags == ACT_CONTINUE)
1813 		goto next_phase;
1814 	/* All other actions "break" from the loop. */
1815 
1816 	SUNSCPAL_TRACE("machine: act_flags=0x%x\n", act_flags);
1817 
1818 	if (act_flags & ACT_RESET_BUS) {
1819 		act_flags |= ACT_CMD_DONE;
1820 		/*
1821 		 * Reset the SCSI bus, usually due to a timeout.
1822 		 * The error code XS_TIMEOUT allows retries.
1823 		 */
1824 		sc->sc_state |= SUNSCPAL_ABORTING;
1825 		printf("%s: reset SCSI bus for TID=%d LUN=%d\n",
1826 		    device_xname(sc->sc_dev), sr->sr_target, sr->sr_lun);
1827 		sunscpal_reset_scsibus(sc);
1828 	}
1829 
1830 	if (act_flags & ACT_CMD_DONE) {
1831 		act_flags |= ACT_DISCONNECT;
1832 		/* Need to call scsipi_done() */
1833 		/* XXX: from the aic6360 driver, but why? */
1834 		if (sc->sc_datalen < 0) {
1835 			printf("%s: %d extra bytes from %d:%d\n",
1836 			    device_xname(sc->sc_dev), -sc->sc_datalen,
1837 			    sr->sr_target, sr->sr_lun);
1838 			sc->sc_datalen = 0;
1839 		}
1840 		xs->resid = sc->sc_datalen;
1841 		/* Note: this will clear sc_current */
1842 		SUNSCPAL_TRACE("machine: call done, cur=0x%x\n", (long)sr);
1843 		sunscpal_done(sc);
1844 	}
1845 
1846 	if (act_flags & ACT_DISCONNECT) {
1847 		/*
1848 		 * The device has dropped BSY (or will soon).
1849 		 * We have to wait here for BSY to drop, otherwise
1850 		 * the next command may decide we need a bus reset.
1851 		 */
1852 		timo = sunscpal_wait_req_timo;	/* XXX */
1853 		for (;;) {
1854 			if (!SUNSCPAL_BUSY(sc))
1855 				goto busfree;
1856 			if (--timo <= 0)
1857 				break;
1858 			delay(2);
1859 		}
1860 		/* Device is sitting on the bus! */
1861 		printf("%s: Target %d LUN %d stuck busy, resetting...\n",
1862 		    device_xname(sc->sc_dev), sr->sr_target, sr->sr_lun);
1863 		sunscpal_reset_scsibus(sc);
1864  busfree:
1865 		SUNSCPAL_TRACE("machine: discon, waited %d\n",
1866 			sunscpal_wait_req_timo - timo);
1867 
1868 		SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
1869 
1870 		if ((act_flags & ACT_CMD_DONE) == 0) {
1871 			SUNSCPAL_TRACE("machine: discon, cur=0x%x\n", (long)sr);
1872 		}
1873 
1874 		/*
1875 		 * We may be here due to a disconnect message,
1876 		 * in which case we did NOT call sunscpal_done,
1877 		 * and we need to clear sc_current.
1878 		 */
1879 		sc->sc_state = SUNSCPAL_IDLE;
1880 		sc->sc_current = NULL;
1881 
1882 		/* Paranoia: clear everything. */
1883 		sc->sc_dataptr = NULL;
1884 		sc->sc_datalen = 0;
1885 		sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
1886 		sc->sc_msgpriq = 0;
1887 		sc->sc_msgoutq = 0;
1888 		sc->sc_msgout  = 0;
1889 
1890 		/* Our caller will re-enable interrupts. */
1891 	}
1892 }
1893 
1894 
1895 #ifdef	SUNSCPAL_DEBUG
1896 
1897 static void
1898 sunscpal_show_scsi_cmd(struct scsipi_xfer *xs)
1899 {
1900 	uint8_t *b = (uint8_t *)xs->cmd;
1901 	int i = 0;
1902 
1903 	scsipi_printaddr(xs->xs_periph);
1904 	if ((xs->xs_control & XS_CTL_RESET) == 0) {
1905 		printf("-");
1906 		while (i < xs->cmdlen) {
1907 			if (i != 0)
1908 				printf(",");
1909 			printf("%x", b[i++]);
1910 		}
1911 		printf("-\n");
1912 	} else {
1913 		printf("-RESET-\n");
1914 	}
1915 }
1916 
1917 
1918 int sunscpal_traceidx = 0;
1919 
1920 #define	TRACE_MAX	1024
1921 struct trace_ent {
1922 	char *msg;
1923 	long  val;
1924 } sunscpal_tracebuf[TRACE_MAX];
1925 
1926 void
1927 sunscpal_trace(char *msg, long val)
1928 {
1929 	struct trace_ent *tr;
1930 	int s;
1931 
1932 	s = splbio();
1933 
1934 	tr = &sunscpal_tracebuf[sunscpal_traceidx];
1935 
1936 	sunscpal_traceidx++;
1937 	if (sunscpal_traceidx >= TRACE_MAX)
1938 		sunscpal_traceidx = 0;
1939 
1940 	tr->msg = msg;
1941 	tr->val = val;
1942 
1943 	splx(s);
1944 }
1945 
1946 #ifdef	DDB
1947 void
1948 sunscpal_clear_trace(void)
1949 {
1950 
1951 	sunscpal_traceidx = 0;
1952 	memset((void *)sunscpal_tracebuf, 0, sizeof(sunscpal_tracebuf));
1953 }
1954 
1955 void
1956 sunscpal_show_trace(void)
1957 {
1958 	struct trace_ent *tr;
1959 	int idx;
1960 
1961 	idx = sunscpal_traceidx;
1962 	do {
1963 		tr = &sunscpal_tracebuf[idx];
1964 		idx++;
1965 		if (idx >= TRACE_MAX)
1966 			idx = 0;
1967 		if (tr->msg)
1968 			db_printf(tr->msg, tr->val);
1969 	} while (idx != sunscpal_traceidx);
1970 }
1971 
1972 void
1973 sunscpal_show_req(struct sunscpal_req *sr)
1974 {
1975 	struct scsipi_xfer *xs = sr->sr_xs;
1976 
1977 	db_printf("TID=%d ",	sr->sr_target);
1978 	db_printf("LUN=%d ",	sr->sr_lun);
1979 	db_printf("dh=%p ",	sr->sr_dma_hand);
1980 	db_printf("dptr=%p ",	sr->sr_dataptr);
1981 	db_printf("dlen=0x%x ",	sr->sr_datalen);
1982 	db_printf("flags=%d ",	sr->sr_flags);
1983 	db_printf("stat=%d ",	sr->sr_status);
1984 
1985 	if (xs == NULL) {
1986 		db_printf("(xs=NULL)\n");
1987 		return;
1988 	}
1989 	db_printf("\n");
1990 #ifdef	SCSIDEBUG
1991 	show_scsipi_xs(xs);
1992 #else
1993 	db_printf("xs=%p\n", xs);
1994 #endif
1995 }
1996 
1997 void
1998 sunscpal_show_state(void)
1999 {
2000 	struct sunscpal_softc *sc;
2001 	struct sunscpal_req *sr;
2002 	int i, j, k;
2003 
2004 	sc = sunscpal_debug_sc;
2005 
2006 	if (sc == NULL) {
2007 		db_printf("sunscpal_debug_sc == NULL\n");
2008 		return;
2009 	}
2010 
2011 	db_printf("sc_ncmds=%d\n",  	sc->sc_ncmds);
2012 	k = -1;	/* which is current? */
2013 	for (i = 0; i < SUNSCPAL_OPENINGS; i++) {
2014 		sr = &sc->sc_ring[i];
2015 		if (sr->sr_xs) {
2016 			if (sr == sc->sc_current)
2017 				k = i;
2018 			db_printf("req %d: (sr=%p)", i, sr);
2019 			sunscpal_show_req(sr);
2020 		}
2021 	}
2022 	db_printf("sc_rr=%d, current=%d\n", sc->sc_rr, k);
2023 
2024 	db_printf("Active request matrix:\n");
2025 	for(i = 0; i < 8; i++) {		/* targets */
2026 		for (j = 0; j < 8; j++) {	/* LUN */
2027 			sr = sc->sc_matrix[i][j];
2028 			if (sr) {
2029 				db_printf("TID=%d LUN=%d sr=%p\n", i, j, sr);
2030 			}
2031 		}
2032 	}
2033 
2034 	db_printf("sc_state=0x%x\n",	sc->sc_state);
2035 	db_printf("sc_current=%p\n",	sc->sc_current);
2036 	db_printf("sc_dataptr=%p\n",	sc->sc_dataptr);
2037 	db_printf("sc_datalen=0x%x\n",	sc->sc_datalen);
2038 
2039 	db_printf("sc_prevphase=%d\n",	sc->sc_prevphase);
2040 	db_printf("sc_msgpriq=0x%x\n",	sc->sc_msgpriq);
2041 }
2042 #endif	/* DDB */
2043 #endif	/* SUNSCPAL_DEBUG */
2044 
2045 void
2046 sunscpal_attach(struct sunscpal_softc *sc, int options)
2047 {
2048 
2049 	/*
2050 	 * Handle our options.
2051 	 */
2052 	aprint_normal(": options=0x%x\n", options);
2053 	sc->sc_parity_disable = (options & SUNSCPAL_OPT_NO_PARITY_CHK);
2054 	if (options & SUNSCPAL_OPT_DISABLE_DMA)
2055 		sc->sc_flags |= SUNSCPAL_DISABLE_DMA;
2056 
2057 	/*
2058 	 * Fill in the adapter.
2059 	 */
2060 	memset(&sc->sc_adapter, 0, sizeof(sc->sc_adapter));
2061 	sc->sc_adapter.adapt_dev = sc->sc_dev;
2062 	sc->sc_adapter.adapt_nchannels = 1;
2063 	sc->sc_adapter.adapt_openings = SUNSCPAL_OPENINGS;
2064 	sc->sc_adapter.adapt_max_periph = 1;
2065 	sc->sc_adapter.adapt_request = sunscpal_scsipi_request;
2066 	sc->sc_adapter.adapt_minphys = sunscpal_minphys;
2067 	if (options & SUNSCPAL_OPT_FORCE_POLLING)
2068 		sc->sc_adapter.adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
2069 
2070 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
2071 	sc->sc_channel.chan_bustype = &scsi_bustype;
2072 	sc->sc_channel.chan_channel = 0;
2073 	sc->sc_channel.chan_ntargets = 8;
2074 	sc->sc_channel.chan_nluns = 8;
2075 	sc->sc_channel.chan_id = 7;
2076 
2077 	/*
2078 	 * Add reference to adapter so that we drop the reference after
2079 	 * config_found() to make sure the adatper is disabled.
2080 	 */
2081 	if (scsipi_adapter_addref(&sc->sc_adapter) != 0) {
2082 		aprint_error_dev(sc->sc_dev, "unable to enable controller\n");
2083 		return;
2084 	}
2085 
2086 	sunscpal_init(sc);	/* Init chip and driver */
2087 	sunscpal_reset_scsibus(sc);
2088 
2089 	/*
2090 	 * Ask the adapter what subunits are present
2091 	 */
2092 	(void)config_found(sc->sc_dev, &sc->sc_channel, scsiprint);
2093 	scsipi_adapter_delref(&sc->sc_adapter);
2094 }
2095 
2096 int
2097 sunscpal_detach(struct sunscpal_softc *sc, int flags)
2098 {
2099 
2100 	return EOPNOTSUPP;
2101 }
2102 
2103 static void
2104 sunscpal_minphys(struct buf *bp)
2105 {
2106 
2107 	if (bp->b_bcount > SUNSCPAL_MAX_DMA_LEN) {
2108 #ifdef	SUNSCPAL_DEBUG
2109 		if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
2110 			printf("%s: len = 0x%lx.\n", __func__, bp->b_bcount);
2111 			Debugger();
2112 		}
2113 #endif
2114 		bp->b_bcount = SUNSCPAL_MAX_DMA_LEN;
2115 	}
2116 	return minphys(bp);
2117 }
2118 
2119 #ifdef SUNSCPAL_USE_BUS_DMA
2120 
2121 /*
2122  * Allocate a DMA handle and put it in sr->sr_dma_hand.  Prepare
2123  * for DMA transfer.
2124  */
2125 static void
2126 sunscpal_dma_alloc(struct sunscpal_softc *sc)
2127 {
2128 	struct sunscpal_req *sr = sc->sc_current;
2129 	sunscpal_dma_handle_t dh;
2130 	int i, xlen;
2131 	u_long addr;
2132 
2133 #ifdef	DIAGNOSTIC
2134 	if (sr->sr_dma_hand != NULL)
2135 		panic("%s: already have DMA handle", __func__);
2136 #endif
2137 
2138 	addr = (u_long)sc->sc_dataptr;
2139 	xlen = sc->sc_datalen;
2140 
2141 	/* If the DMA start addr is misaligned then do PIO */
2142 	if ((addr & 1) || (xlen & 1)) {
2143 		printf("%s: misaligned.\n", __func__);
2144 		return;
2145 	}
2146 
2147 	/* Make sure our caller checked sc_min_dma_len. */
2148 	if (xlen < sc->sc_min_dma_len)
2149 		panic("%s: xlen=0x%x", __func__, xlen);
2150 
2151 	/*
2152 	 * Never attempt single transfers of more than 63k, because
2153 	 * our count register is only 16 bits.
2154 	 * This should never happen since already bounded by minphys().
2155 	 * XXX - Should just segment these...
2156 	 */
2157 	if (xlen > SUNSCPAL_MAX_DMA_LEN) {
2158 		printf("%s: excessive xlen=0x%x\n", __func__, xlen);
2159 		Debugger();
2160 		sc->sc_datalen = xlen = SUNSCPAL_MAX_DMA_LEN;
2161 	}
2162 
2163 	/* Find free DMA handle.  Guaranteed to find one since we have
2164 	   as many DMA handles as the driver has processes. */
2165 	for (i = 0; i < SUNSCPAL_OPENINGS; i++) {
2166 		if ((sc->sc_dma_handles[i].dh_flags & SUNSCDH_BUSY) == 0)
2167 			goto found;
2168 	}
2169 	panic("%s: no free DMA handles.", device_xname(sc->sc_dev));
2170  found:
2171 
2172 	dh = &sc->sc_dma_handles[i];
2173 	dh->dh_flags = SUNSCDH_BUSY;
2174 	dh->dh_mapaddr = (uint8_t *)addr;
2175 	dh->dh_maplen  = xlen;
2176 	dh->dh_dvma = 0;
2177 
2178 	/* Load the DMA map. */
2179 	if (bus_dmamap_load(sc->sunscpal_dmat, dh->dh_dmamap,
2180 	    dh->dh_mapaddr, dh->dh_maplen, NULL, BUS_DMA_NOWAIT) != 0) {
2181 		/* Can't load map */
2182 		printf("%s: can't DMA %p/0x%x\n", __func__,
2183 		    dh->dh_mapaddr, dh->dh_maplen);
2184 		dh->dh_flags = 0;
2185 		return;
2186 	}
2187 
2188 	/* success */
2189 	sr->sr_dma_hand = dh;
2190 }
2191 
2192 static void
2193 sunscpal_dma_free(struct sunscpal_softc *sc)
2194 {
2195 	struct sunscpal_req *sr = sc->sc_current;
2196 	sunscpal_dma_handle_t dh = sr->sr_dma_hand;
2197 
2198 #ifdef	DIAGNOSTIC
2199 	if (dh == NULL)
2200 		panic("%s: no DMA handle", __func__);
2201 #endif
2202 
2203 	if (sc->sc_state & SUNSCPAL_DOINGDMA)
2204 		panic("%s: free while in progress", __func__);
2205 
2206 	if (dh->dh_flags & SUNSCDH_BUSY) {
2207 		/* XXX - Should separate allocation and mapping. */
2208 		/* Give back the DVMA space. */
2209 		bus_dmamap_unload(sc->sunscpal_dmat, dh->dh_dmamap);
2210 		dh->dh_flags = 0;
2211 	}
2212 	sr->sr_dma_hand = NULL;
2213 }
2214 
2215 /*
2216  * This function is called during the SELECT phase that
2217  * precedes a COMMAND phase, in case we need to setup the
2218  * DMA engine before the bus enters a DATA phase.
2219  *
2220  * On the sc version, setup the start address and the count.
2221  */
2222 static void
2223 sunscpal_dma_setup(struct sunscpal_softc *sc)
2224 {
2225 	struct sunscpal_req *sr = sc->sc_current;
2226 	struct scsipi_xfer *xs = sr->sr_xs;
2227 	sunscpal_dma_handle_t dh = sr->sr_dma_hand;
2228 	long data_pa;
2229 	int xlen;
2230 
2231 	/*
2232 	 * Get the DVMA mapping for this segment.
2233 	 * XXX - Should separate allocation and mapin.
2234 	 */
2235 	data_pa = dh->dh_dvma;
2236 	data_pa += (sc->sc_dataptr - dh->dh_mapaddr);
2237 	if (data_pa & 1)
2238 		panic("%s: bad pa=0x%lx", __func__, data_pa);
2239 	xlen = sc->sc_datalen;
2240 	if (xlen & 1)
2241 		panic("%s: bad xlen=0x%x", __func__, xlen);
2242 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
2243 
2244 #ifdef	SUNSCPAL_DEBUG
2245 	if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
2246 		printf("%s: dh=%p, pa=0x%lx, xlen=0x%x\n",
2247 		    __func__, dh, data_pa, xlen);
2248 	}
2249 #endif
2250 
2251 	/* sync the DMA map: */
2252 	bus_dmamap_sync(sc->sunscpal_dmat, dh->dh_dmamap, 0, dh->dh_maplen,
2253 	    ((xs->xs_control & XS_CTL_DATA_OUT) == 0 ?
2254 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
2255 
2256 	/* Load the start address and the count. */
2257 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_h, (data_pa >> 16) & 0xFFFF);
2258 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_l, (data_pa >> 0) & 0xFFFF);
2259 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(xlen));
2260 }
2261 
2262 #endif /* SUNSCPAL_USE_BUS_DMA */
2263