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