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