xref: /linux/drivers/net/ethernet/intel/ixgbe/ixgbe_debugfs.c (revision a58130ddc896e5a15e4de2bf50a1d89247118c23)
1 /*******************************************************************************
2 
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2012 Intel Corporation.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9 
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14 
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21 
22   Contact Information:
23   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 
26 *******************************************************************************/
27 
28 #ifdef CONFIG_DEBUG_FS
29 
30 #include <linux/debugfs.h>
31 #include <linux/module.h>
32 
33 #include "ixgbe.h"
34 
35 static struct dentry *ixgbe_dbg_root;
36 
37 static char ixgbe_dbg_reg_ops_buf[256] = "";
38 
39 /**
40  * ixgbe_dbg_reg_ops_read - read for reg_ops datum
41  * @filp: the opened file
42  * @buffer: where to write the data for the user to read
43  * @count: the size of the user's buffer
44  * @ppos: file position offset
45  **/
46 static ssize_t ixgbe_dbg_reg_ops_read(struct file *filp, char __user *buffer,
47 				    size_t count, loff_t *ppos)
48 {
49 	struct ixgbe_adapter *adapter = filp->private_data;
50 	char *buf;
51 	int len;
52 
53 	/* don't allow partial reads */
54 	if (*ppos != 0)
55 		return 0;
56 
57 	buf = kasprintf(GFP_KERNEL, "%s: %s\n",
58 			adapter->netdev->name,
59 			ixgbe_dbg_reg_ops_buf);
60 	if (!buf)
61 		return -ENOMEM;
62 
63 	if (count < strlen(buf)) {
64 		kfree(buf);
65 		return -ENOSPC;
66 	}
67 
68 	len = simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
69 
70 	kfree(buf);
71 	return len;
72 }
73 
74 /**
75  * ixgbe_dbg_reg_ops_write - write into reg_ops datum
76  * @filp: the opened file
77  * @buffer: where to find the user's data
78  * @count: the length of the user's data
79  * @ppos: file position offset
80  **/
81 static ssize_t ixgbe_dbg_reg_ops_write(struct file *filp,
82 				     const char __user *buffer,
83 				     size_t count, loff_t *ppos)
84 {
85 	struct ixgbe_adapter *adapter = filp->private_data;
86 	int len;
87 
88 	/* don't allow partial writes */
89 	if (*ppos != 0)
90 		return 0;
91 	if (count >= sizeof(ixgbe_dbg_reg_ops_buf))
92 		return -ENOSPC;
93 
94 	len = simple_write_to_buffer(ixgbe_dbg_reg_ops_buf,
95 				     sizeof(ixgbe_dbg_reg_ops_buf)-1,
96 				     ppos,
97 				     buffer,
98 				     count);
99 	if (len < 0)
100 		return len;
101 
102 	ixgbe_dbg_reg_ops_buf[len] = '\0';
103 
104 	if (strncmp(ixgbe_dbg_reg_ops_buf, "write", 5) == 0) {
105 		u32 reg, value;
106 		int cnt;
107 		cnt = sscanf(&ixgbe_dbg_reg_ops_buf[5], "%x %x", &reg, &value);
108 		if (cnt == 2) {
109 			IXGBE_WRITE_REG(&adapter->hw, reg, value);
110 			value = IXGBE_READ_REG(&adapter->hw, reg);
111 			e_dev_info("write: 0x%08x = 0x%08x\n", reg, value);
112 		} else {
113 			e_dev_info("write <reg> <value>\n");
114 		}
115 	} else if (strncmp(ixgbe_dbg_reg_ops_buf, "read", 4) == 0) {
116 		u32 reg, value;
117 		int cnt;
118 		cnt = sscanf(&ixgbe_dbg_reg_ops_buf[4], "%x", &reg);
119 		if (cnt == 1) {
120 			value = IXGBE_READ_REG(&adapter->hw, reg);
121 			e_dev_info("read 0x%08x = 0x%08x\n", reg, value);
122 		} else {
123 			e_dev_info("read <reg>\n");
124 		}
125 	} else {
126 		e_dev_info("Unknown command %s\n", ixgbe_dbg_reg_ops_buf);
127 		e_dev_info("Available commands:\n");
128 		e_dev_info("   read <reg>\n");
129 		e_dev_info("   write <reg> <value>\n");
130 	}
131 	return count;
132 }
133 
134 static const struct file_operations ixgbe_dbg_reg_ops_fops = {
135 	.owner = THIS_MODULE,
136 	.open = simple_open,
137 	.read =  ixgbe_dbg_reg_ops_read,
138 	.write = ixgbe_dbg_reg_ops_write,
139 };
140 
141 static char ixgbe_dbg_netdev_ops_buf[256] = "";
142 
143 /**
144  * ixgbe_dbg_netdev_ops_read - read for netdev_ops datum
145  * @filp: the opened file
146  * @buffer: where to write the data for the user to read
147  * @count: the size of the user's buffer
148  * @ppos: file position offset
149  **/
150 static ssize_t ixgbe_dbg_netdev_ops_read(struct file *filp,
151 					 char __user *buffer,
152 					 size_t count, loff_t *ppos)
153 {
154 	struct ixgbe_adapter *adapter = filp->private_data;
155 	char *buf;
156 	int len;
157 
158 	/* don't allow partial reads */
159 	if (*ppos != 0)
160 		return 0;
161 
162 	buf = kasprintf(GFP_KERNEL, "%s: %s\n",
163 			adapter->netdev->name,
164 			ixgbe_dbg_netdev_ops_buf);
165 	if (!buf)
166 		return -ENOMEM;
167 
168 	if (count < strlen(buf)) {
169 		kfree(buf);
170 		return -ENOSPC;
171 	}
172 
173 	len = simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
174 
175 	kfree(buf);
176 	return len;
177 }
178 
179 /**
180  * ixgbe_dbg_netdev_ops_write - write into netdev_ops datum
181  * @filp: the opened file
182  * @buffer: where to find the user's data
183  * @count: the length of the user's data
184  * @ppos: file position offset
185  **/
186 static ssize_t ixgbe_dbg_netdev_ops_write(struct file *filp,
187 					  const char __user *buffer,
188 					  size_t count, loff_t *ppos)
189 {
190 	struct ixgbe_adapter *adapter = filp->private_data;
191 	int len;
192 
193 	/* don't allow partial writes */
194 	if (*ppos != 0)
195 		return 0;
196 	if (count >= sizeof(ixgbe_dbg_netdev_ops_buf))
197 		return -ENOSPC;
198 
199 	len = simple_write_to_buffer(ixgbe_dbg_netdev_ops_buf,
200 				     sizeof(ixgbe_dbg_netdev_ops_buf)-1,
201 				     ppos,
202 				     buffer,
203 				     count);
204 	if (len < 0)
205 		return len;
206 
207 	ixgbe_dbg_netdev_ops_buf[len] = '\0';
208 
209 	if (strncmp(ixgbe_dbg_netdev_ops_buf, "tx_timeout", 10) == 0) {
210 		adapter->netdev->netdev_ops->ndo_tx_timeout(adapter->netdev);
211 		e_dev_info("tx_timeout called\n");
212 	} else {
213 		e_dev_info("Unknown command: %s\n", ixgbe_dbg_netdev_ops_buf);
214 		e_dev_info("Available commands:\n");
215 		e_dev_info("    tx_timeout\n");
216 	}
217 	return count;
218 }
219 
220 static const struct file_operations ixgbe_dbg_netdev_ops_fops = {
221 	.owner = THIS_MODULE,
222 	.open = simple_open,
223 	.read = ixgbe_dbg_netdev_ops_read,
224 	.write = ixgbe_dbg_netdev_ops_write,
225 };
226 
227 /**
228  * ixgbe_dbg_adapter_init - setup the debugfs directory for the adapter
229  * @adapter: the adapter that is starting up
230  **/
231 void ixgbe_dbg_adapter_init(struct ixgbe_adapter *adapter)
232 {
233 	const char *name = pci_name(adapter->pdev);
234 	struct dentry *pfile;
235 	adapter->ixgbe_dbg_adapter = debugfs_create_dir(name, ixgbe_dbg_root);
236 	if (adapter->ixgbe_dbg_adapter) {
237 		pfile = debugfs_create_file("reg_ops", 0600,
238 					    adapter->ixgbe_dbg_adapter, adapter,
239 					    &ixgbe_dbg_reg_ops_fops);
240 		if (!pfile)
241 			e_dev_err("debugfs reg_ops for %s failed\n", name);
242 		pfile = debugfs_create_file("netdev_ops", 0600,
243 					    adapter->ixgbe_dbg_adapter, adapter,
244 					    &ixgbe_dbg_netdev_ops_fops);
245 		if (!pfile)
246 			e_dev_err("debugfs netdev_ops for %s failed\n", name);
247 	} else {
248 		e_dev_err("debugfs entry for %s failed\n", name);
249 	}
250 }
251 
252 /**
253  * ixgbe_dbg_adapter_exit - clear out the adapter's debugfs entries
254  * @pf: the pf that is stopping
255  **/
256 void ixgbe_dbg_adapter_exit(struct ixgbe_adapter *adapter)
257 {
258 	if (adapter->ixgbe_dbg_adapter)
259 		debugfs_remove_recursive(adapter->ixgbe_dbg_adapter);
260 	adapter->ixgbe_dbg_adapter = NULL;
261 }
262 
263 /**
264  * ixgbe_dbg_init - start up debugfs for the driver
265  **/
266 void ixgbe_dbg_init(void)
267 {
268 	ixgbe_dbg_root = debugfs_create_dir(ixgbe_driver_name, NULL);
269 	if (ixgbe_dbg_root == NULL)
270 		pr_err("init of debugfs failed\n");
271 }
272 
273 /**
274  * ixgbe_dbg_exit - clean out the driver's debugfs entries
275  **/
276 void ixgbe_dbg_exit(void)
277 {
278 	debugfs_remove_recursive(ixgbe_dbg_root);
279 }
280 
281 #endif /* CONFIG_DEBUG_FS */
282