mmc_test.c 67 KB

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