docecc.c 16 KB

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