1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2022 Intel Corporation.
4  */
5 
6 #include <linux/debugfs.h>
7 #include <linux/relay.h>
8 #include <linux/skbuff.h>
9 #include <linux/wwan.h>
10 
11 #include "t7xx_port.h"
12 #include "t7xx_port_proxy.h"
13 #include "t7xx_state_monitor.h"
14 
15 #define T7XX_TRC_SUB_BUFF_SIZE		131072
16 #define T7XX_TRC_N_SUB_BUFF		32
17 
t7xx_trace_create_buf_file_handler(const char * filename,struct dentry * parent,umode_t mode,struct rchan_buf * buf,int * is_global)18 static struct dentry *t7xx_trace_create_buf_file_handler(const char *filename,
19 							 struct dentry *parent,
20 							 umode_t mode,
21 							 struct rchan_buf *buf,
22 							 int *is_global)
23 {
24 	*is_global = 1;
25 	return debugfs_create_file(filename, mode, parent, buf,
26 				   &relay_file_operations);
27 }
28 
t7xx_trace_remove_buf_file_handler(struct dentry * dentry)29 static int t7xx_trace_remove_buf_file_handler(struct dentry *dentry)
30 {
31 	debugfs_remove(dentry);
32 	return 0;
33 }
34 
t7xx_trace_subbuf_start_handler(struct rchan_buf * buf,void * subbuf,void * prev_subbuf,size_t prev_padding)35 static int t7xx_trace_subbuf_start_handler(struct rchan_buf *buf, void *subbuf,
36 					   void *prev_subbuf, size_t prev_padding)
37 {
38 	if (relay_buf_full(buf)) {
39 		pr_err_ratelimited("Relay_buf full dropping traces");
40 		return 0;
41 	}
42 
43 	return 1;
44 }
45 
46 static struct rchan_callbacks relay_callbacks = {
47 	.subbuf_start = t7xx_trace_subbuf_start_handler,
48 	.create_buf_file = t7xx_trace_create_buf_file_handler,
49 	.remove_buf_file = t7xx_trace_remove_buf_file_handler,
50 };
51 
t7xx_trace_port_uninit(struct t7xx_port * port)52 static void t7xx_trace_port_uninit(struct t7xx_port *port)
53 {
54 	struct dentry *debugfs_dir = port->t7xx_dev->debugfs_dir;
55 	struct rchan *relaych = port->log.relaych;
56 
57 	if (!relaych)
58 		return;
59 
60 	relay_close(relaych);
61 	debugfs_remove_recursive(debugfs_dir);
62 }
63 
t7xx_trace_port_recv_skb(struct t7xx_port * port,struct sk_buff * skb)64 static int t7xx_trace_port_recv_skb(struct t7xx_port *port, struct sk_buff *skb)
65 {
66 	struct rchan *relaych = port->log.relaych;
67 
68 	if (!relaych)
69 		return -EINVAL;
70 
71 	relay_write(relaych, skb->data, skb->len);
72 	dev_kfree_skb(skb);
73 	return 0;
74 }
75 
t7xx_port_trace_md_state_notify(struct t7xx_port * port,unsigned int state)76 static void t7xx_port_trace_md_state_notify(struct t7xx_port *port, unsigned int state)
77 {
78 	struct rchan *relaych = port->log.relaych;
79 	struct dentry *debugfs_wwan_dir;
80 	struct dentry *debugfs_dir;
81 
82 	if (state != MD_STATE_READY || relaych)
83 		return;
84 
85 	debugfs_wwan_dir = wwan_get_debugfs_dir(port->dev);
86 	if (IS_ERR(debugfs_wwan_dir))
87 		return;
88 
89 	debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, debugfs_wwan_dir);
90 	if (IS_ERR_OR_NULL(debugfs_dir)) {
91 		wwan_put_debugfs_dir(debugfs_wwan_dir);
92 		dev_err(port->dev, "Unable to create debugfs for trace");
93 		return;
94 	}
95 
96 	relaych = relay_open("relay_ch", debugfs_dir, T7XX_TRC_SUB_BUFF_SIZE,
97 			     T7XX_TRC_N_SUB_BUFF, &relay_callbacks, NULL);
98 	if (!relaych)
99 		goto err_rm_debugfs_dir;
100 
101 	wwan_put_debugfs_dir(debugfs_wwan_dir);
102 	port->log.relaych = relaych;
103 	port->t7xx_dev->debugfs_dir = debugfs_dir;
104 	return;
105 
106 err_rm_debugfs_dir:
107 	debugfs_remove_recursive(debugfs_dir);
108 	wwan_put_debugfs_dir(debugfs_wwan_dir);
109 	dev_err(port->dev, "Unable to create trace port %s", port->port_conf->name);
110 }
111 
112 struct port_ops t7xx_trace_port_ops = {
113 	.recv_skb = t7xx_trace_port_recv_skb,
114 	.uninit = t7xx_trace_port_uninit,
115 	.md_state_notify = t7xx_port_trace_md_state_notify,
116 };
117