Lines Matching full:bch
2 * Generic binary BCH encoding/decoding library
24 * Bose-Chaudhuri-Hocquenghem (BCH) codes.
33 * On systems supporting hw BCH features, intermediate results may be provided
40 * (m,t) are fixed and known in advance, e.g. when using BCH error correction
75 #include <linux/bch.h>
120 static void encode_bch_unaligned(struct bch_control *bch, in encode_bch_unaligned() argument
126 const int l = BCH_ECC_WORDS(bch)-1; in encode_bch_unaligned()
129 p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(*data++)) & 0xff); in encode_bch_unaligned()
141 static void load_ecc8(struct bch_control *bch, uint32_t *dst, in load_ecc8() argument
145 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in load_ecc8()
150 memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords); in load_ecc8()
157 static void store_ecc8(struct bch_control *bch, uint8_t *dst, in store_ecc8() argument
161 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in store_ecc8()
173 memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords); in store_ecc8()
177 * encode_bch - calculate BCH ecc parity of data
178 * @bch: BCH control structure
185 * @ecc_bytes of @bch, and should be initialized to 0 before the first call.
188 * @bch; it may be less than m*t for large values of t.
190 void encode_bch(struct bch_control *bch, const uint8_t *data, in encode_bch() argument
193 const unsigned int l = BCH_ECC_WORDS(bch)-1; in encode_bch()
197 const size_t r_bytes = BCH_ECC_WORDS(bch) * sizeof(*r); in encode_bch()
198 const uint32_t * const tab0 = bch->mod8_tab; in encode_bch()
209 load_ecc8(bch, bch->ecc_buf, ecc); in encode_bch()
211 memset(bch->ecc_buf, 0, r_bytes); in encode_bch()
218 encode_bch_unaligned(bch, data, mlen, bch->ecc_buf); in encode_bch()
228 memcpy(r, bch->ecc_buf, r_bytes); in encode_bch()
254 memcpy(bch->ecc_buf, r, r_bytes); in encode_bch()
258 encode_bch_unaligned(bch, data, len, bch->ecc_buf); in encode_bch()
262 store_ecc8(bch, ecc, bch->ecc_buf); in encode_bch()
266 static inline int modulo(struct bch_control *bch, unsigned int v) in modulo() argument
268 const unsigned int n = GF_N(bch); in modulo()
271 v = (v & n) + (v >> GF_M(bch)); in modulo()
279 static inline int mod_s(struct bch_control *bch, unsigned int v) in mod_s() argument
281 const unsigned int n = GF_N(bch); in mod_s()
305 static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a, in gf_mul() argument
308 return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_mul()
309 bch->a_log_tab[b])] : 0; in gf_mul()
312 static inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a) in gf_sqr() argument
314 return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0; in gf_sqr()
317 static inline unsigned int gf_div(struct bch_control *bch, unsigned int a, in gf_div() argument
320 return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_div()
321 GF_N(bch)-bch->a_log_tab[b])] : 0; in gf_div()
324 static inline unsigned int gf_inv(struct bch_control *bch, unsigned int a) in gf_inv() argument
326 return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]]; in gf_inv()
329 static inline unsigned int a_pow(struct bch_control *bch, int i) in a_pow() argument
331 return bch->a_pow_tab[modulo(bch, i)]; in a_pow()
334 static inline int a_log(struct bch_control *bch, unsigned int x) in a_log() argument
336 return bch->a_log_tab[x]; in a_log()
339 static inline int a_ilog(struct bch_control *bch, unsigned int x) in a_ilog() argument
341 return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]); in a_ilog()
347 static void compute_syndromes(struct bch_control *bch, uint32_t *ecc, in compute_syndromes() argument
353 const int t = GF_T(bch); in compute_syndromes()
355 s = bch->ecc_bits; in compute_syndromes()
370 syn[j] ^= a_pow(bch, (j+1)*(i+s)); in compute_syndromes()
378 syn[2*j+1] = gf_sqr(bch, syn[j]); in compute_syndromes()
386 static int compute_error_locator_polynomial(struct bch_control *bch, in compute_error_locator_polynomial() argument
389 const unsigned int t = GF_T(bch); in compute_error_locator_polynomial()
390 const unsigned int n = GF_N(bch); in compute_error_locator_polynomial()
392 struct gf_poly *elp = bch->elp; in compute_error_locator_polynomial()
393 struct gf_poly *pelp = bch->poly_2t[0]; in compute_error_locator_polynomial()
394 struct gf_poly *elp_copy = bch->poly_2t[1]; in compute_error_locator_polynomial()
411 tmp = a_log(bch, d)+n-a_log(bch, pd); in compute_error_locator_polynomial()
414 l = a_log(bch, pelp->c[j]); in compute_error_locator_polynomial()
415 elp->c[j+k] ^= a_pow(bch, tmp+l); in compute_error_locator_polynomial()
431 d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]); in compute_error_locator_polynomial()
442 static int solve_linear_system(struct bch_control *bch, unsigned int *rows, in solve_linear_system() argument
445 const int m = GF_M(bch); in solve_linear_system()
518 static int find_affine4_roots(struct bch_control *bch, unsigned int a, in find_affine4_roots() argument
523 const int m = GF_M(bch); in find_affine4_roots()
526 j = a_log(bch, b); in find_affine4_roots()
527 k = a_log(bch, a); in find_affine4_roots()
532 rows[i+1] = bch->a_pow_tab[4*i]^ in find_affine4_roots()
533 (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^ in find_affine4_roots()
534 (b ? bch->a_pow_tab[mod_s(bch, j)] : 0); in find_affine4_roots()
549 return solve_linear_system(bch, rows, roots, 4); in find_affine4_roots()
555 static int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg1_roots() argument
562 roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+ in find_poly_deg1_roots()
563 bch->a_log_tab[poly->c[1]]); in find_poly_deg1_roots()
570 static int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg2_roots() argument
578 l0 = bch->a_log_tab[poly->c[0]]; in find_poly_deg2_roots()
579 l1 = bch->a_log_tab[poly->c[1]]; in find_poly_deg2_roots()
580 l2 = bch->a_log_tab[poly->c[2]]; in find_poly_deg2_roots()
583 u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1)); in find_poly_deg2_roots()
594 r ^= bch->xi_tab[i]; in find_poly_deg2_roots()
598 if ((gf_sqr(bch, r)^r) == u) { in find_poly_deg2_roots()
600 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
601 bch->a_log_tab[r]+l2); in find_poly_deg2_roots()
602 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
603 bch->a_log_tab[r^1]+l2); in find_poly_deg2_roots()
612 static int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg3_roots() argument
621 c2 = gf_div(bch, poly->c[0], e3); in find_poly_deg3_roots()
622 b2 = gf_div(bch, poly->c[1], e3); in find_poly_deg3_roots()
623 a2 = gf_div(bch, poly->c[2], e3); in find_poly_deg3_roots()
626 c = gf_mul(bch, a2, c2); /* c = a2c2 */ in find_poly_deg3_roots()
627 b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */ in find_poly_deg3_roots()
628 a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */ in find_poly_deg3_roots()
631 if (find_affine4_roots(bch, a, b, c, tmp) == 4) { in find_poly_deg3_roots()
635 roots[n++] = a_ilog(bch, tmp[i]); in find_poly_deg3_roots()
645 static int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg4_roots() argument
656 d = gf_div(bch, poly->c[0], e4); in find_poly_deg4_roots()
657 c = gf_div(bch, poly->c[1], e4); in find_poly_deg4_roots()
658 b = gf_div(bch, poly->c[2], e4); in find_poly_deg4_roots()
659 a = gf_div(bch, poly->c[3], e4); in find_poly_deg4_roots()
666 f = gf_div(bch, c, a); in find_poly_deg4_roots()
667 l = a_log(bch, f); in find_poly_deg4_roots()
668 l += (l & 1) ? GF_N(bch) : 0; in find_poly_deg4_roots()
669 e = a_pow(bch, l/2); in find_poly_deg4_roots()
677 d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d; in find_poly_deg4_roots()
678 b = gf_mul(bch, a, e)^b; in find_poly_deg4_roots()
685 c2 = gf_inv(bch, d); in find_poly_deg4_roots()
686 b2 = gf_div(bch, a, d); in find_poly_deg4_roots()
687 a2 = gf_div(bch, b, d); in find_poly_deg4_roots()
695 if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) { in find_poly_deg4_roots()
698 f = a ? gf_inv(bch, roots[i]) : roots[i]; in find_poly_deg4_roots()
699 roots[i] = a_ilog(bch, f^e); in find_poly_deg4_roots()
709 static void gf_poly_logrep(struct bch_control *bch, in gf_poly_logrep() argument
712 int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]); in gf_poly_logrep()
716 rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1; in gf_poly_logrep()
722 static void gf_poly_mod(struct bch_control *bch, struct gf_poly *a, in gf_poly_mod() argument
734 rep = bch->cache; in gf_poly_mod()
735 gf_poly_logrep(bch, b, rep); in gf_poly_mod()
740 la = a_log(bch, c[j]); in gf_poly_mod()
745 c[p] ^= bch->a_pow_tab[mod_s(bch, in gf_poly_mod()
758 static void gf_poly_div(struct bch_control *bch, struct gf_poly *a, in gf_poly_div() argument
764 gf_poly_mod(bch, a, b, NULL); in gf_poly_div()
776 static struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a, in gf_poly_gcd() argument
790 gf_poly_mod(bch, a, b, NULL); in gf_poly_gcd()
805 static void compute_trace_bk_mod(struct bch_control *bch, int k, in compute_trace_bk_mod() argument
809 const int m = GF_M(bch); in compute_trace_bk_mod()
815 z->c[1] = bch->a_pow_tab[k]; in compute_trace_bk_mod()
821 gf_poly_logrep(bch, f, bch->cache); in compute_trace_bk_mod()
827 z->c[2*j] = gf_sqr(bch, z->c[j]); in compute_trace_bk_mod()
836 gf_poly_mod(bch, z, f, bch->cache); in compute_trace_bk_mod()
848 static void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f, in factor_polynomial() argument
851 struct gf_poly *f2 = bch->poly_2t[0]; in factor_polynomial()
852 struct gf_poly *q = bch->poly_2t[1]; in factor_polynomial()
853 struct gf_poly *tk = bch->poly_2t[2]; in factor_polynomial()
854 struct gf_poly *z = bch->poly_2t[3]; in factor_polynomial()
863 compute_trace_bk_mod(bch, k, f, z, tk); in factor_polynomial()
868 gcd = gf_poly_gcd(bch, f2, tk); in factor_polynomial()
871 gf_poly_div(bch, f, gcd, q); in factor_polynomial()
884 static int find_poly_roots(struct bch_control *bch, unsigned int k, in find_poly_roots() argument
893 cnt = find_poly_deg1_roots(bch, poly, roots); in find_poly_roots()
896 cnt = find_poly_deg2_roots(bch, poly, roots); in find_poly_roots()
899 cnt = find_poly_deg3_roots(bch, poly, roots); in find_poly_roots()
902 cnt = find_poly_deg4_roots(bch, poly, roots); in find_poly_roots()
907 if (poly->deg && (k <= GF_M(bch))) { in find_poly_roots()
908 factor_polynomial(bch, k, poly, &f1, &f2); in find_poly_roots()
910 cnt += find_poly_roots(bch, k+1, f1, roots); in find_poly_roots()
912 cnt += find_poly_roots(bch, k+1, f2, roots+cnt); in find_poly_roots()
924 static int chien_search(struct bch_control *bch, unsigned int len, in chien_search() argument
929 const unsigned int k = 8*len+bch->ecc_bits; in chien_search()
932 gf_poly_logrep(bch, p, bch->cache); in chien_search()
933 bch->cache[p->deg] = 0; in chien_search()
934 syn0 = gf_div(bch, p->c[0], p->c[p->deg]); in chien_search()
936 for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) { in chien_search()
939 m = bch->cache[j]; in chien_search()
941 syn ^= a_pow(bch, m+j*i); in chien_search()
944 roots[count++] = GF_N(bch)-i; in chien_search()
956 * @bch: BCH control structure
968 * Depending on the available hw BCH support and the need to compute @calc_ecc
973 * decode_bch(@bch, @data, @len, @recv_ecc, NULL, NULL, @errloc)
976 * decode_bch(@bch, NULL, @len, @recv_ecc, @calc_ecc, NULL, @errloc)
979 * decode_bch(@bch, NULL, @len, NULL, ecc, NULL, @errloc)
982 * decode_bch(@bch, NULL, @len, NULL, NULL, @syn, @errloc)
996 int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len, in decode_bch() argument
1000 const unsigned int ecc_words = BCH_ECC_WORDS(bch); in decode_bch()
1006 if (8*len > (bch->n-bch->ecc_bits)) in decode_bch()
1015 encode_bch(bch, data, len, NULL); in decode_bch()
1018 load_ecc8(bch, bch->ecc_buf, calc_ecc); in decode_bch()
1022 load_ecc8(bch, bch->ecc_buf2, recv_ecc); in decode_bch()
1025 bch->ecc_buf[i] ^= bch->ecc_buf2[i]; in decode_bch()
1026 sum |= bch->ecc_buf[i]; in decode_bch()
1032 compute_syndromes(bch, bch->ecc_buf, bch->syn); in decode_bch()
1033 syn = bch->syn; in decode_bch()
1036 err = compute_error_locator_polynomial(bch, syn); in decode_bch()
1038 nroots = find_poly_roots(bch, 1, bch->elp, errloc); in decode_bch()
1044 nbits = (len*8)+bch->ecc_bits; in decode_bch()
1061 static int build_gf_tables(struct bch_control *bch, unsigned int poly) in build_gf_tables() argument
1067 if (k != (1u << GF_M(bch))) in build_gf_tables()
1070 for (i = 0; i < GF_N(bch); i++) { in build_gf_tables()
1071 bch->a_pow_tab[i] = x; in build_gf_tables()
1072 bch->a_log_tab[x] = i; in build_gf_tables()
1080 bch->a_pow_tab[GF_N(bch)] = 1; in build_gf_tables()
1081 bch->a_log_tab[0] = 0; in build_gf_tables()
1089 static void build_mod8_tables(struct bch_control *bch, const uint32_t *g) in build_mod8_tables() argument
1093 const int l = BCH_ECC_WORDS(bch); in build_mod8_tables()
1094 const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32); in build_mod8_tables()
1095 const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32); in build_mod8_tables()
1097 memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab)); in build_mod8_tables()
1103 tab = bch->mod8_tab + (b*256+i)*l; in build_mod8_tables()
1123 static int build_deg2_base(struct bch_control *bch) in build_deg2_base() argument
1125 const int m = GF_M(bch); in build_deg2_base()
1132 sum ^= a_pow(bch, i*(1 << j)); in build_deg2_base()
1135 ak = bch->a_pow_tab[i]; in build_deg2_base()
1143 for (x = 0; (x <= GF_N(bch)) && remaining; x++) { in build_deg2_base()
1144 y = gf_sqr(bch, x)^x; in build_deg2_base()
1146 r = a_log(bch, y); in build_deg2_base()
1148 bch->xi_tab[r] = x; in build_deg2_base()
1174 static uint32_t *compute_generator_polynomial(struct bch_control *bch) in compute_generator_polynomial() argument
1176 const unsigned int m = GF_M(bch); in compute_generator_polynomial()
1177 const unsigned int t = GF_T(bch); in compute_generator_polynomial()
1184 roots = bch_alloc((bch->n+1)*sizeof(*roots), &err); in compute_generator_polynomial()
1194 memset(roots , 0, (bch->n+1)*sizeof(*roots)); in compute_generator_polynomial()
1198 r = mod_s(bch, 2*r); in compute_generator_polynomial()
1204 for (i = 0; i < GF_N(bch); i++) { in compute_generator_polynomial()
1207 r = bch->a_pow_tab[i]; in compute_generator_polynomial()
1210 g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1]; in compute_generator_polynomial()
1212 g->c[0] = gf_mul(bch, g->c[0], r); in compute_generator_polynomial()
1229 bch->ecc_bits = g->deg; in compute_generator_polynomial()
1239 * init_bch - initialize a BCH encoder/decoder
1245 * a newly allocated BCH control structure if successful, NULL otherwise
1256 * BCH control structure, ecc length in bytes is given by member @ecc_bytes of
1264 struct bch_control *bch = NULL; in init_bch() local
1276 printk(KERN_ERR "bch encoder/decoder was configured to support " in init_bch()
1306 bch = kzalloc(sizeof(*bch), GFP_KERNEL); in init_bch()
1307 if (bch == NULL) in init_bch()
1310 bch->m = m; in init_bch()
1311 bch->t = t; in init_bch()
1312 bch->n = (1 << m)-1; in init_bch()
1314 bch->ecc_bytes = DIV_ROUND_UP(m*t, 8); in init_bch()
1315 bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err); in init_bch()
1316 bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err); in init_bch()
1317 bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err); in init_bch()
1318 bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err); in init_bch()
1319 bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err); in init_bch()
1320 bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err); in init_bch()
1321 bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err); in init_bch()
1322 bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err); in init_bch()
1323 bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err); in init_bch()
1325 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in init_bch()
1326 bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err); in init_bch()
1331 err = build_gf_tables(bch, prim_poly); in init_bch()
1336 genpoly = compute_generator_polynomial(bch); in init_bch()
1340 build_mod8_tables(bch, genpoly); in init_bch()
1343 err = build_deg2_base(bch); in init_bch()
1347 return bch; in init_bch()
1350 free_bch(bch); in init_bch()
1356 * free_bch - free the BCH control structure
1357 * @bch: BCH control structure to release
1359 void free_bch(struct bch_control *bch) in free_bch() argument
1363 if (bch) { in free_bch()
1364 kfree(bch->a_pow_tab); in free_bch()
1365 kfree(bch->a_log_tab); in free_bch()
1366 kfree(bch->mod8_tab); in free_bch()
1367 kfree(bch->ecc_buf); in free_bch()
1368 kfree(bch->ecc_buf2); in free_bch()
1369 kfree(bch->xi_tab); in free_bch()
1370 kfree(bch->syn); in free_bch()
1371 kfree(bch->cache); in free_bch()
1372 kfree(bch->elp); in free_bch()
1374 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in free_bch()
1375 kfree(bch->poly_2t[i]); in free_bch()
1377 kfree(bch); in free_bch()
1384 MODULE_DESCRIPTION("Binary BCH encoder/decoder");