xref: /csrg-svn/sys/vax/uba/uda.c (revision 33195)
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.11 (Berkeley) 01/02/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 	u_long	ra_openpart;	/* partitions open */
180 	u_long	ra_bopenpart;	/* block partitions open */
181 	u_long	ra_copenpart;	/* character partitions open */
182 } ra_info[NRA];
183 
184 /*
185  * Software state, per drive
186  */
187 #define	CLOSED		0
188 #define	WANTOPEN	1
189 #define	RDLABEL		2
190 #define	OPEN		3
191 #define	OPENRAW		4
192 
193 /*
194  * Definition of the driver for autoconf.
195  */
196 int	udaprobe(), udaslave(), udaattach(), udadgo(), udaintr();
197 struct	uba_ctlr *udaminfo[NUDA];
198 struct	uba_device *udadinfo[NRA];
199 struct	disklabel udalabel[NRA];
200 
201 u_short	udastd[] = { 0772150, 0772550, 0777550, 0 };
202 struct	uba_driver udadriver =
203  { udaprobe, udaslave, udaattach, udadgo, udastd, "ra", udadinfo, "uda",
204    udaminfo };
205 
206 /*
207  * More driver definitions, for generic MSCP code.
208  */
209 int	udadgram(), udactlrdone(), udaunconf(), udaiodone();
210 int	udaonline(), udagotstatus(), udaioerror(), udareplace(), udabb();
211 
212 struct	buf udautab[NRA];	/* per drive transfer queue */
213 
214 struct	mscp_driver udamscpdriver =
215  { MAXUNIT, NRA, UNITSHIFT, udautab, udadinfo,
216    udadgram, udactlrdone, udaunconf, udaiodone,
217    udaonline, udagotstatus, udareplace, udaioerror, udabb,
218    "uda", "ra" };
219 
220 /*
221  * Miscellaneous private variables.
222  */
223 char	udasr_bits[] = UDASR_BITS;
224 
225 struct	uba_device *udaip[NUDA][MAXUNIT];
226 				/* inverting pointers: ctlr & unit => Unibus
227 				   device pointer */
228 
229 int	udaburst[NUDA] = { 0 };	/* burst size, per UDA50, zero => default;
230 				   in data space so patchable via adb */
231 
232 struct	mscp udaslavereply;	/* get unit status response packet, set
233 				   for udaslave by udaunconf, via udaintr */
234 
235 static struct uba_ctlr *probeum;/* this is a hack---autoconf should pass ctlr
236 				   info to slave routine; instead, we remember
237 				   the last ctlr argument to probe */
238 
239 int	udawstart, udawatch();	/* watchdog timer */
240 
241 /*
242  * Externals
243  */
244 int	wakeup();
245 int	hz;
246 
247 /*
248  * Poke at a supposed UDA50 to see if it is there.
249  * This routine duplicates some of the code in udainit() only
250  * because autoconf has not set up the right information yet.
251  * We have to do everything `by hand'.
252  */
253 udaprobe(reg, ctlr, um)
254 	caddr_t reg;
255 	int ctlr;
256 	struct uba_ctlr *um;
257 {
258 	register int br, cvec;
259 	register struct uda_softc *sc;
260 	register struct udadevice *udaddr;
261 	register struct mscp_info *mi;
262 	int timeout, tries;
263 
264 #ifdef VAX750
265 	/*
266 	 * The UDA50 wants to share BDPs on 750s, but not on 780s or
267 	 * 8600s.  (730s have no BDPs anyway.)  Toward this end, we
268 	 * here set the `keep bdp' flag in the per-driver information
269 	 * if this is a 750.  (We just need to do it once, but it is
270 	 * easiest to do it now, for each UDA50.)
271 	 */
272 	if (cpu == VAX_750)
273 		udadriver.ud_keepbdp = 1;
274 #endif
275 
276 	probeum = um;			/* remember for udaslave() */
277 #ifdef lint
278 	br = 0; cvec = br; br = cvec; udaintr(0);
279 #endif
280 	/*
281 	 * Set up the controller-specific generic MSCP driver info.
282 	 * Note that this should really be done in the (nonexistent)
283 	 * controller attach routine.
284 	 */
285 	sc = &uda_softc[ctlr];
286 	mi = &sc->sc_mi;
287 	mi->mi_md = &udamscpdriver;
288 	mi->mi_ctlr = um->um_ctlr;
289 	mi->mi_tab = &um->um_tab;
290 	mi->mi_ip = udaip[ctlr];
291 	mi->mi_cmd.mri_size = NCMD;
292 	mi->mi_cmd.mri_desc = uda[ctlr].uda_ca.ca_cmddsc;
293 	mi->mi_cmd.mri_ring = uda[ctlr].uda_cmd;
294 	mi->mi_rsp.mri_size = NRSP;
295 	mi->mi_rsp.mri_desc = uda[ctlr].uda_ca.ca_rspdsc;
296 	mi->mi_rsp.mri_ring = uda[ctlr].uda_rsp;
297 	mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab;
298 
299 	/*
300 	 * More controller specific variables.  Again, this should
301 	 * be in the controller attach routine.
302 	 */
303 	if (udaburst[ctlr] == 0)
304 		udaburst[ctlr] = DEFAULT_BURST;
305 
306 	/*
307 	 * Get an interrupt vector.  Note that even if the controller
308 	 * does not respond, we keep the vector.  This is not a serious
309 	 * problem; but it would be easily fixed if we had a controller
310 	 * attach routine.  Sigh.
311 	 */
312 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
313 	udaddr = (struct udadevice *) reg;
314 
315 	/*
316 	 * Initialise the controller (partially).  The UDA50 programmer's
317 	 * manual states that if initialisation fails, it should be retried
318 	 * at least once, but after a second failure the port should be
319 	 * considered `down'; it also mentions that the controller should
320 	 * initialise within ten seconds.  Or so I hear; I have not seen
321 	 * this manual myself.
322 	 */
323 	tries = 0;
324 again:
325 	udaddr->udaip = 0;		/* start initialisation */
326 	timeout = todr() + 1000;	/* timeout in 10 seconds */
327 	while ((udaddr->udasa & UDA_STEP1) == 0)
328 		if (todr() > timeout)
329 			goto bad;
330 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
331 		(sc->sc_ivec >> 2);
332 	while ((udaddr->udasa & UDA_STEP2) == 0)
333 		if (todr() > timeout)
334 			goto bad;
335 
336 	/* should have interrupted by now */
337 #ifdef VAX630
338 	if (cpu == VAX_630)
339 		br = 0x15;	/* screwy interrupt structure */
340 #endif
341 	return (sizeof (struct udadevice));
342 bad:
343 	if (++tries < 2)
344 		goto again;
345 	return (0);
346 }
347 
348 /*
349  * Find a slave.  We allow wildcard slave numbers (something autoconf
350  * is not really prepared to deal with); and we need to know the
351  * controller number to talk to the UDA.  For the latter, we keep
352  * track of the last controller probed, since a controller probe
353  * immediately precedes all slave probes for that controller.  For the
354  * former, we simply put the unit number into ui->ui_slave after we
355  * have found one.
356  *
357  * Note that by the time udaslave is called, the interrupt vector
358  * for the UDA50 has been set up (so that udaunconf() will be called).
359  */
360 udaslave(ui, reg)
361 	register struct uba_device *ui;
362 	caddr_t reg;
363 {
364 	register struct uba_ctlr *um = probeum;
365 	register struct mscp *mp;
366 	register struct uda_softc *sc;
367 	register struct ra_info *ra;
368 	int next = 0, type, 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 `mspw' 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 	if (ui->ui_dk >= 0)
535 		dk_mspw[ui->ui_dk] = 1.0 / (60 * 31 * 256);	/* approx */
536 	udaip[ui->ui_ctlr][ui->ui_slave] = ui;
537 
538 	/*
539 	 * RX50s cannot be brought on line unless there is
540 	 * a floppy in the drive.  Since an ONLINE while cold
541 	 * takes ten seconds to fail, and (when notyet becomes now)
542 	 * no sensible person will swap to an RX50, we just
543 	 * defer the ONLINE until someone tries to use the drive.
544 	 */
545 	if (ra_info[unit].ra_type == RA_TYPE_RX50) {
546 		printf("ra%d: rx50\n", unit);
547 		return;
548 	}
549 	if (uda_rainit(ui, 0))
550 		printf("ra%d: offline\n", unit);
551 	else {
552 		printf("ra%d: %s\n", unit, udalabel[unit].d_typename);
553 #ifdef notyet
554 		addswap(makedev(UDADEVNUM, udaminor(unit, 0)), &udalabel[unit]);
555 #endif
556 	}
557 }
558 
559 /*
560  * Initialise a UDA50.  Return true iff something goes wrong.
561  */
562 udainit(ctlr)
563 	int ctlr;
564 {
565 	register struct uda_softc *sc;
566 	register struct udadevice *udaddr;
567 	struct uba_ctlr *um;
568 	int timo, ubinfo;
569 
570 	sc = &uda_softc[ctlr];
571 	um = udaminfo[ctlr];
572 	if ((sc->sc_flags & SC_MAPPED) == 0) {
573 		/*
574 		 * Map the communication area and command and
575 		 * response packets into Unibus space.
576 		 */
577 		ubinfo = uballoc(um->um_ubanum, (caddr_t) &uda[ctlr],
578 			sizeof (struct uda), UBA_CANTWAIT);
579 		if (ubinfo == 0) {
580 			printf("uda%d: uballoc map failed\n", ctlr);
581 			return (-1);
582 		}
583 		sc->sc_uda = (struct uda *) (ubinfo & 0x3ffff);
584 		sc->sc_flags |= SC_MAPPED;
585 	}
586 
587 	/*
588 	 * While we are thinking about it, reset the next command
589 	 * and response indicies.
590 	 */
591 	sc->sc_mi.mi_cmd.mri_next = 0;
592 	sc->sc_mi.mi_rsp.mri_next = 0;
593 
594 	/*
595 	 * Start up the hardware initialisation sequence.
596 	 */
597 #define	STEP0MASK	(UDA_ERR | UDA_STEP4 | UDA_STEP3 | UDA_STEP2 | \
598 			 UDA_STEP1 | UDA_NV)
599 
600 	sc->sc_state = ST_IDLE;	/* in case init fails */
601 	udaddr = (struct udadevice *) um->um_addr;
602 	udaddr->udaip = 0;
603 	timo = todr() + 1000;
604 	while ((udaddr->udasa & STEP0MASK) == 0) {
605 		if (todr() > timo) {
606 			printf("uda%d: timeout during init\n", ctlr);
607 			return (-1);
608 		}
609 	}
610 	if ((udaddr->udasa & STEP0MASK) != UDA_STEP1) {
611 		printf("uda%d: init failed, sa=%b\n", ctlr,
612 			udaddr->udasa, udasr_bits);
613 		return (-1);
614 	}
615 
616 	/*
617 	 * Success!  Record new state, and start step 1 initialisation.
618 	 * The rest is done in the interrupt handler.
619 	 */
620 	sc->sc_state = ST_STEP1;
621 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
622 	    (sc->sc_ivec >> 2);
623 	return (0);
624 }
625 
626 /*
627  * Open a drive.
628  */
629 /*ARGSUSED*/
630 udaopen(dev, flag, fmt)
631 	dev_t dev;
632 	int flag, fmt;
633 {
634 	register int unit;
635 	register struct uba_device *ui;
636 	register struct uda_softc *sc;
637 	register struct disklabel *lp;
638 	register struct partition *pp;
639 	register struct ra_info *ra;
640 	int s, i, part, mask, error = 0;
641 	daddr_t start, end;
642 
643 	/*
644 	 * Make sure this is a reasonable open request.
645 	 */
646 	unit = udaunit(dev);
647 	if (unit >= NRA || (ui = udadinfo[unit]) == 0 || ui->ui_alive == 0)
648 		return (ENXIO);
649 
650 	/*
651 	 * Make sure the controller is running, by (re)initialising it if
652 	 * necessary.
653 	 */
654 	sc = &uda_softc[ui->ui_ctlr];
655 	s = spl5();
656 	if (sc->sc_state != ST_RUN) {
657 		if (sc->sc_state == ST_IDLE && udainit(ui->ui_ctlr)) {
658 			splx(s);
659 			return (EIO);
660 		}
661 		/*
662 		 * In case it does not come up, make sure we will be
663 		 * restarted in 10 seconds.  This corresponds to the
664 		 * 10 second timeouts in udaprobe() and udaslave().
665 		 */
666 		sc->sc_flags |= SC_DOWAKE;
667 		timeout(wakeup, (caddr_t) sc, 10 * hz);
668 		sleep((caddr_t) sc, PRIBIO);
669 		if (sc->sc_state != ST_RUN) {
670 			splx(s);
671 			printf("uda%d: controller hung\n", ui->ui_ctlr);
672 			return (EIO);
673 		}
674 		untimeout(wakeup, (caddr_t) sc);
675 	}
676 
677 	/*
678 	 * Wait for the state to settle
679 	 */
680 	ra = &ra_info[unit];
681 	while (ra->ra_state != OPEN && ra->ra_state != OPENRAW &&
682 	    ra->ra_state != CLOSED)
683 		sleep((caddr_t)ra, PZERO + 1);
684 
685 	/*
686 	 * If not on line, or we are not sure of the label, reinitialise
687 	 * the drive.
688 	 */
689 	if ((ui->ui_flags & UNIT_ONLINE) == 0 ||
690 	    (ra->ra_state != OPEN && ra->ra_state != OPENRAW))
691 		error = uda_rainit(ui, flag);
692 	splx(s);
693 	if (error)
694 		return (error);
695 
696 	part = udapart(dev);
697 	lp = &udalabel[unit];
698 	if (part >= lp->d_npartitions)
699 		return (ENXIO);
700 	/*
701 	 * Warn if a partition is opened that overlaps another
702 	 * already open, unless either is the `raw' partition
703 	 * (whole disk).
704 	 */
705 #define	RAWPART		2	/* 'c' partition */	/* XXX */
706 	mask = 1 << part;
707 	if ((ra->ra_openpart & mask) == 0 && part != RAWPART) {
708 		pp = &lp->d_partitions[part];
709 		start = pp->p_offset;
710 		end = pp->p_offset + pp->p_size;
711 		for (pp = lp->d_partitions, i = 0;
712 		     i < lp->d_npartitions; pp++, i++) {
713 			if (pp->p_offset + pp->p_size <= start ||
714 			    pp->p_offset >= end || i == RAWPART)
715 				continue;
716 			if (ra->ra_openpart & (1 << i))
717 				log(LOG_WARNING,
718 				    "ra%d%c: overlaps open partition (%c)\n",
719 				    unit, part + 'a', i + 'a');
720 		}
721 	}
722 	switch (fmt) {
723 	case S_IFCHR:
724 		ra->ra_copenpart |= mask;
725 		break;
726 	case S_IFBLK:
727 		ra->ra_bopenpart |= mask;
728 		break;
729 	}
730 	ra->ra_openpart |= mask;
731 	return (0);
732 }
733 
734 udaclose(dev, flags, fmt)
735 	dev_t dev;
736 	int flags, fmt;
737 {
738 	register int unit = udaunit(dev);
739 	register struct ra_info *ra = &ra_info[unit];
740 	int s, mask = (1 << udapart(dev));
741 
742 	switch (fmt) {
743 	case S_IFCHR:
744 		ra->ra_copenpart &= ~mask;
745 		break;
746 	case S_IFBLK:
747 		ra->ra_bopenpart &= ~mask;
748 		break;
749 	}
750 	ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
751 
752 	/*
753 	 * Should wait for I/O to complete on this partition even if
754 	 * others are open, but wait for work on blkflush().
755 	 */
756 	if (ra->ra_openpart == 0) {
757 		s = spl5();
758 		while (udautab[unit].b_actf)
759 			sleep((caddr_t)&udautab[unit], PZERO - 1);
760 		splx(s);
761 		ra->ra_state = CLOSED;
762 	}
763 	return (0);
764 }
765 
766 /*
767  * Initialise a drive.  If it is not already, bring it on line,
768  * and set a timeout on it in case it fails to respond.
769  * When on line, read in the pack label.
770  */
771 uda_rainit(ui, flags)
772 	register struct uba_device *ui;
773 	int flags;
774 {
775 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
776 	register struct disklabel *lp;
777 	register struct mscp *mp;
778 	register int unit = ui->ui_unit;
779 	register struct ra_info *ra;
780 	char *msg, *readdisklabel();
781 	int s, i, udastrategy();
782 	extern int cold;
783 
784 	ra = &ra_info[unit];
785 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
786 		mp = mscp_getcp(&sc->sc_mi, MSCP_WAIT);
787 		mp->mscp_opcode = M_OP_ONLINE;
788 		mp->mscp_unit = ui->ui_slave;
789 		mp->mscp_cmdref = (long)&ui->ui_flags;
790 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
791 		ra->ra_state = WANTOPEN;
792 		if (!cold)
793 			s = spl5();
794 		i = ((struct udadevice *)ui->ui_addr)->udaip;
795 
796 		if (cold) {
797 			i = todr() + 1000;
798 			while ((ui->ui_flags & UNIT_ONLINE) == 0)
799 				if (todr() > i)
800 					break;
801 		} else {
802 			timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz);
803 			sleep((caddr_t)&ui->ui_flags, PSWP + 1);
804 			splx(s);
805 			untimeout(wakeup, (caddr_t)&ui->ui_flags);
806 		}
807 		if (ra->ra_state != OPENRAW) {
808 			ra->ra_state = CLOSED;
809 			wakeup((caddr_t)ra);
810 			return (EIO);
811 		}
812 	}
813 
814 	lp = &udalabel[unit];
815 	lp->d_secsize = DEV_BSIZE;
816 	lp->d_secperunit = ra->ra_dsize;
817 
818 	if (flags & O_NDELAY)
819 		return (0);
820 	ra->ra_state = RDLABEL;
821 	/*
822 	 * Set up default sizes until we have the label, or longer
823 	 * if there is none.  Set secpercyl, as readdisklabel wants
824 	 * to compute b_cylin (although we do not need it).
825 	 */
826 	lp->d_secpercyl = 1;
827 	lp->d_npartitions = 1;
828 	lp->d_partitions[0].p_size = lp->d_secperunit;
829 	lp->d_partitions[0].p_offset = 0;
830 
831 	/*
832 	 * Read pack label.
833 	 */
834 	if ((msg = readdisklabel(udaminor(unit, 0), udastrategy, lp)) != NULL) {
835 		log(LOG_ERR, "ra%d: %s\n", unit, msg);
836 #ifdef COMPAT_42
837 		if (udamaptype(unit, lp))
838 			ra->ra_state = OPEN;
839 		else
840 			ra->ra_state = OPENRAW;
841 #else
842 		ra->ra_state = OPENRAW;
843 		/* uda_makefakelabel(ra, lp); */
844 #endif
845 	} else
846 		ra->ra_state = OPEN;
847 	wakeup((caddr_t)ra);
848 	return (0);
849 }
850 
851 /*
852  * Copy the geometry information for the given ra from a
853  * GET UNIT STATUS response.  If check, see if it changed.
854  */
855 uda_rasave(unit, mp, check)
856 	int unit;
857 	register struct mscp *mp;
858 	int check;
859 {
860 	register struct ra_info *ra = &ra_info[unit];
861 
862 	if (check && ra->ra_type != mp->mscp_guse.guse_drivetype) {
863 		printf("ra%d: changed types! was %d now %d\n",
864 			ra->ra_type, mp->mscp_guse.guse_drivetype);
865 		ra->ra_state = CLOSED;	/* ??? */
866 	}
867 	ra->ra_type = mp->mscp_guse.guse_drivetype;
868 	ra->ra_mediaid = mp->mscp_guse.guse_mediaid;
869 	ra->ra_geom.rg_nsectors = mp->mscp_guse.guse_nspt;
870 	ra->ra_geom.rg_ngroups = mp->mscp_guse.guse_group;
871 	ra->ra_geom.rg_ngpc = mp->mscp_guse.guse_ngpc;
872 	ra->ra_geom.rg_ntracks = ra->ra_geom.rg_ngroups * ra->ra_geom.rg_ngpc;
873 	/* ra_geom.rg_ncyl cannot be computed until we have ra_dsize */
874 #ifdef notyet
875 	ra->ra_geom.rg_rctsize = mp->mscp_guse.guse_rctsize;
876 	ra->ra_geom.rg_rbns = mp->mscp_guse.guse_nrpt;
877 	ra->ra_geom.rg_nrct = mp->mscp_guse.guse_nrct;
878 #endif
879 }
880 
881 /*
882  * Queue a transfer request, and if possible, hand it to the controller.
883  *
884  * This routine is broken into two so that the internal version
885  * udastrat1() can be called by the (nonexistent, as yet) bad block
886  * revectoring routine.
887  */
888 udastrategy(bp)
889 	register struct buf *bp;
890 {
891 	register int unit;
892 	register struct uba_device *ui;
893 	register struct disklabel *lp;
894 	register struct ra_info *ra;
895 	struct partition *pp;
896 	int p;
897 	daddr_t sz, maxsz;
898 
899 	/*
900 	 * Make sure this is a reasonable drive to use.
901 	 */
902 	if ((unit = udaunit(bp->b_dev)) >= NRA ||
903 	    (ui = udadinfo[unit]) == NULL || ui->ui_alive == 0 ||
904 	    (ra = &ra_info[unit])->ra_state == CLOSED) {
905 		bp->b_error = ENXIO;
906 		goto bad;
907 	}
908 
909 	/*
910 	 * If drive is open `raw' or reading label, let it at it.
911 	 */
912 	if (ra->ra_state < OPEN) {
913 		udastrat1(bp);
914 		return;
915 	}
916 	p = udapart(bp->b_dev);
917 	if ((ra->ra_openpart & (1 << p)) == 0)	/* can't happen? */
918 		panic("udastrategy");
919 		/* alternatively, ENODEV */
920 
921 	/*
922 	 * Determine the size of the transfer, and make sure it is
923 	 * within the boundaries of the partition.
924 	 */
925 	pp = &udalabel[unit].d_partitions[p];
926 	maxsz = pp->p_size;
927 	if (pp->p_offset + pp->p_size > ra->ra_dsize)
928 		maxsz = ra->ra_dsize - pp->p_offset;
929 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
930 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
931 		/* if exactly at end of disk, return an EOF */
932 		if (bp->b_blkno == maxsz) {
933 			bp->b_resid = bp->b_bcount;
934 			biodone(bp);
935 			return;
936 		}
937 		/* or truncate if part of it fits */
938 		sz = maxsz - bp->b_blkno;
939 		if (sz <= 0) {
940 			bp->b_error = EINVAL;	/* or hang it up */
941 			goto bad;
942 		}
943 		bp->b_bcount = sz << DEV_BSHIFT;
944 	}
945 	udastrat1(bp);
946 	return;
947 bad:
948 	bp->b_flags |= B_ERROR;
949 	biodone(bp);
950 }
951 
952 /*
953  * Work routine for udastrategy.
954  */
955 udastrat1(bp)
956 	register struct buf *bp;
957 {
958 	register int unit = udaunit(bp->b_dev);
959 	register struct uba_ctlr *um;
960 	register struct buf *dp;
961 	struct uba_device *ui;
962 	int s = spl5();
963 
964 	/*
965 	 * Append the buffer to the drive queue, and if it is not
966 	 * already there, the drive to the controller queue.  (However,
967 	 * if the drive queue is marked to be requeued, we must be
968 	 * awaiting an on line or get unit status command; in this
969 	 * case, leave it off the controller queue.)
970 	 */
971 	um = (ui = udadinfo[unit])->ui_mi;
972 	dp = &udautab[unit];
973 	APPEND(bp, dp, av_forw);
974 	if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) {
975 		APPEND(dp, &um->um_tab, b_forw);
976 		dp->b_active++;
977 	}
978 
979 	/*
980 	 * Start activity on the controller.  Note that unlike other
981 	 * Unibus drivers, we must always do this, not just when the
982 	 * controller is not active.
983 	 */
984 	udastart(um);
985 	splx(s);
986 }
987 
988 /*
989  * Start up whatever transfers we can find.
990  * Note that udastart() must be called at spl5().
991  */
992 udastart(um)
993 	register struct uba_ctlr *um;
994 {
995 	register struct uda_softc *sc = &uda_softc[um->um_ctlr];
996 	register struct buf *bp, *dp;
997 	register struct mscp *mp;
998 	struct uba_device *ui;
999 	struct udadevice *udaddr;
1000 	struct partition *pp;
1001 	int i, sz;
1002 
1003 #ifdef lint
1004 	i = 0; i = i;
1005 #endif
1006 	/*
1007 	 * If it is not running, try (again and again...) to initialise
1008 	 * it.  If it is currently initialising just ignore it for now.
1009 	 */
1010 	if (sc->sc_state != ST_RUN) {
1011 		if (sc->sc_state == ST_IDLE && udainit(um->um_ctlr))
1012 			printf("uda%d: still hung\n", um->um_ctlr);
1013 		return;
1014 	}
1015 
1016 	/*
1017 	 * If um_cmd is nonzero, this controller is on the Unibus
1018 	 * resource wait queue.  It will not help to try more requests;
1019 	 * instead, when the Unibus unblocks and calls udadgo(), we
1020 	 * will call udastart() again.
1021 	 */
1022 	if (um->um_cmd)
1023 		return;
1024 
1025 	sc->sc_flags |= SC_INSTART;
1026 	udaddr = (struct udadevice *) um->um_addr;
1027 
1028 loop:
1029 	/*
1030 	 * Service the drive at the head of the queue.  It may not
1031 	 * need anything, in which case it might be shutting down
1032 	 * in udaclose().
1033 	 */
1034 	if ((dp = um->um_tab.b_actf) == NULL)
1035 		goto out;
1036 	if ((bp = dp->b_actf) == NULL) {
1037 		dp->b_active = 0;
1038 		um->um_tab.b_actf = dp->b_forw;
1039 		if (ra_info[dp - udautab].ra_openpart == 0)
1040 			wakeup((caddr_t)dp); /* finish close protocol */
1041 		goto loop;
1042 	}
1043 
1044 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
1045 		udasaerror(um);
1046 		goto out;
1047 	}
1048 
1049 	/*
1050 	 * Get an MSCP packet, then figure out what to do.  If
1051 	 * we cannot get a command packet, the command ring may
1052 	 * be too small:  We should have at least as many command
1053 	 * packets as credits, for best performance.
1054 	 */
1055 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) {
1056 		if (sc->sc_mi.mi_credits > MSCP_MINCREDITS &&
1057 		    (sc->sc_flags & SC_GRIPED) == 0) {
1058 			log(LOG_NOTICE, "uda%d: command ring too small\n",
1059 				um->um_ctlr);
1060 			sc->sc_flags |= SC_GRIPED;/* complain only once */
1061 		}
1062 		goto out;
1063 	}
1064 
1065 	/*
1066 	 * Bring the drive on line if it is not already.  Get its status
1067 	 * if we do not already have it.  Otherwise just start the transfer.
1068 	 */
1069 	ui = udadinfo[udaunit(bp->b_dev)];
1070 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
1071 		mp->mscp_opcode = M_OP_ONLINE;
1072 		goto common;
1073 	}
1074 	if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) {
1075 		mp->mscp_opcode = M_OP_GETUNITST;
1076 common:
1077 if (ui->ui_flags & UNIT_REQUEUE) panic("udastart");
1078 		/*
1079 		 * Take the drive off the controller queue.  When the
1080 		 * command finishes, make sure the drive is requeued.
1081 		 */
1082 		um->um_tab.b_actf = dp->b_forw;
1083 		dp->b_active = 0;
1084 		ui->ui_flags |= UNIT_REQUEUE;
1085 		mp->mscp_unit = ui->ui_slave;
1086 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
1087 		sc->sc_flags |= SC_STARTPOLL;
1088 #ifdef POLLSTATS
1089 		sc->sc_ncmd++;
1090 #endif
1091 		goto loop;
1092 	}
1093 
1094 	pp = &udalabel[ui->ui_unit].d_partitions[udapart(bp->b_dev)];
1095 	mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
1096 	mp->mscp_unit = ui->ui_slave;
1097 	mp->mscp_seq.seq_lbn = bp->b_blkno + pp->p_offset;
1098 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
1099 	mp->mscp_seq.seq_bytecount = bp->b_blkno + sz > pp->p_size ?
1100 		(pp->p_size - bp->b_blkno) >> DEV_BSHIFT : bp->b_bcount;
1101 	/* mscp_cmdref is filled in by mscp_go() */
1102 
1103 	/*
1104 	 * Drop the packet pointer into the `command' field so udadgo()
1105 	 * can tell what to start.  If ubago returns 1, we can do another
1106 	 * transfer.  If not, um_cmd will still point at mp, so we will
1107 	 * know that we are waiting for resources.
1108 	 */
1109 	um->um_cmd = (int)mp;
1110 	if (ubago(ui))
1111 		goto loop;
1112 
1113 	/*
1114 	 * All done, or blocked in ubago().  If we managed to
1115 	 * issue some commands, start up the beast.
1116 	 */
1117 out:
1118 	if (sc->sc_flags & SC_STARTPOLL) {
1119 #ifdef POLLSTATS
1120 		udastats.cmd[sc->sc_ncmd]++;
1121 		sc->sc_ncmd = 0;
1122 #endif
1123 		i = ((struct udadevice *) um->um_addr)->udaip;
1124 	}
1125 	sc->sc_flags &= ~(SC_INSTART | SC_STARTPOLL);
1126 }
1127 
1128 /*
1129  * Start a transfer.
1130  *
1131  * If we are not called from within udastart(), we must have been
1132  * blocked, so call udastart to do more requests (if any).  If
1133  * this calls us again immediately we will not recurse, because
1134  * that time we will be in udastart().  Clever....
1135  */
1136 udadgo(um)
1137 	register struct uba_ctlr *um;
1138 {
1139 	struct uda_softc *sc = &uda_softc[um->um_ctlr];
1140 	struct mscp *mp = (struct mscp *)um->um_cmd;
1141 
1142 	um->um_tab.b_active++;	/* another transfer going */
1143 
1144 	/*
1145 	 * Fill in the MSCP packet and move the buffer to the
1146 	 * I/O wait queue.  Mark the controller as no longer on
1147 	 * the resource queue, and remember to initiate polling.
1148 	 */
1149 	mp->mscp_seq.seq_buffer = (um->um_ubinfo & 0x3ffff) |
1150 		(UBAI_BDP(um->um_ubinfo) << 24);
1151 	mscp_go(&sc->sc_mi, mp, um->um_ubinfo);
1152 	um->um_cmd = 0;
1153 	um->um_ubinfo = 0;	/* tyke it awye */
1154 	sc->sc_flags |= SC_STARTPOLL;
1155 #ifdef POLLSTATS
1156 	sc->sc_ncmd++;
1157 #endif
1158 	if ((sc->sc_flags & SC_INSTART) == 0)
1159 		udastart(um);
1160 }
1161 
1162 udaiodone(mi, bp, info)
1163 	register struct mscp_info *mi;
1164 	struct buf *bp;
1165 	int info;
1166 {
1167 	register struct uba_ctlr *um = udaminfo[mi->mi_ctlr];
1168 
1169 	um->um_ubinfo = info;
1170 	ubadone(um);
1171 	biodone(bp);
1172 	if (um->um_bdp && mi->mi_wtab.av_forw == &mi->mi_wtab)
1173 		ubarelse(um->um_ubanum, &um->um_bdp);
1174 	um->um_tab.b_active--;	/* another transfer done */
1175 }
1176 
1177 /*
1178  * The error bit was set in the controller status register.  Gripe,
1179  * reset the controller, requeue pending transfers.
1180  */
1181 udasaerror(um)
1182 	register struct uba_ctlr *um;
1183 {
1184 
1185 	printf("uda%d: controller error, sa=%b\n", um->um_ctlr,
1186 		((struct udadevice *) um->um_addr)->udasa, udasr_bits);
1187 	mscp_requeue(&uda_softc[um->um_ctlr].sc_mi);
1188 	(void) udainit(um->um_ctlr);
1189 }
1190 
1191 /*
1192  * Interrupt routine.  Depending on the state of the controller,
1193  * continue initialisation, or acknowledge command and response
1194  * interrupts, and process responses.
1195  */
1196 udaintr(ctlr)
1197 	int ctlr;
1198 {
1199 	register struct uba_ctlr *um = udaminfo[ctlr];
1200 	register struct uda_softc *sc = &uda_softc[ctlr];
1201 	register struct udadevice *udaddr = (struct udadevice *) um->um_addr;
1202 	register struct uda *ud;
1203 	register struct mscp *mp;
1204 	register int i;
1205 
1206 #ifdef VAX630
1207 	(void) spl5();		/* Qbus interrupt protocol is odd */
1208 #endif
1209 	sc->sc_wticks = 0;	/* reset interrupt watchdog */
1210 
1211 	/*
1212 	 * Combinations during steps 1, 2, and 3: STEPnMASK
1213 	 * corresponds to which bits should be tested;
1214 	 * STEPnGOOD corresponds to the pattern that should
1215 	 * appear after the interrupt from STEPn initialisation.
1216 	 * All steps test the bits in ALLSTEPS.
1217 	 */
1218 #define	ALLSTEPS	(UDA_ERR|UDA_STEP4|UDA_STEP3|UDA_STEP2|UDA_STEP1)
1219 
1220 #define	STEP1MASK	(ALLSTEPS | UDA_IE | UDA_NCNRMASK)
1221 #define	STEP1GOOD	(UDA_STEP2 | UDA_IE | (NCMDL2 << 3) | NRSPL2)
1222 
1223 #define	STEP2MASK	(ALLSTEPS | UDA_IE | UDA_IVECMASK)
1224 #define	STEP2GOOD	(UDA_STEP3 | UDA_IE | (sc->sc_ivec >> 2))
1225 
1226 #define	STEP3MASK	ALLSTEPS
1227 #define	STEP3GOOD	UDA_STEP4
1228 
1229 	switch (sc->sc_state) {
1230 
1231 	case ST_IDLE:
1232 		/*
1233 		 * Ignore unsolicited interrupts.
1234 		 */
1235 		log(LOG_WARNING, "uda%d: stray intr\n", ctlr);
1236 		return;
1237 
1238 	case ST_STEP1:
1239 		/*
1240 		 * Begin step two initialisation.
1241 		 */
1242 		if ((udaddr->udasa & STEP1MASK) != STEP1GOOD) {
1243 			i = 1;
1244 initfailed:
1245 			printf("uda%d: init step %d failed, sa=%b\n",
1246 				ctlr, i, udaddr->udasa, udasr_bits);
1247 			sc->sc_state = ST_IDLE;
1248 			if (sc->sc_flags & SC_DOWAKE) {
1249 				sc->sc_flags &= ~SC_DOWAKE;
1250 				wakeup((caddr_t) sc);
1251 			}
1252 			return;
1253 		}
1254 		udaddr->udasa = (int) &sc->sc_uda->uda_ca.ca_rspdsc[0] |
1255 			(cpu == VAX_780 || cpu == VAX_8600 ? UDA_PI : 0);
1256 		sc->sc_state = ST_STEP2;
1257 		return;
1258 
1259 	case ST_STEP2:
1260 		/*
1261 		 * Begin step 3 initialisation.
1262 		 */
1263 		if ((udaddr->udasa & STEP2MASK) != STEP2GOOD) {
1264 			i = 2;
1265 			goto initfailed;
1266 		}
1267 		udaddr->udasa = ((int) &sc->sc_uda->uda_ca.ca_rspdsc[0]) >> 16;
1268 		sc->sc_state = ST_STEP3;
1269 		return;
1270 
1271 	case ST_STEP3:
1272 		/*
1273 		 * Set controller characteristics (finish initialisation).
1274 		 */
1275 		if ((udaddr->udasa & STEP3MASK) != STEP3GOOD) {
1276 			i = 3;
1277 			goto initfailed;
1278 		}
1279 		i = udaddr->udasa & 0xff;
1280 		if (i != sc->sc_micro) {
1281 			sc->sc_micro = i;
1282 			printf("uda%d: version %d model %d\n",
1283 				ctlr, i & 0xf, i >> 4);
1284 		}
1285 
1286 		/*
1287 		 * Present the burst size, then remove it.  Why this
1288 		 * should be done this way, I have no idea.
1289 		 *
1290 		 * Note that this assumes udaburst[ctlr] > 0.
1291 		 */
1292 		udaddr->udasa = UDA_GO | (udaburst[ctlr] - 1) << 2;
1293 		udaddr->udasa = UDA_GO;
1294 		printf("uda%d: DMA burst size set to %d\n",
1295 			ctlr, udaburst[ctlr]);
1296 
1297 		udainitds(ctlr);	/* initialise data structures */
1298 
1299 		/*
1300 		 * Before we can get a command packet, we need some
1301 		 * credits.  Fake some up to keep mscp_getcp() happy,
1302 		 * get a packet, and cancel all credits (the right
1303 		 * number should come back in the response to the
1304 		 * SCC packet).
1305 		 */
1306 		sc->sc_mi.mi_credits = MSCP_MINCREDITS + 1;
1307 		mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT);
1308 		if (mp == NULL)	/* `cannot happen' */
1309 			panic("udaintr");
1310 		sc->sc_mi.mi_credits = 0;
1311 		mp->mscp_opcode = M_OP_SETCTLRC;
1312 		mp->mscp_unit = 0;
1313 		mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC |
1314 			M_CF_THIS;
1315 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
1316 		i = udaddr->udaip;
1317 		sc->sc_state = ST_SETCHAR;
1318 		return;
1319 
1320 	case ST_SETCHAR:
1321 	case ST_RUN:
1322 		/*
1323 		 * Handle Set Ctlr Characteristics responses and operational
1324 		 * responses (via mscp_dorsp).
1325 		 */
1326 		break;
1327 
1328 	default:
1329 		printf("uda%d: driver bug, state %d\n", ctlr, sc->sc_state);
1330 		panic("udastate");
1331 	}
1332 
1333 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
1334 		udasaerror(um);
1335 		return;
1336 	}
1337 
1338 	ud = &uda[ctlr];
1339 
1340 	/*
1341 	 * Handle buffer purge requests.
1342 	 * I have never seen these to work usefully, thus the log().
1343 	 */
1344 	if (ud->uda_ca.ca_bdp) {
1345 		log(LOG_DEBUG, "uda%d: purge bdp %d\n",
1346 			ctlr, ud->uda_ca.ca_bdp);
1347 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
1348 		ud->uda_ca.ca_bdp = 0;
1349 		udaddr->udasa = 0;	/* signal purge complete */
1350 	}
1351 
1352 	/*
1353 	 * Check for response and command ring transitions.
1354 	 */
1355 	if (ud->uda_ca.ca_rspint) {
1356 		ud->uda_ca.ca_rspint = 0;
1357 		mscp_dorsp(&sc->sc_mi);
1358 	}
1359 	if (ud->uda_ca.ca_cmdint) {
1360 		ud->uda_ca.ca_cmdint = 0;
1361 		MSCP_DOCMD(&sc->sc_mi);
1362 	}
1363 	udastart(um);
1364 }
1365 
1366 #ifndef GENERIC_RAW
1367 struct buf rudabuf[NRA];
1368 
1369 /*
1370  * Read and write.
1371  */
1372 udaread(dev, uio)
1373 	dev_t dev;
1374 	struct uio *uio;
1375 {
1376 
1377 	return (physio(udastrategy, &rudabuf[udaunit(dev)], dev, B_READ,
1378 		minphys, uio));
1379 }
1380 
1381 udawrite(dev, uio)
1382 	dev_t dev;
1383 	struct uio *uio;
1384 {
1385 
1386 	return (physio(udastrategy, &rudabuf[udaunit(dev)], dev, B_WRITE,
1387 		minphys, uio));
1388 }
1389 #endif /* GENERIC_RAW */
1390 
1391 /*
1392  * Initialise the various data structures that control the UDA50.
1393  */
1394 udainitds(ctlr)
1395 	int ctlr;
1396 {
1397 	register struct uda *ud = &uda[ctlr];
1398 	register struct uda *uud = uda_softc[ctlr].sc_uda;
1399 	register struct mscp *mp;
1400 	register int i;
1401 
1402 	for (i = 0, mp = ud->uda_rsp; i < NRSP; i++, mp++) {
1403 		ud->uda_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
1404 			(long)&uud->uda_rsp[i].mscp_cmdref;
1405 		mp->mscp_addr = &ud->uda_ca.ca_rspdsc[i];
1406 		mp->mscp_msglen = MSCP_MSGLEN;
1407 	}
1408 	for (i = 0, mp = ud->uda_cmd; i < NCMD; i++, mp++) {
1409 		ud->uda_ca.ca_cmddsc[i] = MSCP_INT |
1410 			(long)&uud->uda_cmd[i].mscp_cmdref;
1411 		mp->mscp_addr = &ud->uda_ca.ca_cmddsc[i];
1412 		mp->mscp_msglen = MSCP_MSGLEN;
1413 	}
1414 }
1415 
1416 /*
1417  * Handle an error datagram.  All we do now is decode it.
1418  */
1419 udadgram(mi, mp)
1420 	struct mscp_info *mi;
1421 	struct mscp *mp;
1422 {
1423 
1424 	mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp);
1425 }
1426 
1427 /*
1428  * The Set Controller Characteristics command finished.
1429  * Record the new state of the controller.
1430  */
1431 udactlrdone(mi, mp)
1432 	register struct mscp_info *mi;
1433 	struct mscp *mp;
1434 {
1435 	register struct uda_softc *sc = &uda_softc[mi->mi_ctlr];
1436 
1437 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
1438 		sc->sc_state = ST_RUN;
1439 	else {
1440 		printf("uda%d: SETCTLRC failed: ",
1441 			mi->mi_ctlr, mp->mscp_status);
1442 		mscp_printevent(mp);
1443 		sc->sc_state = ST_IDLE;
1444 	}
1445 	if (sc->sc_flags & SC_DOWAKE) {
1446 		sc->sc_flags &= ~SC_DOWAKE;
1447 		wakeup((caddr_t)sc);
1448 	}
1449 }
1450 
1451 /*
1452  * Received a response from an as-yet unconfigured drive.  Configure it
1453  * in, if possible.
1454  */
1455 udaunconf(mi, mp)
1456 	struct mscp_info *mi;
1457 	register struct mscp *mp;
1458 {
1459 
1460 	/*
1461 	 * If it is a slave response, copy it to udaslavereply for
1462 	 * udaslave() to look at.
1463 	 */
1464 	if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) &&
1465 	    (uda_softc[mi->mi_ctlr].sc_flags & SC_INSLAVE) != 0) {
1466 		udaslavereply = *mp;
1467 		return (MSCP_DONE);
1468 	}
1469 
1470 	/*
1471 	 * Otherwise, it had better be an available attention response.
1472 	 */
1473 	if (mp->mscp_opcode != M_OP_AVAILATTN)
1474 		return (MSCP_FAILED);
1475 
1476 	/* do what autoconf does */
1477 	return (MSCP_FAILED);	/* not yet, arwhite, not yet */
1478 }
1479 
1480 /*
1481  * A drive came on line.  Check its type and size.  Return DONE if
1482  * we think the drive is truly on line.  In any case, awaken anyone
1483  * sleeping on the drive on-line-ness.
1484  */
1485 udaonline(ui, mp)
1486 	register struct uba_device *ui;
1487 	struct mscp *mp;
1488 {
1489 	register struct ra_info *ra = &ra_info[ui->ui_unit];
1490 
1491 	wakeup((caddr_t)&ui->ui_flags);
1492 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1493 		printf("uda%d: attempt to bring ra%d on line failed: ",
1494 			ui->ui_ctlr, ui->ui_unit);
1495 		mscp_printevent(mp);
1496 		ra->ra_state = CLOSED;
1497 		return (MSCP_FAILED);
1498 	}
1499 
1500 	ra->ra_state = OPENRAW;
1501 	ra->ra_dsize = (daddr_t)mp->mscp_onle.onle_unitsize;
1502 	printf("ra%d: uda%d, unit %d, size = %d sectors\n", ui->ui_unit,
1503 		ui->ui_ctlr, mp->mscp_unit, ra->ra_dsize);
1504 	/* can now compute ncyl */
1505 	ra->ra_geom.rg_ncyl = ra->ra_dsize / ra->ra_geom.rg_ntracks /
1506 		ra->ra_geom.rg_nsectors;
1507 	return (MSCP_DONE);
1508 }
1509 
1510 /*
1511  * We got some (configured) unit's status.  Return DONE if it succeeded.
1512  */
1513 udagotstatus(ui, mp)
1514 	register struct uba_device *ui;
1515 	register struct mscp *mp;
1516 {
1517 
1518 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1519 		printf("uda%d: attempt to get status for ra%d failed: ",
1520 			ui->ui_ctlr, ui->ui_unit);
1521 		mscp_printevent(mp);
1522 		return (MSCP_FAILED);
1523 	}
1524 	/* record for (future) bad block forwarding and whatever else */
1525 	uda_rasave(ui->ui_unit, mp, 1);
1526 	return (MSCP_DONE);
1527 }
1528 
1529 /*
1530  * A transfer failed.  We get a chance to fix or restart it.
1531  * Need to write the bad block forwaring code first....
1532  */
1533 /*ARGSUSED*/
1534 udaioerror(ui, mp, bp)
1535 	register struct uba_device *ui;
1536 	register struct mscp *mp;
1537 	struct buf *bp;
1538 {
1539 
1540 	if (mp->mscp_flags & M_EF_BBLKR) {
1541 		/*
1542 		 * A bad block report.  Eventually we will
1543 		 * restart this transfer, but for now, just
1544 		 * log it and give up.
1545 		 */
1546 		log(LOG_ERR, "ra%d: bad block report: %d%s\n",
1547 			ui->ui_unit, mp->mscp_seq.seq_lbn,
1548 			mp->mscp_flags & M_EF_BBLKU ? " + others" : "");
1549 	} else {
1550 		/*
1551 		 * What the heck IS a `serious exception' anyway?
1552 		 * IT SURE WOULD BE NICE IF DEC SOLD DOCUMENTATION
1553 		 * FOR THEIR OWN CONTROLLERS.
1554 		 */
1555 		if (mp->mscp_flags & M_EF_SEREX)
1556 			log(LOG_ERR, "ra%d: serious exception reported\n",
1557 				ui->ui_unit);
1558 	}
1559 	return (MSCP_FAILED);
1560 }
1561 
1562 /*
1563  * A replace operation finished.
1564  */
1565 /*ARGSUSED*/
1566 udareplace(ui, mp)
1567 	struct uba_device *ui;
1568 	struct mscp *mp;
1569 {
1570 
1571 	panic("udareplace");
1572 }
1573 
1574 /*
1575  * A bad block related operation finished.
1576  */
1577 /*ARGSUSED*/
1578 udabb(ui, mp, bp)
1579 	struct uba_device *ui;
1580 	struct mscp *mp;
1581 	struct buf *bp;
1582 {
1583 
1584 	panic("udabb");
1585 }
1586 
1587 
1588 /*
1589  * I/O controls.
1590  */
1591 udaioctl(dev, cmd, data, flag)
1592 	dev_t dev;
1593 	int cmd;
1594 	caddr_t data;
1595 	int flag;
1596 {
1597 	register int unit = udaunit(dev);
1598 	register struct disklabel *lp;
1599 	int error = 0;
1600 
1601 	lp = &udalabel[unit];
1602 
1603 	switch (cmd) {
1604 
1605 	case DIOCGDINFO:
1606 		*(struct disklabel *)data = *lp;
1607 		break;
1608 
1609 	case DIOCGPART:
1610 		((struct partinfo *)data)->disklab = lp;
1611 		((struct partinfo *)data)->part =
1612 		    &lp->d_partitions[udapart(dev)];
1613 		break;
1614 
1615 	case DIOCSDINFO:
1616 		if ((flag & FWRITE) == 0)
1617 			error = EBADF;
1618 		else
1619 			error = setdisklabel(lp, (struct disklabel *)data,
1620 			    ra_info[unit].ra_openpart);
1621 		break;
1622 
1623 	case DIOCWDINFO:
1624 		if ((flag & FWRITE) == 0)
1625 			error = EBADF;
1626 		else if ((error = setdisklabel(lp, (struct disklabel *)data,
1627 			    ra_info[unit].ra_openpart)) == 0)
1628 			error = writedisklabel(dev, udastrategy, lp);
1629 		break;
1630 
1631 #ifdef notyet
1632 	case UDAIOCREPLACE:
1633 		/*
1634 		 * Initiate bad block replacement for the given LBN.
1635 		 * (Should we allow modifiers?)
1636 		 */
1637 		error = EOPNOTSUPP;
1638 		break;
1639 
1640 	case UDAIOCGMICRO:
1641 		/*
1642 		 * Return the microcode revision for the UDA50 running
1643 		 * this drive.
1644 		 */
1645 		*(int *) data = uda_softc[uddinfo[unit]->ui_ctlr].sc_micro;
1646 		break;
1647 #endif
1648 
1649 	default:
1650 		error = ENOTTY;
1651 		break;
1652 	}
1653 	return (error);
1654 }
1655 
1656 /*
1657  * A Unibus reset has occurred on UBA uban.  Reinitialise the controller(s)
1658  * on that Unibus, and requeue outstanding I/O.
1659  */
1660 udareset(uban)
1661 	int uban;
1662 {
1663 	register struct uba_ctlr *um;
1664 	register struct uda_softc *sc;
1665 	register int ctlr;
1666 
1667 	for (ctlr = 0, sc = uda_softc; ctlr < NUDA; ctlr++, sc++) {
1668 		if ((um = udaminfo[ctlr]) == NULL || um->um_ubanum != uban ||
1669 		    um->um_alive == 0)
1670 			continue;
1671 		printf(" uda%d", ctlr);
1672 
1673 		/*
1674 		 * Our BDP (if any) is gone; our command (if any) is
1675 		 * flushed; the device is no longer mapped; and the
1676 		 * UDA50 is not yet initialised.
1677 		 */
1678 		if (um->um_bdp) {
1679 			printf("<%d>", UBAI_BDP(um->um_bdp));
1680 			um->um_bdp = 0;
1681 		}
1682 		um->um_ubinfo = 0;
1683 		um->um_cmd = 0;
1684 		sc->sc_flags &= ~SC_MAPPED;
1685 		sc->sc_state = ST_IDLE;
1686 
1687 		/* reset queues and requeue pending transfers */
1688 		mscp_requeue(&sc->sc_mi);
1689 
1690 		/*
1691 		 * If it fails to initialise we will notice later and
1692 		 * try again (and again...).  Do not call udastart()
1693 		 * here; it will be done after the controller finishes
1694 		 * initialisation.
1695 		 */
1696 		if (udainit(ctlr))
1697 			printf(" (hung)");
1698 	}
1699 }
1700 
1701 /*
1702  * Watchdog timer:  If the controller is active, and no interrupts
1703  * have occurred for 30 seconds, assume it has gone away.
1704  */
1705 udawatch()
1706 {
1707 	register int i;
1708 	register struct uba_ctlr *um;
1709 	register struct uda_softc *sc;
1710 
1711 	timeout(udawatch, (caddr_t) 0, hz);	/* every second */
1712 	for (i = 0, sc = uda_softc; i < NUDA; i++, sc++) {
1713 		if ((um = udaminfo[i]) == 0 || !um->um_alive)
1714 			continue;
1715 		if (sc->sc_state == ST_IDLE)
1716 			continue;
1717 		if (sc->sc_state == ST_RUN && !um->um_tab.b_active)
1718 			sc->sc_wticks = 0;
1719 		else if (++sc->sc_wticks >= 30) {
1720 			sc->sc_wticks = 0;
1721 			printf("uda%d: lost interrupt\n", i);
1722 			ubareset(um->um_ubanum);
1723 		}
1724 	}
1725 }
1726 
1727 /*
1728  * Do a panic dump.  We set up the controller for one command packet
1729  * and one response packet, for which we use `struct uda1'.
1730  */
1731 struct	uda1 {
1732 	struct	uda1ca uda1_ca;	/* communications area */
1733 	struct	mscp uda1_rsp;	/* response packet */
1734 	struct	mscp uda1_cmd;	/* command packet */
1735 } uda1;
1736 
1737 #define	DBSIZE	32		/* dump 16K at a time */
1738 
1739 udadump(dev)
1740 	dev_t dev;
1741 {
1742 	struct udadevice *udaddr;
1743 	struct uda1 *ud_ubaddr;
1744 	char *start;
1745 	int num, blk, unit, maxsz, blkoff, reg;
1746 	struct partition *pp;
1747 	register struct uba_regs *uba;
1748 	register struct uba_device *ui;
1749 	register struct uda1 *ud;
1750 	register struct pte *io;
1751 	register int i;
1752 
1753 	/*
1754 	 * Make sure the device is a reasonable place on which to dump.
1755 	 */
1756 	unit = udaunit(dev);
1757 	if (unit >= NRA)
1758 		return (ENXIO);
1759 #define	phys(cast, addr)	((cast) ((int) addr & 0x7fffffff))
1760 	ui = phys(struct uba_device *, udadinfo[unit]);
1761 	if (ui == NULL || ui->ui_alive == 0)
1762 		return (ENXIO);
1763 
1764 	/*
1765 	 * Find and initialise the UBA; get the physical address of the
1766 	 * device registers, and of communications area and command and
1767 	 * response packet.
1768 	 */
1769 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
1770 	ubainit(uba);
1771 	udaddr = (struct udadevice *)ui->ui_physaddr;
1772 	ud = phys(struct uda1 *, &uda1);
1773 
1774 	/*
1775 	 * Map the ca+packets into Unibus I/O space so the UDA50 can get
1776 	 * at them.  Use the registers at the end of the Unibus map (since
1777 	 * we will use the registers at the beginning to map the memory
1778 	 * we are dumping).
1779 	 */
1780 	num = btoc(sizeof(struct uda1)) + 1;
1781 	reg = NUBMREG - num;
1782 	io = &uba->uba_map[reg];
1783 	for (i = 0; i < num; i++)
1784 		*(int *)io++ = UBAMR_MRV | (btop(ud) + i);
1785 	ud_ubaddr = (struct uda1 *)(((int)ud & PGOFSET) | (reg << 9));
1786 
1787 	/*
1788 	 * Initialise the controller, with one command and one response
1789 	 * packet.
1790 	 */
1791 	udaddr->udaip = 0;
1792 	if (udadumpwait(udaddr, UDA_STEP1))
1793 		return (EFAULT);
1794 	udaddr->udasa = UDA_ERR;
1795 	if (udadumpwait(udaddr, UDA_STEP2))
1796 		return (EFAULT);
1797 	udaddr->udasa = (int)&ud_ubaddr->uda1_ca.ca_rspdsc;
1798 	if (udadumpwait(udaddr, UDA_STEP3))
1799 		return (EFAULT);
1800 	udaddr->udasa = ((int)&ud_ubaddr->uda1_ca.ca_rspdsc) >> 16;
1801 	if (udadumpwait(udaddr, UDA_STEP4))
1802 		return (EFAULT);
1803 	uda_softc[ui->ui_ctlr].sc_micro = udaddr->udasa & 0xff;
1804 	udaddr->udasa = UDA_GO;
1805 
1806 	/*
1807 	 * Set up the command and response descriptor, then set the
1808 	 * controller characteristics and bring the drive on line.
1809 	 * Note that all uninitialised locations in uda1_cmd are zero.
1810 	 */
1811 	ud->uda1_ca.ca_rspdsc = (long)&ud_ubaddr->uda1_rsp.mscp_cmdref;
1812 	ud->uda1_ca.ca_cmddsc = (long)&ud_ubaddr->uda1_cmd.mscp_cmdref;
1813 	/* ud->uda1_cmd.mscp_sccc.sccc_ctlrflags = 0; */
1814 	/* ud->uda1_cmd.mscp_sccc.sccc_version = 0; */
1815 	if (udadumpcmd(M_OP_SETCTLRC, ud, ui))
1816 		return (EFAULT);
1817 	ud->uda1_cmd.mscp_unit = ui->ui_slave;
1818 	if (udadumpcmd(M_OP_ONLINE, ud, ui))
1819 		return (EFAULT);
1820 
1821 	pp = phys(struct partition *,
1822 	    &udalabel[unit].d_partitions[udapart(dev)]);
1823 	maxsz = pp->p_size;
1824 	blkoff = pp->p_offset;
1825 
1826 	/*
1827 	 * Dump all of physical memory, or as much as will fit in the
1828 	 * space provided.
1829 	 */
1830 	start = 0;
1831 	num = maxfree;
1832 	if (dumplo < 0)
1833 		return (EINVAL);
1834 	if (dumplo + num >= maxsz)
1835 		num = maxsz - dumplo;
1836 	blkoff += dumplo;
1837 
1838 	/*
1839 	 * Write out memory, DBSIZE pages at a time.
1840 	 * N.B.: this code depends on the fact that the sector
1841 	 * size == the page size.
1842 	 */
1843 	while (num > 0) {
1844 		blk = num > DBSIZE ? DBSIZE : num;
1845 		io = uba->uba_map;
1846 		/*
1847 		 * Map in the pages to write, leaving an invalid entry
1848 		 * at the end to guard against wild Unibus transfers.
1849 		 * Then do the write.
1850 		 */
1851 		for (i = 0; i < blk; i++)
1852 			*(int *) io++ = UBAMR_MRV | (btop(start) + i);
1853 		*(int *) io = 0;
1854 		ud->uda1_cmd.mscp_unit = ui->ui_slave;
1855 		ud->uda1_cmd.mscp_seq.seq_lbn = btop(start) + blkoff;
1856 		ud->uda1_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT;
1857 		if (udadumpcmd(M_OP_WRITE, ud, ui))
1858 			return (EIO);
1859 		start += blk << PGSHIFT;
1860 		num -= blk;
1861 	}
1862 	return (0);		/* made it! */
1863 }
1864 
1865 /*
1866  * Wait for some of the bits in `bits' to come on.  If the error bit
1867  * comes on, or ten seconds pass without response, return true (error).
1868  */
1869 udadumpwait(udaddr, bits)
1870 	register struct udadevice *udaddr;
1871 	register int bits;
1872 {
1873 	register int timo = todr() + 1000;
1874 
1875 	while ((udaddr->udasa & bits) == 0) {
1876 		if (udaddr->udasa & UDA_ERR) {
1877 			printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
1878 			return (1);
1879 		}
1880 		if (todr() >= timo) {
1881 			printf("timeout\ndump ");
1882 			return (1);
1883 		}
1884 	}
1885 	return (0);
1886 }
1887 
1888 /*
1889  * Feed a command to the UDA50, wait for its response, and return
1890  * true iff something went wrong.
1891  */
1892 udadumpcmd(op, ud, ui)
1893 	int op;
1894 	register struct uda1 *ud;
1895 	struct uba_device *ui;
1896 {
1897 	register struct udadevice *udaddr;
1898 	register int n;
1899 #define mp (&ud->uda1_rsp)
1900 
1901 	udaddr = (struct udadevice *) ui->ui_physaddr;
1902 	ud->uda1_cmd.mscp_opcode = op;
1903 	ud->uda1_cmd.mscp_msglen = MSCP_MSGLEN;
1904 	ud->uda1_rsp.mscp_msglen = MSCP_MSGLEN;
1905 	ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
1906 	ud->uda1_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT;
1907 	if (udaddr->udasa & UDA_ERR) {
1908 		printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
1909 		return (1);
1910 	}
1911 	n = udaddr->udaip;
1912 	n = todr() + 1000;
1913 	for (;;) {
1914 		if (todr() > n) {
1915 			printf("timeout\ndump ");
1916 			return (1);
1917 		}
1918 		if (ud->uda1_ca.ca_cmdint)
1919 			ud->uda1_ca.ca_cmdint = 0;
1920 		if (ud->uda1_ca.ca_rspint == 0)
1921 			continue;
1922 		ud->uda1_ca.ca_rspint = 0;
1923 		if (mp->mscp_opcode == (op | M_OP_END))
1924 			break;
1925 		printf("\n");
1926 		switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
1927 
1928 		case MSCPT_SEQ:
1929 			printf("sequential");
1930 			break;
1931 
1932 		case MSCPT_DATAGRAM:
1933 			mscp_decodeerror("uda", ui->ui_ctlr, mp);
1934 			printf("datagram");
1935 			break;
1936 
1937 		case MSCPT_CREDITS:
1938 			printf("credits");
1939 			break;
1940 
1941 		case MSCPT_MAINTENANCE:
1942 			printf("maintenance");
1943 			break;
1944 
1945 		default:
1946 			printf("unknown (type 0x%x)",
1947 				MSCP_MSGTYPE(mp->mscp_msgtc));
1948 			break;
1949 		}
1950 		printf(" ignored\ndump ");
1951 		ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
1952 	}
1953 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1954 		printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op,
1955 			mp->mscp_opcode, mp->mscp_status);
1956 		return (1);
1957 	}
1958 	return (0);
1959 #undef mp
1960 }
1961 
1962 /*
1963  * Return the size of a partition, if known, or -1 if not.
1964  */
1965 udasize(dev)
1966 	dev_t dev;
1967 {
1968 	register int unit = udaunit(dev);
1969 	register struct uba_device *ui;
1970 	register struct size *st;
1971 
1972 	if (unit >= NRA || (ui = udadinfo[unit]) == NULL ||
1973 	    ui->ui_alive == 0 || (ui->ui_flags & UNIT_ONLINE) == 0 ||
1974 	    ra_info[unit].ra_state != OPEN)
1975 		return (-1);
1976 	return ((int)udalabel[unit].d_partitions[udapart(dev)].p_size);
1977 }
1978 
1979 #ifdef COMPAT_42
1980 /*
1981  * Tables mapping unlabelled drives.
1982  */
1983 struct size {
1984 	daddr_t nblocks;
1985 	daddr_t blkoff;
1986 } ra25_sizes[8] = {
1987 	15884,	0,		/* A=blk 0 thru 15883 */
1988 	10032,	15884,		/* B=blk 15884 thru 49323 */
1989 	-1,	0,		/* C=blk 0 thru end */
1990 	0,	0,		/* D=blk 340670 thru 356553 */
1991 	0,	0,		/* E=blk 356554 thru 412489 */
1992 	0,	0,		/* F=blk 412490 thru end */
1993 	-1,	25916,		/* G=blk 49324 thru 131403 */
1994 	0,	0,		/* H=blk 131404 thru end */
1995 }, rx50_sizes[8] = {
1996 	800,	0,		/* A=blk 0 thru 799 */
1997 	0,	0,
1998 	-1,	0,		/* C=blk 0 thru end */
1999 	0,	0,
2000 	0,	0,
2001 	0,	0,
2002 	0,	0,
2003 	0,	0,
2004 }, rd52_sizes[8] = {
2005 	15884,	0,		/* A=blk 0 thru 15883 */
2006 	9766,	15884,		/* B=blk 15884 thru 25649 */
2007 	-1,	0,		/* C=blk 0 thru end */
2008 	0,	0,		/* D=unused */
2009 	0,	0,		/* E=unused */
2010 	0,	0,		/* F=unused */
2011 	-1,	25650,		/* G=blk 25650 thru end */
2012 	0,	0,		/* H=unused */
2013 }, rd53_sizes[8] = {
2014 	15884,	0,		/* A=blk 0 thru 15883 */
2015 	33440,	15884,		/* B=blk 15884 thru 49323 */
2016 	-1,	0,		/* C=blk 0 thru end */
2017 	0,	0,		/* D=unused */
2018 	33440,	0,		/* E=blk 0 thru 33439 */
2019 	-1,	33440,		/* F=blk 33440 thru end */
2020 	-1,	49324,		/* G=blk 49324 thru end */
2021 	-1,	15884,		/* H=blk 15884 thru end */
2022 }, ra60_sizes[8] = {
2023 	15884,	0,		/* A=sectors 0 thru 15883 */
2024 	33440,	15884,		/* B=sectors 15884 thru 49323 */
2025 	400176,	0,		/* C=sectors 0 thru 400175 */
2026 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
2027 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
2028 	350852,	49324,		/* F=sectors 49324 thru 400175 */
2029 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
2030 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
2031 }, ra80_sizes[8] = {
2032 	15884,	0,		/* A=sectors 0 thru 15883 */
2033 	33440,	15884,		/* B=sectors 15884 thru 49323 */
2034 	242606,	0,		/* C=sectors 0 thru 242605 */
2035 	0,	0,		/* D=unused */
2036 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
2037 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
2038 	192696,	49910,		/* G=sectors 49910 thru 242605 */
2039 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
2040 }, ra81_sizes[8] ={
2041 /*
2042  * These are the new standard partition sizes for ra81's.
2043  * An RA_COMPAT system is compiled with D, E, and F corresponding
2044  * to the 4.2 partitions for G, H, and F respectively.
2045  */
2046 #ifndef	UCBRA
2047 	15884,	0,		/* A=sectors 0 thru 15883 */
2048 	66880,	16422,		/* B=sectors 16422 thru 83301 */
2049 	891072,	0,		/* C=sectors 0 thru 891071 */
2050 #ifdef RA_COMPAT
2051 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
2052 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
2053 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
2054 #else
2055 	15884,	375564,		/* D=sectors 375564 thru 391447 */
2056 	307200,	391986,		/* E=sectors 391986 thru 699185 */
2057 	191352,	699720,		/* F=sectors 699720 thru 891071 */
2058 #endif RA_COMPAT
2059 	515508,	375564,		/* G=sectors 375564 thru 891071 */
2060 	291346,	83538,		/* H=sectors 83538 thru 374883 */
2061 
2062 /*
2063  * These partitions correspond to the sizes used by sites at Berkeley,
2064  * and by those sites that have received copies of the Berkeley driver
2065  * with deltas 6.2 or greater (11/15/83).
2066  */
2067 #else UCBRA
2068 
2069 	15884,	0,		/* A=sectors 0 thru 15883 */
2070 	33440,	15884,		/* B=sectors 15884 thru 49323 */
2071 	891072,	0,		/* C=sectors 0 thru 891071 */
2072 	15884,	242606,		/* D=sectors 242606 thru 258489 */
2073 	307200,	258490,		/* E=sectors 258490 thru 565689 */
2074 	325382,	565690,		/* F=sectors 565690 thru 891071 */
2075 	648466,	242606,		/* G=sectors 242606 thru 891071 */
2076 	193282,	49324,		/* H=sectors 49324 thru 242605 */
2077 
2078 #endif UCBRA
2079 };
2080 
2081 /*
2082  * Drive type index decoding table.  `ut_name' is null iff the
2083  * type is not known.
2084  */
2085 struct	udatypes {
2086 	char	*ut_name;	/* drive type name */
2087 	struct	size *ut_sizes;	/* partition tables */
2088 	int	ut_nsectors, ut_ntracks, ut_ncylinders;
2089 } udatypes[] = {
2090 	NULL,		NULL,
2091 		0, 0, 0,
2092 	"ra80",		ra80_sizes,	/* 1 = ra80 */
2093 		31, 14, 559,
2094 	"rc25-removable", ra25_sizes,	/* 2 = rc25-r */
2095 		42, 4, 302,
2096 	"rc25-fixed",	ra25_sizes,	/* 3 = rc25-f */
2097 		42, 4, 302,
2098 	"ra60",		ra60_sizes,	/* 4 = ra60 */
2099 		42, 4, 2382,
2100 	"ra81",		ra81_sizes,	/* 5 = ra81 */
2101 		51, 14, 1248,
2102 	NULL,		NULL,		/* 6 = ? */
2103 		0, 0, 0,
2104 	"rx50",		rx50_sizes,	/* 7 = rx50 */
2105 		10, 1, 80,
2106 	"rd52",		rd52_sizes,	/* 8 = rd52 */
2107 		18, 7, 480,
2108 	"rd53",		rd53_sizes,	/* 9 = rd53 */
2109 		18, 8, 963,
2110 };
2111 
2112 #define NTYPES (sizeof(udatypes) / sizeof(*udatypes))
2113 
2114 udamaptype(unit, lp)
2115 	int unit;
2116 	register struct disklabel *lp;
2117 {
2118 	register struct udatypes *ut;
2119 	register struct size *sz;
2120 	register struct partition *pp;
2121 	register char *p;
2122 	register int i;
2123 	register struct ra_info *ra = &ra_info[unit];
2124 
2125 	lp->d_secsize = 512;
2126 	lp->d_secperunit = ra->ra_dsize;
2127 	if ((u_long)ra->ra_type >= NTYPES) {
2128 		printf("ra%d: don't have a partition table for", unit);
2129 		mscp_printmedia(ra->ra_mediaid);
2130 		lp->d_nsectors = ra->ra_geom.rg_nsectors;
2131 		lp->d_ntracks = ra->ra_geom.rg_ntracks;
2132 		lp->d_ncylinders = ra->ra_geom.rg_ncyl;
2133 		printf(";\nusing (t,s,c)=(%d,%d,%d)\n", lp->d_nsectors,
2134 			lp->d_ntracks, lp->d_ncylinders);
2135 		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
2136 		lp->d_typename[0] = 'r';
2137 		lp->d_typename[1] = 'a';
2138 		lp->d_typename[2] = '?';
2139 		lp->d_typename[3] = '?';
2140 		lp->d_typename[4] = 0;
2141 		lp->d_npartitions = 1;
2142 		lp->d_partitions[0].p_offset = 0;
2143 		lp->d_partitions[0].p_size = lp->d_secperunit;
2144 		return (0);
2145 	}
2146 	ut = &udatypes[ra->ra_type];
2147 	p = ut->ut_name;
2148 	for (i = 0; i < sizeof(lp->d_typename) - 1 && *p; i++)
2149 		lp->d_typename[i] = *p++;
2150 	lp->d_typename[i] = 0;
2151 	sz = ut->ut_sizes;
2152 	/* GET nsectors, ntracks, ncylinders FROM SAVED GEOMETRY? */
2153 	lp->d_nsectors = ut->ut_nsectors;
2154 	lp->d_ntracks = ut->ut_ntracks;
2155 	lp->d_ncylinders = ut->ut_ncylinders;
2156 	lp->d_npartitions = 8;
2157 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
2158 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8]; pp++, sz++) {
2159 		pp->p_offset = sz->blkoff;
2160 		if ((pp->p_size = sz->nblocks) == (u_long)-1)
2161 			pp->p_size = ra->ra_dsize - sz->blkoff;
2162 	}
2163 	return (1);
2164 }
2165 #endif /* COMPAT_42 */
2166 #endif /* NUDA > 0 */
2167