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