ia64/linux-2.6.18-xen.hg

annotate lib/reed_solomon/reed_solomon.c @ 0:831230e53067

Import 2.6.18 from kernel.org tarball.
author Ian Campbell <ian.campbell@xensource.com>
date Wed Apr 11 14:15:44 2007 +0100 (2007-04-11)
parents
children
rev   line source
ian@0 1 /*
ian@0 2 * lib/reed_solomon/rslib.c
ian@0 3 *
ian@0 4 * Overview:
ian@0 5 * Generic Reed Solomon encoder / decoder library
ian@0 6 *
ian@0 7 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
ian@0 8 *
ian@0 9 * Reed Solomon code lifted from reed solomon library written by Phil Karn
ian@0 10 * Copyright 2002 Phil Karn, KA9Q
ian@0 11 *
ian@0 12 * $Id: rslib.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $
ian@0 13 *
ian@0 14 * This program is free software; you can redistribute it and/or modify
ian@0 15 * it under the terms of the GNU General Public License version 2 as
ian@0 16 * published by the Free Software Foundation.
ian@0 17 *
ian@0 18 * Description:
ian@0 19 *
ian@0 20 * The generic Reed Solomon library provides runtime configurable
ian@0 21 * encoding / decoding of RS codes.
ian@0 22 * Each user must call init_rs to get a pointer to a rs_control
ian@0 23 * structure for the given rs parameters. This structure is either
ian@0 24 * generated or a already available matching control structure is used.
ian@0 25 * If a structure is generated then the polynomial arrays for
ian@0 26 * fast encoding / decoding are built. This can take some time so
ian@0 27 * make sure not to call this function from a time critical path.
ian@0 28 * Usually a module / driver should initialize the necessary
ian@0 29 * rs_control structure on module / driver init and release it
ian@0 30 * on exit.
ian@0 31 * The encoding puts the calculated syndrome into a given syndrome
ian@0 32 * buffer.
ian@0 33 * The decoding is a two step process. The first step calculates
ian@0 34 * the syndrome over the received (data + syndrome) and calls the
ian@0 35 * second stage, which does the decoding / error correction itself.
ian@0 36 * Many hw encoders provide a syndrome calculation over the received
ian@0 37 * data + syndrome and can call the second stage directly.
ian@0 38 *
ian@0 39 */
ian@0 40
ian@0 41 #include <linux/errno.h>
ian@0 42 #include <linux/kernel.h>
ian@0 43 #include <linux/init.h>
ian@0 44 #include <linux/module.h>
ian@0 45 #include <linux/rslib.h>
ian@0 46 #include <linux/slab.h>
ian@0 47 #include <linux/mutex.h>
ian@0 48 #include <asm/semaphore.h>
ian@0 49
ian@0 50 /* This list holds all currently allocated rs control structures */
ian@0 51 static LIST_HEAD (rslist);
ian@0 52 /* Protection for the list */
ian@0 53 static DEFINE_MUTEX(rslistlock);
ian@0 54
ian@0 55 /**
ian@0 56 * rs_init - Initialize a Reed-Solomon codec
ian@0 57 * @symsize: symbol size, bits (1-8)
ian@0 58 * @gfpoly: Field generator polynomial coefficients
ian@0 59 * @fcr: first root of RS code generator polynomial, index form
ian@0 60 * @prim: primitive element to generate polynomial roots
ian@0 61 * @nroots: RS code generator polynomial degree (number of roots)
ian@0 62 *
ian@0 63 * Allocate a control structure and the polynom arrays for faster
ian@0 64 * en/decoding. Fill the arrays according to the given parameters.
ian@0 65 */
ian@0 66 static struct rs_control *rs_init(int symsize, int gfpoly, int fcr,
ian@0 67 int prim, int nroots)
ian@0 68 {
ian@0 69 struct rs_control *rs;
ian@0 70 int i, j, sr, root, iprim;
ian@0 71
ian@0 72 /* Allocate the control structure */
ian@0 73 rs = kmalloc(sizeof (struct rs_control), GFP_KERNEL);
ian@0 74 if (rs == NULL)
ian@0 75 return NULL;
ian@0 76
ian@0 77 INIT_LIST_HEAD(&rs->list);
ian@0 78
ian@0 79 rs->mm = symsize;
ian@0 80 rs->nn = (1 << symsize) - 1;
ian@0 81 rs->fcr = fcr;
ian@0 82 rs->prim = prim;
ian@0 83 rs->nroots = nroots;
ian@0 84 rs->gfpoly = gfpoly;
ian@0 85
ian@0 86 /* Allocate the arrays */
ian@0 87 rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL);
ian@0 88 if (rs->alpha_to == NULL)
ian@0 89 goto errrs;
ian@0 90
ian@0 91 rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL);
ian@0 92 if (rs->index_of == NULL)
ian@0 93 goto erralp;
ian@0 94
ian@0 95 rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), GFP_KERNEL);
ian@0 96 if(rs->genpoly == NULL)
ian@0 97 goto erridx;
ian@0 98
ian@0 99 /* Generate Galois field lookup tables */
ian@0 100 rs->index_of[0] = rs->nn; /* log(zero) = -inf */
ian@0 101 rs->alpha_to[rs->nn] = 0; /* alpha**-inf = 0 */
ian@0 102 sr = 1;
ian@0 103 for (i = 0; i < rs->nn; i++) {
ian@0 104 rs->index_of[sr] = i;
ian@0 105 rs->alpha_to[i] = sr;
ian@0 106 sr <<= 1;
ian@0 107 if (sr & (1 << symsize))
ian@0 108 sr ^= gfpoly;
ian@0 109 sr &= rs->nn;
ian@0 110 }
ian@0 111 /* If it's not primitive, exit */
ian@0 112 if(sr != 1)
ian@0 113 goto errpol;
ian@0 114
ian@0 115 /* Find prim-th root of 1, used in decoding */
ian@0 116 for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn);
ian@0 117 /* prim-th root of 1, index form */
ian@0 118 rs->iprim = iprim / prim;
ian@0 119
ian@0 120 /* Form RS code generator polynomial from its roots */
ian@0 121 rs->genpoly[0] = 1;
ian@0 122 for (i = 0, root = fcr * prim; i < nroots; i++, root += prim) {
ian@0 123 rs->genpoly[i + 1] = 1;
ian@0 124 /* Multiply rs->genpoly[] by @**(root + x) */
ian@0 125 for (j = i; j > 0; j--) {
ian@0 126 if (rs->genpoly[j] != 0) {
ian@0 127 rs->genpoly[j] = rs->genpoly[j -1] ^
ian@0 128 rs->alpha_to[rs_modnn(rs,
ian@0 129 rs->index_of[rs->genpoly[j]] + root)];
ian@0 130 } else
ian@0 131 rs->genpoly[j] = rs->genpoly[j - 1];
ian@0 132 }
ian@0 133 /* rs->genpoly[0] can never be zero */
ian@0 134 rs->genpoly[0] =
ian@0 135 rs->alpha_to[rs_modnn(rs,
ian@0 136 rs->index_of[rs->genpoly[0]] + root)];
ian@0 137 }
ian@0 138 /* convert rs->genpoly[] to index form for quicker encoding */
ian@0 139 for (i = 0; i <= nroots; i++)
ian@0 140 rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
ian@0 141 return rs;
ian@0 142
ian@0 143 /* Error exit */
ian@0 144 errpol:
ian@0 145 kfree(rs->genpoly);
ian@0 146 erridx:
ian@0 147 kfree(rs->index_of);
ian@0 148 erralp:
ian@0 149 kfree(rs->alpha_to);
ian@0 150 errrs:
ian@0 151 kfree(rs);
ian@0 152 return NULL;
ian@0 153 }
ian@0 154
ian@0 155
ian@0 156 /**
ian@0 157 * free_rs - Free the rs control structure, if it is no longer used
ian@0 158 * @rs: the control structure which is not longer used by the
ian@0 159 * caller
ian@0 160 */
ian@0 161 void free_rs(struct rs_control *rs)
ian@0 162 {
ian@0 163 mutex_lock(&rslistlock);
ian@0 164 rs->users--;
ian@0 165 if(!rs->users) {
ian@0 166 list_del(&rs->list);
ian@0 167 kfree(rs->alpha_to);
ian@0 168 kfree(rs->index_of);
ian@0 169 kfree(rs->genpoly);
ian@0 170 kfree(rs);
ian@0 171 }
ian@0 172 mutex_unlock(&rslistlock);
ian@0 173 }
ian@0 174
ian@0 175 /**
ian@0 176 * init_rs - Find a matching or allocate a new rs control structure
ian@0 177 * @symsize: the symbol size (number of bits)
ian@0 178 * @gfpoly: the extended Galois field generator polynomial coefficients,
ian@0 179 * with the 0th coefficient in the low order bit. The polynomial
ian@0 180 * must be primitive;
ian@0 181 * @fcr: the first consecutive root of the rs code generator polynomial
ian@0 182 * in index form
ian@0 183 * @prim: primitive element to generate polynomial roots
ian@0 184 * @nroots: RS code generator polynomial degree (number of roots)
ian@0 185 */
ian@0 186 struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
ian@0 187 int nroots)
ian@0 188 {
ian@0 189 struct list_head *tmp;
ian@0 190 struct rs_control *rs;
ian@0 191
ian@0 192 /* Sanity checks */
ian@0 193 if (symsize < 1)
ian@0 194 return NULL;
ian@0 195 if (fcr < 0 || fcr >= (1<<symsize))
ian@0 196 return NULL;
ian@0 197 if (prim <= 0 || prim >= (1<<symsize))
ian@0 198 return NULL;
ian@0 199 if (nroots < 0 || nroots >= (1<<symsize))
ian@0 200 return NULL;
ian@0 201
ian@0 202 mutex_lock(&rslistlock);
ian@0 203
ian@0 204 /* Walk through the list and look for a matching entry */
ian@0 205 list_for_each(tmp, &rslist) {
ian@0 206 rs = list_entry(tmp, struct rs_control, list);
ian@0 207 if (symsize != rs->mm)
ian@0 208 continue;
ian@0 209 if (gfpoly != rs->gfpoly)
ian@0 210 continue;
ian@0 211 if (fcr != rs->fcr)
ian@0 212 continue;
ian@0 213 if (prim != rs->prim)
ian@0 214 continue;
ian@0 215 if (nroots != rs->nroots)
ian@0 216 continue;
ian@0 217 /* We have a matching one already */
ian@0 218 rs->users++;
ian@0 219 goto out;
ian@0 220 }
ian@0 221
ian@0 222 /* Create a new one */
ian@0 223 rs = rs_init(symsize, gfpoly, fcr, prim, nroots);
ian@0 224 if (rs) {
ian@0 225 rs->users = 1;
ian@0 226 list_add(&rs->list, &rslist);
ian@0 227 }
ian@0 228 out:
ian@0 229 mutex_unlock(&rslistlock);
ian@0 230 return rs;
ian@0 231 }
ian@0 232
ian@0 233 #ifdef CONFIG_REED_SOLOMON_ENC8
ian@0 234 /**
ian@0 235 * encode_rs8 - Calculate the parity for data values (8bit data width)
ian@0 236 * @rs: the rs control structure
ian@0 237 * @data: data field of a given type
ian@0 238 * @len: data length
ian@0 239 * @par: parity data, must be initialized by caller (usually all 0)
ian@0 240 * @invmsk: invert data mask (will be xored on data)
ian@0 241 *
ian@0 242 * The parity uses a uint16_t data type to enable
ian@0 243 * symbol size > 8. The calling code must take care of encoding of the
ian@0 244 * syndrome result for storage itself.
ian@0 245 */
ian@0 246 int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
ian@0 247 uint16_t invmsk)
ian@0 248 {
ian@0 249 #include "encode_rs.c"
ian@0 250 }
ian@0 251 EXPORT_SYMBOL_GPL(encode_rs8);
ian@0 252 #endif
ian@0 253
ian@0 254 #ifdef CONFIG_REED_SOLOMON_DEC8
ian@0 255 /**
ian@0 256 * decode_rs8 - Decode codeword (8bit data width)
ian@0 257 * @rs: the rs control structure
ian@0 258 * @data: data field of a given type
ian@0 259 * @par: received parity data field
ian@0 260 * @len: data length
ian@0 261 * @s: syndrome data field (if NULL, syndrome is calculated)
ian@0 262 * @no_eras: number of erasures
ian@0 263 * @eras_pos: position of erasures, can be NULL
ian@0 264 * @invmsk: invert data mask (will be xored on data, not on parity!)
ian@0 265 * @corr: buffer to store correction bitmask on eras_pos
ian@0 266 *
ian@0 267 * The syndrome and parity uses a uint16_t data type to enable
ian@0 268 * symbol size > 8. The calling code must take care of decoding of the
ian@0 269 * syndrome result and the received parity before calling this code.
ian@0 270 */
ian@0 271 int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
ian@0 272 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
ian@0 273 uint16_t *corr)
ian@0 274 {
ian@0 275 #include "decode_rs.c"
ian@0 276 }
ian@0 277 EXPORT_SYMBOL_GPL(decode_rs8);
ian@0 278 #endif
ian@0 279
ian@0 280 #ifdef CONFIG_REED_SOLOMON_ENC16
ian@0 281 /**
ian@0 282 * encode_rs16 - Calculate the parity for data values (16bit data width)
ian@0 283 * @rs: the rs control structure
ian@0 284 * @data: data field of a given type
ian@0 285 * @len: data length
ian@0 286 * @par: parity data, must be initialized by caller (usually all 0)
ian@0 287 * @invmsk: invert data mask (will be xored on data, not on parity!)
ian@0 288 *
ian@0 289 * Each field in the data array contains up to symbol size bits of valid data.
ian@0 290 */
ian@0 291 int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
ian@0 292 uint16_t invmsk)
ian@0 293 {
ian@0 294 #include "encode_rs.c"
ian@0 295 }
ian@0 296 EXPORT_SYMBOL_GPL(encode_rs16);
ian@0 297 #endif
ian@0 298
ian@0 299 #ifdef CONFIG_REED_SOLOMON_DEC16
ian@0 300 /**
ian@0 301 * decode_rs16 - Decode codeword (16bit data width)
ian@0 302 * @rs: the rs control structure
ian@0 303 * @data: data field of a given type
ian@0 304 * @par: received parity data field
ian@0 305 * @len: data length
ian@0 306 * @s: syndrome data field (if NULL, syndrome is calculated)
ian@0 307 * @no_eras: number of erasures
ian@0 308 * @eras_pos: position of erasures, can be NULL
ian@0 309 * @invmsk: invert data mask (will be xored on data, not on parity!)
ian@0 310 * @corr: buffer to store correction bitmask on eras_pos
ian@0 311 *
ian@0 312 * Each field in the data array contains up to symbol size bits of valid data.
ian@0 313 */
ian@0 314 int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
ian@0 315 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
ian@0 316 uint16_t *corr)
ian@0 317 {
ian@0 318 #include "decode_rs.c"
ian@0 319 }
ian@0 320 EXPORT_SYMBOL_GPL(decode_rs16);
ian@0 321 #endif
ian@0 322
ian@0 323 EXPORT_SYMBOL_GPL(init_rs);
ian@0 324 EXPORT_SYMBOL_GPL(free_rs);
ian@0 325
ian@0 326 MODULE_LICENSE("GPL");
ian@0 327 MODULE_DESCRIPTION("Reed Solomon encoder/decoder");
ian@0 328 MODULE_AUTHOR("Phil Karn, Thomas Gleixner");
ian@0 329