ath79: use downstream ag71xx for Kernel 5.4
[openwrt/openwrt.git] / target / linux / ath79 / files / drivers / net / ethernet / atheros / ag71xx / ag71xx_debugfs.c
1 /*
2 * Atheros AR71xx built-in ethernet mac driver
3 *
4 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6 *
7 * Based on Atheros' AG7100 driver
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
12 */
13
14 #include <linux/debugfs.h>
15
16 #include "ag71xx.h"
17
18 static struct dentry *ag71xx_debugfs_root;
19
20 static int ag71xx_debugfs_generic_open(struct inode *inode, struct file *file)
21 {
22 file->private_data = inode->i_private;
23 return 0;
24 }
25
26 void ag71xx_debugfs_update_int_stats(struct ag71xx *ag, u32 status)
27 {
28 if (status)
29 ag->debug.int_stats.total++;
30 if (status & AG71XX_INT_TX_PS)
31 ag->debug.int_stats.tx_ps++;
32 if (status & AG71XX_INT_TX_UR)
33 ag->debug.int_stats.tx_ur++;
34 if (status & AG71XX_INT_TX_BE)
35 ag->debug.int_stats.tx_be++;
36 if (status & AG71XX_INT_RX_PR)
37 ag->debug.int_stats.rx_pr++;
38 if (status & AG71XX_INT_RX_OF)
39 ag->debug.int_stats.rx_of++;
40 if (status & AG71XX_INT_RX_BE)
41 ag->debug.int_stats.rx_be++;
42 }
43
44 static ssize_t read_file_int_stats(struct file *file, char __user *user_buf,
45 size_t count, loff_t *ppos)
46 {
47 #define PR_INT_STAT(_label, _field) \
48 len += snprintf(buf + len, sizeof(buf) - len, \
49 "%20s: %10lu\n", _label, ag->debug.int_stats._field);
50
51 struct ag71xx *ag = file->private_data;
52 char buf[256];
53 unsigned int len = 0;
54
55 PR_INT_STAT("TX Packet Sent", tx_ps);
56 PR_INT_STAT("TX Underrun", tx_ur);
57 PR_INT_STAT("TX Bus Error", tx_be);
58 PR_INT_STAT("RX Packet Received", rx_pr);
59 PR_INT_STAT("RX Overflow", rx_of);
60 PR_INT_STAT("RX Bus Error", rx_be);
61 len += snprintf(buf + len, sizeof(buf) - len, "\n");
62 PR_INT_STAT("Total", total);
63
64 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
65 #undef PR_INT_STAT
66 }
67
68 static const struct file_operations ag71xx_fops_int_stats = {
69 .open = ag71xx_debugfs_generic_open,
70 .read = read_file_int_stats,
71 .owner = THIS_MODULE
72 };
73
74 void ag71xx_debugfs_update_napi_stats(struct ag71xx *ag, int rx, int tx)
75 {
76 struct ag71xx_napi_stats *stats = &ag->debug.napi_stats;
77
78 if (rx) {
79 stats->rx_count++;
80 stats->rx_packets += rx;
81 if (rx <= AG71XX_NAPI_WEIGHT)
82 stats->rx[rx]++;
83 if (rx > stats->rx_packets_max)
84 stats->rx_packets_max = rx;
85 }
86
87 if (tx) {
88 stats->tx_count++;
89 stats->tx_packets += tx;
90 if (tx <= AG71XX_NAPI_WEIGHT)
91 stats->tx[tx]++;
92 if (tx > stats->tx_packets_max)
93 stats->tx_packets_max = tx;
94 }
95 }
96
97 static ssize_t read_file_napi_stats(struct file *file, char __user *user_buf,
98 size_t count, loff_t *ppos)
99 {
100 struct ag71xx *ag = file->private_data;
101 struct ag71xx_napi_stats *stats = &ag->debug.napi_stats;
102 char *buf;
103 unsigned int buflen;
104 unsigned int len = 0;
105 unsigned long rx_avg = 0;
106 unsigned long tx_avg = 0;
107 int ret;
108 int i;
109
110 buflen = 2048;
111 buf = kmalloc(buflen, GFP_KERNEL);
112 if (!buf)
113 return -ENOMEM;
114
115 if (stats->rx_count)
116 rx_avg = stats->rx_packets / stats->rx_count;
117
118 if (stats->tx_count)
119 tx_avg = stats->tx_packets / stats->tx_count;
120
121 len += snprintf(buf + len, buflen - len, "%3s %10s %10s\n",
122 "len", "rx", "tx");
123
124 for (i = 1; i <= AG71XX_NAPI_WEIGHT; i++)
125 len += snprintf(buf + len, buflen - len,
126 "%3d: %10lu %10lu\n",
127 i, stats->rx[i], stats->tx[i]);
128
129 len += snprintf(buf + len, buflen - len, "\n");
130
131 len += snprintf(buf + len, buflen - len, "%3s: %10lu %10lu\n",
132 "sum", stats->rx_count, stats->tx_count);
133 len += snprintf(buf + len, buflen - len, "%3s: %10lu %10lu\n",
134 "avg", rx_avg, tx_avg);
135 len += snprintf(buf + len, buflen - len, "%3s: %10lu %10lu\n",
136 "max", stats->rx_packets_max, stats->tx_packets_max);
137 len += snprintf(buf + len, buflen - len, "%3s: %10lu %10lu\n",
138 "pkt", stats->rx_packets, stats->tx_packets);
139
140 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
141 kfree(buf);
142
143 return ret;
144 }
145
146 static const struct file_operations ag71xx_fops_napi_stats = {
147 .open = ag71xx_debugfs_generic_open,
148 .read = read_file_napi_stats,
149 .owner = THIS_MODULE
150 };
151
152 #define DESC_PRINT_LEN 64
153
154 static ssize_t read_file_ring(struct file *file, char __user *user_buf,
155 size_t count, loff_t *ppos,
156 struct ag71xx *ag,
157 struct ag71xx_ring *ring,
158 unsigned desc_reg)
159 {
160 int ring_size = BIT(ring->order);
161 int ring_mask = ring_size - 1;
162 char *buf;
163 unsigned int buflen;
164 unsigned int len = 0;
165 unsigned long flags;
166 ssize_t ret;
167 int curr;
168 int dirty;
169 u32 desc_hw;
170 int i;
171
172 buflen = (ring_size * DESC_PRINT_LEN);
173 buf = kmalloc(buflen, GFP_KERNEL);
174 if (!buf)
175 return -ENOMEM;
176
177 len += snprintf(buf + len, buflen - len,
178 "Idx ... %-8s %-8s %-8s %-8s .\n",
179 "desc", "next", "data", "ctrl");
180
181 spin_lock_irqsave(&ag->lock, flags);
182
183 curr = (ring->curr & ring_mask);
184 dirty = (ring->dirty & ring_mask);
185 desc_hw = ag71xx_rr(ag, desc_reg);
186 for (i = 0; i < ring_size; i++) {
187 struct ag71xx_desc *desc = ag71xx_ring_desc(ring, i);
188 u32 desc_dma = ((u32) ring->descs_dma) + i * AG71XX_DESC_SIZE;
189
190 len += snprintf(buf + len, buflen - len,
191 "%3d %c%c%c %08x %08x %08x %08x %c\n",
192 i,
193 (i == curr) ? 'C' : ' ',
194 (i == dirty) ? 'D' : ' ',
195 (desc_hw == desc_dma) ? 'H' : ' ',
196 desc_dma,
197 desc->next,
198 desc->data,
199 desc->ctrl,
200 (desc->ctrl & DESC_EMPTY) ? 'E' : '*');
201 }
202
203 spin_unlock_irqrestore(&ag->lock, flags);
204
205 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
206 kfree(buf);
207
208 return ret;
209 }
210
211 static ssize_t read_file_tx_ring(struct file *file, char __user *user_buf,
212 size_t count, loff_t *ppos)
213 {
214 struct ag71xx *ag = file->private_data;
215
216 return read_file_ring(file, user_buf, count, ppos, ag, &ag->tx_ring,
217 AG71XX_REG_TX_DESC);
218 }
219
220 static const struct file_operations ag71xx_fops_tx_ring = {
221 .open = ag71xx_debugfs_generic_open,
222 .read = read_file_tx_ring,
223 .owner = THIS_MODULE
224 };
225
226 static ssize_t read_file_rx_ring(struct file *file, char __user *user_buf,
227 size_t count, loff_t *ppos)
228 {
229 struct ag71xx *ag = file->private_data;
230
231 return read_file_ring(file, user_buf, count, ppos, ag, &ag->rx_ring,
232 AG71XX_REG_RX_DESC);
233 }
234
235 static const struct file_operations ag71xx_fops_rx_ring = {
236 .open = ag71xx_debugfs_generic_open,
237 .read = read_file_rx_ring,
238 .owner = THIS_MODULE
239 };
240
241 void ag71xx_debugfs_exit(struct ag71xx *ag)
242 {
243 debugfs_remove_recursive(ag->debug.debugfs_dir);
244 }
245
246 int ag71xx_debugfs_init(struct ag71xx *ag)
247 {
248 struct device *dev = &ag->pdev->dev;
249
250 ag->debug.debugfs_dir = debugfs_create_dir(dev_name(dev),
251 ag71xx_debugfs_root);
252 if (!ag->debug.debugfs_dir) {
253 dev_err(dev, "unable to create debugfs directory\n");
254 return -ENOENT;
255 }
256
257 debugfs_create_file("int_stats", S_IRUGO, ag->debug.debugfs_dir,
258 ag, &ag71xx_fops_int_stats);
259 debugfs_create_file("napi_stats", S_IRUGO, ag->debug.debugfs_dir,
260 ag, &ag71xx_fops_napi_stats);
261 debugfs_create_file("tx_ring", S_IRUGO, ag->debug.debugfs_dir,
262 ag, &ag71xx_fops_tx_ring);
263 debugfs_create_file("rx_ring", S_IRUGO, ag->debug.debugfs_dir,
264 ag, &ag71xx_fops_rx_ring);
265
266 return 0;
267 }
268
269 int ag71xx_debugfs_root_init(void)
270 {
271 if (ag71xx_debugfs_root)
272 return -EBUSY;
273
274 ag71xx_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
275 if (!ag71xx_debugfs_root)
276 return -ENOENT;
277
278 return 0;
279 }
280
281 void ag71xx_debugfs_root_exit(void)
282 {
283 debugfs_remove(ag71xx_debugfs_root);
284 ag71xx_debugfs_root = NULL;
285 }