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