bch.c 35 KB

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