bch.c 36 KB

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