blob: 204f796ed3e7b3b38d5a905ad6bcaf97c0bf584b [file] [log] [blame]
Akinobu Mita7126bd82009-10-22 16:53:33 +09001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/list.h>
Akinobu Mita7126bd82009-10-22 16:53:33 +09004#include <linux/random.h>
5#include <linux/string.h>
6#include <linux/bitops.h>
Akinobu Mita1749c002012-09-03 22:00:01 +09007#include <linux/slab.h>
Akinobu Mita7126bd82009-10-22 16:53:33 +09008#include <linux/mtd/nand_ecc.h>
9
Akinobu Mita6060fb42012-09-08 01:48:07 +090010/*
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 Mita7126bd82009-10-22 16:53:33 +090020#if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE)
21
Akinobu Mita6060fb42012-09-08 01:48:07 +090022struct 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 Mitac092b432012-09-08 01:48:06 +090028/*
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 Mita6060fb42012-09-08 01:48:07 +090042static void single_bit_error_data(void *error_data, void *correct_data,
43 size_t size)
Akinobu Mita7126bd82009-10-22 16:53:33 +090044{
Akinobu Mitac092b432012-09-08 01:48:06 +090045 unsigned int offset = random32() % (size * BITS_PER_BYTE);
Akinobu Mita7126bd82009-10-22 16:53:33 +090046
Akinobu Mita6060fb42012-09-08 01:48:07 +090047 memcpy(error_data, correct_data, size);
48 __change_bit_le(offset, error_data);
Akinobu Mita7126bd82009-10-22 16:53:33 +090049}
50
Akinobu Mita6060fb42012-09-08 01:48:07 +090051static 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
58static 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
72static 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 Mitac5b83842012-09-03 22:00:00 +090080static 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 Mita6060fb42012-09-08 01:48:07 +090096static int nand_ecc_test_run(const size_t size)
Akinobu Mita7126bd82009-10-22 16:53:33 +090097{
Akinobu Mita6060fb42012-09-08 01:48:07 +090098 int i;
Akinobu Mita1749c002012-09-03 22:00:01 +090099 int err = 0;
100 void *error_data;
101 void *error_ecc;
102 void *correct_data;
103 void *correct_ecc;
Akinobu Mita7126bd82009-10-22 16:53:33 +0900104
Akinobu Mita1749c002012-09-03 22:00:01 +0900105 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 Mita7126bd82009-10-22 16:53:33 +0900114
Akinobu Mitac5b83842012-09-03 22:00:00 +0900115 get_random_bytes(correct_data, size);
Akinobu Mitac5b83842012-09-03 22:00:00 +0900116 __nand_calculate_ecc(correct_data, size, correct_ecc);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900117
Akinobu Mita6060fb42012-09-08 01:48:07 +0900118 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 Mita7126bd82009-10-22 16:53:33 +0900133 }
Akinobu Mita1749c002012-09-03 22:00:01 +0900134error:
135 kfree(error_data);
136 kfree(error_ecc);
137 kfree(correct_data);
138 kfree(correct_ecc);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900139
Akinobu Mita1749c002012-09-03 22:00:01 +0900140 return err;
Akinobu Mita7126bd82009-10-22 16:53:33 +0900141}
142
143#else
144
Akinobu Mita6060fb42012-09-08 01:48:07 +0900145static int nand_ecc_test_run(const size_t size)
Akinobu Mita7126bd82009-10-22 16:53:33 +0900146{
147 return 0;
148}
149
150#endif
151
152static int __init ecc_test_init(void)
153{
Akinobu Mitaf45c2992012-08-26 21:06:44 +0900154 int err;
Akinobu Mita7126bd82009-10-22 16:53:33 +0900155
Akinobu Mita6060fb42012-09-08 01:48:07 +0900156 err = nand_ecc_test_run(256);
Akinobu Mitaf45c2992012-08-26 21:06:44 +0900157 if (err)
158 return err;
159
Akinobu Mita6060fb42012-09-08 01:48:07 +0900160 return nand_ecc_test_run(512);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900161}
162
163static void __exit ecc_test_exit(void)
164{
165}
166
167module_init(ecc_test_init);
168module_exit(ecc_test_exit);
169
170MODULE_DESCRIPTION("NAND ECC function test module");
171MODULE_AUTHOR("Akinobu Mita");
172MODULE_LICENSE("GPL");