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