bch.c 35 KB

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