mmc_test.c 73 KB

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