xref: /csrg-svn/sys/vax/uba/uda.c (revision 31022)
1 /*
2  *	@(#)uda.c	7.5 (Berkeley) 05/04/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 uda_softc *sc;
391 	struct uba_ctlr *um;
392 	register struct ra_info *ra = &ra_info[unit];
393 	int s, mask = (1 << udpart(dev));
394 
395 	um = udminfo[unit];
396 	sc = &uda_softc[um->um_ctlr];
397 	switch (fmt) {
398 	case S_IFCHR:
399 		ra->copenpart &= ~mask;
400 		break;
401 	case S_IFBLK:
402 		ra->bopenpart &= ~mask;
403 		break;
404 	}
405 	if (((ra->copenpart | ra->bopenpart) & mask) == 0)
406 		ra->openpart &= ~mask;
407 	/*
408 	 * Should wait for I/O to complete on this partition
409 	 * even if others are open, but wait for work on blkflush().
410 	 */
411 	if (ra->openpart == 0) {
412 		s = spl5();
413 		while (udutab[unit].b_actf)
414 			sleep((caddr_t)&udutab[unit], PZERO - 1);
415 		splx(s);
416 		ra->rastate = CLOSED;
417 	}
418 	return (0);
419 }
420 
421 /*
422  * Initialize a UDA.  Set up UBA mapping registers,
423  * initialize data structures, and start hardware
424  * initialization sequence.
425  */
426 udinit(d)
427 	int d;
428 {
429 	register struct uda_softc *sc;
430 	register struct uda *ud;
431 	struct udadevice *udaddr;
432 	struct uba_ctlr *um;
433 
434 	sc = &uda_softc[d];
435 	um = udminfo[d];
436 	um->um_tab.b_active++;
437 	ud = &uda[d];
438 	udaddr = (struct udadevice *)um->um_addr;
439 	if (sc->sc_mapped == 0) {
440 		/*
441 		 * Map the communications area and command
442 		 * and response packets into Unibus address
443 		 * space.
444 		 */
445 		sc->sc_ubainfo = uballoc(um->um_ubanum, (caddr_t)ud,
446 		    sizeof (struct uda), 0);
447 		sc->sc_uda = (struct uda *)(sc->sc_ubainfo & 0x3ffff);
448 		sc->sc_mapped = 1;
449 	}
450 
451 	/*
452 	 * Start the hardware initialization sequence.
453 	 */
454 
455 	if (udaburst[d] == 0)
456 		udaburst[d] = UDABURST;
457  	udaddr->udaip = 0;              /* start initialization */
458 
459 	while((udaddr->udasa & UDA_STEP1) == 0){
460 		if(udaddr->udasa & UDA_ERR)
461 			return(0);	/* CHECK */
462 	}
463 	udaddr->udasa=UDA_ERR|(NCMDL2<<11)|(NRSPL2<<8)|UDA_IE|(sc->sc_ivec/4);
464 	/*
465 	 * Initialization continues in interrupt routine.
466 	 */
467 	sc->sc_state = S_STEP1;
468 	sc->sc_credits = 0;
469 	return(1);
470 }
471 
472 /*
473  * Initialize a drive:
474  * bring on line and read in pack label.
475  */
476 rainit(ui, flags)
477 	register struct uba_device *ui;
478 {
479 	register struct mscp *mp;
480 	register struct disklabel *lp;
481 	register struct uda_softc *sc;
482 	register unit = ui->ui_unit;
483 	register struct ra_info *ra = &ra_info[unit];
484 	struct udadevice *udaddr;
485 	char *msg, *readdisklabel();
486 	int s, i;
487 	extern int cold;
488 
489 	lp = &udlabel[unit];
490 	sc = &uda_softc[ui->ui_ctlr];
491 
492 	if (ui->ui_flags == 0) {
493 		/* check to see if the device is really there. */
494 		/* this code was taken from Fred Canters 11 driver */
495 		udaddr = (struct udadevice *) ui->ui_mi->um_addr;
496 
497 		ra->rastate = WANTOPEN;
498 		s = spl5();
499 		while(0 ==(mp = udgetcp(ui->ui_mi))){
500 			uda_cp_wait++;
501 			sleep((caddr_t)&uda_cp_wait,PSWP+1);
502 			uda_cp_wait--;
503 		}
504 		mp->mscp_opcode = M_OP_ONLIN;
505 		mp->mscp_unit = ui->ui_slave;
506 			/* need to sleep on something */
507 		mp->mscp_cmdref = (long)ra;
508 #ifdef	DEBUG
509 		printd("uda: bring unit %d online\n",unit);
510 #endif
511 		*((long *) mp->mscp_dscptr ) |= UDA_OWN | UDA_INT ;
512 		i = udaddr->udaip;
513 #ifdef	lint
514 		i = i;
515 #endif
516 			/* make sure we wake up */
517 		if (cold) {
518 			(void) splx(s);
519 			for (i = 10*1000; ra->rastate == WANTOPEN && --i; )
520 				DELAY(1000);
521 		} else {
522 			timeout(wakeup, (caddr_t)ra, 10 * hz);
523 			sleep((caddr_t)ra, PSWP+1);
524 			/*wakeup in udrsp() */
525 			(void) splx(s);
526 		}
527 		if (ra->rastate != OPENRAW) {
528 			ra->rastate = CLOSED;
529 			return (EIO);
530 		}
531 	}
532 
533 	lp->d_secsize = DEV_BSIZE;
534 	lp->d_secperunit = ra->radsize;
535 
536 	if (flags & O_NDELAY)
537 		return (0);
538 	ra->rastate = RDLABEL;
539 	/*
540 	 * Set up default sizes until we've read the label,
541 	 * or longer if there isn't one there.
542 	 * Set secpercyl, as readdisklabel wants to compute b_cylin
543 	 * (although we don't need it).
544 	 */
545 	lp->d_secpercyl = 1;
546 	lp->d_npartitions = 1;
547 	lp->d_partitions[0].p_size = lp->d_secperunit;
548 	lp->d_partitions[0].p_offset = 0;
549 	/*
550 	 * Read pack label.
551 	 */
552 	if (msg = readdisklabel(udminor(unit, 0), udstrategy, lp)) {
553 		log(LOG_ERR, "ra%d: %s\n", unit, msg);
554 #ifdef COMPAT_42
555 		if (udmaptype(unit, lp))
556 			ra->rastate = OPEN;
557 		else
558 			ra->rastate = OPENRAW;
559 #else
560 		ra->rastate = OPENRAW;
561 #endif
562 	} else
563 		ra->rastate = OPEN;
564 	wakeup((caddr_t)ra);
565 	return (0);
566 }
567 
568 udstrategy(bp)
569 	register struct buf *bp;
570 {
571 	register struct uba_device *ui;
572 	register struct uba_ctlr *um;
573 	register struct buf *dp;
574 	register struct disklabel *lp;
575 	register int unit;
576 	struct uda_softc *sc;
577 	int xunit = udpart(bp->b_dev);
578 	daddr_t sz, maxsz;
579 	int s;
580 
581 	unit = udunit(bp->b_dev);
582 	if (unit >= NRA) {
583 		bp->b_error = ENXIO;
584 		goto bad;
585 	}
586 	ui = uddinfo[unit];
587 	lp = &udlabel[unit];
588 	sc = &uda_softc[ui->ui_ctlr];
589 	um = ui->ui_mi;
590 	if (ui == 0 || ui->ui_alive == 0 || ra_info[unit].rastate == CLOSED) {
591 		bp->b_error = ENXIO;
592 		goto bad;
593 	}
594 	if (ra_info[unit].rastate < OPEN)
595 		goto q;
596 	if ((ra_info[unit].openpart & (1 << xunit)) == 0) {
597 		bp->b_error = ENODEV;
598 		goto bad;
599 	}
600 	maxsz = lp->d_partitions[xunit].p_size;
601 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
602 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
603 		if (bp->b_blkno == maxsz) {
604 			bp->b_resid = bp->b_bcount;
605 			goto done;
606 		}
607 		sz = maxsz - bp->b_blkno;
608 		if (sz <= 0) {
609 			bp->b_error = EINVAL;
610 			goto bad;
611 		}
612 		bp->b_bcount = sz << DEV_BSHIFT;
613 	}
614 q:
615 	s = spl5();
616 	/*
617 	 * Link the buffer onto the drive queue
618 	 */
619 	dp = &udutab[ui->ui_unit];
620 	if (dp->b_actf == 0)
621 		dp->b_actf = bp;
622 	else
623 		dp->b_actl->av_forw = bp;
624 	dp->b_actl = bp;
625 	bp->av_forw = 0;
626 	/*
627 	 * Link the drive onto the controller queue
628 	 */
629 	if (dp->b_active == 0) {
630 		dp->b_forw = NULL;
631 		if (um->um_tab.b_actf == NULL)
632 			um->um_tab.b_actf = dp;
633 		else
634 			um->um_tab.b_actl->b_forw = dp;
635 		um->um_tab.b_actl = dp;
636 		dp->b_active = 1;
637 	}
638 	if (um->um_tab.b_active == 0) {
639 #if defined(VAX750)
640 		if (cpu == VAX_750
641 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
642 			if (um->um_ubinfo != 0) {
643 				printd("udastrat: ubinfo 0x%x\n",um->um_ubinfo);
644 			} else
645 				um->um_ubinfo =
646 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
647 					UBA_NEEDBDP);
648 		}
649 #endif
650 		(void) udstart(um);
651 	}
652 	splx(s);
653 	return;
654 
655 bad:
656 	bp->b_flags |= B_ERROR;
657 done:
658 	iodone(bp);
659 	return;
660 }
661 
662 udstart(um)
663 	register struct uba_ctlr *um;
664 {
665 	register struct buf *bp, *dp;
666 	register struct mscp *mp;
667 	register struct uda_softc *sc;
668 	register struct uba_device *ui;
669 	struct disklabel *lp;
670 	struct udadevice *udaddr;
671 	struct uda *ud = &uda[um->um_ctlr];
672 	daddr_t sz;
673 	int i;
674 
675 	sc = &uda_softc[um->um_ctlr];
676 
677 loop:
678 	if ((dp = um->um_tab.b_actf) == NULL) {
679 
680 		um->um_tab.b_active = 0;
681 		/* Check for response ring transitions lost in the
682 		 * Race condition
683 		 */
684 		for (i = sc->sc_lastrsp;; i++) {
685 			i %= NRSP;
686 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
687 				break;
688 			udrsp(um, ud, sc, i);
689 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
690 		}
691 		sc->sc_lastrsp = i;
692 		return (0);
693 	}
694 	if ((bp = dp->b_actf) == NULL) {
695 		/*
696 		 * No more requests for this drive, remove
697 		 * from controller queue and look at next drive.
698 		 * We know we're at the head of the controller queue.
699 		 */
700 		dp->b_active = 0;
701 		um->um_tab.b_actf = dp->b_forw;
702 		if (ra_info[dp - udutab].openpart == 0)
703 			wakeup((caddr_t)dp);
704 		goto loop;		/* Need to check for loop */
705 	}
706 	um->um_tab.b_active++;
707 	udaddr = (struct udadevice *)um->um_addr;
708 	if ((udaddr->udasa&UDA_ERR) || sc->sc_state != S_RUN) {
709 		harderr(bp, "ra");
710 		mprintf("Uda%d udasa %o, state %d\n",um->um_ctlr , udaddr->udasa&0xffff, sc->sc_state);
711 		(void)udinit(um->um_ctlr);
712 		/* SHOULD REQUEUE OUTSTANDING REQUESTS, LIKE UDRESET */
713 		return (0);
714 	}
715 	ui = uddinfo[udunit(bp->b_dev)];
716 	lp = &udlabel[ui->ui_unit];
717 	if (ui->ui_flags == 0) {        /* not online */
718 		if ((mp = udgetcp(um)) == NULL){
719 			return (0);
720 		}
721 		mp->mscp_opcode = M_OP_ONLIN;
722 		mp->mscp_unit = ui->ui_slave;
723 		dp->b_active = 2;
724 		um->um_tab.b_actf = dp->b_forw; /* remove from controller q */
725 #ifdef	DEBUG
726 		printd("uda: bring unit %d online\n", ui->ui_slave);
727 #endif
728 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
729 		if (udaddr->udasa&UDA_ERR)
730 			printf("Uda (%d) Error (%x)\n",um->um_ctlr , udaddr->udasa&0xffff);
731 		i = udaddr->udaip;
732 		goto loop;
733 	}
734 	switch (cpu) {
735 	case VAX_8600:
736 	case VAX_780:
737 		i = UBA_NEEDBDP|UBA_CANTWAIT;
738 		break;
739 
740 	case VAX_750:
741 		i = um->um_ubinfo|UBA_HAVEBDP|UBA_CANTWAIT;
742 		break;
743 
744 	case VAX_730:
745 	case VAX_630:
746 		i = UBA_CANTWAIT;
747 		break;
748 	}
749 	if ((i = ubasetup(um->um_ubanum, bp, i)) == 0)
750 		return(1);
751 	if ((mp = udgetcp(um)) == NULL) {
752 #if defined(VAX750)
753 		if (cpu == VAX_750)
754 			i &= 0xfffffff;         /* mask off bdp */
755 #endif
756 		ubarelse(um->um_ubanum,&i);
757 		return(0);
758 	}
759 	mp->mscp_cmdref = (long)bp;     /* pointer to get back */
760 	mp->mscp_opcode = bp->b_flags&B_READ ? M_OP_READ : M_OP_WRITE;
761 	mp->mscp_unit = ui->ui_slave;
762 	mp->mscp_buffer = (i & 0x3ffff) | (((i>>28)&0xf)<<24);
763 #if defined(VAX750)
764 	if (cpu == VAX_750)
765 		i &= 0xfffffff;         /* mask off bdp */
766 #endif
767 	bp->b_ubinfo = i;               /* save mapping info */
768 	i = udpart(bp->b_dev);
769 	mp->mscp_lbn = bp->b_blkno +
770 	    lp->d_partitions[i].p_offset;
771 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
772 	if (bp->b_blkno + sz > lp->d_partitions[i].p_size)
773 		mp->mscp_bytecnt = (lp->d_partitions[i].p_size - bp->b_blkno) >>
774 		    DEV_BSHIFT;
775 	else
776 		mp->mscp_bytecnt = bp->b_bcount;
777 	*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
778 	if (udaddr->udasa&UDA_ERR)
779 		printf("Uda(%d) udasa (%x)\n",um->um_ctlr , udaddr->udasa&0xffff);
780 	i = udaddr->udaip;              /* initiate polling */
781 	dp->b_qsize++;
782 	if (ui->ui_dk >= 0) {
783 		dk_busy |= 1<<ui->ui_dk;
784 		dk_xfer[ui->ui_dk]++;
785 		dk_wds[ui->ui_dk] += bp->b_bcount>>6;
786 	}
787 
788 	/*
789 	 * Move drive to the end of the controller queue
790 	 */
791 	if (dp->b_forw != NULL) {
792 		um->um_tab.b_actf = dp->b_forw;
793 		um->um_tab.b_actl->b_forw = dp;
794 		um->um_tab.b_actl = dp;
795 		dp->b_forw = NULL;
796 	}
797 	/*
798 	 * Move buffer to I/O wait queue
799 	 */
800 	dp->b_actf = bp->av_forw;
801 	dp = &udwtab[um->um_ctlr];
802 	bp->av_forw = dp;
803 	bp->av_back = dp->av_back;
804 	dp->av_back->av_forw = bp;
805 	dp->av_back = bp;
806 	goto loop;
807 }
808 
809 /*
810  * UDA interrupt routine.
811  */
812 udintr(d)
813 	register d;
814 {
815 	struct uba_ctlr *um = udminfo[d];
816 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
817 	struct buf *bp;
818 	register int i;
819 	register struct uda_softc *sc = &uda_softc[d];
820 	register struct uda *ud = &uda[d];
821 	struct uda *uud;
822 	register struct mscp *mp;
823 
824 #ifdef	DEBUG
825 	printd10("udintr: state %d, udasa %o\n", sc->sc_state, udaddr->udasa);
826 #endif
827 #ifdef VAX630
828 	(void) spl5();
829 #endif
830 	switch (sc->sc_state) {
831 	case S_IDLE:
832 		printf("uda%d: random interrupt ignored\n", d);
833 		return;
834 
835 	case S_STEP1:
836 #define STEP1MASK       0174377
837 #define STEP1GOOD       (UDA_STEP2|UDA_IE|(NCMDL2<<3)|NRSPL2)
838 		if ((udaddr->udasa&STEP1MASK) != STEP1GOOD) {
839 			sc->sc_state = S_IDLE;
840 			wakeup((caddr_t)um);
841 			return;
842 		}
843 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)|
844 		    ((cpu == VAX_780) || (cpu == VAX_8600) ? UDA_PI : 0);
845 		sc->sc_state = S_STEP2;
846 		return;
847 
848 	case S_STEP2:
849 #define STEP2MASK       0174377
850 #define STEP2GOOD       (UDA_STEP3|UDA_IE|(sc->sc_ivec/4))
851 		if ((udaddr->udasa&STEP2MASK) != STEP2GOOD) {
852 			sc->sc_state = S_IDLE;
853 			wakeup((caddr_t)um);
854 			return;
855 		}
856 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)>>16;
857 		sc->sc_state = S_STEP3;
858 		return;
859 
860 	case S_STEP3:
861 #define STEP3MASK       0174000
862 #define STEP3GOOD       UDA_STEP4
863 		if ((udaddr->udasa&STEP3MASK) != STEP3GOOD) {
864 			sc->sc_state = S_IDLE;
865 			wakeup((caddr_t)um);
866 			return;
867 		}
868 		udamicro[d] = udaddr->udasa;
869 		log(LOG_INFO, "uda%d: version %d model %d\n", d,
870 		    udamicro[d] & 0xf, (udamicro[d] >> 4) & 0xf);
871 		/*
872 		 * Requesting the error status (|= 2)
873 		 * may hang older controllers.
874 		 */
875 		i = UDA_GO | (udaerror? 2 : 0);
876 		if (udaburst[d])
877 			i |= (udaburst[d] - 1) << 2;
878 		udaddr->udasa = i;
879 		udaddr->udasa = UDA_GO;
880 		sc->sc_state = S_SCHAR;
881 
882 		/*
883 		 * Initialize the data structures.
884 		 */
885 		uud = sc->sc_uda;
886 		for (i = 0; i < NRSP; i++) {
887 			ud->uda_ca.ca_rspdsc[i] = UDA_OWN|UDA_INT|
888 				(long)&uud->uda_rsp[i].mscp_cmdref;
889 			ud->uda_rsp[i].mscp_dscptr = &ud->uda_ca.ca_rspdsc[i];
890 			ud->uda_rsp[i].mscp_header.uda_msglen = mscp_msglen;
891 		}
892 		for (i = 0; i < NCMD; i++) {
893 			ud->uda_ca.ca_cmddsc[i] = UDA_INT|
894 				(long)&uud->uda_cmd[i].mscp_cmdref;
895 			ud->uda_cmd[i].mscp_dscptr = &ud->uda_ca.ca_cmddsc[i];
896 			ud->uda_cmd[i].mscp_header.uda_msglen = mscp_msglen;
897 		}
898 		bp = &udwtab[d];
899 		bp->av_forw = bp->av_back = bp;
900 		sc->sc_lastcmd = 1;
901 		sc->sc_lastrsp = 0;
902 		mp = &uda[um->um_ctlr].uda_cmd[0];
903 		mp->mscp_unit = mp->mscp_modifier = 0;
904 		mp->mscp_flags = 0;
905 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
906 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
907 		mp->mscp_opcode = M_OP_STCON;
908 		mp->mscp_cntflgs = M_CF_ATTN|M_CF_MISC|M_CF_THIS;
909 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
910 		i = udaddr->udaip;      /* initiate polling */
911 		return;
912 
913 	case S_SCHAR:
914 	case S_RUN:
915 		break;
916 
917 	default:
918 		printf("uda%d: interrupt in unknown state %d ignored\n",
919 			d, sc->sc_state);
920 		return;
921 	}
922 
923 	if (udaddr->udasa&UDA_ERR) {
924 		printf("uda(%d): fatal error (%o)\n", d, udaddr->udasa&0xffff);
925 		udaddr->udaip = 0;
926 		wakeup((caddr_t)um);
927 	}
928 
929 	/*
930 	 * Check for a buffer purge request.
931 	 */
932 	if (ud->uda_ca.ca_bdp) {
933 #ifdef	DEBUG
934 		printd("uda: purge bdp %d\n", ud->uda_ca.ca_bdp);
935 #endif
936 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
937 		ud->uda_ca.ca_bdp = 0;
938 		udaddr->udasa = 0;      /* signal purge complete */
939 	}
940 
941 	/*
942 	 * Check for response ring transition.
943 	 */
944 	if (ud->uda_ca.ca_rspint) {
945 		ud->uda_ca.ca_rspint = 0;
946 		for (i = sc->sc_lastrsp;; i++) {
947 			i %= NRSP;
948 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
949 				break;
950 			udrsp(um, ud, sc, i);
951 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
952 		}
953 		sc->sc_lastrsp = i;
954 	}
955 
956 	/*
957 	 * Check for command ring transition.
958 	 */
959 	if (ud->uda_ca.ca_cmdint) {
960 #ifdef	DEBUG
961 		printd("uda: command ring transition\n");
962 #endif
963 		ud->uda_ca.ca_cmdint = 0;
964 	}
965 	if(uda_cp_wait)
966 		wakeup((caddr_t)&uda_cp_wait);
967 	(void) udstart(um);
968 }
969 
970 /*
971  * Process a response packet
972  */
973 udrsp(um, ud, sc, i)
974 	register struct uba_ctlr *um;
975 	register struct uda *ud;
976 	register struct uda_softc *sc;
977 	int i;
978 {
979 	register struct mscp *mp;
980 	register struct uba_device *ui;
981 	register int unit;
982 	struct buf *dp, *bp, nullbp;
983 	int st;
984 
985 	mp = &ud->uda_rsp[i];
986 	mp->mscp_header.uda_msglen = mscp_msglen;
987 	sc->sc_credits += mp->mscp_header.uda_credits & 0xf;  /* just 4 bits?*/
988 	if ((mp->mscp_header.uda_credits & 0xf0) > 0x10)	/* Check */
989 		return;
990 #ifdef	DEBUG
991 	printd10("udarsp, opcode 0x%x status 0x%x\n",mp->mscp_opcode,mp->mscp_status);
992 #endif
993 	/*
994 	 * If it's an error log message (datagram),
995 	 * pass it on for more extensive processing.
996 	 */
997 	if ((mp->mscp_header.uda_credits & 0xf0) == 0x10) {	/* check */
998 		uderror(um, (struct mslg *)mp);
999 		return;
1000 	}
1001 	st = mp->mscp_status&M_ST_MASK;
1002 	/* The controller interrupts as drive 0 */
1003 	/* this means that you must check for controller interrupts */
1004 	/* before you check to see if there is a drive 0 */
1005 	if((M_OP_STCON|M_OP_END) == mp->mscp_opcode){
1006 		if (st == M_ST_SUCC)
1007 			sc->sc_state = S_RUN;
1008 		else
1009 			sc->sc_state = S_IDLE;
1010 		um->um_tab.b_active = 0;
1011 		wakeup((caddr_t)um);
1012 		return;
1013 	}
1014 	if (mp->mscp_unit >= 8)
1015 		return;
1016 	if ((ui = udip[um->um_ctlr][mp->mscp_unit]) == 0)
1017 		return;
1018 	unit = ui->ui_unit;
1019 	switch (mp->mscp_opcode) {
1020 
1021 	case M_OP_ONLIN|M_OP_END:
1022 		ra_info[unit].rastatus = st;
1023 		ra_info[unit].ratype =  mp->mscp_mediaid;
1024 		dp = &udutab[unit];
1025 		if (st == M_ST_SUCC) {
1026 			/*
1027 			 * Link the drive onto the controller queue
1028 			 */
1029 			dp->b_forw = NULL;
1030 			if (um->um_tab.b_actf == NULL)
1031 				um->um_tab.b_actf = dp;
1032 			else
1033 				um->um_tab.b_actl->b_forw = dp;
1034 			um->um_tab.b_actl = dp;
1035 			ui->ui_flags = 1;       /* mark it online */
1036 			ra_info[unit].rastate = OPENRAW;
1037 			ra_info[unit].radsize=(daddr_t)mp->mscp_untsize;
1038 #ifdef	DEBUG
1039 			printd("uda: unit %d online\n", mp->mscp_unit);
1040 #endif
1041 #define F_to_C(x,i)     ( ((x)->mscp_mediaid) >> (i*5+7) & 0x1f ? ( ( (((x)->mscp_mediaid) >>( i*5 + 7)) & 0x1f) + 'A' - 1): ' ')
1042 		/* this mess decodes the Media type identifier */
1043 #ifdef	DEBUG
1044 			printd("uda: unit %d online %x %c%c %c%c%c%d\n"
1045 				,mp->mscp_unit, mp->mscp_mediaid
1046 				,F_to_C(mp,4),F_to_C(mp,3),F_to_C(mp,2)
1047 				,F_to_C(mp,1),F_to_C(mp,0)
1048 				,mp->mscp_mediaid & 0x7f);
1049 #endif
1050 			dp->b_active = 1;
1051 		} else {
1052 			if(dp->b_actf){
1053 				harderr(dp->b_actf,"ra");
1054 			} else {
1055 				nullbp.b_blkno = 0;
1056 				nullbp.b_dev = makedev(UDADEVNUM,unit);
1057 				harderr(&nullbp, "ra");
1058 			}
1059 			printf("OFFLINE\n");
1060 			while (bp = dp->b_actf) {
1061 				dp->b_actf = bp->av_forw;
1062 				bp->b_flags |= B_ERROR;
1063 				iodone(bp);
1064 			}
1065 			ra_info[unit].rastate = CLOSED;
1066 		}
1067 		if(mp->mscp_cmdref!=NULL){/* Seems to get lost sometimes */
1068 			wakeup((caddr_t)mp->mscp_cmdref);
1069 		}
1070 		break;
1071 
1072 /*
1073  * The AVAILABLE ATTENTION messages occurs when the
1074  * unit becomes available after spinup,
1075  * marking the unit offline will force an online command
1076  * prior to using the unit.
1077  */
1078 	case M_OP_AVATN:
1079 #ifdef	DEBUG
1080 		printd("uda: unit %d attention\n", mp->mscp_unit);
1081 #endif
1082 		ui->ui_flags = 0;       /* it went offline and we didn't notice */
1083 		ra_info[unit].ratype =  mp->mscp_mediaid;
1084 		break;
1085 
1086 	case M_OP_END:
1087 /*
1088  * An endcode without an opcode (0200) is an invalid command.
1089  * The mscp specification states that this would be a protocol
1090  * type error, such as illegal opcodes. The mscp spec. also
1091  * states that parameter error type of invalid commands should
1092  * return the normal end message for the command. This does not appear
1093  * to be the case. An invalid logical block number returned an endcode
1094  * of 0200 instead of the 0241 (read) that was expected.
1095  */
1096 
1097 		printf("endcd=%o, stat=%o\n", mp->mscp_opcode, mp->mscp_status);
1098 		break;
1099 	case M_OP_READ|M_OP_END:
1100 	case M_OP_WRITE|M_OP_END:
1101 		bp = (struct buf *)mp->mscp_cmdref;
1102 		ubarelse(um->um_ubanum, (int *)&bp->b_ubinfo);
1103 		/*
1104 		 * Unlink buffer from I/O wait queue.
1105 		 */
1106 		bp->av_back->av_forw = bp->av_forw;
1107 		bp->av_forw->av_back = bp->av_back;
1108 #if defined(VAX750)
1109 		if (cpu == VAX_750 && um->um_tab.b_active == 0
1110 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
1111 			if (um->um_ubinfo == 0)
1112 				printf("udintr: um_ubinfo == 0\n");
1113 			else
1114 				ubarelse(um->um_ubanum, &um->um_ubinfo);
1115 		}
1116 #endif
1117 		dp = &udutab[unit];
1118 		dp->b_qsize--;
1119 		if (ui->ui_dk >= 0)
1120 			if (dp->b_qsize == 0)
1121 				dk_busy &= ~(1<<ui->ui_dk);
1122 		if (st == M_ST_OFFLN || st == M_ST_AVLBL) {
1123 			ui->ui_flags = 0;       /* mark unit offline */
1124 			/*
1125 			 * Link the buffer onto the front of the drive queue
1126 			 */
1127 			if ((bp->av_forw = dp->b_actf) == 0)
1128 				dp->b_actl = bp;
1129 			dp->b_actf = bp;
1130 			/*
1131 			 * Link the drive onto the controller queue
1132 			 */
1133 			if (dp->b_active == 0) {
1134 				dp->b_forw = NULL;
1135 				if (um->um_tab.b_actf == NULL)
1136 					um->um_tab.b_actf = dp;
1137 				else
1138 					um->um_tab.b_actl->b_forw = dp;
1139 				um->um_tab.b_actl = dp;
1140 				dp->b_active = 1;
1141 			}
1142 #if defined(VAX750)
1143 			if (cpu == VAX750 && um->um_ubinfo == 0)
1144 				um->um_ubinfo =
1145 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
1146 					UBA_NEEDBDP);
1147 #endif
1148 			return;
1149 		}
1150 		if (st != M_ST_SUCC) {
1151 			harderr(bp, "ra");
1152 #ifdef	DEBUG
1153 			printd("status %o\n", mp->mscp_status);
1154 #endif
1155 			bp->b_flags |= B_ERROR;
1156 		}
1157 		bp->b_resid = bp->b_bcount - mp->mscp_bytecnt;
1158 		iodone(bp);
1159 		break;
1160 
1161 	case M_OP_GTUNT|M_OP_END:
1162 #ifdef	DEBUG
1163 		printd("GTUNT end packet status = 0x%x media id 0x%x\n"
1164 			,st,mp->mscp_mediaid);
1165 #endif
1166 		ra_info[unit].rastatus = st;
1167 		ra_info[unit].ratype = mp->mscp_mediaid;
1168 		break;
1169 
1170 	default:
1171 		printf("uda: unknown packet\n");
1172 		uderror(um, (struct mslg *)mp);
1173 	}
1174 }
1175 
1176 
1177 /*
1178  * Process an error log message
1179  *
1180  * For now, just log the error on the console.
1181  * Only minimal decoding is done, only "useful"
1182  * information is printed.  Eventually should
1183  * send message to an error logger.
1184  */
1185 uderror(um, mp)
1186 	register struct uba_ctlr *um;
1187 	register struct mslg *mp;
1188 {
1189 	register	i;
1190 
1191 
1192 	if(!(mp->mslg_flags & (M_LF_SUCC | M_LF_CONT)))
1193 		printf("uda%d: hard error\n");
1194 
1195 	mprintf("uda%d: %s error, ", um->um_ctlr,
1196 		mp->mslg_flags & ( M_LF_SUCC | M_LF_CONT ) ? "soft" : "hard");
1197 	switch (mp->mslg_format) {
1198 	case M_FM_CNTERR:
1199 		mprintf("controller error, event 0%o\n", mp->mslg_event);
1200 		break;
1201 
1202 	case M_FM_BUSADDR:
1203 		mprintf("host memory access error, event 0%o, addr 0%o\n",
1204 			mp->mslg_event, mp->mslg_busaddr);
1205 		break;
1206 
1207 	case M_FM_DISKTRN:
1208 		mprintf("disk transfer error, unit %d, grp 0x%x, hdr 0x%x, event 0%o\n",
1209 			mp->mslg_unit, mp->mslg_group, mp->mslg_hdr,
1210 mp->mslg_event);
1211 		break;
1212 
1213 	case M_FM_SDI:
1214 		mprintf("SDI error, unit %d, event 0%o, hdr 0x%x\n",
1215 			mp->mslg_unit, mp->mslg_event, mp->mslg_hdr);
1216 		for(i = 0; i < 12;i++)
1217 			mprintf("\t0x%x",mp->mslg_sdistat[i] & 0xff);
1218 		mprintf("\n");
1219 		break;
1220 
1221 	case M_FM_SMLDSK:
1222 		mprintf("small disk error, unit %d, event 0%o, cyl %d\n",
1223 			mp->mslg_unit, mp->mslg_event, mp->mslg_sdecyl);
1224 		break;
1225 
1226 	default:
1227 		mprintf("unknown error, unit %d, format 0%o, event 0%o\n",
1228 			mp->mslg_unit, mp->mslg_format, mp->mslg_event);
1229 	}
1230 
1231 	if (udaerror) {
1232 		register long *p = (long *)mp;
1233 
1234 		for (i = 0; i < mp->mslg_header.uda_msglen; i += sizeof(*p))
1235 			printf("%x ", *p++);
1236 		printf("\n");
1237 	}
1238 }
1239 
1240 
1241 /*
1242  * Find an unused command packet
1243  */
1244 struct mscp *
1245 udgetcp(um)
1246 	struct uba_ctlr *um;
1247 {
1248 	register struct mscp *mp;
1249 	register struct udaca *cp;
1250 	register struct uda_softc *sc;
1251 	register int i;
1252 	int	s;
1253 
1254 	s = spl5();
1255 	cp = &uda[um->um_ctlr].uda_ca;
1256 	sc = &uda_softc[um->um_ctlr];
1257 	/*
1258 	 * If no credits, can't issue any commands
1259 	 * until some outstanding commands complete.
1260 	 */
1261 	i = sc->sc_lastcmd;
1262 	if(((cp->ca_cmddsc[i]&(UDA_OWN|UDA_INT))==UDA_INT)&&
1263 	    (sc->sc_credits >= 2)) {
1264 		sc->sc_credits--;       /* committed to issuing a command */
1265 		cp->ca_cmddsc[i] &= ~UDA_INT;
1266 		mp = &uda[um->um_ctlr].uda_cmd[i];
1267 		mp->mscp_unit = mp->mscp_modifier = 0;
1268 		mp->mscp_opcode = mp->mscp_flags = 0;
1269 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
1270 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
1271 		sc->sc_lastcmd = (i + 1) % NCMD;
1272 		(void) splx(s);
1273 		return(mp);
1274 	}
1275 	(void) splx(s);
1276 	return(NULL);
1277 }
1278 
1279 udread(dev, uio)
1280 	dev_t dev;
1281 	struct uio *uio;
1282 {
1283 	register int unit = udunit(dev);
1284 
1285 	if (unit >= NRA)
1286 		return (ENXIO);
1287 	return (physio(udstrategy, &rudbuf[unit], dev, B_READ, minphys, uio));
1288 }
1289 
1290 udwrite(dev, uio)
1291 	dev_t dev;
1292 	struct uio *uio;
1293 {
1294 	register int unit = udunit(dev);
1295 
1296 	if (unit >= NRA)
1297 		return (ENXIO);
1298 	return (physio(udstrategy, &rudbuf[unit], dev, B_WRITE, minphys, uio));
1299 }
1300 
1301 udreset(uban)
1302 	int uban;
1303 {
1304 	register struct uba_ctlr *um;
1305 	register struct uba_device *ui;
1306 	register struct buf *bp, *dp;
1307 	register int unit;
1308 	struct buf *nbp;
1309 	int d;
1310 
1311 	for (d = 0; d < NUDA; d++) {
1312 		if ((um = udminfo[d]) == 0 || um->um_ubanum != uban ||
1313 		    um->um_alive == 0)
1314 			continue;
1315 		printf(" uda%d", d);
1316 		um->um_tab.b_active = 0;
1317 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
1318 		uda_softc[d].sc_state = S_IDLE;
1319 		uda_softc[d].sc_mapped = 0;	/* Rich */
1320 		for (unit = 0; unit < NRA; unit++) {
1321 			if ((ui = uddinfo[unit]) == 0)
1322 				continue;
1323 			if (ui->ui_alive == 0 || ui->ui_mi != um)
1324 				continue;
1325 			udutab[unit].b_active = 0;
1326 			udutab[unit].b_qsize = 0;
1327 		}
1328 		for (bp = udwtab[d].av_forw; bp != &udwtab[d]; bp = nbp) {
1329 			nbp = bp->av_forw;
1330 			bp->b_ubinfo = 0;
1331 			/*
1332 			 * Link the buffer onto the drive queue
1333 			 */
1334 			dp = &udutab[udunit(bp->b_dev)];
1335 			if (dp->b_actf == 0)
1336 				dp->b_actf = bp;
1337 			else
1338 				dp->b_actl->av_forw = bp;
1339 			dp->b_actl = bp;
1340 			bp->av_forw = 0;
1341 			/*
1342 			 * Link the drive onto the controller queue
1343 			 */
1344 			if (dp->b_active == 0) {
1345 				dp->b_forw = NULL;
1346 				if (um->um_tab.b_actf == NULL)
1347 					um->um_tab.b_actf = dp;
1348 				else
1349 					um->um_tab.b_actl->b_forw = dp;
1350 				um->um_tab.b_actl = dp;
1351 				dp->b_active = 1;
1352 			}
1353 		}
1354 		(void)udinit(d);
1355 	}
1356 }
1357 
1358 #define DBSIZE 32
1359 
1360 #define ca_Rspdsc       ca_rspdsc[0]
1361 #define ca_Cmddsc       ca_rspdsc[1]
1362 #define uda_Rsp         uda_rsp[0]
1363 #define uda_Cmd         uda_cmd[0]
1364 
1365 struct  uda     udad[NUDA];
1366 
1367 uddump(dev)
1368 	dev_t dev;
1369 {
1370 	struct udadevice *udaddr;
1371 	struct uda *ud_ubaddr;
1372 	char *start;
1373 	int num, blk, unit;
1374 	int maxsz;
1375 	int blkoff;
1376 	register struct uba_regs *uba;
1377 	register struct uba_device *ui;
1378 	register struct uda *udp;
1379 	register struct pte *io;
1380 	register int i;
1381 	struct disklabel *lp;
1382 	unit = udunit(dev);
1383 	if (unit >= NRA)
1384 		return (ENXIO);
1385 #define phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
1386 	ui = phys(struct uba_device *, uddinfo[unit]);
1387 	if (ui->ui_alive == 0)
1388 		return (ENXIO);
1389 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
1390 	ubainit(uba);
1391 	udaddr = (struct udadevice *)ui->ui_physaddr;
1392 	DELAY(2000000);
1393 	udp = phys(struct uda *, &udad[ui->ui_ctlr]);
1394 	lp = &udlabel[unit];
1395 
1396 	num = btoc(sizeof(struct uda)) + 1;
1397 	io = &uba->uba_map[NUBMREG-num];
1398 	for(i = 0; i<num; i++)
1399 		*(int *)io++ = UBAMR_MRV|(btop(udp)+i);
1400 	ud_ubaddr = (struct uda *)(((int)udp & PGOFSET)|((NUBMREG-num)<<9));
1401 
1402 	udaddr->udaip = 0;
1403 	while ((udaddr->udasa & UDA_STEP1) == 0)
1404 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1405 	udaddr->udasa = UDA_ERR;
1406 	while ((udaddr->udasa & UDA_STEP2) == 0)
1407 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1408 	udaddr->udasa = (short)&ud_ubaddr->uda_ca.ca_ringbase;
1409 	while ((udaddr->udasa & UDA_STEP3) == 0)
1410 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1411 	udaddr->udasa = (short)(((int)&ud_ubaddr->uda_ca.ca_ringbase) >> 16);
1412 	while ((udaddr->udasa & UDA_STEP4) == 0)
1413 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1414 	udaddr->udasa = UDA_GO;
1415 	udp->uda_ca.ca_Rspdsc = (long)&ud_ubaddr->uda_Rsp.mscp_cmdref;
1416 	udp->uda_ca.ca_Cmddsc = (long)&ud_ubaddr->uda_Cmd.mscp_cmdref;
1417 	udp->uda_Cmd.mscp_cntflgs = 0;
1418 	udp->uda_Cmd.mscp_version = 0;
1419 	if (udcmd(M_OP_STCON, udp, udaddr) == 0) {
1420 		return(EFAULT);
1421 	}
1422 	udp->uda_Cmd.mscp_unit = ui->ui_slave;
1423 	if (udcmd(M_OP_ONLIN, udp, udaddr) == 0) {
1424 		return(EFAULT);
1425 	}
1426 
1427 	num = maxfree;
1428 	start = 0;
1429 	blkoff = lp->d_partitions[udpart(dev)].p_offset;
1430 	maxsz = lp->d_partitions[udpart(dev)].p_size;
1431 	if (dumplo < 0)
1432 		return (EINVAL);
1433 	if (dumplo + num >= maxsz)
1434 		num = maxsz - dumplo;
1435 	blkoff += dumplo;
1436 	while (num > 0) {
1437 		blk = num > DBSIZE ? DBSIZE : num;
1438 		io = uba->uba_map;
1439 		for (i = 0; i < blk; i++)
1440 			*(int *)io++ = (btop(start)+i) | UBAMR_MRV;
1441 		*(int *)io = 0;
1442 		udp->uda_Cmd.mscp_lbn = btop(start) + blkoff;
1443 		udp->uda_Cmd.mscp_unit = ui->ui_slave;
1444 		udp->uda_Cmd.mscp_bytecnt = blk*NBPG;
1445 		udp->uda_Cmd.mscp_buffer = 0;
1446 		if (udcmd(M_OP_WRITE, udp, udaddr) == 0) {
1447 			return(EIO);
1448 		}
1449 		start += blk*NBPG;
1450 		num -= blk;
1451 	}
1452 	return (0);
1453 }
1454 
1455 
1456 udcmd(op, udp, udaddr)
1457 	int op;
1458 	register struct uda *udp;
1459 	struct udadevice *udaddr;
1460 {
1461 	int i;
1462 
1463 	udp->uda_Cmd.mscp_opcode = op;
1464 	udp->uda_Rsp.mscp_header.uda_msglen = mscp_msglen;
1465 	udp->uda_Cmd.mscp_header.uda_msglen = mscp_msglen;
1466 	udp->uda_ca.ca_Rspdsc |= UDA_OWN|UDA_INT;
1467 	udp->uda_ca.ca_Cmddsc |= UDA_OWN|UDA_INT;
1468 	if (udaddr->udasa&UDA_ERR)
1469 		printf("Udaerror udasa (%x)\n", udaddr->udasa&0xffff);
1470 	i = udaddr->udaip;
1471 #ifdef	lint
1472 	i = i;
1473 #endif
1474 	for (;;) {
1475 		if (udp->uda_ca.ca_cmdint)
1476 			udp->uda_ca.ca_cmdint = 0;
1477 		if (udp->uda_ca.ca_rspint)
1478 			break;
1479 	}
1480 	udp->uda_ca.ca_rspint = 0;
1481 	if (udp->uda_Rsp.mscp_opcode != (op|M_OP_END) ||
1482 	    (udp->uda_Rsp.mscp_status&M_ST_MASK) != M_ST_SUCC) {
1483 		printf("error: com %d opc 0x%x stat 0x%x\ndump ",
1484 			op,
1485 			udp->uda_Rsp.mscp_opcode,
1486 			udp->uda_Rsp.mscp_status);
1487 		return(0);
1488 	}
1489 	return(1);
1490 }
1491 
1492 udioctl(dev, cmd, data, flag)
1493 	dev_t dev;
1494 	int cmd;
1495 	caddr_t data;
1496 	int flag;
1497 {
1498 	int unit = udunit(dev);
1499 	register struct disklabel *lp;
1500 	int error = 0;
1501 
1502 	lp = &udlabel[unit];
1503 
1504 	switch (cmd) {
1505 
1506 	case DIOCGDINFO:
1507 		*(struct disklabel *)data = *lp;
1508 		break;
1509 
1510 	case DIOCGPART:
1511 		((struct partinfo *)data)->disklab = lp;
1512 		((struct partinfo *)data)->part =
1513 		    &lp->d_partitions[udpart(dev)];
1514 		break;
1515 
1516 	case DIOCSDINFO:
1517 		if ((flag & FWRITE) == 0)
1518 			error = EBADF;
1519 		else
1520 			*lp = *(struct disklabel *)data;
1521 		break;
1522 
1523 	case DIOCWDINFO:
1524 		if ((flag & FWRITE) == 0) {
1525 			error = EBADF;
1526 			break;
1527 		}
1528 		{
1529 		struct buf *bp;
1530 		struct disklabel *dlp;
1531 		daddr_t alt, end;
1532 
1533 		*lp = *(struct disklabel *)data;
1534 		bp = geteblk(lp->d_secsize);
1535 		bp->b_dev = makedev(major(dev), udminor(udunit(dev), 0));
1536 		bp->b_bcount = lp->d_secsize;
1537 		bp->b_blkno = LABELSECTOR;
1538 		bp->b_flags = B_READ;
1539 		dlp = (struct disklabel *)(bp->b_un.b_addr + LABELOFFSET);
1540 		udstrategy(bp);
1541 		biowait(bp);
1542 		if (bp->b_flags & B_ERROR) {
1543 			error = u.u_error;		/* XXX */
1544 			u.u_error = 0;
1545 			goto bad;
1546 		}
1547 		*dlp = *lp;
1548 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_ntracks + 1;
1549 		end = alt + 8;
1550 		for (;;) {
1551 			bp->b_flags = B_WRITE;
1552 			udstrategy(bp);
1553 			biowait(bp);
1554 			if (bp->b_flags & B_ERROR) {
1555 				error = u.u_error;	/* XXX */
1556 				u.u_error = 0;
1557 			}
1558 			if (bp->b_blkno >= end)
1559 				break;
1560 			bp->b_blkno = alt;
1561 			alt += 2;
1562 		}
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