Thomas Gleixner | dc8f923 | 2018-04-22 18:23:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2 | /* |
Thomas Gleixner | 3413e18 | 2018-04-22 18:23:49 +0200 | [diff] [blame] | 3 | * Generic Reed Solomon encoder / decoder library |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Description: |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * The generic Reed Solomon library provides runtime configurable |
| 13 | * encoding / decoding of RS codes. |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 38 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Thomas Gleixner | 45888b4 | 2018-04-22 18:23:55 +0200 | [diff] [blame] | 40 | enum { |
| 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 | |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 52 | /* This list holds all currently allocated rs codec structures */ |
| 53 | static LIST_HEAD(codec_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* Protection for the list */ |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 55 | static DEFINE_MUTEX(rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 57 | /** |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 58 | * codec_init - Initialize a Reed-Solomon codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * @symsize: symbol size, bits (1-8) |
| 60 | * @gfpoly: Field generator polynomial coefficients |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 61 | * @gffunc: Field generator function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 65 | * @gfp: GFP_ flags for allocations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | * |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 67 | * Allocate a codec structure and the polynom arrays for faster |
Randy Dunlap | 9dc6557 | 2006-06-25 05:49:14 -0700 | [diff] [blame] | 68 | * en/decoding. Fill the arrays according to the given parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | */ |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 70 | static struct rs_codec *codec_init(int symsize, int gfpoly, int (*gffunc)(int), |
| 71 | int fcr, int prim, int nroots, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | int i, j, sr, root, iprim; |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 74 | struct rs_codec *rs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Thomas Gleixner | a85e126 | 2018-04-22 18:23:52 +0200 | [diff] [blame] | 76 | rs = kzalloc(sizeof(*rs), gfp); |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 77 | if (!rs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 88 | rs->gffunc = gffunc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* Allocate the arrays */ |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 91 | rs->alpha_to = kmalloc_array(rs->nn + 1, sizeof(uint16_t), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (rs->alpha_to == NULL) |
Thomas Gleixner | a85e126 | 2018-04-22 18:23:52 +0200 | [diff] [blame] | 93 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 95 | rs->index_of = kmalloc_array(rs->nn + 1, sizeof(uint16_t), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (rs->index_of == NULL) |
Thomas Gleixner | a85e126 | 2018-04-22 18:23:52 +0200 | [diff] [blame] | 97 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 99 | rs->genpoly = kmalloc_array(rs->nroots + 1, sizeof(uint16_t), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if(rs->genpoly == NULL) |
Thomas Gleixner | a85e126 | 2018-04-22 18:23:52 +0200 | [diff] [blame] | 101 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | /* If it's not primitive, exit */ |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 125 | if(sr != rs->alpha_to[0]) |
Thomas Gleixner | a85e126 | 2018-04-22 18:23:52 +0200 | [diff] [blame] | 126 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 140 | rs->genpoly[j] = rs->genpoly[j -1] ^ |
| 141 | rs->alpha_to[rs_modnn(rs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 147 | rs->genpoly[0] = |
| 148 | rs->alpha_to[rs_modnn(rs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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]]; |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 154 | |
| 155 | rs->users = 1; |
| 156 | list_add(&rs->list, &codec_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return rs; |
| 158 | |
Thomas Gleixner | a85e126 | 2018-04-22 18:23:52 +0200 | [diff] [blame] | 159 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | kfree(rs->genpoly); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | kfree(rs->index_of); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | kfree(rs->alpha_to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | kfree(rs); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 168 | /** |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 169 | * free_rs - Free the rs control structure |
| 170 | * @rs: The control structure which is not longer used by the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | * caller |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 172 | * |
| 173 | * Free the control structure. If @rs is the last user of the associated |
| 174 | * codec, free the codec as well. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | */ |
| 176 | void free_rs(struct rs_control *rs) |
| 177 | { |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 178 | struct rs_codec *cd; |
| 179 | |
| 180 | if (!rs) |
| 181 | return; |
| 182 | |
| 183 | cd = rs->codec; |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 184 | mutex_lock(&rslistlock); |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 193 | mutex_unlock(&rslistlock); |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 194 | kfree(rs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 196 | EXPORT_SYMBOL_GPL(free_rs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 198 | /** |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 199 | * init_rs_internal - Allocate rs control, find a matching codec or allocate a new one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 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 |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame] | 207 | * @fcr: the first consecutive root of the rs code generator polynomial |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | * in index form |
| 209 | * @prim: primitive element to generate polynomial roots |
| 210 | * @nroots: RS code generator polynomial degree (number of roots) |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 211 | * @gfp: GFP_ flags for allocations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | */ |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 213 | static struct rs_control *init_rs_internal(int symsize, int gfpoly, |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 214 | int (*gffunc)(int), int fcr, |
| 215 | int prim, int nroots, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 217 | struct list_head *tmp; |
| 218 | struct rs_control *rs; |
Thomas Gleixner | 45888b4 | 2018-04-22 18:23:55 +0200 | [diff] [blame] | 219 | unsigned int bsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | /* Sanity checks */ |
| 222 | if (symsize < 1) |
| 223 | return NULL; |
| 224 | if (fcr < 0 || fcr >= (1<<symsize)) |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame] | 225 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (prim <= 0 || prim >= (1<<symsize)) |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame] | 227 | return NULL; |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 228 | if (nroots < 0 || nroots >= (1<<symsize)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return NULL; |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 230 | |
Thomas Gleixner | 45888b4 | 2018-04-22 18:23:55 +0200 | [diff] [blame] | 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); |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 238 | if (!rs) |
| 239 | return NULL; |
| 240 | |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 241 | mutex_lock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | /* Walk through the list and look for a matching entry */ |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | continue; |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 249 | if (gfpoly != cd->gfpoly) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | continue; |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 251 | if (gffunc != cd->gffunc) |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 252 | continue; |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 253 | if (fcr != cd->fcr) |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 254 | continue; |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 255 | if (prim != cd->prim) |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 256 | continue; |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 257 | if (nroots != cd->nroots) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | continue; |
| 259 | /* We have a matching one already */ |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 260 | cd->users++; |
| 261 | rs->codec = cd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | goto out; |
| 263 | } |
| 264 | |
| 265 | /* Create a new one */ |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 266 | rs->codec = codec_init(symsize, gfpoly, gffunc, fcr, prim, nroots, gfp); |
| 267 | if (!rs->codec) { |
| 268 | kfree(rs); |
| 269 | rs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 271 | out: |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 272 | mutex_unlock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return rs; |
| 274 | } |
| 275 | |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 276 | /** |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 277 | * init_rs_gfp - Create a RS control struct and initialize it |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 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; |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame] | 282 | * @fcr: the first consecutive root of the rs code generator polynomial |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 283 | * in index form |
| 284 | * @prim: primitive element to generate polynomial roots |
| 285 | * @nroots: RS code generator polynomial degree (number of roots) |
Matthew Wilcox | 8dd9987 | 2018-07-03 12:43:54 -0700 | [diff] [blame] | 286 | * @gfp: Memory allocation flags. |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 287 | */ |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 288 | struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim, |
| 289 | int nroots, gfp_t gfp) |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 290 | { |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 291 | return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots, gfp); |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 292 | } |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 293 | EXPORT_SYMBOL_GPL(init_rs_gfp); |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 294 | |
| 295 | /** |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 296 | * init_rs_non_canonical - Allocate rs control struct for fields with |
| 297 | * non-canonical representation |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 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 |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame] | 302 | * @fcr: the first consecutive root of the rs code generator polynomial |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 303 | * in index form |
| 304 | * @prim: primitive element to generate polynomial roots |
| 305 | * @nroots: RS code generator polynomial degree (number of roots) |
| 306 | */ |
| 307 | struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int), |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame] | 308 | int fcr, int prim, int nroots) |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 309 | { |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 310 | return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots, |
| 311 | GFP_KERNEL); |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 312 | } |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(init_rs_non_canonical); |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | #ifdef CONFIG_REED_SOLOMON_ENC8 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 316 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | * encode_rs8 - Calculate the parity for data values (8bit data width) |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 318 | * @rsc: the rs control structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | * @data: data field of a given type |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 320 | * @len: data length |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 328 | int encode_rs8(struct rs_control *rsc, uint8_t *data, int len, uint16_t *par, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | uint16_t invmsk) |
| 330 | { |
| 331 | #include "encode_rs.c" |
| 332 | } |
| 333 | EXPORT_SYMBOL_GPL(encode_rs8); |
| 334 | #endif |
| 335 | |
| 336 | #ifdef CONFIG_REED_SOLOMON_DEC8 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 337 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | * decode_rs8 - Decode codeword (8bit data width) |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 339 | * @rsc: the rs control structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | * @data: data field of a given type |
| 341 | * @par: received parity data field |
| 342 | * @len: data length |
Ferdinand Blomqvist | 38cbae1 | 2019-06-20 17:10:38 +0300 | [diff] [blame] | 343 | * @s: syndrome data field, must be in index form |
| 344 | * (if NULL, syndrome is calculated) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Thomas Gleixner | 45888b4 | 2018-04-22 18:23:55 +0200 | [diff] [blame] | 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 | * |
Ferdinand Blomqvist | 38cbae1 | 2019-06-20 17:10:38 +0300 | [diff] [blame] | 358 | * Returns the number of corrected symbols or -EBADMSG for uncorrectable |
| 359 | * errors. The count includes errors in the parity. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | */ |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 361 | int decode_rs8(struct rs_control *rsc, uint8_t *data, uint16_t *par, int len, |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 362 | uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | uint16_t *corr) |
| 364 | { |
| 365 | #include "decode_rs.c" |
| 366 | } |
| 367 | EXPORT_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) |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 373 | * @rsc: the rs control structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | * @data: data field of a given type |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 375 | * @len: data length |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 381 | int encode_rs16(struct rs_control *rsc, uint16_t *data, int len, uint16_t *par, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | uint16_t invmsk) |
| 383 | { |
| 384 | #include "encode_rs.c" |
| 385 | } |
| 386 | EXPORT_SYMBOL_GPL(encode_rs16); |
| 387 | #endif |
| 388 | |
| 389 | #ifdef CONFIG_REED_SOLOMON_DEC16 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 390 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | * decode_rs16 - Decode codeword (16bit data width) |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 392 | * @rsc: the rs control structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | * @data: data field of a given type |
| 394 | * @par: received parity data field |
| 395 | * @len: data length |
Ferdinand Blomqvist | 38cbae1 | 2019-06-20 17:10:38 +0300 | [diff] [blame] | 396 | * @s: syndrome data field, must be in index form |
| 397 | * (if NULL, syndrome is calculated) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | * @no_eras: number of erasures |
| 399 | * @eras_pos: position of erasures, can be NULL |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 400 | * @invmsk: invert data mask (will be xored on data, not on parity!) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Thomas Gleixner | 45888b4 | 2018-04-22 18:23:55 +0200 | [diff] [blame] | 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 | * |
Ferdinand Blomqvist | 38cbae1 | 2019-06-20 17:10:38 +0300 | [diff] [blame] | 409 | * Returns the number of corrected symbols or -EBADMSG for uncorrectable |
| 410 | * errors. The count includes errors in the parity. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | */ |
Thomas Gleixner | 2163398 | 2018-04-22 18:23:53 +0200 | [diff] [blame] | 412 | int decode_rs16(struct rs_control *rsc, uint16_t *data, uint16_t *par, int len, |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 413 | uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | uint16_t *corr) |
| 415 | { |
| 416 | #include "decode_rs.c" |
| 417 | } |
| 418 | EXPORT_SYMBOL_GPL(decode_rs16); |
| 419 | #endif |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | MODULE_LICENSE("GPL"); |
| 422 | MODULE_DESCRIPTION("Reed Solomon encoder/decoder"); |
| 423 | MODULE_AUTHOR("Phil Karn, Thomas Gleixner"); |
| 424 | |