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