Lines Matching refs:bch
152 static u8 swap_bits(struct bch_control *bch, u8 in) in swap_bits() argument
154 if (!bch->swap_bits) in swap_bits()
163 static void bch_encode_unaligned(struct bch_control *bch, in bch_encode_unaligned() argument
169 const int l = BCH_ECC_WORDS(bch)-1; in bch_encode_unaligned()
172 u8 tmp = swap_bits(bch, *data++); in bch_encode_unaligned()
174 p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(tmp)) & 0xff); in bch_encode_unaligned()
186 static void load_ecc8(struct bch_control *bch, uint32_t *dst, in load_ecc8() argument
190 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in load_ecc8()
193 dst[i] = ((u32)swap_bits(bch, src[0]) << 24) | in load_ecc8()
194 ((u32)swap_bits(bch, src[1]) << 16) | in load_ecc8()
195 ((u32)swap_bits(bch, src[2]) << 8) | in load_ecc8()
196 swap_bits(bch, src[3]); in load_ecc8()
198 memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords); in load_ecc8()
199 dst[nwords] = ((u32)swap_bits(bch, pad[0]) << 24) | in load_ecc8()
200 ((u32)swap_bits(bch, pad[1]) << 16) | in load_ecc8()
201 ((u32)swap_bits(bch, pad[2]) << 8) | in load_ecc8()
202 swap_bits(bch, pad[3]); in load_ecc8()
208 static void store_ecc8(struct bch_control *bch, uint8_t *dst, in store_ecc8() argument
212 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; in store_ecc8()
215 *dst++ = swap_bits(bch, src[i] >> 24); in store_ecc8()
216 *dst++ = swap_bits(bch, src[i] >> 16); in store_ecc8()
217 *dst++ = swap_bits(bch, src[i] >> 8); in store_ecc8()
218 *dst++ = swap_bits(bch, src[i]); in store_ecc8()
220 pad[0] = swap_bits(bch, src[nwords] >> 24); in store_ecc8()
221 pad[1] = swap_bits(bch, src[nwords] >> 16); in store_ecc8()
222 pad[2] = swap_bits(bch, src[nwords] >> 8); in store_ecc8()
223 pad[3] = swap_bits(bch, src[nwords]); in store_ecc8()
224 memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords); in store_ecc8()
241 void bch_encode(struct bch_control *bch, const uint8_t *data, in bch_encode() argument
244 const unsigned int l = BCH_ECC_WORDS(bch)-1; in bch_encode()
248 const size_t r_bytes = BCH_ECC_WORDS(bch) * sizeof(*r); in bch_encode()
249 const uint32_t * const tab0 = bch->mod8_tab; in bch_encode()
260 load_ecc8(bch, bch->ecc_buf, ecc); in bch_encode()
262 memset(bch->ecc_buf, 0, r_bytes); in bch_encode()
269 bch_encode_unaligned(bch, data, mlen, bch->ecc_buf); in bch_encode()
279 memcpy(r, bch->ecc_buf, r_bytes); in bch_encode()
295 if (bch->swap_bits) in bch_encode()
296 w = (u32)swap_bits(bch, w) | in bch_encode()
297 ((u32)swap_bits(bch, w >> 8) << 8) | in bch_encode()
298 ((u32)swap_bits(bch, w >> 16) << 16) | in bch_encode()
299 ((u32)swap_bits(bch, w >> 24) << 24); in bch_encode()
311 memcpy(bch->ecc_buf, r, r_bytes); in bch_encode()
315 bch_encode_unaligned(bch, data, len, bch->ecc_buf); in bch_encode()
319 store_ecc8(bch, ecc, bch->ecc_buf); in bch_encode()
323 static inline int modulo(struct bch_control *bch, unsigned int v) in modulo() argument
325 const unsigned int n = GF_N(bch); in modulo()
328 v = (v & n) + (v >> GF_M(bch)); in modulo()
336 static inline int mod_s(struct bch_control *bch, unsigned int v) in mod_s() argument
338 const unsigned int n = GF_N(bch); in mod_s()
362 static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a, in gf_mul() argument
365 return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_mul()
366 bch->a_log_tab[b])] : 0; in gf_mul()
369 static inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a) in gf_sqr() argument
371 return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0; in gf_sqr()
374 static inline unsigned int gf_div(struct bch_control *bch, unsigned int a, in gf_div() argument
377 return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ in gf_div()
378 GF_N(bch)-bch->a_log_tab[b])] : 0; in gf_div()
381 static inline unsigned int gf_inv(struct bch_control *bch, unsigned int a) in gf_inv() argument
383 return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]]; in gf_inv()
386 static inline unsigned int a_pow(struct bch_control *bch, int i) in a_pow() argument
388 return bch->a_pow_tab[modulo(bch, i)]; in a_pow()
391 static inline int a_log(struct bch_control *bch, unsigned int x) in a_log() argument
393 return bch->a_log_tab[x]; in a_log()
396 static inline int a_ilog(struct bch_control *bch, unsigned int x) in a_ilog() argument
398 return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]); in a_ilog()
404 static void compute_syndromes(struct bch_control *bch, uint32_t *ecc, in compute_syndromes() argument
410 const int t = GF_T(bch); in compute_syndromes()
412 s = bch->ecc_bits; in compute_syndromes()
427 syn[j] ^= a_pow(bch, (j+1)*(i+s)); in compute_syndromes()
435 syn[2*j+1] = gf_sqr(bch, syn[j]); in compute_syndromes()
443 static int compute_error_locator_polynomial(struct bch_control *bch, in compute_error_locator_polynomial() argument
446 const unsigned int t = GF_T(bch); in compute_error_locator_polynomial()
447 const unsigned int n = GF_N(bch); in compute_error_locator_polynomial()
449 struct gf_poly *elp = bch->elp; in compute_error_locator_polynomial()
450 struct gf_poly *pelp = bch->poly_2t[0]; in compute_error_locator_polynomial()
451 struct gf_poly *elp_copy = bch->poly_2t[1]; in compute_error_locator_polynomial()
468 tmp = a_log(bch, d)+n-a_log(bch, pd); in compute_error_locator_polynomial()
471 l = a_log(bch, pelp->c[j]); in compute_error_locator_polynomial()
472 elp->c[j+k] ^= a_pow(bch, tmp+l); in compute_error_locator_polynomial()
488 d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]); in compute_error_locator_polynomial()
499 static int solve_linear_system(struct bch_control *bch, unsigned int *rows, in solve_linear_system() argument
502 const int m = GF_M(bch); in solve_linear_system()
575 static int find_affine4_roots(struct bch_control *bch, unsigned int a, in find_affine4_roots() argument
580 const int m = GF_M(bch); in find_affine4_roots()
583 j = a_log(bch, b); in find_affine4_roots()
584 k = a_log(bch, a); in find_affine4_roots()
589 rows[i+1] = bch->a_pow_tab[4*i]^ in find_affine4_roots()
590 (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^ in find_affine4_roots()
591 (b ? bch->a_pow_tab[mod_s(bch, j)] : 0); in find_affine4_roots()
606 return solve_linear_system(bch, rows, roots, 4); in find_affine4_roots()
612 static int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg1_roots() argument
619 roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+ in find_poly_deg1_roots()
620 bch->a_log_tab[poly->c[1]]); in find_poly_deg1_roots()
627 static int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg2_roots() argument
635 l0 = bch->a_log_tab[poly->c[0]]; in find_poly_deg2_roots()
636 l1 = bch->a_log_tab[poly->c[1]]; in find_poly_deg2_roots()
637 l2 = bch->a_log_tab[poly->c[2]]; in find_poly_deg2_roots()
640 u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1)); in find_poly_deg2_roots()
651 r ^= bch->xi_tab[i]; in find_poly_deg2_roots()
655 if ((gf_sqr(bch, r)^r) == u) { in find_poly_deg2_roots()
657 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
658 bch->a_log_tab[r]+l2); in find_poly_deg2_roots()
659 roots[n++] = modulo(bch, 2*GF_N(bch)-l1- in find_poly_deg2_roots()
660 bch->a_log_tab[r^1]+l2); in find_poly_deg2_roots()
669 static int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg3_roots() argument
678 c2 = gf_div(bch, poly->c[0], e3); in find_poly_deg3_roots()
679 b2 = gf_div(bch, poly->c[1], e3); in find_poly_deg3_roots()
680 a2 = gf_div(bch, poly->c[2], e3); in find_poly_deg3_roots()
683 c = gf_mul(bch, a2, c2); /* c = a2c2 */ in find_poly_deg3_roots()
684 b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */ in find_poly_deg3_roots()
685 a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */ in find_poly_deg3_roots()
688 if (find_affine4_roots(bch, a, b, c, tmp) == 4) { in find_poly_deg3_roots()
692 roots[n++] = a_ilog(bch, tmp[i]); in find_poly_deg3_roots()
702 static int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly, in find_poly_deg4_roots() argument
713 d = gf_div(bch, poly->c[0], e4); in find_poly_deg4_roots()
714 c = gf_div(bch, poly->c[1], e4); in find_poly_deg4_roots()
715 b = gf_div(bch, poly->c[2], e4); in find_poly_deg4_roots()
716 a = gf_div(bch, poly->c[3], e4); in find_poly_deg4_roots()
723 f = gf_div(bch, c, a); in find_poly_deg4_roots()
724 l = a_log(bch, f); in find_poly_deg4_roots()
725 l += (l & 1) ? GF_N(bch) : 0; in find_poly_deg4_roots()
726 e = a_pow(bch, l/2); in find_poly_deg4_roots()
734 d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d; in find_poly_deg4_roots()
735 b = gf_mul(bch, a, e)^b; in find_poly_deg4_roots()
742 c2 = gf_inv(bch, d); in find_poly_deg4_roots()
743 b2 = gf_div(bch, a, d); in find_poly_deg4_roots()
744 a2 = gf_div(bch, b, d); in find_poly_deg4_roots()
752 if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) { in find_poly_deg4_roots()
755 f = a ? gf_inv(bch, roots[i]) : roots[i]; in find_poly_deg4_roots()
756 roots[i] = a_ilog(bch, f^e); in find_poly_deg4_roots()
766 static void gf_poly_logrep(struct bch_control *bch, in gf_poly_logrep() argument
769 int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]); in gf_poly_logrep()
773 rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1; in gf_poly_logrep()
779 static void gf_poly_mod(struct bch_control *bch, struct gf_poly *a, in gf_poly_mod() argument
791 rep = bch->cache; in gf_poly_mod()
792 gf_poly_logrep(bch, b, rep); in gf_poly_mod()
797 la = a_log(bch, c[j]); in gf_poly_mod()
802 c[p] ^= bch->a_pow_tab[mod_s(bch, in gf_poly_mod()
815 static void gf_poly_div(struct bch_control *bch, struct gf_poly *a, in gf_poly_div() argument
821 gf_poly_mod(bch, a, b, NULL); in gf_poly_div()
833 static struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a, in gf_poly_gcd() argument
847 gf_poly_mod(bch, a, b, NULL); in gf_poly_gcd()
862 static void compute_trace_bk_mod(struct bch_control *bch, int k, in compute_trace_bk_mod() argument
866 const int m = GF_M(bch); in compute_trace_bk_mod()
872 z->c[1] = bch->a_pow_tab[k]; in compute_trace_bk_mod()
878 gf_poly_logrep(bch, f, bch->cache); in compute_trace_bk_mod()
884 z->c[2*j] = gf_sqr(bch, z->c[j]); in compute_trace_bk_mod()
893 gf_poly_mod(bch, z, f, bch->cache); in compute_trace_bk_mod()
905 static void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f, in factor_polynomial() argument
908 struct gf_poly *f2 = bch->poly_2t[0]; in factor_polynomial()
909 struct gf_poly *q = bch->poly_2t[1]; in factor_polynomial()
910 struct gf_poly *tk = bch->poly_2t[2]; in factor_polynomial()
911 struct gf_poly *z = bch->poly_2t[3]; in factor_polynomial()
920 compute_trace_bk_mod(bch, k, f, z, tk); in factor_polynomial()
925 gcd = gf_poly_gcd(bch, f2, tk); in factor_polynomial()
928 gf_poly_div(bch, f, gcd, q); in factor_polynomial()
941 static int find_poly_roots(struct bch_control *bch, unsigned int k, in find_poly_roots() argument
950 cnt = find_poly_deg1_roots(bch, poly, roots); in find_poly_roots()
953 cnt = find_poly_deg2_roots(bch, poly, roots); in find_poly_roots()
956 cnt = find_poly_deg3_roots(bch, poly, roots); in find_poly_roots()
959 cnt = find_poly_deg4_roots(bch, poly, roots); in find_poly_roots()
964 if (poly->deg && (k <= GF_M(bch))) { in find_poly_roots()
965 factor_polynomial(bch, k, poly, &f1, &f2); in find_poly_roots()
967 cnt += find_poly_roots(bch, k+1, f1, roots); in find_poly_roots()
969 cnt += find_poly_roots(bch, k+1, f2, roots+cnt); in find_poly_roots()
981 static int chien_search(struct bch_control *bch, unsigned int len, in chien_search() argument
986 const unsigned int k = 8*len+bch->ecc_bits; in chien_search()
989 gf_poly_logrep(bch, p, bch->cache); in chien_search()
990 bch->cache[p->deg] = 0; in chien_search()
991 syn0 = gf_div(bch, p->c[0], p->c[p->deg]); in chien_search()
993 for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) { in chien_search()
996 m = bch->cache[j]; in chien_search()
998 syn ^= a_pow(bch, m+j*i); in chien_search()
1001 roots[count++] = GF_N(bch)-i; in chien_search()
1053 int bch_decode(struct bch_control *bch, const uint8_t *data, unsigned int len, in bch_decode() argument
1057 const unsigned int ecc_words = BCH_ECC_WORDS(bch); in bch_decode()
1063 if (8*len > (bch->n-bch->ecc_bits)) in bch_decode()
1072 bch_encode(bch, data, len, NULL); in bch_decode()
1075 load_ecc8(bch, bch->ecc_buf, calc_ecc); in bch_decode()
1079 load_ecc8(bch, bch->ecc_buf2, recv_ecc); in bch_decode()
1082 bch->ecc_buf[i] ^= bch->ecc_buf2[i]; in bch_decode()
1083 sum |= bch->ecc_buf[i]; in bch_decode()
1089 compute_syndromes(bch, bch->ecc_buf, bch->syn); in bch_decode()
1090 syn = bch->syn; in bch_decode()
1093 err = compute_error_locator_polynomial(bch, syn); in bch_decode()
1095 nroots = find_poly_roots(bch, 1, bch->elp, errloc); in bch_decode()
1101 nbits = (len*8)+bch->ecc_bits; in bch_decode()
1108 if (!bch->swap_bits) in bch_decode()
1120 static int build_gf_tables(struct bch_control *bch, unsigned int poly) in build_gf_tables() argument
1126 if (k != (1u << GF_M(bch))) in build_gf_tables()
1129 for (i = 0; i < GF_N(bch); i++) { in build_gf_tables()
1130 bch->a_pow_tab[i] = x; in build_gf_tables()
1131 bch->a_log_tab[x] = i; in build_gf_tables()
1139 bch->a_pow_tab[GF_N(bch)] = 1; in build_gf_tables()
1140 bch->a_log_tab[0] = 0; in build_gf_tables()
1148 static void build_mod8_tables(struct bch_control *bch, const uint32_t *g) in build_mod8_tables() argument
1152 const int l = BCH_ECC_WORDS(bch); in build_mod8_tables()
1153 const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32); in build_mod8_tables()
1154 const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32); in build_mod8_tables()
1156 memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab)); in build_mod8_tables()
1162 tab = bch->mod8_tab + (b*256+i)*l; in build_mod8_tables()
1182 static int build_deg2_base(struct bch_control *bch) in build_deg2_base() argument
1184 const int m = GF_M(bch); in build_deg2_base()
1191 sum ^= a_pow(bch, i*(1 << j)); in build_deg2_base()
1194 ak = bch->a_pow_tab[i]; in build_deg2_base()
1202 for (x = 0; (x <= GF_N(bch)) && remaining; x++) { in build_deg2_base()
1203 y = gf_sqr(bch, x)^x; in build_deg2_base()
1205 r = a_log(bch, y); in build_deg2_base()
1207 bch->xi_tab[r] = x; in build_deg2_base()
1233 static uint32_t *compute_generator_polynomial(struct bch_control *bch) in compute_generator_polynomial() argument
1235 const unsigned int m = GF_M(bch); in compute_generator_polynomial()
1236 const unsigned int t = GF_T(bch); in compute_generator_polynomial()
1243 roots = bch_alloc((bch->n+1)*sizeof(*roots), &err); in compute_generator_polynomial()
1253 memset(roots , 0, (bch->n+1)*sizeof(*roots)); in compute_generator_polynomial()
1257 r = mod_s(bch, 2*r); in compute_generator_polynomial()
1263 for (i = 0; i < GF_N(bch); i++) { in compute_generator_polynomial()
1266 r = bch->a_pow_tab[i]; in compute_generator_polynomial()
1269 g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1]; in compute_generator_polynomial()
1271 g->c[0] = gf_mul(bch, g->c[0], r); in compute_generator_polynomial()
1288 bch->ecc_bits = g->deg; in compute_generator_polynomial()
1325 struct bch_control *bch = NULL; in bch_init() local
1367 bch = kzalloc(sizeof(*bch), GFP_KERNEL); in bch_init()
1368 if (bch == NULL) in bch_init()
1371 bch->m = m; in bch_init()
1372 bch->t = t; in bch_init()
1373 bch->n = (1 << m)-1; in bch_init()
1375 bch->ecc_bytes = DIV_ROUND_UP(m*t, 8); in bch_init()
1376 bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err); in bch_init()
1377 bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err); in bch_init()
1378 bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err); in bch_init()
1379 bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err); in bch_init()
1380 bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err); in bch_init()
1381 bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err); in bch_init()
1382 bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err); in bch_init()
1383 bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err); in bch_init()
1384 bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err); in bch_init()
1385 bch->swap_bits = swap_bits; in bch_init()
1387 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in bch_init()
1388 bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err); in bch_init()
1393 err = build_gf_tables(bch, prim_poly); in bch_init()
1398 genpoly = compute_generator_polynomial(bch); in bch_init()
1402 build_mod8_tables(bch, genpoly); in bch_init()
1405 err = build_deg2_base(bch); in bch_init()
1409 return bch; in bch_init()
1412 bch_free(bch); in bch_init()
1421 void bch_free(struct bch_control *bch) in bch_free() argument
1425 if (bch) { in bch_free()
1426 kfree(bch->a_pow_tab); in bch_free()
1427 kfree(bch->a_log_tab); in bch_free()
1428 kfree(bch->mod8_tab); in bch_free()
1429 kfree(bch->ecc_buf); in bch_free()
1430 kfree(bch->ecc_buf2); in bch_free()
1431 kfree(bch->xi_tab); in bch_free()
1432 kfree(bch->syn); in bch_free()
1433 kfree(bch->cache); in bch_free()
1434 kfree(bch->elp); in bch_free()
1436 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) in bch_free()
1437 kfree(bch->poly_2t[i]); in bch_free()
1439 kfree(bch); in bch_free()