1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) "bcmasp_ethtool: " fmt
3
4 #include <linux/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 const char *str;
105 unsigned int i;
106
107 switch (stringset) {
108 case ETH_SS_STATS:
109 for (i = 0; i < BCMASP_STATS_LEN; i++) {
110 str = bcmasp_gstrings_stats[i].stat_string;
111 ethtool_puts(&data, str);
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_get_eee(struct net_device * dev,struct ethtool_keee * e)351 static int bcmasp_get_eee(struct net_device *dev, struct ethtool_keee *e)
352 {
353 if (!dev->phydev)
354 return -ENODEV;
355
356 return phy_ethtool_get_eee(dev->phydev, e);
357 }
358
bcmasp_set_eee(struct net_device * dev,struct ethtool_keee * e)359 static int bcmasp_set_eee(struct net_device *dev, struct ethtool_keee *e)
360 {
361 if (!dev->phydev)
362 return -ENODEV;
363
364 return phy_ethtool_set_eee(dev->phydev, e);
365 }
366
bcmasp_get_eth_mac_stats(struct net_device * dev,struct ethtool_eth_mac_stats * mac_stats)367 static void bcmasp_get_eth_mac_stats(struct net_device *dev,
368 struct ethtool_eth_mac_stats *mac_stats)
369 {
370 struct bcmasp_intf *intf = netdev_priv(dev);
371
372 mac_stats->FramesTransmittedOK = umac_rl(intf, UMC_GTPOK);
373 mac_stats->SingleCollisionFrames = umac_rl(intf, UMC_GTSCL);
374 mac_stats->MultipleCollisionFrames = umac_rl(intf, UMC_GTMCL);
375 mac_stats->FramesReceivedOK = umac_rl(intf, UMC_GRPOK);
376 mac_stats->FrameCheckSequenceErrors = umac_rl(intf, UMC_GRFCS);
377 mac_stats->AlignmentErrors = umac_rl(intf, UMC_GRALN);
378 mac_stats->OctetsTransmittedOK = umac_rl(intf, UMC_GTBYT);
379 mac_stats->FramesWithDeferredXmissions = umac_rl(intf, UMC_GTDRF);
380 mac_stats->LateCollisions = umac_rl(intf, UMC_GTLCL);
381 mac_stats->FramesAbortedDueToXSColls = umac_rl(intf, UMC_GTXCL);
382 mac_stats->OctetsReceivedOK = umac_rl(intf, UMC_GRBYT);
383 mac_stats->MulticastFramesXmittedOK = umac_rl(intf, UMC_GTMCA);
384 mac_stats->BroadcastFramesXmittedOK = umac_rl(intf, UMC_GTBCA);
385 mac_stats->FramesWithExcessiveDeferral = umac_rl(intf, UMC_GTEDF);
386 mac_stats->MulticastFramesReceivedOK = umac_rl(intf, UMC_GRMCA);
387 mac_stats->BroadcastFramesReceivedOK = umac_rl(intf, UMC_GRBCA);
388 }
389
390 static const struct ethtool_rmon_hist_range bcmasp_rmon_ranges[] = {
391 { 0, 64},
392 { 65, 127},
393 { 128, 255},
394 { 256, 511},
395 { 512, 1023},
396 { 1024, 1518},
397 { 1519, 1522},
398 {}
399 };
400
bcmasp_get_rmon_stats(struct net_device * dev,struct ethtool_rmon_stats * rmon_stats,const struct ethtool_rmon_hist_range ** ranges)401 static void bcmasp_get_rmon_stats(struct net_device *dev,
402 struct ethtool_rmon_stats *rmon_stats,
403 const struct ethtool_rmon_hist_range **ranges)
404 {
405 struct bcmasp_intf *intf = netdev_priv(dev);
406
407 *ranges = bcmasp_rmon_ranges;
408
409 rmon_stats->undersize_pkts = umac_rl(intf, UMC_RRUND);
410 rmon_stats->oversize_pkts = umac_rl(intf, UMC_GROVR);
411 rmon_stats->fragments = umac_rl(intf, UMC_RRFRG);
412 rmon_stats->jabbers = umac_rl(intf, UMC_GRJBR);
413
414 rmon_stats->hist[0] = umac_rl(intf, UMC_GR64);
415 rmon_stats->hist[1] = umac_rl(intf, UMC_GR127);
416 rmon_stats->hist[2] = umac_rl(intf, UMC_GR255);
417 rmon_stats->hist[3] = umac_rl(intf, UMC_GR511);
418 rmon_stats->hist[4] = umac_rl(intf, UMC_GR1023);
419 rmon_stats->hist[5] = umac_rl(intf, UMC_GR1518);
420 rmon_stats->hist[6] = umac_rl(intf, UMC_GRMGV);
421
422 rmon_stats->hist_tx[0] = umac_rl(intf, UMC_TR64);
423 rmon_stats->hist_tx[1] = umac_rl(intf, UMC_TR127);
424 rmon_stats->hist_tx[2] = umac_rl(intf, UMC_TR255);
425 rmon_stats->hist_tx[3] = umac_rl(intf, UMC_TR511);
426 rmon_stats->hist_tx[4] = umac_rl(intf, UMC_TR1023);
427 rmon_stats->hist_tx[5] = umac_rl(intf, UMC_TR1518);
428 rmon_stats->hist_tx[6] = umac_rl(intf, UMC_TRMGV);
429 }
430
bcmasp_get_eth_ctrl_stats(struct net_device * dev,struct ethtool_eth_ctrl_stats * ctrl_stats)431 static void bcmasp_get_eth_ctrl_stats(struct net_device *dev,
432 struct ethtool_eth_ctrl_stats *ctrl_stats)
433 {
434 struct bcmasp_intf *intf = netdev_priv(dev);
435
436 ctrl_stats->MACControlFramesTransmitted = umac_rl(intf, UMC_GTXCF);
437 ctrl_stats->MACControlFramesReceived = umac_rl(intf, UMC_GRXCF);
438 ctrl_stats->UnsupportedOpcodesReceived = umac_rl(intf, UMC_GRXUO);
439 }
440
441 const struct ethtool_ops bcmasp_ethtool_ops = {
442 .get_drvinfo = bcmasp_get_drvinfo,
443 .get_link = ethtool_op_get_link,
444 .get_link_ksettings = phy_ethtool_get_link_ksettings,
445 .set_link_ksettings = phy_ethtool_set_link_ksettings,
446 .get_msglevel = bcmasp_get_msglevel,
447 .set_msglevel = bcmasp_set_msglevel,
448 .get_wol = bcmasp_get_wol,
449 .set_wol = bcmasp_set_wol,
450 .get_rxnfc = bcmasp_get_rxnfc,
451 .set_rxnfc = bcmasp_set_rxnfc,
452 .set_eee = bcmasp_set_eee,
453 .get_eee = bcmasp_get_eee,
454 .get_eth_mac_stats = bcmasp_get_eth_mac_stats,
455 .get_rmon_stats = bcmasp_get_rmon_stats,
456 .get_eth_ctrl_stats = bcmasp_get_eth_ctrl_stats,
457 .get_strings = bcmasp_get_strings,
458 .get_ethtool_stats = bcmasp_get_ethtool_stats,
459 .get_sset_count = bcmasp_get_sset_count,
460 .get_ts_info = ethtool_op_get_ts_info,
461 };
462