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 | * RS 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 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #ifndef _RSLIB_H_ |
| 16 | #define _RSLIB_H_ |
| 17 | |
| 18 | #include <linux/list.h> |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 19 | #include <linux/types.h> /* for gfp_t */ |
| 20 | #include <linux/gfp.h> /* for GFP_KERNEL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 22 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * struct rs_control - rs control structure |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 24 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * @mm: Bits per symbol |
| 26 | * @nn: Symbols per block (= (1<<mm)-1) |
| 27 | * @alpha_to: log lookup table |
| 28 | * @index_of: Antilog lookup table |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 29 | * @genpoly: Generator polynomial |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * @nroots: Number of generator roots = number of parity symbols |
| 31 | * @fcr: First consecutive root, index form |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 32 | * @prim: Primitive element, index form |
| 33 | * @iprim: prim-th root of 1, index form |
| 34 | * @gfpoly: The primitive generator polynominal |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 35 | * @gffunc: Function to generate the field, if non-canonical representation |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 36 | * @users: Users of this structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * @list: List entry for the rs control list |
| 38 | */ |
| 39 | struct rs_control { |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame] | 40 | int mm; |
| 41 | int nn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | uint16_t *alpha_to; |
| 43 | uint16_t *index_of; |
| 44 | uint16_t *genpoly; |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame] | 45 | int nroots; |
| 46 | int fcr; |
| 47 | int prim; |
| 48 | int iprim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | int gfpoly; |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 50 | int (*gffunc)(int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | int users; |
| 52 | struct list_head list; |
| 53 | }; |
| 54 | |
| 55 | /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */ |
| 56 | #ifdef CONFIG_REED_SOLOMON_ENC8 |
| 57 | int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par, |
| 58 | uint16_t invmsk); |
| 59 | #endif |
| 60 | #ifdef CONFIG_REED_SOLOMON_DEC8 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 61 | int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, |
| 63 | uint16_t *corr); |
| 64 | #endif |
| 65 | |
| 66 | /* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */ |
| 67 | #ifdef CONFIG_REED_SOLOMON_ENC16 |
| 68 | int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par, |
| 69 | uint16_t invmsk); |
| 70 | #endif |
| 71 | #ifdef CONFIG_REED_SOLOMON_DEC16 |
| 72 | int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len, |
| 73 | uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, |
| 74 | uint16_t *corr); |
| 75 | #endif |
| 76 | |
| 77 | /* Create or get a matching rs control structure */ |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 78 | struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim, |
| 79 | int nroots, gfp_t gfp); |
| 80 | |
| 81 | /** |
| 82 | * init_rs - Create a RS control struct and initialize it |
| 83 | * @symsize: the symbol size (number of bits) |
| 84 | * @gfpoly: the extended Galois field generator polynomial coefficients, |
| 85 | * with the 0th coefficient in the low order bit. The polynomial |
| 86 | * must be primitive; |
| 87 | * @fcr: the first consecutive root of the rs code generator polynomial |
| 88 | * in index form |
| 89 | * @prim: primitive element to generate polynomial roots |
| 90 | * @nroots: RS code generator polynomial degree (number of roots) |
| 91 | * |
| 92 | * Allocations use GFP_KERNEL. |
| 93 | */ |
| 94 | static inline struct rs_control *init_rs(int symsize, int gfpoly, int fcr, |
| 95 | int prim, int nroots) |
| 96 | { |
| 97 | return init_rs_gfp(symsize, gfpoly, fcr, prim, nroots, GFP_KERNEL); |
| 98 | } |
| 99 | |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 100 | struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int), |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 101 | int fcr, int prim, int nroots); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | /* Release a rs control structure */ |
| 104 | void free_rs(struct rs_control *rs); |
| 105 | |
| 106 | /** modulo replacement for galois field arithmetics |
| 107 | * |
| 108 | * @rs: the rs control structure |
| 109 | * @x: the value to reduce |
| 110 | * |
| 111 | * where |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 112 | * rs->mm = number of bits per symbol |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * rs->nn = (2^rs->mm) - 1 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 114 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * Simple arithmetic modulo would return a wrong result for values |
| 116 | * >= 3 * rs->nn |
| 117 | */ |
| 118 | static inline int rs_modnn(struct rs_control *rs, int x) |
| 119 | { |
| 120 | while (x >= rs->nn) { |
| 121 | x -= rs->nn; |
| 122 | x = (x >> rs->mm) + (x & rs->nn); |
| 123 | } |
| 124 | return x; |
| 125 | } |
| 126 | |
| 127 | #endif |