mmc_test.c 74 KB

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