Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 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 | |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 30 | #include <linux/sched.h> |
| 31 | #include <linux/vmalloc.h> |
Artem Bityutskiy | bfea1d4 | 2012-05-18 18:44:53 +0300 | [diff] [blame] | 32 | #include <linux/random.h> |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 33 | |
Akinobu Mita | 5a78df6 | 2013-08-03 18:52:13 +0900 | [diff] [blame] | 34 | #include "mtd_test.h" |
| 35 | |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 36 | static int dev = -EINVAL; |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 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; |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 54 | |
| 55 | static int rand_eb(void) |
| 56 | { |
Artem Bityutskiy | bfea1d4 | 2012-05-18 18:44:53 +0300 | [diff] [blame] | 57 | unsigned int eb; |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 58 | |
| 59 | again: |
Akinobu Mita | aca662a | 2013-01-03 21:19:13 +0900 | [diff] [blame] | 60 | eb = prandom_u32(); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 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 | { |
Artem Bityutskiy | bfea1d4 | 2012-05-18 18:44:53 +0300 | [diff] [blame] | 70 | unsigned int offs; |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 71 | |
Akinobu Mita | aca662a | 2013-01-03 21:19:13 +0900 | [diff] [blame] | 72 | offs = prandom_u32(); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 73 | offs %= bufsize; |
| 74 | return offs; |
| 75 | } |
| 76 | |
| 77 | static int rand_len(int offs) |
| 78 | { |
Artem Bityutskiy | bfea1d4 | 2012-05-18 18:44:53 +0300 | [diff] [blame] | 79 | unsigned int len; |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 80 | |
Akinobu Mita | aca662a | 2013-01-03 21:19:13 +0900 | [diff] [blame] | 81 | len = prandom_u32(); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 82 | len %= (bufsize - offs); |
| 83 | return len; |
| 84 | } |
| 85 | |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 86 | static int do_read(void) |
| 87 | { |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 88 | int eb = rand_eb(); |
| 89 | int offs = rand_offs(); |
Akinobu Mita | abc173a | 2013-08-15 22:55:08 +0900 | [diff] [blame^] | 90 | int len = rand_len(offs); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 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; |
Akinobu Mita | abc173a | 2013-08-15 22:55:08 +0900 | [diff] [blame^] | 100 | return mtdtest_read(mtd, addr, len, readbuf); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static int do_write(void) |
| 104 | { |
| 105 | int eb = rand_eb(), offs, err, len; |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 106 | loff_t addr; |
| 107 | |
| 108 | offs = offsets[eb]; |
| 109 | if (offs >= mtd->erasesize) { |
Akinobu Mita | 5a78df6 | 2013-08-03 18:52:13 +0900 | [diff] [blame] | 110 | err = mtdtest_erase_eraseblock(mtd, eb); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 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 { |
Akinobu Mita | 5a78df6 | 2013-08-03 18:52:13 +0900 | [diff] [blame] | 121 | err = mtdtest_erase_eraseblock(mtd, eb + 1); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 122 | if (err) |
| 123 | return err; |
| 124 | offsets[eb + 1] = 0; |
| 125 | } |
| 126 | } |
| 127 | addr = eb * mtd->erasesize + offs; |
Akinobu Mita | 5a78df6 | 2013-08-03 18:52:13 +0900 | [diff] [blame] | 128 | err = mtdtest_write(mtd, addr, len, writebuf); |
| 129 | if (unlikely(err)) { |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 130 | pr_err("error: write failed at 0x%llx\n", |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 131 | (long long)addr); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 132 | return err; |
| 133 | } |
| 134 | offs += len; |
| 135 | while (offs > mtd->erasesize) { |
| 136 | offsets[eb++] = mtd->erasesize; |
| 137 | offs -= mtd->erasesize; |
| 138 | } |
| 139 | offsets[eb] = offs; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int do_operation(void) |
| 144 | { |
Akinobu Mita | aca662a | 2013-01-03 21:19:13 +0900 | [diff] [blame] | 145 | if (prandom_u32() & 1) |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 146 | return do_read(); |
| 147 | else |
| 148 | return do_write(); |
| 149 | } |
| 150 | |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 151 | static int __init mtd_stresstest_init(void) |
| 152 | { |
| 153 | int err; |
| 154 | int i, op; |
| 155 | uint64_t tmp; |
| 156 | |
| 157 | printk(KERN_INFO "\n"); |
| 158 | printk(KERN_INFO "=================================================\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 159 | |
| 160 | if (dev < 0) { |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 161 | pr_info("Please specify a valid mtd-device via module parameter\n"); |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 162 | pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | } |
| 165 | |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 166 | pr_info("MTD device: %d\n", dev); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 167 | |
| 168 | mtd = get_mtd_device(NULL, dev); |
| 169 | if (IS_ERR(mtd)) { |
| 170 | err = PTR_ERR(mtd); |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 171 | pr_err("error: cannot get MTD device\n"); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 172 | return err; |
| 173 | } |
| 174 | |
| 175 | if (mtd->writesize == 1) { |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 176 | pr_info("not NAND flash, assume page size is 512 " |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 177 | "bytes.\n"); |
| 178 | pgsize = 512; |
| 179 | } else |
| 180 | pgsize = mtd->writesize; |
| 181 | |
| 182 | tmp = mtd->size; |
| 183 | do_div(tmp, mtd->erasesize); |
| 184 | ebcnt = tmp; |
Morten Thunberg Svendsen | f5e2bae | 2010-01-06 10:48:18 +0100 | [diff] [blame] | 185 | pgcnt = mtd->erasesize / pgsize; |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 186 | |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 187 | pr_info("MTD device size %llu, eraseblock size %u, " |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 188 | "page size %u, count of eraseblocks %u, pages per " |
| 189 | "eraseblock %u, OOB size %u\n", |
| 190 | (unsigned long long)mtd->size, mtd->erasesize, |
| 191 | pgsize, ebcnt, pgcnt, mtd->oobsize); |
| 192 | |
Wolfram Sang | 2f4478c | 2011-11-29 15:34:08 +0100 | [diff] [blame] | 193 | if (ebcnt < 2) { |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 194 | pr_err("error: need at least 2 eraseblocks\n"); |
Wolfram Sang | 2f4478c | 2011-11-29 15:34:08 +0100 | [diff] [blame] | 195 | err = -ENOSPC; |
| 196 | goto out_put_mtd; |
| 197 | } |
| 198 | |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 199 | /* Read or write up 2 eraseblocks at a time */ |
| 200 | bufsize = mtd->erasesize * 2; |
| 201 | |
| 202 | err = -ENOMEM; |
| 203 | readbuf = vmalloc(bufsize); |
| 204 | writebuf = vmalloc(bufsize); |
| 205 | offsets = kmalloc(ebcnt * sizeof(int), GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 206 | if (!readbuf || !writebuf || !offsets) |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 207 | goto out; |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 208 | for (i = 0; i < ebcnt; i++) |
| 209 | offsets[i] = mtd->erasesize; |
Akinobu Mita | 4debec7 | 2013-02-27 17:05:40 -0800 | [diff] [blame] | 210 | prandom_bytes(writebuf, bufsize); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 211 | |
Akinobu Mita | 5a78df6 | 2013-08-03 18:52:13 +0900 | [diff] [blame] | 212 | bbt = kzalloc(ebcnt, GFP_KERNEL); |
| 213 | if (!bbt) |
| 214 | goto out; |
| 215 | err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 216 | if (err) |
| 217 | goto out; |
| 218 | |
| 219 | /* Do operations */ |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 220 | pr_info("doing operations\n"); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 221 | for (op = 0; op < count; op++) { |
| 222 | if ((op & 1023) == 0) |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 223 | pr_info("%d operations done\n", op); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 224 | err = do_operation(); |
| 225 | if (err) |
| 226 | goto out; |
| 227 | cond_resched(); |
| 228 | } |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 229 | pr_info("finished, %d operations done\n", op); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 230 | |
| 231 | out: |
| 232 | kfree(offsets); |
| 233 | kfree(bbt); |
| 234 | vfree(writebuf); |
| 235 | vfree(readbuf); |
Wolfram Sang | 2f4478c | 2011-11-29 15:34:08 +0100 | [diff] [blame] | 236 | out_put_mtd: |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 237 | put_mtd_device(mtd); |
| 238 | if (err) |
Vikram Narayanan | ae0086c | 2012-10-10 23:10:22 +0530 | [diff] [blame] | 239 | pr_info("error %d occurred\n", err); |
Artem Bityutskiy | 7163cea | 2008-12-08 13:36:47 +0200 | [diff] [blame] | 240 | printk(KERN_INFO "=================================================\n"); |
| 241 | return err; |
| 242 | } |
| 243 | module_init(mtd_stresstest_init); |
| 244 | |
| 245 | static void __exit mtd_stresstest_exit(void) |
| 246 | { |
| 247 | return; |
| 248 | } |
| 249 | module_exit(mtd_stresstest_exit); |
| 250 | |
| 251 | MODULE_DESCRIPTION("Stress test module"); |
| 252 | MODULE_AUTHOR("Adrian Hunter"); |
| 253 | MODULE_LICENSE("GPL"); |