Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
return -EINVAL;
encode_bch(bch, data, len, NULL);
} else {
/* load provided calculated ecc */
load_ecc8(bch, bch->ecc_buf, calc_ecc);
}
/* load received ecc or assume it was XORed in calc_ecc */
if (recv_ecc) {
load_ecc8(bch, bch->ecc_buf2, recv_ecc);
/* XOR received and calculated ecc */
for (i = 0, sum = 0; i < (int)ecc_words; i++) {
bch->ecc_buf[i] ^= bch->ecc_buf2[i];
sum |= bch->ecc_buf[i];
}
if (!sum)
/* no error found */
return 0;
}
compute_syndromes(bch, bch->ecc_buf, bch->syn);
syn = bch->syn;
}
err = compute_error_locator_polynomial(bch, syn);
if (err > 0) {
nroots = find_poly_roots(bch, 1, bch->elp, errloc);
if (err != nroots)
err = -1;
}
if (err > 0) {
/* post-process raw error locations for easier correction */
nbits = (len*8)+bch->ecc_bits;
for (i = 0; i < err; i++) {
if (errloc[i] >= nbits) {
err = -1;
break;
}
errloc[i] = nbits-1-errloc[i];
errloc[i] = (errloc[i] & ~7)|(7-(errloc[i] & 7));
}
}
return (err >= 0) ? err : -EBADMSG;
}
/*
* generate Galois field lookup tables
*/
static int build_gf_tables(struct bch_control *bch, unsigned int poly)
{
unsigned int i, x = 1;
const unsigned int k = 1 << deg(poly);
/* primitive polynomial must be of degree m */
if (k != (1u << GF_M(bch)))
return -1;
for (i = 0; i < GF_N(bch); i++) {
bch->a_pow_tab[i] = x;
bch->a_log_tab[x] = i;
if (i && (x == 1))
/* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
return -1;
x <<= 1;
if (x & k)
x ^= poly;
}
bch->a_pow_tab[GF_N(bch)] = 1;
bch->a_log_tab[0] = 0;
return 0;
}
/*
* compute generator polynomial remainder tables for fast encoding
*/
static void build_mod8_tables(struct bch_control *bch, const uint32_t *g)
{
int i, j, b, d;
uint32_t data, hi, lo, *tab;
const int l = BCH_ECC_WORDS(bch);
const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32);
const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32);
memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab));
for (i = 0; i < 256; i++) {
/* p(X)=i is a small polynomial of weight <= 8 */
for (b = 0; b < 4; b++) {
/* we want to compute (p(X).X^(8*b+deg(g))) mod g(X) */
tab = bch->mod8_tab + (b*256+i)*l;
data = i << (8*b);
while (data) {
d = deg(data);
/* subtract X^d.g(X) from p(X).X^(8*b+deg(g)) */
data ^= g[0] >> (31-d);
for (j = 0; j < ecclen; j++) {
hi = (d < 31) ? g[j] << (d+1) : 0;
lo = (j+1 < plen) ?
g[j+1] >> (31-d) : 0;
tab[j] ^= hi|lo;
}
}
}
}
}
/*
* build a base for factoring degree 2 polynomials
*/
static int build_deg2_base(struct bch_control *bch)
{
const int m = GF_M(bch);
int i, j, r;
unsigned int sum, x, y, remaining, ak = 0, xi[m];
/* find k s.t. Tr(a^k) = 1 and 0 <= k < m */
for (i = 0; i < m; i++) {
for (j = 0, sum = 0; j < m; j++)
sum ^= a_pow(bch, i*(1 << j));
if (sum) {
ak = bch->a_pow_tab[i];
break;
}
}
/* find xi, i=0..m-1 such that xi^2+xi = a^i+Tr(a^i).a^k */
remaining = m;
memset(xi, 0, sizeof(xi));
for (x = 0; (x <= GF_N(bch)) && remaining; x++) {
y = gf_sqr(bch, x)^x;
for (i = 0; i < 2; i++) {
r = a_log(bch, y);
if (y && (r < m) && !xi[r]) {
bch->xi_tab[r] = x;
xi[r] = 1;
remaining--;
dbg("x%d = %x\n", r, x);
break;
}
y ^= ak;
}
}
/* should not happen but check anyway */
return remaining ? -1 : 0;
}
static void *bch_alloc(size_t size, int *err)
{
void *ptr;
ptr = kmalloc(size, GFP_KERNEL);
if (ptr == NULL)
*err = 1;
return ptr;
}
/*
* compute generator polynomial for given (m,t) parameters.
*/
static uint32_t *compute_generator_polynomial(struct bch_control *bch)
{
const unsigned int m = GF_M(bch);
const unsigned int t = GF_T(bch);
int n, err = 0;
unsigned int i, j, nbits, r, word, *roots;
struct gf_poly *g;
uint32_t *genpoly;
g = bch_alloc(GF_POLY_SZ(m*t), &err);
roots = bch_alloc((bch->n+1)*sizeof(*roots), &err);
genpoly = bch_alloc(DIV_ROUND_UP(m*t+1, 32)*sizeof(*genpoly), &err);
if (err) {
kfree(genpoly);
genpoly = NULL;
goto finish;
}
/* enumerate all roots of g(X) */
memset(roots , 0, (bch->n+1)*sizeof(*roots));
for (i = 0; i < t; i++) {
for (j = 0, r = 2*i+1; j < m; j++) {
roots[r] = 1;
r = mod_s(bch, 2*r);
}
}
/* build generator polynomial g(X) */
g->deg = 0;
g->c[0] = 1;
for (i = 0; i < GF_N(bch); i++) {
if (roots[i]) {
/* multiply g(X) by (X+root) */
r = bch->a_pow_tab[i];
g->c[g->deg+1] = 1;
for (j = g->deg; j > 0; j--)
g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1];
g->c[0] = gf_mul(bch, g->c[0], r);
g->deg++;
}
}
/* store left-justified binary representation of g(X) */
n = g->deg+1;
i = 0;
while (n > 0) {
nbits = (n > 32) ? 32 : n;
for (j = 0, word = 0; j < nbits; j++) {
if (g->c[n-1-j])
word |= 1u << (31-j);
}
genpoly[i++] = word;
n -= nbits;
}
bch->ecc_bits = g->deg;
finish:
kfree(g);
kfree(roots);
return genpoly;
}
/**
* init_bch - initialize a BCH encoder/decoder
* @m: Galois field order, should be in the range 5-15
* @t: maximum error correction capability, in bits
* @prim_poly: user-provided primitive polynomial (or 0 to use default)
*
* Returns:
* a newly allocated BCH control structure if successful, NULL otherwise
*
* This initialization can take some time, as lookup tables are built for fast
* encoding/decoding; make sure not to call this function from a time critical
* path. Usually, init_bch() should be called on module/driver init and
* free_bch() should be called to release memory on exit.
*
* You may provide your own primitive polynomial of degree @m in argument
* @prim_poly, or let init_bch() use its default polynomial.
*
* Once init_bch() has successfully returned a pointer to a newly allocated
* BCH control structure, ecc length in bytes is given by member @ecc_bytes of
* the structure.
*/
struct bch_control *init_bch(int m, int t, unsigned int prim_poly)
{
int err = 0;
unsigned int i, words;
uint32_t *genpoly;
struct bch_control *bch = NULL;
const int min_m = 5;
const int max_m = 15;
/* default primitive polynomials */
static const unsigned int prim_poly_tab[] = {
0x25, 0x43, 0x83, 0x11d, 0x211, 0x409, 0x805, 0x1053, 0x201b,
0x402b, 0x8003,
};
#if defined(CONFIG_BCH_CONST_PARAMS)
if ((m != (CONFIG_BCH_CONST_M)) || (t != (CONFIG_BCH_CONST_T))) {
printk(KERN_ERR "bch encoder/decoder was configured to support "
"parameters m=%d, t=%d only!\n",
CONFIG_BCH_CONST_M, CONFIG_BCH_CONST_T);
goto fail;
}
#endif
if ((m < min_m) || (m > max_m))
/*
* values of m greater than 15 are not currently supported;
* supporting m > 15 would require changing table base type
* (uint16_t) and a small patch in matrix transposition
*/
goto fail;
/* sanity checks */
if ((t < 1) || (m*t >= ((1 << m)-1)))
/* invalid t value */
goto fail;
/* select a primitive polynomial for generating GF(2^m) */
if (prim_poly == 0)
prim_poly = prim_poly_tab[m-min_m];
bch = kzalloc(sizeof(*bch), GFP_KERNEL);
if (bch == NULL)
goto fail;
bch->m = m;
bch->t = t;
bch->n = (1 << m)-1;
words = DIV_ROUND_UP(m*t, 32);
bch->ecc_bytes = DIV_ROUND_UP(m*t, 8);
bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err);
bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err);
bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err);
bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err);
bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err);
bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err);
bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err);
bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err);
bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err);
for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++)
bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err);
if (err)
goto fail;
err = build_gf_tables(bch, prim_poly);
if (err)
goto fail;
/* use generator polynomial for computing encoding tables */
genpoly = compute_generator_polynomial(bch);
if (genpoly == NULL)
goto fail;
build_mod8_tables(bch, genpoly);
kfree(genpoly);
err = build_deg2_base(bch);
if (err)
goto fail;
return bch;
fail:
free_bch(bch);
return NULL;
}
/**
* free_bch - free the BCH control structure
* @bch: BCH control structure to release
*/
void free_bch(struct bch_control *bch)
{
unsigned int i;
if (bch) {
kfree(bch->a_pow_tab);
kfree(bch->a_log_tab);
kfree(bch->mod8_tab);
kfree(bch->ecc_buf);
kfree(bch->ecc_buf2);
kfree(bch->xi_tab);
kfree(bch->syn);
kfree(bch->cache);
kfree(bch->elp);
for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++)
kfree(bch->poly_2t[i]);
kfree(bch);
}
}