docecc.c 16 KB

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