1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2015 Freescale Semiconductor Inc.
3  * Copyright 2018-2019 NXP
4  */
5 #include <linux/module.h>
6 #include <linux/debugfs.h>
7 #include "dpaa2-eth.h"
8 #include "dpaa2-eth-debugfs.h"
9 
10 #define DPAA2_ETH_DBG_ROOT "dpaa2-eth"
11 
12 static struct dentry *dpaa2_dbg_root;
13 
14 static int dpaa2_dbg_cpu_show(struct seq_file *file, void *offset)
15 {
16 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)file->private;
17 	struct rtnl_link_stats64 *stats;
18 	struct dpaa2_eth_drv_stats *extras;
19 	int i;
20 
21 	seq_printf(file, "Per-CPU stats for %s\n", priv->net_dev->name);
22 	seq_printf(file, "%s%16s%16s%16s%16s%16s%16s%16s%16s%16s\n",
23 		   "CPU", "Rx", "Rx Err", "Rx SG", "Tx", "Tx Err", "Tx conf",
24 		   "Tx SG", "Tx realloc", "Enq busy");
25 
26 	for_each_online_cpu(i) {
27 		stats = per_cpu_ptr(priv->percpu_stats, i);
28 		extras = per_cpu_ptr(priv->percpu_extras, i);
29 		seq_printf(file, "%3d%16llu%16llu%16llu%16llu%16llu%16llu%16llu%16llu%16llu\n",
30 			   i,
31 			   stats->rx_packets,
32 			   stats->rx_errors,
33 			   extras->rx_sg_frames,
34 			   stats->tx_packets,
35 			   stats->tx_errors,
36 			   extras->tx_conf_frames,
37 			   extras->tx_sg_frames,
38 			   extras->tx_reallocs,
39 			   extras->tx_portal_busy);
40 	}
41 
42 	return 0;
43 }
44 
45 static int dpaa2_dbg_cpu_open(struct inode *inode, struct file *file)
46 {
47 	int err;
48 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)inode->i_private;
49 
50 	err = single_open(file, dpaa2_dbg_cpu_show, priv);
51 	if (err < 0)
52 		netdev_err(priv->net_dev, "single_open() failed\n");
53 
54 	return err;
55 }
56 
57 static const struct file_operations dpaa2_dbg_cpu_ops = {
58 	.open = dpaa2_dbg_cpu_open,
59 	.read = seq_read,
60 	.llseek = seq_lseek,
61 	.release = single_release,
62 };
63 
64 static char *fq_type_to_str(struct dpaa2_eth_fq *fq)
65 {
66 	switch (fq->type) {
67 	case DPAA2_RX_FQ:
68 		return "Rx";
69 	case DPAA2_TX_CONF_FQ:
70 		return "Tx conf";
71 	default:
72 		return "N/A";
73 	}
74 }
75 
76 static int dpaa2_dbg_fqs_show(struct seq_file *file, void *offset)
77 {
78 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)file->private;
79 	struct dpaa2_eth_fq *fq;
80 	u32 fcnt, bcnt;
81 	int i, err;
82 
83 	seq_printf(file, "FQ stats for %s:\n", priv->net_dev->name);
84 	seq_printf(file, "%s%16s%16s%16s%16s%16s\n",
85 		   "VFQID", "CPU", "TC", "Type", "Frames", "Pending frames");
86 
87 	for (i = 0; i <  priv->num_fqs; i++) {
88 		fq = &priv->fq[i];
89 		err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
90 		if (err)
91 			fcnt = 0;
92 
93 		seq_printf(file, "%5d%16d%16d%16s%16llu%16u\n",
94 			   fq->fqid,
95 			   fq->target_cpu,
96 			   fq->tc,
97 			   fq_type_to_str(fq),
98 			   fq->stats.frames,
99 			   fcnt);
100 	}
101 
102 	return 0;
103 }
104 
105 static int dpaa2_dbg_fqs_open(struct inode *inode, struct file *file)
106 {
107 	int err;
108 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)inode->i_private;
109 
110 	err = single_open(file, dpaa2_dbg_fqs_show, priv);
111 	if (err < 0)
112 		netdev_err(priv->net_dev, "single_open() failed\n");
113 
114 	return err;
115 }
116 
117 static const struct file_operations dpaa2_dbg_fq_ops = {
118 	.open = dpaa2_dbg_fqs_open,
119 	.read = seq_read,
120 	.llseek = seq_lseek,
121 	.release = single_release,
122 };
123 
124 static int dpaa2_dbg_ch_show(struct seq_file *file, void *offset)
125 {
126 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)file->private;
127 	struct dpaa2_eth_channel *ch;
128 	int i;
129 
130 	seq_printf(file, "Channel stats for %s:\n", priv->net_dev->name);
131 	seq_printf(file, "%s%16s%16s%16s%16s%16s%16s\n",
132 		   "CHID", "CPU", "Deq busy", "Frames", "CDANs",
133 		   "Avg Frm/CDAN", "Buf count");
134 
135 	for (i = 0; i < priv->num_channels; i++) {
136 		ch = priv->channel[i];
137 		seq_printf(file, "%4d%16d%16llu%16llu%16llu%16llu%16d\n",
138 			   ch->ch_id,
139 			   ch->nctx.desired_cpu,
140 			   ch->stats.dequeue_portal_busy,
141 			   ch->stats.frames,
142 			   ch->stats.cdan,
143 			   div64_u64(ch->stats.frames, ch->stats.cdan),
144 			   ch->buf_count);
145 	}
146 
147 	return 0;
148 }
149 
150 static int dpaa2_dbg_ch_open(struct inode *inode, struct file *file)
151 {
152 	int err;
153 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)inode->i_private;
154 
155 	err = single_open(file, dpaa2_dbg_ch_show, priv);
156 	if (err < 0)
157 		netdev_err(priv->net_dev, "single_open() failed\n");
158 
159 	return err;
160 }
161 
162 static const struct file_operations dpaa2_dbg_ch_ops = {
163 	.open = dpaa2_dbg_ch_open,
164 	.read = seq_read,
165 	.llseek = seq_lseek,
166 	.release = single_release,
167 };
168 
169 void dpaa2_dbg_add(struct dpaa2_eth_priv *priv)
170 {
171 	struct dentry *dir;
172 
173 	/* Create a directory for the interface */
174 	dir = debugfs_create_dir(priv->net_dev->name, dpaa2_dbg_root);
175 	priv->dbg.dir = dir;
176 
177 	/* per-cpu stats file */
178 	debugfs_create_file("cpu_stats", 0444, dir, priv, &dpaa2_dbg_cpu_ops);
179 
180 	/* per-fq stats file */
181 	debugfs_create_file("fq_stats", 0444, dir, priv, &dpaa2_dbg_fq_ops);
182 
183 	/* per-fq stats file */
184 	debugfs_create_file("ch_stats", 0444, dir, priv, &dpaa2_dbg_ch_ops);
185 }
186 
187 void dpaa2_dbg_remove(struct dpaa2_eth_priv *priv)
188 {
189 	debugfs_remove_recursive(priv->dbg.dir);
190 }
191 
192 void dpaa2_eth_dbg_init(void)
193 {
194 	dpaa2_dbg_root = debugfs_create_dir(DPAA2_ETH_DBG_ROOT, NULL);
195 	pr_debug("DPAA2-ETH: debugfs created\n");
196 }
197 
198 void dpaa2_eth_dbg_exit(void)
199 {
200 	debugfs_remove(dpaa2_dbg_root);
201 }
202