mmc_test.c 67 KB

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