mmc_test.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022
  1. /*
  2. * linux/drivers/mmc/card/mmc_test.c
  3. *
  4. * Copyright 2007-2008 Pierre Ossman
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. */
  11. #include <linux/mmc/core.h>
  12. #include <linux/mmc/card.h>
  13. #include <linux/mmc/host.h>
  14. #include <linux/mmc/mmc.h>
  15. #include <linux/slab.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/swap.h> /* For nr_free_buffer_pages() */
  18. #define RESULT_OK 0
  19. #define RESULT_FAIL 1
  20. #define RESULT_UNSUP_HOST 2
  21. #define RESULT_UNSUP_CARD 3
  22. #define BUFFER_ORDER 2
  23. #define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
  24. /*
  25. * Limit the test area size to the maximum MMC HC erase group size. Note that
  26. * the maximum SD allocation unit size is just 4MiB.
  27. */
  28. #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
  29. /**
  30. * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
  31. * @page: first page in the allocation
  32. * @order: order of the number of pages allocated
  33. */
  34. struct mmc_test_pages {
  35. struct page *page;
  36. unsigned int order;
  37. };
  38. /**
  39. * struct mmc_test_mem - allocated memory.
  40. * @arr: array of allocations
  41. * @cnt: number of allocations
  42. */
  43. struct mmc_test_mem {
  44. struct mmc_test_pages *arr;
  45. unsigned int cnt;
  46. };
  47. /**
  48. * struct mmc_test_area - information for performance tests.
  49. * @max_sz: test area size (in bytes)
  50. * @dev_addr: address on card at which to do performance tests
  51. * @max_segs: maximum segments in scatterlist @sg
  52. * @blocks: number of (512 byte) blocks currently mapped by @sg
  53. * @sg_len: length of currently mapped scatterlist @sg
  54. * @mem: allocated memory
  55. * @sg: scatterlist
  56. */
  57. struct mmc_test_area {
  58. unsigned long max_sz;
  59. unsigned int dev_addr;
  60. unsigned int max_segs;
  61. unsigned int blocks;
  62. unsigned int sg_len;
  63. struct mmc_test_mem *mem;
  64. struct scatterlist *sg;
  65. };
  66. /**
  67. * struct mmc_test_card - test information.
  68. * @card: card under test
  69. * @scratch: transfer buffer
  70. * @buffer: transfer buffer
  71. * @highmem: buffer for highmem tests
  72. * @area: information for performance tests
  73. */
  74. struct mmc_test_card {
  75. struct mmc_card *card;
  76. u8 scratch[BUFFER_SIZE];
  77. u8 *buffer;
  78. #ifdef CONFIG_HIGHMEM
  79. struct page *highmem;
  80. #endif
  81. struct mmc_test_area area;
  82. };
  83. /*******************************************************************/
  84. /* General helper functions */
  85. /*******************************************************************/
  86. /*
  87. * Configure correct block size in card
  88. */
  89. static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
  90. {
  91. struct mmc_command cmd;
  92. int ret;
  93. cmd.opcode = MMC_SET_BLOCKLEN;
  94. cmd.arg = size;
  95. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  96. ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
  97. if (ret)
  98. return ret;
  99. return 0;
  100. }
  101. /*
  102. * Fill in the mmc_request structure given a set of transfer parameters.
  103. */
  104. static void mmc_test_prepare_mrq(struct mmc_test_card *test,
  105. struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
  106. unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
  107. {
  108. BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
  109. if (blocks > 1) {
  110. mrq->cmd->opcode = write ?
  111. MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
  112. } else {
  113. mrq->cmd->opcode = write ?
  114. MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
  115. }
  116. mrq->cmd->arg = dev_addr;
  117. if (!mmc_card_blockaddr(test->card))
  118. mrq->cmd->arg <<= 9;
  119. mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  120. if (blocks == 1)
  121. mrq->stop = NULL;
  122. else {
  123. mrq->stop->opcode = MMC_STOP_TRANSMISSION;
  124. mrq->stop->arg = 0;
  125. mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  126. }
  127. mrq->data->blksz = blksz;
  128. mrq->data->blocks = blocks;
  129. mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
  130. mrq->data->sg = sg;
  131. mrq->data->sg_len = sg_len;
  132. mmc_set_data_timeout(mrq->data, test->card);
  133. }
  134. static int mmc_test_busy(struct mmc_command *cmd)
  135. {
  136. return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
  137. (R1_CURRENT_STATE(cmd->resp[0]) == 7);
  138. }
  139. /*
  140. * Wait for the card to finish the busy state
  141. */
  142. static int mmc_test_wait_busy(struct mmc_test_card *test)
  143. {
  144. int ret, busy;
  145. struct mmc_command cmd;
  146. busy = 0;
  147. do {
  148. memset(&cmd, 0, sizeof(struct mmc_command));
  149. cmd.opcode = MMC_SEND_STATUS;
  150. cmd.arg = test->card->rca << 16;
  151. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  152. ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
  153. if (ret)
  154. break;
  155. if (!busy && mmc_test_busy(&cmd)) {
  156. busy = 1;
  157. printk(KERN_INFO "%s: Warning: Host did not "
  158. "wait for busy state to end.\n",
  159. mmc_hostname(test->card->host));
  160. }
  161. } while (mmc_test_busy(&cmd));
  162. return ret;
  163. }
  164. /*
  165. * Transfer a single sector of kernel addressable data
  166. */
  167. static int mmc_test_buffer_transfer(struct mmc_test_card *test,
  168. u8 *buffer, unsigned addr, unsigned blksz, int write)
  169. {
  170. int ret;
  171. struct mmc_request mrq;
  172. struct mmc_command cmd;
  173. struct mmc_command stop;
  174. struct mmc_data data;
  175. struct scatterlist sg;
  176. memset(&mrq, 0, sizeof(struct mmc_request));
  177. memset(&cmd, 0, sizeof(struct mmc_command));
  178. memset(&data, 0, sizeof(struct mmc_data));
  179. memset(&stop, 0, sizeof(struct mmc_command));
  180. mrq.cmd = &cmd;
  181. mrq.data = &data;
  182. mrq.stop = &stop;
  183. sg_init_one(&sg, buffer, blksz);
  184. mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
  185. mmc_wait_for_req(test->card->host, &mrq);
  186. if (cmd.error)
  187. return cmd.error;
  188. if (data.error)
  189. return data.error;
  190. ret = mmc_test_wait_busy(test);
  191. if (ret)
  192. return ret;
  193. return 0;
  194. }
  195. static void mmc_test_free_mem(struct mmc_test_mem *mem)
  196. {
  197. if (!mem)
  198. return;
  199. while (mem->cnt--)
  200. __free_pages(mem->arr[mem->cnt].page,
  201. mem->arr[mem->cnt].order);
  202. kfree(mem->arr);
  203. kfree(mem);
  204. }
  205. /*
  206. * Allocate a lot of memory, preferrably max_sz but at least min_sz. In case
  207. * there isn't much memory do not exceed 1/16th total lowmem pages.
  208. */
  209. static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
  210. unsigned long max_sz)
  211. {
  212. unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
  213. unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
  214. unsigned long page_cnt = 0;
  215. unsigned long limit = nr_free_buffer_pages() >> 4;
  216. struct mmc_test_mem *mem;
  217. if (max_page_cnt > limit)
  218. max_page_cnt = limit;
  219. if (max_page_cnt < min_page_cnt)
  220. max_page_cnt = min_page_cnt;
  221. mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
  222. if (!mem)
  223. return NULL;
  224. mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_page_cnt,
  225. GFP_KERNEL);
  226. if (!mem->arr)
  227. goto out_free;
  228. while (max_page_cnt) {
  229. struct page *page;
  230. unsigned int order;
  231. gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
  232. __GFP_NORETRY;
  233. order = get_order(max_page_cnt << PAGE_SHIFT);
  234. while (1) {
  235. page = alloc_pages(flags, order);
  236. if (page || !order)
  237. break;
  238. order -= 1;
  239. }
  240. if (!page) {
  241. if (page_cnt < min_page_cnt)
  242. goto out_free;
  243. break;
  244. }
  245. mem->arr[mem->cnt].page = page;
  246. mem->arr[mem->cnt].order = order;
  247. mem->cnt += 1;
  248. if (max_page_cnt <= (1UL << order))
  249. break;
  250. max_page_cnt -= 1UL << order;
  251. page_cnt += 1UL << order;
  252. }
  253. return mem;
  254. out_free:
  255. mmc_test_free_mem(mem);
  256. return NULL;
  257. }
  258. /*
  259. * Map memory into a scatterlist. Optionally allow the same memory to be
  260. * mapped more than once.
  261. */
  262. static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
  263. struct scatterlist *sglist, int repeat,
  264. unsigned int max_segs, unsigned int *sg_len)
  265. {
  266. struct scatterlist *sg = NULL;
  267. unsigned int i;
  268. sg_init_table(sglist, max_segs);
  269. *sg_len = 0;
  270. do {
  271. for (i = 0; i < mem->cnt; i++) {
  272. unsigned long len = PAGE_SIZE << mem->arr[i].order;
  273. if (sz < len)
  274. len = sz;
  275. if (sg)
  276. sg = sg_next(sg);
  277. else
  278. sg = sglist;
  279. if (!sg)
  280. return -EINVAL;
  281. sg_set_page(sg, mem->arr[i].page, len, 0);
  282. sz -= len;
  283. *sg_len += 1;
  284. if (!sz)
  285. break;
  286. }
  287. } while (sz && repeat);
  288. if (sz)
  289. return -EINVAL;
  290. if (sg)
  291. sg_mark_end(sg);
  292. return 0;
  293. }
  294. /*
  295. * Map memory into a scatterlist so that no pages are contiguous. Allow the
  296. * same memory to be mapped more than once.
  297. */
  298. static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
  299. unsigned long sz,
  300. struct scatterlist *sglist,
  301. unsigned int max_segs,
  302. unsigned int *sg_len)
  303. {
  304. struct scatterlist *sg = NULL;
  305. unsigned int i = mem->cnt, cnt;
  306. unsigned long len;
  307. void *base, *addr, *last_addr = NULL;
  308. sg_init_table(sglist, max_segs);
  309. *sg_len = 0;
  310. while (sz && i) {
  311. base = page_address(mem->arr[--i].page);
  312. cnt = 1 << mem->arr[i].order;
  313. while (sz && cnt) {
  314. addr = base + PAGE_SIZE * --cnt;
  315. if (last_addr && last_addr + PAGE_SIZE == addr)
  316. continue;
  317. last_addr = addr;
  318. len = PAGE_SIZE;
  319. if (sz < len)
  320. len = sz;
  321. if (sg)
  322. sg = sg_next(sg);
  323. else
  324. sg = sglist;
  325. if (!sg)
  326. return -EINVAL;
  327. sg_set_page(sg, virt_to_page(addr), len, 0);
  328. sz -= len;
  329. *sg_len += 1;
  330. }
  331. }
  332. if (sg)
  333. sg_mark_end(sg);
  334. return 0;
  335. }
  336. /*
  337. * Calculate transfer rate in bytes per second.
  338. */
  339. static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
  340. {
  341. uint64_t ns;
  342. ns = ts->tv_sec;
  343. ns *= 1000000000;
  344. ns += ts->tv_nsec;
  345. bytes *= 1000000000;
  346. while (ns > UINT_MAX) {
  347. bytes >>= 1;
  348. ns >>= 1;
  349. }
  350. if (!ns)
  351. return 0;
  352. do_div(bytes, (uint32_t)ns);
  353. return bytes;
  354. }
  355. /*
  356. * Print the transfer rate.
  357. */
  358. static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
  359. struct timespec *ts1, struct timespec *ts2)
  360. {
  361. unsigned int rate, sectors = bytes >> 9;
  362. struct timespec ts;
  363. ts = timespec_sub(*ts2, *ts1);
  364. rate = mmc_test_rate(bytes, &ts);
  365. printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
  366. "seconds (%u kB/s, %u KiB/s)\n",
  367. mmc_hostname(test->card->host), sectors, sectors >> 1,
  368. (sectors == 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
  369. (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024);
  370. }
  371. /*
  372. * Print the average transfer rate.
  373. */
  374. static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
  375. unsigned int count, struct timespec *ts1,
  376. struct timespec *ts2)
  377. {
  378. unsigned int rate, sectors = bytes >> 9;
  379. uint64_t tot = bytes * count;
  380. struct timespec ts;
  381. ts = timespec_sub(*ts2, *ts1);
  382. rate = mmc_test_rate(tot, &ts);
  383. printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
  384. "%lu.%09lu seconds (%u kB/s, %u KiB/s)\n",
  385. mmc_hostname(test->card->host), count, sectors, count,
  386. sectors >> 1, (sectors == 1 ? ".5" : ""),
  387. (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
  388. rate / 1000, rate / 1024);
  389. }
  390. /*
  391. * Return the card size in sectors.
  392. */
  393. static unsigned int mmc_test_capacity(struct mmc_card *card)
  394. {
  395. if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
  396. return card->ext_csd.sectors;
  397. else
  398. return card->csd.capacity << (card->csd.read_blkbits - 9);
  399. }
  400. /*******************************************************************/
  401. /* Test preparation and cleanup */
  402. /*******************************************************************/
  403. /*
  404. * Fill the first couple of sectors of the card with known data
  405. * so that bad reads/writes can be detected
  406. */
  407. static int __mmc_test_prepare(struct mmc_test_card *test, int write)
  408. {
  409. int ret, i;
  410. ret = mmc_test_set_blksize(test, 512);
  411. if (ret)
  412. return ret;
  413. if (write)
  414. memset(test->buffer, 0xDF, 512);
  415. else {
  416. for (i = 0;i < 512;i++)
  417. test->buffer[i] = i;
  418. }
  419. for (i = 0;i < BUFFER_SIZE / 512;i++) {
  420. ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
  421. if (ret)
  422. return ret;
  423. }
  424. return 0;
  425. }
  426. static int mmc_test_prepare_write(struct mmc_test_card *test)
  427. {
  428. return __mmc_test_prepare(test, 1);
  429. }
  430. static int mmc_test_prepare_read(struct mmc_test_card *test)
  431. {
  432. return __mmc_test_prepare(test, 0);
  433. }
  434. static int mmc_test_cleanup(struct mmc_test_card *test)
  435. {
  436. int ret, i;
  437. ret = mmc_test_set_blksize(test, 512);
  438. if (ret)
  439. return ret;
  440. memset(test->buffer, 0, 512);
  441. for (i = 0;i < BUFFER_SIZE / 512;i++) {
  442. ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
  443. if (ret)
  444. return ret;
  445. }
  446. return 0;
  447. }
  448. /*******************************************************************/
  449. /* Test execution helpers */
  450. /*******************************************************************/
  451. /*
  452. * Modifies the mmc_request to perform the "short transfer" tests
  453. */
  454. static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
  455. struct mmc_request *mrq, int write)
  456. {
  457. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  458. if (mrq->data->blocks > 1) {
  459. mrq->cmd->opcode = write ?
  460. MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
  461. mrq->stop = NULL;
  462. } else {
  463. mrq->cmd->opcode = MMC_SEND_STATUS;
  464. mrq->cmd->arg = test->card->rca << 16;
  465. }
  466. }
  467. /*
  468. * Checks that a normal transfer didn't have any errors
  469. */
  470. static int mmc_test_check_result(struct mmc_test_card *test,
  471. struct mmc_request *mrq)
  472. {
  473. int ret;
  474. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  475. ret = 0;
  476. if (!ret && mrq->cmd->error)
  477. ret = mrq->cmd->error;
  478. if (!ret && mrq->data->error)
  479. ret = mrq->data->error;
  480. if (!ret && mrq->stop && mrq->stop->error)
  481. ret = mrq->stop->error;
  482. if (!ret && mrq->data->bytes_xfered !=
  483. mrq->data->blocks * mrq->data->blksz)
  484. ret = RESULT_FAIL;
  485. if (ret == -EINVAL)
  486. ret = RESULT_UNSUP_HOST;
  487. return ret;
  488. }
  489. /*
  490. * Checks that a "short transfer" behaved as expected
  491. */
  492. static int mmc_test_check_broken_result(struct mmc_test_card *test,
  493. struct mmc_request *mrq)
  494. {
  495. int ret;
  496. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  497. ret = 0;
  498. if (!ret && mrq->cmd->error)
  499. ret = mrq->cmd->error;
  500. if (!ret && mrq->data->error == 0)
  501. ret = RESULT_FAIL;
  502. if (!ret && mrq->data->error != -ETIMEDOUT)
  503. ret = mrq->data->error;
  504. if (!ret && mrq->stop && mrq->stop->error)
  505. ret = mrq->stop->error;
  506. if (mrq->data->blocks > 1) {
  507. if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
  508. ret = RESULT_FAIL;
  509. } else {
  510. if (!ret && mrq->data->bytes_xfered > 0)
  511. ret = RESULT_FAIL;
  512. }
  513. if (ret == -EINVAL)
  514. ret = RESULT_UNSUP_HOST;
  515. return ret;
  516. }
  517. /*
  518. * Tests a basic transfer with certain parameters
  519. */
  520. static int mmc_test_simple_transfer(struct mmc_test_card *test,
  521. struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
  522. unsigned blocks, unsigned blksz, int write)
  523. {
  524. struct mmc_request mrq;
  525. struct mmc_command cmd;
  526. struct mmc_command stop;
  527. struct mmc_data data;
  528. memset(&mrq, 0, sizeof(struct mmc_request));
  529. memset(&cmd, 0, sizeof(struct mmc_command));
  530. memset(&data, 0, sizeof(struct mmc_data));
  531. memset(&stop, 0, sizeof(struct mmc_command));
  532. mrq.cmd = &cmd;
  533. mrq.data = &data;
  534. mrq.stop = &stop;
  535. mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
  536. blocks, blksz, write);
  537. mmc_wait_for_req(test->card->host, &mrq);
  538. mmc_test_wait_busy(test);
  539. return mmc_test_check_result(test, &mrq);
  540. }
  541. /*
  542. * Tests a transfer where the card will fail completely or partly
  543. */
  544. static int mmc_test_broken_transfer(struct mmc_test_card *test,
  545. unsigned blocks, unsigned blksz, int write)
  546. {
  547. struct mmc_request mrq;
  548. struct mmc_command cmd;
  549. struct mmc_command stop;
  550. struct mmc_data data;
  551. struct scatterlist sg;
  552. memset(&mrq, 0, sizeof(struct mmc_request));
  553. memset(&cmd, 0, sizeof(struct mmc_command));
  554. memset(&data, 0, sizeof(struct mmc_data));
  555. memset(&stop, 0, sizeof(struct mmc_command));
  556. mrq.cmd = &cmd;
  557. mrq.data = &data;
  558. mrq.stop = &stop;
  559. sg_init_one(&sg, test->buffer, blocks * blksz);
  560. mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
  561. mmc_test_prepare_broken_mrq(test, &mrq, write);
  562. mmc_wait_for_req(test->card->host, &mrq);
  563. mmc_test_wait_busy(test);
  564. return mmc_test_check_broken_result(test, &mrq);
  565. }
  566. /*
  567. * Does a complete transfer test where data is also validated
  568. *
  569. * Note: mmc_test_prepare() must have been done before this call
  570. */
  571. static int mmc_test_transfer(struct mmc_test_card *test,
  572. struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
  573. unsigned blocks, unsigned blksz, int write)
  574. {
  575. int ret, i;
  576. unsigned long flags;
  577. if (write) {
  578. for (i = 0;i < blocks * blksz;i++)
  579. test->scratch[i] = i;
  580. } else {
  581. memset(test->scratch, 0, BUFFER_SIZE);
  582. }
  583. local_irq_save(flags);
  584. sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
  585. local_irq_restore(flags);
  586. ret = mmc_test_set_blksize(test, blksz);
  587. if (ret)
  588. return ret;
  589. ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
  590. blocks, blksz, write);
  591. if (ret)
  592. return ret;
  593. if (write) {
  594. int sectors;
  595. ret = mmc_test_set_blksize(test, 512);
  596. if (ret)
  597. return ret;
  598. sectors = (blocks * blksz + 511) / 512;
  599. if ((sectors * 512) == (blocks * blksz))
  600. sectors++;
  601. if ((sectors * 512) > BUFFER_SIZE)
  602. return -EINVAL;
  603. memset(test->buffer, 0, sectors * 512);
  604. for (i = 0;i < sectors;i++) {
  605. ret = mmc_test_buffer_transfer(test,
  606. test->buffer + i * 512,
  607. dev_addr + i, 512, 0);
  608. if (ret)
  609. return ret;
  610. }
  611. for (i = 0;i < blocks * blksz;i++) {
  612. if (test->buffer[i] != (u8)i)
  613. return RESULT_FAIL;
  614. }
  615. for (;i < sectors * 512;i++) {
  616. if (test->buffer[i] != 0xDF)
  617. return RESULT_FAIL;
  618. }
  619. } else {
  620. local_irq_save(flags);
  621. sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
  622. local_irq_restore(flags);
  623. for (i = 0;i < blocks * blksz;i++) {
  624. if (test->scratch[i] != (u8)i)
  625. return RESULT_FAIL;
  626. }
  627. }
  628. return 0;
  629. }
  630. /*******************************************************************/
  631. /* Tests */
  632. /*******************************************************************/
  633. struct mmc_test_case {
  634. const char *name;
  635. int (*prepare)(struct mmc_test_card *);
  636. int (*run)(struct mmc_test_card *);
  637. int (*cleanup)(struct mmc_test_card *);
  638. };
  639. static int mmc_test_basic_write(struct mmc_test_card *test)
  640. {
  641. int ret;
  642. struct scatterlist sg;
  643. ret = mmc_test_set_blksize(test, 512);
  644. if (ret)
  645. return ret;
  646. sg_init_one(&sg, test->buffer, 512);
  647. ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
  648. if (ret)
  649. return ret;
  650. return 0;
  651. }
  652. static int mmc_test_basic_read(struct mmc_test_card *test)
  653. {
  654. int ret;
  655. struct scatterlist sg;
  656. ret = mmc_test_set_blksize(test, 512);
  657. if (ret)
  658. return ret;
  659. sg_init_one(&sg, test->buffer, 512);
  660. ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
  661. if (ret)
  662. return ret;
  663. return 0;
  664. }
  665. static int mmc_test_verify_write(struct mmc_test_card *test)
  666. {
  667. int ret;
  668. struct scatterlist sg;
  669. sg_init_one(&sg, test->buffer, 512);
  670. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
  671. if (ret)
  672. return ret;
  673. return 0;
  674. }
  675. static int mmc_test_verify_read(struct mmc_test_card *test)
  676. {
  677. int ret;
  678. struct scatterlist sg;
  679. sg_init_one(&sg, test->buffer, 512);
  680. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
  681. if (ret)
  682. return ret;
  683. return 0;
  684. }
  685. static int mmc_test_multi_write(struct mmc_test_card *test)
  686. {
  687. int ret;
  688. unsigned int size;
  689. struct scatterlist sg;
  690. if (test->card->host->max_blk_count == 1)
  691. return RESULT_UNSUP_HOST;
  692. size = PAGE_SIZE * 2;
  693. size = min(size, test->card->host->max_req_size);
  694. size = min(size, test->card->host->max_seg_size);
  695. size = min(size, test->card->host->max_blk_count * 512);
  696. if (size < 1024)
  697. return RESULT_UNSUP_HOST;
  698. sg_init_one(&sg, test->buffer, size);
  699. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
  700. if (ret)
  701. return ret;
  702. return 0;
  703. }
  704. static int mmc_test_multi_read(struct mmc_test_card *test)
  705. {
  706. int ret;
  707. unsigned int size;
  708. struct scatterlist sg;
  709. if (test->card->host->max_blk_count == 1)
  710. return RESULT_UNSUP_HOST;
  711. size = PAGE_SIZE * 2;
  712. size = min(size, test->card->host->max_req_size);
  713. size = min(size, test->card->host->max_seg_size);
  714. size = min(size, test->card->host->max_blk_count * 512);
  715. if (size < 1024)
  716. return RESULT_UNSUP_HOST;
  717. sg_init_one(&sg, test->buffer, size);
  718. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
  719. if (ret)
  720. return ret;
  721. return 0;
  722. }
  723. static int mmc_test_pow2_write(struct mmc_test_card *test)
  724. {
  725. int ret, i;
  726. struct scatterlist sg;
  727. if (!test->card->csd.write_partial)
  728. return RESULT_UNSUP_CARD;
  729. for (i = 1; i < 512;i <<= 1) {
  730. sg_init_one(&sg, test->buffer, i);
  731. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
  732. if (ret)
  733. return ret;
  734. }
  735. return 0;
  736. }
  737. static int mmc_test_pow2_read(struct mmc_test_card *test)
  738. {
  739. int ret, i;
  740. struct scatterlist sg;
  741. if (!test->card->csd.read_partial)
  742. return RESULT_UNSUP_CARD;
  743. for (i = 1; i < 512;i <<= 1) {
  744. sg_init_one(&sg, test->buffer, i);
  745. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
  746. if (ret)
  747. return ret;
  748. }
  749. return 0;
  750. }
  751. static int mmc_test_weird_write(struct mmc_test_card *test)
  752. {
  753. int ret, i;
  754. struct scatterlist sg;
  755. if (!test->card->csd.write_partial)
  756. return RESULT_UNSUP_CARD;
  757. for (i = 3; i < 512;i += 7) {
  758. sg_init_one(&sg, test->buffer, i);
  759. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
  760. if (ret)
  761. return ret;
  762. }
  763. return 0;
  764. }
  765. static int mmc_test_weird_read(struct mmc_test_card *test)
  766. {
  767. int ret, i;
  768. struct scatterlist sg;
  769. if (!test->card->csd.read_partial)
  770. return RESULT_UNSUP_CARD;
  771. for (i = 3; i < 512;i += 7) {
  772. sg_init_one(&sg, test->buffer, i);
  773. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
  774. if (ret)
  775. return ret;
  776. }
  777. return 0;
  778. }
  779. static int mmc_test_align_write(struct mmc_test_card *test)
  780. {
  781. int ret, i;
  782. struct scatterlist sg;
  783. for (i = 1;i < 4;i++) {
  784. sg_init_one(&sg, test->buffer + i, 512);
  785. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
  786. if (ret)
  787. return ret;
  788. }
  789. return 0;
  790. }
  791. static int mmc_test_align_read(struct mmc_test_card *test)
  792. {
  793. int ret, i;
  794. struct scatterlist sg;
  795. for (i = 1;i < 4;i++) {
  796. sg_init_one(&sg, test->buffer + i, 512);
  797. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
  798. if (ret)
  799. return ret;
  800. }
  801. return 0;
  802. }
  803. static int mmc_test_align_multi_write(struct mmc_test_card *test)
  804. {
  805. int ret, i;
  806. unsigned int size;
  807. struct scatterlist sg;
  808. if (test->card->host->max_blk_count == 1)
  809. return RESULT_UNSUP_HOST;
  810. size = PAGE_SIZE * 2;
  811. size = min(size, test->card->host->max_req_size);
  812. size = min(size, test->card->host->max_seg_size);
  813. size = min(size, test->card->host->max_blk_count * 512);
  814. if (size < 1024)
  815. return RESULT_UNSUP_HOST;
  816. for (i = 1;i < 4;i++) {
  817. sg_init_one(&sg, test->buffer + i, size);
  818. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
  819. if (ret)
  820. return ret;
  821. }
  822. return 0;
  823. }
  824. static int mmc_test_align_multi_read(struct mmc_test_card *test)
  825. {
  826. int ret, i;
  827. unsigned int size;
  828. struct scatterlist sg;
  829. if (test->card->host->max_blk_count == 1)
  830. return RESULT_UNSUP_HOST;
  831. size = PAGE_SIZE * 2;
  832. size = min(size, test->card->host->max_req_size);
  833. size = min(size, test->card->host->max_seg_size);
  834. size = min(size, test->card->host->max_blk_count * 512);
  835. if (size < 1024)
  836. return RESULT_UNSUP_HOST;
  837. for (i = 1;i < 4;i++) {
  838. sg_init_one(&sg, test->buffer + i, size);
  839. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
  840. if (ret)
  841. return ret;
  842. }
  843. return 0;
  844. }
  845. static int mmc_test_xfersize_write(struct mmc_test_card *test)
  846. {
  847. int ret;
  848. ret = mmc_test_set_blksize(test, 512);
  849. if (ret)
  850. return ret;
  851. ret = mmc_test_broken_transfer(test, 1, 512, 1);
  852. if (ret)
  853. return ret;
  854. return 0;
  855. }
  856. static int mmc_test_xfersize_read(struct mmc_test_card *test)
  857. {
  858. int ret;
  859. ret = mmc_test_set_blksize(test, 512);
  860. if (ret)
  861. return ret;
  862. ret = mmc_test_broken_transfer(test, 1, 512, 0);
  863. if (ret)
  864. return ret;
  865. return 0;
  866. }
  867. static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
  868. {
  869. int ret;
  870. if (test->card->host->max_blk_count == 1)
  871. return RESULT_UNSUP_HOST;
  872. ret = mmc_test_set_blksize(test, 512);
  873. if (ret)
  874. return ret;
  875. ret = mmc_test_broken_transfer(test, 2, 512, 1);
  876. if (ret)
  877. return ret;
  878. return 0;
  879. }
  880. static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
  881. {
  882. int ret;
  883. if (test->card->host->max_blk_count == 1)
  884. return RESULT_UNSUP_HOST;
  885. ret = mmc_test_set_blksize(test, 512);
  886. if (ret)
  887. return ret;
  888. ret = mmc_test_broken_transfer(test, 2, 512, 0);
  889. if (ret)
  890. return ret;
  891. return 0;
  892. }
  893. #ifdef CONFIG_HIGHMEM
  894. static int mmc_test_write_high(struct mmc_test_card *test)
  895. {
  896. int ret;
  897. struct scatterlist sg;
  898. sg_init_table(&sg, 1);
  899. sg_set_page(&sg, test->highmem, 512, 0);
  900. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
  901. if (ret)
  902. return ret;
  903. return 0;
  904. }
  905. static int mmc_test_read_high(struct mmc_test_card *test)
  906. {
  907. int ret;
  908. struct scatterlist sg;
  909. sg_init_table(&sg, 1);
  910. sg_set_page(&sg, test->highmem, 512, 0);
  911. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
  912. if (ret)
  913. return ret;
  914. return 0;
  915. }
  916. static int mmc_test_multi_write_high(struct mmc_test_card *test)
  917. {
  918. int ret;
  919. unsigned int size;
  920. struct scatterlist sg;
  921. if (test->card->host->max_blk_count == 1)
  922. return RESULT_UNSUP_HOST;
  923. size = PAGE_SIZE * 2;
  924. size = min(size, test->card->host->max_req_size);
  925. size = min(size, test->card->host->max_seg_size);
  926. size = min(size, test->card->host->max_blk_count * 512);
  927. if (size < 1024)
  928. return RESULT_UNSUP_HOST;
  929. sg_init_table(&sg, 1);
  930. sg_set_page(&sg, test->highmem, size, 0);
  931. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
  932. if (ret)
  933. return ret;
  934. return 0;
  935. }
  936. static int mmc_test_multi_read_high(struct mmc_test_card *test)
  937. {
  938. int ret;
  939. unsigned int size;
  940. struct scatterlist sg;
  941. if (test->card->host->max_blk_count == 1)
  942. return RESULT_UNSUP_HOST;
  943. size = PAGE_SIZE * 2;
  944. size = min(size, test->card->host->max_req_size);
  945. size = min(size, test->card->host->max_seg_size);
  946. size = min(size, test->card->host->max_blk_count * 512);
  947. if (size < 1024)
  948. return RESULT_UNSUP_HOST;
  949. sg_init_table(&sg, 1);
  950. sg_set_page(&sg, test->highmem, size, 0);
  951. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
  952. if (ret)
  953. return ret;
  954. return 0;
  955. }
  956. #else
  957. static int mmc_test_no_highmem(struct mmc_test_card *test)
  958. {
  959. printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
  960. mmc_hostname(test->card->host));
  961. return 0;
  962. }
  963. #endif /* CONFIG_HIGHMEM */
  964. /*
  965. * Map sz bytes so that it can be transferred.
  966. */
  967. static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
  968. int max_scatter)
  969. {
  970. struct mmc_test_area *t = &test->area;
  971. t->blocks = sz >> 9;
  972. if (max_scatter) {
  973. return mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
  974. t->max_segs, &t->sg_len);
  975. } else {
  976. return mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
  977. &t->sg_len);
  978. }
  979. }
  980. /*
  981. * Transfer bytes mapped by mmc_test_area_map().
  982. */
  983. static int mmc_test_area_transfer(struct mmc_test_card *test,
  984. unsigned int dev_addr, int write)
  985. {
  986. struct mmc_test_area *t = &test->area;
  987. return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
  988. t->blocks, 512, write);
  989. }
  990. /*
  991. * Map and transfer bytes.
  992. */
  993. static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
  994. unsigned int dev_addr, int write, int max_scatter,
  995. int timed)
  996. {
  997. struct timespec ts1, ts2;
  998. int ret;
  999. ret = mmc_test_area_map(test, sz, max_scatter);
  1000. if (ret)
  1001. return ret;
  1002. if (timed)
  1003. getnstimeofday(&ts1);
  1004. ret = mmc_test_area_transfer(test, dev_addr, write);
  1005. if (ret)
  1006. return ret;
  1007. if (timed)
  1008. getnstimeofday(&ts2);
  1009. if (timed)
  1010. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1011. return 0;
  1012. }
  1013. /*
  1014. * Write the test area entirely.
  1015. */
  1016. static int mmc_test_area_fill(struct mmc_test_card *test)
  1017. {
  1018. return mmc_test_area_io(test, test->area.max_sz, test->area.dev_addr,
  1019. 1, 0, 0);
  1020. }
  1021. /*
  1022. * Erase the test area entirely.
  1023. */
  1024. static int mmc_test_area_erase(struct mmc_test_card *test)
  1025. {
  1026. struct mmc_test_area *t = &test->area;
  1027. if (!mmc_can_erase(test->card))
  1028. return 0;
  1029. return mmc_erase(test->card, t->dev_addr, test->area.max_sz >> 9,
  1030. MMC_ERASE_ARG);
  1031. }
  1032. /*
  1033. * Cleanup struct mmc_test_area.
  1034. */
  1035. static int mmc_test_area_cleanup(struct mmc_test_card *test)
  1036. {
  1037. struct mmc_test_area *t = &test->area;
  1038. kfree(t->sg);
  1039. mmc_test_free_mem(t->mem);
  1040. return 0;
  1041. }
  1042. /*
  1043. * Initialize an area for testing large transfers. The size of the area is the
  1044. * preferred erase size which is a good size for optimal transfer speed. Note
  1045. * that is typically 4MiB for modern cards. The test area is set to the middle
  1046. * of the card because cards may have different charateristics at the front
  1047. * (for FAT file system optimization). Optionally, the area is erased (if the
  1048. * card supports it) which may improve write performance. Optionally, the area
  1049. * is filled with data for subsequent read tests.
  1050. */
  1051. static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
  1052. {
  1053. struct mmc_test_area *t = &test->area;
  1054. unsigned long min_sz = 64 * 1024;
  1055. int ret;
  1056. ret = mmc_test_set_blksize(test, 512);
  1057. if (ret)
  1058. return ret;
  1059. if (test->card->pref_erase > TEST_AREA_MAX_SIZE >> 9)
  1060. t->max_sz = TEST_AREA_MAX_SIZE;
  1061. else
  1062. t->max_sz = (unsigned long)test->card->pref_erase << 9;
  1063. /*
  1064. * Try to allocate enough memory for the whole area. Less is OK
  1065. * because the same memory can be mapped into the scatterlist more than
  1066. * once.
  1067. */
  1068. t->mem = mmc_test_alloc_mem(min_sz, t->max_sz);
  1069. if (!t->mem)
  1070. return -ENOMEM;
  1071. t->max_segs = DIV_ROUND_UP(t->max_sz, PAGE_SIZE);
  1072. t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
  1073. if (!t->sg) {
  1074. ret = -ENOMEM;
  1075. goto out_free;
  1076. }
  1077. t->dev_addr = mmc_test_capacity(test->card) / 2;
  1078. t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
  1079. if (erase) {
  1080. ret = mmc_test_area_erase(test);
  1081. if (ret)
  1082. goto out_free;
  1083. }
  1084. if (fill) {
  1085. ret = mmc_test_area_fill(test);
  1086. if (ret)
  1087. goto out_free;
  1088. }
  1089. return 0;
  1090. out_free:
  1091. mmc_test_area_cleanup(test);
  1092. return ret;
  1093. }
  1094. /*
  1095. * Prepare for large transfers. Do not erase the test area.
  1096. */
  1097. static int mmc_test_area_prepare(struct mmc_test_card *test)
  1098. {
  1099. return mmc_test_area_init(test, 0, 0);
  1100. }
  1101. /*
  1102. * Prepare for large transfers. Do erase the test area.
  1103. */
  1104. static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
  1105. {
  1106. return mmc_test_area_init(test, 1, 0);
  1107. }
  1108. /*
  1109. * Prepare for large transfers. Erase and fill the test area.
  1110. */
  1111. static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
  1112. {
  1113. return mmc_test_area_init(test, 1, 1);
  1114. }
  1115. /*
  1116. * Test best-case performance. Best-case performance is expected from
  1117. * a single large transfer.
  1118. *
  1119. * An additional option (max_scatter) allows the measurement of the same
  1120. * transfer but with no contiguous pages in the scatter list. This tests
  1121. * the efficiency of DMA to handle scattered pages.
  1122. */
  1123. static int mmc_test_best_performance(struct mmc_test_card *test, int write,
  1124. int max_scatter)
  1125. {
  1126. return mmc_test_area_io(test, test->area.max_sz, test->area.dev_addr,
  1127. write, max_scatter, 1);
  1128. }
  1129. /*
  1130. * Best-case read performance.
  1131. */
  1132. static int mmc_test_best_read_performance(struct mmc_test_card *test)
  1133. {
  1134. return mmc_test_best_performance(test, 0, 0);
  1135. }
  1136. /*
  1137. * Best-case write performance.
  1138. */
  1139. static int mmc_test_best_write_performance(struct mmc_test_card *test)
  1140. {
  1141. return mmc_test_best_performance(test, 1, 0);
  1142. }
  1143. /*
  1144. * Best-case read performance into scattered pages.
  1145. */
  1146. static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
  1147. {
  1148. return mmc_test_best_performance(test, 0, 1);
  1149. }
  1150. /*
  1151. * Best-case write performance from scattered pages.
  1152. */
  1153. static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
  1154. {
  1155. return mmc_test_best_performance(test, 1, 1);
  1156. }
  1157. /*
  1158. * Single read performance by transfer size.
  1159. */
  1160. static int mmc_test_profile_read_perf(struct mmc_test_card *test)
  1161. {
  1162. unsigned long sz;
  1163. unsigned int dev_addr;
  1164. int ret;
  1165. for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
  1166. dev_addr = test->area.dev_addr + (sz >> 9);
  1167. ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
  1168. if (ret)
  1169. return ret;
  1170. }
  1171. dev_addr = test->area.dev_addr;
  1172. return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
  1173. }
  1174. /*
  1175. * Single write performance by transfer size.
  1176. */
  1177. static int mmc_test_profile_write_perf(struct mmc_test_card *test)
  1178. {
  1179. unsigned long sz;
  1180. unsigned int dev_addr;
  1181. int ret;
  1182. ret = mmc_test_area_erase(test);
  1183. if (ret)
  1184. return ret;
  1185. for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
  1186. dev_addr = test->area.dev_addr + (sz >> 9);
  1187. ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
  1188. if (ret)
  1189. return ret;
  1190. }
  1191. ret = mmc_test_area_erase(test);
  1192. if (ret)
  1193. return ret;
  1194. dev_addr = test->area.dev_addr;
  1195. return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
  1196. }
  1197. /*
  1198. * Single trim performance by transfer size.
  1199. */
  1200. static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
  1201. {
  1202. unsigned long sz;
  1203. unsigned int dev_addr;
  1204. struct timespec ts1, ts2;
  1205. int ret;
  1206. if (!mmc_can_trim(test->card))
  1207. return RESULT_UNSUP_CARD;
  1208. if (!mmc_can_erase(test->card))
  1209. return RESULT_UNSUP_HOST;
  1210. for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
  1211. dev_addr = test->area.dev_addr + (sz >> 9);
  1212. getnstimeofday(&ts1);
  1213. ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
  1214. if (ret)
  1215. return ret;
  1216. getnstimeofday(&ts2);
  1217. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1218. }
  1219. dev_addr = test->area.dev_addr;
  1220. getnstimeofday(&ts1);
  1221. ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
  1222. if (ret)
  1223. return ret;
  1224. getnstimeofday(&ts2);
  1225. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1226. return 0;
  1227. }
  1228. /*
  1229. * Consecutive read performance by transfer size.
  1230. */
  1231. static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
  1232. {
  1233. unsigned long sz;
  1234. unsigned int dev_addr, i, cnt;
  1235. struct timespec ts1, ts2;
  1236. int ret;
  1237. for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
  1238. cnt = test->area.max_sz / sz;
  1239. dev_addr = test->area.dev_addr;
  1240. getnstimeofday(&ts1);
  1241. for (i = 0; i < cnt; i++) {
  1242. ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
  1243. if (ret)
  1244. return ret;
  1245. dev_addr += (sz >> 9);
  1246. }
  1247. getnstimeofday(&ts2);
  1248. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1249. }
  1250. return 0;
  1251. }
  1252. /*
  1253. * Consecutive write performance by transfer size.
  1254. */
  1255. static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
  1256. {
  1257. unsigned long sz;
  1258. unsigned int dev_addr, i, cnt;
  1259. struct timespec ts1, ts2;
  1260. int ret;
  1261. for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
  1262. ret = mmc_test_area_erase(test);
  1263. if (ret)
  1264. return ret;
  1265. cnt = test->area.max_sz / sz;
  1266. dev_addr = test->area.dev_addr;
  1267. getnstimeofday(&ts1);
  1268. for (i = 0; i < cnt; i++) {
  1269. ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
  1270. if (ret)
  1271. return ret;
  1272. dev_addr += (sz >> 9);
  1273. }
  1274. getnstimeofday(&ts2);
  1275. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1276. }
  1277. return 0;
  1278. }
  1279. /*
  1280. * Consecutive trim performance by transfer size.
  1281. */
  1282. static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
  1283. {
  1284. unsigned long sz;
  1285. unsigned int dev_addr, i, cnt;
  1286. struct timespec ts1, ts2;
  1287. int ret;
  1288. if (!mmc_can_trim(test->card))
  1289. return RESULT_UNSUP_CARD;
  1290. if (!mmc_can_erase(test->card))
  1291. return RESULT_UNSUP_HOST;
  1292. for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
  1293. ret = mmc_test_area_erase(test);
  1294. if (ret)
  1295. return ret;
  1296. ret = mmc_test_area_fill(test);
  1297. if (ret)
  1298. return ret;
  1299. cnt = test->area.max_sz / sz;
  1300. dev_addr = test->area.dev_addr;
  1301. getnstimeofday(&ts1);
  1302. for (i = 0; i < cnt; i++) {
  1303. ret = mmc_erase(test->card, dev_addr, sz >> 9,
  1304. MMC_TRIM_ARG);
  1305. if (ret)
  1306. return ret;
  1307. dev_addr += (sz >> 9);
  1308. }
  1309. getnstimeofday(&ts2);
  1310. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1311. }
  1312. return 0;
  1313. }
  1314. static const struct mmc_test_case mmc_test_cases[] = {
  1315. {
  1316. .name = "Basic write (no data verification)",
  1317. .run = mmc_test_basic_write,
  1318. },
  1319. {
  1320. .name = "Basic read (no data verification)",
  1321. .run = mmc_test_basic_read,
  1322. },
  1323. {
  1324. .name = "Basic write (with data verification)",
  1325. .prepare = mmc_test_prepare_write,
  1326. .run = mmc_test_verify_write,
  1327. .cleanup = mmc_test_cleanup,
  1328. },
  1329. {
  1330. .name = "Basic read (with data verification)",
  1331. .prepare = mmc_test_prepare_read,
  1332. .run = mmc_test_verify_read,
  1333. .cleanup = mmc_test_cleanup,
  1334. },
  1335. {
  1336. .name = "Multi-block write",
  1337. .prepare = mmc_test_prepare_write,
  1338. .run = mmc_test_multi_write,
  1339. .cleanup = mmc_test_cleanup,
  1340. },
  1341. {
  1342. .name = "Multi-block read",
  1343. .prepare = mmc_test_prepare_read,
  1344. .run = mmc_test_multi_read,
  1345. .cleanup = mmc_test_cleanup,
  1346. },
  1347. {
  1348. .name = "Power of two block writes",
  1349. .prepare = mmc_test_prepare_write,
  1350. .run = mmc_test_pow2_write,
  1351. .cleanup = mmc_test_cleanup,
  1352. },
  1353. {
  1354. .name = "Power of two block reads",
  1355. .prepare = mmc_test_prepare_read,
  1356. .run = mmc_test_pow2_read,
  1357. .cleanup = mmc_test_cleanup,
  1358. },
  1359. {
  1360. .name = "Weird sized block writes",
  1361. .prepare = mmc_test_prepare_write,
  1362. .run = mmc_test_weird_write,
  1363. .cleanup = mmc_test_cleanup,
  1364. },
  1365. {
  1366. .name = "Weird sized block reads",
  1367. .prepare = mmc_test_prepare_read,
  1368. .run = mmc_test_weird_read,
  1369. .cleanup = mmc_test_cleanup,
  1370. },
  1371. {
  1372. .name = "Badly aligned write",
  1373. .prepare = mmc_test_prepare_write,
  1374. .run = mmc_test_align_write,
  1375. .cleanup = mmc_test_cleanup,
  1376. },
  1377. {
  1378. .name = "Badly aligned read",
  1379. .prepare = mmc_test_prepare_read,
  1380. .run = mmc_test_align_read,
  1381. .cleanup = mmc_test_cleanup,
  1382. },
  1383. {
  1384. .name = "Badly aligned multi-block write",
  1385. .prepare = mmc_test_prepare_write,
  1386. .run = mmc_test_align_multi_write,
  1387. .cleanup = mmc_test_cleanup,
  1388. },
  1389. {
  1390. .name = "Badly aligned multi-block read",
  1391. .prepare = mmc_test_prepare_read,
  1392. .run = mmc_test_align_multi_read,
  1393. .cleanup = mmc_test_cleanup,
  1394. },
  1395. {
  1396. .name = "Correct xfer_size at write (start failure)",
  1397. .run = mmc_test_xfersize_write,
  1398. },
  1399. {
  1400. .name = "Correct xfer_size at read (start failure)",
  1401. .run = mmc_test_xfersize_read,
  1402. },
  1403. {
  1404. .name = "Correct xfer_size at write (midway failure)",
  1405. .run = mmc_test_multi_xfersize_write,
  1406. },
  1407. {
  1408. .name = "Correct xfer_size at read (midway failure)",
  1409. .run = mmc_test_multi_xfersize_read,
  1410. },
  1411. #ifdef CONFIG_HIGHMEM
  1412. {
  1413. .name = "Highmem write",
  1414. .prepare = mmc_test_prepare_write,
  1415. .run = mmc_test_write_high,
  1416. .cleanup = mmc_test_cleanup,
  1417. },
  1418. {
  1419. .name = "Highmem read",
  1420. .prepare = mmc_test_prepare_read,
  1421. .run = mmc_test_read_high,
  1422. .cleanup = mmc_test_cleanup,
  1423. },
  1424. {
  1425. .name = "Multi-block highmem write",
  1426. .prepare = mmc_test_prepare_write,
  1427. .run = mmc_test_multi_write_high,
  1428. .cleanup = mmc_test_cleanup,
  1429. },
  1430. {
  1431. .name = "Multi-block highmem read",
  1432. .prepare = mmc_test_prepare_read,
  1433. .run = mmc_test_multi_read_high,
  1434. .cleanup = mmc_test_cleanup,
  1435. },
  1436. #else
  1437. {
  1438. .name = "Highmem write",
  1439. .run = mmc_test_no_highmem,
  1440. },
  1441. {
  1442. .name = "Highmem read",
  1443. .run = mmc_test_no_highmem,
  1444. },
  1445. {
  1446. .name = "Multi-block highmem write",
  1447. .run = mmc_test_no_highmem,
  1448. },
  1449. {
  1450. .name = "Multi-block highmem read",
  1451. .run = mmc_test_no_highmem,
  1452. },
  1453. #endif /* CONFIG_HIGHMEM */
  1454. {
  1455. .name = "Best-case read performance",
  1456. .prepare = mmc_test_area_prepare_fill,
  1457. .run = mmc_test_best_read_performance,
  1458. .cleanup = mmc_test_area_cleanup,
  1459. },
  1460. {
  1461. .name = "Best-case write performance",
  1462. .prepare = mmc_test_area_prepare_erase,
  1463. .run = mmc_test_best_write_performance,
  1464. .cleanup = mmc_test_area_cleanup,
  1465. },
  1466. {
  1467. .name = "Best-case read performance into scattered pages",
  1468. .prepare = mmc_test_area_prepare_fill,
  1469. .run = mmc_test_best_read_perf_max_scatter,
  1470. .cleanup = mmc_test_area_cleanup,
  1471. },
  1472. {
  1473. .name = "Best-case write performance from scattered pages",
  1474. .prepare = mmc_test_area_prepare_erase,
  1475. .run = mmc_test_best_write_perf_max_scatter,
  1476. .cleanup = mmc_test_area_cleanup,
  1477. },
  1478. {
  1479. .name = "Single read performance by transfer size",
  1480. .prepare = mmc_test_area_prepare_fill,
  1481. .run = mmc_test_profile_read_perf,
  1482. .cleanup = mmc_test_area_cleanup,
  1483. },
  1484. {
  1485. .name = "Single write performance by transfer size",
  1486. .prepare = mmc_test_area_prepare,
  1487. .run = mmc_test_profile_write_perf,
  1488. .cleanup = mmc_test_area_cleanup,
  1489. },
  1490. {
  1491. .name = "Single trim performance by transfer size",
  1492. .prepare = mmc_test_area_prepare_fill,
  1493. .run = mmc_test_profile_trim_perf,
  1494. .cleanup = mmc_test_area_cleanup,
  1495. },
  1496. {
  1497. .name = "Consecutive read performance by transfer size",
  1498. .prepare = mmc_test_area_prepare_fill,
  1499. .run = mmc_test_profile_seq_read_perf,
  1500. .cleanup = mmc_test_area_cleanup,
  1501. },
  1502. {
  1503. .name = "Consecutive write performance by transfer size",
  1504. .prepare = mmc_test_area_prepare,
  1505. .run = mmc_test_profile_seq_write_perf,
  1506. .cleanup = mmc_test_area_cleanup,
  1507. },
  1508. {
  1509. .name = "Consecutive trim performance by transfer size",
  1510. .prepare = mmc_test_area_prepare,
  1511. .run = mmc_test_profile_seq_trim_perf,
  1512. .cleanup = mmc_test_area_cleanup,
  1513. },
  1514. };
  1515. static DEFINE_MUTEX(mmc_test_lock);
  1516. static void mmc_test_run(struct mmc_test_card *test, int testcase)
  1517. {
  1518. int i, ret;
  1519. printk(KERN_INFO "%s: Starting tests of card %s...\n",
  1520. mmc_hostname(test->card->host), mmc_card_id(test->card));
  1521. mmc_claim_host(test->card->host);
  1522. for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
  1523. if (testcase && ((i + 1) != testcase))
  1524. continue;
  1525. printk(KERN_INFO "%s: Test case %d. %s...\n",
  1526. mmc_hostname(test->card->host), i + 1,
  1527. mmc_test_cases[i].name);
  1528. if (mmc_test_cases[i].prepare) {
  1529. ret = mmc_test_cases[i].prepare(test);
  1530. if (ret) {
  1531. printk(KERN_INFO "%s: Result: Prepare "
  1532. "stage failed! (%d)\n",
  1533. mmc_hostname(test->card->host),
  1534. ret);
  1535. continue;
  1536. }
  1537. }
  1538. ret = mmc_test_cases[i].run(test);
  1539. switch (ret) {
  1540. case RESULT_OK:
  1541. printk(KERN_INFO "%s: Result: OK\n",
  1542. mmc_hostname(test->card->host));
  1543. break;
  1544. case RESULT_FAIL:
  1545. printk(KERN_INFO "%s: Result: FAILED\n",
  1546. mmc_hostname(test->card->host));
  1547. break;
  1548. case RESULT_UNSUP_HOST:
  1549. printk(KERN_INFO "%s: Result: UNSUPPORTED "
  1550. "(by host)\n",
  1551. mmc_hostname(test->card->host));
  1552. break;
  1553. case RESULT_UNSUP_CARD:
  1554. printk(KERN_INFO "%s: Result: UNSUPPORTED "
  1555. "(by card)\n",
  1556. mmc_hostname(test->card->host));
  1557. break;
  1558. default:
  1559. printk(KERN_INFO "%s: Result: ERROR (%d)\n",
  1560. mmc_hostname(test->card->host), ret);
  1561. }
  1562. if (mmc_test_cases[i].cleanup) {
  1563. ret = mmc_test_cases[i].cleanup(test);
  1564. if (ret) {
  1565. printk(KERN_INFO "%s: Warning: Cleanup "
  1566. "stage failed! (%d)\n",
  1567. mmc_hostname(test->card->host),
  1568. ret);
  1569. }
  1570. }
  1571. }
  1572. mmc_release_host(test->card->host);
  1573. printk(KERN_INFO "%s: Tests completed.\n",
  1574. mmc_hostname(test->card->host));
  1575. }
  1576. static ssize_t mmc_test_show(struct device *dev,
  1577. struct device_attribute *attr, char *buf)
  1578. {
  1579. mutex_lock(&mmc_test_lock);
  1580. mutex_unlock(&mmc_test_lock);
  1581. return 0;
  1582. }
  1583. static ssize_t mmc_test_store(struct device *dev,
  1584. struct device_attribute *attr, const char *buf, size_t count)
  1585. {
  1586. struct mmc_card *card;
  1587. struct mmc_test_card *test;
  1588. int testcase;
  1589. card = container_of(dev, struct mmc_card, dev);
  1590. testcase = simple_strtol(buf, NULL, 10);
  1591. test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
  1592. if (!test)
  1593. return -ENOMEM;
  1594. test->card = card;
  1595. test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
  1596. #ifdef CONFIG_HIGHMEM
  1597. test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
  1598. #endif
  1599. #ifdef CONFIG_HIGHMEM
  1600. if (test->buffer && test->highmem) {
  1601. #else
  1602. if (test->buffer) {
  1603. #endif
  1604. mutex_lock(&mmc_test_lock);
  1605. mmc_test_run(test, testcase);
  1606. mutex_unlock(&mmc_test_lock);
  1607. }
  1608. #ifdef CONFIG_HIGHMEM
  1609. __free_pages(test->highmem, BUFFER_ORDER);
  1610. #endif
  1611. kfree(test->buffer);
  1612. kfree(test);
  1613. return count;
  1614. }
  1615. static DEVICE_ATTR(test, S_IWUSR | S_IRUGO, mmc_test_show, mmc_test_store);
  1616. static int mmc_test_probe(struct mmc_card *card)
  1617. {
  1618. int ret;
  1619. if ((card->type != MMC_TYPE_MMC) && (card->type != MMC_TYPE_SD))
  1620. return -ENODEV;
  1621. ret = device_create_file(&card->dev, &dev_attr_test);
  1622. if (ret)
  1623. return ret;
  1624. dev_info(&card->dev, "Card claimed for testing.\n");
  1625. return 0;
  1626. }
  1627. static void mmc_test_remove(struct mmc_card *card)
  1628. {
  1629. device_remove_file(&card->dev, &dev_attr_test);
  1630. }
  1631. static struct mmc_driver mmc_driver = {
  1632. .drv = {
  1633. .name = "mmc_test",
  1634. },
  1635. .probe = mmc_test_probe,
  1636. .remove = mmc_test_remove,
  1637. };
  1638. static int __init mmc_test_init(void)
  1639. {
  1640. return mmc_register_driver(&mmc_driver);
  1641. }
  1642. static void __exit mmc_test_exit(void)
  1643. {
  1644. mmc_unregister_driver(&mmc_driver);
  1645. }
  1646. module_init(mmc_test_init);
  1647. module_exit(mmc_test_exit);
  1648. MODULE_LICENSE("GPL");
  1649. MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
  1650. MODULE_AUTHOR("Pierre Ossman");