mmc_test.c 50 KB

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