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