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