xref: /csrg-svn/sys/vax/stand/hp.c (revision 10647)
1 /*	hp.c	4.7	83/02/01	*/
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 */, 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 	1,	1,	1,	1,	0,	/* rm02 - not used */
75 };
76 struct dkbad hpbad[MAXNMBA*8];
77 int sectsiz;
78 
79 hpopen(io)
80 	register struct iob *io;
81 {
82 	register unit = io->i_unit;
83 	struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
84 	register struct st *st;
85 
86 	mbainit(UNITTOMBA(unit));
87 	if (hp_type[unit] == 0) {
88 		register type = hpaddr->hpdt & MBDT_TYPE;
89 		register int i;
90 		struct iob tio;
91 
92 		for (i = 0; hptypes[i]; i++)
93 			if (hptypes[i] == type)
94 				goto found;
95 		_stop("unknown drive type");
96 found:
97 		switch (i) {
98 		case 0: case 1:	{	/* rm03 or rm05 */
99 			register hpsn = hpaddr->hpsn;
100 
101 			if ((hpsn & SIMB_LU) != unit)
102 				break;
103 			switch ((hpsn & SIMB_MB) &~ (SIMB_S6|SIRM03|SIRM05)) {
104 			case SI9775D:
105 				i = 8;	/* si9775 */
106 				break;
107 			case SI9730D:
108 				i = 9;	/* si9730 */
109 				break;
110 			case SI9766:
111 				i = 1;	/* rm05 */
112 				hpaddr->hpcs1 = HP_RECAL|HP_GO;
113 				DELAY(100000);
114 				break;
115 			case SI9762:
116 				i = 0;	/* rm03 */
117 				break;
118 			}
119 			break;
120 		}
121 
122 		case 12:		/* rm02 */
123 			hpaddr->hpcs1 = HP_NOP;
124 			hpaddr->hphr = HPHR_MAXTRAK;
125 			if (MASKREG(hpaddr->hphr) == 15)
126 				i = 10;		/* ampex capricorn */
127 			else
128 				i = 11;		/* eagle */
129 			break;
130 
131 		case 6: case 7:		/* ml11a ml11b */
132 			i = 6;			/* ml11a */
133 			break;
134 		}
135 		hp_type[unit] = i;
136 		hpaddr->hpcs1 = HP_DCLR|HP_GO;		/* init drive */
137 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
138 		if (!ML11)
139 			hpaddr->hpof = HPOF_FMT22;
140 		/*
141 		 * Read in the bad sector table:
142 		 *	copy the contents of the io structure
143 		 *	to tio for use during the bb pointer
144 		 *	read operation.
145 		 */
146 		st = &hpst[i];
147 		tio = *io;
148 		tio.i_bn = st->nspc * st->ncyl - st->nsect;
149 		tio.i_ma = (char *)&hpbad[unit];
150 		tio.i_cc = sizeof (struct dkbad);
151 		tio.i_flgs |= F_RDDATA;
152 		for (i = 0; i < 5; i++) {
153 			if (hpstrategy(&tio, READ) == sizeof (struct dkbad))
154 				break;
155 			tio.i_bn += 2;
156 		}
157 		if (i == 5) {
158 			printf("Unable to read bad sector table\n");
159 			for (i = 0; i < MAXBADDESC; i++) {
160 				hpbad[unit].bt_bad[i].bt_cyl = -1;
161 				hpbad[unit].bt_bad[i].bt_trksec = -1;
162 			}
163 		}
164 	}
165 	if (io->i_boff < 0 || io->i_boff > 7 ||
166 	    st->off[io->i_boff]== -1)
167 		_stop("hp bad minor");
168 	io->i_boff = st->off[io->i_boff] * st->nspc;
169 }
170 
171 int	ssect;		/* set to 1 if we are on a track with skip sectors */
172 
173 hpstrategy(io, func)
174 	register struct iob *io;
175 {
176 	register unit = io->i_unit;
177 	struct mba_regs *mba = mbamba(unit);
178 	daddr_t bn;
179 	struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
180 	struct st *st = &hpst[hp_type[unit]];
181 	int cn, tn, sn, bytecnt, bytesleft;
182 	daddr_t startblock;
183 	char *membase;
184 	int er1, er2, hprecal;
185 
186 	sectsiz = SECTSIZ;
187 	if ((io->i_flgs & (F_HDR|F_HCHECK)) != 0)
188 		sectsiz += HDRSIZ;
189 	if ((hpaddr->hpds & HPDS_VV) == 0) {
190 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
191 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
192 		if (!ML11)			/* any but ml11 */
193 			hpaddr->hpof = HPOF_FMT22;
194 	}
195 	io->i_errcnt = 0;
196 	ssect = 0;
197 	bytecnt = io->i_cc;
198 	membase = io->i_ma;
199 	startblock = io->i_bn;
200 	hprecal = 0;
201 restart:
202 	bn = io->i_bn;
203 	cn = bn/st->nspc;
204 	sn = bn%st->nspc;
205 	tn = sn/st->nsect;
206 	sn = sn%st->nsect + ssect;
207 
208 	while ((hpaddr->hpds & HPDS_DRY) == 0)
209 		;
210 	mba->mba_sr = -1;
211 	if (ML11)
212 		hpaddr->hpda = bn;
213 	else {
214 		hpaddr->hpdc = cn;
215 		hpaddr->hpda = (tn << 8) + sn;
216 	}
217 	if (mbastart(io, func) != 0)		/* start transfer */
218 		return (-1);
219 
220 	while ((hpaddr->hpds & HPDS_DRY) == 0)
221 		;
222 	if (((hpaddr->hpds&HPDS_ERR) | (mba->mba_sr&MBSR_EBITS)) == 0 )
223 		return(bytecnt);
224 
225 	/* ------- error handling ------- */
226 
227 	if (bytesleft = MASKREG(mba->mba_bcr>>16))
228 		bytesleft |= 0xffff0000;	/* sign ext */
229 	bn = io->i_bn + (io->i_cc + bytesleft)/sectsiz;
230 	cn = bn/st->nspc;
231 	sn = bn%st->nspc;
232 	tn = sn/st->nsect;
233 	sn = sn%st->nsect;
234 	er1 = MASKREG(hpaddr->hper1);
235 	er2 = MASKREG(hpaddr->hper2);
236 #ifdef HPDEBUG
237 	printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
238 		cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
239 	printf("er1=%b er2=%b", er1, HPER1_BITS, er2, HPER2_BITS);
240 	printf("\nbytes left: %d, of 0x%x, da 0x%x",-bytesleft,
241 	hpaddr->hpof, hpaddr->hpda);
242 	printf("\n");
243 #endif
244 	if (er1 & HPER1_HCRC) {
245 		er1 &= ~(HPER1_HCE|HPER1_FER);
246 		er2 &= ~HPER2_BSE;
247 	}
248 	if (er1&HPER1_WLE) {
249 		printf("hp%d: write locked\n", unit);
250 		return(-1);
251 	} else if (MASKREG(er1) == HPER1_FER && RP06) {
252 		goto badsect;
253 
254 	} else if (++io->i_errcnt > 27 ||
255 		   er1 & HPER1_HARD ||
256 		   (!ML11 && (er2 & HPER2_HARD))) {
257 hard0:
258 		io->i_error = EHER;
259 		if ((mba->mba_sr & (MBSR_WCKUP | MBSR_WCKLWR)) != 0)
260 			io->i_error = EWCK;
261 hard:
262 		io->i_errblk = bn + ssect;
263 		printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
264 			   cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
265 		printf("er1=%b er2=%b",
266 			   er1, HPER1_BITS,
267 			   er2, HPER2_BITS);
268 		if (hpaddr->hpmr)
269 			printf(" mr1=%o", MASKREG(hpaddr->hpmr));
270 		if (hpaddr->hpmr2)
271 			printf(" mr2=%o", MASKREG(hpaddr->hpmr2));
272 #ifdef HPDEBUG
273 		printf("dc: %d, da: 0x%x",MASKREG(hpaddr->hpdc),
274 					  MASKREG(hpaddr->hpda));
275 #endif
276 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
277 		printf("\n");
278 		return(-1);
279 
280 	} else if ((er2 & HPER2_BSE) && !ML11) {
281 badsect:
282 		if (!ssect && (er2&HPER2_SSE))
283 			goto skipsect;
284 		if ((io->i_flgs & F_NBSF) != 0) {
285 			io->i_error = EBSE;
286 			goto hard;
287 		}
288 		if (hpecc(io, BSE) == 0)
289 			goto success;
290 		else {
291 			io->i_error = EBSE;
292 			goto hard;
293 		}
294 	} else if (RM80 && er2&HPER2_SSE) {
295 skipsect:
296 	/* skip sector error */
297 		(void) hpecc(io, SSE);
298 		ssect=1;
299 		goto success;
300 	} else if ((er1&(HPER1_DCK|HPER1_ECH))==HPER1_DCK) {
301 		if ( hpecc(io, ECC) == 0)
302 			goto success;
303 		else {
304 			io->i_error = EECC;
305 			return(1);
306 		}
307 	}
308 
309 	if (ML11 && (io->i_errcnt >= 16))
310 			goto hard0;
311 
312 	/* fall thru to retry */
313 
314 	hpaddr->hpcs1 = HP_DCLR|HP_GO;
315 	while ((hpaddr->hpds & HPDS_DRY) == 0)
316 		;
317 	if (((io->i_errcnt&07) == 4) ) {
318 		hpaddr->hpcs1 = HP_RECAL|HP_GO;
319 		hprecal = 1;
320 		goto try_again;
321 	}
322 	switch (hprecal) {
323 
324 	case 1:
325 		hpaddr->hpdc = cn;
326 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
327 		hprecal++;
328 		goto try_again;
329 	case 2:
330 		if (io->i_errcnt < 16 || (io->i_flgs & F_READ) == 0)
331 			goto donerecal;
332 		hpaddr->hpof = hp_offset[io->i_errcnt & 017]|HPOF_FMT22;
333 		hpaddr->hpcs1 = HP_OFFSET|HP_GO;
334 		hprecal++;
335 		goto try_again;
336 	donerecal:
337 	case 3:
338 		hprecal = 0;
339 		goto try_again;
340 	}
341 	if (io->i_errcnt >= 16) {
342 		hpaddr->hpcs1 = HP_RTC|HP_GO;
343 		while (hpaddr->hpds & HPDS_PIP)
344 			;
345 	}
346 	goto try_again;
347 success:		 /* continue with the next block */
348 	bn++;
349 	if ((bn-startblock) * sectsiz < bytecnt) {
350 
351 try_again:		/* re-read same block */
352 		io->i_bn = bn;
353 		io->i_ma = membase + (io->i_bn - startblock)*sectsiz;
354 		io->i_cc = bytecnt - (io->i_bn - startblock)*sectsiz;
355 #ifdef HPDEBUG
356 		printf("restart: bl %d, byte %d, mem 0x%x hprecal %d\n",
357 			io->i_bn, io->i_cc, io->i_ma, hprecal);
358 #endif
359 		goto restart;
360 	}
361 	return (bytecnt);
362 }
363 hpecc(io, flag)
364 	register struct iob *io;
365 	int flag;
366 {
367 	register unit = io->i_unit;
368 	register struct mba_regs *mbp = mbamba(unit);
369 	register struct hpdevice *rp = (struct hpdevice *)mbadrv(unit);
370 	register struct st *st = &hpst[hp_type[unit]];
371 	int npf;
372 	int bn, cn, tn, sn;
373 	int bcr;
374 
375 	if (bcr = MASKREG(mbp->mba_bcr>>16))
376 		bcr |= 0xffff0000;		/* sxt */
377 	npf = (bcr + io->i_cc)/sectsiz;		/* number of sectors read */
378 	bn = io->i_bn + npf + ssect;		/* bn is physical block number*/
379 	switch (flag) {
380 	case ECC:
381 		{
382 		register int i;
383 		caddr_t addr;
384 		int bit, byte, mask, ecccnt = 0;
385 
386 		printf("hp%d: soft ecc sn%d\n", unit, bn);
387 		mask = MASKREG(rp->hpec2);
388 		i = MASKREG(rp->hpec1) - 1;		/* -1 makes 0 origin */
389 		bit = i&07;
390 		i = (i&~07)>>3;
391 		byte = i;
392 		rp->hpcs1 = HP_DCLR | HP_GO;
393 		while (i <sectsiz && npf*sectsiz + i < io->i_cc && bit > -11) {
394 			addr = io->i_ma + (npf*sectsiz) + byte;
395 #ifdef HPECCDEBUG
396 			printf("addr %x old:%x ",addr, (*addr&0xff));
397 #endif
398 			if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0)
399 				*addr ^= (mask << bit);	/* don't 'correct' mem-
400 							 * ory during Wcheck */
401 #ifdef HPECCDEBUG
402 			printf("new:%x\n",(*addr&0xff));
403 #endif
404 			byte++;
405 			i++;
406 			bit -= 8;
407 			if ((ecccnt++>=MAXECC) && ((io->i_flgs&F_ECCLM) != 0))
408 				return(1);
409 		}
410 		return(0);
411 		}
412 
413 	case SSE:	/* skip sector error */
414 			/* set skip-sector-inhibit and read next sector */
415 		rp->hpcs1 = HP_DCLR | HP_GO;
416 		while(rp->hpds & HPDS_DRY == 0)
417 			;			/* avoid RMR error */
418 		rp->hpof |= HPOF_SSEI;
419 		return(0);
420 
421 #ifndef NOBADSECT
422 	case BSE:
423 		{
424 		int bbn;
425 		rp->hpcs1 = HP_DCLR | HP_GO;
426 #ifdef HPDEBUG
427 		printf("hpecc: BSE @ bn %d\n", bn);
428 #endif
429 		cn = bn/st->nspc;
430 		sn = bn%st->nspc;
431 		tn = sn/st->nsect;
432 		sn = sn%st->nsect;
433 		bcr += sectsiz;
434 		if ((bbn = isbad(&hpbad[unit], cn, tn, sn)) < 0)
435 			return(1);
436 		bbn = st->ncyl*st->nspc - st->nsect - 1 - bbn;
437 		cn = bbn/st->nspc;
438 		sn = bbn%st->nspc;
439 		tn = sn/st->nsect;
440 		sn = sn%st->nsect;
441 		io->i_cc = sectsiz;
442 		io->i_ma += npf*sectsiz;
443 #ifdef HPDEBUG
444 		printf("revector to cn %d tn %d sn %d mem: 0x%x\n",
445 			cn, tn, sn, io->i_ma);
446 #endif
447 		rp->hpof &= ~HPOF_SSEI;	/* clear skip sector inhibit if set */
448 		mbp->mba_sr = -1;
449 		rp->hpdc = cn;
450 		rp->hpda = (tn<<8) + sn;
451 		mbastart(io,io->i_flgs);
452 		io->i_errcnt = 0;	/* error has been corrected */
453 		while(rp->hpds & HPDS_DRY == 0)
454 			;		/* wait for the read to complete */
455 		if (rp->hpds&HPDS_ERR)
456 			return(1);
457 		else
458 			return(0);
459 		}
460 	}
461 }
462 /*ARGSUSED*/
463 hpioctl(io, cmd, arg)
464 	struct iob *io;
465 	int cmd;
466 	caddr_t arg;
467 {
468 
469 	register unit = io->i_unit;
470 	struct st *st = &hpst[hp_type[unit]], *tmp;
471 	struct mba_drv *drv = mbadrv(unit);
472 
473 	switch(cmd) {
474 
475 	case SAIODEVDATA:
476 		if ((drv->mbd_dt&MBDT_TAP) == 0) {
477 			tmp = (struct st *)arg;
478 			*tmp = *st;
479 			return(0);
480 		}
481 		else
482 			return(ECMD);
483 
484 	case SAIOSSI:			/* set the skip-sector-inhibit flag */
485 		if ((drv->mbd_dt&MBDT_TAP) == 0) {
486 			if ((io->i_flgs&F_SSI)==0) { /* make sure this is */
487 				io->i_flgs |= F_SSI;  /* done only once    */
488 				st->nsect++;
489 				st->nspc += st->ntrak;
490 			}
491 			return(0);
492 		} else
493 			return(ECMD);
494 
495 	case SAIONOSSI:			/* remove the skip-sector-inh. flag */
496 		if (io->i_flgs & F_SSI) {
497 			io->i_flgs &= ~F_SSI;
498 			drv->mbd_of &= ~HPOF_SSEI;
499 			st->nsect--;
500 			st->nspc -= st->ntrak;
501 		}
502 		return(0);
503 
504 	case SAIOSSDEV:			/* return null if device has skip sector
505 					 * handling, otherwise return ECMD
506 					 */
507 		if (RM80)
508 			return(0);
509 		else
510 			return(ECMD);
511 
512 	default:
513 		return (ECMD);
514 	}
515 }
516 
517