2 * linux/drivers/mmc/card/mmc_test.c
4 * Copyright 2007-2008 Pierre Ossman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
12 #include <linux/mmc/core.h>
13 #include <linux/mmc/card.h>
14 #include <linux/mmc/host.h>
15 #include <linux/mmc/mmc.h>
16 #include <linux/slab.h>
18 #include <linux/scatterlist.h>
19 #include <linux/swap.h> /* For nr_free_buffer_pages() */
20 #include <linux/list.h>
22 #include <linux/debugfs.h>
23 #include <linux/uaccess.h>
24 #include <linux/seq_file.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)
35 * Limit the test area size to the maximum MMC HC erase group size. Note that
36 * the maximum SD allocation unit size is just 4MiB.
38 #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
41 * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
42 * @page: first page in the allocation
43 * @order: order of the number of pages allocated
45 struct mmc_test_pages
{
51 * struct mmc_test_mem - allocated memory.
52 * @arr: array of allocations
53 * @cnt: number of allocations
56 struct mmc_test_pages
*arr
;
61 * struct mmc_test_area - information for performance tests.
62 * @max_sz: test area size (in bytes)
63 * @dev_addr: address on card at which to do performance tests
64 * @max_tfr: maximum transfer size allowed by driver (in bytes)
65 * @max_segs: maximum segments allowed by driver in scatterlist @sg
66 * @max_seg_sz: maximum segment size allowed by driver
67 * @blocks: number of (512 byte) blocks currently mapped by @sg
68 * @sg_len: length of currently mapped scatterlist @sg
69 * @mem: allocated memory
72 struct mmc_test_area
{
74 unsigned int dev_addr
;
76 unsigned int max_segs
;
77 unsigned int max_seg_sz
;
80 struct mmc_test_mem
*mem
;
81 struct scatterlist
*sg
;
85 * struct mmc_test_transfer_result - transfer results for performance tests.
86 * @link: double-linked list
87 * @count: amount of group of sectors to check
88 * @sectors: amount of sectors to check in one group
89 * @ts: time values of transfer
90 * @rate: calculated transfer rate
91 * @iops: I/O operations per second (times 100)
93 struct mmc_test_transfer_result
{
94 struct list_head link
;
103 * struct mmc_test_general_result - results for tests.
104 * @link: double-linked list
105 * @card: card under test
106 * @testcase: number of test case
107 * @result: result of test run
108 * @tr_lst: transfer measurements if any as mmc_test_transfer_result
110 struct mmc_test_general_result
{
111 struct list_head link
;
112 struct mmc_card
*card
;
115 struct list_head tr_lst
;
119 * struct mmc_test_dbgfs_file - debugfs related file.
120 * @link: double-linked list
121 * @card: card under test
122 * @file: file created under debugfs
124 struct mmc_test_dbgfs_file
{
125 struct list_head link
;
126 struct mmc_card
*card
;
131 * struct mmc_test_card - test information.
132 * @card: card under test
133 * @scratch: transfer buffer
134 * @buffer: transfer buffer
135 * @highmem: buffer for highmem tests
136 * @area: information for performance tests
137 * @gr: pointer to results of current testcase
139 struct mmc_test_card
{
140 struct mmc_card
*card
;
142 u8 scratch
[BUFFER_SIZE
];
144 #ifdef CONFIG_HIGHMEM
145 struct page
*highmem
;
147 struct mmc_test_area area
;
148 struct mmc_test_general_result
*gr
;
151 /*******************************************************************/
152 /* General helper functions */
153 /*******************************************************************/
156 * Configure correct block size in card
158 static int mmc_test_set_blksize(struct mmc_test_card
*test
, unsigned size
)
160 return mmc_set_blocklen(test
->card
, size
);
164 * Fill in the mmc_request structure given a set of transfer parameters.
166 static void mmc_test_prepare_mrq(struct mmc_test_card
*test
,
167 struct mmc_request
*mrq
, struct scatterlist
*sg
, unsigned sg_len
,
168 unsigned dev_addr
, unsigned blocks
, unsigned blksz
, int write
)
170 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
|| !mrq
->stop
);
173 mrq
->cmd
->opcode
= write
?
174 MMC_WRITE_MULTIPLE_BLOCK
: MMC_READ_MULTIPLE_BLOCK
;
176 mrq
->cmd
->opcode
= write
?
177 MMC_WRITE_BLOCK
: MMC_READ_SINGLE_BLOCK
;
180 mrq
->cmd
->arg
= dev_addr
;
181 if (!mmc_card_blockaddr(test
->card
))
184 mrq
->cmd
->flags
= MMC_RSP_R1
| MMC_CMD_ADTC
;
189 mrq
->stop
->opcode
= MMC_STOP_TRANSMISSION
;
191 mrq
->stop
->flags
= MMC_RSP_R1B
| MMC_CMD_AC
;
194 mrq
->data
->blksz
= blksz
;
195 mrq
->data
->blocks
= blocks
;
196 mrq
->data
->flags
= write
? MMC_DATA_WRITE
: MMC_DATA_READ
;
198 mrq
->data
->sg_len
= sg_len
;
200 mmc_set_data_timeout(mrq
->data
, test
->card
);
203 static int mmc_test_busy(struct mmc_command
*cmd
)
205 return !(cmd
->resp
[0] & R1_READY_FOR_DATA
) ||
206 (R1_CURRENT_STATE(cmd
->resp
[0]) == 7);
210 * Wait for the card to finish the busy state
212 static int mmc_test_wait_busy(struct mmc_test_card
*test
)
215 struct mmc_command cmd
= {0};
219 memset(&cmd
, 0, sizeof(struct mmc_command
));
221 cmd
.opcode
= MMC_SEND_STATUS
;
222 cmd
.arg
= test
->card
->rca
<< 16;
223 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
225 ret
= mmc_wait_for_cmd(test
->card
->host
, &cmd
, 0);
229 if (!busy
&& mmc_test_busy(&cmd
)) {
231 if (test
->card
->host
->caps
& MMC_CAP_WAIT_WHILE_BUSY
)
232 printk(KERN_INFO
"%s: Warning: Host did not "
233 "wait for busy state to end.\n",
234 mmc_hostname(test
->card
->host
));
236 } while (mmc_test_busy(&cmd
));
242 * Transfer a single sector of kernel addressable data
244 static int mmc_test_buffer_transfer(struct mmc_test_card
*test
,
245 u8
*buffer
, unsigned addr
, unsigned blksz
, int write
)
249 struct mmc_request mrq
= {0};
250 struct mmc_command cmd
= {0};
251 struct mmc_command stop
= {0};
252 struct mmc_data data
= {0};
254 struct scatterlist sg
;
260 sg_init_one(&sg
, buffer
, blksz
);
262 mmc_test_prepare_mrq(test
, &mrq
, &sg
, 1, addr
, 1, blksz
, write
);
264 mmc_wait_for_req(test
->card
->host
, &mrq
);
271 ret
= mmc_test_wait_busy(test
);
278 static void mmc_test_free_mem(struct mmc_test_mem
*mem
)
283 __free_pages(mem
->arr
[mem
->cnt
].page
,
284 mem
->arr
[mem
->cnt
].order
);
290 * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
291 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
292 * not exceed a maximum number of segments and try not to make segments much
293 * bigger than maximum segment size.
295 static struct mmc_test_mem
*mmc_test_alloc_mem(unsigned long min_sz
,
296 unsigned long max_sz
,
297 unsigned int max_segs
,
298 unsigned int max_seg_sz
)
300 unsigned long max_page_cnt
= DIV_ROUND_UP(max_sz
, PAGE_SIZE
);
301 unsigned long min_page_cnt
= DIV_ROUND_UP(min_sz
, PAGE_SIZE
);
302 unsigned long max_seg_page_cnt
= DIV_ROUND_UP(max_seg_sz
, PAGE_SIZE
);
303 unsigned long page_cnt
= 0;
304 unsigned long limit
= nr_free_buffer_pages() >> 4;
305 struct mmc_test_mem
*mem
;
307 if (max_page_cnt
> limit
)
308 max_page_cnt
= limit
;
309 if (min_page_cnt
> max_page_cnt
)
310 min_page_cnt
= max_page_cnt
;
312 if (max_seg_page_cnt
> max_page_cnt
)
313 max_seg_page_cnt
= max_page_cnt
;
315 if (max_segs
> max_page_cnt
)
316 max_segs
= max_page_cnt
;
318 mem
= kzalloc(sizeof(struct mmc_test_mem
), GFP_KERNEL
);
322 mem
->arr
= kzalloc(sizeof(struct mmc_test_pages
) * max_segs
,
327 while (max_page_cnt
) {
330 gfp_t flags
= GFP_KERNEL
| GFP_DMA
| __GFP_NOWARN
|
333 order
= get_order(max_seg_page_cnt
<< PAGE_SHIFT
);
335 page
= alloc_pages(flags
, order
);
341 if (page_cnt
< min_page_cnt
)
345 mem
->arr
[mem
->cnt
].page
= page
;
346 mem
->arr
[mem
->cnt
].order
= order
;
348 if (max_page_cnt
<= (1UL << order
))
350 max_page_cnt
-= 1UL << order
;
351 page_cnt
+= 1UL << order
;
352 if (mem
->cnt
>= max_segs
) {
353 if (page_cnt
< min_page_cnt
)
362 mmc_test_free_mem(mem
);
367 * Map memory into a scatterlist. Optionally allow the same memory to be
368 * mapped more than once.
370 static int mmc_test_map_sg(struct mmc_test_mem
*mem
, unsigned long sz
,
371 struct scatterlist
*sglist
, int repeat
,
372 unsigned int max_segs
, unsigned int max_seg_sz
,
373 unsigned int *sg_len
)
375 struct scatterlist
*sg
= NULL
;
378 sg_init_table(sglist
, max_segs
);
382 for (i
= 0; i
< mem
->cnt
; i
++) {
383 unsigned long len
= PAGE_SIZE
<< mem
->arr
[i
].order
;
387 if (len
> max_seg_sz
)
395 sg_set_page(sg
, mem
->arr
[i
].page
, len
, 0);
401 } while (sz
&& repeat
);
413 * Map memory into a scatterlist so that no pages are contiguous. Allow the
414 * same memory to be mapped more than once.
416 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem
*mem
,
418 struct scatterlist
*sglist
,
419 unsigned int max_segs
,
420 unsigned int max_seg_sz
,
421 unsigned int *sg_len
)
423 struct scatterlist
*sg
= NULL
;
424 unsigned int i
= mem
->cnt
, cnt
;
426 void *base
, *addr
, *last_addr
= NULL
;
428 sg_init_table(sglist
, max_segs
);
432 base
= page_address(mem
->arr
[--i
].page
);
433 cnt
= 1 << mem
->arr
[i
].order
;
435 addr
= base
+ PAGE_SIZE
* --cnt
;
436 if (last_addr
&& last_addr
+ PAGE_SIZE
== addr
)
440 if (len
> max_seg_sz
)
450 sg_set_page(sg
, virt_to_page(addr
), len
, 0);
465 * Calculate transfer rate in bytes per second.
467 static unsigned int mmc_test_rate(uint64_t bytes
, struct timespec
*ts
)
477 while (ns
> UINT_MAX
) {
485 do_div(bytes
, (uint32_t)ns
);
491 * Save transfer results for future usage
493 static void mmc_test_save_transfer_result(struct mmc_test_card
*test
,
494 unsigned int count
, unsigned int sectors
, struct timespec ts
,
495 unsigned int rate
, unsigned int iops
)
497 struct mmc_test_transfer_result
*tr
;
502 tr
= kmalloc(sizeof(struct mmc_test_transfer_result
), GFP_KERNEL
);
507 tr
->sectors
= sectors
;
512 list_add_tail(&tr
->link
, &test
->gr
->tr_lst
);
516 * Print the transfer rate.
518 static void mmc_test_print_rate(struct mmc_test_card
*test
, uint64_t bytes
,
519 struct timespec
*ts1
, struct timespec
*ts2
)
521 unsigned int rate
, iops
, sectors
= bytes
>> 9;
524 ts
= timespec_sub(*ts2
, *ts1
);
526 rate
= mmc_test_rate(bytes
, &ts
);
527 iops
= mmc_test_rate(100, &ts
); /* I/O ops per sec x 100 */
529 printk(KERN_INFO
"%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
530 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
531 mmc_hostname(test
->card
->host
), sectors
, sectors
>> 1,
532 (sectors
& 1 ? ".5" : ""), (unsigned long)ts
.tv_sec
,
533 (unsigned long)ts
.tv_nsec
, rate
/ 1000, rate
/ 1024,
534 iops
/ 100, iops
% 100);
536 mmc_test_save_transfer_result(test
, 1, sectors
, ts
, rate
, iops
);
540 * Print the average transfer rate.
542 static void mmc_test_print_avg_rate(struct mmc_test_card
*test
, uint64_t bytes
,
543 unsigned int count
, struct timespec
*ts1
,
544 struct timespec
*ts2
)
546 unsigned int rate
, iops
, sectors
= bytes
>> 9;
547 uint64_t tot
= bytes
* count
;
550 ts
= timespec_sub(*ts2
, *ts1
);
552 rate
= mmc_test_rate(tot
, &ts
);
553 iops
= mmc_test_rate(count
* 100, &ts
); /* I/O ops per sec x 100 */
555 printk(KERN_INFO
"%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
556 "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
558 mmc_hostname(test
->card
->host
), count
, sectors
, count
,
559 sectors
>> 1, (sectors
& 1 ? ".5" : ""),
560 (unsigned long)ts
.tv_sec
, (unsigned long)ts
.tv_nsec
,
561 rate
/ 1000, rate
/ 1024, iops
/ 100, iops
% 100);
563 mmc_test_save_transfer_result(test
, count
, sectors
, ts
, rate
, iops
);
567 * Return the card size in sectors.
569 static unsigned int mmc_test_capacity(struct mmc_card
*card
)
571 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
))
572 return card
->ext_csd
.sectors
;
574 return card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9);
577 /*******************************************************************/
578 /* Test preparation and cleanup */
579 /*******************************************************************/
582 * Fill the first couple of sectors of the card with known data
583 * so that bad reads/writes can be detected
585 static int __mmc_test_prepare(struct mmc_test_card
*test
, int write
)
589 ret
= mmc_test_set_blksize(test
, 512);
594 memset(test
->buffer
, 0xDF, 512);
596 for (i
= 0;i
< 512;i
++)
600 for (i
= 0;i
< BUFFER_SIZE
/ 512;i
++) {
601 ret
= mmc_test_buffer_transfer(test
, test
->buffer
, i
, 512, 1);
609 static int mmc_test_prepare_write(struct mmc_test_card
*test
)
611 return __mmc_test_prepare(test
, 1);
614 static int mmc_test_prepare_read(struct mmc_test_card
*test
)
616 return __mmc_test_prepare(test
, 0);
619 static int mmc_test_cleanup(struct mmc_test_card
*test
)
623 ret
= mmc_test_set_blksize(test
, 512);
627 memset(test
->buffer
, 0, 512);
629 for (i
= 0;i
< BUFFER_SIZE
/ 512;i
++) {
630 ret
= mmc_test_buffer_transfer(test
, test
->buffer
, i
, 512, 1);
638 /*******************************************************************/
639 /* Test execution helpers */
640 /*******************************************************************/
643 * Modifies the mmc_request to perform the "short transfer" tests
645 static void mmc_test_prepare_broken_mrq(struct mmc_test_card
*test
,
646 struct mmc_request
*mrq
, int write
)
648 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
);
650 if (mrq
->data
->blocks
> 1) {
651 mrq
->cmd
->opcode
= write
?
652 MMC_WRITE_BLOCK
: MMC_READ_SINGLE_BLOCK
;
655 mrq
->cmd
->opcode
= MMC_SEND_STATUS
;
656 mrq
->cmd
->arg
= test
->card
->rca
<< 16;
661 * Checks that a normal transfer didn't have any errors
663 static int mmc_test_check_result(struct mmc_test_card
*test
,
664 struct mmc_request
*mrq
)
668 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
);
672 if (!ret
&& mrq
->cmd
->error
)
673 ret
= mrq
->cmd
->error
;
674 if (!ret
&& mrq
->data
->error
)
675 ret
= mrq
->data
->error
;
676 if (!ret
&& mrq
->stop
&& mrq
->stop
->error
)
677 ret
= mrq
->stop
->error
;
678 if (!ret
&& mrq
->data
->bytes_xfered
!=
679 mrq
->data
->blocks
* mrq
->data
->blksz
)
683 ret
= RESULT_UNSUP_HOST
;
689 * Checks that a "short transfer" behaved as expected
691 static int mmc_test_check_broken_result(struct mmc_test_card
*test
,
692 struct mmc_request
*mrq
)
696 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
);
700 if (!ret
&& mrq
->cmd
->error
)
701 ret
= mrq
->cmd
->error
;
702 if (!ret
&& mrq
->data
->error
== 0)
704 if (!ret
&& mrq
->data
->error
!= -ETIMEDOUT
)
705 ret
= mrq
->data
->error
;
706 if (!ret
&& mrq
->stop
&& mrq
->stop
->error
)
707 ret
= mrq
->stop
->error
;
708 if (mrq
->data
->blocks
> 1) {
709 if (!ret
&& mrq
->data
->bytes_xfered
> mrq
->data
->blksz
)
712 if (!ret
&& mrq
->data
->bytes_xfered
> 0)
717 ret
= RESULT_UNSUP_HOST
;
723 * Tests a basic transfer with certain parameters
725 static int mmc_test_simple_transfer(struct mmc_test_card
*test
,
726 struct scatterlist
*sg
, unsigned sg_len
, unsigned dev_addr
,
727 unsigned blocks
, unsigned blksz
, int write
)
729 struct mmc_request mrq
= {0};
730 struct mmc_command cmd
= {0};
731 struct mmc_command stop
= {0};
732 struct mmc_data data
= {0};
738 mmc_test_prepare_mrq(test
, &mrq
, sg
, sg_len
, dev_addr
,
739 blocks
, blksz
, write
);
741 mmc_wait_for_req(test
->card
->host
, &mrq
);
743 mmc_test_wait_busy(test
);
745 return mmc_test_check_result(test
, &mrq
);
749 * Tests a transfer where the card will fail completely or partly
751 static int mmc_test_broken_transfer(struct mmc_test_card
*test
,
752 unsigned blocks
, unsigned blksz
, int write
)
754 struct mmc_request mrq
= {0};
755 struct mmc_command cmd
= {0};
756 struct mmc_command stop
= {0};
757 struct mmc_data data
= {0};
759 struct scatterlist sg
;
765 sg_init_one(&sg
, test
->buffer
, blocks
* blksz
);
767 mmc_test_prepare_mrq(test
, &mrq
, &sg
, 1, 0, blocks
, blksz
, write
);
768 mmc_test_prepare_broken_mrq(test
, &mrq
, write
);
770 mmc_wait_for_req(test
->card
->host
, &mrq
);
772 mmc_test_wait_busy(test
);
774 return mmc_test_check_broken_result(test
, &mrq
);
778 * Does a complete transfer test where data is also validated
780 * Note: mmc_test_prepare() must have been done before this call
782 static int mmc_test_transfer(struct mmc_test_card
*test
,
783 struct scatterlist
*sg
, unsigned sg_len
, unsigned dev_addr
,
784 unsigned blocks
, unsigned blksz
, int write
)
790 for (i
= 0;i
< blocks
* blksz
;i
++)
791 test
->scratch
[i
] = i
;
793 memset(test
->scratch
, 0, BUFFER_SIZE
);
795 local_irq_save(flags
);
796 sg_copy_from_buffer(sg
, sg_len
, test
->scratch
, BUFFER_SIZE
);
797 local_irq_restore(flags
);
799 ret
= mmc_test_set_blksize(test
, blksz
);
803 ret
= mmc_test_simple_transfer(test
, sg
, sg_len
, dev_addr
,
804 blocks
, blksz
, write
);
811 ret
= mmc_test_set_blksize(test
, 512);
815 sectors
= (blocks
* blksz
+ 511) / 512;
816 if ((sectors
* 512) == (blocks
* blksz
))
819 if ((sectors
* 512) > BUFFER_SIZE
)
822 memset(test
->buffer
, 0, sectors
* 512);
824 for (i
= 0;i
< sectors
;i
++) {
825 ret
= mmc_test_buffer_transfer(test
,
826 test
->buffer
+ i
* 512,
827 dev_addr
+ i
, 512, 0);
832 for (i
= 0;i
< blocks
* blksz
;i
++) {
833 if (test
->buffer
[i
] != (u8
)i
)
837 for (;i
< sectors
* 512;i
++) {
838 if (test
->buffer
[i
] != 0xDF)
842 local_irq_save(flags
);
843 sg_copy_to_buffer(sg
, sg_len
, test
->scratch
, BUFFER_SIZE
);
844 local_irq_restore(flags
);
845 for (i
= 0;i
< blocks
* blksz
;i
++) {
846 if (test
->scratch
[i
] != (u8
)i
)
854 /*******************************************************************/
856 /*******************************************************************/
858 struct mmc_test_case
{
861 int (*prepare
)(struct mmc_test_card
*);
862 int (*run
)(struct mmc_test_card
*);
863 int (*cleanup
)(struct mmc_test_card
*);
866 static int mmc_test_basic_write(struct mmc_test_card
*test
)
869 struct scatterlist sg
;
871 ret
= mmc_test_set_blksize(test
, 512);
875 sg_init_one(&sg
, test
->buffer
, 512);
877 ret
= mmc_test_simple_transfer(test
, &sg
, 1, 0, 1, 512, 1);
884 static int mmc_test_basic_read(struct mmc_test_card
*test
)
887 struct scatterlist sg
;
889 ret
= mmc_test_set_blksize(test
, 512);
893 sg_init_one(&sg
, test
->buffer
, 512);
895 ret
= mmc_test_simple_transfer(test
, &sg
, 1, 0, 1, 512, 0);
902 static int mmc_test_verify_write(struct mmc_test_card
*test
)
905 struct scatterlist sg
;
907 sg_init_one(&sg
, test
->buffer
, 512);
909 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 1);
916 static int mmc_test_verify_read(struct mmc_test_card
*test
)
919 struct scatterlist sg
;
921 sg_init_one(&sg
, test
->buffer
, 512);
923 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 0);
930 static int mmc_test_multi_write(struct mmc_test_card
*test
)
934 struct scatterlist sg
;
936 if (test
->card
->host
->max_blk_count
== 1)
937 return RESULT_UNSUP_HOST
;
939 size
= PAGE_SIZE
* 2;
940 size
= min(size
, test
->card
->host
->max_req_size
);
941 size
= min(size
, test
->card
->host
->max_seg_size
);
942 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
945 return RESULT_UNSUP_HOST
;
947 sg_init_one(&sg
, test
->buffer
, size
);
949 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 1);
956 static int mmc_test_multi_read(struct mmc_test_card
*test
)
960 struct scatterlist sg
;
962 if (test
->card
->host
->max_blk_count
== 1)
963 return RESULT_UNSUP_HOST
;
965 size
= PAGE_SIZE
* 2;
966 size
= min(size
, test
->card
->host
->max_req_size
);
967 size
= min(size
, test
->card
->host
->max_seg_size
);
968 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
971 return RESULT_UNSUP_HOST
;
973 sg_init_one(&sg
, test
->buffer
, size
);
975 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 0);
982 static int mmc_test_pow2_write(struct mmc_test_card
*test
)
985 struct scatterlist sg
;
987 if (!test
->card
->csd
.write_partial
)
988 return RESULT_UNSUP_CARD
;
990 for (i
= 1; i
< 512;i
<<= 1) {
991 sg_init_one(&sg
, test
->buffer
, i
);
992 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 1);
1000 static int mmc_test_pow2_read(struct mmc_test_card
*test
)
1003 struct scatterlist sg
;
1005 if (!test
->card
->csd
.read_partial
)
1006 return RESULT_UNSUP_CARD
;
1008 for (i
= 1; i
< 512;i
<<= 1) {
1009 sg_init_one(&sg
, test
->buffer
, i
);
1010 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 0);
1018 static int mmc_test_weird_write(struct mmc_test_card
*test
)
1021 struct scatterlist sg
;
1023 if (!test
->card
->csd
.write_partial
)
1024 return RESULT_UNSUP_CARD
;
1026 for (i
= 3; i
< 512;i
+= 7) {
1027 sg_init_one(&sg
, test
->buffer
, i
);
1028 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 1);
1036 static int mmc_test_weird_read(struct mmc_test_card
*test
)
1039 struct scatterlist sg
;
1041 if (!test
->card
->csd
.read_partial
)
1042 return RESULT_UNSUP_CARD
;
1044 for (i
= 3; i
< 512;i
+= 7) {
1045 sg_init_one(&sg
, test
->buffer
, i
);
1046 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 0);
1054 static int mmc_test_align_write(struct mmc_test_card
*test
)
1057 struct scatterlist sg
;
1059 for (i
= 1;i
< 4;i
++) {
1060 sg_init_one(&sg
, test
->buffer
+ i
, 512);
1061 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 1);
1069 static int mmc_test_align_read(struct mmc_test_card
*test
)
1072 struct scatterlist sg
;
1074 for (i
= 1;i
< 4;i
++) {
1075 sg_init_one(&sg
, test
->buffer
+ i
, 512);
1076 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 0);
1084 static int mmc_test_align_multi_write(struct mmc_test_card
*test
)
1088 struct scatterlist sg
;
1090 if (test
->card
->host
->max_blk_count
== 1)
1091 return RESULT_UNSUP_HOST
;
1093 size
= PAGE_SIZE
* 2;
1094 size
= min(size
, test
->card
->host
->max_req_size
);
1095 size
= min(size
, test
->card
->host
->max_seg_size
);
1096 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1099 return RESULT_UNSUP_HOST
;
1101 for (i
= 1;i
< 4;i
++) {
1102 sg_init_one(&sg
, test
->buffer
+ i
, size
);
1103 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 1);
1111 static int mmc_test_align_multi_read(struct mmc_test_card
*test
)
1115 struct scatterlist sg
;
1117 if (test
->card
->host
->max_blk_count
== 1)
1118 return RESULT_UNSUP_HOST
;
1120 size
= PAGE_SIZE
* 2;
1121 size
= min(size
, test
->card
->host
->max_req_size
);
1122 size
= min(size
, test
->card
->host
->max_seg_size
);
1123 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1126 return RESULT_UNSUP_HOST
;
1128 for (i
= 1;i
< 4;i
++) {
1129 sg_init_one(&sg
, test
->buffer
+ i
, size
);
1130 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 0);
1138 static int mmc_test_xfersize_write(struct mmc_test_card
*test
)
1142 ret
= mmc_test_set_blksize(test
, 512);
1146 ret
= mmc_test_broken_transfer(test
, 1, 512, 1);
1153 static int mmc_test_xfersize_read(struct mmc_test_card
*test
)
1157 ret
= mmc_test_set_blksize(test
, 512);
1161 ret
= mmc_test_broken_transfer(test
, 1, 512, 0);
1168 static int mmc_test_multi_xfersize_write(struct mmc_test_card
*test
)
1172 if (test
->card
->host
->max_blk_count
== 1)
1173 return RESULT_UNSUP_HOST
;
1175 ret
= mmc_test_set_blksize(test
, 512);
1179 ret
= mmc_test_broken_transfer(test
, 2, 512, 1);
1186 static int mmc_test_multi_xfersize_read(struct mmc_test_card
*test
)
1190 if (test
->card
->host
->max_blk_count
== 1)
1191 return RESULT_UNSUP_HOST
;
1193 ret
= mmc_test_set_blksize(test
, 512);
1197 ret
= mmc_test_broken_transfer(test
, 2, 512, 0);
1204 #ifdef CONFIG_HIGHMEM
1206 static int mmc_test_write_high(struct mmc_test_card
*test
)
1209 struct scatterlist sg
;
1211 sg_init_table(&sg
, 1);
1212 sg_set_page(&sg
, test
->highmem
, 512, 0);
1214 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 1);
1221 static int mmc_test_read_high(struct mmc_test_card
*test
)
1224 struct scatterlist sg
;
1226 sg_init_table(&sg
, 1);
1227 sg_set_page(&sg
, test
->highmem
, 512, 0);
1229 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 0);
1236 static int mmc_test_multi_write_high(struct mmc_test_card
*test
)
1240 struct scatterlist sg
;
1242 if (test
->card
->host
->max_blk_count
== 1)
1243 return RESULT_UNSUP_HOST
;
1245 size
= PAGE_SIZE
* 2;
1246 size
= min(size
, test
->card
->host
->max_req_size
);
1247 size
= min(size
, test
->card
->host
->max_seg_size
);
1248 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1251 return RESULT_UNSUP_HOST
;
1253 sg_init_table(&sg
, 1);
1254 sg_set_page(&sg
, test
->highmem
, size
, 0);
1256 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 1);
1263 static int mmc_test_multi_read_high(struct mmc_test_card
*test
)
1267 struct scatterlist sg
;
1269 if (test
->card
->host
->max_blk_count
== 1)
1270 return RESULT_UNSUP_HOST
;
1272 size
= PAGE_SIZE
* 2;
1273 size
= min(size
, test
->card
->host
->max_req_size
);
1274 size
= min(size
, test
->card
->host
->max_seg_size
);
1275 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1278 return RESULT_UNSUP_HOST
;
1280 sg_init_table(&sg
, 1);
1281 sg_set_page(&sg
, test
->highmem
, size
, 0);
1283 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 0);
1292 static int mmc_test_no_highmem(struct mmc_test_card
*test
)
1294 printk(KERN_INFO
"%s: Highmem not configured - test skipped\n",
1295 mmc_hostname(test
->card
->host
));
1299 #endif /* CONFIG_HIGHMEM */
1302 * Map sz bytes so that it can be transferred.
1304 static int mmc_test_area_map(struct mmc_test_card
*test
, unsigned long sz
,
1307 struct mmc_test_area
*t
= &test
->area
;
1310 t
->blocks
= sz
>> 9;
1313 err
= mmc_test_map_sg_max_scatter(t
->mem
, sz
, t
->sg
,
1314 t
->max_segs
, t
->max_seg_sz
,
1317 err
= mmc_test_map_sg(t
->mem
, sz
, t
->sg
, 1, t
->max_segs
,
1318 t
->max_seg_sz
, &t
->sg_len
);
1321 printk(KERN_INFO
"%s: Failed to map sg list\n",
1322 mmc_hostname(test
->card
->host
));
1327 * Transfer bytes mapped by mmc_test_area_map().
1329 static int mmc_test_area_transfer(struct mmc_test_card
*test
,
1330 unsigned int dev_addr
, int write
)
1332 struct mmc_test_area
*t
= &test
->area
;
1334 return mmc_test_simple_transfer(test
, t
->sg
, t
->sg_len
, dev_addr
,
1335 t
->blocks
, 512, write
);
1339 * Map and transfer bytes.
1341 static int mmc_test_area_io(struct mmc_test_card
*test
, unsigned long sz
,
1342 unsigned int dev_addr
, int write
, int max_scatter
,
1345 struct timespec ts1
, ts2
;
1349 * In the case of a maximally scattered transfer, the maximum transfer
1350 * size is further limited by using PAGE_SIZE segments.
1353 struct mmc_test_area
*t
= &test
->area
;
1354 unsigned long max_tfr
;
1356 if (t
->max_seg_sz
>= PAGE_SIZE
)
1357 max_tfr
= t
->max_segs
* PAGE_SIZE
;
1359 max_tfr
= t
->max_segs
* t
->max_seg_sz
;
1364 ret
= mmc_test_area_map(test
, sz
, max_scatter
);
1369 getnstimeofday(&ts1
);
1371 ret
= mmc_test_area_transfer(test
, dev_addr
, write
);
1376 getnstimeofday(&ts2
);
1379 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1385 * Write the test area entirely.
1387 static int mmc_test_area_fill(struct mmc_test_card
*test
)
1389 struct mmc_test_area
*t
= &test
->area
;
1391 return mmc_test_area_io(test
, t
->max_tfr
, t
->dev_addr
, 1, 0, 0);
1395 * Erase the test area entirely.
1397 static int mmc_test_area_erase(struct mmc_test_card
*test
)
1399 struct mmc_test_area
*t
= &test
->area
;
1401 if (!mmc_can_erase(test
->card
))
1404 return mmc_erase(test
->card
, t
->dev_addr
, t
->max_sz
>> 9,
1409 * Cleanup struct mmc_test_area.
1411 static int mmc_test_area_cleanup(struct mmc_test_card
*test
)
1413 struct mmc_test_area
*t
= &test
->area
;
1416 mmc_test_free_mem(t
->mem
);
1422 * Initialize an area for testing large transfers. The test area is set to the
1423 * middle of the card because cards may have different charateristics at the
1424 * front (for FAT file system optimization). Optionally, the area is erased
1425 * (if the card supports it) which may improve write performance. Optionally,
1426 * the area is filled with data for subsequent read tests.
1428 static int mmc_test_area_init(struct mmc_test_card
*test
, int erase
, int fill
)
1430 struct mmc_test_area
*t
= &test
->area
;
1431 unsigned long min_sz
= 64 * 1024, sz
;
1434 ret
= mmc_test_set_blksize(test
, 512);
1438 /* Make the test area size about 4MiB */
1439 sz
= (unsigned long)test
->card
->pref_erase
<< 9;
1441 while (t
->max_sz
< 4 * 1024 * 1024)
1443 while (t
->max_sz
> TEST_AREA_MAX_SIZE
&& t
->max_sz
> sz
)
1446 t
->max_segs
= test
->card
->host
->max_segs
;
1447 t
->max_seg_sz
= test
->card
->host
->max_seg_size
;
1449 t
->max_tfr
= t
->max_sz
;
1450 if (t
->max_tfr
>> 9 > test
->card
->host
->max_blk_count
)
1451 t
->max_tfr
= test
->card
->host
->max_blk_count
<< 9;
1452 if (t
->max_tfr
> test
->card
->host
->max_req_size
)
1453 t
->max_tfr
= test
->card
->host
->max_req_size
;
1454 if (t
->max_tfr
/ t
->max_seg_sz
> t
->max_segs
)
1455 t
->max_tfr
= t
->max_segs
* t
->max_seg_sz
;
1458 * Try to allocate enough memory for a max. sized transfer. Less is OK
1459 * because the same memory can be mapped into the scatterlist more than
1460 * once. Also, take into account the limits imposed on scatterlist
1461 * segments by the host driver.
1463 t
->mem
= mmc_test_alloc_mem(min_sz
, t
->max_tfr
, t
->max_segs
,
1468 t
->sg
= kmalloc(sizeof(struct scatterlist
) * t
->max_segs
, GFP_KERNEL
);
1474 t
->dev_addr
= mmc_test_capacity(test
->card
) / 2;
1475 t
->dev_addr
-= t
->dev_addr
% (t
->max_sz
>> 9);
1478 ret
= mmc_test_area_erase(test
);
1484 ret
= mmc_test_area_fill(test
);
1492 mmc_test_area_cleanup(test
);
1497 * Prepare for large transfers. Do not erase the test area.
1499 static int mmc_test_area_prepare(struct mmc_test_card
*test
)
1501 return mmc_test_area_init(test
, 0, 0);
1505 * Prepare for large transfers. Do erase the test area.
1507 static int mmc_test_area_prepare_erase(struct mmc_test_card
*test
)
1509 return mmc_test_area_init(test
, 1, 0);
1513 * Prepare for large transfers. Erase and fill the test area.
1515 static int mmc_test_area_prepare_fill(struct mmc_test_card
*test
)
1517 return mmc_test_area_init(test
, 1, 1);
1521 * Test best-case performance. Best-case performance is expected from
1522 * a single large transfer.
1524 * An additional option (max_scatter) allows the measurement of the same
1525 * transfer but with no contiguous pages in the scatter list. This tests
1526 * the efficiency of DMA to handle scattered pages.
1528 static int mmc_test_best_performance(struct mmc_test_card
*test
, int write
,
1531 struct mmc_test_area
*t
= &test
->area
;
1533 return mmc_test_area_io(test
, t
->max_tfr
, t
->dev_addr
, write
,
1538 * Best-case read performance.
1540 static int mmc_test_best_read_performance(struct mmc_test_card
*test
)
1542 return mmc_test_best_performance(test
, 0, 0);
1546 * Best-case write performance.
1548 static int mmc_test_best_write_performance(struct mmc_test_card
*test
)
1550 return mmc_test_best_performance(test
, 1, 0);
1554 * Best-case read performance into scattered pages.
1556 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card
*test
)
1558 return mmc_test_best_performance(test
, 0, 1);
1562 * Best-case write performance from scattered pages.
1564 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card
*test
)
1566 return mmc_test_best_performance(test
, 1, 1);
1570 * Single read performance by transfer size.
1572 static int mmc_test_profile_read_perf(struct mmc_test_card
*test
)
1574 struct mmc_test_area
*t
= &test
->area
;
1576 unsigned int dev_addr
;
1579 for (sz
= 512; sz
< t
->max_tfr
; sz
<<= 1) {
1580 dev_addr
= t
->dev_addr
+ (sz
>> 9);
1581 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 0, 0, 1);
1586 dev_addr
= t
->dev_addr
;
1587 return mmc_test_area_io(test
, sz
, dev_addr
, 0, 0, 1);
1591 * Single write performance by transfer size.
1593 static int mmc_test_profile_write_perf(struct mmc_test_card
*test
)
1595 struct mmc_test_area
*t
= &test
->area
;
1597 unsigned int dev_addr
;
1600 ret
= mmc_test_area_erase(test
);
1603 for (sz
= 512; sz
< t
->max_tfr
; sz
<<= 1) {
1604 dev_addr
= t
->dev_addr
+ (sz
>> 9);
1605 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 1, 0, 1);
1609 ret
= mmc_test_area_erase(test
);
1613 dev_addr
= t
->dev_addr
;
1614 return mmc_test_area_io(test
, sz
, dev_addr
, 1, 0, 1);
1618 * Single trim performance by transfer size.
1620 static int mmc_test_profile_trim_perf(struct mmc_test_card
*test
)
1622 struct mmc_test_area
*t
= &test
->area
;
1624 unsigned int dev_addr
;
1625 struct timespec ts1
, ts2
;
1628 if (!mmc_can_trim(test
->card
))
1629 return RESULT_UNSUP_CARD
;
1631 if (!mmc_can_erase(test
->card
))
1632 return RESULT_UNSUP_HOST
;
1634 for (sz
= 512; sz
< t
->max_sz
; sz
<<= 1) {
1635 dev_addr
= t
->dev_addr
+ (sz
>> 9);
1636 getnstimeofday(&ts1
);
1637 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9, MMC_TRIM_ARG
);
1640 getnstimeofday(&ts2
);
1641 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1643 dev_addr
= t
->dev_addr
;
1644 getnstimeofday(&ts1
);
1645 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9, MMC_TRIM_ARG
);
1648 getnstimeofday(&ts2
);
1649 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1653 static int mmc_test_seq_read_perf(struct mmc_test_card
*test
, unsigned long sz
)
1655 struct mmc_test_area
*t
= &test
->area
;
1656 unsigned int dev_addr
, i
, cnt
;
1657 struct timespec ts1
, ts2
;
1660 cnt
= t
->max_sz
/ sz
;
1661 dev_addr
= t
->dev_addr
;
1662 getnstimeofday(&ts1
);
1663 for (i
= 0; i
< cnt
; i
++) {
1664 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 0, 0, 0);
1667 dev_addr
+= (sz
>> 9);
1669 getnstimeofday(&ts2
);
1670 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1675 * Consecutive read performance by transfer size.
1677 static int mmc_test_profile_seq_read_perf(struct mmc_test_card
*test
)
1679 struct mmc_test_area
*t
= &test
->area
;
1683 for (sz
= 512; sz
< t
->max_tfr
; sz
<<= 1) {
1684 ret
= mmc_test_seq_read_perf(test
, sz
);
1689 return mmc_test_seq_read_perf(test
, sz
);
1692 static int mmc_test_seq_write_perf(struct mmc_test_card
*test
, unsigned long sz
)
1694 struct mmc_test_area
*t
= &test
->area
;
1695 unsigned int dev_addr
, i
, cnt
;
1696 struct timespec ts1
, ts2
;
1699 ret
= mmc_test_area_erase(test
);
1702 cnt
= t
->max_sz
/ sz
;
1703 dev_addr
= t
->dev_addr
;
1704 getnstimeofday(&ts1
);
1705 for (i
= 0; i
< cnt
; i
++) {
1706 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 1, 0, 0);
1709 dev_addr
+= (sz
>> 9);
1711 getnstimeofday(&ts2
);
1712 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1717 * Consecutive write performance by transfer size.
1719 static int mmc_test_profile_seq_write_perf(struct mmc_test_card
*test
)
1721 struct mmc_test_area
*t
= &test
->area
;
1725 for (sz
= 512; sz
< t
->max_tfr
; sz
<<= 1) {
1726 ret
= mmc_test_seq_write_perf(test
, sz
);
1731 return mmc_test_seq_write_perf(test
, sz
);
1735 * Consecutive trim performance by transfer size.
1737 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card
*test
)
1739 struct mmc_test_area
*t
= &test
->area
;
1741 unsigned int dev_addr
, i
, cnt
;
1742 struct timespec ts1
, ts2
;
1745 if (!mmc_can_trim(test
->card
))
1746 return RESULT_UNSUP_CARD
;
1748 if (!mmc_can_erase(test
->card
))
1749 return RESULT_UNSUP_HOST
;
1751 for (sz
= 512; sz
<= t
->max_sz
; sz
<<= 1) {
1752 ret
= mmc_test_area_erase(test
);
1755 ret
= mmc_test_area_fill(test
);
1758 cnt
= t
->max_sz
/ sz
;
1759 dev_addr
= t
->dev_addr
;
1760 getnstimeofday(&ts1
);
1761 for (i
= 0; i
< cnt
; i
++) {
1762 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9,
1766 dev_addr
+= (sz
>> 9);
1768 getnstimeofday(&ts2
);
1769 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1774 static unsigned int rnd_next
= 1;
1776 static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt
)
1780 rnd_next
= rnd_next
* 1103515245 + 12345;
1781 r
= (rnd_next
>> 16) & 0x7fff;
1782 return (r
* rnd_cnt
) >> 15;
1785 static int mmc_test_rnd_perf(struct mmc_test_card
*test
, int write
, int print
,
1788 unsigned int dev_addr
, cnt
, rnd_addr
, range1
, range2
, last_ea
= 0, ea
;
1790 struct timespec ts1
, ts2
, ts
;
1795 rnd_addr
= mmc_test_capacity(test
->card
) / 4;
1796 range1
= rnd_addr
/ test
->card
->pref_erase
;
1797 range2
= range1
/ ssz
;
1799 getnstimeofday(&ts1
);
1800 for (cnt
= 0; cnt
< UINT_MAX
; cnt
++) {
1801 getnstimeofday(&ts2
);
1802 ts
= timespec_sub(ts2
, ts1
);
1803 if (ts
.tv_sec
>= 10)
1805 ea
= mmc_test_rnd_num(range1
);
1809 dev_addr
= rnd_addr
+ test
->card
->pref_erase
* ea
+
1810 ssz
* mmc_test_rnd_num(range2
);
1811 ret
= mmc_test_area_io(test
, sz
, dev_addr
, write
, 0, 0);
1816 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1820 static int mmc_test_random_perf(struct mmc_test_card
*test
, int write
)
1822 struct mmc_test_area
*t
= &test
->area
;
1827 for (sz
= 512; sz
< t
->max_tfr
; sz
<<= 1) {
1829 * When writing, try to get more consistent results by running
1830 * the test twice with exactly the same I/O but outputting the
1831 * results only for the 2nd run.
1835 ret
= mmc_test_rnd_perf(test
, write
, 0, sz
);
1840 ret
= mmc_test_rnd_perf(test
, write
, 1, sz
);
1847 ret
= mmc_test_rnd_perf(test
, write
, 0, sz
);
1852 return mmc_test_rnd_perf(test
, write
, 1, sz
);
1856 * Random read performance by transfer size.
1858 static int mmc_test_random_read_perf(struct mmc_test_card
*test
)
1860 return mmc_test_random_perf(test
, 0);
1864 * Random write performance by transfer size.
1866 static int mmc_test_random_write_perf(struct mmc_test_card
*test
)
1868 return mmc_test_random_perf(test
, 1);
1871 static int mmc_test_seq_perf(struct mmc_test_card
*test
, int write
,
1872 unsigned int tot_sz
, int max_scatter
)
1874 struct mmc_test_area
*t
= &test
->area
;
1875 unsigned int dev_addr
, i
, cnt
, sz
, ssz
;
1876 struct timespec ts1
, ts2
;
1882 * In the case of a maximally scattered transfer, the maximum transfer
1883 * size is further limited by using PAGE_SIZE segments.
1886 unsigned long max_tfr
;
1888 if (t
->max_seg_sz
>= PAGE_SIZE
)
1889 max_tfr
= t
->max_segs
* PAGE_SIZE
;
1891 max_tfr
= t
->max_segs
* t
->max_seg_sz
;
1897 dev_addr
= mmc_test_capacity(test
->card
) / 4;
1898 if (tot_sz
> dev_addr
<< 9)
1899 tot_sz
= dev_addr
<< 9;
1901 dev_addr
&= 0xffff0000; /* Round to 64MiB boundary */
1903 getnstimeofday(&ts1
);
1904 for (i
= 0; i
< cnt
; i
++) {
1905 ret
= mmc_test_area_io(test
, sz
, dev_addr
, write
,
1911 getnstimeofday(&ts2
);
1913 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1918 static int mmc_test_large_seq_perf(struct mmc_test_card
*test
, int write
)
1922 for (i
= 0; i
< 10; i
++) {
1923 ret
= mmc_test_seq_perf(test
, write
, 10 * 1024 * 1024, 1);
1927 for (i
= 0; i
< 5; i
++) {
1928 ret
= mmc_test_seq_perf(test
, write
, 100 * 1024 * 1024, 1);
1932 for (i
= 0; i
< 3; i
++) {
1933 ret
= mmc_test_seq_perf(test
, write
, 1000 * 1024 * 1024, 1);
1942 * Large sequential read performance.
1944 static int mmc_test_large_seq_read_perf(struct mmc_test_card
*test
)
1946 return mmc_test_large_seq_perf(test
, 0);
1950 * Large sequential write performance.
1952 static int mmc_test_large_seq_write_perf(struct mmc_test_card
*test
)
1954 return mmc_test_large_seq_perf(test
, 1);
1957 static const struct mmc_test_case mmc_test_cases
[] = {
1959 .name
= "Basic write (no data verification)",
1960 .run
= mmc_test_basic_write
,
1964 .name
= "Basic read (no data verification)",
1965 .run
= mmc_test_basic_read
,
1969 .name
= "Basic write (with data verification)",
1970 .prepare
= mmc_test_prepare_write
,
1971 .run
= mmc_test_verify_write
,
1972 .cleanup
= mmc_test_cleanup
,
1976 .name
= "Basic read (with data verification)",
1977 .prepare
= mmc_test_prepare_read
,
1978 .run
= mmc_test_verify_read
,
1979 .cleanup
= mmc_test_cleanup
,
1983 .name
= "Multi-block write",
1984 .prepare
= mmc_test_prepare_write
,
1985 .run
= mmc_test_multi_write
,
1986 .cleanup
= mmc_test_cleanup
,
1990 .name
= "Multi-block read",
1991 .prepare
= mmc_test_prepare_read
,
1992 .run
= mmc_test_multi_read
,
1993 .cleanup
= mmc_test_cleanup
,
1997 .name
= "Power of two block writes",
1998 .prepare
= mmc_test_prepare_write
,
1999 .run
= mmc_test_pow2_write
,
2000 .cleanup
= mmc_test_cleanup
,
2004 .name
= "Power of two block reads",
2005 .prepare
= mmc_test_prepare_read
,
2006 .run
= mmc_test_pow2_read
,
2007 .cleanup
= mmc_test_cleanup
,
2011 .name
= "Weird sized block writes",
2012 .prepare
= mmc_test_prepare_write
,
2013 .run
= mmc_test_weird_write
,
2014 .cleanup
= mmc_test_cleanup
,
2018 .name
= "Weird sized block reads",
2019 .prepare
= mmc_test_prepare_read
,
2020 .run
= mmc_test_weird_read
,
2021 .cleanup
= mmc_test_cleanup
,
2025 .name
= "Badly aligned write",
2026 .prepare
= mmc_test_prepare_write
,
2027 .run
= mmc_test_align_write
,
2028 .cleanup
= mmc_test_cleanup
,
2032 .name
= "Badly aligned read",
2033 .prepare
= mmc_test_prepare_read
,
2034 .run
= mmc_test_align_read
,
2035 .cleanup
= mmc_test_cleanup
,
2039 .name
= "Badly aligned multi-block write",
2040 .prepare
= mmc_test_prepare_write
,
2041 .run
= mmc_test_align_multi_write
,
2042 .cleanup
= mmc_test_cleanup
,
2046 .name
= "Badly aligned multi-block read",
2047 .prepare
= mmc_test_prepare_read
,
2048 .run
= mmc_test_align_multi_read
,
2049 .cleanup
= mmc_test_cleanup
,
2053 .name
= "Correct xfer_size at write (start failure)",
2054 .run
= mmc_test_xfersize_write
,
2058 .name
= "Correct xfer_size at read (start failure)",
2059 .run
= mmc_test_xfersize_read
,
2063 .name
= "Correct xfer_size at write (midway failure)",
2064 .run
= mmc_test_multi_xfersize_write
,
2068 .name
= "Correct xfer_size at read (midway failure)",
2069 .run
= mmc_test_multi_xfersize_read
,
2072 #ifdef CONFIG_HIGHMEM
2075 .name
= "Highmem write",
2076 .prepare
= mmc_test_prepare_write
,
2077 .run
= mmc_test_write_high
,
2078 .cleanup
= mmc_test_cleanup
,
2082 .name
= "Highmem read",
2083 .prepare
= mmc_test_prepare_read
,
2084 .run
= mmc_test_read_high
,
2085 .cleanup
= mmc_test_cleanup
,
2089 .name
= "Multi-block highmem write",
2090 .prepare
= mmc_test_prepare_write
,
2091 .run
= mmc_test_multi_write_high
,
2092 .cleanup
= mmc_test_cleanup
,
2096 .name
= "Multi-block highmem read",
2097 .prepare
= mmc_test_prepare_read
,
2098 .run
= mmc_test_multi_read_high
,
2099 .cleanup
= mmc_test_cleanup
,
2105 .name
= "Highmem write",
2106 .run
= mmc_test_no_highmem
,
2110 .name
= "Highmem read",
2111 .run
= mmc_test_no_highmem
,
2115 .name
= "Multi-block highmem write",
2116 .run
= mmc_test_no_highmem
,
2120 .name
= "Multi-block highmem read",
2121 .run
= mmc_test_no_highmem
,
2124 #endif /* CONFIG_HIGHMEM */
2127 .name
= "Best-case read performance",
2128 .prepare
= mmc_test_area_prepare_fill
,
2129 .run
= mmc_test_best_read_performance
,
2130 .cleanup
= mmc_test_area_cleanup
,
2134 .name
= "Best-case write performance",
2135 .prepare
= mmc_test_area_prepare_erase
,
2136 .run
= mmc_test_best_write_performance
,
2137 .cleanup
= mmc_test_area_cleanup
,
2141 .name
= "Best-case read performance into scattered pages",
2142 .prepare
= mmc_test_area_prepare_fill
,
2143 .run
= mmc_test_best_read_perf_max_scatter
,
2144 .cleanup
= mmc_test_area_cleanup
,
2148 .name
= "Best-case write performance from scattered pages",
2149 .prepare
= mmc_test_area_prepare_erase
,
2150 .run
= mmc_test_best_write_perf_max_scatter
,
2151 .cleanup
= mmc_test_area_cleanup
,
2155 .name
= "Single read performance by transfer size",
2156 .prepare
= mmc_test_area_prepare_fill
,
2157 .run
= mmc_test_profile_read_perf
,
2158 .cleanup
= mmc_test_area_cleanup
,
2162 .name
= "Single write performance by transfer size",
2163 .prepare
= mmc_test_area_prepare
,
2164 .run
= mmc_test_profile_write_perf
,
2165 .cleanup
= mmc_test_area_cleanup
,
2169 .name
= "Single trim performance by transfer size",
2170 .prepare
= mmc_test_area_prepare_fill
,
2171 .run
= mmc_test_profile_trim_perf
,
2172 .cleanup
= mmc_test_area_cleanup
,
2176 .name
= "Consecutive read performance by transfer size",
2177 .prepare
= mmc_test_area_prepare_fill
,
2178 .run
= mmc_test_profile_seq_read_perf
,
2179 .cleanup
= mmc_test_area_cleanup
,
2183 .name
= "Consecutive write performance by transfer size",
2184 .prepare
= mmc_test_area_prepare
,
2185 .run
= mmc_test_profile_seq_write_perf
,
2186 .cleanup
= mmc_test_area_cleanup
,
2190 .name
= "Consecutive trim performance by transfer size",
2191 .prepare
= mmc_test_area_prepare
,
2192 .run
= mmc_test_profile_seq_trim_perf
,
2193 .cleanup
= mmc_test_area_cleanup
,
2197 .name
= "Random read performance by transfer size",
2198 .prepare
= mmc_test_area_prepare
,
2199 .run
= mmc_test_random_read_perf
,
2200 .cleanup
= mmc_test_area_cleanup
,
2204 .name
= "Random write performance by transfer size",
2205 .prepare
= mmc_test_area_prepare
,
2206 .run
= mmc_test_random_write_perf
,
2207 .cleanup
= mmc_test_area_cleanup
,
2211 .name
= "Large sequential read into scattered pages",
2212 .prepare
= mmc_test_area_prepare
,
2213 .run
= mmc_test_large_seq_read_perf
,
2214 .cleanup
= mmc_test_area_cleanup
,
2218 .name
= "Large sequential write from scattered pages",
2219 .prepare
= mmc_test_area_prepare
,
2220 .run
= mmc_test_large_seq_write_perf
,
2221 .cleanup
= mmc_test_area_cleanup
,
2226 static DEFINE_MUTEX(mmc_test_lock
);
2228 static LIST_HEAD(mmc_test_result
);
2230 static void mmc_test_run(struct mmc_test_card
*test
, int testcase
)
2234 printk(KERN_INFO
"%s: Starting tests of card %s...\n",
2235 mmc_hostname(test
->card
->host
), mmc_card_id(test
->card
));
2237 mmc_claim_host(test
->card
->host
);
2239 for (i
= 0;i
< ARRAY_SIZE(mmc_test_cases
);i
++) {
2240 struct mmc_test_general_result
*gr
;
2242 if (testcase
&& ((i
+ 1) != testcase
))
2245 printk(KERN_INFO
"%s: Test case %d. %s...\n",
2246 mmc_hostname(test
->card
->host
), i
+ 1,
2247 mmc_test_cases
[i
].name
);
2249 if (mmc_test_cases
[i
].prepare
) {
2250 ret
= mmc_test_cases
[i
].prepare(test
);
2252 printk(KERN_INFO
"%s: Result: Prepare "
2253 "stage failed! (%d)\n",
2254 mmc_hostname(test
->card
->host
),
2260 gr
= kzalloc(sizeof(struct mmc_test_general_result
),
2263 INIT_LIST_HEAD(&gr
->tr_lst
);
2265 /* Assign data what we know already */
2266 gr
->card
= test
->card
;
2269 /* Append container to global one */
2270 list_add_tail(&gr
->link
, &mmc_test_result
);
2273 * Save the pointer to created container in our private
2279 ret
= mmc_test_cases
[i
].run(test
);
2282 printk(KERN_INFO
"%s: Result: OK\n",
2283 mmc_hostname(test
->card
->host
));
2286 printk(KERN_INFO
"%s: Result: FAILED\n",
2287 mmc_hostname(test
->card
->host
));
2289 case RESULT_UNSUP_HOST
:
2290 printk(KERN_INFO
"%s: Result: UNSUPPORTED "
2292 mmc_hostname(test
->card
->host
));
2294 case RESULT_UNSUP_CARD
:
2295 printk(KERN_INFO
"%s: Result: UNSUPPORTED "
2297 mmc_hostname(test
->card
->host
));
2300 printk(KERN_INFO
"%s: Result: ERROR (%d)\n",
2301 mmc_hostname(test
->card
->host
), ret
);
2304 /* Save the result */
2308 if (mmc_test_cases
[i
].cleanup
) {
2309 ret
= mmc_test_cases
[i
].cleanup(test
);
2311 printk(KERN_INFO
"%s: Warning: Cleanup "
2312 "stage failed! (%d)\n",
2313 mmc_hostname(test
->card
->host
),
2319 mmc_release_host(test
->card
->host
);
2321 printk(KERN_INFO
"%s: Tests completed.\n",
2322 mmc_hostname(test
->card
->host
));
2325 static void mmc_test_free_result(struct mmc_card
*card
)
2327 struct mmc_test_general_result
*gr
, *grs
;
2329 mutex_lock(&mmc_test_lock
);
2331 list_for_each_entry_safe(gr
, grs
, &mmc_test_result
, link
) {
2332 struct mmc_test_transfer_result
*tr
, *trs
;
2334 if (card
&& gr
->card
!= card
)
2337 list_for_each_entry_safe(tr
, trs
, &gr
->tr_lst
, link
) {
2338 list_del(&tr
->link
);
2342 list_del(&gr
->link
);
2346 mutex_unlock(&mmc_test_lock
);
2349 static LIST_HEAD(mmc_test_file_test
);
2351 static int mtf_test_show(struct seq_file
*sf
, void *data
)
2353 struct mmc_card
*card
= (struct mmc_card
*)sf
->private;
2354 struct mmc_test_general_result
*gr
;
2356 mutex_lock(&mmc_test_lock
);
2358 list_for_each_entry(gr
, &mmc_test_result
, link
) {
2359 struct mmc_test_transfer_result
*tr
;
2361 if (gr
->card
!= card
)
2364 seq_printf(sf
, "Test %d: %d\n", gr
->testcase
+ 1, gr
->result
);
2366 list_for_each_entry(tr
, &gr
->tr_lst
, link
) {
2367 seq_printf(sf
, "%u %d %lu.%09lu %u %u.%02u\n",
2368 tr
->count
, tr
->sectors
,
2369 (unsigned long)tr
->ts
.tv_sec
,
2370 (unsigned long)tr
->ts
.tv_nsec
,
2371 tr
->rate
, tr
->iops
/ 100, tr
->iops
% 100);
2375 mutex_unlock(&mmc_test_lock
);
2380 static int mtf_test_open(struct inode
*inode
, struct file
*file
)
2382 return single_open(file
, mtf_test_show
, inode
->i_private
);
2385 static ssize_t
mtf_test_write(struct file
*file
, const char __user
*buf
,
2386 size_t count
, loff_t
*pos
)
2388 struct seq_file
*sf
= (struct seq_file
*)file
->private_data
;
2389 struct mmc_card
*card
= (struct mmc_card
*)sf
->private;
2390 struct mmc_test_card
*test
;
2394 if (count
>= sizeof(lbuf
))
2397 if (copy_from_user(lbuf
, buf
, count
))
2401 if (strict_strtol(lbuf
, 10, &testcase
))
2404 test
= kzalloc(sizeof(struct mmc_test_card
), GFP_KERNEL
);
2409 * Remove all test cases associated with given card. Thus we have only
2410 * actual data of the last run.
2412 mmc_test_free_result(card
);
2416 test
->buffer
= kzalloc(BUFFER_SIZE
, GFP_KERNEL
);
2417 #ifdef CONFIG_HIGHMEM
2418 test
->highmem
= alloc_pages(GFP_KERNEL
| __GFP_HIGHMEM
, BUFFER_ORDER
);
2421 #ifdef CONFIG_HIGHMEM
2422 if (test
->buffer
&& test
->highmem
) {
2426 mutex_lock(&mmc_test_lock
);
2427 mmc_test_run(test
, testcase
);
2428 mutex_unlock(&mmc_test_lock
);
2431 #ifdef CONFIG_HIGHMEM
2432 __free_pages(test
->highmem
, BUFFER_ORDER
);
2434 kfree(test
->buffer
);
2440 static const struct file_operations mmc_test_fops_test
= {
2441 .open
= mtf_test_open
,
2443 .write
= mtf_test_write
,
2444 .llseek
= seq_lseek
,
2445 .release
= single_release
,
2448 static void mmc_test_free_file_test(struct mmc_card
*card
)
2450 struct mmc_test_dbgfs_file
*df
, *dfs
;
2452 mutex_lock(&mmc_test_lock
);
2454 list_for_each_entry_safe(df
, dfs
, &mmc_test_file_test
, link
) {
2455 if (card
&& df
->card
!= card
)
2457 debugfs_remove(df
->file
);
2458 list_del(&df
->link
);
2462 mutex_unlock(&mmc_test_lock
);
2465 static int mmc_test_register_file_test(struct mmc_card
*card
)
2467 struct dentry
*file
= NULL
;
2468 struct mmc_test_dbgfs_file
*df
;
2471 mutex_lock(&mmc_test_lock
);
2473 if (card
->debugfs_root
)
2474 file
= debugfs_create_file("test", S_IWUSR
| S_IRUGO
,
2475 card
->debugfs_root
, card
, &mmc_test_fops_test
);
2477 if (IS_ERR_OR_NULL(file
)) {
2479 "Can't create file. Perhaps debugfs is disabled.\n");
2484 df
= kmalloc(sizeof(struct mmc_test_dbgfs_file
), GFP_KERNEL
);
2486 debugfs_remove(file
);
2488 "Can't allocate memory for internal usage.\n");
2496 list_add(&df
->link
, &mmc_test_file_test
);
2499 mutex_unlock(&mmc_test_lock
);
2504 static int mmc_test_probe(struct mmc_card
*card
)
2508 if (!mmc_card_mmc(card
) && !mmc_card_sd(card
))
2511 ret
= mmc_test_register_file_test(card
);
2515 dev_info(&card
->dev
, "Card claimed for testing.\n");
2520 static void mmc_test_remove(struct mmc_card
*card
)
2522 mmc_test_free_result(card
);
2523 mmc_test_free_file_test(card
);
2526 static struct mmc_driver mmc_driver
= {
2530 .probe
= mmc_test_probe
,
2531 .remove
= mmc_test_remove
,
2534 static int __init
mmc_test_init(void)
2536 return mmc_register_driver(&mmc_driver
);
2539 static void __exit
mmc_test_exit(void)
2541 /* Clear stalled data if card is still plugged */
2542 mmc_test_free_result(NULL
);
2543 mmc_test_free_file_test(NULL
);
2545 mmc_unregister_driver(&mmc_driver
);
2548 module_init(mmc_test_init
);
2549 module_exit(mmc_test_exit
);
2551 MODULE_LICENSE("GPL");
2552 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
2553 MODULE_AUTHOR("Pierre Ossman");