mmc_test.c 67 KB

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