summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/broadcom/asp2/bcmasp_ethtool.c
blob: 484fc2b5626fe26f7ddf108874eb6e1ec817d8b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// SPDX-License-Identifier: GPL-2.0
#define pr_fmt(fmt)				"bcmasp_ethtool: " fmt

#include <asm-generic/unaligned.h>
#include <linux/ethtool.h>
#include <linux/netdevice.h>
#include <linux/platform_device.h>

#include "bcmasp.h"
#include "bcmasp_intf_defs.h"

enum bcmasp_stat_type {
	BCMASP_STAT_RX_EDPKT,
	BCMASP_STAT_RX_CTRL,
	BCMASP_STAT_RX_CTRL_PER_INTF,
	BCMASP_STAT_SOFT,
};

struct bcmasp_stats {
	char stat_string[ETH_GSTRING_LEN];
	enum bcmasp_stat_type type;
	u32 reg_offset;
};

#define STAT_BCMASP_SOFT_MIB(str) { \
	.stat_string = str, \
	.type = BCMASP_STAT_SOFT, \
}

#define STAT_BCMASP_OFFSET(str, _type, offset) { \
	.stat_string = str, \
	.type = _type, \
	.reg_offset = offset, \
}

#define STAT_BCMASP_RX_EDPKT(str, offset) \
	STAT_BCMASP_OFFSET(str, BCMASP_STAT_RX_EDPKT, offset)
#define STAT_BCMASP_RX_CTRL(str, offset) \
	STAT_BCMASP_OFFSET(str, BCMASP_STAT_RX_CTRL, offset)
#define STAT_BCMASP_RX_CTRL_PER_INTF(str, offset) \
	STAT_BCMASP_OFFSET(str, BCMASP_STAT_RX_CTRL_PER_INTF, offset)

/* Must match the order of struct bcmasp_mib_counters */
static const struct bcmasp_stats bcmasp_gstrings_stats[] = {
	/* EDPKT counters */
	STAT_BCMASP_RX_EDPKT("RX Time Stamp", ASP_EDPKT_RX_TS_COUNTER),
	STAT_BCMASP_RX_EDPKT("RX PKT Count", ASP_EDPKT_RX_PKT_CNT),
	STAT_BCMASP_RX_EDPKT("RX PKT Buffered", ASP_EDPKT_HDR_EXTR_CNT),
	STAT_BCMASP_RX_EDPKT("RX PKT Pushed to DRAM", ASP_EDPKT_HDR_OUT_CNT),
	/* ASP RX control */
	STAT_BCMASP_RX_CTRL_PER_INTF("Frames From Unimac",
				     ASP_RX_CTRL_UMAC_0_FRAME_COUNT),
	STAT_BCMASP_RX_CTRL_PER_INTF("Frames From Port",
				     ASP_RX_CTRL_FB_0_FRAME_COUNT),
	STAT_BCMASP_RX_CTRL_PER_INTF("RX Buffer FIFO Depth",
				     ASP_RX_CTRL_FB_RX_FIFO_DEPTH),
	STAT_BCMASP_RX_CTRL("Frames Out(Buffer)",
			    ASP_RX_CTRL_FB_OUT_FRAME_COUNT),
	STAT_BCMASP_RX_CTRL("Frames Out(Filters)",
			    ASP_RX_CTRL_FB_FILT_OUT_FRAME_COUNT),
	/* Software maintained statistics */
	STAT_BCMASP_SOFT_MIB("RX SKB Alloc Failed"),
	STAT_BCMASP_SOFT_MIB("TX DMA Failed"),
	STAT_BCMASP_SOFT_MIB("Multicast Filters Full"),
	STAT_BCMASP_SOFT_MIB("Unicast Filters Full"),
	STAT_BCMASP_SOFT_MIB("MDA Filters Combined"),
	STAT_BCMASP_SOFT_MIB("Promisc Filter Set"),
	STAT_BCMASP_SOFT_MIB("TX Realloc For Offload Failed"),
	STAT_BCMASP_SOFT_MIB("Tx Timeout Count"),
};

