Lines Matching full:bch
3 * Generic binary BCH encoding/decoding library
12 * Bose-Chaudhuri-Hocquenghem (BCH) codes.
21 * On systems supporting hw BCH features, intermediate results may be provided
28 * (m,t) are fixed and known in advance, e.g. when using BCH error correction
89 #include <linux/bch.h>
161 static void encode_bch_unaligned(struct bch_control *bch, in encode_bch_unaligned() argument
167 const int l = BCH_ECC_WORDS(bch)-1; in encode_bch_unaligned()
170 p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(*data++)) & 0xff); in encode_bch_unaligned()
182 static void load_ecc8(struct bch_control *bch, uint32_t *dst, in load_ecc8() argument
186 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in load_ecc8()
191 memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords); in load_ecc8()
198 static void store_ecc8(struct bch_control *bch, uint8_t *dst, in store_ecc8() argument
202 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in store_ecc8()
214 memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords); in store_ecc8()
218 * encode_bch - calculate BCH ecc parity of data
219 * @bch: BCH control structure
226 * @ecc_bytes of @bch, and should be initialized to 0 before the first call.
229 * @bch; it may be less than m*t for large values of t.
231 void encode_bch(struct bch_control *bch, const uint8_t *data, in encode_bch() argument
234 const unsigned int l = BCH_ECC_WORDS(bch)-1; in encode_bch()
238 const uint32_t * const tab0 = bch->mod8_tab; in encode_bch()
246 load_ecc8(bch, bch->ecc_buf, ecc); in encode_bch()
248 memset(bch->ecc_buf, 0, sizeof(r)); in encode_bch()
255 encode_bch_unaligned(bch, data, mlen, bch->ecc_buf); in encode_bch()
265 memcpy(r, bch->ecc_buf, sizeof(r)); in encode_bch()
291 memcpy(bch->ecc_buf, r, sizeof(r)); in encode_bch()
295 encode_bch_unaligned(bch, data, len, bch->ecc_buf); in encode_bch()
299 store_ecc8(bch, ecc, bch->ecc_buf); in encode_bch()
302 static inline int modulo(struct bch_control *bch, unsigned int v) in modulo() argument
304 const unsigned int n = GF_N(bch); in modulo()
307 v = (v & n) + (v >> GF_M(bch)); in modulo()
315 static inline int mod_s(struct bch_control *bch, unsigned int v) in mod_s() argument
317 const unsigned int n = GF_N(bch); in mod_s()
341 static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a, in gf_mul() argument
344 return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_mul()
345 bch->a_log_tab[b])] : 0; in gf_mul()
348 static inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a) in gf_sqr() argument
350 return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0; in gf_sqr()
353 static inline unsigned int gf_div(struct bch_control *bch, unsigned int a, in gf_div() argument
356 return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_div()
357 GF_N(bch)-bch->a_log_tab[b])] : 0; in gf_div()
360 static inline unsigned int gf_inv(struct bch_control *bch, unsigned int a) in gf_inv() argument
362 return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]]; in gf_inv()
365 static inline unsigned int a_pow(struct bch_control *bch, int i) in a_pow() argument
367 return bch->a_pow_tab[modulo(bch, i)]; in a_pow()
370 static inline int a_log(struct bch_control *bch, unsigned int x) in a_log() argument
372 return bch->a_log_tab[x]; in a_log()
375 static inline int a_ilog(struct bch_control *bch, unsigned int x) in a_ilog() argument
377 return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]); in a_ilog()
383 static void compute_syndromes(struct bch_control *bch, uint32_t *ecc, in compute_syndromes() argument
389 const int t = GF_T(bch); in compute_syndromes()
391 s = bch->ecc_bits; in compute_syndromes()
406 syn[j] ^= a_pow(bch, (j+1)*(i+s)); in compute_syndromes()
414 syn[2*j+1] = gf_sqr(bch, syn[j]); in compute_syndromes()
422 static int compute_error_locator_polynomial(struct bch_control *bch, in compute_error_locator_polynomial() argument
425 const unsigned int t = GF_T(bch); in compute_error_locator_polynomial()
426 const unsigned int n = GF_N(bch); in compute_error_locator_polynomial()
428 struct gf_poly *elp = bch->elp; in compute_error_locator_polynomial()
429 struct gf_poly *pelp = bch->poly_2t[0]; in compute_error_locator_polynomial()
430 struct gf_poly *elp_copy = bch->poly_2t[1]; in compute_error_locator_polynomial()
447 tmp = a_log(bch, d)+n-a_log(bch, pd); in compute_error_locator_polynomial()
450 l = a_log(bch, pelp->c[j]); in compute_error_locator_polynomial()
451 elp->c[j+k] ^= a_pow(bch, tmp+l); in compute_error_locator_polynomial()
467 d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]); in compute_error_locator_polynomial()
478 static int solve_linear_system(struct bch_control *bch, unsigned int *rows, in solve_linear_system() argument
481 const int m = GF_M(bch); in solve_linear_system()
554 static int find_affine4_roots(struct bch_control *bch, unsigned int a, in find_affine4_roots() argument
559 const int m = GF_M(bch); in find_affine4_roots()
562 j = a_log(bch, b); in find_affine4_roots()
563 k = a_log(bch, a); in find_affine4_roots()
568 rows[i+1] = bch->a_pow_tab[4*i]^ in find_affine4_roots()
569 (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^ in find_affine4_roots()
570 (b ? bch->a_pow_tab[mod_s(bch, j)] : 0); in find_affine4_roots()
585 return solve_linear_system(bch, rows, roots, 4); in find_affine4_roots()
591 static int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg1_roots() argument
598 roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+ in find_poly_deg1_roots()
599 bch->a_log_tab[poly->c[1]]); in find_poly_deg1_roots()
606 static int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg2_roots() argument
614 l0 = bch->a_log_tab[poly->c[0]]; in find_poly_deg2_roots()
615 l1 = bch->a_log_tab[poly->c[1]]; in find_poly_deg2_roots()
616 l2 = bch->a_log_tab[poly->c[2]]; in find_poly_deg2_roots()
619 u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1)); in find_poly_deg2_roots()
630 r ^= bch->xi_tab[i]; in find_poly_deg2_roots()
634 if ((gf_sqr(bch, r)^r) == u) { in find_poly_deg2_roots()
636 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
637 bch->a_log_tab[r]+l2); in find_poly_deg2_roots()
638 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
639 bch->a_log_tab[r^1]+l2); in find_poly_deg2_roots()
648 static int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg3_roots() argument
657 c2 = gf_div(bch, poly->c[0], e3); in find_poly_deg3_roots()
658 b2 = gf_div(bch, poly->c[1], e3); in find_poly_deg3_roots()
659 a2 = gf_div(bch, poly->c[2], e3); in find_poly_deg3_roots()
662 c = gf_mul(bch, a2, c2); /* c = a2c2 */ in find_poly_deg3_roots()
663 b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */ in find_poly_deg3_roots()
664 a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */ in find_poly_deg3_roots()
667 if (find_affine4_roots(bch, a, b, c, tmp) == 4) { in find_poly_deg3_roots()
671 roots[n++] = a_ilog(bch, tmp[i]); in find_poly_deg3_roots()
681 static int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg4_roots() argument
692 d = gf_div(bch, poly->c[0], e4); in find_poly_deg4_roots()
693 c = gf_div(bch, poly->c[1], e4); in find_poly_deg4_roots()
694 b = gf_div(bch, poly->c[2], e4); in find_poly_deg4_roots()
695 a = gf_div(bch, poly->c[3], e4); in find_poly_deg4_roots()
702 f = gf_div(bch, c, a); in find_poly_deg4_roots()
703 l = a_log(bch, f); in find_poly_deg4_roots()
704 l += (l & 1) ? GF_N(bch) : 0; in find_poly_deg4_roots()
705 e = a_pow(bch, l/2); in find_poly_deg4_roots()
713 d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d; in find_poly_deg4_roots()
714 b = gf_mul(bch, a, e)^b; in find_poly_deg4_roots()
721 c2 = gf_inv(bch, d); in find_poly_deg4_roots()
722 b2 = gf_div(bch, a, d); in find_poly_deg4_roots()
723 a2 = gf_div(bch, b, d); in find_poly_deg4_roots()
731 if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) { in find_poly_deg4_roots()
734 f = a ? gf_inv(bch, roots[i]) : roots[i]; in find_poly_deg4_roots()
735 roots[i] = a_ilog(bch, f^e); in find_poly_deg4_roots()
745 static void gf_poly_logrep(struct bch_control *bch, in gf_poly_logrep() argument
748 int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]); in gf_poly_logrep()
752 rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1; in gf_poly_logrep()
758 static void gf_poly_mod(struct bch_control *bch, struct gf_poly *a, in gf_poly_mod() argument
770 rep = bch->cache; in gf_poly_mod()
771 gf_poly_logrep(bch, b, rep); in gf_poly_mod()
776 la = a_log(bch, c[j]); in gf_poly_mod()
781 c[p] ^= bch->a_pow_tab[mod_s(bch, in gf_poly_mod()
794 static void gf_poly_div(struct bch_control *bch, struct gf_poly *a, in gf_poly_div() argument
800 gf_poly_mod(bch, a, b, NULL); in gf_poly_div()
812 static struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a, in gf_poly_gcd() argument
826 gf_poly_mod(bch, a, b, NULL); in gf_poly_gcd()
841 static void compute_trace_bk_mod(struct bch_control *bch, int k, in compute_trace_bk_mod() argument
845 const int m = GF_M(bch); in compute_trace_bk_mod()
851 z->c[1] = bch->a_pow_tab[k]; in compute_trace_bk_mod()
857 gf_poly_logrep(bch, f, bch->cache); in compute_trace_bk_mod()
863 z->c[2*j] = gf_sqr(bch, z->c[j]); in compute_trace_bk_mod()
872 gf_poly_mod(bch, z, f, bch->cache); in compute_trace_bk_mod()
884 static void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f, in factor_polynomial() argument
887 struct gf_poly *f2 = bch->poly_2t[0]; in factor_polynomial()
888 struct gf_poly *q = bch->poly_2t[1]; in factor_polynomial()
889 struct gf_poly *tk = bch->poly_2t[2]; in factor_polynomial()
890 struct gf_poly *z = bch->poly_2t[3]; in factor_polynomial()
899 compute_trace_bk_mod(bch, k, f, z, tk); in factor_polynomial()
904 gcd = gf_poly_gcd(bch, f2, tk); in factor_polynomial()
907 gf_poly_div(bch, f, gcd, q); in factor_polynomial()
920 static int find_poly_roots(struct bch_control *bch, unsigned int k, in find_poly_roots() argument
929 cnt = find_poly_deg1_roots(bch, poly, roots); in find_poly_roots()
932 cnt = find_poly_deg2_roots(bch, poly, roots); in find_poly_roots()
935 cnt = find_poly_deg3_roots(bch, poly, roots); in find_poly_roots()
938 cnt = find_poly_deg4_roots(bch, poly, roots); in find_poly_roots()
943 if (poly->deg && (k <= GF_M(bch))) { in find_poly_roots()
944 factor_polynomial(bch, k, poly, &f1, &f2); in find_poly_roots()
946 cnt += find_poly_roots(bch, k+1, f1, roots); in find_poly_roots()
948 cnt += find_poly_roots(bch, k+1, f2, roots+cnt); in find_poly_roots()
960 static int chien_search(struct bch_control *bch, unsigned int len, in chien_search() argument
965 const unsigned int k = 8*len+bch->ecc_bits; in chien_search()
968 gf_poly_logrep(bch, p, bch->cache); in chien_search()
969 bch->cache[p->deg] = 0; in chien_search()
970 syn0 = gf_div(bch, p->c[0], p->c[p->deg]); in chien_search()
972 for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) { in chien_search()
975 m = bch->cache[j]; in chien_search()
977 syn ^= a_pow(bch, m+j*i); in chien_search()
980 roots[count++] = GF_N(bch)-i; in chien_search()
992 * @bch: BCH control structure
1004 * Depending on the available hw BCH support and the need to compute @calc_ecc
1009 * decode_bch(@bch, @data, @len, @recv_ecc, NULL, NULL, @errloc)
1012 * decode_bch(@bch, NULL, @len, @recv_ecc, @calc_ecc, NULL, @errloc)
1015 * decode_bch(@bch, NULL, @len, NULL, ecc, NULL, @errloc)
1018 * decode_bch(@bch, NULL, @len, NULL, NULL, @syn, @errloc)
1032 int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len, in decode_bch() argument
1036 const unsigned int ecc_words = BCH_ECC_WORDS(bch); in decode_bch()
1042 if (8*len > (bch->n-bch->ecc_bits)) in decode_bch()
1051 encode_bch(bch, data, len, NULL); in decode_bch()
1054 load_ecc8(bch, bch->ecc_buf, calc_ecc); in decode_bch()
1058 load_ecc8(bch, bch->ecc_buf2, recv_ecc); in decode_bch()
1061 bch->ecc_buf[i] ^= bch->ecc_buf2[i]; in decode_bch()
1062 sum |= bch->ecc_buf[i]; in decode_bch()
1068 compute_syndromes(bch, bch->ecc_buf, bch->syn); in decode_bch()
1069 syn = bch->syn; in decode_bch()
1072 err = compute_error_locator_polynomial(bch, syn); in decode_bch()
1074 nroots = find_poly_roots(bch, 1, bch->elp, errloc); in decode_bch()
1080 nbits = (len*8)+bch->ecc_bits; in decode_bch()
1096 static int build_gf_tables(struct bch_control *bch, unsigned int poly) in build_gf_tables() argument
1102 if (k != (1u << GF_M(bch))) in build_gf_tables()
1105 for (i = 0; i < GF_N(bch); i++) { in build_gf_tables()
1106 bch->a_pow_tab[i] = x; in build_gf_tables()
1107 bch->a_log_tab[x] = i; in build_gf_tables()
1115 bch->a_pow_tab[GF_N(bch)] = 1; in build_gf_tables()
1116 bch->a_log_tab[0] = 0; in build_gf_tables()
1124 static void build_mod8_tables(struct bch_control *bch, const uint32_t *g) in build_mod8_tables() argument
1128 const int l = BCH_ECC_WORDS(bch); in build_mod8_tables()
1129 const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32); in build_mod8_tables()
1130 const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32); in build_mod8_tables()
1132 memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab)); in build_mod8_tables()
1138 tab = bch->mod8_tab + (b*256+i)*l; in build_mod8_tables()
1158 static int build_deg2_base(struct bch_control *bch) in build_deg2_base() argument
1160 const int m = GF_M(bch); in build_deg2_base()
1167 sum ^= a_pow(bch, i*(1 << j)); in build_deg2_base()
1170 ak = bch->a_pow_tab[i]; in build_deg2_base()
1178 for (x = 0; (x <= GF_N(bch)) && remaining; x++) { in build_deg2_base()
1179 y = gf_sqr(bch, x)^x; in build_deg2_base()
1181 r = a_log(bch, y); in build_deg2_base()
1183 bch->xi_tab[r] = x; in build_deg2_base()
1209 static uint32_t *compute_generator_polynomial(struct bch_control *bch) in compute_generator_polynomial() argument
1211 const unsigned int m = GF_M(bch); in compute_generator_polynomial()
1212 const unsigned int t = GF_T(bch); in compute_generator_polynomial()
1219 roots = bch_alloc((bch->n+1)*sizeof(*roots), &err); in compute_generator_polynomial()
1229 memset(roots , 0, (bch->n+1)*sizeof(*roots)); in compute_generator_polynomial()
1233 r = mod_s(bch, 2*r); in compute_generator_polynomial()
1239 for (i = 0; i < GF_N(bch); i++) { in compute_generator_polynomial()
1242 r = bch->a_pow_tab[i]; in compute_generator_polynomial()
1245 g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1]; in compute_generator_polynomial()
1247 g->c[0] = gf_mul(bch, g->c[0], r); in compute_generator_polynomial()
1264 bch->ecc_bits = g->deg; in compute_generator_polynomial()
1274 * init_bch - initialize a BCH encoder/decoder
1280 * a newly allocated BCH control structure if successful, NULL otherwise
1291 * BCH control structure, ecc length in bytes is given by member @ecc_bytes of
1299 struct bch_control *bch = NULL; in init_bch() local
1312 printk(KERN_ERR "bch encoder/decoder was configured to support " in init_bch()
1335 bch = kzalloc(sizeof(*bch), GFP_KERNEL); in init_bch()
1336 if (bch == NULL) in init_bch()
1339 bch->m = m; in init_bch()
1340 bch->t = t; in init_bch()
1341 bch->n = (1 << m)-1; in init_bch()
1343 bch->ecc_bytes = DIV_ROUND_UP(m*t, 8); in init_bch()
1344 bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err); in init_bch()
1345 bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err); in init_bch()
1346 bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err); in init_bch()
1347 bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err); in init_bch()
1348 bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err); in init_bch()
1349 bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err); in init_bch()
1350 bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err); in init_bch()
1351 bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err); in init_bch()
1352 bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err); in init_bch()
1354 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in init_bch()
1355 bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err); in init_bch()
1360 err = build_gf_tables(bch, prim_poly); in init_bch()
1365 genpoly = compute_generator_polynomial(bch); in init_bch()
1369 build_mod8_tables(bch, genpoly); in init_bch()
1372 err = build_deg2_base(bch); in init_bch()
1376 return bch; in init_bch()
1379 free_bch(bch); in init_bch()
1384 * free_bch - free the BCH control structure
1385 * @bch: BCH control structure to release
1387 void free_bch(struct bch_control *bch) in free_bch() argument
1391 if (bch) { in free_bch()
1392 kfree(bch->a_pow_tab); in free_bch()
1393 kfree(bch->a_log_tab); in free_bch()
1394 kfree(bch->mod8_tab); in free_bch()
1395 kfree(bch->ecc_buf); in free_bch()
1396 kfree(bch->ecc_buf2); in free_bch()
1397 kfree(bch->xi_tab); in free_bch()
1398 kfree(bch->syn); in free_bch()
1399 kfree(bch->cache); in free_bch()
1400 kfree(bch->elp); in free_bch()
1402 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in free_bch()
1403 kfree(bch->poly_2t[i]); in free_bch()
1405 kfree(bch); in free_bch()