1 // SPDX-License-Identifier: GPL-2.0
3 * DMA traffic test driver
5 * Copyright (C) 2020, Intel Corporation
6 * Authors: Isaac Hazan <isaac.hazan@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 #include <linux/completion.h>
11 #include <linux/debugfs.h>
12 #include <linux/module.h>
13 #include <linux/sizes.h>
14 #include <linux/thunderbolt.h>
16 #define DMA_TEST_HOPID 8
17 #define DMA_TEST_TX_RING_SIZE 64
18 #define DMA_TEST_RX_RING_SIZE 256
19 #define DMA_TEST_FRAME_SIZE SZ_4K
20 #define DMA_TEST_DATA_PATTERN 0x0123456789abcdefLL
21 #define DMA_TEST_MAX_PACKETS 1000
23 enum dma_test_frame_pdf {
24 DMA_TEST_PDF_FRAME_START = 1,
25 DMA_TEST_PDF_FRAME_END,
28 struct dma_test_frame {
29 struct dma_test *dma_test;
31 struct ring_frame frame;
34 enum dma_test_test_error {
37 DMA_TEST_BUFFER_ERROR,
39 DMA_TEST_CONFIG_ERROR,
42 DMA_TEST_BONDING_ERROR,
43 DMA_TEST_PACKET_ERROR,
46 static const char * const dma_test_error_names[] = {
47 [DMA_TEST_NO_ERROR] = "no errors",
48 [DMA_TEST_INTERRUPTED] = "interrupted by signal",
49 [DMA_TEST_BUFFER_ERROR] = "no memory for packet buffers",
50 [DMA_TEST_DMA_ERROR] = "DMA ring setup failed",
51 [DMA_TEST_CONFIG_ERROR] = "configuration is not valid",
52 [DMA_TEST_SPEED_ERROR] = "unexpected link speed",
53 [DMA_TEST_WIDTH_ERROR] = "unexpected link width",
54 [DMA_TEST_BONDING_ERROR] = "lane bonding configuration error",
55 [DMA_TEST_PACKET_ERROR] = "packet check failed",
58 enum dma_test_result {
64 static const char * const dma_test_result_names[] = {
65 [DMA_TEST_NOT_RUN] = "not run",
66 [DMA_TEST_SUCCESS] = "success",
67 [DMA_TEST_FAIL] = "failed",
71 * struct dma_test - DMA test device driver private data
72 * @svc: XDomain service the driver is bound to
73 * @xd: XDomain the service belongs to
74 * @rx_ring: Software ring holding RX frames
75 * @tx_ring: Software ring holding TX frames
76 * @packets_to_send: Number of packets to send
77 * @packets_to_receive: Number of packets to receive
78 * @packets_sent: Actual number of packets sent
79 * @packets_received: Actual number of packets received
80 * @link_speed: Expected link speed (Gb/s), %0 to use whatever is negotiated
81 * @link_width: Expected link width (Gb/s), %0 to use whatever is negotiated
82 * @crc_errors: Number of CRC errors during the test run
83 * @buffer_overflow_errors: Number of buffer overflow errors during the test
85 * @result: Result of the last run
86 * @error_code: Error code of the last run
87 * @complete: Used to wait for the Rx to complete
88 * @lock: Lock serializing access to this structure
89 * @debugfs_dir: dentry of this dma_test
92 const struct tb_service *svc;
93 struct tb_xdomain *xd;
94 struct tb_ring *rx_ring;
95 struct tb_ring *tx_ring;
96 unsigned int packets_to_send;
97 unsigned int packets_to_receive;
98 unsigned int packets_sent;
99 unsigned int packets_received;
100 unsigned int link_speed;
101 unsigned int link_width;
102 unsigned int crc_errors;
103 unsigned int buffer_overflow_errors;
104 enum dma_test_result result;
105 enum dma_test_test_error error_code;
106 struct completion complete;
108 struct dentry *debugfs_dir;
111 /* DMA test property directory UUID: 3188cd10-6523-4a5a-a682-fdca07a248d8 */
112 static const uuid_t dma_test_dir_uuid =
113 UUID_INIT(0x3188cd10, 0x6523, 0x4a5a,
114 0xa6, 0x82, 0xfd, 0xca, 0x07, 0xa2, 0x48, 0xd8);
116 static struct tb_property_dir *dma_test_dir;
117 static void *dma_test_pattern;
119 static void dma_test_free_rings(struct dma_test *dt)
122 tb_ring_free(dt->rx_ring);
126 tb_ring_free(dt->tx_ring);
131 static int dma_test_start_rings(struct dma_test *dt)
133 unsigned int flags = RING_FLAG_FRAME;
134 struct tb_xdomain *xd = dt->xd;
135 int ret, e2e_tx_hop = 0;
136 struct tb_ring *ring;
139 * If we are both sender and receiver (traffic goes over a
140 * special loopback dongle) enable E2E flow control. This avoids
143 if (dt->packets_to_send && dt->packets_to_receive)
144 flags |= RING_FLAG_E2E;
146 if (dt->packets_to_send) {
147 ring = tb_ring_alloc_tx(xd->tb->nhi, -1, DMA_TEST_TX_RING_SIZE,
153 e2e_tx_hop = ring->hop;
156 if (dt->packets_to_receive) {
157 u16 sof_mask, eof_mask;
159 sof_mask = BIT(DMA_TEST_PDF_FRAME_START);
160 eof_mask = BIT(DMA_TEST_PDF_FRAME_END);
162 ring = tb_ring_alloc_rx(xd->tb->nhi, -1, DMA_TEST_RX_RING_SIZE,
163 flags, e2e_tx_hop, sof_mask, eof_mask,
166 dma_test_free_rings(dt);
173 ret = tb_xdomain_enable_paths(dt->xd, DMA_TEST_HOPID,
174 dt->tx_ring ? dt->tx_ring->hop : 0,
176 dt->rx_ring ? dt->rx_ring->hop : 0);
178 dma_test_free_rings(dt);
183 tb_ring_start(dt->tx_ring);
185 tb_ring_start(dt->rx_ring);
190 static void dma_test_stop_rings(struct dma_test *dt)
193 tb_ring_stop(dt->rx_ring);
195 tb_ring_stop(dt->tx_ring);
197 if (tb_xdomain_disable_paths(dt->xd))
198 dev_warn(&dt->svc->dev, "failed to disable DMA paths\n");
200 dma_test_free_rings(dt);
203 static void dma_test_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
206 struct dma_test_frame *tf = container_of(frame, typeof(*tf), frame);
207 struct dma_test *dt = tf->dma_test;
208 struct device *dma_dev = tb_ring_dma_device(dt->rx_ring);
210 dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
219 dt->packets_received++;
220 dev_dbg(&dt->svc->dev, "packet %u/%u received\n", dt->packets_received,
221 dt->packets_to_receive);
223 if (tf->frame.flags & RING_DESC_CRC_ERROR)
225 if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN)
226 dt->buffer_overflow_errors++;
230 if (dt->packets_received == dt->packets_to_receive)
231 complete(&dt->complete);
234 static int dma_test_submit_rx(struct dma_test *dt, size_t npackets)
236 struct device *dma_dev = tb_ring_dma_device(dt->rx_ring);
239 for (i = 0; i < npackets; i++) {
240 struct dma_test_frame *tf;
243 tf = kzalloc(sizeof(*tf), GFP_KERNEL);
247 tf->data = kzalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
253 dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
255 if (dma_mapping_error(dma_dev, dma_addr)) {
261 tf->frame.buffer_phy = dma_addr;
262 tf->frame.callback = dma_test_rx_callback;
264 INIT_LIST_HEAD(&tf->frame.list);
266 tb_ring_rx(dt->rx_ring, &tf->frame);
272 static void dma_test_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
275 struct dma_test_frame *tf = container_of(frame, typeof(*tf), frame);
276 struct dma_test *dt = tf->dma_test;
277 struct device *dma_dev = tb_ring_dma_device(dt->tx_ring);
279 dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
285 static int dma_test_submit_tx(struct dma_test *dt, size_t npackets)
287 struct device *dma_dev = tb_ring_dma_device(dt->tx_ring);
290 for (i = 0; i < npackets; i++) {
291 struct dma_test_frame *tf;
294 tf = kzalloc(sizeof(*tf), GFP_KERNEL);
298 tf->frame.size = 0; /* means 4096 */
301 tf->data = kmemdup(dma_test_pattern, DMA_TEST_FRAME_SIZE, GFP_KERNEL);
307 dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
309 if (dma_mapping_error(dma_dev, dma_addr)) {
315 tf->frame.buffer_phy = dma_addr;
316 tf->frame.callback = dma_test_tx_callback;
317 tf->frame.sof = DMA_TEST_PDF_FRAME_START;
318 tf->frame.eof = DMA_TEST_PDF_FRAME_END;
319 INIT_LIST_HEAD(&tf->frame.list);
322 dev_dbg(&dt->svc->dev, "packet %u/%u sent\n", dt->packets_sent,
323 dt->packets_to_send);
325 tb_ring_tx(dt->tx_ring, &tf->frame);
331 #define DMA_TEST_DEBUGFS_ATTR(__fops, __get, __validate, __set) \
332 static int __fops ## _show(void *data, u64 *val) \
334 struct tb_service *svc = data; \
335 struct dma_test *dt = tb_service_get_drvdata(svc); \
338 ret = mutex_lock_interruptible(&dt->lock); \
342 mutex_unlock(&dt->lock); \
345 static int __fops ## _store(void *data, u64 val) \
347 struct tb_service *svc = data; \
348 struct dma_test *dt = tb_service_get_drvdata(svc); \
351 ret = __validate(val); \
354 ret = mutex_lock_interruptible(&dt->lock); \
358 mutex_unlock(&dt->lock); \
361 DEFINE_DEBUGFS_ATTRIBUTE(__fops ## _fops, __fops ## _show, \
362 __fops ## _store, "%llu\n")
364 static void lanes_get(const struct dma_test *dt, u64 *val)
366 *val = dt->link_width;
369 static int lanes_validate(u64 val)
371 return val > 2 ? -EINVAL : 0;
374 static void lanes_set(struct dma_test *dt, u64 val)
376 dt->link_width = val;
378 DMA_TEST_DEBUGFS_ATTR(lanes, lanes_get, lanes_validate, lanes_set);
380 static void speed_get(const struct dma_test *dt, u64 *val)
382 *val = dt->link_speed;
385 static int speed_validate(u64 val)
397 static void speed_set(struct dma_test *dt, u64 val)
399 dt->link_speed = val;
401 DMA_TEST_DEBUGFS_ATTR(speed, speed_get, speed_validate, speed_set);
403 static void packets_to_receive_get(const struct dma_test *dt, u64 *val)
405 *val = dt->packets_to_receive;
408 static int packets_to_receive_validate(u64 val)
410 return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
413 static void packets_to_receive_set(struct dma_test *dt, u64 val)
415 dt->packets_to_receive = val;
417 DMA_TEST_DEBUGFS_ATTR(packets_to_receive, packets_to_receive_get,
418 packets_to_receive_validate, packets_to_receive_set);
420 static void packets_to_send_get(const struct dma_test *dt, u64 *val)
422 *val = dt->packets_to_send;
425 static int packets_to_send_validate(u64 val)
427 return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
430 static void packets_to_send_set(struct dma_test *dt, u64 val)
432 dt->packets_to_send = val;
434 DMA_TEST_DEBUGFS_ATTR(packets_to_send, packets_to_send_get,
435 packets_to_send_validate, packets_to_send_set);
437 static int dma_test_set_bonding(struct dma_test *dt)
439 switch (dt->link_width) {
441 return tb_xdomain_lane_bonding_enable(dt->xd);
443 tb_xdomain_lane_bonding_disable(dt->xd);
450 static bool dma_test_validate_config(struct dma_test *dt)
452 if (!dt->packets_to_send && !dt->packets_to_receive)
454 if (dt->packets_to_send && dt->packets_to_receive &&
455 dt->packets_to_send != dt->packets_to_receive)
460 static void dma_test_check_errors(struct dma_test *dt, int ret)
462 if (!dt->error_code) {
463 if (dt->link_speed && dt->xd->link_speed != dt->link_speed) {
464 dt->error_code = DMA_TEST_SPEED_ERROR;
465 } else if (dt->link_width &&
466 dt->xd->link_width != dt->link_width) {
467 dt->error_code = DMA_TEST_WIDTH_ERROR;
468 } else if (dt->packets_to_send != dt->packets_sent ||
469 dt->packets_to_receive != dt->packets_received ||
470 dt->crc_errors || dt->buffer_overflow_errors) {
471 dt->error_code = DMA_TEST_PACKET_ERROR;
477 dt->result = DMA_TEST_FAIL;
480 static int test_store(void *data, u64 val)
482 struct tb_service *svc = data;
483 struct dma_test *dt = tb_service_get_drvdata(svc);
489 ret = mutex_lock_interruptible(&dt->lock);
493 dt->packets_sent = 0;
494 dt->packets_received = 0;
496 dt->buffer_overflow_errors = 0;
497 dt->result = DMA_TEST_SUCCESS;
498 dt->error_code = DMA_TEST_NO_ERROR;
500 dev_dbg(&svc->dev, "DMA test starting\n");
502 dev_dbg(&svc->dev, "link_speed: %u Gb/s\n", dt->link_speed);
504 dev_dbg(&svc->dev, "link_width: %u\n", dt->link_width);
505 dev_dbg(&svc->dev, "packets_to_send: %u\n", dt->packets_to_send);
506 dev_dbg(&svc->dev, "packets_to_receive: %u\n", dt->packets_to_receive);
508 if (!dma_test_validate_config(dt)) {
509 dev_err(&svc->dev, "invalid test configuration\n");
510 dt->error_code = DMA_TEST_CONFIG_ERROR;
514 ret = dma_test_set_bonding(dt);
516 dev_err(&svc->dev, "failed to set lanes\n");
517 dt->error_code = DMA_TEST_BONDING_ERROR;
521 ret = dma_test_start_rings(dt);
523 dev_err(&svc->dev, "failed to enable DMA rings\n");
524 dt->error_code = DMA_TEST_DMA_ERROR;
528 if (dt->packets_to_receive) {
529 reinit_completion(&dt->complete);
530 ret = dma_test_submit_rx(dt, dt->packets_to_receive);
532 dev_err(&svc->dev, "failed to submit receive buffers\n");
533 dt->error_code = DMA_TEST_BUFFER_ERROR;
538 if (dt->packets_to_send) {
539 ret = dma_test_submit_tx(dt, dt->packets_to_send);
541 dev_err(&svc->dev, "failed to submit transmit buffers\n");
542 dt->error_code = DMA_TEST_BUFFER_ERROR;
547 if (dt->packets_to_receive) {
548 ret = wait_for_completion_interruptible(&dt->complete);
550 dt->error_code = DMA_TEST_INTERRUPTED;
556 dma_test_stop_rings(dt);
558 dma_test_check_errors(dt, ret);
559 mutex_unlock(&dt->lock);
561 dev_dbg(&svc->dev, "DMA test %s\n", dma_test_result_names[dt->result]);
564 DEFINE_DEBUGFS_ATTRIBUTE(test_fops, NULL, test_store, "%llu\n");
566 static int status_show(struct seq_file *s, void *not_used)
568 struct tb_service *svc = s->private;
569 struct dma_test *dt = tb_service_get_drvdata(svc);
572 ret = mutex_lock_interruptible(&dt->lock);
576 seq_printf(s, "result: %s\n", dma_test_result_names[dt->result]);
577 if (dt->result == DMA_TEST_NOT_RUN)
580 seq_printf(s, "packets received: %u\n", dt->packets_received);
581 seq_printf(s, "packets sent: %u\n", dt->packets_sent);
582 seq_printf(s, "CRC errors: %u\n", dt->crc_errors);
583 seq_printf(s, "buffer overflow errors: %u\n",
584 dt->buffer_overflow_errors);
585 seq_printf(s, "error: %s\n", dma_test_error_names[dt->error_code]);
588 mutex_unlock(&dt->lock);
591 DEFINE_SHOW_ATTRIBUTE(status);
593 static void dma_test_debugfs_init(struct tb_service *svc)
595 struct dma_test *dt = tb_service_get_drvdata(svc);
597 dt->debugfs_dir = debugfs_create_dir("dma_test", svc->debugfs_dir);
599 debugfs_create_file("lanes", 0600, dt->debugfs_dir, svc, &lanes_fops);
600 debugfs_create_file("speed", 0600, dt->debugfs_dir, svc, &speed_fops);
601 debugfs_create_file("packets_to_receive", 0600, dt->debugfs_dir, svc,
602 &packets_to_receive_fops);
603 debugfs_create_file("packets_to_send", 0600, dt->debugfs_dir, svc,
604 &packets_to_send_fops);
605 debugfs_create_file("status", 0400, dt->debugfs_dir, svc, &status_fops);
606 debugfs_create_file("test", 0200, dt->debugfs_dir, svc, &test_fops);
609 static int dma_test_probe(struct tb_service *svc, const struct tb_service_id *id)
611 struct tb_xdomain *xd = tb_service_parent(svc);
614 dt = devm_kzalloc(&svc->dev, sizeof(*dt), GFP_KERNEL);
620 mutex_init(&dt->lock);
621 init_completion(&dt->complete);
623 tb_service_set_drvdata(svc, dt);
624 dma_test_debugfs_init(svc);
629 static void dma_test_remove(struct tb_service *svc)
631 struct dma_test *dt = tb_service_get_drvdata(svc);
633 mutex_lock(&dt->lock);
634 debugfs_remove_recursive(dt->debugfs_dir);
635 mutex_unlock(&dt->lock);
638 static int __maybe_unused dma_test_suspend(struct device *dev)
641 * No need to do anything special here. If userspace is writing
642 * to the test attribute when suspend started, it comes out from
643 * wait_for_completion_interruptible() with -ERESTARTSYS and the
644 * DMA test fails tearing down the rings. Once userspace is
645 * thawed the kernel restarts the write syscall effectively
646 * re-running the test.
651 static int __maybe_unused dma_test_resume(struct device *dev)
656 static const struct dev_pm_ops dma_test_pm_ops = {
657 SET_SYSTEM_SLEEP_PM_OPS(dma_test_suspend, dma_test_resume)
660 static const struct tb_service_id dma_test_ids[] = {
661 { TB_SERVICE("dma_test", 1) },
664 MODULE_DEVICE_TABLE(tbsvc, dma_test_ids);
666 static struct tb_service_driver dma_test_driver = {
668 .owner = THIS_MODULE,
669 .name = "thunderbolt_dma_test",
670 .pm = &dma_test_pm_ops,
672 .probe = dma_test_probe,
673 .remove = dma_test_remove,
674 .id_table = dma_test_ids,
677 static int __init dma_test_init(void)
679 u64 data_value = DMA_TEST_DATA_PATTERN;
682 dma_test_pattern = kmalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
683 if (!dma_test_pattern)
686 for (i = 0; i < DMA_TEST_FRAME_SIZE / sizeof(data_value); i++)
687 ((u32 *)dma_test_pattern)[i] = data_value++;
689 dma_test_dir = tb_property_create_dir(&dma_test_dir_uuid);
692 goto err_free_pattern;
695 tb_property_add_immediate(dma_test_dir, "prtcid", 1);
696 tb_property_add_immediate(dma_test_dir, "prtcvers", 1);
697 tb_property_add_immediate(dma_test_dir, "prtcrevs", 0);
698 tb_property_add_immediate(dma_test_dir, "prtcstns", 0);
700 ret = tb_register_property_dir("dma_test", dma_test_dir);
704 ret = tb_register_service_driver(&dma_test_driver);
706 goto err_unregister_dir;
711 tb_unregister_property_dir("dma_test", dma_test_dir);
713 tb_property_free_dir(dma_test_dir);
715 kfree(dma_test_pattern);
719 module_init(dma_test_init);
721 static void __exit dma_test_exit(void)
723 tb_unregister_service_driver(&dma_test_driver);
724 tb_unregister_property_dir("dma_test", dma_test_dir);
725 tb_property_free_dir(dma_test_dir);
726 kfree(dma_test_pattern);
728 module_exit(dma_test_exit);
730 MODULE_AUTHOR("Isaac Hazan <isaac.hazan@intel.com>");
731 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
732 MODULE_DESCRIPTION("DMA traffic test driver");
733 MODULE_LICENSE("GPL v2");