PageRenderTime 66ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/mtd/tests/mtd_speedtest.c

https://bitbucket.org/ahluntang/ubuntu-precise
C | 580 lines | 472 code | 75 blank | 33 comment | 101 complexity | ac1a6d4ed0a1bcb7aed0499f4c75b17c MD5 | raw file
  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 = 0;
  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 = 0;
  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 = 0, 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 = 0;
  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 = 0;
  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 = 0, 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. /* NOR flash does not implement block_isbad */
  291. if (mtd->block_isbad == NULL)
  292. goto out;
  293. printk(PRINT_PREF "scanning for bad eraseblocks\n");
  294. for (i = 0; i < ebcnt; ++i) {
  295. bbt[i] = is_block_bad(i) ? 1 : 0;
  296. if (bbt[i])
  297. bad += 1;
  298. cond_resched();
  299. }
  300. printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
  301. out:
  302. goodebcnt = ebcnt - bad;
  303. return 0;
  304. }
  305. static int __init mtd_speedtest_init(void)
  306. {
  307. int err, i, blocks, j, k;
  308. long speed;
  309. uint64_t tmp;
  310. printk(KERN_INFO "\n");
  311. printk(KERN_INFO "=================================================\n");
  312. if (dev < 0) {
  313. printk(PRINT_PREF "Please specify a valid mtd-device via module paramter\n");
  314. printk(KERN_CRIT "CAREFUL: This test wipes all data on the specified MTD device!\n");
  315. return -EINVAL;
  316. }
  317. if (count)
  318. printk(PRINT_PREF "MTD device: %d count: %d\n", dev, count);
  319. else
  320. printk(PRINT_PREF "MTD device: %d\n", dev);
  321. mtd = get_mtd_device(NULL, dev);
  322. if (IS_ERR(mtd)) {
  323. err = PTR_ERR(mtd);
  324. printk(PRINT_PREF "error: cannot get MTD device\n");
  325. return err;
  326. }
  327. if (mtd->writesize == 1) {
  328. printk(PRINT_PREF "not NAND flash, assume page size is 512 "
  329. "bytes.\n");
  330. pgsize = 512;
  331. } else
  332. pgsize = mtd->writesize;
  333. tmp = mtd->size;
  334. do_div(tmp, mtd->erasesize);
  335. ebcnt = tmp;
  336. pgcnt = mtd->erasesize / pgsize;
  337. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  338. "page size %u, count of eraseblocks %u, pages per "
  339. "eraseblock %u, OOB size %u\n",
  340. (unsigned long long)mtd->size, mtd->erasesize,
  341. pgsize, ebcnt, pgcnt, mtd->oobsize);
  342. if (count > 0 && count < ebcnt)
  343. ebcnt = count;
  344. err = -ENOMEM;
  345. iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  346. if (!iobuf) {
  347. printk(PRINT_PREF "error: cannot allocate memory\n");
  348. goto out;
  349. }
  350. simple_srand(1);
  351. set_random_data(iobuf, mtd->erasesize);
  352. err = scan_for_bad_eraseblocks();
  353. if (err)
  354. goto out;
  355. err = erase_whole_device();
  356. if (err)
  357. goto out;
  358. /* Write all eraseblocks, 1 eraseblock at a time */
  359. printk(PRINT_PREF "testing eraseblock write speed\n");
  360. start_timing();
  361. for (i = 0; i < ebcnt; ++i) {
  362. if (bbt[i])
  363. continue;
  364. err = write_eraseblock(i);
  365. if (err)
  366. goto out;
  367. cond_resched();
  368. }
  369. stop_timing();
  370. speed = calc_speed();
  371. printk(PRINT_PREF "eraseblock write speed is %ld KiB/s\n", speed);
  372. /* Read all eraseblocks, 1 eraseblock at a time */
  373. printk(PRINT_PREF "testing eraseblock read speed\n");
  374. start_timing();
  375. for (i = 0; i < ebcnt; ++i) {
  376. if (bbt[i])
  377. continue;
  378. err = read_eraseblock(i);
  379. if (err)
  380. goto out;
  381. cond_resched();
  382. }
  383. stop_timing();
  384. speed = calc_speed();
  385. printk(PRINT_PREF "eraseblock read speed is %ld KiB/s\n", speed);
  386. err = erase_whole_device();
  387. if (err)
  388. goto out;
  389. /* Write all eraseblocks, 1 page at a time */
  390. printk(PRINT_PREF "testing page write speed\n");
  391. start_timing();
  392. for (i = 0; i < ebcnt; ++i) {
  393. if (bbt[i])
  394. continue;
  395. err = write_eraseblock_by_page(i);
  396. if (err)
  397. goto out;
  398. cond_resched();
  399. }
  400. stop_timing();
  401. speed = calc_speed();
  402. printk(PRINT_PREF "page write speed is %ld KiB/s\n", speed);
  403. /* Read all eraseblocks, 1 page at a time */
  404. printk(PRINT_PREF "testing page read speed\n");
  405. start_timing();
  406. for (i = 0; i < ebcnt; ++i) {
  407. if (bbt[i])
  408. continue;
  409. err = read_eraseblock_by_page(i);
  410. if (err)
  411. goto out;
  412. cond_resched();
  413. }
  414. stop_timing();
  415. speed = calc_speed();
  416. printk(PRINT_PREF "page read speed is %ld KiB/s\n", speed);
  417. err = erase_whole_device();
  418. if (err)
  419. goto out;
  420. /* Write all eraseblocks, 2 pages at a time */
  421. printk(PRINT_PREF "testing 2 page write speed\n");
  422. start_timing();
  423. for (i = 0; i < ebcnt; ++i) {
  424. if (bbt[i])
  425. continue;
  426. err = write_eraseblock_by_2pages(i);
  427. if (err)
  428. goto out;
  429. cond_resched();
  430. }
  431. stop_timing();
  432. speed = calc_speed();
  433. printk(PRINT_PREF "2 page write speed is %ld KiB/s\n", speed);
  434. /* Read all eraseblocks, 2 pages at a time */
  435. printk(PRINT_PREF "testing 2 page read speed\n");
  436. start_timing();
  437. for (i = 0; i < ebcnt; ++i) {
  438. if (bbt[i])
  439. continue;
  440. err = read_eraseblock_by_2pages(i);
  441. if (err)
  442. goto out;
  443. cond_resched();
  444. }
  445. stop_timing();
  446. speed = calc_speed();
  447. printk(PRINT_PREF "2 page read speed is %ld KiB/s\n", speed);
  448. /* Erase all eraseblocks */
  449. printk(PRINT_PREF "Testing erase speed\n");
  450. start_timing();
  451. for (i = 0; i < ebcnt; ++i) {
  452. if (bbt[i])
  453. continue;
  454. err = erase_eraseblock(i);
  455. if (err)
  456. goto out;
  457. cond_resched();
  458. }
  459. stop_timing();
  460. speed = calc_speed();
  461. printk(PRINT_PREF "erase speed is %ld KiB/s\n", speed);
  462. /* Multi-block erase all eraseblocks */
  463. for (k = 1; k < 7; k++) {
  464. blocks = 1 << k;
  465. printk(PRINT_PREF "Testing %dx multi-block erase speed\n",
  466. blocks);
  467. start_timing();
  468. for (i = 0; i < ebcnt; ) {
  469. for (j = 0; j < blocks && (i + j) < ebcnt; j++)
  470. if (bbt[i + j])
  471. break;
  472. if (j < 1) {
  473. i++;
  474. continue;
  475. }
  476. err = multiblock_erase(i, j);
  477. if (err)
  478. goto out;
  479. cond_resched();
  480. i += j;
  481. }
  482. stop_timing();
  483. speed = calc_speed();
  484. printk(PRINT_PREF "%dx multi-block erase speed is %ld KiB/s\n",
  485. blocks, speed);
  486. }
  487. printk(PRINT_PREF "finished\n");
  488. out:
  489. kfree(iobuf);
  490. kfree(bbt);
  491. put_mtd_device(mtd);
  492. if (err)
  493. printk(PRINT_PREF "error %d occurred\n", err);
  494. printk(KERN_INFO "=================================================\n");
  495. return err;
  496. }
  497. module_init(mtd_speedtest_init);
  498. static void __exit mtd_speedtest_exit(void)
  499. {
  500. return;
  501. }
  502. module_exit(mtd_speedtest_exit);
  503. MODULE_DESCRIPTION("Speed test module");
  504. MODULE_AUTHOR("Adrian Hunter");
  505. MODULE_LICENSE("GPL");