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