x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / mtd / tests / mtd_nandecctest.c
blob88b6c81cebbe68123da96775b3a42c3f96da4a11
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>
12 #include "mtd_test.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
21 * or detected.
24 #if IS_ENABLED(CONFIG_MTD_NAND)
26 struct nand_ecc_test {
27 const char *name;
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)
42 #else
43 #error "Unknown byte order"
44 #endif
46 static void single_bit_error_data(void *error_data, void *correct_data,
47 size_t size)
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,
56 size_t size)
58 unsigned int offset[2];
60 offset[0] = prandom_u32() % (size * BITS_PER_BYTE);
61 do {
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);
75 if (size == 256) {
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);
84 return offset;
87 static void single_bit_error_ecc(void *error_ecc, void *correct_ecc,
88 size_t size)
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,
97 size_t size)
99 unsigned int offset[2];
101 offset[0] = random_ecc_bit(size);
102 do {
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];
122 int ret;
124 __nand_calculate_ecc(error_data, size, calc_ecc);
125 ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size);
126 if (ret == 0 && !memcmp(correct_data, error_data, size))
127 return 0;
129 return -EINVAL;
132 static void single_bit_error_in_data(void *error_data, void *error_ecc,
133 void *correct_data, void *correct_ecc, const size_t size)
135 single_bit_error_data(error_data, correct_data, size);
136 memcpy(error_ecc, correct_ecc, 3);
139 static void single_bit_error_in_ecc(void *error_data, void *error_ecc,
140 void *correct_data, void *correct_ecc, const size_t size)
142 memcpy(error_data, correct_data, size);
143 single_bit_error_ecc(error_ecc, correct_ecc, size);
146 static int single_bit_error_correct(void *error_data, void *error_ecc,
147 void *correct_data, const size_t size)
149 unsigned char calc_ecc[3];
150 int ret;
152 __nand_calculate_ecc(error_data, size, calc_ecc);
153 ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size);
154 if (ret == 1 && !memcmp(correct_data, error_data, size))
155 return 0;
157 return -EINVAL;
160 static void double_bit_error_in_data(void *error_data, void *error_ecc,
161 void *correct_data, void *correct_ecc, const size_t size)
163 double_bit_error_data(error_data, correct_data, size);
164 memcpy(error_ecc, correct_ecc, 3);
167 static void single_bit_error_in_data_and_ecc(void *error_data, void *error_ecc,
168 void *correct_data, void *correct_ecc, const size_t size)
170 single_bit_error_data(error_data, correct_data, size);
171 single_bit_error_ecc(error_ecc, correct_ecc, size);
174 static void double_bit_error_in_ecc(void *error_data, void *error_ecc,
175 void *correct_data, void *correct_ecc, const size_t size)
177 memcpy(error_data, correct_data, size);
178 double_bit_error_ecc(error_ecc, correct_ecc, size);
181 static int double_bit_error_detect(void *error_data, void *error_ecc,
182 void *correct_data, const size_t size)
184 unsigned char calc_ecc[3];
185 int ret;
187 __nand_calculate_ecc(error_data, size, calc_ecc);
188 ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size);
190 return (ret == -EBADMSG) ? 0 : -EINVAL;
193 static const struct nand_ecc_test nand_ecc_test[] = {
195 .name = "no-bit-error",
196 .prepare = no_bit_error,
197 .verify = no_bit_error_verify,
200 .name = "single-bit-error-in-data-correct",
201 .prepare = single_bit_error_in_data,
202 .verify = single_bit_error_correct,
205 .name = "single-bit-error-in-ecc-correct",
206 .prepare = single_bit_error_in_ecc,
207 .verify = single_bit_error_correct,
210 .name = "double-bit-error-in-data-detect",
211 .prepare = double_bit_error_in_data,
212 .verify = double_bit_error_detect,
215 .name = "single-bit-error-in-data-and-ecc-detect",
216 .prepare = single_bit_error_in_data_and_ecc,
217 .verify = double_bit_error_detect,
220 .name = "double-bit-error-in-ecc-detect",
221 .prepare = double_bit_error_in_ecc,
222 .verify = double_bit_error_detect,
226 static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data,
227 void *correct_ecc, const size_t size)
229 pr_info("hexdump of error data:\n");
230 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
231 error_data, size, false);
232 print_hex_dump(KERN_INFO, "hexdump of error ecc: ",
233 DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false);
235 pr_info("hexdump of correct data:\n");
236 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
237 correct_data, size, false);
238 print_hex_dump(KERN_INFO, "hexdump of correct ecc: ",
239 DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false);
242 static int nand_ecc_test_run(const size_t size)
244 int i;
245 int err = 0;
246 void *error_data;
247 void *error_ecc;
248 void *correct_data;
249 void *correct_ecc;
251 error_data = kmalloc(size, GFP_KERNEL);
252 error_ecc = kmalloc(3, GFP_KERNEL);
253 correct_data = kmalloc(size, GFP_KERNEL);
254 correct_ecc = kmalloc(3, GFP_KERNEL);
256 if (!error_data || !error_ecc || !correct_data || !correct_ecc) {
257 err = -ENOMEM;
258 goto error;
261 prandom_bytes(correct_data, size);
262 __nand_calculate_ecc(correct_data, size, correct_ecc);
264 for (i = 0; i < ARRAY_SIZE(nand_ecc_test); i++) {
265 nand_ecc_test[i].prepare(error_data, error_ecc,
266 correct_data, correct_ecc, size);
267 err = nand_ecc_test[i].verify(error_data, error_ecc,
268 correct_data, size);
270 if (err) {
271 pr_err("not ok - %s-%zd\n",
272 nand_ecc_test[i].name, size);
273 dump_data_ecc(error_data, error_ecc,
274 correct_data, correct_ecc, size);
275 break;
277 pr_info("ok - %s-%zd\n",
278 nand_ecc_test[i].name, size);
280 err = mtdtest_relax();
281 if (err)
282 break;
284 error:
285 kfree(error_data);
286 kfree(error_ecc);
287 kfree(correct_data);
288 kfree(correct_ecc);
290 return err;
293 #else
295 static int nand_ecc_test_run(const size_t size)
297 return 0;
300 #endif
302 static int __init ecc_test_init(void)
304 int err;
306 err = nand_ecc_test_run(256);
307 if (err)
308 return err;
310 return nand_ecc_test_run(512);
313 static void __exit ecc_test_exit(void)
317 module_init(ecc_test_init);
318 module_exit(ecc_test_exit);
320 MODULE_DESCRIPTION("NAND ECC function test module");
321 MODULE_AUTHOR("Akinobu Mita");
322 MODULE_LICENSE("GPL");