xref: /linux/drivers/net/ethernet/freescale/enetc/enetc_ethtool.c (revision bca5cfbb694d66a1c482d0c347eee80f6afbc870)
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2017-2019 NXP */
3 
4 #include <linux/ethtool_netlink.h>
5 #include <linux/net_tstamp.h>
6 #include <linux/module.h>
7 #include "enetc.h"
8 
9 static const u32 enetc_si_regs[] = {
10 	ENETC_SIMR, ENETC_SIPMAR0, ENETC_SIPMAR1, ENETC_SICBDRMR,
11 	ENETC_SICBDRSR,	ENETC_SICBDRBAR0, ENETC_SICBDRBAR1, ENETC_SICBDRPIR,
12 	ENETC_SICBDRCIR, ENETC_SICBDRLENR, ENETC_SICAPR0, ENETC_SICAPR1,
13 	ENETC_SIUEFDCR
14 };
15 
16 static const u32 enetc_txbdr_regs[] = {
17 	ENETC_TBMR, ENETC_TBSR, ENETC_TBBAR0, ENETC_TBBAR1,
18 	ENETC_TBPIR, ENETC_TBCIR, ENETC_TBLENR, ENETC_TBIER, ENETC_TBICR0,
19 	ENETC_TBICR1
20 };
21 
22 static const u32 enetc_rxbdr_regs[] = {
23 	ENETC_RBMR, ENETC_RBSR, ENETC_RBBSR, ENETC_RBCIR, ENETC_RBBAR0,
24 	ENETC_RBBAR1, ENETC_RBPIR, ENETC_RBLENR, ENETC_RBIER, ENETC_RBICR0,
25 	ENETC_RBICR1
26 };
27 
28 static const u32 enetc_port_regs[] = {
29 	ENETC_PMR, ENETC_PSR, ENETC_PSIPMR, ENETC_PSIPMAR0(0),
30 	ENETC_PSIPMAR1(0), ENETC_PTXMBAR, ENETC_PCAPR0, ENETC_PCAPR1,
31 	ENETC_PSICFGR0(0), ENETC_PRFSCAPR, ENETC_PTCMSDUR(0),
32 	ENETC_PM0_CMD_CFG, ENETC_PM0_MAXFRM, ENETC_PM0_IF_MODE
33 };
34 
35 static const u32 enetc_port_mm_regs[] = {
36 	ENETC_MMCSR, ENETC_PFPMR, ENETC_PTCFPR(0), ENETC_PTCFPR(1),
37 	ENETC_PTCFPR(2), ENETC_PTCFPR(3), ENETC_PTCFPR(4), ENETC_PTCFPR(5),
38 	ENETC_PTCFPR(6), ENETC_PTCFPR(7),
39 };
40 
41 static int enetc_get_reglen(struct net_device *ndev)
42 {
43 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
44 	struct enetc_hw *hw = &priv->si->hw;
45 	int len;
46 
47 	len = ARRAY_SIZE(enetc_si_regs);
48 	len += ARRAY_SIZE(enetc_txbdr_regs) * priv->num_tx_rings;
49 	len += ARRAY_SIZE(enetc_rxbdr_regs) * priv->num_rx_rings;
50 
51 	if (hw->port)
52 		len += ARRAY_SIZE(enetc_port_regs);
53 
54 	if (hw->port && !!(priv->si->hw_features & ENETC_SI_F_QBU))
55 		len += ARRAY_SIZE(enetc_port_mm_regs);
56 
57 	len *= sizeof(u32) * 2; /* store 2 entries per reg: addr and value */
58 
59 	return len;
60 }
61 
62 static void enetc_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
63 			   void *regbuf)
64 {
65 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
66 	struct enetc_hw *hw = &priv->si->hw;
67 	u32 *buf = (u32 *)regbuf;
68 	int i, j;
69 	u32 addr;
70 
71 	for (i = 0; i < ARRAY_SIZE(enetc_si_regs); i++) {
72 		*buf++ = enetc_si_regs[i];
73 		*buf++ = enetc_rd(hw, enetc_si_regs[i]);
74 	}
75 
76 	for (i = 0; i < priv->num_tx_rings; i++) {
77 		for (j = 0; j < ARRAY_SIZE(enetc_txbdr_regs); j++) {
78 			addr = ENETC_BDR(TX, i, enetc_txbdr_regs[j]);
79 
80 			*buf++ = addr;
81 			*buf++ = enetc_rd(hw, addr);
82 		}
83 	}
84 
85 	for (i = 0; i < priv->num_rx_rings; i++) {
86 		for (j = 0; j < ARRAY_SIZE(enetc_rxbdr_regs); j++) {
87 			addr = ENETC_BDR(RX, i, enetc_rxbdr_regs[j]);
88 
89 			*buf++ = addr;
90 			*buf++ = enetc_rd(hw, addr);
91 		}
92 	}
93 
94 	if (!hw->port)
95 		return;
96 
97 	for (i = 0; i < ARRAY_SIZE(enetc_port_regs); i++) {
98 		addr = ENETC_PORT_BASE + enetc_port_regs[i];
99 		*buf++ = addr;
100 		*buf++ = enetc_rd(hw, addr);
101 	}
102 
103 	if (priv->si->hw_features & ENETC_SI_F_QBU) {
104 		for (i = 0; i < ARRAY_SIZE(enetc_port_mm_regs); i++) {
105 			addr = ENETC_PORT_BASE + enetc_port_mm_regs[i];
106 			*buf++ = addr;
107 			*buf++ = enetc_rd(hw, addr);
108 		}
109 	}
110 }
111 
112 static const struct {
113 	int reg;
114 	char name[ETH_GSTRING_LEN];
115 } enetc_si_counters[] =  {
116 	{ ENETC_SIROCT, "SI rx octets" },
117 	{ ENETC_SIRFRM, "SI rx frames" },
118 	{ ENETC_SIRUCA, "SI rx u-cast frames" },
119 	{ ENETC_SIRMCA, "SI rx m-cast frames" },
120 	{ ENETC_SITOCT, "SI tx octets" },
121 	{ ENETC_SITFRM, "SI tx frames" },
122 	{ ENETC_SITUCA, "SI tx u-cast frames" },
123 	{ ENETC_SITMCA, "SI tx m-cast frames" },
124 	{ ENETC_RBDCR(0), "Rx ring  0 discarded frames" },
125 	{ ENETC_RBDCR(1), "Rx ring  1 discarded frames" },
126 	{ ENETC_RBDCR(2), "Rx ring  2 discarded frames" },
127 	{ ENETC_RBDCR(3), "Rx ring  3 discarded frames" },
128 	{ ENETC_RBDCR(4), "Rx ring  4 discarded frames" },
129 	{ ENETC_RBDCR(5), "Rx ring  5 discarded frames" },
130 	{ ENETC_RBDCR(6), "Rx ring  6 discarded frames" },
131 	{ ENETC_RBDCR(7), "Rx ring  7 discarded frames" },
132 	{ ENETC_RBDCR(8), "Rx ring  8 discarded frames" },
133 	{ ENETC_RBDCR(9), "Rx ring  9 discarded frames" },
134 	{ ENETC_RBDCR(10), "Rx ring 10 discarded frames" },
135 	{ ENETC_RBDCR(11), "Rx ring 11 discarded frames" },
136 	{ ENETC_RBDCR(12), "Rx ring 12 discarded frames" },
137 	{ ENETC_RBDCR(13), "Rx ring 13 discarded frames" },
138 	{ ENETC_RBDCR(14), "Rx ring 14 discarded frames" },
139 	{ ENETC_RBDCR(15), "Rx ring 15 discarded frames" },
140 };
141 
142 static const struct {
143 	int reg;
144 	char name[ETH_GSTRING_LEN] __nonstring;
145 } enetc_port_counters[] = {
146 	{ ENETC_PM_REOCT(0),	"MAC rx ethernet octets" },
147 	{ ENETC_PM_RALN(0),	"MAC rx alignment errors" },
148 	{ ENETC_PM_RXPF(0),	"MAC rx valid pause frames" },
149 	{ ENETC_PM_RFRM(0),	"MAC rx valid frames" },
150 	{ ENETC_PM_RFCS(0),	"MAC rx fcs errors" },
151 	{ ENETC_PM_RVLAN(0),	"MAC rx VLAN frames" },
152 	{ ENETC_PM_RERR(0),	"MAC rx frame errors" },
153 	{ ENETC_PM_RUCA(0),	"MAC rx unicast frames" },
154 	{ ENETC_PM_RMCA(0),	"MAC rx multicast frames" },
155 	{ ENETC_PM_RBCA(0),	"MAC rx broadcast frames" },
156 	{ ENETC_PM_RDRP(0),	"MAC rx dropped packets" },
157 	{ ENETC_PM_RPKT(0),	"MAC rx packets" },
158 	{ ENETC_PM_RUND(0),	"MAC rx undersized packets" },
159 	{ ENETC_PM_R64(0),	"MAC rx 64 byte packets" },
160 	{ ENETC_PM_R127(0),	"MAC rx 65-127 byte packets" },
161 	{ ENETC_PM_R255(0),	"MAC rx 128-255 byte packets" },
162 	{ ENETC_PM_R511(0),	"MAC rx 256-511 byte packets" },
163 	{ ENETC_PM_R1023(0),	"MAC rx 512-1023 byte packets" },
164 	{ ENETC_PM_R1522(0),	"MAC rx 1024-1522 byte packets" },
165 	{ ENETC_PM_R1523X(0),	"MAC rx 1523 to max-octet packets" },
166 	{ ENETC_PM_ROVR(0),	"MAC rx oversized packets" },
167 	{ ENETC_PM_RJBR(0),	"MAC rx jabber packets" },
168 	{ ENETC_PM_RFRG(0),	"MAC rx fragment packets" },
169 	{ ENETC_PM_RCNP(0),	"MAC rx control packets" },
170 	{ ENETC_PM_RDRNTP(0),	"MAC rx fifo drop" },
171 	{ ENETC_PM_TEOCT(0),	"MAC tx ethernet octets" },
172 	{ ENETC_PM_TOCT(0),	"MAC tx octets" },
173 	{ ENETC_PM_TCRSE(0),	"MAC tx carrier sense errors" },
174 	{ ENETC_PM_TXPF(0),	"MAC tx valid pause frames" },
175 	{ ENETC_PM_TFRM(0),	"MAC tx frames" },
176 	{ ENETC_PM_TFCS(0),	"MAC tx fcs errors" },
177 	{ ENETC_PM_TVLAN(0),	"MAC tx VLAN frames" },
178 	{ ENETC_PM_TERR(0),	"MAC tx frame errors" },
179 	{ ENETC_PM_TUCA(0),	"MAC tx unicast frames" },
180 	{ ENETC_PM_TMCA(0),	"MAC tx multicast frames" },
181 	{ ENETC_PM_TBCA(0),	"MAC tx broadcast frames" },
182 	{ ENETC_PM_TPKT(0),	"MAC tx packets" },
183 	{ ENETC_PM_TUND(0),	"MAC tx undersized packets" },
184 	{ ENETC_PM_T64(0),	"MAC tx 64 byte packets" },
185 	{ ENETC_PM_T127(0),	"MAC tx 65-127 byte packets" },
186 	{ ENETC_PM_T255(0),	"MAC tx 128-255 byte packets" },
187 	{ ENETC_PM_T511(0),	"MAC tx 256-511 byte packets" },
188 	{ ENETC_PM_T1023(0),	"MAC tx 512-1023 byte packets" },
189 	{ ENETC_PM_T1522(0),	"MAC tx 1024-1522 byte packets" },
190 	{ ENETC_PM_T1523X(0),	"MAC tx 1523 to max-octet packets" },
191 	{ ENETC_PM_TCNP(0),	"MAC tx control packets" },
192 	{ ENETC_PM_TDFR(0),	"MAC tx deferred packets" },
193 	{ ENETC_PM_TMCOL(0),	"MAC tx multiple collisions" },
194 	{ ENETC_PM_TSCOL(0),	"MAC tx single collisions" },
195 	{ ENETC_PM_TLCOL(0),	"MAC tx late collisions" },
196 	{ ENETC_PM_TECOL(0),	"MAC tx excessive collisions" },
197 	{ ENETC_UFDMF,		"SI MAC nomatch u-cast discards" },
198 	{ ENETC_MFDMF,		"SI MAC nomatch m-cast discards" },
199 	{ ENETC_PBFDSIR,	"SI MAC nomatch b-cast discards" },
200 	{ ENETC_PUFDVFR,	"SI VLAN nomatch u-cast discards" },
201 	{ ENETC_PMFDVFR,	"SI VLAN nomatch m-cast discards" },
202 	{ ENETC_PBFDVFR,	"SI VLAN nomatch b-cast discards" },
203 	{ ENETC_PFDMSAPR,	"SI pruning discarded frames" },
204 	{ ENETC_PICDR(0),	"ICM DR0 discarded frames" },
205 	{ ENETC_PICDR(1),	"ICM DR1 discarded frames" },
206 	{ ENETC_PICDR(2),	"ICM DR2 discarded frames" },
207 	{ ENETC_PICDR(3),	"ICM DR3 discarded frames" },
208 };
209 
210 static const char rx_ring_stats[][ETH_GSTRING_LEN] = {
211 	"Rx ring %2d frames",
212 	"Rx ring %2d alloc errors",
213 	"Rx ring %2d XDP drops",
214 	"Rx ring %2d recycles",
215 	"Rx ring %2d recycle failures",
216 	"Rx ring %2d redirects",
217 	"Rx ring %2d redirect failures",
218 };
219 
220 static const char tx_ring_stats[][ETH_GSTRING_LEN] = {
221 	"Tx ring %2d frames",
222 	"Tx ring %2d XDP frames",
223 	"Tx ring %2d XDP drops",
224 	"Tx window drop %2d frames",
225 };
226 
227 static int enetc_get_sset_count(struct net_device *ndev, int sset)
228 {
229 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
230 	int len;
231 
232 	if (sset != ETH_SS_STATS)
233 		return -EOPNOTSUPP;
234 
235 	len = ARRAY_SIZE(enetc_si_counters) +
236 	      ARRAY_SIZE(tx_ring_stats) * priv->num_tx_rings +
237 	      ARRAY_SIZE(rx_ring_stats) * priv->num_rx_rings;
238 
239 	if (!enetc_si_is_pf(priv->si))
240 		return len;
241 
242 	len += ARRAY_SIZE(enetc_port_counters);
243 
244 	return len;
245 }
246 
247 static void enetc_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
248 {
249 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
250 	int i, j;
251 
252 	switch (stringset) {
253 	case ETH_SS_STATS:
254 		for (i = 0; i < ARRAY_SIZE(enetc_si_counters); i++)
255 			ethtool_puts(&data, enetc_si_counters[i].name);
256 		for (i = 0; i < priv->num_tx_rings; i++)
257 			for (j = 0; j < ARRAY_SIZE(tx_ring_stats); j++)
258 				ethtool_sprintf(&data, tx_ring_stats[j], i);
259 		for (i = 0; i < priv->num_rx_rings; i++)
260 			for (j = 0; j < ARRAY_SIZE(rx_ring_stats); j++)
261 				ethtool_sprintf(&data, rx_ring_stats[j], i);
262 
263 		if (!enetc_si_is_pf(priv->si))
264 			break;
265 
266 		for (i = 0; i < ARRAY_SIZE(enetc_port_counters); i++)
267 			ethtool_cpy(&data, enetc_port_counters[i].name);
268 
269 		break;
270 	}
271 }
272 
273 static void enetc_get_ethtool_stats(struct net_device *ndev,
274 				    struct ethtool_stats *stats, u64 *data)
275 {
276 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
277 	struct enetc_hw *hw = &priv->si->hw;
278 	int i, o = 0;
279 
280 	for (i = 0; i < ARRAY_SIZE(enetc_si_counters); i++)
281 		data[o++] = enetc_rd64(hw, enetc_si_counters[i].reg);
282 
283 	for (i = 0; i < priv->num_tx_rings; i++) {
284 		data[o++] = priv->tx_ring[i]->stats.packets;
285 		data[o++] = priv->tx_ring[i]->stats.xdp_tx;
286 		data[o++] = priv->tx_ring[i]->stats.xdp_tx_drops;
287 		data[o++] = priv->tx_ring[i]->stats.win_drop;
288 	}
289 
290 	for (i = 0; i < priv->num_rx_rings; i++) {
291 		data[o++] = priv->rx_ring[i]->stats.packets;
292 		data[o++] = priv->rx_ring[i]->stats.rx_alloc_errs;
293 		data[o++] = priv->rx_ring[i]->stats.xdp_drops;
294 		data[o++] = priv->rx_ring[i]->stats.recycles;
295 		data[o++] = priv->rx_ring[i]->stats.recycle_failures;
296 		data[o++] = priv->rx_ring[i]->stats.xdp_redirect;
297 		data[o++] = priv->rx_ring[i]->stats.xdp_redirect_failures;
298 	}
299 
300 	if (!enetc_si_is_pf(priv->si))
301 		return;
302 
303 	for (i = 0; i < ARRAY_SIZE(enetc_port_counters); i++)
304 		data[o++] = enetc_port_rd(hw, enetc_port_counters[i].reg);
305 }
306 
307 static void enetc_pause_stats(struct enetc_hw *hw, int mac,
308 			      struct ethtool_pause_stats *pause_stats)
309 {
310 	pause_stats->tx_pause_frames = enetc_port_rd(hw, ENETC_PM_TXPF(mac));
311 	pause_stats->rx_pause_frames = enetc_port_rd(hw, ENETC_PM_RXPF(mac));
312 }
313 
314 static void enetc_get_pause_stats(struct net_device *ndev,
315 				  struct ethtool_pause_stats *pause_stats)
316 {
317 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
318 	struct enetc_hw *hw = &priv->si->hw;
319 	struct enetc_si *si = priv->si;
320 
321 	switch (pause_stats->src) {
322 	case ETHTOOL_MAC_STATS_SRC_EMAC:
323 		enetc_pause_stats(hw, 0, pause_stats);
324 		break;
325 	case ETHTOOL_MAC_STATS_SRC_PMAC:
326 		if (si->hw_features & ENETC_SI_F_QBU)
327 			enetc_pause_stats(hw, 1, pause_stats);
328 		break;
329 	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
330 		ethtool_aggregate_pause_stats(ndev, pause_stats);
331 		break;
332 	}
333 }
334 
335 static void enetc_mac_stats(struct enetc_hw *hw, int mac,
336 			    struct ethtool_eth_mac_stats *s)
337 {
338 	s->FramesTransmittedOK = enetc_port_rd(hw, ENETC_PM_TFRM(mac));
339 	s->SingleCollisionFrames = enetc_port_rd(hw, ENETC_PM_TSCOL(mac));
340 	s->MultipleCollisionFrames = enetc_port_rd(hw, ENETC_PM_TMCOL(mac));
341 	s->FramesReceivedOK = enetc_port_rd(hw, ENETC_PM_RFRM(mac));
342 	s->FrameCheckSequenceErrors = enetc_port_rd(hw, ENETC_PM_RFCS(mac));
343 	s->AlignmentErrors = enetc_port_rd(hw, ENETC_PM_RALN(mac));
344 	s->OctetsTransmittedOK = enetc_port_rd(hw, ENETC_PM_TEOCT(mac));
345 	s->FramesWithDeferredXmissions = enetc_port_rd(hw, ENETC_PM_TDFR(mac));
346 	s->LateCollisions = enetc_port_rd(hw, ENETC_PM_TLCOL(mac));
347 	s->FramesAbortedDueToXSColls = enetc_port_rd(hw, ENETC_PM_TECOL(mac));
348 	s->FramesLostDueToIntMACXmitError = enetc_port_rd(hw, ENETC_PM_TERR(mac));
349 	s->CarrierSenseErrors = enetc_port_rd(hw, ENETC_PM_TCRSE(mac));
350 	s->OctetsReceivedOK = enetc_port_rd(hw, ENETC_PM_REOCT(mac));
351 	s->FramesLostDueToIntMACRcvError = enetc_port_rd(hw, ENETC_PM_RDRNTP(mac));
352 	s->MulticastFramesXmittedOK = enetc_port_rd(hw, ENETC_PM_TMCA(mac));
353 	s->BroadcastFramesXmittedOK = enetc_port_rd(hw, ENETC_PM_TBCA(mac));
354 	s->MulticastFramesReceivedOK = enetc_port_rd(hw, ENETC_PM_RMCA(mac));
355 	s->BroadcastFramesReceivedOK = enetc_port_rd(hw, ENETC_PM_RBCA(mac));
356 }
357 
358 static void enetc_ctrl_stats(struct enetc_hw *hw, int mac,
359 			     struct ethtool_eth_ctrl_stats *s)
360 {
361 	s->MACControlFramesTransmitted = enetc_port_rd(hw, ENETC_PM_TCNP(mac));
362 	s->MACControlFramesReceived = enetc_port_rd(hw, ENETC_PM_RCNP(mac));
363 }
364 
365 static const struct ethtool_rmon_hist_range enetc_rmon_ranges[] = {
366 	{   64,   64 },
367 	{   65,  127 },
368 	{  128,  255 },
369 	{  256,  511 },
370 	{  512, 1023 },
371 	{ 1024, 1522 },
372 	{ 1523, ENETC_MAC_MAXFRM_SIZE },
373 	{},
374 };
375 
376 static void enetc_rmon_stats(struct enetc_hw *hw, int mac,
377 			     struct ethtool_rmon_stats *s)
378 {
379 	s->undersize_pkts = enetc_port_rd(hw, ENETC_PM_RUND(mac));
380 	s->oversize_pkts = enetc_port_rd(hw, ENETC_PM_ROVR(mac));
381 	s->fragments = enetc_port_rd(hw, ENETC_PM_RFRG(mac));
382 	s->jabbers = enetc_port_rd(hw, ENETC_PM_RJBR(mac));
383 
384 	s->hist[0] = enetc_port_rd(hw, ENETC_PM_R64(mac));
385 	s->hist[1] = enetc_port_rd(hw, ENETC_PM_R127(mac));
386 	s->hist[2] = enetc_port_rd(hw, ENETC_PM_R255(mac));
387 	s->hist[3] = enetc_port_rd(hw, ENETC_PM_R511(mac));
388 	s->hist[4] = enetc_port_rd(hw, ENETC_PM_R1023(mac));
389 	s->hist[5] = enetc_port_rd(hw, ENETC_PM_R1522(mac));
390 	s->hist[6] = enetc_port_rd(hw, ENETC_PM_R1523X(mac));
391 
392 	s->hist_tx[0] = enetc_port_rd(hw, ENETC_PM_T64(mac));
393 	s->hist_tx[1] = enetc_port_rd(hw, ENETC_PM_T127(mac));
394 	s->hist_tx[2] = enetc_port_rd(hw, ENETC_PM_T255(mac));
395 	s->hist_tx[3] = enetc_port_rd(hw, ENETC_PM_T511(mac));
396 	s->hist_tx[4] = enetc_port_rd(hw, ENETC_PM_T1023(mac));
397 	s->hist_tx[5] = enetc_port_rd(hw, ENETC_PM_T1522(mac));
398 	s->hist_tx[6] = enetc_port_rd(hw, ENETC_PM_T1523X(mac));
399 }
400 
401 static void enetc_get_eth_mac_stats(struct net_device *ndev,
402 				    struct ethtool_eth_mac_stats *mac_stats)
403 {
404 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
405 	struct enetc_hw *hw = &priv->si->hw;
406 	struct enetc_si *si = priv->si;
407 
408 	switch (mac_stats->src) {
409 	case ETHTOOL_MAC_STATS_SRC_EMAC:
410 		enetc_mac_stats(hw, 0, mac_stats);
411 		break;
412 	case ETHTOOL_MAC_STATS_SRC_PMAC:
413 		if (si->hw_features & ENETC_SI_F_QBU)
414 			enetc_mac_stats(hw, 1, mac_stats);
415 		break;
416 	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
417 		ethtool_aggregate_mac_stats(ndev, mac_stats);
418 		break;
419 	}
420 }
421 
422 static void enetc_get_eth_ctrl_stats(struct net_device *ndev,
423 				     struct ethtool_eth_ctrl_stats *ctrl_stats)
424 {
425 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
426 	struct enetc_hw *hw = &priv->si->hw;
427 	struct enetc_si *si = priv->si;
428 
429 	switch (ctrl_stats->src) {
430 	case ETHTOOL_MAC_STATS_SRC_EMAC:
431 		enetc_ctrl_stats(hw, 0, ctrl_stats);
432 		break;
433 	case ETHTOOL_MAC_STATS_SRC_PMAC:
434 		if (si->hw_features & ENETC_SI_F_QBU)
435 			enetc_ctrl_stats(hw, 1, ctrl_stats);
436 		break;
437 	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
438 		ethtool_aggregate_ctrl_stats(ndev, ctrl_stats);
439 		break;
440 	}
441 }
442 
443 static void enetc_get_rmon_stats(struct net_device *ndev,
444 				 struct ethtool_rmon_stats *rmon_stats,
445 				 const struct ethtool_rmon_hist_range **ranges)
446 {
447 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
448 	struct enetc_hw *hw = &priv->si->hw;
449 	struct enetc_si *si = priv->si;
450 
451 	*ranges = enetc_rmon_ranges;
452 
453 	switch (rmon_stats->src) {
454 	case ETHTOOL_MAC_STATS_SRC_EMAC:
455 		enetc_rmon_stats(hw, 0, rmon_stats);
456 		break;
457 	case ETHTOOL_MAC_STATS_SRC_PMAC:
458 		if (si->hw_features & ENETC_SI_F_QBU)
459 			enetc_rmon_stats(hw, 1, rmon_stats);
460 		break;
461 	case ETHTOOL_MAC_STATS_SRC_AGGREGATE:
462 		ethtool_aggregate_rmon_stats(ndev, rmon_stats);
463 		break;
464 	}
465 }
466 
467 #define ENETC_RSSHASH_L3 (RXH_L2DA | RXH_VLAN | RXH_L3_PROTO | RXH_IP_SRC | \
468 			  RXH_IP_DST)
469 #define ENETC_RSSHASH_L4 (ENETC_RSSHASH_L3 | RXH_L4_B_0_1 | RXH_L4_B_2_3)
470 static int enetc_get_rsshash(struct ethtool_rxnfc *rxnfc)
471 {
472 	static const u32 rsshash[] = {
473 			[TCP_V4_FLOW]    = ENETC_RSSHASH_L4,
474 			[UDP_V4_FLOW]    = ENETC_RSSHASH_L4,
475 			[SCTP_V4_FLOW]   = ENETC_RSSHASH_L4,
476 			[AH_ESP_V4_FLOW] = ENETC_RSSHASH_L3,
477 			[IPV4_FLOW]      = ENETC_RSSHASH_L3,
478 			[TCP_V6_FLOW]    = ENETC_RSSHASH_L4,
479 			[UDP_V6_FLOW]    = ENETC_RSSHASH_L4,
480 			[SCTP_V6_FLOW]   = ENETC_RSSHASH_L4,
481 			[AH_ESP_V6_FLOW] = ENETC_RSSHASH_L3,
482 			[IPV6_FLOW]      = ENETC_RSSHASH_L3,
483 			[ETHER_FLOW]     = 0,
484 	};
485 
486 	if (rxnfc->flow_type >= ARRAY_SIZE(rsshash))
487 		return -EINVAL;
488 
489 	rxnfc->data = rsshash[rxnfc->flow_type];
490 
491 	return 0;
492 }
493 
494 /* current HW spec does byte reversal on everything including MAC addresses */
495 static void ether_addr_copy_swap(u8 *dst, const u8 *src)
496 {
497 	int i;
498 
499 	for (i = 0; i < ETH_ALEN; i++)
500 		dst[i] = src[ETH_ALEN - i - 1];
501 }
502 
503 static int enetc_set_cls_entry(struct enetc_si *si,
504 			       struct ethtool_rx_flow_spec *fs, bool en)
505 {
506 	struct ethtool_tcpip4_spec *l4ip4_h, *l4ip4_m;
507 	struct ethtool_usrip4_spec *l3ip4_h, *l3ip4_m;
508 	struct ethhdr *eth_h, *eth_m;
509 	struct enetc_cmd_rfse rfse = { {0} };
510 
511 	if (!en)
512 		goto done;
513 
514 	switch (fs->flow_type & 0xff) {
515 	case TCP_V4_FLOW:
516 		l4ip4_h = &fs->h_u.tcp_ip4_spec;
517 		l4ip4_m = &fs->m_u.tcp_ip4_spec;
518 		goto l4ip4;
519 	case UDP_V4_FLOW:
520 		l4ip4_h = &fs->h_u.udp_ip4_spec;
521 		l4ip4_m = &fs->m_u.udp_ip4_spec;
522 		goto l4ip4;
523 	case SCTP_V4_FLOW:
524 		l4ip4_h = &fs->h_u.sctp_ip4_spec;
525 		l4ip4_m = &fs->m_u.sctp_ip4_spec;
526 l4ip4:
527 		rfse.sip_h[0] = l4ip4_h->ip4src;
528 		rfse.sip_m[0] = l4ip4_m->ip4src;
529 		rfse.dip_h[0] = l4ip4_h->ip4dst;
530 		rfse.dip_m[0] = l4ip4_m->ip4dst;
531 		rfse.sport_h = ntohs(l4ip4_h->psrc);
532 		rfse.sport_m = ntohs(l4ip4_m->psrc);
533 		rfse.dport_h = ntohs(l4ip4_h->pdst);
534 		rfse.dport_m = ntohs(l4ip4_m->pdst);
535 		if (l4ip4_m->tos)
536 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
537 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
538 		rfse.ethtype_m = 0xffff;
539 		break;
540 	case IP_USER_FLOW:
541 		l3ip4_h = &fs->h_u.usr_ip4_spec;
542 		l3ip4_m = &fs->m_u.usr_ip4_spec;
543 
544 		rfse.sip_h[0] = l3ip4_h->ip4src;
545 		rfse.sip_m[0] = l3ip4_m->ip4src;
546 		rfse.dip_h[0] = l3ip4_h->ip4dst;
547 		rfse.dip_m[0] = l3ip4_m->ip4dst;
548 		if (l3ip4_m->tos)
549 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
550 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
551 		rfse.ethtype_m = 0xffff;
552 		break;
553 	case ETHER_FLOW:
554 		eth_h = &fs->h_u.ether_spec;
555 		eth_m = &fs->m_u.ether_spec;
556 
557 		ether_addr_copy_swap(rfse.smac_h, eth_h->h_source);
558 		ether_addr_copy_swap(rfse.smac_m, eth_m->h_source);
559 		ether_addr_copy_swap(rfse.dmac_h, eth_h->h_dest);
560 		ether_addr_copy_swap(rfse.dmac_m, eth_m->h_dest);
561 		rfse.ethtype_h = ntohs(eth_h->h_proto);
562 		rfse.ethtype_m = ntohs(eth_m->h_proto);
563 		break;
564 	default:
565 		return -EOPNOTSUPP;
566 	}
567 
568 	rfse.mode |= ENETC_RFSE_EN;
569 	if (fs->ring_cookie != RX_CLS_FLOW_DISC) {
570 		rfse.mode |= ENETC_RFSE_MODE_BD;
571 		rfse.result = fs->ring_cookie;
572 	}
573 done:
574 	return enetc_set_fs_entry(si, &rfse, fs->location);
575 }
576 
577 static int enetc_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc,
578 			   u32 *rule_locs)
579 {
580 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
581 	int i, j;
582 
583 	switch (rxnfc->cmd) {
584 	case ETHTOOL_GRXRINGS:
585 		rxnfc->data = priv->num_rx_rings;
586 		break;
587 	case ETHTOOL_GRXFH:
588 		/* get RSS hash config */
589 		return enetc_get_rsshash(rxnfc);
590 	case ETHTOOL_GRXCLSRLCNT:
591 		/* total number of entries */
592 		rxnfc->data = priv->si->num_fs_entries;
593 		/* number of entries in use */
594 		rxnfc->rule_cnt = 0;
595 		for (i = 0; i < priv->si->num_fs_entries; i++)
596 			if (priv->cls_rules[i].used)
597 				rxnfc->rule_cnt++;
598 		break;
599 	case ETHTOOL_GRXCLSRULE:
600 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
601 			return -EINVAL;
602 
603 		/* get entry x */
604 		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
605 		break;
606 	case ETHTOOL_GRXCLSRLALL:
607 		/* total number of entries */
608 		rxnfc->data = priv->si->num_fs_entries;
609 		/* array of indexes of used entries */
610 		j = 0;
611 		for (i = 0; i < priv->si->num_fs_entries; i++) {
612 			if (!priv->cls_rules[i].used)
613 				continue;
614 			if (j == rxnfc->rule_cnt)
615 				return -EMSGSIZE;
616 			rule_locs[j++] = i;
617 		}
618 		/* number of entries in use */
619 		rxnfc->rule_cnt = j;
620 		break;
621 	default:
622 		return -EOPNOTSUPP;
623 	}
624 
625 	return 0;
626 }
627 
628 /* i.MX95 ENETC does not support RFS table, but we can use ingress port
629  * filter table to implement Wake-on-LAN filter or drop the matched flow,
630  * so the implementation will be different from enetc_get_rxnfc() and
631  * enetc_set_rxnfc(). Therefore, add enetc4_get_rxnfc() for ENETC v4 PF.
632  */
633 static int enetc4_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc,
634 			    u32 *rule_locs)
635 {
636 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
637 
638 	switch (rxnfc->cmd) {
639 	case ETHTOOL_GRXRINGS:
640 		rxnfc->data = priv->num_rx_rings;
641 		break;
642 	case ETHTOOL_GRXFH:
643 		return enetc_get_rsshash(rxnfc);
644 	default:
645 		return -EOPNOTSUPP;
646 	}
647 
648 	return 0;
649 }
650 
651 static int enetc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc)
652 {
653 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
654 	int err;
655 
656 	switch (rxnfc->cmd) {
657 	case ETHTOOL_SRXCLSRLINS:
658 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
659 			return -EINVAL;
660 
661 		if (rxnfc->fs.ring_cookie >= priv->num_rx_rings &&
662 		    rxnfc->fs.ring_cookie != RX_CLS_FLOW_DISC)
663 			return -EINVAL;
664 
665 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, true);
666 		if (err)
667 			return err;
668 		priv->cls_rules[rxnfc->fs.location].fs = rxnfc->fs;
669 		priv->cls_rules[rxnfc->fs.location].used = 1;
670 		break;
671 	case ETHTOOL_SRXCLSRLDEL:
672 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
673 			return -EINVAL;
674 
675 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, false);
676 		if (err)
677 			return err;
678 		priv->cls_rules[rxnfc->fs.location].used = 0;
679 		break;
680 	default:
681 		return -EOPNOTSUPP;
682 	}
683 
684 	return 0;
685 }
686 
687 static u32 enetc_get_rxfh_key_size(struct net_device *ndev)
688 {
689 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
690 
691 	/* return the size of the RX flow hash key.  PF only */
692 	return (priv->si->hw.port) ? ENETC_RSSHASH_KEY_SIZE : 0;
693 }
694 
695 static u32 enetc_get_rxfh_indir_size(struct net_device *ndev)
696 {
697 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
698 
699 	/* return the size of the RX flow hash indirection table */
700 	return priv->si->num_rss;
701 }
702 
703 static int enetc_get_rss_key_base(struct enetc_si *si)
704 {
705 	if (is_enetc_rev1(si))
706 		return ENETC_PRSSK(0);
707 
708 	return ENETC4_PRSSKR(0);
709 }
710 
711 static void enetc_get_rss_key(struct enetc_si *si, const u8 *key)
712 {
713 	int base = enetc_get_rss_key_base(si);
714 	struct enetc_hw *hw = &si->hw;
715 	int i;
716 
717 	for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
718 		((u32 *)key)[i] = enetc_port_rd(hw, base + i * 4);
719 }
720 
721 static int enetc_get_rxfh(struct net_device *ndev,
722 			  struct ethtool_rxfh_param *rxfh)
723 {
724 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
725 	struct enetc_si *si = priv->si;
726 	int err = 0;
727 
728 	/* return hash function */
729 	rxfh->hfunc = ETH_RSS_HASH_TOP;
730 
731 	/* return hash key */
732 	if (rxfh->key && enetc_si_is_pf(si))
733 		enetc_get_rss_key(si, rxfh->key);
734 
735 	/* return RSS table */
736 	if (rxfh->indir)
737 		err = si->ops->get_rss_table(si, rxfh->indir, si->num_rss);
738 
739 	return err;
740 }
741 
742 void enetc_set_rss_key(struct enetc_si *si, const u8 *bytes)
743 {
744 	int base = enetc_get_rss_key_base(si);
745 	struct enetc_hw *hw = &si->hw;
746 	int i;
747 
748 	for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
749 		enetc_port_wr(hw, base + i * 4, ((u32 *)bytes)[i]);
750 }
751 EXPORT_SYMBOL_GPL(enetc_set_rss_key);
752 
753 static int enetc_set_rxfh(struct net_device *ndev,
754 			  struct ethtool_rxfh_param *rxfh,
755 			  struct netlink_ext_ack *extack)
756 {
757 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
758 	struct enetc_si *si = priv->si;
759 	int err = 0;
760 
761 	/* set hash key, if PF */
762 	if (rxfh->key && enetc_si_is_pf(si))
763 		enetc_set_rss_key(si, rxfh->key);
764 
765 	/* set RSS table */
766 	if (rxfh->indir)
767 		err = si->ops->set_rss_table(si, rxfh->indir, si->num_rss);
768 
769 	return err;
770 }
771 
772 static void enetc_get_ringparam(struct net_device *ndev,
773 				struct ethtool_ringparam *ring,
774 				struct kernel_ethtool_ringparam *kernel_ring,
775 				struct netlink_ext_ack *extack)
776 {
777 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
778 
779 	ring->rx_pending = priv->rx_bd_count;
780 	ring->tx_pending = priv->tx_bd_count;
781 
782 	/* do some h/w sanity checks for BDR length */
783 	if (netif_running(ndev)) {
784 		struct enetc_hw *hw = &priv->si->hw;
785 		u32 val = enetc_rxbdr_rd(hw, 0, ENETC_RBLENR);
786 
787 		if (val != priv->rx_bd_count)
788 			netif_err(priv, hw, ndev, "RxBDR[RBLENR] = %d!\n", val);
789 
790 		val = enetc_txbdr_rd(hw, 0, ENETC_TBLENR);
791 
792 		if (val != priv->tx_bd_count)
793 			netif_err(priv, hw, ndev, "TxBDR[TBLENR] = %d!\n", val);
794 	}
795 }
796 
797 static int enetc_get_coalesce(struct net_device *ndev,
798 			      struct ethtool_coalesce *ic,
799 			      struct kernel_ethtool_coalesce *kernel_coal,
800 			      struct netlink_ext_ack *extack)
801 {
802 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
803 	struct enetc_int_vector *v = priv->int_vector[0];
804 	u64 clk_freq = priv->sysclk_freq;
805 
806 	ic->tx_coalesce_usecs = enetc_cycles_to_usecs(priv->tx_ictt, clk_freq);
807 	ic->rx_coalesce_usecs = enetc_cycles_to_usecs(v->rx_ictt, clk_freq);
808 
809 	ic->tx_max_coalesced_frames = ENETC_TXIC_PKTTHR;
810 	ic->rx_max_coalesced_frames = ENETC_RXIC_PKTTHR;
811 
812 	ic->use_adaptive_rx_coalesce = priv->ic_mode & ENETC_IC_RX_ADAPTIVE;
813 
814 	return 0;
815 }
816 
817 static int enetc_set_coalesce(struct net_device *ndev,
818 			      struct ethtool_coalesce *ic,
819 			      struct kernel_ethtool_coalesce *kernel_coal,
820 			      struct netlink_ext_ack *extack)
821 {
822 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
823 	u64 clk_freq = priv->sysclk_freq;
824 	u32 rx_ictt, tx_ictt;
825 	int i, ic_mode;
826 	bool changed;
827 
828 	tx_ictt = enetc_usecs_to_cycles(ic->tx_coalesce_usecs, clk_freq);
829 	rx_ictt = enetc_usecs_to_cycles(ic->rx_coalesce_usecs, clk_freq);
830 
831 	if (ic->rx_max_coalesced_frames != ENETC_RXIC_PKTTHR)
832 		return -EOPNOTSUPP;
833 
834 	if (ic->tx_max_coalesced_frames != ENETC_TXIC_PKTTHR)
835 		return -EOPNOTSUPP;
836 
837 	ic_mode = ENETC_IC_NONE;
838 	if (ic->use_adaptive_rx_coalesce) {
839 		ic_mode |= ENETC_IC_RX_ADAPTIVE;
840 		rx_ictt = 0x1;
841 	} else {
842 		ic_mode |= rx_ictt ? ENETC_IC_RX_MANUAL : 0;
843 	}
844 
845 	ic_mode |= tx_ictt ? ENETC_IC_TX_MANUAL : 0;
846 
847 	/* commit the settings */
848 	changed = (ic_mode != priv->ic_mode) || (priv->tx_ictt != tx_ictt);
849 
850 	priv->ic_mode = ic_mode;
851 	priv->tx_ictt = tx_ictt;
852 
853 	for (i = 0; i < priv->bdr_int_num; i++) {
854 		struct enetc_int_vector *v = priv->int_vector[i];
855 
856 		v->rx_ictt = rx_ictt;
857 		v->rx_dim_en = !!(ic_mode & ENETC_IC_RX_ADAPTIVE);
858 	}
859 
860 	if (netif_running(ndev) && changed) {
861 		/* reconfigure the operation mode of h/w interrupts,
862 		 * traffic needs to be paused in the process
863 		 */
864 		enetc_stop(ndev);
865 		enetc_start(ndev);
866 	}
867 
868 	return 0;
869 }
870 
871 static int enetc_get_ts_info(struct net_device *ndev,
872 			     struct kernel_ethtool_ts_info *info)
873 {
874 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
875 	int *phc_idx;
876 
877 	phc_idx = symbol_get(enetc_phc_index);
878 	if (phc_idx) {
879 		info->phc_index = *phc_idx;
880 		symbol_put(enetc_phc_index);
881 	}
882 
883 	if (!IS_ENABLED(CONFIG_FSL_ENETC_PTP_CLOCK)) {
884 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE;
885 
886 		return 0;
887 	}
888 
889 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
890 				SOF_TIMESTAMPING_RX_HARDWARE |
891 				SOF_TIMESTAMPING_RAW_HARDWARE |
892 				SOF_TIMESTAMPING_TX_SOFTWARE;
893 
894 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
895 			 (1 << HWTSTAMP_TX_ON);
896 
897 	if (enetc_si_is_pf(priv->si))
898 		info->tx_types |= (1 << HWTSTAMP_TX_ONESTEP_SYNC);
899 
900 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
901 			   (1 << HWTSTAMP_FILTER_ALL);
902 
903 	return 0;
904 }
905 
906 static void enetc_get_wol(struct net_device *dev,
907 			  struct ethtool_wolinfo *wol)
908 {
909 	wol->supported = 0;
910 	wol->wolopts = 0;
911 
912 	if (dev->phydev)
913 		phy_ethtool_get_wol(dev->phydev, wol);
914 }
915 
916 static int enetc_set_wol(struct net_device *dev,
917 			 struct ethtool_wolinfo *wol)
918 {
919 	int ret;
920 
921 	if (!dev->phydev)
922 		return -EOPNOTSUPP;
923 
924 	ret = phy_ethtool_set_wol(dev->phydev, wol);
925 	if (!ret)
926 		device_set_wakeup_enable(&dev->dev, wol->wolopts);
927 
928 	return ret;
929 }
930 
931 static void enetc_get_pauseparam(struct net_device *dev,
932 				 struct ethtool_pauseparam *pause)
933 {
934 	struct enetc_ndev_priv *priv = netdev_priv(dev);
935 
936 	phylink_ethtool_get_pauseparam(priv->phylink, pause);
937 }
938 
939 static int enetc_set_pauseparam(struct net_device *dev,
940 				struct ethtool_pauseparam *pause)
941 {
942 	struct enetc_ndev_priv *priv = netdev_priv(dev);
943 
944 	return phylink_ethtool_set_pauseparam(priv->phylink, pause);
945 }
946 
947 static int enetc_get_link_ksettings(struct net_device *dev,
948 				    struct ethtool_link_ksettings *cmd)
949 {
950 	struct enetc_ndev_priv *priv = netdev_priv(dev);
951 
952 	if (!priv->phylink)
953 		return -EOPNOTSUPP;
954 
955 	return phylink_ethtool_ksettings_get(priv->phylink, cmd);
956 }
957 
958 static int enetc_set_link_ksettings(struct net_device *dev,
959 				    const struct ethtool_link_ksettings *cmd)
960 {
961 	struct enetc_ndev_priv *priv = netdev_priv(dev);
962 
963 	if (!priv->phylink)
964 		return -EOPNOTSUPP;
965 
966 	return phylink_ethtool_ksettings_set(priv->phylink, cmd);
967 }
968 
969 static void enetc_get_mm_stats(struct net_device *ndev,
970 			       struct ethtool_mm_stats *s)
971 {
972 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
973 	struct enetc_hw *hw = &priv->si->hw;
974 	struct enetc_si *si = priv->si;
975 
976 	if (!(si->hw_features & ENETC_SI_F_QBU))
977 		return;
978 
979 	s->MACMergeFrameAssErrorCount = enetc_port_rd(hw, ENETC_MMFAECR);
980 	s->MACMergeFrameSmdErrorCount = enetc_port_rd(hw, ENETC_MMFSECR);
981 	s->MACMergeFrameAssOkCount = enetc_port_rd(hw, ENETC_MMFAOCR);
982 	s->MACMergeFragCountRx = enetc_port_rd(hw, ENETC_MMFCRXR);
983 	s->MACMergeFragCountTx = enetc_port_rd(hw, ENETC_MMFCTXR);
984 	s->MACMergeHoldCount = enetc_port_rd(hw, ENETC_MMHCR);
985 }
986 
987 static int enetc_get_mm(struct net_device *ndev, struct ethtool_mm_state *state)
988 {
989 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
990 	struct enetc_si *si = priv->si;
991 	struct enetc_hw *hw = &si->hw;
992 	u32 lafs, rafs, val;
993 
994 	if (!(si->hw_features & ENETC_SI_F_QBU))
995 		return -EOPNOTSUPP;
996 
997 	mutex_lock(&priv->mm_lock);
998 
999 	val = enetc_port_rd(hw, ENETC_PFPMR);
1000 	state->pmac_enabled = !!(val & ENETC_PFPMR_PMACE);
1001 
1002 	val = enetc_port_rd(hw, ENETC_MMCSR);
1003 
1004 	switch (ENETC_MMCSR_GET_VSTS(val)) {
1005 	case 0:
1006 		state->verify_status = ETHTOOL_MM_VERIFY_STATUS_DISABLED;
1007 		break;
1008 	case 2:
1009 		state->verify_status = ETHTOOL_MM_VERIFY_STATUS_VERIFYING;
1010 		break;
1011 	case 3:
1012 		state->verify_status = ETHTOOL_MM_VERIFY_STATUS_SUCCEEDED;
1013 		break;
1014 	case 4:
1015 		state->verify_status = ETHTOOL_MM_VERIFY_STATUS_FAILED;
1016 		break;
1017 	case 5:
1018 	default:
1019 		state->verify_status = ETHTOOL_MM_VERIFY_STATUS_UNKNOWN;
1020 		break;
1021 	}
1022 
1023 	rafs = ENETC_MMCSR_GET_RAFS(val);
1024 	state->tx_min_frag_size = ethtool_mm_frag_size_add_to_min(rafs);
1025 	lafs = ENETC_MMCSR_GET_LAFS(val);
1026 	state->rx_min_frag_size = ethtool_mm_frag_size_add_to_min(lafs);
1027 	state->tx_enabled = !!(val & ENETC_MMCSR_LPE); /* mirror of MMCSR_ME */
1028 	state->tx_active = state->tx_enabled &&
1029 			   (state->verify_status == ETHTOOL_MM_VERIFY_STATUS_SUCCEEDED ||
1030 			    state->verify_status == ETHTOOL_MM_VERIFY_STATUS_DISABLED);
1031 	state->verify_enabled = !(val & ENETC_MMCSR_VDIS);
1032 	state->verify_time = ENETC_MMCSR_GET_VT(val);
1033 	/* A verifyTime of 128 ms would exceed the 7 bit width
1034 	 * of the ENETC_MMCSR_VT field
1035 	 */
1036 	state->max_verify_time = 127;
1037 
1038 	mutex_unlock(&priv->mm_lock);
1039 
1040 	return 0;
1041 }
1042 
1043 static int enetc_mm_wait_tx_active(struct enetc_hw *hw, int verify_time)
1044 {
1045 	int timeout = verify_time * USEC_PER_MSEC * ENETC_MM_VERIFY_RETRIES;
1046 	u32 val;
1047 
1048 	/* This will time out after the standard value of 3 verification
1049 	 * attempts. To not sleep forever, it relies on a non-zero verify_time,
1050 	 * guarantee which is provided by the ethtool nlattr policy.
1051 	 */
1052 	return read_poll_timeout(enetc_port_rd, val,
1053 				 ENETC_MMCSR_GET_VSTS(val) == 3,
1054 				 ENETC_MM_VERIFY_SLEEP_US, timeout,
1055 				 true, hw, ENETC_MMCSR);
1056 }
1057 
1058 static void enetc_set_ptcfpr(struct enetc_hw *hw, u8 preemptible_tcs)
1059 {
1060 	u32 val;
1061 	int tc;
1062 
1063 	for (tc = 0; tc < 8; tc++) {
1064 		val = enetc_port_rd(hw, ENETC_PTCFPR(tc));
1065 
1066 		if (preemptible_tcs & BIT(tc))
1067 			val |= ENETC_PTCFPR_FPE;
1068 		else
1069 			val &= ~ENETC_PTCFPR_FPE;
1070 
1071 		enetc_port_wr(hw, ENETC_PTCFPR(tc), val);
1072 	}
1073 }
1074 
1075 /* ENETC does not have an IRQ to notify changes to the MAC Merge TX status
1076  * (active/inactive), but the preemptible traffic classes should only be
1077  * committed to hardware once TX is active. Resort to polling.
1078  */
1079 void enetc_mm_commit_preemptible_tcs(struct enetc_ndev_priv *priv)
1080 {
1081 	struct enetc_hw *hw = &priv->si->hw;
1082 	u8 preemptible_tcs = 0;
1083 	u32 val;
1084 	int err;
1085 
1086 	val = enetc_port_rd(hw, ENETC_MMCSR);
1087 	if (!(val & ENETC_MMCSR_ME))
1088 		goto out;
1089 
1090 	if (!(val & ENETC_MMCSR_VDIS)) {
1091 		err = enetc_mm_wait_tx_active(hw, ENETC_MMCSR_GET_VT(val));
1092 		if (err)
1093 			goto out;
1094 	}
1095 
1096 	preemptible_tcs = priv->preemptible_tcs;
1097 out:
1098 	enetc_set_ptcfpr(hw, preemptible_tcs);
1099 }
1100 
1101 /* FIXME: Workaround for the link partner's verification failing if ENETC
1102  * priorly received too much express traffic. The documentation doesn't
1103  * suggest this is needed.
1104  */
1105 static void enetc_restart_emac_rx(struct enetc_si *si)
1106 {
1107 	u32 val = enetc_port_rd(&si->hw, ENETC_PM0_CMD_CFG);
1108 
1109 	enetc_port_wr(&si->hw, ENETC_PM0_CMD_CFG, val & ~ENETC_PM0_RX_EN);
1110 
1111 	if (val & ENETC_PM0_RX_EN)
1112 		enetc_port_wr(&si->hw, ENETC_PM0_CMD_CFG, val);
1113 }
1114 
1115 static int enetc_set_mm(struct net_device *ndev, struct ethtool_mm_cfg *cfg,
1116 			struct netlink_ext_ack *extack)
1117 {
1118 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1119 	struct enetc_hw *hw = &priv->si->hw;
1120 	struct enetc_si *si = priv->si;
1121 	u32 val, add_frag_size;
1122 	int err;
1123 
1124 	if (!(si->hw_features & ENETC_SI_F_QBU))
1125 		return -EOPNOTSUPP;
1126 
1127 	err = ethtool_mm_frag_size_min_to_add(cfg->tx_min_frag_size,
1128 					      &add_frag_size, extack);
1129 	if (err)
1130 		return err;
1131 
1132 	mutex_lock(&priv->mm_lock);
1133 
1134 	val = enetc_port_rd(hw, ENETC_PFPMR);
1135 	if (cfg->pmac_enabled)
1136 		val |= ENETC_PFPMR_PMACE;
1137 	else
1138 		val &= ~ENETC_PFPMR_PMACE;
1139 	enetc_port_wr(hw, ENETC_PFPMR, val);
1140 
1141 	val = enetc_port_rd(hw, ENETC_MMCSR);
1142 
1143 	if (cfg->verify_enabled)
1144 		val &= ~ENETC_MMCSR_VDIS;
1145 	else
1146 		val |= ENETC_MMCSR_VDIS;
1147 
1148 	if (cfg->tx_enabled)
1149 		priv->active_offloads |= ENETC_F_QBU;
1150 	else
1151 		priv->active_offloads &= ~ENETC_F_QBU;
1152 
1153 	/* If link is up, enable/disable MAC Merge right away */
1154 	if (!(val & ENETC_MMCSR_LINK_FAIL)) {
1155 		if (!!(priv->active_offloads & ENETC_F_QBU))
1156 			val |= ENETC_MMCSR_ME;
1157 		else
1158 			val &= ~ENETC_MMCSR_ME;
1159 	}
1160 
1161 	val &= ~ENETC_MMCSR_VT_MASK;
1162 	val |= ENETC_MMCSR_VT(cfg->verify_time);
1163 
1164 	val &= ~ENETC_MMCSR_RAFS_MASK;
1165 	val |= ENETC_MMCSR_RAFS(add_frag_size);
1166 
1167 	enetc_port_wr(hw, ENETC_MMCSR, val);
1168 
1169 	enetc_restart_emac_rx(priv->si);
1170 
1171 	enetc_mm_commit_preemptible_tcs(priv);
1172 
1173 	mutex_unlock(&priv->mm_lock);
1174 
1175 	return 0;
1176 }
1177 
1178 /* When the link is lost, the verification state machine goes to the FAILED
1179  * state and doesn't restart on its own after a new link up event.
1180  * According to 802.3 Figure 99-8 - Verify state diagram, the LINK_FAIL bit
1181  * should have been sufficient to re-trigger verification, but for ENETC it
1182  * doesn't. As a workaround, we need to toggle the Merge Enable bit to
1183  * re-trigger verification when link comes up.
1184  */
1185 void enetc_mm_link_state_update(struct enetc_ndev_priv *priv, bool link)
1186 {
1187 	struct enetc_hw *hw = &priv->si->hw;
1188 	u32 val;
1189 
1190 	mutex_lock(&priv->mm_lock);
1191 
1192 	val = enetc_port_rd(hw, ENETC_MMCSR);
1193 
1194 	if (link) {
1195 		val &= ~ENETC_MMCSR_LINK_FAIL;
1196 		if (priv->active_offloads & ENETC_F_QBU)
1197 			val |= ENETC_MMCSR_ME;
1198 	} else {
1199 		val |= ENETC_MMCSR_LINK_FAIL;
1200 		if (priv->active_offloads & ENETC_F_QBU)
1201 			val &= ~ENETC_MMCSR_ME;
1202 	}
1203 
1204 	enetc_port_wr(hw, ENETC_MMCSR, val);
1205 
1206 	enetc_mm_commit_preemptible_tcs(priv);
1207 
1208 	mutex_unlock(&priv->mm_lock);
1209 }
1210 EXPORT_SYMBOL_GPL(enetc_mm_link_state_update);
1211 
1212 const struct ethtool_ops enetc_pf_ethtool_ops = {
1213 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1214 				     ETHTOOL_COALESCE_MAX_FRAMES |
1215 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
1216 	.get_regs_len = enetc_get_reglen,
1217 	.get_regs = enetc_get_regs,
1218 	.get_sset_count = enetc_get_sset_count,
1219 	.get_strings = enetc_get_strings,
1220 	.get_ethtool_stats = enetc_get_ethtool_stats,
1221 	.get_pause_stats = enetc_get_pause_stats,
1222 	.get_rmon_stats = enetc_get_rmon_stats,
1223 	.get_eth_ctrl_stats = enetc_get_eth_ctrl_stats,
1224 	.get_eth_mac_stats = enetc_get_eth_mac_stats,
1225 	.get_rxnfc = enetc_get_rxnfc,
1226 	.set_rxnfc = enetc_set_rxnfc,
1227 	.get_rxfh_key_size = enetc_get_rxfh_key_size,
1228 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
1229 	.get_rxfh = enetc_get_rxfh,
1230 	.set_rxfh = enetc_set_rxfh,
1231 	.get_ringparam = enetc_get_ringparam,
1232 	.get_coalesce = enetc_get_coalesce,
1233 	.set_coalesce = enetc_set_coalesce,
1234 	.get_link_ksettings = enetc_get_link_ksettings,
1235 	.set_link_ksettings = enetc_set_link_ksettings,
1236 	.get_link = ethtool_op_get_link,
1237 	.get_ts_info = enetc_get_ts_info,
1238 	.get_wol = enetc_get_wol,
1239 	.set_wol = enetc_set_wol,
1240 	.get_pauseparam = enetc_get_pauseparam,
1241 	.set_pauseparam = enetc_set_pauseparam,
1242 	.get_mm = enetc_get_mm,
1243 	.set_mm = enetc_set_mm,
1244 	.get_mm_stats = enetc_get_mm_stats,
1245 };
1246 
1247 const struct ethtool_ops enetc_vf_ethtool_ops = {
1248 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1249 				     ETHTOOL_COALESCE_MAX_FRAMES |
1250 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
1251 	.get_regs_len = enetc_get_reglen,
1252 	.get_regs = enetc_get_regs,
1253 	.get_sset_count = enetc_get_sset_count,
1254 	.get_strings = enetc_get_strings,
1255 	.get_ethtool_stats = enetc_get_ethtool_stats,
1256 	.get_rxnfc = enetc_get_rxnfc,
1257 	.set_rxnfc = enetc_set_rxnfc,
1258 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
1259 	.get_rxfh = enetc_get_rxfh,
1260 	.set_rxfh = enetc_set_rxfh,
1261 	.get_ringparam = enetc_get_ringparam,
1262 	.get_coalesce = enetc_get_coalesce,
1263 	.set_coalesce = enetc_set_coalesce,
1264 	.get_link = ethtool_op_get_link,
1265 	.get_ts_info = enetc_get_ts_info,
1266 };
1267 
1268 const struct ethtool_ops enetc4_pf_ethtool_ops = {
1269 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1270 				     ETHTOOL_COALESCE_MAX_FRAMES |
1271 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
1272 	.get_ringparam = enetc_get_ringparam,
1273 	.get_coalesce = enetc_get_coalesce,
1274 	.set_coalesce = enetc_set_coalesce,
1275 	.get_link_ksettings = enetc_get_link_ksettings,
1276 	.set_link_ksettings = enetc_set_link_ksettings,
1277 	.get_link = ethtool_op_get_link,
1278 	.get_wol = enetc_get_wol,
1279 	.set_wol = enetc_set_wol,
1280 	.get_pauseparam = enetc_get_pauseparam,
1281 	.set_pauseparam = enetc_set_pauseparam,
1282 	.get_rxnfc = enetc4_get_rxnfc,
1283 	.get_rxfh_key_size = enetc_get_rxfh_key_size,
1284 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
1285 	.get_rxfh = enetc_get_rxfh,
1286 	.set_rxfh = enetc_set_rxfh,
1287 };
1288 
1289 void enetc_set_ethtool_ops(struct net_device *ndev)
1290 {
1291 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1292 
1293 	ndev->ethtool_ops = priv->si->drvdata->eth_ops;
1294 }
1295 EXPORT_SYMBOL_GPL(enetc_set_ethtool_ops);
1296