ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / mmc / card / mmc_test.c
blob233cdfae92f4bdff7c814737ad2c6a4a4dd1c26e
1 /*
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>
26 #define RESULT_OK 0
27 #define RESULT_FAIL 1
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)
40 /**
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 {
46 struct page *page;
47 unsigned int order;
50 /**
51 * struct mmc_test_mem - allocated memory.
52 * @arr: array of allocations
53 * @cnt: number of allocations
55 struct mmc_test_mem {
56 struct mmc_test_pages *arr;
57 unsigned int cnt;
60 /**
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
70 * @sg: scatterlist
72 struct mmc_test_area {
73 unsigned long max_sz;
74 unsigned int dev_addr;
75 unsigned int max_tfr;
76 unsigned int max_segs;
77 unsigned int max_seg_sz;
78 unsigned int blocks;
79 unsigned int sg_len;
80 struct mmc_test_mem *mem;
81 struct scatterlist *sg;
84 /**
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;
95 unsigned int count;
96 unsigned int sectors;
97 struct timespec ts;
98 unsigned int rate;
99 unsigned int iops;
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;
113 int testcase;
114 int result;
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;
127 struct dentry *file;
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];
143 u8 *buffer;
144 #ifdef CONFIG_HIGHMEM
145 struct page *highmem;
146 #endif
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);
172 if (blocks > 1) {
173 mrq->cmd->opcode = write ?
174 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
175 } else {
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))
182 mrq->cmd->arg <<= 9;
184 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
186 if (blocks == 1)
187 mrq->stop = NULL;
188 else {
189 mrq->stop->opcode = MMC_STOP_TRANSMISSION;
190 mrq->stop->arg = 0;
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;
197 mrq->data->sg = sg;
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)
214 int ret, busy;
215 struct mmc_command cmd = {0};
217 busy = 0;
218 do {
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);
226 if (ret)
227 break;
229 if (!busy && mmc_test_busy(&cmd)) {
230 busy = 1;
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));
238 return ret;
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)
247 int ret;
249 struct mmc_request mrq = {0};
250 struct mmc_command cmd = {0};
251 struct mmc_command stop = {0};
252 struct mmc_data data = {0};
254 struct scatterlist sg;
256 mrq.cmd = &cmd;
257 mrq.data = &data;
258 mrq.stop = &stop;
260 sg_init_one(&sg, buffer, blksz);
262 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
264 mmc_wait_for_req(test->card->host, &mrq);
266 if (cmd.error)
267 return cmd.error;
268 if (data.error)
269 return data.error;
271 ret = mmc_test_wait_busy(test);
272 if (ret)
273 return ret;
275 return 0;
278 static void mmc_test_free_mem(struct mmc_test_mem *mem)
280 if (!mem)
281 return;
282 while (mem->cnt--)
283 __free_pages(mem->arr[mem->cnt].page,
284 mem->arr[mem->cnt].order);
285 kfree(mem->arr);
286 kfree(mem);
290 * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
291 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
292 * not exceed a maximum number of segments and try not to make segments much
293 * bigger than maximum segment size.
295 static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
296 unsigned long max_sz,
297 unsigned int max_segs,
298 unsigned int max_seg_sz)
300 unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
301 unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
302 unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
303 unsigned long page_cnt = 0;
304 unsigned long limit = nr_free_buffer_pages() >> 4;
305 struct mmc_test_mem *mem;
307 if (max_page_cnt > limit)
308 max_page_cnt = limit;
309 if (min_page_cnt > max_page_cnt)
310 min_page_cnt = max_page_cnt;
312 if (max_seg_page_cnt > max_page_cnt)
313 max_seg_page_cnt = max_page_cnt;
315 if (max_segs > max_page_cnt)
316 max_segs = max_page_cnt;
318 mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
319 if (!mem)
320 return NULL;
322 mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_segs,
323 GFP_KERNEL);
324 if (!mem->arr)
325 goto out_free;
327 while (max_page_cnt) {
328 struct page *page;
329 unsigned int order;
330 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
331 __GFP_NORETRY;
333 order = get_order(max_seg_page_cnt << PAGE_SHIFT);
334 while (1) {
335 page = alloc_pages(flags, order);
336 if (page || !order)
337 break;
338 order -= 1;
340 if (!page) {
341 if (page_cnt < min_page_cnt)
342 goto out_free;
343 break;
345 mem->arr[mem->cnt].page = page;
346 mem->arr[mem->cnt].order = order;
347 mem->cnt += 1;
348 if (max_page_cnt <= (1UL << order))
349 break;
350 max_page_cnt -= 1UL << order;
351 page_cnt += 1UL << order;
352 if (mem->cnt >= max_segs) {
353 if (page_cnt < min_page_cnt)
354 goto out_free;
355 break;
359 return mem;
361 out_free:
362 mmc_test_free_mem(mem);
363 return NULL;
367 * Map memory into a scatterlist. Optionally allow the same memory to be
368 * mapped more than once.
370 static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
371 struct scatterlist *sglist, int repeat,
372 unsigned int max_segs, unsigned int max_seg_sz,
373 unsigned int *sg_len)
375 struct scatterlist *sg = NULL;
376 unsigned int i;
378 sg_init_table(sglist, max_segs);
380 *sg_len = 0;
381 do {
382 for (i = 0; i < mem->cnt; i++) {
383 unsigned long len = PAGE_SIZE << mem->arr[i].order;
385 if (len > sz)
386 len = sz;
387 if (len > max_seg_sz)
388 len = max_seg_sz;
389 if (sg)
390 sg = sg_next(sg);
391 else
392 sg = sglist;
393 if (!sg)
394 return -EINVAL;
395 sg_set_page(sg, mem->arr[i].page, len, 0);
396 sz -= len;
397 *sg_len += 1;
398 if (!sz)
399 break;
401 } while (sz && repeat);
403 if (sz)
404 return -EINVAL;
406 if (sg)
407 sg_mark_end(sg);
409 return 0;
413 * Map memory into a scatterlist so that no pages are contiguous. Allow the
414 * same memory to be mapped more than once.
416 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
417 unsigned long sz,
418 struct scatterlist *sglist,
419 unsigned int max_segs,
420 unsigned int max_seg_sz,
421 unsigned int *sg_len)
423 struct scatterlist *sg = NULL;
424 unsigned int i = mem->cnt, cnt;
425 unsigned long len;
426 void *base, *addr, *last_addr = NULL;
428 sg_init_table(sglist, max_segs);
430 *sg_len = 0;
431 while (sz) {
432 base = page_address(mem->arr[--i].page);
433 cnt = 1 << mem->arr[i].order;
434 while (sz && cnt) {
435 addr = base + PAGE_SIZE * --cnt;
436 if (last_addr && last_addr + PAGE_SIZE == addr)
437 continue;
438 last_addr = addr;
439 len = PAGE_SIZE;
440 if (len > max_seg_sz)
441 len = max_seg_sz;
442 if (len > sz)
443 len = sz;
444 if (sg)
445 sg = sg_next(sg);
446 else
447 sg = sglist;
448 if (!sg)
449 return -EINVAL;
450 sg_set_page(sg, virt_to_page(addr), len, 0);
451 sz -= len;
452 *sg_len += 1;
454 if (i == 0)
455 i = mem->cnt;
458 if (sg)
459 sg_mark_end(sg);
461 return 0;
465 * Calculate transfer rate in bytes per second.
467 static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
469 uint64_t ns;
471 ns = ts->tv_sec;
472 ns *= 1000000000;
473 ns += ts->tv_nsec;
475 bytes *= 1000000000;
477 while (ns > UINT_MAX) {
478 bytes >>= 1;
479 ns >>= 1;
482 if (!ns)
483 return 0;
485 do_div(bytes, (uint32_t)ns);
487 return bytes;
491 * Save transfer results for future usage
493 static void mmc_test_save_transfer_result(struct mmc_test_card *test,
494 unsigned int count, unsigned int sectors, struct timespec ts,
495 unsigned int rate, unsigned int iops)
497 struct mmc_test_transfer_result *tr;
499 if (!test->gr)
500 return;
502 tr = kmalloc(sizeof(struct mmc_test_transfer_result), GFP_KERNEL);
503 if (!tr)
504 return;
506 tr->count = count;
507 tr->sectors = sectors;
508 tr->ts = ts;
509 tr->rate = rate;
510 tr->iops = iops;
512 list_add_tail(&tr->link, &test->gr->tr_lst);
516 * Print the transfer rate.
518 static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
519 struct timespec *ts1, struct timespec *ts2)
521 unsigned int rate, iops, sectors = bytes >> 9;
522 struct timespec ts;
524 ts = timespec_sub(*ts2, *ts1);
526 rate = mmc_test_rate(bytes, &ts);
527 iops = mmc_test_rate(100, &ts); /* I/O ops per sec x 100 */
529 printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
530 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
531 mmc_hostname(test->card->host), sectors, sectors >> 1,
532 (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
533 (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024,
534 iops / 100, iops % 100);
536 mmc_test_save_transfer_result(test, 1, sectors, ts, rate, iops);
540 * Print the average transfer rate.
542 static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
543 unsigned int count, struct timespec *ts1,
544 struct timespec *ts2)
546 unsigned int rate, iops, sectors = bytes >> 9;
547 uint64_t tot = bytes * count;
548 struct timespec ts;
550 ts = timespec_sub(*ts2, *ts1);
552 rate = mmc_test_rate(tot, &ts);
553 iops = mmc_test_rate(count * 100, &ts); /* I/O ops per sec x 100 */
555 printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
556 "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
557 "%u.%02u IOPS)\n",
558 mmc_hostname(test->card->host), count, sectors, count,
559 sectors >> 1, (sectors & 1 ? ".5" : ""),
560 (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
561 rate / 1000, rate / 1024, iops / 100, iops % 100);
563 mmc_test_save_transfer_result(test, count, sectors, ts, rate, iops);
567 * Return the card size in sectors.
569 static unsigned int mmc_test_capacity(struct mmc_card *card)
571 if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
572 return card->ext_csd.sectors;
573 else
574 return card->csd.capacity << (card->csd.read_blkbits - 9);
577 /*******************************************************************/
578 /* Test preparation and cleanup */
579 /*******************************************************************/
582 * Fill the first couple of sectors of the card with known data
583 * so that bad reads/writes can be detected
585 static int __mmc_test_prepare(struct mmc_test_card *test, int write)
587 int ret, i;
589 ret = mmc_test_set_blksize(test, 512);
590 if (ret)
591 return ret;
593 if (write)
594 memset(test->buffer, 0xDF, 512);
595 else {
596 for (i = 0;i < 512;i++)
597 test->buffer[i] = i;
600 for (i = 0;i < BUFFER_SIZE / 512;i++) {
601 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
602 if (ret)
603 return ret;
606 return 0;
609 static int mmc_test_prepare_write(struct mmc_test_card *test)
611 return __mmc_test_prepare(test, 1);
614 static int mmc_test_prepare_read(struct mmc_test_card *test)
616 return __mmc_test_prepare(test, 0);
619 static int mmc_test_cleanup(struct mmc_test_card *test)
621 int ret, i;
623 ret = mmc_test_set_blksize(test, 512);
624 if (ret)
625 return ret;
627 memset(test->buffer, 0, 512);
629 for (i = 0;i < BUFFER_SIZE / 512;i++) {
630 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
631 if (ret)
632 return ret;
635 return 0;
638 /*******************************************************************/
639 /* Test execution helpers */
640 /*******************************************************************/
643 * Modifies the mmc_request to perform the "short transfer" tests
645 static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
646 struct mmc_request *mrq, int write)
648 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
650 if (mrq->data->blocks > 1) {
651 mrq->cmd->opcode = write ?
652 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
653 mrq->stop = NULL;
654 } else {
655 mrq->cmd->opcode = MMC_SEND_STATUS;
656 mrq->cmd->arg = test->card->rca << 16;
661 * Checks that a normal transfer didn't have any errors
663 static int mmc_test_check_result(struct mmc_test_card *test,
664 struct mmc_request *mrq)
666 int ret;
668 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
670 ret = 0;
672 if (!ret && mrq->cmd->error)
673 ret = mrq->cmd->error;
674 if (!ret && mrq->data->error)
675 ret = mrq->data->error;
676 if (!ret && mrq->stop && mrq->stop->error)
677 ret = mrq->stop->error;
678 if (!ret && mrq->data->bytes_xfered !=
679 mrq->data->blocks * mrq->data->blksz)
680 ret = RESULT_FAIL;
682 if (ret == -EINVAL)
683 ret = RESULT_UNSUP_HOST;
685 return ret;
689 * Checks that a "short transfer" behaved as expected
691 static int mmc_test_check_broken_result(struct mmc_test_card *test,
692 struct mmc_request *mrq)
694 int ret;
696 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
698 ret = 0;
700 if (!ret && mrq->cmd->error)
701 ret = mrq->cmd->error;
702 if (!ret && mrq->data->error == 0)
703 ret = RESULT_FAIL;
704 if (!ret && mrq->data->error != -ETIMEDOUT)
705 ret = mrq->data->error;
706 if (!ret && mrq->stop && mrq->stop->error)
707 ret = mrq->stop->error;
708 if (mrq->data->blocks > 1) {
709 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
710 ret = RESULT_FAIL;
711 } else {
712 if (!ret && mrq->data->bytes_xfered > 0)
713 ret = RESULT_FAIL;
716 if (ret == -EINVAL)
717 ret = RESULT_UNSUP_HOST;
719 return ret;
723 * Tests a basic transfer with certain parameters
725 static int mmc_test_simple_transfer(struct mmc_test_card *test,
726 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
727 unsigned blocks, unsigned blksz, int write)
729 struct mmc_request mrq = {0};
730 struct mmc_command cmd = {0};
731 struct mmc_command stop = {0};
732 struct mmc_data data = {0};
734 mrq.cmd = &cmd;
735 mrq.data = &data;
736 mrq.stop = &stop;
738 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
739 blocks, blksz, write);
741 mmc_wait_for_req(test->card->host, &mrq);
743 mmc_test_wait_busy(test);
745 return mmc_test_check_result(test, &mrq);
749 * Tests a transfer where the card will fail completely or partly
751 static int mmc_test_broken_transfer(struct mmc_test_card *test,
752 unsigned blocks, unsigned blksz, int write)
754 struct mmc_request mrq = {0};
755 struct mmc_command cmd = {0};
756 struct mmc_command stop = {0};
757 struct mmc_data data = {0};
759 struct scatterlist sg;
761 mrq.cmd = &cmd;
762 mrq.data = &data;
763 mrq.stop = &stop;
765 sg_init_one(&sg, test->buffer, blocks * blksz);
767 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
768 mmc_test_prepare_broken_mrq(test, &mrq, write);
770 mmc_wait_for_req(test->card->host, &mrq);
772 mmc_test_wait_busy(test);
774 return mmc_test_check_broken_result(test, &mrq);
778 * Does a complete transfer test where data is also validated
780 * Note: mmc_test_prepare() must have been done before this call
782 static int mmc_test_transfer(struct mmc_test_card *test,
783 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
784 unsigned blocks, unsigned blksz, int write)
786 int ret, i;
787 unsigned long flags;
789 if (write) {
790 for (i = 0;i < blocks * blksz;i++)
791 test->scratch[i] = i;
792 } else {
793 memset(test->scratch, 0, BUFFER_SIZE);
795 local_irq_save(flags);
796 sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
797 local_irq_restore(flags);
799 ret = mmc_test_set_blksize(test, blksz);
800 if (ret)
801 return ret;
803 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
804 blocks, blksz, write);
805 if (ret)
806 return ret;
808 if (write) {
809 int sectors;
811 ret = mmc_test_set_blksize(test, 512);
812 if (ret)
813 return ret;
815 sectors = (blocks * blksz + 511) / 512;
816 if ((sectors * 512) == (blocks * blksz))
817 sectors++;
819 if ((sectors * 512) > BUFFER_SIZE)
820 return -EINVAL;
822 memset(test->buffer, 0, sectors * 512);
824 for (i = 0;i < sectors;i++) {
825 ret = mmc_test_buffer_transfer(test,
826 test->buffer + i * 512,
827 dev_addr + i, 512, 0);
828 if (ret)
829 return ret;
832 for (i = 0;i < blocks * blksz;i++) {
833 if (test->buffer[i] != (u8)i)
834 return RESULT_FAIL;
837 for (;i < sectors * 512;i++) {
838 if (test->buffer[i] != 0xDF)
839 return RESULT_FAIL;
841 } else {
842 local_irq_save(flags);
843 sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
844 local_irq_restore(flags);
845 for (i = 0;i < blocks * blksz;i++) {
846 if (test->scratch[i] != (u8)i)
847 return RESULT_FAIL;
851 return 0;
854 /*******************************************************************/
855 /* Tests */
856 /*******************************************************************/
858 struct mmc_test_case {
859 const char *name;
861 int (*prepare)(struct mmc_test_card *);
862 int (*run)(struct mmc_test_card *);
863 int (*cleanup)(struct mmc_test_card *);
866 static int mmc_test_basic_write(struct mmc_test_card *test)
868 int ret;
869 struct scatterlist sg;
871 ret = mmc_test_set_blksize(test, 512);
872 if (ret)
873 return ret;
875 sg_init_one(&sg, test->buffer, 512);
877 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
878 if (ret)
879 return ret;
881 return 0;
884 static int mmc_test_basic_read(struct mmc_test_card *test)
886 int ret;
887 struct scatterlist sg;
889 ret = mmc_test_set_blksize(test, 512);
890 if (ret)
891 return ret;
893 sg_init_one(&sg, test->buffer, 512);
895 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
896 if (ret)
897 return ret;
899 return 0;
902 static int mmc_test_verify_write(struct mmc_test_card *test)
904 int ret;
905 struct scatterlist sg;
907 sg_init_one(&sg, test->buffer, 512);
909 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
910 if (ret)
911 return ret;
913 return 0;
916 static int mmc_test_verify_read(struct mmc_test_card *test)
918 int ret;
919 struct scatterlist sg;
921 sg_init_one(&sg, test->buffer, 512);
923 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
924 if (ret)
925 return ret;
927 return 0;
930 static int mmc_test_multi_write(struct mmc_test_card *test)
932 int ret;
933 unsigned int size;
934 struct scatterlist sg;
936 if (test->card->host->max_blk_count == 1)
937 return RESULT_UNSUP_HOST;
939 size = PAGE_SIZE * 2;
940 size = min(size, test->card->host->max_req_size);
941 size = min(size, test->card->host->max_seg_size);
942 size = min(size, test->card->host->max_blk_count * 512);
944 if (size < 1024)
945 return RESULT_UNSUP_HOST;
947 sg_init_one(&sg, test->buffer, size);
949 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
950 if (ret)
951 return ret;
953 return 0;
956 static int mmc_test_multi_read(struct mmc_test_card *test)
958 int ret;
959 unsigned int size;
960 struct scatterlist sg;
962 if (test->card->host->max_blk_count == 1)
963 return RESULT_UNSUP_HOST;
965 size = PAGE_SIZE * 2;
966 size = min(size, test->card->host->max_req_size);
967 size = min(size, test->card->host->max_seg_size);
968 size = min(size, test->card->host->max_blk_count * 512);
970 if (size < 1024)
971 return RESULT_UNSUP_HOST;
973 sg_init_one(&sg, test->buffer, size);
975 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
976 if (ret)
977 return ret;
979 return 0;
982 static int mmc_test_pow2_write(struct mmc_test_card *test)
984 int ret, i;
985 struct scatterlist sg;
987 if (!test->card->csd.write_partial)
988 return RESULT_UNSUP_CARD;
990 for (i = 1; i < 512;i <<= 1) {
991 sg_init_one(&sg, test->buffer, i);
992 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
993 if (ret)
994 return ret;
997 return 0;
1000 static int mmc_test_pow2_read(struct mmc_test_card *test)
1002 int ret, i;
1003 struct scatterlist sg;
1005 if (!test->card->csd.read_partial)
1006 return RESULT_UNSUP_CARD;
1008 for (i = 1; i < 512;i <<= 1) {
1009 sg_init_one(&sg, test->buffer, i);
1010 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1011 if (ret)
1012 return ret;
1015 return 0;
1018 static int mmc_test_weird_write(struct mmc_test_card *test)
1020 int ret, i;
1021 struct scatterlist sg;
1023 if (!test->card->csd.write_partial)
1024 return RESULT_UNSUP_CARD;
1026 for (i = 3; i < 512;i += 7) {
1027 sg_init_one(&sg, test->buffer, i);
1028 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1029 if (ret)
1030 return ret;
1033 return 0;
1036 static int mmc_test_weird_read(struct mmc_test_card *test)
1038 int ret, i;
1039 struct scatterlist sg;
1041 if (!test->card->csd.read_partial)
1042 return RESULT_UNSUP_CARD;
1044 for (i = 3; i < 512;i += 7) {
1045 sg_init_one(&sg, test->buffer, i);
1046 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1047 if (ret)
1048 return ret;
1051 return 0;
1054 static int mmc_test_align_write(struct mmc_test_card *test)
1056 int ret, i;
1057 struct scatterlist sg;
1059 for (i = 1;i < 4;i++) {
1060 sg_init_one(&sg, test->buffer + i, 512);
1061 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1062 if (ret)
1063 return ret;
1066 return 0;
1069 static int mmc_test_align_read(struct mmc_test_card *test)
1071 int ret, i;
1072 struct scatterlist sg;
1074 for (i = 1;i < 4;i++) {
1075 sg_init_one(&sg, test->buffer + i, 512);
1076 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1077 if (ret)
1078 return ret;
1081 return 0;
1084 static int mmc_test_align_multi_write(struct mmc_test_card *test)
1086 int ret, i;
1087 unsigned int size;
1088 struct scatterlist sg;
1090 if (test->card->host->max_blk_count == 1)
1091 return RESULT_UNSUP_HOST;
1093 size = PAGE_SIZE * 2;
1094 size = min(size, test->card->host->max_req_size);
1095 size = min(size, test->card->host->max_seg_size);
1096 size = min(size, test->card->host->max_blk_count * 512);
1098 if (size < 1024)
1099 return RESULT_UNSUP_HOST;
1101 for (i = 1;i < 4;i++) {
1102 sg_init_one(&sg, test->buffer + i, size);
1103 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1104 if (ret)
1105 return ret;
1108 return 0;
1111 static int mmc_test_align_multi_read(struct mmc_test_card *test)
1113 int ret, i;
1114 unsigned int size;
1115 struct scatterlist sg;
1117 if (test->card->host->max_blk_count == 1)
1118 return RESULT_UNSUP_HOST;
1120 size = PAGE_SIZE * 2;
1121 size = min(size, test->card->host->max_req_size);
1122 size = min(size, test->card->host->max_seg_size);
1123 size = min(size, test->card->host->max_blk_count * 512);
1125 if (size < 1024)
1126 return RESULT_UNSUP_HOST;
1128 for (i = 1;i < 4;i++) {
1129 sg_init_one(&sg, test->buffer + i, size);
1130 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1131 if (ret)
1132 return ret;
1135 return 0;
1138 static int mmc_test_xfersize_write(struct mmc_test_card *test)
1140 int ret;
1142 ret = mmc_test_set_blksize(test, 512);
1143 if (ret)
1144 return ret;
1146 ret = mmc_test_broken_transfer(test, 1, 512, 1);
1147 if (ret)
1148 return ret;
1150 return 0;
1153 static int mmc_test_xfersize_read(struct mmc_test_card *test)
1155 int ret;
1157 ret = mmc_test_set_blksize(test, 512);
1158 if (ret)
1159 return ret;
1161 ret = mmc_test_broken_transfer(test, 1, 512, 0);
1162 if (ret)
1163 return ret;
1165 return 0;
1168 static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
1170 int ret;
1172 if (test->card->host->max_blk_count == 1)
1173 return RESULT_UNSUP_HOST;
1175 ret = mmc_test_set_blksize(test, 512);
1176 if (ret)
1177 return ret;
1179 ret = mmc_test_broken_transfer(test, 2, 512, 1);
1180 if (ret)
1181 return ret;
1183 return 0;
1186 static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
1188 int ret;
1190 if (test->card->host->max_blk_count == 1)
1191 return RESULT_UNSUP_HOST;
1193 ret = mmc_test_set_blksize(test, 512);
1194 if (ret)
1195 return ret;
1197 ret = mmc_test_broken_transfer(test, 2, 512, 0);
1198 if (ret)
1199 return ret;
1201 return 0;
1204 #ifdef CONFIG_HIGHMEM
1206 static int mmc_test_write_high(struct mmc_test_card *test)
1208 int ret;
1209 struct scatterlist sg;
1211 sg_init_table(&sg, 1);
1212 sg_set_page(&sg, test->highmem, 512, 0);
1214 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1215 if (ret)
1216 return ret;
1218 return 0;
1221 static int mmc_test_read_high(struct mmc_test_card *test)
1223 int ret;
1224 struct scatterlist sg;
1226 sg_init_table(&sg, 1);
1227 sg_set_page(&sg, test->highmem, 512, 0);
1229 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1230 if (ret)
1231 return ret;
1233 return 0;
1236 static int mmc_test_multi_write_high(struct mmc_test_card *test)
1238 int ret;
1239 unsigned int size;
1240 struct scatterlist sg;
1242 if (test->card->host->max_blk_count == 1)
1243 return RESULT_UNSUP_HOST;
1245 size = PAGE_SIZE * 2;
1246 size = min(size, test->card->host->max_req_size);
1247 size = min(size, test->card->host->max_seg_size);
1248 size = min(size, test->card->host->max_blk_count * 512);
1250 if (size < 1024)
1251 return RESULT_UNSUP_HOST;
1253 sg_init_table(&sg, 1);
1254 sg_set_page(&sg, test->highmem, size, 0);
1256 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1257 if (ret)
1258 return ret;
1260 return 0;
1263 static int mmc_test_multi_read_high(struct mmc_test_card *test)
1265 int ret;
1266 unsigned int size;
1267 struct scatterlist sg;
1269 if (test->card->host->max_blk_count == 1)
1270 return RESULT_UNSUP_HOST;
1272 size = PAGE_SIZE * 2;
1273 size = min(size, test->card->host->max_req_size);
1274 size = min(size, test->card->host->max_seg_size);
1275 size = min(size, test->card->host->max_blk_count * 512);
1277 if (size < 1024)
1278 return RESULT_UNSUP_HOST;
1280 sg_init_table(&sg, 1);
1281 sg_set_page(&sg, test->highmem, size, 0);
1283 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1284 if (ret)
1285 return ret;
1287 return 0;
1290 #else
1292 static int mmc_test_no_highmem(struct mmc_test_card *test)
1294 printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
1295 mmc_hostname(test->card->host));
1296 return 0;
1299 #endif /* CONFIG_HIGHMEM */
1302 * Map sz bytes so that it can be transferred.
1304 static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
1305 int max_scatter)
1307 struct mmc_test_area *t = &test->area;
1308 int err;
1310 t->blocks = sz >> 9;
1312 if (max_scatter) {
1313 err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
1314 t->max_segs, t->max_seg_sz,
1315 &t->sg_len);
1316 } else {
1317 err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
1318 t->max_seg_sz, &t->sg_len);
1320 if (err)
1321 printk(KERN_INFO "%s: Failed to map sg list\n",
1322 mmc_hostname(test->card->host));
1323 return err;
1327 * Transfer bytes mapped by mmc_test_area_map().
1329 static int mmc_test_area_transfer(struct mmc_test_card *test,
1330 unsigned int dev_addr, int write)
1332 struct mmc_test_area *t = &test->area;
1334 return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
1335 t->blocks, 512, write);
1339 * Map and transfer bytes.
1341 static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
1342 unsigned int dev_addr, int write, int max_scatter,
1343 int timed)
1345 struct timespec ts1, ts2;
1346 int ret;
1349 * In the case of a maximally scattered transfer, the maximum transfer
1350 * size is further limited by using PAGE_SIZE segments.
1352 if (max_scatter) {
1353 struct mmc_test_area *t = &test->area;
1354 unsigned long max_tfr;
1356 if (t->max_seg_sz >= PAGE_SIZE)
1357 max_tfr = t->max_segs * PAGE_SIZE;
1358 else
1359 max_tfr = t->max_segs * t->max_seg_sz;
1360 if (sz > max_tfr)
1361 sz = max_tfr;
1364 ret = mmc_test_area_map(test, sz, max_scatter);
1365 if (ret)
1366 return ret;
1368 if (timed)
1369 getnstimeofday(&ts1);
1371 ret = mmc_test_area_transfer(test, dev_addr, write);
1372 if (ret)
1373 return ret;
1375 if (timed)
1376 getnstimeofday(&ts2);
1378 if (timed)
1379 mmc_test_print_rate(test, sz, &ts1, &ts2);
1381 return 0;
1385 * Write the test area entirely.
1387 static int mmc_test_area_fill(struct mmc_test_card *test)
1389 struct mmc_test_area *t = &test->area;
1391 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, 1, 0, 0);
1395 * Erase the test area entirely.
1397 static int mmc_test_area_erase(struct mmc_test_card *test)
1399 struct mmc_test_area *t = &test->area;
1401 if (!mmc_can_erase(test->card))
1402 return 0;
1404 return mmc_erase(test->card, t->dev_addr, t->max_sz >> 9,
1405 MMC_ERASE_ARG);
1409 * Cleanup struct mmc_test_area.
1411 static int mmc_test_area_cleanup(struct mmc_test_card *test)
1413 struct mmc_test_area *t = &test->area;
1415 kfree(t->sg);
1416 mmc_test_free_mem(t->mem);
1418 return 0;
1422 * Initialize an area for testing large transfers. The test area is set to the
1423 * middle of the card because cards may have different charateristics at the
1424 * front (for FAT file system optimization). Optionally, the area is erased
1425 * (if the card supports it) which may improve write performance. Optionally,
1426 * the area is filled with data for subsequent read tests.
1428 static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
1430 struct mmc_test_area *t = &test->area;
1431 unsigned long min_sz = 64 * 1024, sz;
1432 int ret;
1434 ret = mmc_test_set_blksize(test, 512);
1435 if (ret)
1436 return ret;
1438 /* Make the test area size about 4MiB */
1439 sz = (unsigned long)test->card->pref_erase << 9;
1440 t->max_sz = sz;
1441 while (t->max_sz < 4 * 1024 * 1024)
1442 t->max_sz += sz;
1443 while (t->max_sz > TEST_AREA_MAX_SIZE && t->max_sz > sz)
1444 t->max_sz -= sz;
1446 t->max_segs = test->card->host->max_segs;
1447 t->max_seg_sz = test->card->host->max_seg_size;
1449 t->max_tfr = t->max_sz;
1450 if (t->max_tfr >> 9 > test->card->host->max_blk_count)
1451 t->max_tfr = test->card->host->max_blk_count << 9;
1452 if (t->max_tfr > test->card->host->max_req_size)
1453 t->max_tfr = test->card->host->max_req_size;
1454 if (t->max_tfr / t->max_seg_sz > t->max_segs)
1455 t->max_tfr = t->max_segs * t->max_seg_sz;
1458 * Try to allocate enough memory for a max. sized transfer. Less is OK
1459 * because the same memory can be mapped into the scatterlist more than
1460 * once. Also, take into account the limits imposed on scatterlist
1461 * segments by the host driver.
1463 t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
1464 t->max_seg_sz);
1465 if (!t->mem)
1466 return -ENOMEM;
1468 t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
1469 if (!t->sg) {
1470 ret = -ENOMEM;
1471 goto out_free;
1474 t->dev_addr = mmc_test_capacity(test->card) / 2;
1475 t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
1477 if (erase) {
1478 ret = mmc_test_area_erase(test);
1479 if (ret)
1480 goto out_free;
1483 if (fill) {
1484 ret = mmc_test_area_fill(test);
1485 if (ret)
1486 goto out_free;
1489 return 0;
1491 out_free:
1492 mmc_test_area_cleanup(test);
1493 return ret;
1497 * Prepare for large transfers. Do not erase the test area.
1499 static int mmc_test_area_prepare(struct mmc_test_card *test)
1501 return mmc_test_area_init(test, 0, 0);
1505 * Prepare for large transfers. Do erase the test area.
1507 static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
1509 return mmc_test_area_init(test, 1, 0);
1513 * Prepare for large transfers. Erase and fill the test area.
1515 static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
1517 return mmc_test_area_init(test, 1, 1);
1521 * Test best-case performance. Best-case performance is expected from
1522 * a single large transfer.
1524 * An additional option (max_scatter) allows the measurement of the same
1525 * transfer but with no contiguous pages in the scatter list. This tests
1526 * the efficiency of DMA to handle scattered pages.
1528 static int mmc_test_best_performance(struct mmc_test_card *test, int write,
1529 int max_scatter)
1531 struct mmc_test_area *t = &test->area;
1533 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, write,
1534 max_scatter, 1);
1538 * Best-case read performance.
1540 static int mmc_test_best_read_performance(struct mmc_test_card *test)
1542 return mmc_test_best_performance(test, 0, 0);
1546 * Best-case write performance.
1548 static int mmc_test_best_write_performance(struct mmc_test_card *test)
1550 return mmc_test_best_performance(test, 1, 0);
1554 * Best-case read performance into scattered pages.
1556 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
1558 return mmc_test_best_performance(test, 0, 1);
1562 * Best-case write performance from scattered pages.
1564 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
1566 return mmc_test_best_performance(test, 1, 1);
1570 * Single read performance by transfer size.
1572 static int mmc_test_profile_read_perf(struct mmc_test_card *test)
1574 struct mmc_test_area *t = &test->area;
1575 unsigned long sz;
1576 unsigned int dev_addr;
1577 int ret;
1579 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1580 dev_addr = t->dev_addr + (sz >> 9);
1581 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1582 if (ret)
1583 return ret;
1585 sz = t->max_tfr;
1586 dev_addr = t->dev_addr;
1587 return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1591 * Single write performance by transfer size.
1593 static int mmc_test_profile_write_perf(struct mmc_test_card *test)
1595 struct mmc_test_area *t = &test->area;
1596 unsigned long sz;
1597 unsigned int dev_addr;
1598 int ret;
1600 ret = mmc_test_area_erase(test);
1601 if (ret)
1602 return ret;
1603 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1604 dev_addr = t->dev_addr + (sz >> 9);
1605 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1606 if (ret)
1607 return ret;
1609 ret = mmc_test_area_erase(test);
1610 if (ret)
1611 return ret;
1612 sz = t->max_tfr;
1613 dev_addr = t->dev_addr;
1614 return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1618 * Single trim performance by transfer size.
1620 static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
1622 struct mmc_test_area *t = &test->area;
1623 unsigned long sz;
1624 unsigned int dev_addr;
1625 struct timespec ts1, ts2;
1626 int ret;
1628 if (!mmc_can_trim(test->card))
1629 return RESULT_UNSUP_CARD;
1631 if (!mmc_can_erase(test->card))
1632 return RESULT_UNSUP_HOST;
1634 for (sz = 512; sz < t->max_sz; sz <<= 1) {
1635 dev_addr = t->dev_addr + (sz >> 9);
1636 getnstimeofday(&ts1);
1637 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1638 if (ret)
1639 return ret;
1640 getnstimeofday(&ts2);
1641 mmc_test_print_rate(test, sz, &ts1, &ts2);
1643 dev_addr = t->dev_addr;
1644 getnstimeofday(&ts1);
1645 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1646 if (ret)
1647 return ret;
1648 getnstimeofday(&ts2);
1649 mmc_test_print_rate(test, sz, &ts1, &ts2);
1650 return 0;
1653 static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
1655 struct mmc_test_area *t = &test->area;
1656 unsigned int dev_addr, i, cnt;
1657 struct timespec ts1, ts2;
1658 int ret;
1660 cnt = t->max_sz / sz;
1661 dev_addr = t->dev_addr;
1662 getnstimeofday(&ts1);
1663 for (i = 0; i < cnt; i++) {
1664 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
1665 if (ret)
1666 return ret;
1667 dev_addr += (sz >> 9);
1669 getnstimeofday(&ts2);
1670 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1671 return 0;
1675 * Consecutive read performance by transfer size.
1677 static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
1679 struct mmc_test_area *t = &test->area;
1680 unsigned long sz;
1681 int ret;
1683 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1684 ret = mmc_test_seq_read_perf(test, sz);
1685 if (ret)
1686 return ret;
1688 sz = t->max_tfr;
1689 return mmc_test_seq_read_perf(test, sz);
1692 static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
1694 struct mmc_test_area *t = &test->area;
1695 unsigned int dev_addr, i, cnt;
1696 struct timespec ts1, ts2;
1697 int ret;
1699 ret = mmc_test_area_erase(test);
1700 if (ret)
1701 return ret;
1702 cnt = t->max_sz / sz;
1703 dev_addr = t->dev_addr;
1704 getnstimeofday(&ts1);
1705 for (i = 0; i < cnt; i++) {
1706 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
1707 if (ret)
1708 return ret;
1709 dev_addr += (sz >> 9);
1711 getnstimeofday(&ts2);
1712 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1713 return 0;
1717 * Consecutive write performance by transfer size.
1719 static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
1721 struct mmc_test_area *t = &test->area;
1722 unsigned long sz;
1723 int ret;
1725 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1726 ret = mmc_test_seq_write_perf(test, sz);
1727 if (ret)
1728 return ret;
1730 sz = t->max_tfr;
1731 return mmc_test_seq_write_perf(test, sz);
1735 * Consecutive trim performance by transfer size.
1737 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
1739 struct mmc_test_area *t = &test->area;
1740 unsigned long sz;
1741 unsigned int dev_addr, i, cnt;
1742 struct timespec ts1, ts2;
1743 int ret;
1745 if (!mmc_can_trim(test->card))
1746 return RESULT_UNSUP_CARD;
1748 if (!mmc_can_erase(test->card))
1749 return RESULT_UNSUP_HOST;
1751 for (sz = 512; sz <= t->max_sz; sz <<= 1) {
1752 ret = mmc_test_area_erase(test);
1753 if (ret)
1754 return ret;
1755 ret = mmc_test_area_fill(test);
1756 if (ret)
1757 return ret;
1758 cnt = t->max_sz / sz;
1759 dev_addr = t->dev_addr;
1760 getnstimeofday(&ts1);
1761 for (i = 0; i < cnt; i++) {
1762 ret = mmc_erase(test->card, dev_addr, sz >> 9,
1763 MMC_TRIM_ARG);
1764 if (ret)
1765 return ret;
1766 dev_addr += (sz >> 9);
1768 getnstimeofday(&ts2);
1769 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1771 return 0;
1774 static unsigned int rnd_next = 1;
1776 static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt)
1778 uint64_t r;
1780 rnd_next = rnd_next * 1103515245 + 12345;
1781 r = (rnd_next >> 16) & 0x7fff;
1782 return (r * rnd_cnt) >> 15;
1785 static int mmc_test_rnd_perf(struct mmc_test_card *test, int write, int print,
1786 unsigned long sz)
1788 unsigned int dev_addr, cnt, rnd_addr, range1, range2, last_ea = 0, ea;
1789 unsigned int ssz;
1790 struct timespec ts1, ts2, ts;
1791 int ret;
1793 ssz = sz >> 9;
1795 rnd_addr = mmc_test_capacity(test->card) / 4;
1796 range1 = rnd_addr / test->card->pref_erase;
1797 range2 = range1 / ssz;
1799 getnstimeofday(&ts1);
1800 for (cnt = 0; cnt < UINT_MAX; cnt++) {
1801 getnstimeofday(&ts2);
1802 ts = timespec_sub(ts2, ts1);
1803 if (ts.tv_sec >= 10)
1804 break;
1805 ea = mmc_test_rnd_num(range1);
1806 if (ea == last_ea)
1807 ea -= 1;
1808 last_ea = ea;
1809 dev_addr = rnd_addr + test->card->pref_erase * ea +
1810 ssz * mmc_test_rnd_num(range2);
1811 ret = mmc_test_area_io(test, sz, dev_addr, write, 0, 0);
1812 if (ret)
1813 return ret;
1815 if (print)
1816 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1817 return 0;
1820 static int mmc_test_random_perf(struct mmc_test_card *test, int write)
1822 struct mmc_test_area *t = &test->area;
1823 unsigned int next;
1824 unsigned long sz;
1825 int ret;
1827 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1829 * When writing, try to get more consistent results by running
1830 * the test twice with exactly the same I/O but outputting the
1831 * results only for the 2nd run.
1833 if (write) {
1834 next = rnd_next;
1835 ret = mmc_test_rnd_perf(test, write, 0, sz);
1836 if (ret)
1837 return ret;
1838 rnd_next = next;
1840 ret = mmc_test_rnd_perf(test, write, 1, sz);
1841 if (ret)
1842 return ret;
1844 sz = t->max_tfr;
1845 if (write) {
1846 next = rnd_next;
1847 ret = mmc_test_rnd_perf(test, write, 0, sz);
1848 if (ret)
1849 return ret;
1850 rnd_next = next;
1852 return mmc_test_rnd_perf(test, write, 1, sz);
1856 * Random read performance by transfer size.
1858 static int mmc_test_random_read_perf(struct mmc_test_card *test)
1860 return mmc_test_random_perf(test, 0);
1864 * Random write performance by transfer size.
1866 static int mmc_test_random_write_perf(struct mmc_test_card *test)
1868 return mmc_test_random_perf(test, 1);
1871 static int mmc_test_seq_perf(struct mmc_test_card *test, int write,
1872 unsigned int tot_sz, int max_scatter)
1874 struct mmc_test_area *t = &test->area;
1875 unsigned int dev_addr, i, cnt, sz, ssz;
1876 struct timespec ts1, ts2;
1877 int ret;
1879 sz = t->max_tfr;
1882 * In the case of a maximally scattered transfer, the maximum transfer
1883 * size is further limited by using PAGE_SIZE segments.
1885 if (max_scatter) {
1886 unsigned long max_tfr;
1888 if (t->max_seg_sz >= PAGE_SIZE)
1889 max_tfr = t->max_segs * PAGE_SIZE;
1890 else
1891 max_tfr = t->max_segs * t->max_seg_sz;
1892 if (sz > max_tfr)
1893 sz = max_tfr;
1896 ssz = sz >> 9;
1897 dev_addr = mmc_test_capacity(test->card) / 4;
1898 if (tot_sz > dev_addr << 9)
1899 tot_sz = dev_addr << 9;
1900 cnt = tot_sz / sz;
1901 dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
1903 getnstimeofday(&ts1);
1904 for (i = 0; i < cnt; i++) {
1905 ret = mmc_test_area_io(test, sz, dev_addr, write,
1906 max_scatter, 0);
1907 if (ret)
1908 return ret;
1909 dev_addr += ssz;
1911 getnstimeofday(&ts2);
1913 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1915 return 0;
1918 static int mmc_test_large_seq_perf(struct mmc_test_card *test, int write)
1920 int ret, i;
1922 for (i = 0; i < 10; i++) {
1923 ret = mmc_test_seq_perf(test, write, 10 * 1024 * 1024, 1);
1924 if (ret)
1925 return ret;
1927 for (i = 0; i < 5; i++) {
1928 ret = mmc_test_seq_perf(test, write, 100 * 1024 * 1024, 1);
1929 if (ret)
1930 return ret;
1932 for (i = 0; i < 3; i++) {
1933 ret = mmc_test_seq_perf(test, write, 1000 * 1024 * 1024, 1);
1934 if (ret)
1935 return ret;
1938 return ret;
1942 * Large sequential read performance.
1944 static int mmc_test_large_seq_read_perf(struct mmc_test_card *test)
1946 return mmc_test_large_seq_perf(test, 0);
1950 * Large sequential write performance.
1952 static int mmc_test_large_seq_write_perf(struct mmc_test_card *test)
1954 return mmc_test_large_seq_perf(test, 1);
1957 static const struct mmc_test_case mmc_test_cases[] = {
1959 .name = "Basic write (no data verification)",
1960 .run = mmc_test_basic_write,
1964 .name = "Basic read (no data verification)",
1965 .run = mmc_test_basic_read,
1969 .name = "Basic write (with data verification)",
1970 .prepare = mmc_test_prepare_write,
1971 .run = mmc_test_verify_write,
1972 .cleanup = mmc_test_cleanup,
1976 .name = "Basic read (with data verification)",
1977 .prepare = mmc_test_prepare_read,
1978 .run = mmc_test_verify_read,
1979 .cleanup = mmc_test_cleanup,
1983 .name = "Multi-block write",
1984 .prepare = mmc_test_prepare_write,
1985 .run = mmc_test_multi_write,
1986 .cleanup = mmc_test_cleanup,
1990 .name = "Multi-block read",
1991 .prepare = mmc_test_prepare_read,
1992 .run = mmc_test_multi_read,
1993 .cleanup = mmc_test_cleanup,
1997 .name = "Power of two block writes",
1998 .prepare = mmc_test_prepare_write,
1999 .run = mmc_test_pow2_write,
2000 .cleanup = mmc_test_cleanup,
2004 .name = "Power of two block reads",
2005 .prepare = mmc_test_prepare_read,
2006 .run = mmc_test_pow2_read,
2007 .cleanup = mmc_test_cleanup,
2011 .name = "Weird sized block writes",
2012 .prepare = mmc_test_prepare_write,
2013 .run = mmc_test_weird_write,
2014 .cleanup = mmc_test_cleanup,
2018 .name = "Weird sized block reads",
2019 .prepare = mmc_test_prepare_read,
2020 .run = mmc_test_weird_read,
2021 .cleanup = mmc_test_cleanup,
2025 .name = "Badly aligned write",
2026 .prepare = mmc_test_prepare_write,
2027 .run = mmc_test_align_write,
2028 .cleanup = mmc_test_cleanup,
2032 .name = "Badly aligned read",
2033 .prepare = mmc_test_prepare_read,
2034 .run = mmc_test_align_read,
2035 .cleanup = mmc_test_cleanup,
2039 .name = "Badly aligned multi-block write",
2040 .prepare = mmc_test_prepare_write,
2041 .run = mmc_test_align_multi_write,
2042 .cleanup = mmc_test_cleanup,
2046 .name = "Badly aligned multi-block read",
2047 .prepare = mmc_test_prepare_read,
2048 .run = mmc_test_align_multi_read,
2049 .cleanup = mmc_test_cleanup,
2053 .name = "Correct xfer_size at write (start failure)",
2054 .run = mmc_test_xfersize_write,
2058 .name = "Correct xfer_size at read (start failure)",
2059 .run = mmc_test_xfersize_read,
2063 .name = "Correct xfer_size at write (midway failure)",
2064 .run = mmc_test_multi_xfersize_write,
2068 .name = "Correct xfer_size at read (midway failure)",
2069 .run = mmc_test_multi_xfersize_read,
2072 #ifdef CONFIG_HIGHMEM
2075 .name = "Highmem write",
2076 .prepare = mmc_test_prepare_write,
2077 .run = mmc_test_write_high,
2078 .cleanup = mmc_test_cleanup,
2082 .name = "Highmem read",
2083 .prepare = mmc_test_prepare_read,
2084 .run = mmc_test_read_high,
2085 .cleanup = mmc_test_cleanup,
2089 .name = "Multi-block highmem write",
2090 .prepare = mmc_test_prepare_write,
2091 .run = mmc_test_multi_write_high,
2092 .cleanup = mmc_test_cleanup,
2096 .name = "Multi-block highmem read",
2097 .prepare = mmc_test_prepare_read,
2098 .run = mmc_test_multi_read_high,
2099 .cleanup = mmc_test_cleanup,
2102 #else
2105 .name = "Highmem write",
2106 .run = mmc_test_no_highmem,
2110 .name = "Highmem read",
2111 .run = mmc_test_no_highmem,
2115 .name = "Multi-block highmem write",
2116 .run = mmc_test_no_highmem,
2120 .name = "Multi-block highmem read",
2121 .run = mmc_test_no_highmem,
2124 #endif /* CONFIG_HIGHMEM */
2127 .name = "Best-case read performance",
2128 .prepare = mmc_test_area_prepare_fill,
2129 .run = mmc_test_best_read_performance,
2130 .cleanup = mmc_test_area_cleanup,
2134 .name = "Best-case write performance",
2135 .prepare = mmc_test_area_prepare_erase,
2136 .run = mmc_test_best_write_performance,
2137 .cleanup = mmc_test_area_cleanup,
2141 .name = "Best-case read performance into scattered pages",
2142 .prepare = mmc_test_area_prepare_fill,
2143 .run = mmc_test_best_read_perf_max_scatter,
2144 .cleanup = mmc_test_area_cleanup,
2148 .name = "Best-case write performance from scattered pages",
2149 .prepare = mmc_test_area_prepare_erase,
2150 .run = mmc_test_best_write_perf_max_scatter,
2151 .cleanup = mmc_test_area_cleanup,
2155 .name = "Single read performance by transfer size",
2156 .prepare = mmc_test_area_prepare_fill,
2157 .run = mmc_test_profile_read_perf,
2158 .cleanup = mmc_test_area_cleanup,
2162 .name = "Single write performance by transfer size",
2163 .prepare = mmc_test_area_prepare,
2164 .run = mmc_test_profile_write_perf,
2165 .cleanup = mmc_test_area_cleanup,
2169 .name = "Single trim performance by transfer size",
2170 .prepare = mmc_test_area_prepare_fill,
2171 .run = mmc_test_profile_trim_perf,
2172 .cleanup = mmc_test_area_cleanup,
2176 .name = "Consecutive read performance by transfer size",
2177 .prepare = mmc_test_area_prepare_fill,
2178 .run = mmc_test_profile_seq_read_perf,
2179 .cleanup = mmc_test_area_cleanup,
2183 .name = "Consecutive write performance by transfer size",
2184 .prepare = mmc_test_area_prepare,
2185 .run = mmc_test_profile_seq_write_perf,
2186 .cleanup = mmc_test_area_cleanup,
2190 .name = "Consecutive trim performance by transfer size",
2191 .prepare = mmc_test_area_prepare,
2192 .run = mmc_test_profile_seq_trim_perf,
2193 .cleanup = mmc_test_area_cleanup,
2197 .name = "Random read performance by transfer size",
2198 .prepare = mmc_test_area_prepare,
2199 .run = mmc_test_random_read_perf,
2200 .cleanup = mmc_test_area_cleanup,
2204 .name = "Random write performance by transfer size",
2205 .prepare = mmc_test_area_prepare,
2206 .run = mmc_test_random_write_perf,
2207 .cleanup = mmc_test_area_cleanup,
2211 .name = "Large sequential read into scattered pages",
2212 .prepare = mmc_test_area_prepare,
2213 .run = mmc_test_large_seq_read_perf,
2214 .cleanup = mmc_test_area_cleanup,
2218 .name = "Large sequential write from scattered pages",
2219 .prepare = mmc_test_area_prepare,
2220 .run = mmc_test_large_seq_write_perf,
2221 .cleanup = mmc_test_area_cleanup,
2226 static DEFINE_MUTEX(mmc_test_lock);
2228 static LIST_HEAD(mmc_test_result);
2230 static void mmc_test_run(struct mmc_test_card *test, int testcase)
2232 int i, ret;
2234 printk(KERN_INFO "%s: Starting tests of card %s...\n",
2235 mmc_hostname(test->card->host), mmc_card_id(test->card));
2237 mmc_claim_host(test->card->host);
2239 for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
2240 struct mmc_test_general_result *gr;
2242 if (testcase && ((i + 1) != testcase))
2243 continue;
2245 printk(KERN_INFO "%s: Test case %d. %s...\n",
2246 mmc_hostname(test->card->host), i + 1,
2247 mmc_test_cases[i].name);
2249 if (mmc_test_cases[i].prepare) {
2250 ret = mmc_test_cases[i].prepare(test);
2251 if (ret) {
2252 printk(KERN_INFO "%s: Result: Prepare "
2253 "stage failed! (%d)\n",
2254 mmc_hostname(test->card->host),
2255 ret);
2256 continue;
2260 gr = kzalloc(sizeof(struct mmc_test_general_result),
2261 GFP_KERNEL);
2262 if (gr) {
2263 INIT_LIST_HEAD(&gr->tr_lst);
2265 /* Assign data what we know already */
2266 gr->card = test->card;
2267 gr->testcase = i;
2269 /* Append container to global one */
2270 list_add_tail(&gr->link, &mmc_test_result);
2273 * Save the pointer to created container in our private
2274 * structure.
2276 test->gr = gr;
2279 ret = mmc_test_cases[i].run(test);
2280 switch (ret) {
2281 case RESULT_OK:
2282 printk(KERN_INFO "%s: Result: OK\n",
2283 mmc_hostname(test->card->host));
2284 break;
2285 case RESULT_FAIL:
2286 printk(KERN_INFO "%s: Result: FAILED\n",
2287 mmc_hostname(test->card->host));
2288 break;
2289 case RESULT_UNSUP_HOST:
2290 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2291 "(by host)\n",
2292 mmc_hostname(test->card->host));
2293 break;
2294 case RESULT_UNSUP_CARD:
2295 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2296 "(by card)\n",
2297 mmc_hostname(test->card->host));
2298 break;
2299 default:
2300 printk(KERN_INFO "%s: Result: ERROR (%d)\n",
2301 mmc_hostname(test->card->host), ret);
2304 /* Save the result */
2305 if (gr)
2306 gr->result = ret;
2308 if (mmc_test_cases[i].cleanup) {
2309 ret = mmc_test_cases[i].cleanup(test);
2310 if (ret) {
2311 printk(KERN_INFO "%s: Warning: Cleanup "
2312 "stage failed! (%d)\n",
2313 mmc_hostname(test->card->host),
2314 ret);
2319 mmc_release_host(test->card->host);
2321 printk(KERN_INFO "%s: Tests completed.\n",
2322 mmc_hostname(test->card->host));
2325 static void mmc_test_free_result(struct mmc_card *card)
2327 struct mmc_test_general_result *gr, *grs;
2329 mutex_lock(&mmc_test_lock);
2331 list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
2332 struct mmc_test_transfer_result *tr, *trs;
2334 if (card && gr->card != card)
2335 continue;
2337 list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
2338 list_del(&tr->link);
2339 kfree(tr);
2342 list_del(&gr->link);
2343 kfree(gr);
2346 mutex_unlock(&mmc_test_lock);
2349 static LIST_HEAD(mmc_test_file_test);
2351 static int mtf_test_show(struct seq_file *sf, void *data)
2353 struct mmc_card *card = (struct mmc_card *)sf->private;
2354 struct mmc_test_general_result *gr;
2356 mutex_lock(&mmc_test_lock);
2358 list_for_each_entry(gr, &mmc_test_result, link) {
2359 struct mmc_test_transfer_result *tr;
2361 if (gr->card != card)
2362 continue;
2364 seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
2366 list_for_each_entry(tr, &gr->tr_lst, link) {
2367 seq_printf(sf, "%u %d %lu.%09lu %u %u.%02u\n",
2368 tr->count, tr->sectors,
2369 (unsigned long)tr->ts.tv_sec,
2370 (unsigned long)tr->ts.tv_nsec,
2371 tr->rate, tr->iops / 100, tr->iops % 100);
2375 mutex_unlock(&mmc_test_lock);
2377 return 0;
2380 static int mtf_test_open(struct inode *inode, struct file *file)
2382 return single_open(file, mtf_test_show, inode->i_private);
2385 static ssize_t mtf_test_write(struct file *file, const char __user *buf,
2386 size_t count, loff_t *pos)
2388 struct seq_file *sf = (struct seq_file *)file->private_data;
2389 struct mmc_card *card = (struct mmc_card *)sf->private;
2390 struct mmc_test_card *test;
2391 char lbuf[12];
2392 long testcase;
2394 if (count >= sizeof(lbuf))
2395 return -EINVAL;
2397 if (copy_from_user(lbuf, buf, count))
2398 return -EFAULT;
2399 lbuf[count] = '\0';
2401 if (strict_strtol(lbuf, 10, &testcase))
2402 return -EINVAL;
2404 test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
2405 if (!test)
2406 return -ENOMEM;
2409 * Remove all test cases associated with given card. Thus we have only
2410 * actual data of the last run.
2412 mmc_test_free_result(card);
2414 test->card = card;
2416 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
2417 #ifdef CONFIG_HIGHMEM
2418 test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
2419 #endif
2421 #ifdef CONFIG_HIGHMEM
2422 if (test->buffer && test->highmem) {
2423 #else
2424 if (test->buffer) {
2425 #endif
2426 mutex_lock(&mmc_test_lock);
2427 mmc_test_run(test, testcase);
2428 mutex_unlock(&mmc_test_lock);
2431 #ifdef CONFIG_HIGHMEM
2432 __free_pages(test->highmem, BUFFER_ORDER);
2433 #endif
2434 kfree(test->buffer);
2435 kfree(test);
2437 return count;
2440 static const struct file_operations mmc_test_fops_test = {
2441 .open = mtf_test_open,
2442 .read = seq_read,
2443 .write = mtf_test_write,
2444 .llseek = seq_lseek,
2445 .release = single_release,
2448 static void mmc_test_free_file_test(struct mmc_card *card)
2450 struct mmc_test_dbgfs_file *df, *dfs;
2452 mutex_lock(&mmc_test_lock);
2454 list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
2455 if (card && df->card != card)
2456 continue;
2457 debugfs_remove(df->file);
2458 list_del(&df->link);
2459 kfree(df);
2462 mutex_unlock(&mmc_test_lock);
2465 static int mmc_test_register_file_test(struct mmc_card *card)
2467 struct dentry *file = NULL;
2468 struct mmc_test_dbgfs_file *df;
2469 int ret = 0;
2471 mutex_lock(&mmc_test_lock);
2473 if (card->debugfs_root)
2474 file = debugfs_create_file("test", S_IWUSR | S_IRUGO,
2475 card->debugfs_root, card, &mmc_test_fops_test);
2477 if (IS_ERR_OR_NULL(file)) {
2478 dev_err(&card->dev,
2479 "Can't create file. Perhaps debugfs is disabled.\n");
2480 ret = -ENODEV;
2481 goto err;
2484 df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
2485 if (!df) {
2486 debugfs_remove(file);
2487 dev_err(&card->dev,
2488 "Can't allocate memory for internal usage.\n");
2489 ret = -ENOMEM;
2490 goto err;
2493 df->card = card;
2494 df->file = file;
2496 list_add(&df->link, &mmc_test_file_test);
2498 err:
2499 mutex_unlock(&mmc_test_lock);
2501 return ret;
2504 static int mmc_test_probe(struct mmc_card *card)
2506 int ret;
2508 if (!mmc_card_mmc(card) && !mmc_card_sd(card))
2509 return -ENODEV;
2511 ret = mmc_test_register_file_test(card);
2512 if (ret)
2513 return ret;
2515 dev_info(&card->dev, "Card claimed for testing.\n");
2517 return 0;
2520 static void mmc_test_remove(struct mmc_card *card)
2522 mmc_test_free_result(card);
2523 mmc_test_free_file_test(card);
2526 static struct mmc_driver mmc_driver = {
2527 .drv = {
2528 .name = "mmc_test",
2530 .probe = mmc_test_probe,
2531 .remove = mmc_test_remove,
2534 static int __init mmc_test_init(void)
2536 return mmc_register_driver(&mmc_driver);
2539 static void __exit mmc_test_exit(void)
2541 /* Clear stalled data if card is still plugged */
2542 mmc_test_free_result(NULL);
2543 mmc_test_free_file_test(NULL);
2545 mmc_unregister_driver(&mmc_driver);
2548 module_init(mmc_test_init);
2549 module_exit(mmc_test_exit);
2551 MODULE_LICENSE("GPL");
2552 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
2553 MODULE_AUTHOR("Pierre Ossman");