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