mmc_test.c 73 KB

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