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 converted to SG", "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_converted_sg_frames,
39 			   extras->tx_portal_busy);
40 	}
41 
42 	return 0;
43 }
44 
45 DEFINE_SHOW_ATTRIBUTE(dpaa2_dbg_cpu);
46 
47 static char *fq_type_to_str(struct dpaa2_eth_fq *fq)
48 {
49 	switch (fq->type) {
50 	case DPAA2_RX_FQ:
51 		return "Rx";
52 	case DPAA2_TX_CONF_FQ:
53 		return "Tx conf";
54 	default:
55 		return "N/A";
56 	}
57 }
58 
59 static int dpaa2_dbg_fqs_show(struct seq_file *file, void *offset)
60 {
61 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)file->private;
62 	struct dpaa2_eth_fq *fq;
63 	u32 fcnt, bcnt;
64 	int i, err;
65 
66 	seq_printf(file, "FQ stats for %s:\n", priv->net_dev->name);
67 	seq_printf(file, "%s%16s%16s%16s%16s%16s\n",
68 		   "VFQID", "CPU", "TC", "Type", "Frames", "Pending frames");
69 
70 	for (i = 0; i <  priv->num_fqs; i++) {
71 		fq = &priv->fq[i];
72 		err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
73 		if (err)
74 			fcnt = 0;
75 
76 		/* Skip FQs with no traffic */
77 		if (!fq->stats.frames && !fcnt)
78 			continue;
79 
80 		seq_printf(file, "%5d%16d%16d%16s%16llu%16u\n",
81 			   fq->fqid,
82 			   fq->target_cpu,
83 			   fq->tc,
84 			   fq_type_to_str(fq),
85 			   fq->stats.frames,
86 			   fcnt);
87 	}
88 
89 	return 0;
90 }
91 
92 DEFINE_SHOW_ATTRIBUTE(dpaa2_dbg_fqs);
93 
94 static int dpaa2_dbg_ch_show(struct seq_file *file, void *offset)
95 {
96 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)file->private;
97 	struct dpaa2_eth_channel *ch;
98 	int i;
99 
100 	seq_printf(file, "Channel stats for %s:\n", priv->net_dev->name);
101 	seq_printf(file, "%s%16s%16s%16s%16s%16s%16s\n",
102 		   "CHID", "CPU", "Deq busy", "Frames", "CDANs",
103 		   "Avg Frm/CDAN", "Buf count");
104 
105 	for (i = 0; i < priv->num_channels; i++) {
106 		ch = priv->channel[i];
107 		seq_printf(file, "%4d%16d%16llu%16llu%16llu%16llu%16d\n",
108 			   ch->ch_id,
109 			   ch->nctx.desired_cpu,
110 			   ch->stats.dequeue_portal_busy,
111 			   ch->stats.frames,
112 			   ch->stats.cdan,
113 			   div64_u64(ch->stats.frames, ch->stats.cdan),
114 			   ch->buf_count);
115 	}
116 
117 	return 0;
118 }
119 
120 DEFINE_SHOW_ATTRIBUTE(dpaa2_dbg_ch);
121 
122 void dpaa2_dbg_add(struct dpaa2_eth_priv *priv)
123 {
124 	struct fsl_mc_device *dpni_dev;
125 	struct dentry *dir;
126 	char name[10];
127 
128 	/* Create a directory for the interface */
129 	dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
130 	snprintf(name, 10, "dpni.%d", dpni_dev->obj_desc.id);
131 	dir = debugfs_create_dir(name, dpaa2_dbg_root);
132 	priv->dbg.dir = dir;
133 
134 	/* per-cpu stats file */
135 	debugfs_create_file("cpu_stats", 0444, dir, priv, &dpaa2_dbg_cpu_fops);
136 
137 	/* per-fq stats file */
138 	debugfs_create_file("fq_stats", 0444, dir, priv, &dpaa2_dbg_fqs_fops);
139 
140 	/* per-fq stats file */
141 	debugfs_create_file("ch_stats", 0444, dir, priv, &dpaa2_dbg_ch_fops);
142 }
143 
144 void dpaa2_dbg_remove(struct dpaa2_eth_priv *priv)
145 {
146 	debugfs_remove_recursive(priv->dbg.dir);
147 }
148 
149 void dpaa2_eth_dbg_init(void)
150 {
151 	dpaa2_dbg_root = debugfs_create_dir(DPAA2_ETH_DBG_ROOT, NULL);
152 	pr_debug("DPAA2-ETH: debugfs created\n");
153 }
154 
155 void dpaa2_eth_dbg_exit(void)
156 {
157 	debugfs_remove(dpaa2_dbg_root);
158 }
159