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