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