Thomas Gleixner | 8116125 | 2019-05-20 19:08:14 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 2 | /* |
| 3 | * This file provides ECC correction for more than 1 bit per block of data, |
| 4 | * using binary BCH codes. It relies on the generic BCH library lib/bch.c. |
| 5 | * |
| 6 | * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/bitops.h> |
| 14 | #include <linux/mtd/mtd.h> |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 15 | #include <linux/mtd/rawnand.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 16 | #include <linux/mtd/nand_bch.h> |
| 17 | #include <linux/bch.h> |
| 18 | |
| 19 | /** |
| 20 | * struct nand_bch_control - private NAND BCH control structure |
| 21 | * @bch: BCH control structure |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 22 | * @errloc: error location array |
| 23 | * @eccmask: XOR ecc mask, allows erased pages to be decoded as valid |
| 24 | */ |
| 25 | struct nand_bch_control { |
| 26 | struct bch_control *bch; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 27 | unsigned int *errloc; |
| 28 | unsigned char *eccmask; |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * nand_bch_calculate_ecc - [NAND Interface] Calculate ECC for data block |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 33 | * @chip: NAND chip object |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 34 | * @buf: input buffer with raw data |
| 35 | * @code: output buffer with ECC |
| 36 | */ |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 37 | int nand_bch_calculate_ecc(struct nand_chip *chip, const unsigned char *buf, |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 38 | unsigned char *code) |
| 39 | { |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 40 | struct nand_bch_control *nbc = chip->ecc.priv; |
| 41 | unsigned int i; |
| 42 | |
| 43 | memset(code, 0, chip->ecc.bytes); |
| 44 | encode_bch(nbc->bch, buf, chip->ecc.size, code); |
| 45 | |
| 46 | /* apply mask so that an erased page is a valid codeword */ |
| 47 | for (i = 0; i < chip->ecc.bytes; i++) |
| 48 | code[i] ^= nbc->eccmask[i]; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | EXPORT_SYMBOL(nand_bch_calculate_ecc); |
| 53 | |
| 54 | /** |
| 55 | * nand_bch_correct_data - [NAND Interface] Detect and correct bit error(s) |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 56 | * @chip: NAND chip object |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 57 | * @buf: raw data read from the chip |
| 58 | * @read_ecc: ECC from the chip |
| 59 | * @calc_ecc: the ECC calculated from raw data |
| 60 | * |
| 61 | * Detect and correct bit errors for a data byte block |
| 62 | */ |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 63 | int nand_bch_correct_data(struct nand_chip *chip, unsigned char *buf, |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 64 | unsigned char *read_ecc, unsigned char *calc_ecc) |
| 65 | { |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 66 | struct nand_bch_control *nbc = chip->ecc.priv; |
| 67 | unsigned int *errloc = nbc->errloc; |
| 68 | int i, count; |
| 69 | |
| 70 | count = decode_bch(nbc->bch, NULL, chip->ecc.size, read_ecc, calc_ecc, |
| 71 | NULL, errloc); |
| 72 | if (count > 0) { |
| 73 | for (i = 0; i < count; i++) { |
| 74 | if (errloc[i] < (chip->ecc.size*8)) |
| 75 | /* error is located in data, correct it */ |
| 76 | buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7)); |
| 77 | /* else error in ecc, no action needed */ |
| 78 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 79 | pr_debug("%s: corrected bitflip %u\n", __func__, |
| 80 | errloc[i]); |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 81 | } |
| 82 | } else if (count < 0) { |
Shreeya Patel | 63fa37f | 2018-02-22 22:01:22 +0530 | [diff] [blame] | 83 | pr_err("ecc unrecoverable error\n"); |
Boris BREZILLON | 6e94119 | 2015-12-30 20:32:03 +0100 | [diff] [blame] | 84 | count = -EBADMSG; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 85 | } |
| 86 | return count; |
| 87 | } |
| 88 | EXPORT_SYMBOL(nand_bch_correct_data); |
| 89 | |
| 90 | /** |
| 91 | * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction |
| 92 | * @mtd: MTD block structure |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 93 | * |
| 94 | * Returns: |
| 95 | * a pointer to a new NAND BCH control structure, or NULL upon failure |
| 96 | * |
| 97 | * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes |
| 98 | * are used to compute BCH parameters m (Galois field order) and t (error |
| 99 | * correction capability). @eccbytes should be equal to the number of bytes |
| 100 | * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8. |
| 101 | * |
| 102 | * Example: to configure 4 bit correction per 512 bytes, you should pass |
| 103 | * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8) |
| 104 | * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits) |
| 105 | */ |
Boris BREZILLON | a8c65d5 | 2016-03-07 10:46:54 +0100 | [diff] [blame] | 106 | struct nand_bch_control *nand_bch_init(struct mtd_info *mtd) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 107 | { |
Boris BREZILLON | a8c65d5 | 2016-03-07 10:46:54 +0100 | [diff] [blame] | 108 | struct nand_chip *nand = mtd_to_nand(mtd); |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 109 | unsigned int m, t, eccsteps, i; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 110 | struct nand_bch_control *nbc = NULL; |
| 111 | unsigned char *erased_page; |
Boris BREZILLON | a8c65d5 | 2016-03-07 10:46:54 +0100 | [diff] [blame] | 112 | unsigned int eccsize = nand->ecc.size; |
| 113 | unsigned int eccbytes = nand->ecc.bytes; |
| 114 | unsigned int eccstrength = nand->ecc.strength; |
| 115 | |
| 116 | if (!eccbytes && eccstrength) { |
| 117 | eccbytes = DIV_ROUND_UP(eccstrength * fls(8 * eccsize), 8); |
| 118 | nand->ecc.bytes = eccbytes; |
| 119 | } |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 120 | |
| 121 | if (!eccsize || !eccbytes) { |
Shreeya Patel | 63fa37f | 2018-02-22 22:01:22 +0530 | [diff] [blame] | 122 | pr_warn("ecc parameters not supplied\n"); |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 123 | goto fail; |
| 124 | } |
| 125 | |
| 126 | m = fls(1+8*eccsize); |
| 127 | t = (eccbytes*8)/m; |
| 128 | |
| 129 | nbc = kzalloc(sizeof(*nbc), GFP_KERNEL); |
| 130 | if (!nbc) |
| 131 | goto fail; |
| 132 | |
| 133 | nbc->bch = init_bch(m, t, 0); |
| 134 | if (!nbc->bch) |
| 135 | goto fail; |
| 136 | |
| 137 | /* verify that eccbytes has the expected value */ |
| 138 | if (nbc->bch->ecc_bytes != eccbytes) { |
Shreeya Patel | 63fa37f | 2018-02-22 22:01:22 +0530 | [diff] [blame] | 139 | pr_warn("invalid eccbytes %u, should be %u\n", |
| 140 | eccbytes, nbc->bch->ecc_bytes); |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 141 | goto fail; |
| 142 | } |
| 143 | |
| 144 | eccsteps = mtd->writesize/eccsize; |
| 145 | |
Boris Brezillon | 7cf9c19 | 2016-02-03 19:53:40 +0100 | [diff] [blame] | 146 | /* Check that we have an oob layout description. */ |
| 147 | if (!mtd->ooblayout) { |
| 148 | pr_warn("missing oob scheme"); |
| 149 | goto fail; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* sanity checks */ |
| 153 | if (8*(eccsize+eccbytes) >= (1 << m)) { |
Shreeya Patel | 63fa37f | 2018-02-22 22:01:22 +0530 | [diff] [blame] | 154 | pr_warn("eccsize %u is too large\n", eccsize); |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 155 | goto fail; |
| 156 | } |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | * ecc->steps and ecc->total might be used by mtd->ooblayout->ecc(), |
| 160 | * which is called by mtd_ooblayout_count_eccbytes(). |
| 161 | * Make sure they are properly initialized before calling |
| 162 | * mtd_ooblayout_count_eccbytes(). |
Brian Norris | 3603ea0 | 2016-05-06 09:31:18 -0700 | [diff] [blame] | 163 | * FIXME: we should probably rework the sequencing in nand_scan_tail() |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 164 | * to avoid setting those fields twice. |
| 165 | */ |
| 166 | nand->ecc.steps = eccsteps; |
| 167 | nand->ecc.total = eccsteps * eccbytes; |
| 168 | if (mtd_ooblayout_count_eccbytes(mtd) != (eccsteps*eccbytes)) { |
Shreeya Patel | 63fa37f | 2018-02-22 22:01:22 +0530 | [diff] [blame] | 169 | pr_warn("invalid ecc layout\n"); |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 170 | goto fail; |
| 171 | } |
| 172 | |
| 173 | nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL); |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 174 | nbc->errloc = kmalloc_array(t, sizeof(*nbc->errloc), GFP_KERNEL); |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 175 | if (!nbc->eccmask || !nbc->errloc) |
| 176 | goto fail; |
| 177 | /* |
| 178 | * compute and store the inverted ecc of an erased ecc block |
| 179 | */ |
| 180 | erased_page = kmalloc(eccsize, GFP_KERNEL); |
| 181 | if (!erased_page) |
| 182 | goto fail; |
| 183 | |
| 184 | memset(erased_page, 0xff, eccsize); |
| 185 | memset(nbc->eccmask, 0, eccbytes); |
| 186 | encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask); |
| 187 | kfree(erased_page); |
| 188 | |
| 189 | for (i = 0; i < eccbytes; i++) |
| 190 | nbc->eccmask[i] ^= 0xff; |
| 191 | |
Boris BREZILLON | a8c65d5 | 2016-03-07 10:46:54 +0100 | [diff] [blame] | 192 | if (!eccstrength) |
| 193 | nand->ecc.strength = (eccbytes * 8) / fls(8 * eccsize); |
| 194 | |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 195 | return nbc; |
| 196 | fail: |
| 197 | nand_bch_free(nbc); |
| 198 | return NULL; |
| 199 | } |
| 200 | EXPORT_SYMBOL(nand_bch_init); |
| 201 | |
| 202 | /** |
| 203 | * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources |
| 204 | * @nbc: NAND BCH control structure |
| 205 | */ |
| 206 | void nand_bch_free(struct nand_bch_control *nbc) |
| 207 | { |
| 208 | if (nbc) { |
| 209 | free_bch(nbc->bch); |
| 210 | kfree(nbc->errloc); |
| 211 | kfree(nbc->eccmask); |
| 212 | kfree(nbc); |
| 213 | } |
| 214 | } |
| 215 | EXPORT_SYMBOL(nand_bch_free); |
| 216 | |
| 217 | MODULE_LICENSE("GPL"); |
| 218 | MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>"); |
| 219 | MODULE_DESCRIPTION("NAND software BCH ECC support"); |