blob: e8508e99aed5c5c62347ed216a59a511cbb2bb0b [file] [log] [blame]
Pierre Ossman88ae6002007-08-12 14:23:50 +02001/*
2 * linux/drivers/mmc/card/mmc_test.c
3 *
Pierre Ossman0121a982008-06-28 17:51:27 +02004 * Copyright 2007-2008 Pierre Ossman
Pierre Ossman88ae6002007-08-12 14:23:50 +02005 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Pierre Ossman88ae6002007-08-12 14:23:50 +020017
18#include <linux/scatterlist.h>
Adrian Hunterfec4dcc2010-08-11 14:17:51 -070019#include <linux/swap.h> /* For nr_free_buffer_pages() */
Andy Shevchenko3183aa12010-09-01 09:26:47 +030020#include <linux/list.h>
Pierre Ossman88ae6002007-08-12 14:23:50 +020021
Andy Shevchenko130067e2010-09-10 10:10:50 +030022#include <linux/debugfs.h>
23#include <linux/uaccess.h>
24#include <linux/seq_file.h>
25
Pierre Ossman88ae6002007-08-12 14:23:50 +020026#define RESULT_OK 0
27#define RESULT_FAIL 1
28#define RESULT_UNSUP_HOST 2
29#define RESULT_UNSUP_CARD 3
30
Pierre Ossman26610812008-07-04 18:17:13 +020031#define BUFFER_ORDER 2
32#define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
Pierre Ossman88ae6002007-08-12 14:23:50 +020033
Adrian Hunterfec4dcc2010-08-11 14:17:51 -070034/*
35 * Limit the test area size to the maximum MMC HC erase group size. Note that
36 * the maximum SD allocation unit size is just 4MiB.
37 */
38#define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
39
Adrian Hunter64f71202010-08-11 14:17:51 -070040/**
41 * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
42 * @page: first page in the allocation
43 * @order: order of the number of pages allocated
44 */
45struct mmc_test_pages {
46 struct page *page;
47 unsigned int order;
48};
49
50/**
51 * struct mmc_test_mem - allocated memory.
52 * @arr: array of allocations
53 * @cnt: number of allocations
54 */
55struct mmc_test_mem {
56 struct mmc_test_pages *arr;
57 unsigned int cnt;
58};
59
60/**
61 * struct mmc_test_area - information for performance tests.
Adrian Hunter64f71202010-08-11 14:17:51 -070062 * @max_sz: test area size (in bytes)
Adrian Hunterfec4dcc2010-08-11 14:17:51 -070063 * @dev_addr: address on card at which to do performance tests
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +030064 * @max_tfr: maximum transfer size allowed by driver (in bytes)
65 * @max_segs: maximum segments allowed by driver in scatterlist @sg
66 * @max_seg_sz: maximum segment size allowed by driver
Adrian Hunter64f71202010-08-11 14:17:51 -070067 * @blocks: number of (512 byte) blocks currently mapped by @sg
68 * @sg_len: length of currently mapped scatterlist @sg
69 * @mem: allocated memory
70 * @sg: scatterlist
71 */
72struct mmc_test_area {
Adrian Hunterfec4dcc2010-08-11 14:17:51 -070073 unsigned long max_sz;
Adrian Hunter64f71202010-08-11 14:17:51 -070074 unsigned int dev_addr;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +030075 unsigned int max_tfr;
Adrian Hunter64f71202010-08-11 14:17:51 -070076 unsigned int max_segs;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +030077 unsigned int max_seg_sz;
Adrian Hunter64f71202010-08-11 14:17:51 -070078 unsigned int blocks;
79 unsigned int sg_len;
80 struct mmc_test_mem *mem;
81 struct scatterlist *sg;
82};
83
84/**
Andy Shevchenko3183aa12010-09-01 09:26:47 +030085 * struct mmc_test_transfer_result - transfer results for performance tests.
86 * @link: double-linked list
87 * @count: amount of group of sectors to check
88 * @sectors: amount of sectors to check in one group
89 * @ts: time values of transfer
90 * @rate: calculated transfer rate
Adrian Hunterb6056d12011-02-08 13:41:02 +020091 * @iops: I/O operations per second (times 100)
Andy Shevchenko3183aa12010-09-01 09:26:47 +030092 */
93struct mmc_test_transfer_result {
94 struct list_head link;
95 unsigned int count;
96 unsigned int sectors;
97 struct timespec ts;
98 unsigned int rate;
Adrian Hunterb6056d12011-02-08 13:41:02 +020099 unsigned int iops;
Andy Shevchenko3183aa12010-09-01 09:26:47 +0300100};
101
102/**
103 * struct mmc_test_general_result - results for tests.
104 * @link: double-linked list
105 * @card: card under test
106 * @testcase: number of test case
107 * @result: result of test run
108 * @tr_lst: transfer measurements if any as mmc_test_transfer_result
109 */
110struct mmc_test_general_result {
111 struct list_head link;
112 struct mmc_card *card;
113 int testcase;
114 int result;
115 struct list_head tr_lst;
116};
117
118/**
Andy Shevchenko130067e2010-09-10 10:10:50 +0300119 * struct mmc_test_dbgfs_file - debugfs related file.
120 * @link: double-linked list
121 * @card: card under test
122 * @file: file created under debugfs
123 */
124struct mmc_test_dbgfs_file {
125 struct list_head link;
126 struct mmc_card *card;
127 struct dentry *file;
128};
129
130/**
Adrian Hunter64f71202010-08-11 14:17:51 -0700131 * struct mmc_test_card - test information.
132 * @card: card under test
133 * @scratch: transfer buffer
134 * @buffer: transfer buffer
135 * @highmem: buffer for highmem tests
136 * @area: information for performance tests
Andy Shevchenko3183aa12010-09-01 09:26:47 +0300137 * @gr: pointer to results of current testcase
Adrian Hunter64f71202010-08-11 14:17:51 -0700138 */
Pierre Ossman88ae6002007-08-12 14:23:50 +0200139struct mmc_test_card {
140 struct mmc_card *card;
141
Pierre Ossman6b174932008-06-30 09:09:27 +0200142 u8 scratch[BUFFER_SIZE];
Pierre Ossman88ae6002007-08-12 14:23:50 +0200143 u8 *buffer;
Pierre Ossman26610812008-07-04 18:17:13 +0200144#ifdef CONFIG_HIGHMEM
145 struct page *highmem;
146#endif
Andy Shevchenko3183aa12010-09-01 09:26:47 +0300147 struct mmc_test_area area;
148 struct mmc_test_general_result *gr;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200149};
150
151/*******************************************************************/
Pierre Ossman6b174932008-06-30 09:09:27 +0200152/* General helper functions */
Pierre Ossman88ae6002007-08-12 14:23:50 +0200153/*******************************************************************/
154
Pierre Ossman6b174932008-06-30 09:09:27 +0200155/*
156 * Configure correct block size in card
157 */
Pierre Ossman88ae6002007-08-12 14:23:50 +0200158static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
159{
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300160 return mmc_set_blocklen(test->card, size);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200161}
162
Pierre Ossman6b174932008-06-30 09:09:27 +0200163/*
164 * Fill in the mmc_request structure given a set of transfer parameters.
165 */
166static void mmc_test_prepare_mrq(struct mmc_test_card *test,
167 struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
168 unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
169{
170 BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
171
172 if (blocks > 1) {
173 mrq->cmd->opcode = write ?
174 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
175 } else {
176 mrq->cmd->opcode = write ?
177 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
178 }
179
180 mrq->cmd->arg = dev_addr;
Johan Kristellc286d032010-02-10 13:56:34 -0800181 if (!mmc_card_blockaddr(test->card))
182 mrq->cmd->arg <<= 9;
183
Pierre Ossman6b174932008-06-30 09:09:27 +0200184 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
185
186 if (blocks == 1)
187 mrq->stop = NULL;
188 else {
189 mrq->stop->opcode = MMC_STOP_TRANSMISSION;
190 mrq->stop->arg = 0;
191 mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
192 }
193
194 mrq->data->blksz = blksz;
195 mrq->data->blocks = blocks;
196 mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
197 mrq->data->sg = sg;
198 mrq->data->sg_len = sg_len;
199
200 mmc_set_data_timeout(mrq->data, test->card);
201}
202
Adrian Hunter64f71202010-08-11 14:17:51 -0700203static int mmc_test_busy(struct mmc_command *cmd)
204{
205 return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
206 (R1_CURRENT_STATE(cmd->resp[0]) == 7);
207}
208
Pierre Ossman6b174932008-06-30 09:09:27 +0200209/*
210 * Wait for the card to finish the busy state
211 */
212static int mmc_test_wait_busy(struct mmc_test_card *test)
Pierre Ossman88ae6002007-08-12 14:23:50 +0200213{
214 int ret, busy;
Chris Ball1278dba2011-04-13 23:40:30 -0400215 struct mmc_command cmd = {0};
Pierre Ossman88ae6002007-08-12 14:23:50 +0200216
217 busy = 0;
218 do {
Pierre Ossman88ae6002007-08-12 14:23:50 +0200219 memset(&cmd, 0, sizeof(struct mmc_command));
220
221 cmd.opcode = MMC_SEND_STATUS;
222 cmd.arg = test->card->rca << 16;
223 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
224
Pierre Ossman6b174932008-06-30 09:09:27 +0200225 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
226 if (ret)
Pierre Ossman88ae6002007-08-12 14:23:50 +0200227 break;
228
Adrian Hunter64f71202010-08-11 14:17:51 -0700229 if (!busy && mmc_test_busy(&cmd)) {
Pierre Ossman88ae6002007-08-12 14:23:50 +0200230 busy = 1;
Pawel Moll54d6b442011-02-06 15:06:24 -0500231 if (test->card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
232 printk(KERN_INFO "%s: Warning: Host did not "
233 "wait for busy state to end.\n",
234 mmc_hostname(test->card->host));
Pierre Ossman88ae6002007-08-12 14:23:50 +0200235 }
Adrian Hunter64f71202010-08-11 14:17:51 -0700236 } while (mmc_test_busy(&cmd));
Pierre Ossman88ae6002007-08-12 14:23:50 +0200237
238 return ret;
239}
240
Pierre Ossman6b174932008-06-30 09:09:27 +0200241/*
242 * Transfer a single sector of kernel addressable data
243 */
244static int mmc_test_buffer_transfer(struct mmc_test_card *test,
245 u8 *buffer, unsigned addr, unsigned blksz, int write)
Pierre Ossman88ae6002007-08-12 14:23:50 +0200246{
Pierre Ossman6b174932008-06-30 09:09:27 +0200247 int ret;
248
Chris Ball24f5b532011-04-13 23:49:45 -0400249 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400250 struct mmc_command cmd = {0};
251 struct mmc_command stop = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400252 struct mmc_data data = {0};
Pierre Ossman6b174932008-06-30 09:09:27 +0200253
254 struct scatterlist sg;
255
Pierre Ossman6b174932008-06-30 09:09:27 +0200256 mrq.cmd = &cmd;
257 mrq.data = &data;
258 mrq.stop = &stop;
259
260 sg_init_one(&sg, buffer, blksz);
261
262 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
263
264 mmc_wait_for_req(test->card->host, &mrq);
265
266 if (cmd.error)
267 return cmd.error;
268 if (data.error)
269 return data.error;
270
271 ret = mmc_test_wait_busy(test);
272 if (ret)
273 return ret;
274
275 return 0;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200276}
277
Adrian Hunter64f71202010-08-11 14:17:51 -0700278static void mmc_test_free_mem(struct mmc_test_mem *mem)
279{
280 if (!mem)
281 return;
282 while (mem->cnt--)
283 __free_pages(mem->arr[mem->cnt].page,
284 mem->arr[mem->cnt].order);
285 kfree(mem->arr);
286 kfree(mem);
287}
288
289/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300290 * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300291 * there isn't much memory do not exceed 1/16th total lowmem pages. Also do
292 * not exceed a maximum number of segments and try not to make segments much
293 * bigger than maximum segment size.
Adrian Hunter64f71202010-08-11 14:17:51 -0700294 */
Adrian Hunterfec4dcc2010-08-11 14:17:51 -0700295static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300296 unsigned long max_sz,
297 unsigned int max_segs,
298 unsigned int max_seg_sz)
Adrian Hunter64f71202010-08-11 14:17:51 -0700299{
Adrian Hunterfec4dcc2010-08-11 14:17:51 -0700300 unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
301 unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300302 unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
Adrian Hunterfec4dcc2010-08-11 14:17:51 -0700303 unsigned long page_cnt = 0;
304 unsigned long limit = nr_free_buffer_pages() >> 4;
Adrian Hunter64f71202010-08-11 14:17:51 -0700305 struct mmc_test_mem *mem;
Adrian Hunter64f71202010-08-11 14:17:51 -0700306
Adrian Hunterfec4dcc2010-08-11 14:17:51 -0700307 if (max_page_cnt > limit)
308 max_page_cnt = limit;
Adrian Hunter3d203be2010-09-23 14:51:29 +0300309 if (min_page_cnt > max_page_cnt)
310 min_page_cnt = max_page_cnt;
Adrian Hunter64f71202010-08-11 14:17:51 -0700311
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300312 if (max_seg_page_cnt > max_page_cnt)
313 max_seg_page_cnt = max_page_cnt;
314
315 if (max_segs > max_page_cnt)
316 max_segs = max_page_cnt;
317
Adrian Hunter64f71202010-08-11 14:17:51 -0700318 mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
319 if (!mem)
320 return NULL;
321
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300322 mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_segs,
Adrian Hunter64f71202010-08-11 14:17:51 -0700323 GFP_KERNEL);
324 if (!mem->arr)
325 goto out_free;
326
327 while (max_page_cnt) {
328 struct page *page;
329 unsigned int order;
330 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
331 __GFP_NORETRY;
332
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300333 order = get_order(max_seg_page_cnt << PAGE_SHIFT);
Adrian Hunter64f71202010-08-11 14:17:51 -0700334 while (1) {
335 page = alloc_pages(flags, order);
336 if (page || !order)
337 break;
338 order -= 1;
339 }
340 if (!page) {
341 if (page_cnt < min_page_cnt)
342 goto out_free;
343 break;
344 }
345 mem->arr[mem->cnt].page = page;
346 mem->arr[mem->cnt].order = order;
347 mem->cnt += 1;
Adrian Hunterfec4dcc2010-08-11 14:17:51 -0700348 if (max_page_cnt <= (1UL << order))
349 break;
Adrian Hunter3d203be2010-09-23 14:51:29 +0300350 max_page_cnt -= 1UL << order;
351 page_cnt += 1UL << order;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300352 if (mem->cnt >= max_segs) {
353 if (page_cnt < min_page_cnt)
354 goto out_free;
355 break;
356 }
Adrian Hunter64f71202010-08-11 14:17:51 -0700357 }
358
359 return mem;
360
361out_free:
362 mmc_test_free_mem(mem);
363 return NULL;
364}
365
366/*
367 * Map memory into a scatterlist. Optionally allow the same memory to be
368 * mapped more than once.
369 */
Adrian Hunterfec4dcc2010-08-11 14:17:51 -0700370static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
Adrian Hunter64f71202010-08-11 14:17:51 -0700371 struct scatterlist *sglist, int repeat,
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300372 unsigned int max_segs, unsigned int max_seg_sz,
373 unsigned int *sg_len)
Adrian Hunter64f71202010-08-11 14:17:51 -0700374{
375 struct scatterlist *sg = NULL;
376 unsigned int i;
377
378 sg_init_table(sglist, max_segs);
379
380 *sg_len = 0;
381 do {
382 for (i = 0; i < mem->cnt; i++) {
Adrian Hunterfec4dcc2010-08-11 14:17:51 -0700383 unsigned long len = PAGE_SIZE << mem->arr[i].order;
Adrian Hunter64f71202010-08-11 14:17:51 -0700384
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300385 if (len > sz)
Adrian Hunter64f71202010-08-11 14:17:51 -0700386 len = sz;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300387 if (len > max_seg_sz)
388 len = max_seg_sz;
Adrian Hunter64f71202010-08-11 14:17:51 -0700389 if (sg)
390 sg = sg_next(sg);
391 else
392 sg = sglist;
393 if (!sg)
394 return -EINVAL;
395 sg_set_page(sg, mem->arr[i].page, len, 0);
396 sz -= len;
397 *sg_len += 1;
398 if (!sz)
399 break;
400 }
401 } while (sz && repeat);
402
403 if (sz)
404 return -EINVAL;
405
406 if (sg)
407 sg_mark_end(sg);
408
409 return 0;
410}
411
412/*
413 * Map memory into a scatterlist so that no pages are contiguous. Allow the
414 * same memory to be mapped more than once.
415 */
416static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
Adrian Hunterfec4dcc2010-08-11 14:17:51 -0700417 unsigned long sz,
Adrian Hunter64f71202010-08-11 14:17:51 -0700418 struct scatterlist *sglist,
419 unsigned int max_segs,
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300420 unsigned int max_seg_sz,
Adrian Hunter64f71202010-08-11 14:17:51 -0700421 unsigned int *sg_len)
422{
423 struct scatterlist *sg = NULL;
Adrian Hunterfec4dcc2010-08-11 14:17:51 -0700424 unsigned int i = mem->cnt, cnt;
425 unsigned long len;
Adrian Hunter64f71202010-08-11 14:17:51 -0700426 void *base, *addr, *last_addr = NULL;
427
428 sg_init_table(sglist, max_segs);
429
430 *sg_len = 0;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300431 while (sz) {
Adrian Hunter64f71202010-08-11 14:17:51 -0700432 base = page_address(mem->arr[--i].page);
433 cnt = 1 << mem->arr[i].order;
434 while (sz && cnt) {
435 addr = base + PAGE_SIZE * --cnt;
436 if (last_addr && last_addr + PAGE_SIZE == addr)
437 continue;
438 last_addr = addr;
439 len = PAGE_SIZE;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300440 if (len > max_seg_sz)
441 len = max_seg_sz;
442 if (len > sz)
Adrian Hunter64f71202010-08-11 14:17:51 -0700443 len = sz;
444 if (sg)
445 sg = sg_next(sg);
446 else
447 sg = sglist;
448 if (!sg)
449 return -EINVAL;
450 sg_set_page(sg, virt_to_page(addr), len, 0);
451 sz -= len;
452 *sg_len += 1;
453 }
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +0300454 if (i == 0)
455 i = mem->cnt;
Adrian Hunter64f71202010-08-11 14:17:51 -0700456 }
457
458 if (sg)
459 sg_mark_end(sg);
460
461 return 0;
462}
463
464/*
465 * Calculate transfer rate in bytes per second.
466 */
467static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
468{
469 uint64_t ns;
470
471 ns = ts->tv_sec;
472 ns *= 1000000000;
473 ns += ts->tv_nsec;
474
475 bytes *= 1000000000;
476
477 while (ns > UINT_MAX) {
478 bytes >>= 1;
479 ns >>= 1;
480 }
481
482 if (!ns)
483 return 0;
484
485 do_div(bytes, (uint32_t)ns);
486
487 return bytes;
488}
489
490/*
Andy Shevchenko3183aa12010-09-01 09:26:47 +0300491 * Save transfer results for future usage
492 */
493static void mmc_test_save_transfer_result(struct mmc_test_card *test,
494 unsigned int count, unsigned int sectors, struct timespec ts,
Adrian Hunterb6056d12011-02-08 13:41:02 +0200495 unsigned int rate, unsigned int iops)
Andy Shevchenko3183aa12010-09-01 09:26:47 +0300496{
497 struct mmc_test_transfer_result *tr;
498
499 if (!test->gr)
500 return;
501
502 tr = kmalloc(sizeof(struct mmc_test_transfer_result), GFP_KERNEL);
503 if (!tr)
504 return;
505
506 tr->count = count;
507 tr->sectors = sectors;
508 tr->ts = ts;
509 tr->rate = rate;
Adrian Hunterb6056d12011-02-08 13:41:02 +0200510 tr->iops = iops;
Andy Shevchenko3183aa12010-09-01 09:26:47 +0300511
512 list_add_tail(&tr->link, &test->gr->tr_lst);
513}
514
515/*
Adrian Hunter64f71202010-08-11 14:17:51 -0700516 * Print the transfer rate.
517 */
518static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
519 struct timespec *ts1, struct timespec *ts2)
520{
Adrian Hunterb6056d12011-02-08 13:41:02 +0200521 unsigned int rate, iops, sectors = bytes >> 9;
Adrian Hunter64f71202010-08-11 14:17:51 -0700522 struct timespec ts;
523
524 ts = timespec_sub(*ts2, *ts1);
525
526 rate = mmc_test_rate(bytes, &ts);
Adrian Hunterb6056d12011-02-08 13:41:02 +0200527 iops = mmc_test_rate(100, &ts); /* I/O ops per sec x 100 */
Adrian Hunter64f71202010-08-11 14:17:51 -0700528
529 printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
Adrian Hunterb6056d12011-02-08 13:41:02 +0200530 "seconds (%u kB/s, %u KiB/s, %u.%02u IOPS)\n",
Adrian Hunter64f71202010-08-11 14:17:51 -0700531 mmc_hostname(test->card->host), sectors, sectors >> 1,
Adrian Hunterc27d37a2010-09-23 14:51:36 +0300532 (sectors & 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
Adrian Hunterb6056d12011-02-08 13:41:02 +0200533 (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024,
534 iops / 100, iops % 100);
Andy Shevchenko3183aa12010-09-01 09:26:47 +0300535
Adrian Hunterb6056d12011-02-08 13:41:02 +0200536 mmc_test_save_transfer_result(test, 1, sectors, ts, rate, iops);
Adrian Hunter64f71202010-08-11 14:17:51 -0700537}
538
539/*
540 * Print the average transfer rate.
541 */
542static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
543 unsigned int count, struct timespec *ts1,
544 struct timespec *ts2)
545{
Adrian Hunterb6056d12011-02-08 13:41:02 +0200546 unsigned int rate, iops, sectors = bytes >> 9;
Adrian Hunter64f71202010-08-11 14:17:51 -0700547 uint64_t tot = bytes * count;
548 struct timespec ts;
549
550 ts = timespec_sub(*ts2, *ts1);
551
552 rate = mmc_test_rate(tot, &ts);
Adrian Hunterb6056d12011-02-08 13:41:02 +0200553 iops = mmc_test_rate(count * 100, &ts); /* I/O ops per sec x 100 */
Adrian Hunter64f71202010-08-11 14:17:51 -0700554
555 printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
Adrian Hunterb6056d12011-02-08 13:41:02 +0200556 "%lu.%09lu seconds (%u kB/s, %u KiB/s, "
557 "%u.%02u IOPS)\n",
Adrian Hunter64f71202010-08-11 14:17:51 -0700558 mmc_hostname(test->card->host), count, sectors, count,
Adrian Hunterc27d37a2010-09-23 14:51:36 +0300559 sectors >> 1, (sectors & 1 ? ".5" : ""),
Adrian Hunter64f71202010-08-11 14:17:51 -0700560 (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
Adrian Hunterb6056d12011-02-08 13:41:02 +0200561 rate / 1000, rate / 1024, iops / 100, iops % 100);
Andy Shevchenko3183aa12010-09-01 09:26:47 +0300562
Adrian Hunterb6056d12011-02-08 13:41:02 +0200563 mmc_test_save_transfer_result(test, count, sectors, ts, rate, iops);
Adrian Hunter64f71202010-08-11 14:17:51 -0700564}
565
566/*
567 * Return the card size in sectors.
568 */
569static unsigned int mmc_test_capacity(struct mmc_card *card)
570{
571 if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
572 return card->ext_csd.sectors;
573 else
574 return card->csd.capacity << (card->csd.read_blkbits - 9);
575}
576
Pierre Ossman6b174932008-06-30 09:09:27 +0200577/*******************************************************************/
578/* Test preparation and cleanup */
579/*******************************************************************/
580
581/*
582 * Fill the first couple of sectors of the card with known data
583 * so that bad reads/writes can be detected
584 */
585static int __mmc_test_prepare(struct mmc_test_card *test, int write)
Pierre Ossman88ae6002007-08-12 14:23:50 +0200586{
587 int ret, i;
588
589 ret = mmc_test_set_blksize(test, 512);
590 if (ret)
591 return ret;
592
593 if (write)
Pierre Ossman6b174932008-06-30 09:09:27 +0200594 memset(test->buffer, 0xDF, 512);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200595 else {
Pierre Ossman6b174932008-06-30 09:09:27 +0200596 for (i = 0;i < 512;i++)
Pierre Ossman88ae6002007-08-12 14:23:50 +0200597 test->buffer[i] = i;
598 }
599
600 for (i = 0;i < BUFFER_SIZE / 512;i++) {
Johan Kristellc286d032010-02-10 13:56:34 -0800601 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200602 if (ret)
603 return ret;
604 }
605
606 return 0;
607}
608
Pierre Ossman6b174932008-06-30 09:09:27 +0200609static int mmc_test_prepare_write(struct mmc_test_card *test)
Pierre Ossman88ae6002007-08-12 14:23:50 +0200610{
Pierre Ossman6b174932008-06-30 09:09:27 +0200611 return __mmc_test_prepare(test, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200612}
613
Pierre Ossman6b174932008-06-30 09:09:27 +0200614static int mmc_test_prepare_read(struct mmc_test_card *test)
Pierre Ossman88ae6002007-08-12 14:23:50 +0200615{
Pierre Ossman6b174932008-06-30 09:09:27 +0200616 return __mmc_test_prepare(test, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200617}
618
Pierre Ossman6b174932008-06-30 09:09:27 +0200619static int mmc_test_cleanup(struct mmc_test_card *test)
Pierre Ossman88ae6002007-08-12 14:23:50 +0200620{
Pierre Ossman6b174932008-06-30 09:09:27 +0200621 int ret, i;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200622
Pierre Ossman6b174932008-06-30 09:09:27 +0200623 ret = mmc_test_set_blksize(test, 512);
624 if (ret)
625 return ret;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200626
Pierre Ossman6b174932008-06-30 09:09:27 +0200627 memset(test->buffer, 0, 512);
628
629 for (i = 0;i < BUFFER_SIZE / 512;i++) {
Johan Kristellc286d032010-02-10 13:56:34 -0800630 ret = mmc_test_buffer_transfer(test, test->buffer, i, 512, 1);
Pierre Ossman6b174932008-06-30 09:09:27 +0200631 if (ret)
632 return ret;
633 }
634
635 return 0;
636}
637
638/*******************************************************************/
639/* Test execution helpers */
640/*******************************************************************/
641
642/*
643 * Modifies the mmc_request to perform the "short transfer" tests
644 */
645static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
646 struct mmc_request *mrq, int write)
647{
648 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
649
650 if (mrq->data->blocks > 1) {
651 mrq->cmd->opcode = write ?
652 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
653 mrq->stop = NULL;
654 } else {
655 mrq->cmd->opcode = MMC_SEND_STATUS;
656 mrq->cmd->arg = test->card->rca << 16;
657 }
658}
659
660/*
661 * Checks that a normal transfer didn't have any errors
662 */
663static int mmc_test_check_result(struct mmc_test_card *test,
664 struct mmc_request *mrq)
665{
666 int ret;
667
668 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
669
670 ret = 0;
671
672 if (!ret && mrq->cmd->error)
673 ret = mrq->cmd->error;
674 if (!ret && mrq->data->error)
675 ret = mrq->data->error;
676 if (!ret && mrq->stop && mrq->stop->error)
677 ret = mrq->stop->error;
678 if (!ret && mrq->data->bytes_xfered !=
679 mrq->data->blocks * mrq->data->blksz)
680 ret = RESULT_FAIL;
681
682 if (ret == -EINVAL)
683 ret = RESULT_UNSUP_HOST;
684
685 return ret;
686}
687
688/*
689 * Checks that a "short transfer" behaved as expected
690 */
691static int mmc_test_check_broken_result(struct mmc_test_card *test,
692 struct mmc_request *mrq)
693{
694 int ret;
695
696 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
697
698 ret = 0;
699
700 if (!ret && mrq->cmd->error)
701 ret = mrq->cmd->error;
702 if (!ret && mrq->data->error == 0)
703 ret = RESULT_FAIL;
704 if (!ret && mrq->data->error != -ETIMEDOUT)
705 ret = mrq->data->error;
706 if (!ret && mrq->stop && mrq->stop->error)
707 ret = mrq->stop->error;
708 if (mrq->data->blocks > 1) {
709 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
710 ret = RESULT_FAIL;
711 } else {
712 if (!ret && mrq->data->bytes_xfered > 0)
713 ret = RESULT_FAIL;
714 }
715
716 if (ret == -EINVAL)
717 ret = RESULT_UNSUP_HOST;
718
719 return ret;
720}
721
722/*
723 * Tests a basic transfer with certain parameters
724 */
725static int mmc_test_simple_transfer(struct mmc_test_card *test,
726 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
727 unsigned blocks, unsigned blksz, int write)
728{
Chris Ball24f5b532011-04-13 23:49:45 -0400729 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400730 struct mmc_command cmd = {0};
731 struct mmc_command stop = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400732 struct mmc_data data = {0};
Pierre Ossman6b174932008-06-30 09:09:27 +0200733
Pierre Ossman6b174932008-06-30 09:09:27 +0200734 mrq.cmd = &cmd;
735 mrq.data = &data;
736 mrq.stop = &stop;
737
738 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
739 blocks, blksz, write);
740
741 mmc_wait_for_req(test->card->host, &mrq);
742
743 mmc_test_wait_busy(test);
744
745 return mmc_test_check_result(test, &mrq);
746}
747
748/*
749 * Tests a transfer where the card will fail completely or partly
750 */
751static int mmc_test_broken_transfer(struct mmc_test_card *test,
752 unsigned blocks, unsigned blksz, int write)
753{
Chris Ball24f5b532011-04-13 23:49:45 -0400754 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400755 struct mmc_command cmd = {0};
756 struct mmc_command stop = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400757 struct mmc_data data = {0};
Pierre Ossman6b174932008-06-30 09:09:27 +0200758
759 struct scatterlist sg;
760
Pierre Ossman6b174932008-06-30 09:09:27 +0200761 mrq.cmd = &cmd;
762 mrq.data = &data;
763 mrq.stop = &stop;
764
765 sg_init_one(&sg, test->buffer, blocks * blksz);
766
767 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
768 mmc_test_prepare_broken_mrq(test, &mrq, write);
769
770 mmc_wait_for_req(test->card->host, &mrq);
771
772 mmc_test_wait_busy(test);
773
774 return mmc_test_check_broken_result(test, &mrq);
775}
776
777/*
778 * Does a complete transfer test where data is also validated
779 *
780 * Note: mmc_test_prepare() must have been done before this call
781 */
782static int mmc_test_transfer(struct mmc_test_card *test,
783 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
784 unsigned blocks, unsigned blksz, int write)
785{
786 int ret, i;
787 unsigned long flags;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200788
789 if (write) {
790 for (i = 0;i < blocks * blksz;i++)
Pierre Ossman6b174932008-06-30 09:09:27 +0200791 test->scratch[i] = i;
792 } else {
Pierre Ossmanb7ac2cf2008-07-29 01:05:22 +0200793 memset(test->scratch, 0, BUFFER_SIZE);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200794 }
Pierre Ossman6b174932008-06-30 09:09:27 +0200795 local_irq_save(flags);
Pierre Ossmanb7ac2cf2008-07-29 01:05:22 +0200796 sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
Pierre Ossman6b174932008-06-30 09:09:27 +0200797 local_irq_restore(flags);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200798
799 ret = mmc_test_set_blksize(test, blksz);
800 if (ret)
801 return ret;
802
Pierre Ossman6b174932008-06-30 09:09:27 +0200803 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
804 blocks, blksz, write);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200805 if (ret)
806 return ret;
807
808 if (write) {
Pierre Ossman6b174932008-06-30 09:09:27 +0200809 int sectors;
810
Pierre Ossman88ae6002007-08-12 14:23:50 +0200811 ret = mmc_test_set_blksize(test, 512);
812 if (ret)
813 return ret;
814
815 sectors = (blocks * blksz + 511) / 512;
816 if ((sectors * 512) == (blocks * blksz))
817 sectors++;
818
819 if ((sectors * 512) > BUFFER_SIZE)
820 return -EINVAL;
821
822 memset(test->buffer, 0, sectors * 512);
823
824 for (i = 0;i < sectors;i++) {
Pierre Ossman6b174932008-06-30 09:09:27 +0200825 ret = mmc_test_buffer_transfer(test,
Pierre Ossman88ae6002007-08-12 14:23:50 +0200826 test->buffer + i * 512,
Johan Kristellc286d032010-02-10 13:56:34 -0800827 dev_addr + i, 512, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200828 if (ret)
829 return ret;
830 }
831
832 for (i = 0;i < blocks * blksz;i++) {
833 if (test->buffer[i] != (u8)i)
834 return RESULT_FAIL;
835 }
836
837 for (;i < sectors * 512;i++) {
838 if (test->buffer[i] != 0xDF)
839 return RESULT_FAIL;
840 }
841 } else {
Pierre Ossman6b174932008-06-30 09:09:27 +0200842 local_irq_save(flags);
Pierre Ossmanb7ac2cf2008-07-29 01:05:22 +0200843 sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
Pierre Ossman6b174932008-06-30 09:09:27 +0200844 local_irq_restore(flags);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200845 for (i = 0;i < blocks * blksz;i++) {
Pierre Ossman6b174932008-06-30 09:09:27 +0200846 if (test->scratch[i] != (u8)i)
Pierre Ossman88ae6002007-08-12 14:23:50 +0200847 return RESULT_FAIL;
848 }
849 }
850
851 return 0;
852}
853
Pierre Ossman88ae6002007-08-12 14:23:50 +0200854/*******************************************************************/
855/* Tests */
856/*******************************************************************/
857
858struct mmc_test_case {
859 const char *name;
860
861 int (*prepare)(struct mmc_test_card *);
862 int (*run)(struct mmc_test_card *);
863 int (*cleanup)(struct mmc_test_card *);
864};
865
866static int mmc_test_basic_write(struct mmc_test_card *test)
867{
868 int ret;
Pierre Ossman6b174932008-06-30 09:09:27 +0200869 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200870
871 ret = mmc_test_set_blksize(test, 512);
872 if (ret)
873 return ret;
874
Pierre Ossman6b174932008-06-30 09:09:27 +0200875 sg_init_one(&sg, test->buffer, 512);
876
877 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200878 if (ret)
879 return ret;
880
881 return 0;
882}
883
884static int mmc_test_basic_read(struct mmc_test_card *test)
885{
886 int ret;
Pierre Ossman6b174932008-06-30 09:09:27 +0200887 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200888
889 ret = mmc_test_set_blksize(test, 512);
890 if (ret)
891 return ret;
892
Pierre Ossman6b174932008-06-30 09:09:27 +0200893 sg_init_one(&sg, test->buffer, 512);
894
Rabin Vincent58a5dd32009-02-13 22:55:26 +0530895 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200896 if (ret)
897 return ret;
898
899 return 0;
900}
901
902static int mmc_test_verify_write(struct mmc_test_card *test)
903{
904 int ret;
Pierre Ossman6b174932008-06-30 09:09:27 +0200905 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200906
Pierre Ossman6b174932008-06-30 09:09:27 +0200907 sg_init_one(&sg, test->buffer, 512);
908
909 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200910 if (ret)
911 return ret;
912
913 return 0;
914}
915
916static int mmc_test_verify_read(struct mmc_test_card *test)
917{
918 int ret;
Pierre Ossman6b174932008-06-30 09:09:27 +0200919 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200920
Pierre Ossman6b174932008-06-30 09:09:27 +0200921 sg_init_one(&sg, test->buffer, 512);
922
923 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200924 if (ret)
925 return ret;
926
927 return 0;
928}
929
930static int mmc_test_multi_write(struct mmc_test_card *test)
931{
932 int ret;
933 unsigned int size;
Pierre Ossman6b174932008-06-30 09:09:27 +0200934 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200935
936 if (test->card->host->max_blk_count == 1)
937 return RESULT_UNSUP_HOST;
938
939 size = PAGE_SIZE * 2;
940 size = min(size, test->card->host->max_req_size);
941 size = min(size, test->card->host->max_seg_size);
942 size = min(size, test->card->host->max_blk_count * 512);
943
944 if (size < 1024)
945 return RESULT_UNSUP_HOST;
946
Pierre Ossman6b174932008-06-30 09:09:27 +0200947 sg_init_one(&sg, test->buffer, size);
948
949 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200950 if (ret)
951 return ret;
952
953 return 0;
954}
955
956static int mmc_test_multi_read(struct mmc_test_card *test)
957{
958 int ret;
959 unsigned int size;
Pierre Ossman6b174932008-06-30 09:09:27 +0200960 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200961
962 if (test->card->host->max_blk_count == 1)
963 return RESULT_UNSUP_HOST;
964
965 size = PAGE_SIZE * 2;
966 size = min(size, test->card->host->max_req_size);
967 size = min(size, test->card->host->max_seg_size);
968 size = min(size, test->card->host->max_blk_count * 512);
969
970 if (size < 1024)
971 return RESULT_UNSUP_HOST;
972
Pierre Ossman6b174932008-06-30 09:09:27 +0200973 sg_init_one(&sg, test->buffer, size);
974
975 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200976 if (ret)
977 return ret;
978
979 return 0;
980}
981
982static int mmc_test_pow2_write(struct mmc_test_card *test)
983{
984 int ret, i;
Pierre Ossman6b174932008-06-30 09:09:27 +0200985 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +0200986
987 if (!test->card->csd.write_partial)
988 return RESULT_UNSUP_CARD;
989
990 for (i = 1; i < 512;i <<= 1) {
Pierre Ossman6b174932008-06-30 09:09:27 +0200991 sg_init_one(&sg, test->buffer, i);
992 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +0200993 if (ret)
994 return ret;
995 }
996
997 return 0;
998}
999
1000static int mmc_test_pow2_read(struct mmc_test_card *test)
1001{
1002 int ret, i;
Pierre Ossman6b174932008-06-30 09:09:27 +02001003 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +02001004
1005 if (!test->card->csd.read_partial)
1006 return RESULT_UNSUP_CARD;
1007
1008 for (i = 1; i < 512;i <<= 1) {
Pierre Ossman6b174932008-06-30 09:09:27 +02001009 sg_init_one(&sg, test->buffer, i);
1010 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001011 if (ret)
1012 return ret;
1013 }
1014
1015 return 0;
1016}
1017
1018static int mmc_test_weird_write(struct mmc_test_card *test)
1019{
1020 int ret, i;
Pierre Ossman6b174932008-06-30 09:09:27 +02001021 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +02001022
1023 if (!test->card->csd.write_partial)
1024 return RESULT_UNSUP_CARD;
1025
1026 for (i = 3; i < 512;i += 7) {
Pierre Ossman6b174932008-06-30 09:09:27 +02001027 sg_init_one(&sg, test->buffer, i);
1028 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001029 if (ret)
1030 return ret;
1031 }
1032
1033 return 0;
1034}
1035
1036static int mmc_test_weird_read(struct mmc_test_card *test)
1037{
1038 int ret, i;
Pierre Ossman6b174932008-06-30 09:09:27 +02001039 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +02001040
1041 if (!test->card->csd.read_partial)
1042 return RESULT_UNSUP_CARD;
1043
1044 for (i = 3; i < 512;i += 7) {
Pierre Ossman6b174932008-06-30 09:09:27 +02001045 sg_init_one(&sg, test->buffer, i);
1046 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001047 if (ret)
1048 return ret;
1049 }
1050
1051 return 0;
1052}
1053
1054static int mmc_test_align_write(struct mmc_test_card *test)
1055{
1056 int ret, i;
Pierre Ossman6b174932008-06-30 09:09:27 +02001057 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +02001058
1059 for (i = 1;i < 4;i++) {
Pierre Ossman6b174932008-06-30 09:09:27 +02001060 sg_init_one(&sg, test->buffer + i, 512);
1061 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001062 if (ret)
1063 return ret;
1064 }
1065
1066 return 0;
1067}
1068
1069static int mmc_test_align_read(struct mmc_test_card *test)
1070{
1071 int ret, i;
Pierre Ossman6b174932008-06-30 09:09:27 +02001072 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +02001073
1074 for (i = 1;i < 4;i++) {
Pierre Ossman6b174932008-06-30 09:09:27 +02001075 sg_init_one(&sg, test->buffer + i, 512);
1076 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001077 if (ret)
1078 return ret;
1079 }
1080
1081 return 0;
1082}
1083
1084static int mmc_test_align_multi_write(struct mmc_test_card *test)
1085{
1086 int ret, i;
1087 unsigned int size;
Pierre Ossman6b174932008-06-30 09:09:27 +02001088 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +02001089
1090 if (test->card->host->max_blk_count == 1)
1091 return RESULT_UNSUP_HOST;
1092
1093 size = PAGE_SIZE * 2;
1094 size = min(size, test->card->host->max_req_size);
1095 size = min(size, test->card->host->max_seg_size);
1096 size = min(size, test->card->host->max_blk_count * 512);
1097
1098 if (size < 1024)
1099 return RESULT_UNSUP_HOST;
1100
1101 for (i = 1;i < 4;i++) {
Pierre Ossman6b174932008-06-30 09:09:27 +02001102 sg_init_one(&sg, test->buffer + i, size);
1103 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001104 if (ret)
1105 return ret;
1106 }
1107
1108 return 0;
1109}
1110
1111static int mmc_test_align_multi_read(struct mmc_test_card *test)
1112{
1113 int ret, i;
1114 unsigned int size;
Pierre Ossman6b174932008-06-30 09:09:27 +02001115 struct scatterlist sg;
Pierre Ossman88ae6002007-08-12 14:23:50 +02001116
1117 if (test->card->host->max_blk_count == 1)
1118 return RESULT_UNSUP_HOST;
1119
1120 size = PAGE_SIZE * 2;
1121 size = min(size, test->card->host->max_req_size);
1122 size = min(size, test->card->host->max_seg_size);
1123 size = min(size, test->card->host->max_blk_count * 512);
1124
1125 if (size < 1024)
1126 return RESULT_UNSUP_HOST;
1127
1128 for (i = 1;i < 4;i++) {
Pierre Ossman6b174932008-06-30 09:09:27 +02001129 sg_init_one(&sg, test->buffer + i, size);
1130 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001131 if (ret)
1132 return ret;
1133 }
1134
1135 return 0;
1136}
1137
1138static int mmc_test_xfersize_write(struct mmc_test_card *test)
1139{
1140 int ret;
1141
1142 ret = mmc_test_set_blksize(test, 512);
1143 if (ret)
1144 return ret;
1145
Pierre Ossman6b174932008-06-30 09:09:27 +02001146 ret = mmc_test_broken_transfer(test, 1, 512, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001147 if (ret)
1148 return ret;
1149
1150 return 0;
1151}
1152
1153static int mmc_test_xfersize_read(struct mmc_test_card *test)
1154{
1155 int ret;
1156
1157 ret = mmc_test_set_blksize(test, 512);
1158 if (ret)
1159 return ret;
1160
Pierre Ossman6b174932008-06-30 09:09:27 +02001161 ret = mmc_test_broken_transfer(test, 1, 512, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001162 if (ret)
1163 return ret;
1164
1165 return 0;
1166}
1167
1168static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
1169{
1170 int ret;
1171
1172 if (test->card->host->max_blk_count == 1)
1173 return RESULT_UNSUP_HOST;
1174
1175 ret = mmc_test_set_blksize(test, 512);
1176 if (ret)
1177 return ret;
1178
Pierre Ossman6b174932008-06-30 09:09:27 +02001179 ret = mmc_test_broken_transfer(test, 2, 512, 1);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001180 if (ret)
1181 return ret;
1182
1183 return 0;
1184}
1185
1186static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
1187{
1188 int ret;
1189
1190 if (test->card->host->max_blk_count == 1)
1191 return RESULT_UNSUP_HOST;
1192
1193 ret = mmc_test_set_blksize(test, 512);
1194 if (ret)
1195 return ret;
1196
Pierre Ossman6b174932008-06-30 09:09:27 +02001197 ret = mmc_test_broken_transfer(test, 2, 512, 0);
Pierre Ossman88ae6002007-08-12 14:23:50 +02001198 if (ret)
1199 return ret;
1200
1201 return 0;
1202}
1203
Pierre Ossman26610812008-07-04 18:17:13 +02001204#ifdef CONFIG_HIGHMEM
1205
1206static int mmc_test_write_high(struct mmc_test_card *test)
1207{
1208 int ret;
1209 struct scatterlist sg;
1210
1211 sg_init_table(&sg, 1);
1212 sg_set_page(&sg, test->highmem, 512, 0);
1213
1214 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
1215 if (ret)
1216 return ret;
1217
1218 return 0;
1219}
1220
1221static int mmc_test_read_high(struct mmc_test_card *test)
1222{
1223 int ret;
1224 struct scatterlist sg;
1225
1226 sg_init_table(&sg, 1);
1227 sg_set_page(&sg, test->highmem, 512, 0);
1228
1229 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
1230 if (ret)
1231 return ret;
1232
1233 return 0;
1234}
1235
1236static int mmc_test_multi_write_high(struct mmc_test_card *test)
1237{
1238 int ret;
1239 unsigned int size;
1240 struct scatterlist sg;
1241
1242 if (test->card->host->max_blk_count == 1)
1243 return RESULT_UNSUP_HOST;
1244
1245 size = PAGE_SIZE * 2;
1246 size = min(size, test->card->host->max_req_size);
1247 size = min(size, test->card->host->max_seg_size);
1248 size = min(size, test->card->host->max_blk_count * 512);
1249
1250 if (size < 1024)
1251 return RESULT_UNSUP_HOST;
1252
1253 sg_init_table(&sg, 1);
1254 sg_set_page(&sg, test->highmem, size, 0);
1255
1256 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
1257 if (ret)
1258 return ret;
1259
1260 return 0;
1261}
1262
1263static int mmc_test_multi_read_high(struct mmc_test_card *test)
1264{
1265 int ret;
1266 unsigned int size;
1267 struct scatterlist sg;
1268
1269 if (test->card->host->max_blk_count == 1)
1270 return RESULT_UNSUP_HOST;
1271
1272 size = PAGE_SIZE * 2;
1273 size = min(size, test->card->host->max_req_size);
1274 size = min(size, test->card->host->max_seg_size);
1275 size = min(size, test->card->host->max_blk_count * 512);
1276
1277 if (size < 1024)
1278 return RESULT_UNSUP_HOST;
1279
1280 sg_init_table(&sg, 1);
1281 sg_set_page(&sg, test->highmem, size, 0);
1282
1283 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
1284 if (ret)
1285 return ret;
1286
1287 return 0;
1288}
1289
Adrian Hunter64f71202010-08-11 14:17:51 -07001290#else
1291
1292static int mmc_test_no_highmem(struct mmc_test_card *test)
1293{
1294 printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
1295 mmc_hostname(test->card->host));
1296 return 0;
1297}
1298
Pierre Ossman26610812008-07-04 18:17:13 +02001299#endif /* CONFIG_HIGHMEM */
1300
Adrian Hunter64f71202010-08-11 14:17:51 -07001301/*
1302 * Map sz bytes so that it can be transferred.
1303 */
Adrian Hunterfec4dcc2010-08-11 14:17:51 -07001304static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
Adrian Hunter64f71202010-08-11 14:17:51 -07001305 int max_scatter)
1306{
1307 struct mmc_test_area *t = &test->area;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001308 int err;
Adrian Hunter64f71202010-08-11 14:17:51 -07001309
1310 t->blocks = sz >> 9;
1311
1312 if (max_scatter) {
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001313 err = mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
1314 t->max_segs, t->max_seg_sz,
Adrian Hunter64f71202010-08-11 14:17:51 -07001315 &t->sg_len);
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001316 } else {
1317 err = mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
1318 t->max_seg_sz, &t->sg_len);
Adrian Hunter64f71202010-08-11 14:17:51 -07001319 }
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001320 if (err)
1321 printk(KERN_INFO "%s: Failed to map sg list\n",
1322 mmc_hostname(test->card->host));
1323 return err;
Adrian Hunter64f71202010-08-11 14:17:51 -07001324}
1325
1326/*
1327 * Transfer bytes mapped by mmc_test_area_map().
1328 */
1329static int mmc_test_area_transfer(struct mmc_test_card *test,
1330 unsigned int dev_addr, int write)
1331{
1332 struct mmc_test_area *t = &test->area;
1333
1334 return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
1335 t->blocks, 512, write);
1336}
1337
1338/*
1339 * Map and transfer bytes.
1340 */
Adrian Hunterfec4dcc2010-08-11 14:17:51 -07001341static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
Adrian Hunter64f71202010-08-11 14:17:51 -07001342 unsigned int dev_addr, int write, int max_scatter,
1343 int timed)
1344{
1345 struct timespec ts1, ts2;
1346 int ret;
1347
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001348 /*
1349 * In the case of a maximally scattered transfer, the maximum transfer
1350 * size is further limited by using PAGE_SIZE segments.
1351 */
1352 if (max_scatter) {
1353 struct mmc_test_area *t = &test->area;
1354 unsigned long max_tfr;
1355
1356 if (t->max_seg_sz >= PAGE_SIZE)
1357 max_tfr = t->max_segs * PAGE_SIZE;
1358 else
1359 max_tfr = t->max_segs * t->max_seg_sz;
1360 if (sz > max_tfr)
1361 sz = max_tfr;
1362 }
1363
Adrian Hunter64f71202010-08-11 14:17:51 -07001364 ret = mmc_test_area_map(test, sz, max_scatter);
1365 if (ret)
1366 return ret;
1367
1368 if (timed)
1369 getnstimeofday(&ts1);
1370
1371 ret = mmc_test_area_transfer(test, dev_addr, write);
1372 if (ret)
1373 return ret;
1374
1375 if (timed)
1376 getnstimeofday(&ts2);
1377
1378 if (timed)
1379 mmc_test_print_rate(test, sz, &ts1, &ts2);
1380
1381 return 0;
1382}
1383
1384/*
1385 * Write the test area entirely.
1386 */
1387static int mmc_test_area_fill(struct mmc_test_card *test)
1388{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001389 struct mmc_test_area *t = &test->area;
1390
1391 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, 1, 0, 0);
Adrian Hunter64f71202010-08-11 14:17:51 -07001392}
1393
1394/*
1395 * Erase the test area entirely.
1396 */
1397static int mmc_test_area_erase(struct mmc_test_card *test)
1398{
1399 struct mmc_test_area *t = &test->area;
1400
1401 if (!mmc_can_erase(test->card))
1402 return 0;
1403
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001404 return mmc_erase(test->card, t->dev_addr, t->max_sz >> 9,
Adrian Hunter64f71202010-08-11 14:17:51 -07001405 MMC_ERASE_ARG);
1406}
1407
1408/*
1409 * Cleanup struct mmc_test_area.
1410 */
1411static int mmc_test_area_cleanup(struct mmc_test_card *test)
1412{
1413 struct mmc_test_area *t = &test->area;
1414
1415 kfree(t->sg);
1416 mmc_test_free_mem(t->mem);
1417
1418 return 0;
1419}
1420
1421/*
Adrian Hunter0532ff62011-02-08 13:41:01 +02001422 * Initialize an area for testing large transfers. The test area is set to the
1423 * middle of the card because cards may have different charateristics at the
1424 * front (for FAT file system optimization). Optionally, the area is erased
1425 * (if the card supports it) which may improve write performance. Optionally,
1426 * the area is filled with data for subsequent read tests.
Adrian Hunter64f71202010-08-11 14:17:51 -07001427 */
1428static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
1429{
1430 struct mmc_test_area *t = &test->area;
Adrian Hunter0532ff62011-02-08 13:41:01 +02001431 unsigned long min_sz = 64 * 1024, sz;
Adrian Hunter64f71202010-08-11 14:17:51 -07001432 int ret;
1433
1434 ret = mmc_test_set_blksize(test, 512);
1435 if (ret)
1436 return ret;
1437
Adrian Hunter0532ff62011-02-08 13:41:01 +02001438 /* Make the test area size about 4MiB */
1439 sz = (unsigned long)test->card->pref_erase << 9;
1440 t->max_sz = sz;
1441 while (t->max_sz < 4 * 1024 * 1024)
1442 t->max_sz += sz;
1443 while (t->max_sz > TEST_AREA_MAX_SIZE && t->max_sz > sz)
1444 t->max_sz -= sz;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001445
1446 t->max_segs = test->card->host->max_segs;
1447 t->max_seg_sz = test->card->host->max_seg_size;
1448
1449 t->max_tfr = t->max_sz;
1450 if (t->max_tfr >> 9 > test->card->host->max_blk_count)
1451 t->max_tfr = test->card->host->max_blk_count << 9;
1452 if (t->max_tfr > test->card->host->max_req_size)
1453 t->max_tfr = test->card->host->max_req_size;
1454 if (t->max_tfr / t->max_seg_sz > t->max_segs)
1455 t->max_tfr = t->max_segs * t->max_seg_sz;
1456
Adrian Hunter64f71202010-08-11 14:17:51 -07001457 /*
Adrian Hunter3d203be2010-09-23 14:51:29 +03001458 * Try to allocate enough memory for a max. sized transfer. Less is OK
Adrian Hunter64f71202010-08-11 14:17:51 -07001459 * because the same memory can be mapped into the scatterlist more than
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001460 * once. Also, take into account the limits imposed on scatterlist
1461 * segments by the host driver.
Adrian Hunter64f71202010-08-11 14:17:51 -07001462 */
Adrian Hunter3d203be2010-09-23 14:51:29 +03001463 t->mem = mmc_test_alloc_mem(min_sz, t->max_tfr, t->max_segs,
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001464 t->max_seg_sz);
Adrian Hunter64f71202010-08-11 14:17:51 -07001465 if (!t->mem)
1466 return -ENOMEM;
1467
Adrian Hunter64f71202010-08-11 14:17:51 -07001468 t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
1469 if (!t->sg) {
1470 ret = -ENOMEM;
1471 goto out_free;
1472 }
1473
1474 t->dev_addr = mmc_test_capacity(test->card) / 2;
1475 t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
1476
1477 if (erase) {
1478 ret = mmc_test_area_erase(test);
1479 if (ret)
1480 goto out_free;
1481 }
1482
1483 if (fill) {
1484 ret = mmc_test_area_fill(test);
1485 if (ret)
1486 goto out_free;
1487 }
1488
1489 return 0;
1490
1491out_free:
1492 mmc_test_area_cleanup(test);
1493 return ret;
1494}
1495
1496/*
1497 * Prepare for large transfers. Do not erase the test area.
1498 */
1499static int mmc_test_area_prepare(struct mmc_test_card *test)
1500{
1501 return mmc_test_area_init(test, 0, 0);
1502}
1503
1504/*
1505 * Prepare for large transfers. Do erase the test area.
1506 */
1507static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
1508{
1509 return mmc_test_area_init(test, 1, 0);
1510}
1511
1512/*
1513 * Prepare for large transfers. Erase and fill the test area.
1514 */
1515static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
1516{
1517 return mmc_test_area_init(test, 1, 1);
1518}
1519
1520/*
1521 * Test best-case performance. Best-case performance is expected from
1522 * a single large transfer.
1523 *
1524 * An additional option (max_scatter) allows the measurement of the same
1525 * transfer but with no contiguous pages in the scatter list. This tests
1526 * the efficiency of DMA to handle scattered pages.
1527 */
1528static int mmc_test_best_performance(struct mmc_test_card *test, int write,
1529 int max_scatter)
1530{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001531 struct mmc_test_area *t = &test->area;
1532
1533 return mmc_test_area_io(test, t->max_tfr, t->dev_addr, write,
1534 max_scatter, 1);
Adrian Hunter64f71202010-08-11 14:17:51 -07001535}
1536
1537/*
1538 * Best-case read performance.
1539 */
1540static int mmc_test_best_read_performance(struct mmc_test_card *test)
1541{
1542 return mmc_test_best_performance(test, 0, 0);
1543}
1544
1545/*
1546 * Best-case write performance.
1547 */
1548static int mmc_test_best_write_performance(struct mmc_test_card *test)
1549{
1550 return mmc_test_best_performance(test, 1, 0);
1551}
1552
1553/*
1554 * Best-case read performance into scattered pages.
1555 */
1556static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
1557{
1558 return mmc_test_best_performance(test, 0, 1);
1559}
1560
1561/*
1562 * Best-case write performance from scattered pages.
1563 */
1564static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
1565{
1566 return mmc_test_best_performance(test, 1, 1);
1567}
1568
1569/*
1570 * Single read performance by transfer size.
1571 */
1572static int mmc_test_profile_read_perf(struct mmc_test_card *test)
1573{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001574 struct mmc_test_area *t = &test->area;
Adrian Hunterfec4dcc2010-08-11 14:17:51 -07001575 unsigned long sz;
1576 unsigned int dev_addr;
Adrian Hunter64f71202010-08-11 14:17:51 -07001577 int ret;
1578
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001579 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1580 dev_addr = t->dev_addr + (sz >> 9);
Adrian Hunter64f71202010-08-11 14:17:51 -07001581 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1582 if (ret)
1583 return ret;
1584 }
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001585 sz = t->max_tfr;
1586 dev_addr = t->dev_addr;
Adrian Hunter64f71202010-08-11 14:17:51 -07001587 return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1588}
1589
1590/*
1591 * Single write performance by transfer size.
1592 */
1593static int mmc_test_profile_write_perf(struct mmc_test_card *test)
1594{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001595 struct mmc_test_area *t = &test->area;
Adrian Hunterfec4dcc2010-08-11 14:17:51 -07001596 unsigned long sz;
1597 unsigned int dev_addr;
Adrian Hunter64f71202010-08-11 14:17:51 -07001598 int ret;
1599
1600 ret = mmc_test_area_erase(test);
1601 if (ret)
1602 return ret;
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001603 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
1604 dev_addr = t->dev_addr + (sz >> 9);
Adrian Hunter64f71202010-08-11 14:17:51 -07001605 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1606 if (ret)
1607 return ret;
1608 }
1609 ret = mmc_test_area_erase(test);
1610 if (ret)
1611 return ret;
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001612 sz = t->max_tfr;
1613 dev_addr = t->dev_addr;
Adrian Hunter64f71202010-08-11 14:17:51 -07001614 return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1615}
1616
1617/*
1618 * Single trim performance by transfer size.
1619 */
1620static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
1621{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001622 struct mmc_test_area *t = &test->area;
Adrian Hunterfec4dcc2010-08-11 14:17:51 -07001623 unsigned long sz;
1624 unsigned int dev_addr;
Adrian Hunter64f71202010-08-11 14:17:51 -07001625 struct timespec ts1, ts2;
1626 int ret;
1627
1628 if (!mmc_can_trim(test->card))
1629 return RESULT_UNSUP_CARD;
1630
1631 if (!mmc_can_erase(test->card))
1632 return RESULT_UNSUP_HOST;
1633
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001634 for (sz = 512; sz < t->max_sz; sz <<= 1) {
1635 dev_addr = t->dev_addr + (sz >> 9);
Adrian Hunter64f71202010-08-11 14:17:51 -07001636 getnstimeofday(&ts1);
1637 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1638 if (ret)
1639 return ret;
1640 getnstimeofday(&ts2);
1641 mmc_test_print_rate(test, sz, &ts1, &ts2);
1642 }
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001643 dev_addr = t->dev_addr;
Adrian Hunter64f71202010-08-11 14:17:51 -07001644 getnstimeofday(&ts1);
1645 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1646 if (ret)
1647 return ret;
1648 getnstimeofday(&ts2);
1649 mmc_test_print_rate(test, sz, &ts1, &ts2);
1650 return 0;
1651}
1652
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001653static int mmc_test_seq_read_perf(struct mmc_test_card *test, unsigned long sz)
1654{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001655 struct mmc_test_area *t = &test->area;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001656 unsigned int dev_addr, i, cnt;
1657 struct timespec ts1, ts2;
1658 int ret;
1659
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001660 cnt = t->max_sz / sz;
1661 dev_addr = t->dev_addr;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001662 getnstimeofday(&ts1);
1663 for (i = 0; i < cnt; i++) {
1664 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
1665 if (ret)
1666 return ret;
1667 dev_addr += (sz >> 9);
1668 }
1669 getnstimeofday(&ts2);
1670 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1671 return 0;
1672}
1673
Adrian Hunter64f71202010-08-11 14:17:51 -07001674/*
1675 * Consecutive read performance by transfer size.
1676 */
1677static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
1678{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001679 struct mmc_test_area *t = &test->area;
Adrian Hunterfec4dcc2010-08-11 14:17:51 -07001680 unsigned long sz;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001681 int ret;
1682
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001683 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001684 ret = mmc_test_seq_read_perf(test, sz);
1685 if (ret)
1686 return ret;
1687 }
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001688 sz = t->max_tfr;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001689 return mmc_test_seq_read_perf(test, sz);
1690}
1691
1692static int mmc_test_seq_write_perf(struct mmc_test_card *test, unsigned long sz)
1693{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001694 struct mmc_test_area *t = &test->area;
Adrian Hunterfec4dcc2010-08-11 14:17:51 -07001695 unsigned int dev_addr, i, cnt;
Adrian Hunter64f71202010-08-11 14:17:51 -07001696 struct timespec ts1, ts2;
1697 int ret;
1698
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001699 ret = mmc_test_area_erase(test);
1700 if (ret)
1701 return ret;
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001702 cnt = t->max_sz / sz;
1703 dev_addr = t->dev_addr;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001704 getnstimeofday(&ts1);
1705 for (i = 0; i < cnt; i++) {
1706 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
1707 if (ret)
1708 return ret;
1709 dev_addr += (sz >> 9);
Adrian Hunter64f71202010-08-11 14:17:51 -07001710 }
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001711 getnstimeofday(&ts2);
1712 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
Adrian Hunter64f71202010-08-11 14:17:51 -07001713 return 0;
1714}
1715
1716/*
1717 * Consecutive write performance by transfer size.
1718 */
1719static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
1720{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001721 struct mmc_test_area *t = &test->area;
Adrian Hunterfec4dcc2010-08-11 14:17:51 -07001722 unsigned long sz;
Adrian Hunter64f71202010-08-11 14:17:51 -07001723 int ret;
1724
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001725 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001726 ret = mmc_test_seq_write_perf(test, sz);
Adrian Hunter64f71202010-08-11 14:17:51 -07001727 if (ret)
1728 return ret;
Adrian Hunter64f71202010-08-11 14:17:51 -07001729 }
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001730 sz = t->max_tfr;
Adrian Hunterc8c8c1b2010-09-10 11:33:45 +03001731 return mmc_test_seq_write_perf(test, sz);
Adrian Hunter64f71202010-08-11 14:17:51 -07001732}
1733
1734/*
1735 * Consecutive trim performance by transfer size.
1736 */
1737static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
1738{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001739 struct mmc_test_area *t = &test->area;
Adrian Hunterfec4dcc2010-08-11 14:17:51 -07001740 unsigned long sz;
1741 unsigned int dev_addr, i, cnt;
Adrian Hunter64f71202010-08-11 14:17:51 -07001742 struct timespec ts1, ts2;
1743 int ret;
1744
1745 if (!mmc_can_trim(test->card))
1746 return RESULT_UNSUP_CARD;
1747
1748 if (!mmc_can_erase(test->card))
1749 return RESULT_UNSUP_HOST;
1750
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001751 for (sz = 512; sz <= t->max_sz; sz <<= 1) {
Adrian Hunter64f71202010-08-11 14:17:51 -07001752 ret = mmc_test_area_erase(test);
1753 if (ret)
1754 return ret;
1755 ret = mmc_test_area_fill(test);
1756 if (ret)
1757 return ret;
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001758 cnt = t->max_sz / sz;
1759 dev_addr = t->dev_addr;
Adrian Hunter64f71202010-08-11 14:17:51 -07001760 getnstimeofday(&ts1);
1761 for (i = 0; i < cnt; i++) {
1762 ret = mmc_erase(test->card, dev_addr, sz >> 9,
1763 MMC_TRIM_ARG);
1764 if (ret)
1765 return ret;
1766 dev_addr += (sz >> 9);
1767 }
1768 getnstimeofday(&ts2);
1769 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1770 }
1771 return 0;
1772}
1773
Adrian Hunterb6056d12011-02-08 13:41:02 +02001774static unsigned int rnd_next = 1;
1775
1776static unsigned int mmc_test_rnd_num(unsigned int rnd_cnt)
1777{
1778 uint64_t r;
1779
1780 rnd_next = rnd_next * 1103515245 + 12345;
1781 r = (rnd_next >> 16) & 0x7fff;
1782 return (r * rnd_cnt) >> 15;
1783}
1784
1785static int mmc_test_rnd_perf(struct mmc_test_card *test, int write, int print,
1786 unsigned long sz)
1787{
1788 unsigned int dev_addr, cnt, rnd_addr, range1, range2, last_ea = 0, ea;
1789 unsigned int ssz;
1790 struct timespec ts1, ts2, ts;
1791 int ret;
1792
1793 ssz = sz >> 9;
1794
1795 rnd_addr = mmc_test_capacity(test->card) / 4;
1796 range1 = rnd_addr / test->card->pref_erase;
1797 range2 = range1 / ssz;
1798
1799 getnstimeofday(&ts1);
1800 for (cnt = 0; cnt < UINT_MAX; cnt++) {
1801 getnstimeofday(&ts2);
1802 ts = timespec_sub(ts2, ts1);
1803 if (ts.tv_sec >= 10)
1804 break;
1805 ea = mmc_test_rnd_num(range1);
1806 if (ea == last_ea)
1807 ea -= 1;
1808 last_ea = ea;
1809 dev_addr = rnd_addr + test->card->pref_erase * ea +
1810 ssz * mmc_test_rnd_num(range2);
1811 ret = mmc_test_area_io(test, sz, dev_addr, write, 0, 0);
1812 if (ret)
1813 return ret;
1814 }
1815 if (print)
1816 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1817 return 0;
1818}
1819
1820static int mmc_test_random_perf(struct mmc_test_card *test, int write)
1821{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001822 struct mmc_test_area *t = &test->area;
Adrian Hunterb6056d12011-02-08 13:41:02 +02001823 unsigned int next;
1824 unsigned long sz;
1825 int ret;
1826
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001827 for (sz = 512; sz < t->max_tfr; sz <<= 1) {
Adrian Hunterb6056d12011-02-08 13:41:02 +02001828 /*
1829 * When writing, try to get more consistent results by running
1830 * the test twice with exactly the same I/O but outputting the
1831 * results only for the 2nd run.
1832 */
1833 if (write) {
1834 next = rnd_next;
1835 ret = mmc_test_rnd_perf(test, write, 0, sz);
1836 if (ret)
1837 return ret;
1838 rnd_next = next;
1839 }
1840 ret = mmc_test_rnd_perf(test, write, 1, sz);
1841 if (ret)
1842 return ret;
1843 }
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001844 sz = t->max_tfr;
Adrian Hunterb6056d12011-02-08 13:41:02 +02001845 if (write) {
1846 next = rnd_next;
1847 ret = mmc_test_rnd_perf(test, write, 0, sz);
1848 if (ret)
1849 return ret;
1850 rnd_next = next;
1851 }
1852 return mmc_test_rnd_perf(test, write, 1, sz);
1853}
1854
1855/*
1856 * Random read performance by transfer size.
1857 */
1858static int mmc_test_random_read_perf(struct mmc_test_card *test)
1859{
1860 return mmc_test_random_perf(test, 0);
1861}
1862
1863/*
1864 * Random write performance by transfer size.
1865 */
1866static int mmc_test_random_write_perf(struct mmc_test_card *test)
1867{
1868 return mmc_test_random_perf(test, 1);
1869}
1870
Adrian Huntera803d552011-02-08 13:41:03 +02001871static int mmc_test_seq_perf(struct mmc_test_card *test, int write,
1872 unsigned int tot_sz, int max_scatter)
1873{
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001874 struct mmc_test_area *t = &test->area;
Adrian Huntera803d552011-02-08 13:41:03 +02001875 unsigned int dev_addr, i, cnt, sz, ssz;
Chris Ball5a8fba52011-03-16 17:46:45 -04001876 struct timespec ts1, ts2;
Adrian Huntera803d552011-02-08 13:41:03 +02001877 int ret;
1878
Andy Shevchenko253d6a22011-05-10 15:59:01 +03001879 sz = t->max_tfr;
1880
Adrian Huntera803d552011-02-08 13:41:03 +02001881 /*
1882 * In the case of a maximally scattered transfer, the maximum transfer
1883 * size is further limited by using PAGE_SIZE segments.
1884 */
1885 if (max_scatter) {
Adrian Huntera803d552011-02-08 13:41:03 +02001886 unsigned long max_tfr;
1887
1888 if (t->max_seg_sz >= PAGE_SIZE)
1889 max_tfr = t->max_segs * PAGE_SIZE;
1890 else
1891 max_tfr = t->max_segs * t->max_seg_sz;
1892 if (sz > max_tfr)
1893 sz = max_tfr;
1894 }
1895
1896 ssz = sz >> 9;
1897 dev_addr = mmc_test_capacity(test->card) / 4;
1898 if (tot_sz > dev_addr << 9)
1899 tot_sz = dev_addr << 9;
1900 cnt = tot_sz / sz;
1901 dev_addr &= 0xffff0000; /* Round to 64MiB boundary */
1902
1903 getnstimeofday(&ts1);
1904 for (i = 0; i < cnt; i++) {
1905 ret = mmc_test_area_io(test, sz, dev_addr, write,
1906 max_scatter, 0);
1907 if (ret)
1908 return ret;
1909 dev_addr += ssz;
1910 }
1911 getnstimeofday(&ts2);
1912
Adrian Huntera803d552011-02-08 13:41:03 +02001913 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1914
1915 return 0;
1916}
1917
1918static int mmc_test_large_seq_perf(struct mmc_test_card *test, int write)
1919{
1920 int ret, i;
1921
1922 for (i = 0; i < 10; i++) {
1923 ret = mmc_test_seq_perf(test, write, 10 * 1024 * 1024, 1);
1924 if (ret)
1925 return ret;
1926 }
1927 for (i = 0; i < 5; i++) {
1928 ret = mmc_test_seq_perf(test, write, 100 * 1024 * 1024, 1);
1929 if (ret)
1930 return ret;
1931 }
1932 for (i = 0; i < 3; i++) {
1933 ret = mmc_test_seq_perf(test, write, 1000 * 1024 * 1024, 1);
1934 if (ret)
1935 return ret;
1936 }
1937
1938 return ret;
1939}
1940
1941/*
1942 * Large sequential read performance.
1943 */
1944static int mmc_test_large_seq_read_perf(struct mmc_test_card *test)
1945{
1946 return mmc_test_large_seq_perf(test, 0);
1947}
1948
1949/*
1950 * Large sequential write performance.
1951 */
1952static int mmc_test_large_seq_write_perf(struct mmc_test_card *test)
1953{
1954 return mmc_test_large_seq_perf(test, 1);
1955}
1956
Pierre Ossman88ae6002007-08-12 14:23:50 +02001957static const struct mmc_test_case mmc_test_cases[] = {
1958 {
1959 .name = "Basic write (no data verification)",
1960 .run = mmc_test_basic_write,
1961 },
1962
1963 {
1964 .name = "Basic read (no data verification)",
1965 .run = mmc_test_basic_read,
1966 },
1967
1968 {
1969 .name = "Basic write (with data verification)",
Pierre Ossman6b174932008-06-30 09:09:27 +02001970 .prepare = mmc_test_prepare_write,
Pierre Ossman88ae6002007-08-12 14:23:50 +02001971 .run = mmc_test_verify_write,
Pierre Ossman6b174932008-06-30 09:09:27 +02001972 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02001973 },
1974
1975 {
1976 .name = "Basic read (with data verification)",
Pierre Ossman6b174932008-06-30 09:09:27 +02001977 .prepare = mmc_test_prepare_read,
Pierre Ossman88ae6002007-08-12 14:23:50 +02001978 .run = mmc_test_verify_read,
Pierre Ossman6b174932008-06-30 09:09:27 +02001979 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02001980 },
1981
1982 {
1983 .name = "Multi-block write",
Pierre Ossman6b174932008-06-30 09:09:27 +02001984 .prepare = mmc_test_prepare_write,
Pierre Ossman88ae6002007-08-12 14:23:50 +02001985 .run = mmc_test_multi_write,
Pierre Ossman6b174932008-06-30 09:09:27 +02001986 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02001987 },
1988
1989 {
1990 .name = "Multi-block read",
Pierre Ossman6b174932008-06-30 09:09:27 +02001991 .prepare = mmc_test_prepare_read,
Pierre Ossman88ae6002007-08-12 14:23:50 +02001992 .run = mmc_test_multi_read,
Pierre Ossman6b174932008-06-30 09:09:27 +02001993 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02001994 },
1995
1996 {
1997 .name = "Power of two block writes",
Pierre Ossman6b174932008-06-30 09:09:27 +02001998 .prepare = mmc_test_prepare_write,
Pierre Ossman88ae6002007-08-12 14:23:50 +02001999 .run = mmc_test_pow2_write,
Pierre Ossman6b174932008-06-30 09:09:27 +02002000 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002001 },
2002
2003 {
2004 .name = "Power of two block reads",
Pierre Ossman6b174932008-06-30 09:09:27 +02002005 .prepare = mmc_test_prepare_read,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002006 .run = mmc_test_pow2_read,
Pierre Ossman6b174932008-06-30 09:09:27 +02002007 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002008 },
2009
2010 {
2011 .name = "Weird sized block writes",
Pierre Ossman6b174932008-06-30 09:09:27 +02002012 .prepare = mmc_test_prepare_write,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002013 .run = mmc_test_weird_write,
Pierre Ossman6b174932008-06-30 09:09:27 +02002014 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002015 },
2016
2017 {
2018 .name = "Weird sized block reads",
Pierre Ossman6b174932008-06-30 09:09:27 +02002019 .prepare = mmc_test_prepare_read,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002020 .run = mmc_test_weird_read,
Pierre Ossman6b174932008-06-30 09:09:27 +02002021 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002022 },
2023
2024 {
2025 .name = "Badly aligned write",
Pierre Ossman6b174932008-06-30 09:09:27 +02002026 .prepare = mmc_test_prepare_write,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002027 .run = mmc_test_align_write,
Pierre Ossman6b174932008-06-30 09:09:27 +02002028 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002029 },
2030
2031 {
2032 .name = "Badly aligned read",
Pierre Ossman6b174932008-06-30 09:09:27 +02002033 .prepare = mmc_test_prepare_read,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002034 .run = mmc_test_align_read,
Pierre Ossman6b174932008-06-30 09:09:27 +02002035 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002036 },
2037
2038 {
2039 .name = "Badly aligned multi-block write",
Pierre Ossman6b174932008-06-30 09:09:27 +02002040 .prepare = mmc_test_prepare_write,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002041 .run = mmc_test_align_multi_write,
Pierre Ossman6b174932008-06-30 09:09:27 +02002042 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002043 },
2044
2045 {
2046 .name = "Badly aligned multi-block read",
Pierre Ossman6b174932008-06-30 09:09:27 +02002047 .prepare = mmc_test_prepare_read,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002048 .run = mmc_test_align_multi_read,
Pierre Ossman6b174932008-06-30 09:09:27 +02002049 .cleanup = mmc_test_cleanup,
Pierre Ossman88ae6002007-08-12 14:23:50 +02002050 },
2051
2052 {
2053 .name = "Correct xfer_size at write (start failure)",
2054 .run = mmc_test_xfersize_write,
2055 },
2056
2057 {
2058 .name = "Correct xfer_size at read (start failure)",
2059 .run = mmc_test_xfersize_read,
2060 },
2061
2062 {
2063 .name = "Correct xfer_size at write (midway failure)",
2064 .run = mmc_test_multi_xfersize_write,
2065 },
2066
2067 {
2068 .name = "Correct xfer_size at read (midway failure)",
2069 .run = mmc_test_multi_xfersize_read,
2070 },
Pierre Ossman26610812008-07-04 18:17:13 +02002071
2072#ifdef CONFIG_HIGHMEM
2073
2074 {
2075 .name = "Highmem write",
2076 .prepare = mmc_test_prepare_write,
2077 .run = mmc_test_write_high,
2078 .cleanup = mmc_test_cleanup,
2079 },
2080
2081 {
2082 .name = "Highmem read",
2083 .prepare = mmc_test_prepare_read,
2084 .run = mmc_test_read_high,
2085 .cleanup = mmc_test_cleanup,
2086 },
2087
2088 {
2089 .name = "Multi-block highmem write",
2090 .prepare = mmc_test_prepare_write,
2091 .run = mmc_test_multi_write_high,
2092 .cleanup = mmc_test_cleanup,
2093 },
2094
2095 {
2096 .name = "Multi-block highmem read",
2097 .prepare = mmc_test_prepare_read,
2098 .run = mmc_test_multi_read_high,
2099 .cleanup = mmc_test_cleanup,
2100 },
2101
Adrian Hunter64f71202010-08-11 14:17:51 -07002102#else
2103
2104 {
2105 .name = "Highmem write",
2106 .run = mmc_test_no_highmem,
2107 },
2108
2109 {
2110 .name = "Highmem read",
2111 .run = mmc_test_no_highmem,
2112 },
2113
2114 {
2115 .name = "Multi-block highmem write",
2116 .run = mmc_test_no_highmem,
2117 },
2118
2119 {
2120 .name = "Multi-block highmem read",
2121 .run = mmc_test_no_highmem,
2122 },
2123
Pierre Ossman26610812008-07-04 18:17:13 +02002124#endif /* CONFIG_HIGHMEM */
2125
Adrian Hunter64f71202010-08-11 14:17:51 -07002126 {
2127 .name = "Best-case read performance",
2128 .prepare = mmc_test_area_prepare_fill,
2129 .run = mmc_test_best_read_performance,
2130 .cleanup = mmc_test_area_cleanup,
2131 },
2132
2133 {
2134 .name = "Best-case write performance",
2135 .prepare = mmc_test_area_prepare_erase,
2136 .run = mmc_test_best_write_performance,
2137 .cleanup = mmc_test_area_cleanup,
2138 },
2139
2140 {
2141 .name = "Best-case read performance into scattered pages",
2142 .prepare = mmc_test_area_prepare_fill,
2143 .run = mmc_test_best_read_perf_max_scatter,
2144 .cleanup = mmc_test_area_cleanup,
2145 },
2146
2147 {
2148 .name = "Best-case write performance from scattered pages",
2149 .prepare = mmc_test_area_prepare_erase,
2150 .run = mmc_test_best_write_perf_max_scatter,
2151 .cleanup = mmc_test_area_cleanup,
2152 },
2153
2154 {
2155 .name = "Single read performance by transfer size",
2156 .prepare = mmc_test_area_prepare_fill,
2157 .run = mmc_test_profile_read_perf,
2158 .cleanup = mmc_test_area_cleanup,
2159 },
2160
2161 {
2162 .name = "Single write performance by transfer size",
2163 .prepare = mmc_test_area_prepare,
2164 .run = mmc_test_profile_write_perf,
2165 .cleanup = mmc_test_area_cleanup,
2166 },
2167
2168 {
2169 .name = "Single trim performance by transfer size",
2170 .prepare = mmc_test_area_prepare_fill,
2171 .run = mmc_test_profile_trim_perf,
2172 .cleanup = mmc_test_area_cleanup,
2173 },
2174
2175 {
2176 .name = "Consecutive read performance by transfer size",
2177 .prepare = mmc_test_area_prepare_fill,
2178 .run = mmc_test_profile_seq_read_perf,
2179 .cleanup = mmc_test_area_cleanup,
2180 },
2181
2182 {
2183 .name = "Consecutive write performance by transfer size",
2184 .prepare = mmc_test_area_prepare,
2185 .run = mmc_test_profile_seq_write_perf,
2186 .cleanup = mmc_test_area_cleanup,
2187 },
2188
2189 {
2190 .name = "Consecutive trim performance by transfer size",
2191 .prepare = mmc_test_area_prepare,
2192 .run = mmc_test_profile_seq_trim_perf,
2193 .cleanup = mmc_test_area_cleanup,
2194 },
2195
Adrian Hunterb6056d12011-02-08 13:41:02 +02002196 {
2197 .name = "Random read performance by transfer size",
2198 .prepare = mmc_test_area_prepare,
2199 .run = mmc_test_random_read_perf,
2200 .cleanup = mmc_test_area_cleanup,
2201 },
2202
2203 {
2204 .name = "Random write performance by transfer size",
2205 .prepare = mmc_test_area_prepare,
2206 .run = mmc_test_random_write_perf,
2207 .cleanup = mmc_test_area_cleanup,
2208 },
2209
Adrian Huntera803d552011-02-08 13:41:03 +02002210 {
2211 .name = "Large sequential read into scattered pages",
2212 .prepare = mmc_test_area_prepare,
2213 .run = mmc_test_large_seq_read_perf,
2214 .cleanup = mmc_test_area_cleanup,
2215 },
2216
2217 {
2218 .name = "Large sequential write from scattered pages",
2219 .prepare = mmc_test_area_prepare,
2220 .run = mmc_test_large_seq_write_perf,
2221 .cleanup = mmc_test_area_cleanup,
2222 },
2223
Pierre Ossman88ae6002007-08-12 14:23:50 +02002224};
2225
Akinobu Mitaa6500312008-09-13 19:03:32 +09002226static DEFINE_MUTEX(mmc_test_lock);
Pierre Ossman88ae6002007-08-12 14:23:50 +02002227
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002228static LIST_HEAD(mmc_test_result);
2229
Pierre Ossmanfd8c3262008-05-24 22:36:31 +02002230static void mmc_test_run(struct mmc_test_card *test, int testcase)
Pierre Ossman88ae6002007-08-12 14:23:50 +02002231{
2232 int i, ret;
2233
2234 printk(KERN_INFO "%s: Starting tests of card %s...\n",
2235 mmc_hostname(test->card->host), mmc_card_id(test->card));
2236
2237 mmc_claim_host(test->card->host);
2238
2239 for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002240 struct mmc_test_general_result *gr;
2241
Pierre Ossmanfd8c3262008-05-24 22:36:31 +02002242 if (testcase && ((i + 1) != testcase))
2243 continue;
2244
Pierre Ossman88ae6002007-08-12 14:23:50 +02002245 printk(KERN_INFO "%s: Test case %d. %s...\n",
2246 mmc_hostname(test->card->host), i + 1,
2247 mmc_test_cases[i].name);
2248
2249 if (mmc_test_cases[i].prepare) {
2250 ret = mmc_test_cases[i].prepare(test);
2251 if (ret) {
2252 printk(KERN_INFO "%s: Result: Prepare "
2253 "stage failed! (%d)\n",
2254 mmc_hostname(test->card->host),
2255 ret);
2256 continue;
2257 }
2258 }
2259
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002260 gr = kzalloc(sizeof(struct mmc_test_general_result),
2261 GFP_KERNEL);
2262 if (gr) {
2263 INIT_LIST_HEAD(&gr->tr_lst);
2264
2265 /* Assign data what we know already */
2266 gr->card = test->card;
2267 gr->testcase = i;
2268
2269 /* Append container to global one */
2270 list_add_tail(&gr->link, &mmc_test_result);
2271
2272 /*
2273 * Save the pointer to created container in our private
2274 * structure.
2275 */
2276 test->gr = gr;
2277 }
2278
Pierre Ossman88ae6002007-08-12 14:23:50 +02002279 ret = mmc_test_cases[i].run(test);
2280 switch (ret) {
2281 case RESULT_OK:
2282 printk(KERN_INFO "%s: Result: OK\n",
2283 mmc_hostname(test->card->host));
2284 break;
2285 case RESULT_FAIL:
2286 printk(KERN_INFO "%s: Result: FAILED\n",
2287 mmc_hostname(test->card->host));
2288 break;
2289 case RESULT_UNSUP_HOST:
2290 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2291 "(by host)\n",
2292 mmc_hostname(test->card->host));
2293 break;
2294 case RESULT_UNSUP_CARD:
2295 printk(KERN_INFO "%s: Result: UNSUPPORTED "
2296 "(by card)\n",
2297 mmc_hostname(test->card->host));
2298 break;
2299 default:
2300 printk(KERN_INFO "%s: Result: ERROR (%d)\n",
2301 mmc_hostname(test->card->host), ret);
2302 }
2303
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002304 /* Save the result */
2305 if (gr)
2306 gr->result = ret;
2307
Pierre Ossman88ae6002007-08-12 14:23:50 +02002308 if (mmc_test_cases[i].cleanup) {
2309 ret = mmc_test_cases[i].cleanup(test);
2310 if (ret) {
2311 printk(KERN_INFO "%s: Warning: Cleanup "
2312 "stage failed! (%d)\n",
2313 mmc_hostname(test->card->host),
2314 ret);
2315 }
2316 }
2317 }
2318
2319 mmc_release_host(test->card->host);
2320
2321 printk(KERN_INFO "%s: Tests completed.\n",
2322 mmc_hostname(test->card->host));
2323}
2324
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002325static void mmc_test_free_result(struct mmc_card *card)
2326{
2327 struct mmc_test_general_result *gr, *grs;
2328
2329 mutex_lock(&mmc_test_lock);
2330
2331 list_for_each_entry_safe(gr, grs, &mmc_test_result, link) {
2332 struct mmc_test_transfer_result *tr, *trs;
2333
2334 if (card && gr->card != card)
2335 continue;
2336
2337 list_for_each_entry_safe(tr, trs, &gr->tr_lst, link) {
2338 list_del(&tr->link);
2339 kfree(tr);
2340 }
2341
2342 list_del(&gr->link);
2343 kfree(gr);
2344 }
2345
2346 mutex_unlock(&mmc_test_lock);
2347}
2348
Andy Shevchenko130067e2010-09-10 10:10:50 +03002349static LIST_HEAD(mmc_test_file_test);
2350
2351static int mtf_test_show(struct seq_file *sf, void *data)
Pierre Ossman88ae6002007-08-12 14:23:50 +02002352{
Andy Shevchenko130067e2010-09-10 10:10:50 +03002353 struct mmc_card *card = (struct mmc_card *)sf->private;
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002354 struct mmc_test_general_result *gr;
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002355
Pierre Ossman88ae6002007-08-12 14:23:50 +02002356 mutex_lock(&mmc_test_lock);
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002357
2358 list_for_each_entry(gr, &mmc_test_result, link) {
2359 struct mmc_test_transfer_result *tr;
2360
2361 if (gr->card != card)
2362 continue;
2363
Andy Shevchenko130067e2010-09-10 10:10:50 +03002364 seq_printf(sf, "Test %d: %d\n", gr->testcase + 1, gr->result);
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002365
2366 list_for_each_entry(tr, &gr->tr_lst, link) {
Adrian Hunterb6056d12011-02-08 13:41:02 +02002367 seq_printf(sf, "%u %d %lu.%09lu %u %u.%02u\n",
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002368 tr->count, tr->sectors,
2369 (unsigned long)tr->ts.tv_sec,
2370 (unsigned long)tr->ts.tv_nsec,
Adrian Hunterb6056d12011-02-08 13:41:02 +02002371 tr->rate, tr->iops / 100, tr->iops % 100);
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002372 }
2373 }
2374
Pierre Ossman88ae6002007-08-12 14:23:50 +02002375 mutex_unlock(&mmc_test_lock);
2376
Andy Shevchenko130067e2010-09-10 10:10:50 +03002377 return 0;
Pierre Ossman88ae6002007-08-12 14:23:50 +02002378}
2379
Andy Shevchenko130067e2010-09-10 10:10:50 +03002380static int mtf_test_open(struct inode *inode, struct file *file)
Pierre Ossman88ae6002007-08-12 14:23:50 +02002381{
Andy Shevchenko130067e2010-09-10 10:10:50 +03002382 return single_open(file, mtf_test_show, inode->i_private);
2383}
2384
2385static ssize_t mtf_test_write(struct file *file, const char __user *buf,
2386 size_t count, loff_t *pos)
2387{
2388 struct seq_file *sf = (struct seq_file *)file->private_data;
2389 struct mmc_card *card = (struct mmc_card *)sf->private;
Pierre Ossman88ae6002007-08-12 14:23:50 +02002390 struct mmc_test_card *test;
Andy Shevchenko130067e2010-09-10 10:10:50 +03002391 char lbuf[12];
Andy Shevchenko5c25aee2010-09-01 09:26:46 +03002392 long testcase;
Pierre Ossman88ae6002007-08-12 14:23:50 +02002393
Andy Shevchenko130067e2010-09-10 10:10:50 +03002394 if (count >= sizeof(lbuf))
2395 return -EINVAL;
2396
2397 if (copy_from_user(lbuf, buf, count))
2398 return -EFAULT;
2399 lbuf[count] = '\0';
2400
2401 if (strict_strtol(lbuf, 10, &testcase))
Andy Shevchenko5c25aee2010-09-01 09:26:46 +03002402 return -EINVAL;
Pierre Ossmanfd8c3262008-05-24 22:36:31 +02002403
Pierre Ossman88ae6002007-08-12 14:23:50 +02002404 test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
2405 if (!test)
2406 return -ENOMEM;
2407
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002408 /*
2409 * Remove all test cases associated with given card. Thus we have only
2410 * actual data of the last run.
2411 */
2412 mmc_test_free_result(card);
2413
Pierre Ossman88ae6002007-08-12 14:23:50 +02002414 test->card = card;
2415
2416 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
Pierre Ossman26610812008-07-04 18:17:13 +02002417#ifdef CONFIG_HIGHMEM
2418 test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
2419#endif
2420
2421#ifdef CONFIG_HIGHMEM
2422 if (test->buffer && test->highmem) {
2423#else
Pierre Ossman88ae6002007-08-12 14:23:50 +02002424 if (test->buffer) {
Pierre Ossman26610812008-07-04 18:17:13 +02002425#endif
Pierre Ossman88ae6002007-08-12 14:23:50 +02002426 mutex_lock(&mmc_test_lock);
Pierre Ossmanfd8c3262008-05-24 22:36:31 +02002427 mmc_test_run(test, testcase);
Pierre Ossman88ae6002007-08-12 14:23:50 +02002428 mutex_unlock(&mmc_test_lock);
2429 }
2430
Pierre Ossman26610812008-07-04 18:17:13 +02002431#ifdef CONFIG_HIGHMEM
2432 __free_pages(test->highmem, BUFFER_ORDER);
2433#endif
Pierre Ossman88ae6002007-08-12 14:23:50 +02002434 kfree(test->buffer);
2435 kfree(test);
2436
2437 return count;
2438}
2439
Andy Shevchenko130067e2010-09-10 10:10:50 +03002440static const struct file_operations mmc_test_fops_test = {
2441 .open = mtf_test_open,
2442 .read = seq_read,
2443 .write = mtf_test_write,
2444 .llseek = seq_lseek,
2445 .release = single_release,
2446};
2447
Per Forlin54f3caf2011-07-01 18:55:25 +02002448static int mtf_testlist_show(struct seq_file *sf, void *data)
2449{
2450 int i;
2451
2452 mutex_lock(&mmc_test_lock);
2453
2454 for (i = 0; i < ARRAY_SIZE(mmc_test_cases); i++)
2455 seq_printf(sf, "%d:\t%s\n", i+1, mmc_test_cases[i].name);
2456
2457 mutex_unlock(&mmc_test_lock);
2458
2459 return 0;
2460}
2461
2462static int mtf_testlist_open(struct inode *inode, struct file *file)
2463{
2464 return single_open(file, mtf_testlist_show, inode->i_private);
2465}
2466
2467static const struct file_operations mmc_test_fops_testlist = {
2468 .open = mtf_testlist_open,
2469 .read = seq_read,
2470 .llseek = seq_lseek,
2471 .release = single_release,
2472};
2473
Andy Shevchenko130067e2010-09-10 10:10:50 +03002474static void mmc_test_free_file_test(struct mmc_card *card)
2475{
2476 struct mmc_test_dbgfs_file *df, *dfs;
2477
2478 mutex_lock(&mmc_test_lock);
2479
2480 list_for_each_entry_safe(df, dfs, &mmc_test_file_test, link) {
2481 if (card && df->card != card)
2482 continue;
2483 debugfs_remove(df->file);
2484 list_del(&df->link);
2485 kfree(df);
2486 }
2487
2488 mutex_unlock(&mmc_test_lock);
2489}
2490
2491static int mmc_test_register_file_test(struct mmc_card *card)
2492{
2493 struct dentry *file = NULL;
2494 struct mmc_test_dbgfs_file *df;
2495 int ret = 0;
2496
2497 mutex_lock(&mmc_test_lock);
2498
2499 if (card->debugfs_root)
2500 file = debugfs_create_file("test", S_IWUSR | S_IRUGO,
2501 card->debugfs_root, card, &mmc_test_fops_test);
2502
2503 if (IS_ERR_OR_NULL(file)) {
2504 dev_err(&card->dev,
Per Forlin54f3caf2011-07-01 18:55:25 +02002505 "Can't create test. Perhaps debugfs is disabled.\n");
2506 ret = -ENODEV;
2507 goto err;
2508 }
2509
2510 if (card->debugfs_root)
2511 file = debugfs_create_file("testlist", S_IRUGO,
2512 card->debugfs_root, card, &mmc_test_fops_testlist);
2513
2514 if (IS_ERR_OR_NULL(file)) {
2515 dev_err(&card->dev,
2516 "Can't create testlist. Perhaps debugfs is disabled.\n");
Andy Shevchenko130067e2010-09-10 10:10:50 +03002517 ret = -ENODEV;
2518 goto err;
2519 }
2520
2521 df = kmalloc(sizeof(struct mmc_test_dbgfs_file), GFP_KERNEL);
2522 if (!df) {
2523 debugfs_remove(file);
2524 dev_err(&card->dev,
2525 "Can't allocate memory for internal usage.\n");
2526 ret = -ENOMEM;
2527 goto err;
2528 }
2529
2530 df->card = card;
2531 df->file = file;
2532
2533 list_add(&df->link, &mmc_test_file_test);
2534
2535err:
2536 mutex_unlock(&mmc_test_lock);
2537
2538 return ret;
2539}
Pierre Ossman88ae6002007-08-12 14:23:50 +02002540
2541static int mmc_test_probe(struct mmc_card *card)
2542{
2543 int ret;
2544
Andy Shevchenko63be54c2010-09-01 09:26:45 +03002545 if (!mmc_card_mmc(card) && !mmc_card_sd(card))
Pierre Ossman0121a982008-06-28 17:51:27 +02002546 return -ENODEV;
2547
Andy Shevchenko130067e2010-09-10 10:10:50 +03002548 ret = mmc_test_register_file_test(card);
Pierre Ossman88ae6002007-08-12 14:23:50 +02002549 if (ret)
2550 return ret;
2551
Pierre Ossman60c9c7b2008-07-22 14:38:35 +02002552 dev_info(&card->dev, "Card claimed for testing.\n");
2553
Pierre Ossman88ae6002007-08-12 14:23:50 +02002554 return 0;
2555}
2556
2557static void mmc_test_remove(struct mmc_card *card)
2558{
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002559 mmc_test_free_result(card);
Andy Shevchenko130067e2010-09-10 10:10:50 +03002560 mmc_test_free_file_test(card);
Pierre Ossman88ae6002007-08-12 14:23:50 +02002561}
2562
2563static struct mmc_driver mmc_driver = {
2564 .drv = {
2565 .name = "mmc_test",
2566 },
2567 .probe = mmc_test_probe,
2568 .remove = mmc_test_remove,
2569};
2570
2571static int __init mmc_test_init(void)
2572{
2573 return mmc_register_driver(&mmc_driver);
2574}
2575
2576static void __exit mmc_test_exit(void)
2577{
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002578 /* Clear stalled data if card is still plugged */
2579 mmc_test_free_result(NULL);
Andy Shevchenko130067e2010-09-10 10:10:50 +03002580 mmc_test_free_file_test(NULL);
Andy Shevchenko3183aa12010-09-01 09:26:47 +03002581
Pierre Ossman88ae6002007-08-12 14:23:50 +02002582 mmc_unregister_driver(&mmc_driver);
2583}
2584
2585module_init(mmc_test_init);
2586module_exit(mmc_test_exit);
2587
2588MODULE_LICENSE("GPL");
2589MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
2590MODULE_AUTHOR("Pierre Ossman");