xref: /original-bsd/sys/vax/uba/uda.c (revision 6f738a42)
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  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  *	@(#)uda.c	7.20 (Berkeley) 08/27/88
21  */
22 
23 /*
24  * UDA50/MSCP device driver
25  */
26 
27 #define	POLLSTATS
28 
29 /*
30  * TODO
31  *	write bad block forwarding code
32  */
33 
34 #include "ra.h"
35 
36 #if NUDA > 0
37 
38 /*
39  * CONFIGURATION OPTIONS.  The next three defines are tunable -- tune away!
40  *
41  * COMPAT_42 enables 4.2/4.3 compatibility (label mapping)
42  *
43  * NRSPL2 and NCMDL2 control the number of response and command
44  * packets respectively.  They may be any value from 0 to 7, though
45  * setting them higher than 5 is unlikely to be of any value.
46  * If you get warnings about your command ring being too small,
47  * try increasing the values by one.
48  *
49  * MAXUNIT controls the maximum unit number (number of drives per
50  * controller) we are prepared to handle.
51  *
52  * DEFAULT_BURST must be at least 1.
53  */
54 #define	COMPAT_42
55 
56 #define	NRSPL2	5		/* log2 number of response packets */
57 #define NCMDL2	5		/* log2 number of command packets */
58 #define	MAXUNIT	8		/* maximum allowed unit number */
59 #define	DEFAULT_BURST	4	/* default DMA burst size */
60 
61 #include "param.h"
62 #include "systm.h"
63 #include "buf.h"
64 #include "conf.h"
65 #include "dir.h"
66 #include "file.h"
67 #include "ioctl.h"
68 #include "user.h"
69 #include "map.h"
70 #include "vm.h"
71 #include "dkstat.h"
72 #include "cmap.h"
73 #include "disklabel.h"
74 #include "syslog.h"
75 #include "stat.h"
76 
77 #include "../machine/pte.h"
78 
79 #include "../vax/cpu.h"
80 #include "ubareg.h"
81 #include "ubavar.h"
82 
83 #define	NRSP	(1 << NRSPL2)
84 #define	NCMD	(1 << NCMDL2)
85 
86 #include "udareg.h"
87 #include "../vax/mscp.h"
88 #include "../vax/mscpvar.h"
89 #include "../vax/mtpr.h"
90 
91 /*
92  * UDA communications area and MSCP packet pools, per controller.
93  */
94 struct	uda {
95 	struct	udaca uda_ca;		/* communications area */
96 	struct	mscp uda_rsp[NRSP];	/* response packets */
97 	struct	mscp uda_cmd[NCMD];	/* command packets */
98 } uda[NUDA];
99 
100 /*
101  * Software status, per controller.
102  */
103 struct	uda_softc {
104 	struct	uda *sc_uda;	/* Unibus address of uda struct */
105 	short	sc_state;	/* UDA50 state; see below */
106 	short	sc_flags;	/* flags; see below */
107 	int	sc_micro;	/* microcode revision */
108 	int	sc_ivec;	/* interrupt vector address */
109 	struct	mscp_info sc_mi;/* MSCP info (per mscpvar.h) */
110 #ifndef POLLSTATS
111 	int	sc_wticks;	/* watchdog timer ticks */
112 #else
113 	short	sc_wticks;
114 	short	sc_ncmd;
115 #endif
116 } uda_softc[NUDA];
117 
118 #ifdef POLLSTATS
119 struct udastats {
120 	int	ncmd;
121 	int	cmd[NCMD + 1];
122 } udastats = { NCMD + 1 };
123 #endif
124 
125 /*
126  * Controller states
127  */
128 #define	ST_IDLE		0	/* uninitialised */
129 #define	ST_STEP1	1	/* in `STEP 1' */
130 #define	ST_STEP2	2	/* in `STEP 2' */
131 #define	ST_STEP3	3	/* in `STEP 3' */
132 #define	ST_SETCHAR	4	/* in `Set Controller Characteristics' */
133 #define	ST_RUN		5	/* up and running */
134 
135 /*
136  * Flags
137  */
138 #define	SC_MAPPED	0x01	/* mapped in Unibus I/O space */
139 #define	SC_INSTART	0x02	/* inside udastart() */
140 #define	SC_GRIPED	0x04	/* griped about cmd ring too small */
141 #define	SC_INSLAVE	0x08	/* inside udaslave() */
142 #define	SC_DOWAKE	0x10	/* wakeup when ctlr init done */
143 #define	SC_STARTPOLL	0x20	/* need to initiate polling */
144 
145 /*
146  * Device to unit number and partition and back
147  */
148 #define	UNITSHIFT	3
149 #define	UNITMASK	7
150 #define	udaunit(dev)	(minor(dev) >> UNITSHIFT)
151 #define	udapart(dev)	(minor(dev) & UNITMASK)
152 #define	udaminor(u, p)	(((u) << UNITSHIFT) | (p))
153 
154 /*
155  * Drive status, per drive
156  */
157 struct ra_info {
158 	daddr_t	ra_dsize;	/* size in sectors */
159 /*	u_long	ra_type;	/* drive type */
160 	u_long	ra_mediaid;	/* media id */
161 	int	ra_state;	/* open/closed state */
162 	struct	ra_geom {	/* geometry information */
163 		u_short	rg_nsectors;	/* sectors/track */
164 		u_short	rg_ngroups;	/* track groups */
165 		u_short	rg_ngpc;	/* groups/cylinder */
166 		u_short	rg_ntracks;	/* ngroups*ngpc */
167 		u_short	rg_ncyl;	/* ra_dsize/ntracks/nsectors */
168 #ifdef notyet
169 		u_short	rg_rctsize;	/* size of rct */
170 		u_short	rg_rbns;	/* replacement blocks per track */
171 		u_short	rg_nrct;	/* number of rct copies */
172 #endif
173 	} ra_geom;
174 	int	ra_wlabel;	/* label sector is currently writable */
175 	u_long	ra_openpart;	/* partitions open */
176 	u_long	ra_bopenpart;	/* block partitions open */
177 	u_long	ra_copenpart;	/* character partitions open */
178 } ra_info[NRA];
179 
180 /*
181  * Software state, per drive
182  */
183 #define	CLOSED		0
184 #define	WANTOPEN	1
185 #define	RDLABEL		2
186 #define	OPEN		3
187 #define	OPENRAW		4
188 
189 /*
190  * Definition of the driver for autoconf.
191  */
192 int	udaprobe(), udaslave(), udaattach(), udadgo(), udaintr();
193 struct	uba_ctlr *udaminfo[NUDA];
194 struct	uba_device *udadinfo[NRA];
195 struct	disklabel udalabel[NRA];
196 
197 u_short	udastd[] = { 0772150, 0772550, 0777550, 0 };
198 struct	uba_driver udadriver =
199  { udaprobe, udaslave, udaattach, udadgo, udastd, "ra", udadinfo, "uda",
200    udaminfo };
201 
202 /*
203  * More driver definitions, for generic MSCP code.
204  */
205 int	udadgram(), udactlrdone(), udaunconf(), udaiodone();
206 int	udaonline(), udagotstatus(), udaioerror(), udareplace(), udabb();
207 
208 struct	buf udautab[NRA];	/* per drive transfer queue */
209 
210 struct	mscp_driver udamscpdriver =
211  { MAXUNIT, NRA, UNITSHIFT, udautab, udalabel, udadinfo,
212    udadgram, udactlrdone, udaunconf, udaiodone,
213    udaonline, udagotstatus, udareplace, udaioerror, udabb,
214    "uda", "ra" };
215 
216 /*
217  * Miscellaneous private variables.
218  */
219 char	udasr_bits[] = UDASR_BITS;
220 
221 struct	uba_device *udaip[NUDA][MAXUNIT];
222 				/* inverting pointers: ctlr & unit => Unibus
223 				   device pointer */
224 
225 int	udaburst[NUDA] = { 0 };	/* burst size, per UDA50, zero => default;
226 				   in data space so patchable via adb */
227 
228 struct	mscp udaslavereply;	/* get unit status response packet, set
229 				   for udaslave by udaunconf, via udaintr */
230 
231 static struct uba_ctlr *probeum;/* this is a hack---autoconf should pass ctlr
232 				   info to slave routine; instead, we remember
233 				   the last ctlr argument to probe */
234 
235 int	udawstart, udawatch();	/* watchdog timer */
236 
237 /*
238  * Externals
239  */
240 int	wakeup();
241 int	hz;
242 
243 /*
244  * Poke at a supposed UDA50 to see if it is there.
245  * This routine duplicates some of the code in udainit() only
246  * because autoconf has not set up the right information yet.
247  * We have to do everything `by hand'.
248  */
249 udaprobe(reg, ctlr, um)
250 	caddr_t reg;
251 	int ctlr;
252 	struct uba_ctlr *um;
253 {
254 	register int br, cvec;
255 	register struct uda_softc *sc;
256 	register struct udadevice *udaddr;
257 	register struct mscp_info *mi;
258 	int timeout, tries;
259 
260 #ifdef VAX750
261 	/*
262 	 * The UDA50 wants to share BDPs on 750s, but not on 780s or
263 	 * 8600s.  (730s have no BDPs anyway.)  Toward this end, we
264 	 * here set the `keep bdp' flag in the per-driver information
265 	 * if this is a 750.  (We just need to do it once, but it is
266 	 * easiest to do it now, for each UDA50.)
267 	 */
268 	if (cpu == VAX_750)
269 		udadriver.ud_keepbdp = 1;
270 #endif
271 
272 	probeum = um;			/* remember for udaslave() */
273 #ifdef lint
274 	br = 0; cvec = br; br = cvec; udaintr(0);
275 #endif
276 	/*
277 	 * Set up the controller-specific generic MSCP driver info.
278 	 * Note that this should really be done in the (nonexistent)
279 	 * controller attach routine.
280 	 */
281 	sc = &uda_softc[ctlr];
282 	mi = &sc->sc_mi;
283 	mi->mi_md = &udamscpdriver;
284 	mi->mi_ctlr = um->um_ctlr;
285 	mi->mi_tab = &um->um_tab;
286 	mi->mi_ip = udaip[ctlr];
287 	mi->mi_cmd.mri_size = NCMD;
288 	mi->mi_cmd.mri_desc = uda[ctlr].uda_ca.ca_cmddsc;
289 	mi->mi_cmd.mri_ring = uda[ctlr].uda_cmd;
290 	mi->mi_rsp.mri_size = NRSP;
291 	mi->mi_rsp.mri_desc = uda[ctlr].uda_ca.ca_rspdsc;
292 	mi->mi_rsp.mri_ring = uda[ctlr].uda_rsp;
293 	mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab;
294 
295 	/*
296 	 * More controller specific variables.  Again, this should
297 	 * be in the controller attach routine.
298 	 */
299 	if (udaburst[ctlr] == 0)
300 		udaburst[ctlr] = DEFAULT_BURST;
301 
302 	/*
303 	 * Get an interrupt vector.  Note that even if the controller
304 	 * does not respond, we keep the vector.  This is not a serious
305 	 * problem; but it would be easily fixed if we had a controller
306 	 * attach routine.  Sigh.
307 	 */
308 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
309 	udaddr = (struct udadevice *) reg;
310 
311 	/*
312 	 * Initialise the controller (partially).  The UDA50 programmer's
313 	 * manual states that if initialisation fails, it should be retried
314 	 * at least once, but after a second failure the port should be
315 	 * considered `down'; it also mentions that the controller should
316 	 * initialise within ten seconds.  Or so I hear; I have not seen
317 	 * this manual myself.
318 	 */
319 	tries = 0;
320 again:
321 	udaddr->udaip = 0;		/* start initialisation */
322 	timeout = todr() + 1000;	/* timeout in 10 seconds */
323 	while ((udaddr->udasa & UDA_STEP1) == 0)
324 		if (todr() > timeout)
325 			goto bad;
326 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
327 		(sc->sc_ivec >> 2);
328 	while ((udaddr->udasa & UDA_STEP2) == 0)
329 		if (todr() > timeout)
330 			goto bad;
331 
332 	/* should have interrupted by now */
333 #ifdef QBA
334 	if (cpu == VAX_630 || cpu == VAX_650)
335 		br = 0x15;	/* screwy interrupt structure */
336 #endif
337 	return (sizeof (struct udadevice));
338 bad:
339 	if (++tries < 2)
340 		goto again;
341 	return (0);
342 }
343 
344 /*
345  * Find a slave.  We allow wildcard slave numbers (something autoconf
346  * is not really prepared to deal with); and we need to know the
347  * controller number to talk to the UDA.  For the latter, we keep
348  * track of the last controller probed, since a controller probe
349  * immediately precedes all slave probes for that controller.  For the
350  * former, we simply put the unit number into ui->ui_slave after we
351  * have found one.
352  *
353  * Note that by the time udaslave is called, the interrupt vector
354  * for the UDA50 has been set up (so that udaunconf() will be called).
355  */
356 udaslave(ui, reg)
357 	register struct uba_device *ui;
358 	caddr_t reg;
359 {
360 	register struct uba_ctlr *um = probeum;
361 	register struct mscp *mp;
362 	register struct uda_softc *sc;
363 	int next = 0, timeout, tries, i;
364 
365 #ifdef lint
366 	i = 0; i = i;
367 #endif
368 	/*
369 	 * Make sure the controller is fully initialised, by waiting
370 	 * for it if necessary.
371 	 */
372 	sc = &uda_softc[um->um_ctlr];
373 	if (sc->sc_state == ST_RUN)
374 		goto findunit;
375 	tries = 0;
376 again:
377 	if (udainit(ui->ui_ctlr))
378 		return (0);
379 	timeout = todr() + 1000;		/* 10 seconds */
380 	while (todr() < timeout)
381 		if (sc->sc_state == ST_RUN)	/* made it */
382 			goto findunit;
383 	if (++tries < 2)
384 		goto again;
385 	printf("uda%d: controller hung\n", um->um_ctlr);
386 	return (0);
387 
388 	/*
389 	 * The controller is all set; go find the unit.  Grab an
390 	 * MSCP packet and send out a Get Unit Status command, with
391 	 * the `next unit' modifier if we are looking for a generic
392 	 * unit.  We set the `in slave' flag so that udaunconf()
393 	 * knows to copy the response to `udaslavereply'.
394 	 */
395 findunit:
396 	udaslavereply.mscp_opcode = 0;
397 	sc->sc_flags |= SC_INSLAVE;
398 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL)
399 		panic("udaslave");		/* `cannot happen' */
400 	mp->mscp_opcode = M_OP_GETUNITST;
401 	if (ui->ui_slave == '?') {
402 		mp->mscp_unit = next;
403 		mp->mscp_modifier = M_GUM_NEXTUNIT;
404 	} else {
405 		mp->mscp_unit = ui->ui_slave;
406 		mp->mscp_modifier = 0;
407 	}
408 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
409 	i = ((struct udadevice *) reg)->udaip;	/* initiate polling */
410 	mp = &udaslavereply;
411 	timeout = todr() + 1000;
412 	while (todr() < timeout)
413 		if (mp->mscp_opcode)
414 			goto gotit;
415 	printf("uda%d: no response to Get Unit Status request\n",
416 		um->um_ctlr);
417 	sc->sc_flags &= ~SC_INSLAVE;
418 	return (0);
419 
420 gotit:
421 	sc->sc_flags &= ~SC_INSLAVE;
422 
423 	/*
424 	 * Got a slave response.  If the unit is there, use it.
425 	 */
426 	switch (mp->mscp_status & M_ST_MASK) {
427 
428 	case M_ST_SUCCESS:	/* worked */
429 	case M_ST_AVAILABLE:	/* found another drive */
430 		break;		/* use it */
431 
432 	case M_ST_OFFLINE:
433 		/*
434 		 * Figure out why it is off line.  It may be because
435 		 * it is nonexistent, or because it is spun down, or
436 		 * for some other reason.
437 		 */
438 		switch (mp->mscp_status & ~M_ST_MASK) {
439 
440 		case M_OFFLINE_UNKNOWN:
441 			/*
442 			 * No such drive, and there are none with
443 			 * higher unit numbers either, if we are
444 			 * using M_GUM_NEXTUNIT.
445 			 */
446 			return (0);
447 
448 		case M_OFFLINE_UNMOUNTED:
449 			/*
450 			 * The drive is not spun up.  Use it anyway.
451 			 *
452 			 * N.B.: this seems to be a common occurrance
453 			 * after a power failure.  The first attempt
454 			 * to bring it on line seems to spin it up
455 			 * (and thus takes several minutes).  Perhaps
456 			 * we should note here that the on-line may
457 			 * take longer than usual.
458 			 */
459 			break;
460 
461 		default:
462 			/*
463 			 * In service, or something else equally unusable.
464 			 */
465 			printf("uda%d: unit %d off line: ", um->um_ctlr,
466 				mp->mscp_unit);
467 			mscp_printevent(mp);
468 			goto try_another;
469 		}
470 		break;
471 
472 	default:
473 		printf("uda%d: unable to get unit status: ", um->um_ctlr);
474 		mscp_printevent(mp);
475 		return (0);
476 	}
477 
478 	/*
479 	 * Does this ever happen?  What (if anything) does it mean?
480 	 */
481 	if (mp->mscp_unit < next) {
482 		printf("uda%d: unit %d, next %d\n",
483 			um->um_ctlr, mp->mscp_unit, next);
484 		return (0);
485 	}
486 
487 	if (mp->mscp_unit >= MAXUNIT) {
488 		printf("uda%d: cannot handle unit number %d (max is %d)\n",
489 			um->um_ctlr, mp->mscp_unit, MAXUNIT - 1);
490 		return (0);
491 	}
492 
493 	/*
494 	 * See if we already handle this drive.
495 	 * (Only likely if ui->ui_slave=='?'.)
496 	 */
497 	if (udaip[um->um_ctlr][mp->mscp_unit] != NULL) {
498 try_another:
499 		if (ui->ui_slave != '?')
500 			return (0);
501 		next = mp->mscp_unit + 1;
502 		goto findunit;
503 	}
504 
505 	/*
506 	 * Voila!
507 	 */
508 	uda_rasave(ui->ui_unit, mp, 0);
509 	ui->ui_flags = 0;	/* not on line, nor anything else */
510 	ui->ui_slave = mp->mscp_unit;
511 	return (1);
512 }
513 
514 /*
515  * Attach a found slave.  Make sure the watchdog timer is running.
516  * If this disk is being profiled, fill in the `mspw' value (used by
517  * what?).  Set up the inverting pointer, and attempt to bring the
518  * drive on line and read its label.
519  */
520 udaattach(ui)
521 	register struct uba_device *ui;
522 {
523 	register int unit = ui->ui_unit;
524 
525 	if (udawstart == 0) {
526 		timeout(udawatch, (caddr_t) 0, hz);
527 		udawstart++;
528 	}
529 
530 	/*
531 	 * Floppies cannot be brought on line unless there is
532 	 * a disk in the drive.  Since an ONLINE while cold
533 	 * takes ten seconds to fail, and (when notyet becomes now)
534 	 * no sensible person will swap to one, we just
535 	 * defer the ONLINE until someone tries to use the drive.
536 	 *
537 	 * THIS ASSUMES THAT DRIVE TYPES ?X? ARE FLOPPIES
538 	 */
539 	if (MSCP_MID_ECH(1, ra_info[unit].ra_mediaid) == 'X' - '@') {
540 		printf(": floppy");
541 		return;
542 	}
543 	if (ui->ui_dk >= 0)
544 		dk_mspw[ui->ui_dk] = 1.0 / (60 * 31 * 256);	/* approx */
545 	udaip[ui->ui_ctlr][ui->ui_slave] = ui;
546 
547 	if (uda_rainit(ui, 0))
548 		printf(": offline");
549 	else {
550 		printf(": %s, size = %d sectors",
551 		    udalabel[unit].d_typename, ra_info[unit].ra_dsize);
552 #ifdef notyet
553 		addswap(makedev(UDADEVNUM, udaminor(unit, 0)), &udalabel[unit]);
554 #endif
555 	}
556 }
557 
558 /*
559  * Initialise a UDA50.  Return true iff something goes wrong.
560  */
561 udainit(ctlr)
562 	int ctlr;
563 {
564 	register struct uda_softc *sc;
565 	register struct udadevice *udaddr;
566 	struct uba_ctlr *um;
567 	int timo, ubinfo;
568 
569 	sc = &uda_softc[ctlr];
570 	um = udaminfo[ctlr];
571 	if ((sc->sc_flags & SC_MAPPED) == 0) {
572 		/*
573 		 * Map the communication area and command and
574 		 * response packets into Unibus space.
575 		 */
576 		ubinfo = uballoc(um->um_ubanum, (caddr_t) &uda[ctlr],
577 			sizeof (struct uda), UBA_CANTWAIT);
578 		if (ubinfo == 0) {
579 			printf("uda%d: uballoc map failed\n", ctlr);
580 			return (-1);
581 		}
582 		sc->sc_uda = (struct uda *) (ubinfo & 0x3ffff);
583 		sc->sc_flags |= SC_MAPPED;
584 	}
585 
586 	/*
587 	 * While we are thinking about it, reset the next command
588 	 * and response indicies.
589 	 */
590 	sc->sc_mi.mi_cmd.mri_next = 0;
591 	sc->sc_mi.mi_rsp.mri_next = 0;
592 
593 	/*
594 	 * Start up the hardware initialisation sequence.
595 	 */
596 #define	STEP0MASK	(UDA_ERR | UDA_STEP4 | UDA_STEP3 | UDA_STEP2 | \
597 			 UDA_STEP1 | UDA_NV)
598 
599 	sc->sc_state = ST_IDLE;	/* in case init fails */
600 	udaddr = (struct udadevice *)um->um_addr;
601 	udaddr->udaip = 0;
602 	timo = todr() + 1000;
603 	while ((udaddr->udasa & STEP0MASK) == 0) {
604 		if (todr() > timo) {
605 			printf("uda%d: timeout during init\n", ctlr);
606 			return (-1);
607 		}
608 	}
609 	if ((udaddr->udasa & STEP0MASK) != UDA_STEP1) {
610 		printf("uda%d: init failed, sa=%b\n", ctlr,
611 			udaddr->udasa, udasr_bits);
612 		udasaerror(um, 0);
613 		return (-1);
614 	}
615 
616 	/*
617 	 * Success!  Record new state, and start step 1 initialisation.
618 	 * The rest is done in the interrupt handler.
619 	 */
620 	sc->sc_state = ST_STEP1;
621 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
622 	    (sc->sc_ivec >> 2);
623 	return (0);
624 }
625 
626 /*
627  * Open a drive.
628  */
629 /*ARGSUSED*/
630 udaopen(dev, flag, fmt)
631 	dev_t dev;
632 	int flag, fmt;
633 {
634 	register int unit;
635 	register struct uba_device *ui;
636 	register struct uda_softc *sc;
637 	register struct disklabel *lp;
638 	register struct partition *pp;
639 	register struct ra_info *ra;
640 	int s, i, part, mask, error = 0;
641 	daddr_t start, end;
642 
643 	/*
644 	 * Make sure this is a reasonable open request.
645 	 */
646 	unit = udaunit(dev);
647 	if (unit >= NRA || (ui = udadinfo[unit]) == 0 || ui->ui_alive == 0)
648 		return (ENXIO);
649 
650 	/*
651 	 * Make sure the controller is running, by (re)initialising it if
652 	 * necessary.
653 	 */
654 	sc = &uda_softc[ui->ui_ctlr];
655 	s = spl5();
656 	if (sc->sc_state != ST_RUN) {
657 		if (sc->sc_state == ST_IDLE && udainit(ui->ui_ctlr)) {
658 			splx(s);
659 			return (EIO);
660 		}
661 		/*
662 		 * In case it does not come up, make sure we will be
663 		 * restarted in 10 seconds.  This corresponds to the
664 		 * 10 second timeouts in udaprobe() and udaslave().
665 		 */
666 		sc->sc_flags |= SC_DOWAKE;
667 		timeout(wakeup, (caddr_t) sc, 10 * hz);
668 		sleep((caddr_t) sc, PRIBIO);
669 		if (sc->sc_state != ST_RUN) {
670 			splx(s);
671 			printf("uda%d: controller hung\n", ui->ui_ctlr);
672 			return (EIO);
673 		}
674 		untimeout(wakeup, (caddr_t) sc);
675 	}
676 
677 	/*
678 	 * Wait for the state to settle
679 	 */
680 	ra = &ra_info[unit];
681 	while (ra->ra_state != OPEN && ra->ra_state != OPENRAW &&
682 	    ra->ra_state != CLOSED)
683 		sleep((caddr_t)ra, PZERO + 1);
684 
685 	/*
686 	 * If not on line, or we are not sure of the label, reinitialise
687 	 * the drive.
688 	 */
689 	if ((ui->ui_flags & UNIT_ONLINE) == 0 ||
690 	    (ra->ra_state != OPEN && ra->ra_state != OPENRAW))
691 		error = uda_rainit(ui, flag);
692 	splx(s);
693 	if (error)
694 		return (error);
695 
696 	part = udapart(dev);
697 	lp = &udalabel[unit];
698 	if (part >= lp->d_npartitions)
699 		return (ENXIO);
700 	/*
701 	 * Warn if a partition is opened that overlaps another
702 	 * already open, unless either is the `raw' partition
703 	 * (whole disk).
704 	 */
705 #define	RAWPART		2	/* 'c' partition */	/* XXX */
706 	mask = 1 << part;
707 	if ((ra->ra_openpart & mask) == 0 && part != RAWPART) {
708 		pp = &lp->d_partitions[part];
709 		start = pp->p_offset;
710 		end = pp->p_offset + pp->p_size;
711 		for (pp = lp->d_partitions, i = 0;
712 		     i < lp->d_npartitions; pp++, i++) {
713 			if (pp->p_offset + pp->p_size <= start ||
714 			    pp->p_offset >= end || i == RAWPART)
715 				continue;
716 			if (ra->ra_openpart & (1 << i))
717 				log(LOG_WARNING,
718 				    "ra%d%c: overlaps open partition (%c)\n",
719 				    unit, part + 'a', i + 'a');
720 		}
721 	}
722 	switch (fmt) {
723 	case S_IFCHR:
724 		ra->ra_copenpart |= mask;
725 		break;
726 	case S_IFBLK:
727 		ra->ra_bopenpart |= mask;
728 		break;
729 	}
730 	ra->ra_openpart |= mask;
731 	return (0);
732 }
733 
734 /* ARGSUSED */
735 udaclose(dev, flags, fmt)
736 	dev_t dev;
737 	int flags, fmt;
738 {
739 	register int unit = udaunit(dev);
740 	register struct ra_info *ra = &ra_info[unit];
741 	int s, mask = (1 << udapart(dev));
742 
743 	switch (fmt) {
744 	case S_IFCHR:
745 		ra->ra_copenpart &= ~mask;
746 		break;
747 	case S_IFBLK:
748 		ra->ra_bopenpart &= ~mask;
749 		break;
750 	}
751 	ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
752 
753 	/*
754 	 * Should wait for I/O to complete on this partition even if
755 	 * others are open, but wait for work on blkflush().
756 	 */
757 	if (ra->ra_openpart == 0) {
758 		s = spl5();
759 		while (udautab[unit].b_actf)
760 			sleep((caddr_t)&udautab[unit], PZERO - 1);
761 		splx(s);
762 		ra->ra_state = CLOSED;
763 		ra->ra_wlabel = 0;
764 	}
765 	return (0);
766 }
767 
768 /*
769  * Initialise a drive.  If it is not already, bring it on line,
770  * and set a timeout on it in case it fails to respond.
771  * When on line, read in the pack label.
772  */
773 uda_rainit(ui, flags)
774 	register struct uba_device *ui;
775 	int flags;
776 {
777 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
778 	register struct disklabel *lp;
779 	register struct mscp *mp;
780 	register int unit = ui->ui_unit;
781 	register struct ra_info *ra;
782 	char *msg, *readdisklabel();
783 	int s, i, udastrategy();
784 	extern int cold;
785 
786 	ra = &ra_info[unit];
787 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
788 		mp = mscp_getcp(&sc->sc_mi, MSCP_WAIT);
789 		mp->mscp_opcode = M_OP_ONLINE;
790 		mp->mscp_unit = ui->ui_slave;
791 		mp->mscp_cmdref = (long)&ui->ui_flags;
792 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
793 		ra->ra_state = WANTOPEN;
794 		if (!cold)
795 			s = spl5();
796 		i = ((struct udadevice *)ui->ui_addr)->udaip;
797 
798 		if (cold) {
799 			i = todr() + 1000;
800 			while ((ui->ui_flags & UNIT_ONLINE) == 0)
801 				if (todr() > i)
802 					break;
803 		} else {
804 			timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz);
805 			sleep((caddr_t)&ui->ui_flags, PSWP + 1);
806 			splx(s);
807 			untimeout(wakeup, (caddr_t)&ui->ui_flags);
808 		}
809 		if (ra->ra_state != OPENRAW) {
810 			ra->ra_state = CLOSED;
811 			wakeup((caddr_t)ra);
812 			return (EIO);
813 		}
814 	}
815 
816 	lp = &udalabel[unit];
817 	lp->d_secsize = DEV_BSIZE;
818 	lp->d_secperunit = ra->ra_dsize;
819 
820 	if (flags & O_NDELAY)
821 		return (0);
822 	ra->ra_state = RDLABEL;
823 	/*
824 	 * Set up default sizes until we have the label, or longer
825 	 * if there is none.  Set secpercyl, as readdisklabel wants
826 	 * to compute b_cylin (although we do not need it).
827 	 */
828 	lp->d_secpercyl = 1;
829 	lp->d_npartitions = 1;
830 	lp->d_partitions[0].p_size = lp->d_secperunit;
831 	lp->d_partitions[0].p_offset = 0;
832 
833 	/*
834 	 * Read pack label.
835 	 */
836 	if ((msg = readdisklabel(udaminor(unit, 0), udastrategy, lp)) != NULL) {
837 		if (cold)
838 			printf(": %s", msg);
839 		else
840 			log(LOG_ERR, "ra%d: %s\n", unit, msg);
841 #ifdef COMPAT_42
842 		if (udamaptype(unit, lp))
843 			ra->ra_state = OPEN;
844 		else
845 			ra->ra_state = OPENRAW;
846 #else
847 		ra->ra_state = OPENRAW;
848 		/* uda_makefakelabel(ra, lp); */
849 #endif
850 	} else
851 		ra->ra_state = OPEN;
852 	wakeup((caddr_t)ra);
853 	return (0);
854 }
855 
856 /*
857  * Copy the geometry information for the given ra from a
858  * GET UNIT STATUS response.  If check, see if it changed.
859  */
860 uda_rasave(unit, mp, check)
861 	int unit;
862 	register struct mscp *mp;
863 	int check;
864 {
865 	register struct ra_info *ra = &ra_info[unit];
866 
867 	if (check && ra->ra_mediaid != mp->mscp_guse.guse_mediaid) {
868 		printf("ra%d: changed types! was %d now %d\n", unit,
869 			ra->ra_mediaid, mp->mscp_guse.guse_mediaid);
870 		ra->ra_state = CLOSED;	/* ??? */
871 	}
872 	/* ra->ra_type = mp->mscp_guse.guse_drivetype; */
873 	ra->ra_mediaid = mp->mscp_guse.guse_mediaid;
874 	ra->ra_geom.rg_nsectors = mp->mscp_guse.guse_nspt;
875 	ra->ra_geom.rg_ngroups = mp->mscp_guse.guse_group;
876 	ra->ra_geom.rg_ngpc = mp->mscp_guse.guse_ngpc;
877 	ra->ra_geom.rg_ntracks = ra->ra_geom.rg_ngroups * ra->ra_geom.rg_ngpc;
878 	/* ra_geom.rg_ncyl cannot be computed until we have ra_dsize */
879 #ifdef notyet
880 	ra->ra_geom.rg_rctsize = mp->mscp_guse.guse_rctsize;
881 	ra->ra_geom.rg_rbns = mp->mscp_guse.guse_nrpt;
882 	ra->ra_geom.rg_nrct = mp->mscp_guse.guse_nrct;
883 #endif
884 }
885 
886 /*
887  * Queue a transfer request, and if possible, hand it to the controller.
888  *
889  * This routine is broken into two so that the internal version
890  * udastrat1() can be called by the (nonexistent, as yet) bad block
891  * revectoring routine.
892  */
893 udastrategy(bp)
894 	register struct buf *bp;
895 {
896 	register int unit;
897 	register struct uba_device *ui;
898 	register struct ra_info *ra;
899 	struct partition *pp;
900 	int p;
901 	daddr_t sz, maxsz;
902 
903 	/*
904 	 * Make sure this is a reasonable drive to use.
905 	 */
906 	if ((unit = udaunit(bp->b_dev)) >= NRA ||
907 	    (ui = udadinfo[unit]) == NULL || ui->ui_alive == 0 ||
908 	    (ra = &ra_info[unit])->ra_state == CLOSED) {
909 		bp->b_error = ENXIO;
910 		goto bad;
911 	}
912 
913 	/*
914 	 * If drive is open `raw' or reading label, let it at it.
915 	 */
916 	if (ra->ra_state < OPEN) {
917 		udastrat1(bp);
918 		return;
919 	}
920 	p = udapart(bp->b_dev);
921 	if ((ra->ra_openpart & (1 << p)) == 0) {
922 		bp->b_error = ENODEV;
923 		goto bad;
924 	}
925 
926 	/*
927 	 * Determine the size of the transfer, and make sure it is
928 	 * within the boundaries of the partition.
929 	 */
930 	pp = &udalabel[unit].d_partitions[p];
931 	maxsz = pp->p_size;
932 	if (pp->p_offset + pp->p_size > ra->ra_dsize)
933 		maxsz = ra->ra_dsize - pp->p_offset;
934 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
935 	if (bp->b_blkno + pp->p_offset <= LABELSECTOR &&
936 #if LABELSECTOR != 0
937 	    bp->b_blkno + pp->p_offset + sz > LABELSECTOR &&
938 #endif
939 	    (bp->b_flags & B_READ) == 0 && ra->ra_wlabel == 0) {
940 		bp->b_error = EROFS;
941 		goto bad;
942 	}
943 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
944 		/* if exactly at end of disk, return an EOF */
945 		if (bp->b_blkno == maxsz) {
946 			bp->b_resid = bp->b_bcount;
947 			biodone(bp);
948 			return;
949 		}
950 		/* or truncate if part of it fits */
951 		sz = maxsz - bp->b_blkno;
952 		if (sz <= 0) {
953 			bp->b_error = EINVAL;	/* or hang it up */
954 			goto bad;
955 		}
956 		bp->b_bcount = sz << DEV_BSHIFT;
957 	}
958 	udastrat1(bp);
959 	return;
960 bad:
961 	bp->b_flags |= B_ERROR;
962 	biodone(bp);
963 }
964 
965 /*
966  * Work routine for udastrategy.
967  */
968 udastrat1(bp)
969 	register struct buf *bp;
970 {
971 	register int unit = udaunit(bp->b_dev);
972 	register struct uba_ctlr *um;
973 	register struct buf *dp;
974 	struct uba_device *ui;
975 	int s = spl5();
976 
977 	/*
978 	 * Append the buffer to the drive queue, and if it is not
979 	 * already there, the drive to the controller queue.  (However,
980 	 * if the drive queue is marked to be requeued, we must be
981 	 * awaiting an on line or get unit status command; in this
982 	 * case, leave it off the controller queue.)
983 	 */
984 	um = (ui = udadinfo[unit])->ui_mi;
985 	dp = &udautab[unit];
986 	APPEND(bp, dp, av_forw);
987 	if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) {
988 		APPEND(dp, &um->um_tab, b_forw);
989 		dp->b_active++;
990 	}
991 
992 	/*
993 	 * Start activity on the controller.  Note that unlike other
994 	 * Unibus drivers, we must always do this, not just when the
995 	 * controller is not active.
996 	 */
997 	udastart(um);
998 	splx(s);
999 }
1000 
1001 /*
1002  * Start up whatever transfers we can find.
1003  * Note that udastart() must be called at spl5().
1004  */
1005 udastart(um)
1006 	register struct uba_ctlr *um;
1007 {
1008 	register struct uda_softc *sc = &uda_softc[um->um_ctlr];
1009 	register struct buf *bp, *dp;
1010 	register struct mscp *mp;
1011 	struct uba_device *ui;
1012 	struct udadevice *udaddr;
1013 	struct partition *pp;
1014 	int i, sz;
1015 
1016 #ifdef lint
1017 	i = 0; i = i;
1018 #endif
1019 	/*
1020 	 * If it is not running, try (again and again...) to initialise
1021 	 * it.  If it is currently initialising just ignore it for now.
1022 	 */
1023 	if (sc->sc_state != ST_RUN) {
1024 		if (sc->sc_state == ST_IDLE && udainit(um->um_ctlr))
1025 			printf("uda%d: still hung\n", um->um_ctlr);
1026 		return;
1027 	}
1028 
1029 	/*
1030 	 * If um_cmd is nonzero, this controller is on the Unibus
1031 	 * resource wait queue.  It will not help to try more requests;
1032 	 * instead, when the Unibus unblocks and calls udadgo(), we
1033 	 * will call udastart() again.
1034 	 */
1035 	if (um->um_cmd)
1036 		return;
1037 
1038 	sc->sc_flags |= SC_INSTART;
1039 	udaddr = (struct udadevice *) um->um_addr;
1040 
1041 loop:
1042 	/*
1043 	 * Service the drive at the head of the queue.  It may not
1044 	 * need anything, in which case it might be shutting down
1045 	 * in udaclose().
1046 	 */
1047 	if ((dp = um->um_tab.b_actf) == NULL)
1048 		goto out;
1049 	if ((bp = dp->b_actf) == NULL) {
1050 		dp->b_active = 0;
1051 		um->um_tab.b_actf = dp->b_forw;
1052 		if (ra_info[dp - udautab].ra_openpart == 0)
1053 			wakeup((caddr_t)dp); /* finish close protocol */
1054 		goto loop;
1055 	}
1056 
1057 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
1058 		udasaerror(um, 1);
1059 		goto out;
1060 	}
1061 
1062 	/*
1063 	 * Get an MSCP packet, then figure out what to do.  If
1064 	 * we cannot get a command packet, the command ring may
1065 	 * be too small:  We should have at least as many command
1066 	 * packets as credits, for best performance.
1067 	 */
1068 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) {
1069 		if (sc->sc_mi.mi_credits > MSCP_MINCREDITS &&
1070 		    (sc->sc_flags & SC_GRIPED) == 0) {
1071 			log(LOG_NOTICE, "uda%d: command ring too small\n",
1072 				um->um_ctlr);
1073 			sc->sc_flags |= SC_GRIPED;/* complain only once */
1074 		}
1075 		goto out;
1076 	}
1077 
1078 	/*
1079 	 * Bring the drive on line if it is not already.  Get its status
1080 	 * if we do not already have it.  Otherwise just start the transfer.
1081 	 */
1082 	ui = udadinfo[udaunit(bp->b_dev)];
1083 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
1084 		mp->mscp_opcode = M_OP_ONLINE;
1085 		goto common;
1086 	}
1087 	if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) {
1088 		mp->mscp_opcode = M_OP_GETUNITST;
1089 common:
1090 if (ui->ui_flags & UNIT_REQUEUE) panic("udastart");
1091 		/*
1092 		 * Take the drive off the controller queue.  When the
1093 		 * command finishes, make sure the drive is requeued.
1094 		 */
1095 		um->um_tab.b_actf = dp->b_forw;
1096 		dp->b_active = 0;
1097 		ui->ui_flags |= UNIT_REQUEUE;
1098 		mp->mscp_unit = ui->ui_slave;
1099 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
1100 		sc->sc_flags |= SC_STARTPOLL;
1101 #ifdef POLLSTATS
1102 		sc->sc_ncmd++;
1103 #endif
1104 		goto loop;
1105 	}
1106 
1107 	pp = &udalabel[ui->ui_unit].d_partitions[udapart(bp->b_dev)];
1108 	mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
1109 	mp->mscp_unit = ui->ui_slave;
1110 	mp->mscp_seq.seq_lbn = bp->b_blkno + pp->p_offset;
1111 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
1112 	mp->mscp_seq.seq_bytecount = bp->b_blkno + sz > pp->p_size ?
1113 		(pp->p_size - bp->b_blkno) >> DEV_BSHIFT : bp->b_bcount;
1114 	/* mscp_cmdref is filled in by mscp_go() */
1115 
1116 	/*
1117 	 * Drop the packet pointer into the `command' field so udadgo()
1118 	 * can tell what to start.  If ubago returns 1, we can do another
1119 	 * transfer.  If not, um_cmd will still point at mp, so we will
1120 	 * know that we are waiting for resources.
1121 	 */
1122 	um->um_cmd = (int)mp;
1123 	if (ubago(ui))
1124 		goto loop;
1125 
1126 	/*
1127 	 * All done, or blocked in ubago().  If we managed to
1128 	 * issue some commands, start up the beast.
1129 	 */
1130 out:
1131 	if (sc->sc_flags & SC_STARTPOLL) {
1132 #ifdef POLLSTATS
1133 		udastats.cmd[sc->sc_ncmd]++;
1134 		sc->sc_ncmd = 0;
1135 #endif
1136 		i = ((struct udadevice *)um->um_addr)->udaip;
1137 	}
1138 	sc->sc_flags &= ~(SC_INSTART | SC_STARTPOLL);
1139 }
1140 
1141 /*
1142  * Start a transfer.
1143  *
1144  * If we are not called from within udastart(), we must have been
1145  * blocked, so call udastart to do more requests (if any).  If
1146  * this calls us again immediately we will not recurse, because
1147  * that time we will be in udastart().  Clever....
1148  */
1149 udadgo(um)
1150 	register struct uba_ctlr *um;
1151 {
1152 	struct uda_softc *sc = &uda_softc[um->um_ctlr];
1153 	struct mscp *mp = (struct mscp *)um->um_cmd;
1154 
1155 	um->um_tab.b_active++;	/* another transfer going */
1156 
1157 	/*
1158 	 * Fill in the MSCP packet and move the buffer to the
1159 	 * I/O wait queue.  Mark the controller as no longer on
1160 	 * the resource queue, and remember to initiate polling.
1161 	 */
1162 	mp->mscp_seq.seq_buffer = (um->um_ubinfo & 0x3ffff) |
1163 		(UBAI_BDP(um->um_ubinfo) << 24);
1164 	mscp_go(&sc->sc_mi, mp, um->um_ubinfo);
1165 	um->um_cmd = 0;
1166 	um->um_ubinfo = 0;	/* tyke it awye */
1167 	sc->sc_flags |= SC_STARTPOLL;
1168 #ifdef POLLSTATS
1169 	sc->sc_ncmd++;
1170 #endif
1171 	if ((sc->sc_flags & SC_INSTART) == 0)
1172 		udastart(um);
1173 }
1174 
1175 udaiodone(mi, bp, info)
1176 	register struct mscp_info *mi;
1177 	struct buf *bp;
1178 	int info;
1179 {
1180 	register struct uba_ctlr *um = udaminfo[mi->mi_ctlr];
1181 
1182 	um->um_ubinfo = info;
1183 	ubadone(um);
1184 	biodone(bp);
1185 	if (um->um_bdp && mi->mi_wtab.av_forw == &mi->mi_wtab)
1186 		ubarelse(um->um_ubanum, &um->um_bdp);
1187 	um->um_tab.b_active--;	/* another transfer done */
1188 }
1189 
1190 static struct saerr {
1191 	int	code;		/* error code (including UDA_ERR) */
1192 	char	*desc;		/* what it means: Efoo => foo error */
1193 } saerr[] = {
1194 	{ 0100001, "Eunibus packet read" },
1195 	{ 0100002, "Eunibus packet write" },
1196 	{ 0100003, "EUDA ROM and RAM parity" },
1197 	{ 0100004, "EUDA RAM parity" },
1198 	{ 0100005, "EUDA ROM parity" },
1199 	{ 0100006, "Eunibus ring read" },
1200 	{ 0100007, "Eunibus ring write" },
1201 	{ 0100010, " unibus interrupt master failure" },
1202 	{ 0100011, "Ehost access timeout" },
1203 	{ 0100012, " host exceeded command limit" },
1204 	{ 0100013, " unibus bus master failure" },
1205 	{ 0100014, " DM XFC fatal error" },
1206 	{ 0100015, " hardware timeout of instruction loop" },
1207 	{ 0100016, " invalid virtual circuit id" },
1208 	{ 0100017, "Eunibus interrupt write" },
1209 	{ 0104000, "Efatal sequence" },
1210 	{ 0104040, " D proc ALU" },
1211 	{ 0104041, "ED proc control ROM parity" },
1212 	{ 0105102, "ED proc w/no BD#2 or RAM parity" },
1213 	{ 0105105, "ED proc RAM buffer" },
1214 	{ 0105152, "ED proc SDI" },
1215 	{ 0105153, "ED proc write mode wrap serdes" },
1216 	{ 0105154, "ED proc read mode serdes, RSGEN & ECC" },
1217 	{ 0106040, "EU proc ALU" },
1218 	{ 0106041, "EU proc control reg" },
1219 	{ 0106042, " U proc DFAIL/cntl ROM parity/BD #1 test CNT" },
1220 	{ 0106047, " U proc const PROM err w/D proc running SDI test" },
1221 	{ 0106055, " unexpected trap" },
1222 	{ 0106071, "EU proc const PROM" },
1223 	{ 0106072, "EU proc control ROM parity" },
1224 	{ 0106200, "Estep 1 data" },
1225 	{ 0107103, "EU proc RAM parity" },
1226 	{ 0107107, "EU proc RAM buffer" },
1227 	{ 0107115, " test count wrong (BD 12)" },
1228 	{ 0112300, "Estep 2" },
1229 	{ 0122240, "ENPR" },
1230 	{ 0122300, "Estep 3" },
1231 	{ 0142300, "Estep 4" },
1232 	{ 0, " unknown error code" }
1233 };
1234 
1235 /*
1236  * If the error bit was set in the controller status register, gripe,
1237  * then (optionally) reset the controller and requeue pending transfers.
1238  */
1239 udasaerror(um, doreset)
1240 	register struct uba_ctlr *um;
1241 	int doreset;
1242 {
1243 	register int code = ((struct udadevice *)um->um_addr)->udasa;
1244 	register struct saerr *e;
1245 
1246 	if ((code & UDA_ERR) == 0)
1247 		return;
1248 	for (e = saerr; e->code; e++)
1249 		if (e->code == code)
1250 			break;
1251 	printf("uda%d: controller error, sa=0%o (%s%s)\n",
1252 		um->um_ctlr, code, e->desc + 1,
1253 		*e->desc == 'E' ? " error" : "");
1254 	if (doreset) {
1255 		mscp_requeue(&uda_softc[um->um_ctlr].sc_mi);
1256 		(void) udainit(um->um_ctlr);
1257 	}
1258 }
1259 
1260 /*
1261  * Interrupt routine.  Depending on the state of the controller,
1262  * continue initialisation, or acknowledge command and response
1263  * interrupts, and process responses.
1264  */
1265 udaintr(ctlr)
1266 	int ctlr;
1267 {
1268 	register struct uba_ctlr *um = udaminfo[ctlr];
1269 	register struct uda_softc *sc = &uda_softc[ctlr];
1270 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
1271 	register struct uda *ud;
1272 	register struct mscp *mp;
1273 	register int i;
1274 
1275 #ifdef QBA
1276 	(void) spl5();		/* Qbus interrupt protocol is odd */
1277 #endif
1278 	sc->sc_wticks = 0;	/* reset interrupt watchdog */
1279 
1280 	/*
1281 	 * Combinations during steps 1, 2, and 3: STEPnMASK
1282 	 * corresponds to which bits should be tested;
1283 	 * STEPnGOOD corresponds to the pattern that should
1284 	 * appear after the interrupt from STEPn initialisation.
1285 	 * All steps test the bits in ALLSTEPS.
1286 	 */
1287 #define	ALLSTEPS	(UDA_ERR|UDA_STEP4|UDA_STEP3|UDA_STEP2|UDA_STEP1)
1288 
1289 #define	STEP1MASK	(ALLSTEPS | UDA_IE | UDA_NCNRMASK)
1290 #define	STEP1GOOD	(UDA_STEP2 | UDA_IE | (NCMDL2 << 3) | NRSPL2)
1291 
1292 #define	STEP2MASK	(ALLSTEPS | UDA_IE | UDA_IVECMASK)
1293 #define	STEP2GOOD	(UDA_STEP3 | UDA_IE | (sc->sc_ivec >> 2))
1294 
1295 #define	STEP3MASK	ALLSTEPS
1296 #define	STEP3GOOD	UDA_STEP4
1297 
1298 	switch (sc->sc_state) {
1299 
1300 	case ST_IDLE:
1301 		/*
1302 		 * Ignore unsolicited interrupts.
1303 		 */
1304 		log(LOG_WARNING, "uda%d: stray intr\n", ctlr);
1305 		return;
1306 
1307 	case ST_STEP1:
1308 		/*
1309 		 * Begin step two initialisation.
1310 		 */
1311 		if ((udaddr->udasa & STEP1MASK) != STEP1GOOD) {
1312 			i = 1;
1313 initfailed:
1314 			printf("uda%d: init step %d failed, sa=%b\n",
1315 				ctlr, i, udaddr->udasa, udasr_bits);
1316 			udasaerror(um, 0);
1317 			sc->sc_state = ST_IDLE;
1318 			if (sc->sc_flags & SC_DOWAKE) {
1319 				sc->sc_flags &= ~SC_DOWAKE;
1320 				wakeup((caddr_t)sc);
1321 			}
1322 			return;
1323 		}
1324 		udaddr->udasa = (int)&sc->sc_uda->uda_ca.ca_rspdsc[0] |
1325 			(cpu == VAX_780 || cpu == VAX_8600 ? UDA_PI : 0);
1326 		sc->sc_state = ST_STEP2;
1327 		return;
1328 
1329 	case ST_STEP2:
1330 		/*
1331 		 * Begin step 3 initialisation.
1332 		 */
1333 		if ((udaddr->udasa & STEP2MASK) != STEP2GOOD) {
1334 			i = 2;
1335 			goto initfailed;
1336 		}
1337 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_rspdsc[0]) >> 16;
1338 		sc->sc_state = ST_STEP3;
1339 		return;
1340 
1341 	case ST_STEP3:
1342 		/*
1343 		 * Set controller characteristics (finish initialisation).
1344 		 */
1345 		if ((udaddr->udasa & STEP3MASK) != STEP3GOOD) {
1346 			i = 3;
1347 			goto initfailed;
1348 		}
1349 		i = udaddr->udasa & 0xff;
1350 		if (i != sc->sc_micro) {
1351 			sc->sc_micro = i;
1352 			printf("uda%d: version %d model %d\n",
1353 				ctlr, i & 0xf, i >> 4);
1354 		}
1355 
1356 		/*
1357 		 * Present the burst size, then remove it.  Why this
1358 		 * should be done this way, I have no idea.
1359 		 *
1360 		 * Note that this assumes udaburst[ctlr] > 0.
1361 		 */
1362 		udaddr->udasa = UDA_GO | (udaburst[ctlr] - 1) << 2;
1363 		udaddr->udasa = UDA_GO;
1364 		printf("uda%d: DMA burst size set to %d\n",
1365 			ctlr, udaburst[ctlr]);
1366 
1367 		udainitds(ctlr);	/* initialise data structures */
1368 
1369 		/*
1370 		 * Before we can get a command packet, we need some
1371 		 * credits.  Fake some up to keep mscp_getcp() happy,
1372 		 * get a packet, and cancel all credits (the right
1373 		 * number should come back in the response to the
1374 		 * SCC packet).
1375 		 */
1376 		sc->sc_mi.mi_credits = MSCP_MINCREDITS + 1;
1377 		mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT);
1378 		if (mp == NULL)	/* `cannot happen' */
1379 			panic("udaintr");
1380 		sc->sc_mi.mi_credits = 0;
1381 		mp->mscp_opcode = M_OP_SETCTLRC;
1382 		mp->mscp_unit = 0;
1383 		mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC |
1384 			M_CF_THIS;
1385 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
1386 		i = udaddr->udaip;
1387 		sc->sc_state = ST_SETCHAR;
1388 		return;
1389 
1390 	case ST_SETCHAR:
1391 	case ST_RUN:
1392 		/*
1393 		 * Handle Set Ctlr Characteristics responses and operational
1394 		 * responses (via mscp_dorsp).
1395 		 */
1396 		break;
1397 
1398 	default:
1399 		printf("uda%d: driver bug, state %d\n", ctlr, sc->sc_state);
1400 		panic("udastate");
1401 	}
1402 
1403 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
1404 		udasaerror(um, 1);
1405 		return;
1406 	}
1407 
1408 	ud = &uda[ctlr];
1409 
1410 	/*
1411 	 * Handle buffer purge requests.
1412 	 */
1413 	if (ud->uda_ca.ca_bdp) {
1414 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
1415 		ud->uda_ca.ca_bdp = 0;
1416 		udaddr->udasa = 0;	/* signal purge complete */
1417 	}
1418 
1419 	/*
1420 	 * Check for response and command ring transitions.
1421 	 */
1422 	if (ud->uda_ca.ca_rspint) {
1423 		ud->uda_ca.ca_rspint = 0;
1424 		mscp_dorsp(&sc->sc_mi);
1425 	}
1426 	if (ud->uda_ca.ca_cmdint) {
1427 		ud->uda_ca.ca_cmdint = 0;
1428 		MSCP_DOCMD(&sc->sc_mi);
1429 	}
1430 	udastart(um);
1431 }
1432 
1433 /*
1434  * Initialise the various data structures that control the UDA50.
1435  */
1436 udainitds(ctlr)
1437 	int ctlr;
1438 {
1439 	register struct uda *ud = &uda[ctlr];
1440 	register struct uda *uud = uda_softc[ctlr].sc_uda;
1441 	register struct mscp *mp;
1442 	register int i;
1443 
1444 	for (i = 0, mp = ud->uda_rsp; i < NRSP; i++, mp++) {
1445 		ud->uda_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
1446 			(long)&uud->uda_rsp[i].mscp_cmdref;
1447 		mp->mscp_addr = &ud->uda_ca.ca_rspdsc[i];
1448 		mp->mscp_msglen = MSCP_MSGLEN;
1449 	}
1450 	for (i = 0, mp = ud->uda_cmd; i < NCMD; i++, mp++) {
1451 		ud->uda_ca.ca_cmddsc[i] = MSCP_INT |
1452 			(long)&uud->uda_cmd[i].mscp_cmdref;
1453 		mp->mscp_addr = &ud->uda_ca.ca_cmddsc[i];
1454 		mp->mscp_msglen = MSCP_MSGLEN;
1455 	}
1456 }
1457 
1458 /*
1459  * Handle an error datagram.
1460  */
1461 udadgram(mi, mp)
1462 	struct mscp_info *mi;
1463 	struct mscp *mp;
1464 {
1465 
1466 	mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp);
1467 	/*
1468 	 * SDI status information bytes 10 and 11 are the microprocessor
1469 	 * error code and front panel code respectively.  These vary per
1470 	 * drive type and are printed purely for field service information.
1471 	 */
1472 	if (mp->mscp_format == M_FM_SDI)
1473 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
1474 			mp->mscp_erd.erd_sdistat[10],
1475 			mp->mscp_erd.erd_sdistat[11]);
1476 }
1477 
1478 /*
1479  * The Set Controller Characteristics command finished.
1480  * Record the new state of the controller.
1481  */
1482 udactlrdone(mi, mp)
1483 	register struct mscp_info *mi;
1484 	struct mscp *mp;
1485 {
1486 	register struct uda_softc *sc = &uda_softc[mi->mi_ctlr];
1487 
1488 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
1489 		sc->sc_state = ST_RUN;
1490 	else {
1491 		printf("uda%d: SETCTLRC failed: ",
1492 			mi->mi_ctlr, mp->mscp_status);
1493 		mscp_printevent(mp);
1494 		sc->sc_state = ST_IDLE;
1495 	}
1496 	if (sc->sc_flags & SC_DOWAKE) {
1497 		sc->sc_flags &= ~SC_DOWAKE;
1498 		wakeup((caddr_t)sc);
1499 	}
1500 }
1501 
1502 /*
1503  * Received a response from an as-yet unconfigured drive.  Configure it
1504  * in, if possible.
1505  */
1506 udaunconf(mi, mp)
1507 	struct mscp_info *mi;
1508 	register struct mscp *mp;
1509 {
1510 
1511 	/*
1512 	 * If it is a slave response, copy it to udaslavereply for
1513 	 * udaslave() to look at.
1514 	 */
1515 	if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) &&
1516 	    (uda_softc[mi->mi_ctlr].sc_flags & SC_INSLAVE) != 0) {
1517 		udaslavereply = *mp;
1518 		return (MSCP_DONE);
1519 	}
1520 
1521 	/*
1522 	 * Otherwise, it had better be an available attention response.
1523 	 */
1524 	if (mp->mscp_opcode != M_OP_AVAILATTN)
1525 		return (MSCP_FAILED);
1526 
1527 	/* do what autoconf does */
1528 	return (MSCP_FAILED);	/* not yet, arwhite, not yet */
1529 }
1530 
1531 /*
1532  * A drive came on line.  Check its type and size.  Return DONE if
1533  * we think the drive is truly on line.  In any case, awaken anyone
1534  * sleeping on the drive on-line-ness.
1535  */
1536 udaonline(ui, mp)
1537 	register struct uba_device *ui;
1538 	struct mscp *mp;
1539 {
1540 	register struct ra_info *ra = &ra_info[ui->ui_unit];
1541 
1542 	wakeup((caddr_t)&ui->ui_flags);
1543 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1544 		printf("uda%d: attempt to bring ra%d on line failed: ",
1545 			ui->ui_ctlr, ui->ui_unit);
1546 		mscp_printevent(mp);
1547 		ra->ra_state = CLOSED;
1548 		return (MSCP_FAILED);
1549 	}
1550 
1551 	ra->ra_state = OPENRAW;
1552 	ra->ra_dsize = (daddr_t)mp->mscp_onle.onle_unitsize;
1553 	if (!cold)
1554 		printf("ra%d: uda%d, unit %d, size = %d sectors\n", ui->ui_unit,
1555 		    ui->ui_ctlr, mp->mscp_unit, ra->ra_dsize);
1556 	/* can now compute ncyl */
1557 	ra->ra_geom.rg_ncyl = ra->ra_dsize / ra->ra_geom.rg_ntracks /
1558 		ra->ra_geom.rg_nsectors;
1559 	return (MSCP_DONE);
1560 }
1561 
1562 /*
1563  * We got some (configured) unit's status.  Return DONE if it succeeded.
1564  */
1565 udagotstatus(ui, mp)
1566 	register struct uba_device *ui;
1567 	register struct mscp *mp;
1568 {
1569 
1570 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1571 		printf("uda%d: attempt to get status for ra%d failed: ",
1572 			ui->ui_ctlr, ui->ui_unit);
1573 		mscp_printevent(mp);
1574 		return (MSCP_FAILED);
1575 	}
1576 	/* record for (future) bad block forwarding and whatever else */
1577 	uda_rasave(ui->ui_unit, mp, 1);
1578 	return (MSCP_DONE);
1579 }
1580 
1581 /*
1582  * A transfer failed.  We get a chance to fix or restart it.
1583  * Need to write the bad block forwaring code first....
1584  */
1585 /*ARGSUSED*/
1586 udaioerror(ui, mp, bp)
1587 	register struct uba_device *ui;
1588 	register struct mscp *mp;
1589 	struct buf *bp;
1590 {
1591 
1592 	if (mp->mscp_flags & M_EF_BBLKR) {
1593 		/*
1594 		 * A bad block report.  Eventually we will
1595 		 * restart this transfer, but for now, just
1596 		 * log it and give up.
1597 		 */
1598 		log(LOG_ERR, "ra%d: bad block report: %d%s\n",
1599 			ui->ui_unit, mp->mscp_seq.seq_lbn,
1600 			mp->mscp_flags & M_EF_BBLKU ? " + others" : "");
1601 	} else {
1602 		/*
1603 		 * What the heck IS a `serious exception' anyway?
1604 		 * IT SURE WOULD BE NICE IF DEC SOLD DOCUMENTATION
1605 		 * FOR THEIR OWN CONTROLLERS.
1606 		 */
1607 		if (mp->mscp_flags & M_EF_SEREX)
1608 			log(LOG_ERR, "ra%d: serious exception reported\n",
1609 				ui->ui_unit);
1610 	}
1611 	return (MSCP_FAILED);
1612 }
1613 
1614 /*
1615  * A replace operation finished.
1616  */
1617 /*ARGSUSED*/
1618 udareplace(ui, mp)
1619 	struct uba_device *ui;
1620 	struct mscp *mp;
1621 {
1622 
1623 	panic("udareplace");
1624 }
1625 
1626 /*
1627  * A bad block related operation finished.
1628  */
1629 /*ARGSUSED*/
1630 udabb(ui, mp, bp)
1631 	struct uba_device *ui;
1632 	struct mscp *mp;
1633 	struct buf *bp;
1634 {
1635 
1636 	panic("udabb");
1637 }
1638 
1639 
1640 /*
1641  * I/O controls.
1642  */
1643 udaioctl(dev, cmd, data, flag)
1644 	dev_t dev;
1645 	int cmd;
1646 	caddr_t data;
1647 	int flag;
1648 {
1649 	register int unit = udaunit(dev);
1650 	register struct disklabel *lp;
1651 	register struct ra_info *ra = &ra_info[unit];
1652 	int error = 0;
1653 
1654 	lp = &udalabel[unit];
1655 
1656 	switch (cmd) {
1657 
1658 	case DIOCGDINFO:
1659 		*(struct disklabel *)data = *lp;
1660 		break;
1661 
1662 	case DIOCGPART:
1663 		((struct partinfo *)data)->disklab = lp;
1664 		((struct partinfo *)data)->part =
1665 		    &lp->d_partitions[udapart(dev)];
1666 		break;
1667 
1668 	case DIOCSDINFO:
1669 		if ((flag & FWRITE) == 0)
1670 			error = EBADF;
1671 		else
1672 			error = setdisklabel(lp, (struct disklabel *)data,
1673 			    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart);
1674 		break;
1675 
1676 	case DIOCWLABEL:
1677 		if ((flag & FWRITE) == 0)
1678 			error = EBADF;
1679 		else
1680 			ra->ra_wlabel = *(int *)data;
1681 		break;
1682 
1683 	case DIOCWDINFO:
1684 		if ((flag & FWRITE) == 0)
1685 			error = EBADF;
1686 		else if ((error = setdisklabel(lp, (struct disklabel *)data,
1687 		    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart)) == 0) {
1688 			int wlab;
1689 
1690 			ra->ra_state = OPEN;
1691 			/* simulate opening partition 0 so write succeeds */
1692 			ra->ra_openpart |= (1 << 0);		/* XXX */
1693 			wlab = ra->ra_wlabel;
1694 			ra->ra_wlabel = 1;
1695 			error = writedisklabel(dev, udastrategy, lp);
1696 			ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
1697 			ra->ra_wlabel = wlab;
1698 		}
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