bch.c 36 KB

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