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