1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) "bcmasp_ethtool: " fmt
4 #include <asm-generic/unaligned.h>
5 #include <linux/ethtool.h>
6 #include <linux/netdevice.h>
7 #include <linux/platform_device.h>
10 #include "bcmasp_intf_defs.h"
12 enum bcmasp_stat_type {
15 BCMASP_STAT_RX_CTRL_PER_INTF,
20 char stat_string[ETH_GSTRING_LEN];
21 enum bcmasp_stat_type type;
25 #define STAT_BCMASP_SOFT_MIB(str) { \
27 .type = BCMASP_STAT_SOFT, \
30 #define STAT_BCMASP_OFFSET(str, _type, offset) { \
33 .reg_offset = offset, \
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)
43 /* Must match the order of struct bcmasp_mib_counters */
44 static const struct bcmasp_stats bcmasp_gstrings_stats[] = {
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),
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"),
72 #define BCMASP_STATS_LEN ARRAY_SIZE(bcmasp_gstrings_stats)
74 static u16 bcmasp_stat_fixup_offset(struct bcmasp_intf *intf,
75 const struct bcmasp_stats *s)
77 struct bcmasp_priv *priv = intf->parent;
79 if (!strcmp("Frames Out(Buffer)", s->stat_string))
80 return priv->hw_info->rx_ctrl_fb_out_frame_count;
82 if (!strcmp("Frames Out(Filters)", s->stat_string))
83 return priv->hw_info->rx_ctrl_fb_filt_out_frame_count;
85 if (!strcmp("RX Buffer FIFO Depth", s->stat_string))
86 return priv->hw_info->rx_ctrl_fb_rx_fifo_depth;
91 static int bcmasp_get_sset_count(struct net_device *dev, int string_set)
95 return BCMASP_STATS_LEN;
101 static void bcmasp_get_strings(struct net_device *dev, u32 stringset,
108 for (i = 0; i < BCMASP_STATS_LEN; i++) {
109 memcpy(data + i * ETH_GSTRING_LEN,
110 bcmasp_gstrings_stats[i].stat_string,
119 static void bcmasp_update_mib_counters(struct bcmasp_intf *intf)
123 for (i = 0; i < BCMASP_STATS_LEN; i++) {
124 const struct bcmasp_stats *s;
128 s = &bcmasp_gstrings_stats[i];
129 offset = bcmasp_stat_fixup_offset(intf, s);
131 case BCMASP_STAT_SOFT:
133 case BCMASP_STAT_RX_EDPKT:
134 val = rx_edpkt_core_rl(intf->parent, offset);
136 case BCMASP_STAT_RX_CTRL:
137 val = rx_ctrl_core_rl(intf->parent, offset);
139 case BCMASP_STAT_RX_CTRL_PER_INTF:
140 offset += sizeof(u32) * intf->port;
141 val = rx_ctrl_core_rl(intf->parent, offset);
146 p = (char *)(&intf->mib) + (i * sizeof(u32));
147 put_unaligned(val, (u32 *)p);
151 static void bcmasp_get_ethtool_stats(struct net_device *dev,
152 struct ethtool_stats *stats,
155 struct bcmasp_intf *intf = netdev_priv(dev);
159 if (netif_running(dev))
160 bcmasp_update_mib_counters(intf);
162 for (i = 0; i < BCMASP_STATS_LEN; i++) {
163 p = (char *)(&intf->mib) + (i * sizeof(u32));
168 static void bcmasp_get_drvinfo(struct net_device *dev,
169 struct ethtool_drvinfo *info)
171 strscpy(info->driver, "bcmasp", sizeof(info->driver));
172 strscpy(info->bus_info, dev_name(dev->dev.parent),
173 sizeof(info->bus_info));
176 static u32 bcmasp_get_msglevel(struct net_device *dev)
178 struct bcmasp_intf *intf = netdev_priv(dev);
180 return intf->msg_enable;
183 static void bcmasp_set_msglevel(struct net_device *dev, u32 level)
185 struct bcmasp_intf *intf = netdev_priv(dev);
187 intf->msg_enable = level;
190 #define BCMASP_SUPPORTED_WAKE (WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER)
191 static void bcmasp_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
193 struct bcmasp_intf *intf = netdev_priv(dev);
195 wol->supported = BCMASP_SUPPORTED_WAKE;
196 wol->wolopts = intf->wolopts;
197 memset(wol->sopass, 0, sizeof(wol->sopass));
199 if (wol->wolopts & WAKE_MAGICSECURE)
200 memcpy(wol->sopass, intf->sopass, sizeof(intf->sopass));
203 static int bcmasp_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
205 struct bcmasp_intf *intf = netdev_priv(dev);
206 struct bcmasp_priv *priv = intf->parent;
207 struct device *kdev = &priv->pdev->dev;
209 if (!device_can_wakeup(kdev))
212 /* Interface Specific */
213 intf->wolopts = wol->wolopts;
214 if (intf->wolopts & WAKE_MAGICSECURE)
215 memcpy(intf->sopass, wol->sopass, sizeof(wol->sopass));
217 mutex_lock(&priv->wol_lock);
218 priv->enable_wol(intf, !!intf->wolopts);
219 mutex_unlock(&priv->wol_lock);
224 static int bcmasp_flow_insert(struct net_device *dev, struct ethtool_rxnfc *cmd)
226 struct bcmasp_intf *intf = netdev_priv(dev);
227 struct bcmasp_net_filter *nfilter;
228 u32 loc = cmd->fs.location;
231 if (cmd->fs.ring_cookie == RX_CLS_FLOW_WAKE)
234 /* Currently only supports WAKE filters */
238 switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
250 /* Check if filter already exists */
251 if (bcmasp_netfilt_check_dup(intf, &cmd->fs))
254 nfilter = bcmasp_netfilt_get_init(intf, loc, wake, true);
256 return PTR_ERR(nfilter);
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));
262 /* Since we only support wake filters, defer register programming till
268 static int bcmasp_flow_delete(struct net_device *dev, struct ethtool_rxnfc *cmd)
270 struct bcmasp_intf *intf = netdev_priv(dev);
271 struct bcmasp_net_filter *nfilter;
273 nfilter = bcmasp_netfilt_get_init(intf, cmd->fs.location, false, false);
275 return PTR_ERR(nfilter);
277 bcmasp_netfilt_release(intf, nfilter);
282 static int bcmasp_flow_get(struct bcmasp_intf *intf, struct ethtool_rxnfc *cmd)
284 struct bcmasp_net_filter *nfilter;
286 nfilter = bcmasp_netfilt_get_init(intf, cmd->fs.location, false, false);
288 return PTR_ERR(nfilter);
290 memcpy(&cmd->fs, &nfilter->fs, sizeof(nfilter->fs));
292 cmd->data = NUM_NET_FILTERS;
297 static int bcmasp_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
299 struct bcmasp_intf *intf = netdev_priv(dev);
300 int ret = -EOPNOTSUPP;
302 mutex_lock(&intf->parent->net_lock);
305 case ETHTOOL_SRXCLSRLINS:
306 ret = bcmasp_flow_insert(dev, cmd);
308 case ETHTOOL_SRXCLSRLDEL:
309 ret = bcmasp_flow_delete(dev, cmd);
315 mutex_unlock(&intf->parent->net_lock);
320 static int bcmasp_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
323 struct bcmasp_intf *intf = netdev_priv(dev);
326 mutex_lock(&intf->parent->net_lock);
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;
334 case ETHTOOL_GRXCLSRULE:
335 err = bcmasp_flow_get(intf, cmd);
337 case ETHTOOL_GRXCLSRLALL:
338 err = bcmasp_netfilt_get_all_active(intf, rule_locs, &cmd->rule_cnt);
339 cmd->data = NUM_NET_FILTERS;
346 mutex_unlock(&intf->parent->net_lock);
351 void bcmasp_eee_enable_set(struct bcmasp_intf *intf, bool enable)
355 reg = umac_rl(intf, UMC_EEE_CTRL);
360 umac_wl(intf, reg, UMC_EEE_CTRL);
362 intf->eee.eee_enabled = enable;
363 intf->eee.eee_active = enable;
366 static int bcmasp_get_eee(struct net_device *dev, struct ethtool_keee *e)
368 struct bcmasp_intf *intf = netdev_priv(dev);
369 struct ethtool_keee *p = &intf->eee;
374 e->eee_enabled = p->eee_enabled;
375 e->eee_active = p->eee_active;
376 e->tx_lpi_enabled = p->tx_lpi_enabled;
377 e->tx_lpi_timer = umac_rl(intf, UMC_EEE_LPI_TIMER);
379 return phy_ethtool_get_eee(dev->phydev, e);
382 static int bcmasp_set_eee(struct net_device *dev, struct ethtool_keee *e)
384 struct bcmasp_intf *intf = netdev_priv(dev);
385 struct ethtool_keee *p = &intf->eee;
391 if (!p->eee_enabled) {
392 bcmasp_eee_enable_set(intf, false);
394 ret = phy_init_eee(dev->phydev, 0);
396 netif_err(intf, hw, dev,
397 "EEE initialization failed: %d\n", ret);
401 umac_wl(intf, e->tx_lpi_timer, UMC_EEE_LPI_TIMER);
402 intf->eee.eee_active = ret >= 0;
403 intf->eee.tx_lpi_enabled = e->tx_lpi_enabled;
404 bcmasp_eee_enable_set(intf, true);
407 return phy_ethtool_set_eee(dev->phydev, e);
410 static void bcmasp_get_eth_mac_stats(struct net_device *dev,
411 struct ethtool_eth_mac_stats *mac_stats)
413 struct bcmasp_intf *intf = netdev_priv(dev);
415 mac_stats->FramesTransmittedOK = umac_rl(intf, UMC_GTPOK);
416 mac_stats->SingleCollisionFrames = umac_rl(intf, UMC_GTSCL);
417 mac_stats->MultipleCollisionFrames = umac_rl(intf, UMC_GTMCL);
418 mac_stats->FramesReceivedOK = umac_rl(intf, UMC_GRPOK);
419 mac_stats->FrameCheckSequenceErrors = umac_rl(intf, UMC_GRFCS);
420 mac_stats->AlignmentErrors = umac_rl(intf, UMC_GRALN);
421 mac_stats->OctetsTransmittedOK = umac_rl(intf, UMC_GTBYT);
422 mac_stats->FramesWithDeferredXmissions = umac_rl(intf, UMC_GTDRF);
423 mac_stats->LateCollisions = umac_rl(intf, UMC_GTLCL);
424 mac_stats->FramesAbortedDueToXSColls = umac_rl(intf, UMC_GTXCL);
425 mac_stats->OctetsReceivedOK = umac_rl(intf, UMC_GRBYT);
426 mac_stats->MulticastFramesXmittedOK = umac_rl(intf, UMC_GTMCA);
427 mac_stats->BroadcastFramesXmittedOK = umac_rl(intf, UMC_GTBCA);
428 mac_stats->FramesWithExcessiveDeferral = umac_rl(intf, UMC_GTEDF);
429 mac_stats->MulticastFramesReceivedOK = umac_rl(intf, UMC_GRMCA);
430 mac_stats->BroadcastFramesReceivedOK = umac_rl(intf, UMC_GRBCA);
433 static const struct ethtool_rmon_hist_range bcmasp_rmon_ranges[] = {
444 static void bcmasp_get_rmon_stats(struct net_device *dev,
445 struct ethtool_rmon_stats *rmon_stats,
446 const struct ethtool_rmon_hist_range **ranges)
448 struct bcmasp_intf *intf = netdev_priv(dev);
450 *ranges = bcmasp_rmon_ranges;
452 rmon_stats->undersize_pkts = umac_rl(intf, UMC_RRUND);
453 rmon_stats->oversize_pkts = umac_rl(intf, UMC_GROVR);
454 rmon_stats->fragments = umac_rl(intf, UMC_RRFRG);
455 rmon_stats->jabbers = umac_rl(intf, UMC_GRJBR);
457 rmon_stats->hist[0] = umac_rl(intf, UMC_GR64);
458 rmon_stats->hist[1] = umac_rl(intf, UMC_GR127);
459 rmon_stats->hist[2] = umac_rl(intf, UMC_GR255);
460 rmon_stats->hist[3] = umac_rl(intf, UMC_GR511);
461 rmon_stats->hist[4] = umac_rl(intf, UMC_GR1023);
462 rmon_stats->hist[5] = umac_rl(intf, UMC_GR1518);
463 rmon_stats->hist[6] = umac_rl(intf, UMC_GRMGV);
465 rmon_stats->hist_tx[0] = umac_rl(intf, UMC_TR64);
466 rmon_stats->hist_tx[1] = umac_rl(intf, UMC_TR127);
467 rmon_stats->hist_tx[2] = umac_rl(intf, UMC_TR255);
468 rmon_stats->hist_tx[3] = umac_rl(intf, UMC_TR511);
469 rmon_stats->hist_tx[4] = umac_rl(intf, UMC_TR1023);
470 rmon_stats->hist_tx[5] = umac_rl(intf, UMC_TR1518);
471 rmon_stats->hist_tx[6] = umac_rl(intf, UMC_TRMGV);
474 static void bcmasp_get_eth_ctrl_stats(struct net_device *dev,
475 struct ethtool_eth_ctrl_stats *ctrl_stats)
477 struct bcmasp_intf *intf = netdev_priv(dev);
479 ctrl_stats->MACControlFramesTransmitted = umac_rl(intf, UMC_GTXCF);
480 ctrl_stats->MACControlFramesReceived = umac_rl(intf, UMC_GRXCF);
481 ctrl_stats->UnsupportedOpcodesReceived = umac_rl(intf, UMC_GRXUO);
484 const struct ethtool_ops bcmasp_ethtool_ops = {
485 .get_drvinfo = bcmasp_get_drvinfo,
486 .get_link = ethtool_op_get_link,
487 .get_link_ksettings = phy_ethtool_get_link_ksettings,
488 .set_link_ksettings = phy_ethtool_set_link_ksettings,
489 .get_msglevel = bcmasp_get_msglevel,
490 .set_msglevel = bcmasp_set_msglevel,
491 .get_wol = bcmasp_get_wol,
492 .set_wol = bcmasp_set_wol,
493 .get_rxnfc = bcmasp_get_rxnfc,
494 .set_rxnfc = bcmasp_set_rxnfc,
495 .set_eee = bcmasp_set_eee,
496 .get_eee = bcmasp_get_eee,
497 .get_eth_mac_stats = bcmasp_get_eth_mac_stats,
498 .get_rmon_stats = bcmasp_get_rmon_stats,
499 .get_eth_ctrl_stats = bcmasp_get_eth_ctrl_stats,
500 .get_strings = bcmasp_get_strings,
501 .get_ethtool_stats = bcmasp_get_ethtool_stats,
502 .get_sset_count = bcmasp_get_sset_count,