blob: 7ace7eec302823142171a987d6e99692012f23f0 [file] [log] [blame]
Thomas Gleixnerdc8f9232018-04-22 18:23:50 +02001// SPDX-License-Identifier: GPL-2.0
Thomas Gleixner03ead842005-11-07 11:15:37 +00002/*
Thomas Gleixner3413e182018-04-22 18:23:49 +02003 * Generic Reed Solomon encoder / decoder library
Thomas Gleixner03ead842005-11-07 11:15:37 +00004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 Torvalds1da177e2005-04-16 15:20:36 -070010 * 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 Gleixner83a530e2018-04-22 18:23:46 +020019#include <linux/types.h> /* for gfp_t */
20#include <linux/gfp.h> /* for GFP_KERNEL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Thomas Gleixner03ead842005-11-07 11:15:37 +000022/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * struct rs_control - rs control structure
Thomas Gleixner03ead842005-11-07 11:15:37 +000024 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * @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 Gleixner03ead842005-11-07 11:15:37 +000029 * @genpoly: Generator polynomial
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * @nroots: Number of generator roots = number of parity symbols
31 * @fcr: First consecutive root, index form
Thomas Gleixner03ead842005-11-07 11:15:37 +000032 * @prim: Primitive element, index form
33 * @iprim: prim-th root of 1, index form
34 * @gfpoly: The primitive generator polynominal
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020035 * @gffunc: Function to generate the field, if non-canonical representation
Thomas Gleixner03ead842005-11-07 11:15:37 +000036 * @users: Users of this structure
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * @list: List entry for the rs control list
38*/
39struct rs_control {
Thomas Gleixnercc4b86e42018-04-22 18:23:48 +020040 int mm;
41 int nn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 uint16_t *alpha_to;
43 uint16_t *index_of;
44 uint16_t *genpoly;
Thomas Gleixnercc4b86e42018-04-22 18:23:48 +020045 int nroots;
46 int fcr;
47 int prim;
48 int iprim;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int gfpoly;
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020050 int (*gffunc)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 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
57int 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 Gleixner03ead842005-11-07 11:15:37 +000061int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 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
68int 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
72int 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 Gleixner83a530e2018-04-22 18:23:46 +020078struct 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 */
94static 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 Boessenkoold7e5a542007-05-02 12:18:41 +0200100struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
Thomas Gleixner83a530e2018-04-22 18:23:46 +0200101 int fcr, int prim, int nroots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* Release a rs control structure */
104void 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 Gleixner03ead842005-11-07 11:15:37 +0000112 * rs->mm = number of bits per symbol
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * rs->nn = (2^rs->mm) - 1
Thomas Gleixner03ead842005-11-07 11:15:37 +0000114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * Simple arithmetic modulo would return a wrong result for values
116 * >= 3 * rs->nn
117*/
118static 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