Commit | Line | Data |
---|---|---|
7163cea1 AB |
1 | /* |
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 | ||
ae0086cf VN |
22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
23 | ||
7163cea1 AB |
24 | #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> | |
5a0e3ad6 | 29 | #include <linux/slab.h> |
7163cea1 AB |
30 | #include <linux/sched.h> |
31 | #include <linux/vmalloc.h> | |
bfea1d4e | 32 | #include <linux/random.h> |
7163cea1 | 33 | |
5a78df69 AM |
34 | #include "mtd_test.h" |
35 | ||
7406060e | 36 | static int dev = -EINVAL; |
7163cea1 AB |
37 | module_param(dev, int, S_IRUGO); |
38 | MODULE_PARM_DESC(dev, "MTD device number to use"); | |
39 | ||
40 | static int count = 10000; | |
41 | module_param(count, int, S_IRUGO); | |
42 | MODULE_PARM_DESC(count, "Number of operations to do (default is 10000)"); | |
43 | ||
44 | static struct mtd_info *mtd; | |
45 | static unsigned char *writebuf; | |
46 | static unsigned char *readbuf; | |
47 | static unsigned char *bbt; | |
48 | static int *offsets; | |
49 | ||
50 | static int pgsize; | |
51 | static int bufsize; | |
52 | static int ebcnt; | |
53 | static int pgcnt; | |
7163cea1 AB |
54 | |
55 | static int rand_eb(void) | |
56 | { | |
bfea1d4e | 57 | unsigned int eb; |
7163cea1 AB |
58 | |
59 | again: | |
aca662a3 | 60 | eb = prandom_u32(); |
7163cea1 AB |
61 | /* 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 | ||
68 | static int rand_offs(void) | |
69 | { | |
bfea1d4e | 70 | unsigned int offs; |
7163cea1 | 71 | |
aca662a3 | 72 | offs = prandom_u32(); |
7163cea1 AB |
73 | offs %= bufsize; |
74 | return offs; | |
75 | } | |
76 | ||
77 | static int rand_len(int offs) | |
78 | { | |
bfea1d4e | 79 | unsigned int len; |
7163cea1 | 80 | |
aca662a3 | 81 | len = prandom_u32(); |
7163cea1 AB |
82 | len %= (bufsize - offs); |
83 | return len; | |
84 | } | |
85 | ||
7163cea1 AB |
86 | static int do_read(void) |
87 | { | |
7163cea1 AB |
88 | int eb = rand_eb(); |
89 | int offs = rand_offs(); | |
abc173ad | 90 | int len = rand_len(offs); |
7163cea1 AB |
91 | 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 | } | |
99 | addr = eb * mtd->erasesize + offs; | |
abc173ad | 100 | return mtdtest_read(mtd, addr, len, readbuf); |
7163cea1 AB |
101 | } |
102 | ||
103 | static int do_write(void) | |
104 | { | |
105 | int eb = rand_eb(), offs, err, len; | |
7163cea1 AB |
106 | loff_t addr; |
107 | ||
108 | offs = offsets[eb]; | |
109 | if (offs >= mtd->erasesize) { | |
5a78df69 | 110 | err = mtdtest_erase_eraseblock(mtd, eb); |
7163cea1 AB |
111 | 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 { | |
5a78df69 | 121 | err = mtdtest_erase_eraseblock(mtd, eb + 1); |
7163cea1 AB |
122 | if (err) |
123 | return err; | |
124 | offsets[eb + 1] = 0; | |
125 | } | |
126 | } | |
127 | addr = eb * mtd->erasesize + offs; | |
5a78df69 | 128 | err = mtdtest_write(mtd, addr, len, writebuf); |
8a9f4aa3 | 129 | if (unlikely(err)) |
7163cea1 | 130 | return err; |
7163cea1 AB |
131 | 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 | ||
140 | static int do_operation(void) | |
141 | { | |
aca662a3 | 142 | if (prandom_u32() & 1) |
7163cea1 AB |
143 | return do_read(); |
144 | else | |
145 | return do_write(); | |
146 | } | |
147 | ||
7163cea1 AB |
148 | static 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"); | |
7406060e WS |
156 | |
157 | if (dev < 0) { | |
064a7694 | 158 | pr_info("Please specify a valid mtd-device via module parameter\n"); |
ae0086cf | 159 | pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n"); |
7406060e WS |
160 | return -EINVAL; |
161 | } | |
162 | ||
ae0086cf | 163 | pr_info("MTD device: %d\n", dev); |
7163cea1 AB |
164 | |
165 | mtd = get_mtd_device(NULL, dev); | |
166 | if (IS_ERR(mtd)) { | |
167 | err = PTR_ERR(mtd); | |
ae0086cf | 168 | pr_err("error: cannot get MTD device\n"); |
7163cea1 AB |
169 | return err; |
170 | } | |
171 | ||
172 | if (mtd->writesize == 1) { | |
ae0086cf | 173 | pr_info("not NAND flash, assume page size is 512 " |
7163cea1 AB |
174 | "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; | |
f5e2bae0 | 182 | pgcnt = mtd->erasesize / pgsize; |
7163cea1 | 183 | |
ae0086cf | 184 | pr_info("MTD device size %llu, eraseblock size %u, " |
7163cea1 AB |
185 | "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 | ||
2f4478cc | 190 | if (ebcnt < 2) { |
ae0086cf | 191 | pr_err("error: need at least 2 eraseblocks\n"); |
2f4478cc WS |
192 | err = -ENOSPC; |
193 | goto out_put_mtd; | |
194 | } | |
195 | ||
7163cea1 AB |
196 | /* 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); | |
33777e66 | 203 | if (!readbuf || !writebuf || !offsets) |
7163cea1 | 204 | goto out; |
7163cea1 AB |
205 | for (i = 0; i < ebcnt; i++) |
206 | offsets[i] = mtd->erasesize; | |
4debec7a | 207 | prandom_bytes(writebuf, bufsize); |
7163cea1 | 208 | |
5a78df69 AM |
209 | bbt = kzalloc(ebcnt, GFP_KERNEL); |
210 | if (!bbt) | |
211 | goto out; | |
212 | err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt); | |
7163cea1 AB |
213 | if (err) |
214 | goto out; | |
215 | ||
216 | /* Do operations */ | |
ae0086cf | 217 | pr_info("doing operations\n"); |
7163cea1 AB |
218 | for (op = 0; op < count; op++) { |
219 | if ((op & 1023) == 0) | |
ae0086cf | 220 | pr_info("%d operations done\n", op); |
7163cea1 AB |
221 | err = do_operation(); |
222 | if (err) | |
223 | goto out; | |
224 | cond_resched(); | |
225 | } | |
ae0086cf | 226 | pr_info("finished, %d operations done\n", op); |
7163cea1 AB |
227 | |
228 | out: | |
229 | kfree(offsets); | |
230 | kfree(bbt); | |
231 | vfree(writebuf); | |
232 | vfree(readbuf); | |
2f4478cc | 233 | out_put_mtd: |
7163cea1 AB |
234 | put_mtd_device(mtd); |
235 | if (err) | |
ae0086cf | 236 | pr_info("error %d occurred\n", err); |
7163cea1 AB |
237 | printk(KERN_INFO "=================================================\n"); |
238 | return err; | |
239 | } | |
240 | module_init(mtd_stresstest_init); | |
241 | ||
242 | static void __exit mtd_stresstest_exit(void) | |
243 | { | |
244 | return; | |
245 | } | |
246 | module_exit(mtd_stresstest_exit); | |
247 | ||
248 | MODULE_DESCRIPTION("Stress test module"); | |
249 | MODULE_AUTHOR("Adrian Hunter"); | |
250 | MODULE_LICENSE("GPL"); |