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
12 enum 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
19 struct 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 */
44 static 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
bcmasp_stat_fixup_offset(struct bcmasp_intf * intf,const struct bcmasp_stats * s)74 static 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
bcmasp_get_sset_count(struct net_device * dev,int string_set)91 static 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
bcmasp_get_strings(struct net_device * dev,u32 stringset,u8 * data)101 static 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
bcmasp_update_mib_counters(struct bcmasp_intf * intf)119 static 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(intf->parent, offset);
135 break;
136 case BCMASP_STAT_RX_CTRL:
137 val = rx_ctrl_core_rl(intf->parent, offset);
138 break;
139 case BCMASP_STAT_RX_CTRL_PER_INTF:
140 offset += sizeof(u32) * intf->port;
141 val = rx_ctrl_core_rl(intf->parent, 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
bcmasp_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)151 static 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
bcmasp_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)168 static 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
bcmasp_get_msglevel(struct net_device * dev)176 static 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
bcmasp_set_msglevel(struct net_device * dev,u32 level)183 static 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)
bcmasp_get_wol(struct net_device * dev,struct ethtool_wolinfo * wol)191 static 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
bcmasp_set_wol(struct net_device * dev,struct ethtool_wolinfo * wol)203 static 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(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(&priv->wol_lock);
220
221 return 0;
222 }
223
bcmasp_flow_insert(struct net_device * dev,struct ethtool_rxnfc * cmd)224 static 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, &cmd->fs))
252 return -EINVAL;
253
254 nfilter = bcmasp_netfilt_get_init(intf, loc, wake, true);
255 if (IS_ERR(nfilter))
256 return PTR_ERR(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
bcmasp_flow_delete(struct net_device * dev,struct ethtool_rxnfc * cmd)268 static 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, cmd->fs.location, false, false);
274 if (IS_ERR(nfilter))
275 return PTR_ERR(nfilter);
276
277 bcmasp_netfilt_release(intf, nfilter);
278
279 return 0;
280 }
281
bcmasp_flow_get(struct bcmasp_intf * intf,struct ethtool_rxnfc * cmd)282 static 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, cmd->fs.location, false, false);
287 if (IS_ERR(nfilter))
288 return PTR_ERR(nfilter);
289
290 memcpy(&cmd->fs, &nfilter->fs, sizeof(nfilter->fs));
291
292 cmd->data = NUM_NET_FILTERS;
293
294 return 0;
295 }
296
bcmasp_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * cmd)297 static 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(&intf->parent->net_lock);
316
317 return ret;
318 }
319
bcmasp_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * cmd,u32 * rule_locs)320 static 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, &cmd->rule_cnt);
339 cmd->data = NUM_NET_FILTERS;
340 break;
341 default:
342 err = -EOPNOTSUPP;
343 break;
344 }
345
346 mutex_unlock(&intf->parent->net_lock);
347
348 return err;
349 }
350
bcmasp_eee_enable_set(struct bcmasp_intf * intf,bool enable)351 void 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, reg, UMC_EEE_CTRL);
361
362 intf->eee.eee_enabled = enable;
363 intf->eee.eee_active = enable;
364 }
365
bcmasp_get_eee(struct net_device * dev,struct ethtool_eee * e)366 static int bcmasp_get_eee(struct net_device *dev, struct ethtool_eee *e)
367 {
368 struct bcmasp_intf *intf = netdev_priv(dev);
369 struct ethtool_eee *p = &intf->eee;
370
371 if (!dev->phydev)
372 return -ENODEV;
373
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);
378
379 return phy_ethtool_get_eee(dev->phydev, e);
380 }
381
bcmasp_set_eee(struct net_device * dev,struct ethtool_eee * e)382 static int bcmasp_set_eee(struct net_device *dev, struct ethtool_eee *e)
383 {
384 struct bcmasp_intf *intf = netdev_priv(dev);
385 struct ethtool_eee *p = &intf->eee;
386 int ret;
387
388 if (!dev->phydev)
389 return -ENODEV;
390
391 if (!p->eee_enabled) {
392 bcmasp_eee_enable_set(intf, false);
393 } else {
394 ret = phy_init_eee(dev->phydev, 0);
395 if (ret) {
396 netif_err(intf, hw, dev,
397 "EEE initialization failed: %d\n", ret);
398 return ret;
399 }
400
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);
405 }
406
407 return phy_ethtool_set_eee(dev->phydev, e);
408 }
409
bcmasp_get_eth_mac_stats(struct net_device * dev,struct ethtool_eth_mac_stats * mac_stats)410 static void bcmasp_get_eth_mac_stats(struct net_device *dev,
411 struct ethtool_eth_mac_stats *mac_stats)
412 {
413 struct bcmasp_intf *intf = netdev_priv(dev);
414
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);
431 }
432
433 static const struct ethtool_rmon_hist_range bcmasp_rmon_ranges[] = {
434 { 0, 64},
435 { 65, 127},
436 { 128, 255},
437 { 256, 511},
438 { 512, 1023},
439 { 1024, 1518},
440 { 1519, 1522},
441 {}
442 };
443
bcmasp_get_rmon_stats(struct net_device * dev,struct ethtool_rmon_stats * rmon_stats,const struct ethtool_rmon_hist_range ** 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)
447 {
448 struct bcmasp_intf *intf = netdev_priv(dev);
449
450 *ranges = bcmasp_rmon_ranges;
451
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);
456
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);
464
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);
472 }
473
bcmasp_get_eth_ctrl_stats(struct net_device * dev,struct ethtool_eth_ctrl_stats * ctrl_stats)474 static void bcmasp_get_eth_ctrl_stats(struct net_device *dev,
475 struct ethtool_eth_ctrl_stats *ctrl_stats)
476 {
477 struct bcmasp_intf *intf = netdev_priv(dev);
478
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);
482 }
483
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,
503 };
504