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