xref: /csrg-svn/sys/vax/uba/uda.c (revision 32574)
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.9 (Berkeley) 11/01/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\n", 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 			error = setdisklabel(lp, (struct disklabel *)data,
1608 			    ra_info[unit].ra_openpart);
1609 		break;
1610 
1611 	case DIOCWDINFO:
1612 		if ((flag & FWRITE) == 0)
1613 			error = EBADF;
1614 		else if ((error = setdisklabel(lp, (struct disklabel *)data,
1615 			    ra_info[unit].ra_openpart)) == 0)
1616 			error = writedisklabel(dev, udastrategy, lp);
1617 		break;
1618 
1619 #ifdef notyet
1620 	case UDAIOCREPLACE:
1621 		/*
1622 		 * Initiate bad block replacement for the given LBN.
1623 		 * (Should we allow modifiers?)
1624 		 */
1625 		error = EOPNOTSUPP;
1626 		break;
1627 
1628 	case UDAIOCGMICRO:
1629 		/*
1630 		 * Return the microcode revision for the UDA50 running
1631 		 * this drive.
1632 		 */
1633 		*(int *) data = uda_softc[uddinfo[unit]->ui_ctlr].sc_micro;
1634 		break;
1635 #endif
1636 
1637 	default:
1638 		error = ENOTTY;
1639 		break;
1640 	}
1641 	return (error);
1642 }
1643 
1644 /*
1645  * A Unibus reset has occurred on UBA uban.  Reinitialise the controller(s)
1646  * on that Unibus, and requeue outstanding I/O.
1647  */
1648 udareset(uban)
1649 	int uban;
1650 {
1651 	register struct uba_ctlr *um;
1652 	register struct uda_softc *sc;
1653 	register int ctlr;
1654 
1655 	for (ctlr = 0, sc = uda_softc; ctlr < NUDA; ctlr++, sc++) {
1656 		if ((um = udaminfo[ctlr]) == NULL || um->um_ubanum != uban ||
1657 		    um->um_alive == 0)
1658 			continue;
1659 		printf(" uda%d", ctlr);
1660 
1661 		/*
1662 		 * Our BDP (if any) is gone; our command (if any) is
1663 		 * flushed; the device is no longer mapped; and the
1664 		 * UDA50 is not yet initialised.
1665 		 */
1666 		if (um->um_bdp) {
1667 			printf("<%d>", UBAI_BDP(um->um_bdp));
1668 			um->um_bdp = 0;
1669 		}
1670 		um->um_ubinfo = 0;
1671 		um->um_cmd = 0;
1672 		sc->sc_flags &= ~SC_MAPPED;
1673 		sc->sc_state = ST_IDLE;
1674 
1675 		/* reset queues and requeue pending transfers */
1676 		mscp_requeue(&sc->sc_mi);
1677 
1678 		/*
1679 		 * If it fails to initialise we will notice later and
1680 		 * try again (and again...).  Do not call udastart()
1681 		 * here; it will be done after the controller finishes
1682 		 * initialisation.
1683 		 */
1684 		if (udainit(ctlr))
1685 			printf(" (hung)");
1686 	}
1687 }
1688 
1689 /*
1690  * Watchdog timer:  If the controller is active, and no interrupts
1691  * have occurred for 30 seconds, assume it has gone away.
1692  */
1693 udawatch()
1694 {
1695 	register int i;
1696 	register struct uba_ctlr *um;
1697 	register struct uda_softc *sc;
1698 
1699 	timeout(udawatch, (caddr_t) 0, hz);	/* every second */
1700 	for (i = 0, sc = uda_softc; i < NUDA; i++, sc++) {
1701 		if ((um = udaminfo[i]) == 0 || !um->um_alive)
1702 			continue;
1703 		if (sc->sc_state == ST_IDLE)
1704 			continue;
1705 		if (sc->sc_state == ST_RUN && !um->um_tab.b_active)
1706 			sc->sc_wticks = 0;
1707 		else if (++sc->sc_wticks >= 30) {
1708 			sc->sc_wticks = 0;
1709 			printf("uda%d: lost interrupt\n", i);
1710 			ubareset(um->um_ubanum);
1711 		}
1712 	}
1713 }
1714 
1715 /*
1716  * Do a panic dump.  We set up the controller for one command packet
1717  * and one response packet, for which we use `struct uda1'.
1718  */
1719 struct	uda1 {
1720 	struct	uda1ca uda1_ca;	/* communications area */
1721 	struct	mscp uda1_rsp;	/* response packet */
1722 	struct	mscp uda1_cmd;	/* command packet */
1723 } uda1;
1724 
1725 #define	DBSIZE	32		/* dump 16K at a time */
1726 
1727 udadump(dev)
1728 	dev_t dev;
1729 {
1730 	struct udadevice *udaddr;
1731 	struct uda1 *ud_ubaddr;
1732 	char *start;
1733 	int num, blk, unit, maxsz, blkoff, reg;
1734 	struct partition *pp;
1735 	register struct uba_regs *uba;
1736 	register struct uba_device *ui;
1737 	register struct uda1 *ud;
1738 	register struct pte *io;
1739 	register int i;
1740 
1741 	/*
1742 	 * Make sure the device is a reasonable place on which to dump.
1743 	 */
1744 	unit = udaunit(dev);
1745 	if (unit >= NRA)
1746 		return (ENXIO);
1747 #define	phys(cast, addr)	((cast) ((int) addr & 0x7fffffff))
1748 	ui = phys(struct uba_device *, udadinfo[unit]);
1749 	if (ui == NULL || ui->ui_alive == 0)
1750 		return (ENXIO);
1751 
1752 	/*
1753 	 * Find and initialise the UBA; get the physical address of the
1754 	 * device registers, and of communications area and command and
1755 	 * response packet.
1756 	 */
1757 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
1758 	ubainit(uba);
1759 	udaddr = (struct udadevice *)ui->ui_physaddr;
1760 	ud = phys(struct uda1 *, &uda1);
1761 
1762 	/*
1763 	 * Map the ca+packets into Unibus I/O space so the UDA50 can get
1764 	 * at them.  Use the registers at the end of the Unibus map (since
1765 	 * we will use the registers at the beginning to map the memory
1766 	 * we are dumping).
1767 	 */
1768 	num = btoc(sizeof(struct uda1)) + 1;
1769 	reg = NUBMREG - num;
1770 	io = &uba->uba_map[reg];
1771 	for (i = 0; i < num; i++)
1772 		*(int *)io++ = UBAMR_MRV | (btop(ud) + i);
1773 	ud_ubaddr = (struct uda1 *)(((int)ud & PGOFSET) | (reg << 9));
1774 
1775 	/*
1776 	 * Initialise the controller, with one command and one response
1777 	 * packet.
1778 	 */
1779 	udaddr->udaip = 0;
1780 	if (udadumpwait(udaddr, UDA_STEP1))
1781 		return (EFAULT);
1782 	udaddr->udasa = UDA_ERR;
1783 	if (udadumpwait(udaddr, UDA_STEP2))
1784 		return (EFAULT);
1785 	udaddr->udasa = (int)&ud_ubaddr->uda1_ca.ca_rspdsc;
1786 	if (udadumpwait(udaddr, UDA_STEP3))
1787 		return (EFAULT);
1788 	udaddr->udasa = ((int)&ud_ubaddr->uda1_ca.ca_rspdsc) >> 16;
1789 	if (udadumpwait(udaddr, UDA_STEP4))
1790 		return (EFAULT);
1791 	uda_softc[ui->ui_ctlr].sc_micro = udaddr->udasa & 0xff;
1792 	udaddr->udasa = UDA_GO;
1793 
1794 	/*
1795 	 * Set up the command and response descriptor, then set the
1796 	 * controller characteristics and bring the drive on line.
1797 	 * Note that all uninitialised locations in uda1_cmd are zero.
1798 	 */
1799 	ud->uda1_ca.ca_rspdsc = (long)&ud_ubaddr->uda1_rsp.mscp_cmdref;
1800 	ud->uda1_ca.ca_cmddsc = (long)&ud_ubaddr->uda1_cmd.mscp_cmdref;
1801 	/* ud->uda1_cmd.mscp_sccc.sccc_ctlrflags = 0; */
1802 	/* ud->uda1_cmd.mscp_sccc.sccc_version = 0; */
1803 	if (udadumpcmd(M_OP_SETCTLRC, ud, ui))
1804 		return (EFAULT);
1805 	ud->uda1_cmd.mscp_unit = ui->ui_slave;
1806 	if (udadumpcmd(M_OP_ONLINE, ud, ui))
1807 		return (EFAULT);
1808 
1809 	pp = phys(struct partition *,
1810 	    &udalabel[unit].d_partitions[udapart(dev)]);
1811 	maxsz = pp->p_size;
1812 	blkoff = pp->p_offset;
1813 
1814 	/*
1815 	 * Dump all of physical memory, or as much as will fit in the
1816 	 * space provided.
1817 	 */
1818 	start = 0;
1819 	num = maxfree;
1820 	if (dumplo < 0)
1821 		return (EINVAL);
1822 	if (dumplo + num >= maxsz)
1823 		num = maxsz - dumplo;
1824 	blkoff += dumplo;
1825 
1826 	/*
1827 	 * Write out memory, DBSIZE pages at a time.
1828 	 * N.B.: this code depends on the fact that the sector
1829 	 * size == the page size.
1830 	 */
1831 	while (num > 0) {
1832 		blk = num > DBSIZE ? DBSIZE : num;
1833 		io = uba->uba_map;
1834 		/*
1835 		 * Map in the pages to write, leaving an invalid entry
1836 		 * at the end to guard against wild Unibus transfers.
1837 		 * Then do the write.
1838 		 */
1839 		for (i = 0; i < blk; i++)
1840 			*(int *) io++ = UBAMR_MRV | (btop(start) + i);
1841 		*(int *) io = 0;
1842 		ud->uda1_cmd.mscp_unit = ui->ui_slave;
1843 		ud->uda1_cmd.mscp_seq.seq_lbn = btop(start) + blkoff;
1844 		ud->uda1_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT;
1845 		if (udadumpcmd(M_OP_WRITE, ud, ui))
1846 			return (EIO);
1847 		start += blk << PGSHIFT;
1848 		num -= blk;
1849 	}
1850 	return (0);		/* made it! */
1851 }
1852 
1853 /*
1854  * Wait for some of the bits in `bits' to come on.  If the error bit
1855  * comes on, or ten seconds pass without response, return true (error).
1856  */
1857 udadumpwait(udaddr, bits)
1858 	register struct udadevice *udaddr;
1859 	register int bits;
1860 {
1861 	register int timo = todr() + 1000;
1862 
1863 	while ((udaddr->udasa & bits) == 0) {
1864 		if (udaddr->udasa & UDA_ERR) {
1865 			printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
1866 			return (1);
1867 		}
1868 		if (todr() >= timo) {
1869 			printf("timeout\ndump ");
1870 			return (1);
1871 		}
1872 	}
1873 	return (0);
1874 }
1875 
1876 /*
1877  * Feed a command to the UDA50, wait for its response, and return
1878  * true iff something went wrong.
1879  */
1880 udadumpcmd(op, ud, ui)
1881 	int op;
1882 	register struct uda1 *ud;
1883 	struct uba_device *ui;
1884 {
1885 	register struct udadevice *udaddr;
1886 	register int n;
1887 #define mp (&ud->uda1_rsp)
1888 
1889 	udaddr = (struct udadevice *) ui->ui_physaddr;
1890 	ud->uda1_cmd.mscp_opcode = op;
1891 	ud->uda1_cmd.mscp_msglen = MSCP_MSGLEN;
1892 	ud->uda1_rsp.mscp_msglen = MSCP_MSGLEN;
1893 	ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
1894 	ud->uda1_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT;
1895 	if (udaddr->udasa & UDA_ERR) {
1896 		printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
1897 		return (1);
1898 	}
1899 	n = udaddr->udaip;
1900 	n = todr() + 1000;
1901 	for (;;) {
1902 		if (todr() > n) {
1903 			printf("timeout\ndump ");
1904 			return (1);
1905 		}
1906 		if (ud->uda1_ca.ca_cmdint)
1907 			ud->uda1_ca.ca_cmdint = 0;
1908 		if (ud->uda1_ca.ca_rspint == 0)
1909 			continue;
1910 		ud->uda1_ca.ca_rspint = 0;
1911 		if (mp->mscp_opcode == (op | M_OP_END))
1912 			break;
1913 		printf("\n");
1914 		switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
1915 
1916 		case MSCPT_SEQ:
1917 			printf("sequential");
1918 			break;
1919 
1920 		case MSCPT_DATAGRAM:
1921 			mscp_decodeerror("uda", ui->ui_ctlr, mp);
1922 			printf("datagram");
1923 			break;
1924 
1925 		case MSCPT_CREDITS:
1926 			printf("credits");
1927 			break;
1928 
1929 		case MSCPT_MAINTENANCE:
1930 			printf("maintenance");
1931 			break;
1932 
1933 		default:
1934 			printf("unknown (type 0x%x)",
1935 				MSCP_MSGTYPE(mp->mscp_msgtc));
1936 			break;
1937 		}
1938 		printf(" ignored\ndump ");
1939 		ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
1940 	}
1941 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1942 		printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op,
1943 			mp->mscp_opcode, mp->mscp_status);
1944 		return (1);
1945 	}
1946 	return (0);
1947 #undef mp
1948 }
1949 
1950 /*
1951  * Return the size of a partition, if known, or -1 if not.
1952  */
1953 udasize(dev)
1954 	dev_t dev;
1955 {
1956 	register int unit = udaunit(dev);
1957 	register struct uba_device *ui;
1958 	register struct size *st;
1959 
1960 	if (unit >= NRA || (ui = udadinfo[unit]) == NULL ||
1961 	    ui->ui_alive == 0 || (ui->ui_flags & UNIT_ONLINE) == 0 ||
1962 	    ra_info[unit].ra_state != OPEN)
1963 		return (-1);
1964 	return ((int)udalabel[unit].d_partitions[udapart(dev)].p_size);
1965 }
1966 
1967 #ifdef COMPAT_42
1968 /*
1969  * Tables mapping unlabelled drives.
1970  */
1971 struct size {
1972 	daddr_t nblocks;
1973 	daddr_t blkoff;
1974 } ra25_sizes[8] = {
1975 	15884,	0,		/* A=blk 0 thru 15883 */
1976 	10032,	15884,		/* B=blk 15884 thru 49323 */
1977 	-1,	0,		/* C=blk 0 thru end */
1978 	0,	0,		/* D=blk 340670 thru 356553 */
1979 	0,	0,		/* E=blk 356554 thru 412489 */
1980 	0,	0,		/* F=blk 412490 thru end */
1981 	-1,	25916,		/* G=blk 49324 thru 131403 */
1982 	0,	0,		/* H=blk 131404 thru end */
1983 }, rx50_sizes[8] = {
1984 	800,	0,		/* A=blk 0 thru 799 */
1985 	0,	0,
1986 	-1,	0,		/* C=blk 0 thru end */
1987 	0,	0,
1988 	0,	0,
1989 	0,	0,
1990 	0,	0,
1991 	0,	0,
1992 }, rd52_sizes[8] = {
1993 	15884,	0,		/* A=blk 0 thru 15883 */
1994 	9766,	15884,		/* B=blk 15884 thru 25649 */
1995 	-1,	0,		/* C=blk 0 thru end */
1996 	0,	0,		/* D=unused */
1997 	0,	0,		/* E=unused */
1998 	0,	0,		/* F=unused */
1999 	-1,	25650,		/* G=blk 25650 thru end */
2000 	0,	0,		/* H=unused */
2001 }, rd53_sizes[8] = {
2002 	15884,	0,		/* A=blk 0 thru 15883 */
2003 	33440,	15884,		/* B=blk 15884 thru 49323 */
2004 	-1,	0,		/* C=blk 0 thru end */
2005 	0,	0,		/* D=unused */
2006 	33440,	0,		/* E=blk 0 thru 33439 */
2007 	-1,	33440,		/* F=blk 33440 thru end */
2008 	-1,	49324,		/* G=blk 49324 thru end */
2009 	-1,	15884,		/* H=blk 15884 thru end */
2010 }, ra60_sizes[8] = {
2011 	15884,	0,		/* A=sectors 0 thru 15883 */
2012 	33440,	15884,		/* B=sectors 15884 thru 49323 */
2013 	400176,	0,		/* C=sectors 0 thru 400175 */
2014 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
2015 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
2016 	350852,	49324,		/* F=sectors 49324 thru 400175 */
2017 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
2018 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
2019 }, ra80_sizes[8] = {
2020 	15884,	0,		/* A=sectors 0 thru 15883 */
2021 	33440,	15884,		/* B=sectors 15884 thru 49323 */
2022 	242606,	0,		/* C=sectors 0 thru 242605 */
2023 	0,	0,		/* D=unused */
2024 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
2025 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
2026 	192696,	49910,		/* G=sectors 49910 thru 242605 */
2027 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
2028 }, ra81_sizes[8] ={
2029 /*
2030  * These are the new standard partition sizes for ra81's.
2031  * An RA_COMPAT system is compiled with D, E, and F corresponding
2032  * to the 4.2 partitions for G, H, and F respectively.
2033  */
2034 #ifndef	UCBRA
2035 	15884,	0,		/* A=sectors 0 thru 15883 */
2036 	66880,	16422,		/* B=sectors 16422 thru 83301 */
2037 	891072,	0,		/* C=sectors 0 thru 891071 */
2038 #ifdef RA_COMPAT
2039 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
2040 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
2041 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
2042 #else
2043 	15884,	375564,		/* D=sectors 375564 thru 391447 */
2044 	307200,	391986,		/* E=sectors 391986 thru 699185 */
2045 	191352,	699720,		/* F=sectors 699720 thru 891071 */
2046 #endif RA_COMPAT
2047 	515508,	375564,		/* G=sectors 375564 thru 891071 */
2048 	291346,	83538,		/* H=sectors 83538 thru 374883 */
2049 
2050 /*
2051  * These partitions correspond to the sizes used by sites at Berkeley,
2052  * and by those sites that have received copies of the Berkeley driver
2053  * with deltas 6.2 or greater (11/15/83).
2054  */
2055 #else UCBRA
2056 
2057 	15884,	0,		/* A=sectors 0 thru 15883 */
2058 	33440,	15884,		/* B=sectors 15884 thru 49323 */
2059 	891072,	0,		/* C=sectors 0 thru 891071 */
2060 	15884,	242606,		/* D=sectors 242606 thru 258489 */
2061 	307200,	258490,		/* E=sectors 258490 thru 565689 */
2062 	325382,	565690,		/* F=sectors 565690 thru 891071 */
2063 	648466,	242606,		/* G=sectors 242606 thru 891071 */
2064 	193282,	49324,		/* H=sectors 49324 thru 242605 */
2065 
2066 #endif UCBRA
2067 };
2068 
2069 /*
2070  * Drive type index decoding table.  `ut_name' is null iff the
2071  * type is not known.
2072  */
2073 struct	udatypes {
2074 	char	*ut_name;	/* drive type name */
2075 	struct	size *ut_sizes;	/* partition tables */
2076 	int	ut_nsectors, ut_ntracks, ut_ncylinders;
2077 } udatypes[] = {
2078 	NULL,		NULL,
2079 		0, 0, 0,
2080 	"ra80",		ra80_sizes,	/* 1 = ra80 */
2081 		31, 14, 559,
2082 	"rc25-removable", ra25_sizes,	/* 2 = rc25-r */
2083 		42, 4, 302,
2084 	"rc25-fixed",	ra25_sizes,	/* 3 = rc25-f */
2085 		42, 4, 302,
2086 	"ra60",		ra60_sizes,	/* 4 = ra60 */
2087 		42, 4, 2382,
2088 	"ra81",		ra81_sizes,	/* 5 = ra81 */
2089 		51, 14, 1248,
2090 	NULL,		NULL,		/* 6 = ? */
2091 		0, 0, 0,
2092 	"rx50",		rx50_sizes,	/* 7 = rx50 */
2093 		10, 1, 80,
2094 	"rd52",		rd52_sizes,	/* 8 = rd52 */
2095 		18, 7, 480,
2096 	"rd53",		rd53_sizes,	/* 9 = rd53 */
2097 		18, 8, 963,
2098 };
2099 
2100 #define NTYPES (sizeof(udatypes) / sizeof(*udatypes))
2101 
2102 udamaptype(unit, lp)
2103 	int unit;
2104 	register struct disklabel *lp;
2105 {
2106 	register struct udatypes *ut;
2107 	register struct size *sz;
2108 	register struct partition *pp;
2109 	register char *p;
2110 	register int i;
2111 	register struct ra_info *ra = &ra_info[unit];
2112 
2113 	lp->d_secsize = 512;
2114 	lp->d_secperunit = ra->ra_dsize;
2115 	if ((u_long)ra->ra_type >= NTYPES) {
2116 		printf("ra%d: don't have a partition table for", unit);
2117 		mscp_printmedia(ra->ra_mediaid);
2118 		lp->d_nsectors = ra->ra_geom.rg_nsectors;
2119 		lp->d_ntracks = ra->ra_geom.rg_ntracks;
2120 		lp->d_ncylinders = ra->ra_geom.rg_ncyl;
2121 		printf(";\nusing (t,s,c)=(%d,%d,%d)\n", lp->d_nsectors,
2122 			lp->d_ntracks, lp->d_ncylinders);
2123 		lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
2124 		lp->d_typename[0] = 'r';
2125 		lp->d_typename[1] = 'a';
2126 		lp->d_typename[2] = '?';
2127 		lp->d_typename[3] = '?';
2128 		lp->d_typename[4] = 0;
2129 		lp->d_npartitions = 1;
2130 		lp->d_partitions[0].p_offset = 0;
2131 		lp->d_partitions[0].p_size = lp->d_secperunit;
2132 		return (0);
2133 	}
2134 	ut = &udatypes[ra->ra_type];
2135 	p = ut->ut_name;
2136 	for (i = 0; i < sizeof(lp->d_typename) - 1 && *p; i++)
2137 		lp->d_typename[i] = *p++;
2138 	lp->d_typename[i] = 0;
2139 	sz = ut->ut_sizes;
2140 	/* GET nsectors, ntracks, ncylinders FROM SAVED GEOMETRY? */
2141 	lp->d_nsectors = ut->ut_nsectors;
2142 	lp->d_ntracks = ut->ut_ntracks;
2143 	lp->d_ncylinders = ut->ut_ncylinders;
2144 	lp->d_npartitions = 8;
2145 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
2146 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8]; pp++, sz++) {
2147 		pp->p_offset = sz->blkoff;
2148 		if ((pp->p_size = sz->nblocks) == (u_long)-1)
2149 			pp->p_size = ra->ra_dsize - sz->blkoff;
2150 	}
2151 	return (1);
2152 }
2153 #endif /* COMPAT_42 */
2154 #endif /* NUDA > 0 */
2155