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