docecc.c 16 KB

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