mmc: initialize struct mmc_command at declaration time
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / mmc / card / mmc_test.c
1 /*
2 * linux/drivers/mmc/card/mmc_test.c
3 *
4 * Copyright 2007-2008 Pierre Ossman
5 *
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.
10 */
11
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
18 #include <linux/scatterlist.h>
19 #include <linux/swap.h> /* For nr_free_buffer_pages() */
20 #include <linux/list.h>
21
22 #include <linux/debugfs.h>
23 #include <linux/uaccess.h>
24 #include <linux/seq_file.h>
25
26 #define RESULT_OK 0
27 #define RESULT_FAIL 1
28 #define RESULT_UNSUP_HOST 2
29 #define RESULT_UNSUP_CARD 3
30
31 #define BUFFER_ORDER 2
32 #define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
33
34 /*
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.
37 */
38 #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
39
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
44 */
45 struct mmc_test_pages {
46 struct page *page;
47 unsigned int order;
48 };
49
50 /**
51 * struct mmc_test_mem - allocated memory.
52 * @arr: array of allocations
53 * @cnt: number of allocations
54 */
55 struct mmc_test_mem {
56 struct mmc_test_pages *arr;
57 unsigned int cnt;
58 };
59
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
71 */
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;
82 };
83
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)
92 */
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;
100 };
101
102 /**
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
109 */
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;
116 };
117
118 /**
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
123 */
124 struct mmc_test_dbgfs_file {
125 struct list_head link;
126 struct mmc_card *card;
127 struct dentry *file;
128 };
129
130 /**
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
138 */
139 struct mmc_test_card {
140 struct mmc_card *card;
141
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;
149 };
150
151 /*******************************************************************/
152 /* General helper functions */
153 /*******************************************************************/
154
155 /*
156 * Configure correct block size in card
157 */
158 static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
159 {
160 return mmc_set_blocklen(test->card, size);
161 }
162
163 /*
164 * Fill in the mmc_request structure given a set of transfer parameters.
165 */
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)
169 {
170 BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
171
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;
178 }
179
180 mrq->cmd->arg = dev_addr;
181 if (!mmc_card_blockaddr(test->card))
182 mrq->cmd->arg <<= 9;
183
184 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
185
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;
192 }
193
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;
199
200 mmc_set_data_timeout(mrq->data, test->card);
201 }
202
203 static int mmc_test_busy(struct mmc_command *cmd)
204 {
205 return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
206 (R1_CURRENT_STATE(cmd->resp[0]) == 7);
207 }
208
209 /*
210 * Wait for the card to finish the busy state
211 */
212 static int mmc_test_wait_busy(struct mmc_test_card *test)
213 {
214 int ret, busy;
215 struct mmc_command cmd = {0};
216
217 busy = 0;
218 do {
219 memset(&cmd, 0, sizeof(struct mmc_command));
220
221 cmd.opcode = MMC_SEND_STATUS;
222 cmd.arg = test->card->rca << 16;
223 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
224
225 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
226 if (ret)
227 break;
228
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));
235 }
236 } while (mmc_test_busy(&cmd));
237
238 return ret;
239 }
240
241 /*
242 * Transfer a single sector of kernel addressable data
243 */
244 static int mmc_test_buffer_transfer(struct mmc_test_card *test,
245 u8 *buffer, unsigned addr, unsigned blksz, int write)
246 {
247 int ret;
248
249 struct mmc_request mrq;
250 struct mmc_command cmd = {0};
251 struct mmc_command stop = {0};
252 struct mmc_data data;
253
254 struct scatterlist sg;
255
256 memset(&mrq, 0, sizeof(struct mmc_request));
257 memset(&data, 0, sizeof(struct mmc_data));
258
259 mrq.cmd = &cmd;
260 mrq.data = &data;
261 mrq.stop = &stop;
262
263 sg_init_one(&sg, buffer, blksz);
264
265 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
266
267 mmc_wait_for_req(test->card->host, &mrq);
268
269 if (cmd.error)
270 return cmd.error;
271 if (data.error)
272 return data.error;
273
274 ret = mmc_test_wait_busy(test);
275 if (ret)
276 return ret;
277
278 return 0;
279 }
280
281 static void mmc_test_free_mem(struct mmc_test_mem *mem)
282 {
283 if (!mem)
284 return;
285 while (mem->cnt--)
286 __free_pages(mem->arr[mem->cnt].page,
287 mem->arr[mem->cnt].order);
288 kfree(mem->arr);
289 kfree(mem);
290 }
291
292 /*
293 * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
294 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
295 * not exceed a maximum number of segments and try not to make segments much
296 * bigger than maximum segment size.
297 */
298 static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
299 unsigned long max_sz,
300 unsigned int max_segs,
301 unsigned int max_seg_sz)
302 {
303 unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
304 unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
305 unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
306 unsigned long page_cnt = 0;
307 unsigned long limit = nr_free_buffer_pages() >> 4;
308 struct mmc_test_mem *mem;
309
310 if (max_page_cnt > limit)
311 max_page_cnt = limit;
312 if (min_page_cnt > max_page_cnt)
313 min_page_cnt = max_page_cnt;
314
315 if (max_seg_page_cnt > max_page_cnt)
316 max_seg_page_cnt = max_page_cnt;
317
318 if (max_segs > max_page_cnt)
319 max_segs = max_page_cnt;
320
321 mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
322 if (!mem)
323 return NULL;
324
325 mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_segs,
326 GFP_KERNEL);
327 if (!mem->arr)
328 goto out_free;
329
330 while (max_page_cnt) {
331 struct page *page;
332 unsigned int order;
333 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
334 __GFP_NORETRY;
335
336 order = get_order(max_seg_page_cnt << PAGE_SHIFT);
337 while (1) {
338 page = alloc_pages(flags, order);
339 if (page || !order)
340 break;
341 order -= 1;
342 }
343 if (!page) {
344 if (page_cnt < min_page_cnt)
345 goto out_free;
346 break;
347 }
348 mem->arr[mem->cnt].page = page;
349 mem->arr[mem->cnt].order = order;
350 mem->cnt += 1;
351 if (max_page_cnt <= (1UL << order))
352 break;
353 max_page_cnt -= 1UL << order;
354 page_cnt += 1UL << order;
355 if (mem->cnt >= max_segs) {
356 if (page_cnt < min_page_cnt)
357 goto out_free;
358 break;
359 }
360 }
361
362 return mem;
363
364 out_free:
365 mmc_test_free_mem(mem);
366 return NULL;
367 }
368
369 /*
370 * Map memory into a scatterlist. Optionally allow the same memory to be
371 * mapped more than once.
372 */
373 static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
374 struct scatterlist *sglist, int repeat,
375 unsigned int max_segs, unsigned int max_seg_sz,
376 unsigned int *sg_len)
377 {
378 struct scatterlist *sg = NULL;
379 unsigned int i;
380
381 sg_init_table(sglist, max_segs);
382
383 *sg_len = 0;
384 do {
385 for (i = 0; i < mem->cnt; i++) {
386 unsigned long len = PAGE_SIZE << mem->arr[i].order;
387
388 if (len > sz)
389 len = sz;
390 if (len > max_seg_sz)
391 len = max_seg_sz;
392 if (sg)
393 sg = sg_next(sg);
394 else
395 sg = sglist;
396 if (!sg)
397 return -EINVAL;
398 sg_set_page(sg, mem->arr[i].page, len, 0);
399 sz -= len;
400 *sg_len += 1;
401 if (!sz)
402 break;
403 }
404 } while (sz && repeat);
405
406 if (sz)
407 return -EINVAL;
408
409 if (sg)
410 sg_mark_end(sg);
411
412 return 0;
413 }
414
415 /*
416 * Map memory into a scatterlist so that no pages are contiguous. Allow the
417 * same memory to be mapped more than once.
418 */
419 static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
420 unsigned long sz,
421 struct scatterlist *sglist,
422 unsigned int max_segs,
423 unsigned int max_seg_sz,
424 unsigned int *sg_len)
425 {
426 struct scatterlist *sg = NULL;
427 unsigned int i = mem->cnt, cnt;
428 unsigned long len;
429 void *base, *addr, *last_addr = NULL;
430
431 sg_init_table(sglist, max_segs);
432
433 *sg_len = 0;
434 while (sz) {
435 base = page_address(mem->arr[--i].page);
436 cnt = 1 << mem->arr[i].order;
437 while (sz && cnt) {
438 addr = base + PAGE_SIZE * --cnt;
439 if (last_addr && last_addr + PAGE_SIZE == addr)
440 continue;
441 last_addr = addr;
442 len = PAGE_SIZE;
443 if (len > max_seg_sz)
444 len = max_seg_sz;
445 if (len > sz)
446 len = sz;
447 if (sg)
448 sg = sg_next(sg);
449 else
450 sg = sglist;
451 if (!sg)
452 return -EINVAL;
453 sg_set_page(sg, virt_to_page(addr), len, 0);
454 sz -= len;
455 *sg_len += 1;
456 }
457 if (i == 0)
458 i = mem->cnt;
459 }
460
461 if (sg)
462 sg_mark_end(sg);
463
464 return 0;
465 }
466
467 /*
468 * Calculate transfer rate in bytes per second.
469 */
470 static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
471 {
472 uint64_t ns;
473
474 ns = ts->tv_sec;
475 ns *= 1000000000;
476 ns += ts->tv_nsec;
477
478 bytes *= 1000000000;
479
480 while (ns > UINT_MAX) {
481 bytes >>= 1;
482 ns >>= 1;
483 }
484
485 if (!ns)
486 return 0;
487
488 do_div(bytes, (uint32_t)ns);
489
490 return bytes;
491 }
492
493 /*
494 * Save transfer results for future usage
495 */
496 static void mmc_test_save_transfer_result(struct mmc_test_card *test,
497 unsigned int count, unsigned int sectors, struct timespec ts,
498 unsigned int rate, unsigned int iops)
499 {
500 struct mmc_test_transfer_result *tr;
501
502 if (!test->gr)
503 return;
504
505 tr = kmalloc(sizeof(struct mmc_test_transfer_result), GFP_KERNEL);
506 if (!tr)
507 return;
508
509 tr->count = count;
510 tr->sectors = sectors;
511 tr->ts = ts;
512 tr->rate = rate;
513 tr->iops = iops;
514
515 list_add_tail(&tr->link, &test->gr->tr_lst);
516 }
517
518 /*
519 * Print the transfer rate.
520 */
521 static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
522 struct timespec *ts1, struct timespec *ts2)
523 {
524 unsigned int rate, iops, sectors = bytes >> 9;
525 struct timespec ts;
526
527 ts = timespec_sub(*ts2, *ts1);
528
529 rate = mmc_test_rate(bytes, &ts);
530 iops = mmc_test_rate(100, &ts); /* I/O ops per sec x 100 */
531
532 printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
533 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
534 mmc_hostname(test->card->host), sectors, sectors >> 1,
535 (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
536 (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024,
537 iops / 100, iops % 100);
538
539 mmc_test_save_transfer_result(test, 1, sectors, ts, rate, iops);
540 }
541
542 /*
543 * Print the average transfer rate.
544 */
545 static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
546 unsigned int count, struct timespec *ts1,
547 struct timespec *ts2)
548 {
549 unsigned int rate, iops, sectors = bytes >> 9;
550 uint64_t tot = bytes * count;
551 struct timespec ts;
552
553 ts = timespec_sub(*ts2, *ts1);
554
555 rate = mmc_test_rate(tot, &ts);
556 iops = mmc_test_rate(count * 100, &ts); /* I/O ops per sec x 100 */
557
558 printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
559 "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
560 "%u.%02u IOPS)\n",
561 mmc_hostname(test->card->host), count, sectors, count,
562 sectors >> 1, (sectors & 1 ? ".5" : ""),
563 (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
564 rate / 1000, rate / 1024, iops / 100, iops % 100);
565
566 mmc_test_save_transfer_result(test, count, sectors, ts, rate, iops);
567 }
568
569 /*
570 * Return the card size in sectors.
571 */
572 static unsigned int mmc_test_capacity(struct mmc_card *card)
573 {
574 if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
575 return card->ext_csd.sectors;
576 else
577 return card->csd.capacity << (card->csd.read_blkbits - 9);
578 }
579
580 /*******************************************************************/
581 /* Test preparation and cleanup */
582 /*******************************************************************/
583
584 /*
585 * Fill the first couple of sectors of the card with known data
586 * so that bad reads/writes can be detected
587 */
588 static int __mmc_test_prepare(struct mmc_test_card *test, int write)
589 {
590 int ret, i;
591
592 ret = mmc_test_set_blksize(test, 512);
593 if (ret)
594 return ret;
595
596 if (write)
597 memset(test->buffer, 0xDF, 512);
598 else {
599 for (i = 0;i < 512;i++)
600 test->buffer[i] = i;
601 }
602
603 for (i = 0;i < BUFFER_SIZE / 512;i++) {
604 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
605 if (ret)
606 return ret;
607 }
608
609 return 0;
610 }
611
612 static int mmc_test_prepare_write(struct mmc_test_card *test)
613 {
614 return __mmc_test_prepare(test, 1);
615 }
616
617 static int mmc_test_prepare_read(struct mmc_test_card *test)
618 {
619 return __mmc_test_prepare(test, 0);
620 }
621
622 static int mmc_test_cleanup(struct mmc_test_card *test)
623 {
624 int ret, i;
625
626 ret = mmc_test_set_blksize(test, 512);
627 if (ret)
628 return ret;
629
630 memset(test->buffer, 0, 512);
631
632 for (i = 0;i < BUFFER_SIZE / 512;i++) {
633 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
634 if (ret)
635 return ret;
636 }
637
638 return 0;
639 }
640
641 /*******************************************************************/
642 /* Test execution helpers */
643 /*******************************************************************/
644
645 /*
646 * Modifies the mmc_request to perform the "short transfer" tests
647 */
648 static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
649 struct mmc_request *mrq, int write)
650 {
651 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
652
653 if (mrq->data->blocks > 1) {
654 mrq->cmd->opcode = write ?
655 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
656 mrq->stop = NULL;
657 } else {
658 mrq->cmd->opcode = MMC_SEND_STATUS;
659 mrq->cmd->arg = test->card->rca << 16;
660 }
661 }
662
663 /*
664 * Checks that a normal transfer didn't have any errors
665 */
666 static int mmc_test_check_result(struct mmc_test_card *test,
667 struct mmc_request *mrq)
668 {
669 int ret;
670
671 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
672
673 ret = 0;
674
675 if (!ret && mrq->cmd->error)
676 ret = mrq->cmd->error;
677 if (!ret && mrq->data->error)
678 ret = mrq->data->error;
679 if (!ret && mrq->stop && mrq->stop->error)
680 ret = mrq->stop->error;
681 if (!ret && mrq->data->bytes_xfered !=
682 mrq->data->blocks * mrq->data->blksz)
683 ret = RESULT_FAIL;
684
685 if (ret == -EINVAL)
686 ret = RESULT_UNSUP_HOST;
687
688 return ret;
689 }
690
691 /*
692 * Checks that a "short transfer" behaved as expected
693 */
694 static int mmc_test_check_broken_result(struct mmc_test_card *test,
695 struct mmc_request *mrq)
696 {
697 int ret;
698
699 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
700
701 ret = 0;
702
703 if (!ret && mrq->cmd->error)
704 ret = mrq->cmd->error;
705 if (!ret && mrq->data->error == 0)
706 ret = RESULT_FAIL;
707 if (!ret && mrq->data->error != -ETIMEDOUT)
708 ret = mrq->data->error;
709 if (!ret && mrq->stop && mrq->stop->error)
710 ret = mrq->stop->error;
711 if (mrq->data->blocks > 1) {
712 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
713 ret = RESULT_FAIL;
714 } else {
715 if (!ret && mrq->data->bytes_xfered > 0)
716 ret = RESULT_FAIL;
717 }
718
719 if (ret == -EINVAL)
720 ret = RESULT_UNSUP_HOST;
721
722 return ret;
723 }
724
725 /*
726 * Tests a basic transfer with certain parameters
727 */
728 static int mmc_test_simple_transfer(struct mmc_test_card *test,
729 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
730 unsigned blocks, unsigned blksz, int write)
731 {
732 struct mmc_request mrq;
733 struct mmc_command cmd = {0};
734 struct mmc_command stop = {0};
735 struct mmc_data data;
736
737 memset(&mrq, 0, sizeof(struct mmc_request));
738 memset(&data, 0, sizeof(struct mmc_data));
739
740 mrq.cmd = &cmd;
741 mrq.data = &data;
742 mrq.stop = &stop;
743
744 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
745 blocks, blksz, write);
746
747 mmc_wait_for_req(test->card->host, &mrq);
748
749 mmc_test_wait_busy(test);
750
751 return mmc_test_check_result(test, &mrq);
752 }
753
754 /*
755 * Tests a transfer where the card will fail completely or partly
756 */
757 static int mmc_test_broken_transfer(struct mmc_test_card *test,
758 unsigned blocks, unsigned blksz, int write)
759 {
760 struct mmc_request mrq;
761 struct mmc_command cmd = {0};
762 struct mmc_command stop = {0};
763 struct mmc_data data;
764
765 struct scatterlist sg;
766
767 memset(&mrq, 0, sizeof(struct mmc_request));
768 memset(&data, 0, sizeof(struct mmc_data));
769
770 mrq.cmd = &cmd;
771 mrq.data = &data;
772 mrq.stop = &stop;
773
774 sg_init_one(&sg, test->buffer, blocks * blksz);
775
776 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
777 mmc_test_prepare_broken_mrq(test, &mrq, write);
778
779 mmc_wait_for_req(test->card->host, &mrq);
780
781 mmc_test_wait_busy(test);
782
783 return mmc_test_check_broken_result(test, &mrq);
784 }
785
786 /*
787 * Does a complete transfer test where data is also validated
788 *
789 * Note: mmc_test_prepare() must have been done before this call
790 */
791 static int mmc_test_transfer(struct mmc_test_card *test,
792 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
793 unsigned blocks, unsigned blksz, int write)
794 {
795 int ret, i;
796 unsigned long flags;
797
798 if (write) {
799 for (i = 0;i < blocks * blksz;i++)
800 test->scratch[i] = i;
801 } else {
802 memset(test->scratch, 0, BUFFER_SIZE);
803 }
804 local_irq_save(flags);
805 sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
806 local_irq_restore(flags);
807
808 ret = mmc_test_set_blksize(test, blksz);
809 if (ret)
810 return ret;
811
812 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
813 blocks, blksz, write);
814 if (ret)
815 return ret;
816
817 if (write) {
818 int sectors;
819
820 ret = mmc_test_set_blksize(test, 512);
821 if (ret)
822 return ret;
823
824 sectors = (blocks * blksz + 511) / 512;
825 if ((sectors * 512) == (blocks * blksz))
826 sectors++;
827
828 if ((sectors * 512) > BUFFER_SIZE)
829 return -EINVAL;
830
831 memset(test->buffer, 0, sectors * 512);
832
833 for (i = 0;i < sectors;i++) {
834 ret = mmc_test_buffer_transfer(test,
835 test->buffer + i * 512,
836 dev_addr + i, 512, 0);
837 if (ret)
838 return ret;
839 }
840
841 for (i = 0;i < blocks * blksz;i++) {
842 if (test->buffer[i] != (u8)i)
843 return RESULT_FAIL;
844 }
845
846 for (;i < sectors * 512;i++) {
847 if (test->buffer[i] != 0xDF)
848 return RESULT_FAIL;
849 }
850 } else {
851 local_irq_save(flags);
852 sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
853 local_irq_restore(flags);
854 for (i = 0;i < blocks * blksz;i++) {
855 if (test->scratch[i] != (u8)i)
856 return RESULT_FAIL;
857 }
858 }
859
860 return 0;
861 }
862
863 /*******************************************************************/
864 /* Tests */
865 /*******************************************************************/
866
867 struct mmc_test_case {
868 const char *name;
869
870 int (*prepare)(struct mmc_test_card *);
871 int (*run)(struct mmc_test_card *);
872 int (*cleanup)(struct mmc_test_card *);
873 };
874
875 static int mmc_test_basic_write(struct mmc_test_card *test)
876 {
877 int ret;
878 struct scatterlist sg;
879
880 ret = mmc_test_set_blksize(test, 512);
881 if (ret)
882 return ret;
883
884 sg_init_one(&sg, test->buffer, 512);
885
886 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
887 if (ret)
888 return ret;
889
890 return 0;
891 }
892
893 static int mmc_test_basic_read(struct mmc_test_card *test)
894 {
895 int ret;
896 struct scatterlist sg;
897
898 ret = mmc_test_set_blksize(test, 512);
899 if (ret)
900 return ret;
901
902 sg_init_one(&sg, test->buffer, 512);
903
904 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
905 if (ret)
906 return ret;
907
908 return 0;
909 }
910
911 static int mmc_test_verify_write(struct mmc_test_card *test)
912 {
913 int ret;
914 struct scatterlist sg;
915
916 sg_init_one(&sg, test->buffer, 512);
917
918 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
919 if (ret)
920 return ret;
921
922 return 0;
923 }
924
925 static int mmc_test_verify_read(struct mmc_test_card *test)
926 {
927 int ret;
928 struct scatterlist sg;
929
930 sg_init_one(&sg, test->buffer, 512);
931
932 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
933 if (ret)
934 return ret;
935
936 return 0;
937 }
938
939 static int mmc_test_multi_write(struct mmc_test_card *test)
940 {
941 int ret;
942 unsigned int size;
943 struct scatterlist sg;
944
945 if (test->card->host->max_blk_count == 1)
946 return RESULT_UNSUP_HOST;
947
948 size = PAGE_SIZE * 2;
949 size = min(size, test->card->host->max_req_size);
950 size = min(size, test->card->host->max_seg_size);
951 size = min(size, test->card->host->max_blk_count * 512);
952
953 if (size < 1024)
954 return RESULT_UNSUP_HOST;
955
956 sg_init_one(&sg, test->buffer, size);
957
958 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
959 if (ret)
960 return ret;
961
962 return 0;
963 }
964
965 static int mmc_test_multi_read(struct mmc_test_card *test)
966 {
967 int ret;
968 unsigned int size;
969 struct scatterlist sg;
970
971 if (test->card->host->max_blk_count == 1)
972 return RESULT_UNSUP_HOST;
973
974 size = PAGE_SIZE * 2;
975 size = min(size, test->card->host->max_req_size);
976 size = min(size, test->card->host->max_seg_size);
977 size = min(size, test->card->host->max_blk_count * 512);
978
979 if (size < 1024)
980 return RESULT_UNSUP_HOST;
981
982 sg_init_one(&sg, test->buffer, size);
983
984 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
985 if (ret)
986 return ret;
987
988 return 0;
989 }
990
991 static int mmc_test_pow2_write(struct mmc_test_card *test)
992 {
993 int ret, i;
994 struct scatterlist sg;
995
996 if (!test->card->csd.write_partial)
997 return RESULT_UNSUP_CARD;
998
999 for (i = 1; i < 512;i <<= 1) {
1000 sg_init_one(&sg, test->buffer, i);
1001 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1002 if (ret)
1003 return ret;
1004 }
1005
1006 return 0;
1007 }
1008
1009 static int mmc_test_pow2_read(struct mmc_test_card *test)
1010 {
1011 int ret, i;
1012 struct scatterlist sg;
1013
1014 if (!test->card->csd.read_partial)
1015 return RESULT_UNSUP_CARD;
1016
1017 for (i = 1; i < 512;i <<= 1) {
1018 sg_init_one(&sg, test->buffer, i);
1019 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1020 if (ret)
1021 return ret;
1022 }
1023
1024 return 0;
1025 }
1026
1027 static int mmc_test_weird_write(struct mmc_test_card *test)
1028 {
1029 int ret, i;
1030 struct scatterlist sg;
1031
1032 if (!test->card->csd.write_partial)
1033 return RESULT_UNSUP_CARD;
1034
1035 for (i = 3; i < 512;i += 7) {
1036 sg_init_one(&sg, test->buffer, i);
1037 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
1038 if (ret)
1039 return ret;
1040 }
1041
1042 return 0;
1043 }
1044
1045 static int mmc_test_weird_read(struct mmc_test_card *test)
1046 {
1047 int ret, i;
1048 struct scatterlist sg;
1049
1050 if (!test->card->csd.read_partial)
1051 return RESULT_UNSUP_CARD;
1052
1053 for (i = 3; i < 512;i += 7) {
1054 sg_init_one(&sg, test->buffer, i);
1055 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
1056 if (ret)
1057 return ret;
1058 }
1059
1060 return 0;
1061 }
1062
1063 static int mmc_test_align_write(struct mmc_test_card *test)
1064 {
1065 int ret, i;
1066 struct scatterlist sg;
1067
1068 for (i = 1;i < 4;i++) {
1069 sg_init_one(&sg, test->buffer + i, 512);
1070 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1071 if (ret)
1072 return ret;
1073 }
1074
1075 return 0;
1076 }
1077
1078 static int mmc_test_align_read(struct mmc_test_card *test)
1079 {
1080 int ret, i;
1081 struct scatterlist sg;
1082
1083 for (i = 1;i < 4;i++) {
1084 sg_init_one(&sg, test->buffer + i, 512);
1085 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1086 if (ret)
1087 return ret;
1088 }
1089
1090 return 0;
1091 }
1092
1093 static int mmc_test_align_multi_write(struct mmc_test_card *test)
1094 {
1095 int ret, i;
1096 unsigned int size;
1097 struct scatterlist sg;
1098
1099 if (test->card->host->max_blk_count == 1)
1100 return RESULT_UNSUP_HOST;
1101
1102 size = PAGE_SIZE * 2;
1103 size = min(size, test->card->host->max_req_size);
1104 size = min(size, test->card->host->max_seg_size);
1105 size = min(size, test->card->host->max_blk_count * 512);
1106
1107 if (size < 1024)
1108 return RESULT_UNSUP_HOST;
1109
1110 for (i = 1;i < 4;i++) {
1111 sg_init_one(&sg, test->buffer + i, size);
1112 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1113 if (ret)
1114 return ret;
1115 }
1116
1117 return 0;
1118 }
1119
1120 static int mmc_test_align_multi_read(struct mmc_test_card *test)
1121 {
1122 int ret, i;
1123 unsigned int size;
1124 struct scatterlist sg;
1125
1126 if (test->card->host->max_blk_count == 1)
1127 return RESULT_UNSUP_HOST;
1128
1129 size = PAGE_SIZE * 2;
1130 size = min(size, test->card->host->max_req_size);
1131 size = min(size, test->card->host->max_seg_size);
1132 size = min(size, test->card->host->max_blk_count * 512);
1133
1134 if (size < 1024)
1135 return RESULT_UNSUP_HOST;
1136
1137 for (i = 1;i < 4;i++) {
1138 sg_init_one(&sg, test->buffer + i, size);
1139 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1140 if (ret)
1141 return ret;
1142 }
1143
1144 return 0;
1145 }
1146
1147 static int mmc_test_xfersize_write(struct mmc_test_card *test)
1148 {
1149 int ret;
1150
1151 ret = mmc_test_set_blksize(test, 512);
1152 if (ret)
1153 return ret;
1154
1155 ret = mmc_test_broken_transfer(test, 1, 512, 1);
1156 if (ret)
1157 return ret;
1158
1159 return 0;
1160 }
1161
1162 static int mmc_test_xfersize_read(struct mmc_test_card *test)
1163 {
1164 int ret;
1165
1166 ret = mmc_test_set_blksize(test, 512);
1167 if (ret)
1168 return ret;
1169
1170 ret = mmc_test_broken_transfer(test, 1, 512, 0);
1171 if (ret)
1172 return ret;
1173
1174 return 0;
1175 }
1176
1177 static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
1178 {
1179 int ret;
1180
1181 if (test->card->host->max_blk_count == 1)
1182 return RESULT_UNSUP_HOST;
1183
1184 ret = mmc_test_set_blksize(test, 512);
1185 if (ret)
1186 return ret;
1187
1188 ret = mmc_test_broken_transfer(test, 2, 512, 1);
1189 if (ret)
1190 return ret;
1191
1192 return 0;
1193 }
1194
1195 static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
1196 {
1197 int ret;
1198
1199 if (test->card->host->max_blk_count == 1)
1200 return RESULT_UNSUP_HOST;
1201
1202 ret = mmc_test_set_blksize(test, 512);
1203 if (ret)
1204 return ret;
1205
1206 ret = mmc_test_broken_transfer(test, 2, 512, 0);
1207 if (ret)
1208 return ret;
1209
1210 return 0;
1211 }
1212
1213 #ifdef CONFIG_HIGHMEM
1214
1215 static int mmc_test_write_high(struct mmc_test_card *test)
1216 {
1217 int ret;
1218 struct scatterlist sg;
1219
1220 sg_init_table(&sg, 1);
1221 sg_set_page(&sg, test->highmem, 512, 0);
1222
1223 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1224 if (ret)
1225 return ret;
1226
1227 return 0;
1228 }
1229
1230 static int mmc_test_read_high(struct mmc_test_card *test)
1231 {
1232 int ret;
1233 struct scatterlist sg;
1234
1235 sg_init_table(&sg, 1);
1236 sg_set_page(&sg, test->highmem, 512, 0);
1237
1238 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1239 if (ret)
1240 return ret;
1241
1242 return 0;
1243 }
1244
1245 static int mmc_test_multi_write_high(struct mmc_test_card *test)
1246 {
1247 int ret;
1248 unsigned int size;
1249 struct scatterlist sg;
1250
1251 if (test->card->host->max_blk_count == 1)
1252 return RESULT_UNSUP_HOST;
1253
1254 size = PAGE_SIZE * 2;
1255 size = min(size, test->card->host->max_req_size);
1256 size = min(size, test->card->host->max_seg_size);
1257 size = min(size, test->card->host->max_blk_count * 512);
1258
1259 if (size < 1024)
1260 return RESULT_UNSUP_HOST;
1261
1262 sg_init_table(&sg, 1);
1263 sg_set_page(&sg, test->highmem, size, 0);
1264
1265 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1266 if (ret)
1267 return ret;
1268
1269 return 0;
1270 }
1271
1272 static int mmc_test_multi_read_high(struct mmc_test_card *test)
1273 {
1274 int ret;
1275 unsigned int size;
1276 struct scatterlist sg;
1277
1278 if (test->card->host->max_blk_count == 1)
1279 return RESULT_UNSUP_HOST;
1280
1281 size = PAGE_SIZE * 2;
1282 size = min(size, test->card->host->max_req_size);
1283 size = min(size, test->card->host->max_seg_size);
1284 size = min(size, test->card->host->max_blk_count * 512);
1285
1286 if (size < 1024)
1287 return RESULT_UNSUP_HOST;
1288
1289 sg_init_table(&sg, 1);
1290 sg_set_page(&sg, test->highmem, size, 0);
1291
1292 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1293 if (ret)
1294 return ret;
1295
1296 return 0;
1297 }
1298
1299 #else
1300
1301 static int mmc_test_no_highmem(struct mmc_test_card *test)
1302 {
1303 printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
1304 mmc_hostname(test->card->host));
1305 return 0;
1306 }
1307
1308 #endif /* CONFIG_HIGHMEM */
1309
1310 /*
1311 * Map sz bytes so that it can be transferred.
1312 */
1313 static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
1314 int max_scatter)
1315 {
1316 struct mmc_test_area *t = &test->area;
1317 int err;
1318
1319 t->blocks = sz >> 9;
1320
1321 if (max_scatter) {
1322 err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
1323 t->max_segs, t->max_seg_sz,
1324 &t->sg_len);
1325 } else {
1326 err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
1327 t->max_seg_sz, &t->sg_len);
1328 }
1329 if (err)
1330 printk(KERN_INFO "%s: Failed to map sg list\n",
1331 mmc_hostname(test->card->host));
1332 return err;
1333 }
1334
1335 /*
1336 * Transfer bytes mapped by mmc_test_area_map().
1337 */
1338 static int mmc_test_area_transfer(struct mmc_test_card *test,
1339 unsigned int dev_addr, int write)
1340 {
1341 struct mmc_test_area *t = &test->area;
1342
1343 return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
1344 t->blocks, 512, write);
1345 }
1346
1347 /*
1348 * Map and transfer bytes.
1349 */
1350 static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
1351 unsigned int dev_addr, int write, int max_scatter,
1352 int timed)
1353 {
1354 struct timespec ts1, ts2;
1355 int ret;
1356
1357 /*
1358 * In the case of a maximally scattered transfer, the maximum transfer
1359 * size is further limited by using PAGE_SIZE segments.
1360 */
1361 if (max_scatter) {
1362 struct mmc_test_area *t = &test->area;
1363 unsigned long max_tfr;
1364
1365 if (t->max_seg_sz >= PAGE_SIZE)
1366 max_tfr = t->max_segs * PAGE_SIZE;
1367 else
1368 max_tfr = t->max_segs * t->max_seg_sz;
1369 if (sz > max_tfr)
1370 sz = max_tfr;
1371 }
1372
1373 ret = mmc_test_area_map(test, sz, max_scatter);
1374 if (ret)
1375 return ret;
1376
1377 if (timed)
1378 getnstimeofday(&ts1);
1379
1380 ret = mmc_test_area_transfer(test, dev_addr, write);
1381 if (ret)
1382 return ret;
1383
1384 if (timed)
1385 getnstimeofday(&ts2);
1386
1387 if (timed)
1388 mmc_test_print_rate(test, sz, &ts1, &ts2);
1389
1390 return 0;
1391 }
1392
1393 /*
1394 * Write the test area entirely.
1395 */
1396 static int mmc_test_area_fill(struct mmc_test_card *test)
1397 {
1398 return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
1399 1, 0, 0);
1400 }
1401
1402 /*
1403 * Erase the test area entirely.
1404 */
1405 static int mmc_test_area_erase(struct mmc_test_card *test)
1406 {
1407 struct mmc_test_area *t = &test->area;
1408
1409 if (!mmc_can_erase(test->card))
1410 return 0;
1411
1412 return mmc_erase(test->card, t->dev_addr, test->area.max_sz >> 9,
1413 MMC_ERASE_ARG);
1414 }
1415
1416 /*
1417 * Cleanup struct mmc_test_area.
1418 */
1419 static int mmc_test_area_cleanup(struct mmc_test_card *test)
1420 {
1421 struct mmc_test_area *t = &test->area;
1422
1423 kfree(t->sg);
1424 mmc_test_free_mem(t->mem);
1425
1426 return 0;
1427 }
1428
1429 /*
1430 * Initialize an area for testing large transfers. The test area is set to the
1431 * middle of the card because cards may have different charateristics at the
1432 * front (for FAT file system optimization). Optionally, the area is erased
1433 * (if the card supports it) which may improve write performance. Optionally,
1434 * the area is filled with data for subsequent read tests.
1435 */
1436 static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
1437 {
1438 struct mmc_test_area *t = &test->area;
1439 unsigned long min_sz = 64 * 1024, sz;
1440 int ret;
1441
1442 ret = mmc_test_set_blksize(test, 512);
1443 if (ret)
1444 return ret;
1445
1446 /* Make the test area size about 4MiB */
1447 sz = (unsigned long)test->card->pref_erase << 9;
1448 t->max_sz = sz;
1449 while (t->max_sz < 4 * 1024 * 1024)
1450 t->max_sz += sz;
1451 while (t->max_sz > TEST_AREA_MAX_SIZE && t->max_sz > sz)
1452 t->max_sz -= sz;
1453
1454 t->max_segs = test->card->host->max_segs;
1455 t->max_seg_sz = test->card->host->max_seg_size;
1456
1457 t->max_tfr = t->max_sz;
1458 if (t->max_tfr >> 9 > test->card->host->max_blk_count)
1459 t->max_tfr = test->card->host->max_blk_count << 9;
1460 if (t->max_tfr > test->card->host->max_req_size)
1461 t->max_tfr = test->card->host->max_req_size;
1462 if (t->max_tfr / t->max_seg_sz > t->max_segs)
1463 t->max_tfr = t->max_segs * t->max_seg_sz;
1464
1465 /*
1466 * Try to allocate enough memory for a max. sized transfer. Less is OK
1467 * because the same memory can be mapped into the scatterlist more than
1468 * once. Also, take into account the limits imposed on scatterlist
1469 * segments by the host driver.
1470 */
1471 t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
1472 t->max_seg_sz);
1473 if (!t->mem)
1474 return -ENOMEM;
1475
1476 t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
1477 if (!t->sg) {
1478 ret = -ENOMEM;
1479 goto out_free;
1480 }
1481
1482 t->dev_addr = mmc_test_capacity(test->card) / 2;
1483 t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
1484
1485 if (erase) {
1486 ret = mmc_test_area_erase(test);
1487 if (ret)
1488 goto out_free;
1489 }
1490
1491 if (fill) {
1492 ret = mmc_test_area_fill(test);
1493 if (ret)
1494 goto out_free;
1495 }
1496
1497 return 0;
1498
1499 out_free:
1500 mmc_test_area_cleanup(test);
1501 return ret;
1502 }
1503
1504 /*
1505 * Prepare for large transfers. Do not erase the test area.
1506 */
1507 static int mmc_test_area_prepare(struct mmc_test_card *test)
1508 {
1509 return mmc_test_area_init(test, 0, 0);
1510 }
1511
1512 /*
1513 * Prepare for large transfers. Do erase the test area.
1514 */
1515 static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
1516 {
1517 return mmc_test_area_init(test, 1, 0);
1518 }
1519
1520 /*
1521 * Prepare for large transfers. Erase and fill the test area.
1522 */
1523 static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
1524 {
1525 return mmc_test_area_init(test, 1, 1);
1526 }
1527
1528 /*
1529 * Test best-case performance. Best-case performance is expected from
1530 * a single large transfer.
1531 *
1532 * An additional option (max_scatter) allows the measurement of the same
1533 * transfer but with no contiguous pages in the scatter list. This tests
1534 * the efficiency of DMA to handle scattered pages.
1535 */
1536 static int mmc_test_best_performance(struct mmc_test_card *test, int write,
1537 int max_scatter)
1538 {
1539 return mmc_test_area_io(test, test->area.max_tfr, test->area.dev_addr,
1540 write, max_scatter, 1);
1541 }
1542
1543 /*
1544 * Best-case read performance.
1545 */
1546 static int mmc_test_best_read_performance(struct mmc_test_card *test)
1547 {
1548 return mmc_test_best_performance(test, 0, 0);
1549 }
1550
1551 /*
1552 * Best-case write performance.
1553 */
1554 static int mmc_test_best_write_performance(struct mmc_test_card *test)
1555 {
1556 return mmc_test_best_performance(test, 1, 0);
1557 }
1558
1559 /*
1560 * Best-case read performance into scattered pages.
1561 */
1562 static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
1563 {
1564 return mmc_test_best_performance(test, 0, 1);
1565 }
1566
1567 /*
1568 * Best-case write performance from scattered pages.
1569 */
1570 static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
1571 {
1572 return mmc_test_best_performance(test, 1, 1);
1573 }
1574
1575 /*
1576 * Single read performance by transfer size.
1577 */
1578 static int mmc_test_profile_read_perf(struct mmc_test_card *test)
1579 {
1580 unsigned long sz;
1581 unsigned int dev_addr;
1582 int ret;
1583
1584 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1585 dev_addr = test->area.dev_addr + (sz >> 9);
1586 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1587 if (ret)
1588 return ret;
1589 }
1590 sz = test->area.max_tfr;
1591 dev_addr = test->area.dev_addr;
1592 return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1593 }
1594
1595 /*
1596 * Single write performance by transfer size.
1597 */
1598 static int mmc_test_profile_write_perf(struct mmc_test_card *test)
1599 {
1600 unsigned long sz;
1601 unsigned int dev_addr;
1602 int ret;
1603
1604 ret = mmc_test_area_erase(test);
1605 if (ret)
1606 return ret;
1607 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1608 dev_addr = test->area.dev_addr + (sz >> 9);
1609 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1610 if (ret)
1611 return ret;
1612 }
1613 ret = mmc_test_area_erase(test);
1614 if (ret)
1615 return ret;
1616 sz = test->area.max_tfr;
1617 dev_addr = test->area.dev_addr;
1618 return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1619 }
1620
1621 /*
1622 * Single trim performance by transfer size.
1623 */
1624 static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
1625 {
1626 unsigned long sz;
1627 unsigned int dev_addr;
1628 struct timespec ts1, ts2;
1629 int ret;
1630
1631 if (!mmc_can_trim(test->card))
1632 return RESULT_UNSUP_CARD;
1633
1634 if (!mmc_can_erase(test->card))
1635 return RESULT_UNSUP_HOST;
1636
1637 for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
1638 dev_addr = test->area.dev_addr + (sz >> 9);
1639 getnstimeofday(&ts1);
1640 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1641 if (ret)
1642 return ret;
1643 getnstimeofday(&ts2);
1644 mmc_test_print_rate(test, sz, &ts1, &ts2);
1645 }
1646 dev_addr = test->area.dev_addr;
1647 getnstimeofday(&ts1);
1648 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1649 if (ret)
1650 return ret;
1651 getnstimeofday(&ts2);
1652 mmc_test_print_rate(test, sz, &ts1, &ts2);
1653 return 0;
1654 }
1655
1656 static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
1657 {
1658 unsigned int dev_addr, i, cnt;
1659 struct timespec ts1, ts2;
1660 int ret;
1661
1662 cnt = test->area.max_sz / sz;
1663 dev_addr = test->area.dev_addr;
1664 getnstimeofday(&ts1);
1665 for (i = 0; i < cnt; i++) {
1666 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
1667 if (ret)
1668 return ret;
1669 dev_addr += (sz >> 9);
1670 }
1671 getnstimeofday(&ts2);
1672 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1673 return 0;
1674 }
1675
1676 /*
1677 * Consecutive read performance by transfer size.
1678 */
1679 static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
1680 {
1681 unsigned long sz;
1682 int ret;
1683
1684 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1685 ret = mmc_test_seq_read_perf(test, sz);
1686 if (ret)
1687 return ret;
1688 }
1689 sz = test->area.max_tfr;
1690 return mmc_test_seq_read_perf(test, sz);
1691 }
1692
1693 static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
1694 {
1695 unsigned int dev_addr, i, cnt;
1696 struct timespec ts1, ts2;
1697 int ret;
1698
1699 ret = mmc_test_area_erase(test);
1700 if (ret)
1701 return ret;
1702 cnt = test->area.max_sz / sz;
1703 dev_addr = test->area.dev_addr;
1704 getnstimeofday(&ts1);
1705 for (i = 0; i < cnt; i++) {
1706 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
1707 if (ret)
1708 return ret;
1709 dev_addr += (sz >> 9);
1710 }
1711 getnstimeofday(&ts2);
1712 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1713 return 0;
1714 }
1715
1716 /*
1717 * Consecutive write performance by transfer size.
1718 */
1719 static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
1720 {
1721 unsigned long sz;
1722 int ret;
1723
1724 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1725 ret = mmc_test_seq_write_perf(test, sz);
1726 if (ret)
1727 return ret;
1728 }
1729 sz = test->area.max_tfr;
1730 return mmc_test_seq_write_perf(test, sz);
1731 }
1732
1733 /*
1734 * Consecutive trim performance by transfer size.
1735 */
1736 static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
1737 {
1738 unsigned long sz;
1739 unsigned int dev_addr, i, cnt;
1740 struct timespec ts1, ts2;
1741 int ret;
1742
1743 if (!mmc_can_trim(test->card))
1744 return RESULT_UNSUP_CARD;
1745
1746 if (!mmc_can_erase(test->card))
1747 return RESULT_UNSUP_HOST;
1748
1749 for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
1750 ret = mmc_test_area_erase(test);
1751 if (ret)
1752 return ret;
1753 ret = mmc_test_area_fill(test);
1754 if (ret)
1755 return ret;
1756 cnt = test->area.max_sz / sz;
1757 dev_addr = test->area.dev_addr;
1758 getnstimeofday(&ts1);
1759 for (i = 0; i < cnt; i++) {
1760 ret = mmc_erase(test->card, dev_addr, sz >> 9,
1761 MMC_TRIM_ARG);
1762 if (ret)
1763 return ret;
1764 dev_addr += (sz >> 9);
1765 }
1766 getnstimeofday(&ts2);
1767 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1768 }
1769 return 0;
1770 }
1771
1772 static unsigned int rnd_next = 1;
1773
1774 static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt)
1775 {
1776 uint64_t r;
1777
1778 rnd_next = rnd_next * 1103515245 + 12345;
1779 r = (rnd_next >> 16) & 0x7fff;
1780 return (r * rnd_cnt) >> 15;
1781 }
1782
1783 static int mmc_test_rnd_perf(struct mmc_test_card *test, int write, int print,
1784 unsigned long sz)
1785 {
1786 unsigned int dev_addr, cnt, rnd_addr, range1, range2, last_ea = 0, ea;
1787 unsigned int ssz;
1788 struct timespec ts1, ts2, ts;
1789 int ret;
1790
1791 ssz = sz >> 9;
1792
1793 rnd_addr = mmc_test_capacity(test->card) / 4;
1794 range1 = rnd_addr / test->card->pref_erase;
1795 range2 = range1 / ssz;
1796
1797 getnstimeofday(&ts1);
1798 for (cnt = 0; cnt < UINT_MAX; cnt++) {
1799 getnstimeofday(&ts2);
1800 ts = timespec_sub(ts2, ts1);
1801 if (ts.tv_sec >= 10)
1802 break;
1803 ea = mmc_test_rnd_num(range1);
1804 if (ea == last_ea)
1805 ea -= 1;
1806 last_ea = ea;
1807 dev_addr = rnd_addr + test->card->pref_erase * ea +
1808 ssz * mmc_test_rnd_num(range2);
1809 ret = mmc_test_area_io(test, sz, dev_addr, write, 0, 0);
1810 if (ret)
1811 return ret;
1812 }
1813 if (print)
1814 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1815 return 0;
1816 }
1817
1818 static int mmc_test_random_perf(struct mmc_test_card *test, int write)
1819 {
1820 unsigned int next;
1821 unsigned long sz;
1822 int ret;
1823
1824 for (sz = 512; sz < test->area.max_tfr; sz <<= 1) {
1825 /*
1826 * When writing, try to get more consistent results by running
1827 * the test twice with exactly the same I/O but outputting the
1828 * results only for the 2nd run.
1829 */
1830 if (write) {
1831 next = rnd_next;
1832 ret = mmc_test_rnd_perf(test, write, 0, sz);
1833 if (ret)
1834 return ret;
1835 rnd_next = next;
1836 }
1837 ret = mmc_test_rnd_perf(test, write, 1, sz);
1838 if (ret)
1839 return ret;
1840 }
1841 sz = test->area.max_tfr;
1842 if (write) {
1843 next = rnd_next;
1844 ret = mmc_test_rnd_perf(test, write, 0, sz);
1845 if (ret)
1846 return ret;
1847 rnd_next = next;
1848 }
1849 return mmc_test_rnd_perf(test, write, 1, sz);
1850 }
1851
1852 /*
1853 * Random read performance by transfer size.
1854 */
1855 static int mmc_test_random_read_perf(struct mmc_test_card *test)
1856 {
1857 return mmc_test_random_perf(test, 0);
1858 }
1859
1860 /*
1861 * Random write performance by transfer size.
1862 */
1863 static int mmc_test_random_write_perf(struct mmc_test_card *test)
1864 {
1865 return mmc_test_random_perf(test, 1);
1866 }
1867
1868 static int mmc_test_seq_perf(struct mmc_test_card *test, int write,
1869 unsigned int tot_sz, int max_scatter)
1870 {
1871 unsigned int dev_addr, i, cnt, sz, ssz;
1872 struct timespec ts1, ts2;
1873 int ret;
1874
1875 sz = test->area.max_tfr;
1876 /*
1877 * In the case of a maximally scattered transfer, the maximum transfer
1878 * size is further limited by using PAGE_SIZE segments.
1879 */
1880 if (max_scatter) {
1881 struct mmc_test_area *t = &test->area;
1882 unsigned long max_tfr;
1883
1884 if (t->max_seg_sz >= PAGE_SIZE)
1885 max_tfr = t->max_segs * PAGE_SIZE;
1886 else
1887 max_tfr = t->max_segs * t->max_seg_sz;
1888 if (sz > max_tfr)
1889 sz = max_tfr;
1890 }
1891
1892 ssz = sz >> 9;
1893 dev_addr = mmc_test_capacity(test->card) / 4;
1894 if (tot_sz > dev_addr << 9)
1895 tot_sz = dev_addr << 9;
1896 cnt = tot_sz / sz;
1897 dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
1898
1899 getnstimeofday(&ts1);
1900 for (i = 0; i < cnt; i++) {
1901 ret = mmc_test_area_io(test, sz, dev_addr, write,
1902 max_scatter, 0);
1903 if (ret)
1904 return ret;
1905 dev_addr += ssz;
1906 }
1907 getnstimeofday(&ts2);
1908
1909 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1910
1911 return 0;
1912 }
1913
1914 static int mmc_test_large_seq_perf(struct mmc_test_card *test, int write)
1915 {
1916 int ret, i;
1917
1918 for (i = 0; i < 10; i++) {
1919 ret = mmc_test_seq_perf(test, write, 10 * 1024 * 1024, 1);
1920 if (ret)
1921 return ret;
1922 }
1923 for (i = 0; i < 5; i++) {
1924 ret = mmc_test_seq_perf(test, write, 100 * 1024 * 1024, 1);
1925 if (ret)
1926 return ret;
1927 }
1928 for (i = 0; i < 3; i++) {
1929 ret = mmc_test_seq_perf(test, write, 1000 * 1024 * 1024, 1);
1930 if (ret)
1931 return ret;
1932 }
1933
1934 return ret;
1935 }
1936
1937 /*
1938 * Large sequential read performance.
1939 */
1940 static int mmc_test_large_seq_read_perf(struct mmc_test_card *test)
1941 {
1942 return mmc_test_large_seq_perf(test, 0);
1943 }
1944
1945 /*
1946 * Large sequential write performance.
1947 */
1948 static int mmc_test_large_seq_write_perf(struct mmc_test_card *test)
1949 {
1950 return mmc_test_large_seq_perf(test, 1);
1951 }
1952
1953 static const struct mmc_test_case mmc_test_cases[] = {
1954 {
1955 .name = "Basic write (no data verification)",
1956 .run = mmc_test_basic_write,
1957 },
1958
1959 {
1960 .name = "Basic read (no data verification)",
1961 .run = mmc_test_basic_read,
1962 },
1963
1964 {
1965 .name = "Basic write (with data verification)",
1966 .prepare = mmc_test_prepare_write,
1967 .run = mmc_test_verify_write,
1968 .cleanup = mmc_test_cleanup,
1969 },
1970
1971 {
1972 .name = "Basic read (with data verification)",
1973 .prepare = mmc_test_prepare_read,
1974 .run = mmc_test_verify_read,
1975 .cleanup = mmc_test_cleanup,
1976 },
1977
1978 {
1979 .name = "Multi-block write",
1980 .prepare = mmc_test_prepare_write,
1981 .run = mmc_test_multi_write,
1982 .cleanup = mmc_test_cleanup,
1983 },
1984
1985 {
1986 .name = "Multi-block read",
1987 .prepare = mmc_test_prepare_read,
1988 .run = mmc_test_multi_read,
1989 .cleanup = mmc_test_cleanup,
1990 },
1991
1992 {
1993 .name = "Power of two block writes",
1994 .prepare = mmc_test_prepare_write,
1995 .run = mmc_test_pow2_write,
1996 .cleanup = mmc_test_cleanup,
1997 },
1998
1999 {
2000 .name = "Power of two block reads",
2001 .prepare = mmc_test_prepare_read,
2002 .run = mmc_test_pow2_read,
2003 .cleanup = mmc_test_cleanup,
2004 },
2005
2006 {
2007 .name = "Weird sized block writes",
2008 .prepare = mmc_test_prepare_write,
2009 .run = mmc_test_weird_write,
2010 .cleanup = mmc_test_cleanup,
2011 },
2012
2013 {
2014 .name = "Weird sized block reads",
2015 .prepare = mmc_test_prepare_read,
2016 .run = mmc_test_weird_read,
2017 .cleanup = mmc_test_cleanup,
2018 },
2019
2020 {
2021 .name = "Badly aligned write",
2022 .prepare = mmc_test_prepare_write,
2023 .run = mmc_test_align_write,
2024 .cleanup = mmc_test_cleanup,
2025 },
2026
2027 {
2028 .name = "Badly aligned read",
2029 .prepare = mmc_test_prepare_read,
2030 .run = mmc_test_align_read,
2031 .cleanup = mmc_test_cleanup,
2032 },
2033
2034 {
2035 .name = "Badly aligned multi-block write",
2036 .prepare = mmc_test_prepare_write,
2037 .run = mmc_test_align_multi_write,
2038 .cleanup = mmc_test_cleanup,
2039 },
2040
2041 {
2042 .name = "Badly aligned multi-block read",
2043 .prepare = mmc_test_prepare_read,
2044 .run = mmc_test_align_multi_read,
2045 .cleanup = mmc_test_cleanup,
2046 },
2047
2048 {
2049 .name = "Correct xfer_size at write (start failure)",
2050 .run = mmc_test_xfersize_write,
2051 },
2052
2053 {
2054 .name = "Correct xfer_size at read (start failure)",
2055 .run = mmc_test_xfersize_read,
2056 },
2057
2058 {
2059 .name = "Correct xfer_size at write (midway failure)",
2060 .run = mmc_test_multi_xfersize_write,
2061 },
2062
2063 {
2064 .name = "Correct xfer_size at read (midway failure)",
2065 .run = mmc_test_multi_xfersize_read,
2066 },
2067
2068 #ifdef CONFIG_HIGHMEM
2069
2070 {
2071 .name = "Highmem write",
2072 .prepare = mmc_test_prepare_write,
2073 .run = mmc_test_write_high,
2074 .cleanup = mmc_test_cleanup,
2075 },
2076
2077 {
2078 .name = "Highmem read",
2079 .prepare = mmc_test_prepare_read,
2080 .run = mmc_test_read_high,
2081 .cleanup = mmc_test_cleanup,
2082 },
2083
2084 {
2085 .name = "Multi-block highmem write",
2086 .prepare = mmc_test_prepare_write,
2087 .run = mmc_test_multi_write_high,
2088 .cleanup = mmc_test_cleanup,
2089 },
2090
2091 {
2092 .name = "Multi-block highmem read",
2093 .prepare = mmc_test_prepare_read,
2094 .run = mmc_test_multi_read_high,
2095 .cleanup = mmc_test_cleanup,
2096 },
2097
2098 #else
2099
2100 {
2101 .name = "Highmem write",
2102 .run = mmc_test_no_highmem,
2103 },
2104
2105 {
2106 .name = "Highmem read",
2107 .run = mmc_test_no_highmem,
2108 },
2109
2110 {
2111 .name = "Multi-block highmem write",
2112 .run = mmc_test_no_highmem,
2113 },
2114
2115 {
2116 .name = "Multi-block highmem read",
2117 .run = mmc_test_no_highmem,
2118 },
2119
2120 #endif /* CONFIG_HIGHMEM */
2121
2122 {
2123 .name = "Best-case read performance",
2124 .prepare = mmc_test_area_prepare_fill,
2125 .run = mmc_test_best_read_performance,
2126 .cleanup = mmc_test_area_cleanup,
2127 },
2128
2129 {
2130 .name = "Best-case write performance",
2131 .prepare = mmc_test_area_prepare_erase,
2132 .run = mmc_test_best_write_performance,
2133 .cleanup = mmc_test_area_cleanup,
2134 },
2135
2136 {
2137 .name = "Best-case read performance into scattered pages",
2138 .prepare = mmc_test_area_prepare_fill,
2139 .run = mmc_test_best_read_perf_max_scatter,
2140 .cleanup = mmc_test_area_cleanup,
2141 },
2142
2143 {
2144 .name = "Best-case write performance from scattered pages",
2145 .prepare = mmc_test_area_prepare_erase,
2146 .run = mmc_test_best_write_perf_max_scatter,
2147 .cleanup = mmc_test_area_cleanup,
2148 },
2149
2150 {
2151 .name = "Single read performance by transfer size",
2152 .prepare = mmc_test_area_prepare_fill,
2153 .run = mmc_test_profile_read_perf,
2154 .cleanup = mmc_test_area_cleanup,
2155 },
2156
2157 {
2158 .name = "Single write performance by transfer size",
2159 .prepare = mmc_test_area_prepare,
2160 .run = mmc_test_profile_write_perf,
2161 .cleanup = mmc_test_area_cleanup,
2162 },
2163
2164 {
2165 .name = "Single trim performance by transfer size",
2166 .prepare = mmc_test_area_prepare_fill,
2167 .run = mmc_test_profile_trim_perf,
2168 .cleanup = mmc_test_area_cleanup,
2169 },
2170
2171 {
2172 .name = "Consecutive read performance by transfer size",
2173 .prepare = mmc_test_area_prepare_fill,
2174 .run = mmc_test_profile_seq_read_perf,
2175 .cleanup = mmc_test_area_cleanup,
2176 },
2177
2178 {
2179 .name = "Consecutive write performance by transfer size",
2180 .prepare = mmc_test_area_prepare,
2181 .run = mmc_test_profile_seq_write_perf,
2182 .cleanup = mmc_test_area_cleanup,
2183 },
2184
2185 {
2186 .name = "Consecutive trim performance by transfer size",
2187 .prepare = mmc_test_area_prepare,
2188 .run = mmc_test_profile_seq_trim_perf,
2189 .cleanup = mmc_test_area_cleanup,
2190 },
2191
2192 {
2193 .name = "Random read performance by transfer size",
2194 .prepare = mmc_test_area_prepare,
2195 .run = mmc_test_random_read_perf,
2196 .cleanup = mmc_test_area_cleanup,
2197 },
2198
2199 {
2200 .name = "Random write performance by transfer size",
2201 .prepare = mmc_test_area_prepare,
2202 .run = mmc_test_random_write_perf,
2203 .cleanup = mmc_test_area_cleanup,
2204 },
2205
2206 {
2207 .name = "Large sequential read into scattered pages",
2208 .prepare = mmc_test_area_prepare,
2209 .run = mmc_test_large_seq_read_perf,
2210 .cleanup = mmc_test_area_cleanup,
2211 },
2212
2213 {
2214 .name = "Large sequential write from scattered pages",
2215 .prepare = mmc_test_area_prepare,
2216 .run = mmc_test_large_seq_write_perf,
2217 .cleanup = mmc_test_area_cleanup,
2218 },
2219
2220 };
2221
2222 static DEFINE_MUTEX(mmc_test_lock);
2223
2224 static LIST_HEAD(mmc_test_result);
2225
2226 static void mmc_test_run(struct mmc_test_card *test, int testcase)
2227 {
2228 int i, ret;
2229
2230 printk(KERN_INFO "%s: Starting tests of card %s...\n",
2231 mmc_hostname(test->card->host), mmc_card_id(test->card));
2232
2233 mmc_claim_host(test->card->host);
2234
2235 for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
2236 struct mmc_test_general_result *gr;
2237
2238 if (testcase && ((i + 1) != testcase))
2239 continue;
2240
2241 printk(KERN_INFO "%s: Test case %d. %s...\n",
2242 mmc_hostname(test->card->host), i + 1,
2243 mmc_test_cases[i].name);
2244
2245 if (mmc_test_cases[i].prepare) {
2246 ret = mmc_test_cases[i].prepare(test);
2247 if (ret) {
2248 printk(KERN_INFO "%s: Result: Prepare "
2249 "stage failed! (%d)\n",
2250 mmc_hostname(test->card->host),
2251 ret);
2252 continue;
2253 }
2254 }
2255
2256 gr = kzalloc(sizeof(struct mmc_test_general_result),
2257 GFP_KERNEL);
2258 if (gr) {
2259 INIT_LIST_HEAD(&gr->tr_lst);
2260
2261 /* Assign data what we know already */
2262 gr->card = test->card;
2263 gr->testcase = i;
2264
2265 /* Append container to global one */
2266 list_add_tail(&gr->link, &mmc_test_result);
2267
2268 /*
2269 * Save the pointer to created container in our private
2270 * structure.
2271 */
2272 test->gr = gr;
2273 }
2274
2275 ret = mmc_test_cases[i].run(test);
2276 switch (ret) {
2277 case RESULT_OK:
2278 printk(KERN_INFO "%s: Result: OK\n",
2279 mmc_hostname(test->card->host));
2280 break;
2281 case RESULT_FAIL:
2282 printk(KERN_INFO "%s: Result: FAILED\n",
2283 mmc_hostname(test->card->host));
2284 break;
2285 case RESULT_UNSUP_HOST:
2286 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2287 "(by host)\n",
2288 mmc_hostname(test->card->host));
2289 break;
2290 case RESULT_UNSUP_CARD:
2291 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2292 "(by card)\n",
2293 mmc_hostname(test->card->host));
2294 break;
2295 default:
2296 printk(KERN_INFO "%s: Result: ERROR (%d)\n",
2297 mmc_hostname(test->card->host), ret);
2298 }
2299
2300 /* Save the result */
2301 if (gr)
2302 gr->result = ret;
2303
2304 if (mmc_test_cases[i].cleanup) {
2305 ret = mmc_test_cases[i].cleanup(test);
2306 if (ret) {
2307 printk(KERN_INFO "%s: Warning: Cleanup "
2308 "stage failed! (%d)\n",
2309 mmc_hostname(test->card->host),
2310 ret);
2311 }
2312 }
2313 }
2314
2315 mmc_release_host(test->card->host);
2316
2317 printk(KERN_INFO "%s: Tests completed.\n",
2318 mmc_hostname(test->card->host));
2319 }
2320
2321 static void mmc_test_free_result(struct mmc_card *card)
2322 {
2323 struct mmc_test_general_result *gr, *grs;
2324
2325 mutex_lock(&mmc_test_lock);
2326
2327 list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
2328 struct mmc_test_transfer_result *tr, *trs;
2329
2330 if (card && gr->card != card)
2331 continue;
2332
2333 list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
2334 list_del(&tr->link);
2335 kfree(tr);
2336 }
2337
2338 list_del(&gr->link);
2339 kfree(gr);
2340 }
2341
2342 mutex_unlock(&mmc_test_lock);
2343 }
2344
2345 static LIST_HEAD(mmc_test_file_test);
2346
2347 static int mtf_test_show(struct seq_file *sf, void *data)
2348 {
2349 struct mmc_card *card = (struct mmc_card *)sf->private;
2350 struct mmc_test_general_result *gr;
2351
2352 mutex_lock(&mmc_test_lock);
2353
2354 list_for_each_entry(gr, &mmc_test_result, link) {
2355 struct mmc_test_transfer_result *tr;
2356
2357 if (gr->card != card)
2358 continue;
2359
2360 seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
2361
2362 list_for_each_entry(tr, &gr->tr_lst, link) {
2363 seq_printf(sf, "%u %d %lu.%09lu %u %u.%02u\n",
2364 tr->count, tr->sectors,
2365 (unsigned long)tr->ts.tv_sec,
2366 (unsigned long)tr->ts.tv_nsec,
2367 tr->rate, tr->iops / 100, tr->iops % 100);
2368 }
2369 }
2370
2371 mutex_unlock(&mmc_test_lock);
2372
2373 return 0;
2374 }
2375
2376 static int mtf_test_open(struct inode *inode, struct file *file)
2377 {
2378 return single_open(file, mtf_test_show, inode->i_private);
2379 }
2380
2381 static ssize_t mtf_test_write(struct file *file, const char __user *buf,
2382 size_t count, loff_t *pos)
2383 {
2384 struct seq_file *sf = (struct seq_file *)file->private_data;
2385 struct mmc_card *card = (struct mmc_card *)sf->private;
2386 struct mmc_test_card *test;
2387 char lbuf[12];
2388 long testcase;
2389
2390 if (count >= sizeof(lbuf))
2391 return -EINVAL;
2392
2393 if (copy_from_user(lbuf, buf, count))
2394 return -EFAULT;
2395 lbuf[count] = '\0';
2396
2397 if (strict_strtol(lbuf, 10, &testcase))
2398 return -EINVAL;
2399
2400 test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
2401 if (!test)
2402 return -ENOMEM;
2403
2404 /*
2405 * Remove all test cases associated with given card. Thus we have only
2406 * actual data of the last run.
2407 */
2408 mmc_test_free_result(card);
2409
2410 test->card = card;
2411
2412 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
2413 #ifdef CONFIG_HIGHMEM
2414 test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
2415 #endif
2416
2417 #ifdef CONFIG_HIGHMEM
2418 if (test->buffer && test->highmem) {
2419 #else
2420 if (test->buffer) {
2421 #endif
2422 mutex_lock(&mmc_test_lock);
2423 mmc_test_run(test, testcase);
2424 mutex_unlock(&mmc_test_lock);
2425 }
2426
2427 #ifdef CONFIG_HIGHMEM
2428 __free_pages(test->highmem, BUFFER_ORDER);
2429 #endif
2430 kfree(test->buffer);
2431 kfree(test);
2432
2433 return count;
2434 }
2435
2436 static const struct file_operations mmc_test_fops_test = {
2437 .open = mtf_test_open,
2438 .read = seq_read,
2439 .write = mtf_test_write,
2440 .llseek = seq_lseek,
2441 .release = single_release,
2442 };
2443
2444 static void mmc_test_free_file_test(struct mmc_card *card)
2445 {
2446 struct mmc_test_dbgfs_file *df, *dfs;
2447
2448 mutex_lock(&mmc_test_lock);
2449
2450 list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
2451 if (card && df->card != card)
2452 continue;
2453 debugfs_remove(df->file);
2454 list_del(&df->link);
2455 kfree(df);
2456 }
2457
2458 mutex_unlock(&mmc_test_lock);
2459 }
2460
2461 static int mmc_test_register_file_test(struct mmc_card *card)
2462 {
2463 struct dentry *file = NULL;
2464 struct mmc_test_dbgfs_file *df;
2465 int ret = 0;
2466
2467 mutex_lock(&mmc_test_lock);
2468
2469 if (card->debugfs_root)
2470 file = debugfs_create_file("test", S_IWUSR | S_IRUGO,
2471 card->debugfs_root, card, &mmc_test_fops_test);
2472
2473 if (IS_ERR_OR_NULL(file)) {
2474 dev_err(&card->dev,
2475 "Can't create file. Perhaps debugfs is disabled.\n");
2476 ret = -ENODEV;
2477 goto err;
2478 }
2479
2480 df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
2481 if (!df) {
2482 debugfs_remove(file);
2483 dev_err(&card->dev,
2484 "Can't allocate memory for internal usage.\n");
2485 ret = -ENOMEM;
2486 goto err;
2487 }
2488
2489 df->card = card;
2490 df->file = file;
2491
2492 list_add(&df->link, &mmc_test_file_test);
2493
2494 err:
2495 mutex_unlock(&mmc_test_lock);
2496
2497 return ret;
2498 }
2499
2500 static int mmc_test_probe(struct mmc_card *card)
2501 {
2502 int ret;
2503
2504 if (!mmc_card_mmc(card) && !mmc_card_sd(card))
2505 return -ENODEV;
2506
2507 ret = mmc_test_register_file_test(card);
2508 if (ret)
2509 return ret;
2510
2511 dev_info(&card->dev, "Card claimed for testing.\n");
2512
2513 return 0;
2514 }
2515
2516 static void mmc_test_remove(struct mmc_card *card)
2517 {
2518 mmc_test_free_result(card);
2519 mmc_test_free_file_test(card);
2520 }
2521
2522 static struct mmc_driver mmc_driver = {
2523 .drv = {
2524 .name = "mmc_test",
2525 },
2526 .probe = mmc_test_probe,
2527 .remove = mmc_test_remove,
2528 };
2529
2530 static int __init mmc_test_init(void)
2531 {
2532 return mmc_register_driver(&mmc_driver);
2533 }
2534
2535 static void __exit mmc_test_exit(void)
2536 {
2537 /* Clear stalled data if card is still plugged */
2538 mmc_test_free_result(NULL);
2539 mmc_test_free_file_test(NULL);
2540
2541 mmc_unregister_driver(&mmc_driver);
2542 }
2543
2544 module_init(mmc_test_init);
2545 module_exit(mmc_test_exit);
2546
2547 MODULE_LICENSE("GPL");
2548 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
2549 MODULE_AUTHOR("Pierre Ossman");