xref: /csrg-svn/sys/vax/uba/uda.c (revision 30773)
1 /*
2  *	@(#)uda.c	7.3 (Berkeley) 04/02/87
3  */
4 
5 /************************************************************************
6  *									*
7  *			Copyright (c) 1983 by				*
8  *		Digital Equipment Corporation, Maynard, MA		*
9  *			All rights reserved.				*
10  *									*
11  ************************************************************************/
12 /*
13  * uda.c - UDA50A Driver
14  *
15  * decvax!rich
16  */
17 
18 #define	COMPAT_42
19 #define	DEBUG
20 #define	UDADEVNUM	(9)		/* entry in bdevsw */
21 #include "ra.h"
22 #if NUDA > 0
23 /*
24  * UDA50/RAxx disk device driver
25  *
26  * Restrictions:
27  *      Unit numbers must be less than 8.
28  */
29 #include "../machine/pte.h"
30 
31 #include "param.h"
32 #include "systm.h"
33 #include "buf.h"
34 #include "conf.h"
35 #include "dir.h"
36 #include "file.h"
37 #include "ioctl.h"
38 #include "user.h"
39 #include "map.h"
40 #include "vm.h"
41 #include "dkstat.h"
42 #include "cmap.h"
43 #include "uio.h"
44 #include "disklabel.h"
45 #include "syslog.h"
46 #include "stat.h"
47 
48 #include "../vax/cpu.h"
49 #include "ubareg.h"
50 #include "ubavar.h"
51 #include "../vax/mtpr.h"
52 
53 #define TENSEC	(1000)
54 
55 #define NRSPL2  3               /* log2 number of response packets */
56 #define NCMDL2  3               /* log2 number of command packets */
57 #define NRSP    (1<<NRSPL2)
58 #define NCMD    (1<<NCMDL2)
59 #define	UDABURST	4	/* default for DMA burst size */
60 
61 #include "../vaxuba/udareg.h"
62 #include "../vax/mscp.h"
63 
64 
65 struct uda_softc {
66 	short   sc_state;       /* state of controller */
67 	short   sc_mapped;      /* Unibus map allocated for uda struct? */
68 	int     sc_ubainfo;     /* Unibus mapping info */
69 	struct uda *sc_uda;     /* Unibus address of uda struct */
70 	int     sc_ivec;        /* interrupt vector address */
71 	short   sc_credits;     /* transfer credits */
72 	short   sc_lastcmd;     /* pointer into command ring */
73 	short   sc_lastrsp;     /* pointer into response ring */
74 } uda_softc[NUDA];
75 struct uda {
76 	struct udaca    uda_ca;         /* communications area */
77 	struct mscp     uda_rsp[NRSP];  /* response packets */
78 	struct mscp     uda_cmd[NCMD];  /* command packets */
79 } uda[NUDA];
80 
81 #define udunit(dev)	(minor(dev) >> 3)
82 #define udpart(dev)	(minor(dev) & 07)
83 #define udminor(unit, part)	(((unit) << 3) | (part))
84 
85 struct	ra_info {
86 	daddr_t		radsize;	/* Max user size form online pkt */
87 	unsigned	ratype;		/* Drive type int field  */
88 	unsigned	rastatus;	/* Command status from */
89 					/* last onlin or GTUNT */
90 	int		rastate;   	/* open/closed state */
91 	u_long		openpart;	/* partitions open */
92 	u_long		bopenpart;	/* block partitions open */
93 	u_long		copenpart;	/* characters partitions open */
94 } ra_info[NRA];
95 
96 struct  uba_ctlr *udminfo[NUDA];
97 struct  uba_device *uddinfo[NRA];
98 struct  uba_device *udip[NUDA][8];      /* 8 == max number of drives */
99 struct  disklabel udlabel[NRA];
100 struct  buf rudbuf[NRA];
101 struct  buf udutab[NRA];
102 struct  buf udwtab[NUDA];               /* I/O wait queue, per controller */
103 
104 
105 int     udamicro[NUDA];         /* to store microcode level */
106 int     udaburst[NUDA] = { 0 };	/* DMA burst size, 0 is default */
107 
108 
109 /*
110  * Controller states
111  */
112 #define S_IDLE  0               /* hasn't been initialized */
113 #define S_STEP1 1               /* doing step 1 init */
114 #define S_STEP2 2               /* doing step 2 init */
115 #define S_STEP3 3               /* doing step 3 init */
116 #define S_SCHAR 4               /* doing "set controller characteristics" */
117 #define S_RUN   5               /* running */
118 
119 /*
120  * Software state, per drive
121  */
122 #define	CLOSED		0
123 #define	WANTOPEN	1
124 #define	RDLABEL		2
125 #define	OPEN		3
126 #define	OPENRAW		4
127 
128 int     udaerror = 0;                   /* causes hex dump of packets */
129 int     udadebug = 0;
130 int	uda_cp_wait = 0;		/* Something to wait on for command */
131 					/* packets and or credits. */
132 int	wakeup();
133 extern	int	hz;			/* Should find the right include */
134 #ifdef	DEBUG
135 #define printd  if (udadebug) printf
136 #define	printd10	if(udadebug >= 10) printf
137 #endif
138 #define mprintf printf			/* temporary JG hack until Rich fixes*/
139 
140 int     udprobe(), udslave(), udattach(), udintr(), udstrategy();
141 struct  mscp *udgetcp();
142 
143 u_short udstd[] = { 0772150, 0772550, 0777550, 0 };
144 struct  uba_driver udadriver =
145  { udprobe, udslave, udattach, 0, udstd, "ra", uddinfo, "uda", udminfo, 0 };
146 
147 #define b_qsize         b_resid         /* queue size per drive, in udutab */
148 #define b_ubinfo        b_resid         /* Unibus mapping info, per buffer */
149 
150 udprobe(reg, ctlr)
151 	caddr_t reg;
152 	int ctlr;
153 {
154 	register int br, cvec;
155 	register struct uda_softc *sc = &uda_softc[ctlr];
156 	struct udadevice *udaddr;
157 
158 	int	cur_time;
159 
160 #ifdef lint
161 	br = 0; cvec = br; br = cvec;
162 	udreset(0); udintr(0);
163 #endif
164 	udaddr = (struct udadevice *) reg;
165 
166 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
167 #if VAX630
168 	if (cpu == VAX_630) {
169 		br = 0x15;
170 		cvec = sc->sc_ivec;
171  		return(sizeof (struct udadevice));
172 	}
173 #endif
174 	udaddr->udaip = 0;              /* start initialization */
175 
176 	cur_time = mfpr(TODR);			/* Time of day */
177 	while(cur_time + TENSEC > mfpr(TODR)){	/* wait for at most 10 secs */
178 		if((udaddr->udasa & UDA_STEP1) != 0)
179 			break;
180 	}
181 	if(cur_time + TENSEC <= mfpr(TODR))
182 		return(0);		/* Not a uda or it won't init as it  */
183 					/* should within ten seconds.  */
184 	udaddr->udasa=UDA_ERR|(NCMDL2<<11)|(NRSPL2<<8)|UDA_IE|(sc->sc_ivec/4);
185 	while((udaddr->udasa&UDA_STEP2)==0)
186 		DELAY(1000);		/* intr should have */
187 					/*   have happened by now */
188 
189 	return(sizeof (struct udadevice));
190 }
191 
192 /* ARGSUSED */
193 udslave(ui, reg)
194 	struct uba_device *ui;
195 	caddr_t reg;
196 {
197 	register struct uba_ctlr *um = udminfo[ui->ui_ctlr];
198 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
199 	struct udadevice *udaddr;
200 	struct	mscp	*mp;
201 	int	i;			/* Something to write into to start */
202 					/* the uda polling */
203 
204 
205 	udaddr = (struct udadevice *)um->um_addr;
206 	if(sc->sc_state != S_RUN){
207 		if(!udinit(ui->ui_ctlr))
208 			return(0);
209 	}
210 	/* Here we will wait for the controller */
211 	/* to come into the run state or go idle.  If we go idle we are in */
212 	/* touble and I don't yet know what to do so I will punt */
213 	while(sc->sc_state != S_RUN && sc->sc_state != S_IDLE);	/* spin */
214 	if(sc->sc_state == S_IDLE){	/* The Uda failed to initialize */
215 		printf("UDA failed to init\n");
216 		return(0);
217 	}
218 	/* The controller is up so let see if the drive is there! */
219 	if(0 == (mp = udgetcp(um))){	/* ditto */
220 		printf("UDA can't get command packet\n");
221 		return(0);
222 	}
223 	mp->mscp_opcode = M_OP_GTUNT;	/* This should give us the drive type*/
224 	mp->mscp_unit = ui->ui_slave;
225 	mp->mscp_cmdref = (long) ui->ui_slave;
226 #ifdef	DEBUG
227 	printd("uda%d Get unit status slave %d\n",ui->ui_ctlr,ui->ui_slave);
228 #endif
229 	ra_info[ui->ui_unit].rastatus = 0;	/* set to zero */
230 	udip[ui->ui_ctlr][ui->ui_slave] = ui;
231 	*((long *) mp->mscp_dscptr ) |= UDA_OWN | UDA_INT;/* maybe we should poll*/
232 	i = udaddr->udaip;
233 #ifdef lint
234 	i = i;
235 #endif
236 	while(!ra_info[ui->ui_unit].rastatus);  /* Wait for some status */
237 	udip[ui->ui_ctlr][ui->ui_slave] = 0;
238 	if(!ra_info[ui->ui_unit].ratype)	/* packet from a GTUNT */
239 		return(0);		/* Failed No such drive */
240 	else
241 		return(1);		/* Got it and it is there */
242 }
243 
244 udattach(ui)
245 	register struct uba_device *ui;
246 {
247 	register struct uba_ctlr *um = ui->ui_mi ;
248 	struct udadevice *udaddr = (struct udadevice *) um->um_addr;
249 	register struct	mscp	*mp;
250 	register unit = ui->ui_unit;
251 	int	i;			/* Something to write into to start */
252 					/* the uda polling */
253 	if (ui->ui_dk >= 0)
254 		dk_mspw[ui->ui_dk] = 1.0 / (60 * 31 * 256);     /* approx */
255 	ui->ui_flags = 0;
256 	udip[ui->ui_ctlr][ui->ui_slave] = ui;
257 	/* check to see if the drive is a available if it is bring it online */
258 	/* if not then just return.  open will try an online later */
259 	if(ra_info[unit].rastatus != M_ST_AVLBL)
260 		return;			/* status was set by a GTUNT */
261 	if(0 == (mp = udgetcp(um))){	/* ditto */
262 		printf("UDA can't get command packet\n");
263 		return;
264 	}
265 	mp->mscp_opcode = M_OP_ONLIN;
266 	mp->mscp_unit = ui->ui_slave;
267 	mp->mscp_cmdref = (long) ui->ui_slave;
268 #ifdef	DEBUG
269 	printd("uda%d ONLIN slave %d\n",ui->ui_ctlr,ui->ui_slave);
270 #endif
271 	*((long *) mp->mscp_dscptr ) |= UDA_OWN | UDA_INT;
272 	i = udaddr->udaip;
273 #ifdef	lint
274 	i = i;
275 #endif
276 	for (i = 1000; ui->ui_flags == 0 && ra_info[unit].ratype != 0; ) {
277 		if (--i == 0)
278 			break;
279 		DELAY(1000);
280 	}
281 	/*
282 	 * Try to read pack label.
283 	 */
284 	if (rainit(ui, 0) == 0) {
285 		printf("ra%d: %s\n", unit, udlabel[unit].d_typename);
286 #ifdef notyet
287 		addswap(makedev(UDADEVNUM, udminor(unit, 0)), &udlabel[unit]);
288 #endif
289 	} else
290 		printf("ra%d: offline\n", unit);
291 }
292 
293 /*
294  * Open a UDA.  Initialize the device and
295  * set the unit online.
296  */
297 udopen(dev, flag, fmt)
298 	dev_t dev;
299 	int flag, fmt;
300 {
301 	register int unit;
302 	register struct uba_device *ui;
303 	register struct uda_softc *sc;
304 	register struct disklabel *lp;
305 	register struct partition *pp;
306 	struct ra_info *ra = &ra_info[unit];
307 	int s, i, part, mask = 1 << part;
308 	daddr_t start, end;
309 
310 	unit = udunit(dev);
311 	part = udpart(dev);
312 	if (unit >= NRA || (ui = uddinfo[unit]) == 0 || ui->ui_alive == 0)
313 		return (ENXIO);
314 	sc = &uda_softc[ui->ui_ctlr];
315 	lp = &udlabel[unit];
316 	s = spl5();
317 	if (sc->sc_state != S_RUN) {
318 		if (sc->sc_state == S_IDLE)
319 			if(!udinit(ui->ui_ctlr)){
320 				printf("uda: Controller failed to init\n");
321 				(void) splx(s);
322 				return(ENXIO);
323 			}
324 		/* wait for initialization to complete */
325 		timeout(wakeup,(caddr_t)ui->ui_mi,11*hz);	/* to be sure*/
326 		sleep((caddr_t)ui->ui_mi, 0);
327 		if (sc->sc_state != S_RUN)
328 		{
329 			(void) splx(s); /* added by Rich */
330 			return (EIO);
331 		}
332 	}
333 	(void) splx(s);
334 	if (ui->ui_flags == 0)
335 		rainit(ui, flag);
336 	if (ui->ui_flags == 0)
337 		return(ENXIO);  /* Didn't go online */
338 
339 	if (part >= lp->d_npartitions)
340 		return (ENXIO);
341 	/*
342 	 * Warn if a partion is opened
343 	 * that overlaps another partition which is open
344 	 * unless one is the "raw" partition (whole disk).
345 	 */
346 #define	RAWPART		2		/* 'c' partition */	/* XXX */
347 	if ((ra->openpart & mask) == 0 &&
348 	    part != RAWPART) {
349 		pp = &lp->d_partitions[part];
350 		start = pp->p_offset;
351 		end = pp->p_offset + pp->p_size;
352 		for (pp = lp->d_partitions;
353 		     pp < &lp->d_partitions[lp->d_npartitions]; pp++) {
354 			if (pp->p_offset + pp->p_size <= start ||
355 			    pp->p_offset >= end)
356 				continue;
357 			if (pp - lp->d_partitions == RAWPART)
358 				continue;
359 			if (ra->openpart &
360 			    (1 << (pp - lp->d_partitions)))
361 				log(LOG_WARNING,
362 				    "ra%d%c: overlaps open partition (%c)\n",
363 				    unit, part + 'a',
364 				    pp - lp->d_partitions + 'a');
365 		}
366 	}
367 	switch (fmt) {
368 	case S_IFCHR:
369 		ra->copenpart |= mask;
370 		break;
371 	case S_IFBLK:
372 		ra->bopenpart |= mask;
373 		break;
374 	}
375 	ra->openpart |= mask;
376 	return (0);
377 }
378 
379 /* ARGSUSED */
380 udclose(dev, flags, fmt)
381 	dev_t dev;
382 	int flags, fmt;
383 {
384 	register int unit = udunit(dev);
385 	register struct uda_softc *sc;
386 	struct uba_ctlr *um;
387 	register struct ra_info *ra = &ra_info[unit];
388 	int s, mask = (1 << udpart(dev));
389 
390 	um = udminfo[unit];
391 	sc = &uda_softc[um->um_ctlr];
392 	switch (fmt) {
393 	case S_IFCHR:
394 		ra->copenpart &= ~mask;
395 		break;
396 	case S_IFBLK:
397 		ra->bopenpart &= ~mask;
398 		break;
399 	}
400 	if (((ra->copenpart | ra->bopenpart) & mask) == 0)
401 		ra->openpart &= ~mask;
402 	/*
403 	 * Should wait for I/O to complete on this partition
404 	 * even if others are open, but wait for work on blkflush().
405 	 */
406 	if (ra->openpart == 0) {
407 		s = spl5();
408 		while (udutab[unit].b_actf)
409 			sleep((caddr_t)&udutab[unit], PZERO - 1);
410 		splx(s);
411 		ra->rastate = CLOSED;
412 	}
413 	return (0);
414 }
415 
416 /*
417  * Initialize a UDA.  Set up UBA mapping registers,
418  * initialize data structures, and start hardware
419  * initialization sequence.
420  */
421 udinit(d)
422 	int d;
423 {
424 	register struct uda_softc *sc;
425 	register struct uda *ud;
426 	struct udadevice *udaddr;
427 	struct uba_ctlr *um;
428 
429 	sc = &uda_softc[d];
430 	um = udminfo[d];
431 	um->um_tab.b_active++;
432 	ud = &uda[d];
433 	udaddr = (struct udadevice *)um->um_addr;
434 	if (sc->sc_mapped == 0) {
435 		/*
436 		 * Map the communications area and command
437 		 * and response packets into Unibus address
438 		 * space.
439 		 */
440 		sc->sc_ubainfo = uballoc(um->um_ubanum, (caddr_t)ud,
441 		    sizeof (struct uda), 0);
442 		sc->sc_uda = (struct uda *)(sc->sc_ubainfo & 0x3ffff);
443 		sc->sc_mapped = 1;
444 	}
445 
446 	/*
447 	 * Start the hardware initialization sequence.
448 	 */
449 
450 	if (udaburst[d] == 0)
451 		udaburst[d] = UDABURST;
452  	udaddr->udaip = 0;              /* start initialization */
453 
454 	while((udaddr->udasa & UDA_STEP1) == 0){
455 		if(udaddr->udasa & UDA_ERR)
456 			return(0);	/* CHECK */
457 	}
458 	udaddr->udasa=UDA_ERR|(NCMDL2<<11)|(NRSPL2<<8)|UDA_IE|(sc->sc_ivec/4);
459 	/*
460 	 * Initialization continues in interrupt routine.
461 	 */
462 	sc->sc_state = S_STEP1;
463 	sc->sc_credits = 0;
464 	return(1);
465 }
466 
467 /*
468  * Initialize a drive:
469  * bring on line and read in pack label.
470  */
471 rainit(ui, flags)
472 	register struct uba_device *ui;
473 {
474 	register struct mscp *mp;
475 	register struct disklabel *lp;
476 	register struct uda_softc *sc;
477 	register unit = ui->ui_unit;
478 	struct udadevice *udaddr;
479 	char *msg, *readdisklabel();
480 	int s, i, error = 0;
481 	extern int cold;
482 
483 	lp = &udlabel[unit];
484 	sc = &uda_softc[ui->ui_ctlr];
485 
486 	/* check to see if the device is really there. */
487 	/* this code was taken from Fred Canters 11 driver */
488 	udaddr = (struct udadevice *) ui->ui_mi->um_addr;
489 
490 	ra_info[unit].rastate = WANTOPEN;
491 	s = spl5();
492 	while(0 ==(mp = udgetcp(ui->ui_mi))){
493 		uda_cp_wait++;
494 		sleep((caddr_t)&uda_cp_wait,PSWP+1);
495 		uda_cp_wait--;
496 	}
497 	mp->mscp_opcode = M_OP_ONLIN;
498 	mp->mscp_unit = ui->ui_slave;
499 		/* need to sleep on something */
500 	mp->mscp_cmdref = (long) & ra_info[unit].ratype;
501 #ifdef	DEBUG
502 	printd("uda: bring unit %d online\n",unit);
503 #endif
504 	*((long *) mp->mscp_dscptr ) |= UDA_OWN | UDA_INT ;
505 	i = udaddr->udaip;
506 #ifdef	lint
507 	i = i;
508 #endif
509 		/* make sure we wake up */
510 	if (cold) {
511 		(void) splx(s);
512 		for (i = 10*1000; ra_info[unit].rastate == WANTOPEN && --i; )
513 			DELAY(1000);
514 	} else {
515 		timeout(wakeup,(caddr_t) mp->mscp_cmdref,10 * hz);
516 		sleep((caddr_t) mp->mscp_cmdref,PSWP+1); /*wakeup in udrsp() */
517 		(void) splx(s);
518 	}
519 	if (ra_info[unit].rastate != OPENRAW)
520 		return (EIO);
521 
522 	if (flags & O_NDELAY)
523 		return (0);
524 	ra_info[unit].rastate = RDLABEL;
525 	/*
526 	 * Set up default sizes until we've read the label,
527 	 * or longer if there isn't one there.
528 	 */
529 	lp->d_secsize = DEV_BSIZE;
530 	lp->d_secperunit = ra_info[unit].radsize;
531 	lp->d_npartitions = 1;
532 	lp->d_partitions[0].p_size = lp->d_secperunit;
533 	lp->d_partitions[0].p_offset = 0;
534 	/*
535 	 * Read pack label.
536 	 */
537 	if (msg = readdisklabel(udminor(unit, 0), udstrategy, lp)) {
538 		log(LOG_ERR, "ra%d: no disk label\n", unit);
539 #ifdef COMPAT_42
540 		if (udmaptype(unit, lp))
541 			ra_info[unit].rastate = OPEN;
542 		else
543 #else
544 			ra_info[unit].rastate = OPENRAW;
545 #endif
546 		ra_info[unit].rastate = OPENRAW;
547 	} else
548 		ra_info[unit].rastate = OPEN;
549 	return (error);
550 }
551 
552 udstrategy(bp)
553 	register struct buf *bp;
554 {
555 	register struct uba_device *ui;
556 	register struct uba_ctlr *um;
557 	register struct buf *dp;
558 	register struct disklabel *lp;
559 	register int unit;
560 	struct uda_softc *sc;
561 	int xunit = udpart(bp->b_dev);
562 	daddr_t sz, maxsz;
563 	int s;
564 
565 	unit = udunit(bp->b_dev);
566 	if (unit >= NRA) {
567 		bp->b_error = ENXIO;
568 		goto bad;
569 	}
570 	ui = uddinfo[unit];
571 	lp = &udlabel[unit];
572 	sc = &uda_softc[ui->ui_ctlr];
573 	um = ui->ui_mi;
574 	if (ui == 0 || ui->ui_alive == 0 || ra_info[unit].rastate == CLOSED) {
575 		bp->b_error = ENXIO;
576 		goto bad;
577 	}
578 	if (ra_info[unit].rastate < OPEN)
579 		goto q;
580 	if ((ra_info[unit].openpart & (1 << xunit)) == 0) {
581 		bp->b_error = ENODEV;
582 		goto bad;
583 	}
584 	maxsz = lp->d_partitions[xunit].p_size;
585 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
586 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
587 		if (bp->b_blkno == maxsz) {
588 			bp->b_resid = bp->b_bcount;
589 			goto done;
590 		}
591 		sz = maxsz - bp->b_blkno;
592 		if (sz <= 0) {
593 			bp->b_error = EINVAL;
594 			goto bad;
595 		}
596 		bp->b_bcount = sz << DEV_BSHIFT;
597 	}
598 q:
599 	s = spl5();
600 	/*
601 	 * Link the buffer onto the drive queue
602 	 */
603 	dp = &udutab[ui->ui_unit];
604 	if (dp->b_actf == 0)
605 		dp->b_actf = bp;
606 	else
607 		dp->b_actl->av_forw = bp;
608 	dp->b_actl = bp;
609 	bp->av_forw = 0;
610 	/*
611 	 * Link the drive onto the controller queue
612 	 */
613 	if (dp->b_active == 0) {
614 		dp->b_forw = NULL;
615 		if (um->um_tab.b_actf == NULL)
616 			um->um_tab.b_actf = dp;
617 		else
618 			um->um_tab.b_actl->b_forw = dp;
619 		um->um_tab.b_actl = dp;
620 		dp->b_active = 1;
621 	}
622 	if (um->um_tab.b_active == 0) {
623 #if defined(VAX750)
624 		if (cpu == VAX_750
625 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
626 			if (um->um_ubinfo != 0) {
627 				printd("udastrat: ubinfo 0x%x\n",um->um_ubinfo);
628 			} else
629 				um->um_ubinfo =
630 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
631 					UBA_NEEDBDP);
632 		}
633 #endif
634 		(void) udstart(um);
635 	}
636 	splx(s);
637 	return;
638 
639 bad:
640 	bp->b_flags |= B_ERROR;
641 done:
642 	iodone(bp);
643 	return;
644 }
645 
646 udstart(um)
647 	register struct uba_ctlr *um;
648 {
649 	register struct buf *bp, *dp;
650 	register struct mscp *mp;
651 	register struct uda_softc *sc;
652 	register struct uba_device *ui;
653 	struct disklabel *lp;
654 	struct udadevice *udaddr;
655 	struct uda *ud = &uda[um->um_ctlr];
656 	daddr_t sz;
657 	int i;
658 
659 	sc = &uda_softc[um->um_ctlr];
660 
661 loop:
662 	if ((dp = um->um_tab.b_actf) == NULL) {
663 
664 		um->um_tab.b_active = 0;
665 		/* Check for response ring transitions lost in the
666 		 * Race condition
667 		 */
668 		for (i = sc->sc_lastrsp;; i++) {
669 			i %= NRSP;
670 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
671 				break;
672 			udrsp(um, ud, sc, i);
673 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
674 		}
675 		sc->sc_lastrsp = i;
676 		return (0);
677 	}
678 	if ((bp = dp->b_actf) == NULL) {
679 		/*
680 		 * No more requests for this drive, remove
681 		 * from controller queue and look at next drive.
682 		 * We know we're at the head of the controller queue.
683 		 */
684 		dp->b_active = 0;
685 		um->um_tab.b_actf = dp->b_forw;
686 		if (ra_info[dp - udutab].openpart == 0)
687 			wakeup((caddr_t)dp);
688 		goto loop;		/* Need to check for loop */
689 	}
690 	um->um_tab.b_active++;
691 	udaddr = (struct udadevice *)um->um_addr;
692 	if ((udaddr->udasa&UDA_ERR) || sc->sc_state != S_RUN) {
693 		harderr(bp, "ra");
694 		mprintf("Uda%d udasa %o, state %d\n",um->um_ctlr , udaddr->udasa&0xffff, sc->sc_state);
695 		(void)udinit(um->um_ctlr);
696 		/* SHOULD REQUEUE OUTSTANDING REQUESTS, LIKE UDRESET */
697 		return (0);
698 	}
699 	ui = uddinfo[udunit(bp->b_dev)];
700 	lp = &udlabel[ui->ui_unit];
701 	if (ui->ui_flags == 0) {        /* not online */
702 		if ((mp = udgetcp(um)) == NULL){
703 			return (0);
704 		}
705 		mp->mscp_opcode = M_OP_ONLIN;
706 		mp->mscp_unit = ui->ui_slave;
707 		dp->b_active = 2;
708 		um->um_tab.b_actf = dp->b_forw; /* remove from controller q */
709 #ifdef	DEBUG
710 		printd("uda: bring unit %d online\n", ui->ui_slave);
711 #endif
712 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
713 		if (udaddr->udasa&UDA_ERR)
714 			printf("Uda (%d) Error (%x)\n",um->um_ctlr , udaddr->udasa&0xffff);
715 		i = udaddr->udaip;
716 		goto loop;
717 	}
718 	switch (cpu) {
719 	case VAX_8600:
720 	case VAX_780:
721 		i = UBA_NEEDBDP|UBA_CANTWAIT;
722 		break;
723 
724 	case VAX_750:
725 		i = um->um_ubinfo|UBA_HAVEBDP|UBA_CANTWAIT;
726 		break;
727 
728 	case VAX_730:
729 	case VAX_630:
730 		i = UBA_CANTWAIT;
731 		break;
732 	}
733 	if ((i = ubasetup(um->um_ubanum, bp, i)) == 0)
734 		return(1);
735 	if ((mp = udgetcp(um)) == NULL) {
736 #if defined(VAX750)
737 		if (cpu == VAX_750)
738 			i &= 0xfffffff;         /* mask off bdp */
739 #endif
740 		ubarelse(um->um_ubanum,&i);
741 		return(0);
742 	}
743 	mp->mscp_cmdref = (long)bp;     /* pointer to get back */
744 	mp->mscp_opcode = bp->b_flags&B_READ ? M_OP_READ : M_OP_WRITE;
745 	mp->mscp_unit = ui->ui_slave;
746 	mp->mscp_buffer = (i & 0x3ffff) | (((i>>28)&0xf)<<24);
747 #if defined(VAX750)
748 	if (cpu == VAX_750)
749 		i &= 0xfffffff;         /* mask off bdp */
750 #endif
751 	bp->b_ubinfo = i;               /* save mapping info */
752 	i = udpart(bp->b_dev);
753 	mp->mscp_lbn = bp->b_blkno +
754 	    lp->d_partitions[i].p_offset;
755 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
756 	if (bp->b_blkno + sz > lp->d_partitions[i].p_size)
757 		mp->mscp_bytecnt = (lp->d_partitions[i].p_size - bp->b_blkno) >>
758 		    DEV_BSHIFT;
759 	else
760 		mp->mscp_bytecnt = bp->b_bcount;
761 	*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
762 	if (udaddr->udasa&UDA_ERR)
763 		printf("Uda(%d) udasa (%x)\n",um->um_ctlr , udaddr->udasa&0xffff);
764 	i = udaddr->udaip;              /* initiate polling */
765 	dp->b_qsize++;
766 	if (ui->ui_dk >= 0) {
767 		dk_busy |= 1<<ui->ui_dk;
768 		dk_xfer[ui->ui_dk]++;
769 		dk_wds[ui->ui_dk] += bp->b_bcount>>6;
770 	}
771 
772 	/*
773 	 * Move drive to the end of the controller queue
774 	 */
775 	if (dp->b_forw != NULL) {
776 		um->um_tab.b_actf = dp->b_forw;
777 		um->um_tab.b_actl->b_forw = dp;
778 		um->um_tab.b_actl = dp;
779 		dp->b_forw = NULL;
780 	}
781 	/*
782 	 * Move buffer to I/O wait queue
783 	 */
784 	dp->b_actf = bp->av_forw;
785 	dp = &udwtab[um->um_ctlr];
786 	bp->av_forw = dp;
787 	bp->av_back = dp->av_back;
788 	dp->av_back->av_forw = bp;
789 	dp->av_back = bp;
790 	goto loop;
791 }
792 
793 /*
794  * UDA interrupt routine.
795  */
796 udintr(d)
797 	register d;
798 {
799 	struct uba_ctlr *um = udminfo[d];
800 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
801 	struct buf *bp;
802 	register int i;
803 	register struct uda_softc *sc = &uda_softc[d];
804 	register struct uda *ud = &uda[d];
805 	struct uda *uud;
806 	register struct mscp *mp;
807 
808 #ifdef	DEBUG
809 	printd10("udintr: state %d, udasa %o\n", sc->sc_state, udaddr->udasa);
810 #endif
811 #ifdef VAX630
812 	(void) spl5();
813 #endif
814 	switch (sc->sc_state) {
815 	case S_IDLE:
816 		printf("uda%d: random interrupt ignored\n", d);
817 		return;
818 
819 	case S_STEP1:
820 #define STEP1MASK       0174377
821 #define STEP1GOOD       (UDA_STEP2|UDA_IE|(NCMDL2<<3)|NRSPL2)
822 		if ((udaddr->udasa&STEP1MASK) != STEP1GOOD) {
823 			sc->sc_state = S_IDLE;
824 			wakeup((caddr_t)um);
825 			return;
826 		}
827 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)|
828 		    ((cpu == VAX_780) || (cpu == VAX_8600) ? UDA_PI : 0);
829 		sc->sc_state = S_STEP2;
830 		return;
831 
832 	case S_STEP2:
833 #define STEP2MASK       0174377
834 #define STEP2GOOD       (UDA_STEP3|UDA_IE|(sc->sc_ivec/4))
835 		if ((udaddr->udasa&STEP2MASK) != STEP2GOOD) {
836 			sc->sc_state = S_IDLE;
837 			wakeup((caddr_t)um);
838 			return;
839 		}
840 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)>>16;
841 		sc->sc_state = S_STEP3;
842 		return;
843 
844 	case S_STEP3:
845 #define STEP3MASK       0174000
846 #define STEP3GOOD       UDA_STEP4
847 		if ((udaddr->udasa&STEP3MASK) != STEP3GOOD) {
848 			sc->sc_state = S_IDLE;
849 			wakeup((caddr_t)um);
850 			return;
851 		}
852 		udamicro[d] = udaddr->udasa;
853 		log(LOG_INFO, "uda%d: version %d model %d\n", d,
854 		    udamicro[d] & 0xf, (udamicro[d] >> 4) & 0xf);
855 		/*
856 		 * Requesting the error status (|= 2)
857 		 * may hang older controllers.
858 		 */
859 		i = UDA_GO | (udaerror? 2 : 0);
860 		if (udaburst[d])
861 			i |= (udaburst[d] - 1) << 2;
862 		udaddr->udasa = i;
863 		udaddr->udasa = UDA_GO;
864 		sc->sc_state = S_SCHAR;
865 
866 		/*
867 		 * Initialize the data structures.
868 		 */
869 		uud = sc->sc_uda;
870 		for (i = 0; i < NRSP; i++) {
871 			ud->uda_ca.ca_rspdsc[i] = UDA_OWN|UDA_INT|
872 				(long)&uud->uda_rsp[i].mscp_cmdref;
873 			ud->uda_rsp[i].mscp_dscptr = &ud->uda_ca.ca_rspdsc[i];
874 			ud->uda_rsp[i].mscp_header.uda_msglen = mscp_msglen;
875 		}
876 		for (i = 0; i < NCMD; i++) {
877 			ud->uda_ca.ca_cmddsc[i] = UDA_INT|
878 				(long)&uud->uda_cmd[i].mscp_cmdref;
879 			ud->uda_cmd[i].mscp_dscptr = &ud->uda_ca.ca_cmddsc[i];
880 			ud->uda_cmd[i].mscp_header.uda_msglen = mscp_msglen;
881 		}
882 		bp = &udwtab[d];
883 		bp->av_forw = bp->av_back = bp;
884 		sc->sc_lastcmd = 1;
885 		sc->sc_lastrsp = 0;
886 		mp = &uda[um->um_ctlr].uda_cmd[0];
887 		mp->mscp_unit = mp->mscp_modifier = 0;
888 		mp->mscp_flags = 0;
889 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
890 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
891 		mp->mscp_opcode = M_OP_STCON;
892 		mp->mscp_cntflgs = M_CF_ATTN|M_CF_MISC|M_CF_THIS;
893 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
894 		i = udaddr->udaip;      /* initiate polling */
895 		return;
896 
897 	case S_SCHAR:
898 	case S_RUN:
899 		break;
900 
901 	default:
902 		printf("uda%d: interrupt in unknown state %d ignored\n",
903 			d, sc->sc_state);
904 		return;
905 	}
906 
907 	if (udaddr->udasa&UDA_ERR) {
908 		printf("uda(%d): fatal error (%o)\n", d, udaddr->udasa&0xffff);
909 		udaddr->udaip = 0;
910 		wakeup((caddr_t)um);
911 	}
912 
913 	/*
914 	 * Check for a buffer purge request.
915 	 */
916 	if (ud->uda_ca.ca_bdp) {
917 #ifdef	DEBUG
918 		printd("uda: purge bdp %d\n", ud->uda_ca.ca_bdp);
919 #endif
920 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
921 		ud->uda_ca.ca_bdp = 0;
922 		udaddr->udasa = 0;      /* signal purge complete */
923 	}
924 
925 	/*
926 	 * Check for response ring transition.
927 	 */
928 	if (ud->uda_ca.ca_rspint) {
929 		ud->uda_ca.ca_rspint = 0;
930 		for (i = sc->sc_lastrsp;; i++) {
931 			i %= NRSP;
932 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
933 				break;
934 			udrsp(um, ud, sc, i);
935 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
936 		}
937 		sc->sc_lastrsp = i;
938 	}
939 
940 	/*
941 	 * Check for command ring transition.
942 	 */
943 	if (ud->uda_ca.ca_cmdint) {
944 #ifdef	DEBUG
945 		printd("uda: command ring transition\n");
946 #endif
947 		ud->uda_ca.ca_cmdint = 0;
948 	}
949 	if(uda_cp_wait)
950 		wakeup((caddr_t)&uda_cp_wait);
951 	(void) udstart(um);
952 }
953 
954 /*
955  * Process a response packet
956  */
957 udrsp(um, ud, sc, i)
958 	register struct uba_ctlr *um;
959 	register struct uda *ud;
960 	register struct uda_softc *sc;
961 	int i;
962 {
963 	register struct mscp *mp;
964 	register struct uba_device *ui;
965 	register int unit;
966 	struct buf *dp, *bp, nullbp;
967 	int st;
968 
969 	mp = &ud->uda_rsp[i];
970 	mp->mscp_header.uda_msglen = mscp_msglen;
971 	sc->sc_credits += mp->mscp_header.uda_credits & 0xf;  /* just 4 bits?*/
972 	if ((mp->mscp_header.uda_credits & 0xf0) > 0x10)	/* Check */
973 		return;
974 #ifdef	DEBUG
975 	printd10("udarsp, opcode 0x%x status 0x%x\n",mp->mscp_opcode,mp->mscp_status);
976 #endif
977 	/*
978 	 * If it's an error log message (datagram),
979 	 * pass it on for more extensive processing.
980 	 */
981 	if ((mp->mscp_header.uda_credits & 0xf0) == 0x10) {	/* check */
982 		uderror(um, (struct mslg *)mp);
983 		return;
984 	}
985 	st = mp->mscp_status&M_ST_MASK;
986 	/* The controller interrupts as drive 0 */
987 	/* this means that you must check for controller interrupts */
988 	/* before you check to see if there is a drive 0 */
989 	if((M_OP_STCON|M_OP_END) == mp->mscp_opcode){
990 		if (st == M_ST_SUCC)
991 			sc->sc_state = S_RUN;
992 		else
993 			sc->sc_state = S_IDLE;
994 		um->um_tab.b_active = 0;
995 		wakeup((caddr_t)um);
996 		return;
997 	}
998 	if (mp->mscp_unit >= 8)
999 		return;
1000 	if ((ui = udip[um->um_ctlr][mp->mscp_unit]) == 0)
1001 		return;
1002 	unit = ui->ui_unit;
1003 	switch (mp->mscp_opcode) {
1004 
1005 	case M_OP_ONLIN|M_OP_END:
1006 		ra_info[unit].rastatus = st;
1007 		ra_info[unit].ratype =  mp->mscp_mediaid;
1008 		dp = &udutab[unit];
1009 		if (st == M_ST_SUCC) {
1010 			/*
1011 			 * Link the drive onto the controller queue
1012 			 */
1013 			dp->b_forw = NULL;
1014 			if (um->um_tab.b_actf == NULL)
1015 				um->um_tab.b_actf = dp;
1016 			else
1017 				um->um_tab.b_actl->b_forw = dp;
1018 			um->um_tab.b_actl = dp;
1019 			ui->ui_flags = 1;       /* mark it online */
1020 			ra_info[unit].radsize=(daddr_t)mp->mscp_untsize;
1021 			ra_info[unit].rastate = OPENRAW;
1022 #ifdef	DEBUG
1023 			printd("uda: unit %d online\n", mp->mscp_unit);
1024 #endif
1025 #define F_to_C(x,i)     ( ((x)->mscp_mediaid) >> (i*5+7) & 0x1f ? ( ( (((x)->mscp_mediaid) >>( i*5 + 7)) & 0x1f) + 'A' - 1): ' ')
1026 		/* this mess decodes the Media type identifier */
1027 #ifdef	DEBUG
1028 			printd("uda: unit %d online %x %c%c %c%c%c%d\n"
1029 				,mp->mscp_unit, mp->mscp_mediaid
1030 				,F_to_C(mp,4),F_to_C(mp,3),F_to_C(mp,2)
1031 				,F_to_C(mp,1),F_to_C(mp,0)
1032 				,mp->mscp_mediaid & 0x7f);
1033 #endif
1034 			dp->b_active = 1;
1035 		} else {
1036 			if(dp->b_actf){
1037 				harderr(dp->b_actf,"ra");
1038 			} else {
1039 				nullbp.b_blkno = 0;
1040 				nullbp.b_dev = makedev(UDADEVNUM,unit);
1041 				harderr(&nullbp, "ra");
1042 			}
1043 			printf("OFFLINE\n");
1044 			while (bp = dp->b_actf) {
1045 				dp->b_actf = bp->av_forw;
1046 				bp->b_flags |= B_ERROR;
1047 				iodone(bp);
1048 			}
1049 			ra_info[unit].rastate = CLOSED;
1050 		}
1051 		if(mp->mscp_cmdref!=NULL){/* Seems to get lost sometimes */
1052 			wakeup((caddr_t)mp->mscp_cmdref);
1053 		}
1054 		break;
1055 
1056 /*
1057  * The AVAILABLE ATTENTION messages occurs when the
1058  * unit becomes available after spinup,
1059  * marking the unit offline will force an online command
1060  * prior to using the unit.
1061  */
1062 	case M_OP_AVATN:
1063 #ifdef	DEBUG
1064 		printd("uda: unit %d attention\n", mp->mscp_unit);
1065 #endif
1066 		ui->ui_flags = 0;       /* it went offline and we didn't notice */
1067 		ra_info[unit].ratype =  mp->mscp_mediaid;
1068 		break;
1069 
1070 	case M_OP_END:
1071 /*
1072  * An endcode without an opcode (0200) is an invalid command.
1073  * The mscp specification states that this would be a protocol
1074  * type error, such as illegal opcodes. The mscp spec. also
1075  * states that parameter error type of invalid commands should
1076  * return the normal end message for the command. This does not appear
1077  * to be the case. An invalid logical block number returned an endcode
1078  * of 0200 instead of the 0241 (read) that was expected.
1079  */
1080 
1081 		printf("endcd=%o, stat=%o\n", mp->mscp_opcode, mp->mscp_status);
1082 		break;
1083 	case M_OP_READ|M_OP_END:
1084 	case M_OP_WRITE|M_OP_END:
1085 		bp = (struct buf *)mp->mscp_cmdref;
1086 		ubarelse(um->um_ubanum, (int *)&bp->b_ubinfo);
1087 		/*
1088 		 * Unlink buffer from I/O wait queue.
1089 		 */
1090 		bp->av_back->av_forw = bp->av_forw;
1091 		bp->av_forw->av_back = bp->av_back;
1092 #if defined(VAX750)
1093 		if (cpu == VAX_750 && um->um_tab.b_active == 0
1094 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
1095 			if (um->um_ubinfo == 0)
1096 				printf("udintr: um_ubinfo == 0\n");
1097 			else
1098 				ubarelse(um->um_ubanum, &um->um_ubinfo);
1099 		}
1100 #endif
1101 		dp = &udutab[unit];
1102 		dp->b_qsize--;
1103 		if (ui->ui_dk >= 0)
1104 			if (dp->b_qsize == 0)
1105 				dk_busy &= ~(1<<ui->ui_dk);
1106 		if (st == M_ST_OFFLN || st == M_ST_AVLBL) {
1107 			ui->ui_flags = 0;       /* mark unit offline */
1108 			/*
1109 			 * Link the buffer onto the front of the drive queue
1110 			 */
1111 			if ((bp->av_forw = dp->b_actf) == 0)
1112 				dp->b_actl = bp;
1113 			dp->b_actf = bp;
1114 			/*
1115 			 * Link the drive onto the controller queue
1116 			 */
1117 			if (dp->b_active == 0) {
1118 				dp->b_forw = NULL;
1119 				if (um->um_tab.b_actf == NULL)
1120 					um->um_tab.b_actf = dp;
1121 				else
1122 					um->um_tab.b_actl->b_forw = dp;
1123 				um->um_tab.b_actl = dp;
1124 				dp->b_active = 1;
1125 			}
1126 #if defined(VAX750)
1127 			if (cpu == VAX750 && um->um_ubinfo == 0)
1128 				um->um_ubinfo =
1129 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
1130 					UBA_NEEDBDP);
1131 #endif
1132 			return;
1133 		}
1134 		if (st != M_ST_SUCC) {
1135 			harderr(bp, "ra");
1136 #ifdef	DEBUG
1137 			printd("status %o\n", mp->mscp_status);
1138 #endif
1139 			bp->b_flags |= B_ERROR;
1140 		}
1141 		bp->b_resid = bp->b_bcount - mp->mscp_bytecnt;
1142 		iodone(bp);
1143 		break;
1144 
1145 	case M_OP_GTUNT|M_OP_END:
1146 #ifdef	DEBUG
1147 		printd("GTUNT end packet status = 0x%x media id 0x%x\n"
1148 			,st,mp->mscp_mediaid);
1149 #endif
1150 		ra_info[unit].rastatus = st;
1151 		ra_info[unit].ratype = mp->mscp_mediaid;
1152 		break;
1153 
1154 	default:
1155 		printf("uda: unknown packet\n");
1156 		uderror(um, (struct mslg *)mp);
1157 	}
1158 }
1159 
1160 
1161 /*
1162  * Process an error log message
1163  *
1164  * For now, just log the error on the console.
1165  * Only minimal decoding is done, only "useful"
1166  * information is printed.  Eventually should
1167  * send message to an error logger.
1168  */
1169 uderror(um, mp)
1170 	register struct uba_ctlr *um;
1171 	register struct mslg *mp;
1172 {
1173 	register	i;
1174 
1175 
1176 	if(!(mp->mslg_flags & (M_LF_SUCC | M_LF_CONT)))
1177 		printf("uda%d: hard error\n");
1178 
1179 	mprintf("uda%d: %s error, ", um->um_ctlr,
1180 		mp->mslg_flags & ( M_LF_SUCC | M_LF_CONT ) ? "soft" : "hard");
1181 	switch (mp->mslg_format) {
1182 	case M_FM_CNTERR:
1183 		mprintf("controller error, event 0%o\n", mp->mslg_event);
1184 		break;
1185 
1186 	case M_FM_BUSADDR:
1187 		mprintf("host memory access error, event 0%o, addr 0%o\n",
1188 			mp->mslg_event, mp->mslg_busaddr);
1189 		break;
1190 
1191 	case M_FM_DISKTRN:
1192 		mprintf("disk transfer error, unit %d, grp 0x%x, hdr 0x%x, event 0%o\n",
1193 			mp->mslg_unit, mp->mslg_group, mp->mslg_hdr,
1194 mp->mslg_event);
1195 		break;
1196 
1197 	case M_FM_SDI:
1198 		mprintf("SDI error, unit %d, event 0%o, hdr 0x%x\n",
1199 			mp->mslg_unit, mp->mslg_event, mp->mslg_hdr);
1200 		for(i = 0; i < 12;i++)
1201 			mprintf("\t0x%x",mp->mslg_sdistat[i] & 0xff);
1202 		mprintf("\n");
1203 		break;
1204 
1205 	case M_FM_SMLDSK:
1206 		mprintf("small disk error, unit %d, event 0%o, cyl %d\n",
1207 			mp->mslg_unit, mp->mslg_event, mp->mslg_sdecyl);
1208 		break;
1209 
1210 	default:
1211 		mprintf("unknown error, unit %d, format 0%o, event 0%o\n",
1212 			mp->mslg_unit, mp->mslg_format, mp->mslg_event);
1213 	}
1214 
1215 	if (udaerror) {
1216 		register long *p = (long *)mp;
1217 
1218 		for (i = 0; i < mp->mslg_header.uda_msglen; i += sizeof(*p))
1219 			printf("%x ", *p++);
1220 		printf("\n");
1221 	}
1222 }
1223 
1224 
1225 /*
1226  * Find an unused command packet
1227  */
1228 struct mscp *
1229 udgetcp(um)
1230 	struct uba_ctlr *um;
1231 {
1232 	register struct mscp *mp;
1233 	register struct udaca *cp;
1234 	register struct uda_softc *sc;
1235 	register int i;
1236 	int	s;
1237 
1238 	s = spl5();
1239 	cp = &uda[um->um_ctlr].uda_ca;
1240 	sc = &uda_softc[um->um_ctlr];
1241 	/*
1242 	 * If no credits, can't issue any commands
1243 	 * until some outstanding commands complete.
1244 	 */
1245 	i = sc->sc_lastcmd;
1246 	if(((cp->ca_cmddsc[i]&(UDA_OWN|UDA_INT))==UDA_INT)&&
1247 	    (sc->sc_credits >= 2)) {
1248 		sc->sc_credits--;       /* committed to issuing a command */
1249 		cp->ca_cmddsc[i] &= ~UDA_INT;
1250 		mp = &uda[um->um_ctlr].uda_cmd[i];
1251 		mp->mscp_unit = mp->mscp_modifier = 0;
1252 		mp->mscp_opcode = mp->mscp_flags = 0;
1253 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
1254 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
1255 		sc->sc_lastcmd = (i + 1) % NCMD;
1256 		(void) splx(s);
1257 		return(mp);
1258 	}
1259 	(void) splx(s);
1260 	return(NULL);
1261 }
1262 
1263 udread(dev, uio)
1264 	dev_t dev;
1265 	struct uio *uio;
1266 {
1267 	register int unit = udunit(dev);
1268 
1269 	if (unit >= NRA)
1270 		return (ENXIO);
1271 	return (physio(udstrategy, &rudbuf[unit], dev, B_READ, minphys, uio));
1272 }
1273 
1274 udwrite(dev, uio)
1275 	dev_t dev;
1276 	struct uio *uio;
1277 {
1278 	register int unit = udunit(dev);
1279 
1280 	if (unit >= NRA)
1281 		return (ENXIO);
1282 	return (physio(udstrategy, &rudbuf[unit], dev, B_WRITE, minphys, uio));
1283 }
1284 
1285 udreset(uban)
1286 	int uban;
1287 {
1288 	register struct uba_ctlr *um;
1289 	register struct uba_device *ui;
1290 	register struct buf *bp, *dp;
1291 	register int unit;
1292 	struct buf *nbp;
1293 	int d;
1294 
1295 	for (d = 0; d < NUDA; d++) {
1296 		if ((um = udminfo[d]) == 0 || um->um_ubanum != uban ||
1297 		    um->um_alive == 0)
1298 			continue;
1299 		printf(" uda%d", d);
1300 		um->um_tab.b_active = 0;
1301 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
1302 		uda_softc[d].sc_state = S_IDLE;
1303 		uda_softc[d].sc_mapped = 0;	/* Rich */
1304 		for (unit = 0; unit < NRA; unit++) {
1305 			if ((ui = uddinfo[unit]) == 0)
1306 				continue;
1307 			if (ui->ui_alive == 0 || ui->ui_mi != um)
1308 				continue;
1309 			udutab[unit].b_active = 0;
1310 			udutab[unit].b_qsize = 0;
1311 		}
1312 		for (bp = udwtab[d].av_forw; bp != &udwtab[d]; bp = nbp) {
1313 			nbp = bp->av_forw;
1314 			bp->b_ubinfo = 0;
1315 			/*
1316 			 * Link the buffer onto the drive queue
1317 			 */
1318 			dp = &udutab[udunit(bp->b_dev)];
1319 			if (dp->b_actf == 0)
1320 				dp->b_actf = bp;
1321 			else
1322 				dp->b_actl->av_forw = bp;
1323 			dp->b_actl = bp;
1324 			bp->av_forw = 0;
1325 			/*
1326 			 * Link the drive onto the controller queue
1327 			 */
1328 			if (dp->b_active == 0) {
1329 				dp->b_forw = NULL;
1330 				if (um->um_tab.b_actf == NULL)
1331 					um->um_tab.b_actf = dp;
1332 				else
1333 					um->um_tab.b_actl->b_forw = dp;
1334 				um->um_tab.b_actl = dp;
1335 				dp->b_active = 1;
1336 			}
1337 		}
1338 		(void)udinit(d);
1339 	}
1340 }
1341 
1342 #define DBSIZE 32
1343 
1344 #define ca_Rspdsc       ca_rspdsc[0]
1345 #define ca_Cmddsc       ca_rspdsc[1]
1346 #define uda_Rsp         uda_rsp[0]
1347 #define uda_Cmd         uda_cmd[0]
1348 
1349 struct  uda     udad[NUDA];
1350 
1351 uddump(dev)
1352 	dev_t dev;
1353 {
1354 	struct udadevice *udaddr;
1355 	struct uda *ud_ubaddr;
1356 	char *start;
1357 	int num, blk, unit;
1358 	int maxsz;
1359 	int blkoff;
1360 	register struct uba_regs *uba;
1361 	register struct uba_device *ui;
1362 	register struct uda *udp;
1363 	register struct pte *io;
1364 	register int i;
1365 	struct disklabel *lp;
1366 	unit = udunit(dev);
1367 	if (unit >= NRA)
1368 		return (ENXIO);
1369 #define phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
1370 	ui = phys(struct uba_device *, uddinfo[unit]);
1371 	if (ui->ui_alive == 0)
1372 		return (ENXIO);
1373 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
1374 	ubainit(uba);
1375 	udaddr = (struct udadevice *)ui->ui_physaddr;
1376 	DELAY(2000000);
1377 	udp = phys(struct uda *, &udad[ui->ui_ctlr]);
1378 	lp = &udlabel[unit];
1379 
1380 	num = btoc(sizeof(struct uda)) + 1;
1381 	io = &uba->uba_map[NUBMREG-num];
1382 	for(i = 0; i<num; i++)
1383 		*(int *)io++ = UBAMR_MRV|(btop(udp)+i);
1384 	ud_ubaddr = (struct uda *)(((int)udp & PGOFSET)|((NUBMREG-num)<<9));
1385 
1386 	udaddr->udaip = 0;
1387 	while ((udaddr->udasa & UDA_STEP1) == 0)
1388 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1389 	udaddr->udasa = UDA_ERR;
1390 	while ((udaddr->udasa & UDA_STEP2) == 0)
1391 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1392 	udaddr->udasa = (short)&ud_ubaddr->uda_ca.ca_ringbase;
1393 	while ((udaddr->udasa & UDA_STEP3) == 0)
1394 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1395 	udaddr->udasa = (short)(((int)&ud_ubaddr->uda_ca.ca_ringbase) >> 16);
1396 	while ((udaddr->udasa & UDA_STEP4) == 0)
1397 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1398 	udaddr->udasa = UDA_GO;
1399 	udp->uda_ca.ca_Rspdsc = (long)&ud_ubaddr->uda_Rsp.mscp_cmdref;
1400 	udp->uda_ca.ca_Cmddsc = (long)&ud_ubaddr->uda_Cmd.mscp_cmdref;
1401 	udp->uda_Cmd.mscp_cntflgs = 0;
1402 	udp->uda_Cmd.mscp_version = 0;
1403 	if (udcmd(M_OP_STCON, udp, udaddr) == 0) {
1404 		return(EFAULT);
1405 	}
1406 	udp->uda_Cmd.mscp_unit = ui->ui_slave;
1407 	if (udcmd(M_OP_ONLIN, udp, udaddr) == 0) {
1408 		return(EFAULT);
1409 	}
1410 
1411 	num = maxfree;
1412 	start = 0;
1413 	blkoff = lp->d_partitions[udpart(dev)].p_offset;
1414 	maxsz = lp->d_partitions[udpart(dev)].p_size;
1415 	if (dumplo < 0)
1416 		return (EINVAL);
1417 	if (dumplo + num >= maxsz)
1418 		num = maxsz - dumplo;
1419 	blkoff += dumplo;
1420 	while (num > 0) {
1421 		blk = num > DBSIZE ? DBSIZE : num;
1422 		io = uba->uba_map;
1423 		for (i = 0; i < blk; i++)
1424 			*(int *)io++ = (btop(start)+i) | UBAMR_MRV;
1425 		*(int *)io = 0;
1426 		udp->uda_Cmd.mscp_lbn = btop(start) + blkoff;
1427 		udp->uda_Cmd.mscp_unit = ui->ui_slave;
1428 		udp->uda_Cmd.mscp_bytecnt = blk*NBPG;
1429 		udp->uda_Cmd.mscp_buffer = 0;
1430 		if (udcmd(M_OP_WRITE, udp, udaddr) == 0) {
1431 			return(EIO);
1432 		}
1433 		start += blk*NBPG;
1434 		num -= blk;
1435 	}
1436 	return (0);
1437 }
1438 
1439 
1440 udcmd(op, udp, udaddr)
1441 	int op;
1442 	register struct uda *udp;
1443 	struct udadevice *udaddr;
1444 {
1445 	int i;
1446 
1447 	udp->uda_Cmd.mscp_opcode = op;
1448 	udp->uda_Rsp.mscp_header.uda_msglen = mscp_msglen;
1449 	udp->uda_Cmd.mscp_header.uda_msglen = mscp_msglen;
1450 	udp->uda_ca.ca_Rspdsc |= UDA_OWN|UDA_INT;
1451 	udp->uda_ca.ca_Cmddsc |= UDA_OWN|UDA_INT;
1452 	if (udaddr->udasa&UDA_ERR)
1453 		printf("Udaerror udasa (%x)\n", udaddr->udasa&0xffff);
1454 	i = udaddr->udaip;
1455 #ifdef	lint
1456 	i = i;
1457 #endif
1458 	for (;;) {
1459 		if (udp->uda_ca.ca_cmdint)
1460 			udp->uda_ca.ca_cmdint = 0;
1461 		if (udp->uda_ca.ca_rspint)
1462 			break;
1463 	}
1464 	udp->uda_ca.ca_rspint = 0;
1465 	if (udp->uda_Rsp.mscp_opcode != (op|M_OP_END) ||
1466 	    (udp->uda_Rsp.mscp_status&M_ST_MASK) != M_ST_SUCC) {
1467 		printf("error: com %d opc 0x%x stat 0x%x\ndump ",
1468 			op,
1469 			udp->uda_Rsp.mscp_opcode,
1470 			udp->uda_Rsp.mscp_status);
1471 		return(0);
1472 	}
1473 	return(1);
1474 }
1475 
1476 udioctl(dev, cmd, data, flag)
1477 	dev_t dev;
1478 	int cmd;
1479 	caddr_t data;
1480 	int flag;
1481 {
1482 	int unit = udunit(dev);
1483 	register struct disklabel *lp;
1484 	int error = 0;
1485 
1486 	lp = &udlabel[unit];
1487 
1488 	switch (cmd) {
1489 
1490 	case DIOCGDINFO:
1491 		*(struct disklabel *)data = *lp;
1492 		break;
1493 
1494 	case DIOCGPART:
1495 		((struct partinfo *)data)->disklab = lp;
1496 		((struct partinfo *)data)->part =
1497 		    &lp->d_partitions[udpart(dev)];
1498 		break;
1499 
1500 	case DIOCSDINFO:
1501 		if ((flag & FWRITE) == 0)
1502 			error = EBADF;
1503 		else
1504 			*lp = *(struct disklabel *)data;
1505 		break;
1506 
1507 	case DIOCWDINFO:
1508 		if ((flag & FWRITE) == 0) {
1509 			error = EBADF;
1510 			break;
1511 		}
1512 		{
1513 		struct buf *bp;
1514 		struct disklabel *dlp;
1515 		daddr_t alt, end;
1516 
1517 		*lp = *(struct disklabel *)data;
1518 		bp = geteblk(lp->d_secsize);
1519 		bp->b_dev = makedev(major(dev), udminor(udunit(dev), 0));
1520 		bp->b_bcount = lp->d_secsize;
1521 		bp->b_blkno = LABELSECTOR;
1522 		bp->b_flags = B_READ;
1523 		dlp = (struct disklabel *)(bp->b_un.b_addr + LABELOFFSET);
1524 		udstrategy(bp);
1525 		biowait(bp);
1526 		if (bp->b_flags & B_ERROR) {
1527 			error = u.u_error;		/* XXX */
1528 			u.u_error = 0;
1529 			goto bad;
1530 		}
1531 		*dlp = *lp;
1532 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_ntracks + 1;
1533 		end = alt + 8;
1534 		for (;;) {
1535 			bp->b_flags = B_WRITE;
1536 			udstrategy(bp);
1537 			biowait(bp);
1538 			if (bp->b_flags & B_ERROR) {
1539 				error = u.u_error;	/* XXX */
1540 				u.u_error = 0;
1541 			}
1542 			if (bp->b_blkno >= end)
1543 				break;
1544 			bp->b_blkno = alt;
1545 			alt += 2;
1546 		}
1547 bad:
1548 		brelse(bp);
1549 		}
1550 		break;
1551 
1552 	default:
1553 		error = ENOTTY;
1554 		break;
1555 	}
1556 	return (0);
1557 }
1558 
1559 udsize(dev)
1560 	dev_t dev;
1561 {
1562 	register int unit = udunit(dev);
1563 	register struct uba_device *ui;
1564 
1565 	if (unit >= NRA || (ui = uddinfo[unit]) == 0 || ui->ui_alive == 0 ||
1566 	    ui->ui_flags == 0 || ra_info[unit].rastate != OPEN)
1567 		return (-1);
1568 	return ((int)udlabel[unit].d_partitions[udpart(dev)].p_size);
1569 }
1570 
1571 #ifdef COMPAT_42
1572 struct size {
1573 	daddr_t nblocks;
1574 	daddr_t blkoff;
1575 }  ra25_sizes[8] = {
1576 	15884,	0,		/* A=blk 0 thru 15883 */
1577 	10032,	15884,		/* B=blk 15884 thru 49323 */
1578 	-1,	0,		/* C=blk 0 thru end */
1579 	0,	0,		/* D=blk 340670 thru 356553 */
1580 	0,	0,		/* E=blk 356554 thru 412489 */
1581 	0,	0,		/* F=blk 412490 thru end */
1582 	-1,	25916,		/* G=blk 49324 thru 131403 */
1583 	0,	0,		/* H=blk 131404 thru end */
1584 }, rd52_sizes[8] = {
1585 	15884,	0,		/* A=blk 0 thru 15883 */
1586 	9766,	15884,		/* B=blk 15884 thru 25649 */
1587 	-1,	0,		/* C=blk 0 thru end */
1588 	0,	0,		/* D=unused */
1589 	0,	0,		/* E=unused */
1590 	0,	0,		/* F=unused */
1591 	-1,	25650,		/* G=blk 25650 thru end */
1592 	0,	0,		/* H=unused */
1593 }, rd53_sizes[8] = {
1594 	15884,	0,		/* A=blk 0 thru 15883 */
1595 	33440,	15884,		/* B=blk 15884 thru 49323 */
1596 	-1,	0,		/* C=blk 0 thru end */
1597 	0,	0,		/* D=unused */
1598 	33440,	0,		/* E=blk 0 thru 33439 */
1599 	-1,	33440,		/* F=blk 33440 thru end */
1600 	-1,	49324,		/* G=blk 49324 thru end */
1601 	-1,	15884,		/* H=blk 15884 thru end */
1602 }, ra60_sizes[8] = {
1603 	15884,	0,		/* A=sectors 0 thru 15883 */
1604 	33440,	15884,		/* B=sectors 15884 thru 49323 */
1605 	400176,	0,		/* C=sectors 0 thru 400175 */
1606 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
1607 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
1608 	350852,	49324,		/* F=sectors 49324 thru 400175 */
1609 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
1610 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
1611 }, ra80_sizes[8] = {
1612 	15884,	0,		/* A=sectors 0 thru 15883 */
1613 	33440,	15884,		/* B=sectors 15884 thru 49323 */
1614 	242606,	0,		/* C=sectors 0 thru 242605 */
1615 	0,	0,		/* D=unused */
1616 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
1617 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
1618 	192696,	49910,		/* G=sectors 49910 thru 242605 */
1619 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
1620 }, ra81_sizes[8] ={
1621 /*
1622  * These are the new standard partition sizes for ra81's.
1623  * An RA_COMPAT system is compiled with D, E, and F corresponding
1624  * to the 4.2 partitions for G, H, and F respectively.
1625  */
1626 #ifndef	UCBRA
1627 	15884,	0,		/* A=sectors 0 thru 15883 */
1628 	66880,	16422,		/* B=sectors 16422 thru 83301 */
1629 	891072,	0,		/* C=sectors 0 thru 891071 */
1630 #ifdef RA_COMPAT
1631 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
1632 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
1633 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
1634 #else
1635 	15884,	375564,		/* D=sectors 375564 thru 391447 */
1636 	307200,	391986,		/* E=sectors 391986 thru 699185 */
1637 	191352,	699720,		/* F=sectors 699720 thru 891071 */
1638 #endif RA_COMPAT
1639 	515508,	375564,		/* G=sectors 375564 thru 891071 */
1640 	291346,	83538,		/* H=sectors 83538 thru 374883 */
1641 
1642 /*
1643  * These partitions correspond to the sizes used by sites at Berkeley,
1644  * and by those sites that have received copies of the Berkeley driver
1645  * with deltas 6.2 or greater (11/15/83).
1646  */
1647 #else UCBRA
1648 
1649 	15884,	0,		/* A=sectors 0 thru 15883 */
1650 	33440,	15884,		/* B=sectors 15884 thru 49323 */
1651 	891072,	0,		/* C=sectors 0 thru 891071 */
1652 	15884,	242606,		/* D=sectors 242606 thru 258489 */
1653 	307200,	258490,		/* E=sectors 258490 thru 565689 */
1654 	325382,	565690,		/* F=sectors 565690 thru 891071 */
1655 	648466,	242606,		/* G=sectors 242606 thru 891071 */
1656 	193282,	49324,		/* H=sectors 49324 thru 242605 */
1657 
1658 #endif UCBRA
1659 };
1660 
1661 udmaptype(unit, lp)
1662 	register unit;
1663 	register struct disklabel *lp;
1664 {
1665 	register struct size *rasizes;
1666 	register struct partition *pp;
1667 	register type;
1668 
1669 	lp->d_secperunit = ra_info[unit].radsize;
1670 	type = ra_info[unit].ratype & 0x7f;
1671 	lp->d_typename[0] = 'r';
1672 	lp->d_typename[1] = 'a';
1673 	lp->d_typename[2] = '0' + type/10;
1674 	lp->d_typename[3] = '0' + type%10;
1675 	switch (type) {
1676 	case    25:
1677 		rasizes = ra25_sizes;
1678 		lp->d_nsectors = 42;
1679 		lp->d_ntracks = 4;
1680 		lp->d_ncylinders = 302;
1681 		break;
1682 	case    52:
1683 		lp->d_typename[1] = 'd';
1684 		rasizes = rd52_sizes;
1685 		lp->d_nsectors = 18;
1686 		lp->d_ntracks = 7;
1687 		lp->d_ncylinders = 480;
1688 		break;
1689 	case    53:
1690 		rasizes = rd53_sizes;
1691 		lp->d_typename[1] = 'd';
1692 		lp->d_nsectors = 18;
1693 		lp->d_ntracks = 8;
1694 		lp->d_ncylinders = 963;
1695 		break;
1696 	case    60:
1697 		rasizes = ra60_sizes;
1698 		lp->d_nsectors = 42;
1699 		lp->d_ntracks = 4;
1700 		lp->d_ncylinders = 2382;
1701 		break;
1702 	case    80:
1703 		rasizes = ra80_sizes;
1704 		lp->d_nsectors = 31;
1705 		lp->d_ntracks = 14;
1706 		lp->d_ncylinders = 559;
1707 		break;
1708 	case    81:
1709 		rasizes = ra81_sizes;
1710 		lp->d_nsectors = 51;
1711 		lp->d_ntracks = 14;
1712 		lp->d_ncylinders = 1248;
1713 		break;
1714 	default:
1715 		printf("Don't have a partition table for an ra%d\n", type);
1716 		lp->d_npartitions = 1;
1717 		lp->d_partitions[0].p_offset = 0;
1718 		lp->d_partitions[0].p_size = lp->d_secperunit;
1719 		return (0);
1720 	}
1721 	lp->d_secsize = 512;
1722 	lp->d_npartitions = 8;
1723 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1724 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8];
1725 	    pp++, rasizes++) {
1726 		pp->p_offset = rasizes->blkoff;
1727 		if ((pp->p_size = rasizes->nblocks) == (u_long)-1)
1728 			pp->p_size = ra_info[unit].radsize - rasizes->blkoff;
1729 	}
1730 	return (1);
1731 }
1732 #endif COMPAT_42
1733 #endif
1734