xref: /csrg-svn/sys/vax/mba/hp.c (revision 6927)
1*6927Ssam /*	hp.c	4.48	82/05/24	*/
23706Sroot 
33706Sroot #ifdef HPDEBUG
43289Swnj int	hpdebug;
53706Sroot #endif
63706Sroot #ifdef HPBDEBUG
73706Sroot int	hpbdebug;
83706Sroot #endif
921Sbill 
101939Swnj #include "hp.h"
111565Sbill #if NHP > 0
1221Sbill /*
135726Sroot  * HP disk driver for RP0x+RMxx+ML11
142827Swnj  *
152827Swnj  * TODO:
163706Sroot  *	check RM80 skip sector handling when ECC's occur later
173093Swnj  *	check offset recovery handling
183706Sroot  *	see if DCLR and/or RELEASE set attention status
193706Sroot  *	print bits of mr && mr2 symbolically
2021Sbill  */
2121Sbill 
2221Sbill #include "../h/param.h"
2321Sbill #include "../h/systm.h"
24305Sbill #include "../h/dk.h"
2521Sbill #include "../h/buf.h"
2621Sbill #include "../h/conf.h"
2721Sbill #include "../h/dir.h"
2821Sbill #include "../h/user.h"
2921Sbill #include "../h/map.h"
30420Sbill #include "../h/pte.h"
312978Swnj #include "../h/mbareg.h"
322978Swnj #include "../h/mbavar.h"
3321Sbill #include "../h/mtpr.h"
34420Sbill #include "../h/vm.h"
352362Swnj #include "../h/cmap.h"
363706Sroot #include "../h/dkbad.h"
375726Sroot #include "../h/dkio.h"
3821Sbill 
392383Swnj #include "../h/hpreg.h"
4021Sbill 
412383Swnj /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
422383Swnj struct	size {
4321Sbill 	daddr_t	nblocks;
4421Sbill 	int	cyloff;
453706Sroot } hp6_sizes[8] = {
46886Sbill 	15884,	0,		/* A=cyl 0 thru 37 */
47886Sbill 	33440,	38,		/* B=cyl 38 thru 117 */
48886Sbill 	340670,	0,		/* C=cyl 0 thru 814 */
4921Sbill 	0,	0,
5021Sbill 	0,	0,
5121Sbill 	0,	0,
523726Sroot #ifndef NOBADSECT
533729Sroot 	291280,	118,		/* G=cyl 118 thru 814 */
543706Sroot #else
553706Sroot 	291346,	118,
563706Sroot #endif
5721Sbill 	0,	0,
583706Sroot }, rm3_sizes[8] = {
59886Sbill 	15884,	0,		/* A=cyl 0 thru 99 */
60886Sbill 	33440,	100,		/* B=cyl 100 thru 309 */
61886Sbill 	131680,	0,		/* C=cyl 0 thru 822 */
6221Sbill 	0,	0,
6321Sbill 	0,	0,
643442Sroot 	0,	0,
653726Sroot #ifndef NOBADSECT
663729Sroot 	81984,	310,		/* G=cyl 310 thru 822 */
673706Sroot #else
683706Sroot 	82080,	310,
693706Sroot #endif
7021Sbill 	0,	0,
71886Sbill }, rm5_sizes[8] = {
72*6927Ssam #ifndef CAD
73886Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
74886Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
753271Swnj 	500384,	0,		/* C=cyl 0 thru 822 */
76886Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
77886Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
783726Sroot #ifndef NOBADSECT
793729Sroot 	86240,	681,		/* F=cyl 681 thru 822 */
803729Sroot 	158592,	562,		/* G=cyl 562 thru 822 */
813706Sroot #else
825695Sroot 	86336,	681,
833706Sroot 	158688,	562,
843706Sroot #endif
85886Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
86*6927Ssam #else
87*6927Ssam 	15884,	0,		/* A=cyl 0 thru 26 */
88*6927Ssam 	33440,	27,		/* B=cyl 27 thru 81 */
89*6927Ssam 	495520,	0,		/* C=cyl 0 thru 814 */
90*6927Ssam 	15884,	562,		/* D=cyl 562 thru 588 */
91*6927Ssam 	55936,	589,		/* E=cyl 589 thru 680 */
92*6927Ssam #ifndef NOBADSECT
93*6927Ssam 	81376,	681,		/* F=cyl 681 thru 814 */
94*6927Ssam 	153728,	562,		/* G=cyl 562 thru 814 */
95*6927Ssam #else
96*6927Ssam 	81472,	681,
97*6927Ssam 	153824,	562,
98*6927Ssam #endif
99*6927Ssam 	291346,	82,		/* H=cyl 82 thru 561 */
100*6927Ssam #endif
1012383Swnj }, rm80_sizes[8] = {
1022383Swnj 	15884,	0,		/* A=cyl 0 thru 36 */
1032383Swnj 	33440,	37,		/* B=cyl 37 thru 114 */
1042383Swnj 	242606,	0,		/* C=cyl 0 thru 558 */
1052383Swnj 	0,	0,
1062383Swnj 	0,	0,
1072383Swnj 	0,	0,
1082383Swnj 	82080,	115,		/* G=cyl 115 thru 304 */
1093729Sroot 	110143,	305,		/* H=cyl 305 thru 558 */
1103706Sroot }, hp7_sizes[8] = {
111*6927Ssam 	15884,	0,		/* A=cyl 0 thru 9 */
1123706Sroot 	64000,	10,		/* B=cyl 10 thru 49 */
1133706Sroot 	1008000,0,		/* C=cyl 0 thru 629 */
1143706Sroot 	15884,	330,		/* D=cyl 330 thru 339 */
1153706Sroot 	256000,	340,		/* E=cyl 340 thru 499 */
1163729Sroot 	207850,	500,		/* F=cyl 500 thru 629 */
1173729Sroot 	479850,	330,		/* G=cyl 330 thru 629 */
1183706Sroot 	448000,	50,		/* H=cyl 50 thru 329 */
119*6927Ssam }, si9775_sizes[8] = {
120*6927Ssam 	16640,	  0,		/* A=cyl   0 thru  12 */
121*6927Ssam 	34560,	 13,		/* B=cyl  13 thru  39 */
122*6927Ssam 	1079040,  0,		/* C=cyl   0 thru 842 - whole disk */
123*6927Ssam 	0,	  0,		/* D unused */
124*6927Ssam 	0,	  0,		/* E unused */
125*6927Ssam 	0,	  0,		/* F unused */
126*6927Ssam 	513280,	 40,		/* G=cyl  40 thru 440 */
127*6927Ssam 	513280,	441,		/* H=cyl 441 thru 841 */
128*6927Ssam }, si9730_sizes[8] = {
129*6927Ssam 	15884,	0,		/* A=cyl 0 thru 49 */
130*6927Ssam 	33440,	50,		/* B=cyl 50 thru 154 */
131*6927Ssam 	263360,	0,		/* C=cyl 0 thru 822 */
132*6927Ssam 	0,	0,
133*6927Ssam 	0,	0,
134*6927Ssam 	0,	0,
135*6927Ssam 	0,	0,
136*6927Ssam #ifndef NOBADSECT
137*6927Ssam 	213664,	155,		/* H=cyl 155 thru 822 */
138*6927Ssam #else
139*6927Ssam 	213760,	155,
140*6927Ssam #endif
141*6927Ssam }, hpam_sizes[8] = {
142*6927Ssam 	15884,	0,		/* A=cyl 0 thru 31 */
143*6927Ssam 	33440,	32,		/* B=cyl 32 thru 97 */
144*6927Ssam 	524288,	0,		/* C=cyl 0 thru 1023 */
145*6927Ssam 	27786,	668,
146*6927Ssam 	27786,	723,
147*6927Ssam 	125440,	778,
148*6927Ssam 	181760,	668,		/* G=cyl 668 thru 1022 */
149*6927Ssam 	291346,	98,		/* H=cyl 98 thru 667 */
15021Sbill };
1512383Swnj /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
15221Sbill 
1532383Swnj #define	_hpSDIST	2
1542383Swnj #define	_hpRDIST	3
1552383Swnj 
1562383Swnj int	hpSDIST = _hpSDIST;
1572383Swnj int	hpRDIST = _hpRDIST;
1582383Swnj 
159*6927Ssam /*
160*6927Ssam  * Table for converting Massbus drive types into
161*6927Ssam  * indices into the partition tables.  Slots are
162*6927Ssam  * left for those drives devined from other means
163*6927Ssam  * (e.g. SI, AMPEX, etc.).
164*6927Ssam  */
165*6927Ssam short	hptypes[] = {
166*6927Ssam #define	HPDT_RM03	0
167*6927Ssam 	MBDT_RM03,
168*6927Ssam #define	HPDT_RM05	1
169*6927Ssam 	MBDT_RM05,
170*6927Ssam #define	HPDT_RP06	2
171*6927Ssam 	MBDT_RP06,
172*6927Ssam #define	HPDT_RM80	3
173*6927Ssam 	MBDT_RM80,
174*6927Ssam #define	HPDT_RP05	4
175*6927Ssam 	MBDT_RP05,
176*6927Ssam #define	HPDT_RP07	5
177*6927Ssam 	MBDT_RP07,
178*6927Ssam #define	HPDT_ML11A	6
179*6927Ssam 	MBDT_ML11A,
180*6927Ssam #define	HPDT_ML11B	7
181*6927Ssam 	MBDT_ML11B,
182*6927Ssam #define	HPDT_9775	8
183*6927Ssam 	-1,
184*6927Ssam #define	HPDT_9730	9
185*6927Ssam 	-1,
186*6927Ssam #define	HPDT_CAPRICORN	10
187*6927Ssam 	-1,
188*6927Ssam #define	HPDT_RM02	11
189*6927Ssam 	MBDT_RM02,		/* beware, actually capricorn */
190*6927Ssam 	0
191*6927Ssam };
1922978Swnj struct	mba_device *hpinfo[NHP];
1932978Swnj int	hpattach(),hpustart(),hpstart(),hpdtint();
1942383Swnj struct	mba_driver hpdriver =
1952978Swnj 	{ hpattach, 0, hpustart, hpstart, hpdtint, 0,
1962978Swnj 	  hptypes, "hp", 0, hpinfo };
1972383Swnj 
1982383Swnj struct hpst {
1992383Swnj 	short	nsect;
2002383Swnj 	short	ntrak;
2012383Swnj 	short	nspc;
2022383Swnj 	short	ncyl;
2032383Swnj 	struct	size *sizes;
2042383Swnj } hpst[] = {
2053706Sroot 	32,	5,	32*5,	823,	rm3_sizes,	/* RM03 */
2062383Swnj 	32,	19,	32*19,	823,	rm5_sizes,	/* RM05 */
2073706Sroot 	22,	19,	22*19,	815,	hp6_sizes,	/* RP06 */
2083706Sroot 	31,	14, 	31*14,	559,	rm80_sizes,	/* RM80 */
2093706Sroot 	22,	19,	22*19,	411,	hp6_sizes,	/* RP05 */
2103706Sroot 	50,	32,	50*32,	630,	hp7_sizes,	/* RP07 */
2115726Sroot 	1,	1,	1,	1,	0,		/* ML11A */
2125726Sroot 	1,	1,	1,	1,	0,		/* ML11B */
213*6927Ssam 	32,	40,	32*40,	843,	si9775_sizes,	/* 9775 */
214*6927Ssam 	32,	10,	32*10,	823,	si9730_sizes,	/* 9730 */
215*6927Ssam 	32,	16,	32*16,	1024,	hpam_sizes,	/* AMPEX capricorn */
2162383Swnj };
2172383Swnj 
2182624Swnj u_char	hp_offset[16] = {
2193093Swnj     HPOF_P400, HPOF_M400, HPOF_P400, HPOF_M400,
2203093Swnj     HPOF_P800, HPOF_M800, HPOF_P800, HPOF_M800,
2213093Swnj     HPOF_P1200, HPOF_M1200, HPOF_P1200, HPOF_M1200,
2223093Swnj     0, 0, 0, 0,
22321Sbill };
22421Sbill 
2252624Swnj struct	buf	rhpbuf[NHP];
2263726Sroot #ifndef NOBADSECT
2273706Sroot struct	buf	bhpbuf[NHP];
2283706Sroot struct	dkbad	hpbad[NHP];
2293706Sroot #endif
2305726Sroot /* SHOULD CONSOLIDATE ALL THIS STUFF INTO A STRUCTURE */
2313706Sroot char	hpinit[NHP];
2322892Swnj char	hprecal[NHP];
2335726Sroot char	hphdr[NHP];
2345726Sroot daddr_t	mlsize[NHP];
23521Sbill 
23621Sbill #define	b_cylin b_resid
23721Sbill 
2385726Sroot /* #define ML11 0  to remove ML11 support */
2395726Sroot #define	ML11	(hptypes[mi->mi_type] == MBDT_ML11A)
2405726Sroot #define	RP06	(hptypes[mi->mi_type] <= MBDT_RP06)
2415726Sroot #define	RM80	(hptypes[mi->mi_type] == MBDT_RM80)
2425726Sroot 
24321Sbill #ifdef INTRLVE
24421Sbill daddr_t dkblock();
24521Sbill #endif
24621Sbill 
2472604Swnj int	hpseek;
2482604Swnj 
2492978Swnj /*ARGSUSED*/
2502978Swnj hpattach(mi, slave)
2512978Swnj 	struct mba_device *mi;
2522604Swnj {
253*6927Ssam 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
254*6927Ssam 
255*6927Ssam 	switch (mi->mi_type) {
256*6927Ssam 
257*6927Ssam 	/*
258*6927Ssam 	 * Model-byte processing for SI 9400 controllers.
259*6927Ssam 	 * NB:  Only deals with RM03 and RM05 emulations.
260*6927Ssam 	 */
261*6927Ssam 	case HPDT_RM03:
262*6927Ssam 	case HPDT_RM05: {
263*6927Ssam 		register int hpsn;
264*6927Ssam 
265*6927Ssam 		hpsn = hpaddr->hpsn;
266*6927Ssam 		if ((hpsn & SIMB_LU) != mi->mi_drive)
267*6927Ssam 			break;
268*6927Ssam 		switch ((hpsn & SIMB_MB) & ~(SIMB_S6|SIRM03|SIRM05)) {
269*6927Ssam 
270*6927Ssam 		case SI9775D:
271*6927Ssam 			printf("hp%d: si 9775 (direct)\n", mi->mi_unit);
272*6927Ssam 			mi->mi_type = HPDT_9775;
273*6927Ssam 			break;
274*6927Ssam 
275*6927Ssam 		case SI9730D:
276*6927Ssam 			printf("hp%d: si 9730 (direct)\n", mi->mi_unit);
277*6927Ssam 			mi->mi_type = HPDT_9730;
278*6927Ssam 			break;
279*6927Ssam 
280*6927Ssam #ifdef CAD
281*6927Ssam 		case SI9766:
282*6927Ssam 			printf("hp%d: 9776/9300\n", mi->mi_unit);
283*6927Ssam 			mi->mi_type = HPDT_RM05;
284*6927Ssam 			break;
285*6927Ssam 
286*6927Ssam 		case SI9762:
287*6927Ssam 			printf("hp%d: 9762\n", mi->mi_unit);
288*6927Ssam 			mi->mi_type = HPDT_RM03;
289*6927Ssam 			break;
290*6927Ssam #endif
291*6927Ssam 		}
292*6927Ssam 		break;
293*6927Ssam 		}
294*6927Ssam 
295*6927Ssam 	/*
296*6927Ssam 	 * CAPRICORN KLUDGE...poke the holding register
297*6927Ssam 	 * to find out the number of tracks.  If it's 15
298*6927Ssam 	 * we believe it's a Capricorn.
299*6927Ssam 	 */
300*6927Ssam 	case HPDT_RM02:
301*6927Ssam 		hpaddr->hpcs1 = HP_NOP;
302*6927Ssam 		hpaddr->hphr = HPHR_MAXTRAK;
303*6927Ssam 		if (hpaddr->hphr == 15) {
304*6927Ssam 			printf("hp%d: capricorn\n", mi->mi_unit);
305*6927Ssam 			mi->mi_type = HPDT_CAPRICORN;
306*6927Ssam 		}
307*6927Ssam 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
308*6927Ssam 		break;
309*6927Ssam 
310*6927Ssam 	case HPDT_ML11A:
311*6927Ssam 	case HPDT_ML11B: {
3125726Sroot 		register int trt, sz;
3132604Swnj 
3145726Sroot 		sz = hpaddr->hpmr & HPMR_SZ;
3155726Sroot 		if ((hpaddr->hpmr & HPMR_ARRTYP) == 0)
3165726Sroot 			sz >>= 2;
3175726Sroot 		mlsize[mi->mi_unit] = sz;
3185726Sroot 		if (mi->mi_dk >= 0) {
3195726Sroot 			trt = (hpaddr->hpmr & HPMR_TRT) >> 8;
3205726Sroot 			dk_mspw[mi->mi_dk] = 1.0 / (1<<(20-trt));
3215726Sroot 		}
322*6927Ssam 		/* A CHEAT - ML11B D.T. SHOULD == ML11A */
323*6927Ssam 		mi->mi_type = HPDT_ML11A;
324*6927Ssam 		break;
325*6927Ssam 		}
326*6927Ssam 	}
327*6927Ssam 	if (!ML11 && mi->mi_dk >= 0) {
3285726Sroot 		register struct hpst *st = &hpst[mi->mi_type];
3295726Sroot 
3302757Swnj 		dk_mspw[mi->mi_dk] = 1.0 / 60 / (st->nsect * 256);
3315726Sroot 	}
3322604Swnj }
3332604Swnj 
33421Sbill hpstrategy(bp)
3352383Swnj 	register struct buf *bp;
33621Sbill {
3372978Swnj 	register struct mba_device *mi;
3382383Swnj 	register struct hpst *st;
3392383Swnj 	register int unit;
34021Sbill 	long sz, bn;
3412383Swnj 	int xunit = minor(bp->b_dev) & 07;
3425432Sroot 	int s;
34321Sbill 
34421Sbill 	sz = bp->b_bcount;
34521Sbill 	sz = (sz+511) >> 9;
34621Sbill 	unit = dkunit(bp);
3472383Swnj 	if (unit >= NHP)
3482383Swnj 		goto bad;
3492383Swnj 	mi = hpinfo[unit];
3502395Swnj 	if (mi == 0 || mi->mi_alive == 0)
3512383Swnj 		goto bad;
3522383Swnj 	st = &hpst[mi->mi_type];
3535726Sroot 	if (ML11) {
3545726Sroot 		if (bp->b_blkno < 0 ||
3555726Sroot 		    dkblock(bp)+sz > mlsize[mi->mi_unit])
3565726Sroot 			goto bad;
3575726Sroot 		bp->b_cylin = 0;
3585726Sroot 	} else {
3595726Sroot 		if (bp->b_blkno < 0 ||
3605726Sroot 		    (bn = dkblock(bp))+sz > st->sizes[xunit].nblocks)
3615726Sroot 			goto bad;
3625726Sroot 		bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff;
3635726Sroot 	}
3645432Sroot 	s = spl5();
3652383Swnj 	disksort(&mi->mi_tab, bp);
3662383Swnj 	if (mi->mi_tab.b_active == 0)
3672383Swnj 		mbustart(mi);
3685432Sroot 	splx(s);
3692383Swnj 	return;
3702383Swnj 
3712383Swnj bad:
3722383Swnj 	bp->b_flags |= B_ERROR;
3732383Swnj 	iodone(bp);
3742383Swnj 	return;
37521Sbill }
37621Sbill 
3772383Swnj hpustart(mi)
3782978Swnj 	register struct mba_device *mi;
37921Sbill {
3802624Swnj 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
3812383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
3823706Sroot 	register struct hpst *st = &hpst[mi->mi_type];
38321Sbill 	daddr_t bn;
3843102Swnj 	int sn, dist;
38521Sbill 
3863706Sroot 	hpaddr->hpcs1 = 0;
3872624Swnj 	if ((hpaddr->hpcs1&HP_DVA) == 0)
3882383Swnj 		return (MBU_BUSY);
3893706Sroot 	if ((hpaddr->hpds & HPDS_VV) == 0 || hpinit[mi->mi_unit] == 0) {
3903726Sroot #ifndef NOBADSECT
3913706Sroot 		struct buf *bbp = &bhpbuf[mi->mi_unit];
3923706Sroot #endif
3933706Sroot 
3943706Sroot 		hpinit[mi->mi_unit] = 1;
3952624Swnj 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
3963140Swnj 		if (mi->mi_mba->mba_drv[0].mbd_as & (1<<mi->mi_drive))
3973140Swnj 			printf("DCLR attn\n");
3982624Swnj 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
3995726Sroot 		if (!ML11)
4005726Sroot 			hpaddr->hpof = HPOF_FMT22;
4013140Swnj 		mbclrattn(mi);
4023726Sroot #ifndef NOBADSECT
4035726Sroot 		if (!ML11) {
4045726Sroot 			bbp->b_flags = B_READ|B_BUSY;
4055726Sroot 			bbp->b_dev = bp->b_dev;
4065726Sroot 			bbp->b_bcount = 512;
4075726Sroot 			bbp->b_un.b_addr = (caddr_t)&hpbad[mi->mi_unit];
4085726Sroot 			bbp->b_blkno = st->ncyl*st->nspc - st->nsect;
4095726Sroot 			bbp->b_cylin = st->ncyl - 1;
4105726Sroot 			mi->mi_tab.b_actf = bbp;
4115726Sroot 			bbp->av_forw = bp;
4125726Sroot 			bp = bbp;
4135726Sroot 		}
4143706Sroot #endif
41521Sbill 	}
4162604Swnj 	if (mi->mi_tab.b_active || mi->mi_hd->mh_ndrive == 1)
4172383Swnj 		return (MBU_DODATA);
4185726Sroot 	if (ML11)
4195726Sroot 		return (MBU_DODATA);
4203093Swnj 	if ((hpaddr->hpds & HPDS_DREADY) != HPDS_DREADY)
4212383Swnj 		return (MBU_DODATA);
4222395Swnj 	bn = dkblock(bp);
4232395Swnj 	sn = bn%st->nspc;
4242395Swnj 	sn = (sn+st->nsect-hpSDIST)%st->nsect;
4252383Swnj 	if (bp->b_cylin == (hpaddr->hpdc & 0xffff)) {
4262604Swnj 		if (hpseek)
4272383Swnj 			return (MBU_DODATA);
4282383Swnj 		dist = ((hpaddr->hpla & 0xffff)>>6) - st->nsect + 1;
4292383Swnj 		if (dist < 0)
4302383Swnj 			dist += st->nsect;
4312383Swnj 		if (dist > st->nsect - hpRDIST)
4322383Swnj 			return (MBU_DODATA);
4332614Swnj 	} else
4342614Swnj 		hpaddr->hpdc = bp->b_cylin;
4352604Swnj 	if (hpseek)
4362624Swnj 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
437305Sbill 	else {
438305Sbill 		hpaddr->hpda = sn;
4392624Swnj 		hpaddr->hpcs1 = HP_SEARCH|HP_GO;
440305Sbill 	}
4412383Swnj 	return (MBU_STARTED);
44221Sbill }
44321Sbill 
4442383Swnj hpstart(mi)
4452978Swnj 	register struct mba_device *mi;
44621Sbill {
4472624Swnj 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
4482383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
4492383Swnj 	register struct hpst *st = &hpst[mi->mi_type];
45021Sbill 	daddr_t bn;
4512383Swnj 	int sn, tn;
45221Sbill 
45321Sbill 	bn = dkblock(bp);
4545726Sroot 	if (ML11)
4555726Sroot 		hpaddr->hpda = bn;
4565726Sroot 	else {
4575726Sroot 		sn = bn%st->nspc;
4585726Sroot 		tn = sn/st->nsect;
4595726Sroot 		sn %= st->nsect;
4605726Sroot 		hpaddr->hpdc = bp->b_cylin;
4615726Sroot 		hpaddr->hpda = (tn << 8) + sn;
4625726Sroot 	}
4635726Sroot 	if (hphdr[mi->mi_unit]) {
4645726Sroot 		if (bp->b_flags & B_READ)
4655726Sroot 			return (HP_RHDR|HP_GO);
4665726Sroot 		else
4675726Sroot 			return (HP_WHDR|HP_GO);
4685726Sroot 	}
4695726Sroot 	return (0);
47021Sbill }
47121Sbill 
4723102Swnj hpdtint(mi, mbsr)
4732978Swnj 	register struct mba_device *mi;
4743102Swnj 	int mbsr;
47521Sbill {
4762624Swnj 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
4772383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
4785893Swnj 	register int er1, er2;
4792826Swnj 	int retry = 0;
48021Sbill 
4813726Sroot #ifndef NOBADSECT
4823706Sroot 	if (bp->b_flags&B_BAD) {
4833706Sroot 		if (hpecc(mi, CONT))
4843706Sroot 			return(MBD_RESTARTED);
4853706Sroot 	}
4863706Sroot #endif
4873102Swnj 	if (hpaddr->hpds&HPDS_ERR || mbsr&MBSR_EBITS) {
4883706Sroot #ifdef HPDEBUG
4893289Swnj 		if (hpdebug) {
4903706Sroot 			int dc = hpaddr->hpdc, da = hpaddr->hpda;
4913706Sroot 
4923706Sroot 			printf("hperr: bp %x cyl %d blk %d as %o ",
4933706Sroot 				bp, bp->b_cylin, bp->b_blkno,
4943706Sroot 				hpaddr->hpas&0xff);
4953706Sroot 			printf("dc %x da %x\n",dc&0xffff, da&0xffff);
4963289Swnj 			printf("errcnt %d ", mi->mi_tab.b_errcnt);
4973289Swnj 			printf("mbsr=%b ", mbsr, mbsr_bits);
4983289Swnj 			printf("er1=%b er2=%b\n",
4993289Swnj 			    hpaddr->hper1, HPER1_BITS,
5003289Swnj 			    hpaddr->hper2, HPER2_BITS);
5013289Swnj 			DELAY(1000000);
5023289Swnj 		}
5033706Sroot #endif
5045858Swnj 		er1 = hpaddr->hper1;
5055893Swnj 		er2 = hpaddr->hper2;
5065893Swnj 		if (er1 & HPER1_HCRC) {
5075858Swnj 			er1 &= ~(HPER1_HCE|HPER1_FER);
5085893Swnj 			er2 &= ~HPER2_BSE;
5095893Swnj 		}
5105858Swnj 		if (er1&HPER1_WLE) {
5112925Swnj 			printf("hp%d: write locked\n", dkunit(bp));
5122826Swnj 			bp->b_flags |= B_ERROR;
5135858Swnj 		} else if ((er1&0xffff) == HPER1_FER && RP06 &&
5145726Sroot 		    hphdr[mi->mi_unit] == 0) {
5155726Sroot #ifndef NOBADSECT
5165726Sroot 			if (hpecc(mi, BSE))
5175726Sroot 				return(MBD_RESTARTED);
5185726Sroot 			else
5195726Sroot #endif
5205726Sroot 				goto hard;
5212826Swnj 		} else if (++mi->mi_tab.b_errcnt > 27 ||
5223102Swnj 		    mbsr & MBSR_HARD ||
5235858Swnj 		    er1 & HPER1_HARD ||
5245726Sroot 		    hphdr[mi->mi_unit] ||
5255893Swnj 		    (!ML11 && (er2 & HPER2_HARD))) {
5263706Sroot hard:
5272925Swnj 			harderr(bp, "hp");
5283271Swnj 			if (mbsr & (MBSR_EBITS &~ (MBSR_DTABT|MBSR_MBEXC)))
5293271Swnj 				printf("mbsr=%b ", mbsr, mbsr_bits);
5303706Sroot 			printf("er1=%b er2=%b",
5312826Swnj 			    hpaddr->hper1, HPER1_BITS,
5322826Swnj 			    hpaddr->hper2, HPER2_BITS);
5333706Sroot 			if (hpaddr->hpmr)
5343706Sroot 				printf(" mr=%o", hpaddr->hpmr&0xffff);
5353706Sroot 			if (hpaddr->hpmr2)
5363706Sroot 				printf(" mr2=%o", hpaddr->hpmr2&0xffff);
5373706Sroot 			printf("\n");
5382826Swnj 			bp->b_flags |= B_ERROR;
5393143Swnj 			hprecal[mi->mi_unit] = 0;
5405893Swnj 		} else if ((er2 & HPER2_BSE) && !ML11) {
5413726Sroot #ifndef NOBADSECT
5423706Sroot 			if (hpecc(mi, BSE))
5433706Sroot 				return(MBD_RESTARTED);
5443706Sroot 			else
5453706Sroot #endif
5463706Sroot 				goto hard;
5475893Swnj 		} else if (RM80 && er2&HPER2_SSE) {
5483988Sroot 			(void) hpecc(mi, SSE);
5492883Swnj 			return (MBD_RESTARTED);
5505858Swnj 		} else if ((er1&(HPER1_DCK|HPER1_ECH))==HPER1_DCK) {
5513706Sroot 			if (hpecc(mi, ECC))
5522383Swnj 				return (MBD_RESTARTED);
5532826Swnj 			/* else done */
5542826Swnj 		} else
5552826Swnj 			retry = 1;
5562826Swnj 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
5575726Sroot 		if (ML11) {
5585726Sroot 			if (mi->mi_tab.b_errcnt >= 16)
5595726Sroot 				goto hard;
5605726Sroot 		} else if ((mi->mi_tab.b_errcnt&07) == 4) {
5612826Swnj 			hpaddr->hpcs1 = HP_RECAL|HP_GO;
5623706Sroot 			hprecal[mi->mi_unit] = 1;
5633706Sroot 			return(MBD_RESTARTED);
56421Sbill 		}
5652826Swnj 		if (retry)
5662826Swnj 			return (MBD_RETRY);
5672826Swnj 	}
5683706Sroot #ifdef HPDEBUG
5693289Swnj 	else
5703289Swnj 		if (hpdebug && hprecal[mi->mi_unit]) {
5713289Swnj 			printf("recal %d ", hprecal[mi->mi_unit]);
5723289Swnj 			printf("errcnt %d\n", mi->mi_tab.b_errcnt);
5733289Swnj 			printf("mbsr=%b ", mbsr, mbsr_bits);
5743289Swnj 			printf("er1=%b er2=%b\n",
5753289Swnj 			    hpaddr->hper1, HPER1_BITS,
5763289Swnj 			    hpaddr->hper2, HPER2_BITS);
5773289Swnj 		}
5783706Sroot #endif
5793093Swnj 	switch (hprecal[mi->mi_unit]) {
5803093Swnj 
5813093Swnj 	case 1:
5823093Swnj 		hpaddr->hpdc = bp->b_cylin;
5833093Swnj 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
5843706Sroot 		hprecal[mi->mi_unit]++;
5853706Sroot 		return (MBD_RESTARTED);
5863093Swnj 	case 2:
5873093Swnj 		if (mi->mi_tab.b_errcnt < 16 ||
5883289Swnj 		    (bp->b_flags & B_READ) == 0)
5893093Swnj 			goto donerecal;
5903093Swnj 		hpaddr->hpof = hp_offset[mi->mi_tab.b_errcnt & 017]|HPOF_FMT22;
5913093Swnj 		hpaddr->hpcs1 = HP_OFFSET|HP_GO;
5923093Swnj 		hprecal[mi->mi_unit]++;
5933093Swnj 		return (MBD_RESTARTED);
5943093Swnj 	donerecal:
5953158Swnj 	case 3:
5962892Swnj 		hprecal[mi->mi_unit] = 0;
5972892Swnj 		return (MBD_RETRY);
5982892Swnj 	}
5995726Sroot 	hphdr[mi->mi_unit] = 0;
6002383Swnj 	bp->b_resid = -(mi->mi_mba->mba_bcr) & 0xffff;
6013640Swnj 	if (mi->mi_tab.b_errcnt >= 16) {
6023093Swnj 		/*
6033093Swnj 		 * This is fast and occurs rarely; we don't
6043093Swnj 		 * bother with interrupts.
6053093Swnj 		 */
6062624Swnj 		hpaddr->hpcs1 = HP_RTC|HP_GO;
6073093Swnj 		while (hpaddr->hpds & HPDS_PIP)
6082383Swnj 			;
6092383Swnj 		mbclrattn(mi);
61021Sbill 	}
6115726Sroot 	if (!ML11) {
6125726Sroot 		hpaddr->hpof = HPOF_FMT22;
6135726Sroot 		hpaddr->hpcs1 = HP_RELEASE|HP_GO;
6145726Sroot 	}
6152383Swnj 	return (MBD_DONE);
61621Sbill }
61721Sbill 
61821Sbill hpread(dev)
6192624Swnj 	dev_t dev;
62021Sbill {
6212624Swnj 	register int unit = minor(dev) >> 3;
62221Sbill 
6232624Swnj 	if (unit >= NHP)
6242624Swnj 		u.u_error = ENXIO;
6252624Swnj 	else
6262624Swnj 		physio(hpstrategy, &rhpbuf[unit], dev, B_READ, minphys);
62721Sbill }
62821Sbill 
62921Sbill hpwrite(dev)
6302624Swnj 	dev_t dev;
63121Sbill {
6322624Swnj 	register int unit = minor(dev) >> 3;
63321Sbill 
6342624Swnj 	if (unit >= NHP)
6352624Swnj 		u.u_error = ENXIO;
6362624Swnj 	else
6372624Swnj 		physio(hpstrategy, &rhpbuf[unit], dev, B_WRITE, minphys);
63821Sbill }
63921Sbill 
6405726Sroot /*ARGSUSED*/
6415726Sroot hpioctl(dev, cmd, addr, flag)
6425726Sroot 	dev_t dev;
6435726Sroot 	int cmd;
6445726Sroot 	caddr_t addr;
6455726Sroot 	int flag;
6465726Sroot {
6475726Sroot 
6485726Sroot 	switch (cmd) {
6495726Sroot 	case DKIOCHDR:	/* do header read/write */
6505726Sroot 		hphdr[minor(dev)>>3] = 1;
6515726Sroot 		return;
6525726Sroot 
6535726Sroot 	default:
6545726Sroot 		u.u_error = ENXIO;
6555726Sroot 	}
6565726Sroot }
6575726Sroot 
6583706Sroot hpecc(mi, flag)
6592978Swnj 	register struct mba_device *mi;
6603706Sroot 	int flag;
66121Sbill {
6622383Swnj 	register struct mba_regs *mbp = mi->mi_mba;
6632624Swnj 	register struct hpdevice *rp = (struct hpdevice *)mi->mi_drv;
6642383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
6653706Sroot 	register struct hpst *st = &hpst[mi->mi_type];
6663706Sroot 	int npf, o;
6672383Swnj 	int bn, cn, tn, sn;
668914Sbill 	int bcr;
66921Sbill 
670914Sbill 	bcr = mbp->mba_bcr & 0xffff;
671914Sbill 	if (bcr)
672914Sbill 		bcr |= 0xffff0000;		/* sxt */
6733726Sroot #ifndef NOBADSECT
6743706Sroot 	if (flag == CONT)
6753706Sroot 		npf = bp->b_error;
6763706Sroot 	else
6773706Sroot #endif
6783706Sroot 		npf = btop(bcr + bp->b_bcount);
679420Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
680420Sbill 	bn = dkblock(bp);
681420Sbill 	cn = bp->b_cylin;
6823706Sroot 	sn = bn%(st->nspc) + npf;
6832383Swnj 	tn = sn/st->nsect;
6842383Swnj 	sn %= st->nsect;
6852383Swnj 	cn += tn/st->ntrak;
6862383Swnj 	tn %= st->ntrak;
6873706Sroot 	switch (flag) {
6883706Sroot 	case ECC:
6893706Sroot 		{
6903706Sroot 		register int i;
6913706Sroot 		caddr_t addr;
6923706Sroot 		struct pte mpte;
6933706Sroot 		int bit, byte, mask;
6943706Sroot 
6953706Sroot 		npf--;		/* because block in error is previous block */
6963706Sroot 		printf("hp%d%c: soft ecc sn%d\n", dkunit(bp),
6973706Sroot 		    'a'+(minor(bp->b_dev)&07), bp->b_blkno + npf);
6983706Sroot 		mask = rp->hpec2&0xffff;
6993706Sroot 		i = (rp->hpec1&0xffff) - 1;		/* -1 makes 0 origin */
7003706Sroot 		bit = i&07;
7013706Sroot 		i = (i&~07)>>3;
7023706Sroot 		byte = i + o;
7033706Sroot 		while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
7043706Sroot 			mpte = mbp->mba_map[npf+btop(byte)];
7053706Sroot 			addr = ptob(mpte.pg_pfnum) + (byte & PGOFSET);
7063706Sroot 			putmemc(addr, getmemc(addr)^(mask<<bit));
7073706Sroot 			byte++;
7083706Sroot 			i++;
7093706Sroot 			bit -= 8;
7103706Sroot 		}
7113706Sroot 		if (bcr == 0)
7123706Sroot 			return (0);
7133847Sroot 		npf++;
7143706Sroot 		break;
7153706Sroot 		}
7163706Sroot 
7173706Sroot 	case SSE:
7183706Sroot 		rp->hpof |= HPOF_SSEI;
7193706Sroot 		mbp->mba_bcr = -(bp->b_bcount - (int)ptob(npf));
7203706Sroot 		break;
7213706Sroot 
7223726Sroot #ifndef NOBADSECT
7233706Sroot 	case BSE:
7243706Sroot #ifdef HPBDEBUG
7253706Sroot 		if (hpbdebug)
7263706Sroot 		printf("hpecc, BSE: bn %d cn %d tn %d sn %d\n", bn, cn, tn, sn);
7273706Sroot #endif
7283706Sroot 		if ((bn = isbad(&hpbad[mi->mi_unit], cn, tn, sn)) < 0)
7293706Sroot 			return(0);
7303706Sroot 		bp->b_flags |= B_BAD;
7313706Sroot 		bp->b_error = npf + 1;
7323706Sroot 		bn = st->ncyl*st->nspc - st->nsect - 1 - bn;
7333706Sroot 		cn = bn/st->nspc;
7343706Sroot 		sn = bn%st->nspc;
7353706Sroot 		tn = sn/st->nsect;
7363706Sroot 		sn %= st->nsect;
7373706Sroot 		mbp->mba_bcr = -512;
7383706Sroot #ifdef HPBDEBUG
7393706Sroot 		if (hpbdebug)
7403706Sroot 		printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
7413706Sroot #endif
7423706Sroot 		break;
7433706Sroot 
7443706Sroot 	case CONT:
7453706Sroot #ifdef HPBDEBUG
7463706Sroot 		if (hpbdebug)
7473706Sroot 		printf("hpecc, CONT: bn %d cn %d tn %d sn %d\n", bn,cn,tn,sn);
7483706Sroot #endif
7493706Sroot 		npf = bp->b_error;
7503706Sroot 		bp->b_flags &= ~B_BAD;
7513706Sroot 		mbp->mba_bcr = -(bp->b_bcount - (int)ptob(npf));
7523706Sroot 		if ((mbp->mba_bcr & 0xffff) == 0)
7533706Sroot 			return(0);
7543706Sroot 		break;
7553706Sroot #endif
7563706Sroot 	}
7573706Sroot 	rp->hpcs1 = HP_DCLR|HP_GO;
7583350Swnj 	if (rp->hpof&HPOF_SSEI)
7592883Swnj 		sn++;
760420Sbill 	rp->hpdc = cn;
761420Sbill 	rp->hpda = (tn<<8) + sn;
762420Sbill 	mbp->mba_sr = -1;
7633706Sroot 	mbp->mba_var = (int)ptob(npf) + o;
7643706Sroot 	rp->hpcs1 = bp->b_flags&B_READ ? HP_RCOM|HP_GO : HP_WCOM|HP_GO;
7653706Sroot 	mi->mi_tab.b_errcnt = 0;	/* error has been corrected */
766420Sbill 	return (1);
76721Sbill }
7682362Swnj 
7692362Swnj #define	DBSIZE	20
7702362Swnj 
7712362Swnj hpdump(dev)
7722362Swnj 	dev_t dev;
7732362Swnj {
7742978Swnj 	register struct mba_device *mi;
7752383Swnj 	register struct mba_regs *mba;
7762624Swnj 	struct hpdevice *hpaddr;
7772362Swnj 	char *start;
7782383Swnj 	int num, unit;
7792383Swnj 	register struct hpst *st;
7802362Swnj 
7812362Swnj 	num = maxfree;
7822362Swnj 	start = 0;
7832362Swnj 	unit = minor(dev) >> 3;
7842827Swnj 	if (unit >= NHP)
7852827Swnj 		return (ENXIO);
7862383Swnj #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
7872978Swnj 	mi = phys(hpinfo[unit],struct mba_device *);
7882827Swnj 	if (mi == 0 || mi->mi_alive == 0)
7892827Swnj 		return (ENXIO);
7902383Swnj 	mba = phys(mi->mi_hd, struct mba_hd *)->mh_physmba;
7913102Swnj 	mba->mba_cr = MBCR_INIT;
7922624Swnj 	hpaddr = (struct hpdevice *)&mba->mba_drv[mi->mi_drive];
7933093Swnj 	if ((hpaddr->hpds & HPDS_VV) == 0) {
7942624Swnj 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
7952624Swnj 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
7963093Swnj 		hpaddr->hpof = HPOF_FMT22;
7972362Swnj 	}
7982383Swnj 	st = &hpst[mi->mi_type];
7992827Swnj 	if (dumplo < 0 || dumplo + num >= st->sizes[minor(dev)&07].nblocks)
8002827Swnj 		return (EINVAL);
8012362Swnj 	while (num > 0) {
8022383Swnj 		register struct pte *hpte = mba->mba_map;
8032362Swnj 		register int i;
8042383Swnj 		int blk, cn, sn, tn;
8052362Swnj 		daddr_t bn;
8062362Swnj 
8072362Swnj 		blk = num > DBSIZE ? DBSIZE : num;
8082362Swnj 		bn = dumplo + btop(start);
8092383Swnj 		cn = bn/st->nspc + st->sizes[minor(dev)&07].cyloff;
8102383Swnj 		sn = bn%st->nspc;
8112383Swnj 		tn = sn/st->nsect;
8122383Swnj 		sn = sn%st->nsect;
8132362Swnj 		hpaddr->hpdc = cn;
8142362Swnj 		hpaddr->hpda = (tn << 8) + sn;
8152362Swnj 		for (i = 0; i < blk; i++)
8162362Swnj 			*(int *)hpte++ = (btop(start)+i) | PG_V;
8172383Swnj 		mba->mba_sr = -1;
8182383Swnj 		mba->mba_bcr = -(blk*NBPG);
8192383Swnj 		mba->mba_var = 0;
8202624Swnj 		hpaddr->hpcs1 = HP_WCOM | HP_GO;
8213093Swnj 		while ((hpaddr->hpds & HPDS_DRY) == 0)
8222362Swnj 			;
8233093Swnj 		if (hpaddr->hpds&HPDS_ERR)
8242827Swnj 			return (EIO);
8252362Swnj 		start += blk*NBPG;
8262362Swnj 		num -= blk;
8272362Swnj 	}
8282362Swnj 	return (0);
8292362Swnj }
8301565Sbill #endif
831