mtd_speedtest.c 12 KB

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