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