bch.c 35 KB

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