Home | History | Annotate | Line # | Download | only in vsa
ncr.c revision 1.6
      1 /*	$NetBSD: ncr.c,v 1.6 1997/02/12 17:55:54 ragge Exp $	*/
      2 
      3 /* #define DEBUG	/* */
      4 /* #define TRACE	/* */
      5 /* #define POLL_MODE	/* */
      6 #define USE_VMAPBUF
      7 
      8 /*
      9  * Copyright (c) 1995 David Jones, Gordon W. Ross
     10  * Copyright (c) 1994 Adam Glass
     11  * All rights reserved.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. The name of the authors may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  * 4. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *	This product includes software developed by
     26  *	Adam Glass, David Jones, and Gordon Ross
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     30  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     31  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     33  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     37  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * This file contains only the machine-dependent parts of the
     42  * Sun3 SCSI driver.  (Autoconfig stuff and DMA functions.)
     43  * The machine-independent parts are in ncr5380sbc.c
     44  *
     45  * Supported hardware includes:
     46  * Sun SCSI-3 on OBIO (Sun3/50,Sun3/60)
     47  * Sun SCSI-3 on VME (Sun3/160,Sun3/260)
     48  *
     49  * Could be made to support the Sun3/E if someone wanted to.
     50  *
     51  * Note:  Both supported variants of the Sun SCSI-3 adapter have
     52  * some really unusual "features" for this driver to deal with,
     53  * generally related to the DMA engine.	 The OBIO variant will
     54  * ignore any attempt to write the FIFO count register while the
     55  * SCSI bus is in DATA_IN or DATA_OUT phase.  This is dealt with
     56  * by setting the FIFO count early in COMMAND or MSG_IN phase.
     57  *
     58  * The VME variant has a bit to enable or disable the DMA engine,
     59  * but that bit also gates the interrupt line from the NCR5380!
     60  * Therefore, in order to get any interrupt from the 5380, (i.e.
     61  * for reselect) one must clear the DMA engine transfer count and
     62  * then enable DMA.  This has the further complication that you
     63  * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
     64  * we have to turn DMA back off before we even look at the 5380.
     65  *
     66  * What wonderfully whacky hardware this is!
     67  *
     68  * Credits, history:
     69  *
     70  * David Jones wrote the initial version of this module, which
     71  * included support for the VME adapter only. (no reselection).
     72  *
     73  * Gordon Ross added support for the OBIO adapter, and re-worked
     74  * both the VME and OBIO code to support disconnect/reselect.
     75  * (Required figuring out the hardware "features" noted above.)
     76  *
     77  * The autoconfiguration boilerplate came from Adam Glass.
     78  *
     79  * VS2000:
     80  */
     81 
     82 #include <sys/param.h>
     83 #include <sys/systm.h>
     84 #include <sys/kernel.h>
     85 #include <sys/conf.h>
     86 #include <sys/file.h>
     87 #include <sys/stat.h>
     88 #include <sys/ioctl.h>
     89 #include <sys/buf.h>
     90 #include <sys/proc.h>
     91 #include <sys/user.h>
     92 #include <sys/map.h>
     93 #include <sys/device.h>
     94 #include <sys/dkstat.h>
     95 #include <sys/disklabel.h>
     96 #include <sys/disk.h>
     97 #include <sys/syslog.h>
     98 
     99 /* #include <sys/errno.h> */
    100 
    101 #include <scsi/scsi_all.h>
    102 #include <scsi/scsi_debug.h>
    103 #include <scsi/scsiconf.h>
    104 
    105 #include <machine/uvax.h>
    106 #include <machine/ka410.h>
    107 #include <machine/ka43.h>
    108 #include <machine/vsbus.h>	/* struct confargs */
    109 
    110 #include <dev/ic/ncr5380reg.h>
    111 #include <dev/ic/ncr5380var.h>
    112 
    113 #define trace(x)
    114 #define debug(x)
    115 
    116 #ifndef NCR5380_CSRBITS
    117 #define NCR5380_CSRBITS \
    118 	"\020\010DEND\007DREQ\006PERR\005IREQ\004MTCH\003DCON\002ATN\001ACK"
    119 #endif
    120 
    121 #ifndef NCR5380_BUSCSRBITS
    122 #define NCR5380_BUSCSRBITS \
    123 	"\020\010RST\007BSY\006REQ\005MSG\004C/D\003I/O\002SEL\001DBP"
    124 #endif
    125 
    126 #include "ncr.h"
    127 
    128 #ifdef DDB
    129 #define integrate
    130 #else
    131 #define integrate static
    132 #endif
    133 
    134 /*
    135  * Transfers smaller than this are done using PIO
    136  * (on assumption they're not worth DMA overhead)
    137  */
    138 #define MIN_DMA_LEN 128
    139 
    140 /*
    141  * Transfers lager than 65535 bytes need to be split-up.
    142  * (Some of the FIFO logic has only 16 bits counters.)
    143  * Make the size an integer multiple of the page size
    144  * to avoid buf/cluster remap problems.	 (paranoid?)
    145  *
    146  * bertram: VS2000 has an DMA-area which is 16KB, thus
    147  * have a maximum DMA-size of 16KB...
    148  */
    149 #ifdef DMA_SHARED
    150 #define MAX_DMA_LEN	0x2000		/* (8 * 1024) */
    151 #define DMA_ADDR_HBYTE	0x20
    152 #define DMA_ADDR_LBYTE	0x00
    153 #else
    154 #define MAX_DMA_LEN	0x4000		/* (16 * 1024) */
    155 #define DMA_ADDR_HBYTE	0x00
    156 #define DMA_ADDR_LBYTE	0x00
    157 #endif
    158 
    159 #ifdef	DEBUG
    160 int si_debug = 3;
    161 static int si_link_flags = 0 /* | SDEV_DB2 */ ;
    162 #endif
    163 
    164 /*
    165  * This structure is used to keep track of mappedpwd DMA requests.
    166  * Note: combined the UDC command block with this structure, so
    167  * the array of these has to be in DVMA space.
    168  */
    169 struct si_dma_handle {
    170 	int		dh_flags;
    171 #define SIDH_BUSY	1		/* This DH is in use */
    172 #define SIDH_OUT	2		/* DMA does data out (write) */
    173 #define SIDH_PHYS	4
    174 #define SIDH_DONE	8
    175 	u_char *	dh_addr;	/* KVA of start of buffer */
    176 	int		dh_maplen;	/* Length of KVA mapping. */
    177 	u_char *	dh_dvma;	/* VA of buffer in DVMA space */
    178 	int		dh_xlen;
    179 };
    180 
    181 /*
    182  * The first structure member has to be the ncr5380_softc
    183  * so we can just cast to go back and fourth between them.
    184  */
    185 struct si_softc {
    186 	struct ncr5380_softc	ncr_sc;
    187 	volatile struct si_regs *sc_regs;	/* do we really need this? */
    188 
    189 	struct si_dma_handle	*sc_dma;
    190 	struct confargs		*sc_cfargs;
    191 
    192 	int	sc_xflags;	/* ka410/ka43: resid, sizeof(areg) */
    193 
    194 	char	*sc_dbase;
    195 	int	sc_dsize;
    196 
    197 	volatile char	*sc_dareg;
    198 	volatile short	*sc_dcreg;
    199 	volatile char	*sc_ddreg;
    200 	volatile int	sc_dflags;
    201 
    202 #define VSDMA_LOCKED	0x80	/* */
    203 #define VSDMA_WANTED	0x40	/* */
    204 #define VSDMA_IWANTED	0x20
    205 #define VSDMA_BLOCKED	0x10
    206 #define VSDMA_DMABUSY	0x08	/* DMA in progress */
    207 #define VSDMA_REGBUSY	0x04	/* accessing registers */
    208 #define VSDMA_WRBUF	0x02	/* writing to bounce-buffer */
    209 #define VSDMA_RDBUF	0x01	/* reading from bounce-buffer */
    210 
    211 #define VSDMA_STATUS	0xF0
    212 #define VSDMA_LCKTYPE	0x0F
    213 
    214 #ifdef POLL_MODE
    215 	volatile u_char *intreq;
    216 	volatile u_char *intclr;
    217 	volatile u_char *intmsk;
    218 	volatile int	intbit;
    219 #endif
    220 };
    221 
    222 extern int cold;	/* enable polling while cold-flag set */
    223 
    224 /* Options.  Interesting values are: 1,3,7 */
    225 int si_options = 3;	/* bertram: 3 or 7 ??? */
    226 #define SI_ENABLE_DMA	1	/* Use DMA (maybe polled) */
    227 #define SI_DMA_INTR	2	/* DMA completion interrupts */
    228 #define SI_DO_RESELECT	4	/* Allow disconnect/reselect */
    229 
    230 #define DMA_DIR_IN  1
    231 #define DMA_DIR_OUT 0
    232 
    233 /* How long to wait for DMA before declaring an error. */
    234 int si_dma_intr_timo = 500;	/* ticks (sec. X 100) */
    235 
    236 integrate char si_name[] = "ncr";
    237 integrate int	si_match();
    238 integrate void	si_attach();
    239 integrate int	si_intr __P((void *));
    240 
    241 integrate void	si_minphys __P((struct buf *bp));
    242 integrate void	si_reset_adapter __P((struct ncr5380_softc *sc));
    243 
    244 void si_dma_alloc __P((struct ncr5380_softc *));
    245 void si_dma_free __P((struct ncr5380_softc *));
    246 void si_dma_poll __P((struct ncr5380_softc *));
    247 
    248 void si_intr_on __P((struct ncr5380_softc *));
    249 void si_intr_off __P((struct ncr5380_softc *));
    250 
    251 int si_dmaLockBus __P((struct ncr5380_softc *, int));
    252 int si_dmaToggleLock __P((struct ncr5380_softc *, int, int));
    253 int si_dmaReleaseBus __P((struct ncr5380_softc *, int));
    254 
    255 void si_dma_setup __P((struct ncr5380_softc *));
    256 void si_dma_start __P((struct ncr5380_softc *));
    257 void si_dma_eop __P((struct ncr5380_softc *));
    258 void si_dma_stop __P((struct ncr5380_softc *));
    259 
    260 static struct scsi_adapter	si_ops = {
    261 	ncr5380_scsi_cmd,		/* scsi_cmd()		*/
    262 	si_minphys,			/* scsi_minphys()	*/
    263 	NULL,				/* open_target_lu()	*/
    264 	NULL,				/* close_target_lu()	*/
    265 };
    266 
    267 /* This is copied from julian's bt driver */
    268 /* "so we have a default dev struct for our link struct." */
    269 static struct scsi_device si_dev = {
    270 	NULL,		/* Use default error handler.	    */
    271 	NULL,		/* Use default start handler.		*/
    272 	NULL,		/* Use default async handler.	    */
    273 	NULL,		/* Use default "done" routine.	    */
    274 };
    275 
    276 
    277 struct cfdriver ncr_cd = {
    278 	NULL, si_name, DV_DULL
    279 };
    280 struct cfattach ncr_ca = {
    281 	sizeof(struct si_softc), si_match, si_attach,
    282 };
    283 
    284 void
    285 dk_establish(p,q)
    286 	struct disk *p;
    287 	struct device *q;
    288 {
    289 #if 0
    290 	printf ("faking dk_establish()...\n");
    291 #endif
    292 }
    293 
    294 
    295 integrate int
    296 si_match(parent, match, aux)
    297 	struct device	*parent;
    298 	void		*match, *aux;
    299 {
    300 	struct cfdata	*cf = match;
    301 	struct confargs *ca = aux;
    302 
    303 	trace(("ncr_match(0x%x, %d, %s)\n", parent, cf->cf_unit, ca->ca_name));
    304 
    305 	if (strcmp(ca->ca_name, "ncr") &&
    306 	    strcmp(ca->ca_name, "ncr5380") &&
    307 	    strcmp(ca->ca_name, "NCR5380"))
    308 		return (0);
    309 
    310 	/*
    311 	 * we just define it being there ...
    312 	 */
    313 	return (1);
    314 }
    315 
    316 integrate void
    317 si_set_portid(pid,port)
    318 	int pid;
    319 	int port;
    320 {
    321 	struct {
    322 	  u_long    :2;
    323 	  u_long id0:3;
    324 	  u_long id1:3;
    325 	  u_long    :26;
    326 	} *p;
    327 
    328 #ifdef DEBUG
    329 	int *ip;
    330 	ip = (void*)uvax_phys2virt(KA410_SCSIPORT);
    331 	p = (void*)uvax_phys2virt(KA410_SCSIPORT);
    332 	printf("scsi-id: (%x/%d) %d / %d\n", *ip, *ip, p->id0, p->id1);
    333 #endif
    334 
    335 	p = (void*)uvax_phys2virt(KA410_SCSIPORT);
    336 	switch (port) {
    337 	case 0:
    338 		p->id0 = pid;
    339 		printf(": scsi-id %d\n", p->id0);
    340 		break;
    341 	case 1:
    342 		p->id1 = pid;
    343 		printf(": scsi-id %d\n", p->id1);
    344 		break;
    345 	default:
    346 		printf("invalid port-number %d\n", port);
    347 	}
    348 }
    349 
    350 integrate void
    351 si_attach(parent, self, aux)
    352 	struct device	*parent, *self;
    353 	void		*aux;
    354 {
    355 	struct si_softc *sc = (struct si_softc *) self;
    356 	struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *)sc;
    357 	volatile struct si_regs *regs;
    358 	struct confargs *ca = aux;
    359 	int i;
    360 	int *ip = aux;;
    361 
    362 	trace (("ncr_attach(0x%x, 0x%x, %s)\n", parent, self, ca->ca_name));
    363 
    364 	/*
    365 	 *
    366 	 */
    367 #ifdef POLL_MODE
    368 	sc->intreq = (void*)uvax_phys2virt(KA410_INTREQ);
    369 	sc->intmsk = (void*)uvax_phys2virt(KA410_INTMSK);
    370 	sc->intclr = (void*)uvax_phys2virt(KA410_INTCLR);
    371 	sc->intbit = ca->ca_intbit;
    372 #endif
    373 
    374 	sc->sc_cfargs = ca;	/* needed for interrupt-setup */
    375 
    376 	regs = (void*)uvax_phys2virt(ca->ca_ioaddr);
    377 
    378 	sc->sc_dareg = (void*)uvax_phys2virt(ca->ca_dareg);
    379 	sc->sc_dcreg = (void*)uvax_phys2virt(ca->ca_dcreg);
    380 	sc->sc_ddreg = (void*)uvax_phys2virt(ca->ca_ddreg);
    381 	sc->sc_dbase = (void*)uvax_phys2virt(ca->ca_dbase);
    382 	sc->sc_dsize = ca->ca_dsize;
    383 	sc->sc_dflags = 4;	/* XXX */
    384 	sc->sc_xflags = ca->ca_dflag;	/* should/will be renamed */
    385 	/*
    386 	 * Fill in the prototype scsi_link.
    387 	 */
    388 	ncr_sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
    389 	ncr_sc->sc_link.adapter_softc = sc;
    390 	ncr_sc->sc_link.adapter_target = ca->ca_idval;
    391 	ncr_sc->sc_link.adapter = &si_ops;
    392 	ncr_sc->sc_link.device = &si_dev;
    393 
    394 	si_set_portid(ca->ca_idval, ncr_sc->sc_dev.dv_unit);
    395 
    396 	/*
    397 	 * Initialize fields used by the MI code
    398 	 */
    399 	ncr_sc->sci_r0 = (void*)&regs->sci.sci_r0;
    400 	ncr_sc->sci_r1 = (void*)&regs->sci.sci_r1;
    401 	ncr_sc->sci_r2 = (void*)&regs->sci.sci_r2;
    402 	ncr_sc->sci_r3 = (void*)&regs->sci.sci_r3;
    403 	ncr_sc->sci_r4 = (void*)&regs->sci.sci_r4;
    404 	ncr_sc->sci_r5 = (void*)&regs->sci.sci_r5;
    405 	ncr_sc->sci_r6 = (void*)&regs->sci.sci_r6;
    406 	ncr_sc->sci_r7 = (void*)&regs->sci.sci_r7;
    407 
    408 	/*
    409 	 * MD function pointers used by the MI code.
    410 	 */
    411 	ncr_sc->sc_pio_out = ncr5380_pio_out;
    412 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
    413 	ncr_sc->sc_dma_alloc = si_dma_alloc;
    414 	ncr_sc->sc_dma_free  = si_dma_free;
    415 	ncr_sc->sc_dma_poll  = si_dma_poll;	/* si_dma_poll not used! */
    416 	ncr_sc->sc_intr_on   = si_intr_on;	/* vsbus_unlockDMA; */
    417 	ncr_sc->sc_intr_off  = si_intr_off;	/* vsbus_lockDMA; */
    418 
    419 	ncr_sc->sc_dma_setup = NULL;		/* si_dma_setup not used! */
    420 	ncr_sc->sc_dma_start = si_dma_start;
    421 	ncr_sc->sc_dma_eop   = NULL;
    422 	ncr_sc->sc_dma_stop  = si_dma_stop;
    423 
    424 	ncr_sc->sc_flags = 0;
    425 	if (si_options & SI_DO_RESELECT)
    426 		ncr_sc->sc_flags |= NCR5380_PERMIT_RESELECT;
    427 	if ((si_options & SI_DMA_INTR) == 0)
    428 		ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
    429 	ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
    430 
    431 	/*
    432 	 * Initialize fields used only here in the MD code.
    433 	 */
    434 	i = SCI_OPENINGS * sizeof(struct si_dma_handle);
    435 	sc->sc_dma = (struct si_dma_handle *) malloc(i);
    436 	if (sc->sc_dma == NULL)
    437 		panic("si: dvma_malloc failed\n");
    438 	for (i = 0; i < SCI_OPENINGS; i++)
    439 		sc->sc_dma[i].dh_flags = 0;
    440 
    441 	sc->sc_regs = regs;
    442 
    443 #ifdef	DEBUG
    444 	if (si_debug)
    445 		printf("si: Set TheSoftC=%x TheRegs=%x\n", sc, regs);
    446 	ncr_sc->sc_link.flags |= si_link_flags;
    447 #endif
    448 
    449 	/*
    450 	 *  Initialize si board itself.
    451 	 */
    452 	si_reset_adapter(ncr_sc);
    453 	ncr5380_init(ncr_sc);
    454 	ncr5380_reset_scsibus(ncr_sc);
    455 	config_found(self, &(ncr_sc->sc_link), scsiprint);
    456 
    457 	/*
    458 	 * Now ready for interrupts.
    459 	 */
    460 	vsbus_intr_register(sc->sc_cfargs, si_intr, (void *)sc);
    461 	vsbus_intr_enable(sc->sc_cfargs);
    462 }
    463 
    464 integrate void
    465 si_minphys(struct buf *bp)
    466 {
    467 	debug(("minphys: blkno=%d, bcount=%d, data=0x%x, flags=%x\n",
    468 	      bp->b_blkno, bp->b_bcount, bp->b_data, bp->b_flags));
    469 
    470 	if (bp->b_bcount > MAX_DMA_LEN) {
    471 #ifdef	DEBUG
    472 		if (si_debug) {
    473 			printf("si_minphys len = 0x%x.\n", bp->b_bcount);
    474 #ifdef DDB
    475 			Debugger();
    476 #endif
    477 		}
    478 #endif
    479 		bp->b_bcount = MAX_DMA_LEN;
    480 	}
    481 	return (minphys(bp));
    482 }
    483 
    484 
    485 #define CSR_WANT (SI_CSR_SBC_IP | SI_CSR_DMA_IP | \
    486 	SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR )
    487 
    488 static int si_intrCount = 0;
    489 static int lastCSR = 0;
    490 
    491 integrate int
    492 si_intr(arg)
    493 	void *arg;
    494 {
    495 	struct ncr5380_softc *ncr_sc = arg;
    496 	struct si_softc *sc = arg;
    497 	int count, claimed;
    498 
    499 	count = ++si_intrCount;
    500 	trace(("%s: si-intr(%d).....\n", ncr_sc->sc_dev.dv_xname, count));
    501 
    502 #ifdef DEBUG
    503 	/*
    504 	 * Each DMA interrupt is followed by one spurious(?) interrupt.
    505 	 * if (ncr_sc->sc_state & NCR_WORKING == 0) we know, that the
    506 	 * interrupt was not claimed by the higher-level routine, so that
    507 	 * it might be save to ignore these...
    508 	 */
    509 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    510 		printf("spurious(%d): %x, %d, status=%b\n", count,
    511 		       sc->sc_dflags, ncr_sc->sc_ncmds,
    512 		       *ncr_sc->sci_csr, NCR5380_CSRBITS);
    513 	}
    514 #endif
    515 	/*
    516 	 * If there was a DMA operation in progress, now it's no longer
    517 	 * active, since whatever caused the interrupt also interrupted
    518 	 * the DMA operation. Thus accessing the registers now doesn't
    519 	 * harm anything which is not yet broken...
    520 	 */
    521 	debug(("si_intr(status: %x, dma-count: %d)\n",
    522 	       *ncr_sc->sci_csr, *sc->sc_dcreg));
    523 
    524 	/*
    525 	 * First check for DMA errors / incomplete transfers
    526 	 * If operation was read/data-in, the copy data from buffer
    527 	 */
    528 	if (ncr_sc->sc_state & NCR_DOINGDMA) {
    529 		struct sci_req *sr = ncr_sc->sc_current;
    530 		struct si_dma_handle *dh = sr->sr_dma_hand;
    531 		int resid, ntrans;
    532 
    533 		resid = *sc->sc_dcreg;
    534 		if (resid == 1 && sc->sc_xflags) {
    535 		  debug(("correcting resid...\n"));
    536 		  resid = 0;
    537 		}
    538 		ntrans = dh->dh_xlen + resid;
    539 		if (resid == 0) {
    540 			if ((dh->dh_flags & SIDH_OUT) == 0) {
    541 				si_dmaToggleLock(ncr_sc,
    542 						 VSDMA_DMABUSY, VSDMA_RDBUF);
    543 				bcopy(sc->sc_dbase, dh->dh_dvma, ntrans);
    544 				si_dmaToggleLock(ncr_sc,
    545 						 VSDMA_RDBUF, VSDMA_DMABUSY);
    546 				dh->dh_flags |= SIDH_DONE;
    547 			}
    548 		}
    549 		else {
    550 #ifdef DEBUG
    551 			int csr = *ncr_sc->sci_csr;
    552 			printf("DMA incomplete (%d/%d) status = %b\n",
    553 			       ntrans, resid, csr, NCR5380_CSRBITS);
    554 			if(csr != lastCSR) {
    555 				int k = (csr & ~lastCSR) | (~csr & lastCSR);
    556 				debug(("Changed status bits: %b\n",
    557 				       k, NCR5380_CSRBITS));
    558 				lastCSR = csr & 0xFF;
    559 			}
    560 #endif
    561 			printf("DMA incomplete: ntrans=%d/%d, lock=%x\n",
    562 			       ntrans, dh->dh_xlen, sc->sc_dflags);
    563 			ncr_sc->sc_state |= NCR_ABORTING;
    564 		}
    565 
    566 		if ((sc->sc_dflags & VSDMA_BLOCKED) == 0) {
    567 			printf("not blocked during DMA.\n");
    568 		}
    569 		sc->sc_dflags &= ~VSDMA_BLOCKED;
    570 		si_dmaReleaseBus(ncr_sc, VSDMA_DMABUSY);
    571 	}
    572 	if ((sc->sc_dflags & VSDMA_BLOCKED) != 0) {
    573 		printf("blocked while not doing DMA.\n");
    574 		sc->sc_dflags &= ~VSDMA_BLOCKED;
    575 	}
    576 
    577 	/*
    578 	 * Now, whatever it was, let the ncr5380sbc routine handle it...
    579 	 */
    580 	claimed = ncr5380_intr(ncr_sc);
    581 #ifdef	DEBUG
    582 	if (!claimed) {
    583 		printf("si_intr: spurious from SBC\n");
    584 		if (si_debug & 4) {
    585 			Debugger();	/* XXX */
    586 		}
    587 	}
    588 #endif
    589 	trace(("%s: si-intr(%d) done, claimed=%d\n",
    590 	       ncr_sc->sc_dev.dv_xname, count, claimed));
    591 	return (claimed);
    592 }
    593 
    594 
    595 integrate void
    596 si_reset_adapter(struct ncr5380_softc *ncr_sc)
    597 {
    598 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    599 	volatile struct si_regs *si = sc->sc_regs;
    600 
    601 #ifdef	DEBUG
    602 	if (si_debug) {
    603 		printf("si_reset_adapter\n");
    604 	}
    605 #endif
    606 	SCI_CLR_INTR(ncr_sc);
    607 }
    608 
    609 
    610 /*****************************************************************
    611  * Common functions for DMA
    612  ****************************************************************/
    613 
    614 /*
    615  * Allocate a DMA handle and put it in sc->sc_dma.  Prepare
    616  * for DMA transfer.  On the Sun3, this means mapping the buffer
    617  * into DVMA space.  dvma_mapin() flushes the cache for us.
    618  */
    619 void
    620 si_dma_alloc(ncr_sc)
    621 	struct ncr5380_softc *ncr_sc;
    622 {
    623 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    624 	struct sci_req *sr = ncr_sc->sc_current;
    625 	struct scsi_xfer *xs = sr->sr_xs;
    626 	struct buf *bp = sr->sr_xs->bp;
    627 	struct si_dma_handle *dh;
    628 	int i, xlen;
    629 	u_long addr;
    630 
    631 	trace (("si_dma_alloc()\n"));
    632 
    633 #ifdef	DIAGNOSTIC
    634 	if (sr->sr_dma_hand != NULL)
    635 		panic("si_dma_alloc: already have DMA handle");
    636 #endif
    637 
    638 	addr = (u_long) ncr_sc->sc_dataptr;
    639 	debug(("addr=%x, dataptr=%x\n", addr, ncr_sc->sc_dataptr));
    640 	xlen = ncr_sc->sc_datalen;
    641 
    642 	/* Make sure our caller checked sc_min_dma_len. */
    643 	if (xlen < MIN_DMA_LEN)
    644 		panic("si_dma_alloc: xlen=0x%x\n", xlen);
    645 
    646 	/*
    647 	 * Never attempt single transfers of more than 63k, because
    648 	 * our count register may be only 16 bits (an OBIO adapter).
    649 	 * This should never happen since already bounded by minphys().
    650 	 * XXX - Should just segment these...
    651 	 */
    652 	if (xlen > MAX_DMA_LEN) {
    653 		printf("si_dma_alloc: excessive xlen=0x%x\n", xlen);
    654 		Debugger();
    655 		ncr_sc->sc_datalen = xlen = MAX_DMA_LEN;
    656 	}
    657 
    658 	/* Find free DMA handle.  Guaranteed to find one since we have
    659 	   as many DMA handles as the driver has processes. */
    660 	for (i = 0; i < SCI_OPENINGS; i++) {
    661 		if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
    662 			goto found;
    663 	}
    664 	panic("si: no free DMA handles.");
    665 found:
    666 
    667 	dh = &sc->sc_dma[i];
    668 	dh->dh_flags = SIDH_BUSY;
    669 	dh->dh_addr = (u_char*) addr;
    670 	dh->dh_maplen  = xlen;
    671 	dh->dh_xlen  = xlen;
    672 	dh->dh_dvma = 0;
    673 
    674 	/* Copy the "write" flag for convenience. */
    675 	if (xs->flags & SCSI_DATA_OUT)
    676 		dh->dh_flags |= SIDH_OUT;
    677 
    678 #if 1
    679 	/*
    680 	 * If the buffer has the flag B_PHYS, the the address specified
    681 	 * in the buffer is a user-space address and we need to remap
    682 	 * this address into kernel space so that using this buffer
    683 	 * within the interrupt routine will work.
    684 	 * If it's already a kernel space address, we need to make sure
    685 	 * that all pages are in-core. the mapin() routine takes care
    686 	 * of that.
    687 	 */
    688 	if (bp && (bp->b_flags & B_PHYS))
    689 		dh->dh_flags |= SIDH_PHYS;
    690 #endif
    691 
    692 	if (!bp) {
    693 		printf("ncr.c: struct buf *bp is null-pointer.\n");
    694 		dh->dh_flags = 0;
    695 		return;
    696 	}
    697 	if (bp->b_bcount < 0 || bp->b_bcount > MAX_DMA_LEN) {
    698 		printf("ncr.c: invalid bcount %d (0x%x)\n",
    699 		       bp->b_bcount, bp->b_bcount);
    700 		dh->dh_flags = 0;
    701 		return;
    702 	}
    703 	dh->dh_dvma = bp->b_data;
    704 #if 0
    705 	/*
    706 	 * mapping of user-space addresses is no longer neccessary, now
    707 	 * that the vmapbuf/vunmapbuf routines exist. Now the higher-level
    708 	 * driver already cares for the mapping!
    709 	 */
    710 	if (bp->b_flags & B_PHYS) {
    711 		xdebug(("not mapping in... %x/%x %x\n", bp->b_saveaddr,
    712 			bp->b_data, bp->b_bcount));
    713 #ifdef USE_VMAPBUF
    714 		dh->dh_addr = bp->b_data;
    715 		dh->dh_maplen = bp->b_bcount;
    716 		vmapbuf(bp, bp->b_bcount);
    717 		dh->dh_dvma = bp->b_data;
    718 #else
    719 		dh->dh_dvma = (u_char*)vsdma_mapin(bp);
    720 #endif
    721 		xdebug(("addr %x, maplen %d, dvma %x, bcount %d, dir %s\n",
    722 		       dh->dh_addr, dh->dh_maplen, dh->dh_dvma, bp->b_bcount,
    723 		       (dh->dh_flags & SIDH_OUT ? "OUT" : "IN")));
    724 	}
    725 #endif
    726 	/* success */
    727 	sr->sr_dma_hand = dh;
    728 
    729 	return;
    730 }
    731 
    732 
    733 void
    734 si_dma_free(ncr_sc)
    735 	struct ncr5380_softc *ncr_sc;
    736 {
    737 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    738 	struct sci_req *sr = ncr_sc->sc_current;
    739 	struct scsi_xfer *xs = sr->sr_xs;
    740 	struct buf *bp = sr->sr_xs->bp;
    741 	struct si_dma_handle *dh = sr->sr_dma_hand;
    742 
    743 	trace (("si_dma_free()\n"));
    744 
    745 #ifdef	DIAGNOSTIC
    746 	if (dh == NULL)
    747 		panic("si_dma_free: no DMA handle");
    748 #endif
    749 
    750 	if (ncr_sc->sc_state & NCR_DOINGDMA)
    751 		panic("si_dma_free: free while in progress");
    752 
    753 	if (dh->dh_flags & SIDH_BUSY) {
    754 #if 0
    755 		debug(("bp->b_flags=0x%x\n", bp->b_flags));
    756 		if (bp->b_flags & B_PHYS) {
    757 #ifdef USE_VMAPBUF
    758 			printf("not unmapping(%x/%x %x/%x %d/%d)...\n",
    759 			       dh->dh_addr, dh->dh_dvma,
    760 			       bp->b_saveaddr, bp->b_data,
    761 			       bp->b_bcount, dh->dh_maplen);
    762 			/* vunmapbuf(bp, dh->dh_maplen); */
    763 			printf("done.\n");
    764 #endif
    765 			dh->dh_dvma = 0;
    766 		}
    767 #endif
    768 		dh->dh_flags = 0;
    769 	}
    770 	sr->sr_dma_hand = NULL;
    771 }
    772 
    773 
    774 /*
    775  * REGBUSY and DMABUSY won't collide since the higher-level driver
    776  * issues intr_on/intr_off before/after doing DMA. The only problem
    777  * is to handle RDBUF/WRBUF wrt REGBUSY/DMABUSY
    778  *
    779  * There might be race-conditions, but for now we don't care for them...
    780  */
    781 int
    782 si_dmaLockBus(ncr_sc, lt)
    783 	struct ncr5380_softc *ncr_sc;
    784 	int lt;			/* Lock-Type */
    785 {
    786 	struct si_softc *sc = (void*)ncr_sc;
    787 	int timeout = 200;	/* wait .2 seconds max. */
    788 
    789 	trace(("si_dmaLockBus(%x), cold: %d, current: %x\n",
    790 	       lt, cold, sc->sc_dflags));
    791 
    792 #ifdef POLL_MODE
    793 	if (cold)
    794 		return (0);
    795 #endif
    796 
    797 	if ((ncr_sc->sc_current != NULL) && (lt == VSDMA_REGBUSY)) {
    798 		printf("trying to use regs while sc_current is set.\n");
    799 		printf("lt=%x, fl=%x, cur=%x\n",
    800 		       lt, sc->sc_dflags, ncr_sc->sc_current);
    801 	}
    802 	if ((ncr_sc->sc_current == NULL) && (lt != VSDMA_REGBUSY)) {
    803 		printf("trying to use/prepare DMA without current.\n");
    804 		printf("lt=%x, fl=%x, cur=%x\n",
    805 		       lt, sc->sc_dflags, ncr_sc->sc_current);
    806 	}
    807 
    808 	if ((sc->sc_dflags & VSDMA_LOCKED) == 0) {
    809 		struct si_softc *sc = (struct si_softc *)ncr_sc;
    810 		sc->sc_dflags |= VSDMA_WANTED;
    811 		vsbus_lockDMA(sc->sc_cfargs);
    812 		sc->sc_dflags = VSDMA_LOCKED | lt;
    813 		return (0);
    814 	}
    815 
    816 #if 1
    817 	while ((sc->sc_dflags & VSDMA_LCKTYPE) != lt) {
    818 		debug(("busy wait(1)...\n"));
    819 		if (--timeout == 0) {
    820 			printf("timeout in busy-wait(%x %x)\n",
    821 			       lt, sc->sc_dflags);
    822 			sc->sc_dflags &= ~VSDMA_LCKTYPE;
    823 			break;
    824 		}
    825 		delay(1000);
    826 	}
    827 	debug(("busy wait(1) done.\n"));
    828 	sc->sc_dflags |= lt;
    829 
    830 #else
    831 	if ((sc->sc_dflags & VSDMA_LCKTYPE) != lt) {
    832 		switch (lt) {
    833 
    834 		case VSDMA_RDBUF:
    835 			/* sc->sc_dflags |= VSDMA_IWANTED; */
    836 			debug(("busy wait(1)...\n"));
    837 			while (sc->sc_dflags &
    838 			       (VSDMA_WRBUF | VSDMA_DMABUSY)) {
    839 				if (--timeout == 0) {
    840 					printf("timeout in busy-wait(1)\n");
    841 					sc->sc_dflags &= ~VSDMA_WRBUF;
    842 					sc->sc_dflags &= ~VSDMA_DMABUSY;
    843 				}
    844 				delay(1000);
    845 			}
    846 			/* sc->sc_dflags &= ~VSDMA_IWANTED; */
    847 			debug(("busy wait(1) done.\n"));
    848 			sc->sc_dflags |= lt;
    849 			break;
    850 
    851 		case VSDMA_WRBUF:
    852 			/* sc->sc_dflags |= VSDMA_IWANTED; */
    853 			debug(("busy wait(2)...\n"));
    854 			while (sc->sc_dflags &
    855 			       (VSDMA_RDBUF | VSDMA_DMABUSY)) {
    856 				if (--timeout == 0) {
    857 					printf("timeout in busy-wait(2)\n");
    858 					sc->sc_dflags &= ~VSDMA_RDBUF;
    859 					sc->sc_dflags &= ~VSDMA_DMABUSY;
    860 				}
    861 				delay(1000);
    862 			}
    863 			/* sc->sc_dflags &= ~VSDMA_IWANTED; */
    864 			debug(("busy wait(2) done.\n"));
    865 			sc->sc_dflags |= lt;
    866 			break;
    867 
    868 		case VSDMA_DMABUSY:
    869 			/* sc->sc_dflags |= VSDMA_IWANTED; */
    870 			debug(("busy wait(3)...\n"));
    871 			while (sc->sc_dflags &
    872 			       (VSDMA_RDBUF | VSDMA_WRBUF)) {
    873 				if (--timeout == 0) {
    874 					printf("timeout in busy-wait(3)\n");
    875 					sc->sc_dflags &= ~VSDMA_RDBUF;
    876 					sc->sc_dflags &= ~VSDMA_WRBUF;
    877 				}
    878 				delay(1000);
    879 			}
    880 			/* sc->sc_dflags &= ~VSDMA_IWANTED; */
    881 			debug(("busy wait(3) done.\n"));
    882 			sc->sc_dflags |= lt;
    883 			break;
    884 
    885 		case VSDMA_REGBUSY:
    886 			/* sc->sc_dflags |= VSDMA_IWANTED; */
    887 			debug(("busy wait(4)...\n"));
    888 			while (sc->sc_dflags &
    889 			       (VSDMA_RDBUF | VSDMA_WRBUF | VSDMA_DMABUSY)) {
    890 				if (--timeout == 0) {
    891 					printf("timeout in busy-wait(4)\n");
    892 					sc->sc_dflags &= ~VSDMA_RDBUF;
    893 					sc->sc_dflags &= ~VSDMA_WRBUF;
    894 					sc->sc_dflags &= ~VSDMA_DMABUSY;
    895 				}
    896 				delay(1000);
    897 			}
    898 			/* sc->sc_dflags &= ~VSDMA_IWANTED; */
    899 			debug(("busy wait(4) done.\n"));
    900 			sc->sc_dflags |= lt;
    901 			break;
    902 
    903 		default:
    904 			printf("illegal lockType %x in si_dmaLockBus()\n");
    905 		}
    906 	}
    907 	else
    908 		printf("already locked. (%x/%x)\n", lt, sc->sc_dflags);
    909 #endif
    910 	if (sc->sc_dflags & lt) /* successfully locked for this type */
    911 		return (0);
    912 
    913 	printf("spurious %x in si_dmaLockBus(%x)\n", lt, sc->sc_dflags);
    914 }
    915 
    916 /*
    917  * the lock of this type is no longer needed. If all (internal) locks are
    918  * released, release the DMA bus.
    919  */
    920 int
    921 si_dmaReleaseBus(ncr_sc, lt)
    922 	struct ncr5380_softc *ncr_sc;
    923 	int lt;			/* Lock-Type */
    924 {
    925 	struct si_softc *sc = (void*)ncr_sc;
    926 
    927 	trace(("si_dmaReleaseBus(%x), cold: %d, current: %x\n",
    928 	       lt, cold, sc->sc_dflags));
    929 
    930 #ifdef POLL_MODE
    931 	if (cold)
    932 		return (0);
    933 #endif
    934 
    935 	if ((sc->sc_dflags & VSDMA_LCKTYPE) == lt) {
    936 		sc->sc_dflags &= ~lt;
    937 	}
    938 	else
    939 		printf("trying to release %x while flags = %x\n", lt,
    940 		       sc->sc_dflags);
    941 
    942 	if (sc->sc_dflags == VSDMA_LOCKED) {	/* no longer needed */
    943 		struct si_softc *sc = (struct si_softc *)ncr_sc;
    944 		vsbus_unlockDMA(sc->sc_cfargs);
    945 		sc->sc_dflags = 0;
    946 		return (0);
    947 	}
    948 }
    949 
    950 /*
    951  * Just toggle the type of lock without releasing the lock...
    952  * This is usually needed before/after bcopy() to/from DMA-buffer
    953  */
    954 int
    955 si_dmaToggleLock(ncr_sc, lt1, lt2)
    956 	struct ncr5380_softc *ncr_sc;
    957 	int lt1, lt2;		/* Lock-Type */
    958 {
    959 	struct si_softc *sc = (void*)ncr_sc;
    960 
    961 #ifdef POLL_MODE
    962 	if (cold)
    963 		return (0);
    964 #endif
    965 
    966 	if (((sc->sc_dflags & lt1) != 0) &&
    967 	    ((sc->sc_dflags & lt2) == 0)) {
    968 		sc->sc_dflags |= lt2;
    969 		sc->sc_dflags &= ~lt1;
    970 		return (0);
    971 	}
    972 	printf("cannot toggle locking from %x to %x (current = %x)\n",
    973 	       lt1, lt2, sc->sc_dflags);
    974 }
    975 
    976 /*
    977  * This is called when the bus is going idle,
    978  * so we want to enable the SBC interrupts.
    979  * That is controlled by the DMA enable!
    980  * Who would have guessed!
    981  * What a NASTY trick!
    982  */
    983 void
    984 si_intr_on(ncr_sc)
    985 	struct ncr5380_softc *ncr_sc;
    986 {
    987 	si_dmaReleaseBus(ncr_sc, VSDMA_REGBUSY);
    988 }
    989 
    990 /*
    991  * This is called when the bus is idle and we are
    992  * about to start playing with the SBC chip.
    993  *
    994  * VS2000 note: we have four kinds of access which are mutually exclusive:
    995  * - access to the NCR5380 registers
    996  * - access to the HDC9224 registers
    997  * - access to the DMA area
    998  * - doing DMA
    999  */
   1000 void
   1001 si_intr_off(ncr_sc)
   1002 	struct ncr5380_softc *ncr_sc;
   1003 {
   1004 	si_dmaLockBus(ncr_sc, VSDMA_REGBUSY);
   1005 }
   1006 
   1007 /*****************************************************************
   1008  * VME functions for DMA
   1009  ****************************************************************/
   1010 
   1011 
   1012 /*
   1013  * This function is called during the COMMAND or MSG_IN phase
   1014  * that preceeds a DATA_IN or DATA_OUT phase, in case we need
   1015  * to setup the DMA engine before the bus enters a DATA phase.
   1016  *
   1017  * XXX: The VME adapter appears to suppress SBC interrupts
   1018  * when the FIFO is not empty or the FIFO count is non-zero!
   1019  *
   1020  * On the VME version we just clear the DMA count and address
   1021  * here (to make sure it stays idle) and do the real setup
   1022  * later, in dma_start.
   1023  */
   1024 void
   1025 si_dma_setup(ncr_sc)
   1026 	struct ncr5380_softc *ncr_sc;
   1027 {
   1028 	trace (("si_dma_setup(ncr_sc) !!!\n"));
   1029 
   1030 	/*
   1031 	 * VS2000: nothing to do ...
   1032 	 */
   1033 }
   1034 
   1035 
   1036 void
   1037 si_dma_start(ncr_sc)
   1038 	struct ncr5380_softc *ncr_sc;
   1039 {
   1040 	struct si_softc *sc = (struct si_softc *)ncr_sc;
   1041 	struct sci_req *sr = ncr_sc->sc_current;
   1042 	struct si_dma_handle *dh = sr->sr_dma_hand;
   1043 	volatile struct si_regs *si = sc->sc_regs;
   1044 	long data_pa;
   1045 	int xlen;
   1046 
   1047 	trace(("si_dma_start(%x)\n", sr->sr_dma_hand));
   1048 
   1049 	/*
   1050 	 * we always transfer from/to base of DMA-area,
   1051 	 * thus the DMA-address is always the same, only size
   1052 	 * and direction matter/differ on VS2000
   1053 	 */
   1054 
   1055 	debug(("ncr_sc->sc_datalen = %d\n", ncr_sc->sc_datalen));
   1056 	xlen = ncr_sc->sc_datalen;
   1057 	dh->dh_xlen = xlen;
   1058 
   1059 	/*
   1060 	 * VS2000 has a fixed 16KB-area where DMA is restricted to.
   1061 	 * All DMA-addresses are relative to this base: KA410_DMA_BASE
   1062 	 * Thus we need to copy the data into this area when writing,
   1063 	 * or copy from this area when reading. (kind of bounce-buffer)
   1064 	 */
   1065 
   1066 	/* Set direction (send/recv) */
   1067 	if (dh->dh_flags & SIDH_OUT) {
   1068 		/*
   1069 		 * We know that we are called while intr_off (regs locked)
   1070 		 * thus we toggle the lock from REGBUSY to WRBUF
   1071 		 * also we set the BLOCKIT flag, so that the locking of
   1072 		 * the DMA bus won't be released to the HDC9224...
   1073 		 */
   1074 		debug(("preparing msg-out (bcopy)\n"));
   1075 		si_dmaToggleLock(ncr_sc, VSDMA_REGBUSY, VSDMA_WRBUF);
   1076 		bcopy(dh->dh_dvma, sc->sc_dbase, xlen);
   1077 		si_dmaToggleLock(ncr_sc, VSDMA_WRBUF, VSDMA_REGBUSY);
   1078 		*sc->sc_ddreg = DMA_DIR_OUT;
   1079 	}
   1080 	else {
   1081 		debug(("preparing data-in (bzero)\n"));
   1082 		/* bzero(sc->sc_dbase, xlen); */
   1083 		*sc->sc_ddreg = DMA_DIR_IN;
   1084 	}
   1085 	sc->sc_dflags |= VSDMA_BLOCKED;
   1086 
   1087 	*sc->sc_dareg = DMA_ADDR_HBYTE; /* high byte (6 bits) */
   1088 	*sc->sc_dareg = DMA_ADDR_LBYTE; /* low byte */
   1089 	*sc->sc_dcreg = 0 - xlen; /* bertram XXX */
   1090 
   1091 #ifdef	DEBUG
   1092 	if (si_debug & 2) {
   1093 		printf("si_dma_start: dh=0x%x, pa=0x%x, xlen=%d, creg=0x%x\n",
   1094 			   dh, data_pa, xlen, *sc->sc_dcreg);
   1095 	}
   1096 #endif
   1097 
   1098 #ifdef POLL_MODE
   1099 	debug(("dma_start: cold=%d\n", cold));
   1100 	if (cold) {
   1101 		*sc->intmsk &= ~sc->intbit;
   1102 		*sc->intclr = sc->intbit;
   1103 	}
   1104 	else
   1105 		*sc->intmsk |= sc->intbit;
   1106 #endif
   1107 	/*
   1108 	 * Acknowledge the phase change.  (After DMA setup!)
   1109 	 * Put the SBIC into DMA mode, and start the transfer.
   1110 	 */
   1111 	si_dmaToggleLock(ncr_sc, VSDMA_REGBUSY, VSDMA_DMABUSY);
   1112 	if (dh->dh_flags & SIDH_OUT) {
   1113 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
   1114 		SCI_CLR_INTR(ncr_sc);
   1115 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
   1116 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
   1117 		*ncr_sc->sci_dma_send = 0;	/* start it */
   1118 	} else {
   1119 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
   1120 		SCI_CLR_INTR(ncr_sc);
   1121 		*ncr_sc->sci_icmd = 0;
   1122 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
   1123 		*ncr_sc->sci_irecv = 0; /* start it */
   1124 	}
   1125 	ncr_sc->sc_state |= NCR_DOINGDMA;
   1126 	/*
   1127 	 * having a delay (eg. printf) here, seems to solve the problem.
   1128 	 * Isn't that strange ????
   1129 	 * Maybe the higher-level driver accesses one of the registers of
   1130 	 * the controller while DMA is in progress. Having a long enough
   1131 	 * delay here might prevent/delay this access until DMA bus is
   1132 	 * free again...
   1133 	 *
   1134 	 * The instruction ++++ printf("DMA started.\n"); ++++
   1135 	 * is long/slow enough, to make the SSCI driver work. Thus we
   1136 	 * try to find a delay() long/slow enough to do the same. The
   1137 	 * argument to this delay is relative to the transfer-count.
   1138 	 */
   1139 	delay(3*xlen/4);		/* XXX solve this problem!!! XXX */
   1140 
   1141 #ifdef	DEBUG
   1142 	if (si_debug & 2) {
   1143 		printf("si_dma_start: started, flags=0x%x\n",
   1144 			   ncr_sc->sc_state);
   1145 	}
   1146 #endif
   1147 }
   1148 
   1149 
   1150 void
   1151 si_vme_dma_eop(ncr_sc)
   1152 	struct ncr5380_softc *ncr_sc;
   1153 {
   1154 	trace (("si_vme_dma_eop() !!!\n"));
   1155 	/* Not needed - DMA was stopped prior to examining sci_csr */
   1156 }
   1157 
   1158 /*
   1159  * si_dma_stop() has now become almost a nop-routine, since DMA-buffer
   1160  * has already been read within si_intr(), so there's nothing left to do.
   1161  */
   1162 void
   1163 si_dma_stop(ncr_sc)
   1164 	struct ncr5380_softc *ncr_sc;
   1165 {
   1166 	struct si_softc *sc = (struct si_softc *)ncr_sc;
   1167 	struct sci_req *sr = ncr_sc->sc_current;
   1168 	struct si_dma_handle *dh = sr->sr_dma_hand;
   1169 	volatile struct si_regs *si = sc->sc_regs;
   1170 	int resid, ntrans;
   1171 
   1172 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
   1173 #ifdef	DEBUG
   1174 		printf("si_dma_stop: dma not running\n");
   1175 #endif
   1176 		return;
   1177 	}
   1178 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
   1179 
   1180 	/* Note that timeout may have set the error flag. */
   1181 	if (ncr_sc->sc_state & NCR_ABORTING) {
   1182 		printf("si_dma_stop: timeout?\n");
   1183 		goto out;
   1184 	}
   1185 
   1186 	/*
   1187 	 * Now try to figure out how much actually transferred
   1188 	 */
   1189 	si_dmaLockBus(ncr_sc, VSDMA_DMABUSY);
   1190 	si_dmaToggleLock(ncr_sc, VSDMA_DMABUSY, VSDMA_REGBUSY);
   1191 	resid = *sc->sc_dcreg;
   1192 	/*
   1193 	 * XXX: don't correct at two places !!!
   1194 	 */
   1195 	if (resid == 1 && sc->sc_xflags) {
   1196 		resid = 0;
   1197 	}
   1198 	ntrans = dh->dh_xlen + resid;
   1199 	if (resid != 0)
   1200 		printf("resid=%d, xlen=%d, ntrans=%d\n",
   1201 		       resid, dh->dh_xlen, ntrans);
   1202 
   1203 #ifdef	DEBUG
   1204 	if (si_debug & 2) {
   1205 		printf("si_dma_stop: resid=0x%x ntrans=0x%x\n",
   1206 		       resid, ntrans);
   1207 	}
   1208 #endif
   1209 
   1210 	if (ntrans < MIN_DMA_LEN) {
   1211 		printf("si: fifo count: 0x%x\n", resid);
   1212 		ncr_sc->sc_state |= NCR_ABORTING;
   1213 		goto out;
   1214 	}
   1215 	if (ntrans > ncr_sc->sc_datalen)
   1216 		panic("si_dma_stop: excess transfer");
   1217 
   1218 	/*
   1219 	 * On VS2000 in case of a READ-operation, we must now copy
   1220 	 * the buffer-contents to the destination-address!
   1221 	 */
   1222 	if ((dh->dh_flags & SIDH_OUT) == 0 &&
   1223 	    (dh->dh_flags & SIDH_DONE) == 0) {
   1224 		printf("DMA buffer not yet copied.\n");
   1225 		si_dmaToggleLock(ncr_sc, VSDMA_REGBUSY, VSDMA_RDBUF);
   1226 		bcopy(sc->sc_dbase, dh->dh_dvma, ntrans);
   1227 		si_dmaToggleLock(ncr_sc, VSDMA_RDBUF, VSDMA_REGBUSY);
   1228 	}
   1229 	si_dmaReleaseBus(ncr_sc, VSDMA_REGBUSY);
   1230 
   1231 	/* Adjust data pointer */
   1232 	ncr_sc->sc_dataptr += ntrans;
   1233 	ncr_sc->sc_datalen -= ntrans;
   1234 
   1235 out:
   1236 	si_dmaLockBus(ncr_sc, VSDMA_DMABUSY);
   1237 
   1238 	/* Put SBIC back in PIO mode. */
   1239 	*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
   1240 	*ncr_sc->sci_icmd = 0;
   1241 
   1242 	si_dmaReleaseBus(ncr_sc, VSDMA_DMABUSY);
   1243 }
   1244 
   1245 /*
   1246  * Poll (spin-wait) for DMA completion.
   1247  * Called right after xx_dma_start(), and
   1248  * xx_dma_stop() will be called next.
   1249  */
   1250 void
   1251 si_dma_poll(ncr_sc)
   1252 	struct ncr5380_softc *ncr_sc;
   1253 {
   1254 	struct si_softc *sc = (struct si_softc *)ncr_sc;
   1255 	struct sci_req *sr = ncr_sc->sc_current;
   1256 	struct si_dma_handle *dh = sr->sr_dma_hand;
   1257 	int i, timeout;
   1258 
   1259 	if (! cold)
   1260 		printf("spurious call of DMA-poll ???");
   1261 
   1262 #ifdef POLL_MODE
   1263 
   1264 	delay(10000);
   1265 	trace(("si_dma_poll(%x)\n", *sc->sc_dcreg));
   1266 
   1267 	/*
   1268 	 * interrupt-request has been cleared by dma_start, thus
   1269 	 * we do nothing else but wait for the intreq to reappear...
   1270 	 */
   1271 
   1272 	timeout = 5000;
   1273 	for (i=0; i<timeout; i++) {
   1274 		if (*sc->intreq & sc->intbit)
   1275 			break;
   1276 		delay(100);
   1277 	}
   1278 	if ((*sc->intreq & sc->intbit) == 0) {
   1279 		printf("si: DMA timeout (while polling)\n");
   1280 		/* Indicate timeout as MI code would. */
   1281 		sr->sr_flags |= SR_OVERDUE;
   1282 	}
   1283 #endif
   1284 	return;
   1285 }
   1286