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