mmc_test.c 63 KB

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