Home | History | Annotate | Line # | Download | only in dev
esp.c revision 1.14
      1 /*	$NetBSD: esp.c,v 1.14 1999/01/27 06:37:49 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 #if 1
    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 /* Linkup to the rest of the kernel */
    137 struct cfattach esp_ca = {
    138 	sizeof(struct esp_softc), espmatch_intio, espattach_intio
    139 };
    140 
    141 struct scsipi_device esp_dev = {
    142 	NULL,			/* Use default error handler */
    143 	NULL,			/* have a queue, served by this */
    144 	NULL,			/* have no async handler */
    145 	NULL,			/* Use default 'done' routine */
    146 };
    147 
    148 /*
    149  * Functions and the switch for the MI code.
    150  */
    151 u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
    152 void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    153 int	esp_dma_isintr __P((struct ncr53c9x_softc *));
    154 void	esp_dma_reset __P((struct ncr53c9x_softc *));
    155 int	esp_dma_intr __P((struct ncr53c9x_softc *));
    156 int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    157 	    size_t *, int, size_t *));
    158 void	esp_dma_go __P((struct ncr53c9x_softc *));
    159 void	esp_dma_stop __P((struct ncr53c9x_softc *));
    160 int	esp_dma_isactive __P((struct ncr53c9x_softc *));
    161 
    162 struct ncr53c9x_glue esp_glue = {
    163 	esp_read_reg,
    164 	esp_write_reg,
    165 	esp_dma_isintr,
    166 	esp_dma_reset,
    167 	esp_dma_intr,
    168 	esp_dma_setup,
    169 	esp_dma_go,
    170 	esp_dma_stop,
    171 	esp_dma_isactive,
    172 	NULL,			/* gl_clear_latched_intr */
    173 };
    174 
    175 #ifdef ESP_DEBUG
    176 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
    177 static void
    178 esp_hex_dump(unsigned char *pkt, size_t len)
    179 {
    180 	size_t i, j;
    181 
    182 	printf("0000: ");
    183 	for(i=0; i<len; i++) {
    184 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
    185 		if ((i+1) % 16 == 0) {
    186 			printf("  %c", '"');
    187 			for(j=0; j<16; j++)
    188 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
    189 			printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
    190 				XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
    191 		}
    192 	}
    193 	printf("\n");
    194 }
    195 #endif
    196 
    197 int
    198 espmatch_intio(parent, cf, aux)
    199 	struct device *parent;
    200 	struct cfdata *cf;
    201 	void *aux;
    202 {
    203   /* should probably probe here */
    204   /* Should also probably set up data from config */
    205 
    206 #if 1
    207 /* this code isn't working yet, don't match on it */
    208 	return(0);
    209 #else
    210 	return(1);
    211 #endif
    212 }
    213 
    214 void
    215 espattach_intio(parent, self, aux)
    216 	struct device *parent, *self;
    217 	void *aux;
    218 {
    219 	struct esp_softc *esc = (void *)self;
    220 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    221 
    222 	esc->sc_bst = NEXT68K_INTIO_BUS_SPACE;
    223 	if (bus_space_map(esc->sc_bst, NEXT_P_SCSI,
    224 			ESP_DEVICE_SIZE, 0, &esc->sc_bsh)) {
    225     panic("\n%s: can't map ncr53c90 registers",
    226 				sc->sc_dev.dv_xname);
    227 	}
    228 
    229 	sc->sc_id = 7;
    230 	sc->sc_freq = 20;							/* Mhz */
    231 
    232 	/*
    233 	 * Set up glue for MI code early; we use some of it here.
    234 	 */
    235 	sc->sc_glue = &esp_glue;
    236 
    237 	/*
    238 	 * XXX More of this should be in ncr53c9x_attach(), but
    239 	 * XXX should we really poke around the chip that much in
    240 	 * XXX the MI code?  Think about this more...
    241 	 */
    242 
    243 	/*
    244 	 * It is necessary to try to load the 2nd config register here,
    245 	 * to find out what rev the esp chip is, else the ncr53c9x_reset
    246 	 * will not set up the defaults correctly.
    247 	 */
    248 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    249 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
    250 	sc->sc_cfg3 = NCRCFG3_CDB;
    251 	NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    252 
    253 	if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
    254 	    (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
    255 		sc->sc_rev = NCR_VARIANT_ESP100;
    256 	} else {
    257 		sc->sc_cfg2 = NCRCFG2_SCSI2;
    258 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    259 		sc->sc_cfg3 = 0;
    260 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    261 		sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
    262 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    263 		if (NCR_READ_REG(sc, NCR_CFG3) !=
    264 		    (NCRCFG3_CDB | NCRCFG3_FCLK)) {
    265 			sc->sc_rev = NCR_VARIANT_ESP100A;
    266 		} else {
    267 			/* NCRCFG2_FE enables > 64K transfers */
    268 			sc->sc_cfg2 |= NCRCFG2_FE;
    269 			sc->sc_cfg3 = 0;
    270 			NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    271 			sc->sc_rev = NCR_VARIANT_ESP200;
    272 		}
    273 	}
    274 
    275 	/*
    276 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    277 	 * XXX but it appears to have some dependency on what sort
    278 	 * XXX of DMA we're hooked up to, etc.
    279 	 */
    280 
    281 	/*
    282 	 * This is the value used to start sync negotiations
    283 	 * Note that the NCR register "SYNCTP" is programmed
    284 	 * in "clocks per byte", and has a minimum value of 4.
    285 	 * The SCSI period used in negotiation is one-fourth
    286 	 * of the time (in nanoseconds) needed to transfer one byte.
    287 	 * Since the chip's clock is given in MHz, we have the following
    288 	 * formula: 4 * period = (1000 / freq) * 4
    289 	 */
    290 	sc->sc_minsync = 1000 / sc->sc_freq;
    291 
    292 	/*
    293 	 * Alas, we must now modify the value a bit, because it's
    294 	 * only valid when can switch on FASTCLK and FASTSCSI bits
    295 	 * in config register 3...
    296 	 */
    297 	switch (sc->sc_rev) {
    298 	case NCR_VARIANT_ESP100:
    299 		sc->sc_maxxfer = 64 * 1024;
    300 		sc->sc_minsync = 0;	/* No synch on old chip? */
    301 		break;
    302 
    303 	case NCR_VARIANT_ESP100A:
    304 		sc->sc_maxxfer = 64 * 1024;
    305 		/* Min clocks/byte is 5 */
    306 		sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
    307 		break;
    308 
    309 	case NCR_VARIANT_ESP200:
    310 		sc->sc_maxxfer = 16 * 1024 * 1024;
    311 		/* XXX - do actually set FAST* bits */
    312 		break;
    313 	}
    314 
    315 	/* @@@ Some ESP_DCTL bits probably need setting */
    316 	NCR_WRITE_REG(sc, ESP_DCTL,
    317 			ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_RESET);
    318 	DELAY(10);
    319 	NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_20MHZ | ESPDCTL_INTENB);
    320 	DELAY(10);
    321 
    322 	/* Set up SCSI DMA */
    323 	{
    324 		esc->sc_scsi_dma.nd_bst = NEXT68K_INTIO_BUS_SPACE;
    325 
    326 		if (bus_space_map(esc->sc_scsi_dma.nd_bst, NEXT_P_SCSI_CSR,
    327 				sizeof(struct dma_dev),0, &esc->sc_scsi_dma.nd_bsh)) {
    328 			panic("\n%s: can't map scsi DMA registers",
    329 					sc->sc_dev.dv_xname);
    330 		}
    331 
    332 		esc->sc_scsi_dma.nd_intr = NEXT_I_SCSI_DMA;
    333 		esc->sc_scsi_dma.nd_shutdown_cb  = &esp_dmacb_shutdown;
    334 		esc->sc_scsi_dma.nd_continue_cb  = &esp_dmacb_continue;
    335 		esc->sc_scsi_dma.nd_completed_cb = &esp_dmacb_completed;
    336 		esc->sc_scsi_dma.nd_cb_arg       = sc;
    337 		nextdma_config(&esc->sc_scsi_dma);
    338 		nextdma_init(&esc->sc_scsi_dma);
    339 
    340 		{
    341 			int error;
    342 			if ((error = bus_dmamap_create(esc->sc_scsi_dma.nd_dmat,
    343 					sc->sc_maxxfer, 1, sc->sc_maxxfer,
    344 					0, BUS_DMA_ALLOCNOW, &esc->sc_dmamap)) != 0) {
    345 				panic("%s: can't create i/o DMA map, error = %d",
    346 						sc->sc_dev.dv_xname,error);
    347 			}
    348 		}
    349 
    350 		{
    351 			int error;
    352 			if ((error = bus_dmamap_create(esc->sc_scsi_dma.nd_dmat,
    353 					ESP_DMA_MAXTAIL, 1, ESP_DMA_MAXTAIL,
    354 					0, BUS_DMA_ALLOCNOW, &esc->sc_tail_dmamap)) != 0) {
    355 				panic("%s: can't create tail i/o DMA map, error = %d",
    356 						sc->sc_dev.dv_xname,error);
    357 			}
    358 		}
    359 	}
    360 
    361 #if 0
    362 	/* Turn on target selection using the `dma' method */
    363 	ncr53c9x_dmaselect = 1;
    364 #else
    365 	ncr53c9x_dmaselect = 0;
    366 #endif
    367 
    368 	esc->sc_datain = -1;
    369 	esc->sc_slop_bgn_addr = 0;
    370 	esc->sc_slop_bgn_size = 0;
    371 	esc->sc_slop_end_addr = 0;
    372 	esc->sc_slop_end_size = 0;
    373 	esc->sc_dmamap_loaded = 0;
    374 	esc->sc_tail = 0;
    375 	esc->sc_tail_size = 0;
    376 
    377 	/* Establish interrupt channel */
    378 	isrlink_autovec((int(*)__P((void*)))ncr53c9x_intr, sc,
    379 			NEXT_I_IPL(NEXT_I_SCSI), 0);
    380 	INTR_ENABLE(NEXT_I_SCSI);
    381 
    382 	/* register interrupt stats */
    383 	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
    384 
    385 	/* Do the common parts of attachment. */
    386 	sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
    387 	sc->sc_adapter.scsipi_minphys = minphys;
    388 	ncr53c9x_attach(sc, &esp_dev);
    389 }
    390 
    391 /*
    392  * Glue functions.
    393  */
    394 
    395 u_char
    396 esp_read_reg(sc, reg)
    397 	struct ncr53c9x_softc *sc;
    398 	int reg;
    399 {
    400 	struct esp_softc *esc = (struct esp_softc *)sc;
    401 
    402 	return(bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg));
    403 }
    404 
    405 void
    406 esp_write_reg(sc, reg, val)
    407 	struct ncr53c9x_softc *sc;
    408 	int reg;
    409 	u_char val;
    410 {
    411 	struct esp_softc *esc = (struct esp_softc *)sc;
    412 
    413 	bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg, val);
    414 }
    415 
    416 int
    417 esp_dma_isintr(sc)
    418 	struct ncr53c9x_softc *sc;
    419 {
    420 	struct esp_softc *esc = (struct esp_softc *)sc;
    421 
    422 	int r = (INTR_OCCURRED(NEXT_I_SCSI));
    423 
    424 	if (r) {
    425 		DPRINTF(("esp_dma_isintr = 0x%b\n",
    426 				(*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),NEXT_INTR_BITS));
    427 
    428 		if (esp_dma_isactive(sc)) {
    429 			if (esc->sc_datain) {
    430 				NCR_WRITE_REG(sc, ESP_DCTL,
    431 						ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD | ESPDCTL_FLUSH);
    432 				NCR_WRITE_REG(sc, ESP_DCTL,
    433 						ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
    434 			} else {
    435 				NCR_WRITE_REG(sc, ESP_DCTL,
    436 						ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_FLUSH);
    437 				NCR_WRITE_REG(sc, ESP_DCTL,
    438 						ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
    439 			}
    440 			nextdma_intr(&esc->sc_scsi_dma);
    441 			return 1;
    442 		}
    443 
    444 		/* Clear the DMAMOD bit in the DCTL register, since if this
    445 		 * routine returns true, then the ncr53c9x_intr handler will
    446 		 * be called and needs access to the scsi registers.
    447 		 */
    448 		if (esc->sc_datain) {
    449 			NCR_WRITE_REG(sc, ESP_DCTL,
    450 					ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
    451 		} else {
    452 			NCR_WRITE_REG(sc, ESP_DCTL,
    453 					ESPDCTL_20MHZ | ESPDCTL_INTENB);
    454 		}
    455 
    456 	}
    457 
    458 	return (r);
    459 }
    460 
    461 void
    462 esp_dma_reset(sc)
    463 	struct ncr53c9x_softc *sc;
    464 {
    465 	struct esp_softc *esc = (struct esp_softc *)sc;
    466 
    467 	DPRINTF(("esp dma reset\n"));
    468 
    469 #ifdef ESP_DEBUG
    470 	if (esp_debug) {
    471 		printf("  *intrstat = 0x%b\n",
    472 				(*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),NEXT_INTR_BITS);
    473 		printf("  *intrmask = 0x%b\n",
    474 				(*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),NEXT_INTR_BITS);
    475 	}
    476 #endif
    477 
    478 
    479 	/* Clear the DMAMOD bit in the DCTL register: */
    480 	if (esc->sc_datain) {
    481 		NCR_WRITE_REG(sc, ESP_DCTL,
    482 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
    483 	} else {
    484 		NCR_WRITE_REG(sc, ESP_DCTL,
    485 				ESPDCTL_20MHZ | ESPDCTL_INTENB);
    486 	}
    487 
    488 	nextdma_reset(&esc->sc_scsi_dma);
    489 
    490 	if (esc->sc_dmamap_loaded) {
    491 		/* fixing this is slightly complicated since multiple maps may be loaded,
    492 		 * and the esc->sc_dmamap_loaded variable only indicates the most recent one.
    493 		 */
    494 		panic("invoking completed callbacks upon esp_dma_reset is not yet implemented");
    495 
    496 		esp_dmacb_completed(esc->sc_dmamap,sc);
    497 		esp_dmacb_completed(esc->sc_tail_dmamap,sc);
    498 	}
    499 
    500 	esp_dmacb_shutdown(sc);				/* this will clean up */
    501 }
    502 
    503 int
    504 esp_dma_intr(sc)
    505 	struct ncr53c9x_softc *sc;
    506 {
    507 	int trans;
    508 	int resid;
    509 	int datain;
    510 	struct esp_softc *esc = (struct esp_softc *)sc;
    511 
    512 	datain = esc->sc_datain;
    513 
    514 	DPRINTF(("esp_dma_intr resetting dma\n"));
    515 
    516 	/* If the dma hasn't finished when we are in a scsi
    517 	 * interrupt. Then, "Houston, we have a problem."
    518 	 * Stop DMA and figure out how many bytes were transferred
    519 	 */
    520 	esp_dma_reset(sc);
    521 
    522 	resid = 0;
    523 
    524 	/*
    525 	 * If a transfer onto the SCSI bus gets interrupted by the device
    526 	 * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
    527 	 * as residual since the ESP counter registers get decremented as
    528 	 * bytes are clocked into the FIFO.
    529 	 */
    530 
    531 	if (! datain) {
    532 		resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF);
    533 		if (resid) {
    534 			NCR_DMA(("dmaintr: empty esp FIFO of %d ", resid));
    535 			NCRCMD(sc, NCRCMD_FLUSH);
    536 			DELAY(1);
    537 		}
    538 	}
    539 
    540 	if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
    541 		/*
    542 		 * `Terminal count' is off, so read the residue
    543 		 * out of the ESP counter registers.
    544 		 */
    545 		resid += (NCR_READ_REG(sc, NCR_TCL) |
    546 			  (NCR_READ_REG(sc, NCR_TCM) << 8) |
    547 			   ((sc->sc_cfg2 & NCRCFG2_FE)
    548 				? (NCR_READ_REG(sc, NCR_TCH) << 16)
    549 				: 0));
    550 
    551 		if (resid == 0 && esc->sc_dmasize == 65536 &&
    552 		    (sc->sc_cfg2 & NCRCFG2_FE) == 0)
    553 			/* A transfer of 64K is encoded as `TCL=TCM=0' */
    554 			resid = 65536;
    555 	}
    556 
    557 	trans = esc->sc_dmasize - resid;
    558 	if (trans < 0) {			/* transferred < 0 ? */
    559 #if 0
    560 		/*
    561 		 * This situation can happen in perfectly normal operation
    562 		 * if the ESP is reselected while using DMA to select
    563 		 * another target.  As such, don't print the warning.
    564 		 */
    565 		printf("%s: xfer (%d) > req (%d)\n",
    566 		    esc->sc_dev.dv_xname, trans, esc->sc_dmasize);
    567 #endif
    568 		trans = esc->sc_dmasize;
    569 	}
    570 
    571 	NCR_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n",
    572 		NCR_READ_REG(sc, NCR_TCL),
    573 		NCR_READ_REG(sc, NCR_TCM),
    574 		(sc->sc_cfg2 & NCRCFG2_FE)
    575 			? NCR_READ_REG(sc, NCR_TCH) : 0,
    576 		trans, resid));
    577 
    578 #ifdef ESP_DEBUG
    579 	if (esp_debug) esp_hex_dump(*(esc->sc_dmaaddr),esc->sc_dmasize);
    580 #endif
    581 
    582 	*esc->sc_dmalen -= trans;
    583 	*esc->sc_dmaaddr += trans;
    584 
    585 	return 0;
    586 }
    587 
    588 int
    589 esp_dma_setup(sc, addr, len, datain, dmasize)
    590 	struct ncr53c9x_softc *sc;
    591 	caddr_t *addr;
    592 	size_t *len;
    593 	int datain;
    594 	size_t *dmasize;
    595 {
    596 	struct esp_softc *esc = (struct esp_softc *)sc;
    597 
    598 #ifdef DIAGNOSTIC
    599 	/* if this is a read DMA, pre-fill the buffer with 0xdeadbeef
    600 	 * to identify bogus reads
    601 	 */
    602 	if (datain) {
    603 		int *v = (int *)(*addr);
    604 		int i;
    605 		for(i=0;i<((*len)/4);i++) v[i] = 0xdeadbeef;
    606 	}
    607 #endif
    608 
    609 	DPRINTF(("esp_dma_setup(0x%08lx,0x%08lx,0x%08lx)\n",*addr,*len,*dmasize));
    610 
    611 #ifdef DIAGNOSTIC /* @@@ this is ok sometimes. verify that we handle it ok
    612 									 * and then remove this check
    613 									 */
    614 	if (*len != *dmasize) {
    615 		panic("esp dmalen != size");
    616 	}
    617 #endif
    618 
    619 #ifdef DIAGNOSTIC
    620 	if ((esc->sc_datain != -1) ||
    621 			(esc->sc_dmamap->dm_mapsize != 0) ||
    622 			(esc->sc_dmamap_loaded != 0)) {
    623 		panic("%s: map already loaded in esp_dma_setup\n"
    624 				"\tdatain = %d\n\tmapsize=%d\n\tloaed = %d",
    625 				sc->sc_dev.dv_xname,esc->sc_datain,esc->sc_dmamap->dm_mapsize,
    626 				esc->sc_dmamap_loaded);
    627 	}
    628 #endif
    629 
    630 	/* Save these in case we have to abort DMA */
    631 	esc->sc_datain   = datain;
    632 	esc->sc_dmaaddr  = addr;
    633 	esc->sc_dmalen   = len;
    634 	esc->sc_dmasize  = *dmasize;
    635 
    636 	/* Deal with DMA alignment issues, by stuffing the FIFO.
    637 	 * This assumes that if bus_dmamap_load is given an aligned
    638 	 * buffer, then it will generate aligned hardware addresses
    639 	 * to give to the device.  Perhaps that is not a good assumption,
    640 	 * but it is probably true. [dbj (at) netbsd.org:19980719.0135EDT]
    641 	 */
    642 	{
    643 		int slop_bgn_size; /* # bytes to be fifo'd at beginning */
    644 		int slop_end_size; /* # bytes to be fifo'd at end */
    645 
    646 		{
    647 			u_long bgn = (u_long)(*esc->sc_dmaaddr);
    648 			u_long end = (u_long)(*esc->sc_dmaaddr+esc->sc_dmasize);
    649 
    650 			slop_bgn_size = DMA_BEGINALIGNMENT-(bgn % DMA_BEGINALIGNMENT);
    651 			if (slop_bgn_size == DMA_BEGINALIGNMENT) slop_bgn_size = 0;
    652 			slop_end_size = end % DMA_ENDALIGNMENT;
    653 		}
    654 
    655 		/* Check to make sure we haven't counted extra slop
    656 		 * as would happen for a very short dma buffer, also
    657 		 * for short buffers, just stuff the entire thing in the tail
    658 		 */
    659 		if ((slop_bgn_size+slop_end_size >= esc->sc_dmasize) ||
    660 				(esc->sc_dmasize <= ESP_DMA_MAXTAIL)) {
    661  			slop_bgn_size = 0;
    662 			slop_end_size = esc->sc_dmasize;
    663 		} else {
    664 			panic("Chaining DMA interrupts are currently broken.\n");	/* @@@ */
    665 		}
    666 
    667 		esc->sc_slop_bgn_addr = *esc->sc_dmaaddr;
    668 		esc->sc_slop_bgn_size = slop_bgn_size;
    669 		esc->sc_slop_end_addr = (*esc->sc_dmaaddr+esc->sc_dmasize)-slop_end_size;
    670 		esc->sc_slop_end_size = slop_end_size;
    671 	}
    672 
    673 	/* Load the normal DMA map */
    674 	if (esc->sc_dmasize-(esc->sc_slop_bgn_size+esc->sc_slop_end_size)) {
    675 		int error;
    676 		error = bus_dmamap_load(esc->sc_scsi_dma.nd_dmat,
    677 				esc->sc_dmamap,
    678 				*esc->sc_dmaaddr+esc->sc_slop_bgn_size,
    679 				esc->sc_dmasize-(esc->sc_slop_bgn_size+esc->sc_slop_end_size),
    680 				NULL, BUS_DMA_NOWAIT);
    681 		if (error) {
    682 			panic("%s: can't load dma map. error = %d",
    683 					sc->sc_dev.dv_xname, error);
    684 		}
    685 	}
    686 
    687 	/* Now set up the tail dma buffer, including alignment. */
    688 	if (esc->sc_slop_end_size) {
    689 		esc->sc_tail = DMA_ENDALIGN(caddr_t,esc->sc_tailbuf+esc->sc_slop_end_size)-esc->sc_slop_end_size;
    690 		/* If the beginning of the tail is not correctly aligned,
    691 		 * we have no choice but to align the start, which might then unalign the end.
    692 		 */
    693 		esc->sc_tail = DMA_ALIGN(caddr_t,esc->sc_tail);
    694 		/* So therefore, we change the tail size to be end aligned again. */
    695 		esc->sc_tail_size = DMA_ENDALIGN(caddr_t,esc->sc_tail+esc->sc_slop_end_size)-esc->sc_tail;
    696 
    697 		{
    698 			int error;
    699 			error = bus_dmamap_load(esc->sc_scsi_dma.nd_dmat,
    700 					esc->sc_tail_dmamap,
    701 					esc->sc_tail, esc->sc_tail_size,
    702 					NULL, BUS_DMA_NOWAIT);
    703 			if (error) {
    704 				panic("%s: can't load dma map. error = %d",
    705 						sc->sc_dev.dv_xname, error);
    706 			}
    707 		}
    708 	} else {
    709 		esc->sc_tail = 0;
    710 		esc->sc_tail_size = 0;
    711 	}
    712 
    713 	return (0);
    714 }
    715 
    716 void
    717 esp_dma_go(sc)
    718 	struct ncr53c9x_softc *sc;
    719 {
    720 	struct esp_softc *esc = (struct esp_softc *)sc;
    721 
    722 	DPRINTF(("esp_dma_go(datain = %d)\n",esc->sc_datain));
    723 
    724 	DPRINTF(("\tbgn slop = %d\n\tend slop = %d\n\tmapsize = %d\n",
    725 			esc->sc_slop_bgn_size,esc->sc_slop_end_size,
    726 			esc->sc_dmamap->dm_mapsize));
    727 
    728 #ifdef DIAGNOSTIC
    729 	{
    730 		int n = NCR_READ_REG(sc, NCR_FFLAG);
    731 		DPRINTF(("esp fifo size = %d, seq = 0x%x\n",n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
    732 	}
    733 #endif
    734 
    735 	if (esc->sc_datain) {
    736 		int i;
    737 		for(i=0;i<esc->sc_slop_bgn_size;i++) {
    738 			esc->sc_slop_bgn_addr[i]=NCR_READ_REG(sc, NCR_FIFO);
    739 		}
    740 	} else {
    741 		int i;
    742 		for(i=0;i<esc->sc_slop_bgn_size;i++) {
    743 			NCR_WRITE_REG(sc, NCR_FIFO, esc->sc_slop_bgn_addr[i]);
    744 		}
    745 	}
    746 
    747 #ifdef DIAGNOSTIC
    748 	{
    749 		int n = NCR_READ_REG(sc, NCR_FFLAG);
    750 		DPRINTF(("esp fifo size = %d, seq = 0x%x\n",n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
    751 	}
    752 #endif
    753 
    754 #if defined(DIAGNOSTIC)
    755 	if ((esc->sc_dmamap->dm_mapsize == 0) && (esc->sc_tail_dmamap->dm_mapsize == 0)) {
    756 		panic("%s: No DMA requested!");
    757 	}
    758 #endif
    759 
    760 	/* if we are a dma write cycle, copy the end slop */
    761 	if (esc->sc_datain == 0) {
    762 		memcpy(esc->sc_tail,esc->sc_slop_end_addr,esc->sc_slop_end_size);
    763 	}
    764 
    765 	nextdma_start(&esc->sc_scsi_dma,
    766 			(esc->sc_datain ? DMACSR_READ : DMACSR_WRITE));
    767 
    768 	if (esc->sc_datain) {
    769 		NCR_WRITE_REG(sc, ESP_DCTL,
    770 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
    771 	} else {
    772 		NCR_WRITE_REG(sc, ESP_DCTL,
    773 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
    774 	}
    775 }
    776 
    777 void
    778 esp_dma_stop(sc)
    779 	struct ncr53c9x_softc *sc;
    780 {
    781 	panic("Not yet implemented");
    782 }
    783 
    784 int
    785 esp_dma_isactive(sc)
    786 	struct ncr53c9x_softc *sc;
    787 {
    788 	struct esp_softc *esc = (struct esp_softc *)sc;
    789 	int r = !nextdma_finished(&esc->sc_scsi_dma);
    790 	DPRINTF(("esp_dma_isactive = %d\n",r));
    791 	return(r);
    792 }
    793 
    794 /****************************************************************/
    795 
    796 /* Internal dma callback routines */
    797 bus_dmamap_t
    798 esp_dmacb_continue(arg)
    799 	void *arg;
    800 {
    801 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    802 	struct esp_softc *esc = (struct esp_softc *)sc;
    803 
    804 	DPRINTF(("esp dma continue\n"));
    805 
    806 #ifdef DIAGNOSTIC
    807 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
    808 		panic("%s: map not loaded in dma continue callback, datain = %d",
    809 				sc->sc_dev.dv_xname,esc->sc_datain);
    810 	}
    811 #endif
    812 	switch(esc->sc_dmamap_loaded) {
    813 	case 0:
    814 		if (esc->sc_dmamap->dm_mapsize) {
    815 			bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_dmamap,
    816 					0, esc->sc_dmamap->dm_mapsize,
    817 					(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    818 			esc->sc_dmamap_loaded = 1;
    819 			DPRINTF(("Loading primary map\n"));
    820 			return(esc->sc_dmamap);
    821 		}
    822 		/* Fallthrough */
    823 	case 1:
    824 		if (esc->sc_tail_dmamap->dm_mapsize) {
    825 			bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap,
    826 					0, esc->sc_tail_dmamap->dm_mapsize,
    827 					(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    828 			esc->sc_dmamap_loaded = 2;
    829 			DPRINTF(("Loading tail map\n"));
    830 			return(esc->sc_tail_dmamap);
    831 		}
    832 		/* Fallthrough */
    833 	case 2:
    834 		DPRINTF(("Not loading map\n"));
    835 		return(0);
    836 	default:
    837 		panic("%s: Unexpected sc_dmamap_loaded (%d) in continue_cb",
    838 				sc->sc_dev.dv_xname,esc->sc_dmamap_loaded);
    839 	}
    840 }
    841 
    842 
    843 void
    844 esp_dmacb_completed(map, arg)
    845 	bus_dmamap_t map;
    846 	void *arg;
    847 {
    848 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    849 	struct esp_softc *esc = (struct esp_softc *)sc;
    850 
    851 	DPRINTF(("esp dma completed\n"));
    852 
    853 #ifdef DIAGNOSTIC
    854 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
    855 		panic("%s: map not loaded in dma completed callback, datain = %d, loaded = %d",
    856 				sc->sc_dev.dv_xname,esc->sc_datain,esc->sc_dmamap_loaded);
    857 	}
    858 	if ((map != esc->sc_dmamap) && (map != esc->sc_tail_dmamap)) {
    859 		panic("%s: unexpected completed map", sc->sc_dev.dv_xname);
    860 	}
    861 #endif
    862 
    863 #if 0
    864 	if (esc->sc_datain) {					/* @@@ this may not be needed */
    865 		NCR_WRITE_REG(sc, ESP_DCTL,
    866 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
    867 	} else {
    868 		NCR_WRITE_REG(sc, ESP_DCTL,
    869 				ESPDCTL_20MHZ | ESPDCTL_INTENB);
    870 	}
    871 #endif
    872 
    873 	bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, map,
    874 			0, map->dm_mapsize,
    875 			(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
    876 
    877 #if 0
    878 	if (esc->sc_datain) {					/* @@@ this may not be needed */
    879 		NCR_WRITE_REG(sc, ESP_DCTL,
    880 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
    881 	} else {
    882 		NCR_WRITE_REG(sc, ESP_DCTL,
    883 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
    884 	}
    885 #endif
    886 
    887 }
    888 
    889 void
    890 esp_dmacb_shutdown(arg)
    891 	void *arg;
    892 {
    893 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    894 	struct esp_softc *esc = (struct esp_softc *)sc;
    895 
    896 	DPRINTF(("esp dma shutdown\n"));
    897 
    898 	/* Stuff the end slop into fifo */
    899 
    900 #ifdef ESP_DEBUG
    901 	if (esp_debug) {
    902 
    903 		int n = NCR_READ_REG(sc, NCR_FFLAG);
    904 		DPRINTF(("esp fifo size = %d, seq = 0x%x\n",n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
    905 
    906 		NCR_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d\n",
    907 				NCR_READ_REG(sc, NCR_TCL),
    908 				NCR_READ_REG(sc, NCR_TCM),
    909 				(sc->sc_cfg2 & NCRCFG2_FE)
    910 				? NCR_READ_REG(sc, NCR_TCH) : 0));
    911 	}
    912 #endif
    913 
    914 	if (esc->sc_dmamap->dm_mapsize) {
    915 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_dmamap);
    916 	}
    917 	if (esc->sc_tail_dmamap->dm_mapsize) {
    918 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap);
    919 	}
    920 
    921 	if (esc->sc_datain == 1) {
    922 		memcpy(esc->sc_slop_end_addr,esc->sc_tail,esc->sc_slop_end_size);
    923 	}
    924 
    925 #ifdef ESP_DEBUG
    926 	if (esp_debug) esp_hex_dump(*(esc->sc_dmaaddr),esc->sc_dmasize);
    927 #endif
    928 
    929 	esc->sc_datain = -1;
    930 	esc->sc_slop_bgn_addr = 0;
    931 	esc->sc_slop_bgn_size = 0;
    932 	esc->sc_slop_end_addr = 0;
    933 	esc->sc_slop_end_size = 0;
    934 	esc->sc_dmamap_loaded = 0;
    935 	esc->sc_tail = 0;
    936 	esc->sc_tail_size = 0;
    937 }
    938