Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/list.h> |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 4 | #include <linux/random.h> |
| 5 | #include <linux/string.h> |
| 6 | #include <linux/bitops.h> |
Akinobu Mita | 1749c00 | 2012-09-03 22:00:01 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 8 | #include <linux/mtd/nand_ecc.h> |
| 9 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 10 | /* |
| 11 | * Test the implementation for software ECC |
| 12 | * |
| 13 | * No actual MTD device is needed, So we don't need to warry about losing |
| 14 | * important data by human error. |
| 15 | * |
| 16 | * This covers possible patterns of corruption which can be reliably corrected |
| 17 | * or detected. |
| 18 | */ |
| 19 | |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 20 | #if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE) |
| 21 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 22 | struct nand_ecc_test { |
| 23 | const char *name; |
| 24 | void (*prepare)(void *, void *, void *, void *, const size_t); |
| 25 | int (*verify)(void *, void *, void *, const size_t); |
| 26 | }; |
| 27 | |
Akinobu Mita | c092b43 | 2012-09-08 01:48:06 +0900 | [diff] [blame] | 28 | /* |
| 29 | * The reason for this __change_bit_le() instead of __change_bit() is to inject |
| 30 | * bit error properly within the region which is not a multiple of |
| 31 | * sizeof(unsigned long) on big-endian systems |
| 32 | */ |
| 33 | #ifdef __LITTLE_ENDIAN |
| 34 | #define __change_bit_le(nr, addr) __change_bit(nr, addr) |
| 35 | #elif defined(__BIG_ENDIAN) |
| 36 | #define __change_bit_le(nr, addr) \ |
| 37 | __change_bit((nr) ^ ((BITS_PER_LONG - 1) & ~0x7), addr) |
| 38 | #else |
| 39 | #error "Unknown byte order" |
| 40 | #endif |
| 41 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 42 | static void single_bit_error_data(void *error_data, void *correct_data, |
| 43 | size_t size) |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 44 | { |
Akinobu Mita | c092b43 | 2012-09-08 01:48:06 +0900 | [diff] [blame] | 45 | unsigned int offset = random32() % (size * BITS_PER_BYTE); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 46 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 47 | memcpy(error_data, correct_data, size); |
| 48 | __change_bit_le(offset, error_data); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 49 | } |
| 50 | |
Akinobu Mita | 6ed089c | 2012-09-08 01:48:10 +0900 | [diff] [blame] | 51 | static void double_bit_error_data(void *error_data, void *correct_data, |
| 52 | size_t size) |
| 53 | { |
| 54 | unsigned int offset[2]; |
| 55 | |
| 56 | offset[0] = random32() % (size * BITS_PER_BYTE); |
| 57 | do { |
| 58 | offset[1] = random32() % (size * BITS_PER_BYTE); |
| 59 | } while (offset[0] == offset[1]); |
| 60 | |
| 61 | memcpy(error_data, correct_data, size); |
| 62 | |
| 63 | __change_bit_le(offset[0], error_data); |
| 64 | __change_bit_le(offset[1], error_data); |
| 65 | } |
| 66 | |
Akinobu Mita | 200ab84 | 2012-09-08 01:48:09 +0900 | [diff] [blame] | 67 | static unsigned int random_ecc_bit(size_t size) |
| 68 | { |
| 69 | unsigned int offset = random32() % (3 * BITS_PER_BYTE); |
| 70 | |
| 71 | if (size == 256) { |
| 72 | /* |
| 73 | * Don't inject a bit error into the insignificant bits (16th |
| 74 | * and 17th bit) in ECC code for 256 byte data block |
| 75 | */ |
| 76 | while (offset == 16 || offset == 17) |
| 77 | offset = random32() % (3 * BITS_PER_BYTE); |
| 78 | } |
| 79 | |
| 80 | return offset; |
| 81 | } |
| 82 | |
| 83 | static void single_bit_error_ecc(void *error_ecc, void *correct_ecc, |
| 84 | size_t size) |
| 85 | { |
| 86 | unsigned int offset = random_ecc_bit(size); |
| 87 | |
| 88 | memcpy(error_ecc, correct_ecc, 3); |
| 89 | __change_bit_le(offset, error_ecc); |
| 90 | } |
| 91 | |
Akinobu Mita | 6ed089c | 2012-09-08 01:48:10 +0900 | [diff] [blame] | 92 | static void double_bit_error_ecc(void *error_ecc, void *correct_ecc, |
| 93 | size_t size) |
| 94 | { |
| 95 | unsigned int offset[2]; |
| 96 | |
| 97 | offset[0] = random_ecc_bit(size); |
| 98 | do { |
| 99 | offset[1] = random_ecc_bit(size); |
| 100 | } while (offset[0] == offset[1]); |
| 101 | |
| 102 | memcpy(error_ecc, correct_ecc, 3); |
| 103 | __change_bit_le(offset[0], error_ecc); |
| 104 | __change_bit_le(offset[1], error_ecc); |
| 105 | } |
| 106 | |
Akinobu Mita | ccaa679 | 2012-09-08 01:48:08 +0900 | [diff] [blame] | 107 | static void no_bit_error(void *error_data, void *error_ecc, |
| 108 | void *correct_data, void *correct_ecc, const size_t size) |
| 109 | { |
| 110 | memcpy(error_data, correct_data, size); |
| 111 | memcpy(error_ecc, correct_ecc, 3); |
| 112 | } |
| 113 | |
| 114 | static int no_bit_error_verify(void *error_data, void *error_ecc, |
| 115 | void *correct_data, const size_t size) |
| 116 | { |
| 117 | unsigned char calc_ecc[3]; |
| 118 | int ret; |
| 119 | |
| 120 | __nand_calculate_ecc(error_data, size, calc_ecc); |
| 121 | ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size); |
| 122 | if (ret == 0 && !memcmp(correct_data, error_data, size)) |
| 123 | return 0; |
| 124 | |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 128 | static void single_bit_error_in_data(void *error_data, void *error_ecc, |
| 129 | void *correct_data, void *correct_ecc, const size_t size) |
| 130 | { |
| 131 | single_bit_error_data(error_data, correct_data, size); |
| 132 | memcpy(error_ecc, correct_ecc, 3); |
| 133 | } |
| 134 | |
Akinobu Mita | 200ab84 | 2012-09-08 01:48:09 +0900 | [diff] [blame] | 135 | static void single_bit_error_in_ecc(void *error_data, void *error_ecc, |
| 136 | void *correct_data, void *correct_ecc, const size_t size) |
| 137 | { |
| 138 | memcpy(error_data, correct_data, size); |
| 139 | single_bit_error_ecc(error_ecc, correct_ecc, size); |
| 140 | } |
| 141 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 142 | static int single_bit_error_correct(void *error_data, void *error_ecc, |
| 143 | void *correct_data, const size_t size) |
| 144 | { |
| 145 | unsigned char calc_ecc[3]; |
| 146 | int ret; |
| 147 | |
| 148 | __nand_calculate_ecc(error_data, size, calc_ecc); |
| 149 | ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size); |
| 150 | if (ret == 1 && !memcmp(correct_data, error_data, size)) |
| 151 | return 0; |
| 152 | |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | |
Akinobu Mita | 6ed089c | 2012-09-08 01:48:10 +0900 | [diff] [blame] | 156 | static void double_bit_error_in_data(void *error_data, void *error_ecc, |
| 157 | void *correct_data, void *correct_ecc, const size_t size) |
| 158 | { |
| 159 | double_bit_error_data(error_data, correct_data, size); |
| 160 | memcpy(error_ecc, correct_ecc, 3); |
| 161 | } |
| 162 | |
| 163 | static void single_bit_error_in_data_and_ecc(void *error_data, void *error_ecc, |
| 164 | void *correct_data, void *correct_ecc, const size_t size) |
| 165 | { |
| 166 | single_bit_error_data(error_data, correct_data, size); |
| 167 | single_bit_error_ecc(error_ecc, correct_ecc, size); |
| 168 | } |
| 169 | |
| 170 | static void double_bit_error_in_ecc(void *error_data, void *error_ecc, |
| 171 | void *correct_data, void *correct_ecc, const size_t size) |
| 172 | { |
| 173 | memcpy(error_data, correct_data, size); |
| 174 | double_bit_error_ecc(error_ecc, correct_ecc, size); |
| 175 | } |
| 176 | |
| 177 | static int double_bit_error_detect(void *error_data, void *error_ecc, |
| 178 | void *correct_data, const size_t size) |
| 179 | { |
| 180 | unsigned char calc_ecc[3]; |
| 181 | int ret; |
| 182 | |
| 183 | __nand_calculate_ecc(error_data, size, calc_ecc); |
| 184 | ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size); |
| 185 | |
| 186 | return (ret == -1) ? 0 : -EINVAL; |
| 187 | } |
| 188 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 189 | static const struct nand_ecc_test nand_ecc_test[] = { |
| 190 | { |
Akinobu Mita | ccaa679 | 2012-09-08 01:48:08 +0900 | [diff] [blame] | 191 | .name = "no-bit-error", |
| 192 | .prepare = no_bit_error, |
| 193 | .verify = no_bit_error_verify, |
| 194 | }, |
| 195 | { |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 196 | .name = "single-bit-error-in-data-correct", |
| 197 | .prepare = single_bit_error_in_data, |
| 198 | .verify = single_bit_error_correct, |
| 199 | }, |
Akinobu Mita | 200ab84 | 2012-09-08 01:48:09 +0900 | [diff] [blame] | 200 | { |
| 201 | .name = "single-bit-error-in-ecc-correct", |
| 202 | .prepare = single_bit_error_in_ecc, |
| 203 | .verify = single_bit_error_correct, |
| 204 | }, |
Akinobu Mita | 6ed089c | 2012-09-08 01:48:10 +0900 | [diff] [blame] | 205 | { |
| 206 | .name = "double-bit-error-in-data-detect", |
| 207 | .prepare = double_bit_error_in_data, |
| 208 | .verify = double_bit_error_detect, |
| 209 | }, |
| 210 | { |
| 211 | .name = "single-bit-error-in-data-and-ecc-detect", |
| 212 | .prepare = single_bit_error_in_data_and_ecc, |
| 213 | .verify = double_bit_error_detect, |
| 214 | }, |
| 215 | { |
| 216 | .name = "double-bit-error-in-ecc-detect", |
| 217 | .prepare = double_bit_error_in_ecc, |
| 218 | .verify = double_bit_error_detect, |
| 219 | }, |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 220 | }; |
| 221 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame] | 222 | static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data, |
| 223 | void *correct_ecc, const size_t size) |
| 224 | { |
| 225 | pr_info("hexdump of error data:\n"); |
| 226 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4, |
| 227 | error_data, size, false); |
| 228 | print_hex_dump(KERN_INFO, "hexdump of error ecc: ", |
| 229 | DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false); |
| 230 | |
| 231 | pr_info("hexdump of correct data:\n"); |
| 232 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4, |
| 233 | correct_data, size, false); |
| 234 | print_hex_dump(KERN_INFO, "hexdump of correct ecc: ", |
| 235 | DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false); |
| 236 | } |
| 237 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 238 | static int nand_ecc_test_run(const size_t size) |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 239 | { |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 240 | int i; |
Akinobu Mita | 1749c00 | 2012-09-03 22:00:01 +0900 | [diff] [blame] | 241 | int err = 0; |
| 242 | void *error_data; |
| 243 | void *error_ecc; |
| 244 | void *correct_data; |
| 245 | void *correct_ecc; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 246 | |
Akinobu Mita | 1749c00 | 2012-09-03 22:00:01 +0900 | [diff] [blame] | 247 | error_data = kmalloc(size, GFP_KERNEL); |
| 248 | error_ecc = kmalloc(3, GFP_KERNEL); |
| 249 | correct_data = kmalloc(size, GFP_KERNEL); |
| 250 | correct_ecc = kmalloc(3, GFP_KERNEL); |
| 251 | |
| 252 | if (!error_data || !error_ecc || !correct_data || !correct_ecc) { |
| 253 | err = -ENOMEM; |
| 254 | goto error; |
| 255 | } |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 256 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame] | 257 | get_random_bytes(correct_data, size); |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame] | 258 | __nand_calculate_ecc(correct_data, size, correct_ecc); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 259 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 260 | for (i = 0; i < ARRAY_SIZE(nand_ecc_test); i++) { |
| 261 | nand_ecc_test[i].prepare(error_data, error_ecc, |
| 262 | correct_data, correct_ecc, size); |
| 263 | err = nand_ecc_test[i].verify(error_data, error_ecc, |
| 264 | correct_data, size); |
| 265 | |
| 266 | if (err) { |
| 267 | pr_err("mtd_nandecctest: not ok - %s-%zd\n", |
| 268 | nand_ecc_test[i].name, size); |
| 269 | dump_data_ecc(error_data, error_ecc, |
| 270 | correct_data, correct_ecc, size); |
| 271 | break; |
| 272 | } |
| 273 | pr_info("mtd_nandecctest: ok - %s-%zd\n", |
| 274 | nand_ecc_test[i].name, size); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 275 | } |
Akinobu Mita | 1749c00 | 2012-09-03 22:00:01 +0900 | [diff] [blame] | 276 | error: |
| 277 | kfree(error_data); |
| 278 | kfree(error_ecc); |
| 279 | kfree(correct_data); |
| 280 | kfree(correct_ecc); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 281 | |
Akinobu Mita | 1749c00 | 2012-09-03 22:00:01 +0900 | [diff] [blame] | 282 | return err; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | #else |
| 286 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 287 | static int nand_ecc_test_run(const size_t size) |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 288 | { |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | #endif |
| 293 | |
| 294 | static int __init ecc_test_init(void) |
| 295 | { |
Akinobu Mita | f45c299 | 2012-08-26 21:06:44 +0900 | [diff] [blame] | 296 | int err; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 297 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 298 | err = nand_ecc_test_run(256); |
Akinobu Mita | f45c299 | 2012-08-26 21:06:44 +0900 | [diff] [blame] | 299 | if (err) |
| 300 | return err; |
| 301 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame] | 302 | return nand_ecc_test_run(512); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static void __exit ecc_test_exit(void) |
| 306 | { |
| 307 | } |
| 308 | |
| 309 | module_init(ecc_test_init); |
| 310 | module_exit(ecc_test_exit); |
| 311 | |
| 312 | MODULE_DESCRIPTION("NAND ECC function test module"); |
| 313 | MODULE_AUTHOR("Akinobu Mita"); |
| 314 | MODULE_LICENSE("GPL"); |