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
92 struct mmc_test_transfer_result {
93 struct list_head link;
101 * struct mmc_test_general_result - results for tests.
102 * @link: double-linked list
103 * @card: card under test
104 * @testcase: number of test case
105 * @result: result of test run
106 * @tr_lst: transfer measurements if any as mmc_test_transfer_result
108 struct mmc_test_general_result {
109 struct list_head link;
110 struct mmc_card *card;
113 struct list_head tr_lst;
117 * struct mmc_test_dbgfs_file - debugfs related file.
118 * @link: double-linked list
119 * @card: card under test
120 * @file: file created under debugfs
122 struct mmc_test_dbgfs_file {
123 struct list_head link;
124 struct mmc_card *card;
129 * struct mmc_test_card - test information.
130 * @card: card under test
131 * @scratch: transfer buffer
132 * @buffer: transfer buffer
133 * @highmem: buffer for highmem tests
134 * @area: information for performance tests
135 * @gr: pointer to results of current testcase
137 struct mmc_test_card {
138 struct mmc_card *card;
140 u8 scratch[BUFFER_SIZE];
142 #ifdef CONFIG_HIGHMEM
143 struct page *highmem;
145 struct mmc_test_area area;
146 struct mmc_test_general_result *gr;
149 /*******************************************************************/
150 /* General helper functions */
151 /*******************************************************************/
154 * Configure correct block size in card
156 static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
158 return mmc_set_blocklen(test->card, size);
162 * Fill in the mmc_request structure given a set of transfer parameters.
164 static void mmc_test_prepare_mrq(struct mmc_test_card *test,
165 struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
166 unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
168 BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
171 mrq->cmd->opcode = write ?
172 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
174 mrq->cmd->opcode = write ?
175 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
178 mrq->cmd->arg = dev_addr;
179 if (!mmc_card_blockaddr(test->card))
182 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
187 mrq->stop->opcode = MMC_STOP_TRANSMISSION;
189 mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
192 mrq->data->blksz = blksz;
193 mrq->data->blocks = blocks;
194 mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
196 mrq->data->sg_len = sg_len;
198 mmc_set_data_timeout(mrq->data, test->card);
201 static int mmc_test_busy(struct mmc_command *cmd)
203 return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
204 (R1_CURRENT_STATE(cmd->resp[0]) == 7);
208 * Wait for the card to finish the busy state
210 static int mmc_test_wait_busy(struct mmc_test_card *test)
213 struct mmc_command cmd;
217 memset(&cmd, 0, sizeof(struct mmc_command));
219 cmd.opcode = MMC_SEND_STATUS;
220 cmd.arg = test->card->rca << 16;
221 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
223 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
227 if (!busy && mmc_test_busy(&cmd)) {
229 printk(KERN_INFO "%s: Warning: Host did not "
230 "wait for busy state to end.\n",
231 mmc_hostname(test->card->host));
233 } while (mmc_test_busy(&cmd));
239 * Transfer a single sector of kernel addressable data
241 static int mmc_test_buffer_transfer(struct mmc_test_card *test,
242 u8 *buffer, unsigned addr, unsigned blksz, int write)
246 struct mmc_request mrq;
247 struct mmc_command cmd;
248 struct mmc_command stop;
249 struct mmc_data data;
251 struct scatterlist sg;
253 memset(&mrq, 0, sizeof(struct mmc_request));
254 memset(&cmd, 0, sizeof(struct mmc_command));
255 memset(&data, 0, sizeof(struct mmc_data));
256 memset(&stop, 0, sizeof(struct mmc_command));
262 sg_init_one(&sg, buffer, blksz);
264 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
266 mmc_wait_for_req(test->card->host, &mrq);
273 ret = mmc_test_wait_busy(test);
280 static void mmc_test_free_mem(struct mmc_test_mem *mem)
285 __free_pages(mem->arr[mem->cnt].page,
286 mem->arr[mem->cnt].order);
292 * Allocate a lot of memory, preferrably max_sz but at least min_sz. In case
293 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
294 * not exceed a maximum number of segments and try not to make segments much
295 * bigger than maximum segment size.
297 static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
298 unsigned long max_sz,
299 unsigned int max_segs,
300 unsigned int max_seg_sz)
302 unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
303 unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
304 unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
305 unsigned long page_cnt = 0;
306 unsigned long limit = nr_free_buffer_pages() >> 4;
307 struct mmc_test_mem *mem;
309 if (max_page_cnt > limit)
310 max_page_cnt = limit;
311 if (min_page_cnt > max_page_cnt)
312 min_page_cnt = max_page_cnt;
314 if (max_seg_page_cnt > max_page_cnt)
315 max_seg_page_cnt = max_page_cnt;
317 if (max_segs > max_page_cnt)
318 max_segs = max_page_cnt;
320 mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
324 mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_segs,
329 while (max_page_cnt) {
332 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
335 order = get_order(max_seg_page_cnt << PAGE_SHIFT);
337 page = alloc_pages(flags, order);
343 if (page_cnt < min_page_cnt)
347 mem->arr[mem->cnt].page = page;
348 mem->arr[mem->cnt].order = order;
350 if (max_page_cnt <= (1UL << order))
352 max_page_cnt -= 1UL << order;
353 page_cnt += 1UL << order;
354 if (mem->cnt >= max_segs) {
355 if (page_cnt < min_page_cnt)
364 mmc_test_free_mem(mem);
369 * Map memory into a scatterlist. Optionally allow the same memory to be
370 * mapped more than once.
372 static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
373 struct scatterlist *sglist, int repeat,
374 unsigned int max_segs, unsigned int max_seg_sz,
375 unsigned int *sg_len)
377 struct scatterlist *sg = NULL;
380 sg_init_table(sglist, max_segs);
384 for (i = 0; i < mem->cnt; i++) {
385 unsigned long len = PAGE_SIZE << mem->arr[i].order;
389 if (len > max_seg_sz)
397 sg_set_page(sg, mem->arr[i].page, len, 0);
403 } while (sz && repeat);
415 * Map memory into a scatterlist so that no pages are contiguous. Allow the
416 * same memory to be mapped more than once.
418 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
420 struct scatterlist *sglist,
421 unsigned int max_segs,
422 unsigned int max_seg_sz,
423 unsigned int *sg_len)
425 struct scatterlist *sg = NULL;
426 unsigned int i = mem->cnt, cnt;
428 void *base, *addr, *last_addr = NULL;
430 sg_init_table(sglist, max_segs);
434 base = page_address(mem->arr[--i].page);
435 cnt = 1 << mem->arr[i].order;
437 addr = base + PAGE_SIZE * --cnt;
438 if (last_addr && last_addr + PAGE_SIZE == addr)
442 if (len > max_seg_sz)
452 sg_set_page(sg, virt_to_page(addr), len, 0);
467 * Calculate transfer rate in bytes per second.
469 static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
479 while (ns > UINT_MAX) {
487 do_div(bytes, (uint32_t)ns);
493 * Save transfer results for future usage
495 static void mmc_test_save_transfer_result(struct mmc_test_card *test,
496 unsigned int count, unsigned int sectors, struct timespec ts,
499 struct mmc_test_transfer_result *tr;
504 tr = kmalloc(sizeof(struct mmc_test_transfer_result), GFP_KERNEL);
509 tr->sectors = sectors;
513 list_add_tail(&tr->link, &test->gr->tr_lst);
517 * Print the transfer rate.
519 static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
520 struct timespec *ts1, struct timespec *ts2)
522 unsigned int rate, sectors = bytes >> 9;
525 ts = timespec_sub(*ts2, *ts1);
527 rate = mmc_test_rate(bytes, &ts);
529 printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
530 "seconds (%u kB/s, %u KiB/s)\n",
531 mmc_hostname(test->card->host), sectors, sectors >> 1,
532 (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
533 (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024);
535 mmc_test_save_transfer_result(test, 1, sectors, ts, rate);
539 * Print the average transfer rate.
541 static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
542 unsigned int count, struct timespec *ts1,
543 struct timespec *ts2)
545 unsigned int rate, sectors = bytes >> 9;
546 uint64_t tot = bytes * count;
549 ts = timespec_sub(*ts2, *ts1);
551 rate = mmc_test_rate(tot, &ts);
553 printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
554 "%lu.%09lu seconds (%u kB/s, %u KiB/s)\n",
555 mmc_hostname(test->card->host), count, sectors, count,
556 sectors >> 1, (sectors & 1 ? ".5" : ""),
557 (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
558 rate / 1000, rate / 1024);
560 mmc_test_save_transfer_result(test, count, sectors, ts, rate);
564 * Return the card size in sectors.
566 static unsigned int mmc_test_capacity(struct mmc_card *card)
568 if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
569 return card->ext_csd.sectors;
571 return card->csd.capacity << (card->csd.read_blkbits - 9);
574 /*******************************************************************/
575 /* Test preparation and cleanup */
576 /*******************************************************************/
579 * Fill the first couple of sectors of the card with known data
580 * so that bad reads/writes can be detected
582 static int __mmc_test_prepare(struct mmc_test_card *test, int write)
586 ret = mmc_test_set_blksize(test, 512);
591 memset(test->buffer, 0xDF, 512);
593 for (i = 0;i < 512;i++)
597 for (i = 0;i < BUFFER_SIZE / 512;i++) {
598 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
606 static int mmc_test_prepare_write(struct mmc_test_card *test)
608 return __mmc_test_prepare(test, 1);
611 static int mmc_test_prepare_read(struct mmc_test_card *test)
613 return __mmc_test_prepare(test, 0);
616 static int mmc_test_cleanup(struct mmc_test_card *test)
620 ret = mmc_test_set_blksize(test, 512);
624 memset(test->buffer, 0, 512);
626 for (i = 0;i < BUFFER_SIZE / 512;i++) {
627 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
635 /*******************************************************************/
636 /* Test execution helpers */
637 /*******************************************************************/
640 * Modifies the mmc_request to perform the "short transfer" tests
642 static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
643 struct mmc_request *mrq, int write)
645 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
647 if (mrq->data->blocks > 1) {
648 mrq->cmd->opcode = write ?
649 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
652 mrq->cmd->opcode = MMC_SEND_STATUS;
653 mrq->cmd->arg = test->card->rca << 16;
658 * Checks that a normal transfer didn't have any errors
660 static int mmc_test_check_result(struct mmc_test_card *test,
661 struct mmc_request *mrq)
665 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
669 if (!ret && mrq->cmd->error)
670 ret = mrq->cmd->error;
671 if (!ret && mrq->data->error)
672 ret = mrq->data->error;
673 if (!ret && mrq->stop && mrq->stop->error)
674 ret = mrq->stop->error;
675 if (!ret && mrq->data->bytes_xfered !=
676 mrq->data->blocks * mrq->data->blksz)
680 ret = RESULT_UNSUP_HOST;
686 * Checks that a "short transfer" behaved as expected
688 static int mmc_test_check_broken_result(struct mmc_test_card *test,
689 struct mmc_request *mrq)
693 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
697 if (!ret && mrq->cmd->error)
698 ret = mrq->cmd->error;
699 if (!ret && mrq->data->error == 0)
701 if (!ret && mrq->data->error != -ETIMEDOUT)
702 ret = mrq->data->error;
703 if (!ret && mrq->stop && mrq->stop->error)
704 ret = mrq->stop->error;
705 if (mrq->data->blocks > 1) {
706 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
709 if (!ret && mrq->data->bytes_xfered > 0)
714 ret = RESULT_UNSUP_HOST;
720 * Tests a basic transfer with certain parameters
722 static int mmc_test_simple_transfer(struct mmc_test_card *test,
723 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
724 unsigned blocks, unsigned blksz, int write)
726 struct mmc_request mrq;
727 struct mmc_command cmd;
728 struct mmc_command stop;
729 struct mmc_data data;
731 memset(&mrq, 0, sizeof(struct mmc_request));
732 memset(&cmd, 0, sizeof(struct mmc_command));
733 memset(&data, 0, sizeof(struct mmc_data));
734 memset(&stop, 0, sizeof(struct mmc_command));
740 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
741 blocks, blksz, write);
743 mmc_wait_for_req(test->card->host, &mrq);
745 mmc_test_wait_busy(test);
747 return mmc_test_check_result(test, &mrq);
751 * Tests a transfer where the card will fail completely or partly
753 static int mmc_test_broken_transfer(struct mmc_test_card *test,
754 unsigned blocks, unsigned blksz, int write)
756 struct mmc_request mrq;
757 struct mmc_command cmd;
758 struct mmc_command stop;
759 struct mmc_data data;
761 struct scatterlist sg;
763 memset(&mrq, 0, sizeof(struct mmc_request));
764 memset(&cmd, 0, sizeof(struct mmc_command));
765 memset(&data, 0, sizeof(struct mmc_data));
766 memset(&stop, 0, sizeof(struct mmc_command));
772 sg_init_one(&sg, test->buffer, blocks * blksz);
774 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
775 mmc_test_prepare_broken_mrq(test, &mrq, write);
777 mmc_wait_for_req(test->card->host, &mrq);
779 mmc_test_wait_busy(test);
781 return mmc_test_check_broken_result(test, &mrq);
785 * Does a complete transfer test where data is also validated
787 * Note: mmc_test_prepare() must have been done before this call
789 static int mmc_test_transfer(struct mmc_test_card *test,
790 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
791 unsigned blocks, unsigned blksz, int write)
797 for (i = 0;i < blocks * blksz;i++)
798 test->scratch[i] = i;
800 memset(test->scratch, 0, BUFFER_SIZE);
802 local_irq_save(flags);
803 sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
804 local_irq_restore(flags);
806 ret = mmc_test_set_blksize(test, blksz);
810 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
811 blocks, blksz, write);
818 ret = mmc_test_set_blksize(test, 512);
822 sectors = (blocks * blksz + 511) / 512;
823 if ((sectors * 512) == (blocks * blksz))
826 if ((sectors * 512) > BUFFER_SIZE)
829 memset(test->buffer, 0, sectors * 512);
831 for (i = 0;i < sectors;i++) {
832 ret = mmc_test_buffer_transfer(test,
833 test->buffer + i * 512,
834 dev_addr + i, 512, 0);
839 for (i = 0;i < blocks * blksz;i++) {
840 if (test->buffer[i] != (u8)i)
844 for (;i < sectors * 512;i++) {
845 if (test->buffer[i] != 0xDF)
849 local_irq_save(flags);
850 sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
851 local_irq_restore(flags);
852 for (i = 0;i < blocks * blksz;i++) {
853 if (test->scratch[i] != (u8)i)
861 /*******************************************************************/
863 /*******************************************************************/
865 struct mmc_test_case {
868 int (*prepare)(struct mmc_test_card *);
869 int (*run)(struct mmc_test_card *);
870 int (*cleanup)(struct mmc_test_card *);
873 static int mmc_test_basic_write(struct mmc_test_card *test)
876 struct scatterlist sg;
878 ret = mmc_test_set_blksize(test, 512);
882 sg_init_one(&sg, test->buffer, 512);
884 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
891 static int mmc_test_basic_read(struct mmc_test_card *test)
894 struct scatterlist sg;
896 ret = mmc_test_set_blksize(test, 512);
900 sg_init_one(&sg, test->buffer, 512);
902 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
909 static int mmc_test_verify_write(struct mmc_test_card *test)
912 struct scatterlist sg;
914 sg_init_one(&sg, test->buffer, 512);
916 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
923 static int mmc_test_verify_read(struct mmc_test_card *test)
926 struct scatterlist sg;
928 sg_init_one(&sg, test->buffer, 512);
930 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
937 static int mmc_test_multi_write(struct mmc_test_card *test)
941 struct scatterlist sg;
943 if (test->card->host->max_blk_count == 1)
944 return RESULT_UNSUP_HOST;
946 size = PAGE_SIZE * 2;
947 size = min(size, test->card->host->max_req_size);
948 size = min(size, test->card->host->max_seg_size);
949 size = min(size, test->card->host->max_blk_count * 512);
952 return RESULT_UNSUP_HOST;
954 sg_init_one(&sg, test->buffer, size);
956 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
963 static int mmc_test_multi_read(struct mmc_test_card *test)
967 struct scatterlist sg;
969 if (test->card->host->max_blk_count == 1)
970 return RESULT_UNSUP_HOST;
972 size = PAGE_SIZE * 2;
973 size = min(size, test->card->host->max_req_size);
974 size = min(size, test->card->host->max_seg_size);
975 size = min(size, test->card->host->max_blk_count * 512);
978 return RESULT_UNSUP_HOST;
980 sg_init_one(&sg, test->buffer, size);
982 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
989 static int mmc_test_pow2_write(struct mmc_test_card *test)
992 struct scatterlist sg;
994 if (!test->card->csd.write_partial)
995 return RESULT_UNSUP_CARD;
997 for (i = 1; i < 512;i <<= 1) {
998 sg_init_one(&sg, test->buffer, i);
999 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1007 static int mmc_test_pow2_read(struct mmc_test_card *test)
1010 struct scatterlist sg;
1012 if (!test->card->csd.read_partial)
1013 return RESULT_UNSUP_CARD;
1015 for (i = 1; i < 512;i <<= 1) {
1016 sg_init_one(&sg, test->buffer, i);
1017 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1025 static int mmc_test_weird_write(struct mmc_test_card *test)
1028 struct scatterlist sg;
1030 if (!test->card->csd.write_partial)
1031 return RESULT_UNSUP_CARD;
1033 for (i = 3; i < 512;i += 7) {
1034 sg_init_one(&sg, test->buffer, i);
1035 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1043 static int mmc_test_weird_read(struct mmc_test_card *test)
1046 struct scatterlist sg;
1048 if (!test->card->csd.read_partial)
1049 return RESULT_UNSUP_CARD;
1051 for (i = 3; i < 512;i += 7) {
1052 sg_init_one(&sg, test->buffer, i);
1053 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1061 static int mmc_test_align_write(struct mmc_test_card *test)
1064 struct scatterlist sg;
1066 for (i = 1;i < 4;i++) {
1067 sg_init_one(&sg, test->buffer + i, 512);
1068 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1076 static int mmc_test_align_read(struct mmc_test_card *test)
1079 struct scatterlist sg;
1081 for (i = 1;i < 4;i++) {
1082 sg_init_one(&sg, test->buffer + i, 512);
1083 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1091 static int mmc_test_align_multi_write(struct mmc_test_card *test)
1095 struct scatterlist sg;
1097 if (test->card->host->max_blk_count == 1)
1098 return RESULT_UNSUP_HOST;
1100 size = PAGE_SIZE * 2;
1101 size = min(size, test->card->host->max_req_size);
1102 size = min(size, test->card->host->max_seg_size);
1103 size = min(size, test->card->host->max_blk_count * 512);
1106 return RESULT_UNSUP_HOST;
1108 for (i = 1;i < 4;i++) {
1109 sg_init_one(&sg, test->buffer + i, size);
1110 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1118 static int mmc_test_align_multi_read(struct mmc_test_card *test)
1122 struct scatterlist sg;
1124 if (test->card->host->max_blk_count == 1)
1125 return RESULT_UNSUP_HOST;
1127 size = PAGE_SIZE * 2;
1128 size = min(size, test->card->host->max_req_size);
1129 size = min(size, test->card->host->max_seg_size);
1130 size = min(size, test->card->host->max_blk_count * 512);
1133 return RESULT_UNSUP_HOST;
1135 for (i = 1;i < 4;i++) {
1136 sg_init_one(&sg, test->buffer + i, size);
1137 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1145 static int mmc_test_xfersize_write(struct mmc_test_card *test)
1149 ret = mmc_test_set_blksize(test, 512);
1153 ret = mmc_test_broken_transfer(test, 1, 512, 1);
1160 static int mmc_test_xfersize_read(struct mmc_test_card *test)
1164 ret = mmc_test_set_blksize(test, 512);
1168 ret = mmc_test_broken_transfer(test, 1, 512, 0);
1175 static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
1179 if (test->card->host->max_blk_count == 1)
1180 return RESULT_UNSUP_HOST;
1182 ret = mmc_test_set_blksize(test, 512);
1186 ret = mmc_test_broken_transfer(test, 2, 512, 1);
1193 static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
1197 if (test->card->host->max_blk_count == 1)
1198 return RESULT_UNSUP_HOST;
1200 ret = mmc_test_set_blksize(test, 512);
1204 ret = mmc_test_broken_transfer(test, 2, 512, 0);
1211 #ifdef CONFIG_HIGHMEM
1213 static int mmc_test_write_high(struct mmc_test_card *test)
1216 struct scatterlist sg;
1218 sg_init_table(&sg, 1);
1219 sg_set_page(&sg, test->highmem, 512, 0);
1221 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1228 static int mmc_test_read_high(struct mmc_test_card *test)
1231 struct scatterlist sg;
1233 sg_init_table(&sg, 1);
1234 sg_set_page(&sg, test->highmem, 512, 0);
1236 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1243 static int mmc_test_multi_write_high(struct mmc_test_card *test)
1247 struct scatterlist sg;
1249 if (test->card->host->max_blk_count == 1)
1250 return RESULT_UNSUP_HOST;
1252 size = PAGE_SIZE * 2;
1253 size = min(size, test->card->host->max_req_size);
1254 size = min(size, test->card->host->max_seg_size);
1255 size = min(size, test->card->host->max_blk_count * 512);
1258 return RESULT_UNSUP_HOST;
1260 sg_init_table(&sg, 1);
1261 sg_set_page(&sg, test->highmem, size, 0);
1263 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1270 static int mmc_test_multi_read_high(struct mmc_test_card *test)
1274 struct scatterlist sg;
1276 if (test->card->host->max_blk_count == 1)
1277 return RESULT_UNSUP_HOST;
1279 size = PAGE_SIZE * 2;
1280 size = min(size, test->card->host->max_req_size);
1281 size = min(size, test->card->host->max_seg_size);
1282 size = min(size, test->card->host->max_blk_count * 512);
1285 return RESULT_UNSUP_HOST;
1287 sg_init_table(&sg, 1);
1288 sg_set_page(&sg, test->highmem, size, 0);
1290 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1299 static int mmc_test_no_highmem(struct mmc_test_card *test)
1301 printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
1302 mmc_hostname(test->card->host));
1306 #endif /* CONFIG_HIGHMEM */
1309 * Map sz bytes so that it can be transferred.
1311 static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
1314 struct mmc_test_area *t = &test->area;
1317 t->blocks = sz >> 9;
1320 err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
1321 t->max_segs, t->max_seg_sz,
1324 err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
1325 t->max_seg_sz, &t->sg_len);
1328 printk(KERN_INFO "%s: Failed to map sg list\n",
1329 mmc_hostname(test->card->host));
1334 * Transfer bytes mapped by mmc_test_area_map().
1336 static int mmc_test_area_transfer(struct mmc_test_card *test,
1337 unsigned int dev_addr, int write)
1339 struct mmc_test_area *t = &test->area;
1341 return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
1342 t->blocks, 512, write);
1346 * Map and transfer bytes.
1348 static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
1349 unsigned int dev_addr, int write, int max_scatter,
1352 struct timespec ts1, ts2;
1356 * In the case of a maximally scattered transfer, the maximum transfer
1357 * size is further limited by using PAGE_SIZE segments.
1360 struct mmc_test_area *t = &test->area;
1361 unsigned long max_tfr;
1363 if (t->max_seg_sz >= PAGE_SIZE)
1364 max_tfr = t->max_segs * PAGE_SIZE;
1366 max_tfr = t->max_segs * t->max_seg_sz;
1371 ret = mmc_test_area_map(test, sz, max_scatter);
1376 getnstimeofday(&ts1);
1378 ret = mmc_test_area_transfer(test, dev_addr, write);
1383 getnstimeofday(&ts2);
1386 mmc_test_print_rate(test, sz, &ts1, &ts2);
1392 * Write the test area entirely.
1394 static int mmc_test_area_fill(struct mmc_test_card *test)
1396 return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
1401 * Erase the test area entirely.
1403 static int mmc_test_area_erase(struct mmc_test_card *test)
1405 struct mmc_test_area *t = &test->area;
1407 if (!mmc_can_erase(test->card))
1410 return mmc_erase(test->card, t->dev_addr, test->area.max_sz >> 9,
1415 * Cleanup struct mmc_test_area.
1417 static int mmc_test_area_cleanup(struct mmc_test_card *test)
1419 struct mmc_test_area *t = &test->area;
1422 mmc_test_free_mem(t->mem);
1428 * Initialize an area for testing large transfers. The size of the area is the
1429 * preferred erase size which is a good size for optimal transfer speed. Note
1430 * that is typically 4MiB for modern cards. The test area is set to the middle
1431 * of the card because cards may have different charateristics at the front
1432 * (for FAT file system optimization). Optionally, the area is erased (if the
1433 * card supports it) which may improve write performance. Optionally, the area
1434 * 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;
1442 ret = mmc_test_set_blksize(test, 512);
1446 if (test->card->pref_erase > TEST_AREA_MAX_SIZE >> 9)
1447 t->max_sz = TEST_AREA_MAX_SIZE;
1449 t->max_sz = (unsigned long)test->card->pref_erase << 9;
1451 t->max_segs = test->card->host->max_segs;
1452 t->max_seg_sz = test->card->host->max_seg_size;
1454 t->max_tfr = t->max_sz;
1455 if (t->max_tfr >> 9 > test->card->host->max_blk_count)
1456 t->max_tfr = test->card->host->max_blk_count << 9;
1457 if (t->max_tfr > test->card->host->max_req_size)
1458 t->max_tfr = test->card->host->max_req_size;
1459 if (t->max_tfr / t->max_seg_sz > t->max_segs)
1460 t->max_tfr = t->max_segs * t->max_seg_sz;
1463 * Try to allocate enough memory for a max. sized transfer. Less is OK
1464 * because the same memory can be mapped into the scatterlist more than
1465 * once. Also, take into account the limits imposed on scatterlist
1466 * segments by the host driver.
1468 t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
1473 t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
1479 t->dev_addr = mmc_test_capacity(test->card) / 2;
1480 t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
1483 ret = mmc_test_area_erase(test);
1489 ret = mmc_test_area_fill(test);
1497 mmc_test_area_cleanup(test);
1502 * Prepare for large transfers. Do not erase the test area.
1504 static int mmc_test_area_prepare(struct mmc_test_card *test)
1506 return mmc_test_area_init(test, 0, 0);
1510 * Prepare for large transfers. Do erase the test area.
1512 static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
1514 return mmc_test_area_init(test, 1, 0);
1518 * Prepare for large transfers. Erase and fill the test area.
1520 static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
1522 return mmc_test_area_init(test, 1, 1);
1526 * Test best-case performance. Best-case performance is expected from
1527 * a single large transfer.
1529 * An additional option (max_scatter) allows the measurement of the same
1530 * transfer but with no contiguous pages in the scatter list. This tests
1531 * the efficiency of DMA to handle scattered pages.
1533 static int mmc_test_best_performance(struct mmc_test_card *test, int write,
1536 return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
1537 write, max_scatter, 1);
1541 * Best-case read performance.
1543 static int mmc_test_best_read_performance(struct mmc_test_card *test)
1545 return mmc_test_best_performance(test, 0, 0);
1549 * Best-case write performance.
1551 static int mmc_test_best_write_performance(struct mmc_test_card *test)
1553 return mmc_test_best_performance(test, 1, 0);
1557 * Best-case read performance into scattered pages.
1559 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
1561 return mmc_test_best_performance(test, 0, 1);
1565 * Best-case write performance from scattered pages.
1567 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
1569 return mmc_test_best_performance(test, 1, 1);
1573 * Single read performance by transfer size.
1575 static int mmc_test_profile_read_perf(struct mmc_test_card *test)
1578 unsigned int dev_addr;
1581 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1582 dev_addr = test->area.dev_addr + (sz >> 9);
1583 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1587 sz = test->area.max_tfr;
1588 dev_addr = test->area.dev_addr;
1589 return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1593 * Single write performance by transfer size.
1595 static int mmc_test_profile_write_perf(struct mmc_test_card *test)
1598 unsigned int dev_addr;
1601 ret = mmc_test_area_erase(test);
1604 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1605 dev_addr = test->area.dev_addr + (sz >> 9);
1606 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1610 ret = mmc_test_area_erase(test);
1613 sz = test->area.max_tfr;
1614 dev_addr = test->area.dev_addr;
1615 return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1619 * Single trim performance by transfer size.
1621 static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
1624 unsigned int dev_addr;
1625 struct timespec ts1, ts2;
1628 if (!mmc_can_trim(test->card))
1629 return RESULT_UNSUP_CARD;
1631 if (!mmc_can_erase(test->card))
1632 return RESULT_UNSUP_HOST;
1634 for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
1635 dev_addr = test->area.dev_addr + (sz >> 9);
1636 getnstimeofday(&ts1);
1637 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1640 getnstimeofday(&ts2);
1641 mmc_test_print_rate(test, sz, &ts1, &ts2);
1643 dev_addr = test->area.dev_addr;
1644 getnstimeofday(&ts1);
1645 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1648 getnstimeofday(&ts2);
1649 mmc_test_print_rate(test, sz, &ts1, &ts2);
1653 static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
1655 unsigned int dev_addr, i, cnt;
1656 struct timespec ts1, ts2;
1659 cnt = test->area.max_sz / sz;
1660 dev_addr = test->area.dev_addr;
1661 getnstimeofday(&ts1);
1662 for (i = 0; i < cnt; i++) {
1663 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
1666 dev_addr += (sz >> 9);
1668 getnstimeofday(&ts2);
1669 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1674 * Consecutive read performance by transfer size.
1676 static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
1681 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1682 ret = mmc_test_seq_read_perf(test, sz);
1686 sz = test->area.max_tfr;
1687 return mmc_test_seq_read_perf(test, sz);
1690 static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
1692 unsigned int dev_addr, i, cnt;
1693 struct timespec ts1, ts2;
1696 ret = mmc_test_area_erase(test);
1699 cnt = test->area.max_sz / sz;
1700 dev_addr = test->area.dev_addr;
1701 getnstimeofday(&ts1);
1702 for (i = 0; i < cnt; i++) {
1703 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
1706 dev_addr += (sz >> 9);
1708 getnstimeofday(&ts2);
1709 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1714 * Consecutive write performance by transfer size.
1716 static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
1721 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1722 ret = mmc_test_seq_write_perf(test, sz);
1726 sz = test->area.max_tfr;
1727 return mmc_test_seq_write_perf(test, sz);
1731 * Consecutive trim performance by transfer size.
1733 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
1736 unsigned int dev_addr, i, cnt;
1737 struct timespec ts1, ts2;
1740 if (!mmc_can_trim(test->card))
1741 return RESULT_UNSUP_CARD;
1743 if (!mmc_can_erase(test->card))
1744 return RESULT_UNSUP_HOST;
1746 for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
1747 ret = mmc_test_area_erase(test);
1750 ret = mmc_test_area_fill(test);
1753 cnt = test->area.max_sz / sz;
1754 dev_addr = test->area.dev_addr;
1755 getnstimeofday(&ts1);
1756 for (i = 0; i < cnt; i++) {
1757 ret = mmc_erase(test->card, dev_addr, sz >> 9,
1761 dev_addr += (sz >> 9);
1763 getnstimeofday(&ts2);
1764 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1769 static const struct mmc_test_case mmc_test_cases[] = {
1771 .name = "Basic write (no data verification)",
1772 .run = mmc_test_basic_write,
1776 .name = "Basic read (no data verification)",
1777 .run = mmc_test_basic_read,
1781 .name = "Basic write (with data verification)",
1782 .prepare = mmc_test_prepare_write,
1783 .run = mmc_test_verify_write,
1784 .cleanup = mmc_test_cleanup,
1788 .name = "Basic read (with data verification)",
1789 .prepare = mmc_test_prepare_read,
1790 .run = mmc_test_verify_read,
1791 .cleanup = mmc_test_cleanup,
1795 .name = "Multi-block write",
1796 .prepare = mmc_test_prepare_write,
1797 .run = mmc_test_multi_write,
1798 .cleanup = mmc_test_cleanup,
1802 .name = "Multi-block read",
1803 .prepare = mmc_test_prepare_read,
1804 .run = mmc_test_multi_read,
1805 .cleanup = mmc_test_cleanup,
1809 .name = "Power of two block writes",
1810 .prepare = mmc_test_prepare_write,
1811 .run = mmc_test_pow2_write,
1812 .cleanup = mmc_test_cleanup,
1816 .name = "Power of two block reads",
1817 .prepare = mmc_test_prepare_read,
1818 .run = mmc_test_pow2_read,
1819 .cleanup = mmc_test_cleanup,
1823 .name = "Weird sized block writes",
1824 .prepare = mmc_test_prepare_write,
1825 .run = mmc_test_weird_write,
1826 .cleanup = mmc_test_cleanup,
1830 .name = "Weird sized block reads",
1831 .prepare = mmc_test_prepare_read,
1832 .run = mmc_test_weird_read,
1833 .cleanup = mmc_test_cleanup,
1837 .name = "Badly aligned write",
1838 .prepare = mmc_test_prepare_write,
1839 .run = mmc_test_align_write,
1840 .cleanup = mmc_test_cleanup,
1844 .name = "Badly aligned read",
1845 .prepare = mmc_test_prepare_read,
1846 .run = mmc_test_align_read,
1847 .cleanup = mmc_test_cleanup,
1851 .name = "Badly aligned multi-block write",
1852 .prepare = mmc_test_prepare_write,
1853 .run = mmc_test_align_multi_write,
1854 .cleanup = mmc_test_cleanup,
1858 .name = "Badly aligned multi-block read",
1859 .prepare = mmc_test_prepare_read,
1860 .run = mmc_test_align_multi_read,
1861 .cleanup = mmc_test_cleanup,
1865 .name = "Correct xfer_size at write (start failure)",
1866 .run = mmc_test_xfersize_write,
1870 .name = "Correct xfer_size at read (start failure)",
1871 .run = mmc_test_xfersize_read,
1875 .name = "Correct xfer_size at write (midway failure)",
1876 .run = mmc_test_multi_xfersize_write,
1880 .name = "Correct xfer_size at read (midway failure)",
1881 .run = mmc_test_multi_xfersize_read,
1884 #ifdef CONFIG_HIGHMEM
1887 .name = "Highmem write",
1888 .prepare = mmc_test_prepare_write,
1889 .run = mmc_test_write_high,
1890 .cleanup = mmc_test_cleanup,
1894 .name = "Highmem read",
1895 .prepare = mmc_test_prepare_read,
1896 .run = mmc_test_read_high,
1897 .cleanup = mmc_test_cleanup,
1901 .name = "Multi-block highmem write",
1902 .prepare = mmc_test_prepare_write,
1903 .run = mmc_test_multi_write_high,
1904 .cleanup = mmc_test_cleanup,
1908 .name = "Multi-block highmem read",
1909 .prepare = mmc_test_prepare_read,
1910 .run = mmc_test_multi_read_high,
1911 .cleanup = mmc_test_cleanup,
1917 .name = "Highmem write",
1918 .run = mmc_test_no_highmem,
1922 .name = "Highmem read",
1923 .run = mmc_test_no_highmem,
1927 .name = "Multi-block highmem write",
1928 .run = mmc_test_no_highmem,
1932 .name = "Multi-block highmem read",
1933 .run = mmc_test_no_highmem,
1936 #endif /* CONFIG_HIGHMEM */
1939 .name = "Best-case read performance",
1940 .prepare = mmc_test_area_prepare_fill,
1941 .run = mmc_test_best_read_performance,
1942 .cleanup = mmc_test_area_cleanup,
1946 .name = "Best-case write performance",
1947 .prepare = mmc_test_area_prepare_erase,
1948 .run = mmc_test_best_write_performance,
1949 .cleanup = mmc_test_area_cleanup,
1953 .name = "Best-case read performance into scattered pages",
1954 .prepare = mmc_test_area_prepare_fill,
1955 .run = mmc_test_best_read_perf_max_scatter,
1956 .cleanup = mmc_test_area_cleanup,
1960 .name = "Best-case write performance from scattered pages",
1961 .prepare = mmc_test_area_prepare_erase,
1962 .run = mmc_test_best_write_perf_max_scatter,
1963 .cleanup = mmc_test_area_cleanup,
1967 .name = "Single read performance by transfer size",
1968 .prepare = mmc_test_area_prepare_fill,
1969 .run = mmc_test_profile_read_perf,
1970 .cleanup = mmc_test_area_cleanup,
1974 .name = "Single write performance by transfer size",
1975 .prepare = mmc_test_area_prepare,
1976 .run = mmc_test_profile_write_perf,
1977 .cleanup = mmc_test_area_cleanup,
1981 .name = "Single trim performance by transfer size",
1982 .prepare = mmc_test_area_prepare_fill,
1983 .run = mmc_test_profile_trim_perf,
1984 .cleanup = mmc_test_area_cleanup,
1988 .name = "Consecutive read performance by transfer size",
1989 .prepare = mmc_test_area_prepare_fill,
1990 .run = mmc_test_profile_seq_read_perf,
1991 .cleanup = mmc_test_area_cleanup,
1995 .name = "Consecutive write performance by transfer size",
1996 .prepare = mmc_test_area_prepare,
1997 .run = mmc_test_profile_seq_write_perf,
1998 .cleanup = mmc_test_area_cleanup,
2002 .name = "Consecutive trim performance by transfer size",
2003 .prepare = mmc_test_area_prepare,
2004 .run = mmc_test_profile_seq_trim_perf,
2005 .cleanup = mmc_test_area_cleanup,
2010 static DEFINE_MUTEX(mmc_test_lock);
2012 static LIST_HEAD(mmc_test_result);
2014 static void mmc_test_run(struct mmc_test_card *test, int testcase)
2018 printk(KERN_INFO "%s: Starting tests of card %s...\n",
2019 mmc_hostname(test->card->host), mmc_card_id(test->card));
2021 mmc_claim_host(test->card->host);
2023 for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
2024 struct mmc_test_general_result *gr;
2026 if (testcase && ((i + 1) != testcase))
2029 printk(KERN_INFO "%s: Test case %d. %s...\n",
2030 mmc_hostname(test->card->host), i + 1,
2031 mmc_test_cases[i].name);
2033 if (mmc_test_cases[i].prepare) {
2034 ret = mmc_test_cases[i].prepare(test);
2036 printk(KERN_INFO "%s: Result: Prepare "
2037 "stage failed! (%d)\n",
2038 mmc_hostname(test->card->host),
2044 gr = kzalloc(sizeof(struct mmc_test_general_result),
2047 INIT_LIST_HEAD(&gr->tr_lst);
2049 /* Assign data what we know already */
2050 gr->card = test->card;
2053 /* Append container to global one */
2054 list_add_tail(&gr->link, &mmc_test_result);
2057 * Save the pointer to created container in our private
2063 ret = mmc_test_cases[i].run(test);
2066 printk(KERN_INFO "%s: Result: OK\n",
2067 mmc_hostname(test->card->host));
2070 printk(KERN_INFO "%s: Result: FAILED\n",
2071 mmc_hostname(test->card->host));
2073 case RESULT_UNSUP_HOST:
2074 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2076 mmc_hostname(test->card->host));
2078 case RESULT_UNSUP_CARD:
2079 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2081 mmc_hostname(test->card->host));
2084 printk(KERN_INFO "%s: Result: ERROR (%d)\n",
2085 mmc_hostname(test->card->host), ret);
2088 /* Save the result */
2092 if (mmc_test_cases[i].cleanup) {
2093 ret = mmc_test_cases[i].cleanup(test);
2095 printk(KERN_INFO "%s: Warning: Cleanup "
2096 "stage failed! (%d)\n",
2097 mmc_hostname(test->card->host),
2103 mmc_release_host(test->card->host);
2105 printk(KERN_INFO "%s: Tests completed.\n",
2106 mmc_hostname(test->card->host));
2109 static void mmc_test_free_result(struct mmc_card *card)
2111 struct mmc_test_general_result *gr, *grs;
2113 mutex_lock(&mmc_test_lock);
2115 list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
2116 struct mmc_test_transfer_result *tr, *trs;
2118 if (card && gr->card != card)
2121 list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
2122 list_del(&tr->link);
2126 list_del(&gr->link);
2130 mutex_unlock(&mmc_test_lock);
2133 static LIST_HEAD(mmc_test_file_test);
2135 static int mtf_test_show(struct seq_file *sf, void *data)
2137 struct mmc_card *card = (struct mmc_card *)sf->private;
2138 struct mmc_test_general_result *gr;
2140 mutex_lock(&mmc_test_lock);
2142 list_for_each_entry(gr, &mmc_test_result, link) {
2143 struct mmc_test_transfer_result *tr;
2145 if (gr->card != card)
2148 seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
2150 list_for_each_entry(tr, &gr->tr_lst, link) {
2151 seq_printf(sf, "%u %d %lu.%09lu %u\n",
2152 tr->count, tr->sectors,
2153 (unsigned long)tr->ts.tv_sec,
2154 (unsigned long)tr->ts.tv_nsec,
2159 mutex_unlock(&mmc_test_lock);
2164 static int mtf_test_open(struct inode *inode, struct file *file)
2166 return single_open(file, mtf_test_show, inode->i_private);
2169 static ssize_t mtf_test_write(struct file *file, const char __user *buf,
2170 size_t count, loff_t *pos)
2172 struct seq_file *sf = (struct seq_file *)file->private_data;
2173 struct mmc_card *card = (struct mmc_card *)sf->private;
2174 struct mmc_test_card *test;
2178 if (count >= sizeof(lbuf))
2181 if (copy_from_user(lbuf, buf, count))
2185 if (strict_strtol(lbuf, 10, &testcase))
2188 test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
2193 * Remove all test cases associated with given card. Thus we have only
2194 * actual data of the last run.
2196 mmc_test_free_result(card);
2200 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
2201 #ifdef CONFIG_HIGHMEM
2202 test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
2205 #ifdef CONFIG_HIGHMEM
2206 if (test->buffer && test->highmem) {
2210 mutex_lock(&mmc_test_lock);
2211 mmc_test_run(test, testcase);
2212 mutex_unlock(&mmc_test_lock);
2215 #ifdef CONFIG_HIGHMEM
2216 __free_pages(test->highmem, BUFFER_ORDER);
2218 kfree(test->buffer);
2224 static const struct file_operations mmc_test_fops_test = {
2225 .open = mtf_test_open,
2227 .write = mtf_test_write,
2228 .llseek = seq_lseek,
2229 .release = single_release,
2232 static void mmc_test_free_file_test(struct mmc_card *card)
2234 struct mmc_test_dbgfs_file *df, *dfs;
2236 mutex_lock(&mmc_test_lock);
2238 list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
2239 if (card && df->card != card)
2241 debugfs_remove(df->file);
2242 list_del(&df->link);
2246 mutex_unlock(&mmc_test_lock);
2249 static int mmc_test_register_file_test(struct mmc_card *card)
2251 struct dentry *file = NULL;
2252 struct mmc_test_dbgfs_file *df;
2255 mutex_lock(&mmc_test_lock);
2257 if (card->debugfs_root)
2258 file = debugfs_create_file("test", S_IWUSR | S_IRUGO,
2259 card->debugfs_root, card, &mmc_test_fops_test);
2261 if (IS_ERR_OR_NULL(file)) {
2263 "Can't create file. Perhaps debugfs is disabled.\n");
2268 df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
2270 debugfs_remove(file);
2272 "Can't allocate memory for internal usage.\n");
2280 list_add(&df->link, &mmc_test_file_test);
2283 mutex_unlock(&mmc_test_lock);
2288 static int mmc_test_probe(struct mmc_card *card)
2292 if (!mmc_card_mmc(card) && !mmc_card_sd(card))
2295 ret = mmc_test_register_file_test(card);
2299 dev_info(&card->dev, "Card claimed for testing.\n");
2304 static void mmc_test_remove(struct mmc_card *card)
2306 mmc_test_free_result(card);
2307 mmc_test_free_file_test(card);
2310 static struct mmc_driver mmc_driver = {
2314 .probe = mmc_test_probe,
2315 .remove = mmc_test_remove,
2318 static int __init mmc_test_init(void)
2320 return mmc_register_driver(&mmc_driver);
2323 static void __exit mmc_test_exit(void)
2325 /* Clear stalled data if card is still plugged */
2326 mmc_test_free_result(NULL);
2327 mmc_test_free_file_test(NULL);
2329 mmc_unregister_driver(&mmc_driver);
2332 module_init(mmc_test_init);
2333 module_exit(mmc_test_exit);
2335 MODULE_LICENSE("GPL");
2336 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
2337 MODULE_AUTHOR("Pierre Ossman");