mtd_speedtest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Copyright (C) 2007 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 read and write speed of a MTD device.
  18. *
  19. * Author: Adrian Hunter <adrian.hunter@nokia.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/err.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/slab.h>
  28. #include <linux/sched.h>
  29. #include <linux/random.h>
  30. static int dev = -EINVAL;
  31. module_param(dev, int, S_IRUGO);
  32. MODULE_PARM_DESC(dev, "MTD device number to use");
  33. static int count;
  34. module_param(count, int, S_IRUGO);
  35. MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use "
  36. "(0 means use all)");
  37. static struct mtd_info *mtd;
  38. static unsigned char *iobuf;
  39. static unsigned char *bbt;
  40. static int pgsize;
  41. static int ebcnt;
  42. static int pgcnt;
  43. static int goodebcnt;
  44. static struct timeval start, finish;
  45. static int erase_eraseblock(int ebnum)
  46. {
  47. int err;
  48. struct erase_info ei;
  49. loff_t addr = ebnum * mtd->erasesize;
  50. memset(&ei, 0, sizeof(struct erase_info));
  51. ei.mtd = mtd;
  52. ei.addr = addr;
  53. ei.len = mtd->erasesize;
  54. err = mtd_erase(mtd, &ei);
  55. if (err) {
  56. pr_err("error %d while erasing EB %d\n", err, ebnum);
  57. return err;
  58. }
  59. if (ei.state == MTD_ERASE_FAILED) {
  60. pr_err("some erase error occurred at EB %d\n",
  61. ebnum);
  62. return -EIO;
  63. }
  64. return 0;
  65. }
  66. static int multiblock_erase(int ebnum, int blocks)
  67. {
  68. int err;
  69. struct erase_info ei;
  70. loff_t addr = ebnum * mtd->erasesize;
  71. memset(&ei, 0, sizeof(struct erase_info));
  72. ei.mtd = mtd;
  73. ei.addr = addr;
  74. ei.len = mtd->erasesize * blocks;
  75. err = mtd_erase(mtd, &ei);
  76. if (err) {
  77. pr_err("error %d while erasing EB %d, blocks %d\n",
  78. err, ebnum, blocks);
  79. return err;
  80. }
  81. if (ei.state == MTD_ERASE_FAILED) {
  82. pr_err("some erase error occurred at EB %d,"
  83. "blocks %d\n", ebnum, blocks);
  84. return -EIO;
  85. }
  86. return 0;
  87. }
  88. static int erase_whole_device(void)
  89. {
  90. int err;
  91. unsigned int i;
  92. for (i = 0; i < ebcnt; ++i) {
  93. if (bbt[i])
  94. continue;
  95. err = erase_eraseblock(i);
  96. if (err)
  97. return err;
  98. cond_resched();
  99. }
  100. return 0;
  101. }
  102. static int write_eraseblock(int ebnum)
  103. {
  104. size_t written;
  105. int err = 0;
  106. loff_t addr = ebnum * mtd->erasesize;
  107. err = mtd_write(mtd, addr, mtd->erasesize, &written, iobuf);
  108. if (err || written != mtd->erasesize) {
  109. pr_err("error: write failed at %#llx\n", addr);
  110. if (!err)
  111. err = -EINVAL;
  112. }
  113. return err;
  114. }
  115. static int write_eraseblock_by_page(int ebnum)
  116. {
  117. size_t written;
  118. int i, err = 0;
  119. loff_t addr = ebnum * mtd->erasesize;
  120. void *buf = iobuf;
  121. for (i = 0; i < pgcnt; i++) {
  122. err = mtd_write(mtd, addr, pgsize, &written, buf);
  123. if (err || written != pgsize) {
  124. pr_err("error: write failed at %#llx\n",
  125. addr);
  126. if (!err)
  127. err = -EINVAL;
  128. break;
  129. }
  130. addr += pgsize;
  131. buf += pgsize;
  132. }
  133. return err;
  134. }
  135. static int write_eraseblock_by_2pages(int ebnum)
  136. {
  137. size_t written, sz = pgsize * 2;
  138. int i, n = pgcnt / 2, err = 0;
  139. loff_t addr = ebnum * mtd->erasesize;
  140. void *buf = iobuf;
  141. for (i = 0; i < n; i++) {
  142. err = mtd_write(mtd, addr, sz, &written, buf);
  143. if (err || written != sz) {
  144. pr_err("error: write failed at %#llx\n",
  145. addr);
  146. if (!err)
  147. err = -EINVAL;
  148. return err;
  149. }
  150. addr += sz;
  151. buf += sz;
  152. }
  153. if (pgcnt % 2) {
  154. err = mtd_write(mtd, addr, pgsize, &written, buf);
  155. if (err || written != pgsize) {
  156. pr_err("error: write failed at %#llx\n",
  157. addr);
  158. if (!err)
  159. err = -EINVAL;
  160. }
  161. }
  162. return err;
  163. }
  164. static int read_eraseblock(int ebnum)
  165. {
  166. size_t read;
  167. int err = 0;
  168. loff_t addr = ebnum * mtd->erasesize;
  169. err = mtd_read(mtd, addr, mtd->erasesize, &read, iobuf);
  170. /* Ignore corrected ECC errors */
  171. if (mtd_is_bitflip(err))
  172. err = 0;
  173. if (err || read != mtd->erasesize) {
  174. pr_err("error: read failed at %#llx\n", addr);
  175. if (!err)
  176. err = -EINVAL;
  177. }
  178. return err;
  179. }
  180. static int read_eraseblock_by_page(int ebnum)
  181. {
  182. size_t read;
  183. int i, err = 0;
  184. loff_t addr = ebnum * mtd->erasesize;
  185. void *buf = iobuf;
  186. for (i = 0; i < pgcnt; i++) {
  187. err = mtd_read(mtd, addr, pgsize, &read, buf);
  188. /* Ignore corrected ECC errors */
  189. if (mtd_is_bitflip(err))
  190. err = 0;
  191. if (err || read != pgsize) {
  192. pr_err("error: read failed at %#llx\n",
  193. addr);
  194. if (!err)
  195. err = -EINVAL;
  196. break;
  197. }
  198. addr += pgsize;
  199. buf += pgsize;
  200. }
  201. return err;
  202. }
  203. static int read_eraseblock_by_2pages(int ebnum)
  204. {
  205. size_t read, sz = pgsize * 2;
  206. int i, n = pgcnt / 2, err = 0;
  207. loff_t addr = ebnum * mtd->erasesize;
  208. void *buf = iobuf;
  209. for (i = 0; i < n; i++) {
  210. err = mtd_read(mtd, addr, sz, &read, buf);
  211. /* Ignore corrected ECC errors */
  212. if (mtd_is_bitflip(err))
  213. err = 0;
  214. if (err || read != sz) {
  215. pr_err("error: read failed at %#llx\n",
  216. addr);
  217. if (!err)
  218. err = -EINVAL;
  219. return err;
  220. }
  221. addr += sz;
  222. buf += sz;
  223. }
  224. if (pgcnt % 2) {
  225. err = mtd_read(mtd, addr, pgsize, &read, buf);
  226. /* Ignore corrected ECC errors */
  227. if (mtd_is_bitflip(err))
  228. err = 0;
  229. if (err || read != pgsize) {
  230. pr_err("error: read failed at %#llx\n",
  231. addr);
  232. if (!err)
  233. err = -EINVAL;
  234. }
  235. }
  236. return err;
  237. }
  238. static int is_block_bad(int ebnum)
  239. {
  240. loff_t addr = ebnum * mtd->erasesize;
  241. int ret;
  242. ret = mtd_block_isbad(mtd, addr);
  243. if (ret)
  244. pr_info("block %d is bad\n", ebnum);
  245. return ret;
  246. }
  247. static inline void start_timing(void)
  248. {
  249. do_gettimeofday(&start);
  250. }
  251. static inline void stop_timing(void)
  252. {
  253. do_gettimeofday(&finish);
  254. }
  255. static long calc_speed(void)
  256. {
  257. uint64_t k;
  258. long ms;
  259. ms = (finish.tv_sec - start.tv_sec) * 1000 +
  260. (finish.tv_usec - start.tv_usec) / 1000;
  261. if (ms == 0)
  262. return 0;
  263. k = goodebcnt * (mtd->erasesize / 1024) * 1000;
  264. do_div(k, ms);
  265. return k;
  266. }
  267. static int scan_for_bad_eraseblocks(void)
  268. {
  269. int i, bad = 0;
  270. bbt = kzalloc(ebcnt, GFP_KERNEL);
  271. if (!bbt)
  272. return -ENOMEM;
  273. if (!mtd_can_have_bb(mtd))
  274. goto out;
  275. pr_info("scanning for bad eraseblocks\n");
  276. for (i = 0; i < ebcnt; ++i) {
  277. bbt[i] = is_block_bad(i) ? 1 : 0;
  278. if (bbt[i])
  279. bad += 1;
  280. cond_resched();
  281. }
  282. pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
  283. out:
  284. goodebcnt = ebcnt - bad;
  285. return 0;
  286. }
  287. static int __init mtd_speedtest_init(void)
  288. {
  289. int err, i, blocks, j, k;
  290. long speed;
  291. uint64_t tmp;
  292. printk(KERN_INFO "\n");
  293. printk(KERN_INFO "=================================================\n");
  294. if (dev < 0) {
  295. pr_info("Please specify a valid mtd-device via module parameter\n");
  296. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  297. return -EINVAL;
  298. }
  299. if (count)
  300. pr_info("MTD device: %d count: %d\n", dev, count);
  301. else
  302. pr_info("MTD device: %d\n", dev);
  303. mtd = get_mtd_device(NULL, dev);
  304. if (IS_ERR(mtd)) {
  305. err = PTR_ERR(mtd);
  306. pr_err("error: cannot get MTD device\n");
  307. return err;
  308. }
  309. if (mtd->writesize == 1) {
  310. pr_info("not NAND flash, assume page size is 512 "
  311. "bytes.\n");
  312. pgsize = 512;
  313. } else
  314. pgsize = mtd->writesize;
  315. tmp = mtd->size;
  316. do_div(tmp, mtd->erasesize);
  317. ebcnt = tmp;
  318. pgcnt = mtd->erasesize / pgsize;
  319. pr_info("MTD device size %llu, eraseblock size %u, "
  320. "page size %u, count of eraseblocks %u, pages per "
  321. "eraseblock %u, OOB size %u\n",
  322. (unsigned long long)mtd->size, mtd->erasesize,
  323. pgsize, ebcnt, pgcnt, mtd->oobsize);
  324. if (count > 0 && count < ebcnt)
  325. ebcnt = count;
  326. err = -ENOMEM;
  327. iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  328. if (!iobuf)
  329. goto out;
  330. prandom_bytes(iobuf, mtd->erasesize);
  331. err = scan_for_bad_eraseblocks();
  332. if (err)
  333. goto out;
  334. err = erase_whole_device();
  335. if (err)
  336. goto out;
  337. /* Write all eraseblocks, 1 eraseblock at a time */
  338. pr_info("testing eraseblock write speed\n");
  339. start_timing();
  340. for (i = 0; i < ebcnt; ++i) {
  341. if (bbt[i])
  342. continue;
  343. err = write_eraseblock(i);
  344. if (err)
  345. goto out;
  346. cond_resched();
  347. }
  348. stop_timing();
  349. speed = calc_speed();
  350. pr_info("eraseblock write speed is %ld KiB/s\n", speed);
  351. /* Read all eraseblocks, 1 eraseblock at a time */
  352. pr_info("testing eraseblock read speed\n");
  353. start_timing();
  354. for (i = 0; i < ebcnt; ++i) {
  355. if (bbt[i])
  356. continue;
  357. err = read_eraseblock(i);
  358. if (err)
  359. goto out;
  360. cond_resched();
  361. }
  362. stop_timing();
  363. speed = calc_speed();
  364. pr_info("eraseblock read speed is %ld KiB/s\n", speed);
  365. err = erase_whole_device();
  366. if (err)
  367. goto out;
  368. /* Write all eraseblocks, 1 page at a time */
  369. pr_info("testing page write speed\n");
  370. start_timing();
  371. for (i = 0; i < ebcnt; ++i) {
  372. if (bbt[i])
  373. continue;
  374. err = write_eraseblock_by_page(i);
  375. if (err)
  376. goto out;
  377. cond_resched();
  378. }
  379. stop_timing();
  380. speed = calc_speed();
  381. pr_info("page write speed is %ld KiB/s\n", speed);
  382. /* Read all eraseblocks, 1 page at a time */
  383. pr_info("testing page read speed\n");
  384. start_timing();
  385. for (i = 0; i < ebcnt; ++i) {
  386. if (bbt[i])
  387. continue;
  388. err = read_eraseblock_by_page(i);
  389. if (err)
  390. goto out;
  391. cond_resched();
  392. }
  393. stop_timing();
  394. speed = calc_speed();
  395. pr_info("page read speed is %ld KiB/s\n", speed);
  396. err = erase_whole_device();
  397. if (err)
  398. goto out;
  399. /* Write all eraseblocks, 2 pages at a time */
  400. pr_info("testing 2 page write speed\n");
  401. start_timing();
  402. for (i = 0; i < ebcnt; ++i) {
  403. if (bbt[i])
  404. continue;
  405. err = write_eraseblock_by_2pages(i);
  406. if (err)
  407. goto out;
  408. cond_resched();
  409. }
  410. stop_timing();
  411. speed = calc_speed();
  412. pr_info("2 page write speed is %ld KiB/s\n", speed);
  413. /* Read all eraseblocks, 2 pages at a time */
  414. pr_info("testing 2 page read speed\n");
  415. start_timing();
  416. for (i = 0; i < ebcnt; ++i) {
  417. if (bbt[i])
  418. continue;
  419. err = read_eraseblock_by_2pages(i);
  420. if (err)
  421. goto out;
  422. cond_resched();
  423. }
  424. stop_timing();
  425. speed = calc_speed();
  426. pr_info("2 page read speed is %ld KiB/s\n", speed);
  427. /* Erase all eraseblocks */
  428. pr_info("Testing erase speed\n");
  429. start_timing();
  430. for (i = 0; i < ebcnt; ++i) {
  431. if (bbt[i])
  432. continue;
  433. err = erase_eraseblock(i);
  434. if (err)
  435. goto out;
  436. cond_resched();
  437. }
  438. stop_timing();
  439. speed = calc_speed();
  440. pr_info("erase speed is %ld KiB/s\n", speed);
  441. /* Multi-block erase all eraseblocks */
  442. for (k = 1; k < 7; k++) {
  443. blocks = 1 << k;
  444. pr_info("Testing %dx multi-block erase speed\n",
  445. blocks);
  446. start_timing();
  447. for (i = 0; i < ebcnt; ) {
  448. for (j = 0; j < blocks && (i + j) < ebcnt; j++)
  449. if (bbt[i + j])
  450. break;
  451. if (j < 1) {
  452. i++;
  453. continue;
  454. }
  455. err = multiblock_erase(i, j);
  456. if (err)
  457. goto out;
  458. cond_resched();
  459. i += j;
  460. }
  461. stop_timing();
  462. speed = calc_speed();
  463. pr_info("%dx multi-block erase speed is %ld KiB/s\n",
  464. blocks, speed);
  465. }
  466. pr_info("finished\n");
  467. out:
  468. kfree(iobuf);
  469. kfree(bbt);
  470. put_mtd_device(mtd);
  471. if (err)
  472. pr_info("error %d occurred\n", err);
  473. printk(KERN_INFO "=================================================\n");
  474. return err;
  475. }
  476. module_init(mtd_speedtest_init);
  477. static void __exit mtd_speedtest_exit(void)
  478. {
  479. return;
  480. }
  481. module_exit(mtd_speedtest_exit);
  482. MODULE_DESCRIPTION("Speed test module");
  483. MODULE_AUTHOR("Adrian Hunter");
  484. MODULE_LICENSE("GPL");