xref: /csrg-svn/sys/vax/uba/uda.c (revision 24742)
1 /*
2  *	@(#)uda.c	6.11 (Berkeley) 09/14/85
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  * Date:        Jan  30 1984
16  *
17  * This thing has been beaten beyound belief.  It still has two main features.
18  * 1) When this device is on the same unibus as another DMA device
19  * like a versatec or a rk07. the Udstrat routine complains that it still
20  * has a buffered data path that it shouldn't.  I don't know why.
21  *
22  * decvax!rich.
23  *
24  */
25 
26 #define	DEBUG
27 #define	UDADEVNUM	(9)		/* entry in bdevsw */
28 #include "ra.h"
29 #if NUDA > 0
30 /*
31  * UDA50/RAxx disk device driver
32  *
33  * Restrictions:
34  *      Unit numbers must be less than 8.
35  *      Partitions A and B must be the same size on all RA drives.
36  */
37 #include "../machine/pte.h"
38 
39 #include "param.h"
40 #include "systm.h"
41 #include "buf.h"
42 #include "conf.h"
43 #include "dir.h"
44 #include "user.h"
45 #include "map.h"
46 #include "vm.h"
47 #include "dk.h"
48 #include "cmap.h"
49 #include "uio.h"
50 
51 #include "../vax/cpu.h"
52 #include "ubareg.h"
53 #include "ubavar.h"
54 #include "../vax/mtpr.h"
55 
56 #define TENSEC	(1000)
57 
58 #define NRSPL2  3               /* log2 number of response packets */
59 #define NCMDL2  3               /* log2 number of command packets */
60 #define NRSP    (1<<NRSPL2)
61 #define NCMD    (1<<NCMDL2)
62 
63 #include "../vaxuba/udareg.h"
64 #include "../vax/mscp.h"
65 
66 
67 struct uda_softc {
68 	short   sc_state;       /* state of controller */
69 	short   sc_mapped;      /* Unibus map allocated for uda struct? */
70 	int     sc_ubainfo;     /* Unibus mapping info */
71 	struct uda *sc_uda;     /* Unibus address of uda struct */
72 	int     sc_ivec;        /* interrupt vector address */
73 	short   sc_credits;     /* transfer credits */
74 	short   sc_lastcmd;     /* pointer into command ring */
75 	short   sc_lastrsp;     /* pointer into response ring */
76 } uda_softc[NUDA];
77 struct uda {
78 	struct udaca    uda_ca;         /* communications area */
79 	struct mscp     uda_rsp[NRSP];  /* response packets */
80 	struct mscp     uda_cmd[NCMD];  /* command packets */
81 } uda[NUDA];
82 
83 #define udunit(dev)	(minor(dev) >> 3)
84 
85 /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
86 struct size {
87 	daddr_t nblocks;
88 	daddr_t blkoff;
89 }  ra25_sizes[8] = {
90 	15884,	0,		/* A=blk 0 thru 15883 */
91 	10032,	15884,		/* B=blk 15884 thru 49323 */
92 	-1,	0,		/* C=blk 0 thru end */
93 	0,	0,		/* D=blk 340670 thru 356553 */
94 	0,	0,		/* E=blk 356554 thru 412489 */
95 	0,	0,		/* F=blk 412490 thru end */
96 	-1,	25916,		/* G=blk 49324 thru 131403 */
97 	0,	0,		/* H=blk 131404 thru end */
98 }, ra60_sizes[8] = {
99 	15884,	0,		/* A=blk 0 thru 15883 */
100 	33440,	15884,		/* B=blk 15884 thru 49323 */
101 	-1,	0,		/* C=blk 0 thru end */
102 	15884,	242606,		/* D=blk 242606 thru 258489 */
103 	-1,	258490,		/* E=blk 258490 thru end */
104 	0,	0,		/* F=unused */
105 	-1,	242606,		/* G=blk 242606 thru end */
106 	193282,	49324,		/* H=blk 49324 thru 242605 */
107 }, ra80_sizes[8] = {
108 	15884,	0,		/* A=blk 0 thru 15883 */
109 	33440,	15884,		/* B=blk 15884 thru 49323 */
110 	-1,	0,		/* C=blk 0 thru end */
111 	0,	0,		/* D=unused */
112 	0,	0,		/* E=unused */
113 	0,	0,		/* F=unused */
114 	0,	0,		/* G=unused */
115 	193282,	49324,		/* H=blk 49324 thru 242605 */
116 }, ra81_sizes[8] ={
117 	15884,	0,		/* A=blk 0 thru 15883 */
118 	33440,	15884,		/* B=blk 15884 thru 49323 */
119 	-1,	0,		/* C=blk 0 thru end */
120 	15884,	242606,		/* D=blk 242606 thru 258489 */
121 	307200,	258490,		/* E=blk 258490 thru 565689 */
122 	-1,	565690,		/* F=blk 565690 thru end */
123 	-1,	242606,		/* G=blk 242606 thru end */
124 	193282,	49324,		/* H=blk 49324 thru 242605 */
125 };
126 
127 struct	ra_info {
128 	struct  size    *ra_sizes;	/* Partion tables for drive */
129 	daddr_t		radsize;	/* Max user size form online pkt */
130 	unsigned	ratype;		/* Drive type int field  */
131 	unsigned	rastatus;	/* Command status from */
132 					/* last onlin or GTUNT */
133 } ra_info[NRA];
134 
135 
136 /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
137 struct  uba_ctlr *udminfo[NUDA];
138 struct  uba_device *uddinfo[NRA];
139 struct  uba_device *udip[NUDA][8];      /* 8 == max number of drives */
140 struct  buf rudbuf[NRA];
141 struct  buf udutab[NRA];
142 struct  buf udwtab[NUDA];               /* I/O wait queue, per controller */
143 
144 
145 int     nNRA = NRA;
146 int     nNUDA = NUDA;
147 int     udamicro[NUDA];         /* to store microcode level */
148 
149 
150 /*
151  * Controller states
152  */
153 #define S_IDLE  0               /* hasn't been initialized */
154 #define S_STEP1 1               /* doing step 1 init */
155 #define S_STEP2 2               /* doing step 2 init */
156 #define S_STEP3 3               /* doing step 3 init */
157 #define S_SCHAR 4               /* doing "set controller characteristics" */
158 #define S_RUN   5               /* running */
159 
160 
161 int     udaerror = 0;                   /* causes hex dump of packets */
162 int     udadebug = 0;
163 int	uda_cp_wait = 0;		/* Something to wait on for command */
164 					/* packets and or credits. */
165 int	wakeup();
166 extern	int	hz;			/* Should find the right include */
167 #ifdef	DEBUG
168 #define printd  if (udadebug) printf
169 #define	printd10	if(udadebug >= 10) printf
170 #endif
171 #define mprintf printf			/* temporary JG hack until Rich fixes*/
172 
173 int     udprobe(), udslave(), udattach(), udintr();
174 struct  mscp *udgetcp();
175 
176 u_short udstd[] = { 0772150, 0772550, 0777550, 0 };
177 struct  uba_driver udadriver =
178  { udprobe, udslave, udattach, 0, udstd, "ra", uddinfo, "uda", udminfo, 0 };
179 
180 #define b_qsize         b_resid         /* queue size per drive, in udutab */
181 #define b_ubinfo        b_resid         /* Unibus mapping info, per buffer */
182 
183 udprobe(reg, ctlr)
184 	caddr_t reg;
185 	int ctlr;
186 {
187 	register int br, cvec;
188 	register struct uda_softc *sc = &uda_softc[ctlr];
189 	struct udadevice *udaddr;
190 
191 	int	cur_time;
192 
193 #ifdef lint
194 	br = 0; cvec = br; br = cvec;
195 	udreset(0); udintr(0);
196 #endif
197 	udaddr = (struct udadevice *) reg;
198 
199 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
200 	udaddr->udaip = 0;              /* start initialization */
201 
202 	cur_time = mfpr(TODR);			/* Time of day */
203 	while(cur_time + TENSEC > mfpr(TODR)){	/* wait for at most 10 secs */
204 		if((udaddr->udasa & UDA_STEP1) != 0)
205 			break;
206 	}
207 	if(cur_time + TENSEC <= mfpr(TODR))
208 		return(0);		/* Not a uda or it won't init as it  */
209 					/* should within ten seconds.  */
210 	udaddr->udasa=UDA_ERR|(NCMDL2<<11)|(NRSPL2<<8)|UDA_IE|(sc->sc_ivec/4);
211 	while((udaddr->udasa&UDA_STEP2)==0)
212 		DELAY(1000);		/* intr should have */
213 					/*   have happened by now */
214 
215 	return(sizeof (struct udadevice));
216 }
217 
218 udslave(ui, reg)
219 	struct uba_device *ui;
220 	caddr_t reg;
221 {
222 	register struct uba_ctlr *um = udminfo[ui->ui_ctlr];
223 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
224 	struct udadevice *udaddr;
225 	struct	mscp	*mp;
226 	int	i;			/* Something to write into to start */
227 					/* the uda polling */
228 
229 
230 #ifdef lint
231 	ui = ui; reg = reg; i = i;
232 #endif
233 	udaddr = (struct udadevice *)um->um_addr;
234 	if(sc->sc_state != S_RUN){
235 		if(!udinit(ui->ui_ctlr))
236 			return(0);
237 	}
238 	/* Here we will wait for the controller */
239 	/* to come into the run state or go idle.  If we go idle we are in */
240 	/* touble and I don't yet know what to do so I will punt */
241 	while(sc->sc_state != S_RUN && sc->sc_state != S_IDLE);	/* spin */
242 	if(sc->sc_state == S_IDLE){	/* The Uda failed to initialize */
243 		printf("UDA failed to init\n");
244 		return(0);
245 	}
246 	/* The controller is up so let see if the drive is there! */
247 	if(0 == (mp = udgetcp(um))){	/* ditto */
248 		printf("UDA can't get command packet\n");
249 		return(0);
250 	}
251 	mp->mscp_opcode = M_OP_GTUNT;	/* This should give us the drive type*/
252 	mp->mscp_unit = ui->ui_slave;
253 	mp->mscp_cmdref = (long) ui->ui_slave;
254 #ifdef	DEBUG
255 	printd("uda%d Get unit status slave %d\n",ui->ui_ctlr,ui->ui_slave);
256 #endif
257 	ra_info[ui->ui_unit].rastatus = 0;	/* set to zero */
258 	udip[ui->ui_ctlr][ui->ui_slave] = ui;
259 	*((long *) mp->mscp_dscptr ) |= UDA_OWN | UDA_INT;/* maybe we should poll*/
260 	i = udaddr->udaip;
261 	while(!ra_info[ui->ui_unit].rastatus);  /* Wait for some status */
262 	udip[ui->ui_ctlr][ui->ui_slave] = 0;
263 	if(!ra_info[ui->ui_unit].ratype)	/* packet from a GTUNT */
264 		return(0);		/* Failed No such drive */
265 	else
266 		return(1);		/* Got it and it is there */
267 }
268 
269 udattach(ui)
270 	register struct uba_device *ui;
271 {
272 	register struct uba_ctlr *um = ui->ui_mi ;
273 	struct udadevice *udaddr = (struct udadevice *) um->um_addr;
274 	struct	mscp	*mp;
275 	int	i;			/* Something to write into to start */
276 					/* the uda polling */
277 #ifdef	lint
278 	i = i;
279 #endif
280 	if (ui->ui_dk >= 0)
281 		dk_mspw[ui->ui_dk] = 1.0 / (60 * 31 * 256);     /* approx */
282 	ui->ui_flags = 0;
283 	udip[ui->ui_ctlr][ui->ui_slave] = ui;
284 	/* check to see if the drive is a available if it is bring it online */
285 	/* if not then just return.  open will try an online later */
286 	if(ra_info[ui->ui_unit].rastatus != M_ST_AVLBL)
287 		return;			/* status was set by a GTUNT */
288 	if(0 == (mp = udgetcp(um))){	/* ditto */
289 		printf("UDA can't get command packet\n");
290 		return;
291 	}
292 	mp->mscp_opcode = M_OP_ONLIN;
293 	mp->mscp_unit = ui->ui_slave;
294 	mp->mscp_cmdref = (long) ui->ui_slave;
295 #ifdef	DEBUG
296 	printd("uda%d ONLIN slave %d\n",ui->ui_ctlr,ui->ui_slave);
297 #endif
298 	*((long *) mp->mscp_dscptr ) |= UDA_OWN | UDA_INT;
299 	i = udaddr->udaip;
300 	while(ui->ui_flags == 0 && ra_info[ui->ui_unit].ratype != 0);
301 }
302 
303 /*
304  * Open a UDA.  Initialize the device and
305  * set the unit online.
306  */
307 udopen(dev, flag)
308 	dev_t dev;
309 	int flag;
310 {
311 	register int unit;
312 	register struct uba_device *ui;
313 	register struct uda_softc *sc;
314 	register struct mscp *mp;
315 	register struct uba_ctlr *um;
316 	struct udadevice *udaddr;
317 	int s,i;
318 	extern quota;
319 
320 #ifdef lint
321 	flag = flag; i = i;
322 #endif
323 	unit = udunit(dev);
324 	if (unit >= nNRA || (ui = uddinfo[unit]) == 0 || ui->ui_alive == 0)
325 		return (ENXIO);
326 	sc = &uda_softc[ui->ui_ctlr];
327 	s = spl5();
328 	if (sc->sc_state != S_RUN) {
329 		if (sc->sc_state == S_IDLE)
330 			if(!udinit(ui->ui_ctlr)){
331 				printf("uda: Controller failed to init\n");
332 				return(ENXIO);
333 			}
334 		/* wait for initialization to complete */
335 		timeout(wakeup,(caddr_t)ui->ui_mi,11*hz);	/* to be sure*/
336 		sleep((caddr_t)ui->ui_mi, 0);
337 		if (sc->sc_state != S_RUN)
338 		{
339 			(void) splx(s); /* added by Rich */
340 			return (EIO);
341 		}
342 	}
343 	/* check to see if the device is really there. */
344 	/* this code was taken from Fred Canters 11 driver */
345 	um = ui->ui_mi;
346 	udaddr = (struct udadevice *) um->um_addr;
347 	(void) splx(s);
348 	if(ui->ui_flags == 0){
349 		s = spl5();
350 		while(0 ==(mp = udgetcp(um))){
351 			uda_cp_wait++;
352 			sleep(&uda_cp_wait,PSWP+1);
353 			uda_cp_wait--;
354 		}
355 		mp->mscp_opcode = M_OP_ONLIN;
356 		mp->mscp_unit = ui->ui_slave;
357 		mp->mscp_cmdref = (long) & ra_info[ui->ui_unit].ratype;
358 			/* need to sleep on something */
359 #ifdef	DEBUG
360 		printd("uda: bring unit %d online\n",ui->ui_unit);
361 #endif
362 		*((long *) mp->mscp_dscptr ) |= UDA_OWN | UDA_INT ;
363 		i = udaddr->udaip;
364 		timeout(wakeup,(caddr_t) mp->mscp_cmdref,10 * hz);
365 			/* make sure we wake up */
366 		sleep((caddr_t) mp->mscp_cmdref,PSWP+1); /*wakeup in udrsp() */
367 		(void) splx(s);
368 	}
369 	if(ui->ui_flags == 0){
370 		return(ENXIO);  /* Didn't go online */
371 	}
372 	return (0);
373 }
374 
375 /*
376  * Initialize a UDA.  Set up UBA mapping registers,
377  * initialize data structures, and start hardware
378  * initialization sequence.
379  */
380 udinit(d)
381 	int d;
382 {
383 	register struct uda_softc *sc;
384 	register struct uda *ud;
385 	struct udadevice *udaddr;
386 	struct uba_ctlr *um;
387 
388 	sc = &uda_softc[d];
389 	um = udminfo[d];
390 	um->um_tab.b_active++;
391 	ud = &uda[d];
392 	udaddr = (struct udadevice *)um->um_addr;
393 	if (sc->sc_mapped == 0) {
394 		/*
395 		 * Map the communications area and command
396 		 * and response packets into Unibus address
397 		 * space.
398 		 */
399 		sc->sc_ubainfo = uballoc(um->um_ubanum, (caddr_t)ud,
400 		    sizeof (struct uda), 0);
401 		sc->sc_uda = (struct uda *)(sc->sc_ubainfo & 0x3ffff);
402 		sc->sc_mapped = 1;
403 	}
404 
405 	/*
406 	 * Start the hardware initialization sequence.
407 	 */
408 
409  	udaddr->udaip = 0;              /* start initialization */
410 
411 	while((udaddr->udasa & UDA_STEP1) == 0){
412 		if(udaddr->udasa & UDA_ERR)
413 			return(0);	/* CHECK */
414 	}
415 	udaddr->udasa=UDA_ERR|(NCMDL2<<11)|(NRSPL2<<8)|UDA_IE|(sc->sc_ivec/4);
416 	/*
417 	 * Initialization continues in interrupt routine.
418 	 */
419 	sc->sc_state = S_STEP1;
420 	sc->sc_credits = 0;
421 	return(1);
422 }
423 
424 udstrategy(bp)
425 	register struct buf *bp;
426 {
427 	register struct uba_device *ui;
428 	register struct uba_ctlr *um;
429 	register struct buf *dp;
430 	register int unit;
431 	register struct size    *rasizes;
432 	int xunit = minor(bp->b_dev) & 07;
433 	daddr_t sz, maxsz;
434 	int s;
435 
436 	sz = (bp->b_bcount+511) >> 9;
437 	unit = udunit(bp->b_dev);
438 	if (unit >= nNRA) {
439 		bp->b_error = ENXIO;
440 		goto bad;
441 	}
442 	rasizes = ra_info[unit].ra_sizes;
443 	ui = uddinfo[unit];
444 	um = ui->ui_mi;
445 	if (ui == 0 || ui->ui_alive == 0) {
446 		bp->b_error = ENXIO;
447 		goto bad;
448 	}
449 	if ((maxsz = rasizes[xunit].nblocks) < 0)
450 		maxsz = ra_info[unit].radsize - rasizes[xunit].blkoff;
451 	if (bp->b_blkno < 0 || bp->b_blkno+sz > maxsz ||
452 	    rasizes[xunit].blkoff >= ra_info[unit].radsize) {
453 		if (bp->b_blkno == maxsz +1)
454 		        goto done;
455 		bp->b_error = EINVAL;
456 		goto bad;
457 	}
458 	s = spl5();
459 	/*
460 	 * Link the buffer onto the drive queue
461 	 */
462 	dp = &udutab[ui->ui_unit];
463 	if (dp->b_actf == 0)
464 		dp->b_actf = bp;
465 	else
466 		dp->b_actl->av_forw = bp;
467 	dp->b_actl = bp;
468 	bp->av_forw = 0;
469 	/*
470 	 * Link the drive onto the controller queue
471 	 */
472 	if (dp->b_active == 0) {
473 		dp->b_forw = NULL;
474 		if (um->um_tab.b_actf == NULL)
475 			um->um_tab.b_actf = dp;
476 		else
477 			um->um_tab.b_actl->b_forw = dp;
478 		um->um_tab.b_actl = dp;
479 		dp->b_active = 1;
480 	}
481 	if (um->um_tab.b_active == 0) {
482 #if defined(VAX750)
483 		if (cpu == VAX_750
484 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
485 			if (um->um_ubinfo != 0) {
486 				printd("udastrat: ubinfo 0x%x\n",um->um_ubinfo);
487 			} else
488 				um->um_ubinfo =
489 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
490 					UBA_NEEDBDP);
491 		}
492 #endif
493 		(void) udstart(um);
494 	}
495 	splx(s);
496 	return;
497 
498 bad:
499 	bp->b_flags |= B_ERROR;
500 done:
501 	iodone(bp);
502 	return;
503 }
504 
505 udstart(um)
506 	register struct uba_ctlr *um;
507 {
508 	register struct buf *bp, *dp;
509 	register struct mscp *mp;
510 	register struct uda_softc *sc;
511 	register struct uba_device *ui;
512 	struct  size    *rasizes;
513 	struct udadevice *udaddr;
514 	struct  uda     *ud = &uda[um->um_ctlr];
515 	int i;
516 
517 	sc = &uda_softc[um->um_ctlr];
518 
519 loop:
520 	if ((dp = um->um_tab.b_actf) == NULL) {
521 		/*
522 		 * Release uneeded UBA resources and return
523 		 */
524 		um->um_tab.b_active = 0;
525 		/* Check for response ring transitions lost in the
526 		 * Race condition
527 		 */
528 		for (i = sc->sc_lastrsp;; i++) {
529 			i %= NRSP;
530 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
531 				break;
532 			udrsp(um, ud, sc, i);
533 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
534 		}
535 		sc->sc_lastrsp = i;
536 		return (0);
537 	}
538 	if ((bp = dp->b_actf) == NULL) {
539 		/*
540 		 * No more requests for this drive, remove
541 		 * from controller queue and look at next drive.
542 		 * We know we're at the head of the controller queue.
543 		 */
544 		dp->b_active = 0;
545 		um->um_tab.b_actf = dp->b_forw;
546 		goto loop;		/* Need to check for loop */
547 	}
548 	um->um_tab.b_active++;
549 	udaddr = (struct udadevice *)um->um_addr;
550 	if ((udaddr->udasa&UDA_ERR) || sc->sc_state != S_RUN) {
551 		harderr(bp, "ra");
552 		mprintf("Uda%d udasa %o, state %d\n",um->um_ctlr , udaddr->udasa&0xffff, sc->sc_state);
553 		udinit(um->um_ctlr);
554 		/* SHOULD REQUEUE OUTSTANDING REQUESTS, LIKE UDRESET */
555 		return (0);
556 	}
557 	ui = uddinfo[udunit(bp->b_dev)];
558 	rasizes = ra_info[ui->ui_unit].ra_sizes;
559 	if (ui->ui_flags == 0) {        /* not online */
560 		if ((mp = udgetcp(um)) == NULL){
561 			return (0);
562 		}
563 		mp->mscp_opcode = M_OP_ONLIN;
564 		mp->mscp_unit = ui->ui_slave;
565 		dp->b_active = 2;
566 		um->um_tab.b_actf = dp->b_forw; /* remove from controller q */
567 #ifdef	DEBUG
568 		printd("uda: bring unit %d online\n", ui->ui_slave);
569 #endif
570 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
571 		if (udaddr->udasa&UDA_ERR)
572 			printf("Uda (%d) Error (%x)\n",um->um_ctlr , udaddr->udasa&0xffff);
573 		i = udaddr->udaip;
574 		goto loop;
575 	}
576 	switch (cpu) {
577 	case VAX_8600:
578 	case VAX_780:
579 		i = UBA_NEEDBDP|UBA_CANTWAIT;
580 		break;
581 
582 	case VAX_750:
583 		i = um->um_ubinfo|UBA_HAVEBDP|UBA_CANTWAIT;
584 		break;
585 
586 	case VAX_730:
587 		i = UBA_CANTWAIT;
588 		break;
589 	}
590 	if ((i = ubasetup(um->um_ubanum, bp, i)) == 0)
591 		return(1);
592 	if ((mp = udgetcp(um)) == NULL) {
593 		ubarelse(um->um_ubanum,&i);
594 		return(0);
595 	}
596 	mp->mscp_cmdref = (long)bp;     /* pointer to get back */
597 	mp->mscp_opcode = bp->b_flags&B_READ ? M_OP_READ : M_OP_WRITE;
598 	mp->mscp_unit = ui->ui_slave;
599 	mp->mscp_lbn = bp->b_blkno + rasizes[minor(bp->b_dev)&7].blkoff;
600 	mp->mscp_bytecnt = bp->b_bcount;
601 	mp->mscp_buffer = (i & 0x3ffff) | (((i>>28)&0xf)<<24);
602 #if defined(VAX750)
603 	if (cpu == VAX_750)
604 		i &= 0xfffffff;         /* mask off bdp */
605 #endif
606 	bp->b_ubinfo = i;               /* save mapping info */
607 	*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
608 	if (udaddr->udasa&UDA_ERR)
609 		printf("Uda(%d) udasa (%x)\n",um->um_ctlr , udaddr->udasa&0xffff);
610 	i = udaddr->udaip;              /* initiate polling */
611 	dp->b_qsize++;
612 	if (ui->ui_dk >= 0) {
613 		dk_busy |= 1<<ui->ui_dk;
614 		dk_xfer[ui->ui_dk]++;
615 		dk_wds[ui->ui_dk] += bp->b_bcount>>6;
616 	}
617 
618 	/*
619 	 * Move drive to the end of the controller queue
620 	 */
621 	if (dp->b_forw != NULL) {
622 		um->um_tab.b_actf = dp->b_forw;
623 		um->um_tab.b_actl->b_forw = dp;
624 		um->um_tab.b_actl = dp;
625 		dp->b_forw = NULL;
626 	}
627 	/*
628 	 * Move buffer to I/O wait queue
629 	 */
630 	dp->b_actf = bp->av_forw;
631 	dp = &udwtab[um->um_ctlr];
632 	bp->av_forw = dp;
633 	bp->av_back = dp->av_back;
634 	dp->av_back->av_forw = bp;
635 	dp->av_back = bp;
636 	goto loop;
637 }
638 
639 /*
640  * UDA interrupt routine.
641  */
642 udintr(d)
643 	int d;
644 {
645 	register struct uba_ctlr *um = udminfo[d];
646 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
647 	struct buf *bp;
648 	register int i;
649 	register struct uda_softc *sc = &uda_softc[d];
650 	register struct uda *ud = &uda[d];
651 	struct uda *uud;
652 	struct mscp *mp;
653 
654 #ifdef	DEBUG
655 	printd10("udintr: state %d, udasa %o\n", sc->sc_state, udaddr->udasa);
656 #endif
657 	switch (sc->sc_state) {
658 	case S_IDLE:
659 		printf("uda%d: random interrupt ignored\n", d);
660 		return;
661 
662 	case S_STEP1:
663 #define STEP1MASK       0174377
664 #define STEP1GOOD       (UDA_STEP2|UDA_IE|(NCMDL2<<3)|NRSPL2)
665 		if ((udaddr->udasa&STEP1MASK) != STEP1GOOD) {
666 			sc->sc_state = S_IDLE;
667 			wakeup((caddr_t)um);
668 			return;
669 		}
670 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)|
671 		    ((cpu == VAX_780) || (cpu == VAX_8600) ? UDA_PI : 0);
672 		sc->sc_state = S_STEP2;
673 		return;
674 
675 	case S_STEP2:
676 #define STEP2MASK       0174377
677 #define STEP2GOOD       (UDA_STEP3|UDA_IE|(sc->sc_ivec/4))
678 		if ((udaddr->udasa&STEP2MASK) != STEP2GOOD) {
679 			sc->sc_state = S_IDLE;
680 			wakeup((caddr_t)um);
681 			return;
682 		}
683 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)>>16;
684 		sc->sc_state = S_STEP3;
685 		return;
686 
687 	case S_STEP3:
688 #define STEP3MASK       0174000
689 #define STEP3GOOD       UDA_STEP4
690 		if ((udaddr->udasa&STEP3MASK) != STEP3GOOD) {
691 			sc->sc_state = S_IDLE;
692 			wakeup((caddr_t)um);
693 			return;
694 		}
695 		udamicro[d] = udaddr->udasa;
696 #ifdef	DEBUG
697 		printd("Uda%d Version %d model %d\n",d,udamicro[d]&0xF,
698 			(udamicro[d]>>4) & 0xF);
699 		/*
700 		 * Requesting the error status (|= 2)
701 		 * may hang older controllers.
702 		 */
703 		udaddr->udasa = UDA_GO | (udaerror? 2 : 0);
704 #endif
705 		udaddr->udasa = UDA_GO;
706 		sc->sc_state = S_SCHAR;
707 
708 		/*
709 		 * Initialize the data structures.
710 		 */
711 		uud = sc->sc_uda;
712 		for (i = 0; i < NRSP; i++) {
713 			ud->uda_ca.ca_rspdsc[i] = UDA_OWN|UDA_INT|
714 				(long)&uud->uda_rsp[i].mscp_cmdref;
715 			ud->uda_rsp[i].mscp_dscptr = &ud->uda_ca.ca_rspdsc[i];
716 			ud->uda_rsp[i].mscp_header.uda_msglen = mscp_msglen;
717 		}
718 		for (i = 0; i < NCMD; i++) {
719 			ud->uda_ca.ca_cmddsc[i] = UDA_INT|
720 				(long)&uud->uda_cmd[i].mscp_cmdref;
721 			ud->uda_cmd[i].mscp_dscptr = &ud->uda_ca.ca_cmddsc[i];
722 			ud->uda_cmd[i].mscp_header.uda_msglen = mscp_msglen;
723 		}
724 		bp = &udwtab[d];
725 		bp->av_forw = bp->av_back = bp;
726 		sc->sc_lastcmd = 1;
727 		sc->sc_lastrsp = 0;
728 		mp = &uda[um->um_ctlr].uda_cmd[0];
729 		mp->mscp_unit = mp->mscp_modifier = 0;
730 		mp->mscp_flags = 0;
731 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
732 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
733 		mp->mscp_opcode = M_OP_STCON;
734 		mp->mscp_cntflgs = M_CF_ATTN|M_CF_MISC|M_CF_THIS;
735 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
736 		i = udaddr->udaip;      /* initiate polling */
737 		return;
738 
739 	case S_SCHAR:
740 	case S_RUN:
741 		break;
742 
743 	default:
744 		printf("uda%d: interrupt in unknown state %d ignored\n",
745 			d, sc->sc_state);
746 		return;
747 	}
748 
749 	if (udaddr->udasa&UDA_ERR) {
750 		printf("uda(%d): fatal error (%o)\n", d, udaddr->udasa&0xffff);
751 		udaddr->udaip = 0;
752 		wakeup((caddr_t)um);
753 	}
754 
755 	/*
756 	 * Check for a buffer purge request.
757 	 */
758 	if (ud->uda_ca.ca_bdp) {
759 		/*
760 		 * THIS IS A KLUDGE.
761 		 * Maybe we should change the entire
762 		 * UBA interface structure.
763 		 */
764 		int s = spl6();		/* was spl7 but I don't like turning */
765 					/* off machine checks */
766 		i = um->um_ubinfo;
767 #ifdef	DEBUG
768 		printd("uda: purge bdp %d\n", ud->uda_ca.ca_bdp);
769 #endif
770 		um->um_ubinfo = ud->uda_ca.ca_bdp<<28;
771 		ubapurge(um);
772 		um->um_ubinfo = i;
773 		(void) splx(s);
774 		ud->uda_ca.ca_bdp = 0;
775 		udaddr->udasa = 0;      /* signal purge complete */
776 	}
777 
778 	/*
779 	 * Check for response ring transition.
780 	 */
781 	if (ud->uda_ca.ca_rspint) {
782 		ud->uda_ca.ca_rspint = 0;
783 		for (i = sc->sc_lastrsp;; i++) {
784 			i %= NRSP;
785 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
786 				break;
787 			udrsp(um, ud, sc, i);
788 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
789 		}
790 		sc->sc_lastrsp = i;
791 	}
792 
793 	/*
794 	 * Check for command ring transition.
795 	 */
796 	if (ud->uda_ca.ca_cmdint) {
797 #ifdef	DEBUG
798 		printd("uda: command ring transition\n");
799 #endif
800 		ud->uda_ca.ca_cmdint = 0;
801 	}
802 	if(uda_cp_wait)
803 		wakeup(&uda_cp_wait);
804 	(void) udstart(um);
805 }
806 
807 /*
808  * Process a response packet
809  */
810 udrsp(um, ud, sc, i)
811 	register struct uba_ctlr *um;
812 	register struct uda *ud;
813 	register struct uda_softc *sc;
814 	int i;
815 {
816 	register struct mscp *mp;
817 	struct uba_device *ui;
818 	struct buf *dp, *bp,nullbp;
819 	int st;
820 
821 	mp = &ud->uda_rsp[i];
822 	mp->mscp_header.uda_msglen = mscp_msglen;
823 	sc->sc_credits += mp->mscp_header.uda_credits & 0xf;  /* just 4 bits?*/
824 	if ((mp->mscp_header.uda_credits & 0xf0) > 0x10)	/* Check */
825 		return;
826 #ifdef	DEBUG
827 	printd10("udarsp, opcode 0x%x status 0x%x\n",mp->mscp_opcode,mp->mscp_status);
828 #endif
829 	/*
830 	 * If it's an error log message (datagram),
831 	 * pass it on for more extensive processing.
832 	 */
833 	if ((mp->mscp_header.uda_credits & 0xf0) == 0x10) {	/* check */
834 		uderror(um, (struct mslg *)mp);
835 		return;
836 	}
837 	st = mp->mscp_status&M_ST_MASK;
838 	/* The controller interrupts as drive 0 */
839 	/* this means that you must check for controller interrupts */
840 	/* before you check to see if there is a drive 0 */
841 	if((M_OP_STCON|M_OP_END) == mp->mscp_opcode){
842 		if (st == M_ST_SUCC)
843 			sc->sc_state = S_RUN;
844 		else
845 			sc->sc_state = S_IDLE;
846 		um->um_tab.b_active = 0;
847 		wakeup((caddr_t)um);
848 		return;
849 	}
850 	if (mp->mscp_unit >= 8)
851 		return;
852 	if ((ui = udip[um->um_ctlr][mp->mscp_unit]) == 0)
853 		return;
854 	switch (mp->mscp_opcode) {
855 
856 	case M_OP_ONLIN|M_OP_END:
857 		ra_info[ui->ui_unit].rastatus = st;
858 		ra_info[ui->ui_unit].ratype =  mp->mscp_mediaid;
859 		dp = &udutab[ui->ui_unit];
860 		if (st == M_ST_SUCC) {
861 			/*
862 			 * Link the drive onto the controller queue
863 			 */
864 			dp->b_forw = NULL;
865 			if (um->um_tab.b_actf == NULL)
866 				um->um_tab.b_actf = dp;
867 			else
868 				um->um_tab.b_actl->b_forw = dp;
869 			um->um_tab.b_actl = dp;
870 			ui->ui_flags = 1;       /* mark it online */
871 			ra_info[ui->ui_unit].radsize=(daddr_t)mp->mscp_untsize;
872 #ifdef	DEBUG
873 			printd("uda: unit %d online\n", mp->mscp_unit);
874 #endif
875 #define F_to_C(x,i)     ( ((x)->mscp_mediaid) >> (i*5+7) & 0x1f ? ( ( (((x)->mscp_mediaid) >>( i*5 + 7)) & 0x1f) + 'A' - 1): ' ')
876 		/* this mess decodes the Media type identifier */
877 #ifdef	DEBUG
878 			printd("uda: unit %d online %x %c%c %c%c%c%d\n"
879 				,mp->mscp_unit, mp->mscp_mediaid
880 				,F_to_C(mp,4),F_to_C(mp,3),F_to_C(mp,2)
881 				,F_to_C(mp,1),F_to_C(mp,0)
882 				,mp->mscp_mediaid & 0x7f);
883 #endif
884 			switch(mp->mscp_mediaid & 0x7f){
885 			case    25:
886 				ra_info[ui->ui_unit].ra_sizes = ra25_sizes;
887 				break;
888 			case    60:
889 				ra_info[ui->ui_unit].ra_sizes = ra60_sizes;
890 				break;
891 			case    80:
892 				ra_info[ui->ui_unit].ra_sizes = ra80_sizes;
893 				break;
894 			case    81:
895 				ra_info[ui->ui_unit].ra_sizes = ra81_sizes;
896 				break;
897 			default:
898 				ui->ui_flags = 0;       /* mark it offline */
899 				ra_info[ui->ui_unit].ratype = 0;
900 				printf("Don't have a parition table for ");
901 				printf("a %c%c %c%c%c%d\n"
902 				,F_to_C(mp,4),F_to_C(mp,3),F_to_C(mp,2)
903 				,F_to_C(mp,1),F_to_C(mp,0)
904 				,mp->mscp_mediaid & 0x7f);
905 				while (bp = dp->b_actf) {
906 					dp->b_actf = bp->av_forw;
907 					bp->b_flags |= B_ERROR;
908 					iodone(bp);
909 				}
910 			}
911 			dp->b_active = 1;
912 		} else {
913 			if(dp->b_actf){
914 				harderr(dp->b_actf,"ra");
915 			} else {
916 				nullbp.b_blkno = 0;
917 				nullbp.b_dev = makedev(UDADEVNUM,ui->ui_unit);
918 				harderr(&nullbp, "ra");
919 			}
920 			printf("OFFLINE\n");
921 			while (bp = dp->b_actf) {
922 				dp->b_actf = bp->av_forw;
923 				bp->b_flags |= B_ERROR;
924 				iodone(bp);
925 			}
926 		}
927 		if(mp->mscp_cmdref!=NULL){/* Seems to get lost sometimes */
928 			wakeup((caddr_t *) mp->mscp_cmdref);
929 		}
930 		break;
931 
932 /*
933  * The AVAILABLE ATTENTION messages occurs when the
934  * unit becomes available after spinup,
935  * marking the unit offline will force an online command
936  * prior to using the unit.
937  */
938 	case M_OP_AVATN:
939 #ifdef	DEBUG
940 		printd("uda: unit %d attention\n", mp->mscp_unit);
941 #endif
942 		ui->ui_flags = 0;       /* it went offline and we didn't notice */
943 		ra_info[ui->ui_unit].ratype =  mp->mscp_mediaid;
944 		break;
945 
946 	case M_OP_END:
947 /*
948  * An endcode without an opcode (0200) is an invalid command.
949  * The mscp specification states that this would be a protocol
950  * type error, such as illegal opcodes. The mscp spec. also
951  * states that parameter error type of invalid commands should
952  * return the normal end message for the command. This does not appear
953  * to be the case. An invalid logical block number returned an endcode
954  * of 0200 instead of the 0241 (read) that was expected.
955  */
956 
957 		printf("endcd=%o, stat=%o\n", mp->mscp_opcode, mp->mscp_status);
958 		break;
959 	case M_OP_READ|M_OP_END:
960 	case M_OP_WRITE|M_OP_END:
961 		bp = (struct buf *)mp->mscp_cmdref;
962 		ubarelse(um->um_ubanum, (int *)&bp->b_ubinfo);
963 		/*
964 		 * Unlink buffer from I/O wait queue.
965 		 */
966 		bp->av_back->av_forw = bp->av_forw;
967 		bp->av_forw->av_back = bp->av_back;
968 #if defined(VAX750)
969 		if (cpu == VAX_750
970 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
971 			if (um->um_ubinfo == 0)
972 				printf("udintr: um_ubinfo == 0\n");
973 			else
974 				ubarelse(um->um_ubanum, &um->um_ubinfo);
975 		}
976 #endif
977 		dp = &udutab[ui->ui_unit];
978 		dp->b_qsize--;
979 		if (ui->ui_dk >= 0)
980 			if (dp->b_qsize == 0)
981 				dk_busy &= ~(1<<ui->ui_dk);
982 		if (st == M_ST_OFFLN || st == M_ST_AVLBL) {
983 			ui->ui_flags = 0;       /* mark unit offline */
984 			/*
985 			 * Link the buffer onto the front of the drive queue
986 			 */
987 			if ((bp->av_forw = dp->b_actf) == 0)
988 				dp->b_actl = bp;
989 			dp->b_actf = bp;
990 			/*
991 			 * Link the drive onto the controller queue
992 			 */
993 			if (dp->b_active == 0) {
994 				dp->b_forw = NULL;
995 				if (um->um_tab.b_actf == NULL)
996 					um->um_tab.b_actf = dp;
997 				else
998 					um->um_tab.b_actl->b_forw = dp;
999 				um->um_tab.b_actl = dp;
1000 				dp->b_active = 1;
1001 			}
1002 #if defined(VAX750)
1003 			if (cpu == VAX750 && um->um_ubinfo == 0)
1004 				um->um_ubinfo =
1005 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
1006 					UBA_NEEDBDP);
1007 #endif
1008 			return;
1009 		}
1010 		if (st != M_ST_SUCC) {
1011 			harderr(bp, "ra");
1012 #ifdef	DEBUG
1013 			printd("status %o\n", mp->mscp_status);
1014 #endif
1015 			bp->b_flags |= B_ERROR;
1016 		}
1017 		bp->b_resid = bp->b_bcount - mp->mscp_bytecnt;
1018 		iodone(bp);
1019 		break;
1020 
1021 	case M_OP_GTUNT|M_OP_END:
1022 #ifdef	DEBUG
1023 		printd("GTUNT end packet status = 0x%x media id 0x%x\n"
1024 			,st,mp->mscp_mediaid);
1025 #endif
1026 		ra_info[ui->ui_unit].rastatus = st;
1027 		ra_info[ui->ui_unit].ratype =  mp->mscp_mediaid;
1028 		break;
1029 
1030 	default:
1031 		printf("uda: unknown packet\n");
1032 		uderror(um, (struct mslg *)mp);
1033 	}
1034 }
1035 
1036 
1037 /*
1038  * Process an error log message
1039  *
1040  * For now, just log the error on the console.
1041  * Only minimal decoding is done, only "useful"
1042  * information is printed.  Eventually should
1043  * send message to an error logger.
1044  */
1045 uderror(um, mp)
1046 	register struct uba_ctlr *um;
1047 	register struct mslg *mp;
1048 {
1049 	register	i;
1050 
1051 
1052 	if(!(mp->mslg_flags & (M_LF_SUCC | M_LF_CONT)))
1053 		printf("uda%d: hard error\n");
1054 
1055 	mprintf("uda%d: %s error, ", um->um_ctlr,
1056 		mp->mslg_flags & ( M_LF_SUCC | M_LF_CONT ) ? "soft" : "hard");
1057 	switch (mp->mslg_format) {
1058 	case M_FM_CNTERR:
1059 		mprintf("controller error, event 0%o\n", mp->mslg_event);
1060 		break;
1061 
1062 	case M_FM_BUSADDR:
1063 		mprintf("host memory access error, event 0%o, addr 0%o\n",
1064 			mp->mslg_event, mp->mslg_busaddr);
1065 		break;
1066 
1067 	case M_FM_DISKTRN:
1068 		mprintf("disk transfer error, unit %d, grp 0x%x, hdr 0x%x, event 0%o\n",
1069 			mp->mslg_unit, mp->mslg_group, mp->mslg_hdr,
1070 mp->mslg_event);
1071 		break;
1072 
1073 	case M_FM_SDI:
1074 		mprintf("SDI error, unit %d, event 0%o, hdr 0x%x\n",
1075 			mp->mslg_unit, mp->mslg_event, mp->mslg_hdr);
1076 		for(i = 0; i < 12;i++)
1077 			mprintf("\t0x%x",mp->mslg_sdistat[i] & 0xff);
1078 		mprintf("\n");
1079 		break;
1080 
1081 	case M_FM_SMLDSK:
1082 		mprintf("small disk error, unit %d, event 0%o, cyl %d\n",
1083 			mp->mslg_unit, mp->mslg_event, mp->mslg_sdecyl);
1084 		break;
1085 
1086 	default:
1087 		mprintf("unknown error, unit %d, format 0%o, event 0%o\n",
1088 			mp->mslg_unit, mp->mslg_format, mp->mslg_event);
1089 	}
1090 
1091 	if (udaerror) {
1092 		register long *p = (long *)mp;
1093 
1094 		for (i = 0; i < mp->mslg_header.uda_msglen; i += sizeof(*p))
1095 			printf("%x ", *p++);
1096 		printf("\n");
1097 	}
1098 }
1099 
1100 
1101 /*
1102  * Find an unused command packet
1103  */
1104 struct mscp *
1105 udgetcp(um)
1106 	struct uba_ctlr *um;
1107 {
1108 	register struct mscp *mp;
1109 	register struct udaca *cp;
1110 	register struct uda_softc *sc;
1111 	register int i;
1112 	int	s;
1113 
1114 	s = spl5();
1115 	cp = &uda[um->um_ctlr].uda_ca;
1116 	sc = &uda_softc[um->um_ctlr];
1117 	/*
1118 	 * If no credits, can't issue any commands
1119 	 * until some outstanding commands complete.
1120 	 */
1121 	i = sc->sc_lastcmd;
1122 	if(((cp->ca_cmddsc[i]&(UDA_OWN|UDA_INT))==UDA_INT)&&
1123 	    (sc->sc_credits >= 2)) {
1124 		sc->sc_credits--;       /* committed to issuing a command */
1125 		cp->ca_cmddsc[i] &= ~UDA_INT;
1126 		mp = &uda[um->um_ctlr].uda_cmd[i];
1127 		mp->mscp_unit = mp->mscp_modifier = 0;
1128 		mp->mscp_opcode = mp->mscp_flags = 0;
1129 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
1130 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
1131 		sc->sc_lastcmd = (i + 1) % NCMD;
1132 		(void) splx(s);
1133 		return(mp);
1134 	}
1135 	(void) splx(s);
1136 	return(NULL);
1137 }
1138 
1139 udread(dev, uio)
1140 	dev_t dev;
1141 	struct uio *uio;
1142 {
1143 	register int unit = udunit(dev);
1144 
1145 	if (unit >= nNRA)
1146 		return (ENXIO);
1147 	return (physio(udstrategy, &rudbuf[unit], dev, B_READ, minphys, uio));
1148 }
1149 
1150 udwrite(dev, uio)
1151 	dev_t dev;
1152 	struct uio *uio;
1153 {
1154 	register int unit = udunit(dev);
1155 
1156 	if (unit >= nNRA)
1157 		return (ENXIO);
1158 	return (physio(udstrategy, &rudbuf[unit], dev, B_WRITE, minphys, uio));
1159 }
1160 
1161 udreset(uban)
1162 	int uban;
1163 {
1164 	register struct uba_ctlr *um;
1165 	register struct uba_device *ui;
1166 	register struct buf *bp, *dp;
1167 	register int unit;
1168 	struct buf *nbp;
1169 	int d;
1170 
1171 	for (d = 0; d < NUDA; d++) {
1172 		if ((um = udminfo[d]) == 0 || um->um_ubanum != uban ||
1173 		    um->um_alive == 0)
1174 			continue;
1175 		printf(" uda%d", d);
1176 		um->um_tab.b_active = 0;
1177 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
1178 		uda_softc[d].sc_state = S_IDLE;
1179 		uda_softc[d].sc_mapped = 0;	/* Rich */
1180 		for (unit = 0; unit < nNRA; unit++) {
1181 			if ((ui = uddinfo[unit]) == 0)
1182 				continue;
1183 			if (ui->ui_alive == 0 || ui->ui_mi != um)
1184 				continue;
1185 			udutab[unit].b_active = 0;
1186 			udutab[unit].b_qsize = 0;
1187 		}
1188 		for (bp = udwtab[d].av_forw; bp != &udwtab[d]; bp = nbp) {
1189 			nbp = bp->av_forw;
1190 			bp->b_ubinfo = 0;
1191 			/*
1192 			 * Link the buffer onto the drive queue
1193 			 */
1194 			dp = &udutab[udunit(bp->b_dev)];
1195 			if (dp->b_actf == 0)
1196 				dp->b_actf = bp;
1197 			else
1198 				dp->b_actl->av_forw = bp;
1199 			dp->b_actl = bp;
1200 			bp->av_forw = 0;
1201 			/*
1202 			 * Link the drive onto the controller queue
1203 			 */
1204 			if (dp->b_active == 0) {
1205 				dp->b_forw = NULL;
1206 				if (um->um_tab.b_actf == NULL)
1207 					um->um_tab.b_actf = dp;
1208 				else
1209 					um->um_tab.b_actl->b_forw = dp;
1210 				um->um_tab.b_actl = dp;
1211 				dp->b_active = 1;
1212 			}
1213 		}
1214 		udinit(d);
1215 	}
1216 }
1217 
1218 #define DBSIZE 32
1219 
1220 #define ca_Rspdsc       ca_rspdsc[0]
1221 #define ca_Cmddsc       ca_rspdsc[1]
1222 #define uda_Rsp         uda_rsp[0]
1223 #define uda_Cmd         uda_cmd[0]
1224 
1225 struct  uda     udad[NUDA];
1226 
1227 uddump(dev)
1228 	dev_t dev;
1229 {
1230 	struct udadevice *udaddr;
1231 	struct uda *ud_ubaddr;
1232 	char *start;
1233 	int num, blk, unit;
1234 	int maxsz;
1235 	int blkoff;
1236 	register struct uba_regs *uba;
1237 	register struct uba_device *ui;
1238 	register struct uda *udp;
1239 	register struct pte *io;
1240 	register int i;
1241 	struct  size    *rasizes;
1242 	unit = udunit(dev);
1243 	if (unit >= nNRA)
1244 		return (ENXIO);
1245 #define phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
1246 	ui = phys(struct uba_device *, uddinfo[unit]);
1247 	if (ui->ui_alive == 0)
1248 		return (ENXIO);
1249 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
1250 	ubainit(uba);
1251 	udaddr = (struct udadevice *)ui->ui_physaddr;
1252 	DELAY(2000000);
1253 	udp = phys(struct uda *, &udad[ui->ui_ctlr]);
1254 
1255 	num = btoc(sizeof(struct uda)) + 1;
1256 	io = &uba->uba_map[NUBMREG-num];
1257 	for(i = 0; i<num; i++)
1258 		*(int *)io++ = UBAMR_MRV|(btop(udp)+i);
1259 	ud_ubaddr = (struct uda *)(((int)udp & PGOFSET)|((NUBMREG-num)<<9));
1260 
1261 	udaddr->udaip = 0;
1262 	while ((udaddr->udasa & UDA_STEP1) == 0)
1263 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1264 	udaddr->udasa = UDA_ERR;
1265 	while ((udaddr->udasa & UDA_STEP2) == 0)
1266 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1267 	udaddr->udasa = (short)&ud_ubaddr->uda_ca.ca_ringbase;
1268 	while ((udaddr->udasa & UDA_STEP3) == 0)
1269 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1270 	udaddr->udasa = (short)(((int)&ud_ubaddr->uda_ca.ca_ringbase) >> 16);
1271 	while ((udaddr->udasa & UDA_STEP4) == 0)
1272 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1273 	udaddr->udasa = UDA_GO;
1274 	udp->uda_ca.ca_Rspdsc = (long)&ud_ubaddr->uda_Rsp.mscp_cmdref;
1275 	udp->uda_ca.ca_Cmddsc = (long)&ud_ubaddr->uda_Cmd.mscp_cmdref;
1276 	udp->uda_Cmd.mscp_cntflgs = 0;
1277 	udp->uda_Cmd.mscp_version = 0;
1278 	if (udcmd(M_OP_STCON, udp, udaddr) == 0) {
1279 		return(EFAULT);
1280 	}
1281 	udp->uda_Cmd.mscp_unit = ui->ui_slave;
1282 	if (udcmd(M_OP_ONLIN, udp, udaddr) == 0) {
1283 		return(EFAULT);
1284 	}
1285 
1286 	num = maxfree;
1287 	start = 0;
1288 	rasizes = ra_info[ui->ui_unit].ra_sizes;
1289 	maxsz = rasizes[minor(dev)&07].nblocks;
1290 	blkoff = rasizes[minor(dev)&07].blkoff;
1291 	if(maxsz < 0)
1292 		maxsz = ra_info[unit].radsize-blkoff;
1293 	if (dumplo < 0)
1294 		return (EINVAL);
1295 	if (dumplo + num >= maxsz)
1296 		num = maxsz - dumplo;
1297 	blkoff += dumplo;
1298 	while (num > 0) {
1299 		blk = num > DBSIZE ? DBSIZE : num;
1300 		io = uba->uba_map;
1301 		for (i = 0; i < blk; i++)
1302 			*(int *)io++ = (btop(start)+i) | UBAMR_MRV;
1303 		*(int *)io = 0;
1304 		udp->uda_Cmd.mscp_lbn = btop(start) + blkoff;
1305 		udp->uda_Cmd.mscp_unit = ui->ui_slave;
1306 		udp->uda_Cmd.mscp_bytecnt = blk*NBPG;
1307 		udp->uda_Cmd.mscp_buffer = 0;
1308 		if (udcmd(M_OP_WRITE, udp, udaddr) == 0) {
1309 			return(EIO);
1310 		}
1311 		start += blk*NBPG;
1312 		num -= blk;
1313 	}
1314 	return (0);
1315 }
1316 
1317 
1318 udcmd(op, udp, udaddr)
1319 	int op;
1320 	register struct uda *udp;
1321 	struct udadevice *udaddr;
1322 {
1323 	int i;
1324 
1325 #ifdef	lint
1326 	i = i;
1327 #endif
1328 
1329 	udp->uda_Cmd.mscp_opcode = op;
1330 	udp->uda_Rsp.mscp_header.uda_msglen = mscp_msglen;
1331 	udp->uda_Cmd.mscp_header.uda_msglen = mscp_msglen;
1332 	udp->uda_ca.ca_Rspdsc |= UDA_OWN|UDA_INT;
1333 	udp->uda_ca.ca_Cmddsc |= UDA_OWN|UDA_INT;
1334 	if (udaddr->udasa&UDA_ERR)
1335 		printf("Udaerror udasa (%x)\n", udaddr->udasa&0xffff);
1336 	i = udaddr->udaip;
1337 	for (;;) {
1338 		if (udp->uda_ca.ca_cmdint)
1339 			udp->uda_ca.ca_cmdint = 0;
1340 		if (udp->uda_ca.ca_rspint)
1341 			break;
1342 	}
1343 	udp->uda_ca.ca_rspint = 0;
1344 	if (udp->uda_Rsp.mscp_opcode != (op|M_OP_END) ||
1345 	    (udp->uda_Rsp.mscp_status&M_ST_MASK) != M_ST_SUCC) {
1346 		printf("error: com %d opc 0x%x stat 0x%x\ndump ",
1347 			op,
1348 			udp->uda_Rsp.mscp_opcode,
1349 			udp->uda_Rsp.mscp_status);
1350 		return(0);
1351 	}
1352 	return(1);
1353 }
1354 
1355 udsize(dev)
1356 	dev_t dev;
1357 {
1358 	int unit = udunit(dev);
1359 	struct uba_device *ui;
1360 	struct	size	*rasizes;
1361 
1362 	if (unit >= nNRA || (ui = uddinfo[unit]) == 0 || ui->ui_alive == 0
1363 		 || ui->ui_flags == 0)
1364 		return (-1);
1365 	rasizes = ra_info[ui->ui_unit].ra_sizes;
1366 	return (rasizes[minor(dev) & 07].nblocks);
1367 }
1368 
1369 #endif
1370