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 struct mmc_command cmd;
161 cmd.opcode = MMC_SET_BLOCKLEN;
163 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
164 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
172 * Fill in the mmc_request structure given a set of transfer parameters.
174 static void mmc_test_prepare_mrq(struct mmc_test_card *test,
175 struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
176 unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
178 BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
181 mrq->cmd->opcode = write ?
182 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
184 mrq->cmd->opcode = write ?
185 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
188 mrq->cmd->arg = dev_addr;
189 if (!mmc_card_blockaddr(test->card))
192 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
197 mrq->stop->opcode = MMC_STOP_TRANSMISSION;
199 mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
202 mrq->data->blksz = blksz;
203 mrq->data->blocks = blocks;
204 mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
206 mrq->data->sg_len = sg_len;
208 mmc_set_data_timeout(mrq->data, test->card);
211 static int mmc_test_busy(struct mmc_command *cmd)
213 return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
214 (R1_CURRENT_STATE(cmd->resp[0]) == 7);
218 * Wait for the card to finish the busy state
220 static int mmc_test_wait_busy(struct mmc_test_card *test)
223 struct mmc_command cmd;
227 memset(&cmd, 0, sizeof(struct mmc_command));
229 cmd.opcode = MMC_SEND_STATUS;
230 cmd.arg = test->card->rca << 16;
231 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
233 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
237 if (!busy && mmc_test_busy(&cmd)) {
239 printk(KERN_INFO "%s: Warning: Host did not "
240 "wait for busy state to end.\n",
241 mmc_hostname(test->card->host));
243 } while (mmc_test_busy(&cmd));
249 * Transfer a single sector of kernel addressable data
251 static int mmc_test_buffer_transfer(struct mmc_test_card *test,
252 u8 *buffer, unsigned addr, unsigned blksz, int write)
256 struct mmc_request mrq;
257 struct mmc_command cmd;
258 struct mmc_command stop;
259 struct mmc_data data;
261 struct scatterlist sg;
263 memset(&mrq, 0, sizeof(struct mmc_request));
264 memset(&cmd, 0, sizeof(struct mmc_command));
265 memset(&data, 0, sizeof(struct mmc_data));
266 memset(&stop, 0, sizeof(struct mmc_command));
272 sg_init_one(&sg, buffer, blksz);
274 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
276 mmc_wait_for_req(test->card->host, &mrq);
283 ret = mmc_test_wait_busy(test);
290 static void mmc_test_free_mem(struct mmc_test_mem *mem)
295 __free_pages(mem->arr[mem->cnt].page,
296 mem->arr[mem->cnt].order);
302 * Allocate a lot of memory, preferrably max_sz but at least min_sz. In case
303 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
304 * not exceed a maximum number of segments and try not to make segments much
305 * bigger than maximum segment size.
307 static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
308 unsigned long max_sz,
309 unsigned int max_segs,
310 unsigned int max_seg_sz)
312 unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
313 unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
314 unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
315 unsigned long page_cnt = 0;
316 unsigned long limit = nr_free_buffer_pages() >> 4;
317 struct mmc_test_mem *mem;
319 if (max_page_cnt > limit)
320 max_page_cnt = limit;
321 if (min_page_cnt > max_page_cnt)
322 min_page_cnt = max_page_cnt;
324 if (max_seg_page_cnt > max_page_cnt)
325 max_seg_page_cnt = max_page_cnt;
327 if (max_segs > max_page_cnt)
328 max_segs = max_page_cnt;
330 mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
334 mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_segs,
339 while (max_page_cnt) {
342 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
345 order = get_order(max_seg_page_cnt << PAGE_SHIFT);
347 page = alloc_pages(flags, order);
353 if (page_cnt < min_page_cnt)
357 mem->arr[mem->cnt].page = page;
358 mem->arr[mem->cnt].order = order;
360 if (max_page_cnt <= (1UL << order))
362 max_page_cnt -= 1UL << order;
363 page_cnt += 1UL << order;
364 if (mem->cnt >= max_segs) {
365 if (page_cnt < min_page_cnt)
374 mmc_test_free_mem(mem);
379 * Map memory into a scatterlist. Optionally allow the same memory to be
380 * mapped more than once.
382 static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
383 struct scatterlist *sglist, int repeat,
384 unsigned int max_segs, unsigned int max_seg_sz,
385 unsigned int *sg_len)
387 struct scatterlist *sg = NULL;
390 sg_init_table(sglist, max_segs);
394 for (i = 0; i < mem->cnt; i++) {
395 unsigned long len = PAGE_SIZE << mem->arr[i].order;
399 if (len > max_seg_sz)
407 sg_set_page(sg, mem->arr[i].page, len, 0);
413 } while (sz && repeat);
425 * Map memory into a scatterlist so that no pages are contiguous. Allow the
426 * same memory to be mapped more than once.
428 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
430 struct scatterlist *sglist,
431 unsigned int max_segs,
432 unsigned int max_seg_sz,
433 unsigned int *sg_len)
435 struct scatterlist *sg = NULL;
436 unsigned int i = mem->cnt, cnt;
438 void *base, *addr, *last_addr = NULL;
440 sg_init_table(sglist, max_segs);
444 base = page_address(mem->arr[--i].page);
445 cnt = 1 << mem->arr[i].order;
447 addr = base + PAGE_SIZE * --cnt;
448 if (last_addr && last_addr + PAGE_SIZE == addr)
452 if (len > max_seg_sz)
462 sg_set_page(sg, virt_to_page(addr), len, 0);
477 * Calculate transfer rate in bytes per second.
479 static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
489 while (ns > UINT_MAX) {
497 do_div(bytes, (uint32_t)ns);
503 * Save transfer results for future usage
505 static void mmc_test_save_transfer_result(struct mmc_test_card *test,
506 unsigned int count, unsigned int sectors, struct timespec ts,
509 struct mmc_test_transfer_result *tr;
514 tr = kmalloc(sizeof(struct mmc_test_transfer_result), GFP_KERNEL);
519 tr->sectors = sectors;
523 list_add_tail(&tr->link, &test->gr->tr_lst);
527 * Print the transfer rate.
529 static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
530 struct timespec *ts1, struct timespec *ts2)
532 unsigned int rate, sectors = bytes >> 9;
535 ts = timespec_sub(*ts2, *ts1);
537 rate = mmc_test_rate(bytes, &ts);
539 printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
540 "seconds (%u kB/s, %u KiB/s)\n",
541 mmc_hostname(test->card->host), sectors, sectors >> 1,
542 (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
543 (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024);
545 mmc_test_save_transfer_result(test, 1, sectors, ts, rate);
549 * Print the average transfer rate.
551 static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
552 unsigned int count, struct timespec *ts1,
553 struct timespec *ts2)
555 unsigned int rate, sectors = bytes >> 9;
556 uint64_t tot = bytes * count;
559 ts = timespec_sub(*ts2, *ts1);
561 rate = mmc_test_rate(tot, &ts);
563 printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
564 "%lu.%09lu seconds (%u kB/s, %u KiB/s)\n",
565 mmc_hostname(test->card->host), count, sectors, count,
566 sectors >> 1, (sectors & 1 ? ".5" : ""),
567 (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
568 rate / 1000, rate / 1024);
570 mmc_test_save_transfer_result(test, count, sectors, ts, rate);
574 * Return the card size in sectors.
576 static unsigned int mmc_test_capacity(struct mmc_card *card)
578 if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
579 return card->ext_csd.sectors;
581 return card->csd.capacity << (card->csd.read_blkbits - 9);
584 /*******************************************************************/
585 /* Test preparation and cleanup */
586 /*******************************************************************/
589 * Fill the first couple of sectors of the card with known data
590 * so that bad reads/writes can be detected
592 static int __mmc_test_prepare(struct mmc_test_card *test, int write)
596 ret = mmc_test_set_blksize(test, 512);
601 memset(test->buffer, 0xDF, 512);
603 for (i = 0;i < 512;i++)
607 for (i = 0;i < BUFFER_SIZE / 512;i++) {
608 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
616 static int mmc_test_prepare_write(struct mmc_test_card *test)
618 return __mmc_test_prepare(test, 1);
621 static int mmc_test_prepare_read(struct mmc_test_card *test)
623 return __mmc_test_prepare(test, 0);
626 static int mmc_test_cleanup(struct mmc_test_card *test)
630 ret = mmc_test_set_blksize(test, 512);
634 memset(test->buffer, 0, 512);
636 for (i = 0;i < BUFFER_SIZE / 512;i++) {
637 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
645 /*******************************************************************/
646 /* Test execution helpers */
647 /*******************************************************************/
650 * Modifies the mmc_request to perform the "short transfer" tests
652 static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
653 struct mmc_request *mrq, int write)
655 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
657 if (mrq->data->blocks > 1) {
658 mrq->cmd->opcode = write ?
659 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
662 mrq->cmd->opcode = MMC_SEND_STATUS;
663 mrq->cmd->arg = test->card->rca << 16;
668 * Checks that a normal transfer didn't have any errors
670 static int mmc_test_check_result(struct mmc_test_card *test,
671 struct mmc_request *mrq)
675 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
679 if (!ret && mrq->cmd->error)
680 ret = mrq->cmd->error;
681 if (!ret && mrq->data->error)
682 ret = mrq->data->error;
683 if (!ret && mrq->stop && mrq->stop->error)
684 ret = mrq->stop->error;
685 if (!ret && mrq->data->bytes_xfered !=
686 mrq->data->blocks * mrq->data->blksz)
690 ret = RESULT_UNSUP_HOST;
696 * Checks that a "short transfer" behaved as expected
698 static int mmc_test_check_broken_result(struct mmc_test_card *test,
699 struct mmc_request *mrq)
703 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
707 if (!ret && mrq->cmd->error)
708 ret = mrq->cmd->error;
709 if (!ret && mrq->data->error == 0)
711 if (!ret && mrq->data->error != -ETIMEDOUT)
712 ret = mrq->data->error;
713 if (!ret && mrq->stop && mrq->stop->error)
714 ret = mrq->stop->error;
715 if (mrq->data->blocks > 1) {
716 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
719 if (!ret && mrq->data->bytes_xfered > 0)
724 ret = RESULT_UNSUP_HOST;
730 * Tests a basic transfer with certain parameters
732 static int mmc_test_simple_transfer(struct mmc_test_card *test,
733 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
734 unsigned blocks, unsigned blksz, int write)
736 struct mmc_request mrq;
737 struct mmc_command cmd;
738 struct mmc_command stop;
739 struct mmc_data data;
741 memset(&mrq, 0, sizeof(struct mmc_request));
742 memset(&cmd, 0, sizeof(struct mmc_command));
743 memset(&data, 0, sizeof(struct mmc_data));
744 memset(&stop, 0, sizeof(struct mmc_command));
750 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
751 blocks, blksz, write);
753 mmc_wait_for_req(test->card->host, &mrq);
755 mmc_test_wait_busy(test);
757 return mmc_test_check_result(test, &mrq);
761 * Tests a transfer where the card will fail completely or partly
763 static int mmc_test_broken_transfer(struct mmc_test_card *test,
764 unsigned blocks, unsigned blksz, int write)
766 struct mmc_request mrq;
767 struct mmc_command cmd;
768 struct mmc_command stop;
769 struct mmc_data data;
771 struct scatterlist sg;
773 memset(&mrq, 0, sizeof(struct mmc_request));
774 memset(&cmd, 0, sizeof(struct mmc_command));
775 memset(&data, 0, sizeof(struct mmc_data));
776 memset(&stop, 0, sizeof(struct mmc_command));
782 sg_init_one(&sg, test->buffer, blocks * blksz);
784 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
785 mmc_test_prepare_broken_mrq(test, &mrq, write);
787 mmc_wait_for_req(test->card->host, &mrq);
789 mmc_test_wait_busy(test);
791 return mmc_test_check_broken_result(test, &mrq);
795 * Does a complete transfer test where data is also validated
797 * Note: mmc_test_prepare() must have been done before this call
799 static int mmc_test_transfer(struct mmc_test_card *test,
800 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
801 unsigned blocks, unsigned blksz, int write)
807 for (i = 0;i < blocks * blksz;i++)
808 test->scratch[i] = i;
810 memset(test->scratch, 0, BUFFER_SIZE);
812 local_irq_save(flags);
813 sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
814 local_irq_restore(flags);
816 ret = mmc_test_set_blksize(test, blksz);
820 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
821 blocks, blksz, write);
828 ret = mmc_test_set_blksize(test, 512);
832 sectors = (blocks * blksz + 511) / 512;
833 if ((sectors * 512) == (blocks * blksz))
836 if ((sectors * 512) > BUFFER_SIZE)
839 memset(test->buffer, 0, sectors * 512);
841 for (i = 0;i < sectors;i++) {
842 ret = mmc_test_buffer_transfer(test,
843 test->buffer + i * 512,
844 dev_addr + i, 512, 0);
849 for (i = 0;i < blocks * blksz;i++) {
850 if (test->buffer[i] != (u8)i)
854 for (;i < sectors * 512;i++) {
855 if (test->buffer[i] != 0xDF)
859 local_irq_save(flags);
860 sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
861 local_irq_restore(flags);
862 for (i = 0;i < blocks * blksz;i++) {
863 if (test->scratch[i] != (u8)i)
871 /*******************************************************************/
873 /*******************************************************************/
875 struct mmc_test_case {
878 int (*prepare)(struct mmc_test_card *);
879 int (*run)(struct mmc_test_card *);
880 int (*cleanup)(struct mmc_test_card *);
883 static int mmc_test_basic_write(struct mmc_test_card *test)
886 struct scatterlist sg;
888 ret = mmc_test_set_blksize(test, 512);
892 sg_init_one(&sg, test->buffer, 512);
894 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
901 static int mmc_test_basic_read(struct mmc_test_card *test)
904 struct scatterlist sg;
906 ret = mmc_test_set_blksize(test, 512);
910 sg_init_one(&sg, test->buffer, 512);
912 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
919 static int mmc_test_verify_write(struct mmc_test_card *test)
922 struct scatterlist sg;
924 sg_init_one(&sg, test->buffer, 512);
926 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
933 static int mmc_test_verify_read(struct mmc_test_card *test)
936 struct scatterlist sg;
938 sg_init_one(&sg, test->buffer, 512);
940 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
947 static int mmc_test_multi_write(struct mmc_test_card *test)
951 struct scatterlist sg;
953 if (test->card->host->max_blk_count == 1)
954 return RESULT_UNSUP_HOST;
956 size = PAGE_SIZE * 2;
957 size = min(size, test->card->host->max_req_size);
958 size = min(size, test->card->host->max_seg_size);
959 size = min(size, test->card->host->max_blk_count * 512);
962 return RESULT_UNSUP_HOST;
964 sg_init_one(&sg, test->buffer, size);
966 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
973 static int mmc_test_multi_read(struct mmc_test_card *test)
977 struct scatterlist sg;
979 if (test->card->host->max_blk_count == 1)
980 return RESULT_UNSUP_HOST;
982 size = PAGE_SIZE * 2;
983 size = min(size, test->card->host->max_req_size);
984 size = min(size, test->card->host->max_seg_size);
985 size = min(size, test->card->host->max_blk_count * 512);
988 return RESULT_UNSUP_HOST;
990 sg_init_one(&sg, test->buffer, size);
992 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
999 static int mmc_test_pow2_write(struct mmc_test_card *test)
1002 struct scatterlist sg;
1004 if (!test->card->csd.write_partial)
1005 return RESULT_UNSUP_CARD;
1007 for (i = 1; i < 512;i <<= 1) {
1008 sg_init_one(&sg, test->buffer, i);
1009 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1017 static int mmc_test_pow2_read(struct mmc_test_card *test)
1020 struct scatterlist sg;
1022 if (!test->card->csd.read_partial)
1023 return RESULT_UNSUP_CARD;
1025 for (i = 1; i < 512;i <<= 1) {
1026 sg_init_one(&sg, test->buffer, i);
1027 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1035 static int mmc_test_weird_write(struct mmc_test_card *test)
1038 struct scatterlist sg;
1040 if (!test->card->csd.write_partial)
1041 return RESULT_UNSUP_CARD;
1043 for (i = 3; i < 512;i += 7) {
1044 sg_init_one(&sg, test->buffer, i);
1045 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1053 static int mmc_test_weird_read(struct mmc_test_card *test)
1056 struct scatterlist sg;
1058 if (!test->card->csd.read_partial)
1059 return RESULT_UNSUP_CARD;
1061 for (i = 3; i < 512;i += 7) {
1062 sg_init_one(&sg, test->buffer, i);
1063 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1071 static int mmc_test_align_write(struct mmc_test_card *test)
1074 struct scatterlist sg;
1076 for (i = 1;i < 4;i++) {
1077 sg_init_one(&sg, test->buffer + i, 512);
1078 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1086 static int mmc_test_align_read(struct mmc_test_card *test)
1089 struct scatterlist sg;
1091 for (i = 1;i < 4;i++) {
1092 sg_init_one(&sg, test->buffer + i, 512);
1093 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1101 static int mmc_test_align_multi_write(struct mmc_test_card *test)
1105 struct scatterlist sg;
1107 if (test->card->host->max_blk_count == 1)
1108 return RESULT_UNSUP_HOST;
1110 size = PAGE_SIZE * 2;
1111 size = min(size, test->card->host->max_req_size);
1112 size = min(size, test->card->host->max_seg_size);
1113 size = min(size, test->card->host->max_blk_count * 512);
1116 return RESULT_UNSUP_HOST;
1118 for (i = 1;i < 4;i++) {
1119 sg_init_one(&sg, test->buffer + i, size);
1120 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1128 static int mmc_test_align_multi_read(struct mmc_test_card *test)
1132 struct scatterlist sg;
1134 if (test->card->host->max_blk_count == 1)
1135 return RESULT_UNSUP_HOST;
1137 size = PAGE_SIZE * 2;
1138 size = min(size, test->card->host->max_req_size);
1139 size = min(size, test->card->host->max_seg_size);
1140 size = min(size, test->card->host->max_blk_count * 512);
1143 return RESULT_UNSUP_HOST;
1145 for (i = 1;i < 4;i++) {
1146 sg_init_one(&sg, test->buffer + i, size);
1147 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1155 static int mmc_test_xfersize_write(struct mmc_test_card *test)
1159 ret = mmc_test_set_blksize(test, 512);
1163 ret = mmc_test_broken_transfer(test, 1, 512, 1);
1170 static int mmc_test_xfersize_read(struct mmc_test_card *test)
1174 ret = mmc_test_set_blksize(test, 512);
1178 ret = mmc_test_broken_transfer(test, 1, 512, 0);
1185 static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
1189 if (test->card->host->max_blk_count == 1)
1190 return RESULT_UNSUP_HOST;
1192 ret = mmc_test_set_blksize(test, 512);
1196 ret = mmc_test_broken_transfer(test, 2, 512, 1);
1203 static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
1207 if (test->card->host->max_blk_count == 1)
1208 return RESULT_UNSUP_HOST;
1210 ret = mmc_test_set_blksize(test, 512);
1214 ret = mmc_test_broken_transfer(test, 2, 512, 0);
1221 #ifdef CONFIG_HIGHMEM
1223 static int mmc_test_write_high(struct mmc_test_card *test)
1226 struct scatterlist sg;
1228 sg_init_table(&sg, 1);
1229 sg_set_page(&sg, test->highmem, 512, 0);
1231 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1238 static int mmc_test_read_high(struct mmc_test_card *test)
1241 struct scatterlist sg;
1243 sg_init_table(&sg, 1);
1244 sg_set_page(&sg, test->highmem, 512, 0);
1246 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1253 static int mmc_test_multi_write_high(struct mmc_test_card *test)
1257 struct scatterlist sg;
1259 if (test->card->host->max_blk_count == 1)
1260 return RESULT_UNSUP_HOST;
1262 size = PAGE_SIZE * 2;
1263 size = min(size, test->card->host->max_req_size);
1264 size = min(size, test->card->host->max_seg_size);
1265 size = min(size, test->card->host->max_blk_count * 512);
1268 return RESULT_UNSUP_HOST;
1270 sg_init_table(&sg, 1);
1271 sg_set_page(&sg, test->highmem, size, 0);
1273 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1280 static int mmc_test_multi_read_high(struct mmc_test_card *test)
1284 struct scatterlist sg;
1286 if (test->card->host->max_blk_count == 1)
1287 return RESULT_UNSUP_HOST;
1289 size = PAGE_SIZE * 2;
1290 size = min(size, test->card->host->max_req_size);
1291 size = min(size, test->card->host->max_seg_size);
1292 size = min(size, test->card->host->max_blk_count * 512);
1295 return RESULT_UNSUP_HOST;
1297 sg_init_table(&sg, 1);
1298 sg_set_page(&sg, test->highmem, size, 0);
1300 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1309 static int mmc_test_no_highmem(struct mmc_test_card *test)
1311 printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
1312 mmc_hostname(test->card->host));
1316 #endif /* CONFIG_HIGHMEM */
1319 * Map sz bytes so that it can be transferred.
1321 static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
1324 struct mmc_test_area *t = &test->area;
1327 t->blocks = sz >> 9;
1330 err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
1331 t->max_segs, t->max_seg_sz,
1334 err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
1335 t->max_seg_sz, &t->sg_len);
1338 printk(KERN_INFO "%s: Failed to map sg list\n",
1339 mmc_hostname(test->card->host));
1344 * Transfer bytes mapped by mmc_test_area_map().
1346 static int mmc_test_area_transfer(struct mmc_test_card *test,
1347 unsigned int dev_addr, int write)
1349 struct mmc_test_area *t = &test->area;
1351 return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
1352 t->blocks, 512, write);
1356 * Map and transfer bytes.
1358 static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
1359 unsigned int dev_addr, int write, int max_scatter,
1362 struct timespec ts1, ts2;
1366 * In the case of a maximally scattered transfer, the maximum transfer
1367 * size is further limited by using PAGE_SIZE segments.
1370 struct mmc_test_area *t = &test->area;
1371 unsigned long max_tfr;
1373 if (t->max_seg_sz >= PAGE_SIZE)
1374 max_tfr = t->max_segs * PAGE_SIZE;
1376 max_tfr = t->max_segs * t->max_seg_sz;
1381 ret = mmc_test_area_map(test, sz, max_scatter);
1386 getnstimeofday(&ts1);
1388 ret = mmc_test_area_transfer(test, dev_addr, write);
1393 getnstimeofday(&ts2);
1396 mmc_test_print_rate(test, sz, &ts1, &ts2);
1402 * Write the test area entirely.
1404 static int mmc_test_area_fill(struct mmc_test_card *test)
1406 return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
1411 * Erase the test area entirely.
1413 static int mmc_test_area_erase(struct mmc_test_card *test)
1415 struct mmc_test_area *t = &test->area;
1417 if (!mmc_can_erase(test->card))
1420 return mmc_erase(test->card, t->dev_addr, test->area.max_sz >> 9,
1425 * Cleanup struct mmc_test_area.
1427 static int mmc_test_area_cleanup(struct mmc_test_card *test)
1429 struct mmc_test_area *t = &test->area;
1432 mmc_test_free_mem(t->mem);
1438 * Initialize an area for testing large transfers. The size of the area is the
1439 * preferred erase size which is a good size for optimal transfer speed. Note
1440 * that is typically 4MiB for modern cards. The test area is set to the middle
1441 * of the card because cards may have different charateristics at the front
1442 * (for FAT file system optimization). Optionally, the area is erased (if the
1443 * card supports it) which may improve write performance. Optionally, the area
1444 * is filled with data for subsequent read tests.
1446 static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
1448 struct mmc_test_area *t = &test->area;
1449 unsigned long min_sz = 64 * 1024;
1452 ret = mmc_test_set_blksize(test, 512);
1456 if (test->card->pref_erase > TEST_AREA_MAX_SIZE >> 9)
1457 t->max_sz = TEST_AREA_MAX_SIZE;
1459 t->max_sz = (unsigned long)test->card->pref_erase << 9;
1461 t->max_segs = test->card->host->max_segs;
1462 t->max_seg_sz = test->card->host->max_seg_size;
1464 t->max_tfr = t->max_sz;
1465 if (t->max_tfr >> 9 > test->card->host->max_blk_count)
1466 t->max_tfr = test->card->host->max_blk_count << 9;
1467 if (t->max_tfr > test->card->host->max_req_size)
1468 t->max_tfr = test->card->host->max_req_size;
1469 if (t->max_tfr / t->max_seg_sz > t->max_segs)
1470 t->max_tfr = t->max_segs * t->max_seg_sz;
1473 * Try to allocate enough memory for a max. sized transfer. Less is OK
1474 * because the same memory can be mapped into the scatterlist more than
1475 * once. Also, take into account the limits imposed on scatterlist
1476 * segments by the host driver.
1478 t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
1483 t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
1489 t->dev_addr = mmc_test_capacity(test->card) / 2;
1490 t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
1493 ret = mmc_test_area_erase(test);
1499 ret = mmc_test_area_fill(test);
1507 mmc_test_area_cleanup(test);
1512 * Prepare for large transfers. Do not erase the test area.
1514 static int mmc_test_area_prepare(struct mmc_test_card *test)
1516 return mmc_test_area_init(test, 0, 0);
1520 * Prepare for large transfers. Do erase the test area.
1522 static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
1524 return mmc_test_area_init(test, 1, 0);
1528 * Prepare for large transfers. Erase and fill the test area.
1530 static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
1532 return mmc_test_area_init(test, 1, 1);
1536 * Test best-case performance. Best-case performance is expected from
1537 * a single large transfer.
1539 * An additional option (max_scatter) allows the measurement of the same
1540 * transfer but with no contiguous pages in the scatter list. This tests
1541 * the efficiency of DMA to handle scattered pages.
1543 static int mmc_test_best_performance(struct mmc_test_card *test, int write,
1546 return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
1547 write, max_scatter, 1);
1551 * Best-case read performance.
1553 static int mmc_test_best_read_performance(struct mmc_test_card *test)
1555 return mmc_test_best_performance(test, 0, 0);
1559 * Best-case write performance.
1561 static int mmc_test_best_write_performance(struct mmc_test_card *test)
1563 return mmc_test_best_performance(test, 1, 0);
1567 * Best-case read performance into scattered pages.
1569 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
1571 return mmc_test_best_performance(test, 0, 1);
1575 * Best-case write performance from scattered pages.
1577 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
1579 return mmc_test_best_performance(test, 1, 1);
1583 * Single read performance by transfer size.
1585 static int mmc_test_profile_read_perf(struct mmc_test_card *test)
1588 unsigned int dev_addr;
1591 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1592 dev_addr = test->area.dev_addr + (sz >> 9);
1593 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1597 sz = test->area.max_tfr;
1598 dev_addr = test->area.dev_addr;
1599 return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1603 * Single write performance by transfer size.
1605 static int mmc_test_profile_write_perf(struct mmc_test_card *test)
1608 unsigned int dev_addr;
1611 ret = mmc_test_area_erase(test);
1614 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1615 dev_addr = test->area.dev_addr + (sz >> 9);
1616 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1620 ret = mmc_test_area_erase(test);
1623 sz = test->area.max_tfr;
1624 dev_addr = test->area.dev_addr;
1625 return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1629 * Single trim performance by transfer size.
1631 static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
1634 unsigned int dev_addr;
1635 struct timespec ts1, ts2;
1638 if (!mmc_can_trim(test->card))
1639 return RESULT_UNSUP_CARD;
1641 if (!mmc_can_erase(test->card))
1642 return RESULT_UNSUP_HOST;
1644 for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
1645 dev_addr = test->area.dev_addr + (sz >> 9);
1646 getnstimeofday(&ts1);
1647 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1650 getnstimeofday(&ts2);
1651 mmc_test_print_rate(test, sz, &ts1, &ts2);
1653 dev_addr = test->area.dev_addr;
1654 getnstimeofday(&ts1);
1655 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1658 getnstimeofday(&ts2);
1659 mmc_test_print_rate(test, sz, &ts1, &ts2);
1663 static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
1665 unsigned int dev_addr, i, cnt;
1666 struct timespec ts1, ts2;
1669 cnt = test->area.max_sz / sz;
1670 dev_addr = test->area.dev_addr;
1671 getnstimeofday(&ts1);
1672 for (i = 0; i < cnt; i++) {
1673 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
1676 dev_addr += (sz >> 9);
1678 getnstimeofday(&ts2);
1679 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1684 * Consecutive read performance by transfer size.
1686 static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
1691 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1692 ret = mmc_test_seq_read_perf(test, sz);
1696 sz = test->area.max_tfr;
1697 return mmc_test_seq_read_perf(test, sz);
1700 static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
1702 unsigned int dev_addr, i, cnt;
1703 struct timespec ts1, ts2;
1706 ret = mmc_test_area_erase(test);
1709 cnt = test->area.max_sz / sz;
1710 dev_addr = test->area.dev_addr;
1711 getnstimeofday(&ts1);
1712 for (i = 0; i < cnt; i++) {
1713 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
1716 dev_addr += (sz >> 9);
1718 getnstimeofday(&ts2);
1719 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1724 * Consecutive write performance by transfer size.
1726 static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
1731 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1732 ret = mmc_test_seq_write_perf(test, sz);
1736 sz = test->area.max_tfr;
1737 return mmc_test_seq_write_perf(test, sz);
1741 * Consecutive trim performance by transfer size.
1743 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
1746 unsigned int dev_addr, i, cnt;
1747 struct timespec ts1, ts2;
1750 if (!mmc_can_trim(test->card))
1751 return RESULT_UNSUP_CARD;
1753 if (!mmc_can_erase(test->card))
1754 return RESULT_UNSUP_HOST;
1756 for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
1757 ret = mmc_test_area_erase(test);
1760 ret = mmc_test_area_fill(test);
1763 cnt = test->area.max_sz / sz;
1764 dev_addr = test->area.dev_addr;
1765 getnstimeofday(&ts1);
1766 for (i = 0; i < cnt; i++) {
1767 ret = mmc_erase(test->card, dev_addr, sz >> 9,
1771 dev_addr += (sz >> 9);
1773 getnstimeofday(&ts2);
1774 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1779 static const struct mmc_test_case mmc_test_cases[] = {
1781 .name = "Basic write (no data verification)",
1782 .run = mmc_test_basic_write,
1786 .name = "Basic read (no data verification)",
1787 .run = mmc_test_basic_read,
1791 .name = "Basic write (with data verification)",
1792 .prepare = mmc_test_prepare_write,
1793 .run = mmc_test_verify_write,
1794 .cleanup = mmc_test_cleanup,
1798 .name = "Basic read (with data verification)",
1799 .prepare = mmc_test_prepare_read,
1800 .run = mmc_test_verify_read,
1801 .cleanup = mmc_test_cleanup,
1805 .name = "Multi-block write",
1806 .prepare = mmc_test_prepare_write,
1807 .run = mmc_test_multi_write,
1808 .cleanup = mmc_test_cleanup,
1812 .name = "Multi-block read",
1813 .prepare = mmc_test_prepare_read,
1814 .run = mmc_test_multi_read,
1815 .cleanup = mmc_test_cleanup,
1819 .name = "Power of two block writes",
1820 .prepare = mmc_test_prepare_write,
1821 .run = mmc_test_pow2_write,
1822 .cleanup = mmc_test_cleanup,
1826 .name = "Power of two block reads",
1827 .prepare = mmc_test_prepare_read,
1828 .run = mmc_test_pow2_read,
1829 .cleanup = mmc_test_cleanup,
1833 .name = "Weird sized block writes",
1834 .prepare = mmc_test_prepare_write,
1835 .run = mmc_test_weird_write,
1836 .cleanup = mmc_test_cleanup,
1840 .name = "Weird sized block reads",
1841 .prepare = mmc_test_prepare_read,
1842 .run = mmc_test_weird_read,
1843 .cleanup = mmc_test_cleanup,
1847 .name = "Badly aligned write",
1848 .prepare = mmc_test_prepare_write,
1849 .run = mmc_test_align_write,
1850 .cleanup = mmc_test_cleanup,
1854 .name = "Badly aligned read",
1855 .prepare = mmc_test_prepare_read,
1856 .run = mmc_test_align_read,
1857 .cleanup = mmc_test_cleanup,
1861 .name = "Badly aligned multi-block write",
1862 .prepare = mmc_test_prepare_write,
1863 .run = mmc_test_align_multi_write,
1864 .cleanup = mmc_test_cleanup,
1868 .name = "Badly aligned multi-block read",
1869 .prepare = mmc_test_prepare_read,
1870 .run = mmc_test_align_multi_read,
1871 .cleanup = mmc_test_cleanup,
1875 .name = "Correct xfer_size at write (start failure)",
1876 .run = mmc_test_xfersize_write,
1880 .name = "Correct xfer_size at read (start failure)",
1881 .run = mmc_test_xfersize_read,
1885 .name = "Correct xfer_size at write (midway failure)",
1886 .run = mmc_test_multi_xfersize_write,
1890 .name = "Correct xfer_size at read (midway failure)",
1891 .run = mmc_test_multi_xfersize_read,
1894 #ifdef CONFIG_HIGHMEM
1897 .name = "Highmem write",
1898 .prepare = mmc_test_prepare_write,
1899 .run = mmc_test_write_high,
1900 .cleanup = mmc_test_cleanup,
1904 .name = "Highmem read",
1905 .prepare = mmc_test_prepare_read,
1906 .run = mmc_test_read_high,
1907 .cleanup = mmc_test_cleanup,
1911 .name = "Multi-block highmem write",
1912 .prepare = mmc_test_prepare_write,
1913 .run = mmc_test_multi_write_high,
1914 .cleanup = mmc_test_cleanup,
1918 .name = "Multi-block highmem read",
1919 .prepare = mmc_test_prepare_read,
1920 .run = mmc_test_multi_read_high,
1921 .cleanup = mmc_test_cleanup,
1927 .name = "Highmem write",
1928 .run = mmc_test_no_highmem,
1932 .name = "Highmem read",
1933 .run = mmc_test_no_highmem,
1937 .name = "Multi-block highmem write",
1938 .run = mmc_test_no_highmem,
1942 .name = "Multi-block highmem read",
1943 .run = mmc_test_no_highmem,
1946 #endif /* CONFIG_HIGHMEM */
1949 .name = "Best-case read performance",
1950 .prepare = mmc_test_area_prepare_fill,
1951 .run = mmc_test_best_read_performance,
1952 .cleanup = mmc_test_area_cleanup,
1956 .name = "Best-case write performance",
1957 .prepare = mmc_test_area_prepare_erase,
1958 .run = mmc_test_best_write_performance,
1959 .cleanup = mmc_test_area_cleanup,
1963 .name = "Best-case read performance into scattered pages",
1964 .prepare = mmc_test_area_prepare_fill,
1965 .run = mmc_test_best_read_perf_max_scatter,
1966 .cleanup = mmc_test_area_cleanup,
1970 .name = "Best-case write performance from scattered pages",
1971 .prepare = mmc_test_area_prepare_erase,
1972 .run = mmc_test_best_write_perf_max_scatter,
1973 .cleanup = mmc_test_area_cleanup,
1977 .name = "Single read performance by transfer size",
1978 .prepare = mmc_test_area_prepare_fill,
1979 .run = mmc_test_profile_read_perf,
1980 .cleanup = mmc_test_area_cleanup,
1984 .name = "Single write performance by transfer size",
1985 .prepare = mmc_test_area_prepare,
1986 .run = mmc_test_profile_write_perf,
1987 .cleanup = mmc_test_area_cleanup,
1991 .name = "Single trim performance by transfer size",
1992 .prepare = mmc_test_area_prepare_fill,
1993 .run = mmc_test_profile_trim_perf,
1994 .cleanup = mmc_test_area_cleanup,
1998 .name = "Consecutive read performance by transfer size",
1999 .prepare = mmc_test_area_prepare_fill,
2000 .run = mmc_test_profile_seq_read_perf,
2001 .cleanup = mmc_test_area_cleanup,
2005 .name = "Consecutive write performance by transfer size",
2006 .prepare = mmc_test_area_prepare,
2007 .run = mmc_test_profile_seq_write_perf,
2008 .cleanup = mmc_test_area_cleanup,
2012 .name = "Consecutive trim performance by transfer size",
2013 .prepare = mmc_test_area_prepare,
2014 .run = mmc_test_profile_seq_trim_perf,
2015 .cleanup = mmc_test_area_cleanup,
2020 static DEFINE_MUTEX(mmc_test_lock);
2022 static LIST_HEAD(mmc_test_result);
2024 static void mmc_test_run(struct mmc_test_card *test, int testcase)
2028 printk(KERN_INFO "%s: Starting tests of card %s...\n",
2029 mmc_hostname(test->card->host), mmc_card_id(test->card));
2031 mmc_claim_host(test->card->host);
2033 for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
2034 struct mmc_test_general_result *gr;
2036 if (testcase && ((i + 1) != testcase))
2039 printk(KERN_INFO "%s: Test case %d. %s...\n",
2040 mmc_hostname(test->card->host), i + 1,
2041 mmc_test_cases[i].name);
2043 if (mmc_test_cases[i].prepare) {
2044 ret = mmc_test_cases[i].prepare(test);
2046 printk(KERN_INFO "%s: Result: Prepare "
2047 "stage failed! (%d)\n",
2048 mmc_hostname(test->card->host),
2054 gr = kzalloc(sizeof(struct mmc_test_general_result),
2057 INIT_LIST_HEAD(&gr->tr_lst);
2059 /* Assign data what we know already */
2060 gr->card = test->card;
2063 /* Append container to global one */
2064 list_add_tail(&gr->link, &mmc_test_result);
2067 * Save the pointer to created container in our private
2073 ret = mmc_test_cases[i].run(test);
2076 printk(KERN_INFO "%s: Result: OK\n",
2077 mmc_hostname(test->card->host));
2080 printk(KERN_INFO "%s: Result: FAILED\n",
2081 mmc_hostname(test->card->host));
2083 case RESULT_UNSUP_HOST:
2084 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2086 mmc_hostname(test->card->host));
2088 case RESULT_UNSUP_CARD:
2089 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2091 mmc_hostname(test->card->host));
2094 printk(KERN_INFO "%s: Result: ERROR (%d)\n",
2095 mmc_hostname(test->card->host), ret);
2098 /* Save the result */
2102 if (mmc_test_cases[i].cleanup) {
2103 ret = mmc_test_cases[i].cleanup(test);
2105 printk(KERN_INFO "%s: Warning: Cleanup "
2106 "stage failed! (%d)\n",
2107 mmc_hostname(test->card->host),
2113 mmc_release_host(test->card->host);
2115 printk(KERN_INFO "%s: Tests completed.\n",
2116 mmc_hostname(test->card->host));
2119 static void mmc_test_free_result(struct mmc_card *card)
2121 struct mmc_test_general_result *gr, *grs;
2123 mutex_lock(&mmc_test_lock);
2125 list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
2126 struct mmc_test_transfer_result *tr, *trs;
2128 if (card && gr->card != card)
2131 list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
2132 list_del(&tr->link);
2136 list_del(&gr->link);
2140 mutex_unlock(&mmc_test_lock);
2143 static LIST_HEAD(mmc_test_file_test);
2145 static int mtf_test_show(struct seq_file *sf, void *data)
2147 struct mmc_card *card = (struct mmc_card *)sf->private;
2148 struct mmc_test_general_result *gr;
2150 mutex_lock(&mmc_test_lock);
2152 list_for_each_entry(gr, &mmc_test_result, link) {
2153 struct mmc_test_transfer_result *tr;
2155 if (gr->card != card)
2158 seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
2160 list_for_each_entry(tr, &gr->tr_lst, link) {
2161 seq_printf(sf, "%u %d %lu.%09lu %u\n",
2162 tr->count, tr->sectors,
2163 (unsigned long)tr->ts.tv_sec,
2164 (unsigned long)tr->ts.tv_nsec,
2169 mutex_unlock(&mmc_test_lock);
2174 static int mtf_test_open(struct inode *inode, struct file *file)
2176 return single_open(file, mtf_test_show, inode->i_private);
2179 static ssize_t mtf_test_write(struct file *file, const char __user *buf,
2180 size_t count, loff_t *pos)
2182 struct seq_file *sf = (struct seq_file *)file->private_data;
2183 struct mmc_card *card = (struct mmc_card *)sf->private;
2184 struct mmc_test_card *test;
2188 if (count >= sizeof(lbuf))
2191 if (copy_from_user(lbuf, buf, count))
2195 if (strict_strtol(lbuf, 10, &testcase))
2198 test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
2203 * Remove all test cases associated with given card. Thus we have only
2204 * actual data of the last run.
2206 mmc_test_free_result(card);
2210 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
2211 #ifdef CONFIG_HIGHMEM
2212 test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
2215 #ifdef CONFIG_HIGHMEM
2216 if (test->buffer && test->highmem) {
2220 mutex_lock(&mmc_test_lock);
2221 mmc_test_run(test, testcase);
2222 mutex_unlock(&mmc_test_lock);
2225 #ifdef CONFIG_HIGHMEM
2226 __free_pages(test->highmem, BUFFER_ORDER);
2228 kfree(test->buffer);
2234 static const struct file_operations mmc_test_fops_test = {
2235 .open = mtf_test_open,
2237 .write = mtf_test_write,
2238 .llseek = seq_lseek,
2239 .release = single_release,
2242 static void mmc_test_free_file_test(struct mmc_card *card)
2244 struct mmc_test_dbgfs_file *df, *dfs;
2246 mutex_lock(&mmc_test_lock);
2248 list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
2249 if (card && df->card != card)
2251 debugfs_remove(df->file);
2252 list_del(&df->link);
2256 mutex_unlock(&mmc_test_lock);
2259 static int mmc_test_register_file_test(struct mmc_card *card)
2261 struct dentry *file = NULL;
2262 struct mmc_test_dbgfs_file *df;
2265 mutex_lock(&mmc_test_lock);
2267 if (card->debugfs_root)
2268 file = debugfs_create_file("test", S_IWUSR | S_IRUGO,
2269 card->debugfs_root, card, &mmc_test_fops_test);
2271 if (IS_ERR_OR_NULL(file)) {
2273 "Can't create file. Perhaps debugfs is disabled.\n");
2278 df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
2280 debugfs_remove(file);
2282 "Can't allocate memory for internal usage.\n");
2290 list_add(&df->link, &mmc_test_file_test);
2293 mutex_unlock(&mmc_test_lock);
2298 static int mmc_test_probe(struct mmc_card *card)
2302 if (!mmc_card_mmc(card) && !mmc_card_sd(card))
2305 ret = mmc_test_register_file_test(card);
2309 dev_info(&card->dev, "Card claimed for testing.\n");
2314 static void mmc_test_remove(struct mmc_card *card)
2316 mmc_test_free_result(card);
2317 mmc_test_free_file_test(card);
2320 static struct mmc_driver mmc_driver = {
2324 .probe = mmc_test_probe,
2325 .remove = mmc_test_remove,
2328 static int __init mmc_test_init(void)
2330 return mmc_register_driver(&mmc_driver);
2333 static void __exit mmc_test_exit(void)
2335 /* Clear stalled data if card is still plugged */
2336 mmc_test_free_result(NULL);
2337 mmc_test_free_file_test(NULL);
2339 mmc_unregister_driver(&mmc_driver);
2342 module_init(mmc_test_init);
2343 module_exit(mmc_test_exit);
2345 MODULE_LICENSE("GPL");
2346 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
2347 MODULE_AUTHOR("Pierre Ossman");