xref: /csrg-svn/sys/vax/stand/hp.c (revision 11109)
1 /*	hp.c	4.11	83/02/16	*/
2 
3 /*
4  * RP??/RM?? disk driver
5  * with ECC handling and bad block forwarding.
6  * Also supports header io operations and
7  * commands to write check header and data.
8  */
9 
10 #include "../h/param.h"
11 #include "../h/inode.h"
12 #include "../h/fs.h"
13 #include "../h/dkbad.h"
14 
15 #include "../vax/pte.h"
16 #include "../vaxmba/hpreg.h"
17 #include "../vaxmba/mbareg.h"
18 
19 #include "saio.h"
20 #include "savax.h"
21 
22 #define	MASKREG(reg)	((reg)&0xffff)
23 
24 #define MAXBADDESC	126
25 #define SECTSIZ 	512	/* sector size in bytes */
26 #define HDRSIZ		4	/* number of bytes in sector header */
27 #define MAXECC		5	/* the maximum number of bad bits accepted in
28 				 * an ecc error when F_ECCLM is set */
29 
30 char	hp_type[MAXNMBA*8] = { 0 };
31 
32 /* THIS SHOULD BE READ IN OFF THE PACK, PER DRIVE */
33 short	hp6_off[8] =	{ 0, 38, 0, -1, -1, -1, 118, -1 };
34 short	rm3_off[8] =	{ 0, 100, 0, -1, -1, -1, 310, -1 };
35 short	rm5_off[8] =	{ 0, 27, 0, 562, 589, 681, 562, 82 };
36 short	rm80_off[8] =	{ 0, 37, 0, -1, -1, -1, 115, 305 };
37 short	hp7_off[8] = 	{ 0, 10, 0, 330, 340, 500, 330, 50 };
38 short	ml_off[8] =	{ 0, -1, -1, -1, -1, -1, -1, -1 };
39 short	si9775_off[8] =	{ 0, 13, 0, -1, -1, -1, 40, 441 };
40 short	si9730_off[8] = { 0, 50, 0, -1, -1, -1, -1, 155 };
41 short	hpam_off[8] =	{ 0, 32, 0, 668, 723, 778, 668, 98 };
42 short	hpfj_off[8] =	{ 0, 19, 0, -1, -1, -1, 398, 59 };
43 /* END SHOULD BE READ IN */
44 
45 short	hptypes[] =
46     { MBDT_RM03, MBDT_RM05, MBDT_RP06, MBDT_RM80, MBDT_RP05, MBDT_RP07,
47       MBDT_ML11A, MBDT_ML11B, -1/*9755*/, -1/*9730*/, -1/*Capr*/,
48       -1/* eagle */, -1/* 48 sect eagle */, MBDT_RM02, 0};
49 
50 #define RP06 (hptypes[hp_type[unit]] <= MBDT_RP06)
51 #define ML11 (hptypes[hp_type[unit]] == MBDT_ML11A)
52 #define RM80 (hptypes[hp_type[unit]] == MBDT_RM80)
53 
54 u_char	hp_offset[16] = {
55     HPOF_P400, HPOF_M400, HPOF_P400, HPOF_M400,
56     HPOF_P800, HPOF_M800, HPOF_P800, HPOF_M800,
57     HPOF_P1200, HPOF_M1200, HPOF_P1200, HPOF_M1200,
58     0, 0, 0, 0,
59 };
60 
61 struct st hpst[] = {
62 	32,	5,	32*5,	823,	rm3_off,	/* RM03 */
63 	32,	19,	32*19,	823,	rm5_off,	/* RM05 */
64 	22,	19,	22*19,	815,	hp6_off,	/* RP06 */
65 	31,	14, 	31*14,	559,	rm80_off,	/* RM80 */
66 	22,	19,	22*19,	411,	hp6_off,	/* RP06 */
67 	50,	32,	50*32,	630,	hp7_off,	/* RP07 */
68 	1,	1,	1,	1,	ml_off,		/* ML11A */
69 	1,	1,	1,	1,	ml_off,		/* ML11B */
70 	32,	40,	32*40,	843,	si9775_off,	/* 9775 */
71 	32,	10,	32*10,	823,	si9730_off,	/* 9730 */
72 	32,	16,	32*16,	1024,	hpam_off,	/* capricorn */
73 	43,	20,	43*20,	842,	hpfj_off,	/* Eagle */
74 	48,	20,	48*20,	842,	hpfj_off,	/* modif. eagle */
75 	1,	1,	1,	1,	0,	/* rm02 - not used */
76 };
77 
78 struct dkbad hpbad[MAXNMBA*8];
79 int sectsiz;
80 
81 /*
82  * When awaiting command completion, don't
83  * hang on to the status register since
84  * this ties up the controller.
85  */
86 #define	HPWAIT(addr)	while ((((addr)->hpds)&HPDS_DRY)==0) DELAY(500);
87 
88 hpopen(io)
89 	register struct iob *io;
90 {
91 	register unit = io->i_unit;
92 	struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
93 	register struct st *st;
94 
95 	mbainit(UNITTOMBA(unit));
96 	if (hp_type[unit] == 0) {
97 		register type = hpaddr->hpdt & MBDT_TYPE;
98 		register int i;
99 		struct iob tio;
100 
101 		for (i = 0; hptypes[i]; i++)
102 			if (hptypes[i] == type)
103 				goto found;
104 		_stop("unknown drive type");
105 found:
106 		hp_type[unit] = hpmaptype(hpaddr, i, unit);
107 		hpaddr->hpcs1 = HP_DCLR|HP_GO;		/* init drive */
108 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
109 		if (!ML11)
110 			hpaddr->hpof = HPOF_FMT22;
111 		/*
112 		 * Read in the bad sector table:
113 		 *	copy the contents of the io structure
114 		 *	to tio for use during the bb pointer
115 		 *	read operation.
116 		 */
117 		st = &hpst[i];
118 		tio = *io;
119 		tio.i_bn = st->nspc * st->ncyl - st->nsect;
120 		tio.i_ma = (char *)&hpbad[unit];
121 		tio.i_cc = sizeof (struct dkbad);
122 		tio.i_flgs |= F_RDDATA;
123 		for (i = 0; i < 5; i++) {
124 			if (hpstrategy(&tio, READ) == sizeof (struct dkbad))
125 				break;
126 			tio.i_bn += 2;
127 		}
128 		if (i == 5) {
129 			printf("Unable to read bad sector table\n");
130 			for (i = 0; i < MAXBADDESC; i++) {
131 				hpbad[unit].bt_bad[i].bt_cyl = -1;
132 				hpbad[unit].bt_bad[i].bt_trksec = -1;
133 			}
134 		}
135 	}
136 	if (io->i_boff < 0 || io->i_boff > 7 ||
137 	    st->off[io->i_boff]== -1)
138 		_stop("hp bad minor");
139 	io->i_boff = st->off[io->i_boff] * st->nspc;
140 }
141 
142 hpmaptype(hpaddr, type, unit)
143 	register struct hpdevice *hpaddr;
144 	unsigned type;
145 	int unit;
146 {
147 	int ntracks, hpsn;
148 
149 	/*
150 	 * Handle SI model byte stuff when
151 	 * we think it's an RM03 or RM05.
152 	 */
153 	if (type == 0 || type == 1) {
154 		hpsn = hpaddr->hpsn;
155 		if ((hpsn & SIMB_LU) != unit)
156 			return (type);
157 		switch ((hpsn & SIMB_MB) &~ (SIMB_S6|SIRM03|SIRM05)) {
158 		case SI9775D:
159 			return (8);
160 		case SI9730D:
161 			return (9);
162 		case SI9766:
163 			hpaddr->hpcs1 = HP_RECAL|HP_GO;
164 			DELAY(100000);
165 			return (1);
166 
167 		case SI9762:
168 			return (0);
169 		}
170 		return (type);
171 	}
172 	/*
173 	 * RM03: EMULEX controller.  Map to correct
174 	 * drive type by checking the holding
175 	 * register for the disk geometry.
176 	 */
177 	if (type == 13) {
178 		hpaddr->hpcs1 = HP_NOP;
179 		hpaddr->hphr = HPHR_MAXTRAK;
180 		ntracks = MASKREG(hpaddr->hphr) + 1;
181 		if (ntracks == 16)
182 			return (10);	/* AMPEX capricorn */
183 		hpaddr->hphr = HPHR_MAXSECT;
184 		if (ntracks == 48)
185 			return (12);	/* 48 sector Eagle */
186 		if (ntracks == 43)
187 			return (11);	/* 43 sector Eagle */
188 		printf("RM02 with %d tracks?\n", ntracks);
189 		return (type);
190 	}
191 	/*
192 	 * ML11's all map to the same type.
193 	 */
194 	if (type == 6 || type == 7)
195 		return (6);
196 	return (type);
197 }
198 
199 int	ssect;		/* set to 1 if we are on a track with skip sectors */
200 
201 hpstrategy(io, func)
202 	register struct iob *io;
203 {
204 	register unit = io->i_unit;
205 	struct mba_regs *mba = mbamba(unit);
206 	daddr_t bn;
207 	struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
208 	struct st *st = &hpst[hp_type[unit]];
209 	int cn, tn, sn, bytecnt, bytesleft;
210 	daddr_t startblock;
211 	char *membase;
212 	int er1, er2, hprecal;
213 
214 	sectsiz = SECTSIZ;
215 	if ((io->i_flgs & (F_HDR|F_HCHECK)) != 0)
216 		sectsiz += HDRSIZ;
217 	if ((hpaddr->hpds & HPDS_VV) == 0) {
218 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
219 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
220 		if (!ML11)
221 			hpaddr->hpof = HPOF_FMT22;
222 	}
223 	io->i_errcnt = 0;
224 	ssect = 0;
225 	bytecnt = io->i_cc;
226 	membase = io->i_ma;
227 	startblock = io->i_bn;
228 	hprecal = 0;
229 restart:
230 	bn = io->i_bn;
231 	cn = bn/st->nspc;
232 	sn = bn%st->nspc;
233 	tn = sn/st->nsect;
234 	sn = sn%st->nsect + ssect;
235 
236 	HPWAIT(hpaddr);
237 	mba->mba_sr = -1;
238 	if (ML11)
239 		hpaddr->hpda = bn;
240 	else {
241 		hpaddr->hpdc = cn;
242 		hpaddr->hpda = (tn << 8) + sn;
243 	}
244 	if (mbastart(io, func) != 0)		/* start transfer */
245 		return (-1);
246 	HPWAIT(hpaddr);
247 	if ((hpaddr->hpds&HPDS_ERR) == 0 && (mba->mba_sr&MBSR_EBITS) == 0)
248 		return (bytecnt);
249 
250 	/* ------- error handling ------- */
251 
252 	if (bytesleft = MASKREG(mba->mba_bcr>>16))
253 		bytesleft |= 0xffff0000;	/* sign ext */
254 	bn = io->i_bn + (io->i_cc + bytesleft)/sectsiz;
255 	cn = bn/st->nspc;
256 	sn = bn%st->nspc;
257 	tn = sn/st->nsect;
258 	sn = sn%st->nsect;
259 	er1 = MASKREG(hpaddr->hper1);
260 	er2 = MASKREG(hpaddr->hper2);
261 #ifdef HPDEBUG
262 	printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
263 		cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
264 	printf("er1=%b er2=%b", er1, HPER1_BITS, er2, HPER2_BITS);
265 	printf("\nbytes left: %d, of 0x%x, da 0x%x",-bytesleft,
266 	hpaddr->hpof, hpaddr->hpda);
267 	printf("\n");
268 #endif
269 	if (er1 & HPER1_HCRC) {
270 		er1 &= ~(HPER1_HCE|HPER1_FER);
271 		er2 &= ~HPER2_BSE;
272 	}
273 	if (er1&HPER1_WLE) {
274 		printf("hp%d: write locked\n", unit);
275 		return (-1);
276 	}
277 	if (MASKREG(er1) == HPER1_FER && RP06)
278 		goto badsect;
279 	if (++io->i_errcnt > 27 || (er1 & HPER1_HARD) ||
280 	    (!ML11 && (er2 & HPER2_HARD))) {
281 hard0:
282 		io->i_error = EHER;
283 		if (mba->mba_sr & (MBSR_WCKUP|MBSR_WCKLWR))
284 			io->i_error = EWCK;
285 hard:
286 		io->i_errblk = bn + ssect;
287 		printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
288 			   cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
289 		printf("er1=%b er2=%b", er1, HPER1_BITS, er2, HPER2_BITS);
290 		if (hpaddr->hpmr)
291 			printf(" mr1=%o", MASKREG(hpaddr->hpmr));
292 		if (hpaddr->hpmr2)
293 			printf(" mr2=%o", MASKREG(hpaddr->hpmr2));
294 #ifdef HPDEBUG
295 		printf("dc: %d, da: 0x%x",MASKREG(hpaddr->hpdc),
296 		  MASKREG(hpaddr->hpda));
297 #endif
298 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
299 		printf("\n");
300 		return (-1);
301 
302 	}
303 	if ((er2 & HPER2_BSE) && !ML11) {
304 badsect:
305 		if (!ssect && (er2&HPER2_SSE))
306 			goto skipsect;
307 		if (io->i_flgs & F_NBSF) {
308 			io->i_error = EBSE;
309 			goto hard;
310 		}
311 		if (hpecc(io, BSE) == 0)
312 			goto success;
313 		io->i_error = EBSE;
314 		goto hard;
315 	}
316 	if (RM80 && er2&HPER2_SSE) {
317 skipsect:
318 		(void) hpecc(io, SSE);
319 		ssect = 1;
320 		goto success;
321 	}
322 	if ((er1 & (HPER1_DCK|HPER1_ECH)) == HPER1_DCK) {
323 		if (hpecc(io, ECC) == 0)
324 			goto success;
325 		io->i_error = EECC;
326 		return (-1);
327 	}
328 	if (ML11 && (io->i_errcnt >= 16))
329 		goto hard0;
330 	/* fall thru to retry */
331 	hpaddr->hpcs1 = HP_DCLR|HP_GO;
332 	HPWAIT(hpaddr);
333 	if (((io->i_errcnt&07) == 4) ) {
334 		hpaddr->hpcs1 = HP_RECAL|HP_GO;
335 		hprecal = 1;
336 		goto again;
337 	}
338 	switch (hprecal) {
339 
340 	case 1:
341 		hpaddr->hpdc = cn;
342 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
343 		hprecal = 2;
344 		goto again;
345 
346 	case 2:
347 		if (io->i_errcnt < 16 || (io->i_flgs & F_READ) == 0)
348 			goto donerecal;
349 		hpaddr->hpof = hp_offset[io->i_errcnt & 017]|HPOF_FMT22;
350 		hpaddr->hpcs1 = HP_OFFSET|HP_GO;
351 		hprecal = 3;
352 		goto again;
353 
354 	case 3:
355 	donerecal:
356 		hprecal = 0;
357 		goto again;
358 	}
359 	if (io->i_errcnt >= 16) {
360 		hpaddr->hpcs1 = HP_RTC|HP_GO;
361 		while (hpaddr->hpds & HPDS_PIP)
362 			;
363 	}
364 	goto again;
365 
366 success:		 		/* continue with the next block */
367 	bn++;
368 	if ((bn-startblock) * sectsiz < bytecnt) {
369 again:					/* re-read same block */
370 		io->i_bn = bn;
371 		io->i_ma = membase + (io->i_bn - startblock)*sectsiz;
372 		io->i_cc = bytecnt - (io->i_bn - startblock)*sectsiz;
373 #ifdef HPDEBUG
374 		printf("restart: bl %d, byte %d, mem 0x%x hprecal %d\n",
375 			io->i_bn, io->i_cc, io->i_ma, hprecal);
376 #endif
377 		goto restart;
378 	}
379 	return (bytecnt);
380 }
381 
382 hpecc(io, flag)
383 	register struct iob *io;
384 	int flag;
385 {
386 	register unit = io->i_unit;
387 	register struct mba_regs *mbp = mbamba(unit);
388 	register struct hpdevice *rp = (struct hpdevice *)mbadrv(unit);
389 	register struct st *st = &hpst[hp_type[unit]];
390 	int npf, bn, cn, tn, sn, bcr;
391 
392 	if (bcr = MASKREG(mbp->mba_bcr>>16))
393 		bcr |= 0xffff0000;		/* sxt */
394 	npf = (bcr + io->i_cc) / sectsiz;	/* # sectors read */
395 	bn = io->i_bn + npf + ssect;		/* physical block #*/
396 	switch (flag) {
397 
398 	case ECC: {
399 		register int i;
400 		caddr_t addr;
401 		int bit, byte, mask, ecccnt = 0;
402 
403 		printf("hp%d: soft ecc sn%d\n", unit, bn);
404 		mask = MASKREG(rp->hpec2);
405 		i = MASKREG(rp->hpec1) - 1;	/* -1 makes 0 origin */
406 		bit = i&07;
407 		i = (i&~07)>>3;
408 		byte = i;
409 		rp->hpcs1 = HP_DCLR | HP_GO;
410 		while (i <sectsiz && npf*sectsiz + i < io->i_cc && bit > -11) {
411 			addr = io->i_ma + (npf*sectsiz) + byte;
412 #ifdef HPECCDEBUG
413 			printf("addr %x old:%x ",addr, (*addr&0xff));
414 #endif
415 			if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0)
416 				*addr ^= (mask << bit);	/* don't 'correct' mem-
417 							 * ory during Wcheck */
418 #ifdef HPECCDEBUG
419 			printf("new:%x\n",(*addr&0xff));
420 #endif
421 			byte++;
422 			i++;
423 			bit -= 8;
424 			if ((io->i_flgs & F_ECCLM) && ecccnt++ >= MAXECC)
425 				return (1);
426 		}
427 		return(0);
428 	}
429 
430 	/*
431 	 * Skip sector error.
432 	 * Set skip-sector-inhibit and
433 	 * read next sector
434 	 */
435 	case SSE:
436 		rp->hpcs1 = HP_DCLR | HP_GO;
437 		HPWAIT(rp);
438 		rp->hpof |= HPOF_SSEI;
439 		return (0);
440 
441 	case BSE: {
442 		int bbn;
443 
444 		rp->hpcs1 = HP_DCLR | HP_GO;
445 #ifdef HPDEBUG
446 		printf("hpecc: BSE @ bn %d\n", bn);
447 #endif
448 		cn = bn/st->nspc;
449 		sn = bn%st->nspc;
450 		tn = sn/st->nsect;
451 		sn = sn%st->nsect;
452 		bcr += sectsiz;
453 		if ((bbn = isbad(&hpbad[unit], cn, tn, sn)) < 0)
454 			return (1);
455 		bbn = st->ncyl*st->nspc - st->nsect - 1 - bbn;
456 		cn = bbn/st->nspc;
457 		sn = bbn%st->nspc;
458 		tn = sn/st->nsect;
459 		sn = sn%st->nsect;
460 		io->i_cc = sectsiz;
461 		io->i_ma += npf*sectsiz;
462 #ifdef HPDEBUG
463 		printf("revector to cn %d tn %d sn %d mem: 0x%x\n",
464 			cn, tn, sn, io->i_ma);
465 #endif
466 		rp->hpof &= ~HPOF_SSEI;	/* clear skip sector inhibit if set */
467 		mbp->mba_sr = -1;
468 		rp->hpdc = cn;
469 		rp->hpda = (tn<<8) + sn;
470 		mbastart(io,io->i_flgs);
471 		io->i_errcnt = 0;	/* error has been corrected */
472 		HPWAIT(rp);
473 		return (rp->hpds&HPDS_ERR);
474 	}
475 	}
476 	printf("hpecc: flag=%d\n", flag);
477 	return (1);
478 }
479 /*ARGSUSED*/
480 hpioctl(io, cmd, arg)
481 	struct iob *io;
482 	int cmd;
483 	caddr_t arg;
484 {
485 	register unit = io->i_unit;
486 	struct st *st = &hpst[hp_type[unit]], *tmp;
487 	struct mba_drv *drv = mbadrv(unit);
488 
489 	switch(cmd) {
490 
491 	case SAIODEVDATA:
492 		if ((drv->mbd_dt&MBDT_TAP) == 0) {
493 			tmp = (struct st *)arg;
494 			*tmp = *st;
495 			return (0);
496 		}
497 		return (ECMD);
498 
499 	case SAIOSSI:			/* skip-sector-inhibit */
500 		if (drv->mbd_dt&MBDT_TAP)
501 			return (ECMD);
502 		if ((io->i_flgs&F_SSI) == 0) {
503 			/* make sure this is done once only */
504 			io->i_flgs |= F_SSI;
505 			st->nsect++;
506 			st->nspc += st->ntrak;
507 		}
508 		return (0);
509 
510 	case SAIONOSSI:			/* remove skip-sector-inhibit */
511 		if (io->i_flgs & F_SSI) {
512 			io->i_flgs &= ~F_SSI;
513 			drv->mbd_of &= ~HPOF_SSEI;
514 			st->nsect--;
515 			st->nspc -= st->ntrak;
516 		}
517 		return(0);
518 
519 	case SAIOSSDEV:			/* drive have skip sector? */
520 		return (RM80 ? 0 : ECMD);
521 	}
522 	return (ECMD);
523 }
524