18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Generic binary BCH encoding/decoding library 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it 58c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License version 2 as published by 68c2ecf20Sopenharmony_ci * the Free Software Foundation. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but WITHOUT 98c2ecf20Sopenharmony_ci * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 108c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 118c2ecf20Sopenharmony_ci * more details. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License along with 148c2ecf20Sopenharmony_ci * this program; if not, write to the Free Software Foundation, Inc., 51 158c2ecf20Sopenharmony_ci * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * Copyright © 2011 Parrot S.A. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * Author: Ivan Djelic <ivan.djelic@parrot.com> 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * Description: 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * This library provides runtime configurable encoding/decoding of binary 248c2ecf20Sopenharmony_ci * Bose-Chaudhuri-Hocquenghem (BCH) codes. 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * Call bch_init to get a pointer to a newly allocated bch_control structure for 278c2ecf20Sopenharmony_ci * the given m (Galois field order), t (error correction capability) and 288c2ecf20Sopenharmony_ci * (optional) primitive polynomial parameters. 298c2ecf20Sopenharmony_ci * 308c2ecf20Sopenharmony_ci * Call bch_encode to compute and store ecc parity bytes to a given buffer. 318c2ecf20Sopenharmony_ci * Call bch_decode to detect and locate errors in received data. 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * On systems supporting hw BCH features, intermediate results may be provided 348c2ecf20Sopenharmony_ci * to bch_decode in order to skip certain steps. See bch_decode() documentation 358c2ecf20Sopenharmony_ci * for details. 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * Option CONFIG_BCH_CONST_PARAMS can be used to force fixed values of 388c2ecf20Sopenharmony_ci * parameters m and t; thus allowing extra compiler optimizations and providing 398c2ecf20Sopenharmony_ci * better (up to 2x) encoding performance. Using this option makes sense when 408c2ecf20Sopenharmony_ci * (m,t) are fixed and known in advance, e.g. when using BCH error correction 418c2ecf20Sopenharmony_ci * on a particular NAND flash device. 428c2ecf20Sopenharmony_ci * 438c2ecf20Sopenharmony_ci * Algorithmic details: 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * Encoding is performed by processing 32 input bits in parallel, using 4 468c2ecf20Sopenharmony_ci * remainder lookup tables. 478c2ecf20Sopenharmony_ci * 488c2ecf20Sopenharmony_ci * The final stage of decoding involves the following internal steps: 498c2ecf20Sopenharmony_ci * a. Syndrome computation 508c2ecf20Sopenharmony_ci * b. Error locator polynomial computation using Berlekamp-Massey algorithm 518c2ecf20Sopenharmony_ci * c. Error locator root finding (by far the most expensive step) 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * In this implementation, step c is not performed using the usual Chien search. 548c2ecf20Sopenharmony_ci * Instead, an alternative approach described in [1] is used. It consists in 558c2ecf20Sopenharmony_ci * factoring the error locator polynomial using the Berlekamp Trace algorithm 568c2ecf20Sopenharmony_ci * (BTA) down to a certain degree (4), after which ad hoc low-degree polynomial 578c2ecf20Sopenharmony_ci * solving techniques [2] are used. The resulting algorithm, called BTZ, yields 588c2ecf20Sopenharmony_ci * much better performance than Chien search for usual (m,t) values (typically 598c2ecf20Sopenharmony_ci * m >= 13, t < 32, see [1]). 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * [1] B. Biswas, V. Herbert. Efficient root finding of polynomials over fields 628c2ecf20Sopenharmony_ci * of characteristic 2, in: Western European Workshop on Research in Cryptology 638c2ecf20Sopenharmony_ci * - WEWoRC 2009, Graz, Austria, LNCS, Springer, July 2009, to appear. 648c2ecf20Sopenharmony_ci * [2] [Zin96] V.A. Zinoviev. On the solution of equations of degree 10 over 658c2ecf20Sopenharmony_ci * finite fields GF(2^q). In Rapport de recherche INRIA no 2829, 1996. 668c2ecf20Sopenharmony_ci */ 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci#include <linux/kernel.h> 698c2ecf20Sopenharmony_ci#include <linux/errno.h> 708c2ecf20Sopenharmony_ci#include <linux/init.h> 718c2ecf20Sopenharmony_ci#include <linux/module.h> 728c2ecf20Sopenharmony_ci#include <linux/slab.h> 738c2ecf20Sopenharmony_ci#include <linux/bitops.h> 748c2ecf20Sopenharmony_ci#include <asm/byteorder.h> 758c2ecf20Sopenharmony_ci#include <linux/bch.h> 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci#if defined(CONFIG_BCH_CONST_PARAMS) 788c2ecf20Sopenharmony_ci#define GF_M(_p) (CONFIG_BCH_CONST_M) 798c2ecf20Sopenharmony_ci#define GF_T(_p) (CONFIG_BCH_CONST_T) 808c2ecf20Sopenharmony_ci#define GF_N(_p) ((1 << (CONFIG_BCH_CONST_M))-1) 818c2ecf20Sopenharmony_ci#define BCH_MAX_M (CONFIG_BCH_CONST_M) 828c2ecf20Sopenharmony_ci#define BCH_MAX_T (CONFIG_BCH_CONST_T) 838c2ecf20Sopenharmony_ci#else 848c2ecf20Sopenharmony_ci#define GF_M(_p) ((_p)->m) 858c2ecf20Sopenharmony_ci#define GF_T(_p) ((_p)->t) 868c2ecf20Sopenharmony_ci#define GF_N(_p) ((_p)->n) 878c2ecf20Sopenharmony_ci#define BCH_MAX_M 15 /* 2KB */ 888c2ecf20Sopenharmony_ci#define BCH_MAX_T 64 /* 64 bit correction */ 898c2ecf20Sopenharmony_ci#endif 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci#define BCH_ECC_WORDS(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 32) 928c2ecf20Sopenharmony_ci#define BCH_ECC_BYTES(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 8) 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci#define BCH_ECC_MAX_WORDS DIV_ROUND_UP(BCH_MAX_M * BCH_MAX_T, 32) 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci#ifndef dbg 978c2ecf20Sopenharmony_ci#define dbg(_fmt, args...) do {} while (0) 988c2ecf20Sopenharmony_ci#endif 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* 1018c2ecf20Sopenharmony_ci * represent a polynomial over GF(2^m) 1028c2ecf20Sopenharmony_ci */ 1038c2ecf20Sopenharmony_cistruct gf_poly { 1048c2ecf20Sopenharmony_ci unsigned int deg; /* polynomial degree */ 1058c2ecf20Sopenharmony_ci unsigned int c[]; /* polynomial terms */ 1068c2ecf20Sopenharmony_ci}; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci/* given its degree, compute a polynomial size in bytes */ 1098c2ecf20Sopenharmony_ci#define GF_POLY_SZ(_d) (sizeof(struct gf_poly)+((_d)+1)*sizeof(unsigned int)) 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci/* polynomial of degree 1 */ 1128c2ecf20Sopenharmony_cistruct gf_poly_deg1 { 1138c2ecf20Sopenharmony_ci struct gf_poly poly; 1148c2ecf20Sopenharmony_ci unsigned int c[2]; 1158c2ecf20Sopenharmony_ci}; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic u8 swap_bits_table[] = { 1188c2ecf20Sopenharmony_ci 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 1198c2ecf20Sopenharmony_ci 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 1208c2ecf20Sopenharmony_ci 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 1218c2ecf20Sopenharmony_ci 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 1228c2ecf20Sopenharmony_ci 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 1238c2ecf20Sopenharmony_ci 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 1248c2ecf20Sopenharmony_ci 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 1258c2ecf20Sopenharmony_ci 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 1268c2ecf20Sopenharmony_ci 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 1278c2ecf20Sopenharmony_ci 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 1288c2ecf20Sopenharmony_ci 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 1298c2ecf20Sopenharmony_ci 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 1308c2ecf20Sopenharmony_ci 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 1318c2ecf20Sopenharmony_ci 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, 1328c2ecf20Sopenharmony_ci 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 1338c2ecf20Sopenharmony_ci 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 1348c2ecf20Sopenharmony_ci 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 1358c2ecf20Sopenharmony_ci 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 1368c2ecf20Sopenharmony_ci 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 1378c2ecf20Sopenharmony_ci 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 1388c2ecf20Sopenharmony_ci 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 1398c2ecf20Sopenharmony_ci 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 1408c2ecf20Sopenharmony_ci 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 1418c2ecf20Sopenharmony_ci 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 1428c2ecf20Sopenharmony_ci 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 1438c2ecf20Sopenharmony_ci 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 1448c2ecf20Sopenharmony_ci 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 1458c2ecf20Sopenharmony_ci 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 1468c2ecf20Sopenharmony_ci 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 1478c2ecf20Sopenharmony_ci 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 1488c2ecf20Sopenharmony_ci 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 1498c2ecf20Sopenharmony_ci 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff, 1508c2ecf20Sopenharmony_ci}; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic u8 swap_bits(struct bch_control *bch, u8 in) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci if (!bch->swap_bits) 1558c2ecf20Sopenharmony_ci return in; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci return swap_bits_table[in]; 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci/* 1618c2ecf20Sopenharmony_ci * same as bch_encode(), but process input data one byte at a time 1628c2ecf20Sopenharmony_ci */ 1638c2ecf20Sopenharmony_cistatic void bch_encode_unaligned(struct bch_control *bch, 1648c2ecf20Sopenharmony_ci const unsigned char *data, unsigned int len, 1658c2ecf20Sopenharmony_ci uint32_t *ecc) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci int i; 1688c2ecf20Sopenharmony_ci const uint32_t *p; 1698c2ecf20Sopenharmony_ci const int l = BCH_ECC_WORDS(bch)-1; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci while (len--) { 1728c2ecf20Sopenharmony_ci u8 tmp = swap_bits(bch, *data++); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci p = bch->mod8_tab + (l+1)*(((ecc[0] >> 24)^(tmp)) & 0xff); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci for (i = 0; i < l; i++) 1778c2ecf20Sopenharmony_ci ecc[i] = ((ecc[i] << 8)|(ecc[i+1] >> 24))^(*p++); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci ecc[l] = (ecc[l] << 8)^(*p); 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/* 1848c2ecf20Sopenharmony_ci * convert ecc bytes to aligned, zero-padded 32-bit ecc words 1858c2ecf20Sopenharmony_ci */ 1868c2ecf20Sopenharmony_cistatic void load_ecc8(struct bch_control *bch, uint32_t *dst, 1878c2ecf20Sopenharmony_ci const uint8_t *src) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci uint8_t pad[4] = {0, 0, 0, 0}; 1908c2ecf20Sopenharmony_ci unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci for (i = 0; i < nwords; i++, src += 4) 1938c2ecf20Sopenharmony_ci dst[i] = ((u32)swap_bits(bch, src[0]) << 24) | 1948c2ecf20Sopenharmony_ci ((u32)swap_bits(bch, src[1]) << 16) | 1958c2ecf20Sopenharmony_ci ((u32)swap_bits(bch, src[2]) << 8) | 1968c2ecf20Sopenharmony_ci swap_bits(bch, src[3]); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci memcpy(pad, src, BCH_ECC_BYTES(bch)-4*nwords); 1998c2ecf20Sopenharmony_ci dst[nwords] = ((u32)swap_bits(bch, pad[0]) << 24) | 2008c2ecf20Sopenharmony_ci ((u32)swap_bits(bch, pad[1]) << 16) | 2018c2ecf20Sopenharmony_ci ((u32)swap_bits(bch, pad[2]) << 8) | 2028c2ecf20Sopenharmony_ci swap_bits(bch, pad[3]); 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci/* 2068c2ecf20Sopenharmony_ci * convert 32-bit ecc words to ecc bytes 2078c2ecf20Sopenharmony_ci */ 2088c2ecf20Sopenharmony_cistatic void store_ecc8(struct bch_control *bch, uint8_t *dst, 2098c2ecf20Sopenharmony_ci const uint32_t *src) 2108c2ecf20Sopenharmony_ci{ 2118c2ecf20Sopenharmony_ci uint8_t pad[4]; 2128c2ecf20Sopenharmony_ci unsigned int i, nwords = BCH_ECC_WORDS(bch)-1; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci for (i = 0; i < nwords; i++) { 2158c2ecf20Sopenharmony_ci *dst++ = swap_bits(bch, src[i] >> 24); 2168c2ecf20Sopenharmony_ci *dst++ = swap_bits(bch, src[i] >> 16); 2178c2ecf20Sopenharmony_ci *dst++ = swap_bits(bch, src[i] >> 8); 2188c2ecf20Sopenharmony_ci *dst++ = swap_bits(bch, src[i]); 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci pad[0] = swap_bits(bch, src[nwords] >> 24); 2218c2ecf20Sopenharmony_ci pad[1] = swap_bits(bch, src[nwords] >> 16); 2228c2ecf20Sopenharmony_ci pad[2] = swap_bits(bch, src[nwords] >> 8); 2238c2ecf20Sopenharmony_ci pad[3] = swap_bits(bch, src[nwords]); 2248c2ecf20Sopenharmony_ci memcpy(dst, pad, BCH_ECC_BYTES(bch)-4*nwords); 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci/** 2288c2ecf20Sopenharmony_ci * bch_encode - calculate BCH ecc parity of data 2298c2ecf20Sopenharmony_ci * @bch: BCH control structure 2308c2ecf20Sopenharmony_ci * @data: data to encode 2318c2ecf20Sopenharmony_ci * @len: data length in bytes 2328c2ecf20Sopenharmony_ci * @ecc: ecc parity data, must be initialized by caller 2338c2ecf20Sopenharmony_ci * 2348c2ecf20Sopenharmony_ci * The @ecc parity array is used both as input and output parameter, in order to 2358c2ecf20Sopenharmony_ci * allow incremental computations. It should be of the size indicated by member 2368c2ecf20Sopenharmony_ci * @ecc_bytes of @bch, and should be initialized to 0 before the first call. 2378c2ecf20Sopenharmony_ci * 2388c2ecf20Sopenharmony_ci * The exact number of computed ecc parity bits is given by member @ecc_bits of 2398c2ecf20Sopenharmony_ci * @bch; it may be less than m*t for large values of t. 2408c2ecf20Sopenharmony_ci */ 2418c2ecf20Sopenharmony_civoid bch_encode(struct bch_control *bch, const uint8_t *data, 2428c2ecf20Sopenharmony_ci unsigned int len, uint8_t *ecc) 2438c2ecf20Sopenharmony_ci{ 2448c2ecf20Sopenharmony_ci const unsigned int l = BCH_ECC_WORDS(bch)-1; 2458c2ecf20Sopenharmony_ci unsigned int i, mlen; 2468c2ecf20Sopenharmony_ci unsigned long m; 2478c2ecf20Sopenharmony_ci uint32_t w, r[BCH_ECC_MAX_WORDS]; 2488c2ecf20Sopenharmony_ci const size_t r_bytes = BCH_ECC_WORDS(bch) * sizeof(*r); 2498c2ecf20Sopenharmony_ci const uint32_t * const tab0 = bch->mod8_tab; 2508c2ecf20Sopenharmony_ci const uint32_t * const tab1 = tab0 + 256*(l+1); 2518c2ecf20Sopenharmony_ci const uint32_t * const tab2 = tab1 + 256*(l+1); 2528c2ecf20Sopenharmony_ci const uint32_t * const tab3 = tab2 + 256*(l+1); 2538c2ecf20Sopenharmony_ci const uint32_t *pdata, *p0, *p1, *p2, *p3; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci if (WARN_ON(r_bytes > sizeof(r))) 2568c2ecf20Sopenharmony_ci return; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci if (ecc) { 2598c2ecf20Sopenharmony_ci /* load ecc parity bytes into internal 32-bit buffer */ 2608c2ecf20Sopenharmony_ci load_ecc8(bch, bch->ecc_buf, ecc); 2618c2ecf20Sopenharmony_ci } else { 2628c2ecf20Sopenharmony_ci memset(bch->ecc_buf, 0, r_bytes); 2638c2ecf20Sopenharmony_ci } 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci /* process first unaligned data bytes */ 2668c2ecf20Sopenharmony_ci m = ((unsigned long)data) & 3; 2678c2ecf20Sopenharmony_ci if (m) { 2688c2ecf20Sopenharmony_ci mlen = (len < (4-m)) ? len : 4-m; 2698c2ecf20Sopenharmony_ci bch_encode_unaligned(bch, data, mlen, bch->ecc_buf); 2708c2ecf20Sopenharmony_ci data += mlen; 2718c2ecf20Sopenharmony_ci len -= mlen; 2728c2ecf20Sopenharmony_ci } 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci /* process 32-bit aligned data words */ 2758c2ecf20Sopenharmony_ci pdata = (uint32_t *)data; 2768c2ecf20Sopenharmony_ci mlen = len/4; 2778c2ecf20Sopenharmony_ci data += 4*mlen; 2788c2ecf20Sopenharmony_ci len -= 4*mlen; 2798c2ecf20Sopenharmony_ci memcpy(r, bch->ecc_buf, r_bytes); 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci /* 2828c2ecf20Sopenharmony_ci * split each 32-bit word into 4 polynomials of weight 8 as follows: 2838c2ecf20Sopenharmony_ci * 2848c2ecf20Sopenharmony_ci * 31 ...24 23 ...16 15 ... 8 7 ... 0 2858c2ecf20Sopenharmony_ci * xxxxxxxx yyyyyyyy zzzzzzzz tttttttt 2868c2ecf20Sopenharmony_ci * tttttttt mod g = r0 (precomputed) 2878c2ecf20Sopenharmony_ci * zzzzzzzz 00000000 mod g = r1 (precomputed) 2888c2ecf20Sopenharmony_ci * yyyyyyyy 00000000 00000000 mod g = r2 (precomputed) 2898c2ecf20Sopenharmony_ci * xxxxxxxx 00000000 00000000 00000000 mod g = r3 (precomputed) 2908c2ecf20Sopenharmony_ci * xxxxxxxx yyyyyyyy zzzzzzzz tttttttt mod g = r0^r1^r2^r3 2918c2ecf20Sopenharmony_ci */ 2928c2ecf20Sopenharmony_ci while (mlen--) { 2938c2ecf20Sopenharmony_ci /* input data is read in big-endian format */ 2948c2ecf20Sopenharmony_ci w = cpu_to_be32(*pdata++); 2958c2ecf20Sopenharmony_ci if (bch->swap_bits) 2968c2ecf20Sopenharmony_ci w = (u32)swap_bits(bch, w) | 2978c2ecf20Sopenharmony_ci ((u32)swap_bits(bch, w >> 8) << 8) | 2988c2ecf20Sopenharmony_ci ((u32)swap_bits(bch, w >> 16) << 16) | 2998c2ecf20Sopenharmony_ci ((u32)swap_bits(bch, w >> 24) << 24); 3008c2ecf20Sopenharmony_ci w ^= r[0]; 3018c2ecf20Sopenharmony_ci p0 = tab0 + (l+1)*((w >> 0) & 0xff); 3028c2ecf20Sopenharmony_ci p1 = tab1 + (l+1)*((w >> 8) & 0xff); 3038c2ecf20Sopenharmony_ci p2 = tab2 + (l+1)*((w >> 16) & 0xff); 3048c2ecf20Sopenharmony_ci p3 = tab3 + (l+1)*((w >> 24) & 0xff); 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci for (i = 0; i < l; i++) 3078c2ecf20Sopenharmony_ci r[i] = r[i+1]^p0[i]^p1[i]^p2[i]^p3[i]; 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci r[l] = p0[l]^p1[l]^p2[l]^p3[l]; 3108c2ecf20Sopenharmony_ci } 3118c2ecf20Sopenharmony_ci memcpy(bch->ecc_buf, r, r_bytes); 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci /* process last unaligned bytes */ 3148c2ecf20Sopenharmony_ci if (len) 3158c2ecf20Sopenharmony_ci bch_encode_unaligned(bch, data, len, bch->ecc_buf); 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci /* store ecc parity bytes into original parity buffer */ 3188c2ecf20Sopenharmony_ci if (ecc) 3198c2ecf20Sopenharmony_ci store_ecc8(bch, ecc, bch->ecc_buf); 3208c2ecf20Sopenharmony_ci} 3218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bch_encode); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_cistatic inline int modulo(struct bch_control *bch, unsigned int v) 3248c2ecf20Sopenharmony_ci{ 3258c2ecf20Sopenharmony_ci const unsigned int n = GF_N(bch); 3268c2ecf20Sopenharmony_ci while (v >= n) { 3278c2ecf20Sopenharmony_ci v -= n; 3288c2ecf20Sopenharmony_ci v = (v & n) + (v >> GF_M(bch)); 3298c2ecf20Sopenharmony_ci } 3308c2ecf20Sopenharmony_ci return v; 3318c2ecf20Sopenharmony_ci} 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci/* 3348c2ecf20Sopenharmony_ci * shorter and faster modulo function, only works when v < 2N. 3358c2ecf20Sopenharmony_ci */ 3368c2ecf20Sopenharmony_cistatic inline int mod_s(struct bch_control *bch, unsigned int v) 3378c2ecf20Sopenharmony_ci{ 3388c2ecf20Sopenharmony_ci const unsigned int n = GF_N(bch); 3398c2ecf20Sopenharmony_ci return (v < n) ? v : v-n; 3408c2ecf20Sopenharmony_ci} 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_cistatic inline int deg(unsigned int poly) 3438c2ecf20Sopenharmony_ci{ 3448c2ecf20Sopenharmony_ci /* polynomial degree is the most-significant bit index */ 3458c2ecf20Sopenharmony_ci return fls(poly)-1; 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_cistatic inline int parity(unsigned int x) 3498c2ecf20Sopenharmony_ci{ 3508c2ecf20Sopenharmony_ci /* 3518c2ecf20Sopenharmony_ci * public domain code snippet, lifted from 3528c2ecf20Sopenharmony_ci * http://www-graphics.stanford.edu/~seander/bithacks.html 3538c2ecf20Sopenharmony_ci */ 3548c2ecf20Sopenharmony_ci x ^= x >> 1; 3558c2ecf20Sopenharmony_ci x ^= x >> 2; 3568c2ecf20Sopenharmony_ci x = (x & 0x11111111U) * 0x11111111U; 3578c2ecf20Sopenharmony_ci return (x >> 28) & 1; 3588c2ecf20Sopenharmony_ci} 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci/* Galois field basic operations: multiply, divide, inverse, etc. */ 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_cistatic inline unsigned int gf_mul(struct bch_control *bch, unsigned int a, 3638c2ecf20Sopenharmony_ci unsigned int b) 3648c2ecf20Sopenharmony_ci{ 3658c2ecf20Sopenharmony_ci return (a && b) ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ 3668c2ecf20Sopenharmony_ci bch->a_log_tab[b])] : 0; 3678c2ecf20Sopenharmony_ci} 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_cistatic inline unsigned int gf_sqr(struct bch_control *bch, unsigned int a) 3708c2ecf20Sopenharmony_ci{ 3718c2ecf20Sopenharmony_ci return a ? bch->a_pow_tab[mod_s(bch, 2*bch->a_log_tab[a])] : 0; 3728c2ecf20Sopenharmony_ci} 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_cistatic inline unsigned int gf_div(struct bch_control *bch, unsigned int a, 3758c2ecf20Sopenharmony_ci unsigned int b) 3768c2ecf20Sopenharmony_ci{ 3778c2ecf20Sopenharmony_ci return a ? bch->a_pow_tab[mod_s(bch, bch->a_log_tab[a]+ 3788c2ecf20Sopenharmony_ci GF_N(bch)-bch->a_log_tab[b])] : 0; 3798c2ecf20Sopenharmony_ci} 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_cistatic inline unsigned int gf_inv(struct bch_control *bch, unsigned int a) 3828c2ecf20Sopenharmony_ci{ 3838c2ecf20Sopenharmony_ci return bch->a_pow_tab[GF_N(bch)-bch->a_log_tab[a]]; 3848c2ecf20Sopenharmony_ci} 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_cistatic inline unsigned int a_pow(struct bch_control *bch, int i) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci return bch->a_pow_tab[modulo(bch, i)]; 3898c2ecf20Sopenharmony_ci} 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_cistatic inline int a_log(struct bch_control *bch, unsigned int x) 3928c2ecf20Sopenharmony_ci{ 3938c2ecf20Sopenharmony_ci return bch->a_log_tab[x]; 3948c2ecf20Sopenharmony_ci} 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_cistatic inline int a_ilog(struct bch_control *bch, unsigned int x) 3978c2ecf20Sopenharmony_ci{ 3988c2ecf20Sopenharmony_ci return mod_s(bch, GF_N(bch)-bch->a_log_tab[x]); 3998c2ecf20Sopenharmony_ci} 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci/* 4028c2ecf20Sopenharmony_ci * compute 2t syndromes of ecc polynomial, i.e. ecc(a^j) for j=1..2t 4038c2ecf20Sopenharmony_ci */ 4048c2ecf20Sopenharmony_cistatic void compute_syndromes(struct bch_control *bch, uint32_t *ecc, 4058c2ecf20Sopenharmony_ci unsigned int *syn) 4068c2ecf20Sopenharmony_ci{ 4078c2ecf20Sopenharmony_ci int i, j, s; 4088c2ecf20Sopenharmony_ci unsigned int m; 4098c2ecf20Sopenharmony_ci uint32_t poly; 4108c2ecf20Sopenharmony_ci const int t = GF_T(bch); 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci s = bch->ecc_bits; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci /* make sure extra bits in last ecc word are cleared */ 4158c2ecf20Sopenharmony_ci m = ((unsigned int)s) & 31; 4168c2ecf20Sopenharmony_ci if (m) 4178c2ecf20Sopenharmony_ci ecc[s/32] &= ~((1u << (32-m))-1); 4188c2ecf20Sopenharmony_ci memset(syn, 0, 2*t*sizeof(*syn)); 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci /* compute v(a^j) for j=1 .. 2t-1 */ 4218c2ecf20Sopenharmony_ci do { 4228c2ecf20Sopenharmony_ci poly = *ecc++; 4238c2ecf20Sopenharmony_ci s -= 32; 4248c2ecf20Sopenharmony_ci while (poly) { 4258c2ecf20Sopenharmony_ci i = deg(poly); 4268c2ecf20Sopenharmony_ci for (j = 0; j < 2*t; j += 2) 4278c2ecf20Sopenharmony_ci syn[j] ^= a_pow(bch, (j+1)*(i+s)); 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci poly ^= (1 << i); 4308c2ecf20Sopenharmony_ci } 4318c2ecf20Sopenharmony_ci } while (s > 0); 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci /* v(a^(2j)) = v(a^j)^2 */ 4348c2ecf20Sopenharmony_ci for (j = 0; j < t; j++) 4358c2ecf20Sopenharmony_ci syn[2*j+1] = gf_sqr(bch, syn[j]); 4368c2ecf20Sopenharmony_ci} 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_cistatic void gf_poly_copy(struct gf_poly *dst, struct gf_poly *src) 4398c2ecf20Sopenharmony_ci{ 4408c2ecf20Sopenharmony_ci memcpy(dst, src, GF_POLY_SZ(src->deg)); 4418c2ecf20Sopenharmony_ci} 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_cistatic int compute_error_locator_polynomial(struct bch_control *bch, 4448c2ecf20Sopenharmony_ci const unsigned int *syn) 4458c2ecf20Sopenharmony_ci{ 4468c2ecf20Sopenharmony_ci const unsigned int t = GF_T(bch); 4478c2ecf20Sopenharmony_ci const unsigned int n = GF_N(bch); 4488c2ecf20Sopenharmony_ci unsigned int i, j, tmp, l, pd = 1, d = syn[0]; 4498c2ecf20Sopenharmony_ci struct gf_poly *elp = bch->elp; 4508c2ecf20Sopenharmony_ci struct gf_poly *pelp = bch->poly_2t[0]; 4518c2ecf20Sopenharmony_ci struct gf_poly *elp_copy = bch->poly_2t[1]; 4528c2ecf20Sopenharmony_ci int k, pp = -1; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci memset(pelp, 0, GF_POLY_SZ(2*t)); 4558c2ecf20Sopenharmony_ci memset(elp, 0, GF_POLY_SZ(2*t)); 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci pelp->deg = 0; 4588c2ecf20Sopenharmony_ci pelp->c[0] = 1; 4598c2ecf20Sopenharmony_ci elp->deg = 0; 4608c2ecf20Sopenharmony_ci elp->c[0] = 1; 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci /* use simplified binary Berlekamp-Massey algorithm */ 4638c2ecf20Sopenharmony_ci for (i = 0; (i < t) && (elp->deg <= t); i++) { 4648c2ecf20Sopenharmony_ci if (d) { 4658c2ecf20Sopenharmony_ci k = 2*i-pp; 4668c2ecf20Sopenharmony_ci gf_poly_copy(elp_copy, elp); 4678c2ecf20Sopenharmony_ci /* e[i+1](X) = e[i](X)+di*dp^-1*X^2(i-p)*e[p](X) */ 4688c2ecf20Sopenharmony_ci tmp = a_log(bch, d)+n-a_log(bch, pd); 4698c2ecf20Sopenharmony_ci for (j = 0; j <= pelp->deg; j++) { 4708c2ecf20Sopenharmony_ci if (pelp->c[j]) { 4718c2ecf20Sopenharmony_ci l = a_log(bch, pelp->c[j]); 4728c2ecf20Sopenharmony_ci elp->c[j+k] ^= a_pow(bch, tmp+l); 4738c2ecf20Sopenharmony_ci } 4748c2ecf20Sopenharmony_ci } 4758c2ecf20Sopenharmony_ci /* compute l[i+1] = max(l[i]->c[l[p]+2*(i-p]) */ 4768c2ecf20Sopenharmony_ci tmp = pelp->deg+k; 4778c2ecf20Sopenharmony_ci if (tmp > elp->deg) { 4788c2ecf20Sopenharmony_ci elp->deg = tmp; 4798c2ecf20Sopenharmony_ci gf_poly_copy(pelp, elp_copy); 4808c2ecf20Sopenharmony_ci pd = d; 4818c2ecf20Sopenharmony_ci pp = 2*i; 4828c2ecf20Sopenharmony_ci } 4838c2ecf20Sopenharmony_ci } 4848c2ecf20Sopenharmony_ci /* di+1 = S(2i+3)+elp[i+1].1*S(2i+2)+...+elp[i+1].lS(2i+3-l) */ 4858c2ecf20Sopenharmony_ci if (i < t-1) { 4868c2ecf20Sopenharmony_ci d = syn[2*i+2]; 4878c2ecf20Sopenharmony_ci for (j = 1; j <= elp->deg; j++) 4888c2ecf20Sopenharmony_ci d ^= gf_mul(bch, elp->c[j], syn[2*i+2-j]); 4898c2ecf20Sopenharmony_ci } 4908c2ecf20Sopenharmony_ci } 4918c2ecf20Sopenharmony_ci dbg("elp=%s\n", gf_poly_str(elp)); 4928c2ecf20Sopenharmony_ci return (elp->deg > t) ? -1 : (int)elp->deg; 4938c2ecf20Sopenharmony_ci} 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci/* 4968c2ecf20Sopenharmony_ci * solve a m x m linear system in GF(2) with an expected number of solutions, 4978c2ecf20Sopenharmony_ci * and return the number of found solutions 4988c2ecf20Sopenharmony_ci */ 4998c2ecf20Sopenharmony_cistatic int solve_linear_system(struct bch_control *bch, unsigned int *rows, 5008c2ecf20Sopenharmony_ci unsigned int *sol, int nsol) 5018c2ecf20Sopenharmony_ci{ 5028c2ecf20Sopenharmony_ci const int m = GF_M(bch); 5038c2ecf20Sopenharmony_ci unsigned int tmp, mask; 5048c2ecf20Sopenharmony_ci int rem, c, r, p, k, param[BCH_MAX_M]; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci k = 0; 5078c2ecf20Sopenharmony_ci mask = 1 << m; 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci /* Gaussian elimination */ 5108c2ecf20Sopenharmony_ci for (c = 0; c < m; c++) { 5118c2ecf20Sopenharmony_ci rem = 0; 5128c2ecf20Sopenharmony_ci p = c-k; 5138c2ecf20Sopenharmony_ci /* find suitable row for elimination */ 5148c2ecf20Sopenharmony_ci for (r = p; r < m; r++) { 5158c2ecf20Sopenharmony_ci if (rows[r] & mask) { 5168c2ecf20Sopenharmony_ci if (r != p) { 5178c2ecf20Sopenharmony_ci tmp = rows[r]; 5188c2ecf20Sopenharmony_ci rows[r] = rows[p]; 5198c2ecf20Sopenharmony_ci rows[p] = tmp; 5208c2ecf20Sopenharmony_ci } 5218c2ecf20Sopenharmony_ci rem = r+1; 5228c2ecf20Sopenharmony_ci break; 5238c2ecf20Sopenharmony_ci } 5248c2ecf20Sopenharmony_ci } 5258c2ecf20Sopenharmony_ci if (rem) { 5268c2ecf20Sopenharmony_ci /* perform elimination on remaining rows */ 5278c2ecf20Sopenharmony_ci tmp = rows[p]; 5288c2ecf20Sopenharmony_ci for (r = rem; r < m; r++) { 5298c2ecf20Sopenharmony_ci if (rows[r] & mask) 5308c2ecf20Sopenharmony_ci rows[r] ^= tmp; 5318c2ecf20Sopenharmony_ci } 5328c2ecf20Sopenharmony_ci } else { 5338c2ecf20Sopenharmony_ci /* elimination not needed, store defective row index */ 5348c2ecf20Sopenharmony_ci param[k++] = c; 5358c2ecf20Sopenharmony_ci } 5368c2ecf20Sopenharmony_ci mask >>= 1; 5378c2ecf20Sopenharmony_ci } 5388c2ecf20Sopenharmony_ci /* rewrite system, inserting fake parameter rows */ 5398c2ecf20Sopenharmony_ci if (k > 0) { 5408c2ecf20Sopenharmony_ci p = k; 5418c2ecf20Sopenharmony_ci for (r = m-1; r >= 0; r--) { 5428c2ecf20Sopenharmony_ci if ((r > m-1-k) && rows[r]) 5438c2ecf20Sopenharmony_ci /* system has no solution */ 5448c2ecf20Sopenharmony_ci return 0; 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci rows[r] = (p && (r == param[p-1])) ? 5478c2ecf20Sopenharmony_ci p--, 1u << (m-r) : rows[r-p]; 5488c2ecf20Sopenharmony_ci } 5498c2ecf20Sopenharmony_ci } 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci if (nsol != (1 << k)) 5528c2ecf20Sopenharmony_ci /* unexpected number of solutions */ 5538c2ecf20Sopenharmony_ci return 0; 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci for (p = 0; p < nsol; p++) { 5568c2ecf20Sopenharmony_ci /* set parameters for p-th solution */ 5578c2ecf20Sopenharmony_ci for (c = 0; c < k; c++) 5588c2ecf20Sopenharmony_ci rows[param[c]] = (rows[param[c]] & ~1)|((p >> c) & 1); 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci /* compute unique solution */ 5618c2ecf20Sopenharmony_ci tmp = 0; 5628c2ecf20Sopenharmony_ci for (r = m-1; r >= 0; r--) { 5638c2ecf20Sopenharmony_ci mask = rows[r] & (tmp|1); 5648c2ecf20Sopenharmony_ci tmp |= parity(mask) << (m-r); 5658c2ecf20Sopenharmony_ci } 5668c2ecf20Sopenharmony_ci sol[p] = tmp >> 1; 5678c2ecf20Sopenharmony_ci } 5688c2ecf20Sopenharmony_ci return nsol; 5698c2ecf20Sopenharmony_ci} 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci/* 5728c2ecf20Sopenharmony_ci * this function builds and solves a linear system for finding roots of a degree 5738c2ecf20Sopenharmony_ci * 4 affine monic polynomial X^4+aX^2+bX+c over GF(2^m). 5748c2ecf20Sopenharmony_ci */ 5758c2ecf20Sopenharmony_cistatic int find_affine4_roots(struct bch_control *bch, unsigned int a, 5768c2ecf20Sopenharmony_ci unsigned int b, unsigned int c, 5778c2ecf20Sopenharmony_ci unsigned int *roots) 5788c2ecf20Sopenharmony_ci{ 5798c2ecf20Sopenharmony_ci int i, j, k; 5808c2ecf20Sopenharmony_ci const int m = GF_M(bch); 5818c2ecf20Sopenharmony_ci unsigned int mask = 0xff, t, rows[16] = {0,}; 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci j = a_log(bch, b); 5848c2ecf20Sopenharmony_ci k = a_log(bch, a); 5858c2ecf20Sopenharmony_ci rows[0] = c; 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ci /* buid linear system to solve X^4+aX^2+bX+c = 0 */ 5888c2ecf20Sopenharmony_ci for (i = 0; i < m; i++) { 5898c2ecf20Sopenharmony_ci rows[i+1] = bch->a_pow_tab[4*i]^ 5908c2ecf20Sopenharmony_ci (a ? bch->a_pow_tab[mod_s(bch, k)] : 0)^ 5918c2ecf20Sopenharmony_ci (b ? bch->a_pow_tab[mod_s(bch, j)] : 0); 5928c2ecf20Sopenharmony_ci j++; 5938c2ecf20Sopenharmony_ci k += 2; 5948c2ecf20Sopenharmony_ci } 5958c2ecf20Sopenharmony_ci /* 5968c2ecf20Sopenharmony_ci * transpose 16x16 matrix before passing it to linear solver 5978c2ecf20Sopenharmony_ci * warning: this code assumes m < 16 5988c2ecf20Sopenharmony_ci */ 5998c2ecf20Sopenharmony_ci for (j = 8; j != 0; j >>= 1, mask ^= (mask << j)) { 6008c2ecf20Sopenharmony_ci for (k = 0; k < 16; k = (k+j+1) & ~j) { 6018c2ecf20Sopenharmony_ci t = ((rows[k] >> j)^rows[k+j]) & mask; 6028c2ecf20Sopenharmony_ci rows[k] ^= (t << j); 6038c2ecf20Sopenharmony_ci rows[k+j] ^= t; 6048c2ecf20Sopenharmony_ci } 6058c2ecf20Sopenharmony_ci } 6068c2ecf20Sopenharmony_ci return solve_linear_system(bch, rows, roots, 4); 6078c2ecf20Sopenharmony_ci} 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci/* 6108c2ecf20Sopenharmony_ci * compute root r of a degree 1 polynomial over GF(2^m) (returned as log(1/r)) 6118c2ecf20Sopenharmony_ci */ 6128c2ecf20Sopenharmony_cistatic int find_poly_deg1_roots(struct bch_control *bch, struct gf_poly *poly, 6138c2ecf20Sopenharmony_ci unsigned int *roots) 6148c2ecf20Sopenharmony_ci{ 6158c2ecf20Sopenharmony_ci int n = 0; 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci if (poly->c[0]) 6188c2ecf20Sopenharmony_ci /* poly[X] = bX+c with c!=0, root=c/b */ 6198c2ecf20Sopenharmony_ci roots[n++] = mod_s(bch, GF_N(bch)-bch->a_log_tab[poly->c[0]]+ 6208c2ecf20Sopenharmony_ci bch->a_log_tab[poly->c[1]]); 6218c2ecf20Sopenharmony_ci return n; 6228c2ecf20Sopenharmony_ci} 6238c2ecf20Sopenharmony_ci 6248c2ecf20Sopenharmony_ci/* 6258c2ecf20Sopenharmony_ci * compute roots of a degree 2 polynomial over GF(2^m) 6268c2ecf20Sopenharmony_ci */ 6278c2ecf20Sopenharmony_cistatic int find_poly_deg2_roots(struct bch_control *bch, struct gf_poly *poly, 6288c2ecf20Sopenharmony_ci unsigned int *roots) 6298c2ecf20Sopenharmony_ci{ 6308c2ecf20Sopenharmony_ci int n = 0, i, l0, l1, l2; 6318c2ecf20Sopenharmony_ci unsigned int u, v, r; 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_ci if (poly->c[0] && poly->c[1]) { 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci l0 = bch->a_log_tab[poly->c[0]]; 6368c2ecf20Sopenharmony_ci l1 = bch->a_log_tab[poly->c[1]]; 6378c2ecf20Sopenharmony_ci l2 = bch->a_log_tab[poly->c[2]]; 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci /* using z=a/bX, transform aX^2+bX+c into z^2+z+u (u=ac/b^2) */ 6408c2ecf20Sopenharmony_ci u = a_pow(bch, l0+l2+2*(GF_N(bch)-l1)); 6418c2ecf20Sopenharmony_ci /* 6428c2ecf20Sopenharmony_ci * let u = sum(li.a^i) i=0..m-1; then compute r = sum(li.xi): 6438c2ecf20Sopenharmony_ci * r^2+r = sum(li.(xi^2+xi)) = sum(li.(a^i+Tr(a^i).a^k)) = 6448c2ecf20Sopenharmony_ci * u + sum(li.Tr(a^i).a^k) = u+a^k.Tr(sum(li.a^i)) = u+a^k.Tr(u) 6458c2ecf20Sopenharmony_ci * i.e. r and r+1 are roots iff Tr(u)=0 6468c2ecf20Sopenharmony_ci */ 6478c2ecf20Sopenharmony_ci r = 0; 6488c2ecf20Sopenharmony_ci v = u; 6498c2ecf20Sopenharmony_ci while (v) { 6508c2ecf20Sopenharmony_ci i = deg(v); 6518c2ecf20Sopenharmony_ci r ^= bch->xi_tab[i]; 6528c2ecf20Sopenharmony_ci v ^= (1 << i); 6538c2ecf20Sopenharmony_ci } 6548c2ecf20Sopenharmony_ci /* verify root */ 6558c2ecf20Sopenharmony_ci if ((gf_sqr(bch, r)^r) == u) { 6568c2ecf20Sopenharmony_ci /* reverse z=a/bX transformation and compute log(1/r) */ 6578c2ecf20Sopenharmony_ci roots[n++] = modulo(bch, 2*GF_N(bch)-l1- 6588c2ecf20Sopenharmony_ci bch->a_log_tab[r]+l2); 6598c2ecf20Sopenharmony_ci roots[n++] = modulo(bch, 2*GF_N(bch)-l1- 6608c2ecf20Sopenharmony_ci bch->a_log_tab[r^1]+l2); 6618c2ecf20Sopenharmony_ci } 6628c2ecf20Sopenharmony_ci } 6638c2ecf20Sopenharmony_ci return n; 6648c2ecf20Sopenharmony_ci} 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci/* 6678c2ecf20Sopenharmony_ci * compute roots of a degree 3 polynomial over GF(2^m) 6688c2ecf20Sopenharmony_ci */ 6698c2ecf20Sopenharmony_cistatic int find_poly_deg3_roots(struct bch_control *bch, struct gf_poly *poly, 6708c2ecf20Sopenharmony_ci unsigned int *roots) 6718c2ecf20Sopenharmony_ci{ 6728c2ecf20Sopenharmony_ci int i, n = 0; 6738c2ecf20Sopenharmony_ci unsigned int a, b, c, a2, b2, c2, e3, tmp[4]; 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci if (poly->c[0]) { 6768c2ecf20Sopenharmony_ci /* transform polynomial into monic X^3 + a2X^2 + b2X + c2 */ 6778c2ecf20Sopenharmony_ci e3 = poly->c[3]; 6788c2ecf20Sopenharmony_ci c2 = gf_div(bch, poly->c[0], e3); 6798c2ecf20Sopenharmony_ci b2 = gf_div(bch, poly->c[1], e3); 6808c2ecf20Sopenharmony_ci a2 = gf_div(bch, poly->c[2], e3); 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci /* (X+a2)(X^3+a2X^2+b2X+c2) = X^4+aX^2+bX+c (affine) */ 6838c2ecf20Sopenharmony_ci c = gf_mul(bch, a2, c2); /* c = a2c2 */ 6848c2ecf20Sopenharmony_ci b = gf_mul(bch, a2, b2)^c2; /* b = a2b2 + c2 */ 6858c2ecf20Sopenharmony_ci a = gf_sqr(bch, a2)^b2; /* a = a2^2 + b2 */ 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci /* find the 4 roots of this affine polynomial */ 6888c2ecf20Sopenharmony_ci if (find_affine4_roots(bch, a, b, c, tmp) == 4) { 6898c2ecf20Sopenharmony_ci /* remove a2 from final list of roots */ 6908c2ecf20Sopenharmony_ci for (i = 0; i < 4; i++) { 6918c2ecf20Sopenharmony_ci if (tmp[i] != a2) 6928c2ecf20Sopenharmony_ci roots[n++] = a_ilog(bch, tmp[i]); 6938c2ecf20Sopenharmony_ci } 6948c2ecf20Sopenharmony_ci } 6958c2ecf20Sopenharmony_ci } 6968c2ecf20Sopenharmony_ci return n; 6978c2ecf20Sopenharmony_ci} 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_ci/* 7008c2ecf20Sopenharmony_ci * compute roots of a degree 4 polynomial over GF(2^m) 7018c2ecf20Sopenharmony_ci */ 7028c2ecf20Sopenharmony_cistatic int find_poly_deg4_roots(struct bch_control *bch, struct gf_poly *poly, 7038c2ecf20Sopenharmony_ci unsigned int *roots) 7048c2ecf20Sopenharmony_ci{ 7058c2ecf20Sopenharmony_ci int i, l, n = 0; 7068c2ecf20Sopenharmony_ci unsigned int a, b, c, d, e = 0, f, a2, b2, c2, e4; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci if (poly->c[0] == 0) 7098c2ecf20Sopenharmony_ci return 0; 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci /* transform polynomial into monic X^4 + aX^3 + bX^2 + cX + d */ 7128c2ecf20Sopenharmony_ci e4 = poly->c[4]; 7138c2ecf20Sopenharmony_ci d = gf_div(bch, poly->c[0], e4); 7148c2ecf20Sopenharmony_ci c = gf_div(bch, poly->c[1], e4); 7158c2ecf20Sopenharmony_ci b = gf_div(bch, poly->c[2], e4); 7168c2ecf20Sopenharmony_ci a = gf_div(bch, poly->c[3], e4); 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci /* use Y=1/X transformation to get an affine polynomial */ 7198c2ecf20Sopenharmony_ci if (a) { 7208c2ecf20Sopenharmony_ci /* first, eliminate cX by using z=X+e with ae^2+c=0 */ 7218c2ecf20Sopenharmony_ci if (c) { 7228c2ecf20Sopenharmony_ci /* compute e such that e^2 = c/a */ 7238c2ecf20Sopenharmony_ci f = gf_div(bch, c, a); 7248c2ecf20Sopenharmony_ci l = a_log(bch, f); 7258c2ecf20Sopenharmony_ci l += (l & 1) ? GF_N(bch) : 0; 7268c2ecf20Sopenharmony_ci e = a_pow(bch, l/2); 7278c2ecf20Sopenharmony_ci /* 7288c2ecf20Sopenharmony_ci * use transformation z=X+e: 7298c2ecf20Sopenharmony_ci * z^4+e^4 + a(z^3+ez^2+e^2z+e^3) + b(z^2+e^2) +cz+ce+d 7308c2ecf20Sopenharmony_ci * z^4 + az^3 + (ae+b)z^2 + (ae^2+c)z+e^4+be^2+ae^3+ce+d 7318c2ecf20Sopenharmony_ci * z^4 + az^3 + (ae+b)z^2 + e^4+be^2+d 7328c2ecf20Sopenharmony_ci * z^4 + az^3 + b'z^2 + d' 7338c2ecf20Sopenharmony_ci */ 7348c2ecf20Sopenharmony_ci d = a_pow(bch, 2*l)^gf_mul(bch, b, f)^d; 7358c2ecf20Sopenharmony_ci b = gf_mul(bch, a, e)^b; 7368c2ecf20Sopenharmony_ci } 7378c2ecf20Sopenharmony_ci /* now, use Y=1/X to get Y^4 + b/dY^2 + a/dY + 1/d */ 7388c2ecf20Sopenharmony_ci if (d == 0) 7398c2ecf20Sopenharmony_ci /* assume all roots have multiplicity 1 */ 7408c2ecf20Sopenharmony_ci return 0; 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_ci c2 = gf_inv(bch, d); 7438c2ecf20Sopenharmony_ci b2 = gf_div(bch, a, d); 7448c2ecf20Sopenharmony_ci a2 = gf_div(bch, b, d); 7458c2ecf20Sopenharmony_ci } else { 7468c2ecf20Sopenharmony_ci /* polynomial is already affine */ 7478c2ecf20Sopenharmony_ci c2 = d; 7488c2ecf20Sopenharmony_ci b2 = c; 7498c2ecf20Sopenharmony_ci a2 = b; 7508c2ecf20Sopenharmony_ci } 7518c2ecf20Sopenharmony_ci /* find the 4 roots of this affine polynomial */ 7528c2ecf20Sopenharmony_ci if (find_affine4_roots(bch, a2, b2, c2, roots) == 4) { 7538c2ecf20Sopenharmony_ci for (i = 0; i < 4; i++) { 7548c2ecf20Sopenharmony_ci /* post-process roots (reverse transformations) */ 7558c2ecf20Sopenharmony_ci f = a ? gf_inv(bch, roots[i]) : roots[i]; 7568c2ecf20Sopenharmony_ci roots[i] = a_ilog(bch, f^e); 7578c2ecf20Sopenharmony_ci } 7588c2ecf20Sopenharmony_ci n = 4; 7598c2ecf20Sopenharmony_ci } 7608c2ecf20Sopenharmony_ci return n; 7618c2ecf20Sopenharmony_ci} 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci/* 7648c2ecf20Sopenharmony_ci * build monic, log-based representation of a polynomial 7658c2ecf20Sopenharmony_ci */ 7668c2ecf20Sopenharmony_cistatic void gf_poly_logrep(struct bch_control *bch, 7678c2ecf20Sopenharmony_ci const struct gf_poly *a, int *rep) 7688c2ecf20Sopenharmony_ci{ 7698c2ecf20Sopenharmony_ci int i, d = a->deg, l = GF_N(bch)-a_log(bch, a->c[a->deg]); 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci /* represent 0 values with -1; warning, rep[d] is not set to 1 */ 7728c2ecf20Sopenharmony_ci for (i = 0; i < d; i++) 7738c2ecf20Sopenharmony_ci rep[i] = a->c[i] ? mod_s(bch, a_log(bch, a->c[i])+l) : -1; 7748c2ecf20Sopenharmony_ci} 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci/* 7778c2ecf20Sopenharmony_ci * compute polynomial Euclidean division remainder in GF(2^m)[X] 7788c2ecf20Sopenharmony_ci */ 7798c2ecf20Sopenharmony_cistatic void gf_poly_mod(struct bch_control *bch, struct gf_poly *a, 7808c2ecf20Sopenharmony_ci const struct gf_poly *b, int *rep) 7818c2ecf20Sopenharmony_ci{ 7828c2ecf20Sopenharmony_ci int la, p, m; 7838c2ecf20Sopenharmony_ci unsigned int i, j, *c = a->c; 7848c2ecf20Sopenharmony_ci const unsigned int d = b->deg; 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_ci if (a->deg < d) 7878c2ecf20Sopenharmony_ci return; 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_ci /* reuse or compute log representation of denominator */ 7908c2ecf20Sopenharmony_ci if (!rep) { 7918c2ecf20Sopenharmony_ci rep = bch->cache; 7928c2ecf20Sopenharmony_ci gf_poly_logrep(bch, b, rep); 7938c2ecf20Sopenharmony_ci } 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci for (j = a->deg; j >= d; j--) { 7968c2ecf20Sopenharmony_ci if (c[j]) { 7978c2ecf20Sopenharmony_ci la = a_log(bch, c[j]); 7988c2ecf20Sopenharmony_ci p = j-d; 7998c2ecf20Sopenharmony_ci for (i = 0; i < d; i++, p++) { 8008c2ecf20Sopenharmony_ci m = rep[i]; 8018c2ecf20Sopenharmony_ci if (m >= 0) 8028c2ecf20Sopenharmony_ci c[p] ^= bch->a_pow_tab[mod_s(bch, 8038c2ecf20Sopenharmony_ci m+la)]; 8048c2ecf20Sopenharmony_ci } 8058c2ecf20Sopenharmony_ci } 8068c2ecf20Sopenharmony_ci } 8078c2ecf20Sopenharmony_ci a->deg = d-1; 8088c2ecf20Sopenharmony_ci while (!c[a->deg] && a->deg) 8098c2ecf20Sopenharmony_ci a->deg--; 8108c2ecf20Sopenharmony_ci} 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci/* 8138c2ecf20Sopenharmony_ci * compute polynomial Euclidean division quotient in GF(2^m)[X] 8148c2ecf20Sopenharmony_ci */ 8158c2ecf20Sopenharmony_cistatic void gf_poly_div(struct bch_control *bch, struct gf_poly *a, 8168c2ecf20Sopenharmony_ci const struct gf_poly *b, struct gf_poly *q) 8178c2ecf20Sopenharmony_ci{ 8188c2ecf20Sopenharmony_ci if (a->deg >= b->deg) { 8198c2ecf20Sopenharmony_ci q->deg = a->deg-b->deg; 8208c2ecf20Sopenharmony_ci /* compute a mod b (modifies a) */ 8218c2ecf20Sopenharmony_ci gf_poly_mod(bch, a, b, NULL); 8228c2ecf20Sopenharmony_ci /* quotient is stored in upper part of polynomial a */ 8238c2ecf20Sopenharmony_ci memcpy(q->c, &a->c[b->deg], (1+q->deg)*sizeof(unsigned int)); 8248c2ecf20Sopenharmony_ci } else { 8258c2ecf20Sopenharmony_ci q->deg = 0; 8268c2ecf20Sopenharmony_ci q->c[0] = 0; 8278c2ecf20Sopenharmony_ci } 8288c2ecf20Sopenharmony_ci} 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci/* 8318c2ecf20Sopenharmony_ci * compute polynomial GCD (Greatest Common Divisor) in GF(2^m)[X] 8328c2ecf20Sopenharmony_ci */ 8338c2ecf20Sopenharmony_cistatic struct gf_poly *gf_poly_gcd(struct bch_control *bch, struct gf_poly *a, 8348c2ecf20Sopenharmony_ci struct gf_poly *b) 8358c2ecf20Sopenharmony_ci{ 8368c2ecf20Sopenharmony_ci struct gf_poly *tmp; 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci dbg("gcd(%s,%s)=", gf_poly_str(a), gf_poly_str(b)); 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci if (a->deg < b->deg) { 8418c2ecf20Sopenharmony_ci tmp = b; 8428c2ecf20Sopenharmony_ci b = a; 8438c2ecf20Sopenharmony_ci a = tmp; 8448c2ecf20Sopenharmony_ci } 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ci while (b->deg > 0) { 8478c2ecf20Sopenharmony_ci gf_poly_mod(bch, a, b, NULL); 8488c2ecf20Sopenharmony_ci tmp = b; 8498c2ecf20Sopenharmony_ci b = a; 8508c2ecf20Sopenharmony_ci a = tmp; 8518c2ecf20Sopenharmony_ci } 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci dbg("%s\n", gf_poly_str(a)); 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci return a; 8568c2ecf20Sopenharmony_ci} 8578c2ecf20Sopenharmony_ci 8588c2ecf20Sopenharmony_ci/* 8598c2ecf20Sopenharmony_ci * Given a polynomial f and an integer k, compute Tr(a^kX) mod f 8608c2ecf20Sopenharmony_ci * This is used in Berlekamp Trace algorithm for splitting polynomials 8618c2ecf20Sopenharmony_ci */ 8628c2ecf20Sopenharmony_cistatic void compute_trace_bk_mod(struct bch_control *bch, int k, 8638c2ecf20Sopenharmony_ci const struct gf_poly *f, struct gf_poly *z, 8648c2ecf20Sopenharmony_ci struct gf_poly *out) 8658c2ecf20Sopenharmony_ci{ 8668c2ecf20Sopenharmony_ci const int m = GF_M(bch); 8678c2ecf20Sopenharmony_ci int i, j; 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_ci /* z contains z^2j mod f */ 8708c2ecf20Sopenharmony_ci z->deg = 1; 8718c2ecf20Sopenharmony_ci z->c[0] = 0; 8728c2ecf20Sopenharmony_ci z->c[1] = bch->a_pow_tab[k]; 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci out->deg = 0; 8758c2ecf20Sopenharmony_ci memset(out, 0, GF_POLY_SZ(f->deg)); 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci /* compute f log representation only once */ 8788c2ecf20Sopenharmony_ci gf_poly_logrep(bch, f, bch->cache); 8798c2ecf20Sopenharmony_ci 8808c2ecf20Sopenharmony_ci for (i = 0; i < m; i++) { 8818c2ecf20Sopenharmony_ci /* add a^(k*2^i)(z^(2^i) mod f) and compute (z^(2^i) mod f)^2 */ 8828c2ecf20Sopenharmony_ci for (j = z->deg; j >= 0; j--) { 8838c2ecf20Sopenharmony_ci out->c[j] ^= z->c[j]; 8848c2ecf20Sopenharmony_ci z->c[2*j] = gf_sqr(bch, z->c[j]); 8858c2ecf20Sopenharmony_ci z->c[2*j+1] = 0; 8868c2ecf20Sopenharmony_ci } 8878c2ecf20Sopenharmony_ci if (z->deg > out->deg) 8888c2ecf20Sopenharmony_ci out->deg = z->deg; 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ci if (i < m-1) { 8918c2ecf20Sopenharmony_ci z->deg *= 2; 8928c2ecf20Sopenharmony_ci /* z^(2(i+1)) mod f = (z^(2^i) mod f)^2 mod f */ 8938c2ecf20Sopenharmony_ci gf_poly_mod(bch, z, f, bch->cache); 8948c2ecf20Sopenharmony_ci } 8958c2ecf20Sopenharmony_ci } 8968c2ecf20Sopenharmony_ci while (!out->c[out->deg] && out->deg) 8978c2ecf20Sopenharmony_ci out->deg--; 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ci dbg("Tr(a^%d.X) mod f = %s\n", k, gf_poly_str(out)); 9008c2ecf20Sopenharmony_ci} 9018c2ecf20Sopenharmony_ci 9028c2ecf20Sopenharmony_ci/* 9038c2ecf20Sopenharmony_ci * factor a polynomial using Berlekamp Trace algorithm (BTA) 9048c2ecf20Sopenharmony_ci */ 9058c2ecf20Sopenharmony_cistatic void factor_polynomial(struct bch_control *bch, int k, struct gf_poly *f, 9068c2ecf20Sopenharmony_ci struct gf_poly **g, struct gf_poly **h) 9078c2ecf20Sopenharmony_ci{ 9088c2ecf20Sopenharmony_ci struct gf_poly *f2 = bch->poly_2t[0]; 9098c2ecf20Sopenharmony_ci struct gf_poly *q = bch->poly_2t[1]; 9108c2ecf20Sopenharmony_ci struct gf_poly *tk = bch->poly_2t[2]; 9118c2ecf20Sopenharmony_ci struct gf_poly *z = bch->poly_2t[3]; 9128c2ecf20Sopenharmony_ci struct gf_poly *gcd; 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci dbg("factoring %s...\n", gf_poly_str(f)); 9158c2ecf20Sopenharmony_ci 9168c2ecf20Sopenharmony_ci *g = f; 9178c2ecf20Sopenharmony_ci *h = NULL; 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_ci /* tk = Tr(a^k.X) mod f */ 9208c2ecf20Sopenharmony_ci compute_trace_bk_mod(bch, k, f, z, tk); 9218c2ecf20Sopenharmony_ci 9228c2ecf20Sopenharmony_ci if (tk->deg > 0) { 9238c2ecf20Sopenharmony_ci /* compute g = gcd(f, tk) (destructive operation) */ 9248c2ecf20Sopenharmony_ci gf_poly_copy(f2, f); 9258c2ecf20Sopenharmony_ci gcd = gf_poly_gcd(bch, f2, tk); 9268c2ecf20Sopenharmony_ci if (gcd->deg < f->deg) { 9278c2ecf20Sopenharmony_ci /* compute h=f/gcd(f,tk); this will modify f and q */ 9288c2ecf20Sopenharmony_ci gf_poly_div(bch, f, gcd, q); 9298c2ecf20Sopenharmony_ci /* store g and h in-place (clobbering f) */ 9308c2ecf20Sopenharmony_ci *h = &((struct gf_poly_deg1 *)f)[gcd->deg].poly; 9318c2ecf20Sopenharmony_ci gf_poly_copy(*g, gcd); 9328c2ecf20Sopenharmony_ci gf_poly_copy(*h, q); 9338c2ecf20Sopenharmony_ci } 9348c2ecf20Sopenharmony_ci } 9358c2ecf20Sopenharmony_ci} 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci/* 9388c2ecf20Sopenharmony_ci * find roots of a polynomial, using BTZ algorithm; see the beginning of this 9398c2ecf20Sopenharmony_ci * file for details 9408c2ecf20Sopenharmony_ci */ 9418c2ecf20Sopenharmony_cistatic int find_poly_roots(struct bch_control *bch, unsigned int k, 9428c2ecf20Sopenharmony_ci struct gf_poly *poly, unsigned int *roots) 9438c2ecf20Sopenharmony_ci{ 9448c2ecf20Sopenharmony_ci int cnt; 9458c2ecf20Sopenharmony_ci struct gf_poly *f1, *f2; 9468c2ecf20Sopenharmony_ci 9478c2ecf20Sopenharmony_ci switch (poly->deg) { 9488c2ecf20Sopenharmony_ci /* handle low degree polynomials with ad hoc techniques */ 9498c2ecf20Sopenharmony_ci case 1: 9508c2ecf20Sopenharmony_ci cnt = find_poly_deg1_roots(bch, poly, roots); 9518c2ecf20Sopenharmony_ci break; 9528c2ecf20Sopenharmony_ci case 2: 9538c2ecf20Sopenharmony_ci cnt = find_poly_deg2_roots(bch, poly, roots); 9548c2ecf20Sopenharmony_ci break; 9558c2ecf20Sopenharmony_ci case 3: 9568c2ecf20Sopenharmony_ci cnt = find_poly_deg3_roots(bch, poly, roots); 9578c2ecf20Sopenharmony_ci break; 9588c2ecf20Sopenharmony_ci case 4: 9598c2ecf20Sopenharmony_ci cnt = find_poly_deg4_roots(bch, poly, roots); 9608c2ecf20Sopenharmony_ci break; 9618c2ecf20Sopenharmony_ci default: 9628c2ecf20Sopenharmony_ci /* factor polynomial using Berlekamp Trace Algorithm (BTA) */ 9638c2ecf20Sopenharmony_ci cnt = 0; 9648c2ecf20Sopenharmony_ci if (poly->deg && (k <= GF_M(bch))) { 9658c2ecf20Sopenharmony_ci factor_polynomial(bch, k, poly, &f1, &f2); 9668c2ecf20Sopenharmony_ci if (f1) 9678c2ecf20Sopenharmony_ci cnt += find_poly_roots(bch, k+1, f1, roots); 9688c2ecf20Sopenharmony_ci if (f2) 9698c2ecf20Sopenharmony_ci cnt += find_poly_roots(bch, k+1, f2, roots+cnt); 9708c2ecf20Sopenharmony_ci } 9718c2ecf20Sopenharmony_ci break; 9728c2ecf20Sopenharmony_ci } 9738c2ecf20Sopenharmony_ci return cnt; 9748c2ecf20Sopenharmony_ci} 9758c2ecf20Sopenharmony_ci 9768c2ecf20Sopenharmony_ci#if defined(USE_CHIEN_SEARCH) 9778c2ecf20Sopenharmony_ci/* 9788c2ecf20Sopenharmony_ci * exhaustive root search (Chien) implementation - not used, included only for 9798c2ecf20Sopenharmony_ci * reference/comparison tests 9808c2ecf20Sopenharmony_ci */ 9818c2ecf20Sopenharmony_cistatic int chien_search(struct bch_control *bch, unsigned int len, 9828c2ecf20Sopenharmony_ci struct gf_poly *p, unsigned int *roots) 9838c2ecf20Sopenharmony_ci{ 9848c2ecf20Sopenharmony_ci int m; 9858c2ecf20Sopenharmony_ci unsigned int i, j, syn, syn0, count = 0; 9868c2ecf20Sopenharmony_ci const unsigned int k = 8*len+bch->ecc_bits; 9878c2ecf20Sopenharmony_ci 9888c2ecf20Sopenharmony_ci /* use a log-based representation of polynomial */ 9898c2ecf20Sopenharmony_ci gf_poly_logrep(bch, p, bch->cache); 9908c2ecf20Sopenharmony_ci bch->cache[p->deg] = 0; 9918c2ecf20Sopenharmony_ci syn0 = gf_div(bch, p->c[0], p->c[p->deg]); 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_ci for (i = GF_N(bch)-k+1; i <= GF_N(bch); i++) { 9948c2ecf20Sopenharmony_ci /* compute elp(a^i) */ 9958c2ecf20Sopenharmony_ci for (j = 1, syn = syn0; j <= p->deg; j++) { 9968c2ecf20Sopenharmony_ci m = bch->cache[j]; 9978c2ecf20Sopenharmony_ci if (m >= 0) 9988c2ecf20Sopenharmony_ci syn ^= a_pow(bch, m+j*i); 9998c2ecf20Sopenharmony_ci } 10008c2ecf20Sopenharmony_ci if (syn == 0) { 10018c2ecf20Sopenharmony_ci roots[count++] = GF_N(bch)-i; 10028c2ecf20Sopenharmony_ci if (count == p->deg) 10038c2ecf20Sopenharmony_ci break; 10048c2ecf20Sopenharmony_ci } 10058c2ecf20Sopenharmony_ci } 10068c2ecf20Sopenharmony_ci return (count == p->deg) ? count : 0; 10078c2ecf20Sopenharmony_ci} 10088c2ecf20Sopenharmony_ci#define find_poly_roots(_p, _k, _elp, _loc) chien_search(_p, len, _elp, _loc) 10098c2ecf20Sopenharmony_ci#endif /* USE_CHIEN_SEARCH */ 10108c2ecf20Sopenharmony_ci 10118c2ecf20Sopenharmony_ci/** 10128c2ecf20Sopenharmony_ci * bch_decode - decode received codeword and find bit error locations 10138c2ecf20Sopenharmony_ci * @bch: BCH control structure 10148c2ecf20Sopenharmony_ci * @data: received data, ignored if @calc_ecc is provided 10158c2ecf20Sopenharmony_ci * @len: data length in bytes, must always be provided 10168c2ecf20Sopenharmony_ci * @recv_ecc: received ecc, if NULL then assume it was XORed in @calc_ecc 10178c2ecf20Sopenharmony_ci * @calc_ecc: calculated ecc, if NULL then calc_ecc is computed from @data 10188c2ecf20Sopenharmony_ci * @syn: hw computed syndrome data (if NULL, syndrome is calculated) 10198c2ecf20Sopenharmony_ci * @errloc: output array of error locations 10208c2ecf20Sopenharmony_ci * 10218c2ecf20Sopenharmony_ci * Returns: 10228c2ecf20Sopenharmony_ci * The number of errors found, or -EBADMSG if decoding failed, or -EINVAL if 10238c2ecf20Sopenharmony_ci * invalid parameters were provided 10248c2ecf20Sopenharmony_ci * 10258c2ecf20Sopenharmony_ci * Depending on the available hw BCH support and the need to compute @calc_ecc 10268c2ecf20Sopenharmony_ci * separately (using bch_encode()), this function should be called with one of 10278c2ecf20Sopenharmony_ci * the following parameter configurations - 10288c2ecf20Sopenharmony_ci * 10298c2ecf20Sopenharmony_ci * by providing @data and @recv_ecc only: 10308c2ecf20Sopenharmony_ci * bch_decode(@bch, @data, @len, @recv_ecc, NULL, NULL, @errloc) 10318c2ecf20Sopenharmony_ci * 10328c2ecf20Sopenharmony_ci * by providing @recv_ecc and @calc_ecc: 10338c2ecf20Sopenharmony_ci * bch_decode(@bch, NULL, @len, @recv_ecc, @calc_ecc, NULL, @errloc) 10348c2ecf20Sopenharmony_ci * 10358c2ecf20Sopenharmony_ci * by providing ecc = recv_ecc XOR calc_ecc: 10368c2ecf20Sopenharmony_ci * bch_decode(@bch, NULL, @len, NULL, ecc, NULL, @errloc) 10378c2ecf20Sopenharmony_ci * 10388c2ecf20Sopenharmony_ci * by providing syndrome results @syn: 10398c2ecf20Sopenharmony_ci * bch_decode(@bch, NULL, @len, NULL, NULL, @syn, @errloc) 10408c2ecf20Sopenharmony_ci * 10418c2ecf20Sopenharmony_ci * Once bch_decode() has successfully returned with a positive value, error 10428c2ecf20Sopenharmony_ci * locations returned in array @errloc should be interpreted as follows - 10438c2ecf20Sopenharmony_ci * 10448c2ecf20Sopenharmony_ci * if (errloc[n] >= 8*len), then n-th error is located in ecc (no need for 10458c2ecf20Sopenharmony_ci * data correction) 10468c2ecf20Sopenharmony_ci * 10478c2ecf20Sopenharmony_ci * if (errloc[n] < 8*len), then n-th error is located in data and can be 10488c2ecf20Sopenharmony_ci * corrected with statement data[errloc[n]/8] ^= 1 << (errloc[n] % 8); 10498c2ecf20Sopenharmony_ci * 10508c2ecf20Sopenharmony_ci * Note that this function does not perform any data correction by itself, it 10518c2ecf20Sopenharmony_ci * merely indicates error locations. 10528c2ecf20Sopenharmony_ci */ 10538c2ecf20Sopenharmony_ciint bch_decode(struct bch_control *bch, const uint8_t *data, unsigned int len, 10548c2ecf20Sopenharmony_ci const uint8_t *recv_ecc, const uint8_t *calc_ecc, 10558c2ecf20Sopenharmony_ci const unsigned int *syn, unsigned int *errloc) 10568c2ecf20Sopenharmony_ci{ 10578c2ecf20Sopenharmony_ci const unsigned int ecc_words = BCH_ECC_WORDS(bch); 10588c2ecf20Sopenharmony_ci unsigned int nbits; 10598c2ecf20Sopenharmony_ci int i, err, nroots; 10608c2ecf20Sopenharmony_ci uint32_t sum; 10618c2ecf20Sopenharmony_ci 10628c2ecf20Sopenharmony_ci /* sanity check: make sure data length can be handled */ 10638c2ecf20Sopenharmony_ci if (8*len > (bch->n-bch->ecc_bits)) 10648c2ecf20Sopenharmony_ci return -EINVAL; 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci /* if caller does not provide syndromes, compute them */ 10678c2ecf20Sopenharmony_ci if (!syn) { 10688c2ecf20Sopenharmony_ci if (!calc_ecc) { 10698c2ecf20Sopenharmony_ci /* compute received data ecc into an internal buffer */ 10708c2ecf20Sopenharmony_ci if (!data || !recv_ecc) 10718c2ecf20Sopenharmony_ci return -EINVAL; 10728c2ecf20Sopenharmony_ci bch_encode(bch, data, len, NULL); 10738c2ecf20Sopenharmony_ci } else { 10748c2ecf20Sopenharmony_ci /* load provided calculated ecc */ 10758c2ecf20Sopenharmony_ci load_ecc8(bch, bch->ecc_buf, calc_ecc); 10768c2ecf20Sopenharmony_ci } 10778c2ecf20Sopenharmony_ci /* load received ecc or assume it was XORed in calc_ecc */ 10788c2ecf20Sopenharmony_ci if (recv_ecc) { 10798c2ecf20Sopenharmony_ci load_ecc8(bch, bch->ecc_buf2, recv_ecc); 10808c2ecf20Sopenharmony_ci /* XOR received and calculated ecc */ 10818c2ecf20Sopenharmony_ci for (i = 0, sum = 0; i < (int)ecc_words; i++) { 10828c2ecf20Sopenharmony_ci bch->ecc_buf[i] ^= bch->ecc_buf2[i]; 10838c2ecf20Sopenharmony_ci sum |= bch->ecc_buf[i]; 10848c2ecf20Sopenharmony_ci } 10858c2ecf20Sopenharmony_ci if (!sum) 10868c2ecf20Sopenharmony_ci /* no error found */ 10878c2ecf20Sopenharmony_ci return 0; 10888c2ecf20Sopenharmony_ci } 10898c2ecf20Sopenharmony_ci compute_syndromes(bch, bch->ecc_buf, bch->syn); 10908c2ecf20Sopenharmony_ci syn = bch->syn; 10918c2ecf20Sopenharmony_ci } 10928c2ecf20Sopenharmony_ci 10938c2ecf20Sopenharmony_ci err = compute_error_locator_polynomial(bch, syn); 10948c2ecf20Sopenharmony_ci if (err > 0) { 10958c2ecf20Sopenharmony_ci nroots = find_poly_roots(bch, 1, bch->elp, errloc); 10968c2ecf20Sopenharmony_ci if (err != nroots) 10978c2ecf20Sopenharmony_ci err = -1; 10988c2ecf20Sopenharmony_ci } 10998c2ecf20Sopenharmony_ci if (err > 0) { 11008c2ecf20Sopenharmony_ci /* post-process raw error locations for easier correction */ 11018c2ecf20Sopenharmony_ci nbits = (len*8)+bch->ecc_bits; 11028c2ecf20Sopenharmony_ci for (i = 0; i < err; i++) { 11038c2ecf20Sopenharmony_ci if (errloc[i] >= nbits) { 11048c2ecf20Sopenharmony_ci err = -1; 11058c2ecf20Sopenharmony_ci break; 11068c2ecf20Sopenharmony_ci } 11078c2ecf20Sopenharmony_ci errloc[i] = nbits-1-errloc[i]; 11088c2ecf20Sopenharmony_ci if (!bch->swap_bits) 11098c2ecf20Sopenharmony_ci errloc[i] = (errloc[i] & ~7) | 11108c2ecf20Sopenharmony_ci (7-(errloc[i] & 7)); 11118c2ecf20Sopenharmony_ci } 11128c2ecf20Sopenharmony_ci } 11138c2ecf20Sopenharmony_ci return (err >= 0) ? err : -EBADMSG; 11148c2ecf20Sopenharmony_ci} 11158c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bch_decode); 11168c2ecf20Sopenharmony_ci 11178c2ecf20Sopenharmony_ci/* 11188c2ecf20Sopenharmony_ci * generate Galois field lookup tables 11198c2ecf20Sopenharmony_ci */ 11208c2ecf20Sopenharmony_cistatic int build_gf_tables(struct bch_control *bch, unsigned int poly) 11218c2ecf20Sopenharmony_ci{ 11228c2ecf20Sopenharmony_ci unsigned int i, x = 1; 11238c2ecf20Sopenharmony_ci const unsigned int k = 1 << deg(poly); 11248c2ecf20Sopenharmony_ci 11258c2ecf20Sopenharmony_ci /* primitive polynomial must be of degree m */ 11268c2ecf20Sopenharmony_ci if (k != (1u << GF_M(bch))) 11278c2ecf20Sopenharmony_ci return -1; 11288c2ecf20Sopenharmony_ci 11298c2ecf20Sopenharmony_ci for (i = 0; i < GF_N(bch); i++) { 11308c2ecf20Sopenharmony_ci bch->a_pow_tab[i] = x; 11318c2ecf20Sopenharmony_ci bch->a_log_tab[x] = i; 11328c2ecf20Sopenharmony_ci if (i && (x == 1)) 11338c2ecf20Sopenharmony_ci /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */ 11348c2ecf20Sopenharmony_ci return -1; 11358c2ecf20Sopenharmony_ci x <<= 1; 11368c2ecf20Sopenharmony_ci if (x & k) 11378c2ecf20Sopenharmony_ci x ^= poly; 11388c2ecf20Sopenharmony_ci } 11398c2ecf20Sopenharmony_ci bch->a_pow_tab[GF_N(bch)] = 1; 11408c2ecf20Sopenharmony_ci bch->a_log_tab[0] = 0; 11418c2ecf20Sopenharmony_ci 11428c2ecf20Sopenharmony_ci return 0; 11438c2ecf20Sopenharmony_ci} 11448c2ecf20Sopenharmony_ci 11458c2ecf20Sopenharmony_ci/* 11468c2ecf20Sopenharmony_ci * compute generator polynomial remainder tables for fast encoding 11478c2ecf20Sopenharmony_ci */ 11488c2ecf20Sopenharmony_cistatic void build_mod8_tables(struct bch_control *bch, const uint32_t *g) 11498c2ecf20Sopenharmony_ci{ 11508c2ecf20Sopenharmony_ci int i, j, b, d; 11518c2ecf20Sopenharmony_ci uint32_t data, hi, lo, *tab; 11528c2ecf20Sopenharmony_ci const int l = BCH_ECC_WORDS(bch); 11538c2ecf20Sopenharmony_ci const int plen = DIV_ROUND_UP(bch->ecc_bits+1, 32); 11548c2ecf20Sopenharmony_ci const int ecclen = DIV_ROUND_UP(bch->ecc_bits, 32); 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci memset(bch->mod8_tab, 0, 4*256*l*sizeof(*bch->mod8_tab)); 11578c2ecf20Sopenharmony_ci 11588c2ecf20Sopenharmony_ci for (i = 0; i < 256; i++) { 11598c2ecf20Sopenharmony_ci /* p(X)=i is a small polynomial of weight <= 8 */ 11608c2ecf20Sopenharmony_ci for (b = 0; b < 4; b++) { 11618c2ecf20Sopenharmony_ci /* we want to compute (p(X).X^(8*b+deg(g))) mod g(X) */ 11628c2ecf20Sopenharmony_ci tab = bch->mod8_tab + (b*256+i)*l; 11638c2ecf20Sopenharmony_ci data = i << (8*b); 11648c2ecf20Sopenharmony_ci while (data) { 11658c2ecf20Sopenharmony_ci d = deg(data); 11668c2ecf20Sopenharmony_ci /* subtract X^d.g(X) from p(X).X^(8*b+deg(g)) */ 11678c2ecf20Sopenharmony_ci data ^= g[0] >> (31-d); 11688c2ecf20Sopenharmony_ci for (j = 0; j < ecclen; j++) { 11698c2ecf20Sopenharmony_ci hi = (d < 31) ? g[j] << (d+1) : 0; 11708c2ecf20Sopenharmony_ci lo = (j+1 < plen) ? 11718c2ecf20Sopenharmony_ci g[j+1] >> (31-d) : 0; 11728c2ecf20Sopenharmony_ci tab[j] ^= hi|lo; 11738c2ecf20Sopenharmony_ci } 11748c2ecf20Sopenharmony_ci } 11758c2ecf20Sopenharmony_ci } 11768c2ecf20Sopenharmony_ci } 11778c2ecf20Sopenharmony_ci} 11788c2ecf20Sopenharmony_ci 11798c2ecf20Sopenharmony_ci/* 11808c2ecf20Sopenharmony_ci * build a base for factoring degree 2 polynomials 11818c2ecf20Sopenharmony_ci */ 11828c2ecf20Sopenharmony_cistatic int build_deg2_base(struct bch_control *bch) 11838c2ecf20Sopenharmony_ci{ 11848c2ecf20Sopenharmony_ci const int m = GF_M(bch); 11858c2ecf20Sopenharmony_ci int i, j, r; 11868c2ecf20Sopenharmony_ci unsigned int sum, x, y, remaining, ak = 0, xi[BCH_MAX_M]; 11878c2ecf20Sopenharmony_ci 11888c2ecf20Sopenharmony_ci /* find k s.t. Tr(a^k) = 1 and 0 <= k < m */ 11898c2ecf20Sopenharmony_ci for (i = 0; i < m; i++) { 11908c2ecf20Sopenharmony_ci for (j = 0, sum = 0; j < m; j++) 11918c2ecf20Sopenharmony_ci sum ^= a_pow(bch, i*(1 << j)); 11928c2ecf20Sopenharmony_ci 11938c2ecf20Sopenharmony_ci if (sum) { 11948c2ecf20Sopenharmony_ci ak = bch->a_pow_tab[i]; 11958c2ecf20Sopenharmony_ci break; 11968c2ecf20Sopenharmony_ci } 11978c2ecf20Sopenharmony_ci } 11988c2ecf20Sopenharmony_ci /* find xi, i=0..m-1 such that xi^2+xi = a^i+Tr(a^i).a^k */ 11998c2ecf20Sopenharmony_ci remaining = m; 12008c2ecf20Sopenharmony_ci memset(xi, 0, sizeof(xi)); 12018c2ecf20Sopenharmony_ci 12028c2ecf20Sopenharmony_ci for (x = 0; (x <= GF_N(bch)) && remaining; x++) { 12038c2ecf20Sopenharmony_ci y = gf_sqr(bch, x)^x; 12048c2ecf20Sopenharmony_ci for (i = 0; i < 2; i++) { 12058c2ecf20Sopenharmony_ci r = a_log(bch, y); 12068c2ecf20Sopenharmony_ci if (y && (r < m) && !xi[r]) { 12078c2ecf20Sopenharmony_ci bch->xi_tab[r] = x; 12088c2ecf20Sopenharmony_ci xi[r] = 1; 12098c2ecf20Sopenharmony_ci remaining--; 12108c2ecf20Sopenharmony_ci dbg("x%d = %x\n", r, x); 12118c2ecf20Sopenharmony_ci break; 12128c2ecf20Sopenharmony_ci } 12138c2ecf20Sopenharmony_ci y ^= ak; 12148c2ecf20Sopenharmony_ci } 12158c2ecf20Sopenharmony_ci } 12168c2ecf20Sopenharmony_ci /* should not happen but check anyway */ 12178c2ecf20Sopenharmony_ci return remaining ? -1 : 0; 12188c2ecf20Sopenharmony_ci} 12198c2ecf20Sopenharmony_ci 12208c2ecf20Sopenharmony_cistatic void *bch_alloc(size_t size, int *err) 12218c2ecf20Sopenharmony_ci{ 12228c2ecf20Sopenharmony_ci void *ptr; 12238c2ecf20Sopenharmony_ci 12248c2ecf20Sopenharmony_ci ptr = kmalloc(size, GFP_KERNEL); 12258c2ecf20Sopenharmony_ci if (ptr == NULL) 12268c2ecf20Sopenharmony_ci *err = 1; 12278c2ecf20Sopenharmony_ci return ptr; 12288c2ecf20Sopenharmony_ci} 12298c2ecf20Sopenharmony_ci 12308c2ecf20Sopenharmony_ci/* 12318c2ecf20Sopenharmony_ci * compute generator polynomial for given (m,t) parameters. 12328c2ecf20Sopenharmony_ci */ 12338c2ecf20Sopenharmony_cistatic uint32_t *compute_generator_polynomial(struct bch_control *bch) 12348c2ecf20Sopenharmony_ci{ 12358c2ecf20Sopenharmony_ci const unsigned int m = GF_M(bch); 12368c2ecf20Sopenharmony_ci const unsigned int t = GF_T(bch); 12378c2ecf20Sopenharmony_ci int n, err = 0; 12388c2ecf20Sopenharmony_ci unsigned int i, j, nbits, r, word, *roots; 12398c2ecf20Sopenharmony_ci struct gf_poly *g; 12408c2ecf20Sopenharmony_ci uint32_t *genpoly; 12418c2ecf20Sopenharmony_ci 12428c2ecf20Sopenharmony_ci g = bch_alloc(GF_POLY_SZ(m*t), &err); 12438c2ecf20Sopenharmony_ci roots = bch_alloc((bch->n+1)*sizeof(*roots), &err); 12448c2ecf20Sopenharmony_ci genpoly = bch_alloc(DIV_ROUND_UP(m*t+1, 32)*sizeof(*genpoly), &err); 12458c2ecf20Sopenharmony_ci 12468c2ecf20Sopenharmony_ci if (err) { 12478c2ecf20Sopenharmony_ci kfree(genpoly); 12488c2ecf20Sopenharmony_ci genpoly = NULL; 12498c2ecf20Sopenharmony_ci goto finish; 12508c2ecf20Sopenharmony_ci } 12518c2ecf20Sopenharmony_ci 12528c2ecf20Sopenharmony_ci /* enumerate all roots of g(X) */ 12538c2ecf20Sopenharmony_ci memset(roots , 0, (bch->n+1)*sizeof(*roots)); 12548c2ecf20Sopenharmony_ci for (i = 0; i < t; i++) { 12558c2ecf20Sopenharmony_ci for (j = 0, r = 2*i+1; j < m; j++) { 12568c2ecf20Sopenharmony_ci roots[r] = 1; 12578c2ecf20Sopenharmony_ci r = mod_s(bch, 2*r); 12588c2ecf20Sopenharmony_ci } 12598c2ecf20Sopenharmony_ci } 12608c2ecf20Sopenharmony_ci /* build generator polynomial g(X) */ 12618c2ecf20Sopenharmony_ci g->deg = 0; 12628c2ecf20Sopenharmony_ci g->c[0] = 1; 12638c2ecf20Sopenharmony_ci for (i = 0; i < GF_N(bch); i++) { 12648c2ecf20Sopenharmony_ci if (roots[i]) { 12658c2ecf20Sopenharmony_ci /* multiply g(X) by (X+root) */ 12668c2ecf20Sopenharmony_ci r = bch->a_pow_tab[i]; 12678c2ecf20Sopenharmony_ci g->c[g->deg+1] = 1; 12688c2ecf20Sopenharmony_ci for (j = g->deg; j > 0; j--) 12698c2ecf20Sopenharmony_ci g->c[j] = gf_mul(bch, g->c[j], r)^g->c[j-1]; 12708c2ecf20Sopenharmony_ci 12718c2ecf20Sopenharmony_ci g->c[0] = gf_mul(bch, g->c[0], r); 12728c2ecf20Sopenharmony_ci g->deg++; 12738c2ecf20Sopenharmony_ci } 12748c2ecf20Sopenharmony_ci } 12758c2ecf20Sopenharmony_ci /* store left-justified binary representation of g(X) */ 12768c2ecf20Sopenharmony_ci n = g->deg+1; 12778c2ecf20Sopenharmony_ci i = 0; 12788c2ecf20Sopenharmony_ci 12798c2ecf20Sopenharmony_ci while (n > 0) { 12808c2ecf20Sopenharmony_ci nbits = (n > 32) ? 32 : n; 12818c2ecf20Sopenharmony_ci for (j = 0, word = 0; j < nbits; j++) { 12828c2ecf20Sopenharmony_ci if (g->c[n-1-j]) 12838c2ecf20Sopenharmony_ci word |= 1u << (31-j); 12848c2ecf20Sopenharmony_ci } 12858c2ecf20Sopenharmony_ci genpoly[i++] = word; 12868c2ecf20Sopenharmony_ci n -= nbits; 12878c2ecf20Sopenharmony_ci } 12888c2ecf20Sopenharmony_ci bch->ecc_bits = g->deg; 12898c2ecf20Sopenharmony_ci 12908c2ecf20Sopenharmony_cifinish: 12918c2ecf20Sopenharmony_ci kfree(g); 12928c2ecf20Sopenharmony_ci kfree(roots); 12938c2ecf20Sopenharmony_ci 12948c2ecf20Sopenharmony_ci return genpoly; 12958c2ecf20Sopenharmony_ci} 12968c2ecf20Sopenharmony_ci 12978c2ecf20Sopenharmony_ci/** 12988c2ecf20Sopenharmony_ci * bch_init - initialize a BCH encoder/decoder 12998c2ecf20Sopenharmony_ci * @m: Galois field order, should be in the range 5-15 13008c2ecf20Sopenharmony_ci * @t: maximum error correction capability, in bits 13018c2ecf20Sopenharmony_ci * @prim_poly: user-provided primitive polynomial (or 0 to use default) 13028c2ecf20Sopenharmony_ci * @swap_bits: swap bits within data and syndrome bytes 13038c2ecf20Sopenharmony_ci * 13048c2ecf20Sopenharmony_ci * Returns: 13058c2ecf20Sopenharmony_ci * a newly allocated BCH control structure if successful, NULL otherwise 13068c2ecf20Sopenharmony_ci * 13078c2ecf20Sopenharmony_ci * This initialization can take some time, as lookup tables are built for fast 13088c2ecf20Sopenharmony_ci * encoding/decoding; make sure not to call this function from a time critical 13098c2ecf20Sopenharmony_ci * path. Usually, bch_init() should be called on module/driver init and 13108c2ecf20Sopenharmony_ci * bch_free() should be called to release memory on exit. 13118c2ecf20Sopenharmony_ci * 13128c2ecf20Sopenharmony_ci * You may provide your own primitive polynomial of degree @m in argument 13138c2ecf20Sopenharmony_ci * @prim_poly, or let bch_init() use its default polynomial. 13148c2ecf20Sopenharmony_ci * 13158c2ecf20Sopenharmony_ci * Once bch_init() has successfully returned a pointer to a newly allocated 13168c2ecf20Sopenharmony_ci * BCH control structure, ecc length in bytes is given by member @ecc_bytes of 13178c2ecf20Sopenharmony_ci * the structure. 13188c2ecf20Sopenharmony_ci */ 13198c2ecf20Sopenharmony_cistruct bch_control *bch_init(int m, int t, unsigned int prim_poly, 13208c2ecf20Sopenharmony_ci bool swap_bits) 13218c2ecf20Sopenharmony_ci{ 13228c2ecf20Sopenharmony_ci int err = 0; 13238c2ecf20Sopenharmony_ci unsigned int i, words; 13248c2ecf20Sopenharmony_ci uint32_t *genpoly; 13258c2ecf20Sopenharmony_ci struct bch_control *bch = NULL; 13268c2ecf20Sopenharmony_ci 13278c2ecf20Sopenharmony_ci const int min_m = 5; 13288c2ecf20Sopenharmony_ci 13298c2ecf20Sopenharmony_ci /* default primitive polynomials */ 13308c2ecf20Sopenharmony_ci static const unsigned int prim_poly_tab[] = { 13318c2ecf20Sopenharmony_ci 0x25, 0x43, 0x83, 0x11d, 0x211, 0x409, 0x805, 0x1053, 0x201b, 13328c2ecf20Sopenharmony_ci 0x402b, 0x8003, 13338c2ecf20Sopenharmony_ci }; 13348c2ecf20Sopenharmony_ci 13358c2ecf20Sopenharmony_ci#if defined(CONFIG_BCH_CONST_PARAMS) 13368c2ecf20Sopenharmony_ci if ((m != (CONFIG_BCH_CONST_M)) || (t != (CONFIG_BCH_CONST_T))) { 13378c2ecf20Sopenharmony_ci printk(KERN_ERR "bch encoder/decoder was configured to support " 13388c2ecf20Sopenharmony_ci "parameters m=%d, t=%d only!\n", 13398c2ecf20Sopenharmony_ci CONFIG_BCH_CONST_M, CONFIG_BCH_CONST_T); 13408c2ecf20Sopenharmony_ci goto fail; 13418c2ecf20Sopenharmony_ci } 13428c2ecf20Sopenharmony_ci#endif 13438c2ecf20Sopenharmony_ci if ((m < min_m) || (m > BCH_MAX_M)) 13448c2ecf20Sopenharmony_ci /* 13458c2ecf20Sopenharmony_ci * values of m greater than 15 are not currently supported; 13468c2ecf20Sopenharmony_ci * supporting m > 15 would require changing table base type 13478c2ecf20Sopenharmony_ci * (uint16_t) and a small patch in matrix transposition 13488c2ecf20Sopenharmony_ci */ 13498c2ecf20Sopenharmony_ci goto fail; 13508c2ecf20Sopenharmony_ci 13518c2ecf20Sopenharmony_ci if (t > BCH_MAX_T) 13528c2ecf20Sopenharmony_ci /* 13538c2ecf20Sopenharmony_ci * we can support larger than 64 bits if necessary, at the 13548c2ecf20Sopenharmony_ci * cost of higher stack usage. 13558c2ecf20Sopenharmony_ci */ 13568c2ecf20Sopenharmony_ci goto fail; 13578c2ecf20Sopenharmony_ci 13588c2ecf20Sopenharmony_ci /* sanity checks */ 13598c2ecf20Sopenharmony_ci if ((t < 1) || (m*t >= ((1 << m)-1))) 13608c2ecf20Sopenharmony_ci /* invalid t value */ 13618c2ecf20Sopenharmony_ci goto fail; 13628c2ecf20Sopenharmony_ci 13638c2ecf20Sopenharmony_ci /* select a primitive polynomial for generating GF(2^m) */ 13648c2ecf20Sopenharmony_ci if (prim_poly == 0) 13658c2ecf20Sopenharmony_ci prim_poly = prim_poly_tab[m-min_m]; 13668c2ecf20Sopenharmony_ci 13678c2ecf20Sopenharmony_ci bch = kzalloc(sizeof(*bch), GFP_KERNEL); 13688c2ecf20Sopenharmony_ci if (bch == NULL) 13698c2ecf20Sopenharmony_ci goto fail; 13708c2ecf20Sopenharmony_ci 13718c2ecf20Sopenharmony_ci bch->m = m; 13728c2ecf20Sopenharmony_ci bch->t = t; 13738c2ecf20Sopenharmony_ci bch->n = (1 << m)-1; 13748c2ecf20Sopenharmony_ci words = DIV_ROUND_UP(m*t, 32); 13758c2ecf20Sopenharmony_ci bch->ecc_bytes = DIV_ROUND_UP(m*t, 8); 13768c2ecf20Sopenharmony_ci bch->a_pow_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_pow_tab), &err); 13778c2ecf20Sopenharmony_ci bch->a_log_tab = bch_alloc((1+bch->n)*sizeof(*bch->a_log_tab), &err); 13788c2ecf20Sopenharmony_ci bch->mod8_tab = bch_alloc(words*1024*sizeof(*bch->mod8_tab), &err); 13798c2ecf20Sopenharmony_ci bch->ecc_buf = bch_alloc(words*sizeof(*bch->ecc_buf), &err); 13808c2ecf20Sopenharmony_ci bch->ecc_buf2 = bch_alloc(words*sizeof(*bch->ecc_buf2), &err); 13818c2ecf20Sopenharmony_ci bch->xi_tab = bch_alloc(m*sizeof(*bch->xi_tab), &err); 13828c2ecf20Sopenharmony_ci bch->syn = bch_alloc(2*t*sizeof(*bch->syn), &err); 13838c2ecf20Sopenharmony_ci bch->cache = bch_alloc(2*t*sizeof(*bch->cache), &err); 13848c2ecf20Sopenharmony_ci bch->elp = bch_alloc((t+1)*sizeof(struct gf_poly_deg1), &err); 13858c2ecf20Sopenharmony_ci bch->swap_bits = swap_bits; 13868c2ecf20Sopenharmony_ci 13878c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) 13888c2ecf20Sopenharmony_ci bch->poly_2t[i] = bch_alloc(GF_POLY_SZ(2*t), &err); 13898c2ecf20Sopenharmony_ci 13908c2ecf20Sopenharmony_ci if (err) 13918c2ecf20Sopenharmony_ci goto fail; 13928c2ecf20Sopenharmony_ci 13938c2ecf20Sopenharmony_ci err = build_gf_tables(bch, prim_poly); 13948c2ecf20Sopenharmony_ci if (err) 13958c2ecf20Sopenharmony_ci goto fail; 13968c2ecf20Sopenharmony_ci 13978c2ecf20Sopenharmony_ci /* use generator polynomial for computing encoding tables */ 13988c2ecf20Sopenharmony_ci genpoly = compute_generator_polynomial(bch); 13998c2ecf20Sopenharmony_ci if (genpoly == NULL) 14008c2ecf20Sopenharmony_ci goto fail; 14018c2ecf20Sopenharmony_ci 14028c2ecf20Sopenharmony_ci build_mod8_tables(bch, genpoly); 14038c2ecf20Sopenharmony_ci kfree(genpoly); 14048c2ecf20Sopenharmony_ci 14058c2ecf20Sopenharmony_ci err = build_deg2_base(bch); 14068c2ecf20Sopenharmony_ci if (err) 14078c2ecf20Sopenharmony_ci goto fail; 14088c2ecf20Sopenharmony_ci 14098c2ecf20Sopenharmony_ci return bch; 14108c2ecf20Sopenharmony_ci 14118c2ecf20Sopenharmony_cifail: 14128c2ecf20Sopenharmony_ci bch_free(bch); 14138c2ecf20Sopenharmony_ci return NULL; 14148c2ecf20Sopenharmony_ci} 14158c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bch_init); 14168c2ecf20Sopenharmony_ci 14178c2ecf20Sopenharmony_ci/** 14188c2ecf20Sopenharmony_ci * bch_free - free the BCH control structure 14198c2ecf20Sopenharmony_ci * @bch: BCH control structure to release 14208c2ecf20Sopenharmony_ci */ 14218c2ecf20Sopenharmony_civoid bch_free(struct bch_control *bch) 14228c2ecf20Sopenharmony_ci{ 14238c2ecf20Sopenharmony_ci unsigned int i; 14248c2ecf20Sopenharmony_ci 14258c2ecf20Sopenharmony_ci if (bch) { 14268c2ecf20Sopenharmony_ci kfree(bch->a_pow_tab); 14278c2ecf20Sopenharmony_ci kfree(bch->a_log_tab); 14288c2ecf20Sopenharmony_ci kfree(bch->mod8_tab); 14298c2ecf20Sopenharmony_ci kfree(bch->ecc_buf); 14308c2ecf20Sopenharmony_ci kfree(bch->ecc_buf2); 14318c2ecf20Sopenharmony_ci kfree(bch->xi_tab); 14328c2ecf20Sopenharmony_ci kfree(bch->syn); 14338c2ecf20Sopenharmony_ci kfree(bch->cache); 14348c2ecf20Sopenharmony_ci kfree(bch->elp); 14358c2ecf20Sopenharmony_ci 14368c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(bch->poly_2t); i++) 14378c2ecf20Sopenharmony_ci kfree(bch->poly_2t[i]); 14388c2ecf20Sopenharmony_ci 14398c2ecf20Sopenharmony_ci kfree(bch); 14408c2ecf20Sopenharmony_ci } 14418c2ecf20Sopenharmony_ci} 14428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bch_free); 14438c2ecf20Sopenharmony_ci 14448c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 14458c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>"); 14468c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Binary BCH encoder/decoder"); 1447