mmc_test: test oversized sg lists
[linux-2.6/mini2440.git] / drivers / mmc / card / mmc_test.c
blob25296011df5979f3bd321b59581345da216adaae
1 /*
2 * linux/drivers/mmc/card/mmc_test.c
4 * Copyright 2007-2008 Pierre Ossman
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.
12 #include <linux/mmc/core.h>
13 #include <linux/mmc/card.h>
14 #include <linux/mmc/host.h>
15 #include <linux/mmc/mmc.h>
17 #include <linux/scatterlist.h>
19 #define RESULT_OK 0
20 #define RESULT_FAIL 1
21 #define RESULT_UNSUP_HOST 2
22 #define RESULT_UNSUP_CARD 3
24 #define BUFFER_ORDER 2
25 #define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
27 struct mmc_test_card {
28 struct mmc_card *card;
30 u8 scratch[BUFFER_SIZE];
31 u8 *buffer;
32 #ifdef CONFIG_HIGHMEM
33 struct page *highmem;
34 #endif
37 /*******************************************************************/
38 /* General helper functions */
39 /*******************************************************************/
42 * Configure correct block size in card
44 static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
46 struct mmc_command cmd;
47 int ret;
49 cmd.opcode = MMC_SET_BLOCKLEN;
50 cmd.arg = size;
51 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
52 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
53 if (ret)
54 return ret;
56 return 0;
60 * Fill in the mmc_request structure given a set of transfer parameters.
62 static void mmc_test_prepare_mrq(struct mmc_test_card *test,
63 struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
64 unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
66 BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
68 if (blocks > 1) {
69 mrq->cmd->opcode = write ?
70 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
71 } else {
72 mrq->cmd->opcode = write ?
73 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
76 mrq->cmd->arg = dev_addr;
77 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
79 if (blocks == 1)
80 mrq->stop = NULL;
81 else {
82 mrq->stop->opcode = MMC_STOP_TRANSMISSION;
83 mrq->stop->arg = 0;
84 mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
87 mrq->data->blksz = blksz;
88 mrq->data->blocks = blocks;
89 mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
90 mrq->data->sg = sg;
91 mrq->data->sg_len = sg_len;
93 mmc_set_data_timeout(mrq->data, test->card);
97 * Wait for the card to finish the busy state
99 static int mmc_test_wait_busy(struct mmc_test_card *test)
101 int ret, busy;
102 struct mmc_command cmd;
104 busy = 0;
105 do {
106 memset(&cmd, 0, sizeof(struct mmc_command));
108 cmd.opcode = MMC_SEND_STATUS;
109 cmd.arg = test->card->rca << 16;
110 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
112 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
113 if (ret)
114 break;
116 if (!busy && !(cmd.resp[0] & R1_READY_FOR_DATA)) {
117 busy = 1;
118 printk(KERN_INFO "%s: Warning: Host did not "
119 "wait for busy state to end.\n",
120 mmc_hostname(test->card->host));
122 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
124 return ret;
128 * Transfer a single sector of kernel addressable data
130 static int mmc_test_buffer_transfer(struct mmc_test_card *test,
131 u8 *buffer, unsigned addr, unsigned blksz, int write)
133 int ret;
135 struct mmc_request mrq;
136 struct mmc_command cmd;
137 struct mmc_command stop;
138 struct mmc_data data;
140 struct scatterlist sg;
142 memset(&mrq, 0, sizeof(struct mmc_request));
143 memset(&cmd, 0, sizeof(struct mmc_command));
144 memset(&data, 0, sizeof(struct mmc_data));
145 memset(&stop, 0, sizeof(struct mmc_command));
147 mrq.cmd = &cmd;
148 mrq.data = &data;
149 mrq.stop = &stop;
151 sg_init_one(&sg, buffer, blksz);
153 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
155 mmc_wait_for_req(test->card->host, &mrq);
157 if (cmd.error)
158 return cmd.error;
159 if (data.error)
160 return data.error;
162 ret = mmc_test_wait_busy(test);
163 if (ret)
164 return ret;
166 return 0;
169 /*******************************************************************/
170 /* Test preparation and cleanup */
171 /*******************************************************************/
174 * Fill the first couple of sectors of the card with known data
175 * so that bad reads/writes can be detected
177 static int __mmc_test_prepare(struct mmc_test_card *test, int write)
179 int ret, i;
181 ret = mmc_test_set_blksize(test, 512);
182 if (ret)
183 return ret;
185 if (write)
186 memset(test->buffer, 0xDF, 512);
187 else {
188 for (i = 0;i < 512;i++)
189 test->buffer[i] = i;
192 for (i = 0;i < BUFFER_SIZE / 512;i++) {
193 ret = mmc_test_buffer_transfer(test, test->buffer, i * 512, 512, 1);
194 if (ret)
195 return ret;
198 return 0;
201 static int mmc_test_prepare_write(struct mmc_test_card *test)
203 return __mmc_test_prepare(test, 1);
206 static int mmc_test_prepare_read(struct mmc_test_card *test)
208 return __mmc_test_prepare(test, 0);
211 static int mmc_test_cleanup(struct mmc_test_card *test)
213 int ret, i;
215 ret = mmc_test_set_blksize(test, 512);
216 if (ret)
217 return ret;
219 memset(test->buffer, 0, 512);
221 for (i = 0;i < BUFFER_SIZE / 512;i++) {
222 ret = mmc_test_buffer_transfer(test, test->buffer, i * 512, 512, 1);
223 if (ret)
224 return ret;
227 return 0;
230 /*******************************************************************/
231 /* Test execution helpers */
232 /*******************************************************************/
235 * Modifies the mmc_request to perform the "short transfer" tests
237 static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
238 struct mmc_request *mrq, int write)
240 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
242 if (mrq->data->blocks > 1) {
243 mrq->cmd->opcode = write ?
244 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
245 mrq->stop = NULL;
246 } else {
247 mrq->cmd->opcode = MMC_SEND_STATUS;
248 mrq->cmd->arg = test->card->rca << 16;
253 * Checks that a normal transfer didn't have any errors
255 static int mmc_test_check_result(struct mmc_test_card *test,
256 struct mmc_request *mrq)
258 int ret;
260 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
262 ret = 0;
264 if (!ret && mrq->cmd->error)
265 ret = mrq->cmd->error;
266 if (!ret && mrq->data->error)
267 ret = mrq->data->error;
268 if (!ret && mrq->stop && mrq->stop->error)
269 ret = mrq->stop->error;
270 if (!ret && mrq->data->bytes_xfered !=
271 mrq->data->blocks * mrq->data->blksz)
272 ret = RESULT_FAIL;
274 if (ret == -EINVAL)
275 ret = RESULT_UNSUP_HOST;
277 return ret;
281 * Checks that a "short transfer" behaved as expected
283 static int mmc_test_check_broken_result(struct mmc_test_card *test,
284 struct mmc_request *mrq)
286 int ret;
288 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
290 ret = 0;
292 if (!ret && mrq->cmd->error)
293 ret = mrq->cmd->error;
294 if (!ret && mrq->data->error == 0)
295 ret = RESULT_FAIL;
296 if (!ret && mrq->data->error != -ETIMEDOUT)
297 ret = mrq->data->error;
298 if (!ret && mrq->stop && mrq->stop->error)
299 ret = mrq->stop->error;
300 if (mrq->data->blocks > 1) {
301 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
302 ret = RESULT_FAIL;
303 } else {
304 if (!ret && mrq->data->bytes_xfered > 0)
305 ret = RESULT_FAIL;
308 if (ret == -EINVAL)
309 ret = RESULT_UNSUP_HOST;
311 return ret;
315 * Tests a basic transfer with certain parameters
317 static int mmc_test_simple_transfer(struct mmc_test_card *test,
318 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
319 unsigned blocks, unsigned blksz, int write)
321 struct mmc_request mrq;
322 struct mmc_command cmd;
323 struct mmc_command stop;
324 struct mmc_data data;
326 memset(&mrq, 0, sizeof(struct mmc_request));
327 memset(&cmd, 0, sizeof(struct mmc_command));
328 memset(&data, 0, sizeof(struct mmc_data));
329 memset(&stop, 0, sizeof(struct mmc_command));
331 mrq.cmd = &cmd;
332 mrq.data = &data;
333 mrq.stop = &stop;
335 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
336 blocks, blksz, write);
338 mmc_wait_for_req(test->card->host, &mrq);
340 mmc_test_wait_busy(test);
342 return mmc_test_check_result(test, &mrq);
346 * Tests a transfer where the card will fail completely or partly
348 static int mmc_test_broken_transfer(struct mmc_test_card *test,
349 unsigned blocks, unsigned blksz, int write)
351 struct mmc_request mrq;
352 struct mmc_command cmd;
353 struct mmc_command stop;
354 struct mmc_data data;
356 struct scatterlist sg;
358 memset(&mrq, 0, sizeof(struct mmc_request));
359 memset(&cmd, 0, sizeof(struct mmc_command));
360 memset(&data, 0, sizeof(struct mmc_data));
361 memset(&stop, 0, sizeof(struct mmc_command));
363 mrq.cmd = &cmd;
364 mrq.data = &data;
365 mrq.stop = &stop;
367 sg_init_one(&sg, test->buffer, blocks * blksz);
369 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
370 mmc_test_prepare_broken_mrq(test, &mrq, write);
372 mmc_wait_for_req(test->card->host, &mrq);
374 mmc_test_wait_busy(test);
376 return mmc_test_check_broken_result(test, &mrq);
380 * Does a complete transfer test where data is also validated
382 * Note: mmc_test_prepare() must have been done before this call
384 static int mmc_test_transfer(struct mmc_test_card *test,
385 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
386 unsigned blocks, unsigned blksz, int write)
388 int ret, i;
389 unsigned long flags;
391 BUG_ON(blocks * blksz > BUFFER_SIZE);
393 if (write) {
394 for (i = 0;i < blocks * blksz;i++)
395 test->scratch[i] = i;
396 } else {
397 memset(test->scratch, 0, blocks * blksz);
399 local_irq_save(flags);
400 sg_copy_from_buffer(sg, sg_len, test->scratch, blocks * blksz);
401 local_irq_restore(flags);
403 ret = mmc_test_set_blksize(test, blksz);
404 if (ret)
405 return ret;
407 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
408 blocks, blksz, write);
409 if (ret)
410 return ret;
412 if (write) {
413 int sectors;
415 ret = mmc_test_set_blksize(test, 512);
416 if (ret)
417 return ret;
419 sectors = (blocks * blksz + 511) / 512;
420 if ((sectors * 512) == (blocks * blksz))
421 sectors++;
423 if ((sectors * 512) > BUFFER_SIZE)
424 return -EINVAL;
426 memset(test->buffer, 0, sectors * 512);
428 for (i = 0;i < sectors;i++) {
429 ret = mmc_test_buffer_transfer(test,
430 test->buffer + i * 512,
431 dev_addr + i * 512, 512, 0);
432 if (ret)
433 return ret;
436 for (i = 0;i < blocks * blksz;i++) {
437 if (test->buffer[i] != (u8)i)
438 return RESULT_FAIL;
441 for (;i < sectors * 512;i++) {
442 if (test->buffer[i] != 0xDF)
443 return RESULT_FAIL;
445 } else {
446 local_irq_save(flags);
447 sg_copy_to_buffer(sg, sg_len, test->scratch, blocks * blksz);
448 local_irq_restore(flags);
449 for (i = 0;i < blocks * blksz;i++) {
450 if (test->scratch[i] != (u8)i)
451 return RESULT_FAIL;
455 return 0;
458 /*******************************************************************/
459 /* Tests */
460 /*******************************************************************/
462 struct mmc_test_case {
463 const char *name;
465 int (*prepare)(struct mmc_test_card *);
466 int (*run)(struct mmc_test_card *);
467 int (*cleanup)(struct mmc_test_card *);
470 static int mmc_test_basic_write(struct mmc_test_card *test)
472 int ret;
473 struct scatterlist sg;
475 ret = mmc_test_set_blksize(test, 512);
476 if (ret)
477 return ret;
479 sg_init_one(&sg, test->buffer, 512);
481 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
482 if (ret)
483 return ret;
485 return 0;
488 static int mmc_test_basic_read(struct mmc_test_card *test)
490 int ret;
491 struct scatterlist sg;
493 ret = mmc_test_set_blksize(test, 512);
494 if (ret)
495 return ret;
497 sg_init_one(&sg, test->buffer, 512);
499 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
500 if (ret)
501 return ret;
503 return 0;
506 static int mmc_test_verify_write(struct mmc_test_card *test)
508 int ret;
509 struct scatterlist sg;
511 sg_init_one(&sg, test->buffer, 512);
513 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
514 if (ret)
515 return ret;
517 return 0;
520 static int mmc_test_verify_read(struct mmc_test_card *test)
522 int ret;
523 struct scatterlist sg;
525 sg_init_one(&sg, test->buffer, 512);
527 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
528 if (ret)
529 return ret;
531 return 0;
534 static int mmc_test_multi_write(struct mmc_test_card *test)
536 int ret;
537 unsigned int size;
538 struct scatterlist sg;
540 if (test->card->host->max_blk_count == 1)
541 return RESULT_UNSUP_HOST;
543 size = PAGE_SIZE * 2;
544 size = min(size, test->card->host->max_req_size);
545 size = min(size, test->card->host->max_seg_size);
546 size = min(size, test->card->host->max_blk_count * 512);
548 if (size < 1024)
549 return RESULT_UNSUP_HOST;
551 sg_init_one(&sg, test->buffer, size);
553 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
554 if (ret)
555 return ret;
557 return 0;
560 static int mmc_test_multi_read(struct mmc_test_card *test)
562 int ret;
563 unsigned int size;
564 struct scatterlist sg;
566 if (test->card->host->max_blk_count == 1)
567 return RESULT_UNSUP_HOST;
569 size = PAGE_SIZE * 2;
570 size = min(size, test->card->host->max_req_size);
571 size = min(size, test->card->host->max_seg_size);
572 size = min(size, test->card->host->max_blk_count * 512);
574 if (size < 1024)
575 return RESULT_UNSUP_HOST;
577 sg_init_one(&sg, test->buffer, size);
579 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
580 if (ret)
581 return ret;
583 return 0;
586 static int mmc_test_pow2_write(struct mmc_test_card *test)
588 int ret, i;
589 struct scatterlist sg;
591 if (!test->card->csd.write_partial)
592 return RESULT_UNSUP_CARD;
594 for (i = 1; i < 512;i <<= 1) {
595 sg_init_one(&sg, test->buffer, i);
596 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
597 if (ret)
598 return ret;
601 return 0;
604 static int mmc_test_pow2_read(struct mmc_test_card *test)
606 int ret, i;
607 struct scatterlist sg;
609 if (!test->card->csd.read_partial)
610 return RESULT_UNSUP_CARD;
612 for (i = 1; i < 512;i <<= 1) {
613 sg_init_one(&sg, test->buffer, i);
614 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
615 if (ret)
616 return ret;
619 return 0;
622 static int mmc_test_weird_write(struct mmc_test_card *test)
624 int ret, i;
625 struct scatterlist sg;
627 if (!test->card->csd.write_partial)
628 return RESULT_UNSUP_CARD;
630 for (i = 3; i < 512;i += 7) {
631 sg_init_one(&sg, test->buffer, i);
632 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
633 if (ret)
634 return ret;
637 return 0;
640 static int mmc_test_weird_read(struct mmc_test_card *test)
642 int ret, i;
643 struct scatterlist sg;
645 if (!test->card->csd.read_partial)
646 return RESULT_UNSUP_CARD;
648 for (i = 3; i < 512;i += 7) {
649 sg_init_one(&sg, test->buffer, i);
650 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
651 if (ret)
652 return ret;
655 return 0;
658 static int mmc_test_align_write(struct mmc_test_card *test)
660 int ret, i;
661 struct scatterlist sg;
663 for (i = 1;i < 4;i++) {
664 sg_init_one(&sg, test->buffer + i, 512);
665 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
666 if (ret)
667 return ret;
670 return 0;
673 static int mmc_test_align_read(struct mmc_test_card *test)
675 int ret, i;
676 struct scatterlist sg;
678 for (i = 1;i < 4;i++) {
679 sg_init_one(&sg, test->buffer + i, 512);
680 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
681 if (ret)
682 return ret;
685 return 0;
688 static int mmc_test_align_multi_write(struct mmc_test_card *test)
690 int ret, i;
691 unsigned int size;
692 struct scatterlist sg;
694 if (test->card->host->max_blk_count == 1)
695 return RESULT_UNSUP_HOST;
697 size = PAGE_SIZE * 2;
698 size = min(size, test->card->host->max_req_size);
699 size = min(size, test->card->host->max_seg_size);
700 size = min(size, test->card->host->max_blk_count * 512);
702 if (size < 1024)
703 return RESULT_UNSUP_HOST;
705 for (i = 1;i < 4;i++) {
706 sg_init_one(&sg, test->buffer + i, size);
707 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
708 if (ret)
709 return ret;
712 return 0;
715 static int mmc_test_align_multi_read(struct mmc_test_card *test)
717 int ret, i;
718 unsigned int size;
719 struct scatterlist sg;
721 if (test->card->host->max_blk_count == 1)
722 return RESULT_UNSUP_HOST;
724 size = PAGE_SIZE * 2;
725 size = min(size, test->card->host->max_req_size);
726 size = min(size, test->card->host->max_seg_size);
727 size = min(size, test->card->host->max_blk_count * 512);
729 if (size < 1024)
730 return RESULT_UNSUP_HOST;
732 for (i = 1;i < 4;i++) {
733 sg_init_one(&sg, test->buffer + i, size);
734 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
735 if (ret)
736 return ret;
739 return 0;
742 static int mmc_test_xfersize_write(struct mmc_test_card *test)
744 int ret;
746 ret = mmc_test_set_blksize(test, 512);
747 if (ret)
748 return ret;
750 ret = mmc_test_broken_transfer(test, 1, 512, 1);
751 if (ret)
752 return ret;
754 return 0;
757 static int mmc_test_xfersize_read(struct mmc_test_card *test)
759 int ret;
761 ret = mmc_test_set_blksize(test, 512);
762 if (ret)
763 return ret;
765 ret = mmc_test_broken_transfer(test, 1, 512, 0);
766 if (ret)
767 return ret;
769 return 0;
772 static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
774 int ret;
776 if (test->card->host->max_blk_count == 1)
777 return RESULT_UNSUP_HOST;
779 ret = mmc_test_set_blksize(test, 512);
780 if (ret)
781 return ret;
783 ret = mmc_test_broken_transfer(test, 2, 512, 1);
784 if (ret)
785 return ret;
787 return 0;
790 static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
792 int ret;
794 if (test->card->host->max_blk_count == 1)
795 return RESULT_UNSUP_HOST;
797 ret = mmc_test_set_blksize(test, 512);
798 if (ret)
799 return ret;
801 ret = mmc_test_broken_transfer(test, 2, 512, 0);
802 if (ret)
803 return ret;
805 return 0;
808 static int mmc_test_bigsg_write(struct mmc_test_card *test)
810 int ret;
811 unsigned int size;
812 struct scatterlist sg;
814 if (test->card->host->max_blk_count == 1)
815 return RESULT_UNSUP_HOST;
817 size = PAGE_SIZE * 2;
818 size = min(size, test->card->host->max_req_size);
819 size = min(size, test->card->host->max_seg_size);
820 size = min(size, test->card->host->max_blk_count * 512);
822 memset(test->buffer, 0, BUFFER_SIZE);
824 if (size < 1024)
825 return RESULT_UNSUP_HOST;
827 sg_init_table(&sg, 1);
828 sg_init_one(&sg, test->buffer, BUFFER_SIZE);
830 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
831 if (ret)
832 return ret;
834 return 0;
837 static int mmc_test_bigsg_read(struct mmc_test_card *test)
839 int ret, i;
840 unsigned int size;
841 struct scatterlist sg;
843 if (test->card->host->max_blk_count == 1)
844 return RESULT_UNSUP_HOST;
846 size = PAGE_SIZE * 2;
847 size = min(size, test->card->host->max_req_size);
848 size = min(size, test->card->host->max_seg_size);
849 size = min(size, test->card->host->max_blk_count * 512);
851 if (size < 1024)
852 return RESULT_UNSUP_HOST;
854 memset(test->buffer, 0xCD, BUFFER_SIZE);
856 sg_init_table(&sg, 1);
857 sg_init_one(&sg, test->buffer, BUFFER_SIZE);
858 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
859 if (ret)
860 return ret;
862 /* mmc_test_transfer() doesn't check for read overflows */
863 for (i = size;i < BUFFER_SIZE;i++) {
864 if (test->buffer[i] != 0xCD)
865 return RESULT_FAIL;
868 return 0;
871 #ifdef CONFIG_HIGHMEM
873 static int mmc_test_write_high(struct mmc_test_card *test)
875 int ret;
876 struct scatterlist sg;
878 sg_init_table(&sg, 1);
879 sg_set_page(&sg, test->highmem, 512, 0);
881 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
882 if (ret)
883 return ret;
885 return 0;
888 static int mmc_test_read_high(struct mmc_test_card *test)
890 int ret;
891 struct scatterlist sg;
893 sg_init_table(&sg, 1);
894 sg_set_page(&sg, test->highmem, 512, 0);
896 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
897 if (ret)
898 return ret;
900 return 0;
903 static int mmc_test_multi_write_high(struct mmc_test_card *test)
905 int ret;
906 unsigned int size;
907 struct scatterlist sg;
909 if (test->card->host->max_blk_count == 1)
910 return RESULT_UNSUP_HOST;
912 size = PAGE_SIZE * 2;
913 size = min(size, test->card->host->max_req_size);
914 size = min(size, test->card->host->max_seg_size);
915 size = min(size, test->card->host->max_blk_count * 512);
917 if (size < 1024)
918 return RESULT_UNSUP_HOST;
920 sg_init_table(&sg, 1);
921 sg_set_page(&sg, test->highmem, size, 0);
923 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
924 if (ret)
925 return ret;
927 return 0;
930 static int mmc_test_multi_read_high(struct mmc_test_card *test)
932 int ret;
933 unsigned int size;
934 struct scatterlist sg;
936 if (test->card->host->max_blk_count == 1)
937 return RESULT_UNSUP_HOST;
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);
944 if (size < 1024)
945 return RESULT_UNSUP_HOST;
947 sg_init_table(&sg, 1);
948 sg_set_page(&sg, test->highmem, size, 0);
950 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
951 if (ret)
952 return ret;
954 return 0;
957 #endif /* CONFIG_HIGHMEM */
959 static const struct mmc_test_case mmc_test_cases[] = {
961 .name = "Basic write (no data verification)",
962 .run = mmc_test_basic_write,
966 .name = "Basic read (no data verification)",
967 .run = mmc_test_basic_read,
971 .name = "Basic write (with data verification)",
972 .prepare = mmc_test_prepare_write,
973 .run = mmc_test_verify_write,
974 .cleanup = mmc_test_cleanup,
978 .name = "Basic read (with data verification)",
979 .prepare = mmc_test_prepare_read,
980 .run = mmc_test_verify_read,
981 .cleanup = mmc_test_cleanup,
985 .name = "Multi-block write",
986 .prepare = mmc_test_prepare_write,
987 .run = mmc_test_multi_write,
988 .cleanup = mmc_test_cleanup,
992 .name = "Multi-block read",
993 .prepare = mmc_test_prepare_read,
994 .run = mmc_test_multi_read,
995 .cleanup = mmc_test_cleanup,
999 .name = "Power of two block writes",
1000 .prepare = mmc_test_prepare_write,
1001 .run = mmc_test_pow2_write,
1002 .cleanup = mmc_test_cleanup,
1006 .name = "Power of two block reads",
1007 .prepare = mmc_test_prepare_read,
1008 .run = mmc_test_pow2_read,
1009 .cleanup = mmc_test_cleanup,
1013 .name = "Weird sized block writes",
1014 .prepare = mmc_test_prepare_write,
1015 .run = mmc_test_weird_write,
1016 .cleanup = mmc_test_cleanup,
1020 .name = "Weird sized block reads",
1021 .prepare = mmc_test_prepare_read,
1022 .run = mmc_test_weird_read,
1023 .cleanup = mmc_test_cleanup,
1027 .name = "Badly aligned write",
1028 .prepare = mmc_test_prepare_write,
1029 .run = mmc_test_align_write,
1030 .cleanup = mmc_test_cleanup,
1034 .name = "Badly aligned read",
1035 .prepare = mmc_test_prepare_read,
1036 .run = mmc_test_align_read,
1037 .cleanup = mmc_test_cleanup,
1041 .name = "Badly aligned multi-block write",
1042 .prepare = mmc_test_prepare_write,
1043 .run = mmc_test_align_multi_write,
1044 .cleanup = mmc_test_cleanup,
1048 .name = "Badly aligned multi-block read",
1049 .prepare = mmc_test_prepare_read,
1050 .run = mmc_test_align_multi_read,
1051 .cleanup = mmc_test_cleanup,
1055 .name = "Correct xfer_size at write (start failure)",
1056 .run = mmc_test_xfersize_write,
1060 .name = "Correct xfer_size at read (start failure)",
1061 .run = mmc_test_xfersize_read,
1065 .name = "Correct xfer_size at write (midway failure)",
1066 .run = mmc_test_multi_xfersize_write,
1070 .name = "Correct xfer_size at read (midway failure)",
1071 .run = mmc_test_multi_xfersize_read,
1075 .name = "Over-sized SG list write",
1076 .prepare = mmc_test_prepare_write,
1077 .run = mmc_test_bigsg_write,
1078 .cleanup = mmc_test_cleanup,
1082 .name = "Over-sized SG list read",
1083 .prepare = mmc_test_prepare_read,
1084 .run = mmc_test_bigsg_read,
1085 .cleanup = mmc_test_cleanup,
1088 #ifdef CONFIG_HIGHMEM
1091 .name = "Highmem write",
1092 .prepare = mmc_test_prepare_write,
1093 .run = mmc_test_write_high,
1094 .cleanup = mmc_test_cleanup,
1098 .name = "Highmem read",
1099 .prepare = mmc_test_prepare_read,
1100 .run = mmc_test_read_high,
1101 .cleanup = mmc_test_cleanup,
1105 .name = "Multi-block highmem write",
1106 .prepare = mmc_test_prepare_write,
1107 .run = mmc_test_multi_write_high,
1108 .cleanup = mmc_test_cleanup,
1112 .name = "Multi-block highmem read",
1113 .prepare = mmc_test_prepare_read,
1114 .run = mmc_test_multi_read_high,
1115 .cleanup = mmc_test_cleanup,
1118 #endif /* CONFIG_HIGHMEM */
1122 static struct mutex mmc_test_lock;
1124 static void mmc_test_run(struct mmc_test_card *test, int testcase)
1126 int i, ret;
1128 printk(KERN_INFO "%s: Starting tests of card %s...\n",
1129 mmc_hostname(test->card->host), mmc_card_id(test->card));
1131 mmc_claim_host(test->card->host);
1133 for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
1134 if (testcase && ((i + 1) != testcase))
1135 continue;
1137 printk(KERN_INFO "%s: Test case %d. %s...\n",
1138 mmc_hostname(test->card->host), i + 1,
1139 mmc_test_cases[i].name);
1141 if (mmc_test_cases[i].prepare) {
1142 ret = mmc_test_cases[i].prepare(test);
1143 if (ret) {
1144 printk(KERN_INFO "%s: Result: Prepare "
1145 "stage failed! (%d)\n",
1146 mmc_hostname(test->card->host),
1147 ret);
1148 continue;
1152 ret = mmc_test_cases[i].run(test);
1153 switch (ret) {
1154 case RESULT_OK:
1155 printk(KERN_INFO "%s: Result: OK\n",
1156 mmc_hostname(test->card->host));
1157 break;
1158 case RESULT_FAIL:
1159 printk(KERN_INFO "%s: Result: FAILED\n",
1160 mmc_hostname(test->card->host));
1161 break;
1162 case RESULT_UNSUP_HOST:
1163 printk(KERN_INFO "%s: Result: UNSUPPORTED "
1164 "(by host)\n",
1165 mmc_hostname(test->card->host));
1166 break;
1167 case RESULT_UNSUP_CARD:
1168 printk(KERN_INFO "%s: Result: UNSUPPORTED "
1169 "(by card)\n",
1170 mmc_hostname(test->card->host));
1171 break;
1172 default:
1173 printk(KERN_INFO "%s: Result: ERROR (%d)\n",
1174 mmc_hostname(test->card->host), ret);
1177 if (mmc_test_cases[i].cleanup) {
1178 ret = mmc_test_cases[i].cleanup(test);
1179 if (ret) {
1180 printk(KERN_INFO "%s: Warning: Cleanup "
1181 "stage failed! (%d)\n",
1182 mmc_hostname(test->card->host),
1183 ret);
1188 mmc_release_host(test->card->host);
1190 printk(KERN_INFO "%s: Tests completed.\n",
1191 mmc_hostname(test->card->host));
1194 static ssize_t mmc_test_show(struct device *dev,
1195 struct device_attribute *attr, char *buf)
1197 mutex_lock(&mmc_test_lock);
1198 mutex_unlock(&mmc_test_lock);
1200 return 0;
1203 static ssize_t mmc_test_store(struct device *dev,
1204 struct device_attribute *attr, const char *buf, size_t count)
1206 struct mmc_card *card;
1207 struct mmc_test_card *test;
1208 int testcase;
1210 card = container_of(dev, struct mmc_card, dev);
1212 testcase = simple_strtol(buf, NULL, 10);
1214 test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
1215 if (!test)
1216 return -ENOMEM;
1218 test->card = card;
1220 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
1221 #ifdef CONFIG_HIGHMEM
1222 test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
1223 #endif
1225 #ifdef CONFIG_HIGHMEM
1226 if (test->buffer && test->highmem) {
1227 #else
1228 if (test->buffer) {
1229 #endif
1230 mutex_lock(&mmc_test_lock);
1231 mmc_test_run(test, testcase);
1232 mutex_unlock(&mmc_test_lock);
1235 #ifdef CONFIG_HIGHMEM
1236 __free_pages(test->highmem, BUFFER_ORDER);
1237 #endif
1238 kfree(test->buffer);
1239 kfree(test);
1241 return count;
1244 static DEVICE_ATTR(test, S_IWUSR | S_IRUGO, mmc_test_show, mmc_test_store);
1246 static int mmc_test_probe(struct mmc_card *card)
1248 int ret;
1250 if ((card->type != MMC_TYPE_MMC) && (card->type != MMC_TYPE_SD))
1251 return -ENODEV;
1253 mutex_init(&mmc_test_lock);
1255 ret = device_create_file(&card->dev, &dev_attr_test);
1256 if (ret)
1257 return ret;
1259 return 0;
1262 static void mmc_test_remove(struct mmc_card *card)
1264 device_remove_file(&card->dev, &dev_attr_test);
1267 static struct mmc_driver mmc_driver = {
1268 .drv = {
1269 .name = "mmc_test",
1271 .probe = mmc_test_probe,
1272 .remove = mmc_test_remove,
1275 static int __init mmc_test_init(void)
1277 return mmc_register_driver(&mmc_driver);
1280 static void __exit mmc_test_exit(void)
1282 mmc_unregister_driver(&mmc_driver);
1285 module_init(mmc_test_init);
1286 module_exit(mmc_test_exit);
1288 MODULE_LICENSE("GPL");
1289 MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
1290 MODULE_AUTHOR("Pierre Ossman");