Home | History | Annotate | Line # | Download | only in dev
si.c revision 1.35.2.1
      1  1.35.2.1    bouyer /*	$NetBSD: si.c,v 1.35.2.1 1997/07/01 17:34:35 bouyer Exp $	*/
      2       1.8       cgd 
      3      1.31       gwr /*-
      4      1.31       gwr  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5       1.1     glass  * All rights reserved.
      6       1.1     glass  *
      7      1.31       gwr  * This code is derived from software contributed to The NetBSD Foundation
      8      1.31       gwr  * by Adam Glass, David Jones, and Gordon W. Ross.
      9      1.31       gwr  *
     10       1.1     glass  * Redistribution and use in source and binary forms, with or without
     11       1.1     glass  * modification, are permitted provided that the following conditions
     12       1.1     glass  * are met:
     13       1.1     glass  * 1. Redistributions of source code must retain the above copyright
     14       1.1     glass  *    notice, this list of conditions and the following disclaimer.
     15       1.1     glass  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     glass  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     glass  *    documentation and/or other materials provided with the distribution.
     18      1.31       gwr  * 3. All advertising materials mentioning features or use of this software
     19       1.1     glass  *    must display the following acknowledgement:
     20      1.31       gwr  *        This product includes software developed by the NetBSD
     21      1.31       gwr  *        Foundation, Inc. and its contributors.
     22      1.31       gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.31       gwr  *    contributors may be used to endorse or promote products derived
     24      1.31       gwr  *    from this software without specific prior written permission.
     25       1.1     glass  *
     26      1.31       gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.31       gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.31       gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.33       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.33       gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.31       gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.31       gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.31       gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.31       gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.31       gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.31       gwr  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1     glass  */
     38       1.1     glass 
     39      1.24       gwr /*
     40      1.24       gwr  * This file contains only the machine-dependent parts of the
     41      1.24       gwr  * Sun3 SCSI driver.  (Autoconfig stuff and DMA functions.)
     42      1.24       gwr  * The machine-independent parts are in ncr5380sbc.c
     43      1.24       gwr  *
     44      1.24       gwr  * Supported hardware includes:
     45      1.24       gwr  * Sun SCSI-3 on OBIO (Sun3/50,Sun3/60)
     46      1.24       gwr  * Sun SCSI-3 on VME (Sun3/160,Sun3/260)
     47      1.24       gwr  *
     48      1.24       gwr  * Could be made to support the Sun3/E if someone wanted to.
     49      1.24       gwr  *
     50      1.24       gwr  * Note:  Both supported variants of the Sun SCSI-3 adapter have
     51      1.24       gwr  * some really unusual "features" for this driver to deal with,
     52      1.24       gwr  * generally related to the DMA engine.  The OBIO variant will
     53      1.24       gwr  * ignore any attempt to write the FIFO count register while the
     54      1.24       gwr  * SCSI bus is in DATA_IN or DATA_OUT phase.  This is dealt with
     55      1.24       gwr  * by setting the FIFO count early in COMMAND or MSG_IN phase.
     56      1.24       gwr  *
     57      1.24       gwr  * The VME variant has a bit to enable or disable the DMA engine,
     58      1.24       gwr  * but that bit also gates the interrupt line from the NCR5380!
     59      1.24       gwr  * Therefore, in order to get any interrupt from the 5380, (i.e.
     60      1.24       gwr  * for reselect) one must clear the DMA engine transfer count and
     61      1.24       gwr  * then enable DMA.  This has the further complication that you
     62      1.24       gwr  * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
     63      1.24       gwr  * we have to turn DMA back off before we even look at the 5380.
     64      1.24       gwr  *
     65      1.24       gwr  * What wonderfully whacky hardware this is!
     66      1.24       gwr  *
     67      1.24       gwr  * Credits, history:
     68      1.24       gwr  *
     69      1.24       gwr  * David Jones wrote the initial version of this module, which
     70      1.24       gwr  * included support for the VME adapter only. (no reselection).
     71      1.24       gwr  *
     72      1.24       gwr  * Gordon Ross added support for the OBIO adapter, and re-worked
     73      1.24       gwr  * both the VME and OBIO code to support disconnect/reselect.
     74      1.24       gwr  * (Required figuring out the hardware "features" noted above.)
     75      1.24       gwr  *
     76      1.24       gwr  * The autoconfiguration boilerplate came from Adam Glass.
     77      1.24       gwr  */
     78       1.1     glass 
     79       1.1     glass #include <sys/param.h>
     80       1.1     glass #include <sys/systm.h>
     81       1.1     glass #include <sys/errno.h>
     82      1.24       gwr #include <sys/kernel.h>
     83      1.24       gwr #include <sys/malloc.h>
     84      1.24       gwr #include <sys/device.h>
     85       1.1     glass #include <sys/buf.h>
     86       1.1     glass #include <sys/proc.h>
     87       1.1     glass #include <sys/user.h>
     88      1.24       gwr 
     89  1.35.2.1    bouyer #include <dev/scsipi/scsi_all.h>
     90  1.35.2.1    bouyer #include <dev/scsipi/scsipi_all.h>
     91  1.35.2.1    bouyer #include <dev/scsipi/scsipi_debug.h>
     92  1.35.2.1    bouyer #include <dev/scsipi/scsiconf.h>
     93       1.1     glass 
     94       1.1     glass #include <machine/autoconf.h>
     95      1.24       gwr #include <machine/dvma.h>
     96       1.1     glass 
     97      1.24       gwr #define DEBUG XXX
     98      1.24       gwr 
     99      1.24       gwr #include <dev/ic/ncr5380reg.h>
    100      1.24       gwr #include <dev/ic/ncr5380var.h>
    101       1.2       gwr 
    102      1.24       gwr #include "sireg.h"
    103      1.24       gwr #include "sivar.h"
    104      1.14       gwr 
    105      1.30       gwr /*
    106      1.30       gwr  * Transfers smaller than this are done using PIO
    107      1.30       gwr  * (on assumption they're not worth DMA overhead)
    108      1.30       gwr  */
    109      1.30       gwr #define	MIN_DMA_LEN 128
    110      1.30       gwr 
    111      1.24       gwr int si_debug = 0;
    112      1.14       gwr #ifdef	DEBUG
    113      1.24       gwr static int si_link_flags = 0 /* | SDEV_DB2 */ ;
    114      1.14       gwr #endif
    115       1.1     glass 
    116      1.24       gwr /* How long to wait for DMA before declaring an error. */
    117      1.24       gwr int si_dma_intr_timo = 500;	/* ticks (sec. X 100) */
    118       1.1     glass 
    119      1.24       gwr static void	si_minphys __P((struct buf *));
    120       1.1     glass 
    121  1.35.2.1    bouyer static struct scsipi_adapter	si_ops = {
    122       1.1     glass 	ncr5380_scsi_cmd,		/* scsi_cmd()		*/
    123      1.24       gwr 	si_minphys,			/* scsi_minphys()	*/
    124      1.12       gwr 	NULL,				/* open_target_lu()	*/
    125      1.12       gwr 	NULL,				/* close_target_lu()	*/
    126       1.1     glass };
    127       1.1     glass 
    128       1.1     glass /* This is copied from julian's bt driver */
    129       1.1     glass /* "so we have a default dev struct for our link struct." */
    130  1.35.2.1    bouyer static struct scsipi_device si_dev = {
    131       1.1     glass 	NULL,		/* Use default error handler.	    */
    132       1.2       gwr 	NULL,		/* Use default start handler.		*/
    133       1.2       gwr 	NULL,		/* Use default async handler.	    */
    134       1.1     glass 	NULL,		/* Use default "done" routine.	    */
    135       1.1     glass };
    136       1.1     glass 
    137      1.24       gwr /*
    138      1.24       gwr  * New-style autoconfig attachment. The cfattach
    139      1.24       gwr  * structures are in si_obio.c and si_vme.c
    140      1.24       gwr  */
    141       1.1     glass 
    142      1.24       gwr struct cfdriver si_cd = {
    143      1.24       gwr 	NULL, "si", DV_DULL
    144       1.2       gwr };
    145       1.1     glass 
    146       1.1     glass 
    147      1.24       gwr void
    148      1.24       gwr si_attach(sc)
    149      1.24       gwr 	struct si_softc *sc;
    150      1.24       gwr {
    151      1.24       gwr 	struct ncr5380_softc *ncr_sc = (void *)sc;
    152      1.24       gwr 	volatile struct si_regs *regs = sc->sc_regs;
    153      1.24       gwr 	int i;
    154      1.30       gwr 
    155      1.30       gwr 	/*
    156      1.30       gwr 	 * Support the "options" (config file flags).
    157      1.34       gwr 	 * Disconnect/reselect is a per-target mask.
    158      1.34       gwr 	 * Interrupts and DMA are per-controller.
    159      1.30       gwr 	 */
    160      1.34       gwr 	ncr_sc->sc_no_disconnect =
    161      1.34       gwr 		(sc->sc_options & SI_NO_DISCONNECT);
    162      1.34       gwr 	ncr_sc->sc_parity_disable =
    163      1.34       gwr 		(sc->sc_options & SI_NO_PARITY_CHK) >> 8;
    164      1.34       gwr 	if (sc->sc_options & SI_FORCE_POLLING)
    165      1.30       gwr 		ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
    166      1.34       gwr 
    167      1.30       gwr #if 1	/* XXX - Temporary */
    168      1.30       gwr 	/* XXX - In case we think DMA is completely broken... */
    169      1.34       gwr 	if (sc->sc_options & SI_DISABLE_DMA) {
    170      1.30       gwr 		/* Override this function pointer. */
    171      1.30       gwr 		ncr_sc->sc_dma_alloc = NULL;
    172      1.30       gwr 	}
    173      1.30       gwr #endif
    174      1.30       gwr 	ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
    175      1.16       gwr 
    176      1.16       gwr 	/*
    177      1.24       gwr 	 * Fill in the prototype scsi_link.
    178      1.16       gwr 	 */
    179  1.35.2.1    bouyer 	ncr_sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    180      1.24       gwr 	ncr_sc->sc_link.adapter_softc = sc;
    181  1.35.2.1    bouyer 	ncr_sc->sc_link.scsipi_scsi.adapter_target = 7;
    182      1.24       gwr 	ncr_sc->sc_link.adapter = &si_ops;
    183      1.24       gwr 	ncr_sc->sc_link.device = &si_dev;
    184  1.35.2.1    bouyer 	ncr_sc->sc_link.type = BUS_SCSI;
    185      1.13       gwr 
    186      1.24       gwr #ifdef	DEBUG
    187      1.24       gwr 	if (si_debug)
    188      1.32       gwr 		printf("si: Set TheSoftC=%p TheRegs=%p\n", sc, regs);
    189      1.24       gwr 	ncr_sc->sc_link.flags |= si_link_flags;
    190      1.24       gwr #endif
    191       1.1     glass 
    192      1.24       gwr 	/*
    193      1.24       gwr 	 * Initialize fields used by the MI code
    194      1.24       gwr 	 */
    195      1.24       gwr 	ncr_sc->sci_r0 = &regs->sci.sci_r0;
    196      1.24       gwr 	ncr_sc->sci_r1 = &regs->sci.sci_r1;
    197      1.24       gwr 	ncr_sc->sci_r2 = &regs->sci.sci_r2;
    198      1.24       gwr 	ncr_sc->sci_r3 = &regs->sci.sci_r3;
    199      1.24       gwr 	ncr_sc->sci_r4 = &regs->sci.sci_r4;
    200      1.24       gwr 	ncr_sc->sci_r5 = &regs->sci.sci_r5;
    201      1.24       gwr 	ncr_sc->sci_r6 = &regs->sci.sci_r6;
    202      1.24       gwr 	ncr_sc->sci_r7 = &regs->sci.sci_r7;
    203      1.12       gwr 
    204      1.24       gwr 	/*
    205      1.24       gwr 	 * Allocate DMA handles.
    206      1.24       gwr 	 */
    207      1.24       gwr 	i = SCI_OPENINGS * sizeof(struct si_dma_handle);
    208      1.24       gwr 	sc->sc_dma = (struct si_dma_handle *)
    209      1.24       gwr 		malloc(i, M_DEVBUF, M_WAITOK);
    210      1.24       gwr 	if (sc->sc_dma == NULL)
    211      1.24       gwr 		panic("si: dvma_malloc failed\n");
    212      1.24       gwr 	for (i = 0; i < SCI_OPENINGS; i++)
    213      1.24       gwr 		sc->sc_dma[i].dh_flags = 0;
    214      1.12       gwr 
    215      1.12       gwr 	/*
    216      1.24       gwr 	 *  Initialize si board itself.
    217      1.12       gwr 	 */
    218      1.24       gwr 	si_reset_adapter(ncr_sc);
    219      1.24       gwr 	ncr5380_init(ncr_sc);
    220      1.24       gwr 	ncr5380_reset_scsibus(ncr_sc);
    221      1.27       cgd 	config_found(&(ncr_sc->sc_dev), &(ncr_sc->sc_link), scsiprint);
    222       1.1     glass }
    223       1.1     glass 
    224      1.24       gwr static void
    225      1.24       gwr si_minphys(struct buf *bp)
    226       1.1     glass {
    227      1.24       gwr 	if (bp->b_bcount > MAX_DMA_LEN) {
    228      1.14       gwr #ifdef	DEBUG
    229      1.24       gwr 		if (si_debug) {
    230      1.29  christos 			printf("si_minphys len = 0x%x.\n", bp->b_bcount);
    231      1.24       gwr 			Debugger();
    232       1.1     glass 		}
    233      1.24       gwr #endif
    234      1.24       gwr 		bp->b_bcount = MAX_DMA_LEN;
    235       1.1     glass 	}
    236      1.24       gwr 	return (minphys(bp));
    237       1.1     glass }
    238       1.1     glass 
    239       1.1     glass 
    240      1.24       gwr #define CSR_WANT (SI_CSR_SBC_IP | SI_CSR_DMA_IP | \
    241      1.24       gwr 	SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR )
    242      1.24       gwr 
    243      1.24       gwr int
    244      1.24       gwr si_intr(void *arg)
    245       1.1     glass {
    246      1.24       gwr 	struct si_softc *sc = arg;
    247      1.24       gwr 	volatile struct si_regs *si = sc->sc_regs;
    248      1.24       gwr 	int dma_error, claimed;
    249      1.24       gwr 	u_short csr;
    250       1.1     glass 
    251      1.24       gwr 	claimed = 0;
    252      1.24       gwr 	dma_error = 0;
    253      1.24       gwr 
    254      1.24       gwr 	/* SBC interrupt? DMA interrupt? */
    255      1.24       gwr 	csr = si->si_csr;
    256      1.24       gwr 	NCR_TRACE("si_intr: csr=0x%x\n", csr);
    257      1.24       gwr 
    258      1.24       gwr 	if (csr & SI_CSR_DMA_CONFLICT) {
    259      1.24       gwr 		dma_error |= SI_CSR_DMA_CONFLICT;
    260      1.29  christos 		printf("si_intr: DMA conflict\n");
    261      1.24       gwr 	}
    262      1.24       gwr 	if (csr & SI_CSR_DMA_BUS_ERR) {
    263      1.24       gwr 		dma_error |= SI_CSR_DMA_BUS_ERR;
    264      1.29  christos 		printf("si_intr: DMA bus error\n");
    265      1.24       gwr 	}
    266      1.24       gwr 	if (dma_error) {
    267      1.24       gwr 		if (sc->ncr_sc.sc_state & NCR_DOINGDMA)
    268      1.24       gwr 			sc->ncr_sc.sc_state |= NCR_ABORTING;
    269      1.24       gwr 		/* Make sure we will call the main isr. */
    270      1.24       gwr 		csr |= SI_CSR_DMA_IP;
    271      1.24       gwr 	}
    272      1.24       gwr 
    273      1.24       gwr 	if (csr & (SI_CSR_SBC_IP | SI_CSR_DMA_IP)) {
    274      1.24       gwr 		claimed = ncr5380_intr(&sc->ncr_sc);
    275      1.24       gwr #ifdef	DEBUG
    276      1.24       gwr 		if (!claimed) {
    277      1.29  christos 			printf("si_intr: spurious from SBC\n");
    278      1.24       gwr 			if (si_debug & 4) {
    279      1.24       gwr 				Debugger();	/* XXX */
    280      1.24       gwr 			}
    281      1.24       gwr 		}
    282       1.2       gwr #endif
    283      1.35       gwr 		/* Yes, we DID cause this interrupt. */
    284      1.35       gwr 		claimed = 1;
    285      1.10       gwr 	}
    286      1.14       gwr 
    287      1.24       gwr 	return (claimed);
    288       1.1     glass }
    289       1.1     glass 
    290      1.14       gwr 
    291      1.24       gwr void
    292      1.24       gwr si_reset_adapter(struct ncr5380_softc *ncr_sc)
    293      1.14       gwr {
    294      1.24       gwr 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    295      1.17       gwr 	volatile struct si_regs *si = sc->sc_regs;
    296      1.14       gwr 
    297      1.14       gwr #ifdef	DEBUG
    298      1.14       gwr 	if (si_debug) {
    299      1.29  christos 		printf("si_reset_adapter\n");
    300      1.14       gwr 	}
    301       1.2       gwr #endif
    302      1.14       gwr 
    303      1.24       gwr 	/*
    304      1.24       gwr 	 * The SCSI3 controller has an 8K FIFO to buffer data between the
    305      1.24       gwr 	 * 5380 and the DMA.  Make sure it starts out empty.
    306      1.24       gwr 	 *
    307      1.24       gwr 	 * The reset bits in the CSR are active low.
    308      1.24       gwr 	 */
    309      1.14       gwr 	si->si_csr = 0;
    310      1.24       gwr 	delay(10);
    311      1.24       gwr 	si->si_csr = SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN;
    312      1.24       gwr 	delay(10);
    313      1.17       gwr 	si->fifo_count = 0;
    314      1.24       gwr 
    315      1.17       gwr 	if (sc->sc_adapter_type == BUS_VME16) {
    316      1.17       gwr 		si->dma_addrh = 0;
    317      1.17       gwr 		si->dma_addrl = 0;
    318      1.17       gwr 		si->dma_counth = 0;
    319      1.17       gwr 		si->dma_countl = 0;
    320      1.24       gwr 		si->si_iv_am = sc->sc_adapter_iv_am;
    321      1.24       gwr 		si->fifo_cnt_hi = 0;
    322      1.17       gwr 	}
    323      1.24       gwr 
    324      1.24       gwr 	SCI_CLR_INTR(ncr_sc);
    325       1.1     glass }
    326       1.1     glass 
    327       1.2       gwr 
    328      1.24       gwr /*****************************************************************
    329      1.24       gwr  * Common functions for DMA
    330      1.24       gwr  ****************************************************************/
    331       1.1     glass 
    332      1.24       gwr /*
    333      1.24       gwr  * Allocate a DMA handle and put it in sc->sc_dma.  Prepare
    334      1.24       gwr  * for DMA transfer.  On the Sun3, this means mapping the buffer
    335      1.24       gwr  * into DVMA space.  dvma_mapin() flushes the cache for us.
    336      1.24       gwr  */
    337      1.24       gwr void
    338      1.24       gwr si_dma_alloc(ncr_sc)
    339      1.24       gwr 	struct ncr5380_softc *ncr_sc;
    340      1.24       gwr {
    341      1.24       gwr 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    342      1.24       gwr 	struct sci_req *sr = ncr_sc->sc_current;
    343  1.35.2.1    bouyer 	struct scsipi_xfer *xs = sr->sr_xs;
    344      1.24       gwr 	struct si_dma_handle *dh;
    345      1.24       gwr 	int i, xlen;
    346      1.24       gwr 	u_long addr;
    347       1.1     glass 
    348      1.14       gwr #ifdef	DIAGNOSTIC
    349      1.24       gwr 	if (sr->sr_dma_hand != NULL)
    350      1.24       gwr 		panic("si_dma_alloc: already have DMA handle");
    351      1.14       gwr #endif
    352      1.14       gwr 
    353      1.24       gwr 	addr = (u_long) ncr_sc->sc_dataptr;
    354      1.24       gwr 	xlen = ncr_sc->sc_datalen;
    355      1.13       gwr 
    356      1.24       gwr 	/* If the DMA start addr is misaligned then do PIO */
    357      1.24       gwr 	if ((addr & 1) || (xlen & 1)) {
    358      1.29  christos 		printf("si_dma_alloc: misaligned.\n");
    359      1.24       gwr 		return;
    360       1.1     glass 	}
    361       1.1     glass 
    362      1.24       gwr 	/* Make sure our caller checked sc_min_dma_len. */
    363      1.24       gwr 	if (xlen < MIN_DMA_LEN)
    364      1.24       gwr 		panic("si_dma_alloc: xlen=0x%x\n", xlen);
    365      1.14       gwr 
    366      1.24       gwr 	/*
    367      1.24       gwr 	 * Never attempt single transfers of more than 63k, because
    368      1.24       gwr 	 * our count register may be only 16 bits (an OBIO adapter).
    369      1.24       gwr 	 * This should never happen since already bounded by minphys().
    370      1.24       gwr 	 * XXX - Should just segment these...
    371      1.24       gwr 	 */
    372      1.24       gwr 	if (xlen > MAX_DMA_LEN) {
    373      1.29  christos 		printf("si_dma_alloc: excessive xlen=0x%x\n", xlen);
    374      1.24       gwr 		Debugger();
    375      1.24       gwr 		ncr_sc->sc_datalen = xlen = MAX_DMA_LEN;
    376      1.24       gwr 	}
    377      1.24       gwr 
    378      1.24       gwr 	/* Find free DMA handle.  Guaranteed to find one since we have
    379      1.24       gwr 	   as many DMA handles as the driver has processes. */
    380      1.24       gwr 	for (i = 0; i < SCI_OPENINGS; i++) {
    381      1.24       gwr 		if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
    382      1.24       gwr 			goto found;
    383      1.24       gwr 	}
    384      1.24       gwr 	panic("si: no free DMA handles.");
    385      1.24       gwr found:
    386      1.24       gwr 
    387      1.24       gwr 	dh = &sc->sc_dma[i];
    388      1.24       gwr 	dh->dh_flags = SIDH_BUSY;
    389      1.24       gwr 	dh->dh_addr = (u_char*) addr;
    390      1.24       gwr 	dh->dh_maplen  = xlen;
    391      1.24       gwr 	dh->dh_dvma = 0;
    392      1.24       gwr 
    393      1.24       gwr 	/* Copy the "write" flag for convenience. */
    394      1.24       gwr 	if (xs->flags & SCSI_DATA_OUT)
    395      1.24       gwr 		dh->dh_flags |= SIDH_OUT;
    396      1.17       gwr 
    397      1.24       gwr #if 0
    398      1.24       gwr 	/*
    399      1.24       gwr 	 * Some machines might not need to remap B_PHYS buffers.
    400      1.24       gwr 	 * The sun3 does not map B_PHYS buffers into DVMA space,
    401      1.24       gwr 	 * (they are mapped into normal KV space) so on the sun3
    402      1.24       gwr 	 * we must always remap to a DVMA address here. Re-map is
    403      1.24       gwr 	 * cheap anyway, because it's done by segments, not pages.
    404      1.24       gwr 	 */
    405      1.24       gwr 	if (xs->bp && (xs->bp->b_flags & B_PHYS))
    406      1.24       gwr 		dh->dh_flags |= SIDH_PHYS;
    407      1.14       gwr #endif
    408      1.17       gwr 
    409      1.24       gwr 	dh->dh_dvma = (u_long) dvma_mapin((char *)addr, xlen);
    410      1.24       gwr 	if (!dh->dh_dvma) {
    411      1.24       gwr 		/* Can't remap segment */
    412      1.32       gwr 		printf("si_dma_alloc: can't remap %p/0x%x\n",
    413      1.24       gwr 			dh->dh_addr, dh->dh_maplen);
    414      1.24       gwr 		dh->dh_flags = 0;
    415      1.24       gwr 		return;
    416      1.14       gwr 	}
    417       1.1     glass 
    418      1.24       gwr 	/* success */
    419      1.24       gwr 	sr->sr_dma_hand = dh;
    420       1.1     glass 
    421      1.24       gwr 	return;
    422       1.1     glass }
    423       1.1     glass 
    424       1.1     glass 
    425      1.24       gwr void
    426      1.24       gwr si_dma_free(ncr_sc)
    427      1.24       gwr 	struct ncr5380_softc *ncr_sc;
    428       1.1     glass {
    429      1.24       gwr 	struct sci_req *sr = ncr_sc->sc_current;
    430      1.24       gwr 	struct si_dma_handle *dh = sr->sr_dma_hand;
    431       1.1     glass 
    432      1.24       gwr #ifdef	DIAGNOSTIC
    433      1.24       gwr 	if (dh == NULL)
    434      1.24       gwr 		panic("si_dma_free: no DMA handle");
    435      1.24       gwr #endif
    436       1.1     glass 
    437      1.24       gwr 	if (ncr_sc->sc_state & NCR_DOINGDMA)
    438      1.24       gwr 		panic("si_dma_free: free while in progress");
    439       1.1     glass 
    440      1.24       gwr 	if (dh->dh_flags & SIDH_BUSY) {
    441      1.24       gwr 		/* XXX - Should separate allocation and mapping. */
    442      1.24       gwr 		/* Give back the DVMA space. */
    443      1.24       gwr 		dvma_mapout((caddr_t)dh->dh_dvma, dh->dh_maplen);
    444      1.24       gwr 		dh->dh_dvma = 0;
    445      1.24       gwr 		dh->dh_flags = 0;
    446       1.1     glass 	}
    447      1.24       gwr 	sr->sr_dma_hand = NULL;
    448       1.1     glass }
    449       1.1     glass 
    450       1.1     glass 
    451      1.25       gwr #define	CSR_MASK (SI_CSR_SBC_IP | SI_CSR_DMA_IP | \
    452      1.25       gwr 		SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)
    453      1.25       gwr #define	POLL_TIMO	50000	/* X100 = 5 sec. */
    454      1.25       gwr 
    455      1.24       gwr /*
    456      1.24       gwr  * Poll (spin-wait) for DMA completion.
    457      1.24       gwr  * Called right after xx_dma_start(), and
    458      1.24       gwr  * xx_dma_stop() will be called next.
    459      1.24       gwr  * Same for either VME or OBIO.
    460      1.24       gwr  */
    461      1.24       gwr void
    462      1.24       gwr si_dma_poll(ncr_sc)
    463      1.24       gwr 	struct ncr5380_softc *ncr_sc;
    464      1.24       gwr {
    465      1.24       gwr 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    466      1.24       gwr 	struct sci_req *sr = ncr_sc->sc_current;
    467      1.24       gwr 	volatile struct si_regs *si = sc->sc_regs;
    468      1.25       gwr 	int tmo;
    469       1.1     glass 
    470      1.24       gwr 	/* Make sure DMA started successfully. */
    471      1.24       gwr 	if (ncr_sc->sc_state & NCR_ABORTING)
    472      1.24       gwr 		return;
    473       1.1     glass 
    474      1.25       gwr 	/*
    475      1.25       gwr 	 * XXX: The Sun driver waits for ~SI_CSR_DMA_ACTIVE here
    476      1.25       gwr 	 * XXX: (on obio) or even worse (on vme) a 10mS. delay!
    477      1.25       gwr 	 * XXX: I really doubt that is necessary...
    478      1.25       gwr 	 */
    479       1.1     glass 
    480      1.25       gwr 	/* Wait for any "dma complete" or error bits. */
    481      1.25       gwr 	tmo = POLL_TIMO;
    482      1.24       gwr 	for (;;) {
    483      1.25       gwr 		if (si->si_csr & CSR_MASK)
    484      1.24       gwr 			break;
    485      1.24       gwr 		if (--tmo <= 0) {
    486      1.29  christos 			printf("si: DMA timeout (while polling)\n");
    487      1.24       gwr 			/* Indicate timeout as MI code would. */
    488      1.24       gwr 			sr->sr_flags |= SR_OVERDUE;
    489      1.24       gwr 			break;
    490       1.1     glass 		}
    491      1.24       gwr 		delay(100);
    492       1.1     glass 	}
    493      1.25       gwr 	NCR_TRACE("si_dma_poll: waited %d\n",
    494      1.25       gwr 			  POLL_TIMO - tmo);
    495       1.1     glass 
    496      1.17       gwr #ifdef	DEBUG
    497      1.25       gwr 	if (si_debug & 2) {
    498      1.29  christos 		printf("si_dma_poll: done, csr=0x%x\n", si->si_csr);
    499      1.17       gwr 	}
    500      1.17       gwr #endif
    501       1.1     glass }
    502       1.1     glass 
    503