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