mmc_test.c 67 KB

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