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_seg_sz -= t->max_seg_sz % 512;
  1286. t->max_tfr = t->max_sz;
  1287. if (t->max_tfr >> 9 > test->card->host->max_blk_count)
  1288. t->max_tfr = test->card->host->max_blk_count << 9;
  1289. if (t->max_tfr > test->card->host->max_req_size)
  1290. t->max_tfr = test->card->host->max_req_size;
  1291. if (t->max_tfr / t->max_seg_sz > t->max_segs)
  1292. t->max_tfr = t->max_segs * t->max_seg_sz;
  1293. /*
  1294. * Try to allocate enough memory for a max. sized transfer. Less is OK
  1295. * because the same memory can be mapped into the scatterlist more than
  1296. * once. Also, take into account the limits imposed on scatterlist
  1297. * segments by the host driver.
  1298. */
  1299. t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
  1300. t->max_seg_sz);
  1301. if (!t->mem)
  1302. return -ENOMEM;
  1303. t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
  1304. if (!t->sg) {
  1305. ret = -ENOMEM;
  1306. goto out_free;
  1307. }
  1308. t->dev_addr = mmc_test_capacity(test->card) / 2;
  1309. t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
  1310. if (erase) {
  1311. ret = mmc_test_area_erase(test);
  1312. if (ret)
  1313. goto out_free;
  1314. }
  1315. if (fill) {
  1316. ret = mmc_test_area_fill(test);
  1317. if (ret)
  1318. goto out_free;
  1319. }
  1320. return 0;
  1321. out_free:
  1322. mmc_test_area_cleanup(test);
  1323. return ret;
  1324. }
  1325. /*
  1326. * Prepare for large transfers. Do not erase the test area.
  1327. */
  1328. static int mmc_test_area_prepare(struct mmc_test_card *test)
  1329. {
  1330. return mmc_test_area_init(test, 0, 0);
  1331. }
  1332. /*
  1333. * Prepare for large transfers. Do erase the test area.
  1334. */
  1335. static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
  1336. {
  1337. return mmc_test_area_init(test, 1, 0);
  1338. }
  1339. /*
  1340. * Prepare for large transfers. Erase and fill the test area.
  1341. */
  1342. static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
  1343. {
  1344. return mmc_test_area_init(test, 1, 1);
  1345. }
  1346. /*
  1347. * Test best-case performance. Best-case performance is expected from
  1348. * a single large transfer.
  1349. *
  1350. * An additional option (max_scatter) allows the measurement of the same
  1351. * transfer but with no contiguous pages in the scatter list. This tests
  1352. * the efficiency of DMA to handle scattered pages.
  1353. */
  1354. static int mmc_test_best_performance(struct mmc_test_card *test, int write,
  1355. int max_scatter)
  1356. {
  1357. struct mmc_test_area *t = &test->area;
  1358. return mmc_test_area_io(test, t->max_tfr, t->dev_addr, write,
  1359. max_scatter, 1);
  1360. }
  1361. /*
  1362. * Best-case read performance.
  1363. */
  1364. static int mmc_test_best_read_performance(struct mmc_test_card *test)
  1365. {
  1366. return mmc_test_best_performance(test, 0, 0);
  1367. }
  1368. /*
  1369. * Best-case write performance.
  1370. */
  1371. static int mmc_test_best_write_performance(struct mmc_test_card *test)
  1372. {
  1373. return mmc_test_best_performance(test, 1, 0);
  1374. }
  1375. /*
  1376. * Best-case read performance into scattered pages.
  1377. */
  1378. static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
  1379. {
  1380. return mmc_test_best_performance(test, 0, 1);
  1381. }
  1382. /*
  1383. * Best-case write performance from scattered pages.
  1384. */
  1385. static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
  1386. {
  1387. return mmc_test_best_performance(test, 1, 1);
  1388. }
  1389. /*
  1390. * Single read performance by transfer size.
  1391. */
  1392. static int mmc_test_profile_read_perf(struct mmc_test_card *test)
  1393. {
  1394. struct mmc_test_area *t = &test->area;
  1395. unsigned long sz;
  1396. unsigned int dev_addr;
  1397. int ret;
  1398. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1399. dev_addr = t->dev_addr + (sz >> 9);
  1400. ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
  1401. if (ret)
  1402. return ret;
  1403. }
  1404. sz = t->max_tfr;
  1405. dev_addr = t->dev_addr;
  1406. return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
  1407. }
  1408. /*
  1409. * Single write performance by transfer size.
  1410. */
  1411. static int mmc_test_profile_write_perf(struct mmc_test_card *test)
  1412. {
  1413. struct mmc_test_area *t = &test->area;
  1414. unsigned long sz;
  1415. unsigned int dev_addr;
  1416. int ret;
  1417. ret = mmc_test_area_erase(test);
  1418. if (ret)
  1419. return ret;
  1420. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1421. dev_addr = t->dev_addr + (sz >> 9);
  1422. ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
  1423. if (ret)
  1424. return ret;
  1425. }
  1426. ret = mmc_test_area_erase(test);
  1427. if (ret)
  1428. return ret;
  1429. sz = t->max_tfr;
  1430. dev_addr = t->dev_addr;
  1431. return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
  1432. }
  1433. /*
  1434. * Single trim performance by transfer size.
  1435. */
  1436. static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
  1437. {
  1438. struct mmc_test_area *t = &test->area;
  1439. unsigned long sz;
  1440. unsigned int dev_addr;
  1441. struct timespec ts1, ts2;
  1442. int ret;
  1443. if (!mmc_can_trim(test->card))
  1444. return RESULT_UNSUP_CARD;
  1445. if (!mmc_can_erase(test->card))
  1446. return RESULT_UNSUP_HOST;
  1447. for (sz = 512; sz < t->max_sz; sz <<= 1) {
  1448. dev_addr = t->dev_addr + (sz >> 9);
  1449. getnstimeofday(&ts1);
  1450. ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
  1451. if (ret)
  1452. return ret;
  1453. getnstimeofday(&ts2);
  1454. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1455. }
  1456. dev_addr = t->dev_addr;
  1457. getnstimeofday(&ts1);
  1458. ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
  1459. if (ret)
  1460. return ret;
  1461. getnstimeofday(&ts2);
  1462. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1463. return 0;
  1464. }
  1465. static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
  1466. {
  1467. struct mmc_test_area *t = &test->area;
  1468. unsigned int dev_addr, i, cnt;
  1469. struct timespec ts1, ts2;
  1470. int ret;
  1471. cnt = t->max_sz / sz;
  1472. dev_addr = t->dev_addr;
  1473. getnstimeofday(&ts1);
  1474. for (i = 0; i < cnt; i++) {
  1475. ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
  1476. if (ret)
  1477. return ret;
  1478. dev_addr += (sz >> 9);
  1479. }
  1480. getnstimeofday(&ts2);
  1481. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1482. return 0;
  1483. }
  1484. /*
  1485. * Consecutive read performance by transfer size.
  1486. */
  1487. static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
  1488. {
  1489. struct mmc_test_area *t = &test->area;
  1490. unsigned long sz;
  1491. int ret;
  1492. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1493. ret = mmc_test_seq_read_perf(test, sz);
  1494. if (ret)
  1495. return ret;
  1496. }
  1497. sz = t->max_tfr;
  1498. return mmc_test_seq_read_perf(test, sz);
  1499. }
  1500. static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
  1501. {
  1502. struct mmc_test_area *t = &test->area;
  1503. unsigned int dev_addr, i, cnt;
  1504. struct timespec ts1, ts2;
  1505. int ret;
  1506. ret = mmc_test_area_erase(test);
  1507. if (ret)
  1508. return ret;
  1509. cnt = t->max_sz / sz;
  1510. dev_addr = t->dev_addr;
  1511. getnstimeofday(&ts1);
  1512. for (i = 0; i < cnt; i++) {
  1513. ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
  1514. if (ret)
  1515. return ret;
  1516. dev_addr += (sz >> 9);
  1517. }
  1518. getnstimeofday(&ts2);
  1519. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1520. return 0;
  1521. }
  1522. /*
  1523. * Consecutive write performance by transfer size.
  1524. */
  1525. static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
  1526. {
  1527. struct mmc_test_area *t = &test->area;
  1528. unsigned long sz;
  1529. int ret;
  1530. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1531. ret = mmc_test_seq_write_perf(test, sz);
  1532. if (ret)
  1533. return ret;
  1534. }
  1535. sz = t->max_tfr;
  1536. return mmc_test_seq_write_perf(test, sz);
  1537. }
  1538. /*
  1539. * Consecutive trim performance by transfer size.
  1540. */
  1541. static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
  1542. {
  1543. struct mmc_test_area *t = &test->area;
  1544. unsigned long sz;
  1545. unsigned int dev_addr, i, cnt;
  1546. struct timespec ts1, ts2;
  1547. int ret;
  1548. if (!mmc_can_trim(test->card))
  1549. return RESULT_UNSUP_CARD;
  1550. if (!mmc_can_erase(test->card))
  1551. return RESULT_UNSUP_HOST;
  1552. for (sz = 512; sz <= t->max_sz; sz <<= 1) {
  1553. ret = mmc_test_area_erase(test);
  1554. if (ret)
  1555. return ret;
  1556. ret = mmc_test_area_fill(test);
  1557. if (ret)
  1558. return ret;
  1559. cnt = t->max_sz / sz;
  1560. dev_addr = t->dev_addr;
  1561. getnstimeofday(&ts1);
  1562. for (i = 0; i < cnt; i++) {
  1563. ret = mmc_erase(test->card, dev_addr, sz >> 9,
  1564. MMC_TRIM_ARG);
  1565. if (ret)
  1566. return ret;
  1567. dev_addr += (sz >> 9);
  1568. }
  1569. getnstimeofday(&ts2);
  1570. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1571. }
  1572. return 0;
  1573. }
  1574. static unsigned int rnd_next = 1;
  1575. static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt)
  1576. {
  1577. uint64_t r;
  1578. rnd_next = rnd_next * 1103515245 + 12345;
  1579. r = (rnd_next >> 16) & 0x7fff;
  1580. return (r * rnd_cnt) >> 15;
  1581. }
  1582. static int mmc_test_rnd_perf(struct mmc_test_card *test, int write, int print,
  1583. unsigned long sz)
  1584. {
  1585. unsigned int dev_addr, cnt, rnd_addr, range1, range2, last_ea = 0, ea;
  1586. unsigned int ssz;
  1587. struct timespec ts1, ts2, ts;
  1588. int ret;
  1589. ssz = sz >> 9;
  1590. rnd_addr = mmc_test_capacity(test->card) / 4;
  1591. range1 = rnd_addr / test->card->pref_erase;
  1592. range2 = range1 / ssz;
  1593. getnstimeofday(&ts1);
  1594. for (cnt = 0; cnt < UINT_MAX; cnt++) {
  1595. getnstimeofday(&ts2);
  1596. ts = timespec_sub(ts2, ts1);
  1597. if (ts.tv_sec >= 10)
  1598. break;
  1599. ea = mmc_test_rnd_num(range1);
  1600. if (ea == last_ea)
  1601. ea -= 1;
  1602. last_ea = ea;
  1603. dev_addr = rnd_addr + test->card->pref_erase * ea +
  1604. ssz * mmc_test_rnd_num(range2);
  1605. ret = mmc_test_area_io(test, sz, dev_addr, write, 0, 0);
  1606. if (ret)
  1607. return ret;
  1608. }
  1609. if (print)
  1610. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1611. return 0;
  1612. }
  1613. static int mmc_test_random_perf(struct mmc_test_card *test, int write)
  1614. {
  1615. struct mmc_test_area *t = &test->area;
  1616. unsigned int next;
  1617. unsigned long sz;
  1618. int ret;
  1619. for (sz = 512; sz < t->max_tfr; sz <<= 1) {
  1620. /*
  1621. * When writing, try to get more consistent results by running
  1622. * the test twice with exactly the same I/O but outputting the
  1623. * results only for the 2nd run.
  1624. */
  1625. if (write) {
  1626. next = rnd_next;
  1627. ret = mmc_test_rnd_perf(test, write, 0, sz);
  1628. if (ret)
  1629. return ret;
  1630. rnd_next = next;
  1631. }
  1632. ret = mmc_test_rnd_perf(test, write, 1, sz);
  1633. if (ret)
  1634. return ret;
  1635. }
  1636. sz = t->max_tfr;
  1637. if (write) {
  1638. next = rnd_next;
  1639. ret = mmc_test_rnd_perf(test, write, 0, sz);
  1640. if (ret)
  1641. return ret;
  1642. rnd_next = next;
  1643. }
  1644. return mmc_test_rnd_perf(test, write, 1, sz);
  1645. }
  1646. /*
  1647. * Random read performance by transfer size.
  1648. */
  1649. static int mmc_test_random_read_perf(struct mmc_test_card *test)
  1650. {
  1651. return mmc_test_random_perf(test, 0);
  1652. }
  1653. /*
  1654. * Random write performance by transfer size.
  1655. */
  1656. static int mmc_test_random_write_perf(struct mmc_test_card *test)
  1657. {
  1658. return mmc_test_random_perf(test, 1);
  1659. }
  1660. static int mmc_test_seq_perf(struct mmc_test_card *test, int write,
  1661. unsigned int tot_sz, int max_scatter)
  1662. {
  1663. struct mmc_test_area *t = &test->area;
  1664. unsigned int dev_addr, i, cnt, sz, ssz;
  1665. struct timespec ts1, ts2;
  1666. int ret;
  1667. sz = t->max_tfr;
  1668. /*
  1669. * In the case of a maximally scattered transfer, the maximum transfer
  1670. * size is further limited by using PAGE_SIZE segments.
  1671. */
  1672. if (max_scatter) {
  1673. unsigned long max_tfr;
  1674. if (t->max_seg_sz >= PAGE_SIZE)
  1675. max_tfr = t->max_segs * PAGE_SIZE;
  1676. else
  1677. max_tfr = t->max_segs * t->max_seg_sz;
  1678. if (sz > max_tfr)
  1679. sz = max_tfr;
  1680. }
  1681. ssz = sz >> 9;
  1682. dev_addr = mmc_test_capacity(test->card) / 4;
  1683. if (tot_sz > dev_addr << 9)
  1684. tot_sz = dev_addr << 9;
  1685. cnt = tot_sz / sz;
  1686. dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
  1687. getnstimeofday(&ts1);
  1688. for (i = 0; i < cnt; i++) {
  1689. ret = mmc_test_area_io(test, sz, dev_addr, write,
  1690. max_scatter, 0);
  1691. if (ret)
  1692. return ret;
  1693. dev_addr += ssz;
  1694. }
  1695. getnstimeofday(&ts2);
  1696. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1697. return 0;
  1698. }
  1699. static int mmc_test_large_seq_perf(struct mmc_test_card *test, int write)
  1700. {
  1701. int ret, i;
  1702. for (i = 0; i < 10; i++) {
  1703. ret = mmc_test_seq_perf(test, write, 10 * 1024 * 1024, 1);
  1704. if (ret)
  1705. return ret;
  1706. }
  1707. for (i = 0; i < 5; i++) {
  1708. ret = mmc_test_seq_perf(test, write, 100 * 1024 * 1024, 1);
  1709. if (ret)
  1710. return ret;
  1711. }
  1712. for (i = 0; i < 3; i++) {
  1713. ret = mmc_test_seq_perf(test, write, 1000 * 1024 * 1024, 1);
  1714. if (ret)
  1715. return ret;
  1716. }
  1717. return ret;
  1718. }
  1719. /*
  1720. * Large sequential read performance.
  1721. */
  1722. static int mmc_test_large_seq_read_perf(struct mmc_test_card *test)
  1723. {
  1724. return mmc_test_large_seq_perf(test, 0);
  1725. }
  1726. /*
  1727. * Large sequential write performance.
  1728. */
  1729. static int mmc_test_large_seq_write_perf(struct mmc_test_card *test)
  1730. {
  1731. return mmc_test_large_seq_perf(test, 1);
  1732. }
  1733. static int mmc_test_rw_multiple(struct mmc_test_card *test,
  1734. struct mmc_test_multiple_rw *tdata,
  1735. unsigned int reqsize, unsigned int size,
  1736. int min_sg_len)
  1737. {
  1738. unsigned int dev_addr;
  1739. struct mmc_test_area *t = &test->area;
  1740. int ret = 0;
  1741. /* Set up test area */
  1742. if (size > mmc_test_capacity(test->card) / 2 * 512)
  1743. size = mmc_test_capacity(test->card) / 2 * 512;
  1744. if (reqsize > t->max_tfr)
  1745. reqsize = t->max_tfr;
  1746. dev_addr = mmc_test_capacity(test->card) / 4;
  1747. if ((dev_addr & 0xffff0000))
  1748. dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
  1749. else
  1750. dev_addr &= 0xfffff800; /* Round to 1MiB boundary */
  1751. if (!dev_addr)
  1752. goto err;
  1753. if (reqsize > size)
  1754. return 0;
  1755. /* prepare test area */
  1756. if (mmc_can_erase(test->card) &&
  1757. tdata->prepare & MMC_TEST_PREP_ERASE) {
  1758. ret = mmc_erase(test->card, dev_addr,
  1759. size / 512, MMC_SECURE_ERASE_ARG);
  1760. if (ret)
  1761. ret = mmc_erase(test->card, dev_addr,
  1762. size / 512, MMC_ERASE_ARG);
  1763. if (ret)
  1764. goto err;
  1765. }
  1766. /* Run test */
  1767. ret = mmc_test_area_io_seq(test, reqsize, dev_addr,
  1768. tdata->do_write, 0, 1, size / reqsize,
  1769. tdata->do_nonblock_req, min_sg_len);
  1770. if (ret)
  1771. goto err;
  1772. return ret;
  1773. err:
  1774. pr_info("[%s] error\n", __func__);
  1775. return ret;
  1776. }
  1777. static int mmc_test_rw_multiple_size(struct mmc_test_card *test,
  1778. struct mmc_test_multiple_rw *rw)
  1779. {
  1780. int ret = 0;
  1781. int i;
  1782. void *pre_req = test->card->host->ops->pre_req;
  1783. void *post_req = test->card->host->ops->post_req;
  1784. if (rw->do_nonblock_req &&
  1785. ((!pre_req && post_req) || (pre_req && !post_req))) {
  1786. pr_info("error: only one of pre/post is defined\n");
  1787. return -EINVAL;
  1788. }
  1789. for (i = 0 ; i < rw->len && ret == 0; i++) {
  1790. ret = mmc_test_rw_multiple(test, rw, rw->bs[i], rw->size, 0);
  1791. if (ret)
  1792. break;
  1793. }
  1794. return ret;
  1795. }
  1796. static int mmc_test_rw_multiple_sg_len(struct mmc_test_card *test,
  1797. struct mmc_test_multiple_rw *rw)
  1798. {
  1799. int ret = 0;
  1800. int i;
  1801. for (i = 0 ; i < rw->len && ret == 0; i++) {
  1802. ret = mmc_test_rw_multiple(test, rw, 512*1024, rw->size,
  1803. rw->sg_len[i]);
  1804. if (ret)
  1805. break;
  1806. }
  1807. return ret;
  1808. }
  1809. /*
  1810. * Multiple blocking write 4k to 4 MB chunks
  1811. */
  1812. static int mmc_test_profile_mult_write_blocking_perf(struct mmc_test_card *test)
  1813. {
  1814. unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
  1815. 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
  1816. struct mmc_test_multiple_rw test_data = {
  1817. .bs = bs,
  1818. .size = TEST_AREA_MAX_SIZE,
  1819. .len = ARRAY_SIZE(bs),
  1820. .do_write = true,
  1821. .do_nonblock_req = false,
  1822. .prepare = MMC_TEST_PREP_ERASE,
  1823. };
  1824. return mmc_test_rw_multiple_size(test, &test_data);
  1825. };
  1826. /*
  1827. * Multiple non-blocking write 4k to 4 MB chunks
  1828. */
  1829. static int mmc_test_profile_mult_write_nonblock_perf(struct mmc_test_card *test)
  1830. {
  1831. unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
  1832. 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
  1833. struct mmc_test_multiple_rw test_data = {
  1834. .bs = bs,
  1835. .size = TEST_AREA_MAX_SIZE,
  1836. .len = ARRAY_SIZE(bs),
  1837. .do_write = true,
  1838. .do_nonblock_req = true,
  1839. .prepare = MMC_TEST_PREP_ERASE,
  1840. };
  1841. return mmc_test_rw_multiple_size(test, &test_data);
  1842. }
  1843. /*
  1844. * Multiple blocking read 4k to 4 MB chunks
  1845. */
  1846. static int mmc_test_profile_mult_read_blocking_perf(struct mmc_test_card *test)
  1847. {
  1848. unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
  1849. 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
  1850. struct mmc_test_multiple_rw test_data = {
  1851. .bs = bs,
  1852. .size = TEST_AREA_MAX_SIZE,
  1853. .len = ARRAY_SIZE(bs),
  1854. .do_write = false,
  1855. .do_nonblock_req = false,
  1856. .prepare = MMC_TEST_PREP_NONE,
  1857. };
  1858. return mmc_test_rw_multiple_size(test, &test_data);
  1859. }
  1860. /*
  1861. * Multiple non-blocking read 4k to 4 MB chunks
  1862. */
  1863. static int mmc_test_profile_mult_read_nonblock_perf(struct mmc_test_card *test)
  1864. {
  1865. unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
  1866. 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
  1867. struct mmc_test_multiple_rw test_data = {
  1868. .bs = bs,
  1869. .size = TEST_AREA_MAX_SIZE,
  1870. .len = ARRAY_SIZE(bs),
  1871. .do_write = false,
  1872. .do_nonblock_req = true,
  1873. .prepare = MMC_TEST_PREP_NONE,
  1874. };
  1875. return mmc_test_rw_multiple_size(test, &test_data);
  1876. }
  1877. /*
  1878. * Multiple blocking write 1 to 512 sg elements
  1879. */
  1880. static int mmc_test_profile_sglen_wr_blocking_perf(struct mmc_test_card *test)
  1881. {
  1882. unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
  1883. 1 << 7, 1 << 8, 1 << 9};
  1884. struct mmc_test_multiple_rw test_data = {
  1885. .sg_len = sg_len,
  1886. .size = TEST_AREA_MAX_SIZE,
  1887. .len = ARRAY_SIZE(sg_len),
  1888. .do_write = true,
  1889. .do_nonblock_req = false,
  1890. .prepare = MMC_TEST_PREP_ERASE,
  1891. };
  1892. return mmc_test_rw_multiple_sg_len(test, &test_data);
  1893. };
  1894. /*
  1895. * Multiple non-blocking write 1 to 512 sg elements
  1896. */
  1897. static int mmc_test_profile_sglen_wr_nonblock_perf(struct mmc_test_card *test)
  1898. {
  1899. unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
  1900. 1 << 7, 1 << 8, 1 << 9};
  1901. struct mmc_test_multiple_rw test_data = {
  1902. .sg_len = sg_len,
  1903. .size = TEST_AREA_MAX_SIZE,
  1904. .len = ARRAY_SIZE(sg_len),
  1905. .do_write = true,
  1906. .do_nonblock_req = true,
  1907. .prepare = MMC_TEST_PREP_ERASE,
  1908. };
  1909. return mmc_test_rw_multiple_sg_len(test, &test_data);
  1910. }
  1911. /*
  1912. * Multiple blocking read 1 to 512 sg elements
  1913. */
  1914. static int mmc_test_profile_sglen_r_blocking_perf(struct mmc_test_card *test)
  1915. {
  1916. unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
  1917. 1 << 7, 1 << 8, 1 << 9};
  1918. struct mmc_test_multiple_rw test_data = {
  1919. .sg_len = sg_len,
  1920. .size = TEST_AREA_MAX_SIZE,
  1921. .len = ARRAY_SIZE(sg_len),
  1922. .do_write = false,
  1923. .do_nonblock_req = false,
  1924. .prepare = MMC_TEST_PREP_NONE,
  1925. };
  1926. return mmc_test_rw_multiple_sg_len(test, &test_data);
  1927. }
  1928. /*
  1929. * Multiple non-blocking read 1 to 512 sg elements
  1930. */
  1931. static int mmc_test_profile_sglen_r_nonblock_perf(struct mmc_test_card *test)
  1932. {
  1933. unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
  1934. 1 << 7, 1 << 8, 1 << 9};
  1935. struct mmc_test_multiple_rw test_data = {
  1936. .sg_len = sg_len,
  1937. .size = TEST_AREA_MAX_SIZE,
  1938. .len = ARRAY_SIZE(sg_len),
  1939. .do_write = false,
  1940. .do_nonblock_req = true,
  1941. .prepare = MMC_TEST_PREP_NONE,
  1942. };
  1943. return mmc_test_rw_multiple_sg_len(test, &test_data);
  1944. }
  1945. /*
  1946. * eMMC hardware reset.
  1947. */
  1948. static int mmc_test_hw_reset(struct mmc_test_card *test)
  1949. {
  1950. struct mmc_card *card = test->card;
  1951. struct mmc_host *host = card->host;
  1952. int err;
  1953. err = mmc_hw_reset_check(host);
  1954. if (!err)
  1955. return RESULT_OK;
  1956. if (err == -ENOSYS)
  1957. return RESULT_FAIL;
  1958. if (err != -EOPNOTSUPP)
  1959. return err;
  1960. if (!mmc_can_reset(card))
  1961. return RESULT_UNSUP_CARD;
  1962. return RESULT_UNSUP_HOST;
  1963. }
  1964. static const struct mmc_test_case mmc_test_cases[] = {
  1965. {
  1966. .name = "Basic write (no data verification)",
  1967. .run = mmc_test_basic_write,
  1968. },
  1969. {
  1970. .name = "Basic read (no data verification)",
  1971. .run = mmc_test_basic_read,
  1972. },
  1973. {
  1974. .name = "Basic write (with data verification)",
  1975. .prepare = mmc_test_prepare_write,
  1976. .run = mmc_test_verify_write,
  1977. .cleanup = mmc_test_cleanup,
  1978. },
  1979. {
  1980. .name = "Basic read (with data verification)",
  1981. .prepare = mmc_test_prepare_read,
  1982. .run = mmc_test_verify_read,
  1983. .cleanup = mmc_test_cleanup,
  1984. },
  1985. {
  1986. .name = "Multi-block write",
  1987. .prepare = mmc_test_prepare_write,
  1988. .run = mmc_test_multi_write,
  1989. .cleanup = mmc_test_cleanup,
  1990. },
  1991. {
  1992. .name = "Multi-block read",
  1993. .prepare = mmc_test_prepare_read,
  1994. .run = mmc_test_multi_read,
  1995. .cleanup = mmc_test_cleanup,
  1996. },
  1997. {
  1998. .name = "Power of two block writes",
  1999. .prepare = mmc_test_prepare_write,
  2000. .run = mmc_test_pow2_write,
  2001. .cleanup = mmc_test_cleanup,
  2002. },
  2003. {
  2004. .name = "Power of two block reads",
  2005. .prepare = mmc_test_prepare_read,
  2006. .run = mmc_test_pow2_read,
  2007. .cleanup = mmc_test_cleanup,
  2008. },
  2009. {
  2010. .name = "Weird sized block writes",
  2011. .prepare = mmc_test_prepare_write,
  2012. .run = mmc_test_weird_write,
  2013. .cleanup = mmc_test_cleanup,
  2014. },
  2015. {
  2016. .name = "Weird sized block reads",
  2017. .prepare = mmc_test_prepare_read,
  2018. .run = mmc_test_weird_read,
  2019. .cleanup = mmc_test_cleanup,
  2020. },
  2021. {
  2022. .name = "Badly aligned write",
  2023. .prepare = mmc_test_prepare_write,
  2024. .run = mmc_test_align_write,
  2025. .cleanup = mmc_test_cleanup,
  2026. },
  2027. {
  2028. .name = "Badly aligned read",
  2029. .prepare = mmc_test_prepare_read,
  2030. .run = mmc_test_align_read,
  2031. .cleanup = mmc_test_cleanup,
  2032. },
  2033. {
  2034. .name = "Badly aligned multi-block write",
  2035. .prepare = mmc_test_prepare_write,
  2036. .run = mmc_test_align_multi_write,
  2037. .cleanup = mmc_test_cleanup,
  2038. },
  2039. {
  2040. .name = "Badly aligned multi-block read",
  2041. .prepare = mmc_test_prepare_read,
  2042. .run = mmc_test_align_multi_read,
  2043. .cleanup = mmc_test_cleanup,
  2044. },
  2045. {
  2046. .name = "Correct xfer_size at write (start failure)",
  2047. .run = mmc_test_xfersize_write,
  2048. },
  2049. {
  2050. .name = "Correct xfer_size at read (start failure)",
  2051. .run = mmc_test_xfersize_read,
  2052. },
  2053. {
  2054. .name = "Correct xfer_size at write (midway failure)",
  2055. .run = mmc_test_multi_xfersize_write,
  2056. },
  2057. {
  2058. .name = "Correct xfer_size at read (midway failure)",
  2059. .run = mmc_test_multi_xfersize_read,
  2060. },
  2061. #ifdef CONFIG_HIGHMEM
  2062. {
  2063. .name = "Highmem write",
  2064. .prepare = mmc_test_prepare_write,
  2065. .run = mmc_test_write_high,
  2066. .cleanup = mmc_test_cleanup,
  2067. },
  2068. {
  2069. .name = "Highmem read",
  2070. .prepare = mmc_test_prepare_read,
  2071. .run = mmc_test_read_high,
  2072. .cleanup = mmc_test_cleanup,
  2073. },
  2074. {
  2075. .name = "Multi-block highmem write",
  2076. .prepare = mmc_test_prepare_write,
  2077. .run = mmc_test_multi_write_high,
  2078. .cleanup = mmc_test_cleanup,
  2079. },
  2080. {
  2081. .name = "Multi-block highmem read",
  2082. .prepare = mmc_test_prepare_read,
  2083. .run = mmc_test_multi_read_high,
  2084. .cleanup = mmc_test_cleanup,
  2085. },
  2086. #else
  2087. {
  2088. .name = "Highmem write",
  2089. .run = mmc_test_no_highmem,
  2090. },
  2091. {
  2092. .name = "Highmem read",
  2093. .run = mmc_test_no_highmem,
  2094. },
  2095. {
  2096. .name = "Multi-block highmem write",
  2097. .run = mmc_test_no_highmem,
  2098. },
  2099. {
  2100. .name = "Multi-block highmem read",
  2101. .run = mmc_test_no_highmem,
  2102. },
  2103. #endif /* CONFIG_HIGHMEM */
  2104. {
  2105. .name = "Best-case read performance",
  2106. .prepare = mmc_test_area_prepare_fill,
  2107. .run = mmc_test_best_read_performance,
  2108. .cleanup = mmc_test_area_cleanup,
  2109. },
  2110. {
  2111. .name = "Best-case write performance",
  2112. .prepare = mmc_test_area_prepare_erase,
  2113. .run = mmc_test_best_write_performance,
  2114. .cleanup = mmc_test_area_cleanup,
  2115. },
  2116. {
  2117. .name = "Best-case read performance into scattered pages",
  2118. .prepare = mmc_test_area_prepare_fill,
  2119. .run = mmc_test_best_read_perf_max_scatter,
  2120. .cleanup = mmc_test_area_cleanup,
  2121. },
  2122. {
  2123. .name = "Best-case write performance from scattered pages",
  2124. .prepare = mmc_test_area_prepare_erase,
  2125. .run = mmc_test_best_write_perf_max_scatter,
  2126. .cleanup = mmc_test_area_cleanup,
  2127. },
  2128. {
  2129. .name = "Single read performance by transfer size",
  2130. .prepare = mmc_test_area_prepare_fill,
  2131. .run = mmc_test_profile_read_perf,
  2132. .cleanup = mmc_test_area_cleanup,
  2133. },
  2134. {
  2135. .name = "Single write performance by transfer size",
  2136. .prepare = mmc_test_area_prepare,
  2137. .run = mmc_test_profile_write_perf,
  2138. .cleanup = mmc_test_area_cleanup,
  2139. },
  2140. {
  2141. .name = "Single trim performance by transfer size",
  2142. .prepare = mmc_test_area_prepare_fill,
  2143. .run = mmc_test_profile_trim_perf,
  2144. .cleanup = mmc_test_area_cleanup,
  2145. },
  2146. {
  2147. .name = "Consecutive read performance by transfer size",
  2148. .prepare = mmc_test_area_prepare_fill,
  2149. .run = mmc_test_profile_seq_read_perf,
  2150. .cleanup = mmc_test_area_cleanup,
  2151. },
  2152. {
  2153. .name = "Consecutive write performance by transfer size",
  2154. .prepare = mmc_test_area_prepare,
  2155. .run = mmc_test_profile_seq_write_perf,
  2156. .cleanup = mmc_test_area_cleanup,
  2157. },
  2158. {
  2159. .name = "Consecutive trim performance by transfer size",
  2160. .prepare = mmc_test_area_prepare,
  2161. .run = mmc_test_profile_seq_trim_perf,
  2162. .cleanup = mmc_test_area_cleanup,
  2163. },
  2164. {
  2165. .name = "Random read performance by transfer size",
  2166. .prepare = mmc_test_area_prepare,
  2167. .run = mmc_test_random_read_perf,
  2168. .cleanup = mmc_test_area_cleanup,
  2169. },
  2170. {
  2171. .name = "Random write performance by transfer size",
  2172. .prepare = mmc_test_area_prepare,
  2173. .run = mmc_test_random_write_perf,
  2174. .cleanup = mmc_test_area_cleanup,
  2175. },
  2176. {
  2177. .name = "Large sequential read into scattered pages",
  2178. .prepare = mmc_test_area_prepare,
  2179. .run = mmc_test_large_seq_read_perf,
  2180. .cleanup = mmc_test_area_cleanup,
  2181. },
  2182. {
  2183. .name = "Large sequential write from scattered pages",
  2184. .prepare = mmc_test_area_prepare,
  2185. .run = mmc_test_large_seq_write_perf,
  2186. .cleanup = mmc_test_area_cleanup,
  2187. },
  2188. {
  2189. .name = "Write performance with blocking req 4k to 4MB",
  2190. .prepare = mmc_test_area_prepare,
  2191. .run = mmc_test_profile_mult_write_blocking_perf,
  2192. .cleanup = mmc_test_area_cleanup,
  2193. },
  2194. {
  2195. .name = "Write performance with non-blocking req 4k to 4MB",
  2196. .prepare = mmc_test_area_prepare,
  2197. .run = mmc_test_profile_mult_write_nonblock_perf,
  2198. .cleanup = mmc_test_area_cleanup,
  2199. },
  2200. {
  2201. .name = "Read performance with blocking req 4k to 4MB",
  2202. .prepare = mmc_test_area_prepare,
  2203. .run = mmc_test_profile_mult_read_blocking_perf,
  2204. .cleanup = mmc_test_area_cleanup,
  2205. },
  2206. {
  2207. .name = "Read performance with non-blocking req 4k to 4MB",
  2208. .prepare = mmc_test_area_prepare,
  2209. .run = mmc_test_profile_mult_read_nonblock_perf,
  2210. .cleanup = mmc_test_area_cleanup,
  2211. },
  2212. {
  2213. .name = "Write performance blocking req 1 to 512 sg elems",
  2214. .prepare = mmc_test_area_prepare,
  2215. .run = mmc_test_profile_sglen_wr_blocking_perf,
  2216. .cleanup = mmc_test_area_cleanup,
  2217. },
  2218. {
  2219. .name = "Write performance non-blocking req 1 to 512 sg elems",
  2220. .prepare = mmc_test_area_prepare,
  2221. .run = mmc_test_profile_sglen_wr_nonblock_perf,
  2222. .cleanup = mmc_test_area_cleanup,
  2223. },
  2224. {
  2225. .name = "Read performance blocking req 1 to 512 sg elems",
  2226. .prepare = mmc_test_area_prepare,
  2227. .run = mmc_test_profile_sglen_r_blocking_perf,
  2228. .cleanup = mmc_test_area_cleanup,
  2229. },
  2230. {
  2231. .name = "Read performance non-blocking req 1 to 512 sg elems",
  2232. .prepare = mmc_test_area_prepare,
  2233. .run = mmc_test_profile_sglen_r_nonblock_perf,
  2234. .cleanup = mmc_test_area_cleanup,
  2235. },
  2236. {
  2237. .name = "eMMC hardware reset",
  2238. .run = mmc_test_hw_reset,
  2239. },
  2240. };
  2241. static DEFINE_MUTEX(mmc_test_lock);
  2242. static LIST_HEAD(mmc_test_result);
  2243. static void mmc_test_run(struct mmc_test_card *test, int testcase)
  2244. {
  2245. int i, ret;
  2246. pr_info("%s: Starting tests of card %s...\n",
  2247. mmc_hostname(test->card->host), mmc_card_id(test->card));
  2248. mmc_claim_host(test->card->host);
  2249. for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
  2250. struct mmc_test_general_result *gr;
  2251. if (testcase && ((i + 1) != testcase))
  2252. continue;
  2253. pr_info("%s: Test case %d. %s...\n",
  2254. mmc_hostname(test->card->host), i + 1,
  2255. mmc_test_cases[i].name);
  2256. if (mmc_test_cases[i].prepare) {
  2257. ret = mmc_test_cases[i].prepare(test);
  2258. if (ret) {
  2259. pr_info("%s: Result: Prepare "
  2260. "stage failed! (%d)\n",
  2261. mmc_hostname(test->card->host),
  2262. ret);
  2263. continue;
  2264. }
  2265. }
  2266. gr = kzalloc(sizeof(struct mmc_test_general_result),
  2267. GFP_KERNEL);
  2268. if (gr) {
  2269. INIT_LIST_HEAD(&gr->tr_lst);
  2270. /* Assign data what we know already */
  2271. gr->card = test->card;
  2272. gr->testcase = i;
  2273. /* Append container to global one */
  2274. list_add_tail(&gr->link, &mmc_test_result);
  2275. /*
  2276. * Save the pointer to created container in our private
  2277. * structure.
  2278. */
  2279. test->gr = gr;
  2280. }
  2281. ret = mmc_test_cases[i].run(test);
  2282. switch (ret) {
  2283. case RESULT_OK:
  2284. pr_info("%s: Result: OK\n",
  2285. mmc_hostname(test->card->host));
  2286. break;
  2287. case RESULT_FAIL:
  2288. pr_info("%s: Result: FAILED\n",
  2289. mmc_hostname(test->card->host));
  2290. break;
  2291. case RESULT_UNSUP_HOST:
  2292. pr_info("%s: Result: UNSUPPORTED "
  2293. "(by host)\n",
  2294. mmc_hostname(test->card->host));
  2295. break;
  2296. case RESULT_UNSUP_CARD:
  2297. pr_info("%s: Result: UNSUPPORTED "
  2298. "(by card)\n",
  2299. mmc_hostname(test->card->host));
  2300. break;
  2301. default:
  2302. pr_info("%s: Result: ERROR (%d)\n",
  2303. mmc_hostname(test->card->host), ret);
  2304. }
  2305. /* Save the result */
  2306. if (gr)
  2307. gr->result = ret;
  2308. if (mmc_test_cases[i].cleanup) {
  2309. ret = mmc_test_cases[i].cleanup(test);
  2310. if (ret) {
  2311. pr_info("%s: Warning: Cleanup "
  2312. "stage failed! (%d)\n",
  2313. mmc_hostname(test->card->host),
  2314. ret);
  2315. }
  2316. }
  2317. }
  2318. mmc_release_host(test->card->host);
  2319. pr_info("%s: Tests completed.\n",
  2320. mmc_hostname(test->card->host));
  2321. }
  2322. static void mmc_test_free_result(struct mmc_card *card)
  2323. {
  2324. struct mmc_test_general_result *gr, *grs;
  2325. mutex_lock(&mmc_test_lock);
  2326. list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
  2327. struct mmc_test_transfer_result *tr, *trs;
  2328. if (card && gr->card != card)
  2329. continue;
  2330. list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
  2331. list_del(&tr->link);
  2332. kfree(tr);
  2333. }
  2334. list_del(&gr->link);
  2335. kfree(gr);
  2336. }
  2337. mutex_unlock(&mmc_test_lock);
  2338. }
  2339. static LIST_HEAD(mmc_test_file_test);
  2340. static int mtf_test_show(struct seq_file *sf, void *data)
  2341. {
  2342. struct mmc_card *card = (struct mmc_card *)sf->private;
  2343. struct mmc_test_general_result *gr;
  2344. mutex_lock(&mmc_test_lock);
  2345. list_for_each_entry(gr, &mmc_test_result, link) {
  2346. struct mmc_test_transfer_result *tr;
  2347. if (gr->card != card)
  2348. continue;
  2349. seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
  2350. list_for_each_entry(tr, &gr->tr_lst, link) {
  2351. seq_printf(sf, "%u %d %lu.%09lu %u %u.%02u\n",
  2352. tr->count, tr->sectors,
  2353. (unsigned long)tr->ts.tv_sec,
  2354. (unsigned long)tr->ts.tv_nsec,
  2355. tr->rate, tr->iops / 100, tr->iops % 100);
  2356. }
  2357. }
  2358. mutex_unlock(&mmc_test_lock);
  2359. return 0;
  2360. }
  2361. static int mtf_test_open(struct inode *inode, struct file *file)
  2362. {
  2363. return single_open(file, mtf_test_show, inode->i_private);
  2364. }
  2365. static ssize_t mtf_test_write(struct file *file, const char __user *buf,
  2366. size_t count, loff_t *pos)
  2367. {
  2368. struct seq_file *sf = (struct seq_file *)file->private_data;
  2369. struct mmc_card *card = (struct mmc_card *)sf->private;
  2370. struct mmc_test_card *test;
  2371. long testcase;
  2372. int ret;
  2373. ret = kstrtol_from_user(buf, count, 10, &testcase);
  2374. if (ret)
  2375. return ret;
  2376. test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
  2377. if (!test)
  2378. return -ENOMEM;
  2379. /*
  2380. * Remove all test cases associated with given card. Thus we have only
  2381. * actual data of the last run.
  2382. */
  2383. mmc_test_free_result(card);
  2384. test->card = card;
  2385. test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
  2386. #ifdef CONFIG_HIGHMEM
  2387. test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
  2388. #endif
  2389. #ifdef CONFIG_HIGHMEM
  2390. if (test->buffer && test->highmem) {
  2391. #else
  2392. if (test->buffer) {
  2393. #endif
  2394. mutex_lock(&mmc_test_lock);
  2395. mmc_test_run(test, testcase);
  2396. mutex_unlock(&mmc_test_lock);
  2397. }
  2398. #ifdef CONFIG_HIGHMEM
  2399. __free_pages(test->highmem, BUFFER_ORDER);
  2400. #endif
  2401. kfree(test->buffer);
  2402. kfree(test);
  2403. return count;
  2404. }
  2405. static const struct file_operations mmc_test_fops_test = {
  2406. .open = mtf_test_open,
  2407. .read = seq_read,
  2408. .write = mtf_test_write,
  2409. .llseek = seq_lseek,
  2410. .release = single_release,
  2411. };
  2412. static int mtf_testlist_show(struct seq_file *sf, void *data)
  2413. {
  2414. int i;
  2415. mutex_lock(&mmc_test_lock);
  2416. for (i = 0; i < ARRAY_SIZE(mmc_test_cases); i++)
  2417. seq_printf(sf, "%d:\t%s\n", i+1, mmc_test_cases[i].name);
  2418. mutex_unlock(&mmc_test_lock);
  2419. return 0;
  2420. }
  2421. static int mtf_testlist_open(struct inode *inode, struct file *file)
  2422. {
  2423. return single_open(file, mtf_testlist_show, inode->i_private);
  2424. }
  2425. static const struct file_operations mmc_test_fops_testlist = {
  2426. .open = mtf_testlist_open,
  2427. .read = seq_read,
  2428. .llseek = seq_lseek,
  2429. .release = single_release,
  2430. };
  2431. static void mmc_test_free_dbgfs_file(struct mmc_card *card)
  2432. {
  2433. struct mmc_test_dbgfs_file *df, *dfs;
  2434. mutex_lock(&mmc_test_lock);
  2435. list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
  2436. if (card && df->card != card)
  2437. continue;
  2438. debugfs_remove(df->file);
  2439. list_del(&df->link);
  2440. kfree(df);
  2441. }
  2442. mutex_unlock(&mmc_test_lock);
  2443. }
  2444. static int __mmc_test_register_dbgfs_file(struct mmc_card *card,
  2445. const char *name, umode_t mode, const struct file_operations *fops)
  2446. {
  2447. struct dentry *file = NULL;
  2448. struct mmc_test_dbgfs_file *df;
  2449. if (card->debugfs_root)
  2450. file = debugfs_create_file(name, mode, card->debugfs_root,
  2451. card, fops);
  2452. if (IS_ERR_OR_NULL(file)) {
  2453. dev_err(&card->dev,
  2454. "Can't create %s. Perhaps debugfs is disabled.\n",
  2455. name);
  2456. return -ENODEV;
  2457. }
  2458. df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
  2459. if (!df) {
  2460. debugfs_remove(file);
  2461. dev_err(&card->dev,
  2462. "Can't allocate memory for internal usage.\n");
  2463. return -ENOMEM;
  2464. }
  2465. df->card = card;
  2466. df->file = file;
  2467. list_add(&df->link, &mmc_test_file_test);
  2468. return 0;
  2469. }
  2470. static int mmc_test_register_dbgfs_file(struct mmc_card *card)
  2471. {
  2472. int ret;
  2473. mutex_lock(&mmc_test_lock);
  2474. ret = __mmc_test_register_dbgfs_file(card, "test", S_IWUSR | S_IRUGO,
  2475. &mmc_test_fops_test);
  2476. if (ret)
  2477. goto err;
  2478. ret = __mmc_test_register_dbgfs_file(card, "testlist", S_IRUGO,
  2479. &mmc_test_fops_testlist);
  2480. if (ret)
  2481. goto err;
  2482. err:
  2483. mutex_unlock(&mmc_test_lock);
  2484. return ret;
  2485. }
  2486. static int mmc_test_probe(struct mmc_card *card)
  2487. {
  2488. int ret;
  2489. if (!mmc_card_mmc(card) && !mmc_card_sd(card))
  2490. return -ENODEV;
  2491. ret = mmc_test_register_dbgfs_file(card);
  2492. if (ret)
  2493. return ret;
  2494. dev_info(&card->dev, "Card claimed for testing.\n");
  2495. return 0;
  2496. }
  2497. static void mmc_test_remove(struct mmc_card *card)
  2498. {
  2499. mmc_test_free_result(card);
  2500. mmc_test_free_dbgfs_file(card);
  2501. }
  2502. static void mmc_test_shutdown(struct mmc_card *card)
  2503. {
  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. .shutdown = mmc_test_shutdown,
  2512. };
  2513. static int __init mmc_test_init(void)
  2514. {
  2515. return mmc_register_driver(&mmc_driver);
  2516. }
  2517. static void __exit mmc_test_exit(void)
  2518. {
  2519. /* Clear stalled data if card is still plugged */
  2520. mmc_test_free_result(NULL);
  2521. mmc_test_free_dbgfs_file(NULL);
  2522. mmc_unregister_driver(&mmc_driver);
  2523. }
  2524. module_init(mmc_test_init);
  2525. module_exit(mmc_test_exit);
  2526. MODULE_LICENSE("GPL");
  2527. MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
  2528. MODULE_AUTHOR("Pierre Ossman");