// SPDX-License-Identifier: GPL-2.0 /* * Generic Reed Solomon encoder / decoder library * * Copyright (C) 2004 Thomas Gleixner ([email protected]) * * Reed Solomon code lifted from reed solomon library written by Phil Karn * Copyright 2002 Phil Karn, KA9Q * * Description: * * The generic Reed Solomon library provides runtime configurable * encoding / decoding of RS codes. * * Each user must call init_rs to get a pointer to a rs_control structure * for the given rs parameters. The control struct is unique per instance. * It points to a codec which can be shared by multiple control structures. * If a codec is newly allocated then the polynomial arrays for fast * encoding / decoding are built. This can take some time so make sure not * to call this function from a time critical path. Usually a module / * driver should initialize the necessary rs_control structure on module / * driver init and release it on exit. * * The encoding puts the calculated syndrome into a given syndrome buffer. * * The decoding is a two step process. The first step calculates the * syndrome over the received (data + syndrome) and calls the second stage, * which does the decoding / error correction itself. Many hw encoders * provide a syndrome calculation over the received data + syndrome and can * call the second stage directly. */ #include <linux/errno.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/module.h> #include <linux/rslib.h> #include <linux/slab.h> #include <linux/mutex.h> enum { … }; /* This list holds all currently allocated rs codec structures */ static LIST_HEAD(codec_list); /* Protection for the list */ static DEFINE_MUTEX(rslistlock); /** * codec_init - Initialize a Reed-Solomon codec * @symsize: symbol size, bits (1-8) * @gfpoly: Field generator polynomial coefficients * @gffunc: Field generator function * @fcr: first root of RS code generator polynomial, index form * @prim: primitive element to generate polynomial roots * @nroots: RS code generator polynomial degree (number of roots) * @gfp: GFP_ flags for allocations * * Allocate a codec structure and the polynom arrays for faster * en/decoding. Fill the arrays according to the given parameters. */ static struct rs_codec *codec_init(int symsize, int gfpoly, int (*gffunc)(int), int fcr, int prim, int nroots, gfp_t gfp) { … } /** * free_rs - Free the rs control structure * @rs: The control structure which is not longer used by the * caller * * Free the control structure. If @rs is the last user of the associated * codec, free the codec as well. */ void free_rs(struct rs_control *rs) { … } EXPORT_SYMBOL_GPL(…); /** * init_rs_internal - Allocate rs control, find a matching codec or allocate a new one * @symsize: the symbol size (number of bits) * @gfpoly: the extended Galois field generator polynomial coefficients, * with the 0th coefficient in the low order bit. The polynomial * must be primitive; * @gffunc: pointer to function to generate the next field element, * or the multiplicative identity element if given 0. Used * instead of gfpoly if gfpoly is 0 * @fcr: the first consecutive root of the rs code generator polynomial * in index form * @prim: primitive element to generate polynomial roots * @nroots: RS code generator polynomial degree (number of roots) * @gfp: GFP_ flags for allocations */ static struct rs_control *init_rs_internal(int symsize, int gfpoly, int (*gffunc)(int), int fcr, int prim, int nroots, gfp_t gfp) { … } /** * init_rs_gfp - Create a RS control struct and initialize it * @symsize: the symbol size (number of bits) * @gfpoly: the extended Galois field generator polynomial coefficients, * with the 0th coefficient in the low order bit. The polynomial * must be primitive; * @fcr: the first consecutive root of the rs code generator polynomial * in index form * @prim: primitive element to generate polynomial roots * @nroots: RS code generator polynomial degree (number of roots) * @gfp: Memory allocation flags. */ struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim, int nroots, gfp_t gfp) { … } EXPORT_SYMBOL_GPL(…); /** * init_rs_non_canonical - Allocate rs control struct for fields with * non-canonical representation * @symsize: the symbol size (number of bits) * @gffunc: pointer to function to generate the next field element, * or the multiplicative identity element if given 0. Used * instead of gfpoly if gfpoly is 0 * @fcr: the first consecutive root of the rs code generator polynomial * in index form * @prim: primitive element to generate polynomial roots * @nroots: RS code generator polynomial degree (number of roots) */ struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int), int fcr, int prim, int nroots) { … } EXPORT_SYMBOL_GPL(…); #ifdef CONFIG_REED_SOLOMON_ENC8 /** * encode_rs8 - Calculate the parity for data values (8bit data width) * @rsc: the rs control structure * @data: data field of a given type * @len: data length * @par: parity data, must be initialized by caller (usually all 0) * @invmsk: invert data mask (will be xored on data) * * The parity uses a uint16_t data type to enable * symbol size > 8. The calling code must take care of encoding of the * syndrome result for storage itself. */ int encode_rs8(struct rs_control *rsc, uint8_t *data, int len, uint16_t *par, uint16_t invmsk) { … } EXPORT_SYMBOL_GPL(…); #endif #ifdef CONFIG_REED_SOLOMON_DEC8 /** * decode_rs8 - Decode codeword (8bit data width) * @rsc: the rs control structure * @data: data field of a given type * @par: received parity data field * @len: data length * @s: syndrome data field, must be in index form * (if NULL, syndrome is calculated) * @no_eras: number of erasures * @eras_pos: position of erasures, can be NULL * @invmsk: invert data mask (will be xored on data, not on parity!) * @corr: buffer to store correction bitmask on eras_pos * * The syndrome and parity uses a uint16_t data type to enable * symbol size > 8. The calling code must take care of decoding of the * syndrome result and the received parity before calling this code. * * Note: The rs_control struct @rsc contains buffers which are used for * decoding, so the caller has to ensure that decoder invocations are * serialized. * * Returns the number of corrected symbols or -EBADMSG for uncorrectable * errors. The count includes errors in the parity. */ int 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) { … } EXPORT_SYMBOL_GPL(…); #endif #ifdef CONFIG_REED_SOLOMON_ENC16 /** * encode_rs16 - Calculate the parity for data values (16bit data width) * @rsc: the rs control structure * @data: data field of a given type * @len: data length * @par: parity data, must be initialized by caller (usually all 0) * @invmsk: invert data mask (will be xored on data, not on parity!) * * Each field in the data array contains up to symbol size bits of valid data. */ int encode_rs16(struct rs_control *rsc, uint16_t *data, int len, uint16_t *par, uint16_t invmsk) { … } EXPORT_SYMBOL_GPL(…); #endif #ifdef CONFIG_REED_SOLOMON_DEC16 /** * decode_rs16 - Decode codeword (16bit data width) * @rsc: the rs control structure * @data: data field of a given type * @par: received parity data field * @len: data length * @s: syndrome data field, must be in index form * (if NULL, syndrome is calculated) * @no_eras: number of erasures * @eras_pos: position of erasures, can be NULL * @invmsk: invert data mask (will be xored on data, not on parity!) * @corr: buffer to store correction bitmask on eras_pos * * Each field in the data array contains up to symbol size bits of valid data. * * Note: The rc_control struct @rsc contains buffers which are used for * decoding, so the caller has to ensure that decoder invocations are * serialized. * * Returns the number of corrected symbols or -EBADMSG for uncorrectable * errors. The count includes errors in the parity. */ int 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) { … } EXPORT_SYMBOL_GPL(…); #endif MODULE_LICENSE(…) …; MODULE_DESCRIPTION(…) …; MODULE_AUTHOR(…) …;