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