mmc_test.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  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. #include <linux/list.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/seq_file.h>
  22. #define RESULT_OK 0
  23. #define RESULT_FAIL 1
  24. #define RESULT_UNSUP_HOST 2
  25. #define RESULT_UNSUP_CARD 3
  26. #define BUFFER_ORDER 2
  27. #define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
  28. /*
  29. * Limit the test area size to the maximum MMC HC erase group size. Note that
  30. * the maximum SD allocation unit size is just 4MiB.
  31. */
  32. #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
  33. /**
  34. * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
  35. * @page: first page in the allocation
  36. * @order: order of the number of pages allocated
  37. */
  38. struct mmc_test_pages {
  39. struct page *page;
  40. unsigned int order;
  41. };
  42. /**
  43. * struct mmc_test_mem - allocated memory.
  44. * @arr: array of allocations
  45. * @cnt: number of allocations
  46. */
  47. struct mmc_test_mem {
  48. struct mmc_test_pages *arr;
  49. unsigned int cnt;
  50. };
  51. /**
  52. * struct mmc_test_area - information for performance tests.
  53. * @max_sz: test area size (in bytes)
  54. * @dev_addr: address on card at which to do performance tests
  55. * @max_tfr: maximum transfer size allowed by driver (in bytes)
  56. * @max_segs: maximum segments allowed by driver in scatterlist @sg
  57. * @max_seg_sz: maximum segment size allowed by driver
  58. * @blocks: number of (512 byte) blocks currently mapped by @sg
  59. * @sg_len: length of currently mapped scatterlist @sg
  60. * @mem: allocated memory
  61. * @sg: scatterlist
  62. */
  63. struct mmc_test_area {
  64. unsigned long max_sz;
  65. unsigned int dev_addr;
  66. unsigned int max_tfr;
  67. unsigned int max_segs;
  68. unsigned int max_seg_sz;
  69. unsigned int blocks;
  70. unsigned int sg_len;
  71. struct mmc_test_mem *mem;
  72. struct scatterlist *sg;
  73. };
  74. /**
  75. * struct mmc_test_transfer_result - transfer results for performance tests.
  76. * @link: double-linked list
  77. * @count: amount of group of sectors to check
  78. * @sectors: amount of sectors to check in one group
  79. * @ts: time values of transfer
  80. * @rate: calculated transfer rate
  81. * @iops: I/O operations per second (times 100)
  82. */
  83. struct mmc_test_transfer_result {
  84. struct list_head link;
  85. unsigned int count;
  86. unsigned int sectors;
  87. struct timespec ts;
  88. unsigned int rate;
  89. unsigned int iops;
  90. };
  91. /**
  92. * struct mmc_test_general_result - results for tests.
  93. * @link: double-linked list
  94. * @card: card under test
  95. * @testcase: number of test case
  96. * @result: result of test run
  97. * @tr_lst: transfer measurements if any as mmc_test_transfer_result
  98. */
  99. struct mmc_test_general_result {
  100. struct list_head link;
  101. struct mmc_card *card;
  102. int testcase;
  103. int result;
  104. struct list_head tr_lst;
  105. };
  106. /**
  107. * struct mmc_test_dbgfs_file - debugfs related file.
  108. * @link: double-linked list
  109. * @card: card under test
  110. * @file: file created under debugfs
  111. */
  112. struct mmc_test_dbgfs_file {
  113. struct list_head link;
  114. struct mmc_card *card;
  115. struct dentry *file;
  116. };
  117. /**
  118. * struct mmc_test_card - test information.
  119. * @card: card under test
  120. * @scratch: transfer buffer
  121. * @buffer: transfer buffer
  122. * @highmem: buffer for highmem tests
  123. * @area: information for performance tests
  124. * @gr: pointer to results of current testcase
  125. */
  126. struct mmc_test_card {
  127. struct mmc_card *card;
  128. u8 scratch[BUFFER_SIZE];
  129. u8 *buffer;
  130. #ifdef CONFIG_HIGHMEM
  131. struct page *highmem;
  132. #endif
  133. struct mmc_test_area area;
  134. struct mmc_test_general_result *gr;
  135. };
  136. /*******************************************************************/
  137. /* General helper functions */
  138. /*******************************************************************/
  139. /*
  140. * Configure correct block size in card
  141. */
  142. static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
  143. {
  144. return mmc_set_blocklen(test->card, size);
  145. }
  146. /*
  147. * Fill in the mmc_request structure given a set of transfer parameters.
  148. */
  149. static void mmc_test_prepare_mrq(struct mmc_test_card *test,
  150. struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
  151. unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
  152. {
  153. BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
  154. if (blocks > 1) {
  155. mrq->cmd->opcode = write ?
  156. MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
  157. } else {
  158. mrq->cmd->opcode = write ?
  159. MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
  160. }
  161. mrq->cmd->arg = dev_addr;
  162. if (!mmc_card_blockaddr(test->card))
  163. mrq->cmd->arg <<= 9;
  164. mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  165. if (blocks == 1)
  166. mrq->stop = NULL;
  167. else {
  168. mrq->stop->opcode = MMC_STOP_TRANSMISSION;
  169. mrq->stop->arg = 0;
  170. mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  171. }
  172. mrq->data->blksz = blksz;
  173. mrq->data->blocks = blocks;
  174. mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
  175. mrq->data->sg = sg;
  176. mrq->data->sg_len = sg_len;
  177. mmc_set_data_timeout(mrq->data, test->card);
  178. }
  179. static int mmc_test_busy(struct mmc_command *cmd)
  180. {
  181. return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
  182. (R1_CURRENT_STATE(cmd->resp[0]) == 7);
  183. }
  184. /*
  185. * Wait for the card to finish the busy state
  186. */
  187. static int mmc_test_wait_busy(struct mmc_test_card *test)
  188. {
  189. int ret, busy;
  190. struct mmc_command cmd = {0};
  191. busy = 0;
  192. do {
  193. memset(&cmd, 0, sizeof(struct mmc_command));
  194. cmd.opcode = MMC_SEND_STATUS;
  195. cmd.arg = test->card->rca << 16;
  196. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  197. ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
  198. if (ret)
  199. break;
  200. if (!busy && mmc_test_busy(&cmd)) {
  201. busy = 1;
  202. if (test->card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
  203. printk(KERN_INFO "%s: Warning: Host did not "
  204. "wait for busy state to end.\n",
  205. mmc_hostname(test->card->host));
  206. }
  207. } while (mmc_test_busy(&cmd));
  208. return ret;
  209. }
  210. /*
  211. * Transfer a single sector of kernel addressable data
  212. */
  213. static int mmc_test_buffer_transfer(struct mmc_test_card *test,
  214. u8 *buffer, unsigned addr, unsigned blksz, int write)
  215. {
  216. int ret;
  217. struct mmc_request mrq = {0};
  218. struct mmc_command cmd = {0};
  219. struct mmc_command stop = {0};
  220. struct mmc_data data = {0};
  221. struct scatterlist sg;
  222. mrq.cmd = &cmd;
  223. mrq.data = &data;
  224. mrq.stop = &stop;
  225. sg_init_one(&sg, buffer, blksz);
  226. mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
  227. mmc_wait_for_req(test->card->host, &mrq);
  228. if (cmd.error)
  229. return cmd.error;
  230. if (data.error)
  231. return data.error;
  232. ret = mmc_test_wait_busy(test);
  233. if (ret)
  234. return ret;
  235. return 0;
  236. }
  237. static void mmc_test_free_mem(struct mmc_test_mem *mem)
  238. {
  239. if (!mem)
  240. return;
  241. while (mem->cnt--)
  242. __free_pages(mem->arr[mem->cnt].page,
  243. mem->arr[mem->cnt].order);
  244. kfree(mem->arr);
  245. kfree(mem);
  246. }
  247. /*
  248. * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
  249. * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
  250. * not exceed a maximum number of segments and try not to make segments much
  251. * bigger than maximum segment size.
  252. */
  253. static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
  254. unsigned long max_sz,
  255. unsigned int max_segs,
  256. unsigned int max_seg_sz)
  257. {
  258. unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
  259. unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
  260. unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
  261. unsigned long page_cnt = 0;
  262. unsigned long limit = nr_free_buffer_pages() >> 4;
  263. struct mmc_test_mem *mem;
  264. if (max_page_cnt > limit)
  265. max_page_cnt = limit;
  266. if (min_page_cnt > max_page_cnt)
  267. min_page_cnt = max_page_cnt;
  268. if (max_seg_page_cnt > max_page_cnt)
  269. max_seg_page_cnt = max_page_cnt;
  270. if (max_segs > max_page_cnt)
  271. max_segs = max_page_cnt;
  272. mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
  273. if (!mem)
  274. return NULL;
  275. mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_segs,
  276. GFP_KERNEL);
  277. if (!mem->arr)
  278. goto out_free;
  279. while (max_page_cnt) {
  280. struct page *page;
  281. unsigned int order;
  282. gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
  283. __GFP_NORETRY;
  284. order = get_order(max_seg_page_cnt << PAGE_SHIFT);
  285. while (1) {
  286. page = alloc_pages(flags, order);
  287. if (page || !order)
  288. break;
  289. order -= 1;
  290. }
  291. if (!page) {
  292. if (page_cnt < min_page_cnt)
  293. goto out_free;
  294. break;
  295. }
  296. mem->arr[mem->cnt].page = page;
  297. mem->arr[mem->cnt].order = order;
  298. mem->cnt += 1;
  299. if (max_page_cnt <= (1UL << order))
  300. break;
  301. max_page_cnt -= 1UL << order;
  302. page_cnt += 1UL << order;
  303. if (mem->cnt >= max_segs) {
  304. if (page_cnt < min_page_cnt)
  305. goto out_free;
  306. break;
  307. }
  308. }
  309. return mem;
  310. out_free:
  311. mmc_test_free_mem(mem);
  312. return NULL;
  313. }
  314. /*
  315. * Map memory into a scatterlist. Optionally allow the same memory to be
  316. * mapped more than once.
  317. */
  318. static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
  319. struct scatterlist *sglist, int repeat,
  320. unsigned int max_segs, unsigned int max_seg_sz,
  321. unsigned int *sg_len)
  322. {
  323. struct scatterlist *sg = NULL;
  324. unsigned int i;
  325. sg_init_table(sglist, max_segs);
  326. *sg_len = 0;
  327. do {
  328. for (i = 0; i < mem->cnt; i++) {
  329. unsigned long len = PAGE_SIZE << mem->arr[i].order;
  330. if (len > sz)
  331. len = sz;
  332. if (len > max_seg_sz)
  333. len = max_seg_sz;
  334. if (sg)
  335. sg = sg_next(sg);
  336. else
  337. sg = sglist;
  338. if (!sg)
  339. return -EINVAL;
  340. sg_set_page(sg, mem->arr[i].page, len, 0);
  341. sz -= len;
  342. *sg_len += 1;
  343. if (!sz)
  344. break;
  345. }
  346. } while (sz && repeat);
  347. if (sz)
  348. return -EINVAL;
  349. if (sg)
  350. sg_mark_end(sg);
  351. return 0;
  352. }
  353. /*
  354. * Map memory into a scatterlist so that no pages are contiguous. Allow the
  355. * same memory to be mapped more than once.
  356. */
  357. static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
  358. unsigned long sz,
  359. struct scatterlist *sglist,
  360. unsigned int max_segs,
  361. unsigned int max_seg_sz,
  362. unsigned int *sg_len)
  363. {
  364. struct scatterlist *sg = NULL;
  365. unsigned int i = mem->cnt, cnt;
  366. unsigned long len;
  367. void *base, *addr, *last_addr = NULL;
  368. sg_init_table(sglist, max_segs);
  369. *sg_len = 0;
  370. while (sz) {
  371. base = page_address(mem->arr[--i].page);
  372. cnt = 1 << mem->arr[i].order;
  373. while (sz && cnt) {
  374. addr = base + PAGE_SIZE * --cnt;
  375. if (last_addr && last_addr + PAGE_SIZE == addr)
  376. continue;
  377. last_addr = addr;
  378. len = PAGE_SIZE;
  379. if (len > max_seg_sz)
  380. len = max_seg_sz;
  381. if (len > sz)
  382. len = sz;
  383. if (sg)
  384. sg = sg_next(sg);
  385. else
  386. sg = sglist;
  387. if (!sg)
  388. return -EINVAL;
  389. sg_set_page(sg, virt_to_page(addr), len, 0);
  390. sz -= len;
  391. *sg_len += 1;
  392. }
  393. if (i == 0)
  394. i = mem->cnt;
  395. }
  396. if (sg)
  397. sg_mark_end(sg);
  398. return 0;
  399. }
  400. /*
  401. * Calculate transfer rate in bytes per second.
  402. */
  403. static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
  404. {
  405. uint64_t ns;
  406. ns = ts->tv_sec;
  407. ns *= 1000000000;
  408. ns += ts->tv_nsec;
  409. bytes *= 1000000000;
  410. while (ns > UINT_MAX) {
  411. bytes >>= 1;
  412. ns >>= 1;
  413. }
  414. if (!ns)
  415. return 0;
  416. do_div(bytes, (uint32_t)ns);
  417. return bytes;
  418. }
  419. /*
  420. * Save transfer results for future usage
  421. */
  422. static void mmc_test_save_transfer_result(struct mmc_test_card *test,
  423. unsigned int count, unsigned int sectors, struct timespec ts,
  424. unsigned int rate, unsigned int iops)
  425. {
  426. struct mmc_test_transfer_result *tr;
  427. if (!test->gr)
  428. return;
  429. tr = kmalloc(sizeof(struct mmc_test_transfer_result), GFP_KERNEL);
  430. if (!tr)
  431. return;
  432. tr->count = count;
  433. tr->sectors = sectors;
  434. tr->ts = ts;
  435. tr->rate = rate;
  436. tr->iops = iops;
  437. list_add_tail(&tr->link, &test->gr->tr_lst);
  438. }
  439. /*
  440. * Print the transfer rate.
  441. */
  442. static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
  443. struct timespec *ts1, struct timespec *ts2)
  444. {
  445. unsigned int rate, iops, sectors = bytes >> 9;
  446. struct timespec ts;
  447. ts = timespec_sub(*ts2, *ts1);
  448. rate = mmc_test_rate(bytes, &ts);
  449. iops = mmc_test_rate(100, &ts); /* I/O ops per sec x 100 */
  450. printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
  451. "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
  452. mmc_hostname(test->card->host), sectors, sectors >> 1,
  453. (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
  454. (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024,
  455. iops / 100, iops % 100);
  456. mmc_test_save_transfer_result(test, 1, sectors, ts, rate, iops);
  457. }
  458. /*
  459. * Print the average transfer rate.
  460. */
  461. static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
  462. unsigned int count, struct timespec *ts1,
  463. struct timespec *ts2)
  464. {
  465. unsigned int rate, iops, sectors = bytes >> 9;
  466. uint64_t tot = bytes * count;
  467. struct timespec ts;
  468. ts = timespec_sub(*ts2, *ts1);
  469. rate = mmc_test_rate(tot, &ts);
  470. iops = mmc_test_rate(count * 100, &ts); /* I/O ops per sec x 100 */
  471. printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
  472. "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
  473. "%u.%02u IOPS)\n",
  474. mmc_hostname(test->card->host), count, sectors, count,
  475. sectors >> 1, (sectors & 1 ? ".5" : ""),
  476. (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
  477. rate / 1000, rate / 1024, iops / 100, iops % 100);
  478. mmc_test_save_transfer_result(test, count, sectors, ts, rate, iops);
  479. }
  480. /*
  481. * Return the card size in sectors.
  482. */
  483. static unsigned int mmc_test_capacity(struct mmc_card *card)
  484. {
  485. if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
  486. return card->ext_csd.sectors;
  487. else
  488. return card->csd.capacity << (card->csd.read_blkbits - 9);
  489. }
  490. /*******************************************************************/
  491. /* Test preparation and cleanup */
  492. /*******************************************************************/
  493. /*
  494. * Fill the first couple of sectors of the card with known data
  495. * so that bad reads/writes can be detected
  496. */
  497. static int __mmc_test_prepare(struct mmc_test_card *test, int write)
  498. {
  499. int ret, i;
  500. ret = mmc_test_set_blksize(test, 512);
  501. if (ret)
  502. return ret;
  503. if (write)
  504. memset(test->buffer, 0xDF, 512);
  505. else {
  506. for (i = 0;i < 512;i++)
  507. test->buffer[i] = i;
  508. }
  509. for (i = 0;i < BUFFER_SIZE / 512;i++) {
  510. ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
  511. if (ret)
  512. return ret;
  513. }
  514. return 0;
  515. }
  516. static int mmc_test_prepare_write(struct mmc_test_card *test)
  517. {
  518. return __mmc_test_prepare(test, 1);
  519. }
  520. static int mmc_test_prepare_read(struct mmc_test_card *test)
  521. {
  522. return __mmc_test_prepare(test, 0);
  523. }
  524. static int mmc_test_cleanup(struct mmc_test_card *test)
  525. {
  526. int ret, i;
  527. ret = mmc_test_set_blksize(test, 512);
  528. if (ret)
  529. return ret;
  530. memset(test->buffer, 0, 512);
  531. for (i = 0;i < BUFFER_SIZE / 512;i++) {
  532. ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
  533. if (ret)
  534. return ret;
  535. }
  536. return 0;
  537. }
  538. /*******************************************************************/
  539. /* Test execution helpers */
  540. /*******************************************************************/
  541. /*
  542. * Modifies the mmc_request to perform the "short transfer" tests
  543. */
  544. static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
  545. struct mmc_request *mrq, int write)
  546. {
  547. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  548. if (mrq->data->blocks > 1) {
  549. mrq->cmd->opcode = write ?
  550. MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
  551. mrq->stop = NULL;
  552. } else {
  553. mrq->cmd->opcode = MMC_SEND_STATUS;
  554. mrq->cmd->arg = test->card->rca << 16;
  555. }
  556. }
  557. /*
  558. * Checks that a normal transfer didn't have any errors
  559. */
  560. static int mmc_test_check_result(struct mmc_test_card *test,
  561. struct mmc_request *mrq)
  562. {
  563. int ret;
  564. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  565. ret = 0;
  566. if (!ret && mrq->cmd->error)
  567. ret = mrq->cmd->error;
  568. if (!ret && mrq->data->error)
  569. ret = mrq->data->error;
  570. if (!ret && mrq->stop && mrq->stop->error)
  571. ret = mrq->stop->error;
  572. if (!ret && mrq->data->bytes_xfered !=
  573. mrq->data->blocks * mrq->data->blksz)
  574. ret = RESULT_FAIL;
  575. if (ret == -EINVAL)
  576. ret = RESULT_UNSUP_HOST;
  577. return ret;
  578. }
  579. /*
  580. * Checks that a "short transfer" behaved as expected
  581. */
  582. static int mmc_test_check_broken_result(struct mmc_test_card *test,
  583. struct mmc_request *mrq)
  584. {
  585. int ret;
  586. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  587. ret = 0;
  588. if (!ret && mrq->cmd->error)
  589. ret = mrq->cmd->error;
  590. if (!ret && mrq->data->error == 0)
  591. ret = RESULT_FAIL;
  592. if (!ret && mrq->data->error != -ETIMEDOUT)
  593. ret = mrq->data->error;
  594. if (!ret && mrq->stop && mrq->stop->error)
  595. ret = mrq->stop->error;
  596. if (mrq->data->blocks > 1) {
  597. if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
  598. ret = RESULT_FAIL;
  599. } else {
  600. if (!ret && mrq->data->bytes_xfered > 0)
  601. ret = RESULT_FAIL;
  602. }
  603. if (ret == -EINVAL)
  604. ret = RESULT_UNSUP_HOST;
  605. return ret;
  606. }
  607. /*
  608. * Tests a basic transfer with certain parameters
  609. */
  610. static int mmc_test_simple_transfer(struct mmc_test_card *test,
  611. struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
  612. unsigned blocks, unsigned blksz, int write)
  613. {
  614. struct mmc_request mrq = {0};
  615. struct mmc_command cmd = {0};
  616. struct mmc_command stop = {0};
  617. struct mmc_data data = {0};
  618. mrq.cmd = &cmd;
  619. mrq.data = &data;
  620. mrq.stop = &stop;
  621. mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
  622. blocks, blksz, write);
  623. mmc_wait_for_req(test->card->host, &mrq);
  624. mmc_test_wait_busy(test);
  625. return mmc_test_check_result(test, &mrq);
  626. }
  627. /*
  628. * Tests a transfer where the card will fail completely or partly
  629. */
  630. static int mmc_test_broken_transfer(struct mmc_test_card *test,
  631. unsigned blocks, unsigned blksz, int write)
  632. {
  633. struct mmc_request mrq = {0};
  634. struct mmc_command cmd = {0};
  635. struct mmc_command stop = {0};
  636. struct mmc_data data = {0};
  637. struct scatterlist sg;
  638. mrq.cmd = &cmd;
  639. mrq.data = &data;
  640. mrq.stop = &stop;
  641. sg_init_one(&sg, test->buffer, blocks * blksz);
  642. mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
  643. mmc_test_prepare_broken_mrq(test, &mrq, write);
  644. mmc_wait_for_req(test->card->host, &mrq);
  645. mmc_test_wait_busy(test);
  646. return mmc_test_check_broken_result(test, &mrq);
  647. }
  648. /*
  649. * Does a complete transfer test where data is also validated
  650. *
  651. * Note: mmc_test_prepare() must have been done before this call
  652. */
  653. static int mmc_test_transfer(struct mmc_test_card *test,
  654. struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
  655. unsigned blocks, unsigned blksz, int write)
  656. {
  657. int ret, i;
  658. unsigned long flags;
  659. if (write) {
  660. for (i = 0;i < blocks * blksz;i++)
  661. test->scratch[i] = i;
  662. } else {
  663. memset(test->scratch, 0, BUFFER_SIZE);
  664. }
  665. local_irq_save(flags);
  666. sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
  667. local_irq_restore(flags);
  668. ret = mmc_test_set_blksize(test, blksz);
  669. if (ret)
  670. return ret;
  671. ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
  672. blocks, blksz, write);
  673. if (ret)
  674. return ret;
  675. if (write) {
  676. int sectors;
  677. ret = mmc_test_set_blksize(test, 512);
  678. if (ret)
  679. return ret;
  680. sectors = (blocks * blksz + 511) / 512;
  681. if ((sectors * 512) == (blocks * blksz))
  682. sectors++;
  683. if ((sectors * 512) > BUFFER_SIZE)
  684. return -EINVAL;
  685. memset(test->buffer, 0, sectors * 512);
  686. for (i = 0;i < sectors;i++) {
  687. ret = mmc_test_buffer_transfer(test,
  688. test->buffer + i * 512,
  689. dev_addr + i, 512, 0);
  690. if (ret)
  691. return ret;
  692. }
  693. for (i = 0;i < blocks * blksz;i++) {
  694. if (test->buffer[i] != (u8)i)
  695. return RESULT_FAIL;
  696. }
  697. for (;i < sectors * 512;i++) {
  698. if (test->buffer[i] != 0xDF)
  699. return RESULT_FAIL;
  700. }
  701. } else {
  702. local_irq_save(flags);
  703. sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
  704. local_irq_restore(flags);
  705. for (i = 0;i < blocks * blksz;i++) {
  706. if (test->scratch[i] != (u8)i)
  707. return RESULT_FAIL;
  708. }
  709. }
  710. return 0;
  711. }
  712. /*******************************************************************/
  713. /* Tests */
  714. /*******************************************************************/
  715. struct mmc_test_case {
  716. const char *name;
  717. int (*prepare)(struct mmc_test_card *);
  718. int (*run)(struct mmc_test_card *);
  719. int (*cleanup)(struct mmc_test_card *);
  720. };
  721. static int mmc_test_basic_write(struct mmc_test_card *test)
  722. {
  723. int ret;
  724. struct scatterlist sg;
  725. ret = mmc_test_set_blksize(test, 512);
  726. if (ret)
  727. return ret;
  728. sg_init_one(&sg, test->buffer, 512);
  729. ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
  730. if (ret)
  731. return ret;
  732. return 0;
  733. }
  734. static int mmc_test_basic_read(struct mmc_test_card *test)
  735. {
  736. int ret;
  737. struct scatterlist sg;
  738. ret = mmc_test_set_blksize(test, 512);
  739. if (ret)
  740. return ret;
  741. sg_init_one(&sg, test->buffer, 512);
  742. ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
  743. if (ret)
  744. return ret;
  745. return 0;
  746. }
  747. static int mmc_test_verify_write(struct mmc_test_card *test)
  748. {
  749. int ret;
  750. struct scatterlist sg;
  751. sg_init_one(&sg, test->buffer, 512);
  752. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
  753. if (ret)
  754. return ret;
  755. return 0;
  756. }
  757. static int mmc_test_verify_read(struct mmc_test_card *test)
  758. {
  759. int ret;
  760. struct scatterlist sg;
  761. sg_init_one(&sg, test->buffer, 512);
  762. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
  763. if (ret)
  764. return ret;
  765. return 0;
  766. }
  767. static int mmc_test_multi_write(struct mmc_test_card *test)
  768. {
  769. int ret;
  770. unsigned int size;
  771. struct scatterlist sg;
  772. if (test->card->host->max_blk_count == 1)
  773. return RESULT_UNSUP_HOST;
  774. size = PAGE_SIZE * 2;
  775. size = min(size, test->card->host->max_req_size);
  776. size = min(size, test->card->host->max_seg_size);
  777. size = min(size, test->card->host->max_blk_count * 512);
  778. if (size < 1024)
  779. return RESULT_UNSUP_HOST;
  780. sg_init_one(&sg, test->buffer, size);
  781. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
  782. if (ret)
  783. return ret;
  784. return 0;
  785. }
  786. static int mmc_test_multi_read(struct mmc_test_card *test)
  787. {
  788. int ret;
  789. unsigned int size;
  790. struct scatterlist sg;
  791. if (test->card->host->max_blk_count == 1)
  792. return RESULT_UNSUP_HOST;
  793. size = PAGE_SIZE * 2;
  794. size = min(size, test->card->host->max_req_size);
  795. size = min(size, test->card->host->max_seg_size);
  796. size = min(size, test->card->host->max_blk_count * 512);
  797. if (size < 1024)
  798. return RESULT_UNSUP_HOST;
  799. sg_init_one(&sg, test->buffer, size);
  800. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
  801. if (ret)
  802. return ret;
  803. return 0;
  804. }
  805. static int mmc_test_pow2_write(struct mmc_test_card *test)
  806. {
  807. int ret, i;
  808. struct scatterlist sg;
  809. if (!test->card->csd.write_partial)
  810. return RESULT_UNSUP_CARD;
  811. for (i = 1; i < 512;i <<= 1) {
  812. sg_init_one(&sg, test->buffer, i);
  813. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
  814. if (ret)
  815. return ret;
  816. }
  817. return 0;
  818. }
  819. static int mmc_test_pow2_read(struct mmc_test_card *test)
  820. {
  821. int ret, i;
  822. struct scatterlist sg;
  823. if (!test->card->csd.read_partial)
  824. return RESULT_UNSUP_CARD;
  825. for (i = 1; i < 512;i <<= 1) {
  826. sg_init_one(&sg, test->buffer, i);
  827. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
  828. if (ret)
  829. return ret;
  830. }
  831. return 0;
  832. }
  833. static int mmc_test_weird_write(struct mmc_test_card *test)
  834. {
  835. int ret, i;
  836. struct scatterlist sg;
  837. if (!test->card->csd.write_partial)
  838. return RESULT_UNSUP_CARD;
  839. for (i = 3; i < 512;i += 7) {
  840. sg_init_one(&sg, test->buffer, i);
  841. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
  842. if (ret)
  843. return ret;
  844. }
  845. return 0;
  846. }
  847. static int mmc_test_weird_read(struct mmc_test_card *test)
  848. {
  849. int ret, i;
  850. struct scatterlist sg;
  851. if (!test->card->csd.read_partial)
  852. return RESULT_UNSUP_CARD;
  853. for (i = 3; i < 512;i += 7) {
  854. sg_init_one(&sg, test->buffer, i);
  855. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
  856. if (ret)
  857. return ret;
  858. }
  859. return 0;
  860. }
  861. static int mmc_test_align_write(struct mmc_test_card *test)
  862. {
  863. int ret, i;
  864. struct scatterlist sg;
  865. for (i = 1;i < 4;i++) {
  866. sg_init_one(&sg, test->buffer + i, 512);
  867. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
  868. if (ret)
  869. return ret;
  870. }
  871. return 0;
  872. }
  873. static int mmc_test_align_read(struct mmc_test_card *test)
  874. {
  875. int ret, i;
  876. struct scatterlist sg;
  877. for (i = 1;i < 4;i++) {
  878. sg_init_one(&sg, test->buffer + i, 512);
  879. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
  880. if (ret)
  881. return ret;
  882. }
  883. return 0;
  884. }
  885. static int mmc_test_align_multi_write(struct mmc_test_card *test)
  886. {
  887. int ret, i;
  888. unsigned int size;
  889. struct scatterlist sg;
  890. if (test->card->host->max_blk_count == 1)
  891. return RESULT_UNSUP_HOST;
  892. size = PAGE_SIZE * 2;
  893. size = min(size, test->card->host->max_req_size);
  894. size = min(size, test->card->host->max_seg_size);
  895. size = min(size, test->card->host->max_blk_count * 512);
  896. if (size < 1024)
  897. return RESULT_UNSUP_HOST;
  898. for (i = 1;i < 4;i++) {
  899. sg_init_one(&sg, test->buffer + i, size);
  900. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
  901. if (ret)
  902. return ret;
  903. }
  904. return 0;
  905. }
  906. static int mmc_test_align_multi_read(struct mmc_test_card *test)
  907. {
  908. int ret, i;
  909. unsigned int size;
  910. struct scatterlist sg;
  911. if (test->card->host->max_blk_count == 1)
  912. return RESULT_UNSUP_HOST;
  913. size = PAGE_SIZE * 2;
  914. size = min(size, test->card->host->max_req_size);
  915. size = min(size, test->card->host->max_seg_size);
  916. size = min(size, test->card->host->max_blk_count * 512);
  917. if (size < 1024)
  918. return RESULT_UNSUP_HOST;
  919. for (i = 1;i < 4;i++) {
  920. sg_init_one(&sg, test->buffer + i, size);
  921. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
  922. if (ret)
  923. return ret;
  924. }
  925. return 0;
  926. }
  927. static int mmc_test_xfersize_write(struct mmc_test_card *test)
  928. {
  929. int ret;
  930. ret = mmc_test_set_blksize(test, 512);
  931. if (ret)
  932. return ret;
  933. ret = mmc_test_broken_transfer(test, 1, 512, 1);
  934. if (ret)
  935. return ret;
  936. return 0;
  937. }
  938. static int mmc_test_xfersize_read(struct mmc_test_card *test)
  939. {
  940. int ret;
  941. ret = mmc_test_set_blksize(test, 512);
  942. if (ret)
  943. return ret;
  944. ret = mmc_test_broken_transfer(test, 1, 512, 0);
  945. if (ret)
  946. return ret;
  947. return 0;
  948. }
  949. static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
  950. {
  951. int ret;
  952. if (test->card->host->max_blk_count == 1)
  953. return RESULT_UNSUP_HOST;
  954. ret = mmc_test_set_blksize(test, 512);
  955. if (ret)
  956. return ret;
  957. ret = mmc_test_broken_transfer(test, 2, 512, 1);
  958. if (ret)
  959. return ret;
  960. return 0;
  961. }
  962. static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
  963. {
  964. int ret;
  965. if (test->card->host->max_blk_count == 1)
  966. return RESULT_UNSUP_HOST;
  967. ret = mmc_test_set_blksize(test, 512);
  968. if (ret)
  969. return ret;
  970. ret = mmc_test_broken_transfer(test, 2, 512, 0);
  971. if (ret)
  972. return ret;
  973. return 0;
  974. }
  975. #ifdef CONFIG_HIGHMEM
  976. static int mmc_test_write_high(struct mmc_test_card *test)
  977. {
  978. int ret;
  979. struct scatterlist sg;
  980. sg_init_table(&sg, 1);
  981. sg_set_page(&sg, test->highmem, 512, 0);
  982. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
  983. if (ret)
  984. return ret;
  985. return 0;
  986. }
  987. static int mmc_test_read_high(struct mmc_test_card *test)
  988. {
  989. int ret;
  990. struct scatterlist sg;
  991. sg_init_table(&sg, 1);
  992. sg_set_page(&sg, test->highmem, 512, 0);
  993. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
  994. if (ret)
  995. return ret;
  996. return 0;
  997. }
  998. static int mmc_test_multi_write_high(struct mmc_test_card *test)
  999. {
  1000. int ret;
  1001. unsigned int size;
  1002. struct scatterlist sg;
  1003. if (test->card->host->max_blk_count == 1)
  1004. return RESULT_UNSUP_HOST;
  1005. size = PAGE_SIZE * 2;
  1006. size = min(size, test->card->host->max_req_size);
  1007. size = min(size, test->card->host->max_seg_size);
  1008. size = min(size, test->card->host->max_blk_count * 512);
  1009. if (size < 1024)
  1010. return RESULT_UNSUP_HOST;
  1011. sg_init_table(&sg, 1);
  1012. sg_set_page(&sg, test->highmem, size, 0);
  1013. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
  1014. if (ret)
  1015. return ret;
  1016. return 0;
  1017. }
  1018. static int mmc_test_multi_read_high(struct mmc_test_card *test)
  1019. {
  1020. int ret;
  1021. unsigned int size;
  1022. struct scatterlist sg;
  1023. if (test->card->host->max_blk_count == 1)
  1024. return RESULT_UNSUP_HOST;
  1025. size = PAGE_SIZE * 2;
  1026. size = min(size, test->card->host->max_req_size);
  1027. size = min(size, test->card->host->max_seg_size);
  1028. size = min(size, test->card->host->max_blk_count * 512);
  1029. if (size < 1024)
  1030. return RESULT_UNSUP_HOST;
  1031. sg_init_table(&sg, 1);
  1032. sg_set_page(&sg, test->highmem, size, 0);
  1033. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
  1034. if (ret)
  1035. return ret;
  1036. return 0;
  1037. }
  1038. #else
  1039. static int mmc_test_no_highmem(struct mmc_test_card *test)
  1040. {
  1041. printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
  1042. mmc_hostname(test->card->host));
  1043. return 0;
  1044. }
  1045. #endif /* CONFIG_HIGHMEM */
  1046. /*
  1047. * Map sz bytes so that it can be transferred.
  1048. */
  1049. static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
  1050. int max_scatter)
  1051. {
  1052. struct mmc_test_area *t = &test->area;
  1053. int err;
  1054. t->blocks = sz >> 9;
  1055. if (max_scatter) {
  1056. err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
  1057. t->max_segs, t->max_seg_sz,
  1058. &t->sg_len);
  1059. } else {
  1060. err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
  1061. t->max_seg_sz, &t->sg_len);
  1062. }
  1063. if (err)
  1064. printk(KERN_INFO "%s: Failed to map sg list\n",
  1065. mmc_hostname(test->card->host));
  1066. return err;
  1067. }
  1068. /*
  1069. * Transfer bytes mapped by mmc_test_area_map().
  1070. */
  1071. static int mmc_test_area_transfer(struct mmc_test_card *test,
  1072. unsigned int dev_addr, int write)
  1073. {
  1074. struct mmc_test_area *t = &test->area;
  1075. return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
  1076. t->blocks, 512, write);
  1077. }
  1078. /*
  1079. * Map and transfer bytes.
  1080. */
  1081. static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
  1082. unsigned int dev_addr, int write, int max_scatter,
  1083. int timed)
  1084. {
  1085. struct timespec ts1, ts2;
  1086. int ret;
  1087. /*
  1088. * In the case of a maximally scattered transfer, the maximum transfer
  1089. * size is further limited by using PAGE_SIZE segments.
  1090. */
  1091. if (max_scatter) {
  1092. struct mmc_test_area *t = &test->area;
  1093. unsigned long max_tfr;
  1094. if (t->max_seg_sz >= PAGE_SIZE)
  1095. max_tfr = t->max_segs * PAGE_SIZE;
  1096. else
  1097. max_tfr = t->max_segs * t->max_seg_sz;
  1098. if (sz > max_tfr)
  1099. sz = max_tfr;
  1100. }
  1101. ret = mmc_test_area_map(test, sz, max_scatter);
  1102. if (ret)
  1103. return ret;
  1104. if (timed)
  1105. getnstimeofday(&ts1);
  1106. ret = mmc_test_area_transfer(test, dev_addr, write);
  1107. if (ret)
  1108. return ret;
  1109. if (timed)
  1110. getnstimeofday(&ts2);
  1111. if (timed)
  1112. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1113. return 0;
  1114. }
  1115. /*
  1116. * Write the test area entirely.
  1117. */
  1118. static int mmc_test_area_fill(struct mmc_test_card *test)
  1119. {
  1120. struct mmc_test_area *t = &test->area;
  1121. return mmc_test_area_io(test, t->max_tfr, t->dev_addr, 1, 0, 0);
  1122. }
  1123. /*
  1124. * Erase the test area entirely.
  1125. */
  1126. static int mmc_test_area_erase(struct mmc_test_card *test)
  1127. {
  1128. struct mmc_test_area *t = &test->area;
  1129. if (!mmc_can_erase(test->card))
  1130. return 0;
  1131. return mmc_erase(test->card, t->dev_addr, t->max_sz >> 9,
  1132. MMC_ERASE_ARG);
  1133. }
  1134. /*
  1135. * Cleanup struct mmc_test_area.
  1136. */
  1137. static int mmc_test_area_cleanup(struct mmc_test_card *test)
  1138. {
  1139. struct mmc_test_area *t = &test->area;
  1140. kfree(t->sg);
  1141. mmc_test_free_mem(t->mem);
  1142. return 0;
  1143. }
  1144. /*
  1145. * Initialize an area for testing large transfers. The test area is set to the
  1146. * middle of the card because cards may have different charateristics at the
  1147. * front (for FAT file system optimization). Optionally, the area is erased
  1148. * (if the card supports it) which may improve write performance. Optionally,
  1149. * the area is filled with data for subsequent read tests.
  1150. */
  1151. static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
  1152. {
  1153. struct mmc_test_area *t = &test->area;
  1154. unsigned long min_sz = 64 * 1024, sz;
  1155. int ret;
  1156. ret = mmc_test_set_blksize(test, 512);
  1157. if (ret)
  1158. return ret;
  1159. /* Make the test area size about 4MiB */
  1160. sz = (unsigned long)test->card->pref_erase << 9;
  1161. t->max_sz = sz;
  1162. while (t->max_sz < 4 * 1024 * 1024)
  1163. t->max_sz += sz;
  1164. while (t->max_sz > TEST_AREA_MAX_SIZE && t->max_sz > sz)
  1165. t->max_sz -= sz;
  1166. t->max_segs = test->card->host->max_segs;
  1167. t->max_seg_sz = test->card->host->max_seg_size;
  1168. t->max_tfr = t->max_sz;
  1169. if (t->max_tfr >> 9 > test->card->host->max_blk_count)
  1170. t->max_tfr = test->card->host->max_blk_count << 9;
  1171. if (t->max_tfr > test->card->host->max_req_size)
  1172. t->max_tfr = test->card->host->max_req_size;
  1173. if (t->max_tfr / t->max_seg_sz > t->max_segs)
  1174. t->max_tfr = t->max_segs * t->max_seg_sz;
  1175. /*
  1176. * Try to allocate enough memory for a max. sized transfer. Less is OK
  1177. * because the same memory can be mapped into the scatterlist more than
  1178. * once. Also, take into account the limits imposed on scatterlist
  1179. * segments by the host driver.
  1180. */
  1181. t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
  1182. t->max_seg_sz);
  1183. if (!t->mem)
  1184. return -ENOMEM;
  1185. t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
  1186. if (!t->sg) {
  1187. ret = -ENOMEM;
  1188. goto out_free;
  1189. }
  1190. t->dev_addr = mmc_test_capacity(test->card) / 2;
  1191. t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
  1192. if (erase) {
  1193. ret = mmc_test_area_erase(test);
  1194. if (ret)
  1195. goto out_free;
  1196. }
  1197. if (fill) {
  1198. ret = mmc_test_area_fill(test);
  1199. if (ret)
  1200. goto out_free;
  1201. }
  1202. return 0;
  1203. out_free:
  1204. mmc_test_area_cleanup(test);
  1205. return ret;
  1206. }
  1207. /*
  1208. * Prepare for large transfers. Do not erase the test area.
  1209. */
  1210. static int mmc_test_area_prepare(struct mmc_test_card *test)
  1211. {
  1212. return mmc_test_area_init(test, 0, 0);
  1213. }
  1214. /*
  1215. * Prepare for large transfers. Do erase the test area.
  1216. */
  1217. static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
  1218. {
  1219. return mmc_test_area_init(test, 1, 0);
  1220. }
  1221. /*
  1222. * Prepare for large transfers. Erase and fill the test area.
  1223. */
  1224. static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
  1225. {
  1226. return mmc_test_area_init(test, 1, 1);
  1227. }
  1228. /*
  1229. * Test best-case performance. Best-case performance is expected from
  1230. * a single large transfer.
  1231. *
  1232. * An additional option (max_scatter) allows the measurement of the same
  1233. * transfer but with no contiguous pages in the scatter list. This tests
  1234. * the efficiency of DMA to handle scattered pages.
  1235. */
  1236. static int mmc_test_best_performance(struct mmc_test_card *test, int write,
  1237. int max_scatter)
  1238. {
  1239. struct mmc_test_area *t = &test->area;
  1240. return mmc_test_area_io(test, t->max_tfr, t->dev_addr, write,
  1241. max_scatter, 1);
  1242. }
  1243. /*
  1244. * Best-case read performance.
  1245. */
  1246. static int mmc_test_best_read_performance(struct mmc_test_card *test)
  1247. {
  1248. return mmc_test_best_performance(test, 0, 0);
  1249. }
  1250. /*
  1251. * Best-case write performance.
  1252. */
  1253. static int mmc_test_best_write_performance(struct mmc_test_card *test)
  1254. {
  1255. return mmc_test_best_performance(test, 1, 0);
  1256. }
  1257. /*
  1258. * Best-case read performance into scattered pages.
  1259. */
  1260. static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
  1261. {
  1262. return mmc_test_best_performance(test, 0, 1);
  1263. }
  1264. /*
  1265. * Best-case write performance from scattered pages.
  1266. */
  1267. static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
  1268. {
  1269. return mmc_test_best_performance(test, 1, 1);
  1270. }
  1271. /*
  1272. * Single read performance by transfer size.
  1273. */
  1274. static int mmc_test_profile_read_perf(struct mmc_test_card *test)
  1275. {
  1276. struct mmc_test_area *t = &test->area;
  1277. unsigned long sz;
  1278. unsigned int dev_addr;
  1279. int ret;
  1280. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1281. dev_addr = t->dev_addr + (sz >> 9);
  1282. ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
  1283. if (ret)
  1284. return ret;
  1285. }
  1286. sz = t->max_tfr;
  1287. dev_addr = t->dev_addr;
  1288. return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
  1289. }
  1290. /*
  1291. * Single write performance by transfer size.
  1292. */
  1293. static int mmc_test_profile_write_perf(struct mmc_test_card *test)
  1294. {
  1295. struct mmc_test_area *t = &test->area;
  1296. unsigned long sz;
  1297. unsigned int dev_addr;
  1298. int ret;
  1299. ret = mmc_test_area_erase(test);
  1300. if (ret)
  1301. return ret;
  1302. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1303. dev_addr = t->dev_addr + (sz >> 9);
  1304. ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
  1305. if (ret)
  1306. return ret;
  1307. }
  1308. ret = mmc_test_area_erase(test);
  1309. if (ret)
  1310. return ret;
  1311. sz = t->max_tfr;
  1312. dev_addr = t->dev_addr;
  1313. return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
  1314. }
  1315. /*
  1316. * Single trim performance by transfer size.
  1317. */
  1318. static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
  1319. {
  1320. struct mmc_test_area *t = &test->area;
  1321. unsigned long sz;
  1322. unsigned int dev_addr;
  1323. struct timespec ts1, ts2;
  1324. int ret;
  1325. if (!mmc_can_trim(test->card))
  1326. return RESULT_UNSUP_CARD;
  1327. if (!mmc_can_erase(test->card))
  1328. return RESULT_UNSUP_HOST;
  1329. for (sz = 512; sz < t->max_sz; sz <<= 1) {
  1330. dev_addr = t->dev_addr + (sz >> 9);
  1331. getnstimeofday(&ts1);
  1332. ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
  1333. if (ret)
  1334. return ret;
  1335. getnstimeofday(&ts2);
  1336. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1337. }
  1338. dev_addr = t->dev_addr;
  1339. getnstimeofday(&ts1);
  1340. ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
  1341. if (ret)
  1342. return ret;
  1343. getnstimeofday(&ts2);
  1344. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1345. return 0;
  1346. }
  1347. static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
  1348. {
  1349. struct mmc_test_area *t = &test->area;
  1350. unsigned int dev_addr, i, cnt;
  1351. struct timespec ts1, ts2;
  1352. int ret;
  1353. cnt = t->max_sz / sz;
  1354. dev_addr = t->dev_addr;
  1355. getnstimeofday(&ts1);
  1356. for (i = 0; i < cnt; i++) {
  1357. ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
  1358. if (ret)
  1359. return ret;
  1360. dev_addr += (sz >> 9);
  1361. }
  1362. getnstimeofday(&ts2);
  1363. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1364. return 0;
  1365. }
  1366. /*
  1367. * Consecutive read performance by transfer size.
  1368. */
  1369. static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
  1370. {
  1371. struct mmc_test_area *t = &test->area;
  1372. unsigned long sz;
  1373. int ret;
  1374. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1375. ret = mmc_test_seq_read_perf(test, sz);
  1376. if (ret)
  1377. return ret;
  1378. }
  1379. sz = t->max_tfr;
  1380. return mmc_test_seq_read_perf(test, sz);
  1381. }
  1382. static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
  1383. {
  1384. struct mmc_test_area *t = &test->area;
  1385. unsigned int dev_addr, i, cnt;
  1386. struct timespec ts1, ts2;
  1387. int ret;
  1388. ret = mmc_test_area_erase(test);
  1389. if (ret)
  1390. return ret;
  1391. cnt = t->max_sz / sz;
  1392. dev_addr = t->dev_addr;
  1393. getnstimeofday(&ts1);
  1394. for (i = 0; i < cnt; i++) {
  1395. ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
  1396. if (ret)
  1397. return ret;
  1398. dev_addr += (sz >> 9);
  1399. }
  1400. getnstimeofday(&ts2);
  1401. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1402. return 0;
  1403. }
  1404. /*
  1405. * Consecutive write performance by transfer size.
  1406. */
  1407. static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
  1408. {
  1409. struct mmc_test_area *t = &test->area;
  1410. unsigned long sz;
  1411. int ret;
  1412. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1413. ret = mmc_test_seq_write_perf(test, sz);
  1414. if (ret)
  1415. return ret;
  1416. }
  1417. sz = t->max_tfr;
  1418. return mmc_test_seq_write_perf(test, sz);
  1419. }
  1420. /*
  1421. * Consecutive trim performance by transfer size.
  1422. */
  1423. static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
  1424. {
  1425. struct mmc_test_area *t = &test->area;
  1426. unsigned long sz;
  1427. unsigned int dev_addr, i, cnt;
  1428. struct timespec ts1, ts2;
  1429. int ret;
  1430. if (!mmc_can_trim(test->card))
  1431. return RESULT_UNSUP_CARD;
  1432. if (!mmc_can_erase(test->card))
  1433. return RESULT_UNSUP_HOST;
  1434. for (sz = 512; sz <= t->max_sz; sz <<= 1) {
  1435. ret = mmc_test_area_erase(test);
  1436. if (ret)
  1437. return ret;
  1438. ret = mmc_test_area_fill(test);
  1439. if (ret)
  1440. return ret;
  1441. cnt = t->max_sz / sz;
  1442. dev_addr = t->dev_addr;
  1443. getnstimeofday(&ts1);
  1444. for (i = 0; i < cnt; i++) {
  1445. ret = mmc_erase(test->card, dev_addr, sz >> 9,
  1446. MMC_TRIM_ARG);
  1447. if (ret)
  1448. return ret;
  1449. dev_addr += (sz >> 9);
  1450. }
  1451. getnstimeofday(&ts2);
  1452. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1453. }
  1454. return 0;
  1455. }
  1456. static unsigned int rnd_next = 1;
  1457. static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt)
  1458. {
  1459. uint64_t r;
  1460. rnd_next = rnd_next * 1103515245 + 12345;
  1461. r = (rnd_next >> 16) & 0x7fff;
  1462. return (r * rnd_cnt) >> 15;
  1463. }
  1464. static int mmc_test_rnd_perf(struct mmc_test_card *test, int write, int print,
  1465. unsigned long sz)
  1466. {
  1467. unsigned int dev_addr, cnt, rnd_addr, range1, range2, last_ea = 0, ea;
  1468. unsigned int ssz;
  1469. struct timespec ts1, ts2, ts;
  1470. int ret;
  1471. ssz = sz >> 9;
  1472. rnd_addr = mmc_test_capacity(test->card) / 4;
  1473. range1 = rnd_addr / test->card->pref_erase;
  1474. range2 = range1 / ssz;
  1475. getnstimeofday(&ts1);
  1476. for (cnt = 0; cnt < UINT_MAX; cnt++) {
  1477. getnstimeofday(&ts2);
  1478. ts = timespec_sub(ts2, ts1);
  1479. if (ts.tv_sec >= 10)
  1480. break;
  1481. ea = mmc_test_rnd_num(range1);
  1482. if (ea == last_ea)
  1483. ea -= 1;
  1484. last_ea = ea;
  1485. dev_addr = rnd_addr + test->card->pref_erase * ea +
  1486. ssz * mmc_test_rnd_num(range2);
  1487. ret = mmc_test_area_io(test, sz, dev_addr, write, 0, 0);
  1488. if (ret)
  1489. return ret;
  1490. }
  1491. if (print)
  1492. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1493. return 0;
  1494. }
  1495. static int mmc_test_random_perf(struct mmc_test_card *test, int write)
  1496. {
  1497. struct mmc_test_area *t = &test->area;
  1498. unsigned int next;
  1499. unsigned long sz;
  1500. int ret;
  1501. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1502. /*
  1503. * When writing, try to get more consistent results by running
  1504. * the test twice with exactly the same I/O but outputting the
  1505. * results only for the 2nd run.
  1506. */
  1507. if (write) {
  1508. next = rnd_next;
  1509. ret = mmc_test_rnd_perf(test, write, 0, sz);
  1510. if (ret)
  1511. return ret;
  1512. rnd_next = next;
  1513. }
  1514. ret = mmc_test_rnd_perf(test, write, 1, sz);
  1515. if (ret)
  1516. return ret;
  1517. }
  1518. sz = t->max_tfr;
  1519. if (write) {
  1520. next = rnd_next;
  1521. ret = mmc_test_rnd_perf(test, write, 0, sz);
  1522. if (ret)
  1523. return ret;
  1524. rnd_next = next;
  1525. }
  1526. return mmc_test_rnd_perf(test, write, 1, sz);
  1527. }
  1528. /*
  1529. * Random read performance by transfer size.
  1530. */
  1531. static int mmc_test_random_read_perf(struct mmc_test_card *test)
  1532. {
  1533. return mmc_test_random_perf(test, 0);
  1534. }
  1535. /*
  1536. * Random write performance by transfer size.
  1537. */
  1538. static int mmc_test_random_write_perf(struct mmc_test_card *test)
  1539. {
  1540. return mmc_test_random_perf(test, 1);
  1541. }
  1542. static int mmc_test_seq_perf(struct mmc_test_card *test, int write,
  1543. unsigned int tot_sz, int max_scatter)
  1544. {
  1545. struct mmc_test_area *t = &test->area;
  1546. unsigned int dev_addr, i, cnt, sz, ssz;
  1547. struct timespec ts1, ts2;
  1548. int ret;
  1549. sz = t->max_tfr;
  1550. /*
  1551. * In the case of a maximally scattered transfer, the maximum transfer
  1552. * size is further limited by using PAGE_SIZE segments.
  1553. */
  1554. if (max_scatter) {
  1555. unsigned long max_tfr;
  1556. if (t->max_seg_sz >= PAGE_SIZE)
  1557. max_tfr = t->max_segs * PAGE_SIZE;
  1558. else
  1559. max_tfr = t->max_segs * t->max_seg_sz;
  1560. if (sz > max_tfr)
  1561. sz = max_tfr;
  1562. }
  1563. ssz = sz >> 9;
  1564. dev_addr = mmc_test_capacity(test->card) / 4;
  1565. if (tot_sz > dev_addr << 9)
  1566. tot_sz = dev_addr << 9;
  1567. cnt = tot_sz / sz;
  1568. dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
  1569. getnstimeofday(&ts1);
  1570. for (i = 0; i < cnt; i++) {
  1571. ret = mmc_test_area_io(test, sz, dev_addr, write,
  1572. max_scatter, 0);
  1573. if (ret)
  1574. return ret;
  1575. dev_addr += ssz;
  1576. }
  1577. getnstimeofday(&ts2);
  1578. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1579. return 0;
  1580. }
  1581. static int mmc_test_large_seq_perf(struct mmc_test_card *test, int write)
  1582. {
  1583. int ret, i;
  1584. for (i = 0; i < 10; i++) {
  1585. ret = mmc_test_seq_perf(test, write, 10 * 1024 * 1024, 1);
  1586. if (ret)
  1587. return ret;
  1588. }
  1589. for (i = 0; i < 5; i++) {
  1590. ret = mmc_test_seq_perf(test, write, 100 * 1024 * 1024, 1);
  1591. if (ret)
  1592. return ret;
  1593. }
  1594. for (i = 0; i < 3; i++) {
  1595. ret = mmc_test_seq_perf(test, write, 1000 * 1024 * 1024, 1);
  1596. if (ret)
  1597. return ret;
  1598. }
  1599. return ret;
  1600. }
  1601. /*
  1602. * Large sequential read performance.
  1603. */
  1604. static int mmc_test_large_seq_read_perf(struct mmc_test_card *test)
  1605. {
  1606. return mmc_test_large_seq_perf(test, 0);
  1607. }
  1608. /*
  1609. * Large sequential write performance.
  1610. */
  1611. static int mmc_test_large_seq_write_perf(struct mmc_test_card *test)
  1612. {
  1613. return mmc_test_large_seq_perf(test, 1);
  1614. }
  1615. static const struct mmc_test_case mmc_test_cases[] = {
  1616. {
  1617. .name = "Basic write (no data verification)",
  1618. .run = mmc_test_basic_write,
  1619. },
  1620. {
  1621. .name = "Basic read (no data verification)",
  1622. .run = mmc_test_basic_read,
  1623. },
  1624. {
  1625. .name = "Basic write (with data verification)",
  1626. .prepare = mmc_test_prepare_write,
  1627. .run = mmc_test_verify_write,
  1628. .cleanup = mmc_test_cleanup,
  1629. },
  1630. {
  1631. .name = "Basic read (with data verification)",
  1632. .prepare = mmc_test_prepare_read,
  1633. .run = mmc_test_verify_read,
  1634. .cleanup = mmc_test_cleanup,
  1635. },
  1636. {
  1637. .name = "Multi-block write",
  1638. .prepare = mmc_test_prepare_write,
  1639. .run = mmc_test_multi_write,
  1640. .cleanup = mmc_test_cleanup,
  1641. },
  1642. {
  1643. .name = "Multi-block read",
  1644. .prepare = mmc_test_prepare_read,
  1645. .run = mmc_test_multi_read,
  1646. .cleanup = mmc_test_cleanup,
  1647. },
  1648. {
  1649. .name = "Power of two block writes",
  1650. .prepare = mmc_test_prepare_write,
  1651. .run = mmc_test_pow2_write,
  1652. .cleanup = mmc_test_cleanup,
  1653. },
  1654. {
  1655. .name = "Power of two block reads",
  1656. .prepare = mmc_test_prepare_read,
  1657. .run = mmc_test_pow2_read,
  1658. .cleanup = mmc_test_cleanup,
  1659. },
  1660. {
  1661. .name = "Weird sized block writes",
  1662. .prepare = mmc_test_prepare_write,
  1663. .run = mmc_test_weird_write,
  1664. .cleanup = mmc_test_cleanup,
  1665. },
  1666. {
  1667. .name = "Weird sized block reads",
  1668. .prepare = mmc_test_prepare_read,
  1669. .run = mmc_test_weird_read,
  1670. .cleanup = mmc_test_cleanup,
  1671. },
  1672. {
  1673. .name = "Badly aligned write",
  1674. .prepare = mmc_test_prepare_write,
  1675. .run = mmc_test_align_write,
  1676. .cleanup = mmc_test_cleanup,
  1677. },
  1678. {
  1679. .name = "Badly aligned read",
  1680. .prepare = mmc_test_prepare_read,
  1681. .run = mmc_test_align_read,
  1682. .cleanup = mmc_test_cleanup,
  1683. },
  1684. {
  1685. .name = "Badly aligned multi-block write",
  1686. .prepare = mmc_test_prepare_write,
  1687. .run = mmc_test_align_multi_write,
  1688. .cleanup = mmc_test_cleanup,
  1689. },
  1690. {
  1691. .name = "Badly aligned multi-block read",
  1692. .prepare = mmc_test_prepare_read,
  1693. .run = mmc_test_align_multi_read,
  1694. .cleanup = mmc_test_cleanup,
  1695. },
  1696. {
  1697. .name = "Correct xfer_size at write (start failure)",
  1698. .run = mmc_test_xfersize_write,
  1699. },
  1700. {
  1701. .name = "Correct xfer_size at read (start failure)",
  1702. .run = mmc_test_xfersize_read,
  1703. },
  1704. {
  1705. .name = "Correct xfer_size at write (midway failure)",
  1706. .run = mmc_test_multi_xfersize_write,
  1707. },
  1708. {
  1709. .name = "Correct xfer_size at read (midway failure)",
  1710. .run = mmc_test_multi_xfersize_read,
  1711. },
  1712. #ifdef CONFIG_HIGHMEM
  1713. {
  1714. .name = "Highmem write",
  1715. .prepare = mmc_test_prepare_write,
  1716. .run = mmc_test_write_high,
  1717. .cleanup = mmc_test_cleanup,
  1718. },
  1719. {
  1720. .name = "Highmem read",
  1721. .prepare = mmc_test_prepare_read,
  1722. .run = mmc_test_read_high,
  1723. .cleanup = mmc_test_cleanup,
  1724. },
  1725. {
  1726. .name = "Multi-block highmem write",
  1727. .prepare = mmc_test_prepare_write,
  1728. .run = mmc_test_multi_write_high,
  1729. .cleanup = mmc_test_cleanup,
  1730. },
  1731. {
  1732. .name = "Multi-block highmem read",
  1733. .prepare = mmc_test_prepare_read,
  1734. .run = mmc_test_multi_read_high,
  1735. .cleanup = mmc_test_cleanup,
  1736. },
  1737. #else
  1738. {
  1739. .name = "Highmem write",
  1740. .run = mmc_test_no_highmem,
  1741. },
  1742. {
  1743. .name = "Highmem read",
  1744. .run = mmc_test_no_highmem,
  1745. },
  1746. {
  1747. .name = "Multi-block highmem write",
  1748. .run = mmc_test_no_highmem,
  1749. },
  1750. {
  1751. .name = "Multi-block highmem read",
  1752. .run = mmc_test_no_highmem,
  1753. },
  1754. #endif /* CONFIG_HIGHMEM */
  1755. {
  1756. .name = "Best-case read performance",
  1757. .prepare = mmc_test_area_prepare_fill,
  1758. .run = mmc_test_best_read_performance,
  1759. .cleanup = mmc_test_area_cleanup,
  1760. },
  1761. {
  1762. .name = "Best-case write performance",
  1763. .prepare = mmc_test_area_prepare_erase,
  1764. .run = mmc_test_best_write_performance,
  1765. .cleanup = mmc_test_area_cleanup,
  1766. },
  1767. {
  1768. .name = "Best-case read performance into scattered pages",
  1769. .prepare = mmc_test_area_prepare_fill,
  1770. .run = mmc_test_best_read_perf_max_scatter,
  1771. .cleanup = mmc_test_area_cleanup,
  1772. },
  1773. {
  1774. .name = "Best-case write performance from scattered pages",
  1775. .prepare = mmc_test_area_prepare_erase,
  1776. .run = mmc_test_best_write_perf_max_scatter,
  1777. .cleanup = mmc_test_area_cleanup,
  1778. },
  1779. {
  1780. .name = "Single read performance by transfer size",
  1781. .prepare = mmc_test_area_prepare_fill,
  1782. .run = mmc_test_profile_read_perf,
  1783. .cleanup = mmc_test_area_cleanup,
  1784. },
  1785. {
  1786. .name = "Single write performance by transfer size",
  1787. .prepare = mmc_test_area_prepare,
  1788. .run = mmc_test_profile_write_perf,
  1789. .cleanup = mmc_test_area_cleanup,
  1790. },
  1791. {
  1792. .name = "Single trim performance by transfer size",
  1793. .prepare = mmc_test_area_prepare_fill,
  1794. .run = mmc_test_profile_trim_perf,
  1795. .cleanup = mmc_test_area_cleanup,
  1796. },
  1797. {
  1798. .name = "Consecutive read performance by transfer size",
  1799. .prepare = mmc_test_area_prepare_fill,
  1800. .run = mmc_test_profile_seq_read_perf,
  1801. .cleanup = mmc_test_area_cleanup,
  1802. },
  1803. {
  1804. .name = "Consecutive write performance by transfer size",
  1805. .prepare = mmc_test_area_prepare,
  1806. .run = mmc_test_profile_seq_write_perf,
  1807. .cleanup = mmc_test_area_cleanup,
  1808. },
  1809. {
  1810. .name = "Consecutive trim performance by transfer size",
  1811. .prepare = mmc_test_area_prepare,
  1812. .run = mmc_test_profile_seq_trim_perf,
  1813. .cleanup = mmc_test_area_cleanup,
  1814. },
  1815. {
  1816. .name = "Random read performance by transfer size",
  1817. .prepare = mmc_test_area_prepare,
  1818. .run = mmc_test_random_read_perf,
  1819. .cleanup = mmc_test_area_cleanup,
  1820. },
  1821. {
  1822. .name = "Random write performance by transfer size",
  1823. .prepare = mmc_test_area_prepare,
  1824. .run = mmc_test_random_write_perf,
  1825. .cleanup = mmc_test_area_cleanup,
  1826. },
  1827. {
  1828. .name = "Large sequential read into scattered pages",
  1829. .prepare = mmc_test_area_prepare,
  1830. .run = mmc_test_large_seq_read_perf,
  1831. .cleanup = mmc_test_area_cleanup,
  1832. },
  1833. {
  1834. .name = "Large sequential write from scattered pages",
  1835. .prepare = mmc_test_area_prepare,
  1836. .run = mmc_test_large_seq_write_perf,
  1837. .cleanup = mmc_test_area_cleanup,
  1838. },
  1839. };
  1840. static DEFINE_MUTEX(mmc_test_lock);
  1841. static LIST_HEAD(mmc_test_result);
  1842. static void mmc_test_run(struct mmc_test_card *test, int testcase)
  1843. {
  1844. int i, ret;
  1845. printk(KERN_INFO "%s: Starting tests of card %s...\n",
  1846. mmc_hostname(test->card->host), mmc_card_id(test->card));
  1847. mmc_claim_host(test->card->host);
  1848. for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
  1849. struct mmc_test_general_result *gr;
  1850. if (testcase && ((i + 1) != testcase))
  1851. continue;
  1852. printk(KERN_INFO "%s: Test case %d. %s...\n",
  1853. mmc_hostname(test->card->host), i + 1,
  1854. mmc_test_cases[i].name);
  1855. if (mmc_test_cases[i].prepare) {
  1856. ret = mmc_test_cases[i].prepare(test);
  1857. if (ret) {
  1858. printk(KERN_INFO "%s: Result: Prepare "
  1859. "stage failed! (%d)\n",
  1860. mmc_hostname(test->card->host),
  1861. ret);
  1862. continue;
  1863. }
  1864. }
  1865. gr = kzalloc(sizeof(struct mmc_test_general_result),
  1866. GFP_KERNEL);
  1867. if (gr) {
  1868. INIT_LIST_HEAD(&gr->tr_lst);
  1869. /* Assign data what we know already */
  1870. gr->card = test->card;
  1871. gr->testcase = i;
  1872. /* Append container to global one */
  1873. list_add_tail(&gr->link, &mmc_test_result);
  1874. /*
  1875. * Save the pointer to created container in our private
  1876. * structure.
  1877. */
  1878. test->gr = gr;
  1879. }
  1880. ret = mmc_test_cases[i].run(test);
  1881. switch (ret) {
  1882. case RESULT_OK:
  1883. printk(KERN_INFO "%s: Result: OK\n",
  1884. mmc_hostname(test->card->host));
  1885. break;
  1886. case RESULT_FAIL:
  1887. printk(KERN_INFO "%s: Result: FAILED\n",
  1888. mmc_hostname(test->card->host));
  1889. break;
  1890. case RESULT_UNSUP_HOST:
  1891. printk(KERN_INFO "%s: Result: UNSUPPORTED "
  1892. "(by host)\n",
  1893. mmc_hostname(test->card->host));
  1894. break;
  1895. case RESULT_UNSUP_CARD:
  1896. printk(KERN_INFO "%s: Result: UNSUPPORTED "
  1897. "(by card)\n",
  1898. mmc_hostname(test->card->host));
  1899. break;
  1900. default:
  1901. printk(KERN_INFO "%s: Result: ERROR (%d)\n",
  1902. mmc_hostname(test->card->host), ret);
  1903. }
  1904. /* Save the result */
  1905. if (gr)
  1906. gr->result = ret;
  1907. if (mmc_test_cases[i].cleanup) {
  1908. ret = mmc_test_cases[i].cleanup(test);
  1909. if (ret) {
  1910. printk(KERN_INFO "%s: Warning: Cleanup "
  1911. "stage failed! (%d)\n",
  1912. mmc_hostname(test->card->host),
  1913. ret);
  1914. }
  1915. }
  1916. }
  1917. mmc_release_host(test->card->host);
  1918. printk(KERN_INFO "%s: Tests completed.\n",
  1919. mmc_hostname(test->card->host));
  1920. }
  1921. static void mmc_test_free_result(struct mmc_card *card)
  1922. {
  1923. struct mmc_test_general_result *gr, *grs;
  1924. mutex_lock(&mmc_test_lock);
  1925. list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
  1926. struct mmc_test_transfer_result *tr, *trs;
  1927. if (card && gr->card != card)
  1928. continue;
  1929. list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
  1930. list_del(&tr->link);
  1931. kfree(tr);
  1932. }
  1933. list_del(&gr->link);
  1934. kfree(gr);
  1935. }
  1936. mutex_unlock(&mmc_test_lock);
  1937. }
  1938. static LIST_HEAD(mmc_test_file_test);
  1939. static int mtf_test_show(struct seq_file *sf, void *data)
  1940. {
  1941. struct mmc_card *card = (struct mmc_card *)sf->private;
  1942. struct mmc_test_general_result *gr;
  1943. mutex_lock(&mmc_test_lock);
  1944. list_for_each_entry(gr, &mmc_test_result, link) {
  1945. struct mmc_test_transfer_result *tr;
  1946. if (gr->card != card)
  1947. continue;
  1948. seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
  1949. list_for_each_entry(tr, &gr->tr_lst, link) {
  1950. seq_printf(sf, "%u %d %lu.%09lu %u %u.%02u\n",
  1951. tr->count, tr->sectors,
  1952. (unsigned long)tr->ts.tv_sec,
  1953. (unsigned long)tr->ts.tv_nsec,
  1954. tr->rate, tr->iops / 100, tr->iops % 100);
  1955. }
  1956. }
  1957. mutex_unlock(&mmc_test_lock);
  1958. return 0;
  1959. }
  1960. static int mtf_test_open(struct inode *inode, struct file *file)
  1961. {
  1962. return single_open(file, mtf_test_show, inode->i_private);
  1963. }
  1964. static ssize_t mtf_test_write(struct file *file, const char __user *buf,
  1965. size_t count, loff_t *pos)
  1966. {
  1967. struct seq_file *sf = (struct seq_file *)file->private_data;
  1968. struct mmc_card *card = (struct mmc_card *)sf->private;
  1969. struct mmc_test_card *test;
  1970. char lbuf[12];
  1971. long testcase;
  1972. if (count >= sizeof(lbuf))
  1973. return -EINVAL;
  1974. if (copy_from_user(lbuf, buf, count))
  1975. return -EFAULT;
  1976. lbuf[count] = '\0';
  1977. if (strict_strtol(lbuf, 10, &testcase))
  1978. return -EINVAL;
  1979. test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
  1980. if (!test)
  1981. return -ENOMEM;
  1982. /*
  1983. * Remove all test cases associated with given card. Thus we have only
  1984. * actual data of the last run.
  1985. */
  1986. mmc_test_free_result(card);
  1987. test->card = card;
  1988. test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
  1989. #ifdef CONFIG_HIGHMEM
  1990. test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
  1991. #endif
  1992. #ifdef CONFIG_HIGHMEM
  1993. if (test->buffer && test->highmem) {
  1994. #else
  1995. if (test->buffer) {
  1996. #endif
  1997. mutex_lock(&mmc_test_lock);
  1998. mmc_test_run(test, testcase);
  1999. mutex_unlock(&mmc_test_lock);
  2000. }
  2001. #ifdef CONFIG_HIGHMEM
  2002. __free_pages(test->highmem, BUFFER_ORDER);
  2003. #endif
  2004. kfree(test->buffer);
  2005. kfree(test);
  2006. return count;
  2007. }
  2008. static const struct file_operations mmc_test_fops_test = {
  2009. .open = mtf_test_open,
  2010. .read = seq_read,
  2011. .write = mtf_test_write,
  2012. .llseek = seq_lseek,
  2013. .release = single_release,
  2014. };
  2015. static void mmc_test_free_file_test(struct mmc_card *card)
  2016. {
  2017. struct mmc_test_dbgfs_file *df, *dfs;
  2018. mutex_lock(&mmc_test_lock);
  2019. list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
  2020. if (card && df->card != card)
  2021. continue;
  2022. debugfs_remove(df->file);
  2023. list_del(&df->link);
  2024. kfree(df);
  2025. }
  2026. mutex_unlock(&mmc_test_lock);
  2027. }
  2028. static int mmc_test_register_file_test(struct mmc_card *card)
  2029. {
  2030. struct dentry *file = NULL;
  2031. struct mmc_test_dbgfs_file *df;
  2032. int ret = 0;
  2033. mutex_lock(&mmc_test_lock);
  2034. if (card->debugfs_root)
  2035. file = debugfs_create_file("test", S_IWUSR | S_IRUGO,
  2036. card->debugfs_root, card, &mmc_test_fops_test);
  2037. if (IS_ERR_OR_NULL(file)) {
  2038. dev_err(&card->dev,
  2039. "Can't create file. Perhaps debugfs is disabled.\n");
  2040. ret = -ENODEV;
  2041. goto err;
  2042. }
  2043. df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
  2044. if (!df) {
  2045. debugfs_remove(file);
  2046. dev_err(&card->dev,
  2047. "Can't allocate memory for internal usage.\n");
  2048. ret = -ENOMEM;
  2049. goto err;
  2050. }
  2051. df->card = card;
  2052. df->file = file;
  2053. list_add(&df->link, &mmc_test_file_test);
  2054. err:
  2055. mutex_unlock(&mmc_test_lock);
  2056. return ret;
  2057. }
  2058. static int mmc_test_probe(struct mmc_card *card)
  2059. {
  2060. int ret;
  2061. if (!mmc_card_mmc(card) && !mmc_card_sd(card))
  2062. return -ENODEV;
  2063. ret = mmc_test_register_file_test(card);
  2064. if (ret)
  2065. return ret;
  2066. dev_info(&card->dev, "Card claimed for testing.\n");
  2067. return 0;
  2068. }
  2069. static void mmc_test_remove(struct mmc_card *card)
  2070. {
  2071. mmc_test_free_result(card);
  2072. mmc_test_free_file_test(card);
  2073. }
  2074. static struct mmc_driver mmc_driver = {
  2075. .drv = {
  2076. .name = "mmc_test",
  2077. },
  2078. .probe = mmc_test_probe,
  2079. .remove = mmc_test_remove,
  2080. };
  2081. static int __init mmc_test_init(void)
  2082. {
  2083. return mmc_register_driver(&mmc_driver);
  2084. }
  2085. static void __exit mmc_test_exit(void)
  2086. {
  2087. /* Clear stalled data if card is still plugged */
  2088. mmc_test_free_result(NULL);
  2089. mmc_test_free_file_test(NULL);
  2090. mmc_unregister_driver(&mmc_driver);
  2091. }
  2092. module_init(mmc_test_init);
  2093. module_exit(mmc_test_exit);
  2094. MODULE_LICENSE("GPL");
  2095. MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
  2096. MODULE_AUTHOR("Pierre Ossman");