mmc_test.c 67 KB

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