#define BCMASP_STATS_LEN	ARRAY_SIZE(bcmasp_gstrings_stats)

static u16 bcmasp_stat_fixup_offset(struct bcmasp_intf *intf,
				    const struct bcmasp_stats *s)
{
	struct bcmasp_priv *priv = intf->parent;

	if (!strcmp("Frames Out(Buffer)", s->stat_string))
		return priv->hw_info->rx_ctrl_fb_out_frame_count;

	if (!strcmp("Frames Out(Filters)", s->stat_string))
		return priv->hw_info->rx_ctrl_fb_filt_out_frame_count;

	if (!strcmp("RX Buffer FIFO Depth", s->stat_string))
		return priv->hw_info->rx_ctrl_fb_rx_fifo_depth;

	return s->reg_offset;
}

static int bcmasp_get_sset_count(struct net_device *dev, int string_set)
{
	switch (string_set) {
	case ETH_SS_STATS:
		return BCMASP_STATS_LEN;
	default:
		return -EOPNOTSUPP;
	}
}

static void bcmasp_get_strings(struct net_device *dev, u32 stringset,
			       u8 *data)
{
	unsigned int i;

	switch (stringset) {
	case ETH_SS_STATS:
		for (i = 0; i < BCMASP_STATS_LEN; i++) {
			memcpy(data + i * ETH_GSTRING_LEN,
			       bcmasp_gstrings_stats[i].stat_string,
			       ETH_GSTRING_LEN);
		}
		break;
	default:
		return;
	}
}

static void bcmasp_update_mib_counters(struct bcmasp_intf *intf)
{
	unsigned int i;

	for (i = 0; i < BCMASP_STATS_LEN; i++) {
		const struct bcmasp_stats *s;
		u32 offset, val;
		char *p;

		s = &bcmasp_gstrings_stats[i];
		offset = bcmasp_stat_fixup_offset(intf, s);
		switch (s->type) {
		case BCMASP_STAT_SOFT:
			continue;
		case BCMASP_STAT_RX_EDPKT:
			val = rx_edpkt_core_rl(intf->parent, offset);
			break;
		case BCMASP_STAT_RX_CTRL:
			val = rx_ctrl_core_rl(intf->parent, offset);
			break;
		case BCMASP_STAT_RX_CTRL_PER_INTF:
			offset += sizeof(u32) * intf->port;
			val = rx_ctrl_core_rl(intf->parent, offset);
			break;
		default:
			continue;
		}
		p = (char *)(&intf->mib) + (i * sizeof(u32));
		put_unaligned(val, (u32 *)p);
	}
}

static void bcmasp_get_ethtool_stats(struct net_device *dev,
				     struct ethtool_stats *stats,
				     u64 *data)
{
	struct bcmasp_intf *intf = netdev_priv(dev);
	unsigned int i;
	char *p;

	if (netif_running(dev))
		bcmasp_update_mib_counters(intf);

	for (i = 0; i < BCMASP_STATS_LEN; i++) {
		p = (char *)(&intf->mib) + (i * sizeof(u32));
		data[i] = *(u32 *)p;
	}
}

static void bcmasp_get_drvinfo(struct net_device *dev,
			       struct ethtool_drvinfo *info)
{
	strscpy(info->driver, "bcmasp", sizeof(info->driver));
	strscpy(info->bus_info, dev_name(dev->dev.parent),
		sizeof(info->bus_info));
}

static u32 bcmasp_get_msglevel(struct net_device *dev)
{
	struct bcmasp_intf *intf = netdev_priv(dev);

	return intf->msg_enable;
}

static void bcmasp_set_msglevel(struct net_device *dev, u32 level)
{
	struct bcmasp_intf *intf = netdev_priv(dev);

	intf->msg_enable = level;
}

#define BCMASP_SUPPORTED_WAKE   (WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER)
static void bcmasp_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct bcmasp_intf *intf = netdev_priv(dev);

	wol->supported = BCMASP_SUPPORTED_WAKE;
	wol->wolopts = intf->wolopts;
	memset(wol->sopass, 0, sizeof(wol->sopass));

	if (wol->wolopts & WAKE_MAGICSECURE)
		memcpy(wol->sopass, intf->sopass, sizeof(intf->sopass));
}

