mmc_test.c 67 KB

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