2 * Atheros AR71xx built-in ethernet mac driver
4 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * Based on Atheros' AG7100 driver
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.
14 #include <linux/debugfs.h>
18 static struct dentry
*ag71xx_debugfs_root
;
20 static int ag71xx_debugfs_generic_open(struct inode
*inode
, struct file
*file
)
22 file
->private_data
= inode
->i_private
;
26 void ag71xx_debugfs_update_int_stats(struct ag71xx
*ag
, u32 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
++;
44 static ssize_t
read_file_int_stats(struct file
*file
, char __user
*user_buf
,
45 size_t count
, loff_t
*ppos
)
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);
51 struct ag71xx
*ag
= file
->private_data
;
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
);
64 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
68 static const struct file_operations ag71xx_fops_int_stats
= {
69 .open
= ag71xx_debugfs_generic_open
,
70 .read
= read_file_int_stats
,
74 void ag71xx_debugfs_update_napi_stats(struct ag71xx
*ag
, int rx
, int tx
)
76 struct ag71xx_napi_stats
*stats
= &ag
->debug
.napi_stats
;
80 stats
->rx_packets
+= rx
;
81 if (rx
<= AG71XX_NAPI_WEIGHT
)
83 if (rx
> stats
->rx_packets_max
)
84 stats
->rx_packets_max
= rx
;
89 stats
->tx_packets
+= tx
;
90 if (tx
<= AG71XX_NAPI_WEIGHT
)
92 if (tx
> stats
->tx_packets_max
)
93 stats
->tx_packets_max
= tx
;
97 static ssize_t
read_file_napi_stats(struct file
*file
, char __user
*user_buf
,
98 size_t count
, loff_t
*ppos
)
100 struct ag71xx
*ag
= file
->private_data
;
101 struct ag71xx_napi_stats
*stats
= &ag
->debug
.napi_stats
;
104 unsigned int len
= 0;
105 unsigned long rx_avg
= 0;
106 unsigned long tx_avg
= 0;
111 buf
= kmalloc(buflen
, GFP_KERNEL
);
116 rx_avg
= stats
->rx_packets
/ stats
->rx_count
;
119 tx_avg
= stats
->tx_packets
/ stats
->tx_count
;
121 len
+= snprintf(buf
+ len
, buflen
- len
, "%3s %10s %10s\n",
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
]);
129 len
+= snprintf(buf
+ len
, buflen
- len
, "\n");
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
);
140 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
146 static const struct file_operations ag71xx_fops_napi_stats
= {
147 .open
= ag71xx_debugfs_generic_open
,
148 .read
= read_file_napi_stats
,
152 #define DESC_PRINT_LEN 64
154 static ssize_t
read_file_ring(struct file
*file
, char __user
*user_buf
,
155 size_t count
, loff_t
*ppos
,
157 struct ag71xx_ring
*ring
,
162 unsigned int len
= 0;
170 buflen
= (ring
->size
* DESC_PRINT_LEN
);
171 buf
= kmalloc(buflen
, GFP_KERNEL
);
175 len
+= snprintf(buf
+ len
, buflen
- len
,
176 "Idx ... %-8s %-8s %-8s %-8s . %-10s\n",
177 "desc", "next", "data", "ctrl", "timestamp");
179 spin_lock_irqsave(&ag
->lock
, flags
);
181 curr
= (ring
->curr
% ring
->size
);
182 dirty
= (ring
->dirty
% ring
->size
);
183 desc_hw
= ag71xx_rr(ag
, desc_reg
);
184 for (i
= 0; i
< ring
->size
; i
++) {
185 struct ag71xx_buf
*ab
= &ring
->buf
[i
];
186 u32 desc_dma
= ((u32
) ring
->descs_dma
) + i
* ring
->desc_size
;
188 len
+= snprintf(buf
+ len
, buflen
- len
,
189 "%3d %c%c%c %08x %08x %08x %08x %c %10lu\n",
191 (i
== curr
) ? 'C' : ' ',
192 (i
== dirty
) ? 'D' : ' ',
193 (desc_hw
== desc_dma
) ? 'H' : ' ',
198 (ab
->desc
->ctrl
& DESC_EMPTY
) ? 'E' : '*',
202 spin_unlock_irqrestore(&ag
->lock
, flags
);
204 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
210 static ssize_t
read_file_tx_ring(struct file
*file
, char __user
*user_buf
,
211 size_t count
, loff_t
*ppos
)
213 struct ag71xx
*ag
= file
->private_data
;
215 return read_file_ring(file
, user_buf
, count
, ppos
, ag
, &ag
->tx_ring
,
219 static const struct file_operations ag71xx_fops_tx_ring
= {
220 .open
= ag71xx_debugfs_generic_open
,
221 .read
= read_file_tx_ring
,
225 static ssize_t
read_file_rx_ring(struct file
*file
, char __user
*user_buf
,
226 size_t count
, loff_t
*ppos
)
228 struct ag71xx
*ag
= file
->private_data
;
230 return read_file_ring(file
, user_buf
, count
, ppos
, ag
, &ag
->rx_ring
,
234 static const struct file_operations ag71xx_fops_rx_ring
= {
235 .open
= ag71xx_debugfs_generic_open
,
236 .read
= read_file_rx_ring
,
240 void ag71xx_debugfs_exit(struct ag71xx
*ag
)
242 debugfs_remove_recursive(ag
->debug
.debugfs_dir
);
245 int ag71xx_debugfs_init(struct ag71xx
*ag
)
247 ag
->debug
.debugfs_dir
= debugfs_create_dir(ag
->dev
->name
,
248 ag71xx_debugfs_root
);
249 if (!ag
->debug
.debugfs_dir
)
252 debugfs_create_file("int_stats", S_IRUGO
, ag
->debug
.debugfs_dir
,
253 ag
, &ag71xx_fops_int_stats
);
254 debugfs_create_file("napi_stats", S_IRUGO
, ag
->debug
.debugfs_dir
,
255 ag
, &ag71xx_fops_napi_stats
);
256 debugfs_create_file("tx_ring", S_IRUGO
, ag
->debug
.debugfs_dir
,
257 ag
, &ag71xx_fops_tx_ring
);
258 debugfs_create_file("rx_ring", S_IRUGO
, ag
->debug
.debugfs_dir
,
259 ag
, &ag71xx_fops_rx_ring
);
264 int ag71xx_debugfs_root_init(void)
266 if (ag71xx_debugfs_root
)
269 ag71xx_debugfs_root
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
270 if (!ag71xx_debugfs_root
)
276 void ag71xx_debugfs_root_exit(void)
278 debugfs_remove(ag71xx_debugfs_root
);
279 ag71xx_debugfs_root
= NULL
;