static int bcmasp_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct bcmasp_intf *intf = netdev_priv(dev);
	struct bcmasp_priv *priv = intf->parent;
	struct device *kdev = &priv->pdev->dev;

	if (!device_can_wakeup(kdev))
		return -EOPNOTSUPP;

	/* Interface Specific */
	intf->wolopts = wol->wolopts;
	if (intf->wolopts & WAKE_MAGICSECURE)
		memcpy(intf->sopass, wol->sopass, sizeof(wol->sopass));

	mutex_lock(&priv->wol_lock);
	priv->enable_wol(intf, !!intf->wolopts);
	mutex_unlock(&priv->wol_lock);

	return 0;
}

static int bcmasp_flow_insert(struct net_device *dev, struct ethtool_rxnfc *cmd)
{
	struct bcmasp_intf *intf = netdev_priv(dev);
	struct bcmasp_net_filter *nfilter;
	u32 loc = cmd->fs.location;
	bool wake = false;

	if (cmd->fs.ring_cookie == RX_CLS_FLOW_WAKE)
		wake = true;

	/* Currently only supports WAKE filters */
	if (!wake)
		return -EOPNOTSUPP;

	switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
	case ETHER_FLOW:
	case IP_USER_FLOW:
	case TCP_V4_FLOW:
	case UDP_V4_FLOW:
	case TCP_V6_FLOW:
	case UDP_V6_FLOW:
		break;
	default:
		return -EOPNOTSUPP;
	}

	/* Check if filter already exists */
	if (bcmasp_netfilt_check_dup(intf, &cmd->fs))
		return -EINVAL;

	nfilter = bcmasp_netfilt_get_init(intf, loc, wake, true);
	if (IS_ERR(nfilter))
		return PTR_ERR(nfilter);

	/* Return the location where we did insert the filter */
	cmd->fs.location = nfilter->hw_index;
	memcpy(&nfilter->fs, &cmd->fs, sizeof(struct ethtool_rx_flow_spec));

	/* Since we only support wake filters, defer register programming till
	 * suspend time.
	 */
	return 0;
}

static int bcmasp_flow_delete(struct net_device *dev, struct ethtool_rxnfc *cmd)
{
	struct bcmasp_intf *intf = netdev_priv(dev);
	struct bcmasp_net_filter *nfilter;

	nfilter = bcmasp_netfilt_get_init(intf, cmd->fs.location, false, false);
	if (IS_ERR(nfilter))
		return PTR_ERR(nfilter);

	bcmasp_netfilt_release(intf, nfilter);

	return 0;
}

static int bcmasp_flow_get(struct bcmasp_intf *intf, struct ethtool_rxnfc *cmd)
{
	struct bcmasp_net_filter *nfilter;

	nfilter = bcmasp_netfilt_get_init(intf, cmd->fs.location, false, false);
	if (IS_ERR(nfilter))
		return PTR_ERR(nfilter);

	memcpy(&cmd->fs, &nfilter->fs, sizeof(nfilter->fs));

	cmd->data = NUM_NET_FILTERS;

	return 0;
}

static int bcmasp_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
{
	struct bcmasp_intf *intf = netdev_priv(dev);
	int ret = -EOPNOTSUPP;

	mutex_lock(&intf->parent->net_lock);

	switch (cmd->cmd) {
	case ETHTOOL_SRXCLSRLINS:
		ret = bcmasp_flow_insert(dev, cmd);
		break;
	case ETHTOOL_SRXCLSRLDEL:
		ret = bcmasp_flow_delete(dev, cmd);
		break;
	default:
		break;
	}

	mutex_unlock(&intf->parent->net_lock);

	return ret;
}

