xref: /linux/drivers/thunderbolt/dma_test.c (revision c51777370ac2ef435401340e205ef1d0c778df28)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DMA traffic test driver
4  *
5  * Copyright (C) 2020, Intel Corporation
6  * Authors: Isaac Hazan <isaac.hazan@intel.com>
7  *	    Mika Westerberg <mika.westerberg@linux.intel.com>
8  */
9 
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>
15 
16 #define DMA_TEST_TX_RING_SIZE		64
17 #define DMA_TEST_RX_RING_SIZE		256
18 #define DMA_TEST_FRAME_SIZE		SZ_4K
19 #define DMA_TEST_DATA_PATTERN		0x0123456789abcdefLL
20 #define DMA_TEST_MAX_PACKETS		1000
21 
22 enum dma_test_frame_pdf {
23 	DMA_TEST_PDF_FRAME_START = 1,
24 	DMA_TEST_PDF_FRAME_END,
25 };
26 
27 struct dma_test_frame {
28 	struct dma_test *dma_test;
29 	void *data;
30 	struct ring_frame frame;
31 };
32 
33 enum dma_test_test_error {
34 	DMA_TEST_NO_ERROR,
35 	DMA_TEST_INTERRUPTED,
36 	DMA_TEST_BUFFER_ERROR,
37 	DMA_TEST_DMA_ERROR,
38 	DMA_TEST_CONFIG_ERROR,
39 	DMA_TEST_SPEED_ERROR,
40 	DMA_TEST_WIDTH_ERROR,
41 	DMA_TEST_BONDING_ERROR,
42 	DMA_TEST_PACKET_ERROR,
43 };
44 
45 static const char * const dma_test_error_names[] = {
46 	[DMA_TEST_NO_ERROR] = "no errors",
47 	[DMA_TEST_INTERRUPTED] = "interrupted by signal",
48 	[DMA_TEST_BUFFER_ERROR] = "no memory for packet buffers",
49 	[DMA_TEST_DMA_ERROR] = "DMA ring setup failed",
50 	[DMA_TEST_CONFIG_ERROR] = "configuration is not valid",
51 	[DMA_TEST_SPEED_ERROR] = "unexpected link speed",
52 	[DMA_TEST_WIDTH_ERROR] = "unexpected link width",
53 	[DMA_TEST_BONDING_ERROR] = "lane bonding configuration error",
54 	[DMA_TEST_PACKET_ERROR] = "packet check failed",
55 };
56 
57 enum dma_test_result {
58 	DMA_TEST_NOT_RUN,
59 	DMA_TEST_SUCCESS,
60 	DMA_TEST_FAIL,
61 };
62 
63 static const char * const dma_test_result_names[] = {
64 	[DMA_TEST_NOT_RUN] = "not run",
65 	[DMA_TEST_SUCCESS] = "success",
66 	[DMA_TEST_FAIL] = "failed",
67 };
68 
69 /**
70  * struct dma_test - DMA test device driver private data
71  * @svc: XDomain service the driver is bound to
72  * @xd: XDomain the service belongs to
73  * @rx_ring: Software ring holding RX frames
74  * @rx_hopid: HopID used for receiving frames
75  * @tx_ring: Software ring holding TX frames
76  * @tx_hopid: HopID used for sending fames
77  * @packets_to_send: Number of packets to send
78  * @packets_to_receive: Number of packets to receive
79  * @packets_sent: Actual number of packets sent
80  * @packets_received: Actual number of packets received
81  * @link_speed: Expected link speed (Gb/s), %0 to use whatever is negotiated
82  * @link_width: Expected link width (Gb/s), %0 to use whatever is negotiated
83  * @crc_errors: Number of CRC errors during the test run
84  * @buffer_overflow_errors: Number of buffer overflow errors during the test
85  *			    run
86  * @result: Result of the last run
87  * @error_code: Error code of the last run
88  * @complete: Used to wait for the Rx to complete
89  * @lock: Lock serializing access to this structure
90  */
91 struct dma_test {
92 	const struct tb_service *svc;
93 	struct tb_xdomain *xd;
94 	struct tb_ring *rx_ring;
95 	int rx_hopid;
96 	struct tb_ring *tx_ring;
97 	int tx_hopid;
98 	unsigned int packets_to_send;
99 	unsigned int packets_to_receive;
100 	unsigned int packets_sent;
101 	unsigned int packets_received;
102 	unsigned int link_speed;
103 	enum tb_link_width link_width;
104 	unsigned int crc_errors;
105 	unsigned int buffer_overflow_errors;
106 	enum dma_test_result result;
107 	enum dma_test_test_error error_code;
108 	struct completion complete;
109 	struct mutex lock;
110 };
111 
112 /* DMA test property directory UUID: 3188cd10-6523-4a5a-a682-fdca07a248d8 */
113 static const uuid_t dma_test_dir_uuid =
114 	UUID_INIT(0x3188cd10, 0x6523, 0x4a5a,
115 		  0xa6, 0x82, 0xfd, 0xca, 0x07, 0xa2, 0x48, 0xd8);
116 
117 static struct tb_property_dir *dma_test_dir;
118 static void *dma_test_pattern;
119 
120 static void dma_test_free_rings(struct dma_test *dt)
121 {
122 	if (dt->rx_ring) {
123 		tb_xdomain_release_in_hopid(dt->xd, dt->rx_hopid);
124 		tb_ring_free(dt->rx_ring);
125 		dt->rx_ring = NULL;
126 	}
127 	if (dt->tx_ring) {
128 		tb_xdomain_release_out_hopid(dt->xd, dt->tx_hopid);
129 		tb_ring_free(dt->tx_ring);
130 		dt->tx_ring = NULL;
131 	}
132 }
133 
134 static int dma_test_start_rings(struct dma_test *dt)
135 {
136 	unsigned int flags = RING_FLAG_FRAME;
137 	struct tb_xdomain *xd = dt->xd;
138 	int ret, e2e_tx_hop = 0;
139 	struct tb_ring *ring;
140 
141 	/*
142 	 * If we are both sender and receiver (traffic goes over a
143 	 * special loopback dongle) enable E2E flow control. This avoids
144 	 * losing packets.
145 	 */
146 	if (dt->packets_to_send && dt->packets_to_receive)
147 		flags |= RING_FLAG_E2E;
148 
149 	if (dt->packets_to_send) {
150 		ring = tb_ring_alloc_tx(xd->tb->nhi, -1, DMA_TEST_TX_RING_SIZE,
151 					flags);
152 		if (!ring)
153 			return -ENOMEM;
154 
155 		dt->tx_ring = ring;
156 		e2e_tx_hop = ring->hop;
157 
158 		tb_ring_throttling(ring, 128000);
159 
160 		ret = tb_xdomain_alloc_out_hopid(xd, -1);
161 		if (ret < 0) {
162 			dma_test_free_rings(dt);
163 			return ret;
164 		}
165 
166 		dt->tx_hopid = ret;
167 
168 	}
169 
170 	if (dt->packets_to_receive) {
171 		u16 sof_mask, eof_mask;
172 
173 		sof_mask = BIT(DMA_TEST_PDF_FRAME_START);
174 		eof_mask = BIT(DMA_TEST_PDF_FRAME_END);
175 
176 		ring = tb_ring_alloc_rx(xd->tb->nhi, -1, DMA_TEST_RX_RING_SIZE,
177 					flags, e2e_tx_hop, sof_mask, eof_mask,
178 					NULL, NULL);
179 		if (!ring) {
180 			dma_test_free_rings(dt);
181 			return -ENOMEM;
182 		}
183 
184 		dt->rx_ring = ring;
185 
186 		tb_ring_throttling(ring, 128000);
187 
188 		ret = tb_xdomain_alloc_in_hopid(xd, -1);
189 		if (ret < 0) {
190 			dma_test_free_rings(dt);
191 			return ret;
192 		}
193 
194 		dt->rx_hopid = ret;
195 	}
196 
197 	ret = tb_xdomain_enable_paths(dt->xd, dt->tx_hopid,
198 				      dt->tx_ring ? dt->tx_ring->hop : -1,
199 				      dt->rx_hopid,
200 				      dt->rx_ring ? dt->rx_ring->hop : -1);
201 	if (ret) {
202 		dma_test_free_rings(dt);
203 		return ret;
204 	}
205 
206 	if (dt->tx_ring)
207 		tb_ring_start(dt->tx_ring);
208 	if (dt->rx_ring)
209 		tb_ring_start(dt->rx_ring);
210 
211 	return 0;
212 }
213 
214 static void dma_test_stop_rings(struct dma_test *dt)
215 {
216 	int ret;
217 
218 	if (dt->rx_ring)
219 		tb_ring_stop(dt->rx_ring);
220 	if (dt->tx_ring)
221 		tb_ring_stop(dt->tx_ring);
222 
223 	ret = tb_xdomain_disable_paths(dt->xd, dt->tx_hopid,
224 				       dt->tx_ring ? dt->tx_ring->hop : -1,
225 				       dt->rx_hopid,
226 				       dt->rx_ring ? dt->rx_ring->hop : -1);
227 	if (ret)
228 		dev_warn(&dt->svc->dev, "failed to disable DMA paths\n");
229 
230 	dma_test_free_rings(dt);
231 }
232 
233 static void dma_test_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
234 				 bool canceled)
235 {
236 	struct dma_test_frame *tf = container_of(frame, typeof(*tf), frame);
237 	struct dma_test *dt = tf->dma_test;
238 	struct device *dma_dev = tb_ring_dma_device(dt->rx_ring);
239 
240 	dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
241 			 DMA_FROM_DEVICE);
242 	kfree(tf->data);
243 
244 	if (canceled) {
245 		kfree(tf);
246 		return;
247 	}
248 
249 	dt->packets_received++;
250 	dev_dbg(&dt->svc->dev, "packet %u/%u received\n", dt->packets_received,
251 		dt->packets_to_receive);
252 
253 	if (tf->frame.flags & RING_DESC_CRC_ERROR)
254 		dt->crc_errors++;
255 	if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN)
256 		dt->buffer_overflow_errors++;
257 
258 	kfree(tf);
259 
260 	if (dt->packets_received == dt->packets_to_receive)
261 		complete(&dt->complete);
262 }
263 
264 static int dma_test_submit_rx(struct dma_test *dt, size_t npackets)
265 {
266 	struct device *dma_dev = tb_ring_dma_device(dt->rx_ring);
267 	int i;
268 
269 	for (i = 0; i < npackets; i++) {
270 		struct dma_test_frame *tf;
271 		dma_addr_t dma_addr;
272 
273 		tf = kzalloc_obj(*tf);
274 		if (!tf)
275 			return -ENOMEM;
276 
277 		tf->data = kzalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
278 		if (!tf->data) {
279 			kfree(tf);
280 			return -ENOMEM;
281 		}
282 
283 		dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
284 					  DMA_FROM_DEVICE);
285 		if (dma_mapping_error(dma_dev, dma_addr)) {
286 			kfree(tf->data);
287 			kfree(tf);
288 			return -ENOMEM;
289 		}
290 
291 		tf->frame.buffer_phy = dma_addr;
292 		tf->frame.callback = dma_test_rx_callback;
293 		tf->dma_test = dt;
294 		INIT_LIST_HEAD(&tf->frame.list);
295 
296 		tb_ring_rx(dt->rx_ring, &tf->frame);
297 	}
298 
299 	return 0;
300 }
301 
302 static void dma_test_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
303 				 bool canceled)
304 {
305 	struct dma_test_frame *tf = container_of(frame, typeof(*tf), frame);
306 	struct dma_test *dt = tf->dma_test;
307 	struct device *dma_dev = tb_ring_dma_device(dt->tx_ring);
308 
309 	dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
310 			 DMA_TO_DEVICE);
311 	kfree(tf->data);
312 	kfree(tf);
313 }
314 
315 static int dma_test_submit_tx(struct dma_test *dt, size_t npackets)
316 {
317 	struct device *dma_dev = tb_ring_dma_device(dt->tx_ring);
318 	int i;
319 
320 	for (i = 0; i < npackets; i++) {
321 		struct dma_test_frame *tf;
322 		dma_addr_t dma_addr;
323 
324 		tf = kzalloc_obj(*tf);
325 		if (!tf)
326 			return -ENOMEM;
327 
328 		tf->frame.size = 0; /* means 4096 */
329 		tf->dma_test = dt;
330 
331 		tf->data = kmemdup(dma_test_pattern, DMA_TEST_FRAME_SIZE, GFP_KERNEL);
332 		if (!tf->data) {
333 			kfree(tf);
334 			return -ENOMEM;
335 		}
336 
337 		dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
338 					  DMA_TO_DEVICE);
339 		if (dma_mapping_error(dma_dev, dma_addr)) {
340 			kfree(tf->data);
341 			kfree(tf);
342 			return -ENOMEM;
343 		}
344 
345 		tf->frame.buffer_phy = dma_addr;
346 		tf->frame.callback = dma_test_tx_callback;
347 		tf->frame.sof = DMA_TEST_PDF_FRAME_START;
348 		tf->frame.eof = DMA_TEST_PDF_FRAME_END;
349 		INIT_LIST_HEAD(&tf->frame.list);
350 
351 		dt->packets_sent++;
352 		dev_dbg(&dt->svc->dev, "packet %u/%u sent\n", dt->packets_sent,
353 			dt->packets_to_send);
354 
355 		tb_ring_tx(dt->tx_ring, &tf->frame);
356 	}
357 
358 	return 0;
359 }
360 
361 #define DMA_TEST_DEBUGFS_ATTR(__fops, __get, __validate, __set)	\
362 static int __fops ## _show(void *data, u64 *val)		\
363 {								\
364 	struct tb_service *svc = data;				\
365 	struct dma_test *dt = tb_service_get_drvdata(svc);	\
366 	int ret;						\
367 								\
368 	ret = mutex_lock_interruptible(&dt->lock);		\
369 	if (ret)						\
370 		return ret;					\
371 	__get(dt, val);						\
372 	mutex_unlock(&dt->lock);				\
373 	return 0;						\
374 }								\
375 static int __fops ## _store(void *data, u64 val)		\
376 {								\
377 	struct tb_service *svc = data;				\
378 	struct dma_test *dt = tb_service_get_drvdata(svc);	\
379 	int ret;						\
380 								\
381 	ret = __validate(val);					\
382 	if (ret)						\
383 		return ret;					\
384 	ret = mutex_lock_interruptible(&dt->lock);		\
385 	if (ret)						\
386 		return ret;					\
387 	__set(dt, val);						\
388 	mutex_unlock(&dt->lock);				\
389 	return 0;						\
390 }								\
391 DEFINE_DEBUGFS_ATTRIBUTE(__fops ## _fops, __fops ## _show,	\
392 			 __fops ## _store, "%llu\n")
393 
394 static void lanes_get(const struct dma_test *dt, u64 *val)
395 {
396 	*val = dt->link_width;
397 }
398 
399 static int lanes_validate(u64 val)
400 {
401 	return val > 2 ? -EINVAL : 0;
402 }
403 
404 static void lanes_set(struct dma_test *dt, u64 val)
405 {
406 	dt->link_width = val;
407 }
408 DMA_TEST_DEBUGFS_ATTR(lanes, lanes_get, lanes_validate, lanes_set);
409 
410 static void speed_get(const struct dma_test *dt, u64 *val)
411 {
412 	*val = dt->link_speed;
413 }
414 
415 static int speed_validate(u64 val)
416 {
417 	switch (val) {
418 	case 40:
419 	case 20:
420 	case 10:
421 	case 0:
422 		return 0;
423 	default:
424 		return -EINVAL;
425 	}
426 }
427 
428 static void speed_set(struct dma_test *dt, u64 val)
429 {
430 	dt->link_speed = val;
431 }
432 DMA_TEST_DEBUGFS_ATTR(speed, speed_get, speed_validate, speed_set);
433 
434 static void packets_to_receive_get(const struct dma_test *dt, u64 *val)
435 {
436 	*val = dt->packets_to_receive;
437 }
438 
439 static int packets_to_receive_validate(u64 val)
440 {
441 	return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
442 }
443 
444 static void packets_to_receive_set(struct dma_test *dt, u64 val)
445 {
446 	dt->packets_to_receive = val;
447 }
448 DMA_TEST_DEBUGFS_ATTR(packets_to_receive, packets_to_receive_get,
449 		      packets_to_receive_validate, packets_to_receive_set);
450 
451 static void packets_to_send_get(const struct dma_test *dt, u64 *val)
452 {
453 	*val = dt->packets_to_send;
454 }
455 
456 static int packets_to_send_validate(u64 val)
457 {
458 	return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
459 }
460 
461 static void packets_to_send_set(struct dma_test *dt, u64 val)
462 {
463 	dt->packets_to_send = val;
464 }
465 DMA_TEST_DEBUGFS_ATTR(packets_to_send, packets_to_send_get,
466 		      packets_to_send_validate, packets_to_send_set);
467 
468 static int dma_test_set_bonding(struct dma_test *dt)
469 {
470 	switch (dt->link_width) {
471 	case TB_LINK_WIDTH_DUAL:
472 		return tb_xdomain_lane_bonding_enable(dt->xd);
473 	case TB_LINK_WIDTH_SINGLE:
474 		tb_xdomain_lane_bonding_disable(dt->xd);
475 		fallthrough;
476 	default:
477 		return 0;
478 	}
479 }
480 
481 static bool dma_test_validate_config(struct dma_test *dt)
482 {
483 	if (!dt->packets_to_send && !dt->packets_to_receive)
484 		return false;
485 	if (dt->packets_to_send && dt->packets_to_receive &&
486 	    dt->packets_to_send != dt->packets_to_receive)
487 		return false;
488 	return true;
489 }
490 
491 static void dma_test_check_errors(struct dma_test *dt, int ret)
492 {
493 	if (!dt->error_code) {
494 		if (dt->link_speed && dt->xd->link_speed != dt->link_speed) {
495 			dt->error_code = DMA_TEST_SPEED_ERROR;
496 		} else if (dt->link_width && dt->link_width != dt->xd->link_width) {
497 			dt->error_code = DMA_TEST_WIDTH_ERROR;
498 		} else if (dt->packets_to_send != dt->packets_sent ||
499 			 dt->packets_to_receive != dt->packets_received ||
500 			 dt->crc_errors || dt->buffer_overflow_errors) {
501 			dt->error_code = DMA_TEST_PACKET_ERROR;
502 		} else {
503 			return;
504 		}
505 	}
506 
507 	dt->result = DMA_TEST_FAIL;
508 }
509 
510 static int test_store(void *data, u64 val)
511 {
512 	struct tb_service *svc = data;
513 	struct dma_test *dt = tb_service_get_drvdata(svc);
514 	int ret;
515 
516 	if (val != 1)
517 		return -EINVAL;
518 
519 	ret = mutex_lock_interruptible(&dt->lock);
520 	if (ret)
521 		return ret;
522 
523 	dt->packets_sent = 0;
524 	dt->packets_received = 0;
525 	dt->crc_errors = 0;
526 	dt->buffer_overflow_errors = 0;
527 	dt->result = DMA_TEST_SUCCESS;
528 	dt->error_code = DMA_TEST_NO_ERROR;
529 
530 	dev_dbg(&svc->dev, "DMA test starting\n");
531 	if (dt->link_speed)
532 		dev_dbg(&svc->dev, "link_speed: %u Gb/s\n", dt->link_speed);
533 	if (dt->link_width)
534 		dev_dbg(&svc->dev, "link_width: %u\n", dt->link_width);
535 	dev_dbg(&svc->dev, "packets_to_send: %u\n", dt->packets_to_send);
536 	dev_dbg(&svc->dev, "packets_to_receive: %u\n", dt->packets_to_receive);
537 
538 	if (!dma_test_validate_config(dt)) {
539 		dev_err(&svc->dev, "invalid test configuration\n");
540 		dt->error_code = DMA_TEST_CONFIG_ERROR;
541 		goto out_unlock;
542 	}
543 
544 	ret = dma_test_set_bonding(dt);
545 	if (ret) {
546 		dev_err(&svc->dev, "failed to set lanes\n");
547 		dt->error_code = DMA_TEST_BONDING_ERROR;
548 		goto out_unlock;
549 	}
550 
551 	ret = dma_test_start_rings(dt);
552 	if (ret) {
553 		dev_err(&svc->dev, "failed to enable DMA rings\n");
554 		dt->error_code = DMA_TEST_DMA_ERROR;
555 		goto out_unlock;
556 	}
557 
558 	if (dt->packets_to_receive) {
559 		reinit_completion(&dt->complete);
560 		ret = dma_test_submit_rx(dt, dt->packets_to_receive);
561 		if (ret) {
562 			dev_err(&svc->dev, "failed to submit receive buffers\n");
563 			dt->error_code = DMA_TEST_BUFFER_ERROR;
564 			goto out_stop;
565 		}
566 	}
567 
568 	if (dt->packets_to_send) {
569 		ret = dma_test_submit_tx(dt, dt->packets_to_send);
570 		if (ret) {
571 			dev_err(&svc->dev, "failed to submit transmit buffers\n");
572 			dt->error_code = DMA_TEST_BUFFER_ERROR;
573 			goto out_stop;
574 		}
575 	}
576 
577 	if (dt->packets_to_receive) {
578 		ret = wait_for_completion_interruptible(&dt->complete);
579 		if (ret) {
580 			dt->error_code = DMA_TEST_INTERRUPTED;
581 			goto out_stop;
582 		}
583 	}
584 
585 out_stop:
586 	dma_test_stop_rings(dt);
587 out_unlock:
588 	dma_test_check_errors(dt, ret);
589 	mutex_unlock(&dt->lock);
590 
591 	dev_dbg(&svc->dev, "DMA test %s\n", dma_test_result_names[dt->result]);
592 	return ret;
593 }
594 DEFINE_DEBUGFS_ATTRIBUTE(test_fops, NULL, test_store, "%llu\n");
595 
596 static int status_show(struct seq_file *s, void *not_used)
597 {
598 	struct tb_service *svc = s->private;
599 	struct dma_test *dt = tb_service_get_drvdata(svc);
600 	int ret;
601 
602 	ret = mutex_lock_interruptible(&dt->lock);
603 	if (ret)
604 		return ret;
605 
606 	seq_printf(s, "result: %s\n", dma_test_result_names[dt->result]);
607 	if (dt->result == DMA_TEST_NOT_RUN)
608 		goto out_unlock;
609 
610 	seq_printf(s, "packets received: %u\n", dt->packets_received);
611 	seq_printf(s, "packets sent: %u\n", dt->packets_sent);
612 	seq_printf(s, "CRC errors: %u\n", dt->crc_errors);
613 	seq_printf(s, "buffer overflow errors: %u\n",
614 		   dt->buffer_overflow_errors);
615 	seq_printf(s, "error: %s\n", dma_test_error_names[dt->error_code]);
616 
617 out_unlock:
618 	mutex_unlock(&dt->lock);
619 	return 0;
620 }
621 DEFINE_SHOW_ATTRIBUTE(status);
622 
623 static void dma_test_debugfs_init(struct tb_service *svc)
624 {
625 	struct dentry *debugfs_dir;
626 
627 	debugfs_dir = debugfs_create_dir("dma_test", svc->debugfs_dir);
628 
629 	debugfs_create_file("lanes", 0600, debugfs_dir, svc, &lanes_fops);
630 	debugfs_create_file("speed", 0600, debugfs_dir, svc, &speed_fops);
631 	debugfs_create_file("packets_to_receive", 0600, debugfs_dir, svc,
632 			    &packets_to_receive_fops);
633 	debugfs_create_file("packets_to_send", 0600, debugfs_dir, svc,
634 			    &packets_to_send_fops);
635 	debugfs_create_file("status", 0400, debugfs_dir, svc, &status_fops);
636 	debugfs_create_file("test", 0200, debugfs_dir, svc, &test_fops);
637 }
638 
639 static int dma_test_probe(struct tb_service *svc, const struct tb_service_id *id)
640 {
641 	struct tb_xdomain *xd = tb_service_parent(svc);
642 	struct dma_test *dt;
643 
644 	dt = devm_kzalloc(&svc->dev, sizeof(*dt), GFP_KERNEL);
645 	if (!dt)
646 		return -ENOMEM;
647 
648 	dt->svc = svc;
649 	dt->xd = xd;
650 	mutex_init(&dt->lock);
651 	init_completion(&dt->complete);
652 
653 	tb_service_set_drvdata(svc, dt);
654 	dma_test_debugfs_init(svc);
655 
656 	return 0;
657 }
658 
659 static void dma_test_remove(struct tb_service *svc)
660 {
661 	struct dma_test *dt = tb_service_get_drvdata(svc);
662 
663 	mutex_lock(&dt->lock);
664 	debugfs_lookup_and_remove("dma_test", svc->debugfs_dir);
665 	mutex_unlock(&dt->lock);
666 }
667 
668 static int __maybe_unused dma_test_suspend(struct device *dev)
669 {
670 	/*
671 	 * No need to do anything special here. If userspace is writing
672 	 * to the test attribute when suspend started, it comes out from
673 	 * wait_for_completion_interruptible() with -ERESTARTSYS and the
674 	 * DMA test fails tearing down the rings. Once userspace is
675 	 * thawed the kernel restarts the write syscall effectively
676 	 * re-running the test.
677 	 */
678 	return 0;
679 }
680 
681 static int __maybe_unused dma_test_resume(struct device *dev)
682 {
683 	return 0;
684 }
685 
686 static const struct dev_pm_ops dma_test_pm_ops = {
687 	SET_SYSTEM_SLEEP_PM_OPS(dma_test_suspend, dma_test_resume)
688 };
689 
690 static const struct tb_service_id dma_test_ids[] = {
691 	{ TB_SERVICE("dma_test", 1) },
692 	{ },
693 };
694 MODULE_DEVICE_TABLE(tbsvc, dma_test_ids);
695 
696 static struct tb_service_driver dma_test_driver = {
697 	.driver = {
698 		.owner = THIS_MODULE,
699 		.name = "thunderbolt_dma_test",
700 		.pm = &dma_test_pm_ops,
701 	},
702 	.probe = dma_test_probe,
703 	.remove = dma_test_remove,
704 	.id_table = dma_test_ids,
705 };
706 
707 static int __init dma_test_init(void)
708 {
709 	u64 data_value = DMA_TEST_DATA_PATTERN;
710 	int i, ret;
711 
712 	dma_test_pattern = kmalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
713 	if (!dma_test_pattern)
714 		return -ENOMEM;
715 
716 	for (i = 0; i <	DMA_TEST_FRAME_SIZE / sizeof(data_value); i++)
717 		((u32 *)dma_test_pattern)[i] = data_value++;
718 
719 	dma_test_dir = tb_property_create_dir(&dma_test_dir_uuid);
720 	if (!dma_test_dir) {
721 		ret = -ENOMEM;
722 		goto err_free_pattern;
723 	}
724 
725 	tb_property_add_immediate(dma_test_dir, "prtcid", 1);
726 	tb_property_add_immediate(dma_test_dir, "prtcvers", 1);
727 	tb_property_add_immediate(dma_test_dir, "prtcrevs", 0);
728 	tb_property_add_immediate(dma_test_dir, "prtcstns", 0);
729 
730 	ret = tb_register_property_dir("dma_test", dma_test_dir);
731 	if (ret)
732 		goto err_free_dir;
733 
734 	ret = tb_register_service_driver(&dma_test_driver);
735 	if (ret)
736 		goto err_unregister_dir;
737 
738 	return 0;
739 
740 err_unregister_dir:
741 	tb_unregister_property_dir("dma_test", dma_test_dir);
742 err_free_dir:
743 	tb_property_free_dir(dma_test_dir);
744 err_free_pattern:
745 	kfree(dma_test_pattern);
746 
747 	return ret;
748 }
749 module_init(dma_test_init);
750 
751 static void __exit dma_test_exit(void)
752 {
753 	tb_unregister_service_driver(&dma_test_driver);
754 	tb_unregister_property_dir("dma_test", dma_test_dir);
755 	tb_property_free_dir(dma_test_dir);
756 	kfree(dma_test_pattern);
757 }
758 module_exit(dma_test_exit);
759 
760 MODULE_AUTHOR("Isaac Hazan <isaac.hazan@intel.com>");
761 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
762 MODULE_DESCRIPTION("Thunderbolt/USB4 DMA traffic test driver");
763 MODULE_LICENSE("GPL v2");
764