blob: 787f539d16ca436099826f0626bb4c57efe6da6d [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
Wolfram Sang74060602011-10-30 00:11:53 +020034static int dev = -EINVAL;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020035module_param(dev, int, S_IRUGO);
36MODULE_PARM_DESC(dev, "MTD device number to use");
37
38static int count = 10000;
39module_param(count, int, S_IRUGO);
40MODULE_PARM_DESC(count, "Number of operations to do (default is 10000)");
41
42static struct mtd_info *mtd;
43static unsigned char *writebuf;
44static unsigned char *readbuf;
45static unsigned char *bbt;
46static int *offsets;
47
48static int pgsize;
49static int bufsize;
50static int ebcnt;
51static int pgcnt;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020052
53static int rand_eb(void)
54{
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030055 unsigned int eb;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020056
57again:
Akinobu Mitaaca662a2013-01-03 21:19:13 +090058 eb = prandom_u32();
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020059 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
60 eb %= (ebcnt - 1);
61 if (bbt[eb])
62 goto again;
63 return eb;
64}
65
66static int rand_offs(void)
67{
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030068 unsigned int offs;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020069
Akinobu Mitaaca662a2013-01-03 21:19:13 +090070 offs = prandom_u32();
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020071 offs %= bufsize;
72 return offs;
73}
74
75static int rand_len(int offs)
76{
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030077 unsigned int len;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020078
Akinobu Mitaaca662a2013-01-03 21:19:13 +090079 len = prandom_u32();
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020080 len %= (bufsize - offs);
81 return len;
82}
83
84static int erase_eraseblock(int ebnum)
85{
86 int err;
87 struct erase_info ei;
88 loff_t addr = ebnum * mtd->erasesize;
89
90 memset(&ei, 0, sizeof(struct erase_info));
91 ei.mtd = mtd;
92 ei.addr = addr;
93 ei.len = mtd->erasesize;
94
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +020095 err = mtd_erase(mtd, &ei);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020096 if (unlikely(err)) {
Vikram Narayananae0086c2012-10-10 23:10:22 +053097 pr_err("error %d while erasing EB %d\n", err, ebnum);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020098 return err;
99 }
100
101 if (unlikely(ei.state == MTD_ERASE_FAILED)) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530102 pr_err("some erase error occurred at EB %d\n",
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200103 ebnum);
104 return -EIO;
105 }
106
107 return 0;
108}
109
110static int is_block_bad(int ebnum)
111{
112 loff_t addr = ebnum * mtd->erasesize;
113 int ret;
114
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200115 ret = mtd_block_isbad(mtd, addr);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200116 if (ret)
Vikram Narayananae0086c2012-10-10 23:10:22 +0530117 pr_info("block %d is bad\n", ebnum);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200118 return ret;
119}
120
121static int do_read(void)
122{
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200123 size_t read;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200124 int eb = rand_eb();
125 int offs = rand_offs();
126 int len = rand_len(offs), err;
127 loff_t addr;
128
129 if (bbt[eb + 1]) {
130 if (offs >= mtd->erasesize)
131 offs -= mtd->erasesize;
132 if (offs + len > mtd->erasesize)
133 len = mtd->erasesize - offs;
134 }
135 addr = eb * mtd->erasesize + offs;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200136 err = mtd_read(mtd, addr, len, &read, readbuf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700137 if (mtd_is_bitflip(err))
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200138 err = 0;
139 if (unlikely(err || read != len)) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530140 pr_err("error: read failed at 0x%llx\n",
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200141 (long long)addr);
142 if (!err)
143 err = -EINVAL;
144 return err;
145 }
146 return 0;
147}
148
149static int do_write(void)
150{
151 int eb = rand_eb(), offs, err, len;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200152 size_t written;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200153 loff_t addr;
154
155 offs = offsets[eb];
156 if (offs >= mtd->erasesize) {
157 err = erase_eraseblock(eb);
158 if (err)
159 return err;
160 offs = offsets[eb] = 0;
161 }
162 len = rand_len(offs);
163 len = ((len + pgsize - 1) / pgsize) * pgsize;
164 if (offs + len > mtd->erasesize) {
165 if (bbt[eb + 1])
166 len = mtd->erasesize - offs;
167 else {
168 err = erase_eraseblock(eb + 1);
169 if (err)
170 return err;
171 offsets[eb + 1] = 0;
172 }
173 }
174 addr = eb * mtd->erasesize + offs;
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200175 err = mtd_write(mtd, addr, len, &written, writebuf);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200176 if (unlikely(err || written != len)) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530177 pr_err("error: write failed at 0x%llx\n",
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200178 (long long)addr);
179 if (!err)
180 err = -EINVAL;
181 return err;
182 }
183 offs += len;
184 while (offs > mtd->erasesize) {
185 offsets[eb++] = mtd->erasesize;
186 offs -= mtd->erasesize;
187 }
188 offsets[eb] = offs;
189 return 0;
190}
191
192static int do_operation(void)
193{
Akinobu Mitaaca662a2013-01-03 21:19:13 +0900194 if (prandom_u32() & 1)
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200195 return do_read();
196 else
197 return do_write();
198}
199
200static int scan_for_bad_eraseblocks(void)
201{
202 int i, bad = 0;
203
Julia Lawall2bfefa42010-05-13 22:03:15 +0200204 bbt = kzalloc(ebcnt, GFP_KERNEL);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200205 if (!bbt) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530206 pr_err("error: cannot allocate memory\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200207 return -ENOMEM;
208 }
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200209
Artem Bityutskiy8f461a72012-01-02 13:48:54 +0200210 if (!mtd_can_have_bb(mtd))
Morten Thunberg Svendsenf5e2bae2010-01-06 10:48:18 +0100211 return 0;
212
Vikram Narayananae0086c2012-10-10 23:10:22 +0530213 pr_info("scanning for bad eraseblocks\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200214 for (i = 0; i < ebcnt; ++i) {
215 bbt[i] = is_block_bad(i) ? 1 : 0;
216 if (bbt[i])
217 bad += 1;
218 cond_resched();
219 }
Vikram Narayananae0086c2012-10-10 23:10:22 +0530220 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200221 return 0;
222}
223
224static int __init mtd_stresstest_init(void)
225{
226 int err;
227 int i, op;
228 uint64_t tmp;
229
230 printk(KERN_INFO "\n");
231 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200232
233 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900234 pr_info("Please specify a valid mtd-device via module parameter\n");
Vikram Narayananae0086c2012-10-10 23:10:22 +0530235 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200236 return -EINVAL;
237 }
238
Vikram Narayananae0086c2012-10-10 23:10:22 +0530239 pr_info("MTD device: %d\n", dev);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200240
241 mtd = get_mtd_device(NULL, dev);
242 if (IS_ERR(mtd)) {
243 err = PTR_ERR(mtd);
Vikram Narayananae0086c2012-10-10 23:10:22 +0530244 pr_err("error: cannot get MTD device\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200245 return err;
246 }
247
248 if (mtd->writesize == 1) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530249 pr_info("not NAND flash, assume page size is 512 "
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200250 "bytes.\n");
251 pgsize = 512;
252 } else
253 pgsize = mtd->writesize;
254
255 tmp = mtd->size;
256 do_div(tmp, mtd->erasesize);
257 ebcnt = tmp;
Morten Thunberg Svendsenf5e2bae2010-01-06 10:48:18 +0100258 pgcnt = mtd->erasesize / pgsize;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200259
Vikram Narayananae0086c2012-10-10 23:10:22 +0530260 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200261 "page size %u, count of eraseblocks %u, pages per "
262 "eraseblock %u, OOB size %u\n",
263 (unsigned long long)mtd->size, mtd->erasesize,
264 pgsize, ebcnt, pgcnt, mtd->oobsize);
265
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100266 if (ebcnt < 2) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530267 pr_err("error: need at least 2 eraseblocks\n");
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100268 err = -ENOSPC;
269 goto out_put_mtd;
270 }
271
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200272 /* Read or write up 2 eraseblocks at a time */
273 bufsize = mtd->erasesize * 2;
274
275 err = -ENOMEM;
276 readbuf = vmalloc(bufsize);
277 writebuf = vmalloc(bufsize);
278 offsets = kmalloc(ebcnt * sizeof(int), GFP_KERNEL);
279 if (!readbuf || !writebuf || !offsets) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530280 pr_err("error: cannot allocate memory\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200281 goto out;
282 }
283 for (i = 0; i < ebcnt; i++)
284 offsets[i] = mtd->erasesize;
Akinobu Mita4debec72013-02-27 17:05:40 -0800285 prandom_bytes(writebuf, bufsize);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200286
287 err = scan_for_bad_eraseblocks();
288 if (err)
289 goto out;
290
291 /* Do operations */
Vikram Narayananae0086c2012-10-10 23:10:22 +0530292 pr_info("doing operations\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200293 for (op = 0; op < count; op++) {
294 if ((op & 1023) == 0)
Vikram Narayananae0086c2012-10-10 23:10:22 +0530295 pr_info("%d operations done\n", op);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200296 err = do_operation();
297 if (err)
298 goto out;
299 cond_resched();
300 }
Vikram Narayananae0086c2012-10-10 23:10:22 +0530301 pr_info("finished, %d operations done\n", op);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200302
303out:
304 kfree(offsets);
305 kfree(bbt);
306 vfree(writebuf);
307 vfree(readbuf);
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100308out_put_mtd:
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200309 put_mtd_device(mtd);
310 if (err)
Vikram Narayananae0086c2012-10-10 23:10:22 +0530311 pr_info("error %d occurred\n", err);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200312 printk(KERN_INFO "=================================================\n");
313 return err;
314}
315module_init(mtd_stresstest_init);
316
317static void __exit mtd_stresstest_exit(void)
318{
319 return;
320}
321module_exit(mtd_stresstest_exit);
322
323MODULE_DESCRIPTION("Stress test module");
324MODULE_AUTHOR("Adrian Hunter");
325MODULE_LICENSE("GPL");