ACPI: pci_root: simplify list traversals
[linux-2.6/linux-acpi-2.6.git] / drivers / mtd / devices / docecc.c
bloba19cda52da5c15804cdf50aa594f41dc4f27b433
1 /*
2 * ECC algorithm for M-systems disk on chip. We use the excellent Reed
3 * Solmon code of Phil Karn (karn@ka9q.ampr.org) available under the
4 * GNU GPL License. The rest is simply to convert the disk on chip
5 * syndrom into a standard syndom.
7 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
8 * Copyright (C) 2000 Netgem S.A.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <asm/errno.h>
27 #include <asm/io.h>
28 #include <asm/uaccess.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
34 #include <linux/mtd/compatmac.h> /* for min() in older kernels */
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/doc2000.h>
38 #define DEBUG_ECC 0
39 /* need to undef it (from asm/termbits.h) */
40 #undef B0
42 #define MM 10 /* Symbol size in bits */
43 #define KK (1023-4) /* Number of data symbols per block */
44 #define B0 510 /* First root of generator polynomial, alpha form */
45 #define PRIM 1 /* power of alpha used to generate roots of generator poly */
46 #define NN ((1 << MM) - 1)
48 typedef unsigned short dtype;
50 /* 1+x^3+x^10 */
51 static const int Pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
53 /* This defines the type used to store an element of the Galois Field
54 * used by the code. Make sure this is something larger than a char if
55 * if anything larger than GF(256) is used.
57 * Note: unsigned char will work up to GF(256) but int seems to run
58 * faster on the Pentium.
60 typedef int gf;
62 /* No legal value in index form represents zero, so
63 * we need a special value for this purpose
65 #define A0 (NN)
67 /* Compute x % NN, where NN is 2**MM - 1,
68 * without a slow divide
70 static inline gf
71 modnn(int x)
73 while (x >= NN) {
74 x -= NN;
75 x = (x >> MM) + (x & NN);
77 return x;
80 #define CLEAR(a,n) {\
81 int ci;\
82 for(ci=(n)-1;ci >=0;ci--)\
83 (a)[ci] = 0;\
86 #define COPY(a,b,n) {\
87 int ci;\
88 for(ci=(n)-1;ci >=0;ci--)\
89 (a)[ci] = (b)[ci];\
92 #define COPYDOWN(a,b,n) {\
93 int ci;\
94 for(ci=(n)-1;ci >=0;ci--)\
95 (a)[ci] = (b)[ci];\
98 #define Ldec 1
100 /* generate GF(2**m) from the irreducible polynomial p(X) in Pp[0]..Pp[m]
101 lookup tables: index->polynomial form alpha_to[] contains j=alpha**i;
102 polynomial form -> index form index_of[j=alpha**i] = i
103 alpha=2 is the primitive element of GF(2**m)
104 HARI's COMMENT: (4/13/94) alpha_to[] can be used as follows:
105 Let @ represent the primitive element commonly called "alpha" that
106 is the root of the primitive polynomial p(x). Then in GF(2^m), for any
107 0 <= i <= 2^m-2,
108 @^i = a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
109 where the binary vector (a(0),a(1),a(2),...,a(m-1)) is the representation
110 of the integer "alpha_to[i]" with a(0) being the LSB and a(m-1) the MSB. Thus for
111 example the polynomial representation of @^5 would be given by the binary
112 representation of the integer "alpha_to[5]".
113 Similarily, index_of[] can be used as follows:
114 As above, let @ represent the primitive element of GF(2^m) that is
115 the root of the primitive polynomial p(x). In order to find the power
116 of @ (alpha) that has the polynomial representation
117 a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
118 we consider the integer "i" whose binary representation with a(0) being LSB
119 and a(m-1) MSB is (a(0),a(1),...,a(m-1)) and locate the entry
120 "index_of[i]". Now, @^index_of[i] is that element whose polynomial
121 representation is (a(0),a(1),a(2),...,a(m-1)).
122 NOTE:
123 The element alpha_to[2^m-1] = 0 always signifying that the
124 representation of "@^infinity" = 0 is (0,0,0,...,0).
125 Similarily, the element index_of[0] = A0 always signifying
126 that the power of alpha which has the polynomial representation
127 (0,0,...,0) is "infinity".
131 static void
132 generate_gf(dtype Alpha_to[NN + 1], dtype Index_of[NN + 1])
134 register int i, mask;
136 mask = 1;
137 Alpha_to[MM] = 0;
138 for (i = 0; i < MM; i++) {
139 Alpha_to[i] = mask;
140 Index_of[Alpha_to[i]] = i;
141 /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
142 if (Pp[i] != 0)
143 Alpha_to[MM] ^= mask; /* Bit-wise EXOR operation */
144 mask <<= 1; /* single left-shift */
146 Index_of[Alpha_to[MM]] = MM;
148 * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
149 * poly-repr of @^i shifted left one-bit and accounting for any @^MM
150 * term that may occur when poly-repr of @^i is shifted.
152 mask >>= 1;
153 for (i = MM + 1; i < NN; i++) {
154 if (Alpha_to[i - 1] >= mask)
155 Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1);
156 else
157 Alpha_to[i] = Alpha_to[i - 1] << 1;
158 Index_of[Alpha_to[i]] = i;
160 Index_of[0] = A0;
161 Alpha_to[NN] = 0;
165 * Performs ERRORS+ERASURES decoding of RS codes. bb[] is the content
166 * of the feedback shift register after having processed the data and
167 * the ECC.
169 * Return number of symbols corrected, or -1 if codeword is illegal
170 * or uncorrectable. If eras_pos is non-null, the detected error locations
171 * are written back. NOTE! This array must be at least NN-KK elements long.
172 * The corrected data are written in eras_val[]. They must be xor with the data
173 * to retrieve the correct data : data[erase_pos[i]] ^= erase_val[i] .
175 * First "no_eras" erasures are declared by the calling program. Then, the
176 * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2).
177 * If the number of channel errors is not greater than "t_after_eras" the
178 * transmitted codeword will be recovered. Details of algorithm can be found
179 * in R. Blahut's "Theory ... of Error-Correcting Codes".
181 * Warning: the eras_pos[] array must not contain duplicate entries; decoder failure
182 * will result. The decoder *could* check for this condition, but it would involve
183 * extra time on every decoding operation.
184 * */
185 static int
186 eras_dec_rs(dtype Alpha_to[NN + 1], dtype Index_of[NN + 1],
187 gf bb[NN - KK + 1], gf eras_val[NN-KK], int eras_pos[NN-KK],
188 int no_eras)
190 int deg_lambda, el, deg_omega;
191 int i, j, r,k;
192 gf u,q,tmp,num1,num2,den,discr_r;
193 gf lambda[NN-KK + 1], s[NN-KK + 1]; /* Err+Eras Locator poly
194 * and syndrome poly */
195 gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1];
196 gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];
197 int syn_error, count;
199 syn_error = 0;
200 for(i=0;i<NN-KK;i++)
201 syn_error |= bb[i];
203 if (!syn_error) {
204 /* if remainder is zero, data[] is a codeword and there are no
205 * errors to correct. So return data[] unmodified
207 count = 0;
208 goto finish;
211 for(i=1;i<=NN-KK;i++){
212 s[i] = bb[0];
214 for(j=1;j<NN-KK;j++){
215 if(bb[j] == 0)
216 continue;
217 tmp = Index_of[bb[j]];
219 for(i=1;i<=NN-KK;i++)
220 s[i] ^= Alpha_to[modnn(tmp + (B0+i-1)*PRIM*j)];
223 /* undo the feedback register implicit multiplication and convert
224 syndromes to index form */
226 for(i=1;i<=NN-KK;i++) {
227 tmp = Index_of[s[i]];
228 if (tmp != A0)
229 tmp = modnn(tmp + 2 * KK * (B0+i-1)*PRIM);
230 s[i] = tmp;
233 CLEAR(&lambda[1],NN-KK);
234 lambda[0] = 1;
236 if (no_eras > 0) {
237 /* Init lambda to be the erasure locator polynomial */
238 lambda[1] = Alpha_to[modnn(PRIM * eras_pos[0])];
239 for (i = 1; i < no_eras; i++) {
240 u = modnn(PRIM*eras_pos[i]);
241 for (j = i+1; j > 0; j--) {
242 tmp = Index_of[lambda[j - 1]];
243 if(tmp != A0)
244 lambda[j] ^= Alpha_to[modnn(u + tmp)];
247 #if DEBUG_ECC >= 1
248 /* Test code that verifies the erasure locator polynomial just constructed
249 Needed only for decoder debugging. */
251 /* find roots of the erasure location polynomial */
252 for(i=1;i<=no_eras;i++)
253 reg[i] = Index_of[lambda[i]];
254 count = 0;
255 for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
256 q = 1;
257 for (j = 1; j <= no_eras; j++)
258 if (reg[j] != A0) {
259 reg[j] = modnn(reg[j] + j);
260 q ^= Alpha_to[reg[j]];
262 if (q != 0)
263 continue;
264 /* store root and error location number indices */
265 root[count] = i;
266 loc[count] = k;
267 count++;
269 if (count != no_eras) {
270 printf("\n lambda(x) is WRONG\n");
271 count = -1;
272 goto finish;
274 #if DEBUG_ECC >= 2
275 printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
276 for (i = 0; i < count; i++)
277 printf("%d ", loc[i]);
278 printf("\n");
279 #endif
280 #endif
282 for(i=0;i<NN-KK+1;i++)
283 b[i] = Index_of[lambda[i]];
286 * Begin Berlekamp-Massey algorithm to determine error+erasure
287 * locator polynomial
289 r = no_eras;
290 el = no_eras;
291 while (++r <= NN-KK) { /* r is the step number */
292 /* Compute discrepancy at the r-th step in poly-form */
293 discr_r = 0;
294 for (i = 0; i < r; i++){
295 if ((lambda[i] != 0) && (s[r - i] != A0)) {
296 discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])];
299 discr_r = Index_of[discr_r]; /* Index form */
300 if (discr_r == A0) {
301 /* 2 lines below: B(x) <-- x*B(x) */
302 COPYDOWN(&b[1],b,NN-KK);
303 b[0] = A0;
304 } else {
305 /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
306 t[0] = lambda[0];
307 for (i = 0 ; i < NN-KK; i++) {
308 if(b[i] != A0)
309 t[i+1] = lambda[i+1] ^ Alpha_to[modnn(discr_r + b[i])];
310 else
311 t[i+1] = lambda[i+1];
313 if (2 * el <= r + no_eras - 1) {
314 el = r + no_eras - el;
316 * 2 lines below: B(x) <-- inv(discr_r) *
317 * lambda(x)
319 for (i = 0; i <= NN-KK; i++)
320 b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN);
321 } else {
322 /* 2 lines below: B(x) <-- x*B(x) */
323 COPYDOWN(&b[1],b,NN-KK);
324 b[0] = A0;
326 COPY(lambda,t,NN-KK+1);
330 /* Convert lambda to index form and compute deg(lambda(x)) */
331 deg_lambda = 0;
332 for(i=0;i<NN-KK+1;i++){
333 lambda[i] = Index_of[lambda[i]];
334 if(lambda[i] != A0)
335 deg_lambda = i;
338 * Find roots of the error+erasure locator polynomial by Chien
339 * Search
341 COPY(&reg[1],&lambda[1],NN-KK);
342 count = 0; /* Number of roots of lambda(x) */
343 for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
344 q = 1;
345 for (j = deg_lambda; j > 0; j--){
346 if (reg[j] != A0) {
347 reg[j] = modnn(reg[j] + j);
348 q ^= Alpha_to[reg[j]];
351 if (q != 0)
352 continue;
353 /* store root (index-form) and error location number */
354 root[count] = i;
355 loc[count] = k;
356 /* If we've already found max possible roots,
357 * abort the search to save time
359 if(++count == deg_lambda)
360 break;
362 if (deg_lambda != count) {
364 * deg(lambda) unequal to number of roots => uncorrectable
365 * error detected
367 count = -1;
368 goto finish;
371 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
372 * x**(NN-KK)). in index form. Also find deg(omega).
374 deg_omega = 0;
375 for (i = 0; i < NN-KK;i++){
376 tmp = 0;
377 j = (deg_lambda < i) ? deg_lambda : i;
378 for(;j >= 0; j--){
379 if ((s[i + 1 - j] != A0) && (lambda[j] != A0))
380 tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
382 if(tmp != 0)
383 deg_omega = i;
384 omega[i] = Index_of[tmp];
386 omega[NN-KK] = A0;
389 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
390 * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form
392 for (j = count-1; j >=0; j--) {
393 num1 = 0;
394 for (i = deg_omega; i >= 0; i--) {
395 if (omega[i] != A0)
396 num1 ^= Alpha_to[modnn(omega[i] + i * root[j])];
398 num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)];
399 den = 0;
401 /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
402 for (i = min(deg_lambda,NN-KK-1) & ~1; i >= 0; i -=2) {
403 if(lambda[i+1] != A0)
404 den ^= Alpha_to[modnn(lambda[i+1] + i * root[j])];
406 if (den == 0) {
407 #if DEBUG_ECC >= 1
408 printf("\n ERROR: denominator = 0\n");
409 #endif
410 /* Convert to dual- basis */
411 count = -1;
412 goto finish;
414 /* Apply error to data */
415 if (num1 != 0) {
416 eras_val[j] = Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])];
417 } else {
418 eras_val[j] = 0;
421 finish:
422 for(i=0;i<count;i++)
423 eras_pos[i] = loc[i];
424 return count;
427 /***************************************************************************/
428 /* The DOC specific code begins here */
430 #define SECTOR_SIZE 512
431 /* The sector bytes are packed into NB_DATA MM bits words */
432 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / MM)
435 * Correct the errors in 'sector[]' by using 'ecc1[]' which is the
436 * content of the feedback shift register applyied to the sector and
437 * the ECC. Return the number of errors corrected (and correct them in
438 * sector), or -1 if error
440 int doc_decode_ecc(unsigned char sector[SECTOR_SIZE], unsigned char ecc1[6])
442 int parity, i, nb_errors;
443 gf bb[NN - KK + 1];
444 gf error_val[NN-KK];
445 int error_pos[NN-KK], pos, bitpos, index, val;
446 dtype *Alpha_to, *Index_of;
448 /* init log and exp tables here to save memory. However, it is slower */
449 Alpha_to = kmalloc((NN + 1) * sizeof(dtype), GFP_KERNEL);
450 if (!Alpha_to)
451 return -1;
453 Index_of = kmalloc((NN + 1) * sizeof(dtype), GFP_KERNEL);
454 if (!Index_of) {
455 kfree(Alpha_to);
456 return -1;
459 generate_gf(Alpha_to, Index_of);
461 parity = ecc1[1];
463 bb[0] = (ecc1[4] & 0xff) | ((ecc1[5] & 0x03) << 8);
464 bb[1] = ((ecc1[5] & 0xfc) >> 2) | ((ecc1[2] & 0x0f) << 6);
465 bb[2] = ((ecc1[2] & 0xf0) >> 4) | ((ecc1[3] & 0x3f) << 4);
466 bb[3] = ((ecc1[3] & 0xc0) >> 6) | ((ecc1[0] & 0xff) << 2);
468 nb_errors = eras_dec_rs(Alpha_to, Index_of, bb,
469 error_val, error_pos, 0);
470 if (nb_errors <= 0)
471 goto the_end;
473 /* correct the errors */
474 for(i=0;i<nb_errors;i++) {
475 pos = error_pos[i];
476 if (pos >= NB_DATA && pos < KK) {
477 nb_errors = -1;
478 goto the_end;
480 if (pos < NB_DATA) {
481 /* extract bit position (MSB first) */
482 pos = 10 * (NB_DATA - 1 - pos) - 6;
483 /* now correct the following 10 bits. At most two bytes
484 can be modified since pos is even */
485 index = (pos >> 3) ^ 1;
486 bitpos = pos & 7;
487 if ((index >= 0 && index < SECTOR_SIZE) ||
488 index == (SECTOR_SIZE + 1)) {
489 val = error_val[i] >> (2 + bitpos);
490 parity ^= val;
491 if (index < SECTOR_SIZE)
492 sector[index] ^= val;
494 index = ((pos >> 3) + 1) ^ 1;
495 bitpos = (bitpos + 10) & 7;
496 if (bitpos == 0)
497 bitpos = 8;
498 if ((index >= 0 && index < SECTOR_SIZE) ||
499 index == (SECTOR_SIZE + 1)) {
500 val = error_val[i] << (8 - bitpos);
501 parity ^= val;
502 if (index < SECTOR_SIZE)
503 sector[index] ^= val;
508 /* use parity to test extra errors */
509 if ((parity & 0xff) != 0)
510 nb_errors = -1;
512 the_end:
513 kfree(Alpha_to);
514 kfree(Index_of);
515 return nb_errors;
518 EXPORT_SYMBOL_GPL(doc_decode_ecc);
520 MODULE_LICENSE("GPL");
521 MODULE_AUTHOR("Fabrice Bellard <fabrice.bellard@netgem.com>");
522 MODULE_DESCRIPTION("ECC code for correcting errors detected by DiskOnChip 2000 and Millennium ECC hardware");