1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/list.h>
6 #include <linux/random.h>
7 #include <linux/string.h>
8 #include <linux/bitops.h>
9 #include <linux/slab.h>
10 #include <linux/mtd/nand_ecc.h>
15 * Test the implementation for software ECC
17 * No actual MTD device is needed, So we don't need to warry about losing
18 * important data by human error.
20 * This covers possible patterns of corruption which can be reliably corrected
24 #if IS_ENABLED(CONFIG_MTD_NAND)
26 struct nand_ecc_test
{
28 void (*prepare
)(void *, void *, void *, void *, const size_t);
29 int (*verify
)(void *, void *, void *, const size_t);
33 * The reason for this __change_bit_le() instead of __change_bit() is to inject
34 * bit error properly within the region which is not a multiple of
35 * sizeof(unsigned long) on big-endian systems
37 #ifdef __LITTLE_ENDIAN
38 #define __change_bit_le(nr, addr) __change_bit(nr, addr)
39 #elif defined(__BIG_ENDIAN)
40 #define __change_bit_le(nr, addr) \
41 __change_bit((nr) ^ ((BITS_PER_LONG - 1) & ~0x7), addr)
43 #error "Unknown byte order"
46 static void single_bit_error_data(void *error_data
, void *correct_data
,
49 unsigned int offset
= prandom_u32() % (size
* BITS_PER_BYTE
);
51 memcpy(error_data
, correct_data
, size
);
52 __change_bit_le(offset
, error_data
);
55 static void double_bit_error_data(void *error_data
, void *correct_data
,
58 unsigned int offset
[2];
60 offset
[0] = prandom_u32() % (size
* BITS_PER_BYTE
);
62 offset
[1] = prandom_u32() % (size
* BITS_PER_BYTE
);
63 } while (offset
[0] == offset
[1]);
65 memcpy(error_data
, correct_data
, size
);
67 __change_bit_le(offset
[0], error_data
);
68 __change_bit_le(offset
[1], error_data
);
71 static unsigned int random_ecc_bit(size_t size
)
73 unsigned int offset
= prandom_u32() % (3 * BITS_PER_BYTE
);
77 * Don't inject a bit error into the insignificant bits (16th
78 * and 17th bit) in ECC code for 256 byte data block
80 while (offset
== 16 || offset
== 17)
81 offset
= prandom_u32() % (3 * BITS_PER_BYTE
);
87 static void single_bit_error_ecc(void *error_ecc
, void *correct_ecc
,
90 unsigned int offset
= random_ecc_bit(size
);
92 memcpy(error_ecc
, correct_ecc
, 3);
93 __change_bit_le(offset
, error_ecc
);
96 static void double_bit_error_ecc(void *error_ecc
, void *correct_ecc
,
99 unsigned int offset
[2];
101 offset
[0] = random_ecc_bit(size
);
103 offset
[1] = random_ecc_bit(size
);
104 } while (offset
[0] == offset
[1]);
106 memcpy(error_ecc
, correct_ecc
, 3);
107 __change_bit_le(offset
[0], error_ecc
);
108 __change_bit_le(offset
[1], error_ecc
);
111 static void no_bit_error(void *error_data
, void *error_ecc
,
112 void *correct_data
, void *correct_ecc
, const size_t size
)
114 memcpy(error_data
, correct_data
, size
);
115 memcpy(error_ecc
, correct_ecc
, 3);
118 static int no_bit_error_verify(void *error_data
, void *error_ecc
,
119 void *correct_data
, const size_t size
)
121 unsigned char calc_ecc
[3];
124 __nand_calculate_ecc(error_data
, size
, calc_ecc
,
125 IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC
));
126 ret
= __nand_correct_data(error_data
, error_ecc
, calc_ecc
, size
,
127 IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC
));
128 if (ret
== 0 && !memcmp(correct_data
, error_data
, size
))
134 static void single_bit_error_in_data(void *error_data
, void *error_ecc
,
135 void *correct_data
, void *correct_ecc
, const size_t size
)
137 single_bit_error_data(error_data
, correct_data
, size
);
138 memcpy(error_ecc
, correct_ecc
, 3);
141 static void single_bit_error_in_ecc(void *error_data
, void *error_ecc
,
142 void *correct_data
, void *correct_ecc
, const size_t size
)
144 memcpy(error_data
, correct_data
, size
);
145 single_bit_error_ecc(error_ecc
, correct_ecc
, size
);
148 static int single_bit_error_correct(void *error_data
, void *error_ecc
,
149 void *correct_data
, const size_t size
)
151 unsigned char calc_ecc
[3];
154 __nand_calculate_ecc(error_data
, size
, calc_ecc
,
155 IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC
));
156 ret
= __nand_correct_data(error_data
, error_ecc
, calc_ecc
, size
,
157 IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC
));
158 if (ret
== 1 && !memcmp(correct_data
, error_data
, size
))
164 static void double_bit_error_in_data(void *error_data
, void *error_ecc
,
165 void *correct_data
, void *correct_ecc
, const size_t size
)
167 double_bit_error_data(error_data
, correct_data
, size
);
168 memcpy(error_ecc
, correct_ecc
, 3);
171 static void single_bit_error_in_data_and_ecc(void *error_data
, void *error_ecc
,
172 void *correct_data
, void *correct_ecc
, const size_t size
)
174 single_bit_error_data(error_data
, correct_data
, size
);
175 single_bit_error_ecc(error_ecc
, correct_ecc
, size
);
178 static void double_bit_error_in_ecc(void *error_data
, void *error_ecc
,
179 void *correct_data
, void *correct_ecc
, const size_t size
)
181 memcpy(error_data
, correct_data
, size
);
182 double_bit_error_ecc(error_ecc
, correct_ecc
, size
);
185 static int double_bit_error_detect(void *error_data
, void *error_ecc
,
186 void *correct_data
, const size_t size
)
188 unsigned char calc_ecc
[3];
191 __nand_calculate_ecc(error_data
, size
, calc_ecc
,
192 IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC
));
193 ret
= __nand_correct_data(error_data
, error_ecc
, calc_ecc
, size
,
194 IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC
));
196 return (ret
== -EBADMSG
) ? 0 : -EINVAL
;
199 static const struct nand_ecc_test nand_ecc_test
[] = {
201 .name
= "no-bit-error",
202 .prepare
= no_bit_error
,
203 .verify
= no_bit_error_verify
,
206 .name
= "single-bit-error-in-data-correct",
207 .prepare
= single_bit_error_in_data
,
208 .verify
= single_bit_error_correct
,
211 .name
= "single-bit-error-in-ecc-correct",
212 .prepare
= single_bit_error_in_ecc
,
213 .verify
= single_bit_error_correct
,
216 .name
= "double-bit-error-in-data-detect",
217 .prepare
= double_bit_error_in_data
,
218 .verify
= double_bit_error_detect
,
221 .name
= "single-bit-error-in-data-and-ecc-detect",
222 .prepare
= single_bit_error_in_data_and_ecc
,
223 .verify
= double_bit_error_detect
,
226 .name
= "double-bit-error-in-ecc-detect",
227 .prepare
= double_bit_error_in_ecc
,
228 .verify
= double_bit_error_detect
,
232 static void dump_data_ecc(void *error_data
, void *error_ecc
, void *correct_data
,
233 void *correct_ecc
, const size_t size
)
235 pr_info("hexdump of error data:\n");
236 print_hex_dump(KERN_INFO
, "", DUMP_PREFIX_OFFSET
, 16, 4,
237 error_data
, size
, false);
238 print_hex_dump(KERN_INFO
, "hexdump of error ecc: ",
239 DUMP_PREFIX_NONE
, 16, 1, error_ecc
, 3, false);
241 pr_info("hexdump of correct data:\n");
242 print_hex_dump(KERN_INFO
, "", DUMP_PREFIX_OFFSET
, 16, 4,
243 correct_data
, size
, false);
244 print_hex_dump(KERN_INFO
, "hexdump of correct ecc: ",
245 DUMP_PREFIX_NONE
, 16, 1, correct_ecc
, 3, false);
248 static int nand_ecc_test_run(const size_t size
)
257 error_data
= kmalloc(size
, GFP_KERNEL
);
258 error_ecc
= kmalloc(3, GFP_KERNEL
);
259 correct_data
= kmalloc(size
, GFP_KERNEL
);
260 correct_ecc
= kmalloc(3, GFP_KERNEL
);
262 if (!error_data
|| !error_ecc
|| !correct_data
|| !correct_ecc
) {
267 prandom_bytes(correct_data
, size
);
268 __nand_calculate_ecc(correct_data
, size
, correct_ecc
,
269 IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC
));
271 for (i
= 0; i
< ARRAY_SIZE(nand_ecc_test
); i
++) {
272 nand_ecc_test
[i
].prepare(error_data
, error_ecc
,
273 correct_data
, correct_ecc
, size
);
274 err
= nand_ecc_test
[i
].verify(error_data
, error_ecc
,
278 pr_err("not ok - %s-%zd\n",
279 nand_ecc_test
[i
].name
, size
);
280 dump_data_ecc(error_data
, error_ecc
,
281 correct_data
, correct_ecc
, size
);
284 pr_info("ok - %s-%zd\n",
285 nand_ecc_test
[i
].name
, size
);
287 err
= mtdtest_relax();
302 static int nand_ecc_test_run(const size_t size
)
309 static int __init
ecc_test_init(void)
313 err
= nand_ecc_test_run(256);
317 return nand_ecc_test_run(512);
320 static void __exit
ecc_test_exit(void)
324 module_init(ecc_test_init
);
325 module_exit(ecc_test_exit
);
327 MODULE_DESCRIPTION("NAND ECC function test module");
328 MODULE_AUTHOR("Akinobu Mita");
329 MODULE_LICENSE("GPL");