mmc_test.c 74 KB

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