xref: /OK3568_Linux_fs/kernel/lib/reed_solomon/test_rslib.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Tests for Generic Reed Solomon encoder / decoder library
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Written by Ferdinand Blomqvist
6*4882a593Smuzhiyun  * Based on previous work by Phil Karn, KA9Q
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun #include <linux/rslib.h>
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/moduleparam.h>
12*4882a593Smuzhiyun #include <linux/random.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun enum verbosity {
16*4882a593Smuzhiyun 	V_SILENT,
17*4882a593Smuzhiyun 	V_PROGRESS,
18*4882a593Smuzhiyun 	V_CSUMMARY
19*4882a593Smuzhiyun };
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun enum method {
22*4882a593Smuzhiyun 	CORR_BUFFER,
23*4882a593Smuzhiyun 	CALLER_SYNDROME,
24*4882a593Smuzhiyun 	IN_PLACE
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define __param(type, name, init, msg)		\
28*4882a593Smuzhiyun 	static type name = init;		\
29*4882a593Smuzhiyun 	module_param(name, type, 0444);		\
30*4882a593Smuzhiyun 	MODULE_PARM_DESC(name, msg)
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun __param(int, v, V_PROGRESS, "Verbosity level");
33*4882a593Smuzhiyun __param(int, ewsc, 1, "Erasures without symbol corruption");
34*4882a593Smuzhiyun __param(int, bc, 1, "Test for correct behaviour beyond error correction capacity");
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun struct etab {
37*4882a593Smuzhiyun 	int	symsize;
38*4882a593Smuzhiyun 	int	genpoly;
39*4882a593Smuzhiyun 	int	fcs;
40*4882a593Smuzhiyun 	int	prim;
41*4882a593Smuzhiyun 	int	nroots;
42*4882a593Smuzhiyun 	int	ntrials;
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /* List of codes to test */
46*4882a593Smuzhiyun static struct etab Tab[] = {
47*4882a593Smuzhiyun 	{2,	0x7,	1,	1,	1,	100000	},
48*4882a593Smuzhiyun 	{3,	0xb,	1,	1,	2,	100000	},
49*4882a593Smuzhiyun 	{3,	0xb,	1,	1,	3,	100000	},
50*4882a593Smuzhiyun 	{3,	0xb,	2,	1,	4,	100000	},
51*4882a593Smuzhiyun 	{4,	0x13,	1,	1,	4,	10000	},
52*4882a593Smuzhiyun 	{5,	0x25,	1,	1,	6,	1000	},
53*4882a593Smuzhiyun 	{6,	0x43,	3,	1,	8,	1000	},
54*4882a593Smuzhiyun 	{7,	0x89,	1,	1,	14,	500	},
55*4882a593Smuzhiyun 	{8,	0x11d,	1,	1,	30,	100	},
56*4882a593Smuzhiyun 	{8,	0x187,	112,	11,	32,	100	},
57*4882a593Smuzhiyun 	{9,	0x211,	1,	1,	33,	80	},
58*4882a593Smuzhiyun 	{0, 0, 0, 0, 0, 0},
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun struct estat {
63*4882a593Smuzhiyun 	int	dwrong;
64*4882a593Smuzhiyun 	int	irv;
65*4882a593Smuzhiyun 	int	wepos;
66*4882a593Smuzhiyun 	int	nwords;
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun struct bcstat {
70*4882a593Smuzhiyun 	int	rfail;
71*4882a593Smuzhiyun 	int	rsuccess;
72*4882a593Smuzhiyun 	int	noncw;
73*4882a593Smuzhiyun 	int	nwords;
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun struct wspace {
77*4882a593Smuzhiyun 	uint16_t	*c;		/* sent codeword */
78*4882a593Smuzhiyun 	uint16_t	*r;		/* received word */
79*4882a593Smuzhiyun 	uint16_t	*s;		/* syndrome */
80*4882a593Smuzhiyun 	uint16_t	*corr;		/* correction buffer */
81*4882a593Smuzhiyun 	int		*errlocs;
82*4882a593Smuzhiyun 	int		*derrlocs;
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun struct pad {
86*4882a593Smuzhiyun 	int	mult;
87*4882a593Smuzhiyun 	int	shift;
88*4882a593Smuzhiyun };
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun static struct pad pad_coef[] = {
91*4882a593Smuzhiyun 	{ 0, 0 },
92*4882a593Smuzhiyun 	{ 1, 2 },
93*4882a593Smuzhiyun 	{ 1, 1 },
94*4882a593Smuzhiyun 	{ 3, 2 },
95*4882a593Smuzhiyun 	{ 1, 0 },
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun 
free_ws(struct wspace * ws)98*4882a593Smuzhiyun static void free_ws(struct wspace *ws)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	if (!ws)
101*4882a593Smuzhiyun 		return;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	kfree(ws->errlocs);
104*4882a593Smuzhiyun 	kfree(ws->c);
105*4882a593Smuzhiyun 	kfree(ws);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
alloc_ws(struct rs_codec * rs)108*4882a593Smuzhiyun static struct wspace *alloc_ws(struct rs_codec *rs)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	int nroots = rs->nroots;
111*4882a593Smuzhiyun 	struct wspace *ws;
112*4882a593Smuzhiyun 	int nn = rs->nn;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	ws = kzalloc(sizeof(*ws), GFP_KERNEL);
115*4882a593Smuzhiyun 	if (!ws)
116*4882a593Smuzhiyun 		return NULL;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	ws->c = kmalloc_array(2 * (nn + nroots),
119*4882a593Smuzhiyun 				sizeof(uint16_t), GFP_KERNEL);
120*4882a593Smuzhiyun 	if (!ws->c)
121*4882a593Smuzhiyun 		goto err;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	ws->r = ws->c + nn;
124*4882a593Smuzhiyun 	ws->s = ws->r + nn;
125*4882a593Smuzhiyun 	ws->corr = ws->s + nroots;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	ws->errlocs = kmalloc_array(nn + nroots, sizeof(int), GFP_KERNEL);
128*4882a593Smuzhiyun 	if (!ws->errlocs)
129*4882a593Smuzhiyun 		goto err;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	ws->derrlocs = ws->errlocs + nn;
132*4882a593Smuzhiyun 	return ws;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun err:
135*4882a593Smuzhiyun 	free_ws(ws);
136*4882a593Smuzhiyun 	return NULL;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun  * Generates a random codeword and stores it in c. Generates random errors and
142*4882a593Smuzhiyun  * erasures, and stores the random word with errors in r. Erasure positions are
143*4882a593Smuzhiyun  * stored in derrlocs, while errlocs has one of three values in every position:
144*4882a593Smuzhiyun  *
145*4882a593Smuzhiyun  * 0 if there is no error in this position;
146*4882a593Smuzhiyun  * 1 if there is a symbol error in this position;
147*4882a593Smuzhiyun  * 2 if there is an erasure without symbol corruption.
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  * Returns the number of corrupted symbols.
150*4882a593Smuzhiyun  */
get_rcw_we(struct rs_control * rs,struct wspace * ws,int len,int errs,int eras)151*4882a593Smuzhiyun static int get_rcw_we(struct rs_control *rs, struct wspace *ws,
152*4882a593Smuzhiyun 			int len, int errs, int eras)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	int nroots = rs->codec->nroots;
155*4882a593Smuzhiyun 	int *derrlocs = ws->derrlocs;
156*4882a593Smuzhiyun 	int *errlocs = ws->errlocs;
157*4882a593Smuzhiyun 	int dlen = len - nroots;
158*4882a593Smuzhiyun 	int nn = rs->codec->nn;
159*4882a593Smuzhiyun 	uint16_t *c = ws->c;
160*4882a593Smuzhiyun 	uint16_t *r = ws->r;
161*4882a593Smuzhiyun 	int errval;
162*4882a593Smuzhiyun 	int errloc;
163*4882a593Smuzhiyun 	int i;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* Load c with random data and encode */
166*4882a593Smuzhiyun 	for (i = 0; i < dlen; i++)
167*4882a593Smuzhiyun 		c[i] = prandom_u32() & nn;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	memset(c + dlen, 0, nroots * sizeof(*c));
170*4882a593Smuzhiyun 	encode_rs16(rs, c, dlen, c + dlen, 0);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	/* Make copyand add errors and erasures */
173*4882a593Smuzhiyun 	memcpy(r, c, len * sizeof(*r));
174*4882a593Smuzhiyun 	memset(errlocs, 0, len * sizeof(*errlocs));
175*4882a593Smuzhiyun 	memset(derrlocs, 0, nroots * sizeof(*derrlocs));
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/* Generating random errors */
178*4882a593Smuzhiyun 	for (i = 0; i < errs; i++) {
179*4882a593Smuzhiyun 		do {
180*4882a593Smuzhiyun 			/* Error value must be nonzero */
181*4882a593Smuzhiyun 			errval = prandom_u32() & nn;
182*4882a593Smuzhiyun 		} while (errval == 0);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 		do {
185*4882a593Smuzhiyun 			/* Must not choose the same location twice */
186*4882a593Smuzhiyun 			errloc = prandom_u32() % len;
187*4882a593Smuzhiyun 		} while (errlocs[errloc] != 0);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 		errlocs[errloc] = 1;
190*4882a593Smuzhiyun 		r[errloc] ^= errval;
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/* Generating random erasures */
194*4882a593Smuzhiyun 	for (i = 0; i < eras; i++) {
195*4882a593Smuzhiyun 		do {
196*4882a593Smuzhiyun 			/* Must not choose the same location twice */
197*4882a593Smuzhiyun 			errloc = prandom_u32() % len;
198*4882a593Smuzhiyun 		} while (errlocs[errloc] != 0);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		derrlocs[i] = errloc;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 		if (ewsc && (prandom_u32() & 1)) {
203*4882a593Smuzhiyun 			/* Erasure with the symbol intact */
204*4882a593Smuzhiyun 			errlocs[errloc] = 2;
205*4882a593Smuzhiyun 		} else {
206*4882a593Smuzhiyun 			/* Erasure with corrupted symbol */
207*4882a593Smuzhiyun 			do {
208*4882a593Smuzhiyun 				/* Error value must be nonzero */
209*4882a593Smuzhiyun 				errval = prandom_u32() & nn;
210*4882a593Smuzhiyun 			} while (errval == 0);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 			errlocs[errloc] = 1;
213*4882a593Smuzhiyun 			r[errloc] ^= errval;
214*4882a593Smuzhiyun 			errs++;
215*4882a593Smuzhiyun 		}
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	return errs;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
fix_err(uint16_t * data,int nerrs,uint16_t * corr,int * errlocs)221*4882a593Smuzhiyun static void fix_err(uint16_t *data, int nerrs, uint16_t *corr, int *errlocs)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	int i;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	for (i = 0; i < nerrs; i++)
226*4882a593Smuzhiyun 		data[errlocs[i]] ^= corr[i];
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
compute_syndrome(struct rs_control * rsc,uint16_t * data,int len,uint16_t * syn)229*4882a593Smuzhiyun static void compute_syndrome(struct rs_control *rsc, uint16_t *data,
230*4882a593Smuzhiyun 				int len, uint16_t *syn)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	struct rs_codec *rs = rsc->codec;
233*4882a593Smuzhiyun 	uint16_t *alpha_to = rs->alpha_to;
234*4882a593Smuzhiyun 	uint16_t *index_of = rs->index_of;
235*4882a593Smuzhiyun 	int nroots = rs->nroots;
236*4882a593Smuzhiyun 	int prim = rs->prim;
237*4882a593Smuzhiyun 	int fcr = rs->fcr;
238*4882a593Smuzhiyun 	int i, j;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	/* Calculating syndrome */
241*4882a593Smuzhiyun 	for (i = 0; i < nroots; i++) {
242*4882a593Smuzhiyun 		syn[i] = data[0];
243*4882a593Smuzhiyun 		for (j = 1; j < len; j++) {
244*4882a593Smuzhiyun 			if (syn[i] == 0) {
245*4882a593Smuzhiyun 				syn[i] = data[j];
246*4882a593Smuzhiyun 			} else {
247*4882a593Smuzhiyun 				syn[i] = data[j] ^
248*4882a593Smuzhiyun 					alpha_to[rs_modnn(rs, index_of[syn[i]]
249*4882a593Smuzhiyun 						+ (fcr + i) * prim)];
250*4882a593Smuzhiyun 			}
251*4882a593Smuzhiyun 		}
252*4882a593Smuzhiyun 	}
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	/* Convert to index form */
255*4882a593Smuzhiyun 	for (i = 0; i < nroots; i++)
256*4882a593Smuzhiyun 		syn[i] = rs->index_of[syn[i]];
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun /* Test up to error correction capacity */
test_uc(struct rs_control * rs,int len,int errs,int eras,int trials,struct estat * stat,struct wspace * ws,int method)260*4882a593Smuzhiyun static void test_uc(struct rs_control *rs, int len, int errs,
261*4882a593Smuzhiyun 		int eras, int trials, struct estat *stat,
262*4882a593Smuzhiyun 		struct wspace *ws, int method)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	int dlen = len - rs->codec->nroots;
265*4882a593Smuzhiyun 	int *derrlocs = ws->derrlocs;
266*4882a593Smuzhiyun 	int *errlocs = ws->errlocs;
267*4882a593Smuzhiyun 	uint16_t *corr = ws->corr;
268*4882a593Smuzhiyun 	uint16_t *c = ws->c;
269*4882a593Smuzhiyun 	uint16_t *r = ws->r;
270*4882a593Smuzhiyun 	uint16_t *s = ws->s;
271*4882a593Smuzhiyun 	int derrs, nerrs;
272*4882a593Smuzhiyun 	int i, j;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	for (j = 0; j < trials; j++) {
275*4882a593Smuzhiyun 		nerrs = get_rcw_we(rs, ws, len, errs, eras);
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 		switch (method) {
278*4882a593Smuzhiyun 		case CORR_BUFFER:
279*4882a593Smuzhiyun 			derrs = decode_rs16(rs, r, r + dlen, dlen,
280*4882a593Smuzhiyun 					NULL, eras, derrlocs, 0, corr);
281*4882a593Smuzhiyun 			fix_err(r, derrs, corr, derrlocs);
282*4882a593Smuzhiyun 			break;
283*4882a593Smuzhiyun 		case CALLER_SYNDROME:
284*4882a593Smuzhiyun 			compute_syndrome(rs, r, len, s);
285*4882a593Smuzhiyun 			derrs = decode_rs16(rs, NULL, NULL, dlen,
286*4882a593Smuzhiyun 					s, eras, derrlocs, 0, corr);
287*4882a593Smuzhiyun 			fix_err(r, derrs, corr, derrlocs);
288*4882a593Smuzhiyun 			break;
289*4882a593Smuzhiyun 		case IN_PLACE:
290*4882a593Smuzhiyun 			derrs = decode_rs16(rs, r, r + dlen, dlen,
291*4882a593Smuzhiyun 					NULL, eras, derrlocs, 0, NULL);
292*4882a593Smuzhiyun 			break;
293*4882a593Smuzhiyun 		default:
294*4882a593Smuzhiyun 			continue;
295*4882a593Smuzhiyun 		}
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 		if (derrs != nerrs)
298*4882a593Smuzhiyun 			stat->irv++;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 		if (method != IN_PLACE) {
301*4882a593Smuzhiyun 			for (i = 0; i < derrs; i++) {
302*4882a593Smuzhiyun 				if (errlocs[derrlocs[i]] != 1)
303*4882a593Smuzhiyun 					stat->wepos++;
304*4882a593Smuzhiyun 			}
305*4882a593Smuzhiyun 		}
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 		if (memcmp(r, c, len * sizeof(*r)))
308*4882a593Smuzhiyun 			stat->dwrong++;
309*4882a593Smuzhiyun 	}
310*4882a593Smuzhiyun 	stat->nwords += trials;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
ex_rs_helper(struct rs_control * rs,struct wspace * ws,int len,int trials,int method)313*4882a593Smuzhiyun static int ex_rs_helper(struct rs_control *rs, struct wspace *ws,
314*4882a593Smuzhiyun 			int len, int trials, int method)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	static const char * const desc[] = {
317*4882a593Smuzhiyun 		"Testing correction buffer interface...",
318*4882a593Smuzhiyun 		"Testing with caller provided syndrome...",
319*4882a593Smuzhiyun 		"Testing in-place interface..."
320*4882a593Smuzhiyun 	};
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	struct estat stat = {0, 0, 0, 0};
323*4882a593Smuzhiyun 	int nroots = rs->codec->nroots;
324*4882a593Smuzhiyun 	int errs, eras, retval;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	if (v >= V_PROGRESS)
327*4882a593Smuzhiyun 		pr_info("  %s\n", desc[method]);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	for (errs = 0; errs <= nroots / 2; errs++)
330*4882a593Smuzhiyun 		for (eras = 0; eras <= nroots - 2 * errs; eras++)
331*4882a593Smuzhiyun 			test_uc(rs, len, errs, eras, trials, &stat, ws, method);
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	if (v >= V_CSUMMARY) {
334*4882a593Smuzhiyun 		pr_info("    Decodes wrong:        %d / %d\n",
335*4882a593Smuzhiyun 				stat.dwrong, stat.nwords);
336*4882a593Smuzhiyun 		pr_info("    Wrong return value:   %d / %d\n",
337*4882a593Smuzhiyun 				stat.irv, stat.nwords);
338*4882a593Smuzhiyun 		if (method != IN_PLACE)
339*4882a593Smuzhiyun 			pr_info("    Wrong error position: %d\n", stat.wepos);
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	retval = stat.dwrong + stat.wepos + stat.irv;
343*4882a593Smuzhiyun 	if (retval && v >= V_PROGRESS)
344*4882a593Smuzhiyun 		pr_warn("    FAIL: %d decoding failures!\n", retval);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	return retval;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
exercise_rs(struct rs_control * rs,struct wspace * ws,int len,int trials)349*4882a593Smuzhiyun static int exercise_rs(struct rs_control *rs, struct wspace *ws,
350*4882a593Smuzhiyun 		       int len, int trials)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	int retval = 0;
354*4882a593Smuzhiyun 	int i;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	if (v >= V_PROGRESS)
357*4882a593Smuzhiyun 		pr_info("Testing up to error correction capacity...\n");
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	for (i = 0; i <= IN_PLACE; i++)
360*4882a593Smuzhiyun 		retval |= ex_rs_helper(rs, ws, len, trials, i);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	return retval;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun /* Tests for correct behaviour beyond error correction capacity */
test_bc(struct rs_control * rs,int len,int errs,int eras,int trials,struct bcstat * stat,struct wspace * ws)366*4882a593Smuzhiyun static void test_bc(struct rs_control *rs, int len, int errs,
367*4882a593Smuzhiyun 		int eras, int trials, struct bcstat *stat,
368*4882a593Smuzhiyun 		struct wspace *ws)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	int nroots = rs->codec->nroots;
371*4882a593Smuzhiyun 	int dlen = len - nroots;
372*4882a593Smuzhiyun 	int *derrlocs = ws->derrlocs;
373*4882a593Smuzhiyun 	uint16_t *corr = ws->corr;
374*4882a593Smuzhiyun 	uint16_t *r = ws->r;
375*4882a593Smuzhiyun 	int derrs, j;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	for (j = 0; j < trials; j++) {
378*4882a593Smuzhiyun 		get_rcw_we(rs, ws, len, errs, eras);
379*4882a593Smuzhiyun 		derrs = decode_rs16(rs, r, r + dlen, dlen,
380*4882a593Smuzhiyun 				NULL, eras, derrlocs, 0, corr);
381*4882a593Smuzhiyun 		fix_err(r, derrs, corr, derrlocs);
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 		if (derrs >= 0) {
384*4882a593Smuzhiyun 			stat->rsuccess++;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 			/*
387*4882a593Smuzhiyun 			 * We check that the returned word is actually a
388*4882a593Smuzhiyun 			 * codeword. The obious way to do this would be to
389*4882a593Smuzhiyun 			 * compute the syndrome, but we don't want to replicate
390*4882a593Smuzhiyun 			 * that code here. However, all the codes are in
391*4882a593Smuzhiyun 			 * systematic form, and therefore we can encode the
392*4882a593Smuzhiyun 			 * returned word, and see whether the parity changes or
393*4882a593Smuzhiyun 			 * not.
394*4882a593Smuzhiyun 			 */
395*4882a593Smuzhiyun 			memset(corr, 0, nroots * sizeof(*corr));
396*4882a593Smuzhiyun 			encode_rs16(rs, r, dlen, corr, 0);
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 			if (memcmp(r + dlen, corr, nroots * sizeof(*corr)))
399*4882a593Smuzhiyun 				stat->noncw++;
400*4882a593Smuzhiyun 		} else {
401*4882a593Smuzhiyun 			stat->rfail++;
402*4882a593Smuzhiyun 		}
403*4882a593Smuzhiyun 	}
404*4882a593Smuzhiyun 	stat->nwords += trials;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
exercise_rs_bc(struct rs_control * rs,struct wspace * ws,int len,int trials)407*4882a593Smuzhiyun static int exercise_rs_bc(struct rs_control *rs, struct wspace *ws,
408*4882a593Smuzhiyun 			  int len, int trials)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun 	struct bcstat stat = {0, 0, 0, 0};
411*4882a593Smuzhiyun 	int nroots = rs->codec->nroots;
412*4882a593Smuzhiyun 	int errs, eras, cutoff;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	if (v >= V_PROGRESS)
415*4882a593Smuzhiyun 		pr_info("Testing beyond error correction capacity...\n");
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	for (errs = 1; errs <= nroots; errs++) {
418*4882a593Smuzhiyun 		eras = nroots - 2 * errs + 1;
419*4882a593Smuzhiyun 		if (eras < 0)
420*4882a593Smuzhiyun 			eras = 0;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 		cutoff = nroots <= len - errs ? nroots : len - errs;
423*4882a593Smuzhiyun 		for (; eras <= cutoff; eras++)
424*4882a593Smuzhiyun 			test_bc(rs, len, errs, eras, trials, &stat, ws);
425*4882a593Smuzhiyun 	}
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	if (v >= V_CSUMMARY) {
428*4882a593Smuzhiyun 		pr_info("  decoder gives up:        %d / %d\n",
429*4882a593Smuzhiyun 				stat.rfail, stat.nwords);
430*4882a593Smuzhiyun 		pr_info("  decoder returns success: %d / %d\n",
431*4882a593Smuzhiyun 				stat.rsuccess, stat.nwords);
432*4882a593Smuzhiyun 		pr_info("    not a codeword:        %d / %d\n",
433*4882a593Smuzhiyun 				stat.noncw, stat.rsuccess);
434*4882a593Smuzhiyun 	}
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	if (stat.noncw && v >= V_PROGRESS)
437*4882a593Smuzhiyun 		pr_warn("    FAIL: %d silent failures!\n", stat.noncw);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	return stat.noncw;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
run_exercise(struct etab * e)442*4882a593Smuzhiyun static int run_exercise(struct etab *e)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun 	int nn = (1 << e->symsize) - 1;
445*4882a593Smuzhiyun 	int kk = nn - e->nroots;
446*4882a593Smuzhiyun 	struct rs_control *rsc;
447*4882a593Smuzhiyun 	int retval = -ENOMEM;
448*4882a593Smuzhiyun 	int max_pad = kk - 1;
449*4882a593Smuzhiyun 	int prev_pad = -1;
450*4882a593Smuzhiyun 	struct wspace *ws;
451*4882a593Smuzhiyun 	int i;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	rsc = init_rs(e->symsize, e->genpoly, e->fcs, e->prim, e->nroots);
454*4882a593Smuzhiyun 	if (!rsc)
455*4882a593Smuzhiyun 		return retval;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	ws = alloc_ws(rsc->codec);
458*4882a593Smuzhiyun 	if (!ws)
459*4882a593Smuzhiyun 		goto err;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	retval = 0;
462*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(pad_coef); i++) {
463*4882a593Smuzhiyun 		int pad = (pad_coef[i].mult * max_pad) >> pad_coef[i].shift;
464*4882a593Smuzhiyun 		int len = nn - pad;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 		if (pad == prev_pad)
467*4882a593Smuzhiyun 			continue;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 		prev_pad = pad;
470*4882a593Smuzhiyun 		if (v >= V_PROGRESS) {
471*4882a593Smuzhiyun 			pr_info("Testing (%d,%d)_%d code...\n",
472*4882a593Smuzhiyun 					len, kk - pad, nn + 1);
473*4882a593Smuzhiyun 		}
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 		retval |= exercise_rs(rsc, ws, len, e->ntrials);
476*4882a593Smuzhiyun 		if (bc)
477*4882a593Smuzhiyun 			retval |= exercise_rs_bc(rsc, ws, len, e->ntrials);
478*4882a593Smuzhiyun 	}
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	free_ws(ws);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun err:
483*4882a593Smuzhiyun 	free_rs(rsc);
484*4882a593Smuzhiyun 	return retval;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun 
test_rslib_init(void)487*4882a593Smuzhiyun static int __init test_rslib_init(void)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	int i, fail = 0;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	for (i = 0; Tab[i].symsize != 0 ; i++) {
492*4882a593Smuzhiyun 		int retval;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 		retval = run_exercise(Tab + i);
495*4882a593Smuzhiyun 		if (retval < 0)
496*4882a593Smuzhiyun 			return -ENOMEM;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 		fail |= retval;
499*4882a593Smuzhiyun 	}
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	if (fail)
502*4882a593Smuzhiyun 		pr_warn("rslib: test failed\n");
503*4882a593Smuzhiyun 	else
504*4882a593Smuzhiyun 		pr_info("rslib: test ok\n");
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	return -EAGAIN; /* Fail will directly unload the module */
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun 
test_rslib_exit(void)509*4882a593Smuzhiyun static void __exit test_rslib_exit(void)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun module_init(test_rslib_init)
514*4882a593Smuzhiyun module_exit(test_rslib_exit)
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun MODULE_LICENSE("GPL");
517*4882a593Smuzhiyun MODULE_AUTHOR("Ferdinand Blomqvist");
518*4882a593Smuzhiyun MODULE_DESCRIPTION("Reed-Solomon library test");
519