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