mmc_test.c 50 KB

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