1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2017-2019 NXP */
3 
4 #include <linux/net_tstamp.h>
5 #include <linux/module.h>
6 #include "enetc.h"
7 
8 static const u32 enetc_si_regs[] = {
9 	ENETC_SIMR, ENETC_SIPMAR0, ENETC_SIPMAR1, ENETC_SICBDRMR,
10 	ENETC_SICBDRSR,	ENETC_SICBDRBAR0, ENETC_SICBDRBAR1, ENETC_SICBDRPIR,
11 	ENETC_SICBDRCIR, ENETC_SICBDRLENR, ENETC_SICAPR0, ENETC_SICAPR1,
12 	ENETC_SIUEFDCR
13 };
14 
15 static const u32 enetc_txbdr_regs[] = {
16 	ENETC_TBMR, ENETC_TBSR, ENETC_TBBAR0, ENETC_TBBAR1,
17 	ENETC_TBPIR, ENETC_TBCIR, ENETC_TBLENR, ENETC_TBIER, ENETC_TBICR0,
18 	ENETC_TBICR1
19 };
20 
21 static const u32 enetc_rxbdr_regs[] = {
22 	ENETC_RBMR, ENETC_RBSR, ENETC_RBBSR, ENETC_RBCIR, ENETC_RBBAR0,
23 	ENETC_RBBAR1, ENETC_RBPIR, ENETC_RBLENR, ENETC_RBIER, ENETC_RBICR0,
24 	ENETC_RBICR1
25 };
26 
27 static const u32 enetc_port_regs[] = {
28 	ENETC_PMR, ENETC_PSR, ENETC_PSIPMR, ENETC_PSIPMAR0(0),
29 	ENETC_PSIPMAR1(0), ENETC_PTXMBAR, ENETC_PCAPR0, ENETC_PCAPR1,
30 	ENETC_PSICFGR0(0), ENETC_PRFSCAPR, ENETC_PTCMSDUR(0),
31 	ENETC_PM0_CMD_CFG, ENETC_PM0_MAXFRM, ENETC_PM0_IF_MODE
32 };
33 
34 static int enetc_get_reglen(struct net_device *ndev)
35 {
36 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
37 	struct enetc_hw *hw = &priv->si->hw;
38 	int len;
39 
40 	len = ARRAY_SIZE(enetc_si_regs);
41 	len += ARRAY_SIZE(enetc_txbdr_regs) * priv->num_tx_rings;
42 	len += ARRAY_SIZE(enetc_rxbdr_regs) * priv->num_rx_rings;
43 
44 	if (hw->port)
45 		len += ARRAY_SIZE(enetc_port_regs);
46 
47 	len *= sizeof(u32) * 2; /* store 2 entries per reg: addr and value */
48 
49 	return len;
50 }
51 
52 static void enetc_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
53 			   void *regbuf)
54 {
55 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
56 	struct enetc_hw *hw = &priv->si->hw;
57 	u32 *buf = (u32 *)regbuf;
58 	int i, j;
59 	u32 addr;
60 
61 	for (i = 0; i < ARRAY_SIZE(enetc_si_regs); i++) {
62 		*buf++ = enetc_si_regs[i];
63 		*buf++ = enetc_rd(hw, enetc_si_regs[i]);
64 	}
65 
66 	for (i = 0; i < priv->num_tx_rings; i++) {
67 		for (j = 0; j < ARRAY_SIZE(enetc_txbdr_regs); j++) {
68 			addr = ENETC_BDR(TX, i, enetc_txbdr_regs[j]);
69 
70 			*buf++ = addr;
71 			*buf++ = enetc_rd(hw, addr);
72 		}
73 	}
74 
75 	for (i = 0; i < priv->num_rx_rings; i++) {
76 		for (j = 0; j < ARRAY_SIZE(enetc_rxbdr_regs); j++) {
77 			addr = ENETC_BDR(RX, i, enetc_rxbdr_regs[j]);
78 
79 			*buf++ = addr;
80 			*buf++ = enetc_rd(hw, addr);
81 		}
82 	}
83 
84 	if (!hw->port)
85 		return;
86 
87 	for (i = 0; i < ARRAY_SIZE(enetc_port_regs); i++) {
88 		addr = ENETC_PORT_BASE + enetc_port_regs[i];
89 		*buf++ = addr;
90 		*buf++ = enetc_rd(hw, addr);
91 	}
92 }
93 
94 static const struct {
95 	int reg;
96 	char name[ETH_GSTRING_LEN];
97 } enetc_si_counters[] =  {
98 	{ ENETC_SIROCT, "SI rx octets" },
99 	{ ENETC_SIRFRM, "SI rx frames" },
100 	{ ENETC_SIRUCA, "SI rx u-cast frames" },
101 	{ ENETC_SIRMCA, "SI rx m-cast frames" },
102 	{ ENETC_SITOCT, "SI tx octets" },
103 	{ ENETC_SITFRM, "SI tx frames" },
104 	{ ENETC_SITUCA, "SI tx u-cast frames" },
105 	{ ENETC_SITMCA, "SI tx m-cast frames" },
106 	{ ENETC_RBDCR(0), "Rx ring  0 discarded frames" },
107 	{ ENETC_RBDCR(1), "Rx ring  1 discarded frames" },
108 	{ ENETC_RBDCR(2), "Rx ring  2 discarded frames" },
109 	{ ENETC_RBDCR(3), "Rx ring  3 discarded frames" },
110 	{ ENETC_RBDCR(4), "Rx ring  4 discarded frames" },
111 	{ ENETC_RBDCR(5), "Rx ring  5 discarded frames" },
112 	{ ENETC_RBDCR(6), "Rx ring  6 discarded frames" },
113 	{ ENETC_RBDCR(7), "Rx ring  7 discarded frames" },
114 	{ ENETC_RBDCR(8), "Rx ring  8 discarded frames" },
115 	{ ENETC_RBDCR(9), "Rx ring  9 discarded frames" },
116 	{ ENETC_RBDCR(10), "Rx ring 10 discarded frames" },
117 	{ ENETC_RBDCR(11), "Rx ring 11 discarded frames" },
118 	{ ENETC_RBDCR(12), "Rx ring 12 discarded frames" },
119 	{ ENETC_RBDCR(13), "Rx ring 13 discarded frames" },
120 	{ ENETC_RBDCR(14), "Rx ring 14 discarded frames" },
121 	{ ENETC_RBDCR(15), "Rx ring 15 discarded frames" },
122 };
123 
124 static const struct {
125 	int reg;
126 	char name[ETH_GSTRING_LEN];
127 } enetc_port_counters[] = {
128 	{ ENETC_PM_REOCT(0),	"MAC rx ethernet octets" },
129 	{ ENETC_PM_RALN(0),	"MAC rx alignment errors" },
130 	{ ENETC_PM_RXPF(0),	"MAC rx valid pause frames" },
131 	{ ENETC_PM_RFRM(0),	"MAC rx valid frames" },
132 	{ ENETC_PM_RFCS(0),	"MAC rx fcs errors" },
133 	{ ENETC_PM_RVLAN(0),	"MAC rx VLAN frames" },
134 	{ ENETC_PM_RERR(0),	"MAC rx frame errors" },
135 	{ ENETC_PM_RUCA(0),	"MAC rx unicast frames" },
136 	{ ENETC_PM_RMCA(0),	"MAC rx multicast frames" },
137 	{ ENETC_PM_RBCA(0),	"MAC rx broadcast frames" },
138 	{ ENETC_PM_RDRP(0),	"MAC rx dropped packets" },
139 	{ ENETC_PM_RPKT(0),	"MAC rx packets" },
140 	{ ENETC_PM_RUND(0),	"MAC rx undersized packets" },
141 	{ ENETC_PM_R64(0),	"MAC rx 64 byte packets" },
142 	{ ENETC_PM_R127(0),	"MAC rx 65-127 byte packets" },
143 	{ ENETC_PM_R255(0),	"MAC rx 128-255 byte packets" },
144 	{ ENETC_PM_R511(0),	"MAC rx 256-511 byte packets" },
145 	{ ENETC_PM_R1023(0),	"MAC rx 512-1023 byte packets" },
146 	{ ENETC_PM_R1522(0),	"MAC rx 1024-1522 byte packets" },
147 	{ ENETC_PM_R1523X(0),	"MAC rx 1523 to max-octet packets" },
148 	{ ENETC_PM_ROVR(0),	"MAC rx oversized packets" },
149 	{ ENETC_PM_RJBR(0),	"MAC rx jabber packets" },
150 	{ ENETC_PM_RFRG(0),	"MAC rx fragment packets" },
151 	{ ENETC_PM_RCNP(0),	"MAC rx control packets" },
152 	{ ENETC_PM_RDRNTP(0),	"MAC rx fifo drop" },
153 	{ ENETC_PM_TEOCT(0),	"MAC tx ethernet octets" },
154 	{ ENETC_PM_TOCT(0),	"MAC tx octets" },
155 	{ ENETC_PM_TCRSE(0),	"MAC tx carrier sense errors" },
156 	{ ENETC_PM_TXPF(0),	"MAC tx valid pause frames" },
157 	{ ENETC_PM_TFRM(0),	"MAC tx frames" },
158 	{ ENETC_PM_TFCS(0),	"MAC tx fcs errors" },
159 	{ ENETC_PM_TVLAN(0),	"MAC tx VLAN frames" },
160 	{ ENETC_PM_TERR(0),	"MAC tx frame errors" },
161 	{ ENETC_PM_TUCA(0),	"MAC tx unicast frames" },
162 	{ ENETC_PM_TMCA(0),	"MAC tx multicast frames" },
163 	{ ENETC_PM_TBCA(0),	"MAC tx broadcast frames" },
164 	{ ENETC_PM_TPKT(0),	"MAC tx packets" },
165 	{ ENETC_PM_TUND(0),	"MAC tx undersized packets" },
166 	{ ENETC_PM_T64(0),	"MAC tx 64 byte packets" },
167 	{ ENETC_PM_T127(0),	"MAC tx 65-127 byte packets" },
168 	{ ENETC_PM_T255(0),	"MAC tx 128-255 byte packets" },
169 	{ ENETC_PM_T511(0),	"MAC tx 256-511 byte packets" },
170 	{ ENETC_PM_T1023(0),	"MAC tx 512-1023 byte packets" },
171 	{ ENETC_PM_T1522(0),	"MAC tx 1024-1522 byte packets" },
172 	{ ENETC_PM_T1523X(0),	"MAC tx 1523 to max-octet packets" },
173 	{ ENETC_PM_TCNP(0),	"MAC tx control packets" },
174 	{ ENETC_PM_TDFR(0),	"MAC tx deferred packets" },
175 	{ ENETC_PM_TMCOL(0),	"MAC tx multiple collisions" },
176 	{ ENETC_PM_TSCOL(0),	"MAC tx single collisions" },
177 	{ ENETC_PM_TLCOL(0),	"MAC tx late collisions" },
178 	{ ENETC_PM_TECOL(0),	"MAC tx excessive collisions" },
179 	{ ENETC_UFDMF,		"SI MAC nomatch u-cast discards" },
180 	{ ENETC_MFDMF,		"SI MAC nomatch m-cast discards" },
181 	{ ENETC_PBFDSIR,	"SI MAC nomatch b-cast discards" },
182 	{ ENETC_PUFDVFR,	"SI VLAN nomatch u-cast discards" },
183 	{ ENETC_PMFDVFR,	"SI VLAN nomatch m-cast discards" },
184 	{ ENETC_PBFDVFR,	"SI VLAN nomatch b-cast discards" },
185 	{ ENETC_PFDMSAPR,	"SI pruning discarded frames" },
186 	{ ENETC_PICDR(0),	"ICM DR0 discarded frames" },
187 	{ ENETC_PICDR(1),	"ICM DR1 discarded frames" },
188 	{ ENETC_PICDR(2),	"ICM DR2 discarded frames" },
189 	{ ENETC_PICDR(3),	"ICM DR3 discarded frames" },
190 };
191 
192 static const char rx_ring_stats[][ETH_GSTRING_LEN] = {
193 	"Rx ring %2d frames",
194 	"Rx ring %2d alloc errors",
195 	"Rx ring %2d XDP drops",
196 	"Rx ring %2d recycles",
197 	"Rx ring %2d recycle failures",
198 	"Rx ring %2d redirects",
199 	"Rx ring %2d redirect failures",
200 	"Rx ring %2d redirect S/G",
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 		data[o++] = priv->rx_ring[i]->stats.xdp_redirect_sg;
295 	}
296 
297 	if (!enetc_si_is_pf(priv->si))
298 		return;
299 
300 	for (i = 0; i < ARRAY_SIZE(enetc_port_counters); i++)
301 		data[o++] = enetc_port_rd(hw, enetc_port_counters[i].reg);
302 }
303 
304 static void enetc_get_pause_stats(struct net_device *ndev,
305 				  struct ethtool_pause_stats *pause_stats)
306 {
307 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
308 	struct enetc_hw *hw = &priv->si->hw;
309 
310 	pause_stats->tx_pause_frames = enetc_port_rd(hw, ENETC_PM_TXPF(0));
311 	pause_stats->rx_pause_frames = enetc_port_rd(hw, ENETC_PM_RXPF(0));
312 }
313 
314 static void enetc_mac_stats(struct enetc_hw *hw, int mac,
315 			    struct ethtool_eth_mac_stats *s)
316 {
317 	s->FramesTransmittedOK = enetc_port_rd(hw, ENETC_PM_TFRM(mac));
318 	s->SingleCollisionFrames = enetc_port_rd(hw, ENETC_PM_TSCOL(mac));
319 	s->MultipleCollisionFrames = enetc_port_rd(hw, ENETC_PM_TMCOL(mac));
320 	s->FramesReceivedOK = enetc_port_rd(hw, ENETC_PM_RFRM(mac));
321 	s->FrameCheckSequenceErrors = enetc_port_rd(hw, ENETC_PM_RFCS(mac));
322 	s->AlignmentErrors = enetc_port_rd(hw, ENETC_PM_RALN(mac));
323 	s->OctetsTransmittedOK = enetc_port_rd(hw, ENETC_PM_TEOCT(mac));
324 	s->FramesWithDeferredXmissions = enetc_port_rd(hw, ENETC_PM_TDFR(mac));
325 	s->LateCollisions = enetc_port_rd(hw, ENETC_PM_TLCOL(mac));
326 	s->FramesAbortedDueToXSColls = enetc_port_rd(hw, ENETC_PM_TECOL(mac));
327 	s->FramesLostDueToIntMACXmitError = enetc_port_rd(hw, ENETC_PM_TERR(mac));
328 	s->CarrierSenseErrors = enetc_port_rd(hw, ENETC_PM_TCRSE(mac));
329 	s->OctetsReceivedOK = enetc_port_rd(hw, ENETC_PM_REOCT(mac));
330 	s->FramesLostDueToIntMACRcvError = enetc_port_rd(hw, ENETC_PM_RDRNTP(mac));
331 	s->MulticastFramesXmittedOK = enetc_port_rd(hw, ENETC_PM_TMCA(mac));
332 	s->BroadcastFramesXmittedOK = enetc_port_rd(hw, ENETC_PM_TBCA(mac));
333 	s->MulticastFramesReceivedOK = enetc_port_rd(hw, ENETC_PM_RMCA(mac));
334 	s->BroadcastFramesReceivedOK = enetc_port_rd(hw, ENETC_PM_RBCA(mac));
335 }
336 
337 static void enetc_ctrl_stats(struct enetc_hw *hw, int mac,
338 			     struct ethtool_eth_ctrl_stats *s)
339 {
340 	s->MACControlFramesTransmitted = enetc_port_rd(hw, ENETC_PM_TCNP(mac));
341 	s->MACControlFramesReceived = enetc_port_rd(hw, ENETC_PM_RCNP(mac));
342 }
343 
344 static const struct ethtool_rmon_hist_range enetc_rmon_ranges[] = {
345 	{   64,   64 },
346 	{   65,  127 },
347 	{  128,  255 },
348 	{  256,  511 },
349 	{  512, 1023 },
350 	{ 1024, 1522 },
351 	{ 1523, ENETC_MAC_MAXFRM_SIZE },
352 	{},
353 };
354 
355 static void enetc_rmon_stats(struct enetc_hw *hw, int mac,
356 			     struct ethtool_rmon_stats *s,
357 			     const struct ethtool_rmon_hist_range **ranges)
358 {
359 	s->undersize_pkts = enetc_port_rd(hw, ENETC_PM_RUND(mac));
360 	s->oversize_pkts = enetc_port_rd(hw, ENETC_PM_ROVR(mac));
361 	s->fragments = enetc_port_rd(hw, ENETC_PM_RFRG(mac));
362 	s->jabbers = enetc_port_rd(hw, ENETC_PM_RJBR(mac));
363 
364 	s->hist[0] = enetc_port_rd(hw, ENETC_PM_R64(mac));
365 	s->hist[1] = enetc_port_rd(hw, ENETC_PM_R127(mac));
366 	s->hist[2] = enetc_port_rd(hw, ENETC_PM_R255(mac));
367 	s->hist[3] = enetc_port_rd(hw, ENETC_PM_R511(mac));
368 	s->hist[4] = enetc_port_rd(hw, ENETC_PM_R1023(mac));
369 	s->hist[5] = enetc_port_rd(hw, ENETC_PM_R1522(mac));
370 	s->hist[6] = enetc_port_rd(hw, ENETC_PM_R1523X(mac));
371 
372 	s->hist_tx[0] = enetc_port_rd(hw, ENETC_PM_T64(mac));
373 	s->hist_tx[1] = enetc_port_rd(hw, ENETC_PM_T127(mac));
374 	s->hist_tx[2] = enetc_port_rd(hw, ENETC_PM_T255(mac));
375 	s->hist_tx[3] = enetc_port_rd(hw, ENETC_PM_T511(mac));
376 	s->hist_tx[4] = enetc_port_rd(hw, ENETC_PM_T1023(mac));
377 	s->hist_tx[5] = enetc_port_rd(hw, ENETC_PM_T1522(mac));
378 	s->hist_tx[6] = enetc_port_rd(hw, ENETC_PM_T1523X(mac));
379 
380 	*ranges = enetc_rmon_ranges;
381 }
382 
383 static void enetc_get_eth_mac_stats(struct net_device *ndev,
384 				    struct ethtool_eth_mac_stats *mac_stats)
385 {
386 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
387 	struct enetc_hw *hw = &priv->si->hw;
388 
389 	enetc_mac_stats(hw, 0, mac_stats);
390 }
391 
392 static void enetc_get_eth_ctrl_stats(struct net_device *ndev,
393 				     struct ethtool_eth_ctrl_stats *ctrl_stats)
394 {
395 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
396 	struct enetc_hw *hw = &priv->si->hw;
397 
398 	enetc_ctrl_stats(hw, 0, ctrl_stats);
399 }
400 
401 static void enetc_get_rmon_stats(struct net_device *ndev,
402 				 struct ethtool_rmon_stats *rmon_stats,
403 				 const struct ethtool_rmon_hist_range **ranges)
404 {
405 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
406 	struct enetc_hw *hw = &priv->si->hw;
407 
408 	enetc_rmon_stats(hw, 0, rmon_stats, ranges);
409 }
410 
411 #define ENETC_RSSHASH_L3 (RXH_L2DA | RXH_VLAN | RXH_L3_PROTO | RXH_IP_SRC | \
412 			  RXH_IP_DST)
413 #define ENETC_RSSHASH_L4 (ENETC_RSSHASH_L3 | RXH_L4_B_0_1 | RXH_L4_B_2_3)
414 static int enetc_get_rsshash(struct ethtool_rxnfc *rxnfc)
415 {
416 	static const u32 rsshash[] = {
417 			[TCP_V4_FLOW]    = ENETC_RSSHASH_L4,
418 			[UDP_V4_FLOW]    = ENETC_RSSHASH_L4,
419 			[SCTP_V4_FLOW]   = ENETC_RSSHASH_L4,
420 			[AH_ESP_V4_FLOW] = ENETC_RSSHASH_L3,
421 			[IPV4_FLOW]      = ENETC_RSSHASH_L3,
422 			[TCP_V6_FLOW]    = ENETC_RSSHASH_L4,
423 			[UDP_V6_FLOW]    = ENETC_RSSHASH_L4,
424 			[SCTP_V6_FLOW]   = ENETC_RSSHASH_L4,
425 			[AH_ESP_V6_FLOW] = ENETC_RSSHASH_L3,
426 			[IPV6_FLOW]      = ENETC_RSSHASH_L3,
427 			[ETHER_FLOW]     = 0,
428 	};
429 
430 	if (rxnfc->flow_type >= ARRAY_SIZE(rsshash))
431 		return -EINVAL;
432 
433 	rxnfc->data = rsshash[rxnfc->flow_type];
434 
435 	return 0;
436 }
437 
438 /* current HW spec does byte reversal on everything including MAC addresses */
439 static void ether_addr_copy_swap(u8 *dst, const u8 *src)
440 {
441 	int i;
442 
443 	for (i = 0; i < ETH_ALEN; i++)
444 		dst[i] = src[ETH_ALEN - i - 1];
445 }
446 
447 static int enetc_set_cls_entry(struct enetc_si *si,
448 			       struct ethtool_rx_flow_spec *fs, bool en)
449 {
450 	struct ethtool_tcpip4_spec *l4ip4_h, *l4ip4_m;
451 	struct ethtool_usrip4_spec *l3ip4_h, *l3ip4_m;
452 	struct ethhdr *eth_h, *eth_m;
453 	struct enetc_cmd_rfse rfse = { {0} };
454 
455 	if (!en)
456 		goto done;
457 
458 	switch (fs->flow_type & 0xff) {
459 	case TCP_V4_FLOW:
460 		l4ip4_h = &fs->h_u.tcp_ip4_spec;
461 		l4ip4_m = &fs->m_u.tcp_ip4_spec;
462 		goto l4ip4;
463 	case UDP_V4_FLOW:
464 		l4ip4_h = &fs->h_u.udp_ip4_spec;
465 		l4ip4_m = &fs->m_u.udp_ip4_spec;
466 		goto l4ip4;
467 	case SCTP_V4_FLOW:
468 		l4ip4_h = &fs->h_u.sctp_ip4_spec;
469 		l4ip4_m = &fs->m_u.sctp_ip4_spec;
470 l4ip4:
471 		rfse.sip_h[0] = l4ip4_h->ip4src;
472 		rfse.sip_m[0] = l4ip4_m->ip4src;
473 		rfse.dip_h[0] = l4ip4_h->ip4dst;
474 		rfse.dip_m[0] = l4ip4_m->ip4dst;
475 		rfse.sport_h = ntohs(l4ip4_h->psrc);
476 		rfse.sport_m = ntohs(l4ip4_m->psrc);
477 		rfse.dport_h = ntohs(l4ip4_h->pdst);
478 		rfse.dport_m = ntohs(l4ip4_m->pdst);
479 		if (l4ip4_m->tos)
480 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
481 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
482 		rfse.ethtype_m = 0xffff;
483 		break;
484 	case IP_USER_FLOW:
485 		l3ip4_h = &fs->h_u.usr_ip4_spec;
486 		l3ip4_m = &fs->m_u.usr_ip4_spec;
487 
488 		rfse.sip_h[0] = l3ip4_h->ip4src;
489 		rfse.sip_m[0] = l3ip4_m->ip4src;
490 		rfse.dip_h[0] = l3ip4_h->ip4dst;
491 		rfse.dip_m[0] = l3ip4_m->ip4dst;
492 		if (l3ip4_m->tos)
493 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
494 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
495 		rfse.ethtype_m = 0xffff;
496 		break;
497 	case ETHER_FLOW:
498 		eth_h = &fs->h_u.ether_spec;
499 		eth_m = &fs->m_u.ether_spec;
500 
501 		ether_addr_copy_swap(rfse.smac_h, eth_h->h_source);
502 		ether_addr_copy_swap(rfse.smac_m, eth_m->h_source);
503 		ether_addr_copy_swap(rfse.dmac_h, eth_h->h_dest);
504 		ether_addr_copy_swap(rfse.dmac_m, eth_m->h_dest);
505 		rfse.ethtype_h = ntohs(eth_h->h_proto);
506 		rfse.ethtype_m = ntohs(eth_m->h_proto);
507 		break;
508 	default:
509 		return -EOPNOTSUPP;
510 	}
511 
512 	rfse.mode |= ENETC_RFSE_EN;
513 	if (fs->ring_cookie != RX_CLS_FLOW_DISC) {
514 		rfse.mode |= ENETC_RFSE_MODE_BD;
515 		rfse.result = fs->ring_cookie;
516 	}
517 done:
518 	return enetc_set_fs_entry(si, &rfse, fs->location);
519 }
520 
521 static int enetc_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc,
522 			   u32 *rule_locs)
523 {
524 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
525 	int i, j;
526 
527 	switch (rxnfc->cmd) {
528 	case ETHTOOL_GRXRINGS:
529 		rxnfc->data = priv->num_rx_rings;
530 		break;
531 	case ETHTOOL_GRXFH:
532 		/* get RSS hash config */
533 		return enetc_get_rsshash(rxnfc);
534 	case ETHTOOL_GRXCLSRLCNT:
535 		/* total number of entries */
536 		rxnfc->data = priv->si->num_fs_entries;
537 		/* number of entries in use */
538 		rxnfc->rule_cnt = 0;
539 		for (i = 0; i < priv->si->num_fs_entries; i++)
540 			if (priv->cls_rules[i].used)
541 				rxnfc->rule_cnt++;
542 		break;
543 	case ETHTOOL_GRXCLSRULE:
544 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
545 			return -EINVAL;
546 
547 		/* get entry x */
548 		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
549 		break;
550 	case ETHTOOL_GRXCLSRLALL:
551 		/* total number of entries */
552 		rxnfc->data = priv->si->num_fs_entries;
553 		/* array of indexes of used entries */
554 		j = 0;
555 		for (i = 0; i < priv->si->num_fs_entries; i++) {
556 			if (!priv->cls_rules[i].used)
557 				continue;
558 			if (j == rxnfc->rule_cnt)
559 				return -EMSGSIZE;
560 			rule_locs[j++] = i;
561 		}
562 		/* number of entries in use */
563 		rxnfc->rule_cnt = j;
564 		break;
565 	default:
566 		return -EOPNOTSUPP;
567 	}
568 
569 	return 0;
570 }
571 
572 static int enetc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc)
573 {
574 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
575 	int err;
576 
577 	switch (rxnfc->cmd) {
578 	case ETHTOOL_SRXCLSRLINS:
579 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
580 			return -EINVAL;
581 
582 		if (rxnfc->fs.ring_cookie >= priv->num_rx_rings &&
583 		    rxnfc->fs.ring_cookie != RX_CLS_FLOW_DISC)
584 			return -EINVAL;
585 
586 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, true);
587 		if (err)
588 			return err;
589 		priv->cls_rules[rxnfc->fs.location].fs = rxnfc->fs;
590 		priv->cls_rules[rxnfc->fs.location].used = 1;
591 		break;
592 	case ETHTOOL_SRXCLSRLDEL:
593 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
594 			return -EINVAL;
595 
596 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, false);
597 		if (err)
598 			return err;
599 		priv->cls_rules[rxnfc->fs.location].used = 0;
600 		break;
601 	default:
602 		return -EOPNOTSUPP;
603 	}
604 
605 	return 0;
606 }
607 
608 static u32 enetc_get_rxfh_key_size(struct net_device *ndev)
609 {
610 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
611 
612 	/* return the size of the RX flow hash key.  PF only */
613 	return (priv->si->hw.port) ? ENETC_RSSHASH_KEY_SIZE : 0;
614 }
615 
616 static u32 enetc_get_rxfh_indir_size(struct net_device *ndev)
617 {
618 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
619 
620 	/* return the size of the RX flow hash indirection table */
621 	return priv->si->num_rss;
622 }
623 
624 static int enetc_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key,
625 			  u8 *hfunc)
626 {
627 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
628 	struct enetc_hw *hw = &priv->si->hw;
629 	int err = 0, i;
630 
631 	/* return hash function */
632 	if (hfunc)
633 		*hfunc = ETH_RSS_HASH_TOP;
634 
635 	/* return hash key */
636 	if (key && hw->port)
637 		for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
638 			((u32 *)key)[i] = enetc_port_rd(hw, ENETC_PRSSK(i));
639 
640 	/* return RSS table */
641 	if (indir)
642 		err = enetc_get_rss_table(priv->si, indir, priv->si->num_rss);
643 
644 	return err;
645 }
646 
647 void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes)
648 {
649 	int i;
650 
651 	for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
652 		enetc_port_wr(hw, ENETC_PRSSK(i), ((u32 *)bytes)[i]);
653 }
654 
655 static int enetc_set_rxfh(struct net_device *ndev, const u32 *indir,
656 			  const u8 *key, const u8 hfunc)
657 {
658 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
659 	struct enetc_hw *hw = &priv->si->hw;
660 	int err = 0;
661 
662 	/* set hash key, if PF */
663 	if (key && hw->port)
664 		enetc_set_rss_key(hw, key);
665 
666 	/* set RSS table */
667 	if (indir)
668 		err = enetc_set_rss_table(priv->si, indir, priv->si->num_rss);
669 
670 	return err;
671 }
672 
673 static void enetc_get_ringparam(struct net_device *ndev,
674 				struct ethtool_ringparam *ring,
675 				struct kernel_ethtool_ringparam *kernel_ring,
676 				struct netlink_ext_ack *extack)
677 {
678 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
679 
680 	ring->rx_pending = priv->rx_bd_count;
681 	ring->tx_pending = priv->tx_bd_count;
682 
683 	/* do some h/w sanity checks for BDR length */
684 	if (netif_running(ndev)) {
685 		struct enetc_hw *hw = &priv->si->hw;
686 		u32 val = enetc_rxbdr_rd(hw, 0, ENETC_RBLENR);
687 
688 		if (val != priv->rx_bd_count)
689 			netif_err(priv, hw, ndev, "RxBDR[RBLENR] = %d!\n", val);
690 
691 		val = enetc_txbdr_rd(hw, 0, ENETC_TBLENR);
692 
693 		if (val != priv->tx_bd_count)
694 			netif_err(priv, hw, ndev, "TxBDR[TBLENR] = %d!\n", val);
695 	}
696 }
697 
698 static int enetc_get_coalesce(struct net_device *ndev,
699 			      struct ethtool_coalesce *ic,
700 			      struct kernel_ethtool_coalesce *kernel_coal,
701 			      struct netlink_ext_ack *extack)
702 {
703 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
704 	struct enetc_int_vector *v = priv->int_vector[0];
705 
706 	ic->tx_coalesce_usecs = enetc_cycles_to_usecs(priv->tx_ictt);
707 	ic->rx_coalesce_usecs = enetc_cycles_to_usecs(v->rx_ictt);
708 
709 	ic->tx_max_coalesced_frames = ENETC_TXIC_PKTTHR;
710 	ic->rx_max_coalesced_frames = ENETC_RXIC_PKTTHR;
711 
712 	ic->use_adaptive_rx_coalesce = priv->ic_mode & ENETC_IC_RX_ADAPTIVE;
713 
714 	return 0;
715 }
716 
717 static int enetc_set_coalesce(struct net_device *ndev,
718 			      struct ethtool_coalesce *ic,
719 			      struct kernel_ethtool_coalesce *kernel_coal,
720 			      struct netlink_ext_ack *extack)
721 {
722 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
723 	u32 rx_ictt, tx_ictt;
724 	int i, ic_mode;
725 	bool changed;
726 
727 	tx_ictt = enetc_usecs_to_cycles(ic->tx_coalesce_usecs);
728 	rx_ictt = enetc_usecs_to_cycles(ic->rx_coalesce_usecs);
729 
730 	if (ic->rx_max_coalesced_frames != ENETC_RXIC_PKTTHR)
731 		return -EOPNOTSUPP;
732 
733 	if (ic->tx_max_coalesced_frames != ENETC_TXIC_PKTTHR)
734 		return -EOPNOTSUPP;
735 
736 	ic_mode = ENETC_IC_NONE;
737 	if (ic->use_adaptive_rx_coalesce) {
738 		ic_mode |= ENETC_IC_RX_ADAPTIVE;
739 		rx_ictt = 0x1;
740 	} else {
741 		ic_mode |= rx_ictt ? ENETC_IC_RX_MANUAL : 0;
742 	}
743 
744 	ic_mode |= tx_ictt ? ENETC_IC_TX_MANUAL : 0;
745 
746 	/* commit the settings */
747 	changed = (ic_mode != priv->ic_mode) || (priv->tx_ictt != tx_ictt);
748 
749 	priv->ic_mode = ic_mode;
750 	priv->tx_ictt = tx_ictt;
751 
752 	for (i = 0; i < priv->bdr_int_num; i++) {
753 		struct enetc_int_vector *v = priv->int_vector[i];
754 
755 		v->rx_ictt = rx_ictt;
756 		v->rx_dim_en = !!(ic_mode & ENETC_IC_RX_ADAPTIVE);
757 	}
758 
759 	if (netif_running(ndev) && changed) {
760 		/* reconfigure the operation mode of h/w interrupts,
761 		 * traffic needs to be paused in the process
762 		 */
763 		enetc_stop(ndev);
764 		enetc_start(ndev);
765 	}
766 
767 	return 0;
768 }
769 
770 static int enetc_get_ts_info(struct net_device *ndev,
771 			     struct ethtool_ts_info *info)
772 {
773 	int *phc_idx;
774 
775 	phc_idx = symbol_get(enetc_phc_index);
776 	if (phc_idx) {
777 		info->phc_index = *phc_idx;
778 		symbol_put(enetc_phc_index);
779 	} else {
780 		info->phc_index = -1;
781 	}
782 
783 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
784 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
785 				SOF_TIMESTAMPING_RX_HARDWARE |
786 				SOF_TIMESTAMPING_RAW_HARDWARE |
787 				SOF_TIMESTAMPING_TX_SOFTWARE |
788 				SOF_TIMESTAMPING_RX_SOFTWARE |
789 				SOF_TIMESTAMPING_SOFTWARE;
790 
791 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
792 			 (1 << HWTSTAMP_TX_ON) |
793 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
794 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
795 			   (1 << HWTSTAMP_FILTER_ALL);
796 #else
797 	info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
798 				SOF_TIMESTAMPING_TX_SOFTWARE |
799 				SOF_TIMESTAMPING_SOFTWARE;
800 #endif
801 	return 0;
802 }
803 
804 static void enetc_get_wol(struct net_device *dev,
805 			  struct ethtool_wolinfo *wol)
806 {
807 	wol->supported = 0;
808 	wol->wolopts = 0;
809 
810 	if (dev->phydev)
811 		phy_ethtool_get_wol(dev->phydev, wol);
812 }
813 
814 static int enetc_set_wol(struct net_device *dev,
815 			 struct ethtool_wolinfo *wol)
816 {
817 	int ret;
818 
819 	if (!dev->phydev)
820 		return -EOPNOTSUPP;
821 
822 	ret = phy_ethtool_set_wol(dev->phydev, wol);
823 	if (!ret)
824 		device_set_wakeup_enable(&dev->dev, wol->wolopts);
825 
826 	return ret;
827 }
828 
829 static void enetc_get_pauseparam(struct net_device *dev,
830 				 struct ethtool_pauseparam *pause)
831 {
832 	struct enetc_ndev_priv *priv = netdev_priv(dev);
833 
834 	phylink_ethtool_get_pauseparam(priv->phylink, pause);
835 }
836 
837 static int enetc_set_pauseparam(struct net_device *dev,
838 				struct ethtool_pauseparam *pause)
839 {
840 	struct enetc_ndev_priv *priv = netdev_priv(dev);
841 
842 	return phylink_ethtool_set_pauseparam(priv->phylink, pause);
843 }
844 
845 static int enetc_get_link_ksettings(struct net_device *dev,
846 				    struct ethtool_link_ksettings *cmd)
847 {
848 	struct enetc_ndev_priv *priv = netdev_priv(dev);
849 
850 	if (!priv->phylink)
851 		return -EOPNOTSUPP;
852 
853 	return phylink_ethtool_ksettings_get(priv->phylink, cmd);
854 }
855 
856 static int enetc_set_link_ksettings(struct net_device *dev,
857 				    const struct ethtool_link_ksettings *cmd)
858 {
859 	struct enetc_ndev_priv *priv = netdev_priv(dev);
860 
861 	if (!priv->phylink)
862 		return -EOPNOTSUPP;
863 
864 	return phylink_ethtool_ksettings_set(priv->phylink, cmd);
865 }
866 
867 static const struct ethtool_ops enetc_pf_ethtool_ops = {
868 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
869 				     ETHTOOL_COALESCE_MAX_FRAMES |
870 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
871 	.get_regs_len = enetc_get_reglen,
872 	.get_regs = enetc_get_regs,
873 	.get_sset_count = enetc_get_sset_count,
874 	.get_strings = enetc_get_strings,
875 	.get_ethtool_stats = enetc_get_ethtool_stats,
876 	.get_pause_stats = enetc_get_pause_stats,
877 	.get_rmon_stats = enetc_get_rmon_stats,
878 	.get_eth_ctrl_stats = enetc_get_eth_ctrl_stats,
879 	.get_eth_mac_stats = enetc_get_eth_mac_stats,
880 	.get_rxnfc = enetc_get_rxnfc,
881 	.set_rxnfc = enetc_set_rxnfc,
882 	.get_rxfh_key_size = enetc_get_rxfh_key_size,
883 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
884 	.get_rxfh = enetc_get_rxfh,
885 	.set_rxfh = enetc_set_rxfh,
886 	.get_ringparam = enetc_get_ringparam,
887 	.get_coalesce = enetc_get_coalesce,
888 	.set_coalesce = enetc_set_coalesce,
889 	.get_link_ksettings = enetc_get_link_ksettings,
890 	.set_link_ksettings = enetc_set_link_ksettings,
891 	.get_link = ethtool_op_get_link,
892 	.get_ts_info = enetc_get_ts_info,
893 	.get_wol = enetc_get_wol,
894 	.set_wol = enetc_set_wol,
895 	.get_pauseparam = enetc_get_pauseparam,
896 	.set_pauseparam = enetc_set_pauseparam,
897 };
898 
899 static const struct ethtool_ops enetc_vf_ethtool_ops = {
900 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
901 				     ETHTOOL_COALESCE_MAX_FRAMES |
902 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
903 	.get_regs_len = enetc_get_reglen,
904 	.get_regs = enetc_get_regs,
905 	.get_sset_count = enetc_get_sset_count,
906 	.get_strings = enetc_get_strings,
907 	.get_ethtool_stats = enetc_get_ethtool_stats,
908 	.get_rxnfc = enetc_get_rxnfc,
909 	.set_rxnfc = enetc_set_rxnfc,
910 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
911 	.get_rxfh = enetc_get_rxfh,
912 	.set_rxfh = enetc_set_rxfh,
913 	.get_ringparam = enetc_get_ringparam,
914 	.get_coalesce = enetc_get_coalesce,
915 	.set_coalesce = enetc_set_coalesce,
916 	.get_link = ethtool_op_get_link,
917 	.get_ts_info = enetc_get_ts_info,
918 };
919 
920 void enetc_set_ethtool_ops(struct net_device *ndev)
921 {
922 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
923 
924 	if (enetc_si_is_pf(priv->si))
925 		ndev->ethtool_ops = &enetc_pf_ethtool_ops;
926 	else
927 		ndev->ethtool_ops = &enetc_vf_ethtool_ops;
928 }
929