OSDN Git Service

mmc: mmc_test: Extend "Badly aligned" tests for 8-byte alignment
[uclinux-h8/linux.git] / drivers / mmc / card / mmc_test.c
1 /*
2  *  linux/drivers/mmc/card/mmc_test.c
3  *
4  *  Copyright 2007-2008 Pierre Ossman
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  */
11
12 #include <linux/mmc/core.h>
13 #include <linux/mmc/card.h>
14 #include <linux/mmc/host.h>
15 #include <linux/mmc/mmc.h>
16 #include <linux/slab.h>
17 #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         err = mmc_hw_reset_check(host);
2346         if (!err)
2347                 return RESULT_OK;
2348
2349         if (err == -ENOSYS)
2350                 return RESULT_FAIL;
2351
2352         if (err != -EOPNOTSUPP)
2353                 return err;
2354
2355         if (!mmc_can_reset(card))
2356                 return RESULT_UNSUP_CARD;
2357
2358         return RESULT_UNSUP_HOST;
2359 }
2360
2361 static const struct mmc_test_case mmc_test_cases[] = {
2362         {
2363                 .name = "Basic write (no data verification)",
2364                 .run = mmc_test_basic_write,
2365         },
2366
2367         {
2368                 .name = "Basic read (no data verification)",
2369                 .run = mmc_test_basic_read,
2370         },
2371
2372         {
2373                 .name = "Basic write (with data verification)",
2374                 .prepare = mmc_test_prepare_write,
2375                 .run = mmc_test_verify_write,
2376                 .cleanup = mmc_test_cleanup,
2377         },
2378
2379         {
2380                 .name = "Basic read (with data verification)",
2381                 .prepare = mmc_test_prepare_read,
2382                 .run = mmc_test_verify_read,
2383                 .cleanup = mmc_test_cleanup,
2384         },
2385
2386         {
2387                 .name = "Multi-block write",
2388                 .prepare = mmc_test_prepare_write,
2389                 .run = mmc_test_multi_write,
2390                 .cleanup = mmc_test_cleanup,
2391         },
2392
2393         {
2394                 .name = "Multi-block read",
2395                 .prepare = mmc_test_prepare_read,
2396                 .run = mmc_test_multi_read,
2397                 .cleanup = mmc_test_cleanup,
2398         },
2399
2400         {
2401                 .name = "Power of two block writes",
2402                 .prepare = mmc_test_prepare_write,
2403                 .run = mmc_test_pow2_write,
2404                 .cleanup = mmc_test_cleanup,
2405         },
2406
2407         {
2408                 .name = "Power of two block reads",
2409                 .prepare = mmc_test_prepare_read,
2410                 .run = mmc_test_pow2_read,
2411                 .cleanup = mmc_test_cleanup,
2412         },
2413
2414         {
2415                 .name = "Weird sized block writes",
2416                 .prepare = mmc_test_prepare_write,
2417                 .run = mmc_test_weird_write,
2418                 .cleanup = mmc_test_cleanup,
2419         },
2420
2421         {
2422                 .name = "Weird sized block reads",
2423                 .prepare = mmc_test_prepare_read,
2424                 .run = mmc_test_weird_read,
2425                 .cleanup = mmc_test_cleanup,
2426         },
2427
2428         {
2429                 .name = "Badly aligned write",
2430                 .prepare = mmc_test_prepare_write,
2431                 .run = mmc_test_align_write,
2432                 .cleanup = mmc_test_cleanup,
2433         },
2434
2435         {
2436                 .name = "Badly aligned read",
2437                 .prepare = mmc_test_prepare_read,
2438                 .run = mmc_test_align_read,
2439                 .cleanup = mmc_test_cleanup,
2440         },
2441
2442         {
2443                 .name = "Badly aligned multi-block write",
2444                 .prepare = mmc_test_prepare_write,
2445                 .run = mmc_test_align_multi_write,
2446                 .cleanup = mmc_test_cleanup,
2447         },
2448
2449         {
2450                 .name = "Badly aligned multi-block read",
2451                 .prepare = mmc_test_prepare_read,
2452                 .run = mmc_test_align_multi_read,
2453                 .cleanup = mmc_test_cleanup,
2454         },
2455
2456         {
2457                 .name = "Correct xfer_size at write (start failure)",
2458                 .run = mmc_test_xfersize_write,
2459         },
2460
2461         {
2462                 .name = "Correct xfer_size at read (start failure)",
2463                 .run = mmc_test_xfersize_read,
2464         },
2465
2466         {
2467                 .name = "Correct xfer_size at write (midway failure)",
2468                 .run = mmc_test_multi_xfersize_write,
2469         },
2470
2471         {
2472                 .name = "Correct xfer_size at read (midway failure)",
2473                 .run = mmc_test_multi_xfersize_read,
2474         },
2475
2476 #ifdef CONFIG_HIGHMEM
2477
2478         {
2479                 .name = "Highmem write",
2480                 .prepare = mmc_test_prepare_write,
2481                 .run = mmc_test_write_high,
2482                 .cleanup = mmc_test_cleanup,
2483         },
2484
2485         {
2486                 .name = "Highmem read",
2487                 .prepare = mmc_test_prepare_read,
2488                 .run = mmc_test_read_high,
2489                 .cleanup = mmc_test_cleanup,
2490         },
2491
2492         {
2493                 .name = "Multi-block highmem write",
2494                 .prepare = mmc_test_prepare_write,
2495                 .run = mmc_test_multi_write_high,
2496                 .cleanup = mmc_test_cleanup,
2497         },
2498
2499         {
2500                 .name = "Multi-block highmem read",
2501                 .prepare = mmc_test_prepare_read,
2502                 .run = mmc_test_multi_read_high,
2503                 .cleanup = mmc_test_cleanup,
2504         },
2505
2506 #else
2507
2508         {
2509                 .name = "Highmem write",
2510                 .run = mmc_test_no_highmem,
2511         },
2512
2513         {
2514                 .name = "Highmem read",
2515                 .run = mmc_test_no_highmem,
2516         },
2517
2518         {
2519                 .name = "Multi-block highmem write",
2520                 .run = mmc_test_no_highmem,
2521         },
2522
2523         {
2524                 .name = "Multi-block highmem read",
2525                 .run = mmc_test_no_highmem,
2526         },
2527
2528 #endif /* CONFIG_HIGHMEM */
2529
2530         {
2531                 .name = "Best-case read performance",
2532                 .prepare = mmc_test_area_prepare_fill,
2533                 .run = mmc_test_best_read_performance,
2534                 .cleanup = mmc_test_area_cleanup,
2535         },
2536
2537         {
2538                 .name = "Best-case write performance",
2539                 .prepare = mmc_test_area_prepare_erase,
2540                 .run = mmc_test_best_write_performance,
2541                 .cleanup = mmc_test_area_cleanup,
2542         },
2543
2544         {
2545                 .name = "Best-case read performance into scattered pages",
2546                 .prepare = mmc_test_area_prepare_fill,
2547                 .run = mmc_test_best_read_perf_max_scatter,
2548                 .cleanup = mmc_test_area_cleanup,
2549         },
2550
2551         {
2552                 .name = "Best-case write performance from scattered pages",
2553                 .prepare = mmc_test_area_prepare_erase,
2554                 .run = mmc_test_best_write_perf_max_scatter,
2555                 .cleanup = mmc_test_area_cleanup,
2556         },
2557
2558         {
2559                 .name = "Single read performance by transfer size",
2560                 .prepare = mmc_test_area_prepare_fill,
2561                 .run = mmc_test_profile_read_perf,
2562                 .cleanup = mmc_test_area_cleanup,
2563         },
2564
2565         {
2566                 .name = "Single write performance by transfer size",
2567                 .prepare = mmc_test_area_prepare,
2568                 .run = mmc_test_profile_write_perf,
2569                 .cleanup = mmc_test_area_cleanup,
2570         },
2571
2572         {
2573                 .name = "Single trim performance by transfer size",
2574                 .prepare = mmc_test_area_prepare_fill,
2575                 .run = mmc_test_profile_trim_perf,
2576                 .cleanup = mmc_test_area_cleanup,
2577         },
2578
2579         {
2580                 .name = "Consecutive read performance by transfer size",
2581                 .prepare = mmc_test_area_prepare_fill,
2582                 .run = mmc_test_profile_seq_read_perf,
2583                 .cleanup = mmc_test_area_cleanup,
2584         },
2585
2586         {
2587                 .name = "Consecutive write performance by transfer size",
2588                 .prepare = mmc_test_area_prepare,
2589                 .run = mmc_test_profile_seq_write_perf,
2590                 .cleanup = mmc_test_area_cleanup,
2591         },
2592
2593         {
2594                 .name = "Consecutive trim performance by transfer size",
2595                 .prepare = mmc_test_area_prepare,
2596                 .run = mmc_test_profile_seq_trim_perf,
2597                 .cleanup = mmc_test_area_cleanup,
2598         },
2599
2600         {
2601                 .name = "Random read performance by transfer size",
2602                 .prepare = mmc_test_area_prepare,
2603                 .run = mmc_test_random_read_perf,
2604                 .cleanup = mmc_test_area_cleanup,
2605         },
2606
2607         {
2608                 .name = "Random write performance by transfer size",
2609                 .prepare = mmc_test_area_prepare,
2610                 .run = mmc_test_random_write_perf,
2611                 .cleanup = mmc_test_area_cleanup,
2612         },
2613
2614         {
2615                 .name = "Large sequential read into scattered pages",
2616                 .prepare = mmc_test_area_prepare,
2617                 .run = mmc_test_large_seq_read_perf,
2618                 .cleanup = mmc_test_area_cleanup,
2619         },
2620
2621         {
2622                 .name = "Large sequential write from scattered pages",
2623                 .prepare = mmc_test_area_prepare,
2624                 .run = mmc_test_large_seq_write_perf,
2625                 .cleanup = mmc_test_area_cleanup,
2626         },
2627
2628         {
2629                 .name = "Write performance with blocking req 4k to 4MB",
2630                 .prepare = mmc_test_area_prepare,
2631                 .run = mmc_test_profile_mult_write_blocking_perf,
2632                 .cleanup = mmc_test_area_cleanup,
2633         },
2634
2635         {
2636                 .name = "Write performance with non-blocking req 4k to 4MB",
2637                 .prepare = mmc_test_area_prepare,
2638                 .run = mmc_test_profile_mult_write_nonblock_perf,
2639                 .cleanup = mmc_test_area_cleanup,
2640         },
2641
2642         {
2643                 .name = "Read performance with blocking req 4k to 4MB",
2644                 .prepare = mmc_test_area_prepare,
2645                 .run = mmc_test_profile_mult_read_blocking_perf,
2646                 .cleanup = mmc_test_area_cleanup,
2647         },
2648
2649         {
2650                 .name = "Read performance with non-blocking req 4k to 4MB",
2651                 .prepare = mmc_test_area_prepare,
2652                 .run = mmc_test_profile_mult_read_nonblock_perf,
2653                 .cleanup = mmc_test_area_cleanup,
2654         },
2655
2656         {
2657                 .name = "Write performance blocking req 1 to 512 sg elems",
2658                 .prepare = mmc_test_area_prepare,
2659                 .run = mmc_test_profile_sglen_wr_blocking_perf,
2660                 .cleanup = mmc_test_area_cleanup,
2661         },
2662
2663         {
2664                 .name = "Write performance non-blocking req 1 to 512 sg elems",
2665                 .prepare = mmc_test_area_prepare,
2666                 .run = mmc_test_profile_sglen_wr_nonblock_perf,
2667                 .cleanup = mmc_test_area_cleanup,
2668         },
2669
2670         {
2671                 .name = "Read performance blocking req 1 to 512 sg elems",
2672                 .prepare = mmc_test_area_prepare,
2673                 .run = mmc_test_profile_sglen_r_blocking_perf,
2674                 .cleanup = mmc_test_area_cleanup,
2675         },
2676
2677         {
2678                 .name = "Read performance non-blocking req 1 to 512 sg elems",
2679                 .prepare = mmc_test_area_prepare,
2680                 .run = mmc_test_profile_sglen_r_nonblock_perf,
2681                 .cleanup = mmc_test_area_cleanup,
2682         },
2683
2684         {
2685                 .name = "eMMC hardware reset",
2686                 .run = mmc_test_hw_reset,
2687         },
2688 };
2689
2690 static DEFINE_MUTEX(mmc_test_lock);
2691
2692 static LIST_HEAD(mmc_test_result);
2693
2694 static void mmc_test_run(struct mmc_test_card *test, int testcase)
2695 {
2696         int i, ret;
2697
2698         pr_info("%s: Starting tests of card %s...\n",
2699                 mmc_hostname(test->card->host), mmc_card_id(test->card));
2700
2701         mmc_claim_host(test->card->host);
2702
2703         for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
2704                 struct mmc_test_general_result *gr;
2705
2706                 if (testcase && ((i + 1) != testcase))
2707                         continue;
2708
2709                 pr_info("%s: Test case %d. %s...\n",
2710                         mmc_hostname(test->card->host), i + 1,
2711                         mmc_test_cases[i].name);
2712
2713                 if (mmc_test_cases[i].prepare) {
2714                         ret = mmc_test_cases[i].prepare(test);
2715                         if (ret) {
2716                                 pr_info("%s: Result: Prepare "
2717                                         "stage failed! (%d)\n",
2718                                         mmc_hostname(test->card->host),
2719                                         ret);
2720                                 continue;
2721                         }
2722                 }
2723
2724                 gr = kzalloc(sizeof(struct mmc_test_general_result),
2725                         GFP_KERNEL);
2726                 if (gr) {
2727                         INIT_LIST_HEAD(&gr->tr_lst);
2728
2729                         /* Assign data what we know already */
2730                         gr->card = test->card;
2731                         gr->testcase = i;
2732
2733                         /* Append container to global one */
2734                         list_add_tail(&gr->link, &mmc_test_result);
2735
2736                         /*
2737                          * Save the pointer to created container in our private
2738                          * structure.
2739                          */
2740                         test->gr = gr;
2741                 }
2742
2743                 ret = mmc_test_cases[i].run(test);
2744                 switch (ret) {
2745                 case RESULT_OK:
2746                         pr_info("%s: Result: OK\n",
2747                                 mmc_hostname(test->card->host));
2748                         break;
2749                 case RESULT_FAIL:
2750                         pr_info("%s: Result: FAILED\n",
2751                                 mmc_hostname(test->card->host));
2752                         break;
2753                 case RESULT_UNSUP_HOST:
2754                         pr_info("%s: Result: UNSUPPORTED "
2755                                 "(by host)\n",
2756                                 mmc_hostname(test->card->host));
2757                         break;
2758                 case RESULT_UNSUP_CARD:
2759                         pr_info("%s: Result: UNSUPPORTED "
2760                                 "(by card)\n",
2761                                 mmc_hostname(test->card->host));
2762                         break;
2763                 default:
2764                         pr_info("%s: Result: ERROR (%d)\n",
2765                                 mmc_hostname(test->card->host), ret);
2766                 }
2767
2768                 /* Save the result */
2769                 if (gr)
2770                         gr->result = ret;
2771
2772                 if (mmc_test_cases[i].cleanup) {
2773                         ret = mmc_test_cases[i].cleanup(test);
2774                         if (ret) {
2775                                 pr_info("%s: Warning: Cleanup "
2776                                         "stage failed! (%d)\n",
2777                                         mmc_hostname(test->card->host),
2778                                         ret);
2779                         }
2780                 }
2781         }
2782
2783         mmc_release_host(test->card->host);
2784
2785         pr_info("%s: Tests completed.\n",
2786                 mmc_hostname(test->card->host));
2787 }
2788
2789 static void mmc_test_free_result(struct mmc_card *card)
2790 {
2791         struct mmc_test_general_result *gr, *grs;
2792
2793         mutex_lock(&mmc_test_lock);
2794
2795         list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
2796                 struct mmc_test_transfer_result *tr, *trs;
2797
2798                 if (card && gr->card != card)
2799                         continue;
2800
2801                 list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
2802                         list_del(&tr->link);
2803                         kfree(tr);
2804                 }
2805
2806                 list_del(&gr->link);
2807                 kfree(gr);
2808         }
2809
2810         mutex_unlock(&mmc_test_lock);
2811 }
2812
2813 static LIST_HEAD(mmc_test_file_test);
2814
2815 static int mtf_test_show(struct seq_file *sf, void *data)
2816 {
2817         struct mmc_card *card = (struct mmc_card *)sf->private;
2818         struct mmc_test_general_result *gr;
2819
2820         mutex_lock(&mmc_test_lock);
2821
2822         list_for_each_entry(gr, &mmc_test_result, link) {
2823                 struct mmc_test_transfer_result *tr;
2824
2825                 if (gr->card != card)
2826                         continue;
2827
2828                 seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
2829
2830                 list_for_each_entry(tr, &gr->tr_lst, link) {
2831                         seq_printf(sf, "%u %d %lu.%09lu %u %u.%02u\n",
2832                                 tr->count, tr->sectors,
2833                                 (unsigned long)tr->ts.tv_sec,
2834                                 (unsigned long)tr->ts.tv_nsec,
2835                                 tr->rate, tr->iops / 100, tr->iops % 100);
2836                 }
2837         }
2838
2839         mutex_unlock(&mmc_test_lock);
2840
2841         return 0;
2842 }
2843
2844 static int mtf_test_open(struct inode *inode, struct file *file)
2845 {
2846         return single_open(file, mtf_test_show, inode->i_private);
2847 }
2848
2849 static ssize_t mtf_test_write(struct file *file, const char __user *buf,
2850         size_t count, loff_t *pos)
2851 {
2852         struct seq_file *sf = (struct seq_file *)file->private_data;
2853         struct mmc_card *card = (struct mmc_card *)sf->private;
2854         struct mmc_test_card *test;
2855         long testcase;
2856         int ret;
2857
2858         ret = kstrtol_from_user(buf, count, 10, &testcase);
2859         if (ret)
2860                 return ret;
2861
2862         test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
2863         if (!test)
2864                 return -ENOMEM;
2865
2866         /*
2867          * Remove all test cases associated with given card. Thus we have only
2868          * actual data of the last run.
2869          */
2870         mmc_test_free_result(card);
2871
2872         test->card = card;
2873
2874         test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
2875 #ifdef CONFIG_HIGHMEM
2876         test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
2877 #endif
2878
2879 #ifdef CONFIG_HIGHMEM
2880         if (test->buffer && test->highmem) {
2881 #else
2882         if (test->buffer) {
2883 #endif
2884                 mutex_lock(&mmc_test_lock);
2885                 mmc_test_run(test, testcase);
2886                 mutex_unlock(&mmc_test_lock);
2887         }
2888
2889 #ifdef CONFIG_HIGHMEM
2890         __free_pages(test->highmem, BUFFER_ORDER);
2891 #endif
2892         kfree(test->buffer);
2893         kfree(test);
2894
2895         return count;
2896 }
2897
2898 static const struct file_operations mmc_test_fops_test = {
2899         .open           = mtf_test_open,
2900         .read           = seq_read,
2901         .write          = mtf_test_write,
2902         .llseek         = seq_lseek,
2903         .release        = single_release,
2904 };
2905
2906 static int mtf_testlist_show(struct seq_file *sf, void *data)
2907 {
2908         int i;
2909
2910         mutex_lock(&mmc_test_lock);
2911
2912         for (i = 0; i < ARRAY_SIZE(mmc_test_cases); i++)
2913                 seq_printf(sf, "%d:\t%s\n", i+1, mmc_test_cases[i].name);
2914
2915         mutex_unlock(&mmc_test_lock);
2916
2917         return 0;
2918 }
2919
2920 static int mtf_testlist_open(struct inode *inode, struct file *file)
2921 {
2922         return single_open(file, mtf_testlist_show, inode->i_private);
2923 }
2924
2925 static const struct file_operations mmc_test_fops_testlist = {
2926         .open           = mtf_testlist_open,
2927         .read           = seq_read,
2928         .llseek         = seq_lseek,
2929         .release        = single_release,
2930 };
2931
2932 static void mmc_test_free_dbgfs_file(struct mmc_card *card)
2933 {
2934         struct mmc_test_dbgfs_file *df, *dfs;
2935
2936         mutex_lock(&mmc_test_lock);
2937
2938         list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
2939                 if (card && df->card != card)
2940                         continue;
2941                 debugfs_remove(df->file);
2942                 list_del(&df->link);
2943                 kfree(df);
2944         }
2945
2946         mutex_unlock(&mmc_test_lock);
2947 }
2948
2949 static int __mmc_test_register_dbgfs_file(struct mmc_card *card,
2950         const char *name, umode_t mode, const struct file_operations *fops)
2951 {
2952         struct dentry *file = NULL;
2953         struct mmc_test_dbgfs_file *df;
2954
2955         if (card->debugfs_root)
2956                 file = debugfs_create_file(name, mode, card->debugfs_root,
2957                         card, fops);
2958
2959         if (IS_ERR_OR_NULL(file)) {
2960                 dev_err(&card->dev,
2961                         "Can't create %s. Perhaps debugfs is disabled.\n",
2962                         name);
2963                 return -ENODEV;
2964         }
2965
2966         df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
2967         if (!df) {
2968                 debugfs_remove(file);
2969                 dev_err(&card->dev,
2970                         "Can't allocate memory for internal usage.\n");
2971                 return -ENOMEM;
2972         }
2973
2974         df->card = card;
2975         df->file = file;
2976
2977         list_add(&df->link, &mmc_test_file_test);
2978         return 0;
2979 }
2980
2981 static int mmc_test_register_dbgfs_file(struct mmc_card *card)
2982 {
2983         int ret;
2984
2985         mutex_lock(&mmc_test_lock);
2986
2987         ret = __mmc_test_register_dbgfs_file(card, "test", S_IWUSR | S_IRUGO,
2988                 &mmc_test_fops_test);
2989         if (ret)
2990                 goto err;
2991
2992         ret = __mmc_test_register_dbgfs_file(card, "testlist", S_IRUGO,
2993                 &mmc_test_fops_testlist);
2994         if (ret)
2995                 goto err;
2996
2997 err:
2998         mutex_unlock(&mmc_test_lock);
2999
3000         return ret;
3001 }
3002
3003 static int mmc_test_probe(struct device *dev)
3004 {
3005         struct mmc_card *card = mmc_dev_to_card(dev);
3006         int ret;
3007
3008         if (!mmc_card_mmc(card) && !mmc_card_sd(card))
3009                 return -ENODEV;
3010
3011         ret = mmc_test_register_dbgfs_file(card);
3012         if (ret)
3013                 return ret;
3014
3015         dev_info(&card->dev, "Card claimed for testing.\n");
3016
3017         return 0;
3018 }
3019
3020 static int mmc_test_remove(struct device *dev)
3021 {
3022         struct mmc_card *card = mmc_dev_to_card(dev);
3023
3024         mmc_test_free_result(card);
3025         mmc_test_free_dbgfs_file(card);
3026
3027         return 0;
3028 }
3029
3030 static void mmc_test_shutdown(struct device *dev)
3031 {
3032 }
3033
3034 static struct device_driver mmc_driver = {
3035         .name   = "mmc_test",
3036         .probe          = mmc_test_probe,
3037         .remove         = mmc_test_remove,
3038         .shutdown       = mmc_test_shutdown,
3039 };
3040
3041 static int __init mmc_test_init(void)
3042 {
3043         return mmc_register_driver(&mmc_driver);
3044 }
3045
3046 static void __exit mmc_test_exit(void)
3047 {
3048         /* Clear stalled data if card is still plugged */
3049         mmc_test_free_result(NULL);
3050         mmc_test_free_dbgfs_file(NULL);
3051
3052         mmc_unregister_driver(&mmc_driver);
3053 }
3054
3055 module_init(mmc_test_init);
3056 module_exit(mmc_test_exit);
3057
3058 MODULE_LICENSE("GPL");
3059 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
3060 MODULE_AUTHOR("Pierre Ossman");