Home | History | Annotate | Line # | Download | only in ic
sunscpal.c revision 1.14
      1 /*	$NetBSD: sunscpal.c,v 1.14 2003/05/03 18:11:24 wiz 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 (at) 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.14 2003/05/03 18:11:24 wiz 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 __P((struct sunscpal_softc *));
    107 static void	sunscpal_sched __P((struct sunscpal_softc *));
    108 static void	sunscpal_done __P((struct sunscpal_softc *));
    109 
    110 static int	sunscpal_select
    111 	__P((struct sunscpal_softc *, struct sunscpal_req *));
    112 static void	sunscpal_reselect __P((struct sunscpal_softc *));
    113 
    114 static int	sunscpal_msg_in __P((struct sunscpal_softc *));
    115 static int	sunscpal_msg_out __P((struct sunscpal_softc *));
    116 static int	sunscpal_data_xfer __P((struct sunscpal_softc *, int));
    117 static int	sunscpal_command __P((struct sunscpal_softc *));
    118 static int	sunscpal_status __P((struct sunscpal_softc *));
    119 static void	sunscpal_machine __P((struct sunscpal_softc *));
    120 
    121 void	sunscpal_abort __P((struct sunscpal_softc *));
    122 void	sunscpal_cmd_timeout __P((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 __P((struct scsipi_xfer *));
    153 #ifdef DDB
    154 void	sunscpal_clear_trace __P((void));
    155 void	sunscpal_show_trace __P((void));
    156 void	sunscpal_show_req __P((struct sunscpal_req *));
    157 void	sunscpal_show_state __P((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 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 __P((struct sunscpal_softc *));
    180 static void sunscpal_dma_free __P((struct sunscpal_softc *));
    181 static void sunscpal_dma_setup __P((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 __P((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 __P((struct sunscpal_softc *));
    205 static __inline int sunscpal_wait_not_req __P((struct sunscpal_softc *));
    206 static __inline void sunscpal_sched_msgout __P((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 __P((struct sunscpal_softc *));
    247 static void sunscpal_dma_poll __P((struct sunscpal_softc *));
    248 static void sunscpal_dma_stop __P((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 	    sc->sc_dev.dv_xname,
    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 (sc->sc_flags & SUNSCPAL_FORCE_POLLING)
    756 			flags |= XS_CTL_POLL;
    757 
    758 		if (flags & XS_CTL_DATA_UIO)
    759 			panic("sunscpal: scsi data uio requested");
    760 
    761 		s = splbio();
    762 
    763 		if (flags & XS_CTL_POLL) {
    764 			/* Terminate any current command. */
    765 			sr = sc->sc_current;
    766 			if (sr) {
    767 				printf("%s: polled request aborting %d/%d\n",
    768 				    sc->sc_dev.dv_xname,
    769 				    sr->sr_target, sr->sr_lun);
    770 				sunscpal_abort(sc);
    771 			}
    772 			if (sc->sc_state != SUNSCPAL_IDLE) {
    773 				panic("sunscpal_scsi_cmd: polled request, abort failed");
    774 			}
    775 		}
    776 
    777 		/*
    778 		 * Find lowest empty slot in ring buffer.
    779 		 * XXX: What about "fairness" and cmd order?
    780 		 */
    781 		for (i = 0; i < SUNSCPAL_OPENINGS; i++)
    782 			if (sc->sc_ring[i].sr_xs == NULL)
    783 				goto new;
    784 
    785 		xs->error = XS_RESOURCE_SHORTAGE;
    786 		SUNSCPAL_TRACE("scsipi_cmd: no openings, rv=%d\n", rv);
    787 		goto out;
    788 
    789 new:
    790 		/* Create queue entry */
    791 		sr = &sc->sc_ring[i];
    792 		sr->sr_xs = xs;
    793 		sr->sr_target = xs->xs_periph->periph_target;
    794 		sr->sr_lun = xs->xs_periph->periph_lun;
    795 		sr->sr_dma_hand = NULL;
    796 		sr->sr_dataptr = xs->data;
    797 		sr->sr_datalen = xs->datalen;
    798 		sr->sr_flags = (flags & XS_CTL_POLL) ? SR_IMMED : 0;
    799 		sr->sr_status = -1;	/* no value */
    800 		sc->sc_ncmds++;
    801 
    802 		SUNSCPAL_TRACE("scsipi_cmd: new sr=0x%x\n", (long)sr);
    803 
    804 		if (flags & XS_CTL_POLL) {
    805 			/* Force this new command to be next. */
    806 			sc->sc_rr = i;
    807 		}
    808 
    809 		/*
    810 		 * If we were idle, run some commands...
    811 		 */
    812 		if (sc->sc_state == SUNSCPAL_IDLE) {
    813 			SUNSCPAL_TRACE("scsipi_cmd: call sched, cur=0x%x\n",
    814 				  (long) sc->sc_current);
    815 			sunscpal_sched(sc);
    816 			SUNSCPAL_TRACE("scsipi_cmd: sched done, cur=0x%x\n",
    817 				  (long) sc->sc_current);
    818 		}
    819 
    820 		if (flags & XS_CTL_POLL) {
    821 			/* Make sure sunscpal_sched() finished it. */
    822 			if ((xs->xs_status & XS_STS_DONE) == 0)
    823 				panic("sunscpal_scsi_cmd: poll didn't finish");
    824 		}
    825 
    826 out:
    827 		splx(s);
    828 		return;
    829 
    830 	case ADAPTER_REQ_GROW_RESOURCES:
    831 		/* XXX Not supported. */
    832 		return;
    833 
    834 	case ADAPTER_REQ_SET_XFER_MODE:
    835 	    {
    836 		/*
    837 		 * We don't support Sync, Wide, or Tagged Queueing.
    838 		 * Just callback now, to report this.
    839 		 */
    840 		struct scsipi_xfer_mode *xm = arg;
    841 
    842 		xm->xm_mode = 0;
    843 		xm->xm_period = 0;
    844 		xm->xm_offset = 0;
    845 		scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
    846 		return;
    847 	    }
    848 	}
    849 }
    850 
    851 
    852 /*
    853  * POST PROCESSING OF SCSI_CMD (usually current)
    854  * Called by sunscpal_sched(), sunscpal_machine()
    855  */
    856 static void
    857 sunscpal_done(sc)
    858 	struct sunscpal_softc *sc;
    859 {
    860 	struct	sunscpal_req *sr;
    861 	struct	scsipi_xfer *xs;
    862 
    863 #ifdef	DIAGNOSTIC
    864 	if (sc->sc_state == SUNSCPAL_IDLE)
    865 		panic("sunscpal_done: state=idle");
    866 	if (sc->sc_current == NULL)
    867 		panic("sunscpal_done: current=0");
    868 #endif
    869 
    870 	sr = sc->sc_current;
    871 	xs = sr->sr_xs;
    872 
    873 	SUNSCPAL_TRACE("done: top, cur=0x%x\n", (long) sc->sc_current);
    874 
    875 	/*
    876 	 * Clean up DMA resources for this command.
    877 	 */
    878 	if (sr->sr_dma_hand) {
    879 		SUNSCPAL_TRACE("done: dma_free, dh=0x%x\n",
    880 				  (long) sr->sr_dma_hand);
    881 		sunscpal_dma_free(sc);
    882 	}
    883 #ifdef	DIAGNOSTIC
    884 	if (sr->sr_dma_hand)
    885 		panic("sunscpal_done: DMA free did not");
    886 #endif
    887 
    888 	if (sc->sc_state & SUNSCPAL_ABORTING) {
    889 		SUNSCPAL_TRACE("done: aborting, error=%d\n", xs->error);
    890 		if (xs->error == XS_NOERROR)
    891 			xs->error = XS_TIMEOUT;
    892 	}
    893 
    894 	SUNSCPAL_TRACE("done: check error=%d\n", (long) xs->error);
    895 
    896 	/* If error is already set, ignore sr_status value. */
    897 	if (xs->error != XS_NOERROR)
    898 		goto finish;
    899 
    900 	SUNSCPAL_TRACE("done: check status=%d\n", sr->sr_status);
    901 
    902 	xs->status = sr->sr_status;
    903 	switch (sr->sr_status) {
    904 	case SCSI_OK:	/* 0 */
    905 		break;
    906 
    907 	case SCSI_CHECK:
    908 	case SCSI_BUSY:
    909 		xs->error = XS_BUSY;
    910 		break;
    911 
    912 	case -1:
    913 		/* This is our "impossible" initial value. */
    914 		/* fallthrough */
    915 	default:
    916 		printf("%s: target %d, bad status=%d\n",
    917 		    sc->sc_dev.dv_xname, sr->sr_target, sr->sr_status);
    918 		xs->error = XS_DRIVER_STUFFUP;
    919 		break;
    920 	}
    921 
    922 finish:
    923 
    924 	SUNSCPAL_TRACE("done: finish, error=%d\n", xs->error);
    925 
    926 	/*
    927 	 * Dequeue the finished command, but don't clear sc_state until
    928 	 * after the call to scsipi_done(), because that may call back to
    929 	 * sunscpal_scsi_cmd() - unwanted recursion!
    930 	 *
    931 	 * Keeping sc->sc_state != idle terminates the recursion.
    932 	 */
    933 #ifdef	DIAGNOSTIC
    934 	if ((sc->sc_state & SUNSCPAL_WORKING) == 0)
    935 		panic("sunscpal_done: bad state");
    936 #endif
    937 
    938 	/* Clear our pointers to the request. */
    939 	sc->sc_current = NULL;
    940 	sc->sc_matrix[sr->sr_target][sr->sr_lun] = NULL;
    941 	callout_stop(&sr->sr_xs->xs_callout);
    942 
    943 	/* Make the request free. */
    944 	sr->sr_xs = NULL;
    945 	sc->sc_ncmds--;
    946 
    947 	/* Tell common SCSI code it is done. */
    948 	scsipi_done(xs);
    949 
    950 	sc->sc_state = SUNSCPAL_IDLE;
    951 	/* Now sunscpal_sched() may be called again. */
    952 }
    953 
    954 
    955 /*
    956  * Schedule a SCSI operation.  This routine should return
    957  * only after it achieves one of the following conditions:
    958  *  	Busy (sc->sc_state != SUNSCPAL_IDLE)
    959  *  	No more work can be started.
    960  */
    961 static void
    962 sunscpal_sched(sc)
    963 	struct	sunscpal_softc *sc;
    964 {
    965 	struct sunscpal_req	*sr;
    966 	struct scsipi_xfer *xs;
    967 	int	target = 0, lun = 0;
    968 	int	error, i;
    969 
    970 	/* Another hack (Er.. hook!) for anything that needs it: */
    971 	if (sc->sc_intr_off) {
    972 		SUNSCPAL_TRACE("sched: top, intr off\n", 0);
    973 	    sc->sc_intr_off(sc);
    974 	}
    975 
    976 next_job:
    977 	/*
    978 	 * Grab the next job from queue.  Must be idle.
    979 	 */
    980 #ifdef	DIAGNOSTIC
    981 	if (sc->sc_state != SUNSCPAL_IDLE)
    982 		panic("sunscpal_sched: not idle");
    983 	if (sc->sc_current)
    984 		panic("sunscpal_sched: current set");
    985 #endif
    986 
    987 	/*
    988 	 * Always start the search where we last looked.
    989 	 */
    990 	i = sc->sc_rr;
    991 	sr = NULL;
    992 	do {
    993 		if (sc->sc_ring[i].sr_xs) {
    994 			target = sc->sc_ring[i].sr_target;
    995 			lun = sc->sc_ring[i].sr_lun;
    996 			if (sc->sc_matrix[target][lun] == NULL) {
    997 				/*
    998 				 * Do not mark the  target/LUN busy yet,
    999 				 * because reselect may cause some other
   1000 				 * job to become the current one, so we
   1001 				 * might not actually start this job.
   1002 				 * Instead, set sc_matrix later on.
   1003 				 */
   1004 				sc->sc_rr = i;
   1005 				sr = &sc->sc_ring[i];
   1006 				break;
   1007 			}
   1008 		}
   1009 		i++;
   1010 		if (i == SUNSCPAL_OPENINGS)
   1011 			i = 0;
   1012 	} while (i != sc->sc_rr);
   1013 
   1014 	if (sr == NULL) {
   1015 		SUNSCPAL_TRACE("sched: no work, cur=0x%x\n",
   1016 				  (long) sc->sc_current);
   1017 
   1018 		/* Another hack (Er.. hook!) for anything that needs it: */
   1019 		if (sc->sc_intr_on) {
   1020 			SUNSCPAL_TRACE("sched: ret, intr ON\n", 0);
   1021 		    sc->sc_intr_on(sc);
   1022 		}
   1023 
   1024 		return;		/* No more work to do. */
   1025 	}
   1026 
   1027 	SUNSCPAL_TRACE("sched: select for t/l=0x%02x\n",
   1028 			  (sr->sr_target << 4) | sr->sr_lun);
   1029 
   1030 	sc->sc_state = SUNSCPAL_WORKING;
   1031 	error = sunscpal_select(sc, sr);
   1032 	if (sc->sc_current) {
   1033 		/* Lost the race!  reselected out from under us! */
   1034 		/* Work with the reselected job. */
   1035 		if (sr->sr_flags & SR_IMMED) {
   1036 			printf("%s: reselected while polling (abort)\n",
   1037 			    sc->sc_dev.dv_xname);
   1038 			/* Abort the reselected job. */
   1039 			sc->sc_state |= SUNSCPAL_ABORTING;
   1040 			sc->sc_msgpriq |= SEND_ABORT;
   1041 		}
   1042 		sr = sc->sc_current;
   1043 		xs = sr->sr_xs;
   1044 		SUNSCPAL_TRACE("sched: reselect, new sr=0x%x\n", (long)sr);
   1045 		goto have_nexus;
   1046 	}
   1047 
   1048 	/* Normal selection result.  Target/LUN is now busy. */
   1049 	sc->sc_matrix[target][lun] = sr;
   1050 	sc->sc_current = sr;	/* connected */
   1051 	xs = sr->sr_xs;
   1052 
   1053 	/*
   1054 	 * Initialize pointers, etc. for this job
   1055 	 */
   1056 	sc->sc_dataptr  = sr->sr_dataptr;
   1057 	sc->sc_datalen  = sr->sr_datalen;
   1058 	sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
   1059 	sc->sc_msgpriq = SEND_IDENTIFY;
   1060 	sc->sc_msgoutq = 0;
   1061 	sc->sc_msgout  = 0;
   1062 
   1063 	SUNSCPAL_TRACE("sched: select rv=%d\n", error);
   1064 
   1065 	switch (error) {
   1066 	case XS_NOERROR:
   1067 		break;
   1068 
   1069 	case XS_BUSY:
   1070 		/* XXX - Reset and try again. */
   1071 		printf("%s: select found SCSI bus busy, resetting...\n",
   1072 		    sc->sc_dev.dv_xname);
   1073 		sunscpal_reset_scsibus(sc);
   1074 		/* fallthrough */
   1075 	case XS_SELTIMEOUT:
   1076 	default:
   1077 		xs->error = error;	/* from select */
   1078 		SUNSCPAL_TRACE("sched: call done, sr=0x%x\n", (long)sr);
   1079 		sunscpal_done(sc);
   1080 
   1081 		/* Paranoia: clear everything. */
   1082 		sc->sc_dataptr = NULL;
   1083 		sc->sc_datalen = 0;
   1084 		sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
   1085 		sc->sc_msgpriq = 0;
   1086 		sc->sc_msgoutq = 0;
   1087 		sc->sc_msgout  = 0;
   1088 
   1089 		goto next_job;
   1090 	}
   1091 
   1092 	/*
   1093 	 * Selection was successful.  Normally, this means
   1094 	 * we are starting a new command.  However, this
   1095 	 * might be the termination of an overdue job.
   1096 	 */
   1097 	if (sr->sr_flags & SR_OVERDUE) {
   1098 		SUNSCPAL_TRACE("sched: overdue, sr=0x%x\n", (long)sr);
   1099 		sc->sc_state |= SUNSCPAL_ABORTING;
   1100 		sc->sc_msgpriq |= SEND_ABORT;
   1101 		goto have_nexus;
   1102 	}
   1103 
   1104 	/*
   1105 	 * OK, we are starting a new command.
   1106 	 * Initialize and allocate resources for the new command.
   1107 	 * Device reset is special (only uses MSG_OUT phase).
   1108 	 * Normal commands start in MSG_OUT phase where we will
   1109 	 * send and IDENDIFY message, and then expect CMD phase.
   1110 	 */
   1111 #ifdef	SUNSCPAL_DEBUG
   1112 	if (sunscpal_debug & SUNSCPAL_DBG_CMDS) {
   1113 		printf("sunscpal_sched: begin, target=%d, LUN=%d\n",
   1114 		    xs->xs_periph->periph_target, xs->xs_periph->periph_lun);
   1115 		sunscpal_show_scsi_cmd(xs);
   1116 	}
   1117 #endif
   1118 	if (xs->xs_control & XS_CTL_RESET) {
   1119 		SUNSCPAL_TRACE("sched: cmd=reset, sr=0x%x\n", (long)sr);
   1120 		/* Not an error, so do not set SUNSCPAL_ABORTING */
   1121 		sc->sc_msgpriq |= SEND_DEV_RESET;
   1122 		goto have_nexus;
   1123 	}
   1124 
   1125 #ifdef	DIAGNOSTIC
   1126 	if ((xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) == 0) {
   1127 		if (sc->sc_dataptr) {
   1128 			printf("%s: ptr but no data in/out flags?\n",
   1129 			    sc->sc_dev.dv_xname);
   1130 			SUNSCPAL_BREAK();
   1131 			sc->sc_dataptr = NULL;
   1132 		}
   1133 	}
   1134 #endif
   1135 
   1136 	/* Allocate DMA space (maybe) */
   1137 	if (sc->sc_dataptr && (sc->sc_flags & SUNSCPAL_DISABLE_DMA) == 0 &&
   1138 		(sc->sc_datalen >= sc->sc_min_dma_len))
   1139 	{
   1140 		SUNSCPAL_TRACE("sched: dma_alloc, len=%d\n", sc->sc_datalen);
   1141 		sunscpal_dma_alloc(sc);
   1142 	}
   1143 
   1144 	/*
   1145 	 * Initialization hook called just after select,
   1146 	 * at the beginning of COMMAND phase.
   1147 	 * (but AFTER the DMA allocation is done)
   1148 	 *
   1149 	 * We need to set up the DMA engine BEFORE the target puts
   1150 	 * the SCSI bus into any DATA phase.
   1151 	 */
   1152 	if (sr->sr_dma_hand) {
   1153 		SUNSCPAL_TRACE("sched: dma_setup, dh=0x%x\n",
   1154 				  (long) sr->sr_dma_hand);
   1155 	    sunscpal_dma_setup(sc);
   1156 	}
   1157 
   1158 	/*
   1159 	 * Schedule a timeout for the job we are starting.
   1160 	 */
   1161 	if ((sr->sr_flags & SR_IMMED) == 0) {
   1162 		i = mstohz(xs->timeout);
   1163 		SUNSCPAL_TRACE("sched: set timeout=%d\n", i);
   1164 		callout_reset(&sr->sr_xs->xs_callout, i,
   1165 		    sunscpal_cmd_timeout, sr);
   1166 	}
   1167 
   1168 have_nexus:
   1169 
   1170 	SUNSCPAL_TRACE("sched: call machine, cur=0x%x\n",
   1171 			  (long) sc->sc_current);
   1172 	sunscpal_machine(sc);
   1173 	SUNSCPAL_TRACE("sched: machine done, cur=0x%x\n",
   1174 			  (long) sc->sc_current);
   1175 
   1176 	/*
   1177 	 * What state did sunscpal_machine() leave us in?
   1178 	 * Hopefully it sometimes completes a job...
   1179 	 */
   1180 	if (sc->sc_state == SUNSCPAL_IDLE)
   1181 		goto next_job;
   1182 
   1183 	return; 	/* Have work in progress. */
   1184 }
   1185 
   1186 
   1187 /*
   1188  *  Reselect handler: checks for reselection, and if we are being
   1189  *	reselected, it sets up sc->sc_current.
   1190  *
   1191  *  We are reselected when:
   1192  *	SEL is TRUE
   1193  *	IO  is TRUE
   1194  *	BSY is FALSE
   1195  */
   1196 void
   1197 sunscpal_reselect(sc)
   1198 	struct sunscpal_softc *sc;
   1199 {
   1200 	/*
   1201 	 * This controller does not implement disconnect/reselect, so
   1202 	 * we really don't have anything to do here.  We keep this
   1203 	 * function as a placeholder, though.
   1204 	 */
   1205 }
   1206 
   1207 /*
   1208  *  Select target: xs is the transfer that we are selecting for.
   1209  *  sc->sc_current should be NULL.
   1210  *
   1211  *  Returns:
   1212  *	sc->sc_current != NULL  ==> we were reselected (race!)
   1213  *	XS_NOERROR		==> selection worked
   1214  *	XS_BUSY 		==> lost arbitration
   1215  *	XS_SELTIMEOUT   	==> no response to selection
   1216  */
   1217 static int
   1218 sunscpal_select(sc, sr)
   1219 	struct sunscpal_softc *sc;
   1220 	struct sunscpal_req *sr;
   1221 {
   1222 	int timo, target_mask;
   1223 	u_short	mode;
   1224 
   1225 	/* Check for reselect */
   1226 	sunscpal_reselect(sc);
   1227 	if (sc->sc_current) {
   1228 		SUNSCPAL_TRACE("select: reselect, cur=0x%x\n",
   1229 				  (long) sc->sc_current);
   1230 		return XS_BUSY;	/* reselected */
   1231 	}
   1232 
   1233 	/*
   1234 	 * Select the target.
   1235 	 */
   1236 	target_mask = (1 << sr->sr_target);
   1237 	SUNSCPAL_WRITE_1(sc, sunscpal_data, target_mask);
   1238 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, SUNSCPAL_ICR_SELECT);
   1239 
   1240 	/*
   1241 	 * Wait for the target to assert BSY.
   1242 	 * SCSI spec. says wait for 250 mS.
   1243 	 */
   1244 	for (timo = 25000;;) {
   1245 		if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_BUSY)
   1246 			goto success;
   1247 		if (--timo <= 0)
   1248 			break;
   1249 		delay(10);
   1250 	}
   1251 
   1252 	SUNSCPAL_WRITE_1(sc, sunscpal_data, 0);
   1253 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
   1254 
   1255 	SUNSCPAL_TRACE("select: device down, rc=%d\n", XS_SELTIMEOUT);
   1256 	return XS_SELTIMEOUT;
   1257 
   1258 success:
   1259 
   1260 	/*
   1261 	 * The target is now driving BSY, so we can stop
   1262 	 * driving SEL and the data bus.  We do set up
   1263 	 * whether or not this target needs parity.
   1264 	 */
   1265 	mode = 0;
   1266 	if ((sc->sc_parity_disable & target_mask) == 0)
   1267 		mode |= SUNSCPAL_ICR_PARITY_ENABLE;
   1268 	SUNSCPAL_WRITE_2(sc, sunscpal_icr, mode);
   1269 
   1270 	return XS_NOERROR;
   1271 }
   1272 
   1273 /*****************************************************************
   1274  * Functions to handle each info. transfer phase:
   1275  *****************************************************************/
   1276 
   1277 /*
   1278  * The message system:
   1279  *
   1280  * This is a revamped message system that now should easier accomodate
   1281  * new messages, if necessary.
   1282  *
   1283  * Currently we accept these messages:
   1284  * IDENTIFY (when reselecting)
   1285  * COMMAND COMPLETE # (expect bus free after messages marked #)
   1286  * NOOP
   1287  * MESSAGE REJECT
   1288  * SYNCHRONOUS DATA TRANSFER REQUEST
   1289  * SAVE DATA POINTER
   1290  * RESTORE POINTERS
   1291  * DISCONNECT #
   1292  *
   1293  * We may send these messages in prioritized order:
   1294  * BUS DEVICE RESET #		if XS_CTL_RESET & xs->xs_control (or in
   1295  *				weird sits.)
   1296  * MESSAGE PARITY ERROR		par. err. during MSGI
   1297  * MESSAGE REJECT		If we get a message we don't know how to handle
   1298  * ABORT #			send on errors
   1299  * INITIATOR DETECTED ERROR	also on errors (SCSI2) (during info xfer)
   1300  * IDENTIFY			At the start of each transfer
   1301  * SYNCHRONOUS DATA TRANSFER REQUEST	if appropriate
   1302  * NOOP				if nothing else fits the bill ...
   1303  */
   1304 
   1305 /*
   1306  * Precondition:
   1307  * The SCSI bus is already in the MSGI phase and there is a message byte
   1308  * on the bus, along with an asserted REQ signal.
   1309  *
   1310  * Our return value determines whether our caller, sunscpal_machine()
   1311  * will expect to see another REQ (and possibly phase change).
   1312  */
   1313 static int
   1314 sunscpal_msg_in(sc)
   1315 	struct sunscpal_softc *sc;
   1316 {
   1317 	struct sunscpal_req *sr = sc->sc_current;
   1318 	struct scsipi_xfer *xs = sr->sr_xs;
   1319 	int n, phase;
   1320 	int act_flags;
   1321 
   1322 	act_flags = ACT_CONTINUE;
   1323 
   1324 	if (sc->sc_prevphase == SUNSCPAL_PHASE_MSG_IN) {
   1325 		/* This is a continuation of the previous message. */
   1326 		n = sc->sc_imp - sc->sc_imess;
   1327 		SUNSCPAL_TRACE("msg_in: continuation, n=%d\n", n);
   1328 		goto nextbyte;
   1329 	}
   1330 
   1331 	/* This is a new MESSAGE IN phase.  Clean up our state. */
   1332 	sc->sc_state &= ~SUNSCPAL_DROP_MSGIN;
   1333 
   1334 nextmsg:
   1335 	n = 0;
   1336 	sc->sc_imp = &sc->sc_imess[n];
   1337 
   1338 nextbyte:
   1339 	/*
   1340 	 * Read a whole message, but don't ack the last byte.  If we reject the
   1341 	 * message, we have to assert ATN during the message transfer phase
   1342 	 * itself.
   1343 	 */
   1344 	for (;;) {
   1345 		/*
   1346 		 * Read a message byte.
   1347 		 * First, check BSY, REQ, phase...
   1348 		 */
   1349 		if (!SUNSCPAL_BUSY(sc)) {
   1350 			SUNSCPAL_TRACE("msg_in: lost BSY, n=%d\n", n);
   1351 			/* XXX - Assume the command completed? */
   1352 			act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
   1353 			return (act_flags);
   1354 		}
   1355 		if (sunscpal_wait_req(sc)) {
   1356 			SUNSCPAL_TRACE("msg_in: BSY but no REQ, n=%d\n", n);
   1357 			/* Just let sunscpal_machine() handle it... */
   1358 			return (act_flags);
   1359 		}
   1360 		phase = SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr));
   1361 		if (phase != SUNSCPAL_PHASE_MSG_IN) {
   1362 			/*
   1363 			 * Target left MESSAGE IN, probably because it
   1364 			 * a) noticed our ATN signal, or
   1365 			 * b) ran out of messages.
   1366 			 */
   1367 			return (act_flags);
   1368 		}
   1369 		/* Still in MESSAGE IN phase, and REQ is asserted. */
   1370 		if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_PARITY_ERROR) {
   1371 			sunscpal_sched_msgout(sc, SEND_PARITY_ERROR);
   1372 			sc->sc_state |= SUNSCPAL_DROP_MSGIN;
   1373 		}
   1374 
   1375 		/* Gather incoming message bytes if needed. */
   1376 		if ((sc->sc_state & SUNSCPAL_DROP_MSGIN) == 0) {
   1377 			if (n >= SUNSCPAL_MAX_MSG_LEN) {
   1378 				sunscpal_sched_msgout(sc, SEND_REJECT);
   1379 				sc->sc_state |= SUNSCPAL_DROP_MSGIN;
   1380 			} else {
   1381 				*sc->sc_imp++ = SUNSCPAL_READ_1(sc, sunscpal_cmd_stat);
   1382 				n++;
   1383 				/*
   1384 				 * This testing is suboptimal, but most
   1385 				 * messages will be of the one byte variety, so
   1386 				 * it should not affect performance
   1387 				 * significantly.
   1388 				 */
   1389 				if (n == 1 && MSG_IS1BYTE(sc->sc_imess[0]))
   1390 					goto have_msg;
   1391 				if (n == 2 && MSG_IS2BYTE(sc->sc_imess[0]))
   1392 					goto have_msg;
   1393 				if (n >= 3 && MSG_ISEXTENDED(sc->sc_imess[0]) &&
   1394 					n == sc->sc_imess[1] + 2)
   1395 					goto have_msg;
   1396 			}
   1397 		}
   1398 
   1399 		/*
   1400 		 * If we reach this spot we're either:
   1401 		 * a) in the middle of a multi-byte message, or
   1402 		 * b) dropping bytes.
   1403 		 */
   1404 
   1405 		if (act_flags != ACT_CONTINUE)
   1406 			return (act_flags);
   1407 
   1408 		/* back to nextbyte */
   1409 	}
   1410 
   1411 have_msg:
   1412 	/* We now have a complete message.  Parse it. */
   1413 
   1414 	switch (sc->sc_imess[0]) {
   1415 	case MSG_CMDCOMPLETE:
   1416 		SUNSCPAL_TRACE("msg_in: CMDCOMPLETE\n", 0);
   1417 		/* Target is about to disconnect. */
   1418 		act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
   1419 		break;
   1420 
   1421 	case MSG_PARITY_ERROR:
   1422 		SUNSCPAL_TRACE("msg_in: PARITY_ERROR\n", 0);
   1423 		/* Resend the last message. */
   1424 		sunscpal_sched_msgout(sc, sc->sc_msgout);
   1425 		break;
   1426 
   1427 	case MSG_MESSAGE_REJECT:
   1428 		/* The target rejects the last message we sent. */
   1429 		SUNSCPAL_TRACE("msg_in: got reject for 0x%x\n", sc->sc_msgout);
   1430 		switch (sc->sc_msgout) {
   1431 		case SEND_IDENTIFY:
   1432 			/* Really old target controller? */
   1433 			/* XXX ... */
   1434 			break;
   1435 		case SEND_INIT_DET_ERR:
   1436 			goto abort;
   1437 		}
   1438 		break;
   1439 
   1440 	case MSG_NOOP:
   1441 		SUNSCPAL_TRACE("msg_in: NOOP\n", 0);
   1442 		break;
   1443 
   1444 	case MSG_DISCONNECT:
   1445 		SUNSCPAL_TRACE("msg_in: DISCONNECT\n", 0);
   1446 		/* Target is about to disconnect. */
   1447 		act_flags |= ACT_DISCONNECT;
   1448 		if ((xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0)
   1449 			break;
   1450 		/*FALLTHROUGH*/
   1451 
   1452 	case MSG_SAVEDATAPOINTER:
   1453 		SUNSCPAL_TRACE("msg_in: SAVE_PTRS\n", 0);
   1454 		sr->sr_dataptr = sc->sc_dataptr;
   1455 		sr->sr_datalen = sc->sc_datalen;
   1456 		break;
   1457 
   1458 	case MSG_RESTOREPOINTERS:
   1459 		SUNSCPAL_TRACE("msg_in: RESTORE_PTRS\n", 0);
   1460 		sc->sc_dataptr = sr->sr_dataptr;
   1461 		sc->sc_datalen = sr->sr_datalen;
   1462 		break;
   1463 
   1464 	case MSG_EXTENDED:
   1465 		switch (sc->sc_imess[2]) {
   1466 		case MSG_EXT_SDTR:
   1467 		case MSG_EXT_WDTR:
   1468 			/* The ncr5380 can not do synchronous mode. */
   1469 			goto reject;
   1470 		default:
   1471 			printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
   1472 			    sc->sc_dev.dv_xname);
   1473 			SUNSCPAL_BREAK();
   1474 			goto reject;
   1475 		}
   1476 		break;
   1477 
   1478 	default:
   1479 		SUNSCPAL_TRACE("msg_in: eh? imsg=0x%x\n", sc->sc_imess[0]);
   1480 		printf("%s: unrecognized MESSAGE; sending REJECT\n",
   1481 		    sc->sc_dev.dv_xname);
   1482 		SUNSCPAL_BREAK();
   1483 		/* fallthrough */
   1484 	reject:
   1485 		sunscpal_sched_msgout(sc, SEND_REJECT);
   1486 		break;
   1487 
   1488 	abort:
   1489 		sc->sc_state |= SUNSCPAL_ABORTING;
   1490 		sunscpal_sched_msgout(sc, SEND_ABORT);
   1491 		break;
   1492 	}
   1493 
   1494 	/* Go get the next message, if any. */
   1495 	if (act_flags == ACT_CONTINUE)
   1496 		goto nextmsg;
   1497 
   1498 	return (act_flags);
   1499 }
   1500 
   1501 
   1502 /*
   1503  * The message out (and in) stuff is a bit complicated:
   1504  * If the target requests another message (sequence) without
   1505  * having changed phase in between it really asks for a
   1506  * retransmit, probably due to parity error(s).
   1507  * The following messages can be sent:
   1508  * IDENTIFY	   @ These 4 stem from SCSI command activity
   1509  * SDTR		   @
   1510  * WDTR		   @
   1511  * DEV_RESET	   @
   1512  * REJECT if MSGI doesn't make sense
   1513  * PARITY_ERROR if parity error while in MSGI
   1514  * INIT_DET_ERR if parity error while not in MSGI
   1515  * ABORT if INIT_DET_ERR rejected
   1516  * NOOP if asked for a message and there's nothing to send
   1517  *
   1518  * Note that we call this one with (sc_current == NULL)
   1519  * when sending ABORT for unwanted reselections.
   1520  */
   1521 static int
   1522 sunscpal_msg_out(sc)
   1523 	struct sunscpal_softc *sc;
   1524 {
   1525 	/*
   1526 	 * This controller does not allow you to assert ATN, which
   1527 	 * means we will never get the opportunity to send messages to
   1528 	 * the target (the bus will never enter this MSG_OUT phase).
   1529 	 * This will eventually leave us with no option other than to
   1530 	 * reset the bus.  We keep this function as a placeholder,
   1531 	 * though, and this printf will eventually go away or get
   1532 	 * #ifdef'ed:
   1533 	 */
   1534 	printf("sunscpal_msg_out: bus is in MSG_OUT phase?\n");
   1535 	return (ACT_CONTINUE | ACT_RESET_BUS);
   1536 }
   1537 
   1538 /*
   1539  * Handle command phase.
   1540  */
   1541 static int
   1542 sunscpal_command(sc)
   1543 	struct sunscpal_softc *sc;
   1544 {
   1545 	struct sunscpal_req *sr = sc->sc_current;
   1546 	struct scsipi_xfer *xs = sr->sr_xs;
   1547 	int len;
   1548 
   1549 	/* Assume command can be sent in one go. */
   1550 	/* XXX: Do this using DMA, and get a phase change intr? */
   1551 	len = sunscpal_pio_out(sc, SUNSCPAL_PHASE_COMMAND, xs->cmdlen,
   1552 		(u_char *)xs->cmd);
   1553 
   1554 	if (len != xs->cmdlen) {
   1555 #ifdef	SUNSCPAL_DEBUG
   1556 		printf("sunscpal_command: short transfer: wanted %d got %d.\n",
   1557 		    xs->cmdlen, len);
   1558 		sunscpal_show_scsi_cmd(xs);
   1559 		SUNSCPAL_BREAK();
   1560 #endif
   1561 		if (len < 6) {
   1562 			xs->error = XS_DRIVER_STUFFUP;
   1563 			sc->sc_state |= SUNSCPAL_ABORTING;
   1564 			sunscpal_sched_msgout(sc, SEND_ABORT);
   1565 		}
   1566 
   1567 	}
   1568 
   1569 	return ACT_CONTINUE;
   1570 }
   1571 
   1572 
   1573 /*
   1574  * Handle either data_in or data_out
   1575  */
   1576 static int
   1577 sunscpal_data_xfer(sc, phase)
   1578 	struct sunscpal_softc *sc;
   1579 	int phase;
   1580 {
   1581 	struct sunscpal_req *sr = sc->sc_current;
   1582 	struct scsipi_xfer *xs = sr->sr_xs;
   1583 	int expected_phase;
   1584 	int len;
   1585 
   1586 	/*
   1587 	 * When aborting a command, disallow any data phase.
   1588 	 */
   1589 	if (sc->sc_state & SUNSCPAL_ABORTING) {
   1590 		printf("%s: aborting, bus phase=%s (reset)\n",
   1591 		    sc->sc_dev.dv_xname, phase_names[(phase >> 8) & 7]);
   1592 		return ACT_RESET_BUS;	/* XXX */
   1593 	}
   1594 
   1595 	/* Validate expected phase (data_in or data_out) */
   1596 	expected_phase = (xs->xs_control & XS_CTL_DATA_OUT) ?
   1597 		SUNSCPAL_PHASE_DATA_OUT : SUNSCPAL_PHASE_DATA_IN;
   1598 	if (phase != expected_phase) {
   1599 		printf("%s: data phase error\n", sc->sc_dev.dv_xname);
   1600 		goto abort;
   1601 	}
   1602 
   1603 	/* Make sure we have some data to move. */
   1604 	if (sc->sc_datalen <= 0) {
   1605 		/* Device needs padding. */
   1606 		if (phase == SUNSCPAL_PHASE_DATA_IN)
   1607 			sunscpal_pio_in(sc, phase, 4096, NULL);
   1608 		else
   1609 			sunscpal_pio_out(sc, phase, 4096, NULL);
   1610 		/* Make sure that caused a phase change. */
   1611 		if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) == phase) {
   1612 			/* More than 4k is just too much! */
   1613 			printf("%s: too much data padding\n",
   1614 				sc->sc_dev.dv_xname);
   1615 			goto abort;
   1616 		}
   1617 		return ACT_CONTINUE;
   1618 	}
   1619 
   1620 	/*
   1621 	 * Attempt DMA only if dma_alloc gave us a DMA handle AND
   1622 	 * there is enough left to transfer so DMA is worth while.
   1623 	 */
   1624 	if (sr->sr_dma_hand &&
   1625 		(sc->sc_datalen >= sc->sc_min_dma_len))
   1626 	{
   1627 		/*
   1628 		 * OK, really start DMA.  Note, the MD start function
   1629 		 * is responsible for setting the TCMD register, etc.
   1630 		 * (Acknowledge the phase change there, not here.)
   1631 		 */
   1632 		SUNSCPAL_TRACE("data_xfer: dma_start, dh=0x%x\n",
   1633 		          (long) sr->sr_dma_hand);
   1634 		sunscpal_dma_start(sc);
   1635 		return ACT_WAIT_DMA;
   1636 	}
   1637 
   1638 	/*
   1639 	 * Doing PIO for data transfer.  (Possibly "Pseudo DMA")
   1640 	 * XXX:  Do PDMA functions need to set tcmd later?
   1641 	 */
   1642 	SUNSCPAL_TRACE("data_xfer: doing PIO, len=%d\n", sc->sc_datalen);
   1643 	if (phase == SUNSCPAL_PHASE_DATA_OUT) {
   1644 		len = sunscpal_pio_out(sc, phase, sc->sc_datalen, sc->sc_dataptr);
   1645 	} else {
   1646 		len = sunscpal_pio_in(sc, phase, sc->sc_datalen, sc->sc_dataptr);
   1647 	}
   1648 	sc->sc_dataptr += len;
   1649 	sc->sc_datalen -= len;
   1650 
   1651 	SUNSCPAL_TRACE("data_xfer: did PIO, resid=%d\n", sc->sc_datalen);
   1652 	return (ACT_CONTINUE);
   1653 
   1654 abort:
   1655 	sc->sc_state |= SUNSCPAL_ABORTING;
   1656 	sunscpal_sched_msgout(sc, SEND_ABORT);
   1657 	return (ACT_CONTINUE);
   1658 }
   1659 
   1660 
   1661 static int
   1662 sunscpal_status(sc)
   1663 	struct sunscpal_softc *sc;
   1664 {
   1665 	int len;
   1666 	u_char status;
   1667 	struct sunscpal_req *sr = sc->sc_current;
   1668 
   1669 	len = sunscpal_pio_in(sc, SUNSCPAL_PHASE_STATUS, 1, &status);
   1670 	if (len) {
   1671 		sr->sr_status = status;
   1672 	} else {
   1673 		printf("sunscpal_status: none?\n");
   1674 	}
   1675 
   1676 	return ACT_CONTINUE;
   1677 }
   1678 
   1679 
   1680 /*
   1681  * This is the big state machine that follows SCSI phase changes.
   1682  * This is somewhat like a co-routine.  It will do a SCSI command,
   1683  * and exit if the command is complete, or if it must wait, i.e.
   1684  * for DMA to complete or for reselect to resume the job.
   1685  *
   1686  * The bus must be selected, and we need to know which command is
   1687  * being undertaken.
   1688  */
   1689 static void
   1690 sunscpal_machine(sc)
   1691 	struct sunscpal_softc *sc;
   1692 {
   1693 	struct sunscpal_req *sr;
   1694 	struct scsipi_xfer *xs;
   1695 	int act_flags, phase, timo;
   1696 
   1697 #ifdef	DIAGNOSTIC
   1698 	if (sc->sc_state == SUNSCPAL_IDLE)
   1699 		panic("sunscpal_machine: state=idle");
   1700 	if (sc->sc_current == NULL)
   1701 		panic("sunscpal_machine: no current cmd");
   1702 #endif
   1703 
   1704 	sr = sc->sc_current;
   1705 	xs = sr->sr_xs;
   1706 	act_flags = ACT_CONTINUE;
   1707 
   1708 	/*
   1709 	 * This will be called by sunscpal_intr() when DMA is
   1710 	 * complete.  Must stop DMA before touching the PAL or
   1711 	 * there will be "register conflict" errors.
   1712 	 */
   1713 	if (sc->sc_state & SUNSCPAL_DOINGDMA) {
   1714 		/* Pick-up where where we left off... */
   1715 		goto dma_done;
   1716 	}
   1717 
   1718 next_phase:
   1719 
   1720 	if (!SUNSCPAL_BUSY(sc)) {
   1721 		/* Unexpected disconnect */
   1722 		printf("sunscpal_machine: unexpected disconnect.\n");
   1723 		xs->error = XS_DRIVER_STUFFUP;
   1724 		act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
   1725 		goto do_actions;
   1726 	}
   1727 
   1728 	/*
   1729 	 * Wait for REQ before reading the phase.
   1730 	 * Need to wait longer than usual here, because
   1731 	 * some devices are just plain slow...
   1732 	 */
   1733 	timo = sunscpal_wait_phase_timo;
   1734 	for (;;) {
   1735 		if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_REQUEST)
   1736 			break;
   1737 		if (--timo <= 0) {
   1738 			if (sc->sc_state & SUNSCPAL_ABORTING) {
   1739 				printf("%s: no REQ while aborting, reset\n",
   1740 				    sc->sc_dev.dv_xname);
   1741 				act_flags |= ACT_RESET_BUS;
   1742 				goto do_actions;
   1743 			}
   1744 			printf("%s: no REQ for next phase, abort\n",
   1745 			    sc->sc_dev.dv_xname);
   1746 			sc->sc_state |= SUNSCPAL_ABORTING;
   1747 			sunscpal_sched_msgout(sc, SEND_ABORT);
   1748 			goto next_phase;
   1749 		}
   1750 		delay(100);
   1751 	}
   1752 
   1753 	phase = SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr));
   1754 	SUNSCPAL_TRACE("machine: phase=%s\n",
   1755 			  (long) phase_names[(phase >> 8) & 7]);
   1756 
   1757 	/*
   1758 	 * We assume that the device knows what it's doing,
   1759 	 * so any phase is good.
   1760 	 */
   1761 
   1762 	switch (phase) {
   1763 
   1764 	case SUNSCPAL_PHASE_DATA_OUT:
   1765 	case SUNSCPAL_PHASE_DATA_IN:
   1766 		act_flags = sunscpal_data_xfer(sc, phase);
   1767 		break;
   1768 
   1769 	case SUNSCPAL_PHASE_COMMAND:
   1770 		act_flags = sunscpal_command(sc);
   1771 		break;
   1772 
   1773 	case SUNSCPAL_PHASE_STATUS:
   1774 		act_flags = sunscpal_status(sc);
   1775 		break;
   1776 
   1777 	case SUNSCPAL_PHASE_MSG_OUT:
   1778 		act_flags = sunscpal_msg_out(sc);
   1779 		break;
   1780 
   1781 	case SUNSCPAL_PHASE_MSG_IN:
   1782 		act_flags = sunscpal_msg_in(sc);
   1783 		break;
   1784 
   1785 	default:
   1786 		printf("sunscpal_machine: Unexpected phase 0x%x\n", phase);
   1787 		sc->sc_state |= SUNSCPAL_ABORTING;
   1788 		sunscpal_sched_msgout(sc, SEND_ABORT);
   1789 		goto next_phase;
   1790 
   1791 	} /* switch */
   1792 	sc->sc_prevphase = phase;
   1793 
   1794 do_actions:
   1795 
   1796 	if (act_flags & ACT_WAIT_DMA) {
   1797 		act_flags &= ~ACT_WAIT_DMA;
   1798 		/* Wait for DMA to complete (polling, or interrupt). */
   1799 		if ((sr->sr_flags & SR_IMMED) == 0) {
   1800 			SUNSCPAL_TRACE("machine: wait for DMA intr.\n", 0);
   1801 			return; 	/* will resume at dma_done */
   1802 		}
   1803 		/* Busy-wait for it to finish. */
   1804 		SUNSCPAL_TRACE("machine: dma_poll, dh=0x%x\n",
   1805 				  (long) sr->sr_dma_hand);
   1806 		sunscpal_dma_poll(sc);
   1807 	dma_done:
   1808 		/* Return here after interrupt. */
   1809 		if (sr->sr_flags & SR_OVERDUE)
   1810 			sc->sc_state |= SUNSCPAL_ABORTING;
   1811 		SUNSCPAL_TRACE("machine: dma_stop, dh=0x%x\n",
   1812 				  (long) sr->sr_dma_hand);
   1813 		sunscpal_dma_stop(sc);
   1814 		SUNSCPAL_CLR_INTR(sc);	/* XXX */
   1815 		/*
   1816 		 * While DMA is running we can not touch the SBC,
   1817 		 * so various places just set SUNSCPAL_ABORTING and
   1818 		 * expect us the "kick it" when DMA is done.
   1819 		 */
   1820 		if (sc->sc_state & SUNSCPAL_ABORTING) {
   1821 			sunscpal_sched_msgout(sc, SEND_ABORT);
   1822 		}
   1823 	}
   1824 
   1825 	/*
   1826 	 * Check for parity error.
   1827 	 * XXX - better place to check?
   1828 	 */
   1829 	if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_PARITY_ERROR) {
   1830 		printf("%s: parity error!\n", sc->sc_dev.dv_xname);
   1831 		/* XXX: sc->sc_state |= SUNSCPAL_ABORTING; */
   1832 		sunscpal_sched_msgout(sc, SEND_PARITY_ERROR);
   1833 	}
   1834 
   1835 	if (act_flags == ACT_CONTINUE)
   1836 		goto next_phase;
   1837 	/* All other actions "break" from the loop. */
   1838 
   1839 	SUNSCPAL_TRACE("machine: act_flags=0x%x\n", act_flags);
   1840 
   1841 	if (act_flags & ACT_RESET_BUS) {
   1842 		act_flags |= ACT_CMD_DONE;
   1843 		/*
   1844 		 * Reset the SCSI bus, usually due to a timeout.
   1845 		 * The error code XS_TIMEOUT allows retries.
   1846 		 */
   1847 		sc->sc_state |= SUNSCPAL_ABORTING;
   1848 		printf("%s: reset SCSI bus for TID=%d LUN=%d\n",
   1849 		    sc->sc_dev.dv_xname, sr->sr_target, sr->sr_lun);
   1850 		sunscpal_reset_scsibus(sc);
   1851 	}
   1852 
   1853 	if (act_flags & ACT_CMD_DONE) {
   1854 		act_flags |= ACT_DISCONNECT;
   1855 		/* Need to call scsipi_done() */
   1856 		/* XXX: from the aic6360 driver, but why? */
   1857 		if (sc->sc_datalen < 0) {
   1858 			printf("%s: %d extra bytes from %d:%d\n",
   1859 			    sc->sc_dev.dv_xname, -sc->sc_datalen,
   1860 			    sr->sr_target, sr->sr_lun);
   1861 			sc->sc_datalen = 0;
   1862 		}
   1863 		xs->resid = sc->sc_datalen;
   1864 		/* Note: this will clear sc_current */
   1865 		SUNSCPAL_TRACE("machine: call done, cur=0x%x\n", (long)sr);
   1866 		sunscpal_done(sc);
   1867 	}
   1868 
   1869 	if (act_flags & ACT_DISCONNECT) {
   1870 		/*
   1871 		 * The device has dropped BSY (or will soon).
   1872 		 * We have to wait here for BSY to drop, otherwise
   1873 		 * the next command may decide we need a bus reset.
   1874 		 */
   1875 		timo = sunscpal_wait_req_timo;	/* XXX */
   1876 		for (;;) {
   1877 			if (!SUNSCPAL_BUSY(sc))
   1878 				goto busfree;
   1879 			if (--timo <= 0)
   1880 				break;
   1881 			delay(2);
   1882 		}
   1883 		/* Device is sitting on the bus! */
   1884 		printf("%s: Target %d LUN %d stuck busy, resetting...\n",
   1885 		    sc->sc_dev.dv_xname, sr->sr_target, sr->sr_lun);
   1886 		sunscpal_reset_scsibus(sc);
   1887 	busfree:
   1888 		SUNSCPAL_TRACE("machine: discon, waited %d\n",
   1889 			sunscpal_wait_req_timo - timo);
   1890 
   1891 		SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
   1892 
   1893 		if ((act_flags & ACT_CMD_DONE) == 0) {
   1894 			SUNSCPAL_TRACE("machine: discon, cur=0x%x\n", (long)sr);
   1895 		}
   1896 
   1897 		/*
   1898 		 * We may be here due to a disconnect message,
   1899 		 * in which case we did NOT call sunscpal_done,
   1900 		 * and we need to clear sc_current.
   1901 		 */
   1902 		sc->sc_state = SUNSCPAL_IDLE;
   1903 		sc->sc_current = NULL;
   1904 
   1905 		/* Paranoia: clear everything. */
   1906 		sc->sc_dataptr = NULL;
   1907 		sc->sc_datalen = 0;
   1908 		sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
   1909 		sc->sc_msgpriq = 0;
   1910 		sc->sc_msgoutq = 0;
   1911 		sc->sc_msgout  = 0;
   1912 
   1913 		/* Our caller will re-enable interrupts. */
   1914 	}
   1915 }
   1916 
   1917 
   1918 #ifdef	SUNSCPAL_DEBUG
   1919 
   1920 static void
   1921 sunscpal_show_scsi_cmd(xs)
   1922 	struct scsipi_xfer *xs;
   1923 {
   1924 	u_char	*b = (u_char *) xs->cmd;
   1925 	int	i  = 0;
   1926 
   1927 	scsipi_printaddr(xs->xs_periph);
   1928 	if ( ! ( xs->xs_control & XS_CTL_RESET ) ) {
   1929 		printf("-");
   1930 		while (i < xs->cmdlen) {
   1931 			if (i) printf(",");
   1932 			printf("%x",b[i++]);
   1933 		}
   1934 		printf("-\n");
   1935 	} else {
   1936 
   1937 		printf("-RESET-\n");
   1938 	}
   1939 }
   1940 
   1941 
   1942 int sunscpal_traceidx = 0;
   1943 
   1944 #define	TRACE_MAX	1024
   1945 struct trace_ent {
   1946 	char *msg;
   1947 	long  val;
   1948 } sunscpal_tracebuf[TRACE_MAX];
   1949 
   1950 void
   1951 sunscpal_trace(msg, val)
   1952 	char *msg;
   1953 	long  val;
   1954 {
   1955 	struct trace_ent *tr;
   1956 	int s;
   1957 
   1958 	s = splbio();
   1959 
   1960 	tr = &sunscpal_tracebuf[sunscpal_traceidx];
   1961 
   1962 	sunscpal_traceidx++;
   1963 	if (sunscpal_traceidx >= TRACE_MAX)
   1964 		sunscpal_traceidx = 0;
   1965 
   1966 	tr->msg = msg;
   1967 	tr->val = val;
   1968 
   1969 	splx(s);
   1970 }
   1971 
   1972 #ifdef	DDB
   1973 void
   1974 sunscpal_clear_trace()
   1975 {
   1976 	sunscpal_traceidx = 0;
   1977 	memset((char*) sunscpal_tracebuf, 0, sizeof(sunscpal_tracebuf));
   1978 }
   1979 
   1980 void
   1981 sunscpal_show_trace()
   1982 {
   1983 	struct trace_ent *tr;
   1984 	int idx;
   1985 
   1986 	idx = sunscpal_traceidx;
   1987 	do {
   1988 		tr = &sunscpal_tracebuf[idx];
   1989 		idx++;
   1990 		if (idx >= TRACE_MAX)
   1991 			idx = 0;
   1992 		if (tr->msg)
   1993 			db_printf(tr->msg, tr->val);
   1994 	} while (idx != sunscpal_traceidx);
   1995 }
   1996 
   1997 void
   1998 sunscpal_show_req(sr)
   1999 	struct sunscpal_req *sr;
   2000 {
   2001 	struct scsipi_xfer *xs = sr->sr_xs;
   2002 
   2003 	db_printf("TID=%d ",	sr->sr_target);
   2004 	db_printf("LUN=%d ",	sr->sr_lun);
   2005 	db_printf("dh=%p ",	sr->sr_dma_hand);
   2006 	db_printf("dptr=%p ",	sr->sr_dataptr);
   2007 	db_printf("dlen=0x%x ",	sr->sr_datalen);
   2008 	db_printf("flags=%d ",	sr->sr_flags);
   2009 	db_printf("stat=%d ",	sr->sr_status);
   2010 
   2011 	if (xs == NULL) {
   2012 		db_printf("(xs=NULL)\n");
   2013 		return;
   2014 	}
   2015 	db_printf("\n");
   2016 #ifdef	SCSIDEBUG
   2017 	show_scsipi_xs(xs);
   2018 #else
   2019 	db_printf("xs=%p\n", xs);
   2020 #endif
   2021 }
   2022 
   2023 void
   2024 sunscpal_show_state()
   2025 {
   2026 	struct sunscpal_softc *sc;
   2027 	struct sunscpal_req *sr;
   2028 	int i, j, k;
   2029 
   2030 	sc = sunscpal_debug_sc;
   2031 
   2032 	if (sc == NULL) {
   2033 		db_printf("sunscpal_debug_sc == NULL\n");
   2034 		return;
   2035 	}
   2036 
   2037 	db_printf("sc_ncmds=%d\n",  	sc->sc_ncmds);
   2038 	k = -1;	/* which is current? */
   2039 	for (i = 0; i < SUNSCPAL_OPENINGS; i++) {
   2040 		sr = &sc->sc_ring[i];
   2041 		if (sr->sr_xs) {
   2042 			if (sr == sc->sc_current)
   2043 				k = i;
   2044 			db_printf("req %d: (sr=%p)", i, sr);
   2045 			sunscpal_show_req(sr);
   2046 		}
   2047 	}
   2048 	db_printf("sc_rr=%d, current=%d\n", sc->sc_rr, k);
   2049 
   2050 	db_printf("Active request matrix:\n");
   2051 	for(i = 0; i < 8; i++) {		/* targets */
   2052 		for (j = 0; j < 8; j++) {	/* LUN */
   2053 			sr = sc->sc_matrix[i][j];
   2054 			if (sr) {
   2055 				db_printf("TID=%d LUN=%d sr=%p\n", i, j, sr);
   2056 			}
   2057 		}
   2058 	}
   2059 
   2060 	db_printf("sc_state=0x%x\n",	sc->sc_state);
   2061 	db_printf("sc_current=%p\n",	sc->sc_current);
   2062 	db_printf("sc_dataptr=%p\n",	sc->sc_dataptr);
   2063 	db_printf("sc_datalen=0x%x\n",	sc->sc_datalen);
   2064 
   2065 	db_printf("sc_prevphase=%d\n",	sc->sc_prevphase);
   2066 	db_printf("sc_msgpriq=0x%x\n",	sc->sc_msgpriq);
   2067 }
   2068 #endif	/* DDB */
   2069 #endif	/* SUNSCPAL_DEBUG */
   2070 
   2071 void
   2072 sunscpal_attach(sc, options)
   2073 	struct sunscpal_softc *sc;
   2074 	int options;
   2075 {
   2076 
   2077 	/*
   2078 	 * Handle our options.
   2079 	 */
   2080 	printf(": options=0x%x\n", options);
   2081 	sc->sc_parity_disable = (options & SUNSCPAL_OPT_NO_PARITY_CHK);
   2082 	if (options & SUNSCPAL_OPT_FORCE_POLLING)
   2083 		sc->sc_flags |= SUNSCPAL_FORCE_POLLING;
   2084 	if (options & SUNSCPAL_OPT_DISABLE_DMA)
   2085 		sc->sc_flags |= SUNSCPAL_DISABLE_DMA;
   2086 
   2087 	/*
   2088 	 * Fill in the adapter.
   2089 	 */
   2090 	memset(&sc->sc_adapter, 0, sizeof(sc->sc_adapter));
   2091 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
   2092 	sc->sc_adapter.adapt_nchannels = 1;
   2093 	sc->sc_adapter.adapt_openings = SUNSCPAL_OPENINGS;
   2094 	sc->sc_adapter.adapt_max_periph = 1;
   2095 	sc->sc_adapter.adapt_request = sunscpal_scsipi_request;
   2096 	sc->sc_adapter.adapt_minphys = sunscpal_minphys;
   2097 
   2098 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
   2099 	sc->sc_channel.chan_bustype = &scsi_bustype;
   2100 	sc->sc_channel.chan_channel = 0;
   2101 	sc->sc_channel.chan_ntargets = 8;
   2102 	sc->sc_channel.chan_nluns = 8;
   2103 	sc->sc_channel.chan_id = 7;
   2104 
   2105 	/*
   2106 	 * Add reference to adapter so that we drop the reference after
   2107 	 * config_found() to make sure the adatper is disabled.
   2108 	 */
   2109 	if (scsipi_adapter_addref(&sc->sc_adapter) != 0) {
   2110 		printf("%s: unable to enable controller\n",
   2111 		    sc->sc_dev.dv_xname);
   2112 		return;
   2113 	}
   2114 
   2115 	sunscpal_init(sc);	/* Init chip and driver */
   2116 	sunscpal_reset_scsibus(sc);
   2117 
   2118 	/*
   2119 	 * Ask the adapter what subunits are present
   2120 	 */
   2121 	(void) config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
   2122 	scsipi_adapter_delref(&sc->sc_adapter);
   2123 }
   2124 
   2125 int
   2126 sunscpal_detach(sc, flags)
   2127 	struct sunscpal_softc *sc;
   2128 	int flags;
   2129 {
   2130 
   2131 	return (EOPNOTSUPP);
   2132 }
   2133 
   2134 static void
   2135 sunscpal_minphys(struct buf *bp)
   2136 {
   2137 	if (bp->b_bcount > SUNSCPAL_MAX_DMA_LEN) {
   2138 #ifdef	SUNSCPAL_DEBUG
   2139 		if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
   2140 			printf("sunscpal_minphys len = 0x%lx.\n", bp->b_bcount);
   2141 			Debugger();
   2142 		}
   2143 #endif
   2144 		bp->b_bcount = SUNSCPAL_MAX_DMA_LEN;
   2145 	}
   2146 	return (minphys(bp));
   2147 }
   2148 
   2149 #ifdef SUNSCPAL_USE_BUS_DMA
   2150 
   2151 /*
   2152  * Allocate a DMA handle and put it in sr->sr_dma_hand.  Prepare
   2153  * for DMA transfer.
   2154  */
   2155 static void
   2156 sunscpal_dma_alloc(sc)
   2157 	struct sunscpal_softc *sc;
   2158 {
   2159 	struct sunscpal_req *sr = sc->sc_current;
   2160 	sunscpal_dma_handle_t dh;
   2161 	int i, xlen;
   2162 	u_long addr;
   2163 
   2164 #ifdef	DIAGNOSTIC
   2165 	if (sr->sr_dma_hand != NULL)
   2166 		panic("sunscpal_dma_alloc: already have DMA handle");
   2167 #endif
   2168 
   2169 	addr = (u_long) sc->sc_dataptr;
   2170 	xlen = sc->sc_datalen;
   2171 
   2172 	/* If the DMA start addr is misaligned then do PIO */
   2173 	if ((addr & 1) || (xlen & 1)) {
   2174 		printf("sunscpal_dma_alloc: misaligned.\n");
   2175 		return;
   2176 	}
   2177 
   2178 	/* Make sure our caller checked sc_min_dma_len. */
   2179 	if (xlen < sc->sc_min_dma_len)
   2180 		panic("sunscpal_dma_alloc: xlen=0x%x", xlen);
   2181 
   2182 	/*
   2183 	 * Never attempt single transfers of more than 63k, because
   2184 	 * our count register is only 16 bits.
   2185 	 * This should never happen since already bounded by minphys().
   2186 	 * XXX - Should just segment these...
   2187 	 */
   2188 	if (xlen > SUNSCPAL_MAX_DMA_LEN) {
   2189 		printf("sunscpal_dma_alloc: excessive xlen=0x%x\n", xlen);
   2190 		Debugger();
   2191 		sc->sc_datalen = xlen = SUNSCPAL_MAX_DMA_LEN;
   2192 	}
   2193 
   2194 	/* Find free DMA handle.  Guaranteed to find one since we have
   2195 	   as many DMA handles as the driver has processes. */
   2196 	for (i = 0; i < SUNSCPAL_OPENINGS; i++) {
   2197 		if ((sc->sc_dma_handles[i].dh_flags & SUNSCDH_BUSY) == 0)
   2198 			goto found;
   2199 	}
   2200 	panic("sc: no free DMA handles.");
   2201 found:
   2202 
   2203 	dh = &sc->sc_dma_handles[i];
   2204 	dh->dh_flags = SUNSCDH_BUSY;
   2205 	dh->dh_mapaddr = (u_char*) addr;
   2206 	dh->dh_maplen  = xlen;
   2207 	dh->dh_dvma = 0;
   2208 
   2209 	/* Load the DMA map. */
   2210 	if (bus_dmamap_load(sc->sunscpal_dmat, dh->dh_dmamap, dh->dh_mapaddr, dh->dh_maplen, NULL, BUS_DMA_NOWAIT) != 0) {
   2211 		/* Can't load map */
   2212 		printf("sunscpal_dma_alloc: can't DMA %p/0x%x\n",
   2213 			dh->dh_mapaddr, dh->dh_maplen);
   2214 		dh->dh_flags = 0;
   2215 		return;
   2216 	}
   2217 
   2218 	/* success */
   2219 	sr->sr_dma_hand = dh;
   2220 
   2221 	return;
   2222 }
   2223 
   2224 static void
   2225 sunscpal_dma_free(sc)
   2226 	struct sunscpal_softc *sc;
   2227 {
   2228 	struct sunscpal_req *sr = sc->sc_current;
   2229 	sunscpal_dma_handle_t dh = sr->sr_dma_hand;
   2230 
   2231 #ifdef	DIAGNOSTIC
   2232 	if (dh == NULL)
   2233 		panic("sunscpal_dma_free: no DMA handle");
   2234 #endif
   2235 
   2236 	if (sc->sc_state & SUNSCPAL_DOINGDMA)
   2237 		panic("sunscpal_dma_free: free while in progress");
   2238 
   2239 	if (dh->dh_flags & SUNSCDH_BUSY) {
   2240 		/* XXX - Should separate allocation and mapping. */
   2241 		/* Give back the DVMA space. */
   2242 		bus_dmamap_unload(sc->sunscpal_dmat, dh->dh_dmamap);
   2243 		dh->dh_flags = 0;
   2244 	}
   2245 	sr->sr_dma_hand = NULL;
   2246 }
   2247 
   2248 /*
   2249  * This function is called during the SELECT phase that
   2250  * precedes a COMMAND phase, in case we need to setup the
   2251  * DMA engine before the bus enters a DATA phase.
   2252  *
   2253  * On the sc version, setup the start address and the count.
   2254  */
   2255 static void
   2256 sunscpal_dma_setup(sc)
   2257 	struct sunscpal_softc *sc;
   2258 {
   2259 	struct sunscpal_req *sr = sc->sc_current;
   2260 	struct scsipi_xfer *xs = sr->sr_xs;
   2261 	sunscpal_dma_handle_t dh = sr->sr_dma_hand;
   2262 	long data_pa;
   2263 	int xlen;
   2264 
   2265 	/*
   2266 	 * Get the DVMA mapping for this segment.
   2267 	 * XXX - Should separate allocation and mapin.
   2268 	 */
   2269 	data_pa = dh->dh_dvma;
   2270 	data_pa += (sc->sc_dataptr - dh->dh_mapaddr);
   2271 	if (data_pa & 1)
   2272 		panic("sunscpal_dma_setup: bad pa=0x%lx", data_pa);
   2273 	xlen = sc->sc_datalen;
   2274 	if (xlen & 1)
   2275 		panic("sunscpal_dma_setup: bad xlen=0x%x", xlen);
   2276 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
   2277 
   2278 #ifdef	SUNSCPAL_DEBUG
   2279 	if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
   2280 		printf("sunscpal_dma_setup: dh=%p, pa=0x%lx, xlen=0x%x\n",
   2281 			   dh, data_pa, xlen);
   2282 	}
   2283 #endif
   2284 
   2285 	/* sync the DMA map: */
   2286 	bus_dmamap_sync(sc->sunscpal_dmat, dh->dh_dmamap, 0, dh->dh_maplen,
   2287 	    ((xs->xs_control & XS_CTL_DATA_OUT) == 0 ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
   2288 
   2289 	/* Load the start address and the count. */
   2290 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_h, (data_pa >> 16) & 0xFFFF);
   2291 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_l, (data_pa >> 0) & 0xFFFF);
   2292 	SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(xlen));
   2293 }
   2294 
   2295 #endif /* SUNSCPAL_USE_BUS_DMA */
   2296