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