summaryrefslogtreecommitdiff
path: root/drivers/net/dsa/netc/netc_ethtool.c
blob: e6028851a6d09cb37d8b3659f168414b5fec30b7 (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
// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
/*
 * NXP NETC switch driver
 * Copyright 2025-2026 NXP
 */

#include <linux/ethtool_netlink.h>

#include "netc_switch.h"

static const struct ethtool_rmon_hist_range netc_rmon_ranges[] = {
	{   64,   64 },
	{   65,  127 },
	{  128,  255 },
	{  256,  511 },
	{  512, 1023 },
	{ 1024, 1522 },
	{ 1523, NETC_MAX_FRAME_LEN },
	{ }
};

static const struct netc_port_stat netc_port_counters[] = {
	{ NETC_PTGSLACR,	"port gate late arrival frames" },
	{ NETC_PSDFTCR,	"port SDF transmit frames" },
	{ NETC_PSDFDDCR,	"port SDF drop duplicate frames" },
	{ NETC_PRXDCR,		"port rx discard frames" },
	{ NETC_PRXDCRRR,	"port rx discard read-reset" },
	{ NETC_PRXDCRR0,	"port rx discard reason 0" },
	{ NETC_PRXDCRR1,	"port rx discard reason 1" },
	{ NETC_PTXDCR,		"port tx discard frames" },
	{ NETC_BPDCR,		"bridge port discard frames" },
};

static const struct netc_port_stat netc_emac_counters[] = {
	{ NETC_PM_ROCT(0),	"eMAC rx octets" },
	{ NETC_PM_RVLAN(0),	"eMAC rx VLAN frames" },
	{ NETC_PM_RERR(0),	"eMAC rx frame errors" },
	{ NETC_PM_RUCA(0),	"eMAC rx unicast frames" },
	{ NETC_PM_RDRP(0),	"eMAC rx dropped packets" },
	{ NETC_PM_RPKT(0),	"eMAC rx packets" },
	{ NETC_PM_TOCT(0),	"eMAC tx octets" },
	{ NETC_PM_TVLAN(0),	"eMAC tx VLAN frames" },
	{ NETC_PM_TFCS(0),	"eMAC tx FCS errors" },
	{ NETC_PM_TUCA(0),	"eMAC tx unicast frames" },
	{ NETC_PM_TPKT(0),	"eMAC tx packets" },
	{ NETC_PM_TUND(0),	"eMAC tx undersized packets" },
	{ NETC_PM_TIOCT(0),	"eMAC tx invalid octets" },
};

static const struct netc_port_stat netc_pmac_counters[] = {
	{ NETC_PM_ROCT(1),	"pMAC rx octets" },
	{ NETC_PM_RVLAN(1),	"pMAC rx VLAN frames" },
	{ NETC_PM_RERR(1),	"pMAC rx frame errors" },
	{ NETC_PM_RUCA(1),	"pMAC rx unicast frames" },
	{ NETC_PM_RDRP(1),	"pMAC rx dropped packets" },
	{ NETC_PM_RPKT(1),	"pMAC rx packets" },
	{ NETC_PM_TOCT(1),	"pMAC tx octets" },
	{ NETC_PM_TVLAN(1),	"pMAC tx VLAN frames" },
	{ NETC_PM_TFCS(1),	"pMAC tx FCS errors" },
	{ NETC_PM_TUCA(1),	"pMAC tx unicast frames" },
	{ NETC_PM_TPKT(1),	"pMAC tx packets" },
	{ NETC_PM_TUND(1),	"pMAC tx undersized packets" },
	{ NETC_PM_TIOCT(1),	"pMAC tx invalid octets" },
};

static void netc_port_pause_stats(struct netc_port *np, int mac,
				  struct ethtool_pause_stats *stats)
{
	if (mac && !np->caps.pmac)
		return;

	stats->tx_pause_frames = netc_port_rd64(np, NETC_PM_TXPF(mac));
	stats->rx_pause_frames = netc_port_rd64(np, NETC_PM_RXPF(mac));
}

void netc_port_get_pause_stats(struct dsa_switch *ds, int port,
			       struct ethtool_pause_stats *pause_stats)
{
	struct netc_port *np = NETC_PORT(ds, port);
	struct net_device *ndev;

	switch (pause_stats->src) {
	case ETHTOOL_MAC_STATS_SRC_EMAC:
		netc_port_pause_stats(np, 0, pause_stats);
		break;
	case ETHTOOL_MAC_STATS_SRC_PMAC:
		netc_port_pause_stats(np, 1, pause_stats);
		break;
	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
		ndev = dsa_to_port(ds, port)->user;
		ethtool_aggregate_pause_stats(ndev, pause_stats);
		break;
	}
}

static void netc_port_rmon_stats(struct netc_port *np, int mac,
				 struct ethtool_rmon_stats *stats)
{
	if (mac && !np->caps.pmac)
		return;

	stats->undersize_pkts = netc_port_rd64(np, NETC_PM_RUND(mac));
	stats->oversize_pkts = netc_port_rd64(np, NETC_PM_ROVR(mac));
	stats->fragments = netc_port_rd64(np, NETC_PM_RFRG(mac));
	stats->jabbers = netc_port_rd64(np, NETC_PM_RJBR(mac));

	stats->hist[0] = netc_port_rd64(np, NETC_PM_R64(mac));
	stats->hist[1] = netc_port_rd64(np, NETC_PM_R127(mac));
	stats->hist[2] = netc_port_rd64(np, NETC_PM_R255(mac));
	stats->hist[3] = netc_port_rd64(np, NETC_PM_R511(mac));
	stats->hist[4] = netc_port_rd64(np, NETC_PM_R1023(mac));
	stats->hist[5] = netc_port_rd64(np, NETC_PM_R1522(mac));
	stats->hist[6] = netc_port_rd64(np, NETC_PM_R1523X(mac));

	stats->hist_tx[0] = netc_port_rd64(np, NETC_PM_T64(mac));
	stats->hist_tx[1] = netc_port_rd64(np, NETC_PM_T127(mac));
	stats->hist_tx[2] = netc_port_rd64(np, NETC_PM_T255(mac));
	stats->hist_tx[3] = netc_port_rd64(np, NETC_PM_T511(mac));
	stats->hist_tx[4] = netc_port_rd64(np, NETC_PM_T1023(mac));
	stats->hist_tx[5] = netc_port_rd64(np, NETC_PM_T1522(mac));
	stats->hist_tx[6] = netc_port_rd64(np, NETC_PM_T1523X(mac));
}

void netc_port_get_rmon_stats(struct dsa_switch *ds, int port,
			      struct ethtool_rmon_stats *rmon_stats,
			      const struct ethtool_rmon_hist_range **ranges)
{
	struct netc_port *np = NETC_PORT(ds, port);
	struct net_device *ndev;

	*ranges = netc_rmon_ranges;

	switch (rmon_stats->src) {
	case ETHTOOL_MAC_STATS_SRC_EMAC:
		netc_port_rmon_stats(np, 0, rmon_stats);
		break;
	case ETHTOOL_MAC_STATS_SRC_PMAC:
		netc_port_rmon_stats(np, 1, rmon_stats);
		break;
	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
		ndev = dsa_to_port(ds, port)->user;
		ethtool_aggregate_rmon_stats(ndev, rmon_stats);
		break;
	}
}

static void netc_port_ctrl_stats(struct netc_port *np, int mac,
				 struct ethtool_eth_ctrl_stats *stats)
{
	if (mac && !np->caps.pmac)
		return;

	stats->MACControlFramesTransmitted =
		netc_port_rd64(np, NETC_PM_TCNP(mac));
	stats->MACControlFramesReceived =
		netc_port_rd64(np, NETC_PM_RCNP(mac));
}

void netc_port_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
				  struct ethtool_eth_ctrl_stats *ctrl_stats)
{
	struct netc_port *np = NETC_PORT(ds, port);
	struct net_device *ndev;

