Home | History | Annotate | Line # | Download | only in dev
sbc.c revision 1.7
      1 /*	$NetBSD: sbc.c,v 1.7 1996/05/29 14:26:33 scottr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Scott Reynolds
      5  * Copyright (c) 1995 David Jones
      6  * Copyright (c) 1995 Allen Briggs
      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 David Jones, Allen
     22  *	Briggs and Scott Reynolds.
     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 file contains only the machine-dependent parts of the mac68k
     38  * NCR 5380 SCSI driver.  (Autoconfig stuff and PDMA functions.)
     39  * The machine-independent parts are in ncr5380sbc.c
     40  *
     41  * Supported hardware includes:
     42  * Macintosh II family 5380-based controller
     43  *
     44  * Credits, history:
     45  *
     46  * Scott Reynolds wrote this module, based on work by Allen Briggs
     47  * (mac68k), David Jones (sun3), and Leo Weppelman (atari).  Allen
     48  * supplied some crucial interpretation of the NetBSD 1.1 'ncrscsi'
     49  * driver.  Allen, Gordon W. Ross, and Jason Thorpe all helped to
     50  * refine this code, and were considerable sources of moral support.
     51  *
     52  * The sbc_options code is based on similar code in Jason's modified
     53  * NetBSD/sparc 'si' driver.
     54  */
     55 
     56 #include <sys/types.h>
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/kernel.h>
     60 #include <sys/errno.h>
     61 #include <sys/device.h>
     62 #include <sys/buf.h>
     63 #include <sys/proc.h>
     64 #include <sys/user.h>
     65 
     66 #include <scsi/scsi_all.h>
     67 #include <scsi/scsi_debug.h>
     68 #include <scsi/scsiconf.h>
     69 
     70 #include <dev/ic/ncr5380reg.h>
     71 #include <dev/ic/ncr5380var.h>
     72 
     73 #include <machine/viareg.h>
     74 
     75 #include "sbcreg.h"
     76 
     77 /*
     78  * Transfers smaller than this are done using PIO
     79  * (on assumption they're not worth PDMA overhead)
     80  */
     81 #define	MIN_DMA_LEN 128
     82 
     83 /*
     84  * Transfers larger than 8192 bytes need to be split up
     85  * due to the size of the PDMA space.
     86  */
     87 #define	MAX_DMA_LEN 0x2000
     88 
     89 /*
     90  * From Guide to the Macintosh Family Hardware, p. 137
     91  * These are offsets from SCSIBase (see pmap_bootstrap.c)
     92  */
     93 #define	SBC_REGISTER_OFFSET	0x10000
     94 #define	SBC_DMA_DRQ_OFFSET	0x06000
     95 #define	SBC_DMA_NODRQ_OFFSET	0x12000
     96 
     97 #ifdef SBC_DEBUG
     98 # define	SBC_DB_INTR	0x01
     99 # define	SBC_DB_DMA	0x02
    100 # define	SBC_DB_REG	0x04
    101 # define	SBC_DB_BREAK	0x08
    102 
    103 	int	sbc_debug = 0 /* | SBC_DB_INTR | SBC_DB_DMA */;
    104 	int	sbc_link_flags = 0 /* | SDEV_DB2 */;
    105 
    106 # ifndef DDB
    107 #  define	Debugger()	printf("Debug: sbc.c:%d\n", __LINE__)
    108 # endif
    109 # define	SBC_BREAK \
    110 		do { if (sbc_debug & SBC_DB_BREAK) Debugger(); } while (0)
    111 #else
    112 # define	SBC_BREAK
    113 #endif
    114 
    115 /*
    116  * This structure is used to keep track of PDMA requests.
    117  */
    118 struct sbc_pdma_handle {
    119 	int	dh_flags;	/* flags */
    120 #define	SBC_DH_BUSY	0x01	/* This handle is in use */
    121 #define	SBC_DH_OUT	0x02	/* PDMA data out (write) */
    122 	u_char	*dh_addr;	/* data buffer */
    123 	int	dh_len;		/* length of data buffer */
    124 };
    125 
    126 /*
    127  * The first structure member has to be the ncr5380_softc
    128  * so we can just cast to go back and forth between them.
    129  */
    130 struct sbc_softc {
    131 	struct ncr5380_softc ncr_sc;
    132 	volatile struct sbc_regs *sc_regs;
    133 	volatile long	*sc_drq_addr;
    134 	volatile u_char	*sc_nodrq_addr;
    135 	volatile u_char	*sc_ienable;
    136 	volatile u_char	*sc_iflag;
    137 	int		sc_options;	/* options for this instance. */
    138 	struct sbc_pdma_handle sc_pdma[SCI_OPENINGS];
    139 };
    140 
    141 /*
    142  * Options.  By default, SCSI interrupts and reselect are disabled.
    143  * You may enable either of these features with the `flags' directive
    144  * in your kernel's configuration file.
    145  *
    146  * Alternatively, you can patch your kernel with DDB or some other
    147  * mechanism.  The sc_options member of the softc is OR'd with
    148  * the value in sbc_options.
    149  */
    150 #define	SBC_PDMA	0x01	/* Use PDMA for polled transfers */
    151 #define	SBC_INTR	0x02	/* Allow SCSI IRQ/DRQ interrupts */
    152 #define	SBC_RESELECT	0x04	/* Allow disconnect/reselect */
    153 #define	SBC_OPTIONS_MASK	(SBC_RESELECT|SBC_INTR|SBC_PDMA)
    154 #define	SBC_OPTIONS_BITS	"\10\3RESELECT\2INTR\1PDMA"
    155 int sbc_options = SBC_PDMA;
    156 
    157 static	int	sbc_match __P((struct device *, void *, void *));
    158 static	void	sbc_attach __P((struct device *, struct device *, void *));
    159 static	int	sbc_print __P((void *, char *));
    160 static	void	sbc_minphys __P((struct buf *bp));
    161 
    162 static	int	sbc_wait_busy __P((struct ncr5380_softc *));
    163 static	int	sbc_ready __P((struct ncr5380_softc *));
    164 static	int	sbc_wait_dreq __P((struct ncr5380_softc *));
    165 static	int	sbc_pdma_in __P((struct ncr5380_softc *, int, int, u_char *));
    166 static	int	sbc_pdma_out __P((struct ncr5380_softc *, int, int, u_char *));
    167 #ifdef SBC_DEBUG
    168 static	void	decode_5380_intr __P((struct ncr5380_softc *));
    169 #endif
    170 
    171 	void	sbc_intr_enable __P((struct ncr5380_softc *));
    172 	void	sbc_intr_disable __P((struct ncr5380_softc *));
    173 	void	sbc_irq_intr __P((void *));
    174 	void	sbc_drq_intr __P((void *));
    175 	void	sbc_dma_alloc __P((struct ncr5380_softc *));
    176 	void	sbc_dma_free __P((struct ncr5380_softc *));
    177 	void	sbc_dma_poll __P((struct ncr5380_softc *));
    178 	void	sbc_dma_setup __P((struct ncr5380_softc *));
    179 	void	sbc_dma_start __P((struct ncr5380_softc *));
    180 	void	sbc_dma_eop __P((struct ncr5380_softc *));
    181 	void	sbc_dma_stop __P((struct ncr5380_softc *));
    182 
    183 static struct scsi_adapter	sbc_ops = {
    184 	ncr5380_scsi_cmd,		/* scsi_cmd()		*/
    185 	sbc_minphys,			/* scsi_minphys()	*/
    186 	NULL,				/* open_target_lu()	*/
    187 	NULL,				/* close_target_lu()	*/
    188 };
    189 
    190 /* This is copied from julian's bt driver */
    191 /* "so we have a default dev struct for our link struct." */
    192 static struct scsi_device sbc_dev = {
    193 	NULL,		/* Use default error handler.	    */
    194 	NULL,		/* Use default start handler.		*/
    195 	NULL,		/* Use default async handler.	    */
    196 	NULL,		/* Use default "done" routine.	    */
    197 };
    198 
    199 struct cfattach sbc_ca = {
    200 	sizeof(struct sbc_softc), sbc_match, sbc_attach
    201 };
    202 
    203 struct cfdriver sbc_cd = {
    204 	NULL, "sbc", DV_DULL
    205 };
    206 
    207 
    208 static int
    209 sbc_match(parent, match, args)
    210 	struct device *parent;
    211 	void *match, *args;
    212 {
    213 	if (!mac68k_machine.scsi80)
    214 		return 0;
    215 	return 1;
    216 }
    217 
    218 static void
    219 sbc_attach(parent, self, args)
    220 	struct device *parent, *self;
    221 	void *args;
    222 {
    223 	struct sbc_softc *sc = (struct sbc_softc *) self;
    224 	struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *) sc;
    225 	extern vm_offset_t SCSIBase;
    226 
    227 	/* Pull in the options flags. */
    228 	sc->sc_options = ((ncr_sc->sc_dev.dv_cfdata->cf_flags | sbc_options)
    229 	    & SBC_OPTIONS_MASK);
    230 
    231 	/*
    232 	 * Set up base address of 5380
    233 	 */
    234 	sc->sc_regs = (struct sbc_regs *)(SCSIBase + SBC_REGISTER_OFFSET);
    235 
    236 	/*
    237 	 * Fill in the prototype scsi_link.
    238 	 */
    239 	ncr_sc->sc_link.adapter_softc = sc;
    240 	ncr_sc->sc_link.adapter_target = 7;
    241 	ncr_sc->sc_link.adapter = &sbc_ops;
    242 	ncr_sc->sc_link.device = &sbc_dev;
    243 
    244 	/*
    245 	 * Initialize fields used by the MI code
    246 	 */
    247 	ncr_sc->sci_r0 = &sc->sc_regs->sci_pr0.sci_reg;
    248 	ncr_sc->sci_r1 = &sc->sc_regs->sci_pr1.sci_reg;
    249 	ncr_sc->sci_r2 = &sc->sc_regs->sci_pr2.sci_reg;
    250 	ncr_sc->sci_r3 = &sc->sc_regs->sci_pr3.sci_reg;
    251 	ncr_sc->sci_r4 = &sc->sc_regs->sci_pr4.sci_reg;
    252 	ncr_sc->sci_r5 = &sc->sc_regs->sci_pr5.sci_reg;
    253 	ncr_sc->sci_r6 = &sc->sc_regs->sci_pr6.sci_reg;
    254 	ncr_sc->sci_r7 = &sc->sc_regs->sci_pr7.sci_reg;
    255 
    256 	/*
    257 	 * MD function pointers used by the MI code.
    258 	 */
    259 	ncr_sc->sc_pio_out   = sbc_pdma_out;
    260 	ncr_sc->sc_pio_in    = sbc_pdma_in;
    261 	ncr_sc->sc_dma_alloc = NULL;
    262 	ncr_sc->sc_dma_free  = NULL;
    263 	ncr_sc->sc_dma_poll  = NULL;
    264 	ncr_sc->sc_intr_on   = NULL;
    265 	ncr_sc->sc_intr_off  = NULL;
    266 	ncr_sc->sc_dma_setup = NULL;
    267 	ncr_sc->sc_dma_start = NULL;
    268 	ncr_sc->sc_dma_eop   = NULL;
    269 	ncr_sc->sc_dma_stop  = NULL;
    270 	ncr_sc->sc_flags = 0;
    271 	ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
    272 
    273 	if ((sc->sc_options & SBC_INTR) == 0) {
    274 		ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
    275 	} else {
    276 		if (sc->sc_options & SBC_RESELECT)
    277 			ncr_sc->sc_flags |= NCR5380_PERMIT_RESELECT;
    278 		ncr_sc->sc_dma_alloc = sbc_dma_alloc;
    279 		ncr_sc->sc_dma_free  = sbc_dma_free;
    280 		ncr_sc->sc_dma_poll  = sbc_dma_poll;
    281 		ncr_sc->sc_dma_setup = sbc_dma_setup;
    282 		ncr_sc->sc_dma_start = sbc_dma_start;
    283 		ncr_sc->sc_dma_eop   = sbc_dma_eop;
    284 		ncr_sc->sc_dma_stop  = sbc_dma_stop;
    285 		mac68k_register_scsi_drq(sbc_drq_intr, ncr_sc);
    286 		mac68k_register_scsi_irq(sbc_irq_intr, ncr_sc);
    287 	}
    288 
    289 	/*
    290 	 * Initialize fields used only here in the MD code.
    291 	 */
    292 	sc->sc_drq_addr = (long *) (SCSIBase + SBC_DMA_DRQ_OFFSET);
    293 	sc->sc_nodrq_addr = (u_char *) (SCSIBase + SBC_DMA_NODRQ_OFFSET);
    294 	if (VIA2 == VIA2OFF) {
    295 		sc->sc_ienable = Via1Base + VIA2 * 0x2000 + vIER;
    296 		sc->sc_iflag   = Via1Base + VIA2 * 0x2000 + vIFR;
    297 	} else {
    298 		sc->sc_ienable = Via1Base + VIA2 * 0x2000 + rIER;
    299 		sc->sc_iflag   = Via1Base + VIA2 * 0x2000 + rIFR;
    300 	}
    301 
    302 	if (sc->sc_options)
    303 		printf(": options=%b", sc->sc_options, SBC_OPTIONS_BITS);
    304 	printf("\n");
    305 
    306 	/* Now enable SCSI interrupts through VIA2, if appropriate */
    307 	if (sc->sc_options & SBC_INTR)
    308 		sbc_intr_enable(ncr_sc);
    309 
    310 #ifdef SBC_DEBUG
    311 	if (sbc_debug)
    312 		printf("%s: softc=%p regs=%p\n", ncr_sc->sc_dev.dv_xname,
    313 		    sc, sc->sc_regs);
    314 	ncr_sc->sc_link.flags |= sbc_link_flags;
    315 #endif
    316 
    317 	/*
    318 	 *  Initialize the SCSI controller itself.
    319 	 */
    320 	ncr5380_init(ncr_sc);
    321 	ncr5380_reset_scsibus(ncr_sc);
    322 	config_found(self, &(ncr_sc->sc_link), sbc_print);
    323 }
    324 
    325 static int
    326 sbc_print(aux, name)
    327 	void *aux;
    328 	char *name;
    329 {
    330 	if (name != NULL)
    331 		printf("%s: scsibus ", name);
    332 	return UNCONF;
    333 }
    334 
    335 static void
    336 sbc_minphys(struct buf *bp)
    337 {
    338 	if (bp->b_bcount > MAX_DMA_LEN)
    339 		bp->b_bcount = MAX_DMA_LEN;
    340 	return (minphys(bp));
    341 }
    342 
    343 
    344 /***
    345  * General support for Mac-specific SCSI logic.
    346  ***/
    347 
    348 /* These are used in the following inline functions. */
    349 int sbc_wait_busy_timo = 1000 * 5000;	/* X2 = 10 S. */
    350 int sbc_ready_timo = 1000 * 5000;	/* X2 = 10 S. */
    351 int sbc_wait_dreq_timo = 1000 * 5000;	/* X2 = 10 S. */
    352 
    353 /* Return zero on success. */
    354 static __inline__ int
    355 sbc_wait_busy(sc)
    356 	struct ncr5380_softc *sc;
    357 {
    358 	register int timo = sbc_wait_busy_timo;
    359 	for (;;) {
    360 		if (SCI_BUSY(sc)) {
    361 			timo = 0;	/* return 0 */
    362 			break;
    363 		}
    364 		if (--timo < 0)
    365 			break;	/* return -1 */
    366 		delay(2);
    367 	}
    368 	return (timo);
    369 }
    370 
    371 static __inline__ int
    372 sbc_ready(sc)
    373 	struct ncr5380_softc *sc;
    374 {
    375 	register int timo = sbc_ready_timo;
    376 
    377 	for (;;) {
    378 		if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
    379 		    == (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
    380 			timo = 0;
    381 			break;
    382 		}
    383 		if (((*sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0)
    384 		    || (SCI_BUSY(sc) == 0)) {
    385 			timo = -1;
    386 			break;
    387 		}
    388 		if (--timo < 0)
    389 			break;	/* return -1 */
    390 		delay(2);
    391 	}
    392 	return (timo);
    393 }
    394 
    395 static __inline__ int
    396 sbc_wait_dreq(sc)
    397 	struct ncr5380_softc *sc;
    398 {
    399 	register int timo = sbc_wait_dreq_timo;
    400 
    401 	for (;;) {
    402 		if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
    403 		    == (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
    404 			timo = 0;
    405 			break;
    406 		}
    407 		if (--timo < 0)
    408 			break;	/* return -1 */
    409 		delay(2);
    410 	}
    411 	return (timo);
    412 }
    413 
    414 
    415 /***
    416  * Macintosh SCSI interrupt support routines.
    417  ***/
    418 
    419 void
    420 sbc_intr_enable(ncr_sc)
    421 	struct ncr5380_softc *ncr_sc;
    422 {
    423 	register struct sbc_softc *sc = (struct sbc_softc *) ncr_sc;
    424 	int s;
    425 
    426 	s = splhigh();
    427 	*sc->sc_ienable = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
    428 	splx(s);
    429 }
    430 
    431 void
    432 sbc_intr_disable(ncr_sc)
    433 	struct ncr5380_softc *ncr_sc;
    434 {
    435 	register struct sbc_softc *sc = (struct sbc_softc *) ncr_sc;
    436 	int s;
    437 
    438 	s = splhigh();
    439 	*sc->sc_ienable = (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
    440 	splx(s);
    441 }
    442 
    443 void
    444 sbc_irq_intr(p)
    445 	void *p;
    446 {
    447 	register struct ncr5380_softc *ncr_sc = p;
    448 	register int claimed = 0;
    449 
    450 	/* How we ever arrive here without IRQ set is a mystery... */
    451 	if (*ncr_sc->sci_csr & SCI_CSR_INT) {
    452 #ifdef SBC_DEBUG
    453 		if (sbc_debug & SBC_DB_INTR)
    454 			decode_5380_intr(ncr_sc);
    455 #endif
    456 		claimed = ncr5380_intr(ncr_sc);
    457 		if (!claimed) {
    458 			if (((*ncr_sc->sci_csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT)
    459 			    && ((*ncr_sc->sci_bus_csr & ~SCI_BUS_RST) == 0))
    460 				SCI_CLR_INTR(ncr_sc);	/* RST interrupt */
    461 #ifdef SBC_DEBUG
    462 			else {
    463 				printf("%s: spurious intr\n",
    464 				    ncr_sc->sc_dev.dv_xname);
    465 				SBC_BREAK;
    466 			}
    467 #endif
    468 		}
    469 	}
    470 }
    471 
    472 #ifdef SBC_DEBUG
    473 void
    474 decode_5380_intr(ncr_sc)
    475 	struct ncr5380_softc *ncr_sc;
    476 {
    477 	register u_char csr = *ncr_sc->sci_csr;
    478 	register u_char bus_csr = *ncr_sc->sci_bus_csr;
    479 
    480 	if (((csr & ~(SCI_CSR_PHASE_MATCH | SCI_CSR_ATN)) == SCI_CSR_INT) &&
    481 	    ((bus_csr & ~(SCI_BUS_MSG | SCI_BUS_CD | SCI_BUS_IO | SCI_BUS_DBP)) == SCI_BUS_SEL)) {
    482 		if (csr & SCI_BUS_IO)
    483 			printf("%s: reselect\n", ncr_sc->sc_dev.dv_xname);
    484 		else
    485 			printf("%s: select\n", ncr_sc->sc_dev.dv_xname);
    486 	} else if (((csr & ~SCI_CSR_ACK) == (SCI_CSR_DONE | SCI_CSR_INT)) &&
    487 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
    488 		printf("%s: dma eop\n", ncr_sc->sc_dev.dv_xname);
    489 	else if (((csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT) &&
    490 	    ((bus_csr & ~SCI_BUS_RST) == 0))
    491 		printf("%s: bus reset\n", ncr_sc->sc_dev.dv_xname);
    492 	else if (((csr & ~(SCI_CSR_DREQ | SCI_CSR_ATN | SCI_CSR_ACK)) == (SCI_CSR_PERR | SCI_CSR_INT | SCI_CSR_PHASE_MATCH)) &&
    493 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
    494 		printf("%s: parity error\n", ncr_sc->sc_dev.dv_xname);
    495 	else if (((csr & ~SCI_CSR_ATN) == SCI_CSR_INT) &&
    496 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_REQ | SCI_BUS_SEL)) == (SCI_BUS_BSY | SCI_BUS_REQ)))
    497 		printf("%s: phase mismatch\n", ncr_sc->sc_dev.dv_xname);
    498 	else if (((csr & ~SCI_CSR_PHASE_MATCH) == (SCI_CSR_INT | SCI_CSR_DISC)) &&
    499 	    (bus_csr == 0))
    500 		printf("%s: disconnect\n", ncr_sc->sc_dev.dv_xname);
    501 	else
    502 		printf("%s: unknown intr: csr=%x, bus_csr=%x\n",
    503 		    ncr_sc->sc_dev.dv_xname, csr, bus_csr);
    504 }
    505 #endif
    506 
    507 /***
    508  * The following code implements polled PDMA.
    509  ***/
    510 
    511 static	int
    512 sbc_pdma_out(ncr_sc, phase, count, data)
    513 	struct ncr5380_softc *ncr_sc;
    514 	int phase;
    515 	int count;
    516 	u_char *data;
    517 {
    518 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    519 	register volatile long *long_data = sc->sc_drq_addr;
    520 	register volatile u_char *byte_data = sc->sc_nodrq_addr;
    521 	register int len = count;
    522 
    523 	if (count < ncr_sc->sc_min_dma_len || (sc->sc_options & SBC_PDMA) == 0)
    524 		return ncr5380_pio_out(ncr_sc, phase, count, data);
    525 
    526 	if (sbc_wait_busy(ncr_sc) == 0) {
    527 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
    528 		*ncr_sc->sci_icmd |= SCI_ICMD_DATA;
    529 		*ncr_sc->sci_dma_send = 0;
    530 
    531 #define W1	*byte_data = *data++
    532 #define W4	*long_data = *((long*)data)++
    533 		while (len >= 64) {
    534 			if (sbc_ready(ncr_sc))
    535 				goto timeout;
    536 			W1;
    537 			if (sbc_ready(ncr_sc))
    538 				goto timeout;
    539 			W1;
    540 			if (sbc_ready(ncr_sc))
    541 				goto timeout;
    542 			W1;
    543 			if (sbc_ready(ncr_sc))
    544 				goto timeout;
    545 			W1;
    546 			if (sbc_ready(ncr_sc))
    547 				goto timeout;
    548 			W4; W4; W4; W4;
    549 			W4; W4; W4; W4;
    550 			W4; W4; W4; W4;
    551 			W4; W4; W4;
    552 			len -= 64;
    553 		}
    554 		while (len) {
    555 			if (sbc_ready(ncr_sc))
    556 				goto timeout;
    557 			W1;
    558 			len--;
    559 		}
    560 #undef  W1
    561 #undef  W4
    562 		if (sbc_wait_dreq(ncr_sc))
    563 			printf("%s: timeout waiting for DREQ.\n",
    564 			    ncr_sc->sc_dev.dv_xname);
    565 
    566 		*byte_data = 0;
    567 
    568 		SCI_CLR_INTR(ncr_sc);
    569 		*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    570 		*ncr_sc->sci_icmd = 0;
    571 	}
    572 	return count - len;
    573 
    574 timeout:
    575 	printf("%s: pdma_out: timeout len=%d count=%d\n",
    576 	    ncr_sc->sc_dev.dv_xname, len, count);
    577 	if ((*ncr_sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0) {
    578 		*ncr_sc->sci_icmd &= ~SCI_ICMD_DATA;
    579 		--len;
    580 	}
    581 
    582 	SCI_CLR_INTR(ncr_sc);
    583 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    584 	*ncr_sc->sci_icmd = 0;
    585 	return count - len;
    586 }
    587 
    588 static	int
    589 sbc_pdma_in(ncr_sc, phase, count, data)
    590 	struct ncr5380_softc *ncr_sc;
    591 	int phase;
    592 	int count;
    593 	u_char *data;
    594 {
    595 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    596 	register volatile long *long_data = sc->sc_drq_addr;
    597 	register volatile u_char *byte_data = sc->sc_nodrq_addr;
    598 	register int len = count;
    599 
    600 	if (count < ncr_sc->sc_min_dma_len || (sc->sc_options & SBC_PDMA) == 0)
    601 		return ncr5380_pio_in(ncr_sc, phase, count, data);
    602 
    603 	if (sbc_wait_busy(ncr_sc) == 0) {
    604 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
    605 		*ncr_sc->sci_icmd |= SCI_ICMD_DATA;
    606 		*ncr_sc->sci_irecv = 0;
    607 
    608 #define R4	*((long *)data)++ = *long_data
    609 #define R1	*data++ = *byte_data
    610 		while (len >= 1024) {
    611 			if (sbc_ready(ncr_sc))
    612 				goto timeout;
    613 			R4; R4; R4; R4; R4; R4; R4; R4;
    614 			R4; R4; R4; R4; R4; R4; R4; R4;
    615 			R4; R4; R4; R4; R4; R4; R4; R4;
    616 			R4; R4; R4; R4; R4; R4; R4; R4;		/* 128 */
    617 			if (sbc_ready(ncr_sc))
    618 				goto timeout;
    619 			R4; R4; R4; R4; R4; R4; R4; R4;
    620 			R4; R4; R4; R4; R4; R4; R4; R4;
    621 			R4; R4; R4; R4; R4; R4; R4; R4;
    622 			R4; R4; R4; R4; R4; R4; R4; R4;		/* 256 */
    623 			if (sbc_ready(ncr_sc))
    624 				goto timeout;
    625 			R4; R4; R4; R4; R4; R4; R4; R4;
    626 			R4; R4; R4; R4; R4; R4; R4; R4;
    627 			R4; R4; R4; R4; R4; R4; R4; R4;
    628 			R4; R4; R4; R4; R4; R4; R4; R4;		/* 384 */
    629 			if (sbc_ready(ncr_sc))
    630 				goto timeout;
    631 			R4; R4; R4; R4; R4; R4; R4; R4;
    632 			R4; R4; R4; R4; R4; R4; R4; R4;
    633 			R4; R4; R4; R4; R4; R4; R4; R4;
    634 			R4; R4; R4; R4; R4; R4; R4; R4;		/* 512 */
    635 			if (sbc_ready(ncr_sc))
    636 				goto timeout;
    637 			R4; R4; R4; R4; R4; R4; R4; R4;
    638 			R4; R4; R4; R4; R4; R4; R4; R4;
    639 			R4; R4; R4; R4; R4; R4; R4; R4;
    640 			R4; R4; R4; R4; R4; R4; R4; R4;		/* 640 */
    641 			if (sbc_ready(ncr_sc))
    642 				goto timeout;
    643 			R4; R4; R4; R4; R4; R4; R4; R4;
    644 			R4; R4; R4; R4; R4; R4; R4; R4;
    645 			R4; R4; R4; R4; R4; R4; R4; R4;
    646 			R4; R4; R4; R4; R4; R4; R4; R4;		/* 768 */
    647 			if (sbc_ready(ncr_sc))
    648 				goto timeout;
    649 			R4; R4; R4; R4; R4; R4; R4; R4;
    650 			R4; R4; R4; R4; R4; R4; R4; R4;
    651 			R4; R4; R4; R4; R4; R4; R4; R4;
    652 			R4; R4; R4; R4; R4; R4; R4; R4;		/* 896 */
    653 			if (sbc_ready(ncr_sc))
    654 				goto timeout;
    655 			R4; R4; R4; R4; R4; R4; R4; R4;
    656 			R4; R4; R4; R4; R4; R4; R4; R4;
    657 			R4; R4; R4; R4; R4; R4; R4; R4;
    658 			R4; R4; R4; R4; R4; R4; R4; R4;		/* 1024 */
    659 			len -= 1024;
    660 		}
    661 		while (len >= 128) {
    662 			if (sbc_ready(ncr_sc))
    663 				goto timeout;
    664 			R4; R4; R4; R4; R4; R4; R4; R4;
    665 			R4; R4; R4; R4; R4; R4; R4; R4;
    666 			R4; R4; R4; R4; R4; R4; R4; R4;
    667 			R4; R4; R4; R4; R4; R4; R4; R4;		/* 128 */
    668 			len -= 128;
    669 		}
    670 		while (len) {
    671 			if (sbc_ready(ncr_sc))
    672 				goto timeout;
    673 			R1;
    674 			len--;
    675 		}
    676 #undef R4
    677 #undef R1
    678 		SCI_CLR_INTR(ncr_sc);
    679 		*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    680 		*ncr_sc->sci_icmd = 0;
    681 	}
    682 	return count - len;
    683 
    684 timeout:
    685 	printf("%s: pdma_in: timeout len=%d count=%d\n",
    686 	    ncr_sc->sc_dev.dv_xname, len, count);
    687 
    688 	SCI_CLR_INTR(ncr_sc);
    689 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    690 	*ncr_sc->sci_icmd = 0;
    691 	return count - len;
    692 }
    693 
    694 
    695 /***
    696  * The following code implements interrupt-driven PDMA.
    697  ***/
    698 
    699 /*
    700  * This is the meat of the PDMA transfer.
    701  * When we get here, we shove data as fast as the mac can take it.
    702  * We depend on several things:
    703  *   * All macs after the Mac Plus that have a 5380 chip should have a general
    704  *     logic IC that handshakes data for blind transfers.
    705  *   * If the SCSI controller finishes sending/receiving data before we do,
    706  *     the same general logic IC will generate a /BERR for us in short order.
    707  *   * The fault address for said /BERR minus the base address for the
    708  *     transfer will be the amount of data that was actually written.
    709  *
    710  * We use the nofault flag and the setjmp/longjmp in locore.s so we can
    711  * detect and handle the bus error for early termination of a command.
    712  * This is usually caused by a disconnecting target.
    713  */
    714 void
    715 sbc_drq_intr(p)
    716 	void *p;
    717 {
    718 	extern	int		*nofault, mac68k_buserr_addr;
    719 	register struct sbc_softc *sc = (struct sbc_softc *) p;
    720 	register struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *) p;
    721 	register struct sci_req *sr = ncr_sc->sc_current;
    722 	register struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    723 	label_t			faultbuf;
    724 	volatile u_int32_t	*long_drq;
    725 	u_int32_t		*long_data;
    726 	volatile u_int8_t	*drq;
    727 	u_int8_t		*data;
    728 	register int		count;
    729 	int			dcount, resid;
    730 
    731 	/*
    732 	 * If we're not ready to xfer data, or have no more, just return.
    733 	 */
    734 	if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0 || dh->dh_len == 0)
    735 		return;
    736 
    737 #ifdef SBC_DEBUG
    738 	if (sbc_debug & SBC_DB_INTR)
    739 		printf("%s: drq intr, dh_len=0x%x, dh_flags=0x%x\n",
    740 		    ncr_sc->sc_dev.dv_xname, dh->dh_len, dh->dh_flags);
    741 #endif
    742 
    743 	/*
    744 	 * Setup for a possible bus error caused by SCSI controller
    745 	 * switching out of DATA-IN/OUT before we're done with the
    746 	 * current transfer.
    747 	 */
    748 	nofault = (int *) &faultbuf;
    749 
    750 	if (setjmp((label_t *) nofault)) {
    751 		nofault = (int *) 0;
    752 		count = ((  (u_long) mac68k_buserr_addr
    753 			  - (u_long) sc->sc_drq_addr));
    754 
    755 		if ((count < 0) || (count > dh->dh_len)) {
    756 			printf("%s: complete=0x%x (pending 0x%x)\n",
    757 			    ncr_sc->sc_dev.dv_xname, count, dh->dh_len);
    758 			panic("something is wrong");
    759 		}
    760 #ifdef SBC_DEBUG
    761 		if (sbc_debug & SBC_DB_INTR)
    762 			printf("%s: drq /berr, pending=0x%x, complete=0x%x\n",
    763 			   ncr_sc->sc_dev.dv_xname, dh->dh_len, count);
    764 #endif
    765 
    766 		dh->dh_addr += count;
    767 		dh->dh_len -= count;
    768 		mac68k_buserr_addr = 0;
    769 
    770 		return;
    771 	}
    772 
    773 	if (dh->dh_flags & SBC_DH_OUT) { /* Data Out */
    774 #ifdef notyet
    775 		/*
    776 		 * Get the source address aligned.
    777 		 */
    778 		resid =
    779 		    count = min(dh->dh_len, 4 - (((int) dh->dh_addr) & 0x3));
    780 		if (count && count < 4) {
    781 			data = (u_int8_t *) dh->dh_addr;
    782 			drq = (u_int8_t *) sc->sc_drq_addr;
    783 #define W1		*drq++ = *data++
    784 			while (count) {
    785 				W1; count--;
    786 			}
    787 #undef W1
    788 			dh->dh_addr += resid;
    789 			dh->dh_len -= resid;
    790 		}
    791 
    792 		/*
    793 		 * Get ready to start the transfer.
    794 		 */
    795 		while (dh->dh_len) {
    796 			dcount = count = min(dh->dh_len, MAX_DMA_LEN);
    797 			long_drq = (volatile u_int32_t *) sc->sc_drq_addr;
    798 			long_data = (u_int32_t *) dh->dh_addr;
    799 
    800 #define W4		*long_drq++ = *long_data++
    801 			while (count >= 64) {
    802 				W4; W4; W4; W4; W4; W4; W4; W4;
    803 				W4; W4; W4; W4; W4; W4; W4; W4; /*  64 */
    804 				count -= 64;
    805 			}
    806 			while (count >= 4) {
    807 				W4; count -= 4;
    808 			}
    809 #undef W4
    810 			data = (u_int8_t *) long_data;
    811 			drq = (u_int8_t *) long_drq;
    812 #define W1		*drq++ = *data++
    813 			while (count) {
    814 				W1; count--;
    815 			}
    816 #undef W1
    817 			dh->dh_len -= dcount;
    818 			dh->dh_addr += dcount;
    819 		}
    820 #else
    821 		while (dh->dh_len) {
    822 			dcount = count = min(dh->dh_len, MAX_DMA_LEN);
    823 			drq = (volatile u_int8_t *) sc->sc_drq_addr;
    824 			data = (u_int8_t *) dh->dh_addr;
    825 #define W1		*drq++ = *data++
    826 			while (count) {
    827 				W1; count--;
    828 			}
    829 #undef W1
    830 			dh->dh_len -= dcount;
    831 			dh->dh_addr += dcount;
    832 		}
    833 #endif
    834 
    835 		/* Wait for the GLUE to raise /ACK */
    836 		while ((*ncr_sc->sci_csr & SCI_CSR_ACK) == 0)
    837 			;
    838 
    839 		/*
    840 		 * If the SCSI bus is still busy, trigger a bus error
    841 		 * by writing another byte to the SBC.
    842 		 */
    843 		if (*ncr_sc->sci_bus_csr & SCI_BUS_BSY)
    844 			*((u_int8_t *) sc->sc_drq_addr) = 0;
    845 
    846 	} else {	/* Data In */
    847 		/*
    848 		 * Get the dest address aligned.
    849 		 */
    850 		resid =
    851 		    count = min(dh->dh_len, 4 - (((int) dh->dh_addr) & 0x3));
    852 		if (count && count < 4) {
    853 			data = (u_int8_t *) dh->dh_addr;
    854 			drq = (u_int8_t *) sc->sc_drq_addr;
    855 #define R1		*data++ = *drq++
    856 			while (count) {
    857 				R1; count--;
    858 			}
    859 #undef R1
    860 			dh->dh_addr += resid;
    861 			dh->dh_len -= resid;
    862 		}
    863 
    864 		/*
    865 		 * Get ready to start the transfer.
    866 		 */
    867 		while (dh->dh_len) {
    868 			dcount = count = min(dh->dh_len, MAX_DMA_LEN);
    869 			long_drq = (volatile u_int32_t *) sc->sc_drq_addr;
    870 			long_data = (u_int32_t *) dh->dh_addr;
    871 
    872 #define R4		*long_data++ = *long_drq++
    873 			while (count >= 512) {
    874 				if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0) {
    875 					nofault = (int *) 0;
    876 
    877 					dh->dh_addr += (dcount - count);
    878 					dh->dh_len -= (dcount - count);
    879 					return;
    880 				}
    881 				R4; R4; R4; R4; R4; R4; R4; R4;
    882 				R4; R4; R4; R4; R4; R4; R4; R4;	/* 64 */
    883 				R4; R4; R4; R4; R4; R4; R4; R4;
    884 				R4; R4; R4; R4; R4; R4; R4; R4;	/* 128 */
    885 				R4; R4; R4; R4; R4; R4; R4; R4;
    886 				R4; R4; R4; R4; R4; R4; R4; R4;
    887 				R4; R4; R4; R4; R4; R4; R4; R4;
    888 				R4; R4; R4; R4; R4; R4; R4; R4;	/* 256 */
    889 				R4; R4; R4; R4; R4; R4; R4; R4;
    890 				R4; R4; R4; R4; R4; R4; R4; R4;
    891 				R4; R4; R4; R4; R4; R4; R4; R4;
    892 				R4; R4; R4; R4; R4; R4; R4; R4;
    893 				R4; R4; R4; R4; R4; R4; R4; R4;
    894 				R4; R4; R4; R4; R4; R4; R4; R4;
    895 				R4; R4; R4; R4; R4; R4; R4; R4;
    896 				R4; R4; R4; R4; R4; R4; R4; R4;	/* 512 */
    897 				count -= 512;
    898 			}
    899 			while (count >= 4) {
    900 				R4; count -= 4;
    901 			}
    902 #undef R4
    903 			data = (u_int8_t *) long_data;
    904 			drq = (u_int8_t *) long_drq;
    905 #define R1		*data++ = *drq++
    906 			while (count) {
    907 				R1; count--;
    908 			}
    909 #undef R1
    910 			dh->dh_len -= dcount;
    911 			dh->dh_addr += dcount;
    912 		}
    913 	}
    914 
    915 	/*
    916 	 * OK.  No bus error occurred above.  Clear the nofault flag
    917 	 * so we no longer short-circuit bus errors.
    918 	 */
    919 	nofault = (int *) 0;
    920 
    921 #ifdef SBC_DEBUG
    922 	if (sbc_debug & (SBC_DB_REG | SBC_DB_INTR))
    923 		printf("%s: drq intr complete: csr=0x%x, bus_csr=0x%x\n",
    924 		    ncr_sc->sc_dev.dv_xname, *ncr_sc->sci_csr,
    925 		    *ncr_sc->sci_bus_csr);
    926 #endif
    927 }
    928 
    929 void
    930 sbc_dma_alloc(ncr_sc)
    931 	struct ncr5380_softc *ncr_sc;
    932 {
    933 	struct sbc_softc *sc = (struct sbc_softc *) ncr_sc;
    934 	struct sci_req *sr = ncr_sc->sc_current;
    935 	struct scsi_xfer *xs = sr->sr_xs;
    936 	struct sbc_pdma_handle *dh;
    937 	int		i, xlen;
    938 
    939 #ifdef DIAGNOSTIC
    940 	if (sr->sr_dma_hand != NULL)
    941 		panic("sbc_dma_alloc: already have PDMA handle");
    942 #endif
    943 
    944 	/* Polled transfers shouldn't allocate a PDMA handle. */
    945 	if (sr->sr_flags & SR_IMMED)
    946 		return;
    947 
    948 #ifndef SBCTEST
    949 	/* XXX - we don't trust PDMA writes yet! */
    950 	if (xs->flags & SCSI_DATA_OUT)
    951 		return;
    952 #endif
    953 
    954 	xlen = ncr_sc->sc_datalen;
    955 
    956 	/* Make sure our caller checked sc_min_dma_len. */
    957 	if (xlen < MIN_DMA_LEN)
    958 		panic("sbc_dma_alloc: len=0x%x\n", xlen);
    959 
    960 	/*
    961 	 * Find free PDMA handle.  Guaranteed to find one since we
    962 	 * have as many PDMA handles as the driver has processes.
    963 	 * (instances?)
    964 	 */
    965 	 for (i = 0; i < SCI_OPENINGS; i++) {
    966 		if ((sc->sc_pdma[i].dh_flags & SBC_DH_BUSY) == 0)
    967 			goto found;
    968 	}
    969 	panic("sbc: no free PDMA handles");
    970 found:
    971 	dh = &sc->sc_pdma[i];
    972 	dh->dh_flags = SBC_DH_BUSY;
    973 	dh->dh_addr = ncr_sc->sc_dataptr;
    974 	dh->dh_len = xlen;
    975 
    976 	/* Copy the 'write' flag for convenience. */
    977 	if (xs->flags & SCSI_DATA_OUT)
    978 		dh->dh_flags |= SBC_DH_OUT;
    979 
    980 	sr->sr_dma_hand = dh;
    981 }
    982 
    983 void
    984 sbc_dma_free(ncr_sc)
    985 	struct ncr5380_softc *ncr_sc;
    986 {
    987 	struct sci_req *sr = ncr_sc->sc_current;
    988 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    989 
    990 #ifdef DIAGNOSTIC
    991 	if (sr->sr_dma_hand == NULL)
    992 		panic("sbc_dma_free: no DMA handle");
    993 #endif
    994 
    995 	if (ncr_sc->sc_state & NCR_DOINGDMA)
    996 		panic("sbc_dma_free: free while in progress");
    997 
    998 	if (dh->dh_flags & SBC_DH_BUSY) {
    999 		dh->dh_flags = 0;
   1000 		dh->dh_addr = NULL;
   1001 		dh->dh_len = 0;
   1002 	}
   1003 	sr->sr_dma_hand = NULL;
   1004 }
   1005 
   1006 void
   1007 sbc_dma_poll(ncr_sc)
   1008 	struct ncr5380_softc *ncr_sc;
   1009 {
   1010 	struct sci_req *sr = ncr_sc->sc_current;
   1011 
   1012 	/*
   1013 	 * We shouldn't arrive here; if SR_IMMED is set, then
   1014 	 * dma_alloc() should have refused to allocate a handle
   1015 	 * for the transfer.  This forces the polled PDMA code
   1016 	 * to handle the request...
   1017 	 */
   1018 #ifdef SBC_DEBUG
   1019 	if (sbc_debug & SBC_DB_DMA)
   1020 		printf("%s: lost DRQ interrupt?\n", ncr_sc->sc_dev.dv_xname);
   1021 #endif
   1022 	sr->sr_flags |= SR_OVERDUE;
   1023 }
   1024 
   1025 void
   1026 sbc_dma_setup(ncr_sc)
   1027 	struct ncr5380_softc *ncr_sc;
   1028 {
   1029 	/* Not needed; we don't have real DMA */
   1030 }
   1031 
   1032 void
   1033 sbc_dma_start(ncr_sc)
   1034 	struct ncr5380_softc *ncr_sc;
   1035 {
   1036 	register struct sbc_softc *sc = (struct sbc_softc *) ncr_sc;
   1037 	struct sci_req *sr = ncr_sc->sc_current;
   1038 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
   1039 
   1040 	/*
   1041 	 * Match bus phase, clear pending interrupts, set DMA mode, and
   1042 	 * assert data bus (for writing only), then start the transfer.
   1043 	 */
   1044 	if (dh->dh_flags & SBC_DH_OUT) {
   1045 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
   1046 		SCI_CLR_INTR(ncr_sc);
   1047 		*sc->sc_iflag = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
   1048 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
   1049 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
   1050 		*ncr_sc->sci_dma_send = 0;
   1051 	} else {
   1052 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
   1053 		SCI_CLR_INTR(ncr_sc);
   1054 		*sc->sc_iflag = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
   1055 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
   1056 		*ncr_sc->sci_icmd = 0;
   1057 		*ncr_sc->sci_irecv = 0;
   1058 	}
   1059 	ncr_sc->sc_state |= NCR_DOINGDMA;
   1060 
   1061 #ifdef SBC_DEBUG
   1062 	if (sbc_debug & SBC_DB_DMA)
   1063 		printf("%s: PDMA started, va=%p, len=0x%x\n",
   1064 		    ncr_sc->sc_dev.dv_xname, dh->dh_addr, dh->dh_len);
   1065 #endif
   1066 }
   1067 
   1068 void
   1069 sbc_dma_eop(ncr_sc)
   1070 	struct ncr5380_softc *ncr_sc;
   1071 {
   1072 	/* Not used; the EOP pin is wired high (GMFH, pp. 389-390) */
   1073 }
   1074 
   1075 void
   1076 sbc_dma_stop(ncr_sc)
   1077 	struct ncr5380_softc *ncr_sc;
   1078 {
   1079 	register struct sbc_softc *sc = (struct sbc_softc *) ncr_sc;
   1080 	struct sci_req *sr = ncr_sc->sc_current;
   1081 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
   1082 	register int ntrans;
   1083 
   1084 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
   1085 #ifdef SBC_DEBUG
   1086 		if (sbc_debug & SBC_DB_DMA)
   1087 			printf("%s: dma_stop: DMA not running\n",
   1088 			    ncr_sc->sc_dev.dv_xname);
   1089 #endif
   1090 		return;
   1091 	}
   1092 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
   1093 
   1094 	if ((ncr_sc->sc_state & NCR_ABORTING) == 0) {
   1095 		ntrans = ncr_sc->sc_datalen - dh->dh_len;
   1096 
   1097 #ifdef SBC_DEBUG
   1098 		if (sbc_debug & SBC_DB_DMA)
   1099 			printf("%s: dma_stop: ntrans=0x%x\n",
   1100 			    ncr_sc->sc_dev.dv_xname, ntrans);
   1101 #endif
   1102 
   1103 		if (ntrans > ncr_sc->sc_datalen)
   1104 			panic("sbc_dma_stop: excess transfer\n");
   1105 
   1106 		/* Adjust data pointer */
   1107 		ncr_sc->sc_dataptr += ntrans;
   1108 		ncr_sc->sc_datalen -= ntrans;
   1109 
   1110 		/* Clear any pending interrupts. */
   1111 		SCI_CLR_INTR(ncr_sc);
   1112 		*sc->sc_iflag = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
   1113 	}
   1114 
   1115 	/* Put SBIC back into PIO mode. */
   1116 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
   1117 	*ncr_sc->sci_icmd = 0;
   1118 
   1119 #ifdef SBC_DEBUG
   1120 	if (sbc_debug & SBC_DB_REG)
   1121 		printf("%s: dma_stop: csr=0x%x, bus_csr=0x%x\n",
   1122 		    ncr_sc->sc_dev.dv_xname, *ncr_sc->sci_csr,
   1123 		    *ncr_sc->sci_bus_csr);
   1124 #endif
   1125 }
   1126