blob: e509f8aa9a7eea21db08004a8eca1608880ac7d1 [file] [log] [blame]
Artem Bityutskiy7163cea2008-12-08 13:36:47 +02001/*
2 * Copyright (C) 2006-2008 Nokia Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; see the file COPYING. If not, write to the Free Software
15 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 *
17 * Test random reads, writes and erases on MTD device.
18 *
19 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
20 */
21
Vikram Narayananae0086c2012-10-10 23:10:22 +053022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020024#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/err.h>
28#include <linux/mtd/mtd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020030#include <linux/sched.h>
31#include <linux/vmalloc.h>
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030032#include <linux/random.h>
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020033
Akinobu Mita5a78df62013-08-03 18:52:13 +090034#include "mtd_test.h"
35
Wolfram Sang74060602011-10-30 00:11:53 +020036static int dev = -EINVAL;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020037module_param(dev, int, S_IRUGO);
38MODULE_PARM_DESC(dev, "MTD device number to use");
39
40static int count = 10000;
41module_param(count, int, S_IRUGO);
42MODULE_PARM_DESC(count, "Number of operations to do (default is 10000)");
43
44static struct mtd_info *mtd;
45static unsigned char *writebuf;
46static unsigned char *readbuf;
47static unsigned char *bbt;
48static int *offsets;
49
50static int pgsize;
51static int bufsize;
52static int ebcnt;
53static int pgcnt;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020054
55static int rand_eb(void)
56{
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030057 unsigned int eb;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020058
59again:
Akinobu Mitaaca662a2013-01-03 21:19:13 +090060 eb = prandom_u32();
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020061 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
62 eb %= (ebcnt - 1);
63 if (bbt[eb])
64 goto again;
65 return eb;
66}
67
68static int rand_offs(void)
69{
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030070 unsigned int offs;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020071
Akinobu Mitaaca662a2013-01-03 21:19:13 +090072 offs = prandom_u32();
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020073 offs %= bufsize;
74 return offs;
75}
76
77static int rand_len(int offs)
78{
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030079 unsigned int len;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020080
Akinobu Mitaaca662a2013-01-03 21:19:13 +090081 len = prandom_u32();
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020082 len %= (bufsize - offs);
83 return len;
84}
85
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020086static int do_read(void)
87{
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020088 int eb = rand_eb();
89 int offs = rand_offs();
Akinobu Mitaabc173a2013-08-15 22:55:08 +090090 int len = rand_len(offs);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020091 loff_t addr;
92
93 if (bbt[eb + 1]) {
94 if (offs >= mtd->erasesize)
95 offs -= mtd->erasesize;
96 if (offs + len > mtd->erasesize)
97 len = mtd->erasesize - offs;
98 }
Brian Norrisb9da8ba2015-02-28 02:02:26 -080099 addr = (loff_t)eb * mtd->erasesize + offs;
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900100 return mtdtest_read(mtd, addr, len, readbuf);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200101}
102
103static int do_write(void)
104{
105 int eb = rand_eb(), offs, err, len;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200106 loff_t addr;
107
108 offs = offsets[eb];
109 if (offs >= mtd->erasesize) {
Akinobu Mita5a78df62013-08-03 18:52:13 +0900110 err = mtdtest_erase_eraseblock(mtd, eb);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200111 if (err)
112 return err;
113 offs = offsets[eb] = 0;
114 }
115 len = rand_len(offs);
116 len = ((len + pgsize - 1) / pgsize) * pgsize;
117 if (offs + len > mtd->erasesize) {
118 if (bbt[eb + 1])
119 len = mtd->erasesize - offs;
120 else {
Akinobu Mita5a78df62013-08-03 18:52:13 +0900121 err = mtdtest_erase_eraseblock(mtd, eb + 1);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200122 if (err)
123 return err;
124 offsets[eb + 1] = 0;
125 }
126 }
Brian Norrisb9da8ba2015-02-28 02:02:26 -0800127 addr = (loff_t)eb * mtd->erasesize + offs;
Akinobu Mita5a78df62013-08-03 18:52:13 +0900128 err = mtdtest_write(mtd, addr, len, writebuf);
Akinobu Mita8a9f4aa2013-08-15 22:55:09 +0900129 if (unlikely(err))
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200130 return err;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200131 offs += len;
132 while (offs > mtd->erasesize) {
133 offsets[eb++] = mtd->erasesize;
134 offs -= mtd->erasesize;
135 }
136 offsets[eb] = offs;
137 return 0;
138}
139
140static int do_operation(void)
141{
Akinobu Mitaaca662a2013-01-03 21:19:13 +0900142 if (prandom_u32() & 1)
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200143 return do_read();
144 else
145 return do_write();
146}
147
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200148static int __init mtd_stresstest_init(void)
149{
150 int err;
151 int i, op;
152 uint64_t tmp;
153
154 printk(KERN_INFO "\n");
155 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200156
157 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900158 pr_info("Please specify a valid mtd-device via module parameter\n");
Vikram Narayananae0086c2012-10-10 23:10:22 +0530159 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200160 return -EINVAL;
161 }
162
Vikram Narayananae0086c2012-10-10 23:10:22 +0530163 pr_info("MTD device: %d\n", dev);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200164
165 mtd = get_mtd_device(NULL, dev);
166 if (IS_ERR(mtd)) {
167 err = PTR_ERR(mtd);
Vikram Narayananae0086c2012-10-10 23:10:22 +0530168 pr_err("error: cannot get MTD device\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200169 return err;
170 }
171
172 if (mtd->writesize == 1) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530173 pr_info("not NAND flash, assume page size is 512 "
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200174 "bytes.\n");
175 pgsize = 512;
176 } else
177 pgsize = mtd->writesize;
178
179 tmp = mtd->size;
180 do_div(tmp, mtd->erasesize);
181 ebcnt = tmp;
Morten Thunberg Svendsenf5e2bae2010-01-06 10:48:18 +0100182 pgcnt = mtd->erasesize / pgsize;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200183
Vikram Narayananae0086c2012-10-10 23:10:22 +0530184 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200185 "page size %u, count of eraseblocks %u, pages per "
186 "eraseblock %u, OOB size %u\n",
187 (unsigned long long)mtd->size, mtd->erasesize,
188 pgsize, ebcnt, pgcnt, mtd->oobsize);
189
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100190 if (ebcnt < 2) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530191 pr_err("error: need at least 2 eraseblocks\n");
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100192 err = -ENOSPC;
193 goto out_put_mtd;
194 }
195
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200196 /* Read or write up 2 eraseblocks at a time */
197 bufsize = mtd->erasesize * 2;
198
199 err = -ENOMEM;
200 readbuf = vmalloc(bufsize);
201 writebuf = vmalloc(bufsize);
202 offsets = kmalloc(ebcnt * sizeof(int), GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700203 if (!readbuf || !writebuf || !offsets)
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200204 goto out;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200205 for (i = 0; i < ebcnt; i++)
206 offsets[i] = mtd->erasesize;
Akinobu Mita4debec72013-02-27 17:05:40 -0800207 prandom_bytes(writebuf, bufsize);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200208
Akinobu Mita5a78df62013-08-03 18:52:13 +0900209 bbt = kzalloc(ebcnt, GFP_KERNEL);
210 if (!bbt)
211 goto out;
212 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200213 if (err)
214 goto out;
215
216 /* Do operations */
Vikram Narayananae0086c2012-10-10 23:10:22 +0530217 pr_info("doing operations\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200218 for (op = 0; op < count; op++) {
219 if ((op & 1023) == 0)
Vikram Narayananae0086c2012-10-10 23:10:22 +0530220 pr_info("%d operations done\n", op);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200221 err = do_operation();
222 if (err)
223 goto out;
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200224
225 err = mtdtest_relax();
226 if (err)
227 goto out;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200228 }
Vikram Narayananae0086c2012-10-10 23:10:22 +0530229 pr_info("finished, %d operations done\n", op);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200230
231out:
232 kfree(offsets);
233 kfree(bbt);
234 vfree(writebuf);
235 vfree(readbuf);
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100236out_put_mtd:
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200237 put_mtd_device(mtd);
238 if (err)
Vikram Narayananae0086c2012-10-10 23:10:22 +0530239 pr_info("error %d occurred\n", err);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200240 printk(KERN_INFO "=================================================\n");
241 return err;
242}
243module_init(mtd_stresstest_init);
244
245static void __exit mtd_stresstest_exit(void)
246{
247 return;
248}
249module_exit(mtd_stresstest_exit);
250
251MODULE_DESCRIPTION("Stress test module");
252MODULE_AUTHOR("Adrian Hunter");
253MODULE_LICENSE("GPL");