mmc_test.c 73 KB

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