static int bcmasp_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
			    u32 *rule_locs)
{
	struct bcmasp_intf *intf = netdev_priv(dev);
	int err = 0;

	mutex_lock(&intf->parent->net_lock);

	switch (cmd->cmd) {
	case ETHTOOL_GRXCLSRLCNT:
		cmd->rule_cnt = bcmasp_netfilt_get_active(intf);
		/* We support specifying rule locations */
		cmd->data |= RX_CLS_LOC_SPECIAL;
		break;
	case ETHTOOL_GRXCLSRULE:
		err = bcmasp_flow_get(intf, cmd);
		break;
	case ETHTOOL_GRXCLSRLALL:
		err = bcmasp_netfilt_get_all_active(intf, rule_locs, &cmd->rule_cnt);
		cmd->data = NUM_NET_FILTERS;
		break;
	default:
		err = -EOPNOTSUPP;
		break;
	}

	mutex_unlock(&intf->parent->net_lock);

	return err;
}

void bcmasp_eee_enable_set(struct bcmasp_intf *intf, bool enable)
{
	u32 reg;

	reg = umac_rl(intf, UMC_EEE_CTRL);
	if (enable)
		reg |= EEE_EN;
	else
		reg &= ~EEE_EN;
	umac_wl(intf, reg, UMC_EEE_CTRL);

	intf->eee.eee_enabled = enable;
}

static int bcmasp_get_eee(struct net_device *dev, struct ethtool_keee *e)
{
	struct bcmasp_intf *intf = netdev_priv(dev);
	struct ethtool_keee *p = &intf->eee;

	if (!dev->phydev)
		return -ENODEV;

	e->tx_lpi_enabled = p->tx_lpi_enabled;
	e->tx_lpi_timer = umac_rl(intf, UMC_EEE_LPI_TIMER);

	return phy_ethtool_get_eee(dev->phydev, e);
}

static int bcmasp_set_eee(struct net_device *dev, struct ethtool_keee *e)
{
	struct bcmasp_intf *intf = netdev_priv(dev);
	struct ethtool_keee *p = &intf->eee;
	int ret;

	if (!dev->phydev)
		return -ENODEV;

	if (!p->eee_enabled) {
		bcmasp_eee_enable_set(intf, false);
	} else {
		ret = phy_init_eee(dev->phydev, 0);
		if (ret) {
			netif_err(intf, hw, dev,
				  "EEE initialization failed: %d\n", ret);
			return ret;
		}

		umac_wl(intf, e->tx_lpi_timer, UMC_EEE_LPI_TIMER);
		intf->eee.tx_lpi_enabled = e->tx_lpi_enabled;
		bcmasp_eee_enable_set(intf, true);
	}

	return phy_ethtool_set_eee(dev->phydev, e);
}

static void bcmasp_get_eth_mac_stats(struct net_device *dev,
				     struct ethtool_eth_mac_stats *mac_stats)
{
	struct bcmasp_intf *intf = netdev_priv(dev);

	mac_stats->FramesTransmittedOK = umac_rl(intf, UMC_GTPOK);
	mac_stats->SingleCollisionFrames = umac_rl(intf, UMC_GTSCL);
	mac_stats->MultipleCollisionFrames = umac_rl(intf, UMC_GTMCL);
	mac_stats->FramesReceivedOK = umac_rl(intf, UMC_GRPOK);
	mac_stats->FrameCheckSequenceErrors = umac_rl(intf, UMC_GRFCS);
	mac_stats->AlignmentErrors = umac_rl(intf, UMC_GRALN);
	mac_stats->OctetsTransmittedOK = umac_rl(intf, UMC_GTBYT);
	mac_stats->FramesWithDeferredXmissions = umac_rl(intf, UMC_GTDRF);
	mac_stats->LateCollisions = umac_rl(intf, UMC_GTLCL);
	mac_stats->FramesAbortedDueToXSColls = umac_rl(intf, UMC_GTXCL);
	mac_stats->OctetsReceivedOK = umac_rl(intf, UMC_GRBYT);
	mac_stats->MulticastFramesXmittedOK = umac_rl(intf, UMC_GTMCA);
	mac_stats->BroadcastFramesXmittedOK = umac_rl(intf, UMC_GTBCA);
	mac_stats->FramesWithExcessiveDeferral = umac_rl(intf, UMC_GTEDF);
	mac_stats->MulticastFramesReceivedOK = umac_rl(intf, UMC_GRMCA);
	mac_stats->BroadcastFramesReceivedOK = umac_rl(intf, UMC_GRBCA);
}

