Lines Matching full:bch
2 * Generic binary BCH encoding/decoding library
13 * Bose-Chaudhuri-Hocquenghem (BCH) codes.
22 * On systems supporting hw BCH features, intermediate results may be provided
29 * (m,t) are fixed and known in advance, e.g. when using BCH error correction
83 #include <linux/bch.h>
155 static void encode_bch_unaligned(struct bch_control *bch, in encode_bch_unaligned() argument
161 const int l = BCH_ECC_WORDS(bch)-1; in encode_bch_unaligned()
164 p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(*data++)) & 0xff); in encode_bch_unaligned()
176 static void load_ecc8(struct bch_control *bch, uint32_t *dst, in load_ecc8() argument
180 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in load_ecc8()
185 memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords); in load_ecc8()
192 static void store_ecc8(struct bch_control *bch, uint8_t *dst, in store_ecc8() argument
196 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in store_ecc8()
208 memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords); in store_ecc8()
212 * encode_bch - calculate BCH ecc parity of data
213 * @bch: BCH control structure
220 * @ecc_bytes of @bch, and should be initialized to 0 before the first call.
223 * @bch; it may be less than m*t for large values of t.
225 void encode_bch(struct bch_control *bch, const uint8_t *data, in encode_bch() argument
228 const unsigned int l = BCH_ECC_WORDS(bch)-1; in encode_bch()
232 const uint32_t * const tab0 = bch->mod8_tab; in encode_bch()
240 load_ecc8(bch, bch->ecc_buf, ecc); in encode_bch()
242 memset(bch->ecc_buf, 0, sizeof(r)); in encode_bch()
249 encode_bch_unaligned(bch, data, mlen, bch->ecc_buf); in encode_bch()
259 memcpy(r, bch->ecc_buf, sizeof(r)); in encode_bch()
285 memcpy(bch->ecc_buf, r, sizeof(r)); in encode_bch()
289 encode_bch_unaligned(bch, data, len, bch->ecc_buf); in encode_bch()
293 store_ecc8(bch, ecc, bch->ecc_buf); in encode_bch()
296 static inline int modulo(struct bch_control *bch, unsigned int v) in modulo() argument
298 const unsigned int n = GF_N(bch); in modulo()
301 v = (v & n) + (v >> GF_M(bch)); in modulo()
309 static inline int mod_s(struct bch_control *bch, unsigned int v) in mod_s() argument
311 const unsigned int n = GF_N(bch); in mod_s()
335 static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a, in gf_mul() argument
338 return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_mul()
339 bch->a_log_tab[b])] : 0; in gf_mul()
342 static inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a) in gf_sqr() argument
344 return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0; in gf_sqr()
347 static inline unsigned int gf_div(struct bch_control *bch, unsigned int a, in gf_div() argument
350 return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_div()
351 GF_N(bch)-bch->a_log_tab[b])] : 0; in gf_div()
354 static inline unsigned int gf_inv(struct bch_control *bch, unsigned int a) in gf_inv() argument
356 return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]]; in gf_inv()
359 static inline unsigned int a_pow(struct bch_control *bch, int i) in a_pow() argument
361 return bch->a_pow_tab[modulo(bch, i)]; in a_pow()
364 static inline int a_log(struct bch_control *bch, unsigned int x) in a_log() argument
366 return bch->a_log_tab[x]; in a_log()
369 static inline int a_ilog(struct bch_control *bch, unsigned int x) in a_ilog() argument
371 return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]); in a_ilog()
377 static void compute_syndromes(struct bch_control *bch, uint32_t *ecc, in compute_syndromes() argument
383 const int t = GF_T(bch); in compute_syndromes()
385 s = bch->ecc_bits; in compute_syndromes()
400 syn[j] ^= a_pow(bch, (j+1)*(i+s)); in compute_syndromes()
408 syn[2*j+1] = gf_sqr(bch, syn[j]); in compute_syndromes()
416 static int compute_error_locator_polynomial(struct bch_control *bch, in compute_error_locator_polynomial() argument
419 const unsigned int t = GF_T(bch); in compute_error_locator_polynomial()
420 const unsigned int n = GF_N(bch); in compute_error_locator_polynomial()
422 struct gf_poly *elp = bch->elp; in compute_error_locator_polynomial()
423 struct gf_poly *pelp = bch->poly_2t[0]; in compute_error_locator_polynomial()
424 struct gf_poly *elp_copy = bch->poly_2t[1]; in compute_error_locator_polynomial()
441 tmp = a_log(bch, d)+n-a_log(bch, pd); in compute_error_locator_polynomial()
444 l = a_log(bch, pelp->c[j]); in compute_error_locator_polynomial()
445 elp->c[j+k] ^= a_pow(bch, tmp+l); in compute_error_locator_polynomial()
461 d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]); in compute_error_locator_polynomial()
472 static int solve_linear_system(struct bch_control *bch, unsigned int *rows, in solve_linear_system() argument
475 const int m = GF_M(bch); in solve_linear_system()
548 static int find_affine4_roots(struct bch_control *bch, unsigned int a, in find_affine4_roots() argument
553 const int m = GF_M(bch); in find_affine4_roots()
556 j = a_log(bch, b); in find_affine4_roots()
557 k = a_log(bch, a); in find_affine4_roots()
562 rows[i+1] = bch->a_pow_tab[4*i]^ in find_affine4_roots()
563 (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^ in find_affine4_roots()
564 (b ? bch->a_pow_tab[mod_s(bch, j)] : 0); in find_affine4_roots()
579 return solve_linear_system(bch, rows, roots, 4); in find_affine4_roots()
585 static int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg1_roots() argument
592 roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+ in find_poly_deg1_roots()
593 bch->a_log_tab[poly->c[1]]); in find_poly_deg1_roots()
600 static int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg2_roots() argument
608 l0 = bch->a_log_tab[poly->c[0]]; in find_poly_deg2_roots()
609 l1 = bch->a_log_tab[poly->c[1]]; in find_poly_deg2_roots()
610 l2 = bch->a_log_tab[poly->c[2]]; in find_poly_deg2_roots()
613 u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1)); in find_poly_deg2_roots()
624 r ^= bch->xi_tab[i]; in find_poly_deg2_roots()
628 if ((gf_sqr(bch, r)^r) == u) { in find_poly_deg2_roots()
630 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
631 bch->a_log_tab[r]+l2); in find_poly_deg2_roots()
632 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
633 bch->a_log_tab[r^1]+l2); in find_poly_deg2_roots()
642 static int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg3_roots() argument
651 c2 = gf_div(bch, poly->c[0], e3); in find_poly_deg3_roots()
652 b2 = gf_div(bch, poly->c[1], e3); in find_poly_deg3_roots()
653 a2 = gf_div(bch, poly->c[2], e3); in find_poly_deg3_roots()
656 c = gf_mul(bch, a2, c2); /* c = a2c2 */ in find_poly_deg3_roots()
657 b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */ in find_poly_deg3_roots()
658 a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */ in find_poly_deg3_roots()
661 if (find_affine4_roots(bch, a, b, c, tmp) == 4) { in find_poly_deg3_roots()
665 roots[n++] = a_ilog(bch, tmp[i]); in find_poly_deg3_roots()
675 static int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg4_roots() argument
686 d = gf_div(bch, poly->c[0], e4); in find_poly_deg4_roots()
687 c = gf_div(bch, poly->c[1], e4); in find_poly_deg4_roots()
688 b = gf_div(bch, poly->c[2], e4); in find_poly_deg4_roots()
689 a = gf_div(bch, poly->c[3], e4); in find_poly_deg4_roots()
696 f = gf_div(bch, c, a); in find_poly_deg4_roots()
697 l = a_log(bch, f); in find_poly_deg4_roots()
698 l += (l & 1) ? GF_N(bch) : 0; in find_poly_deg4_roots()
699 e = a_pow(bch, l/2); in find_poly_deg4_roots()
707 d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d; in find_poly_deg4_roots()
708 b = gf_mul(bch, a, e)^b; in find_poly_deg4_roots()
715 c2 = gf_inv(bch, d); in find_poly_deg4_roots()
716 b2 = gf_div(bch, a, d); in find_poly_deg4_roots()
717 a2 = gf_div(bch, b, d); in find_poly_deg4_roots()
725 if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) { in find_poly_deg4_roots()
728 f = a ? gf_inv(bch, roots[i]) : roots[i]; in find_poly_deg4_roots()
729 roots[i] = a_ilog(bch, f^e); in find_poly_deg4_roots()
739 static void gf_poly_logrep(struct bch_control *bch, in gf_poly_logrep() argument
742 int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]); in gf_poly_logrep()
746 rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1; in gf_poly_logrep()
752 static void gf_poly_mod(struct bch_control *bch, struct gf_poly *a, in gf_poly_mod() argument
764 rep = bch->cache; in gf_poly_mod()
765 gf_poly_logrep(bch, b, rep); in gf_poly_mod()
770 la = a_log(bch, c[j]); in gf_poly_mod()
775 c[p] ^= bch->a_pow_tab[mod_s(bch, in gf_poly_mod()
788 static void gf_poly_div(struct bch_control *bch, struct gf_poly *a, in gf_poly_div() argument
794 gf_poly_mod(bch, a, b, NULL); in gf_poly_div()
806 static struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a, in gf_poly_gcd() argument
820 gf_poly_mod(bch, a, b, NULL); in gf_poly_gcd()
835 static void compute_trace_bk_mod(struct bch_control *bch, int k, in compute_trace_bk_mod() argument
839 const int m = GF_M(bch); in compute_trace_bk_mod()
845 z->c[1] = bch->a_pow_tab[k]; in compute_trace_bk_mod()
851 gf_poly_logrep(bch, f, bch->cache); in compute_trace_bk_mod()
857 z->c[2*j] = gf_sqr(bch, z->c[j]); in compute_trace_bk_mod()
866 gf_poly_mod(bch, z, f, bch->cache); in compute_trace_bk_mod()
878 static void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f, in factor_polynomial() argument
881 struct gf_poly *f2 = bch->poly_2t[0]; in factor_polynomial()
882 struct gf_poly *q = bch->poly_2t[1]; in factor_polynomial()
883 struct gf_poly *tk = bch->poly_2t[2]; in factor_polynomial()
884 struct gf_poly *z = bch->poly_2t[3]; in factor_polynomial()
893 compute_trace_bk_mod(bch, k, f, z, tk); in factor_polynomial()
898 gcd = gf_poly_gcd(bch, f2, tk); in factor_polynomial()
901 gf_poly_div(bch, f, gcd, q); in factor_polynomial()
914 static int find_poly_roots(struct bch_control *bch, unsigned int k, in find_poly_roots() argument
923 cnt = find_poly_deg1_roots(bch, poly, roots); in find_poly_roots()
926 cnt = find_poly_deg2_roots(bch, poly, roots); in find_poly_roots()
929 cnt = find_poly_deg3_roots(bch, poly, roots); in find_poly_roots()
932 cnt = find_poly_deg4_roots(bch, poly, roots); in find_poly_roots()
937 if (poly->deg && (k <= GF_M(bch))) { in find_poly_roots()
938 factor_polynomial(bch, k, poly, &f1, &f2); in find_poly_roots()
940 cnt += find_poly_roots(bch, k+1, f1, roots); in find_poly_roots()
942 cnt += find_poly_roots(bch, k+1, f2, roots+cnt); in find_poly_roots()
954 static int chien_search(struct bch_control *bch, unsigned int len, in chien_search() argument
959 const unsigned int k = 8*len+bch->ecc_bits; in chien_search()
962 gf_poly_logrep(bch, p, bch->cache); in chien_search()
963 bch->cache[p->deg] = 0; in chien_search()
964 syn0 = gf_div(bch, p->c[0], p->c[p->deg]); in chien_search()
966 for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) { in chien_search()
969 m = bch->cache[j]; in chien_search()
971 syn ^= a_pow(bch, m+j*i); in chien_search()
974 roots[count++] = GF_N(bch)-i; in chien_search()
986 * @bch: BCH control structure
998 * Depending on the available hw BCH support and the need to compute @calc_ecc
1003 * decode_bch(@bch, @data, @len, @recv_ecc, NULL, NULL, @errloc)
1006 * decode_bch(@bch, NULL, @len, @recv_ecc, @calc_ecc, NULL, @errloc)
1009 * decode_bch(@bch, NULL, @len, NULL, ecc, NULL, @errloc)
1012 * decode_bch(@bch, NULL, @len, NULL, NULL, @syn, @errloc)
1026 int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len, in decode_bch() argument
1030 const unsigned int ecc_words = BCH_ECC_WORDS(bch); in decode_bch()
1036 if (8*len > (bch->n-bch->ecc_bits)) in decode_bch()
1045 encode_bch(bch, data, len, NULL); in decode_bch()
1048 load_ecc8(bch, bch->ecc_buf, calc_ecc); in decode_bch()
1052 load_ecc8(bch, bch->ecc_buf2, recv_ecc); in decode_bch()
1055 bch->ecc_buf[i] ^= bch->ecc_buf2[i]; in decode_bch()
1056 sum |= bch->ecc_buf[i]; in decode_bch()
1062 compute_syndromes(bch, bch->ecc_buf, bch->syn); in decode_bch()
1063 syn = bch->syn; in decode_bch()
1066 err = compute_error_locator_polynomial(bch, syn); in decode_bch()
1068 nroots = find_poly_roots(bch, 1, bch->elp, errloc); in decode_bch()
1074 nbits = (len*8)+bch->ecc_bits; in decode_bch()
1090 static int build_gf_tables(struct bch_control *bch, unsigned int poly) in build_gf_tables() argument
1096 if (k != (1u << GF_M(bch))) in build_gf_tables()
1099 for (i = 0; i < GF_N(bch); i++) { in build_gf_tables()
1100 bch->a_pow_tab[i] = x; in build_gf_tables()
1101 bch->a_log_tab[x] = i; in build_gf_tables()
1109 bch->a_pow_tab[GF_N(bch)] = 1; in build_gf_tables()
1110 bch->a_log_tab[0] = 0; in build_gf_tables()
1118 static void build_mod8_tables(struct bch_control *bch, const uint32_t *g) in build_mod8_tables() argument
1122 const int l = BCH_ECC_WORDS(bch); in build_mod8_tables()
1123 const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32); in build_mod8_tables()
1124 const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32); in build_mod8_tables()
1126 memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab)); in build_mod8_tables()
1132 tab = bch->mod8_tab + (b*256+i)*l; in build_mod8_tables()
1152 static int build_deg2_base(struct bch_control *bch) in build_deg2_base() argument
1154 const int m = GF_M(bch); in build_deg2_base()
1161 sum ^= a_pow(bch, i*(1 << j)); in build_deg2_base()
1164 ak = bch->a_pow_tab[i]; in build_deg2_base()
1172 for (x = 0; (x <= GF_N(bch)) && remaining; x++) { in build_deg2_base()
1173 y = gf_sqr(bch, x)^x; in build_deg2_base()
1175 r = a_log(bch, y); in build_deg2_base()
1177 bch->xi_tab[r] = x; in build_deg2_base()
1203 static uint32_t *compute_generator_polynomial(struct bch_control *bch) in compute_generator_polynomial() argument
1205 const unsigned int m = GF_M(bch); in compute_generator_polynomial()
1206 const unsigned int t = GF_T(bch); in compute_generator_polynomial()
1213 roots = bch_alloc((bch->n+1)*sizeof(*roots), &err); in compute_generator_polynomial()
1223 memset(roots , 0, (bch->n+1)*sizeof(*roots)); in compute_generator_polynomial()
1227 r = mod_s(bch, 2*r); in compute_generator_polynomial()
1233 for (i = 0; i < GF_N(bch); i++) { in compute_generator_polynomial()
1236 r = bch->a_pow_tab[i]; in compute_generator_polynomial()
1239 g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1]; in compute_generator_polynomial()
1241 g->c[0] = gf_mul(bch, g->c[0], r); in compute_generator_polynomial()
1258 bch->ecc_bits = g->deg; in compute_generator_polynomial()
1268 * init_bch - initialize a BCH encoder/decoder
1274 * a newly allocated BCH control structure if successful, NULL otherwise
1285 * BCH control structure, ecc length in bytes is given by member @ecc_bytes of
1293 struct bch_control *bch = NULL; in init_bch() local
1306 printk(KERN_ERR "bch encoder/decoder was configured to support " in init_bch()
1329 bch = kzalloc(sizeof(*bch), GFP_KERNEL); in init_bch()
1330 if (bch == NULL) in init_bch()
1333 bch->m = m; in init_bch()
1334 bch->t = t; in init_bch()
1335 bch->n = (1 << m)-1; in init_bch()
1337 bch->ecc_bytes = DIV_ROUND_UP(m*t, 8); in init_bch()
1338 bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err); in init_bch()
1339 bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err); in init_bch()
1340 bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err); in init_bch()
1341 bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err); in init_bch()
1342 bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err); in init_bch()
1343 bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err); in init_bch()
1344 bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err); in init_bch()
1345 bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err); in init_bch()
1346 bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err); in init_bch()
1348 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in init_bch()
1349 bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err); in init_bch()
1354 err = build_gf_tables(bch, prim_poly); in init_bch()
1359 genpoly = compute_generator_polynomial(bch); in init_bch()
1363 build_mod8_tables(bch, genpoly); in init_bch()
1366 err = build_deg2_base(bch); in init_bch()
1370 return bch; in init_bch()
1373 free_bch(bch); in init_bch()
1378 * free_bch - free the BCH control structure
1379 * @bch: BCH control structure to release
1381 void free_bch(struct bch_control *bch) in free_bch() argument
1385 if (bch) { in free_bch()
1386 kfree(bch->a_pow_tab); in free_bch()
1387 kfree(bch->a_log_tab); in free_bch()
1388 kfree(bch->mod8_tab); in free_bch()
1389 kfree(bch->ecc_buf); in free_bch()
1390 kfree(bch->ecc_buf2); in free_bch()
1391 kfree(bch->xi_tab); in free_bch()
1392 kfree(bch->syn); in free_bch()
1393 kfree(bch->cache); in free_bch()
1394 kfree(bch->elp); in free_bch()
1396 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in free_bch()
1397 kfree(bch->poly_2t[i]); in free_bch()
1399 kfree(bch); in free_bch()