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