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>
33 #define RESULT_UNSUP_HOST 2
34 #define RESULT_UNSUP_CARD 3
36 #define BUFFER_ORDER 2
37 #define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
39 #define TEST_ALIGN_END 8
42 * Limit the test area size to the maximum MMC HC erase group size. Note that
43 * the maximum SD allocation unit size is just 4MiB.
45 #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
48 * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
49 * @page: first page in the allocation
50 * @order: order of the number of pages allocated
52 struct mmc_test_pages
{
58 * struct mmc_test_mem - allocated memory.
59 * @arr: array of allocations
60 * @cnt: number of allocations
63 struct mmc_test_pages
*arr
;
68 * struct mmc_test_area - information for performance tests.
69 * @max_sz: test area size (in bytes)
70 * @dev_addr: address on card at which to do performance tests
71 * @max_tfr: maximum transfer size allowed by driver (in bytes)
72 * @max_segs: maximum segments allowed by driver in scatterlist @sg
73 * @max_seg_sz: maximum segment size allowed by driver
74 * @blocks: number of (512 byte) blocks currently mapped by @sg
75 * @sg_len: length of currently mapped scatterlist @sg
76 * @mem: allocated memory
79 struct mmc_test_area
{
81 unsigned int dev_addr
;
83 unsigned int max_segs
;
84 unsigned int max_seg_sz
;
87 struct mmc_test_mem
*mem
;
88 struct scatterlist
*sg
;
92 * struct mmc_test_transfer_result - transfer results for performance tests.
93 * @link: double-linked list
94 * @count: amount of group of sectors to check
95 * @sectors: amount of sectors to check in one group
96 * @ts: time values of transfer
97 * @rate: calculated transfer rate
98 * @iops: I/O operations per second (times 100)
100 struct mmc_test_transfer_result
{
101 struct list_head link
;
103 unsigned int sectors
;
104 struct timespec64 ts
;
110 * struct mmc_test_general_result - results for tests.
111 * @link: double-linked list
112 * @card: card under test
113 * @testcase: number of test case
114 * @result: result of test run
115 * @tr_lst: transfer measurements if any as mmc_test_transfer_result
117 struct mmc_test_general_result
{
118 struct list_head link
;
119 struct mmc_card
*card
;
122 struct list_head tr_lst
;
126 * struct mmc_test_dbgfs_file - debugfs related file.
127 * @link: double-linked list
128 * @card: card under test
129 * @file: file created under debugfs
131 struct mmc_test_dbgfs_file
{
132 struct list_head link
;
133 struct mmc_card
*card
;
138 * struct mmc_test_card - test information.
139 * @card: card under test
140 * @scratch: transfer buffer
141 * @buffer: transfer buffer
142 * @highmem: buffer for highmem tests
143 * @area: information for performance tests
144 * @gr: pointer to results of current testcase
146 struct mmc_test_card
{
147 struct mmc_card
*card
;
149 u8 scratch
[BUFFER_SIZE
];
151 #ifdef CONFIG_HIGHMEM
152 struct page
*highmem
;
154 struct mmc_test_area area
;
155 struct mmc_test_general_result
*gr
;
158 enum mmc_test_prep_media
{
159 MMC_TEST_PREP_NONE
= 0,
160 MMC_TEST_PREP_WRITE_FULL
= 1 << 0,
161 MMC_TEST_PREP_ERASE
= 1 << 1,
164 struct mmc_test_multiple_rw
{
165 unsigned int *sg_len
;
170 bool do_nonblock_req
;
171 enum mmc_test_prep_media prepare
;
174 /*******************************************************************/
175 /* General helper functions */
176 /*******************************************************************/
179 * Configure correct block size in card
181 static int mmc_test_set_blksize(struct mmc_test_card
*test
, unsigned size
)
183 return mmc_set_blocklen(test
->card
, size
);
186 static bool mmc_test_card_cmd23(struct mmc_card
*card
)
188 return mmc_card_mmc(card
) ||
189 (mmc_card_sd(card
) && card
->scr
.cmds
& SD_SCR_CMD23_SUPPORT
);
192 static void mmc_test_prepare_sbc(struct mmc_test_card
*test
,
193 struct mmc_request
*mrq
, unsigned int blocks
)
195 struct mmc_card
*card
= test
->card
;
197 if (!mrq
->sbc
|| !mmc_host_cmd23(card
->host
) ||
198 !mmc_test_card_cmd23(card
) || !mmc_op_multi(mrq
->cmd
->opcode
) ||
199 (card
->quirks
& MMC_QUIRK_BLK_NO_CMD23
)) {
204 mrq
->sbc
->opcode
= MMC_SET_BLOCK_COUNT
;
205 mrq
->sbc
->arg
= blocks
;
206 mrq
->sbc
->flags
= MMC_RSP_R1
| MMC_CMD_AC
;
210 * Fill in the mmc_request structure given a set of transfer parameters.
212 static void mmc_test_prepare_mrq(struct mmc_test_card
*test
,
213 struct mmc_request
*mrq
, struct scatterlist
*sg
, unsigned sg_len
,
214 unsigned dev_addr
, unsigned blocks
, unsigned blksz
, int write
)
216 if (WARN_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
|| !mrq
->stop
))
220 mrq
->cmd
->opcode
= write
?
221 MMC_WRITE_MULTIPLE_BLOCK
: MMC_READ_MULTIPLE_BLOCK
;
223 mrq
->cmd
->opcode
= write
?
224 MMC_WRITE_BLOCK
: MMC_READ_SINGLE_BLOCK
;
227 mrq
->cmd
->arg
= dev_addr
;
228 if (!mmc_card_blockaddr(test
->card
))
231 mrq
->cmd
->flags
= MMC_RSP_R1
| MMC_CMD_ADTC
;
236 mrq
->stop
->opcode
= MMC_STOP_TRANSMISSION
;
238 mrq
->stop
->flags
= MMC_RSP_R1B
| MMC_CMD_AC
;
241 mrq
->data
->blksz
= blksz
;
242 mrq
->data
->blocks
= blocks
;
243 mrq
->data
->flags
= write
? MMC_DATA_WRITE
: MMC_DATA_READ
;
245 mrq
->data
->sg_len
= sg_len
;
247 mmc_test_prepare_sbc(test
, mrq
, blocks
);
249 mmc_set_data_timeout(mrq
->data
, test
->card
);
252 static int mmc_test_busy(struct mmc_command
*cmd
)
254 return !(cmd
->resp
[0] & R1_READY_FOR_DATA
) ||
255 (R1_CURRENT_STATE(cmd
->resp
[0]) == R1_STATE_PRG
);
259 * Wait for the card to finish the busy state
261 static int mmc_test_wait_busy(struct mmc_test_card
*test
)
264 struct mmc_command cmd
= {};
268 memset(&cmd
, 0, sizeof(struct mmc_command
));
270 cmd
.opcode
= MMC_SEND_STATUS
;
271 cmd
.arg
= test
->card
->rca
<< 16;
272 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
274 ret
= mmc_wait_for_cmd(test
->card
->host
, &cmd
, 0);
278 if (!busy
&& mmc_test_busy(&cmd
)) {
280 if (test
->card
->host
->caps
& MMC_CAP_WAIT_WHILE_BUSY
)
281 pr_info("%s: Warning: Host did not wait for busy state to end.\n",
282 mmc_hostname(test
->card
->host
));
284 } while (mmc_test_busy(&cmd
));
290 * Transfer a single sector of kernel addressable data
292 static int mmc_test_buffer_transfer(struct mmc_test_card
*test
,
293 u8
*buffer
, unsigned addr
, unsigned blksz
, int write
)
295 struct mmc_request mrq
= {};
296 struct mmc_command cmd
= {};
297 struct mmc_command stop
= {};
298 struct mmc_data data
= {};
300 struct scatterlist sg
;
306 sg_init_one(&sg
, buffer
, blksz
);
308 mmc_test_prepare_mrq(test
, &mrq
, &sg
, 1, addr
, 1, blksz
, write
);
310 mmc_wait_for_req(test
->card
->host
, &mrq
);
317 return mmc_test_wait_busy(test
);
320 static void mmc_test_free_mem(struct mmc_test_mem
*mem
)
325 __free_pages(mem
->arr
[mem
->cnt
].page
,
326 mem
->arr
[mem
->cnt
].order
);
332 * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
333 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
334 * not exceed a maximum number of segments and try not to make segments much
335 * bigger than maximum segment size.
337 static struct mmc_test_mem
*mmc_test_alloc_mem(unsigned long min_sz
,
338 unsigned long max_sz
,
339 unsigned int max_segs
,
340 unsigned int max_seg_sz
)
342 unsigned long max_page_cnt
= DIV_ROUND_UP(max_sz
, PAGE_SIZE
);
343 unsigned long min_page_cnt
= DIV_ROUND_UP(min_sz
, PAGE_SIZE
);
344 unsigned long max_seg_page_cnt
= DIV_ROUND_UP(max_seg_sz
, PAGE_SIZE
);
345 unsigned long page_cnt
= 0;
346 unsigned long limit
= nr_free_buffer_pages() >> 4;
347 struct mmc_test_mem
*mem
;
349 if (max_page_cnt
> limit
)
350 max_page_cnt
= limit
;
351 if (min_page_cnt
> max_page_cnt
)
352 min_page_cnt
= max_page_cnt
;
354 if (max_seg_page_cnt
> max_page_cnt
)
355 max_seg_page_cnt
= max_page_cnt
;
357 if (max_segs
> max_page_cnt
)
358 max_segs
= max_page_cnt
;
360 mem
= kzalloc(sizeof(*mem
), GFP_KERNEL
);
364 mem
->arr
= kcalloc(max_segs
, sizeof(*mem
->arr
), GFP_KERNEL
);
368 while (max_page_cnt
) {
371 gfp_t flags
= GFP_KERNEL
| GFP_DMA
| __GFP_NOWARN
|
374 order
= get_order(max_seg_page_cnt
<< PAGE_SHIFT
);
376 page
= alloc_pages(flags
, order
);
382 if (page_cnt
< min_page_cnt
)
386 mem
->arr
[mem
->cnt
].page
= page
;
387 mem
->arr
[mem
->cnt
].order
= order
;
389 if (max_page_cnt
<= (1UL << order
))
391 max_page_cnt
-= 1UL << order
;
392 page_cnt
+= 1UL << order
;
393 if (mem
->cnt
>= max_segs
) {
394 if (page_cnt
< min_page_cnt
)
403 mmc_test_free_mem(mem
);
408 * Map memory into a scatterlist. Optionally allow the same memory to be
409 * mapped more than once.
411 static int mmc_test_map_sg(struct mmc_test_mem
*mem
, unsigned long size
,
412 struct scatterlist
*sglist
, int repeat
,
413 unsigned int max_segs
, unsigned int max_seg_sz
,
414 unsigned int *sg_len
, int min_sg_len
)
416 struct scatterlist
*sg
= NULL
;
418 unsigned long sz
= size
;
420 sg_init_table(sglist
, max_segs
);
421 if (min_sg_len
> max_segs
)
422 min_sg_len
= max_segs
;
426 for (i
= 0; i
< mem
->cnt
; i
++) {
427 unsigned long len
= PAGE_SIZE
<< mem
->arr
[i
].order
;
429 if (min_sg_len
&& (size
/ min_sg_len
< len
))
430 len
= ALIGN(size
/ min_sg_len
, 512);
433 if (len
> max_seg_sz
)
441 sg_set_page(sg
, mem
->arr
[i
].page
, len
, 0);
447 } while (sz
&& repeat
);
459 * Map memory into a scatterlist so that no pages are contiguous. Allow the
460 * same memory to be mapped more than once.
462 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem
*mem
,
464 struct scatterlist
*sglist
,
465 unsigned int max_segs
,
466 unsigned int max_seg_sz
,
467 unsigned int *sg_len
)
469 struct scatterlist
*sg
= NULL
;
470 unsigned int i
= mem
->cnt
, cnt
;
472 void *base
, *addr
, *last_addr
= NULL
;
474 sg_init_table(sglist
, max_segs
);
478 base
= page_address(mem
->arr
[--i
].page
);
479 cnt
= 1 << mem
->arr
[i
].order
;
481 addr
= base
+ PAGE_SIZE
* --cnt
;
482 if (last_addr
&& last_addr
+ PAGE_SIZE
== addr
)
486 if (len
> max_seg_sz
)
496 sg_set_page(sg
, virt_to_page(addr
), len
, 0);
511 * Calculate transfer rate in bytes per second.
513 static unsigned int mmc_test_rate(uint64_t bytes
, struct timespec64
*ts
)
517 ns
= timespec64_to_ns(ts
);
520 while (ns
> UINT_MAX
) {
528 do_div(bytes
, (uint32_t)ns
);
534 * Save transfer results for future usage
536 static void mmc_test_save_transfer_result(struct mmc_test_card
*test
,
537 unsigned int count
, unsigned int sectors
, struct timespec64 ts
,
538 unsigned int rate
, unsigned int iops
)
540 struct mmc_test_transfer_result
*tr
;
545 tr
= kmalloc(sizeof(*tr
), GFP_KERNEL
);
550 tr
->sectors
= sectors
;
555 list_add_tail(&tr
->link
, &test
->gr
->tr_lst
);
559 * Print the transfer rate.
561 static void mmc_test_print_rate(struct mmc_test_card
*test
, uint64_t bytes
,
562 struct timespec64
*ts1
, struct timespec64
*ts2
)
564 unsigned int rate
, iops
, sectors
= bytes
>> 9;
565 struct timespec64 ts
;
567 ts
= timespec64_sub(*ts2
, *ts1
);
569 rate
= mmc_test_rate(bytes
, &ts
);
570 iops
= mmc_test_rate(100, &ts
); /* I/O ops per sec x 100 */
572 pr_info("%s: Transfer of %u sectors (%u%s KiB) took %llu.%09u "
573 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
574 mmc_hostname(test
->card
->host
), sectors
, sectors
>> 1,
575 (sectors
& 1 ? ".5" : ""), (u64
)ts
.tv_sec
,
576 (u32
)ts
.tv_nsec
, rate
/ 1000, rate
/ 1024,
577 iops
/ 100, iops
% 100);
579 mmc_test_save_transfer_result(test
, 1, sectors
, ts
, rate
, iops
);
583 * Print the average transfer rate.
585 static void mmc_test_print_avg_rate(struct mmc_test_card
*test
, uint64_t bytes
,
586 unsigned int count
, struct timespec64
*ts1
,
587 struct timespec64
*ts2
)
589 unsigned int rate
, iops
, sectors
= bytes
>> 9;
590 uint64_t tot
= bytes
* count
;
591 struct timespec64 ts
;
593 ts
= timespec64_sub(*ts2
, *ts1
);
595 rate
= mmc_test_rate(tot
, &ts
);
596 iops
= mmc_test_rate(count
* 100, &ts
); /* I/O ops per sec x 100 */
598 pr_info("%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
599 "%llu.%09u seconds (%u kB/s, %u KiB/s, "
600 "%u.%02u IOPS, sg_len %d)\n",
601 mmc_hostname(test
->card
->host
), count
, sectors
, count
,
602 sectors
>> 1, (sectors
& 1 ? ".5" : ""),
603 (u64
)ts
.tv_sec
, (u32
)ts
.tv_nsec
,
604 rate
/ 1000, rate
/ 1024, iops
/ 100, iops
% 100,
607 mmc_test_save_transfer_result(test
, count
, sectors
, ts
, rate
, iops
);
611 * Return the card size in sectors.
613 static unsigned int mmc_test_capacity(struct mmc_card
*card
)
615 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
))
616 return card
->ext_csd
.sectors
;
618 return card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9);
621 /*******************************************************************/
622 /* Test preparation and cleanup */
623 /*******************************************************************/
626 * Fill the first couple of sectors of the card with known data
627 * so that bad reads/writes can be detected
629 static int __mmc_test_prepare(struct mmc_test_card
*test
, int write
)
633 ret
= mmc_test_set_blksize(test
, 512);
638 memset(test
->buffer
, 0xDF, 512);
640 for (i
= 0; i
< 512; i
++)
644 for (i
= 0; i
< BUFFER_SIZE
/ 512; i
++) {
645 ret
= mmc_test_buffer_transfer(test
, test
->buffer
, i
, 512, 1);
653 static int mmc_test_prepare_write(struct mmc_test_card
*test
)
655 return __mmc_test_prepare(test
, 1);
658 static int mmc_test_prepare_read(struct mmc_test_card
*test
)
660 return __mmc_test_prepare(test
, 0);
663 static int mmc_test_cleanup(struct mmc_test_card
*test
)
667 ret
= mmc_test_set_blksize(test
, 512);
671 memset(test
->buffer
, 0, 512);
673 for (i
= 0; i
< BUFFER_SIZE
/ 512; i
++) {
674 ret
= mmc_test_buffer_transfer(test
, test
->buffer
, i
, 512, 1);
682 /*******************************************************************/
683 /* Test execution helpers */
684 /*******************************************************************/
687 * Modifies the mmc_request to perform the "short transfer" tests
689 static void mmc_test_prepare_broken_mrq(struct mmc_test_card
*test
,
690 struct mmc_request
*mrq
, int write
)
692 if (WARN_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
))
695 if (mrq
->data
->blocks
> 1) {
696 mrq
->cmd
->opcode
= write
?
697 MMC_WRITE_BLOCK
: MMC_READ_SINGLE_BLOCK
;
700 mrq
->cmd
->opcode
= MMC_SEND_STATUS
;
701 mrq
->cmd
->arg
= test
->card
->rca
<< 16;
706 * Checks that a normal transfer didn't have any errors
708 static int mmc_test_check_result(struct mmc_test_card
*test
,
709 struct mmc_request
*mrq
)
713 if (WARN_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
))
718 if (mrq
->sbc
&& mrq
->sbc
->error
)
719 ret
= mrq
->sbc
->error
;
720 if (!ret
&& mrq
->cmd
->error
)
721 ret
= mrq
->cmd
->error
;
722 if (!ret
&& mrq
->data
->error
)
723 ret
= mrq
->data
->error
;
724 if (!ret
&& mrq
->stop
&& mrq
->stop
->error
)
725 ret
= mrq
->stop
->error
;
726 if (!ret
&& mrq
->data
->bytes_xfered
!=
727 mrq
->data
->blocks
* mrq
->data
->blksz
)
731 ret
= RESULT_UNSUP_HOST
;
737 * Checks that a "short transfer" behaved as expected
739 static int mmc_test_check_broken_result(struct mmc_test_card
*test
,
740 struct mmc_request
*mrq
)
744 if (WARN_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
))
749 if (!ret
&& mrq
->cmd
->error
)
750 ret
= mrq
->cmd
->error
;
751 if (!ret
&& mrq
->data
->error
== 0)
753 if (!ret
&& mrq
->data
->error
!= -ETIMEDOUT
)
754 ret
= mrq
->data
->error
;
755 if (!ret
&& mrq
->stop
&& mrq
->stop
->error
)
756 ret
= mrq
->stop
->error
;
757 if (mrq
->data
->blocks
> 1) {
758 if (!ret
&& mrq
->data
->bytes_xfered
> mrq
->data
->blksz
)
761 if (!ret
&& mrq
->data
->bytes_xfered
> 0)
766 ret
= RESULT_UNSUP_HOST
;
771 struct mmc_test_req
{
772 struct mmc_request mrq
;
773 struct mmc_command sbc
;
774 struct mmc_command cmd
;
775 struct mmc_command stop
;
776 struct mmc_command status
;
777 struct mmc_data data
;
781 * Tests nonblock transfer with certain parameters
783 static void mmc_test_req_reset(struct mmc_test_req
*rq
)
785 memset(rq
, 0, sizeof(struct mmc_test_req
));
787 rq
->mrq
.cmd
= &rq
->cmd
;
788 rq
->mrq
.data
= &rq
->data
;
789 rq
->mrq
.stop
= &rq
->stop
;
792 static struct mmc_test_req
*mmc_test_req_alloc(void)
794 struct mmc_test_req
*rq
= kmalloc(sizeof(*rq
), GFP_KERNEL
);
797 mmc_test_req_reset(rq
);
802 static void mmc_test_wait_done(struct mmc_request
*mrq
)
804 complete(&mrq
->completion
);
807 static int mmc_test_start_areq(struct mmc_test_card
*test
,
808 struct mmc_request
*mrq
,
809 struct mmc_request
*prev_mrq
)
811 struct mmc_host
*host
= test
->card
->host
;
815 init_completion(&mrq
->completion
);
816 mrq
->done
= mmc_test_wait_done
;
817 mmc_pre_req(host
, mrq
);
821 wait_for_completion(&prev_mrq
->completion
);
822 err
= mmc_test_wait_busy(test
);
824 err
= mmc_test_check_result(test
, prev_mrq
);
828 err
= mmc_start_request(host
, mrq
);
830 mmc_retune_release(host
);
834 mmc_post_req(host
, prev_mrq
, 0);
837 mmc_post_req(host
, mrq
, err
);
842 static int mmc_test_nonblock_transfer(struct mmc_test_card
*test
,
843 struct scatterlist
*sg
, unsigned sg_len
,
844 unsigned dev_addr
, unsigned blocks
,
845 unsigned blksz
, int write
, int count
)
847 struct mmc_test_req
*rq1
, *rq2
;
848 struct mmc_request
*mrq
, *prev_mrq
;
852 rq1
= mmc_test_req_alloc();
853 rq2
= mmc_test_req_alloc();
862 for (i
= 0; i
< count
; i
++) {
863 mmc_test_req_reset(container_of(mrq
, struct mmc_test_req
, mrq
));
864 mmc_test_prepare_mrq(test
, mrq
, sg
, sg_len
, dev_addr
, blocks
,
866 ret
= mmc_test_start_areq(test
, mrq
, prev_mrq
);
871 prev_mrq
= &rq2
->mrq
;
877 ret
= mmc_test_start_areq(test
, NULL
, prev_mrq
);
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 timespec64 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 ktime_get_ts64(&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 ktime_get_ts64(&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 timespec64 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 ktime_get_ts64(&ts1
);
1754 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9, MMC_TRIM_ARG
);
1757 ktime_get_ts64(&ts2
);
1758 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1760 dev_addr
= t
->dev_addr
;
1761 ktime_get_ts64(&ts1
);
1762 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9, MMC_TRIM_ARG
);
1765 ktime_get_ts64(&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 timespec64 ts1
, ts2
;
1777 cnt
= t
->max_sz
/ sz
;
1778 dev_addr
= t
->dev_addr
;
1779 ktime_get_ts64(&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 ktime_get_ts64(&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 timespec64 ts1
, ts2
;
1816 ret
= mmc_test_area_erase(test
);
1819 cnt
= t
->max_sz
/ sz
;
1820 dev_addr
= t
->dev_addr
;
1821 ktime_get_ts64(&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 ktime_get_ts64(&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 timespec64 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 ktime_get_ts64(&ts1
);
1878 for (i
= 0; i
< cnt
; i
++) {
1879 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9,
1883 dev_addr
+= (sz
>> 9);
1885 ktime_get_ts64(&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 timespec64 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 ktime_get_ts64(&ts1
);
1917 for (cnt
= 0; cnt
< UINT_MAX
; cnt
++) {
1918 ktime_get_ts64(&ts2
);
1919 ts
= timespec64_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 timespec64 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 ktime_get_ts64(&ts1
);
2021 for (i
= 0; i
< cnt
; i
++) {
2022 ret
= mmc_test_area_io(test
, sz
, dev_addr
, write
,
2028 ktime_get_ts64(&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 * Reset will re-enable the card's command queue, but tests
2326 * expect it to be disabled.
2328 if (card
->ext_csd
.cmdq_en
)
2329 mmc_cmdq_disable(card
);
2331 } else if (err
== -EOPNOTSUPP
) {
2332 return RESULT_UNSUP_HOST
;
2338 static int mmc_test_send_status(struct mmc_test_card
*test
,
2339 struct mmc_command
*cmd
)
2341 memset(cmd
, 0, sizeof(*cmd
));
2343 cmd
->opcode
= MMC_SEND_STATUS
;
2344 if (!mmc_host_is_spi(test
->card
->host
))
2345 cmd
->arg
= test
->card
->rca
<< 16;
2346 cmd
->flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
2348 return mmc_wait_for_cmd(test
->card
->host
, cmd
, 0);
2351 static int mmc_test_ongoing_transfer(struct mmc_test_card
*test
,
2352 unsigned int dev_addr
, int use_sbc
,
2353 int repeat_cmd
, int write
, int use_areq
)
2355 struct mmc_test_req
*rq
= mmc_test_req_alloc();
2356 struct mmc_host
*host
= test
->card
->host
;
2357 struct mmc_test_area
*t
= &test
->area
;
2358 struct mmc_request
*mrq
;
2359 unsigned long timeout
;
2360 bool expired
= false;
2361 int ret
= 0, cmd_ret
;
2370 mrq
->sbc
= &rq
->sbc
;
2371 mrq
->cap_cmd_during_tfr
= true;
2373 mmc_test_prepare_mrq(test
, mrq
, t
->sg
, t
->sg_len
, dev_addr
, t
->blocks
,
2376 if (use_sbc
&& t
->blocks
> 1 && !mrq
->sbc
) {
2377 ret
= mmc_host_cmd23(host
) ?
2383 /* Start ongoing data request */
2385 ret
= mmc_test_start_areq(test
, mrq
, NULL
);
2389 mmc_wait_for_req(host
, mrq
);
2392 timeout
= jiffies
+ msecs_to_jiffies(3000);
2396 /* Send status command while data transfer in progress */
2397 cmd_ret
= mmc_test_send_status(test
, &rq
->status
);
2401 status
= rq
->status
.resp
[0];
2402 if (status
& R1_ERROR
) {
2407 if (mmc_is_req_done(host
, mrq
))
2410 expired
= time_after(jiffies
, timeout
);
2412 pr_info("%s: timeout waiting for Tran state status %#x\n",
2413 mmc_hostname(host
), status
);
2414 cmd_ret
= -ETIMEDOUT
;
2417 } while (repeat_cmd
&& R1_CURRENT_STATE(status
) != R1_STATE_TRAN
);
2419 /* Wait for data request to complete */
2421 ret
= mmc_test_start_areq(test
, NULL
, mrq
);
2423 mmc_wait_for_req_done(test
->card
->host
, mrq
);
2427 * For cap_cmd_during_tfr request, upper layer must send stop if
2430 if (mrq
->data
->stop
&& (mrq
->data
->error
|| !mrq
->sbc
)) {
2432 mmc_wait_for_cmd(host
, mrq
->data
->stop
, 0);
2434 ret
= mmc_wait_for_cmd(host
, mrq
->data
->stop
, 0);
2441 pr_info("%s: Send Status failed: status %#x, error %d\n",
2442 mmc_hostname(test
->card
->host
), status
, cmd_ret
);
2445 ret
= mmc_test_check_result(test
, mrq
);
2449 ret
= mmc_test_wait_busy(test
);
2453 if (repeat_cmd
&& (t
->blocks
+ 1) << 9 > t
->max_tfr
)
2454 pr_info("%s: %d commands completed during transfer of %u blocks\n",
2455 mmc_hostname(test
->card
->host
), count
, t
->blocks
);
2465 static int __mmc_test_cmds_during_tfr(struct mmc_test_card
*test
,
2466 unsigned long sz
, int use_sbc
, int write
,
2469 struct mmc_test_area
*t
= &test
->area
;
2472 if (!(test
->card
->host
->caps
& MMC_CAP_CMD_DURING_TFR
))
2473 return RESULT_UNSUP_HOST
;
2475 ret
= mmc_test_area_map(test
, sz
, 0, 0);
2479 ret
= mmc_test_ongoing_transfer(test
, t
->dev_addr
, use_sbc
, 0, write
,
2484 return mmc_test_ongoing_transfer(test
, t
->dev_addr
, use_sbc
, 1, write
,
2488 static int mmc_test_cmds_during_tfr(struct mmc_test_card
*test
, int use_sbc
,
2489 int write
, int use_areq
)
2491 struct mmc_test_area
*t
= &test
->area
;
2495 for (sz
= 512; sz
<= t
->max_tfr
; sz
+= 512) {
2496 ret
= __mmc_test_cmds_during_tfr(test
, sz
, use_sbc
, write
,
2505 * Commands during read - no Set Block Count (CMD23).
2507 static int mmc_test_cmds_during_read(struct mmc_test_card
*test
)
2509 return mmc_test_cmds_during_tfr(test
, 0, 0, 0);
2513 * Commands during write - no Set Block Count (CMD23).
2515 static int mmc_test_cmds_during_write(struct mmc_test_card
*test
)
2517 return mmc_test_cmds_during_tfr(test
, 0, 1, 0);
2521 * Commands during read - use Set Block Count (CMD23).
2523 static int mmc_test_cmds_during_read_cmd23(struct mmc_test_card
*test
)
2525 return mmc_test_cmds_during_tfr(test
, 1, 0, 0);
2529 * Commands during write - use Set Block Count (CMD23).
2531 static int mmc_test_cmds_during_write_cmd23(struct mmc_test_card
*test
)
2533 return mmc_test_cmds_during_tfr(test
, 1, 1, 0);
2537 * Commands during non-blocking read - use Set Block Count (CMD23).
2539 static int mmc_test_cmds_during_read_cmd23_nonblock(struct mmc_test_card
*test
)
2541 return mmc_test_cmds_during_tfr(test
, 1, 0, 1);
2545 * Commands during non-blocking write - use Set Block Count (CMD23).
2547 static int mmc_test_cmds_during_write_cmd23_nonblock(struct mmc_test_card
*test
)
2549 return mmc_test_cmds_during_tfr(test
, 1, 1, 1);
2552 static const struct mmc_test_case mmc_test_cases
[] = {
2554 .name
= "Basic write (no data verification)",
2555 .run
= mmc_test_basic_write
,
2559 .name
= "Basic read (no data verification)",
2560 .run
= mmc_test_basic_read
,
2564 .name
= "Basic write (with data verification)",
2565 .prepare
= mmc_test_prepare_write
,
2566 .run
= mmc_test_verify_write
,
2567 .cleanup
= mmc_test_cleanup
,
2571 .name
= "Basic read (with data verification)",
2572 .prepare
= mmc_test_prepare_read
,
2573 .run
= mmc_test_verify_read
,
2574 .cleanup
= mmc_test_cleanup
,
2578 .name
= "Multi-block write",
2579 .prepare
= mmc_test_prepare_write
,
2580 .run
= mmc_test_multi_write
,
2581 .cleanup
= mmc_test_cleanup
,
2585 .name
= "Multi-block read",
2586 .prepare
= mmc_test_prepare_read
,
2587 .run
= mmc_test_multi_read
,
2588 .cleanup
= mmc_test_cleanup
,
2592 .name
= "Power of two block writes",
2593 .prepare
= mmc_test_prepare_write
,
2594 .run
= mmc_test_pow2_write
,
2595 .cleanup
= mmc_test_cleanup
,
2599 .name
= "Power of two block reads",
2600 .prepare
= mmc_test_prepare_read
,
2601 .run
= mmc_test_pow2_read
,
2602 .cleanup
= mmc_test_cleanup
,
2606 .name
= "Weird sized block writes",
2607 .prepare
= mmc_test_prepare_write
,
2608 .run
= mmc_test_weird_write
,
2609 .cleanup
= mmc_test_cleanup
,
2613 .name
= "Weird sized block reads",
2614 .prepare
= mmc_test_prepare_read
,
2615 .run
= mmc_test_weird_read
,
2616 .cleanup
= mmc_test_cleanup
,
2620 .name
= "Badly aligned write",
2621 .prepare
= mmc_test_prepare_write
,
2622 .run
= mmc_test_align_write
,
2623 .cleanup
= mmc_test_cleanup
,
2627 .name
= "Badly aligned read",
2628 .prepare
= mmc_test_prepare_read
,
2629 .run
= mmc_test_align_read
,
2630 .cleanup
= mmc_test_cleanup
,
2634 .name
= "Badly aligned multi-block write",
2635 .prepare
= mmc_test_prepare_write
,
2636 .run
= mmc_test_align_multi_write
,
2637 .cleanup
= mmc_test_cleanup
,
2641 .name
= "Badly aligned multi-block read",
2642 .prepare
= mmc_test_prepare_read
,
2643 .run
= mmc_test_align_multi_read
,
2644 .cleanup
= mmc_test_cleanup
,
2648 .name
= "Correct xfer_size at write (start failure)",
2649 .run
= mmc_test_xfersize_write
,
2653 .name
= "Correct xfer_size at read (start failure)",
2654 .run
= mmc_test_xfersize_read
,
2658 .name
= "Correct xfer_size at write (midway failure)",
2659 .run
= mmc_test_multi_xfersize_write
,
2663 .name
= "Correct xfer_size at read (midway failure)",
2664 .run
= mmc_test_multi_xfersize_read
,
2667 #ifdef CONFIG_HIGHMEM
2670 .name
= "Highmem write",
2671 .prepare
= mmc_test_prepare_write
,
2672 .run
= mmc_test_write_high
,
2673 .cleanup
= mmc_test_cleanup
,
2677 .name
= "Highmem read",
2678 .prepare
= mmc_test_prepare_read
,
2679 .run
= mmc_test_read_high
,
2680 .cleanup
= mmc_test_cleanup
,
2684 .name
= "Multi-block highmem write",
2685 .prepare
= mmc_test_prepare_write
,
2686 .run
= mmc_test_multi_write_high
,
2687 .cleanup
= mmc_test_cleanup
,
2691 .name
= "Multi-block highmem read",
2692 .prepare
= mmc_test_prepare_read
,
2693 .run
= mmc_test_multi_read_high
,
2694 .cleanup
= mmc_test_cleanup
,
2700 .name
= "Highmem write",
2701 .run
= mmc_test_no_highmem
,
2705 .name
= "Highmem read",
2706 .run
= mmc_test_no_highmem
,
2710 .name
= "Multi-block highmem write",
2711 .run
= mmc_test_no_highmem
,
2715 .name
= "Multi-block highmem read",
2716 .run
= mmc_test_no_highmem
,
2719 #endif /* CONFIG_HIGHMEM */
2722 .name
= "Best-case read performance",
2723 .prepare
= mmc_test_area_prepare_fill
,
2724 .run
= mmc_test_best_read_performance
,
2725 .cleanup
= mmc_test_area_cleanup
,
2729 .name
= "Best-case write performance",
2730 .prepare
= mmc_test_area_prepare_erase
,
2731 .run
= mmc_test_best_write_performance
,
2732 .cleanup
= mmc_test_area_cleanup
,
2736 .name
= "Best-case read performance into scattered pages",
2737 .prepare
= mmc_test_area_prepare_fill
,
2738 .run
= mmc_test_best_read_perf_max_scatter
,
2739 .cleanup
= mmc_test_area_cleanup
,
2743 .name
= "Best-case write performance from scattered pages",
2744 .prepare
= mmc_test_area_prepare_erase
,
2745 .run
= mmc_test_best_write_perf_max_scatter
,
2746 .cleanup
= mmc_test_area_cleanup
,
2750 .name
= "Single read performance by transfer size",
2751 .prepare
= mmc_test_area_prepare_fill
,
2752 .run
= mmc_test_profile_read_perf
,
2753 .cleanup
= mmc_test_area_cleanup
,
2757 .name
= "Single write performance by transfer size",
2758 .prepare
= mmc_test_area_prepare
,
2759 .run
= mmc_test_profile_write_perf
,
2760 .cleanup
= mmc_test_area_cleanup
,
2764 .name
= "Single trim performance by transfer size",
2765 .prepare
= mmc_test_area_prepare_fill
,
2766 .run
= mmc_test_profile_trim_perf
,
2767 .cleanup
= mmc_test_area_cleanup
,
2771 .name
= "Consecutive read performance by transfer size",
2772 .prepare
= mmc_test_area_prepare_fill
,
2773 .run
= mmc_test_profile_seq_read_perf
,
2774 .cleanup
= mmc_test_area_cleanup
,
2778 .name
= "Consecutive write performance by transfer size",
2779 .prepare
= mmc_test_area_prepare
,
2780 .run
= mmc_test_profile_seq_write_perf
,
2781 .cleanup
= mmc_test_area_cleanup
,
2785 .name
= "Consecutive trim performance by transfer size",
2786 .prepare
= mmc_test_area_prepare
,
2787 .run
= mmc_test_profile_seq_trim_perf
,
2788 .cleanup
= mmc_test_area_cleanup
,
2792 .name
= "Random read performance by transfer size",
2793 .prepare
= mmc_test_area_prepare
,
2794 .run
= mmc_test_random_read_perf
,
2795 .cleanup
= mmc_test_area_cleanup
,
2799 .name
= "Random write performance by transfer size",
2800 .prepare
= mmc_test_area_prepare
,
2801 .run
= mmc_test_random_write_perf
,
2802 .cleanup
= mmc_test_area_cleanup
,
2806 .name
= "Large sequential read into scattered pages",
2807 .prepare
= mmc_test_area_prepare
,
2808 .run
= mmc_test_large_seq_read_perf
,
2809 .cleanup
= mmc_test_area_cleanup
,
2813 .name
= "Large sequential write from scattered pages",
2814 .prepare
= mmc_test_area_prepare
,
2815 .run
= mmc_test_large_seq_write_perf
,
2816 .cleanup
= mmc_test_area_cleanup
,
2820 .name
= "Write performance with blocking req 4k to 4MB",
2821 .prepare
= mmc_test_area_prepare
,
2822 .run
= mmc_test_profile_mult_write_blocking_perf
,
2823 .cleanup
= mmc_test_area_cleanup
,
2827 .name
= "Write performance with non-blocking req 4k to 4MB",
2828 .prepare
= mmc_test_area_prepare
,
2829 .run
= mmc_test_profile_mult_write_nonblock_perf
,
2830 .cleanup
= mmc_test_area_cleanup
,
2834 .name
= "Read performance with blocking req 4k to 4MB",
2835 .prepare
= mmc_test_area_prepare
,
2836 .run
= mmc_test_profile_mult_read_blocking_perf
,
2837 .cleanup
= mmc_test_area_cleanup
,
2841 .name
= "Read performance with non-blocking req 4k to 4MB",
2842 .prepare
= mmc_test_area_prepare
,
2843 .run
= mmc_test_profile_mult_read_nonblock_perf
,
2844 .cleanup
= mmc_test_area_cleanup
,
2848 .name
= "Write performance blocking req 1 to 512 sg elems",
2849 .prepare
= mmc_test_area_prepare
,
2850 .run
= mmc_test_profile_sglen_wr_blocking_perf
,
2851 .cleanup
= mmc_test_area_cleanup
,
2855 .name
= "Write performance non-blocking req 1 to 512 sg elems",
2856 .prepare
= mmc_test_area_prepare
,
2857 .run
= mmc_test_profile_sglen_wr_nonblock_perf
,
2858 .cleanup
= mmc_test_area_cleanup
,
2862 .name
= "Read performance blocking req 1 to 512 sg elems",
2863 .prepare
= mmc_test_area_prepare
,
2864 .run
= mmc_test_profile_sglen_r_blocking_perf
,
2865 .cleanup
= mmc_test_area_cleanup
,
2869 .name
= "Read performance non-blocking req 1 to 512 sg elems",
2870 .prepare
= mmc_test_area_prepare
,
2871 .run
= mmc_test_profile_sglen_r_nonblock_perf
,
2872 .cleanup
= mmc_test_area_cleanup
,
2876 .name
= "Reset test",
2877 .run
= mmc_test_reset
,
2881 .name
= "Commands during read - no Set Block Count (CMD23)",
2882 .prepare
= mmc_test_area_prepare
,
2883 .run
= mmc_test_cmds_during_read
,
2884 .cleanup
= mmc_test_area_cleanup
,
2888 .name
= "Commands during write - no Set Block Count (CMD23)",
2889 .prepare
= mmc_test_area_prepare
,
2890 .run
= mmc_test_cmds_during_write
,
2891 .cleanup
= mmc_test_area_cleanup
,
2895 .name
= "Commands during read - use Set Block Count (CMD23)",
2896 .prepare
= mmc_test_area_prepare
,
2897 .run
= mmc_test_cmds_during_read_cmd23
,
2898 .cleanup
= mmc_test_area_cleanup
,
2902 .name
= "Commands during write - use Set Block Count (CMD23)",
2903 .prepare
= mmc_test_area_prepare
,
2904 .run
= mmc_test_cmds_during_write_cmd23
,
2905 .cleanup
= mmc_test_area_cleanup
,
2909 .name
= "Commands during non-blocking read - use Set Block Count (CMD23)",
2910 .prepare
= mmc_test_area_prepare
,
2911 .run
= mmc_test_cmds_during_read_cmd23_nonblock
,
2912 .cleanup
= mmc_test_area_cleanup
,
2916 .name
= "Commands during non-blocking write - use Set Block Count (CMD23)",
2917 .prepare
= mmc_test_area_prepare
,
2918 .run
= mmc_test_cmds_during_write_cmd23_nonblock
,
2919 .cleanup
= mmc_test_area_cleanup
,
2923 static DEFINE_MUTEX(mmc_test_lock
);
2925 static LIST_HEAD(mmc_test_result
);
2927 static void mmc_test_run(struct mmc_test_card
*test
, int testcase
)
2931 pr_info("%s: Starting tests of card %s...\n",
2932 mmc_hostname(test
->card
->host
), mmc_card_id(test
->card
));
2934 mmc_claim_host(test
->card
->host
);
2936 for (i
= 0; i
< ARRAY_SIZE(mmc_test_cases
); i
++) {
2937 struct mmc_test_general_result
*gr
;
2939 if (testcase
&& ((i
+ 1) != testcase
))
2942 pr_info("%s: Test case %d. %s...\n",
2943 mmc_hostname(test
->card
->host
), i
+ 1,
2944 mmc_test_cases
[i
].name
);
2946 if (mmc_test_cases
[i
].prepare
) {
2947 ret
= mmc_test_cases
[i
].prepare(test
);
2949 pr_info("%s: Result: Prepare stage failed! (%d)\n",
2950 mmc_hostname(test
->card
->host
),
2956 gr
= kzalloc(sizeof(*gr
), GFP_KERNEL
);
2958 INIT_LIST_HEAD(&gr
->tr_lst
);
2960 /* Assign data what we know already */
2961 gr
->card
= test
->card
;
2964 /* Append container to global one */
2965 list_add_tail(&gr
->link
, &mmc_test_result
);
2968 * Save the pointer to created container in our private
2974 ret
= mmc_test_cases
[i
].run(test
);
2977 pr_info("%s: Result: OK\n",
2978 mmc_hostname(test
->card
->host
));
2981 pr_info("%s: Result: FAILED\n",
2982 mmc_hostname(test
->card
->host
));
2984 case RESULT_UNSUP_HOST
:
2985 pr_info("%s: Result: UNSUPPORTED (by host)\n",
2986 mmc_hostname(test
->card
->host
));
2988 case RESULT_UNSUP_CARD
:
2989 pr_info("%s: Result: UNSUPPORTED (by card)\n",
2990 mmc_hostname(test
->card
->host
));
2993 pr_info("%s: Result: ERROR (%d)\n",
2994 mmc_hostname(test
->card
->host
), ret
);
2997 /* Save the result */
3001 if (mmc_test_cases
[i
].cleanup
) {
3002 ret
= mmc_test_cases
[i
].cleanup(test
);
3004 pr_info("%s: Warning: Cleanup stage failed! (%d)\n",
3005 mmc_hostname(test
->card
->host
),
3011 mmc_release_host(test
->card
->host
);
3013 pr_info("%s: Tests completed.\n",
3014 mmc_hostname(test
->card
->host
));
3017 static void mmc_test_free_result(struct mmc_card
*card
)
3019 struct mmc_test_general_result
*gr
, *grs
;
3021 mutex_lock(&mmc_test_lock
);
3023 list_for_each_entry_safe(gr
, grs
, &mmc_test_result
, link
) {
3024 struct mmc_test_transfer_result
*tr
, *trs
;
3026 if (card
&& gr
->card
!= card
)
3029 list_for_each_entry_safe(tr
, trs
, &gr
->tr_lst
, link
) {
3030 list_del(&tr
->link
);
3034 list_del(&gr
->link
);
3038 mutex_unlock(&mmc_test_lock
);
3041 static LIST_HEAD(mmc_test_file_test
);
3043 static int mtf_test_show(struct seq_file
*sf
, void *data
)
3045 struct mmc_card
*card
= (struct mmc_card
*)sf
->private;
3046 struct mmc_test_general_result
*gr
;
3048 mutex_lock(&mmc_test_lock
);
3050 list_for_each_entry(gr
, &mmc_test_result
, link
) {
3051 struct mmc_test_transfer_result
*tr
;
3053 if (gr
->card
!= card
)
3056 seq_printf(sf
, "Test %d: %d\n", gr
->testcase
+ 1, gr
->result
);
3058 list_for_each_entry(tr
, &gr
->tr_lst
, link
) {
3059 seq_printf(sf
, "%u %d %llu.%09u %u %u.%02u\n",
3060 tr
->count
, tr
->sectors
,
3061 (u64
)tr
->ts
.tv_sec
, (u32
)tr
->ts
.tv_nsec
,
3062 tr
->rate
, tr
->iops
/ 100, tr
->iops
% 100);
3066 mutex_unlock(&mmc_test_lock
);
3071 static int mtf_test_open(struct inode
*inode
, struct file
*file
)
3073 return single_open(file
, mtf_test_show
, inode
->i_private
);
3076 static ssize_t
mtf_test_write(struct file
*file
, const char __user
*buf
,
3077 size_t count
, loff_t
*pos
)
3079 struct seq_file
*sf
= (struct seq_file
*)file
->private_data
;
3080 struct mmc_card
*card
= (struct mmc_card
*)sf
->private;
3081 struct mmc_test_card
*test
;
3085 ret
= kstrtol_from_user(buf
, count
, 10, &testcase
);
3089 test
= kzalloc(sizeof(*test
), GFP_KERNEL
);
3094 * Remove all test cases associated with given card. Thus we have only
3095 * actual data of the last run.
3097 mmc_test_free_result(card
);
3101 test
->buffer
= kzalloc(BUFFER_SIZE
, GFP_KERNEL
);
3102 #ifdef CONFIG_HIGHMEM
3103 test
->highmem
= alloc_pages(GFP_KERNEL
| __GFP_HIGHMEM
, BUFFER_ORDER
);
3106 #ifdef CONFIG_HIGHMEM
3107 if (test
->buffer
&& test
->highmem
) {
3111 mutex_lock(&mmc_test_lock
);
3112 mmc_test_run(test
, testcase
);
3113 mutex_unlock(&mmc_test_lock
);
3116 #ifdef CONFIG_HIGHMEM
3117 __free_pages(test
->highmem
, BUFFER_ORDER
);
3119 kfree(test
->buffer
);
3125 static const struct file_operations mmc_test_fops_test
= {
3126 .open
= mtf_test_open
,
3128 .write
= mtf_test_write
,
3129 .llseek
= seq_lseek
,
3130 .release
= single_release
,
3133 static int mtf_testlist_show(struct seq_file
*sf
, void *data
)
3137 mutex_lock(&mmc_test_lock
);
3139 seq_puts(sf
, "0:\tRun all tests\n");
3140 for (i
= 0; i
< ARRAY_SIZE(mmc_test_cases
); i
++)
3141 seq_printf(sf
, "%d:\t%s\n", i
+ 1, mmc_test_cases
[i
].name
);
3143 mutex_unlock(&mmc_test_lock
);
3148 static int mtf_testlist_open(struct inode
*inode
, struct file
*file
)
3150 return single_open(file
, mtf_testlist_show
, inode
->i_private
);
3153 static const struct file_operations mmc_test_fops_testlist
= {
3154 .open
= mtf_testlist_open
,
3156 .llseek
= seq_lseek
,
3157 .release
= single_release
,
3160 static void mmc_test_free_dbgfs_file(struct mmc_card
*card
)
3162 struct mmc_test_dbgfs_file
*df
, *dfs
;
3164 mutex_lock(&mmc_test_lock
);
3166 list_for_each_entry_safe(df
, dfs
, &mmc_test_file_test
, link
) {
3167 if (card
&& df
->card
!= card
)
3169 debugfs_remove(df
->file
);
3170 list_del(&df
->link
);
3174 mutex_unlock(&mmc_test_lock
);
3177 static int __mmc_test_register_dbgfs_file(struct mmc_card
*card
,
3178 const char *name
, umode_t mode
, const struct file_operations
*fops
)
3180 struct dentry
*file
= NULL
;
3181 struct mmc_test_dbgfs_file
*df
;
3183 if (card
->debugfs_root
)
3184 file
= debugfs_create_file(name
, mode
, card
->debugfs_root
,
3187 if (IS_ERR_OR_NULL(file
)) {
3189 "Can't create %s. Perhaps debugfs is disabled.\n",
3194 df
= kmalloc(sizeof(*df
), GFP_KERNEL
);
3196 debugfs_remove(file
);
3203 list_add(&df
->link
, &mmc_test_file_test
);
3207 static int mmc_test_register_dbgfs_file(struct mmc_card
*card
)
3211 mutex_lock(&mmc_test_lock
);
3213 ret
= __mmc_test_register_dbgfs_file(card
, "test", S_IWUSR
| S_IRUGO
,
3214 &mmc_test_fops_test
);
3218 ret
= __mmc_test_register_dbgfs_file(card
, "testlist", S_IRUGO
,
3219 &mmc_test_fops_testlist
);
3224 mutex_unlock(&mmc_test_lock
);
3229 static int mmc_test_probe(struct mmc_card
*card
)
3233 if (!mmc_card_mmc(card
) && !mmc_card_sd(card
))
3236 ret
= mmc_test_register_dbgfs_file(card
);
3240 if (card
->ext_csd
.cmdq_en
) {
3241 mmc_claim_host(card
->host
);
3242 ret
= mmc_cmdq_disable(card
);
3243 mmc_release_host(card
->host
);
3248 dev_info(&card
->dev
, "Card claimed for testing.\n");
3253 static void mmc_test_remove(struct mmc_card
*card
)
3255 if (card
->reenable_cmdq
) {
3256 mmc_claim_host(card
->host
);
3257 mmc_cmdq_enable(card
);
3258 mmc_release_host(card
->host
);
3260 mmc_test_free_result(card
);
3261 mmc_test_free_dbgfs_file(card
);
3264 static void mmc_test_shutdown(struct mmc_card
*card
)
3268 static struct mmc_driver mmc_driver
= {
3272 .probe
= mmc_test_probe
,
3273 .remove
= mmc_test_remove
,
3274 .shutdown
= mmc_test_shutdown
,
3277 static int __init
mmc_test_init(void)
3279 return mmc_register_driver(&mmc_driver
);
3282 static void __exit
mmc_test_exit(void)
3284 /* Clear stalled data if card is still plugged */
3285 mmc_test_free_result(NULL
);
3286 mmc_test_free_dbgfs_file(NULL
);
3288 mmc_unregister_driver(&mmc_driver
);
3291 module_init(mmc_test_init
);
3292 module_exit(mmc_test_exit
);
3294 MODULE_LICENSE("GPL");
3295 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
3296 MODULE_AUTHOR("Pierre Ossman");