mmc_test.c 56 KB

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