blob: 5b0d8522b7ca16882b54cee337536a665cb825d0 [file] [log] [blame]
Thomas Gleixner03ead842005-11-07 11:15:37 +00001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * lib/reed_solomon/reed_solomon.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Overview:
5 * Generic Reed Solomon encoder / decoder library
Thomas Gleixner03ead842005-11-07 11:15:37 +00006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 *
Thomas Gleixner03ead842005-11-07 11:15:37 +000012 * $Id: rslib.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
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:
Thomas Gleixner03ead842005-11-07 11:15:37 +000019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * 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.
Thomas Gleixner03ead842005-11-07 11:15:37 +000028 * Usually a module / driver should initialize the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * rs_control structure on module / driver init and release it
30 * on exit.
Thomas Gleixner03ead842005-11-07 11:15:37 +000031 * The encoding puts the calculated syndrome into a given syndrome
32 * buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * 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>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080047#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/semaphore.h>
49
50/* This list holds all currently allocated rs control structures */
51static LIST_HEAD (rslist);
52/* Protection for the list */
Arjan van de Ven97d1f152006-03-23 03:00:24 -080053static DEFINE_MUTEX(rslistlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Thomas Gleixner03ead842005-11-07 11:15:37 +000055/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * rs_init - Initialize a Reed-Solomon codec
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * @symsize: symbol size, bits (1-8)
58 * @gfpoly: Field generator polynomial coefficients
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020059 * @gffunc: Field generator function
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * @fcr: first root of RS code generator polynomial, index form
61 * @prim: primitive element to generate polynomial roots
62 * @nroots: RS code generator polynomial degree (number of roots)
63 *
64 * Allocate a control structure and the polynom arrays for faster
Randy Dunlap9dc65572006-06-25 05:49:14 -070065 * en/decoding. Fill the arrays according to the given parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020067static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
68 int fcr, int prim, int nroots)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct rs_control *rs;
71 int i, j, sr, root, iprim;
72
73 /* Allocate the control structure */
74 rs = kmalloc(sizeof (struct rs_control), GFP_KERNEL);
75 if (rs == NULL)
76 return NULL;
77
78 INIT_LIST_HEAD(&rs->list);
79
80 rs->mm = symsize;
81 rs->nn = (1 << symsize) - 1;
82 rs->fcr = fcr;
83 rs->prim = prim;
84 rs->nroots = nroots;
85 rs->gfpoly = gfpoly;
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020086 rs->gffunc = gffunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /* Allocate the arrays */
89 rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL);
90 if (rs->alpha_to == NULL)
91 goto errrs;
92
93 rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL);
94 if (rs->index_of == NULL)
95 goto erralp;
96
97 rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), GFP_KERNEL);
98 if(rs->genpoly == NULL)
99 goto erridx;
100
101 /* Generate Galois field lookup tables */
102 rs->index_of[0] = rs->nn; /* log(zero) = -inf */
103 rs->alpha_to[rs->nn] = 0; /* alpha**-inf = 0 */
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200104 if (gfpoly) {
105 sr = 1;
106 for (i = 0; i < rs->nn; i++) {
107 rs->index_of[sr] = i;
108 rs->alpha_to[i] = sr;
109 sr <<= 1;
110 if (sr & (1 << symsize))
111 sr ^= gfpoly;
112 sr &= rs->nn;
113 }
114 } else {
115 sr = gffunc(0);
116 for (i = 0; i < rs->nn; i++) {
117 rs->index_of[sr] = i;
118 rs->alpha_to[i] = sr;
119 sr = gffunc(sr);
120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 /* If it's not primitive, exit */
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200123 if(sr != rs->alpha_to[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 goto errpol;
125
126 /* Find prim-th root of 1, used in decoding */
127 for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn);
128 /* prim-th root of 1, index form */
129 rs->iprim = iprim / prim;
130
131 /* Form RS code generator polynomial from its roots */
132 rs->genpoly[0] = 1;
133 for (i = 0, root = fcr * prim; i < nroots; i++, root += prim) {
134 rs->genpoly[i + 1] = 1;
135 /* Multiply rs->genpoly[] by @**(root + x) */
136 for (j = i; j > 0; j--) {
137 if (rs->genpoly[j] != 0) {
Thomas Gleixner03ead842005-11-07 11:15:37 +0000138 rs->genpoly[j] = rs->genpoly[j -1] ^
139 rs->alpha_to[rs_modnn(rs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 rs->index_of[rs->genpoly[j]] + root)];
141 } else
142 rs->genpoly[j] = rs->genpoly[j - 1];
143 }
144 /* rs->genpoly[0] can never be zero */
Thomas Gleixner03ead842005-11-07 11:15:37 +0000145 rs->genpoly[0] =
146 rs->alpha_to[rs_modnn(rs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 rs->index_of[rs->genpoly[0]] + root)];
148 }
149 /* convert rs->genpoly[] to index form for quicker encoding */
150 for (i = 0; i <= nroots; i++)
151 rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
152 return rs;
153
154 /* Error exit */
155errpol:
156 kfree(rs->genpoly);
157erridx:
158 kfree(rs->index_of);
159erralp:
160 kfree(rs->alpha_to);
161errrs:
162 kfree(rs);
163 return NULL;
164}
165
166
Thomas Gleixner03ead842005-11-07 11:15:37 +0000167/**
Randy Dunlap9dc65572006-06-25 05:49:14 -0700168 * free_rs - Free the rs control structure, if it is no longer used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * @rs: the control structure which is not longer used by the
170 * caller
171 */
172void free_rs(struct rs_control *rs)
173{
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800174 mutex_lock(&rslistlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 rs->users--;
176 if(!rs->users) {
177 list_del(&rs->list);
178 kfree(rs->alpha_to);
179 kfree(rs->index_of);
180 kfree(rs->genpoly);
181 kfree(rs);
182 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800183 mutex_unlock(&rslistlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Thomas Gleixner03ead842005-11-07 11:15:37 +0000186/**
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200187 * init_rs_internal - Find a matching or allocate a new rs control structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * @symsize: the symbol size (number of bits)
189 * @gfpoly: the extended Galois field generator polynomial coefficients,
190 * with the 0th coefficient in the low order bit. The polynomial
191 * must be primitive;
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200192 * @gffunc: pointer to function to generate the next field element,
193 * or the multiplicative identity element if given 0. Used
194 * instead of gfpoly if gfpoly is 0
Thomas Gleixner03ead842005-11-07 11:15:37 +0000195 * @fcr: the first consecutive root of the rs code generator polynomial
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * in index form
197 * @prim: primitive element to generate polynomial roots
198 * @nroots: RS code generator polynomial degree (number of roots)
199 */
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200200static struct rs_control *init_rs_internal(int symsize, int gfpoly,
201 int (*gffunc)(int), int fcr,
202 int prim, int nroots)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct list_head *tmp;
205 struct rs_control *rs;
206
207 /* Sanity checks */
208 if (symsize < 1)
209 return NULL;
210 if (fcr < 0 || fcr >= (1<<symsize))
211 return NULL;
212 if (prim <= 0 || prim >= (1<<symsize))
213 return NULL;
Thomas Gleixner03ead842005-11-07 11:15:37 +0000214 if (nroots < 0 || nroots >= (1<<symsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return NULL;
Thomas Gleixner03ead842005-11-07 11:15:37 +0000216
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800217 mutex_lock(&rslistlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* Walk through the list and look for a matching entry */
220 list_for_each(tmp, &rslist) {
221 rs = list_entry(tmp, struct rs_control, list);
222 if (symsize != rs->mm)
223 continue;
224 if (gfpoly != rs->gfpoly)
225 continue;
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200226 if (gffunc != rs->gffunc)
227 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (fcr != rs->fcr)
Thomas Gleixner03ead842005-11-07 11:15:37 +0000229 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (prim != rs->prim)
Thomas Gleixner03ead842005-11-07 11:15:37 +0000231 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (nroots != rs->nroots)
233 continue;
234 /* We have a matching one already */
235 rs->users++;
236 goto out;
237 }
238
239 /* Create a new one */
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200240 rs = rs_init(symsize, gfpoly, gffunc, fcr, prim, nroots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (rs) {
242 rs->users = 1;
243 list_add(&rs->list, &rslist);
244 }
Thomas Gleixner03ead842005-11-07 11:15:37 +0000245out:
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800246 mutex_unlock(&rslistlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return rs;
248}
249
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200250/**
251 * init_rs - Find a matching or allocate a new rs control structure
252 * @symsize: the symbol size (number of bits)
253 * @gfpoly: the extended Galois field generator polynomial coefficients,
254 * with the 0th coefficient in the low order bit. The polynomial
255 * must be primitive;
256 * @fcr: the first consecutive root of the rs code generator polynomial
257 * in index form
258 * @prim: primitive element to generate polynomial roots
259 * @nroots: RS code generator polynomial degree (number of roots)
260 */
261struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
262 int nroots)
263{
264 return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots);
265}
266
267/**
268 * init_rs_non_canonical - Find a matching or allocate a new rs control
269 * structure, for fields with non-canonical
270 * representation
271 * @symsize: the symbol size (number of bits)
272 * @gffunc: pointer to function to generate the next field element,
273 * or the multiplicative identity element if given 0. Used
274 * instead of gfpoly if gfpoly is 0
275 * @fcr: the first consecutive root of the rs code generator polynomial
276 * in index form
277 * @prim: primitive element to generate polynomial roots
278 * @nroots: RS code generator polynomial degree (number of roots)
279 */
280struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int),
281 int fcr, int prim, int nroots)
282{
283 return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots);
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#ifdef CONFIG_REED_SOLOMON_ENC8
Thomas Gleixner03ead842005-11-07 11:15:37 +0000287/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * encode_rs8 - Calculate the parity for data values (8bit data width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * @rs: the rs control structure
290 * @data: data field of a given type
Thomas Gleixner03ead842005-11-07 11:15:37 +0000291 * @len: data length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * @par: parity data, must be initialized by caller (usually all 0)
293 * @invmsk: invert data mask (will be xored on data)
294 *
295 * The parity uses a uint16_t data type to enable
296 * symbol size > 8. The calling code must take care of encoding of the
297 * syndrome result for storage itself.
298 */
Thomas Gleixner03ead842005-11-07 11:15:37 +0000299int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 uint16_t invmsk)
301{
302#include "encode_rs.c"
303}
304EXPORT_SYMBOL_GPL(encode_rs8);
305#endif
306
307#ifdef CONFIG_REED_SOLOMON_DEC8
Thomas Gleixner03ead842005-11-07 11:15:37 +0000308/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * decode_rs8 - Decode codeword (8bit data width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * @rs: the rs control structure
311 * @data: data field of a given type
312 * @par: received parity data field
313 * @len: data length
314 * @s: syndrome data field (if NULL, syndrome is calculated)
315 * @no_eras: number of erasures
316 * @eras_pos: position of erasures, can be NULL
317 * @invmsk: invert data mask (will be xored on data, not on parity!)
318 * @corr: buffer to store correction bitmask on eras_pos
319 *
320 * The syndrome and parity uses a uint16_t data type to enable
321 * symbol size > 8. The calling code must take care of decoding of the
322 * syndrome result and the received parity before calling this code.
323 */
324int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
Thomas Gleixner03ead842005-11-07 11:15:37 +0000325 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * @rs: the rs control structure
337 * @data: data field of a given type
Thomas Gleixner03ead842005-11-07 11:15:37 +0000338 * @len: data length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * @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 */
Thomas Gleixner03ead842005-11-07 11:15:37 +0000344int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 uint16_t invmsk)
346{
347#include "encode_rs.c"
348}
349EXPORT_SYMBOL_GPL(encode_rs16);
350#endif
351
352#ifdef CONFIG_REED_SOLOMON_DEC16
Thomas Gleixner03ead842005-11-07 11:15:37 +0000353/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * decode_rs16 - Decode codeword (16bit data width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * @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
Thomas Gleixner03ead842005-11-07 11:15:37 +0000362 * @invmsk: invert data mask (will be xored on data, not on parity!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * @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 */
367int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
Thomas Gleixner03ead842005-11-07 11:15:37 +0000368 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 uint16_t *corr)
370{
371#include "decode_rs.c"
372}
373EXPORT_SYMBOL_GPL(decode_rs16);
374#endif
375
376EXPORT_SYMBOL_GPL(init_rs);
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200377EXPORT_SYMBOL_GPL(init_rs_non_canonical);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378EXPORT_SYMBOL_GPL(free_rs);
379
380MODULE_LICENSE("GPL");
381MODULE_DESCRIPTION("Reed Solomon encoder/decoder");
382MODULE_AUTHOR("Phil Karn, Thomas Gleixner");
383