mmc_test.c 50 KB

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