xref: /original-bsd/sys/vax/stand/hp.c (revision 92d3de31)
1 /*	hp.c	4.17	83/03/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 #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 		return (bytecnt);
178 
179 	/*
180 	 * Error handling.  Calculate location of error.
181 	 */
182 	bytesleft = MASKREG(mba->mba_bcr);
183 	if (bytesleft)
184 		bytesleft |= 0xffff0000;	/* sxt */
185 	bn = io->i_bn + (io->i_cc + bytesleft) / sectsiz;
186 	cn = bn/st->nspc;
187 	sn = bn%st->nspc;
188 	tn = sn/st->nsect;
189 	sn = sn%st->nsect;
190 	er1 = MASKREG(hpaddr->hper1);
191 	er2 = MASKREG(hpaddr->hper2);
192 	if (hpdebug[unit] & (HPF_ECCDEBUG|HPF_BSEDEBUG)) {
193 		printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b\n",
194 			cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
195 		printf("er1=%b er2=%b\n", er1, HPER1_BITS, er2, HPER2_BITS);
196 		printf("bytes left: %d, of 0x%x, da 0x%x\n",-bytesleft,
197 			hpaddr->hpof, hpaddr->hpda);
198 	}
199 	if (er1 & HPER1_HCRC) {
200 		er1 &= ~(HPER1_HCE|HPER1_FER);
201 		er2 &= ~HPER2_BSE;
202 	}
203 	/*
204 	 * Give up early if drive write locked.
205 	 */
206 	if (er1&HPER1_WLE) {
207 		printf("hp%d: write locked\n", unit);
208 		return (-1);
209 	}
210 	/*
211 	 * Interpret format error bit as a bad block on RP06's.
212 	 */
213 	if (MASKREG(er1) == HPER1_FER && RP06)
214 		goto badsect;
215 
216 	/*
217 	 * If a hard error, or maximum retry count
218 	 * exceeded, clear controller state and
219 	 * pass back error to caller.
220 	 */
221 	if (++io->i_errcnt > 27 || (er1 & HPER1_HARD) ||
222 	    (!ML11 && (er2 & HPER2_HARD))) {
223 		if ((io->i_flgs&F_NBSF) == 0 && hpecc(io, BSE) == 0)
224 			goto success;
225 hard0:
226 		io->i_error = EHER;
227 		if (mba->mba_sr & (MBSR_WCKUP|MBSR_WCKLWR))
228 			io->i_error = EWCK;
229 hard:
230 		io->i_errblk = bn + ssect[unit];
231 		printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
232 			   cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
233 		printf("er1=%b er2=%b", er1, HPER1_BITS, er2, HPER2_BITS);
234 		if (hpaddr->hpmr)
235 			printf(" mr1=%o", MASKREG(hpaddr->hpmr));
236 		if (hpaddr->hpmr2)
237 			printf(" mr2=%o", MASKREG(hpaddr->hpmr2));
238 		if (hpdebug[unit] & (HPF_BSEDEBUG|HPF_ECCDEBUG))
239 			printf(" dc=%d, da=0x%x",MASKREG(hpaddr->hpdc),
240 			  MASKREG(hpaddr->hpda));
241 		hpaddr->hpcs1 = HP_DCLR|HP_GO;
242 		printf("\n");
243 		return (-1);
244 
245 	}
246 	/*
247 	 * Attempt to forward bad sectors on
248 	 * anything but an ML11.  If drive
249 	 * supports skip sector handling, try to
250 	 * use it first; otherwise try the
251 	 * bad sector table.
252 	 */
253 	if ((er2 & HPER2_BSE) && !ML11) {
254 badsect:
255 		if (!ssect[unit] && (er2&HPER2_SSE))
256 			goto skipsect;
257 		if (io->i_flgs & F_NBSF) {
258 			io->i_error = EBSE;
259 			goto hard;
260 		}
261 		if (hpecc(io, BSE) == 0)
262 			goto success;
263 		io->i_error = EBSE;
264 		goto hard;
265 	}
266 
267 	/*
268 	 * Skip sector handling.
269 	 */
270 	if (RM80 && (er2 & HPER2_SSE)) {
271 skipsect:
272 		(void) hpecc(io, SSE);
273 		ssect[unit] = 1;
274 		goto success;
275 	}
276 	/*
277 	 * ECC correction?
278 	 */
279 	if ((er1 & (HPER1_DCK|HPER1_ECH)) == HPER1_DCK) {
280 		if (hpecc(io, ECC) == 0)
281 			goto success;
282 		io->i_error = EECC;
283 		return (-1);
284 	}
285 	if (ML11 && (io->i_errcnt >= 16))
286 		goto hard0;
287 	/* fall thru to retry */
288 	hpaddr->hpcs1 = HP_DCLR|HP_GO;
289 	HPWAIT(hpaddr);
290 
291 	/*
292 	 * Every fourth retry recalibrate.
293 	 */
294 	if (((io->i_errcnt & 07) == 4) ) {
295 		hpaddr->hpcs1 = HP_RECAL|HP_GO;
296 		hprecal = 1;
297 		goto again;
298 	}
299 
300 	/*
301 	 * Recalibration state machine.
302 	 */
303 	switch (hprecal) {
304 
305 	case 1:
306 		hpaddr->hpdc = cn;
307 		hpaddr->hpcs1 = HP_SEEK|HP_GO;
308 		hprecal = 2;
309 		goto again;
310 
311 	case 2:
312 		if (io->i_errcnt < 16 || (io->i_flgs & F_READ) == 0)
313 			goto donerecal;
314 		hpaddr->hpof = hp_offset[io->i_errcnt & 017]|HPOF_FMT22;
315 		hpaddr->hpcs1 = HP_OFFSET|HP_GO;
316 		hprecal = 3;
317 		goto again;
318 
319 	case 3:
320 	donerecal:
321 		hprecal = 0;
322 		goto again;
323 	}
324 	if (io->i_errcnt >= 16) {
325 		hpaddr->hpcs1 = HP_RTC|HP_GO;
326 		while (hpaddr->hpds & HPDS_PIP)
327 			;
328 	}
329 	goto again;
330 
331 success:
332 	/*
333 	 * On successful error recovery, bump
334 	 * block number to advance to next portion
335 	 * of i/o transfer.
336 	 */
337 	bn++;
338 	if ((bn-startblock) * sectsiz < bytecnt) {
339 again:
340 		io->i_bn = bn;
341 		io->i_ma = membase + (io->i_bn - startblock)*sectsiz;
342 		io->i_cc = bytecnt - (io->i_bn - startblock)*sectsiz;
343 		if (hpdebug[unit] & (HPF_ECCDEBUG|HPF_BSEDEBUG))
344 			printf("restart: bn=%d, cc=%d, ma=0x%x hprecal=%d\n",
345 			  io->i_bn, io->i_cc, io->i_ma, hprecal);
346 		goto restart;
347 	}
348 	return (bytecnt);
349 }
350 
351 hpecc(io, flag)
352 	register struct iob *io;
353 	int flag;
354 {
355 	register unit = io->i_unit;
356 	register struct mba_regs *mbp = mbamba(unit);
357 	register struct hpdevice *rp = (struct hpdevice *)mbadrv(unit);
358 	register struct st *st = &hpst[hp_type[unit]];
359 	int npf, bn, cn, tn, sn, bcr;
360 
361 	bcr = MASKREG(mbp->mba_bcr);
362 	if (bcr)
363 		bcr |= 0xffff0000;		/* sxt */
364 	npf = (bcr + io->i_cc) / sectsiz;	/* # sectors read */
365 	bn = io->i_bn + npf + ssect[unit];	/* physical block #*/
366 	if (hpdebug[unit]&HPF_ECCDEBUG)
367 		printf("bcr=%d npf=%d ssect=%d sectsiz=%d i_cc=%d\n",
368 			bcr, npf, ssect[unit], sectsiz, io->i_cc);
369 	/*
370 	 * ECC correction logic.
371 	 */
372 	if (flag == ECC) {
373 		register int i;
374 		caddr_t addr;
375 		int bit, o, mask, ecccnt = 0;
376 
377 		printf("hp%d: soft ecc sn%d\n", unit, bn);
378 		mask = MASKREG(rp->hpec2);
379 		i = MASKREG(rp->hpec1) - 1;	/* -1 makes 0 origin */
380 		bit = i&07;
381 		o = (i & ~07) >> 3;
382 		rp->hpcs1 = HP_DCLR | HP_GO;
383 		while (o <sectsiz && npf*sectsiz + o < io->i_cc && bit > -11) {
384 			addr = io->i_ma + (npf*sectsiz) + o;
385 			/*
386 			 * No data transfer occurs with a write check,
387 			 * so don't correct the resident copy of data.
388 			 */
389 			if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) {
390 				if (hpdebug[unit] & HPF_ECCDEBUG)
391 					printf("addr=%x old=%x ", addr,
392 						(*addr & 0xff));
393 				*addr ^= (mask << bit);
394 				if (hpdebug[unit] & HPF_ECCDEBUG)
395 					printf("new=%x\n",(*addr & 0xff));
396 			}
397 			o++, bit -= 8;
398 			if ((io->i_flgs & F_ECCLM) && ecccnt++ >= MAXECC)
399 				return (1);
400 		}
401 		return (0);
402 	}
403 
404 	/*
405 	 * Skip sector error.
406 	 * Set skip-sector-inhibit and
407 	 * read next sector
408 	 */
409 	if (flag == SSE) {
410 		rp->hpcs1 = HP_DCLR | HP_GO;
411 		HPWAIT(rp);
412 		rp->hpof |= HPOF_SSEI;
413 		return (0);
414 	}
415 
416 	/*
417 	 * Bad block forwarding.
418 	 */
419 	 if (flag == BSE) {
420 		int bbn;
421 
422 		rp->hpcs1 = HP_DCLR | HP_GO;
423 		if (hpdebug[unit] & HPF_BSEDEBUG)
424 			printf("hpecc: BSE @ bn %d\n", bn);
425 		cn = bn/st->nspc;
426 		sn = bn%st->nspc;
427 		tn = sn/st->nsect;
428 		sn = sn%st->nsect;
429 		bcr += sectsiz;
430 		if ((bbn = isbad(&hpbad[unit], cn, tn, sn)) < 0)
431 			return (1);
432 		bbn = st->ncyl*st->nspc - st->nsect - 1 - bbn;
433 		cn = bbn/st->nspc;
434 		sn = bbn%st->nspc;
435 		tn = sn/st->nsect;
436 		sn = sn%st->nsect;
437 		io->i_cc = sectsiz;
438 		io->i_ma += npf*sectsiz;
439 		if (hpdebug[unit] & HPF_BSEDEBUG)
440 			printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
441 		rp->hpof &= ~HPOF_SSEI;
442 		mbp->mba_sr = -1;
443 		rp->hpdc = cn;
444 		rp->hpda = (tn<<8) + sn;
445 		mbastart(io,io->i_flgs);
446 		io->i_errcnt = 0;
447 		HPWAIT(rp);
448 		return (rp->hpds&HPDS_ERR);
449 	}
450 	printf("hpecc: flag=%d\n", flag);
451 	return (1);
452 }
453 
454 /*ARGSUSED*/
455 hpioctl(io, cmd, arg)
456 	struct iob *io;
457 	int cmd;
458 	caddr_t arg;
459 {
460 	register unit = io->i_unit;
461 	struct st *st = &hpst[hp_type[unit]], *tmp;
462 	struct mba_drv *drv = mbadrv(unit);
463 	int flag;
464 
465 	switch(cmd) {
466 
467 	case SAIODEBUG:
468 		flag = (int)arg;
469 		if (flag > 0)
470 			hpdebug[unit] |= flag;
471 		else
472 			hpdebug[unit] &= ~flag;
473 		return (0);
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 		return (ECMD);
482 
483 	case SAIOSSI:			/* skip-sector-inhibit */
484 		if (drv->mbd_dt&MBDT_TAP)
485 			return (ECMD);
486 		if ((io->i_flgs&F_SSI) == 0) {
487 			/* make sure this is done once only */
488 			io->i_flgs |= F_SSI;
489 			st->nsect++;
490 			st->nspc += st->ntrak;
491 		}
492 		return (0);
493 
494 	case SAIONOSSI:			/* remove skip-sector-inhibit */
495 		if (io->i_flgs & F_SSI) {
496 			io->i_flgs &= ~F_SSI;
497 			drv->mbd_of &= ~HPOF_SSEI;
498 			st->nsect--;
499 			st->nspc -= st->ntrak;
500 		}
501 		return(0);
502 
503 	case SAIOSSDEV:			/* drive have skip sector? */
504 		return (RM80 ? 0 : ECMD);
505 	}
506 	return (ECMD);
507 }
508