mmc_test.c 73 KB

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