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 | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame^] | 51 | static void single_bit_error_in_data(void *error_data, void *error_ecc, |
| 52 | void *correct_data, void *correct_ecc, const size_t size) |
| 53 | { |
| 54 | single_bit_error_data(error_data, correct_data, size); |
| 55 | memcpy(error_ecc, correct_ecc, 3); |
| 56 | } |
| 57 | |
| 58 | static int single_bit_error_correct(void *error_data, void *error_ecc, |
| 59 | void *correct_data, const size_t size) |
| 60 | { |
| 61 | unsigned char calc_ecc[3]; |
| 62 | int ret; |
| 63 | |
| 64 | __nand_calculate_ecc(error_data, size, calc_ecc); |
| 65 | ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size); |
| 66 | if (ret == 1 && !memcmp(correct_data, error_data, size)) |
| 67 | return 0; |
| 68 | |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
| 72 | static const struct nand_ecc_test nand_ecc_test[] = { |
| 73 | { |
| 74 | .name = "single-bit-error-in-data-correct", |
| 75 | .prepare = single_bit_error_in_data, |
| 76 | .verify = single_bit_error_correct, |
| 77 | }, |
| 78 | }; |
| 79 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame] | 80 | static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data, |
| 81 | void *correct_ecc, const size_t size) |
| 82 | { |
| 83 | pr_info("hexdump of error data:\n"); |
| 84 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4, |
| 85 | error_data, size, false); |
| 86 | print_hex_dump(KERN_INFO, "hexdump of error ecc: ", |
| 87 | DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false); |
| 88 | |
| 89 | pr_info("hexdump of correct data:\n"); |
| 90 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4, |
| 91 | correct_data, size, false); |
| 92 | print_hex_dump(KERN_INFO, "hexdump of correct ecc: ", |
| 93 | DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false); |
| 94 | } |
| 95 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame^] | 96 | static int nand_ecc_test_run(const size_t size) |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 97 | { |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame^] | 98 | int i; |
Akinobu Mita | 1749c00 | 2012-09-03 22:00:01 +0900 | [diff] [blame] | 99 | int err = 0; |
| 100 | void *error_data; |
| 101 | void *error_ecc; |
| 102 | void *correct_data; |
| 103 | void *correct_ecc; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 104 | |
Akinobu Mita | 1749c00 | 2012-09-03 22:00:01 +0900 | [diff] [blame] | 105 | error_data = kmalloc(size, GFP_KERNEL); |
| 106 | error_ecc = kmalloc(3, GFP_KERNEL); |
| 107 | correct_data = kmalloc(size, GFP_KERNEL); |
| 108 | correct_ecc = kmalloc(3, GFP_KERNEL); |
| 109 | |
| 110 | if (!error_data || !error_ecc || !correct_data || !correct_ecc) { |
| 111 | err = -ENOMEM; |
| 112 | goto error; |
| 113 | } |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 114 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame] | 115 | get_random_bytes(correct_data, size); |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame] | 116 | __nand_calculate_ecc(correct_data, size, correct_ecc); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 117 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame^] | 118 | for (i = 0; i < ARRAY_SIZE(nand_ecc_test); i++) { |
| 119 | nand_ecc_test[i].prepare(error_data, error_ecc, |
| 120 | correct_data, correct_ecc, size); |
| 121 | err = nand_ecc_test[i].verify(error_data, error_ecc, |
| 122 | correct_data, size); |
| 123 | |
| 124 | if (err) { |
| 125 | pr_err("mtd_nandecctest: not ok - %s-%zd\n", |
| 126 | nand_ecc_test[i].name, size); |
| 127 | dump_data_ecc(error_data, error_ecc, |
| 128 | correct_data, correct_ecc, size); |
| 129 | break; |
| 130 | } |
| 131 | pr_info("mtd_nandecctest: ok - %s-%zd\n", |
| 132 | nand_ecc_test[i].name, size); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 133 | } |
Akinobu Mita | 1749c00 | 2012-09-03 22:00:01 +0900 | [diff] [blame] | 134 | error: |
| 135 | kfree(error_data); |
| 136 | kfree(error_ecc); |
| 137 | kfree(correct_data); |
| 138 | kfree(correct_ecc); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 139 | |
Akinobu Mita | 1749c00 | 2012-09-03 22:00:01 +0900 | [diff] [blame] | 140 | return err; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | #else |
| 144 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame^] | 145 | static int nand_ecc_test_run(const size_t size) |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 146 | { |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | #endif |
| 151 | |
| 152 | static int __init ecc_test_init(void) |
| 153 | { |
Akinobu Mita | f45c299 | 2012-08-26 21:06:44 +0900 | [diff] [blame] | 154 | int err; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 155 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame^] | 156 | err = nand_ecc_test_run(256); |
Akinobu Mita | f45c299 | 2012-08-26 21:06:44 +0900 | [diff] [blame] | 157 | if (err) |
| 158 | return err; |
| 159 | |
Akinobu Mita | 6060fb4 | 2012-09-08 01:48:07 +0900 | [diff] [blame^] | 160 | return nand_ecc_test_run(512); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void __exit ecc_test_exit(void) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | module_init(ecc_test_init); |
| 168 | module_exit(ecc_test_exit); |
| 169 | |
| 170 | MODULE_DESCRIPTION("NAND ECC function test module"); |
| 171 | MODULE_AUTHOR("Akinobu Mita"); |
| 172 | MODULE_LICENSE("GPL"); |