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