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