xref: /csrg-svn/sys/vax/stand/hp.c (revision 15052)
1 /*	hp.c	6.2	83/09/23	*/
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, 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 	if (io->i_boff < 0 || io->i_boff > 7 ||
122 	    st->off[io->i_boff]== -1)
123 		_stop("hp bad minor");
124 	io->i_boff = st->off[io->i_boff] * st->nspc;
125 }
126 
127 hpstrategy(io, func)
128 	register struct iob *io;
129 {
130 	register unit = io->i_unit;
131 	struct mba_regs *mba = mbamba(unit);
132 	daddr_t bn, startblock;
133 	struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
134 	struct st *st = &hpst[hp_type[unit]];
135 	int cn, tn, sn, bytecnt, bytesleft;
136 	char *membase;
137 	int er1, er2, hprecal;
138 
139 	sectsiz = SECTSIZ;
140 	if ((io->i_flgs & (F_HDR|F_HCHECK)) != 0)
141 		sectsiz += HDRSIZ;
142 	if ((hpaddr->hpds & HPDS_VV) == 0) {
143 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
144 		hpaddr->hpcs1 = HP_PRESET|HP_GO;
145 		if (!ML11)
146 			hpaddr->hpof = HPOF_FMT22;
147 	}
148 	io->i_errcnt = 0;
149 	ssect[unit] = 0;
150 	bytecnt = io->i_cc;
151 	membase = io->i_ma;
152 	startblock = io->i_bn;
153 	hprecal = 0;
154 
155 restart:
156 	bn = io->i_bn;
157 	cn = bn/st->nspc;
158 	sn = bn%st->nspc;
159 	tn = sn/st->nsect;
160 	sn = sn%st->nsect + ssect[unit];
161 
162 	HPWAIT(hpaddr);
163 	mba->mba_sr = -1;
164 	if (ML11)
165 		hpaddr->hpda = bn;
166 	else {
167 		hpaddr->hpdc = cn;
168 		hpaddr->hpda = (tn << 8) + sn;
169 	}
170 	if (mbastart(io, func) != 0)		/* start transfer */
171 		return (-1);
172 	HPWAIT(hpaddr);
173 	/*
174 	 * Successful data transfer, return.
175 	 */
176 	if ((hpaddr->hpds&HPDS_ERR) == 0 && (mba->mba_sr&MBSR_EBITS) == 0) {
177 		if (io->i_errcnt >= 16) {
178 			hpaddr->hpcs1 = HP_RTC|HP_GO;
179 			while (hpaddr->hpds & HPDS_PIP)
180 				;
181 		}
182 		return (bytecnt);
183 	}
184 
185 	/*
186 	 * Error handling.  Calculate location of error.
187 	 */
188 	bytesleft = MASKREG(mba->mba_bcr);
189 	if (bytesleft)
190 		bytesleft |= 0xffff0000;	/* sxt */
191 	bn = io->i_bn + (io->i_cc + bytesleft) / sectsiz;
192 	er1 = MASKREG(hpaddr->hper1);
193 	er2 = MASKREG(hpaddr->hper2);
194 	if (er1 & (HPER1_DCK|HPER1_ECH))
195 		bn--;	/* Error is in Prev block */
196 	cn = bn/st->nspc;
197 	sn = bn%st->nspc;
198 	tn = sn/st->nsect;
199 	sn = sn%st->nsect;
200 	if (hpdebug[unit] & (HPF_ECCDEBUG|HPF_BSEDEBUG)) {
201 		printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b\n",
202 			cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
203 		printf("er1=%b er2=%b\n", er1, HPER1_BITS, er2, HPER2_BITS);
204 		printf("bytes left: %d, of 0x%x, da 0x%x\n",-bytesleft,
205 			hpaddr->hpof, hpaddr->hpda);
206 	}
207 	if (er1 & HPER1_HCRC) {
208 		er1 &= ~(HPER1_HCE|HPER1_FER);
209 		er2 &= ~HPER2_BSE;
210 	}
211 	/*
212 	 * Give up early if drive write locked.
213 	 */
214 	if (er1&HPER1_WLE) {
215 		printf("hp%d: write locked\n", unit);
216 		return (-1);
217 	}
218 	/*
219 	 * Interpret format error bit as a bad block on RP06's.
220 	 */
221 	if (MASKREG(er1) == HPER1_FER && RP06)
222 		goto badsect;
223 
224 	/*
225 	 * If a hard error, or maximum retry count
226 	 * exceeded, clear controller state and
227 	 * pass back error to caller.
228 	 */
229 	if (++io->i_errcnt > 27 || (er1 & HPER1_HARD) ||
230 	    (!ML11 && (er2 & HPER2_HARD))) {
231 		/*
232 		 * The last ditch effort to bad sector forward
233 		 * below will probably fail since mba byte ctr
234 		 * (bcr) is different for BSE and ECC errors and
235 		 * the wrong block will be revectored to if one
236 		 * has 2 contiguous bad blocks and reads the second.
237 		 * For now, we can probably just let a header CRC
238 		 * error be handled like a BSE since no data will
239 		 * have been transferred and the bcr should the same
240 		 * as it would with a BSE error.
241 		 * --ghg.
242 		 */
243 		if (er1 & HPER1_HCRC)
244 			if ((io->i_flgs&F_NBSF) == 0 && hpecc(io, BSE) == 0)
245 				goto success;
246 hard0:
247 		io->i_error = EHER;
248 		if (mba->mba_sr & (MBSR_WCKUP|MBSR_WCKLWR))
249 			io->i_error = EWCK;
250 hard:
251 		io->i_errblk = bn + ssect[unit];
252 		printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
253 			   cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
254 		printf("er1=%b er2=%b", er1, HPER1_BITS, er2, HPER2_BITS);
255 		if (hpaddr->hpmr)
256 			printf(" mr1=%o", MASKREG(hpaddr->hpmr));
257 		if (hpaddr->hpmr2)
258 			printf(" mr2=%o", MASKREG(hpaddr->hpmr2));
259 		if (hpdebug[unit] & (HPF_BSEDEBUG|HPF_ECCDEBUG))
260 			printf(" dc=%d, da=0x%x",MASKREG(hpaddr->hpdc),
261 			  MASKREG(hpaddr->hpda));
262 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
263 		printf("\n");
264 		return (-1);
265 
266 	}
267 	/*
268 	 * Attempt to forward bad sectors on
269 	 * anything but an ML11.  If drive
270 	 * supports skip sector handling, try to
271 	 * use it first; otherwise try the
272 	 * bad sector table.
273 	 */
274 	if ((er2 & HPER2_BSE) && !ML11) {
275 badsect:
276 		if (!ssect[unit] && (er2&HPER2_SSE))
277 			goto skipsect;
278 		if (io->i_flgs & F_NBSF) {
279 			io->i_error = EBSE;
280 			goto hard;
281 		}
282 		if (hpecc(io, BSE) == 0)
283 			goto success;
284 		io->i_error = EBSE;
285 		goto hard;
286 	}
287 
288 	/*
289 	 * Skip sector handling.
290 	 */
291 	if (RM80 && (er2 & HPER2_SSE)) {
292 skipsect:
293 		(void) hpecc(io, SSE);
294 		ssect[unit] = 1;
295 		goto success;
296 	}
297 	/*
298 	 * ECC correction?
299 	 */
300 	if ((er1 & (HPER1_DCK|HPER1_ECH)) == HPER1_DCK) {
301 		if (hpecc(io, ECC) == 0)
302 			goto success;
303 		io->i_error = EECC;
304 		io->i_errblk = bn + ssect[unit];
305 		return (-1);
306 	}
307 #ifdef F_SEVRE
308 	if (io->i_flgs & F_SEVRE)
309 		goto hard;
310 #endif
311 	if (ML11 && (io->i_errcnt >= 16))
312 		goto hard0;
313 	/* fall thru to retry */
314 	hpaddr->hpcs1 = HP_DCLR|HP_GO;
315 	HPWAIT(hpaddr);
316 
317 	/*
318 	 * Every fourth retry recalibrate.
319 	 */
320 	if (((io->i_errcnt & 07) == 4) ) {
321 		hpaddr->hpcs1 = HP_RECAL|HP_GO;
322 		HPWAIT(hpaddr);
323 		hpaddr->hpdc = cn;
324 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
325 		HPWAIT(hpaddr);
326 	}
327 
328 	if (io->i_errcnt >= 16 && (io->i_flgs & F_READ)) {
329 		hpaddr->hpof = hp_offset[io->i_errcnt & 017]|HPOF_FMT22;
330 		hpaddr->hpcs1 = HP_OFFSET|HP_GO;
331 		HPWAIT(hpaddr);
332 	}
333 	if (hpdebug[unit] & (HPF_ECCDEBUG|HPF_BSEDEBUG))
334 		printf("restart: bn=%d, cc=%d, ma=0x%x hprecal=%d\n",
335 		  io->i_bn, io->i_cc, io->i_ma, hprecal);
336 	goto restart;	/* retry whole transfer  --ghg */
337 
338 success:
339 	/*
340 	 * On successful error recovery, bump
341 	 * block number to advance to next portion
342 	 * of i/o transfer.
343 	 */
344 	bn++;
345 	if ((bn-startblock) * sectsiz < bytecnt) {
346 		io->i_bn = bn;
347 		io->i_ma = membase + (io->i_bn - startblock)*sectsiz;
348 		io->i_cc = bytecnt - (io->i_bn - startblock)*sectsiz;
349 		if (hpdebug[unit] & (HPF_ECCDEBUG|HPF_BSEDEBUG))
350 			printf("restart: bn=%d, cc=%d, ma=0x%x hprecal=%d\n",
351 			  io->i_bn, io->i_cc, io->i_ma, hprecal);
352 		goto restart;
353 	}
354 	if (io->i_errcnt >= 16) {
355 		hpaddr->hpcs1 = HP_RTC|HP_GO;
356 		while (hpaddr->hpds & HPDS_PIP)
357 			;
358 	}
359 	return (bytecnt);
360 }
361 
362 hpecc(io, flag)
363 	register struct iob *io;
364 	int flag;
365 {
366 	register unit = io->i_unit;
367 	register struct mba_regs *mbp = mbamba(unit);
368 	register struct hpdevice *rp = (struct hpdevice *)mbadrv(unit);
369 	register struct st *st = &hpst[hp_type[unit]];
370 	int npf, bn, cn, tn, sn, bcr;
371 
372 	bcr = MASKREG(mbp->mba_bcr);
373 	if (bcr)
374 		bcr |= 0xffff0000;		/* sxt */
375 	npf = (bcr + io->i_cc) / sectsiz;	/* # sectors read */
376 	if (flag == ECC)
377 		npf--;		/* Error is in prev block --ghg */
378 	bn = io->i_bn + npf + ssect[unit];	/* physical block #*/
379 	if (hpdebug[unit]&HPF_ECCDEBUG)
380 		printf("bcr=%d npf=%d ssect=%d sectsiz=%d i_cc=%d\n",
381 			bcr, npf, ssect[unit], sectsiz, io->i_cc);
382 	/*
383 	 * ECC correction logic.
384 	 */
385 	if (flag == ECC) {
386 		register int i;
387 		caddr_t addr;
388 		int bit, o, mask, ecccnt = 0;
389 
390 		printf("hp%d: soft ecc sn%d\n", unit, bn);
391 		mask = MASKREG(rp->hpec2);
392 		i = MASKREG(rp->hpec1) - 1;	/* -1 makes 0 origin */
393 		bit = i&07;
394 		o = (i & ~07) >> 3;
395 		rp->hpcs1 = HP_DCLR | HP_GO;
396 		while (o <sectsiz && npf*sectsiz + o < io->i_cc && bit > -11) {
397 			addr = io->i_ma + (npf*sectsiz) + o;
398 			/*
399 			 * No data transfer occurs with a write check,
400 			 * so don't correct the resident copy of data.
401 			 */
402 			if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) {
403 				if (hpdebug[unit] & HPF_ECCDEBUG)
404 					printf("addr=%x old=%x ", addr,
405 						(*addr & 0xff));
406 				*addr ^= (mask << bit);
407 				if (hpdebug[unit] & HPF_ECCDEBUG)
408 					printf("new=%x\n",(*addr & 0xff));
409 			}
410 			o++, bit -= 8;
411 			if ((io->i_flgs & F_ECCLM) && ecccnt++ >= MAXECC)
412 				return (1);
413 		}
414 #ifdef F_SEVRE
415 		if (io->i_flgs & F_SEVRE)
416 			return(1);
417 #endif
418 		return (0);
419 	}
420 
421 	/*
422 	 * Skip sector error.
423 	 * Set skip-sector-inhibit and
424 	 * read next sector
425 	 */
426 	if (flag == SSE) {
427 		rp->hpcs1 = HP_DCLR | HP_GO;
428 		HPWAIT(rp);
429 		rp->hpof |= HPOF_SSEI;
430 		return (0);
431 	}
432 
433 	/*
434 	 * Bad block forwarding.
435 	 */
436 	 if (flag == BSE) {
437 		int bbn;
438 
439 		rp->hpcs1 = HP_DCLR | HP_GO;
440 		if (hpdebug[unit] & HPF_BSEDEBUG)
441 			printf("hpecc: BSE @ bn %d\n", bn);
442 		cn = bn/st->nspc;
443 		sn = bn%st->nspc;
444 		tn = sn/st->nsect;
445 		sn = sn%st->nsect;
446 		bcr += sectsiz;
447 		if ((bbn = isbad(&hpbad[unit], cn, tn, sn)) < 0)
448 			return (1);
449 		bbn = st->ncyl*st->nspc - st->nsect - 1 - bbn;
450 		cn = bbn/st->nspc;
451 		sn = bbn%st->nspc;
452 		tn = sn/st->nsect;
453 		sn = sn%st->nsect;
454 		io->i_cc = sectsiz;
455 		io->i_ma += npf*sectsiz;
456 		if (hpdebug[unit] & HPF_BSEDEBUG)
457 			printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
458 		rp->hpof &= ~HPOF_SSEI;
459 		mbp->mba_sr = -1;
460 		rp->hpdc = cn;
461 		rp->hpda = (tn<<8) + sn;
462 		mbastart(io,io->i_flgs);
463 		io->i_errcnt = 0;
464 		HPWAIT(rp);
465 		return (rp->hpds&HPDS_ERR);
466 	}
467 	printf("hpecc: flag=%d\n", flag);
468 	return (1);
469 }
470 
471 /*ARGSUSED*/
472 hpioctl(io, cmd, arg)
473 	struct iob *io;
474 	int cmd;
475 	caddr_t arg;
476 {
477 	register unit = io->i_unit;
478 	struct st *st = &hpst[hp_type[unit]], *tmp;
479 	struct mba_drv *drv = mbadrv(unit);
480 	int flag;
481 
482 	switch(cmd) {
483 
484 	case SAIODEBUG:
485 		flag = (int)arg;
486 		if (flag > 0)
487 			hpdebug[unit] |= flag;
488 		else
489 			hpdebug[unit] &= ~flag;
490 		return (0);
491 
492 	case SAIODEVDATA:
493 		if ((drv->mbd_dt&MBDT_TAP) == 0) {
494 			tmp = (struct st *)arg;
495 			*tmp = *st;
496 			return (0);
497 		}
498 		return (ECMD);
499 
500 	case SAIOSSI:			/* skip-sector-inhibit */
501 		if (drv->mbd_dt&MBDT_TAP)
502 			return (ECMD);
503 		if ((io->i_flgs&F_SSI) == 0) {
504 			/* make sure this is done once only */
505 			io->i_flgs |= F_SSI;
506 			st->nsect++;
507 			st->nspc += st->ntrak;
508 		}
509 		return (0);
510 
511 	case SAIONOSSI:			/* remove skip-sector-inhibit */
512 		if (io->i_flgs & F_SSI) {
513 			io->i_flgs &= ~F_SSI;
514 			drv->mbd_of &= ~HPOF_SSEI;
515 			st->nsect--;
516 			st->nspc -= st->ntrak;
517 		}
518 		return(0);
519 
520 	case SAIOSSDEV:			/* drive have skip sector? */
521 		return (RM80 ? 0 : ECMD);
522 	}
523 	return (ECMD);
524 }
525