Lines Matching refs:bch
154 static void encode_bch_unaligned(struct bch_control *bch, in encode_bch_unaligned() argument
160 const int l = BCH_ECC_WORDS(bch)-1; in encode_bch_unaligned()
163 p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(*data++)) & 0xff); in encode_bch_unaligned()
175 static void load_ecc8(struct bch_control *bch, uint32_t *dst, in load_ecc8() argument
179 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in load_ecc8()
184 memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords); in load_ecc8()
191 static void store_ecc8(struct bch_control *bch, uint8_t *dst, in store_ecc8() argument
195 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in store_ecc8()
207 memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords); in store_ecc8()
224 void encode_bch(struct bch_control *bch, const uint8_t *data, in encode_bch() argument
227 const unsigned int l = BCH_ECC_WORDS(bch)-1; in encode_bch()
231 const uint32_t * const tab0 = bch->mod8_tab; in encode_bch()
239 load_ecc8(bch, bch->ecc_buf, ecc); in encode_bch()
241 memset(bch->ecc_buf, 0, sizeof(r)); in encode_bch()
248 encode_bch_unaligned(bch, data, mlen, bch->ecc_buf); in encode_bch()
258 memcpy(r, bch->ecc_buf, sizeof(r)); in encode_bch()
284 memcpy(bch->ecc_buf, r, sizeof(r)); in encode_bch()
288 encode_bch_unaligned(bch, data, len, bch->ecc_buf); in encode_bch()
292 store_ecc8(bch, ecc, bch->ecc_buf); in encode_bch()
295 static inline int modulo(struct bch_control *bch, unsigned int v) in modulo() argument
297 const unsigned int n = GF_N(bch); in modulo()
300 v = (v & n) + (v >> GF_M(bch)); in modulo()
308 static inline int mod_s(struct bch_control *bch, unsigned int v) in mod_s() argument
310 const unsigned int n = GF_N(bch); in mod_s()
334 static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a, in gf_mul() argument
337 return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_mul()
338 bch->a_log_tab[b])] : 0; in gf_mul()
341 static inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a) in gf_sqr() argument
343 return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0; in gf_sqr()
346 static inline unsigned int gf_div(struct bch_control *bch, unsigned int a, in gf_div() argument
349 return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_div()
350 GF_N(bch)-bch->a_log_tab[b])] : 0; in gf_div()
353 static inline unsigned int gf_inv(struct bch_control *bch, unsigned int a) in gf_inv() argument
355 return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]]; in gf_inv()
358 static inline unsigned int a_pow(struct bch_control *bch, int i) in a_pow() argument
360 return bch->a_pow_tab[modulo(bch, i)]; in a_pow()
363 static inline int a_log(struct bch_control *bch, unsigned int x) in a_log() argument
365 return bch->a_log_tab[x]; in a_log()
368 static inline int a_ilog(struct bch_control *bch, unsigned int x) in a_ilog() argument
370 return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]); in a_ilog()
376 static void compute_syndromes(struct bch_control *bch, uint32_t *ecc, in compute_syndromes() argument
382 const int t = GF_T(bch); in compute_syndromes()
384 s = bch->ecc_bits; in compute_syndromes()
399 syn[j] ^= a_pow(bch, (j+1)*(i+s)); in compute_syndromes()
407 syn[2*j+1] = gf_sqr(bch, syn[j]); in compute_syndromes()
415 static int compute_error_locator_polynomial(struct bch_control *bch, in compute_error_locator_polynomial() argument
418 const unsigned int t = GF_T(bch); in compute_error_locator_polynomial()
419 const unsigned int n = GF_N(bch); in compute_error_locator_polynomial()
421 struct gf_poly *elp = bch->elp; in compute_error_locator_polynomial()
422 struct gf_poly *pelp = bch->poly_2t[0]; in compute_error_locator_polynomial()
423 struct gf_poly *elp_copy = bch->poly_2t[1]; in compute_error_locator_polynomial()
440 tmp = a_log(bch, d)+n-a_log(bch, pd); in compute_error_locator_polynomial()
443 l = a_log(bch, pelp->c[j]); in compute_error_locator_polynomial()
444 elp->c[j+k] ^= a_pow(bch, tmp+l); in compute_error_locator_polynomial()
460 d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]); in compute_error_locator_polynomial()
471 static int solve_linear_system(struct bch_control *bch, unsigned int *rows, in solve_linear_system() argument
474 const int m = GF_M(bch); in solve_linear_system()
547 static int find_affine4_roots(struct bch_control *bch, unsigned int a, in find_affine4_roots() argument
552 const int m = GF_M(bch); in find_affine4_roots()
555 j = a_log(bch, b); in find_affine4_roots()
556 k = a_log(bch, a); in find_affine4_roots()
561 rows[i+1] = bch->a_pow_tab[4*i]^ in find_affine4_roots()
562 (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^ in find_affine4_roots()
563 (b ? bch->a_pow_tab[mod_s(bch, j)] : 0); in find_affine4_roots()
578 return solve_linear_system(bch, rows, roots, 4); in find_affine4_roots()
584 static int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg1_roots() argument
591 roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+ in find_poly_deg1_roots()
592 bch->a_log_tab[poly->c[1]]); in find_poly_deg1_roots()
599 static int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg2_roots() argument
607 l0 = bch->a_log_tab[poly->c[0]]; in find_poly_deg2_roots()
608 l1 = bch->a_log_tab[poly->c[1]]; in find_poly_deg2_roots()
609 l2 = bch->a_log_tab[poly->c[2]]; in find_poly_deg2_roots()
612 u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1)); in find_poly_deg2_roots()
623 r ^= bch->xi_tab[i]; in find_poly_deg2_roots()
627 if ((gf_sqr(bch, r)^r) == u) { in find_poly_deg2_roots()
629 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
630 bch->a_log_tab[r]+l2); in find_poly_deg2_roots()
631 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
632 bch->a_log_tab[r^1]+l2); in find_poly_deg2_roots()
641 static int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg3_roots() argument
650 c2 = gf_div(bch, poly->c[0], e3); in find_poly_deg3_roots()
651 b2 = gf_div(bch, poly->c[1], e3); in find_poly_deg3_roots()
652 a2 = gf_div(bch, poly->c[2], e3); in find_poly_deg3_roots()
655 c = gf_mul(bch, a2, c2); /* c = a2c2 */ in find_poly_deg3_roots()
656 b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */ in find_poly_deg3_roots()
657 a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */ in find_poly_deg3_roots()
660 if (find_affine4_roots(bch, a, b, c, tmp) == 4) { in find_poly_deg3_roots()
664 roots[n++] = a_ilog(bch, tmp[i]); in find_poly_deg3_roots()
674 static int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg4_roots() argument
685 d = gf_div(bch, poly->c[0], e4); in find_poly_deg4_roots()
686 c = gf_div(bch, poly->c[1], e4); in find_poly_deg4_roots()
687 b = gf_div(bch, poly->c[2], e4); in find_poly_deg4_roots()
688 a = gf_div(bch, poly->c[3], e4); in find_poly_deg4_roots()
695 f = gf_div(bch, c, a); in find_poly_deg4_roots()
696 l = a_log(bch, f); in find_poly_deg4_roots()
697 l += (l & 1) ? GF_N(bch) : 0; in find_poly_deg4_roots()
698 e = a_pow(bch, l/2); in find_poly_deg4_roots()
706 d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d; in find_poly_deg4_roots()
707 b = gf_mul(bch, a, e)^b; in find_poly_deg4_roots()
714 c2 = gf_inv(bch, d); in find_poly_deg4_roots()
715 b2 = gf_div(bch, a, d); in find_poly_deg4_roots()
716 a2 = gf_div(bch, b, d); in find_poly_deg4_roots()
724 if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) { in find_poly_deg4_roots()
727 f = a ? gf_inv(bch, roots[i]) : roots[i]; in find_poly_deg4_roots()
728 roots[i] = a_ilog(bch, f^e); in find_poly_deg4_roots()
738 static void gf_poly_logrep(struct bch_control *bch, in gf_poly_logrep() argument
741 int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]); in gf_poly_logrep()
745 rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1; in gf_poly_logrep()
751 static void gf_poly_mod(struct bch_control *bch, struct gf_poly *a, in gf_poly_mod() argument
763 rep = bch->cache; in gf_poly_mod()
764 gf_poly_logrep(bch, b, rep); in gf_poly_mod()
769 la = a_log(bch, c[j]); in gf_poly_mod()
774 c[p] ^= bch->a_pow_tab[mod_s(bch, in gf_poly_mod()
787 static void gf_poly_div(struct bch_control *bch, struct gf_poly *a, in gf_poly_div() argument
793 gf_poly_mod(bch, a, b, NULL); in gf_poly_div()
805 static struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a, in gf_poly_gcd() argument
819 gf_poly_mod(bch, a, b, NULL); in gf_poly_gcd()
834 static void compute_trace_bk_mod(struct bch_control *bch, int k, in compute_trace_bk_mod() argument
838 const int m = GF_M(bch); in compute_trace_bk_mod()
844 z->c[1] = bch->a_pow_tab[k]; in compute_trace_bk_mod()
850 gf_poly_logrep(bch, f, bch->cache); in compute_trace_bk_mod()
856 z->c[2*j] = gf_sqr(bch, z->c[j]); in compute_trace_bk_mod()
865 gf_poly_mod(bch, z, f, bch->cache); in compute_trace_bk_mod()
877 static void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f, in factor_polynomial() argument
880 struct gf_poly *f2 = bch->poly_2t[0]; in factor_polynomial()
881 struct gf_poly *q = bch->poly_2t[1]; in factor_polynomial()
882 struct gf_poly *tk = bch->poly_2t[2]; in factor_polynomial()
883 struct gf_poly *z = bch->poly_2t[3]; in factor_polynomial()
892 compute_trace_bk_mod(bch, k, f, z, tk); in factor_polynomial()
897 gcd = gf_poly_gcd(bch, f2, tk); in factor_polynomial()
900 gf_poly_div(bch, f, gcd, q); in factor_polynomial()
913 static int find_poly_roots(struct bch_control *bch, unsigned int k, in find_poly_roots() argument
922 cnt = find_poly_deg1_roots(bch, poly, roots); in find_poly_roots()
925 cnt = find_poly_deg2_roots(bch, poly, roots); in find_poly_roots()
928 cnt = find_poly_deg3_roots(bch, poly, roots); in find_poly_roots()
931 cnt = find_poly_deg4_roots(bch, poly, roots); in find_poly_roots()
936 if (poly->deg && (k <= GF_M(bch))) { in find_poly_roots()
937 factor_polynomial(bch, k, poly, &f1, &f2); in find_poly_roots()
939 cnt += find_poly_roots(bch, k+1, f1, roots); in find_poly_roots()
941 cnt += find_poly_roots(bch, k+1, f2, roots+cnt); in find_poly_roots()
953 static int chien_search(struct bch_control *bch, unsigned int len, in chien_search() argument
958 const unsigned int k = 8*len+bch->ecc_bits; in chien_search()
961 gf_poly_logrep(bch, p, bch->cache); in chien_search()
962 bch->cache[p->deg] = 0; in chien_search()
963 syn0 = gf_div(bch, p->c[0], p->c[p->deg]); in chien_search()
965 for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) { in chien_search()
968 m = bch->cache[j]; in chien_search()
970 syn ^= a_pow(bch, m+j*i); in chien_search()
973 roots[count++] = GF_N(bch)-i; in chien_search()
1025 int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len, in decode_bch() argument
1029 const unsigned int ecc_words = BCH_ECC_WORDS(bch); in decode_bch()
1035 if (8*len > (bch->n-bch->ecc_bits)) in decode_bch()
1044 encode_bch(bch, data, len, NULL); in decode_bch()
1047 load_ecc8(bch, bch->ecc_buf, calc_ecc); in decode_bch()
1051 load_ecc8(bch, bch->ecc_buf2, recv_ecc); in decode_bch()
1054 bch->ecc_buf[i] ^= bch->ecc_buf2[i]; in decode_bch()
1055 sum |= bch->ecc_buf[i]; in decode_bch()
1061 compute_syndromes(bch, bch->ecc_buf, bch->syn); in decode_bch()
1062 syn = bch->syn; in decode_bch()
1065 err = compute_error_locator_polynomial(bch, syn); in decode_bch()
1067 nroots = find_poly_roots(bch, 1, bch->elp, errloc); in decode_bch()
1073 nbits = (len*8)+bch->ecc_bits; in decode_bch()
1089 static int build_gf_tables(struct bch_control *bch, unsigned int poly) in build_gf_tables() argument
1095 if (k != (1u << GF_M(bch))) in build_gf_tables()
1098 for (i = 0; i < GF_N(bch); i++) { in build_gf_tables()
1099 bch->a_pow_tab[i] = x; in build_gf_tables()
1100 bch->a_log_tab[x] = i; in build_gf_tables()
1108 bch->a_pow_tab[GF_N(bch)] = 1; in build_gf_tables()
1109 bch->a_log_tab[0] = 0; in build_gf_tables()
1117 static void build_mod8_tables(struct bch_control *bch, const uint32_t *g) in build_mod8_tables() argument
1121 const int l = BCH_ECC_WORDS(bch); in build_mod8_tables()
1122 const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32); in build_mod8_tables()
1123 const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32); in build_mod8_tables()
1125 memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab)); in build_mod8_tables()
1131 tab = bch->mod8_tab + (b*256+i)*l; in build_mod8_tables()
1151 static int build_deg2_base(struct bch_control *bch) in build_deg2_base() argument
1153 const int m = GF_M(bch); in build_deg2_base()
1160 sum ^= a_pow(bch, i*(1 << j)); in build_deg2_base()
1163 ak = bch->a_pow_tab[i]; in build_deg2_base()
1171 for (x = 0; (x <= GF_N(bch)) && remaining; x++) { in build_deg2_base()
1172 y = gf_sqr(bch, x)^x; in build_deg2_base()
1174 r = a_log(bch, y); in build_deg2_base()
1176 bch->xi_tab[r] = x; in build_deg2_base()
1202 static uint32_t *compute_generator_polynomial(struct bch_control *bch) in compute_generator_polynomial() argument
1204 const unsigned int m = GF_M(bch); in compute_generator_polynomial()
1205 const unsigned int t = GF_T(bch); in compute_generator_polynomial()
1212 roots = bch_alloc((bch->n+1)*sizeof(*roots), &err); in compute_generator_polynomial()
1222 memset(roots , 0, (bch->n+1)*sizeof(*roots)); in compute_generator_polynomial()
1226 r = mod_s(bch, 2*r); in compute_generator_polynomial()
1232 for (i = 0; i < GF_N(bch); i++) { in compute_generator_polynomial()
1235 r = bch->a_pow_tab[i]; in compute_generator_polynomial()
1238 g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1]; in compute_generator_polynomial()
1240 g->c[0] = gf_mul(bch, g->c[0], r); in compute_generator_polynomial()
1257 bch->ecc_bits = g->deg; in compute_generator_polynomial()
1292 struct bch_control *bch = NULL; in init_bch() local
1328 bch = kzalloc(sizeof(*bch), GFP_KERNEL); in init_bch()
1329 if (bch == NULL) in init_bch()
1332 bch->m = m; in init_bch()
1333 bch->t = t; in init_bch()
1334 bch->n = (1 << m)-1; in init_bch()
1336 bch->ecc_bytes = DIV_ROUND_UP(m*t, 8); in init_bch()
1337 bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err); in init_bch()
1338 bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err); in init_bch()
1339 bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err); in init_bch()
1340 bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err); in init_bch()
1341 bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err); in init_bch()
1342 bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err); in init_bch()
1343 bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err); in init_bch()
1344 bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err); in init_bch()
1345 bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err); in init_bch()
1347 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in init_bch()
1348 bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err); in init_bch()
1353 err = build_gf_tables(bch, prim_poly); in init_bch()
1358 genpoly = compute_generator_polynomial(bch); in init_bch()
1362 build_mod8_tables(bch, genpoly); in init_bch()
1365 err = build_deg2_base(bch); in init_bch()
1369 return bch; in init_bch()
1372 free_bch(bch); in init_bch()
1380 void free_bch(struct bch_control *bch) in free_bch() argument
1384 if (bch) { in free_bch()
1385 kfree(bch->a_pow_tab); in free_bch()
1386 kfree(bch->a_log_tab); in free_bch()
1387 kfree(bch->mod8_tab); in free_bch()
1388 kfree(bch->ecc_buf); in free_bch()
1389 kfree(bch->ecc_buf2); in free_bch()
1390 kfree(bch->xi_tab); in free_bch()
1391 kfree(bch->syn); in free_bch()
1392 kfree(bch->cache); in free_bch()
1393 kfree(bch->elp); in free_bch()
1395 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in free_bch()
1396 kfree(bch->poly_2t[i]); in free_bch()
1398 kfree(bch); in free_bch()