blob: 72b4fdcc9c446d6bc4a7c70aa7911a9ea3a6bc4c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Christian Hitz55f7bca2011-10-12 09:31:59 +02002/*
3 * Generic binary BCH encoding/decoding library
4 *
Christian Hitz55f7bca2011-10-12 09:31:59 +02005 * Copyright © 2011 Parrot S.A.
6 *
7 * Author: Ivan Djelic <ivan.djelic@parrot.com>
8 *
9 * Description:
10 *
11 * This library provides runtime configurable encoding/decoding of binary
12 * Bose-Chaudhuri-Hocquenghem (BCH) codes.
13 *
14 * Call init_bch to get a pointer to a newly allocated bch_control structure for
15 * the given m (Galois field order), t (error correction capability) and
16 * (optional) primitive polynomial parameters.
17 *
18 * Call encode_bch to compute and store ecc parity bytes to a given buffer.
19 * Call decode_bch to detect and locate errors in received data.
20 *
21 * On systems supporting hw BCH features, intermediate results may be provided
22 * to decode_bch in order to skip certain steps. See decode_bch() documentation
23 * for details.
24 *
25 * Option CONFIG_BCH_CONST_PARAMS can be used to force fixed values of
26 * parameters m and t; thus allowing extra compiler optimizations and providing
27 * better (up to 2x) encoding performance. Using this option makes sense when
28 * (m,t) are fixed and known in advance, e.g. when using BCH error correction
29 * on a particular NAND flash device.
30 *
31 * Algorithmic details:
32 *
33 * Encoding is performed by processing 32 input bits in parallel, using 4
34 * remainder lookup tables.
35 *
36 * The final stage of decoding involves the following internal steps:
37 * a. Syndrome computation
38 * b. Error locator polynomial computation using Berlekamp-Massey algorithm
39 * c. Error locator root finding (by far the most expensive step)
40 *
41 * In this implementation, step c is not performed using the usual Chien search.
42 * Instead, an alternative approach described in [1] is used. It consists in
43 * factoring the error locator polynomial using the Berlekamp Trace algorithm
44 * (BTA) down to a certain degree (4), after which ad hoc low-degree polynomial
45 * solving techniques [2] are used. The resulting algorithm, called BTZ, yields
46 * much better performance than Chien search for usual (m,t) values (typically
47 * m >= 13, t < 32, see [1]).
48 *
49 * [1] B. Biswas, V. Herbert. Efficient root finding of polynomials over fields
50 * of characteristic 2, in: Western European Workshop on Research in Cryptology
51 * - WEWoRC 2009, Graz, Austria, LNCS, Springer, July 2009, to appear.
52 * [2] [Zin96] V.A. Zinoviev. On the solution of equations of degree 10 over
53 * finite fields GF(2^q). In Rapport de recherche INRIA no 2829, 1996.
54 */
55
Maxime Riparda8bbc202017-02-27 18:22:01 +010056#ifndef USE_HOSTCC
Christian Hitz55f7bca2011-10-12 09:31:59 +020057#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060058#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070059#include <malloc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020060#include <ubi_uboot.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070061#include <dm/devres.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020062
63#include <linux/bitops.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060064#include <linux/printk.h>
Maxime Riparda8bbc202017-02-27 18:22:01 +010065#else
66#include <errno.h>
Emmanuel Vadot3fae8e52017-06-20 09:02:29 +020067#if defined(__FreeBSD__)
68#include <sys/endian.h>
默默8ada1342019-03-31 16:07:03 +080069#elif defined(__APPLE__)
70#include <machine/endian.h>
71#include <libkern/OSByteOrder.h>
Emmanuel Vadot3fae8e52017-06-20 09:02:29 +020072#else
Maxime Riparda8bbc202017-02-27 18:22:01 +010073#include <endian.h>
Emmanuel Vadot3fae8e52017-06-20 09:02:29 +020074#endif
Maxime Riparda8bbc202017-02-27 18:22:01 +010075#include <stdint.h>
76#include <stdlib.h>
77#include <string.h>
78
79#undef cpu_to_be32
默默8ada1342019-03-31 16:07:03 +080080#if defined(__APPLE__)
81#define cpu_to_be32 OSSwapHostToBigInt32
82#else
Maxime Riparda8bbc202017-02-27 18:22:01 +010083#define cpu_to_be32 htobe32
默默8ada1342019-03-31 16:07:03 +080084#endif
Maxime Riparda8bbc202017-02-27 18:22:01 +010085#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
86#define kmalloc(size, flags) malloc(size)
87#define kzalloc(size, flags) calloc(1, size)
88#define kfree free
89#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
90#endif
91
Christian Hitz55f7bca2011-10-12 09:31:59 +020092#include <asm/byteorder.h>
93#include <linux/bch.h>
94
95#if defined(CONFIG_BCH_CONST_PARAMS)
96#define GF_M(_p) (CONFIG_BCH_CONST_M)
97#define GF_T(_p) (CONFIG_BCH_CONST_T)
98#define GF_N(_p) ((1 << (CONFIG_BCH_CONST_M))-1)
99#else
100#define GF_M(_p) ((_p)->m)
101#define GF_T(_p) ((_p)->t)
102#define GF_N(_p) ((_p)->n)
103#endif
104
105#define BCH_ECC_WORDS(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 32)
106#define BCH_ECC_BYTES(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 8)
107
108#ifndef dbg
109#define dbg(_fmt, args...) do {} while (0)
110#endif
111
112/*
113 * represent a polynomial over GF(2^m)
114 */
115struct gf_poly {
116 unsigned int deg; /* polynomial degree */
117 unsigned int c[0]; /* polynomial terms */
118};
119
120/* given its degree, compute a polynomial size in bytes */
121#define GF_POLY_SZ(_d) (sizeof(struct gf_poly)+((_d)+1)*sizeof(unsigned int))
122
123/* polynomial of degree 1 */
124struct gf_poly_deg1 {
125 struct gf_poly poly;
126 unsigned int c[2];
127};
128
Maxime Riparda8bbc202017-02-27 18:22:01 +0100129#ifdef USE_HOSTCC
默默8ada1342019-03-31 16:07:03 +0800130#if !defined(__DragonFly__) && !defined(__FreeBSD__) && !defined(__APPLE__)
Maxime Riparda8bbc202017-02-27 18:22:01 +0100131static int fls(int x)
132{
133 int r = 32;
134
135 if (!x)
136 return 0;
137 if (!(x & 0xffff0000u)) {
138 x <<= 16;
139 r -= 16;
140 }
141 if (!(x & 0xff000000u)) {
142 x <<= 8;
143 r -= 8;
144 }
145 if (!(x & 0xf0000000u)) {
146 x <<= 4;
147 r -= 4;
148 }
149 if (!(x & 0xc0000000u)) {
150 x <<= 2;
151 r -= 2;
152 }
153 if (!(x & 0x80000000u)) {
154 x <<= 1;
155 r -= 1;
156 }
157 return r;
158}
159#endif
Emmanuel Vadot3fae8e52017-06-20 09:02:29 +0200160#endif
Maxime Riparda8bbc202017-02-27 18:22:01 +0100161
Christian Hitz55f7bca2011-10-12 09:31:59 +0200162/*
163 * same as encode_bch(), but process input data one byte at a time
164 */
165static void encode_bch_unaligned(struct bch_control *bch,
166 const unsigned char *data, unsigned int len,
167 uint32_t *ecc)
168{
169 int i;
170 const uint32_t *p;
171 const int l = BCH_ECC_WORDS(bch)-1;
172
173 while (len--) {
174 p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(*data++)) & 0xff);
175
176 for (i = 0; i < l; i++)
177 ecc[i] = ((ecc[i] << 8)|(ecc[i+1] >> 24))^(*p++);
178
179 ecc[l] = (ecc[l] << 8)^(*p);
180 }
181}
182
183/*
184 * convert ecc bytes to aligned, zero-padded 32-bit ecc words
185 */
186static void load_ecc8(struct bch_control *bch, uint32_t *dst,
187 const uint8_t *src)
188{
189 uint8_t pad[4] = {0, 0, 0, 0};
190 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1;
191
192 for (i = 0; i < nwords; i++, src += 4)
193 dst[i] = (src[0] << 24)|(src[1] << 16)|(src[2] << 8)|src[3];
194
195 memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords);
196 dst[nwords] = (pad[0] << 24)|(pad[1] << 16)|(pad[2] << 8)|pad[3];
197}
198
199/*
200 * convert 32-bit ecc words to ecc bytes
201 */
202static void store_ecc8(struct bch_control *bch, uint8_t *dst,
203 const uint32_t *src)
204{
205 uint8_t pad[4];
206 unsigned int i, nwords = BCH_ECC_WORDS(bch)-1;
207
208 for (i = 0; i < nwords; i++) {
209 *dst++ = (src[i] >> 24);
210 *dst++ = (src[i] >> 16) & 0xff;
211 *dst++ = (src[i] >> 8) & 0xff;
212 *dst++ = (src[i] >> 0) & 0xff;
213 }
214 pad[0] = (src[nwords] >> 24);
215 pad[1] = (src[nwords] >> 16) & 0xff;
216 pad[2] = (src[nwords] >> 8) & 0xff;
217 pad[3] = (src[nwords] >> 0) & 0xff;
218 memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords);
219}
220
221/**
222 * encode_bch - calculate BCH ecc parity of data
223 * @bch: BCH control structure
224 * @data: data to encode
225 * @len: data length in bytes
226 * @ecc: ecc parity data, must be initialized by caller
227 *
228 * The @ecc parity array is used both as input and output parameter, in order to
229 * allow incremental computations. It should be of the size indicated by member
230 * @ecc_bytes of @bch, and should be initialized to 0 before the first call.
231 *
232 * The exact number of computed ecc parity bits is given by member @ecc_bits of
233 * @bch; it may be less than m*t for large values of t.
234 */
235void encode_bch(struct bch_control *bch, const uint8_t *data,
236 unsigned int len, uint8_t *ecc)
237{
238 const unsigned int l = BCH_ECC_WORDS(bch)-1;
239 unsigned int i, mlen;
240 unsigned long m;
241 uint32_t w, r[l+1];
242 const uint32_t * const tab0 = bch->mod8_tab;
243 const uint32_t * const tab1 = tab0 + 256*(l+1);
244 const uint32_t * const tab2 = tab1 + 256*(l+1);
245 const uint32_t * const tab3 = tab2 + 256*(l+1);
246 const uint32_t *pdata, *p0, *p1, *p2, *p3;
247
248 if (ecc) {
249 /* load ecc parity bytes into internal 32-bit buffer */
250 load_ecc8(bch, bch->ecc_buf, ecc);
251 } else {
252 memset(bch->ecc_buf, 0, sizeof(r));
253 }
254
255 /* process first unaligned data bytes */
256 m = ((unsigned long)data) & 3;
257 if (m) {
258 mlen = (len < (4-m)) ? len : 4-m;
259 encode_bch_unaligned(bch, data, mlen, bch->ecc_buf);
260 data += mlen;
261 len -= mlen;
262 }
263
264 /* process 32-bit aligned data words */
265 pdata = (uint32_t *)data;
266 mlen = len/4;
267 data += 4*mlen;
268 len -= 4*mlen;
269 memcpy(r, bch->ecc_buf, sizeof(r));
270
271 /*
272 * split each 32-bit word into 4 polynomials of weight 8 as follows:
273 *
274 * 31 ...24 23 ...16 15 ... 8 7 ... 0
275 * xxxxxxxx yyyyyyyy zzzzzzzz tttttttt
276 * tttttttt mod g = r0 (precomputed)
277 * zzzzzzzz 00000000 mod g = r1 (precomputed)
278 * yyyyyyyy 00000000 00000000 mod g = r2 (precomputed)
279 * xxxxxxxx 00000000 00000000 00000000 mod g = r3 (precomputed)
280 * xxxxxxxx yyyyyyyy zzzzzzzz tttttttt mod g = r0^r1^r2^r3
281 */
282 while (mlen--) {
283 /* input data is read in big-endian format */
284 w = r[0]^cpu_to_be32(*pdata++);
285 p0 = tab0 + (l+1)*((w >> 0) & 0xff);
286 p1 = tab1 + (l+1)*((w >> 8) & 0xff);
287 p2 = tab2 + (l+1)*((w >> 16) & 0xff);
288 p3 = tab3 + (l+1)*((w >> 24) & 0xff);
289
290 for (i = 0; i < l; i++)
291 r[i] = r[i+1]^p0[i]^p1[i]^p2[i]^p3[i];
292
293 r[l] = p0[l]^p1[l]^p2[l]^p3[l];
294 }
295 memcpy(bch->ecc_buf, r, sizeof(r));
296
297 /* process last unaligned bytes */
298 if (len)
299 encode_bch_unaligned(bch, data, len, bch->ecc_buf);
300
301 /* store ecc parity bytes into original parity buffer */
302 if (ecc)
303 store_ecc8(bch, ecc, bch->ecc_buf);
304}
305
306static inline int modulo(struct bch_control *bch, unsigned int v)
307{
308 const unsigned int n = GF_N(bch);
309 while (v >= n) {
310 v -= n;
311 v = (v & n) + (v >> GF_M(bch));
312 }
313 return v;
314}
315
316/*
317 * shorter and faster modulo function, only works when v < 2N.
318 */
319static inline int mod_s(struct bch_control *bch, unsigned int v)
320{
321 const unsigned int n = GF_N(bch);
322 return (v < n) ? v : v-n;
323}
324
325static inline int deg(unsigned int poly)
326{
327 /* polynomial degree is the most-significant bit index */
328 return fls(poly)-1;
329}
330
331static inline int parity(unsigned int x)
332{
333 /*
334 * public domain code snippet, lifted from
335 * http://www-graphics.stanford.edu/~seander/bithacks.html
336 */
337 x ^= x >> 1;
338 x ^= x >> 2;
339 x = (x & 0x11111111U) * 0x11111111U;
340 return (x >> 28) & 1;
341}
342
343/* Galois field basic operations: multiply, divide, inverse, etc. */
344
345static inline unsigned int gf_mul(struct bch_control *bch, unsigned int a,
346 unsigned int b)
347{
348 return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+
349 bch->a_log_tab[b])] : 0;
350}
351
352static inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a)
353{
354 return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0;
355}
356
357static inline unsigned int gf_div(struct bch_control *bch, unsigned int a,
358 unsigned int b)
359{
360 return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+
361 GF_N(bch)-bch->a_log_tab[b])] : 0;
362}
363
364static inline unsigned int gf_inv(struct bch_control *bch, unsigned int a)
365{
366 return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]];
367}
368
369static inline unsigned int a_pow(struct bch_control *bch, int i)
370{
371 return bch->a_pow_tab[modulo(bch, i)];
372}
373
374static inline int a_log(struct bch_control *bch, unsigned int x)
375{
376 return bch->a_log_tab[x];
377}
378
379static inline int a_ilog(struct bch_control *bch, unsigned int x)
380{
381 return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]);
382}
383
384/*
385 * compute 2t syndromes of ecc polynomial, i.e. ecc(a^j) for j=1..2t
386 */
387static void compute_syndromes(struct bch_control *bch, uint32_t *ecc,
388 unsigned int *syn)
389{
390 int i, j, s;
391 unsigned int m;
392 uint32_t poly;
393 const int t = GF_T(bch);
394
395 s = bch->ecc_bits;
396
397 /* make sure extra bits in last ecc word are cleared */
398 m = ((unsigned int)s) & 31;
399 if (m)
400 ecc[s/32] &= ~((1u << (32-m))-1);
401 memset(syn, 0, 2*t*sizeof(*syn));
402
403 /* compute v(a^j) for j=1 .. 2t-1 */
404 do {
405 poly = *ecc++;
406 s -= 32;
407 while (poly) {
408 i = deg(poly);
409 for (j = 0; j < 2*t; j += 2)
410 syn[j] ^= a_pow(bch, (j+1)*(i+s));
411
412 poly ^= (1 << i);
413 }
414 } while (s > 0);
415
416 /* v(a^(2j)) = v(a^j)^2 */
417 for (j = 0; j < t; j++)
418 syn[2*j+1] = gf_sqr(bch, syn[j]);
419}
420
421static void gf_poly_copy(struct gf_poly *dst, struct gf_poly *src)
422{
423 memcpy(dst, src, GF_POLY_SZ(src->deg));
424}
425
426static int compute_error_locator_polynomial(struct bch_control *bch,
427 const unsigned int *syn)
428{
429 const unsigned int t = GF_T(bch);
430 const unsigned int n = GF_N(bch);
431 unsigned int i, j, tmp, l, pd = 1, d = syn[0];
432 struct gf_poly *elp = bch->elp;
433 struct gf_poly *pelp = bch->poly_2t[0];
434 struct gf_poly *elp_copy = bch->poly_2t[1];
435 int k, pp = -1;
436
437 memset(pelp, 0, GF_POLY_SZ(2*t));
438 memset(elp, 0, GF_POLY_SZ(2*t));
439
440 pelp->deg = 0;
441 pelp->c[0] = 1;
442 elp->deg = 0;
443 elp->c[0] = 1;
444
445 /* use simplified binary Berlekamp-Massey algorithm */
446 for (i = 0; (i < t) && (elp->deg <= t); i++) {
447 if (d) {
448 k = 2*i-pp;
449 gf_poly_copy(elp_copy, elp);
450 /* e[i+1](X) = e[i](X)+di*dp^-1*X^2(i-p)*e[p](X) */
451 tmp = a_log(bch, d)+n-a_log(bch, pd);
452 for (j = 0; j <= pelp->deg; j++) {
453 if (pelp->c[j]) {
454 l = a_log(bch, pelp->c[j]);
455 elp->c[j+k] ^= a_pow(bch, tmp+l);
456 }
457 }
458 /* compute l[i+1] = max(l[i]->c[l[p]+2*(i-p]) */
459 tmp = pelp->deg+k;
460 if (tmp > elp->deg) {
461 elp->deg = tmp;
462 gf_poly_copy(pelp, elp_copy);
463 pd = d;
464 pp = 2*i;
465 }
466 }
467 /* di+1 = S(2i+3)+elp[i+1].1*S(2i+2)+...+elp[i+1].lS(2i+3-l) */
468 if (i < t-1) {
469 d = syn[2*i+2];
470 for (j = 1; j <= elp->deg; j++)
471 d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]);
472 }
473 }
474 dbg("elp=%s\n", gf_poly_str(elp));
475 return (elp->deg > t) ? -1 : (int)elp->deg;
476}
477
478/*
479 * solve a m x m linear system in GF(2) with an expected number of solutions,
480 * and return the number of found solutions
481 */
482static int solve_linear_system(struct bch_control *bch, unsigned int *rows,
483 unsigned int *sol, int nsol)
484{
485 const int m = GF_M(bch);
486 unsigned int tmp, mask;
487 int rem, c, r, p, k, param[m];
488
489 k = 0;
490 mask = 1 << m;
491
492 /* Gaussian elimination */
493 for (c = 0; c < m; c++) {
494 rem = 0;
495 p = c-k;
496 /* find suitable row for elimination */
497 for (r = p; r < m; r++) {
498 if (rows[r] & mask) {
499 if (r != p) {
500 tmp = rows[r];
501 rows[r] = rows[p];
502 rows[p] = tmp;
503 }
504 rem = r+1;
505 break;
506 }
507 }
508 if (rem) {
509 /* perform elimination on remaining rows */
510 tmp = rows[p];
511 for (r = rem; r < m; r++) {
512 if (rows[r] & mask)
513 rows[r] ^= tmp;
514 }
515 } else {
516 /* elimination not needed, store defective row index */
517 param[k++] = c;
518 }
519 mask >>= 1;
520 }
521 /* rewrite system, inserting fake parameter rows */
522 if (k > 0) {
523 p = k;
524 for (r = m-1; r >= 0; r--) {
525 if ((r > m-1-k) && rows[r])
526 /* system has no solution */
527 return 0;
528
529 rows[r] = (p && (r == param[p-1])) ?
530 p--, 1u << (m-r) : rows[r-p];
531 }
532 }
533
534 if (nsol != (1 << k))
535 /* unexpected number of solutions */
536 return 0;
537
538 for (p = 0; p < nsol; p++) {
539 /* set parameters for p-th solution */
540 for (c = 0; c < k; c++)
541 rows[param[c]] = (rows[param[c]] & ~1)|((p >> c) & 1);
542
543 /* compute unique solution */
544 tmp = 0;
545 for (r = m-1; r >= 0; r--) {
546 mask = rows[r] & (tmp|1);
547 tmp |= parity(mask) << (m-r);
548 }
549 sol[p] = tmp >> 1;
550 }
551 return nsol;
552}
553
554/*
555 * this function builds and solves a linear system for finding roots of a degree
556 * 4 affine monic polynomial X^4+aX^2+bX+c over GF(2^m).
557 */
558static int find_affine4_roots(struct bch_control *bch, unsigned int a,
559 unsigned int b, unsigned int c,
560 unsigned int *roots)
561{
562 int i, j, k;
563 const int m = GF_M(bch);
564 unsigned int mask = 0xff, t, rows[16] = {0,};
565
566 j = a_log(bch, b);
567 k = a_log(bch, a);
568 rows[0] = c;
569
570 /* buid linear system to solve X^4+aX^2+bX+c = 0 */
571 for (i = 0; i < m; i++) {
572 rows[i+1] = bch->a_pow_tab[4*i]^
573 (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^
574 (b ? bch->a_pow_tab[mod_s(bch, j)] : 0);
575 j++;
576 k += 2;
577 }
578 /*
579 * transpose 16x16 matrix before passing it to linear solver
580 * warning: this code assumes m < 16
581 */
582 for (j = 8; j != 0; j >>= 1, mask ^= (mask << j)) {
583 for (k = 0; k < 16; k = (k+j+1) & ~j) {
584 t = ((rows[k] >> j)^rows[k+j]) & mask;
585 rows[k] ^= (t << j);
586 rows[k+j] ^= t;
587 }
588 }
589 return solve_linear_system(bch, rows, roots, 4);
590}
591
592/*
593 * compute root r of a degree 1 polynomial over GF(2^m) (returned as log(1/r))
594 */
595static int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly,
596 unsigned int *roots)
597{
598 int n = 0;
599
600 if (poly->c[0])
601 /* poly[X] = bX+c with c!=0, root=c/b */
602 roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+
603 bch->a_log_tab[poly->c[1]]);
604 return n;
605}
606
607/*
608 * compute roots of a degree 2 polynomial over GF(2^m)
609 */
610static int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly,
611 unsigned int *roots)
612{
613 int n = 0, i, l0, l1, l2;
614 unsigned int u, v, r;
615
616 if (poly->c[0] && poly->c[1]) {
617
618 l0 = bch->a_log_tab[poly->c[0]];
619 l1 = bch->a_log_tab[poly->c[1]];
620 l2 = bch->a_log_tab[poly->c[2]];
621
622 /* using z=a/bX, transform aX^2+bX+c into z^2+z+u (u=ac/b^2) */
623 u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1));
624 /*
625 * let u = sum(li.a^i) i=0..m-1; then compute r = sum(li.xi):
626 * r^2+r = sum(li.(xi^2+xi)) = sum(li.(a^i+Tr(a^i).a^k)) =
627 * u + sum(li.Tr(a^i).a^k) = u+a^k.Tr(sum(li.a^i)) = u+a^k.Tr(u)
628 * i.e. r and r+1 are roots iff Tr(u)=0
629 */
630 r = 0;
631 v = u;
632 while (v) {
633 i = deg(v);
634 r ^= bch->xi_tab[i];
635 v ^= (1 << i);
636 }
637 /* verify root */
638 if ((gf_sqr(bch, r)^r) == u) {
639 /* reverse z=a/bX transformation and compute log(1/r) */
640 roots[n++] = modulo(bch, 2*GF_N(bch)-l1-
641 bch->a_log_tab[r]+l2);
642 roots[n++] = modulo(bch, 2*GF_N(bch)-l1-
643 bch->a_log_tab[r^1]+l2);
644 }
645 }
646 return n;
647}
648
649/*
650 * compute roots of a degree 3 polynomial over GF(2^m)
651 */
652static int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly,
653 unsigned int *roots)
654{
655 int i, n = 0;
656 unsigned int a, b, c, a2, b2, c2, e3, tmp[4];
657
658 if (poly->c[0]) {
659 /* transform polynomial into monic X^3 + a2X^2 + b2X + c2 */
660 e3 = poly->c[3];
661 c2 = gf_div(bch, poly->c[0], e3);
662 b2 = gf_div(bch, poly->c[1], e3);
663 a2 = gf_div(bch, poly->c[2], e3);
664
665 /* (X+a2)(X^3+a2X^2+b2X+c2) = X^4+aX^2+bX+c (affine) */
666 c = gf_mul(bch, a2, c2); /* c = a2c2 */
667 b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */
668 a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */
669
670 /* find the 4 roots of this affine polynomial */
671 if (find_affine4_roots(bch, a, b, c, tmp) == 4) {
672 /* remove a2 from final list of roots */
673 for (i = 0; i < 4; i++) {
674 if (tmp[i] != a2)
675 roots[n++] = a_ilog(bch, tmp[i]);
676 }
677 }
678 }
679 return n;
680}
681
682/*
683 * compute roots of a degree 4 polynomial over GF(2^m)
684 */
685static int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly,
686 unsigned int *roots)
687{
688 int i, l, n = 0;
689 unsigned int a, b, c, d, e = 0, f, a2, b2, c2, e4;
690
691 if (poly->c[0] == 0)
692 return 0;
693
694 /* transform polynomial into monic X^4 + aX^3 + bX^2 + cX + d */
695 e4 = poly->c[4];
696 d = gf_div(bch, poly->c[0], e4);
697 c = gf_div(bch, poly->c[1], e4);
698 b = gf_div(bch, poly->c[2], e4);
699 a = gf_div(bch, poly->c[3], e4);
700
701 /* use Y=1/X transformation to get an affine polynomial */
702 if (a) {
703 /* first, eliminate cX by using z=X+e with ae^2+c=0 */
704 if (c) {
705 /* compute e such that e^2 = c/a */
706 f = gf_div(bch, c, a);
707 l = a_log(bch, f);
708 l += (l & 1) ? GF_N(bch) : 0;
709 e = a_pow(bch, l/2);
710 /*
711 * use transformation z=X+e:
712 * z^4+e^4 + a(z^3+ez^2+e^2z+e^3) + b(z^2+e^2) +cz+ce+d
713 * z^4 + az^3 + (ae+b)z^2 + (ae^2+c)z+e^4+be^2+ae^3+ce+d
714 * z^4 + az^3 + (ae+b)z^2 + e^4+be^2+d
715 * z^4 + az^3 + b'z^2 + d'
716 */
717 d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d;
718 b = gf_mul(bch, a, e)^b;
719 }
720 /* now, use Y=1/X to get Y^4 + b/dY^2 + a/dY + 1/d */
721 if (d == 0)
722 /* assume all roots have multiplicity 1 */
723 return 0;
724
725 c2 = gf_inv(bch, d);
726 b2 = gf_div(bch, a, d);
727 a2 = gf_div(bch, b, d);
728 } else {
729 /* polynomial is already affine */
730 c2 = d;
731 b2 = c;
732 a2 = b;
733 }
734 /* find the 4 roots of this affine polynomial */
735 if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) {
736 for (i = 0; i < 4; i++) {
737 /* post-process roots (reverse transformations) */
738 f = a ? gf_inv(bch, roots[i]) : roots[i];
739 roots[i] = a_ilog(bch, f^e);
740 }
741 n = 4;
742 }
743 return n;
744}
745
746/*
747 * build monic, log-based representation of a polynomial
748 */
749static void gf_poly_logrep(struct bch_control *bch,
750 const struct gf_poly *a, int *rep)
751{
752 int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]);
753
754 /* represent 0 values with -1; warning, rep[d] is not set to 1 */
755 for (i = 0; i < d; i++)
756 rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1;
757}
758
759/*
760 * compute polynomial Euclidean division remainder in GF(2^m)[X]
761 */
762static void gf_poly_mod(struct bch_control *bch, struct gf_poly *a,
763 const struct gf_poly *b, int *rep)
764{
765 int la, p, m;
766 unsigned int i, j, *c = a->c;
767 const unsigned int d = b->deg;
768
769 if (a->deg < d)
770 return;
771
772 /* reuse or compute log representation of denominator */
773 if (!rep) {
774 rep = bch->cache;
775 gf_poly_logrep(bch, b, rep);
776 }
777
778 for (j = a->deg; j >= d; j--) {
779 if (c[j]) {
780 la = a_log(bch, c[j]);
781 p = j-d;
782 for (i = 0; i < d; i++, p++) {
783 m = rep[i];
784 if (m >= 0)
785 c[p] ^= bch->a_pow_tab[mod_s(bch,
786 m+la)];
787 }
788 }
789 }
790 a->deg = d-1;
791 while (!c[a->deg] && a->deg)
792 a->deg--;
793}
794
795/*
796 * compute polynomial Euclidean division quotient in GF(2^m)[X]
797 */
798static void gf_poly_div(struct bch_control *bch, struct gf_poly *a,
799 const struct gf_poly *b, struct gf_poly *q)
800{
801 if (a->deg >= b->deg) {
802 q->deg = a->deg-b->deg;
803 /* compute a mod b (modifies a) */
804 gf_poly_mod(bch, a, b, NULL);
805 /* quotient is stored in upper part of polynomial a */
806 memcpy(q->c, &a->c[b->deg], (1+q->deg)*sizeof(unsigned int));
807 } else {
808 q->deg = 0;
809 q->c[0] = 0;
810 }
811}
812
813/*
814 * compute polynomial GCD (Greatest Common Divisor) in GF(2^m)[X]
815 */
816static struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a,
817 struct gf_poly *b)
818{
819 struct gf_poly *tmp;
820
821 dbg("gcd(%s,%s)=", gf_poly_str(a), gf_poly_str(b));
822
823 if (a->deg < b->deg) {
824 tmp = b;
825 b = a;
826 a = tmp;
827 }
828
829 while (b->deg > 0) {
830 gf_poly_mod(bch, a, b, NULL);
831 tmp = b;
832 b = a;
833 a = tmp;
834 }
835
836 dbg("%s\n", gf_poly_str(a));
837
838 return a;
839}
840
841/*
842 * Given a polynomial f and an integer k, compute Tr(a^kX) mod f
843 * This is used in Berlekamp Trace algorithm for splitting polynomials
844 */
845static void compute_trace_bk_mod(struct bch_control *bch, int k,
846 const struct gf_poly *f, struct gf_poly *z,
847 struct gf_poly *out)
848{
849 const int m = GF_M(bch);
850 int i, j;
851
852 /* z contains z^2j mod f */
853 z->deg = 1;
854 z->c[0] = 0;
855 z->c[1] = bch->a_pow_tab[k];
856
857 out->deg = 0;
858 memset(out, 0, GF_POLY_SZ(f->deg));
859
860 /* compute f log representation only once */
861 gf_poly_logrep(bch, f, bch->cache);
862
863 for (i = 0; i < m; i++) {
864 /* add a^(k*2^i)(z^(2^i) mod f) and compute (z^(2^i) mod f)^2 */
865 for (j = z->deg; j >= 0; j--) {
866 out->c[j] ^= z->c[j];
867 z->c[2*j] = gf_sqr(bch, z->c[j]);
868 z->c[2*j+1] = 0;
869 }
870 if (z->deg > out->deg)
871 out->deg = z->deg;
872
873 if (i < m-1) {
874 z->deg *= 2;
875 /* z^(2(i+1)) mod f = (z^(2^i) mod f)^2 mod f */
876 gf_poly_mod(bch, z, f, bch->cache);
877 }
878 }
879 while (!out->c[out->deg] && out->deg)
880 out->deg--;
881
882 dbg("Tr(a^%d.X) mod f = %s\n", k, gf_poly_str(out));
883}
884
885/*
886 * factor a polynomial using Berlekamp Trace algorithm (BTA)
887 */
888static void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f,
889 struct gf_poly **g, struct gf_poly **h)
890{
891 struct gf_poly *f2 = bch->poly_2t[0];
892 struct gf_poly *q = bch->poly_2t[1];
893 struct gf_poly *tk = bch->poly_2t[2];
894 struct gf_poly *z = bch->poly_2t[3];
895 struct gf_poly *gcd;
896
897 dbg("factoring %s...\n", gf_poly_str(f));
898
899 *g = f;
900 *h = NULL;
901
902 /* tk = Tr(a^k.X) mod f */
903 compute_trace_bk_mod(bch, k, f, z, tk);
904
905 if (tk->deg > 0) {
906 /* compute g = gcd(f, tk) (destructive operation) */
907 gf_poly_copy(f2, f);
908 gcd = gf_poly_gcd(bch, f2, tk);
909 if (gcd->deg < f->deg) {
910 /* compute h=f/gcd(f,tk); this will modify f and q */
911 gf_poly_div(bch, f, gcd, q);
912 /* store g and h in-place (clobbering f) */
913 *h = &((struct gf_poly_deg1 *)f)[gcd->deg].poly;
914 gf_poly_copy(*g, gcd);
915 gf_poly_copy(*h, q);
916 }
917 }
918}
919
920/*
921 * find roots of a polynomial, using BTZ algorithm; see the beginning of this
922 * file for details
923 */
924static int find_poly_roots(struct bch_control *bch, unsigned int k,
925 struct gf_poly *poly, unsigned int *roots)
926{
927 int cnt;
928 struct gf_poly *f1, *f2;
929
930 switch (poly->deg) {
931 /* handle low degree polynomials with ad hoc techniques */
932 case 1:
933 cnt = find_poly_deg1_roots(bch, poly, roots);
934 break;
935 case 2:
936 cnt = find_poly_deg2_roots(bch, poly, roots);
937 break;
938 case 3:
939 cnt = find_poly_deg3_roots(bch, poly, roots);
940 break;
941 case 4:
942 cnt = find_poly_deg4_roots(bch, poly, roots);
943 break;
944 default:
945 /* factor polynomial using Berlekamp Trace Algorithm (BTA) */
946 cnt = 0;
947 if (poly->deg && (k <= GF_M(bch))) {
948 factor_polynomial(bch, k, poly, &f1, &f2);
949 if (f1)
950 cnt += find_poly_roots(bch, k+1, f1, roots);
951 if (f2)
952 cnt += find_poly_roots(bch, k+1, f2, roots+cnt);
953 }
954 break;
955 }
956 return cnt;
957}
958
959#if defined(USE_CHIEN_SEARCH)
960/*
961 * exhaustive root search (Chien) implementation - not used, included only for
962 * reference/comparison tests
963 */
964static int chien_search(struct bch_control *bch, unsigned int len,
965 struct gf_poly *p, unsigned int *roots)
966{
967 int m;
968 unsigned int i, j, syn, syn0, count = 0;
969 const unsigned int k = 8*len+bch->ecc_bits;
970
971 /* use a log-based representation of polynomial */
972 gf_poly_logrep(bch, p, bch->cache);
973 bch->cache[p->deg] = 0;
974 syn0 = gf_div(bch, p->c[0], p->c[p->deg]);
975
976 for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) {
977 /* compute elp(a^i) */
978 for (j = 1, syn = syn0; j <= p->deg; j++) {
979 m = bch->cache[j];
980 if (m >= 0)
981 syn ^= a_pow(bch, m+j*i);
982 }
983 if (syn == 0) {
984 roots[count++] = GF_N(bch)-i;
985 if (count == p->deg)
986 break;
987 }
988 }
989 return (count == p->deg) ? count : 0;
990}
991#define find_poly_roots(_p, _k, _elp, _loc) chien_search(_p, len, _elp, _loc)
992#endif /* USE_CHIEN_SEARCH */
993
994/**
995 * decode_bch - decode received codeword and find bit error locations
996 * @bch: BCH control structure
997 * @data: received data, ignored if @calc_ecc is provided
998 * @len: data length in bytes, must always be provided
999 * @recv_ecc: received ecc, if NULL then assume it was XORed in @calc_ecc
1000 * @calc_ecc: calculated ecc, if NULL then calc_ecc is computed from @data
1001 * @syn: hw computed syndrome data (if NULL, syndrome is calculated)
1002 * @errloc: output array of error locations
1003 *
1004 * Returns:
1005 * The number of errors found, or -EBADMSG if decoding failed, or -EINVAL if
1006 * invalid parameters were provided
1007 *
1008 * Depending on the available hw BCH support and the need to compute @calc_ecc
1009 * separately (using encode_bch()), this function should be called with one of
1010 * the following parameter configurations -
1011 *
1012 * by providing @data and @recv_ecc only:
1013 * decode_bch(@bch, @data, @len, @recv_ecc, NULL, NULL, @errloc)
1014 *
1015 * by providing @recv_ecc and @calc_ecc:
1016 * decode_bch(@bch, NULL, @len, @recv_ecc, @calc_ecc, NULL, @errloc)
1017 *
1018 * by providing ecc = recv_ecc XOR calc_ecc:
1019 * decode_bch(@bch, NULL, @len, NULL, ecc, NULL, @errloc)
1020 *
1021 * by providing syndrome results @syn:
1022 * decode_bch(@bch, NULL, @len, NULL, NULL, @syn, @errloc)
1023 *
1024 * Once decode_bch() has successfully returned with a positive value, error
1025 * locations returned in array @errloc should be interpreted as follows -
1026 *
1027 * if (errloc[n] >= 8*len), then n-th error is located in ecc (no need for
1028 * data correction)
1029 *
1030 * if (errloc[n] < 8*len), then n-th error is located in data and can be
1031 * corrected with statement data[errloc[n]/8] ^= 1 << (errloc[n] % 8);
1032 *
1033 * Note that this function does not perform any data correction by itself, it
1034 * merely indicates error locations.
1035 */
1036int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len,
1037 const uint8_t *recv_ecc, const uint8_t *calc_ecc,
1038 const unsigned int *syn, unsigned int *errloc)
1039{
1040 const unsigned int ecc_words = BCH_ECC_WORDS(bch);
1041 unsigned int nbits;
1042 int i, err, nroots;
1043 uint32_t sum;
1044
1045 /* sanity check: make sure data length can be handled */
1046 if (8*len > (bch->n-bch->ecc_bits))
1047 return -EINVAL;
1048
1049 /* if caller does not provide syndromes, compute them */
1050 if (!syn) {
1051 if (!calc_ecc) {
1052 /* compute received data ecc into an internal buffer */
1053 if (!data || !recv_ecc)
1054 return -EINVAL;
1055 encode_bch(bch, data, len, NULL);
1056 } else {
1057 /* load provided calculated ecc */
1058 load_ecc8(bch, bch->ecc_buf, calc_ecc);
1059 }
1060 /* load received ecc or assume it was XORed in calc_ecc */
1061 if (recv_ecc) {
1062 load_ecc8(bch, bch->ecc_buf2, recv_ecc);
1063 /* XOR received and calculated ecc */
1064 for (i = 0, sum = 0; i < (int)ecc_words; i++) {
1065 bch->ecc_buf[i] ^= bch->ecc_buf2[i];
1066 sum |= bch->ecc_buf[i];
1067 }
1068 if (!sum)
1069 /* no error found */
1070 return 0;
1071 }
1072 compute_syndromes(bch, bch->ecc_buf, bch->syn);
1073 syn = bch->syn;
1074 }
1075
1076 err = compute_error_locator_polynomial(bch, syn);
1077 if (err > 0) {
1078 nroots = find_poly_roots(bch, 1, bch->elp, errloc);
1079 if (err != nroots)
1080 err = -1;
1081 }
1082 if (err > 0) {
1083 /* post-process raw error locations for easier correction */
1084 nbits = (len*8)+bch->ecc_bits;
1085 for (i = 0; i < err; i++) {
1086 if (errloc[i] >= nbits) {
1087 err = -1;
1088 break;
1089 }
1090 errloc[i] = nbits-1-errloc[i];
1091 errloc[i] = (errloc[i] & ~7)|(7-(errloc[i] & 7));
1092 }
1093 }
1094 return (err >= 0) ? err : -EBADMSG;
1095}
1096
1097/*
1098 * generate Galois field lookup tables
1099 */
1100static int build_gf_tables(struct bch_control *bch, unsigned int poly)
1101{
1102 unsigned int i, x = 1;
1103 const unsigned int k = 1 << deg(poly);
1104
1105 /* primitive polynomial must be of degree m */
1106 if (k != (1u << GF_M(bch)))
1107 return -1;
1108
1109 for (i = 0; i < GF_N(bch); i++) {
1110 bch->a_pow_tab[i] = x;
1111 bch->a_log_tab[x] = i;
1112 if (i && (x == 1))
1113 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1114 return -1;
1115 x <<= 1;
1116 if (x & k)
1117 x ^= poly;
1118 }
1119 bch->a_pow_tab[GF_N(bch)] = 1;
1120 bch->a_log_tab[0] = 0;
1121
1122 return 0;
1123}
1124
1125/*
1126 * compute generator polynomial remainder tables for fast encoding
1127 */
1128static void build_mod8_tables(struct bch_control *bch, const uint32_t *g)
1129{
1130 int i, j, b, d;
1131 uint32_t data, hi, lo, *tab;
1132 const int l = BCH_ECC_WORDS(bch);
1133 const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32);
1134 const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32);
1135
1136 memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab));
1137
1138 for (i = 0; i < 256; i++) {
1139 /* p(X)=i is a small polynomial of weight <= 8 */
1140 for (b = 0; b < 4; b++) {
1141 /* we want to compute (p(X).X^(8*b+deg(g))) mod g(X) */
1142 tab = bch->mod8_tab + (b*256+i)*l;
1143 data = i << (8*b);
1144 while (data) {
1145 d = deg(data);
1146 /* subtract X^d.g(X) from p(X).X^(8*b+deg(g)) */
1147 data ^= g[0] >> (31-d);
1148 for (j = 0; j < ecclen; j++) {
1149 hi = (d < 31) ? g[j] << (d+1) : 0;
1150 lo = (j+1 < plen) ?
1151 g[j+1] >> (31-d) : 0;
1152 tab[j] ^= hi|lo;
1153 }
1154 }
1155 }
1156 }
1157}
1158
1159/*
1160 * build a base for factoring degree 2 polynomials
1161 */
1162static int build_deg2_base(struct bch_control *bch)
1163{
1164 const int m = GF_M(bch);
1165 int i, j, r;
1166 unsigned int sum, x, y, remaining, ak = 0, xi[m];
1167
1168 /* find k s.t. Tr(a^k) = 1 and 0 <= k < m */
1169 for (i = 0; i < m; i++) {
1170 for (j = 0, sum = 0; j < m; j++)
1171 sum ^= a_pow(bch, i*(1 << j));
1172
1173 if (sum) {
1174 ak = bch->a_pow_tab[i];
1175 break;
1176 }
1177 }
1178 /* find xi, i=0..m-1 such that xi^2+xi = a^i+Tr(a^i).a^k */
1179 remaining = m;
1180 memset(xi, 0, sizeof(xi));
1181
1182 for (x = 0; (x <= GF_N(bch)) && remaining; x++) {
1183 y = gf_sqr(bch, x)^x;
1184 for (i = 0; i < 2; i++) {
1185 r = a_log(bch, y);
1186 if (y && (r < m) && !xi[r]) {
1187 bch->xi_tab[r] = x;
1188 xi[r] = 1;
1189 remaining--;
1190 dbg("x%d = %x\n", r, x);
1191 break;
1192 }
1193 y ^= ak;
1194 }
1195 }
1196 /* should not happen but check anyway */
1197 return remaining ? -1 : 0;
1198}
1199
1200static void *bch_alloc(size_t size, int *err)
1201{
1202 void *ptr;
1203
1204 ptr = kmalloc(size, GFP_KERNEL);
1205 if (ptr == NULL)
1206 *err = 1;
1207 return ptr;
1208}
1209
1210/*
1211 * compute generator polynomial for given (m,t) parameters.
1212 */
1213static uint32_t *compute_generator_polynomial(struct bch_control *bch)
1214{
1215 const unsigned int m = GF_M(bch);
1216 const unsigned int t = GF_T(bch);
1217 int n, err = 0;
1218 unsigned int i, j, nbits, r, word, *roots;
1219 struct gf_poly *g;
1220 uint32_t *genpoly;
1221
1222 g = bch_alloc(GF_POLY_SZ(m*t), &err);
1223 roots = bch_alloc((bch->n+1)*sizeof(*roots), &err);
1224 genpoly = bch_alloc(DIV_ROUND_UP(m*t+1, 32)*sizeof(*genpoly), &err);
1225
1226 if (err) {
1227 kfree(genpoly);
1228 genpoly = NULL;
1229 goto finish;
1230 }
1231
1232 /* enumerate all roots of g(X) */
1233 memset(roots , 0, (bch->n+1)*sizeof(*roots));
1234 for (i = 0; i < t; i++) {
1235 for (j = 0, r = 2*i+1; j < m; j++) {
1236 roots[r] = 1;
1237 r = mod_s(bch, 2*r);
1238 }
1239 }
1240 /* build generator polynomial g(X) */
1241 g->deg = 0;
1242 g->c[0] = 1;
1243 for (i = 0; i < GF_N(bch); i++) {
1244 if (roots[i]) {
1245 /* multiply g(X) by (X+root) */
1246 r = bch->a_pow_tab[i];
1247 g->c[g->deg+1] = 1;
1248 for (j = g->deg; j > 0; j--)
1249 g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1];
1250
1251 g->c[0] = gf_mul(bch, g->c[0], r);
1252 g->deg++;
1253 }
1254 }
1255 /* store left-justified binary representation of g(X) */
1256 n = g->deg+1;
1257 i = 0;
1258
1259 while (n > 0) {
1260 nbits = (n > 32) ? 32 : n;
1261 for (j = 0, word = 0; j < nbits; j++) {
1262 if (g->c[n-1-j])
1263 word |= 1u << (31-j);
1264 }
1265 genpoly[i++] = word;
1266 n -= nbits;
1267 }
1268 bch->ecc_bits = g->deg;
1269
1270finish:
1271 kfree(g);
1272 kfree(roots);
1273
1274 return genpoly;
1275}
1276
1277/**
1278 * init_bch - initialize a BCH encoder/decoder
1279 * @m: Galois field order, should be in the range 5-15
1280 * @t: maximum error correction capability, in bits
1281 * @prim_poly: user-provided primitive polynomial (or 0 to use default)
1282 *
1283 * Returns:
1284 * a newly allocated BCH control structure if successful, NULL otherwise
1285 *
1286 * This initialization can take some time, as lookup tables are built for fast
1287 * encoding/decoding; make sure not to call this function from a time critical
1288 * path. Usually, init_bch() should be called on module/driver init and
1289 * free_bch() should be called to release memory on exit.
1290 *
1291 * You may provide your own primitive polynomial of degree @m in argument
1292 * @prim_poly, or let init_bch() use its default polynomial.
1293 *
1294 * Once init_bch() has successfully returned a pointer to a newly allocated
1295 * BCH control structure, ecc length in bytes is given by member @ecc_bytes of
1296 * the structure.
1297 */
1298struct bch_control *init_bch(int m, int t, unsigned int prim_poly)
1299{
1300 int err = 0;
1301 unsigned int i, words;
1302 uint32_t *genpoly;
1303 struct bch_control *bch = NULL;
1304
1305 const int min_m = 5;
1306 const int max_m = 15;
1307
1308 /* default primitive polynomials */
1309 static const unsigned int prim_poly_tab[] = {
1310 0x25, 0x43, 0x83, 0x11d, 0x211, 0x409, 0x805, 0x1053, 0x201b,
1311 0x402b, 0x8003,
1312 };
1313
1314#if defined(CONFIG_BCH_CONST_PARAMS)
1315 if ((m != (CONFIG_BCH_CONST_M)) || (t != (CONFIG_BCH_CONST_T))) {
1316 printk(KERN_ERR "bch encoder/decoder was configured to support "
1317 "parameters m=%d, t=%d only!\n",
1318 CONFIG_BCH_CONST_M, CONFIG_BCH_CONST_T);
1319 goto fail;
1320 }
1321#endif
1322 if ((m < min_m) || (m > max_m))
1323 /*
1324 * values of m greater than 15 are not currently supported;
1325 * supporting m > 15 would require changing table base type
1326 * (uint16_t) and a small patch in matrix transposition
1327 */
1328 goto fail;
1329
1330 /* sanity checks */
1331 if ((t < 1) || (m*t >= ((1 << m)-1)))
1332 /* invalid t value */
1333 goto fail;
1334
1335 /* select a primitive polynomial for generating GF(2^m) */
1336 if (prim_poly == 0)
1337 prim_poly = prim_poly_tab[m-min_m];
1338
1339 bch = kzalloc(sizeof(*bch), GFP_KERNEL);
1340 if (bch == NULL)
1341 goto fail;
1342
1343 bch->m = m;
1344 bch->t = t;
1345 bch->n = (1 << m)-1;
1346 words = DIV_ROUND_UP(m*t, 32);
1347 bch->ecc_bytes = DIV_ROUND_UP(m*t, 8);
1348 bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err);
1349 bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err);
1350 bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err);
1351 bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err);
1352 bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err);
1353 bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err);
1354 bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err);
1355 bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err);
1356 bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err);
1357
1358 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++)
1359 bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err);
1360
1361 if (err)
1362 goto fail;
1363
1364 err = build_gf_tables(bch, prim_poly);
1365 if (err)
1366 goto fail;
1367
1368 /* use generator polynomial for computing encoding tables */
1369 genpoly = compute_generator_polynomial(bch);
1370 if (genpoly == NULL)
1371 goto fail;
1372
1373 build_mod8_tables(bch, genpoly);
1374 kfree(genpoly);
1375
1376 err = build_deg2_base(bch);
1377 if (err)
1378 goto fail;
1379
1380 return bch;
1381
1382fail:
1383 free_bch(bch);
1384 return NULL;
1385}
1386
1387/**
1388 * free_bch - free the BCH control structure
1389 * @bch: BCH control structure to release
1390 */
1391void free_bch(struct bch_control *bch)
1392{
1393 unsigned int i;
1394
1395 if (bch) {
1396 kfree(bch->a_pow_tab);
1397 kfree(bch->a_log_tab);
1398 kfree(bch->mod8_tab);
1399 kfree(bch->ecc_buf);
1400 kfree(bch->ecc_buf2);
1401 kfree(bch->xi_tab);
1402 kfree(bch->syn);
1403 kfree(bch->cache);
1404 kfree(bch->elp);
1405
1406 for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++)
1407 kfree(bch->poly_2t[i]);
1408
1409 kfree(bch);
1410 }
1411}