	switch (ctrl_stats->src) {
	case ETHTOOL_MAC_STATS_SRC_EMAC:
		netc_port_ctrl_stats(np, 0, ctrl_stats);
		break;
	case ETHTOOL_MAC_STATS_SRC_PMAC:
		netc_port_ctrl_stats(np, 1, ctrl_stats);
		break;
	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
		ndev = dsa_to_port(ds, port)->user;
		ethtool_aggregate_ctrl_stats(ndev, ctrl_stats);
		break;
	}
}

static void netc_port_mac_stats(struct netc_port *np, int mac,
				struct ethtool_eth_mac_stats *stats)
{
	if (mac && !np->caps.pmac)
		return;

	stats->FramesTransmittedOK = netc_port_rd64(np, NETC_PM_TFRM(mac));
	stats->SingleCollisionFrames = netc_port_rd64(np, NETC_PM_TSCOL(mac));
	stats->MultipleCollisionFrames =
		netc_port_rd64(np, NETC_PM_TMCOL(mac));
	stats->FramesReceivedOK = netc_port_rd64(np, NETC_PM_RFRM(mac));
	stats->FrameCheckSequenceErrors =
		netc_port_rd64(np, NETC_PM_RFCS(mac));
	stats->AlignmentErrors = netc_port_rd64(np, NETC_PM_RALN(mac));
	stats->OctetsTransmittedOK = netc_port_rd64(np, NETC_PM_TEOCT(mac));
	stats->FramesWithDeferredXmissions =
		netc_port_rd64(np, NETC_PM_TDFR(mac));
	stats->LateCollisions = netc_port_rd64(np, NETC_PM_TLCOL(mac));
	stats->FramesAbortedDueToXSColls =
		netc_port_rd64(np, NETC_PM_TECOL(mac));
	stats->FramesLostDueToIntMACXmitError =
		netc_port_rd64(np, NETC_PM_TERR(mac));
	stats->OctetsReceivedOK = netc_port_rd64(np, NETC_PM_REOCT(mac));
	stats->FramesLostDueToIntMACRcvError =
		netc_port_rd64(np, NETC_PM_RDRNTP(mac));
	stats->MulticastFramesXmittedOK =
		netc_port_rd64(np, NETC_PM_TMCA(mac));
	stats->BroadcastFramesXmittedOK =
		netc_port_rd64(np, NETC_PM_TBCA(mac));
	stats->MulticastFramesReceivedOK =
		netc_port_rd64(np, NETC_PM_RMCA(mac));
	stats->BroadcastFramesReceivedOK =
		netc_port_rd64(np, NETC_PM_RBCA(mac));
	stats->FramesWithExcessiveDeferral =
		netc_port_rd64(np, NETC_PM_TEDFR(mac));
}

