xref: /csrg-svn/sys/vax/uba/up.c (revision 2889)
1*2889Swnj /*	up.c	4.28	81/03/03	*/
2264Sbill 
31937Swnj #include "up.h"
42646Swnj #if NSC > 0
5264Sbill /*
6885Sbill  * UNIBUS disk driver with overlapped seeks and ECC recovery.
7*2889Swnj  *
8*2889Swnj  * TODO:
9*2889Swnj  *	Check out handling of spun-down drives and write lock
10*2889Swnj  *	Add reading of bad sector information and disk layout from sector 1
11*2889Swnj  *	Add bad sector forwarding code
12*2889Swnj  *	Check multiple drive handling
13*2889Swnj  *	Check dump code
14*2889Swnj  *	Check unibus reset code
15264Sbill  */
161756Sbill #define	DELAY(N)		{ register int d; d = N; while (--d > 0); }
17264Sbill 
18264Sbill #include "../h/param.h"
19264Sbill #include "../h/systm.h"
202395Swnj #include "../h/cpu.h"
212395Swnj #include "../h/nexus.h"
22308Sbill #include "../h/dk.h"
23264Sbill #include "../h/buf.h"
24264Sbill #include "../h/conf.h"
25264Sbill #include "../h/dir.h"
26264Sbill #include "../h/user.h"
27264Sbill #include "../h/map.h"
28420Sbill #include "../h/pte.h"
29264Sbill #include "../h/mtpr.h"
302571Swnj #include "../h/vm.h"
31264Sbill #include "../h/uba.h"
322379Swnj #include "../h/cmap.h"
33264Sbill 
342379Swnj #include "../h/upreg.h"
35264Sbill 
362395Swnj struct	up_softc {
372395Swnj 	int	sc_softas;
382607Swnj 	int	sc_ndrive;
392395Swnj 	int	sc_wticks;
402674Swnj 	int	sc_recal;
412646Swnj } up_softc[NSC];
42275Sbill 
432395Swnj /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
44264Sbill struct	size
45264Sbill {
46264Sbill 	daddr_t	nblocks;
47264Sbill 	int	cyloff;
48264Sbill } up_sizes[8] = {
49264Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
50264Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
51341Sbill 	495520,	0,		/* C=cyl 0 thru 814 */
52264Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
53264Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
54264Sbill 	81472,	681,		/* F=cyl 681 thru 814 */
55264Sbill 	153824,	562,		/* G=cyl 562 thru 814 */
56264Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
572395Swnj }, fj_sizes[8] = {
582395Swnj 	15884,	0,		/* A=cyl 0 thru 49 */
592395Swnj 	33440,	50,		/* B=cyl 50 thru 154 */
602395Swnj 	263360,	0,		/* C=cyl 0 thru 822 */
612395Swnj 	0,	0,
622395Swnj 	0,	0,
632395Swnj 	0,	0,
642395Swnj 	0,	0,
652395Swnj 	213760,	155,		/* H=cyl 155 thru 822 */
66264Sbill };
672395Swnj /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
68264Sbill 
692395Swnj #define	_upSDIST	2		/* 1.0 msec */
702395Swnj #define	_upRDIST	4		/* 2.0 msec */
71264Sbill 
722395Swnj int	upSDIST = _upSDIST;
732395Swnj int	upRDIST = _upRDIST;
742395Swnj 
752607Swnj int	upprobe(), upslave(), upattach(), updgo(), upintr();
762646Swnj struct	uba_minfo *upminfo[NSC];
772395Swnj struct	uba_dinfo *updinfo[NUP];
782646Swnj struct	uba_dinfo *upip[NSC][4];
792395Swnj 
802607Swnj u_short	upstd[] = { 0776700, 0774400, 0776300, 0 };
812616Swnj struct	uba_driver scdriver =
822607Swnj     { upprobe, upslave, upattach, updgo, upstd, "up", updinfo, "sc", upminfo };
832395Swnj struct	buf	uputab[NUP];
842395Swnj 
852395Swnj struct	upst {
862395Swnj 	short	nsect;
872395Swnj 	short	ntrak;
882395Swnj 	short	nspc;
892395Swnj 	short	ncyl;
902395Swnj 	struct	size *sizes;
912395Swnj } upst[] = {
922607Swnj 	32,	19,	32*19,	823,	up_sizes,	/* 9300/cdc */
932607Swnj /* 9300 actually has 815 cylinders... */
942395Swnj 	32,	10,	32*10,	823,	fj_sizes,	/* fujitsu 160m */
952395Swnj };
962395Swnj 
972629Swnj u_char	up_offset[16] = {
982629Swnj     UP_P400, UP_M400, UP_P400, UP_M400, UP_P800, UP_M800, UP_P800, UP_M800,
992629Swnj     UP_P1200, UP_M1200, UP_P1200, UP_M1200, 0, 0, 0, 0
1002629Swnj };
101264Sbill 
1022616Swnj struct	buf	rupbuf[NUP];
103264Sbill 
104264Sbill #define	b_cylin b_resid
105264Sbill 
106264Sbill #ifdef INTRLVE
107264Sbill daddr_t dkblock();
108264Sbill #endif
1092395Swnj 
1102395Swnj int	upwstart, upwatch();		/* Have started guardian */
1112470Swnj int	upseek;
1122681Swnj int	upwaitdry;
1132395Swnj 
1142395Swnj /*ARGSUSED*/
1152607Swnj upprobe(reg)
1162395Swnj 	caddr_t reg;
1172395Swnj {
1182459Swnj 	register int br, cvec;
1192459Swnj 
1202607Swnj #ifdef lint
1212607Swnj 	br = 0; cvec = br; br = cvec;
1222607Swnj #endif
1232629Swnj 	((struct updevice *)reg)->upcs1 = UP_IE|UP_RDY;
1242607Swnj 	DELAY(10);
1252629Swnj 	((struct updevice *)reg)->upcs1 = 0;
1262459Swnj 	return (1);
1272395Swnj }
1282395Swnj 
1292607Swnj upslave(ui, reg)
1302395Swnj 	struct uba_dinfo *ui;
1312395Swnj 	caddr_t reg;
1322395Swnj {
1332629Swnj 	register struct updevice *upaddr = (struct updevice *)reg;
1342395Swnj 
1352395Swnj 	upaddr->upcs1 = 0;		/* conservative */
1362607Swnj 	upaddr->upcs2 = ui->ui_slave;
1372629Swnj 	if (upaddr->upcs2&UP_NED) {
1382629Swnj 		upaddr->upcs1 = UP_DCLR|UP_GO;
1392395Swnj 		return (0);
1402395Swnj 	}
1412607Swnj 	return (1);
1422607Swnj }
1432607Swnj 
1442607Swnj upattach(ui)
1452607Swnj 	register struct uba_dinfo *ui;
1462607Swnj {
1472629Swnj #ifdef notdef
1482629Swnj 	register struct updevice *upaddr;
1492629Swnj #endif
1502607Swnj 
1512395Swnj 	if (upwstart == 0) {
1522759Swnj 		timeout(upwatch, (caddr_t)0, hz);
1532395Swnj 		upwstart++;
1542395Swnj 	}
1552571Swnj 	if (ui->ui_dk >= 0)
1562571Swnj 		dk_mspw[ui->ui_dk] = .0000020345;
1572607Swnj 	upip[ui->ui_ctlr][ui->ui_slave] = ui;
1582607Swnj 	up_softc[ui->ui_ctlr].sc_ndrive++;
1592629Swnj #ifdef notdef
1602629Swnj 	upaddr = (struct updevice *)ui->ui_addr;
1612629Swnj 	upaddr->upcs1 = 0;
1622629Swnj 	upaddr->upcs2 = ui->ui_slave;
1632629Swnj 	upaddr->uphr = -1;
1642629Swnj 	/* ... */
1652629Swnj 	if (upaddr-> ... == 10)
1662629Swnj 		ui->ui_type = 1;
1672629Swnj #endif
1682395Swnj }
169264Sbill 
170264Sbill upstrategy(bp)
1712395Swnj 	register struct buf *bp;
172264Sbill {
1732395Swnj 	register struct uba_dinfo *ui;
1742395Swnj 	register struct upst *st;
1752395Swnj 	register int unit;
1762470Swnj 	register struct buf *dp;
1772395Swnj 	int xunit = minor(bp->b_dev) & 07;
1782470Swnj 	long bn, sz;
179264Sbill 
1802470Swnj 	sz = (bp->b_bcount+511) >> 9;
181264Sbill 	unit = dkunit(bp);
1822395Swnj 	if (unit >= NUP)
1832395Swnj 		goto bad;
1842395Swnj 	ui = updinfo[unit];
1852395Swnj 	if (ui == 0 || ui->ui_alive == 0)
1862395Swnj 		goto bad;
1872395Swnj 	st = &upst[ui->ui_type];
1882395Swnj 	if (bp->b_blkno < 0 ||
1892395Swnj 	    (bn = dkblock(bp))+sz > st->sizes[xunit].nblocks)
1902395Swnj 		goto bad;
1912395Swnj 	bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff;
192264Sbill 	(void) spl5();
1932470Swnj 	dp = &uputab[ui->ui_unit];
1942470Swnj 	disksort(dp, bp);
1952470Swnj 	if (dp->b_active == 0) {
1962395Swnj 		(void) upustart(ui);
1972395Swnj 		bp = &ui->ui_mi->um_tab;
1982395Swnj 		if (bp->b_actf && bp->b_active == 0)
1992395Swnj 			(void) upstart(ui->ui_mi);
200264Sbill 	}
201264Sbill 	(void) spl0();
2022395Swnj 	return;
2032395Swnj 
2042395Swnj bad:
2052395Swnj 	bp->b_flags |= B_ERROR;
2062395Swnj 	iodone(bp);
2072395Swnj 	return;
208264Sbill }
209264Sbill 
2102674Swnj /*
2112674Swnj  * Unit start routine.
2122674Swnj  * Seek the drive to be where the data is
2132674Swnj  * and then generate another interrupt
2142674Swnj  * to actually start the transfer.
2152674Swnj  * If there is only one drive on the controller,
2162674Swnj  * or we are very close to the data, don't
2172674Swnj  * bother with the search.  If called after
2182674Swnj  * searching once, don't bother to look where
2192674Swnj  * we are, just queue for transfer (to avoid
2202674Swnj  * positioning forever without transferrring.)
2212674Swnj  */
2222395Swnj upustart(ui)
2232395Swnj 	register struct uba_dinfo *ui;
224264Sbill {
225264Sbill 	register struct buf *bp, *dp;
2262674Swnj 	register struct uba_minfo *um = ui->ui_mi;
2272629Swnj 	register struct updevice *upaddr;
2282395Swnj 	register struct upst *st;
229264Sbill 	daddr_t bn;
2302674Swnj 	int sn, csn;
2312607Swnj 	/*
2322607Swnj 	 * The SC21 cancels commands if you just say
2332629Swnj 	 *	cs1 = UP_IE
2342607Swnj 	 * so we are cautious about handling of cs1.
2352607Swnj 	 * Also don't bother to clear as bits other than in upintr().
2362607Swnj 	 */
2372674Swnj 	int didie = 0;
2382674Swnj 
2392674Swnj 	if (ui == 0)
2402674Swnj 		return (0);
2412395Swnj 	dk_busy &= ~(1<<ui->ui_dk);
2422395Swnj 	dp = &uputab[ui->ui_unit];
243266Sbill 	if ((bp = dp->b_actf) == NULL)
244268Sbill 		goto out;
2452674Swnj 	/*
2462674Swnj 	 * If the controller is active, just remember
2472674Swnj 	 * that this device would like to be positioned...
2482674Swnj 	 * if we tried to position now we would confuse the SC21.
2492674Swnj 	 */
2502395Swnj 	if (um->um_tab.b_active) {
2512459Swnj 		up_softc[um->um_ctlr].sc_softas |= 1<<ui->ui_slave;
252275Sbill 		return (0);
253275Sbill 	}
2542674Swnj 	/*
2552674Swnj 	 * If we have already positioned this drive,
2562674Swnj 	 * then just put it on the ready queue.
2572674Swnj 	 */
258276Sbill 	if (dp->b_active)
259276Sbill 		goto done;
260276Sbill 	dp->b_active = 1;
2612629Swnj 	upaddr = (struct updevice *)um->um_addr;
2622395Swnj 	upaddr->upcs2 = ui->ui_slave;
2632674Swnj 	/*
2642674Swnj 	 * If drive has just come up,
2652674Swnj 	 * setup the pack.
2662674Swnj 	 */
2672629Swnj 	if ((upaddr->upds & UP_VV) == 0) {
2682607Swnj 		/* SHOULD WARN SYSTEM THAT THIS HAPPENED */
2692629Swnj 		upaddr->upcs1 = UP_IE|UP_DCLR|UP_GO;
2702629Swnj 		upaddr->upcs1 = UP_IE|UP_PRESET|UP_GO;
2712629Swnj 		upaddr->upof = UP_FMT22;
272268Sbill 		didie = 1;
273264Sbill 	}
2742674Swnj 	/*
2752674Swnj 	 * If drive is offline, forget about positioning.
2762674Swnj 	 */
2772629Swnj 	if ((upaddr->upds & (UP_DPR|UP_MOL)) != (UP_DPR|UP_MOL))
278275Sbill 		goto done;
2792674Swnj 	/*
2802674Swnj 	 * If there is only one drive,
2812674Swnj 	 * dont bother searching.
2822674Swnj 	 */
2832607Swnj 	if (up_softc[um->um_ctlr].sc_ndrive == 1)
2842607Swnj 		goto done;
2852674Swnj 	/*
2862674Swnj 	 * Figure out where this transfer is going to
2872674Swnj 	 * and see if we are close enough to justify not searching.
2882674Swnj 	 */
2892395Swnj 	st = &upst[ui->ui_type];
290264Sbill 	bn = dkblock(bp);
2912395Swnj 	sn = bn%st->nspc;
2922395Swnj 	sn = (sn + st->nsect - upSDIST) % st->nsect;
2932674Swnj 	if (bp->b_cylin - upaddr->updc)
294266Sbill 		goto search;		/* Not on-cylinder */
295275Sbill 	else if (upseek)
296275Sbill 		goto done;		/* Ok just to be on-cylinder */
297264Sbill 	csn = (upaddr->upla>>6) - sn - 1;
298266Sbill 	if (csn < 0)
2992395Swnj 		csn += st->nsect;
3002395Swnj 	if (csn > st->nsect - upRDIST)
301264Sbill 		goto done;
302264Sbill search:
3032674Swnj 	upaddr->updc = bp->b_cylin;
3042674Swnj 	/*
3052674Swnj 	 * Not on cylinder at correct position,
3062674Swnj 	 * seek/search.
3072674Swnj 	 */
308275Sbill 	if (upseek)
3092629Swnj 		upaddr->upcs1 = UP_IE|UP_SEEK|UP_GO;
3102470Swnj 	else {
311275Sbill 		upaddr->upda = sn;
3122629Swnj 		upaddr->upcs1 = UP_IE|UP_SEARCH|UP_GO;
313275Sbill 	}
314268Sbill 	didie = 1;
3152674Swnj 	/*
3162674Swnj 	 * Mark unit busy for iostat.
3172674Swnj 	 */
3182395Swnj 	if (ui->ui_dk >= 0) {
3192395Swnj 		dk_busy |= 1<<ui->ui_dk;
3202395Swnj 		dk_seek[ui->ui_dk]++;
321264Sbill 	}
322268Sbill 	goto out;
323264Sbill done:
3242674Swnj 	/*
3252674Swnj 	 * Device is ready to go.
3262674Swnj 	 * Put it on the ready queue for the controller
3272674Swnj 	 * (unless its already there.)
3282674Swnj 	 */
3292629Swnj 	if (dp->b_active != 2) {
3302629Swnj 		dp->b_forw = NULL;
3312629Swnj 		if (um->um_tab.b_actf == NULL)
3322629Swnj 			um->um_tab.b_actf = dp;
3332629Swnj 		else
3342629Swnj 			um->um_tab.b_actl->b_forw = dp;
3352629Swnj 		um->um_tab.b_actl = dp;
3362629Swnj 		dp->b_active = 2;
3372629Swnj 	}
338268Sbill out:
339268Sbill 	return (didie);
340264Sbill }
341264Sbill 
3422674Swnj /*
3432674Swnj  * Start up a transfer on a drive.
3442674Swnj  */
3452395Swnj upstart(um)
3462395Swnj 	register struct uba_minfo *um;
347264Sbill {
348264Sbill 	register struct buf *bp, *dp;
3492395Swnj 	register struct uba_dinfo *ui;
3502629Swnj 	register struct updevice *upaddr;
3512470Swnj 	struct upst *st;
352264Sbill 	daddr_t bn;
3532681Swnj 	int dn, sn, tn, cmd, waitdry;
354264Sbill 
355264Sbill loop:
3562674Swnj 	/*
3572674Swnj 	 * Pull a request off the controller queue
3582674Swnj 	 */
3592395Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
360268Sbill 		return (0);
361264Sbill 	if ((bp = dp->b_actf) == NULL) {
3622395Swnj 		um->um_tab.b_actf = dp->b_forw;
363264Sbill 		goto loop;
364264Sbill 	}
3652674Swnj 	/*
3662674Swnj 	 * Mark controller busy, and
3672674Swnj 	 * determine destination of this request.
3682674Swnj 	 */
3692395Swnj 	um->um_tab.b_active++;
3702395Swnj 	ui = updinfo[dkunit(bp)];
371264Sbill 	bn = dkblock(bp);
3722395Swnj 	dn = ui->ui_slave;
3732395Swnj 	st = &upst[ui->ui_type];
3742395Swnj 	sn = bn%st->nspc;
3752395Swnj 	tn = sn/st->nsect;
3762395Swnj 	sn %= st->nsect;
3772629Swnj 	upaddr = (struct updevice *)ui->ui_addr;
3782674Swnj 	/*
3792674Swnj 	 * Select drive if not selected already.
3802674Swnj 	 */
3812674Swnj 	if ((upaddr->upcs2&07) != dn)
3822674Swnj 		upaddr->upcs2 = dn;
3832674Swnj 	/*
3842674Swnj 	 * Check that it is ready and online
3852674Swnj 	 */
3862681Swnj 	waitdry = 0;
3872681Swnj 	while ((upaddr->upds&UP_DRY) == 0) {
3882681Swnj 		if (++waitdry > 512)
3892681Swnj 			break;
3902681Swnj 		upwaitdry++;
3912681Swnj 	}
3922681Swnj 	if ((upaddr->upds & UP_DREADY) != UP_DREADY) {
3932607Swnj 		printf("up%d not ready", dkunit(bp));
3942681Swnj 		if ((upaddr->upds & UP_DREADY) != UP_DREADY) {
3952607Swnj 			printf("\n");
3962395Swnj 			um->um_tab.b_active = 0;
3972395Swnj 			um->um_tab.b_errcnt = 0;
398893Sbill 			dp->b_actf = bp->av_forw;
399893Sbill 			dp->b_active = 0;
400893Sbill 			bp->b_flags |= B_ERROR;
401893Sbill 			iodone(bp);
402893Sbill 			goto loop;
403893Sbill 		}
4042674Swnj 		/*
4052674Swnj 		 * Oh, well, sometimes this
4062674Swnj 		 * happens, for reasons unknown.
4072674Swnj 		 */
4082629Swnj 		printf(" (flakey)\n");
409264Sbill 	}
4102674Swnj 	/*
4112674Swnj 	 * After 16th retry, do offset positioning
4122674Swnj 	 */
4132395Swnj 	if (um->um_tab.b_errcnt >= 16 && (bp->b_flags&B_READ) != 0) {
4142629Swnj 		upaddr->upof = up_offset[um->um_tab.b_errcnt & 017] | UP_FMT22;
4152629Swnj 		upaddr->upcs1 = UP_IE|UP_OFFSET|UP_GO;
4162629Swnj 		while (upaddr->upds & UP_PIP)
417264Sbill 			DELAY(25);
418264Sbill 	}
4192674Swnj 	/*
4202674Swnj 	 * Setup for the transfer, and get in the
4212674Swnj 	 * UNIBUS adaptor queue.
4222674Swnj 	 */
4232424Skre 	upaddr->updc = bp->b_cylin;
424264Sbill 	upaddr->upda = (tn << 8) + sn;
425264Sbill 	upaddr->upwc = -bp->b_bcount / sizeof (short);
426264Sbill 	if (bp->b_flags & B_READ)
4272629Swnj 		cmd = UP_IE|UP_RCOM|UP_GO;
428264Sbill 	else
4292629Swnj 		cmd = UP_IE|UP_WCOM|UP_GO;
4302571Swnj 	um->um_cmd = cmd;
4312571Swnj 	ubago(ui);
432268Sbill 	return (1);
433264Sbill }
434264Sbill 
4352674Swnj /*
4362674Swnj  * Now all ready to go, stuff the registers.
4372674Swnj  */
4382571Swnj updgo(um)
4392571Swnj 	struct uba_minfo *um;
4402395Swnj {
4412629Swnj 	register struct updevice *upaddr = (struct updevice *)um->um_addr;
4422470Swnj 
4432571Swnj 	upaddr->upba = um->um_ubinfo;
4442571Swnj 	upaddr->upcs1 = um->um_cmd|((um->um_ubinfo>>8)&0x300);
4452395Swnj }
4462395Swnj 
4472674Swnj /*
4482674Swnj  * Handle a disk interrupt.
4492674Swnj  */
4502707Swnj upintr(sc21)
4512395Swnj 	register sc21;
452264Sbill {
453264Sbill 	register struct buf *bp, *dp;
4542395Swnj 	register struct uba_minfo *um = upminfo[sc21];
4552395Swnj 	register struct uba_dinfo *ui;
4562629Swnj 	register struct updevice *upaddr = (struct updevice *)um->um_addr;
457264Sbill 	register unit;
4582470Swnj 	struct up_softc *sc = &up_softc[um->um_ctlr];
4592607Swnj 	int as = (upaddr->upas & 0377) | sc->sc_softas;
4602681Swnj 	int needie = 1, waitdry;
461264Sbill 
4622470Swnj 	sc->sc_wticks = 0;
4632607Swnj 	sc->sc_softas = 0;
4642674Swnj 	/*
4652674Swnj 	 * If controller wasn't transferring, then this is an
4662674Swnj 	 * interrupt for attention status on seeking drives.
4672674Swnj 	 * Just service them.
4682674Swnj 	 */
4692674Swnj 	if (um->um_tab.b_active == 0) {
4702674Swnj 		if (upaddr->upcs1 & UP_TRE)
4712674Swnj 			upaddr->upcs1 = UP_TRE;
4722674Swnj 		goto doattn;
4732674Swnj 	}
4742674Swnj 	if ((upaddr->upcs1 & UP_RDY) == 0)
4752674Swnj 		printf("upintr !RDY\n");		/* shouldn't happen */
4762674Swnj 	/*
4772674Swnj 	 * Get device and block structures, and a pointer
4782674Swnj 	 * to the uba_dinfo for the drive.  Select the drive.
4792674Swnj 	 */
4802674Swnj 	dp = um->um_tab.b_actf;
4812674Swnj 	bp = dp->b_actf;
4822674Swnj 	ui = updinfo[dkunit(bp)];
4832674Swnj 	dk_busy &= ~(1 << ui->ui_dk);
4842674Swnj 	if ((upaddr->upcs2&07) != ui->ui_slave)
4852395Swnj 		upaddr->upcs2 = ui->ui_slave;
4862674Swnj 	/*
4872674Swnj 	 * Check for and process errors on
4882674Swnj 	 * either the drive or the controller.
4892674Swnj 	 */
4902674Swnj 	if ((upaddr->upds&UP_ERR) || (upaddr->upcs1&UP_TRE)) {
4912681Swnj 		waitdry = 0;
4922681Swnj 		while ((upaddr->upds & UP_DRY) == 0) {
4932681Swnj 			if (++waitdry > 512)
4942681Swnj 				break;
4952681Swnj 			upwaitdry++;
4962681Swnj 		}
4972681Swnj 		if ((upaddr->upds&UP_DREADY) != UP_DREADY) {
4982681Swnj 			printf("up%d not ready", dkunit(bp));
4992681Swnj 			bp->b_flags |= B_ERROR;
5002681Swnj 		} else if (upaddr->uper1&UP_WLE) {
5012674Swnj 			/*
5022674Swnj 			 * Give up on write locked devices
5032674Swnj 			 * immediately.
5042674Swnj 			 */
5052674Swnj 			printf("up%d is write locked\n", dkunit(bp));
5062674Swnj 			bp->b_flags |= B_ERROR;
5072674Swnj 		} else if (++um->um_tab.b_errcnt > 27) {
5082674Swnj 			/*
5092674Swnj 			 * After 28 retries (16 without offset, and
5102674Swnj 			 * 12 with offset positioning) give up.
5112674Swnj 			 */
5122674Swnj 			if (upaddr->upcs2&(UP_NEM|UP_MXF)) {
5132674Swnj 				printf("FLAKEY UP ");
5142674Swnj 				ubareset(um->um_ubanum);
5152674Swnj 				return;
5162395Swnj 			}
5172674Swnj 			harderr(bp);
5182785Swnj 			printf("up%d cs2=%b er1=%b er2=%b\n", dkunit(bp),
5192785Swnj 			    upaddr->upcs2, UPCS2_BITS,
5202785Swnj 			    upaddr->uper1, UPER1_BITS,
5212785Swnj 			    upaddr->uper2, UPER2_BITS);
5222674Swnj 			bp->b_flags |= B_ERROR;
5232674Swnj 		} else {
5242674Swnj 			/*
5252674Swnj 			 * Retriable error.
5262674Swnj 			 * If a soft ecc, correct it (continuing
5272674Swnj 			 * by returning if necessary.
5282674Swnj 			 * Otherwise fall through and retry the transfer
5292674Swnj 			 */
5302674Swnj 			um->um_tab.b_active = 0;	 /* force retry */
5312629Swnj 			if ((upaddr->uper1&(UP_DCK|UP_ECH))==UP_DCK)
5322629Swnj 				if (upecc(ui))
5332629Swnj 					return;
5342674Swnj 		}
5352674Swnj 		/*
5362674Swnj 		 * Clear drive error and, every eight attempts,
5372674Swnj 		 * (starting with the fourth)
5382674Swnj 		 * recalibrate to clear the slate.
5392674Swnj 		 */
5402674Swnj 		upaddr->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
5412674Swnj 		needie = 0;
5422674Swnj 		if ((um->um_tab.b_errcnt&07) == 4) {
5432674Swnj 			upaddr->upcs1 = UP_RECAL|UP_IE|UP_GO;
5442674Swnj 			um->um_tab.b_active = 1;
5452674Swnj 			sc->sc_recal = 1;
5462674Swnj 			return;
5472674Swnj 		}
5482674Swnj 	}
5492674Swnj 	/*
5502674Swnj 	 * Done retrying transfer... release
5512674Swnj 	 * resources... if we were recalibrating,
5522674Swnj 	 * then retry the transfer.
5532674Swnj 	 * Mathematical note: 28%8 != 4.
5542674Swnj 	 */
5552674Swnj 	ubadone(um);
5562674Swnj 	if (sc->sc_recal) {
5572674Swnj 		sc->sc_recal = 0;
5582674Swnj 		um->um_tab.b_active = 0;	/* force retry */
5592674Swnj 	}
5602674Swnj 	/*
5612674Swnj 	 * If still ``active'', then don't need any more retries.
5622674Swnj 	 */
5632674Swnj 	if (um->um_tab.b_active) {
5642674Swnj 		/*
5652674Swnj 		 * If we were offset positioning,
5662674Swnj 		 * return to centerline.
5672674Swnj 		 */
5682674Swnj 		if (um->um_tab.b_errcnt >= 16) {
5692674Swnj 			upaddr->upof = UP_FMT22;
5702674Swnj 			upaddr->upcs1 = UP_RTC|UP_GO|UP_IE;
5712674Swnj 			while (upaddr->upds & UP_PIP)
5722674Swnj 				DELAY(25);
573268Sbill 			needie = 0;
574264Sbill 		}
5752674Swnj 		um->um_tab.b_active = 0;
5762674Swnj 		um->um_tab.b_errcnt = 0;
5772674Swnj 		um->um_tab.b_actf = dp->b_forw;
5782674Swnj 		dp->b_active = 0;
5792674Swnj 		dp->b_errcnt = 0;
5802674Swnj 		dp->b_actf = bp->av_forw;
5812674Swnj 		bp->b_resid = (-upaddr->upwc * sizeof(short));
5822674Swnj 		iodone(bp);
5832674Swnj 		/*
5842674Swnj 		 * If this unit has more work to do,
5852674Swnj 		 * then start it up right away.
5862674Swnj 		 */
5872674Swnj 		if (dp->b_actf)
5882674Swnj 			if (upustart(ui))
589268Sbill 				needie = 0;
590264Sbill 	}
5912674Swnj 	as &= ~(1<<ui->ui_slave);
5922674Swnj doattn:
5932674Swnj 	/*
5942674Swnj 	 * Process other units which need attention.
5952674Swnj 	 * For each unit which needs attention, call
5962674Swnj 	 * the unit start routine to place the slave
5972674Swnj 	 * on the controller device queue.
5982674Swnj 	 */
5992607Swnj 	for (unit = 0; as; as >>= 1, unit++)
6002607Swnj 		if (as & 1) {
6012470Swnj 			upaddr->upas = 1<<unit;
6022607Swnj 			if (upustart(upip[sc21][unit]))
603273Sbill 				needie = 0;
604273Sbill 		}
6052674Swnj 	/*
6062674Swnj 	 * If the controller is not transferring, but
6072674Swnj 	 * there are devices ready to transfer, start
6082674Swnj 	 * the controller.
6092674Swnj 	 */
6102395Swnj 	if (um->um_tab.b_actf && um->um_tab.b_active == 0)
6112395Swnj 		if (upstart(um))
612268Sbill 			needie = 0;
613275Sbill 	if (needie)
6142629Swnj 		upaddr->upcs1 = UP_IE;
615264Sbill }
616264Sbill 
617264Sbill upread(dev)
6182616Swnj 	dev_t dev;
619264Sbill {
6202616Swnj 	register int unit = minor(dev) >> 3;
6212470Swnj 
6222616Swnj 	if (unit >= NUP)
6232616Swnj 		u.u_error = ENXIO;
6242616Swnj 	else
6252616Swnj 		physio(upstrategy, &rupbuf[unit], dev, B_READ, minphys);
626264Sbill }
627264Sbill 
628264Sbill upwrite(dev)
6292616Swnj 	dev_t dev;
630264Sbill {
6312616Swnj 	register int unit = minor(dev) >> 3;
6322470Swnj 
6332616Swnj 	if (unit >= NUP)
6342616Swnj 		u.u_error = ENXIO;
6352616Swnj 	else
6362616Swnj 		physio(upstrategy, &rupbuf[unit], dev, B_WRITE, minphys);
637264Sbill }
638264Sbill 
639266Sbill /*
640266Sbill  * Correct an ECC error, and restart the i/o to complete
641266Sbill  * the transfer if necessary.  This is quite complicated because
642266Sbill  * the transfer may be going to an odd memory address base and/or
643266Sbill  * across a page boundary.
644266Sbill  */
6452395Swnj upecc(ui)
6462395Swnj 	register struct uba_dinfo *ui;
647264Sbill {
6482629Swnj 	register struct updevice *up = (struct updevice *)ui->ui_addr;
6492395Swnj 	register struct buf *bp = uputab[ui->ui_unit].b_actf;
6502395Swnj 	register struct uba_minfo *um = ui->ui_mi;
6512395Swnj 	register struct upst *st;
6522395Swnj 	struct uba_regs *ubp = ui->ui_hd->uh_uba;
653266Sbill 	register int i;
654264Sbill 	caddr_t addr;
655266Sbill 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
656264Sbill 	int bn, cn, tn, sn;
657264Sbill 
658264Sbill 	/*
659266Sbill 	 * Npf is the number of sectors transferred before the sector
660266Sbill 	 * containing the ECC error, and reg is the UBA register
661266Sbill 	 * mapping (the first part of) the transfer.
662266Sbill 	 * O is offset within a memory page of the first byte transferred.
663264Sbill 	 */
664266Sbill 	npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
6652571Swnj 	reg = btop(um->um_ubinfo&0x3ffff) + npf;
666264Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
667*2889Swnj 	printf("SOFT ECC up%d%c bn%d\n", dkunit(bp),
668*2889Swnj 	    'a'+(minor(bp->b_dev)&07), bp->b_blkno + npf);
669264Sbill 	mask = up->upec2;
670266Sbill 	/*
671266Sbill 	 * Flush the buffered data path, and compute the
672266Sbill 	 * byte and bit position of the error.  The variable i
673266Sbill 	 * is the byte offset in the transfer, the variable byte
674266Sbill 	 * is the offset from a page boundary in main memory.
675266Sbill 	 */
6762725Swnj 	ubapurge(um);
677266Sbill 	i = up->upec1 - 1;		/* -1 makes 0 origin */
678266Sbill 	bit = i&07;
679266Sbill 	i = (i&~07)>>3;
680264Sbill 	byte = i + o;
681266Sbill 	/*
682266Sbill 	 * Correct while possible bits remain of mask.  Since mask
683266Sbill 	 * contains 11 bits, we continue while the bit offset is > -11.
684266Sbill 	 * Also watch out for end of this block and the end of the whole
685266Sbill 	 * transfer.
686266Sbill 	 */
687266Sbill 	while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
688266Sbill 		addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
689266Sbill 		    (byte & PGOFSET);
690266Sbill 		putmemc(addr, getmemc(addr)^(mask<<bit));
691266Sbill 		byte++;
692266Sbill 		i++;
693266Sbill 		bit -= 8;
694264Sbill 	}
6952395Swnj 	um->um_tab.b_active++;	/* Either complete or continuing... */
696264Sbill 	if (up->upwc == 0)
697264Sbill 		return (0);
698266Sbill 	/*
699266Sbill 	 * Have to continue the transfer... clear the drive,
700266Sbill 	 * and compute the position where the transfer is to continue.
701266Sbill 	 * We have completed npf+1 sectors of the transfer already;
702266Sbill 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
703266Sbill 	 */
7042629Swnj #ifdef notdef
7052629Swnj 	up->uper1 = 0;
7062629Swnj 	up->upcs1 |= UP_GO;
7072629Swnj #else
7082629Swnj 	up->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
709264Sbill 	bn = dkblock(bp);
7102395Swnj 	st = &upst[ui->ui_type];
711264Sbill 	cn = bp->b_cylin;
7122395Swnj 	sn = bn%st->nspc + npf + 1;
7132395Swnj 	tn = sn/st->nsect;
7142395Swnj 	sn %= st->nsect;
7152395Swnj 	cn += tn/st->ntrak;
7162395Swnj 	tn %= st->ntrak;
717264Sbill 	up->updc = cn;
718266Sbill 	up->upda = (tn << 8) | sn;
719266Sbill 	ubaddr = (int)ptob(reg+1) + o;
720266Sbill 	up->upba = ubaddr;
721266Sbill 	cmd = (ubaddr >> 8) & 0x300;
7222629Swnj 	cmd |= UP_IE|UP_GO|UP_RCOM;
723266Sbill 	up->upcs1 = cmd;
7242629Swnj #endif
725264Sbill 	return (1);
726264Sbill }
727286Sbill 
728286Sbill /*
729286Sbill  * Reset driver after UBA init.
730286Sbill  * Cancel software state of all pending transfers
731286Sbill  * and restart all units and the controller.
732286Sbill  */
7332395Swnj upreset(uban)
734286Sbill {
7352395Swnj 	register struct uba_minfo *um;
7362395Swnj 	register struct uba_dinfo *ui;
7372395Swnj 	register sc21, unit;
7382424Skre 	int any = 0;
739286Sbill 
7402646Swnj 	for (sc21 = 0; sc21 < NSC; sc21++) {
7412470Swnj 		if ((um = upminfo[sc21]) == 0 || um->um_ubanum != uban ||
7422470Swnj 		    um->um_alive == 0)
7432395Swnj 			continue;
7442424Skre 		if (any == 0) {
7452424Skre 			printf(" up");
7462470Swnj 			DELAY(10000000);	/* give it time to self-test */
7472424Skre 			any++;
7482424Skre 		}
7492395Swnj 		um->um_tab.b_active = 0;
7502395Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
7512571Swnj 		if (um->um_ubinfo) {
7522571Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
7532616Swnj 			ubadone(um);
7542395Swnj 		}
7552629Swnj 		((struct updevice *)(um->um_addr))->upcs2 = UP_CLR;
7562395Swnj 		for (unit = 0; unit < NUP; unit++) {
7572395Swnj 			if ((ui = updinfo[unit]) == 0)
7582395Swnj 				continue;
7592395Swnj 			if (ui->ui_alive == 0)
7602395Swnj 				continue;
7612395Swnj 			uputab[unit].b_active = 0;
7622395Swnj 			(void) upustart(ui);
7632395Swnj 		}
7642395Swnj 		(void) upstart(um);
765286Sbill 	}
766286Sbill }
767313Sbill 
768313Sbill /*
769313Sbill  * Wake up every second and if an interrupt is pending
770313Sbill  * but nothing has happened increment a counter.
771313Sbill  * If nothing happens for 20 seconds, reset the controller
772313Sbill  * and begin anew.
773313Sbill  */
774313Sbill upwatch()
775313Sbill {
7762395Swnj 	register struct uba_minfo *um;
7772395Swnj 	register sc21, unit;
7782470Swnj 	register struct up_softc *sc;
779313Sbill 
7802759Swnj 	timeout(upwatch, (caddr_t)0, hz);
7812646Swnj 	for (sc21 = 0; sc21 < NSC; sc21++) {
7822395Swnj 		um = upminfo[sc21];
7832470Swnj 		if (um == 0 || um->um_alive == 0)
7842470Swnj 			continue;
7852470Swnj 		sc = &up_softc[sc21];
7862395Swnj 		if (um->um_tab.b_active == 0) {
7872395Swnj 			for (unit = 0; unit < NUP; unit++)
7882629Swnj 				if (uputab[unit].b_active &&
7892629Swnj 				    updinfo[unit]->ui_mi == um)
7902395Swnj 					goto active;
7912470Swnj 			sc->sc_wticks = 0;
7922395Swnj 			continue;
7932395Swnj 		}
7942395Swnj     active:
7952470Swnj 		sc->sc_wticks++;
7962470Swnj 		if (sc->sc_wticks >= 20) {
7972470Swnj 			sc->sc_wticks = 0;
7982646Swnj 			printf("LOST upintr ");
7992646Swnj 			ubareset(um->um_ubanum);
8002395Swnj 		}
801313Sbill 	}
802313Sbill }
8032379Swnj 
8042379Swnj #define	DBSIZE	20
8052379Swnj 
8062379Swnj updump(dev)
8072379Swnj 	dev_t dev;
8082379Swnj {
8092629Swnj 	struct updevice *upaddr;
8102379Swnj 	char *start;
811*2889Swnj 	int num, blk, unit, i;
8122379Swnj 	struct size *sizes;
8132395Swnj 	register struct uba_regs *uba;
8142395Swnj 	register struct uba_dinfo *ui;
8152379Swnj 	register short *rp;
8162395Swnj 	struct upst *st;
8172379Swnj 
8182395Swnj 	unit = minor(dev) >> 3;
819*2889Swnj 	if (unit >= NUP)
820*2889Swnj 		return (ENXIO);
8212470Swnj #define	phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
8222395Swnj 	ui = phys(struct uba_dinfo *, updinfo[unit]);
823*2889Swnj 	if (ui->ui_alive == 0)
824*2889Swnj 		return (ENXIO);
8252395Swnj 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
8262395Swnj #if VAX780
8272470Swnj 	if (cpu == VAX_780)
8282470Swnj 		ubainit(uba);
8291809Sbill #endif
830*2889Swnj 	DELAY(2000000);
8312629Swnj 	upaddr = (struct updevice *)ui->ui_physaddr;
832*2889Swnj 	if ((upaddr->upcs1&UP_DVA) == 0)
833*2889Swnj 		return (EFAULT);
8342379Swnj 	num = maxfree;
8352379Swnj 	start = 0;
8362379Swnj 	upaddr->upcs2 = unit;
8372629Swnj 	if ((upaddr->upds & UP_VV) == 0) {
8382629Swnj 		upaddr->upcs1 = UP_DCLR|UP_GO;
8392629Swnj 		upaddr->upcs1 = UP_PRESET|UP_GO;
8402629Swnj 		upaddr->upof = UP_FMT22;
8412379Swnj 	}
842*2889Swnj 	if ((upaddr->upds & UP_DREADY) != UP_DREADY)
843*2889Swnj 		return (EFAULT);
8442470Swnj 	st = &upst[ui->ui_type];
8452395Swnj 	sizes = phys(struct size *, st->sizes);
846*2889Swnj 	if (dumplo < 0 || dumplo + num >= sizes[minor(dev)&07].nblocks)
847*2889Swnj 		return (EINVAL);
8482379Swnj 	while (num > 0) {
8492379Swnj 		register struct pte *io;
8502379Swnj 		register int i;
8512379Swnj 		int cn, sn, tn;
8522379Swnj 		daddr_t bn;
8532379Swnj 
8542379Swnj 		blk = num > DBSIZE ? DBSIZE : num;
8552395Swnj 		io = uba->uba_map;
8562379Swnj 		for (i = 0; i < blk; i++)
8572395Swnj 			*(int *)io++ = (btop(start)+i) | (1<<21) | UBA_MRV;
8582379Swnj 		*(int *)io = 0;
8592379Swnj 		bn = dumplo + btop(start);
8602607Swnj 		cn = bn/st->nspc + sizes[minor(dev)&07].cyloff;
8612607Swnj 		sn = bn%st->nspc;
8622607Swnj 		tn = sn/st->nsect;
8632607Swnj 		sn = sn%st->nsect;
8642379Swnj 		upaddr->updc = cn;
8652379Swnj 		rp = (short *) &upaddr->upda;
8662379Swnj 		*rp = (tn << 8) + sn;
8672379Swnj 		*--rp = 0;
8682379Swnj 		*--rp = -blk*NBPG / sizeof (short);
8692629Swnj 		*--rp = UP_GO|UP_WCOM;
8702379Swnj 		do {
8712379Swnj 			DELAY(25);
8722629Swnj 		} while ((upaddr->upcs1 & UP_RDY) == 0);
873*2889Swnj 		if (upaddr->upcs1&UP_ERR)
874*2889Swnj 			return (EIO);
8752379Swnj 		start += blk*NBPG;
8762379Swnj 		num -= blk;
8772379Swnj 	}
8782379Swnj 	return (0);
8792379Swnj }
8801902Swnj #endif
881