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