Home | History | Annotate | Line # | Download | only in dev
sbc.c revision 1.26
      1 /*	$NetBSD: sbc.c,v 1.26 1997/05/13 06:34:00 scottr Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1996 Scott Reynolds.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Scott Reynolds for
     17  *      the NetBSD Project.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * This file contains only the machine-dependent parts of the mac68k
     35  * NCR 5380 SCSI driver.  (Autoconfig stuff and PDMA functions.)
     36  * The machine-independent parts are in ncr5380sbc.c
     37  *
     38  * Supported hardware includes:
     39  * Macintosh II family 5380-based controller
     40  *
     41  * Credits, history:
     42  *
     43  * Scott Reynolds wrote this module, based on work by Allen Briggs
     44  * (mac68k), Gordon W. Ross and David Jones (sun3), and Leo Weppelman
     45  * (atari).  Thanks to Allen for supplying crucial interpretation of the
     46  * NetBSD/mac68k 1.1 'ncrscsi' driver.  Also, Allen, Gordon, and Jason
     47  * Thorpe all helped to refine this code, and were considerable sources
     48  * of moral support.
     49  */
     50 
     51 #include <sys/types.h>
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/kernel.h>
     55 #include <sys/errno.h>
     56 #include <sys/device.h>
     57 #include <sys/buf.h>
     58 #include <sys/proc.h>
     59 #include <sys/user.h>
     60 
     61 #include <scsi/scsi_all.h>
     62 #include <scsi/scsi_debug.h>
     63 #include <scsi/scsiconf.h>
     64 
     65 #include <dev/ic/ncr5380reg.h>
     66 #include <dev/ic/ncr5380var.h>
     67 
     68 #include <machine/cpu.h>
     69 #include <machine/viareg.h>
     70 
     71 #include "sbcreg.h"
     72 #include "sbcvar.h"
     73 
     74 int	sbc_debug = 0 /* | SBC_DB_INTR | SBC_DB_DMA */;
     75 int	sbc_link_flags = 0 /* | SDEV_DB2 */;
     76 int	sbc_options = 0 /* | SBC_PDMA */;
     77 
     78 static	void	sbc_minphys __P((struct buf *bp));
     79 
     80 struct scsi_adapter	sbc_ops = {
     81 	ncr5380_scsi_cmd,		/* scsi_cmd()		*/
     82 	sbc_minphys,			/* scsi_minphys()	*/
     83 	NULL,				/* open_target_lu()	*/
     84 	NULL,				/* close_target_lu()	*/
     85 };
     86 
     87 /* This is copied from julian's bt driver */
     88 /* "so we have a default dev struct for our link struct." */
     89 struct scsi_device sbc_dev = {
     90 	NULL,		/* Use default error handler.	    */
     91 	NULL,		/* Use default start handler.		*/
     92 	NULL,		/* Use default async handler.	    */
     93 	NULL,		/* Use default "done" routine.	    */
     94 };
     95 
     96 struct cfdriver sbc_cd = {
     97 	NULL, "sbc", DV_DULL
     98 };
     99 
    100 static	int	sbc_ready __P((struct ncr5380_softc *));
    101 static	void	sbc_wait_not_req __P((struct ncr5380_softc *));
    102 
    103 static void
    104 sbc_minphys(struct buf *bp)
    105 {
    106 	if (bp->b_bcount > MAX_DMA_LEN)
    107 		bp->b_bcount = MAX_DMA_LEN;
    108 	return (minphys(bp));
    109 }
    110 
    111 
    112 /***
    113  * General support for Mac-specific SCSI logic.
    114  ***/
    115 
    116 void
    117 sbc_irq_intr(p)
    118 	void *p;
    119 {
    120 	struct ncr5380_softc *ncr_sc = p;
    121 	int claimed = 0;
    122 
    123 	/* How we ever arrive here without IRQ set is a mystery... */
    124 	if (*ncr_sc->sci_csr & SCI_CSR_INT) {
    125 #ifdef SBC_DEBUG
    126 		if (sbc_debug & SBC_DB_INTR)
    127 			decode_5380_intr(ncr_sc);
    128 #endif
    129 		claimed = ncr5380_intr(ncr_sc);
    130 		if (!claimed) {
    131 			if (((*ncr_sc->sci_csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT)
    132 			    && ((*ncr_sc->sci_bus_csr & ~SCI_BUS_RST) == 0))
    133 				SCI_CLR_INTR(ncr_sc);	/* RST interrupt */
    134 #ifdef SBC_DEBUG
    135 			else {
    136 				printf("%s: spurious intr\n",
    137 				    ncr_sc->sc_dev.dv_xname);
    138 				SBC_BREAK;
    139 			}
    140 #endif
    141 		}
    142 	}
    143 }
    144 
    145 #ifdef SBC_DEBUG
    146 void
    147 decode_5380_intr(ncr_sc)
    148 	struct ncr5380_softc *ncr_sc;
    149 {
    150 	u_char csr = *ncr_sc->sci_csr;
    151 	u_char bus_csr = *ncr_sc->sci_bus_csr;
    152 
    153 	if (((csr & ~(SCI_CSR_PHASE_MATCH | SCI_CSR_ATN)) == SCI_CSR_INT) &&
    154 	    ((bus_csr & ~(SCI_BUS_MSG | SCI_BUS_CD | SCI_BUS_IO | SCI_BUS_DBP)) == SCI_BUS_SEL)) {
    155 		if (csr & SCI_BUS_IO)
    156 			printf("%s: reselect\n", ncr_sc->sc_dev.dv_xname);
    157 		else
    158 			printf("%s: select\n", ncr_sc->sc_dev.dv_xname);
    159 	} else if (((csr & ~SCI_CSR_ACK) == (SCI_CSR_DONE | SCI_CSR_INT)) &&
    160 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
    161 		printf("%s: dma eop\n", ncr_sc->sc_dev.dv_xname);
    162 	else if (((csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT) &&
    163 	    ((bus_csr & ~SCI_BUS_RST) == 0))
    164 		printf("%s: bus reset\n", ncr_sc->sc_dev.dv_xname);
    165 	else if (((csr & ~(SCI_CSR_DREQ | SCI_CSR_ATN | SCI_CSR_ACK)) == (SCI_CSR_PERR | SCI_CSR_INT | SCI_CSR_PHASE_MATCH)) &&
    166 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
    167 		printf("%s: parity error\n", ncr_sc->sc_dev.dv_xname);
    168 	else if (((csr & ~SCI_CSR_ATN) == SCI_CSR_INT) &&
    169 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_REQ | SCI_BUS_SEL)) == (SCI_BUS_BSY | SCI_BUS_REQ)))
    170 		printf("%s: phase mismatch\n", ncr_sc->sc_dev.dv_xname);
    171 	else if (((csr & ~SCI_CSR_PHASE_MATCH) == (SCI_CSR_INT | SCI_CSR_DISC)) &&
    172 	    (bus_csr == 0))
    173 		printf("%s: disconnect\n", ncr_sc->sc_dev.dv_xname);
    174 	else
    175 		printf("%s: unknown intr: csr=%x, bus_csr=%x\n",
    176 		    ncr_sc->sc_dev.dv_xname, csr, bus_csr);
    177 }
    178 #endif
    179 
    180 
    181 /***
    182  * The following code implements polled PDMA.
    183  ***/
    184 
    185 #define	TIMEOUT	5000000			/* x 2 usec = 10 sec */
    186 
    187 static __inline__ int
    188 sbc_ready(sc)
    189 	struct ncr5380_softc *sc;
    190 {
    191 	int i = TIMEOUT;
    192 
    193 	for (;;) {
    194 		if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) ==
    195 		    (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
    196 			return 1;
    197 		if (((*sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0) ||
    198 		    (SCI_BUSY(sc) == 0))
    199 			return 0;
    200 		if (--i < 0)
    201 			break;
    202 		delay(2);
    203 	}
    204 
    205 	printf("%s: ready timeout\n", sc->sc_dev.dv_xname);
    206 	return 0;
    207 }
    208 
    209 static __inline__ void
    210 sbc_wait_not_req(sc)
    211 	struct ncr5380_softc *sc;
    212 {
    213 	int i = TIMEOUT;
    214 
    215 	for (;;) {
    216 		if ((*sc->sci_bus_csr & SCI_BUS_REQ) == 0 ||
    217 		    (*sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0 ||
    218 		    SCI_BUSY(sc) == 0) {
    219 			return;
    220 		}
    221 		if (--i < 0)
    222 			break;
    223 		delay(2);
    224 	}
    225 	printf("%s: pdma not_req timeout\n", sc->sc_dev.dv_xname);
    226 }
    227 
    228 int
    229 sbc_pdma_in(ncr_sc, phase, datalen, data)
    230 	struct ncr5380_softc *ncr_sc;
    231 	int phase, datalen;
    232 	u_char *data;
    233 {
    234 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    235 	volatile u_int32_t *long_data = (u_int32_t *)sc->sc_drq_addr;
    236 	volatile u_int8_t *byte_data = (u_int8_t *)sc->sc_nodrq_addr;
    237 	int resid, s;
    238 
    239 	s = splbio();
    240 	*ncr_sc->sci_mode |= SCI_MODE_DMA;
    241 	*ncr_sc->sci_irecv = 0;
    242 
    243 #define R4	*((u_int32_t *)data)++ = *long_data++
    244 #define R1	*data++ = *byte_data++
    245 	for (resid = datalen; resid >= 128; resid -= 128) {
    246 		if (sbc_ready(ncr_sc) == 0)
    247 			goto interrupt;
    248 		R4; R4; R4; R4; R4; R4; R4; R4;
    249 		R4; R4; R4; R4; R4; R4; R4; R4;
    250 		R4; R4; R4; R4; R4; R4; R4; R4;
    251 		R4; R4; R4; R4; R4; R4; R4; R4;
    252 
    253 		long_data = (u_int32_t *)sc->sc_drq_addr;
    254 		byte_data = (u_int8_t *)sc->sc_nodrq_addr;
    255 	}
    256 	while (resid) {
    257 		if (sbc_ready(ncr_sc) == 0)
    258 			goto interrupt;
    259 		R1;
    260 		resid--;
    261 	}
    262 #undef R4
    263 #undef R1
    264 
    265 	sbc_wait_not_req(ncr_sc);
    266 interrupt:
    267 	SCI_CLR_INTR(ncr_sc);
    268 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    269 	splx(s);
    270 	return datalen - resid;
    271 }
    272 
    273 int
    274 sbc_pdma_out(ncr_sc, phase, datalen, data)
    275 	struct ncr5380_softc *ncr_sc;
    276 	int phase, datalen;
    277 	u_char *data;
    278 {
    279 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    280 	volatile u_int32_t *long_data = (u_int32_t *)sc->sc_drq_addr;
    281 	volatile u_int8_t *byte_data = (u_int8_t *)sc->sc_nodrq_addr;
    282 	int i, s, resid;
    283 	u_char icmd;
    284 
    285 	if (datalen < ncr_sc->sc_min_dma_len)
    286 		return ncr5380_pio_out(ncr_sc, phase, datalen, data);
    287 
    288 	s = splbio();
    289 	icmd = *(ncr_sc->sci_icmd) & SCI_ICMD_RMASK;
    290 	*ncr_sc->sci_icmd = icmd | SCI_ICMD_DATA;
    291 	*ncr_sc->sci_mode |= SCI_MODE_DMA;
    292 	*ncr_sc->sci_dma_send = 0;
    293 
    294 	resid = datalen;
    295 	if (sbc_ready(ncr_sc) == 0)
    296 		goto interrupt;
    297 
    298 #define W1	*byte_data++ = *data++
    299 #define W4	*long_data++ = *((u_int32_t *)data)++
    300 	while (resid >= 64) {
    301 		if (sbc_ready(ncr_sc) == 0)
    302 			goto interrupt;
    303 		W1;
    304 		resid--;
    305 		if (sbc_ready(ncr_sc) == 0)
    306 			goto interrupt;
    307 		W1;
    308 		resid--;
    309 		if (sbc_ready(ncr_sc) == 0)
    310 			goto interrupt;
    311 		W1;
    312 		resid--;
    313 		if (sbc_ready(ncr_sc) == 0)
    314 			goto interrupt;
    315 		W1;
    316 		resid--;
    317 		if (sbc_ready(ncr_sc) == 0)
    318 			goto interrupt;
    319 		W4; W4; W4; W4;
    320 		W4; W4; W4; W4;
    321 		W4; W4; W4; W4;
    322 		W4; W4; W4;
    323 		resid -= 60;
    324 
    325 		long_data = (u_int32_t *)sc->sc_drq_addr;
    326 		byte_data = (u_int8_t *)sc->sc_nodrq_addr;
    327 	}
    328 	for (; resid; resid--) {
    329 		if (sbc_ready(ncr_sc) == 0)
    330 			goto interrupt;
    331 		W1;
    332 	}
    333 #undef  W1
    334 #undef  W4
    335 
    336 	for (i = TIMEOUT; i > 0; i--) {
    337 		if ((*ncr_sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
    338 		    != SCI_CSR_DREQ)
    339 			break;
    340 	}
    341 	if (i == 0)
    342 		printf("%s: timeout waiting for final SCI_DSR_DREQ.\n",
    343 			ncr_sc->sc_dev.dv_xname);
    344 #ifdef __notyet__	/* not sure why this is ever necessary... */
    345 	else
    346 		*byte_data = 0;
    347 #endif
    348 
    349 	sbc_wait_not_req(ncr_sc);
    350 interrupt:
    351 	SCI_CLR_INTR(ncr_sc);
    352 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    353 	*ncr_sc->sci_icmd = icmd;
    354 	splx(s);
    355 	return (datalen - resid);
    356 }
    357 
    358 
    359 /***
    360  * The following code implements interrupt-driven PDMA.
    361  ***/
    362 
    363 /*
    364  * This is the meat of the PDMA transfer.
    365  * When we get here, we shove data as fast as the mac can take it.
    366  * We depend on several things:
    367  *   * All macs after the Mac Plus that have a 5380 chip should have a general
    368  *     logic IC that handshakes data for blind transfers.
    369  *   * If the SCSI controller finishes sending/receiving data before we do,
    370  *     the same general logic IC will generate a /BERR for us in short order.
    371  *   * The fault address for said /BERR minus the base address for the
    372  *     transfer will be the amount of data that was actually written.
    373  *
    374  * We use the nofault flag and the setjmp/longjmp in locore.s so we can
    375  * detect and handle the bus error for early termination of a command.
    376  * This is usually caused by a disconnecting target.
    377  */
    378 void
    379 sbc_drq_intr(p)
    380 	void *p;
    381 {
    382 	extern int *nofault, mac68k_buserr_addr;
    383 	struct sbc_softc *sc = (struct sbc_softc *)p;
    384 	struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *)p;
    385 	struct sci_req *sr = ncr_sc->sc_current;
    386 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    387 	label_t faultbuf;
    388 	volatile u_int32_t *long_drq;
    389 	u_int32_t *long_data;
    390 	volatile u_int8_t *drq;
    391 	u_int8_t *data;
    392 	int count, dcount, resid;
    393 	u_int8_t tmp;
    394 
    395 	/* Work around lame gcc initialization bug */
    396 	(void)&drq;
    397 
    398 	/*
    399 	 * If we're not ready to xfer data, or have no more, just return.
    400 	 */
    401 	if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0 || dh->dh_len == 0)
    402 		return;
    403 
    404 #ifdef SBC_DEBUG
    405 	if (sbc_debug & SBC_DB_INTR)
    406 		printf("%s: drq intr, dh_len=0x%x, dh_flags=0x%x\n",
    407 		    ncr_sc->sc_dev.dv_xname, dh->dh_len, dh->dh_flags);
    408 #endif
    409 
    410 	/*
    411 	 * Setup for a possible bus error caused by SCSI controller
    412 	 * switching out of DATA-IN/OUT before we're done with the
    413 	 * current transfer.
    414 	 */
    415 	nofault = (int *)&faultbuf;
    416 
    417 	if (setjmp((label_t *)nofault)) {
    418 		nofault = (int *)0;
    419 		if ((dh->dh_flags & SBC_DH_DONE) == 0) {
    420 			count = ((  (u_long)mac68k_buserr_addr
    421 				  - (u_long)sc->sc_drq_addr));
    422 
    423 			if ((count < 0) || (count > dh->dh_len)) {
    424 				printf("%s: complete=0x%x (pending 0x%x)\n",
    425 				    ncr_sc->sc_dev.dv_xname, count, dh->dh_len);
    426 				panic("something is wrong");
    427 			}
    428 
    429 			dh->dh_addr += count;
    430 			dh->dh_len -= count;
    431 		} else
    432 			count = 0;
    433 
    434 #ifdef SBC_DEBUG
    435 		if (sbc_debug & SBC_DB_INTR)
    436 			printf("%s: drq /berr, complete=0x%x (pending 0x%x)\n",
    437 			   ncr_sc->sc_dev.dv_xname, count, dh->dh_len);
    438 #endif
    439 		mac68k_buserr_addr = 0;
    440 
    441 		return;
    442 	}
    443 
    444 	if (dh->dh_flags & SBC_DH_OUT) { /* Data Out */
    445 		dcount = 0;
    446 
    447 		/*
    448 		 * Get the source address aligned.
    449 		 */
    450 		resid =
    451 		    count = min(dh->dh_len, 4 - (((int)dh->dh_addr) & 0x3));
    452 		if (count && count < 4) {
    453 			drq = (volatile u_int8_t *)sc->sc_drq_addr;
    454 			data = (u_int8_t *)dh->dh_addr;
    455 
    456 #define W1		*drq++ = *data++
    457 			while (count) {
    458 				W1; count--;
    459 			}
    460 #undef W1
    461 			dh->dh_addr += resid;
    462 			dh->dh_len -= resid;
    463 		}
    464 
    465 		/*
    466 		 * Start the transfer.
    467 		 */
    468 		while (dh->dh_len) {
    469 			dcount = count = min(dh->dh_len, MAX_DMA_LEN);
    470 			long_drq = (volatile u_int32_t *)sc->sc_drq_addr;
    471 			long_data = (u_int32_t *)dh->dh_addr;
    472 
    473 #define W4		*long_drq++ = *long_data++
    474 			while (count >= 64) {
    475 				W4; W4; W4; W4; W4; W4; W4; W4;
    476 				W4; W4; W4; W4; W4; W4; W4; W4; /*  64 */
    477 				count -= 64;
    478 			}
    479 			while (count >= 4) {
    480 				W4; count -= 4;
    481 			}
    482 #undef W4
    483 			data = (u_int8_t *)long_data;
    484 			drq = (u_int8_t *)long_drq;
    485 
    486 #define W1		*drq++ = *data++
    487 			while (count) {
    488 				W1; count--;
    489 			}
    490 #undef W1
    491 			dh->dh_len -= dcount;
    492 			dh->dh_addr += dcount;
    493 		}
    494 		dh->dh_flags |= SBC_DH_DONE;
    495 
    496 		/*
    497 		 * XXX -- Read a byte from the SBC to trigger a /BERR.
    498 		 * This seems to be necessary for us to notice that
    499 		 * the target has disconnected.  Ick.  06 jun 1996 (sr)
    500 		 */
    501 		if (dcount >= MAX_DMA_LEN)
    502 			drq = (volatile u_int8_t *)sc->sc_drq_addr;
    503 		tmp = *drq;
    504 	} else {	/* Data In */
    505 		/*
    506 		 * Get the dest address aligned.
    507 		 */
    508 		resid =
    509 		    count = min(dh->dh_len, 4 - (((int)dh->dh_addr) & 0x3));
    510 		if (count && count < 4) {
    511 			data = (u_int8_t *)dh->dh_addr;
    512 			drq = (volatile u_int8_t *)sc->sc_drq_addr;
    513 
    514 #define R1		*data++ = *drq++
    515 			while (count) {
    516 				R1; count--;
    517 			}
    518 #undef R1
    519 			dh->dh_addr += resid;
    520 			dh->dh_len -= resid;
    521 		}
    522 
    523 		/*
    524 		 * Start the transfer.
    525 		 */
    526 		while (dh->dh_len) {
    527 			dcount = count = min(dh->dh_len, MAX_DMA_LEN);
    528 			long_data = (u_int32_t *)dh->dh_addr;
    529 			long_drq = (volatile u_int32_t *)sc->sc_drq_addr;
    530 
    531 #define R4		*long_data++ = *long_drq++
    532 			while (count >= 64) {
    533 				R4; R4; R4; R4; R4; R4; R4; R4;
    534 				R4; R4; R4; R4; R4; R4; R4; R4;	/* 64 */
    535 				count -= 64;
    536 			}
    537 			while (count >= 4) {
    538 				R4; count -= 4;
    539 			}
    540 #undef R4
    541 			data = (u_int8_t *)long_data;
    542 			drq = (volatile u_int8_t *)long_drq;
    543 
    544 #define R1		*data++ = *drq++
    545 			while (count) {
    546 				R1; count--;
    547 			}
    548 #undef R1
    549 			dh->dh_len -= dcount;
    550 			dh->dh_addr += dcount;
    551 		}
    552 		dh->dh_flags |= SBC_DH_DONE;
    553 	}
    554 
    555 	/*
    556 	 * OK.  No bus error occurred above.  Clear the nofault flag
    557 	 * so we no longer short-circuit bus errors.
    558 	 */
    559 	nofault = (int *) 0;
    560 
    561 #ifdef SBC_DEBUG
    562 	if (sbc_debug & (SBC_DB_REG | SBC_DB_INTR))
    563 		printf("%s: drq intr complete: csr=0x%x, bus_csr=0x%x\n",
    564 		    ncr_sc->sc_dev.dv_xname, *ncr_sc->sci_csr,
    565 		    *ncr_sc->sci_bus_csr);
    566 #endif
    567 }
    568 
    569 void
    570 sbc_dma_alloc(ncr_sc)
    571 	struct ncr5380_softc *ncr_sc;
    572 {
    573 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    574 	struct sci_req *sr = ncr_sc->sc_current;
    575 	struct scsi_xfer *xs = sr->sr_xs;
    576 	struct sbc_pdma_handle *dh;
    577 	int		i, xlen;
    578 
    579 #ifdef DIAGNOSTIC
    580 	if (sr->sr_dma_hand != NULL)
    581 		panic("sbc_dma_alloc: already have PDMA handle");
    582 #endif
    583 
    584 	/* Polled transfers shouldn't allocate a PDMA handle. */
    585 	if (sr->sr_flags & SR_IMMED)
    586 		return;
    587 
    588 	xlen = ncr_sc->sc_datalen;
    589 
    590 	/* Make sure our caller checked sc_min_dma_len. */
    591 	if (xlen < MIN_DMA_LEN)
    592 		panic("sbc_dma_alloc: len=0x%x\n", xlen);
    593 
    594 	/*
    595 	 * Find free PDMA handle.  Guaranteed to find one since we
    596 	 * have as many PDMA handles as the driver has processes.
    597 	 * (instances?)
    598 	 */
    599 	 for (i = 0; i < SCI_OPENINGS; i++) {
    600 		if ((sc->sc_pdma[i].dh_flags & SBC_DH_BUSY) == 0)
    601 			goto found;
    602 	}
    603 	panic("sbc: no free PDMA handles");
    604 found:
    605 	dh = &sc->sc_pdma[i];
    606 	dh->dh_flags = SBC_DH_BUSY;
    607 	dh->dh_addr = ncr_sc->sc_dataptr;
    608 	dh->dh_len = xlen;
    609 
    610 	/* Copy the 'write' flag for convenience. */
    611 	if (xs->flags & SCSI_DATA_OUT)
    612 		dh->dh_flags |= SBC_DH_OUT;
    613 
    614 	sr->sr_dma_hand = dh;
    615 }
    616 
    617 void
    618 sbc_dma_free(ncr_sc)
    619 	struct ncr5380_softc *ncr_sc;
    620 {
    621 	struct sci_req *sr = ncr_sc->sc_current;
    622 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    623 
    624 #ifdef DIAGNOSTIC
    625 	if (sr->sr_dma_hand == NULL)
    626 		panic("sbc_dma_free: no DMA handle");
    627 #endif
    628 
    629 	if (ncr_sc->sc_state & NCR_DOINGDMA)
    630 		panic("sbc_dma_free: free while in progress");
    631 
    632 	if (dh->dh_flags & SBC_DH_BUSY) {
    633 		dh->dh_flags = 0;
    634 		dh->dh_addr = NULL;
    635 		dh->dh_len = 0;
    636 	}
    637 	sr->sr_dma_hand = NULL;
    638 }
    639 
    640 void
    641 sbc_dma_poll(ncr_sc)
    642 	struct ncr5380_softc *ncr_sc;
    643 {
    644 	struct sci_req *sr = ncr_sc->sc_current;
    645 
    646 	/*
    647 	 * We shouldn't arrive here; if SR_IMMED is set, then
    648 	 * dma_alloc() should have refused to allocate a handle
    649 	 * for the transfer.  This forces the polled PDMA code
    650 	 * to handle the request...
    651 	 */
    652 #ifdef SBC_DEBUG
    653 	if (sbc_debug & SBC_DB_DMA)
    654 		printf("%s: lost DRQ interrupt?\n", ncr_sc->sc_dev.dv_xname);
    655 #endif
    656 	sr->sr_flags |= SR_OVERDUE;
    657 }
    658 
    659 void
    660 sbc_dma_setup(ncr_sc)
    661 	struct ncr5380_softc *ncr_sc;
    662 {
    663 	/* Not needed; we don't have real DMA */
    664 }
    665 
    666 void
    667 sbc_dma_start(ncr_sc)
    668 	struct ncr5380_softc *ncr_sc;
    669 {
    670 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    671 	struct sci_req *sr = ncr_sc->sc_current;
    672 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    673 
    674 	/*
    675 	 * Match bus phase, clear pending interrupts, set DMA mode, and
    676 	 * assert data bus (for writing only), then start the transfer.
    677 	 */
    678 	if (dh->dh_flags & SBC_DH_OUT) {
    679 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
    680 		SCI_CLR_INTR(ncr_sc);
    681 		if (sc->sc_clrintr)
    682 			(*sc->sc_clrintr)(ncr_sc);
    683 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
    684 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
    685 		*ncr_sc->sci_dma_send = 0;
    686 	} else {
    687 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
    688 		SCI_CLR_INTR(ncr_sc);
    689 		if (sc->sc_clrintr)
    690 			(*sc->sc_clrintr)(ncr_sc);
    691 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
    692 		*ncr_sc->sci_icmd = 0;
    693 		*ncr_sc->sci_irecv = 0;
    694 	}
    695 	ncr_sc->sc_state |= NCR_DOINGDMA;
    696 
    697 #ifdef SBC_DEBUG
    698 	if (sbc_debug & SBC_DB_DMA)
    699 		printf("%s: PDMA started, va=%p, len=0x%x\n",
    700 		    ncr_sc->sc_dev.dv_xname, dh->dh_addr, dh->dh_len);
    701 #endif
    702 }
    703 
    704 void
    705 sbc_dma_eop(ncr_sc)
    706 	struct ncr5380_softc *ncr_sc;
    707 {
    708 	/* Not used; the EOP pin is wired high (GMFH, pp. 389-390) */
    709 }
    710 
    711 void
    712 sbc_dma_stop(ncr_sc)
    713 	struct ncr5380_softc *ncr_sc;
    714 {
    715 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    716 	struct sci_req *sr = ncr_sc->sc_current;
    717 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    718 	int ntrans;
    719 
    720 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    721 #ifdef SBC_DEBUG
    722 		if (sbc_debug & SBC_DB_DMA)
    723 			printf("%s: dma_stop: DMA not running\n",
    724 			    ncr_sc->sc_dev.dv_xname);
    725 #endif
    726 		return;
    727 	}
    728 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
    729 
    730 	if ((ncr_sc->sc_state & NCR_ABORTING) == 0) {
    731 		ntrans = ncr_sc->sc_datalen - dh->dh_len;
    732 
    733 #ifdef SBC_DEBUG
    734 		if (sbc_debug & SBC_DB_DMA)
    735 			printf("%s: dma_stop: ntrans=0x%x\n",
    736 			    ncr_sc->sc_dev.dv_xname, ntrans);
    737 #endif
    738 
    739 		if (ntrans > ncr_sc->sc_datalen)
    740 			panic("sbc_dma_stop: excess transfer\n");
    741 
    742 		/* Adjust data pointer */
    743 		ncr_sc->sc_dataptr += ntrans;
    744 		ncr_sc->sc_datalen -= ntrans;
    745 
    746 		/* Clear any pending interrupts. */
    747 		SCI_CLR_INTR(ncr_sc);
    748 		if (sc->sc_clrintr)
    749 			(*sc->sc_clrintr)(ncr_sc);
    750 	}
    751 
    752 	/* Put SBIC back into PIO mode. */
    753 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    754 	*ncr_sc->sci_icmd = 0;
    755 
    756 #ifdef SBC_DEBUG
    757 	if (sbc_debug & SBC_DB_REG)
    758 		printf("%s: dma_stop: csr=0x%x, bus_csr=0x%x\n",
    759 		    ncr_sc->sc_dev.dv_xname, *ncr_sc->sci_csr,
    760 		    *ncr_sc->sci_bus_csr);
    761 #endif
    762 }
    763