blob: e579f9027c47d82bb1e4d577ddd13128324a0075 [file] [log] [blame]
Vikram Narayananb6489d92012-10-10 23:07:19 +05301#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
Akinobu Mita7126bd82009-10-22 16:53:33 +09003#include <linux/kernel.h>
4#include <linux/module.h>
5#include <linux/list.h>
Akinobu Mita7126bd82009-10-22 16:53:33 +09006#include <linux/random.h>
7#include <linux/string.h>
8#include <linux/bitops.h>
Akinobu Mita1749c002012-09-03 22:00:01 +09009#include <linux/slab.h>
Akinobu Mita7126bd82009-10-22 16:53:33 +090010#include <linux/mtd/nand_ecc.h>
11
Akinobu Mita6060fb42012-09-08 01:48:07 +090012/*
13 * Test the implementation for software ECC
14 *
15 * No actual MTD device is needed, So we don't need to warry about losing
16 * important data by human error.
17 *
18 * This covers possible patterns of corruption which can be reliably corrected
19 * or detected.
20 */
21
Fabio Estevam7e8eb8a2013-12-02 00:30:27 -020022#if IS_ENABLED(CONFIG_MTD_NAND)
Akinobu Mita7126bd82009-10-22 16:53:33 +090023
Akinobu Mita6060fb42012-09-08 01:48:07 +090024struct nand_ecc_test {
25 const char *name;
26 void (*prepare)(void *, void *, void *, void *, const size_t);
27 int (*verify)(void *, void *, void *, const size_t);
28};
29
Akinobu Mitac092b432012-09-08 01:48:06 +090030/*
31 * The reason for this __change_bit_le() instead of __change_bit() is to inject
32 * bit error properly within the region which is not a multiple of
33 * sizeof(unsigned long) on big-endian systems
34 */
35#ifdef __LITTLE_ENDIAN
36#define __change_bit_le(nr, addr) __change_bit(nr, addr)
37#elif defined(__BIG_ENDIAN)
38#define __change_bit_le(nr, addr) \
39 __change_bit((nr) ^ ((BITS_PER_LONG - 1) & ~0x7), addr)
40#else
41#error "Unknown byte order"
42#endif
43
Akinobu Mita6060fb42012-09-08 01:48:07 +090044static void single_bit_error_data(void *error_data, void *correct_data,
45 size_t size)
Akinobu Mita7126bd82009-10-22 16:53:33 +090046{
Akinobu Mitaaca662a2013-01-03 21:19:13 +090047 unsigned int offset = prandom_u32() % (size * BITS_PER_BYTE);
Akinobu Mita7126bd82009-10-22 16:53:33 +090048
Akinobu Mita6060fb42012-09-08 01:48:07 +090049 memcpy(error_data, correct_data, size);
50 __change_bit_le(offset, error_data);
Akinobu Mita7126bd82009-10-22 16:53:33 +090051}
52
Akinobu Mita6ed089c2012-09-08 01:48:10 +090053static void double_bit_error_data(void *error_data, void *correct_data,
54 size_t size)
55{
56 unsigned int offset[2];
57
Akinobu Mitaaca662a2013-01-03 21:19:13 +090058 offset[0] = prandom_u32() % (size * BITS_PER_BYTE);
Akinobu Mita6ed089c2012-09-08 01:48:10 +090059 do {
Akinobu Mitaaca662a2013-01-03 21:19:13 +090060 offset[1] = prandom_u32() % (size * BITS_PER_BYTE);
Akinobu Mita6ed089c2012-09-08 01:48:10 +090061 } while (offset[0] == offset[1]);
62
63 memcpy(error_data, correct_data, size);
64
65 __change_bit_le(offset[0], error_data);
66 __change_bit_le(offset[1], error_data);
67}
68
Akinobu Mita200ab842012-09-08 01:48:09 +090069static unsigned int random_ecc_bit(size_t size)
70{
Akinobu Mitaaca662a2013-01-03 21:19:13 +090071 unsigned int offset = prandom_u32() % (3 * BITS_PER_BYTE);
Akinobu Mita200ab842012-09-08 01:48:09 +090072
73 if (size == 256) {
74 /*
75 * Don't inject a bit error into the insignificant bits (16th
76 * and 17th bit) in ECC code for 256 byte data block
77 */
78 while (offset == 16 || offset == 17)
Akinobu Mitaaca662a2013-01-03 21:19:13 +090079 offset = prandom_u32() % (3 * BITS_PER_BYTE);
Akinobu Mita200ab842012-09-08 01:48:09 +090080 }
81
82 return offset;
83}
84
85static void single_bit_error_ecc(void *error_ecc, void *correct_ecc,
86 size_t size)
87{
88 unsigned int offset = random_ecc_bit(size);
89
90 memcpy(error_ecc, correct_ecc, 3);
91 __change_bit_le(offset, error_ecc);
92}
93
Akinobu Mita6ed089c2012-09-08 01:48:10 +090094static void double_bit_error_ecc(void *error_ecc, void *correct_ecc,
95 size_t size)
96{
97 unsigned int offset[2];
98
99 offset[0] = random_ecc_bit(size);
100 do {
101 offset[1] = random_ecc_bit(size);
102 } while (offset[0] == offset[1]);
103
104 memcpy(error_ecc, correct_ecc, 3);
105 __change_bit_le(offset[0], error_ecc);
106 __change_bit_le(offset[1], error_ecc);
107}
108
Akinobu Mitaccaa6792012-09-08 01:48:08 +0900109static void no_bit_error(void *error_data, void *error_ecc,
110 void *correct_data, void *correct_ecc, const size_t size)
111{
112 memcpy(error_data, correct_data, size);
113 memcpy(error_ecc, correct_ecc, 3);
114}
115
116static int no_bit_error_verify(void *error_data, void *error_ecc,
117 void *correct_data, const size_t size)
118{
119 unsigned char calc_ecc[3];
120 int ret;
121
122 __nand_calculate_ecc(error_data, size, calc_ecc);
123 ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size);
124 if (ret == 0 && !memcmp(correct_data, error_data, size))
125 return 0;
126
127 return -EINVAL;
128}
129
Akinobu Mita6060fb42012-09-08 01:48:07 +0900130static void single_bit_error_in_data(void *error_data, void *error_ecc,
131 void *correct_data, void *correct_ecc, const size_t size)
132{
133 single_bit_error_data(error_data, correct_data, size);
134 memcpy(error_ecc, correct_ecc, 3);
135}
136
Akinobu Mita200ab842012-09-08 01:48:09 +0900137static void single_bit_error_in_ecc(void *error_data, void *error_ecc,
138 void *correct_data, void *correct_ecc, const size_t size)
139{
140 memcpy(error_data, correct_data, size);
141 single_bit_error_ecc(error_ecc, correct_ecc, size);
142}
143
Akinobu Mita6060fb42012-09-08 01:48:07 +0900144static int single_bit_error_correct(void *error_data, void *error_ecc,
145 void *correct_data, const size_t size)
146{
147 unsigned char calc_ecc[3];
148 int ret;
149
150 __nand_calculate_ecc(error_data, size, calc_ecc);
151 ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size);
152 if (ret == 1 && !memcmp(correct_data, error_data, size))
153 return 0;
154
155 return -EINVAL;
156}
157
Akinobu Mita6ed089c2012-09-08 01:48:10 +0900158static void double_bit_error_in_data(void *error_data, void *error_ecc,
159 void *correct_data, void *correct_ecc, const size_t size)
160{
161 double_bit_error_data(error_data, correct_data, size);
162 memcpy(error_ecc, correct_ecc, 3);
163}
164
165static void single_bit_error_in_data_and_ecc(void *error_data, void *error_ecc,
166 void *correct_data, void *correct_ecc, const size_t size)
167{
168 single_bit_error_data(error_data, correct_data, size);
169 single_bit_error_ecc(error_ecc, correct_ecc, size);
170}
171
172static void double_bit_error_in_ecc(void *error_data, void *error_ecc,
173 void *correct_data, void *correct_ecc, const size_t size)
174{
175 memcpy(error_data, correct_data, size);
176 double_bit_error_ecc(error_ecc, correct_ecc, size);
177}
178
179static int double_bit_error_detect(void *error_data, void *error_ecc,
180 void *correct_data, const size_t size)
181{
182 unsigned char calc_ecc[3];
183 int ret;
184
185 __nand_calculate_ecc(error_data, size, calc_ecc);
186 ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size);
187
188 return (ret == -1) ? 0 : -EINVAL;
189}
190
Akinobu Mita6060fb42012-09-08 01:48:07 +0900191static const struct nand_ecc_test nand_ecc_test[] = {
192 {
Akinobu Mitaccaa6792012-09-08 01:48:08 +0900193 .name = "no-bit-error",
194 .prepare = no_bit_error,
195 .verify = no_bit_error_verify,
196 },
197 {
Akinobu Mita6060fb42012-09-08 01:48:07 +0900198 .name = "single-bit-error-in-data-correct",
199 .prepare = single_bit_error_in_data,
200 .verify = single_bit_error_correct,
201 },
Akinobu Mita200ab842012-09-08 01:48:09 +0900202 {
203 .name = "single-bit-error-in-ecc-correct",
204 .prepare = single_bit_error_in_ecc,
205 .verify = single_bit_error_correct,
206 },
Akinobu Mita6ed089c2012-09-08 01:48:10 +0900207 {
208 .name = "double-bit-error-in-data-detect",
209 .prepare = double_bit_error_in_data,
210 .verify = double_bit_error_detect,
211 },
212 {
213 .name = "single-bit-error-in-data-and-ecc-detect",
214 .prepare = single_bit_error_in_data_and_ecc,
215 .verify = double_bit_error_detect,
216 },
217 {
218 .name = "double-bit-error-in-ecc-detect",
219 .prepare = double_bit_error_in_ecc,
220 .verify = double_bit_error_detect,
221 },
Akinobu Mita6060fb42012-09-08 01:48:07 +0900222};
223
Akinobu Mitac5b83842012-09-03 22:00:00 +0900224static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data,
225 void *correct_ecc, const size_t size)
226{
227 pr_info("hexdump of error data:\n");
228 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
229 error_data, size, false);
230 print_hex_dump(KERN_INFO, "hexdump of error ecc: ",
231 DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false);
232
233 pr_info("hexdump of correct data:\n");
234 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
235 correct_data, size, false);
236 print_hex_dump(KERN_INFO, "hexdump of correct ecc: ",
237 DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false);
238}
239
Akinobu Mita6060fb42012-09-08 01:48:07 +0900240static int nand_ecc_test_run(const size_t size)
Akinobu Mita7126bd82009-10-22 16:53:33 +0900241{
Akinobu Mita6060fb42012-09-08 01:48:07 +0900242 int i;
Akinobu Mita1749c002012-09-03 22:00:01 +0900243 int err = 0;
244 void *error_data;
245 void *error_ecc;
246 void *correct_data;
247 void *correct_ecc;
Akinobu Mita7126bd82009-10-22 16:53:33 +0900248
Akinobu Mita1749c002012-09-03 22:00:01 +0900249 error_data = kmalloc(size, GFP_KERNEL);
250 error_ecc = kmalloc(3, GFP_KERNEL);
251 correct_data = kmalloc(size, GFP_KERNEL);
252 correct_ecc = kmalloc(3, GFP_KERNEL);
253
254 if (!error_data || !error_ecc || !correct_data || !correct_ecc) {
255 err = -ENOMEM;
256 goto error;
257 }
Akinobu Mita7126bd82009-10-22 16:53:33 +0900258
Akinobu Mita459a86d2013-02-27 17:05:31 -0800259 prandom_bytes(correct_data, size);
Akinobu Mitac5b83842012-09-03 22:00:00 +0900260 __nand_calculate_ecc(correct_data, size, correct_ecc);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900261
Akinobu Mita6060fb42012-09-08 01:48:07 +0900262 for (i = 0; i < ARRAY_SIZE(nand_ecc_test); i++) {
263 nand_ecc_test[i].prepare(error_data, error_ecc,
264 correct_data, correct_ecc, size);
265 err = nand_ecc_test[i].verify(error_data, error_ecc,
266 correct_data, size);
267
268 if (err) {
Vikram Narayananb6489d92012-10-10 23:07:19 +0530269 pr_err("not ok - %s-%zd\n",
Akinobu Mita6060fb42012-09-08 01:48:07 +0900270 nand_ecc_test[i].name, size);
271 dump_data_ecc(error_data, error_ecc,
272 correct_data, correct_ecc, size);
273 break;
274 }
Vikram Narayananb6489d92012-10-10 23:07:19 +0530275 pr_info("ok - %s-%zd\n",
Akinobu Mita6060fb42012-09-08 01:48:07 +0900276 nand_ecc_test[i].name, size);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900277 }
Akinobu Mita1749c002012-09-03 22:00:01 +0900278error:
279 kfree(error_data);
280 kfree(error_ecc);
281 kfree(correct_data);
282 kfree(correct_ecc);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900283
Akinobu Mita1749c002012-09-03 22:00:01 +0900284 return err;
Akinobu Mita7126bd82009-10-22 16:53:33 +0900285}
286
287#else
288
Akinobu Mita6060fb42012-09-08 01:48:07 +0900289static int nand_ecc_test_run(const size_t size)
Akinobu Mita7126bd82009-10-22 16:53:33 +0900290{
291 return 0;
292}
293
294#endif
295
296static int __init ecc_test_init(void)
297{
Akinobu Mitaf45c2992012-08-26 21:06:44 +0900298 int err;
Akinobu Mita7126bd82009-10-22 16:53:33 +0900299
Akinobu Mita6060fb42012-09-08 01:48:07 +0900300 err = nand_ecc_test_run(256);
Akinobu Mitaf45c2992012-08-26 21:06:44 +0900301 if (err)
302 return err;
303
Akinobu Mita6060fb42012-09-08 01:48:07 +0900304 return nand_ecc_test_run(512);
Akinobu Mita7126bd82009-10-22 16:53:33 +0900305}
306
307static void __exit ecc_test_exit(void)
308{
309}
310
311module_init(ecc_test_init);
312module_exit(ecc_test_exit);
313
314MODULE_DESCRIPTION("NAND ECC function test module");
315MODULE_AUTHOR("Akinobu Mita");
316MODULE_LICENSE("GPL");