1// SPDX-License-Identifier: GPL-2.0
2#define pr_fmt(fmt) "bcmasp_ethtool: " fmt
3
4#include <asm-generic/unaligned.h>
5#include <linux/ethtool.h>
6#include <linux/netdevice.h>
7#include <linux/platform_device.h>
8
9#include "bcmasp.h"
10#include "bcmasp_intf_defs.h"
11
12enum bcmasp_stat_type {
13 BCMASP_STAT_RX_EDPKT,
14 BCMASP_STAT_RX_CTRL,
15 BCMASP_STAT_RX_CTRL_PER_INTF,
16 BCMASP_STAT_SOFT,
17};
18
19struct bcmasp_stats {
20 char stat_string[ETH_GSTRING_LEN];
21 enum bcmasp_stat_type type;
22 u32 reg_offset;
23};
24
25#define STAT_BCMASP_SOFT_MIB(str) { \
26 .stat_string = str, \
27 .type = BCMASP_STAT_SOFT, \
28}
29
30#define STAT_BCMASP_OFFSET(str, _type, offset) { \
31 .stat_string = str, \
32 .type = _type, \
33 .reg_offset = offset, \
34}
35
36#define STAT_BCMASP_RX_EDPKT(str, offset) \
37 STAT_BCMASP_OFFSET(str, BCMASP_STAT_RX_EDPKT, offset)
38#define STAT_BCMASP_RX_CTRL(str, offset) \
39 STAT_BCMASP_OFFSET(str, BCMASP_STAT_RX_CTRL, offset)
40#define STAT_BCMASP_RX_CTRL_PER_INTF(str, offset) \
41 STAT_BCMASP_OFFSET(str, BCMASP_STAT_RX_CTRL_PER_INTF, offset)
42
43/* Must match the order of struct bcmasp_mib_counters */
44static const struct bcmasp_stats bcmasp_gstrings_stats[] = {
45 /* EDPKT counters */
46 STAT_BCMASP_RX_EDPKT("RX Time Stamp", ASP_EDPKT_RX_TS_COUNTER),
47 STAT_BCMASP_RX_EDPKT("RX PKT Count", ASP_EDPKT_RX_PKT_CNT),
48 STAT_BCMASP_RX_EDPKT("RX PKT Buffered", ASP_EDPKT_HDR_EXTR_CNT),
49 STAT_BCMASP_RX_EDPKT("RX PKT Pushed to DRAM", ASP_EDPKT_HDR_OUT_CNT),
50 /* ASP RX control */
51 STAT_BCMASP_RX_CTRL_PER_INTF("Frames From Unimac",
52 ASP_RX_CTRL_UMAC_0_FRAME_COUNT),
53 STAT_BCMASP_RX_CTRL_PER_INTF("Frames From Port",
54 ASP_RX_CTRL_FB_0_FRAME_COUNT),
55 STAT_BCMASP_RX_CTRL_PER_INTF("RX Buffer FIFO Depth",
56 ASP_RX_CTRL_FB_RX_FIFO_DEPTH),
57 STAT_BCMASP_RX_CTRL("Frames Out(Buffer)",
58 ASP_RX_CTRL_FB_OUT_FRAME_COUNT),
59 STAT_BCMASP_RX_CTRL("Frames Out(Filters)",
60 ASP_RX_CTRL_FB_FILT_OUT_FRAME_COUNT),
61 /* Software maintained statistics */
62 STAT_BCMASP_SOFT_MIB("RX SKB Alloc Failed"),
63 STAT_BCMASP_SOFT_MIB("TX DMA Failed"),
64 STAT_BCMASP_SOFT_MIB("Multicast Filters Full"),
65 STAT_BCMASP_SOFT_MIB("Unicast Filters Full"),
66 STAT_BCMASP_SOFT_MIB("MDA Filters Combined"),
67 STAT_BCMASP_SOFT_MIB("Promisc Filter Set"),
68 STAT_BCMASP_SOFT_MIB("TX Realloc For Offload Failed"),
69 STAT_BCMASP_SOFT_MIB("Tx Timeout Count"),
70};
71
72#define BCMASP_STATS_LEN ARRAY_SIZE(bcmasp_gstrings_stats)
73
74static u16 bcmasp_stat_fixup_offset(struct bcmasp_intf *intf,
75 const struct bcmasp_stats *s)
76{
77 struct bcmasp_priv *priv = intf->parent;
78
79 if (!strcmp("Frames Out(Buffer)", s->stat_string))
80 return priv->hw_info->rx_ctrl_fb_out_frame_count;
81
82 if (!strcmp("Frames Out(Filters)", s->stat_string))
83 return priv->hw_info->rx_ctrl_fb_filt_out_frame_count;
84
85 if (!strcmp("RX Buffer FIFO Depth", s->stat_string))
86 return priv->hw_info->rx_ctrl_fb_rx_fifo_depth;
87
88 return s->reg_offset;
89}
90
91static int bcmasp_get_sset_count(struct net_device *dev, int string_set)
92{
93 switch (string_set) {
94 case ETH_SS_STATS:
95 return BCMASP_STATS_LEN;
96 default:
97 return -EOPNOTSUPP;
98 }
99}
100
101static void bcmasp_get_strings(struct net_device *dev, u32 stringset,
102 u8 *data)
103{
104 unsigned int i;
105
106 switch (stringset) {
107 case ETH_SS_STATS:
108 for (i = 0; i < BCMASP_STATS_LEN; i++) {
109 memcpy(data + i * ETH_GSTRING_LEN,
110 bcmasp_gstrings_stats[i].stat_string,
111 ETH_GSTRING_LEN);
112 }
113 break;
114 default:
115 return;
116 }
117}
118
119static void bcmasp_update_mib_counters(struct bcmasp_intf *intf)
120{
121 unsigned int i;
122
123 for (i = 0; i < BCMASP_STATS_LEN; i++) {
124 const struct bcmasp_stats *s;
125 u32 offset, val;
126 char *p;
127
128 s = &bcmasp_gstrings_stats[i];
129 offset = bcmasp_stat_fixup_offset(intf, s);
130 switch (s->type) {
131 case BCMASP_STAT_SOFT:
132 continue;
133 case BCMASP_STAT_RX_EDPKT:
134 val = rx_edpkt_core_rl(priv: intf->parent, off: offset);
135 break;
136 case BCMASP_STAT_RX_CTRL:
137 val = rx_ctrl_core_rl(priv: intf->parent, off: offset);
138 break;
139 case BCMASP_STAT_RX_CTRL_PER_INTF:
140 offset += sizeof(u32) * intf->port;
141 val = rx_ctrl_core_rl(priv: intf->parent, off: offset);
142 break;
143 default:
144 continue;
145 }
146 p = (char *)(&intf->mib) + (i * sizeof(u32));
147 put_unaligned(val, (u32 *)p);
148 }
149}
150
151static void bcmasp_get_ethtool_stats(struct net_device *dev,
152 struct ethtool_stats *stats,
153 u64 *data)
154{
155 struct bcmasp_intf *intf = netdev_priv(dev);
156 unsigned int i;
157 char *p;
158
159 if (netif_running(dev))
160 bcmasp_update_mib_counters(intf);
161
162 for (i = 0; i < BCMASP_STATS_LEN; i++) {
163 p = (char *)(&intf->mib) + (i * sizeof(u32));
164 data[i] = *(u32 *)p;
165 }
166}
167
168static void bcmasp_get_drvinfo(struct net_device *dev,
169 struct ethtool_drvinfo *info)
170{
171 strscpy(info->driver, "bcmasp", sizeof(info->driver));
172 strscpy(info->bus_info, dev_name(dev->dev.parent),
173 sizeof(info->bus_info));
174}
175
176static u32 bcmasp_get_msglevel(struct net_device *dev)
177{
178 struct bcmasp_intf *intf = netdev_priv(dev);
179
180 return intf->msg_enable;
181}
182
183static void bcmasp_set_msglevel(struct net_device *dev, u32 level)
184{
185 struct bcmasp_intf *intf = netdev_priv(dev);
186
187 intf->msg_enable = level;
188}
189
190#define BCMASP_SUPPORTED_WAKE (WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER)
191static void bcmasp_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
192{
193 struct bcmasp_intf *intf = netdev_priv(dev);
194
195 wol->supported = BCMASP_SUPPORTED_WAKE;
196 wol->wolopts = intf->wolopts;
197 memset(wol->sopass, 0, sizeof(wol->sopass));
198
199 if (wol->wolopts & WAKE_MAGICSECURE)
200 memcpy(wol->sopass, intf->sopass, sizeof(intf->sopass));
201}
202
203static int bcmasp_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
204{
205 struct bcmasp_intf *intf = netdev_priv(dev);
206 struct bcmasp_priv *priv = intf->parent;
207 struct device *kdev = &priv->pdev->dev;
208
209 if (!device_can_wakeup(dev: kdev))
210 return -EOPNOTSUPP;
211
212 /* Interface Specific */
213 intf->wolopts = wol->wolopts;
214 if (intf->wolopts & WAKE_MAGICSECURE)
215 memcpy(intf->sopass, wol->sopass, sizeof(wol->sopass));
216
217 mutex_lock(&priv->wol_lock);
218 priv->enable_wol(intf, !!intf->wolopts);
219 mutex_unlock(lock: &priv->wol_lock);
220
221 return 0;
222}
223
224static int bcmasp_flow_insert(struct net_device *dev, struct ethtool_rxnfc *cmd)
225{
226 struct bcmasp_intf *intf = netdev_priv(dev);
227 struct bcmasp_net_filter *nfilter;
228 u32 loc = cmd->fs.location;
229 bool wake = false;
230
231 if (cmd->fs.ring_cookie == RX_CLS_FLOW_WAKE)
232 wake = true;
233
234 /* Currently only supports WAKE filters */
235 if (!wake)
236 return -EOPNOTSUPP;
237
238 switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
239 case ETHER_FLOW:
240 case IP_USER_FLOW:
241 case TCP_V4_FLOW:
242 case UDP_V4_FLOW:
243 case TCP_V6_FLOW:
244 case UDP_V6_FLOW:
245 break;
246 default:
247 return -EOPNOTSUPP;
248 }
249
250 /* Check if filter already exists */
251 if (bcmasp_netfilt_check_dup(intf, fs: &cmd->fs))
252 return -EINVAL;
253
254 nfilter = bcmasp_netfilt_get_init(intf, loc, wake_filter: wake, init: true);
255 if (IS_ERR(ptr: nfilter))
256 return PTR_ERR(ptr: nfilter);
257
258 /* Return the location where we did insert the filter */
259 cmd->fs.location = nfilter->hw_index;
260 memcpy(&nfilter->fs, &cmd->fs, sizeof(struct ethtool_rx_flow_spec));
261
262 /* Since we only support wake filters, defer register programming till
263 * suspend time.
264 */
265 return 0;
266}
267
268static int bcmasp_flow_delete(struct net_device *dev, struct ethtool_rxnfc *cmd)
269{
270 struct bcmasp_intf *intf = netdev_priv(dev);
271 struct bcmasp_net_filter *nfilter;
272
273 nfilter = bcmasp_netfilt_get_init(intf, loc: cmd->fs.location, wake_filter: false, init: false);
274 if (IS_ERR(ptr: nfilter))
275 return PTR_ERR(ptr: nfilter);
276
277 bcmasp_netfilt_release(intf, nfilt: nfilter);
278
279 return 0;
280}
281
282static int bcmasp_flow_get(struct bcmasp_intf *intf, struct ethtool_rxnfc *cmd)
283{
284 struct bcmasp_net_filter *nfilter;
285
286 nfilter = bcmasp_netfilt_get_init(intf, loc: cmd->fs.location, wake_filter: false, init: false);
287 if (IS_ERR(ptr: nfilter))
288 return PTR_ERR(ptr: nfilter);
289
290 memcpy(&cmd->fs, &nfilter->fs, sizeof(nfilter->fs));
291
292 cmd->data = NUM_NET_FILTERS;
293
294 return 0;
295}
296
297static int bcmasp_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
298{
299 struct bcmasp_intf *intf = netdev_priv(dev);
300 int ret = -EOPNOTSUPP;
301
302 mutex_lock(&intf->parent->net_lock);
303
304 switch (cmd->cmd) {
305 case ETHTOOL_SRXCLSRLINS:
306 ret = bcmasp_flow_insert(dev, cmd);
307 break;
308 case ETHTOOL_SRXCLSRLDEL:
309 ret = bcmasp_flow_delete(dev, cmd);
310 break;
311 default:
312 break;
313 }
314
315 mutex_unlock(lock: &intf->parent->net_lock);
316
317 return ret;
318}
319
320static int bcmasp_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
321 u32 *rule_locs)
322{
323 struct bcmasp_intf *intf = netdev_priv(dev);
324 int err = 0;
325
326 mutex_lock(&intf->parent->net_lock);
327
328 switch (cmd->cmd) {
329 case ETHTOOL_GRXCLSRLCNT:
330 cmd->rule_cnt = bcmasp_netfilt_get_active(intf);
331 /* We support specifying rule locations */
332 cmd->data |= RX_CLS_LOC_SPECIAL;
333 break;
334 case ETHTOOL_GRXCLSRULE:
335 err = bcmasp_flow_get(intf, cmd);
336 break;
337 case ETHTOOL_GRXCLSRLALL:
338 err = bcmasp_netfilt_get_all_active(intf, rule_locs, rule_cnt: &cmd->rule_cnt);
339 cmd->data = NUM_NET_FILTERS;
340 break;
341 default:
342 err = -EOPNOTSUPP;
343 break;
344 }
345
346 mutex_unlock(lock: &intf->parent->net_lock);
347
348 return err;
349}
350
351void bcmasp_eee_enable_set(struct bcmasp_intf *intf, bool enable)
352{
353 u32 reg;
354
355 reg = umac_rl(intf, UMC_EEE_CTRL);
356 if (enable)
357 reg |= EEE_EN;
358 else
359 reg &= ~EEE_EN;
360 umac_wl(intf, val: reg, UMC_EEE_CTRL);
361
362 intf->eee.eee_enabled = enable;
363}
364
365static int bcmasp_get_eee(struct net_device *dev, struct ethtool_keee *e)
366{
367 struct bcmasp_intf *intf = netdev_priv(dev);
368 struct ethtool_keee *p = &intf->eee;
369
370 if (!dev->phydev)
371 return -ENODEV;
372
373 e->tx_lpi_enabled = p->tx_lpi_enabled;
374 e->tx_lpi_timer = umac_rl(intf, UMC_EEE_LPI_TIMER);
375
376 return phy_ethtool_get_eee(phydev: dev->phydev, data: e);
377}
378
379static int bcmasp_set_eee(struct net_device *dev, struct ethtool_keee *e)
380{
381 struct bcmasp_intf *intf = netdev_priv(dev);
382 struct ethtool_keee *p = &intf->eee;
383 int ret;
384
385 if (!dev->phydev)
386 return -ENODEV;
387
388 if (!p->eee_enabled) {
389 bcmasp_eee_enable_set(intf, enable: false);
390 } else {
391 ret = phy_init_eee(phydev: dev->phydev, clk_stop_enable: 0);
392 if (ret) {
393 netif_err(intf, hw, dev,
394 "EEE initialization failed: %d\n", ret);
395 return ret;
396 }
397
398 umac_wl(intf, val: e->tx_lpi_timer, UMC_EEE_LPI_TIMER);
399 intf->eee.tx_lpi_enabled = e->tx_lpi_enabled;
400 bcmasp_eee_enable_set(intf, enable: true);
401 }
402
403 return phy_ethtool_set_eee(phydev: dev->phydev, data: e);
404}
405
406static void bcmasp_get_eth_mac_stats(struct net_device *dev,
407 struct ethtool_eth_mac_stats *mac_stats)
408{
409 struct bcmasp_intf *intf = netdev_priv(dev);
410
411 mac_stats->FramesTransmittedOK = umac_rl(intf, UMC_GTPOK);
412 mac_stats->SingleCollisionFrames = umac_rl(intf, UMC_GTSCL);
413 mac_stats->MultipleCollisionFrames = umac_rl(intf, UMC_GTMCL);
414 mac_stats->FramesReceivedOK = umac_rl(intf, UMC_GRPOK);
415 mac_stats->FrameCheckSequenceErrors = umac_rl(intf, UMC_GRFCS);
416 mac_stats->AlignmentErrors = umac_rl(intf, UMC_GRALN);
417 mac_stats->OctetsTransmittedOK = umac_rl(intf, UMC_GTBYT);
418 mac_stats->FramesWithDeferredXmissions = umac_rl(intf, UMC_GTDRF);
419 mac_stats->LateCollisions = umac_rl(intf, UMC_GTLCL);
420 mac_stats->FramesAbortedDueToXSColls = umac_rl(intf, UMC_GTXCL);
421 mac_stats->OctetsReceivedOK = umac_rl(intf, UMC_GRBYT);
422 mac_stats->MulticastFramesXmittedOK = umac_rl(intf, UMC_GTMCA);
423 mac_stats->BroadcastFramesXmittedOK = umac_rl(intf, UMC_GTBCA);
424 mac_stats->FramesWithExcessiveDeferral = umac_rl(intf, UMC_GTEDF);
425 mac_stats->MulticastFramesReceivedOK = umac_rl(intf, UMC_GRMCA);
426 mac_stats->BroadcastFramesReceivedOK = umac_rl(intf, UMC_GRBCA);
427}
428
429static const struct ethtool_rmon_hist_range bcmasp_rmon_ranges[] = {
430 { 0, 64},
431 { 65, 127},
432 { 128, 255},
433 { 256, 511},
434 { 512, 1023},
435 { 1024, 1518},
436 { 1519, 1522},
437 {}
438};
439
440static void bcmasp_get_rmon_stats(struct net_device *dev,
441 struct ethtool_rmon_stats *rmon_stats,
442 const struct ethtool_rmon_hist_range **ranges)
443{
444 struct bcmasp_intf *intf = netdev_priv(dev);
445
446 *ranges = bcmasp_rmon_ranges;
447
448 rmon_stats->undersize_pkts = umac_rl(intf, UMC_RRUND);
449 rmon_stats->oversize_pkts = umac_rl(intf, UMC_GROVR);
450 rmon_stats->fragments = umac_rl(intf, UMC_RRFRG);
451 rmon_stats->jabbers = umac_rl(intf, UMC_GRJBR);
452
453 rmon_stats->hist[0] = umac_rl(intf, UMC_GR64);
454 rmon_stats->hist[1] = umac_rl(intf, UMC_GR127);
455 rmon_stats->hist[2] = umac_rl(intf, UMC_GR255);
456 rmon_stats->hist[3] = umac_rl(intf, UMC_GR511);
457 rmon_stats->hist[4] = umac_rl(intf, UMC_GR1023);
458 rmon_stats->hist[5] = umac_rl(intf, UMC_GR1518);
459 rmon_stats->hist[6] = umac_rl(intf, UMC_GRMGV);
460
461 rmon_stats->hist_tx[0] = umac_rl(intf, UMC_TR64);
462 rmon_stats->hist_tx[1] = umac_rl(intf, UMC_TR127);
463 rmon_stats->hist_tx[2] = umac_rl(intf, UMC_TR255);
464 rmon_stats->hist_tx[3] = umac_rl(intf, UMC_TR511);
465 rmon_stats->hist_tx[4] = umac_rl(intf, UMC_TR1023);
466 rmon_stats->hist_tx[5] = umac_rl(intf, UMC_TR1518);
467 rmon_stats->hist_tx[6] = umac_rl(intf, UMC_TRMGV);
468}
469
470static void bcmasp_get_eth_ctrl_stats(struct net_device *dev,
471 struct ethtool_eth_ctrl_stats *ctrl_stats)
472{
473 struct bcmasp_intf *intf = netdev_priv(dev);
474
475 ctrl_stats->MACControlFramesTransmitted = umac_rl(intf, UMC_GTXCF);
476 ctrl_stats->MACControlFramesReceived = umac_rl(intf, UMC_GRXCF);
477 ctrl_stats->UnsupportedOpcodesReceived = umac_rl(intf, UMC_GRXUO);
478}
479
480const struct ethtool_ops bcmasp_ethtool_ops = {
481 .get_drvinfo = bcmasp_get_drvinfo,
482 .get_link = ethtool_op_get_link,
483 .get_link_ksettings = phy_ethtool_get_link_ksettings,
484 .set_link_ksettings = phy_ethtool_set_link_ksettings,
485 .get_msglevel = bcmasp_get_msglevel,
486 .set_msglevel = bcmasp_set_msglevel,
487 .get_wol = bcmasp_get_wol,
488 .set_wol = bcmasp_set_wol,
489 .get_rxnfc = bcmasp_get_rxnfc,
490 .set_rxnfc = bcmasp_set_rxnfc,
491 .set_eee = bcmasp_set_eee,
492 .get_eee = bcmasp_get_eee,
493 .get_eth_mac_stats = bcmasp_get_eth_mac_stats,
494 .get_rmon_stats = bcmasp_get_rmon_stats,
495 .get_eth_ctrl_stats = bcmasp_get_eth_ctrl_stats,
496 .get_strings = bcmasp_get_strings,
497 .get_ethtool_stats = bcmasp_get_ethtool_stats,
498 .get_sset_count = bcmasp_get_sset_count,
499};
500

source code of linux/drivers/net/ethernet/broadcom/asp2/bcmasp_ethtool.c