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
;
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
;
250 struct mmc_command cmd
;
251 struct mmc_command stop
;
252 struct mmc_data data
;
254 struct scatterlist sg
;
256 memset(&mrq
, 0, sizeof(struct mmc_request
));
257 memset(&cmd
, 0, sizeof(struct mmc_command
));
258 memset(&data
, 0, sizeof(struct mmc_data
));
259 memset(&stop
, 0, sizeof(struct mmc_command
));
265 sg_init_one(&sg
, buffer
, blksz
);
267 mmc_test_prepare_mrq(test
, &mrq
, &sg
, 1, addr
, 1, blksz
, write
);
269 mmc_wait_for_req(test
->card
->host
, &mrq
);
276 ret
= mmc_test_wait_busy(test
);
283 static void mmc_test_free_mem(struct mmc_test_mem
*mem
)
288 __free_pages(mem
->arr
[mem
->cnt
].page
,
289 mem
->arr
[mem
->cnt
].order
);
295 * Allocate a lot of memory, preferrably max_sz but at least min_sz. In case
296 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
297 * not exceed a maximum number of segments and try not to make segments much
298 * bigger than maximum segment size.
300 static struct mmc_test_mem
*mmc_test_alloc_mem(unsigned long min_sz
,
301 unsigned long max_sz
,
302 unsigned int max_segs
,
303 unsigned int max_seg_sz
)
305 unsigned long max_page_cnt
= DIV_ROUND_UP(max_sz
, PAGE_SIZE
);
306 unsigned long min_page_cnt
= DIV_ROUND_UP(min_sz
, PAGE_SIZE
);
307 unsigned long max_seg_page_cnt
= DIV_ROUND_UP(max_seg_sz
, PAGE_SIZE
);
308 unsigned long page_cnt
= 0;
309 unsigned long limit
= nr_free_buffer_pages() >> 4;
310 struct mmc_test_mem
*mem
;
312 if (max_page_cnt
> limit
)
313 max_page_cnt
= limit
;
314 if (min_page_cnt
> max_page_cnt
)
315 min_page_cnt
= max_page_cnt
;
317 if (max_seg_page_cnt
> max_page_cnt
)
318 max_seg_page_cnt
= max_page_cnt
;
320 if (max_segs
> max_page_cnt
)
321 max_segs
= max_page_cnt
;
323 mem
= kzalloc(sizeof(struct mmc_test_mem
), GFP_KERNEL
);
327 mem
->arr
= kzalloc(sizeof(struct mmc_test_pages
) * max_segs
,
332 while (max_page_cnt
) {
335 gfp_t flags
= GFP_KERNEL
| GFP_DMA
| __GFP_NOWARN
|
338 order
= get_order(max_seg_page_cnt
<< PAGE_SHIFT
);
340 page
= alloc_pages(flags
, order
);
346 if (page_cnt
< min_page_cnt
)
350 mem
->arr
[mem
->cnt
].page
= page
;
351 mem
->arr
[mem
->cnt
].order
= order
;
353 if (max_page_cnt
<= (1UL << order
))
355 max_page_cnt
-= 1UL << order
;
356 page_cnt
+= 1UL << order
;
357 if (mem
->cnt
>= max_segs
) {
358 if (page_cnt
< min_page_cnt
)
367 mmc_test_free_mem(mem
);
372 * Map memory into a scatterlist. Optionally allow the same memory to be
373 * mapped more than once.
375 static int mmc_test_map_sg(struct mmc_test_mem
*mem
, unsigned long sz
,
376 struct scatterlist
*sglist
, int repeat
,
377 unsigned int max_segs
, unsigned int max_seg_sz
,
378 unsigned int *sg_len
)
380 struct scatterlist
*sg
= NULL
;
383 sg_init_table(sglist
, max_segs
);
387 for (i
= 0; i
< mem
->cnt
; i
++) {
388 unsigned long len
= PAGE_SIZE
<< mem
->arr
[i
].order
;
392 if (len
> max_seg_sz
)
400 sg_set_page(sg
, mem
->arr
[i
].page
, len
, 0);
406 } while (sz
&& repeat
);
418 * Map memory into a scatterlist so that no pages are contiguous. Allow the
419 * same memory to be mapped more than once.
421 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem
*mem
,
423 struct scatterlist
*sglist
,
424 unsigned int max_segs
,
425 unsigned int max_seg_sz
,
426 unsigned int *sg_len
)
428 struct scatterlist
*sg
= NULL
;
429 unsigned int i
= mem
->cnt
, cnt
;
431 void *base
, *addr
, *last_addr
= NULL
;
433 sg_init_table(sglist
, max_segs
);
437 base
= page_address(mem
->arr
[--i
].page
);
438 cnt
= 1 << mem
->arr
[i
].order
;
440 addr
= base
+ PAGE_SIZE
* --cnt
;
441 if (last_addr
&& last_addr
+ PAGE_SIZE
== addr
)
445 if (len
> max_seg_sz
)
455 sg_set_page(sg
, virt_to_page(addr
), len
, 0);
470 * Calculate transfer rate in bytes per second.
472 static unsigned int mmc_test_rate(uint64_t bytes
, struct timespec
*ts
)
482 while (ns
> UINT_MAX
) {
490 do_div(bytes
, (uint32_t)ns
);
496 * Save transfer results for future usage
498 static void mmc_test_save_transfer_result(struct mmc_test_card
*test
,
499 unsigned int count
, unsigned int sectors
, struct timespec ts
,
500 unsigned int rate
, unsigned int iops
)
502 struct mmc_test_transfer_result
*tr
;
507 tr
= kmalloc(sizeof(struct mmc_test_transfer_result
), GFP_KERNEL
);
512 tr
->sectors
= sectors
;
517 list_add_tail(&tr
->link
, &test
->gr
->tr_lst
);
521 * Print the transfer rate.
523 static void mmc_test_print_rate(struct mmc_test_card
*test
, uint64_t bytes
,
524 struct timespec
*ts1
, struct timespec
*ts2
)
526 unsigned int rate
, iops
, sectors
= bytes
>> 9;
529 ts
= timespec_sub(*ts2
, *ts1
);
531 rate
= mmc_test_rate(bytes
, &ts
);
532 iops
= mmc_test_rate(100, &ts
); /* I/O ops per sec x 100 */
534 printk(KERN_INFO
"%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
535 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
536 mmc_hostname(test
->card
->host
), sectors
, sectors
>> 1,
537 (sectors
& 1 ? ".5" : ""), (unsigned long)ts
.tv_sec
,
538 (unsigned long)ts
.tv_nsec
, rate
/ 1000, rate
/ 1024,
539 iops
/ 100, iops
% 100);
541 mmc_test_save_transfer_result(test
, 1, sectors
, ts
, rate
, iops
);
545 * Print the average transfer rate.
547 static void mmc_test_print_avg_rate(struct mmc_test_card
*test
, uint64_t bytes
,
548 unsigned int count
, struct timespec
*ts1
,
549 struct timespec
*ts2
)
551 unsigned int rate
, iops
, sectors
= bytes
>> 9;
552 uint64_t tot
= bytes
* count
;
555 ts
= timespec_sub(*ts2
, *ts1
);
557 rate
= mmc_test_rate(tot
, &ts
);
558 iops
= mmc_test_rate(count
* 100, &ts
); /* I/O ops per sec x 100 */
560 printk(KERN_INFO
"%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
561 "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
563 mmc_hostname(test
->card
->host
), count
, sectors
, count
,
564 sectors
>> 1, (sectors
& 1 ? ".5" : ""),
565 (unsigned long)ts
.tv_sec
, (unsigned long)ts
.tv_nsec
,
566 rate
/ 1000, rate
/ 1024, iops
/ 100, iops
% 100);
568 mmc_test_save_transfer_result(test
, count
, sectors
, ts
, rate
, iops
);
572 * Return the card size in sectors.
574 static unsigned int mmc_test_capacity(struct mmc_card
*card
)
576 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
))
577 return card
->ext_csd
.sectors
;
579 return card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9);
582 /*******************************************************************/
583 /* Test preparation and cleanup */
584 /*******************************************************************/
587 * Fill the first couple of sectors of the card with known data
588 * so that bad reads/writes can be detected
590 static int __mmc_test_prepare(struct mmc_test_card
*test
, int write
)
594 ret
= mmc_test_set_blksize(test
, 512);
599 memset(test
->buffer
, 0xDF, 512);
601 for (i
= 0;i
< 512;i
++)
605 for (i
= 0;i
< BUFFER_SIZE
/ 512;i
++) {
606 ret
= mmc_test_buffer_transfer(test
, test
->buffer
, i
, 512, 1);
614 static int mmc_test_prepare_write(struct mmc_test_card
*test
)
616 return __mmc_test_prepare(test
, 1);
619 static int mmc_test_prepare_read(struct mmc_test_card
*test
)
621 return __mmc_test_prepare(test
, 0);
624 static int mmc_test_cleanup(struct mmc_test_card
*test
)
628 ret
= mmc_test_set_blksize(test
, 512);
632 memset(test
->buffer
, 0, 512);
634 for (i
= 0;i
< BUFFER_SIZE
/ 512;i
++) {
635 ret
= mmc_test_buffer_transfer(test
, test
->buffer
, i
, 512, 1);
643 /*******************************************************************/
644 /* Test execution helpers */
645 /*******************************************************************/
648 * Modifies the mmc_request to perform the "short transfer" tests
650 static void mmc_test_prepare_broken_mrq(struct mmc_test_card
*test
,
651 struct mmc_request
*mrq
, int write
)
653 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
);
655 if (mrq
->data
->blocks
> 1) {
656 mrq
->cmd
->opcode
= write
?
657 MMC_WRITE_BLOCK
: MMC_READ_SINGLE_BLOCK
;
660 mrq
->cmd
->opcode
= MMC_SEND_STATUS
;
661 mrq
->cmd
->arg
= test
->card
->rca
<< 16;
666 * Checks that a normal transfer didn't have any errors
668 static int mmc_test_check_result(struct mmc_test_card
*test
,
669 struct mmc_request
*mrq
)
673 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
);
677 if (!ret
&& mrq
->cmd
->error
)
678 ret
= mrq
->cmd
->error
;
679 if (!ret
&& mrq
->data
->error
)
680 ret
= mrq
->data
->error
;
681 if (!ret
&& mrq
->stop
&& mrq
->stop
->error
)
682 ret
= mrq
->stop
->error
;
683 if (!ret
&& mrq
->data
->bytes_xfered
!=
684 mrq
->data
->blocks
* mrq
->data
->blksz
)
688 ret
= RESULT_UNSUP_HOST
;
694 * Checks that a "short transfer" behaved as expected
696 static int mmc_test_check_broken_result(struct mmc_test_card
*test
,
697 struct mmc_request
*mrq
)
701 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
);
705 if (!ret
&& mrq
->cmd
->error
)
706 ret
= mrq
->cmd
->error
;
707 if (!ret
&& mrq
->data
->error
== 0)
709 if (!ret
&& mrq
->data
->error
!= -ETIMEDOUT
)
710 ret
= mrq
->data
->error
;
711 if (!ret
&& mrq
->stop
&& mrq
->stop
->error
)
712 ret
= mrq
->stop
->error
;
713 if (mrq
->data
->blocks
> 1) {
714 if (!ret
&& mrq
->data
->bytes_xfered
> mrq
->data
->blksz
)
717 if (!ret
&& mrq
->data
->bytes_xfered
> 0)
722 ret
= RESULT_UNSUP_HOST
;
728 * Tests a basic transfer with certain parameters
730 static int mmc_test_simple_transfer(struct mmc_test_card
*test
,
731 struct scatterlist
*sg
, unsigned sg_len
, unsigned dev_addr
,
732 unsigned blocks
, unsigned blksz
, int write
)
734 struct mmc_request mrq
;
735 struct mmc_command cmd
;
736 struct mmc_command stop
;
737 struct mmc_data data
;
739 memset(&mrq
, 0, sizeof(struct mmc_request
));
740 memset(&cmd
, 0, sizeof(struct mmc_command
));
741 memset(&data
, 0, sizeof(struct mmc_data
));
742 memset(&stop
, 0, sizeof(struct mmc_command
));
748 mmc_test_prepare_mrq(test
, &mrq
, sg
, sg_len
, dev_addr
,
749 blocks
, blksz
, write
);
751 mmc_wait_for_req(test
->card
->host
, &mrq
);
753 mmc_test_wait_busy(test
);
755 return mmc_test_check_result(test
, &mrq
);
759 * Tests a transfer where the card will fail completely or partly
761 static int mmc_test_broken_transfer(struct mmc_test_card
*test
,
762 unsigned blocks
, unsigned blksz
, int write
)
764 struct mmc_request mrq
;
765 struct mmc_command cmd
;
766 struct mmc_command stop
;
767 struct mmc_data data
;
769 struct scatterlist sg
;
771 memset(&mrq
, 0, sizeof(struct mmc_request
));
772 memset(&cmd
, 0, sizeof(struct mmc_command
));
773 memset(&data
, 0, sizeof(struct mmc_data
));
774 memset(&stop
, 0, sizeof(struct mmc_command
));
780 sg_init_one(&sg
, test
->buffer
, blocks
* blksz
);
782 mmc_test_prepare_mrq(test
, &mrq
, &sg
, 1, 0, blocks
, blksz
, write
);
783 mmc_test_prepare_broken_mrq(test
, &mrq
, write
);
785 mmc_wait_for_req(test
->card
->host
, &mrq
);
787 mmc_test_wait_busy(test
);
789 return mmc_test_check_broken_result(test
, &mrq
);
793 * Does a complete transfer test where data is also validated
795 * Note: mmc_test_prepare() must have been done before this call
797 static int mmc_test_transfer(struct mmc_test_card
*test
,
798 struct scatterlist
*sg
, unsigned sg_len
, unsigned dev_addr
,
799 unsigned blocks
, unsigned blksz
, int write
)
805 for (i
= 0;i
< blocks
* blksz
;i
++)
806 test
->scratch
[i
] = i
;
808 memset(test
->scratch
, 0, BUFFER_SIZE
);
810 local_irq_save(flags
);
811 sg_copy_from_buffer(sg
, sg_len
, test
->scratch
, BUFFER_SIZE
);
812 local_irq_restore(flags
);
814 ret
= mmc_test_set_blksize(test
, blksz
);
818 ret
= mmc_test_simple_transfer(test
, sg
, sg_len
, dev_addr
,
819 blocks
, blksz
, write
);
826 ret
= mmc_test_set_blksize(test
, 512);
830 sectors
= (blocks
* blksz
+ 511) / 512;
831 if ((sectors
* 512) == (blocks
* blksz
))
834 if ((sectors
* 512) > BUFFER_SIZE
)
837 memset(test
->buffer
, 0, sectors
* 512);
839 for (i
= 0;i
< sectors
;i
++) {
840 ret
= mmc_test_buffer_transfer(test
,
841 test
->buffer
+ i
* 512,
842 dev_addr
+ i
, 512, 0);
847 for (i
= 0;i
< blocks
* blksz
;i
++) {
848 if (test
->buffer
[i
] != (u8
)i
)
852 for (;i
< sectors
* 512;i
++) {
853 if (test
->buffer
[i
] != 0xDF)
857 local_irq_save(flags
);
858 sg_copy_to_buffer(sg
, sg_len
, test
->scratch
, BUFFER_SIZE
);
859 local_irq_restore(flags
);
860 for (i
= 0;i
< blocks
* blksz
;i
++) {
861 if (test
->scratch
[i
] != (u8
)i
)
869 /*******************************************************************/
871 /*******************************************************************/
873 struct mmc_test_case
{
876 int (*prepare
)(struct mmc_test_card
*);
877 int (*run
)(struct mmc_test_card
*);
878 int (*cleanup
)(struct mmc_test_card
*);
881 static int mmc_test_basic_write(struct mmc_test_card
*test
)
884 struct scatterlist sg
;
886 ret
= mmc_test_set_blksize(test
, 512);
890 sg_init_one(&sg
, test
->buffer
, 512);
892 ret
= mmc_test_simple_transfer(test
, &sg
, 1, 0, 1, 512, 1);
899 static int mmc_test_basic_read(struct mmc_test_card
*test
)
902 struct scatterlist sg
;
904 ret
= mmc_test_set_blksize(test
, 512);
908 sg_init_one(&sg
, test
->buffer
, 512);
910 ret
= mmc_test_simple_transfer(test
, &sg
, 1, 0, 1, 512, 0);
917 static int mmc_test_verify_write(struct mmc_test_card
*test
)
920 struct scatterlist sg
;
922 sg_init_one(&sg
, test
->buffer
, 512);
924 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 1);
931 static int mmc_test_verify_read(struct mmc_test_card
*test
)
934 struct scatterlist sg
;
936 sg_init_one(&sg
, test
->buffer
, 512);
938 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 0);
945 static int mmc_test_multi_write(struct mmc_test_card
*test
)
949 struct scatterlist sg
;
951 if (test
->card
->host
->max_blk_count
== 1)
952 return RESULT_UNSUP_HOST
;
954 size
= PAGE_SIZE
* 2;
955 size
= min(size
, test
->card
->host
->max_req_size
);
956 size
= min(size
, test
->card
->host
->max_seg_size
);
957 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
960 return RESULT_UNSUP_HOST
;
962 sg_init_one(&sg
, test
->buffer
, size
);
964 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 1);
971 static int mmc_test_multi_read(struct mmc_test_card
*test
)
975 struct scatterlist sg
;
977 if (test
->card
->host
->max_blk_count
== 1)
978 return RESULT_UNSUP_HOST
;
980 size
= PAGE_SIZE
* 2;
981 size
= min(size
, test
->card
->host
->max_req_size
);
982 size
= min(size
, test
->card
->host
->max_seg_size
);
983 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
986 return RESULT_UNSUP_HOST
;
988 sg_init_one(&sg
, test
->buffer
, size
);
990 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 0);
997 static int mmc_test_pow2_write(struct mmc_test_card
*test
)
1000 struct scatterlist sg
;
1002 if (!test
->card
->csd
.write_partial
)
1003 return RESULT_UNSUP_CARD
;
1005 for (i
= 1; i
< 512;i
<<= 1) {
1006 sg_init_one(&sg
, test
->buffer
, i
);
1007 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 1);
1015 static int mmc_test_pow2_read(struct mmc_test_card
*test
)
1018 struct scatterlist sg
;
1020 if (!test
->card
->csd
.read_partial
)
1021 return RESULT_UNSUP_CARD
;
1023 for (i
= 1; i
< 512;i
<<= 1) {
1024 sg_init_one(&sg
, test
->buffer
, i
);
1025 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 0);
1033 static int mmc_test_weird_write(struct mmc_test_card
*test
)
1036 struct scatterlist sg
;
1038 if (!test
->card
->csd
.write_partial
)
1039 return RESULT_UNSUP_CARD
;
1041 for (i
= 3; i
< 512;i
+= 7) {
1042 sg_init_one(&sg
, test
->buffer
, i
);
1043 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 1);
1051 static int mmc_test_weird_read(struct mmc_test_card
*test
)
1054 struct scatterlist sg
;
1056 if (!test
->card
->csd
.read_partial
)
1057 return RESULT_UNSUP_CARD
;
1059 for (i
= 3; i
< 512;i
+= 7) {
1060 sg_init_one(&sg
, test
->buffer
, i
);
1061 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 0);
1069 static int mmc_test_align_write(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, 1);
1084 static int mmc_test_align_read(struct mmc_test_card
*test
)
1087 struct scatterlist sg
;
1089 for (i
= 1;i
< 4;i
++) {
1090 sg_init_one(&sg
, test
->buffer
+ i
, 512);
1091 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 0);
1099 static int mmc_test_align_multi_write(struct mmc_test_card
*test
)
1103 struct scatterlist sg
;
1105 if (test
->card
->host
->max_blk_count
== 1)
1106 return RESULT_UNSUP_HOST
;
1108 size
= PAGE_SIZE
* 2;
1109 size
= min(size
, test
->card
->host
->max_req_size
);
1110 size
= min(size
, test
->card
->host
->max_seg_size
);
1111 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1114 return RESULT_UNSUP_HOST
;
1116 for (i
= 1;i
< 4;i
++) {
1117 sg_init_one(&sg
, test
->buffer
+ i
, size
);
1118 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 1);
1126 static int mmc_test_align_multi_read(struct mmc_test_card
*test
)
1130 struct scatterlist sg
;
1132 if (test
->card
->host
->max_blk_count
== 1)
1133 return RESULT_UNSUP_HOST
;
1135 size
= PAGE_SIZE
* 2;
1136 size
= min(size
, test
->card
->host
->max_req_size
);
1137 size
= min(size
, test
->card
->host
->max_seg_size
);
1138 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1141 return RESULT_UNSUP_HOST
;
1143 for (i
= 1;i
< 4;i
++) {
1144 sg_init_one(&sg
, test
->buffer
+ i
, size
);
1145 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 0);
1153 static int mmc_test_xfersize_write(struct mmc_test_card
*test
)
1157 ret
= mmc_test_set_blksize(test
, 512);
1161 ret
= mmc_test_broken_transfer(test
, 1, 512, 1);
1168 static int mmc_test_xfersize_read(struct mmc_test_card
*test
)
1172 ret
= mmc_test_set_blksize(test
, 512);
1176 ret
= mmc_test_broken_transfer(test
, 1, 512, 0);
1183 static int mmc_test_multi_xfersize_write(struct mmc_test_card
*test
)
1187 if (test
->card
->host
->max_blk_count
== 1)
1188 return RESULT_UNSUP_HOST
;
1190 ret
= mmc_test_set_blksize(test
, 512);
1194 ret
= mmc_test_broken_transfer(test
, 2, 512, 1);
1201 static int mmc_test_multi_xfersize_read(struct mmc_test_card
*test
)
1205 if (test
->card
->host
->max_blk_count
== 1)
1206 return RESULT_UNSUP_HOST
;
1208 ret
= mmc_test_set_blksize(test
, 512);
1212 ret
= mmc_test_broken_transfer(test
, 2, 512, 0);
1219 #ifdef CONFIG_HIGHMEM
1221 static int mmc_test_write_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, 1);
1236 static int mmc_test_read_high(struct mmc_test_card
*test
)
1239 struct scatterlist sg
;
1241 sg_init_table(&sg
, 1);
1242 sg_set_page(&sg
, test
->highmem
, 512, 0);
1244 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 0);
1251 static int mmc_test_multi_write_high(struct mmc_test_card
*test
)
1255 struct scatterlist sg
;
1257 if (test
->card
->host
->max_blk_count
== 1)
1258 return RESULT_UNSUP_HOST
;
1260 size
= PAGE_SIZE
* 2;
1261 size
= min(size
, test
->card
->host
->max_req_size
);
1262 size
= min(size
, test
->card
->host
->max_seg_size
);
1263 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1266 return RESULT_UNSUP_HOST
;
1268 sg_init_table(&sg
, 1);
1269 sg_set_page(&sg
, test
->highmem
, size
, 0);
1271 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 1);
1278 static int mmc_test_multi_read_high(struct mmc_test_card
*test
)
1282 struct scatterlist sg
;
1284 if (test
->card
->host
->max_blk_count
== 1)
1285 return RESULT_UNSUP_HOST
;
1287 size
= PAGE_SIZE
* 2;
1288 size
= min(size
, test
->card
->host
->max_req_size
);
1289 size
= min(size
, test
->card
->host
->max_seg_size
);
1290 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1293 return RESULT_UNSUP_HOST
;
1295 sg_init_table(&sg
, 1);
1296 sg_set_page(&sg
, test
->highmem
, size
, 0);
1298 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 0);
1307 static int mmc_test_no_highmem(struct mmc_test_card
*test
)
1309 printk(KERN_INFO
"%s: Highmem not configured - test skipped\n",
1310 mmc_hostname(test
->card
->host
));
1314 #endif /* CONFIG_HIGHMEM */
1317 * Map sz bytes so that it can be transferred.
1319 static int mmc_test_area_map(struct mmc_test_card
*test
, unsigned long sz
,
1322 struct mmc_test_area
*t
= &test
->area
;
1325 t
->blocks
= sz
>> 9;
1328 err
= mmc_test_map_sg_max_scatter(t
->mem
, sz
, t
->sg
,
1329 t
->max_segs
, t
->max_seg_sz
,
1332 err
= mmc_test_map_sg(t
->mem
, sz
, t
->sg
, 1, t
->max_segs
,
1333 t
->max_seg_sz
, &t
->sg_len
);
1336 printk(KERN_INFO
"%s: Failed to map sg list\n",
1337 mmc_hostname(test
->card
->host
));
1342 * Transfer bytes mapped by mmc_test_area_map().
1344 static int mmc_test_area_transfer(struct mmc_test_card
*test
,
1345 unsigned int dev_addr
, int write
)
1347 struct mmc_test_area
*t
= &test
->area
;
1349 return mmc_test_simple_transfer(test
, t
->sg
, t
->sg_len
, dev_addr
,
1350 t
->blocks
, 512, write
);
1354 * Map and transfer bytes.
1356 static int mmc_test_area_io(struct mmc_test_card
*test
, unsigned long sz
,
1357 unsigned int dev_addr
, int write
, int max_scatter
,
1360 struct timespec ts1
, ts2
;
1364 * In the case of a maximally scattered transfer, the maximum transfer
1365 * size is further limited by using PAGE_SIZE segments.
1368 struct mmc_test_area
*t
= &test
->area
;
1369 unsigned long max_tfr
;
1371 if (t
->max_seg_sz
>= PAGE_SIZE
)
1372 max_tfr
= t
->max_segs
* PAGE_SIZE
;
1374 max_tfr
= t
->max_segs
* t
->max_seg_sz
;
1379 ret
= mmc_test_area_map(test
, sz
, max_scatter
);
1384 getnstimeofday(&ts1
);
1386 ret
= mmc_test_area_transfer(test
, dev_addr
, write
);
1391 getnstimeofday(&ts2
);
1394 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1400 * Write the test area entirely.
1402 static int mmc_test_area_fill(struct mmc_test_card
*test
)
1404 return mmc_test_area_io(test
, test
->area
.max_tfr
, test
->area
.dev_addr
,
1409 * Erase the test area entirely.
1411 static int mmc_test_area_erase(struct mmc_test_card
*test
)
1413 struct mmc_test_area
*t
= &test
->area
;
1415 if (!mmc_can_erase(test
->card
))
1418 return mmc_erase(test
->card
, t
->dev_addr
, test
->area
.max_sz
>> 9,
1423 * Cleanup struct mmc_test_area.
1425 static int mmc_test_area_cleanup(struct mmc_test_card
*test
)
1427 struct mmc_test_area
*t
= &test
->area
;
1430 mmc_test_free_mem(t
->mem
);
1436 * Initialize an area for testing large transfers. The test area is set to the
1437 * middle of the card because cards may have different charateristics at the
1438 * front (for FAT file system optimization). Optionally, the area is erased
1439 * (if the card supports it) which may improve write performance. Optionally,
1440 * the area is filled with data for subsequent read tests.
1442 static int mmc_test_area_init(struct mmc_test_card
*test
, int erase
, int fill
)
1444 struct mmc_test_area
*t
= &test
->area
;
1445 unsigned long min_sz
= 64 * 1024, sz
;
1448 ret
= mmc_test_set_blksize(test
, 512);
1452 /* Make the test area size about 4MiB */
1453 sz
= (unsigned long)test
->card
->pref_erase
<< 9;
1455 while (t
->max_sz
< 4 * 1024 * 1024)
1457 while (t
->max_sz
> TEST_AREA_MAX_SIZE
&& t
->max_sz
> sz
)
1460 t
->max_segs
= test
->card
->host
->max_segs
;
1461 t
->max_seg_sz
= test
->card
->host
->max_seg_size
;
1463 t
->max_tfr
= t
->max_sz
;
1464 if (t
->max_tfr
>> 9 > test
->card
->host
->max_blk_count
)
1465 t
->max_tfr
= test
->card
->host
->max_blk_count
<< 9;
1466 if (t
->max_tfr
> test
->card
->host
->max_req_size
)
1467 t
->max_tfr
= test
->card
->host
->max_req_size
;
1468 if (t
->max_tfr
/ t
->max_seg_sz
> t
->max_segs
)
1469 t
->max_tfr
= t
->max_segs
* t
->max_seg_sz
;
1472 * Try to allocate enough memory for a max. sized transfer. Less is OK
1473 * because the same memory can be mapped into the scatterlist more than
1474 * once. Also, take into account the limits imposed on scatterlist
1475 * segments by the host driver.
1477 t
->mem
= mmc_test_alloc_mem(min_sz
, t
->max_tfr
, t
->max_segs
,
1482 t
->sg
= kmalloc(sizeof(struct scatterlist
) * t
->max_segs
, GFP_KERNEL
);
1488 t
->dev_addr
= mmc_test_capacity(test
->card
) / 2;
1489 t
->dev_addr
-= t
->dev_addr
% (t
->max_sz
>> 9);
1492 ret
= mmc_test_area_erase(test
);
1498 ret
= mmc_test_area_fill(test
);
1506 mmc_test_area_cleanup(test
);
1511 * Prepare for large transfers. Do not erase the test area.
1513 static int mmc_test_area_prepare(struct mmc_test_card
*test
)
1515 return mmc_test_area_init(test
, 0, 0);
1519 * Prepare for large transfers. Do erase the test area.
1521 static int mmc_test_area_prepare_erase(struct mmc_test_card
*test
)
1523 return mmc_test_area_init(test
, 1, 0);
1527 * Prepare for large transfers. Erase and fill the test area.
1529 static int mmc_test_area_prepare_fill(struct mmc_test_card
*test
)
1531 return mmc_test_area_init(test
, 1, 1);
1535 * Test best-case performance. Best-case performance is expected from
1536 * a single large transfer.
1538 * An additional option (max_scatter) allows the measurement of the same
1539 * transfer but with no contiguous pages in the scatter list. This tests
1540 * the efficiency of DMA to handle scattered pages.
1542 static int mmc_test_best_performance(struct mmc_test_card
*test
, int write
,
1545 return mmc_test_area_io(test
, test
->area
.max_tfr
, test
->area
.dev_addr
,
1546 write
, max_scatter
, 1);
1550 * Best-case read performance.
1552 static int mmc_test_best_read_performance(struct mmc_test_card
*test
)
1554 return mmc_test_best_performance(test
, 0, 0);
1558 * Best-case write performance.
1560 static int mmc_test_best_write_performance(struct mmc_test_card
*test
)
1562 return mmc_test_best_performance(test
, 1, 0);
1566 * Best-case read performance into scattered pages.
1568 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card
*test
)
1570 return mmc_test_best_performance(test
, 0, 1);
1574 * Best-case write performance from scattered pages.
1576 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card
*test
)
1578 return mmc_test_best_performance(test
, 1, 1);
1582 * Single read performance by transfer size.
1584 static int mmc_test_profile_read_perf(struct mmc_test_card
*test
)
1587 unsigned int dev_addr
;
1590 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1591 dev_addr
= test
->area
.dev_addr
+ (sz
>> 9);
1592 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 0, 0, 1);
1596 sz
= test
->area
.max_tfr
;
1597 dev_addr
= test
->area
.dev_addr
;
1598 return mmc_test_area_io(test
, sz
, dev_addr
, 0, 0, 1);
1602 * Single write performance by transfer size.
1604 static int mmc_test_profile_write_perf(struct mmc_test_card
*test
)
1607 unsigned int dev_addr
;
1610 ret
= mmc_test_area_erase(test
);
1613 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1614 dev_addr
= test
->area
.dev_addr
+ (sz
>> 9);
1615 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 1, 0, 1);
1619 ret
= mmc_test_area_erase(test
);
1622 sz
= test
->area
.max_tfr
;
1623 dev_addr
= test
->area
.dev_addr
;
1624 return mmc_test_area_io(test
, sz
, dev_addr
, 1, 0, 1);
1628 * Single trim performance by transfer size.
1630 static int mmc_test_profile_trim_perf(struct mmc_test_card
*test
)
1633 unsigned int dev_addr
;
1634 struct timespec ts1
, ts2
;
1637 if (!mmc_can_trim(test
->card
))
1638 return RESULT_UNSUP_CARD
;
1640 if (!mmc_can_erase(test
->card
))
1641 return RESULT_UNSUP_HOST
;
1643 for (sz
= 512; sz
< test
->area
.max_sz
; sz
<<= 1) {
1644 dev_addr
= test
->area
.dev_addr
+ (sz
>> 9);
1645 getnstimeofday(&ts1
);
1646 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9, MMC_TRIM_ARG
);
1649 getnstimeofday(&ts2
);
1650 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1652 dev_addr
= test
->area
.dev_addr
;
1653 getnstimeofday(&ts1
);
1654 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9, MMC_TRIM_ARG
);
1657 getnstimeofday(&ts2
);
1658 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1662 static int mmc_test_seq_read_perf(struct mmc_test_card
*test
, unsigned long sz
)
1664 unsigned int dev_addr
, i
, cnt
;
1665 struct timespec ts1
, ts2
;
1668 cnt
= test
->area
.max_sz
/ sz
;
1669 dev_addr
= test
->area
.dev_addr
;
1670 getnstimeofday(&ts1
);
1671 for (i
= 0; i
< cnt
; i
++) {
1672 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 0, 0, 0);
1675 dev_addr
+= (sz
>> 9);
1677 getnstimeofday(&ts2
);
1678 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1683 * Consecutive read performance by transfer size.
1685 static int mmc_test_profile_seq_read_perf(struct mmc_test_card
*test
)
1690 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1691 ret
= mmc_test_seq_read_perf(test
, sz
);
1695 sz
= test
->area
.max_tfr
;
1696 return mmc_test_seq_read_perf(test
, sz
);
1699 static int mmc_test_seq_write_perf(struct mmc_test_card
*test
, unsigned long sz
)
1701 unsigned int dev_addr
, i
, cnt
;
1702 struct timespec ts1
, ts2
;
1705 ret
= mmc_test_area_erase(test
);
1708 cnt
= test
->area
.max_sz
/ sz
;
1709 dev_addr
= test
->area
.dev_addr
;
1710 getnstimeofday(&ts1
);
1711 for (i
= 0; i
< cnt
; i
++) {
1712 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 1, 0, 0);
1715 dev_addr
+= (sz
>> 9);
1717 getnstimeofday(&ts2
);
1718 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1723 * Consecutive write performance by transfer size.
1725 static int mmc_test_profile_seq_write_perf(struct mmc_test_card
*test
)
1730 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1731 ret
= mmc_test_seq_write_perf(test
, sz
);
1735 sz
= test
->area
.max_tfr
;
1736 return mmc_test_seq_write_perf(test
, sz
);
1740 * Consecutive trim performance by transfer size.
1742 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card
*test
)
1745 unsigned int dev_addr
, i
, cnt
;
1746 struct timespec ts1
, ts2
;
1749 if (!mmc_can_trim(test
->card
))
1750 return RESULT_UNSUP_CARD
;
1752 if (!mmc_can_erase(test
->card
))
1753 return RESULT_UNSUP_HOST
;
1755 for (sz
= 512; sz
<= test
->area
.max_sz
; sz
<<= 1) {
1756 ret
= mmc_test_area_erase(test
);
1759 ret
= mmc_test_area_fill(test
);
1762 cnt
= test
->area
.max_sz
/ sz
;
1763 dev_addr
= test
->area
.dev_addr
;
1764 getnstimeofday(&ts1
);
1765 for (i
= 0; i
< cnt
; i
++) {
1766 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9,
1770 dev_addr
+= (sz
>> 9);
1772 getnstimeofday(&ts2
);
1773 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1778 static unsigned int rnd_next
= 1;
1780 static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt
)
1784 rnd_next
= rnd_next
* 1103515245 + 12345;
1785 r
= (rnd_next
>> 16) & 0x7fff;
1786 return (r
* rnd_cnt
) >> 15;
1789 static int mmc_test_rnd_perf(struct mmc_test_card
*test
, int write
, int print
,
1792 unsigned int dev_addr
, cnt
, rnd_addr
, range1
, range2
, last_ea
= 0, ea
;
1794 struct timespec ts1
, ts2
, ts
;
1799 rnd_addr
= mmc_test_capacity(test
->card
) / 4;
1800 range1
= rnd_addr
/ test
->card
->pref_erase
;
1801 range2
= range1
/ ssz
;
1803 getnstimeofday(&ts1
);
1804 for (cnt
= 0; cnt
< UINT_MAX
; cnt
++) {
1805 getnstimeofday(&ts2
);
1806 ts
= timespec_sub(ts2
, ts1
);
1807 if (ts
.tv_sec
>= 10)
1809 ea
= mmc_test_rnd_num(range1
);
1813 dev_addr
= rnd_addr
+ test
->card
->pref_erase
* ea
+
1814 ssz
* mmc_test_rnd_num(range2
);
1815 ret
= mmc_test_area_io(test
, sz
, dev_addr
, write
, 0, 0);
1820 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1824 static int mmc_test_random_perf(struct mmc_test_card
*test
, int write
)
1830 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1832 * When writing, try to get more consistent results by running
1833 * the test twice with exactly the same I/O but outputting the
1834 * results only for the 2nd run.
1838 ret
= mmc_test_rnd_perf(test
, write
, 0, sz
);
1843 ret
= mmc_test_rnd_perf(test
, write
, 1, sz
);
1847 sz
= test
->area
.max_tfr
;
1850 ret
= mmc_test_rnd_perf(test
, write
, 0, sz
);
1855 return mmc_test_rnd_perf(test
, write
, 1, sz
);
1859 * Random read performance by transfer size.
1861 static int mmc_test_random_read_perf(struct mmc_test_card
*test
)
1863 return mmc_test_random_perf(test
, 0);
1867 * Random write performance by transfer size.
1869 static int mmc_test_random_write_perf(struct mmc_test_card
*test
)
1871 return mmc_test_random_perf(test
, 1);
1874 static int mmc_test_seq_perf(struct mmc_test_card
*test
, int write
,
1875 unsigned int tot_sz
, int max_scatter
)
1877 unsigned int dev_addr
, i
, cnt
, sz
, ssz
;
1878 struct timespec ts1
, ts2
, ts
;
1881 sz
= test
->area
.max_tfr
;
1883 * In the case of a maximally scattered transfer, the maximum transfer
1884 * size is further limited by using PAGE_SIZE segments.
1887 struct mmc_test_area
*t
= &test
->area
;
1888 unsigned long max_tfr
;
1890 if (t
->max_seg_sz
>= PAGE_SIZE
)
1891 max_tfr
= t
->max_segs
* PAGE_SIZE
;
1893 max_tfr
= t
->max_segs
* t
->max_seg_sz
;
1899 dev_addr
= mmc_test_capacity(test
->card
) / 4;
1900 if (tot_sz
> dev_addr
<< 9)
1901 tot_sz
= dev_addr
<< 9;
1903 dev_addr
&= 0xffff0000; /* Round to 64MiB boundary */
1905 getnstimeofday(&ts1
);
1906 for (i
= 0; i
< cnt
; i
++) {
1907 ret
= mmc_test_area_io(test
, sz
, dev_addr
, write
,
1913 getnstimeofday(&ts2
);
1915 ts
= timespec_sub(ts2
, ts1
);
1916 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1921 static int mmc_test_large_seq_perf(struct mmc_test_card
*test
, int write
)
1925 for (i
= 0; i
< 10; i
++) {
1926 ret
= mmc_test_seq_perf(test
, write
, 10 * 1024 * 1024, 1);
1930 for (i
= 0; i
< 5; i
++) {
1931 ret
= mmc_test_seq_perf(test
, write
, 100 * 1024 * 1024, 1);
1935 for (i
= 0; i
< 3; i
++) {
1936 ret
= mmc_test_seq_perf(test
, write
, 1000 * 1024 * 1024, 1);
1945 * Large sequential read performance.
1947 static int mmc_test_large_seq_read_perf(struct mmc_test_card
*test
)
1949 return mmc_test_large_seq_perf(test
, 0);
1953 * Large sequential write performance.
1955 static int mmc_test_large_seq_write_perf(struct mmc_test_card
*test
)
1957 return mmc_test_large_seq_perf(test
, 1);
1960 static const struct mmc_test_case mmc_test_cases
[] = {
1962 .name
= "Basic write (no data verification)",
1963 .run
= mmc_test_basic_write
,
1967 .name
= "Basic read (no data verification)",
1968 .run
= mmc_test_basic_read
,
1972 .name
= "Basic write (with data verification)",
1973 .prepare
= mmc_test_prepare_write
,
1974 .run
= mmc_test_verify_write
,
1975 .cleanup
= mmc_test_cleanup
,
1979 .name
= "Basic read (with data verification)",
1980 .prepare
= mmc_test_prepare_read
,
1981 .run
= mmc_test_verify_read
,
1982 .cleanup
= mmc_test_cleanup
,
1986 .name
= "Multi-block write",
1987 .prepare
= mmc_test_prepare_write
,
1988 .run
= mmc_test_multi_write
,
1989 .cleanup
= mmc_test_cleanup
,
1993 .name
= "Multi-block read",
1994 .prepare
= mmc_test_prepare_read
,
1995 .run
= mmc_test_multi_read
,
1996 .cleanup
= mmc_test_cleanup
,
2000 .name
= "Power of two block writes",
2001 .prepare
= mmc_test_prepare_write
,
2002 .run
= mmc_test_pow2_write
,
2003 .cleanup
= mmc_test_cleanup
,
2007 .name
= "Power of two block reads",
2008 .prepare
= mmc_test_prepare_read
,
2009 .run
= mmc_test_pow2_read
,
2010 .cleanup
= mmc_test_cleanup
,
2014 .name
= "Weird sized block writes",
2015 .prepare
= mmc_test_prepare_write
,
2016 .run
= mmc_test_weird_write
,
2017 .cleanup
= mmc_test_cleanup
,
2021 .name
= "Weird sized block reads",
2022 .prepare
= mmc_test_prepare_read
,
2023 .run
= mmc_test_weird_read
,
2024 .cleanup
= mmc_test_cleanup
,
2028 .name
= "Badly aligned write",
2029 .prepare
= mmc_test_prepare_write
,
2030 .run
= mmc_test_align_write
,
2031 .cleanup
= mmc_test_cleanup
,
2035 .name
= "Badly aligned read",
2036 .prepare
= mmc_test_prepare_read
,
2037 .run
= mmc_test_align_read
,
2038 .cleanup
= mmc_test_cleanup
,
2042 .name
= "Badly aligned multi-block write",
2043 .prepare
= mmc_test_prepare_write
,
2044 .run
= mmc_test_align_multi_write
,
2045 .cleanup
= mmc_test_cleanup
,
2049 .name
= "Badly aligned multi-block read",
2050 .prepare
= mmc_test_prepare_read
,
2051 .run
= mmc_test_align_multi_read
,
2052 .cleanup
= mmc_test_cleanup
,
2056 .name
= "Correct xfer_size at write (start failure)",
2057 .run
= mmc_test_xfersize_write
,
2061 .name
= "Correct xfer_size at read (start failure)",
2062 .run
= mmc_test_xfersize_read
,
2066 .name
= "Correct xfer_size at write (midway failure)",
2067 .run
= mmc_test_multi_xfersize_write
,
2071 .name
= "Correct xfer_size at read (midway failure)",
2072 .run
= mmc_test_multi_xfersize_read
,
2075 #ifdef CONFIG_HIGHMEM
2078 .name
= "Highmem write",
2079 .prepare
= mmc_test_prepare_write
,
2080 .run
= mmc_test_write_high
,
2081 .cleanup
= mmc_test_cleanup
,
2085 .name
= "Highmem read",
2086 .prepare
= mmc_test_prepare_read
,
2087 .run
= mmc_test_read_high
,
2088 .cleanup
= mmc_test_cleanup
,
2092 .name
= "Multi-block highmem write",
2093 .prepare
= mmc_test_prepare_write
,
2094 .run
= mmc_test_multi_write_high
,
2095 .cleanup
= mmc_test_cleanup
,
2099 .name
= "Multi-block highmem read",
2100 .prepare
= mmc_test_prepare_read
,
2101 .run
= mmc_test_multi_read_high
,
2102 .cleanup
= mmc_test_cleanup
,
2108 .name
= "Highmem write",
2109 .run
= mmc_test_no_highmem
,
2113 .name
= "Highmem read",
2114 .run
= mmc_test_no_highmem
,
2118 .name
= "Multi-block highmem write",
2119 .run
= mmc_test_no_highmem
,
2123 .name
= "Multi-block highmem read",
2124 .run
= mmc_test_no_highmem
,
2127 #endif /* CONFIG_HIGHMEM */
2130 .name
= "Best-case read performance",
2131 .prepare
= mmc_test_area_prepare_fill
,
2132 .run
= mmc_test_best_read_performance
,
2133 .cleanup
= mmc_test_area_cleanup
,
2137 .name
= "Best-case write performance",
2138 .prepare
= mmc_test_area_prepare_erase
,
2139 .run
= mmc_test_best_write_performance
,
2140 .cleanup
= mmc_test_area_cleanup
,
2144 .name
= "Best-case read performance into scattered pages",
2145 .prepare
= mmc_test_area_prepare_fill
,
2146 .run
= mmc_test_best_read_perf_max_scatter
,
2147 .cleanup
= mmc_test_area_cleanup
,
2151 .name
= "Best-case write performance from scattered pages",
2152 .prepare
= mmc_test_area_prepare_erase
,
2153 .run
= mmc_test_best_write_perf_max_scatter
,
2154 .cleanup
= mmc_test_area_cleanup
,
2158 .name
= "Single read performance by transfer size",
2159 .prepare
= mmc_test_area_prepare_fill
,
2160 .run
= mmc_test_profile_read_perf
,
2161 .cleanup
= mmc_test_area_cleanup
,
2165 .name
= "Single write performance by transfer size",
2166 .prepare
= mmc_test_area_prepare
,
2167 .run
= mmc_test_profile_write_perf
,
2168 .cleanup
= mmc_test_area_cleanup
,
2172 .name
= "Single trim performance by transfer size",
2173 .prepare
= mmc_test_area_prepare_fill
,
2174 .run
= mmc_test_profile_trim_perf
,
2175 .cleanup
= mmc_test_area_cleanup
,
2179 .name
= "Consecutive read performance by transfer size",
2180 .prepare
= mmc_test_area_prepare_fill
,
2181 .run
= mmc_test_profile_seq_read_perf
,
2182 .cleanup
= mmc_test_area_cleanup
,
2186 .name
= "Consecutive write performance by transfer size",
2187 .prepare
= mmc_test_area_prepare
,
2188 .run
= mmc_test_profile_seq_write_perf
,
2189 .cleanup
= mmc_test_area_cleanup
,
2193 .name
= "Consecutive trim performance by transfer size",
2194 .prepare
= mmc_test_area_prepare
,
2195 .run
= mmc_test_profile_seq_trim_perf
,
2196 .cleanup
= mmc_test_area_cleanup
,
2200 .name
= "Random read performance by transfer size",
2201 .prepare
= mmc_test_area_prepare
,
2202 .run
= mmc_test_random_read_perf
,
2203 .cleanup
= mmc_test_area_cleanup
,
2207 .name
= "Random write performance by transfer size",
2208 .prepare
= mmc_test_area_prepare
,
2209 .run
= mmc_test_random_write_perf
,
2210 .cleanup
= mmc_test_area_cleanup
,
2214 .name
= "Large sequential read into scattered pages",
2215 .prepare
= mmc_test_area_prepare
,
2216 .run
= mmc_test_large_seq_read_perf
,
2217 .cleanup
= mmc_test_area_cleanup
,
2221 .name
= "Large sequential write from scattered pages",
2222 .prepare
= mmc_test_area_prepare
,
2223 .run
= mmc_test_large_seq_write_perf
,
2224 .cleanup
= mmc_test_area_cleanup
,
2229 static DEFINE_MUTEX(mmc_test_lock
);
2231 static LIST_HEAD(mmc_test_result
);
2233 static void mmc_test_run(struct mmc_test_card
*test
, int testcase
)
2237 printk(KERN_INFO
"%s: Starting tests of card %s...\n",
2238 mmc_hostname(test
->card
->host
), mmc_card_id(test
->card
));
2240 mmc_claim_host(test
->card
->host
);
2242 for (i
= 0;i
< ARRAY_SIZE(mmc_test_cases
);i
++) {
2243 struct mmc_test_general_result
*gr
;
2245 if (testcase
&& ((i
+ 1) != testcase
))
2248 printk(KERN_INFO
"%s: Test case %d. %s...\n",
2249 mmc_hostname(test
->card
->host
), i
+ 1,
2250 mmc_test_cases
[i
].name
);
2252 if (mmc_test_cases
[i
].prepare
) {
2253 ret
= mmc_test_cases
[i
].prepare(test
);
2255 printk(KERN_INFO
"%s: Result: Prepare "
2256 "stage failed! (%d)\n",
2257 mmc_hostname(test
->card
->host
),
2263 gr
= kzalloc(sizeof(struct mmc_test_general_result
),
2266 INIT_LIST_HEAD(&gr
->tr_lst
);
2268 /* Assign data what we know already */
2269 gr
->card
= test
->card
;
2272 /* Append container to global one */
2273 list_add_tail(&gr
->link
, &mmc_test_result
);
2276 * Save the pointer to created container in our private
2282 ret
= mmc_test_cases
[i
].run(test
);
2285 printk(KERN_INFO
"%s: Result: OK\n",
2286 mmc_hostname(test
->card
->host
));
2289 printk(KERN_INFO
"%s: Result: FAILED\n",
2290 mmc_hostname(test
->card
->host
));
2292 case RESULT_UNSUP_HOST
:
2293 printk(KERN_INFO
"%s: Result: UNSUPPORTED "
2295 mmc_hostname(test
->card
->host
));
2297 case RESULT_UNSUP_CARD
:
2298 printk(KERN_INFO
"%s: Result: UNSUPPORTED "
2300 mmc_hostname(test
->card
->host
));
2303 printk(KERN_INFO
"%s: Result: ERROR (%d)\n",
2304 mmc_hostname(test
->card
->host
), ret
);
2307 /* Save the result */
2311 if (mmc_test_cases
[i
].cleanup
) {
2312 ret
= mmc_test_cases
[i
].cleanup(test
);
2314 printk(KERN_INFO
"%s: Warning: Cleanup "
2315 "stage failed! (%d)\n",
2316 mmc_hostname(test
->card
->host
),
2322 mmc_release_host(test
->card
->host
);
2324 printk(KERN_INFO
"%s: Tests completed.\n",
2325 mmc_hostname(test
->card
->host
));
2328 static void mmc_test_free_result(struct mmc_card
*card
)
2330 struct mmc_test_general_result
*gr
, *grs
;
2332 mutex_lock(&mmc_test_lock
);
2334 list_for_each_entry_safe(gr
, grs
, &mmc_test_result
, link
) {
2335 struct mmc_test_transfer_result
*tr
, *trs
;
2337 if (card
&& gr
->card
!= card
)
2340 list_for_each_entry_safe(tr
, trs
, &gr
->tr_lst
, link
) {
2341 list_del(&tr
->link
);
2345 list_del(&gr
->link
);
2349 mutex_unlock(&mmc_test_lock
);
2352 static LIST_HEAD(mmc_test_file_test
);
2354 static int mtf_test_show(struct seq_file
*sf
, void *data
)
2356 struct mmc_card
*card
= (struct mmc_card
*)sf
->private;
2357 struct mmc_test_general_result
*gr
;
2359 mutex_lock(&mmc_test_lock
);
2361 list_for_each_entry(gr
, &mmc_test_result
, link
) {
2362 struct mmc_test_transfer_result
*tr
;
2364 if (gr
->card
!= card
)
2367 seq_printf(sf
, "Test %d: %d\n", gr
->testcase
+ 1, gr
->result
);
2369 list_for_each_entry(tr
, &gr
->tr_lst
, link
) {
2370 seq_printf(sf
, "%u %d %lu.%09lu %u %u.%02u\n",
2371 tr
->count
, tr
->sectors
,
2372 (unsigned long)tr
->ts
.tv_sec
,
2373 (unsigned long)tr
->ts
.tv_nsec
,
2374 tr
->rate
, tr
->iops
/ 100, tr
->iops
% 100);
2378 mutex_unlock(&mmc_test_lock
);
2383 static int mtf_test_open(struct inode
*inode
, struct file
*file
)
2385 return single_open(file
, mtf_test_show
, inode
->i_private
);
2388 static ssize_t
mtf_test_write(struct file
*file
, const char __user
*buf
,
2389 size_t count
, loff_t
*pos
)
2391 struct seq_file
*sf
= (struct seq_file
*)file
->private_data
;
2392 struct mmc_card
*card
= (struct mmc_card
*)sf
->private;
2393 struct mmc_test_card
*test
;
2397 if (count
>= sizeof(lbuf
))
2400 if (copy_from_user(lbuf
, buf
, count
))
2404 if (strict_strtol(lbuf
, 10, &testcase
))
2407 test
= kzalloc(sizeof(struct mmc_test_card
), GFP_KERNEL
);
2412 * Remove all test cases associated with given card. Thus we have only
2413 * actual data of the last run.
2415 mmc_test_free_result(card
);
2419 test
->buffer
= kzalloc(BUFFER_SIZE
, GFP_KERNEL
);
2420 #ifdef CONFIG_HIGHMEM
2421 test
->highmem
= alloc_pages(GFP_KERNEL
| __GFP_HIGHMEM
, BUFFER_ORDER
);
2424 #ifdef CONFIG_HIGHMEM
2425 if (test
->buffer
&& test
->highmem
) {
2429 mutex_lock(&mmc_test_lock
);
2430 mmc_test_run(test
, testcase
);
2431 mutex_unlock(&mmc_test_lock
);
2434 #ifdef CONFIG_HIGHMEM
2435 __free_pages(test
->highmem
, BUFFER_ORDER
);
2437 kfree(test
->buffer
);
2443 static const struct file_operations mmc_test_fops_test
= {
2444 .open
= mtf_test_open
,
2446 .write
= mtf_test_write
,
2447 .llseek
= seq_lseek
,
2448 .release
= single_release
,
2451 static void mmc_test_free_file_test(struct mmc_card
*card
)
2453 struct mmc_test_dbgfs_file
*df
, *dfs
;
2455 mutex_lock(&mmc_test_lock
);
2457 list_for_each_entry_safe(df
, dfs
, &mmc_test_file_test
, link
) {
2458 if (card
&& df
->card
!= card
)
2460 debugfs_remove(df
->file
);
2461 list_del(&df
->link
);
2465 mutex_unlock(&mmc_test_lock
);
2468 static int mmc_test_register_file_test(struct mmc_card
*card
)
2470 struct dentry
*file
= NULL
;
2471 struct mmc_test_dbgfs_file
*df
;
2474 mutex_lock(&mmc_test_lock
);
2476 if (card
->debugfs_root
)
2477 file
= debugfs_create_file("test", S_IWUSR
| S_IRUGO
,
2478 card
->debugfs_root
, card
, &mmc_test_fops_test
);
2480 if (IS_ERR_OR_NULL(file
)) {
2482 "Can't create file. Perhaps debugfs is disabled.\n");
2487 df
= kmalloc(sizeof(struct mmc_test_dbgfs_file
), GFP_KERNEL
);
2489 debugfs_remove(file
);
2491 "Can't allocate memory for internal usage.\n");
2499 list_add(&df
->link
, &mmc_test_file_test
);
2502 mutex_unlock(&mmc_test_lock
);
2507 static int mmc_test_probe(struct mmc_card
*card
)
2511 if (!mmc_card_mmc(card
) && !mmc_card_sd(card
))
2514 ret
= mmc_test_register_file_test(card
);
2518 dev_info(&card
->dev
, "Card claimed for testing.\n");
2523 static void mmc_test_remove(struct mmc_card
*card
)
2525 mmc_test_free_result(card
);
2526 mmc_test_free_file_test(card
);
2529 static struct mmc_driver mmc_driver
= {
2533 .probe
= mmc_test_probe
,
2534 .remove
= mmc_test_remove
,
2537 static int __init
mmc_test_init(void)
2539 return mmc_register_driver(&mmc_driver
);
2542 static void __exit
mmc_test_exit(void)
2544 /* Clear stalled data if card is still plugged */
2545 mmc_test_free_result(NULL
);
2546 mmc_test_free_file_test(NULL
);
2548 mmc_unregister_driver(&mmc_driver
);
2551 module_init(mmc_test_init
);
2552 module_exit(mmc_test_exit
);
2554 MODULE_LICENSE("GPL");
2555 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
2556 MODULE_AUTHOR("Pierre Ossman");