mmc_test.c 73 KB

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