mmc_test.c 67 KB

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