bch.c 35 KB

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