mmc_test.c 55 KB

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