xref: /csrg-svn/sys/vax/stand/hp.c (revision 18515)
1 /*	hp.c	6.6	85/03/27	*/
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 #include "../h/param.h"
10 #include "../h/inode.h"
11 #include "../h/fs.h"
12 #include "../h/dkbad.h"
13 
14 #include "../vax/pte.h"
15 #include "../vaxmba/hpreg.h"
16 #include "../vaxmba/mbareg.h"
17 
18 #include "saio.h"
19 #include "savax.h"
20 
21 #define	MASKREG(reg)	((reg)&0xffff)
22 
23 #define	MAXBADDESC	126
24 #define	SECTSIZ 	512	/* sector size in bytes */
25 #define	HDRSIZ		4	/* number of bytes in sector header */
26 #define	MAXECC		5	/* max # bits allow in ecc error w/ F_ECCLM */
27 
28 char	hp_type[MAXNMBA*8] = { 0 };
29 extern	struct st hpst[];
30 
31 short	hptypes[] = {
32 	MBDT_RM03,
33 	MBDT_RM05,
34 	MBDT_RP06,
35 	MBDT_RM80,
36 	MBDT_RP05,
37 	MBDT_RP07,
38 	MBDT_ML11A,
39 	MBDT_ML11B,
40 	-1,		/* 9755 */
41 	-1,		/* 9730 */
42 	-1,		/* Capricorn */
43 	-1,		/* Eagle */
44 	MBDT_RM02,	/* actually something else */
45 	-1,		/* 9300 */
46 	0
47 };
48 
49 #define	RP06 (hptypes[hp_type[unit]] <= MBDT_RP06)
50 #define	ML11 (hptypes[hp_type[unit]] == MBDT_ML11A)
51 #define	RM80 (hptypes[hp_type[unit]] == MBDT_RM80)
52 
53 u_char	hp_offset[16] = {
54     HPOF_P400, HPOF_M400, HPOF_P400, HPOF_M400,
55     HPOF_P800, HPOF_M800, HPOF_P800, HPOF_M800,
56     HPOF_P1200, HPOF_M1200, HPOF_P1200, HPOF_M1200,
57     0, 0, 0, 0,
58 };
59 
60 struct	dkbad hpbad[MAXNMBA*8];
61 int	ssect[MAXNMBA*8];		/* 1 when on track w/skip sector */
62 
63 int	hpdebug[MAXNMBA*8];
64 #define	HPF_BSEDEBUG	01	/* debugging bad sector forwarding */
65 #define	HPF_ECCDEBUG	02	/* debugging ecc correction */
66 
67 int	sectsiz;
68 
69 /*
70  * When awaiting command completion, don't
71  * hang on to the status register since
72  * this ties up some controllers.
73  */
74 #define	HPWAIT(addr) \
75 	while ((((addr)->hpds)&HPDS_DRY)==0) DELAY(500);
76 
77 hpopen(io)
78 	register struct iob *io;
79 {
80 	register unit = io->i_unit;
81 	struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
82 	register struct st *st;
83 
84 	mbainit(UNITTOMBA(unit));
85 	if (hp_type[unit] == 0) {
86 		register i, type = hpaddr->hpdt & MBDT_TYPE;
87 		struct iob tio;
88 
89 		for (i = 0; hptypes[i]; i++)
90 			if (hptypes[i] == type)
91 				goto found;
92 		_stop("unknown drive type");
93 found:
94 		hpaddr->hpcs1 = HP_DCLR|HP_GO;		/* init drive */
95 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
96 		if (!ML11)
97 			hpaddr->hpof = HPOF_FMT22;
98 		hp_type[unit] = hpmaptype(hpaddr, i, UNITTODRIVE(unit));
99 		/*
100 		 * Read in the bad sector table.
101 		 */
102 		st = &hpst[hp_type[unit]];
103 		tio = *io;
104 		tio.i_bn = st->nspc * st->ncyl - st->nsect;
105 		tio.i_ma = (char *)&hpbad[unit];
106 		tio.i_cc = sizeof (struct dkbad);
107 		tio.i_flgs |= F_RDDATA;
108 		for (i = 0; i < 5; i++) {
109 			if (hpstrategy(&tio, READ) == sizeof (struct dkbad))
110 				break;
111 			tio.i_bn += 2;
112 		}
113 		if (i == 5) {
114 			printf("Unable to read bad sector table\n");
115 			for (i = 0; i < MAXBADDESC; i++) {
116 				hpbad[unit].bt_bad[i].bt_cyl = -1;
117 				hpbad[unit].bt_bad[i].bt_trksec = -1;
118 			}
119 		}
120 	}
121 	st = &hpst[hp_type[unit]];
122 	if (io->i_boff < 0 || io->i_boff > 7 ||
123 	    st->off[io->i_boff]== -1)
124 		_stop("hp bad minor");
125 	io->i_boff = st->off[io->i_boff] * st->nspc;
126 }
127 
128 hpstrategy(io, func)
129 	register struct iob *io;
130 {
131 	register unit = io->i_unit;
132 	struct mba_regs *mba = mbamba(unit);
133 	daddr_t bn, startblock;
134 	struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
135 	struct st *st = &hpst[hp_type[unit]];
136 	int cn, tn, sn, bytecnt, bytesleft;
137 	char *membase;
138 	int er1, er2, hprecal;
139 
140 	sectsiz = SECTSIZ;
141 	if ((io->i_flgs & (F_HDR|F_HCHECK)) != 0)
142 		sectsiz += HDRSIZ;
143 	if ((hpaddr->hpds & HPDS_VV) == 0) {
144 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
145 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
146 		if (!ML11)
147 			hpaddr->hpof = HPOF_FMT22;
148 	}
149 	io->i_errcnt = 0;
150 	ssect[unit] = 0;
151 	bytecnt = io->i_cc;
152 	membase = io->i_ma;
153 	startblock = io->i_bn;
154 	hprecal = 0;
155 
156 restart:
157 	bn = io->i_bn;
158 	cn = bn/st->nspc;
159 	sn = bn%st->nspc;
160 	tn = sn/st->nsect;
161 	sn = sn%st->nsect + ssect[unit];
162 
163 	HPWAIT(hpaddr);
164 	mba->mba_sr = -1;
165 	if (ML11)
166 		hpaddr->hpda = bn;
167 	else {
168 		hpaddr->hpdc = cn;
169 		hpaddr->hpda = (tn << 8) + sn;
170 	}
171 	if (mbastart(io, func) != 0)		/* start transfer */
172 		return (-1);
173 	HPWAIT(hpaddr);
174 	/*
175 	 * Successful data transfer, return.
176 	 */
177 	if ((hpaddr->hpds&HPDS_ERR) == 0 && (mba->mba_sr&MBSR_EBITS) == 0)
178 		goto done;
179 
180 	/*
181 	 * Error handling.  Calculate location of error.
182 	 */
183 	bytesleft = MASKREG(mba->mba_bcr);
184 	if (bytesleft)
185 		bytesleft |= 0xffff0000;	/* sxt */
186 	bn = io->i_bn + (io->i_cc + bytesleft) / sectsiz;
187 	er1 = MASKREG(hpaddr->hper1);
188 	er2 = MASKREG(hpaddr->hper2);
189 	if (er1 & (HPER1_DCK|HPER1_ECH))
190 		bn--;	/* Error is in Prev block */
191 	cn = bn/st->nspc;
192 	sn = bn%st->nspc;
193 	tn = sn/st->nsect;
194 	sn = sn%st->nsect;
195 	if (hpdebug[unit] & (HPF_ECCDEBUG|HPF_BSEDEBUG)) {
196 		printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b\n",
197 			cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
198 		printf("er1=%b er2=%b\n", er1, HPER1_BITS, er2, HPER2_BITS);
199 		printf("bytes left: %d, of 0x%x, da 0x%x\n",-bytesleft,
200 			hpaddr->hpof, hpaddr->hpda);
201 	}
202 	if (er1 & HPER1_HCRC) {
203 		er1 &= ~(HPER1_HCE|HPER1_FER);
204 		er2 &= ~HPER2_BSE;
205 	}
206 	/*
207 	 * Give up early if drive write locked.
208 	 */
209 	if (er1&HPER1_WLE) {
210 		printf("hp%d: write locked\n", unit);
211 		return (-1);
212 	}
213 	/*
214 	 * Interpret format error bit as a bad block on RP06's.
215 	 */
216 	if (MASKREG(er1) == HPER1_FER && RP06)
217 		goto badsect;
218 
219 	/*
220 	 * If a hard error, or maximum retry count
221 	 * exceeded, clear controller state and
222 	 * pass back error to caller.
223 	 */
224 	if (++io->i_errcnt > 27 || (er1 & HPER1_HARD) ||
225 	    (!ML11 && (er2 & HPER2_HARD))) {
226 		/*
227 		 * The last ditch effort to bad sector forward
228 		 * below will probably fail since mba byte ctr
229 		 * (bcr) is different for BSE and ECC errors and
230 		 * the wrong block will be revectored to if one
231 		 * has 2 contiguous bad blocks and reads the second.
232 		 * For now, we can probably just let a header CRC
233 		 * error be handled like a BSE since no data will
234 		 * have been transferred and the bcr should the same
235 		 * as it would with a BSE error.
236 		 * --ghg.
237 		 */
238 		if (er1 & HPER1_HCRC)
239 			if ((io->i_flgs&F_NBSF) == 0 && hpecc(io, BSE) == 0)
240 				goto success;
241 hard0:
242 		io->i_error = EHER;
243 		if (mba->mba_sr & (MBSR_WCKUP|MBSR_WCKLWR))
244 			io->i_error = EWCK;
245 hard:
246 		io->i_errblk = bn + ssect[unit];
247 		printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
248 			   cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
249 		printf("er1=%b er2=%b", er1, HPER1_BITS, er2, HPER2_BITS);
250 		if (hpaddr->hpmr)
251 			printf(" mr1=%o", MASKREG(hpaddr->hpmr));
252 		if (hpaddr->hpmr2)
253 			printf(" mr2=%o", MASKREG(hpaddr->hpmr2));
254 		if (hpdebug[unit] & (HPF_BSEDEBUG|HPF_ECCDEBUG))
255 			printf(" dc=%d, da=0x%x",MASKREG(hpaddr->hpdc),
256 			  MASKREG(hpaddr->hpda));
257 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
258 		printf("\n");
259 		bytecnt = -1;
260 		goto done;
261 
262 	}
263 	/*
264 	 * Attempt to forward bad sectors on
265 	 * anything but an ML11.
266 	 */
267 	if ((er2 & HPER2_BSE) && !ML11) {
268 badsect:
269 		if (!ssect[unit] && (er2&HPER2_SSE))
270 			goto skipsect;
271 		if (io->i_flgs & F_NBSF) {
272 			io->i_error = EBSE;
273 			goto hard;
274 		}
275 		if (hpecc(io, BSE) == 0)
276 			goto success;
277 		io->i_error = EBSE;
278 		goto hard;
279 	}
280 	/*
281 	 * Skip sector handling.
282 	 */
283 	if (RM80 && (er2 & HPER2_SSE)) {
284 skipsect:
285 		(void) hpecc(io, SSE);
286 		ssect[unit] = 1;
287 		goto restart;
288 	}
289 	/*
290 	 * ECC correction?
291 	 */
292 	if ((er1 & (HPER1_DCK|HPER1_ECH)) == HPER1_DCK) {
293 		if (hpecc(io, ECC) == 0)
294 			goto success;
295 		io->i_error = EECC;
296 		io->i_errblk = bn + ssect[unit];
297 		return (-1);
298 	}
299 #ifdef F_SEVRE
300 	if (io->i_flgs & F_SEVRE)
301 		goto hard;
302 #endif
303 	if (ML11 && (io->i_errcnt >= 16))
304 		goto hard0;
305 	/* fall thru to retry */
306 	hpaddr->hpcs1 = HP_DCLR|HP_GO;
307 	HPWAIT(hpaddr);
308 
309 	/*
310 	 * Every fourth retry recalibrate.
311 	 */
312 	if (((io->i_errcnt & 07) == 4) ) {
313 		hpaddr->hpcs1 = HP_RECAL|HP_GO;
314 		HPWAIT(hpaddr);
315 		hpaddr->hpdc = cn;
316 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
317 		HPWAIT(hpaddr);
318 	}
319 
320 	if (io->i_errcnt >= 16 && (io->i_flgs & F_READ)) {
321 		hpaddr->hpof = hp_offset[io->i_errcnt & 017]|HPOF_FMT22;
322 		hpaddr->hpcs1 = HP_OFFSET|HP_GO;
323 		HPWAIT(hpaddr);
324 	}
325 	if (hpdebug[unit] & (HPF_ECCDEBUG|HPF_BSEDEBUG))
326 		printf("restart: bn=%d, cc=%d, ma=0x%x hprecal=%d\n",
327 		  io->i_bn, io->i_cc, io->i_ma, hprecal);
328 	goto restart;	/* retry whole transfer  --ghg */
329 
330 success:
331 	/*
332 	 * On successful error recovery, bump
333 	 * block number to advance to next portion
334 	 * of i/o transfer.
335 	 */
336 	bn++;
337 	if ((bn-startblock) * sectsiz < bytecnt) {
338 		io->i_bn = bn;
339 		io->i_ma = membase + (io->i_bn - startblock)*sectsiz;
340 		io->i_cc = bytecnt - (io->i_bn - startblock)*sectsiz;
341 		if (hpdebug[unit] & (HPF_ECCDEBUG|HPF_BSEDEBUG))
342 			printf("restart: bn=%d, cc=%d, ma=0x%x hprecal=%d\n",
343 			  io->i_bn, io->i_cc, io->i_ma, hprecal);
344 		goto restart;
345 	}
346 done:
347 	if (io->i_errcnt >= 16) {
348 		hpaddr->hpcs1 = HP_RTC|HP_GO;
349 		while (hpaddr->hpds & HPDS_PIP)
350 			;
351 	}
352 	io->i_cc = bytecnt;		/*reset i_cc to total count xfered*/
353 	return (bytecnt);
354 }
355 
356 hpecc(io, flag)
357 	register struct iob *io;
358 	int flag;
359 {
360 	register unit = io->i_unit;
361 	register struct mba_regs *mbp = mbamba(unit);
362 	register struct hpdevice *rp = (struct hpdevice *)mbadrv(unit);
363 	register struct st *st = &hpst[hp_type[unit]];
364 	int npf, bn, cn, tn, sn, bcr;
365 
366 	bcr = MASKREG(mbp->mba_bcr);
367 	if (bcr)
368 		bcr |= 0xffff0000;		/* sxt */
369 	npf = (bcr + io->i_cc) / sectsiz;	/* # sectors read */
370 	if (flag == ECC)
371 		npf--;		/* Error is in prev block --ghg */
372 	bn = io->i_bn + npf + ssect[unit];	/* physical block #*/
373 	if (hpdebug[unit]&HPF_ECCDEBUG)
374 		printf("bcr=%d npf=%d ssect=%d sectsiz=%d i_cc=%d\n",
375 			bcr, npf, ssect[unit], sectsiz, io->i_cc);
376 	/*
377 	 * ECC correction logic.
378 	 */
379 	if (flag == ECC) {
380 		register int i;
381 		caddr_t addr;
382 		int bit, o, mask, ecccnt = 0;
383 
384 		printf("hp%d: soft ecc sn%d\n", unit, bn);
385 		mask = MASKREG(rp->hpec2);
386 		i = MASKREG(rp->hpec1) - 1;	/* -1 makes 0 origin */
387 		bit = i&07;
388 		o = (i & ~07) >> 3;
389 		rp->hpcs1 = HP_DCLR | HP_GO;
390 		while (o <sectsiz && npf*sectsiz + o < io->i_cc && bit > -11) {
391 			addr = io->i_ma + (npf*sectsiz) + o;
392 			/*
393 			 * No data transfer occurs with a write check,
394 			 * so don't correct the resident copy of data.
395 			 */
396 			if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) {
397 				if (hpdebug[unit] & HPF_ECCDEBUG)
398 					printf("addr=%x old=%x ", addr,
399 						(*addr & 0xff));
400 				*addr ^= (mask << bit);
401 				if (hpdebug[unit] & HPF_ECCDEBUG)
402 					printf("new=%x\n",(*addr & 0xff));
403 			}
404 			o++, bit -= 8;
405 			if ((io->i_flgs & F_ECCLM) && ecccnt++ >= MAXECC)
406 				return (1);
407 		}
408 #ifdef F_SEVRE
409 		if (io->i_flgs & F_SEVRE)
410 			return(1);
411 #endif
412 		return (0);
413 	}
414 
415 	/*
416 	 * Skip sector error.
417 	 * Set skip-sector-inhibit and
418 	 * read next sector
419 	 */
420 	if (flag == SSE) {
421 		rp->hpcs1 = HP_DCLR | HP_GO;
422 		HPWAIT(rp);
423 		rp->hpof |= HPOF_SSEI;
424 		return (0);
425 	}
426 
427 	/*
428 	 * Bad block forwarding.
429 	 */
430 	 if (flag == BSE) {
431 		int bbn;
432 
433 		rp->hpcs1 = HP_DCLR | HP_GO;
434 		if (hpdebug[unit] & HPF_BSEDEBUG)
435 			printf("hpecc: BSE @ bn %d\n", bn);
436 		cn = bn/st->nspc;
437 		sn = bn%st->nspc;
438 		tn = sn/st->nsect;
439 		sn = sn%st->nsect;
440 		bcr += sectsiz;
441 		if ((bbn = isbad(&hpbad[unit], cn, tn, sn)) < 0)
442 			return (1);
443 		bbn = st->ncyl*st->nspc - st->nsect - 1 - bbn;
444 		cn = bbn/st->nspc;
445 		sn = bbn%st->nspc;
446 		tn = sn/st->nsect;
447 		sn = sn%st->nsect;
448 		io->i_cc = sectsiz;
449 		io->i_ma += npf*sectsiz;
450 		if (hpdebug[unit] & HPF_BSEDEBUG)
451 			printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
452 		rp->hpof &= ~HPOF_SSEI;
453 		mbp->mba_sr = -1;
454 		rp->hpdc = cn;
455 		rp->hpda = (tn<<8) + sn;
456 		mbastart(io,io->i_flgs);
457 		io->i_errcnt = 0;
458 		HPWAIT(rp);
459 		return (rp->hpds&HPDS_ERR);
460 	}
461 	printf("hpecc: flag=%d\n", flag);
462 	return (1);
463 }
464 
465 /*ARGSUSED*/
466 hpioctl(io, cmd, arg)
467 	struct iob *io;
468 	int cmd;
469 	caddr_t arg;
470 {
471 	register unit = io->i_unit;
472 	struct st *st = &hpst[hp_type[unit]], *tmp;
473 	struct mba_drv *drv = mbadrv(unit);
474 	int flag;
475 
476 	switch(cmd) {
477 
478 	case SAIODEBUG:
479 		flag = (int)arg;
480 		if (flag > 0)
481 			hpdebug[unit] |= flag;
482 		else
483 			hpdebug[unit] &= ~flag;
484 		return (0);
485 
486 	case SAIODEVDATA:
487 		if ((drv->mbd_dt&MBDT_TAP) == 0) {
488 			tmp = (struct st *)arg;
489 			*tmp = *st;
490 			return (0);
491 		}
492 		return (ECMD);
493 
494 	case SAIOSSI:			/* skip-sector-inhibit */
495 		if (drv->mbd_dt&MBDT_TAP)
496 			return (ECMD);
497 		if ((io->i_flgs&F_SSI) == 0) {
498 			/* make sure this is done once only */
499 			io->i_flgs |= F_SSI;
500 			st->nsect++;
501 			st->nspc += st->ntrak;
502 		}
503 		return (0);
504 
505 	case SAIONOSSI:			/* remove skip-sector-inhibit */
506 		if (io->i_flgs & F_SSI) {
507 			io->i_flgs &= ~F_SSI;
508 			drv->mbd_of &= ~HPOF_SSEI;
509 			st->nsect--;
510 			st->nspc -= st->ntrak;
511 		}
512 		return(0);
513 
514 	case SAIOSSDEV:			/* drive have skip sector? */
515 		return (RM80 ? 0 : ECMD);
516 	}
517 	return (ECMD);
518 }
519