xref: /csrg-svn/sys/vax/uba/uda.c (revision 40045)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  *	@(#)uda.c	7.25 (Berkeley) 02/08/90
21  */
22 
23 /*
24  * UDA50/MSCP device driver
25  */
26 
27 #define	POLLSTATS
28 
29 /*
30  * TODO
31  *	write bad block forwarding code
32  */
33 
34 #include "ra.h"
35 
36 #if NUDA > 0
37 
38 /*
39  * CONFIGURATION OPTIONS.  The next three defines are tunable -- tune away!
40  *
41  * COMPAT_42 enables 4.2/4.3 compatibility (label mapping)
42  *
43  * NRSPL2 and NCMDL2 control the number of response and command
44  * packets respectively.  They may be any value from 0 to 7, though
45  * setting them higher than 5 is unlikely to be of any value.
46  * If you get warnings about your command ring being too small,
47  * try increasing the values by one.
48  *
49  * MAXUNIT controls the maximum unit number (number of drives per
50  * controller) we are prepared to handle.
51  *
52  * DEFAULT_BURST must be at least 1.
53  */
54 #define	COMPAT_42
55 
56 #define	NRSPL2	5		/* log2 number of response packets */
57 #define NCMDL2	5		/* log2 number of command packets */
58 #define	MAXUNIT	8		/* maximum allowed unit number */
59 #define	DEFAULT_BURST	4	/* default DMA burst size */
60 
61 #include "param.h"
62 #include "systm.h"
63 #include "buf.h"
64 #include "conf.h"
65 #include "dir.h"
66 #include "file.h"
67 #include "ioctl.h"
68 #include "user.h"
69 #include "map.h"
70 #include "vm.h"
71 #include "dkstat.h"
72 #include "cmap.h"
73 #include "disklabel.h"
74 #include "syslog.h"
75 #include "stat.h"
76 #include "tsleep.h"
77 
78 #include "machine/pte.h"
79 
80 #include "../vax/cpu.h"
81 #include "ubareg.h"
82 #include "ubavar.h"
83 
84 #define	NRSP	(1 << NRSPL2)
85 #define	NCMD	(1 << NCMDL2)
86 
87 #include "udareg.h"
88 #include "../vax/mscp.h"
89 #include "../vax/mscpvar.h"
90 #include "../vax/mtpr.h"
91 
92 /*
93  * UDA communications area and MSCP packet pools, per controller.
94  */
95 struct	uda {
96 	struct	udaca uda_ca;		/* communications area */
97 	struct	mscp uda_rsp[NRSP];	/* response packets */
98 	struct	mscp uda_cmd[NCMD];	/* command packets */
99 } uda[NUDA];
100 
101 /*
102  * Software status, per controller.
103  */
104 struct	uda_softc {
105 	struct	uda *sc_uda;	/* Unibus address of uda struct */
106 	short	sc_state;	/* UDA50 state; see below */
107 	short	sc_flags;	/* flags; see below */
108 	int	sc_micro;	/* microcode revision */
109 	int	sc_ivec;	/* interrupt vector address */
110 	short	sc_ipl;		/* interrupt priority, Q-bus */
111 	struct	mscp_info sc_mi;/* MSCP info (per mscpvar.h) */
112 #ifndef POLLSTATS
113 	int	sc_wticks;	/* watchdog timer ticks */
114 #else
115 	short	sc_wticks;
116 	short	sc_ncmd;
117 #endif
118 } uda_softc[NUDA];
119 
120 #ifdef POLLSTATS
121 struct udastats {
122 	int	ncmd;
123 	int	cmd[NCMD + 1];
124 } udastats = { NCMD + 1 };
125 #endif
126 
127 /*
128  * Controller states
129  */
130 #define	ST_IDLE		0	/* uninitialised */
131 #define	ST_STEP1	1	/* in `STEP 1' */
132 #define	ST_STEP2	2	/* in `STEP 2' */
133 #define	ST_STEP3	3	/* in `STEP 3' */
134 #define	ST_SETCHAR	4	/* in `Set Controller Characteristics' */
135 #define	ST_RUN		5	/* up and running */
136 
137 /*
138  * Flags
139  */
140 #define	SC_MAPPED	0x01	/* mapped in Unibus I/O space */
141 #define	SC_INSTART	0x02	/* inside udastart() */
142 #define	SC_GRIPED	0x04	/* griped about cmd ring too small */
143 #define	SC_INSLAVE	0x08	/* inside udaslave() */
144 #define	SC_DOWAKE	0x10	/* wakeup when ctlr init done */
145 #define	SC_STARTPOLL	0x20	/* need to initiate polling */
146 
147 /*
148  * Device to unit number and partition and back
149  */
150 #define	UNITSHIFT	3
151 #define	UNITMASK	7
152 #define	udaunit(dev)	(minor(dev) >> UNITSHIFT)
153 #define	udapart(dev)	(minor(dev) & UNITMASK)
154 #define	udaminor(u, p)	(((u) << UNITSHIFT) | (p))
155 
156 /*
157  * Drive status, per drive
158  */
159 struct ra_info {
160 	daddr_t	ra_dsize;	/* size in sectors */
161 /*	u_long	ra_type;	/* drive type */
162 	u_long	ra_mediaid;	/* media id */
163 	int	ra_state;	/* open/closed state */
164 	struct	ra_geom {	/* geometry information */
165 		u_short	rg_nsectors;	/* sectors/track */
166 		u_short	rg_ngroups;	/* track groups */
167 		u_short	rg_ngpc;	/* groups/cylinder */
168 		u_short	rg_ntracks;	/* ngroups*ngpc */
169 		u_short	rg_ncyl;	/* ra_dsize/ntracks/nsectors */
170 #ifdef notyet
171 		u_short	rg_rctsize;	/* size of rct */
172 		u_short	rg_rbns;	/* replacement blocks per track */
173 		u_short	rg_nrct;	/* number of rct copies */
174 #endif
175 	} ra_geom;
176 	int	ra_wlabel;	/* label sector is currently writable */
177 	u_long	ra_openpart;	/* partitions open */
178 	u_long	ra_bopenpart;	/* block partitions open */
179 	u_long	ra_copenpart;	/* character partitions open */
180 } ra_info[NRA];
181 
182 /*
183  * Software state, per drive
184  */
185 #define	CLOSED		0
186 #define	WANTOPEN	1
187 #define	RDLABEL		2
188 #define	OPEN		3
189 #define	OPENRAW		4
190 
191 /*
192  * Definition of the driver for autoconf.
193  */
194 int	udaprobe(), udaslave(), udaattach(), udadgo(), udaintr();
195 struct	uba_ctlr *udaminfo[NUDA];
196 struct	uba_device *udadinfo[NRA];
197 struct	disklabel udalabel[NRA];
198 
199 u_short	udastd[] = { 0772150, 0772550, 0777550, 0 };
200 struct	uba_driver udadriver =
201  { udaprobe, udaslave, udaattach, udadgo, udastd, "ra", udadinfo, "uda",
202    udaminfo };
203 
204 /*
205  * More driver definitions, for generic MSCP code.
206  */
207 int	udadgram(), udactlrdone(), udaunconf(), udaiodone();
208 int	udaonline(), udagotstatus(), udaioerror(), udareplace(), udabb();
209 
210 struct	buf udautab[NRA];	/* per drive transfer queue */
211 
212 struct	mscp_driver udamscpdriver =
213  { MAXUNIT, NRA, UNITSHIFT, udautab, udalabel, udadinfo,
214    udadgram, udactlrdone, udaunconf, udaiodone,
215    udaonline, udagotstatus, udareplace, udaioerror, udabb,
216    "uda", "ra" };
217 
218 /*
219  * Miscellaneous private variables.
220  */
221 char	udasr_bits[] = UDASR_BITS;
222 
223 struct	uba_device *udaip[NUDA][MAXUNIT];
224 				/* inverting pointers: ctlr & unit => Unibus
225 				   device pointer */
226 
227 int	udaburst[NUDA] = { 0 };	/* burst size, per UDA50, zero => default;
228 				   in data space so patchable via adb */
229 
230 struct	mscp udaslavereply;	/* get unit status response packet, set
231 				   for udaslave by udaunconf, via udaintr */
232 
233 static struct uba_ctlr *probeum;/* this is a hack---autoconf should pass ctlr
234 				   info to slave routine; instead, we remember
235 				   the last ctlr argument to probe */
236 
237 int	udawstart, udawatch();	/* watchdog timer */
238 
239 /*
240  * Externals
241  */
242 int	wakeup();
243 int	hz;
244 
245 /*
246  * Poke at a supposed UDA50 to see if it is there.
247  * This routine duplicates some of the code in udainit() only
248  * because autoconf has not set up the right information yet.
249  * We have to do everything `by hand'.
250  */
251 udaprobe(reg, ctlr, um)
252 	caddr_t reg;
253 	int ctlr;
254 	struct uba_ctlr *um;
255 {
256 	register int br, cvec;
257 	register struct uda_softc *sc;
258 	register struct udadevice *udaddr;
259 	register struct mscp_info *mi;
260 	int timeout, tries, s;
261 
262 #ifdef VAX750
263 	/*
264 	 * The UDA50 wants to share BDPs on 750s, but not on 780s or
265 	 * 8600s.  (730s have no BDPs anyway.)  Toward this end, we
266 	 * here set the `keep bdp' flag in the per-driver information
267 	 * if this is a 750.  (We just need to do it once, but it is
268 	 * easiest to do it now, for each UDA50.)
269 	 */
270 	if (cpu == VAX_750)
271 		udadriver.ud_keepbdp = 1;
272 #endif
273 
274 	probeum = um;			/* remember for udaslave() */
275 #ifdef lint
276 	br = 0; cvec = br; br = cvec; udaintr(0);
277 #endif
278 	/*
279 	 * Set up the controller-specific generic MSCP driver info.
280 	 * Note that this should really be done in the (nonexistent)
281 	 * controller attach routine.
282 	 */
283 	sc = &uda_softc[ctlr];
284 	mi = &sc->sc_mi;
285 	mi->mi_md = &udamscpdriver;
286 	mi->mi_ctlr = um->um_ctlr;
287 	mi->mi_tab = &um->um_tab;
288 	mi->mi_ip = udaip[ctlr];
289 	mi->mi_cmd.mri_size = NCMD;
290 	mi->mi_cmd.mri_desc = uda[ctlr].uda_ca.ca_cmddsc;
291 	mi->mi_cmd.mri_ring = uda[ctlr].uda_cmd;
292 	mi->mi_rsp.mri_size = NRSP;
293 	mi->mi_rsp.mri_desc = uda[ctlr].uda_ca.ca_rspdsc;
294 	mi->mi_rsp.mri_ring = uda[ctlr].uda_rsp;
295 	mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab;
296 
297 	/*
298 	 * More controller specific variables.  Again, this should
299 	 * be in the controller attach routine.
300 	 */
301 	if (udaburst[ctlr] == 0)
302 		udaburst[ctlr] = DEFAULT_BURST;
303 
304 	/*
305 	 * Get an interrupt vector.  Note that even if the controller
306 	 * does not respond, we keep the vector.  This is not a serious
307 	 * problem; but it would be easily fixed if we had a controller
308 	 * attach routine.  Sigh.
309 	 */
310 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
311 	udaddr = (struct udadevice *) reg;
312 
313 	/*
314 	 * Initialise the controller (partially).  The UDA50 programmer's
315 	 * manual states that if initialisation fails, it should be retried
316 	 * at least once, but after a second failure the port should be
317 	 * considered `down'; it also mentions that the controller should
318 	 * initialise within ten seconds.  Or so I hear; I have not seen
319 	 * this manual myself.
320 	 */
321 #ifdef QBA
322 	s = spl6();
323 #endif
324 	tries = 0;
325 again:
326 	udaddr->udaip = 0;		/* start initialisation */
327 	timeout = todr() + 1000;	/* timeout in 10 seconds */
328 	while ((udaddr->udasa & UDA_STEP1) == 0)
329 		if (todr() > timeout)
330 			goto bad;
331 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
332 		(sc->sc_ivec >> 2);
333 	while ((udaddr->udasa & UDA_STEP2) == 0)
334 		if (todr() > timeout)
335 			goto bad;
336 
337 	/* should have interrupted by now */
338 #ifdef QBA
339 	sc->sc_ipl = br = qbgetpri();
340 #endif
341 	return (sizeof (struct udadevice));
342 bad:
343 	if (++tries < 2)
344 		goto again;
345 	splx(s);
346 	return (0);
347 }
348 
349 /*
350  * Find a slave.  We allow wildcard slave numbers (something autoconf
351  * is not really prepared to deal with); and we need to know the
352  * controller number to talk to the UDA.  For the latter, we keep
353  * track of the last controller probed, since a controller probe
354  * immediately precedes all slave probes for that controller.  For the
355  * former, we simply put the unit number into ui->ui_slave after we
356  * have found one.
357  *
358  * Note that by the time udaslave is called, the interrupt vector
359  * for the UDA50 has been set up (so that udaunconf() will be called).
360  */
361 udaslave(ui, reg)
362 	register struct uba_device *ui;
363 	caddr_t reg;
364 {
365 	register struct uba_ctlr *um = probeum;
366 	register struct mscp *mp;
367 	register struct uda_softc *sc;
368 	int next = 0, timeout, tries, i;
369 
370 #ifdef lint
371 	i = 0; i = i;
372 #endif
373 	/*
374 	 * Make sure the controller is fully initialised, by waiting
375 	 * for it if necessary.
376 	 */
377 	sc = &uda_softc[um->um_ctlr];
378 	if (sc->sc_state == ST_RUN)
379 		goto findunit;
380 	tries = 0;
381 again:
382 	if (udainit(ui->ui_ctlr))
383 		return (0);
384 	timeout = todr() + 1000;		/* 10 seconds */
385 	while (todr() < timeout)
386 		if (sc->sc_state == ST_RUN)	/* made it */
387 			goto findunit;
388 	if (++tries < 2)
389 		goto again;
390 	printf("uda%d: controller hung\n", um->um_ctlr);
391 	return (0);
392 
393 	/*
394 	 * The controller is all set; go find the unit.  Grab an
395 	 * MSCP packet and send out a Get Unit Status command, with
396 	 * the `next unit' modifier if we are looking for a generic
397 	 * unit.  We set the `in slave' flag so that udaunconf()
398 	 * knows to copy the response to `udaslavereply'.
399 	 */
400 findunit:
401 	udaslavereply.mscp_opcode = 0;
402 	sc->sc_flags |= SC_INSLAVE;
403 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL)
404 		panic("udaslave");		/* `cannot happen' */
405 	mp->mscp_opcode = M_OP_GETUNITST;
406 	if (ui->ui_slave == '?') {
407 		mp->mscp_unit = next;
408 		mp->mscp_modifier = M_GUM_NEXTUNIT;
409 	} else {
410 		mp->mscp_unit = ui->ui_slave;
411 		mp->mscp_modifier = 0;
412 	}
413 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
414 	i = ((struct udadevice *) reg)->udaip;	/* initiate polling */
415 	mp = &udaslavereply;
416 	timeout = todr() + 1000;
417 	while (todr() < timeout)
418 		if (mp->mscp_opcode)
419 			goto gotit;
420 	printf("uda%d: no response to Get Unit Status request\n",
421 		um->um_ctlr);
422 	sc->sc_flags &= ~SC_INSLAVE;
423 	return (0);
424 
425 gotit:
426 	sc->sc_flags &= ~SC_INSLAVE;
427 
428 	/*
429 	 * Got a slave response.  If the unit is there, use it.
430 	 */
431 	switch (mp->mscp_status & M_ST_MASK) {
432 
433 	case M_ST_SUCCESS:	/* worked */
434 	case M_ST_AVAILABLE:	/* found another drive */
435 		break;		/* use it */
436 
437 	case M_ST_OFFLINE:
438 		/*
439 		 * Figure out why it is off line.  It may be because
440 		 * it is nonexistent, or because it is spun down, or
441 		 * for some other reason.
442 		 */
443 		switch (mp->mscp_status & ~M_ST_MASK) {
444 
445 		case M_OFFLINE_UNKNOWN:
446 			/*
447 			 * No such drive, and there are none with
448 			 * higher unit numbers either, if we are
449 			 * using M_GUM_NEXTUNIT.
450 			 */
451 			return (0);
452 
453 		case M_OFFLINE_UNMOUNTED:
454 			/*
455 			 * The drive is not spun up.  Use it anyway.
456 			 *
457 			 * N.B.: this seems to be a common occurrance
458 			 * after a power failure.  The first attempt
459 			 * to bring it on line seems to spin it up
460 			 * (and thus takes several minutes).  Perhaps
461 			 * we should note here that the on-line may
462 			 * take longer than usual.
463 			 */
464 			break;
465 
466 		default:
467 			/*
468 			 * In service, or something else equally unusable.
469 			 */
470 			printf("uda%d: unit %d off line: ", um->um_ctlr,
471 				mp->mscp_unit);
472 			mscp_printevent(mp);
473 			goto try_another;
474 		}
475 		break;
476 
477 	default:
478 		printf("uda%d: unable to get unit status: ", um->um_ctlr);
479 		mscp_printevent(mp);
480 		return (0);
481 	}
482 
483 	/*
484 	 * Does this ever happen?  What (if anything) does it mean?
485 	 */
486 	if (mp->mscp_unit < next) {
487 		printf("uda%d: unit %d, next %d\n",
488 			um->um_ctlr, mp->mscp_unit, next);
489 		return (0);
490 	}
491 
492 	if (mp->mscp_unit >= MAXUNIT) {
493 		printf("uda%d: cannot handle unit number %d (max is %d)\n",
494 			um->um_ctlr, mp->mscp_unit, MAXUNIT - 1);
495 		return (0);
496 	}
497 
498 	/*
499 	 * See if we already handle this drive.
500 	 * (Only likely if ui->ui_slave=='?'.)
501 	 */
502 	if (udaip[um->um_ctlr][mp->mscp_unit] != NULL) {
503 try_another:
504 		if (ui->ui_slave != '?')
505 			return (0);
506 		next = mp->mscp_unit + 1;
507 		goto findunit;
508 	}
509 
510 	/*
511 	 * Voila!
512 	 */
513 	uda_rasave(ui->ui_unit, mp, 0);
514 	ui->ui_flags = 0;	/* not on line, nor anything else */
515 	ui->ui_slave = mp->mscp_unit;
516 	return (1);
517 }
518 
519 /*
520  * Attach a found slave.  Make sure the watchdog timer is running.
521  * If this disk is being profiled, fill in the `wpms' value (used by
522  * what?).  Set up the inverting pointer, and attempt to bring the
523  * drive on line and read its label.
524  */
525 udaattach(ui)
526 	register struct uba_device *ui;
527 {
528 	register int unit = ui->ui_unit;
529 
530 	if (udawstart == 0) {
531 		timeout(udawatch, (caddr_t) 0, hz);
532 		udawstart++;
533 	}
534 
535 	/*
536 	 * Floppies cannot be brought on line unless there is
537 	 * a disk in the drive.  Since an ONLINE while cold
538 	 * takes ten seconds to fail, and (when notyet becomes now)
539 	 * no sensible person will swap to one, we just
540 	 * defer the ONLINE until someone tries to use the drive.
541 	 *
542 	 * THIS ASSUMES THAT DRIVE TYPES ?X? ARE FLOPPIES
543 	 */
544 	if (MSCP_MID_ECH(1, ra_info[unit].ra_mediaid) == 'X' - '@') {
545 		printf(": floppy");
546 		return;
547 	}
548 	if (ui->ui_dk >= 0)
549 		dk_wpms[ui->ui_dk] = (60 * 31 * 256);	/* approx */
550 	udaip[ui->ui_ctlr][ui->ui_slave] = ui;
551 
552 	if (uda_rainit(ui, 0))
553 		printf(": offline");
554 	else if (ra_info[unit].ra_state == OPEN) {
555 		printf(": %s, size = %d sectors",
556 		    udalabel[unit].d_typename, ra_info[unit].ra_dsize);
557 #ifdef notyet
558 		addswap(makedev(UDADEVNUM, udaminor(unit, 0)), &udalabel[unit]);
559 #endif
560 	}
561 }
562 
563 /*
564  * Initialise a UDA50.  Return true iff something goes wrong.
565  */
566 udainit(ctlr)
567 	int ctlr;
568 {
569 	register struct uda_softc *sc;
570 	register struct udadevice *udaddr;
571 	struct uba_ctlr *um;
572 	int timo, ubinfo;
573 
574 	sc = &uda_softc[ctlr];
575 	um = udaminfo[ctlr];
576 	if ((sc->sc_flags & SC_MAPPED) == 0) {
577 		/*
578 		 * Map the communication area and command and
579 		 * response packets into Unibus space.
580 		 */
581 		ubinfo = uballoc(um->um_ubanum, (caddr_t) &uda[ctlr],
582 			sizeof (struct uda), UBA_CANTWAIT);
583 		if (ubinfo == 0) {
584 			printf("uda%d: uballoc map failed\n", ctlr);
585 			return (-1);
586 		}
587 		sc->sc_uda = (struct uda *) UBAI_ADDR(ubinfo);
588 		sc->sc_flags |= SC_MAPPED;
589 	}
590 
591 	/*
592 	 * While we are thinking about it, reset the next command
593 	 * and response indicies.
594 	 */
595 	sc->sc_mi.mi_cmd.mri_next = 0;
596 	sc->sc_mi.mi_rsp.mri_next = 0;
597 
598 	/*
599 	 * Start up the hardware initialisation sequence.
600 	 */
601 #define	STEP0MASK	(UDA_ERR | UDA_STEP4 | UDA_STEP3 | UDA_STEP2 | \
602 			 UDA_STEP1 | UDA_NV)
603 
604 	sc->sc_state = ST_IDLE;	/* in case init fails */
605 	udaddr = (struct udadevice *)um->um_addr;
606 	udaddr->udaip = 0;
607 	timo = todr() + 1000;
608 	while ((udaddr->udasa & STEP0MASK) == 0) {
609 		if (todr() > timo) {
610 			printf("uda%d: timeout during init\n", ctlr);
611 			return (-1);
612 		}
613 	}
614 	if ((udaddr->udasa & STEP0MASK) != UDA_STEP1) {
615 		printf("uda%d: init failed, sa=%b\n", ctlr,
616 			udaddr->udasa, udasr_bits);
617 		udasaerror(um, 0);
618 		return (-1);
619 	}
620 
621 	/*
622 	 * Success!  Record new state, and start step 1 initialisation.
623 	 * The rest is done in the interrupt handler.
624 	 */
625 	sc->sc_state = ST_STEP1;
626 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
627 	    (sc->sc_ivec >> 2);
628 	return (0);
629 }
630 
631 /*
632  * Open a drive.
633  */
634 /*ARGSUSED*/
635 udaopen(dev, flag, fmt)
636 	dev_t dev;
637 	int flag, fmt;
638 {
639 	register int unit;
640 	register struct uba_device *ui;
641 	register struct uda_softc *sc;
642 	register struct disklabel *lp;
643 	register struct partition *pp;
644 	register struct ra_info *ra;
645 	int s, i, part, mask, error = 0;
646 	daddr_t start, end;
647 
648 	/*
649 	 * Make sure this is a reasonable open request.
650 	 */
651 	unit = udaunit(dev);
652 	if (unit >= NRA || (ui = udadinfo[unit]) == 0 || ui->ui_alive == 0)
653 		return (ENXIO);
654 
655 	/*
656 	 * Make sure the controller is running, by (re)initialising it if
657 	 * necessary.
658 	 */
659 	sc = &uda_softc[ui->ui_ctlr];
660 	s = spl5();
661 	if (sc->sc_state != ST_RUN) {
662 		if (sc->sc_state == ST_IDLE && udainit(ui->ui_ctlr)) {
663 			splx(s);
664 			return (EIO);
665 		}
666 		/*
667 		 * In case it does not come up, make sure we will be
668 		 * restarted in 10 seconds.  This corresponds to the
669 		 * 10 second timeouts in udaprobe() and udaslave().
670 		 */
671 		sc->sc_flags |= SC_DOWAKE;
672 		timeout(wakeup, (caddr_t) sc, 10 * hz);
673 		sleep((caddr_t) sc, PRIBIO);
674 		if (sc->sc_state != ST_RUN) {
675 			splx(s);
676 			printf("uda%d: controller hung\n", ui->ui_ctlr);
677 			return (EIO);
678 		}
679 		untimeout(wakeup, (caddr_t) sc);
680 	}
681 
682 	/*
683 	 * Wait for the state to settle
684 	 */
685 	ra = &ra_info[unit];
686 	while (ra->ra_state != OPEN && ra->ra_state != OPENRAW &&
687 	    ra->ra_state != CLOSED)
688 		tsleep((caddr_t)ra, PZERO + 1, SLP_UDA_OPN, 0);
689 
690 	/*
691 	 * If not on line, or we are not sure of the label, reinitialise
692 	 * the drive.
693 	 */
694 	if ((ui->ui_flags & UNIT_ONLINE) == 0 ||
695 	    (ra->ra_state != OPEN && ra->ra_state != OPENRAW))
696 		error = uda_rainit(ui, flag);
697 	splx(s);
698 	if (error)
699 		return (error);
700 
701 	part = udapart(dev);
702 	lp = &udalabel[unit];
703 	if (part >= lp->d_npartitions)
704 		return (ENXIO);
705 	/*
706 	 * Warn if a partition is opened that overlaps another
707 	 * already open, unless either is the `raw' partition
708 	 * (whole disk).
709 	 */
710 #define	RAWPART		2	/* 'c' partition */	/* XXX */
711 	mask = 1 << part;
712 	if ((ra->ra_openpart & mask) == 0 && part != RAWPART) {
713 		pp = &lp->d_partitions[part];
714 		start = pp->p_offset;
715 		end = pp->p_offset + pp->p_size;
716 		for (pp = lp->d_partitions, i = 0;
717 		     i < lp->d_npartitions; pp++, i++) {
718 			if (pp->p_offset + pp->p_size <= start ||
719 			    pp->p_offset >= end || i == RAWPART)
720 				continue;
721 			if (ra->ra_openpart & (1 << i))
722 				log(LOG_WARNING,
723 				    "ra%d%c: overlaps open partition (%c)\n",
724 				    unit, part + 'a', i + 'a');
725 		}
726 	}
727 	switch (fmt) {
728 	case S_IFCHR:
729 		ra->ra_copenpart |= mask;
730 		break;
731 	case S_IFBLK:
732 		ra->ra_bopenpart |= mask;
733 		break;
734 	}
735 	ra->ra_openpart |= mask;
736 	return (0);
737 }
738 
739 /* ARGSUSED */
740 udaclose(dev, flags, fmt)
741 	dev_t dev;
742 	int flags, fmt;
743 {
744 	register int unit = udaunit(dev);
745 	register struct ra_info *ra = &ra_info[unit];
746 	int s, mask = (1 << udapart(dev));
747 
748 	switch (fmt) {
749 	case S_IFCHR:
750 		ra->ra_copenpart &= ~mask;
751 		break;
752 	case S_IFBLK:
753 		ra->ra_bopenpart &= ~mask;
754 		break;
755 	}
756 	ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
757 
758 	/*
759 	 * Should wait for I/O to complete on this partition even if
760 	 * others are open, but wait for work on blkflush().
761 	 */
762 	if (ra->ra_openpart == 0) {
763 		s = spl5();
764 		while (udautab[unit].b_actf)
765 			sleep((caddr_t)&udautab[unit], PZERO - 1);
766 		splx(s);
767 		ra->ra_state = CLOSED;
768 		ra->ra_wlabel = 0;
769 	}
770 	return (0);
771 }
772 
773 /*
774  * Initialise a drive.  If it is not already, bring it on line,
775  * and set a timeout on it in case it fails to respond.
776  * When on line, read in the pack label.
777  */
778 uda_rainit(ui, flags)
779 	register struct uba_device *ui;
780 	int flags;
781 {
782 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
783 	register struct disklabel *lp;
784 	register struct mscp *mp;
785 	register int unit = ui->ui_unit;
786 	register struct ra_info *ra;
787 	char *msg, *readdisklabel();
788 	int s, i, udastrategy();
789 	extern int cold;
790 
791 	ra = &ra_info[unit];
792 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
793 		mp = mscp_getcp(&sc->sc_mi, MSCP_WAIT);
794 		mp->mscp_opcode = M_OP_ONLINE;
795 		mp->mscp_unit = ui->ui_slave;
796 		mp->mscp_cmdref = (long)&ui->ui_flags;
797 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
798 		ra->ra_state = WANTOPEN;
799 		if (!cold)
800 			s = spl5();
801 		i = ((struct udadevice *)ui->ui_addr)->udaip;
802 
803 		if (cold) {
804 			i = todr() + 1000;
805 			while ((ui->ui_flags & UNIT_ONLINE) == 0)
806 				if (todr() > i)
807 					break;
808 		} else {
809 			timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz);
810 			sleep((caddr_t)&ui->ui_flags, PSWP + 1);
811 			splx(s);
812 			untimeout(wakeup, (caddr_t)&ui->ui_flags);
813 		}
814 		if (ra->ra_state != OPENRAW) {
815 			ra->ra_state = CLOSED;
816 			wakeup((caddr_t)ra);
817 			return (EIO);
818 		}
819 	}
820 
821 	lp = &udalabel[unit];
822 	lp->d_secsize = DEV_BSIZE;
823 	lp->d_secperunit = ra->ra_dsize;
824 
825 	if (flags & O_NDELAY)
826 		return (0);
827 	ra->ra_state = RDLABEL;
828 	/*
829 	 * Set up default sizes until we have the label, or longer
830 	 * if there is none.  Set secpercyl, as readdisklabel wants
831 	 * to compute b_cylin (although we do not need it), and set
832 	 * nsectors in case diskerr is called.
833 	 */
834 	lp->d_secpercyl = 1;
835 	lp->d_npartitions = 1;
836 	lp->d_secsize = 512;
837 	lp->d_secperunit = ra->ra_dsize;
838 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
839 	lp->d_partitions[0].p_size = lp->d_secperunit;
840 	lp->d_partitions[0].p_offset = 0;
841 
842 	/*
843 	 * Read pack label.
844 	 */
845 	if ((msg = readdisklabel(udaminor(unit, 0), udastrategy, lp)) != NULL) {
846 		if (cold)
847 			printf(": %s", msg);
848 		else
849 			log(LOG_ERR, "ra%d: %s", unit, msg);
850 #ifdef COMPAT_42
851 		if (udamaptype(unit, lp))
852 			ra->ra_state = OPEN;
853 		else
854 			ra->ra_state = OPENRAW;
855 #else
856 		ra->ra_state = OPENRAW;
857 		uda_makefakelabel(ra, lp);
858 #endif
859 	} else
860 		ra->ra_state = OPEN;
861 	wakeup((caddr_t)ra);
862 	return (0);
863 }
864 
865 /*
866  * Copy the geometry information for the given ra from a
867  * GET UNIT STATUS response.  If check, see if it changed.
868  */
869 uda_rasave(unit, mp, check)
870 	int unit;
871 	register struct mscp *mp;
872 	int check;
873 {
874 	register struct ra_info *ra = &ra_info[unit];
875 
876 	if (check && ra->ra_mediaid != mp->mscp_guse.guse_mediaid) {
877 		printf("ra%d: changed types! was %d now %d\n", unit,
878 			ra->ra_mediaid, mp->mscp_guse.guse_mediaid);
879 		ra->ra_state = CLOSED;	/* ??? */
880 	}
881 	/* ra->ra_type = mp->mscp_guse.guse_drivetype; */
882 	ra->ra_mediaid = mp->mscp_guse.guse_mediaid;
883 	ra->ra_geom.rg_nsectors = mp->mscp_guse.guse_nspt;
884 	ra->ra_geom.rg_ngroups = mp->mscp_guse.guse_group;
885 	ra->ra_geom.rg_ngpc = mp->mscp_guse.guse_ngpc;
886 	ra->ra_geom.rg_ntracks = ra->ra_geom.rg_ngroups * ra->ra_geom.rg_ngpc;
887 	/* ra_geom.rg_ncyl cannot be computed until we have ra_dsize */
888 #ifdef notyet
889 	ra->ra_geom.rg_rctsize = mp->mscp_guse.guse_rctsize;
890 	ra->ra_geom.rg_rbns = mp->mscp_guse.guse_nrpt;
891 	ra->ra_geom.rg_nrct = mp->mscp_guse.guse_nrct;
892 #endif
893 }
894 
895 /*
896  * Queue a transfer request, and if possible, hand it to the controller.
897  *
898  * This routine is broken into two so that the internal version
899  * udastrat1() can be called by the (nonexistent, as yet) bad block
900  * revectoring routine.
901  */
902 udastrategy(bp)
903 	register struct buf *bp;
904 {
905 	register int unit;
906 	register struct uba_device *ui;
907 	register struct ra_info *ra;
908 	struct partition *pp;
909 	int p;
910 	daddr_t sz, maxsz;
911 
912 	/*
913 	 * Make sure this is a reasonable drive to use.
914 	 */
915 	if ((unit = udaunit(bp->b_dev)) >= NRA ||
916 	    (ui = udadinfo[unit]) == NULL || ui->ui_alive == 0 ||
917 	    (ra = &ra_info[unit])->ra_state == CLOSED) {
918 		bp->b_error = ENXIO;
919 		goto bad;
920 	}
921 
922 	/*
923 	 * If drive is open `raw' or reading label, let it at it.
924 	 */
925 	if (ra->ra_state < OPEN) {
926 		udastrat1(bp);
927 		return;
928 	}
929 	p = udapart(bp->b_dev);
930 	if ((ra->ra_openpart & (1 << p)) == 0) {
931 		bp->b_error = ENODEV;
932 		goto bad;
933 	}
934 
935 	/*
936 	 * Determine the size of the transfer, and make sure it is
937 	 * within the boundaries of the partition.
938 	 */
939 	pp = &udalabel[unit].d_partitions[p];
940 	maxsz = pp->p_size;
941 	if (pp->p_offset + pp->p_size > ra->ra_dsize)
942 		maxsz = ra->ra_dsize - pp->p_offset;
943 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
944 	if (bp->b_blkno + pp->p_offset <= LABELSECTOR &&
945 #if LABELSECTOR != 0
946 	    bp->b_blkno + pp->p_offset + sz > LABELSECTOR &&
947 #endif
948 	    (bp->b_flags & B_READ) == 0 && ra->ra_wlabel == 0) {
949 		bp->b_error = EROFS;
950 		goto bad;
951 	}
952 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
953 		/* if exactly at end of disk, return an EOF */
954 		if (bp->b_blkno == maxsz) {
955 			bp->b_resid = bp->b_bcount;
956 			biodone(bp);
957 			return;
958 		}
959 		/* or truncate if part of it fits */
960 		sz = maxsz - bp->b_blkno;
961 		if (sz <= 0) {
962 			bp->b_error = EINVAL;	/* or hang it up */
963 			goto bad;
964 		}
965 		bp->b_bcount = sz << DEV_BSHIFT;
966 	}
967 	udastrat1(bp);
968 	return;
969 bad:
970 	bp->b_flags |= B_ERROR;
971 	biodone(bp);
972 }
973 
974 /*
975  * Work routine for udastrategy.
976  */
977 udastrat1(bp)
978 	register struct buf *bp;
979 {
980 	register int unit = udaunit(bp->b_dev);
981 	register struct uba_ctlr *um;
982 	register struct buf *dp;
983 	struct uba_device *ui;
984 	int s = spl5();
985 
986 	/*
987 	 * Append the buffer to the drive queue, and if it is not
988 	 * already there, the drive to the controller queue.  (However,
989 	 * if the drive queue is marked to be requeued, we must be
990 	 * awaiting an on line or get unit status command; in this
991 	 * case, leave it off the controller queue.)
992 	 */
993 	um = (ui = udadinfo[unit])->ui_mi;
994 	dp = &udautab[unit];
995 	APPEND(bp, dp, av_forw);
996 	if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) {
997 		APPEND(dp, &um->um_tab, b_forw);
998 		dp->b_active++;
999 	}
1000 
1001 	/*
1002 	 * Start activity on the controller.  Note that unlike other
1003 	 * Unibus drivers, we must always do this, not just when the
1004 	 * controller is not active.
1005 	 */
1006 	udastart(um);
1007 	splx(s);
1008 }
1009 
1010 /*
1011  * Start up whatever transfers we can find.
1012  * Note that udastart() must be called at spl5().
1013  */
1014 udastart(um)
1015 	register struct uba_ctlr *um;
1016 {
1017 	register struct uda_softc *sc = &uda_softc[um->um_ctlr];
1018 	register struct buf *bp, *dp;
1019 	register struct mscp *mp;
1020 	struct uba_device *ui;
1021 	struct udadevice *udaddr;
1022 	struct partition *pp;
1023 	int i, sz;
1024 
1025 #ifdef lint
1026 	i = 0; i = i;
1027 #endif
1028 	/*
1029 	 * If it is not running, try (again and again...) to initialise
1030 	 * it.  If it is currently initialising just ignore it for now.
1031 	 */
1032 	if (sc->sc_state != ST_RUN) {
1033 		if (sc->sc_state == ST_IDLE && udainit(um->um_ctlr))
1034 			printf("uda%d: still hung\n", um->um_ctlr);
1035 		return;
1036 	}
1037 
1038 	/*
1039 	 * If um_cmd is nonzero, this controller is on the Unibus
1040 	 * resource wait queue.  It will not help to try more requests;
1041 	 * instead, when the Unibus unblocks and calls udadgo(), we
1042 	 * will call udastart() again.
1043 	 */
1044 	if (um->um_cmd)
1045 		return;
1046 
1047 	sc->sc_flags |= SC_INSTART;
1048 	udaddr = (struct udadevice *) um->um_addr;
1049 
1050 loop:
1051 	/*
1052 	 * Service the drive at the head of the queue.  It may not
1053 	 * need anything, in which case it might be shutting down
1054 	 * in udaclose().
1055 	 */
1056 	if ((dp = um->um_tab.b_actf) == NULL)
1057 		goto out;
1058 	if ((bp = dp->b_actf) == NULL) {
1059 		dp->b_active = 0;
1060 		um->um_tab.b_actf = dp->b_forw;
1061 		if (ra_info[dp - udautab].ra_openpart == 0)
1062 			wakeup((caddr_t)dp); /* finish close protocol */
1063 		goto loop;
1064 	}
1065 
1066 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
1067 		udasaerror(um, 1);
1068 		goto out;
1069 	}
1070 
1071 	/*
1072 	 * Get an MSCP packet, then figure out what to do.  If
1073 	 * we cannot get a command packet, the command ring may
1074 	 * be too small:  We should have at least as many command
1075 	 * packets as credits, for best performance.
1076 	 */
1077 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) {
1078 		if (sc->sc_mi.mi_credits > MSCP_MINCREDITS &&
1079 		    (sc->sc_flags & SC_GRIPED) == 0) {
1080 			log(LOG_NOTICE, "uda%d: command ring too small\n",
1081 				um->um_ctlr);
1082 			sc->sc_flags |= SC_GRIPED;/* complain only once */
1083 		}
1084 		goto out;
1085 	}
1086 
1087 	/*
1088 	 * Bring the drive on line if it is not already.  Get its status
1089 	 * if we do not already have it.  Otherwise just start the transfer.
1090 	 */
1091 	ui = udadinfo[udaunit(bp->b_dev)];
1092 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
1093 		mp->mscp_opcode = M_OP_ONLINE;
1094 		goto common;
1095 	}
1096 	if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) {
1097 		mp->mscp_opcode = M_OP_GETUNITST;
1098 common:
1099 if (ui->ui_flags & UNIT_REQUEUE) panic("udastart");
1100 		/*
1101 		 * Take the drive off the controller queue.  When the
1102 		 * command finishes, make sure the drive is requeued.
1103 		 */
1104 		um->um_tab.b_actf = dp->b_forw;
1105 		dp->b_active = 0;
1106 		ui->ui_flags |= UNIT_REQUEUE;
1107 		mp->mscp_unit = ui->ui_slave;
1108 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
1109 		sc->sc_flags |= SC_STARTPOLL;
1110 #ifdef POLLSTATS
1111 		sc->sc_ncmd++;
1112 #endif
1113 		goto loop;
1114 	}
1115 
1116 	pp = &udalabel[ui->ui_unit].d_partitions[udapart(bp->b_dev)];
1117 	mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
1118 	mp->mscp_unit = ui->ui_slave;
1119 	mp->mscp_seq.seq_lbn = bp->b_blkno + pp->p_offset;
1120 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
1121 	mp->mscp_seq.seq_bytecount = bp->b_blkno + sz > pp->p_size ?
1122 		(pp->p_size - bp->b_blkno) >> DEV_BSHIFT : bp->b_bcount;
1123 	/* mscp_cmdref is filled in by mscp_go() */
1124 
1125 	/*
1126 	 * Drop the packet pointer into the `command' field so udadgo()
1127 	 * can tell what to start.  If ubago returns 1, we can do another
1128 	 * transfer.  If not, um_cmd will still point at mp, so we will
1129 	 * know that we are waiting for resources.
1130 	 */
1131 	um->um_cmd = (int)mp;
1132 	if (ubago(ui))
1133 		goto loop;
1134 
1135 	/*
1136 	 * All done, or blocked in ubago().  If we managed to
1137 	 * issue some commands, start up the beast.
1138 	 */
1139 out:
1140 	if (sc->sc_flags & SC_STARTPOLL) {
1141 #ifdef POLLSTATS
1142 		udastats.cmd[sc->sc_ncmd]++;
1143 		sc->sc_ncmd = 0;
1144 #endif
1145 		i = ((struct udadevice *)um->um_addr)->udaip;
1146 	}
1147 	sc->sc_flags &= ~(SC_INSTART | SC_STARTPOLL);
1148 }
1149 
1150 /*
1151  * Start a transfer.
1152  *
1153  * If we are not called from within udastart(), we must have been
1154  * blocked, so call udastart to do more requests (if any).  If
1155  * this calls us again immediately we will not recurse, because
1156  * that time we will be in udastart().  Clever....
1157  */
1158 udadgo(um)
1159 	register struct uba_ctlr *um;
1160 {
1161 	struct uda_softc *sc = &uda_softc[um->um_ctlr];
1162 	struct mscp *mp = (struct mscp *)um->um_cmd;
1163 
1164 	um->um_tab.b_active++;	/* another transfer going */
1165 
1166 	/*
1167 	 * Fill in the MSCP packet and move the buffer to the
1168 	 * I/O wait queue.  Mark the controller as no longer on
1169 	 * the resource queue, and remember to initiate polling.
1170 	 */
1171 	mp->mscp_seq.seq_buffer = UBAI_ADDR(um->um_ubinfo) |
1172 		(UBAI_BDP(um->um_ubinfo) << 24);
1173 	mscp_go(&sc->sc_mi, mp, um->um_ubinfo);
1174 	um->um_cmd = 0;
1175 	um->um_ubinfo = 0;	/* tyke it awye */
1176 	sc->sc_flags |= SC_STARTPOLL;
1177 #ifdef POLLSTATS
1178 	sc->sc_ncmd++;
1179 #endif
1180 	if ((sc->sc_flags & SC_INSTART) == 0)
1181 		udastart(um);
1182 }
1183 
1184 udaiodone(mi, bp, info)
1185 	register struct mscp_info *mi;
1186 	struct buf *bp;
1187 	int info;
1188 {
1189 	register struct uba_ctlr *um = udaminfo[mi->mi_ctlr];
1190 
1191 	um->um_ubinfo = info;
1192 	ubadone(um);
1193 	biodone(bp);
1194 	if (um->um_bdp && mi->mi_wtab.av_forw == &mi->mi_wtab)
1195 		ubarelse(um->um_ubanum, &um->um_bdp);
1196 	um->um_tab.b_active--;	/* another transfer done */
1197 }
1198 
1199 static struct saerr {
1200 	int	code;		/* error code (including UDA_ERR) */
1201 	char	*desc;		/* what it means: Efoo => foo error */
1202 } saerr[] = {
1203 	{ 0100001, "Eunibus packet read" },
1204 	{ 0100002, "Eunibus packet write" },
1205 	{ 0100003, "EUDA ROM and RAM parity" },
1206 	{ 0100004, "EUDA RAM parity" },
1207 	{ 0100005, "EUDA ROM parity" },
1208 	{ 0100006, "Eunibus ring read" },
1209 	{ 0100007, "Eunibus ring write" },
1210 	{ 0100010, " unibus interrupt master failure" },
1211 	{ 0100011, "Ehost access timeout" },
1212 	{ 0100012, " host exceeded command limit" },
1213 	{ 0100013, " unibus bus master failure" },
1214 	{ 0100014, " DM XFC fatal error" },
1215 	{ 0100015, " hardware timeout of instruction loop" },
1216 	{ 0100016, " invalid virtual circuit id" },
1217 	{ 0100017, "Eunibus interrupt write" },
1218 	{ 0104000, "Efatal sequence" },
1219 	{ 0104040, " D proc ALU" },
1220 	{ 0104041, "ED proc control ROM parity" },
1221 	{ 0105102, "ED proc w/no BD#2 or RAM parity" },
1222 	{ 0105105, "ED proc RAM buffer" },
1223 	{ 0105152, "ED proc SDI" },
1224 	{ 0105153, "ED proc write mode wrap serdes" },
1225 	{ 0105154, "ED proc read mode serdes, RSGEN & ECC" },
1226 	{ 0106040, "EU proc ALU" },
1227 	{ 0106041, "EU proc control reg" },
1228 	{ 0106042, " U proc DFAIL/cntl ROM parity/BD #1 test CNT" },
1229 	{ 0106047, " U proc const PROM err w/D proc running SDI test" },
1230 	{ 0106055, " unexpected trap" },
1231 	{ 0106071, "EU proc const PROM" },
1232 	{ 0106072, "EU proc control ROM parity" },
1233 	{ 0106200, "Estep 1 data" },
1234 	{ 0107103, "EU proc RAM parity" },
1235 	{ 0107107, "EU proc RAM buffer" },
1236 	{ 0107115, " test count wrong (BD 12)" },
1237 	{ 0112300, "Estep 2" },
1238 	{ 0122240, "ENPR" },
1239 	{ 0122300, "Estep 3" },
1240 	{ 0142300, "Estep 4" },
1241 	{ 0, " unknown error code" }
1242 };
1243 
1244 /*
1245  * If the error bit was set in the controller status register, gripe,
1246  * then (optionally) reset the controller and requeue pending transfers.
1247  */
1248 udasaerror(um, doreset)
1249 	register struct uba_ctlr *um;
1250 	int doreset;
1251 {
1252 	register int code = ((struct udadevice *)um->um_addr)->udasa;
1253 	register struct saerr *e;
1254 
1255 	if ((code & UDA_ERR) == 0)
1256 		return;
1257 	for (e = saerr; e->code; e++)
1258 		if (e->code == code)
1259 			break;
1260 	printf("uda%d: controller error, sa=0%o (%s%s)\n",
1261 		um->um_ctlr, code, e->desc + 1,
1262 		*e->desc == 'E' ? " error" : "");
1263 	if (doreset) {
1264 		mscp_requeue(&uda_softc[um->um_ctlr].sc_mi);
1265 		(void) udainit(um->um_ctlr);
1266 	}
1267 }
1268 
1269 /*
1270  * Interrupt routine.  Depending on the state of the controller,
1271  * continue initialisation, or acknowledge command and response
1272  * interrupts, and process responses.
1273  */
1274 udaintr(ctlr)
1275 	int ctlr;
1276 {
1277 	register struct uba_ctlr *um = udaminfo[ctlr];
1278 	register struct uda_softc *sc = &uda_softc[ctlr];
1279 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
1280 	register struct uda *ud;
1281 	register struct mscp *mp;
1282 	register int i;
1283 
1284 #ifdef QBA
1285 	splx(sc->sc_ipl);	/* Qbus interrupt protocol is odd */
1286 #endif
1287 	sc->sc_wticks = 0;	/* reset interrupt watchdog */
1288 
1289 	/*
1290 	 * Combinations during steps 1, 2, and 3: STEPnMASK
1291 	 * corresponds to which bits should be tested;
1292 	 * STEPnGOOD corresponds to the pattern that should
1293 	 * appear after the interrupt from STEPn initialisation.
1294 	 * All steps test the bits in ALLSTEPS.
1295 	 */
1296 #define	ALLSTEPS	(UDA_ERR|UDA_STEP4|UDA_STEP3|UDA_STEP2|UDA_STEP1)
1297 
1298 #define	STEP1MASK	(ALLSTEPS | UDA_IE | UDA_NCNRMASK)
1299 #define	STEP1GOOD	(UDA_STEP2 | UDA_IE | (NCMDL2 << 3) | NRSPL2)
1300 
1301 #define	STEP2MASK	(ALLSTEPS | UDA_IE | UDA_IVECMASK)
1302 #define	STEP2GOOD	(UDA_STEP3 | UDA_IE | (sc->sc_ivec >> 2))
1303 
1304 #define	STEP3MASK	ALLSTEPS
1305 #define	STEP3GOOD	UDA_STEP4
1306 
1307 	switch (sc->sc_state) {
1308 
1309 	case ST_IDLE:
1310 		/*
1311 		 * Ignore unsolicited interrupts.
1312 		 */
1313 		log(LOG_WARNING, "uda%d: stray intr\n", ctlr);
1314 		return;
1315 
1316 	case ST_STEP1:
1317 		/*
1318 		 * Begin step two initialisation.
1319 		 */
1320 		if ((udaddr->udasa & STEP1MASK) != STEP1GOOD) {
1321 			i = 1;
1322 initfailed:
1323 			printf("uda%d: init step %d failed, sa=%b\n",
1324 				ctlr, i, udaddr->udasa, udasr_bits);
1325 			udasaerror(um, 0);
1326 			sc->sc_state = ST_IDLE;
1327 			if (sc->sc_flags & SC_DOWAKE) {
1328 				sc->sc_flags &= ~SC_DOWAKE;
1329 				wakeup((caddr_t)sc);
1330 			}
1331 			return;
1332 		}
1333 		udaddr->udasa = (int)&sc->sc_uda->uda_ca.ca_rspdsc[0] |
1334 			(cpu == VAX_780 || cpu == VAX_8600 ? UDA_PI : 0);
1335 		sc->sc_state = ST_STEP2;
1336 		return;
1337 
1338 	case ST_STEP2:
1339 		/*
1340 		 * Begin step 3 initialisation.
1341 		 */
1342 		if ((udaddr->udasa & STEP2MASK) != STEP2GOOD) {
1343 			i = 2;
1344 			goto initfailed;
1345 		}
1346 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_rspdsc[0]) >> 16;
1347 		sc->sc_state = ST_STEP3;
1348 		return;
1349 
1350 	case ST_STEP3:
1351 		/*
1352 		 * Set controller characteristics (finish initialisation).
1353 		 */
1354 		if ((udaddr->udasa & STEP3MASK) != STEP3GOOD) {
1355 			i = 3;
1356 			goto initfailed;
1357 		}
1358 		i = udaddr->udasa & 0xff;
1359 		if (i != sc->sc_micro) {
1360 			sc->sc_micro = i;
1361 			printf("uda%d: version %d model %d\n",
1362 				ctlr, i & 0xf, i >> 4);
1363 		}
1364 
1365 		/*
1366 		 * Present the burst size, then remove it.  Why this
1367 		 * should be done this way, I have no idea.
1368 		 *
1369 		 * Note that this assumes udaburst[ctlr] > 0.
1370 		 */
1371 		udaddr->udasa = UDA_GO | (udaburst[ctlr] - 1) << 2;
1372 		udaddr->udasa = UDA_GO;
1373 		printf("uda%d: DMA burst size set to %d\n",
1374 			ctlr, udaburst[ctlr]);
1375 
1376 		udainitds(ctlr);	/* initialise data structures */
1377 
1378 		/*
1379 		 * Before we can get a command packet, we need some
1380 		 * credits.  Fake some up to keep mscp_getcp() happy,
1381 		 * get a packet, and cancel all credits (the right
1382 		 * number should come back in the response to the
1383 		 * SCC packet).
1384 		 */
1385 		sc->sc_mi.mi_credits = MSCP_MINCREDITS + 1;
1386 		mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT);
1387 		if (mp == NULL)	/* `cannot happen' */
1388 			panic("udaintr");
1389 		sc->sc_mi.mi_credits = 0;
1390 		mp->mscp_opcode = M_OP_SETCTLRC;
1391 		mp->mscp_unit = 0;
1392 		mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC |
1393 			M_CF_THIS;
1394 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
1395 		i = udaddr->udaip;
1396 		sc->sc_state = ST_SETCHAR;
1397 		return;
1398 
1399 	case ST_SETCHAR:
1400 	case ST_RUN:
1401 		/*
1402 		 * Handle Set Ctlr Characteristics responses and operational
1403 		 * responses (via mscp_dorsp).
1404 		 */
1405 		break;
1406 
1407 	default:
1408 		printf("uda%d: driver bug, state %d\n", ctlr, sc->sc_state);
1409 		panic("udastate");
1410 	}
1411 
1412 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
1413 		udasaerror(um, 1);
1414 		return;
1415 	}
1416 
1417 	ud = &uda[ctlr];
1418 
1419 	/*
1420 	 * Handle buffer purge requests.
1421 	 */
1422 	if (ud->uda_ca.ca_bdp) {
1423 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
1424 		ud->uda_ca.ca_bdp = 0;
1425 		udaddr->udasa = 0;	/* signal purge complete */
1426 	}
1427 
1428 	/*
1429 	 * Check for response and command ring transitions.
1430 	 */
1431 	if (ud->uda_ca.ca_rspint) {
1432 		ud->uda_ca.ca_rspint = 0;
1433 		mscp_dorsp(&sc->sc_mi);
1434 	}
1435 	if (ud->uda_ca.ca_cmdint) {
1436 		ud->uda_ca.ca_cmdint = 0;
1437 		MSCP_DOCMD(&sc->sc_mi);
1438 	}
1439 	udastart(um);
1440 }
1441 
1442 /*
1443  * Initialise the various data structures that control the UDA50.
1444  */
1445 udainitds(ctlr)
1446 	int ctlr;
1447 {
1448 	register struct uda *ud = &uda[ctlr];
1449 	register struct uda *uud = uda_softc[ctlr].sc_uda;
1450 	register struct mscp *mp;
1451 	register int i;
1452 
1453 	for (i = 0, mp = ud->uda_rsp; i < NRSP; i++, mp++) {
1454 		ud->uda_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
1455 			(long)&uud->uda_rsp[i].mscp_cmdref;
1456 		mp->mscp_addr = &ud->uda_ca.ca_rspdsc[i];
1457 		mp->mscp_msglen = MSCP_MSGLEN;
1458 	}
1459 	for (i = 0, mp = ud->uda_cmd; i < NCMD; i++, mp++) {
1460 		ud->uda_ca.ca_cmddsc[i] = MSCP_INT |
1461 			(long)&uud->uda_cmd[i].mscp_cmdref;
1462 		mp->mscp_addr = &ud->uda_ca.ca_cmddsc[i];
1463 		mp->mscp_msglen = MSCP_MSGLEN;
1464 	}
1465 }
1466 
1467 /*
1468  * Handle an error datagram.
1469  */
1470 udadgram(mi, mp)
1471 	struct mscp_info *mi;
1472 	struct mscp *mp;
1473 {
1474 
1475 	mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp);
1476 	/*
1477 	 * SDI status information bytes 10 and 11 are the microprocessor
1478 	 * error code and front panel code respectively.  These vary per
1479 	 * drive type and are printed purely for field service information.
1480 	 */
1481 	if (mp->mscp_format == M_FM_SDI)
1482 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
1483 			mp->mscp_erd.erd_sdistat[10],
1484 			mp->mscp_erd.erd_sdistat[11]);
1485 }
1486 
1487 /*
1488  * The Set Controller Characteristics command finished.
1489  * Record the new state of the controller.
1490  */
1491 udactlrdone(mi, mp)
1492 	register struct mscp_info *mi;
1493 	struct mscp *mp;
1494 {
1495 	register struct uda_softc *sc = &uda_softc[mi->mi_ctlr];
1496 
1497 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
1498 		sc->sc_state = ST_RUN;
1499 	else {
1500 		printf("uda%d: SETCTLRC failed: ",
1501 			mi->mi_ctlr, mp->mscp_status);
1502 		mscp_printevent(mp);
1503 		sc->sc_state = ST_IDLE;
1504 	}
1505 	if (sc->sc_flags & SC_DOWAKE) {
1506 		sc->sc_flags &= ~SC_DOWAKE;
1507 		wakeup((caddr_t)sc);
1508 	}
1509 }
1510 
1511 /*
1512  * Received a response from an as-yet unconfigured drive.  Configure it
1513  * in, if possible.
1514  */
1515 udaunconf(mi, mp)
1516 	struct mscp_info *mi;
1517 	register struct mscp *mp;
1518 {
1519 
1520 	/*
1521 	 * If it is a slave response, copy it to udaslavereply for
1522 	 * udaslave() to look at.
1523 	 */
1524 	if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) &&
1525 	    (uda_softc[mi->mi_ctlr].sc_flags & SC_INSLAVE) != 0) {
1526 		udaslavereply = *mp;
1527 		return (MSCP_DONE);
1528 	}
1529 
1530 	/*
1531 	 * Otherwise, it had better be an available attention response.
1532 	 */
1533 	if (mp->mscp_opcode != M_OP_AVAILATTN)
1534 		return (MSCP_FAILED);
1535 
1536 	/* do what autoconf does */
1537 	return (MSCP_FAILED);	/* not yet, arwhite, not yet */
1538 }
1539 
1540 /*
1541  * A drive came on line.  Check its type and size.  Return DONE if
1542  * we think the drive is truly on line.  In any case, awaken anyone
1543  * sleeping on the drive on-line-ness.
1544  */
1545 udaonline(ui, mp)
1546 	register struct uba_device *ui;
1547 	struct mscp *mp;
1548 {
1549 	register struct ra_info *ra = &ra_info[ui->ui_unit];
1550 
1551 	wakeup((caddr_t)&ui->ui_flags);
1552 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1553 		if (!cold)
1554 			printf("uda%d: ra%d", ui->ui_ctlr, ui->ui_unit);
1555 		printf(": attempt to bring on line failed: ");
1556 		mscp_printevent(mp);
1557 		ra->ra_state = CLOSED;
1558 		return (MSCP_FAILED);
1559 	}
1560 
1561 	ra->ra_state = OPENRAW;
1562 	ra->ra_dsize = (daddr_t)mp->mscp_onle.onle_unitsize;
1563 	if (!cold)
1564 		printf("ra%d: uda%d, unit %d, size = %d sectors\n", ui->ui_unit,
1565 		    ui->ui_ctlr, mp->mscp_unit, ra->ra_dsize);
1566 	/* can now compute ncyl */
1567 	ra->ra_geom.rg_ncyl = ra->ra_dsize / ra->ra_geom.rg_ntracks /
1568 		ra->ra_geom.rg_nsectors;
1569 	return (MSCP_DONE);
1570 }
1571 
1572 /*
1573  * We got some (configured) unit's status.  Return DONE if it succeeded.
1574  */
1575 udagotstatus(ui, mp)
1576 	register struct uba_device *ui;
1577 	register struct mscp *mp;
1578 {
1579 
1580 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1581 		printf("uda%d: attempt to get status for ra%d failed: ",
1582 			ui->ui_ctlr, ui->ui_unit);
1583 		mscp_printevent(mp);
1584 		return (MSCP_FAILED);
1585 	}
1586 	/* record for (future) bad block forwarding and whatever else */
1587 	uda_rasave(ui->ui_unit, mp, 1);
1588 	return (MSCP_DONE);
1589 }
1590 
1591 /*
1592  * A transfer failed.  We get a chance to fix or restart it.
1593  * Need to write the bad block forwaring code first....
1594  */
1595 /*ARGSUSED*/
1596 udaioerror(ui, mp, bp)
1597 	register struct uba_device *ui;
1598 	register struct mscp *mp;
1599 	struct buf *bp;
1600 {
1601 
1602 	if (mp->mscp_flags & M_EF_BBLKR) {
1603 		/*
1604 		 * A bad block report.  Eventually we will
1605 		 * restart this transfer, but for now, just
1606 		 * log it and give up.
1607 		 */
1608 		log(LOG_ERR, "ra%d: bad block report: %d%s\n",
1609 			ui->ui_unit, mp->mscp_seq.seq_lbn,
1610 			mp->mscp_flags & M_EF_BBLKU ? " + others" : "");
1611 	} else {
1612 		/*
1613 		 * What the heck IS a `serious exception' anyway?
1614 		 * IT SURE WOULD BE NICE IF DEC SOLD DOCUMENTATION
1615 		 * FOR THEIR OWN CONTROLLERS.
1616 		 */
1617 		if (mp->mscp_flags & M_EF_SEREX)
1618 			log(LOG_ERR, "ra%d: serious exception reported\n",
1619 				ui->ui_unit);
1620 	}
1621 	return (MSCP_FAILED);
1622 }
1623 
1624 /*
1625  * A replace operation finished.
1626  */
1627 /*ARGSUSED*/
1628 udareplace(ui, mp)
1629 	struct uba_device *ui;
1630 	struct mscp *mp;
1631 {
1632 
1633 	panic("udareplace");
1634 }
1635 
1636 /*
1637  * A bad block related operation finished.
1638  */
1639 /*ARGSUSED*/
1640 udabb(ui, mp, bp)
1641 	struct uba_device *ui;
1642 	struct mscp *mp;
1643 	struct buf *bp;
1644 {
1645 
1646 	panic("udabb");
1647 }
1648 
1649 
1650 /*
1651  * I/O controls.
1652  */
1653 udaioctl(dev, cmd, data, flag)
1654 	dev_t dev;
1655 	int cmd;
1656 	caddr_t data;
1657 	int flag;
1658 {
1659 	register int unit = udaunit(dev);
1660 	register struct disklabel *lp;
1661 	register struct ra_info *ra = &ra_info[unit];
1662 	int error = 0;
1663 
1664 	lp = &udalabel[unit];
1665 
1666 	switch (cmd) {
1667 
1668 	case DIOCGDINFO:
1669 		*(struct disklabel *)data = *lp;
1670 		break;
1671 
1672 	case DIOCGPART:
1673 		((struct partinfo *)data)->disklab = lp;
1674 		((struct partinfo *)data)->part =
1675 		    &lp->d_partitions[udapart(dev)];
1676 		break;
1677 
1678 	case DIOCSDINFO:
1679 		if ((flag & FWRITE) == 0)
1680 			error = EBADF;
1681 		else
1682 			error = setdisklabel(lp, (struct disklabel *)data,
1683 			    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart);
1684 		break;
1685 
1686 	case DIOCWLABEL:
1687 		if ((flag & FWRITE) == 0)
1688 			error = EBADF;
1689 		else
1690 			ra->ra_wlabel = *(int *)data;
1691 		break;
1692 
1693 	case DIOCWDINFO:
1694 		if ((flag & FWRITE) == 0)
1695 			error = EBADF;
1696 		else if ((error = setdisklabel(lp, (struct disklabel *)data,
1697 		    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart)) == 0) {
1698 			int wlab;
1699 
1700 			ra->ra_state = OPEN;
1701 			/* simulate opening partition 0 so write succeeds */
1702 			ra->ra_openpart |= (1 << 0);		/* XXX */
1703 			wlab = ra->ra_wlabel;
1704 			ra->ra_wlabel = 1;
1705 			error = writedisklabel(dev, udastrategy, lp);
1706 			ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
1707 			ra->ra_wlabel = wlab;
1708 		}
1709 		break;
1710 
1711 #ifdef notyet
1712 	case UDAIOCREPLACE:
1713 		/*
1714 		 * Initiate bad block replacement for the given LBN.
1715 		 * (Should we allow modifiers?)
1716 		 */
1717 		error = EOPNOTSUPP;
1718 		break;
1719 
1720 	case UDAIOCGMICRO:
1721 		/*
1722 		 * Return the microcode revision for the UDA50 running
1723 		 * this drive.
1724 		 */
1725 		*(int *)data = uda_softc[uddinfo[unit]->ui_ctlr].sc_micro;
1726 		break;
1727 #endif
1728 
1729 	default:
1730 		error = ENOTTY;
1731 		break;
1732 	}
1733 	return (error);
1734 }
1735 
1736 /*
1737  * A Unibus reset has occurred on UBA uban.  Reinitialise the controller(s)
1738  * on that Unibus, and requeue outstanding I/O.
1739  */
1740 udareset(uban)
1741 	int uban;
1742 {
1743 	register struct uba_ctlr *um;
1744 	register struct uda_softc *sc;
1745 	register int ctlr;
1746 
1747 	for (ctlr = 0, sc = uda_softc; ctlr < NUDA; ctlr++, sc++) {
1748 		if ((um = udaminfo[ctlr]) == NULL || um->um_ubanum != uban ||
1749 		    um->um_alive == 0)
1750 			continue;
1751 		printf(" uda%d", ctlr);
1752 
1753 		/*
1754 		 * Our BDP (if any) is gone; our command (if any) is
1755 		 * flushed; the device is no longer mapped; and the
1756 		 * UDA50 is not yet initialised.
1757 		 */
1758 		if (um->um_bdp) {
1759 			printf("<%d>", UBAI_BDP(um->um_bdp));
1760 			um->um_bdp = 0;
1761 		}
1762 		um->um_ubinfo = 0;
1763 		um->um_cmd = 0;
1764 		sc->sc_flags &= ~SC_MAPPED;
1765 		sc->sc_state = ST_IDLE;
1766 
1767 		/* reset queues and requeue pending transfers */
1768 		mscp_requeue(&sc->sc_mi);
1769 
1770 		/*
1771 		 * If it fails to initialise we will notice later and
1772 		 * try again (and again...).  Do not call udastart()
1773 		 * here; it will be done after the controller finishes
1774 		 * initialisation.
1775 		 */
1776 		if (udainit(ctlr))
1777 			printf(" (hung)");
1778 	}
1779 }
1780 
1781 /*
1782  * Watchdog timer:  If the controller is active, and no interrupts
1783  * have occurred for 30 seconds, assume it has gone away.
1784  */
1785 udawatch()
1786 {
1787 	register int i;
1788 	register struct uba_ctlr *um;
1789 	register struct uda_softc *sc;
1790 
1791 	timeout(udawatch, (caddr_t) 0, hz);	/* every second */
1792 	for (i = 0, sc = uda_softc; i < NUDA; i++, sc++) {
1793 		if ((um = udaminfo[i]) == 0 || !um->um_alive)
1794 			continue;
1795 		if (sc->sc_state == ST_IDLE)
1796 			continue;
1797 		if (sc->sc_state == ST_RUN && !um->um_tab.b_active)
1798 			sc->sc_wticks = 0;
1799 		else if (++sc->sc_wticks >= 30) {
1800 			sc->sc_wticks = 0;
1801 			printf("uda%d: lost interrupt\n", i);
1802 			ubareset(um->um_ubanum);
1803 		}
1804 	}
1805 }
1806 
1807 /*
1808  * Do a panic dump.  We set up the controller for one command packet
1809  * and one response packet, for which we use `struct uda1'.
1810  */
1811 struct	uda1 {
1812 	struct	uda1ca uda1_ca;	/* communications area */
1813 	struct	mscp uda1_rsp;	/* response packet */
1814 	struct	mscp uda1_cmd;	/* command packet */
1815 } uda1;
1816 
1817 #define	DBSIZE	32		/* dump 16K at a time */
1818 
1819 udadump(dev)
1820 	dev_t dev;
1821 {
1822 	struct udadevice *udaddr;
1823 	struct uda1 *ud_ubaddr;
1824 	char *start;
1825 	int num, blk, unit, maxsz, blkoff, reg;
1826 	struct partition *pp;
1827 	register struct uba_regs *uba;
1828 	register struct uba_device *ui;
1829 	register struct uda1 *ud;
1830 	register struct pte *io;
1831 	register int i;
1832 
1833 	/*
1834 	 * Make sure the device is a reasonable place on which to dump.
1835 	 */
1836 	unit = udaunit(dev);
1837 	if (unit >= NRA)
1838 		return (ENXIO);
1839 #define	phys(cast, addr)	((cast) ((int)addr & 0x7fffffff))
1840 	ui = phys(struct uba_device *, udadinfo[unit]);
1841 	if (ui == NULL || ui->ui_alive == 0)
1842 		return (ENXIO);
1843 
1844 	/*
1845 	 * Find and initialise the UBA; get the physical address of the
1846 	 * device registers, and of communications area and command and
1847 	 * response packet.
1848 	 */
1849 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
1850 	ubainit(uba);
1851 	udaddr = (struct udadevice *)ui->ui_physaddr;
1852 	ud = phys(struct uda1 *, &uda1);
1853 
1854 	/*
1855 	 * Map the ca+packets into Unibus I/O space so the UDA50 can get
1856 	 * at them.  Use the registers at the end of the Unibus map (since
1857 	 * we will use the registers at the beginning to map the memory
1858 	 * we are dumping).
1859 	 */
1860 	num = btoc(sizeof(struct uda1)) + 1;
1861 	reg = NUBMREG - num;
1862 	io = &uba->uba_map[reg];
1863 	for (i = 0; i < num; i++)
1864 		*(int *)io++ = UBAMR_MRV | (btop(ud) + i);
1865 	ud_ubaddr = (struct uda1 *)(((int)ud & PGOFSET) | (reg << 9));
1866 
1867 	/*
1868 	 * Initialise the controller, with one command and one response
1869 	 * packet.
1870 	 */
1871 	udaddr->udaip = 0;
1872 	if (udadumpwait(udaddr, UDA_STEP1))
1873 		return (EFAULT);
1874 	udaddr->udasa = UDA_ERR;
1875 	if (udadumpwait(udaddr, UDA_STEP2))
1876 		return (EFAULT);
1877 	udaddr->udasa = (int)&ud_ubaddr->uda1_ca.ca_rspdsc;
1878 	if (udadumpwait(udaddr, UDA_STEP3))
1879 		return (EFAULT);
1880 	udaddr->udasa = ((int)&ud_ubaddr->uda1_ca.ca_rspdsc) >> 16;
1881 	if (udadumpwait(udaddr, UDA_STEP4))
1882 		return (EFAULT);
1883 	uda_softc[ui->ui_ctlr].sc_micro = udaddr->udasa & 0xff;
1884 	udaddr->udasa = UDA_GO;
1885 
1886 	/*
1887 	 * Set up the command and response descriptor, then set the
1888 	 * controller characteristics and bring the drive on line.
1889 	 * Note that all uninitialised locations in uda1_cmd are zero.
1890 	 */
1891 	ud->uda1_ca.ca_rspdsc = (long)&ud_ubaddr->uda1_rsp.mscp_cmdref;
1892 	ud->uda1_ca.ca_cmddsc = (long)&ud_ubaddr->uda1_cmd.mscp_cmdref;
1893 	/* ud->uda1_cmd.mscp_sccc.sccc_ctlrflags = 0; */
1894 	/* ud->uda1_cmd.mscp_sccc.sccc_version = 0; */
1895 	if (udadumpcmd(M_OP_SETCTLRC, ud, ui))
1896 		return (EFAULT);
1897 	ud->uda1_cmd.mscp_unit = ui->ui_slave;
1898 	if (udadumpcmd(M_OP_ONLINE, ud, ui))
1899 		return (EFAULT);
1900 
1901 	pp = phys(struct partition *,
1902 	    &udalabel[unit].d_partitions[udapart(dev)]);
1903 	maxsz = pp->p_size;
1904 	blkoff = pp->p_offset;
1905 
1906 	/*
1907 	 * Dump all of physical memory, or as much as will fit in the
1908 	 * space provided.
1909 	 */
1910 	start = 0;
1911 	num = maxfree;
1912 	if (dumplo + num >= maxsz)
1913 		num = maxsz - dumplo;
1914 	blkoff += dumplo;
1915 
1916 	/*
1917 	 * Write out memory, DBSIZE pages at a time.
1918 	 * N.B.: this code depends on the fact that the sector
1919 	 * size == the page size.
1920 	 */
1921 	while (num > 0) {
1922 		blk = num > DBSIZE ? DBSIZE : num;
1923 		io = uba->uba_map;
1924 		/*
1925 		 * Map in the pages to write, leaving an invalid entry
1926 		 * at the end to guard against wild Unibus transfers.
1927 		 * Then do the write.
1928 		 */
1929 		for (i = 0; i < blk; i++)
1930 			*(int *)io++ = UBAMR_MRV | (btop(start) + i);
1931 		*(int *)io = 0;
1932 		ud->uda1_cmd.mscp_unit = ui->ui_slave;
1933 		ud->uda1_cmd.mscp_seq.seq_lbn = btop(start) + blkoff;
1934 		ud->uda1_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT;
1935 		if (udadumpcmd(M_OP_WRITE, ud, ui))
1936 			return (EIO);
1937 		start += blk << PGSHIFT;
1938 		num -= blk;
1939 	}
1940 	return (0);		/* made it! */
1941 }
1942 
1943 /*
1944  * Wait for some of the bits in `bits' to come on.  If the error bit
1945  * comes on, or ten seconds pass without response, return true (error).
1946  */
1947 udadumpwait(udaddr, bits)
1948 	register struct udadevice *udaddr;
1949 	register int bits;
1950 {
1951 	register int timo = todr() + 1000;
1952 
1953 	while ((udaddr->udasa & bits) == 0) {
1954 		if (udaddr->udasa & UDA_ERR) {
1955 			printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
1956 			return (1);
1957 		}
1958 		if (todr() >= timo) {
1959 			printf("timeout\ndump ");
1960 			return (1);
1961 		}
1962 	}
1963 	return (0);
1964 }
1965 
1966 /*
1967  * Feed a command to the UDA50, wait for its response, and return
1968  * true iff something went wrong.
1969  */
1970 udadumpcmd(op, ud, ui)
1971 	int op;
1972 	register struct uda1 *ud;
1973 	struct uba_device *ui;
1974 {
1975 	register struct udadevice *udaddr;
1976 	register int n;
1977 #define mp (&ud->uda1_rsp)
1978 
1979 	udaddr = (struct udadevice *)ui->ui_physaddr;
1980 	ud->uda1_cmd.mscp_opcode = op;
1981 	ud->uda1_cmd.mscp_msglen = MSCP_MSGLEN;
1982 	ud->uda1_rsp.mscp_msglen = MSCP_MSGLEN;
1983 	ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
1984 	ud->uda1_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT;
1985 	if (udaddr->udasa & UDA_ERR) {
1986 		printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
1987 		return (1);
1988 	}
1989 	n = udaddr->udaip;
1990 	n = todr() + 1000;
1991 	for (;;) {
1992 		if (todr() > n) {
1993 			printf("timeout\ndump ");
1994 			return (1);
1995 		}
1996 		if (ud->uda1_ca.ca_cmdint)
1997 			ud->uda1_ca.ca_cmdint = 0;
1998 		if (ud->uda1_ca.ca_rspint == 0)
1999 			continue;
2000 		ud->uda1_ca.ca_rspint = 0;
2001 		if (mp->mscp_opcode == (op | M_OP_END))
2002 			break;
2003 		printf("\n");
2004 		switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
2005 
2006 		case MSCPT_SEQ:
2007 			printf("sequential");
2008 			break;
2009 
2010 		case MSCPT_DATAGRAM:
2011 			mscp_decodeerror("uda", ui->ui_ctlr, mp);
2012 			printf("datagram");
2013 			break;
2014 
2015 		case MSCPT_CREDITS:
2016 			printf("credits");
2017 			break;
2018 
2019 		case MSCPT_MAINTENANCE:
2020 			printf("maintenance");
2021 			break;
2022 
2023 		default:
2024 			printf("unknown (type 0x%x)",
2025 				MSCP_MSGTYPE(mp->mscp_msgtc));
2026 			break;
2027 		}
2028 		printf(" ignored\ndump ");
2029 		ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
2030 	}
2031 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
2032 		printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op,
2033 			mp->mscp_opcode, mp->mscp_status);
2034 		return (1);
2035 	}
2036 	return (0);
2037 #undef mp
2038 }
2039 
2040 /*
2041  * Return the size of a partition, if known, or -1 if not.
2042  */
2043 udasize(dev)
2044 	dev_t dev;
2045 {
2046 	register int unit = udaunit(dev);
2047 	register struct uba_device *ui;
2048 
2049 	if (unit >= NRA || (ui = udadinfo[unit]) == NULL ||
2050 	    ui->ui_alive == 0 || (ui->ui_flags & UNIT_ONLINE) == 0 ||
2051 	    ra_info[unit].ra_state != OPEN)
2052 		return (-1);
2053 	return ((int)udalabel[unit].d_partitions[udapart(dev)].p_size);
2054 }
2055 
2056 #ifdef COMPAT_42
2057 /*
2058  * Tables mapping unlabelled drives.
2059  */
2060 struct size {
2061 	daddr_t nblocks;
2062 	daddr_t blkoff;
2063 } ra60_sizes[8] = {
2064 	15884,	0,		/* A=sectors 0 thru 15883 */
2065 	33440,	15884,		/* B=sectors 15884 thru 49323 */
2066 	400176,	0,		/* C=sectors 0 thru 400175 */
2067 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
2068 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
2069 	350852,	49324,		/* F=sectors 49324 thru 400175 */
2070 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
2071 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
2072 }, ra70_sizes[8] = {
2073 	15884,	0,		/* A=blk 0 thru 15883 */
2074 	33440,	15972,		/* B=blk 15972 thru 49323 */
2075 	-1,	0,		/* C=blk 0 thru end */
2076 	15884,	341220,		/* D=blk 341220 thru 357103 */
2077 	55936,	357192,		/* E=blk 357192 thru 413127 */
2078 	-1,	413457,		/* F=blk 413457 thru end */
2079 	-1,	341220,		/* G=blk 341220 thru end */
2080 	291346,	49731,		/* H=blk 49731 thru 341076 */
2081 }, ra80_sizes[8] = {
2082 	15884,	0,		/* A=sectors 0 thru 15883 */
2083 	33440,	15884,		/* B=sectors 15884 thru 49323 */
2084 	242606,	0,		/* C=sectors 0 thru 242605 */
2085 	0,	0,		/* D=unused */
2086 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
2087 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
2088 	192696,	49910,		/* G=sectors 49910 thru 242605 */
2089 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
2090 }, ra81_sizes[8] ={
2091 /*
2092  * These are the new standard partition sizes for ra81's.
2093  * An RA_COMPAT system is compiled with D, E, and F corresponding
2094  * to the 4.2 partitions for G, H, and F respectively.
2095  */
2096 #ifndef	UCBRA
2097 	15884,	0,		/* A=sectors 0 thru 15883 */
2098 	66880,	16422,		/* B=sectors 16422 thru 83301 */
2099 	891072,	0,		/* C=sectors 0 thru 891071 */
2100 #ifdef RA_COMPAT
2101 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
2102 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
2103 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
2104 #else
2105 	15884,	375564,		/* D=sectors 375564 thru 391447 */
2106 	307200,	391986,		/* E=sectors 391986 thru 699185 */
2107 	191352,	699720,		/* F=sectors 699720 thru 891071 */
2108 #endif RA_COMPAT
2109 	515508,	375564,		/* G=sectors 375564 thru 891071 */
2110 	291346,	83538,		/* H=sectors 83538 thru 374883 */
2111 
2112 /*
2113  * These partitions correspond to the sizes used by sites at Berkeley,
2114  * and by those sites that have received copies of the Berkeley driver
2115  * with deltas 6.2 or greater (11/15/83).
2116  */
2117 #else UCBRA
2118 
2119 	15884,	0,		/* A=sectors 0 thru 15883 */
2120 	33440,	15884,		/* B=sectors 15884 thru 49323 */
2121 	891072,	0,		/* C=sectors 0 thru 891071 */
2122 	15884,	242606,		/* D=sectors 242606 thru 258489 */
2123 	307200,	258490,		/* E=sectors 258490 thru 565689 */
2124 	325382,	565690,		/* F=sectors 565690 thru 891071 */
2125 	648466,	242606,		/* G=sectors 242606 thru 891071 */
2126 	193282,	49324,		/* H=sectors 49324 thru 242605 */
2127 
2128 #endif UCBRA
2129 }, ra82_sizes[8] = {
2130 	15884,	0,		/* A=blk 0 thru 15883 */
2131 	66880,	16245,		/* B=blk 16245 thru 83124 */
2132 	-1,	0,		/* C=blk 0 thru end */
2133 	15884,	375345,		/* D=blk 375345 thru 391228 */
2134 	307200,	391590,		/* E=blk 391590 thru 698789 */
2135 	-1,	699390,		/* F=blk 699390 thru end */
2136 	-1,	375345,		/* G=blk 375345 thru end */
2137 	291346,	83790,		/* H=blk 83790 thru 375135 */
2138 }, rc25_sizes[8] = {
2139 	15884,	0,		/* A=blk 0 thru 15883 */
2140 	10032,	15884,		/* B=blk 15884 thru 49323 */
2141 	-1,	0,		/* C=blk 0 thru end */
2142 	0,	0,		/* D=blk 340670 thru 356553 */
2143 	0,	0,		/* E=blk 356554 thru 412489 */
2144 	0,	0,		/* F=blk 412490 thru end */
2145 	-1,	25916,		/* G=blk 49324 thru 131403 */
2146 	0,	0,		/* H=blk 131404 thru end */
2147 }, rd52_sizes[8] = {
2148 	15884,	0,		/* A=blk 0 thru 15883 */
2149 	9766,	15884,		/* B=blk 15884 thru 25649 */
2150 	-1,	0,		/* C=blk 0 thru end */
2151 	0,	0,		/* D=unused */
2152 	0,	0,		/* E=unused */
2153 	0,	0,		/* F=unused */
2154 	-1,	25650,		/* G=blk 25650 thru end */
2155 	0,	0,		/* H=unused */
2156 }, rd53_sizes[8] = {
2157 	15884,	0,		/* A=blk 0 thru 15883 */
2158 	33440,	15884,		/* B=blk 15884 thru 49323 */
2159 	-1,	0,		/* C=blk 0 thru end */
2160 	0,	0,		/* D=unused */
2161 	33440,	0,		/* E=blk 0 thru 33439 */
2162 	-1,	33440,		/* F=blk 33440 thru end */
2163 	-1,	49324,		/* G=blk 49324 thru end */
2164 	-1,	15884,		/* H=blk 15884 thru end */
2165 }, rx50_sizes[8] = {
2166 	800,	0,		/* A=blk 0 thru 799 */
2167 	0,	0,
2168 	-1,	0,		/* C=blk 0 thru end */
2169 	0,	0,
2170 	0,	0,
2171 	0,	0,
2172 	0,	0,
2173 	0,	0,
2174 };
2175 
2176 /*
2177  * Media ID decoding table.
2178  */
2179 struct	udatypes {
2180 	u_long	ut_id;		/* media drive ID */
2181 	char	*ut_name;	/* drive type name */
2182 	struct	size *ut_sizes;	/* partition tables */
2183 	int	ut_nsectors, ut_ntracks, ut_ncylinders;
2184 } udatypes[] = {
2185 	{ MSCP_MKDRIVE2('R', 'A', 60), "ra60", ra60_sizes, 42, 4, 2382 },
2186 	{ MSCP_MKDRIVE2('R', 'A', 70), "ra70", ra70_sizes, 33, 11, 1507 },
2187 	{ MSCP_MKDRIVE2('R', 'A', 80), "ra80", ra80_sizes, 31, 14, 559 },
2188 	{ MSCP_MKDRIVE2('R', 'A', 81), "ra81", ra81_sizes, 51, 14, 1248 },
2189 	{ MSCP_MKDRIVE2('R', 'A', 82), "ra82", ra82_sizes, 57, 14, 1423 },
2190 	{ MSCP_MKDRIVE2('R', 'C', 25), "rc25-removable",
2191 						rc25_sizes, 42, 4, 302 },
2192 	{ MSCP_MKDRIVE3('R', 'C', 'F', 25), "rc25-fixed",
2193 						rc25_sizes, 42, 4, 302 },
2194 	{ MSCP_MKDRIVE2('R', 'D', 52), "rd52", rd52_sizes, 18, 7, 480 },
2195 	{ MSCP_MKDRIVE2('R', 'D', 53), "rd53", rd53_sizes, 18, 8, 963 },
2196 	{ MSCP_MKDRIVE2('R', 'X', 50), "rx50", rx50_sizes, 10, 1, 80 },
2197 	0
2198 };
2199 
2200 #define NTYPES (sizeof(udatypes) / sizeof(*udatypes))
2201 
2202 udamaptype(unit, lp)
2203 	int unit;
2204 	register struct disklabel *lp;
2205 {
2206 	register struct udatypes *ut;
2207 	register struct size *sz;
2208 	register struct partition *pp;
2209 	register char *p;
2210 	register int i;
2211 	register struct ra_info *ra = &ra_info[unit];
2212 
2213 	i = MSCP_MEDIA_DRIVE(ra->ra_mediaid);
2214 	for (ut = udatypes; ut->ut_id; ut++)
2215 		if (ut->ut_id == i &&
2216 		    ut->ut_nsectors == ra->ra_geom.rg_nsectors &&
2217 		    ut->ut_ntracks == ra->ra_geom.rg_ntracks &&
2218 		    ut->ut_ncylinders == ra->ra_geom.rg_ncyl)
2219 			goto found;
2220 
2221 	/* not one we know; fake up a label for the whole drive */
2222 	uda_makefakelabel(ra, lp);
2223 	i = ra->ra_mediaid;	/* print the port type too */
2224 	addlog(": no partition table for %c%c %c%c%c%d, size %d;\n\
2225 using (s,t,c)=(%d,%d,%d)",
2226 		MSCP_MID_CHAR(4, i), MSCP_MID_CHAR(3, i),
2227 		MSCP_MID_CHAR(2, i), MSCP_MID_CHAR(1, i),
2228 		MSCP_MID_CHAR(0, i), MSCP_MID_NUM(i), lp->d_secperunit,
2229 		lp->d_nsectors, lp->d_ntracks, lp->d_ncylinders);
2230 	if (!cold)
2231 		addlog("\n");
2232 	return (0);
2233 found:
2234 	p = ut->ut_name;
2235 	for (i = 0; i < sizeof(lp->d_typename) - 1 && *p; i++)
2236 		lp->d_typename[i] = *p++;
2237 	lp->d_typename[i] = 0;
2238 	sz = ut->ut_sizes;
2239 	lp->d_nsectors = ut->ut_nsectors;
2240 	lp->d_ntracks = ut->ut_ntracks;
2241 	lp->d_ncylinders = ut->ut_ncylinders;
2242 	lp->d_npartitions = 8;
2243 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
2244 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8]; pp++, sz++) {
2245 		pp->p_offset = sz->blkoff;
2246 		if ((pp->p_size = sz->nblocks) == (u_long)-1)
2247 			pp->p_size = ra->ra_dsize - sz->blkoff;
2248 	}
2249 	return (1);
2250 }
2251 #endif /* COMPAT_42 */
2252 
2253 /*
2254  * Construct a label for a drive from geometry information
2255  * if we have no better information.
2256  */
2257 uda_makefakelabel(ra, lp)
2258 	register struct ra_info *ra;
2259 	register struct disklabel *lp;
2260 {
2261 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
2262 	lp->d_ntracks = ra->ra_geom.rg_ntracks;
2263 	lp->d_ncylinders = ra->ra_geom.rg_ncyl;
2264 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
2265 	bcopy("ra??", lp->d_typename, sizeof("ra??"));
2266 	lp->d_npartitions = 1;
2267 	lp->d_partitions[0].p_offset = 0;
2268 	lp->d_partitions[0].p_size = lp->d_secperunit;
2269 }
2270 #endif /* NUDA > 0 */
2271