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