xref: /csrg-svn/sys/vax/stand/up.c (revision 33533)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)up.c	7.4 (Berkeley) 02/22/88
7  */
8 
9 /*
10  * UNIBUS peripheral standalone driver
11  * with ECC correction and bad block forwarding.
12  * Also supports header operation and write
13  * check for data and/or header.
14  */
15 #include "param.h"
16 #include "inode.h"
17 #include "fs.h"
18 #include "dkbad.h"
19 #include "vmmac.h"
20 
21 #include "../vax/pte.h"
22 
23 #include "../vaxuba/upreg.h"
24 #include "../vaxuba/ubareg.h"
25 
26 #include "saio.h"
27 #include "savax.h"
28 
29 #define RETRIES		27
30 
31 #define MAXBADDESC	126	/* max number of bad sectors recorded */
32 #define SECTSIZ		512	/* sector size in bytes */
33 #define HDRSIZ		4	/* number of bytes in sector header */
34 
35 #define	MAXPART		8
36 #define	MAXCTLR		1	/* all addresses must be specified */
37 u_short	ubastd[MAXCTLR] = { 0776700 };
38 
39 extern	struct st upst[];
40 
41 #ifndef SMALL
42 struct  dkbad upbad[MAXNUBA][MAXCTLR][8];	/* bad sector table */
43 #endif
44 int 	sectsiz;			/* real sector size */
45 
46 struct	up_softc {
47 	char	gottype;
48 	char	type;
49 	char	debug;
50 #	define	UPF_BSEDEBUG	01	/* debugging bad sector forwarding */
51 #	define	UPF_ECCDEBUG	02	/* debugging ecc correction */
52 	int	retries;
53 	int	ecclim;
54 } up_softc[MAXNUBA][MAXCTLR][8];
55 
56 u_char	up_offset[16] = {
57 	UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400,
58 	UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800,
59 	UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200,
60 	0, 0, 0, 0
61 };
62 
63 upopen(io)
64 	register struct iob *io;
65 {
66 	register unit = io->i_unit;
67 	register struct updevice *upaddr;
68 	register struct up_softc *sc;
69 	register struct st *st;
70 
71 	if ((u_int)io->i_ctlr >= MAXCTLR)
72 		return (ECTLR);
73 	if ((u_int)io->i_part >= MAXPART)
74 		return (EPART);
75 	upaddr = (struct updevice *)ubamem(io->i_adapt, ubastd[io->i_ctlr]);
76 	upaddr->upcs2 = unit % 8;
77 	while ((upaddr->upcs1 & UP_DVA) == 0);
78 	sc = &up_softc[io->i_adapt][io->i_ctlr][unit];
79 	if (sc->gottype == 0) {
80 		register int i;
81 		struct iob tio;
82 
83 		sc->retries = RETRIES;
84 		sc->ecclim = 11;
85 		sc->debug = 0;
86 		sc->type = upmaptype(unit, upaddr);
87 		if (sc->type < 0) {
88 			printf("unknown drive type\n");
89 			return (ENXIO);
90 		}
91 		st = &upst[sc->type];
92 		if (st->off[io->i_part] == -1)
93 			return (EPART);
94 #ifndef SMALL
95 		/*
96 		 * Read in the bad sector table.
97 		 */
98 		tio = *io;
99 		tio.i_bn = st->nspc * st->ncyl - st->nsect;
100 		tio.i_ma = (char *)&upbad[tio.i_adapt][tio.i_ctlr][tio.i_unit];
101 		tio.i_cc = sizeof (struct dkbad);
102 		tio.i_flgs |= F_RDDATA;
103 		for (i = 0; i < 5; i++) {
104 			if (upstrategy(&tio, READ) == sizeof (struct dkbad))
105 				break;
106 			tio.i_bn += 2;
107 		}
108 		if (i == 5) {
109 			printf("Unable to read bad sector table\n");
110 			for (i = 0; i < MAXBADDESC; i++) {
111 				upbad[tio.i_adapt][tio.i_ctlr][unit].bt_bad[i].bt_cyl = -1;
112 				upbad[tio.i_adapt][tio.i_ctlr][unit].bt_bad[i].bt_trksec = -1;
113 			}
114 		}
115 #endif
116 		sc->gottype = 1;
117 	}
118 	st = &upst[sc->type];
119 	io->i_boff = st->off[io->i_part] * st->nspc;
120 	io->i_flgs &= ~F_TYPEMASK;
121 	return (0);
122 }
123 
124 upstrategy(io, func)
125 	register struct iob *io;
126 {
127 	int cn, tn, sn, o;
128 	register unit = io->i_unit;
129 	register daddr_t bn;
130 	int recal, info, waitdry;
131 	register struct updevice *upaddr;
132 	struct up_softc *sc;
133 	register struct st *st;
134 	int error, rv = io->i_cc;
135 #ifndef SMALL
136 	int doprintf = 0;
137 #endif
138 
139 	upaddr = (struct updevice *)ubamem(io->i_adapt, ubastd[io->i_ctlr]);
140 	sc = &up_softc[io->i_adapt][io->i_ctlr][unit];
141 	st = &upst[sc->type];
142 	sectsiz = SECTSIZ;
143 	if (io->i_flgs & (F_HDR|F_HCHECK))
144 		sectsiz += HDRSIZ;
145 	upaddr->upcs2 = unit % 8;
146 	if ((upaddr->upds & UPDS_VV) == 0) {
147 		upaddr->upcs1 = UP_DCLR|UP_GO;
148 		upaddr->upcs1 = UP_PRESET|UP_GO;
149 		upaddr->upof = UPOF_FMT22;
150 	}
151 	if ((upaddr->upds & UPDS_DREADY) == 0) {
152 		printf("up%d not ready", unit);
153 		return (-1);
154 	}
155 	info = ubasetup(io, 1);
156 	upaddr->upwc = -io->i_cc / sizeof (short);
157 	recal = 0;
158 	io->i_errcnt = 0;
159 
160 restart:
161 	error = 0;
162 	o = io->i_cc + (upaddr->upwc * sizeof (short));
163 	upaddr->upba = info + o;
164 	bn = io->i_bn + o / sectsiz;
165 #ifndef SMALL
166 	if (doprintf && sc->debug & (UPF_ECCDEBUG|UPF_BSEDEBUG))
167 		printf("wc=%d o=%d i_bn=%d bn=%d\n",
168 			upaddr->upwc, o, io->i_bn, bn);
169 #endif
170 	while((upaddr->upds & UPDS_DRY) == 0)
171 		;
172 	if (upstart(io, bn) != 0) {
173 		rv = -1;
174 		goto done;
175 	}
176 	do {
177 		DELAY(25);
178 	} while ((upaddr->upcs1 & UP_RDY) == 0);
179 	/*
180 	 * If transfer has completed, free UNIBUS
181 	 * resources and return transfer size.
182 	 */
183 	if ((upaddr->upds&UPDS_ERR) == 0 && (upaddr->upcs1&UP_TRE) == 0)
184 		goto done;
185 	bn = io->i_bn +
186 		(io->i_cc + upaddr->upwc * sizeof (short)) / sectsiz;
187 	if (upaddr->uper1 & (UPER1_DCK|UPER1_ECH))
188 		bn--;
189 	cn = bn/st->nspc;
190 	sn = bn%st->nspc;
191 	tn = sn/st->nsect;
192 	sn = sn%st->nsect;
193 #ifndef SMALL
194 	if (sc->debug & (UPF_ECCDEBUG|UPF_BSEDEBUG)) {
195 		printf("up error: sn%d (cyl,trk,sec)=(%d,%d,%d) ",
196 		   bn, cn, tn, sn);
197 		printf("cs2=%b er1=%b er2=%b wc=%d\n",
198 	    	  upaddr->upcs2, UPCS2_BITS, upaddr->uper1,
199 		  UPER1_BITS, upaddr->uper2, UPER2_BITS, upaddr->upwc);
200 	}
201 #endif
202 	waitdry = 0;
203 	while ((upaddr->upds&UPDS_DRY) == 0 && ++waitdry < sectsiz)
204 		DELAY(5);
205 #ifndef SMALL
206 	if (upaddr->uper1&UPER1_WLE) {
207 		/*
208 		 * Give up on write locked devices immediately.
209 		 */
210 		printf("up%d: write locked\n", unit);
211 		rv = -1;
212 		goto done;
213 	}
214 	if (upaddr->uper2 & UPER2_BSE) {
215 		if ((io->i_flgs&F_NBSF) == 0 && upecc(io, BSE) == 0)
216 			goto success;
217 		error = EBSE;
218 		goto hard;
219 	}
220 	/*
221 	 * ECC error. If a soft error, correct it;
222 	 * if correction is too large, no more retries.
223 	 */
224 	if ((upaddr->uper1 & (UPER1_DCK|UPER1_ECH|UPER1_HCRC)) == UPER1_DCK) {
225 		if (upecc(io, ECC) == 0)
226 			goto success;
227 		error = EECC;
228 		goto hard;
229 	}
230 	/*
231 	 * If the error is a header CRC,
232 	 * check if a replacement sector exists in
233 	 * the bad sector table.
234 	 */
235 	if ((upaddr->uper1&UPER1_HCRC) && (io->i_flgs&F_NBSF) == 0 &&
236 	     upecc(io, BSE) == 0)
237 		goto success;
238 #endif
239 	if (++io->i_errcnt > sc->retries) {
240 		/*
241 		 * After 28 retries (16 without offset, and
242 		 * 12 with offset positioning) give up.
243 		 */
244 hard:
245 		if (error == 0) {
246 			error = EHER;
247 			if (upaddr->upcs2 & UPCS2_WCE)
248 				error = EWCK;
249 		}
250 		printf("up error: sn%d (cyl,trk,sec)=(%d,%d,%d) ",
251 		   bn, cn, tn, sn);
252 		printf("cs2=%b er1=%b er2=%b\n",
253 		   upaddr->upcs2, UPCS2_BITS, upaddr->uper1,
254 		   UPER1_BITS, upaddr->uper2, UPER2_BITS);
255 		upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO;
256 		io->i_errblk = bn;
257 		if (io->i_errcnt >= 16) {
258 			upaddr->upof = UPOF_FMT22;
259 			upaddr->upcs1 = UP_RTC|UP_GO;
260 			while ((upaddr->upds&UPDS_DRY) == 0)
261 				DELAY(25);
262 		}
263 		rv = -1;
264 		goto done;
265 	}
266 	/*
267 	 * Clear drive error and, every eight attempts,
268 	 * (starting with the fourth)
269 	 * recalibrate to clear the slate.
270 	 */
271 	upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO;
272 	if ((io->i_errcnt&07) == 4 ) {
273 		upaddr->upcs1 = UP_RECAL|UP_GO;
274 		while ((upaddr->upds&UPDS_DRY) == 0)
275 			DELAY(25);
276 		upaddr->updc = cn;
277 		upaddr->upcs1 = UP_SEEK|UP_GO;
278 		while ((upaddr->upds&UPDS_DRY) == 0)
279 			DELAY(25);
280 	}
281 	if (io->i_errcnt >= 16 && (func & READ)) {
282 		upaddr->upof = up_offset[io->i_errcnt & 017] | UPOF_FMT22;
283 		upaddr->upcs1 = UP_OFFSET|UP_GO;
284 		while ((upaddr->upds&UPDS_DRY) == 0)
285 			DELAY(25);
286 	}
287 	goto restart;
288 
289 success:
290 #define	rounddown(x, y)	(((x) / (y)) * (y))
291 	upaddr->upwc = rounddown(upaddr->upwc, sectsiz / sizeof (short));
292 	if (upaddr->upwc) {
293 #ifndef SMALL
294 		doprintf++;
295 #endif
296 		goto restart;
297 	}
298 done:
299 	/*
300 	 * Release UNIBUS
301 	 */
302 	ubafree(io, info);
303 	/*
304 	 * If we were offset positioning,
305 	 * return to centerline.
306 	 */
307 	if (io->i_errcnt >= 16) {
308 		upaddr->upof = UPOF_FMT22;
309 		upaddr->upcs1 = UP_RTC|UP_GO;
310 		while ((upaddr->upds&UPDS_DRY) == 0)
311 			DELAY(25);
312 	}
313 	return (rv);
314 }
315 
316 #ifndef SMALL
317 /*
318  * Correct an ECC error, and restart the
319  * i/o to complete the transfer (if necessary).
320  * This is quite complicated because the transfer
321  * may be going to an odd memory address base and/or
322  * across a page boundary.
323  */
324 upecc(io, flag)
325 	register struct iob *io;
326 	int flag;
327 {
328 	register i, unit;
329 	register struct up_softc *sc;
330 	register struct updevice *up;
331 	register struct st *st;
332 	caddr_t addr;
333 	int bn, twc, npf, mask, cn, tn, sn;
334 	daddr_t bbn;
335 
336 	/*
337 	 * Npf is the number of sectors transferred
338 	 * before the sector containing the ECC error;
339 	 * bn is the current block number.
340 	 */
341 	unit = io->i_unit;
342 	sc = &up_softc[io->i_adapt][io->i_ctlr][unit];
343 	up = (struct updevice *)ubamem(io->i_adapt, ubastd[io->i_ctlr]);
344 	twc = up->upwc;
345 	npf = ((twc * sizeof(short)) + io->i_cc) / sectsiz;
346 	if (flag == ECC)
347 		npf--;
348 	if (sc->debug & UPF_ECCDEBUG)
349 		printf("npf=%d mask=0x%x ec1=%d wc=%d\n",
350 			npf, up->upec2, up->upec1, twc);
351 	bn = io->i_bn + npf;
352 	st = &upst[sc->type];
353 	cn = bn/st->nspc;
354 	sn = bn%st->nspc;
355 	tn = sn/st->nsect;
356 	sn = sn%st->nsect;
357 
358 	/*
359 	 * ECC correction.
360 	 */
361 	if (flag == ECC) {
362 		int bit, o;
363 
364 		mask = up->upec2;
365 		printf("up%d: soft ecc sn%d\n", unit, bn);
366 		for (i = mask, bit = 0; i; i >>= 1)
367 			if (i & 1)
368 				bit++;
369 		if (bit > sc->ecclim) {
370 			printf("%d-bit error\n", bit);
371 			return (1);
372 		}
373 		/*
374 		 * Compute the byte and bit position of
375 		 * the error.  o is the byte offset in
376 		 * the transfer at which the correction
377 		 * applied.
378 		 */
379 		i = up->upec1 - 1;		/* -1 makes 0 origin */
380 		bit = i & 07;
381 		o = (i & ~07) >> 3;
382 		up->upcs1 = UP_TRE|UP_DCLR|UP_GO;
383 		/*
384 		 * Correct while possible bits remain of mask.
385 		 * Since mask contains 11 bits, we continue while
386 		 * the bit offset is > -11.  Also watch out for
387 		 * end of this block and the end of the transfer.
388 		 */
389 		while (o < sectsiz && (npf*sectsiz)+o < io->i_cc && bit > -11) {
390 			/*
391 			 * addr =
392 			 *  (base address of transfer) +
393 			 *  (# sectors transferred before the error) *
394 			 *    (sector size) +
395 			 *  (byte offset to incorrect data)
396 			 */
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 (sc->debug & UPF_ECCDEBUG)
404 					printf("addr=0x%x old=0x%x ", addr,
405 						(*addr&0xff));
406 				*addr ^= (mask << bit);
407 				if (sc->debug & UPF_ECCDEBUG)
408 					printf("new=0x%x\n", (*addr&0xff));
409 			}
410 			o++, bit -= 8;
411 		}
412 		return (0);
413 	}
414 
415 	/*
416 	 * Bad sector forwarding.
417 	 */
418 	if (flag == BSE) {
419 		/*
420 		 * If not in bad sector table,
421 		 * indicate a hard error to caller.
422 		 */
423 		up->upcs1 = UP_TRE|UP_DCLR|UP_GO;
424 		if ((bbn = isbad(&upbad[io->i_adapt][io->i_ctlr][unit], cn, tn, sn)) < 0)
425 			return (1);
426 		bbn = (st->ncyl * st->nspc) - st->nsect - 1 - bbn;
427 		twc = up->upwc + sectsiz;
428 		up->upwc = - (sectsiz / sizeof (short));
429 		if (sc->debug & UPF_BSEDEBUG)
430 			printf("revector sn %d to %d\n", sn, bbn);
431 		/*
432 	 	 * Clear the drive & read the replacement
433 		 * sector.  If this is in the middle of a
434 		 * transfer, then set up the controller
435 		 * registers in a normal fashion.
436 	 	 * The UNIBUS address need not be changed.
437 	 	 */
438 		while ((up->upcs1 & UP_RDY) == 0)
439 			;
440 		if (upstart(io, bbn))
441 			return (1);		/* error */
442 		io->i_errcnt = 0;		/* success */
443 		do {
444 			DELAY(25);
445 		} while ((up->upcs1 & UP_RDY) == 0) ;
446 		if ((up->upds & UPDS_ERR) || (up->upcs1 & UP_TRE)) {
447 			up->upwc = twc - sectsiz;
448 			return (1);
449 		}
450 	}
451 	if (twc)
452 		up->upwc = twc;
453 	return (0);
454 }
455 #endif /* !SMALL */
456 
457 upstart(io, bn)
458 	register struct iob *io;
459 	daddr_t bn;
460 {
461 	register struct updevice *upaddr;
462 	register struct up_softc *sc;
463 	register struct st *st;
464 	int sn, tn;
465 
466 	upaddr = (struct updevice *)ubamem(io->i_adapt, ubastd[io->i_ctlr]);
467 	sc = &up_softc[io->i_adapt][io->i_ctlr][io->i_unit];
468 	st = &upst[sc->type];
469 	sn = bn%st->nspc;
470 	tn = sn/st->nsect;
471 	sn %= st->nsect;
472 	upaddr->updc = bn/st->nspc;
473 	upaddr->upda = (tn << 8) + sn;
474 	switch (io->i_flgs & F_TYPEMASK) {
475 
476 	case F_RDDATA:
477 		upaddr->upcs1 = UP_RCOM|UP_GO;
478 		break;
479 
480 	case F_WRDATA:
481 		upaddr->upcs1 = UP_WCOM|UP_GO;
482 		break;
483 
484 #ifndef SMALL
485 	case F_HDR|F_RDDATA:
486 		upaddr->upcs1 = UP_RHDR|UP_GO;
487 		break;
488 
489 	case F_HDR|F_WRDATA:
490 		upaddr->upcs1 = UP_WHDR|UP_GO;
491 		break;
492 
493 	case F_CHECK|F_WRDATA:
494 	case F_CHECK|F_RDDATA:
495 		upaddr->upcs1 = UP_WCDATA|UP_GO;
496 		break;
497 
498 	case F_HCHECK|F_WRDATA:
499 	case F_HCHECK|F_RDDATA:
500 		upaddr->upcs1 = UP_WCHDR|UP_GO;
501 		break;
502 #endif
503 
504 	default:
505 		io->i_error = ECMD;
506 		io->i_flgs &= ~F_TYPEMASK;
507 		return (1);
508 	}
509 	return (0);
510 }
511 
512 #ifndef SMALL
513 /*ARGSUSED*/
514 upioctl(io, cmd, arg)
515 	struct iob *io;
516 	int cmd;
517 	caddr_t arg;
518 {
519 	int unit = io->i_unit;
520 	register struct up_softc *sc;
521 	struct st *st;
522 
523 	sc = &up_softc[io->i_adapt][io->i_ctlr][unit];
524 	st = &upst[sc->type];
525 	switch(cmd) {
526 
527 	case SAIODEBUG:
528 		sc->debug = (int)arg;
529 		break;
530 
531 	case SAIODEVDATA:
532 		*(struct st *)arg = *st;
533 		break;
534 
535 	case SAIOGBADINFO:
536 		*(struct dkbad *)arg = upbad[io->i_adapt][io->i_ctlr][unit];
537 		break;
538 
539 	case SAIOECCLIM:
540 		sc->ecclim = (int)arg;
541 		break;
542 
543 	case SAIORETRIES:
544 		sc->retries = (int)arg;
545 		break;
546 
547 	default:
548 		return (ECMD);
549 	}
550 	return (0);
551 }
552 #endif /* !SMALL */
553