Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Generic Reed Solomon encoder / decoder library
4 *
5 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
6 *
7 * Reed Solomon code lifted from reed solomon library written by Phil Karn
8 * Copyright 2002 Phil Karn, KA9Q
9 *
10 * Description:
11 *
12 * The generic Reed Solomon library provides runtime configurable
13 * encoding / decoding of RS codes.
14 *
15 * Each user must call init_rs to get a pointer to a rs_control structure
16 * for the given rs parameters. The control struct is unique per instance.
17 * It points to a codec which can be shared by multiple control structures.
18 * If a codec is newly allocated then the polynomial arrays for fast
19 * encoding / decoding are built. This can take some time so make sure not
20 * to call this function from a time critical path. Usually a module /
21 * driver should initialize the necessary rs_control structure on module /
22 * driver init and release it on exit.
23 *
24 * The encoding puts the calculated syndrome into a given syndrome buffer.
25 *
26 * The decoding is a two step process. The first step calculates the
27 * syndrome over the received (data + syndrome) and calls the second stage,
28 * which does the decoding / error correction itself. Many hw encoders
29 * provide a syndrome calculation over the received data + syndrome and can
30 * call the second stage directly.
31 */
32#include <linux/errno.h>
33#include <linux/kernel.h>
34#include <linux/init.h>
35#include <linux/module.h>
36#include <linux/rslib.h>
37#include <linux/slab.h>
38#include <linux/mutex.h>
39
40enum {
41 RS_DECODE_LAMBDA,
42 RS_DECODE_SYN,
43 RS_DECODE_B,
44 RS_DECODE_T,
45 RS_DECODE_OMEGA,
46 RS_DECODE_ROOT,
47 RS_DECODE_REG,
48 RS_DECODE_LOC,
49 RS_DECODE_NUM_BUFFERS
50};
51
52/* This list holds all currently allocated rs codec structures */
53static LIST_HEAD(codec_list);
54/* Protection for the list */
55static DEFINE_MUTEX(rslistlock);
56
57/**
58 * codec_init - Initialize a Reed-Solomon codec
59 * @symsize: symbol size, bits (1-8)
60 * @gfpoly: Field generator polynomial coefficients
61 * @gffunc: Field generator function
62 * @fcr: first root of RS code generator polynomial, index form
63 * @prim: primitive element to generate polynomial roots
64 * @nroots: RS code generator polynomial degree (number of roots)
65 * @gfp: GFP_ flags for allocations
66 *
67 * Allocate a codec structure and the polynom arrays for faster
68 * en/decoding. Fill the arrays according to the given parameters.
69 */
70static struct rs_codec *codec_init(int symsize, int gfpoly, int (*gffunc)(int),
71 int fcr, int prim, int nroots, gfp_t gfp)
72{
73 int i, j, sr, root, iprim;
74 struct rs_codec *rs;
75
76 rs = kzalloc(sizeof(*rs), gfp);
77 if (!rs)
78 return NULL;
79
80 INIT_LIST_HEAD(&rs->list);
81
82 rs->mm = symsize;
83 rs->nn = (1 << symsize) - 1;
84 rs->fcr = fcr;
85 rs->prim = prim;
86 rs->nroots = nroots;
87 rs->gfpoly = gfpoly;
88 rs->gffunc = gffunc;
89
90 /* Allocate the arrays */
91 rs->alpha_to = kmalloc_array(rs->nn + 1, sizeof(uint16_t), gfp);
92 if (rs->alpha_to == NULL)
93 goto err;
94
95 rs->index_of = kmalloc_array(rs->nn + 1, sizeof(uint16_t), gfp);
96 if (rs->index_of == NULL)
97 goto err;
98
99 rs->genpoly = kmalloc_array(rs->nroots + 1, sizeof(uint16_t), gfp);
100 if(rs->genpoly == NULL)
101 goto err;
102
103 /* Generate Galois field lookup tables */
104 rs->index_of[0] = rs->nn; /* log(zero) = -inf */
105 rs->alpha_to[rs->nn] = 0; /* alpha**-inf = 0 */
106 if (gfpoly) {
107 sr = 1;
108 for (i = 0; i < rs->nn; i++) {
109 rs->index_of[sr] = i;
110 rs->alpha_to[i] = sr;
111 sr <<= 1;
112 if (sr & (1 << symsize))
113 sr ^= gfpoly;
114 sr &= rs->nn;
115 }
116 } else {
117 sr = gffunc(0);
118 for (i = 0; i < rs->nn; i++) {
119 rs->index_of[sr] = i;
120 rs->alpha_to[i] = sr;
121 sr = gffunc(sr);
122 }
123 }
124 /* If it's not primitive, exit */
125 if(sr != rs->alpha_to[0])
126 goto err;
127
128 /* Find prim-th root of 1, used in decoding */
129 for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn);
130 /* prim-th root of 1, index form */
131 rs->iprim = iprim / prim;
132
133 /* Form RS code generator polynomial from its roots */
134 rs->genpoly[0] = 1;
135 for (i = 0, root = fcr * prim; i < nroots; i++, root += prim) {
136 rs->genpoly[i + 1] = 1;
137 /* Multiply rs->genpoly[] by @**(root + x) */
138 for (j = i; j > 0; j--) {
139 if (rs->genpoly[j] != 0) {
140 rs->genpoly[j] = rs->genpoly[j -1] ^
141 rs->alpha_to[rs_modnn(rs,
142 rs->index_of[rs->genpoly[j]] + root)];
143 } else
144 rs->genpoly[j] = rs->genpoly[j - 1];
145 }
146 /* rs->genpoly[0] can never be zero */
147 rs->genpoly[0] =
148 rs->alpha_to[rs_modnn(rs,
149 rs->index_of[rs->genpoly[0]] + root)];
150 }
151 /* convert rs->genpoly[] to index form for quicker encoding */
152 for (i = 0; i <= nroots; i++)
153 rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
154
155 rs->users = 1;
156 list_add(&rs->list, &codec_list);
157 return rs;
158
159err:
160 kfree(rs->genpoly);
161 kfree(rs->index_of);
162 kfree(rs->alpha_to);
163 kfree(rs);
164 return NULL;
165}
166
167
168/**
169 * free_rs - Free the rs control structure
170 * @rs: The control structure which is not longer used by the
171 * caller
172 *
173 * Free the control structure. If @rs is the last user of the associated
174 * codec, free the codec as well.
175 */
176void free_rs(struct rs_control *rs)
177{
178 struct rs_codec *cd;
179
180 if (!rs)
181 return;
182
183 cd = rs->codec;
184 mutex_lock(&rslistlock);
185 cd->users--;
186 if(!cd->users) {
187 list_del(&cd->list);
188 kfree(cd->alpha_to);
189 kfree(cd->index_of);
190 kfree(cd->genpoly);
191 kfree(cd);
192 }
193 mutex_unlock(&rslistlock);
194 kfree(rs);
195}
196EXPORT_SYMBOL_GPL(free_rs);
197
198/**
199 * init_rs_internal - Allocate rs control, find a matching codec or allocate a new one
200 * @symsize: the symbol size (number of bits)
201 * @gfpoly: the extended Galois field generator polynomial coefficients,
202 * with the 0th coefficient in the low order bit. The polynomial
203 * must be primitive;
204 * @gffunc: pointer to function to generate the next field element,
205 * or the multiplicative identity element if given 0. Used
206 * instead of gfpoly if gfpoly is 0
207 * @fcr: the first consecutive root of the rs code generator polynomial
208 * in index form
209 * @prim: primitive element to generate polynomial roots
210 * @nroots: RS code generator polynomial degree (number of roots)
211 * @gfp: GFP_ flags for allocations
212 */
213static struct rs_control *init_rs_internal(int symsize, int gfpoly,
214 int (*gffunc)(int), int fcr,
215 int prim, int nroots, gfp_t gfp)
216{
217 struct list_head *tmp;
218 struct rs_control *rs;
219 unsigned int bsize;
220
221 /* Sanity checks */
222 if (symsize < 1)
223 return NULL;
224 if (fcr < 0 || fcr >= (1<<symsize))
225 return NULL;
226 if (prim <= 0 || prim >= (1<<symsize))
227 return NULL;
228 if (nroots < 0 || nroots >= (1<<symsize))
229 return NULL;
230
231 /*
232 * The decoder needs buffers in each control struct instance to
233 * avoid variable size or large fixed size allocations on
234 * stack. Size the buffers to arrays of [nroots + 1].
235 */
236 bsize = sizeof(uint16_t) * RS_DECODE_NUM_BUFFERS * (nroots + 1);
237 rs = kzalloc(sizeof(*rs) + bsize, gfp);
238 if (!rs)
239 return NULL;
240
241 mutex_lock(&rslistlock);
242
243 /* Walk through the list and look for a matching entry */
244 list_for_each(tmp, &codec_list) {
245 struct rs_codec *cd = list_entry(tmp, struct rs_codec, list);
246
247 if (symsize != cd->mm)
248 continue;
249 if (gfpoly != cd->gfpoly)
250 continue;
251 if (gffunc != cd->gffunc)
252 continue;
253 if (fcr != cd->fcr)
254 continue;
255 if (prim != cd->prim)
256 continue;
257 if (nroots != cd->nroots)
258 continue;
259 /* We have a matching one already */
260 cd->users++;
261 rs->codec = cd;
262 goto out;
263 }
264
265 /* Create a new one */
266 rs->codec = codec_init(symsize, gfpoly, gffunc, fcr, prim, nroots, gfp);
267 if (!rs->codec) {
268 kfree(rs);
269 rs = NULL;
270 }
271out:
272 mutex_unlock(&rslistlock);
273 return rs;
274}
275
276/**
277 * init_rs_gfp - Create a RS control struct and initialize it
278 * @symsize: the symbol size (number of bits)
279 * @gfpoly: the extended Galois field generator polynomial coefficients,
280 * with the 0th coefficient in the low order bit. The polynomial
281 * must be primitive;
282 * @fcr: the first consecutive root of the rs code generator polynomial
283 * in index form
284 * @prim: primitive element to generate polynomial roots
285 * @nroots: RS code generator polynomial degree (number of roots)
286 * @gfp: Memory allocation flags.
287 */
288struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
289 int nroots, gfp_t gfp)
290{
291 return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots, gfp);
292}
293EXPORT_SYMBOL_GPL(init_rs_gfp);
294
295/**
296 * init_rs_non_canonical - Allocate rs control struct for fields with
297 * non-canonical representation
298 * @symsize: the symbol size (number of bits)
299 * @gffunc: pointer to function to generate the next field element,
300 * or the multiplicative identity element if given 0. Used
301 * instead of gfpoly if gfpoly is 0
302 * @fcr: the first consecutive root of the rs code generator polynomial
303 * in index form
304 * @prim: primitive element to generate polynomial roots
305 * @nroots: RS code generator polynomial degree (number of roots)
306 */
307struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int),
308 int fcr, int prim, int nroots)
309{
310 return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots,
311 GFP_KERNEL);
312}
313EXPORT_SYMBOL_GPL(init_rs_non_canonical);
314
315#ifdef CONFIG_REED_SOLOMON_ENC8
316/**
317 * encode_rs8 - Calculate the parity for data values (8bit data width)
318 * @rsc: the rs control structure
319 * @data: data field of a given type
320 * @len: data length
321 * @par: parity data, must be initialized by caller (usually all 0)
322 * @invmsk: invert data mask (will be xored on data)
323 *
324 * The parity uses a uint16_t data type to enable
325 * symbol size > 8. The calling code must take care of encoding of the
326 * syndrome result for storage itself.
327 */
328int encode_rs8(struct rs_control *rsc, uint8_t *data, int len, uint16_t *par,
329 uint16_t invmsk)
330{
331#include "encode_rs.c"
332}
333EXPORT_SYMBOL_GPL(encode_rs8);
334#endif
335
336#ifdef CONFIG_REED_SOLOMON_DEC8
337/**
338 * decode_rs8 - Decode codeword (8bit data width)
339 * @rsc: the rs control structure
340 * @data: data field of a given type
341 * @par: received parity data field
342 * @len: data length
343 * @s: syndrome data field, must be in index form
344 * (if NULL, syndrome is calculated)
345 * @no_eras: number of erasures
346 * @eras_pos: position of erasures, can be NULL
347 * @invmsk: invert data mask (will be xored on data, not on parity!)
348 * @corr: buffer to store correction bitmask on eras_pos
349 *
350 * The syndrome and parity uses a uint16_t data type to enable
351 * symbol size > 8. The calling code must take care of decoding of the
352 * syndrome result and the received parity before calling this code.
353 *
354 * Note: The rs_control struct @rsc contains buffers which are used for
355 * decoding, so the caller has to ensure that decoder invocations are
356 * serialized.
357 *
358 * Returns the number of corrected symbols or -EBADMSG for uncorrectable
359 * errors. The count includes errors in the parity.
360 */
361int decode_rs8(struct rs_control *rsc, uint8_t *data, uint16_t *par, int len,
362 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
363 uint16_t *corr)
364{
365#include "decode_rs.c"
366}
367EXPORT_SYMBOL_GPL(decode_rs8);
368#endif
369
370#ifdef CONFIG_REED_SOLOMON_ENC16
371/**
372 * encode_rs16 - Calculate the parity for data values (16bit data width)
373 * @rsc: the rs control structure
374 * @data: data field of a given type
375 * @len: data length
376 * @par: parity data, must be initialized by caller (usually all 0)
377 * @invmsk: invert data mask (will be xored on data, not on parity!)
378 *
379 * Each field in the data array contains up to symbol size bits of valid data.
380 */
381int encode_rs16(struct rs_control *rsc, uint16_t *data, int len, uint16_t *par,
382 uint16_t invmsk)
383{
384#include "encode_rs.c"
385}
386EXPORT_SYMBOL_GPL(encode_rs16);
387#endif
388
389#ifdef CONFIG_REED_SOLOMON_DEC16
390/**
391 * decode_rs16 - Decode codeword (16bit data width)
392 * @rsc: the rs control structure
393 * @data: data field of a given type
394 * @par: received parity data field
395 * @len: data length
396 * @s: syndrome data field, must be in index form
397 * (if NULL, syndrome is calculated)
398 * @no_eras: number of erasures
399 * @eras_pos: position of erasures, can be NULL
400 * @invmsk: invert data mask (will be xored on data, not on parity!)
401 * @corr: buffer to store correction bitmask on eras_pos
402 *
403 * Each field in the data array contains up to symbol size bits of valid data.
404 *
405 * Note: The rc_control struct @rsc contains buffers which are used for
406 * decoding, so the caller has to ensure that decoder invocations are
407 * serialized.
408 *
409 * Returns the number of corrected symbols or -EBADMSG for uncorrectable
410 * errors. The count includes errors in the parity.
411 */
412int decode_rs16(struct rs_control *rsc, uint16_t *data, uint16_t *par, int len,
413 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
414 uint16_t *corr)
415{
416#include "decode_rs.c"
417}
418EXPORT_SYMBOL_GPL(decode_rs16);
419#endif
420
421MODULE_LICENSE("GPL");
422MODULE_DESCRIPTION("Reed Solomon encoder/decoder");
423MODULE_AUTHOR("Phil Karn, Thomas Gleixner");
424
1/*
2 * lib/reed_solomon/reed_solomon.c
3 *
4 * Overview:
5 * Generic Reed Solomon encoder / decoder library
6 *
7 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
8 *
9 * Reed Solomon code lifted from reed solomon library written by Phil Karn
10 * Copyright 2002 Phil Karn, KA9Q
11 *
12 * $Id: rslib.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 *
18 * Description:
19 *
20 * The generic Reed Solomon library provides runtime configurable
21 * encoding / decoding of RS codes.
22 * Each user must call init_rs to get a pointer to a rs_control
23 * structure for the given rs parameters. This structure is either
24 * generated or a already available matching control structure is used.
25 * If a structure is generated then the polynomial arrays for
26 * fast encoding / decoding are built. This can take some time so
27 * make sure not to call this function from a time critical path.
28 * Usually a module / driver should initialize the necessary
29 * rs_control structure on module / driver init and release it
30 * on exit.
31 * The encoding puts the calculated syndrome into a given syndrome
32 * buffer.
33 * The decoding is a two step process. The first step calculates
34 * the syndrome over the received (data + syndrome) and calls the
35 * second stage, which does the decoding / error correction itself.
36 * Many hw encoders provide a syndrome calculation over the received
37 * data + syndrome and can call the second stage directly.
38 *
39 */
40
41#include <linux/errno.h>
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/module.h>
45#include <linux/rslib.h>
46#include <linux/slab.h>
47#include <linux/mutex.h>
48
49/* This list holds all currently allocated rs control structures */
50static LIST_HEAD (rslist);
51/* Protection for the list */
52static DEFINE_MUTEX(rslistlock);
53
54/**
55 * rs_init - Initialize a Reed-Solomon codec
56 * @symsize: symbol size, bits (1-8)
57 * @gfpoly: Field generator polynomial coefficients
58 * @gffunc: Field generator function
59 * @fcr: first root of RS code generator polynomial, index form
60 * @prim: primitive element to generate polynomial roots
61 * @nroots: RS code generator polynomial degree (number of roots)
62 *
63 * Allocate a control structure and the polynom arrays for faster
64 * en/decoding. Fill the arrays according to the given parameters.
65 */
66static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
67 int fcr, int prim, int nroots)
68{
69 struct rs_control *rs;
70 int i, j, sr, root, iprim;
71
72 /* Allocate the control structure */
73 rs = kmalloc(sizeof (struct rs_control), GFP_KERNEL);
74 if (rs == NULL)
75 return NULL;
76
77 INIT_LIST_HEAD(&rs->list);
78
79 rs->mm = symsize;
80 rs->nn = (1 << symsize) - 1;
81 rs->fcr = fcr;
82 rs->prim = prim;
83 rs->nroots = nroots;
84 rs->gfpoly = gfpoly;
85 rs->gffunc = gffunc;
86
87 /* Allocate the arrays */
88 rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL);
89 if (rs->alpha_to == NULL)
90 goto errrs;
91
92 rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL);
93 if (rs->index_of == NULL)
94 goto erralp;
95
96 rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), GFP_KERNEL);
97 if(rs->genpoly == NULL)
98 goto erridx;
99
100 /* Generate Galois field lookup tables */
101 rs->index_of[0] = rs->nn; /* log(zero) = -inf */
102 rs->alpha_to[rs->nn] = 0; /* alpha**-inf = 0 */
103 if (gfpoly) {
104 sr = 1;
105 for (i = 0; i < rs->nn; i++) {
106 rs->index_of[sr] = i;
107 rs->alpha_to[i] = sr;
108 sr <<= 1;
109 if (sr & (1 << symsize))
110 sr ^= gfpoly;
111 sr &= rs->nn;
112 }
113 } else {
114 sr = gffunc(0);
115 for (i = 0; i < rs->nn; i++) {
116 rs->index_of[sr] = i;
117 rs->alpha_to[i] = sr;
118 sr = gffunc(sr);
119 }
120 }
121 /* If it's not primitive, exit */
122 if(sr != rs->alpha_to[0])
123 goto errpol;
124
125 /* Find prim-th root of 1, used in decoding */
126 for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn);
127 /* prim-th root of 1, index form */
128 rs->iprim = iprim / prim;
129
130 /* Form RS code generator polynomial from its roots */
131 rs->genpoly[0] = 1;
132 for (i = 0, root = fcr * prim; i < nroots; i++, root += prim) {
133 rs->genpoly[i + 1] = 1;
134 /* Multiply rs->genpoly[] by @**(root + x) */
135 for (j = i; j > 0; j--) {
136 if (rs->genpoly[j] != 0) {
137 rs->genpoly[j] = rs->genpoly[j -1] ^
138 rs->alpha_to[rs_modnn(rs,
139 rs->index_of[rs->genpoly[j]] + root)];
140 } else
141 rs->genpoly[j] = rs->genpoly[j - 1];
142 }
143 /* rs->genpoly[0] can never be zero */
144 rs->genpoly[0] =
145 rs->alpha_to[rs_modnn(rs,
146 rs->index_of[rs->genpoly[0]] + root)];
147 }
148 /* convert rs->genpoly[] to index form for quicker encoding */
149 for (i = 0; i <= nroots; i++)
150 rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
151 return rs;
152
153 /* Error exit */
154errpol:
155 kfree(rs->genpoly);
156erridx:
157 kfree(rs->index_of);
158erralp:
159 kfree(rs->alpha_to);
160errrs:
161 kfree(rs);
162 return NULL;
163}
164
165
166/**
167 * free_rs - Free the rs control structure, if it is no longer used
168 * @rs: the control structure which is not longer used by the
169 * caller
170 */
171void free_rs(struct rs_control *rs)
172{
173 mutex_lock(&rslistlock);
174 rs->users--;
175 if(!rs->users) {
176 list_del(&rs->list);
177 kfree(rs->alpha_to);
178 kfree(rs->index_of);
179 kfree(rs->genpoly);
180 kfree(rs);
181 }
182 mutex_unlock(&rslistlock);
183}
184
185/**
186 * init_rs_internal - Find a matching or allocate a new rs control structure
187 * @symsize: the symbol size (number of bits)
188 * @gfpoly: the extended Galois field generator polynomial coefficients,
189 * with the 0th coefficient in the low order bit. The polynomial
190 * must be primitive;
191 * @gffunc: pointer to function to generate the next field element,
192 * or the multiplicative identity element if given 0. Used
193 * instead of gfpoly if gfpoly is 0
194 * @fcr: the first consecutive root of the rs code generator polynomial
195 * in index form
196 * @prim: primitive element to generate polynomial roots
197 * @nroots: RS code generator polynomial degree (number of roots)
198 */
199static struct rs_control *init_rs_internal(int symsize, int gfpoly,
200 int (*gffunc)(int), int fcr,
201 int prim, int nroots)
202{
203 struct list_head *tmp;
204 struct rs_control *rs;
205
206 /* Sanity checks */
207 if (symsize < 1)
208 return NULL;
209 if (fcr < 0 || fcr >= (1<<symsize))
210 return NULL;
211 if (prim <= 0 || prim >= (1<<symsize))
212 return NULL;
213 if (nroots < 0 || nroots >= (1<<symsize))
214 return NULL;
215
216 mutex_lock(&rslistlock);
217
218 /* Walk through the list and look for a matching entry */
219 list_for_each(tmp, &rslist) {
220 rs = list_entry(tmp, struct rs_control, list);
221 if (symsize != rs->mm)
222 continue;
223 if (gfpoly != rs->gfpoly)
224 continue;
225 if (gffunc != rs->gffunc)
226 continue;
227 if (fcr != rs->fcr)
228 continue;
229 if (prim != rs->prim)
230 continue;
231 if (nroots != rs->nroots)
232 continue;
233 /* We have a matching one already */
234 rs->users++;
235 goto out;
236 }
237
238 /* Create a new one */
239 rs = rs_init(symsize, gfpoly, gffunc, fcr, prim, nroots);
240 if (rs) {
241 rs->users = 1;
242 list_add(&rs->list, &rslist);
243 }
244out:
245 mutex_unlock(&rslistlock);
246 return rs;
247}
248
249/**
250 * init_rs - Find a matching or allocate a new rs control structure
251 * @symsize: the symbol size (number of bits)
252 * @gfpoly: the extended Galois field generator polynomial coefficients,
253 * with the 0th coefficient in the low order bit. The polynomial
254 * must be primitive;
255 * @fcr: the first consecutive root of the rs code generator polynomial
256 * in index form
257 * @prim: primitive element to generate polynomial roots
258 * @nroots: RS code generator polynomial degree (number of roots)
259 */
260struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
261 int nroots)
262{
263 return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots);
264}
265
266/**
267 * init_rs_non_canonical - Find a matching or allocate a new rs control
268 * structure, for fields with non-canonical
269 * representation
270 * @symsize: the symbol size (number of bits)
271 * @gffunc: pointer to function to generate the next field element,
272 * or the multiplicative identity element if given 0. Used
273 * instead of gfpoly if gfpoly is 0
274 * @fcr: the first consecutive root of the rs code generator polynomial
275 * in index form
276 * @prim: primitive element to generate polynomial roots
277 * @nroots: RS code generator polynomial degree (number of roots)
278 */
279struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int),
280 int fcr, int prim, int nroots)
281{
282 return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots);
283}
284
285#ifdef CONFIG_REED_SOLOMON_ENC8
286/**
287 * encode_rs8 - Calculate the parity for data values (8bit data width)
288 * @rs: the rs control structure
289 * @data: data field of a given type
290 * @len: data length
291 * @par: parity data, must be initialized by caller (usually all 0)
292 * @invmsk: invert data mask (will be xored on data)
293 *
294 * The parity uses a uint16_t data type to enable
295 * symbol size > 8. The calling code must take care of encoding of the
296 * syndrome result for storage itself.
297 */
298int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
299 uint16_t invmsk)
300{
301#include "encode_rs.c"
302}
303EXPORT_SYMBOL_GPL(encode_rs8);
304#endif
305
306#ifdef CONFIG_REED_SOLOMON_DEC8
307/**
308 * decode_rs8 - Decode codeword (8bit data width)
309 * @rs: the rs control structure
310 * @data: data field of a given type
311 * @par: received parity data field
312 * @len: data length
313 * @s: syndrome data field (if NULL, syndrome is calculated)
314 * @no_eras: number of erasures
315 * @eras_pos: position of erasures, can be NULL
316 * @invmsk: invert data mask (will be xored on data, not on parity!)
317 * @corr: buffer to store correction bitmask on eras_pos
318 *
319 * The syndrome and parity uses a uint16_t data type to enable
320 * symbol size > 8. The calling code must take care of decoding of the
321 * syndrome result and the received parity before calling this code.
322 * Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
323 */
324int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
325 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
326 uint16_t *corr)
327{
328#include "decode_rs.c"
329}
330EXPORT_SYMBOL_GPL(decode_rs8);
331#endif
332
333#ifdef CONFIG_REED_SOLOMON_ENC16
334/**
335 * encode_rs16 - Calculate the parity for data values (16bit data width)
336 * @rs: the rs control structure
337 * @data: data field of a given type
338 * @len: data length
339 * @par: parity data, must be initialized by caller (usually all 0)
340 * @invmsk: invert data mask (will be xored on data, not on parity!)
341 *
342 * Each field in the data array contains up to symbol size bits of valid data.
343 */
344int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
345 uint16_t invmsk)
346{
347#include "encode_rs.c"
348}
349EXPORT_SYMBOL_GPL(encode_rs16);
350#endif
351
352#ifdef CONFIG_REED_SOLOMON_DEC16
353/**
354 * decode_rs16 - Decode codeword (16bit data width)
355 * @rs: the rs control structure
356 * @data: data field of a given type
357 * @par: received parity data field
358 * @len: data length
359 * @s: syndrome data field (if NULL, syndrome is calculated)
360 * @no_eras: number of erasures
361 * @eras_pos: position of erasures, can be NULL
362 * @invmsk: invert data mask (will be xored on data, not on parity!)
363 * @corr: buffer to store correction bitmask on eras_pos
364 *
365 * Each field in the data array contains up to symbol size bits of valid data.
366 * Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
367 */
368int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
369 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
370 uint16_t *corr)
371{
372#include "decode_rs.c"
373}
374EXPORT_SYMBOL_GPL(decode_rs16);
375#endif
376
377EXPORT_SYMBOL_GPL(init_rs);
378EXPORT_SYMBOL_GPL(init_rs_non_canonical);
379EXPORT_SYMBOL_GPL(free_rs);
380
381MODULE_LICENSE("GPL");
382MODULE_DESCRIPTION("Reed Solomon encoder/decoder");
383MODULE_AUTHOR("Phil Karn, Thomas Gleixner");
384