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_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
22 
23 enum dma_test_frame_pdf {
24 	DMA_TEST_PDF_FRAME_START = 1,
25 	DMA_TEST_PDF_FRAME_END,
26 };
27 
28 struct dma_test_frame {
29 	struct dma_test *dma_test;
30 	void *data;
31 	struct ring_frame frame;
32 };
33 
34 enum dma_test_test_error {
35 	DMA_TEST_NO_ERROR,
36 	DMA_TEST_INTERRUPTED,
37 	DMA_TEST_BUFFER_ERROR,
38 	DMA_TEST_DMA_ERROR,
39 	DMA_TEST_CONFIG_ERROR,
40 	DMA_TEST_SPEED_ERROR,
41 	DMA_TEST_WIDTH_ERROR,
42 	DMA_TEST_BONDING_ERROR,
43 	DMA_TEST_PACKET_ERROR,
44 };
45 
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",
56 };
57 
58 enum dma_test_result {
59 	DMA_TEST_NOT_RUN,
60 	DMA_TEST_SUCCESS,
61 	DMA_TEST_FAIL,
62 };
63 
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",
68 };
69 
70 /**
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
84  *			    run
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
90  */
91 struct 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;
107 	struct mutex lock;
108 	struct dentry *debugfs_dir;
109 };
110 
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);
115 
116 static struct tb_property_dir *dma_test_dir;
117 static void *dma_test_pattern;
118 
119 static void dma_test_free_rings(struct dma_test *dt)
120 {
121 	if (dt->rx_ring) {
122 		tb_ring_free(dt->rx_ring);
123 		dt->rx_ring = NULL;
124 	}
125 	if (dt->tx_ring) {
126 		tb_ring_free(dt->tx_ring);
127 		dt->tx_ring = NULL;
128 	}
129 }
130 
131 static int dma_test_start_rings(struct dma_test *dt)
132 {
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;
137 
138 	/*
139 	 * If we are both sender and receiver (traffic goes over a
140 	 * special loopback dongle) enable E2E flow control. This avoids
141 	 * losing packets.
142 	 */
143 	if (dt->packets_to_send && dt->packets_to_receive)
144 		flags |= RING_FLAG_E2E;
145 
146 	if (dt->packets_to_send) {
147 		ring = tb_ring_alloc_tx(xd->tb->nhi, -1, DMA_TEST_TX_RING_SIZE,
148 					flags);
149 		if (!ring)
150 			return -ENOMEM;
151 
152 		dt->tx_ring = ring;
153 		e2e_tx_hop = ring->hop;
154 	}
155 
156 	if (dt->packets_to_receive) {
157 		u16 sof_mask, eof_mask;
158 
159 		sof_mask = BIT(DMA_TEST_PDF_FRAME_START);
160 		eof_mask = BIT(DMA_TEST_PDF_FRAME_END);
161 
162 		ring = tb_ring_alloc_rx(xd->tb->nhi, -1, DMA_TEST_RX_RING_SIZE,
163 					flags, e2e_tx_hop, sof_mask, eof_mask,
164 					NULL, NULL);
165 		if (!ring) {
166 			dma_test_free_rings(dt);
167 			return -ENOMEM;
168 		}
169 
170 		dt->rx_ring = ring;
171 	}
172 
173 	ret = tb_xdomain_enable_paths(dt->xd, DMA_TEST_HOPID,
174 				      dt->tx_ring ? dt->tx_ring->hop : 0,
175 				      DMA_TEST_HOPID,
176 				      dt->rx_ring ? dt->rx_ring->hop : 0);
177 	if (ret) {
178 		dma_test_free_rings(dt);
179 		return ret;
180 	}
181 
182 	if (dt->tx_ring)
183 		tb_ring_start(dt->tx_ring);
184 	if (dt->rx_ring)
185 		tb_ring_start(dt->rx_ring);
186 
187 	return 0;
188 }
189 
190 static void dma_test_stop_rings(struct dma_test *dt)
191 {
192 	if (dt->rx_ring)
193 		tb_ring_stop(dt->rx_ring);
194 	if (dt->tx_ring)
195 		tb_ring_stop(dt->tx_ring);
196 
197 	if (tb_xdomain_disable_paths(dt->xd))
198 		dev_warn(&dt->svc->dev, "failed to disable DMA paths\n");
199 
200 	dma_test_free_rings(dt);
201 }
202 
203 static void dma_test_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
204 				 bool canceled)
205 {
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);
209 
210 	dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
211 			 DMA_FROM_DEVICE);
212 	kfree(tf->data);
213 
214 	if (canceled) {
215 		kfree(tf);
216 		return;
217 	}
218 
219 	dt->packets_received++;
220 	dev_dbg(&dt->svc->dev, "packet %u/%u received\n", dt->packets_received,
221 		dt->packets_to_receive);
222 
223 	if (tf->frame.flags & RING_DESC_CRC_ERROR)
224 		dt->crc_errors++;
225 	if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN)
226 		dt->buffer_overflow_errors++;
227 
228 	kfree(tf);
229 
230 	if (dt->packets_received == dt->packets_to_receive)
231 		complete(&dt->complete);
232 }
233 
234 static int dma_test_submit_rx(struct dma_test *dt, size_t npackets)
235 {
236 	struct device *dma_dev = tb_ring_dma_device(dt->rx_ring);
237 	int i;
238 
239 	for (i = 0; i < npackets; i++) {
240 		struct dma_test_frame *tf;
241 		dma_addr_t dma_addr;
242 
243 		tf = kzalloc(sizeof(*tf), GFP_KERNEL);
244 		if (!tf)
245 			return -ENOMEM;
246 
247 		tf->data = kzalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
248 		if (!tf->data) {
249 			kfree(tf);
250 			return -ENOMEM;
251 		}
252 
253 		dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
254 					  DMA_FROM_DEVICE);
255 		if (dma_mapping_error(dma_dev, dma_addr)) {
256 			kfree(tf->data);
257 			kfree(tf);
258 			return -ENOMEM;
259 		}
260 
261 		tf->frame.buffer_phy = dma_addr;
262 		tf->frame.callback = dma_test_rx_callback;
263 		tf->dma_test = dt;
264 		INIT_LIST_HEAD(&tf->frame.list);
265 
266 		tb_ring_rx(dt->rx_ring, &tf->frame);
267 	}
268 
269 	return 0;
270 }
271 
272 static void dma_test_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
273 				 bool canceled)
274 {
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);
278 
279 	dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
280 			 DMA_TO_DEVICE);
281 	kfree(tf->data);
282 	kfree(tf);
283 }
284 
285 static int dma_test_submit_tx(struct dma_test *dt, size_t npackets)
286 {
287 	struct device *dma_dev = tb_ring_dma_device(dt->tx_ring);
288 	int i;
289 
290 	for (i = 0; i < npackets; i++) {
291 		struct dma_test_frame *tf;
292 		dma_addr_t dma_addr;
293 
294 		tf = kzalloc(sizeof(*tf), GFP_KERNEL);
295 		if (!tf)
296 			return -ENOMEM;
297 
298 		tf->frame.size = 0; /* means 4096 */
299 		tf->dma_test = dt;
300 
301 		tf->data = kmemdup(dma_test_pattern, DMA_TEST_FRAME_SIZE, GFP_KERNEL);
302 		if (!tf->data) {
303 			kfree(tf);
304 			return -ENOMEM;
305 		}
306 
307 		dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
308 					  DMA_TO_DEVICE);
309 		if (dma_mapping_error(dma_dev, dma_addr)) {
310 			kfree(tf->data);
311 			kfree(tf);
312 			return -ENOMEM;
313 		}
314 
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);
320 
321 		dt->packets_sent++;
322 		dev_dbg(&dt->svc->dev, "packet %u/%u sent\n", dt->packets_sent,
323 			dt->packets_to_send);
324 
325 		tb_ring_tx(dt->tx_ring, &tf->frame);
326 	}
327 
328 	return 0;
329 }
330 
331 #define DMA_TEST_DEBUGFS_ATTR(__fops, __get, __validate, __set)	\
332 static int __fops ## _show(void *data, u64 *val)		\
333 {								\
334 	struct tb_service *svc = data;				\
335 	struct dma_test *dt = tb_service_get_drvdata(svc);	\
336 	int ret;						\
337 								\
338 	ret = mutex_lock_interruptible(&dt->lock);		\
339 	if (ret)						\
340 		return ret;					\
341 	__get(dt, val);						\
342 	mutex_unlock(&dt->lock);				\
343 	return 0;						\
344 }								\
345 static int __fops ## _store(void *data, u64 val)		\
346 {								\
347 	struct tb_service *svc = data;				\
348 	struct dma_test *dt = tb_service_get_drvdata(svc);	\
349 	int ret;						\
350 								\
351 	ret = __validate(val);					\
352 	if (ret)						\
353 		return ret;					\
354 	ret = mutex_lock_interruptible(&dt->lock);		\
355 	if (ret)						\
356 		return ret;					\
357 	__set(dt, val);						\
358 	mutex_unlock(&dt->lock);				\
359 	return 0;						\
360 }								\
361 DEFINE_DEBUGFS_ATTRIBUTE(__fops ## _fops, __fops ## _show,	\
362 			 __fops ## _store, "%llu\n")
363 
364 static void lanes_get(const struct dma_test *dt, u64 *val)
365 {
366 	*val = dt->link_width;
367 }
368 
369 static int lanes_validate(u64 val)
370 {
371 	return val > 2 ? -EINVAL : 0;
372 }
373 
374 static void lanes_set(struct dma_test *dt, u64 val)
375 {
376 	dt->link_width = val;
377 }
378 DMA_TEST_DEBUGFS_ATTR(lanes, lanes_get, lanes_validate, lanes_set);
379 
380 static void speed_get(const struct dma_test *dt, u64 *val)
381 {
382 	*val = dt->link_speed;
383 }
384 
385 static int speed_validate(u64 val)
386 {
387 	switch (val) {
388 	case 20:
389 	case 10:
390 	case 0:
391 		return 0;
392 	default:
393 		return -EINVAL;
394 	}
395 }
396 
397 static void speed_set(struct dma_test *dt, u64 val)
398 {
399 	dt->link_speed = val;
400 }
401 DMA_TEST_DEBUGFS_ATTR(speed, speed_get, speed_validate, speed_set);
402 
403 static void packets_to_receive_get(const struct dma_test *dt, u64 *val)
404 {
405 	*val = dt->packets_to_receive;
406 }
407 
408 static int packets_to_receive_validate(u64 val)
409 {
410 	return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
411 }
412 
413 static void packets_to_receive_set(struct dma_test *dt, u64 val)
414 {
415 	dt->packets_to_receive = val;
416 }
417 DMA_TEST_DEBUGFS_ATTR(packets_to_receive, packets_to_receive_get,
418 		      packets_to_receive_validate, packets_to_receive_set);
419 
420 static void packets_to_send_get(const struct dma_test *dt, u64 *val)
421 {
422 	*val = dt->packets_to_send;
423 }
424 
425 static int packets_to_send_validate(u64 val)
426 {
427 	return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
428 }
429 
430 static void packets_to_send_set(struct dma_test *dt, u64 val)
431 {
432 	dt->packets_to_send = val;
433 }
434 DMA_TEST_DEBUGFS_ATTR(packets_to_send, packets_to_send_get,
435 		      packets_to_send_validate, packets_to_send_set);
436 
437 static int dma_test_set_bonding(struct dma_test *dt)
438 {
439 	switch (dt->link_width) {
440 	case 2:
441 		return tb_xdomain_lane_bonding_enable(dt->xd);
442 	case 1:
443 		tb_xdomain_lane_bonding_disable(dt->xd);
444 		fallthrough;
445 	default:
446 		return 0;
447 	}
448 }
449 
450 static bool dma_test_validate_config(struct dma_test *dt)
451 {
452 	if (!dt->packets_to_send && !dt->packets_to_receive)
453 		return false;
454 	if (dt->packets_to_send && dt->packets_to_receive &&
455 	    dt->packets_to_send != dt->packets_to_receive)
456 		return false;
457 	return true;
458 }
459 
460 static void dma_test_check_errors(struct dma_test *dt, int ret)
461 {
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;
472 		} else {
473 			return;
474 		}
475 	}
476 
477 	dt->result = DMA_TEST_FAIL;
478 }
479 
480 static int test_store(void *data, u64 val)
481 {
482 	struct tb_service *svc = data;
483 	struct dma_test *dt = tb_service_get_drvdata(svc);
484 	int ret;
485 
486 	if (val != 1)
487 		return -EINVAL;
488 
489 	ret = mutex_lock_interruptible(&dt->lock);
490 	if (ret)
491 		return ret;
492 
493 	dt->packets_sent = 0;
494 	dt->packets_received = 0;
495 	dt->crc_errors = 0;
496 	dt->buffer_overflow_errors = 0;
497 	dt->result = DMA_TEST_SUCCESS;
498 	dt->error_code = DMA_TEST_NO_ERROR;
499 
500 	dev_dbg(&svc->dev, "DMA test starting\n");
501 	if (dt->link_speed)
502 		dev_dbg(&svc->dev, "link_speed: %u Gb/s\n", dt->link_speed);
503 	if (dt->link_width)
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);
507 
508 	if (!dma_test_validate_config(dt)) {
509 		dev_err(&svc->dev, "invalid test configuration\n");
510 		dt->error_code = DMA_TEST_CONFIG_ERROR;
511 		goto out_unlock;
512 	}
513 
514 	ret = dma_test_set_bonding(dt);
515 	if (ret) {
516 		dev_err(&svc->dev, "failed to set lanes\n");
517 		dt->error_code = DMA_TEST_BONDING_ERROR;
518 		goto out_unlock;
519 	}
520 
521 	ret = dma_test_start_rings(dt);
522 	if (ret) {
523 		dev_err(&svc->dev, "failed to enable DMA rings\n");
524 		dt->error_code = DMA_TEST_DMA_ERROR;
525 		goto out_unlock;
526 	}
527 
528 	if (dt->packets_to_receive) {
529 		reinit_completion(&dt->complete);
530 		ret = dma_test_submit_rx(dt, dt->packets_to_receive);
531 		if (ret) {
532 			dev_err(&svc->dev, "failed to submit receive buffers\n");
533 			dt->error_code = DMA_TEST_BUFFER_ERROR;
534 			goto out_stop;
535 		}
536 	}
537 
538 	if (dt->packets_to_send) {
539 		ret = dma_test_submit_tx(dt, dt->packets_to_send);
540 		if (ret) {
541 			dev_err(&svc->dev, "failed to submit transmit buffers\n");
542 			dt->error_code = DMA_TEST_BUFFER_ERROR;
543 			goto out_stop;
544 		}
545 	}
546 
547 	if (dt->packets_to_receive) {
548 		ret = wait_for_completion_interruptible(&dt->complete);
549 		if (ret) {
550 			dt->error_code = DMA_TEST_INTERRUPTED;
551 			goto out_stop;
552 		}
553 	}
554 
555 out_stop:
556 	dma_test_stop_rings(dt);
557 out_unlock:
558 	dma_test_check_errors(dt, ret);
559 	mutex_unlock(&dt->lock);
560 
561 	dev_dbg(&svc->dev, "DMA test %s\n", dma_test_result_names[dt->result]);
562 	return ret;
563 }
564 DEFINE_DEBUGFS_ATTRIBUTE(test_fops, NULL, test_store, "%llu\n");
565 
566 static int status_show(struct seq_file *s, void *not_used)
567 {
568 	struct tb_service *svc = s->private;
569 	struct dma_test *dt = tb_service_get_drvdata(svc);
570 	int ret;
571 
572 	ret = mutex_lock_interruptible(&dt->lock);
573 	if (ret)
574 		return ret;
575 
576 	seq_printf(s, "result: %s\n", dma_test_result_names[dt->result]);
577 	if (dt->result == DMA_TEST_NOT_RUN)
578 		goto out_unlock;
579 
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]);
586 
587 out_unlock:
588 	mutex_unlock(&dt->lock);
589 	return 0;
590 }
591 DEFINE_SHOW_ATTRIBUTE(status);
592 
593 static void dma_test_debugfs_init(struct tb_service *svc)
594 {
595 	struct dma_test *dt = tb_service_get_drvdata(svc);
596 
597 	dt->debugfs_dir = debugfs_create_dir("dma_test", svc->debugfs_dir);
598 
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);
607 }
608 
609 static int dma_test_probe(struct tb_service *svc, const struct tb_service_id *id)
610 {
611 	struct tb_xdomain *xd = tb_service_parent(svc);
612 	struct dma_test *dt;
613 
614 	dt = devm_kzalloc(&svc->dev, sizeof(*dt), GFP_KERNEL);
615 	if (!dt)
616 		return -ENOMEM;
617 
618 	dt->svc = svc;
619 	dt->xd = xd;
620 	mutex_init(&dt->lock);
621 	init_completion(&dt->complete);
622 
623 	tb_service_set_drvdata(svc, dt);
624 	dma_test_debugfs_init(svc);
625 
626 	return 0;
627 }
628 
629 static void dma_test_remove(struct tb_service *svc)
630 {
631 	struct dma_test *dt = tb_service_get_drvdata(svc);
632 
633 	mutex_lock(&dt->lock);
634 	debugfs_remove_recursive(dt->debugfs_dir);
635 	mutex_unlock(&dt->lock);
636 }
637 
638 static int __maybe_unused dma_test_suspend(struct device *dev)
639 {
640 	/*
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.
647 	 */
648 	return 0;
649 }
650 
651 static int __maybe_unused dma_test_resume(struct device *dev)
652 {
653 	return 0;
654 }
655 
656 static const struct dev_pm_ops dma_test_pm_ops = {
657 	SET_SYSTEM_SLEEP_PM_OPS(dma_test_suspend, dma_test_resume)
658 };
659 
660 static const struct tb_service_id dma_test_ids[] = {
661 	{ TB_SERVICE("dma_test", 1) },
662 	{ },
663 };
664 MODULE_DEVICE_TABLE(tbsvc, dma_test_ids);
665 
666 static struct tb_service_driver dma_test_driver = {
667 	.driver = {
668 		.owner = THIS_MODULE,
669 		.name = "thunderbolt_dma_test",
670 		.pm = &dma_test_pm_ops,
671 	},
672 	.probe = dma_test_probe,
673 	.remove = dma_test_remove,
674 	.id_table = dma_test_ids,
675 };
676 
677 static int __init dma_test_init(void)
678 {
679 	u64 data_value = DMA_TEST_DATA_PATTERN;
680 	int i, ret;
681 
682 	dma_test_pattern = kmalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
683 	if (!dma_test_pattern)
684 		return -ENOMEM;
685 
686 	for (i = 0; i <	DMA_TEST_FRAME_SIZE / sizeof(data_value); i++)
687 		((u32 *)dma_test_pattern)[i] = data_value++;
688 
689 	dma_test_dir = tb_property_create_dir(&dma_test_dir_uuid);
690 	if (!dma_test_dir) {
691 		ret = -ENOMEM;
692 		goto err_free_pattern;
693 	}
694 
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);
699 
700 	ret = tb_register_property_dir("dma_test", dma_test_dir);
701 	if (ret)
702 		goto err_free_dir;
703 
704 	ret = tb_register_service_driver(&dma_test_driver);
705 	if (ret)
706 		goto err_unregister_dir;
707 
708 	return 0;
709 
710 err_unregister_dir:
711 	tb_unregister_property_dir("dma_test", dma_test_dir);
712 err_free_dir:
713 	tb_property_free_dir(dma_test_dir);
714 err_free_pattern:
715 	kfree(dma_test_pattern);
716 
717 	return ret;
718 }
719 module_init(dma_test_init);
720 
721 static void __exit dma_test_exit(void)
722 {
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);
727 }
728 module_exit(dma_test_exit);
729 
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");
734