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