Lines Matching refs:bch

114 static void encode_bch_unaligned(struct bch_control *bch,  in encode_bch_unaligned()  argument
120 const int l = BCH_ECC_WORDS(bch)-1; in encode_bch_unaligned()
123 p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(*data++)) & 0xff); in encode_bch_unaligned()
135 static void load_ecc8(struct bch_control *bch, uint32_t *dst, in load_ecc8() argument
139 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in load_ecc8()
144 memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords); in load_ecc8()
151 static void store_ecc8(struct bch_control *bch, uint8_t *dst, in store_ecc8() argument
155 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in store_ecc8()
167 memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords); in store_ecc8()
184 void encode_bch(struct bch_control *bch, const uint8_t *data, in encode_bch() argument
187 const unsigned int l = BCH_ECC_WORDS(bch)-1; in encode_bch()
191 const uint32_t * const tab0 = bch->mod8_tab; in encode_bch()
199 load_ecc8(bch, bch->ecc_buf, ecc); in encode_bch()
201 memset(bch->ecc_buf, 0, sizeof(r)); in encode_bch()
208 encode_bch_unaligned(bch, data, mlen, bch->ecc_buf); in encode_bch()
218 memcpy(r, bch->ecc_buf, sizeof(r)); in encode_bch()
244 memcpy(bch->ecc_buf, r, sizeof(r)); in encode_bch()
248 encode_bch_unaligned(bch, data, len, bch->ecc_buf); in encode_bch()
252 store_ecc8(bch, ecc, bch->ecc_buf); in encode_bch()
256 static inline int modulo(struct bch_control *bch, unsigned int v) in modulo() argument
258 const unsigned int n = GF_N(bch); in modulo()
261 v = (v & n) + (v >> GF_M(bch)); in modulo()
269 static inline int mod_s(struct bch_control *bch, unsigned int v) in mod_s() argument
271 const unsigned int n = GF_N(bch); in mod_s()
295 static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a, in gf_mul() argument
298 return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_mul()
299 bch->a_log_tab[b])] : 0; in gf_mul()
302 static inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a) in gf_sqr() argument
304 return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0; in gf_sqr()
307 static inline unsigned int gf_div(struct bch_control *bch, unsigned int a, in gf_div() argument
310 return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_div()
311 GF_N(bch)-bch->a_log_tab[b])] : 0; in gf_div()
314 static inline unsigned int gf_inv(struct bch_control *bch, unsigned int a) in gf_inv() argument
316 return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]]; in gf_inv()
319 static inline unsigned int a_pow(struct bch_control *bch, int i) in a_pow() argument
321 return bch->a_pow_tab[modulo(bch, i)]; in a_pow()
324 static inline int a_log(struct bch_control *bch, unsigned int x) in a_log() argument
326 return bch->a_log_tab[x]; in a_log()
329 static inline int a_ilog(struct bch_control *bch, unsigned int x) in a_ilog() argument
331 return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]); in a_ilog()
337 static void compute_syndromes(struct bch_control *bch, uint32_t *ecc, in compute_syndromes() argument
343 const int t = GF_T(bch); in compute_syndromes()
345 s = bch->ecc_bits; in compute_syndromes()
360 syn[j] ^= a_pow(bch, (j+1)*(i+s)); in compute_syndromes()
368 syn[2*j+1] = gf_sqr(bch, syn[j]); in compute_syndromes()
376 static int compute_error_locator_polynomial(struct bch_control *bch, in compute_error_locator_polynomial() argument
379 const unsigned int t = GF_T(bch); in compute_error_locator_polynomial()
380 const unsigned int n = GF_N(bch); in compute_error_locator_polynomial()
382 struct gf_poly *elp = bch->elp; in compute_error_locator_polynomial()
383 struct gf_poly *pelp = bch->poly_2t[0]; in compute_error_locator_polynomial()
384 struct gf_poly *elp_copy = bch->poly_2t[1]; in compute_error_locator_polynomial()
401 tmp = a_log(bch, d)+n-a_log(bch, pd); in compute_error_locator_polynomial()
404 l = a_log(bch, pelp->c[j]); in compute_error_locator_polynomial()
405 elp->c[j+k] ^= a_pow(bch, tmp+l); in compute_error_locator_polynomial()
421 d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]); in compute_error_locator_polynomial()
432 static int solve_linear_system(struct bch_control *bch, unsigned int *rows, in solve_linear_system() argument
435 const int m = GF_M(bch); in solve_linear_system()
508 static int find_affine4_roots(struct bch_control *bch, unsigned int a, in find_affine4_roots() argument
513 const int m = GF_M(bch); in find_affine4_roots()
516 j = a_log(bch, b); in find_affine4_roots()
517 k = a_log(bch, a); in find_affine4_roots()
522 rows[i+1] = bch->a_pow_tab[4*i]^ in find_affine4_roots()
523 (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^ in find_affine4_roots()
524 (b ? bch->a_pow_tab[mod_s(bch, j)] : 0); in find_affine4_roots()
539 return solve_linear_system(bch, rows, roots, 4); in find_affine4_roots()
545 static int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg1_roots() argument
552 roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+ in find_poly_deg1_roots()
553 bch->a_log_tab[poly->c[1]]); in find_poly_deg1_roots()
560 static int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg2_roots() argument
568 l0 = bch->a_log_tab[poly->c[0]]; in find_poly_deg2_roots()
569 l1 = bch->a_log_tab[poly->c[1]]; in find_poly_deg2_roots()
570 l2 = bch->a_log_tab[poly->c[2]]; in find_poly_deg2_roots()
573 u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1)); in find_poly_deg2_roots()
584 r ^= bch->xi_tab[i]; in find_poly_deg2_roots()
588 if ((gf_sqr(bch, r)^r) == u) { in find_poly_deg2_roots()
590 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
591 bch->a_log_tab[r]+l2); in find_poly_deg2_roots()
592 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
593 bch->a_log_tab[r^1]+l2); in find_poly_deg2_roots()
602 static int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg3_roots() argument
611 c2 = gf_div(bch, poly->c[0], e3); in find_poly_deg3_roots()
612 b2 = gf_div(bch, poly->c[1], e3); in find_poly_deg3_roots()
613 a2 = gf_div(bch, poly->c[2], e3); in find_poly_deg3_roots()
616 c = gf_mul(bch, a2, c2); /* c = a2c2 */ in find_poly_deg3_roots()
617 b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */ in find_poly_deg3_roots()
618 a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */ in find_poly_deg3_roots()
621 if (find_affine4_roots(bch, a, b, c, tmp) == 4) { in find_poly_deg3_roots()
625 roots[n++] = a_ilog(bch, tmp[i]); in find_poly_deg3_roots()
635 static int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg4_roots() argument
646 d = gf_div(bch, poly->c[0], e4); in find_poly_deg4_roots()
647 c = gf_div(bch, poly->c[1], e4); in find_poly_deg4_roots()
648 b = gf_div(bch, poly->c[2], e4); in find_poly_deg4_roots()
649 a = gf_div(bch, poly->c[3], e4); in find_poly_deg4_roots()
656 f = gf_div(bch, c, a); in find_poly_deg4_roots()
657 l = a_log(bch, f); in find_poly_deg4_roots()
658 l += (l & 1) ? GF_N(bch) : 0; in find_poly_deg4_roots()
659 e = a_pow(bch, l/2); in find_poly_deg4_roots()
667 d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d; in find_poly_deg4_roots()
668 b = gf_mul(bch, a, e)^b; in find_poly_deg4_roots()
675 c2 = gf_inv(bch, d); in find_poly_deg4_roots()
676 b2 = gf_div(bch, a, d); in find_poly_deg4_roots()
677 a2 = gf_div(bch, b, d); in find_poly_deg4_roots()
685 if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) { in find_poly_deg4_roots()
688 f = a ? gf_inv(bch, roots[i]) : roots[i]; in find_poly_deg4_roots()
689 roots[i] = a_ilog(bch, f^e); in find_poly_deg4_roots()
699 static void gf_poly_logrep(struct bch_control *bch, in gf_poly_logrep() argument
702 int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]); in gf_poly_logrep()
706 rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1; in gf_poly_logrep()
712 static void gf_poly_mod(struct bch_control *bch, struct gf_poly *a, in gf_poly_mod() argument
724 rep = bch->cache; in gf_poly_mod()
725 gf_poly_logrep(bch, b, rep); in gf_poly_mod()
730 la = a_log(bch, c[j]); in gf_poly_mod()
735 c[p] ^= bch->a_pow_tab[mod_s(bch, in gf_poly_mod()
748 static void gf_poly_div(struct bch_control *bch, struct gf_poly *a, in gf_poly_div() argument
754 gf_poly_mod(bch, a, b, NULL); in gf_poly_div()
766 static struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a, in gf_poly_gcd() argument
780 gf_poly_mod(bch, a, b, NULL); in gf_poly_gcd()
795 static void compute_trace_bk_mod(struct bch_control *bch, int k, in compute_trace_bk_mod() argument
799 const int m = GF_M(bch); in compute_trace_bk_mod()
805 z->c[1] = bch->a_pow_tab[k]; in compute_trace_bk_mod()
811 gf_poly_logrep(bch, f, bch->cache); in compute_trace_bk_mod()
817 z->c[2*j] = gf_sqr(bch, z->c[j]); in compute_trace_bk_mod()
826 gf_poly_mod(bch, z, f, bch->cache); in compute_trace_bk_mod()
838 static void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f, in factor_polynomial() argument
841 struct gf_poly *f2 = bch->poly_2t[0]; in factor_polynomial()
842 struct gf_poly *q = bch->poly_2t[1]; in factor_polynomial()
843 struct gf_poly *tk = bch->poly_2t[2]; in factor_polynomial()
844 struct gf_poly *z = bch->poly_2t[3]; in factor_polynomial()
853 compute_trace_bk_mod(bch, k, f, z, tk); in factor_polynomial()
858 gcd = gf_poly_gcd(bch, f2, tk); in factor_polynomial()
861 gf_poly_div(bch, f, gcd, q); in factor_polynomial()
874 static int find_poly_roots(struct bch_control *bch, unsigned int k, in find_poly_roots() argument
883 cnt = find_poly_deg1_roots(bch, poly, roots); in find_poly_roots()
886 cnt = find_poly_deg2_roots(bch, poly, roots); in find_poly_roots()
889 cnt = find_poly_deg3_roots(bch, poly, roots); in find_poly_roots()
892 cnt = find_poly_deg4_roots(bch, poly, roots); in find_poly_roots()
897 if (poly->deg && (k <= GF_M(bch))) { in find_poly_roots()
898 factor_polynomial(bch, k, poly, &f1, &f2); in find_poly_roots()
900 cnt += find_poly_roots(bch, k+1, f1, roots); in find_poly_roots()
902 cnt += find_poly_roots(bch, k+1, f2, roots+cnt); in find_poly_roots()
914 static int chien_search(struct bch_control *bch, unsigned int len, in chien_search() argument
919 const unsigned int k = 8*len+bch->ecc_bits; in chien_search()
922 gf_poly_logrep(bch, p, bch->cache); in chien_search()
923 bch->cache[p->deg] = 0; in chien_search()
924 syn0 = gf_div(bch, p->c[0], p->c[p->deg]); in chien_search()
926 for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) { in chien_search()
929 m = bch->cache[j]; in chien_search()
931 syn ^= a_pow(bch, m+j*i); in chien_search()
934 roots[count++] = GF_N(bch)-i; in chien_search()
986 int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len, in decode_bch() argument
990 const unsigned int ecc_words = BCH_ECC_WORDS(bch); in decode_bch()
996 if (8*len > (bch->n-bch->ecc_bits)) in decode_bch()
1005 encode_bch(bch, data, len, NULL); in decode_bch()
1008 load_ecc8(bch, bch->ecc_buf, calc_ecc); in decode_bch()
1012 load_ecc8(bch, bch->ecc_buf2, recv_ecc); in decode_bch()
1015 bch->ecc_buf[i] ^= bch->ecc_buf2[i]; in decode_bch()
1016 sum |= bch->ecc_buf[i]; in decode_bch()
1022 compute_syndromes(bch, bch->ecc_buf, bch->syn); in decode_bch()
1023 syn = bch->syn; in decode_bch()
1026 err = compute_error_locator_polynomial(bch, syn); in decode_bch()
1028 nroots = find_poly_roots(bch, 1, bch->elp, errloc); in decode_bch()
1034 nbits = (len*8)+bch->ecc_bits; in decode_bch()
1051 static int build_gf_tables(struct bch_control *bch, unsigned int poly) in build_gf_tables() argument
1057 if (k != (1u << GF_M(bch))) in build_gf_tables()
1060 for (i = 0; i < GF_N(bch); i++) { in build_gf_tables()
1061 bch->a_pow_tab[i] = x; in build_gf_tables()
1062 bch->a_log_tab[x] = i; in build_gf_tables()
1070 bch->a_pow_tab[GF_N(bch)] = 1; in build_gf_tables()
1071 bch->a_log_tab[0] = 0; in build_gf_tables()
1079 static void build_mod8_tables(struct bch_control *bch, const uint32_t *g) in build_mod8_tables() argument
1083 const int l = BCH_ECC_WORDS(bch); in build_mod8_tables()
1084 const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32); in build_mod8_tables()
1085 const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32); in build_mod8_tables()
1087 memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab)); in build_mod8_tables()
1093 tab = bch->mod8_tab + (b*256+i)*l; in build_mod8_tables()
1113 static int build_deg2_base(struct bch_control *bch) in build_deg2_base() argument
1115 const int m = GF_M(bch); in build_deg2_base()
1122 sum ^= a_pow(bch, i*(1 << j)); in build_deg2_base()
1125 ak = bch->a_pow_tab[i]; in build_deg2_base()
1133 for (x = 0; (x <= GF_N(bch)) && remaining; x++) { in build_deg2_base()
1134 y = gf_sqr(bch, x)^x; in build_deg2_base()
1136 r = a_log(bch, y); in build_deg2_base()
1138 bch->xi_tab[r] = x; in build_deg2_base()
1164 static uint32_t *compute_generator_polynomial(struct bch_control *bch) in compute_generator_polynomial() argument
1166 const unsigned int m = GF_M(bch); in compute_generator_polynomial()
1167 const unsigned int t = GF_T(bch); in compute_generator_polynomial()
1174 roots = bch_alloc((bch->n+1)*sizeof(*roots), &err); in compute_generator_polynomial()
1184 memset(roots , 0, (bch->n+1)*sizeof(*roots)); in compute_generator_polynomial()
1188 r = mod_s(bch, 2*r); in compute_generator_polynomial()
1194 for (i = 0; i < GF_N(bch); i++) { in compute_generator_polynomial()
1197 r = bch->a_pow_tab[i]; in compute_generator_polynomial()
1200 g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1]; in compute_generator_polynomial()
1202 g->c[0] = gf_mul(bch, g->c[0], r); in compute_generator_polynomial()
1219 bch->ecc_bits = g->deg; in compute_generator_polynomial()
1254 struct bch_control *bch = NULL; in init_bch() local
1290 bch = kzalloc(sizeof(*bch), GFP_KERNEL); in init_bch()
1291 if (bch == NULL) in init_bch()
1294 bch->m = m; in init_bch()
1295 bch->t = t; in init_bch()
1296 bch->n = (1 << m)-1; in init_bch()
1298 bch->ecc_bytes = DIV_ROUND_UP(m*t, 8); in init_bch()
1299 bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err); in init_bch()
1300 bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err); in init_bch()
1301 bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err); in init_bch()
1302 bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err); in init_bch()
1303 bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err); in init_bch()
1304 bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err); in init_bch()
1305 bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err); in init_bch()
1306 bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err); in init_bch()
1307 bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err); in init_bch()
1309 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in init_bch()
1310 bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err); in init_bch()
1315 err = build_gf_tables(bch, prim_poly); in init_bch()
1320 genpoly = compute_generator_polynomial(bch); in init_bch()
1324 build_mod8_tables(bch, genpoly); in init_bch()
1327 err = build_deg2_base(bch); in init_bch()
1331 return bch; in init_bch()
1334 free_bch(bch); in init_bch()
1343 void free_bch(struct bch_control *bch) in free_bch() argument
1347 if (bch) { in free_bch()
1348 kfree(bch->a_pow_tab); in free_bch()
1349 kfree(bch->a_log_tab); in free_bch()
1350 kfree(bch->mod8_tab); in free_bch()
1351 kfree(bch->ecc_buf); in free_bch()
1352 kfree(bch->ecc_buf2); in free_bch()
1353 kfree(bch->xi_tab); in free_bch()
1354 kfree(bch->syn); in free_bch()
1355 kfree(bch->cache); in free_bch()
1356 kfree(bch->elp); in free_bch()
1358 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in free_bch()
1359 kfree(bch->poly_2t[i]); in free_bch()
1361 kfree(bch); in free_bch()