Reed-Solomon Library Programming Interface¶
- Author:
Thomas Gleixner
Introduction¶
The generic Reed-Solomon Library provides encoding, decoding and errorcorrection functions.
Reed-Solomon codes are used in communication and storage applications toensure data integrity.
This documentation is provided for developers who want to utilize thefunctions provided by the library.
Known Bugs And Assumptions¶
None.
Usage¶
This chapter provides examples of how to use the library.
Initializing¶
The init function init_rs returns a pointer to an rs decoder structure,which holds the necessary information for encoding, decoding and errorcorrection with the given polynomial. It either uses an existingmatching decoder or creates a new one. On creation all the lookup tablesfor fast en/decoding are created. The function may take a while, so makesure not to call it in critical code paths.
/* the Reed Solomon control structure */static struct rs_control *rs_decoder;/* Symbolsize is 10 (bits) * Primitive polynomial is x^10+x^3+1 * first consecutive root is 0 * primitive element to generate roots = 1 * generator polynomial degree (number of roots) = 6 */rs_decoder = init_rs (10, 0x409, 0, 1, 6);
Encoding¶
The encoder calculates the Reed-Solomon code over the given data lengthand stores the result in the parity buffer. Note that the parity buffermust be initialized before calling the encoder.
The expanded data can be inverted on the fly by providing a non-zeroinversion mask. The expanded data is XOR’ed with the mask. This is usede.g. for FLASH ECC, where the all 0xFF is inverted to an all 0x00. TheReed-Solomon code for all 0x00 is all 0x00. The code is inverted beforestoring to FLASH so it is 0xFF too. This prevents that reading from anerased FLASH results in ECC errors.
The databytes are expanded to the given symbol size on the fly. There isno support for encoding continuous bitstreams with a symbol size != 8 atthe moment. If it is necessary it should be not a big deal to implementsuch functionality.
/* Parity buffer. Size = number of roots */uint16_t par[6];/* Initialize the parity buffer */memset(par, 0, sizeof(par));/* Encode 512 byte in data8. Store parity in buffer par */encode_rs8 (rs_decoder, data8, 512, par, 0);
Decoding¶
The decoder calculates the syndrome over the given data length and thereceived parity symbols and corrects errors in the data.
If a syndrome is available from a hardware decoder then the syndromecalculation is skipped.
The correction of the data buffer can be suppressed by providing acorrection pattern buffer and an error location buffer to the decoder.The decoder stores the calculated error location and the correctionbitmask in the given buffers. This is useful for hardware decoders whichuse a weird bit ordering scheme.
The databytes are expanded to the given symbol size on the fly. There isno support for decoding continuous bitstreams with a symbolsize != 8 atthe moment. If it is necessary it should be not a big deal to implementsuch functionality.
Decoding with syndrome calculation, direct data correction¶
/* Parity buffer. Size = number of roots */uint16_t par[6];uint8_t data[512];int numerr;/* Receive data */...../* Receive parity */...../* Decode 512 byte in data8.*/numerr = decode_rs8 (rs_decoder, data8, par, 512, NULL, 0, NULL, 0, NULL);
Decoding with syndrome given by hardware decoder, direct data correction¶
/* Parity buffer. Size = number of roots */uint16_t par[6], syn[6];uint8_t data[512];int numerr;/* Receive data */...../* Receive parity */...../* Get syndrome from hardware decoder */...../* Decode 512 byte in data8.*/numerr = decode_rs8 (rs_decoder, data8, par, 512, syn, 0, NULL, 0, NULL);
Decoding with syndrome given by hardware decoder, no direct data correction.¶
Note: It’s not necessary to give data and received parity to thedecoder.
/* Parity buffer. Size = number of roots */uint16_t par[6], syn[6], corr[8];uint8_t data[512];int numerr, errpos[8];/* Receive data */...../* Receive parity */...../* Get syndrome from hardware decoder */...../* Decode 512 byte in data8.*/numerr = decode_rs8 (rs_decoder, NULL, NULL, 512, syn, 0, errpos, 0, corr);for (i = 0; i < numerr; i++) { do_error_correction_in_your_buffer(errpos[i], corr[i]);}Cleanup¶
The function free_rs frees the allocated resources, if the caller isthe last user of the decoder.
/* Release resources */free_rs(rs_decoder);
Structures¶
This chapter contains the autogenerated documentation of the structureswhich are used in the Reed-Solomon Library and are relevant for adeveloper.
- structrs_codec¶
rs codec data
Definition:
struct rs_codec { int mm; int nn; uint16_t *alpha_to; uint16_t *index_of; uint16_t *genpoly; int nroots; int fcr; int prim; int iprim; int gfpoly; int (*gffunc)(int); int users; struct list_head list;};Members
mmBits per symbol
nnSymbols per block (= (1<<mm)-1)
alpha_tolog lookup table
index_ofAntilog lookup table
genpolyGenerator polynomial
nrootsNumber of generator roots = number of parity symbols
fcrFirst consecutive root, index form
primPrimitive element, index form
iprimprim-th root of 1, index form
gfpolyThe primitive generator polynominal
gffuncFunction to generate the field, if non-canonical representation
usersUsers of this structure
listList entry for the rs codec list
- structrs_control¶
rs control structure per instance
Definition:
struct rs_control { struct rs_codec *codec; uint16_t buffers[];};Members
codecThe codec used for this instance
buffersInternal scratch buffers used in calls to
decode_rs()
- structrs_control*init_rs(intsymsize,intgfpoly,intfcr,intprim,intnroots)¶
Create a RS control
structandinitialize it
Parameters
intsymsizethe symbol size (number of bits)
intgfpolythe extended Galois field generator polynomial coefficients,with the 0th coefficient in the low order bit. The polynomialmust be primitive;
intfcrthe first consecutive root of the rs code generator polynomialin index form
intprimprimitive element to generate polynomial roots
intnrootsRS code generator polynomial degree (number of roots)
Description
Allocations use GFP_KERNEL.
Public Functions Provided¶
This chapter contains the autogenerated documentation of theReed-Solomon functions which are exported.
- voidfree_rs(structrs_control*rs)¶
Free the rs control structure
Parameters
structrs_control*rsThe control structure which is not longer used by thecaller
Description
Free the control structure. Ifrs is the last user of the associatedcodec, free the codec as well.
- structrs_control*init_rs_gfp(intsymsize,intgfpoly,intfcr,intprim,intnroots,gfp_tgfp)¶
Create a RS control
structandinitialize it
Parameters
intsymsizethe symbol size (number of bits)
intgfpolythe extended Galois field generator polynomial coefficients,with the 0th coefficient in the low order bit. The polynomialmust be primitive;
intfcrthe first consecutive root of the rs code generator polynomialin index form
intprimprimitive element to generate polynomial roots
intnrootsRS code generator polynomial degree (number of roots)
gfp_tgfpMemory allocation flags.
- structrs_control*init_rs_non_canonical(intsymsize,int(*gffunc)(int),intfcr,intprim,intnroots)¶
Allocate rs control struct for fields with non-canonical representation
Parameters
intsymsizethe symbol size (number of bits)
int(*gffunc)(int)pointer to function to generate the next field element,or the multiplicative identity element if given 0. Usedinstead of gfpoly if gfpoly is 0
intfcrthe first consecutive root of the rs code generator polynomialin index form
intprimprimitive element to generate polynomial roots
intnrootsRS code generator polynomial degree (number of roots)
- intencode_rs8(structrs_control*rsc,uint8_t*data,intlen,uint16_t*par,uint16_tinvmsk)¶
Calculate the parity for data values (8bit data width)
Parameters
structrs_control*rscthe rs control structure
uint8_t*datadata field of a given type
intlendata length
uint16_t*parparity data, must be initialized by caller (usually all 0)
uint16_tinvmskinvert data mask (will be xored on data)
Description
The parity uses a uint16_t data type to enablesymbol size > 8. The calling code must take care of encoding of thesyndrome result for storage itself.
- intdecode_rs8(structrs_control*rsc,uint8_t*data,uint16_t*par,intlen,uint16_t*s,intno_eras,int*eras_pos,uint16_tinvmsk,uint16_t*corr)¶
Decode codeword (8bit data width)
Parameters
structrs_control*rscthe rs control structure
uint8_t*datadata field of a given type
uint16_t*parreceived parity data field
intlendata length
uint16_t*ssyndrome data field, must be in index form(if NULL, syndrome is calculated)
intno_erasnumber of erasures
int*eras_posposition of erasures, can be NULL
uint16_tinvmskinvert data mask (will be xored on data, not on parity!)
uint16_t*corrbuffer to store correction bitmask on eras_pos
Description
The syndrome and parity uses a uint16_t data type to enablesymbol size > 8. The calling code must take care of decoding of thesyndrome result and the received parity before calling this code.
Note
- The rs_control structrsc contains buffers which are used for
decoding, so the caller has to ensure that decoder invocations areserialized.
Returns the number of corrected symbols or -EBADMSG for uncorrectableerrors. The count includes errors in the parity.
- intencode_rs16(structrs_control*rsc,uint16_t*data,intlen,uint16_t*par,uint16_tinvmsk)¶
Calculate the parity for data values (16bit data width)
Parameters
structrs_control*rscthe rs control structure
uint16_t*datadata field of a given type
intlendata length
uint16_t*parparity data, must be initialized by caller (usually all 0)
uint16_tinvmskinvert data mask (will be xored on data, not on parity!)
Description
Each field in the data array contains up to symbol size bits of valid data.
- intdecode_rs16(structrs_control*rsc,uint16_t*data,uint16_t*par,intlen,uint16_t*s,intno_eras,int*eras_pos,uint16_tinvmsk,uint16_t*corr)¶
Decode codeword (16bit data width)
Parameters
structrs_control*rscthe rs control structure
uint16_t*datadata field of a given type
uint16_t*parreceived parity data field
intlendata length
uint16_t*ssyndrome data field, must be in index form(if NULL, syndrome is calculated)
intno_erasnumber of erasures
int*eras_posposition of erasures, can be NULL
uint16_tinvmskinvert data mask (will be xored on data, not on parity!)
uint16_t*corrbuffer to store correction bitmask on eras_pos
Description
Each field in the data array contains up to symbol size bits of valid data.
Note
- The rc_control structrsc contains buffers which are used for
decoding, so the caller has to ensure that decoder invocations areserialized.
Returns the number of corrected symbols or -EBADMSG for uncorrectableerrors. The count includes errors in the parity.
Credits¶
The library code for encoding and decoding was written by Phil Karn.
Copyright 2002, Phil Karn, KA9QMay be used under the terms of the GNU General Public License (GPL)
The wrapper functions and interfaces are written by Thomas Gleixner.
Many users have provided bugfixes, improvements and helping hands fortesting. Thanks a lot.
The following people have contributed to this document:
Thomas Gleixnertglx@linutronix.de