xref: /csrg-svn/sys/vax/mba/hp.c (revision 10856)
1*10856Ssam /*	hp.c	4.64	83/02/10	*/
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  */
219786Ssam #include "../machine/pte.h"
2221Sbill 
2321Sbill #include "../h/param.h"
2421Sbill #include "../h/systm.h"
25305Sbill #include "../h/dk.h"
2621Sbill #include "../h/buf.h"
2721Sbill #include "../h/conf.h"
2821Sbill #include "../h/dir.h"
2921Sbill #include "../h/user.h"
3021Sbill #include "../h/map.h"
318468Sroot #include "../vax/mtpr.h"
32420Sbill #include "../h/vm.h"
332362Swnj #include "../h/cmap.h"
343706Sroot #include "../h/dkbad.h"
357635Ssam #include "../h/ioctl.h"
367738Sroot #include "../h/uio.h"
3721Sbill 
388988Sroot #include "../vax/dkio.h"
398468Sroot #include "../vaxmba/mbareg.h"
408468Sroot #include "../vaxmba/mbavar.h"
418468Sroot #include "../vaxmba/hpreg.h"
4221Sbill 
432383Swnj /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
442383Swnj struct	size {
4521Sbill 	daddr_t	nblocks;
4621Sbill 	int	cyloff;
473706Sroot } hp6_sizes[8] = {
48886Sbill 	15884,	0,		/* A=cyl 0 thru 37 */
49886Sbill 	33440,	38,		/* B=cyl 38 thru 117 */
50886Sbill 	340670,	0,		/* C=cyl 0 thru 814 */
5121Sbill 	0,	0,
5221Sbill 	0,	0,
5321Sbill 	0,	0,
543729Sroot 	291280,	118,		/* G=cyl 118 thru 814 */
5521Sbill 	0,	0,
563706Sroot }, rm3_sizes[8] = {
57886Sbill 	15884,	0,		/* A=cyl 0 thru 99 */
58886Sbill 	33440,	100,		/* B=cyl 100 thru 309 */
59886Sbill 	131680,	0,		/* C=cyl 0 thru 822 */
6021Sbill 	0,	0,
6121Sbill 	0,	0,
623442Sroot 	0,	0,
633729Sroot 	81984,	310,		/* G=cyl 310 thru 822 */
6421Sbill 	0,	0,
65886Sbill }, rm5_sizes[8] = {
66886Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
67886Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
683271Swnj 	500384,	0,		/* C=cyl 0 thru 822 */
69886Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
70886Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
713729Sroot 	86240,	681,		/* F=cyl 681 thru 822 */
723729Sroot 	158592,	562,		/* G=cyl 562 thru 822 */
73886Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
742383Swnj }, rm80_sizes[8] = {
752383Swnj 	15884,	0,		/* A=cyl 0 thru 36 */
762383Swnj 	33440,	37,		/* B=cyl 37 thru 114 */
772383Swnj 	242606,	0,		/* C=cyl 0 thru 558 */
782383Swnj 	0,	0,
792383Swnj 	0,	0,
802383Swnj 	0,	0,
812383Swnj 	82080,	115,		/* G=cyl 115 thru 304 */
823729Sroot 	110143,	305,		/* H=cyl 305 thru 558 */
833706Sroot }, hp7_sizes[8] = {
846927Ssam 	15884,	0,		/* A=cyl 0 thru 9 */
853706Sroot 	64000,	10,		/* B=cyl 10 thru 49 */
863706Sroot 	1008000,0,		/* C=cyl 0 thru 629 */
873706Sroot 	15884,	330,		/* D=cyl 330 thru 339 */
883706Sroot 	256000,	340,		/* E=cyl 340 thru 499 */
893729Sroot 	207850,	500,		/* F=cyl 500 thru 629 */
903729Sroot 	479850,	330,		/* G=cyl 330 thru 629 */
913706Sroot 	448000,	50,		/* H=cyl 50 thru 329 */
926927Ssam }, si9775_sizes[8] = {
93*10856Ssam 	16640,	  0,		/* A=cyl 0 thru 12 */
94*10856Ssam 	34560,	 13,		/* B=cyl 13 thru 39 */
95*10856Ssam 	1079040,  0,		/* C=cyl 0 thru 842 */
96*10856Ssam 	0,	  0,
97*10856Ssam 	0,	  0,
98*10856Ssam 	0,	  0,
99*10856Ssam 	513280,	 40,		/* G=cyl 40 thru 440 */
1006927Ssam 	513280,	441,		/* H=cyl 441 thru 841 */
1016927Ssam }, si9730_sizes[8] = {
1026927Ssam 	15884,	0,		/* A=cyl 0 thru 49 */
1036927Ssam 	33440,	50,		/* B=cyl 50 thru 154 */
1046927Ssam 	263360,	0,		/* C=cyl 0 thru 822 */
1056927Ssam 	0,	0,
1066927Ssam 	0,	0,
1076927Ssam 	0,	0,
1086927Ssam 	0,	0,
1096927Ssam 	213664,	155,		/* H=cyl 155 thru 822 */
1106927Ssam }, hpam_sizes[8] = {
1116927Ssam 	15884,	0,		/* A=cyl 0 thru 31 */
1126927Ssam 	33440,	32,		/* B=cyl 32 thru 97 */
1136927Ssam 	524288,	0,		/* C=cyl 0 thru 1023 */
1146927Ssam 	27786,	668,
1156927Ssam 	27786,	723,
1166927Ssam 	125440,	778,
1176927Ssam 	181760,	668,		/* G=cyl 668 thru 1022 */
1186927Ssam 	291346,	98,		/* H=cyl 98 thru 667 */
1199175Ssam }, hpfj_sizes[8] = {
1209175Ssam 	15884,	0,		/* A=cyl 0 thru 18 */
1219175Ssam 	33440,	19,		/* B=cyl 19 thru 58 */
12210055Ssam 	724120,	0,		/* C=cyl 0 thru 841 */
1239175Ssam 	0, 0,
1249175Ssam 	0, 0,
1259175Ssam 	0, 0,
1269175Ssam 	381711, 398,		/* G=cyl 398 thru 841 */
1279175Ssam 	291346, 59,		/* H=cyl 59 thru 397 */
12810734Ssam }, hpfj48_sizes[8] = {
12910734Ssam 	15884,	0,		/* A=cyl 0 thru 16 */
13010734Ssam 	33440,	17,		/* B=cyl 17 thru 52 */
13110734Ssam 	808176,	0,		/* C=cyl 0 thru 841 */
13210734Ssam 	0, 0,
13310734Ssam 	0, 0,
13410734Ssam 	0, 0,
13510734Ssam 	465456, 357,		/* G=cyl 357 thru 841 */
13610734Ssam 	291346, 53,		/* H=cyl 52 thru 356 */
13721Sbill };
1382383Swnj /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
13921Sbill 
1402383Swnj #define	_hpSDIST	2
1412383Swnj #define	_hpRDIST	3
1422383Swnj 
1432383Swnj int	hpSDIST = _hpSDIST;
1442383Swnj int	hpRDIST = _hpRDIST;
1452383Swnj 
1466927Ssam /*
1476927Ssam  * Table for converting Massbus drive types into
1486927Ssam  * indices into the partition tables.  Slots are
1496927Ssam  * left for those drives devined from other means
1506927Ssam  * (e.g. SI, AMPEX, etc.).
1516927Ssam  */
1526927Ssam short	hptypes[] = {
1536927Ssam #define	HPDT_RM03	0
1546927Ssam 	MBDT_RM03,
1556927Ssam #define	HPDT_RM05	1
1566927Ssam 	MBDT_RM05,
1576927Ssam #define	HPDT_RP06	2
1586927Ssam 	MBDT_RP06,
1596927Ssam #define	HPDT_RM80	3
1606927Ssam 	MBDT_RM80,
1616927Ssam #define	HPDT_RP05	4
1626927Ssam 	MBDT_RP05,
1636927Ssam #define	HPDT_RP07	5
1646927Ssam 	MBDT_RP07,
1656927Ssam #define	HPDT_ML11A	6
1666927Ssam 	MBDT_ML11A,
1676927Ssam #define	HPDT_ML11B	7
1686927Ssam 	MBDT_ML11B,
1696927Ssam #define	HPDT_9775	8
1706927Ssam 	-1,
1716927Ssam #define	HPDT_9730	9
1726927Ssam 	-1,
1736927Ssam #define	HPDT_CAPRICORN	10
1746927Ssam 	-1,
1759175Ssam #define HPDT_EAGLE	11
1769175Ssam 	-1,
17710734Ssam #define	HPDT_EAGLE48	12
17810734Ssam 	-1,
17910734Ssam #define HPDT_RM02	13
1809175Ssam 	MBDT_RM02,		/* beware, actually capricorn or eagle */
1816927Ssam 	0
1826927Ssam };
1832978Swnj struct	mba_device *hpinfo[NHP];
1842978Swnj int	hpattach(),hpustart(),hpstart(),hpdtint();
1852383Swnj struct	mba_driver hpdriver =
1862978Swnj 	{ hpattach, 0, hpustart, hpstart, hpdtint, 0,
1872978Swnj 	  hptypes, "hp", 0, hpinfo };
1882383Swnj 
1892383Swnj struct hpst {
1902383Swnj 	short	nsect;
1912383Swnj 	short	ntrak;
1922383Swnj 	short	nspc;
1932383Swnj 	short	ncyl;
1942383Swnj 	struct	size *sizes;
1952383Swnj } hpst[] = {
1963706Sroot 	32,	5,	32*5,	823,	rm3_sizes,	/* RM03 */
1972383Swnj 	32,	19,	32*19,	823,	rm5_sizes,	/* RM05 */
1983706Sroot 	22,	19,	22*19,	815,	hp6_sizes,	/* RP06 */
1993706Sroot 	31,	14, 	31*14,	559,	rm80_sizes,	/* RM80 */
2003706Sroot 	22,	19,	22*19,	411,	hp6_sizes,	/* RP05 */
2013706Sroot 	50,	32,	50*32,	630,	hp7_sizes,	/* RP07 */
2025726Sroot 	1,	1,	1,	1,	0,		/* ML11A */
2035726Sroot 	1,	1,	1,	1,	0,		/* ML11B */
2046927Ssam 	32,	40,	32*40,	843,	si9775_sizes,	/* 9775 */
2056927Ssam 	32,	10,	32*10,	823,	si9730_sizes,	/* 9730 */
2066927Ssam 	32,	16,	32*16,	1024,	hpam_sizes,	/* AMPEX capricorn */
20710055Ssam 	43,	20,	43*20,	842,	hpfj_sizes,	/* Fujitsu EAGLE */
20810734Ssam 	48,	20,	48*20,	842,	hpfj48_sizes,	/* 48 sector EAGLE */
2092383Swnj };
2102383Swnj 
2112624Swnj u_char	hp_offset[16] = {
2123093Swnj     HPOF_P400, HPOF_M400, HPOF_P400, HPOF_M400,
2133093Swnj     HPOF_P800, HPOF_M800, HPOF_P800, HPOF_M800,
2143093Swnj     HPOF_P1200, HPOF_M1200, HPOF_P1200, HPOF_M1200,
2153093Swnj     0, 0, 0, 0,
21621Sbill };
21721Sbill 
2182624Swnj struct	buf	rhpbuf[NHP];
2193706Sroot struct	buf	bhpbuf[NHP];
2203706Sroot struct	dkbad	hpbad[NHP];
2215726Sroot /* SHOULD CONSOLIDATE ALL THIS STUFF INTO A STRUCTURE */
2223706Sroot char	hpinit[NHP];
2232892Swnj char	hprecal[NHP];
2245726Sroot char	hphdr[NHP];
2255726Sroot daddr_t	mlsize[NHP];
22621Sbill 
22721Sbill #define	b_cylin b_resid
22821Sbill 
2295726Sroot /* #define ML11 0  to remove ML11 support */
2305726Sroot #define	ML11	(hptypes[mi->mi_type] == MBDT_ML11A)
2315726Sroot #define	RP06	(hptypes[mi->mi_type] <= MBDT_RP06)
2325726Sroot #define	RM80	(hptypes[mi->mi_type] == MBDT_RM80)
2335726Sroot 
23421Sbill #ifdef INTRLVE
23521Sbill daddr_t dkblock();
23621Sbill #endif
23721Sbill 
2382604Swnj int	hpseek;
2392604Swnj 
2402978Swnj /*ARGSUSED*/
2412978Swnj hpattach(mi, slave)
24210734Ssam 	register struct mba_device *mi;
2432604Swnj {
24410734Ssam 
24510734Ssam 	mi->mi_type = hpmaptype(mi);
24610734Ssam 	if (!ML11 && mi->mi_dk >= 0) {
24710734Ssam 		struct hpst *st = &hpst[mi->mi_type];
24810734Ssam 
24910734Ssam 		dk_mspw[mi->mi_dk] = 1.0 / 60 / (st->nsect * 256);
25010734Ssam 	}
25110734Ssam }
25210734Ssam 
25310734Ssam /*
25410734Ssam  * Map apparent MASSBUS drive type into manufacturer
25510734Ssam  * specific configuration.  For SI controllers this is done
25610734Ssam  * based on codes in the serial number register.  For
25710734Ssam  * EMULEX controllers, the track and sector attributes are
25810734Ssam  * used when the drive type is an RM02 (not supported by DEC).
25910734Ssam  */
26010734Ssam hpmaptype(mi)
26110734Ssam 	register struct mba_device *mi;
26210734Ssam {
2636927Ssam 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
26410734Ssam 	register int type = mi->mi_type;
2656927Ssam 
2666927Ssam 	/*
2676927Ssam 	 * Model-byte processing for SI 9400 controllers.
2686927Ssam 	 * NB:  Only deals with RM03 and RM05 emulations.
2696927Ssam 	 */
27010734Ssam 	if (type == HPDT_RM03 || type == HPDT_RM05) {
27110734Ssam 		int hpsn = hpaddr->hpsn;
2726927Ssam 
2736927Ssam 		if ((hpsn & SIMB_LU) != mi->mi_drive)
27410734Ssam 			return (type);
2756927Ssam 		switch ((hpsn & SIMB_MB) & ~(SIMB_S6|SIRM03|SIRM05)) {
2766927Ssam 
2776927Ssam 		case SI9775D:
2786927Ssam 			printf("hp%d: si 9775 (direct)\n", mi->mi_unit);
27910734Ssam 			type = HPDT_9775;
2806927Ssam 			break;
2816927Ssam 
2826927Ssam 		case SI9730D:
2836927Ssam 			printf("hp%d: si 9730 (direct)\n", mi->mi_unit);
28410734Ssam 			type = HPDT_9730;
2856927Ssam 			break;
2866927Ssam 
2877035Swnj 		/*
28810734Ssam 		 * AMPEX 9300, SI Combination needs a have the
28910734Ssam 		 * drive cleared before we start.  We do not know
29010734Ssam 		 * why, but tests show that the recalibrate fixes
29110734Ssam 		 * the problem.
2927035Swnj 		 */
2936927Ssam 		case SI9766:
2946927Ssam 			printf("hp%d: 9776/9300\n", mi->mi_unit);
29510734Ssam 			type = HPDT_RM05;
2967035Swnj 			hpaddr->hpcs1 = HP_RECAL|HP_GO;
2977035Swnj 			DELAY(100000);
2986927Ssam 			break;
2996927Ssam 
3006927Ssam 		case SI9762:
3016927Ssam 			printf("hp%d: 9762\n", mi->mi_unit);
30210734Ssam 			type = HPDT_RM03;
3036927Ssam 			break;
3046927Ssam 		}
30510734Ssam 		return (type);
30610734Ssam 	}
3076927Ssam 
3086927Ssam 	/*
30910734Ssam 	 * EMULEX SC750 or SC780.  Poke the holding register.
3106927Ssam 	 */
31110734Ssam 	if (type == HPDT_RM02) {
31210734Ssam 		int ntracks, nsectors;
31310734Ssam 
3146927Ssam 		hpaddr->hpcs1 = HP_NOP;
3156927Ssam 		hpaddr->hphr = HPHR_MAXTRAK;
31610055Ssam 		ntracks = (hpaddr->hphr & 0xffff) + 1;
31710055Ssam 		if (ntracks == 16) {
3186927Ssam 			printf("hp%d: capricorn\n", mi->mi_unit);
31910734Ssam 			type = HPDT_CAPRICORN;
32010734Ssam 			goto done;
32110734Ssam 		}
32210734Ssam 		if (ntracks != 20) {
32310055Ssam 			printf("hp%d: ntracks %d: unknown device\n", ntracks);
32410734Ssam 			goto done;
32510734Ssam 		}
32610734Ssam 		hpaddr->hpcs1 = HP_NOP;
32710734Ssam 		hpaddr->hphr = HPHR_MAXSECT;
32810734Ssam 		nsectors = (hpaddr->hphr & 0xffff) + 1;
32910734Ssam 		printf("hp%d: ", mi->mi_unit);
33010734Ssam 		if (nsectors == 43)
33110734Ssam 			type = HPDT_EAGLE;
33210734Ssam 		else {
33310734Ssam 			type = HPDT_EAGLE48;
33410734Ssam 			printf("modified ");
33510734Ssam 		}
33610734Ssam 		printf("eagle\n");
33710734Ssam done:
3386927Ssam 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
33910734Ssam 		return (type);
34010734Ssam 	}
3416927Ssam 
34210734Ssam 	/*
34310734Ssam 	 * Map all ML11's to the same type.  Also calculate
34410734Ssam 	 * transfer rates based on device characteristics.
34510734Ssam 	 */
34610734Ssam 	if (type == HPDT_ML11A || type == HPDT_ML11B) {
3475726Sroot 		register int trt, sz;
3482604Swnj 
3495726Sroot 		sz = hpaddr->hpmr & HPMR_SZ;
3505726Sroot 		if ((hpaddr->hpmr & HPMR_ARRTYP) == 0)
3515726Sroot 			sz >>= 2;
3525726Sroot 		mlsize[mi->mi_unit] = sz;
3535726Sroot 		if (mi->mi_dk >= 0) {
3545726Sroot 			trt = (hpaddr->hpmr & HPMR_TRT) >> 8;
3555726Sroot 			dk_mspw[mi->mi_dk] = 1.0 / (1<<(20-trt));
3565726Sroot 		}
35710734Ssam 		type = HPDT_ML11A;
3586927Ssam 	}
35910734Ssam 	return (type);
3602604Swnj }
3612604Swnj 
3628579Sroot hpopen(dev)
3638579Sroot 	dev_t dev;
3648579Sroot {
3658579Sroot 	register int unit = minor(dev) >> 3;
3668579Sroot 	register struct mba_device *mi;
3678579Sroot 
3688579Sroot 	if (unit >= NHP || (mi = hpinfo[unit]) == 0 || mi->mi_alive == 0)
3698579Sroot 		return (ENXIO);
3708579Sroot 	return (0);
3718579Sroot }
3728579Sroot 
37321Sbill hpstrategy(bp)
3742383Swnj 	register struct buf *bp;
37521Sbill {
3762978Swnj 	register struct mba_device *mi;
3772383Swnj 	register struct hpst *st;
3782383Swnj 	register int unit;
37921Sbill 	long sz, bn;
3802383Swnj 	int xunit = minor(bp->b_dev) & 07;
3815432Sroot 	int s;
38221Sbill 
38321Sbill 	sz = bp->b_bcount;
38421Sbill 	sz = (sz+511) >> 9;
38521Sbill 	unit = dkunit(bp);
3862383Swnj 	if (unit >= NHP)
3872383Swnj 		goto bad;
3882383Swnj 	mi = hpinfo[unit];
3892395Swnj 	if (mi == 0 || mi->mi_alive == 0)
3902383Swnj 		goto bad;
3912383Swnj 	st = &hpst[mi->mi_type];
3925726Sroot 	if (ML11) {
3935726Sroot 		if (bp->b_blkno < 0 ||
3945726Sroot 		    dkblock(bp)+sz > mlsize[mi->mi_unit])
3955726Sroot 			goto bad;
3965726Sroot 		bp->b_cylin = 0;
3975726Sroot 	} else {
3985726Sroot 		if (bp->b_blkno < 0 ||
3995726Sroot 		    (bn = dkblock(bp))+sz > st->sizes[xunit].nblocks)
4005726Sroot 			goto bad;
4015726Sroot 		bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff;
4025726Sroot 	}
4035432Sroot 	s = spl5();
4042383Swnj 	disksort(&mi->mi_tab, bp);
4052383Swnj 	if (mi->mi_tab.b_active == 0)
4062383Swnj 		mbustart(mi);
4075432Sroot 	splx(s);
4082383Swnj 	return;
4092383Swnj 
4102383Swnj bad:
4112383Swnj 	bp->b_flags |= B_ERROR;
4122383Swnj 	iodone(bp);
4132383Swnj 	return;
41421Sbill }
41521Sbill 
4162383Swnj hpustart(mi)
4172978Swnj 	register struct mba_device *mi;
41821Sbill {
4192624Swnj 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
4202383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
4213706Sroot 	register struct hpst *st = &hpst[mi->mi_type];
42221Sbill 	daddr_t bn;
4233102Swnj 	int sn, dist;
42421Sbill 
4253706Sroot 	hpaddr->hpcs1 = 0;
4262624Swnj 	if ((hpaddr->hpcs1&HP_DVA) == 0)
4272383Swnj 		return (MBU_BUSY);
4283706Sroot 	if ((hpaddr->hpds & HPDS_VV) == 0 || hpinit[mi->mi_unit] == 0) {
4293706Sroot 		struct buf *bbp = &bhpbuf[mi->mi_unit];
4303706Sroot 
4313706Sroot 		hpinit[mi->mi_unit] = 1;
4322624Swnj 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
4333140Swnj 		if (mi->mi_mba->mba_drv[0].mbd_as & (1<<mi->mi_drive))
4343140Swnj 			printf("DCLR attn\n");
4352624Swnj 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
4365726Sroot 		if (!ML11)
4375726Sroot 			hpaddr->hpof = HPOF_FMT22;
4383140Swnj 		mbclrattn(mi);
4395726Sroot 		if (!ML11) {
4405726Sroot 			bbp->b_flags = B_READ|B_BUSY;
4415726Sroot 			bbp->b_dev = bp->b_dev;
4425726Sroot 			bbp->b_bcount = 512;
4435726Sroot 			bbp->b_un.b_addr = (caddr_t)&hpbad[mi->mi_unit];
4445726Sroot 			bbp->b_blkno = st->ncyl*st->nspc - st->nsect;
4455726Sroot 			bbp->b_cylin = st->ncyl - 1;
4465726Sroot 			mi->mi_tab.b_actf = bbp;
4475726Sroot 			bbp->av_forw = bp;
4485726Sroot 			bp = bbp;
4495726Sroot 		}
45021Sbill 	}
4512604Swnj 	if (mi->mi_tab.b_active || mi->mi_hd->mh_ndrive == 1)
4522383Swnj 		return (MBU_DODATA);
4535726Sroot 	if (ML11)
4545726Sroot 		return (MBU_DODATA);
4553093Swnj 	if ((hpaddr->hpds & HPDS_DREADY) != HPDS_DREADY)
4562383Swnj 		return (MBU_DODATA);
4572395Swnj 	bn = dkblock(bp);
4582395Swnj 	sn = bn%st->nspc;
4592395Swnj 	sn = (sn+st->nsect-hpSDIST)%st->nsect;
4602383Swnj 	if (bp->b_cylin == (hpaddr->hpdc & 0xffff)) {
4612604Swnj 		if (hpseek)
4622383Swnj 			return (MBU_DODATA);
4632383Swnj 		dist = ((hpaddr->hpla & 0xffff)>>6) - st->nsect + 1;
4642383Swnj 		if (dist < 0)
4652383Swnj 			dist += st->nsect;
4662383Swnj 		if (dist > st->nsect - hpRDIST)
4672383Swnj 			return (MBU_DODATA);
4682614Swnj 	} else
4692614Swnj 		hpaddr->hpdc = bp->b_cylin;
4702604Swnj 	if (hpseek)
4712624Swnj 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
472305Sbill 	else {
473305Sbill 		hpaddr->hpda = sn;
4742624Swnj 		hpaddr->hpcs1 = HP_SEARCH|HP_GO;
475305Sbill 	}
4762383Swnj 	return (MBU_STARTED);
47721Sbill }
47821Sbill 
4792383Swnj hpstart(mi)
4802978Swnj 	register struct mba_device *mi;
48121Sbill {
4822624Swnj 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
4832383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
4842383Swnj 	register struct hpst *st = &hpst[mi->mi_type];
48521Sbill 	daddr_t bn;
4862383Swnj 	int sn, tn;
48721Sbill 
48821Sbill 	bn = dkblock(bp);
4895726Sroot 	if (ML11)
4905726Sroot 		hpaddr->hpda = bn;
4915726Sroot 	else {
4925726Sroot 		sn = bn%st->nspc;
4935726Sroot 		tn = sn/st->nsect;
4945726Sroot 		sn %= st->nsect;
4955726Sroot 		hpaddr->hpdc = bp->b_cylin;
4965726Sroot 		hpaddr->hpda = (tn << 8) + sn;
4975726Sroot 	}
4985726Sroot 	if (hphdr[mi->mi_unit]) {
4995726Sroot 		if (bp->b_flags & B_READ)
5005726Sroot 			return (HP_RHDR|HP_GO);
5015726Sroot 		else
5025726Sroot 			return (HP_WHDR|HP_GO);
5035726Sroot 	}
5045726Sroot 	return (0);
50521Sbill }
50621Sbill 
5073102Swnj hpdtint(mi, mbsr)
5082978Swnj 	register struct mba_device *mi;
5093102Swnj 	int mbsr;
51021Sbill {
5112624Swnj 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
5122383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
51310622Shelge 	register struct hpst *st = &hpst[mi->mi_type];
5145893Swnj 	register int er1, er2;
5152826Swnj 	int retry = 0;
51621Sbill 
5173706Sroot 	if (bp->b_flags&B_BAD) {
5183706Sroot 		if (hpecc(mi, CONT))
5193706Sroot 			return(MBD_RESTARTED);
5203706Sroot 	}
5213102Swnj 	if (hpaddr->hpds&HPDS_ERR || mbsr&MBSR_EBITS) {
5223706Sroot #ifdef HPDEBUG
5233289Swnj 		if (hpdebug) {
5243706Sroot 			int dc = hpaddr->hpdc, da = hpaddr->hpda;
5253706Sroot 
5263706Sroot 			printf("hperr: bp %x cyl %d blk %d as %o ",
5273706Sroot 				bp, bp->b_cylin, bp->b_blkno,
5283706Sroot 				hpaddr->hpas&0xff);
5293706Sroot 			printf("dc %x da %x\n",dc&0xffff, da&0xffff);
5303289Swnj 			printf("errcnt %d ", mi->mi_tab.b_errcnt);
5313289Swnj 			printf("mbsr=%b ", mbsr, mbsr_bits);
5323289Swnj 			printf("er1=%b er2=%b\n",
5333289Swnj 			    hpaddr->hper1, HPER1_BITS,
5343289Swnj 			    hpaddr->hper2, HPER2_BITS);
5353289Swnj 			DELAY(1000000);
5363289Swnj 		}
5373706Sroot #endif
5385858Swnj 		er1 = hpaddr->hper1;
5395893Swnj 		er2 = hpaddr->hper2;
5405893Swnj 		if (er1 & HPER1_HCRC) {
5415858Swnj 			er1 &= ~(HPER1_HCE|HPER1_FER);
5425893Swnj 			er2 &= ~HPER2_BSE;
5435893Swnj 		}
5445858Swnj 		if (er1&HPER1_WLE) {
5452925Swnj 			printf("hp%d: write locked\n", dkunit(bp));
5462826Swnj 			bp->b_flags |= B_ERROR;
5475858Swnj 		} else if ((er1&0xffff) == HPER1_FER && RP06 &&
5485726Sroot 		    hphdr[mi->mi_unit] == 0) {
5495726Sroot 			if (hpecc(mi, BSE))
5505726Sroot 				return(MBD_RESTARTED);
5515726Sroot 			else
5525726Sroot 				goto hard;
5532826Swnj 		} else if (++mi->mi_tab.b_errcnt > 27 ||
5543102Swnj 		    mbsr & MBSR_HARD ||
5555858Swnj 		    er1 & HPER1_HARD ||
5565726Sroot 		    hphdr[mi->mi_unit] ||
5575893Swnj 		    (!ML11 && (er2 & HPER2_HARD))) {
5583706Sroot hard:
55910622Shelge 			if (ML11)
56010622Shelge 				bp->b_blkno = hpaddr->hpda&0xffff;
56110622Shelge 			else
56210622Shelge 				bp->b_blkno = (hpaddr->hpdc*st->nspc)&0xffff +
56310622Shelge 					  ((hpaddr->hpda>>8)&0xffff)*st->nsect +
56410622Shelge 						(hpaddr->hpda&0x1f);
5652925Swnj 			harderr(bp, "hp");
5663271Swnj 			if (mbsr & (MBSR_EBITS &~ (MBSR_DTABT|MBSR_MBEXC)))
5673271Swnj 				printf("mbsr=%b ", mbsr, mbsr_bits);
5683706Sroot 			printf("er1=%b er2=%b",
5692826Swnj 			    hpaddr->hper1, HPER1_BITS,
5702826Swnj 			    hpaddr->hper2, HPER2_BITS);
5713706Sroot 			if (hpaddr->hpmr)
5723706Sroot 				printf(" mr=%o", hpaddr->hpmr&0xffff);
5733706Sroot 			if (hpaddr->hpmr2)
5743706Sroot 				printf(" mr2=%o", hpaddr->hpmr2&0xffff);
5753706Sroot 			printf("\n");
5762826Swnj 			bp->b_flags |= B_ERROR;
57710622Shelge 			retry = 0;
5783143Swnj 			hprecal[mi->mi_unit] = 0;
5795893Swnj 		} else if ((er2 & HPER2_BSE) && !ML11) {
5803706Sroot 			if (hpecc(mi, BSE))
5813706Sroot 				return(MBD_RESTARTED);
5823706Sroot 			else
5833706Sroot 				goto hard;
5845893Swnj 		} else if (RM80 && er2&HPER2_SSE) {
5853988Sroot 			(void) hpecc(mi, SSE);
5862883Swnj 			return (MBD_RESTARTED);
5875858Swnj 		} else if ((er1&(HPER1_DCK|HPER1_ECH))==HPER1_DCK) {
5883706Sroot 			if (hpecc(mi, ECC))
5892383Swnj 				return (MBD_RESTARTED);
5902826Swnj 			/* else done */
5912826Swnj 		} else
5922826Swnj 			retry = 1;
5932826Swnj 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
5945726Sroot 		if (ML11) {
5955726Sroot 			if (mi->mi_tab.b_errcnt >= 16)
5965726Sroot 				goto hard;
5975726Sroot 		} else if ((mi->mi_tab.b_errcnt&07) == 4) {
5982826Swnj 			hpaddr->hpcs1 = HP_RECAL|HP_GO;
5993706Sroot 			hprecal[mi->mi_unit] = 1;
6003706Sroot 			return(MBD_RESTARTED);
60121Sbill 		}
6022826Swnj 		if (retry)
6032826Swnj 			return (MBD_RETRY);
6042826Swnj 	}
6053706Sroot #ifdef HPDEBUG
6063289Swnj 	else
6073289Swnj 		if (hpdebug && hprecal[mi->mi_unit]) {
6083289Swnj 			printf("recal %d ", hprecal[mi->mi_unit]);
6093289Swnj 			printf("errcnt %d\n", mi->mi_tab.b_errcnt);
6103289Swnj 			printf("mbsr=%b ", mbsr, mbsr_bits);
6113289Swnj 			printf("er1=%b er2=%b\n",
6123289Swnj 			    hpaddr->hper1, HPER1_BITS,
6133289Swnj 			    hpaddr->hper2, HPER2_BITS);
6143289Swnj 		}
6153706Sroot #endif
6163093Swnj 	switch (hprecal[mi->mi_unit]) {
6173093Swnj 
6183093Swnj 	case 1:
6193093Swnj 		hpaddr->hpdc = bp->b_cylin;
6203093Swnj 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
6213706Sroot 		hprecal[mi->mi_unit]++;
6223706Sroot 		return (MBD_RESTARTED);
6233093Swnj 	case 2:
6243093Swnj 		if (mi->mi_tab.b_errcnt < 16 ||
6253289Swnj 		    (bp->b_flags & B_READ) == 0)
6263093Swnj 			goto donerecal;
6273093Swnj 		hpaddr->hpof = hp_offset[mi->mi_tab.b_errcnt & 017]|HPOF_FMT22;
6283093Swnj 		hpaddr->hpcs1 = HP_OFFSET|HP_GO;
6293093Swnj 		hprecal[mi->mi_unit]++;
6303093Swnj 		return (MBD_RESTARTED);
6313093Swnj 	donerecal:
6323158Swnj 	case 3:
6332892Swnj 		hprecal[mi->mi_unit] = 0;
6342892Swnj 		return (MBD_RETRY);
6352892Swnj 	}
6365726Sroot 	hphdr[mi->mi_unit] = 0;
6372383Swnj 	bp->b_resid = -(mi->mi_mba->mba_bcr) & 0xffff;
6383640Swnj 	if (mi->mi_tab.b_errcnt >= 16) {
6393093Swnj 		/*
6403093Swnj 		 * This is fast and occurs rarely; we don't
6413093Swnj 		 * bother with interrupts.
6423093Swnj 		 */
6432624Swnj 		hpaddr->hpcs1 = HP_RTC|HP_GO;
6443093Swnj 		while (hpaddr->hpds & HPDS_PIP)
6452383Swnj 			;
6462383Swnj 		mbclrattn(mi);
64721Sbill 	}
6485726Sroot 	if (!ML11) {
6495726Sroot 		hpaddr->hpof = HPOF_FMT22;
6505726Sroot 		hpaddr->hpcs1 = HP_RELEASE|HP_GO;
6515726Sroot 	}
6522383Swnj 	return (MBD_DONE);
65321Sbill }
65421Sbill 
6557738Sroot hpread(dev, uio)
6562624Swnj 	dev_t dev;
6577738Sroot 	struct uio *uio;
65821Sbill {
6592624Swnj 	register int unit = minor(dev) >> 3;
66021Sbill 
6612624Swnj 	if (unit >= NHP)
6628156Sroot 		return (ENXIO);
6638156Sroot 	return (physio(hpstrategy, &rhpbuf[unit], dev, B_READ, minphys, uio));
66421Sbill }
66521Sbill 
6667831Sroot hpwrite(dev, uio)
6672624Swnj 	dev_t dev;
6687831Sroot 	struct uio *uio;
66921Sbill {
6702624Swnj 	register int unit = minor(dev) >> 3;
67121Sbill 
6722624Swnj 	if (unit >= NHP)
6738156Sroot 		return (ENXIO);
6748156Sroot 	return (physio(hpstrategy, &rhpbuf[unit], dev, B_WRITE, minphys, uio));
67521Sbill }
67621Sbill 
6775726Sroot /*ARGSUSED*/
6787635Ssam hpioctl(dev, cmd, data, flag)
6795726Sroot 	dev_t dev;
6805726Sroot 	int cmd;
6817635Ssam 	caddr_t data;
6825726Sroot 	int flag;
6835726Sroot {
6845726Sroot 
6855726Sroot 	switch (cmd) {
6867635Ssam 
6875726Sroot 	case DKIOCHDR:	/* do header read/write */
6885726Sroot 		hphdr[minor(dev)>>3] = 1;
6898579Sroot 		return (0);
6905726Sroot 
6915726Sroot 	default:
6928579Sroot 		return (ENXIO);
6935726Sroot 	}
6945726Sroot }
6955726Sroot 
6963706Sroot hpecc(mi, flag)
6972978Swnj 	register struct mba_device *mi;
6983706Sroot 	int flag;
69921Sbill {
7002383Swnj 	register struct mba_regs *mbp = mi->mi_mba;
7012624Swnj 	register struct hpdevice *rp = (struct hpdevice *)mi->mi_drv;
7022383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
7033706Sroot 	register struct hpst *st = &hpst[mi->mi_type];
7043706Sroot 	int npf, o;
7052383Swnj 	int bn, cn, tn, sn;
706914Sbill 	int bcr;
70721Sbill 
708914Sbill 	bcr = mbp->mba_bcr & 0xffff;
709914Sbill 	if (bcr)
710914Sbill 		bcr |= 0xffff0000;		/* sxt */
7113706Sroot 	if (flag == CONT)
7123706Sroot 		npf = bp->b_error;
7133706Sroot 	else
7143706Sroot 		npf = btop(bcr + bp->b_bcount);
715420Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
716420Sbill 	bn = dkblock(bp);
717420Sbill 	cn = bp->b_cylin;
7183706Sroot 	sn = bn%(st->nspc) + npf;
7192383Swnj 	tn = sn/st->nsect;
7202383Swnj 	sn %= st->nsect;
7212383Swnj 	cn += tn/st->ntrak;
7222383Swnj 	tn %= st->ntrak;
7233706Sroot 	switch (flag) {
724*10856Ssam 	case ECC: {
7253706Sroot 		register int i;
7263706Sroot 		caddr_t addr;
7273706Sroot 		struct pte mpte;
7283706Sroot 		int bit, byte, mask;
7293706Sroot 
7303706Sroot 		npf--;		/* because block in error is previous block */
7313706Sroot 		printf("hp%d%c: soft ecc sn%d\n", dkunit(bp),
7323706Sroot 		    'a'+(minor(bp->b_dev)&07), bp->b_blkno + npf);
7333706Sroot 		mask = rp->hpec2&0xffff;
7343706Sroot 		i = (rp->hpec1&0xffff) - 1;		/* -1 makes 0 origin */
7353706Sroot 		bit = i&07;
7363706Sroot 		i = (i&~07)>>3;
7373706Sroot 		byte = i + o;
7383706Sroot 		while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
7393706Sroot 			mpte = mbp->mba_map[npf+btop(byte)];
7403706Sroot 			addr = ptob(mpte.pg_pfnum) + (byte & PGOFSET);
7413706Sroot 			putmemc(addr, getmemc(addr)^(mask<<bit));
7423706Sroot 			byte++;
7433706Sroot 			i++;
7443706Sroot 			bit -= 8;
7453706Sroot 		}
7463706Sroot 		if (bcr == 0)
7473706Sroot 			return (0);
7483847Sroot 		npf++;
7493706Sroot 		break;
7503706Sroot 		}
7513706Sroot 
7523706Sroot 	case SSE:
7533706Sroot 		rp->hpof |= HPOF_SSEI;
7543706Sroot 		mbp->mba_bcr = -(bp->b_bcount - (int)ptob(npf));
7553706Sroot 		break;
7563706Sroot 
7573706Sroot 	case BSE:
7583706Sroot #ifdef HPBDEBUG
7593706Sroot 		if (hpbdebug)
7603706Sroot 		printf("hpecc, BSE: bn %d cn %d tn %d sn %d\n", bn, cn, tn, sn);
7613706Sroot #endif
7623706Sroot 		if ((bn = isbad(&hpbad[mi->mi_unit], cn, tn, sn)) < 0)
7633706Sroot 			return(0);
7643706Sroot 		bp->b_flags |= B_BAD;
7653706Sroot 		bp->b_error = npf + 1;
7663706Sroot 		bn = st->ncyl*st->nspc - st->nsect - 1 - bn;
7673706Sroot 		cn = bn/st->nspc;
7683706Sroot 		sn = bn%st->nspc;
7693706Sroot 		tn = sn/st->nsect;
7703706Sroot 		sn %= st->nsect;
7713706Sroot 		mbp->mba_bcr = -512;
7723706Sroot #ifdef HPBDEBUG
7733706Sroot 		if (hpbdebug)
7743706Sroot 		printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
7753706Sroot #endif
7763706Sroot 		break;
7773706Sroot 
7783706Sroot 	case CONT:
7793706Sroot #ifdef HPBDEBUG
7803706Sroot 		if (hpbdebug)
7813706Sroot 		printf("hpecc, CONT: bn %d cn %d tn %d sn %d\n", bn,cn,tn,sn);
7823706Sroot #endif
7833706Sroot 		npf = bp->b_error;
7843706Sroot 		bp->b_flags &= ~B_BAD;
7853706Sroot 		mbp->mba_bcr = -(bp->b_bcount - (int)ptob(npf));
7863706Sroot 		if ((mbp->mba_bcr & 0xffff) == 0)
7873706Sroot 			return(0);
7883706Sroot 		break;
7893706Sroot 	}
7903706Sroot 	rp->hpcs1 = HP_DCLR|HP_GO;
7913350Swnj 	if (rp->hpof&HPOF_SSEI)
7922883Swnj 		sn++;
793420Sbill 	rp->hpdc = cn;
794420Sbill 	rp->hpda = (tn<<8) + sn;
795420Sbill 	mbp->mba_sr = -1;
7963706Sroot 	mbp->mba_var = (int)ptob(npf) + o;
7973706Sroot 	rp->hpcs1 = bp->b_flags&B_READ ? HP_RCOM|HP_GO : HP_WCOM|HP_GO;
7983706Sroot 	mi->mi_tab.b_errcnt = 0;	/* error has been corrected */
799420Sbill 	return (1);
80021Sbill }
8012362Swnj 
8022362Swnj #define	DBSIZE	20
8032362Swnj 
8042362Swnj hpdump(dev)
8052362Swnj 	dev_t dev;
8062362Swnj {
8072978Swnj 	register struct mba_device *mi;
8082383Swnj 	register struct mba_regs *mba;
8092624Swnj 	struct hpdevice *hpaddr;
8102362Swnj 	char *start;
8112383Swnj 	int num, unit;
8122383Swnj 	register struct hpst *st;
8132362Swnj 
8142362Swnj 	num = maxfree;
8152362Swnj 	start = 0;
8162362Swnj 	unit = minor(dev) >> 3;
8172827Swnj 	if (unit >= NHP)
8182827Swnj 		return (ENXIO);
8192383Swnj #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
8202978Swnj 	mi = phys(hpinfo[unit],struct mba_device *);
8212827Swnj 	if (mi == 0 || mi->mi_alive == 0)
8222827Swnj 		return (ENXIO);
8232383Swnj 	mba = phys(mi->mi_hd, struct mba_hd *)->mh_physmba;
8243102Swnj 	mba->mba_cr = MBCR_INIT;
8252624Swnj 	hpaddr = (struct hpdevice *)&mba->mba_drv[mi->mi_drive];
8263093Swnj 	if ((hpaddr->hpds & HPDS_VV) == 0) {
8272624Swnj 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
8282624Swnj 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
8293093Swnj 		hpaddr->hpof = HPOF_FMT22;
8302362Swnj 	}
8312383Swnj 	st = &hpst[mi->mi_type];
8322827Swnj 	if (dumplo < 0 || dumplo + num >= st->sizes[minor(dev)&07].nblocks)
8332827Swnj 		return (EINVAL);
8342362Swnj 	while (num > 0) {
8352383Swnj 		register struct pte *hpte = mba->mba_map;
8362362Swnj 		register int i;
8372383Swnj 		int blk, cn, sn, tn;
8382362Swnj 		daddr_t bn;
8392362Swnj 
8402362Swnj 		blk = num > DBSIZE ? DBSIZE : num;
8412362Swnj 		bn = dumplo + btop(start);
8422383Swnj 		cn = bn/st->nspc + st->sizes[minor(dev)&07].cyloff;
8432383Swnj 		sn = bn%st->nspc;
8442383Swnj 		tn = sn/st->nsect;
8452383Swnj 		sn = sn%st->nsect;
8462362Swnj 		hpaddr->hpdc = cn;
8472362Swnj 		hpaddr->hpda = (tn << 8) + sn;
8482362Swnj 		for (i = 0; i < blk; i++)
8492362Swnj 			*(int *)hpte++ = (btop(start)+i) | PG_V;
8502383Swnj 		mba->mba_sr = -1;
8512383Swnj 		mba->mba_bcr = -(blk*NBPG);
8522383Swnj 		mba->mba_var = 0;
8532624Swnj 		hpaddr->hpcs1 = HP_WCOM | HP_GO;
8543093Swnj 		while ((hpaddr->hpds & HPDS_DRY) == 0)
8552362Swnj 			;
8563093Swnj 		if (hpaddr->hpds&HPDS_ERR)
8572827Swnj 			return (EIO);
8582362Swnj 		start += blk*NBPG;
8592362Swnj 		num -= blk;
8602362Swnj 	}
8612362Swnj 	return (0);
8622362Swnj }
8631565Sbill #endif
864