bch.c 36 KB

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