xref: /openbmc/linux/drivers/mtd/tests/torturetest.c (revision 884caada)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2006-2008 Artem Bityutskiy
4  * Copyright (C) 2006-2008 Jarkko Lavinen
5  * Copyright (C) 2006-2008 Adrian Hunter
6  *
7  * Authors: Artem Bityutskiy, Jarkko Lavinen, Adria Hunter
8  *
9  * WARNING: this test program may kill your flash and your device. Do not
10  * use it unless you know what you do. Authors are not responsible for any
11  * damage caused by this program.
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/init.h>
17 #include <linux/ktime.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/err.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include "mtd_test.h"
25 
26 #define RETRIES 3
27 
28 static int eb = 8;
29 module_param(eb, int, S_IRUGO);
30 MODULE_PARM_DESC(eb, "eraseblock number within the selected MTD device");
31 
32 static int ebcnt = 32;
33 module_param(ebcnt, int, S_IRUGO);
34 MODULE_PARM_DESC(ebcnt, "number of consecutive eraseblocks to torture");
35 
36 static int pgcnt;
37 module_param(pgcnt, int, S_IRUGO);
38 MODULE_PARM_DESC(pgcnt, "number of pages per eraseblock to torture (0 => all)");
39 
40 static int dev = -EINVAL;
41 module_param(dev, int, S_IRUGO);
42 MODULE_PARM_DESC(dev, "MTD device number to use");
43 
44 static int gran = 512;
45 module_param(gran, int, S_IRUGO);
46 MODULE_PARM_DESC(gran, "how often the status information should be printed");
47 
48 static int check = 1;
49 module_param(check, int, S_IRUGO);
50 MODULE_PARM_DESC(check, "if the written data should be checked");
51 
52 static unsigned int cycles_count;
53 module_param(cycles_count, uint, S_IRUGO);
54 MODULE_PARM_DESC(cycles_count, "how many erase cycles to do "
55 			       "(infinite by default)");
56 
57 static struct mtd_info *mtd;
58 
59 /* This buffer contains 0x555555...0xAAAAAA... pattern */
60 static unsigned char *patt_5A5;
61 /* This buffer contains 0xAAAAAA...0x555555... pattern */
62 static unsigned char *patt_A5A;
63 /* This buffer contains all 0xFF bytes */
64 static unsigned char *patt_FF;
65 /* This a temporary buffer is use when checking data */
66 static unsigned char *check_buf;
67 /* How many erase cycles were done */
68 static unsigned int erase_cycles;
69 
70 static int pgsize;
71 static ktime_t start, finish;
72 
73 static void report_corrupt(unsigned char *read, unsigned char *written);
74 
75 static inline void start_timing(void)
76 {
77 	start = ktime_get();
78 }
79 
80 static inline void stop_timing(void)
81 {
82 	finish = ktime_get();
83 }
84 
85 /*
86  * Check that the contents of eraseblock number @enbum is equivalent to the
87  * @buf buffer.
88  */
89 static inline int check_eraseblock(int ebnum, unsigned char *buf)
90 {
91 	int err, retries = 0;
92 	size_t read;
93 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
94 	size_t len = mtd->erasesize;
95 
96 	if (pgcnt) {
97 		addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
98 		len = pgcnt * pgsize;
99 	}
100 
101 retry:
102 	err = mtd_read(mtd, addr, len, &read, check_buf);
103 	if (mtd_is_bitflip(err))
104 		pr_err("single bit flip occurred at EB %d "
105 		       "MTD reported that it was fixed.\n", ebnum);
106 	else if (err) {
107 		pr_err("error %d while reading EB %d, "
108 		       "read %zd\n", err, ebnum, read);
109 		return err;
110 	}
111 
112 	if (read != len) {
113 		pr_err("failed to read %zd bytes from EB %d, "
114 		       "read only %zd, but no error reported\n",
115 		       len, ebnum, read);
116 		return -EIO;
117 	}
118 
119 	if (memcmp(buf, check_buf, len)) {
120 		pr_err("read wrong data from EB %d\n", ebnum);
121 		report_corrupt(check_buf, buf);
122 
123 		if (retries++ < RETRIES) {
124 			/* Try read again */
125 			yield();
126 			pr_info("re-try reading data from EB %d\n",
127 			       ebnum);
128 			goto retry;
129 		} else {
130 			pr_info("retried %d times, still errors, "
131 			       "give-up\n", RETRIES);
132 			return -EINVAL;
133 		}
134 	}
135 
136 	if (retries != 0)
137 		pr_info("only attempt number %d was OK (!!!)\n",
138 		       retries);
139 
140 	return 0;
141 }
142 
143 static inline int write_pattern(int ebnum, void *buf)
144 {
145 	int err;
146 	size_t written;
147 	loff_t addr = (loff_t)ebnum * mtd->erasesize;
148 	size_t len = mtd->erasesize;
149 
150 	if (pgcnt) {
151 		addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
152 		len = pgcnt * pgsize;
153 	}
154 	err = mtd_write(mtd, addr, len, &written, buf);
155 	if (err) {
156 		pr_err("error %d while writing EB %d, written %zd"
157 		      " bytes\n", err, ebnum, written);
158 		return err;
159 	}
160 	if (written != len) {
161 		pr_info("written only %zd bytes of %zd, but no error"
162 		       " reported\n", written, len);
163 		return -EIO;
164 	}
165 
166 	return 0;
167 }
168 
169 static int __init tort_init(void)
170 {
171 	int err = 0, i, infinite = !cycles_count;
172 	unsigned char *bad_ebs;
173 
174 	printk(KERN_INFO "\n");
175 	printk(KERN_INFO "=================================================\n");
176 	pr_info("Warning: this program is trying to wear out your "
177 	       "flash, stop it if this is not wanted.\n");
178 
179 	if (dev < 0) {
180 		pr_info("Please specify a valid mtd-device via module parameter\n");
181 		pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
182 		return -EINVAL;
183 	}
184 
185 	pr_info("MTD device: %d\n", dev);
186 	pr_info("torture %d eraseblocks (%d-%d) of mtd%d\n",
187 	       ebcnt, eb, eb + ebcnt - 1, dev);
188 	if (pgcnt)
189 		pr_info("torturing just %d pages per eraseblock\n",
190 			pgcnt);
191 	pr_info("write verify %s\n", check ? "enabled" : "disabled");
192 
193 	mtd = get_mtd_device(NULL, dev);
194 	if (IS_ERR(mtd)) {
195 		err = PTR_ERR(mtd);
196 		pr_err("error: cannot get MTD device\n");
197 		return err;
198 	}
199 
200 	if (mtd->writesize == 1) {
201 		pr_info("not NAND flash, assume page size is 512 "
202 		       "bytes.\n");
203 		pgsize = 512;
204 	} else
205 		pgsize = mtd->writesize;
206 
207 	if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
208 		pr_err("error: invalid pgcnt value %d\n", pgcnt);
209 		goto out_mtd;
210 	}
211 
212 	err = -ENOMEM;
213 	patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
214 	if (!patt_5A5)
215 		goto out_mtd;
216 
217 	patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
218 	if (!patt_A5A)
219 		goto out_patt_5A5;
220 
221 	patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
222 	if (!patt_FF)
223 		goto out_patt_A5A;
224 
225 	check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
226 	if (!check_buf)
227 		goto out_patt_FF;
228 
229 	bad_ebs = kzalloc(ebcnt, GFP_KERNEL);
230 	if (!bad_ebs)
231 		goto out_check_buf;
232 
233 	err = 0;
234 
235 	/* Initialize patterns */
236 	memset(patt_FF, 0xFF, mtd->erasesize);
237 	for (i = 0; i < mtd->erasesize / pgsize; i++) {
238 		if (!(i & 1)) {
239 			memset(patt_5A5 + i * pgsize, 0x55, pgsize);
240 			memset(patt_A5A + i * pgsize, 0xAA, pgsize);
241 		} else {
242 			memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
243 			memset(patt_A5A + i * pgsize, 0x55, pgsize);
244 		}
245 	}
246 
247 	err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt);
248 	if (err)
249 		goto out;
250 
251 	start_timing();
252 	while (1) {
253 		int i;
254 		void *patt;
255 
256 		err = mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt);
257 		if (err)
258 			goto out;
259 
260 		/* Check if the eraseblocks contain only 0xFF bytes */
261 		if (check) {
262 			for (i = eb; i < eb + ebcnt; i++) {
263 				if (bad_ebs[i - eb])
264 					continue;
265 				err = check_eraseblock(i, patt_FF);
266 				if (err) {
267 					pr_info("verify failed"
268 					       " for 0xFF... pattern\n");
269 					goto out;
270 				}
271 
272 				err = mtdtest_relax();
273 				if (err)
274 					goto out;
275 			}
276 		}
277 
278 		/* Write the pattern */
279 		for (i = eb; i < eb + ebcnt; i++) {
280 			if (bad_ebs[i - eb])
281 				continue;
282 			if ((eb + erase_cycles) & 1)
283 				patt = patt_5A5;
284 			else
285 				patt = patt_A5A;
286 			err = write_pattern(i, patt);
287 			if (err)
288 				goto out;
289 
290 			err = mtdtest_relax();
291 			if (err)
292 				goto out;
293 		}
294 
295 		/* Verify what we wrote */
296 		if (check) {
297 			for (i = eb; i < eb + ebcnt; i++) {
298 				if (bad_ebs[i - eb])
299 					continue;
300 				if ((eb + erase_cycles) & 1)
301 					patt = patt_5A5;
302 				else
303 					patt = patt_A5A;
304 				err = check_eraseblock(i, patt);
305 				if (err) {
306 					pr_info("verify failed for %s"
307 					       " pattern\n",
308 					       ((eb + erase_cycles) & 1) ?
309 					       "0x55AA55..." : "0xAA55AA...");
310 					goto out;
311 				}
312 
313 				err = mtdtest_relax();
314 				if (err)
315 					goto out;
316 			}
317 		}
318 
319 		erase_cycles += 1;
320 
321 		if (erase_cycles % gran == 0) {
322 			long ms;
323 
324 			stop_timing();
325 			ms = ktime_ms_delta(finish, start);
326 			pr_info("%08u erase cycles done, took %lu "
327 			       "milliseconds (%lu seconds)\n",
328 			       erase_cycles, ms, ms / 1000);
329 			start_timing();
330 		}
331 
332 		if (!infinite && --cycles_count == 0)
333 			break;
334 	}
335 out:
336 
337 	pr_info("finished after %u erase cycles\n",
338 	       erase_cycles);
339 	kfree(bad_ebs);
340 out_check_buf:
341 	kfree(check_buf);
342 out_patt_FF:
343 	kfree(patt_FF);
344 out_patt_A5A:
345 	kfree(patt_A5A);
346 out_patt_5A5:
347 	kfree(patt_5A5);
348 out_mtd:
349 	put_mtd_device(mtd);
350 	if (err)
351 		pr_info("error %d occurred during torturing\n", err);
352 	printk(KERN_INFO "=================================================\n");
353 	return err;
354 }
355 module_init(tort_init);
356 
357 static void __exit tort_exit(void)
358 {
359 	return;
360 }
361 module_exit(tort_exit);
362 
363 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
364 		      unsigned offset, unsigned len, unsigned *bytesp,
365 		      unsigned *bitsp);
366 static void print_bufs(unsigned char *read, unsigned char *written, int start,
367 		       int len);
368 
369 /*
370  * Report the detailed information about how the read EB differs from what was
371  * written.
372  */
373 static void report_corrupt(unsigned char *read, unsigned char *written)
374 {
375 	int i;
376 	int bytes, bits, pages, first;
377 	int offset, len;
378 	size_t check_len = mtd->erasesize;
379 
380 	if (pgcnt)
381 		check_len = pgcnt * pgsize;
382 
383 	bytes = bits = pages = 0;
384 	for (i = 0; i < check_len; i += pgsize)
385 		if (countdiffs(written, read, i, pgsize, &bytes,
386 			       &bits) >= 0)
387 			pages++;
388 
389 	pr_info("verify fails on %d pages, %d bytes/%d bits\n",
390 	       pages, bytes, bits);
391 	pr_info("The following is a list of all differences between"
392 	       " what was read from flash and what was expected\n");
393 
394 	for (i = 0; i < check_len; i += pgsize) {
395 		cond_resched();
396 		bytes = bits = 0;
397 		first = countdiffs(written, read, i, pgsize, &bytes,
398 				   &bits);
399 		if (first < 0)
400 			continue;
401 
402 		printk("-------------------------------------------------------"
403 		       "----------------------------------\n");
404 
405 		pr_info("Page %zd has %d bytes/%d bits failing verify,"
406 		       " starting at offset 0x%x\n",
407 		       (mtd->erasesize - check_len + i) / pgsize,
408 		       bytes, bits, first);
409 
410 		offset = first & ~0x7;
411 		len = ((first + bytes) | 0x7) + 1 - offset;
412 
413 		print_bufs(read, written, offset, len);
414 	}
415 }
416 
417 static void print_bufs(unsigned char *read, unsigned char *written, int start,
418 		       int len)
419 {
420 	int i = 0, j1, j2;
421 	char *diff;
422 
423 	printk("Offset       Read                          Written\n");
424 	while (i < len) {
425 		printk("0x%08x: ", start + i);
426 		diff = "   ";
427 		for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
428 			printk(" %02x", read[start + i + j1]);
429 			if (read[start + i + j1] != written[start + i + j1])
430 				diff = "***";
431 		}
432 
433 		while (j1 < 8) {
434 			printk(" ");
435 			j1 += 1;
436 		}
437 
438 		printk("  %s ", diff);
439 
440 		for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
441 			printk(" %02x", written[start + i + j2]);
442 		printk("\n");
443 		i += 8;
444 	}
445 }
446 
447 /*
448  * Count the number of differing bytes and bits and return the first differing
449  * offset.
450  */
451 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
452 		      unsigned offset, unsigned len, unsigned *bytesp,
453 		      unsigned *bitsp)
454 {
455 	unsigned i, bit;
456 	int first = -1;
457 
458 	for (i = offset; i < offset + len; i++)
459 		if (buf[i] != check_buf[i]) {
460 			first = i;
461 			break;
462 		}
463 
464 	while (i < offset + len) {
465 		if (buf[i] != check_buf[i]) {
466 			(*bytesp)++;
467 			bit = 1;
468 			while (bit < 256) {
469 				if ((buf[i] & bit) != (check_buf[i] & bit))
470 					(*bitsp)++;
471 				bit <<= 1;
472 			}
473 		}
474 		i++;
475 	}
476 
477 	return first;
478 }
479 
480 MODULE_DESCRIPTION("Eraseblock torturing module");
481 MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
482 MODULE_LICENSE("GPL");
483