xref: /csrg-svn/sys/vax/uba/uda.c (revision 30916)
1 /*
2  *	@(#)uda.c	7.4 (Berkeley) 04/17/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 #endif
560 		ra->rastate = OPENRAW;
561 	} else
562 		ra->rastate = OPEN;
563 	wakeup((caddr_t)ra);
564 	return (0);
565 }
566 
567 udstrategy(bp)
568 	register struct buf *bp;
569 {
570 	register struct uba_device *ui;
571 	register struct uba_ctlr *um;
572 	register struct buf *dp;
573 	register struct disklabel *lp;
574 	register int unit;
575 	struct uda_softc *sc;
576 	int xunit = udpart(bp->b_dev);
577 	daddr_t sz, maxsz;
578 	int s;
579 
580 	unit = udunit(bp->b_dev);
581 	if (unit >= NRA) {
582 		bp->b_error = ENXIO;
583 		goto bad;
584 	}
585 	ui = uddinfo[unit];
586 	lp = &udlabel[unit];
587 	sc = &uda_softc[ui->ui_ctlr];
588 	um = ui->ui_mi;
589 	if (ui == 0 || ui->ui_alive == 0 || ra_info[unit].rastate == CLOSED) {
590 		bp->b_error = ENXIO;
591 		goto bad;
592 	}
593 	if (ra_info[unit].rastate < OPEN)
594 		goto q;
595 	if ((ra_info[unit].openpart & (1 << xunit)) == 0) {
596 		bp->b_error = ENODEV;
597 		goto bad;
598 	}
599 	maxsz = lp->d_partitions[xunit].p_size;
600 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
601 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
602 		if (bp->b_blkno == maxsz) {
603 			bp->b_resid = bp->b_bcount;
604 			goto done;
605 		}
606 		sz = maxsz - bp->b_blkno;
607 		if (sz <= 0) {
608 			bp->b_error = EINVAL;
609 			goto bad;
610 		}
611 		bp->b_bcount = sz << DEV_BSHIFT;
612 	}
613 q:
614 	s = spl5();
615 	/*
616 	 * Link the buffer onto the drive queue
617 	 */
618 	dp = &udutab[ui->ui_unit];
619 	if (dp->b_actf == 0)
620 		dp->b_actf = bp;
621 	else
622 		dp->b_actl->av_forw = bp;
623 	dp->b_actl = bp;
624 	bp->av_forw = 0;
625 	/*
626 	 * Link the drive onto the controller queue
627 	 */
628 	if (dp->b_active == 0) {
629 		dp->b_forw = NULL;
630 		if (um->um_tab.b_actf == NULL)
631 			um->um_tab.b_actf = dp;
632 		else
633 			um->um_tab.b_actl->b_forw = dp;
634 		um->um_tab.b_actl = dp;
635 		dp->b_active = 1;
636 	}
637 	if (um->um_tab.b_active == 0) {
638 #if defined(VAX750)
639 		if (cpu == VAX_750
640 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
641 			if (um->um_ubinfo != 0) {
642 				printd("udastrat: ubinfo 0x%x\n",um->um_ubinfo);
643 			} else
644 				um->um_ubinfo =
645 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
646 					UBA_NEEDBDP);
647 		}
648 #endif
649 		(void) udstart(um);
650 	}
651 	splx(s);
652 	return;
653 
654 bad:
655 	bp->b_flags |= B_ERROR;
656 done:
657 	iodone(bp);
658 	return;
659 }
660 
661 udstart(um)
662 	register struct uba_ctlr *um;
663 {
664 	register struct buf *bp, *dp;
665 	register struct mscp *mp;
666 	register struct uda_softc *sc;
667 	register struct uba_device *ui;
668 	struct disklabel *lp;
669 	struct udadevice *udaddr;
670 	struct uda *ud = &uda[um->um_ctlr];
671 	daddr_t sz;
672 	int i;
673 
674 	sc = &uda_softc[um->um_ctlr];
675 
676 loop:
677 	if ((dp = um->um_tab.b_actf) == NULL) {
678 
679 		um->um_tab.b_active = 0;
680 		/* Check for response ring transitions lost in the
681 		 * Race condition
682 		 */
683 		for (i = sc->sc_lastrsp;; i++) {
684 			i %= NRSP;
685 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
686 				break;
687 			udrsp(um, ud, sc, i);
688 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
689 		}
690 		sc->sc_lastrsp = i;
691 		return (0);
692 	}
693 	if ((bp = dp->b_actf) == NULL) {
694 		/*
695 		 * No more requests for this drive, remove
696 		 * from controller queue and look at next drive.
697 		 * We know we're at the head of the controller queue.
698 		 */
699 		dp->b_active = 0;
700 		um->um_tab.b_actf = dp->b_forw;
701 		if (ra_info[dp - udutab].openpart == 0)
702 			wakeup((caddr_t)dp);
703 		goto loop;		/* Need to check for loop */
704 	}
705 	um->um_tab.b_active++;
706 	udaddr = (struct udadevice *)um->um_addr;
707 	if ((udaddr->udasa&UDA_ERR) || sc->sc_state != S_RUN) {
708 		harderr(bp, "ra");
709 		mprintf("Uda%d udasa %o, state %d\n",um->um_ctlr , udaddr->udasa&0xffff, sc->sc_state);
710 		(void)udinit(um->um_ctlr);
711 		/* SHOULD REQUEUE OUTSTANDING REQUESTS, LIKE UDRESET */
712 		return (0);
713 	}
714 	ui = uddinfo[udunit(bp->b_dev)];
715 	lp = &udlabel[ui->ui_unit];
716 	if (ui->ui_flags == 0) {        /* not online */
717 		if ((mp = udgetcp(um)) == NULL){
718 			return (0);
719 		}
720 		mp->mscp_opcode = M_OP_ONLIN;
721 		mp->mscp_unit = ui->ui_slave;
722 		dp->b_active = 2;
723 		um->um_tab.b_actf = dp->b_forw; /* remove from controller q */
724 #ifdef	DEBUG
725 		printd("uda: bring unit %d online\n", ui->ui_slave);
726 #endif
727 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
728 		if (udaddr->udasa&UDA_ERR)
729 			printf("Uda (%d) Error (%x)\n",um->um_ctlr , udaddr->udasa&0xffff);
730 		i = udaddr->udaip;
731 		goto loop;
732 	}
733 	switch (cpu) {
734 	case VAX_8600:
735 	case VAX_780:
736 		i = UBA_NEEDBDP|UBA_CANTWAIT;
737 		break;
738 
739 	case VAX_750:
740 		i = um->um_ubinfo|UBA_HAVEBDP|UBA_CANTWAIT;
741 		break;
742 
743 	case VAX_730:
744 	case VAX_630:
745 		i = UBA_CANTWAIT;
746 		break;
747 	}
748 	if ((i = ubasetup(um->um_ubanum, bp, i)) == 0)
749 		return(1);
750 	if ((mp = udgetcp(um)) == NULL) {
751 #if defined(VAX750)
752 		if (cpu == VAX_750)
753 			i &= 0xfffffff;         /* mask off bdp */
754 #endif
755 		ubarelse(um->um_ubanum,&i);
756 		return(0);
757 	}
758 	mp->mscp_cmdref = (long)bp;     /* pointer to get back */
759 	mp->mscp_opcode = bp->b_flags&B_READ ? M_OP_READ : M_OP_WRITE;
760 	mp->mscp_unit = ui->ui_slave;
761 	mp->mscp_buffer = (i & 0x3ffff) | (((i>>28)&0xf)<<24);
762 #if defined(VAX750)
763 	if (cpu == VAX_750)
764 		i &= 0xfffffff;         /* mask off bdp */
765 #endif
766 	bp->b_ubinfo = i;               /* save mapping info */
767 	i = udpart(bp->b_dev);
768 	mp->mscp_lbn = bp->b_blkno +
769 	    lp->d_partitions[i].p_offset;
770 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
771 	if (bp->b_blkno + sz > lp->d_partitions[i].p_size)
772 		mp->mscp_bytecnt = (lp->d_partitions[i].p_size - bp->b_blkno) >>
773 		    DEV_BSHIFT;
774 	else
775 		mp->mscp_bytecnt = bp->b_bcount;
776 	*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
777 	if (udaddr->udasa&UDA_ERR)
778 		printf("Uda(%d) udasa (%x)\n",um->um_ctlr , udaddr->udasa&0xffff);
779 	i = udaddr->udaip;              /* initiate polling */
780 	dp->b_qsize++;
781 	if (ui->ui_dk >= 0) {
782 		dk_busy |= 1<<ui->ui_dk;
783 		dk_xfer[ui->ui_dk]++;
784 		dk_wds[ui->ui_dk] += bp->b_bcount>>6;
785 	}
786 
787 	/*
788 	 * Move drive to the end of the controller queue
789 	 */
790 	if (dp->b_forw != NULL) {
791 		um->um_tab.b_actf = dp->b_forw;
792 		um->um_tab.b_actl->b_forw = dp;
793 		um->um_tab.b_actl = dp;
794 		dp->b_forw = NULL;
795 	}
796 	/*
797 	 * Move buffer to I/O wait queue
798 	 */
799 	dp->b_actf = bp->av_forw;
800 	dp = &udwtab[um->um_ctlr];
801 	bp->av_forw = dp;
802 	bp->av_back = dp->av_back;
803 	dp->av_back->av_forw = bp;
804 	dp->av_back = bp;
805 	goto loop;
806 }
807 
808 /*
809  * UDA interrupt routine.
810  */
811 udintr(d)
812 	register d;
813 {
814 	struct uba_ctlr *um = udminfo[d];
815 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
816 	struct buf *bp;
817 	register int i;
818 	register struct uda_softc *sc = &uda_softc[d];
819 	register struct uda *ud = &uda[d];
820 	struct uda *uud;
821 	register struct mscp *mp;
822 
823 #ifdef	DEBUG
824 	printd10("udintr: state %d, udasa %o\n", sc->sc_state, udaddr->udasa);
825 #endif
826 #ifdef VAX630
827 	(void) spl5();
828 #endif
829 	switch (sc->sc_state) {
830 	case S_IDLE:
831 		printf("uda%d: random interrupt ignored\n", d);
832 		return;
833 
834 	case S_STEP1:
835 #define STEP1MASK       0174377
836 #define STEP1GOOD       (UDA_STEP2|UDA_IE|(NCMDL2<<3)|NRSPL2)
837 		if ((udaddr->udasa&STEP1MASK) != STEP1GOOD) {
838 			sc->sc_state = S_IDLE;
839 			wakeup((caddr_t)um);
840 			return;
841 		}
842 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)|
843 		    ((cpu == VAX_780) || (cpu == VAX_8600) ? UDA_PI : 0);
844 		sc->sc_state = S_STEP2;
845 		return;
846 
847 	case S_STEP2:
848 #define STEP2MASK       0174377
849 #define STEP2GOOD       (UDA_STEP3|UDA_IE|(sc->sc_ivec/4))
850 		if ((udaddr->udasa&STEP2MASK) != STEP2GOOD) {
851 			sc->sc_state = S_IDLE;
852 			wakeup((caddr_t)um);
853 			return;
854 		}
855 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)>>16;
856 		sc->sc_state = S_STEP3;
857 		return;
858 
859 	case S_STEP3:
860 #define STEP3MASK       0174000
861 #define STEP3GOOD       UDA_STEP4
862 		if ((udaddr->udasa&STEP3MASK) != STEP3GOOD) {
863 			sc->sc_state = S_IDLE;
864 			wakeup((caddr_t)um);
865 			return;
866 		}
867 		udamicro[d] = udaddr->udasa;
868 		log(LOG_INFO, "uda%d: version %d model %d\n", d,
869 		    udamicro[d] & 0xf, (udamicro[d] >> 4) & 0xf);
870 		/*
871 		 * Requesting the error status (|= 2)
872 		 * may hang older controllers.
873 		 */
874 		i = UDA_GO | (udaerror? 2 : 0);
875 		if (udaburst[d])
876 			i |= (udaburst[d] - 1) << 2;
877 		udaddr->udasa = i;
878 		udaddr->udasa = UDA_GO;
879 		sc->sc_state = S_SCHAR;
880 
881 		/*
882 		 * Initialize the data structures.
883 		 */
884 		uud = sc->sc_uda;
885 		for (i = 0; i < NRSP; i++) {
886 			ud->uda_ca.ca_rspdsc[i] = UDA_OWN|UDA_INT|
887 				(long)&uud->uda_rsp[i].mscp_cmdref;
888 			ud->uda_rsp[i].mscp_dscptr = &ud->uda_ca.ca_rspdsc[i];
889 			ud->uda_rsp[i].mscp_header.uda_msglen = mscp_msglen;
890 		}
891 		for (i = 0; i < NCMD; i++) {
892 			ud->uda_ca.ca_cmddsc[i] = UDA_INT|
893 				(long)&uud->uda_cmd[i].mscp_cmdref;
894 			ud->uda_cmd[i].mscp_dscptr = &ud->uda_ca.ca_cmddsc[i];
895 			ud->uda_cmd[i].mscp_header.uda_msglen = mscp_msglen;
896 		}
897 		bp = &udwtab[d];
898 		bp->av_forw = bp->av_back = bp;
899 		sc->sc_lastcmd = 1;
900 		sc->sc_lastrsp = 0;
901 		mp = &uda[um->um_ctlr].uda_cmd[0];
902 		mp->mscp_unit = mp->mscp_modifier = 0;
903 		mp->mscp_flags = 0;
904 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
905 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
906 		mp->mscp_opcode = M_OP_STCON;
907 		mp->mscp_cntflgs = M_CF_ATTN|M_CF_MISC|M_CF_THIS;
908 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
909 		i = udaddr->udaip;      /* initiate polling */
910 		return;
911 
912 	case S_SCHAR:
913 	case S_RUN:
914 		break;
915 
916 	default:
917 		printf("uda%d: interrupt in unknown state %d ignored\n",
918 			d, sc->sc_state);
919 		return;
920 	}
921 
922 	if (udaddr->udasa&UDA_ERR) {
923 		printf("uda(%d): fatal error (%o)\n", d, udaddr->udasa&0xffff);
924 		udaddr->udaip = 0;
925 		wakeup((caddr_t)um);
926 	}
927 
928 	/*
929 	 * Check for a buffer purge request.
930 	 */
931 	if (ud->uda_ca.ca_bdp) {
932 #ifdef	DEBUG
933 		printd("uda: purge bdp %d\n", ud->uda_ca.ca_bdp);
934 #endif
935 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
936 		ud->uda_ca.ca_bdp = 0;
937 		udaddr->udasa = 0;      /* signal purge complete */
938 	}
939 
940 	/*
941 	 * Check for response ring transition.
942 	 */
943 	if (ud->uda_ca.ca_rspint) {
944 		ud->uda_ca.ca_rspint = 0;
945 		for (i = sc->sc_lastrsp;; i++) {
946 			i %= NRSP;
947 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
948 				break;
949 			udrsp(um, ud, sc, i);
950 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
951 		}
952 		sc->sc_lastrsp = i;
953 	}
954 
955 	/*
956 	 * Check for command ring transition.
957 	 */
958 	if (ud->uda_ca.ca_cmdint) {
959 #ifdef	DEBUG
960 		printd("uda: command ring transition\n");
961 #endif
962 		ud->uda_ca.ca_cmdint = 0;
963 	}
964 	if(uda_cp_wait)
965 		wakeup((caddr_t)&uda_cp_wait);
966 	(void) udstart(um);
967 }
968 
969 /*
970  * Process a response packet
971  */
972 udrsp(um, ud, sc, i)
973 	register struct uba_ctlr *um;
974 	register struct uda *ud;
975 	register struct uda_softc *sc;
976 	int i;
977 {
978 	register struct mscp *mp;
979 	register struct uba_device *ui;
980 	register int unit;
981 	struct buf *dp, *bp, nullbp;
982 	int st;
983 
984 	mp = &ud->uda_rsp[i];
985 	mp->mscp_header.uda_msglen = mscp_msglen;
986 	sc->sc_credits += mp->mscp_header.uda_credits & 0xf;  /* just 4 bits?*/
987 	if ((mp->mscp_header.uda_credits & 0xf0) > 0x10)	/* Check */
988 		return;
989 #ifdef	DEBUG
990 	printd10("udarsp, opcode 0x%x status 0x%x\n",mp->mscp_opcode,mp->mscp_status);
991 #endif
992 	/*
993 	 * If it's an error log message (datagram),
994 	 * pass it on for more extensive processing.
995 	 */
996 	if ((mp->mscp_header.uda_credits & 0xf0) == 0x10) {	/* check */
997 		uderror(um, (struct mslg *)mp);
998 		return;
999 	}
1000 	st = mp->mscp_status&M_ST_MASK;
1001 	/* The controller interrupts as drive 0 */
1002 	/* this means that you must check for controller interrupts */
1003 	/* before you check to see if there is a drive 0 */
1004 	if((M_OP_STCON|M_OP_END) == mp->mscp_opcode){
1005 		if (st == M_ST_SUCC)
1006 			sc->sc_state = S_RUN;
1007 		else
1008 			sc->sc_state = S_IDLE;
1009 		um->um_tab.b_active = 0;
1010 		wakeup((caddr_t)um);
1011 		return;
1012 	}
1013 	if (mp->mscp_unit >= 8)
1014 		return;
1015 	if ((ui = udip[um->um_ctlr][mp->mscp_unit]) == 0)
1016 		return;
1017 	unit = ui->ui_unit;
1018 	switch (mp->mscp_opcode) {
1019 
1020 	case M_OP_ONLIN|M_OP_END:
1021 		ra_info[unit].rastatus = st;
1022 		ra_info[unit].ratype =  mp->mscp_mediaid;
1023 		dp = &udutab[unit];
1024 		if (st == M_ST_SUCC) {
1025 			/*
1026 			 * Link the drive onto the controller queue
1027 			 */
1028 			dp->b_forw = NULL;
1029 			if (um->um_tab.b_actf == NULL)
1030 				um->um_tab.b_actf = dp;
1031 			else
1032 				um->um_tab.b_actl->b_forw = dp;
1033 			um->um_tab.b_actl = dp;
1034 			ui->ui_flags = 1;       /* mark it online */
1035 			ra_info[unit].rastate = OPENRAW;
1036 			ra_info[unit].radsize=(daddr_t)mp->mscp_untsize;
1037 #ifdef	DEBUG
1038 			printd("uda: unit %d online\n", mp->mscp_unit);
1039 #endif
1040 #define F_to_C(x,i)     ( ((x)->mscp_mediaid) >> (i*5+7) & 0x1f ? ( ( (((x)->mscp_mediaid) >>( i*5 + 7)) & 0x1f) + 'A' - 1): ' ')
1041 		/* this mess decodes the Media type identifier */
1042 #ifdef	DEBUG
1043 			printd("uda: unit %d online %x %c%c %c%c%c%d\n"
1044 				,mp->mscp_unit, mp->mscp_mediaid
1045 				,F_to_C(mp,4),F_to_C(mp,3),F_to_C(mp,2)
1046 				,F_to_C(mp,1),F_to_C(mp,0)
1047 				,mp->mscp_mediaid & 0x7f);
1048 #endif
1049 			dp->b_active = 1;
1050 		} else {
1051 			if(dp->b_actf){
1052 				harderr(dp->b_actf,"ra");
1053 			} else {
1054 				nullbp.b_blkno = 0;
1055 				nullbp.b_dev = makedev(UDADEVNUM,unit);
1056 				harderr(&nullbp, "ra");
1057 			}
1058 			printf("OFFLINE\n");
1059 			while (bp = dp->b_actf) {
1060 				dp->b_actf = bp->av_forw;
1061 				bp->b_flags |= B_ERROR;
1062 				iodone(bp);
1063 			}
1064 			ra_info[unit].rastate = CLOSED;
1065 		}
1066 		if(mp->mscp_cmdref!=NULL){/* Seems to get lost sometimes */
1067 			wakeup((caddr_t)mp->mscp_cmdref);
1068 		}
1069 		break;
1070 
1071 /*
1072  * The AVAILABLE ATTENTION messages occurs when the
1073  * unit becomes available after spinup,
1074  * marking the unit offline will force an online command
1075  * prior to using the unit.
1076  */
1077 	case M_OP_AVATN:
1078 #ifdef	DEBUG
1079 		printd("uda: unit %d attention\n", mp->mscp_unit);
1080 #endif
1081 		ui->ui_flags = 0;       /* it went offline and we didn't notice */
1082 		ra_info[unit].ratype =  mp->mscp_mediaid;
1083 		break;
1084 
1085 	case M_OP_END:
1086 /*
1087  * An endcode without an opcode (0200) is an invalid command.
1088  * The mscp specification states that this would be a protocol
1089  * type error, such as illegal opcodes. The mscp spec. also
1090  * states that parameter error type of invalid commands should
1091  * return the normal end message for the command. This does not appear
1092  * to be the case. An invalid logical block number returned an endcode
1093  * of 0200 instead of the 0241 (read) that was expected.
1094  */
1095 
1096 		printf("endcd=%o, stat=%o\n", mp->mscp_opcode, mp->mscp_status);
1097 		break;
1098 	case M_OP_READ|M_OP_END:
1099 	case M_OP_WRITE|M_OP_END:
1100 		bp = (struct buf *)mp->mscp_cmdref;
1101 		ubarelse(um->um_ubanum, (int *)&bp->b_ubinfo);
1102 		/*
1103 		 * Unlink buffer from I/O wait queue.
1104 		 */
1105 		bp->av_back->av_forw = bp->av_forw;
1106 		bp->av_forw->av_back = bp->av_back;
1107 #if defined(VAX750)
1108 		if (cpu == VAX_750 && um->um_tab.b_active == 0
1109 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
1110 			if (um->um_ubinfo == 0)
1111 				printf("udintr: um_ubinfo == 0\n");
1112 			else
1113 				ubarelse(um->um_ubanum, &um->um_ubinfo);
1114 		}
1115 #endif
1116 		dp = &udutab[unit];
1117 		dp->b_qsize--;
1118 		if (ui->ui_dk >= 0)
1119 			if (dp->b_qsize == 0)
1120 				dk_busy &= ~(1<<ui->ui_dk);
1121 		if (st == M_ST_OFFLN || st == M_ST_AVLBL) {
1122 			ui->ui_flags = 0;       /* mark unit offline */
1123 			/*
1124 			 * Link the buffer onto the front of the drive queue
1125 			 */
1126 			if ((bp->av_forw = dp->b_actf) == 0)
1127 				dp->b_actl = bp;
1128 			dp->b_actf = bp;
1129 			/*
1130 			 * Link the drive onto the controller queue
1131 			 */
1132 			if (dp->b_active == 0) {
1133 				dp->b_forw = NULL;
1134 				if (um->um_tab.b_actf == NULL)
1135 					um->um_tab.b_actf = dp;
1136 				else
1137 					um->um_tab.b_actl->b_forw = dp;
1138 				um->um_tab.b_actl = dp;
1139 				dp->b_active = 1;
1140 			}
1141 #if defined(VAX750)
1142 			if (cpu == VAX750 && um->um_ubinfo == 0)
1143 				um->um_ubinfo =
1144 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
1145 					UBA_NEEDBDP);
1146 #endif
1147 			return;
1148 		}
1149 		if (st != M_ST_SUCC) {
1150 			harderr(bp, "ra");
1151 #ifdef	DEBUG
1152 			printd("status %o\n", mp->mscp_status);
1153 #endif
1154 			bp->b_flags |= B_ERROR;
1155 		}
1156 		bp->b_resid = bp->b_bcount - mp->mscp_bytecnt;
1157 		iodone(bp);
1158 		break;
1159 
1160 	case M_OP_GTUNT|M_OP_END:
1161 #ifdef	DEBUG
1162 		printd("GTUNT end packet status = 0x%x media id 0x%x\n"
1163 			,st,mp->mscp_mediaid);
1164 #endif
1165 		ra_info[unit].rastatus = st;
1166 		ra_info[unit].ratype = mp->mscp_mediaid;
1167 		break;
1168 
1169 	default:
1170 		printf("uda: unknown packet\n");
1171 		uderror(um, (struct mslg *)mp);
1172 	}
1173 }
1174 
1175 
1176 /*
1177  * Process an error log message
1178  *
1179  * For now, just log the error on the console.
1180  * Only minimal decoding is done, only "useful"
1181  * information is printed.  Eventually should
1182  * send message to an error logger.
1183  */
1184 uderror(um, mp)
1185 	register struct uba_ctlr *um;
1186 	register struct mslg *mp;
1187 {
1188 	register	i;
1189 
1190 
1191 	if(!(mp->mslg_flags & (M_LF_SUCC | M_LF_CONT)))
1192 		printf("uda%d: hard error\n");
1193 
1194 	mprintf("uda%d: %s error, ", um->um_ctlr,
1195 		mp->mslg_flags & ( M_LF_SUCC | M_LF_CONT ) ? "soft" : "hard");
1196 	switch (mp->mslg_format) {
1197 	case M_FM_CNTERR:
1198 		mprintf("controller error, event 0%o\n", mp->mslg_event);
1199 		break;
1200 
1201 	case M_FM_BUSADDR:
1202 		mprintf("host memory access error, event 0%o, addr 0%o\n",
1203 			mp->mslg_event, mp->mslg_busaddr);
1204 		break;
1205 
1206 	case M_FM_DISKTRN:
1207 		mprintf("disk transfer error, unit %d, grp 0x%x, hdr 0x%x, event 0%o\n",
1208 			mp->mslg_unit, mp->mslg_group, mp->mslg_hdr,
1209 mp->mslg_event);
1210 		break;
1211 
1212 	case M_FM_SDI:
1213 		mprintf("SDI error, unit %d, event 0%o, hdr 0x%x\n",
1214 			mp->mslg_unit, mp->mslg_event, mp->mslg_hdr);
1215 		for(i = 0; i < 12;i++)
1216 			mprintf("\t0x%x",mp->mslg_sdistat[i] & 0xff);
1217 		mprintf("\n");
1218 		break;
1219 
1220 	case M_FM_SMLDSK:
1221 		mprintf("small disk error, unit %d, event 0%o, cyl %d\n",
1222 			mp->mslg_unit, mp->mslg_event, mp->mslg_sdecyl);
1223 		break;
1224 
1225 	default:
1226 		mprintf("unknown error, unit %d, format 0%o, event 0%o\n",
1227 			mp->mslg_unit, mp->mslg_format, mp->mslg_event);
1228 	}
1229 
1230 	if (udaerror) {
1231 		register long *p = (long *)mp;
1232 
1233 		for (i = 0; i < mp->mslg_header.uda_msglen; i += sizeof(*p))
1234 			printf("%x ", *p++);
1235 		printf("\n");
1236 	}
1237 }
1238 
1239 
1240 /*
1241  * Find an unused command packet
1242  */
1243 struct mscp *
1244 udgetcp(um)
1245 	struct uba_ctlr *um;
1246 {
1247 	register struct mscp *mp;
1248 	register struct udaca *cp;
1249 	register struct uda_softc *sc;
1250 	register int i;
1251 	int	s;
1252 
1253 	s = spl5();
1254 	cp = &uda[um->um_ctlr].uda_ca;
1255 	sc = &uda_softc[um->um_ctlr];
1256 	/*
1257 	 * If no credits, can't issue any commands
1258 	 * until some outstanding commands complete.
1259 	 */
1260 	i = sc->sc_lastcmd;
1261 	if(((cp->ca_cmddsc[i]&(UDA_OWN|UDA_INT))==UDA_INT)&&
1262 	    (sc->sc_credits >= 2)) {
1263 		sc->sc_credits--;       /* committed to issuing a command */
1264 		cp->ca_cmddsc[i] &= ~UDA_INT;
1265 		mp = &uda[um->um_ctlr].uda_cmd[i];
1266 		mp->mscp_unit = mp->mscp_modifier = 0;
1267 		mp->mscp_opcode = mp->mscp_flags = 0;
1268 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
1269 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
1270 		sc->sc_lastcmd = (i + 1) % NCMD;
1271 		(void) splx(s);
1272 		return(mp);
1273 	}
1274 	(void) splx(s);
1275 	return(NULL);
1276 }
1277 
1278 udread(dev, uio)
1279 	dev_t dev;
1280 	struct uio *uio;
1281 {
1282 	register int unit = udunit(dev);
1283 
1284 	if (unit >= NRA)
1285 		return (ENXIO);
1286 	return (physio(udstrategy, &rudbuf[unit], dev, B_READ, minphys, uio));
1287 }
1288 
1289 udwrite(dev, uio)
1290 	dev_t dev;
1291 	struct uio *uio;
1292 {
1293 	register int unit = udunit(dev);
1294 
1295 	if (unit >= NRA)
1296 		return (ENXIO);
1297 	return (physio(udstrategy, &rudbuf[unit], dev, B_WRITE, minphys, uio));
1298 }
1299 
1300 udreset(uban)
1301 	int uban;
1302 {
1303 	register struct uba_ctlr *um;
1304 	register struct uba_device *ui;
1305 	register struct buf *bp, *dp;
1306 	register int unit;
1307 	struct buf *nbp;
1308 	int d;
1309 
1310 	for (d = 0; d < NUDA; d++) {
1311 		if ((um = udminfo[d]) == 0 || um->um_ubanum != uban ||
1312 		    um->um_alive == 0)
1313 			continue;
1314 		printf(" uda%d", d);
1315 		um->um_tab.b_active = 0;
1316 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
1317 		uda_softc[d].sc_state = S_IDLE;
1318 		uda_softc[d].sc_mapped = 0;	/* Rich */
1319 		for (unit = 0; unit < NRA; unit++) {
1320 			if ((ui = uddinfo[unit]) == 0)
1321 				continue;
1322 			if (ui->ui_alive == 0 || ui->ui_mi != um)
1323 				continue;
1324 			udutab[unit].b_active = 0;
1325 			udutab[unit].b_qsize = 0;
1326 		}
1327 		for (bp = udwtab[d].av_forw; bp != &udwtab[d]; bp = nbp) {
1328 			nbp = bp->av_forw;
1329 			bp->b_ubinfo = 0;
1330 			/*
1331 			 * Link the buffer onto the drive queue
1332 			 */
1333 			dp = &udutab[udunit(bp->b_dev)];
1334 			if (dp->b_actf == 0)
1335 				dp->b_actf = bp;
1336 			else
1337 				dp->b_actl->av_forw = bp;
1338 			dp->b_actl = bp;
1339 			bp->av_forw = 0;
1340 			/*
1341 			 * Link the drive onto the controller queue
1342 			 */
1343 			if (dp->b_active == 0) {
1344 				dp->b_forw = NULL;
1345 				if (um->um_tab.b_actf == NULL)
1346 					um->um_tab.b_actf = dp;
1347 				else
1348 					um->um_tab.b_actl->b_forw = dp;
1349 				um->um_tab.b_actl = dp;
1350 				dp->b_active = 1;
1351 			}
1352 		}
1353 		(void)udinit(d);
1354 	}
1355 }
1356 
1357 #define DBSIZE 32
1358 
1359 #define ca_Rspdsc       ca_rspdsc[0]
1360 #define ca_Cmddsc       ca_rspdsc[1]
1361 #define uda_Rsp         uda_rsp[0]
1362 #define uda_Cmd         uda_cmd[0]
1363 
1364 struct  uda     udad[NUDA];
1365 
1366 uddump(dev)
1367 	dev_t dev;
1368 {
1369 	struct udadevice *udaddr;
1370 	struct uda *ud_ubaddr;
1371 	char *start;
1372 	int num, blk, unit;
1373 	int maxsz;
1374 	int blkoff;
1375 	register struct uba_regs *uba;
1376 	register struct uba_device *ui;
1377 	register struct uda *udp;
1378 	register struct pte *io;
1379 	register int i;
1380 	struct disklabel *lp;
1381 	unit = udunit(dev);
1382 	if (unit >= NRA)
1383 		return (ENXIO);
1384 #define phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
1385 	ui = phys(struct uba_device *, uddinfo[unit]);
1386 	if (ui->ui_alive == 0)
1387 		return (ENXIO);
1388 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
1389 	ubainit(uba);
1390 	udaddr = (struct udadevice *)ui->ui_physaddr;
1391 	DELAY(2000000);
1392 	udp = phys(struct uda *, &udad[ui->ui_ctlr]);
1393 	lp = &udlabel[unit];
1394 
1395 	num = btoc(sizeof(struct uda)) + 1;
1396 	io = &uba->uba_map[NUBMREG-num];
1397 	for(i = 0; i<num; i++)
1398 		*(int *)io++ = UBAMR_MRV|(btop(udp)+i);
1399 	ud_ubaddr = (struct uda *)(((int)udp & PGOFSET)|((NUBMREG-num)<<9));
1400 
1401 	udaddr->udaip = 0;
1402 	while ((udaddr->udasa & UDA_STEP1) == 0)
1403 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1404 	udaddr->udasa = UDA_ERR;
1405 	while ((udaddr->udasa & UDA_STEP2) == 0)
1406 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1407 	udaddr->udasa = (short)&ud_ubaddr->uda_ca.ca_ringbase;
1408 	while ((udaddr->udasa & UDA_STEP3) == 0)
1409 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1410 	udaddr->udasa = (short)(((int)&ud_ubaddr->uda_ca.ca_ringbase) >> 16);
1411 	while ((udaddr->udasa & UDA_STEP4) == 0)
1412 		if(udaddr->udasa & UDA_ERR) return(EFAULT);
1413 	udaddr->udasa = UDA_GO;
1414 	udp->uda_ca.ca_Rspdsc = (long)&ud_ubaddr->uda_Rsp.mscp_cmdref;
1415 	udp->uda_ca.ca_Cmddsc = (long)&ud_ubaddr->uda_Cmd.mscp_cmdref;
1416 	udp->uda_Cmd.mscp_cntflgs = 0;
1417 	udp->uda_Cmd.mscp_version = 0;
1418 	if (udcmd(M_OP_STCON, udp, udaddr) == 0) {
1419 		return(EFAULT);
1420 	}
1421 	udp->uda_Cmd.mscp_unit = ui->ui_slave;
1422 	if (udcmd(M_OP_ONLIN, udp, udaddr) == 0) {
1423 		return(EFAULT);
1424 	}
1425 
1426 	num = maxfree;
1427 	start = 0;
1428 	blkoff = lp->d_partitions[udpart(dev)].p_offset;
1429 	maxsz = lp->d_partitions[udpart(dev)].p_size;
1430 	if (dumplo < 0)
1431 		return (EINVAL);
1432 	if (dumplo + num >= maxsz)
1433 		num = maxsz - dumplo;
1434 	blkoff += dumplo;
1435 	while (num > 0) {
1436 		blk = num > DBSIZE ? DBSIZE : num;
1437 		io = uba->uba_map;
1438 		for (i = 0; i < blk; i++)
1439 			*(int *)io++ = (btop(start)+i) | UBAMR_MRV;
1440 		*(int *)io = 0;
1441 		udp->uda_Cmd.mscp_lbn = btop(start) + blkoff;
1442 		udp->uda_Cmd.mscp_unit = ui->ui_slave;
1443 		udp->uda_Cmd.mscp_bytecnt = blk*NBPG;
1444 		udp->uda_Cmd.mscp_buffer = 0;
1445 		if (udcmd(M_OP_WRITE, udp, udaddr) == 0) {
1446 			return(EIO);
1447 		}
1448 		start += blk*NBPG;
1449 		num -= blk;
1450 	}
1451 	return (0);
1452 }
1453 
1454 
1455 udcmd(op, udp, udaddr)
1456 	int op;
1457 	register struct uda *udp;
1458 	struct udadevice *udaddr;
1459 {
1460 	int i;
1461 
1462 	udp->uda_Cmd.mscp_opcode = op;
1463 	udp->uda_Rsp.mscp_header.uda_msglen = mscp_msglen;
1464 	udp->uda_Cmd.mscp_header.uda_msglen = mscp_msglen;
1465 	udp->uda_ca.ca_Rspdsc |= UDA_OWN|UDA_INT;
1466 	udp->uda_ca.ca_Cmddsc |= UDA_OWN|UDA_INT;
1467 	if (udaddr->udasa&UDA_ERR)
1468 		printf("Udaerror udasa (%x)\n", udaddr->udasa&0xffff);
1469 	i = udaddr->udaip;
1470 #ifdef	lint
1471 	i = i;
1472 #endif
1473 	for (;;) {
1474 		if (udp->uda_ca.ca_cmdint)
1475 			udp->uda_ca.ca_cmdint = 0;
1476 		if (udp->uda_ca.ca_rspint)
1477 			break;
1478 	}
1479 	udp->uda_ca.ca_rspint = 0;
1480 	if (udp->uda_Rsp.mscp_opcode != (op|M_OP_END) ||
1481 	    (udp->uda_Rsp.mscp_status&M_ST_MASK) != M_ST_SUCC) {
1482 		printf("error: com %d opc 0x%x stat 0x%x\ndump ",
1483 			op,
1484 			udp->uda_Rsp.mscp_opcode,
1485 			udp->uda_Rsp.mscp_status);
1486 		return(0);
1487 	}
1488 	return(1);
1489 }
1490 
1491 udioctl(dev, cmd, data, flag)
1492 	dev_t dev;
1493 	int cmd;
1494 	caddr_t data;
1495 	int flag;
1496 {
1497 	int unit = udunit(dev);
1498 	register struct disklabel *lp;
1499 	int error = 0;
1500 
1501 	lp = &udlabel[unit];
1502 
1503 	switch (cmd) {
1504 
1505 	case DIOCGDINFO:
1506 		*(struct disklabel *)data = *lp;
1507 		break;
1508 
1509 	case DIOCGPART:
1510 		((struct partinfo *)data)->disklab = lp;
1511 		((struct partinfo *)data)->part =
1512 		    &lp->d_partitions[udpart(dev)];
1513 		break;
1514 
1515 	case DIOCSDINFO:
1516 		if ((flag & FWRITE) == 0)
1517 			error = EBADF;
1518 		else
1519 			*lp = *(struct disklabel *)data;
1520 		break;
1521 
1522 	case DIOCWDINFO:
1523 		if ((flag & FWRITE) == 0) {
1524 			error = EBADF;
1525 			break;
1526 		}
1527 		{
1528 		struct buf *bp;
1529 		struct disklabel *dlp;
1530 		daddr_t alt, end;
1531 
1532 		*lp = *(struct disklabel *)data;
1533 		bp = geteblk(lp->d_secsize);
1534 		bp->b_dev = makedev(major(dev), udminor(udunit(dev), 0));
1535 		bp->b_bcount = lp->d_secsize;
1536 		bp->b_blkno = LABELSECTOR;
1537 		bp->b_flags = B_READ;
1538 		dlp = (struct disklabel *)(bp->b_un.b_addr + LABELOFFSET);
1539 		udstrategy(bp);
1540 		biowait(bp);
1541 		if (bp->b_flags & B_ERROR) {
1542 			error = u.u_error;		/* XXX */
1543 			u.u_error = 0;
1544 			goto bad;
1545 		}
1546 		*dlp = *lp;
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 bad:
1563 		brelse(bp);
1564 		}
1565 		break;
1566 
1567 	default:
1568 		error = ENOTTY;
1569 		break;
1570 	}
1571 	return (0);
1572 }
1573 
1574 udsize(dev)
1575 	dev_t dev;
1576 {
1577 	register int unit = udunit(dev);
1578 	register struct uba_device *ui;
1579 
1580 	if (unit >= NRA || (ui = uddinfo[unit]) == 0 || ui->ui_alive == 0 ||
1581 	    ui->ui_flags == 0 || ra_info[unit].rastate != OPEN)
1582 		return (-1);
1583 	return ((int)udlabel[unit].d_partitions[udpart(dev)].p_size);
1584 }
1585 
1586 #ifdef COMPAT_42
1587 struct size {
1588 	daddr_t nblocks;
1589 	daddr_t blkoff;
1590 }  ra25_sizes[8] = {
1591 	15884,	0,		/* A=blk 0 thru 15883 */
1592 	10032,	15884,		/* B=blk 15884 thru 49323 */
1593 	-1,	0,		/* C=blk 0 thru end */
1594 	0,	0,		/* D=blk 340670 thru 356553 */
1595 	0,	0,		/* E=blk 356554 thru 412489 */
1596 	0,	0,		/* F=blk 412490 thru end */
1597 	-1,	25916,		/* G=blk 49324 thru 131403 */
1598 	0,	0,		/* H=blk 131404 thru end */
1599 }, rd52_sizes[8] = {
1600 	15884,	0,		/* A=blk 0 thru 15883 */
1601 	9766,	15884,		/* B=blk 15884 thru 25649 */
1602 	-1,	0,		/* C=blk 0 thru end */
1603 	0,	0,		/* D=unused */
1604 	0,	0,		/* E=unused */
1605 	0,	0,		/* F=unused */
1606 	-1,	25650,		/* G=blk 25650 thru end */
1607 	0,	0,		/* H=unused */
1608 }, rd53_sizes[8] = {
1609 	15884,	0,		/* A=blk 0 thru 15883 */
1610 	33440,	15884,		/* B=blk 15884 thru 49323 */
1611 	-1,	0,		/* C=blk 0 thru end */
1612 	0,	0,		/* D=unused */
1613 	33440,	0,		/* E=blk 0 thru 33439 */
1614 	-1,	33440,		/* F=blk 33440 thru end */
1615 	-1,	49324,		/* G=blk 49324 thru end */
1616 	-1,	15884,		/* H=blk 15884 thru end */
1617 }, ra60_sizes[8] = {
1618 	15884,	0,		/* A=sectors 0 thru 15883 */
1619 	33440,	15884,		/* B=sectors 15884 thru 49323 */
1620 	400176,	0,		/* C=sectors 0 thru 400175 */
1621 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
1622 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
1623 	350852,	49324,		/* F=sectors 49324 thru 400175 */
1624 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
1625 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
1626 }, ra80_sizes[8] = {
1627 	15884,	0,		/* A=sectors 0 thru 15883 */
1628 	33440,	15884,		/* B=sectors 15884 thru 49323 */
1629 	242606,	0,		/* C=sectors 0 thru 242605 */
1630 	0,	0,		/* D=unused */
1631 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
1632 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
1633 	192696,	49910,		/* G=sectors 49910 thru 242605 */
1634 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
1635 }, ra81_sizes[8] ={
1636 /*
1637  * These are the new standard partition sizes for ra81's.
1638  * An RA_COMPAT system is compiled with D, E, and F corresponding
1639  * to the 4.2 partitions for G, H, and F respectively.
1640  */
1641 #ifndef	UCBRA
1642 	15884,	0,		/* A=sectors 0 thru 15883 */
1643 	66880,	16422,		/* B=sectors 16422 thru 83301 */
1644 	891072,	0,		/* C=sectors 0 thru 891071 */
1645 #ifdef RA_COMPAT
1646 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
1647 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
1648 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
1649 #else
1650 	15884,	375564,		/* D=sectors 375564 thru 391447 */
1651 	307200,	391986,		/* E=sectors 391986 thru 699185 */
1652 	191352,	699720,		/* F=sectors 699720 thru 891071 */
1653 #endif RA_COMPAT
1654 	515508,	375564,		/* G=sectors 375564 thru 891071 */
1655 	291346,	83538,		/* H=sectors 83538 thru 374883 */
1656 
1657 /*
1658  * These partitions correspond to the sizes used by sites at Berkeley,
1659  * and by those sites that have received copies of the Berkeley driver
1660  * with deltas 6.2 or greater (11/15/83).
1661  */
1662 #else UCBRA
1663 
1664 	15884,	0,		/* A=sectors 0 thru 15883 */
1665 	33440,	15884,		/* B=sectors 15884 thru 49323 */
1666 	891072,	0,		/* C=sectors 0 thru 891071 */
1667 	15884,	242606,		/* D=sectors 242606 thru 258489 */
1668 	307200,	258490,		/* E=sectors 258490 thru 565689 */
1669 	325382,	565690,		/* F=sectors 565690 thru 891071 */
1670 	648466,	242606,		/* G=sectors 242606 thru 891071 */
1671 	193282,	49324,		/* H=sectors 49324 thru 242605 */
1672 
1673 #endif UCBRA
1674 };
1675 
1676 udmaptype(unit, lp)
1677 	register unit;
1678 	register struct disklabel *lp;
1679 {
1680 	register struct size *rasizes;
1681 	register struct partition *pp;
1682 	register type;
1683 
1684 	lp->d_secperunit = ra_info[unit].radsize;
1685 	type = ra_info[unit].ratype & 0x7f;
1686 	lp->d_typename[0] = 'r';
1687 	lp->d_typename[1] = 'a';
1688 	lp->d_typename[2] = '0' + type/10;
1689 	lp->d_typename[3] = '0' + type%10;
1690 	switch (type) {
1691 	case    25:
1692 		rasizes = ra25_sizes;
1693 		lp->d_nsectors = 42;
1694 		lp->d_ntracks = 4;
1695 		lp->d_ncylinders = 302;
1696 		break;
1697 	case    52:
1698 		lp->d_typename[1] = 'd';
1699 		rasizes = rd52_sizes;
1700 		lp->d_nsectors = 18;
1701 		lp->d_ntracks = 7;
1702 		lp->d_ncylinders = 480;
1703 		break;
1704 	case    53:
1705 		rasizes = rd53_sizes;
1706 		lp->d_typename[1] = 'd';
1707 		lp->d_nsectors = 18;
1708 		lp->d_ntracks = 8;
1709 		lp->d_ncylinders = 963;
1710 		break;
1711 	case    60:
1712 		rasizes = ra60_sizes;
1713 		lp->d_nsectors = 42;
1714 		lp->d_ntracks = 4;
1715 		lp->d_ncylinders = 2382;
1716 		break;
1717 	case    80:
1718 		rasizes = ra80_sizes;
1719 		lp->d_nsectors = 31;
1720 		lp->d_ntracks = 14;
1721 		lp->d_ncylinders = 559;
1722 		break;
1723 	case    81:
1724 		rasizes = ra81_sizes;
1725 		lp->d_nsectors = 51;
1726 		lp->d_ntracks = 14;
1727 		lp->d_ncylinders = 1248;
1728 		break;
1729 	default:
1730 		printf("Don't have a partition table for an ra%d\n", type);
1731 		lp->d_npartitions = 1;
1732 		lp->d_partitions[0].p_offset = 0;
1733 		lp->d_partitions[0].p_size = lp->d_secperunit;
1734 		return (0);
1735 	}
1736 	lp->d_secsize = 512;
1737 	lp->d_npartitions = 8;
1738 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1739 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8];
1740 	    pp++, rasizes++) {
1741 		pp->p_offset = rasizes->blkoff;
1742 		if ((pp->p_size = rasizes->nblocks) == (u_long)-1)
1743 			pp->p_size = ra_info[unit].radsize - rasizes->blkoff;
1744 	}
1745 	return (1);
1746 }
1747 #endif COMPAT_42
1748 #endif
1749