bch.c 37 KB

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