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