bch.c 36 KB

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