1 /*
2  * Copyright (C) 2015 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 	int fl_rd_p, fl_wr_p, rx_rd_p, rx_wr_p, rxd_cnt;
44 	struct nfp_net_r_vector *r_vec = file->private;
45 	struct nfp_net_rx_ring *rx_ring;
46 	struct nfp_net_rx_desc *rxd;
47 	struct sk_buff *skb;
48 	struct nfp_net *nn;
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 (!netif_running(nn->netdev))
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 	rx_rd_p = nfp_qcp_rd_ptr_read(rx_ring->qcp_rx);
65 	rx_wr_p = nfp_qcp_wr_ptr_read(rx_ring->qcp_rx);
66 
67 	seq_printf(file, "RX[%02d]: H_RD=%d H_WR=%d FL_RD=%d FL_WR=%d RX_RD=%d RX_WR=%d\n",
68 		   rx_ring->idx, rx_ring->rd_p, rx_ring->wr_p,
69 		   fl_rd_p, fl_wr_p, rx_rd_p, rx_wr_p);
70 
71 	for (i = 0; i < rxd_cnt; i++) {
72 		rxd = &rx_ring->rxds[i];
73 		seq_printf(file, "%04d: 0x%08x 0x%08x", i,
74 			   rxd->vals[0], rxd->vals[1]);
75 
76 		skb = READ_ONCE(rx_ring->rxbufs[i].skb);
77 		if (skb)
78 			seq_printf(file, " skb->head=%p skb->data=%p",
79 				   skb->head, skb->data);
80 
81 		if (rx_ring->rxbufs[i].dma_addr)
82 			seq_printf(file, " dma_addr=%pad",
83 				   &rx_ring->rxbufs[i].dma_addr);
84 
85 		if (i == rx_ring->rd_p % rxd_cnt)
86 			seq_puts(file, " H_RD ");
87 		if (i == rx_ring->wr_p % rxd_cnt)
88 			seq_puts(file, " H_WR ");
89 		if (i == fl_rd_p % rxd_cnt)
90 			seq_puts(file, " FL_RD");
91 		if (i == fl_wr_p % rxd_cnt)
92 			seq_puts(file, " FL_WR");
93 		if (i == rx_rd_p % rxd_cnt)
94 			seq_puts(file, " RX_RD");
95 		if (i == rx_wr_p % rxd_cnt)
96 			seq_puts(file, " RX_WR");
97 
98 		seq_putc(file, '\n');
99 	}
100 out:
101 	rtnl_unlock();
102 	return 0;
103 }
104 
105 static int nfp_net_debugfs_rx_q_open(struct inode *inode, struct file *f)
106 {
107 	return single_open(f, nfp_net_debugfs_rx_q_read, inode->i_private);
108 }
109 
110 static const struct file_operations nfp_rx_q_fops = {
111 	.owner = THIS_MODULE,
112 	.open = nfp_net_debugfs_rx_q_open,
113 	.release = single_release,
114 	.read = seq_read,
115 	.llseek = seq_lseek
116 };
117 
118 static int nfp_net_debugfs_tx_q_read(struct seq_file *file, void *data)
119 {
120 	struct nfp_net_r_vector *r_vec = file->private;
121 	struct nfp_net_tx_ring *tx_ring;
122 	struct nfp_net_tx_desc *txd;
123 	int d_rd_p, d_wr_p, txd_cnt;
124 	struct sk_buff *skb;
125 	struct nfp_net *nn;
126 	int i;
127 
128 	rtnl_lock();
129 
130 	if (!r_vec->nfp_net || !r_vec->tx_ring)
131 		goto out;
132 	nn = r_vec->nfp_net;
133 	tx_ring = r_vec->tx_ring;
134 	if (!netif_running(nn->netdev))
135 		goto out;
136 
137 	txd_cnt = tx_ring->cnt;
138 
139 	d_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
140 	d_wr_p = nfp_qcp_wr_ptr_read(tx_ring->qcp_q);
141 
142 	seq_printf(file, "TX[%02d]: H_RD=%d H_WR=%d D_RD=%d D_WR=%d\n",
143 		   tx_ring->idx, tx_ring->rd_p, tx_ring->wr_p, d_rd_p, d_wr_p);
144 
145 	for (i = 0; i < txd_cnt; i++) {
146 		txd = &tx_ring->txds[i];
147 		seq_printf(file, "%04d: 0x%08x 0x%08x 0x%08x 0x%08x", i,
148 			   txd->vals[0], txd->vals[1],
149 			   txd->vals[2], txd->vals[3]);
150 
151 		skb = READ_ONCE(tx_ring->txbufs[i].skb);
152 		if (skb)
153 			seq_printf(file, " skb->head=%p skb->data=%p",
154 				   skb->head, skb->data);
155 		if (tx_ring->txbufs[i].dma_addr)
156 			seq_printf(file, " dma_addr=%pad",
157 				   &tx_ring->txbufs[i].dma_addr);
158 
159 		if (i == tx_ring->rd_p % txd_cnt)
160 			seq_puts(file, " H_RD");
161 		if (i == tx_ring->wr_p % txd_cnt)
162 			seq_puts(file, " H_WR");
163 		if (i == d_rd_p % txd_cnt)
164 			seq_puts(file, " D_RD");
165 		if (i == d_wr_p % txd_cnt)
166 			seq_puts(file, " D_WR");
167 
168 		seq_putc(file, '\n');
169 	}
170 out:
171 	rtnl_unlock();
172 	return 0;
173 }
174 
175 static int nfp_net_debugfs_tx_q_open(struct inode *inode, struct file *f)
176 {
177 	return single_open(f, nfp_net_debugfs_tx_q_read, inode->i_private);
178 }
179 
180 static const struct file_operations nfp_tx_q_fops = {
181 	.owner = THIS_MODULE,
182 	.open = nfp_net_debugfs_tx_q_open,
183 	.release = single_release,
184 	.read = seq_read,
185 	.llseek = seq_lseek
186 };
187 
188 void nfp_net_debugfs_adapter_add(struct nfp_net *nn)
189 {
190 	struct dentry *queues, *tx, *rx;
191 	char int_name[16];
192 	int i;
193 
194 	if (IS_ERR_OR_NULL(nfp_dir))
195 		return;
196 
197 	nn->debugfs_dir = debugfs_create_dir(pci_name(nn->pdev), nfp_dir);
198 	if (IS_ERR_OR_NULL(nn->debugfs_dir))
199 		return;
200 
201 	/* Create queue debugging sub-tree */
202 	queues = debugfs_create_dir("queue", nn->debugfs_dir);
203 	if (IS_ERR_OR_NULL(queues))
204 		return;
205 
206 	rx = debugfs_create_dir("rx", queues);
207 	tx = debugfs_create_dir("tx", queues);
208 	if (IS_ERR_OR_NULL(rx) || IS_ERR_OR_NULL(tx))
209 		return;
210 
211 	for (i = 0; i < nn->num_rx_rings; i++) {
212 		sprintf(int_name, "%d", i);
213 		debugfs_create_file(int_name, S_IRUSR, rx,
214 				    &nn->r_vecs[i], &nfp_rx_q_fops);
215 	}
216 
217 	for (i = 0; i < nn->num_tx_rings; i++) {
218 		sprintf(int_name, "%d", i);
219 		debugfs_create_file(int_name, S_IRUSR, tx,
220 				    &nn->r_vecs[i], &nfp_tx_q_fops);
221 	}
222 }
223 
224 void nfp_net_debugfs_adapter_del(struct nfp_net *nn)
225 {
226 	debugfs_remove_recursive(nn->debugfs_dir);
227 	nn->debugfs_dir = NULL;
228 }
229 
230 void nfp_net_debugfs_create(void)
231 {
232 	nfp_dir = debugfs_create_dir("nfp_net", NULL);
233 }
234 
235 void nfp_net_debugfs_destroy(void)
236 {
237 	debugfs_remove_recursive(nfp_dir);
238 	nfp_dir = NULL;
239 }
240