xref: /csrg-svn/sys/vax/uba/up.c (revision 10871)
1 /*	up.c	4.67	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 			 */
548 	hard:
549 			harderr(bp, "up");
550 			printf("cn=%d tn=%d sn=%d cs2=%b er1=%b er2=%b\n",
551 			        upaddr->updc, ((upaddr->upda)>>8)&077,
552 			        (upaddr->upda)&037,
553 				upaddr->upcs2, UPCS2_BITS,
554 				upaddr->uper1, UPER1_BITS,
555 				upaddr->uper2, UPER2_BITS);
556 			bp->b_flags |= B_ERROR;
557 		} else if (upaddr->uper2 & UPER2_BSE) {
558 			if (upecc(ui, BSE))
559 				return;
560 			else
561 				goto hard;
562 		} else {
563 			/*
564 			 * Retriable error.
565 			 * If a soft ecc, correct it (continuing
566 			 * by returning if necessary.
567 			 * Otherwise fall through and retry the transfer
568 			 */
569 			if ((upaddr->uper1&(UPER1_DCK|UPER1_ECH))==UPER1_DCK) {
570 				if (upecc(ui, ECC))
571 					return;
572 			} else
573 				um->um_tab.b_active = 0; /* force retry */
574 		}
575 		/*
576 		 * Clear drive error and, every eight attempts,
577 		 * (starting with the fourth)
578 		 * recalibrate to clear the slate.
579 		 */
580 		upaddr->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
581 		needie = 0;
582 		if ((um->um_tab.b_errcnt&07) == 4 && um->um_tab.b_active == 0) {
583 			upaddr->upcs1 = UP_RECAL|UP_IE|UP_GO;
584 			sc->sc_recal = 0;
585 			goto nextrecal;
586 		}
587 	}
588 	/*
589 	 * Advance recalibration finite state machine
590 	 * if recalibrate in progress, through
591 	 *	RECAL
592 	 *	SEEK
593 	 *	OFFSET (optional)
594 	 *	RETRY
595 	 */
596 	switch (sc->sc_recal) {
597 
598 	case 1:
599 		upaddr->updc = bp->b_cylin;
600 		upaddr->upcs1 = UP_SEEK|UP_IE|UP_GO;
601 		goto nextrecal;
602 	case 2:
603 		if (um->um_tab.b_errcnt < 16 || (bp->b_flags&B_READ) == 0)
604 			goto donerecal;
605 		upaddr->upof = up_offset[um->um_tab.b_errcnt & 017] | UPOF_FMT22;
606 		upaddr->upcs1 = UP_IE|UP_OFFSET|UP_GO;
607 		goto nextrecal;
608 	nextrecal:
609 		sc->sc_recal++;
610 		um->um_tab.b_active = 1;
611 		return;
612 	donerecal:
613 	case 3:
614 		sc->sc_recal = 0;
615 		um->um_tab.b_active = 0;
616 		break;
617 	}
618 	/*
619 	 * If still ``active'', then don't need any more retries.
620 	 */
621 	if (um->um_tab.b_active) {
622 		/*
623 		 * If we were offset positioning,
624 		 * return to centerline.
625 		 */
626 		if (um->um_tab.b_errcnt >= 16) {
627 			upaddr->upof = UPOF_FMT22;
628 			upaddr->upcs1 = UP_RTC|UP_GO|UP_IE;
629 			while (upaddr->upds & UPDS_PIP)
630 				DELAY(25);
631 			needie = 0;
632 		}
633 		um->um_tab.b_active = 0;
634 		um->um_tab.b_errcnt = 0;
635 		um->um_tab.b_actf = dp->b_forw;
636 		dp->b_active = 0;
637 		dp->b_errcnt = 0;
638 		dp->b_actf = bp->av_forw;
639 		bp->b_resid = (-upaddr->upwc * sizeof(short));
640 		iodone(bp);
641 		/*
642 		 * If this unit has more work to do,
643 		 * then start it up right away.
644 		 */
645 		if (dp->b_actf)
646 			if (upustart(ui))
647 				needie = 0;
648 	}
649 	as &= ~(1<<ui->ui_slave);
650 	/*
651 	 * Release unibus resources and flush data paths.
652 	 */
653 	ubadone(um);
654 doattn:
655 	/*
656 	 * Process other units which need attention.
657 	 * For each unit which needs attention, call
658 	 * the unit start routine to place the slave
659 	 * on the controller device queue.
660 	 */
661 	while (unit = ffs(as)) {
662 		unit--;		/* was 1 origin */
663 		as &= ~(1<<unit);
664 		upaddr->upas = 1<<unit;
665 		if (unit < UPIPUNITS && upustart(upip[sc21][unit]))
666 			needie = 0;
667 	}
668 	/*
669 	 * If the controller is not transferring, but
670 	 * there are devices ready to transfer, start
671 	 * the controller.
672 	 */
673 	if (um->um_tab.b_actf && um->um_tab.b_active == 0)
674 		if (upstart(um))
675 			needie = 0;
676 	if (needie)
677 		upaddr->upcs1 = UP_IE;
678 }
679 
680 upread(dev, uio)
681 	dev_t dev;
682 	struct uio *uio;
683 {
684 	register int unit = minor(dev) >> 3;
685 
686 	if (unit >= NUP)
687 		return (ENXIO);
688 	return (physio(upstrategy, &rupbuf[unit], dev, B_READ, minphys, uio));
689 }
690 
691 upwrite(dev, uio)
692 	dev_t dev;
693 	struct uio *uio;
694 {
695 	register int unit = minor(dev) >> 3;
696 
697 	if (unit >= NUP)
698 		return (ENXIO);
699 	return (physio(upstrategy, &rupbuf[unit], dev, B_WRITE, minphys, uio));
700 }
701 
702 /*
703  * Correct an ECC error, and restart the i/o to complete
704  * the transfer if necessary.  This is quite complicated because
705  * the transfer may be going to an odd memory address base and/or
706  * across a page boundary.
707  */
708 upecc(ui, flag)
709 	register struct uba_device *ui;
710 	int flag;
711 {
712 	register struct updevice *up = (struct updevice *)ui->ui_addr;
713 	register struct buf *bp = uputab[ui->ui_unit].b_actf;
714 	register struct uba_ctlr *um = ui->ui_mi;
715 	register struct upst *st;
716 	struct uba_regs *ubp = ui->ui_hd->uh_uba;
717 	register int i;
718 	caddr_t addr;
719 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
720 	int bn, cn, tn, sn;
721 
722 	/*
723 	 * Npf is the number of sectors transferred before the sector
724 	 * containing the ECC error, and reg is the UBA register
725 	 * mapping (the first part of) the transfer.
726 	 * O is offset within a memory page of the first byte transferred.
727 	 */
728 	if (flag == CONT)
729 		npf = bp->b_error;
730 	else
731 		npf = btop((up->upwc * sizeof(short)) + bp->b_bcount);
732 	reg = btop(um->um_ubinfo&0x3ffff) + npf;
733 	o = (int)bp->b_un.b_addr & PGOFSET;
734 	mask = up->upec2;
735 #ifdef UPECCDEBUG
736 	printf("npf %d reg %x o %d mask %o pos %d\n", npf, reg, o, mask,
737 	    up->upec1);
738 #endif
739 	bn = dkblock(bp);
740 	st = &upst[ui->ui_type];
741 	cn = bp->b_cylin;
742 	sn = bn%st->nspc + npf;
743 	tn = sn/st->nsect;
744 	sn %= st->nsect;
745 	cn += tn/st->ntrak;
746 	tn %= st->ntrak;
747 	ubapurge(um);
748 	um->um_tab.b_active=2;
749 	/*
750 	 * action taken depends on the flag
751 	 */
752 	switch(flag){
753 	case ECC:
754 		npf--;
755 		reg--;
756 		mask = up->upec2;
757 		printf("up%d%c: soft ecc sn%d\n", dkunit(bp),
758 			'a'+(minor(bp->b_dev)&07), bp->b_blkno + npf);
759 		/*
760 		 * Flush the buffered data path, and compute the
761 		 * byte and bit position of the error.  The variable i
762 		 * is the byte offset in the transfer, the variable byte
763 		 * is the offset from a page boundary in main memory.
764 		 */
765 		i = up->upec1 - 1;		/* -1 makes 0 origin */
766 		bit = i&07;
767 		i = (i&~07)>>3;
768 		byte = i + o;
769 		/*
770 		 * Correct while possible bits remain of mask.  Since mask
771 		 * contains 11 bits, we continue while the bit offset is > -11.
772 		 * Also watch out for end of this block and the end of the whole
773 		 * transfer.
774 		 */
775 		while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
776 			addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
777 				(byte & PGOFSET);
778 #ifdef UPECCDEBUG
779 			printf("addr %x map reg %x\n",
780 				addr, *(int *)(&ubp->uba_map[reg+btop(byte)]));
781 			printf("old: %x, ", getmemc(addr));
782 #endif
783 			putmemc(addr, getmemc(addr)^(mask<<bit));
784 #ifdef UPECCDEBUG
785 			printf("new: %x\n", getmemc(addr));
786 #endif
787 			byte++;
788 			i++;
789 			bit -= 8;
790 		}
791 		if (up->upwc == 0)
792 			return (0);
793 		npf++;
794 		reg++;
795 		break;
796 	case BSE:
797 		/*
798 		 * if not in bad sector table, return 0
799 		 */
800 		if ((bn = isbad(&upbad[ui->ui_unit], cn, tn, sn)) < 0)
801 			return(0);
802 		/*
803 		 * flag this one as bad
804 		 */
805 		bp->b_flags |= B_BAD;
806 		bp->b_error = npf + 1;
807 #ifdef UPECCDEBUG
808 		printf("BSE: restart at %d\n",npf+1);
809 #endif
810 		bn = st->ncyl * st->nspc -st->nsect - 1 - bn;
811 		cn = bn / st->nspc;
812 		sn = bn % st->nspc;
813 		tn = sn / st->nsect;
814 		sn %= st->nsect;
815 		up->upwc = -(512 / sizeof (short));
816 #ifdef UPECCDEBUG
817 		printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
818 #endif
819 		break;
820 	case CONT:
821 #ifdef UPECCDEBUG
822 		printf("upecc, CONT: bn %d cn %d tn %d sn %d\n", bn, cn, tn, sn);
823 #endif
824 		bp->b_flags &= ~B_BAD;
825 		up->upwc = -((bp->b_bcount - (int)ptob(npf)) / sizeof(short));
826 		if (up->upwc == 0)
827 			return(0);
828 		break;
829 	}
830 	if (up->upwc == 0) {
831 		um->um_tab.b_active = 0;
832 		return (0);
833 	}
834 	/*
835 	 * Have to continue the transfer... clear the drive,
836 	 * and compute the position where the transfer is to continue.
837 	 * We have completed npf+1 sectors of the transfer already;
838 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
839 	 */
840 #ifdef notdef
841 	up->uper1 = 0;
842 	up->upcs1 |= UP_GO;
843 #else
844 	up->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
845 	up->updc = cn;
846 	up->upda = (tn << 8) | sn;
847 	ubaddr = (int)ptob(reg) + o;
848 	up->upba = ubaddr;
849 	cmd = (ubaddr >> 8) & 0x300;
850 	cmd |= ((bp->b_flags&B_READ)?UP_RCOM:UP_WCOM)|UP_IE|UP_GO;
851 	um->um_tab.b_errcnt = 0;
852 	up->upcs1 = cmd;
853 #endif
854 	return (1);
855 }
856 
857 /*
858  * Reset driver after UBA init.
859  * Cancel software state of all pending transfers
860  * and restart all units and the controller.
861  */
862 upreset(uban)
863 	int uban;
864 {
865 	register struct uba_ctlr *um;
866 	register struct uba_device *ui;
867 	register sc21, unit;
868 
869 	for (sc21 = 0; sc21 < NSC; sc21++) {
870 		if ((um = upminfo[sc21]) == 0 || um->um_ubanum != uban ||
871 		    um->um_alive == 0)
872 			continue;
873 		printf(" sc%d", sc21);
874 		um->um_tab.b_active = 0;
875 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
876 		up_softc[sc21].sc_recal = 0;
877 		up_softc[sc21].sc_wticks = 0;
878 		if (um->um_ubinfo) {
879 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
880 			um->um_ubinfo = 0;
881 		}
882 		((struct updevice *)(um->um_addr))->upcs2 = UPCS2_CLR;
883 		for (unit = 0; unit < NUP; unit++) {
884 			if ((ui = updinfo[unit]) == 0)
885 				continue;
886 			if (ui->ui_alive == 0 || ui->ui_mi != um)
887 				continue;
888 			uputab[unit].b_active = 0;
889 			(void) upustart(ui);
890 		}
891 		(void) upstart(um);
892 	}
893 }
894 
895 /*
896  * Wake up every second and if an interrupt is pending
897  * but nothing has happened increment a counter.
898  * If nothing happens for 20 seconds, reset the UNIBUS
899  * and begin anew.
900  */
901 upwatch()
902 {
903 	register struct uba_ctlr *um;
904 	register sc21, unit;
905 	register struct up_softc *sc;
906 
907 	timeout(upwatch, (caddr_t)0, hz);
908 	for (sc21 = 0; sc21 < NSC; sc21++) {
909 		um = upminfo[sc21];
910 		if (um == 0 || um->um_alive == 0)
911 			continue;
912 		sc = &up_softc[sc21];
913 		if (um->um_tab.b_active == 0) {
914 			for (unit = 0; unit < NUP; unit++)
915 				if (uputab[unit].b_active &&
916 				    updinfo[unit]->ui_mi == um)
917 					goto active;
918 			sc->sc_wticks = 0;
919 			continue;
920 		}
921 active:
922 		sc->sc_wticks++;
923 		if (sc->sc_wticks >= 20) {
924 			sc->sc_wticks = 0;
925 			printf("sc%d: lost interrupt\n", sc21);
926 			ubareset(um->um_ubanum);
927 		}
928 	}
929 }
930 
931 #define	DBSIZE	20
932 
933 updump(dev)
934 	dev_t dev;
935 {
936 	struct updevice *upaddr;
937 	char *start;
938 	int num, blk, unit;
939 	struct size *sizes;
940 	register struct uba_regs *uba;
941 	register struct uba_device *ui;
942 	register short *rp;
943 	struct upst *st;
944 	register int retry;
945 
946 	unit = minor(dev) >> 3;
947 	if (unit >= NUP)
948 		return (ENXIO);
949 #define	phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
950 	ui = phys(struct uba_device *, updinfo[unit]);
951 	if (ui->ui_alive == 0)
952 		return (ENXIO);
953 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
954 	ubainit(uba);
955 	upaddr = (struct updevice *)ui->ui_physaddr;
956 	DELAY(5000000);
957 	num = maxfree;
958 	upaddr->upcs2 = unit;
959 	DELAY(100);
960 	upaddr->upcs1 = UP_DCLR|UP_GO;
961 	upaddr->upcs1 = UP_PRESET|UP_GO;
962 	upaddr->upof = UPOF_FMT22;
963 	retry = 0;
964 	do {
965 		DELAY(25);
966 		if (++retry > 527)
967 			break;
968 	} while ((upaddr->upds & UP_RDY) == 0);
969 	if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY)
970 		return (EFAULT);
971 	start = 0;
972 	st = &upst[ui->ui_type];
973 	sizes = phys(struct size *, st->sizes);
974 	if (dumplo < 0 || dumplo + num >= sizes[minor(dev)&07].nblocks)
975 		return (EINVAL);
976 	while (num > 0) {
977 		register struct pte *io;
978 		register int i;
979 		int cn, sn, tn;
980 		daddr_t bn;
981 
982 		blk = num > DBSIZE ? DBSIZE : num;
983 		io = uba->uba_map;
984 		for (i = 0; i < blk; i++)
985 			*(int *)io++ = (btop(start)+i) | (1<<21) | UBAMR_MRV;
986 		*(int *)io = 0;
987 		bn = dumplo + btop(start);
988 		cn = bn/st->nspc + sizes[minor(dev)&07].cyloff;
989 		sn = bn%st->nspc;
990 		tn = sn/st->nsect;
991 		sn = sn%st->nsect;
992 		upaddr->updc = cn;
993 		rp = (short *) &upaddr->upda;
994 		*rp = (tn << 8) + sn;
995 		*--rp = 0;
996 		*--rp = -blk*NBPG / sizeof (short);
997 		*--rp = UP_GO|UP_WCOM;
998 		retry = 0;
999 		do {
1000 			DELAY(25);
1001 			if (++retry > 527)
1002 				break;
1003 		} while ((upaddr->upcs1 & UP_RDY) == 0);
1004 		if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
1005 			printf("up%d: not ready", unit);
1006 			if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
1007 				printf("\n");
1008 				return (EIO);
1009 			}
1010 			printf(" (flakey)\n");
1011 		}
1012 		if (upaddr->upds&UPDS_ERR)
1013 			return (EIO);
1014 		start += blk*NBPG;
1015 		num -= blk;
1016 	}
1017 	return (0);
1018 }
1019 #endif
1020