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