mtd_speedtest.c 12 KB

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