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