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