mmc_test.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  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. /*******************************************************************/
  137. /* General helper functions */
  138. /*******************************************************************/
  139. /*
  140. * Configure correct block size in card
  141. */
  142. static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
  143. {
  144. return mmc_set_blocklen(test->card, size);
  145. }
  146. /*
  147. * Fill in the mmc_request structure given a set of transfer parameters.
  148. */
  149. static void mmc_test_prepare_mrq(struct mmc_test_card *test,
  150. struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
  151. unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
  152. {
  153. BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
  154. if (blocks > 1) {
  155. mrq->cmd->opcode = write ?
  156. MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
  157. } else {
  158. mrq->cmd->opcode = write ?
  159. MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
  160. }
  161. mrq->cmd->arg = dev_addr;
  162. if (!mmc_card_blockaddr(test->card))
  163. mrq->cmd->arg <<= 9;
  164. mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  165. if (blocks == 1)
  166. mrq->stop = NULL;
  167. else {
  168. mrq->stop->opcode = MMC_STOP_TRANSMISSION;
  169. mrq->stop->arg = 0;
  170. mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  171. }
  172. mrq->data->blksz = blksz;
  173. mrq->data->blocks = blocks;
  174. mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
  175. mrq->data->sg = sg;
  176. mrq->data->sg_len = sg_len;
  177. mmc_set_data_timeout(mrq->data, test->card);
  178. }
  179. static int mmc_test_busy(struct mmc_command *cmd)
  180. {
  181. return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
  182. (R1_CURRENT_STATE(cmd->resp[0]) == 7);
  183. }
  184. /*
  185. * Wait for the card to finish the busy state
  186. */
  187. static int mmc_test_wait_busy(struct mmc_test_card *test)
  188. {
  189. int ret, busy;
  190. struct mmc_command cmd;
  191. busy = 0;
  192. do {
  193. memset(&cmd, 0, sizeof(struct mmc_command));
  194. cmd.opcode = MMC_SEND_STATUS;
  195. cmd.arg = test->card->rca << 16;
  196. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  197. ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
  198. if (ret)
  199. break;
  200. if (!busy && mmc_test_busy(&cmd)) {
  201. busy = 1;
  202. if (test->card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
  203. printk(KERN_INFO "%s: Warning: Host did not "
  204. "wait for busy state to end.\n",
  205. mmc_hostname(test->card->host));
  206. }
  207. } while (mmc_test_busy(&cmd));
  208. return ret;
  209. }
  210. /*
  211. * Transfer a single sector of kernel addressable data
  212. */
  213. static int mmc_test_buffer_transfer(struct mmc_test_card *test,
  214. u8 *buffer, unsigned addr, unsigned blksz, int write)
  215. {
  216. int ret;
  217. struct mmc_request mrq;
  218. struct mmc_command cmd;
  219. struct mmc_command stop;
  220. struct mmc_data data;
  221. struct scatterlist sg;
  222. memset(&mrq, 0, sizeof(struct mmc_request));
  223. memset(&cmd, 0, sizeof(struct mmc_command));
  224. memset(&data, 0, sizeof(struct mmc_data));
  225. memset(&stop, 0, sizeof(struct mmc_command));
  226. mrq.cmd = &cmd;
  227. mrq.data = &data;
  228. mrq.stop = &stop;
  229. sg_init_one(&sg, buffer, blksz);
  230. mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
  231. mmc_wait_for_req(test->card->host, &mrq);
  232. if (cmd.error)
  233. return cmd.error;
  234. if (data.error)
  235. return data.error;
  236. ret = mmc_test_wait_busy(test);
  237. if (ret)
  238. return ret;
  239. return 0;
  240. }
  241. static void mmc_test_free_mem(struct mmc_test_mem *mem)
  242. {
  243. if (!mem)
  244. return;
  245. while (mem->cnt--)
  246. __free_pages(mem->arr[mem->cnt].page,
  247. mem->arr[mem->cnt].order);
  248. kfree(mem->arr);
  249. kfree(mem);
  250. }
  251. /*
  252. * Allocate a lot of memory, preferrably max_sz but at least min_sz. In case
  253. * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
  254. * not exceed a maximum number of segments and try not to make segments much
  255. * bigger than maximum segment size.
  256. */
  257. static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
  258. unsigned long max_sz,
  259. unsigned int max_segs,
  260. unsigned int max_seg_sz)
  261. {
  262. unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
  263. unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
  264. unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
  265. unsigned long page_cnt = 0;
  266. unsigned long limit = nr_free_buffer_pages() >> 4;
  267. struct mmc_test_mem *mem;
  268. if (max_page_cnt > limit)
  269. max_page_cnt = limit;
  270. if (min_page_cnt > max_page_cnt)
  271. min_page_cnt = max_page_cnt;
  272. if (max_seg_page_cnt > max_page_cnt)
  273. max_seg_page_cnt = max_page_cnt;
  274. if (max_segs > max_page_cnt)
  275. max_segs = max_page_cnt;
  276. mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
  277. if (!mem)
  278. return NULL;
  279. mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_segs,
  280. GFP_KERNEL);
  281. if (!mem->arr)
  282. goto out_free;
  283. while (max_page_cnt) {
  284. struct page *page;
  285. unsigned int order;
  286. gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
  287. __GFP_NORETRY;
  288. order = get_order(max_seg_page_cnt << PAGE_SHIFT);
  289. while (1) {
  290. page = alloc_pages(flags, order);
  291. if (page || !order)
  292. break;
  293. order -= 1;
  294. }
  295. if (!page) {
  296. if (page_cnt < min_page_cnt)
  297. goto out_free;
  298. break;
  299. }
  300. mem->arr[mem->cnt].page = page;
  301. mem->arr[mem->cnt].order = order;
  302. mem->cnt += 1;
  303. if (max_page_cnt <= (1UL << order))
  304. break;
  305. max_page_cnt -= 1UL << order;
  306. page_cnt += 1UL << order;
  307. if (mem->cnt >= max_segs) {
  308. if (page_cnt < min_page_cnt)
  309. goto out_free;
  310. break;
  311. }
  312. }
  313. return mem;
  314. out_free:
  315. mmc_test_free_mem(mem);
  316. return NULL;
  317. }
  318. /*
  319. * Map memory into a scatterlist. Optionally allow the same memory to be
  320. * mapped more than once.
  321. */
  322. static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
  323. struct scatterlist *sglist, int repeat,
  324. unsigned int max_segs, unsigned int max_seg_sz,
  325. unsigned int *sg_len)
  326. {
  327. struct scatterlist *sg = NULL;
  328. unsigned int i;
  329. sg_init_table(sglist, max_segs);
  330. *sg_len = 0;
  331. do {
  332. for (i = 0; i < mem->cnt; i++) {
  333. unsigned long len = PAGE_SIZE << mem->arr[i].order;
  334. if (len > sz)
  335. len = sz;
  336. if (len > max_seg_sz)
  337. len = max_seg_sz;
  338. if (sg)
  339. sg = sg_next(sg);
  340. else
  341. sg = sglist;
  342. if (!sg)
  343. return -EINVAL;
  344. sg_set_page(sg, mem->arr[i].page, len, 0);
  345. sz -= len;
  346. *sg_len += 1;
  347. if (!sz)
  348. break;
  349. }
  350. } while (sz && repeat);
  351. if (sz)
  352. return -EINVAL;
  353. if (sg)
  354. sg_mark_end(sg);
  355. return 0;
  356. }
  357. /*
  358. * Map memory into a scatterlist so that no pages are contiguous. Allow the
  359. * same memory to be mapped more than once.
  360. */
  361. static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
  362. unsigned long sz,
  363. struct scatterlist *sglist,
  364. unsigned int max_segs,
  365. unsigned int max_seg_sz,
  366. unsigned int *sg_len)
  367. {
  368. struct scatterlist *sg = NULL;
  369. unsigned int i = mem->cnt, cnt;
  370. unsigned long len;
  371. void *base, *addr, *last_addr = NULL;
  372. sg_init_table(sglist, max_segs);
  373. *sg_len = 0;
  374. while (sz) {
  375. base = page_address(mem->arr[--i].page);
  376. cnt = 1 << mem->arr[i].order;
  377. while (sz && cnt) {
  378. addr = base + PAGE_SIZE * --cnt;
  379. if (last_addr && last_addr + PAGE_SIZE == addr)
  380. continue;
  381. last_addr = addr;
  382. len = PAGE_SIZE;
  383. if (len > max_seg_sz)
  384. len = max_seg_sz;
  385. if (len > sz)
  386. len = sz;
  387. if (sg)
  388. sg = sg_next(sg);
  389. else
  390. sg = sglist;
  391. if (!sg)
  392. return -EINVAL;
  393. sg_set_page(sg, virt_to_page(addr), len, 0);
  394. sz -= len;
  395. *sg_len += 1;
  396. }
  397. if (i == 0)
  398. i = mem->cnt;
  399. }
  400. if (sg)
  401. sg_mark_end(sg);
  402. return 0;
  403. }
  404. /*
  405. * Calculate transfer rate in bytes per second.
  406. */
  407. static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
  408. {
  409. uint64_t ns;
  410. ns = ts->tv_sec;
  411. ns *= 1000000000;
  412. ns += ts->tv_nsec;
  413. bytes *= 1000000000;
  414. while (ns > UINT_MAX) {
  415. bytes >>= 1;
  416. ns >>= 1;
  417. }
  418. if (!ns)
  419. return 0;
  420. do_div(bytes, (uint32_t)ns);
  421. return bytes;
  422. }
  423. /*
  424. * Save transfer results for future usage
  425. */
  426. static void mmc_test_save_transfer_result(struct mmc_test_card *test,
  427. unsigned int count, unsigned int sectors, struct timespec ts,
  428. unsigned int rate, unsigned int iops)
  429. {
  430. struct mmc_test_transfer_result *tr;
  431. if (!test->gr)
  432. return;
  433. tr = kmalloc(sizeof(struct mmc_test_transfer_result), GFP_KERNEL);
  434. if (!tr)
  435. return;
  436. tr->count = count;
  437. tr->sectors = sectors;
  438. tr->ts = ts;
  439. tr->rate = rate;
  440. tr->iops = iops;
  441. list_add_tail(&tr->link, &test->gr->tr_lst);
  442. }
  443. /*
  444. * Print the transfer rate.
  445. */
  446. static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
  447. struct timespec *ts1, struct timespec *ts2)
  448. {
  449. unsigned int rate, iops, sectors = bytes >> 9;
  450. struct timespec ts;
  451. ts = timespec_sub(*ts2, *ts1);
  452. rate = mmc_test_rate(bytes, &ts);
  453. iops = mmc_test_rate(100, &ts); /* I/O ops per sec x 100 */
  454. printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
  455. "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
  456. mmc_hostname(test->card->host), sectors, sectors >> 1,
  457. (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
  458. (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024,
  459. iops / 100, iops % 100);
  460. mmc_test_save_transfer_result(test, 1, sectors, ts, rate, iops);
  461. }
  462. /*
  463. * Print the average transfer rate.
  464. */
  465. static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
  466. unsigned int count, struct timespec *ts1,
  467. struct timespec *ts2)
  468. {
  469. unsigned int rate, iops, sectors = bytes >> 9;
  470. uint64_t tot = bytes * count;
  471. struct timespec ts;
  472. ts = timespec_sub(*ts2, *ts1);
  473. rate = mmc_test_rate(tot, &ts);
  474. iops = mmc_test_rate(count * 100, &ts); /* I/O ops per sec x 100 */
  475. printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
  476. "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
  477. "%u.%02u IOPS)\n",
  478. mmc_hostname(test->card->host), count, sectors, count,
  479. sectors >> 1, (sectors & 1 ? ".5" : ""),
  480. (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
  481. rate / 1000, rate / 1024, iops / 100, iops % 100);
  482. mmc_test_save_transfer_result(test, count, sectors, ts, rate, iops);
  483. }
  484. /*
  485. * Return the card size in sectors.
  486. */
  487. static unsigned int mmc_test_capacity(struct mmc_card *card)
  488. {
  489. if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
  490. return card->ext_csd.sectors;
  491. else
  492. return card->csd.capacity << (card->csd.read_blkbits - 9);
  493. }
  494. /*******************************************************************/
  495. /* Test preparation and cleanup */
  496. /*******************************************************************/
  497. /*
  498. * Fill the first couple of sectors of the card with known data
  499. * so that bad reads/writes can be detected
  500. */
  501. static int __mmc_test_prepare(struct mmc_test_card *test, int write)
  502. {
  503. int ret, i;
  504. ret = mmc_test_set_blksize(test, 512);
  505. if (ret)
  506. return ret;
  507. if (write)
  508. memset(test->buffer, 0xDF, 512);
  509. else {
  510. for (i = 0;i < 512;i++)
  511. test->buffer[i] = i;
  512. }
  513. for (i = 0;i < BUFFER_SIZE / 512;i++) {
  514. ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
  515. if (ret)
  516. return ret;
  517. }
  518. return 0;
  519. }
  520. static int mmc_test_prepare_write(struct mmc_test_card *test)
  521. {
  522. return __mmc_test_prepare(test, 1);
  523. }
  524. static int mmc_test_prepare_read(struct mmc_test_card *test)
  525. {
  526. return __mmc_test_prepare(test, 0);
  527. }
  528. static int mmc_test_cleanup(struct mmc_test_card *test)
  529. {
  530. int ret, i;
  531. ret = mmc_test_set_blksize(test, 512);
  532. if (ret)
  533. return ret;
  534. memset(test->buffer, 0, 512);
  535. for (i = 0;i < BUFFER_SIZE / 512;i++) {
  536. ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
  537. if (ret)
  538. return ret;
  539. }
  540. return 0;
  541. }
  542. /*******************************************************************/
  543. /* Test execution helpers */
  544. /*******************************************************************/
  545. /*
  546. * Modifies the mmc_request to perform the "short transfer" tests
  547. */
  548. static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
  549. struct mmc_request *mrq, int write)
  550. {
  551. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  552. if (mrq->data->blocks > 1) {
  553. mrq->cmd->opcode = write ?
  554. MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
  555. mrq->stop = NULL;
  556. } else {
  557. mrq->cmd->opcode = MMC_SEND_STATUS;
  558. mrq->cmd->arg = test->card->rca << 16;
  559. }
  560. }
  561. /*
  562. * Checks that a normal transfer didn't have any errors
  563. */
  564. static int mmc_test_check_result(struct mmc_test_card *test,
  565. struct mmc_request *mrq)
  566. {
  567. int ret;
  568. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  569. ret = 0;
  570. if (!ret && mrq->cmd->error)
  571. ret = mrq->cmd->error;
  572. if (!ret && mrq->data->error)
  573. ret = mrq->data->error;
  574. if (!ret && mrq->stop && mrq->stop->error)
  575. ret = mrq->stop->error;
  576. if (!ret && mrq->data->bytes_xfered !=
  577. mrq->data->blocks * mrq->data->blksz)
  578. ret = RESULT_FAIL;
  579. if (ret == -EINVAL)
  580. ret = RESULT_UNSUP_HOST;
  581. return ret;
  582. }
  583. /*
  584. * Checks that a "short transfer" behaved as expected
  585. */
  586. static int mmc_test_check_broken_result(struct mmc_test_card *test,
  587. struct mmc_request *mrq)
  588. {
  589. int ret;
  590. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  591. ret = 0;
  592. if (!ret && mrq->cmd->error)
  593. ret = mrq->cmd->error;
  594. if (!ret && mrq->data->error == 0)
  595. ret = RESULT_FAIL;
  596. if (!ret && mrq->data->error != -ETIMEDOUT)
  597. ret = mrq->data->error;
  598. if (!ret && mrq->stop && mrq->stop->error)
  599. ret = mrq->stop->error;
  600. if (mrq->data->blocks > 1) {
  601. if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
  602. ret = RESULT_FAIL;
  603. } else {
  604. if (!ret && mrq->data->bytes_xfered > 0)
  605. ret = RESULT_FAIL;
  606. }
  607. if (ret == -EINVAL)
  608. ret = RESULT_UNSUP_HOST;
  609. return ret;
  610. }
  611. /*
  612. * Tests a basic transfer with certain parameters
  613. */
  614. static int mmc_test_simple_transfer(struct mmc_test_card *test,
  615. struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
  616. unsigned blocks, unsigned blksz, int write)
  617. {
  618. struct mmc_request mrq;
  619. struct mmc_command cmd;
  620. struct mmc_command stop;
  621. struct mmc_data data;
  622. memset(&mrq, 0, sizeof(struct mmc_request));
  623. memset(&cmd, 0, sizeof(struct mmc_command));
  624. memset(&data, 0, sizeof(struct mmc_data));
  625. memset(&stop, 0, sizeof(struct mmc_command));
  626. mrq.cmd = &cmd;
  627. mrq.data = &data;
  628. mrq.stop = &stop;
  629. mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
  630. blocks, blksz, write);
  631. mmc_wait_for_req(test->card->host, &mrq);
  632. mmc_test_wait_busy(test);
  633. return mmc_test_check_result(test, &mrq);
  634. }
  635. /*
  636. * Tests a transfer where the card will fail completely or partly
  637. */
  638. static int mmc_test_broken_transfer(struct mmc_test_card *test,
  639. unsigned blocks, unsigned blksz, int write)
  640. {
  641. struct mmc_request mrq;
  642. struct mmc_command cmd;
  643. struct mmc_command stop;
  644. struct mmc_data data;
  645. struct scatterlist sg;
  646. memset(&mrq, 0, sizeof(struct mmc_request));
  647. memset(&cmd, 0, sizeof(struct mmc_command));
  648. memset(&data, 0, sizeof(struct mmc_data));
  649. memset(&stop, 0, sizeof(struct mmc_command));
  650. mrq.cmd = &cmd;
  651. mrq.data = &data;
  652. mrq.stop = &stop;
  653. sg_init_one(&sg, test->buffer, blocks * blksz);
  654. mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
  655. mmc_test_prepare_broken_mrq(test, &mrq, write);
  656. mmc_wait_for_req(test->card->host, &mrq);
  657. mmc_test_wait_busy(test);
  658. return mmc_test_check_broken_result(test, &mrq);
  659. }
  660. /*
  661. * Does a complete transfer test where data is also validated
  662. *
  663. * Note: mmc_test_prepare() must have been done before this call
  664. */
  665. static int mmc_test_transfer(struct mmc_test_card *test,
  666. struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
  667. unsigned blocks, unsigned blksz, int write)
  668. {
  669. int ret, i;
  670. unsigned long flags;
  671. if (write) {
  672. for (i = 0;i < blocks * blksz;i++)
  673. test->scratch[i] = i;
  674. } else {
  675. memset(test->scratch, 0, BUFFER_SIZE);
  676. }
  677. local_irq_save(flags);
  678. sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
  679. local_irq_restore(flags);
  680. ret = mmc_test_set_blksize(test, blksz);
  681. if (ret)
  682. return ret;
  683. ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
  684. blocks, blksz, write);
  685. if (ret)
  686. return ret;
  687. if (write) {
  688. int sectors;
  689. ret = mmc_test_set_blksize(test, 512);
  690. if (ret)
  691. return ret;
  692. sectors = (blocks * blksz + 511) / 512;
  693. if ((sectors * 512) == (blocks * blksz))
  694. sectors++;
  695. if ((sectors * 512) > BUFFER_SIZE)
  696. return -EINVAL;
  697. memset(test->buffer, 0, sectors * 512);
  698. for (i = 0;i < sectors;i++) {
  699. ret = mmc_test_buffer_transfer(test,
  700. test->buffer + i * 512,
  701. dev_addr + i, 512, 0);
  702. if (ret)
  703. return ret;
  704. }
  705. for (i = 0;i < blocks * blksz;i++) {
  706. if (test->buffer[i] != (u8)i)
  707. return RESULT_FAIL;
  708. }
  709. for (;i < sectors * 512;i++) {
  710. if (test->buffer[i] != 0xDF)
  711. return RESULT_FAIL;
  712. }
  713. } else {
  714. local_irq_save(flags);
  715. sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
  716. local_irq_restore(flags);
  717. for (i = 0;i < blocks * blksz;i++) {
  718. if (test->scratch[i] != (u8)i)
  719. return RESULT_FAIL;
  720. }
  721. }
  722. return 0;
  723. }
  724. /*******************************************************************/
  725. /* Tests */
  726. /*******************************************************************/
  727. struct mmc_test_case {
  728. const char *name;
  729. int (*prepare)(struct mmc_test_card *);
  730. int (*run)(struct mmc_test_card *);
  731. int (*cleanup)(struct mmc_test_card *);
  732. };
  733. static int mmc_test_basic_write(struct mmc_test_card *test)
  734. {
  735. int ret;
  736. struct scatterlist sg;
  737. ret = mmc_test_set_blksize(test, 512);
  738. if (ret)
  739. return ret;
  740. sg_init_one(&sg, test->buffer, 512);
  741. ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
  742. if (ret)
  743. return ret;
  744. return 0;
  745. }
  746. static int mmc_test_basic_read(struct mmc_test_card *test)
  747. {
  748. int ret;
  749. struct scatterlist sg;
  750. ret = mmc_test_set_blksize(test, 512);
  751. if (ret)
  752. return ret;
  753. sg_init_one(&sg, test->buffer, 512);
  754. ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
  755. if (ret)
  756. return ret;
  757. return 0;
  758. }
  759. static int mmc_test_verify_write(struct mmc_test_card *test)
  760. {
  761. int ret;
  762. struct scatterlist sg;
  763. sg_init_one(&sg, test->buffer, 512);
  764. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
  765. if (ret)
  766. return ret;
  767. return 0;
  768. }
  769. static int mmc_test_verify_read(struct mmc_test_card *test)
  770. {
  771. int ret;
  772. struct scatterlist sg;
  773. sg_init_one(&sg, test->buffer, 512);
  774. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
  775. if (ret)
  776. return ret;
  777. return 0;
  778. }
  779. static int mmc_test_multi_write(struct mmc_test_card *test)
  780. {
  781. int ret;
  782. unsigned int size;
  783. struct scatterlist sg;
  784. if (test->card->host->max_blk_count == 1)
  785. return RESULT_UNSUP_HOST;
  786. size = PAGE_SIZE * 2;
  787. size = min(size, test->card->host->max_req_size);
  788. size = min(size, test->card->host->max_seg_size);
  789. size = min(size, test->card->host->max_blk_count * 512);
  790. if (size < 1024)
  791. return RESULT_UNSUP_HOST;
  792. sg_init_one(&sg, test->buffer, size);
  793. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
  794. if (ret)
  795. return ret;
  796. return 0;
  797. }
  798. static int mmc_test_multi_read(struct mmc_test_card *test)
  799. {
  800. int ret;
  801. unsigned int size;
  802. struct scatterlist sg;
  803. if (test->card->host->max_blk_count == 1)
  804. return RESULT_UNSUP_HOST;
  805. size = PAGE_SIZE * 2;
  806. size = min(size, test->card->host->max_req_size);
  807. size = min(size, test->card->host->max_seg_size);
  808. size = min(size, test->card->host->max_blk_count * 512);
  809. if (size < 1024)
  810. return RESULT_UNSUP_HOST;
  811. sg_init_one(&sg, test->buffer, size);
  812. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
  813. if (ret)
  814. return ret;
  815. return 0;
  816. }
  817. static int mmc_test_pow2_write(struct mmc_test_card *test)
  818. {
  819. int ret, i;
  820. struct scatterlist sg;
  821. if (!test->card->csd.write_partial)
  822. return RESULT_UNSUP_CARD;
  823. for (i = 1; i < 512;i <<= 1) {
  824. sg_init_one(&sg, test->buffer, i);
  825. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
  826. if (ret)
  827. return ret;
  828. }
  829. return 0;
  830. }
  831. static int mmc_test_pow2_read(struct mmc_test_card *test)
  832. {
  833. int ret, i;
  834. struct scatterlist sg;
  835. if (!test->card->csd.read_partial)
  836. return RESULT_UNSUP_CARD;
  837. for (i = 1; i < 512;i <<= 1) {
  838. sg_init_one(&sg, test->buffer, i);
  839. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
  840. if (ret)
  841. return ret;
  842. }
  843. return 0;
  844. }
  845. static int mmc_test_weird_write(struct mmc_test_card *test)
  846. {
  847. int ret, i;
  848. struct scatterlist sg;
  849. if (!test->card->csd.write_partial)
  850. return RESULT_UNSUP_CARD;
  851. for (i = 3; i < 512;i += 7) {
  852. sg_init_one(&sg, test->buffer, i);
  853. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
  854. if (ret)
  855. return ret;
  856. }
  857. return 0;
  858. }
  859. static int mmc_test_weird_read(struct mmc_test_card *test)
  860. {
  861. int ret, i;
  862. struct scatterlist sg;
  863. if (!test->card->csd.read_partial)
  864. return RESULT_UNSUP_CARD;
  865. for (i = 3; i < 512;i += 7) {
  866. sg_init_one(&sg, test->buffer, i);
  867. ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
  868. if (ret)
  869. return ret;
  870. }
  871. return 0;
  872. }
  873. static int mmc_test_align_write(struct mmc_test_card *test)
  874. {
  875. int ret, i;
  876. struct scatterlist sg;
  877. for (i = 1;i < 4;i++) {
  878. sg_init_one(&sg, test->buffer + i, 512);
  879. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
  880. if (ret)
  881. return ret;
  882. }
  883. return 0;
  884. }
  885. static int mmc_test_align_read(struct mmc_test_card *test)
  886. {
  887. int ret, i;
  888. struct scatterlist sg;
  889. for (i = 1;i < 4;i++) {
  890. sg_init_one(&sg, test->buffer + i, 512);
  891. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
  892. if (ret)
  893. return ret;
  894. }
  895. return 0;
  896. }
  897. static int mmc_test_align_multi_write(struct mmc_test_card *test)
  898. {
  899. int ret, i;
  900. unsigned int size;
  901. struct scatterlist sg;
  902. if (test->card->host->max_blk_count == 1)
  903. return RESULT_UNSUP_HOST;
  904. size = PAGE_SIZE * 2;
  905. size = min(size, test->card->host->max_req_size);
  906. size = min(size, test->card->host->max_seg_size);
  907. size = min(size, test->card->host->max_blk_count * 512);
  908. if (size < 1024)
  909. return RESULT_UNSUP_HOST;
  910. for (i = 1;i < 4;i++) {
  911. sg_init_one(&sg, test->buffer + i, size);
  912. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
  913. if (ret)
  914. return ret;
  915. }
  916. return 0;
  917. }
  918. static int mmc_test_align_multi_read(struct mmc_test_card *test)
  919. {
  920. int ret, i;
  921. unsigned int size;
  922. struct scatterlist sg;
  923. if (test->card->host->max_blk_count == 1)
  924. return RESULT_UNSUP_HOST;
  925. size = PAGE_SIZE * 2;
  926. size = min(size, test->card->host->max_req_size);
  927. size = min(size, test->card->host->max_seg_size);
  928. size = min(size, test->card->host->max_blk_count * 512);
  929. if (size < 1024)
  930. return RESULT_UNSUP_HOST;
  931. for (i = 1;i < 4;i++) {
  932. sg_init_one(&sg, test->buffer + i, size);
  933. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
  934. if (ret)
  935. return ret;
  936. }
  937. return 0;
  938. }
  939. static int mmc_test_xfersize_write(struct mmc_test_card *test)
  940. {
  941. int ret;
  942. ret = mmc_test_set_blksize(test, 512);
  943. if (ret)
  944. return ret;
  945. ret = mmc_test_broken_transfer(test, 1, 512, 1);
  946. if (ret)
  947. return ret;
  948. return 0;
  949. }
  950. static int mmc_test_xfersize_read(struct mmc_test_card *test)
  951. {
  952. int ret;
  953. ret = mmc_test_set_blksize(test, 512);
  954. if (ret)
  955. return ret;
  956. ret = mmc_test_broken_transfer(test, 1, 512, 0);
  957. if (ret)
  958. return ret;
  959. return 0;
  960. }
  961. static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
  962. {
  963. int ret;
  964. if (test->card->host->max_blk_count == 1)
  965. return RESULT_UNSUP_HOST;
  966. ret = mmc_test_set_blksize(test, 512);
  967. if (ret)
  968. return ret;
  969. ret = mmc_test_broken_transfer(test, 2, 512, 1);
  970. if (ret)
  971. return ret;
  972. return 0;
  973. }
  974. static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
  975. {
  976. int ret;
  977. if (test->card->host->max_blk_count == 1)
  978. return RESULT_UNSUP_HOST;
  979. ret = mmc_test_set_blksize(test, 512);
  980. if (ret)
  981. return ret;
  982. ret = mmc_test_broken_transfer(test, 2, 512, 0);
  983. if (ret)
  984. return ret;
  985. return 0;
  986. }
  987. #ifdef CONFIG_HIGHMEM
  988. static int mmc_test_write_high(struct mmc_test_card *test)
  989. {
  990. int ret;
  991. struct scatterlist sg;
  992. sg_init_table(&sg, 1);
  993. sg_set_page(&sg, test->highmem, 512, 0);
  994. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
  995. if (ret)
  996. return ret;
  997. return 0;
  998. }
  999. static int mmc_test_read_high(struct mmc_test_card *test)
  1000. {
  1001. int ret;
  1002. struct scatterlist sg;
  1003. sg_init_table(&sg, 1);
  1004. sg_set_page(&sg, test->highmem, 512, 0);
  1005. ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
  1006. if (ret)
  1007. return ret;
  1008. return 0;
  1009. }
  1010. static int mmc_test_multi_write_high(struct mmc_test_card *test)
  1011. {
  1012. int ret;
  1013. unsigned int size;
  1014. struct scatterlist sg;
  1015. if (test->card->host->max_blk_count == 1)
  1016. return RESULT_UNSUP_HOST;
  1017. size = PAGE_SIZE * 2;
  1018. size = min(size, test->card->host->max_req_size);
  1019. size = min(size, test->card->host->max_seg_size);
  1020. size = min(size, test->card->host->max_blk_count * 512);
  1021. if (size < 1024)
  1022. return RESULT_UNSUP_HOST;
  1023. sg_init_table(&sg, 1);
  1024. sg_set_page(&sg, test->highmem, size, 0);
  1025. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
  1026. if (ret)
  1027. return ret;
  1028. return 0;
  1029. }
  1030. static int mmc_test_multi_read_high(struct mmc_test_card *test)
  1031. {
  1032. int ret;
  1033. unsigned int size;
  1034. struct scatterlist sg;
  1035. if (test->card->host->max_blk_count == 1)
  1036. return RESULT_UNSUP_HOST;
  1037. size = PAGE_SIZE * 2;
  1038. size = min(size, test->card->host->max_req_size);
  1039. size = min(size, test->card->host->max_seg_size);
  1040. size = min(size, test->card->host->max_blk_count * 512);
  1041. if (size < 1024)
  1042. return RESULT_UNSUP_HOST;
  1043. sg_init_table(&sg, 1);
  1044. sg_set_page(&sg, test->highmem, size, 0);
  1045. ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
  1046. if (ret)
  1047. return ret;
  1048. return 0;
  1049. }
  1050. #else
  1051. static int mmc_test_no_highmem(struct mmc_test_card *test)
  1052. {
  1053. printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
  1054. mmc_hostname(test->card->host));
  1055. return 0;
  1056. }
  1057. #endif /* CONFIG_HIGHMEM */
  1058. /*
  1059. * Map sz bytes so that it can be transferred.
  1060. */
  1061. static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
  1062. int max_scatter)
  1063. {
  1064. struct mmc_test_area *t = &test->area;
  1065. int err;
  1066. t->blocks = sz >> 9;
  1067. if (max_scatter) {
  1068. err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
  1069. t->max_segs, t->max_seg_sz,
  1070. &t->sg_len);
  1071. } else {
  1072. err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
  1073. t->max_seg_sz, &t->sg_len);
  1074. }
  1075. if (err)
  1076. printk(KERN_INFO "%s: Failed to map sg list\n",
  1077. mmc_hostname(test->card->host));
  1078. return err;
  1079. }
  1080. /*
  1081. * Transfer bytes mapped by mmc_test_area_map().
  1082. */
  1083. static int mmc_test_area_transfer(struct mmc_test_card *test,
  1084. unsigned int dev_addr, int write)
  1085. {
  1086. struct mmc_test_area *t = &test->area;
  1087. return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
  1088. t->blocks, 512, write);
  1089. }
  1090. /*
  1091. * Map and transfer bytes.
  1092. */
  1093. static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
  1094. unsigned int dev_addr, int write, int max_scatter,
  1095. int timed)
  1096. {
  1097. struct timespec ts1, ts2;
  1098. int ret;
  1099. /*
  1100. * In the case of a maximally scattered transfer, the maximum transfer
  1101. * size is further limited by using PAGE_SIZE segments.
  1102. */
  1103. if (max_scatter) {
  1104. struct mmc_test_area *t = &test->area;
  1105. unsigned long max_tfr;
  1106. if (t->max_seg_sz >= PAGE_SIZE)
  1107. max_tfr = t->max_segs * PAGE_SIZE;
  1108. else
  1109. max_tfr = t->max_segs * t->max_seg_sz;
  1110. if (sz > max_tfr)
  1111. sz = max_tfr;
  1112. }
  1113. ret = mmc_test_area_map(test, sz, max_scatter);
  1114. if (ret)
  1115. return ret;
  1116. if (timed)
  1117. getnstimeofday(&ts1);
  1118. ret = mmc_test_area_transfer(test, dev_addr, write);
  1119. if (ret)
  1120. return ret;
  1121. if (timed)
  1122. getnstimeofday(&ts2);
  1123. if (timed)
  1124. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1125. return 0;
  1126. }
  1127. /*
  1128. * Write the test area entirely.
  1129. */
  1130. static int mmc_test_area_fill(struct mmc_test_card *test)
  1131. {
  1132. return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
  1133. 1, 0, 0);
  1134. }
  1135. /*
  1136. * Erase the test area entirely.
  1137. */
  1138. static int mmc_test_area_erase(struct mmc_test_card *test)
  1139. {
  1140. struct mmc_test_area *t = &test->area;
  1141. if (!mmc_can_erase(test->card))
  1142. return 0;
  1143. return mmc_erase(test->card, t->dev_addr, test->area.max_sz >> 9,
  1144. MMC_ERASE_ARG);
  1145. }
  1146. /*
  1147. * Cleanup struct mmc_test_area.
  1148. */
  1149. static int mmc_test_area_cleanup(struct mmc_test_card *test)
  1150. {
  1151. struct mmc_test_area *t = &test->area;
  1152. kfree(t->sg);
  1153. mmc_test_free_mem(t->mem);
  1154. return 0;
  1155. }
  1156. /*
  1157. * Initialize an area for testing large transfers. The test area is set to the
  1158. * middle of the card because cards may have different charateristics at the
  1159. * front (for FAT file system optimization). Optionally, the area is erased
  1160. * (if the card supports it) which may improve write performance. Optionally,
  1161. * the area is filled with data for subsequent read tests.
  1162. */
  1163. static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
  1164. {
  1165. struct mmc_test_area *t = &test->area;
  1166. unsigned long min_sz = 64 * 1024, sz;
  1167. int ret;
  1168. ret = mmc_test_set_blksize(test, 512);
  1169. if (ret)
  1170. return ret;
  1171. /* Make the test area size about 4MiB */
  1172. sz = (unsigned long)test->card->pref_erase << 9;
  1173. t->max_sz = sz;
  1174. while (t->max_sz < 4 * 1024 * 1024)
  1175. t->max_sz += sz;
  1176. while (t->max_sz > TEST_AREA_MAX_SIZE && t->max_sz > sz)
  1177. t->max_sz -= sz;
  1178. t->max_segs = test->card->host->max_segs;
  1179. t->max_seg_sz = test->card->host->max_seg_size;
  1180. t->max_tfr = t->max_sz;
  1181. if (t->max_tfr >> 9 > test->card->host->max_blk_count)
  1182. t->max_tfr = test->card->host->max_blk_count << 9;
  1183. if (t->max_tfr > test->card->host->max_req_size)
  1184. t->max_tfr = test->card->host->max_req_size;
  1185. if (t->max_tfr / t->max_seg_sz > t->max_segs)
  1186. t->max_tfr = t->max_segs * t->max_seg_sz;
  1187. /*
  1188. * Try to allocate enough memory for a max. sized transfer. Less is OK
  1189. * because the same memory can be mapped into the scatterlist more than
  1190. * once. Also, take into account the limits imposed on scatterlist
  1191. * segments by the host driver.
  1192. */
  1193. t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
  1194. t->max_seg_sz);
  1195. if (!t->mem)
  1196. return -ENOMEM;
  1197. t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
  1198. if (!t->sg) {
  1199. ret = -ENOMEM;
  1200. goto out_free;
  1201. }
  1202. t->dev_addr = mmc_test_capacity(test->card) / 2;
  1203. t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
  1204. if (erase) {
  1205. ret = mmc_test_area_erase(test);
  1206. if (ret)
  1207. goto out_free;
  1208. }
  1209. if (fill) {
  1210. ret = mmc_test_area_fill(test);
  1211. if (ret)
  1212. goto out_free;
  1213. }
  1214. return 0;
  1215. out_free:
  1216. mmc_test_area_cleanup(test);
  1217. return ret;
  1218. }
  1219. /*
  1220. * Prepare for large transfers. Do not erase the test area.
  1221. */
  1222. static int mmc_test_area_prepare(struct mmc_test_card *test)
  1223. {
  1224. return mmc_test_area_init(test, 0, 0);
  1225. }
  1226. /*
  1227. * Prepare for large transfers. Do erase the test area.
  1228. */
  1229. static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
  1230. {
  1231. return mmc_test_area_init(test, 1, 0);
  1232. }
  1233. /*
  1234. * Prepare for large transfers. Erase and fill the test area.
  1235. */
  1236. static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
  1237. {
  1238. return mmc_test_area_init(test, 1, 1);
  1239. }
  1240. /*
  1241. * Test best-case performance. Best-case performance is expected from
  1242. * a single large transfer.
  1243. *
  1244. * An additional option (max_scatter) allows the measurement of the same
  1245. * transfer but with no contiguous pages in the scatter list. This tests
  1246. * the efficiency of DMA to handle scattered pages.
  1247. */
  1248. static int mmc_test_best_performance(struct mmc_test_card *test, int write,
  1249. int max_scatter)
  1250. {
  1251. return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
  1252. write, max_scatter, 1);
  1253. }
  1254. /*
  1255. * Best-case read performance.
  1256. */
  1257. static int mmc_test_best_read_performance(struct mmc_test_card *test)
  1258. {
  1259. return mmc_test_best_performance(test, 0, 0);
  1260. }
  1261. /*
  1262. * Best-case write performance.
  1263. */
  1264. static int mmc_test_best_write_performance(struct mmc_test_card *test)
  1265. {
  1266. return mmc_test_best_performance(test, 1, 0);
  1267. }
  1268. /*
  1269. * Best-case read performance into scattered pages.
  1270. */
  1271. static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
  1272. {
  1273. return mmc_test_best_performance(test, 0, 1);
  1274. }
  1275. /*
  1276. * Best-case write performance from scattered pages.
  1277. */
  1278. static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
  1279. {
  1280. return mmc_test_best_performance(test, 1, 1);
  1281. }
  1282. /*
  1283. * Single read performance by transfer size.
  1284. */
  1285. static int mmc_test_profile_read_perf(struct mmc_test_card *test)
  1286. {
  1287. unsigned long sz;
  1288. unsigned int dev_addr;
  1289. int ret;
  1290. for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
  1291. dev_addr = test->area.dev_addr + (sz >> 9);
  1292. ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
  1293. if (ret)
  1294. return ret;
  1295. }
  1296. sz = test->area.max_tfr;
  1297. dev_addr = test->area.dev_addr;
  1298. return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
  1299. }
  1300. /*
  1301. * Single write performance by transfer size.
  1302. */
  1303. static int mmc_test_profile_write_perf(struct mmc_test_card *test)
  1304. {
  1305. unsigned long sz;
  1306. unsigned int dev_addr;
  1307. int ret;
  1308. ret = mmc_test_area_erase(test);
  1309. if (ret)
  1310. return ret;
  1311. for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
  1312. dev_addr = test->area.dev_addr + (sz >> 9);
  1313. ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
  1314. if (ret)
  1315. return ret;
  1316. }
  1317. ret = mmc_test_area_erase(test);
  1318. if (ret)
  1319. return ret;
  1320. sz = test->area.max_tfr;
  1321. dev_addr = test->area.dev_addr;
  1322. return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
  1323. }
  1324. /*
  1325. * Single trim performance by transfer size.
  1326. */
  1327. static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
  1328. {
  1329. unsigned long sz;
  1330. unsigned int dev_addr;
  1331. struct timespec ts1, ts2;
  1332. int ret;
  1333. if (!mmc_can_trim(test->card))
  1334. return RESULT_UNSUP_CARD;
  1335. if (!mmc_can_erase(test->card))
  1336. return RESULT_UNSUP_HOST;
  1337. for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
  1338. dev_addr = test->area.dev_addr + (sz >> 9);
  1339. getnstimeofday(&ts1);
  1340. ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
  1341. if (ret)
  1342. return ret;
  1343. getnstimeofday(&ts2);
  1344. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1345. }
  1346. dev_addr = test->area.dev_addr;
  1347. getnstimeofday(&ts1);
  1348. ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
  1349. if (ret)
  1350. return ret;
  1351. getnstimeofday(&ts2);
  1352. mmc_test_print_rate(test, sz, &ts1, &ts2);
  1353. return 0;
  1354. }
  1355. static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
  1356. {
  1357. unsigned int dev_addr, i, cnt;
  1358. struct timespec ts1, ts2;
  1359. int ret;
  1360. cnt = test->area.max_sz / sz;
  1361. dev_addr = test->area.dev_addr;
  1362. getnstimeofday(&ts1);
  1363. for (i = 0; i < cnt; i++) {
  1364. ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
  1365. if (ret)
  1366. return ret;
  1367. dev_addr += (sz >> 9);
  1368. }
  1369. getnstimeofday(&ts2);
  1370. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1371. return 0;
  1372. }
  1373. /*
  1374. * Consecutive read performance by transfer size.
  1375. */
  1376. static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
  1377. {
  1378. unsigned long sz;
  1379. int ret;
  1380. for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
  1381. ret = mmc_test_seq_read_perf(test, sz);
  1382. if (ret)
  1383. return ret;
  1384. }
  1385. sz = test->area.max_tfr;
  1386. return mmc_test_seq_read_perf(test, sz);
  1387. }
  1388. static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
  1389. {
  1390. unsigned int dev_addr, i, cnt;
  1391. struct timespec ts1, ts2;
  1392. int ret;
  1393. ret = mmc_test_area_erase(test);
  1394. if (ret)
  1395. return ret;
  1396. cnt = test->area.max_sz / sz;
  1397. dev_addr = test->area.dev_addr;
  1398. getnstimeofday(&ts1);
  1399. for (i = 0; i < cnt; i++) {
  1400. ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
  1401. if (ret)
  1402. return ret;
  1403. dev_addr += (sz >> 9);
  1404. }
  1405. getnstimeofday(&ts2);
  1406. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1407. return 0;
  1408. }
  1409. /*
  1410. * Consecutive write performance by transfer size.
  1411. */
  1412. static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
  1413. {
  1414. unsigned long sz;
  1415. int ret;
  1416. for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
  1417. ret = mmc_test_seq_write_perf(test, sz);
  1418. if (ret)
  1419. return ret;
  1420. }
  1421. sz = test->area.max_tfr;
  1422. return mmc_test_seq_write_perf(test, sz);
  1423. }
  1424. /*
  1425. * Consecutive trim performance by transfer size.
  1426. */
  1427. static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
  1428. {
  1429. unsigned long sz;
  1430. unsigned int dev_addr, i, cnt;
  1431. struct timespec ts1, ts2;
  1432. int ret;
  1433. if (!mmc_can_trim(test->card))
  1434. return RESULT_UNSUP_CARD;
  1435. if (!mmc_can_erase(test->card))
  1436. return RESULT_UNSUP_HOST;
  1437. for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
  1438. ret = mmc_test_area_erase(test);
  1439. if (ret)
  1440. return ret;
  1441. ret = mmc_test_area_fill(test);
  1442. if (ret)
  1443. return ret;
  1444. cnt = test->area.max_sz / sz;
  1445. dev_addr = test->area.dev_addr;
  1446. getnstimeofday(&ts1);
  1447. for (i = 0; i < cnt; i++) {
  1448. ret = mmc_erase(test->card, dev_addr, sz >> 9,
  1449. MMC_TRIM_ARG);
  1450. if (ret)
  1451. return ret;
  1452. dev_addr += (sz >> 9);
  1453. }
  1454. getnstimeofday(&ts2);
  1455. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1456. }
  1457. return 0;
  1458. }
  1459. static unsigned int rnd_next = 1;
  1460. static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt)
  1461. {
  1462. uint64_t r;
  1463. rnd_next = rnd_next * 1103515245 + 12345;
  1464. r = (rnd_next >> 16) & 0x7fff;
  1465. return (r * rnd_cnt) >> 15;
  1466. }
  1467. static int mmc_test_rnd_perf(struct mmc_test_card *test, int write, int print,
  1468. unsigned long sz)
  1469. {
  1470. unsigned int dev_addr, cnt, rnd_addr, range1, range2, last_ea = 0, ea;
  1471. unsigned int ssz;
  1472. struct timespec ts1, ts2, ts;
  1473. int ret;
  1474. ssz = sz >> 9;
  1475. rnd_addr = mmc_test_capacity(test->card) / 4;
  1476. range1 = rnd_addr / test->card->pref_erase;
  1477. range2 = range1 / ssz;
  1478. getnstimeofday(&ts1);
  1479. for (cnt = 0; cnt < UINT_MAX; cnt++) {
  1480. getnstimeofday(&ts2);
  1481. ts = timespec_sub(ts2, ts1);
  1482. if (ts.tv_sec >= 10)
  1483. break;
  1484. ea = mmc_test_rnd_num(range1);
  1485. if (ea == last_ea)
  1486. ea -= 1;
  1487. last_ea = ea;
  1488. dev_addr = rnd_addr + test->card->pref_erase * ea +
  1489. ssz * mmc_test_rnd_num(range2);
  1490. ret = mmc_test_area_io(test, sz, dev_addr, write, 0, 0);
  1491. if (ret)
  1492. return ret;
  1493. }
  1494. if (print)
  1495. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1496. return 0;
  1497. }
  1498. static int mmc_test_random_perf(struct mmc_test_card *test, int write)
  1499. {
  1500. unsigned int next;
  1501. unsigned long sz;
  1502. int ret;
  1503. for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
  1504. /*
  1505. * When writing, try to get more consistent results by running
  1506. * the test twice with exactly the same I/O but outputting the
  1507. * results only for the 2nd run.
  1508. */
  1509. if (write) {
  1510. next = rnd_next;
  1511. ret = mmc_test_rnd_perf(test, write, 0, sz);
  1512. if (ret)
  1513. return ret;
  1514. rnd_next = next;
  1515. }
  1516. ret = mmc_test_rnd_perf(test, write, 1, sz);
  1517. if (ret)
  1518. return ret;
  1519. }
  1520. sz = test->area.max_tfr;
  1521. if (write) {
  1522. next = rnd_next;
  1523. ret = mmc_test_rnd_perf(test, write, 0, sz);
  1524. if (ret)
  1525. return ret;
  1526. rnd_next = next;
  1527. }
  1528. return mmc_test_rnd_perf(test, write, 1, sz);
  1529. }
  1530. /*
  1531. * Random read performance by transfer size.
  1532. */
  1533. static int mmc_test_random_read_perf(struct mmc_test_card *test)
  1534. {
  1535. return mmc_test_random_perf(test, 0);
  1536. }
  1537. /*
  1538. * Random write performance by transfer size.
  1539. */
  1540. static int mmc_test_random_write_perf(struct mmc_test_card *test)
  1541. {
  1542. return mmc_test_random_perf(test, 1);
  1543. }
  1544. static int mmc_test_seq_perf(struct mmc_test_card *test, int write,
  1545. unsigned int tot_sz, int max_scatter)
  1546. {
  1547. unsigned int dev_addr, i, cnt, sz, ssz;
  1548. struct timespec ts1, ts2;
  1549. int ret;
  1550. sz = test->area.max_tfr;
  1551. /*
  1552. * In the case of a maximally scattered transfer, the maximum transfer
  1553. * size is further limited by using PAGE_SIZE segments.
  1554. */
  1555. if (max_scatter) {
  1556. struct mmc_test_area *t = &test->area;
  1557. unsigned long max_tfr;
  1558. if (t->max_seg_sz >= PAGE_SIZE)
  1559. max_tfr = t->max_segs * PAGE_SIZE;
  1560. else
  1561. max_tfr = t->max_segs * t->max_seg_sz;
  1562. if (sz > max_tfr)
  1563. sz = max_tfr;
  1564. }
  1565. ssz = sz >> 9;
  1566. dev_addr = mmc_test_capacity(test->card) / 4;
  1567. if (tot_sz > dev_addr << 9)
  1568. tot_sz = dev_addr << 9;
  1569. cnt = tot_sz / sz;
  1570. dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
  1571. getnstimeofday(&ts1);
  1572. for (i = 0; i < cnt; i++) {
  1573. ret = mmc_test_area_io(test, sz, dev_addr, write,
  1574. max_scatter, 0);
  1575. if (ret)
  1576. return ret;
  1577. dev_addr += ssz;
  1578. }
  1579. getnstimeofday(&ts2);
  1580. mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
  1581. return 0;
  1582. }
  1583. static int mmc_test_large_seq_perf(struct mmc_test_card *test, int write)
  1584. {
  1585. int ret, i;
  1586. for (i = 0; i < 10; i++) {
  1587. ret = mmc_test_seq_perf(test, write, 10 * 1024 * 1024, 1);
  1588. if (ret)
  1589. return ret;
  1590. }
  1591. for (i = 0; i < 5; i++) {
  1592. ret = mmc_test_seq_perf(test, write, 100 * 1024 * 1024, 1);
  1593. if (ret)
  1594. return ret;
  1595. }
  1596. for (i = 0; i < 3; i++) {
  1597. ret = mmc_test_seq_perf(test, write, 1000 * 1024 * 1024, 1);
  1598. if (ret)
  1599. return ret;
  1600. }
  1601. return ret;
  1602. }
  1603. /*
  1604. * Large sequential read performance.
  1605. */
  1606. static int mmc_test_large_seq_read_perf(struct mmc_test_card *test)
  1607. {
  1608. return mmc_test_large_seq_perf(test, 0);
  1609. }
  1610. /*
  1611. * Large sequential write performance.
  1612. */
  1613. static int mmc_test_large_seq_write_perf(struct mmc_test_card *test)
  1614. {
  1615. return mmc_test_large_seq_perf(test, 1);
  1616. }
  1617. static const struct mmc_test_case mmc_test_cases[] = {
  1618. {
  1619. .name = "Basic write (no data verification)",
  1620. .run = mmc_test_basic_write,
  1621. },
  1622. {
  1623. .name = "Basic read (no data verification)",
  1624. .run = mmc_test_basic_read,
  1625. },
  1626. {
  1627. .name = "Basic write (with data verification)",
  1628. .prepare = mmc_test_prepare_write,
  1629. .run = mmc_test_verify_write,
  1630. .cleanup = mmc_test_cleanup,
  1631. },
  1632. {
  1633. .name = "Basic read (with data verification)",
  1634. .prepare = mmc_test_prepare_read,
  1635. .run = mmc_test_verify_read,
  1636. .cleanup = mmc_test_cleanup,
  1637. },
  1638. {
  1639. .name = "Multi-block write",
  1640. .prepare = mmc_test_prepare_write,
  1641. .run = mmc_test_multi_write,
  1642. .cleanup = mmc_test_cleanup,
  1643. },
  1644. {
  1645. .name = "Multi-block read",
  1646. .prepare = mmc_test_prepare_read,
  1647. .run = mmc_test_multi_read,
  1648. .cleanup = mmc_test_cleanup,
  1649. },
  1650. {
  1651. .name = "Power of two block writes",
  1652. .prepare = mmc_test_prepare_write,
  1653. .run = mmc_test_pow2_write,
  1654. .cleanup = mmc_test_cleanup,
  1655. },
  1656. {
  1657. .name = "Power of two block reads",
  1658. .prepare = mmc_test_prepare_read,
  1659. .run = mmc_test_pow2_read,
  1660. .cleanup = mmc_test_cleanup,
  1661. },
  1662. {
  1663. .name = "Weird sized block writes",
  1664. .prepare = mmc_test_prepare_write,
  1665. .run = mmc_test_weird_write,
  1666. .cleanup = mmc_test_cleanup,
  1667. },
  1668. {
  1669. .name = "Weird sized block reads",
  1670. .prepare = mmc_test_prepare_read,
  1671. .run = mmc_test_weird_read,
  1672. .cleanup = mmc_test_cleanup,
  1673. },
  1674. {
  1675. .name = "Badly aligned write",
  1676. .prepare = mmc_test_prepare_write,
  1677. .run = mmc_test_align_write,
  1678. .cleanup = mmc_test_cleanup,
  1679. },
  1680. {
  1681. .name = "Badly aligned read",
  1682. .prepare = mmc_test_prepare_read,
  1683. .run = mmc_test_align_read,
  1684. .cleanup = mmc_test_cleanup,
  1685. },
  1686. {
  1687. .name = "Badly aligned multi-block write",
  1688. .prepare = mmc_test_prepare_write,
  1689. .run = mmc_test_align_multi_write,
  1690. .cleanup = mmc_test_cleanup,
  1691. },
  1692. {
  1693. .name = "Badly aligned multi-block read",
  1694. .prepare = mmc_test_prepare_read,
  1695. .run = mmc_test_align_multi_read,
  1696. .cleanup = mmc_test_cleanup,
  1697. },
  1698. {
  1699. .name = "Correct xfer_size at write (start failure)",
  1700. .run = mmc_test_xfersize_write,
  1701. },
  1702. {
  1703. .name = "Correct xfer_size at read (start failure)",
  1704. .run = mmc_test_xfersize_read,
  1705. },
  1706. {
  1707. .name = "Correct xfer_size at write (midway failure)",
  1708. .run = mmc_test_multi_xfersize_write,
  1709. },
  1710. {
  1711. .name = "Correct xfer_size at read (midway failure)",
  1712. .run = mmc_test_multi_xfersize_read,
  1713. },
  1714. #ifdef CONFIG_HIGHMEM
  1715. {
  1716. .name = "Highmem write",
  1717. .prepare = mmc_test_prepare_write,
  1718. .run = mmc_test_write_high,
  1719. .cleanup = mmc_test_cleanup,
  1720. },
  1721. {
  1722. .name = "Highmem read",
  1723. .prepare = mmc_test_prepare_read,
  1724. .run = mmc_test_read_high,
  1725. .cleanup = mmc_test_cleanup,
  1726. },
  1727. {
  1728. .name = "Multi-block highmem write",
  1729. .prepare = mmc_test_prepare_write,
  1730. .run = mmc_test_multi_write_high,
  1731. .cleanup = mmc_test_cleanup,
  1732. },
  1733. {
  1734. .name = "Multi-block highmem read",
  1735. .prepare = mmc_test_prepare_read,
  1736. .run = mmc_test_multi_read_high,
  1737. .cleanup = mmc_test_cleanup,
  1738. },
  1739. #else
  1740. {
  1741. .name = "Highmem write",
  1742. .run = mmc_test_no_highmem,
  1743. },
  1744. {
  1745. .name = "Highmem read",
  1746. .run = mmc_test_no_highmem,
  1747. },
  1748. {
  1749. .name = "Multi-block highmem write",
  1750. .run = mmc_test_no_highmem,
  1751. },
  1752. {
  1753. .name = "Multi-block highmem read",
  1754. .run = mmc_test_no_highmem,
  1755. },
  1756. #endif /* CONFIG_HIGHMEM */
  1757. {
  1758. .name = "Best-case read performance",
  1759. .prepare = mmc_test_area_prepare_fill,
  1760. .run = mmc_test_best_read_performance,
  1761. .cleanup = mmc_test_area_cleanup,
  1762. },
  1763. {
  1764. .name = "Best-case write performance",
  1765. .prepare = mmc_test_area_prepare_erase,
  1766. .run = mmc_test_best_write_performance,
  1767. .cleanup = mmc_test_area_cleanup,
  1768. },
  1769. {
  1770. .name = "Best-case read performance into scattered pages",
  1771. .prepare = mmc_test_area_prepare_fill,
  1772. .run = mmc_test_best_read_perf_max_scatter,
  1773. .cleanup = mmc_test_area_cleanup,
  1774. },
  1775. {
  1776. .name = "Best-case write performance from scattered pages",
  1777. .prepare = mmc_test_area_prepare_erase,
  1778. .run = mmc_test_best_write_perf_max_scatter,
  1779. .cleanup = mmc_test_area_cleanup,
  1780. },
  1781. {
  1782. .name = "Single read performance by transfer size",
  1783. .prepare = mmc_test_area_prepare_fill,
  1784. .run = mmc_test_profile_read_perf,
  1785. .cleanup = mmc_test_area_cleanup,
  1786. },
  1787. {
  1788. .name = "Single write performance by transfer size",
  1789. .prepare = mmc_test_area_prepare,
  1790. .run = mmc_test_profile_write_perf,
  1791. .cleanup = mmc_test_area_cleanup,
  1792. },
  1793. {
  1794. .name = "Single trim performance by transfer size",
  1795. .prepare = mmc_test_area_prepare_fill,
  1796. .run = mmc_test_profile_trim_perf,
  1797. .cleanup = mmc_test_area_cleanup,
  1798. },
  1799. {
  1800. .name = "Consecutive read performance by transfer size",
  1801. .prepare = mmc_test_area_prepare_fill,
  1802. .run = mmc_test_profile_seq_read_perf,
  1803. .cleanup = mmc_test_area_cleanup,
  1804. },
  1805. {
  1806. .name = "Consecutive write performance by transfer size",
  1807. .prepare = mmc_test_area_prepare,
  1808. .run = mmc_test_profile_seq_write_perf,
  1809. .cleanup = mmc_test_area_cleanup,
  1810. },
  1811. {
  1812. .name = "Consecutive trim performance by transfer size",
  1813. .prepare = mmc_test_area_prepare,
  1814. .run = mmc_test_profile_seq_trim_perf,
  1815. .cleanup = mmc_test_area_cleanup,
  1816. },
  1817. {
  1818. .name = "Random read performance by transfer size",
  1819. .prepare = mmc_test_area_prepare,
  1820. .run = mmc_test_random_read_perf,
  1821. .cleanup = mmc_test_area_cleanup,
  1822. },
  1823. {
  1824. .name = "Random write performance by transfer size",
  1825. .prepare = mmc_test_area_prepare,
  1826. .run = mmc_test_random_write_perf,
  1827. .cleanup = mmc_test_area_cleanup,
  1828. },
  1829. {
  1830. .name = "Large sequential read into scattered pages",
  1831. .prepare = mmc_test_area_prepare,
  1832. .run = mmc_test_large_seq_read_perf,
  1833. .cleanup = mmc_test_area_cleanup,
  1834. },
  1835. {
  1836. .name = "Large sequential write from scattered pages",
  1837. .prepare = mmc_test_area_prepare,
  1838. .run = mmc_test_large_seq_write_perf,
  1839. .cleanup = mmc_test_area_cleanup,
  1840. },
  1841. };
  1842. static DEFINE_MUTEX(mmc_test_lock);
  1843. static LIST_HEAD(mmc_test_result);
  1844. static void mmc_test_run(struct mmc_test_card *test, int testcase)
  1845. {
  1846. int i, ret;
  1847. printk(KERN_INFO "%s: Starting tests of card %s...\n",
  1848. mmc_hostname(test->card->host), mmc_card_id(test->card));
  1849. mmc_claim_host(test->card->host);
  1850. for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
  1851. struct mmc_test_general_result *gr;
  1852. if (testcase && ((i + 1) != testcase))
  1853. continue;
  1854. printk(KERN_INFO "%s: Test case %d. %s...\n",
  1855. mmc_hostname(test->card->host), i + 1,
  1856. mmc_test_cases[i].name);
  1857. if (mmc_test_cases[i].prepare) {
  1858. ret = mmc_test_cases[i].prepare(test);
  1859. if (ret) {
  1860. printk(KERN_INFO "%s: Result: Prepare "
  1861. "stage failed! (%d)\n",
  1862. mmc_hostname(test->card->host),
  1863. ret);
  1864. continue;
  1865. }
  1866. }
  1867. gr = kzalloc(sizeof(struct mmc_test_general_result),
  1868. GFP_KERNEL);
  1869. if (gr) {
  1870. INIT_LIST_HEAD(&gr->tr_lst);
  1871. /* Assign data what we know already */
  1872. gr->card = test->card;
  1873. gr->testcase = i;
  1874. /* Append container to global one */
  1875. list_add_tail(&gr->link, &mmc_test_result);
  1876. /*
  1877. * Save the pointer to created container in our private
  1878. * structure.
  1879. */
  1880. test->gr = gr;
  1881. }
  1882. ret = mmc_test_cases[i].run(test);
  1883. switch (ret) {
  1884. case RESULT_OK:
  1885. printk(KERN_INFO "%s: Result: OK\n",
  1886. mmc_hostname(test->card->host));
  1887. break;
  1888. case RESULT_FAIL:
  1889. printk(KERN_INFO "%s: Result: FAILED\n",
  1890. mmc_hostname(test->card->host));
  1891. break;
  1892. case RESULT_UNSUP_HOST:
  1893. printk(KERN_INFO "%s: Result: UNSUPPORTED "
  1894. "(by host)\n",
  1895. mmc_hostname(test->card->host));
  1896. break;
  1897. case RESULT_UNSUP_CARD:
  1898. printk(KERN_INFO "%s: Result: UNSUPPORTED "
  1899. "(by card)\n",
  1900. mmc_hostname(test->card->host));
  1901. break;
  1902. default:
  1903. printk(KERN_INFO "%s: Result: ERROR (%d)\n",
  1904. mmc_hostname(test->card->host), ret);
  1905. }
  1906. /* Save the result */
  1907. if (gr)
  1908. gr->result = ret;
  1909. if (mmc_test_cases[i].cleanup) {
  1910. ret = mmc_test_cases[i].cleanup(test);
  1911. if (ret) {
  1912. printk(KERN_INFO "%s: Warning: Cleanup "
  1913. "stage failed! (%d)\n",
  1914. mmc_hostname(test->card->host),
  1915. ret);
  1916. }
  1917. }
  1918. }
  1919. mmc_release_host(test->card->host);
  1920. printk(KERN_INFO "%s: Tests completed.\n",
  1921. mmc_hostname(test->card->host));
  1922. }
  1923. static void mmc_test_free_result(struct mmc_card *card)
  1924. {
  1925. struct mmc_test_general_result *gr, *grs;
  1926. mutex_lock(&mmc_test_lock);
  1927. list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
  1928. struct mmc_test_transfer_result *tr, *trs;
  1929. if (card && gr->card != card)
  1930. continue;
  1931. list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
  1932. list_del(&tr->link);
  1933. kfree(tr);
  1934. }
  1935. list_del(&gr->link);
  1936. kfree(gr);
  1937. }
  1938. mutex_unlock(&mmc_test_lock);
  1939. }
  1940. static LIST_HEAD(mmc_test_file_test);
  1941. static int mtf_test_show(struct seq_file *sf, void *data)
  1942. {
  1943. struct mmc_card *card = (struct mmc_card *)sf->private;
  1944. struct mmc_test_general_result *gr;
  1945. mutex_lock(&mmc_test_lock);
  1946. list_for_each_entry(gr, &mmc_test_result, link) {
  1947. struct mmc_test_transfer_result *tr;
  1948. if (gr->card != card)
  1949. continue;
  1950. seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
  1951. list_for_each_entry(tr, &gr->tr_lst, link) {
  1952. seq_printf(sf, "%u %d %lu.%09lu %u %u.%02u\n",
  1953. tr->count, tr->sectors,
  1954. (unsigned long)tr->ts.tv_sec,
  1955. (unsigned long)tr->ts.tv_nsec,
  1956. tr->rate, tr->iops / 100, tr->iops % 100);
  1957. }
  1958. }
  1959. mutex_unlock(&mmc_test_lock);
  1960. return 0;
  1961. }
  1962. static int mtf_test_open(struct inode *inode, struct file *file)
  1963. {
  1964. return single_open(file, mtf_test_show, inode->i_private);
  1965. }
  1966. static ssize_t mtf_test_write(struct file *file, const char __user *buf,
  1967. size_t count, loff_t *pos)
  1968. {
  1969. struct seq_file *sf = (struct seq_file *)file->private_data;
  1970. struct mmc_card *card = (struct mmc_card *)sf->private;
  1971. struct mmc_test_card *test;
  1972. char lbuf[12];
  1973. long testcase;
  1974. if (count >= sizeof(lbuf))
  1975. return -EINVAL;
  1976. if (copy_from_user(lbuf, buf, count))
  1977. return -EFAULT;
  1978. lbuf[count] = '\0';
  1979. if (strict_strtol(lbuf, 10, &testcase))
  1980. return -EINVAL;
  1981. test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
  1982. if (!test)
  1983. return -ENOMEM;
  1984. /*
  1985. * Remove all test cases associated with given card. Thus we have only
  1986. * actual data of the last run.
  1987. */
  1988. mmc_test_free_result(card);
  1989. test->card = card;
  1990. test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
  1991. #ifdef CONFIG_HIGHMEM
  1992. test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
  1993. #endif
  1994. #ifdef CONFIG_HIGHMEM
  1995. if (test->buffer && test->highmem) {
  1996. #else
  1997. if (test->buffer) {
  1998. #endif
  1999. mutex_lock(&mmc_test_lock);
  2000. mmc_test_run(test, testcase);
  2001. mutex_unlock(&mmc_test_lock);
  2002. }
  2003. #ifdef CONFIG_HIGHMEM
  2004. __free_pages(test->highmem, BUFFER_ORDER);
  2005. #endif
  2006. kfree(test->buffer);
  2007. kfree(test);
  2008. return count;
  2009. }
  2010. static const struct file_operations mmc_test_fops_test = {
  2011. .open = mtf_test_open,
  2012. .read = seq_read,
  2013. .write = mtf_test_write,
  2014. .llseek = seq_lseek,
  2015. .release = single_release,
  2016. };
  2017. static void mmc_test_free_file_test(struct mmc_card *card)
  2018. {
  2019. struct mmc_test_dbgfs_file *df, *dfs;
  2020. mutex_lock(&mmc_test_lock);
  2021. list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
  2022. if (card && df->card != card)
  2023. continue;
  2024. debugfs_remove(df->file);
  2025. list_del(&df->link);
  2026. kfree(df);
  2027. }
  2028. mutex_unlock(&mmc_test_lock);
  2029. }
  2030. static int mmc_test_register_file_test(struct mmc_card *card)
  2031. {
  2032. struct dentry *file = NULL;
  2033. struct mmc_test_dbgfs_file *df;
  2034. int ret = 0;
  2035. mutex_lock(&mmc_test_lock);
  2036. if (card->debugfs_root)
  2037. file = debugfs_create_file("test", S_IWUSR | S_IRUGO,
  2038. card->debugfs_root, card, &mmc_test_fops_test);
  2039. if (IS_ERR_OR_NULL(file)) {
  2040. dev_err(&card->dev,
  2041. "Can't create file. Perhaps debugfs is disabled.\n");
  2042. ret = -ENODEV;
  2043. goto err;
  2044. }
  2045. df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
  2046. if (!df) {
  2047. debugfs_remove(file);
  2048. dev_err(&card->dev,
  2049. "Can't allocate memory for internal usage.\n");
  2050. ret = -ENOMEM;
  2051. goto err;
  2052. }
  2053. df->card = card;
  2054. df->file = file;
  2055. list_add(&df->link, &mmc_test_file_test);
  2056. err:
  2057. mutex_unlock(&mmc_test_lock);
  2058. return ret;
  2059. }
  2060. static int mmc_test_probe(struct mmc_card *card)
  2061. {
  2062. int ret;
  2063. if (!mmc_card_mmc(card) && !mmc_card_sd(card))
  2064. return -ENODEV;
  2065. ret = mmc_test_register_file_test(card);
  2066. if (ret)
  2067. return ret;
  2068. dev_info(&card->dev, "Card claimed for testing.\n");
  2069. return 0;
  2070. }
  2071. static void mmc_test_remove(struct mmc_card *card)
  2072. {
  2073. mmc_test_free_result(card);
  2074. mmc_test_free_file_test(card);
  2075. }
  2076. static struct mmc_driver mmc_driver = {
  2077. .drv = {
  2078. .name = "mmc_test",
  2079. },
  2080. .probe = mmc_test_probe,
  2081. .remove = mmc_test_remove,
  2082. };
  2083. static int __init mmc_test_init(void)
  2084. {
  2085. return mmc_register_driver(&mmc_driver);
  2086. }
  2087. static void __exit mmc_test_exit(void)
  2088. {
  2089. /* Clear stalled data if card is still plugged */
  2090. mmc_test_free_result(NULL);
  2091. mmc_test_free_file_test(NULL);
  2092. mmc_unregister_driver(&mmc_driver);
  2093. }
  2094. module_init(mmc_test_init);
  2095. module_exit(mmc_test_exit);
  2096. MODULE_LICENSE("GPL");
  2097. MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
  2098. MODULE_AUTHOR("Pierre Ossman");