xref: /csrg-svn/sys/vax/mba/hp.c (revision 26289)
123308Smckusick /*
223308Smckusick  * Copyright (c) 1982 Regents of the University of California.
323308Smckusick  * All rights reserved.  The Berkeley software License Agreement
423308Smckusick  * specifies the terms and conditions for redistribution.
523308Smckusick  *
6*26289Skarels  *	@(#)hp.c	6.19 (Berkeley) 02/20/86
723308Smckusick  */
83706Sroot 
93706Sroot #ifdef HPDEBUG
103289Swnj int	hpdebug;
113706Sroot #endif
123706Sroot #ifdef HPBDEBUG
133706Sroot int	hpbdebug;
143706Sroot #endif
1521Sbill 
161939Swnj #include "hp.h"
171565Sbill #if NHP > 0
1821Sbill /*
195726Sroot  * HP disk driver for RP0x+RMxx+ML11
202827Swnj  *
212827Swnj  * TODO:
223706Sroot  *	see if DCLR and/or RELEASE set attention status
2321Sbill  */
249786Ssam #include "../machine/pte.h"
2521Sbill 
2617120Sbloom #include "param.h"
2717120Sbloom #include "systm.h"
2817120Sbloom #include "dk.h"
2917120Sbloom #include "buf.h"
3017120Sbloom #include "conf.h"
3117120Sbloom #include "dir.h"
3217120Sbloom #include "user.h"
3317120Sbloom #include "map.h"
348468Sroot #include "../vax/mtpr.h"
3517120Sbloom #include "vm.h"
3617120Sbloom #include "cmap.h"
3717120Sbloom #include "dkbad.h"
3817120Sbloom #include "ioctl.h"
3917120Sbloom #include "uio.h"
4018411Skarels #include "syslog.h"
4121Sbill 
428988Sroot #include "../vax/dkio.h"
4317120Sbloom #include "mbareg.h"
4417120Sbloom #include "mbavar.h"
4517120Sbloom #include "hpreg.h"
4621Sbill 
472383Swnj /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
482383Swnj struct	size {
4921Sbill 	daddr_t	nblocks;
5021Sbill 	int	cyloff;
5111212Ssam } rp06_sizes[8] = {
52886Sbill 	15884,	0,		/* A=cyl 0 thru 37 */
53886Sbill 	33440,	38,		/* B=cyl 38 thru 117 */
54886Sbill 	340670,	0,		/* C=cyl 0 thru 814 */
5511212Ssam 	15884,	118,		/* D=cyl 118 thru 155 */
5611212Ssam 	55936,	156,		/* E=cyl 156 thru 289 */
5711212Ssam 	219384,	290,		/* F=cyl 290 thru 814 */
5811212Ssam 	291280,	118,		/* G=cyl 118 thru 814 */
5921Sbill 	0,	0,
6011212Ssam }, rp05_sizes[8] = {
6111212Ssam 	15884,	0,		/* A=cyl 0 thru 37 */
6211212Ssam 	33440,	38,		/* B=cyl 38 thru 117 */
6311212Ssam 	171798,	0,		/* C=cyl 0 thru 410 */
6411212Ssam 	15884,	118,		/* D=cyl 118 thru 155 */
6511212Ssam 	55936,	156,		/* E=cyl 156 thru 289 */
6611212Ssam 	50512,	290,		/* F=cyl 290 thru 410 */
6711212Ssam 	122408,	118,		/* G=cyl 118 thru 410 */
6821Sbill 	0,	0,
6911212Ssam }, rm03_sizes[8] = {
70886Sbill 	15884,	0,		/* A=cyl 0 thru 99 */
7111212Ssam 	33440,	100,		/* B=cyl 100 thru 308 */
72886Sbill 	131680,	0,		/* C=cyl 0 thru 822 */
7311212Ssam 	15884,	309,		/* D=cyl 309 thru 408 */
7411212Ssam 	55936,	409,		/* E=cyl 409 thru 758 */
7511212Ssam 	10144,	759,		/* F=cyl 759 thru 822 */
7611212Ssam 	82144,	309,		/* G=cyl 309 thru 822 */
7721Sbill 	0,	0,
7811212Ssam }, rm05_sizes[8] = {
79886Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
80886Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
813271Swnj 	500384,	0,		/* C=cyl 0 thru 822 */
82886Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
83886Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
843729Sroot 	86240,	681,		/* F=cyl 681 thru 822 */
853729Sroot 	158592,	562,		/* G=cyl 562 thru 822 */
86886Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
872383Swnj }, rm80_sizes[8] = {
882383Swnj 	15884,	0,		/* A=cyl 0 thru 36 */
892383Swnj 	33440,	37,		/* B=cyl 37 thru 114 */
902383Swnj 	242606,	0,		/* C=cyl 0 thru 558 */
9111212Ssam 	15884,	115,		/* D=cyl 115 thru 151 */
9211212Ssam 	55936,	152,		/* E=cyl 152 thru 280 */
9311212Ssam 	120559,	281,		/* F=cyl 281 thru 558 */
9411212Ssam 	192603,	115,		/* G=cyl 115 thru 558 */
952383Swnj 	0,	0,
9611212Ssam }, rp07_sizes[8] = {
976927Ssam 	15884,	0,		/* A=cyl 0 thru 9 */
9811212Ssam 	66880,	10,		/* B=cyl 10 thru 51 */
9911212Ssam 	1008000, 0,		/* C=cyl 0 thru 629 */
10011212Ssam 	15884,	235,		/* D=cyl 235 thru 244 */
10111212Ssam 	307200,	245,		/* E=cyl 245 thru 436 */
10211212Ssam 	308650,	437,		/* F=cyl 437 thru 629 */
10311212Ssam 	631850,	235,		/* G=cyl 235 thru 629 */
10411212Ssam 	291346,	52,		/* H=cyl 52 thru 234 */
10511212Ssam }, cdc9775_sizes[8] = {
10611212Ssam 	15884,	0,		/* A=cyl 0 thru 12 */
10711212Ssam 	66880,	13,		/* B=cyl 13 thru 65 */
10815101Skarels 	1077760, 0,		/* C=cyl 0 thru 841 */
10911212Ssam 	15884,	294,		/* D=cyl 294 thru 306 */
11011212Ssam 	307200,	307,		/* E=cyl 307 thru 546 */
11115101Skarels 	377440,	547,		/* F=cyl 547 thru 841 */
11215101Skarels 	701280,	294,		/* G=cyl 294 thru 841 */
11311212Ssam 	291346,	66,		/* H=cyl 66 thru 293 */
11411212Ssam }, cdc9730_sizes[8] = {
1156927Ssam 	15884,	0,		/* A=cyl 0 thru 49 */
1166927Ssam 	33440,	50,		/* B=cyl 50 thru 154 */
1176927Ssam 	263360,	0,		/* C=cyl 0 thru 822 */
11811212Ssam 	15884,	155,		/* D=cyl 155 thru 204 */
11911212Ssam 	55936,	205,		/* E=cyl 205 thru 379 */
12011212Ssam 	141664,	380,		/* F=cyl 380 thru 822 */
12111212Ssam 	213664,	155,		/* G=cyl 155 thru 822 */
1226927Ssam 	0,	0,
12311212Ssam }, capricorn_sizes[8] = {
1246927Ssam 	15884,	0,		/* A=cyl 0 thru 31 */
1256927Ssam 	33440,	32,		/* B=cyl 32 thru 97 */
1266927Ssam 	524288,	0,		/* C=cyl 0 thru 1023 */
12711212Ssam 	15884,	668,		/* D=cyl 668 thru 699 */
12811212Ssam 	55936,	700,		/* E=cyl 700 thru 809 */
12911212Ssam 	109472,	810,		/* F=cyl 810 thru 1023 */
13011212Ssam 	182176,	668,		/* G=cyl 668 thru 1023 */
1316927Ssam 	291346,	98,		/* H=cyl 98 thru 667 */
13211212Ssam }, eagle_sizes[8] = {
13310734Ssam 	15884,	0,		/* A=cyl 0 thru 16 */
13411212Ssam 	66880,	17,		/* B=cyl 17 thru 86 */
13511629Ssam 	808320,	0,		/* C=cyl 0 thru 841 */
13611212Ssam 	15884,	391,		/* D=cyl 391 thru 407 */
13711212Ssam 	307200,	408,		/* E=cyl 408 thru 727 */
13811629Ssam 	109296,	728,		/* F=cyl 728 thru 841 */
13911629Ssam 	432816,	391,		/* G=cyl 391 thru 841 */
14011212Ssam 	291346,	87,		/* H=cyl 87 thru 390 */
14113202Sroot }, ampex_sizes[8] = {
14211281Ssam 	15884,	0,		/* A=cyl 0 thru 26 */
14311281Ssam 	33440,	27,		/* B=cyl 27 thru 81 */
14413184Shelge 	495520,	0,		/* C=cyl 0 thru 814 */
14511281Ssam 	15884,	562,		/* D=cyl 562 thru 588 */
14611281Ssam 	55936,	589,		/* E=cyl 589 thru 680 */
14713184Shelge 	81312,	681,		/* F=cyl 681 thru 814 */
14813184Shelge 	153664,	562,		/* G=cyl 562 thru 814 */
14911281Ssam 	291346,	82,		/* H=cyl 82 thru 561 */
15021Sbill };
1512383Swnj /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
15221Sbill 
1536927Ssam /*
1546927Ssam  * Table for converting Massbus drive types into
1556927Ssam  * indices into the partition tables.  Slots are
1566927Ssam  * left for those drives devined from other means
1576927Ssam  * (e.g. SI, AMPEX, etc.).
1586927Ssam  */
1596927Ssam short	hptypes[] = {
1606927Ssam #define	HPDT_RM03	0
1616927Ssam 	MBDT_RM03,
1626927Ssam #define	HPDT_RM05	1
1636927Ssam 	MBDT_RM05,
1646927Ssam #define	HPDT_RP06	2
1656927Ssam 	MBDT_RP06,
1666927Ssam #define	HPDT_RM80	3
1676927Ssam 	MBDT_RM80,
16813157Ssam #define	HPDT_RP04	4
16913157Ssam 	MBDT_RP04,
17013157Ssam #define	HPDT_RP05	5
1716927Ssam 	MBDT_RP05,
17213157Ssam #define	HPDT_RP07	6
1736927Ssam 	MBDT_RP07,
17413157Ssam #define	HPDT_ML11A	7
1756927Ssam 	MBDT_ML11A,
17613157Ssam #define	HPDT_ML11B	8
1776927Ssam 	MBDT_ML11B,
17813157Ssam #define	HPDT_9775	9
1796927Ssam 	-1,
18013157Ssam #define	HPDT_9730	10
1816927Ssam 	-1,
18213157Ssam #define	HPDT_CAPRICORN	11
1836927Ssam 	-1,
18413157Ssam #define HPDT_EAGLE	12
1859175Ssam 	-1,
18613157Ssam #define	HPDT_9300	13
18711281Ssam 	-1,
18813157Ssam #define HPDT_RM02	14
1899175Ssam 	MBDT_RM02,		/* beware, actually capricorn or eagle */
1906927Ssam 	0
1916927Ssam };
1922978Swnj struct	mba_device *hpinfo[NHP];
19314145Shelge int	hpattach(),hpustart(),hpstart(),hpdtint();
1942383Swnj struct	mba_driver hpdriver =
1952978Swnj 	{ hpattach, 0, hpustart, hpstart, hpdtint, 0,
1962978Swnj 	  hptypes, "hp", 0, hpinfo };
1972383Swnj 
19812836Ssam /*
19916225Skarels  * These variable are all measured in sectors.
20016225Skarels  * Sdist is how much to "lead" in the search for a desired sector
20116225Skarels  * (i.e. if want N, search for N-sdist.)
20216225Skarels  * Maxdist and mindist define the region right before our desired sector within
20316225Skarels  * which we don't bother searching.  We don't search when we are already less
20416225Skarels  * then maxdist and more than mindist sectors "before" our desired sector.
20516225Skarels  * Maxdist should be >= sdist.
20616225Skarels  *
20716225Skarels  * Beware, sdist, mindist and maxdist are not well tuned
20812836Ssam  * for many of the drives listed in this table.
20912836Ssam  * Try patching things with something i/o intensive
21012836Ssam  * running and watch iostat.
21112836Ssam  */
2122383Swnj struct hpst {
21312836Ssam 	short	nsect;		/* # sectors/track */
21412836Ssam 	short	ntrak;		/* # tracks/cylinder */
21512836Ssam 	short	nspc;		/* # sector/cylinders */
21612836Ssam 	short	ncyl;		/* # cylinders */
21712836Ssam 	struct	size *sizes;	/* partition tables */
21812836Ssam 	short	sdist;		/* seek distance metric */
21916225Skarels 	short	maxdist;	/* boundaries of non-searched area */
22016225Skarels 	short	mindist;	/* preceding the target sector */
2212383Swnj } hpst[] = {
22216225Skarels     { 32, 5,	32*5,	823,	rm03_sizes,	7, 4, 1 },	/* RM03 */
22316225Skarels     { 32, 19,	32*19,	823,	rm05_sizes,	7, 4, 1 },	/* RM05 */
22416225Skarels     { 22,19,	22*19,	815,	rp06_sizes,	7, 4, 1 },	/* RP06 */
22516225Skarels     { 31, 14, 	31*14,	559,	rm80_sizes,	7, 4, 1 },	/* RM80 */
22616225Skarels     { 22, 19,	22*19,	411,	rp05_sizes,	7, 4, 1 },	/* RP04 */
22716225Skarels     { 22, 19,	22*19,	411,	rp05_sizes,	7, 4, 1 },	/* RP05 */
22816225Skarels     { 50, 32,	50*32,	630,	rp07_sizes,    15, 8, 3 },	/* RP07 */
22916225Skarels     { 1, 1,	1,	1,	0,		0, 0, 0 },	/* ML11A */
23016225Skarels     { 1, 1,	1,	1,	0,		0, 0, 0 },	/* ML11B */
23116225Skarels     { 32, 40,	32*40,	843,	cdc9775_sizes,	7, 4, 1 },	/* 9775 */
23216225Skarels     { 32, 10,	32*10,	823,	cdc9730_sizes,	7, 4, 1 },	/* 9730 */
23316225Skarels     { 32, 16,	32*16,	1024,	capricorn_sizes,10,4, 3 },	/* Capricorn */
23416225Skarels     { 48, 20,	48*20,	842,	eagle_sizes,   15, 8, 3 },	/* EAGLE */
23516225Skarels     { 32, 19,	32*19,	815,	ampex_sizes,	7, 4, 1 },	/* 9300 */
2362383Swnj };
2372383Swnj 
2382624Swnj u_char	hp_offset[16] = {
2393093Swnj     HPOF_P400, HPOF_M400, HPOF_P400, HPOF_M400,
2403093Swnj     HPOF_P800, HPOF_M800, HPOF_P800, HPOF_M800,
2413093Swnj     HPOF_P1200, HPOF_M1200, HPOF_P1200, HPOF_M1200,
2423093Swnj     0, 0, 0, 0,
24321Sbill };
24421Sbill 
2452624Swnj struct	buf	rhpbuf[NHP];
2463706Sroot struct	buf	bhpbuf[NHP];
2473706Sroot struct	dkbad	hpbad[NHP];
24821Sbill 
24910857Ssam struct	hpsoftc {
25010857Ssam 	u_char	sc_hpinit;	/* drive initialized */
25110857Ssam 	u_char	sc_recal;	/* recalibrate state */
25210857Ssam 	u_char	sc_hdr;		/* next i/o includes header */
25310857Ssam 	u_char	sc_doseeks;	/* perform explicit seeks */
25410857Ssam 	daddr_t	sc_mlsize;	/* ML11 size */
25525198Skarels 	int	sc_blkdone;	/* amount sucessfully transfered */
25617071Sralph 	daddr_t	sc_badbn;	/* replacement block number */
25710857Ssam } hpsoftc[NHP];
25810857Ssam 
25921Sbill #define	b_cylin b_resid
26021Sbill 
2615726Sroot /* #define ML11 0  to remove ML11 support */
2625726Sroot #define	ML11	(hptypes[mi->mi_type] == MBDT_ML11A)
2635726Sroot #define	RP06	(hptypes[mi->mi_type] <= MBDT_RP06)
2645726Sroot #define	RM80	(hptypes[mi->mi_type] == MBDT_RM80)
2655726Sroot 
26624738Sbloom #define hpunit(dev)	(minor(dev) >> 3)
26711202Ssam #define	MASKREG(reg)	((reg)&0xffff)
26818411Skarels #define HPWAIT(mi, addr) (((addr)->hpds & HPDS_DRY) || hpwait(mi))
26911202Ssam 
2702978Swnj /*ARGSUSED*/
2712978Swnj hpattach(mi, slave)
27210734Ssam 	register struct mba_device *mi;
2732604Swnj {
27410734Ssam 
27510734Ssam 	mi->mi_type = hpmaptype(mi);
27610734Ssam 	if (!ML11 && mi->mi_dk >= 0) {
27710734Ssam 		struct hpst *st = &hpst[mi->mi_type];
27810734Ssam 
27910734Ssam 		dk_mspw[mi->mi_dk] = 1.0 / 60 / (st->nsect * 256);
28010734Ssam 	}
28110734Ssam }
28210734Ssam 
28310734Ssam /*
28410734Ssam  * Map apparent MASSBUS drive type into manufacturer
28510734Ssam  * specific configuration.  For SI controllers this is done
28610734Ssam  * based on codes in the serial number register.  For
28710734Ssam  * EMULEX controllers, the track and sector attributes are
28810734Ssam  * used when the drive type is an RM02 (not supported by DEC).
28910734Ssam  */
29010734Ssam hpmaptype(mi)
29110734Ssam 	register struct mba_device *mi;
29210734Ssam {
2936927Ssam 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
29410734Ssam 	register int type = mi->mi_type;
2956927Ssam 
2966927Ssam 	/*
29711281Ssam 	 * Model-byte processing for SI controllers.
2986927Ssam 	 * NB:  Only deals with RM03 and RM05 emulations.
2996927Ssam 	 */
30010734Ssam 	if (type == HPDT_RM03 || type == HPDT_RM05) {
30110734Ssam 		int hpsn = hpaddr->hpsn;
3026927Ssam 
3036927Ssam 		if ((hpsn & SIMB_LU) != mi->mi_drive)
30410734Ssam 			return (type);
3056927Ssam 		switch ((hpsn & SIMB_MB) & ~(SIMB_S6|SIRM03|SIRM05)) {
3066927Ssam 
3076927Ssam 		case SI9775D:
30811281Ssam 			printf("hp%d: 9775 (direct)\n", mi->mi_unit);
30910734Ssam 			type = HPDT_9775;
3106927Ssam 			break;
3116927Ssam 
3126927Ssam 		case SI9730D:
31311281Ssam 			printf("hp%d: 9730 (direct)\n", mi->mi_unit);
31410734Ssam 			type = HPDT_9730;
3156927Ssam 			break;
3166927Ssam 
3177035Swnj 		/*
31811281Ssam 		 * Beware, since the only SI controller we
31911281Ssam 		 * have has a 9300 instead of a 9766, we map the
32011281Ssam 		 * drive type into the 9300.  This means that
32111281Ssam 		 * on a 9766 you lose the last 8 cylinders (argh).
3227035Swnj 		 */
3236927Ssam 		case SI9766:
32411281Ssam 			printf("hp%d: 9300\n", mi->mi_unit);
32511281Ssam 			type = HPDT_9300;
3266927Ssam 			break;
3276927Ssam 
3286927Ssam 		case SI9762:
3296927Ssam 			printf("hp%d: 9762\n", mi->mi_unit);
33010734Ssam 			type = HPDT_RM03;
3316927Ssam 			break;
33211281Ssam 
33311281Ssam 		case SICAPD:
33411281Ssam 			printf("hp%d: capricorn\n", mi->mi_unit);
33511281Ssam 			type = HPDT_CAPRICORN;
33611281Ssam 			break;
33711281Ssam 
33811281Ssam 		case SI9751D:
33911281Ssam 			printf("hp%d: eagle\n", mi->mi_unit);
34011281Ssam 			type = HPDT_EAGLE;
34111281Ssam 			break;
3426927Ssam 		}
34310734Ssam 		return (type);
34410734Ssam 	}
3456927Ssam 
3466927Ssam 	/*
34710734Ssam 	 * EMULEX SC750 or SC780.  Poke the holding register.
3486927Ssam 	 */
34910734Ssam 	if (type == HPDT_RM02) {
35010734Ssam 		int ntracks, nsectors;
35110734Ssam 
35211315Ssam 		hpaddr->hpof = HPOF_FMT22;
35311315Ssam 		mbclrattn(mi);
3546927Ssam 		hpaddr->hpcs1 = HP_NOP;
3556927Ssam 		hpaddr->hphr = HPHR_MAXTRAK;
35611202Ssam 		ntracks = MASKREG(hpaddr->hphr) + 1;
35710055Ssam 		if (ntracks == 16) {
3586927Ssam 			printf("hp%d: capricorn\n", mi->mi_unit);
35910734Ssam 			type = HPDT_CAPRICORN;
36010734Ssam 			goto done;
36110734Ssam 		}
36213157Ssam 		if (ntracks == 19) {
36313157Ssam 			printf("hp%d: 9300\n", mi->mi_unit);
36413157Ssam 			type = HPDT_9300;
36513157Ssam 			goto done;
36613157Ssam 		}
36710734Ssam 		hpaddr->hpcs1 = HP_NOP;
36810734Ssam 		hpaddr->hphr = HPHR_MAXSECT;
36911202Ssam 		nsectors = MASKREG(hpaddr->hphr) + 1;
37011315Ssam 		if (ntracks == 20 && nsectors == 48) {
37110734Ssam 			type = HPDT_EAGLE;
37211212Ssam 			printf("hp%d: eagle\n", mi->mi_unit);
37311315Ssam 			goto done;
37410734Ssam 		}
37511315Ssam 		printf("hp%d: ntracks %d, nsectors %d: unknown device\n",
37613157Ssam 			mi->mi_unit, ntracks, nsectors);
37710734Ssam done:
3786927Ssam 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
37911315Ssam 		mbclrattn(mi);		/* conservative */
38010734Ssam 		return (type);
38110734Ssam 	}
3826927Ssam 
38310734Ssam 	/*
38410734Ssam 	 * Map all ML11's to the same type.  Also calculate
38510734Ssam 	 * transfer rates based on device characteristics.
38610734Ssam 	 */
38710734Ssam 	if (type == HPDT_ML11A || type == HPDT_ML11B) {
38810857Ssam 		register struct hpsoftc *sc = &hpsoftc[mi->mi_unit];
38910857Ssam 		register int trt;
3902604Swnj 
39110857Ssam 		sc->sc_mlsize = hpaddr->hpmr & HPMR_SZ;
3925726Sroot 		if ((hpaddr->hpmr & HPMR_ARRTYP) == 0)
39310857Ssam 			sc->sc_mlsize >>= 2;
3945726Sroot 		if (mi->mi_dk >= 0) {
3955726Sroot 			trt = (hpaddr->hpmr & HPMR_TRT) >> 8;
3965726Sroot 			dk_mspw[mi->mi_dk] = 1.0 / (1<<(20-trt));
3975726Sroot 		}
39810734Ssam 		type = HPDT_ML11A;
3996927Ssam 	}
40010734Ssam 	return (type);
4012604Swnj }
4022604Swnj 
4038579Sroot hpopen(dev)
4048579Sroot 	dev_t dev;
4058579Sroot {
40624738Sbloom 	register int unit = hpunit(dev);
4078579Sroot 	register struct mba_device *mi;
4088579Sroot 
4098579Sroot 	if (unit >= NHP || (mi = hpinfo[unit]) == 0 || mi->mi_alive == 0)
4108579Sroot 		return (ENXIO);
4118579Sroot 	return (0);
4128579Sroot }
4138579Sroot 
41421Sbill hpstrategy(bp)
4152383Swnj 	register struct buf *bp;
41621Sbill {
4172978Swnj 	register struct mba_device *mi;
4182383Swnj 	register struct hpst *st;
4192383Swnj 	register int unit;
42024783Skarels 	long sz;
4212383Swnj 	int xunit = minor(bp->b_dev) & 07;
4225432Sroot 	int s;
42321Sbill 
42421Sbill 	sz = bp->b_bcount;
42521Sbill 	sz = (sz+511) >> 9;
42624738Sbloom 	unit = hpunit(bp->b_dev);
42724738Sbloom 	if (unit >= NHP) {
42824738Sbloom 		bp->b_error = ENXIO;
4292383Swnj 		goto bad;
43024738Sbloom 	}
4312383Swnj 	mi = hpinfo[unit];
43224738Sbloom 	if (mi == 0 || mi->mi_alive == 0) {
43324738Sbloom 		bp->b_error = ENXIO;
4342383Swnj 		goto bad;
43524738Sbloom 	}
4362383Swnj 	st = &hpst[mi->mi_type];
4375726Sroot 	if (ML11) {
43810857Ssam 		struct hpsoftc *sc = &hpsoftc[unit];
43910857Ssam 
4405726Sroot 		if (bp->b_blkno < 0 ||
44124738Sbloom 		    bp->b_blkno+sz > sc->sc_mlsize) {
44224783Skarels 			if (bp->b_blkno == sc->sc_mlsize) {
44324783Skarels 			    bp->b_resid = bp->b_bcount;
44424738Sbloom 			    goto done;
44524783Skarels 			}
44624738Sbloom 			bp->b_error = EINVAL;
4475726Sroot 			goto bad;
44824738Sbloom 		}
4495726Sroot 		bp->b_cylin = 0;
4505726Sroot 	} else {
4515726Sroot 		if (bp->b_blkno < 0 ||
45224783Skarels 		    bp->b_blkno+sz > st->sizes[xunit].nblocks) {
45324783Skarels 			if (bp->b_blkno == st->sizes[xunit].nblocks) {
45424783Skarels 			    bp->b_resid = bp->b_bcount;
45524738Sbloom 			    goto done;
45624783Skarels 			}
45724738Sbloom 			bp->b_error = EINVAL;
4585726Sroot 			goto bad;
45924738Sbloom 		}
46024783Skarels 		bp->b_cylin = bp->b_blkno/st->nspc + st->sizes[xunit].cyloff;
4615726Sroot 	}
4625432Sroot 	s = spl5();
4632383Swnj 	disksort(&mi->mi_tab, bp);
4642383Swnj 	if (mi->mi_tab.b_active == 0)
4652383Swnj 		mbustart(mi);
4665432Sroot 	splx(s);
4672383Swnj 	return;
4682383Swnj 
4692383Swnj bad:
4702383Swnj 	bp->b_flags |= B_ERROR;
47124738Sbloom done:
4722383Swnj 	iodone(bp);
4732383Swnj 	return;
47421Sbill }
47521Sbill 
4762383Swnj hpustart(mi)
4772978Swnj 	register struct mba_device *mi;
47821Sbill {
4792624Swnj 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
4802383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
48111315Ssam 	register struct hpst *st;
48210857Ssam 	struct hpsoftc *sc = &hpsoftc[mi->mi_unit];
48321Sbill 	daddr_t bn;
48416791Skarels 	int sn, tn, dist;
48521Sbill 
48611315Ssam 	st = &hpst[mi->mi_type];
4873706Sroot 	hpaddr->hpcs1 = 0;
4882624Swnj 	if ((hpaddr->hpcs1&HP_DVA) == 0)
4892383Swnj 		return (MBU_BUSY);
49025198Skarels 
49125198Skarels 	switch (sc->sc_recal) {
49225198Skarels 
49325198Skarels 	case 1:
49425198Skarels 		HPWAIT(mi, hpaddr);
49525198Skarels 		hpaddr->hpdc = bp->b_cylin;
49625198Skarels 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
49725198Skarels 		sc->sc_recal++;
49825198Skarels 		return (MBU_STARTED);
49925198Skarels 	case 2:
50025198Skarels 		break;
50125198Skarels 	}
50225198Skarels 	sc->sc_recal = 0;
50310857Ssam 	if ((hpaddr->hpds & HPDS_VV) == 0 || !sc->sc_hpinit) {
5043706Sroot 		struct buf *bbp = &bhpbuf[mi->mi_unit];
5053706Sroot 
50610857Ssam 		sc->sc_hpinit = 1;
5072624Swnj 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
5083140Swnj 		if (mi->mi_mba->mba_drv[0].mbd_as & (1<<mi->mi_drive))
5093140Swnj 			printf("DCLR attn\n");
5102624Swnj 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
5115726Sroot 		if (!ML11)
5125726Sroot 			hpaddr->hpof = HPOF_FMT22;
5133140Swnj 		mbclrattn(mi);
5145726Sroot 		if (!ML11) {
5155726Sroot 			bbp->b_flags = B_READ|B_BUSY;
5165726Sroot 			bbp->b_dev = bp->b_dev;
5175726Sroot 			bbp->b_bcount = 512;
5185726Sroot 			bbp->b_un.b_addr = (caddr_t)&hpbad[mi->mi_unit];
5195726Sroot 			bbp->b_blkno = st->ncyl*st->nspc - st->nsect;
5205726Sroot 			bbp->b_cylin = st->ncyl - 1;
5215726Sroot 			mi->mi_tab.b_actf = bbp;
5225726Sroot 			bbp->av_forw = bp;
5235726Sroot 			bp = bbp;
5245726Sroot 		}
52521Sbill 	}
52625198Skarels 	if (mi->mi_tab.b_active || mi->mi_hd->mh_ndrive == 1) {
52725198Skarels 		if (mi->mi_tab.b_errcnt >= 16 && (bp->b_flags & B_READ)) {
52825198Skarels 			hpaddr->hpof =
52925198Skarels 			    hp_offset[mi->mi_tab.b_errcnt & 017]|HPOF_FMT22;
53025198Skarels 			hpaddr->hpcs1 = HP_OFFSET|HP_GO;
53125198Skarels 			HPWAIT(mi, hpaddr);
53225198Skarels 			mbclrattn(mi);
53325198Skarels 		}
5342383Swnj 		return (MBU_DODATA);
53525198Skarels 	}
5365726Sroot 	if (ML11)
5375726Sroot 		return (MBU_DODATA);
5383093Swnj 	if ((hpaddr->hpds & HPDS_DREADY) != HPDS_DREADY)
5392383Swnj 		return (MBU_DODATA);
54024738Sbloom 	bn = bp->b_blkno;
54116791Skarels 	sn = bn % st->nspc;
54216791Skarels 	tn = sn / st->nsect;
54316791Skarels 	sn = sn % st->nsect;
54411202Ssam 	if (bp->b_cylin == MASKREG(hpaddr->hpdc)) {
54510908Ssam 		if (sc->sc_doseeks)
5462383Swnj 			return (MBU_DODATA);
54716225Skarels 		dist = sn - (MASKREG(hpaddr->hpla) >> 6) - 1;
5482383Swnj 		if (dist < 0)
5492383Swnj 			dist += st->nsect;
55016225Skarels 		if (dist > st->maxdist || dist < st->mindist)
5512383Swnj 			return (MBU_DODATA);
5522614Swnj 	} else
5532614Swnj 		hpaddr->hpdc = bp->b_cylin;
55410908Ssam 	if (sc->sc_doseeks)
5552624Swnj 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
556305Sbill 	else {
55716225Skarels 		sn = (sn + st->nsect - st->sdist) % st->nsect;
55816791Skarels 		hpaddr->hpda = (tn << 8) + sn;
5592624Swnj 		hpaddr->hpcs1 = HP_SEARCH|HP_GO;
560305Sbill 	}
5612383Swnj 	return (MBU_STARTED);
56221Sbill }
56321Sbill 
5642383Swnj hpstart(mi)
5652978Swnj 	register struct mba_device *mi;
56621Sbill {
5672624Swnj 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
5682383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
5692383Swnj 	register struct hpst *st = &hpst[mi->mi_type];
57010857Ssam 	struct hpsoftc *sc = &hpsoftc[mi->mi_unit];
57121Sbill 	daddr_t bn;
57225198Skarels 	int sn, tn, cn;
57321Sbill 
5745726Sroot 	if (ML11)
57525198Skarels 		hpaddr->hpda = bp->b_blkno + sc->sc_blkdone;
5765726Sroot 	else {
57725198Skarels 		if (bp->b_flags & B_BAD) {
57825198Skarels 			bn = sc->sc_badbn;
57925198Skarels 			cn = bn / st->nspc;
58025198Skarels 		} else {
58125198Skarels 			bn = bp->b_blkno;
58225198Skarels 			cn = bp->b_cylin;
58325198Skarels 		}
58425198Skarels 		sn = bn % st->nspc;
58525198Skarels 		if ((bp->b_flags & B_BAD) == 0)
58625198Skarels 			sn += sc->sc_blkdone;
58725198Skarels 		tn = sn / st->nsect;
5885726Sroot 		sn %= st->nsect;
58925198Skarels 		cn += tn / st->ntrak;
59025198Skarels 		tn %= st->ntrak;
5915726Sroot 		hpaddr->hpda = (tn << 8) + sn;
59225198Skarels 		hpaddr->hpdc = cn;
5935726Sroot 	}
59426041Skarels 	mi->mi_tab.b_bdone = dbtob(sc->sc_blkdone);
59510857Ssam 	if (sc->sc_hdr) {
5965726Sroot 		if (bp->b_flags & B_READ)
5975726Sroot 			return (HP_RHDR|HP_GO);
5985726Sroot 		else
5995726Sroot 			return (HP_WHDR|HP_GO);
6005726Sroot 	}
6015726Sroot 	return (0);
60221Sbill }
60321Sbill 
6043102Swnj hpdtint(mi, mbsr)
6052978Swnj 	register struct mba_device *mi;
6063102Swnj 	int mbsr;
60721Sbill {
6082624Swnj 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
6092383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
6105893Swnj 	register int er1, er2;
61110857Ssam 	struct hpsoftc *sc = &hpsoftc[mi->mi_unit];
612*26289Skarels 	int retry = 0;
61326041Skarels 	int npf;
61426041Skarels 	daddr_t bn;
61526041Skarels 	int bcr;
61621Sbill 
61726041Skarels 	bcr = MASKREG(-mi->mi_mba->mba_bcr);
6183102Swnj 	if (hpaddr->hpds&HPDS_ERR || mbsr&MBSR_EBITS) {
61914145Shelge 		er1 = hpaddr->hper1;
62014145Shelge 		er2 = hpaddr->hper2;
62126041Skarels 		if (bp->b_flags & B_BAD) {
62226041Skarels 			npf = bp->b_error;
62326041Skarels 			bn = sc->sc_badbn;
62426041Skarels 		} else {
62526041Skarels 			npf = btop(bp->b_bcount - bcr);
62626041Skarels 			if (er1 & (HPER1_DCK | HPER1_ECH))
62726041Skarels 				npf--;
62826041Skarels 			bn = bp->b_blkno + npf;
62926041Skarels 		}
63018411Skarels 		if (HPWAIT(mi, hpaddr) == 0)
63118411Skarels 			goto hard;
6323706Sroot #ifdef HPDEBUG
6333289Swnj 		if (hpdebug) {
6343706Sroot 			int dc = hpaddr->hpdc, da = hpaddr->hpda;
6353706Sroot 
63625198Skarels 			log(LOG_DEBUG,
63725198Skarels 		    "hperr: bp %x cyl %d blk %d blkdone %d as %o dc %x da %x\n",
63826041Skarels 				bp, bp->b_cylin, bn, sc->sc_blkdone,
63925198Skarels 				hpaddr->hpas&0xff, MASKREG(dc), MASKREG(da));
64026041Skarels 			log(LOG_DEBUG,
64126041Skarels 				"errcnt %d mbsr=%b er1=%b er2=%b bcr -%d\n",
64225198Skarels 				mi->mi_tab.b_errcnt, mbsr, mbsr_bits,
64325198Skarels 				MASKREG(er1), HPER1_BITS,
64426041Skarels 				MASKREG(er2), HPER2_BITS, bcr);
6453289Swnj 		}
6463706Sroot #endif
6475893Swnj 		if (er1 & HPER1_HCRC) {
6485858Swnj 			er1 &= ~(HPER1_HCE|HPER1_FER);
6495893Swnj 			er2 &= ~HPER2_BSE;
6505893Swnj 		}
65116791Skarels 		if (er1 & HPER1_WLE) {
65224844Seric 			log(LOG_WARNING, "hp%d: write locked\n",
65324738Sbloom 			    hpunit(bp->b_dev));
6542826Swnj 			bp->b_flags |= B_ERROR;
65516791Skarels 		} else if (sc->sc_hdr) {
65616791Skarels 			goto hard;
65717071Sralph 		} else if (RM80 && er2&HPER2_SSE) {
65817071Sralph 			(void) hpecc(mi, SSE);
65917071Sralph 			return (MBD_RESTARTED);
66016791Skarels 		} else if ((er2 & HPER2_BSE) && !ML11) {
6615726Sroot 			if (hpecc(mi, BSE))
66211202Ssam 				return (MBD_RESTARTED);
66311202Ssam 			goto hard;
66416791Skarels 		} else if (MASKREG(er1) == HPER1_FER && RP06) {
66516791Skarels 			if (hpecc(mi, BSE))
66616791Skarels 				return (MBD_RESTARTED);
66716791Skarels 			goto hard;
66817071Sralph 		} else if ((er1 & (HPER1_DCK | HPER1_ECH)) == HPER1_DCK &&
66925198Skarels 		    mi->mi_tab.b_errcnt >= 3) {
67016791Skarels 			if (hpecc(mi, ECC))
67116791Skarels 				return (MBD_RESTARTED);
67225398Skarels 			/*
67325398Skarels 			 * ECC corrected.  Only log retries below
67425398Skarels 			 * if we got errors other than soft ECC
67525398Skarels 			 * (as indicated by additional retries).
67625398Skarels 			 */
67725398Skarels 			if (mi->mi_tab.b_errcnt == 3)
67825398Skarels 				mi->mi_tab.b_errcnt = 0;
67924780Skarels 		} else if ((er1 & HPER1_HCRC) && !ML11 && hpecc(mi, BSE)) {
68024780Skarels  			/*
68124780Skarels  			 * HCRC means the header is screwed up and the sector
68224780Skarels  			 * might well exist in the bad sector table,
68324780Skarels 			 * better check....
68424780Skarels  			 */
68524780Skarels 			return (MBD_RESTARTED);
6862826Swnj 		} else if (++mi->mi_tab.b_errcnt > 27 ||
68716791Skarels 		    (ML11 && mi->mi_tab.b_errcnt > 15) ||
6883102Swnj 		    mbsr & MBSR_HARD ||
6895858Swnj 		    er1 & HPER1_HARD ||
6905893Swnj 		    (!ML11 && (er2 & HPER2_HARD))) {
6913706Sroot hard:
69226041Skarels 			bp->b_blkno = bn;		/* XXX */
6932925Swnj 			harderr(bp, "hp");
6943271Swnj 			if (mbsr & (MBSR_EBITS &~ (MBSR_DTABT|MBSR_MBEXC)))
6953271Swnj 				printf("mbsr=%b ", mbsr, mbsr_bits);
6963706Sroot 			printf("er1=%b er2=%b",
69714145Shelge 			    MASKREG(hpaddr->hper1), HPER1_BITS,
69814145Shelge 			    MASKREG(hpaddr->hper2), HPER2_BITS);
69914134Shelge 			if (sc->sc_hdr)
70014134Shelge 				printf(" (hdr i/o)");
7013706Sroot 			printf("\n");
7022826Swnj 			bp->b_flags |= B_ERROR;
70317071Sralph 			bp->b_flags &= ~B_BAD;
7042826Swnj 		} else
7052826Swnj 			retry = 1;
7062826Swnj 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
70725506Skarels 		if (retry && (mi->mi_tab.b_errcnt & 07) == 4) {
7082826Swnj 			hpaddr->hpcs1 = HP_RECAL|HP_GO;
70910857Ssam 			sc->sc_recal = 1;
71025198Skarels 			return (MBD_REPOSITION);
71121Sbill 		}
7122826Swnj 	}
7133706Sroot #ifdef HPDEBUG
7143289Swnj 	else
71510857Ssam 		if (hpdebug && sc->sc_recal) {
71625198Skarels 			log(LOG_DEBUG,
71725198Skarels 			    "recal %d errcnt %d mbsr=%b er1=%b er2=%b\n",
71825198Skarels 			    sc->sc_recal, mi->mi_tab.b_errcnt, mbsr, mbsr_bits,
7193289Swnj 			    hpaddr->hper1, HPER1_BITS,
7203289Swnj 			    hpaddr->hper2, HPER2_BITS);
7213289Swnj 		}
7223706Sroot #endif
72318411Skarels 	HPWAIT(mi, hpaddr);
72425198Skarels 	if (retry)
7252892Swnj 		return (MBD_RETRY);
7263640Swnj 	if (mi->mi_tab.b_errcnt >= 16) {
7273093Swnj 		/*
7283093Swnj 		 * This is fast and occurs rarely; we don't
7293093Swnj 		 * bother with interrupts.
7303093Swnj 		 */
7312624Swnj 		hpaddr->hpcs1 = HP_RTC|HP_GO;
73218411Skarels 		HPWAIT(mi, hpaddr);
7332383Swnj 		mbclrattn(mi);
73421Sbill 	}
73525398Skarels 	if (mi->mi_tab.b_errcnt && (bp->b_flags & B_ERROR) == 0)
73625198Skarels 		log(LOG_INFO, "hp%d%c: %d retries %sing sn%d\n",
73725198Skarels 		    hpunit(bp->b_dev), 'a'+(minor(bp->b_dev)&07),
73825198Skarels 		    mi->mi_tab.b_errcnt,
73925198Skarels 		    (bp->b_flags & B_READ) ? "read" : "writ",
74025198Skarels 		    (bp->b_flags & B_BAD) ?
74125198Skarels 		    sc->sc_badbn : bp->b_blkno + sc->sc_blkdone);
74217071Sralph 	if ((bp->b_flags & B_BAD) && hpecc(mi, CONT))
74317071Sralph 		return (MBD_RESTARTED);
74417071Sralph 	sc->sc_hdr = 0;
74525198Skarels 	sc->sc_blkdone = 0;
74626041Skarels 	bp->b_resid = bcr;
7475726Sroot 	if (!ML11) {
7485726Sroot 		hpaddr->hpof = HPOF_FMT22;
7495726Sroot 		hpaddr->hpcs1 = HP_RELEASE|HP_GO;
7505726Sroot 	}
7512383Swnj 	return (MBD_DONE);
75221Sbill }
75321Sbill 
75418411Skarels /*
75518411Skarels  * Wait (for a bit) for a drive to come ready;
75618411Skarels  * returns nonzero on success.
75718411Skarels  */
75818411Skarels hpwait(mi)
75918411Skarels 	register struct mba_device *mi;
76018411Skarels {
76118411Skarels 	register struct hpdevice *hpaddr = (struct hpdevice *)mi->mi_drv;
76218411Skarels 	register i = 100000;
76318411Skarels 
76418411Skarels 	while ((hpaddr->hpds & HPDS_DRY) == 0 && --i)
76518411Skarels 		DELAY(10);
76618411Skarels 	if (i == 0)
76718411Skarels 		printf("hp%d: intr, not ready\n", mi->mi_unit);
76818411Skarels 	return (i);
76918411Skarels }
77018411Skarels 
7717738Sroot hpread(dev, uio)
7722624Swnj 	dev_t dev;
7737738Sroot 	struct uio *uio;
77421Sbill {
77524738Sbloom 	register int unit = hpunit(dev);
77621Sbill 
7772624Swnj 	if (unit >= NHP)
7788156Sroot 		return (ENXIO);
7798156Sroot 	return (physio(hpstrategy, &rhpbuf[unit], dev, B_READ, minphys, uio));
78021Sbill }
78121Sbill 
7827831Sroot hpwrite(dev, uio)
7832624Swnj 	dev_t dev;
7847831Sroot 	struct uio *uio;
78521Sbill {
78624738Sbloom 	register int unit = hpunit(dev);
78721Sbill 
7882624Swnj 	if (unit >= NHP)
7898156Sroot 		return (ENXIO);
7908156Sroot 	return (physio(hpstrategy, &rhpbuf[unit], dev, B_WRITE, minphys, uio));
79121Sbill }
79221Sbill 
7935726Sroot /*ARGSUSED*/
7947635Ssam hpioctl(dev, cmd, data, flag)
7955726Sroot 	dev_t dev;
7965726Sroot 	int cmd;
7977635Ssam 	caddr_t data;
7985726Sroot 	int flag;
7995726Sroot {
8005726Sroot 
8015726Sroot 	switch (cmd) {
8027635Ssam 
8035726Sroot 	case DKIOCHDR:	/* do header read/write */
80424738Sbloom 		hpsoftc[hpunit(dev)].sc_hdr = 1;
8058579Sroot 		return (0);
8065726Sroot 
8075726Sroot 	default:
8088579Sroot 		return (ENXIO);
8095726Sroot 	}
8105726Sroot }
8115726Sroot 
8123706Sroot hpecc(mi, flag)
8132978Swnj 	register struct mba_device *mi;
8143706Sroot 	int flag;
81521Sbill {
8162383Swnj 	register struct mba_regs *mbp = mi->mi_mba;
8172624Swnj 	register struct hpdevice *rp = (struct hpdevice *)mi->mi_drv;
8182383Swnj 	register struct buf *bp = mi->mi_tab.b_actf;
8193706Sroot 	register struct hpst *st = &hpst[mi->mi_type];
82017071Sralph 	struct hpsoftc *sc = &hpsoftc[mi->mi_unit];
8213706Sroot 	int npf, o;
8222383Swnj 	int bn, cn, tn, sn;
823914Sbill 	int bcr;
82421Sbill 
82518411Skarels 	bcr = MASKREG(-mbp->mba_bcr);
82617071Sralph 	if (bp->b_flags & B_BAD)
8273706Sroot 		npf = bp->b_error;
8283706Sroot 	else
82926041Skarels 		npf = btodb(bp->b_bcount - bcr);
830420Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
83124738Sbloom 	bn = bp->b_blkno;
832420Sbill 	cn = bp->b_cylin;
83318411Skarels 	sn = bn%(st->nspc) + npf;
8342383Swnj 	tn = sn/st->nsect;
8352383Swnj 	sn %= st->nsect;
8362383Swnj 	cn += tn/st->ntrak;
8372383Swnj 	tn %= st->ntrak;
83824187Skarels 	bn += npf;
8393706Sroot 	switch (flag) {
84010856Ssam 	case ECC: {
8413706Sroot 		register int i;
8423706Sroot 		caddr_t addr;
8433706Sroot 		struct pte mpte;
8443706Sroot 		int bit, byte, mask;
8453706Sroot 
8463706Sroot 		npf--;		/* because block in error is previous block */
84718411Skarels 		bn--;
84817071Sralph 		if (bp->b_flags & B_BAD)
84917071Sralph 			bn = sc->sc_badbn;
85024844Seric 		log(LOG_WARNING, "hp%d%c: soft ecc sn%d\n", hpunit(bp->b_dev),
85117071Sralph 		    'a'+(minor(bp->b_dev)&07), bn);
85211202Ssam 		mask = MASKREG(rp->hpec2);
85311202Ssam 		i = MASKREG(rp->hpec1) - 1;		/* -1 makes 0 origin */
8543706Sroot 		bit = i&07;
8553706Sroot 		i = (i&~07)>>3;
8563706Sroot 		byte = i + o;
85726041Skarels 		while (i < 512 && (int)dbtob(npf)+i < bp->b_bcount && bit > -11) {
8583706Sroot 			mpte = mbp->mba_map[npf+btop(byte)];
8593706Sroot 			addr = ptob(mpte.pg_pfnum) + (byte & PGOFSET);
8603706Sroot 			putmemc(addr, getmemc(addr)^(mask<<bit));
8613706Sroot 			byte++;
8623706Sroot 			i++;
8633706Sroot 			bit -= 8;
8643706Sroot 		}
86518411Skarels 		if (bcr == 0)
8663706Sroot 			return (0);
8673847Sroot 		npf++;
8683706Sroot 		break;
8693706Sroot 		}
8703706Sroot 
8713706Sroot 	case SSE:
8723706Sroot 		rp->hpof |= HPOF_SSEI;
87317071Sralph 		if (bp->b_flags & B_BAD) {
87417071Sralph 			bn = sc->sc_badbn;
87517071Sralph 			goto fixregs;
87617071Sralph 		}
8773706Sroot 		mbp->mba_bcr = -(bp->b_bcount - (int)ptob(npf));
8783706Sroot 		break;
8793706Sroot 
8803706Sroot 	case BSE:
88117071Sralph  		if (rp->hpof & HPOF_SSEI)
88217071Sralph  			sn++;
8833706Sroot #ifdef HPBDEBUG
8843706Sroot 		if (hpbdebug)
88525198Skarels 		log(LOG_DEBUG, "hpecc, BSE: bn %d cn %d tn %d sn %d\n", bn, cn, tn, sn);
8863706Sroot #endif
88717071Sralph 		if (bp->b_flags & B_BAD)
88817071Sralph 			return (0);
8893706Sroot 		if ((bn = isbad(&hpbad[mi->mi_unit], cn, tn, sn)) < 0)
89011202Ssam 			return (0);
8913706Sroot 		bp->b_flags |= B_BAD;
8923706Sroot 		bp->b_error = npf + 1;
89317071Sralph  		rp->hpof &= ~HPOF_SSEI;
8943706Sroot 		bn = st->ncyl*st->nspc - st->nsect - 1 - bn;
89517071Sralph 		sc->sc_badbn = bn;
89617071Sralph 	fixregs:
8973706Sroot 		cn = bn/st->nspc;
8983706Sroot 		sn = bn%st->nspc;
8993706Sroot 		tn = sn/st->nsect;
9003706Sroot 		sn %= st->nsect;
90124780Skarels 		mbp->mba_bcr = -(min(512, bp->b_bcount - (int)ptob(npf)));
9023706Sroot #ifdef HPBDEBUG
9033706Sroot 		if (hpbdebug)
90425198Skarels 		log(LOG_DEBUG, "revector to cn %d tn %d sn %d\n", cn, tn, sn);
9053706Sroot #endif
9063706Sroot 		break;
9073706Sroot 
9083706Sroot 	case CONT:
9093706Sroot #ifdef HPBDEBUG
9103706Sroot 		if (hpbdebug)
91125198Skarels 		log(LOG_DEBUG, "hpecc, CONT: bn %d cn %d tn %d sn %d\n", bn,cn,tn,sn);
9123706Sroot #endif
9133706Sroot 		bp->b_flags &= ~B_BAD;
91424780Skarels 		if ((int)ptob(npf) >= bp->b_bcount)
91524780Skarels 			return (0);
9163706Sroot 		mbp->mba_bcr = -(bp->b_bcount - (int)ptob(npf));
9173706Sroot 		break;
9183706Sroot 	}
9193706Sroot 	rp->hpcs1 = HP_DCLR|HP_GO;
92017071Sralph 	if (rp->hpof & HPOF_SSEI)
9212883Swnj 		sn++;
922420Sbill 	rp->hpdc = cn;
923420Sbill 	rp->hpda = (tn<<8) + sn;
924420Sbill 	mbp->mba_sr = -1;
9253706Sroot 	mbp->mba_var = (int)ptob(npf) + o;
9263706Sroot 	rp->hpcs1 = bp->b_flags&B_READ ? HP_RCOM|HP_GO : HP_WCOM|HP_GO;
9273706Sroot 	mi->mi_tab.b_errcnt = 0;	/* error has been corrected */
92825198Skarels 	sc->sc_blkdone = npf;
929420Sbill 	return (1);
93021Sbill }
9312362Swnj 
9322362Swnj #define	DBSIZE	20
9332362Swnj 
9342362Swnj hpdump(dev)
9352362Swnj 	dev_t dev;
9362362Swnj {
9372978Swnj 	register struct mba_device *mi;
9382383Swnj 	register struct mba_regs *mba;
9392624Swnj 	struct hpdevice *hpaddr;
9402362Swnj 	char *start;
9412383Swnj 	int num, unit;
9422383Swnj 	register struct hpst *st;
9432362Swnj 
9442362Swnj 	num = maxfree;
9452362Swnj 	start = 0;
94624738Sbloom 	unit = hpunit(dev);
9472827Swnj 	if (unit >= NHP)
9482827Swnj 		return (ENXIO);
9492383Swnj #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
9502978Swnj 	mi = phys(hpinfo[unit],struct mba_device *);
9512827Swnj 	if (mi == 0 || mi->mi_alive == 0)
9522827Swnj 		return (ENXIO);
9532383Swnj 	mba = phys(mi->mi_hd, struct mba_hd *)->mh_physmba;
9543102Swnj 	mba->mba_cr = MBCR_INIT;
9552624Swnj 	hpaddr = (struct hpdevice *)&mba->mba_drv[mi->mi_drive];
9563093Swnj 	if ((hpaddr->hpds & HPDS_VV) == 0) {
9572624Swnj 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
9582624Swnj 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
9593093Swnj 		hpaddr->hpof = HPOF_FMT22;
9602362Swnj 	}
9612383Swnj 	st = &hpst[mi->mi_type];
96224224Sbloom 	if (dumplo < 0)
9632827Swnj 		return (EINVAL);
96424224Sbloom 	if (dumplo + num >= st->sizes[minor(dev)&07].nblocks)
96524224Sbloom 		num = st->sizes[minor(dev)&07].nblocks - dumplo;
9662362Swnj 	while (num > 0) {
9672383Swnj 		register struct pte *hpte = mba->mba_map;
9682362Swnj 		register int i;
9692383Swnj 		int blk, cn, sn, tn;
9702362Swnj 		daddr_t bn;
9712362Swnj 
9722362Swnj 		blk = num > DBSIZE ? DBSIZE : num;
9732362Swnj 		bn = dumplo + btop(start);
9742383Swnj 		cn = bn/st->nspc + st->sizes[minor(dev)&07].cyloff;
9752383Swnj 		sn = bn%st->nspc;
9762383Swnj 		tn = sn/st->nsect;
9772383Swnj 		sn = sn%st->nsect;
9782362Swnj 		hpaddr->hpdc = cn;
9792362Swnj 		hpaddr->hpda = (tn << 8) + sn;
9802362Swnj 		for (i = 0; i < blk; i++)
9812362Swnj 			*(int *)hpte++ = (btop(start)+i) | PG_V;
9822383Swnj 		mba->mba_sr = -1;
9832383Swnj 		mba->mba_bcr = -(blk*NBPG);
9842383Swnj 		mba->mba_var = 0;
9852624Swnj 		hpaddr->hpcs1 = HP_WCOM | HP_GO;
9863093Swnj 		while ((hpaddr->hpds & HPDS_DRY) == 0)
98716791Skarels 			DELAY(10);
9883093Swnj 		if (hpaddr->hpds&HPDS_ERR)
9892827Swnj 			return (EIO);
9902362Swnj 		start += blk*NBPG;
9912362Swnj 		num -= blk;
9922362Swnj 	}
9932362Swnj 	return (0);
9942362Swnj }
99512506Ssam 
99612506Ssam hpsize(dev)
99712506Ssam 	dev_t dev;
99812506Ssam {
99924738Sbloom 	int unit = hpunit(dev);
100012506Ssam 	struct mba_device *mi;
100112506Ssam 	struct hpst *st;
100212506Ssam 
100312506Ssam 	if (unit >= NHP || (mi = hpinfo[unit]) == 0 || mi->mi_alive == 0)
100412506Ssam 		return (-1);
100512506Ssam 	st = &hpst[mi->mi_type];
100612506Ssam 	return ((int)st->sizes[minor(dev) & 07].nblocks);
100712506Ssam }
10081565Sbill #endif
1009