]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/spi/spi-loopback-test.c
spi: loopback-test: add option to use vmalloc'ed buffers
[mirror_ubuntu-artful-kernel.git] / drivers / spi / spi-loopback-test.c
CommitLineData
84e0c4e5
MS
1/*
2 * linux/drivers/spi/spi-loopback-test.c
3 *
4 * (c) Martin Sperl <kernel@martin.sperl.org>
5 *
6 * Loopback test driver to test several typical spi_message conditions
7 * that a spi_master driver may encounter
8 * this can also get used for regression testing
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/delay.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/list_sort.h>
25#include <linux/module.h>
26#include <linux/of_device.h>
27#include <linux/printk.h>
28#include <linux/spi/spi.h>
29
30#include "spi-test.h"
31
32/* flag to only simulate transfers */
33int simulate_only;
34module_param(simulate_only, int, 0);
35MODULE_PARM_DESC(simulate_only, "if not 0 do not execute the spi message");
36
37/* dump spi messages */
38int dump_messages;
39module_param(dump_messages, int, 0);
8caad1da 40MODULE_PARM_DESC(dump_messages,
84e0c4e5
MS
41 "=1 dump the basic spi_message_structure, " \
42 "=2 dump the spi_message_structure including data, " \
43 "=3 dump the spi_message structure before and after execution");
44/* the device is jumpered for loopback - enabling some rx_buf tests */
45int loopback;
46module_param(loopback, int, 0);
47MODULE_PARM_DESC(loopback,
48 "if set enable loopback mode, where the rx_buf " \
49 "is checked to match tx_buf after the spi_message " \
50 "is executed");
51
52/* run only a specific test */
53int run_only_test = -1;
54module_param(run_only_test, int, 0);
55MODULE_PARM_DESC(run_only_test,
56 "only run the test with this number (0-based !)");
57
576333a1
FI
58/* use vmalloc'ed buffers */
59int use_vmalloc;
60module_param(use_vmalloc, int, 0644);
61MODULE_PARM_DESC(use_vmalloc,
62 "use vmalloc'ed buffers instead of kmalloc'ed");
63
64/* check rx ranges */
65int check_ranges = 1;
66module_param(check_ranges, int, 0644);
67MODULE_PARM_DESC(check_ranges,
68 "checks rx_buffer pattern are valid");
69
84e0c4e5
MS
70/* the actual tests to execute */
71static struct spi_test spi_tests[] = {
72 {
73 .description = "tx/rx-transfer - start of page",
74 .fill_option = FILL_COUNT_8,
75 .iterate_len = { ITERATE_MAX_LEN },
76 .iterate_tx_align = ITERATE_ALIGN,
77 .iterate_rx_align = ITERATE_ALIGN,
78 .transfers = {
79 {
80 .len = 1,
81 .tx_buf = TX(0),
82 .rx_buf = RX(0),
83 },
84 },
85 },
86 {
87 .description = "tx/rx-transfer - crossing PAGE_SIZE",
88 .fill_option = FILL_COUNT_8,
89 .iterate_len = { ITERATE_MAX_LEN },
90 .iterate_tx_align = ITERATE_ALIGN,
91 .iterate_rx_align = ITERATE_ALIGN,
92 .transfers = {
93 {
94 .len = 1,
95 .tx_buf = TX(PAGE_SIZE - 4),
96 .rx_buf = RX(PAGE_SIZE - 4),
97 },
98 },
99 },
100 {
101 .description = "tx-transfer - only",
102 .fill_option = FILL_COUNT_8,
103 .iterate_len = { ITERATE_MAX_LEN },
104 .iterate_tx_align = ITERATE_ALIGN,
105 .transfers = {
106 {
107 .len = 1,
108 .tx_buf = TX(0),
109 },
110 },
111 },
112 {
113 .description = "rx-transfer - only",
114 .fill_option = FILL_COUNT_8,
115 .iterate_len = { ITERATE_MAX_LEN },
116 .iterate_rx_align = ITERATE_ALIGN,
117 .transfers = {
118 {
119 .len = 1,
120 .rx_buf = RX(0),
121 },
122 },
123 },
124 {
125 .description = "two tx-transfers - alter both",
126 .fill_option = FILL_COUNT_8,
127 .iterate_len = { ITERATE_LEN },
128 .iterate_tx_align = ITERATE_ALIGN,
129 .iterate_transfer_mask = BIT(0) | BIT(1),
130 .transfers = {
131 {
132 .len = 1,
133 .tx_buf = TX(0),
134 },
135 {
136 .len = 1,
137 /* this is why we cant use ITERATE_MAX_LEN */
138 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
139 },
140 },
141 },
142 {
143 .description = "two tx-transfers - alter first",
144 .fill_option = FILL_COUNT_8,
145 .iterate_len = { ITERATE_MAX_LEN },
146 .iterate_tx_align = ITERATE_ALIGN,
147 .iterate_transfer_mask = BIT(1),
148 .transfers = {
149 {
150 .len = 1,
151 .tx_buf = TX(64),
152 },
153 {
154 .len = 1,
155 .tx_buf = TX(0),
156 },
157 },
158 },
159 {
160 .description = "two tx-transfers - alter second",
161 .fill_option = FILL_COUNT_8,
162 .iterate_len = { ITERATE_MAX_LEN },
163 .iterate_tx_align = ITERATE_ALIGN,
164 .iterate_transfer_mask = BIT(0),
165 .transfers = {
166 {
167 .len = 16,
168 .tx_buf = TX(0),
169 },
170 {
171 .len = 1,
172 .tx_buf = TX(64),
173 },
174 },
175 },
176 {
177 .description = "two transfers tx then rx - alter both",
178 .fill_option = FILL_COUNT_8,
179 .iterate_len = { ITERATE_MAX_LEN },
180 .iterate_tx_align = ITERATE_ALIGN,
181 .iterate_transfer_mask = BIT(0) | BIT(1),
182 .transfers = {
183 {
184 .len = 1,
185 .tx_buf = TX(0),
186 },
187 {
188 .len = 1,
189 .rx_buf = RX(0),
190 },
191 },
192 },
193 {
194 .description = "two transfers tx then rx - alter tx",
195 .fill_option = FILL_COUNT_8,
196 .iterate_len = { ITERATE_MAX_LEN },
197 .iterate_tx_align = ITERATE_ALIGN,
198 .iterate_transfer_mask = BIT(0),
199 .transfers = {
200 {
201 .len = 1,
202 .tx_buf = TX(0),
203 },
204 {
205 .len = 1,
206 .rx_buf = RX(0),
207 },
208 },
209 },
210 {
211 .description = "two transfers tx then rx - alter rx",
212 .fill_option = FILL_COUNT_8,
213 .iterate_len = { ITERATE_MAX_LEN },
214 .iterate_tx_align = ITERATE_ALIGN,
215 .iterate_transfer_mask = BIT(1),
216 .transfers = {
217 {
218 .len = 1,
219 .tx_buf = TX(0),
220 },
221 {
222 .len = 1,
223 .rx_buf = RX(0),
224 },
225 },
226 },
227 {
228 .description = "two tx+rx transfers - alter both",
229 .fill_option = FILL_COUNT_8,
230 .iterate_len = { ITERATE_LEN },
231 .iterate_tx_align = ITERATE_ALIGN,
232 .iterate_transfer_mask = BIT(0) | BIT(1),
233 .transfers = {
234 {
235 .len = 1,
236 .tx_buf = TX(0),
237 .rx_buf = RX(0),
238 },
239 {
240 .len = 1,
241 /* making sure we align without overwrite
242 * the reason we can not use ITERATE_MAX_LEN
243 */
244 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
245 .rx_buf = RX(SPI_TEST_MAX_SIZE_HALF),
246 },
247 },
248 },
249 {
250 .description = "two tx+rx transfers - alter first",
251 .fill_option = FILL_COUNT_8,
252 .iterate_len = { ITERATE_MAX_LEN },
253 .iterate_tx_align = ITERATE_ALIGN,
254 .iterate_transfer_mask = BIT(0),
255 .transfers = {
256 {
257 .len = 1,
258 /* making sure we align without overwrite */
259 .tx_buf = TX(1024),
260 .rx_buf = RX(1024),
261 },
262 {
263 .len = 1,
264 /* making sure we align without overwrite */
265 .tx_buf = TX(0),
266 .rx_buf = RX(0),
267 },
268 },
269 },
270 {
271 .description = "two tx+rx transfers - alter second",
272 .fill_option = FILL_COUNT_8,
273 .iterate_len = { ITERATE_MAX_LEN },
274 .iterate_tx_align = ITERATE_ALIGN,
fc8773e1 275 .iterate_transfer_mask = BIT(1),
84e0c4e5
MS
276 .transfers = {
277 {
278 .len = 1,
279 .tx_buf = TX(0),
280 .rx_buf = RX(0),
281 },
282 {
283 .len = 1,
284 /* making sure we align without overwrite */
285 .tx_buf = TX(1024),
286 .rx_buf = RX(1024),
287 },
288 },
289 },
290
291 { /* end of tests sequence */ }
292};
293
294static int spi_loopback_test_probe(struct spi_device *spi)
295{
296 int ret;
297
298 dev_info(&spi->dev, "Executing spi-loopback-tests\n");
299
300 ret = spi_test_run_tests(spi, spi_tests);
301
302 dev_info(&spi->dev, "Finished spi-loopback-tests with return: %i\n",
303 ret);
304
305 return ret;
306}
307
308/* non const match table to permit to change via a module parameter */
309static struct of_device_id spi_loopback_test_of_match[] = {
310 { .compatible = "linux,spi-loopback-test", },
311 { }
312};
313
314/* allow to override the compatible string via a module_parameter */
315module_param_string(compatible, spi_loopback_test_of_match[0].compatible,
316 sizeof(spi_loopback_test_of_match[0].compatible),
317 0000);
318
319MODULE_DEVICE_TABLE(of, spi_loopback_test_of_match);
320
321static struct spi_driver spi_loopback_test_driver = {
322 .driver = {
323 .name = "spi-loopback-test",
324 .owner = THIS_MODULE,
325 .of_match_table = spi_loopback_test_of_match,
326 },
327 .probe = spi_loopback_test_probe,
328};
329
330module_spi_driver(spi_loopback_test_driver);
331
332MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
333MODULE_DESCRIPTION("test spi_driver to check core functionality");
334MODULE_LICENSE("GPL");
335
336/*-------------------------------------------------------------------------*/
337
338/* spi_test implementation */
339
340#define RANGE_CHECK(ptr, plen, start, slen) \
341 ((ptr >= start) && (ptr + plen <= start + slen))
342
343/* we allocate one page more, to allow for offsets */
344#define SPI_TEST_MAX_SIZE_PLUS (SPI_TEST_MAX_SIZE + PAGE_SIZE)
345
346static void spi_test_print_hex_dump(char *pre, const void *ptr, size_t len)
347{
348 /* limit the hex_dump */
349 if (len < 1024) {
350 print_hex_dump(KERN_INFO, pre,
351 DUMP_PREFIX_OFFSET, 16, 1,
352 ptr, len, 0);
353 return;
354 }
355 /* print head */
356 print_hex_dump(KERN_INFO, pre,
357 DUMP_PREFIX_OFFSET, 16, 1,
358 ptr, 512, 0);
359 /* print tail */
d58b9fda 360 pr_info("%s truncated - continuing at offset %04zx\n",
84e0c4e5
MS
361 pre, len - 512);
362 print_hex_dump(KERN_INFO, pre,
363 DUMP_PREFIX_OFFSET, 16, 1,
364 ptr + (len - 512), 512, 0);
365}
366
367static void spi_test_dump_message(struct spi_device *spi,
368 struct spi_message *msg,
369 bool dump_data)
370{
371 struct spi_transfer *xfer;
372 int i;
373 u8 b;
374
375 dev_info(&spi->dev, " spi_msg@%pK\n", msg);
376 if (msg->status)
377 dev_info(&spi->dev, " status: %i\n",
378 msg->status);
379 dev_info(&spi->dev, " frame_length: %i\n",
380 msg->frame_length);
381 dev_info(&spi->dev, " actual_length: %i\n",
382 msg->actual_length);
383
384 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
385 dev_info(&spi->dev, " spi_transfer@%pK\n", xfer);
386 dev_info(&spi->dev, " len: %i\n", xfer->len);
387 dev_info(&spi->dev, " tx_buf: %pK\n", xfer->tx_buf);
388 if (dump_data && xfer->tx_buf)
389 spi_test_print_hex_dump(" TX: ",
390 xfer->tx_buf,
391 xfer->len);
392
393 dev_info(&spi->dev, " rx_buf: %pK\n", xfer->rx_buf);
394 if (dump_data && xfer->rx_buf)
395 spi_test_print_hex_dump(" RX: ",
396 xfer->rx_buf,
397 xfer->len);
398 /* check for unwritten test pattern on rx_buf */
399 if (xfer->rx_buf) {
400 for (i = 0 ; i < xfer->len ; i++) {
401 b = ((u8 *)xfer->rx_buf)[xfer->len - 1 - i];
402 if (b != SPI_TEST_PATTERN_UNWRITTEN)
403 break;
404 }
405 if (i)
406 dev_info(&spi->dev,
407 " rx_buf filled with %02x starts at offset: %i\n",
408 SPI_TEST_PATTERN_UNWRITTEN,
409 xfer->len - i);
410 }
411 }
412}
413
414struct rx_ranges {
415 struct list_head list;
416 u8 *start;
417 u8 *end;
418};
419
dc34b89a 420static int rx_ranges_cmp(void *priv, struct list_head *a, struct list_head *b)
84e0c4e5
MS
421{
422 struct rx_ranges *rx_a = list_entry(a, struct rx_ranges, list);
423 struct rx_ranges *rx_b = list_entry(b, struct rx_ranges, list);
424
425 if (rx_a->start > rx_b->start)
426 return 1;
427 if (rx_a->start < rx_b->start)
428 return -1;
429 return 0;
430}
431
432static int spi_check_rx_ranges(struct spi_device *spi,
433 struct spi_message *msg,
434 void *rx)
435{
436 struct spi_transfer *xfer;
437 struct rx_ranges ranges[SPI_TEST_MAX_TRANSFERS], *r;
438 int i = 0;
439 LIST_HEAD(ranges_list);
440 u8 *addr;
441 int ret = 0;
442
443 /* loop over all transfers to fill in the rx_ranges */
444 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
445 /* if there is no rx, then no check is needed */
446 if (!xfer->rx_buf)
447 continue;
448 /* fill in the rx_range */
449 if (RANGE_CHECK(xfer->rx_buf, xfer->len,
450 rx, SPI_TEST_MAX_SIZE_PLUS)) {
451 ranges[i].start = xfer->rx_buf;
452 ranges[i].end = xfer->rx_buf + xfer->len;
453 list_add(&ranges[i].list, &ranges_list);
454 i++;
455 }
456 }
457
458 /* if no ranges, then we can return and avoid the checks...*/
459 if (!i)
460 return 0;
461
462 /* sort the list */
463 list_sort(NULL, &ranges_list, rx_ranges_cmp);
464
465 /* and iterate over all the rx addresses */
466 for (addr = rx; addr < (u8 *)rx + SPI_TEST_MAX_SIZE_PLUS; addr++) {
467 /* if we are the DO not write pattern,
468 * then continue with the loop...
469 */
470 if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
471 continue;
472
473 /* check if we are inside a range */
474 list_for_each_entry(r, &ranges_list, list) {
475 /* if so then set to end... */
476 if ((addr >= r->start) && (addr < r->end))
477 addr = r->end;
478 }
479 /* second test after a (hopefull) translation */
480 if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
481 continue;
482
483 /* if still not found then something has modified too much */
484 /* we could list the "closest" transfer here... */
485 dev_err(&spi->dev,
486 "loopback strangeness - rx changed outside of allowed range at: %pK\n",
487 addr);
488 /* do not return, only set ret,
489 * so that we list all addresses
490 */
491 ret = -ERANGE;
492 }
493
494 return ret;
495}
496
497static int spi_test_check_loopback_result(struct spi_device *spi,
498 struct spi_message *msg,
499 void *tx, void *rx)
500{
501 struct spi_transfer *xfer;
502 u8 rxb, txb;
503 size_t i;
1e8db97f
MS
504 int ret;
505
506 /* checks rx_buffer pattern are valid with loopback or without */
576333a1
FI
507 if (check_ranges) {
508 ret = spi_check_rx_ranges(spi, msg, rx);
509 if (ret)
510 return ret;
511 }
84e0c4e5 512
1e8db97f
MS
513 /* if we run without loopback, then return now */
514 if (!loopback)
515 return 0;
516
517 /* if applicable to transfer check that rx_buf is equal to tx_buf */
84e0c4e5
MS
518 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
519 /* if there is no rx, then no check is needed */
520 if (!xfer->rx_buf)
521 continue;
522 /* so depending on tx_buf we need to handle things */
523 if (xfer->tx_buf) {
524 for (i = 1; i < xfer->len; i++) {
525 txb = ((u8 *)xfer->tx_buf)[i];
526 rxb = ((u8 *)xfer->rx_buf)[i];
527 if (txb != rxb)
528 goto mismatch_error;
529 }
530 } else {
531 /* first byte received */
532 txb = ((u8 *)xfer->rx_buf)[0];
533 /* first byte may be 0 or xff */
534 if (!((txb == 0) || (txb == 0xff))) {
535 dev_err(&spi->dev,
536 "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
537 txb);
538 return -EINVAL;
539 }
540 /* check that all bytes are identical */
541 for (i = 1; i < xfer->len; i++) {
542 rxb = ((u8 *)xfer->rx_buf)[i];
543 if (rxb != txb)
544 goto mismatch_error;
545 }
546 }
547 }
548
1e8db97f 549 return 0;
84e0c4e5
MS
550
551mismatch_error:
552 dev_err(&spi->dev,
b7ddfb9f 553 "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
84e0c4e5
MS
554 i, txb, rxb);
555
556 return -EINVAL;
557}
558
559static int spi_test_translate(struct spi_device *spi,
560 void **ptr, size_t len,
561 void *tx, void *rx)
562{
563 size_t off;
564
565 /* return on null */
566 if (!*ptr)
567 return 0;
568
569 /* in the MAX_SIZE_HALF case modify the pointer */
570 if (((size_t)*ptr) & SPI_TEST_MAX_SIZE_HALF)
571 /* move the pointer to the correct range */
572 *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) -
573 SPI_TEST_MAX_SIZE_HALF;
574
575 /* RX range
576 * - we check against MAX_SIZE_PLUS to allow for automated alignment
577 */
578 if (RANGE_CHECK(*ptr, len, RX(0), SPI_TEST_MAX_SIZE_PLUS)) {
579 off = *ptr - RX(0);
580 *ptr = rx + off;
581
582 return 0;
583 }
584
585 /* TX range */
586 if (RANGE_CHECK(*ptr, len, TX(0), SPI_TEST_MAX_SIZE_PLUS)) {
587 off = *ptr - TX(0);
588 *ptr = tx + off;
589
590 return 0;
591 }
592
593 dev_err(&spi->dev,
594 "PointerRange [%pK:%pK[ not in range [%pK:%pK[ or [%pK:%pK[\n",
595 *ptr, *ptr + len,
596 RX(0), RX(SPI_TEST_MAX_SIZE),
597 TX(0), TX(SPI_TEST_MAX_SIZE));
598
599 return -EINVAL;
600}
601
339ec3ce
MS
602static int spi_test_fill_pattern(struct spi_device *spi,
603 struct spi_test *test)
84e0c4e5
MS
604{
605 struct spi_transfer *xfers = test->transfers;
606 u8 *tx_buf;
607 size_t count = 0;
608 int i, j;
609
610#ifdef __BIG_ENDIAN
611#define GET_VALUE_BYTE(value, index, bytes) \
612 (value >> (8 * (bytes - 1 - count % bytes)))
613#else
614#define GET_VALUE_BYTE(value, index, bytes) \
615 (value >> (8 * (count % bytes)))
616#endif
617
618 /* fill all transfers with the pattern requested */
619 for (i = 0; i < test->transfer_count; i++) {
e6520a3c
MS
620 /* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */
621 if (xfers[i].rx_buf)
622 memset(xfers[i].rx_buf, SPI_TEST_PATTERN_UNWRITTEN,
623 xfers[i].len);
84e0c4e5
MS
624 /* if tx_buf is NULL then skip */
625 tx_buf = (u8 *)xfers[i].tx_buf;
626 if (!tx_buf)
627 continue;
628 /* modify all the transfers */
629 for (j = 0; j < xfers[i].len; j++, tx_buf++, count++) {
630 /* fill tx */
631 switch (test->fill_option) {
632 case FILL_MEMSET_8:
633 *tx_buf = test->fill_pattern;
634 break;
635 case FILL_MEMSET_16:
636 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
637 count, 2);
638 break;
639 case FILL_MEMSET_24:
640 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
641 count, 3);
642 break;
643 case FILL_MEMSET_32:
644 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
645 count, 4);
646 break;
647 case FILL_COUNT_8:
648 *tx_buf = count;
649 break;
650 case FILL_COUNT_16:
651 *tx_buf = GET_VALUE_BYTE(count, count, 2);
652 break;
653 case FILL_COUNT_24:
654 *tx_buf = GET_VALUE_BYTE(count, count, 3);
655 break;
656 case FILL_COUNT_32:
657 *tx_buf = GET_VALUE_BYTE(count, count, 4);
658 break;
659 case FILL_TRANSFER_BYTE_8:
660 *tx_buf = j;
661 break;
662 case FILL_TRANSFER_BYTE_16:
663 *tx_buf = GET_VALUE_BYTE(j, j, 2);
664 break;
665 case FILL_TRANSFER_BYTE_24:
666 *tx_buf = GET_VALUE_BYTE(j, j, 3);
667 break;
668 case FILL_TRANSFER_BYTE_32:
669 *tx_buf = GET_VALUE_BYTE(j, j, 4);
670 break;
671 case FILL_TRANSFER_NUM:
672 *tx_buf = i;
673 break;
674 default:
675 dev_err(&spi->dev,
676 "unsupported fill_option: %i\n",
677 test->fill_option);
678 return -EINVAL;
679 }
680 }
84e0c4e5
MS
681 }
682
683 return 0;
684}
685
686static int _spi_test_run_iter(struct spi_device *spi,
687 struct spi_test *test,
688 void *tx, void *rx)
689{
690 struct spi_message *msg = &test->msg;
691 struct spi_transfer *x;
692 int i, ret;
693
694 /* initialize message - zero-filled via static initialization */
695 spi_message_init_no_memset(msg);
696
697 /* fill rx with the DO_NOT_WRITE pattern */
698 memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS);
699
700 /* add the individual transfers */
701 for (i = 0; i < test->transfer_count; i++) {
702 x = &test->transfers[i];
703
704 /* patch the values of tx_buf */
705 ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len,
706 (void *)tx, rx);
707 if (ret)
708 return ret;
709
710 /* patch the values of rx_buf */
711 ret = spi_test_translate(spi, &x->rx_buf, x->len,
712 (void *)tx, rx);
713 if (ret)
714 return ret;
715
716 /* and add it to the list */
717 spi_message_add_tail(x, msg);
718 }
719
339ec3ce
MS
720 /* fill in the transfer buffers with pattern */
721 ret = spi_test_fill_pattern(spi, test);
84e0c4e5
MS
722 if (ret)
723 return ret;
724
725 /* and execute */
726 if (test->execute_msg)
727 ret = test->execute_msg(spi, test, tx, rx);
728 else
729 ret = spi_test_execute_msg(spi, test, tx, rx);
730
731 /* handle result */
732 if (ret == test->expected_return)
733 return 0;
734
735 dev_err(&spi->dev,
736 "test failed - test returned %i, but we expect %i\n",
737 ret, test->expected_return);
738
739 if (ret)
740 return ret;
741
742 /* if it is 0, as we expected something else,
743 * then return something special
744 */
745 return -EFAULT;
746}
747
748static int spi_test_run_iter(struct spi_device *spi,
749 const struct spi_test *testtemplate,
750 void *tx, void *rx,
751 size_t len,
752 size_t tx_off,
753 size_t rx_off
754 )
755{
756 struct spi_test test;
757 int i, tx_count, rx_count;
758
759 /* copy the test template to test */
760 memcpy(&test, testtemplate, sizeof(test));
761
762 /* set up test->transfers to the correct count */
763 if (!test.transfer_count) {
764 for (i = 0;
765 (i < SPI_TEST_MAX_TRANSFERS) && test.transfers[i].len;
766 i++) {
767 test.transfer_count++;
768 }
769 }
770
771 /* if iterate_transfer_mask is not set,
772 * then set it to first transfer only
773 */
774 if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1)))
775 test.iterate_transfer_mask = 1;
776
777 /* count number of transfers with tx/rx_buf != NULL */
ebea7c05 778 rx_count = tx_count = 0;
84e0c4e5
MS
779 for (i = 0; i < test.transfer_count; i++) {
780 if (test.transfers[i].tx_buf)
781 tx_count++;
782 if (test.transfers[i].rx_buf)
783 rx_count++;
784 }
785
786 /* in some iteration cases warn and exit early,
787 * as there is nothing to do, that has not been tested already...
788 */
789 if (tx_off && (!tx_count)) {
790 dev_warn_once(&spi->dev,
791 "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
792 test.description);
793 return 0;
794 }
795 if (rx_off && (!rx_count)) {
796 dev_warn_once(&spi->dev,
797 "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
798 test.description);
799 return 0;
800 }
801
802 /* write out info */
803 if (!(len || tx_off || rx_off)) {
804 dev_info(&spi->dev, "Running test %s\n", test.description);
805 } else {
806 dev_info(&spi->dev,
d58b9fda 807 " with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
84e0c4e5
MS
808 len, tx_off, rx_off);
809 }
810
811 /* update in the values from iteration values */
812 for (i = 0; i < test.transfer_count; i++) {
813 /* only when bit in transfer mask is set */
814 if (!(test.iterate_transfer_mask & BIT(i)))
815 continue;
816 if (len)
817 test.transfers[i].len = len;
818 if (test.transfers[i].tx_buf)
819 test.transfers[i].tx_buf += tx_off;
820 if (test.transfers[i].tx_buf)
821 test.transfers[i].rx_buf += rx_off;
822 }
823
824 /* and execute */
825 return _spi_test_run_iter(spi, &test, tx, rx);
826}
827
828/**
829 * spi_test_execute_msg - default implementation to run a test
830 *
831 * spi: @spi_device on which to run the @spi_message
832 * test: the test to execute, which already contains @msg
833 * tx: the tx buffer allocated for the test sequence
834 * rx: the rx buffer allocated for the test sequence
835 *
836 * Returns: error code of spi_sync as well as basic error checking
837 */
838int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test,
839 void *tx, void *rx)
840{
841 struct spi_message *msg = &test->msg;
842 int ret = 0;
843 int i;
844
845 /* only if we do not simulate */
846 if (!simulate_only) {
847 /* dump the complete message before and after the transfer */
848 if (dump_messages == 3)
849 spi_test_dump_message(spi, msg, true);
850
851 /* run spi message */
852 ret = spi_sync(spi, msg);
853 if (ret == -ETIMEDOUT) {
854 dev_info(&spi->dev,
855 "spi-message timed out - reruning...\n");
856 /* rerun after a few explicit schedules */
857 for (i = 0; i < 16; i++)
858 schedule();
859 ret = spi_sync(spi, msg);
860 }
861 if (ret) {
862 dev_err(&spi->dev,
863 "Failed to execute spi_message: %i\n",
864 ret);
865 goto exit;
866 }
867
868 /* do some extra error checks */
869 if (msg->frame_length != msg->actual_length) {
870 dev_err(&spi->dev,
871 "actual length differs from expected\n");
872 ret = -EIO;
873 goto exit;
874 }
875
1e8db97f
MS
876 /* run rx-buffer tests */
877 ret = spi_test_check_loopback_result(spi, msg, tx, rx);
84e0c4e5
MS
878 }
879
880 /* if requested or on error dump message (including data) */
881exit:
882 if (dump_messages || ret)
883 spi_test_dump_message(spi, msg,
884 (dump_messages >= 2) || (ret));
885
886 return ret;
887}
888EXPORT_SYMBOL_GPL(spi_test_execute_msg);
889
890/**
891 * spi_test_run_test - run an individual spi_test
892 * including all the relevant iterations on:
893 * length and buffer alignment
894 *
895 * spi: the spi_device to send the messages to
896 * test: the test which we need to execute
897 * tx: the tx buffer allocated for the test sequence
898 * rx: the rx buffer allocated for the test sequence
899 *
900 * Returns: status code of spi_sync or other failures
901 */
902
903int spi_test_run_test(struct spi_device *spi, const struct spi_test *test,
904 void *tx, void *rx)
905{
906 int idx_len;
907 size_t len;
908 size_t tx_align, rx_align;
909 int ret;
910
911 /* test for transfer limits */
912 if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) {
913 dev_err(&spi->dev,
914 "%s: Exceeded max number of transfers with %i\n",
915 test->description, test->transfer_count);
916 return -E2BIG;
917 }
918
919 /* setting up some values in spi_message
920 * based on some settings in spi_master
921 * some of this can also get done in the run() method
922 */
923
924 /* iterate over all the iterable values using macros
925 * (to make it a bit more readable...
926 */
927#define FOR_EACH_ITERATE(var, defaultvalue) \
928 for (idx_##var = -1, var = defaultvalue; \
929 ((idx_##var < 0) || \
930 ( \
931 (idx_##var < SPI_TEST_MAX_ITERATE) && \
932 (var = test->iterate_##var[idx_##var]) \
933 ) \
934 ); \
935 idx_##var++)
936#define FOR_EACH_ALIGNMENT(var) \
937 for (var = 0; \
938 var < (test->iterate_##var ? \
939 (spi->master->dma_alignment ? \
940 spi->master->dma_alignment : \
941 test->iterate_##var) : \
942 1); \
943 var++)
944
945 FOR_EACH_ITERATE(len, 0) {
946 FOR_EACH_ALIGNMENT(tx_align) {
947 FOR_EACH_ALIGNMENT(rx_align) {
948 /* and run the iteration */
949 ret = spi_test_run_iter(spi, test,
950 tx, rx,
951 len,
952 tx_align,
953 rx_align);
954 if (ret)
955 return ret;
956 }
957 }
958 }
959
960 return 0;
961}
962EXPORT_SYMBOL_GPL(spi_test_run_test);
963
964/**
965 * spi_test_run_tests - run an array of spi_messages tests
966 * @spi: the spi device on which to run the tests
967 * @tests: NULL-terminated array of @spi_test
968 *
969 * Returns: status errors as per @spi_test_run_test()
970 */
971
972int spi_test_run_tests(struct spi_device *spi,
973 struct spi_test *tests)
974{
975 char *rx = NULL, *tx = NULL;
976 int ret = 0, count = 0;
977 struct spi_test *test;
978
979 /* allocate rx/tx buffers of 128kB size without devm
980 * in the hope that is on a page boundary
981 */
576333a1
FI
982 if (use_vmalloc)
983 rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
984 else
985 rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
84e0c4e5
MS
986 if (!rx) {
987 ret = -ENOMEM;
988 goto out;
989 }
990
576333a1
FI
991 if (use_vmalloc)
992 tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
993 else
994 tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
84e0c4e5
MS
995 if (!tx) {
996 ret = -ENOMEM;
997 goto out;
998 }
999
1000 /* now run the individual tests in the table */
1001 for (test = tests, count = 0; test->description[0];
1002 test++, count++) {
1003 /* only run test if requested */
1004 if ((run_only_test > -1) && (count != run_only_test))
1005 continue;
1006 /* run custom implementation */
1007 if (test->run_test)
1008 ret = test->run_test(spi, test, tx, rx);
1009 else
1010 ret = spi_test_run_test(spi, test, tx, rx);
1011 if (ret)
1012 goto out;
1013 /* add some delays so that we can easily
1014 * detect the individual tests when using a logic analyzer
1015 * we also add scheduling to avoid potential spi_timeouts...
1016 */
1017 mdelay(100);
1018 schedule();
1019 }
1020
1021out:
576333a1
FI
1022 kvfree(rx);
1023 kvfree(tx);
84e0c4e5
MS
1024 return ret;
1025}
1026EXPORT_SYMBOL_GPL(spi_test_run_tests);