123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337 |
- /*
- * linux/drivers/mmc/card/mmc_test.c
- *
- * Copyright 2007-2008 Pierre Ossman
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or (at
- * your option) any later version.
- */
- #include <linux/mmc/core.h>
- #include <linux/mmc/card.h>
- #include <linux/mmc/host.h>
- #include <linux/mmc/mmc.h>
- #include <linux/slab.h>
- #include <linux/scatterlist.h>
- #include <linux/swap.h> /* For nr_free_buffer_pages() */
- #include <linux/list.h>
- #include <linux/debugfs.h>
- #include <linux/uaccess.h>
- #include <linux/seq_file.h>
- #define RESULT_OK 0
- #define RESULT_FAIL 1
- #define RESULT_UNSUP_HOST 2
- #define RESULT_UNSUP_CARD 3
- #define BUFFER_ORDER 2
- #define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
- /*
- * Limit the test area size to the maximum MMC HC erase group size. Note that
- * the maximum SD allocation unit size is just 4MiB.
- */
- #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
- /**
- * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
- * @page: first page in the allocation
- * @order: order of the number of pages allocated
- */
- struct mmc_test_pages {
- struct page *page;
- unsigned int order;
- };
- /**
- * struct mmc_test_mem - allocated memory.
- * @arr: array of allocations
- * @cnt: number of allocations
- */
- struct mmc_test_mem {
- struct mmc_test_pages *arr;
- unsigned int cnt;
- };
- /**
- * struct mmc_test_area - information for performance tests.
- * @max_sz: test area size (in bytes)
- * @dev_addr: address on card at which to do performance tests
- * @max_tfr: maximum transfer size allowed by driver (in bytes)
- * @max_segs: maximum segments allowed by driver in scatterlist @sg
- * @max_seg_sz: maximum segment size allowed by driver
- * @blocks: number of (512 byte) blocks currently mapped by @sg
- * @sg_len: length of currently mapped scatterlist @sg
- * @mem: allocated memory
- * @sg: scatterlist
- */
- struct mmc_test_area {
- unsigned long max_sz;
- unsigned int dev_addr;
- unsigned int max_tfr;
- unsigned int max_segs;
- unsigned int max_seg_sz;
- unsigned int blocks;
- unsigned int sg_len;
- struct mmc_test_mem *mem;
- struct scatterlist *sg;
- };
- /**
- * struct mmc_test_transfer_result - transfer results for performance tests.
- * @link: double-linked list
- * @count: amount of group of sectors to check
- * @sectors: amount of sectors to check in one group
- * @ts: time values of transfer
- * @rate: calculated transfer rate
- */
- struct mmc_test_transfer_result {
- struct list_head link;
- unsigned int count;
- unsigned int sectors;
- struct timespec ts;
- unsigned int rate;
- };
- /**
- * struct mmc_test_general_result - results for tests.
- * @link: double-linked list
- * @card: card under test
- * @testcase: number of test case
- * @result: result of test run
- * @tr_lst: transfer measurements if any as mmc_test_transfer_result
- */
- struct mmc_test_general_result {
- struct list_head link;
- struct mmc_card *card;
- int testcase;
- int result;
- struct list_head tr_lst;
- };
- /**
- * struct mmc_test_dbgfs_file - debugfs related file.
- * @link: double-linked list
- * @card: card under test
- * @file: file created under debugfs
- */
- struct mmc_test_dbgfs_file {
- struct list_head link;
- struct mmc_card *card;
- struct dentry *file;
- };
- /**
- * struct mmc_test_card - test information.
- * @card: card under test
- * @scratch: transfer buffer
- * @buffer: transfer buffer
- * @highmem: buffer for highmem tests
- * @area: information for performance tests
- * @gr: pointer to results of current testcase
- */
- struct mmc_test_card {
- struct mmc_card *card;
- u8 scratch[BUFFER_SIZE];
- u8 *buffer;
- #ifdef CONFIG_HIGHMEM
- struct page *highmem;
- #endif
- struct mmc_test_area area;
- struct mmc_test_general_result *gr;
- };
- /*******************************************************************/
- /* General helper functions */
- /*******************************************************************/
- /*
- * Configure correct block size in card
- */
- static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
- {
- return mmc_set_blocklen(test->card, size);
- }
- /*
- * Fill in the mmc_request structure given a set of transfer parameters.
- */
- static void mmc_test_prepare_mrq(struct mmc_test_card *test,
- struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
- unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
- {
- BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
- if (blocks > 1) {
- mrq->cmd->opcode = write ?
- MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
- } else {
- mrq->cmd->opcode = write ?
- MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
- }
- mrq->cmd->arg = dev_addr;
- if (!mmc_card_blockaddr(test->card))
- mrq->cmd->arg <<= 9;
- mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
- if (blocks == 1)
- mrq->stop = NULL;
- else {
- mrq->stop->opcode = MMC_STOP_TRANSMISSION;
- mrq->stop->arg = 0;
- mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
- }
- mrq->data->blksz = blksz;
- mrq->data->blocks = blocks;
- mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
- mrq->data->sg = sg;
- mrq->data->sg_len = sg_len;
- mmc_set_data_timeout(mrq->data, test->card);
- }
- static int mmc_test_busy(struct mmc_command *cmd)
- {
- return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
- (R1_CURRENT_STATE(cmd->resp[0]) == 7);
- }
- /*
- * Wait for the card to finish the busy state
- */
- static int mmc_test_wait_busy(struct mmc_test_card *test)
- {
- int ret, busy;
- struct mmc_command cmd;
- busy = 0;
- do {
- memset(&cmd, 0, sizeof(struct mmc_command));
- cmd.opcode = MMC_SEND_STATUS;
- cmd.arg = test->card->rca << 16;
- cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
- ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
- if (ret)
- break;
- if (!busy && mmc_test_busy(&cmd)) {
- busy = 1;
- printk(KERN_INFO "%s: Warning: Host did not "
- "wait for busy state to end.\n",
- mmc_hostname(test->card->host));
- }
- } while (mmc_test_busy(&cmd));
- return ret;
- }
- /*
- * Transfer a single sector of kernel addressable data
- */
- static int mmc_test_buffer_transfer(struct mmc_test_card *test,
- u8 *buffer, unsigned addr, unsigned blksz, int write)
- {
- int ret;
- struct mmc_request mrq;
- struct mmc_command cmd;
- struct mmc_command stop;
- struct mmc_data data;
- struct scatterlist sg;
- memset(&mrq, 0, sizeof(struct mmc_request));
- memset(&cmd, 0, sizeof(struct mmc_command));
- memset(&data, 0, sizeof(struct mmc_data));
- memset(&stop, 0, sizeof(struct mmc_command));
- mrq.cmd = &cmd;
- mrq.data = &data;
- mrq.stop = &stop;
- sg_init_one(&sg, buffer, blksz);
- mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
- mmc_wait_for_req(test->card->host, &mrq);
- if (cmd.error)
- return cmd.error;
- if (data.error)
- return data.error;
- ret = mmc_test_wait_busy(test);
- if (ret)
- return ret;
- return 0;
- }
- static void mmc_test_free_mem(struct mmc_test_mem *mem)
- {
- if (!mem)
- return;
- while (mem->cnt--)
- __free_pages(mem->arr[mem->cnt].page,
- mem->arr[mem->cnt].order);
- kfree(mem->arr);
- kfree(mem);
- }
- /*
- * Allocate a lot of memory, preferrably max_sz but at least min_sz. In case
- * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
- * not exceed a maximum number of segments and try not to make segments much
- * bigger than maximum segment size.
- */
- static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
- unsigned long max_sz,
- unsigned int max_segs,
- unsigned int max_seg_sz)
- {
- unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
- unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
- unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
- unsigned long page_cnt = 0;
- unsigned long limit = nr_free_buffer_pages() >> 4;
- struct mmc_test_mem *mem;
- if (max_page_cnt > limit)
- max_page_cnt = limit;
- if (min_page_cnt > max_page_cnt)
- min_page_cnt = max_page_cnt;
- if (max_seg_page_cnt > max_page_cnt)
- max_seg_page_cnt = max_page_cnt;
- if (max_segs > max_page_cnt)
- max_segs = max_page_cnt;
- mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
- if (!mem)
- return NULL;
- mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_segs,
- GFP_KERNEL);
- if (!mem->arr)
- goto out_free;
- while (max_page_cnt) {
- struct page *page;
- unsigned int order;
- gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
- __GFP_NORETRY;
- order = get_order(max_seg_page_cnt << PAGE_SHIFT);
- while (1) {
- page = alloc_pages(flags, order);
- if (page || !order)
- break;
- order -= 1;
- }
- if (!page) {
- if (page_cnt < min_page_cnt)
- goto out_free;
- break;
- }
- mem->arr[mem->cnt].page = page;
- mem->arr[mem->cnt].order = order;
- mem->cnt += 1;
- if (max_page_cnt <= (1UL << order))
- break;
- max_page_cnt -= 1UL << order;
- page_cnt += 1UL << order;
- if (mem->cnt >= max_segs) {
- if (page_cnt < min_page_cnt)
- goto out_free;
- break;
- }
- }
- return mem;
- out_free:
- mmc_test_free_mem(mem);
- return NULL;
- }
- /*
- * Map memory into a scatterlist. Optionally allow the same memory to be
- * mapped more than once.
- */
- static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
- struct scatterlist *sglist, int repeat,
- unsigned int max_segs, unsigned int max_seg_sz,
- unsigned int *sg_len)
- {
- struct scatterlist *sg = NULL;
- unsigned int i;
- sg_init_table(sglist, max_segs);
- *sg_len = 0;
- do {
- for (i = 0; i < mem->cnt; i++) {
- unsigned long len = PAGE_SIZE << mem->arr[i].order;
- if (len > sz)
- len = sz;
- if (len > max_seg_sz)
- len = max_seg_sz;
- if (sg)
- sg = sg_next(sg);
- else
- sg = sglist;
- if (!sg)
- return -EINVAL;
- sg_set_page(sg, mem->arr[i].page, len, 0);
- sz -= len;
- *sg_len += 1;
- if (!sz)
- break;
- }
- } while (sz && repeat);
- if (sz)
- return -EINVAL;
- if (sg)
- sg_mark_end(sg);
- return 0;
- }
- /*
- * Map memory into a scatterlist so that no pages are contiguous. Allow the
- * same memory to be mapped more than once.
- */
- static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
- unsigned long sz,
- struct scatterlist *sglist,
- unsigned int max_segs,
- unsigned int max_seg_sz,
- unsigned int *sg_len)
- {
- struct scatterlist *sg = NULL;
- unsigned int i = mem->cnt, cnt;
- unsigned long len;
- void *base, *addr, *last_addr = NULL;
- sg_init_table(sglist, max_segs);
- *sg_len = 0;
- while (sz) {
- base = page_address(mem->arr[--i].page);
- cnt = 1 << mem->arr[i].order;
- while (sz && cnt) {
- addr = base + PAGE_SIZE * --cnt;
- if (last_addr && last_addr + PAGE_SIZE == addr)
- continue;
- last_addr = addr;
- len = PAGE_SIZE;
- if (len > max_seg_sz)
- len = max_seg_sz;
- if (len > sz)
- len = sz;
- if (sg)
- sg = sg_next(sg);
- else
- sg = sglist;
- if (!sg)
- return -EINVAL;
- sg_set_page(sg, virt_to_page(addr), len, 0);
- sz -= len;
- *sg_len += 1;
- }
- if (i == 0)
- i = mem->cnt;
- }
- if (sg)
- sg_mark_end(sg);
- return 0;
- }
- /*
- * Calculate transfer rate in bytes per second.
- */
- static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
- {
- uint64_t ns;
- ns = ts->tv_sec;
- ns *= 1000000000;
- ns += ts->tv_nsec;
- bytes *= 1000000000;
- while (ns > UINT_MAX) {
- bytes >>= 1;
- ns >>= 1;
- }
- if (!ns)
- return 0;
- do_div(bytes, (uint32_t)ns);
- return bytes;
- }
- /*
- * Save transfer results for future usage
- */
- static void mmc_test_save_transfer_result(struct mmc_test_card *test,
- unsigned int count, unsigned int sectors, struct timespec ts,
- unsigned int rate)
- {
- struct mmc_test_transfer_result *tr;
- if (!test->gr)
- return;
- tr = kmalloc(sizeof(struct mmc_test_transfer_result), GFP_KERNEL);
- if (!tr)
- return;
- tr->count = count;
- tr->sectors = sectors;
- tr->ts = ts;
- tr->rate = rate;
- list_add_tail(&tr->link, &test->gr->tr_lst);
- }
- /*
- * Print the transfer rate.
- */
- static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
- struct timespec *ts1, struct timespec *ts2)
- {
- unsigned int rate, sectors = bytes >> 9;
- struct timespec ts;
- ts = timespec_sub(*ts2, *ts1);
- rate = mmc_test_rate(bytes, &ts);
- printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
- "seconds (%u kB/s, %u KiB/s)\n",
- mmc_hostname(test->card->host), sectors, sectors >> 1,
- (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
- (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024);
- mmc_test_save_transfer_result(test, 1, sectors, ts, rate);
- }
- /*
- * Print the average transfer rate.
- */
- static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
- unsigned int count, struct timespec *ts1,
- struct timespec *ts2)
- {
- unsigned int rate, sectors = bytes >> 9;
- uint64_t tot = bytes * count;
- struct timespec ts;
- ts = timespec_sub(*ts2, *ts1);
- rate = mmc_test_rate(tot, &ts);
- printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
- "%lu.%09lu seconds (%u kB/s, %u KiB/s)\n",
- mmc_hostname(test->card->host), count, sectors, count,
- sectors >> 1, (sectors & 1 ? ".5" : ""),
- (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
- rate / 1000, rate / 1024);
- mmc_test_save_transfer_result(test, count, sectors, ts, rate);
- }
- /*
- * Return the card size in sectors.
- */
- static unsigned int mmc_test_capacity(struct mmc_card *card)
- {
- if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
- return card->ext_csd.sectors;
- else
- return card->csd.capacity << (card->csd.read_blkbits - 9);
- }
- /*******************************************************************/
- /* Test preparation and cleanup */
- /*******************************************************************/
- /*
- * Fill the first couple of sectors of the card with known data
- * so that bad reads/writes can be detected
- */
- static int __mmc_test_prepare(struct mmc_test_card *test, int write)
- {
- int ret, i;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- if (write)
- memset(test->buffer, 0xDF, 512);
- else {
- for (i = 0;i < 512;i++)
- test->buffer[i] = i;
- }
- for (i = 0;i < BUFFER_SIZE / 512;i++) {
- ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
- if (ret)
- return ret;
- }
- return 0;
- }
- static int mmc_test_prepare_write(struct mmc_test_card *test)
- {
- return __mmc_test_prepare(test, 1);
- }
- static int mmc_test_prepare_read(struct mmc_test_card *test)
- {
- return __mmc_test_prepare(test, 0);
- }
- static int mmc_test_cleanup(struct mmc_test_card *test)
- {
- int ret, i;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- memset(test->buffer, 0, 512);
- for (i = 0;i < BUFFER_SIZE / 512;i++) {
- ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
- if (ret)
- return ret;
- }
- return 0;
- }
- /*******************************************************************/
- /* Test execution helpers */
- /*******************************************************************/
- /*
- * Modifies the mmc_request to perform the "short transfer" tests
- */
- static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
- struct mmc_request *mrq, int write)
- {
- BUG_ON(!mrq || !mrq->cmd || !mrq->data);
- if (mrq->data->blocks > 1) {
- mrq->cmd->opcode = write ?
- MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
- mrq->stop = NULL;
- } else {
- mrq->cmd->opcode = MMC_SEND_STATUS;
- mrq->cmd->arg = test->card->rca << 16;
- }
- }
- /*
- * Checks that a normal transfer didn't have any errors
- */
- static int mmc_test_check_result(struct mmc_test_card *test,
- struct mmc_request *mrq)
- {
- int ret;
- BUG_ON(!mrq || !mrq->cmd || !mrq->data);
- ret = 0;
- if (!ret && mrq->cmd->error)
- ret = mrq->cmd->error;
- if (!ret && mrq->data->error)
- ret = mrq->data->error;
- if (!ret && mrq->stop && mrq->stop->error)
- ret = mrq->stop->error;
- if (!ret && mrq->data->bytes_xfered !=
- mrq->data->blocks * mrq->data->blksz)
- ret = RESULT_FAIL;
- if (ret == -EINVAL)
- ret = RESULT_UNSUP_HOST;
- return ret;
- }
- /*
- * Checks that a "short transfer" behaved as expected
- */
- static int mmc_test_check_broken_result(struct mmc_test_card *test,
- struct mmc_request *mrq)
- {
- int ret;
- BUG_ON(!mrq || !mrq->cmd || !mrq->data);
- ret = 0;
- if (!ret && mrq->cmd->error)
- ret = mrq->cmd->error;
- if (!ret && mrq->data->error == 0)
- ret = RESULT_FAIL;
- if (!ret && mrq->data->error != -ETIMEDOUT)
- ret = mrq->data->error;
- if (!ret && mrq->stop && mrq->stop->error)
- ret = mrq->stop->error;
- if (mrq->data->blocks > 1) {
- if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
- ret = RESULT_FAIL;
- } else {
- if (!ret && mrq->data->bytes_xfered > 0)
- ret = RESULT_FAIL;
- }
- if (ret == -EINVAL)
- ret = RESULT_UNSUP_HOST;
- return ret;
- }
- /*
- * Tests a basic transfer with certain parameters
- */
- static int mmc_test_simple_transfer(struct mmc_test_card *test,
- struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
- unsigned blocks, unsigned blksz, int write)
- {
- struct mmc_request mrq;
- struct mmc_command cmd;
- struct mmc_command stop;
- struct mmc_data data;
- memset(&mrq, 0, sizeof(struct mmc_request));
- memset(&cmd, 0, sizeof(struct mmc_command));
- memset(&data, 0, sizeof(struct mmc_data));
- memset(&stop, 0, sizeof(struct mmc_command));
- mrq.cmd = &cmd;
- mrq.data = &data;
- mrq.stop = &stop;
- mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
- blocks, blksz, write);
- mmc_wait_for_req(test->card->host, &mrq);
- mmc_test_wait_busy(test);
- return mmc_test_check_result(test, &mrq);
- }
- /*
- * Tests a transfer where the card will fail completely or partly
- */
- static int mmc_test_broken_transfer(struct mmc_test_card *test,
- unsigned blocks, unsigned blksz, int write)
- {
- struct mmc_request mrq;
- struct mmc_command cmd;
- struct mmc_command stop;
- struct mmc_data data;
- struct scatterlist sg;
- memset(&mrq, 0, sizeof(struct mmc_request));
- memset(&cmd, 0, sizeof(struct mmc_command));
- memset(&data, 0, sizeof(struct mmc_data));
- memset(&stop, 0, sizeof(struct mmc_command));
- mrq.cmd = &cmd;
- mrq.data = &data;
- mrq.stop = &stop;
- sg_init_one(&sg, test->buffer, blocks * blksz);
- mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
- mmc_test_prepare_broken_mrq(test, &mrq, write);
- mmc_wait_for_req(test->card->host, &mrq);
- mmc_test_wait_busy(test);
- return mmc_test_check_broken_result(test, &mrq);
- }
- /*
- * Does a complete transfer test where data is also validated
- *
- * Note: mmc_test_prepare() must have been done before this call
- */
- static int mmc_test_transfer(struct mmc_test_card *test,
- struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
- unsigned blocks, unsigned blksz, int write)
- {
- int ret, i;
- unsigned long flags;
- if (write) {
- for (i = 0;i < blocks * blksz;i++)
- test->scratch[i] = i;
- } else {
- memset(test->scratch, 0, BUFFER_SIZE);
- }
- local_irq_save(flags);
- sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
- local_irq_restore(flags);
- ret = mmc_test_set_blksize(test, blksz);
- if (ret)
- return ret;
- ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
- blocks, blksz, write);
- if (ret)
- return ret;
- if (write) {
- int sectors;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- sectors = (blocks * blksz + 511) / 512;
- if ((sectors * 512) == (blocks * blksz))
- sectors++;
- if ((sectors * 512) > BUFFER_SIZE)
- return -EINVAL;
- memset(test->buffer, 0, sectors * 512);
- for (i = 0;i < sectors;i++) {
- ret = mmc_test_buffer_transfer(test,
- test->buffer + i * 512,
- dev_addr + i, 512, 0);
- if (ret)
- return ret;
- }
- for (i = 0;i < blocks * blksz;i++) {
- if (test->buffer[i] != (u8)i)
- return RESULT_FAIL;
- }
- for (;i < sectors * 512;i++) {
- if (test->buffer[i] != 0xDF)
- return RESULT_FAIL;
- }
- } else {
- local_irq_save(flags);
- sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
- local_irq_restore(flags);
- for (i = 0;i < blocks * blksz;i++) {
- if (test->scratch[i] != (u8)i)
- return RESULT_FAIL;
- }
- }
- return 0;
- }
- /*******************************************************************/
- /* Tests */
- /*******************************************************************/
- struct mmc_test_case {
- const char *name;
- int (*prepare)(struct mmc_test_card *);
- int (*run)(struct mmc_test_card *);
- int (*cleanup)(struct mmc_test_card *);
- };
- static int mmc_test_basic_write(struct mmc_test_card *test)
- {
- int ret;
- struct scatterlist sg;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- sg_init_one(&sg, test->buffer, 512);
- ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_basic_read(struct mmc_test_card *test)
- {
- int ret;
- struct scatterlist sg;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- sg_init_one(&sg, test->buffer, 512);
- ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_verify_write(struct mmc_test_card *test)
- {
- int ret;
- struct scatterlist sg;
- sg_init_one(&sg, test->buffer, 512);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_verify_read(struct mmc_test_card *test)
- {
- int ret;
- struct scatterlist sg;
- sg_init_one(&sg, test->buffer, 512);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_multi_write(struct mmc_test_card *test)
- {
- int ret;
- unsigned int size;
- struct scatterlist sg;
- if (test->card->host->max_blk_count == 1)
- return RESULT_UNSUP_HOST;
- size = PAGE_SIZE * 2;
- size = min(size, test->card->host->max_req_size);
- size = min(size, test->card->host->max_seg_size);
- size = min(size, test->card->host->max_blk_count * 512);
- if (size < 1024)
- return RESULT_UNSUP_HOST;
- sg_init_one(&sg, test->buffer, size);
- ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_multi_read(struct mmc_test_card *test)
- {
- int ret;
- unsigned int size;
- struct scatterlist sg;
- if (test->card->host->max_blk_count == 1)
- return RESULT_UNSUP_HOST;
- size = PAGE_SIZE * 2;
- size = min(size, test->card->host->max_req_size);
- size = min(size, test->card->host->max_seg_size);
- size = min(size, test->card->host->max_blk_count * 512);
- if (size < 1024)
- return RESULT_UNSUP_HOST;
- sg_init_one(&sg, test->buffer, size);
- ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_pow2_write(struct mmc_test_card *test)
- {
- int ret, i;
- struct scatterlist sg;
- if (!test->card->csd.write_partial)
- return RESULT_UNSUP_CARD;
- for (i = 1; i < 512;i <<= 1) {
- sg_init_one(&sg, test->buffer, i);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
- if (ret)
- return ret;
- }
- return 0;
- }
- static int mmc_test_pow2_read(struct mmc_test_card *test)
- {
- int ret, i;
- struct scatterlist sg;
- if (!test->card->csd.read_partial)
- return RESULT_UNSUP_CARD;
- for (i = 1; i < 512;i <<= 1) {
- sg_init_one(&sg, test->buffer, i);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
- if (ret)
- return ret;
- }
- return 0;
- }
- static int mmc_test_weird_write(struct mmc_test_card *test)
- {
- int ret, i;
- struct scatterlist sg;
- if (!test->card->csd.write_partial)
- return RESULT_UNSUP_CARD;
- for (i = 3; i < 512;i += 7) {
- sg_init_one(&sg, test->buffer, i);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
- if (ret)
- return ret;
- }
- return 0;
- }
- static int mmc_test_weird_read(struct mmc_test_card *test)
- {
- int ret, i;
- struct scatterlist sg;
- if (!test->card->csd.read_partial)
- return RESULT_UNSUP_CARD;
- for (i = 3; i < 512;i += 7) {
- sg_init_one(&sg, test->buffer, i);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
- if (ret)
- return ret;
- }
- return 0;
- }
- static int mmc_test_align_write(struct mmc_test_card *test)
- {
- int ret, i;
- struct scatterlist sg;
- for (i = 1;i < 4;i++) {
- sg_init_one(&sg, test->buffer + i, 512);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
- if (ret)
- return ret;
- }
- return 0;
- }
- static int mmc_test_align_read(struct mmc_test_card *test)
- {
- int ret, i;
- struct scatterlist sg;
- for (i = 1;i < 4;i++) {
- sg_init_one(&sg, test->buffer + i, 512);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
- if (ret)
- return ret;
- }
- return 0;
- }
- static int mmc_test_align_multi_write(struct mmc_test_card *test)
- {
- int ret, i;
- unsigned int size;
- struct scatterlist sg;
- if (test->card->host->max_blk_count == 1)
- return RESULT_UNSUP_HOST;
- size = PAGE_SIZE * 2;
- size = min(size, test->card->host->max_req_size);
- size = min(size, test->card->host->max_seg_size);
- size = min(size, test->card->host->max_blk_count * 512);
- if (size < 1024)
- return RESULT_UNSUP_HOST;
- for (i = 1;i < 4;i++) {
- sg_init_one(&sg, test->buffer + i, size);
- ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
- if (ret)
- return ret;
- }
- return 0;
- }
- static int mmc_test_align_multi_read(struct mmc_test_card *test)
- {
- int ret, i;
- unsigned int size;
- struct scatterlist sg;
- if (test->card->host->max_blk_count == 1)
- return RESULT_UNSUP_HOST;
- size = PAGE_SIZE * 2;
- size = min(size, test->card->host->max_req_size);
- size = min(size, test->card->host->max_seg_size);
- size = min(size, test->card->host->max_blk_count * 512);
- if (size < 1024)
- return RESULT_UNSUP_HOST;
- for (i = 1;i < 4;i++) {
- sg_init_one(&sg, test->buffer + i, size);
- ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
- if (ret)
- return ret;
- }
- return 0;
- }
- static int mmc_test_xfersize_write(struct mmc_test_card *test)
- {
- int ret;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- ret = mmc_test_broken_transfer(test, 1, 512, 1);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_xfersize_read(struct mmc_test_card *test)
- {
- int ret;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- ret = mmc_test_broken_transfer(test, 1, 512, 0);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
- {
- int ret;
- if (test->card->host->max_blk_count == 1)
- return RESULT_UNSUP_HOST;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- ret = mmc_test_broken_transfer(test, 2, 512, 1);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
- {
- int ret;
- if (test->card->host->max_blk_count == 1)
- return RESULT_UNSUP_HOST;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- ret = mmc_test_broken_transfer(test, 2, 512, 0);
- if (ret)
- return ret;
- return 0;
- }
- #ifdef CONFIG_HIGHMEM
- static int mmc_test_write_high(struct mmc_test_card *test)
- {
- int ret;
- struct scatterlist sg;
- sg_init_table(&sg, 1);
- sg_set_page(&sg, test->highmem, 512, 0);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_read_high(struct mmc_test_card *test)
- {
- int ret;
- struct scatterlist sg;
- sg_init_table(&sg, 1);
- sg_set_page(&sg, test->highmem, 512, 0);
- ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_multi_write_high(struct mmc_test_card *test)
- {
- int ret;
- unsigned int size;
- struct scatterlist sg;
- if (test->card->host->max_blk_count == 1)
- return RESULT_UNSUP_HOST;
- size = PAGE_SIZE * 2;
- size = min(size, test->card->host->max_req_size);
- size = min(size, test->card->host->max_seg_size);
- size = min(size, test->card->host->max_blk_count * 512);
- if (size < 1024)
- return RESULT_UNSUP_HOST;
- sg_init_table(&sg, 1);
- sg_set_page(&sg, test->highmem, size, 0);
- ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
- if (ret)
- return ret;
- return 0;
- }
- static int mmc_test_multi_read_high(struct mmc_test_card *test)
- {
- int ret;
- unsigned int size;
- struct scatterlist sg;
- if (test->card->host->max_blk_count == 1)
- return RESULT_UNSUP_HOST;
- size = PAGE_SIZE * 2;
- size = min(size, test->card->host->max_req_size);
- size = min(size, test->card->host->max_seg_size);
- size = min(size, test->card->host->max_blk_count * 512);
- if (size < 1024)
- return RESULT_UNSUP_HOST;
- sg_init_table(&sg, 1);
- sg_set_page(&sg, test->highmem, size, 0);
- ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
- if (ret)
- return ret;
- return 0;
- }
- #else
- static int mmc_test_no_highmem(struct mmc_test_card *test)
- {
- printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
- mmc_hostname(test->card->host));
- return 0;
- }
- #endif /* CONFIG_HIGHMEM */
- /*
- * Map sz bytes so that it can be transferred.
- */
- static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
- int max_scatter)
- {
- struct mmc_test_area *t = &test->area;
- int err;
- t->blocks = sz >> 9;
- if (max_scatter) {
- err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
- t->max_segs, t->max_seg_sz,
- &t->sg_len);
- } else {
- err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
- t->max_seg_sz, &t->sg_len);
- }
- if (err)
- printk(KERN_INFO "%s: Failed to map sg list\n",
- mmc_hostname(test->card->host));
- return err;
- }
- /*
- * Transfer bytes mapped by mmc_test_area_map().
- */
- static int mmc_test_area_transfer(struct mmc_test_card *test,
- unsigned int dev_addr, int write)
- {
- struct mmc_test_area *t = &test->area;
- return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
- t->blocks, 512, write);
- }
- /*
- * Map and transfer bytes.
- */
- static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
- unsigned int dev_addr, int write, int max_scatter,
- int timed)
- {
- struct timespec ts1, ts2;
- int ret;
- /*
- * In the case of a maximally scattered transfer, the maximum transfer
- * size is further limited by using PAGE_SIZE segments.
- */
- if (max_scatter) {
- struct mmc_test_area *t = &test->area;
- unsigned long max_tfr;
- if (t->max_seg_sz >= PAGE_SIZE)
- max_tfr = t->max_segs * PAGE_SIZE;
- else
- max_tfr = t->max_segs * t->max_seg_sz;
- if (sz > max_tfr)
- sz = max_tfr;
- }
- ret = mmc_test_area_map(test, sz, max_scatter);
- if (ret)
- return ret;
- if (timed)
- getnstimeofday(&ts1);
- ret = mmc_test_area_transfer(test, dev_addr, write);
- if (ret)
- return ret;
- if (timed)
- getnstimeofday(&ts2);
- if (timed)
- mmc_test_print_rate(test, sz, &ts1, &ts2);
- return 0;
- }
- /*
- * Write the test area entirely.
- */
- static int mmc_test_area_fill(struct mmc_test_card *test)
- {
- return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
- 1, 0, 0);
- }
- /*
- * Erase the test area entirely.
- */
- static int mmc_test_area_erase(struct mmc_test_card *test)
- {
- struct mmc_test_area *t = &test->area;
- if (!mmc_can_erase(test->card))
- return 0;
- return mmc_erase(test->card, t->dev_addr, test->area.max_sz >> 9,
- MMC_ERASE_ARG);
- }
- /*
- * Cleanup struct mmc_test_area.
- */
- static int mmc_test_area_cleanup(struct mmc_test_card *test)
- {
- struct mmc_test_area *t = &test->area;
- kfree(t->sg);
- mmc_test_free_mem(t->mem);
- return 0;
- }
- /*
- * Initialize an area for testing large transfers. The size of the area is the
- * preferred erase size which is a good size for optimal transfer speed. Note
- * that is typically 4MiB for modern cards. The test area is set to the middle
- * of the card because cards may have different charateristics at the front
- * (for FAT file system optimization). Optionally, the area is erased (if the
- * card supports it) which may improve write performance. Optionally, the area
- * is filled with data for subsequent read tests.
- */
- static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
- {
- struct mmc_test_area *t = &test->area;
- unsigned long min_sz = 64 * 1024;
- int ret;
- ret = mmc_test_set_blksize(test, 512);
- if (ret)
- return ret;
- if (test->card->pref_erase > TEST_AREA_MAX_SIZE >> 9)
- t->max_sz = TEST_AREA_MAX_SIZE;
- else
- t->max_sz = (unsigned long)test->card->pref_erase << 9;
- t->max_segs = test->card->host->max_segs;
- t->max_seg_sz = test->card->host->max_seg_size;
- t->max_tfr = t->max_sz;
- if (t->max_tfr >> 9 > test->card->host->max_blk_count)
- t->max_tfr = test->card->host->max_blk_count << 9;
- if (t->max_tfr > test->card->host->max_req_size)
- t->max_tfr = test->card->host->max_req_size;
- if (t->max_tfr / t->max_seg_sz > t->max_segs)
- t->max_tfr = t->max_segs * t->max_seg_sz;
- /*
- * Try to allocate enough memory for a max. sized transfer. Less is OK
- * because the same memory can be mapped into the scatterlist more than
- * once. Also, take into account the limits imposed on scatterlist
- * segments by the host driver.
- */
- t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
- t->max_seg_sz);
- if (!t->mem)
- return -ENOMEM;
- t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
- if (!t->sg) {
- ret = -ENOMEM;
- goto out_free;
- }
- t->dev_addr = mmc_test_capacity(test->card) / 2;
- t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
- if (erase) {
- ret = mmc_test_area_erase(test);
- if (ret)
- goto out_free;
- }
- if (fill) {
- ret = mmc_test_area_fill(test);
- if (ret)
- goto out_free;
- }
- return 0;
- out_free:
- mmc_test_area_cleanup(test);
- return ret;
- }
- /*
- * Prepare for large transfers. Do not erase the test area.
- */
- static int mmc_test_area_prepare(struct mmc_test_card *test)
- {
- return mmc_test_area_init(test, 0, 0);
- }
- /*
- * Prepare for large transfers. Do erase the test area.
- */
- static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
- {
- return mmc_test_area_init(test, 1, 0);
- }
- /*
- * Prepare for large transfers. Erase and fill the test area.
- */
- static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
- {
- return mmc_test_area_init(test, 1, 1);
- }
- /*
- * Test best-case performance. Best-case performance is expected from
- * a single large transfer.
- *
- * An additional option (max_scatter) allows the measurement of the same
- * transfer but with no contiguous pages in the scatter list. This tests
- * the efficiency of DMA to handle scattered pages.
- */
- static int mmc_test_best_performance(struct mmc_test_card *test, int write,
- int max_scatter)
- {
- return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
- write, max_scatter, 1);
- }
- /*
- * Best-case read performance.
- */
- static int mmc_test_best_read_performance(struct mmc_test_card *test)
- {
- return mmc_test_best_performance(test, 0, 0);
- }
- /*
- * Best-case write performance.
- */
- static int mmc_test_best_write_performance(struct mmc_test_card *test)
- {
- return mmc_test_best_performance(test, 1, 0);
- }
- /*
- * Best-case read performance into scattered pages.
- */
- static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
- {
- return mmc_test_best_performance(test, 0, 1);
- }
- /*
- * Best-case write performance from scattered pages.
- */
- static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
- {
- return mmc_test_best_performance(test, 1, 1);
- }
- /*
- * Single read performance by transfer size.
- */
- static int mmc_test_profile_read_perf(struct mmc_test_card *test)
- {
- unsigned long sz;
- unsigned int dev_addr;
- int ret;
- for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
- dev_addr = test->area.dev_addr + (sz >> 9);
- ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
- if (ret)
- return ret;
- }
- sz = test->area.max_tfr;
- dev_addr = test->area.dev_addr;
- return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
- }
- /*
- * Single write performance by transfer size.
- */
- static int mmc_test_profile_write_perf(struct mmc_test_card *test)
- {
- unsigned long sz;
- unsigned int dev_addr;
- int ret;
- ret = mmc_test_area_erase(test);
- if (ret)
- return ret;
- for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
- dev_addr = test->area.dev_addr + (sz >> 9);
- ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
- if (ret)
- return ret;
- }
- ret = mmc_test_area_erase(test);
- if (ret)
- return ret;
- sz = test->area.max_tfr;
- dev_addr = test->area.dev_addr;
- return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
- }
- /*
- * Single trim performance by transfer size.
- */
- static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
- {
- unsigned long sz;
- unsigned int dev_addr;
- struct timespec ts1, ts2;
- int ret;
- if (!mmc_can_trim(test->card))
- return RESULT_UNSUP_CARD;
- if (!mmc_can_erase(test->card))
- return RESULT_UNSUP_HOST;
- for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
- dev_addr = test->area.dev_addr + (sz >> 9);
- getnstimeofday(&ts1);
- ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
- if (ret)
- return ret;
- getnstimeofday(&ts2);
- mmc_test_print_rate(test, sz, &ts1, &ts2);
- }
- dev_addr = test->area.dev_addr;
- getnstimeofday(&ts1);
- ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
- if (ret)
- return ret;
- getnstimeofday(&ts2);
- mmc_test_print_rate(test, sz, &ts1, &ts2);
- return 0;
- }
- static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
- {
- unsigned int dev_addr, i, cnt;
- struct timespec ts1, ts2;
- int ret;
- cnt = test->area.max_sz / sz;
- dev_addr = test->area.dev_addr;
- getnstimeofday(&ts1);
- for (i = 0; i < cnt; i++) {
- ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
- if (ret)
- return ret;
- dev_addr += (sz >> 9);
- }
- getnstimeofday(&ts2);
- mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
- return 0;
- }
- /*
- * Consecutive read performance by transfer size.
- */
- static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
- {
- unsigned long sz;
- int ret;
- for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
- ret = mmc_test_seq_read_perf(test, sz);
- if (ret)
- return ret;
- }
- sz = test->area.max_tfr;
- return mmc_test_seq_read_perf(test, sz);
- }
- static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
- {
- unsigned int dev_addr, i, cnt;
- struct timespec ts1, ts2;
- int ret;
- ret = mmc_test_area_erase(test);
- if (ret)
- return ret;
- cnt = test->area.max_sz / sz;
- dev_addr = test->area.dev_addr;
- getnstimeofday(&ts1);
- for (i = 0; i < cnt; i++) {
- ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
- if (ret)
- return ret;
- dev_addr += (sz >> 9);
- }
- getnstimeofday(&ts2);
- mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
- return 0;
- }
- /*
- * Consecutive write performance by transfer size.
- */
- static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
- {
- unsigned long sz;
- int ret;
- for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
- ret = mmc_test_seq_write_perf(test, sz);
- if (ret)
- return ret;
- }
- sz = test->area.max_tfr;
- return mmc_test_seq_write_perf(test, sz);
- }
- /*
- * Consecutive trim performance by transfer size.
- */
- static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
- {
- unsigned long sz;
- unsigned int dev_addr, i, cnt;
- struct timespec ts1, ts2;
- int ret;
- if (!mmc_can_trim(test->card))
- return RESULT_UNSUP_CARD;
- if (!mmc_can_erase(test->card))
- return RESULT_UNSUP_HOST;
- for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
- ret = mmc_test_area_erase(test);
- if (ret)
- return ret;
- ret = mmc_test_area_fill(test);
- if (ret)
- return ret;
- cnt = test->area.max_sz / sz;
- dev_addr = test->area.dev_addr;
- getnstimeofday(&ts1);
- for (i = 0; i < cnt; i++) {
- ret = mmc_erase(test->card, dev_addr, sz >> 9,
- MMC_TRIM_ARG);
- if (ret)
- return ret;
- dev_addr += (sz >> 9);
- }
- getnstimeofday(&ts2);
- mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
- }
- return 0;
- }
- static const struct mmc_test_case mmc_test_cases[] = {
- {
- .name = "Basic write (no data verification)",
- .run = mmc_test_basic_write,
- },
- {
- .name = "Basic read (no data verification)",
- .run = mmc_test_basic_read,
- },
- {
- .name = "Basic write (with data verification)",
- .prepare = mmc_test_prepare_write,
- .run = mmc_test_verify_write,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Basic read (with data verification)",
- .prepare = mmc_test_prepare_read,
- .run = mmc_test_verify_read,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Multi-block write",
- .prepare = mmc_test_prepare_write,
- .run = mmc_test_multi_write,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Multi-block read",
- .prepare = mmc_test_prepare_read,
- .run = mmc_test_multi_read,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Power of two block writes",
- .prepare = mmc_test_prepare_write,
- .run = mmc_test_pow2_write,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Power of two block reads",
- .prepare = mmc_test_prepare_read,
- .run = mmc_test_pow2_read,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Weird sized block writes",
- .prepare = mmc_test_prepare_write,
- .run = mmc_test_weird_write,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Weird sized block reads",
- .prepare = mmc_test_prepare_read,
- .run = mmc_test_weird_read,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Badly aligned write",
- .prepare = mmc_test_prepare_write,
- .run = mmc_test_align_write,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Badly aligned read",
- .prepare = mmc_test_prepare_read,
- .run = mmc_test_align_read,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Badly aligned multi-block write",
- .prepare = mmc_test_prepare_write,
- .run = mmc_test_align_multi_write,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Badly aligned multi-block read",
- .prepare = mmc_test_prepare_read,
- .run = mmc_test_align_multi_read,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Correct xfer_size at write (start failure)",
- .run = mmc_test_xfersize_write,
- },
- {
- .name = "Correct xfer_size at read (start failure)",
- .run = mmc_test_xfersize_read,
- },
- {
- .name = "Correct xfer_size at write (midway failure)",
- .run = mmc_test_multi_xfersize_write,
- },
- {
- .name = "Correct xfer_size at read (midway failure)",
- .run = mmc_test_multi_xfersize_read,
- },
- #ifdef CONFIG_HIGHMEM
- {
- .name = "Highmem write",
- .prepare = mmc_test_prepare_write,
- .run = mmc_test_write_high,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Highmem read",
- .prepare = mmc_test_prepare_read,
- .run = mmc_test_read_high,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Multi-block highmem write",
- .prepare = mmc_test_prepare_write,
- .run = mmc_test_multi_write_high,
- .cleanup = mmc_test_cleanup,
- },
- {
- .name = "Multi-block highmem read",
- .prepare = mmc_test_prepare_read,
- .run = mmc_test_multi_read_high,
- .cleanup = mmc_test_cleanup,
- },
- #else
- {
- .name = "Highmem write",
- .run = mmc_test_no_highmem,
- },
- {
- .name = "Highmem read",
- .run = mmc_test_no_highmem,
- },
- {
- .name = "Multi-block highmem write",
- .run = mmc_test_no_highmem,
- },
- {
- .name = "Multi-block highmem read",
- .run = mmc_test_no_highmem,
- },
- #endif /* CONFIG_HIGHMEM */
- {
- .name = "Best-case read performance",
- .prepare = mmc_test_area_prepare_fill,
- .run = mmc_test_best_read_performance,
- .cleanup = mmc_test_area_cleanup,
- },
- {
- .name = "Best-case write performance",
- .prepare = mmc_test_area_prepare_erase,
- .run = mmc_test_best_write_performance,
- .cleanup = mmc_test_area_cleanup,
- },
- {
- .name = "Best-case read performance into scattered pages",
- .prepare = mmc_test_area_prepare_fill,
- .run = mmc_test_best_read_perf_max_scatter,
- .cleanup = mmc_test_area_cleanup,
- },
- {
- .name = "Best-case write performance from scattered pages",
- .prepare = mmc_test_area_prepare_erase,
- .run = mmc_test_best_write_perf_max_scatter,
- .cleanup = mmc_test_area_cleanup,
- },
- {
- .name = "Single read performance by transfer size",
- .prepare = mmc_test_area_prepare_fill,
- .run = mmc_test_profile_read_perf,
- .cleanup = mmc_test_area_cleanup,
- },
- {
- .name = "Single write performance by transfer size",
- .prepare = mmc_test_area_prepare,
- .run = mmc_test_profile_write_perf,
- .cleanup = mmc_test_area_cleanup,
- },
- {
- .name = "Single trim performance by transfer size",
- .prepare = mmc_test_area_prepare_fill,
- .run = mmc_test_profile_trim_perf,
- .cleanup = mmc_test_area_cleanup,
- },
- {
- .name = "Consecutive read performance by transfer size",
- .prepare = mmc_test_area_prepare_fill,
- .run = mmc_test_profile_seq_read_perf,
- .cleanup = mmc_test_area_cleanup,
- },
- {
- .name = "Consecutive write performance by transfer size",
- .prepare = mmc_test_area_prepare,
- .run = mmc_test_profile_seq_write_perf,
- .cleanup = mmc_test_area_cleanup,
- },
- {
- .name = "Consecutive trim performance by transfer size",
- .prepare = mmc_test_area_prepare,
- .run = mmc_test_profile_seq_trim_perf,
- .cleanup = mmc_test_area_cleanup,
- },
- };
- static DEFINE_MUTEX(mmc_test_lock);
- static LIST_HEAD(mmc_test_result);
- static void mmc_test_run(struct mmc_test_card *test, int testcase)
- {
- int i, ret;
- printk(KERN_INFO "%s: Starting tests of card %s...\n",
- mmc_hostname(test->card->host), mmc_card_id(test->card));
- mmc_claim_host(test->card->host);
- for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
- struct mmc_test_general_result *gr;
- if (testcase && ((i + 1) != testcase))
- continue;
- printk(KERN_INFO "%s: Test case %d. %s...\n",
- mmc_hostname(test->card->host), i + 1,
- mmc_test_cases[i].name);
- if (mmc_test_cases[i].prepare) {
- ret = mmc_test_cases[i].prepare(test);
- if (ret) {
- printk(KERN_INFO "%s: Result: Prepare "
- "stage failed! (%d)\n",
- mmc_hostname(test->card->host),
- ret);
- continue;
- }
- }
- gr = kzalloc(sizeof(struct mmc_test_general_result),
- GFP_KERNEL);
- if (gr) {
- INIT_LIST_HEAD(&gr->tr_lst);
- /* Assign data what we know already */
- gr->card = test->card;
- gr->testcase = i;
- /* Append container to global one */
- list_add_tail(&gr->link, &mmc_test_result);
- /*
- * Save the pointer to created container in our private
- * structure.
- */
- test->gr = gr;
- }
- ret = mmc_test_cases[i].run(test);
- switch (ret) {
- case RESULT_OK:
- printk(KERN_INFO "%s: Result: OK\n",
- mmc_hostname(test->card->host));
- break;
- case RESULT_FAIL:
- printk(KERN_INFO "%s: Result: FAILED\n",
- mmc_hostname(test->card->host));
- break;
- case RESULT_UNSUP_HOST:
- printk(KERN_INFO "%s: Result: UNSUPPORTED "
- "(by host)\n",
- mmc_hostname(test->card->host));
- break;
- case RESULT_UNSUP_CARD:
- printk(KERN_INFO "%s: Result: UNSUPPORTED "
- "(by card)\n",
- mmc_hostname(test->card->host));
- break;
- default:
- printk(KERN_INFO "%s: Result: ERROR (%d)\n",
- mmc_hostname(test->card->host), ret);
- }
- /* Save the result */
- if (gr)
- gr->result = ret;
- if (mmc_test_cases[i].cleanup) {
- ret = mmc_test_cases[i].cleanup(test);
- if (ret) {
- printk(KERN_INFO "%s: Warning: Cleanup "
- "stage failed! (%d)\n",
- mmc_hostname(test->card->host),
- ret);
- }
- }
- }
- mmc_release_host(test->card->host);
- printk(KERN_INFO "%s: Tests completed.\n",
- mmc_hostname(test->card->host));
- }
- static void mmc_test_free_result(struct mmc_card *card)
- {
- struct mmc_test_general_result *gr, *grs;
- mutex_lock(&mmc_test_lock);
- list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
- struct mmc_test_transfer_result *tr, *trs;
- if (card && gr->card != card)
- continue;
- list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
- list_del(&tr->link);
- kfree(tr);
- }
- list_del(&gr->link);
- kfree(gr);
- }
- mutex_unlock(&mmc_test_lock);
- }
- static LIST_HEAD(mmc_test_file_test);
- static int mtf_test_show(struct seq_file *sf, void *data)
- {
- struct mmc_card *card = (struct mmc_card *)sf->private;
- struct mmc_test_general_result *gr;
- mutex_lock(&mmc_test_lock);
- list_for_each_entry(gr, &mmc_test_result, link) {
- struct mmc_test_transfer_result *tr;
- if (gr->card != card)
- continue;
- seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
- list_for_each_entry(tr, &gr->tr_lst, link) {
- seq_printf(sf, "%u %d %lu.%09lu %u\n",
- tr->count, tr->sectors,
- (unsigned long)tr->ts.tv_sec,
- (unsigned long)tr->ts.tv_nsec,
- tr->rate);
- }
- }
- mutex_unlock(&mmc_test_lock);
- return 0;
- }
- static int mtf_test_open(struct inode *inode, struct file *file)
- {
- return single_open(file, mtf_test_show, inode->i_private);
- }
- static ssize_t mtf_test_write(struct file *file, const char __user *buf,
- size_t count, loff_t *pos)
- {
- struct seq_file *sf = (struct seq_file *)file->private_data;
- struct mmc_card *card = (struct mmc_card *)sf->private;
- struct mmc_test_card *test;
- char lbuf[12];
- long testcase;
- if (count >= sizeof(lbuf))
- return -EINVAL;
- if (copy_from_user(lbuf, buf, count))
- return -EFAULT;
- lbuf[count] = '\0';
- if (strict_strtol(lbuf, 10, &testcase))
- return -EINVAL;
- test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
- if (!test)
- return -ENOMEM;
- /*
- * Remove all test cases associated with given card. Thus we have only
- * actual data of the last run.
- */
- mmc_test_free_result(card);
- test->card = card;
- test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
- #ifdef CONFIG_HIGHMEM
- test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
- #endif
- #ifdef CONFIG_HIGHMEM
- if (test->buffer && test->highmem) {
- #else
- if (test->buffer) {
- #endif
- mutex_lock(&mmc_test_lock);
- mmc_test_run(test, testcase);
- mutex_unlock(&mmc_test_lock);
- }
- #ifdef CONFIG_HIGHMEM
- __free_pages(test->highmem, BUFFER_ORDER);
- #endif
- kfree(test->buffer);
- kfree(test);
- return count;
- }
- static const struct file_operations mmc_test_fops_test = {
- .open = mtf_test_open,
- .read = seq_read,
- .write = mtf_test_write,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static void mmc_test_free_file_test(struct mmc_card *card)
- {
- struct mmc_test_dbgfs_file *df, *dfs;
- mutex_lock(&mmc_test_lock);
- list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
- if (card && df->card != card)
- continue;
- debugfs_remove(df->file);
- list_del(&df->link);
- kfree(df);
- }
- mutex_unlock(&mmc_test_lock);
- }
- static int mmc_test_register_file_test(struct mmc_card *card)
- {
- struct dentry *file = NULL;
- struct mmc_test_dbgfs_file *df;
- int ret = 0;
- mutex_lock(&mmc_test_lock);
- if (card->debugfs_root)
- file = debugfs_create_file("test", S_IWUSR | S_IRUGO,
- card->debugfs_root, card, &mmc_test_fops_test);
- if (IS_ERR_OR_NULL(file)) {
- dev_err(&card->dev,
- "Can't create file. Perhaps debugfs is disabled.\n");
- ret = -ENODEV;
- goto err;
- }
- df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
- if (!df) {
- debugfs_remove(file);
- dev_err(&card->dev,
- "Can't allocate memory for internal usage.\n");
- ret = -ENOMEM;
- goto err;
- }
- df->card = card;
- df->file = file;
- list_add(&df->link, &mmc_test_file_test);
- err:
- mutex_unlock(&mmc_test_lock);
- return ret;
- }
- static int mmc_test_probe(struct mmc_card *card)
- {
- int ret;
- if (!mmc_card_mmc(card) && !mmc_card_sd(card))
- return -ENODEV;
- ret = mmc_test_register_file_test(card);
- if (ret)
- return ret;
- dev_info(&card->dev, "Card claimed for testing.\n");
- return 0;
- }
- static void mmc_test_remove(struct mmc_card *card)
- {
- mmc_test_free_result(card);
- mmc_test_free_file_test(card);
- }
- static struct mmc_driver mmc_driver = {
- .drv = {
- .name = "mmc_test",
- },
- .probe = mmc_test_probe,
- .remove = mmc_test_remove,
- };
- static int __init mmc_test_init(void)
- {
- return mmc_register_driver(&mmc_driver);
- }
- static void __exit mmc_test_exit(void)
- {
- /* Clear stalled data if card is still plugged */
- mmc_test_free_result(NULL);
- mmc_test_free_file_test(NULL);
- mmc_unregister_driver(&mmc_driver);
- }
- module_init(mmc_test_init);
- module_exit(mmc_test_exit);
- MODULE_LICENSE("GPL");
- MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
- MODULE_AUTHOR("Pierre Ossman");
|