mmc_test.c 67 KB

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