bch.c 35 KB

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