2 * Copyright 2007-2008 Pierre Ossman
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.
10 #include <linux/mmc/core.h>
11 #include <linux/mmc/card.h>
12 #include <linux/mmc/host.h>
13 #include <linux/mmc/mmc.h>
14 #include <linux/slab.h>
16 #include <linux/scatterlist.h>
17 #include <linux/swap.h> /* For nr_free_buffer_pages() */
18 #include <linux/list.h>
20 #include <linux/debugfs.h>
21 #include <linux/uaccess.h>
22 #include <linux/seq_file.h>
23 #include <linux/module.h>
32 #define RESULT_UNSUP_HOST 2
33 #define RESULT_UNSUP_CARD 3
35 #define BUFFER_ORDER 2
36 #define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
38 #define TEST_ALIGN_END 8
41 * Limit the test area size to the maximum MMC HC erase group size. Note that
42 * the maximum SD allocation unit size is just 4MiB.
44 #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
47 * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
48 * @page: first page in the allocation
49 * @order: order of the number of pages allocated
51 struct mmc_test_pages {
57 * struct mmc_test_mem - allocated memory.
58 * @arr: array of allocations
59 * @cnt: number of allocations
62 struct mmc_test_pages *arr;
67 * struct mmc_test_area - information for performance tests.
68 * @max_sz: test area size (in bytes)
69 * @dev_addr: address on card at which to do performance tests
70 * @max_tfr: maximum transfer size allowed by driver (in bytes)
71 * @max_segs: maximum segments allowed by driver in scatterlist @sg
72 * @max_seg_sz: maximum segment size allowed by driver
73 * @blocks: number of (512 byte) blocks currently mapped by @sg
74 * @sg_len: length of currently mapped scatterlist @sg
75 * @mem: allocated memory
78 struct mmc_test_area {
80 unsigned int dev_addr;
82 unsigned int max_segs;
83 unsigned int max_seg_sz;
86 struct mmc_test_mem *mem;
87 struct scatterlist *sg;
91 * struct mmc_test_transfer_result - transfer results for performance tests.
92 * @link: double-linked list
93 * @count: amount of group of sectors to check
94 * @sectors: amount of sectors to check in one group
95 * @ts: time values of transfer
96 * @rate: calculated transfer rate
97 * @iops: I/O operations per second (times 100)
99 struct mmc_test_transfer_result {
100 struct list_head link;
102 unsigned int sectors;
109 * struct mmc_test_general_result - results for tests.
110 * @link: double-linked list
111 * @card: card under test
112 * @testcase: number of test case
113 * @result: result of test run
114 * @tr_lst: transfer measurements if any as mmc_test_transfer_result
116 struct mmc_test_general_result {
117 struct list_head link;
118 struct mmc_card *card;
121 struct list_head tr_lst;
125 * struct mmc_test_dbgfs_file - debugfs related file.
126 * @link: double-linked list
127 * @card: card under test
128 * @file: file created under debugfs
130 struct mmc_test_dbgfs_file {
131 struct list_head link;
132 struct mmc_card *card;
137 * struct mmc_test_card - test information.
138 * @card: card under test
139 * @scratch: transfer buffer
140 * @buffer: transfer buffer
141 * @highmem: buffer for highmem tests
142 * @area: information for performance tests
143 * @gr: pointer to results of current testcase
145 struct mmc_test_card {
146 struct mmc_card *card;
148 u8 scratch[BUFFER_SIZE];
150 #ifdef CONFIG_HIGHMEM
151 struct page *highmem;
153 struct mmc_test_area area;
154 struct mmc_test_general_result *gr;
157 enum mmc_test_prep_media {
158 MMC_TEST_PREP_NONE = 0,
159 MMC_TEST_PREP_WRITE_FULL = 1 << 0,
160 MMC_TEST_PREP_ERASE = 1 << 1,
163 struct mmc_test_multiple_rw {
164 unsigned int *sg_len;
169 bool do_nonblock_req;
170 enum mmc_test_prep_media prepare;
173 struct mmc_test_async_req {
174 struct mmc_async_req areq;
175 struct mmc_test_card *test;
178 /*******************************************************************/
179 /* General helper functions */
180 /*******************************************************************/
183 * Configure correct block size in card
185 static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
187 return mmc_set_blocklen(test->card, size);
190 static bool mmc_test_card_cmd23(struct mmc_card *card)
192 return mmc_card_mmc(card) ||
193 (mmc_card_sd(card) && card->scr.cmds & SD_SCR_CMD23_SUPPORT);
196 static void mmc_test_prepare_sbc(struct mmc_test_card *test,
197 struct mmc_request *mrq, unsigned int blocks)
199 struct mmc_card *card = test->card;
201 if (!mrq->sbc || !mmc_host_cmd23(card->host) ||
202 !mmc_test_card_cmd23(card) || !mmc_op_multi(mrq->cmd->opcode) ||
203 (card->quirks & MMC_QUIRK_BLK_NO_CMD23)) {
208 mrq->sbc->opcode = MMC_SET_BLOCK_COUNT;
209 mrq->sbc->arg = blocks;
210 mrq->sbc->flags = MMC_RSP_R1 | MMC_CMD_AC;
214 * Fill in the mmc_request structure given a set of transfer parameters.
216 static void mmc_test_prepare_mrq(struct mmc_test_card *test,
217 struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
218 unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
220 if (WARN_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop))
224 mrq->cmd->opcode = write ?
225 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
227 mrq->cmd->opcode = write ?
228 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
231 mrq->cmd->arg = dev_addr;
232 if (!mmc_card_blockaddr(test->card))
235 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
240 mrq->stop->opcode = MMC_STOP_TRANSMISSION;
242 mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
245 mrq->data->blksz = blksz;
246 mrq->data->blocks = blocks;
247 mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
249 mrq->data->sg_len = sg_len;
251 mmc_test_prepare_sbc(test, mrq, blocks);
253 mmc_set_data_timeout(mrq->data, test->card);
256 static int mmc_test_busy(struct mmc_command *cmd)
258 return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
259 (R1_CURRENT_STATE(cmd->resp[0]) == R1_STATE_PRG);
263 * Wait for the card to finish the busy state
265 static int mmc_test_wait_busy(struct mmc_test_card *test)
268 struct mmc_command cmd = {};
272 memset(&cmd, 0, sizeof(struct mmc_command));
274 cmd.opcode = MMC_SEND_STATUS;
275 cmd.arg = test->card->rca << 16;
276 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
278 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
282 if (!busy && mmc_test_busy(&cmd)) {
284 if (test->card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
285 pr_info("%s: Warning: Host did not wait for busy state to end.\n",
286 mmc_hostname(test->card->host));
288 } while (mmc_test_busy(&cmd));
294 * Transfer a single sector of kernel addressable data
296 static int mmc_test_buffer_transfer(struct mmc_test_card *test,
297 u8 *buffer, unsigned addr, unsigned blksz, int write)
299 struct mmc_request mrq = {};
300 struct mmc_command cmd = {};
301 struct mmc_command stop = {};
302 struct mmc_data data = {};
304 struct scatterlist sg;
310 sg_init_one(&sg, buffer, blksz);
312 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
314 mmc_wait_for_req(test->card->host, &mrq);
321 return mmc_test_wait_busy(test);
324 static void mmc_test_free_mem(struct mmc_test_mem *mem)
329 __free_pages(mem->arr[mem->cnt].page,
330 mem->arr[mem->cnt].order);
336 * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
337 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
338 * not exceed a maximum number of segments and try not to make segments much
339 * bigger than maximum segment size.
341 static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
342 unsigned long max_sz,
343 unsigned int max_segs,
344 unsigned int max_seg_sz)
346 unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
347 unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
348 unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
349 unsigned long page_cnt = 0;
350 unsigned long limit = nr_free_buffer_pages() >> 4;
351 struct mmc_test_mem *mem;
353 if (max_page_cnt > limit)
354 max_page_cnt = limit;
355 if (min_page_cnt > max_page_cnt)
356 min_page_cnt = max_page_cnt;
358 if (max_seg_page_cnt > max_page_cnt)
359 max_seg_page_cnt = max_page_cnt;
361 if (max_segs > max_page_cnt)
362 max_segs = max_page_cnt;
364 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
368 mem->arr = kcalloc(max_segs, sizeof(*mem->arr), GFP_KERNEL);
372 while (max_page_cnt) {
375 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
378 order = get_order(max_seg_page_cnt << PAGE_SHIFT);
380 page = alloc_pages(flags, order);
386 if (page_cnt < min_page_cnt)
390 mem->arr[mem->cnt].page = page;
391 mem->arr[mem->cnt].order = order;
393 if (max_page_cnt <= (1UL << order))
395 max_page_cnt -= 1UL << order;
396 page_cnt += 1UL << order;
397 if (mem->cnt >= max_segs) {
398 if (page_cnt < min_page_cnt)
407 mmc_test_free_mem(mem);
412 * Map memory into a scatterlist. Optionally allow the same memory to be
413 * mapped more than once.
415 static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long size,
416 struct scatterlist *sglist, int repeat,
417 unsigned int max_segs, unsigned int max_seg_sz,
418 unsigned int *sg_len, int min_sg_len)
420 struct scatterlist *sg = NULL;
422 unsigned long sz = size;
424 sg_init_table(sglist, max_segs);
425 if (min_sg_len > max_segs)
426 min_sg_len = max_segs;
430 for (i = 0; i < mem->cnt; i++) {
431 unsigned long len = PAGE_SIZE << mem->arr[i].order;
433 if (min_sg_len && (size / min_sg_len < len))
434 len = ALIGN(size / min_sg_len, 512);
437 if (len > max_seg_sz)
445 sg_set_page(sg, mem->arr[i].page, len, 0);
451 } while (sz && repeat);
463 * Map memory into a scatterlist so that no pages are contiguous. Allow the
464 * same memory to be mapped more than once.
466 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
468 struct scatterlist *sglist,
469 unsigned int max_segs,
470 unsigned int max_seg_sz,
471 unsigned int *sg_len)
473 struct scatterlist *sg = NULL;
474 unsigned int i = mem->cnt, cnt;
476 void *base, *addr, *last_addr = NULL;
478 sg_init_table(sglist, max_segs);
482 base = page_address(mem->arr[--i].page);
483 cnt = 1 << mem->arr[i].order;
485 addr = base + PAGE_SIZE * --cnt;
486 if (last_addr && last_addr + PAGE_SIZE == addr)
490 if (len > max_seg_sz)
500 sg_set_page(sg, virt_to_page(addr), len, 0);
515 * Calculate transfer rate in bytes per second.
517 static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
527 while (ns > UINT_MAX) {
535 do_div(bytes, (uint32_t)ns);
541 * Save transfer results for future usage
543 static void mmc_test_save_transfer_result(struct mmc_test_card *test,
544 unsigned int count, unsigned int sectors, struct timespec ts,
545 unsigned int rate, unsigned int iops)
547 struct mmc_test_transfer_result *tr;
552 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
557 tr->sectors = sectors;
562 list_add_tail(&tr->link, &test->gr->tr_lst);
566 * Print the transfer rate.
568 static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
569 struct timespec *ts1, struct timespec *ts2)
571 unsigned int rate, iops, sectors = bytes >> 9;
574 ts = timespec_sub(*ts2, *ts1);
576 rate = mmc_test_rate(bytes, &ts);
577 iops = mmc_test_rate(100, &ts); /* I/O ops per sec x 100 */
579 pr_info("%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
580 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
581 mmc_hostname(test->card->host), sectors, sectors >> 1,
582 (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
583 (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024,
584 iops / 100, iops % 100);
586 mmc_test_save_transfer_result(test, 1, sectors, ts, rate, iops);
590 * Print the average transfer rate.
592 static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
593 unsigned int count, struct timespec *ts1,
594 struct timespec *ts2)
596 unsigned int rate, iops, sectors = bytes >> 9;
597 uint64_t tot = bytes * count;
600 ts = timespec_sub(*ts2, *ts1);
602 rate = mmc_test_rate(tot, &ts);
603 iops = mmc_test_rate(count * 100, &ts); /* I/O ops per sec x 100 */
605 pr_info("%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
606 "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
607 "%u.%02u IOPS, sg_len %d)\n",
608 mmc_hostname(test->card->host), count, sectors, count,
609 sectors >> 1, (sectors & 1 ? ".5" : ""),
610 (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
611 rate / 1000, rate / 1024, iops / 100, iops % 100,
614 mmc_test_save_transfer_result(test, count, sectors, ts, rate, iops);
618 * Return the card size in sectors.
620 static unsigned int mmc_test_capacity(struct mmc_card *card)
622 if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
623 return card->ext_csd.sectors;
625 return card->csd.capacity << (card->csd.read_blkbits - 9);
628 /*******************************************************************/
629 /* Test preparation and cleanup */
630 /*******************************************************************/
633 * Fill the first couple of sectors of the card with known data
634 * so that bad reads/writes can be detected
636 static int __mmc_test_prepare(struct mmc_test_card *test, int write)
640 ret = mmc_test_set_blksize(test, 512);
645 memset(test->buffer, 0xDF, 512);
647 for (i = 0; i < 512; i++)
651 for (i = 0; i < BUFFER_SIZE / 512; i++) {
652 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
660 static int mmc_test_prepare_write(struct mmc_test_card *test)
662 return __mmc_test_prepare(test, 1);
665 static int mmc_test_prepare_read(struct mmc_test_card *test)
667 return __mmc_test_prepare(test, 0);
670 static int mmc_test_cleanup(struct mmc_test_card *test)
674 ret = mmc_test_set_blksize(test, 512);
678 memset(test->buffer, 0, 512);
680 for (i = 0; i < BUFFER_SIZE / 512; i++) {
681 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
689 /*******************************************************************/
690 /* Test execution helpers */
691 /*******************************************************************/
694 * Modifies the mmc_request to perform the "short transfer" tests
696 static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
697 struct mmc_request *mrq, int write)
699 if (WARN_ON(!mrq || !mrq->cmd || !mrq->data))
702 if (mrq->data->blocks > 1) {
703 mrq->cmd->opcode = write ?
704 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
707 mrq->cmd->opcode = MMC_SEND_STATUS;
708 mrq->cmd->arg = test->card->rca << 16;
713 * Checks that a normal transfer didn't have any errors
715 static int mmc_test_check_result(struct mmc_test_card *test,
716 struct mmc_request *mrq)
720 if (WARN_ON(!mrq || !mrq->cmd || !mrq->data))
725 if (mrq->sbc && mrq->sbc->error)
726 ret = mrq->sbc->error;
727 if (!ret && mrq->cmd->error)
728 ret = mrq->cmd->error;
729 if (!ret && mrq->data->error)
730 ret = mrq->data->error;
731 if (!ret && mrq->stop && mrq->stop->error)
732 ret = mrq->stop->error;
733 if (!ret && mrq->data->bytes_xfered !=
734 mrq->data->blocks * mrq->data->blksz)
738 ret = RESULT_UNSUP_HOST;
743 static enum mmc_blk_status mmc_test_check_result_async(struct mmc_card *card,
744 struct mmc_async_req *areq)
746 struct mmc_test_async_req *test_async =
747 container_of(areq, struct mmc_test_async_req, areq);
750 mmc_test_wait_busy(test_async->test);
753 * FIXME: this would earlier just casts a regular error code,
754 * either of the kernel type -ERRORCODE or the local test framework
755 * RESULT_* errorcode, into an enum mmc_blk_status and return as
756 * result check. Instead, convert it to some reasonable type by just
757 * returning either MMC_BLK_SUCCESS or MMC_BLK_CMD_ERR.
758 * If possible, a reasonable error code should be returned.
760 ret = mmc_test_check_result(test_async->test, areq->mrq);
762 return MMC_BLK_CMD_ERR;
764 return MMC_BLK_SUCCESS;
768 * Checks that a "short transfer" behaved as expected
770 static int mmc_test_check_broken_result(struct mmc_test_card *test,
771 struct mmc_request *mrq)
775 if (WARN_ON(!mrq || !mrq->cmd || !mrq->data))
780 if (!ret && mrq->cmd->error)
781 ret = mrq->cmd->error;
782 if (!ret && mrq->data->error == 0)
784 if (!ret && mrq->data->error != -ETIMEDOUT)
785 ret = mrq->data->error;
786 if (!ret && mrq->stop && mrq->stop->error)
787 ret = mrq->stop->error;
788 if (mrq->data->blocks > 1) {
789 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
792 if (!ret && mrq->data->bytes_xfered > 0)
797 ret = RESULT_UNSUP_HOST;
803 * Tests nonblock transfer with certain parameters
805 static void mmc_test_nonblock_reset(struct mmc_request *mrq,
806 struct mmc_command *cmd,
807 struct mmc_command *stop,
808 struct mmc_data *data)
810 memset(mrq, 0, sizeof(struct mmc_request));
811 memset(cmd, 0, sizeof(struct mmc_command));
812 memset(data, 0, sizeof(struct mmc_data));
813 memset(stop, 0, sizeof(struct mmc_command));
819 static int mmc_test_nonblock_transfer(struct mmc_test_card *test,
820 struct scatterlist *sg, unsigned sg_len,
821 unsigned dev_addr, unsigned blocks,
822 unsigned blksz, int write, int count)
824 struct mmc_request mrq1;
825 struct mmc_command cmd1;
826 struct mmc_command stop1;
827 struct mmc_data data1;
829 struct mmc_request mrq2;
830 struct mmc_command cmd2;
831 struct mmc_command stop2;
832 struct mmc_data data2;
834 struct mmc_test_async_req test_areq[2];
835 struct mmc_async_req *done_areq;
836 struct mmc_async_req *cur_areq = &test_areq[0].areq;
837 struct mmc_async_req *other_areq = &test_areq[1].areq;
838 enum mmc_blk_status status;
842 test_areq[0].test = test;
843 test_areq[1].test = test;
845 mmc_test_nonblock_reset(&mrq1, &cmd1, &stop1, &data1);
846 mmc_test_nonblock_reset(&mrq2, &cmd2, &stop2, &data2);
848 cur_areq->mrq = &mrq1;
849 cur_areq->err_check = mmc_test_check_result_async;
850 other_areq->mrq = &mrq2;
851 other_areq->err_check = mmc_test_check_result_async;
853 for (i = 0; i < count; i++) {
854 mmc_test_prepare_mrq(test, cur_areq->mrq, sg, sg_len, dev_addr,
855 blocks, blksz, write);
856 done_areq = mmc_start_areq(test->card->host, cur_areq, &status);
858 if (status != MMC_BLK_SUCCESS || (!done_areq && i > 0)) {
864 if (done_areq->mrq == &mrq2)
865 mmc_test_nonblock_reset(&mrq2, &cmd2,
868 mmc_test_nonblock_reset(&mrq1, &cmd1,
871 swap(cur_areq, other_areq);
875 done_areq = mmc_start_areq(test->card->host, NULL, &status);
876 if (status != MMC_BLK_SUCCESS)
885 * Tests a basic transfer with certain parameters
887 static int mmc_test_simple_transfer(struct mmc_test_card *test,
888 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
889 unsigned blocks, unsigned blksz, int write)
891 struct mmc_request mrq = {};
892 struct mmc_command cmd = {};
893 struct mmc_command stop = {};
894 struct mmc_data data = {};
900 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
901 blocks, blksz, write);
903 mmc_wait_for_req(test->card->host, &mrq);
905 mmc_test_wait_busy(test);
907 return mmc_test_check_result(test, &mrq);
911 * Tests a transfer where the card will fail completely or partly
913 static int mmc_test_broken_transfer(struct mmc_test_card *test,
914 unsigned blocks, unsigned blksz, int write)
916 struct mmc_request mrq = {};
917 struct mmc_command cmd = {};
918 struct mmc_command stop = {};
919 struct mmc_data data = {};
921 struct scatterlist sg;
927 sg_init_one(&sg, test->buffer, blocks * blksz);
929 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
930 mmc_test_prepare_broken_mrq(test, &mrq, write);
932 mmc_wait_for_req(test->card->host, &mrq);
934 mmc_test_wait_busy(test);
936 return mmc_test_check_broken_result(test, &mrq);
940 * Does a complete transfer test where data is also validated
942 * Note: mmc_test_prepare() must have been done before this call
944 static int mmc_test_transfer(struct mmc_test_card *test,
945 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
946 unsigned blocks, unsigned blksz, int write)
952 for (i = 0; i < blocks * blksz; i++)
953 test->scratch[i] = i;
955 memset(test->scratch, 0, BUFFER_SIZE);
957 local_irq_save(flags);
958 sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
959 local_irq_restore(flags);
961 ret = mmc_test_set_blksize(test, blksz);
965 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
966 blocks, blksz, write);
973 ret = mmc_test_set_blksize(test, 512);
977 sectors = (blocks * blksz + 511) / 512;
978 if ((sectors * 512) == (blocks * blksz))
981 if ((sectors * 512) > BUFFER_SIZE)
984 memset(test->buffer, 0, sectors * 512);
986 for (i = 0; i < sectors; i++) {
987 ret = mmc_test_buffer_transfer(test,
988 test->buffer + i * 512,
989 dev_addr + i, 512, 0);
994 for (i = 0; i < blocks * blksz; i++) {
995 if (test->buffer[i] != (u8)i)
999 for (; i < sectors * 512; i++) {
1000 if (test->buffer[i] != 0xDF)
1004 local_irq_save(flags);
1005 sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
1006 local_irq_restore(flags);
1007 for (i = 0; i < blocks * blksz; i++) {
1008 if (test->scratch[i] != (u8)i)
1016 /*******************************************************************/
1018 /*******************************************************************/
1020 struct mmc_test_case {
1023 int (*prepare)(struct mmc_test_card *);
1024 int (*run)(struct mmc_test_card *);
1025 int (*cleanup)(struct mmc_test_card *);
1028 static int mmc_test_basic_write(struct mmc_test_card *test)
1031 struct scatterlist sg;
1033 ret = mmc_test_set_blksize(test, 512);
1037 sg_init_one(&sg, test->buffer, 512);
1039 return mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
1042 static int mmc_test_basic_read(struct mmc_test_card *test)
1045 struct scatterlist sg;
1047 ret = mmc_test_set_blksize(test, 512);
1051 sg_init_one(&sg, test->buffer, 512);
1053 return mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
1056 static int mmc_test_verify_write(struct mmc_test_card *test)
1058 struct scatterlist sg;
1060 sg_init_one(&sg, test->buffer, 512);
1062 return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1065 static int mmc_test_verify_read(struct mmc_test_card *test)
1067 struct scatterlist sg;
1069 sg_init_one(&sg, test->buffer, 512);
1071 return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1074 static int mmc_test_multi_write(struct mmc_test_card *test)
1077 struct scatterlist sg;
1079 if (test->card->host->max_blk_count == 1)
1080 return RESULT_UNSUP_HOST;
1082 size = PAGE_SIZE * 2;
1083 size = min(size, test->card->host->max_req_size);
1084 size = min(size, test->card->host->max_seg_size);
1085 size = min(size, test->card->host->max_blk_count * 512);
1088 return RESULT_UNSUP_HOST;
1090 sg_init_one(&sg, test->buffer, size);
1092 return mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 1);
1095 static int mmc_test_multi_read(struct mmc_test_card *test)
1098 struct scatterlist sg;
1100 if (test->card->host->max_blk_count == 1)
1101 return RESULT_UNSUP_HOST;
1103 size = PAGE_SIZE * 2;
1104 size = min(size, test->card->host->max_req_size);
1105 size = min(size, test->card->host->max_seg_size);
1106 size = min(size, test->card->host->max_blk_count * 512);
1109 return RESULT_UNSUP_HOST;
1111 sg_init_one(&sg, test->buffer, size);
1113 return mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 0);
1116 static int mmc_test_pow2_write(struct mmc_test_card *test)
1119 struct scatterlist sg;
1121 if (!test->card->csd.write_partial)
1122 return RESULT_UNSUP_CARD;
1124 for (i = 1; i < 512; i <<= 1) {
1125 sg_init_one(&sg, test->buffer, i);
1126 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1134 static int mmc_test_pow2_read(struct mmc_test_card *test)
1137 struct scatterlist sg;
1139 if (!test->card->csd.read_partial)
1140 return RESULT_UNSUP_CARD;
1142 for (i = 1; i < 512; i <<= 1) {
1143 sg_init_one(&sg, test->buffer, i);
1144 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1152 static int mmc_test_weird_write(struct mmc_test_card *test)
1155 struct scatterlist sg;
1157 if (!test->card->csd.write_partial)
1158 return RESULT_UNSUP_CARD;
1160 for (i = 3; i < 512; i += 7) {
1161 sg_init_one(&sg, test->buffer, i);
1162 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1170 static int mmc_test_weird_read(struct mmc_test_card *test)
1173 struct scatterlist sg;
1175 if (!test->card->csd.read_partial)
1176 return RESULT_UNSUP_CARD;
1178 for (i = 3; i < 512; i += 7) {
1179 sg_init_one(&sg, test->buffer, i);
1180 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1188 static int mmc_test_align_write(struct mmc_test_card *test)
1191 struct scatterlist sg;
1193 for (i = 1; i < TEST_ALIGN_END; i++) {
1194 sg_init_one(&sg, test->buffer + i, 512);
1195 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1203 static int mmc_test_align_read(struct mmc_test_card *test)
1206 struct scatterlist sg;
1208 for (i = 1; i < TEST_ALIGN_END; i++) {
1209 sg_init_one(&sg, test->buffer + i, 512);
1210 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1218 static int mmc_test_align_multi_write(struct mmc_test_card *test)
1222 struct scatterlist sg;
1224 if (test->card->host->max_blk_count == 1)
1225 return RESULT_UNSUP_HOST;
1227 size = PAGE_SIZE * 2;
1228 size = min(size, test->card->host->max_req_size);
1229 size = min(size, test->card->host->max_seg_size);
1230 size = min(size, test->card->host->max_blk_count * 512);
1233 return RESULT_UNSUP_HOST;
1235 for (i = 1; i < TEST_ALIGN_END; i++) {
1236 sg_init_one(&sg, test->buffer + i, size);
1237 ret = mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 1);
1245 static int mmc_test_align_multi_read(struct mmc_test_card *test)
1249 struct scatterlist sg;
1251 if (test->card->host->max_blk_count == 1)
1252 return RESULT_UNSUP_HOST;
1254 size = PAGE_SIZE * 2;
1255 size = min(size, test->card->host->max_req_size);
1256 size = min(size, test->card->host->max_seg_size);
1257 size = min(size, test->card->host->max_blk_count * 512);
1260 return RESULT_UNSUP_HOST;
1262 for (i = 1; i < TEST_ALIGN_END; i++) {
1263 sg_init_one(&sg, test->buffer + i, size);
1264 ret = mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 0);
1272 static int mmc_test_xfersize_write(struct mmc_test_card *test)
1276 ret = mmc_test_set_blksize(test, 512);
1280 return mmc_test_broken_transfer(test, 1, 512, 1);
1283 static int mmc_test_xfersize_read(struct mmc_test_card *test)
1287 ret = mmc_test_set_blksize(test, 512);
1291 return mmc_test_broken_transfer(test, 1, 512, 0);
1294 static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
1298 if (test->card->host->max_blk_count == 1)
1299 return RESULT_UNSUP_HOST;
1301 ret = mmc_test_set_blksize(test, 512);
1305 return mmc_test_broken_transfer(test, 2, 512, 1);
1308 static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
1312 if (test->card->host->max_blk_count == 1)
1313 return RESULT_UNSUP_HOST;
1315 ret = mmc_test_set_blksize(test, 512);
1319 return mmc_test_broken_transfer(test, 2, 512, 0);
1322 #ifdef CONFIG_HIGHMEM
1324 static int mmc_test_write_high(struct mmc_test_card *test)
1326 struct scatterlist sg;
1328 sg_init_table(&sg, 1);
1329 sg_set_page(&sg, test->highmem, 512, 0);
1331 return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1334 static int mmc_test_read_high(struct mmc_test_card *test)
1336 struct scatterlist sg;
1338 sg_init_table(&sg, 1);
1339 sg_set_page(&sg, test->highmem, 512, 0);
1341 return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1344 static int mmc_test_multi_write_high(struct mmc_test_card *test)
1347 struct scatterlist sg;
1349 if (test->card->host->max_blk_count == 1)
1350 return RESULT_UNSUP_HOST;
1352 size = PAGE_SIZE * 2;
1353 size = min(size, test->card->host->max_req_size);
1354 size = min(size, test->card->host->max_seg_size);
1355 size = min(size, test->card->host->max_blk_count * 512);
1358 return RESULT_UNSUP_HOST;
1360 sg_init_table(&sg, 1);
1361 sg_set_page(&sg, test->highmem, size, 0);
1363 return mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 1);
1366 static int mmc_test_multi_read_high(struct mmc_test_card *test)
1369 struct scatterlist sg;
1371 if (test->card->host->max_blk_count == 1)
1372 return RESULT_UNSUP_HOST;
1374 size = PAGE_SIZE * 2;
1375 size = min(size, test->card->host->max_req_size);
1376 size = min(size, test->card->host->max_seg_size);
1377 size = min(size, test->card->host->max_blk_count * 512);
1380 return RESULT_UNSUP_HOST;
1382 sg_init_table(&sg, 1);
1383 sg_set_page(&sg, test->highmem, size, 0);
1385 return mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 0);
1390 static int mmc_test_no_highmem(struct mmc_test_card *test)
1392 pr_info("%s: Highmem not configured - test skipped\n",
1393 mmc_hostname(test->card->host));
1397 #endif /* CONFIG_HIGHMEM */
1400 * Map sz bytes so that it can be transferred.
1402 static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
1403 int max_scatter, int min_sg_len)
1405 struct mmc_test_area *t = &test->area;
1408 t->blocks = sz >> 9;
1411 err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
1412 t->max_segs, t->max_seg_sz,
1415 err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
1416 t->max_seg_sz, &t->sg_len, min_sg_len);
1419 pr_info("%s: Failed to map sg list\n",
1420 mmc_hostname(test->card->host));
1425 * Transfer bytes mapped by mmc_test_area_map().
1427 static int mmc_test_area_transfer(struct mmc_test_card *test,
1428 unsigned int dev_addr, int write)
1430 struct mmc_test_area *t = &test->area;
1432 return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
1433 t->blocks, 512, write);
1437 * Map and transfer bytes for multiple transfers.
1439 static int mmc_test_area_io_seq(struct mmc_test_card *test, unsigned long sz,
1440 unsigned int dev_addr, int write,
1441 int max_scatter, int timed, int count,
1442 bool nonblock, int min_sg_len)
1444 struct timespec ts1, ts2;
1447 struct mmc_test_area *t = &test->area;
1450 * In the case of a maximally scattered transfer, the maximum transfer
1451 * size is further limited by using PAGE_SIZE segments.
1454 struct mmc_test_area *t = &test->area;
1455 unsigned long max_tfr;
1457 if (t->max_seg_sz >= PAGE_SIZE)
1458 max_tfr = t->max_segs * PAGE_SIZE;
1460 max_tfr = t->max_segs * t->max_seg_sz;
1465 ret = mmc_test_area_map(test, sz, max_scatter, min_sg_len);
1470 getnstimeofday(&ts1);
1472 ret = mmc_test_nonblock_transfer(test, t->sg, t->sg_len,
1473 dev_addr, t->blocks, 512, write, count);
1475 for (i = 0; i < count && ret == 0; i++) {
1476 ret = mmc_test_area_transfer(test, dev_addr, write);
1477 dev_addr += sz >> 9;
1484 getnstimeofday(&ts2);
1487 mmc_test_print_avg_rate(test, sz, count, &ts1, &ts2);
1492 static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
1493 unsigned int dev_addr, int write, int max_scatter,
1496 return mmc_test_area_io_seq(test, sz, dev_addr, write, max_scatter,
1497 timed, 1, false, 0);
1501 * Write the test area entirely.
1503 static int mmc_test_area_fill(struct mmc_test_card *test)
1505 struct mmc_test_area *t = &test->area;
1507 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, 1, 0, 0);
1511 * Erase the test area entirely.
1513 static int mmc_test_area_erase(struct mmc_test_card *test)
1515 struct mmc_test_area *t = &test->area;
1517 if (!mmc_can_erase(test->card))
1520 return mmc_erase(test->card, t->dev_addr, t->max_sz >> 9,
1525 * Cleanup struct mmc_test_area.
1527 static int mmc_test_area_cleanup(struct mmc_test_card *test)
1529 struct mmc_test_area *t = &test->area;
1532 mmc_test_free_mem(t->mem);
1538 * Initialize an area for testing large transfers. The test area is set to the
1539 * middle of the card because cards may have different characteristics at the
1540 * front (for FAT file system optimization). Optionally, the area is erased
1541 * (if the card supports it) which may improve write performance. Optionally,
1542 * the area is filled with data for subsequent read tests.
1544 static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
1546 struct mmc_test_area *t = &test->area;
1547 unsigned long min_sz = 64 * 1024, sz;
1550 ret = mmc_test_set_blksize(test, 512);
1554 /* Make the test area size about 4MiB */
1555 sz = (unsigned long)test->card->pref_erase << 9;
1557 while (t->max_sz < 4 * 1024 * 1024)
1559 while (t->max_sz > TEST_AREA_MAX_SIZE && t->max_sz > sz)
1562 t->max_segs = test->card->host->max_segs;
1563 t->max_seg_sz = test->card->host->max_seg_size;
1564 t->max_seg_sz -= t->max_seg_sz % 512;
1566 t->max_tfr = t->max_sz;
1567 if (t->max_tfr >> 9 > test->card->host->max_blk_count)
1568 t->max_tfr = test->card->host->max_blk_count << 9;
1569 if (t->max_tfr > test->card->host->max_req_size)
1570 t->max_tfr = test->card->host->max_req_size;
1571 if (t->max_tfr / t->max_seg_sz > t->max_segs)
1572 t->max_tfr = t->max_segs * t->max_seg_sz;
1575 * Try to allocate enough memory for a max. sized transfer. Less is OK
1576 * because the same memory can be mapped into the scatterlist more than
1577 * once. Also, take into account the limits imposed on scatterlist
1578 * segments by the host driver.
1580 t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
1585 t->sg = kmalloc_array(t->max_segs, sizeof(*t->sg), GFP_KERNEL);
1591 t->dev_addr = mmc_test_capacity(test->card) / 2;
1592 t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
1595 ret = mmc_test_area_erase(test);
1601 ret = mmc_test_area_fill(test);
1609 mmc_test_area_cleanup(test);
1614 * Prepare for large transfers. Do not erase the test area.
1616 static int mmc_test_area_prepare(struct mmc_test_card *test)
1618 return mmc_test_area_init(test, 0, 0);
1622 * Prepare for large transfers. Do erase the test area.
1624 static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
1626 return mmc_test_area_init(test, 1, 0);
1630 * Prepare for large transfers. Erase and fill the test area.
1632 static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
1634 return mmc_test_area_init(test, 1, 1);
1638 * Test best-case performance. Best-case performance is expected from
1639 * a single large transfer.
1641 * An additional option (max_scatter) allows the measurement of the same
1642 * transfer but with no contiguous pages in the scatter list. This tests
1643 * the efficiency of DMA to handle scattered pages.
1645 static int mmc_test_best_performance(struct mmc_test_card *test, int write,
1648 struct mmc_test_area *t = &test->area;
1650 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, write,
1655 * Best-case read performance.
1657 static int mmc_test_best_read_performance(struct mmc_test_card *test)
1659 return mmc_test_best_performance(test, 0, 0);
1663 * Best-case write performance.
1665 static int mmc_test_best_write_performance(struct mmc_test_card *test)
1667 return mmc_test_best_performance(test, 1, 0);
1671 * Best-case read performance into scattered pages.
1673 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
1675 return mmc_test_best_performance(test, 0, 1);
1679 * Best-case write performance from scattered pages.
1681 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
1683 return mmc_test_best_performance(test, 1, 1);
1687 * Single read performance by transfer size.
1689 static int mmc_test_profile_read_perf(struct mmc_test_card *test)
1691 struct mmc_test_area *t = &test->area;
1693 unsigned int dev_addr;
1696 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1697 dev_addr = t->dev_addr + (sz >> 9);
1698 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1703 dev_addr = t->dev_addr;
1704 return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1708 * Single write performance by transfer size.
1710 static int mmc_test_profile_write_perf(struct mmc_test_card *test)
1712 struct mmc_test_area *t = &test->area;
1714 unsigned int dev_addr;
1717 ret = mmc_test_area_erase(test);
1720 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1721 dev_addr = t->dev_addr + (sz >> 9);
1722 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1726 ret = mmc_test_area_erase(test);
1730 dev_addr = t->dev_addr;
1731 return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1735 * Single trim performance by transfer size.
1737 static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
1739 struct mmc_test_area *t = &test->area;
1741 unsigned int dev_addr;
1742 struct timespec ts1, ts2;
1745 if (!mmc_can_trim(test->card))
1746 return RESULT_UNSUP_CARD;
1748 if (!mmc_can_erase(test->card))
1749 return RESULT_UNSUP_HOST;
1751 for (sz = 512; sz < t->max_sz; sz <<= 1) {
1752 dev_addr = t->dev_addr + (sz >> 9);
1753 getnstimeofday(&ts1);
1754 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1757 getnstimeofday(&ts2);
1758 mmc_test_print_rate(test, sz, &ts1, &ts2);
1760 dev_addr = t->dev_addr;
1761 getnstimeofday(&ts1);
1762 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1765 getnstimeofday(&ts2);
1766 mmc_test_print_rate(test, sz, &ts1, &ts2);
1770 static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
1772 struct mmc_test_area *t = &test->area;
1773 unsigned int dev_addr, i, cnt;
1774 struct timespec ts1, ts2;
1777 cnt = t->max_sz / sz;
1778 dev_addr = t->dev_addr;
1779 getnstimeofday(&ts1);
1780 for (i = 0; i < cnt; i++) {
1781 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
1784 dev_addr += (sz >> 9);
1786 getnstimeofday(&ts2);
1787 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1792 * Consecutive read performance by transfer size.
1794 static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
1796 struct mmc_test_area *t = &test->area;
1800 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1801 ret = mmc_test_seq_read_perf(test, sz);
1806 return mmc_test_seq_read_perf(test, sz);
1809 static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
1811 struct mmc_test_area *t = &test->area;
1812 unsigned int dev_addr, i, cnt;
1813 struct timespec ts1, ts2;
1816 ret = mmc_test_area_erase(test);
1819 cnt = t->max_sz / sz;
1820 dev_addr = t->dev_addr;
1821 getnstimeofday(&ts1);
1822 for (i = 0; i < cnt; i++) {
1823 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
1826 dev_addr += (sz >> 9);
1828 getnstimeofday(&ts2);
1829 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1834 * Consecutive write performance by transfer size.
1836 static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
1838 struct mmc_test_area *t = &test->area;
1842 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1843 ret = mmc_test_seq_write_perf(test, sz);
1848 return mmc_test_seq_write_perf(test, sz);
1852 * Consecutive trim performance by transfer size.
1854 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
1856 struct mmc_test_area *t = &test->area;
1858 unsigned int dev_addr, i, cnt;
1859 struct timespec ts1, ts2;
1862 if (!mmc_can_trim(test->card))
1863 return RESULT_UNSUP_CARD;
1865 if (!mmc_can_erase(test->card))
1866 return RESULT_UNSUP_HOST;
1868 for (sz = 512; sz <= t->max_sz; sz <<= 1) {
1869 ret = mmc_test_area_erase(test);
1872 ret = mmc_test_area_fill(test);
1875 cnt = t->max_sz / sz;
1876 dev_addr = t->dev_addr;
1877 getnstimeofday(&ts1);
1878 for (i = 0; i < cnt; i++) {
1879 ret = mmc_erase(test->card, dev_addr, sz >> 9,
1883 dev_addr += (sz >> 9);
1885 getnstimeofday(&ts2);
1886 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1891 static unsigned int rnd_next = 1;
1893 static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt)
1897 rnd_next = rnd_next * 1103515245 + 12345;
1898 r = (rnd_next >> 16) & 0x7fff;
1899 return (r * rnd_cnt) >> 15;
1902 static int mmc_test_rnd_perf(struct mmc_test_card *test, int write, int print,
1905 unsigned int dev_addr, cnt, rnd_addr, range1, range2, last_ea = 0, ea;
1907 struct timespec ts1, ts2, ts;
1912 rnd_addr = mmc_test_capacity(test->card) / 4;
1913 range1 = rnd_addr / test->card->pref_erase;
1914 range2 = range1 / ssz;
1916 getnstimeofday(&ts1);
1917 for (cnt = 0; cnt < UINT_MAX; cnt++) {
1918 getnstimeofday(&ts2);
1919 ts = timespec_sub(ts2, ts1);
1920 if (ts.tv_sec >= 10)
1922 ea = mmc_test_rnd_num(range1);
1926 dev_addr = rnd_addr + test->card->pref_erase * ea +
1927 ssz * mmc_test_rnd_num(range2);
1928 ret = mmc_test_area_io(test, sz, dev_addr, write, 0, 0);
1933 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1937 static int mmc_test_random_perf(struct mmc_test_card *test, int write)
1939 struct mmc_test_area *t = &test->area;
1944 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1946 * When writing, try to get more consistent results by running
1947 * the test twice with exactly the same I/O but outputting the
1948 * results only for the 2nd run.
1952 ret = mmc_test_rnd_perf(test, write, 0, sz);
1957 ret = mmc_test_rnd_perf(test, write, 1, sz);
1964 ret = mmc_test_rnd_perf(test, write, 0, sz);
1969 return mmc_test_rnd_perf(test, write, 1, sz);
1973 * Random read performance by transfer size.
1975 static int mmc_test_random_read_perf(struct mmc_test_card *test)
1977 return mmc_test_random_perf(test, 0);
1981 * Random write performance by transfer size.
1983 static int mmc_test_random_write_perf(struct mmc_test_card *test)
1985 return mmc_test_random_perf(test, 1);
1988 static int mmc_test_seq_perf(struct mmc_test_card *test, int write,
1989 unsigned int tot_sz, int max_scatter)
1991 struct mmc_test_area *t = &test->area;
1992 unsigned int dev_addr, i, cnt, sz, ssz;
1993 struct timespec ts1, ts2;
1999 * In the case of a maximally scattered transfer, the maximum transfer
2000 * size is further limited by using PAGE_SIZE segments.
2003 unsigned long max_tfr;
2005 if (t->max_seg_sz >= PAGE_SIZE)
2006 max_tfr = t->max_segs * PAGE_SIZE;
2008 max_tfr = t->max_segs * t->max_seg_sz;
2014 dev_addr = mmc_test_capacity(test->card) / 4;
2015 if (tot_sz > dev_addr << 9)
2016 tot_sz = dev_addr << 9;
2018 dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
2020 getnstimeofday(&ts1);
2021 for (i = 0; i < cnt; i++) {
2022 ret = mmc_test_area_io(test, sz, dev_addr, write,
2028 getnstimeofday(&ts2);
2030 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
2035 static int mmc_test_large_seq_perf(struct mmc_test_card *test, int write)
2039 for (i = 0; i < 10; i++) {
2040 ret = mmc_test_seq_perf(test, write, 10 * 1024 * 1024, 1);
2044 for (i = 0; i < 5; i++) {
2045 ret = mmc_test_seq_perf(test, write, 100 * 1024 * 1024, 1);
2049 for (i = 0; i < 3; i++) {
2050 ret = mmc_test_seq_perf(test, write, 1000 * 1024 * 1024, 1);
2059 * Large sequential read performance.
2061 static int mmc_test_large_seq_read_perf(struct mmc_test_card *test)
2063 return mmc_test_large_seq_perf(test, 0);
2067 * Large sequential write performance.
2069 static int mmc_test_large_seq_write_perf(struct mmc_test_card *test)
2071 return mmc_test_large_seq_perf(test, 1);
2074 static int mmc_test_rw_multiple(struct mmc_test_card *test,
2075 struct mmc_test_multiple_rw *tdata,
2076 unsigned int reqsize, unsigned int size,
2079 unsigned int dev_addr;
2080 struct mmc_test_area *t = &test->area;
2083 /* Set up test area */
2084 if (size > mmc_test_capacity(test->card) / 2 * 512)
2085 size = mmc_test_capacity(test->card) / 2 * 512;
2086 if (reqsize > t->max_tfr)
2087 reqsize = t->max_tfr;
2088 dev_addr = mmc_test_capacity(test->card) / 4;
2089 if ((dev_addr & 0xffff0000))
2090 dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
2092 dev_addr &= 0xfffff800; /* Round to 1MiB boundary */
2099 /* prepare test area */
2100 if (mmc_can_erase(test->card) &&
2101 tdata->prepare & MMC_TEST_PREP_ERASE) {
2102 ret = mmc_erase(test->card, dev_addr,
2103 size / 512, MMC_SECURE_ERASE_ARG);
2105 ret = mmc_erase(test->card, dev_addr,
2106 size / 512, MMC_ERASE_ARG);
2112 ret = mmc_test_area_io_seq(test, reqsize, dev_addr,
2113 tdata->do_write, 0, 1, size / reqsize,
2114 tdata->do_nonblock_req, min_sg_len);
2120 pr_info("[%s] error\n", __func__);
2124 static int mmc_test_rw_multiple_size(struct mmc_test_card *test,
2125 struct mmc_test_multiple_rw *rw)
2129 void *pre_req = test->card->host->ops->pre_req;
2130 void *post_req = test->card->host->ops->post_req;
2132 if (rw->do_nonblock_req &&
2133 ((!pre_req && post_req) || (pre_req && !post_req))) {
2134 pr_info("error: only one of pre/post is defined\n");
2138 for (i = 0 ; i < rw->len && ret == 0; i++) {
2139 ret = mmc_test_rw_multiple(test, rw, rw->bs[i], rw->size, 0);
2146 static int mmc_test_rw_multiple_sg_len(struct mmc_test_card *test,
2147 struct mmc_test_multiple_rw *rw)
2152 for (i = 0 ; i < rw->len && ret == 0; i++) {
2153 ret = mmc_test_rw_multiple(test, rw, 512 * 1024, rw->size,
2162 * Multiple blocking write 4k to 4 MB chunks
2164 static int mmc_test_profile_mult_write_blocking_perf(struct mmc_test_card *test)
2166 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2167 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2168 struct mmc_test_multiple_rw test_data = {
2170 .size = TEST_AREA_MAX_SIZE,
2171 .len = ARRAY_SIZE(bs),
2173 .do_nonblock_req = false,
2174 .prepare = MMC_TEST_PREP_ERASE,
2177 return mmc_test_rw_multiple_size(test, &test_data);
2181 * Multiple non-blocking write 4k to 4 MB chunks
2183 static int mmc_test_profile_mult_write_nonblock_perf(struct mmc_test_card *test)
2185 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2186 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2187 struct mmc_test_multiple_rw test_data = {
2189 .size = TEST_AREA_MAX_SIZE,
2190 .len = ARRAY_SIZE(bs),
2192 .do_nonblock_req = true,
2193 .prepare = MMC_TEST_PREP_ERASE,
2196 return mmc_test_rw_multiple_size(test, &test_data);
2200 * Multiple blocking read 4k to 4 MB chunks
2202 static int mmc_test_profile_mult_read_blocking_perf(struct mmc_test_card *test)
2204 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2205 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2206 struct mmc_test_multiple_rw test_data = {
2208 .size = TEST_AREA_MAX_SIZE,
2209 .len = ARRAY_SIZE(bs),
2211 .do_nonblock_req = false,
2212 .prepare = MMC_TEST_PREP_NONE,
2215 return mmc_test_rw_multiple_size(test, &test_data);
2219 * Multiple non-blocking read 4k to 4 MB chunks
2221 static int mmc_test_profile_mult_read_nonblock_perf(struct mmc_test_card *test)
2223 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2224 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2225 struct mmc_test_multiple_rw test_data = {
2227 .size = TEST_AREA_MAX_SIZE,
2228 .len = ARRAY_SIZE(bs),
2230 .do_nonblock_req = true,
2231 .prepare = MMC_TEST_PREP_NONE,
2234 return mmc_test_rw_multiple_size(test, &test_data);
2238 * Multiple blocking write 1 to 512 sg elements
2240 static int mmc_test_profile_sglen_wr_blocking_perf(struct mmc_test_card *test)
2242 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2243 1 << 7, 1 << 8, 1 << 9};
2244 struct mmc_test_multiple_rw test_data = {
2246 .size = TEST_AREA_MAX_SIZE,
2247 .len = ARRAY_SIZE(sg_len),
2249 .do_nonblock_req = false,
2250 .prepare = MMC_TEST_PREP_ERASE,
2253 return mmc_test_rw_multiple_sg_len(test, &test_data);
2257 * Multiple non-blocking write 1 to 512 sg elements
2259 static int mmc_test_profile_sglen_wr_nonblock_perf(struct mmc_test_card *test)
2261 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2262 1 << 7, 1 << 8, 1 << 9};
2263 struct mmc_test_multiple_rw test_data = {
2265 .size = TEST_AREA_MAX_SIZE,
2266 .len = ARRAY_SIZE(sg_len),
2268 .do_nonblock_req = true,
2269 .prepare = MMC_TEST_PREP_ERASE,
2272 return mmc_test_rw_multiple_sg_len(test, &test_data);
2276 * Multiple blocking read 1 to 512 sg elements
2278 static int mmc_test_profile_sglen_r_blocking_perf(struct mmc_test_card *test)
2280 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2281 1 << 7, 1 << 8, 1 << 9};
2282 struct mmc_test_multiple_rw test_data = {
2284 .size = TEST_AREA_MAX_SIZE,
2285 .len = ARRAY_SIZE(sg_len),
2287 .do_nonblock_req = false,
2288 .prepare = MMC_TEST_PREP_NONE,
2291 return mmc_test_rw_multiple_sg_len(test, &test_data);
2295 * Multiple non-blocking read 1 to 512 sg elements
2297 static int mmc_test_profile_sglen_r_nonblock_perf(struct mmc_test_card *test)
2299 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2300 1 << 7, 1 << 8, 1 << 9};
2301 struct mmc_test_multiple_rw test_data = {
2303 .size = TEST_AREA_MAX_SIZE,
2304 .len = ARRAY_SIZE(sg_len),
2306 .do_nonblock_req = true,
2307 .prepare = MMC_TEST_PREP_NONE,
2310 return mmc_test_rw_multiple_sg_len(test, &test_data);
2314 * eMMC hardware reset.
2316 static int mmc_test_reset(struct mmc_test_card *test)
2318 struct mmc_card *card = test->card;
2319 struct mmc_host *host = card->host;
2322 err = mmc_hw_reset(host);
2325 else if (err == -EOPNOTSUPP)
2326 return RESULT_UNSUP_HOST;
2331 struct mmc_test_req {
2332 struct mmc_request mrq;
2333 struct mmc_command sbc;
2334 struct mmc_command cmd;
2335 struct mmc_command stop;
2336 struct mmc_command status;
2337 struct mmc_data data;
2340 static struct mmc_test_req *mmc_test_req_alloc(void)
2342 struct mmc_test_req *rq = kzalloc(sizeof(*rq), GFP_KERNEL);
2345 rq->mrq.cmd = &rq->cmd;
2346 rq->mrq.data = &rq->data;
2347 rq->mrq.stop = &rq->stop;
2353 static int mmc_test_send_status(struct mmc_test_card *test,
2354 struct mmc_command *cmd)
2356 memset(cmd, 0, sizeof(*cmd));
2358 cmd->opcode = MMC_SEND_STATUS;
2359 if (!mmc_host_is_spi(test->card->host))
2360 cmd->arg = test->card->rca << 16;
2361 cmd->flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2363 return mmc_wait_for_cmd(test->card->host, cmd, 0);
2366 static int mmc_test_ongoing_transfer(struct mmc_test_card *test,
2367 unsigned int dev_addr, int use_sbc,
2368 int repeat_cmd, int write, int use_areq)
2370 struct mmc_test_req *rq = mmc_test_req_alloc();
2371 struct mmc_host *host = test->card->host;
2372 struct mmc_test_area *t = &test->area;
2373 struct mmc_test_async_req test_areq = { .test = test };
2374 struct mmc_request *mrq;
2375 unsigned long timeout;
2376 bool expired = false;
2377 enum mmc_blk_status blkstat = MMC_BLK_SUCCESS;
2378 int ret = 0, cmd_ret;
2387 mrq->sbc = &rq->sbc;
2388 mrq->cap_cmd_during_tfr = true;
2390 test_areq.areq.mrq = mrq;
2391 test_areq.areq.err_check = mmc_test_check_result_async;
2393 mmc_test_prepare_mrq(test, mrq, t->sg, t->sg_len, dev_addr, t->blocks,
2396 if (use_sbc && t->blocks > 1 && !mrq->sbc) {
2397 ret = mmc_host_cmd23(host) ?
2403 /* Start ongoing data request */
2405 mmc_start_areq(host, &test_areq.areq, &blkstat);
2406 if (blkstat != MMC_BLK_SUCCESS) {
2411 mmc_wait_for_req(host, mrq);
2414 timeout = jiffies + msecs_to_jiffies(3000);
2418 /* Send status command while data transfer in progress */
2419 cmd_ret = mmc_test_send_status(test, &rq->status);
2423 status = rq->status.resp[0];
2424 if (status & R1_ERROR) {
2429 if (mmc_is_req_done(host, mrq))
2432 expired = time_after(jiffies, timeout);
2434 pr_info("%s: timeout waiting for Tran state status %#x\n",
2435 mmc_hostname(host), status);
2436 cmd_ret = -ETIMEDOUT;
2439 } while (repeat_cmd && R1_CURRENT_STATE(status) != R1_STATE_TRAN);
2441 /* Wait for data request to complete */
2443 mmc_start_areq(host, NULL, &blkstat);
2444 if (blkstat != MMC_BLK_SUCCESS)
2447 mmc_wait_for_req_done(test->card->host, mrq);
2451 * For cap_cmd_during_tfr request, upper layer must send stop if
2454 if (mrq->data->stop && (mrq->data->error || !mrq->sbc)) {
2456 mmc_wait_for_cmd(host, mrq->data->stop, 0);
2458 ret = mmc_wait_for_cmd(host, mrq->data->stop, 0);
2465 pr_info("%s: Send Status failed: status %#x, error %d\n",
2466 mmc_hostname(test->card->host), status, cmd_ret);
2469 ret = mmc_test_check_result(test, mrq);
2473 ret = mmc_test_wait_busy(test);
2477 if (repeat_cmd && (t->blocks + 1) << 9 > t->max_tfr)
2478 pr_info("%s: %d commands completed during transfer of %u blocks\n",
2479 mmc_hostname(test->card->host), count, t->blocks);
2489 static int __mmc_test_cmds_during_tfr(struct mmc_test_card *test,
2490 unsigned long sz, int use_sbc, int write,
2493 struct mmc_test_area *t = &test->area;
2496 if (!(test->card->host->caps & MMC_CAP_CMD_DURING_TFR))
2497 return RESULT_UNSUP_HOST;
2499 ret = mmc_test_area_map(test, sz, 0, 0);
2503 ret = mmc_test_ongoing_transfer(test, t->dev_addr, use_sbc, 0, write,
2508 return mmc_test_ongoing_transfer(test, t->dev_addr, use_sbc, 1, write,
2512 static int mmc_test_cmds_during_tfr(struct mmc_test_card *test, int use_sbc,
2513 int write, int use_areq)
2515 struct mmc_test_area *t = &test->area;
2519 for (sz = 512; sz <= t->max_tfr; sz += 512) {
2520 ret = __mmc_test_cmds_during_tfr(test, sz, use_sbc, write,
2529 * Commands during read - no Set Block Count (CMD23).
2531 static int mmc_test_cmds_during_read(struct mmc_test_card *test)
2533 return mmc_test_cmds_during_tfr(test, 0, 0, 0);
2537 * Commands during write - no Set Block Count (CMD23).
2539 static int mmc_test_cmds_during_write(struct mmc_test_card *test)
2541 return mmc_test_cmds_during_tfr(test, 0, 1, 0);
2545 * Commands during read - use Set Block Count (CMD23).
2547 static int mmc_test_cmds_during_read_cmd23(struct mmc_test_card *test)
2549 return mmc_test_cmds_during_tfr(test, 1, 0, 0);
2553 * Commands during write - use Set Block Count (CMD23).
2555 static int mmc_test_cmds_during_write_cmd23(struct mmc_test_card *test)
2557 return mmc_test_cmds_during_tfr(test, 1, 1, 0);
2561 * Commands during non-blocking read - use Set Block Count (CMD23).
2563 static int mmc_test_cmds_during_read_cmd23_nonblock(struct mmc_test_card *test)
2565 return mmc_test_cmds_during_tfr(test, 1, 0, 1);
2569 * Commands during non-blocking write - use Set Block Count (CMD23).
2571 static int mmc_test_cmds_during_write_cmd23_nonblock(struct mmc_test_card *test)
2573 return mmc_test_cmds_during_tfr(test, 1, 1, 1);
2576 static const struct mmc_test_case mmc_test_cases[] = {
2578 .name = "Basic write (no data verification)",
2579 .run = mmc_test_basic_write,
2583 .name = "Basic read (no data verification)",
2584 .run = mmc_test_basic_read,
2588 .name = "Basic write (with data verification)",
2589 .prepare = mmc_test_prepare_write,
2590 .run = mmc_test_verify_write,
2591 .cleanup = mmc_test_cleanup,
2595 .name = "Basic read (with data verification)",
2596 .prepare = mmc_test_prepare_read,
2597 .run = mmc_test_verify_read,
2598 .cleanup = mmc_test_cleanup,
2602 .name = "Multi-block write",
2603 .prepare = mmc_test_prepare_write,
2604 .run = mmc_test_multi_write,
2605 .cleanup = mmc_test_cleanup,
2609 .name = "Multi-block read",
2610 .prepare = mmc_test_prepare_read,
2611 .run = mmc_test_multi_read,
2612 .cleanup = mmc_test_cleanup,
2616 .name = "Power of two block writes",
2617 .prepare = mmc_test_prepare_write,
2618 .run = mmc_test_pow2_write,
2619 .cleanup = mmc_test_cleanup,
2623 .name = "Power of two block reads",
2624 .prepare = mmc_test_prepare_read,
2625 .run = mmc_test_pow2_read,
2626 .cleanup = mmc_test_cleanup,
2630 .name = "Weird sized block writes",
2631 .prepare = mmc_test_prepare_write,
2632 .run = mmc_test_weird_write,
2633 .cleanup = mmc_test_cleanup,
2637 .name = "Weird sized block reads",
2638 .prepare = mmc_test_prepare_read,
2639 .run = mmc_test_weird_read,
2640 .cleanup = mmc_test_cleanup,
2644 .name = "Badly aligned write",
2645 .prepare = mmc_test_prepare_write,
2646 .run = mmc_test_align_write,
2647 .cleanup = mmc_test_cleanup,
2651 .name = "Badly aligned read",
2652 .prepare = mmc_test_prepare_read,
2653 .run = mmc_test_align_read,
2654 .cleanup = mmc_test_cleanup,
2658 .name = "Badly aligned multi-block write",
2659 .prepare = mmc_test_prepare_write,
2660 .run = mmc_test_align_multi_write,
2661 .cleanup = mmc_test_cleanup,
2665 .name = "Badly aligned multi-block read",
2666 .prepare = mmc_test_prepare_read,
2667 .run = mmc_test_align_multi_read,
2668 .cleanup = mmc_test_cleanup,
2672 .name = "Correct xfer_size at write (start failure)",
2673 .run = mmc_test_xfersize_write,
2677 .name = "Correct xfer_size at read (start failure)",
2678 .run = mmc_test_xfersize_read,
2682 .name = "Correct xfer_size at write (midway failure)",
2683 .run = mmc_test_multi_xfersize_write,
2687 .name = "Correct xfer_size at read (midway failure)",
2688 .run = mmc_test_multi_xfersize_read,
2691 #ifdef CONFIG_HIGHMEM
2694 .name = "Highmem write",
2695 .prepare = mmc_test_prepare_write,
2696 .run = mmc_test_write_high,
2697 .cleanup = mmc_test_cleanup,
2701 .name = "Highmem read",
2702 .prepare = mmc_test_prepare_read,
2703 .run = mmc_test_read_high,
2704 .cleanup = mmc_test_cleanup,
2708 .name = "Multi-block highmem write",
2709 .prepare = mmc_test_prepare_write,
2710 .run = mmc_test_multi_write_high,
2711 .cleanup = mmc_test_cleanup,
2715 .name = "Multi-block highmem read",
2716 .prepare = mmc_test_prepare_read,
2717 .run = mmc_test_multi_read_high,
2718 .cleanup = mmc_test_cleanup,
2724 .name = "Highmem write",
2725 .run = mmc_test_no_highmem,
2729 .name = "Highmem read",
2730 .run = mmc_test_no_highmem,
2734 .name = "Multi-block highmem write",
2735 .run = mmc_test_no_highmem,
2739 .name = "Multi-block highmem read",
2740 .run = mmc_test_no_highmem,
2743 #endif /* CONFIG_HIGHMEM */
2746 .name = "Best-case read performance",
2747 .prepare = mmc_test_area_prepare_fill,
2748 .run = mmc_test_best_read_performance,
2749 .cleanup = mmc_test_area_cleanup,
2753 .name = "Best-case write performance",
2754 .prepare = mmc_test_area_prepare_erase,
2755 .run = mmc_test_best_write_performance,
2756 .cleanup = mmc_test_area_cleanup,
2760 .name = "Best-case read performance into scattered pages",
2761 .prepare = mmc_test_area_prepare_fill,
2762 .run = mmc_test_best_read_perf_max_scatter,
2763 .cleanup = mmc_test_area_cleanup,
2767 .name = "Best-case write performance from scattered pages",
2768 .prepare = mmc_test_area_prepare_erase,
2769 .run = mmc_test_best_write_perf_max_scatter,
2770 .cleanup = mmc_test_area_cleanup,
2774 .name = "Single read performance by transfer size",
2775 .prepare = mmc_test_area_prepare_fill,
2776 .run = mmc_test_profile_read_perf,
2777 .cleanup = mmc_test_area_cleanup,
2781 .name = "Single write performance by transfer size",
2782 .prepare = mmc_test_area_prepare,
2783 .run = mmc_test_profile_write_perf,
2784 .cleanup = mmc_test_area_cleanup,
2788 .name = "Single trim performance by transfer size",
2789 .prepare = mmc_test_area_prepare_fill,
2790 .run = mmc_test_profile_trim_perf,
2791 .cleanup = mmc_test_area_cleanup,
2795 .name = "Consecutive read performance by transfer size",
2796 .prepare = mmc_test_area_prepare_fill,
2797 .run = mmc_test_profile_seq_read_perf,
2798 .cleanup = mmc_test_area_cleanup,
2802 .name = "Consecutive write performance by transfer size",
2803 .prepare = mmc_test_area_prepare,
2804 .run = mmc_test_profile_seq_write_perf,
2805 .cleanup = mmc_test_area_cleanup,
2809 .name = "Consecutive trim performance by transfer size",
2810 .prepare = mmc_test_area_prepare,
2811 .run = mmc_test_profile_seq_trim_perf,
2812 .cleanup = mmc_test_area_cleanup,
2816 .name = "Random read performance by transfer size",
2817 .prepare = mmc_test_area_prepare,
2818 .run = mmc_test_random_read_perf,
2819 .cleanup = mmc_test_area_cleanup,
2823 .name = "Random write performance by transfer size",
2824 .prepare = mmc_test_area_prepare,
2825 .run = mmc_test_random_write_perf,
2826 .cleanup = mmc_test_area_cleanup,
2830 .name = "Large sequential read into scattered pages",
2831 .prepare = mmc_test_area_prepare,
2832 .run = mmc_test_large_seq_read_perf,
2833 .cleanup = mmc_test_area_cleanup,
2837 .name = "Large sequential write from scattered pages",
2838 .prepare = mmc_test_area_prepare,
2839 .run = mmc_test_large_seq_write_perf,
2840 .cleanup = mmc_test_area_cleanup,
2844 .name = "Write performance with blocking req 4k to 4MB",
2845 .prepare = mmc_test_area_prepare,
2846 .run = mmc_test_profile_mult_write_blocking_perf,
2847 .cleanup = mmc_test_area_cleanup,
2851 .name = "Write performance with non-blocking req 4k to 4MB",
2852 .prepare = mmc_test_area_prepare,
2853 .run = mmc_test_profile_mult_write_nonblock_perf,
2854 .cleanup = mmc_test_area_cleanup,
2858 .name = "Read performance with blocking req 4k to 4MB",
2859 .prepare = mmc_test_area_prepare,
2860 .run = mmc_test_profile_mult_read_blocking_perf,
2861 .cleanup = mmc_test_area_cleanup,
2865 .name = "Read performance with non-blocking req 4k to 4MB",
2866 .prepare = mmc_test_area_prepare,
2867 .run = mmc_test_profile_mult_read_nonblock_perf,
2868 .cleanup = mmc_test_area_cleanup,
2872 .name = "Write performance blocking req 1 to 512 sg elems",
2873 .prepare = mmc_test_area_prepare,
2874 .run = mmc_test_profile_sglen_wr_blocking_perf,
2875 .cleanup = mmc_test_area_cleanup,
2879 .name = "Write performance non-blocking req 1 to 512 sg elems",
2880 .prepare = mmc_test_area_prepare,
2881 .run = mmc_test_profile_sglen_wr_nonblock_perf,
2882 .cleanup = mmc_test_area_cleanup,
2886 .name = "Read performance blocking req 1 to 512 sg elems",
2887 .prepare = mmc_test_area_prepare,
2888 .run = mmc_test_profile_sglen_r_blocking_perf,
2889 .cleanup = mmc_test_area_cleanup,
2893 .name = "Read performance non-blocking req 1 to 512 sg elems",
2894 .prepare = mmc_test_area_prepare,
2895 .run = mmc_test_profile_sglen_r_nonblock_perf,
2896 .cleanup = mmc_test_area_cleanup,
2900 .name = "Reset test",
2901 .run = mmc_test_reset,
2905 .name = "Commands during read - no Set Block Count (CMD23)",
2906 .prepare = mmc_test_area_prepare,
2907 .run = mmc_test_cmds_during_read,
2908 .cleanup = mmc_test_area_cleanup,
2912 .name = "Commands during write - no Set Block Count (CMD23)",
2913 .prepare = mmc_test_area_prepare,
2914 .run = mmc_test_cmds_during_write,
2915 .cleanup = mmc_test_area_cleanup,
2919 .name = "Commands during read - use Set Block Count (CMD23)",
2920 .prepare = mmc_test_area_prepare,
2921 .run = mmc_test_cmds_during_read_cmd23,
2922 .cleanup = mmc_test_area_cleanup,
2926 .name = "Commands during write - use Set Block Count (CMD23)",
2927 .prepare = mmc_test_area_prepare,
2928 .run = mmc_test_cmds_during_write_cmd23,
2929 .cleanup = mmc_test_area_cleanup,
2933 .name = "Commands during non-blocking read - use Set Block Count (CMD23)",
2934 .prepare = mmc_test_area_prepare,
2935 .run = mmc_test_cmds_during_read_cmd23_nonblock,
2936 .cleanup = mmc_test_area_cleanup,
2940 .name = "Commands during non-blocking write - use Set Block Count (CMD23)",
2941 .prepare = mmc_test_area_prepare,
2942 .run = mmc_test_cmds_during_write_cmd23_nonblock,
2943 .cleanup = mmc_test_area_cleanup,
2947 static DEFINE_MUTEX(mmc_test_lock);
2949 static LIST_HEAD(mmc_test_result);
2951 static void mmc_test_run(struct mmc_test_card *test, int testcase)
2955 pr_info("%s: Starting tests of card %s...\n",
2956 mmc_hostname(test->card->host), mmc_card_id(test->card));
2958 mmc_claim_host(test->card->host);
2960 for (i = 0; i < ARRAY_SIZE(mmc_test_cases); i++) {
2961 struct mmc_test_general_result *gr;
2963 if (testcase && ((i + 1) != testcase))
2966 pr_info("%s: Test case %d. %s...\n",
2967 mmc_hostname(test->card->host), i + 1,
2968 mmc_test_cases[i].name);
2970 if (mmc_test_cases[i].prepare) {
2971 ret = mmc_test_cases[i].prepare(test);
2973 pr_info("%s: Result: Prepare stage failed! (%d)\n",
2974 mmc_hostname(test->card->host),
2980 gr = kzalloc(sizeof(*gr), GFP_KERNEL);
2982 INIT_LIST_HEAD(&gr->tr_lst);
2984 /* Assign data what we know already */
2985 gr->card = test->card;
2988 /* Append container to global one */
2989 list_add_tail(&gr->link, &mmc_test_result);
2992 * Save the pointer to created container in our private
2998 ret = mmc_test_cases[i].run(test);
3001 pr_info("%s: Result: OK\n",
3002 mmc_hostname(test->card->host));
3005 pr_info("%s: Result: FAILED\n",
3006 mmc_hostname(test->card->host));
3008 case RESULT_UNSUP_HOST:
3009 pr_info("%s: Result: UNSUPPORTED (by host)\n",
3010 mmc_hostname(test->card->host));
3012 case RESULT_UNSUP_CARD:
3013 pr_info("%s: Result: UNSUPPORTED (by card)\n",
3014 mmc_hostname(test->card->host));
3017 pr_info("%s: Result: ERROR (%d)\n",
3018 mmc_hostname(test->card->host), ret);
3021 /* Save the result */
3025 if (mmc_test_cases[i].cleanup) {
3026 ret = mmc_test_cases[i].cleanup(test);
3028 pr_info("%s: Warning: Cleanup stage failed! (%d)\n",
3029 mmc_hostname(test->card->host),
3035 mmc_release_host(test->card->host);
3037 pr_info("%s: Tests completed.\n",
3038 mmc_hostname(test->card->host));
3041 static void mmc_test_free_result(struct mmc_card *card)
3043 struct mmc_test_general_result *gr, *grs;
3045 mutex_lock(&mmc_test_lock);
3047 list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
3048 struct mmc_test_transfer_result *tr, *trs;
3050 if (card && gr->card != card)
3053 list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
3054 list_del(&tr->link);
3058 list_del(&gr->link);
3062 mutex_unlock(&mmc_test_lock);
3065 static LIST_HEAD(mmc_test_file_test);
3067 static int mtf_test_show(struct seq_file *sf, void *data)
3069 struct mmc_card *card = (struct mmc_card *)sf->private;
3070 struct mmc_test_general_result *gr;
3072 mutex_lock(&mmc_test_lock);
3074 list_for_each_entry(gr, &mmc_test_result, link) {
3075 struct mmc_test_transfer_result *tr;
3077 if (gr->card != card)
3080 seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
3082 list_for_each_entry(tr, &gr->tr_lst, link) {
3083 seq_printf(sf, "%u %d %lu.%09lu %u %u.%02u\n",
3084 tr->count, tr->sectors,
3085 (unsigned long)tr->ts.tv_sec,
3086 (unsigned long)tr->ts.tv_nsec,
3087 tr->rate, tr->iops / 100, tr->iops % 100);
3091 mutex_unlock(&mmc_test_lock);
3096 static int mtf_test_open(struct inode *inode, struct file *file)
3098 return single_open(file, mtf_test_show, inode->i_private);
3101 static ssize_t mtf_test_write(struct file *file, const char __user *buf,
3102 size_t count, loff_t *pos)
3104 struct seq_file *sf = (struct seq_file *)file->private_data;
3105 struct mmc_card *card = (struct mmc_card *)sf->private;
3106 struct mmc_test_card *test;
3110 ret = kstrtol_from_user(buf, count, 10, &testcase);
3114 test = kzalloc(sizeof(*test), GFP_KERNEL);
3119 * Remove all test cases associated with given card. Thus we have only
3120 * actual data of the last run.
3122 mmc_test_free_result(card);
3126 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
3127 #ifdef CONFIG_HIGHMEM
3128 test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
3131 #ifdef CONFIG_HIGHMEM
3132 if (test->buffer && test->highmem) {
3136 mutex_lock(&mmc_test_lock);
3137 mmc_test_run(test, testcase);
3138 mutex_unlock(&mmc_test_lock);
3141 #ifdef CONFIG_HIGHMEM
3142 __free_pages(test->highmem, BUFFER_ORDER);
3144 kfree(test->buffer);
3150 static const struct file_operations mmc_test_fops_test = {
3151 .open = mtf_test_open,
3153 .write = mtf_test_write,
3154 .llseek = seq_lseek,
3155 .release = single_release,
3158 static int mtf_testlist_show(struct seq_file *sf, void *data)
3162 mutex_lock(&mmc_test_lock);
3164 seq_puts(sf, "0:\tRun all tests\n");
3165 for (i = 0; i < ARRAY_SIZE(mmc_test_cases); i++)
3166 seq_printf(sf, "%d:\t%s\n", i + 1, mmc_test_cases[i].name);
3168 mutex_unlock(&mmc_test_lock);
3173 static int mtf_testlist_open(struct inode *inode, struct file *file)
3175 return single_open(file, mtf_testlist_show, inode->i_private);
3178 static const struct file_operations mmc_test_fops_testlist = {
3179 .open = mtf_testlist_open,
3181 .llseek = seq_lseek,
3182 .release = single_release,
3185 static void mmc_test_free_dbgfs_file(struct mmc_card *card)
3187 struct mmc_test_dbgfs_file *df, *dfs;
3189 mutex_lock(&mmc_test_lock);
3191 list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
3192 if (card && df->card != card)
3194 debugfs_remove(df->file);
3195 list_del(&df->link);
3199 mutex_unlock(&mmc_test_lock);
3202 static int __mmc_test_register_dbgfs_file(struct mmc_card *card,
3203 const char *name, umode_t mode, const struct file_operations *fops)
3205 struct dentry *file = NULL;
3206 struct mmc_test_dbgfs_file *df;
3208 if (card->debugfs_root)
3209 file = debugfs_create_file(name, mode, card->debugfs_root,
3212 if (IS_ERR_OR_NULL(file)) {
3214 "Can't create %s. Perhaps debugfs is disabled.\n",
3219 df = kmalloc(sizeof(*df), GFP_KERNEL);
3221 debugfs_remove(file);
3223 "Can't allocate memory for internal usage.\n");
3230 list_add(&df->link, &mmc_test_file_test);
3234 static int mmc_test_register_dbgfs_file(struct mmc_card *card)
3238 mutex_lock(&mmc_test_lock);
3240 ret = __mmc_test_register_dbgfs_file(card, "test", S_IWUSR | S_IRUGO,
3241 &mmc_test_fops_test);
3245 ret = __mmc_test_register_dbgfs_file(card, "testlist", S_IRUGO,
3246 &mmc_test_fops_testlist);
3251 mutex_unlock(&mmc_test_lock);
3256 static int mmc_test_probe(struct mmc_card *card)
3260 if (!mmc_card_mmc(card) && !mmc_card_sd(card))
3263 ret = mmc_test_register_dbgfs_file(card);
3267 dev_info(&card->dev, "Card claimed for testing.\n");
3272 static void mmc_test_remove(struct mmc_card *card)
3274 mmc_test_free_result(card);
3275 mmc_test_free_dbgfs_file(card);
3278 static void mmc_test_shutdown(struct mmc_card *card)
3282 static struct mmc_driver mmc_driver = {
3286 .probe = mmc_test_probe,
3287 .remove = mmc_test_remove,
3288 .shutdown = mmc_test_shutdown,
3291 static int __init mmc_test_init(void)
3293 return mmc_register_driver(&mmc_driver);
3296 static void __exit mmc_test_exit(void)
3298 /* Clear stalled data if card is still plugged */
3299 mmc_test_free_result(NULL);
3300 mmc_test_free_dbgfs_file(NULL);
3302 mmc_unregister_driver(&mmc_driver);
3305 module_init(mmc_test_init);
3306 module_exit(mmc_test_exit);
3308 MODULE_LICENSE("GPL");
3309 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
3310 MODULE_AUTHOR("Pierre Ossman");