Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  linux/drivers/spi/spi-loopback-test.c
0004  *
0005  *  (c) Martin Sperl <kernel@martin.sperl.org>
0006  *
0007  *  Loopback test driver to test several typical spi_message conditions
0008  *  that a spi_master driver may encounter
0009  *  this can also get used for regression testing
0010  */
0011 
0012 #include <linux/delay.h>
0013 #include <linux/kernel.h>
0014 #include <linux/ktime.h>
0015 #include <linux/list.h>
0016 #include <linux/list_sort.h>
0017 #include <linux/module.h>
0018 #include <linux/of_device.h>
0019 #include <linux/printk.h>
0020 #include <linux/vmalloc.h>
0021 #include <linux/spi/spi.h>
0022 
0023 #include "spi-test.h"
0024 
0025 /* flag to only simulate transfers */
0026 static int simulate_only;
0027 module_param(simulate_only, int, 0);
0028 MODULE_PARM_DESC(simulate_only, "if not 0 do not execute the spi message");
0029 
0030 /* dump spi messages */
0031 static int dump_messages;
0032 module_param(dump_messages, int, 0);
0033 MODULE_PARM_DESC(dump_messages,
0034          "=1 dump the basic spi_message_structure, " \
0035          "=2 dump the spi_message_structure including data, " \
0036          "=3 dump the spi_message structure before and after execution");
0037 /* the device is jumpered for loopback - enabling some rx_buf tests */
0038 static int loopback;
0039 module_param(loopback, int, 0);
0040 MODULE_PARM_DESC(loopback,
0041          "if set enable loopback mode, where the rx_buf "   \
0042          "is checked to match tx_buf after the spi_message "    \
0043          "is executed");
0044 
0045 static int loop_req;
0046 module_param(loop_req, int, 0);
0047 MODULE_PARM_DESC(loop_req,
0048          "if set controller will be asked to enable test loop mode. " \
0049          "If controller supported it, MISO and MOSI will be connected");
0050 
0051 static int no_cs;
0052 module_param(no_cs, int, 0);
0053 MODULE_PARM_DESC(no_cs,
0054          "if set Chip Select (CS) will not be used");
0055 
0056 /* run only a specific test */
0057 static int run_only_test = -1;
0058 module_param(run_only_test, int, 0);
0059 MODULE_PARM_DESC(run_only_test,
0060          "only run the test with this number (0-based !)");
0061 
0062 /* use vmalloc'ed buffers */
0063 static int use_vmalloc;
0064 module_param(use_vmalloc, int, 0644);
0065 MODULE_PARM_DESC(use_vmalloc,
0066          "use vmalloc'ed buffers instead of kmalloc'ed");
0067 
0068 /* check rx ranges */
0069 static int check_ranges = 1;
0070 module_param(check_ranges, int, 0644);
0071 MODULE_PARM_DESC(check_ranges,
0072          "checks rx_buffer pattern are valid");
0073 
0074 /* the actual tests to execute */
0075 static struct spi_test spi_tests[] = {
0076     {
0077         .description    = "tx/rx-transfer - start of page",
0078         .fill_option    = FILL_COUNT_8,
0079         .iterate_len    = { ITERATE_MAX_LEN },
0080         .iterate_tx_align = ITERATE_ALIGN,
0081         .iterate_rx_align = ITERATE_ALIGN,
0082         .transfer_count = 1,
0083         .transfers      = {
0084             {
0085                 .tx_buf = TX(0),
0086                 .rx_buf = RX(0),
0087             },
0088         },
0089     },
0090     {
0091         .description    = "tx/rx-transfer - crossing PAGE_SIZE",
0092         .fill_option    = FILL_COUNT_8,
0093         .iterate_len    = { ITERATE_LEN },
0094         .iterate_tx_align = ITERATE_ALIGN,
0095         .iterate_rx_align = ITERATE_ALIGN,
0096         .transfer_count = 1,
0097         .transfers      = {
0098             {
0099                 .tx_buf = TX(PAGE_SIZE - 4),
0100                 .rx_buf = RX(PAGE_SIZE - 4),
0101             },
0102         },
0103     },
0104     {
0105         .description    = "tx-transfer - only",
0106         .fill_option    = FILL_COUNT_8,
0107         .iterate_len    = { ITERATE_MAX_LEN },
0108         .iterate_tx_align = ITERATE_ALIGN,
0109         .transfer_count = 1,
0110         .transfers      = {
0111             {
0112                 .tx_buf = TX(0),
0113             },
0114         },
0115     },
0116     {
0117         .description    = "rx-transfer - only",
0118         .fill_option    = FILL_COUNT_8,
0119         .iterate_len    = { ITERATE_MAX_LEN },
0120         .iterate_rx_align = ITERATE_ALIGN,
0121         .transfer_count = 1,
0122         .transfers      = {
0123             {
0124                 .rx_buf = RX(0),
0125             },
0126         },
0127     },
0128     {
0129         .description    = "two tx-transfers - alter both",
0130         .fill_option    = FILL_COUNT_8,
0131         .iterate_len    = { ITERATE_LEN },
0132         .iterate_tx_align = ITERATE_ALIGN,
0133         .iterate_transfer_mask = BIT(0) | BIT(1),
0134         .transfer_count = 2,
0135         .transfers      = {
0136             {
0137                 .tx_buf = TX(0),
0138             },
0139             {
0140                 /* this is why we cant use ITERATE_MAX_LEN */
0141                 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
0142             },
0143         },
0144     },
0145     {
0146         .description    = "two tx-transfers - alter first",
0147         .fill_option    = FILL_COUNT_8,
0148         .iterate_len    = { ITERATE_MAX_LEN },
0149         .iterate_tx_align = ITERATE_ALIGN,
0150         .iterate_transfer_mask = BIT(0),
0151         .transfer_count = 2,
0152         .transfers      = {
0153             {
0154                 .tx_buf = TX(64),
0155             },
0156             {
0157                 .len = 1,
0158                 .tx_buf = TX(0),
0159             },
0160         },
0161     },
0162     {
0163         .description    = "two tx-transfers - alter second",
0164         .fill_option    = FILL_COUNT_8,
0165         .iterate_len    = { ITERATE_MAX_LEN },
0166         .iterate_tx_align = ITERATE_ALIGN,
0167         .iterate_transfer_mask = BIT(1),
0168         .transfer_count = 2,
0169         .transfers      = {
0170             {
0171                 .len = 16,
0172                 .tx_buf = TX(0),
0173             },
0174             {
0175                 .tx_buf = TX(64),
0176             },
0177         },
0178     },
0179     {
0180         .description    = "two transfers tx then rx - alter both",
0181         .fill_option    = FILL_COUNT_8,
0182         .iterate_len    = { ITERATE_MAX_LEN },
0183         .iterate_tx_align = ITERATE_ALIGN,
0184         .iterate_transfer_mask = BIT(0) | BIT(1),
0185         .transfer_count = 2,
0186         .transfers      = {
0187             {
0188                 .tx_buf = TX(0),
0189             },
0190             {
0191                 .rx_buf = RX(0),
0192             },
0193         },
0194     },
0195     {
0196         .description    = "two transfers tx then rx - alter tx",
0197         .fill_option    = FILL_COUNT_8,
0198         .iterate_len    = { ITERATE_MAX_LEN },
0199         .iterate_tx_align = ITERATE_ALIGN,
0200         .iterate_transfer_mask = BIT(0),
0201         .transfer_count = 2,
0202         .transfers      = {
0203             {
0204                 .tx_buf = TX(0),
0205             },
0206             {
0207                 .len = 1,
0208                 .rx_buf = RX(0),
0209             },
0210         },
0211     },
0212     {
0213         .description    = "two transfers tx then rx - alter rx",
0214         .fill_option    = FILL_COUNT_8,
0215         .iterate_len    = { ITERATE_MAX_LEN },
0216         .iterate_tx_align = ITERATE_ALIGN,
0217         .iterate_transfer_mask = BIT(1),
0218         .transfer_count = 2,
0219         .transfers      = {
0220             {
0221                 .len = 1,
0222                 .tx_buf = TX(0),
0223             },
0224             {
0225                 .rx_buf = RX(0),
0226             },
0227         },
0228     },
0229     {
0230         .description    = "two tx+rx transfers - alter both",
0231         .fill_option    = FILL_COUNT_8,
0232         .iterate_len    = { ITERATE_LEN },
0233         .iterate_tx_align = ITERATE_ALIGN,
0234         .iterate_transfer_mask = BIT(0) | BIT(1),
0235         .transfer_count = 2,
0236         .transfers      = {
0237             {
0238                 .tx_buf = TX(0),
0239                 .rx_buf = RX(0),
0240             },
0241             {
0242                 /* making sure we align without overwrite
0243                  * the reason we can not use ITERATE_MAX_LEN
0244                  */
0245                 .tx_buf = TX(SPI_TEST_MAX_SIZE_HALF),
0246                 .rx_buf = RX(SPI_TEST_MAX_SIZE_HALF),
0247             },
0248         },
0249     },
0250     {
0251         .description    = "two tx+rx transfers - alter first",
0252         .fill_option    = FILL_COUNT_8,
0253         .iterate_len    = { ITERATE_MAX_LEN },
0254         .iterate_tx_align = ITERATE_ALIGN,
0255         .iterate_transfer_mask = BIT(0),
0256         .transfer_count = 2,
0257         .transfers      = {
0258             {
0259                 /* making sure we align without overwrite */
0260                 .tx_buf = TX(1024),
0261                 .rx_buf = RX(1024),
0262             },
0263             {
0264                 .len = 1,
0265                 /* making sure we align without overwrite */
0266                 .tx_buf = TX(0),
0267                 .rx_buf = RX(0),
0268             },
0269         },
0270     },
0271     {
0272         .description    = "two tx+rx transfers - alter second",
0273         .fill_option    = FILL_COUNT_8,
0274         .iterate_len    = { ITERATE_MAX_LEN },
0275         .iterate_tx_align = ITERATE_ALIGN,
0276         .iterate_transfer_mask = BIT(1),
0277         .transfer_count = 2,
0278         .transfers      = {
0279             {
0280                 .len = 1,
0281                 .tx_buf = TX(0),
0282                 .rx_buf = RX(0),
0283             },
0284             {
0285                 /* making sure we align without overwrite */
0286                 .tx_buf = TX(1024),
0287                 .rx_buf = RX(1024),
0288             },
0289         },
0290     },
0291     {
0292         .description    = "two tx+rx transfers - delay after transfer",
0293         .fill_option    = FILL_COUNT_8,
0294         .iterate_len    = { ITERATE_MAX_LEN },
0295         .iterate_transfer_mask = BIT(0) | BIT(1),
0296         .transfer_count = 2,
0297         .transfers      = {
0298             {
0299                 .tx_buf = TX(0),
0300                 .rx_buf = RX(0),
0301                 .delay = {
0302                     .value = 1000,
0303                     .unit = SPI_DELAY_UNIT_USECS,
0304                 },
0305             },
0306             {
0307                 .tx_buf = TX(0),
0308                 .rx_buf = RX(0),
0309                 .delay = {
0310                     .value = 1000,
0311                     .unit = SPI_DELAY_UNIT_USECS,
0312                 },
0313             },
0314         },
0315     },
0316 
0317     { /* end of tests sequence */ }
0318 };
0319 
0320 static int spi_loopback_test_probe(struct spi_device *spi)
0321 {
0322     int ret;
0323 
0324     if (loop_req || no_cs) {
0325         spi->mode |= loop_req ? SPI_LOOP : 0;
0326         spi->mode |= no_cs ? SPI_NO_CS : 0;
0327         ret = spi_setup(spi);
0328         if (ret) {
0329             dev_err(&spi->dev, "SPI setup with SPI_LOOP or SPI_NO_CS failed (%d)\n",
0330                 ret);
0331             return ret;
0332         }
0333     }
0334 
0335     dev_info(&spi->dev, "Executing spi-loopback-tests\n");
0336 
0337     ret = spi_test_run_tests(spi, spi_tests);
0338 
0339     dev_info(&spi->dev, "Finished spi-loopback-tests with return: %i\n",
0340          ret);
0341 
0342     return ret;
0343 }
0344 
0345 /* non const match table to permit to change via a module parameter */
0346 static struct of_device_id spi_loopback_test_of_match[] = {
0347     { .compatible   = "linux,spi-loopback-test", },
0348     { }
0349 };
0350 
0351 /* allow to override the compatible string via a module_parameter */
0352 module_param_string(compatible, spi_loopback_test_of_match[0].compatible,
0353             sizeof(spi_loopback_test_of_match[0].compatible),
0354             0000);
0355 
0356 MODULE_DEVICE_TABLE(of, spi_loopback_test_of_match);
0357 
0358 static struct spi_driver spi_loopback_test_driver = {
0359     .driver = {
0360         .name = "spi-loopback-test",
0361         .owner = THIS_MODULE,
0362         .of_match_table = spi_loopback_test_of_match,
0363     },
0364     .probe = spi_loopback_test_probe,
0365 };
0366 
0367 module_spi_driver(spi_loopback_test_driver);
0368 
0369 MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
0370 MODULE_DESCRIPTION("test spi_driver to check core functionality");
0371 MODULE_LICENSE("GPL");
0372 
0373 /*-------------------------------------------------------------------------*/
0374 
0375 /* spi_test implementation */
0376 
0377 #define RANGE_CHECK(ptr, plen, start, slen) \
0378     ((ptr >= start) && (ptr + plen <= start + slen))
0379 
0380 /* we allocate one page more, to allow for offsets */
0381 #define SPI_TEST_MAX_SIZE_PLUS (SPI_TEST_MAX_SIZE + PAGE_SIZE)
0382 
0383 static void spi_test_print_hex_dump(char *pre, const void *ptr, size_t len)
0384 {
0385     /* limit the hex_dump */
0386     if (len < 1024) {
0387         print_hex_dump(KERN_INFO, pre,
0388                    DUMP_PREFIX_OFFSET, 16, 1,
0389                    ptr, len, 0);
0390         return;
0391     }
0392     /* print head */
0393     print_hex_dump(KERN_INFO, pre,
0394                DUMP_PREFIX_OFFSET, 16, 1,
0395                ptr, 512, 0);
0396     /* print tail */
0397     pr_info("%s truncated - continuing at offset %04zx\n",
0398         pre, len - 512);
0399     print_hex_dump(KERN_INFO, pre,
0400                DUMP_PREFIX_OFFSET, 16, 1,
0401                ptr + (len - 512), 512, 0);
0402 }
0403 
0404 static void spi_test_dump_message(struct spi_device *spi,
0405                   struct spi_message *msg,
0406                   bool dump_data)
0407 {
0408     struct spi_transfer *xfer;
0409     int i;
0410     u8 b;
0411 
0412     dev_info(&spi->dev, "  spi_msg@%pK\n", msg);
0413     if (msg->status)
0414         dev_info(&spi->dev, "    status:        %i\n",
0415              msg->status);
0416     dev_info(&spi->dev, "    frame_length:  %i\n",
0417          msg->frame_length);
0418     dev_info(&spi->dev, "    actual_length: %i\n",
0419          msg->actual_length);
0420 
0421     list_for_each_entry(xfer, &msg->transfers, transfer_list) {
0422         dev_info(&spi->dev, "    spi_transfer@%pK\n", xfer);
0423         dev_info(&spi->dev, "      len:    %i\n", xfer->len);
0424         dev_info(&spi->dev, "      tx_buf: %pK\n", xfer->tx_buf);
0425         if (dump_data && xfer->tx_buf)
0426             spi_test_print_hex_dump("          TX: ",
0427                         xfer->tx_buf,
0428                         xfer->len);
0429 
0430         dev_info(&spi->dev, "      rx_buf: %pK\n", xfer->rx_buf);
0431         if (dump_data && xfer->rx_buf)
0432             spi_test_print_hex_dump("          RX: ",
0433                         xfer->rx_buf,
0434                         xfer->len);
0435         /* check for unwritten test pattern on rx_buf */
0436         if (xfer->rx_buf) {
0437             for (i = 0 ; i < xfer->len ; i++) {
0438                 b = ((u8 *)xfer->rx_buf)[xfer->len - 1 - i];
0439                 if (b != SPI_TEST_PATTERN_UNWRITTEN)
0440                     break;
0441             }
0442             if (i)
0443                 dev_info(&spi->dev,
0444                      "      rx_buf filled with %02x starts at offset: %i\n",
0445                      SPI_TEST_PATTERN_UNWRITTEN,
0446                      xfer->len - i);
0447         }
0448     }
0449 }
0450 
0451 struct rx_ranges {
0452     struct list_head list;
0453     u8 *start;
0454     u8 *end;
0455 };
0456 
0457 static int rx_ranges_cmp(void *priv, const struct list_head *a,
0458              const struct list_head *b)
0459 {
0460     struct rx_ranges *rx_a = list_entry(a, struct rx_ranges, list);
0461     struct rx_ranges *rx_b = list_entry(b, struct rx_ranges, list);
0462 
0463     if (rx_a->start > rx_b->start)
0464         return 1;
0465     if (rx_a->start < rx_b->start)
0466         return -1;
0467     return 0;
0468 }
0469 
0470 static int spi_check_rx_ranges(struct spi_device *spi,
0471                    struct spi_message *msg,
0472                    void *rx)
0473 {
0474     struct spi_transfer *xfer;
0475     struct rx_ranges ranges[SPI_TEST_MAX_TRANSFERS], *r;
0476     int i = 0;
0477     LIST_HEAD(ranges_list);
0478     u8 *addr;
0479     int ret = 0;
0480 
0481     /* loop over all transfers to fill in the rx_ranges */
0482     list_for_each_entry(xfer, &msg->transfers, transfer_list) {
0483         /* if there is no rx, then no check is needed */
0484         if (!xfer->rx_buf)
0485             continue;
0486         /* fill in the rx_range */
0487         if (RANGE_CHECK(xfer->rx_buf, xfer->len,
0488                 rx, SPI_TEST_MAX_SIZE_PLUS)) {
0489             ranges[i].start = xfer->rx_buf;
0490             ranges[i].end = xfer->rx_buf + xfer->len;
0491             list_add(&ranges[i].list, &ranges_list);
0492             i++;
0493         }
0494     }
0495 
0496     /* if no ranges, then we can return and avoid the checks...*/
0497     if (!i)
0498         return 0;
0499 
0500     /* sort the list */
0501     list_sort(NULL, &ranges_list, rx_ranges_cmp);
0502 
0503     /* and iterate over all the rx addresses */
0504     for (addr = rx; addr < (u8 *)rx + SPI_TEST_MAX_SIZE_PLUS; addr++) {
0505         /* if we are the DO not write pattern,
0506          * then continue with the loop...
0507          */
0508         if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
0509             continue;
0510 
0511         /* check if we are inside a range */
0512         list_for_each_entry(r, &ranges_list, list) {
0513             /* if so then set to end... */
0514             if ((addr >= r->start) && (addr < r->end))
0515                 addr = r->end;
0516         }
0517         /* second test after a (hopefull) translation */
0518         if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
0519             continue;
0520 
0521         /* if still not found then something has modified too much */
0522         /* we could list the "closest" transfer here... */
0523         dev_err(&spi->dev,
0524             "loopback strangeness - rx changed outside of allowed range at: %pK\n",
0525             addr);
0526         /* do not return, only set ret,
0527          * so that we list all addresses
0528          */
0529         ret = -ERANGE;
0530     }
0531 
0532     return ret;
0533 }
0534 
0535 static int spi_test_check_elapsed_time(struct spi_device *spi,
0536                        struct spi_test *test)
0537 {
0538     int i;
0539     unsigned long long estimated_time = 0;
0540     unsigned long long delay_usecs = 0;
0541 
0542     for (i = 0; i < test->transfer_count; i++) {
0543         struct spi_transfer *xfer = test->transfers + i;
0544         unsigned long long nbits = (unsigned long long)BITS_PER_BYTE *
0545                        xfer->len;
0546 
0547         delay_usecs += xfer->delay.value;
0548         if (!xfer->speed_hz)
0549             continue;
0550         estimated_time += div_u64(nbits * NSEC_PER_SEC, xfer->speed_hz);
0551     }
0552 
0553     estimated_time += delay_usecs * NSEC_PER_USEC;
0554     if (test->elapsed_time < estimated_time) {
0555         dev_err(&spi->dev,
0556             "elapsed time %lld ns is shorter than minimum estimated time %lld ns\n",
0557             test->elapsed_time, estimated_time);
0558 
0559         return -EINVAL;
0560     }
0561 
0562     return 0;
0563 }
0564 
0565 static int spi_test_check_loopback_result(struct spi_device *spi,
0566                       struct spi_message *msg,
0567                       void *tx, void *rx)
0568 {
0569     struct spi_transfer *xfer;
0570     u8 rxb, txb;
0571     size_t i;
0572     int ret;
0573 
0574     /* checks rx_buffer pattern are valid with loopback or without */
0575     if (check_ranges) {
0576         ret = spi_check_rx_ranges(spi, msg, rx);
0577         if (ret)
0578             return ret;
0579     }
0580 
0581     /* if we run without loopback, then return now */
0582     if (!loopback)
0583         return 0;
0584 
0585     /* if applicable to transfer check that rx_buf is equal to tx_buf */
0586     list_for_each_entry(xfer, &msg->transfers, transfer_list) {
0587         /* if there is no rx, then no check is needed */
0588         if (!xfer->len || !xfer->rx_buf)
0589             continue;
0590         /* so depending on tx_buf we need to handle things */
0591         if (xfer->tx_buf) {
0592             for (i = 0; i < xfer->len; i++) {
0593                 txb = ((u8 *)xfer->tx_buf)[i];
0594                 rxb = ((u8 *)xfer->rx_buf)[i];
0595                 if (txb != rxb)
0596                     goto mismatch_error;
0597             }
0598         } else {
0599             /* first byte received */
0600             txb = ((u8 *)xfer->rx_buf)[0];
0601             /* first byte may be 0 or xff */
0602             if (!((txb == 0) || (txb == 0xff))) {
0603                 dev_err(&spi->dev,
0604                     "loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
0605                     txb);
0606                 return -EINVAL;
0607             }
0608             /* check that all bytes are identical */
0609             for (i = 1; i < xfer->len; i++) {
0610                 rxb = ((u8 *)xfer->rx_buf)[i];
0611                 if (rxb != txb)
0612                     goto mismatch_error;
0613             }
0614         }
0615     }
0616 
0617     return 0;
0618 
0619 mismatch_error:
0620     dev_err(&spi->dev,
0621         "loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
0622         i, txb, rxb);
0623 
0624     return -EINVAL;
0625 }
0626 
0627 static int spi_test_translate(struct spi_device *spi,
0628                   void **ptr, size_t len,
0629                   void *tx, void *rx)
0630 {
0631     size_t off;
0632 
0633     /* return on null */
0634     if (!*ptr)
0635         return 0;
0636 
0637     /* in the MAX_SIZE_HALF case modify the pointer */
0638     if (((size_t)*ptr) & SPI_TEST_MAX_SIZE_HALF)
0639         /* move the pointer to the correct range */
0640         *ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) -
0641             SPI_TEST_MAX_SIZE_HALF;
0642 
0643     /* RX range
0644      * - we check against MAX_SIZE_PLUS to allow for automated alignment
0645      */
0646     if (RANGE_CHECK(*ptr, len,  RX(0), SPI_TEST_MAX_SIZE_PLUS)) {
0647         off = *ptr - RX(0);
0648         *ptr = rx + off;
0649 
0650         return 0;
0651     }
0652 
0653     /* TX range */
0654     if (RANGE_CHECK(*ptr, len,  TX(0), SPI_TEST_MAX_SIZE_PLUS)) {
0655         off = *ptr - TX(0);
0656         *ptr = tx + off;
0657 
0658         return 0;
0659     }
0660 
0661     dev_err(&spi->dev,
0662         "PointerRange [%pK:%pK[ not in range [%pK:%pK[ or [%pK:%pK[\n",
0663         *ptr, *ptr + len,
0664         RX(0), RX(SPI_TEST_MAX_SIZE),
0665         TX(0), TX(SPI_TEST_MAX_SIZE));
0666 
0667     return -EINVAL;
0668 }
0669 
0670 static int spi_test_fill_pattern(struct spi_device *spi,
0671                  struct spi_test *test)
0672 {
0673     struct spi_transfer *xfers = test->transfers;
0674     u8 *tx_buf;
0675     size_t count = 0;
0676     int i, j;
0677 
0678 #ifdef __BIG_ENDIAN
0679 #define GET_VALUE_BYTE(value, index, bytes) \
0680     (value >> (8 * (bytes - 1 - count % bytes)))
0681 #else
0682 #define GET_VALUE_BYTE(value, index, bytes) \
0683     (value >> (8 * (count % bytes)))
0684 #endif
0685 
0686     /* fill all transfers with the pattern requested */
0687     for (i = 0; i < test->transfer_count; i++) {
0688         /* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */
0689         if (xfers[i].rx_buf)
0690             memset(xfers[i].rx_buf, SPI_TEST_PATTERN_UNWRITTEN,
0691                    xfers[i].len);
0692         /* if tx_buf is NULL then skip */
0693         tx_buf = (u8 *)xfers[i].tx_buf;
0694         if (!tx_buf)
0695             continue;
0696         /* modify all the transfers */
0697         for (j = 0; j < xfers[i].len; j++, tx_buf++, count++) {
0698             /* fill tx */
0699             switch (test->fill_option) {
0700             case FILL_MEMSET_8:
0701                 *tx_buf = test->fill_pattern;
0702                 break;
0703             case FILL_MEMSET_16:
0704                 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
0705                              count, 2);
0706                 break;
0707             case FILL_MEMSET_24:
0708                 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
0709                              count, 3);
0710                 break;
0711             case FILL_MEMSET_32:
0712                 *tx_buf = GET_VALUE_BYTE(test->fill_pattern,
0713                              count, 4);
0714                 break;
0715             case FILL_COUNT_8:
0716                 *tx_buf = count;
0717                 break;
0718             case FILL_COUNT_16:
0719                 *tx_buf = GET_VALUE_BYTE(count, count, 2);
0720                 break;
0721             case FILL_COUNT_24:
0722                 *tx_buf = GET_VALUE_BYTE(count, count, 3);
0723                 break;
0724             case FILL_COUNT_32:
0725                 *tx_buf = GET_VALUE_BYTE(count, count, 4);
0726                 break;
0727             case FILL_TRANSFER_BYTE_8:
0728                 *tx_buf = j;
0729                 break;
0730             case FILL_TRANSFER_BYTE_16:
0731                 *tx_buf = GET_VALUE_BYTE(j, j, 2);
0732                 break;
0733             case FILL_TRANSFER_BYTE_24:
0734                 *tx_buf = GET_VALUE_BYTE(j, j, 3);
0735                 break;
0736             case FILL_TRANSFER_BYTE_32:
0737                 *tx_buf = GET_VALUE_BYTE(j, j, 4);
0738                 break;
0739             case FILL_TRANSFER_NUM:
0740                 *tx_buf = i;
0741                 break;
0742             default:
0743                 dev_err(&spi->dev,
0744                     "unsupported fill_option: %i\n",
0745                     test->fill_option);
0746                 return -EINVAL;
0747             }
0748         }
0749     }
0750 
0751     return 0;
0752 }
0753 
0754 static int _spi_test_run_iter(struct spi_device *spi,
0755                   struct spi_test *test,
0756                   void *tx, void *rx)
0757 {
0758     struct spi_message *msg = &test->msg;
0759     struct spi_transfer *x;
0760     int i, ret;
0761 
0762     /* initialize message - zero-filled via static initialization */
0763     spi_message_init_no_memset(msg);
0764 
0765     /* fill rx with the DO_NOT_WRITE pattern */
0766     memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS);
0767 
0768     /* add the individual transfers */
0769     for (i = 0; i < test->transfer_count; i++) {
0770         x = &test->transfers[i];
0771 
0772         /* patch the values of tx_buf */
0773         ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len,
0774                      (void *)tx, rx);
0775         if (ret)
0776             return ret;
0777 
0778         /* patch the values of rx_buf */
0779         ret = spi_test_translate(spi, &x->rx_buf, x->len,
0780                      (void *)tx, rx);
0781         if (ret)
0782             return ret;
0783 
0784         /* and add it to the list */
0785         spi_message_add_tail(x, msg);
0786     }
0787 
0788     /* fill in the transfer buffers with pattern */
0789     ret = spi_test_fill_pattern(spi, test);
0790     if (ret)
0791         return ret;
0792 
0793     /* and execute */
0794     if (test->execute_msg)
0795         ret = test->execute_msg(spi, test, tx, rx);
0796     else
0797         ret = spi_test_execute_msg(spi, test, tx, rx);
0798 
0799     /* handle result */
0800     if (ret == test->expected_return)
0801         return 0;
0802 
0803     dev_err(&spi->dev,
0804         "test failed - test returned %i, but we expect %i\n",
0805         ret, test->expected_return);
0806 
0807     if (ret)
0808         return ret;
0809 
0810     /* if it is 0, as we expected something else,
0811      * then return something special
0812      */
0813     return -EFAULT;
0814 }
0815 
0816 static int spi_test_run_iter(struct spi_device *spi,
0817                  const struct spi_test *testtemplate,
0818                  void *tx, void *rx,
0819                  size_t len,
0820                  size_t tx_off,
0821                  size_t rx_off
0822     )
0823 {
0824     struct spi_test test;
0825     int i, tx_count, rx_count;
0826 
0827     /* copy the test template to test */
0828     memcpy(&test, testtemplate, sizeof(test));
0829 
0830     /* if iterate_transfer_mask is not set,
0831      * then set it to first transfer only
0832      */
0833     if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1)))
0834         test.iterate_transfer_mask = 1;
0835 
0836     /* count number of transfers with tx/rx_buf != NULL */
0837     rx_count = tx_count = 0;
0838     for (i = 0; i < test.transfer_count; i++) {
0839         if (test.transfers[i].tx_buf)
0840             tx_count++;
0841         if (test.transfers[i].rx_buf)
0842             rx_count++;
0843     }
0844 
0845     /* in some iteration cases warn and exit early,
0846      * as there is nothing to do, that has not been tested already...
0847      */
0848     if (tx_off && (!tx_count)) {
0849         dev_warn_once(&spi->dev,
0850                   "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
0851                   test.description);
0852         return 0;
0853     }
0854     if (rx_off && (!rx_count)) {
0855         dev_warn_once(&spi->dev,
0856                   "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
0857                   test.description);
0858         return 0;
0859     }
0860 
0861     /* write out info */
0862     if (!(len || tx_off || rx_off)) {
0863         dev_info(&spi->dev, "Running test %s\n", test.description);
0864     } else {
0865         dev_info(&spi->dev,
0866              "  with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
0867              len, tx_off, rx_off);
0868     }
0869 
0870     /* update in the values from iteration values */
0871     for (i = 0; i < test.transfer_count; i++) {
0872         /* only when bit in transfer mask is set */
0873         if (!(test.iterate_transfer_mask & BIT(i)))
0874             continue;
0875         test.transfers[i].len = len;
0876         if (test.transfers[i].tx_buf)
0877             test.transfers[i].tx_buf += tx_off;
0878         if (test.transfers[i].rx_buf)
0879             test.transfers[i].rx_buf += rx_off;
0880     }
0881 
0882     /* and execute */
0883     return _spi_test_run_iter(spi, &test, tx, rx);
0884 }
0885 
0886 /**
0887  * spi_test_execute_msg - default implementation to run a test
0888  *
0889  * @spi: @spi_device on which to run the @spi_message
0890  * @test: the test to execute, which already contains @msg
0891  * @tx:   the tx buffer allocated for the test sequence
0892  * @rx:   the rx buffer allocated for the test sequence
0893  *
0894  * Returns: error code of spi_sync as well as basic error checking
0895  */
0896 int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test,
0897              void *tx, void *rx)
0898 {
0899     struct spi_message *msg = &test->msg;
0900     int ret = 0;
0901     int i;
0902 
0903     /* only if we do not simulate */
0904     if (!simulate_only) {
0905         ktime_t start;
0906 
0907         /* dump the complete message before and after the transfer */
0908         if (dump_messages == 3)
0909             spi_test_dump_message(spi, msg, true);
0910 
0911         start = ktime_get();
0912         /* run spi message */
0913         ret = spi_sync(spi, msg);
0914         test->elapsed_time = ktime_to_ns(ktime_sub(ktime_get(), start));
0915         if (ret == -ETIMEDOUT) {
0916             dev_info(&spi->dev,
0917                  "spi-message timed out - rerunning...\n");
0918             /* rerun after a few explicit schedules */
0919             for (i = 0; i < 16; i++)
0920                 schedule();
0921             ret = spi_sync(spi, msg);
0922         }
0923         if (ret) {
0924             dev_err(&spi->dev,
0925                 "Failed to execute spi_message: %i\n",
0926                 ret);
0927             goto exit;
0928         }
0929 
0930         /* do some extra error checks */
0931         if (msg->frame_length != msg->actual_length) {
0932             dev_err(&spi->dev,
0933                 "actual length differs from expected\n");
0934             ret = -EIO;
0935             goto exit;
0936         }
0937 
0938         /* run rx-buffer tests */
0939         ret = spi_test_check_loopback_result(spi, msg, tx, rx);
0940         if (ret)
0941             goto exit;
0942 
0943         ret = spi_test_check_elapsed_time(spi, test);
0944     }
0945 
0946     /* if requested or on error dump message (including data) */
0947 exit:
0948     if (dump_messages || ret)
0949         spi_test_dump_message(spi, msg,
0950                       (dump_messages >= 2) || (ret));
0951 
0952     return ret;
0953 }
0954 EXPORT_SYMBOL_GPL(spi_test_execute_msg);
0955 
0956 /**
0957  * spi_test_run_test - run an individual spi_test
0958  *                     including all the relevant iterations on:
0959  *                     length and buffer alignment
0960  *
0961  * @spi:  the spi_device to send the messages to
0962  * @test: the test which we need to execute
0963  * @tx:   the tx buffer allocated for the test sequence
0964  * @rx:   the rx buffer allocated for the test sequence
0965  *
0966  * Returns: status code of spi_sync or other failures
0967  */
0968 
0969 int spi_test_run_test(struct spi_device *spi, const struct spi_test *test,
0970               void *tx, void *rx)
0971 {
0972     int idx_len;
0973     size_t len;
0974     size_t tx_align, rx_align;
0975     int ret;
0976 
0977     /* test for transfer limits */
0978     if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) {
0979         dev_err(&spi->dev,
0980             "%s: Exceeded max number of transfers with %i\n",
0981             test->description, test->transfer_count);
0982         return -E2BIG;
0983     }
0984 
0985     /* setting up some values in spi_message
0986      * based on some settings in spi_master
0987      * some of this can also get done in the run() method
0988      */
0989 
0990     /* iterate over all the iterable values using macros
0991      * (to make it a bit more readable...
0992      */
0993 #define FOR_EACH_ALIGNMENT(var)                     \
0994     for (var = 0;                           \
0995         var < (test->iterate_##var ?                \
0996             (spi->master->dma_alignment ?           \
0997              spi->master->dma_alignment :           \
0998              test->iterate_##var) :             \
0999             1);                     \
1000         var++)
1001 
1002     for (idx_len = 0; idx_len < SPI_TEST_MAX_ITERATE &&
1003          (len = test->iterate_len[idx_len]) != -1; idx_len++) {
1004         FOR_EACH_ALIGNMENT(tx_align) {
1005             FOR_EACH_ALIGNMENT(rx_align) {
1006                 /* and run the iteration */
1007                 ret = spi_test_run_iter(spi, test,
1008                             tx, rx,
1009                             len,
1010                             tx_align,
1011                             rx_align);
1012                 if (ret)
1013                     return ret;
1014             }
1015         }
1016     }
1017 
1018     return 0;
1019 }
1020 EXPORT_SYMBOL_GPL(spi_test_run_test);
1021 
1022 /**
1023  * spi_test_run_tests - run an array of spi_messages tests
1024  * @spi: the spi device on which to run the tests
1025  * @tests: NULL-terminated array of @spi_test
1026  *
1027  * Returns: status errors as per @spi_test_run_test()
1028  */
1029 
1030 int spi_test_run_tests(struct spi_device *spi,
1031                struct spi_test *tests)
1032 {
1033     char *rx = NULL, *tx = NULL;
1034     int ret = 0, count = 0;
1035     struct spi_test *test;
1036 
1037     /* allocate rx/tx buffers of 128kB size without devm
1038      * in the hope that is on a page boundary
1039      */
1040     if (use_vmalloc)
1041         rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
1042     else
1043         rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
1044     if (!rx)
1045         return -ENOMEM;
1046 
1047 
1048     if (use_vmalloc)
1049         tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
1050     else
1051         tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
1052     if (!tx) {
1053         ret = -ENOMEM;
1054         goto err_tx;
1055     }
1056 
1057     /* now run the individual tests in the table */
1058     for (test = tests, count = 0; test->description[0];
1059          test++, count++) {
1060         /* only run test if requested */
1061         if ((run_only_test > -1) && (count != run_only_test))
1062             continue;
1063         /* run custom implementation */
1064         if (test->run_test)
1065             ret = test->run_test(spi, test, tx, rx);
1066         else
1067             ret = spi_test_run_test(spi, test, tx, rx);
1068         if (ret)
1069             goto out;
1070         /* add some delays so that we can easily
1071          * detect the individual tests when using a logic analyzer
1072          * we also add scheduling to avoid potential spi_timeouts...
1073          */
1074         mdelay(100);
1075         schedule();
1076     }
1077 
1078 out:
1079     kvfree(tx);
1080 err_tx:
1081     kvfree(rx);
1082     return ret;
1083 }
1084 EXPORT_SYMBOL_GPL(spi_test_run_tests);