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