void netc_port_get_eth_mac_stats(struct dsa_switch *ds, int port,
				 struct ethtool_eth_mac_stats *mac_stats)
{
	struct netc_port *np = NETC_PORT(ds, port);
	struct net_device *ndev;

	switch (mac_stats->src) {
	case ETHTOOL_MAC_STATS_SRC_EMAC:
		netc_port_mac_stats(np, 0, mac_stats);
		break;
	case ETHTOOL_MAC_STATS_SRC_PMAC:
		netc_port_mac_stats(np, 1, mac_stats);
		break;
	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
		ndev = dsa_to_port(ds, port)->user;
		ethtool_aggregate_mac_stats(ndev, mac_stats);
		break;
	}
}

int netc_port_get_sset_count(struct dsa_switch *ds, int port, int sset)
{
	struct netc_port *np = NETC_PORT(ds, port);
	int size;

	if (sset != ETH_SS_STATS)
		return -EOPNOTSUPP;

	size = ARRAY_SIZE(netc_port_counters) +
	       ARRAY_SIZE(netc_emac_counters);

	if (np->caps.pmac)
		size += ARRAY_SIZE(netc_pmac_counters);

	return size;
}

void netc_port_get_strings(struct dsa_switch *ds, int port,
			   u32 sset, u8 *data)
{
	struct netc_port *np = NETC_PORT(ds, port);
	int i;

	if (sset != ETH_SS_STATS)
		return;

	for (i = 0; i < ARRAY_SIZE(netc_port_counters); i++)
		ethtool_cpy(&data, netc_port_counters[i].name);

	for (i = 0; i < ARRAY_SIZE(netc_emac_counters); i++)
		ethtool_cpy(&data, netc_emac_counters[i].name);

	if (!np->caps.pmac)
		return;

	for (i = 0; i < ARRAY_SIZE(netc_pmac_counters); i++)
		ethtool_cpy(&data, netc_pmac_counters[i].name);
}

void netc_port_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
{
	struct netc_port *np = NETC_PORT(ds, port);
	int i;

	for (i = 0; i < ARRAY_SIZE(netc_port_counters); i++)
		*data++ = netc_port_rd(np, netc_port_counters[i].reg);

	for (i = 0; i < ARRAY_SIZE(netc_emac_counters); i++)
		*data++ = netc_port_rd64(np, netc_emac_counters[i].reg);

	if (!np->caps.pmac)
		return;

	for (i = 0; i < ARRAY_SIZE(netc_pmac_counters); i++)
		*data++ = netc_port_rd64(np, netc_pmac_counters[i].reg);
}