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