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 */
26 static int simulate_only;
27 module_param(simulate_only, int, 0);
28 MODULE_PARM_DESC(simulate_only, "if not 0 do not execute the spi message");
29 
30 /* dump spi messages */
31 static int dump_messages;
32 module_param(dump_messages, int, 0);
33 MODULE_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 */
38 static int loopback;
39 module_param(loopback, int, 0);
40 MODULE_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 
45 static int loop_req;
46 module_param(loop_req, int, 0);
47 MODULE_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 
51 static int no_cs;
52 module_param(no_cs, int, 0);
53 MODULE_PARM_DESC(no_cs,
54 		 "if set Chip Select (CS) will not be used");
55 
56 /* run only a specific test */
57 static int run_only_test = -1;
58 module_param(run_only_test, int, 0);
59 MODULE_PARM_DESC(run_only_test,
60 		 "only run the test with this number (0-based !)");
61 
62 /* use vmalloc'ed buffers */
63 static int use_vmalloc;
64 module_param(use_vmalloc, int, 0644);
65 MODULE_PARM_DESC(use_vmalloc,
66 		 "use vmalloc'ed buffers instead of kmalloc'ed");
67 
68 /* check rx ranges */
69 static int check_ranges = 1;
70 module_param(check_ranges, int, 0644);
71 MODULE_PARM_DESC(check_ranges,
72 		 "checks rx_buffer pattern are valid");
73 
74 /* the actual tests to execute */
75 static 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 		.description	= "three tx+rx transfers with overlapping cache lines",
318 		.fill_option	= FILL_COUNT_8,
319 		/*
320 		 * This should be large enough for the controller driver to
321 		 * choose to transfer it with DMA.
322 		 */
323 		.iterate_len    = { 512, -1 },
324 		.iterate_transfer_mask = BIT(1),
325 		.transfer_count = 3,
326 		.transfers		= {
327 			{
328 				.len = 1,
329 				.tx_buf = TX(0),
330 				.rx_buf = RX(0),
331 			},
332 			{
333 				.tx_buf = TX(1),
334 				.rx_buf = RX(1),
335 			},
336 			{
337 				.len = 1,
338 				.tx_buf = TX(513),
339 				.rx_buf = RX(513),
340 			},
341 		},
342 	},
343 
344 	{ /* end of tests sequence */ }
345 };
346 
347 static int spi_loopback_test_probe(struct spi_device *spi)
348 {
349 	int ret;
350 
351 	if (loop_req || no_cs) {
352 		spi->mode |= loop_req ? SPI_LOOP : 0;
353 		spi->mode |= no_cs ? SPI_NO_CS : 0;
354 		ret = spi_setup(spi);
355 		if (ret) {
356 			dev_err(&spi->dev, "SPI setup with SPI_LOOP or SPI_NO_CS failed (%d)\n",
357 				ret);
358 			return ret;
359 		}
360 	}
361 
362 	dev_info(&spi->dev, "Executing spi-loopback-tests\n");
363 
364 	ret = spi_test_run_tests(spi, spi_tests);
365 
366 	dev_info(&spi->dev, "Finished spi-loopback-tests with return: %i\n",
367 		 ret);
368 
369 	return ret;
370 }
371 
372 /* non const match table to permit to change via a module parameter */
373 static struct of_device_id spi_loopback_test_of_match[] = {
374 	{ .compatible	= "linux,spi-loopback-test", },
375 	{ }
376 };
377 
378 /* allow to override the compatible string via a module_parameter */
379 module_param_string(compatible, spi_loopback_test_of_match[0].compatible,
380 		    sizeof(spi_loopback_test_of_match[0].compatible),
381 		    0000);
382 
383 MODULE_DEVICE_TABLE(of, spi_loopback_test_of_match);
384 
385 static struct spi_driver spi_loopback_test_driver = {
386 	.driver = {
387 		.name = "spi-loopback-test",
388 		.owner = THIS_MODULE,
389 		.of_match_table = spi_loopback_test_of_match,
390 	},
391 	.probe = spi_loopback_test_probe,
392 };
393 
394 module_spi_driver(spi_loopback_test_driver);
395 
396 MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
397 MODULE_DESCRIPTION("test spi_driver to check core functionality");
398 MODULE_LICENSE("GPL");
399 
400 /*-------------------------------------------------------------------------*/
401 
402 /* spi_test implementation */
403 
404 #define RANGE_CHECK(ptr, plen, start, slen) \
405 	((ptr >= start) && (ptr + plen <= start + slen))
406 
407 /* we allocate one page more, to allow for offsets */
408 #define SPI_TEST_MAX_SIZE_PLUS (SPI_TEST_MAX_SIZE + PAGE_SIZE)
409 
410 static void spi_test_print_hex_dump(char *pre, const void *ptr, size_t len)
411 {
412 	/* limit the hex_dump */
413 	if (len < 1024) {
414 		print_hex_dump(KERN_INFO, pre,
415 			       DUMP_PREFIX_OFFSET, 16, 1,
416 			       ptr, len, 0);
417 		return;
418 	}
419 	/* print head */
420 	print_hex_dump(KERN_INFO, pre,
421 		       DUMP_PREFIX_OFFSET, 16, 1,
422 		       ptr, 512, 0);
423 	/* print tail */
424 	pr_info("%s truncated - continuing at offset %04zx\n",
425 		pre, len - 512);
426 	print_hex_dump(KERN_INFO, pre,
427 		       DUMP_PREFIX_OFFSET, 16, 1,
428 		       ptr + (len - 512), 512, 0);
429 }
430 
431 static void spi_test_dump_message(struct spi_device *spi,
432 				  struct spi_message *msg,
433 				  bool dump_data)
434 {
435 	struct spi_transfer *xfer;
436 	int i;
437 	u8 b;
438 
439 	dev_info(&spi->dev, "  spi_msg@%pK\n", msg);
440 	if (msg->status)
441 		dev_info(&spi->dev, "    status:        %i\n",
442 			 msg->status);
443 	dev_info(&spi->dev, "    frame_length:  %i\n",
444 		 msg->frame_length);
445 	dev_info(&spi->dev, "    actual_length: %i\n",
446 		 msg->actual_length);
447 
448 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
449 		dev_info(&spi->dev, "    spi_transfer@%pK\n", xfer);
450 		dev_info(&spi->dev, "      len:    %i\n", xfer->len);
451 		dev_info(&spi->dev, "      tx_buf: %pK\n", xfer->tx_buf);
452 		if (dump_data && xfer->tx_buf)
453 			spi_test_print_hex_dump("          TX: ",
454 						xfer->tx_buf,
455 						xfer->len);
456 
457 		dev_info(&spi->dev, "      rx_buf: %pK\n", xfer->rx_buf);
458 		if (dump_data && xfer->rx_buf)
459 			spi_test_print_hex_dump("          RX: ",
460 						xfer->rx_buf,
461 						xfer->len);
462 		/* check for unwritten test pattern on rx_buf */
463 		if (xfer->rx_buf) {
464 			for (i = 0 ; i < xfer->len ; i++) {
465 				b = ((u8 *)xfer->rx_buf)[xfer->len - 1 - i];
466 				if (b != SPI_TEST_PATTERN_UNWRITTEN)
467 					break;
468 			}
469 			if (i)
470 				dev_info(&spi->dev,
471 					 "      rx_buf filled with %02x starts at offset: %i\n",
472 					 SPI_TEST_PATTERN_UNWRITTEN,
473 					 xfer->len - i);
474 		}
475 	}
476 }
477 
478 struct rx_ranges {
479 	struct list_head list;
480 	u8 *start;
481 	u8 *end;
482 };
483 
484 static int rx_ranges_cmp(void *priv, const struct list_head *a,
485 			 const struct list_head *b)
486 {
487 	struct rx_ranges *rx_a = list_entry(a, struct rx_ranges, list);
488 	struct rx_ranges *rx_b = list_entry(b, struct rx_ranges, list);
489 
490 	if (rx_a->start > rx_b->start)
491 		return 1;
492 	if (rx_a->start < rx_b->start)
493 		return -1;
494 	return 0;
495 }
496 
497 static int spi_check_rx_ranges(struct spi_device *spi,
498 			       struct spi_message *msg,
499 			       void *rx)
500 {
501 	struct spi_transfer *xfer;
502 	struct rx_ranges ranges[SPI_TEST_MAX_TRANSFERS], *r;
503 	int i = 0;
504 	LIST_HEAD(ranges_list);
505 	u8 *addr;
506 	int ret = 0;
507 
508 	/* loop over all transfers to fill in the rx_ranges */
509 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
510 		/* if there is no rx, then no check is needed */
511 		if (!xfer->rx_buf)
512 			continue;
513 		/* fill in the rx_range */
514 		if (RANGE_CHECK(xfer->rx_buf, xfer->len,
515 				rx, SPI_TEST_MAX_SIZE_PLUS)) {
516 			ranges[i].start = xfer->rx_buf;
517 			ranges[i].end = xfer->rx_buf + xfer->len;
518 			list_add(&ranges[i].list, &ranges_list);
519 			i++;
520 		}
521 	}
522 
523 	/* if no ranges, then we can return and avoid the checks...*/
524 	if (!i)
525 		return 0;
526 
527 	/* sort the list */
528 	list_sort(NULL, &ranges_list, rx_ranges_cmp);
529 
530 	/* and iterate over all the rx addresses */
531 	for (addr = rx; addr < (u8 *)rx + SPI_TEST_MAX_SIZE_PLUS; addr++) {
532 		/* if we are the DO not write pattern,
533 		 * then continue with the loop...
534 		 */
535 		if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
536 			continue;
537 
538 		/* check if we are inside a range */
539 		list_for_each_entry(r, &ranges_list, list) {
540 			/* if so then set to end... */
541 			if ((addr >= r->start) && (addr < r->end))
542 				addr = r->end;
543 		}
544 		/* second test after a (hopefull) translation */
545 		if (*addr == SPI_TEST_PATTERN_DO_NOT_WRITE)
546 			continue;
547 
548 		/* if still not found then something has modified too much */
549 		/* we could list the "closest" transfer here... */
550 		dev_err(&spi->dev,
551 			"loopback strangeness - rx changed outside of allowed range at: %pK\n",
552 			addr);
553 		/* do not return, only set ret,
554 		 * so that we list all addresses
555 		 */
556 		ret = -ERANGE;
557 	}
558 
559 	return ret;
560 }
561 
562 static int spi_test_check_elapsed_time(struct spi_device *spi,
563 				       struct spi_test *test)
564 {
565 	int i;
566 	unsigned long long estimated_time = 0;
567 	unsigned long long delay_usecs = 0;
568 
569 	for (i = 0; i < test->transfer_count; i++) {
570 		struct spi_transfer *xfer = test->transfers + i;
571 		unsigned long long nbits = (unsigned long long)BITS_PER_BYTE *
572 					   xfer->len;
573 
574 		delay_usecs += xfer->delay.value;
575 		if (!xfer->speed_hz)
576 			continue;
577 		estimated_time += div_u64(nbits * NSEC_PER_SEC, xfer->speed_hz);
578 	}
579 
580 	estimated_time += delay_usecs * NSEC_PER_USEC;
581 	if (test->elapsed_time < estimated_time) {
582 		dev_err(&spi->dev,
583 			"elapsed time %lld ns is shorter than minimum estimated time %lld ns\n",
584 			test->elapsed_time, estimated_time);
585 
586 		return -EINVAL;
587 	}
588 
589 	return 0;
590 }
591 
592 static int spi_test_check_loopback_result(struct spi_device *spi,
593 					  struct spi_message *msg,
594 					  void *tx, void *rx)
595 {
596 	struct spi_transfer *xfer;
597 	u8 rxb, txb;
598 	size_t i;
599 	int ret;
600 
601 	/* checks rx_buffer pattern are valid with loopback or without */
602 	if (check_ranges) {
603 		ret = spi_check_rx_ranges(spi, msg, rx);
604 		if (ret)
605 			return ret;
606 	}
607 
608 	/* if we run without loopback, then return now */
609 	if (!loopback)
610 		return 0;
611 
612 	/* if applicable to transfer check that rx_buf is equal to tx_buf */
613 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
614 		/* if there is no rx, then no check is needed */
615 		if (!xfer->len || !xfer->rx_buf)
616 			continue;
617 		/* so depending on tx_buf we need to handle things */
618 		if (xfer->tx_buf) {
619 			for (i = 0; i < xfer->len; i++) {
620 				txb = ((u8 *)xfer->tx_buf)[i];
621 				rxb = ((u8 *)xfer->rx_buf)[i];
622 				if (txb != rxb)
623 					goto mismatch_error;
624 			}
625 		} else {
626 			/* first byte received */
627 			txb = ((u8 *)xfer->rx_buf)[0];
628 			/* first byte may be 0 or xff */
629 			if (!((txb == 0) || (txb == 0xff))) {
630 				dev_err(&spi->dev,
631 					"loopback strangeness - we expect 0x00 or 0xff, but not 0x%02x\n",
632 					txb);
633 				return -EINVAL;
634 			}
635 			/* check that all bytes are identical */
636 			for (i = 1; i < xfer->len; i++) {
637 				rxb = ((u8 *)xfer->rx_buf)[i];
638 				if (rxb != txb)
639 					goto mismatch_error;
640 			}
641 		}
642 	}
643 
644 	return 0;
645 
646 mismatch_error:
647 	dev_err(&spi->dev,
648 		"loopback strangeness - transfer mismatch on byte %04zx - expected 0x%02x, but got 0x%02x\n",
649 		i, txb, rxb);
650 
651 	return -EINVAL;
652 }
653 
654 static int spi_test_translate(struct spi_device *spi,
655 			      void **ptr, size_t len,
656 			      void *tx, void *rx)
657 {
658 	size_t off;
659 
660 	/* return on null */
661 	if (!*ptr)
662 		return 0;
663 
664 	/* in the MAX_SIZE_HALF case modify the pointer */
665 	if (((size_t)*ptr) & SPI_TEST_MAX_SIZE_HALF)
666 		/* move the pointer to the correct range */
667 		*ptr += (SPI_TEST_MAX_SIZE_PLUS / 2) -
668 			SPI_TEST_MAX_SIZE_HALF;
669 
670 	/* RX range
671 	 * - we check against MAX_SIZE_PLUS to allow for automated alignment
672 	 */
673 	if (RANGE_CHECK(*ptr, len,  RX(0), SPI_TEST_MAX_SIZE_PLUS)) {
674 		off = *ptr - RX(0);
675 		*ptr = rx + off;
676 
677 		return 0;
678 	}
679 
680 	/* TX range */
681 	if (RANGE_CHECK(*ptr, len,  TX(0), SPI_TEST_MAX_SIZE_PLUS)) {
682 		off = *ptr - TX(0);
683 		*ptr = tx + off;
684 
685 		return 0;
686 	}
687 
688 	dev_err(&spi->dev,
689 		"PointerRange [%pK:%pK[ not in range [%pK:%pK[ or [%pK:%pK[\n",
690 		*ptr, *ptr + len,
691 		RX(0), RX(SPI_TEST_MAX_SIZE),
692 		TX(0), TX(SPI_TEST_MAX_SIZE));
693 
694 	return -EINVAL;
695 }
696 
697 static int spi_test_fill_pattern(struct spi_device *spi,
698 				 struct spi_test *test)
699 {
700 	struct spi_transfer *xfers = test->transfers;
701 	u8 *tx_buf;
702 	size_t count = 0;
703 	int i, j;
704 
705 #ifdef __BIG_ENDIAN
706 #define GET_VALUE_BYTE(value, index, bytes) \
707 	(value >> (8 * (bytes - 1 - count % bytes)))
708 #else
709 #define GET_VALUE_BYTE(value, index, bytes) \
710 	(value >> (8 * (count % bytes)))
711 #endif
712 
713 	/* fill all transfers with the pattern requested */
714 	for (i = 0; i < test->transfer_count; i++) {
715 		/* fill rx_buf with SPI_TEST_PATTERN_UNWRITTEN */
716 		if (xfers[i].rx_buf)
717 			memset(xfers[i].rx_buf, SPI_TEST_PATTERN_UNWRITTEN,
718 			       xfers[i].len);
719 		/* if tx_buf is NULL then skip */
720 		tx_buf = (u8 *)xfers[i].tx_buf;
721 		if (!tx_buf)
722 			continue;
723 		/* modify all the transfers */
724 		for (j = 0; j < xfers[i].len; j++, tx_buf++, count++) {
725 			/* fill tx */
726 			switch (test->fill_option) {
727 			case FILL_MEMSET_8:
728 				*tx_buf = test->fill_pattern;
729 				break;
730 			case FILL_MEMSET_16:
731 				*tx_buf = GET_VALUE_BYTE(test->fill_pattern,
732 							 count, 2);
733 				break;
734 			case FILL_MEMSET_24:
735 				*tx_buf = GET_VALUE_BYTE(test->fill_pattern,
736 							 count, 3);
737 				break;
738 			case FILL_MEMSET_32:
739 				*tx_buf = GET_VALUE_BYTE(test->fill_pattern,
740 							 count, 4);
741 				break;
742 			case FILL_COUNT_8:
743 				*tx_buf = count;
744 				break;
745 			case FILL_COUNT_16:
746 				*tx_buf = GET_VALUE_BYTE(count, count, 2);
747 				break;
748 			case FILL_COUNT_24:
749 				*tx_buf = GET_VALUE_BYTE(count, count, 3);
750 				break;
751 			case FILL_COUNT_32:
752 				*tx_buf = GET_VALUE_BYTE(count, count, 4);
753 				break;
754 			case FILL_TRANSFER_BYTE_8:
755 				*tx_buf = j;
756 				break;
757 			case FILL_TRANSFER_BYTE_16:
758 				*tx_buf = GET_VALUE_BYTE(j, j, 2);
759 				break;
760 			case FILL_TRANSFER_BYTE_24:
761 				*tx_buf = GET_VALUE_BYTE(j, j, 3);
762 				break;
763 			case FILL_TRANSFER_BYTE_32:
764 				*tx_buf = GET_VALUE_BYTE(j, j, 4);
765 				break;
766 			case FILL_TRANSFER_NUM:
767 				*tx_buf = i;
768 				break;
769 			default:
770 				dev_err(&spi->dev,
771 					"unsupported fill_option: %i\n",
772 					test->fill_option);
773 				return -EINVAL;
774 			}
775 		}
776 	}
777 
778 	return 0;
779 }
780 
781 static int _spi_test_run_iter(struct spi_device *spi,
782 			      struct spi_test *test,
783 			      void *tx, void *rx)
784 {
785 	struct spi_message *msg = &test->msg;
786 	struct spi_transfer *x;
787 	int i, ret;
788 
789 	/* initialize message - zero-filled via static initialization */
790 	spi_message_init_no_memset(msg);
791 
792 	/* fill rx with the DO_NOT_WRITE pattern */
793 	memset(rx, SPI_TEST_PATTERN_DO_NOT_WRITE, SPI_TEST_MAX_SIZE_PLUS);
794 
795 	/* add the individual transfers */
796 	for (i = 0; i < test->transfer_count; i++) {
797 		x = &test->transfers[i];
798 
799 		/* patch the values of tx_buf */
800 		ret = spi_test_translate(spi, (void **)&x->tx_buf, x->len,
801 					 (void *)tx, rx);
802 		if (ret)
803 			return ret;
804 
805 		/* patch the values of rx_buf */
806 		ret = spi_test_translate(spi, &x->rx_buf, x->len,
807 					 (void *)tx, rx);
808 		if (ret)
809 			return ret;
810 
811 		/* and add it to the list */
812 		spi_message_add_tail(x, msg);
813 	}
814 
815 	/* fill in the transfer buffers with pattern */
816 	ret = spi_test_fill_pattern(spi, test);
817 	if (ret)
818 		return ret;
819 
820 	/* and execute */
821 	if (test->execute_msg)
822 		ret = test->execute_msg(spi, test, tx, rx);
823 	else
824 		ret = spi_test_execute_msg(spi, test, tx, rx);
825 
826 	/* handle result */
827 	if (ret == test->expected_return)
828 		return 0;
829 
830 	dev_err(&spi->dev,
831 		"test failed - test returned %i, but we expect %i\n",
832 		ret, test->expected_return);
833 
834 	if (ret)
835 		return ret;
836 
837 	/* if it is 0, as we expected something else,
838 	 * then return something special
839 	 */
840 	return -EFAULT;
841 }
842 
843 static int spi_test_run_iter(struct spi_device *spi,
844 			     const struct spi_test *testtemplate,
845 			     void *tx, void *rx,
846 			     size_t len,
847 			     size_t tx_off,
848 			     size_t rx_off
849 	)
850 {
851 	struct spi_test test;
852 	int i, tx_count, rx_count;
853 
854 	/* copy the test template to test */
855 	memcpy(&test, testtemplate, sizeof(test));
856 
857 	/* if iterate_transfer_mask is not set,
858 	 * then set it to first transfer only
859 	 */
860 	if (!(test.iterate_transfer_mask & (BIT(test.transfer_count) - 1)))
861 		test.iterate_transfer_mask = 1;
862 
863 	/* count number of transfers with tx/rx_buf != NULL */
864 	rx_count = tx_count = 0;
865 	for (i = 0; i < test.transfer_count; i++) {
866 		if (test.transfers[i].tx_buf)
867 			tx_count++;
868 		if (test.transfers[i].rx_buf)
869 			rx_count++;
870 	}
871 
872 	/* in some iteration cases warn and exit early,
873 	 * as there is nothing to do, that has not been tested already...
874 	 */
875 	if (tx_off && (!tx_count)) {
876 		dev_warn_once(&spi->dev,
877 			      "%s: iterate_tx_off configured with tx_buf==NULL - ignoring\n",
878 			      test.description);
879 		return 0;
880 	}
881 	if (rx_off && (!rx_count)) {
882 		dev_warn_once(&spi->dev,
883 			      "%s: iterate_rx_off configured with rx_buf==NULL - ignoring\n",
884 			      test.description);
885 		return 0;
886 	}
887 
888 	/* write out info */
889 	if (!(len || tx_off || rx_off)) {
890 		dev_info(&spi->dev, "Running test %s\n", test.description);
891 	} else {
892 		dev_info(&spi->dev,
893 			 "  with iteration values: len = %zu, tx_off = %zu, rx_off = %zu\n",
894 			 len, tx_off, rx_off);
895 	}
896 
897 	/* update in the values from iteration values */
898 	for (i = 0; i < test.transfer_count; i++) {
899 		/* only when bit in transfer mask is set */
900 		if (!(test.iterate_transfer_mask & BIT(i)))
901 			continue;
902 		test.transfers[i].len = len;
903 		if (test.transfers[i].tx_buf)
904 			test.transfers[i].tx_buf += tx_off;
905 		if (test.transfers[i].rx_buf)
906 			test.transfers[i].rx_buf += rx_off;
907 	}
908 
909 	/* and execute */
910 	return _spi_test_run_iter(spi, &test, tx, rx);
911 }
912 
913 /**
914  * spi_test_execute_msg - default implementation to run a test
915  *
916  * @spi: @spi_device on which to run the @spi_message
917  * @test: the test to execute, which already contains @msg
918  * @tx:   the tx buffer allocated for the test sequence
919  * @rx:   the rx buffer allocated for the test sequence
920  *
921  * Returns: error code of spi_sync as well as basic error checking
922  */
923 int spi_test_execute_msg(struct spi_device *spi, struct spi_test *test,
924 			 void *tx, void *rx)
925 {
926 	struct spi_message *msg = &test->msg;
927 	int ret = 0;
928 	int i;
929 
930 	/* only if we do not simulate */
931 	if (!simulate_only) {
932 		ktime_t start;
933 
934 		/* dump the complete message before and after the transfer */
935 		if (dump_messages == 3)
936 			spi_test_dump_message(spi, msg, true);
937 
938 		start = ktime_get();
939 		/* run spi message */
940 		ret = spi_sync(spi, msg);
941 		test->elapsed_time = ktime_to_ns(ktime_sub(ktime_get(), start));
942 		if (ret == -ETIMEDOUT) {
943 			dev_info(&spi->dev,
944 				 "spi-message timed out - rerunning...\n");
945 			/* rerun after a few explicit schedules */
946 			for (i = 0; i < 16; i++)
947 				schedule();
948 			ret = spi_sync(spi, msg);
949 		}
950 		if (ret) {
951 			dev_err(&spi->dev,
952 				"Failed to execute spi_message: %i\n",
953 				ret);
954 			goto exit;
955 		}
956 
957 		/* do some extra error checks */
958 		if (msg->frame_length != msg->actual_length) {
959 			dev_err(&spi->dev,
960 				"actual length differs from expected\n");
961 			ret = -EIO;
962 			goto exit;
963 		}
964 
965 		/* run rx-buffer tests */
966 		ret = spi_test_check_loopback_result(spi, msg, tx, rx);
967 		if (ret)
968 			goto exit;
969 
970 		ret = spi_test_check_elapsed_time(spi, test);
971 	}
972 
973 	/* if requested or on error dump message (including data) */
974 exit:
975 	if (dump_messages || ret)
976 		spi_test_dump_message(spi, msg,
977 				      (dump_messages >= 2) || (ret));
978 
979 	return ret;
980 }
981 EXPORT_SYMBOL_GPL(spi_test_execute_msg);
982 
983 /**
984  * spi_test_run_test - run an individual spi_test
985  *                     including all the relevant iterations on:
986  *                     length and buffer alignment
987  *
988  * @spi:  the spi_device to send the messages to
989  * @test: the test which we need to execute
990  * @tx:   the tx buffer allocated for the test sequence
991  * @rx:   the rx buffer allocated for the test sequence
992  *
993  * Returns: status code of spi_sync or other failures
994  */
995 
996 int spi_test_run_test(struct spi_device *spi, const struct spi_test *test,
997 		      void *tx, void *rx)
998 {
999 	int idx_len;
1000 	size_t len;
1001 	size_t tx_align, rx_align;
1002 	int ret;
1003 
1004 	/* test for transfer limits */
1005 	if (test->transfer_count >= SPI_TEST_MAX_TRANSFERS) {
1006 		dev_err(&spi->dev,
1007 			"%s: Exceeded max number of transfers with %i\n",
1008 			test->description, test->transfer_count);
1009 		return -E2BIG;
1010 	}
1011 
1012 	/* setting up some values in spi_message
1013 	 * based on some settings in spi_master
1014 	 * some of this can also get done in the run() method
1015 	 */
1016 
1017 	/* iterate over all the iterable values using macros
1018 	 * (to make it a bit more readable...
1019 	 */
1020 #define FOR_EACH_ALIGNMENT(var)						\
1021 	for (var = 0;							\
1022 	    var < (test->iterate_##var ?				\
1023 			(spi->master->dma_alignment ?			\
1024 			 spi->master->dma_alignment :			\
1025 			 test->iterate_##var) :				\
1026 			1);						\
1027 	    var++)
1028 
1029 	for (idx_len = 0; idx_len < SPI_TEST_MAX_ITERATE &&
1030 	     (len = test->iterate_len[idx_len]) != -1; idx_len++) {
1031 		FOR_EACH_ALIGNMENT(tx_align) {
1032 			FOR_EACH_ALIGNMENT(rx_align) {
1033 				/* and run the iteration */
1034 				ret = spi_test_run_iter(spi, test,
1035 							tx, rx,
1036 							len,
1037 							tx_align,
1038 							rx_align);
1039 				if (ret)
1040 					return ret;
1041 			}
1042 		}
1043 	}
1044 
1045 	return 0;
1046 }
1047 EXPORT_SYMBOL_GPL(spi_test_run_test);
1048 
1049 /**
1050  * spi_test_run_tests - run an array of spi_messages tests
1051  * @spi: the spi device on which to run the tests
1052  * @tests: NULL-terminated array of @spi_test
1053  *
1054  * Returns: status errors as per @spi_test_run_test()
1055  */
1056 
1057 int spi_test_run_tests(struct spi_device *spi,
1058 		       struct spi_test *tests)
1059 {
1060 	char *rx = NULL, *tx = NULL;
1061 	int ret = 0, count = 0;
1062 	struct spi_test *test;
1063 
1064 	/* allocate rx/tx buffers of 128kB size without devm
1065 	 * in the hope that is on a page boundary
1066 	 */
1067 	if (use_vmalloc)
1068 		rx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
1069 	else
1070 		rx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
1071 	if (!rx)
1072 		return -ENOMEM;
1073 
1074 
1075 	if (use_vmalloc)
1076 		tx = vmalloc(SPI_TEST_MAX_SIZE_PLUS);
1077 	else
1078 		tx = kzalloc(SPI_TEST_MAX_SIZE_PLUS, GFP_KERNEL);
1079 	if (!tx) {
1080 		ret = -ENOMEM;
1081 		goto err_tx;
1082 	}
1083 
1084 	/* now run the individual tests in the table */
1085 	for (test = tests, count = 0; test->description[0];
1086 	     test++, count++) {
1087 		/* only run test if requested */
1088 		if ((run_only_test > -1) && (count != run_only_test))
1089 			continue;
1090 		/* run custom implementation */
1091 		if (test->run_test)
1092 			ret = test->run_test(spi, test, tx, rx);
1093 		else
1094 			ret = spi_test_run_test(spi, test, tx, rx);
1095 		if (ret)
1096 			goto out;
1097 		/* add some delays so that we can easily
1098 		 * detect the individual tests when using a logic analyzer
1099 		 * we also add scheduling to avoid potential spi_timeouts...
1100 		 */
1101 		mdelay(100);
1102 		schedule();
1103 	}
1104 
1105 out:
1106 	kvfree(tx);
1107 err_tx:
1108 	kvfree(rx);
1109 	return ret;
1110 }
1111 EXPORT_SYMBOL_GPL(spi_test_run_tests);
1112