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