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