1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2007-2008 Pierre Ossman
6 #include <linux/mmc/core.h>
7 #include <linux/mmc/card.h>
8 #include <linux/mmc/host.h>
9 #include <linux/mmc/mmc.h>
10 #include <linux/slab.h>
12 #include <linux/scatterlist.h>
13 #include <linux/list.h>
15 #include <linux/debugfs.h>
16 #include <linux/uaccess.h>
17 #include <linux/seq_file.h>
18 #include <linux/module.h>
28 #define RESULT_UNSUP_HOST 2
29 #define RESULT_UNSUP_CARD 3
31 #define BUFFER_ORDER 2
32 #define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
34 #define TEST_ALIGN_END 8
37 * Limit the test area size to the maximum MMC HC erase group size. Note that
38 * the maximum SD allocation unit size is just 4MiB.
40 #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
43 * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
44 * @page: first page in the allocation
45 * @order: order of the number of pages allocated
47 struct mmc_test_pages {
53 * struct mmc_test_mem - allocated memory.
54 * @arr: array of allocations
55 * @cnt: number of allocations
58 struct mmc_test_pages *arr;
63 * struct mmc_test_area - information for performance tests.
64 * @max_sz: test area size (in bytes)
65 * @dev_addr: address on card at which to do performance tests
66 * @max_tfr: maximum transfer size allowed by driver (in bytes)
67 * @max_segs: maximum segments allowed by driver in scatterlist @sg
68 * @max_seg_sz: maximum segment size allowed by driver
69 * @blocks: number of (512 byte) blocks currently mapped by @sg
70 * @sg_len: length of currently mapped scatterlist @sg
71 * @mem: allocated memory
73 * @sg_areq: scatterlist for non-blocking request
75 struct mmc_test_area {
77 unsigned int dev_addr;
79 unsigned int max_segs;
80 unsigned int max_seg_sz;
83 struct mmc_test_mem *mem;
84 struct scatterlist *sg;
85 struct scatterlist *sg_areq;
89 * struct mmc_test_transfer_result - transfer results for performance tests.
90 * @link: double-linked list
91 * @count: amount of group of sectors to check
92 * @sectors: amount of sectors to check in one group
93 * @ts: time values of transfer
94 * @rate: calculated transfer rate
95 * @iops: I/O operations per second (times 100)
97 struct mmc_test_transfer_result {
98 struct list_head link;
100 unsigned int sectors;
101 struct timespec64 ts;
107 * struct mmc_test_general_result - results for tests.
108 * @link: double-linked list
109 * @card: card under test
110 * @testcase: number of test case
111 * @result: result of test run
112 * @tr_lst: transfer measurements if any as mmc_test_transfer_result
114 struct mmc_test_general_result {
115 struct list_head link;
116 struct mmc_card *card;
119 struct list_head tr_lst;
123 * struct mmc_test_dbgfs_file - debugfs related file.
124 * @link: double-linked list
125 * @card: card under test
126 * @file: file created under debugfs
128 struct mmc_test_dbgfs_file {
129 struct list_head link;
130 struct mmc_card *card;
135 * struct mmc_test_card - test information.
136 * @card: card under test
137 * @scratch: transfer buffer
138 * @buffer: transfer buffer
139 * @highmem: buffer for highmem tests
140 * @area: information for performance tests
141 * @gr: pointer to results of current testcase
143 struct mmc_test_card {
144 struct mmc_card *card;
146 u8 scratch[BUFFER_SIZE];
148 #ifdef CONFIG_HIGHMEM
149 struct page *highmem;
151 struct mmc_test_area area;
152 struct mmc_test_general_result *gr;
155 enum mmc_test_prep_media {
156 MMC_TEST_PREP_NONE = 0,
157 MMC_TEST_PREP_WRITE_FULL = 1 << 0,
158 MMC_TEST_PREP_ERASE = 1 << 1,
161 struct mmc_test_multiple_rw {
162 unsigned int *sg_len;
167 bool do_nonblock_req;
168 enum mmc_test_prep_media prepare;
171 /*******************************************************************/
172 /* General helper functions */
173 /*******************************************************************/
176 * Configure correct block size in card
178 static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
180 return mmc_set_blocklen(test->card, size);
183 static bool mmc_test_card_cmd23(struct mmc_card *card)
185 return mmc_card_mmc(card) ||
186 (mmc_card_sd(card) && card->scr.cmds & SD_SCR_CMD23_SUPPORT);
189 static void mmc_test_prepare_sbc(struct mmc_test_card *test,
190 struct mmc_request *mrq, unsigned int blocks)
192 struct mmc_card *card = test->card;
194 if (!mrq->sbc || !mmc_host_cmd23(card->host) ||
195 !mmc_test_card_cmd23(card) || !mmc_op_multi(mrq->cmd->opcode) ||
196 (card->quirks & MMC_QUIRK_BLK_NO_CMD23)) {
201 mrq->sbc->opcode = MMC_SET_BLOCK_COUNT;
202 mrq->sbc->arg = blocks;
203 mrq->sbc->flags = MMC_RSP_R1 | MMC_CMD_AC;
207 * Fill in the mmc_request structure given a set of transfer parameters.
209 static void mmc_test_prepare_mrq(struct mmc_test_card *test,
210 struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
211 unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
213 if (WARN_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop))
217 mrq->cmd->opcode = write ?
218 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
220 mrq->cmd->opcode = write ?
221 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
224 mrq->cmd->arg = dev_addr;
225 if (!mmc_card_blockaddr(test->card))
228 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
233 mrq->stop->opcode = MMC_STOP_TRANSMISSION;
235 mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
238 mrq->data->blksz = blksz;
239 mrq->data->blocks = blocks;
240 mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
242 mrq->data->sg_len = sg_len;
244 mmc_test_prepare_sbc(test, mrq, blocks);
246 mmc_set_data_timeout(mrq->data, test->card);
249 static int mmc_test_busy(struct mmc_command *cmd)
251 return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
252 (R1_CURRENT_STATE(cmd->resp[0]) == R1_STATE_PRG);
256 * Wait for the card to finish the busy state
258 static int mmc_test_wait_busy(struct mmc_test_card *test)
261 struct mmc_command cmd = {};
265 memset(&cmd, 0, sizeof(struct mmc_command));
267 cmd.opcode = MMC_SEND_STATUS;
268 cmd.arg = test->card->rca << 16;
269 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
271 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
275 if (!busy && mmc_test_busy(&cmd)) {
277 if (test->card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
278 pr_info("%s: Warning: Host did not wait for busy state to end.\n",
279 mmc_hostname(test->card->host));
281 } while (mmc_test_busy(&cmd));
287 * Transfer a single sector of kernel addressable data
289 static int mmc_test_buffer_transfer(struct mmc_test_card *test,
290 u8 *buffer, unsigned addr, unsigned blksz, int write)
292 struct mmc_request mrq = {};
293 struct mmc_command cmd = {};
294 struct mmc_command stop = {};
295 struct mmc_data data = {};
297 struct scatterlist sg;
303 sg_init_one(&sg, buffer, blksz);
305 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
307 mmc_wait_for_req(test->card->host, &mrq);
314 return mmc_test_wait_busy(test);
317 static void mmc_test_free_mem(struct mmc_test_mem *mem)
322 __free_pages(mem->arr[mem->cnt].page,
323 mem->arr[mem->cnt].order);
329 * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
330 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
331 * not exceed a maximum number of segments and try not to make segments much
332 * bigger than maximum segment size.
334 static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
335 unsigned long max_sz,
336 unsigned int max_segs,
337 unsigned int max_seg_sz)
339 unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
340 unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
341 unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
342 unsigned long page_cnt = 0;
343 unsigned long limit = nr_free_buffer_pages() >> 4;
344 struct mmc_test_mem *mem;
346 if (max_page_cnt > limit)
347 max_page_cnt = limit;
348 if (min_page_cnt > max_page_cnt)
349 min_page_cnt = max_page_cnt;
351 if (max_seg_page_cnt > max_page_cnt)
352 max_seg_page_cnt = max_page_cnt;
354 if (max_segs > max_page_cnt)
355 max_segs = max_page_cnt;
357 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
361 mem->arr = kcalloc(max_segs, sizeof(*mem->arr), GFP_KERNEL);
365 while (max_page_cnt) {
368 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
371 order = get_order(max_seg_page_cnt << PAGE_SHIFT);
373 page = alloc_pages(flags, order);
379 if (page_cnt < min_page_cnt)
383 mem->arr[mem->cnt].page = page;
384 mem->arr[mem->cnt].order = order;
386 if (max_page_cnt <= (1UL << order))
388 max_page_cnt -= 1UL << order;
389 page_cnt += 1UL << order;
390 if (mem->cnt >= max_segs) {
391 if (page_cnt < min_page_cnt)
400 mmc_test_free_mem(mem);
405 * Map memory into a scatterlist. Optionally allow the same memory to be
406 * mapped more than once.
408 static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long size,
409 struct scatterlist *sglist, int repeat,
410 unsigned int max_segs, unsigned int max_seg_sz,
411 unsigned int *sg_len, int min_sg_len)
413 struct scatterlist *sg = NULL;
415 unsigned long sz = size;
417 sg_init_table(sglist, max_segs);
418 if (min_sg_len > max_segs)
419 min_sg_len = max_segs;
423 for (i = 0; i < mem->cnt; i++) {
424 unsigned long len = PAGE_SIZE << mem->arr[i].order;
426 if (min_sg_len && (size / min_sg_len < len))
427 len = ALIGN(size / min_sg_len, 512);
430 if (len > max_seg_sz)
438 sg_set_page(sg, mem->arr[i].page, len, 0);
444 } while (sz && repeat);
456 * Map memory into a scatterlist so that no pages are contiguous. Allow the
457 * same memory to be mapped more than once.
459 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
461 struct scatterlist *sglist,
462 unsigned int max_segs,
463 unsigned int max_seg_sz,
464 unsigned int *sg_len)
466 struct scatterlist *sg = NULL;
467 unsigned int i = mem->cnt, cnt;
469 void *base, *addr, *last_addr = NULL;
471 sg_init_table(sglist, max_segs);
475 base = page_address(mem->arr[--i].page);
476 cnt = 1 << mem->arr[i].order;
478 addr = base + PAGE_SIZE * --cnt;
479 if (last_addr && last_addr + PAGE_SIZE == addr)
483 if (len > max_seg_sz)
493 sg_set_page(sg, virt_to_page(addr), len, 0);
508 * Calculate transfer rate in bytes per second.
510 static unsigned int mmc_test_rate(uint64_t bytes, struct timespec64 *ts)
514 ns = timespec64_to_ns(ts);
517 while (ns > UINT_MAX) {
525 do_div(bytes, (uint32_t)ns);
531 * Save transfer results for future usage
533 static void mmc_test_save_transfer_result(struct mmc_test_card *test,
534 unsigned int count, unsigned int sectors, struct timespec64 ts,
535 unsigned int rate, unsigned int iops)
537 struct mmc_test_transfer_result *tr;
542 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
547 tr->sectors = sectors;
552 list_add_tail(&tr->link, &test->gr->tr_lst);
556 * Print the transfer rate.
558 static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
559 struct timespec64 *ts1, struct timespec64 *ts2)
561 unsigned int rate, iops, sectors = bytes >> 9;
562 struct timespec64 ts;
564 ts = timespec64_sub(*ts2, *ts1);
566 rate = mmc_test_rate(bytes, &ts);
567 iops = mmc_test_rate(100, &ts); /* I/O ops per sec x 100 */
569 pr_info("%s: Transfer of %u sectors (%u%s KiB) took %llu.%09u "
570 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
571 mmc_hostname(test->card->host), sectors, sectors >> 1,
572 (sectors & 1 ? ".5" : ""), (u64)ts.tv_sec,
573 (u32)ts.tv_nsec, rate / 1000, rate / 1024,
574 iops / 100, iops % 100);
576 mmc_test_save_transfer_result(test, 1, sectors, ts, rate, iops);
580 * Print the average transfer rate.
582 static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
583 unsigned int count, struct timespec64 *ts1,
584 struct timespec64 *ts2)
586 unsigned int rate, iops, sectors = bytes >> 9;
587 uint64_t tot = bytes * count;
588 struct timespec64 ts;
590 ts = timespec64_sub(*ts2, *ts1);
592 rate = mmc_test_rate(tot, &ts);
593 iops = mmc_test_rate(count * 100, &ts); /* I/O ops per sec x 100 */
595 pr_info("%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
596 "%llu.%09u seconds (%u kB/s, %u KiB/s, "
597 "%u.%02u IOPS, sg_len %d)\n",
598 mmc_hostname(test->card->host), count, sectors, count,
599 sectors >> 1, (sectors & 1 ? ".5" : ""),
600 (u64)ts.tv_sec, (u32)ts.tv_nsec,
601 rate / 1000, rate / 1024, iops / 100, iops % 100,
604 mmc_test_save_transfer_result(test, count, sectors, ts, rate, iops);
608 * Return the card size in sectors.
610 static unsigned int mmc_test_capacity(struct mmc_card *card)
612 if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
613 return card->ext_csd.sectors;
615 return card->csd.capacity << (card->csd.read_blkbits - 9);
618 /*******************************************************************/
619 /* Test preparation and cleanup */
620 /*******************************************************************/
623 * Fill the first couple of sectors of the card with known data
624 * so that bad reads/writes can be detected
626 static int __mmc_test_prepare(struct mmc_test_card *test, int write, int val)
630 ret = mmc_test_set_blksize(test, 512);
635 memset(test->buffer, val, 512);
637 for (i = 0; i < 512; i++)
641 for (i = 0; i < BUFFER_SIZE / 512; i++) {
642 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
650 static int mmc_test_prepare_write(struct mmc_test_card *test)
652 return __mmc_test_prepare(test, 1, 0xDF);
655 static int mmc_test_prepare_read(struct mmc_test_card *test)
657 return __mmc_test_prepare(test, 0, 0);
660 static int mmc_test_cleanup(struct mmc_test_card *test)
662 return __mmc_test_prepare(test, 1, 0);
665 /*******************************************************************/
666 /* Test execution helpers */
667 /*******************************************************************/
670 * Modifies the mmc_request to perform the "short transfer" tests
672 static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
673 struct mmc_request *mrq, int write)
675 if (WARN_ON(!mrq || !mrq->cmd || !mrq->data))
678 if (mrq->data->blocks > 1) {
679 mrq->cmd->opcode = write ?
680 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
683 mrq->cmd->opcode = MMC_SEND_STATUS;
684 mrq->cmd->arg = test->card->rca << 16;
689 * Checks that a normal transfer didn't have any errors
691 static int mmc_test_check_result(struct mmc_test_card *test,
692 struct mmc_request *mrq)
696 if (WARN_ON(!mrq || !mrq->cmd || !mrq->data))
701 if (mrq->sbc && mrq->sbc->error)
702 ret = mrq->sbc->error;
703 if (!ret && mrq->cmd->error)
704 ret = mrq->cmd->error;
705 if (!ret && mrq->data->error)
706 ret = mrq->data->error;
707 if (!ret && mrq->stop && mrq->stop->error)
708 ret = mrq->stop->error;
709 if (!ret && mrq->data->bytes_xfered !=
710 mrq->data->blocks * mrq->data->blksz)
714 ret = RESULT_UNSUP_HOST;
720 * Checks that a "short transfer" behaved as expected
722 static int mmc_test_check_broken_result(struct mmc_test_card *test,
723 struct mmc_request *mrq)
727 if (WARN_ON(!mrq || !mrq->cmd || !mrq->data))
732 if (!ret && mrq->cmd->error)
733 ret = mrq->cmd->error;
734 if (!ret && mrq->data->error == 0)
736 if (!ret && mrq->data->error != -ETIMEDOUT)
737 ret = mrq->data->error;
738 if (!ret && mrq->stop && mrq->stop->error)
739 ret = mrq->stop->error;
740 if (mrq->data->blocks > 1) {
741 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
744 if (!ret && mrq->data->bytes_xfered > 0)
749 ret = RESULT_UNSUP_HOST;
754 struct mmc_test_req {
755 struct mmc_request mrq;
756 struct mmc_command sbc;
757 struct mmc_command cmd;
758 struct mmc_command stop;
759 struct mmc_command status;
760 struct mmc_data data;
764 * Tests nonblock transfer with certain parameters
766 static void mmc_test_req_reset(struct mmc_test_req *rq)
768 memset(rq, 0, sizeof(struct mmc_test_req));
770 rq->mrq.cmd = &rq->cmd;
771 rq->mrq.data = &rq->data;
772 rq->mrq.stop = &rq->stop;
775 static struct mmc_test_req *mmc_test_req_alloc(void)
777 struct mmc_test_req *rq = kmalloc(sizeof(*rq), GFP_KERNEL);
780 mmc_test_req_reset(rq);
785 static void mmc_test_wait_done(struct mmc_request *mrq)
787 complete(&mrq->completion);
790 static int mmc_test_start_areq(struct mmc_test_card *test,
791 struct mmc_request *mrq,
792 struct mmc_request *prev_mrq)
794 struct mmc_host *host = test->card->host;
798 init_completion(&mrq->completion);
799 mrq->done = mmc_test_wait_done;
800 mmc_pre_req(host, mrq);
804 wait_for_completion(&prev_mrq->completion);
805 err = mmc_test_wait_busy(test);
807 err = mmc_test_check_result(test, prev_mrq);
811 err = mmc_start_request(host, mrq);
813 mmc_retune_release(host);
817 mmc_post_req(host, prev_mrq, 0);
820 mmc_post_req(host, mrq, err);
825 static int mmc_test_nonblock_transfer(struct mmc_test_card *test,
826 unsigned int dev_addr, int write,
829 struct mmc_test_req *rq1, *rq2;
830 struct mmc_request *mrq, *prev_mrq;
833 struct mmc_test_area *t = &test->area;
834 struct scatterlist *sg = t->sg;
835 struct scatterlist *sg_areq = t->sg_areq;
837 rq1 = mmc_test_req_alloc();
838 rq2 = mmc_test_req_alloc();
847 for (i = 0; i < count; i++) {
848 mmc_test_req_reset(container_of(mrq, struct mmc_test_req, mrq));
849 mmc_test_prepare_mrq(test, mrq, sg, t->sg_len, dev_addr,
850 t->blocks, 512, write);
851 ret = mmc_test_start_areq(test, mrq, prev_mrq);
856 prev_mrq = &rq2->mrq;
860 dev_addr += t->blocks;
863 ret = mmc_test_start_areq(test, NULL, prev_mrq);
871 * Tests a basic transfer with certain parameters
873 static int mmc_test_simple_transfer(struct mmc_test_card *test,
874 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
875 unsigned blocks, unsigned blksz, int write)
877 struct mmc_request mrq = {};
878 struct mmc_command cmd = {};
879 struct mmc_command stop = {};
880 struct mmc_data data = {};
886 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
887 blocks, blksz, write);
889 mmc_wait_for_req(test->card->host, &mrq);
891 mmc_test_wait_busy(test);
893 return mmc_test_check_result(test, &mrq);
897 * Tests a transfer where the card will fail completely or partly
899 static int mmc_test_broken_transfer(struct mmc_test_card *test,
900 unsigned blocks, unsigned blksz, int write)
902 struct mmc_request mrq = {};
903 struct mmc_command cmd = {};
904 struct mmc_command stop = {};
905 struct mmc_data data = {};
907 struct scatterlist sg;
913 sg_init_one(&sg, test->buffer, blocks * blksz);
915 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
916 mmc_test_prepare_broken_mrq(test, &mrq, write);
918 mmc_wait_for_req(test->card->host, &mrq);
920 mmc_test_wait_busy(test);
922 return mmc_test_check_broken_result(test, &mrq);
926 * Does a complete transfer test where data is also validated
928 * Note: mmc_test_prepare() must have been done before this call
930 static int mmc_test_transfer(struct mmc_test_card *test,
931 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
932 unsigned blocks, unsigned blksz, int write)
937 for (i = 0; i < blocks * blksz; i++)
938 test->scratch[i] = i;
940 memset(test->scratch, 0, BUFFER_SIZE);
942 sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
944 ret = mmc_test_set_blksize(test, blksz);
948 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
949 blocks, blksz, write);
956 ret = mmc_test_set_blksize(test, 512);
960 sectors = (blocks * blksz + 511) / 512;
961 if ((sectors * 512) == (blocks * blksz))
964 if ((sectors * 512) > BUFFER_SIZE)
967 memset(test->buffer, 0, sectors * 512);
969 for (i = 0; i < sectors; i++) {
970 ret = mmc_test_buffer_transfer(test,
971 test->buffer + i * 512,
972 dev_addr + i, 512, 0);
977 for (i = 0; i < blocks * blksz; i++) {
978 if (test->buffer[i] != (u8)i)
982 for (; i < sectors * 512; i++) {
983 if (test->buffer[i] != 0xDF)
987 sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
988 for (i = 0; i < blocks * blksz; i++) {
989 if (test->scratch[i] != (u8)i)
997 /*******************************************************************/
999 /*******************************************************************/
1001 struct mmc_test_case {
1004 int (*prepare)(struct mmc_test_card *);
1005 int (*run)(struct mmc_test_card *);
1006 int (*cleanup)(struct mmc_test_card *);
1009 static int mmc_test_basic_write(struct mmc_test_card *test)
1012 struct scatterlist sg;
1014 ret = mmc_test_set_blksize(test, 512);
1018 sg_init_one(&sg, test->buffer, 512);
1020 return mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
1023 static int mmc_test_basic_read(struct mmc_test_card *test)
1026 struct scatterlist sg;
1028 ret = mmc_test_set_blksize(test, 512);
1032 sg_init_one(&sg, test->buffer, 512);
1034 return mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
1037 static int mmc_test_verify_write(struct mmc_test_card *test)
1039 struct scatterlist sg;
1041 sg_init_one(&sg, test->buffer, 512);
1043 return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1046 static int mmc_test_verify_read(struct mmc_test_card *test)
1048 struct scatterlist sg;
1050 sg_init_one(&sg, test->buffer, 512);
1052 return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1055 static int mmc_test_multi_write(struct mmc_test_card *test)
1058 struct scatterlist sg;
1060 if (test->card->host->max_blk_count == 1)
1061 return RESULT_UNSUP_HOST;
1063 size = PAGE_SIZE * 2;
1064 size = min(size, test->card->host->max_req_size);
1065 size = min(size, test->card->host->max_seg_size);
1066 size = min(size, test->card->host->max_blk_count * 512);
1069 return RESULT_UNSUP_HOST;
1071 sg_init_one(&sg, test->buffer, size);
1073 return mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 1);
1076 static int mmc_test_multi_read(struct mmc_test_card *test)
1079 struct scatterlist sg;
1081 if (test->card->host->max_blk_count == 1)
1082 return RESULT_UNSUP_HOST;
1084 size = PAGE_SIZE * 2;
1085 size = min(size, test->card->host->max_req_size);
1086 size = min(size, test->card->host->max_seg_size);
1087 size = min(size, test->card->host->max_blk_count * 512);
1090 return RESULT_UNSUP_HOST;
1092 sg_init_one(&sg, test->buffer, size);
1094 return mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 0);
1097 static int mmc_test_pow2_write(struct mmc_test_card *test)
1100 struct scatterlist sg;
1102 if (!test->card->csd.write_partial)
1103 return RESULT_UNSUP_CARD;
1105 for (i = 1; i < 512; i <<= 1) {
1106 sg_init_one(&sg, test->buffer, i);
1107 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1115 static int mmc_test_pow2_read(struct mmc_test_card *test)
1118 struct scatterlist sg;
1120 if (!test->card->csd.read_partial)
1121 return RESULT_UNSUP_CARD;
1123 for (i = 1; i < 512; i <<= 1) {
1124 sg_init_one(&sg, test->buffer, i);
1125 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1133 static int mmc_test_weird_write(struct mmc_test_card *test)
1136 struct scatterlist sg;
1138 if (!test->card->csd.write_partial)
1139 return RESULT_UNSUP_CARD;
1141 for (i = 3; i < 512; i += 7) {
1142 sg_init_one(&sg, test->buffer, i);
1143 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1151 static int mmc_test_weird_read(struct mmc_test_card *test)
1154 struct scatterlist sg;
1156 if (!test->card->csd.read_partial)
1157 return RESULT_UNSUP_CARD;
1159 for (i = 3; i < 512; i += 7) {
1160 sg_init_one(&sg, test->buffer, i);
1161 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1169 static int mmc_test_align_write(struct mmc_test_card *test)
1172 struct scatterlist sg;
1174 for (i = 1; i < TEST_ALIGN_END; i++) {
1175 sg_init_one(&sg, test->buffer + i, 512);
1176 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1184 static int mmc_test_align_read(struct mmc_test_card *test)
1187 struct scatterlist sg;
1189 for (i = 1; i < TEST_ALIGN_END; i++) {
1190 sg_init_one(&sg, test->buffer + i, 512);
1191 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1199 static int mmc_test_align_multi_write(struct mmc_test_card *test)
1203 struct scatterlist sg;
1205 if (test->card->host->max_blk_count == 1)
1206 return RESULT_UNSUP_HOST;
1208 size = PAGE_SIZE * 2;
1209 size = min(size, test->card->host->max_req_size);
1210 size = min(size, test->card->host->max_seg_size);
1211 size = min(size, test->card->host->max_blk_count * 512);
1214 return RESULT_UNSUP_HOST;
1216 for (i = 1; i < TEST_ALIGN_END; i++) {
1217 sg_init_one(&sg, test->buffer + i, size);
1218 ret = mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 1);
1226 static int mmc_test_align_multi_read(struct mmc_test_card *test)
1230 struct scatterlist sg;
1232 if (test->card->host->max_blk_count == 1)
1233 return RESULT_UNSUP_HOST;
1235 size = PAGE_SIZE * 2;
1236 size = min(size, test->card->host->max_req_size);
1237 size = min(size, test->card->host->max_seg_size);
1238 size = min(size, test->card->host->max_blk_count * 512);
1241 return RESULT_UNSUP_HOST;
1243 for (i = 1; i < TEST_ALIGN_END; i++) {
1244 sg_init_one(&sg, test->buffer + i, size);
1245 ret = mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 0);
1253 static int mmc_test_xfersize_write(struct mmc_test_card *test)
1257 ret = mmc_test_set_blksize(test, 512);
1261 return mmc_test_broken_transfer(test, 1, 512, 1);
1264 static int mmc_test_xfersize_read(struct mmc_test_card *test)
1268 ret = mmc_test_set_blksize(test, 512);
1272 return mmc_test_broken_transfer(test, 1, 512, 0);
1275 static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
1279 if (test->card->host->max_blk_count == 1)
1280 return RESULT_UNSUP_HOST;
1282 ret = mmc_test_set_blksize(test, 512);
1286 return mmc_test_broken_transfer(test, 2, 512, 1);
1289 static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
1293 if (test->card->host->max_blk_count == 1)
1294 return RESULT_UNSUP_HOST;
1296 ret = mmc_test_set_blksize(test, 512);
1300 return mmc_test_broken_transfer(test, 2, 512, 0);
1303 #ifdef CONFIG_HIGHMEM
1305 static int mmc_test_write_high(struct mmc_test_card *test)
1307 struct scatterlist sg;
1309 sg_init_table(&sg, 1);
1310 sg_set_page(&sg, test->highmem, 512, 0);
1312 return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1315 static int mmc_test_read_high(struct mmc_test_card *test)
1317 struct scatterlist sg;
1319 sg_init_table(&sg, 1);
1320 sg_set_page(&sg, test->highmem, 512, 0);
1322 return mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1325 static int mmc_test_multi_write_high(struct mmc_test_card *test)
1328 struct scatterlist sg;
1330 if (test->card->host->max_blk_count == 1)
1331 return RESULT_UNSUP_HOST;
1333 size = PAGE_SIZE * 2;
1334 size = min(size, test->card->host->max_req_size);
1335 size = min(size, test->card->host->max_seg_size);
1336 size = min(size, test->card->host->max_blk_count * 512);
1339 return RESULT_UNSUP_HOST;
1341 sg_init_table(&sg, 1);
1342 sg_set_page(&sg, test->highmem, size, 0);
1344 return mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 1);
1347 static int mmc_test_multi_read_high(struct mmc_test_card *test)
1350 struct scatterlist sg;
1352 if (test->card->host->max_blk_count == 1)
1353 return RESULT_UNSUP_HOST;
1355 size = PAGE_SIZE * 2;
1356 size = min(size, test->card->host->max_req_size);
1357 size = min(size, test->card->host->max_seg_size);
1358 size = min(size, test->card->host->max_blk_count * 512);
1361 return RESULT_UNSUP_HOST;
1363 sg_init_table(&sg, 1);
1364 sg_set_page(&sg, test->highmem, size, 0);
1366 return mmc_test_transfer(test, &sg, 1, 0, size / 512, 512, 0);
1371 static int mmc_test_no_highmem(struct mmc_test_card *test)
1373 pr_info("%s: Highmem not configured - test skipped\n",
1374 mmc_hostname(test->card->host));
1378 #endif /* CONFIG_HIGHMEM */
1381 * Map sz bytes so that it can be transferred.
1383 static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
1384 int max_scatter, int min_sg_len, bool nonblock)
1386 struct mmc_test_area *t = &test->area;
1388 unsigned int sg_len = 0;
1390 t->blocks = sz >> 9;
1393 err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
1394 t->max_segs, t->max_seg_sz,
1397 err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
1398 t->max_seg_sz, &t->sg_len, min_sg_len);
1401 if (err || !nonblock)
1405 err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg_areq,
1406 t->max_segs, t->max_seg_sz,
1409 err = mmc_test_map_sg(t->mem, sz, t->sg_areq, 1, t->max_segs,
1410 t->max_seg_sz, &sg_len, min_sg_len);
1412 if (!err && sg_len != t->sg_len)
1417 pr_info("%s: Failed to map sg list\n",
1418 mmc_hostname(test->card->host));
1423 * Transfer bytes mapped by mmc_test_area_map().
1425 static int mmc_test_area_transfer(struct mmc_test_card *test,
1426 unsigned int dev_addr, int write)
1428 struct mmc_test_area *t = &test->area;
1430 return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
1431 t->blocks, 512, write);
1435 * Map and transfer bytes for multiple transfers.
1437 static int mmc_test_area_io_seq(struct mmc_test_card *test, unsigned long sz,
1438 unsigned int dev_addr, int write,
1439 int max_scatter, int timed, int count,
1440 bool nonblock, int min_sg_len)
1442 struct timespec64 ts1, ts2;
1447 * In the case of a maximally scattered transfer, the maximum transfer
1448 * size is further limited by using PAGE_SIZE segments.
1451 struct mmc_test_area *t = &test->area;
1452 unsigned long max_tfr;
1454 if (t->max_seg_sz >= PAGE_SIZE)
1455 max_tfr = t->max_segs * PAGE_SIZE;
1457 max_tfr = t->max_segs * t->max_seg_sz;
1462 ret = mmc_test_area_map(test, sz, max_scatter, min_sg_len, nonblock);
1467 ktime_get_ts64(&ts1);
1469 ret = mmc_test_nonblock_transfer(test, dev_addr, write, count);
1471 for (i = 0; i < count && ret == 0; i++) {
1472 ret = mmc_test_area_transfer(test, dev_addr, write);
1473 dev_addr += sz >> 9;
1480 ktime_get_ts64(&ts2);
1483 mmc_test_print_avg_rate(test, sz, count, &ts1, &ts2);
1488 static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
1489 unsigned int dev_addr, int write, int max_scatter,
1492 return mmc_test_area_io_seq(test, sz, dev_addr, write, max_scatter,
1493 timed, 1, false, 0);
1497 * Write the test area entirely.
1499 static int mmc_test_area_fill(struct mmc_test_card *test)
1501 struct mmc_test_area *t = &test->area;
1503 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, 1, 0, 0);
1507 * Erase the test area entirely.
1509 static int mmc_test_area_erase(struct mmc_test_card *test)
1511 struct mmc_test_area *t = &test->area;
1513 if (!mmc_can_erase(test->card))
1516 return mmc_erase(test->card, t->dev_addr, t->max_sz >> 9,
1521 * Cleanup struct mmc_test_area.
1523 static int mmc_test_area_cleanup(struct mmc_test_card *test)
1525 struct mmc_test_area *t = &test->area;
1529 mmc_test_free_mem(t->mem);
1535 * Initialize an area for testing large transfers. The test area is set to the
1536 * middle of the card because cards may have different characteristics at the
1537 * front (for FAT file system optimization). Optionally, the area is erased
1538 * (if the card supports it) which may improve write performance. Optionally,
1539 * the area is filled with data for subsequent read tests.
1541 static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
1543 struct mmc_test_area *t = &test->area;
1544 unsigned long min_sz = 64 * 1024, sz;
1547 ret = mmc_test_set_blksize(test, 512);
1551 /* Make the test area size about 4MiB */
1552 sz = (unsigned long)test->card->pref_erase << 9;
1554 while (t->max_sz < 4 * 1024 * 1024)
1556 while (t->max_sz > TEST_AREA_MAX_SIZE && t->max_sz > sz)
1559 t->max_segs = test->card->host->max_segs;
1560 t->max_seg_sz = test->card->host->max_seg_size;
1561 t->max_seg_sz -= t->max_seg_sz % 512;
1563 t->max_tfr = t->max_sz;
1564 if (t->max_tfr >> 9 > test->card->host->max_blk_count)
1565 t->max_tfr = test->card->host->max_blk_count << 9;
1566 if (t->max_tfr > test->card->host->max_req_size)
1567 t->max_tfr = test->card->host->max_req_size;
1568 if (t->max_tfr / t->max_seg_sz > t->max_segs)
1569 t->max_tfr = t->max_segs * t->max_seg_sz;
1572 * Try to allocate enough memory for a max. sized transfer. Less is OK
1573 * because the same memory can be mapped into the scatterlist more than
1574 * once. Also, take into account the limits imposed on scatterlist
1575 * segments by the host driver.
1577 t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
1582 t->sg = kmalloc_array(t->max_segs, sizeof(*t->sg), GFP_KERNEL);
1588 t->sg_areq = kmalloc_array(t->max_segs, sizeof(*t->sg_areq),
1595 t->dev_addr = mmc_test_capacity(test->card) / 2;
1596 t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
1599 ret = mmc_test_area_erase(test);
1605 ret = mmc_test_area_fill(test);
1613 mmc_test_area_cleanup(test);
1618 * Prepare for large transfers. Do not erase the test area.
1620 static int mmc_test_area_prepare(struct mmc_test_card *test)
1622 return mmc_test_area_init(test, 0, 0);
1626 * Prepare for large transfers. Do erase the test area.
1628 static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
1630 return mmc_test_area_init(test, 1, 0);
1634 * Prepare for large transfers. Erase and fill the test area.
1636 static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
1638 return mmc_test_area_init(test, 1, 1);
1642 * Test best-case performance. Best-case performance is expected from
1643 * a single large transfer.
1645 * An additional option (max_scatter) allows the measurement of the same
1646 * transfer but with no contiguous pages in the scatter list. This tests
1647 * the efficiency of DMA to handle scattered pages.
1649 static int mmc_test_best_performance(struct mmc_test_card *test, int write,
1652 struct mmc_test_area *t = &test->area;
1654 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, write,
1659 * Best-case read performance.
1661 static int mmc_test_best_read_performance(struct mmc_test_card *test)
1663 return mmc_test_best_performance(test, 0, 0);
1667 * Best-case write performance.
1669 static int mmc_test_best_write_performance(struct mmc_test_card *test)
1671 return mmc_test_best_performance(test, 1, 0);
1675 * Best-case read performance into scattered pages.
1677 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
1679 return mmc_test_best_performance(test, 0, 1);
1683 * Best-case write performance from scattered pages.
1685 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
1687 return mmc_test_best_performance(test, 1, 1);
1691 * Single read performance by transfer size.
1693 static int mmc_test_profile_read_perf(struct mmc_test_card *test)
1695 struct mmc_test_area *t = &test->area;
1697 unsigned int dev_addr;
1700 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1701 dev_addr = t->dev_addr + (sz >> 9);
1702 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1707 dev_addr = t->dev_addr;
1708 return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1712 * Single write performance by transfer size.
1714 static int mmc_test_profile_write_perf(struct mmc_test_card *test)
1716 struct mmc_test_area *t = &test->area;
1718 unsigned int dev_addr;
1721 ret = mmc_test_area_erase(test);
1724 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1725 dev_addr = t->dev_addr + (sz >> 9);
1726 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1730 ret = mmc_test_area_erase(test);
1734 dev_addr = t->dev_addr;
1735 return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1739 * Single trim performance by transfer size.
1741 static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
1743 struct mmc_test_area *t = &test->area;
1745 unsigned int dev_addr;
1746 struct timespec64 ts1, ts2;
1749 if (!mmc_can_trim(test->card))
1750 return RESULT_UNSUP_CARD;
1752 if (!mmc_can_erase(test->card))
1753 return RESULT_UNSUP_HOST;
1755 for (sz = 512; sz < t->max_sz; sz <<= 1) {
1756 dev_addr = t->dev_addr + (sz >> 9);
1757 ktime_get_ts64(&ts1);
1758 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1761 ktime_get_ts64(&ts2);
1762 mmc_test_print_rate(test, sz, &ts1, &ts2);
1764 dev_addr = t->dev_addr;
1765 ktime_get_ts64(&ts1);
1766 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1769 ktime_get_ts64(&ts2);
1770 mmc_test_print_rate(test, sz, &ts1, &ts2);
1774 static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
1776 struct mmc_test_area *t = &test->area;
1777 unsigned int dev_addr, i, cnt;
1778 struct timespec64 ts1, ts2;
1781 cnt = t->max_sz / sz;
1782 dev_addr = t->dev_addr;
1783 ktime_get_ts64(&ts1);
1784 for (i = 0; i < cnt; i++) {
1785 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
1788 dev_addr += (sz >> 9);
1790 ktime_get_ts64(&ts2);
1791 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1796 * Consecutive read performance by transfer size.
1798 static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
1800 struct mmc_test_area *t = &test->area;
1804 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1805 ret = mmc_test_seq_read_perf(test, sz);
1810 return mmc_test_seq_read_perf(test, sz);
1813 static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
1815 struct mmc_test_area *t = &test->area;
1816 unsigned int dev_addr, i, cnt;
1817 struct timespec64 ts1, ts2;
1820 ret = mmc_test_area_erase(test);
1823 cnt = t->max_sz / sz;
1824 dev_addr = t->dev_addr;
1825 ktime_get_ts64(&ts1);
1826 for (i = 0; i < cnt; i++) {
1827 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
1830 dev_addr += (sz >> 9);
1832 ktime_get_ts64(&ts2);
1833 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1838 * Consecutive write performance by transfer size.
1840 static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
1842 struct mmc_test_area *t = &test->area;
1846 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1847 ret = mmc_test_seq_write_perf(test, sz);
1852 return mmc_test_seq_write_perf(test, sz);
1856 * Consecutive trim performance by transfer size.
1858 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
1860 struct mmc_test_area *t = &test->area;
1862 unsigned int dev_addr, i, cnt;
1863 struct timespec64 ts1, ts2;
1866 if (!mmc_can_trim(test->card))
1867 return RESULT_UNSUP_CARD;
1869 if (!mmc_can_erase(test->card))
1870 return RESULT_UNSUP_HOST;
1872 for (sz = 512; sz <= t->max_sz; sz <<= 1) {
1873 ret = mmc_test_area_erase(test);
1876 ret = mmc_test_area_fill(test);
1879 cnt = t->max_sz / sz;
1880 dev_addr = t->dev_addr;
1881 ktime_get_ts64(&ts1);
1882 for (i = 0; i < cnt; i++) {
1883 ret = mmc_erase(test->card, dev_addr, sz >> 9,
1887 dev_addr += (sz >> 9);
1889 ktime_get_ts64(&ts2);
1890 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1895 static unsigned int rnd_next = 1;
1897 static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt)
1901 rnd_next = rnd_next * 1103515245 + 12345;
1902 r = (rnd_next >> 16) & 0x7fff;
1903 return (r * rnd_cnt) >> 15;
1906 static int mmc_test_rnd_perf(struct mmc_test_card *test, int write, int print,
1907 unsigned long sz, int secs, int force_retuning)
1909 unsigned int dev_addr, cnt, rnd_addr, range1, range2, last_ea = 0, ea;
1911 struct timespec64 ts1, ts2, ts;
1916 rnd_addr = mmc_test_capacity(test->card) / 4;
1917 range1 = rnd_addr / test->card->pref_erase;
1918 range2 = range1 / ssz;
1920 ktime_get_ts64(&ts1);
1921 for (cnt = 0; cnt < UINT_MAX; cnt++) {
1922 ktime_get_ts64(&ts2);
1923 ts = timespec64_sub(ts2, ts1);
1924 if (ts.tv_sec >= secs)
1926 ea = mmc_test_rnd_num(range1);
1930 dev_addr = rnd_addr + test->card->pref_erase * ea +
1931 ssz * mmc_test_rnd_num(range2);
1933 mmc_retune_needed(test->card->host);
1934 ret = mmc_test_area_io(test, sz, dev_addr, write, 0, 0);
1939 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1943 static int mmc_test_random_perf(struct mmc_test_card *test, int write)
1945 struct mmc_test_area *t = &test->area;
1950 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1952 * When writing, try to get more consistent results by running
1953 * the test twice with exactly the same I/O but outputting the
1954 * results only for the 2nd run.
1958 ret = mmc_test_rnd_perf(test, write, 0, sz, 10, 0);
1963 ret = mmc_test_rnd_perf(test, write, 1, sz, 10, 0);
1970 ret = mmc_test_rnd_perf(test, write, 0, sz, 10, 0);
1975 return mmc_test_rnd_perf(test, write, 1, sz, 10, 0);
1978 static int mmc_test_retuning(struct mmc_test_card *test)
1980 if (!mmc_can_retune(test->card->host)) {
1981 pr_info("%s: No retuning - test skipped\n",
1982 mmc_hostname(test->card->host));
1983 return RESULT_UNSUP_HOST;
1986 return mmc_test_rnd_perf(test, 0, 0, 8192, 30, 1);
1990 * Random read performance by transfer size.
1992 static int mmc_test_random_read_perf(struct mmc_test_card *test)
1994 return mmc_test_random_perf(test, 0);
1998 * Random write performance by transfer size.
2000 static int mmc_test_random_write_perf(struct mmc_test_card *test)
2002 return mmc_test_random_perf(test, 1);
2005 static int mmc_test_seq_perf(struct mmc_test_card *test, int write,
2006 unsigned int tot_sz, int max_scatter)
2008 struct mmc_test_area *t = &test->area;
2009 unsigned int dev_addr, i, cnt, sz, ssz;
2010 struct timespec64 ts1, ts2;
2016 * In the case of a maximally scattered transfer, the maximum transfer
2017 * size is further limited by using PAGE_SIZE segments.
2020 unsigned long max_tfr;
2022 if (t->max_seg_sz >= PAGE_SIZE)
2023 max_tfr = t->max_segs * PAGE_SIZE;
2025 max_tfr = t->max_segs * t->max_seg_sz;
2031 dev_addr = mmc_test_capacity(test->card) / 4;
2032 if (tot_sz > dev_addr << 9)
2033 tot_sz = dev_addr << 9;
2035 dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
2037 ktime_get_ts64(&ts1);
2038 for (i = 0; i < cnt; i++) {
2039 ret = mmc_test_area_io(test, sz, dev_addr, write,
2045 ktime_get_ts64(&ts2);
2047 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
2052 static int mmc_test_large_seq_perf(struct mmc_test_card *test, int write)
2056 for (i = 0; i < 10; i++) {
2057 ret = mmc_test_seq_perf(test, write, 10 * 1024 * 1024, 1);
2061 for (i = 0; i < 5; i++) {
2062 ret = mmc_test_seq_perf(test, write, 100 * 1024 * 1024, 1);
2066 for (i = 0; i < 3; i++) {
2067 ret = mmc_test_seq_perf(test, write, 1000 * 1024 * 1024, 1);
2076 * Large sequential read performance.
2078 static int mmc_test_large_seq_read_perf(struct mmc_test_card *test)
2080 return mmc_test_large_seq_perf(test, 0);
2084 * Large sequential write performance.
2086 static int mmc_test_large_seq_write_perf(struct mmc_test_card *test)
2088 return mmc_test_large_seq_perf(test, 1);
2091 static int mmc_test_rw_multiple(struct mmc_test_card *test,
2092 struct mmc_test_multiple_rw *tdata,
2093 unsigned int reqsize, unsigned int size,
2096 unsigned int dev_addr;
2097 struct mmc_test_area *t = &test->area;
2100 /* Set up test area */
2101 if (size > mmc_test_capacity(test->card) / 2 * 512)
2102 size = mmc_test_capacity(test->card) / 2 * 512;
2103 if (reqsize > t->max_tfr)
2104 reqsize = t->max_tfr;
2105 dev_addr = mmc_test_capacity(test->card) / 4;
2106 if ((dev_addr & 0xffff0000))
2107 dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
2109 dev_addr &= 0xfffff800; /* Round to 1MiB boundary */
2116 /* prepare test area */
2117 if (mmc_can_erase(test->card) &&
2118 tdata->prepare & MMC_TEST_PREP_ERASE) {
2119 ret = mmc_erase(test->card, dev_addr,
2120 size / 512, test->card->erase_arg);
2122 ret = mmc_erase(test->card, dev_addr,
2123 size / 512, MMC_ERASE_ARG);
2129 ret = mmc_test_area_io_seq(test, reqsize, dev_addr,
2130 tdata->do_write, 0, 1, size / reqsize,
2131 tdata->do_nonblock_req, min_sg_len);
2137 pr_info("[%s] error\n", __func__);
2141 static int mmc_test_rw_multiple_size(struct mmc_test_card *test,
2142 struct mmc_test_multiple_rw *rw)
2146 void *pre_req = test->card->host->ops->pre_req;
2147 void *post_req = test->card->host->ops->post_req;
2149 if (rw->do_nonblock_req &&
2150 ((!pre_req && post_req) || (pre_req && !post_req))) {
2151 pr_info("error: only one of pre/post is defined\n");
2155 for (i = 0 ; i < rw->len && ret == 0; i++) {
2156 ret = mmc_test_rw_multiple(test, rw, rw->bs[i], rw->size, 0);
2163 static int mmc_test_rw_multiple_sg_len(struct mmc_test_card *test,
2164 struct mmc_test_multiple_rw *rw)
2169 for (i = 0 ; i < rw->len && ret == 0; i++) {
2170 ret = mmc_test_rw_multiple(test, rw, 512 * 1024, rw->size,
2179 * Multiple blocking write 4k to 4 MB chunks
2181 static int mmc_test_profile_mult_write_blocking_perf(struct mmc_test_card *test)
2183 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2184 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2185 struct mmc_test_multiple_rw test_data = {
2187 .size = TEST_AREA_MAX_SIZE,
2188 .len = ARRAY_SIZE(bs),
2190 .do_nonblock_req = false,
2191 .prepare = MMC_TEST_PREP_ERASE,
2194 return mmc_test_rw_multiple_size(test, &test_data);
2198 * Multiple non-blocking write 4k to 4 MB chunks
2200 static int mmc_test_profile_mult_write_nonblock_perf(struct mmc_test_card *test)
2202 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2203 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2204 struct mmc_test_multiple_rw test_data = {
2206 .size = TEST_AREA_MAX_SIZE,
2207 .len = ARRAY_SIZE(bs),
2209 .do_nonblock_req = true,
2210 .prepare = MMC_TEST_PREP_ERASE,
2213 return mmc_test_rw_multiple_size(test, &test_data);
2217 * Multiple blocking read 4k to 4 MB chunks
2219 static int mmc_test_profile_mult_read_blocking_perf(struct mmc_test_card *test)
2221 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2222 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2223 struct mmc_test_multiple_rw test_data = {
2225 .size = TEST_AREA_MAX_SIZE,
2226 .len = ARRAY_SIZE(bs),
2228 .do_nonblock_req = false,
2229 .prepare = MMC_TEST_PREP_NONE,
2232 return mmc_test_rw_multiple_size(test, &test_data);
2236 * Multiple non-blocking read 4k to 4 MB chunks
2238 static int mmc_test_profile_mult_read_nonblock_perf(struct mmc_test_card *test)
2240 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2241 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2242 struct mmc_test_multiple_rw test_data = {
2244 .size = TEST_AREA_MAX_SIZE,
2245 .len = ARRAY_SIZE(bs),
2247 .do_nonblock_req = true,
2248 .prepare = MMC_TEST_PREP_NONE,
2251 return mmc_test_rw_multiple_size(test, &test_data);
2255 * Multiple blocking write 1 to 512 sg elements
2257 static int mmc_test_profile_sglen_wr_blocking_perf(struct mmc_test_card *test)
2259 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2260 1 << 7, 1 << 8, 1 << 9};
2261 struct mmc_test_multiple_rw test_data = {
2263 .size = TEST_AREA_MAX_SIZE,
2264 .len = ARRAY_SIZE(sg_len),
2266 .do_nonblock_req = false,
2267 .prepare = MMC_TEST_PREP_ERASE,
2270 return mmc_test_rw_multiple_sg_len(test, &test_data);
2274 * Multiple non-blocking write 1 to 512 sg elements
2276 static int mmc_test_profile_sglen_wr_nonblock_perf(struct mmc_test_card *test)
2278 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2279 1 << 7, 1 << 8, 1 << 9};
2280 struct mmc_test_multiple_rw test_data = {
2282 .size = TEST_AREA_MAX_SIZE,
2283 .len = ARRAY_SIZE(sg_len),
2285 .do_nonblock_req = true,
2286 .prepare = MMC_TEST_PREP_ERASE,
2289 return mmc_test_rw_multiple_sg_len(test, &test_data);
2293 * Multiple blocking read 1 to 512 sg elements
2295 static int mmc_test_profile_sglen_r_blocking_perf(struct mmc_test_card *test)
2297 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2298 1 << 7, 1 << 8, 1 << 9};
2299 struct mmc_test_multiple_rw test_data = {
2301 .size = TEST_AREA_MAX_SIZE,
2302 .len = ARRAY_SIZE(sg_len),
2304 .do_nonblock_req = false,
2305 .prepare = MMC_TEST_PREP_NONE,
2308 return mmc_test_rw_multiple_sg_len(test, &test_data);
2312 * Multiple non-blocking read 1 to 512 sg elements
2314 static int mmc_test_profile_sglen_r_nonblock_perf(struct mmc_test_card *test)
2316 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2317 1 << 7, 1 << 8, 1 << 9};
2318 struct mmc_test_multiple_rw test_data = {
2320 .size = TEST_AREA_MAX_SIZE,
2321 .len = ARRAY_SIZE(sg_len),
2323 .do_nonblock_req = true,
2324 .prepare = MMC_TEST_PREP_NONE,
2327 return mmc_test_rw_multiple_sg_len(test, &test_data);
2331 * eMMC hardware reset.
2333 static int mmc_test_reset(struct mmc_test_card *test)
2335 struct mmc_card *card = test->card;
2338 err = mmc_hw_reset(card);
2341 * Reset will re-enable the card's command queue, but tests
2342 * expect it to be disabled.
2344 if (card->ext_csd.cmdq_en)
2345 mmc_cmdq_disable(card);
2347 } else if (err == -EOPNOTSUPP) {
2348 return RESULT_UNSUP_HOST;
2354 static int mmc_test_send_status(struct mmc_test_card *test,
2355 struct mmc_command *cmd)
2357 memset(cmd, 0, sizeof(*cmd));
2359 cmd->opcode = MMC_SEND_STATUS;
2360 if (!mmc_host_is_spi(test->card->host))
2361 cmd->arg = test->card->rca << 16;
2362 cmd->flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2364 return mmc_wait_for_cmd(test->card->host, cmd, 0);
2367 static int mmc_test_ongoing_transfer(struct mmc_test_card *test,
2368 unsigned int dev_addr, int use_sbc,
2369 int repeat_cmd, int write, int use_areq)
2371 struct mmc_test_req *rq = mmc_test_req_alloc();
2372 struct mmc_host *host = test->card->host;
2373 struct mmc_test_area *t = &test->area;
2374 struct mmc_request *mrq;
2375 unsigned long timeout;
2376 bool expired = false;
2377 int ret = 0, cmd_ret;
2386 mrq->sbc = &rq->sbc;
2387 mrq->cap_cmd_during_tfr = true;
2389 mmc_test_prepare_mrq(test, mrq, t->sg, t->sg_len, dev_addr, t->blocks,
2392 if (use_sbc && t->blocks > 1 && !mrq->sbc) {
2393 ret = mmc_host_cmd23(host) ?
2399 /* Start ongoing data request */
2401 ret = mmc_test_start_areq(test, mrq, NULL);
2405 mmc_wait_for_req(host, mrq);
2408 timeout = jiffies + msecs_to_jiffies(3000);
2412 /* Send status command while data transfer in progress */
2413 cmd_ret = mmc_test_send_status(test, &rq->status);
2417 status = rq->status.resp[0];
2418 if (status & R1_ERROR) {
2423 if (mmc_is_req_done(host, mrq))
2426 expired = time_after(jiffies, timeout);
2428 pr_info("%s: timeout waiting for Tran state status %#x\n",
2429 mmc_hostname(host), status);
2430 cmd_ret = -ETIMEDOUT;
2433 } while (repeat_cmd && R1_CURRENT_STATE(status) != R1_STATE_TRAN);
2435 /* Wait for data request to complete */
2437 ret = mmc_test_start_areq(test, NULL, mrq);
2439 mmc_wait_for_req_done(test->card->host, mrq);
2443 * For cap_cmd_during_tfr request, upper layer must send stop if
2446 if (mrq->data->stop && (mrq->data->error || !mrq->sbc)) {
2448 mmc_wait_for_cmd(host, mrq->data->stop, 0);
2450 ret = mmc_wait_for_cmd(host, mrq->data->stop, 0);
2457 pr_info("%s: Send Status failed: status %#x, error %d\n",
2458 mmc_hostname(test->card->host), status, cmd_ret);
2461 ret = mmc_test_check_result(test, mrq);
2465 ret = mmc_test_wait_busy(test);
2469 if (repeat_cmd && (t->blocks + 1) << 9 > t->max_tfr)
2470 pr_info("%s: %d commands completed during transfer of %u blocks\n",
2471 mmc_hostname(test->card->host), count, t->blocks);
2481 static int __mmc_test_cmds_during_tfr(struct mmc_test_card *test,
2482 unsigned long sz, int use_sbc, int write,
2485 struct mmc_test_area *t = &test->area;
2488 if (!(test->card->host->caps & MMC_CAP_CMD_DURING_TFR))
2489 return RESULT_UNSUP_HOST;
2491 ret = mmc_test_area_map(test, sz, 0, 0, use_areq);
2495 ret = mmc_test_ongoing_transfer(test, t->dev_addr, use_sbc, 0, write,
2500 return mmc_test_ongoing_transfer(test, t->dev_addr, use_sbc, 1, write,
2504 static int mmc_test_cmds_during_tfr(struct mmc_test_card *test, int use_sbc,
2505 int write, int use_areq)
2507 struct mmc_test_area *t = &test->area;
2511 for (sz = 512; sz <= t->max_tfr; sz += 512) {
2512 ret = __mmc_test_cmds_during_tfr(test, sz, use_sbc, write,
2521 * Commands during read - no Set Block Count (CMD23).
2523 static int mmc_test_cmds_during_read(struct mmc_test_card *test)
2525 return mmc_test_cmds_during_tfr(test, 0, 0, 0);
2529 * Commands during write - no Set Block Count (CMD23).
2531 static int mmc_test_cmds_during_write(struct mmc_test_card *test)
2533 return mmc_test_cmds_during_tfr(test, 0, 1, 0);
2537 * Commands during read - use Set Block Count (CMD23).
2539 static int mmc_test_cmds_during_read_cmd23(struct mmc_test_card *test)
2541 return mmc_test_cmds_during_tfr(test, 1, 0, 0);
2545 * Commands during write - use Set Block Count (CMD23).
2547 static int mmc_test_cmds_during_write_cmd23(struct mmc_test_card *test)
2549 return mmc_test_cmds_during_tfr(test, 1, 1, 0);
2553 * Commands during non-blocking read - use Set Block Count (CMD23).
2555 static int mmc_test_cmds_during_read_cmd23_nonblock(struct mmc_test_card *test)
2557 return mmc_test_cmds_during_tfr(test, 1, 0, 1);
2561 * Commands during non-blocking write - use Set Block Count (CMD23).
2563 static int mmc_test_cmds_during_write_cmd23_nonblock(struct mmc_test_card *test)
2565 return mmc_test_cmds_during_tfr(test, 1, 1, 1);
2568 static const struct mmc_test_case mmc_test_cases[] = {
2570 .name = "Basic write (no data verification)",
2571 .run = mmc_test_basic_write,
2575 .name = "Basic read (no data verification)",
2576 .run = mmc_test_basic_read,
2580 .name = "Basic write (with data verification)",
2581 .prepare = mmc_test_prepare_write,
2582 .run = mmc_test_verify_write,
2583 .cleanup = mmc_test_cleanup,
2587 .name = "Basic read (with data verification)",
2588 .prepare = mmc_test_prepare_read,
2589 .run = mmc_test_verify_read,
2590 .cleanup = mmc_test_cleanup,
2594 .name = "Multi-block write",
2595 .prepare = mmc_test_prepare_write,
2596 .run = mmc_test_multi_write,
2597 .cleanup = mmc_test_cleanup,
2601 .name = "Multi-block read",
2602 .prepare = mmc_test_prepare_read,
2603 .run = mmc_test_multi_read,
2604 .cleanup = mmc_test_cleanup,
2608 .name = "Power of two block writes",
2609 .prepare = mmc_test_prepare_write,
2610 .run = mmc_test_pow2_write,
2611 .cleanup = mmc_test_cleanup,
2615 .name = "Power of two block reads",
2616 .prepare = mmc_test_prepare_read,
2617 .run = mmc_test_pow2_read,
2618 .cleanup = mmc_test_cleanup,
2622 .name = "Weird sized block writes",
2623 .prepare = mmc_test_prepare_write,
2624 .run = mmc_test_weird_write,
2625 .cleanup = mmc_test_cleanup,
2629 .name = "Weird sized block reads",
2630 .prepare = mmc_test_prepare_read,
2631 .run = mmc_test_weird_read,
2632 .cleanup = mmc_test_cleanup,
2636 .name = "Badly aligned write",
2637 .prepare = mmc_test_prepare_write,
2638 .run = mmc_test_align_write,
2639 .cleanup = mmc_test_cleanup,
2643 .name = "Badly aligned read",
2644 .prepare = mmc_test_prepare_read,
2645 .run = mmc_test_align_read,
2646 .cleanup = mmc_test_cleanup,
2650 .name = "Badly aligned multi-block write",
2651 .prepare = mmc_test_prepare_write,
2652 .run = mmc_test_align_multi_write,
2653 .cleanup = mmc_test_cleanup,
2657 .name = "Badly aligned multi-block read",
2658 .prepare = mmc_test_prepare_read,
2659 .run = mmc_test_align_multi_read,
2660 .cleanup = mmc_test_cleanup,
2664 .name = "Proper xfer_size at write (start failure)",
2665 .run = mmc_test_xfersize_write,
2669 .name = "Proper xfer_size at read (start failure)",
2670 .run = mmc_test_xfersize_read,
2674 .name = "Proper xfer_size at write (midway failure)",
2675 .run = mmc_test_multi_xfersize_write,
2679 .name = "Proper xfer_size at read (midway failure)",
2680 .run = mmc_test_multi_xfersize_read,
2683 #ifdef CONFIG_HIGHMEM
2686 .name = "Highmem write",
2687 .prepare = mmc_test_prepare_write,
2688 .run = mmc_test_write_high,
2689 .cleanup = mmc_test_cleanup,
2693 .name = "Highmem read",
2694 .prepare = mmc_test_prepare_read,
2695 .run = mmc_test_read_high,
2696 .cleanup = mmc_test_cleanup,
2700 .name = "Multi-block highmem write",
2701 .prepare = mmc_test_prepare_write,
2702 .run = mmc_test_multi_write_high,
2703 .cleanup = mmc_test_cleanup,
2707 .name = "Multi-block highmem read",
2708 .prepare = mmc_test_prepare_read,
2709 .run = mmc_test_multi_read_high,
2710 .cleanup = mmc_test_cleanup,
2716 .name = "Highmem write",
2717 .run = mmc_test_no_highmem,
2721 .name = "Highmem read",
2722 .run = mmc_test_no_highmem,
2726 .name = "Multi-block highmem write",
2727 .run = mmc_test_no_highmem,
2731 .name = "Multi-block highmem read",
2732 .run = mmc_test_no_highmem,
2735 #endif /* CONFIG_HIGHMEM */
2738 .name = "Best-case read performance",
2739 .prepare = mmc_test_area_prepare_fill,
2740 .run = mmc_test_best_read_performance,
2741 .cleanup = mmc_test_area_cleanup,
2745 .name = "Best-case write performance",
2746 .prepare = mmc_test_area_prepare_erase,
2747 .run = mmc_test_best_write_performance,
2748 .cleanup = mmc_test_area_cleanup,
2752 .name = "Best-case read performance into scattered pages",
2753 .prepare = mmc_test_area_prepare_fill,
2754 .run = mmc_test_best_read_perf_max_scatter,
2755 .cleanup = mmc_test_area_cleanup,
2759 .name = "Best-case write performance from scattered pages",
2760 .prepare = mmc_test_area_prepare_erase,
2761 .run = mmc_test_best_write_perf_max_scatter,
2762 .cleanup = mmc_test_area_cleanup,
2766 .name = "Single read performance by transfer size",
2767 .prepare = mmc_test_area_prepare_fill,
2768 .run = mmc_test_profile_read_perf,
2769 .cleanup = mmc_test_area_cleanup,
2773 .name = "Single write performance by transfer size",
2774 .prepare = mmc_test_area_prepare,
2775 .run = mmc_test_profile_write_perf,
2776 .cleanup = mmc_test_area_cleanup,
2780 .name = "Single trim performance by transfer size",
2781 .prepare = mmc_test_area_prepare_fill,
2782 .run = mmc_test_profile_trim_perf,
2783 .cleanup = mmc_test_area_cleanup,
2787 .name = "Consecutive read performance by transfer size",
2788 .prepare = mmc_test_area_prepare_fill,
2789 .run = mmc_test_profile_seq_read_perf,
2790 .cleanup = mmc_test_area_cleanup,
2794 .name = "Consecutive write performance by transfer size",
2795 .prepare = mmc_test_area_prepare,
2796 .run = mmc_test_profile_seq_write_perf,
2797 .cleanup = mmc_test_area_cleanup,
2801 .name = "Consecutive trim performance by transfer size",
2802 .prepare = mmc_test_area_prepare,
2803 .run = mmc_test_profile_seq_trim_perf,
2804 .cleanup = mmc_test_area_cleanup,
2808 .name = "Random read performance by transfer size",
2809 .prepare = mmc_test_area_prepare,
2810 .run = mmc_test_random_read_perf,
2811 .cleanup = mmc_test_area_cleanup,
2815 .name = "Random write performance by transfer size",
2816 .prepare = mmc_test_area_prepare,
2817 .run = mmc_test_random_write_perf,
2818 .cleanup = mmc_test_area_cleanup,
2822 .name = "Large sequential read into scattered pages",
2823 .prepare = mmc_test_area_prepare,
2824 .run = mmc_test_large_seq_read_perf,
2825 .cleanup = mmc_test_area_cleanup,
2829 .name = "Large sequential write from scattered pages",
2830 .prepare = mmc_test_area_prepare,
2831 .run = mmc_test_large_seq_write_perf,
2832 .cleanup = mmc_test_area_cleanup,
2836 .name = "Write performance with blocking req 4k to 4MB",
2837 .prepare = mmc_test_area_prepare,
2838 .run = mmc_test_profile_mult_write_blocking_perf,
2839 .cleanup = mmc_test_area_cleanup,
2843 .name = "Write performance with non-blocking req 4k to 4MB",
2844 .prepare = mmc_test_area_prepare,
2845 .run = mmc_test_profile_mult_write_nonblock_perf,
2846 .cleanup = mmc_test_area_cleanup,
2850 .name = "Read performance with blocking req 4k to 4MB",
2851 .prepare = mmc_test_area_prepare,
2852 .run = mmc_test_profile_mult_read_blocking_perf,
2853 .cleanup = mmc_test_area_cleanup,
2857 .name = "Read performance with non-blocking req 4k to 4MB",
2858 .prepare = mmc_test_area_prepare,
2859 .run = mmc_test_profile_mult_read_nonblock_perf,
2860 .cleanup = mmc_test_area_cleanup,
2864 .name = "Write performance blocking req 1 to 512 sg elems",
2865 .prepare = mmc_test_area_prepare,
2866 .run = mmc_test_profile_sglen_wr_blocking_perf,
2867 .cleanup = mmc_test_area_cleanup,
2871 .name = "Write performance non-blocking req 1 to 512 sg elems",
2872 .prepare = mmc_test_area_prepare,
2873 .run = mmc_test_profile_sglen_wr_nonblock_perf,
2874 .cleanup = mmc_test_area_cleanup,
2878 .name = "Read performance blocking req 1 to 512 sg elems",
2879 .prepare = mmc_test_area_prepare,
2880 .run = mmc_test_profile_sglen_r_blocking_perf,
2881 .cleanup = mmc_test_area_cleanup,
2885 .name = "Read performance non-blocking req 1 to 512 sg elems",
2886 .prepare = mmc_test_area_prepare,
2887 .run = mmc_test_profile_sglen_r_nonblock_perf,
2888 .cleanup = mmc_test_area_cleanup,
2892 .name = "Reset test",
2893 .run = mmc_test_reset,
2897 .name = "Commands during read - no Set Block Count (CMD23)",
2898 .prepare = mmc_test_area_prepare,
2899 .run = mmc_test_cmds_during_read,
2900 .cleanup = mmc_test_area_cleanup,
2904 .name = "Commands during write - no Set Block Count (CMD23)",
2905 .prepare = mmc_test_area_prepare,
2906 .run = mmc_test_cmds_during_write,
2907 .cleanup = mmc_test_area_cleanup,
2911 .name = "Commands during read - use Set Block Count (CMD23)",
2912 .prepare = mmc_test_area_prepare,
2913 .run = mmc_test_cmds_during_read_cmd23,
2914 .cleanup = mmc_test_area_cleanup,
2918 .name = "Commands during write - use Set Block Count (CMD23)",
2919 .prepare = mmc_test_area_prepare,
2920 .run = mmc_test_cmds_during_write_cmd23,
2921 .cleanup = mmc_test_area_cleanup,
2925 .name = "Commands during non-blocking read - use Set Block Count (CMD23)",
2926 .prepare = mmc_test_area_prepare,
2927 .run = mmc_test_cmds_during_read_cmd23_nonblock,
2928 .cleanup = mmc_test_area_cleanup,
2932 .name = "Commands during non-blocking write - use Set Block Count (CMD23)",
2933 .prepare = mmc_test_area_prepare,
2934 .run = mmc_test_cmds_during_write_cmd23_nonblock,
2935 .cleanup = mmc_test_area_cleanup,
2939 .name = "Re-tuning reliability",
2940 .prepare = mmc_test_area_prepare,
2941 .run = mmc_test_retuning,
2942 .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 = 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 %llu.%09u %u %u.%02u\n",
3084 tr->count, tr->sectors,
3085 (u64)tr->ts.tv_sec, (u32)tr->ts.tv_nsec,
3086 tr->rate, tr->iops / 100, tr->iops % 100);
3090 mutex_unlock(&mmc_test_lock);
3095 static int mtf_test_open(struct inode *inode, struct file *file)
3097 return single_open(file, mtf_test_show, inode->i_private);
3100 static ssize_t mtf_test_write(struct file *file, const char __user *buf,
3101 size_t count, loff_t *pos)
3103 struct seq_file *sf = file->private_data;
3104 struct mmc_card *card = sf->private;
3105 struct mmc_test_card *test;
3109 ret = kstrtol_from_user(buf, count, 10, &testcase);
3113 test = kzalloc(sizeof(*test), GFP_KERNEL);
3118 * Remove all test cases associated with given card. Thus we have only
3119 * actual data of the last run.
3121 mmc_test_free_result(card);
3125 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
3126 #ifdef CONFIG_HIGHMEM
3127 test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
3130 #ifdef CONFIG_HIGHMEM
3131 if (test->buffer && test->highmem) {
3135 mutex_lock(&mmc_test_lock);
3136 mmc_test_run(test, testcase);
3137 mutex_unlock(&mmc_test_lock);
3140 #ifdef CONFIG_HIGHMEM
3141 __free_pages(test->highmem, BUFFER_ORDER);
3143 kfree(test->buffer);
3149 static const struct file_operations mmc_test_fops_test = {
3150 .open = mtf_test_open,
3152 .write = mtf_test_write,
3153 .llseek = seq_lseek,
3154 .release = single_release,
3157 static int mtf_testlist_show(struct seq_file *sf, void *data)
3161 mutex_lock(&mmc_test_lock);
3163 seq_puts(sf, "0:\tRun all tests\n");
3164 for (i = 0; i < ARRAY_SIZE(mmc_test_cases); i++)
3165 seq_printf(sf, "%d:\t%s\n", i + 1, mmc_test_cases[i].name);
3167 mutex_unlock(&mmc_test_lock);
3172 DEFINE_SHOW_ATTRIBUTE(mtf_testlist);
3174 static void mmc_test_free_dbgfs_file(struct mmc_card *card)
3176 struct mmc_test_dbgfs_file *df, *dfs;
3178 mutex_lock(&mmc_test_lock);
3180 list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
3181 if (card && df->card != card)
3183 debugfs_remove(df->file);
3184 list_del(&df->link);
3188 mutex_unlock(&mmc_test_lock);
3191 static int __mmc_test_register_dbgfs_file(struct mmc_card *card,
3192 const char *name, umode_t mode, const struct file_operations *fops)
3194 struct dentry *file = NULL;
3195 struct mmc_test_dbgfs_file *df;
3197 if (card->debugfs_root)
3198 file = debugfs_create_file(name, mode, card->debugfs_root,
3201 df = kmalloc(sizeof(*df), GFP_KERNEL);
3203 debugfs_remove(file);
3210 list_add(&df->link, &mmc_test_file_test);
3214 static int mmc_test_register_dbgfs_file(struct mmc_card *card)
3218 mutex_lock(&mmc_test_lock);
3220 ret = __mmc_test_register_dbgfs_file(card, "test", S_IWUSR | S_IRUGO,
3221 &mmc_test_fops_test);
3225 ret = __mmc_test_register_dbgfs_file(card, "testlist", S_IRUGO,
3226 &mtf_testlist_fops);
3231 mutex_unlock(&mmc_test_lock);
3236 static int mmc_test_probe(struct mmc_card *card)
3240 if (!mmc_card_mmc(card) && !mmc_card_sd(card))
3243 ret = mmc_test_register_dbgfs_file(card);
3247 if (card->ext_csd.cmdq_en) {
3248 mmc_claim_host(card->host);
3249 ret = mmc_cmdq_disable(card);
3250 mmc_release_host(card->host);
3255 dev_info(&card->dev, "Card claimed for testing.\n");
3260 static void mmc_test_remove(struct mmc_card *card)
3262 if (card->reenable_cmdq) {
3263 mmc_claim_host(card->host);
3264 mmc_cmdq_enable(card);
3265 mmc_release_host(card->host);
3267 mmc_test_free_result(card);
3268 mmc_test_free_dbgfs_file(card);
3271 static struct mmc_driver mmc_driver = {
3275 .probe = mmc_test_probe,
3276 .remove = mmc_test_remove,
3279 static int __init mmc_test_init(void)
3281 return mmc_register_driver(&mmc_driver);
3284 static void __exit mmc_test_exit(void)
3286 /* Clear stalled data if card is still plugged */
3287 mmc_test_free_result(NULL);
3288 mmc_test_free_dbgfs_file(NULL);
3290 mmc_unregister_driver(&mmc_driver);
3293 module_init(mmc_test_init);
3294 module_exit(mmc_test_exit);
3296 MODULE_LICENSE("GPL");
3297 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
3298 MODULE_AUTHOR("Pierre Ossman");