mmc_test.c 67 KB

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