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