xref: /linux/lib/reed_solomon/reed_solomon.c (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
1dc8f923eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Generic Reed Solomon encoder / decoder library
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Reed Solomon code lifted from reed solomon library written by Phil Karn
81da177e4SLinus Torvalds  * Copyright 2002 Phil Karn, KA9Q
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Description:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * The generic Reed Solomon library provides runtime configurable
131da177e4SLinus Torvalds  * encoding / decoding of RS codes.
1421633981SThomas Gleixner  *
1521633981SThomas Gleixner  * Each user must call init_rs to get a pointer to a rs_control structure
1621633981SThomas Gleixner  * for the given rs parameters. The control struct is unique per instance.
1721633981SThomas Gleixner  * It points to a codec which can be shared by multiple control structures.
1821633981SThomas Gleixner  * If a codec is newly allocated then the polynomial arrays for fast
1921633981SThomas Gleixner  * encoding / decoding are built. This can take some time so make sure not
2021633981SThomas Gleixner  * to call this function from a time critical path.  Usually a module /
2121633981SThomas Gleixner  * driver should initialize the necessary rs_control structure on module /
2221633981SThomas Gleixner  * driver init and release it on exit.
2321633981SThomas Gleixner  *
2421633981SThomas Gleixner  * The encoding puts the calculated syndrome into a given syndrome buffer.
2521633981SThomas Gleixner  *
2621633981SThomas Gleixner  * The decoding is a two step process. The first step calculates the
2721633981SThomas Gleixner  * syndrome over the received (data + syndrome) and calls the second stage,
2821633981SThomas Gleixner  * which does the decoding / error correction itself.  Many hw encoders
2921633981SThomas Gleixner  * provide a syndrome calculation over the received data + syndrome and can
3021633981SThomas Gleixner  * call the second stage directly.
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds #include <linux/errno.h>
331da177e4SLinus Torvalds #include <linux/kernel.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/module.h>
361da177e4SLinus Torvalds #include <linux/rslib.h>
371da177e4SLinus Torvalds #include <linux/slab.h>
3897d1f15bSArjan van de Ven #include <linux/mutex.h>
391da177e4SLinus Torvalds 
4045888b40SThomas Gleixner enum {
4145888b40SThomas Gleixner 	RS_DECODE_LAMBDA,
4245888b40SThomas Gleixner 	RS_DECODE_SYN,
4345888b40SThomas Gleixner 	RS_DECODE_B,
4445888b40SThomas Gleixner 	RS_DECODE_T,
4545888b40SThomas Gleixner 	RS_DECODE_OMEGA,
4645888b40SThomas Gleixner 	RS_DECODE_ROOT,
4745888b40SThomas Gleixner 	RS_DECODE_REG,
4845888b40SThomas Gleixner 	RS_DECODE_LOC,
4945888b40SThomas Gleixner 	RS_DECODE_NUM_BUFFERS
5045888b40SThomas Gleixner };
5145888b40SThomas Gleixner 
5221633981SThomas Gleixner /* This list holds all currently allocated rs codec structures */
5321633981SThomas Gleixner static LIST_HEAD(codec_list);
541da177e4SLinus Torvalds /* Protection for the list */
5597d1f15bSArjan van de Ven static DEFINE_MUTEX(rslistlock);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /**
5821633981SThomas Gleixner  * codec_init - Initialize a Reed-Solomon codec
591da177e4SLinus Torvalds  * @symsize:	symbol size, bits (1-8)
601da177e4SLinus Torvalds  * @gfpoly:	Field generator polynomial coefficients
61d7e5a546SSegher Boessenkool  * @gffunc:	Field generator function
621da177e4SLinus Torvalds  * @fcr:	first root of RS code generator polynomial, index form
631da177e4SLinus Torvalds  * @prim:	primitive element to generate polynomial roots
641da177e4SLinus Torvalds  * @nroots:	RS code generator polynomial degree (number of roots)
6583a530e1SThomas Gleixner  * @gfp:	GFP_ flags for allocations
661da177e4SLinus Torvalds  *
6721633981SThomas Gleixner  * Allocate a codec structure and the polynom arrays for faster
689dc65576SRandy Dunlap  * en/decoding. Fill the arrays according to the given parameters.
691da177e4SLinus Torvalds  */
codec_init(int symsize,int gfpoly,int (* gffunc)(int),int fcr,int prim,int nroots,gfp_t gfp)7021633981SThomas Gleixner static struct rs_codec *codec_init(int symsize, int gfpoly, int (*gffunc)(int),
7183a530e1SThomas Gleixner 				   int fcr, int prim, int nroots, gfp_t gfp)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds 	int i, j, sr, root, iprim;
7421633981SThomas Gleixner 	struct rs_codec *rs;
751da177e4SLinus Torvalds 
76a85e126aSThomas Gleixner 	rs = kzalloc(sizeof(*rs), gfp);
7783a530e1SThomas Gleixner 	if (!rs)
781da177e4SLinus Torvalds 		return NULL;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	INIT_LIST_HEAD(&rs->list);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	rs->mm = symsize;
831da177e4SLinus Torvalds 	rs->nn = (1 << symsize) - 1;
841da177e4SLinus Torvalds 	rs->fcr = fcr;
851da177e4SLinus Torvalds 	rs->prim = prim;
861da177e4SLinus Torvalds 	rs->nroots = nroots;
871da177e4SLinus Torvalds 	rs->gfpoly = gfpoly;
88d7e5a546SSegher Boessenkool 	rs->gffunc = gffunc;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* Allocate the arrays */
916da2ec56SKees Cook 	rs->alpha_to = kmalloc_array(rs->nn + 1, sizeof(uint16_t), gfp);
921da177e4SLinus Torvalds 	if (rs->alpha_to == NULL)
93a85e126aSThomas Gleixner 		goto err;
941da177e4SLinus Torvalds 
956da2ec56SKees Cook 	rs->index_of = kmalloc_array(rs->nn + 1, sizeof(uint16_t), gfp);
961da177e4SLinus Torvalds 	if (rs->index_of == NULL)
97a85e126aSThomas Gleixner 		goto err;
981da177e4SLinus Torvalds 
996da2ec56SKees Cook 	rs->genpoly = kmalloc_array(rs->nroots + 1, sizeof(uint16_t), gfp);
1001da177e4SLinus Torvalds 	if(rs->genpoly == NULL)
101a85e126aSThomas Gleixner 		goto err;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	/* Generate Galois field lookup tables */
1041da177e4SLinus Torvalds 	rs->index_of[0] = rs->nn;	/* log(zero) = -inf */
1051da177e4SLinus Torvalds 	rs->alpha_to[rs->nn] = 0;	/* alpha**-inf = 0 */
106d7e5a546SSegher Boessenkool 	if (gfpoly) {
1071da177e4SLinus Torvalds 		sr = 1;
1081da177e4SLinus Torvalds 		for (i = 0; i < rs->nn; i++) {
1091da177e4SLinus Torvalds 			rs->index_of[sr] = i;
1101da177e4SLinus Torvalds 			rs->alpha_to[i] = sr;
1111da177e4SLinus Torvalds 			sr <<= 1;
1121da177e4SLinus Torvalds 			if (sr & (1 << symsize))
1131da177e4SLinus Torvalds 				sr ^= gfpoly;
1141da177e4SLinus Torvalds 			sr &= rs->nn;
1151da177e4SLinus Torvalds 		}
116d7e5a546SSegher Boessenkool 	} else {
117d7e5a546SSegher Boessenkool 		sr = gffunc(0);
118d7e5a546SSegher Boessenkool 		for (i = 0; i < rs->nn; i++) {
119d7e5a546SSegher Boessenkool 			rs->index_of[sr] = i;
120d7e5a546SSegher Boessenkool 			rs->alpha_to[i] = sr;
121d7e5a546SSegher Boessenkool 			sr = gffunc(sr);
122d7e5a546SSegher Boessenkool 		}
123d7e5a546SSegher Boessenkool 	}
1241da177e4SLinus Torvalds 	/* If it's not primitive, exit */
125d7e5a546SSegher Boessenkool 	if(sr != rs->alpha_to[0])
126a85e126aSThomas Gleixner 		goto err;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	/* Find prim-th root of 1, used in decoding */
1291da177e4SLinus Torvalds 	for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn);
1301da177e4SLinus Torvalds 	/* prim-th root of 1, index form */
1311da177e4SLinus Torvalds 	rs->iprim = iprim / prim;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	/* Form RS code generator polynomial from its roots */
1341da177e4SLinus Torvalds 	rs->genpoly[0] = 1;
1351da177e4SLinus Torvalds 	for (i = 0, root = fcr * prim; i < nroots; i++, root += prim) {
1361da177e4SLinus Torvalds 		rs->genpoly[i + 1] = 1;
1371da177e4SLinus Torvalds 		/* Multiply rs->genpoly[] by  @**(root + x) */
1381da177e4SLinus Torvalds 		for (j = i; j > 0; j--) {
1391da177e4SLinus Torvalds 			if (rs->genpoly[j] != 0) {
1401da177e4SLinus Torvalds 				rs->genpoly[j] = rs->genpoly[j -1] ^
1411da177e4SLinus Torvalds 					rs->alpha_to[rs_modnn(rs,
1421da177e4SLinus Torvalds 					rs->index_of[rs->genpoly[j]] + root)];
1431da177e4SLinus Torvalds 			} else
1441da177e4SLinus Torvalds 				rs->genpoly[j] = rs->genpoly[j - 1];
1451da177e4SLinus Torvalds 		}
1461da177e4SLinus Torvalds 		/* rs->genpoly[0] can never be zero */
1471da177e4SLinus Torvalds 		rs->genpoly[0] =
1481da177e4SLinus Torvalds 			rs->alpha_to[rs_modnn(rs,
1491da177e4SLinus Torvalds 				rs->index_of[rs->genpoly[0]] + root)];
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 	/* convert rs->genpoly[] to index form for quicker encoding */
1521da177e4SLinus Torvalds 	for (i = 0; i <= nroots; i++)
1531da177e4SLinus Torvalds 		rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
15421633981SThomas Gleixner 
15521633981SThomas Gleixner 	rs->users = 1;
15621633981SThomas Gleixner 	list_add(&rs->list, &codec_list);
1571da177e4SLinus Torvalds 	return rs;
1581da177e4SLinus Torvalds 
159a85e126aSThomas Gleixner err:
1601da177e4SLinus Torvalds 	kfree(rs->genpoly);
1611da177e4SLinus Torvalds 	kfree(rs->index_of);
1621da177e4SLinus Torvalds 	kfree(rs->alpha_to);
1631da177e4SLinus Torvalds 	kfree(rs);
1641da177e4SLinus Torvalds 	return NULL;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /**
16921633981SThomas Gleixner  *  free_rs - Free the rs control structure
17021633981SThomas Gleixner  *  @rs:	The control structure which is not longer used by the
1711da177e4SLinus Torvalds  *		caller
17221633981SThomas Gleixner  *
17321633981SThomas Gleixner  * Free the control structure. If @rs is the last user of the associated
17421633981SThomas Gleixner  * codec, free the codec as well.
1751da177e4SLinus Torvalds  */
free_rs(struct rs_control * rs)1761da177e4SLinus Torvalds void free_rs(struct rs_control *rs)
1771da177e4SLinus Torvalds {
17821633981SThomas Gleixner 	struct rs_codec *cd;
17921633981SThomas Gleixner 
18021633981SThomas Gleixner 	if (!rs)
18121633981SThomas Gleixner 		return;
18221633981SThomas Gleixner 
18321633981SThomas Gleixner 	cd = rs->codec;
18497d1f15bSArjan van de Ven 	mutex_lock(&rslistlock);
18521633981SThomas Gleixner 	cd->users--;
18621633981SThomas Gleixner 	if(!cd->users) {
18721633981SThomas Gleixner 		list_del(&cd->list);
18821633981SThomas Gleixner 		kfree(cd->alpha_to);
18921633981SThomas Gleixner 		kfree(cd->index_of);
19021633981SThomas Gleixner 		kfree(cd->genpoly);
19121633981SThomas Gleixner 		kfree(cd);
1921da177e4SLinus Torvalds 	}
19397d1f15bSArjan van de Ven 	mutex_unlock(&rslistlock);
19421633981SThomas Gleixner 	kfree(rs);
1951da177e4SLinus Torvalds }
19683a530e1SThomas Gleixner EXPORT_SYMBOL_GPL(free_rs);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /**
19921633981SThomas Gleixner  * init_rs_internal - Allocate rs control, find a matching codec or allocate a new one
2001da177e4SLinus Torvalds  *  @symsize:	the symbol size (number of bits)
2011da177e4SLinus Torvalds  *  @gfpoly:	the extended Galois field generator polynomial coefficients,
2021da177e4SLinus Torvalds  *		with the 0th coefficient in the low order bit. The polynomial
2031da177e4SLinus Torvalds  *		must be primitive;
204d7e5a546SSegher Boessenkool  *  @gffunc:	pointer to function to generate the next field element,
205d7e5a546SSegher Boessenkool  *		or the multiplicative identity element if given 0.  Used
206d7e5a546SSegher Boessenkool  *		instead of gfpoly if gfpoly is 0
2071da177e4SLinus Torvalds  *  @fcr:	the first consecutive root of the rs code generator polynomial
2081da177e4SLinus Torvalds  *		in index form
2091da177e4SLinus Torvalds  *  @prim:	primitive element to generate polynomial roots
2101da177e4SLinus Torvalds  *  @nroots:	RS code generator polynomial degree (number of roots)
21183a530e1SThomas Gleixner  *  @gfp:	GFP_ flags for allocations
2121da177e4SLinus Torvalds  */
init_rs_internal(int symsize,int gfpoly,int (* gffunc)(int),int fcr,int prim,int nroots,gfp_t gfp)213d7e5a546SSegher Boessenkool static struct rs_control *init_rs_internal(int symsize, int gfpoly,
214d7e5a546SSegher Boessenkool 					   int (*gffunc)(int), int fcr,
21583a530e1SThomas Gleixner 					   int prim, int nroots, gfp_t gfp)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	struct list_head *tmp;
2181da177e4SLinus Torvalds 	struct rs_control *rs;
21945888b40SThomas Gleixner 	unsigned int bsize;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	/* Sanity checks */
2221da177e4SLinus Torvalds 	if (symsize < 1)
2231da177e4SLinus Torvalds 		return NULL;
2241da177e4SLinus Torvalds 	if (fcr < 0 || fcr >= (1<<symsize))
2251da177e4SLinus Torvalds 		return NULL;
2261da177e4SLinus Torvalds 	if (prim <= 0 || prim >= (1<<symsize))
2271da177e4SLinus Torvalds 		return NULL;
22803ead842SThomas Gleixner 	if (nroots < 0 || nroots >= (1<<symsize))
2291da177e4SLinus Torvalds 		return NULL;
2301da177e4SLinus Torvalds 
23145888b40SThomas Gleixner 	/*
23245888b40SThomas Gleixner 	 * The decoder needs buffers in each control struct instance to
23345888b40SThomas Gleixner 	 * avoid variable size or large fixed size allocations on
23445888b40SThomas Gleixner 	 * stack. Size the buffers to arrays of [nroots + 1].
23545888b40SThomas Gleixner 	 */
23645888b40SThomas Gleixner 	bsize = sizeof(uint16_t) * RS_DECODE_NUM_BUFFERS * (nroots + 1);
23745888b40SThomas Gleixner 	rs = kzalloc(sizeof(*rs) + bsize, gfp);
23821633981SThomas Gleixner 	if (!rs)
23921633981SThomas Gleixner 		return NULL;
24021633981SThomas Gleixner 
24197d1f15bSArjan van de Ven 	mutex_lock(&rslistlock);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/* Walk through the list and look for a matching entry */
24421633981SThomas Gleixner 	list_for_each(tmp, &codec_list) {
24521633981SThomas Gleixner 		struct rs_codec *cd = list_entry(tmp, struct rs_codec, list);
24621633981SThomas Gleixner 
24721633981SThomas Gleixner 		if (symsize != cd->mm)
2481da177e4SLinus Torvalds 			continue;
24921633981SThomas Gleixner 		if (gfpoly != cd->gfpoly)
2501da177e4SLinus Torvalds 			continue;
25121633981SThomas Gleixner 		if (gffunc != cd->gffunc)
252d7e5a546SSegher Boessenkool 			continue;
25321633981SThomas Gleixner 		if (fcr != cd->fcr)
2541da177e4SLinus Torvalds 			continue;
25521633981SThomas Gleixner 		if (prim != cd->prim)
2561da177e4SLinus Torvalds 			continue;
25721633981SThomas Gleixner 		if (nroots != cd->nroots)
2581da177e4SLinus Torvalds 			continue;
2591da177e4SLinus Torvalds 		/* We have a matching one already */
26021633981SThomas Gleixner 		cd->users++;
26121633981SThomas Gleixner 		rs->codec = cd;
2621da177e4SLinus Torvalds 		goto out;
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	/* Create a new one */
26621633981SThomas Gleixner 	rs->codec = codec_init(symsize, gfpoly, gffunc, fcr, prim, nroots, gfp);
26721633981SThomas Gleixner 	if (!rs->codec) {
26821633981SThomas Gleixner 		kfree(rs);
26921633981SThomas Gleixner 		rs = NULL;
2701da177e4SLinus Torvalds 	}
2711da177e4SLinus Torvalds out:
27297d1f15bSArjan van de Ven 	mutex_unlock(&rslistlock);
2731da177e4SLinus Torvalds 	return rs;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
276d7e5a546SSegher Boessenkool /**
27721633981SThomas Gleixner  * init_rs_gfp - Create a RS control struct and initialize it
278d7e5a546SSegher Boessenkool  *  @symsize:	the symbol size (number of bits)
279d7e5a546SSegher Boessenkool  *  @gfpoly:	the extended Galois field generator polynomial coefficients,
280d7e5a546SSegher Boessenkool  *		with the 0th coefficient in the low order bit. The polynomial
281d7e5a546SSegher Boessenkool  *		must be primitive;
282d7e5a546SSegher Boessenkool  *  @fcr:	the first consecutive root of the rs code generator polynomial
283d7e5a546SSegher Boessenkool  *		in index form
284d7e5a546SSegher Boessenkool  *  @prim:	primitive element to generate polynomial roots
285d7e5a546SSegher Boessenkool  *  @nroots:	RS code generator polynomial degree (number of roots)
2868dd99871SMatthew Wilcox  *  @gfp:	Memory allocation flags.
287d7e5a546SSegher Boessenkool  */
init_rs_gfp(int symsize,int gfpoly,int fcr,int prim,int nroots,gfp_t gfp)28883a530e1SThomas Gleixner struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
28983a530e1SThomas Gleixner 			       int nroots, gfp_t gfp)
290d7e5a546SSegher Boessenkool {
29183a530e1SThomas Gleixner 	return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots, gfp);
292d7e5a546SSegher Boessenkool }
29383a530e1SThomas Gleixner EXPORT_SYMBOL_GPL(init_rs_gfp);
294d7e5a546SSegher Boessenkool 
295d7e5a546SSegher Boessenkool /**
29621633981SThomas Gleixner  * init_rs_non_canonical - Allocate rs control struct for fields with
29721633981SThomas Gleixner  *                         non-canonical representation
298d7e5a546SSegher Boessenkool  *  @symsize:	the symbol size (number of bits)
299d7e5a546SSegher Boessenkool  *  @gffunc:	pointer to function to generate the next field element,
300d7e5a546SSegher Boessenkool  *		or the multiplicative identity element if given 0.  Used
301d7e5a546SSegher Boessenkool  *		instead of gfpoly if gfpoly is 0
302d7e5a546SSegher Boessenkool  *  @fcr:	the first consecutive root of the rs code generator polynomial
303d7e5a546SSegher Boessenkool  *		in index form
304d7e5a546SSegher Boessenkool  *  @prim:	primitive element to generate polynomial roots
305d7e5a546SSegher Boessenkool  *  @nroots:	RS code generator polynomial degree (number of roots)
306d7e5a546SSegher Boessenkool  */
init_rs_non_canonical(int symsize,int (* gffunc)(int),int fcr,int prim,int nroots)307d7e5a546SSegher Boessenkool struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int),
308d7e5a546SSegher Boessenkool 					 int fcr, int prim, int nroots)
309d7e5a546SSegher Boessenkool {
31083a530e1SThomas Gleixner 	return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots,
31183a530e1SThomas Gleixner 				GFP_KERNEL);
312d7e5a546SSegher Boessenkool }
31383a530e1SThomas Gleixner EXPORT_SYMBOL_GPL(init_rs_non_canonical);
314d7e5a546SSegher Boessenkool 
3151da177e4SLinus Torvalds #ifdef CONFIG_REED_SOLOMON_ENC8
3161da177e4SLinus Torvalds /**
3171da177e4SLinus Torvalds  *  encode_rs8 - Calculate the parity for data values (8bit data width)
31821633981SThomas Gleixner  *  @rsc:	the rs control structure
3191da177e4SLinus Torvalds  *  @data:	data field of a given type
3201da177e4SLinus Torvalds  *  @len:	data length
3211da177e4SLinus Torvalds  *  @par:	parity data, must be initialized by caller (usually all 0)
3221da177e4SLinus Torvalds  *  @invmsk:	invert data mask (will be xored on data)
3231da177e4SLinus Torvalds  *
3241da177e4SLinus Torvalds  *  The parity uses a uint16_t data type to enable
3251da177e4SLinus Torvalds  *  symbol size > 8. The calling code must take care of encoding of the
3261da177e4SLinus Torvalds  *  syndrome result for storage itself.
3271da177e4SLinus Torvalds  */
encode_rs8(struct rs_control * rsc,uint8_t * data,int len,uint16_t * par,uint16_t invmsk)32821633981SThomas Gleixner int encode_rs8(struct rs_control *rsc, uint8_t *data, int len, uint16_t *par,
3291da177e4SLinus Torvalds 	       uint16_t invmsk)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds #include "encode_rs.c"
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(encode_rs8);
3341da177e4SLinus Torvalds #endif
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds #ifdef CONFIG_REED_SOLOMON_DEC8
3371da177e4SLinus Torvalds /**
3381da177e4SLinus Torvalds  *  decode_rs8 - Decode codeword (8bit data width)
33921633981SThomas Gleixner  *  @rsc:	the rs control structure
3401da177e4SLinus Torvalds  *  @data:	data field of a given type
3411da177e4SLinus Torvalds  *  @par:	received parity data field
3421da177e4SLinus Torvalds  *  @len:	data length
343*38cbae14SFerdinand Blomqvist  *  @s: 	syndrome data field, must be in index form
344*38cbae14SFerdinand Blomqvist  *		(if NULL, syndrome is calculated)
3451da177e4SLinus Torvalds  *  @no_eras:	number of erasures
3461da177e4SLinus Torvalds  *  @eras_pos:	position of erasures, can be NULL
3471da177e4SLinus Torvalds  *  @invmsk:	invert data mask (will be xored on data, not on parity!)
3481da177e4SLinus Torvalds  *  @corr:	buffer to store correction bitmask on eras_pos
3491da177e4SLinus Torvalds  *
3501da177e4SLinus Torvalds  *  The syndrome and parity uses a uint16_t data type to enable
3511da177e4SLinus Torvalds  *  symbol size > 8. The calling code must take care of decoding of the
3521da177e4SLinus Torvalds  *  syndrome result and the received parity before calling this code.
35345888b40SThomas Gleixner  *
35445888b40SThomas Gleixner  *  Note: The rs_control struct @rsc contains buffers which are used for
35545888b40SThomas Gleixner  *  decoding, so the caller has to ensure that decoder invocations are
35645888b40SThomas Gleixner  *  serialized.
35745888b40SThomas Gleixner  *
358*38cbae14SFerdinand Blomqvist  *  Returns the number of corrected symbols or -EBADMSG for uncorrectable
359*38cbae14SFerdinand Blomqvist  *  errors. The count includes errors in the parity.
3601da177e4SLinus Torvalds  */
decode_rs8(struct rs_control * rsc,uint8_t * data,uint16_t * par,int len,uint16_t * s,int no_eras,int * eras_pos,uint16_t invmsk,uint16_t * corr)36121633981SThomas Gleixner int decode_rs8(struct rs_control *rsc, uint8_t *data, uint16_t *par, int len,
3621da177e4SLinus Torvalds 	       uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
3631da177e4SLinus Torvalds 	       uint16_t *corr)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds #include "decode_rs.c"
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(decode_rs8);
3681da177e4SLinus Torvalds #endif
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds #ifdef CONFIG_REED_SOLOMON_ENC16
3711da177e4SLinus Torvalds /**
3721da177e4SLinus Torvalds  *  encode_rs16 - Calculate the parity for data values (16bit data width)
37321633981SThomas Gleixner  *  @rsc:	the rs control structure
3741da177e4SLinus Torvalds  *  @data:	data field of a given type
3751da177e4SLinus Torvalds  *  @len:	data length
3761da177e4SLinus Torvalds  *  @par:	parity data, must be initialized by caller (usually all 0)
3771da177e4SLinus Torvalds  *  @invmsk:	invert data mask (will be xored on data, not on parity!)
3781da177e4SLinus Torvalds  *
3791da177e4SLinus Torvalds  *  Each field in the data array contains up to symbol size bits of valid data.
3801da177e4SLinus Torvalds  */
encode_rs16(struct rs_control * rsc,uint16_t * data,int len,uint16_t * par,uint16_t invmsk)38121633981SThomas Gleixner int encode_rs16(struct rs_control *rsc, uint16_t *data, int len, uint16_t *par,
3821da177e4SLinus Torvalds 	uint16_t invmsk)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds #include "encode_rs.c"
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(encode_rs16);
3871da177e4SLinus Torvalds #endif
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds #ifdef CONFIG_REED_SOLOMON_DEC16
3901da177e4SLinus Torvalds /**
3911da177e4SLinus Torvalds  *  decode_rs16 - Decode codeword (16bit data width)
39221633981SThomas Gleixner  *  @rsc:	the rs control structure
3931da177e4SLinus Torvalds  *  @data:	data field of a given type
3941da177e4SLinus Torvalds  *  @par:	received parity data field
3951da177e4SLinus Torvalds  *  @len:	data length
396*38cbae14SFerdinand Blomqvist  *  @s: 	syndrome data field, must be in index form
397*38cbae14SFerdinand Blomqvist  *		(if NULL, syndrome is calculated)
3981da177e4SLinus Torvalds  *  @no_eras:	number of erasures
3991da177e4SLinus Torvalds  *  @eras_pos:	position of erasures, can be NULL
4001da177e4SLinus Torvalds  *  @invmsk:	invert data mask (will be xored on data, not on parity!)
4011da177e4SLinus Torvalds  *  @corr:	buffer to store correction bitmask on eras_pos
4021da177e4SLinus Torvalds  *
4031da177e4SLinus Torvalds  *  Each field in the data array contains up to symbol size bits of valid data.
40445888b40SThomas Gleixner  *
40545888b40SThomas Gleixner  *  Note: The rc_control struct @rsc contains buffers which are used for
40645888b40SThomas Gleixner  *  decoding, so the caller has to ensure that decoder invocations are
40745888b40SThomas Gleixner  *  serialized.
40845888b40SThomas Gleixner  *
409*38cbae14SFerdinand Blomqvist  *  Returns the number of corrected symbols or -EBADMSG for uncorrectable
410*38cbae14SFerdinand Blomqvist  *  errors. The count includes errors in the parity.
4111da177e4SLinus Torvalds  */
decode_rs16(struct rs_control * rsc,uint16_t * data,uint16_t * par,int len,uint16_t * s,int no_eras,int * eras_pos,uint16_t invmsk,uint16_t * corr)41221633981SThomas Gleixner int decode_rs16(struct rs_control *rsc, uint16_t *data, uint16_t *par, int len,
4131da177e4SLinus Torvalds 		uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
4141da177e4SLinus Torvalds 		uint16_t *corr)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds #include "decode_rs.c"
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(decode_rs16);
4191da177e4SLinus Torvalds #endif
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds MODULE_LICENSE("GPL");
4221da177e4SLinus Torvalds MODULE_DESCRIPTION("Reed Solomon encoder/decoder");
4231da177e4SLinus Torvalds MODULE_AUTHOR("Phil Karn, Thomas Gleixner");
4241da177e4SLinus Torvalds 
425