bch.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. /*
  2. * Generic binary BCH encoding/decoding library
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 51
  15. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Copyright © 2011 Parrot S.A.
  18. *
  19. * Author: Ivan Djelic <ivan.djelic@parrot.com>
  20. *
  21. * Description:
  22. *
  23. * This library provides runtime configurable encoding/decoding of binary
  24. * Bose-Chaudhuri-Hocquenghem (BCH) codes.
  25. *
  26. * Call init_bch to get a pointer to a newly allocated bch_control structure for
  27. * the given m (Galois field order), t (error correction capability) and
  28. * (optional) primitive polynomial parameters.
  29. *
  30. * Call encode_bch to compute and store ecc parity bytes to a given buffer.
  31. * Call decode_bch to detect and locate errors in received data.
  32. *
  33. * On systems supporting hw BCH features, intermediate results may be provided
  34. * to decode_bch in order to skip certain steps. See decode_bch() documentation
  35. * for details.
  36. *
  37. * Option CONFIG_BCH_CONST_PARAMS can be used to force fixed values of
  38. * parameters m and t; thus allowing extra compiler optimizations and providing
  39. * better (up to 2x) encoding performance. Using this option makes sense when
  40. * (m,t) are fixed and known in advance, e.g. when using BCH error correction
  41. * on a particular NAND flash device.
  42. *
  43. * Algorithmic details:
  44. *
  45. * Encoding is performed by processing 32 input bits in parallel, using 4
  46. * remainder lookup tables.
  47. *
  48. * The final stage of decoding involves the following internal steps:
  49. * a. Syndrome computation
  50. * b. Error locator polynomial computation using Berlekamp-Massey algorithm
  51. * c. Error locator root finding (by far the most expensive step)
  52. *
  53. * In this implementation, step c is not performed using the usual Chien search.
  54. * Instead, an alternative approach described in [1] is used. It consists in
  55. * factoring the error locator polynomial using the Berlekamp Trace algorithm
  56. * (BTA) down to a certain degree (4), after which ad hoc low-degree polynomial
  57. * solving techniques [2] are used. The resulting algorithm, called BTZ, yields
  58. * much better performance than Chien search for usual (m,t) values (typically
  59. * m >= 13, t < 32, see [1]).
  60. *
  61. * [1] B. Biswas, V. Herbert. Efficient root finding of polynomials over fields
  62. * of characteristic 2, in: Western European Workshop on Research in Cryptology
  63. * - WEWoRC 2009, Graz, Austria, LNCS, Springer, July 2009, to appear.
  64. * [2] [Zin96] V.A. Zinoviev. On the solution of equations of degree 10 over
  65. * finite fields GF(2^q). In Rapport de recherche INRIA no 2829, 1996.
  66. */
  67. #include <linux/kernel.h>
  68. #include <linux/errno.h>
  69. #include <linux/init.h>
  70. #include <linux/module.h>
  71. #include <linux/slab.h>
  72. #include <linux/bitops.h>
  73. #include <asm/byteorder.h>
  74. #include <linux/bch.h>
  75. #if defined(CONFIG_BCH_CONST_PARAMS)
  76. #define GF_M(_p) (CONFIG_BCH_CONST_M)
  77. #define GF_T(_p) (CONFIG_BCH_CONST_T)
  78. #define GF_N(_p) ((1 << (CONFIG_BCH_CONST_M))-1)
  79. #else
  80. #define GF_M(_p) ((_p)->m)
  81. #define GF_T(_p) ((_p)->t)
  82. #define GF_N(_p) ((_p)->n)
  83. #endif
  84. #define BCH_ECC_WORDS(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 32)
  85. #define BCH_ECC_BYTES(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 8)
  86. #ifndef dbg
  87. #define dbg(_fmt, args...) do {} while (0)
  88. #endif
  89. /*
  90. * represent a polynomial over GF(2^m)
  91. */
  92. struct gf_poly {
  93. unsigned int deg; /* polynomial degree */
  94. unsigned int c[0]; /* polynomial terms */
  95. };
  96. /* given its degree, compute a polynomial size in bytes */
  97. #define GF_POLY_SZ(_d) (sizeof(struct gf_poly)+((_d)+1)*sizeof(unsigned int))
  98. /* polynomial of degree 1 */
  99. struct gf_poly_deg1 {
  100. struct gf_poly poly;
  101. unsigned int c[2];
  102. };
  103. /*
  104. * same as encode_bch(), but process input data one byte at a time
  105. */
  106. static void encode_bch_unaligned(struct bch_control *bch,
  107. const unsigned char *data, unsigned int len,
  108. uint32_t *ecc)
  109. {
  110. int i;
  111. const uint32_t *p;
  112. const int l = BCH_ECC_WORDS(bch)-1;
  113. while (len--) {
  114. p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(*data++)) & 0xff);
  115. for (i = 0; i < l; i++)
  116. ecc[i] = ((ecc[i] << 8)|(ecc[i+1] >> 24))^(*p++);
  117. ecc[l] = (ecc[l] << 8)^(*p);
  118. }
  119. }
  120. /*
  121. * convert ecc bytes to aligned, zero-padded 32-bit ecc words
  122. */
  123. static void load_ecc8(struct bch_control *bch, uint32_t *dst,
  124. const uint8_t *src)
  125. {
  126. uint8_t pad[4] = {0, 0, 0, 0};
  127. unsigned int i, nwords = BCH_ECC_WORDS(bch)-1;
  128. for (i = 0; i < nwords; i++, src += 4)
  129. dst[i] = (src[0] << 24)|(src[1] << 16)|(src[2] << 8)|src[3];
  130. memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords);
  131. dst[nwords] = (pad[0] << 24)|(pad[1] << 16)|(pad[2] << 8)|pad[3];
  132. }
  133. /*
  134. * convert 32-bit ecc words to ecc bytes
  135. */
  136. static void store_ecc8(struct bch_control *bch, uint8_t *dst,
  137. const uint32_t *src)
  138. {
  139. uint8_t pad[4];
  140. unsigned int i, nwords = BCH_ECC_WORDS(bch)-1;
  141. for (i = 0; i < nwords; i++) {
  142. *dst++ = (src[i] >> 24);
  143. *dst++ = (src[i] >> 16) & 0xff;
  144. *dst++ = (src[i] >> 8) & 0xff;
  145. *dst++ = (src[i] >> 0) & 0xff;
  146. }
  147. pad[0] = (src[nwords] >> 24);
  148. pad[1] = (src[nwords] >> 16) & 0xff;
  149. pad[2] = (src[nwords] >> 8) & 0xff;
  150. pad[3] = (src[nwords] >> 0) & 0xff;
  151. memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords);
  152. }
  153. /**
  154. * encode_bch - calculate BCH ecc parity of data
  155. * @bch: BCH control structure
  156. * @data: data to encode
  157. * @len: data length in bytes
  158. * @ecc: ecc parity data, must be initialized by caller
  159. *
  160. * The @ecc parity array is used both as input and output parameter, in order to
  161. * allow incremental computations. It should be of the size indicated by member
  162. * @ecc_bytes of @bch, and should be initialized to 0 before the first call.
  163. *
  164. * The exact number of computed ecc parity bits is given by member @ecc_bits of
  165. * @bch; it may be less than m*t for large values of t.
  166. */
  167. void encode_bch(struct bch_control *bch, const uint8_t *data,
  168. unsigned int len, uint8_t *ecc)
  169. {
  170. const unsigned int l = BCH_ECC_WORDS(bch)-1;
  171. unsigned int i, mlen;
  172. unsigned long m;
  173. uint32_t w, r[l+1];
  174. const uint32_t * const tab0 = bch->mod8_tab;
  175. const uint32_t * const tab1 = tab0 + 256*(l+1);
  176. const uint32_t * const tab2 = tab1 + 256*(l+1);
  177. const uint32_t * const tab3 = tab2 + 256*(l+1);
  178. const uint32_t *pdata, *p0, *p1, *p2, *p3;
  179. if (ecc) {
  180. /* load ecc parity bytes into internal 32-bit buffer */
  181. load_ecc8(bch, bch->ecc_buf, ecc);
  182. } else {
  183. memset(bch->ecc_buf, 0, sizeof(r));
  184. }
  185. /* process first unaligned data bytes */
  186. m = ((unsigned long)data) & 3;
  187. if (m) {
  188. mlen = (len < (4-m)) ? len : 4-m;
  189. encode_bch_unaligned(bch, data, mlen, bch->ecc_buf);
  190. data += mlen;
  191. len -= mlen;
  192. }
  193. /* process 32-bit aligned data words */
  194. pdata = (uint32_t *)data;
  195. mlen = len/4;
  196. data += 4*mlen;
  197. len -= 4*mlen;
  198. memcpy(r, bch->ecc_buf, sizeof(r));
  199. /*
  200. * split each 32-bit word into 4 polynomials of weight 8 as follows:
  201. *
  202. * 31 ...24 23 ...16 15 ... 8 7 ... 0
  203. * xxxxxxxx yyyyyyyy zzzzzzzz tttttttt
  204. * tttttttt mod g = r0 (precomputed)
  205. * zzzzzzzz 00000000 mod g = r1 (precomputed)
  206. * yyyyyyyy 00000000 00000000 mod g = r2 (precomputed)
  207. * xxxxxxxx 00000000 00000000 00000000 mod g = r3 (precomputed)
  208. * xxxxxxxx yyyyyyyy zzzzzzzz tttttttt mod g = r0^r1^r2^r3
  209. */
  210. while (mlen--) {
  211. /* input data is read in big-endian format */
  212. w = r[0]^cpu_to_be32(*pdata++);
  213. p0 = tab0 + (l+1)*((w >> 0) & 0xff);
  214. p1 = tab1 + (l+1)*((w >> 8) & 0xff);
  215. p2 = tab2 + (l+1)*((w >> 16) & 0xff);
  216. p3 = tab3 + (l+1)*((w >> 24) & 0xff);
  217. for (i = 0; i < l; i++)
  218. r[i] = r[i+1]^p0[i]^p1[i]^p2[i]^p3[i];
  219. r[l] = p0[l]^p1[l]^p2[l]^p3[l];
  220. }
  221. memcpy(bch->ecc_buf, r, sizeof(r));
  222. /* process last unaligned bytes */
  223. if (len)
  224. encode_bch_unaligned(bch, data, len, bch->ecc_buf);
  225. /* store ecc parity bytes into original parity buffer */
  226. if (ecc)
  227. store_ecc8(bch, ecc, bch->ecc_buf);
  228. }
  229. EXPORT_SYMBOL_GPL(encode_bch);
  230. static inline int modulo(struct bch_control *bch, unsigned int v)
  231. {
  232. const unsigned int n = GF_N(bch);
  233. while (v >= n) {
  234. v -= n;
  235. v = (v & n) + (v >> GF_M(bch));
  236. }
  237. return v;
  238. }
  239. /*
  240. * shorter and faster modulo function, only works when v < 2N.
  241. */
  242. static inline int mod_s(struct bch_control *bch, unsigned int v)
  243. {
  244. const unsigned int n = GF_N(bch);
  245. return (v < n) ? v : v-n;
  246. }
  247. static inline int deg(unsigned int poly)
  248. {
  249. /* polynomial degree is the most-significant bit index */
  250. return fls(poly)-1;
  251. }
  252. static inline int parity(unsigned int x)
  253. {
  254. /*
  255. * public domain code snippet, lifted from
  256. * http://www-graphics.stanford.edu/~seander/bithacks.html
  257. */
  258. x ^= x >> 1;
  259. x ^= x >> 2;
  260. x = (x & 0x11111111U) * 0x11111111U;
  261. return (x >> 28) & 1;
  262. }
  263. /* Galois field basic operations: multiply, divide, inverse, etc. */
  264. static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a,
  265. unsigned int b)
  266. {
  267. return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+
  268. bch->a_log_tab[b])] : 0;
  269. }
  270. static inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a)
  271. {
  272. return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0;
  273. }
  274. static inline unsigned int gf_div(struct bch_control *bch, unsigned int a,
  275. unsigned int b)
  276. {
  277. return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+
  278. GF_N(bch)-bch->a_log_tab[b])] : 0;
  279. }
  280. static inline unsigned int gf_inv(struct bch_control *bch, unsigned int a)
  281. {
  282. return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]];
  283. }
  284. static inline unsigned int a_pow(struct bch_control *bch, int i)
  285. {
  286. return bch->a_pow_tab[modulo(bch, i)];
  287. }
  288. static inline int a_log(struct bch_control *bch, unsigned int x)
  289. {
  290. return bch->a_log_tab[x];
  291. }
  292. static inline int a_ilog(struct bch_control *bch, unsigned int x)
  293. {
  294. return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]);
  295. }
  296. /*
  297. * compute 2t syndromes of ecc polynomial, i.e. ecc(a^j) for j=1..2t
  298. */
  299. static void compute_syndromes(struct bch_control *bch, uint32_t *ecc,
  300. unsigned int *syn)
  301. {
  302. int i, j, s;
  303. unsigned int m;
  304. uint32_t poly;
  305. const int t = GF_T(bch);
  306. s = bch->ecc_bits;
  307. /* make sure extra bits in last ecc word are cleared */
  308. m = ((unsigned int)s) & 31;
  309. if (m)
  310. ecc[s/32] &= ~((1u << (32-m))-1);
  311. memset(syn, 0, 2*t*sizeof(*syn));
  312. /* compute v(a^j) for j=1 .. 2t-1 */
  313. do {
  314. poly = *ecc++;
  315. s -= 32;
  316. while (poly) {
  317. i = deg(poly);
  318. for (j = 0; j < 2*t; j += 2)
  319. syn[j] ^= a_pow(bch, (j+1)*(i+s));
  320. poly ^= (1 << i);
  321. }
  322. } while (s > 0);
  323. /* v(a^(2j)) = v(a^j)^2 */
  324. for (j = 0; j < t; j++)
  325. syn[2*j+1] = gf_sqr(bch, syn[j]);
  326. }
  327. static void gf_poly_copy(struct gf_poly *dst, struct gf_poly *src)
  328. {
  329. memcpy(dst, src, GF_POLY_SZ(src->deg));
  330. }
  331. static int compute_error_locator_polynomial(struct bch_control *bch,
  332. const unsigned int *syn)
  333. {
  334. const unsigned int t = GF_T(bch);
  335. const unsigned int n = GF_N(bch);
  336. unsigned int i, j, tmp, l, pd = 1, d = syn[0];
  337. struct gf_poly *elp = bch->elp;
  338. struct gf_poly *pelp = bch->poly_2t[0];
  339. struct gf_poly *elp_copy = bch->poly_2t[1];
  340. int k, pp = -1;
  341. memset(pelp, 0, GF_POLY_SZ(2*t));
  342. memset(elp, 0, GF_POLY_SZ(2*t));
  343. pelp->deg = 0;
  344. pelp->c[0] = 1;
  345. elp->deg = 0;
  346. elp->c[0] = 1;
  347. /* use simplified binary Berlekamp-Massey algorithm */
  348. for (i = 0; (i < t) && (elp->deg <= t); i++) {
  349. if (d) {
  350. k = 2*i-pp;
  351. gf_poly_copy(elp_copy, elp);
  352. /* e[i+1](X) = e[i](X)+di*dp^-1*X^2(i-p)*e[p](X) */
  353. tmp = a_log(bch, d)+n-a_log(bch, pd);
  354. for (j = 0; j <= pelp->deg; j++) {
  355. if (pelp->c[j]) {
  356. l = a_log(bch, pelp->c[j]);
  357. elp->c[j+k] ^= a_pow(bch, tmp+l);
  358. }
  359. }
  360. /* compute l[i+1] = max(l[i]->c[l[p]+2*(i-p]) */
  361. tmp = pelp->deg+k;
  362. if (tmp > elp->deg) {
  363. elp->deg = tmp;
  364. gf_poly_copy(pelp, elp_copy);
  365. pd = d;
  366. pp = 2*i;
  367. }
  368. }
  369. /* di+1 = S(2i+3)+elp[i+1].1*S(2i+2)+...+elp[i+1].lS(2i+3-l) */
  370. if (i < t-1) {
  371. d = syn[2*i+2];
  372. for (j = 1; j <= elp->deg; j++)
  373. d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]);
  374. }
  375. }
  376. dbg("elp=%s\n", gf_poly_str(elp));
  377. return (elp->deg > t) ? -1 : (int)elp->deg;
  378. }
  379. /*
  380. * solve a m x m linear system in GF(2) with an expected number of solutions,
  381. * and return the number of found solutions
  382. */
  383. static int solve_linear_system(struct bch_control *bch, unsigned int *rows,
  384. unsigned int *sol, int nsol)
  385. {
  386. const int m = GF_M(bch);
  387. unsigned int tmp, mask;
  388. int rem, c, r, p, k, param[m];
  389. k = 0;
  390. mask = 1 << m;
  391. /* Gaussian elimination */
  392. for (c = 0; c < m; c++) {
  393. rem = 0;
  394. p = c-k;
  395. /* find suitable row for elimination */
  396. for (r = p; r < m; r++) {
  397. if (rows[r] & mask) {
  398. if (r != p) {
  399. tmp = rows[r];
  400. rows[r] = rows[p];
  401. rows[p] = tmp;
  402. }
  403. rem = r+1;
  404. break;
  405. }
  406. }
  407. if (rem) {
  408. /* perform elimination on remaining rows */
  409. tmp = rows[p];
  410. for (r = rem; r < m; r++) {
  411. if (rows[r] & mask)
  412. rows[r] ^= tmp;
  413. }
  414. } else {
  415. /* elimination not needed, store defective row index */
  416. param[k++] = c;
  417. }
  418. mask >>= 1;
  419. }
  420. /* rewrite system, inserting fake parameter rows */
  421. if (k > 0) {
  422. p = k;
  423. for (r = m-1; r >= 0; r--) {
  424. if ((r > m-1-k) && rows[r])
  425. /* system has no solution */
  426. return 0;
  427. rows[r] = (p && (r == param[p-1])) ?
  428. p--, 1u << (m-r) : rows[r-p];
  429. }
  430. }
  431. if (nsol != (1 << k))
  432. /* unexpected number of solutions */
  433. return 0;
  434. for (p = 0; p < nsol; p++) {
  435. /* set parameters for p-th solution */
  436. for (c = 0; c < k; c++)
  437. rows[param[c]] = (rows[param[c]] & ~1)|((p >> c) & 1);
  438. /* compute unique solution */
  439. tmp = 0;
  440. for (r = m-1; r >= 0; r--) {
  441. mask = rows[r] & (tmp|1);
  442. tmp |= parity(mask) << (m-r);
  443. }
  444. sol[p] = tmp >> 1;
  445. }
  446. return nsol;
  447. }
  448. /*
  449. * this function builds and solves a linear system for finding roots of a degree
  450. * 4 affine monic polynomial X^4+aX^2+bX+c over GF(2^m).
  451. */
  452. static int find_affine4_roots(struct bch_control *bch, unsigned int a,
  453. unsigned int b, unsigned int c,
  454. unsigned int *roots)
  455. {
  456. int i, j, k;
  457. const int m = GF_M(bch);
  458. unsigned int mask = 0xff, t, rows[16] = {0,};
  459. j = a_log(bch, b);
  460. k = a_log(bch, a);
  461. rows[0] = c;
  462. /* buid linear system to solve X^4+aX^2+bX+c = 0 */
  463. for (i = 0; i < m; i++) {
  464. rows[i+1] = bch->a_pow_tab[4*i]^
  465. (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^
  466. (b ? bch->a_pow_tab[mod_s(bch, j)] : 0);
  467. j++;
  468. k += 2;
  469. }
  470. /*
  471. * transpose 16x16 matrix before passing it to linear solver
  472. * warning: this code assumes m < 16
  473. */
  474. for (j = 8; j != 0; j >>= 1, mask ^= (mask << j)) {
  475. for (k = 0; k < 16; k = (k+j+1) & ~j) {
  476. t = ((rows[k] >> j)^rows[k+j]) & mask;
  477. rows[k] ^= (t << j);
  478. rows[k+j] ^= t;
  479. }
  480. }
  481. return solve_linear_system(bch, rows, roots, 4);
  482. }
  483. /*
  484. * compute root r of a degree 1 polynomial over GF(2^m) (returned as log(1/r))
  485. */
  486. static int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly,
  487. unsigned int *roots)
  488. {
  489. int n = 0;
  490. if (poly->c[0])
  491. /* poly[X] = bX+c with c!=0, root=c/b */
  492. roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+
  493. bch->a_log_tab[poly->c[1]]);
  494. return n;
  495. }
  496. /*
  497. * compute roots of a degree 2 polynomial over GF(2^m)
  498. */
  499. static int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly,
  500. unsigned int *roots)
  501. {
  502. int n = 0, i, l0, l1, l2;
  503. unsigned int u, v, r;
  504. if (poly->c[0] && poly->c[1]) {
  505. l0 = bch->a_log_tab[poly->c[0]];
  506. l1 = bch->a_log_tab[poly->c[1]];
  507. l2 = bch->a_log_tab[poly->c[2]];
  508. /* using z=a/bX, transform aX^2+bX+c into z^2+z+u (u=ac/b^2) */
  509. u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1));
  510. /*
  511. * let u = sum(li.a^i) i=0..m-1; then compute r = sum(li.xi):
  512. * r^2+r = sum(li.(xi^2+xi)) = sum(li.(a^i+Tr(a^i).a^k)) =
  513. * u + sum(li.Tr(a^i).a^k) = u+a^k.Tr(sum(li.a^i)) = u+a^k.Tr(u)
  514. * i.e. r and r+1 are roots iff Tr(u)=0
  515. */
  516. r = 0;
  517. v = u;
  518. while (v) {
  519. i = deg(v);
  520. r ^= bch->xi_tab[i];
  521. v ^= (1 << i);
  522. }
  523. /* verify root */
  524. if ((gf_sqr(bch, r)^r) == u) {
  525. /* reverse z=a/bX transformation and compute log(1/r) */
  526. roots[n++] = modulo(bch, 2*GF_N(bch)-l1-
  527. bch->a_log_tab[r]+l2);
  528. roots[n++] = modulo(bch, 2*GF_N(bch)-l1-
  529. bch->a_log_tab[r^1]+l2);
  530. }
  531. }
  532. return n;
  533. }
  534. /*
  535. * compute roots of a degree 3 polynomial over GF(2^m)
  536. */
  537. static int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly,
  538. unsigned int *roots)
  539. {
  540. int i, n = 0;
  541. unsigned int a, b, c, a2, b2, c2, e3, tmp[4];
  542. if (poly->c[0]) {
  543. /* transform polynomial into monic X^3 + a2X^2 + b2X + c2 */
  544. e3 = poly->c[3];
  545. c2 = gf_div(bch, poly->c[0], e3);
  546. b2 = gf_div(bch, poly->c[1], e3);
  547. a2 = gf_div(bch, poly->c[2], e3);
  548. /* (X+a2)(X^3+a2X^2+b2X+c2) = X^4+aX^2+bX+c (affine) */
  549. c = gf_mul(bch, a2, c2); /* c = a2c2 */
  550. b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */
  551. a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */
  552. /* find the 4 roots of this affine polynomial */
  553. if (find_affine4_roots(bch, a, b, c, tmp) == 4) {
  554. /* remove a2 from final list of roots */
  555. for (i = 0; i < 4; i++) {
  556. if (tmp[i] != a2)
  557. roots[n++] = a_ilog(bch, tmp[i]);
  558. }
  559. }
  560. }
  561. return n;
  562. }
  563. /*
  564. * compute roots of a degree 4 polynomial over GF(2^m)
  565. */
  566. static int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly,
  567. unsigned int *roots)
  568. {
  569. int i, l, n = 0;
  570. unsigned int a, b, c, d, e = 0, f, a2, b2, c2, e4;
  571. if (poly->c[0] == 0)
  572. return 0;
  573. /* transform polynomial into monic X^4 + aX^3 + bX^2 + cX + d */
  574. e4 = poly->c[4];
  575. d = gf_div(bch, poly->c[0], e4);
  576. c = gf_div(bch, poly->c[1], e4);
  577. b = gf_div(bch, poly->c[2], e4);
  578. a = gf_div(bch, poly->c[3], e4);
  579. /* use Y=1/X transformation to get an affine polynomial */
  580. if (a) {
  581. /* first, eliminate cX by using z=X+e with ae^2+c=0 */
  582. if (c) {
  583. /* compute e such that e^2 = c/a */
  584. f = gf_div(bch, c, a);
  585. l = a_log(bch, f);
  586. l += (l & 1) ? GF_N(bch) : 0;
  587. e = a_pow(bch, l/2);
  588. /*
  589. * use transformation z=X+e:
  590. * z^4+e^4 + a(z^3+ez^2+e^2z+e^3) + b(z^2+e^2) +cz+ce+d
  591. * z^4 + az^3 + (ae+b)z^2 + (ae^2+c)z+e^4+be^2+ae^3+ce+d
  592. * z^4 + az^3 + (ae+b)z^2 + e^4+be^2+d
  593. * z^4 + az^3 + b'z^2 + d'
  594. */
  595. d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d;
  596. b = gf_mul(bch, a, e)^b;
  597. }
  598. /* now, use Y=1/X to get Y^4 + b/dY^2 + a/dY + 1/d */
  599. if (d == 0)
  600. /* assume all roots have multiplicity 1 */
  601. return 0;
  602. c2 = gf_inv(bch, d);
  603. b2 = gf_div(bch, a, d);
  604. a2 = gf_div(bch, b, d);
  605. } else {
  606. /* polynomial is already affine */
  607. c2 = d;
  608. b2 = c;
  609. a2 = b;
  610. }
  611. /* find the 4 roots of this affine polynomial */
  612. if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) {
  613. for (i = 0; i < 4; i++) {
  614. /* post-process roots (reverse transformations) */
  615. f = a ? gf_inv(bch, roots[i]) : roots[i];
  616. roots[i] = a_ilog(bch, f^e);
  617. }
  618. n = 4;
  619. }
  620. return n;
  621. }
  622. /*
  623. * build monic, log-based representation of a polynomial
  624. */
  625. static void gf_poly_logrep(struct bch_control *bch,
  626. const struct gf_poly *a, int *rep)
  627. {
  628. int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]);
  629. /* represent 0 values with -1; warning, rep[d] is not set to 1 */
  630. for (i = 0; i < d; i++)
  631. rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1;
  632. }
  633. /*
  634. * compute polynomial Euclidean division remainder in GF(2^m)[X]
  635. */
  636. static void gf_poly_mod(struct bch_control *bch, struct gf_poly *a,
  637. const struct gf_poly *b, int *rep)
  638. {
  639. int la, p, m;
  640. unsigned int i, j, *c = a->c;
  641. const unsigned int d = b->deg;
  642. if (a->deg < d)
  643. return;
  644. /* reuse or compute log representation of denominator */
  645. if (!rep) {
  646. rep = bch->cache;
  647. gf_poly_logrep(bch, b, rep);
  648. }
  649. for (j = a->deg; j >= d; j--) {
  650. if (c[j]) {
  651. la = a_log(bch, c[j]);
  652. p = j-d;
  653. for (i = 0; i < d; i++, p++) {
  654. m = rep[i];
  655. if (m >= 0)
  656. c[p] ^= bch->a_pow_tab[mod_s(bch,
  657. m+la)];
  658. }
  659. }
  660. }
  661. a->deg = d-1;
  662. while (!c[a->deg] && a->deg)
  663. a->deg--;
  664. }
  665. /*
  666. * compute polynomial Euclidean division quotient in GF(2^m)[X]
  667. */
  668. static void gf_poly_div(struct bch_control *bch, struct gf_poly *a,
  669. const struct gf_poly *b, struct gf_poly *q)
  670. {
  671. if (a->deg >= b->deg) {
  672. q->deg = a->deg-b->deg;
  673. /* compute a mod b (modifies a) */
  674. gf_poly_mod(bch, a, b, NULL);
  675. /* quotient is stored in upper part of polynomial a */
  676. memcpy(q->c, &a->c[b->deg], (1+q->deg)*sizeof(unsigned int));
  677. } else {
  678. q->deg = 0;
  679. q->c[0] = 0;
  680. }
  681. }
  682. /*
  683. * compute polynomial GCD (Greatest Common Divisor) in GF(2^m)[X]
  684. */
  685. static struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a,
  686. struct gf_poly *b)
  687. {
  688. struct gf_poly *tmp;
  689. dbg("gcd(%s,%s)=", gf_poly_str(a), gf_poly_str(b));
  690. if (a->deg < b->deg) {
  691. tmp = b;
  692. b = a;
  693. a = tmp;
  694. }
  695. while (b->deg > 0) {
  696. gf_poly_mod(bch, a, b, NULL);
  697. tmp = b;
  698. b = a;
  699. a = tmp;
  700. }
  701. dbg("%s\n", gf_poly_str(a));
  702. return a;
  703. }
  704. /*
  705. * Given a polynomial f and an integer k, compute Tr(a^kX) mod f
  706. * This is used in Berlekamp Trace algorithm for splitting polynomials
  707. */
  708. static void compute_trace_bk_mod(struct bch_control *bch, int k,
  709. const struct gf_poly *f, struct gf_poly *z,
  710. struct gf_poly *out)
  711. {
  712. const int m = GF_M(bch);
  713. int i, j;
  714. /* z contains z^2j mod f */
  715. z->deg = 1;
  716. z->c[0] = 0;
  717. z->c[1] = bch->a_pow_tab[k];
  718. out->deg = 0;
  719. memset(out, 0, GF_POLY_SZ(f->deg));
  720. /* compute f log representation only once */
  721. gf_poly_logrep(bch, f, bch->cache);
  722. for (i = 0; i < m; i++) {
  723. /* add a^(k*2^i)(z^(2^i) mod f) and compute (z^(2^i) mod f)^2 */
  724. for (j = z->deg; j >= 0; j--) {
  725. out->c[j] ^= z->c[j];
  726. z->c[2*j] = gf_sqr(bch, z->c[j]);
  727. z->c[2*j+1] = 0;
  728. }
  729. if (z->deg > out->deg)
  730. out->deg = z->deg;
  731. if (i < m-1) {
  732. z->deg *= 2;
  733. /* z^(2(i+1)) mod f = (z^(2^i) mod f)^2 mod f */
  734. gf_poly_mod(bch, z, f, bch->cache);
  735. }
  736. }
  737. while (!out->c[out->deg] && out->deg)
  738. out->deg--;
  739. dbg("Tr(a^%d.X) mod f = %s\n", k, gf_poly_str(out));
  740. }
  741. /*
  742. * factor a polynomial using Berlekamp Trace algorithm (BTA)
  743. */
  744. static void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f,
  745. struct gf_poly **g, struct gf_poly **h)
  746. {
  747. struct gf_poly *f2 = bch->poly_2t[0];
  748. struct gf_poly *q = bch->poly_2t[1];
  749. struct gf_poly *tk = bch->poly_2t[2];
  750. struct gf_poly *z = bch->poly_2t[3];
  751. struct gf_poly *gcd;
  752. dbg("factoring %s...\n", gf_poly_str(f));
  753. *g = f;
  754. *h = NULL;
  755. /* tk = Tr(a^k.X) mod f */
  756. compute_trace_bk_mod(bch, k, f, z, tk);
  757. if (tk->deg > 0) {
  758. /* compute g = gcd(f, tk) (destructive operation) */
  759. gf_poly_copy(f2, f);
  760. gcd = gf_poly_gcd(bch, f2, tk);
  761. if (gcd->deg < f->deg) {
  762. /* compute h=f/gcd(f,tk); this will modify f and q */
  763. gf_poly_div(bch, f, gcd, q);
  764. /* store g and h in-place (clobbering f) */
  765. *h = &((struct gf_poly_deg1 *)f)[gcd->deg].poly;
  766. gf_poly_copy(*g, gcd);
  767. gf_poly_copy(*h, q);
  768. }
  769. }
  770. }
  771. /*
  772. * find roots of a polynomial, using BTZ algorithm; see the beginning of this
  773. * file for details
  774. */
  775. static int find_poly_roots(struct bch_control *bch, unsigned int k,
  776. struct gf_poly *poly, unsigned int *roots)
  777. {
  778. int cnt;
  779. struct gf_poly *f1, *f2;
  780. switch (poly->deg) {
  781. /* handle low degree polynomials with ad hoc techniques */
  782. case 1:
  783. cnt = find_poly_deg1_roots(bch, poly, roots);
  784. break;
  785. case 2:
  786. cnt = find_poly_deg2_roots(bch, poly, roots);
  787. break;
  788. case 3:
  789. cnt = find_poly_deg3_roots(bch, poly, roots);
  790. break;
  791. case 4:
  792. cnt = find_poly_deg4_roots(bch, poly, roots);
  793. break;
  794. default:
  795. /* factor polynomial using Berlekamp Trace Algorithm (BTA) */
  796. cnt = 0;
  797. if (poly->deg && (k <= GF_M(bch))) {
  798. factor_polynomial(bch, k, poly, &f1, &f2);
  799. if (f1)
  800. cnt += find_poly_roots(bch, k+1, f1, roots);
  801. if (f2)
  802. cnt += find_poly_roots(bch, k+1, f2, roots+cnt);
  803. }
  804. break;
  805. }
  806. return cnt;
  807. }
  808. #if defined(USE_CHIEN_SEARCH)
  809. /*
  810. * exhaustive root search (Chien) implementation - not used, included only for
  811. * reference/comparison tests
  812. */
  813. static int chien_search(struct bch_control *bch, unsigned int len,
  814. struct gf_poly *p, unsigned int *roots)
  815. {
  816. int m;
  817. unsigned int i, j, syn, syn0, count = 0;
  818. const unsigned int k = 8*len+bch->ecc_bits;
  819. /* use a log-based representation of polynomial */
  820. gf_poly_logrep(bch, p, bch->cache);
  821. bch->cache[p->deg] = 0;
  822. syn0 = gf_div(bch, p->c[0], p->c[p->deg]);
  823. for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) {
  824. /* compute elp(a^i) */
  825. for (j = 1, syn = syn0; j <= p->deg; j++) {
  826. m = bch->cache[j];
  827. if (m >= 0)
  828. syn ^= a_pow(bch, m+j*i);
  829. }
  830. if (syn == 0) {
  831. roots[count++] = GF_N(bch)-i;
  832. if (count == p->deg)
  833. break;
  834. }
  835. }
  836. return (count == p->deg) ? count : 0;
  837. }
  838. #define find_poly_roots(_p, _k, _elp, _loc) chien_search(_p, len, _elp, _loc)
  839. #endif /* USE_CHIEN_SEARCH */
  840. /**
  841. * decode_bch - decode received codeword and find bit error locations
  842. * @bch: BCH control structure
  843. * @data: received data, ignored if @calc_ecc is provided
  844. * @len: data length in bytes, must always be provided
  845. * @recv_ecc: received ecc, if NULL then assume it was XORed in @calc_ecc
  846. * @calc_ecc: calculated ecc, if NULL then calc_ecc is computed from @data
  847. * @syn: hw computed syndrome data (if NULL, syndrome is calculated)
  848. * @errloc: output array of error locations
  849. *
  850. * Returns:
  851. * The number of errors found, or -EBADMSG if decoding failed, or -EINVAL if
  852. * invalid parameters were provided
  853. *
  854. * Depending on the available hw BCH support and the need to compute @calc_ecc
  855. * separately (using encode_bch()), this function should be called with one of
  856. * the following parameter configurations -
  857. *
  858. * by providing @data and @recv_ecc only:
  859. * decode_bch(@bch, @data, @len, @recv_ecc, NULL, NULL, @errloc)
  860. *
  861. * by providing @recv_ecc and @calc_ecc:
  862. * decode_bch(@bch, NULL, @len, @recv_ecc, @calc_ecc, NULL, @errloc)
  863. *
  864. * by providing ecc = recv_ecc XOR calc_ecc:
  865. * decode_bch(@bch, NULL, @len, NULL, ecc, NULL, @errloc)
  866. *
  867. * by providing syndrome results @syn:
  868. * decode_bch(@bch, NULL, @len, NULL, NULL, @syn, @errloc)
  869. *
  870. * Once decode_bch() has successfully returned with a positive value, error
  871. * locations returned in array @errloc should be interpreted as follows -
  872. *
  873. * if (errloc[n] >= 8*len), then n-th error is located in ecc (no need for
  874. * data correction)
  875. *
  876. * if (errloc[n] < 8*len), then n-th error is located in data and can be
  877. * corrected with statement data[errloc[n]/8] ^= 1 << (errloc[n] % 8);
  878. *
  879. * Note that this function does not perform any data correction by itself, it
  880. * merely indicates error locations.
  881. */
  882. int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len,
  883. const uint8_t *recv_ecc, const uint8_t *calc_ecc,
  884. const unsigned int *syn, unsigned int *errloc)
  885. {
  886. const unsigned int ecc_words = BCH_ECC_WORDS(bch);
  887. unsigned int nbits;
  888. int i, err, nroots;
  889. uint32_t sum;
  890. /* sanity check: make sure data length can be handled */
  891. if (8*len > (bch->n-bch->ecc_bits))
  892. return -EINVAL;
  893. /* if caller does not provide syndromes, compute them */
  894. if (!syn) {
  895. if (!calc_ecc) {
  896. /* compute received data ecc into an internal buffer */
  897. if (!data || !recv_ecc)
  898. return -EINVAL;
  899. encode_bch(bch, data, len, NULL);
  900. } else {
  901. /* load provided calculated ecc */
  902. load_ecc8(bch, bch->ecc_buf, calc_ecc);
  903. }
  904. /* load received ecc or assume it was XORed in calc_ecc */
  905. if (recv_ecc) {
  906. load_ecc8(bch, bch->ecc_buf2, recv_ecc);
  907. /* XOR received and calculated ecc */
  908. for (i = 0, sum = 0; i < (int)ecc_words; i++) {
  909. bch->ecc_buf[i] ^= bch->ecc_buf2[i];
  910. sum |= bch->ecc_buf[i];
  911. }
  912. if (!sum)
  913. /* no error found */
  914. return 0;
  915. }
  916. compute_syndromes(bch, bch->ecc_buf, bch->syn);
  917. syn = bch->syn;
  918. }
  919. err = compute_error_locator_polynomial(bch, syn);
  920. if (err > 0) {
  921. nroots = find_poly_roots(bch, 1, bch->elp, errloc);
  922. if (err != nroots)
  923. err = -1;
  924. }
  925. if (err > 0) {
  926. /* post-process raw error locations for easier correction */
  927. nbits = (len*8)+bch->ecc_bits;
  928. for (i = 0; i < err; i++) {
  929. if (errloc[i] >= nbits) {
  930. err = -1;
  931. break;
  932. }
  933. errloc[i] = nbits-1-errloc[i];
  934. errloc[i] = (errloc[i] & ~7)|(7-(errloc[i] & 7));
  935. }
  936. }
  937. return (err >= 0) ? err : -EBADMSG;
  938. }
  939. EXPORT_SYMBOL_GPL(decode_bch);
  940. /*
  941. * generate Galois field lookup tables
  942. */
  943. static int build_gf_tables(struct bch_control *bch, unsigned int poly)
  944. {
  945. unsigned int i, x = 1;
  946. const unsigned int k = 1 << deg(poly);
  947. /* primitive polynomial must be of degree m */
  948. if (k != (1u << GF_M(bch)))
  949. return -1;
  950. for (i = 0; i < GF_N(bch); i++) {
  951. bch->a_pow_tab[i] = x;
  952. bch->a_log_tab[x] = i;
  953. if (i && (x == 1))
  954. /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
  955. return -1;
  956. x <<= 1;
  957. if (x & k)
  958. x ^= poly;
  959. }
  960. bch->a_pow_tab[GF_N(bch)] = 1;
  961. bch->a_log_tab[0] = 0;
  962. return 0;
  963. }
  964. /*
  965. * compute generator polynomial remainder tables for fast encoding
  966. */
  967. static void build_mod8_tables(struct bch_control *bch, const uint32_t *g)
  968. {
  969. int i, j, b, d;
  970. uint32_t data, hi, lo, *tab;
  971. const int l = BCH_ECC_WORDS(bch);
  972. const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32);
  973. const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32);
  974. memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab));
  975. for (i = 0; i < 256; i++) {
  976. /* p(X)=i is a small polynomial of weight <= 8 */
  977. for (b = 0; b < 4; b++) {
  978. /* we want to compute (p(X).X^(8*b+deg(g))) mod g(X) */
  979. tab = bch->mod8_tab + (b*256+i)*l;
  980. data = i << (8*b);
  981. while (data) {
  982. d = deg(data);
  983. /* subtract X^d.g(X) from p(X).X^(8*b+deg(g)) */
  984. data ^= g[0] >> (31-d);
  985. for (j = 0; j < ecclen; j++) {
  986. hi = (d < 31) ? g[j] << (d+1) : 0;
  987. lo = (j+1 < plen) ?
  988. g[j+1] >> (31-d) : 0;
  989. tab[j] ^= hi|lo;
  990. }
  991. }
  992. }
  993. }
  994. }
  995. /*
  996. * build a base for factoring degree 2 polynomials
  997. */
  998. static int build_deg2_base(struct bch_control *bch)
  999. {
  1000. const int m = GF_M(bch);
  1001. int i, j, r;
  1002. unsigned int sum, x, y, remaining, ak = 0, xi[m];
  1003. /* find k s.t. Tr(a^k) = 1 and 0 <= k < m */
  1004. for (i = 0; i < m; i++) {
  1005. for (j = 0, sum = 0; j < m; j++)
  1006. sum ^= a_pow(bch, i*(1 << j));
  1007. if (sum) {
  1008. ak = bch->a_pow_tab[i];
  1009. break;
  1010. }
  1011. }
  1012. /* find xi, i=0..m-1 such that xi^2+xi = a^i+Tr(a^i).a^k */
  1013. remaining = m;
  1014. memset(xi, 0, sizeof(xi));
  1015. for (x = 0; (x <= GF_N(bch)) && remaining; x++) {
  1016. y = gf_sqr(bch, x)^x;
  1017. for (i = 0; i < 2; i++) {
  1018. r = a_log(bch, y);
  1019. if (y && (r < m) && !xi[r]) {
  1020. bch->xi_tab[r] = x;
  1021. xi[r] = 1;
  1022. remaining--;
  1023. dbg("x%d = %x\n", r, x);
  1024. break;
  1025. }
  1026. y ^= ak;
  1027. }
  1028. }
  1029. /* should not happen but check anyway */
  1030. return remaining ? -1 : 0;
  1031. }
  1032. static void *bch_alloc(size_t size, int *err)
  1033. {
  1034. void *ptr;
  1035. ptr = kmalloc(size, GFP_KERNEL);
  1036. if (ptr == NULL)
  1037. *err = 1;
  1038. return ptr;
  1039. }
  1040. /*
  1041. * compute generator polynomial for given (m,t) parameters.
  1042. */
  1043. static uint32_t *compute_generator_polynomial(struct bch_control *bch)
  1044. {
  1045. const unsigned int m = GF_M(bch);
  1046. const unsigned int t = GF_T(bch);
  1047. int n, err = 0;
  1048. unsigned int i, j, nbits, r, word, *roots;
  1049. struct gf_poly *g;
  1050. uint32_t *genpoly;
  1051. g = bch_alloc(GF_POLY_SZ(m*t), &err);
  1052. roots = bch_alloc((bch->n+1)*sizeof(*roots), &err);
  1053. genpoly = bch_alloc(DIV_ROUND_UP(m*t+1, 32)*sizeof(*genpoly), &err);
  1054. if (err) {
  1055. kfree(genpoly);
  1056. genpoly = NULL;
  1057. goto finish;
  1058. }
  1059. /* enumerate all roots of g(X) */
  1060. memset(roots , 0, (bch->n+1)*sizeof(*roots));
  1061. for (i = 0; i < t; i++) {
  1062. for (j = 0, r = 2*i+1; j < m; j++) {
  1063. roots[r] = 1;
  1064. r = mod_s(bch, 2*r);
  1065. }
  1066. }
  1067. /* build generator polynomial g(X) */
  1068. g->deg = 0;
  1069. g->c[0] = 1;
  1070. for (i = 0; i < GF_N(bch); i++) {
  1071. if (roots[i]) {
  1072. /* multiply g(X) by (X+root) */
  1073. r = bch->a_pow_tab[i];
  1074. g->c[g->deg+1] = 1;
  1075. for (j = g->deg; j > 0; j--)
  1076. g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1];
  1077. g->c[0] = gf_mul(bch, g->c[0], r);
  1078. g->deg++;
  1079. }
  1080. }
  1081. /* store left-justified binary representation of g(X) */
  1082. n = g->deg+1;
  1083. i = 0;
  1084. while (n > 0) {
  1085. nbits = (n > 32) ? 32 : n;
  1086. for (j = 0, word = 0; j < nbits; j++) {
  1087. if (g->c[n-1-j])
  1088. word |= 1u << (31-j);
  1089. }
  1090. genpoly[i++] = word;
  1091. n -= nbits;
  1092. }
  1093. bch->ecc_bits = g->deg;
  1094. finish:
  1095. kfree(g);
  1096. kfree(roots);
  1097. return genpoly;
  1098. }
  1099. /**
  1100. * init_bch - initialize a BCH encoder/decoder
  1101. * @m: Galois field order, should be in the range 5-15
  1102. * @t: maximum error correction capability, in bits
  1103. * @prim_poly: user-provided primitive polynomial (or 0 to use default)
  1104. *
  1105. * Returns:
  1106. * a newly allocated BCH control structure if successful, NULL otherwise
  1107. *
  1108. * This initialization can take some time, as lookup tables are built for fast
  1109. * encoding/decoding; make sure not to call this function from a time critical
  1110. * path. Usually, init_bch() should be called on module/driver init and
  1111. * free_bch() should be called to release memory on exit.
  1112. *
  1113. * You may provide your own primitive polynomial of degree @m in argument
  1114. * @prim_poly, or let init_bch() use its default polynomial.
  1115. *
  1116. * Once init_bch() has successfully returned a pointer to a newly allocated
  1117. * BCH control structure, ecc length in bytes is given by member @ecc_bytes of
  1118. * the structure.
  1119. */
  1120. struct bch_control *init_bch(int m, int t, unsigned int prim_poly)
  1121. {
  1122. int err = 0;
  1123. unsigned int i, words;
  1124. uint32_t *genpoly;
  1125. struct bch_control *bch = NULL;
  1126. const int min_m = 5;
  1127. const int max_m = 15;
  1128. /* default primitive polynomials */
  1129. static const unsigned int prim_poly_tab[] = {
  1130. 0x25, 0x43, 0x83, 0x11d, 0x211, 0x409, 0x805, 0x1053, 0x201b,
  1131. 0x402b, 0x8003,
  1132. };
  1133. #if defined(CONFIG_BCH_CONST_PARAMS)
  1134. if ((m != (CONFIG_BCH_CONST_M)) || (t != (CONFIG_BCH_CONST_T))) {
  1135. printk(KERN_ERR "bch encoder/decoder was configured to support "
  1136. "parameters m=%d, t=%d only!\n",
  1137. CONFIG_BCH_CONST_M, CONFIG_BCH_CONST_T);
  1138. goto fail;
  1139. }
  1140. #endif
  1141. if ((m < min_m) || (m > max_m))
  1142. /*
  1143. * values of m greater than 15 are not currently supported;
  1144. * supporting m > 15 would require changing table base type
  1145. * (uint16_t) and a small patch in matrix transposition
  1146. */
  1147. goto fail;
  1148. /* sanity checks */
  1149. if ((t < 1) || (m*t >= ((1 << m)-1)))
  1150. /* invalid t value */
  1151. goto fail;
  1152. /* select a primitive polynomial for generating GF(2^m) */
  1153. if (prim_poly == 0)
  1154. prim_poly = prim_poly_tab[m-min_m];
  1155. bch = kzalloc(sizeof(*bch), GFP_KERNEL);
  1156. if (bch == NULL)
  1157. goto fail;
  1158. bch->m = m;
  1159. bch->t = t;
  1160. bch->n = (1 << m)-1;
  1161. words = DIV_ROUND_UP(m*t, 32);
  1162. bch->ecc_bytes = DIV_ROUND_UP(m*t, 8);
  1163. bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err);
  1164. bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err);
  1165. bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err);
  1166. bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err);
  1167. bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err);
  1168. bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err);
  1169. bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err);
  1170. bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err);
  1171. bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err);
  1172. for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++)
  1173. bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err);
  1174. if (err)
  1175. goto fail;
  1176. err = build_gf_tables(bch, prim_poly);
  1177. if (err)
  1178. goto fail;
  1179. /* use generator polynomial for computing encoding tables */
  1180. genpoly = compute_generator_polynomial(bch);
  1181. if (genpoly == NULL)
  1182. goto fail;
  1183. build_mod8_tables(bch, genpoly);
  1184. kfree(genpoly);
  1185. err = build_deg2_base(bch);
  1186. if (err)
  1187. goto fail;
  1188. return bch;
  1189. fail:
  1190. free_bch(bch);
  1191. return NULL;
  1192. }
  1193. EXPORT_SYMBOL_GPL(init_bch);
  1194. /**
  1195. * free_bch - free the BCH control structure
  1196. * @bch: BCH control structure to release
  1197. */
  1198. void free_bch(struct bch_control *bch)
  1199. {
  1200. unsigned int i;
  1201. if (bch) {
  1202. kfree(bch->a_pow_tab);
  1203. kfree(bch->a_log_tab);
  1204. kfree(bch->mod8_tab);
  1205. kfree(bch->ecc_buf);
  1206. kfree(bch->ecc_buf2);
  1207. kfree(bch->xi_tab);
  1208. kfree(bch->syn);
  1209. kfree(bch->cache);
  1210. kfree(bch->elp);
  1211. for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++)
  1212. kfree(bch->poly_2t[i]);
  1213. kfree(bch);
  1214. }
  1215. }
  1216. EXPORT_SYMBOL_GPL(free_bch);
  1217. MODULE_LICENSE("GPL");
  1218. MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>");
  1219. MODULE_DESCRIPTION("Binary BCH encoder/decoder");