Home | History | Annotate | Line # | Download | only in dev
si_vme.c revision 1.12
      1 /*	$NetBSD: si_vme.c,v 1.12 1997/08/27 11:24:27 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Glass, David Jones, and Gordon W. Ross.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * This file contains only the machine-dependent parts of the
     41  * Sun3 SCSI driver.  (Autoconfig stuff and DMA functions.)
     42  * The machine-independent parts are in ncr5380sbc.c
     43  *
     44  * Supported hardware includes:
     45  * Sun SCSI-3 on OBIO (Sun3/50,Sun3/60)
     46  * Sun SCSI-3 on VME (Sun3/160,Sun3/260)
     47  *
     48  * Could be made to support the Sun3/E if someone wanted to.
     49  *
     50  * Note:  Both supported variants of the Sun SCSI-3 adapter have
     51  * some really unusual "features" for this driver to deal with,
     52  * generally related to the DMA engine.  The OBIO variant will
     53  * ignore any attempt to write the FIFO count register while the
     54  * SCSI bus is in DATA_IN or DATA_OUT phase.  This is dealt with
     55  * by setting the FIFO count early in COMMAND or MSG_IN phase.
     56  *
     57  * The VME variant has a bit to enable or disable the DMA engine,
     58  * but that bit also gates the interrupt line from the NCR5380!
     59  * Therefore, in order to get any interrupt from the 5380, (i.e.
     60  * for reselect) one must clear the DMA engine transfer count and
     61  * then enable DMA.  This has the further complication that you
     62  * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
     63  * we have to turn DMA back off before we even look at the 5380.
     64  *
     65  * What wonderfully whacky hardware this is!
     66  *
     67  * Credits, history:
     68  *
     69  * David Jones wrote the initial version of this module, which
     70  * included support for the VME adapter only. (no reselection).
     71  *
     72  * Gordon Ross added support for the OBIO adapter, and re-worked
     73  * both the VME and OBIO code to support disconnect/reselect.
     74  * (Required figuring out the hardware "features" noted above.)
     75  *
     76  * The autoconfiguration boilerplate came from Adam Glass.
     77  */
     78 
     79 /*****************************************************************
     80  * VME functions for DMA
     81  ****************************************************************/
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/errno.h>
     86 #include <sys/kernel.h>
     87 #include <sys/malloc.h>
     88 #include <sys/device.h>
     89 #include <sys/buf.h>
     90 #include <sys/proc.h>
     91 #include <sys/user.h>
     92 
     93 #include <dev/scsipi/scsi_all.h>
     94 #include <dev/scsipi/scsipi_all.h>
     95 #include <dev/scsipi/scsipi_debug.h>
     96 #include <dev/scsipi/scsiconf.h>
     97 
     98 #include <machine/autoconf.h>
     99 #include <machine/dvma.h>
    100 
    101 #define DEBUG XXX
    102 
    103 #include <dev/ic/ncr5380reg.h>
    104 #include <dev/ic/ncr5380var.h>
    105 
    106 #include "sireg.h"
    107 #include "sivar.h"
    108 
    109 void si_vme_dma_setup __P((struct ncr5380_softc *));
    110 void si_vme_dma_start __P((struct ncr5380_softc *));
    111 void si_vme_dma_eop __P((struct ncr5380_softc *));
    112 void si_vme_dma_stop __P((struct ncr5380_softc *));
    113 
    114 void si_vme_intr_on  __P((struct ncr5380_softc *));
    115 void si_vme_intr_off __P((struct ncr5380_softc *));
    116 
    117 /*
    118  * New-style autoconfig attachment
    119  */
    120 
    121 static int	si_vmes_match __P((struct device *, struct cfdata *, void *));
    122 static void	si_vmes_attach __P((struct device *, struct device *, void *));
    123 
    124 struct cfattach si_vmes_ca = {
    125 	sizeof(struct si_softc), si_vmes_match, si_vmes_attach
    126 };
    127 
    128 /*
    129  * Options for disconnect/reselect, DMA, and interrupts.
    130  * By default, allow disconnect/reselect on targets 4-6.
    131  * Those are normally tapes that really need it enabled.
    132  */
    133 int si_vme_options = 0x0f;
    134 
    135 
    136 static int
    137 si_vmes_match(parent, cf, args)
    138 	struct device	*parent;
    139 	struct cfdata *cf;
    140 	void *args;
    141 {
    142 	struct confargs *ca = args;
    143 	int probe_addr;
    144 
    145 #ifdef	DIAGNOSTIC
    146 	if (ca->ca_bustype != BUS_VME16) {
    147 		printf("si_vmes_match: bustype %d?\n", ca->ca_bustype);
    148 		return (0);
    149 	}
    150 #endif
    151 
    152 	/*
    153 	 * Other Sun3 models may have VME "si" or "sc".
    154 	 * This driver has no default address.
    155 	 */
    156 	if (ca->ca_paddr == -1)
    157 		return (0);
    158 
    159 	/* Make sure there is something there... */
    160 	probe_addr = ca->ca_paddr + 1;
    161 	if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1)
    162 		return (0);
    163 
    164 	/*
    165 	 * If this is a VME SCSI board, we have to determine whether
    166 	 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board.  This can
    167 	 * be determined using the fact that the "sc" board occupies
    168 	 * 4K bytes in VME space but the "si" board occupies 2K bytes.
    169 	 */
    170 	/* Note: the "si" board should NOT respond here. */
    171 	probe_addr = ca->ca_paddr + 0x801;
    172 	if (bus_peek(ca->ca_bustype, probe_addr, 1) != -1) {
    173 		/* Something responded at 2K+1.  Maybe an "sc" board? */
    174 #ifdef	DEBUG
    175 		printf("si_vmes_match: May be an `sc' board at pa=0x%x\n",
    176 			   ca->ca_paddr);
    177 #endif
    178 		return(0);
    179 	}
    180 
    181 	/* Default interrupt priority (always splbio==2) */
    182 	if (ca->ca_intpri == -1)
    183 		ca->ca_intpri = 2;
    184 
    185 	return (1);
    186 }
    187 
    188 static void
    189 si_vmes_attach(parent, self, args)
    190 	struct device	*parent, *self;
    191 	void		*args;
    192 {
    193 	struct si_softc *sc = (struct si_softc *) self;
    194 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
    195 	struct cfdata *cf = self->dv_cfdata;
    196 	struct confargs *ca = args;
    197 
    198 	/* Get options from config flags if specified. */
    199 	if (cf->cf_flags)
    200 		sc->sc_options = cf->cf_flags;
    201 	else
    202 		sc->sc_options = si_vme_options;
    203 
    204 	printf(": options=0x%x\n", sc->sc_options);
    205 
    206 	sc->sc_adapter_type = ca->ca_bustype;
    207 	sc->sc_regs = (struct si_regs *)
    208 		bus_mapin(ca->ca_bustype, ca->ca_paddr,
    209 				sizeof(struct si_regs));
    210 	sc->sc_adapter_iv_am =
    211 		VME_SUPV_DATA_24 | (ca->ca_intvec & 0xFF);
    212 
    213 	/*
    214 	 * MD function pointers used by the MI code.
    215 	 */
    216 	ncr_sc->sc_pio_out = ncr5380_pio_out;
    217 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
    218 	ncr_sc->sc_dma_alloc = si_dma_alloc;
    219 	ncr_sc->sc_dma_free  = si_dma_free;
    220 	ncr_sc->sc_dma_setup = si_vme_dma_setup;
    221 	ncr_sc->sc_dma_start = si_vme_dma_start;
    222 	ncr_sc->sc_dma_poll  = si_dma_poll;
    223 	ncr_sc->sc_dma_eop   = si_vme_dma_eop;
    224 	ncr_sc->sc_dma_stop  = si_vme_dma_stop;
    225 	ncr_sc->sc_intr_on   = si_vme_intr_on;
    226 	ncr_sc->sc_intr_off  = si_vme_intr_off;
    227 
    228 	/* Attach interrupt handler. */
    229 	isr_add_vectored(si_intr, (void *)sc,
    230 		ca->ca_intpri, ca->ca_intvec);
    231 
    232 	/* Do the common attach stuff. */
    233 	si_attach(sc);
    234 }
    235 
    236 
    237 /*
    238  * This is called when the bus is going idle,
    239  * so we want to enable the SBC interrupts.
    240  * That is controlled by the DMA enable!
    241  * Who would have guessed!
    242  * What a NASTY trick!
    243  */
    244 void
    245 si_vme_intr_on(ncr_sc)
    246 	struct ncr5380_softc *ncr_sc;
    247 {
    248 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    249 	volatile struct si_regs *si = sc->sc_regs;
    250 
    251 	/* receive mode should be safer */
    252 	si->si_csr &= ~SI_CSR_SEND;
    253 
    254 	/* Clear the count so nothing happens. */
    255 	si->dma_counth = 0;
    256 	si->dma_countl = 0;
    257 
    258 	/* Clear the start address too. (paranoid?) */
    259 	si->dma_addrh = 0;
    260 	si->dma_addrl = 0;
    261 
    262 	/* Finally, enable the DMA engine. */
    263 	si->si_csr |= SI_CSR_DMA_EN;
    264 }
    265 
    266 /*
    267  * This is called when the bus is idle and we are
    268  * about to start playing with the SBC chip.
    269  */
    270 void
    271 si_vme_intr_off(ncr_sc)
    272 	struct ncr5380_softc *ncr_sc;
    273 {
    274 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    275 	volatile struct si_regs *si = sc->sc_regs;
    276 
    277 	si->si_csr &= ~SI_CSR_DMA_EN;
    278 }
    279 
    280 /*
    281  * This function is called during the COMMAND or MSG_IN phase
    282  * that preceeds a DATA_IN or DATA_OUT phase, in case we need
    283  * to setup the DMA engine before the bus enters a DATA phase.
    284  *
    285  * XXX: The VME adapter appears to suppress SBC interrupts
    286  * when the FIFO is not empty or the FIFO count is non-zero!
    287  *
    288  * On the VME version, setup the start addres, but clear the
    289  * count (to make sure it stays idle) and set that later.
    290  */
    291 void
    292 si_vme_dma_setup(ncr_sc)
    293 	struct ncr5380_softc *ncr_sc;
    294 {
    295 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    296 	struct sci_req *sr = ncr_sc->sc_current;
    297 	struct si_dma_handle *dh = sr->sr_dma_hand;
    298 	volatile struct si_regs *si = sc->sc_regs;
    299 	long data_pa;
    300 	int xlen;
    301 
    302 	/*
    303 	 * Get the DVMA mapping for this segment.
    304 	 * XXX - Should separate allocation and mapin.
    305 	 */
    306 	data_pa = dvma_kvtopa(dh->dh_dvma, sc->sc_adapter_type);
    307 	data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
    308 	if (data_pa & 1)
    309 		panic("si_dma_start: bad pa=0x%lx", data_pa);
    310 	xlen = ncr_sc->sc_datalen;
    311 	xlen &= ~1;				/* XXX: necessary? */
    312 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
    313 
    314 #ifdef	DEBUG
    315 	if (si_debug & 2) {
    316 		printf("si_dma_setup: dh=%p, pa=0x%lx, xlen=0x%x\n",
    317 			   dh, data_pa, xlen);
    318 	}
    319 #endif
    320 
    321 	/* Set direction (send/recv) */
    322 	if (dh->dh_flags & SIDH_OUT) {
    323 		si->si_csr |= SI_CSR_SEND;
    324 	} else {
    325 		si->si_csr &= ~SI_CSR_SEND;
    326 	}
    327 
    328 	/* Reset the FIFO. */
    329 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
    330 	si->si_csr |= SI_CSR_FIFO_RES;
    331 
    332 	if (data_pa & 2) {
    333 		si->si_csr |= SI_CSR_BPCON;
    334 	} else {
    335 		si->si_csr &= ~SI_CSR_BPCON;
    336 	}
    337 
    338 	/* Load the start address. */
    339 	si->dma_addrh = (ushort)(data_pa >> 16);
    340 	si->dma_addrl = (ushort)(data_pa & 0xFFFF);
    341 
    342 	/*
    343 	 * Keep the count zero or it may start early!
    344 	 */
    345 	si->dma_counth = 0;
    346 	si->dma_countl = 0;
    347 
    348 #if 0
    349 	/* Clear FIFO counter. (also hits dma_count) */
    350 	si->fifo_cnt_hi = 0;
    351 	si->fifo_count = 0;
    352 #endif
    353 }
    354 
    355 
    356 void
    357 si_vme_dma_start(ncr_sc)
    358 	struct ncr5380_softc *ncr_sc;
    359 {
    360 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    361 	struct sci_req *sr = ncr_sc->sc_current;
    362 	struct si_dma_handle *dh = sr->sr_dma_hand;
    363 	volatile struct si_regs *si = sc->sc_regs;
    364 	int s, xlen;
    365 
    366 	xlen = sc->sc_reqlen;
    367 
    368 	/* This MAY be time critical (not sure). */
    369 	s = splhigh();
    370 
    371 	si->dma_counth = (ushort)(xlen >> 16);
    372 	si->dma_countl = (ushort)(xlen & 0xFFFF);
    373 
    374 	/* Set it anyway, even though dma_count hits it. */
    375 	si->fifo_cnt_hi = (ushort)(xlen >> 16);
    376 	si->fifo_count  = (ushort)(xlen & 0xFFFF);
    377 
    378 	/*
    379 	 * Acknowledge the phase change.  (After DMA setup!)
    380 	 * Put the SBIC into DMA mode, and start the transfer.
    381 	 */
    382 	if (dh->dh_flags & SIDH_OUT) {
    383 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
    384 		SCI_CLR_INTR(ncr_sc);
    385 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
    386 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    387 		*ncr_sc->sci_dma_send = 0;	/* start it */
    388 	} else {
    389 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
    390 		SCI_CLR_INTR(ncr_sc);
    391 		*ncr_sc->sci_icmd = 0;
    392 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    393 		*ncr_sc->sci_irecv = 0;	/* start it */
    394 	}
    395 
    396 	/* Let'er rip! */
    397 	si->si_csr |= SI_CSR_DMA_EN;
    398 
    399 	splx(s);
    400 	ncr_sc->sc_state |= NCR_DOINGDMA;
    401 
    402 #ifdef	DEBUG
    403 	if (si_debug & 2) {
    404 		printf("si_dma_start: started, flags=0x%x\n",
    405 			   ncr_sc->sc_state);
    406 	}
    407 #endif
    408 }
    409 
    410 
    411 void
    412 si_vme_dma_eop(ncr_sc)
    413 	struct ncr5380_softc *ncr_sc;
    414 {
    415 
    416 	/* Not needed - DMA was stopped prior to examining sci_csr */
    417 }
    418 
    419 
    420 void
    421 si_vme_dma_stop(ncr_sc)
    422 	struct ncr5380_softc *ncr_sc;
    423 {
    424 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    425 	struct sci_req *sr = ncr_sc->sc_current;
    426 	struct si_dma_handle *dh = sr->sr_dma_hand;
    427 	volatile struct si_regs *si = sc->sc_regs;
    428 	int resid, ntrans;
    429 
    430 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    431 #ifdef	DEBUG
    432 		printf("si_dma_stop: dma not running\n");
    433 #endif
    434 		return;
    435 	}
    436 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
    437 
    438 	/* First, halt the DMA engine. */
    439 	si->si_csr &= ~SI_CSR_DMA_EN;	/* VME only */
    440 
    441 	/* Set an impossible phase to prevent data movement? */
    442 	*ncr_sc->sci_tcmd = PHASE_INVALID;
    443 
    444 	if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
    445 		printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
    446 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
    447 		ncr_sc->sc_state |= NCR_ABORTING;
    448 		si_reset_adapter(ncr_sc);
    449 		goto out;
    450 	}
    451 
    452 	/* Note that timeout may have set the error flag. */
    453 	if (ncr_sc->sc_state & NCR_ABORTING)
    454 		goto out;
    455 
    456 	/* XXX: Wait for DMA to actually finish? */
    457 
    458 	/*
    459 	 * Now try to figure out how much actually transferred
    460 	 *
    461 	 * The fifo_count does not reflect how many bytes were
    462 	 * actually transferred for VME.
    463 	 *
    464 	 * SCSI-3 VME interface is a little funny on writes:
    465 	 * if we have a disconnect, the dma has overshot by
    466 	 * one byte and the resid needs to be incremented.
    467 	 * Only happens for partial transfers.
    468 	 * (Thanks to Matt Jacob)
    469 	 */
    470 
    471 	resid = si->fifo_count & 0xFFFF;
    472 	if (dh->dh_flags & SIDH_OUT)
    473 		if ((resid > 0) && (resid < sc->sc_reqlen))
    474 			resid++;
    475 	ntrans = sc->sc_reqlen - resid;
    476 
    477 #ifdef	DEBUG
    478 	if (si_debug & 2) {
    479 		printf("si_dma_stop: resid=0x%x ntrans=0x%x\n",
    480 		       resid, ntrans);
    481 	}
    482 #endif
    483 
    484 	if (ntrans < MIN_DMA_LEN) {
    485 		printf("si: fifo count: 0x%x\n", resid);
    486 		ncr_sc->sc_state |= NCR_ABORTING;
    487 		goto out;
    488 	}
    489 	if (ntrans > ncr_sc->sc_datalen)
    490 		panic("si_dma_stop: excess transfer");
    491 
    492 	/* Adjust data pointer */
    493 	ncr_sc->sc_dataptr += ntrans;
    494 	ncr_sc->sc_datalen -= ntrans;
    495 
    496 	/*
    497 	 * After a read, we may need to clean-up
    498 	 * "Left-over bytes" (yuck!)
    499 	 */
    500 	if (((dh->dh_flags & SIDH_OUT) == 0) &&
    501 		((si->si_csr & SI_CSR_LOB) != 0))
    502 	{
    503 		char *cp = ncr_sc->sc_dataptr;
    504 #ifdef DEBUG
    505 		printf("si: Got Left-over bytes!\n");
    506 #endif
    507 		if (si->si_csr & SI_CSR_BPCON) {
    508 			/* have SI_CSR_BPCON */
    509 			cp[-1] = (si->si_bprl & 0xff00) >> 8;
    510 		} else {
    511 			switch (si->si_csr & SI_CSR_LOB) {
    512 			case SI_CSR_LOB_THREE:
    513 				cp[-3] = (si->si_bprh & 0xff00) >> 8;
    514 				cp[-2] = (si->si_bprh & 0x00ff);
    515 				cp[-1] = (si->si_bprl & 0xff00) >> 8;
    516 				break;
    517 			case SI_CSR_LOB_TWO:
    518 				cp[-2] = (si->si_bprh & 0xff00) >> 8;
    519 				cp[-1] = (si->si_bprh & 0x00ff);
    520 				break;
    521 			case SI_CSR_LOB_ONE:
    522 				cp[-1] = (si->si_bprh & 0xff00) >> 8;
    523 				break;
    524 			}
    525 		}
    526 	}
    527 
    528 out:
    529 	si->dma_addrh = 0;
    530 	si->dma_addrl = 0;
    531 
    532 	si->dma_counth = 0;
    533 	si->dma_countl = 0;
    534 
    535 	si->fifo_cnt_hi = 0;
    536 	si->fifo_count  = 0;
    537 
    538 	/* Put SBIC back in PIO mode. */
    539 	*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
    540 	*ncr_sc->sci_icmd = 0;
    541 }
    542