xref: /csrg-svn/sys/vax/uba/up.c (revision 12505)
1*12505Ssam /*	up.c	4.73	83/05/18	*/
2264Sbill 
31937Swnj #include "up.h"
42646Swnj #if NSC > 0
5264Sbill /*
69548Ssam  * UNIBUS disk driver with:
79548Ssam  *	overlapped seeks,
89548Ssam  *	ECC recovery, and
99548Ssam  *	bad sector forwarding.
102889Swnj  *
112889Swnj  * TODO:
123445Sroot  *	Check that offset recovery code works
13264Sbill  */
149782Ssam #include "../machine/pte.h"
15264Sbill 
16264Sbill #include "../h/param.h"
17264Sbill #include "../h/systm.h"
18308Sbill #include "../h/dk.h"
199548Ssam #include "../h/dkbad.h"
20264Sbill #include "../h/buf.h"
21264Sbill #include "../h/conf.h"
22264Sbill #include "../h/dir.h"
23264Sbill #include "../h/user.h"
24264Sbill #include "../h/map.h"
252571Swnj #include "../h/vm.h"
262379Swnj #include "../h/cmap.h"
279357Ssam #include "../h/uio.h"
2810871Ssam #include "../h/kernel.h"
29264Sbill 
309357Ssam #include "../vax/cpu.h"
319357Ssam #include "../vax/nexus.h"
329357Ssam #include "../vaxuba/ubavar.h"
339357Ssam #include "../vaxuba/ubareg.h"
349357Ssam #include "../vaxuba/upreg.h"
359357Ssam 
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 */
4410858Ssam struct	size {
45264Sbill 	daddr_t	nblocks;
46264Sbill 	int	cyloff;
4711211Ssam } up9300_sizes[8] = {
48264Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
49264Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
50341Sbill 	495520,	0,		/* C=cyl 0 thru 814 */
51264Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
52264Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
533730Sroot 	81376,	681,		/* F=cyl 681 thru 814 */
543730Sroot 	153728,	562,		/* G=cyl 562 thru 814 */
55264Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
5611211Ssam }, up9766_sizes[8] = {
5711211Ssam 	15884,	0,		/* A=cyl 0 thru 26 */
5811211Ssam 	33440,	27,		/* B=cyl 27 thru 81 */
5911211Ssam 	500384,	0,		/* C=cyl 0 thru 822 */
6011211Ssam 	15884,	562,		/* D=cyl 562 thru 588 */
6111211Ssam 	55936,	589,		/* E=cyl 589 thru 680 */
6211211Ssam 	86240,	681,		/* F=cyl 681 thru 822 */
6311211Ssam 	158592,	562,		/* G=cyl 562 thru 822 */
6411211Ssam 	291346,	82,		/* H=cyl 82 thru 561 */
6511211Ssam }, up160_sizes[8] = {
662395Swnj 	15884,	0,		/* A=cyl 0 thru 49 */
672395Swnj 	33440,	50,		/* B=cyl 50 thru 154 */
682395Swnj 	263360,	0,		/* C=cyl 0 thru 822 */
6911211Ssam 	15884,	155,		/* D=cyl 155 thru 204 */
7011211Ssam 	55936,	205,		/* E=cyl 205 thru 379 */
7111211Ssam 	141664,	380,		/* F=cyl 380 thru 822 */
7211211Ssam 	213664,	155,		/* G=cyl 155 thru 822 */
732395Swnj 	0,	0,
746851Ssam }, upam_sizes[8] = {
756305Sroot 	15884,	0,		/* A=cyl 0 thru 31 */
766305Sroot 	33440,	32,		/* B=cyl 32 thru 97 */
776305Sroot 	524288,	0,		/* C=cyl 0 thru 1023 */
7811211Ssam 	15884,	668,		/* D=cyl 668 thru 699 */
7911211Ssam 	55936,	700,		/* E=cyl 700 thru 809 */
8011211Ssam 	109472,	810,		/* F=cyl 810 thru 1023 */
8111211Ssam 	182176,	668,		/* G=cyl 668 thru 1023 */
826305Sroot 	291346,	98,		/* H=cyl 98 thru 667 */
83264Sbill };
842395Swnj /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
85264Sbill 
866346Swnj /*
876346Swnj  * On a 780 upSDIST could be 2, but
886346Swnj  * in the interest of 750's...
896346Swnj  */
906346Swnj #define	_upSDIST	3		/* 1.5 msec */
912395Swnj #define	_upRDIST	4		/* 2.0 msec */
92264Sbill 
932395Swnj int	upSDIST = _upSDIST;
942395Swnj int	upRDIST = _upRDIST;
952395Swnj 
962607Swnj int	upprobe(), upslave(), upattach(), updgo(), upintr();
972983Swnj struct	uba_ctlr *upminfo[NSC];
982983Swnj struct	uba_device *updinfo[NUP];
996383Swnj #define	UPIPUNITS	8
1006383Swnj struct	uba_device *upip[NSC][UPIPUNITS]; /* fuji w/fixed head gives n,n+4 */
1012395Swnj 
1022607Swnj u_short	upstd[] = { 0776700, 0774400, 0776300, 0 };
1032616Swnj struct	uba_driver scdriver =
1042607Swnj     { upprobe, upslave, upattach, updgo, upstd, "up", updinfo, "sc", upminfo };
1052395Swnj struct	buf	uputab[NUP];
1069548Ssam char upinit[NUP];
1072395Swnj 
1082395Swnj struct	upst {
1092395Swnj 	short	nsect;
1102395Swnj 	short	ntrak;
1112395Swnj 	short	nspc;
1122395Swnj 	short	ncyl;
1132395Swnj 	struct	size *sizes;
1142395Swnj } upst[] = {
11511211Ssam 	32,	19,	32*19,	815,	up9300_sizes,	/* 9300 */
11611211Ssam 	32,	19,	32*19,	823,	up9766_sizes,	/* 9766 */
11711211Ssam 	32,	10,	32*10,	823,	up160_sizes,	/* fujitsu 160m */
1186851Ssam 	32,	16,	32*16,	1024,	upam_sizes,	/* ampex capricorn */
11911119Ssam 	0,	0,	0,	0,	0
1202395Swnj };
1212395Swnj 
1222629Swnj u_char	up_offset[16] = {
1239548Ssam 	UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400,
1249548Ssam 	UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800,
1259548Ssam 	UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200,
1269548Ssam 	0, 0, 0, 0
1272629Swnj };
128264Sbill 
1292616Swnj struct	buf	rupbuf[NUP];
1309548Ssam struct 	buf	bupbuf[NUP];
1319548Ssam struct	dkbad	upbad[NUP];
132264Sbill 
133264Sbill #define	b_cylin b_resid
134264Sbill 
135264Sbill #ifdef INTRLVE
136264Sbill daddr_t dkblock();
137264Sbill #endif
1382395Swnj 
1392395Swnj int	upwstart, upwatch();		/* Have started guardian */
1402470Swnj int	upseek;
1412681Swnj int	upwaitdry;
1422395Swnj 
1432395Swnj /*ARGSUSED*/
1442607Swnj upprobe(reg)
1452395Swnj 	caddr_t reg;
1462395Swnj {
1472459Swnj 	register int br, cvec;
1482459Swnj 
1492607Swnj #ifdef lint
1502607Swnj 	br = 0; cvec = br; br = cvec;
1512607Swnj #endif
1522629Swnj 	((struct updevice *)reg)->upcs1 = UP_IE|UP_RDY;
1532607Swnj 	DELAY(10);
1542629Swnj 	((struct updevice *)reg)->upcs1 = 0;
1559357Ssam 	return (sizeof (struct updevice));
1562395Swnj }
1572395Swnj 
1582607Swnj upslave(ui, reg)
1592983Swnj 	struct uba_device *ui;
1602395Swnj 	caddr_t reg;
1612395Swnj {
1622629Swnj 	register struct updevice *upaddr = (struct updevice *)reg;
1632395Swnj 
1642395Swnj 	upaddr->upcs1 = 0;		/* conservative */
1652607Swnj 	upaddr->upcs2 = ui->ui_slave;
1666843Swnj 	upaddr->upcs1 = UP_NOP|UP_GO;
1673445Sroot 	if (upaddr->upcs2&UPCS2_NED) {
1682629Swnj 		upaddr->upcs1 = UP_DCLR|UP_GO;
1692395Swnj 		return (0);
1702395Swnj 	}
1712607Swnj 	return (1);
1722607Swnj }
1732607Swnj 
1742607Swnj upattach(ui)
1752983Swnj 	register struct uba_device *ui;
1762607Swnj {
1772607Swnj 
1782395Swnj 	if (upwstart == 0) {
1792759Swnj 		timeout(upwatch, (caddr_t)0, hz);
1802395Swnj 		upwstart++;
1812395Swnj 	}
1822571Swnj 	if (ui->ui_dk >= 0)
1832571Swnj 		dk_mspw[ui->ui_dk] = .0000020345;
1842607Swnj 	upip[ui->ui_ctlr][ui->ui_slave] = ui;
1852607Swnj 	up_softc[ui->ui_ctlr].sc_ndrive++;
18611119Ssam 	ui->ui_type = upmaptype(ui);
18711119Ssam }
18811119Ssam 
18911119Ssam upmaptype(ui)
19011119Ssam 	register struct uba_device *ui;
19111119Ssam {
19211119Ssam 	register struct updevice *upaddr = (struct updevice *)ui->ui_addr;
19311119Ssam 	int type = ui->ui_type;
19411119Ssam 	register struct upst *st;
19511119Ssam 
1962629Swnj 	upaddr->upcs1 = 0;
1972629Swnj 	upaddr->upcs2 = ui->ui_slave;
1983496Sroot 	upaddr->uphr = UPHR_MAXTRAK;
19911119Ssam 	for (st = upst; st->nsect != 0; st++)
20011119Ssam 		if (upaddr->uphr == st->ntrak - 1) {
20111119Ssam 			type = st - upst;
20211119Ssam 			break;
20311119Ssam 		}
20411119Ssam 	if (st->nsect == 0)
20511119Ssam 		printf("up%d: uphr=%x\n", ui->ui_slave, upaddr->uphr);
20611119Ssam 	if (type == 0) {
20711112Shelge 		upaddr->uphr = UPHR_MAXCYL;
20811112Shelge 		if (upaddr->uphr == 822)
20911119Ssam 			type++;
21011112Shelge 	}
2113496Sroot 	upaddr->upcs2 = UPCS2_CLR;
21211119Ssam 	return (type);
2132395Swnj }
214264Sbill 
2159548Ssam upopen(dev)
2169548Ssam 	dev_t dev;
2179548Ssam {
2189548Ssam 	register int unit = minor(dev) >> 3;
2199548Ssam 	register struct uba_device *ui;
2209548Ssam 
2219548Ssam 	if (unit >= NUP || (ui = updinfo[unit]) == 0 || ui->ui_alive == 0)
2229548Ssam 		return (ENXIO);
2239548Ssam 	return (0);
2249548Ssam }
2259548Ssam 
226264Sbill upstrategy(bp)
2272395Swnj 	register struct buf *bp;
228264Sbill {
2292983Swnj 	register struct uba_device *ui;
2302395Swnj 	register struct upst *st;
2312395Swnj 	register int unit;
2322470Swnj 	register struct buf *dp;
2332395Swnj 	int xunit = minor(bp->b_dev) & 07;
2342470Swnj 	long bn, sz;
235264Sbill 
2362470Swnj 	sz = (bp->b_bcount+511) >> 9;
237264Sbill 	unit = dkunit(bp);
2382395Swnj 	if (unit >= NUP)
2392395Swnj 		goto bad;
2402395Swnj 	ui = updinfo[unit];
2412395Swnj 	if (ui == 0 || ui->ui_alive == 0)
2422395Swnj 		goto bad;
2432395Swnj 	st = &upst[ui->ui_type];
2442395Swnj 	if (bp->b_blkno < 0 ||
2452395Swnj 	    (bn = dkblock(bp))+sz > st->sizes[xunit].nblocks)
2462395Swnj 		goto bad;
2472395Swnj 	bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff;
2486305Sroot 	(void) spl5();
2492470Swnj 	dp = &uputab[ui->ui_unit];
2502470Swnj 	disksort(dp, bp);
2512470Swnj 	if (dp->b_active == 0) {
2522395Swnj 		(void) upustart(ui);
2532395Swnj 		bp = &ui->ui_mi->um_tab;
2542395Swnj 		if (bp->b_actf && bp->b_active == 0)
2552395Swnj 			(void) upstart(ui->ui_mi);
256264Sbill 	}
2576305Sroot 	(void) spl0();
2582395Swnj 	return;
2592395Swnj 
2602395Swnj bad:
2612395Swnj 	bp->b_flags |= B_ERROR;
2622395Swnj 	iodone(bp);
2632395Swnj 	return;
264264Sbill }
265264Sbill 
2662674Swnj /*
2672674Swnj  * Unit start routine.
2682674Swnj  * Seek the drive to be where the data is
2692674Swnj  * and then generate another interrupt
2702674Swnj  * to actually start the transfer.
2712674Swnj  * If there is only one drive on the controller,
2722674Swnj  * or we are very close to the data, don't
2732674Swnj  * bother with the search.  If called after
2742674Swnj  * searching once, don't bother to look where
2752674Swnj  * we are, just queue for transfer (to avoid
2762674Swnj  * positioning forever without transferrring.)
2772674Swnj  */
2782395Swnj upustart(ui)
2792983Swnj 	register struct uba_device *ui;
280264Sbill {
281264Sbill 	register struct buf *bp, *dp;
2822983Swnj 	register struct uba_ctlr *um;
2832629Swnj 	register struct updevice *upaddr;
2842395Swnj 	register struct upst *st;
285264Sbill 	daddr_t bn;
2862674Swnj 	int sn, csn;
2872607Swnj 	/*
2882607Swnj 	 * The SC21 cancels commands if you just say
2892629Swnj 	 *	cs1 = UP_IE
2902607Swnj 	 * so we are cautious about handling of cs1.
2912607Swnj 	 * Also don't bother to clear as bits other than in upintr().
2922607Swnj 	 */
2932674Swnj 	int didie = 0;
2942674Swnj 
2952674Swnj 	if (ui == 0)
2962674Swnj 		return (0);
2972983Swnj 	um = ui->ui_mi;
2982395Swnj 	dk_busy &= ~(1<<ui->ui_dk);
2992395Swnj 	dp = &uputab[ui->ui_unit];
300266Sbill 	if ((bp = dp->b_actf) == NULL)
301268Sbill 		goto out;
3022674Swnj 	/*
3032674Swnj 	 * If the controller is active, just remember
3042674Swnj 	 * that this device would like to be positioned...
3052674Swnj 	 * if we tried to position now we would confuse the SC21.
3062674Swnj 	 */
3072395Swnj 	if (um->um_tab.b_active) {
3082459Swnj 		up_softc[um->um_ctlr].sc_softas |= 1<<ui->ui_slave;
309275Sbill 		return (0);
310275Sbill 	}
3112674Swnj 	/*
3122674Swnj 	 * If we have already positioned this drive,
3132674Swnj 	 * then just put it on the ready queue.
3142674Swnj 	 */
315276Sbill 	if (dp->b_active)
316276Sbill 		goto done;
317276Sbill 	dp->b_active = 1;
3182629Swnj 	upaddr = (struct updevice *)um->um_addr;
3192395Swnj 	upaddr->upcs2 = ui->ui_slave;
3202674Swnj 	/*
3212674Swnj 	 * If drive has just come up,
3222674Swnj 	 * setup the pack.
3232674Swnj 	 */
3249548Ssam 	if ((upaddr->upds & UPDS_VV) == 0 || upinit[ui->ui_unit] == 0) {
3259548Ssam 		struct buf *bbp = &bupbuf[ui->ui_unit];
32610858Ssam 
3272607Swnj 		/* SHOULD WARN SYSTEM THAT THIS HAPPENED */
3289548Ssam 		upinit[ui->ui_unit] = 1;
3292629Swnj 		upaddr->upcs1 = UP_IE|UP_DCLR|UP_GO;
3302629Swnj 		upaddr->upcs1 = UP_IE|UP_PRESET|UP_GO;
3313445Sroot 		upaddr->upof = UPOF_FMT22;
332268Sbill 		didie = 1;
3339548Ssam 		st = &upst[ui->ui_type];
3349548Ssam 		bbp->b_flags = B_READ|B_BUSY;
3359548Ssam 		bbp->b_dev = bp->b_dev;
3369548Ssam 		bbp->b_bcount = 512;
3379548Ssam 		bbp->b_un.b_addr = (caddr_t)&upbad[ui->ui_unit];
3389548Ssam 		bbp->b_blkno = st->ncyl * st->nspc - st->nsect;
3399548Ssam 		bbp->b_cylin = st->ncyl - 1;
3409548Ssam 		dp->b_actf = bbp;
3419548Ssam 		bbp->av_forw = bp;
3429548Ssam 		bp = bbp;
343264Sbill 	}
3442674Swnj 	/*
3452674Swnj 	 * If drive is offline, forget about positioning.
3462674Swnj 	 */
3473445Sroot 	if ((upaddr->upds & (UPDS_DPR|UPDS_MOL)) != (UPDS_DPR|UPDS_MOL))
348275Sbill 		goto done;
3492674Swnj 	/*
3502674Swnj 	 * If there is only one drive,
3512674Swnj 	 * dont bother searching.
3522674Swnj 	 */
3532607Swnj 	if (up_softc[um->um_ctlr].sc_ndrive == 1)
3542607Swnj 		goto done;
3552674Swnj 	/*
3562674Swnj 	 * Figure out where this transfer is going to
3572674Swnj 	 * and see if we are close enough to justify not searching.
3582674Swnj 	 */
3592395Swnj 	st = &upst[ui->ui_type];
360264Sbill 	bn = dkblock(bp);
3612395Swnj 	sn = bn%st->nspc;
3622395Swnj 	sn = (sn + st->nsect - upSDIST) % st->nsect;
3632674Swnj 	if (bp->b_cylin - upaddr->updc)
364266Sbill 		goto search;		/* Not on-cylinder */
365275Sbill 	else if (upseek)
366275Sbill 		goto done;		/* Ok just to be on-cylinder */
367264Sbill 	csn = (upaddr->upla>>6) - sn - 1;
368266Sbill 	if (csn < 0)
3692395Swnj 		csn += st->nsect;
3702395Swnj 	if (csn > st->nsect - upRDIST)
371264Sbill 		goto done;
372264Sbill search:
3732674Swnj 	upaddr->updc = bp->b_cylin;
3742674Swnj 	/*
3752674Swnj 	 * Not on cylinder at correct position,
3762674Swnj 	 * seek/search.
3772674Swnj 	 */
378275Sbill 	if (upseek)
3792629Swnj 		upaddr->upcs1 = UP_IE|UP_SEEK|UP_GO;
3802470Swnj 	else {
381275Sbill 		upaddr->upda = sn;
3822629Swnj 		upaddr->upcs1 = UP_IE|UP_SEARCH|UP_GO;
383275Sbill 	}
384268Sbill 	didie = 1;
3852674Swnj 	/*
3862674Swnj 	 * Mark unit busy for iostat.
3872674Swnj 	 */
3882395Swnj 	if (ui->ui_dk >= 0) {
3892395Swnj 		dk_busy |= 1<<ui->ui_dk;
3902395Swnj 		dk_seek[ui->ui_dk]++;
391264Sbill 	}
392268Sbill 	goto out;
393264Sbill done:
3942674Swnj 	/*
3952674Swnj 	 * Device is ready to go.
3962674Swnj 	 * Put it on the ready queue for the controller
3972674Swnj 	 * (unless its already there.)
3982674Swnj 	 */
3992629Swnj 	if (dp->b_active != 2) {
4002629Swnj 		dp->b_forw = NULL;
4012629Swnj 		if (um->um_tab.b_actf == NULL)
4022629Swnj 			um->um_tab.b_actf = dp;
4032629Swnj 		else
4042629Swnj 			um->um_tab.b_actl->b_forw = dp;
4052629Swnj 		um->um_tab.b_actl = dp;
4062629Swnj 		dp->b_active = 2;
4072629Swnj 	}
408268Sbill out:
409268Sbill 	return (didie);
410264Sbill }
411264Sbill 
4122674Swnj /*
4132674Swnj  * Start up a transfer on a drive.
4142674Swnj  */
4152395Swnj upstart(um)
4162983Swnj 	register struct uba_ctlr *um;
417264Sbill {
418264Sbill 	register struct buf *bp, *dp;
4192983Swnj 	register struct uba_device *ui;
4202629Swnj 	register struct updevice *upaddr;
4212470Swnj 	struct upst *st;
422264Sbill 	daddr_t bn;
4232681Swnj 	int dn, sn, tn, cmd, waitdry;
424264Sbill 
425264Sbill loop:
4262674Swnj 	/*
4272674Swnj 	 * Pull a request off the controller queue
4282674Swnj 	 */
4292395Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
430268Sbill 		return (0);
431264Sbill 	if ((bp = dp->b_actf) == NULL) {
4322395Swnj 		um->um_tab.b_actf = dp->b_forw;
433264Sbill 		goto loop;
434264Sbill 	}
4352674Swnj 	/*
4362674Swnj 	 * Mark controller busy, and
4372674Swnj 	 * determine destination of this request.
4382674Swnj 	 */
4392395Swnj 	um->um_tab.b_active++;
4402395Swnj 	ui = updinfo[dkunit(bp)];
441264Sbill 	bn = dkblock(bp);
4422395Swnj 	dn = ui->ui_slave;
4432395Swnj 	st = &upst[ui->ui_type];
4442395Swnj 	sn = bn%st->nspc;
4452395Swnj 	tn = sn/st->nsect;
4462395Swnj 	sn %= st->nsect;
4472629Swnj 	upaddr = (struct updevice *)ui->ui_addr;
4482674Swnj 	/*
4492674Swnj 	 * Select drive if not selected already.
4502674Swnj 	 */
4512674Swnj 	if ((upaddr->upcs2&07) != dn)
4522674Swnj 		upaddr->upcs2 = dn;
4532674Swnj 	/*
4542674Swnj 	 * Check that it is ready and online
4552674Swnj 	 */
4562681Swnj 	waitdry = 0;
4573445Sroot 	while ((upaddr->upds&UPDS_DRY) == 0) {
4587036Swnj 		printf("up%d: ds wait ds=%o\n",dkunit(bp),upaddr->upds);
4592681Swnj 		if (++waitdry > 512)
4602681Swnj 			break;
4612681Swnj 		upwaitdry++;
4622681Swnj 	}
4633445Sroot 	if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
4642931Swnj 		printf("up%d: not ready", dkunit(bp));
4653445Sroot 		if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
4662607Swnj 			printf("\n");
4672395Swnj 			um->um_tab.b_active = 0;
4682395Swnj 			um->um_tab.b_errcnt = 0;
469893Sbill 			dp->b_actf = bp->av_forw;
470893Sbill 			dp->b_active = 0;
471893Sbill 			bp->b_flags |= B_ERROR;
472893Sbill 			iodone(bp);
473893Sbill 			goto loop;
474893Sbill 		}
4752674Swnj 		/*
4762674Swnj 		 * Oh, well, sometimes this
4772674Swnj 		 * happens, for reasons unknown.
4782674Swnj 		 */
4792629Swnj 		printf(" (flakey)\n");
480264Sbill 	}
4812674Swnj 	/*
4822674Swnj 	 * Setup for the transfer, and get in the
4832674Swnj 	 * UNIBUS adaptor queue.
4842674Swnj 	 */
4852424Skre 	upaddr->updc = bp->b_cylin;
486264Sbill 	upaddr->upda = (tn << 8) + sn;
487264Sbill 	upaddr->upwc = -bp->b_bcount / sizeof (short);
488264Sbill 	if (bp->b_flags & B_READ)
4892629Swnj 		cmd = UP_IE|UP_RCOM|UP_GO;
490264Sbill 	else
4912629Swnj 		cmd = UP_IE|UP_WCOM|UP_GO;
4922571Swnj 	um->um_cmd = cmd;
4933107Swnj 	(void) ubago(ui);
494268Sbill 	return (1);
495264Sbill }
496264Sbill 
4972674Swnj /*
4982674Swnj  * Now all ready to go, stuff the registers.
4992674Swnj  */
5002571Swnj updgo(um)
5012983Swnj 	struct uba_ctlr *um;
5022395Swnj {
5032629Swnj 	register struct updevice *upaddr = (struct updevice *)um->um_addr;
5042470Swnj 
5056953Swnj 	um->um_tab.b_active = 2;	/* should now be 2 */
5062571Swnj 	upaddr->upba = um->um_ubinfo;
5072571Swnj 	upaddr->upcs1 = um->um_cmd|((um->um_ubinfo>>8)&0x300);
5082395Swnj }
5092395Swnj 
5102674Swnj /*
5112674Swnj  * Handle a disk interrupt.
5122674Swnj  */
5132707Swnj upintr(sc21)
5142395Swnj 	register sc21;
515264Sbill {
516264Sbill 	register struct buf *bp, *dp;
5172983Swnj 	register struct uba_ctlr *um = upminfo[sc21];
5182983Swnj 	register struct uba_device *ui;
5192629Swnj 	register struct updevice *upaddr = (struct updevice *)um->um_addr;
520264Sbill 	register unit;
5212470Swnj 	struct up_softc *sc = &up_softc[um->um_ctlr];
5222607Swnj 	int as = (upaddr->upas & 0377) | sc->sc_softas;
5232681Swnj 	int needie = 1, waitdry;
524264Sbill 
5252470Swnj 	sc->sc_wticks = 0;
5262607Swnj 	sc->sc_softas = 0;
5272674Swnj 	/*
5282674Swnj 	 * If controller wasn't transferring, then this is an
5292674Swnj 	 * interrupt for attention status on seeking drives.
5302674Swnj 	 * Just service them.
5312674Swnj 	 */
5326346Swnj 	if (um->um_tab.b_active != 2 && !sc->sc_recal) {
5332674Swnj 		if (upaddr->upcs1 & UP_TRE)
5342674Swnj 			upaddr->upcs1 = UP_TRE;
5352674Swnj 		goto doattn;
5362674Swnj 	}
5376953Swnj 	um->um_tab.b_active = 1;
5382674Swnj 	/*
5392674Swnj 	 * Get device and block structures, and a pointer
5402983Swnj 	 * to the uba_device for the drive.  Select the drive.
5412674Swnj 	 */
5422674Swnj 	dp = um->um_tab.b_actf;
5432674Swnj 	bp = dp->b_actf;
5442674Swnj 	ui = updinfo[dkunit(bp)];
5452674Swnj 	dk_busy &= ~(1 << ui->ui_dk);
5462674Swnj 	if ((upaddr->upcs2&07) != ui->ui_slave)
5472395Swnj 		upaddr->upcs2 = ui->ui_slave;
5489548Ssam 	if (bp->b_flags&B_BAD) {
5499548Ssam 		if (upecc(ui, CONT))
5509548Ssam 			return;
5519548Ssam 	}
5522674Swnj 	/*
5532674Swnj 	 * Check for and process errors on
5542674Swnj 	 * either the drive or the controller.
5552674Swnj 	 */
5563445Sroot 	if ((upaddr->upds&UPDS_ERR) || (upaddr->upcs1&UP_TRE)) {
5572681Swnj 		waitdry = 0;
5583445Sroot 		while ((upaddr->upds & UPDS_DRY) == 0) {
5592681Swnj 			if (++waitdry > 512)
5602681Swnj 				break;
5612681Swnj 			upwaitdry++;
5622681Swnj 		}
5633445Sroot 		if (upaddr->uper1&UPER1_WLE) {
5642674Swnj 			/*
5652674Swnj 			 * Give up on write locked devices
5662674Swnj 			 * immediately.
5672674Swnj 			 */
5682931Swnj 			printf("up%d: write locked\n", dkunit(bp));
5692674Swnj 			bp->b_flags |= B_ERROR;
5702674Swnj 		} else if (++um->um_tab.b_errcnt > 27) {
5712674Swnj 			/*
5722674Swnj 			 * After 28 retries (16 without offset, and
5732674Swnj 			 * 12 with offset positioning) give up.
57410904Shelge 			 * If the error was header CRC, the header is
57510904Shelge 			 * screwed up, and the sector may in fact exist
57610904Shelge 			 * in the bad sector table, better check...
5772674Swnj 			 */
57810904Shelge 			if (upaddr->uper1&UPER1_HCRC) {
57910904Shelge 				if (upecc(ui, BSE))
58010904Shelge 					return;
58110904Shelge 			}
5829548Ssam 	hard:
5832931Swnj 			harderr(bp, "up");
5849548Ssam 			printf("cn=%d tn=%d sn=%d cs2=%b er1=%b er2=%b\n",
5859548Ssam 			        upaddr->updc, ((upaddr->upda)>>8)&077,
5869548Ssam 			        (upaddr->upda)&037,
5879548Ssam 				upaddr->upcs2, UPCS2_BITS,
5889548Ssam 				upaddr->uper1, UPER1_BITS,
5899548Ssam 				upaddr->uper2, UPER2_BITS);
5902674Swnj 			bp->b_flags |= B_ERROR;
5919548Ssam 		} else if (upaddr->uper2 & UPER2_BSE) {
5929548Ssam 			if (upecc(ui, BSE))
5939548Ssam 				return;
5949548Ssam 			else
5959548Ssam 				goto hard;
5962674Swnj 		} else {
5972674Swnj 			/*
5982674Swnj 			 * Retriable error.
5992674Swnj 			 * If a soft ecc, correct it (continuing
6002674Swnj 			 * by returning if necessary.
6012674Swnj 			 * Otherwise fall through and retry the transfer
6022674Swnj 			 */
6037183Sroot 			if ((upaddr->uper1&(UPER1_DCK|UPER1_ECH))==UPER1_DCK) {
6049548Ssam 				if (upecc(ui, ECC))
6052629Swnj 					return;
6067183Sroot 			} else
6077183Sroot 				um->um_tab.b_active = 0; /* force retry */
6082674Swnj 		}
6092674Swnj 		/*
6102674Swnj 		 * Clear drive error and, every eight attempts,
6112674Swnj 		 * (starting with the fourth)
6122674Swnj 		 * recalibrate to clear the slate.
6132674Swnj 		 */
6142674Swnj 		upaddr->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
6152674Swnj 		needie = 0;
6163182Swnj 		if ((um->um_tab.b_errcnt&07) == 4 && um->um_tab.b_active == 0) {
6172674Swnj 			upaddr->upcs1 = UP_RECAL|UP_IE|UP_GO;
6183160Swnj 			sc->sc_recal = 0;
6193160Swnj 			goto nextrecal;
6202674Swnj 		}
6212674Swnj 	}
6222674Swnj 	/*
6233160Swnj 	 * Advance recalibration finite state machine
6243160Swnj 	 * if recalibrate in progress, through
6253160Swnj 	 *	RECAL
6263160Swnj 	 *	SEEK
6273160Swnj 	 *	OFFSET (optional)
6283160Swnj 	 *	RETRY
6292674Swnj 	 */
6303160Swnj 	switch (sc->sc_recal) {
6313160Swnj 
6323160Swnj 	case 1:
6333160Swnj 		upaddr->updc = bp->b_cylin;
6343160Swnj 		upaddr->upcs1 = UP_SEEK|UP_IE|UP_GO;
6353160Swnj 		goto nextrecal;
6363160Swnj 	case 2:
6373160Swnj 		if (um->um_tab.b_errcnt < 16 || (bp->b_flags&B_READ) == 0)
6383160Swnj 			goto donerecal;
6393445Sroot 		upaddr->upof = up_offset[um->um_tab.b_errcnt & 017] | UPOF_FMT22;
6403160Swnj 		upaddr->upcs1 = UP_IE|UP_OFFSET|UP_GO;
6413160Swnj 		goto nextrecal;
6423160Swnj 	nextrecal:
6433160Swnj 		sc->sc_recal++;
6443160Swnj 		um->um_tab.b_active = 1;
6453160Swnj 		return;
6463160Swnj 	donerecal:
6473160Swnj 	case 3:
6482674Swnj 		sc->sc_recal = 0;
6493160Swnj 		um->um_tab.b_active = 0;
6503160Swnj 		break;
6512674Swnj 	}
6522674Swnj 	/*
6532674Swnj 	 * If still ``active'', then don't need any more retries.
6542674Swnj 	 */
6552674Swnj 	if (um->um_tab.b_active) {
6562674Swnj 		/*
6572674Swnj 		 * If we were offset positioning,
6582674Swnj 		 * return to centerline.
6592674Swnj 		 */
6602674Swnj 		if (um->um_tab.b_errcnt >= 16) {
6613445Sroot 			upaddr->upof = UPOF_FMT22;
6622674Swnj 			upaddr->upcs1 = UP_RTC|UP_GO|UP_IE;
6633445Sroot 			while (upaddr->upds & UPDS_PIP)
6642674Swnj 				DELAY(25);
665268Sbill 			needie = 0;
666264Sbill 		}
6672674Swnj 		um->um_tab.b_active = 0;
6682674Swnj 		um->um_tab.b_errcnt = 0;
6692674Swnj 		um->um_tab.b_actf = dp->b_forw;
6702674Swnj 		dp->b_active = 0;
6712674Swnj 		dp->b_errcnt = 0;
6722674Swnj 		dp->b_actf = bp->av_forw;
6732674Swnj 		bp->b_resid = (-upaddr->upwc * sizeof(short));
6742674Swnj 		iodone(bp);
6752674Swnj 		/*
6762674Swnj 		 * If this unit has more work to do,
6772674Swnj 		 * then start it up right away.
6782674Swnj 		 */
6792674Swnj 		if (dp->b_actf)
6802674Swnj 			if (upustart(ui))
681268Sbill 				needie = 0;
682264Sbill 	}
6832674Swnj 	as &= ~(1<<ui->ui_slave);
6843403Swnj 	/*
6853403Swnj 	 * Release unibus resources and flush data paths.
6863403Swnj 	 */
6873403Swnj 	ubadone(um);
6882674Swnj doattn:
6892674Swnj 	/*
6902674Swnj 	 * Process other units which need attention.
6912674Swnj 	 * For each unit which needs attention, call
6922674Swnj 	 * the unit start routine to place the slave
6932674Swnj 	 * on the controller device queue.
6942674Swnj 	 */
6953160Swnj 	while (unit = ffs(as)) {
6963160Swnj 		unit--;		/* was 1 origin */
6973160Swnj 		as &= ~(1<<unit);
6983160Swnj 		upaddr->upas = 1<<unit;
6996383Swnj 		if (unit < UPIPUNITS && upustart(upip[sc21][unit]))
7003160Swnj 			needie = 0;
7013160Swnj 	}
7022674Swnj 	/*
7032674Swnj 	 * If the controller is not transferring, but
7042674Swnj 	 * there are devices ready to transfer, start
7052674Swnj 	 * the controller.
7062674Swnj 	 */
7072395Swnj 	if (um->um_tab.b_actf && um->um_tab.b_active == 0)
7082395Swnj 		if (upstart(um))
709268Sbill 			needie = 0;
710275Sbill 	if (needie)
7112629Swnj 		upaddr->upcs1 = UP_IE;
712264Sbill }
713264Sbill 
7149357Ssam upread(dev, uio)
7152616Swnj 	dev_t dev;
7169357Ssam 	struct uio *uio;
717264Sbill {
7182616Swnj 	register int unit = minor(dev) >> 3;
7192470Swnj 
7202616Swnj 	if (unit >= NUP)
7219357Ssam 		return (ENXIO);
7229357Ssam 	return (physio(upstrategy, &rupbuf[unit], dev, B_READ, minphys, uio));
723264Sbill }
724264Sbill 
7259357Ssam upwrite(dev, uio)
7262616Swnj 	dev_t dev;
7279357Ssam 	struct uio *uio;
728264Sbill {
7292616Swnj 	register int unit = minor(dev) >> 3;
7302470Swnj 
7312616Swnj 	if (unit >= NUP)
7329357Ssam 		return (ENXIO);
7339357Ssam 	return (physio(upstrategy, &rupbuf[unit], dev, B_WRITE, minphys, uio));
734264Sbill }
735264Sbill 
736266Sbill /*
737266Sbill  * Correct an ECC error, and restart the i/o to complete
738266Sbill  * the transfer if necessary.  This is quite complicated because
739266Sbill  * the transfer may be going to an odd memory address base and/or
740266Sbill  * across a page boundary.
741266Sbill  */
7429548Ssam upecc(ui, flag)
7432983Swnj 	register struct uba_device *ui;
7449548Ssam 	int flag;
745264Sbill {
7462629Swnj 	register struct updevice *up = (struct updevice *)ui->ui_addr;
7472395Swnj 	register struct buf *bp = uputab[ui->ui_unit].b_actf;
7482983Swnj 	register struct uba_ctlr *um = ui->ui_mi;
7492395Swnj 	register struct upst *st;
7502395Swnj 	struct uba_regs *ubp = ui->ui_hd->uh_uba;
751266Sbill 	register int i;
752264Sbill 	caddr_t addr;
753266Sbill 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
754264Sbill 	int bn, cn, tn, sn;
755264Sbill 
756264Sbill 	/*
757266Sbill 	 * Npf is the number of sectors transferred before the sector
758266Sbill 	 * containing the ECC error, and reg is the UBA register
759266Sbill 	 * mapping (the first part of) the transfer.
760266Sbill 	 * O is offset within a memory page of the first byte transferred.
761264Sbill 	 */
7629548Ssam 	if (flag == CONT)
7639548Ssam 		npf = bp->b_error;
7649548Ssam 	else
76510858Ssam 		npf = btop((up->upwc * sizeof(short)) + bp->b_bcount);
7662571Swnj 	reg = btop(um->um_ubinfo&0x3ffff) + npf;
767264Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
768264Sbill 	mask = up->upec2;
7693445Sroot #ifdef UPECCDEBUG
7703403Swnj 	printf("npf %d reg %x o %d mask %o pos %d\n", npf, reg, o, mask,
7713403Swnj 	    up->upec1);
7723445Sroot #endif
7739548Ssam 	bn = dkblock(bp);
7749548Ssam 	st = &upst[ui->ui_type];
7759548Ssam 	cn = bp->b_cylin;
7769548Ssam 	sn = bn%st->nspc + npf;
7779548Ssam 	tn = sn/st->nsect;
7789548Ssam 	sn %= st->nsect;
7799548Ssam 	cn += tn/st->ntrak;
7809548Ssam 	tn %= st->ntrak;
7812725Swnj 	ubapurge(um);
7829548Ssam 	um->um_tab.b_active=2;
783266Sbill 	/*
7849548Ssam 	 * action taken depends on the flag
785266Sbill 	 */
7869548Ssam 	switch(flag){
7879548Ssam 	case ECC:
7889548Ssam 		npf--;
7899548Ssam 		reg--;
7909548Ssam 		mask = up->upec2;
7919548Ssam 		printf("up%d%c: soft ecc sn%d\n", dkunit(bp),
7929548Ssam 			'a'+(minor(bp->b_dev)&07), bp->b_blkno + npf);
7939548Ssam 		/*
7949548Ssam 		 * Flush the buffered data path, and compute the
7959548Ssam 		 * byte and bit position of the error.  The variable i
7969548Ssam 		 * is the byte offset in the transfer, the variable byte
7979548Ssam 		 * is the offset from a page boundary in main memory.
7989548Ssam 		 */
7999548Ssam 		i = up->upec1 - 1;		/* -1 makes 0 origin */
8009548Ssam 		bit = i&07;
8019548Ssam 		i = (i&~07)>>3;
8029548Ssam 		byte = i + o;
8039548Ssam 		/*
8049548Ssam 		 * Correct while possible bits remain of mask.  Since mask
8059548Ssam 		 * contains 11 bits, we continue while the bit offset is > -11.
8069548Ssam 		 * Also watch out for end of this block and the end of the whole
8079548Ssam 		 * transfer.
8089548Ssam 		 */
8099548Ssam 		while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
8109548Ssam 			addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
8119548Ssam 				(byte & PGOFSET);
8123445Sroot #ifdef UPECCDEBUG
8139548Ssam 			printf("addr %x map reg %x\n",
8149548Ssam 				addr, *(int *)(&ubp->uba_map[reg+btop(byte)]));
8159548Ssam 			printf("old: %x, ", getmemc(addr));
8163445Sroot #endif
8179548Ssam 			putmemc(addr, getmemc(addr)^(mask<<bit));
8183445Sroot #ifdef UPECCDEBUG
8199548Ssam 			printf("new: %x\n", getmemc(addr));
8203445Sroot #endif
8219548Ssam 			byte++;
8229548Ssam 			i++;
8239580Shelge 			bit -= 8;
8249548Ssam 		}
8259548Ssam 		if (up->upwc == 0)
8269548Ssam 			return (0);
8279548Ssam 		npf++;
8289548Ssam 		reg++;
8299548Ssam 		break;
8309548Ssam 	case BSE:
8319548Ssam 		/*
8329548Ssam 		 * if not in bad sector table, return 0
8339548Ssam 		 */
8349548Ssam 		if ((bn = isbad(&upbad[ui->ui_unit], cn, tn, sn)) < 0)
8359548Ssam 			return(0);
8369548Ssam 		/*
8379548Ssam 		 * flag this one as bad
8389548Ssam 		 */
8399548Ssam 		bp->b_flags |= B_BAD;
8409548Ssam 		bp->b_error = npf + 1;
8419548Ssam #ifdef UPECCDEBUG
8429548Ssam 		printf("BSE: restart at %d\n",npf+1);
8439548Ssam #endif
8449548Ssam 		bn = st->ncyl * st->nspc -st->nsect - 1 - bn;
8459548Ssam 		cn = bn / st->nspc;
8469548Ssam 		sn = bn % st->nspc;
8479548Ssam 		tn = sn / st->nsect;
8489548Ssam 		sn %= st->nsect;
8499548Ssam 		up->upwc = -(512 / sizeof (short));
8509548Ssam #ifdef UPECCDEBUG
8519548Ssam 		printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
8529548Ssam #endif
8539548Ssam 		break;
8549548Ssam 	case CONT:
8559548Ssam #ifdef UPECCDEBUG
8569548Ssam 		printf("upecc, CONT: bn %d cn %d tn %d sn %d\n", bn, cn, tn, sn);
8579548Ssam #endif
8589548Ssam 		bp->b_flags &= ~B_BAD;
8599548Ssam 		up->upwc = -((bp->b_bcount - (int)ptob(npf)) / sizeof(short));
8609548Ssam 		if (up->upwc == 0)
8619548Ssam 			return(0);
8629548Ssam 		break;
863264Sbill 	}
8647183Sroot 	if (up->upwc == 0) {
8657183Sroot 		um->um_tab.b_active = 0;
866264Sbill 		return (0);
8677183Sroot 	}
868266Sbill 	/*
869266Sbill 	 * Have to continue the transfer... clear the drive,
870266Sbill 	 * and compute the position where the transfer is to continue.
871266Sbill 	 * We have completed npf+1 sectors of the transfer already;
872266Sbill 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
873266Sbill 	 */
8742629Swnj #ifdef notdef
8752629Swnj 	up->uper1 = 0;
8762629Swnj 	up->upcs1 |= UP_GO;
8772629Swnj #else
8782629Swnj 	up->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
879264Sbill 	up->updc = cn;
880266Sbill 	up->upda = (tn << 8) | sn;
8819548Ssam 	ubaddr = (int)ptob(reg) + o;
882266Sbill 	up->upba = ubaddr;
883266Sbill 	cmd = (ubaddr >> 8) & 0x300;
8849548Ssam 	cmd |= ((bp->b_flags&B_READ)?UP_RCOM:UP_WCOM)|UP_IE|UP_GO;
8859548Ssam 	um->um_tab.b_errcnt = 0;
886266Sbill 	up->upcs1 = cmd;
8872629Swnj #endif
888264Sbill 	return (1);
889264Sbill }
890286Sbill 
891286Sbill /*
892286Sbill  * Reset driver after UBA init.
893286Sbill  * Cancel software state of all pending transfers
894286Sbill  * and restart all units and the controller.
895286Sbill  */
8962395Swnj upreset(uban)
8972931Swnj 	int uban;
898286Sbill {
8992983Swnj 	register struct uba_ctlr *um;
9002983Swnj 	register struct uba_device *ui;
9012395Swnj 	register sc21, unit;
902286Sbill 
9032646Swnj 	for (sc21 = 0; sc21 < NSC; sc21++) {
9042470Swnj 		if ((um = upminfo[sc21]) == 0 || um->um_ubanum != uban ||
9052470Swnj 		    um->um_alive == 0)
9062395Swnj 			continue;
9072931Swnj 		printf(" sc%d", sc21);
9082395Swnj 		um->um_tab.b_active = 0;
9092395Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
9102931Swnj 		up_softc[sc21].sc_recal = 0;
9116346Swnj 		up_softc[sc21].sc_wticks = 0;
9122571Swnj 		if (um->um_ubinfo) {
9132571Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
9149357Ssam 			um->um_ubinfo = 0;
9152395Swnj 		}
9163445Sroot 		((struct updevice *)(um->um_addr))->upcs2 = UPCS2_CLR;
9172395Swnj 		for (unit = 0; unit < NUP; unit++) {
9182395Swnj 			if ((ui = updinfo[unit]) == 0)
9192395Swnj 				continue;
9202931Swnj 			if (ui->ui_alive == 0 || ui->ui_mi != um)
9212395Swnj 				continue;
9222395Swnj 			uputab[unit].b_active = 0;
9232395Swnj 			(void) upustart(ui);
9242395Swnj 		}
9252395Swnj 		(void) upstart(um);
926286Sbill 	}
927286Sbill }
928313Sbill 
929313Sbill /*
930313Sbill  * Wake up every second and if an interrupt is pending
931313Sbill  * but nothing has happened increment a counter.
9322931Swnj  * If nothing happens for 20 seconds, reset the UNIBUS
933313Sbill  * and begin anew.
934313Sbill  */
935313Sbill upwatch()
936313Sbill {
9372983Swnj 	register struct uba_ctlr *um;
9382395Swnj 	register sc21, unit;
9392470Swnj 	register struct up_softc *sc;
940313Sbill 
9412759Swnj 	timeout(upwatch, (caddr_t)0, hz);
9422646Swnj 	for (sc21 = 0; sc21 < NSC; sc21++) {
9432395Swnj 		um = upminfo[sc21];
9442470Swnj 		if (um == 0 || um->um_alive == 0)
9452470Swnj 			continue;
9462470Swnj 		sc = &up_softc[sc21];
9472395Swnj 		if (um->um_tab.b_active == 0) {
9482395Swnj 			for (unit = 0; unit < NUP; unit++)
9492629Swnj 				if (uputab[unit].b_active &&
9502629Swnj 				    updinfo[unit]->ui_mi == um)
9512395Swnj 					goto active;
9522470Swnj 			sc->sc_wticks = 0;
9532395Swnj 			continue;
9542395Swnj 		}
9552931Swnj active:
9562470Swnj 		sc->sc_wticks++;
9572470Swnj 		if (sc->sc_wticks >= 20) {
9582470Swnj 			sc->sc_wticks = 0;
9592931Swnj 			printf("sc%d: lost interrupt\n", sc21);
9602646Swnj 			ubareset(um->um_ubanum);
9612395Swnj 		}
962313Sbill 	}
963313Sbill }
9642379Swnj 
9652379Swnj #define	DBSIZE	20
9662379Swnj 
9672379Swnj updump(dev)
9682379Swnj 	dev_t dev;
9692379Swnj {
9702629Swnj 	struct updevice *upaddr;
9712379Swnj 	char *start;
9723107Swnj 	int num, blk, unit;
9732379Swnj 	struct size *sizes;
9742395Swnj 	register struct uba_regs *uba;
9752983Swnj 	register struct uba_device *ui;
9762379Swnj 	register short *rp;
9772395Swnj 	struct upst *st;
9786848Ssam 	register int retry;
9792379Swnj 
9802395Swnj 	unit = minor(dev) >> 3;
9812889Swnj 	if (unit >= NUP)
9822889Swnj 		return (ENXIO);
9832470Swnj #define	phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
9842983Swnj 	ui = phys(struct uba_device *, updinfo[unit]);
9852889Swnj 	if (ui->ui_alive == 0)
9862889Swnj 		return (ENXIO);
9872395Swnj 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
9882983Swnj 	ubainit(uba);
9892629Swnj 	upaddr = (struct updevice *)ui->ui_physaddr;
9906848Ssam 	DELAY(5000000);
9912379Swnj 	num = maxfree;
9922379Swnj 	upaddr->upcs2 = unit;
9932983Swnj 	DELAY(100);
9946848Ssam 	upaddr->upcs1 = UP_DCLR|UP_GO;
9956848Ssam 	upaddr->upcs1 = UP_PRESET|UP_GO;
9966848Ssam 	upaddr->upof = UPOF_FMT22;
9976848Ssam 	retry = 0;
9986848Ssam 	do {
9996848Ssam 		DELAY(25);
10006848Ssam 		if (++retry > 527)
10016848Ssam 			break;
10026861Ssam 	} while ((upaddr->upds & UP_RDY) == 0);
10033445Sroot 	if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY)
10042889Swnj 		return (EFAULT);
10059357Ssam 	start = 0;
10068489Sroot 	st = &upst[ui->ui_type];
10072395Swnj 	sizes = phys(struct size *, st->sizes);
10082889Swnj 	if (dumplo < 0 || dumplo + num >= sizes[minor(dev)&07].nblocks)
10092889Swnj 		return (EINVAL);
10102379Swnj 	while (num > 0) {
10112379Swnj 		register struct pte *io;
10122379Swnj 		register int i;
10132379Swnj 		int cn, sn, tn;
10142379Swnj 		daddr_t bn;
10152379Swnj 
10162379Swnj 		blk = num > DBSIZE ? DBSIZE : num;
10172395Swnj 		io = uba->uba_map;
10182379Swnj 		for (i = 0; i < blk; i++)
10192983Swnj 			*(int *)io++ = (btop(start)+i) | (1<<21) | UBAMR_MRV;
10202379Swnj 		*(int *)io = 0;
10212379Swnj 		bn = dumplo + btop(start);
10222607Swnj 		cn = bn/st->nspc + sizes[minor(dev)&07].cyloff;
10232607Swnj 		sn = bn%st->nspc;
10242607Swnj 		tn = sn/st->nsect;
10252607Swnj 		sn = sn%st->nsect;
10262379Swnj 		upaddr->updc = cn;
10272379Swnj 		rp = (short *) &upaddr->upda;
10282379Swnj 		*rp = (tn << 8) + sn;
10292379Swnj 		*--rp = 0;
10302379Swnj 		*--rp = -blk*NBPG / sizeof (short);
10312629Swnj 		*--rp = UP_GO|UP_WCOM;
10326848Ssam 		retry = 0;
10332379Swnj 		do {
10342379Swnj 			DELAY(25);
10356848Ssam 			if (++retry > 527)
10366848Ssam 				break;
10372629Swnj 		} while ((upaddr->upcs1 & UP_RDY) == 0);
10386848Ssam 		if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
10396861Ssam 			printf("up%d: not ready", unit);
10406848Ssam 			if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
10416848Ssam 				printf("\n");
10426848Ssam 				return (EIO);
10436848Ssam 			}
10446848Ssam 			printf(" (flakey)\n");
10456848Ssam 		}
10463445Sroot 		if (upaddr->upds&UPDS_ERR)
10472889Swnj 			return (EIO);
10482379Swnj 		start += blk*NBPG;
10492379Swnj 		num -= blk;
10502379Swnj 	}
10512379Swnj 	return (0);
10522379Swnj }
1053*12505Ssam 
1054*12505Ssam upsize(dev)
1055*12505Ssam 	dev_t dev;
1056*12505Ssam {
1057*12505Ssam 	int unit = minor(dev) >> 3;
1058*12505Ssam 	struct uba_device *ui;
1059*12505Ssam 	struct upst *st;
1060*12505Ssam 
1061*12505Ssam 	if (unit >= NUP || (ui = updinfo[unit]) == 0 || ui->ui_alive == 0)
1062*12505Ssam 		return (-1);
1063*12505Ssam 	st = &upst[ui->ui_type];
1064*12505Ssam 	return (st->sizes[minor(dev) & 07].nblocks);
1065*12505Ssam }
10661902Swnj #endif
1067