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