mmc: mmc_test: Extend "Badly aligned" tests for 8-byte alignment
[linux-2.6/btrfs-unstable.git] / drivers / mmc / card / mmc_test.c
blob0a7430f94d2961a163fa647dd109b221b9d25286
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>
17 #include <linux/device.h>
19 #include <linux/scatterlist.h>
20 #include <linux/swap.h> /* For nr_free_buffer_pages() */
21 #include <linux/list.h>
23 #include <linux/debugfs.h>
24 #include <linux/uaccess.h>
25 #include <linux/seq_file.h>
26 #include <linux/module.h>
28 #define RESULT_OK 0
29 #define RESULT_FAIL 1
30 #define RESULT_UNSUP_HOST 2
31 #define RESULT_UNSUP_CARD 3
33 #define BUFFER_ORDER 2
34 #define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
36 #define TEST_ALIGN_END 8
39 * Limit the test area size to the maximum MMC HC erase group size. Note that
40 * the maximum SD allocation unit size is just 4MiB.
42 #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
44 /**
45 * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
46 * @page: first page in the allocation
47 * @order: order of the number of pages allocated
49 struct mmc_test_pages {
50 struct page *page;
51 unsigned int order;
54 /**
55 * struct mmc_test_mem - allocated memory.
56 * @arr: array of allocations
57 * @cnt: number of allocations
59 struct mmc_test_mem {
60 struct mmc_test_pages *arr;
61 unsigned int cnt;
64 /**
65 * struct mmc_test_area - information for performance tests.
66 * @max_sz: test area size (in bytes)
67 * @dev_addr: address on card at which to do performance tests
68 * @max_tfr: maximum transfer size allowed by driver (in bytes)
69 * @max_segs: maximum segments allowed by driver in scatterlist @sg
70 * @max_seg_sz: maximum segment size allowed by driver
71 * @blocks: number of (512 byte) blocks currently mapped by @sg
72 * @sg_len: length of currently mapped scatterlist @sg
73 * @mem: allocated memory
74 * @sg: scatterlist
76 struct mmc_test_area {
77 unsigned long max_sz;
78 unsigned int dev_addr;
79 unsigned int max_tfr;
80 unsigned int max_segs;
81 unsigned int max_seg_sz;
82 unsigned int blocks;
83 unsigned int sg_len;
84 struct mmc_test_mem *mem;
85 struct scatterlist *sg;
88 /**
89 * struct mmc_test_transfer_result - transfer results for performance tests.
90 * @link: double-linked list
91 * @count: amount of group of sectors to check
92 * @sectors: amount of sectors to check in one group
93 * @ts: time values of transfer
94 * @rate: calculated transfer rate
95 * @iops: I/O operations per second (times 100)
97 struct mmc_test_transfer_result {
98 struct list_head link;
99 unsigned int count;
100 unsigned int sectors;
101 struct timespec ts;
102 unsigned int rate;
103 unsigned int iops;
107 * struct mmc_test_general_result - results for tests.
108 * @link: double-linked list
109 * @card: card under test
110 * @testcase: number of test case
111 * @result: result of test run
112 * @tr_lst: transfer measurements if any as mmc_test_transfer_result
114 struct mmc_test_general_result {
115 struct list_head link;
116 struct mmc_card *card;
117 int testcase;
118 int result;
119 struct list_head tr_lst;
123 * struct mmc_test_dbgfs_file - debugfs related file.
124 * @link: double-linked list
125 * @card: card under test
126 * @file: file created under debugfs
128 struct mmc_test_dbgfs_file {
129 struct list_head link;
130 struct mmc_card *card;
131 struct dentry *file;
135 * struct mmc_test_card - test information.
136 * @card: card under test
137 * @scratch: transfer buffer
138 * @buffer: transfer buffer
139 * @highmem: buffer for highmem tests
140 * @area: information for performance tests
141 * @gr: pointer to results of current testcase
143 struct mmc_test_card {
144 struct mmc_card *card;
146 u8 scratch[BUFFER_SIZE];
147 u8 *buffer;
148 #ifdef CONFIG_HIGHMEM
149 struct page *highmem;
150 #endif
151 struct mmc_test_area area;
152 struct mmc_test_general_result *gr;
155 enum mmc_test_prep_media {
156 MMC_TEST_PREP_NONE = 0,
157 MMC_TEST_PREP_WRITE_FULL = 1 << 0,
158 MMC_TEST_PREP_ERASE = 1 << 1,
161 struct mmc_test_multiple_rw {
162 unsigned int *sg_len;
163 unsigned int *bs;
164 unsigned int len;
165 unsigned int size;
166 bool do_write;
167 bool do_nonblock_req;
168 enum mmc_test_prep_media prepare;
171 struct mmc_test_async_req {
172 struct mmc_async_req areq;
173 struct mmc_test_card *test;
176 /*******************************************************************/
177 /* General helper functions */
178 /*******************************************************************/
181 * Configure correct block size in card
183 static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
185 return mmc_set_blocklen(test->card, size);
189 * Fill in the mmc_request structure given a set of transfer parameters.
191 static void mmc_test_prepare_mrq(struct mmc_test_card *test,
192 struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
193 unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
195 BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
197 if (blocks > 1) {
198 mrq->cmd->opcode = write ?
199 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
200 } else {
201 mrq->cmd->opcode = write ?
202 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
205 mrq->cmd->arg = dev_addr;
206 if (!mmc_card_blockaddr(test->card))
207 mrq->cmd->arg <<= 9;
209 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
211 if (blocks == 1)
212 mrq->stop = NULL;
213 else {
214 mrq->stop->opcode = MMC_STOP_TRANSMISSION;
215 mrq->stop->arg = 0;
216 mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
219 mrq->data->blksz = blksz;
220 mrq->data->blocks = blocks;
221 mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
222 mrq->data->sg = sg;
223 mrq->data->sg_len = sg_len;
225 mmc_set_data_timeout(mrq->data, test->card);
228 static int mmc_test_busy(struct mmc_command *cmd)
230 return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
231 (R1_CURRENT_STATE(cmd->resp[0]) == R1_STATE_PRG);
235 * Wait for the card to finish the busy state
237 static int mmc_test_wait_busy(struct mmc_test_card *test)
239 int ret, busy;
240 struct mmc_command cmd = {0};
242 busy = 0;
243 do {
244 memset(&cmd, 0, sizeof(struct mmc_command));
246 cmd.opcode = MMC_SEND_STATUS;
247 cmd.arg = test->card->rca << 16;
248 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
250 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
251 if (ret)
252 break;
254 if (!busy && mmc_test_busy(&cmd)) {
255 busy = 1;
256 if (test->card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
257 pr_info("%s: Warning: Host did not "
258 "wait for busy state to end.\n",
259 mmc_hostname(test->card->host));
261 } while (mmc_test_busy(&cmd));
263 return ret;
267 * Transfer a single sector of kernel addressable data
269 static int mmc_test_buffer_transfer(struct mmc_test_card *test,
270 u8 *buffer, unsigned addr, unsigned blksz, int write)
272 int ret;
274 struct mmc_request mrq = {0};
275 struct mmc_command cmd = {0};
276 struct mmc_command stop = {0};
277 struct mmc_data data = {0};
279 struct scatterlist sg;
281 mrq.cmd = &cmd;
282 mrq.data = &data;
283 mrq.stop = &stop;
285 sg_init_one(&sg, buffer, blksz);
287 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
289 mmc_wait_for_req(test->card->host, &mrq);
291 if (cmd.error)
292 return cmd.error;
293 if (data.error)
294 return data.error;
296 ret = mmc_test_wait_busy(test);
297 if (ret)
298 return ret;
300 return 0;
303 static void mmc_test_free_mem(struct mmc_test_mem *mem)
305 if (!mem)
306 return;
307 while (mem->cnt--)
308 __free_pages(mem->arr[mem->cnt].page,
309 mem->arr[mem->cnt].order);
310 kfree(mem->arr);
311 kfree(mem);
315 * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
316 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
317 * not exceed a maximum number of segments and try not to make segments much
318 * bigger than maximum segment size.
320 static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
321 unsigned long max_sz,
322 unsigned int max_segs,
323 unsigned int max_seg_sz)
325 unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
326 unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
327 unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
328 unsigned long page_cnt = 0;
329 unsigned long limit = nr_free_buffer_pages() >> 4;
330 struct mmc_test_mem *mem;
332 if (max_page_cnt > limit)
333 max_page_cnt = limit;
334 if (min_page_cnt > max_page_cnt)
335 min_page_cnt = max_page_cnt;
337 if (max_seg_page_cnt > max_page_cnt)
338 max_seg_page_cnt = max_page_cnt;
340 if (max_segs > max_page_cnt)
341 max_segs = max_page_cnt;
343 mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
344 if (!mem)
345 return NULL;
347 mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_segs,
348 GFP_KERNEL);
349 if (!mem->arr)
350 goto out_free;
352 while (max_page_cnt) {
353 struct page *page;
354 unsigned int order;
355 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
356 __GFP_NORETRY;
358 order = get_order(max_seg_page_cnt << PAGE_SHIFT);
359 while (1) {
360 page = alloc_pages(flags, order);
361 if (page || !order)
362 break;
363 order -= 1;
365 if (!page) {
366 if (page_cnt < min_page_cnt)
367 goto out_free;
368 break;
370 mem->arr[mem->cnt].page = page;
371 mem->arr[mem->cnt].order = order;
372 mem->cnt += 1;
373 if (max_page_cnt <= (1UL << order))
374 break;
375 max_page_cnt -= 1UL << order;
376 page_cnt += 1UL << order;
377 if (mem->cnt >= max_segs) {
378 if (page_cnt < min_page_cnt)
379 goto out_free;
380 break;
384 return mem;
386 out_free:
387 mmc_test_free_mem(mem);
388 return NULL;
392 * Map memory into a scatterlist. Optionally allow the same memory to be
393 * mapped more than once.
395 static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long size,
396 struct scatterlist *sglist, int repeat,
397 unsigned int max_segs, unsigned int max_seg_sz,
398 unsigned int *sg_len, int min_sg_len)
400 struct scatterlist *sg = NULL;
401 unsigned int i;
402 unsigned long sz = size;
404 sg_init_table(sglist, max_segs);
405 if (min_sg_len > max_segs)
406 min_sg_len = max_segs;
408 *sg_len = 0;
409 do {
410 for (i = 0; i < mem->cnt; i++) {
411 unsigned long len = PAGE_SIZE << mem->arr[i].order;
413 if (min_sg_len && (size / min_sg_len < len))
414 len = ALIGN(size / min_sg_len, 512);
415 if (len > sz)
416 len = sz;
417 if (len > max_seg_sz)
418 len = max_seg_sz;
419 if (sg)
420 sg = sg_next(sg);
421 else
422 sg = sglist;
423 if (!sg)
424 return -EINVAL;
425 sg_set_page(sg, mem->arr[i].page, len, 0);
426 sz -= len;
427 *sg_len += 1;
428 if (!sz)
429 break;
431 } while (sz && repeat);
433 if (sz)
434 return -EINVAL;
436 if (sg)
437 sg_mark_end(sg);
439 return 0;
443 * Map memory into a scatterlist so that no pages are contiguous. Allow the
444 * same memory to be mapped more than once.
446 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
447 unsigned long sz,
448 struct scatterlist *sglist,
449 unsigned int max_segs,
450 unsigned int max_seg_sz,
451 unsigned int *sg_len)
453 struct scatterlist *sg = NULL;
454 unsigned int i = mem->cnt, cnt;
455 unsigned long len;
456 void *base, *addr, *last_addr = NULL;
458 sg_init_table(sglist, max_segs);
460 *sg_len = 0;
461 while (sz) {
462 base = page_address(mem->arr[--i].page);
463 cnt = 1 << mem->arr[i].order;
464 while (sz && cnt) {
465 addr = base + PAGE_SIZE * --cnt;
466 if (last_addr && last_addr + PAGE_SIZE == addr)
467 continue;
468 last_addr = addr;
469 len = PAGE_SIZE;
470 if (len > max_seg_sz)
471 len = max_seg_sz;
472 if (len > sz)
473 len = sz;
474 if (sg)
475 sg = sg_next(sg);
476 else
477 sg = sglist;
478 if (!sg)
479 return -EINVAL;
480 sg_set_page(sg, virt_to_page(addr), len, 0);
481 sz -= len;
482 *sg_len += 1;
484 if (i == 0)
485 i = mem->cnt;
488 if (sg)
489 sg_mark_end(sg);
491 return 0;
495 * Calculate transfer rate in bytes per second.
497 static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
499 uint64_t ns;
501 ns = ts->tv_sec;
502 ns *= 1000000000;
503 ns += ts->tv_nsec;
505 bytes *= 1000000000;
507 while (ns > UINT_MAX) {
508 bytes >>= 1;
509 ns >>= 1;
512 if (!ns)
513 return 0;
515 do_div(bytes, (uint32_t)ns);
517 return bytes;
521 * Save transfer results for future usage
523 static void mmc_test_save_transfer_result(struct mmc_test_card *test,
524 unsigned int count, unsigned int sectors, struct timespec ts,
525 unsigned int rate, unsigned int iops)
527 struct mmc_test_transfer_result *tr;
529 if (!test->gr)
530 return;
532 tr = kmalloc(sizeof(struct mmc_test_transfer_result), GFP_KERNEL);
533 if (!tr)
534 return;
536 tr->count = count;
537 tr->sectors = sectors;
538 tr->ts = ts;
539 tr->rate = rate;
540 tr->iops = iops;
542 list_add_tail(&tr->link, &test->gr->tr_lst);
546 * Print the transfer rate.
548 static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
549 struct timespec *ts1, struct timespec *ts2)
551 unsigned int rate, iops, sectors = bytes >> 9;
552 struct timespec ts;
554 ts = timespec_sub(*ts2, *ts1);
556 rate = mmc_test_rate(bytes, &ts);
557 iops = mmc_test_rate(100, &ts); /* I/O ops per sec x 100 */
559 pr_info("%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
560 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
561 mmc_hostname(test->card->host), sectors, sectors >> 1,
562 (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
563 (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024,
564 iops / 100, iops % 100);
566 mmc_test_save_transfer_result(test, 1, sectors, ts, rate, iops);
570 * Print the average transfer rate.
572 static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
573 unsigned int count, struct timespec *ts1,
574 struct timespec *ts2)
576 unsigned int rate, iops, sectors = bytes >> 9;
577 uint64_t tot = bytes * count;
578 struct timespec ts;
580 ts = timespec_sub(*ts2, *ts1);
582 rate = mmc_test_rate(tot, &ts);
583 iops = mmc_test_rate(count * 100, &ts); /* I/O ops per sec x 100 */
585 pr_info("%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
586 "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
587 "%u.%02u IOPS, sg_len %d)\n",
588 mmc_hostname(test->card->host), count, sectors, count,
589 sectors >> 1, (sectors & 1 ? ".5" : ""),
590 (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
591 rate / 1000, rate / 1024, iops / 100, iops % 100,
592 test->area.sg_len);
594 mmc_test_save_transfer_result(test, count, sectors, ts, rate, iops);
598 * Return the card size in sectors.
600 static unsigned int mmc_test_capacity(struct mmc_card *card)
602 if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
603 return card->ext_csd.sectors;
604 else
605 return card->csd.capacity << (card->csd.read_blkbits - 9);
608 /*******************************************************************/
609 /* Test preparation and cleanup */
610 /*******************************************************************/
613 * Fill the first couple of sectors of the card with known data
614 * so that bad reads/writes can be detected
616 static int __mmc_test_prepare(struct mmc_test_card *test, int write)
618 int ret, i;
620 ret = mmc_test_set_blksize(test, 512);
621 if (ret)
622 return ret;
624 if (write)
625 memset(test->buffer, 0xDF, 512);
626 else {
627 for (i = 0;i < 512;i++)
628 test->buffer[i] = i;
631 for (i = 0;i < BUFFER_SIZE / 512;i++) {
632 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
633 if (ret)
634 return ret;
637 return 0;
640 static int mmc_test_prepare_write(struct mmc_test_card *test)
642 return __mmc_test_prepare(test, 1);
645 static int mmc_test_prepare_read(struct mmc_test_card *test)
647 return __mmc_test_prepare(test, 0);
650 static int mmc_test_cleanup(struct mmc_test_card *test)
652 int ret, i;
654 ret = mmc_test_set_blksize(test, 512);
655 if (ret)
656 return ret;
658 memset(test->buffer, 0, 512);
660 for (i = 0;i < BUFFER_SIZE / 512;i++) {
661 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
662 if (ret)
663 return ret;
666 return 0;
669 /*******************************************************************/
670 /* Test execution helpers */
671 /*******************************************************************/
674 * Modifies the mmc_request to perform the "short transfer" tests
676 static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
677 struct mmc_request *mrq, int write)
679 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
681 if (mrq->data->blocks > 1) {
682 mrq->cmd->opcode = write ?
683 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
684 mrq->stop = NULL;
685 } else {
686 mrq->cmd->opcode = MMC_SEND_STATUS;
687 mrq->cmd->arg = test->card->rca << 16;
692 * Checks that a normal transfer didn't have any errors
694 static int mmc_test_check_result(struct mmc_test_card *test,
695 struct mmc_request *mrq)
697 int ret;
699 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
701 ret = 0;
703 if (!ret && mrq->cmd->error)
704 ret = mrq->cmd->error;
705 if (!ret && mrq->data->error)
706 ret = mrq->data->error;
707 if (!ret && mrq->stop && mrq->stop->error)
708 ret = mrq->stop->error;
709 if (!ret && mrq->data->bytes_xfered !=
710 mrq->data->blocks * mrq->data->blksz)
711 ret = RESULT_FAIL;
713 if (ret == -EINVAL)
714 ret = RESULT_UNSUP_HOST;
716 return ret;
719 static int mmc_test_check_result_async(struct mmc_card *card,
720 struct mmc_async_req *areq)
722 struct mmc_test_async_req *test_async =
723 container_of(areq, struct mmc_test_async_req, areq);
725 mmc_test_wait_busy(test_async->test);
727 return mmc_test_check_result(test_async->test, areq->mrq);
731 * Checks that a "short transfer" behaved as expected
733 static int mmc_test_check_broken_result(struct mmc_test_card *test,
734 struct mmc_request *mrq)
736 int ret;
738 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
740 ret = 0;
742 if (!ret && mrq->cmd->error)
743 ret = mrq->cmd->error;
744 if (!ret && mrq->data->error == 0)
745 ret = RESULT_FAIL;
746 if (!ret && mrq->data->error != -ETIMEDOUT)
747 ret = mrq->data->error;
748 if (!ret && mrq->stop && mrq->stop->error)
749 ret = mrq->stop->error;
750 if (mrq->data->blocks > 1) {
751 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
752 ret = RESULT_FAIL;
753 } else {
754 if (!ret && mrq->data->bytes_xfered > 0)
755 ret = RESULT_FAIL;
758 if (ret == -EINVAL)
759 ret = RESULT_UNSUP_HOST;
761 return ret;
765 * Tests nonblock transfer with certain parameters
767 static void mmc_test_nonblock_reset(struct mmc_request *mrq,
768 struct mmc_command *cmd,
769 struct mmc_command *stop,
770 struct mmc_data *data)
772 memset(mrq, 0, sizeof(struct mmc_request));
773 memset(cmd, 0, sizeof(struct mmc_command));
774 memset(data, 0, sizeof(struct mmc_data));
775 memset(stop, 0, sizeof(struct mmc_command));
777 mrq->cmd = cmd;
778 mrq->data = data;
779 mrq->stop = stop;
781 static int mmc_test_nonblock_transfer(struct mmc_test_card *test,
782 struct scatterlist *sg, unsigned sg_len,
783 unsigned dev_addr, unsigned blocks,
784 unsigned blksz, int write, int count)
786 struct mmc_request mrq1;
787 struct mmc_command cmd1;
788 struct mmc_command stop1;
789 struct mmc_data data1;
791 struct mmc_request mrq2;
792 struct mmc_command cmd2;
793 struct mmc_command stop2;
794 struct mmc_data data2;
796 struct mmc_test_async_req test_areq[2];
797 struct mmc_async_req *done_areq;
798 struct mmc_async_req *cur_areq = &test_areq[0].areq;
799 struct mmc_async_req *other_areq = &test_areq[1].areq;
800 int i;
801 int ret;
803 test_areq[0].test = test;
804 test_areq[1].test = test;
806 mmc_test_nonblock_reset(&mrq1, &cmd1, &stop1, &data1);
807 mmc_test_nonblock_reset(&mrq2, &cmd2, &stop2, &data2);
809 cur_areq->mrq = &mrq1;
810 cur_areq->err_check = mmc_test_check_result_async;
811 other_areq->mrq = &mrq2;
812 other_areq->err_check = mmc_test_check_result_async;
814 for (i = 0; i < count; i++) {
815 mmc_test_prepare_mrq(test, cur_areq->mrq, sg, sg_len, dev_addr,
816 blocks, blksz, write);
817 done_areq = mmc_start_req(test->card->host, cur_areq, &ret);
819 if (ret || (!done_areq && i > 0))
820 goto err;
822 if (done_areq) {
823 if (done_areq->mrq == &mrq2)
824 mmc_test_nonblock_reset(&mrq2, &cmd2,
825 &stop2, &data2);
826 else
827 mmc_test_nonblock_reset(&mrq1, &cmd1,
828 &stop1, &data1);
830 done_areq = cur_areq;
831 cur_areq = other_areq;
832 other_areq = done_areq;
833 dev_addr += blocks;
836 done_areq = mmc_start_req(test->card->host, NULL, &ret);
838 return ret;
839 err:
840 return ret;
844 * Tests a basic transfer with certain parameters
846 static int mmc_test_simple_transfer(struct mmc_test_card *test,
847 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
848 unsigned blocks, unsigned blksz, int write)
850 struct mmc_request mrq = {0};
851 struct mmc_command cmd = {0};
852 struct mmc_command stop = {0};
853 struct mmc_data data = {0};
855 mrq.cmd = &cmd;
856 mrq.data = &data;
857 mrq.stop = &stop;
859 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
860 blocks, blksz, write);
862 mmc_wait_for_req(test->card->host, &mrq);
864 mmc_test_wait_busy(test);
866 return mmc_test_check_result(test, &mrq);
870 * Tests a transfer where the card will fail completely or partly
872 static int mmc_test_broken_transfer(struct mmc_test_card *test,
873 unsigned blocks, unsigned blksz, int write)
875 struct mmc_request mrq = {0};
876 struct mmc_command cmd = {0};
877 struct mmc_command stop = {0};
878 struct mmc_data data = {0};
880 struct scatterlist sg;
882 mrq.cmd = &cmd;
883 mrq.data = &data;
884 mrq.stop = &stop;
886 sg_init_one(&sg, test->buffer, blocks * blksz);
888 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
889 mmc_test_prepare_broken_mrq(test, &mrq, write);
891 mmc_wait_for_req(test->card->host, &mrq);
893 mmc_test_wait_busy(test);
895 return mmc_test_check_broken_result(test, &mrq);
899 * Does a complete transfer test where data is also validated
901 * Note: mmc_test_prepare() must have been done before this call
903 static int mmc_test_transfer(struct mmc_test_card *test,
904 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
905 unsigned blocks, unsigned blksz, int write)
907 int ret, i;
908 unsigned long flags;
910 if (write) {
911 for (i = 0;i < blocks * blksz;i++)
912 test->scratch[i] = i;
913 } else {
914 memset(test->scratch, 0, BUFFER_SIZE);
916 local_irq_save(flags);
917 sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
918 local_irq_restore(flags);
920 ret = mmc_test_set_blksize(test, blksz);
921 if (ret)
922 return ret;
924 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
925 blocks, blksz, write);
926 if (ret)
927 return ret;
929 if (write) {
930 int sectors;
932 ret = mmc_test_set_blksize(test, 512);
933 if (ret)
934 return ret;
936 sectors = (blocks * blksz + 511) / 512;
937 if ((sectors * 512) == (blocks * blksz))
938 sectors++;
940 if ((sectors * 512) > BUFFER_SIZE)
941 return -EINVAL;
943 memset(test->buffer, 0, sectors * 512);
945 for (i = 0;i < sectors;i++) {
946 ret = mmc_test_buffer_transfer(test,
947 test->buffer + i * 512,
948 dev_addr + i, 512, 0);
949 if (ret)
950 return ret;
953 for (i = 0;i < blocks * blksz;i++) {
954 if (test->buffer[i] != (u8)i)
955 return RESULT_FAIL;
958 for (;i < sectors * 512;i++) {
959 if (test->buffer[i] != 0xDF)
960 return RESULT_FAIL;
962 } else {
963 local_irq_save(flags);
964 sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
965 local_irq_restore(flags);
966 for (i = 0;i < blocks * blksz;i++) {
967 if (test->scratch[i] != (u8)i)
968 return RESULT_FAIL;
972 return 0;
975 /*******************************************************************/
976 /* Tests */
977 /*******************************************************************/
979 struct mmc_test_case {
980 const char *name;
982 int (*prepare)(struct mmc_test_card *);
983 int (*run)(struct mmc_test_card *);
984 int (*cleanup)(struct mmc_test_card *);
987 static int mmc_test_basic_write(struct mmc_test_card *test)
989 int ret;
990 struct scatterlist sg;
992 ret = mmc_test_set_blksize(test, 512);
993 if (ret)
994 return ret;
996 sg_init_one(&sg, test->buffer, 512);
998 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
999 if (ret)
1000 return ret;
1002 return 0;
1005 static int mmc_test_basic_read(struct mmc_test_card *test)
1007 int ret;
1008 struct scatterlist sg;
1010 ret = mmc_test_set_blksize(test, 512);
1011 if (ret)
1012 return ret;
1014 sg_init_one(&sg, test->buffer, 512);
1016 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
1017 if (ret)
1018 return ret;
1020 return 0;
1023 static int mmc_test_verify_write(struct mmc_test_card *test)
1025 int ret;
1026 struct scatterlist sg;
1028 sg_init_one(&sg, test->buffer, 512);
1030 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1031 if (ret)
1032 return ret;
1034 return 0;
1037 static int mmc_test_verify_read(struct mmc_test_card *test)
1039 int ret;
1040 struct scatterlist sg;
1042 sg_init_one(&sg, test->buffer, 512);
1044 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1045 if (ret)
1046 return ret;
1048 return 0;
1051 static int mmc_test_multi_write(struct mmc_test_card *test)
1053 int ret;
1054 unsigned int size;
1055 struct scatterlist sg;
1057 if (test->card->host->max_blk_count == 1)
1058 return RESULT_UNSUP_HOST;
1060 size = PAGE_SIZE * 2;
1061 size = min(size, test->card->host->max_req_size);
1062 size = min(size, test->card->host->max_seg_size);
1063 size = min(size, test->card->host->max_blk_count * 512);
1065 if (size < 1024)
1066 return RESULT_UNSUP_HOST;
1068 sg_init_one(&sg, test->buffer, size);
1070 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1071 if (ret)
1072 return ret;
1074 return 0;
1077 static int mmc_test_multi_read(struct mmc_test_card *test)
1079 int ret;
1080 unsigned int size;
1081 struct scatterlist sg;
1083 if (test->card->host->max_blk_count == 1)
1084 return RESULT_UNSUP_HOST;
1086 size = PAGE_SIZE * 2;
1087 size = min(size, test->card->host->max_req_size);
1088 size = min(size, test->card->host->max_seg_size);
1089 size = min(size, test->card->host->max_blk_count * 512);
1091 if (size < 1024)
1092 return RESULT_UNSUP_HOST;
1094 sg_init_one(&sg, test->buffer, size);
1096 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1097 if (ret)
1098 return ret;
1100 return 0;
1103 static int mmc_test_pow2_write(struct mmc_test_card *test)
1105 int ret, i;
1106 struct scatterlist sg;
1108 if (!test->card->csd.write_partial)
1109 return RESULT_UNSUP_CARD;
1111 for (i = 1; i < 512;i <<= 1) {
1112 sg_init_one(&sg, test->buffer, i);
1113 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1114 if (ret)
1115 return ret;
1118 return 0;
1121 static int mmc_test_pow2_read(struct mmc_test_card *test)
1123 int ret, i;
1124 struct scatterlist sg;
1126 if (!test->card->csd.read_partial)
1127 return RESULT_UNSUP_CARD;
1129 for (i = 1; i < 512;i <<= 1) {
1130 sg_init_one(&sg, test->buffer, i);
1131 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1132 if (ret)
1133 return ret;
1136 return 0;
1139 static int mmc_test_weird_write(struct mmc_test_card *test)
1141 int ret, i;
1142 struct scatterlist sg;
1144 if (!test->card->csd.write_partial)
1145 return RESULT_UNSUP_CARD;
1147 for (i = 3; i < 512;i += 7) {
1148 sg_init_one(&sg, test->buffer, i);
1149 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1150 if (ret)
1151 return ret;
1154 return 0;
1157 static int mmc_test_weird_read(struct mmc_test_card *test)
1159 int ret, i;
1160 struct scatterlist sg;
1162 if (!test->card->csd.read_partial)
1163 return RESULT_UNSUP_CARD;
1165 for (i = 3; i < 512;i += 7) {
1166 sg_init_one(&sg, test->buffer, i);
1167 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1168 if (ret)
1169 return ret;
1172 return 0;
1175 static int mmc_test_align_write(struct mmc_test_card *test)
1177 int ret, i;
1178 struct scatterlist sg;
1180 for (i = 1; i < TEST_ALIGN_END; i++) {
1181 sg_init_one(&sg, test->buffer + i, 512);
1182 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1183 if (ret)
1184 return ret;
1187 return 0;
1190 static int mmc_test_align_read(struct mmc_test_card *test)
1192 int ret, i;
1193 struct scatterlist sg;
1195 for (i = 1; i < TEST_ALIGN_END; i++) {
1196 sg_init_one(&sg, test->buffer + i, 512);
1197 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1198 if (ret)
1199 return ret;
1202 return 0;
1205 static int mmc_test_align_multi_write(struct mmc_test_card *test)
1207 int ret, i;
1208 unsigned int size;
1209 struct scatterlist sg;
1211 if (test->card->host->max_blk_count == 1)
1212 return RESULT_UNSUP_HOST;
1214 size = PAGE_SIZE * 2;
1215 size = min(size, test->card->host->max_req_size);
1216 size = min(size, test->card->host->max_seg_size);
1217 size = min(size, test->card->host->max_blk_count * 512);
1219 if (size < 1024)
1220 return RESULT_UNSUP_HOST;
1222 for (i = 1; i < TEST_ALIGN_END; i++) {
1223 sg_init_one(&sg, test->buffer + i, size);
1224 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1225 if (ret)
1226 return ret;
1229 return 0;
1232 static int mmc_test_align_multi_read(struct mmc_test_card *test)
1234 int ret, i;
1235 unsigned int size;
1236 struct scatterlist sg;
1238 if (test->card->host->max_blk_count == 1)
1239 return RESULT_UNSUP_HOST;
1241 size = PAGE_SIZE * 2;
1242 size = min(size, test->card->host->max_req_size);
1243 size = min(size, test->card->host->max_seg_size);
1244 size = min(size, test->card->host->max_blk_count * 512);
1246 if (size < 1024)
1247 return RESULT_UNSUP_HOST;
1249 for (i = 1; i < TEST_ALIGN_END; i++) {
1250 sg_init_one(&sg, test->buffer + i, size);
1251 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1252 if (ret)
1253 return ret;
1256 return 0;
1259 static int mmc_test_xfersize_write(struct mmc_test_card *test)
1261 int ret;
1263 ret = mmc_test_set_blksize(test, 512);
1264 if (ret)
1265 return ret;
1267 ret = mmc_test_broken_transfer(test, 1, 512, 1);
1268 if (ret)
1269 return ret;
1271 return 0;
1274 static int mmc_test_xfersize_read(struct mmc_test_card *test)
1276 int ret;
1278 ret = mmc_test_set_blksize(test, 512);
1279 if (ret)
1280 return ret;
1282 ret = mmc_test_broken_transfer(test, 1, 512, 0);
1283 if (ret)
1284 return ret;
1286 return 0;
1289 static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
1291 int ret;
1293 if (test->card->host->max_blk_count == 1)
1294 return RESULT_UNSUP_HOST;
1296 ret = mmc_test_set_blksize(test, 512);
1297 if (ret)
1298 return ret;
1300 ret = mmc_test_broken_transfer(test, 2, 512, 1);
1301 if (ret)
1302 return ret;
1304 return 0;
1307 static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
1309 int ret;
1311 if (test->card->host->max_blk_count == 1)
1312 return RESULT_UNSUP_HOST;
1314 ret = mmc_test_set_blksize(test, 512);
1315 if (ret)
1316 return ret;
1318 ret = mmc_test_broken_transfer(test, 2, 512, 0);
1319 if (ret)
1320 return ret;
1322 return 0;
1325 #ifdef CONFIG_HIGHMEM
1327 static int mmc_test_write_high(struct mmc_test_card *test)
1329 int ret;
1330 struct scatterlist sg;
1332 sg_init_table(&sg, 1);
1333 sg_set_page(&sg, test->highmem, 512, 0);
1335 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1336 if (ret)
1337 return ret;
1339 return 0;
1342 static int mmc_test_read_high(struct mmc_test_card *test)
1344 int ret;
1345 struct scatterlist sg;
1347 sg_init_table(&sg, 1);
1348 sg_set_page(&sg, test->highmem, 512, 0);
1350 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1351 if (ret)
1352 return ret;
1354 return 0;
1357 static int mmc_test_multi_write_high(struct mmc_test_card *test)
1359 int ret;
1360 unsigned int size;
1361 struct scatterlist sg;
1363 if (test->card->host->max_blk_count == 1)
1364 return RESULT_UNSUP_HOST;
1366 size = PAGE_SIZE * 2;
1367 size = min(size, test->card->host->max_req_size);
1368 size = min(size, test->card->host->max_seg_size);
1369 size = min(size, test->card->host->max_blk_count * 512);
1371 if (size < 1024)
1372 return RESULT_UNSUP_HOST;
1374 sg_init_table(&sg, 1);
1375 sg_set_page(&sg, test->highmem, size, 0);
1377 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1378 if (ret)
1379 return ret;
1381 return 0;
1384 static int mmc_test_multi_read_high(struct mmc_test_card *test)
1386 int ret;
1387 unsigned int size;
1388 struct scatterlist sg;
1390 if (test->card->host->max_blk_count == 1)
1391 return RESULT_UNSUP_HOST;
1393 size = PAGE_SIZE * 2;
1394 size = min(size, test->card->host->max_req_size);
1395 size = min(size, test->card->host->max_seg_size);
1396 size = min(size, test->card->host->max_blk_count * 512);
1398 if (size < 1024)
1399 return RESULT_UNSUP_HOST;
1401 sg_init_table(&sg, 1);
1402 sg_set_page(&sg, test->highmem, size, 0);
1404 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1405 if (ret)
1406 return ret;
1408 return 0;
1411 #else
1413 static int mmc_test_no_highmem(struct mmc_test_card *test)
1415 pr_info("%s: Highmem not configured - test skipped\n",
1416 mmc_hostname(test->card->host));
1417 return 0;
1420 #endif /* CONFIG_HIGHMEM */
1423 * Map sz bytes so that it can be transferred.
1425 static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
1426 int max_scatter, int min_sg_len)
1428 struct mmc_test_area *t = &test->area;
1429 int err;
1431 t->blocks = sz >> 9;
1433 if (max_scatter) {
1434 err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
1435 t->max_segs, t->max_seg_sz,
1436 &t->sg_len);
1437 } else {
1438 err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
1439 t->max_seg_sz, &t->sg_len, min_sg_len);
1441 if (err)
1442 pr_info("%s: Failed to map sg list\n",
1443 mmc_hostname(test->card->host));
1444 return err;
1448 * Transfer bytes mapped by mmc_test_area_map().
1450 static int mmc_test_area_transfer(struct mmc_test_card *test,
1451 unsigned int dev_addr, int write)
1453 struct mmc_test_area *t = &test->area;
1455 return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
1456 t->blocks, 512, write);
1460 * Map and transfer bytes for multiple transfers.
1462 static int mmc_test_area_io_seq(struct mmc_test_card *test, unsigned long sz,
1463 unsigned int dev_addr, int write,
1464 int max_scatter, int timed, int count,
1465 bool nonblock, int min_sg_len)
1467 struct timespec ts1, ts2;
1468 int ret = 0;
1469 int i;
1470 struct mmc_test_area *t = &test->area;
1473 * In the case of a maximally scattered transfer, the maximum transfer
1474 * size is further limited by using PAGE_SIZE segments.
1476 if (max_scatter) {
1477 struct mmc_test_area *t = &test->area;
1478 unsigned long max_tfr;
1480 if (t->max_seg_sz >= PAGE_SIZE)
1481 max_tfr = t->max_segs * PAGE_SIZE;
1482 else
1483 max_tfr = t->max_segs * t->max_seg_sz;
1484 if (sz > max_tfr)
1485 sz = max_tfr;
1488 ret = mmc_test_area_map(test, sz, max_scatter, min_sg_len);
1489 if (ret)
1490 return ret;
1492 if (timed)
1493 getnstimeofday(&ts1);
1494 if (nonblock)
1495 ret = mmc_test_nonblock_transfer(test, t->sg, t->sg_len,
1496 dev_addr, t->blocks, 512, write, count);
1497 else
1498 for (i = 0; i < count && ret == 0; i++) {
1499 ret = mmc_test_area_transfer(test, dev_addr, write);
1500 dev_addr += sz >> 9;
1503 if (ret)
1504 return ret;
1506 if (timed)
1507 getnstimeofday(&ts2);
1509 if (timed)
1510 mmc_test_print_avg_rate(test, sz, count, &ts1, &ts2);
1512 return 0;
1515 static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
1516 unsigned int dev_addr, int write, int max_scatter,
1517 int timed)
1519 return mmc_test_area_io_seq(test, sz, dev_addr, write, max_scatter,
1520 timed, 1, false, 0);
1524 * Write the test area entirely.
1526 static int mmc_test_area_fill(struct mmc_test_card *test)
1528 struct mmc_test_area *t = &test->area;
1530 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, 1, 0, 0);
1534 * Erase the test area entirely.
1536 static int mmc_test_area_erase(struct mmc_test_card *test)
1538 struct mmc_test_area *t = &test->area;
1540 if (!mmc_can_erase(test->card))
1541 return 0;
1543 return mmc_erase(test->card, t->dev_addr, t->max_sz >> 9,
1544 MMC_ERASE_ARG);
1548 * Cleanup struct mmc_test_area.
1550 static int mmc_test_area_cleanup(struct mmc_test_card *test)
1552 struct mmc_test_area *t = &test->area;
1554 kfree(t->sg);
1555 mmc_test_free_mem(t->mem);
1557 return 0;
1561 * Initialize an area for testing large transfers. The test area is set to the
1562 * middle of the card because cards may have different charateristics at the
1563 * front (for FAT file system optimization). Optionally, the area is erased
1564 * (if the card supports it) which may improve write performance. Optionally,
1565 * the area is filled with data for subsequent read tests.
1567 static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
1569 struct mmc_test_area *t = &test->area;
1570 unsigned long min_sz = 64 * 1024, sz;
1571 int ret;
1573 ret = mmc_test_set_blksize(test, 512);
1574 if (ret)
1575 return ret;
1577 /* Make the test area size about 4MiB */
1578 sz = (unsigned long)test->card->pref_erase << 9;
1579 t->max_sz = sz;
1580 while (t->max_sz < 4 * 1024 * 1024)
1581 t->max_sz += sz;
1582 while (t->max_sz > TEST_AREA_MAX_SIZE && t->max_sz > sz)
1583 t->max_sz -= sz;
1585 t->max_segs = test->card->host->max_segs;
1586 t->max_seg_sz = test->card->host->max_seg_size;
1587 t->max_seg_sz -= t->max_seg_sz % 512;
1589 t->max_tfr = t->max_sz;
1590 if (t->max_tfr >> 9 > test->card->host->max_blk_count)
1591 t->max_tfr = test->card->host->max_blk_count << 9;
1592 if (t->max_tfr > test->card->host->max_req_size)
1593 t->max_tfr = test->card->host->max_req_size;
1594 if (t->max_tfr / t->max_seg_sz > t->max_segs)
1595 t->max_tfr = t->max_segs * t->max_seg_sz;
1598 * Try to allocate enough memory for a max. sized transfer. Less is OK
1599 * because the same memory can be mapped into the scatterlist more than
1600 * once. Also, take into account the limits imposed on scatterlist
1601 * segments by the host driver.
1603 t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
1604 t->max_seg_sz);
1605 if (!t->mem)
1606 return -ENOMEM;
1608 t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
1609 if (!t->sg) {
1610 ret = -ENOMEM;
1611 goto out_free;
1614 t->dev_addr = mmc_test_capacity(test->card) / 2;
1615 t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
1617 if (erase) {
1618 ret = mmc_test_area_erase(test);
1619 if (ret)
1620 goto out_free;
1623 if (fill) {
1624 ret = mmc_test_area_fill(test);
1625 if (ret)
1626 goto out_free;
1629 return 0;
1631 out_free:
1632 mmc_test_area_cleanup(test);
1633 return ret;
1637 * Prepare for large transfers. Do not erase the test area.
1639 static int mmc_test_area_prepare(struct mmc_test_card *test)
1641 return mmc_test_area_init(test, 0, 0);
1645 * Prepare for large transfers. Do erase the test area.
1647 static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
1649 return mmc_test_area_init(test, 1, 0);
1653 * Prepare for large transfers. Erase and fill the test area.
1655 static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
1657 return mmc_test_area_init(test, 1, 1);
1661 * Test best-case performance. Best-case performance is expected from
1662 * a single large transfer.
1664 * An additional option (max_scatter) allows the measurement of the same
1665 * transfer but with no contiguous pages in the scatter list. This tests
1666 * the efficiency of DMA to handle scattered pages.
1668 static int mmc_test_best_performance(struct mmc_test_card *test, int write,
1669 int max_scatter)
1671 struct mmc_test_area *t = &test->area;
1673 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, write,
1674 max_scatter, 1);
1678 * Best-case read performance.
1680 static int mmc_test_best_read_performance(struct mmc_test_card *test)
1682 return mmc_test_best_performance(test, 0, 0);
1686 * Best-case write performance.
1688 static int mmc_test_best_write_performance(struct mmc_test_card *test)
1690 return mmc_test_best_performance(test, 1, 0);
1694 * Best-case read performance into scattered pages.
1696 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
1698 return mmc_test_best_performance(test, 0, 1);
1702 * Best-case write performance from scattered pages.
1704 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
1706 return mmc_test_best_performance(test, 1, 1);
1710 * Single read performance by transfer size.
1712 static int mmc_test_profile_read_perf(struct mmc_test_card *test)
1714 struct mmc_test_area *t = &test->area;
1715 unsigned long sz;
1716 unsigned int dev_addr;
1717 int ret;
1719 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1720 dev_addr = t->dev_addr + (sz >> 9);
1721 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1722 if (ret)
1723 return ret;
1725 sz = t->max_tfr;
1726 dev_addr = t->dev_addr;
1727 return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1731 * Single write performance by transfer size.
1733 static int mmc_test_profile_write_perf(struct mmc_test_card *test)
1735 struct mmc_test_area *t = &test->area;
1736 unsigned long sz;
1737 unsigned int dev_addr;
1738 int ret;
1740 ret = mmc_test_area_erase(test);
1741 if (ret)
1742 return ret;
1743 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1744 dev_addr = t->dev_addr + (sz >> 9);
1745 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1746 if (ret)
1747 return ret;
1749 ret = mmc_test_area_erase(test);
1750 if (ret)
1751 return ret;
1752 sz = t->max_tfr;
1753 dev_addr = t->dev_addr;
1754 return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1758 * Single trim performance by transfer size.
1760 static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
1762 struct mmc_test_area *t = &test->area;
1763 unsigned long sz;
1764 unsigned int dev_addr;
1765 struct timespec ts1, ts2;
1766 int ret;
1768 if (!mmc_can_trim(test->card))
1769 return RESULT_UNSUP_CARD;
1771 if (!mmc_can_erase(test->card))
1772 return RESULT_UNSUP_HOST;
1774 for (sz = 512; sz < t->max_sz; sz <<= 1) {
1775 dev_addr = t->dev_addr + (sz >> 9);
1776 getnstimeofday(&ts1);
1777 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1778 if (ret)
1779 return ret;
1780 getnstimeofday(&ts2);
1781 mmc_test_print_rate(test, sz, &ts1, &ts2);
1783 dev_addr = t->dev_addr;
1784 getnstimeofday(&ts1);
1785 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1786 if (ret)
1787 return ret;
1788 getnstimeofday(&ts2);
1789 mmc_test_print_rate(test, sz, &ts1, &ts2);
1790 return 0;
1793 static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
1795 struct mmc_test_area *t = &test->area;
1796 unsigned int dev_addr, i, cnt;
1797 struct timespec ts1, ts2;
1798 int ret;
1800 cnt = t->max_sz / sz;
1801 dev_addr = t->dev_addr;
1802 getnstimeofday(&ts1);
1803 for (i = 0; i < cnt; i++) {
1804 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
1805 if (ret)
1806 return ret;
1807 dev_addr += (sz >> 9);
1809 getnstimeofday(&ts2);
1810 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1811 return 0;
1815 * Consecutive read performance by transfer size.
1817 static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
1819 struct mmc_test_area *t = &test->area;
1820 unsigned long sz;
1821 int ret;
1823 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1824 ret = mmc_test_seq_read_perf(test, sz);
1825 if (ret)
1826 return ret;
1828 sz = t->max_tfr;
1829 return mmc_test_seq_read_perf(test, sz);
1832 static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
1834 struct mmc_test_area *t = &test->area;
1835 unsigned int dev_addr, i, cnt;
1836 struct timespec ts1, ts2;
1837 int ret;
1839 ret = mmc_test_area_erase(test);
1840 if (ret)
1841 return ret;
1842 cnt = t->max_sz / sz;
1843 dev_addr = t->dev_addr;
1844 getnstimeofday(&ts1);
1845 for (i = 0; i < cnt; i++) {
1846 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
1847 if (ret)
1848 return ret;
1849 dev_addr += (sz >> 9);
1851 getnstimeofday(&ts2);
1852 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1853 return 0;
1857 * Consecutive write performance by transfer size.
1859 static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
1861 struct mmc_test_area *t = &test->area;
1862 unsigned long sz;
1863 int ret;
1865 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1866 ret = mmc_test_seq_write_perf(test, sz);
1867 if (ret)
1868 return ret;
1870 sz = t->max_tfr;
1871 return mmc_test_seq_write_perf(test, sz);
1875 * Consecutive trim performance by transfer size.
1877 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
1879 struct mmc_test_area *t = &test->area;
1880 unsigned long sz;
1881 unsigned int dev_addr, i, cnt;
1882 struct timespec ts1, ts2;
1883 int ret;
1885 if (!mmc_can_trim(test->card))
1886 return RESULT_UNSUP_CARD;
1888 if (!mmc_can_erase(test->card))
1889 return RESULT_UNSUP_HOST;
1891 for (sz = 512; sz <= t->max_sz; sz <<= 1) {
1892 ret = mmc_test_area_erase(test);
1893 if (ret)
1894 return ret;
1895 ret = mmc_test_area_fill(test);
1896 if (ret)
1897 return ret;
1898 cnt = t->max_sz / sz;
1899 dev_addr = t->dev_addr;
1900 getnstimeofday(&ts1);
1901 for (i = 0; i < cnt; i++) {
1902 ret = mmc_erase(test->card, dev_addr, sz >> 9,
1903 MMC_TRIM_ARG);
1904 if (ret)
1905 return ret;
1906 dev_addr += (sz >> 9);
1908 getnstimeofday(&ts2);
1909 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1911 return 0;
1914 static unsigned int rnd_next = 1;
1916 static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt)
1918 uint64_t r;
1920 rnd_next = rnd_next * 1103515245 + 12345;
1921 r = (rnd_next >> 16) & 0x7fff;
1922 return (r * rnd_cnt) >> 15;
1925 static int mmc_test_rnd_perf(struct mmc_test_card *test, int write, int print,
1926 unsigned long sz)
1928 unsigned int dev_addr, cnt, rnd_addr, range1, range2, last_ea = 0, ea;
1929 unsigned int ssz;
1930 struct timespec ts1, ts2, ts;
1931 int ret;
1933 ssz = sz >> 9;
1935 rnd_addr = mmc_test_capacity(test->card) / 4;
1936 range1 = rnd_addr / test->card->pref_erase;
1937 range2 = range1 / ssz;
1939 getnstimeofday(&ts1);
1940 for (cnt = 0; cnt < UINT_MAX; cnt++) {
1941 getnstimeofday(&ts2);
1942 ts = timespec_sub(ts2, ts1);
1943 if (ts.tv_sec >= 10)
1944 break;
1945 ea = mmc_test_rnd_num(range1);
1946 if (ea == last_ea)
1947 ea -= 1;
1948 last_ea = ea;
1949 dev_addr = rnd_addr + test->card->pref_erase * ea +
1950 ssz * mmc_test_rnd_num(range2);
1951 ret = mmc_test_area_io(test, sz, dev_addr, write, 0, 0);
1952 if (ret)
1953 return ret;
1955 if (print)
1956 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1957 return 0;
1960 static int mmc_test_random_perf(struct mmc_test_card *test, int write)
1962 struct mmc_test_area *t = &test->area;
1963 unsigned int next;
1964 unsigned long sz;
1965 int ret;
1967 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1969 * When writing, try to get more consistent results by running
1970 * the test twice with exactly the same I/O but outputting the
1971 * results only for the 2nd run.
1973 if (write) {
1974 next = rnd_next;
1975 ret = mmc_test_rnd_perf(test, write, 0, sz);
1976 if (ret)
1977 return ret;
1978 rnd_next = next;
1980 ret = mmc_test_rnd_perf(test, write, 1, sz);
1981 if (ret)
1982 return ret;
1984 sz = t->max_tfr;
1985 if (write) {
1986 next = rnd_next;
1987 ret = mmc_test_rnd_perf(test, write, 0, sz);
1988 if (ret)
1989 return ret;
1990 rnd_next = next;
1992 return mmc_test_rnd_perf(test, write, 1, sz);
1996 * Random read performance by transfer size.
1998 static int mmc_test_random_read_perf(struct mmc_test_card *test)
2000 return mmc_test_random_perf(test, 0);
2004 * Random write performance by transfer size.
2006 static int mmc_test_random_write_perf(struct mmc_test_card *test)
2008 return mmc_test_random_perf(test, 1);
2011 static int mmc_test_seq_perf(struct mmc_test_card *test, int write,
2012 unsigned int tot_sz, int max_scatter)
2014 struct mmc_test_area *t = &test->area;
2015 unsigned int dev_addr, i, cnt, sz, ssz;
2016 struct timespec ts1, ts2;
2017 int ret;
2019 sz = t->max_tfr;
2022 * In the case of a maximally scattered transfer, the maximum transfer
2023 * size is further limited by using PAGE_SIZE segments.
2025 if (max_scatter) {
2026 unsigned long max_tfr;
2028 if (t->max_seg_sz >= PAGE_SIZE)
2029 max_tfr = t->max_segs * PAGE_SIZE;
2030 else
2031 max_tfr = t->max_segs * t->max_seg_sz;
2032 if (sz > max_tfr)
2033 sz = max_tfr;
2036 ssz = sz >> 9;
2037 dev_addr = mmc_test_capacity(test->card) / 4;
2038 if (tot_sz > dev_addr << 9)
2039 tot_sz = dev_addr << 9;
2040 cnt = tot_sz / sz;
2041 dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
2043 getnstimeofday(&ts1);
2044 for (i = 0; i < cnt; i++) {
2045 ret = mmc_test_area_io(test, sz, dev_addr, write,
2046 max_scatter, 0);
2047 if (ret)
2048 return ret;
2049 dev_addr += ssz;
2051 getnstimeofday(&ts2);
2053 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
2055 return 0;
2058 static int mmc_test_large_seq_perf(struct mmc_test_card *test, int write)
2060 int ret, i;
2062 for (i = 0; i < 10; i++) {
2063 ret = mmc_test_seq_perf(test, write, 10 * 1024 * 1024, 1);
2064 if (ret)
2065 return ret;
2067 for (i = 0; i < 5; i++) {
2068 ret = mmc_test_seq_perf(test, write, 100 * 1024 * 1024, 1);
2069 if (ret)
2070 return ret;
2072 for (i = 0; i < 3; i++) {
2073 ret = mmc_test_seq_perf(test, write, 1000 * 1024 * 1024, 1);
2074 if (ret)
2075 return ret;
2078 return ret;
2082 * Large sequential read performance.
2084 static int mmc_test_large_seq_read_perf(struct mmc_test_card *test)
2086 return mmc_test_large_seq_perf(test, 0);
2090 * Large sequential write performance.
2092 static int mmc_test_large_seq_write_perf(struct mmc_test_card *test)
2094 return mmc_test_large_seq_perf(test, 1);
2097 static int mmc_test_rw_multiple(struct mmc_test_card *test,
2098 struct mmc_test_multiple_rw *tdata,
2099 unsigned int reqsize, unsigned int size,
2100 int min_sg_len)
2102 unsigned int dev_addr;
2103 struct mmc_test_area *t = &test->area;
2104 int ret = 0;
2106 /* Set up test area */
2107 if (size > mmc_test_capacity(test->card) / 2 * 512)
2108 size = mmc_test_capacity(test->card) / 2 * 512;
2109 if (reqsize > t->max_tfr)
2110 reqsize = t->max_tfr;
2111 dev_addr = mmc_test_capacity(test->card) / 4;
2112 if ((dev_addr & 0xffff0000))
2113 dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
2114 else
2115 dev_addr &= 0xfffff800; /* Round to 1MiB boundary */
2116 if (!dev_addr)
2117 goto err;
2119 if (reqsize > size)
2120 return 0;
2122 /* prepare test area */
2123 if (mmc_can_erase(test->card) &&
2124 tdata->prepare & MMC_TEST_PREP_ERASE) {
2125 ret = mmc_erase(test->card, dev_addr,
2126 size / 512, MMC_SECURE_ERASE_ARG);
2127 if (ret)
2128 ret = mmc_erase(test->card, dev_addr,
2129 size / 512, MMC_ERASE_ARG);
2130 if (ret)
2131 goto err;
2134 /* Run test */
2135 ret = mmc_test_area_io_seq(test, reqsize, dev_addr,
2136 tdata->do_write, 0, 1, size / reqsize,
2137 tdata->do_nonblock_req, min_sg_len);
2138 if (ret)
2139 goto err;
2141 return ret;
2142 err:
2143 pr_info("[%s] error\n", __func__);
2144 return ret;
2147 static int mmc_test_rw_multiple_size(struct mmc_test_card *test,
2148 struct mmc_test_multiple_rw *rw)
2150 int ret = 0;
2151 int i;
2152 void *pre_req = test->card->host->ops->pre_req;
2153 void *post_req = test->card->host->ops->post_req;
2155 if (rw->do_nonblock_req &&
2156 ((!pre_req && post_req) || (pre_req && !post_req))) {
2157 pr_info("error: only one of pre/post is defined\n");
2158 return -EINVAL;
2161 for (i = 0 ; i < rw->len && ret == 0; i++) {
2162 ret = mmc_test_rw_multiple(test, rw, rw->bs[i], rw->size, 0);
2163 if (ret)
2164 break;
2166 return ret;
2169 static int mmc_test_rw_multiple_sg_len(struct mmc_test_card *test,
2170 struct mmc_test_multiple_rw *rw)
2172 int ret = 0;
2173 int i;
2175 for (i = 0 ; i < rw->len && ret == 0; i++) {
2176 ret = mmc_test_rw_multiple(test, rw, 512*1024, rw->size,
2177 rw->sg_len[i]);
2178 if (ret)
2179 break;
2181 return ret;
2185 * Multiple blocking write 4k to 4 MB chunks
2187 static int mmc_test_profile_mult_write_blocking_perf(struct mmc_test_card *test)
2189 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2190 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2191 struct mmc_test_multiple_rw test_data = {
2192 .bs = bs,
2193 .size = TEST_AREA_MAX_SIZE,
2194 .len = ARRAY_SIZE(bs),
2195 .do_write = true,
2196 .do_nonblock_req = false,
2197 .prepare = MMC_TEST_PREP_ERASE,
2200 return mmc_test_rw_multiple_size(test, &test_data);
2204 * Multiple non-blocking write 4k to 4 MB chunks
2206 static int mmc_test_profile_mult_write_nonblock_perf(struct mmc_test_card *test)
2208 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2209 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2210 struct mmc_test_multiple_rw test_data = {
2211 .bs = bs,
2212 .size = TEST_AREA_MAX_SIZE,
2213 .len = ARRAY_SIZE(bs),
2214 .do_write = true,
2215 .do_nonblock_req = true,
2216 .prepare = MMC_TEST_PREP_ERASE,
2219 return mmc_test_rw_multiple_size(test, &test_data);
2223 * Multiple blocking read 4k to 4 MB chunks
2225 static int mmc_test_profile_mult_read_blocking_perf(struct mmc_test_card *test)
2227 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2228 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2229 struct mmc_test_multiple_rw test_data = {
2230 .bs = bs,
2231 .size = TEST_AREA_MAX_SIZE,
2232 .len = ARRAY_SIZE(bs),
2233 .do_write = false,
2234 .do_nonblock_req = false,
2235 .prepare = MMC_TEST_PREP_NONE,
2238 return mmc_test_rw_multiple_size(test, &test_data);
2242 * Multiple non-blocking read 4k to 4 MB chunks
2244 static int mmc_test_profile_mult_read_nonblock_perf(struct mmc_test_card *test)
2246 unsigned int bs[] = {1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16,
2247 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 22};
2248 struct mmc_test_multiple_rw test_data = {
2249 .bs = bs,
2250 .size = TEST_AREA_MAX_SIZE,
2251 .len = ARRAY_SIZE(bs),
2252 .do_write = false,
2253 .do_nonblock_req = true,
2254 .prepare = MMC_TEST_PREP_NONE,
2257 return mmc_test_rw_multiple_size(test, &test_data);
2261 * Multiple blocking write 1 to 512 sg elements
2263 static int mmc_test_profile_sglen_wr_blocking_perf(struct mmc_test_card *test)
2265 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2266 1 << 7, 1 << 8, 1 << 9};
2267 struct mmc_test_multiple_rw test_data = {
2268 .sg_len = sg_len,
2269 .size = TEST_AREA_MAX_SIZE,
2270 .len = ARRAY_SIZE(sg_len),
2271 .do_write = true,
2272 .do_nonblock_req = false,
2273 .prepare = MMC_TEST_PREP_ERASE,
2276 return mmc_test_rw_multiple_sg_len(test, &test_data);
2280 * Multiple non-blocking write 1 to 512 sg elements
2282 static int mmc_test_profile_sglen_wr_nonblock_perf(struct mmc_test_card *test)
2284 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2285 1 << 7, 1 << 8, 1 << 9};
2286 struct mmc_test_multiple_rw test_data = {
2287 .sg_len = sg_len,
2288 .size = TEST_AREA_MAX_SIZE,
2289 .len = ARRAY_SIZE(sg_len),
2290 .do_write = true,
2291 .do_nonblock_req = true,
2292 .prepare = MMC_TEST_PREP_ERASE,
2295 return mmc_test_rw_multiple_sg_len(test, &test_data);
2299 * Multiple blocking read 1 to 512 sg elements
2301 static int mmc_test_profile_sglen_r_blocking_perf(struct mmc_test_card *test)
2303 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2304 1 << 7, 1 << 8, 1 << 9};
2305 struct mmc_test_multiple_rw test_data = {
2306 .sg_len = sg_len,
2307 .size = TEST_AREA_MAX_SIZE,
2308 .len = ARRAY_SIZE(sg_len),
2309 .do_write = false,
2310 .do_nonblock_req = false,
2311 .prepare = MMC_TEST_PREP_NONE,
2314 return mmc_test_rw_multiple_sg_len(test, &test_data);
2318 * Multiple non-blocking read 1 to 512 sg elements
2320 static int mmc_test_profile_sglen_r_nonblock_perf(struct mmc_test_card *test)
2322 unsigned int sg_len[] = {1, 1 << 3, 1 << 4, 1 << 5, 1 << 6,
2323 1 << 7, 1 << 8, 1 << 9};
2324 struct mmc_test_multiple_rw test_data = {
2325 .sg_len = sg_len,
2326 .size = TEST_AREA_MAX_SIZE,
2327 .len = ARRAY_SIZE(sg_len),
2328 .do_write = false,
2329 .do_nonblock_req = true,
2330 .prepare = MMC_TEST_PREP_NONE,
2333 return mmc_test_rw_multiple_sg_len(test, &test_data);
2337 * eMMC hardware reset.
2339 static int mmc_test_hw_reset(struct mmc_test_card *test)
2341 struct mmc_card *card = test->card;
2342 struct mmc_host *host = card->host;
2343 int err;
2345 err = mmc_hw_reset_check(host);
2346 if (!err)
2347 return RESULT_OK;
2349 if (err == -ENOSYS)
2350 return RESULT_FAIL;
2352 if (err != -EOPNOTSUPP)
2353 return err;
2355 if (!mmc_can_reset(card))
2356 return RESULT_UNSUP_CARD;
2358 return RESULT_UNSUP_HOST;
2361 static const struct mmc_test_case mmc_test_cases[] = {
2363 .name = "Basic write (no data verification)",
2364 .run = mmc_test_basic_write,
2368 .name = "Basic read (no data verification)",
2369 .run = mmc_test_basic_read,
2373 .name = "Basic write (with data verification)",
2374 .prepare = mmc_test_prepare_write,
2375 .run = mmc_test_verify_write,
2376 .cleanup = mmc_test_cleanup,
2380 .name = "Basic read (with data verification)",
2381 .prepare = mmc_test_prepare_read,
2382 .run = mmc_test_verify_read,
2383 .cleanup = mmc_test_cleanup,
2387 .name = "Multi-block write",
2388 .prepare = mmc_test_prepare_write,
2389 .run = mmc_test_multi_write,
2390 .cleanup = mmc_test_cleanup,
2394 .name = "Multi-block read",
2395 .prepare = mmc_test_prepare_read,
2396 .run = mmc_test_multi_read,
2397 .cleanup = mmc_test_cleanup,
2401 .name = "Power of two block writes",
2402 .prepare = mmc_test_prepare_write,
2403 .run = mmc_test_pow2_write,
2404 .cleanup = mmc_test_cleanup,
2408 .name = "Power of two block reads",
2409 .prepare = mmc_test_prepare_read,
2410 .run = mmc_test_pow2_read,
2411 .cleanup = mmc_test_cleanup,
2415 .name = "Weird sized block writes",
2416 .prepare = mmc_test_prepare_write,
2417 .run = mmc_test_weird_write,
2418 .cleanup = mmc_test_cleanup,
2422 .name = "Weird sized block reads",
2423 .prepare = mmc_test_prepare_read,
2424 .run = mmc_test_weird_read,
2425 .cleanup = mmc_test_cleanup,
2429 .name = "Badly aligned write",
2430 .prepare = mmc_test_prepare_write,
2431 .run = mmc_test_align_write,
2432 .cleanup = mmc_test_cleanup,
2436 .name = "Badly aligned read",
2437 .prepare = mmc_test_prepare_read,
2438 .run = mmc_test_align_read,
2439 .cleanup = mmc_test_cleanup,
2443 .name = "Badly aligned multi-block write",
2444 .prepare = mmc_test_prepare_write,
2445 .run = mmc_test_align_multi_write,
2446 .cleanup = mmc_test_cleanup,
2450 .name = "Badly aligned multi-block read",
2451 .prepare = mmc_test_prepare_read,
2452 .run = mmc_test_align_multi_read,
2453 .cleanup = mmc_test_cleanup,
2457 .name = "Correct xfer_size at write (start failure)",
2458 .run = mmc_test_xfersize_write,
2462 .name = "Correct xfer_size at read (start failure)",
2463 .run = mmc_test_xfersize_read,
2467 .name = "Correct xfer_size at write (midway failure)",
2468 .run = mmc_test_multi_xfersize_write,
2472 .name = "Correct xfer_size at read (midway failure)",
2473 .run = mmc_test_multi_xfersize_read,
2476 #ifdef CONFIG_HIGHMEM
2479 .name = "Highmem write",
2480 .prepare = mmc_test_prepare_write,
2481 .run = mmc_test_write_high,
2482 .cleanup = mmc_test_cleanup,
2486 .name = "Highmem read",
2487 .prepare = mmc_test_prepare_read,
2488 .run = mmc_test_read_high,
2489 .cleanup = mmc_test_cleanup,
2493 .name = "Multi-block highmem write",
2494 .prepare = mmc_test_prepare_write,
2495 .run = mmc_test_multi_write_high,
2496 .cleanup = mmc_test_cleanup,
2500 .name = "Multi-block highmem read",
2501 .prepare = mmc_test_prepare_read,
2502 .run = mmc_test_multi_read_high,
2503 .cleanup = mmc_test_cleanup,
2506 #else
2509 .name = "Highmem write",
2510 .run = mmc_test_no_highmem,
2514 .name = "Highmem read",
2515 .run = mmc_test_no_highmem,
2519 .name = "Multi-block highmem write",
2520 .run = mmc_test_no_highmem,
2524 .name = "Multi-block highmem read",
2525 .run = mmc_test_no_highmem,
2528 #endif /* CONFIG_HIGHMEM */
2531 .name = "Best-case read performance",
2532 .prepare = mmc_test_area_prepare_fill,
2533 .run = mmc_test_best_read_performance,
2534 .cleanup = mmc_test_area_cleanup,
2538 .name = "Best-case write performance",
2539 .prepare = mmc_test_area_prepare_erase,
2540 .run = mmc_test_best_write_performance,
2541 .cleanup = mmc_test_area_cleanup,
2545 .name = "Best-case read performance into scattered pages",
2546 .prepare = mmc_test_area_prepare_fill,
2547 .run = mmc_test_best_read_perf_max_scatter,
2548 .cleanup = mmc_test_area_cleanup,
2552 .name = "Best-case write performance from scattered pages",
2553 .prepare = mmc_test_area_prepare_erase,
2554 .run = mmc_test_best_write_perf_max_scatter,
2555 .cleanup = mmc_test_area_cleanup,
2559 .name = "Single read performance by transfer size",
2560 .prepare = mmc_test_area_prepare_fill,
2561 .run = mmc_test_profile_read_perf,
2562 .cleanup = mmc_test_area_cleanup,
2566 .name = "Single write performance by transfer size",
2567 .prepare = mmc_test_area_prepare,
2568 .run = mmc_test_profile_write_perf,
2569 .cleanup = mmc_test_area_cleanup,
2573 .name = "Single trim performance by transfer size",
2574 .prepare = mmc_test_area_prepare_fill,
2575 .run = mmc_test_profile_trim_perf,
2576 .cleanup = mmc_test_area_cleanup,
2580 .name = "Consecutive read performance by transfer size",
2581 .prepare = mmc_test_area_prepare_fill,
2582 .run = mmc_test_profile_seq_read_perf,
2583 .cleanup = mmc_test_area_cleanup,
2587 .name = "Consecutive write performance by transfer size",
2588 .prepare = mmc_test_area_prepare,
2589 .run = mmc_test_profile_seq_write_perf,
2590 .cleanup = mmc_test_area_cleanup,
2594 .name = "Consecutive trim performance by transfer size",
2595 .prepare = mmc_test_area_prepare,
2596 .run = mmc_test_profile_seq_trim_perf,
2597 .cleanup = mmc_test_area_cleanup,
2601 .name = "Random read performance by transfer size",
2602 .prepare = mmc_test_area_prepare,
2603 .run = mmc_test_random_read_perf,
2604 .cleanup = mmc_test_area_cleanup,
2608 .name = "Random write performance by transfer size",
2609 .prepare = mmc_test_area_prepare,
2610 .run = mmc_test_random_write_perf,
2611 .cleanup = mmc_test_area_cleanup,
2615 .name = "Large sequential read into scattered pages",
2616 .prepare = mmc_test_area_prepare,
2617 .run = mmc_test_large_seq_read_perf,
2618 .cleanup = mmc_test_area_cleanup,
2622 .name = "Large sequential write from scattered pages",
2623 .prepare = mmc_test_area_prepare,
2624 .run = mmc_test_large_seq_write_perf,
2625 .cleanup = mmc_test_area_cleanup,
2629 .name = "Write performance with blocking req 4k to 4MB",
2630 .prepare = mmc_test_area_prepare,
2631 .run = mmc_test_profile_mult_write_blocking_perf,
2632 .cleanup = mmc_test_area_cleanup,
2636 .name = "Write performance with non-blocking req 4k to 4MB",
2637 .prepare = mmc_test_area_prepare,
2638 .run = mmc_test_profile_mult_write_nonblock_perf,
2639 .cleanup = mmc_test_area_cleanup,
2643 .name = "Read performance with blocking req 4k to 4MB",
2644 .prepare = mmc_test_area_prepare,
2645 .run = mmc_test_profile_mult_read_blocking_perf,
2646 .cleanup = mmc_test_area_cleanup,
2650 .name = "Read performance with non-blocking req 4k to 4MB",
2651 .prepare = mmc_test_area_prepare,
2652 .run = mmc_test_profile_mult_read_nonblock_perf,
2653 .cleanup = mmc_test_area_cleanup,
2657 .name = "Write performance blocking req 1 to 512 sg elems",
2658 .prepare = mmc_test_area_prepare,
2659 .run = mmc_test_profile_sglen_wr_blocking_perf,
2660 .cleanup = mmc_test_area_cleanup,
2664 .name = "Write performance non-blocking req 1 to 512 sg elems",
2665 .prepare = mmc_test_area_prepare,
2666 .run = mmc_test_profile_sglen_wr_nonblock_perf,
2667 .cleanup = mmc_test_area_cleanup,
2671 .name = "Read performance blocking req 1 to 512 sg elems",
2672 .prepare = mmc_test_area_prepare,
2673 .run = mmc_test_profile_sglen_r_blocking_perf,
2674 .cleanup = mmc_test_area_cleanup,
2678 .name = "Read performance non-blocking req 1 to 512 sg elems",
2679 .prepare = mmc_test_area_prepare,
2680 .run = mmc_test_profile_sglen_r_nonblock_perf,
2681 .cleanup = mmc_test_area_cleanup,
2685 .name = "eMMC hardware reset",
2686 .run = mmc_test_hw_reset,
2690 static DEFINE_MUTEX(mmc_test_lock);
2692 static LIST_HEAD(mmc_test_result);
2694 static void mmc_test_run(struct mmc_test_card *test, int testcase)
2696 int i, ret;
2698 pr_info("%s: Starting tests of card %s...\n",
2699 mmc_hostname(test->card->host), mmc_card_id(test->card));
2701 mmc_claim_host(test->card->host);
2703 for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
2704 struct mmc_test_general_result *gr;
2706 if (testcase && ((i + 1) != testcase))
2707 continue;
2709 pr_info("%s: Test case %d. %s...\n",
2710 mmc_hostname(test->card->host), i + 1,
2711 mmc_test_cases[i].name);
2713 if (mmc_test_cases[i].prepare) {
2714 ret = mmc_test_cases[i].prepare(test);
2715 if (ret) {
2716 pr_info("%s: Result: Prepare "
2717 "stage failed! (%d)\n",
2718 mmc_hostname(test->card->host),
2719 ret);
2720 continue;
2724 gr = kzalloc(sizeof(struct mmc_test_general_result),
2725 GFP_KERNEL);
2726 if (gr) {
2727 INIT_LIST_HEAD(&gr->tr_lst);
2729 /* Assign data what we know already */
2730 gr->card = test->card;
2731 gr->testcase = i;
2733 /* Append container to global one */
2734 list_add_tail(&gr->link, &mmc_test_result);
2737 * Save the pointer to created container in our private
2738 * structure.
2740 test->gr = gr;
2743 ret = mmc_test_cases[i].run(test);
2744 switch (ret) {
2745 case RESULT_OK:
2746 pr_info("%s: Result: OK\n",
2747 mmc_hostname(test->card->host));
2748 break;
2749 case RESULT_FAIL:
2750 pr_info("%s: Result: FAILED\n",
2751 mmc_hostname(test->card->host));
2752 break;
2753 case RESULT_UNSUP_HOST:
2754 pr_info("%s: Result: UNSUPPORTED "
2755 "(by host)\n",
2756 mmc_hostname(test->card->host));
2757 break;
2758 case RESULT_UNSUP_CARD:
2759 pr_info("%s: Result: UNSUPPORTED "
2760 "(by card)\n",
2761 mmc_hostname(test->card->host));
2762 break;
2763 default:
2764 pr_info("%s: Result: ERROR (%d)\n",
2765 mmc_hostname(test->card->host), ret);
2768 /* Save the result */
2769 if (gr)
2770 gr->result = ret;
2772 if (mmc_test_cases[i].cleanup) {
2773 ret = mmc_test_cases[i].cleanup(test);
2774 if (ret) {
2775 pr_info("%s: Warning: Cleanup "
2776 "stage failed! (%d)\n",
2777 mmc_hostname(test->card->host),
2778 ret);
2783 mmc_release_host(test->card->host);
2785 pr_info("%s: Tests completed.\n",
2786 mmc_hostname(test->card->host));
2789 static void mmc_test_free_result(struct mmc_card *card)
2791 struct mmc_test_general_result *gr, *grs;
2793 mutex_lock(&mmc_test_lock);
2795 list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
2796 struct mmc_test_transfer_result *tr, *trs;
2798 if (card && gr->card != card)
2799 continue;
2801 list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
2802 list_del(&tr->link);
2803 kfree(tr);
2806 list_del(&gr->link);
2807 kfree(gr);
2810 mutex_unlock(&mmc_test_lock);
2813 static LIST_HEAD(mmc_test_file_test);
2815 static int mtf_test_show(struct seq_file *sf, void *data)
2817 struct mmc_card *card = (struct mmc_card *)sf->private;
2818 struct mmc_test_general_result *gr;
2820 mutex_lock(&mmc_test_lock);
2822 list_for_each_entry(gr, &mmc_test_result, link) {
2823 struct mmc_test_transfer_result *tr;
2825 if (gr->card != card)
2826 continue;
2828 seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
2830 list_for_each_entry(tr, &gr->tr_lst, link) {
2831 seq_printf(sf, "%u %d %lu.%09lu %u %u.%02u\n",
2832 tr->count, tr->sectors,
2833 (unsigned long)tr->ts.tv_sec,
2834 (unsigned long)tr->ts.tv_nsec,
2835 tr->rate, tr->iops / 100, tr->iops % 100);
2839 mutex_unlock(&mmc_test_lock);
2841 return 0;
2844 static int mtf_test_open(struct inode *inode, struct file *file)
2846 return single_open(file, mtf_test_show, inode->i_private);
2849 static ssize_t mtf_test_write(struct file *file, const char __user *buf,
2850 size_t count, loff_t *pos)
2852 struct seq_file *sf = (struct seq_file *)file->private_data;
2853 struct mmc_card *card = (struct mmc_card *)sf->private;
2854 struct mmc_test_card *test;
2855 long testcase;
2856 int ret;
2858 ret = kstrtol_from_user(buf, count, 10, &testcase);
2859 if (ret)
2860 return ret;
2862 test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
2863 if (!test)
2864 return -ENOMEM;
2867 * Remove all test cases associated with given card. Thus we have only
2868 * actual data of the last run.
2870 mmc_test_free_result(card);
2872 test->card = card;
2874 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
2875 #ifdef CONFIG_HIGHMEM
2876 test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
2877 #endif
2879 #ifdef CONFIG_HIGHMEM
2880 if (test->buffer && test->highmem) {
2881 #else
2882 if (test->buffer) {
2883 #endif
2884 mutex_lock(&mmc_test_lock);
2885 mmc_test_run(test, testcase);
2886 mutex_unlock(&mmc_test_lock);
2889 #ifdef CONFIG_HIGHMEM
2890 __free_pages(test->highmem, BUFFER_ORDER);
2891 #endif
2892 kfree(test->buffer);
2893 kfree(test);
2895 return count;
2898 static const struct file_operations mmc_test_fops_test = {
2899 .open = mtf_test_open,
2900 .read = seq_read,
2901 .write = mtf_test_write,
2902 .llseek = seq_lseek,
2903 .release = single_release,
2906 static int mtf_testlist_show(struct seq_file *sf, void *data)
2908 int i;
2910 mutex_lock(&mmc_test_lock);
2912 for (i = 0; i < ARRAY_SIZE(mmc_test_cases); i++)
2913 seq_printf(sf, "%d:\t%s\n", i+1, mmc_test_cases[i].name);
2915 mutex_unlock(&mmc_test_lock);
2917 return 0;
2920 static int mtf_testlist_open(struct inode *inode, struct file *file)
2922 return single_open(file, mtf_testlist_show, inode->i_private);
2925 static const struct file_operations mmc_test_fops_testlist = {
2926 .open = mtf_testlist_open,
2927 .read = seq_read,
2928 .llseek = seq_lseek,
2929 .release = single_release,
2932 static void mmc_test_free_dbgfs_file(struct mmc_card *card)
2934 struct mmc_test_dbgfs_file *df, *dfs;
2936 mutex_lock(&mmc_test_lock);
2938 list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
2939 if (card && df->card != card)
2940 continue;
2941 debugfs_remove(df->file);
2942 list_del(&df->link);
2943 kfree(df);
2946 mutex_unlock(&mmc_test_lock);
2949 static int __mmc_test_register_dbgfs_file(struct mmc_card *card,
2950 const char *name, umode_t mode, const struct file_operations *fops)
2952 struct dentry *file = NULL;
2953 struct mmc_test_dbgfs_file *df;
2955 if (card->debugfs_root)
2956 file = debugfs_create_file(name, mode, card->debugfs_root,
2957 card, fops);
2959 if (IS_ERR_OR_NULL(file)) {
2960 dev_err(&card->dev,
2961 "Can't create %s. Perhaps debugfs is disabled.\n",
2962 name);
2963 return -ENODEV;
2966 df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
2967 if (!df) {
2968 debugfs_remove(file);
2969 dev_err(&card->dev,
2970 "Can't allocate memory for internal usage.\n");
2971 return -ENOMEM;
2974 df->card = card;
2975 df->file = file;
2977 list_add(&df->link, &mmc_test_file_test);
2978 return 0;
2981 static int mmc_test_register_dbgfs_file(struct mmc_card *card)
2983 int ret;
2985 mutex_lock(&mmc_test_lock);
2987 ret = __mmc_test_register_dbgfs_file(card, "test", S_IWUSR | S_IRUGO,
2988 &mmc_test_fops_test);
2989 if (ret)
2990 goto err;
2992 ret = __mmc_test_register_dbgfs_file(card, "testlist", S_IRUGO,
2993 &mmc_test_fops_testlist);
2994 if (ret)
2995 goto err;
2997 err:
2998 mutex_unlock(&mmc_test_lock);
3000 return ret;
3003 static int mmc_test_probe(struct device *dev)
3005 struct mmc_card *card = mmc_dev_to_card(dev);
3006 int ret;
3008 if (!mmc_card_mmc(card) && !mmc_card_sd(card))
3009 return -ENODEV;
3011 ret = mmc_test_register_dbgfs_file(card);
3012 if (ret)
3013 return ret;
3015 dev_info(&card->dev, "Card claimed for testing.\n");
3017 return 0;
3020 static int mmc_test_remove(struct device *dev)
3022 struct mmc_card *card = mmc_dev_to_card(dev);
3024 mmc_test_free_result(card);
3025 mmc_test_free_dbgfs_file(card);
3027 return 0;
3030 static void mmc_test_shutdown(struct device *dev)
3034 static struct device_driver mmc_driver = {
3035 .name = "mmc_test",
3036 .probe = mmc_test_probe,
3037 .remove = mmc_test_remove,
3038 .shutdown = mmc_test_shutdown,
3041 static int __init mmc_test_init(void)
3043 return mmc_register_driver(&mmc_driver);
3046 static void __exit mmc_test_exit(void)
3048 /* Clear stalled data if card is still plugged */
3049 mmc_test_free_result(NULL);
3050 mmc_test_free_dbgfs_file(NULL);
3052 mmc_unregister_driver(&mmc_driver);
3055 module_init(mmc_test_init);
3056 module_exit(mmc_test_exit);
3058 MODULE_LICENSE("GPL");
3059 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
3060 MODULE_AUTHOR("Pierre Ossman");