blob: 5974cedd008c345a3261229e30ca7e290d388d76 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#ifndef _RSLIB_H_
11#define _RSLIB_H_
12
13#include <linux/list.h>
Thomas Gleixner83a530e2018-04-22 18:23:46 +020014#include <linux/types.h> /* for gfp_t */
15#include <linux/gfp.h> /* for GFP_KERNEL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Thomas Gleixner03ead842005-11-07 11:15:37 +000017/**
Thomas Gleixner21633982018-04-22 18:23:53 +020018 * struct rs_codec - rs codec data
Thomas Gleixner03ead842005-11-07 11:15:37 +000019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * @mm: Bits per symbol
21 * @nn: Symbols per block (= (1<<mm)-1)
22 * @alpha_to: log lookup table
23 * @index_of: Antilog lookup table
Thomas Gleixner03ead842005-11-07 11:15:37 +000024 * @genpoly: Generator polynomial
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * @nroots: Number of generator roots = number of parity symbols
26 * @fcr: First consecutive root, index form
Thomas Gleixner03ead842005-11-07 11:15:37 +000027 * @prim: Primitive element, index form
28 * @iprim: prim-th root of 1, index form
29 * @gfpoly: The primitive generator polynominal
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020030 * @gffunc: Function to generate the field, if non-canonical representation
Thomas Gleixner03ead842005-11-07 11:15:37 +000031 * @users: Users of this structure
Thomas Gleixner21633982018-04-22 18:23:53 +020032 * @list: List entry for the rs codec list
Linus Torvalds1da177e2005-04-16 15:20:36 -070033*/
Thomas Gleixner21633982018-04-22 18:23:53 +020034struct rs_codec {
Thomas Gleixnercc4b86e42018-04-22 18:23:48 +020035 int mm;
36 int nn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 uint16_t *alpha_to;
38 uint16_t *index_of;
39 uint16_t *genpoly;
Thomas Gleixnercc4b86e42018-04-22 18:23:48 +020040 int nroots;
41 int fcr;
42 int prim;
43 int iprim;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 int gfpoly;
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020045 int (*gffunc)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 int users;
47 struct list_head list;
48};
49
Thomas Gleixner21633982018-04-22 18:23:53 +020050/**
51 * struct rs_control - rs control structure per instance
52 * @codec: The codec used for this instance
Thomas Gleixner45888b42018-04-22 18:23:55 +020053 * @buffers: Internal scratch buffers used in calls to decode_rs()
Thomas Gleixner21633982018-04-22 18:23:53 +020054 */
55struct rs_control {
56 struct rs_codec *codec;
Thomas Gleixner45888b42018-04-22 18:23:55 +020057 uint16_t buffers[0];
Thomas Gleixner21633982018-04-22 18:23:53 +020058};
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */
61#ifdef CONFIG_REED_SOLOMON_ENC8
62int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
63 uint16_t invmsk);
64#endif
65#ifdef CONFIG_REED_SOLOMON_DEC8
Thomas Gleixner03ead842005-11-07 11:15:37 +000066int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
68 uint16_t *corr);
69#endif
70
71/* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */
72#ifdef CONFIG_REED_SOLOMON_ENC16
73int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
74 uint16_t invmsk);
75#endif
76#ifdef CONFIG_REED_SOLOMON_DEC16
77int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
78 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
79 uint16_t *corr);
80#endif
81
Thomas Gleixner83a530e2018-04-22 18:23:46 +020082struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
83 int nroots, gfp_t gfp);
84
85/**
86 * init_rs - Create a RS control struct and initialize it
87 * @symsize: the symbol size (number of bits)
88 * @gfpoly: the extended Galois field generator polynomial coefficients,
89 * with the 0th coefficient in the low order bit. The polynomial
90 * must be primitive;
91 * @fcr: the first consecutive root of the rs code generator polynomial
92 * in index form
93 * @prim: primitive element to generate polynomial roots
94 * @nroots: RS code generator polynomial degree (number of roots)
95 *
96 * Allocations use GFP_KERNEL.
97 */
98static inline struct rs_control *init_rs(int symsize, int gfpoly, int fcr,
99 int prim, int nroots)
100{
101 return init_rs_gfp(symsize, gfpoly, fcr, prim, nroots, GFP_KERNEL);
102}
103
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200104struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
Thomas Gleixner83a530e2018-04-22 18:23:46 +0200105 int fcr, int prim, int nroots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* Release a rs control structure */
108void free_rs(struct rs_control *rs);
109
110/** modulo replacement for galois field arithmetics
111 *
Thomas Gleixner21633982018-04-22 18:23:53 +0200112 * @rs: Pointer to the RS codec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * @x: the value to reduce
114 *
115 * where
Thomas Gleixner03ead842005-11-07 11:15:37 +0000116 * rs->mm = number of bits per symbol
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * rs->nn = (2^rs->mm) - 1
Thomas Gleixner03ead842005-11-07 11:15:37 +0000118 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * Simple arithmetic modulo would return a wrong result for values
120 * >= 3 * rs->nn
121*/
Thomas Gleixner21633982018-04-22 18:23:53 +0200122static inline int rs_modnn(struct rs_codec *rs, int x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 while (x >= rs->nn) {
125 x -= rs->nn;
126 x = (x >> rs->mm) + (x & rs->nn);
127 }
128 return x;
129}
130
131#endif