Linux Audio

Check our new training course

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