blob: b437fa425077db48d4bb76da8d343e9739e56205 [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 Mita6ed089c2012-09-08 01:48:10 +090051static 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 Mita200ab842012-09-08 01:48:09 +090067static 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
83static 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 Mita6ed089c2012-09-08 01:48:10 +090092static 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 Mitaccaa6792012-09-08 01:48:08 +0900107static 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
114static 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 Mita6060fb42012-09-08 01:48:07 +0900128static 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 Mita200ab842012-09-08 01:48:09 +0900135static 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 Mita6060fb42012-09-08 01:48:07 +0900142static 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 Mita6ed089c2012-09-08 01:48:10 +0900156static 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
163static 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
170static 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
177static 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 Mita6060fb42012-09-08 01:48:07 +0900189static const struct nand_ecc_test nand_ecc_test[] = {
190 {
Akinobu Mitaccaa6792012-09-08 01:48:08 +0900191 .name = "no-bit-error",
192 .prepare = no_bit_error,
193 .verify = no_bit_error_verify,
194 },
195 {
Akinobu Mita6060fb42012-09-08 01:48:07 +0900196 .name = "single-bit-error-in-data-correct",
197 .prepare = single_bit_error_in_data,
198 .verify = single_bit_error_correct,
199 },
Akinobu Mita200ab842012-09-08 01:48:09 +0900200 {
201 .name = "single-bit-error-in-ecc-correct",
202 .prepare = single_bit_error_in_ecc,
203 .verify = single_bit_error_correct,
204 },
Akinobu Mita6ed089c2012-09-08 01:48:10 +0900205 {
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 Mita6060fb42012-09-08 01:48:07 +0900220};
221
Akinobu Mitac5b83842012-09-03 22:00:00 +0900222static 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 Mita6060fb42012-09-08 01:48:07 +0900238static int nand_ecc_test_run(const size_t size)
Akinobu Mita7126bd82009-10-22 16:53:33 +0900239{
Akinobu Mita6060fb42012-09-08 01:48:07 +0900240 int i;
Akinobu Mita1749c002012-09-03 22:00:01 +0900241 int err = 0;
242 void *error_data;
243 void *error_ecc;
244 void *correct_data;
245 void *correct_ecc;
Akinobu Mita7126bd82009-10-22 16:53:33 +0900246
Akinobu Mita1749c002012-09-03 22:00:01 +0900247 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 Mita7126bd82009-10-22 16:53:33 +0900256
Akinobu Mitac5b83842012-09-03 22:00:00 +0900257 get_random_bytes(correct_data, size);
Akinobu Mitac5b83842012-09-03 22:00:00 +0900258 __nand_calculate_ecc(correct_data, size, correct_ecc);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900259
Akinobu Mita6060fb42012-09-08 01:48:07 +0900260 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 Mita7126bd82009-10-22 16:53:33 +0900275 }
Akinobu Mita1749c002012-09-03 22:00:01 +0900276error:
277 kfree(error_data);
278 kfree(error_ecc);
279 kfree(correct_data);
280 kfree(correct_ecc);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900281
Akinobu Mita1749c002012-09-03 22:00:01 +0900282 return err;
Akinobu Mita7126bd82009-10-22 16:53:33 +0900283}
284
285#else
286
Akinobu Mita6060fb42012-09-08 01:48:07 +0900287static int nand_ecc_test_run(const size_t size)
Akinobu Mita7126bd82009-10-22 16:53:33 +0900288{
289 return 0;
290}
291
292#endif
293
294static int __init ecc_test_init(void)
295{
Akinobu Mitaf45c2992012-08-26 21:06:44 +0900296 int err;
Akinobu Mita7126bd82009-10-22 16:53:33 +0900297
Akinobu Mita6060fb42012-09-08 01:48:07 +0900298 err = nand_ecc_test_run(256);
Akinobu Mitaf45c2992012-08-26 21:06:44 +0900299 if (err)
300 return err;
301
Akinobu Mita6060fb42012-09-08 01:48:07 +0900302 return nand_ecc_test_run(512);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900303}
304
305static void __exit ecc_test_exit(void)
306{
307}
308
309module_init(ecc_test_init);
310module_exit(ecc_test_exit);
311
312MODULE_DESCRIPTION("NAND ECC function test module");
313MODULE_AUTHOR("Akinobu Mita");
314MODULE_LICENSE("GPL");