staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / drivers / mtd / tests / subpagetest.c
blob05250a080139f19e9ba691d9650f7b4b56dcc525
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2006-2007 Nokia Corporation
5 * Test sub-page read and write on MTD device.
6 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
7 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/err.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/slab.h>
17 #include <linux/sched.h>
18 #include <linux/random.h>
20 #include "mtd_test.h"
22 static int dev = -EINVAL;
23 module_param(dev, int, S_IRUGO);
24 MODULE_PARM_DESC(dev, "MTD device number to use");
26 static struct mtd_info *mtd;
27 static unsigned char *writebuf;
28 static unsigned char *readbuf;
29 static unsigned char *bbt;
31 static int subpgsize;
32 static int bufsize;
33 static int ebcnt;
34 static int pgcnt;
35 static int errcnt;
36 static struct rnd_state rnd_state;
38 static inline void clear_data(unsigned char *buf, size_t len)
40 memset(buf, 0, len);
43 static int write_eraseblock(int ebnum)
45 size_t written;
46 int err = 0;
47 loff_t addr = (loff_t)ebnum * mtd->erasesize;
49 prandom_bytes_state(&rnd_state, writebuf, subpgsize);
50 err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
51 if (unlikely(err || written != subpgsize)) {
52 pr_err("error: write failed at %#llx\n",
53 (long long)addr);
54 if (written != subpgsize) {
55 pr_err(" write size: %#x\n", subpgsize);
56 pr_err(" written: %#zx\n", written);
58 return err ? err : -1;
61 addr += subpgsize;
63 prandom_bytes_state(&rnd_state, writebuf, subpgsize);
64 err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
65 if (unlikely(err || written != subpgsize)) {
66 pr_err("error: write failed at %#llx\n",
67 (long long)addr);
68 if (written != subpgsize) {
69 pr_err(" write size: %#x\n", subpgsize);
70 pr_err(" written: %#zx\n", written);
72 return err ? err : -1;
75 return err;
78 static int write_eraseblock2(int ebnum)
80 size_t written;
81 int err = 0, k;
82 loff_t addr = (loff_t)ebnum * mtd->erasesize;
84 for (k = 1; k < 33; ++k) {
85 if (addr + (subpgsize * k) > (loff_t)(ebnum + 1) * mtd->erasesize)
86 break;
87 prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
88 err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf);
89 if (unlikely(err || written != subpgsize * k)) {
90 pr_err("error: write failed at %#llx\n",
91 (long long)addr);
92 if (written != subpgsize * k) {
93 pr_err(" write size: %#x\n",
94 subpgsize * k);
95 pr_err(" written: %#08zx\n",
96 written);
98 return err ? err : -1;
100 addr += subpgsize * k;
103 return err;
106 static void print_subpage(unsigned char *p)
108 int i, j;
110 for (i = 0; i < subpgsize; ) {
111 for (j = 0; i < subpgsize && j < 32; ++i, ++j)
112 printk("%02x", *p++);
113 printk("\n");
117 static int verify_eraseblock(int ebnum)
119 size_t read;
120 int err = 0;
121 loff_t addr = (loff_t)ebnum * mtd->erasesize;
123 prandom_bytes_state(&rnd_state, writebuf, subpgsize);
124 clear_data(readbuf, subpgsize);
125 err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
126 if (unlikely(err || read != subpgsize)) {
127 if (mtd_is_bitflip(err) && read == subpgsize) {
128 pr_info("ECC correction at %#llx\n",
129 (long long)addr);
130 err = 0;
131 } else {
132 pr_err("error: read failed at %#llx\n",
133 (long long)addr);
134 return err ? err : -1;
137 if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
138 pr_err("error: verify failed at %#llx\n",
139 (long long)addr);
140 pr_info("------------- written----------------\n");
141 print_subpage(writebuf);
142 pr_info("------------- read ------------------\n");
143 print_subpage(readbuf);
144 pr_info("-------------------------------------\n");
145 errcnt += 1;
148 addr += subpgsize;
150 prandom_bytes_state(&rnd_state, writebuf, subpgsize);
151 clear_data(readbuf, subpgsize);
152 err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
153 if (unlikely(err || read != subpgsize)) {
154 if (mtd_is_bitflip(err) && read == subpgsize) {
155 pr_info("ECC correction at %#llx\n",
156 (long long)addr);
157 err = 0;
158 } else {
159 pr_err("error: read failed at %#llx\n",
160 (long long)addr);
161 return err ? err : -1;
164 if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
165 pr_info("error: verify failed at %#llx\n",
166 (long long)addr);
167 pr_info("------------- written----------------\n");
168 print_subpage(writebuf);
169 pr_info("------------- read ------------------\n");
170 print_subpage(readbuf);
171 pr_info("-------------------------------------\n");
172 errcnt += 1;
175 return err;
178 static int verify_eraseblock2(int ebnum)
180 size_t read;
181 int err = 0, k;
182 loff_t addr = (loff_t)ebnum * mtd->erasesize;
184 for (k = 1; k < 33; ++k) {
185 if (addr + (subpgsize * k) > (loff_t)(ebnum + 1) * mtd->erasesize)
186 break;
187 prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
188 clear_data(readbuf, subpgsize * k);
189 err = mtd_read(mtd, addr, subpgsize * k, &read, readbuf);
190 if (unlikely(err || read != subpgsize * k)) {
191 if (mtd_is_bitflip(err) && read == subpgsize * k) {
192 pr_info("ECC correction at %#llx\n",
193 (long long)addr);
194 err = 0;
195 } else {
196 pr_err("error: read failed at "
197 "%#llx\n", (long long)addr);
198 return err ? err : -1;
201 if (unlikely(memcmp(readbuf, writebuf, subpgsize * k))) {
202 pr_err("error: verify failed at %#llx\n",
203 (long long)addr);
204 errcnt += 1;
206 addr += subpgsize * k;
209 return err;
212 static int verify_eraseblock_ff(int ebnum)
214 uint32_t j;
215 size_t read;
216 int err = 0;
217 loff_t addr = (loff_t)ebnum * mtd->erasesize;
219 memset(writebuf, 0xff, subpgsize);
220 for (j = 0; j < mtd->erasesize / subpgsize; ++j) {
221 clear_data(readbuf, subpgsize);
222 err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
223 if (unlikely(err || read != subpgsize)) {
224 if (mtd_is_bitflip(err) && read == subpgsize) {
225 pr_info("ECC correction at %#llx\n",
226 (long long)addr);
227 err = 0;
228 } else {
229 pr_err("error: read failed at "
230 "%#llx\n", (long long)addr);
231 return err ? err : -1;
234 if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
235 pr_err("error: verify 0xff failed at "
236 "%#llx\n", (long long)addr);
237 errcnt += 1;
239 addr += subpgsize;
242 return err;
245 static int verify_all_eraseblocks_ff(void)
247 int err;
248 unsigned int i;
250 pr_info("verifying all eraseblocks for 0xff\n");
251 for (i = 0; i < ebcnt; ++i) {
252 if (bbt[i])
253 continue;
254 err = verify_eraseblock_ff(i);
255 if (err)
256 return err;
257 if (i % 256 == 0)
258 pr_info("verified up to eraseblock %u\n", i);
260 err = mtdtest_relax();
261 if (err)
262 return err;
264 pr_info("verified %u eraseblocks\n", i);
265 return 0;
268 static int __init mtd_subpagetest_init(void)
270 int err = 0;
271 uint32_t i;
272 uint64_t tmp;
274 printk(KERN_INFO "\n");
275 printk(KERN_INFO "=================================================\n");
277 if (dev < 0) {
278 pr_info("Please specify a valid mtd-device via module parameter\n");
279 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
280 return -EINVAL;
283 pr_info("MTD device: %d\n", dev);
285 mtd = get_mtd_device(NULL, dev);
286 if (IS_ERR(mtd)) {
287 err = PTR_ERR(mtd);
288 pr_err("error: cannot get MTD device\n");
289 return err;
292 if (!mtd_type_is_nand(mtd)) {
293 pr_info("this test requires NAND flash\n");
294 goto out;
297 subpgsize = mtd->writesize >> mtd->subpage_sft;
298 tmp = mtd->size;
299 do_div(tmp, mtd->erasesize);
300 ebcnt = tmp;
301 pgcnt = mtd->erasesize / mtd->writesize;
303 pr_info("MTD device size %llu, eraseblock size %u, "
304 "page size %u, subpage size %u, count of eraseblocks %u, "
305 "pages per eraseblock %u, OOB size %u\n",
306 (unsigned long long)mtd->size, mtd->erasesize,
307 mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize);
309 err = -ENOMEM;
310 bufsize = subpgsize * 32;
311 writebuf = kmalloc(bufsize, GFP_KERNEL);
312 if (!writebuf)
313 goto out;
314 readbuf = kmalloc(bufsize, GFP_KERNEL);
315 if (!readbuf)
316 goto out;
317 bbt = kzalloc(ebcnt, GFP_KERNEL);
318 if (!bbt)
319 goto out;
321 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
322 if (err)
323 goto out;
325 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
326 if (err)
327 goto out;
329 pr_info("writing whole device\n");
330 prandom_seed_state(&rnd_state, 1);
331 for (i = 0; i < ebcnt; ++i) {
332 if (bbt[i])
333 continue;
334 err = write_eraseblock(i);
335 if (unlikely(err))
336 goto out;
337 if (i % 256 == 0)
338 pr_info("written up to eraseblock %u\n", i);
340 err = mtdtest_relax();
341 if (err)
342 goto out;
344 pr_info("written %u eraseblocks\n", i);
346 prandom_seed_state(&rnd_state, 1);
347 pr_info("verifying all eraseblocks\n");
348 for (i = 0; i < ebcnt; ++i) {
349 if (bbt[i])
350 continue;
351 err = verify_eraseblock(i);
352 if (unlikely(err))
353 goto out;
354 if (i % 256 == 0)
355 pr_info("verified up to eraseblock %u\n", i);
357 err = mtdtest_relax();
358 if (err)
359 goto out;
361 pr_info("verified %u eraseblocks\n", i);
363 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
364 if (err)
365 goto out;
367 err = verify_all_eraseblocks_ff();
368 if (err)
369 goto out;
371 /* Write all eraseblocks */
372 prandom_seed_state(&rnd_state, 3);
373 pr_info("writing whole device\n");
374 for (i = 0; i < ebcnt; ++i) {
375 if (bbt[i])
376 continue;
377 err = write_eraseblock2(i);
378 if (unlikely(err))
379 goto out;
380 if (i % 256 == 0)
381 pr_info("written up to eraseblock %u\n", i);
383 err = mtdtest_relax();
384 if (err)
385 goto out;
387 pr_info("written %u eraseblocks\n", i);
389 /* Check all eraseblocks */
390 prandom_seed_state(&rnd_state, 3);
391 pr_info("verifying all eraseblocks\n");
392 for (i = 0; i < ebcnt; ++i) {
393 if (bbt[i])
394 continue;
395 err = verify_eraseblock2(i);
396 if (unlikely(err))
397 goto out;
398 if (i % 256 == 0)
399 pr_info("verified up to eraseblock %u\n", i);
401 err = mtdtest_relax();
402 if (err)
403 goto out;
405 pr_info("verified %u eraseblocks\n", i);
407 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
408 if (err)
409 goto out;
411 err = verify_all_eraseblocks_ff();
412 if (err)
413 goto out;
415 pr_info("finished with %d errors\n", errcnt);
417 out:
418 kfree(bbt);
419 kfree(readbuf);
420 kfree(writebuf);
421 put_mtd_device(mtd);
422 if (err)
423 pr_info("error %d occurred\n", err);
424 printk(KERN_INFO "=================================================\n");
425 return err;
427 module_init(mtd_subpagetest_init);
429 static void __exit mtd_subpagetest_exit(void)
431 return;
433 module_exit(mtd_subpagetest_exit);
435 MODULE_DESCRIPTION("Subpage test module");
436 MODULE_AUTHOR("Adrian Hunter");
437 MODULE_LICENSE("GPL");