1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/debugfs.h>
21#include "fnic.h"
22
23static struct dentry *fnic_trace_debugfs_root;
24static struct dentry *fnic_trace_debugfs_file;
25static struct dentry *fnic_trace_enable;
26
27
28
29
30
31
32
33
34
35
36
37
38static int fnic_trace_ctrl_open(struct inode *inode, struct file *filp)
39{
40 filp->private_data = inode->i_private;
41 return 0;
42}
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59static ssize_t fnic_trace_ctrl_read(struct file *filp,
60 char __user *ubuf,
61 size_t cnt, loff_t *ppos)
62{
63 char buf[64];
64 int len;
65 len = sprintf(buf, "%u\n", fnic_tracing_enabled);
66
67 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
68}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84static ssize_t fnic_trace_ctrl_write(struct file *filp,
85 const char __user *ubuf,
86 size_t cnt, loff_t *ppos)
87{
88 char buf[64];
89 unsigned long val;
90 int ret;
91
92 if (cnt >= sizeof(buf))
93 return -EINVAL;
94
95 if (copy_from_user(&buf, ubuf, cnt))
96 return -EFAULT;
97
98 buf[cnt] = 0;
99
100 ret = kstrtoul(buf, 10, &val);
101 if (ret < 0)
102 return ret;
103
104 fnic_tracing_enabled = val;
105 (*ppos)++;
106
107 return cnt;
108}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125static int fnic_trace_debugfs_open(struct inode *inode,
126 struct file *file)
127{
128 fnic_dbgfs_t *fnic_dbg_prt;
129 fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
130 if (!fnic_dbg_prt)
131 return -ENOMEM;
132
133 fnic_dbg_prt->buffer = vmalloc((3*(trace_max_pages * PAGE_SIZE)));
134 if (!fnic_dbg_prt->buffer) {
135 kfree(fnic_dbg_prt);
136 return -ENOMEM;
137 }
138 memset((void *)fnic_dbg_prt->buffer, 0,
139 (3*(trace_max_pages * PAGE_SIZE)));
140 fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
141 file->private_data = fnic_dbg_prt;
142 return 0;
143}
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162static loff_t fnic_trace_debugfs_lseek(struct file *file,
163 loff_t offset,
164 int howto)
165{
166 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
167 return fixed_size_llseek(file, offset, howto,
168 fnic_dbg_prt->buffer_len);
169}
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187static ssize_t fnic_trace_debugfs_read(struct file *file,
188 char __user *ubuf,
189 size_t nbytes,
190 loff_t *pos)
191{
192 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
193 int rc = 0;
194 rc = simple_read_from_buffer(ubuf, nbytes, pos,
195 fnic_dbg_prt->buffer,
196 fnic_dbg_prt->buffer_len);
197 return rc;
198}
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213static int fnic_trace_debugfs_release(struct inode *inode,
214 struct file *file)
215{
216 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
217
218 vfree(fnic_dbg_prt->buffer);
219 kfree(fnic_dbg_prt);
220 return 0;
221}
222
223static const struct file_operations fnic_trace_ctrl_fops = {
224 .owner = THIS_MODULE,
225 .open = fnic_trace_ctrl_open,
226 .read = fnic_trace_ctrl_read,
227 .write = fnic_trace_ctrl_write,
228};
229
230static const struct file_operations fnic_trace_debugfs_fops = {
231 .owner = THIS_MODULE,
232 .open = fnic_trace_debugfs_open,
233 .llseek = fnic_trace_debugfs_lseek,
234 .read = fnic_trace_debugfs_read,
235 .release = fnic_trace_debugfs_release,
236};
237
238
239
240
241
242
243
244
245
246
247
248int fnic_trace_debugfs_init(void)
249{
250 int rc = -1;
251 fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
252 if (!fnic_trace_debugfs_root) {
253 printk(KERN_DEBUG "Cannot create debugfs root\n");
254 return rc;
255 }
256 fnic_trace_enable = debugfs_create_file("tracing_enable",
257 S_IFREG|S_IRUGO|S_IWUSR,
258 fnic_trace_debugfs_root,
259 NULL, &fnic_trace_ctrl_fops);
260
261 if (!fnic_trace_enable) {
262 printk(KERN_DEBUG "Cannot create trace_enable file"
263 " under debugfs");
264 return rc;
265 }
266
267 fnic_trace_debugfs_file = debugfs_create_file("trace",
268 S_IFREG|S_IRUGO|S_IWUSR,
269 fnic_trace_debugfs_root,
270 NULL,
271 &fnic_trace_debugfs_fops);
272
273 if (!fnic_trace_debugfs_file) {
274 printk(KERN_DEBUG "Cannot create trace file under debugfs");
275 return rc;
276 }
277 rc = 0;
278 return rc;
279}
280
281
282
283
284
285
286
287
288void fnic_trace_debugfs_terminate(void)
289{
290 if (fnic_trace_debugfs_file) {
291 debugfs_remove(fnic_trace_debugfs_file);
292 fnic_trace_debugfs_file = NULL;
293 }
294 if (fnic_trace_enable) {
295 debugfs_remove(fnic_trace_enable);
296 fnic_trace_enable = NULL;
297 }
298 if (fnic_trace_debugfs_root) {
299 debugfs_remove(fnic_trace_debugfs_root);
300 fnic_trace_debugfs_root = NULL;
301 }
302}
303