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