Home | History | Annotate | Line # | Download | only in dev
esp.c revision 1.31
      1 /*	$NetBSD: esp.c,v 1.31 2001/04/02 05:29:43 dbj Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1994 Peter Galbavy
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Peter Galbavy
     55  * 4. The name of the author may not be used to endorse or promote products
     56  *    derived from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     60  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     61  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     62  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     63  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     64  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     66  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     67  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     68  * POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 /*
     72  * Based on aic6360 by Jarle Greipsland
     73  *
     74  * Acknowledgements: Many of the algorithms used in this driver are
     75  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     76  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     77  */
     78 
     79 /*
     80  * Grabbed from the sparc port at revision 1.73 for the NeXT.
     81  * Darrin B. Jewell <dbj (at) netbsd.org>  Sat Jul  4 15:41:32 1998
     82  */
     83 
     84 #include <sys/types.h>
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/kernel.h>
     88 #include <sys/errno.h>
     89 #include <sys/ioctl.h>
     90 #include <sys/device.h>
     91 #include <sys/buf.h>
     92 #include <sys/proc.h>
     93 #include <sys/user.h>
     94 #include <sys/queue.h>
     95 
     96 #include <dev/scsipi/scsi_all.h>
     97 #include <dev/scsipi/scsipi_all.h>
     98 #include <dev/scsipi/scsiconf.h>
     99 #include <dev/scsipi/scsi_message.h>
    100 
    101 #include <machine/bus.h>
    102 #include <machine/autoconf.h>
    103 #include <machine/cpu.h>
    104 
    105 #include <dev/ic/ncr53c9xreg.h>
    106 #include <dev/ic/ncr53c9xvar.h>
    107 
    108 #include <next68k/next68k/isr.h>
    109 
    110 #include <next68k/dev/nextdmareg.h>
    111 #include <next68k/dev/nextdmavar.h>
    112 
    113 #include "espreg.h"
    114 #include "espvar.h"
    115 
    116 #ifdef DEBUG
    117 #define ESP_DEBUG
    118 #endif
    119 
    120 #ifdef ESP_DEBUG
    121 int esp_debug = 0;
    122 #define DPRINTF(x) if (esp_debug) printf x;
    123 #else
    124 #define DPRINTF(x)
    125 #endif
    126 
    127 
    128 void	espattach_intio	__P((struct device *, struct device *, void *));
    129 int	espmatch_intio	__P((struct device *, struct cfdata *, void *));
    130 
    131 /* DMA callbacks */
    132 bus_dmamap_t esp_dmacb_continue __P((void *arg));
    133 void esp_dmacb_completed __P((bus_dmamap_t map, void *arg));
    134 void esp_dmacb_shutdown __P((void *arg));
    135 
    136 #ifdef ESP_DEBUG
    137 char esp_dma_dump[5*1024] = "";
    138 struct ncr53c9x_softc *esp_debug_sc = 0;
    139 void esp_dma_store __P((struct ncr53c9x_softc *sc));
    140 void esp_dma_print __P((struct ncr53c9x_softc *sc));
    141 int esp_dma_nest = 0;
    142 #endif
    143 
    144 
    145 /* Linkup to the rest of the kernel */
    146 struct cfattach esp_ca = {
    147 	sizeof(struct esp_softc), espmatch_intio, espattach_intio
    148 };
    149 
    150 /*
    151  * Functions and the switch for the MI code.
    152  */
    153 u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
    154 void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    155 int	esp_dma_isintr __P((struct ncr53c9x_softc *));
    156 void	esp_dma_reset __P((struct ncr53c9x_softc *));
    157 int	esp_dma_intr __P((struct ncr53c9x_softc *));
    158 int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    159 	    size_t *, int, size_t *));
    160 void	esp_dma_go __P((struct ncr53c9x_softc *));
    161 void	esp_dma_stop __P((struct ncr53c9x_softc *));
    162 int	esp_dma_isactive __P((struct ncr53c9x_softc *));
    163 
    164 struct ncr53c9x_glue esp_glue = {
    165 	esp_read_reg,
    166 	esp_write_reg,
    167 	esp_dma_isintr,
    168 	esp_dma_reset,
    169 	esp_dma_intr,
    170 	esp_dma_setup,
    171 	esp_dma_go,
    172 	esp_dma_stop,
    173 	esp_dma_isactive,
    174 	NULL,			/* gl_clear_latched_intr */
    175 };
    176 
    177 #ifdef ESP_DEBUG
    178 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
    179 static void
    180 esp_hex_dump(unsigned char *pkt, size_t len)
    181 {
    182 	size_t i, j;
    183 
    184 	printf("00000000  ");
    185 	for(i=0; i<len; i++) {
    186 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
    187 		if ((i+1) % 16 == 8) {
    188 			printf(" ");
    189 		}
    190 		if ((i+1) % 16 == 0) {
    191 			printf(" %c", '|');
    192 			for(j=0; j<16; j++) {
    193 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
    194 			}
    195 			printf("%c\n%c%c%c%c%c%c%c%c  ", '|',
    196 					XCHR((i+1)>>28),XCHR((i+1)>>24),XCHR((i+1)>>20),XCHR((i+1)>>16),
    197 					XCHR((i+1)>>12), XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
    198 		}
    199 	}
    200 	printf("\n");
    201 }
    202 #endif
    203 
    204 int
    205 espmatch_intio(parent, cf, aux)
    206 	struct device *parent;
    207 	struct cfdata *cf;
    208 	void *aux;
    209 {
    210   /* should probably probe here */
    211   /* Should also probably set up data from config */
    212 
    213 	return(1);
    214 }
    215 
    216 void
    217 espattach_intio(parent, self, aux)
    218 	struct device *parent, *self;
    219 	void *aux;
    220 {
    221 	struct esp_softc *esc = (void *)self;
    222 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    223 
    224 #ifdef ESP_DEBUG
    225 	esp_debug_sc = sc;
    226 #endif
    227 
    228 	esc->sc_bst = NEXT68K_INTIO_BUS_SPACE;
    229 	if (bus_space_map(esc->sc_bst, NEXT_P_SCSI,
    230 			ESP_DEVICE_SIZE, 0, &esc->sc_bsh)) {
    231     panic("\n%s: can't map ncr53c90 registers",
    232 				sc->sc_dev.dv_xname);
    233 	}
    234 
    235 	sc->sc_id = 7;
    236 	sc->sc_freq = 20;							/* Mhz */
    237 
    238 	/*
    239 	 * Set up glue for MI code early; we use some of it here.
    240 	 */
    241 	sc->sc_glue = &esp_glue;
    242 
    243 	/*
    244 	 * XXX More of this should be in ncr53c9x_attach(), but
    245 	 * XXX should we really poke around the chip that much in
    246 	 * XXX the MI code?  Think about this more...
    247 	 */
    248 
    249 	/*
    250 	 * It is necessary to try to load the 2nd config register here,
    251 	 * to find out what rev the esp chip is, else the ncr53c9x_reset
    252 	 * will not set up the defaults correctly.
    253 	 */
    254 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    255 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
    256 	sc->sc_cfg3 = NCRCFG3_CDB;
    257 	NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    258 
    259 	if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
    260 	    (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
    261 		sc->sc_rev = NCR_VARIANT_ESP100;
    262 	} else {
    263 		sc->sc_cfg2 = NCRCFG2_SCSI2;
    264 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    265 		sc->sc_cfg3 = 0;
    266 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    267 		sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
    268 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    269 		if (NCR_READ_REG(sc, NCR_CFG3) !=
    270 		    (NCRCFG3_CDB | NCRCFG3_FCLK)) {
    271 			sc->sc_rev = NCR_VARIANT_ESP100A;
    272 		} else {
    273 			/* NCRCFG2_FE enables > 64K transfers */
    274 			sc->sc_cfg2 |= NCRCFG2_FE;
    275 			sc->sc_cfg3 = 0;
    276 			NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    277 			sc->sc_rev = NCR_VARIANT_ESP200;
    278 		}
    279 	}
    280 
    281 	/*
    282 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    283 	 * XXX but it appears to have some dependency on what sort
    284 	 * XXX of DMA we're hooked up to, etc.
    285 	 */
    286 
    287 	/*
    288 	 * This is the value used to start sync negotiations
    289 	 * Note that the NCR register "SYNCTP" is programmed
    290 	 * in "clocks per byte", and has a minimum value of 4.
    291 	 * The SCSI period used in negotiation is one-fourth
    292 	 * of the time (in nanoseconds) needed to transfer one byte.
    293 	 * Since the chip's clock is given in MHz, we have the following
    294 	 * formula: 4 * period = (1000 / freq) * 4
    295 	 */
    296 	sc->sc_minsync = 1000 / sc->sc_freq;
    297 
    298 	/*
    299 	 * Alas, we must now modify the value a bit, because it's
    300 	 * only valid when can switch on FASTCLK and FASTSCSI bits
    301 	 * in config register 3...
    302 	 */
    303 	switch (sc->sc_rev) {
    304 	case NCR_VARIANT_ESP100:
    305 		sc->sc_maxxfer = 64 * 1024;
    306 		sc->sc_minsync = 0;	/* No synch on old chip? */
    307 		break;
    308 
    309 	case NCR_VARIANT_ESP100A:
    310 		sc->sc_maxxfer = 64 * 1024;
    311 		/* Min clocks/byte is 5 */
    312 		sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
    313 		break;
    314 
    315 	case NCR_VARIANT_ESP200:
    316 		sc->sc_maxxfer = 16 * 1024 * 1024;
    317 		/* XXX - do actually set FAST* bits */
    318 		break;
    319 	}
    320 
    321 	/* @@@ Some ESP_DCTL bits probably need setting */
    322 	NCR_WRITE_REG(sc, ESP_DCTL,
    323 			ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_RESET);
    324 	DELAY(10);
    325 	DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    326 	NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_20MHZ | ESPDCTL_INTENB);
    327 	DELAY(10);
    328 	DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    329 
    330 	/* Set up SCSI DMA */
    331 	{
    332 		esc->sc_scsi_dma.nd_bst = NEXT68K_INTIO_BUS_SPACE;
    333 
    334 		if (bus_space_map(esc->sc_scsi_dma.nd_bst, NEXT_P_SCSI_CSR,
    335 				DD_SIZE,0, &esc->sc_scsi_dma.nd_bsh)) {
    336 			panic("\n%s: can't map scsi DMA registers",
    337 					sc->sc_dev.dv_xname);
    338 		}
    339 
    340 		esc->sc_scsi_dma.nd_intr = NEXT_I_SCSI_DMA;
    341 		esc->sc_scsi_dma.nd_shutdown_cb  = &esp_dmacb_shutdown;
    342 		esc->sc_scsi_dma.nd_continue_cb  = &esp_dmacb_continue;
    343 		esc->sc_scsi_dma.nd_completed_cb = &esp_dmacb_completed;
    344 		esc->sc_scsi_dma.nd_cb_arg       = sc;
    345 		nextdma_config(&esc->sc_scsi_dma);
    346 		nextdma_init(&esc->sc_scsi_dma);
    347 
    348 #if 0
    349 		/* Turn on target selection using the `dma' method */
    350 		sc->sc_features |= NCR_F_DMASELECT;
    351 #endif
    352 
    353 		esc->sc_datain = -1;
    354 		esc->sc_dmaaddr = 0;
    355 		esc->sc_dmalen  = 0;
    356 		esc->sc_dmasize = 0;
    357 
    358 		esc->sc_loaded = 0;
    359 
    360 		esc->sc_begin = 0;
    361 		esc->sc_begin_size = 0;
    362 
    363 		{
    364 			int error;
    365 			if ((error = bus_dmamap_create(esc->sc_scsi_dma.nd_dmat,
    366 					sc->sc_maxxfer, sc->sc_maxxfer/NBPG, sc->sc_maxxfer,
    367 					0, BUS_DMA_ALLOCNOW, &esc->sc_main_dmamap)) != 0) {
    368 				panic("%s: can't create main i/o DMA map, error = %d",
    369 						sc->sc_dev.dv_xname,error);
    370 			}
    371 		}
    372 		esc->sc_main = 0;
    373 		esc->sc_main_size = 0;
    374 
    375 		{
    376 			int error;
    377 			if ((error = bus_dmamap_create(esc->sc_scsi_dma.nd_dmat,
    378 					ESP_DMA_TAILBUFSIZE,
    379 					1, ESP_DMA_TAILBUFSIZE,
    380 					0, BUS_DMA_ALLOCNOW, &esc->sc_tail_dmamap)) != 0) {
    381 				panic("%s: can't create tail i/o DMA map, error = %d",
    382 						sc->sc_dev.dv_xname,error);
    383 			}
    384 		}
    385 		esc->sc_tail = 0;
    386 		esc->sc_tail_size = 0;
    387 
    388 	}
    389 
    390 	/* Establish interrupt channel */
    391 	isrlink_autovec(ncr53c9x_intr, sc, NEXT_I_IPL(NEXT_I_SCSI), 0);
    392 	INTR_ENABLE(NEXT_I_SCSI);
    393 
    394 	/* register interrupt stats */
    395 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    396 	    sc->sc_dev.dv_xname, "intr");
    397 
    398 	/* Do the common parts of attachment. */
    399 	ncr53c9x_attach(sc, NULL, NULL);
    400 }
    401 
    402 /*
    403  * Glue functions.
    404  */
    405 
    406 u_char
    407 esp_read_reg(sc, reg)
    408 	struct ncr53c9x_softc *sc;
    409 	int reg;
    410 {
    411 	struct esp_softc *esc = (struct esp_softc *)sc;
    412 
    413 	return(bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg));
    414 }
    415 
    416 void
    417 esp_write_reg(sc, reg, val)
    418 	struct ncr53c9x_softc *sc;
    419 	int reg;
    420 	u_char val;
    421 {
    422 	struct esp_softc *esc = (struct esp_softc *)sc;
    423 
    424 	bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg, val);
    425 }
    426 
    427 int
    428 esp_dma_isintr(sc)
    429 	struct ncr53c9x_softc *sc;
    430 {
    431 	struct esp_softc *esc = (struct esp_softc *)sc;
    432 
    433 	int r = (INTR_OCCURRED(NEXT_I_SCSI));
    434 
    435 	if (r) {
    436 
    437 		{
    438 			int flushcount;
    439 			int s;
    440 			s = spldma();
    441 
    442 			flushcount = 0;
    443 
    444 #ifdef ESP_DEBUG
    445 			esp_dma_nest++;
    446 
    447 			if (esp_debug) {
    448 				char sbuf[256];
    449 
    450 				bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),
    451 						 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    452 				printf("esp_dma_isintr = 0x%s\n", sbuf);
    453 			}
    454 #endif
    455 
    456 			while (esp_dma_isactive(sc)) {
    457 				flushcount++;
    458 
    459 #ifdef DIAGNOSTIC
    460 				r = (INTR_OCCURRED(NEXT_I_SCSI));
    461 				if (!r) panic("esp intr enabled but dma failed to flush");
    462 #endif
    463 #ifdef DIAGNOSTIC
    464 #if 0
    465 				if ((esc->sc_loaded & (ESP_LOADED_TAIL/* |ESP_UNLOADED_MAIN */))
    466 						!= (ESP_LOADED_TAIL /* |ESP_UNLOADED_MAIN */)) {
    467 					if (esc->sc_datain) {
    468 						NCR_WRITE_REG(sc, ESP_DCTL,
    469 								ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
    470 					} else {
    471 						NCR_WRITE_REG(sc, ESP_DCTL,
    472 								ESPDCTL_20MHZ | ESPDCTL_INTENB);
    473 					}
    474 					next_dma_print(&esc->sc_scsi_dma);
    475 					esp_dma_print(sc);
    476 					printf("%s: unexpected flush: tc=0x%06x\n",
    477 							sc->sc_dev.dv_xname,
    478 							(((sc->sc_cfg2 & NCRCFG2_FE)
    479 									? NCR_READ_REG(sc, NCR_TCH) : 0)<<16)|
    480 							(NCR_READ_REG(sc, NCR_TCM)<<8)|
    481 							NCR_READ_REG(sc, NCR_TCL));
    482 					ncr53c9x_readregs(sc);
    483 					printf("%s: readregs[intr=%02x,stat=%02x,step=%02x]\n",
    484 							sc->sc_dev.dv_xname,
    485 							sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
    486 					panic("%s: flushing flushing non-tail dma\n",
    487 							sc->sc_dev.dv_xname);
    488 				}
    489 #endif
    490 #endif
    491 				DPRINTF(("%s: flushing dma, count = %d\n", sc->sc_dev.dv_xname,flushcount));
    492 				if (esc->sc_datain) {
    493 					NCR_WRITE_REG(sc, ESP_DCTL,
    494 							ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD | ESPDCTL_FLUSH);
    495 					DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    496 					NCR_WRITE_REG(sc, ESP_DCTL,
    497 							ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
    498 				} else {
    499 					NCR_WRITE_REG(sc, ESP_DCTL,
    500 							ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_FLUSH);
    501 					DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    502 					NCR_WRITE_REG(sc, ESP_DCTL,
    503 							ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
    504 				}
    505 				DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    506 
    507 				{
    508 					int nr;
    509 					nr = nextdma_intr(&esc->sc_scsi_dma);
    510 					if (nr) {
    511 						DPRINTF(("nextma_intr = %d\n",nr));
    512 #ifdef DIAGNOSTIC
    513 						if (flushcount > 4) {
    514 							printf("%s: unexpected flushcount %d\n",sc->sc_dev.dv_xname,flushcount);
    515 						}
    516 #endif
    517 #ifdef DIAGNOSTIC
    518 #if 0
    519 						if (esp_dma_isactive(sc)) {
    520 							esp_dma_print(sc);
    521 							printf("%s: dma still active after a flush with count %d\n",
    522 									sc->sc_dev.dv_xname,flushcount);
    523 
    524 						}
    525 #endif
    526 #endif
    527 						flushcount = 0;
    528 					}
    529 				}
    530 			}
    531 
    532 #ifdef ESP_DEBUG
    533 			esp_dma_nest--;
    534 #endif
    535 
    536 			splx(s);
    537 		}
    538 
    539 #ifdef DIAGNOSTIC
    540 		r = (INTR_OCCURRED(NEXT_I_SCSI));
    541 		if (!r) panic("esp intr not enabled after dma flush");
    542 #endif
    543 
    544 		/* Clear the DMAMOD bit in the DCTL register, since if this
    545 		 * routine returns true, then the ncr53c9x_intr handler will
    546 		 * be called and needs access to the scsi registers.
    547 		 */
    548 		if (esc->sc_datain) {
    549 			NCR_WRITE_REG(sc, ESP_DCTL,
    550 					ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
    551 		} else {
    552 			NCR_WRITE_REG(sc, ESP_DCTL,
    553 					ESPDCTL_20MHZ | ESPDCTL_INTENB);
    554 		}
    555 		DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    556 
    557 	}
    558 
    559 	return (r);
    560 }
    561 
    562 void
    563 esp_dma_reset(sc)
    564 	struct ncr53c9x_softc *sc;
    565 {
    566 	struct esp_softc *esc = (struct esp_softc *)sc;
    567 
    568 	DPRINTF(("esp dma reset\n"));
    569 
    570 #ifdef ESP_DEBUG
    571 	if (esp_debug) {
    572 		char sbuf[256];
    573 
    574 		bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),
    575 				 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    576 		printf("  *intrstat = 0x%s\n", sbuf);
    577 
    578 		bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),
    579 				 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    580 		printf("  *intrmask = 0x%s\n", sbuf);
    581 	}
    582 #endif
    583 
    584 	/* Clear the DMAMOD bit in the DCTL register: */
    585 	NCR_WRITE_REG(sc, ESP_DCTL,
    586 			ESPDCTL_20MHZ | ESPDCTL_INTENB);
    587 	DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    588 
    589 	nextdma_reset(&esc->sc_scsi_dma);
    590 
    591 	esc->sc_datain = -1;
    592 	esc->sc_dmaaddr = 0;
    593 	esc->sc_dmalen  = 0;
    594 	esc->sc_dmasize = 0;
    595 
    596 	esc->sc_loaded = 0;
    597 
    598 	esc->sc_begin = 0;
    599 	esc->sc_begin_size = 0;
    600 
    601 	if (esc->sc_main_dmamap->dm_mapsize) {
    602 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap);
    603 	}
    604 	esc->sc_main = 0;
    605 	esc->sc_main_size = 0;
    606 
    607 	if (esc->sc_tail_dmamap->dm_mapsize) {
    608 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap);
    609 	}
    610 	esc->sc_tail = 0;
    611 	esc->sc_tail_size = 0;
    612 }
    613 
    614 int
    615 esp_dma_intr(sc)
    616 	struct ncr53c9x_softc *sc;
    617 {
    618 #ifdef DIAGNOSTIC
    619 	panic("%s: esp_dma_intr shouldn't be invoked.\n", sc->sc_dev.dv_xname);
    620 #endif
    621 
    622 	return -1;
    623 }
    624 
    625 /* it appears that:
    626  * addr and len arguments to this need to be kept up to date
    627  * with the status of the transfter.
    628  * the dmasize of this is the actual length of the transfer
    629  * request, which is guaranteed to be less than maxxfer.
    630  * (len may be > maxxfer)
    631  */
    632 
    633 int
    634 esp_dma_setup(sc, addr, len, datain, dmasize)
    635 	struct ncr53c9x_softc *sc;
    636 	caddr_t *addr;
    637 	size_t *len;
    638 	int datain;
    639 	size_t *dmasize;
    640 {
    641 	struct esp_softc *esc = (struct esp_softc *)sc;
    642 
    643 #ifdef DIAGNOSTIC
    644 #ifdef ESP_DEBUG
    645 	/* if this is a read DMA, pre-fill the buffer with 0xdeadbeef
    646 	 * to identify bogus reads
    647 	 */
    648 	if (datain) {
    649 		int *v = (int *)(*addr);
    650 		int i;
    651 		for(i=0;i<((*len)/4);i++) v[i] = 0xdeadbeef;
    652 		v = (int *)(&(esc->sc_tailbuf[0]));
    653 		for(i=0;i<((sizeof(esc->sc_tailbuf)/4));i++) v[i] = 0xdeaffeed;
    654 	} else {
    655 		int *v;
    656 		int i;
    657 		v = (int *)(&(esc->sc_tailbuf[0]));
    658 		for(i=0;i<((sizeof(esc->sc_tailbuf)/4));i++) v[i] = 0xfeeb1eed;
    659 	}
    660 #endif
    661 #endif
    662 
    663 	DPRINTF(("esp_dma_setup(0x%08lx,0x%08lx,0x%08lx)\n",*addr,*len,*dmasize));
    664 
    665 #if 0
    666 #ifdef DIAGNOSTIC /* @@@ this is ok sometimes. verify that we handle it ok
    667 									 * and then remove this check
    668 									 */
    669 	if (*len != *dmasize) {
    670 		panic("esp dmalen 0x%lx != size 0x%lx",*len,*dmasize);
    671 	}
    672 #endif
    673 #endif
    674 
    675 #ifdef DIAGNOSTIC
    676 	if ((esc->sc_datain != -1) ||
    677 			(esc->sc_main_dmamap->dm_mapsize != 0) ||
    678 			(esc->sc_tail_dmamap->dm_mapsize != 0) ||
    679 			(esc->sc_dmasize != 0)) {
    680 		panic("%s: map already loaded in esp_dma_setup\n"
    681 				"\tdatain = %d\n\tmain_mapsize=%d\n\tail_mapsize=%d\n\tdmasize = %d",
    682 				sc->sc_dev.dv_xname, esc->sc_datain,
    683 				esc->sc_main_dmamap->dm_mapsize,
    684 				esc->sc_tail_dmamap->dm_mapsize,
    685 				esc->sc_dmasize);
    686 	}
    687 #endif
    688 
    689 	/* we are sometimes asked to dma zero  bytes, that's easy */
    690 	if (*dmasize <= 0) {
    691 		return(0);
    692 	}
    693 
    694 	/* Save these in case we have to abort DMA */
    695 	esc->sc_datain   = datain;
    696 	esc->sc_dmaaddr  = addr;
    697 	esc->sc_dmalen   = len;
    698 	esc->sc_dmasize  = *dmasize;
    699 
    700 	esc->sc_loaded = 0;
    701 
    702 #define DMA_SCSI_ALIGNMENT 16
    703 #define DMA_SCSI_ALIGN(type, addr)	\
    704 	((type)(((unsigned)(addr)+DMA_SCSI_ALIGNMENT-1) \
    705 		&~(DMA_SCSI_ALIGNMENT-1)))
    706 #define DMA_SCSI_ALIGNED(addr) \
    707 	(((unsigned)(addr)&(DMA_SCSI_ALIGNMENT-1))==0)
    708 
    709 	{
    710 		size_t slop_bgn_size; /* # bytes to be fifo'd at beginning */
    711 		size_t slop_end_size; /* # bytes to be transferred in tail buffer */
    712 
    713 		{
    714 			u_long bgn = (u_long)(*esc->sc_dmaaddr);
    715 			u_long end = (u_long)(*esc->sc_dmaaddr+esc->sc_dmasize);
    716 
    717 			slop_bgn_size = DMA_SCSI_ALIGNMENT-(bgn % DMA_SCSI_ALIGNMENT);
    718 			if (slop_bgn_size == DMA_SCSI_ALIGNMENT) slop_bgn_size = 0;
    719 			slop_end_size = (end % DMA_ENDALIGNMENT);
    720 		}
    721 
    722 		/* Force a minimum slop end size. This ensures that write
    723 		 * requests will overrun, as required to get completion interrupts.
    724 		 * In addition, since the tail buffer is guaranteed to be mapped
    725 		 * in a single dma segment, the overrun won't accidentally
    726 		 * end up in its own segment.
    727 		 */
    728 		if (!esc->sc_datain) {
    729 #if 0
    730 			slop_end_size += ESP_DMA_MAXTAIL;
    731 #else
    732 			slop_end_size += 0x10;
    733 #endif
    734 		}
    735 
    736 		/* Check to make sure we haven't counted extra slop
    737 		 * as would happen for a very short dma buffer, also
    738 		 * for short buffers, just stuff the entire thing in the tail
    739 		 */
    740 		if ((slop_bgn_size+slop_end_size >= esc->sc_dmasize)
    741 #if 0
    742 				|| (esc->sc_dmasize <= ESP_DMA_MAXTAIL)
    743 #endif
    744 				)
    745 		{
    746  			slop_bgn_size = 0;
    747 			slop_end_size = esc->sc_dmasize;
    748 		}
    749 
    750 		/* initialize the fifo buffer */
    751 		if (slop_bgn_size) {
    752 			esc->sc_begin = *esc->sc_dmaaddr;
    753 			esc->sc_begin_size = slop_bgn_size;
    754 		} else {
    755 			esc->sc_begin = 0;
    756 			esc->sc_begin_size = 0;
    757 		}
    758 
    759 		/* Load the normal DMA map */
    760 		{
    761 			esc->sc_main      = *esc->sc_dmaaddr+slop_bgn_size;
    762 			esc->sc_main_size = (esc->sc_dmasize)-(slop_end_size+slop_bgn_size);
    763 
    764 			if (esc->sc_main_size) {
    765 				int error;
    766 				error = bus_dmamap_load(esc->sc_scsi_dma.nd_dmat,
    767 						esc->sc_main_dmamap,
    768 						esc->sc_main, esc->sc_main_size,
    769 						NULL, BUS_DMA_NOWAIT);
    770 				if (error) {
    771 					panic("%s: can't load main dma map. error = %d, addr=0x%08x, size=0x%08x",
    772 							sc->sc_dev.dv_xname, error,esc->sc_main,esc->sc_main_size);
    773 				}
    774 #if 0
    775 				bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap,
    776 						0, esc->sc_main_dmamap->dm_mapsize,
    777 						(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    778 #endif
    779 			} else {
    780 				esc->sc_main = 0;
    781 			}
    782 		}
    783 
    784 		/* Load the tail DMA map */
    785 		if (slop_end_size) {
    786 			esc->sc_tail      = DMA_ENDALIGN(caddr_t,esc->sc_tailbuf+slop_end_size)-slop_end_size;
    787 			/* If the beginning of the tail is not correctly aligned,
    788 			 * we have no choice but to align the start, which might then unalign the end.
    789 			 */
    790 			esc->sc_tail      = DMA_SCSI_ALIGN(caddr_t,esc->sc_tail);
    791 			/* So therefore, we change the tail size to be end aligned again. */
    792 			esc->sc_tail_size = DMA_ENDALIGN(caddr_t,esc->sc_tail+slop_end_size)-esc->sc_tail;
    793 
    794 			/* @@@ next dma overrun lossage */
    795 			if (!esc->sc_datain) {
    796 				esc->sc_tail_size += ESP_DMA_OVERRUN;
    797 			}
    798 
    799 			{
    800 				int error;
    801 				error = bus_dmamap_load(esc->sc_scsi_dma.nd_dmat,
    802 						esc->sc_tail_dmamap,
    803 						esc->sc_tail, esc->sc_tail_size,
    804 						NULL, BUS_DMA_NOWAIT);
    805 				if (error) {
    806 					panic("%s: can't load tail dma map. error = %d, addr=0x%08x, size=0x%08x",
    807 							sc->sc_dev.dv_xname, error,esc->sc_tail,esc->sc_tail_size);
    808 				}
    809 #if 0
    810 				bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap,
    811 						0, esc->sc_tail_dmamap->dm_mapsize,
    812 						(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    813 #endif
    814 			}
    815 		}
    816 	}
    817 
    818 	return (0);
    819 }
    820 
    821 #ifdef ESP_DEBUG
    822 /* For debugging */
    823 void
    824 esp_dma_store(sc)
    825 	struct ncr53c9x_softc *sc;
    826 {
    827 	struct esp_softc *esc = (struct esp_softc *)sc;
    828 	char *p = &esp_dma_dump[0];
    829 
    830 	p += sprintf(p,"%s: sc_datain=%d\n",sc->sc_dev.dv_xname,esc->sc_datain);
    831 	p += sprintf(p,"%s: sc_loaded=0x%08x\n",sc->sc_dev.dv_xname,esc->sc_loaded);
    832 
    833 	if (esc->sc_dmaaddr) {
    834 		p += sprintf(p,"%s: sc_dmaaddr=0x%08lx\n",sc->sc_dev.dv_xname,*esc->sc_dmaaddr);
    835 	} else {
    836 		p += sprintf(p,"%s: sc_dmaaddr=NULL\n",sc->sc_dev.dv_xname);
    837 	}
    838 	if (esc->sc_dmalen) {
    839 		p += sprintf(p,"%s: sc_dmalen=0x%08lx\n",sc->sc_dev.dv_xname,*esc->sc_dmalen);
    840 	} else {
    841 		p += sprintf(p,"%s: sc_dmalen=NULL\n",sc->sc_dev.dv_xname);
    842 	}
    843 	p += sprintf(p,"%s: sc_dmasize=0x%08x\n",sc->sc_dev.dv_xname,esc->sc_dmasize);
    844 
    845 	p += sprintf(p,"%s: sc_begin = 0x%08x, sc_begin_size = 0x%08x\n",
    846 			sc->sc_dev.dv_xname, esc->sc_begin, esc->sc_begin_size);
    847 	p += sprintf(p,"%s: sc_main = 0x%08x, sc_main_size = 0x%08x\n",
    848 			sc->sc_dev.dv_xname, esc->sc_main, esc->sc_main_size);
    849 	{
    850 		int i;
    851 		bus_dmamap_t map = esc->sc_main_dmamap;
    852 		p += sprintf(p,"%s: sc_main_dmamap. mapsize = 0x%08x, nsegs = %d\n",
    853 				sc->sc_dev.dv_xname, map->dm_mapsize, map->dm_nsegs);
    854 		for(i=0;i<map->dm_nsegs;i++) {
    855 			p += sprintf(p,"%s: map->dm_segs[%d]->ds_addr = 0x%08x, len = 0x%08x\n",
    856 			sc->sc_dev.dv_xname, i, map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len);
    857 		}
    858 	}
    859 	p += sprintf(p,"%s: sc_tail = 0x%08x, sc_tail_size = 0x%08x\n",
    860 			sc->sc_dev.dv_xname, esc->sc_tail, esc->sc_tail_size);
    861 	{
    862 		int i;
    863 		bus_dmamap_t map = esc->sc_tail_dmamap;
    864 		p += sprintf(p,"%s: sc_tail_dmamap. mapsize = 0x%08x, nsegs = %d\n",
    865 				sc->sc_dev.dv_xname, map->dm_mapsize, map->dm_nsegs);
    866 		for(i=0;i<map->dm_nsegs;i++) {
    867 			p += sprintf(p,"%s: map->dm_segs[%d]->ds_addr = 0x%08x, len = 0x%08x\n",
    868 			sc->sc_dev.dv_xname, i, map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len);
    869 		}
    870 	}
    871 }
    872 
    873 void
    874 esp_dma_print(sc)
    875 	struct ncr53c9x_softc *sc;
    876 {
    877 	esp_dma_store(sc);
    878 	printf("%s",esp_dma_dump);
    879 }
    880 #endif
    881 
    882 void
    883 esp_dma_go(sc)
    884 	struct ncr53c9x_softc *sc;
    885 {
    886 	struct esp_softc *esc = (struct esp_softc *)sc;
    887 
    888 	DPRINTF(("%s: esp_dma_go(datain = %d)\n",
    889 			sc->sc_dev.dv_xname, esc->sc_datain));
    890 
    891 #ifdef ESP_DEBUG
    892 	if (esp_debug) esp_dma_print(sc);
    893 	else esp_dma_store(sc);
    894 #endif
    895 
    896 #ifdef ESP_DEBUG
    897 	{
    898 		int n = NCR_READ_REG(sc, NCR_FFLAG);
    899 		DPRINTF(("%s: fifo size = %d, seq = 0x%x\n",
    900 				sc->sc_dev.dv_xname,
    901 				n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
    902 	}
    903 #endif
    904 
    905 	/* zero length dma transfers are boring */
    906 	if (esc->sc_dmasize == 0) {
    907 		return;
    908 	}
    909 
    910 #if defined(DIAGNOSTIC)
    911   if ((esc->sc_begin_size == 0) &&
    912 			(esc->sc_main_dmamap->dm_mapsize == 0) &&
    913 			(esc->sc_tail_dmamap->dm_mapsize == 0)) {
    914 		esp_dma_print(sc);
    915 		panic("%s: No DMA requested!",sc->sc_dev.dv_xname);
    916 	}
    917 #endif
    918 
    919 	/* Stuff the fifo with the begin buffer */
    920 	if (esc->sc_datain) {
    921 		int i;
    922 		DPRINTF(("%s: FIFO read of %d bytes:",
    923 				sc->sc_dev.dv_xname,esc->sc_begin_size));
    924 		for(i=0;i<esc->sc_begin_size;i++) {
    925 			esc->sc_begin[i]=NCR_READ_REG(sc, NCR_FIFO);
    926 			DPRINTF((" %02x",esc->sc_begin[i]&0xff));
    927 		}
    928 		DPRINTF(("\n"));
    929 	} else {
    930 		int i;
    931 		DPRINTF(("%s: FIFO write of %d bytes:",
    932 				sc->sc_dev.dv_xname,esc->sc_begin_size));
    933 		for(i=0;i<esc->sc_begin_size;i++) {
    934 			NCR_WRITE_REG(sc, NCR_FIFO, esc->sc_begin[i]);
    935 			DPRINTF((" %02x",esc->sc_begin[i]&0xff));
    936 		}
    937 		DPRINTF(("\n"));
    938 	}
    939 
    940 	/* if we are a dma write cycle, copy the end slop */
    941 	if (esc->sc_datain == 0) {
    942 		memcpy(esc->sc_tail,
    943 				(*esc->sc_dmaaddr+esc->sc_begin_size+esc->sc_main_size),
    944 				(esc->sc_dmasize-(esc->sc_begin_size+esc->sc_main_size)));
    945 	}
    946 
    947 	if (esc->sc_main_dmamap->dm_mapsize) {
    948 		bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap,
    949 				0, esc->sc_main_dmamap->dm_mapsize,
    950 				(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    951 	}
    952 
    953 	if (esc->sc_tail_dmamap->dm_mapsize) {
    954 		bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap,
    955 				0, esc->sc_tail_dmamap->dm_mapsize,
    956 				(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    957 	}
    958 
    959 	nextdma_start(&esc->sc_scsi_dma,
    960 			(esc->sc_datain ? DMACSR_SETREAD : DMACSR_SETWRITE));
    961 
    962 	if (esc->sc_datain) {
    963 		NCR_WRITE_REG(sc, ESP_DCTL,
    964 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
    965 	} else {
    966 		NCR_WRITE_REG(sc, ESP_DCTL,
    967 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
    968 	}
    969 	DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    970 }
    971 
    972 void
    973 esp_dma_stop(sc)
    974 	struct ncr53c9x_softc *sc;
    975 {
    976 	panic("Not yet implemented");
    977 }
    978 
    979 int
    980 esp_dma_isactive(sc)
    981 	struct ncr53c9x_softc *sc;
    982 {
    983 	struct esp_softc *esc = (struct esp_softc *)sc;
    984 	int r = !nextdma_finished(&esc->sc_scsi_dma);
    985 	DPRINTF(("esp_dma_isactive = %d\n",r));
    986 	return(r);
    987 }
    988 
    989 /****************************************************************/
    990 
    991 /* Internal dma callback routines */
    992 bus_dmamap_t
    993 esp_dmacb_continue(arg)
    994 	void *arg;
    995 {
    996 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    997 	struct esp_softc *esc = (struct esp_softc *)sc;
    998 
    999 	DPRINTF(("%s: dma continue\n",sc->sc_dev.dv_xname));
   1000 
   1001 #ifdef DIAGNOSTIC
   1002 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
   1003 		panic("%s: map not loaded in dma continue callback, datain = %d",
   1004 				sc->sc_dev.dv_xname,esc->sc_datain);
   1005 	}
   1006 #endif
   1007 
   1008 	if ((!(esc->sc_loaded & ESP_LOADED_MAIN)) &&
   1009 			(esc->sc_main_dmamap->dm_mapsize)) {
   1010 			DPRINTF(("%s: Loading main map\n",sc->sc_dev.dv_xname));
   1011 #if 0
   1012 			bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap,
   1013 					0, esc->sc_main_dmamap->dm_mapsize,
   1014 					(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
   1015 #endif
   1016 			esc->sc_loaded |= ESP_LOADED_MAIN;
   1017 			return(esc->sc_main_dmamap);
   1018 	}
   1019 
   1020 	if ((!(esc->sc_loaded & ESP_LOADED_TAIL)) &&
   1021 			(esc->sc_tail_dmamap->dm_mapsize)) {
   1022 			DPRINTF(("%s: Loading tail map\n",sc->sc_dev.dv_xname));
   1023 #if 0
   1024 			bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap,
   1025 					0, esc->sc_tail_dmamap->dm_mapsize,
   1026 					(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
   1027 #endif
   1028 			esc->sc_loaded |= ESP_LOADED_TAIL;
   1029 			return(esc->sc_tail_dmamap);
   1030 	}
   1031 
   1032 	DPRINTF(("%s: not loading map\n",sc->sc_dev.dv_xname));
   1033 	return(0);
   1034 }
   1035 
   1036 
   1037 void
   1038 esp_dmacb_completed(map, arg)
   1039 	bus_dmamap_t map;
   1040 	void *arg;
   1041 {
   1042 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
   1043 	struct esp_softc *esc = (struct esp_softc *)sc;
   1044 
   1045 	DPRINTF(("%s: dma completed\n",sc->sc_dev.dv_xname));
   1046 
   1047 #ifdef DIAGNOSTIC
   1048 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
   1049 		panic("%s: invalid dma direction in completed callback, datain = %d",
   1050 				sc->sc_dev.dv_xname,esc->sc_datain);
   1051 	}
   1052 #endif
   1053 
   1054 	if (map == esc->sc_main_dmamap) {
   1055 #ifdef DIAGNOSTIC
   1056 		if ((esc->sc_loaded & ESP_UNLOADED_MAIN) ||
   1057 				!(esc->sc_loaded & ESP_LOADED_MAIN)) {
   1058 			panic("%s: unexpected completed call for main map\n",sc->sc_dev.dv_xname);
   1059 		}
   1060 #endif
   1061 		esc->sc_loaded |= ESP_UNLOADED_MAIN;
   1062 	} else if (map == esc->sc_tail_dmamap) {
   1063 #ifdef DIAGNOSTIC
   1064 		if ((esc->sc_loaded & ESP_UNLOADED_TAIL) ||
   1065 				!(esc->sc_loaded & ESP_LOADED_TAIL)) {
   1066 			panic("%s: unexpected completed call for tail map\n",sc->sc_dev.dv_xname);
   1067 		}
   1068 #endif
   1069 		esc->sc_loaded |= ESP_UNLOADED_TAIL;
   1070 	}
   1071 #ifdef DIAGNOSTIC
   1072 	 else {
   1073 		panic("%s: unexpected completed map", sc->sc_dev.dv_xname);
   1074 	}
   1075 #endif
   1076 
   1077 #ifdef ESP_DEBUG
   1078 	if (esp_debug) {
   1079 		if (map == esc->sc_main_dmamap) {
   1080 			printf("%s: completed main map\n",sc->sc_dev.dv_xname);
   1081 		} else if (map == esc->sc_tail_dmamap) {
   1082 			printf("%s: completed tail map\n",sc->sc_dev.dv_xname);
   1083 		}
   1084 	}
   1085 #endif
   1086 
   1087 #if 0
   1088 	if ((map == esc->sc_tail_dmamap) ||
   1089 			((esc->sc_tail_size == 0) && (map == esc->sc_main_dmamap))) {
   1090 
   1091 		/* Clear the DMAMOD bit in the DCTL register to give control
   1092 		 * back to the scsi chip.
   1093 		 */
   1094 		if (esc->sc_datain) {
   1095 			NCR_WRITE_REG(sc, ESP_DCTL,
   1096 					ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
   1097 		} else {
   1098 			NCR_WRITE_REG(sc, ESP_DCTL,
   1099 					ESPDCTL_20MHZ | ESPDCTL_INTENB);
   1100 		}
   1101 		DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
   1102 	}
   1103 #endif
   1104 
   1105 
   1106 #if 0
   1107 	bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, map,
   1108 			0, map->dm_mapsize,
   1109 			(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
   1110 #endif
   1111 
   1112 }
   1113 
   1114 void
   1115 esp_dmacb_shutdown(arg)
   1116 	void *arg;
   1117 {
   1118 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
   1119 	struct esp_softc *esc = (struct esp_softc *)sc;
   1120 
   1121 	DPRINTF(("%s: dma shutdown\n",sc->sc_dev.dv_xname));
   1122 
   1123 #if 0
   1124 	{
   1125 		/* Clear the DMAMOD bit in the DCTL register to give control
   1126 		 * back to the scsi chip.
   1127 		 */
   1128 		if (esc->sc_datain) {
   1129 			NCR_WRITE_REG(sc, ESP_DCTL,
   1130 					ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
   1131 		} else {
   1132 			NCR_WRITE_REG(sc, ESP_DCTL,
   1133 					ESPDCTL_20MHZ | ESPDCTL_INTENB);
   1134 		}
   1135 		DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
   1136 	}
   1137 #endif
   1138 
   1139 	DPRINTF(("%s: esp_dma_nest == %d\n",sc->sc_dev.dv_xname,esp_dma_nest));
   1140 
   1141 	/* Stuff the end slop into fifo */
   1142 
   1143 #ifdef ESP_DEBUG
   1144 	if (esp_debug) {
   1145 
   1146 		int n = NCR_READ_REG(sc, NCR_FFLAG);
   1147 		DPRINTF(("%s: fifo size = %d, seq = 0x%x\n",
   1148 				sc->sc_dev.dv_xname,n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
   1149 	}
   1150 #endif
   1151 
   1152 	if (esc->sc_main_dmamap->dm_mapsize) {
   1153 		bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap,
   1154 			0, esc->sc_main_dmamap->dm_mapsize,
   1155 				(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
   1156 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap);
   1157 	}
   1158 
   1159 	if (esc->sc_tail_dmamap->dm_mapsize) {
   1160 		bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap,
   1161 			0, esc->sc_tail_dmamap->dm_mapsize,
   1162 				(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
   1163 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap);
   1164 	}
   1165 
   1166 	/* copy the tail dma buffer data for read transfers */
   1167 	if (esc->sc_datain == 1) {
   1168 		memcpy((*esc->sc_dmaaddr+esc->sc_begin_size+esc->sc_main_size),
   1169 				esc->sc_tail,
   1170 				(esc->sc_dmasize-(esc->sc_begin_size+esc->sc_main_size)));
   1171 	}
   1172 
   1173 #ifdef ESP_DEBUG
   1174 	if (esp_debug) {
   1175 		printf("%s: dma_shutdown: addr=0x%08lx,len=0x%08lx,size=0x%08lx\n",
   1176 				sc->sc_dev.dv_xname,
   1177 				*esc->sc_dmaaddr, *esc->sc_dmalen, esc->sc_dmasize);
   1178 		if (esp_debug > 10) {
   1179 			esp_hex_dump(*(esc->sc_dmaaddr),esc->sc_dmasize);
   1180 			printf("%s: tail=0x%08lx,tailbuf=0x%08lx,tail_size=0x%08lx\n",
   1181 					sc->sc_dev.dv_xname,
   1182 					esc->sc_tail, &(esc->sc_tailbuf[0]), esc->sc_tail_size);
   1183 			esp_hex_dump(&(esc->sc_tailbuf[0]),sizeof(esc->sc_tailbuf));
   1184 		}
   1185 	}
   1186 #endif
   1187 
   1188 	*(esc->sc_dmaaddr) += esc->sc_dmasize;
   1189 	*(esc->sc_dmalen)  -= esc->sc_dmasize;
   1190 
   1191 	esc->sc_main = 0;
   1192 	esc->sc_main_size = 0;
   1193 	esc->sc_tail = 0;
   1194 	esc->sc_tail_size = 0;
   1195 
   1196 	esc->sc_datain = -1;
   1197 	esc->sc_dmaaddr = 0;
   1198 	esc->sc_dmalen  = 0;
   1199 	esc->sc_dmasize = 0;
   1200 
   1201 	esc->sc_loaded = 0;
   1202 
   1203 	esc->sc_begin = 0;
   1204 	esc->sc_begin_size = 0;
   1205 
   1206 #ifdef ESP_DEBUG
   1207 	if (esp_debug) {
   1208 		char sbuf[256];
   1209 
   1210 		bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),
   1211 				 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
   1212 		printf("  *intrstat = 0x%s\n", sbuf);
   1213 
   1214 		bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),
   1215 				 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
   1216 		printf("  *intrmask = 0x%s\n", sbuf);
   1217 	}
   1218 #endif
   1219 }
   1220