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