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
;
250 struct mmc_command cmd
= {0};
251 struct mmc_command stop
= {0};
252 struct mmc_data data
;
254 struct scatterlist sg
;
256 memset(&mrq
, 0, sizeof(struct mmc_request
));
257 memset(&data
, 0, sizeof(struct mmc_data
));
263 sg_init_one(&sg
, buffer
, blksz
);
265 mmc_test_prepare_mrq(test
, &mrq
, &sg
, 1, addr
, 1, blksz
, write
);
267 mmc_wait_for_req(test
->card
->host
, &mrq
);
274 ret
= mmc_test_wait_busy(test
);
281 static void mmc_test_free_mem(struct mmc_test_mem
*mem
)
286 __free_pages(mem
->arr
[mem
->cnt
].page
,
287 mem
->arr
[mem
->cnt
].order
);
293 * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
294 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
295 * not exceed a maximum number of segments and try not to make segments much
296 * bigger than maximum segment size.
298 static struct mmc_test_mem
*mmc_test_alloc_mem(unsigned long min_sz
,
299 unsigned long max_sz
,
300 unsigned int max_segs
,
301 unsigned int max_seg_sz
)
303 unsigned long max_page_cnt
= DIV_ROUND_UP(max_sz
, PAGE_SIZE
);
304 unsigned long min_page_cnt
= DIV_ROUND_UP(min_sz
, PAGE_SIZE
);
305 unsigned long max_seg_page_cnt
= DIV_ROUND_UP(max_seg_sz
, PAGE_SIZE
);
306 unsigned long page_cnt
= 0;
307 unsigned long limit
= nr_free_buffer_pages() >> 4;
308 struct mmc_test_mem
*mem
;
310 if (max_page_cnt
> limit
)
311 max_page_cnt
= limit
;
312 if (min_page_cnt
> max_page_cnt
)
313 min_page_cnt
= max_page_cnt
;
315 if (max_seg_page_cnt
> max_page_cnt
)
316 max_seg_page_cnt
= max_page_cnt
;
318 if (max_segs
> max_page_cnt
)
319 max_segs
= max_page_cnt
;
321 mem
= kzalloc(sizeof(struct mmc_test_mem
), GFP_KERNEL
);
325 mem
->arr
= kzalloc(sizeof(struct mmc_test_pages
) * max_segs
,
330 while (max_page_cnt
) {
333 gfp_t flags
= GFP_KERNEL
| GFP_DMA
| __GFP_NOWARN
|
336 order
= get_order(max_seg_page_cnt
<< PAGE_SHIFT
);
338 page
= alloc_pages(flags
, order
);
344 if (page_cnt
< min_page_cnt
)
348 mem
->arr
[mem
->cnt
].page
= page
;
349 mem
->arr
[mem
->cnt
].order
= order
;
351 if (max_page_cnt
<= (1UL << order
))
353 max_page_cnt
-= 1UL << order
;
354 page_cnt
+= 1UL << order
;
355 if (mem
->cnt
>= max_segs
) {
356 if (page_cnt
< min_page_cnt
)
365 mmc_test_free_mem(mem
);
370 * Map memory into a scatterlist. Optionally allow the same memory to be
371 * mapped more than once.
373 static int mmc_test_map_sg(struct mmc_test_mem
*mem
, unsigned long sz
,
374 struct scatterlist
*sglist
, int repeat
,
375 unsigned int max_segs
, unsigned int max_seg_sz
,
376 unsigned int *sg_len
)
378 struct scatterlist
*sg
= NULL
;
381 sg_init_table(sglist
, max_segs
);
385 for (i
= 0; i
< mem
->cnt
; i
++) {
386 unsigned long len
= PAGE_SIZE
<< mem
->arr
[i
].order
;
390 if (len
> max_seg_sz
)
398 sg_set_page(sg
, mem
->arr
[i
].page
, len
, 0);
404 } while (sz
&& repeat
);
416 * Map memory into a scatterlist so that no pages are contiguous. Allow the
417 * same memory to be mapped more than once.
419 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem
*mem
,
421 struct scatterlist
*sglist
,
422 unsigned int max_segs
,
423 unsigned int max_seg_sz
,
424 unsigned int *sg_len
)
426 struct scatterlist
*sg
= NULL
;
427 unsigned int i
= mem
->cnt
, cnt
;
429 void *base
, *addr
, *last_addr
= NULL
;
431 sg_init_table(sglist
, max_segs
);
435 base
= page_address(mem
->arr
[--i
].page
);
436 cnt
= 1 << mem
->arr
[i
].order
;
438 addr
= base
+ PAGE_SIZE
* --cnt
;
439 if (last_addr
&& last_addr
+ PAGE_SIZE
== addr
)
443 if (len
> max_seg_sz
)
453 sg_set_page(sg
, virt_to_page(addr
), len
, 0);
468 * Calculate transfer rate in bytes per second.
470 static unsigned int mmc_test_rate(uint64_t bytes
, struct timespec
*ts
)
480 while (ns
> UINT_MAX
) {
488 do_div(bytes
, (uint32_t)ns
);
494 * Save transfer results for future usage
496 static void mmc_test_save_transfer_result(struct mmc_test_card
*test
,
497 unsigned int count
, unsigned int sectors
, struct timespec ts
,
498 unsigned int rate
, unsigned int iops
)
500 struct mmc_test_transfer_result
*tr
;
505 tr
= kmalloc(sizeof(struct mmc_test_transfer_result
), GFP_KERNEL
);
510 tr
->sectors
= sectors
;
515 list_add_tail(&tr
->link
, &test
->gr
->tr_lst
);
519 * Print the transfer rate.
521 static void mmc_test_print_rate(struct mmc_test_card
*test
, uint64_t bytes
,
522 struct timespec
*ts1
, struct timespec
*ts2
)
524 unsigned int rate
, iops
, sectors
= bytes
>> 9;
527 ts
= timespec_sub(*ts2
, *ts1
);
529 rate
= mmc_test_rate(bytes
, &ts
);
530 iops
= mmc_test_rate(100, &ts
); /* I/O ops per sec x 100 */
532 printk(KERN_INFO
"%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
533 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
534 mmc_hostname(test
->card
->host
), sectors
, sectors
>> 1,
535 (sectors
& 1 ? ".5" : ""), (unsigned long)ts
.tv_sec
,
536 (unsigned long)ts
.tv_nsec
, rate
/ 1000, rate
/ 1024,
537 iops
/ 100, iops
% 100);
539 mmc_test_save_transfer_result(test
, 1, sectors
, ts
, rate
, iops
);
543 * Print the average transfer rate.
545 static void mmc_test_print_avg_rate(struct mmc_test_card
*test
, uint64_t bytes
,
546 unsigned int count
, struct timespec
*ts1
,
547 struct timespec
*ts2
)
549 unsigned int rate
, iops
, sectors
= bytes
>> 9;
550 uint64_t tot
= bytes
* count
;
553 ts
= timespec_sub(*ts2
, *ts1
);
555 rate
= mmc_test_rate(tot
, &ts
);
556 iops
= mmc_test_rate(count
* 100, &ts
); /* I/O ops per sec x 100 */
558 printk(KERN_INFO
"%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
559 "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
561 mmc_hostname(test
->card
->host
), count
, sectors
, count
,
562 sectors
>> 1, (sectors
& 1 ? ".5" : ""),
563 (unsigned long)ts
.tv_sec
, (unsigned long)ts
.tv_nsec
,
564 rate
/ 1000, rate
/ 1024, iops
/ 100, iops
% 100);
566 mmc_test_save_transfer_result(test
, count
, sectors
, ts
, rate
, iops
);
570 * Return the card size in sectors.
572 static unsigned int mmc_test_capacity(struct mmc_card
*card
)
574 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
))
575 return card
->ext_csd
.sectors
;
577 return card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9);
580 /*******************************************************************/
581 /* Test preparation and cleanup */
582 /*******************************************************************/
585 * Fill the first couple of sectors of the card with known data
586 * so that bad reads/writes can be detected
588 static int __mmc_test_prepare(struct mmc_test_card
*test
, int write
)
592 ret
= mmc_test_set_blksize(test
, 512);
597 memset(test
->buffer
, 0xDF, 512);
599 for (i
= 0;i
< 512;i
++)
603 for (i
= 0;i
< BUFFER_SIZE
/ 512;i
++) {
604 ret
= mmc_test_buffer_transfer(test
, test
->buffer
, i
, 512, 1);
612 static int mmc_test_prepare_write(struct mmc_test_card
*test
)
614 return __mmc_test_prepare(test
, 1);
617 static int mmc_test_prepare_read(struct mmc_test_card
*test
)
619 return __mmc_test_prepare(test
, 0);
622 static int mmc_test_cleanup(struct mmc_test_card
*test
)
626 ret
= mmc_test_set_blksize(test
, 512);
630 memset(test
->buffer
, 0, 512);
632 for (i
= 0;i
< BUFFER_SIZE
/ 512;i
++) {
633 ret
= mmc_test_buffer_transfer(test
, test
->buffer
, i
, 512, 1);
641 /*******************************************************************/
642 /* Test execution helpers */
643 /*******************************************************************/
646 * Modifies the mmc_request to perform the "short transfer" tests
648 static void mmc_test_prepare_broken_mrq(struct mmc_test_card
*test
,
649 struct mmc_request
*mrq
, int write
)
651 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
);
653 if (mrq
->data
->blocks
> 1) {
654 mrq
->cmd
->opcode
= write
?
655 MMC_WRITE_BLOCK
: MMC_READ_SINGLE_BLOCK
;
658 mrq
->cmd
->opcode
= MMC_SEND_STATUS
;
659 mrq
->cmd
->arg
= test
->card
->rca
<< 16;
664 * Checks that a normal transfer didn't have any errors
666 static int mmc_test_check_result(struct mmc_test_card
*test
,
667 struct mmc_request
*mrq
)
671 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
);
675 if (!ret
&& mrq
->cmd
->error
)
676 ret
= mrq
->cmd
->error
;
677 if (!ret
&& mrq
->data
->error
)
678 ret
= mrq
->data
->error
;
679 if (!ret
&& mrq
->stop
&& mrq
->stop
->error
)
680 ret
= mrq
->stop
->error
;
681 if (!ret
&& mrq
->data
->bytes_xfered
!=
682 mrq
->data
->blocks
* mrq
->data
->blksz
)
686 ret
= RESULT_UNSUP_HOST
;
692 * Checks that a "short transfer" behaved as expected
694 static int mmc_test_check_broken_result(struct mmc_test_card
*test
,
695 struct mmc_request
*mrq
)
699 BUG_ON(!mrq
|| !mrq
->cmd
|| !mrq
->data
);
703 if (!ret
&& mrq
->cmd
->error
)
704 ret
= mrq
->cmd
->error
;
705 if (!ret
&& mrq
->data
->error
== 0)
707 if (!ret
&& mrq
->data
->error
!= -ETIMEDOUT
)
708 ret
= mrq
->data
->error
;
709 if (!ret
&& mrq
->stop
&& mrq
->stop
->error
)
710 ret
= mrq
->stop
->error
;
711 if (mrq
->data
->blocks
> 1) {
712 if (!ret
&& mrq
->data
->bytes_xfered
> mrq
->data
->blksz
)
715 if (!ret
&& mrq
->data
->bytes_xfered
> 0)
720 ret
= RESULT_UNSUP_HOST
;
726 * Tests a basic transfer with certain parameters
728 static int mmc_test_simple_transfer(struct mmc_test_card
*test
,
729 struct scatterlist
*sg
, unsigned sg_len
, unsigned dev_addr
,
730 unsigned blocks
, unsigned blksz
, int write
)
732 struct mmc_request mrq
;
733 struct mmc_command cmd
= {0};
734 struct mmc_command stop
= {0};
735 struct mmc_data data
;
737 memset(&mrq
, 0, sizeof(struct mmc_request
));
738 memset(&data
, 0, sizeof(struct mmc_data
));
744 mmc_test_prepare_mrq(test
, &mrq
, sg
, sg_len
, dev_addr
,
745 blocks
, blksz
, write
);
747 mmc_wait_for_req(test
->card
->host
, &mrq
);
749 mmc_test_wait_busy(test
);
751 return mmc_test_check_result(test
, &mrq
);
755 * Tests a transfer where the card will fail completely or partly
757 static int mmc_test_broken_transfer(struct mmc_test_card
*test
,
758 unsigned blocks
, unsigned blksz
, int write
)
760 struct mmc_request mrq
;
761 struct mmc_command cmd
= {0};
762 struct mmc_command stop
= {0};
763 struct mmc_data data
;
765 struct scatterlist sg
;
767 memset(&mrq
, 0, sizeof(struct mmc_request
));
768 memset(&data
, 0, sizeof(struct mmc_data
));
774 sg_init_one(&sg
, test
->buffer
, blocks
* blksz
);
776 mmc_test_prepare_mrq(test
, &mrq
, &sg
, 1, 0, blocks
, blksz
, write
);
777 mmc_test_prepare_broken_mrq(test
, &mrq
, write
);
779 mmc_wait_for_req(test
->card
->host
, &mrq
);
781 mmc_test_wait_busy(test
);
783 return mmc_test_check_broken_result(test
, &mrq
);
787 * Does a complete transfer test where data is also validated
789 * Note: mmc_test_prepare() must have been done before this call
791 static int mmc_test_transfer(struct mmc_test_card
*test
,
792 struct scatterlist
*sg
, unsigned sg_len
, unsigned dev_addr
,
793 unsigned blocks
, unsigned blksz
, int write
)
799 for (i
= 0;i
< blocks
* blksz
;i
++)
800 test
->scratch
[i
] = i
;
802 memset(test
->scratch
, 0, BUFFER_SIZE
);
804 local_irq_save(flags
);
805 sg_copy_from_buffer(sg
, sg_len
, test
->scratch
, BUFFER_SIZE
);
806 local_irq_restore(flags
);
808 ret
= mmc_test_set_blksize(test
, blksz
);
812 ret
= mmc_test_simple_transfer(test
, sg
, sg_len
, dev_addr
,
813 blocks
, blksz
, write
);
820 ret
= mmc_test_set_blksize(test
, 512);
824 sectors
= (blocks
* blksz
+ 511) / 512;
825 if ((sectors
* 512) == (blocks
* blksz
))
828 if ((sectors
* 512) > BUFFER_SIZE
)
831 memset(test
->buffer
, 0, sectors
* 512);
833 for (i
= 0;i
< sectors
;i
++) {
834 ret
= mmc_test_buffer_transfer(test
,
835 test
->buffer
+ i
* 512,
836 dev_addr
+ i
, 512, 0);
841 for (i
= 0;i
< blocks
* blksz
;i
++) {
842 if (test
->buffer
[i
] != (u8
)i
)
846 for (;i
< sectors
* 512;i
++) {
847 if (test
->buffer
[i
] != 0xDF)
851 local_irq_save(flags
);
852 sg_copy_to_buffer(sg
, sg_len
, test
->scratch
, BUFFER_SIZE
);
853 local_irq_restore(flags
);
854 for (i
= 0;i
< blocks
* blksz
;i
++) {
855 if (test
->scratch
[i
] != (u8
)i
)
863 /*******************************************************************/
865 /*******************************************************************/
867 struct mmc_test_case
{
870 int (*prepare
)(struct mmc_test_card
*);
871 int (*run
)(struct mmc_test_card
*);
872 int (*cleanup
)(struct mmc_test_card
*);
875 static int mmc_test_basic_write(struct mmc_test_card
*test
)
878 struct scatterlist sg
;
880 ret
= mmc_test_set_blksize(test
, 512);
884 sg_init_one(&sg
, test
->buffer
, 512);
886 ret
= mmc_test_simple_transfer(test
, &sg
, 1, 0, 1, 512, 1);
893 static int mmc_test_basic_read(struct mmc_test_card
*test
)
896 struct scatterlist sg
;
898 ret
= mmc_test_set_blksize(test
, 512);
902 sg_init_one(&sg
, test
->buffer
, 512);
904 ret
= mmc_test_simple_transfer(test
, &sg
, 1, 0, 1, 512, 0);
911 static int mmc_test_verify_write(struct mmc_test_card
*test
)
914 struct scatterlist sg
;
916 sg_init_one(&sg
, test
->buffer
, 512);
918 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 1);
925 static int mmc_test_verify_read(struct mmc_test_card
*test
)
928 struct scatterlist sg
;
930 sg_init_one(&sg
, test
->buffer
, 512);
932 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 0);
939 static int mmc_test_multi_write(struct mmc_test_card
*test
)
943 struct scatterlist sg
;
945 if (test
->card
->host
->max_blk_count
== 1)
946 return RESULT_UNSUP_HOST
;
948 size
= PAGE_SIZE
* 2;
949 size
= min(size
, test
->card
->host
->max_req_size
);
950 size
= min(size
, test
->card
->host
->max_seg_size
);
951 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
954 return RESULT_UNSUP_HOST
;
956 sg_init_one(&sg
, test
->buffer
, size
);
958 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 1);
965 static int mmc_test_multi_read(struct mmc_test_card
*test
)
969 struct scatterlist sg
;
971 if (test
->card
->host
->max_blk_count
== 1)
972 return RESULT_UNSUP_HOST
;
974 size
= PAGE_SIZE
* 2;
975 size
= min(size
, test
->card
->host
->max_req_size
);
976 size
= min(size
, test
->card
->host
->max_seg_size
);
977 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
980 return RESULT_UNSUP_HOST
;
982 sg_init_one(&sg
, test
->buffer
, size
);
984 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 0);
991 static int mmc_test_pow2_write(struct mmc_test_card
*test
)
994 struct scatterlist sg
;
996 if (!test
->card
->csd
.write_partial
)
997 return RESULT_UNSUP_CARD
;
999 for (i
= 1; i
< 512;i
<<= 1) {
1000 sg_init_one(&sg
, test
->buffer
, i
);
1001 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 1);
1009 static int mmc_test_pow2_read(struct mmc_test_card
*test
)
1012 struct scatterlist sg
;
1014 if (!test
->card
->csd
.read_partial
)
1015 return RESULT_UNSUP_CARD
;
1017 for (i
= 1; i
< 512;i
<<= 1) {
1018 sg_init_one(&sg
, test
->buffer
, i
);
1019 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 0);
1027 static int mmc_test_weird_write(struct mmc_test_card
*test
)
1030 struct scatterlist sg
;
1032 if (!test
->card
->csd
.write_partial
)
1033 return RESULT_UNSUP_CARD
;
1035 for (i
= 3; i
< 512;i
+= 7) {
1036 sg_init_one(&sg
, test
->buffer
, i
);
1037 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 1);
1045 static int mmc_test_weird_read(struct mmc_test_card
*test
)
1048 struct scatterlist sg
;
1050 if (!test
->card
->csd
.read_partial
)
1051 return RESULT_UNSUP_CARD
;
1053 for (i
= 3; i
< 512;i
+= 7) {
1054 sg_init_one(&sg
, test
->buffer
, i
);
1055 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, i
, 0);
1063 static int mmc_test_align_write(struct mmc_test_card
*test
)
1066 struct scatterlist sg
;
1068 for (i
= 1;i
< 4;i
++) {
1069 sg_init_one(&sg
, test
->buffer
+ i
, 512);
1070 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 1);
1078 static int mmc_test_align_read(struct mmc_test_card
*test
)
1081 struct scatterlist sg
;
1083 for (i
= 1;i
< 4;i
++) {
1084 sg_init_one(&sg
, test
->buffer
+ i
, 512);
1085 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 0);
1093 static int mmc_test_align_multi_write(struct mmc_test_card
*test
)
1097 struct scatterlist sg
;
1099 if (test
->card
->host
->max_blk_count
== 1)
1100 return RESULT_UNSUP_HOST
;
1102 size
= PAGE_SIZE
* 2;
1103 size
= min(size
, test
->card
->host
->max_req_size
);
1104 size
= min(size
, test
->card
->host
->max_seg_size
);
1105 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1108 return RESULT_UNSUP_HOST
;
1110 for (i
= 1;i
< 4;i
++) {
1111 sg_init_one(&sg
, test
->buffer
+ i
, size
);
1112 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 1);
1120 static int mmc_test_align_multi_read(struct mmc_test_card
*test
)
1124 struct scatterlist sg
;
1126 if (test
->card
->host
->max_blk_count
== 1)
1127 return RESULT_UNSUP_HOST
;
1129 size
= PAGE_SIZE
* 2;
1130 size
= min(size
, test
->card
->host
->max_req_size
);
1131 size
= min(size
, test
->card
->host
->max_seg_size
);
1132 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1135 return RESULT_UNSUP_HOST
;
1137 for (i
= 1;i
< 4;i
++) {
1138 sg_init_one(&sg
, test
->buffer
+ i
, size
);
1139 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 0);
1147 static int mmc_test_xfersize_write(struct mmc_test_card
*test
)
1151 ret
= mmc_test_set_blksize(test
, 512);
1155 ret
= mmc_test_broken_transfer(test
, 1, 512, 1);
1162 static int mmc_test_xfersize_read(struct mmc_test_card
*test
)
1166 ret
= mmc_test_set_blksize(test
, 512);
1170 ret
= mmc_test_broken_transfer(test
, 1, 512, 0);
1177 static int mmc_test_multi_xfersize_write(struct mmc_test_card
*test
)
1181 if (test
->card
->host
->max_blk_count
== 1)
1182 return RESULT_UNSUP_HOST
;
1184 ret
= mmc_test_set_blksize(test
, 512);
1188 ret
= mmc_test_broken_transfer(test
, 2, 512, 1);
1195 static int mmc_test_multi_xfersize_read(struct mmc_test_card
*test
)
1199 if (test
->card
->host
->max_blk_count
== 1)
1200 return RESULT_UNSUP_HOST
;
1202 ret
= mmc_test_set_blksize(test
, 512);
1206 ret
= mmc_test_broken_transfer(test
, 2, 512, 0);
1213 #ifdef CONFIG_HIGHMEM
1215 static int mmc_test_write_high(struct mmc_test_card
*test
)
1218 struct scatterlist sg
;
1220 sg_init_table(&sg
, 1);
1221 sg_set_page(&sg
, test
->highmem
, 512, 0);
1223 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 1);
1230 static int mmc_test_read_high(struct mmc_test_card
*test
)
1233 struct scatterlist sg
;
1235 sg_init_table(&sg
, 1);
1236 sg_set_page(&sg
, test
->highmem
, 512, 0);
1238 ret
= mmc_test_transfer(test
, &sg
, 1, 0, 1, 512, 0);
1245 static int mmc_test_multi_write_high(struct mmc_test_card
*test
)
1249 struct scatterlist sg
;
1251 if (test
->card
->host
->max_blk_count
== 1)
1252 return RESULT_UNSUP_HOST
;
1254 size
= PAGE_SIZE
* 2;
1255 size
= min(size
, test
->card
->host
->max_req_size
);
1256 size
= min(size
, test
->card
->host
->max_seg_size
);
1257 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1260 return RESULT_UNSUP_HOST
;
1262 sg_init_table(&sg
, 1);
1263 sg_set_page(&sg
, test
->highmem
, size
, 0);
1265 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 1);
1272 static int mmc_test_multi_read_high(struct mmc_test_card
*test
)
1276 struct scatterlist sg
;
1278 if (test
->card
->host
->max_blk_count
== 1)
1279 return RESULT_UNSUP_HOST
;
1281 size
= PAGE_SIZE
* 2;
1282 size
= min(size
, test
->card
->host
->max_req_size
);
1283 size
= min(size
, test
->card
->host
->max_seg_size
);
1284 size
= min(size
, test
->card
->host
->max_blk_count
* 512);
1287 return RESULT_UNSUP_HOST
;
1289 sg_init_table(&sg
, 1);
1290 sg_set_page(&sg
, test
->highmem
, size
, 0);
1292 ret
= mmc_test_transfer(test
, &sg
, 1, 0, size
/512, 512, 0);
1301 static int mmc_test_no_highmem(struct mmc_test_card
*test
)
1303 printk(KERN_INFO
"%s: Highmem not configured - test skipped\n",
1304 mmc_hostname(test
->card
->host
));
1308 #endif /* CONFIG_HIGHMEM */
1311 * Map sz bytes so that it can be transferred.
1313 static int mmc_test_area_map(struct mmc_test_card
*test
, unsigned long sz
,
1316 struct mmc_test_area
*t
= &test
->area
;
1319 t
->blocks
= sz
>> 9;
1322 err
= mmc_test_map_sg_max_scatter(t
->mem
, sz
, t
->sg
,
1323 t
->max_segs
, t
->max_seg_sz
,
1326 err
= mmc_test_map_sg(t
->mem
, sz
, t
->sg
, 1, t
->max_segs
,
1327 t
->max_seg_sz
, &t
->sg_len
);
1330 printk(KERN_INFO
"%s: Failed to map sg list\n",
1331 mmc_hostname(test
->card
->host
));
1336 * Transfer bytes mapped by mmc_test_area_map().
1338 static int mmc_test_area_transfer(struct mmc_test_card
*test
,
1339 unsigned int dev_addr
, int write
)
1341 struct mmc_test_area
*t
= &test
->area
;
1343 return mmc_test_simple_transfer(test
, t
->sg
, t
->sg_len
, dev_addr
,
1344 t
->blocks
, 512, write
);
1348 * Map and transfer bytes.
1350 static int mmc_test_area_io(struct mmc_test_card
*test
, unsigned long sz
,
1351 unsigned int dev_addr
, int write
, int max_scatter
,
1354 struct timespec ts1
, ts2
;
1358 * In the case of a maximally scattered transfer, the maximum transfer
1359 * size is further limited by using PAGE_SIZE segments.
1362 struct mmc_test_area
*t
= &test
->area
;
1363 unsigned long max_tfr
;
1365 if (t
->max_seg_sz
>= PAGE_SIZE
)
1366 max_tfr
= t
->max_segs
* PAGE_SIZE
;
1368 max_tfr
= t
->max_segs
* t
->max_seg_sz
;
1373 ret
= mmc_test_area_map(test
, sz
, max_scatter
);
1378 getnstimeofday(&ts1
);
1380 ret
= mmc_test_area_transfer(test
, dev_addr
, write
);
1385 getnstimeofday(&ts2
);
1388 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1394 * Write the test area entirely.
1396 static int mmc_test_area_fill(struct mmc_test_card
*test
)
1398 return mmc_test_area_io(test
, test
->area
.max_tfr
, test
->area
.dev_addr
,
1403 * Erase the test area entirely.
1405 static int mmc_test_area_erase(struct mmc_test_card
*test
)
1407 struct mmc_test_area
*t
= &test
->area
;
1409 if (!mmc_can_erase(test
->card
))
1412 return mmc_erase(test
->card
, t
->dev_addr
, test
->area
.max_sz
>> 9,
1417 * Cleanup struct mmc_test_area.
1419 static int mmc_test_area_cleanup(struct mmc_test_card
*test
)
1421 struct mmc_test_area
*t
= &test
->area
;
1424 mmc_test_free_mem(t
->mem
);
1430 * Initialize an area for testing large transfers. The test area is set to the
1431 * middle of the card because cards may have different charateristics at the
1432 * front (for FAT file system optimization). Optionally, the area is erased
1433 * (if the card supports it) which may improve write performance. Optionally,
1434 * the area is filled with data for subsequent read tests.
1436 static int mmc_test_area_init(struct mmc_test_card
*test
, int erase
, int fill
)
1438 struct mmc_test_area
*t
= &test
->area
;
1439 unsigned long min_sz
= 64 * 1024, sz
;
1442 ret
= mmc_test_set_blksize(test
, 512);
1446 /* Make the test area size about 4MiB */
1447 sz
= (unsigned long)test
->card
->pref_erase
<< 9;
1449 while (t
->max_sz
< 4 * 1024 * 1024)
1451 while (t
->max_sz
> TEST_AREA_MAX_SIZE
&& t
->max_sz
> sz
)
1454 t
->max_segs
= test
->card
->host
->max_segs
;
1455 t
->max_seg_sz
= test
->card
->host
->max_seg_size
;
1457 t
->max_tfr
= t
->max_sz
;
1458 if (t
->max_tfr
>> 9 > test
->card
->host
->max_blk_count
)
1459 t
->max_tfr
= test
->card
->host
->max_blk_count
<< 9;
1460 if (t
->max_tfr
> test
->card
->host
->max_req_size
)
1461 t
->max_tfr
= test
->card
->host
->max_req_size
;
1462 if (t
->max_tfr
/ t
->max_seg_sz
> t
->max_segs
)
1463 t
->max_tfr
= t
->max_segs
* t
->max_seg_sz
;
1466 * Try to allocate enough memory for a max. sized transfer. Less is OK
1467 * because the same memory can be mapped into the scatterlist more than
1468 * once. Also, take into account the limits imposed on scatterlist
1469 * segments by the host driver.
1471 t
->mem
= mmc_test_alloc_mem(min_sz
, t
->max_tfr
, t
->max_segs
,
1476 t
->sg
= kmalloc(sizeof(struct scatterlist
) * t
->max_segs
, GFP_KERNEL
);
1482 t
->dev_addr
= mmc_test_capacity(test
->card
) / 2;
1483 t
->dev_addr
-= t
->dev_addr
% (t
->max_sz
>> 9);
1486 ret
= mmc_test_area_erase(test
);
1492 ret
= mmc_test_area_fill(test
);
1500 mmc_test_area_cleanup(test
);
1505 * Prepare for large transfers. Do not erase the test area.
1507 static int mmc_test_area_prepare(struct mmc_test_card
*test
)
1509 return mmc_test_area_init(test
, 0, 0);
1513 * Prepare for large transfers. Do erase the test area.
1515 static int mmc_test_area_prepare_erase(struct mmc_test_card
*test
)
1517 return mmc_test_area_init(test
, 1, 0);
1521 * Prepare for large transfers. Erase and fill the test area.
1523 static int mmc_test_area_prepare_fill(struct mmc_test_card
*test
)
1525 return mmc_test_area_init(test
, 1, 1);
1529 * Test best-case performance. Best-case performance is expected from
1530 * a single large transfer.
1532 * An additional option (max_scatter) allows the measurement of the same
1533 * transfer but with no contiguous pages in the scatter list. This tests
1534 * the efficiency of DMA to handle scattered pages.
1536 static int mmc_test_best_performance(struct mmc_test_card
*test
, int write
,
1539 return mmc_test_area_io(test
, test
->area
.max_tfr
, test
->area
.dev_addr
,
1540 write
, max_scatter
, 1);
1544 * Best-case read performance.
1546 static int mmc_test_best_read_performance(struct mmc_test_card
*test
)
1548 return mmc_test_best_performance(test
, 0, 0);
1552 * Best-case write performance.
1554 static int mmc_test_best_write_performance(struct mmc_test_card
*test
)
1556 return mmc_test_best_performance(test
, 1, 0);
1560 * Best-case read performance into scattered pages.
1562 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card
*test
)
1564 return mmc_test_best_performance(test
, 0, 1);
1568 * Best-case write performance from scattered pages.
1570 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card
*test
)
1572 return mmc_test_best_performance(test
, 1, 1);
1576 * Single read performance by transfer size.
1578 static int mmc_test_profile_read_perf(struct mmc_test_card
*test
)
1581 unsigned int dev_addr
;
1584 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1585 dev_addr
= test
->area
.dev_addr
+ (sz
>> 9);
1586 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 0, 0, 1);
1590 sz
= test
->area
.max_tfr
;
1591 dev_addr
= test
->area
.dev_addr
;
1592 return mmc_test_area_io(test
, sz
, dev_addr
, 0, 0, 1);
1596 * Single write performance by transfer size.
1598 static int mmc_test_profile_write_perf(struct mmc_test_card
*test
)
1601 unsigned int dev_addr
;
1604 ret
= mmc_test_area_erase(test
);
1607 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1608 dev_addr
= test
->area
.dev_addr
+ (sz
>> 9);
1609 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 1, 0, 1);
1613 ret
= mmc_test_area_erase(test
);
1616 sz
= test
->area
.max_tfr
;
1617 dev_addr
= test
->area
.dev_addr
;
1618 return mmc_test_area_io(test
, sz
, dev_addr
, 1, 0, 1);
1622 * Single trim performance by transfer size.
1624 static int mmc_test_profile_trim_perf(struct mmc_test_card
*test
)
1627 unsigned int dev_addr
;
1628 struct timespec ts1
, ts2
;
1631 if (!mmc_can_trim(test
->card
))
1632 return RESULT_UNSUP_CARD
;
1634 if (!mmc_can_erase(test
->card
))
1635 return RESULT_UNSUP_HOST
;
1637 for (sz
= 512; sz
< test
->area
.max_sz
; sz
<<= 1) {
1638 dev_addr
= test
->area
.dev_addr
+ (sz
>> 9);
1639 getnstimeofday(&ts1
);
1640 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9, MMC_TRIM_ARG
);
1643 getnstimeofday(&ts2
);
1644 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1646 dev_addr
= test
->area
.dev_addr
;
1647 getnstimeofday(&ts1
);
1648 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9, MMC_TRIM_ARG
);
1651 getnstimeofday(&ts2
);
1652 mmc_test_print_rate(test
, sz
, &ts1
, &ts2
);
1656 static int mmc_test_seq_read_perf(struct mmc_test_card
*test
, unsigned long sz
)
1658 unsigned int dev_addr
, i
, cnt
;
1659 struct timespec ts1
, ts2
;
1662 cnt
= test
->area
.max_sz
/ sz
;
1663 dev_addr
= test
->area
.dev_addr
;
1664 getnstimeofday(&ts1
);
1665 for (i
= 0; i
< cnt
; i
++) {
1666 ret
= mmc_test_area_io(test
, sz
, dev_addr
, 0, 0, 0);
1669 dev_addr
+= (sz
>> 9);
1671 getnstimeofday(&ts2
);
1672 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1677 * Consecutive read performance by transfer size.
1679 static int mmc_test_profile_seq_read_perf(struct mmc_test_card
*test
)
1684 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1685 ret
= mmc_test_seq_read_perf(test
, sz
);
1689 sz
= test
->area
.max_tfr
;
1690 return mmc_test_seq_read_perf(test
, sz
);
1693 static int mmc_test_seq_write_perf(struct mmc_test_card
*test
, unsigned long sz
)
1695 unsigned int dev_addr
, i
, cnt
;
1696 struct timespec ts1
, ts2
;
1699 ret
= mmc_test_area_erase(test
);
1702 cnt
= test
->area
.max_sz
/ sz
;
1703 dev_addr
= test
->area
.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
)
1724 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1725 ret
= mmc_test_seq_write_perf(test
, sz
);
1729 sz
= test
->area
.max_tfr
;
1730 return mmc_test_seq_write_perf(test
, sz
);
1734 * Consecutive trim performance by transfer size.
1736 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card
*test
)
1739 unsigned int dev_addr
, i
, cnt
;
1740 struct timespec ts1
, ts2
;
1743 if (!mmc_can_trim(test
->card
))
1744 return RESULT_UNSUP_CARD
;
1746 if (!mmc_can_erase(test
->card
))
1747 return RESULT_UNSUP_HOST
;
1749 for (sz
= 512; sz
<= test
->area
.max_sz
; sz
<<= 1) {
1750 ret
= mmc_test_area_erase(test
);
1753 ret
= mmc_test_area_fill(test
);
1756 cnt
= test
->area
.max_sz
/ sz
;
1757 dev_addr
= test
->area
.dev_addr
;
1758 getnstimeofday(&ts1
);
1759 for (i
= 0; i
< cnt
; i
++) {
1760 ret
= mmc_erase(test
->card
, dev_addr
, sz
>> 9,
1764 dev_addr
+= (sz
>> 9);
1766 getnstimeofday(&ts2
);
1767 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1772 static unsigned int rnd_next
= 1;
1774 static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt
)
1778 rnd_next
= rnd_next
* 1103515245 + 12345;
1779 r
= (rnd_next
>> 16) & 0x7fff;
1780 return (r
* rnd_cnt
) >> 15;
1783 static int mmc_test_rnd_perf(struct mmc_test_card
*test
, int write
, int print
,
1786 unsigned int dev_addr
, cnt
, rnd_addr
, range1
, range2
, last_ea
= 0, ea
;
1788 struct timespec ts1
, ts2
, ts
;
1793 rnd_addr
= mmc_test_capacity(test
->card
) / 4;
1794 range1
= rnd_addr
/ test
->card
->pref_erase
;
1795 range2
= range1
/ ssz
;
1797 getnstimeofday(&ts1
);
1798 for (cnt
= 0; cnt
< UINT_MAX
; cnt
++) {
1799 getnstimeofday(&ts2
);
1800 ts
= timespec_sub(ts2
, ts1
);
1801 if (ts
.tv_sec
>= 10)
1803 ea
= mmc_test_rnd_num(range1
);
1807 dev_addr
= rnd_addr
+ test
->card
->pref_erase
* ea
+
1808 ssz
* mmc_test_rnd_num(range2
);
1809 ret
= mmc_test_area_io(test
, sz
, dev_addr
, write
, 0, 0);
1814 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1818 static int mmc_test_random_perf(struct mmc_test_card
*test
, int write
)
1824 for (sz
= 512; sz
< test
->area
.max_tfr
; sz
<<= 1) {
1826 * When writing, try to get more consistent results by running
1827 * the test twice with exactly the same I/O but outputting the
1828 * results only for the 2nd run.
1832 ret
= mmc_test_rnd_perf(test
, write
, 0, sz
);
1837 ret
= mmc_test_rnd_perf(test
, write
, 1, sz
);
1841 sz
= test
->area
.max_tfr
;
1844 ret
= mmc_test_rnd_perf(test
, write
, 0, sz
);
1849 return mmc_test_rnd_perf(test
, write
, 1, sz
);
1853 * Random read performance by transfer size.
1855 static int mmc_test_random_read_perf(struct mmc_test_card
*test
)
1857 return mmc_test_random_perf(test
, 0);
1861 * Random write performance by transfer size.
1863 static int mmc_test_random_write_perf(struct mmc_test_card
*test
)
1865 return mmc_test_random_perf(test
, 1);
1868 static int mmc_test_seq_perf(struct mmc_test_card
*test
, int write
,
1869 unsigned int tot_sz
, int max_scatter
)
1871 unsigned int dev_addr
, i
, cnt
, sz
, ssz
;
1872 struct timespec ts1
, ts2
;
1875 sz
= test
->area
.max_tfr
;
1877 * In the case of a maximally scattered transfer, the maximum transfer
1878 * size is further limited by using PAGE_SIZE segments.
1881 struct mmc_test_area
*t
= &test
->area
;
1882 unsigned long max_tfr
;
1884 if (t
->max_seg_sz
>= PAGE_SIZE
)
1885 max_tfr
= t
->max_segs
* PAGE_SIZE
;
1887 max_tfr
= t
->max_segs
* t
->max_seg_sz
;
1893 dev_addr
= mmc_test_capacity(test
->card
) / 4;
1894 if (tot_sz
> dev_addr
<< 9)
1895 tot_sz
= dev_addr
<< 9;
1897 dev_addr
&= 0xffff0000; /* Round to 64MiB boundary */
1899 getnstimeofday(&ts1
);
1900 for (i
= 0; i
< cnt
; i
++) {
1901 ret
= mmc_test_area_io(test
, sz
, dev_addr
, write
,
1907 getnstimeofday(&ts2
);
1909 mmc_test_print_avg_rate(test
, sz
, cnt
, &ts1
, &ts2
);
1914 static int mmc_test_large_seq_perf(struct mmc_test_card
*test
, int write
)
1918 for (i
= 0; i
< 10; i
++) {
1919 ret
= mmc_test_seq_perf(test
, write
, 10 * 1024 * 1024, 1);
1923 for (i
= 0; i
< 5; i
++) {
1924 ret
= mmc_test_seq_perf(test
, write
, 100 * 1024 * 1024, 1);
1928 for (i
= 0; i
< 3; i
++) {
1929 ret
= mmc_test_seq_perf(test
, write
, 1000 * 1024 * 1024, 1);
1938 * Large sequential read performance.
1940 static int mmc_test_large_seq_read_perf(struct mmc_test_card
*test
)
1942 return mmc_test_large_seq_perf(test
, 0);
1946 * Large sequential write performance.
1948 static int mmc_test_large_seq_write_perf(struct mmc_test_card
*test
)
1950 return mmc_test_large_seq_perf(test
, 1);
1953 static const struct mmc_test_case mmc_test_cases
[] = {
1955 .name
= "Basic write (no data verification)",
1956 .run
= mmc_test_basic_write
,
1960 .name
= "Basic read (no data verification)",
1961 .run
= mmc_test_basic_read
,
1965 .name
= "Basic write (with data verification)",
1966 .prepare
= mmc_test_prepare_write
,
1967 .run
= mmc_test_verify_write
,
1968 .cleanup
= mmc_test_cleanup
,
1972 .name
= "Basic read (with data verification)",
1973 .prepare
= mmc_test_prepare_read
,
1974 .run
= mmc_test_verify_read
,
1975 .cleanup
= mmc_test_cleanup
,
1979 .name
= "Multi-block write",
1980 .prepare
= mmc_test_prepare_write
,
1981 .run
= mmc_test_multi_write
,
1982 .cleanup
= mmc_test_cleanup
,
1986 .name
= "Multi-block read",
1987 .prepare
= mmc_test_prepare_read
,
1988 .run
= mmc_test_multi_read
,
1989 .cleanup
= mmc_test_cleanup
,
1993 .name
= "Power of two block writes",
1994 .prepare
= mmc_test_prepare_write
,
1995 .run
= mmc_test_pow2_write
,
1996 .cleanup
= mmc_test_cleanup
,
2000 .name
= "Power of two block reads",
2001 .prepare
= mmc_test_prepare_read
,
2002 .run
= mmc_test_pow2_read
,
2003 .cleanup
= mmc_test_cleanup
,
2007 .name
= "Weird sized block writes",
2008 .prepare
= mmc_test_prepare_write
,
2009 .run
= mmc_test_weird_write
,
2010 .cleanup
= mmc_test_cleanup
,
2014 .name
= "Weird sized block reads",
2015 .prepare
= mmc_test_prepare_read
,
2016 .run
= mmc_test_weird_read
,
2017 .cleanup
= mmc_test_cleanup
,
2021 .name
= "Badly aligned write",
2022 .prepare
= mmc_test_prepare_write
,
2023 .run
= mmc_test_align_write
,
2024 .cleanup
= mmc_test_cleanup
,
2028 .name
= "Badly aligned read",
2029 .prepare
= mmc_test_prepare_read
,
2030 .run
= mmc_test_align_read
,
2031 .cleanup
= mmc_test_cleanup
,
2035 .name
= "Badly aligned multi-block write",
2036 .prepare
= mmc_test_prepare_write
,
2037 .run
= mmc_test_align_multi_write
,
2038 .cleanup
= mmc_test_cleanup
,
2042 .name
= "Badly aligned multi-block read",
2043 .prepare
= mmc_test_prepare_read
,
2044 .run
= mmc_test_align_multi_read
,
2045 .cleanup
= mmc_test_cleanup
,
2049 .name
= "Correct xfer_size at write (start failure)",
2050 .run
= mmc_test_xfersize_write
,
2054 .name
= "Correct xfer_size at read (start failure)",
2055 .run
= mmc_test_xfersize_read
,
2059 .name
= "Correct xfer_size at write (midway failure)",
2060 .run
= mmc_test_multi_xfersize_write
,
2064 .name
= "Correct xfer_size at read (midway failure)",
2065 .run
= mmc_test_multi_xfersize_read
,
2068 #ifdef CONFIG_HIGHMEM
2071 .name
= "Highmem write",
2072 .prepare
= mmc_test_prepare_write
,
2073 .run
= mmc_test_write_high
,
2074 .cleanup
= mmc_test_cleanup
,
2078 .name
= "Highmem read",
2079 .prepare
= mmc_test_prepare_read
,
2080 .run
= mmc_test_read_high
,
2081 .cleanup
= mmc_test_cleanup
,
2085 .name
= "Multi-block highmem write",
2086 .prepare
= mmc_test_prepare_write
,
2087 .run
= mmc_test_multi_write_high
,
2088 .cleanup
= mmc_test_cleanup
,
2092 .name
= "Multi-block highmem read",
2093 .prepare
= mmc_test_prepare_read
,
2094 .run
= mmc_test_multi_read_high
,
2095 .cleanup
= mmc_test_cleanup
,
2101 .name
= "Highmem write",
2102 .run
= mmc_test_no_highmem
,
2106 .name
= "Highmem read",
2107 .run
= mmc_test_no_highmem
,
2111 .name
= "Multi-block highmem write",
2112 .run
= mmc_test_no_highmem
,
2116 .name
= "Multi-block highmem read",
2117 .run
= mmc_test_no_highmem
,
2120 #endif /* CONFIG_HIGHMEM */
2123 .name
= "Best-case read performance",
2124 .prepare
= mmc_test_area_prepare_fill
,
2125 .run
= mmc_test_best_read_performance
,
2126 .cleanup
= mmc_test_area_cleanup
,
2130 .name
= "Best-case write performance",
2131 .prepare
= mmc_test_area_prepare_erase
,
2132 .run
= mmc_test_best_write_performance
,
2133 .cleanup
= mmc_test_area_cleanup
,
2137 .name
= "Best-case read performance into scattered pages",
2138 .prepare
= mmc_test_area_prepare_fill
,
2139 .run
= mmc_test_best_read_perf_max_scatter
,
2140 .cleanup
= mmc_test_area_cleanup
,
2144 .name
= "Best-case write performance from scattered pages",
2145 .prepare
= mmc_test_area_prepare_erase
,
2146 .run
= mmc_test_best_write_perf_max_scatter
,
2147 .cleanup
= mmc_test_area_cleanup
,
2151 .name
= "Single read performance by transfer size",
2152 .prepare
= mmc_test_area_prepare_fill
,
2153 .run
= mmc_test_profile_read_perf
,
2154 .cleanup
= mmc_test_area_cleanup
,
2158 .name
= "Single write performance by transfer size",
2159 .prepare
= mmc_test_area_prepare
,
2160 .run
= mmc_test_profile_write_perf
,
2161 .cleanup
= mmc_test_area_cleanup
,
2165 .name
= "Single trim performance by transfer size",
2166 .prepare
= mmc_test_area_prepare_fill
,
2167 .run
= mmc_test_profile_trim_perf
,
2168 .cleanup
= mmc_test_area_cleanup
,
2172 .name
= "Consecutive read performance by transfer size",
2173 .prepare
= mmc_test_area_prepare_fill
,
2174 .run
= mmc_test_profile_seq_read_perf
,
2175 .cleanup
= mmc_test_area_cleanup
,
2179 .name
= "Consecutive write performance by transfer size",
2180 .prepare
= mmc_test_area_prepare
,
2181 .run
= mmc_test_profile_seq_write_perf
,
2182 .cleanup
= mmc_test_area_cleanup
,
2186 .name
= "Consecutive trim performance by transfer size",
2187 .prepare
= mmc_test_area_prepare
,
2188 .run
= mmc_test_profile_seq_trim_perf
,
2189 .cleanup
= mmc_test_area_cleanup
,
2193 .name
= "Random read performance by transfer size",
2194 .prepare
= mmc_test_area_prepare
,
2195 .run
= mmc_test_random_read_perf
,
2196 .cleanup
= mmc_test_area_cleanup
,
2200 .name
= "Random write performance by transfer size",
2201 .prepare
= mmc_test_area_prepare
,
2202 .run
= mmc_test_random_write_perf
,
2203 .cleanup
= mmc_test_area_cleanup
,
2207 .name
= "Large sequential read into scattered pages",
2208 .prepare
= mmc_test_area_prepare
,
2209 .run
= mmc_test_large_seq_read_perf
,
2210 .cleanup
= mmc_test_area_cleanup
,
2214 .name
= "Large sequential write from scattered pages",
2215 .prepare
= mmc_test_area_prepare
,
2216 .run
= mmc_test_large_seq_write_perf
,
2217 .cleanup
= mmc_test_area_cleanup
,
2222 static DEFINE_MUTEX(mmc_test_lock
);
2224 static LIST_HEAD(mmc_test_result
);
2226 static void mmc_test_run(struct mmc_test_card
*test
, int testcase
)
2230 printk(KERN_INFO
"%s: Starting tests of card %s...\n",
2231 mmc_hostname(test
->card
->host
), mmc_card_id(test
->card
));
2233 mmc_claim_host(test
->card
->host
);
2235 for (i
= 0;i
< ARRAY_SIZE(mmc_test_cases
);i
++) {
2236 struct mmc_test_general_result
*gr
;
2238 if (testcase
&& ((i
+ 1) != testcase
))
2241 printk(KERN_INFO
"%s: Test case %d. %s...\n",
2242 mmc_hostname(test
->card
->host
), i
+ 1,
2243 mmc_test_cases
[i
].name
);
2245 if (mmc_test_cases
[i
].prepare
) {
2246 ret
= mmc_test_cases
[i
].prepare(test
);
2248 printk(KERN_INFO
"%s: Result: Prepare "
2249 "stage failed! (%d)\n",
2250 mmc_hostname(test
->card
->host
),
2256 gr
= kzalloc(sizeof(struct mmc_test_general_result
),
2259 INIT_LIST_HEAD(&gr
->tr_lst
);
2261 /* Assign data what we know already */
2262 gr
->card
= test
->card
;
2265 /* Append container to global one */
2266 list_add_tail(&gr
->link
, &mmc_test_result
);
2269 * Save the pointer to created container in our private
2275 ret
= mmc_test_cases
[i
].run(test
);
2278 printk(KERN_INFO
"%s: Result: OK\n",
2279 mmc_hostname(test
->card
->host
));
2282 printk(KERN_INFO
"%s: Result: FAILED\n",
2283 mmc_hostname(test
->card
->host
));
2285 case RESULT_UNSUP_HOST
:
2286 printk(KERN_INFO
"%s: Result: UNSUPPORTED "
2288 mmc_hostname(test
->card
->host
));
2290 case RESULT_UNSUP_CARD
:
2291 printk(KERN_INFO
"%s: Result: UNSUPPORTED "
2293 mmc_hostname(test
->card
->host
));
2296 printk(KERN_INFO
"%s: Result: ERROR (%d)\n",
2297 mmc_hostname(test
->card
->host
), ret
);
2300 /* Save the result */
2304 if (mmc_test_cases
[i
].cleanup
) {
2305 ret
= mmc_test_cases
[i
].cleanup(test
);
2307 printk(KERN_INFO
"%s: Warning: Cleanup "
2308 "stage failed! (%d)\n",
2309 mmc_hostname(test
->card
->host
),
2315 mmc_release_host(test
->card
->host
);
2317 printk(KERN_INFO
"%s: Tests completed.\n",
2318 mmc_hostname(test
->card
->host
));
2321 static void mmc_test_free_result(struct mmc_card
*card
)
2323 struct mmc_test_general_result
*gr
, *grs
;
2325 mutex_lock(&mmc_test_lock
);
2327 list_for_each_entry_safe(gr
, grs
, &mmc_test_result
, link
) {
2328 struct mmc_test_transfer_result
*tr
, *trs
;
2330 if (card
&& gr
->card
!= card
)
2333 list_for_each_entry_safe(tr
, trs
, &gr
->tr_lst
, link
) {
2334 list_del(&tr
->link
);
2338 list_del(&gr
->link
);
2342 mutex_unlock(&mmc_test_lock
);
2345 static LIST_HEAD(mmc_test_file_test
);
2347 static int mtf_test_show(struct seq_file
*sf
, void *data
)
2349 struct mmc_card
*card
= (struct mmc_card
*)sf
->private;
2350 struct mmc_test_general_result
*gr
;
2352 mutex_lock(&mmc_test_lock
);
2354 list_for_each_entry(gr
, &mmc_test_result
, link
) {
2355 struct mmc_test_transfer_result
*tr
;
2357 if (gr
->card
!= card
)
2360 seq_printf(sf
, "Test %d: %d\n", gr
->testcase
+ 1, gr
->result
);
2362 list_for_each_entry(tr
, &gr
->tr_lst
, link
) {
2363 seq_printf(sf
, "%u %d %lu.%09lu %u %u.%02u\n",
2364 tr
->count
, tr
->sectors
,
2365 (unsigned long)tr
->ts
.tv_sec
,
2366 (unsigned long)tr
->ts
.tv_nsec
,
2367 tr
->rate
, tr
->iops
/ 100, tr
->iops
% 100);
2371 mutex_unlock(&mmc_test_lock
);
2376 static int mtf_test_open(struct inode
*inode
, struct file
*file
)
2378 return single_open(file
, mtf_test_show
, inode
->i_private
);
2381 static ssize_t
mtf_test_write(struct file
*file
, const char __user
*buf
,
2382 size_t count
, loff_t
*pos
)
2384 struct seq_file
*sf
= (struct seq_file
*)file
->private_data
;
2385 struct mmc_card
*card
= (struct mmc_card
*)sf
->private;
2386 struct mmc_test_card
*test
;
2390 if (count
>= sizeof(lbuf
))
2393 if (copy_from_user(lbuf
, buf
, count
))
2397 if (strict_strtol(lbuf
, 10, &testcase
))
2400 test
= kzalloc(sizeof(struct mmc_test_card
), GFP_KERNEL
);
2405 * Remove all test cases associated with given card. Thus we have only
2406 * actual data of the last run.
2408 mmc_test_free_result(card
);
2412 test
->buffer
= kzalloc(BUFFER_SIZE
, GFP_KERNEL
);
2413 #ifdef CONFIG_HIGHMEM
2414 test
->highmem
= alloc_pages(GFP_KERNEL
| __GFP_HIGHMEM
, BUFFER_ORDER
);
2417 #ifdef CONFIG_HIGHMEM
2418 if (test
->buffer
&& test
->highmem
) {
2422 mutex_lock(&mmc_test_lock
);
2423 mmc_test_run(test
, testcase
);
2424 mutex_unlock(&mmc_test_lock
);
2427 #ifdef CONFIG_HIGHMEM
2428 __free_pages(test
->highmem
, BUFFER_ORDER
);
2430 kfree(test
->buffer
);
2436 static const struct file_operations mmc_test_fops_test
= {
2437 .open
= mtf_test_open
,
2439 .write
= mtf_test_write
,
2440 .llseek
= seq_lseek
,
2441 .release
= single_release
,
2444 static void mmc_test_free_file_test(struct mmc_card
*card
)
2446 struct mmc_test_dbgfs_file
*df
, *dfs
;
2448 mutex_lock(&mmc_test_lock
);
2450 list_for_each_entry_safe(df
, dfs
, &mmc_test_file_test
, link
) {
2451 if (card
&& df
->card
!= card
)
2453 debugfs_remove(df
->file
);
2454 list_del(&df
->link
);
2458 mutex_unlock(&mmc_test_lock
);
2461 static int mmc_test_register_file_test(struct mmc_card
*card
)
2463 struct dentry
*file
= NULL
;
2464 struct mmc_test_dbgfs_file
*df
;
2467 mutex_lock(&mmc_test_lock
);
2469 if (card
->debugfs_root
)
2470 file
= debugfs_create_file("test", S_IWUSR
| S_IRUGO
,
2471 card
->debugfs_root
, card
, &mmc_test_fops_test
);
2473 if (IS_ERR_OR_NULL(file
)) {
2475 "Can't create file. Perhaps debugfs is disabled.\n");
2480 df
= kmalloc(sizeof(struct mmc_test_dbgfs_file
), GFP_KERNEL
);
2482 debugfs_remove(file
);
2484 "Can't allocate memory for internal usage.\n");
2492 list_add(&df
->link
, &mmc_test_file_test
);
2495 mutex_unlock(&mmc_test_lock
);
2500 static int mmc_test_probe(struct mmc_card
*card
)
2504 if (!mmc_card_mmc(card
) && !mmc_card_sd(card
))
2507 ret
= mmc_test_register_file_test(card
);
2511 dev_info(&card
->dev
, "Card claimed for testing.\n");
2516 static void mmc_test_remove(struct mmc_card
*card
)
2518 mmc_test_free_result(card
);
2519 mmc_test_free_file_test(card
);
2522 static struct mmc_driver mmc_driver
= {
2526 .probe
= mmc_test_probe
,
2527 .remove
= mmc_test_remove
,
2530 static int __init
mmc_test_init(void)
2532 return mmc_register_driver(&mmc_driver
);
2535 static void __exit
mmc_test_exit(void)
2537 /* Clear stalled data if card is still plugged */
2538 mmc_test_free_result(NULL
);
2539 mmc_test_free_file_test(NULL
);
2541 mmc_unregister_driver(&mmc_driver
);
2544 module_init(mmc_test_init
);
2545 module_exit(mmc_test_exit
);
2547 MODULE_LICENSE("GPL");
2548 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
2549 MODULE_AUTHOR("Pierre Ossman");