Merge commit 'v2.6.28-rc2' into x86/pci-ioapic-boot-irq-quirks
[pandora-kernel.git] / drivers / mtd / devices / docecc.c
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.
6  *
7  * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
8  * Copyright (C) 2000 Netgem S.A.
9  *
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.
14  *
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.
19  *
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
23  */
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/miscdevice.h>
30 #include <linux/delay.h>
31 #include <linux/slab.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34
35 #include <linux/mtd/compatmac.h> /* for min() in older kernels */
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/doc2000.h>
38
39 #define DEBUG_ECC 0
40 /* need to undef it (from asm/termbits.h) */
41 #undef B0
42
43 #define MM 10 /* Symbol size in bits */
44 #define KK (1023-4) /* Number of data symbols per block */
45 #define B0 510 /* First root of generator polynomial, alpha form */
46 #define PRIM 1 /* power of alpha used to generate roots of generator poly */
47 #define NN ((1 << MM) - 1)
48
49 typedef unsigned short dtype;
50
51 /* 1+x^3+x^10 */
52 static const int Pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
53
54 /* This defines the type used to store an element of the Galois Field
55  * used by the code. Make sure this is something larger than a char if
56  * if anything larger than GF(256) is used.
57  *
58  * Note: unsigned char will work up to GF(256) but int seems to run
59  * faster on the Pentium.
60  */
61 typedef int gf;
62
63 /* No legal value in index form represents zero, so
64  * we need a special value for this purpose
65  */
66 #define A0      (NN)
67
68 /* Compute x % NN, where NN is 2**MM - 1,
69  * without a slow divide
70  */
71 static inline gf
72 modnn(int x)
73 {
74   while (x >= NN) {
75     x -= NN;
76     x = (x >> MM) + (x & NN);
77   }
78   return x;
79 }
80
81 #define CLEAR(a,n) {\
82 int ci;\
83 for(ci=(n)-1;ci >=0;ci--)\
84 (a)[ci] = 0;\
85 }
86
87 #define COPY(a,b,n) {\
88 int ci;\
89 for(ci=(n)-1;ci >=0;ci--)\
90 (a)[ci] = (b)[ci];\
91 }
92
93 #define COPYDOWN(a,b,n) {\
94 int ci;\
95 for(ci=(n)-1;ci >=0;ci--)\
96 (a)[ci] = (b)[ci];\
97 }
98
99 #define Ldec 1
100
101 /* generate GF(2**m) from the irreducible polynomial p(X) in Pp[0]..Pp[m]
102    lookup tables:  index->polynomial form   alpha_to[] contains j=alpha**i;
103                    polynomial form -> index form  index_of[j=alpha**i] = i
104    alpha=2 is the primitive element of GF(2**m)
105    HARI's COMMENT: (4/13/94) alpha_to[] can be used as follows:
106         Let @ represent the primitive element commonly called "alpha" that
107    is the root of the primitive polynomial p(x). Then in GF(2^m), for any
108    0 <= i <= 2^m-2,
109         @^i = a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
110    where the binary vector (a(0),a(1),a(2),...,a(m-1)) is the representation
111    of the integer "alpha_to[i]" with a(0) being the LSB and a(m-1) the MSB. Thus for
112    example the polynomial representation of @^5 would be given by the binary
113    representation of the integer "alpha_to[5]".
114                    Similarily, index_of[] can be used as follows:
115         As above, let @ represent the primitive element of GF(2^m) that is
116    the root of the primitive polynomial p(x). In order to find the power
117    of @ (alpha) that has the polynomial representation
118         a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
119    we consider the integer "i" whose binary representation with a(0) being LSB
120    and a(m-1) MSB is (a(0),a(1),...,a(m-1)) and locate the entry
121    "index_of[i]". Now, @^index_of[i] is that element whose polynomial
122     representation is (a(0),a(1),a(2),...,a(m-1)).
123    NOTE:
124         The element alpha_to[2^m-1] = 0 always signifying that the
125    representation of "@^infinity" = 0 is (0,0,0,...,0).
126         Similarily, the element index_of[0] = A0 always signifying
127    that the power of alpha which has the polynomial representation
128    (0,0,...,0) is "infinity".
129
130 */
131
132 static void
133 generate_gf(dtype Alpha_to[NN + 1], dtype Index_of[NN + 1])
134 {
135   register int i, mask;
136
137   mask = 1;
138   Alpha_to[MM] = 0;
139   for (i = 0; i < MM; i++) {
140     Alpha_to[i] = mask;
141     Index_of[Alpha_to[i]] = i;
142     /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
143     if (Pp[i] != 0)
144       Alpha_to[MM] ^= mask;     /* Bit-wise EXOR operation */
145     mask <<= 1; /* single left-shift */
146   }
147   Index_of[Alpha_to[MM]] = MM;
148   /*
149    * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
150    * poly-repr of @^i shifted left one-bit and accounting for any @^MM
151    * term that may occur when poly-repr of @^i is shifted.
152    */
153   mask >>= 1;
154   for (i = MM + 1; i < NN; i++) {
155     if (Alpha_to[i - 1] >= mask)
156       Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1);
157     else
158       Alpha_to[i] = Alpha_to[i - 1] << 1;
159     Index_of[Alpha_to[i]] = i;
160   }
161   Index_of[0] = A0;
162   Alpha_to[NN] = 0;
163 }
164
165 /*
166  * Performs ERRORS+ERASURES decoding of RS codes. bb[] is the content
167  * of the feedback shift register after having processed the data and
168  * the ECC.
169  *
170  * Return number of symbols corrected, or -1 if codeword is illegal
171  * or uncorrectable. If eras_pos is non-null, the detected error locations
172  * are written back. NOTE! This array must be at least NN-KK elements long.
173  * The corrected data are written in eras_val[]. They must be xor with the data
174  * to retrieve the correct data : data[erase_pos[i]] ^= erase_val[i] .
175  *
176  * First "no_eras" erasures are declared by the calling program. Then, the
177  * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2).
178  * If the number of channel errors is not greater than "t_after_eras" the
179  * transmitted codeword will be recovered. Details of algorithm can be found
180  * in R. Blahut's "Theory ... of Error-Correcting Codes".
181
182  * Warning: the eras_pos[] array must not contain duplicate entries; decoder failure
183  * will result. The decoder *could* check for this condition, but it would involve
184  * extra time on every decoding operation.
185  * */
186 static int
187 eras_dec_rs(dtype Alpha_to[NN + 1], dtype Index_of[NN + 1],
188             gf bb[NN - KK + 1], gf eras_val[NN-KK], int eras_pos[NN-KK],
189             int no_eras)
190 {
191   int deg_lambda, el, deg_omega;
192   int i, j, r,k;
193   gf u,q,tmp,num1,num2,den,discr_r;
194   gf lambda[NN-KK + 1], s[NN-KK + 1];   /* Err+Eras Locator poly
195                                          * and syndrome poly */
196   gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1];
197   gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];
198   int syn_error, count;
199
200   syn_error = 0;
201   for(i=0;i<NN-KK;i++)
202       syn_error |= bb[i];
203
204   if (!syn_error) {
205     /* if remainder is zero, data[] is a codeword and there are no
206      * errors to correct. So return data[] unmodified
207      */
208     count = 0;
209     goto finish;
210   }
211
212   for(i=1;i<=NN-KK;i++){
213     s[i] = bb[0];
214   }
215   for(j=1;j<NN-KK;j++){
216     if(bb[j] == 0)
217       continue;
218     tmp = Index_of[bb[j]];
219
220     for(i=1;i<=NN-KK;i++)
221       s[i] ^= Alpha_to[modnn(tmp + (B0+i-1)*PRIM*j)];
222   }
223
224   /* undo the feedback register implicit multiplication and convert
225      syndromes to index form */
226
227   for(i=1;i<=NN-KK;i++) {
228       tmp = Index_of[s[i]];
229       if (tmp != A0)
230           tmp = modnn(tmp + 2 * KK * (B0+i-1)*PRIM);
231       s[i] = tmp;
232   }
233
234   CLEAR(&lambda[1],NN-KK);
235   lambda[0] = 1;
236
237   if (no_eras > 0) {
238     /* Init lambda to be the erasure locator polynomial */
239     lambda[1] = Alpha_to[modnn(PRIM * eras_pos[0])];
240     for (i = 1; i < no_eras; i++) {
241       u = modnn(PRIM*eras_pos[i]);
242       for (j = i+1; j > 0; j--) {
243         tmp = Index_of[lambda[j - 1]];
244         if(tmp != A0)
245           lambda[j] ^= Alpha_to[modnn(u + tmp)];
246       }
247     }
248 #if DEBUG_ECC >= 1
249     /* Test code that verifies the erasure locator polynomial just constructed
250        Needed only for decoder debugging. */
251
252     /* find roots of the erasure location polynomial */
253     for(i=1;i<=no_eras;i++)
254       reg[i] = Index_of[lambda[i]];
255     count = 0;
256     for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
257       q = 1;
258       for (j = 1; j <= no_eras; j++)
259         if (reg[j] != A0) {
260           reg[j] = modnn(reg[j] + j);
261           q ^= Alpha_to[reg[j]];
262         }
263       if (q != 0)
264         continue;
265       /* store root and error location number indices */
266       root[count] = i;
267       loc[count] = k;
268       count++;
269     }
270     if (count != no_eras) {
271       printf("\n lambda(x) is WRONG\n");
272       count = -1;
273       goto finish;
274     }
275 #if DEBUG_ECC >= 2
276     printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
277     for (i = 0; i < count; i++)
278       printf("%d ", loc[i]);
279     printf("\n");
280 #endif
281 #endif
282   }
283   for(i=0;i<NN-KK+1;i++)
284     b[i] = Index_of[lambda[i]];
285
286   /*
287    * Begin Berlekamp-Massey algorithm to determine error+erasure
288    * locator polynomial
289    */
290   r = no_eras;
291   el = no_eras;
292   while (++r <= NN-KK) {        /* r is the step number */
293     /* Compute discrepancy at the r-th step in poly-form */
294     discr_r = 0;
295     for (i = 0; i < r; i++){
296       if ((lambda[i] != 0) && (s[r - i] != A0)) {
297         discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])];
298       }
299     }
300     discr_r = Index_of[discr_r];        /* Index form */
301     if (discr_r == A0) {
302       /* 2 lines below: B(x) <-- x*B(x) */
303       COPYDOWN(&b[1],b,NN-KK);
304       b[0] = A0;
305     } else {
306       /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
307       t[0] = lambda[0];
308       for (i = 0 ; i < NN-KK; i++) {
309         if(b[i] != A0)
310           t[i+1] = lambda[i+1] ^ Alpha_to[modnn(discr_r + b[i])];
311         else
312           t[i+1] = lambda[i+1];
313       }
314       if (2 * el <= r + no_eras - 1) {
315         el = r + no_eras - el;
316         /*
317          * 2 lines below: B(x) <-- inv(discr_r) *
318          * lambda(x)
319          */
320         for (i = 0; i <= NN-KK; i++)
321           b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN);
322       } else {
323         /* 2 lines below: B(x) <-- x*B(x) */
324         COPYDOWN(&b[1],b,NN-KK);
325         b[0] = A0;
326       }
327       COPY(lambda,t,NN-KK+1);
328     }
329   }
330
331   /* Convert lambda to index form and compute deg(lambda(x)) */
332   deg_lambda = 0;
333   for(i=0;i<NN-KK+1;i++){
334     lambda[i] = Index_of[lambda[i]];
335     if(lambda[i] != A0)
336       deg_lambda = i;
337   }
338   /*
339    * Find roots of the error+erasure locator polynomial by Chien
340    * Search
341    */
342   COPY(&reg[1],&lambda[1],NN-KK);
343   count = 0;            /* Number of roots of lambda(x) */
344   for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
345     q = 1;
346     for (j = deg_lambda; j > 0; j--){
347       if (reg[j] != A0) {
348         reg[j] = modnn(reg[j] + j);
349         q ^= Alpha_to[reg[j]];
350       }
351     }
352     if (q != 0)
353       continue;
354     /* store root (index-form) and error location number */
355     root[count] = i;
356     loc[count] = k;
357     /* If we've already found max possible roots,
358      * abort the search to save time
359      */
360     if(++count == deg_lambda)
361       break;
362   }
363   if (deg_lambda != count) {
364     /*
365      * deg(lambda) unequal to number of roots => uncorrectable
366      * error detected
367      */
368     count = -1;
369     goto finish;
370   }
371   /*
372    * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
373    * x**(NN-KK)). in index form. Also find deg(omega).
374    */
375   deg_omega = 0;
376   for (i = 0; i < NN-KK;i++){
377     tmp = 0;
378     j = (deg_lambda < i) ? deg_lambda : i;
379     for(;j >= 0; j--){
380       if ((s[i + 1 - j] != A0) && (lambda[j] != A0))
381         tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
382     }
383     if(tmp != 0)
384       deg_omega = i;
385     omega[i] = Index_of[tmp];
386   }
387   omega[NN-KK] = A0;
388
389   /*
390    * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
391    * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form
392    */
393   for (j = count-1; j >=0; j--) {
394     num1 = 0;
395     for (i = deg_omega; i >= 0; i--) {
396       if (omega[i] != A0)
397         num1  ^= Alpha_to[modnn(omega[i] + i * root[j])];
398     }
399     num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)];
400     den = 0;
401
402     /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
403     for (i = min(deg_lambda,NN-KK-1) & ~1; i >= 0; i -=2) {
404       if(lambda[i+1] != A0)
405         den ^= Alpha_to[modnn(lambda[i+1] + i * root[j])];
406     }
407     if (den == 0) {
408 #if DEBUG_ECC >= 1
409       printf("\n ERROR: denominator = 0\n");
410 #endif
411       /* Convert to dual- basis */
412       count = -1;
413       goto finish;
414     }
415     /* Apply error to data */
416     if (num1 != 0) {
417         eras_val[j] = Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])];
418     } else {
419         eras_val[j] = 0;
420     }
421   }
422  finish:
423   for(i=0;i<count;i++)
424       eras_pos[i] = loc[i];
425   return count;
426 }
427
428 /***************************************************************************/
429 /* The DOC specific code begins here */
430
431 #define SECTOR_SIZE 512
432 /* The sector bytes are packed into NB_DATA MM bits words */
433 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / MM)
434
435 /*
436  * Correct the errors in 'sector[]' by using 'ecc1[]' which is the
437  * content of the feedback shift register applyied to the sector and
438  * the ECC. Return the number of errors corrected (and correct them in
439  * sector), or -1 if error
440  */
441 int doc_decode_ecc(unsigned char sector[SECTOR_SIZE], unsigned char ecc1[6])
442 {
443     int parity, i, nb_errors;
444     gf bb[NN - KK + 1];
445     gf error_val[NN-KK];
446     int error_pos[NN-KK], pos, bitpos, index, val;
447     dtype *Alpha_to, *Index_of;
448
449     /* init log and exp tables here to save memory. However, it is slower */
450     Alpha_to = kmalloc((NN + 1) * sizeof(dtype), GFP_KERNEL);
451     if (!Alpha_to)
452         return -1;
453
454     Index_of = kmalloc((NN + 1) * sizeof(dtype), GFP_KERNEL);
455     if (!Index_of) {
456         kfree(Alpha_to);
457         return -1;
458     }
459
460     generate_gf(Alpha_to, Index_of);
461
462     parity = ecc1[1];
463
464     bb[0] =  (ecc1[4] & 0xff) | ((ecc1[5] & 0x03) << 8);
465     bb[1] = ((ecc1[5] & 0xfc) >> 2) | ((ecc1[2] & 0x0f) << 6);
466     bb[2] = ((ecc1[2] & 0xf0) >> 4) | ((ecc1[3] & 0x3f) << 4);
467     bb[3] = ((ecc1[3] & 0xc0) >> 6) | ((ecc1[0] & 0xff) << 2);
468
469     nb_errors = eras_dec_rs(Alpha_to, Index_of, bb,
470                             error_val, error_pos, 0);
471     if (nb_errors <= 0)
472         goto the_end;
473
474     /* correct the errors */
475     for(i=0;i<nb_errors;i++) {
476         pos = error_pos[i];
477         if (pos >= NB_DATA && pos < KK) {
478             nb_errors = -1;
479             goto the_end;
480         }
481         if (pos < NB_DATA) {
482             /* extract bit position (MSB first) */
483             pos = 10 * (NB_DATA - 1 - pos) - 6;
484             /* now correct the following 10 bits. At most two bytes
485                can be modified since pos is even */
486             index = (pos >> 3) ^ 1;
487             bitpos = pos & 7;
488             if ((index >= 0 && index < SECTOR_SIZE) ||
489                 index == (SECTOR_SIZE + 1)) {
490                 val = error_val[i] >> (2 + bitpos);
491                 parity ^= val;
492                 if (index < SECTOR_SIZE)
493                     sector[index] ^= val;
494             }
495             index = ((pos >> 3) + 1) ^ 1;
496             bitpos = (bitpos + 10) & 7;
497             if (bitpos == 0)
498                 bitpos = 8;
499             if ((index >= 0 && index < SECTOR_SIZE) ||
500                 index == (SECTOR_SIZE + 1)) {
501                 val = error_val[i] << (8 - bitpos);
502                 parity ^= val;
503                 if (index < SECTOR_SIZE)
504                     sector[index] ^= val;
505             }
506         }
507     }
508
509     /* use parity to test extra errors */
510     if ((parity & 0xff) != 0)
511         nb_errors = -1;
512
513  the_end:
514     kfree(Alpha_to);
515     kfree(Index_of);
516     return nb_errors;
517 }
518
519 EXPORT_SYMBOL_GPL(doc_decode_ecc);
520
521 MODULE_LICENSE("GPL");
522 MODULE_AUTHOR("Fabrice Bellard <fabrice.bellard@netgem.com>");
523 MODULE_DESCRIPTION("ECC code for correcting errors detected by DiskOnChip 2000 and Millennium ECC hardware");