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