Home | History | Annotate | Line # | Download | only in qbus
uda.c revision 1.31
      1 /*	$NetBSD: uda.c,v 1.31 2000/01/21 23:39:59 thorpej Exp $	*/
      2 /*
      3  * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
      4  * Copyright (c) 1988 Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Chris Torek.
      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 University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)uda.c	7.32 (Berkeley) 2/13/91
     39  */
     40 
     41 /*
     42  * UDA50 disk device driver
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/kernel.h>
     47 #include <sys/systm.h>
     48 #include <sys/device.h>
     49 #include <sys/buf.h>
     50 #include <sys/malloc.h>
     51 
     52 #include <machine/bus.h>
     53 #include <machine/sid.h>
     54 
     55 #include <dev/qbus/ubavar.h>
     56 
     57 #include <dev/mscp/mscp.h>
     58 #include <dev/mscp/mscpreg.h>
     59 #include <dev/mscp/mscpvar.h>
     60 
     61 #include "ioconf.h"
     62 
     63 /*
     64  * Software status, per controller.
     65  */
     66 struct	uda_softc {
     67 	struct	device sc_dev;	/* Autoconfig info */
     68 	struct	uba_unit sc_unit; /* Struct common for UBA to communicate */
     69 	struct	buf_queue sc_bufq;	/* bufs awaiting for resources */
     70 	struct	mscp_pack *sc_uuda;	/* Unibus address of uda struct */
     71 	struct	mscp_pack sc_uda;	/* Struct for uda communication */
     72 	bus_dma_tag_t		sc_dmat;
     73 	bus_space_tag_t		sc_iot;
     74 	bus_space_handle_t	sc_iph;
     75 	bus_space_handle_t	sc_sah;
     76 	bus_dmamap_t		sc_cmap;/* Control structures */
     77 	struct	mscp *sc_mscp;		/* Keep pointer to active mscp */
     78 	struct	mscp_softc *sc_softc;	/* MSCP info (per mscpvar.h) */
     79 	int	sc_wticks;	/* watchdog timer ticks */
     80 	int	sc_inq;
     81 };
     82 
     83 static	int	udamatch __P((struct device *, struct cfdata *, void *));
     84 static	void	udaattach __P((struct device *, struct device *, void *));
     85 static	void	udareset __P((int));
     86 static	void	mtcreset __P((int));
     87 static	void	reset __P((struct uda_softc *));
     88 static	void	udaintr __P((int));
     89 static	void	mtcintr __P((int));
     90 static	void	intr __P((struct uda_softc *));
     91 int	udaready __P((struct uba_unit *));
     92 void	udactlrdone __P((struct device *));
     93 int	udaprint __P((void *, const char *));
     94 void	udasaerror __P((struct device *, int));
     95 void	udago __P((struct device *, struct mscp_xi *));
     96 
     97 struct	cfattach mtc_ca = {
     98 	sizeof(struct uda_softc), udamatch, udaattach
     99 };
    100 
    101 struct	cfattach uda_ca = {
    102 	sizeof(struct uda_softc), udamatch, udaattach
    103 };
    104 
    105 /*
    106  * More driver definitions, for generic MSCP code.
    107  */
    108 struct	mscp_ctlr uda_mscp_ctlr = {
    109 	udactlrdone,
    110 	udago,
    111 	udasaerror,
    112 };
    113 
    114 /*
    115  * Miscellaneous private variables.
    116  */
    117 static	int	ivec_no;
    118 
    119 int
    120 udaprint(aux, name)
    121 	void	*aux;
    122 	const char	*name;
    123 {
    124 	if (name)
    125 		printf("%s: mscpbus", name);
    126 	return UNCONF;
    127 }
    128 
    129 /*
    130  * Poke at a supposed UDA50 to see if it is there.
    131  */
    132 int
    133 udamatch(parent, cf, aux)
    134 	struct device *parent;
    135 	struct cfdata *cf;
    136 	void *aux;
    137 {
    138 	struct	uba_attach_args *ua = aux;
    139 	struct	mscp_softc mi;	/* Nice hack */
    140 	struct	uba_softc *ubasc;
    141 	int	tries;
    142 
    143 	/* Get an interrupt vector. */
    144 	ubasc = (void *)parent;
    145 	ivec_no = ubasc->uh_lastiv - 4;
    146 
    147 	mi.mi_iot = ua->ua_iot;
    148 	mi.mi_iph = ua->ua_ioh;
    149 	mi.mi_sah = ua->ua_ioh + 2;
    150 	mi.mi_swh = ua->ua_ioh + 2;
    151 
    152 	/*
    153 	 * Initialise the controller (partially).  The UDA50 programmer's
    154 	 * manual states that if initialisation fails, it should be retried
    155 	 * at least once, but after a second failure the port should be
    156 	 * considered `down'; it also mentions that the controller should
    157 	 * initialise within ten seconds.  Or so I hear; I have not seen
    158 	 * this manual myself.
    159 	 */
    160 	tries = 0;
    161 again:
    162 
    163 	bus_space_write_2(mi.mi_iot, mi.mi_iph, 0, 0); /* Start init */
    164 	if (mscp_waitstep(&mi, MP_STEP1, MP_STEP1) == 0)
    165 		return 0; /* Nothing here... */
    166 
    167 	bus_space_write_2(mi.mi_iot, mi.mi_sah, 0,
    168 	    MP_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | MP_IE | (ivec_no >> 2));
    169 
    170 	if (mscp_waitstep(&mi, MP_STEP2, MP_STEP2) == 0) {
    171 		printf("udaprobe: init step2 no change. sa=%x\n",
    172 		    bus_space_read_2(mi.mi_iot, mi.mi_sah, 0));
    173 		goto bad;
    174 	}
    175 
    176 	/* should have interrupted by now */
    177 	if (strcmp(cf->cf_driver->cd_name, mtc_cd.cd_name)) {
    178 		ua->ua_ivec = udaintr;
    179 		ua->ua_reset = udareset;
    180 	} else {
    181 		ua->ua_ivec = mtcintr;
    182 		ua->ua_reset = mtcreset;
    183 	}
    184 
    185 	return 1;
    186 bad:
    187 	if (++tries < 2)
    188 		goto again;
    189 	return 0;
    190 }
    191 
    192 void
    193 udaattach(parent, self, aux)
    194 	struct device *parent, *self;
    195 	void *aux;
    196 {
    197 	struct	uda_softc *sc = (void *)self;
    198 	struct	uba_attach_args *ua = aux;
    199 	struct	uba_softc *uh = (void *)parent;
    200 	struct	mscp_attach_args ma;
    201 	int	ctlr, error, rseg;
    202 	bus_dma_segment_t seg;
    203 
    204 	printf("\n");
    205 
    206 	uh->uh_lastiv -= 4;	/* remove dynamic interrupt vector */
    207 
    208 	sc->sc_iot = ua->ua_iot;
    209 	sc->sc_iph = ua->ua_ioh;
    210 	sc->sc_sah = ua->ua_ioh + 2;
    211 	sc->sc_dmat = ua->ua_dmat;
    212 	ctlr = sc->sc_dev.dv_unit;
    213 	BUFQ_INIT(&sc->sc_bufq);
    214 
    215 	/*
    216 	 * Fill in the uba_unit struct, so we can communicate with the uba.
    217 	 */
    218 	sc->sc_unit.uu_softc = sc;	/* Backpointer to softc */
    219 	sc->sc_unit.uu_ready = udaready;/* go routine called from adapter */
    220 	sc->sc_unit.uu_keepbdp = vax_cputype == VAX_750 ? 1 : 0;
    221 
    222 	/*
    223 	 * Map the communication area and command and
    224 	 * response packets into Unibus space.
    225 	 */
    226 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct mscp_pack),
    227 	    NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    228 		printf("Alloc ctrl area %d\n", error);
    229 		return;
    230 	}
    231 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    232 	    sizeof(struct mscp_pack), (caddr_t *) &sc->sc_uda,
    233 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    234 		printf("Map ctrl area %d\n", error);
    235 err:		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    236 		return;
    237 	}
    238 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct mscp_pack),
    239 	    1, sizeof(struct mscp_pack), 0, BUS_DMA_NOWAIT, &sc->sc_cmap))) {
    240 		printf("Create DMA map %d\n", error);
    241 err2:		bus_dmamem_unmap(sc->sc_dmat, (caddr_t)&sc->sc_uda,
    242 		    sizeof(struct mscp_pack));
    243 		goto err;
    244 	}
    245 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmap,
    246 	    &sc->sc_uda, sizeof(struct mscp_pack), 0, BUS_DMA_NOWAIT))) {
    247 		printf("Load ctrl map %d\n", error);
    248 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
    249 		goto err2;
    250 	}
    251 
    252 	bzero(&sc->sc_uda, sizeof (struct mscp_pack));
    253 
    254 	/*
    255 	 * The only thing that differ UDA's and Tape ctlr's is
    256 	 * their vcid. Beacuse there are no way to determine which
    257 	 * ctlr type it is, we check what is generated and later
    258 	 * set the correct vcid.
    259 	 */
    260 	ma.ma_type = (strcmp(self->dv_cfdata->cf_driver->cd_name,
    261 	    mtc_cd.cd_name) ? MSCPBUS_DISK : MSCPBUS_TAPE);
    262 
    263 	ma.ma_mc = &uda_mscp_ctlr;
    264 	ma.ma_type |= MSCPBUS_UDA;
    265 	ma.ma_uda = &sc->sc_uda;
    266 	ma.ma_softc = &sc->sc_softc;
    267 	ma.ma_iot = sc->sc_iot;
    268 	ma.ma_iph = sc->sc_iph;
    269 	ma.ma_sah = sc->sc_sah;
    270 	ma.ma_swh = sc->sc_sah;
    271 	ma.ma_dmat = sc->sc_dmat;
    272 	ma.ma_dmam = sc->sc_cmap;
    273 	ma.ma_ivec = ivec_no;
    274 	ma.ma_ctlrnr = (ua->ua_iaddr == 0172150 ? 0 : 1);	/* XXX */
    275 	ma.ma_adapnr = uh->uh_nr;
    276 	config_found(&sc->sc_dev, &ma, udaprint);
    277 }
    278 
    279 /*
    280  * Start a transfer if there are free resources available, otherwise
    281  * let it go in udaready, forget it for now.
    282  * Called from mscp routines.
    283  */
    284 void
    285 udago(usc, mxi)
    286 	struct device *usc;
    287 	struct mscp_xi *mxi;
    288 {
    289 	struct uda_softc *sc = (void *)usc;
    290 	struct uba_unit *uu;
    291 	struct buf *bp = mxi->mxi_bp;
    292 	int err;
    293 
    294 	/*
    295 	 * If we already have transfers queued, don't try to load
    296 	 * the map again.
    297 	 */
    298 	if (sc->sc_inq == 0) {
    299 		err = bus_dmamap_load(sc->sc_dmat, mxi->mxi_dmam,
    300 		    bp->b_un.b_addr,
    301 		    bp->b_bcount, bp->b_proc, BUS_DMA_NOWAIT);
    302 		if (err == 0) {
    303 			mscp_dgo(sc->sc_softc, mxi);
    304 			return;
    305 		}
    306 	}
    307 	uu = malloc(sizeof(struct uba_unit), M_DEVBUF, M_NOWAIT);
    308 	if (uu == 0)
    309 		panic("udago: no mem");
    310 	uu->uu_ready = udaready;
    311 	uu->uu_softc = sc;
    312 	uu->uu_ref = mxi;
    313 	uba_enqueue(uu);
    314 	sc->sc_inq++;
    315 }
    316 
    317 /*
    318  * Called if we have been blocked for resources, and resources
    319  * have been freed again. Return 1 if we could start all
    320  * transfers again, 0 if we still are waiting.
    321  * Called from uba resource free routines.
    322  */
    323 int
    324 udaready(uu)
    325 	struct uba_unit *uu;
    326 {
    327 	struct uda_softc *sc = uu->uu_softc;
    328 	struct mscp_xi *mxi = uu->uu_ref;
    329 	struct buf *bp = mxi->mxi_bp;
    330 	int err;
    331 
    332 	err = bus_dmamap_load(sc->sc_dmat, mxi->mxi_dmam, bp->b_un.b_addr,
    333 	    bp->b_bcount, bp->b_proc, BUS_DMA_NOWAIT);
    334 	if (err)
    335 		return 0;
    336 	mscp_dgo(sc->sc_softc, mxi);
    337 	sc->sc_inq--;
    338 	free(uu, M_DEVBUF);
    339 	return 1;
    340 }
    341 
    342 static struct saerr {
    343 	int	code;		/* error code (including UDA_ERR) */
    344 	char	*desc;		/* what it means: Efoo => foo error */
    345 } saerr[] = {
    346 	{ 0100001, "Eunibus packet read" },
    347 	{ 0100002, "Eunibus packet write" },
    348 	{ 0100003, "EUDA ROM and RAM parity" },
    349 	{ 0100004, "EUDA RAM parity" },
    350 	{ 0100005, "EUDA ROM parity" },
    351 	{ 0100006, "Eunibus ring read" },
    352 	{ 0100007, "Eunibus ring write" },
    353 	{ 0100010, " unibus interrupt master failure" },
    354 	{ 0100011, "Ehost access timeout" },
    355 	{ 0100012, " host exceeded command limit" },
    356 	{ 0100013, " unibus bus master failure" },
    357 	{ 0100014, " DM XFC fatal error" },
    358 	{ 0100015, " hardware timeout of instruction loop" },
    359 	{ 0100016, " invalid virtual circuit id" },
    360 	{ 0100017, "Eunibus interrupt write" },
    361 	{ 0104000, "Efatal sequence" },
    362 	{ 0104040, " D proc ALU" },
    363 	{ 0104041, "ED proc control ROM parity" },
    364 	{ 0105102, "ED proc w/no BD#2 or RAM parity" },
    365 	{ 0105105, "ED proc RAM buffer" },
    366 	{ 0105152, "ED proc SDI" },
    367 	{ 0105153, "ED proc write mode wrap serdes" },
    368 	{ 0105154, "ED proc read mode serdes, RSGEN & ECC" },
    369 	{ 0106040, "EU proc ALU" },
    370 	{ 0106041, "EU proc control reg" },
    371 	{ 0106042, " U proc DFAIL/cntl ROM parity/BD #1 test CNT" },
    372 	{ 0106047, " U proc const PROM err w/D proc running SDI test" },
    373 	{ 0106055, " unexpected trap" },
    374 	{ 0106071, "EU proc const PROM" },
    375 	{ 0106072, "EU proc control ROM parity" },
    376 	{ 0106200, "Estep 1 data" },
    377 	{ 0107103, "EU proc RAM parity" },
    378 	{ 0107107, "EU proc RAM buffer" },
    379 	{ 0107115, " test count wrong (BD 12)" },
    380 	{ 0112300, "Estep 2" },
    381 	{ 0122240, "ENPR" },
    382 	{ 0122300, "Estep 3" },
    383 	{ 0142300, "Estep 4" },
    384 	{ 0, " unknown error code" }
    385 };
    386 
    387 /*
    388  * If the error bit was set in the controller status register, gripe,
    389  * then (optionally) reset the controller and requeue pending transfers.
    390  */
    391 void
    392 udasaerror(usc, doreset)
    393 	struct device *usc;
    394 	int doreset;
    395 {
    396 	struct	uda_softc *sc = (void *)usc;
    397 	register int code = bus_space_read_2(sc->sc_iot, sc->sc_sah, 0);
    398 	register struct saerr *e;
    399 
    400 	if ((code & MP_ERR) == 0)
    401 		return;
    402 	for (e = saerr; e->code; e++)
    403 		if (e->code == code)
    404 			break;
    405 	printf("%s: controller error, sa=0%o (%s%s)\n",
    406 		sc->sc_dev.dv_xname, code, e->desc + 1,
    407 		*e->desc == 'E' ? " error" : "");
    408 #if 0 /* XXX we just avoid panic when autoconfig non-existent KFQSA devices */
    409 	if (doreset) {
    410 		mscp_requeue(sc->sc_softc);
    411 /*		(void) udainit(sc);	XXX */
    412 	}
    413 #endif
    414 }
    415 
    416 /*
    417  * Interrupt routine.  Depending on the state of the controller,
    418  * continue initialisation, or acknowledge command and response
    419  * interrupts, and process responses.
    420  */
    421 static void
    422 udaintr(ctlr)
    423 	int ctlr;
    424 {
    425 	intr(uda_cd.cd_devs[ctlr]);
    426 }
    427 
    428 static void
    429 mtcintr(ctlr)
    430 	int ctlr;
    431 {
    432 	intr(mtc_cd.cd_devs[ctlr]);
    433 }
    434 
    435 static void
    436 intr(sc)
    437 	struct uda_softc *sc;
    438 {
    439 	struct	uba_softc *uh;
    440 	struct mscp_pack *ud;
    441 
    442 	sc->sc_wticks = 0;	/* reset interrupt watchdog */
    443 
    444 	/* ctlr fatal error */
    445 	if (bus_space_read_2(sc->sc_iot, sc->sc_sah, 0) & MP_ERR) {
    446 		udasaerror(&sc->sc_dev, 1);
    447 		return;
    448 	}
    449 	ud = &sc->sc_uda;
    450 	/*
    451 	 * Handle buffer purge requests.
    452 	 * XXX - should be done in bus_dma_sync().
    453 	 */
    454 	uh = (void *)sc->sc_dev.dv_parent;
    455 	if (ud->mp_ca.ca_bdp) {
    456 		if (uh->uh_ubapurge)
    457 			(*uh->uh_ubapurge)(uh, ud->mp_ca.ca_bdp);
    458 		ud->mp_ca.ca_bdp = 0;
    459 		/* signal purge complete */
    460 		bus_space_write_2(sc->sc_iot, sc->sc_sah, 0, 0);
    461 	}
    462 
    463 	mscp_intr(sc->sc_softc);
    464 }
    465 
    466 /*
    467  * A Unibus reset has occurred on UBA uban.  Reinitialise the controller(s)
    468  * on that Unibus, and requeue outstanding I/O.
    469  */
    470 void
    471 udareset(ctlr)
    472 	int ctlr;
    473 {
    474 	reset(uda_cd.cd_devs[ctlr]);
    475 }
    476 
    477 void
    478 mtcreset(ctlr)
    479 	int ctlr;
    480 {
    481 	reset(mtc_cd.cd_devs[ctlr]);
    482 }
    483 
    484 static void
    485 reset(sc)
    486 	struct uda_softc *sc;
    487 {
    488 	printf(" %s", sc->sc_dev.dv_xname);
    489 
    490 	/*
    491 	 * Our BDP (if any) is gone; our command (if any) is
    492 	 * flushed; the device is no longer mapped; and the
    493 	 * UDA50 is not yet initialised.
    494 	 */
    495 	if (sc->sc_unit.uu_bdp) {
    496 		/* printf("<%d>", UBAI_BDP(sc->sc_unit.uu_bdp)); */
    497 		sc->sc_unit.uu_bdp = 0;
    498 	}
    499 
    500 	/* reset queues and requeue pending transfers */
    501 	mscp_requeue(sc->sc_softc);
    502 
    503 	/*
    504 	 * If it fails to initialise we will notice later and
    505 	 * try again (and again...).  Do not call udastart()
    506 	 * here; it will be done after the controller finishes
    507 	 * initialisation.
    508 	 */
    509 /* XXX	if (udainit(sc)) */
    510 		printf(" (hung)");
    511 }
    512 
    513 void
    514 udactlrdone(usc)
    515 	struct device *usc;
    516 {
    517 	struct uda_softc *sc = (void *)usc;
    518 
    519 	uba_done((struct uba_softc *)sc->sc_dev.dv_parent);
    520 }
    521