xref: /csrg-svn/sys/vax/uba/up.c (revision 24743)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)up.c	6.7 (Berkeley) 09/14/85
7  */
8 
9 #include "up.h"
10 #if NSC > 0
11 /*
12  * UNIBUS disk driver with:
13  *	overlapped seeks,
14  *	ECC recovery, and
15  *	bad sector forwarding.
16  *
17  * TODO:
18  *	Check that offset recovery code works
19  */
20 #include "../machine/pte.h"
21 
22 #include "param.h"
23 #include "systm.h"
24 #include "dk.h"
25 #include "dkbad.h"
26 #include "buf.h"
27 #include "conf.h"
28 #include "dir.h"
29 #include "user.h"
30 #include "map.h"
31 #include "vm.h"
32 #include "cmap.h"
33 #include "uio.h"
34 #include "kernel.h"
35 #include "syslog.h"
36 
37 #include "../vax/cpu.h"
38 #include "../vax/nexus.h"
39 #include "ubavar.h"
40 #include "ubareg.h"
41 #include "upreg.h"
42 
43 struct	up_softc {
44 	int	sc_softas;
45 	int	sc_ndrive;
46 	int	sc_wticks;
47 	int	sc_recal;
48 } up_softc[NSC];
49 
50 #define upunit(dev)	(minor(dev) >> 3)
51 
52 /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
53 struct	size {
54 	daddr_t	nblocks;
55 	int	cyloff;
56 } up9300_sizes[8] = {
57 	15884,	0,		/* A=cyl 0 thru 26 */
58 	33440,	27,		/* B=cyl 27 thru 81 */
59 	495520,	0,		/* C=cyl 0 thru 814 */
60 	15884,	562,		/* D=cyl 562 thru 588 */
61 	55936,	589,		/* E=cyl 589 thru 680 */
62 	81376,	681,		/* F=cyl 681 thru 814 */
63 	153728,	562,		/* G=cyl 562 thru 814 */
64 	291346,	82,		/* H=cyl 82 thru 561 */
65 }, up9766_sizes[8] = {
66 	15884,	0,		/* A=cyl 0 thru 26 */
67 	33440,	27,		/* B=cyl 27 thru 81 */
68 	500384,	0,		/* C=cyl 0 thru 822 */
69 	15884,	562,		/* D=cyl 562 thru 588 */
70 	55936,	589,		/* E=cyl 589 thru 680 */
71 	86240,	681,		/* F=cyl 681 thru 822 */
72 	158592,	562,		/* G=cyl 562 thru 822 */
73 	291346,	82,		/* H=cyl 82 thru 561 */
74 }, up160_sizes[8] = {
75 	15884,	0,		/* A=cyl 0 thru 49 */
76 	33440,	50,		/* B=cyl 50 thru 154 */
77 	263360,	0,		/* C=cyl 0 thru 822 */
78 	15884,	155,		/* D=cyl 155 thru 204 */
79 	55936,	205,		/* E=cyl 205 thru 379 */
80 	141664,	380,		/* F=cyl 380 thru 822 */
81 	213664,	155,		/* G=cyl 155 thru 822 */
82 	0,	0,
83 }, upam_sizes[8] = {
84 	15884,	0,		/* A=cyl 0 thru 31 */
85 	33440,	32,		/* B=cyl 32 thru 97 */
86 	524288,	0,		/* C=cyl 0 thru 1023 */
87 	15884,	668,		/* D=cyl 668 thru 699 */
88 	55936,	700,		/* E=cyl 700 thru 809 */
89 	109472,	810,		/* F=cyl 810 thru 1023 */
90 	182176,	668,		/* G=cyl 668 thru 1023 */
91 	291346,	98,		/* H=cyl 98 thru 667 */
92 }, up980_sizes[8] = {
93 	15884,	0,		/* A=cyl 0 thru 99 */
94 	33440,	100,		/* B=cyl 100 thru 308 */
95 	131680,	0,		/* C=cyl 0 thru 822 */
96 	15884,	309,		/* D=cyl 309 thru 408 */
97 	55936,	409,		/* E=cyl 409 thru 758 */
98 	10080,	759,		/* F=cyl 759 thru 822 */
99 	82080,	309,		/* G=cyl 309 thru 822 */
100 	0,	0,
101 }, upeagle_sizes[8] = {
102 	15884,	0,		/* A=cyl 0 thru 16 */
103 	66880,	17,		/* B=cyl 17 thru 86 */
104 	808320,	0,		/* C=cyl 0 thru 841 */
105 	15884,	391,		/* D=cyl 391 thru 407 */
106 	307200,	408,		/* E=cyl 408 thru 727 */
107 	109296,	728,		/* F=cyl 728 thru 841 */
108 	432816,	391,		/* G=cyl 391 thru 841 */
109 	291346,	87,		/* H=cyl 87 thru 390 */
110 };
111 /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
112 
113 int	upprobe(), upslave(), upattach(), updgo(), upintr();
114 struct	uba_ctlr *upminfo[NSC];
115 struct	uba_device *updinfo[NUP];
116 #define	UPIPUNITS	8
117 struct	uba_device *upip[NSC][UPIPUNITS]; /* fuji w/fixed head gives n,n+4 */
118 
119 u_short	upstd[] = { 0776700, 0774400, 0776300, 0 };
120 struct	uba_driver scdriver =
121     { upprobe, upslave, upattach, updgo, upstd, "up", updinfo, "sc", upminfo };
122 struct	buf	uputab[NUP];
123 char upinit[NUP];
124 
125 struct	upst {
126 	short	nsect;		/* # sectors/track */
127 	short	ntrak;		/* # tracks/cylinder */
128 	short	nspc;		/* # sectors/cylinder */
129 	short	ncyl;		/* # cylinders */
130 	struct	size *sizes;	/* partition tables */
131 	short	sdist;		/* seek distance metric */
132 	short	rdist;		/* rotational distance metric */
133 } upst[] = {
134 	{ 32,	19,	32*19,	815,	up9300_sizes,	3, 4 },	/* 9300 */
135 	{ 32,	19,	32*19,	823,	up9766_sizes,	3, 4 },	/* 9766 */
136 	{ 32,	10,	32*10,	823,	up160_sizes,	3, 4 },	/* fuji 160m */
137 	{ 32,	16,	32*16,	1024,	upam_sizes,	7, 8 },	/* Capricorn */
138 	{ 32,	5,	32*5,	823,	up980_sizes,	3, 4 }, /* DM980 */
139         { 48,	20,	48*20,	842,	upeagle_sizes, 15, 8 },	/* EAGLE */
140 	{ 0,	0,	0,	0,	0,		0, 0 }
141 };
142 
143 u_char	up_offset[16] = {
144 	UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400,
145 	UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800,
146 	UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200,
147 	0, 0, 0, 0
148 };
149 
150 struct	buf	rupbuf[NUP];
151 struct 	buf	bupbuf[NUP];
152 struct	dkbad	upbad[NUP];
153 
154 #define	b_cylin b_resid
155 
156 int	upwstart, upwatch();		/* Have started guardian */
157 int	upseek;
158 int	upwaitdry;
159 
160 /*ARGSUSED*/
161 upprobe(reg)
162 	caddr_t reg;
163 {
164 	register int br, cvec;
165 
166 #ifdef lint
167 	br = 0; cvec = br; br = cvec; upintr(0);
168 #endif
169 	((struct updevice *)reg)->upcs1 = UP_IE|UP_RDY;
170 	DELAY(10);
171 	((struct updevice *)reg)->upcs1 = 0;
172 	return (sizeof (struct updevice));
173 }
174 
175 upslave(ui, reg)
176 	struct uba_device *ui;
177 	caddr_t reg;
178 {
179 	register struct updevice *upaddr = (struct updevice *)reg;
180 
181 	upaddr->upcs1 = 0;		/* conservative */
182 	upaddr->upcs2 = ui->ui_slave;
183 	upaddr->upcs1 = UP_NOP|UP_GO;
184 	if (upaddr->upcs2&UPCS2_NED) {
185 		upaddr->upcs1 = UP_DCLR|UP_GO;
186 		return (0);
187 	}
188 	return (1);
189 }
190 
191 upattach(ui)
192 	register struct uba_device *ui;
193 {
194 
195 	if (upwstart == 0) {
196 		timeout(upwatch, (caddr_t)0, hz);
197 		upwstart++;
198 	}
199 	if (ui->ui_dk >= 0)
200 		dk_mspw[ui->ui_dk] = .0000020345;
201 	upip[ui->ui_ctlr][ui->ui_slave] = ui;
202 	up_softc[ui->ui_ctlr].sc_ndrive++;
203 	ui->ui_type = upmaptype(ui);
204 }
205 
206 upmaptype(ui)
207 	register struct uba_device *ui;
208 {
209 	register struct updevice *upaddr = (struct updevice *)ui->ui_addr;
210 	int type = ui->ui_type;
211 	register struct upst *st;
212 
213 	upaddr->upcs1 = 0;
214 	upaddr->upcs2 = ui->ui_slave;
215 	upaddr->uphr = UPHR_MAXTRAK;
216 	for (st = upst; st->nsect != 0; st++)
217 		if (upaddr->uphr == st->ntrak - 1) {
218 			type = st - upst;
219 			break;
220 		}
221 	if (st->nsect == 0)
222 		printf("up%d: uphr=%x\n", ui->ui_slave, upaddr->uphr);
223 	if (type == 0) {
224 		upaddr->uphr = UPHR_MAXCYL;
225 		if (upaddr->uphr == 822)
226 			type++;
227 	}
228 	upaddr->upcs2 = UPCS2_CLR;
229 	return (type);
230 }
231 
232 upopen(dev)
233 	dev_t dev;
234 {
235 	register int unit = upunit(dev);
236 	register struct uba_device *ui;
237 
238 	if (unit >= NUP || (ui = updinfo[unit]) == 0 || ui->ui_alive == 0)
239 		return (ENXIO);
240 	return (0);
241 }
242 
243 upstrategy(bp)
244 	register struct buf *bp;
245 {
246 	register struct uba_device *ui;
247 	register struct upst *st;
248 	register int unit;
249 	register struct buf *dp;
250 	int xunit = minor(bp->b_dev) & 07;
251 	long bn, sz;
252 	int s;
253 
254 	sz = (bp->b_bcount+511) >> 9;
255 	unit = upunit(bp->b_dev);
256 	if (unit >= NUP) {
257 		bp->b_error = ENXIO;
258 		goto bad;
259 	}
260 	ui = updinfo[unit];
261 	if (ui == 0 || ui->ui_alive == 0) {
262 		bp->b_error = ENXIO;
263 		goto bad;
264 	}
265 	st = &upst[ui->ui_type];
266 	if (bp->b_blkno < 0 ||
267 	    (bn = bp->b_blkno)+sz > st->sizes[xunit].nblocks) {
268 		if (bp->b_blkno == st->sizes[xunit].nblocks + 1)
269 			goto done;
270 		bp->b_error = EINVAL;
271 		goto bad;
272 	}
273 	bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff;
274 	s = spl5();
275 	dp = &uputab[ui->ui_unit];
276 	disksort(dp, bp);
277 	if (dp->b_active == 0) {
278 		(void) upustart(ui);
279 		bp = &ui->ui_mi->um_tab;
280 		if (bp->b_actf && bp->b_active == 0)
281 			(void) upstart(ui->ui_mi);
282 	}
283 	splx(s);
284 	return;
285 
286 bad:
287 	bp->b_flags |= B_ERROR;
288 done:
289 	iodone(bp);
290 	return;
291 }
292 
293 /*
294  * Unit start routine.
295  * Seek the drive to be where the data is
296  * and then generate another interrupt
297  * to actually start the transfer.
298  * If there is only one drive on the controller,
299  * or we are very close to the data, don't
300  * bother with the search.  If called after
301  * searching once, don't bother to look where
302  * we are, just queue for transfer (to avoid
303  * positioning forever without transferrring.)
304  */
305 upustart(ui)
306 	register struct uba_device *ui;
307 {
308 	register struct buf *bp, *dp;
309 	register struct uba_ctlr *um;
310 	register struct updevice *upaddr;
311 	register struct upst *st;
312 	daddr_t bn;
313 	int sn, csn;
314 	/*
315 	 * The SC21 cancels commands if you just say
316 	 *	cs1 = UP_IE
317 	 * so we are cautious about handling of cs1.
318 	 * Also don't bother to clear as bits other than in upintr().
319 	 */
320 	int didie = 0;
321 
322 	if (ui == 0)
323 		return (0);
324 	um = ui->ui_mi;
325 	dk_busy &= ~(1<<ui->ui_dk);
326 	dp = &uputab[ui->ui_unit];
327 	if ((bp = dp->b_actf) == NULL)
328 		goto out;
329 	/*
330 	 * If the controller is active, just remember
331 	 * that this device would like to be positioned...
332 	 * if we tried to position now we would confuse the SC21.
333 	 */
334 	if (um->um_tab.b_active) {
335 		up_softc[um->um_ctlr].sc_softas |= 1<<ui->ui_slave;
336 		return (0);
337 	}
338 	/*
339 	 * If we have already positioned this drive,
340 	 * then just put it on the ready queue.
341 	 */
342 	if (dp->b_active)
343 		goto done;
344 	dp->b_active = 1;
345 	upaddr = (struct updevice *)um->um_addr;
346 	upaddr->upcs2 = ui->ui_slave;
347 	/*
348 	 * If drive has just come up,
349 	 * setup the pack.
350 	 */
351 	if ((upaddr->upds & UPDS_VV) == 0 || upinit[ui->ui_unit] == 0) {
352 		struct buf *bbp = &bupbuf[ui->ui_unit];
353 
354 		/* SHOULD WARN SYSTEM THAT THIS HAPPENED */
355 		upinit[ui->ui_unit] = 1;
356 		upaddr->upcs1 = UP_IE|UP_DCLR|UP_GO;
357 		upaddr->upcs1 = UP_IE|UP_PRESET|UP_GO;
358 		upaddr->upof = UPOF_FMT22;
359 		didie = 1;
360 		st = &upst[ui->ui_type];
361 		bbp->b_flags = B_READ|B_BUSY;
362 		bbp->b_dev = bp->b_dev;
363 		bbp->b_bcount = 512;
364 		bbp->b_un.b_addr = (caddr_t)&upbad[ui->ui_unit];
365 		bbp->b_blkno = st->ncyl * st->nspc - st->nsect;
366 		bbp->b_cylin = st->ncyl - 1;
367 		dp->b_actf = bbp;
368 		bbp->av_forw = bp;
369 		bp = bbp;
370 	}
371 	/*
372 	 * If drive is offline, forget about positioning.
373 	 */
374 	if ((upaddr->upds & (UPDS_DPR|UPDS_MOL)) != (UPDS_DPR|UPDS_MOL))
375 		goto done;
376 	/*
377 	 * If there is only one drive,
378 	 * dont bother searching.
379 	 */
380 	if (up_softc[um->um_ctlr].sc_ndrive == 1)
381 		goto done;
382 	/*
383 	 * Figure out where this transfer is going to
384 	 * and see if we are close enough to justify not searching.
385 	 */
386 	st = &upst[ui->ui_type];
387 	bn = bp->b_blkno;
388 	sn = bn%st->nspc;
389 	sn = (sn + st->nsect - st->sdist) % st->nsect;
390 	if (bp->b_cylin - upaddr->updc)
391 		goto search;		/* Not on-cylinder */
392 	else if (upseek)
393 		goto done;		/* Ok just to be on-cylinder */
394 	csn = (upaddr->upla>>6) - sn - 1;
395 	if (csn < 0)
396 		csn += st->nsect;
397 	if (csn > st->nsect - st->rdist)
398 		goto done;
399 search:
400 	upaddr->updc = bp->b_cylin;
401 	/*
402 	 * Not on cylinder at correct position,
403 	 * seek/search.
404 	 */
405 	if (upseek)
406 		upaddr->upcs1 = UP_IE|UP_SEEK|UP_GO;
407 	else {
408 		upaddr->upda = sn;
409 		upaddr->upcs1 = UP_IE|UP_SEARCH|UP_GO;
410 	}
411 	didie = 1;
412 	/*
413 	 * Mark unit busy for iostat.
414 	 */
415 	if (ui->ui_dk >= 0) {
416 		dk_busy |= 1<<ui->ui_dk;
417 		dk_seek[ui->ui_dk]++;
418 	}
419 	goto out;
420 done:
421 	/*
422 	 * Device is ready to go.
423 	 * Put it on the ready queue for the controller
424 	 * (unless its already there.)
425 	 */
426 	if (dp->b_active != 2) {
427 		dp->b_forw = NULL;
428 		if (um->um_tab.b_actf == NULL)
429 			um->um_tab.b_actf = dp;
430 		else
431 			um->um_tab.b_actl->b_forw = dp;
432 		um->um_tab.b_actl = dp;
433 		dp->b_active = 2;
434 	}
435 out:
436 	return (didie);
437 }
438 
439 /*
440  * Start up a transfer on a drive.
441  */
442 upstart(um)
443 	register struct uba_ctlr *um;
444 {
445 	register struct buf *bp, *dp;
446 	register struct uba_device *ui;
447 	register struct updevice *upaddr;
448 	struct upst *st;
449 	daddr_t bn;
450 	int dn, sn, tn, cmd, waitdry;
451 
452 loop:
453 	/*
454 	 * Pull a request off the controller queue
455 	 */
456 	if ((dp = um->um_tab.b_actf) == NULL)
457 		return (0);
458 	if ((bp = dp->b_actf) == NULL) {
459 		um->um_tab.b_actf = dp->b_forw;
460 		goto loop;
461 	}
462 	/*
463 	 * Mark controller busy, and
464 	 * determine destination of this request.
465 	 */
466 	um->um_tab.b_active++;
467 	ui = updinfo[upunit(bp->b_dev)];
468 	bn = bp->b_blkno;
469 	dn = ui->ui_slave;
470 	st = &upst[ui->ui_type];
471 	sn = bn%st->nspc;
472 	tn = sn/st->nsect;
473 	sn %= st->nsect;
474 	upaddr = (struct updevice *)ui->ui_addr;
475 	/*
476 	 * Select drive if not selected already.
477 	 */
478 	if ((upaddr->upcs2&07) != dn)
479 		upaddr->upcs2 = dn;
480 	/*
481 	 * Check that it is ready and online
482 	 */
483 	waitdry = 0;
484 	while ((upaddr->upds&UPDS_DRY) == 0) {
485 		printf("up%d: ds wait ds=%o\n",upunit(bp->b_dev),upaddr->upds);
486 		if (++waitdry > 512)
487 			break;
488 		upwaitdry++;
489 	}
490 	if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
491 		printf("up%d: not ready", upunit(bp->b_dev));
492 		if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
493 			printf("\n");
494 			um->um_tab.b_active = 0;
495 			um->um_tab.b_errcnt = 0;
496 			dp->b_actf = bp->av_forw;
497 			dp->b_active = 0;
498 			bp->b_flags |= B_ERROR;
499 			iodone(bp);
500 			goto loop;
501 		}
502 		/*
503 		 * Oh, well, sometimes this
504 		 * happens, for reasons unknown.
505 		 */
506 		printf(" (flakey)\n");
507 	}
508 	/*
509 	 * Setup for the transfer, and get in the
510 	 * UNIBUS adaptor queue.
511 	 */
512 	upaddr->updc = bp->b_cylin;
513 	upaddr->upda = (tn << 8) + sn;
514 	upaddr->upwc = -bp->b_bcount / sizeof (short);
515 	if (bp->b_flags & B_READ)
516 		cmd = UP_IE|UP_RCOM|UP_GO;
517 	else
518 		cmd = UP_IE|UP_WCOM|UP_GO;
519 	um->um_cmd = cmd;
520 	(void) ubago(ui);
521 	return (1);
522 }
523 
524 /*
525  * Now all ready to go, stuff the registers.
526  */
527 updgo(um)
528 	struct uba_ctlr *um;
529 {
530 	register struct updevice *upaddr = (struct updevice *)um->um_addr;
531 
532 	um->um_tab.b_active = 2;	/* should now be 2 */
533 	upaddr->upba = um->um_ubinfo;
534 	upaddr->upcs1 = um->um_cmd|((um->um_ubinfo>>8)&0x300);
535 }
536 
537 /*
538  * Handle a disk interrupt.
539  */
540 upintr(sc21)
541 	register sc21;
542 {
543 	register struct buf *bp, *dp;
544 	register struct uba_ctlr *um = upminfo[sc21];
545 	register struct uba_device *ui;
546 	register struct updevice *upaddr = (struct updevice *)um->um_addr;
547 	register unit;
548 	struct up_softc *sc = &up_softc[um->um_ctlr];
549 	int as = (upaddr->upas & 0377) | sc->sc_softas;
550 	int needie = 1, waitdry;
551 
552 	sc->sc_wticks = 0;
553 	sc->sc_softas = 0;
554 	/*
555 	 * If controller wasn't transferring, then this is an
556 	 * interrupt for attention status on seeking drives.
557 	 * Just service them.
558 	 */
559 	if (um->um_tab.b_active != 2 && !sc->sc_recal) {
560 		if (upaddr->upcs1 & UP_TRE)
561 			upaddr->upcs1 = UP_TRE;
562 		goto doattn;
563 	}
564 	um->um_tab.b_active = 1;
565 	/*
566 	 * Get device and block structures, and a pointer
567 	 * to the uba_device for the drive.  Select the drive.
568 	 */
569 	dp = um->um_tab.b_actf;
570 	bp = dp->b_actf;
571 	ui = updinfo[upunit(bp->b_dev)];
572 	dk_busy &= ~(1 << ui->ui_dk);
573 	if ((upaddr->upcs2&07) != ui->ui_slave)
574 		upaddr->upcs2 = ui->ui_slave;
575 	if (bp->b_flags&B_BAD) {
576 		if (upecc(ui, CONT))
577 			return;
578 	}
579 	/*
580 	 * Check for and process errors on
581 	 * either the drive or the controller.
582 	 */
583 	if ((upaddr->upds&UPDS_ERR) || (upaddr->upcs1&UP_TRE)) {
584 		waitdry = 0;
585 		while ((upaddr->upds & UPDS_DRY) == 0) {
586 			if (++waitdry > 512)
587 				break;
588 			upwaitdry++;
589 		}
590 		if (upaddr->uper1&UPER1_WLE) {
591 			/*
592 			 * Give up on write locked devices
593 			 * immediately.
594 			 */
595 			printf("up%d: write locked\n", upunit(bp->b_dev));
596 			bp->b_flags |= B_ERROR;
597 		} else if (++um->um_tab.b_errcnt > 27) {
598 			/*
599 			 * After 28 retries (16 without offset, and
600 			 * 12 with offset positioning) give up.
601 			 * If the error was header CRC, the header is
602 			 * screwed up, and the sector may in fact exist
603 			 * in the bad sector table, better check...
604 			 */
605 			if (upaddr->uper1&UPER1_HCRC) {
606 				if (upecc(ui, BSE))
607 					return;
608 			}
609 	hard:
610 			harderr(bp, "up");
611 			printf("cn=%d tn=%d sn=%d cs2=%b er1=%b er2=%b\n",
612 			        upaddr->updc, ((upaddr->upda)>>8)&077,
613 			        (upaddr->upda)&037,
614 				upaddr->upcs2, UPCS2_BITS,
615 				upaddr->uper1, UPER1_BITS,
616 				upaddr->uper2, UPER2_BITS);
617 			bp->b_flags |= B_ERROR;
618 		} else if (upaddr->uper2 & UPER2_BSE) {
619 			if (upecc(ui, BSE))
620 				return;
621 			else
622 				goto hard;
623 		} else {
624 			/*
625 			 * Retriable error.
626 			 * If a soft ecc, correct it (continuing
627 			 * by returning if necessary.
628 			 * Otherwise fall through and retry the transfer
629 			 */
630 			if ((upaddr->uper1&(UPER1_DCK|UPER1_ECH))==UPER1_DCK) {
631 				if (upecc(ui, ECC))
632 					return;
633 			} else
634 				um->um_tab.b_active = 0; /* force retry */
635 		}
636 		/*
637 		 * Clear drive error and, every eight attempts,
638 		 * (starting with the fourth)
639 		 * recalibrate to clear the slate.
640 		 */
641 		upaddr->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
642 		needie = 0;
643 		if ((um->um_tab.b_errcnt&07) == 4 && um->um_tab.b_active == 0) {
644 			upaddr->upcs1 = UP_RECAL|UP_IE|UP_GO;
645 			sc->sc_recal = 0;
646 			goto nextrecal;
647 		}
648 	}
649 	/*
650 	 * Advance recalibration finite state machine
651 	 * if recalibrate in progress, through
652 	 *	RECAL
653 	 *	SEEK
654 	 *	OFFSET (optional)
655 	 *	RETRY
656 	 */
657 	switch (sc->sc_recal) {
658 
659 	case 1:
660 		upaddr->updc = bp->b_cylin;
661 		upaddr->upcs1 = UP_SEEK|UP_IE|UP_GO;
662 		goto nextrecal;
663 	case 2:
664 		if (um->um_tab.b_errcnt < 16 || (bp->b_flags&B_READ) == 0)
665 			goto donerecal;
666 		upaddr->upof = up_offset[um->um_tab.b_errcnt & 017] | UPOF_FMT22;
667 		upaddr->upcs1 = UP_IE|UP_OFFSET|UP_GO;
668 		goto nextrecal;
669 	nextrecal:
670 		sc->sc_recal++;
671 		um->um_tab.b_active = 1;
672 		return;
673 	donerecal:
674 	case 3:
675 		sc->sc_recal = 0;
676 		um->um_tab.b_active = 0;
677 		break;
678 	}
679 	/*
680 	 * If still ``active'', then don't need any more retries.
681 	 */
682 	if (um->um_tab.b_active) {
683 		/*
684 		 * If we were offset positioning,
685 		 * return to centerline.
686 		 */
687 		if (um->um_tab.b_errcnt >= 16) {
688 			upaddr->upof = UPOF_FMT22;
689 			upaddr->upcs1 = UP_RTC|UP_GO|UP_IE;
690 			while (upaddr->upds & UPDS_PIP)
691 				DELAY(25);
692 			needie = 0;
693 		}
694 		um->um_tab.b_active = 0;
695 		um->um_tab.b_errcnt = 0;
696 		um->um_tab.b_actf = dp->b_forw;
697 		dp->b_active = 0;
698 		dp->b_errcnt = 0;
699 		dp->b_actf = bp->av_forw;
700 		bp->b_resid = (-upaddr->upwc * sizeof(short));
701 		iodone(bp);
702 		/*
703 		 * If this unit has more work to do,
704 		 * then start it up right away.
705 		 */
706 		if (dp->b_actf)
707 			if (upustart(ui))
708 				needie = 0;
709 	}
710 	as &= ~(1<<ui->ui_slave);
711 	/*
712 	 * Release unibus resources and flush data paths.
713 	 */
714 	ubadone(um);
715 doattn:
716 	/*
717 	 * Process other units which need attention.
718 	 * For each unit which needs attention, call
719 	 * the unit start routine to place the slave
720 	 * on the controller device queue.
721 	 */
722 	while (unit = ffs(as)) {
723 		unit--;		/* was 1 origin */
724 		as &= ~(1<<unit);
725 		upaddr->upas = 1<<unit;
726 		if (unit < UPIPUNITS && upustart(upip[sc21][unit]))
727 			needie = 0;
728 	}
729 	/*
730 	 * If the controller is not transferring, but
731 	 * there are devices ready to transfer, start
732 	 * the controller.
733 	 */
734 	if (um->um_tab.b_actf && um->um_tab.b_active == 0)
735 		if (upstart(um))
736 			needie = 0;
737 	if (needie)
738 		upaddr->upcs1 = UP_IE;
739 }
740 
741 upread(dev, uio)
742 	dev_t dev;
743 	struct uio *uio;
744 {
745 	register int unit = upunit(dev);
746 
747 	if (unit >= NUP)
748 		return (ENXIO);
749 	return (physio(upstrategy, &rupbuf[unit], dev, B_READ, minphys, uio));
750 }
751 
752 upwrite(dev, uio)
753 	dev_t dev;
754 	struct uio *uio;
755 {
756 	register int unit = upunit(dev);
757 
758 	if (unit >= NUP)
759 		return (ENXIO);
760 	return (physio(upstrategy, &rupbuf[unit], dev, B_WRITE, minphys, uio));
761 }
762 
763 /*
764  * Correct an ECC error, and restart the i/o to complete
765  * the transfer if necessary.  This is quite complicated because
766  * the transfer may be going to an odd memory address base and/or
767  * across a page boundary.
768  */
769 upecc(ui, flag)
770 	register struct uba_device *ui;
771 	int flag;
772 {
773 	register struct updevice *up = (struct updevice *)ui->ui_addr;
774 	register struct buf *bp = uputab[ui->ui_unit].b_actf;
775 	register struct uba_ctlr *um = ui->ui_mi;
776 	register struct upst *st;
777 	struct uba_regs *ubp = ui->ui_hd->uh_uba;
778 	register int i;
779 	caddr_t addr;
780 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
781 	int bn, cn, tn, sn;
782 
783 	/*
784 	 * Npf is the number of sectors transferred before the sector
785 	 * containing the ECC error, and reg is the UBA register
786 	 * mapping (the first part of) the transfer.
787 	 * O is offset within a memory page of the first byte transferred.
788 	 */
789 	if (flag == CONT)
790 		npf = bp->b_error;
791 	else
792 		npf = btop((up->upwc * sizeof(short)) + bp->b_bcount);
793 	reg = btop(um->um_ubinfo&0x3ffff) + npf;
794 	o = (int)bp->b_un.b_addr & PGOFSET;
795 	mask = up->upec2;
796 #ifdef UPECCDEBUG
797 	printf("npf %d reg %x o %d mask %o pos %d\n", npf, reg, o, mask,
798 	    up->upec1);
799 #endif
800 	bn = bp->b_blkno;
801 	st = &upst[ui->ui_type];
802 	cn = bp->b_cylin;
803 	sn = bn%st->nspc + npf;
804 	tn = sn/st->nsect;
805 	sn %= st->nsect;
806 	cn += tn/st->ntrak;
807 	tn %= st->ntrak;
808 	ubapurge(um);
809 	um->um_tab.b_active=2;
810 	/*
811 	 * action taken depends on the flag
812 	 */
813 	switch(flag){
814 	case ECC:
815 		npf--;
816 		reg--;
817 		mask = up->upec2;
818 		log(KERN_RECOV, "up%d%c: soft ecc sn%d\n", upunit(bp->b_dev),
819 			'a'+(minor(bp->b_dev)&07), bp->b_blkno + npf);
820 		/*
821 		 * Flush the buffered data path, and compute the
822 		 * byte and bit position of the error.  The variable i
823 		 * is the byte offset in the transfer, the variable byte
824 		 * is the offset from a page boundary in main memory.
825 		 */
826 		i = up->upec1 - 1;		/* -1 makes 0 origin */
827 		bit = i&07;
828 		i = (i&~07)>>3;
829 		byte = i + o;
830 		/*
831 		 * Correct while possible bits remain of mask.  Since mask
832 		 * contains 11 bits, we continue while the bit offset is > -11.
833 		 * Also watch out for end of this block and the end of the whole
834 		 * transfer.
835 		 */
836 		while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
837 			struct pte pte;
838 
839 			pte = ubp->uba_map[reg + btop(byte)];
840 			addr = ptob(pte.pg_pfnum) + (byte & PGOFSET);
841 #ifdef UPECCDEBUG
842 			printf("addr %x map reg %x\n",
843 				addr, *(int *)(&ubp->uba_map[reg+btop(byte)]));
844 			printf("old: %x, ", getmemc(addr));
845 #endif
846 			putmemc(addr, getmemc(addr)^(mask<<bit));
847 #ifdef UPECCDEBUG
848 			printf("new: %x\n", getmemc(addr));
849 #endif
850 			byte++;
851 			i++;
852 			bit -= 8;
853 		}
854 		if (up->upwc == 0)
855 			return (0);
856 		npf++;
857 		reg++;
858 		break;
859 	case BSE:
860 		/*
861 		 * if not in bad sector table, return 0
862 		 */
863 		if ((bn = isbad(&upbad[ui->ui_unit], cn, tn, sn)) < 0)
864 			return(0);
865 		/*
866 		 * flag this one as bad
867 		 */
868 		bp->b_flags |= B_BAD;
869 		bp->b_error = npf + 1;
870 #ifdef UPECCDEBUG
871 		printf("BSE: restart at %d\n",npf+1);
872 #endif
873 		bn = st->ncyl * st->nspc -st->nsect - 1 - bn;
874 		cn = bn / st->nspc;
875 		sn = bn % st->nspc;
876 		tn = sn / st->nsect;
877 		sn %= st->nsect;
878 		up->upwc = -(512 / sizeof (short));
879 #ifdef UPECCDEBUG
880 		printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
881 #endif
882 		break;
883 	case CONT:
884 #ifdef UPECCDEBUG
885 		printf("upecc, CONT: bn %d cn %d tn %d sn %d\n", bn, cn, tn, sn);
886 #endif
887 		bp->b_flags &= ~B_BAD;
888 		up->upwc = -((bp->b_bcount - (int)ptob(npf)) / sizeof(short));
889 		if (up->upwc == 0)
890 			return(0);
891 		break;
892 	}
893 	if (up->upwc == 0) {
894 		um->um_tab.b_active = 0;
895 		return (0);
896 	}
897 	/*
898 	 * Have to continue the transfer... clear the drive,
899 	 * and compute the position where the transfer is to continue.
900 	 * We have completed npf+1 sectors of the transfer already;
901 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
902 	 */
903 #ifdef notdef
904 	up->uper1 = 0;
905 	up->upcs1 |= UP_GO;
906 #else
907 	up->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
908 	up->updc = cn;
909 	up->upda = (tn << 8) | sn;
910 	ubaddr = (int)ptob(reg) + o;
911 	up->upba = ubaddr;
912 	cmd = (ubaddr >> 8) & 0x300;
913 	cmd |= ((bp->b_flags&B_READ)?UP_RCOM:UP_WCOM)|UP_IE|UP_GO;
914 	um->um_tab.b_errcnt = 0;
915 	up->upcs1 = cmd;
916 #endif
917 	return (1);
918 }
919 
920 /*
921  * Reset driver after UBA init.
922  * Cancel software state of all pending transfers
923  * and restart all units and the controller.
924  */
925 upreset(uban)
926 	int uban;
927 {
928 	register struct uba_ctlr *um;
929 	register struct uba_device *ui;
930 	register sc21, unit;
931 
932 	for (sc21 = 0; sc21 < NSC; sc21++) {
933 		if ((um = upminfo[sc21]) == 0 || um->um_ubanum != uban ||
934 		    um->um_alive == 0)
935 			continue;
936 		printf(" sc%d", sc21);
937 		um->um_tab.b_active = 0;
938 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
939 		up_softc[sc21].sc_recal = 0;
940 		up_softc[sc21].sc_wticks = 0;
941 		if (um->um_ubinfo) {
942 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
943 			um->um_ubinfo = 0;
944 		}
945 		((struct updevice *)(um->um_addr))->upcs2 = UPCS2_CLR;
946 		for (unit = 0; unit < NUP; unit++) {
947 			if ((ui = updinfo[unit]) == 0)
948 				continue;
949 			if (ui->ui_alive == 0 || ui->ui_mi != um)
950 				continue;
951 			uputab[unit].b_active = 0;
952 			(void) upustart(ui);
953 		}
954 		(void) upstart(um);
955 	}
956 }
957 
958 /*
959  * Wake up every second and if an interrupt is pending
960  * but nothing has happened increment a counter.
961  * If nothing happens for 20 seconds, reset the UNIBUS
962  * and begin anew.
963  */
964 upwatch()
965 {
966 	register struct uba_ctlr *um;
967 	register sc21, unit;
968 	register struct up_softc *sc;
969 
970 	timeout(upwatch, (caddr_t)0, hz);
971 	for (sc21 = 0; sc21 < NSC; sc21++) {
972 		um = upminfo[sc21];
973 		if (um == 0 || um->um_alive == 0)
974 			continue;
975 		sc = &up_softc[sc21];
976 		if (um->um_tab.b_active == 0) {
977 			for (unit = 0; unit < NUP; unit++)
978 				if (uputab[unit].b_active &&
979 				    updinfo[unit]->ui_mi == um)
980 					goto active;
981 			sc->sc_wticks = 0;
982 			continue;
983 		}
984 active:
985 		sc->sc_wticks++;
986 		if (sc->sc_wticks >= 20) {
987 			sc->sc_wticks = 0;
988 			printf("sc%d: lost interrupt\n", sc21);
989 			ubareset(um->um_ubanum);
990 		}
991 	}
992 }
993 
994 #define	DBSIZE	20
995 
996 updump(dev)
997 	dev_t dev;
998 {
999 	struct updevice *upaddr;
1000 	char *start;
1001 	int num, blk, unit;
1002 	struct size *sizes;
1003 	register struct uba_regs *uba;
1004 	register struct uba_device *ui;
1005 	register short *rp;
1006 	struct upst *st;
1007 	register int retry;
1008 
1009 	unit = upunit(dev);
1010 	if (unit >= NUP)
1011 		return (ENXIO);
1012 #define	phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
1013 	ui = phys(struct uba_device *, updinfo[unit]);
1014 	if (ui->ui_alive == 0)
1015 		return (ENXIO);
1016 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
1017 	ubainit(uba);
1018 	upaddr = (struct updevice *)ui->ui_physaddr;
1019 	DELAY(5000000);
1020 	num = maxfree;
1021 	upaddr->upcs2 = unit;
1022 	DELAY(100);
1023 	upaddr->upcs1 = UP_DCLR|UP_GO;
1024 	upaddr->upcs1 = UP_PRESET|UP_GO;
1025 	upaddr->upof = UPOF_FMT22;
1026 	retry = 0;
1027 	do {
1028 		DELAY(25);
1029 		if (++retry > 527)
1030 			break;
1031 	} while ((upaddr->upds & UP_RDY) == 0);
1032 	if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY)
1033 		return (EFAULT);
1034 	start = 0;
1035 	st = &upst[ui->ui_type];
1036 	sizes = phys(struct size *, st->sizes);
1037 	if (dumplo < 0)
1038 		return (EINVAL);
1039 	if (dumplo + num >= sizes[minor(dev)&07].nblocks)
1040 		num = sizes[minor(dev)&07].nblocks - dumplo;
1041 	while (num > 0) {
1042 		register struct pte *io;
1043 		register int i;
1044 		int cn, sn, tn;
1045 		daddr_t bn;
1046 
1047 		blk = num > DBSIZE ? DBSIZE : num;
1048 		io = uba->uba_map;
1049 		for (i = 0; i < blk; i++)
1050 			*(int *)io++ = (btop(start)+i) | (1<<21) | UBAMR_MRV;
1051 		*(int *)io = 0;
1052 		bn = dumplo + btop(start);
1053 		cn = bn/st->nspc + sizes[minor(dev)&07].cyloff;
1054 		sn = bn%st->nspc;
1055 		tn = sn/st->nsect;
1056 		sn = sn%st->nsect;
1057 		upaddr->updc = cn;
1058 		rp = (short *) &upaddr->upda;
1059 		*rp = (tn << 8) + sn;
1060 		*--rp = 0;
1061 		*--rp = -blk*NBPG / sizeof (short);
1062 		*--rp = UP_GO|UP_WCOM;
1063 		retry = 0;
1064 		do {
1065 			DELAY(25);
1066 			if (++retry > 527)
1067 				break;
1068 		} while ((upaddr->upcs1 & UP_RDY) == 0);
1069 		if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
1070 			printf("up%d: not ready", unit);
1071 			if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
1072 				printf("\n");
1073 				return (EIO);
1074 			}
1075 			printf(" (flakey)\n");
1076 		}
1077 		if (upaddr->upds&UPDS_ERR)
1078 			return (EIO);
1079 		start += blk*NBPG;
1080 		num -= blk;
1081 	}
1082 	return (0);
1083 }
1084 
1085 upsize(dev)
1086 	dev_t dev;
1087 {
1088 	int unit = upunit(dev);
1089 	struct uba_device *ui;
1090 	struct upst *st;
1091 
1092 	if (unit >= NUP || (ui = updinfo[unit]) == 0 || ui->ui_alive == 0)
1093 		return (-1);
1094 	st = &upst[ui->ui_type];
1095 	return (st->sizes[minor(dev) & 07].nblocks);
1096 }
1097 #endif
1098