bch.c 38 KB

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