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