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