1 /*
2  * Copyright (C) 2015-2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 #include <linux/debugfs.h>
34 #include <linux/module.h>
35 #include <linux/rtnetlink.h>
36 
37 #include "nfp_net.h"
38 
39 static struct dentry *nfp_dir;
40 
41 static int nfp_net_debugfs_rx_q_read(struct seq_file *file, void *data)
42 {
43 	struct nfp_net_r_vector *r_vec = file->private;
44 	struct nfp_net_rx_ring *rx_ring;
45 	int fl_rd_p, fl_wr_p, rxd_cnt;
46 	struct nfp_net_rx_desc *rxd;
47 	struct nfp_net *nn;
48 	void *frag;
49 	int i;
50 
51 	rtnl_lock();
52 
53 	if (!r_vec->nfp_net || !r_vec->rx_ring)
54 		goto out;
55 	nn = r_vec->nfp_net;
56 	rx_ring = r_vec->rx_ring;
57 	if (!nfp_net_running(nn))
58 		goto out;
59 
60 	rxd_cnt = rx_ring->cnt;
61 
62 	fl_rd_p = nfp_qcp_rd_ptr_read(rx_ring->qcp_fl);
63 	fl_wr_p = nfp_qcp_wr_ptr_read(rx_ring->qcp_fl);
64 
65 	seq_printf(file, "RX[%02d,%02d]: cnt=%u dma=%pad host=%p   H_RD=%u H_WR=%u FL_RD=%u FL_WR=%u\n",
66 		   rx_ring->idx, rx_ring->fl_qcidx,
67 		   rx_ring->cnt, &rx_ring->dma, rx_ring->rxds,
68 		   rx_ring->rd_p, rx_ring->wr_p, fl_rd_p, fl_wr_p);
69 
70 	for (i = 0; i < rxd_cnt; i++) {
71 		rxd = &rx_ring->rxds[i];
72 		seq_printf(file, "%04d: 0x%08x 0x%08x", i,
73 			   rxd->vals[0], rxd->vals[1]);
74 
75 		frag = READ_ONCE(rx_ring->rxbufs[i].frag);
76 		if (frag)
77 			seq_printf(file, " frag=%p", frag);
78 
79 		if (rx_ring->rxbufs[i].dma_addr)
80 			seq_printf(file, " dma_addr=%pad",
81 				   &rx_ring->rxbufs[i].dma_addr);
82 
83 		if (i == rx_ring->rd_p % rxd_cnt)
84 			seq_puts(file, " H_RD ");
85 		if (i == rx_ring->wr_p % rxd_cnt)
86 			seq_puts(file, " H_WR ");
87 		if (i == fl_rd_p % rxd_cnt)
88 			seq_puts(file, " FL_RD");
89 		if (i == fl_wr_p % rxd_cnt)
90 			seq_puts(file, " FL_WR");
91 
92 		seq_putc(file, '\n');
93 	}
94 out:
95 	rtnl_unlock();
96 	return 0;
97 }
98 
99 static int nfp_net_debugfs_rx_q_open(struct inode *inode, struct file *f)
100 {
101 	return single_open(f, nfp_net_debugfs_rx_q_read, inode->i_private);
102 }
103 
104 static const struct file_operations nfp_rx_q_fops = {
105 	.owner = THIS_MODULE,
106 	.open = nfp_net_debugfs_rx_q_open,
107 	.release = single_release,
108 	.read = seq_read,
109 	.llseek = seq_lseek
110 };
111 
112 static int nfp_net_debugfs_tx_q_open(struct inode *inode, struct file *f);
113 
114 static const struct file_operations nfp_tx_q_fops = {
115 	.owner = THIS_MODULE,
116 	.open = nfp_net_debugfs_tx_q_open,
117 	.release = single_release,
118 	.read = seq_read,
119 	.llseek = seq_lseek
120 };
121 
122 static int nfp_net_debugfs_tx_q_read(struct seq_file *file, void *data)
123 {
124 	struct nfp_net_r_vector *r_vec = file->private;
125 	struct nfp_net_tx_ring *tx_ring;
126 	struct nfp_net_tx_desc *txd;
127 	int d_rd_p, d_wr_p, txd_cnt;
128 	struct sk_buff *skb;
129 	struct nfp_net *nn;
130 	int i;
131 
132 	rtnl_lock();
133 
134 	if (debugfs_real_fops(file->file) == &nfp_tx_q_fops)
135 		tx_ring = r_vec->tx_ring;
136 	else
137 		tx_ring = r_vec->xdp_ring;
138 	if (!r_vec->nfp_net || !tx_ring)
139 		goto out;
140 	nn = r_vec->nfp_net;
141 	if (!nfp_net_running(nn))
142 		goto out;
143 
144 	txd_cnt = tx_ring->cnt;
145 
146 	d_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
147 	d_wr_p = nfp_qcp_wr_ptr_read(tx_ring->qcp_q);
148 
149 	seq_printf(file, "TX[%02d,%02d%s]: cnt=%u dma=%pad host=%p   H_RD=%u H_WR=%u D_RD=%u D_WR=%u\n",
150 		   tx_ring->idx, tx_ring->qcidx,
151 		   tx_ring == r_vec->tx_ring ? "" : "xdp",
152 		   tx_ring->cnt, &tx_ring->dma, tx_ring->txds,
153 		   tx_ring->rd_p, tx_ring->wr_p, d_rd_p, d_wr_p);
154 
155 	for (i = 0; i < txd_cnt; i++) {
156 		txd = &tx_ring->txds[i];
157 		seq_printf(file, "%04d: 0x%08x 0x%08x 0x%08x 0x%08x", i,
158 			   txd->vals[0], txd->vals[1],
159 			   txd->vals[2], txd->vals[3]);
160 
161 		skb = READ_ONCE(tx_ring->txbufs[i].skb);
162 		if (skb) {
163 			if (tx_ring == r_vec->tx_ring)
164 				seq_printf(file, " skb->head=%p skb->data=%p",
165 					   skb->head, skb->data);
166 			else
167 				seq_printf(file, " frag=%p", skb);
168 		}
169 
170 		if (tx_ring->txbufs[i].dma_addr)
171 			seq_printf(file, " dma_addr=%pad",
172 				   &tx_ring->txbufs[i].dma_addr);
173 
174 		if (i == tx_ring->rd_p % txd_cnt)
175 			seq_puts(file, " H_RD");
176 		if (i == tx_ring->wr_p % txd_cnt)
177 			seq_puts(file, " H_WR");
178 		if (i == d_rd_p % txd_cnt)
179 			seq_puts(file, " D_RD");
180 		if (i == d_wr_p % txd_cnt)
181 			seq_puts(file, " D_WR");
182 
183 		seq_putc(file, '\n');
184 	}
185 out:
186 	rtnl_unlock();
187 	return 0;
188 }
189 
190 static int nfp_net_debugfs_tx_q_open(struct inode *inode, struct file *f)
191 {
192 	return single_open(f, nfp_net_debugfs_tx_q_read, inode->i_private);
193 }
194 
195 static const struct file_operations nfp_xdp_q_fops = {
196 	.owner = THIS_MODULE,
197 	.open = nfp_net_debugfs_tx_q_open,
198 	.release = single_release,
199 	.read = seq_read,
200 	.llseek = seq_lseek
201 };
202 
203 void nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir, int id)
204 {
205 	struct dentry *queues, *tx, *rx, *xdp;
206 	char name[20];
207 	int i;
208 
209 	if (IS_ERR_OR_NULL(nfp_dir))
210 		return;
211 
212 	if (nfp_net_is_data_vnic(nn))
213 		sprintf(name, "vnic%d", id);
214 	else
215 		strcpy(name, "ctrl-vnic");
216 	nn->debugfs_dir = debugfs_create_dir(name, ddir);
217 	if (IS_ERR_OR_NULL(nn->debugfs_dir))
218 		return;
219 
220 	/* Create queue debugging sub-tree */
221 	queues = debugfs_create_dir("queue", nn->debugfs_dir);
222 	if (IS_ERR_OR_NULL(queues))
223 		return;
224 
225 	rx = debugfs_create_dir("rx", queues);
226 	tx = debugfs_create_dir("tx", queues);
227 	xdp = debugfs_create_dir("xdp", queues);
228 	if (IS_ERR_OR_NULL(rx) || IS_ERR_OR_NULL(tx) || IS_ERR_OR_NULL(xdp))
229 		return;
230 
231 	for (i = 0; i < min(nn->max_rx_rings, nn->max_r_vecs); i++) {
232 		sprintf(name, "%d", i);
233 		debugfs_create_file(name, S_IRUSR, rx,
234 				    &nn->r_vecs[i], &nfp_rx_q_fops);
235 		debugfs_create_file(name, S_IRUSR, xdp,
236 				    &nn->r_vecs[i], &nfp_xdp_q_fops);
237 	}
238 
239 	for (i = 0; i < min(nn->max_tx_rings, nn->max_r_vecs); i++) {
240 		sprintf(name, "%d", i);
241 		debugfs_create_file(name, S_IRUSR, tx,
242 				    &nn->r_vecs[i], &nfp_tx_q_fops);
243 	}
244 }
245 
246 struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev)
247 {
248 	struct dentry *dev_dir;
249 
250 	if (IS_ERR_OR_NULL(nfp_dir))
251 		return NULL;
252 
253 	dev_dir = debugfs_create_dir(pci_name(pdev), nfp_dir);
254 	if (IS_ERR_OR_NULL(dev_dir))
255 		return NULL;
256 
257 	return dev_dir;
258 }
259 
260 void nfp_net_debugfs_dir_clean(struct dentry **dir)
261 {
262 	debugfs_remove_recursive(*dir);
263 	*dir = NULL;
264 }
265 
266 void nfp_net_debugfs_create(void)
267 {
268 	nfp_dir = debugfs_create_dir("nfp_net", NULL);
269 }
270 
271 void nfp_net_debugfs_destroy(void)
272 {
273 	debugfs_remove_recursive(nfp_dir);
274 	nfp_dir = NULL;
275 }
276