static const struct ethtool_rmon_hist_range bcmasp_rmon_ranges[] = {
	{    0,   64},
	{   65,  127},
	{  128,  255},
	{  256,  511},
	{  512, 1023},
	{ 1024, 1518},
	{ 1519, 1522},
	{}
};

static void bcmasp_get_rmon_stats(struct net_device *dev,
				  struct ethtool_rmon_stats *rmon_stats,
				  const struct ethtool_rmon_hist_range **ranges)
{
	struct bcmasp_intf *intf = netdev_priv(dev);

	*ranges = bcmasp_rmon_ranges;

	rmon_stats->undersize_pkts = umac_rl(intf, UMC_RRUND);
	rmon_stats->oversize_pkts = umac_rl(intf, UMC_GROVR);
	rmon_stats->fragments = umac_rl(intf, UMC_RRFRG);
	rmon_stats->jabbers = umac_rl(intf, UMC_GRJBR);

	rmon_stats->hist[0] = umac_rl(intf, UMC_GR64);
	rmon_stats->hist[1] = umac_rl(intf, UMC_GR127);
	rmon_stats->hist[2] = umac_rl(intf, UMC_GR255);
	rmon_stats->hist[3] = umac_rl(intf, UMC_GR511);
	rmon_stats->hist[4] = umac_rl(intf, UMC_GR1023);
	rmon_stats->hist[5] = umac_rl(intf, UMC_GR1518);
	rmon_stats->hist[6] = umac_rl(intf, UMC_GRMGV);

	rmon_stats->hist_tx[0] = umac_rl(intf, UMC_TR64);
	rmon_stats->hist_tx[1] = umac_rl(intf, UMC_TR127);
	rmon_stats->hist_tx[2] = umac_rl(intf, UMC_TR255);
	rmon_stats->hist_tx[3] = umac_rl(intf, UMC_TR511);
	rmon_stats->hist_tx[4] = umac_rl(intf, UMC_TR1023);
	rmon_stats->hist_tx[5] = umac_rl(intf, UMC_TR1518);
	rmon_stats->hist_tx[6] = umac_rl(intf, UMC_TRMGV);
}

static void bcmasp_get_eth_ctrl_stats(struct net_device *dev,
				      struct ethtool_eth_ctrl_stats *ctrl_stats)
{
	struct bcmasp_intf *intf = netdev_priv(dev);

	ctrl_stats->MACControlFramesTransmitted = umac_rl(intf, UMC_GTXCF);
	ctrl_stats->MACControlFramesReceived = umac_rl(intf, UMC_GRXCF);
	ctrl_stats->UnsupportedOpcodesReceived = umac_rl(intf, UMC_GRXUO);
}

const struct ethtool_ops bcmasp_ethtool_ops = {
	.get_drvinfo		= bcmasp_get_drvinfo,
	.get_link		= ethtool_op_get_link,
	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
	.get_msglevel		= bcmasp_get_msglevel,
	.set_msglevel		= bcmasp_set_msglevel,
	.get_wol		= bcmasp_get_wol,
	.set_wol		= bcmasp_set_wol,
	.get_rxnfc		= bcmasp_get_rxnfc,
	.set_rxnfc		= bcmasp_set_rxnfc,
	.set_eee		= bcmasp_set_eee,
	.get_eee		= bcmasp_get_eee,
	.get_eth_mac_stats	= bcmasp_get_eth_mac_stats,
	.get_rmon_stats		= bcmasp_get_rmon_stats,
	.get_eth_ctrl_stats	= bcmasp_get_eth_ctrl_stats,
	.get_strings		= bcmasp_get_strings,
	.get_ethtool_stats	= bcmasp_get_ethtool_stats,
	.get_sset_count		= bcmasp_get_sset_count,
};