xref: /linux-6.15/include/linux/rslib.h (revision dc8f923e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic Reed Solomon encoder / decoder library
4  *
5  * Copyright (C) 2004 Thomas Gleixner ([email protected])
6  *
7  * RS code lifted from reed solomon library written by Phil Karn
8  * Copyright 2002 Phil Karn, KA9Q
9  *
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>
19 #include <linux/types.h>	/* for gfp_t */
20 #include <linux/gfp.h>		/* for GFP_KERNEL */
21 
22 /**
23  * struct rs_control - rs control structure
24  *
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
29  * @genpoly:	Generator polynomial
30  * @nroots:	Number of generator roots = number of parity symbols
31  * @fcr:	First consecutive root, index form
32  * @prim:	Primitive element, index form
33  * @iprim:	prim-th root of 1, index form
34  * @gfpoly:	The primitive generator polynominal
35  * @gffunc:	Function to generate the field, if non-canonical representation
36  * @users:	Users of this structure
37  * @list:	List entry for the rs control list
38 */
39 struct rs_control {
40 	int		mm;
41 	int		nn;
42 	uint16_t	*alpha_to;
43 	uint16_t	*index_of;
44 	uint16_t	*genpoly;
45 	int		nroots;
46 	int		fcr;
47 	int		prim;
48 	int		iprim;
49 	int		gfpoly;
50 	int		(*gffunc)(int);
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
61 int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
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 */
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 
100 struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
101 					 int fcr, int prim, int nroots);
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
112  *  rs->mm = number of bits per symbol
113  *  rs->nn = (2^rs->mm) - 1
114  *
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
128