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 };
201 
202 static const char tx_ring_stats[][ETH_GSTRING_LEN] = {
203 	"Tx ring %2d frames",
204 	"Tx ring %2d XDP frames",
205 	"Tx ring %2d XDP drops",
206 	"Tx window drop %2d frames",
207 };
208 
209 static int enetc_get_sset_count(struct net_device *ndev, int sset)
210 {
211 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
212 	int len;
213 
214 	if (sset != ETH_SS_STATS)
215 		return -EOPNOTSUPP;
216 
217 	len = ARRAY_SIZE(enetc_si_counters) +
218 	      ARRAY_SIZE(tx_ring_stats) * priv->num_tx_rings +
219 	      ARRAY_SIZE(rx_ring_stats) * priv->num_rx_rings;
220 
221 	if (!enetc_si_is_pf(priv->si))
222 		return len;
223 
224 	len += ARRAY_SIZE(enetc_port_counters);
225 
226 	return len;
227 }
228 
229 static void enetc_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
230 {
231 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
232 	u8 *p = data;
233 	int i, j;
234 
235 	switch (stringset) {
236 	case ETH_SS_STATS:
237 		for (i = 0; i < ARRAY_SIZE(enetc_si_counters); i++) {
238 			strscpy(p, enetc_si_counters[i].name, ETH_GSTRING_LEN);
239 			p += ETH_GSTRING_LEN;
240 		}
241 		for (i = 0; i < priv->num_tx_rings; i++) {
242 			for (j = 0; j < ARRAY_SIZE(tx_ring_stats); j++) {
243 				snprintf(p, ETH_GSTRING_LEN, tx_ring_stats[j],
244 					 i);
245 				p += ETH_GSTRING_LEN;
246 			}
247 		}
248 		for (i = 0; i < priv->num_rx_rings; i++) {
249 			for (j = 0; j < ARRAY_SIZE(rx_ring_stats); j++) {
250 				snprintf(p, ETH_GSTRING_LEN, rx_ring_stats[j],
251 					 i);
252 				p += ETH_GSTRING_LEN;
253 			}
254 		}
255 
256 		if (!enetc_si_is_pf(priv->si))
257 			break;
258 
259 		for (i = 0; i < ARRAY_SIZE(enetc_port_counters); i++) {
260 			strscpy(p, enetc_port_counters[i].name,
261 				ETH_GSTRING_LEN);
262 			p += ETH_GSTRING_LEN;
263 		}
264 		break;
265 	}
266 }
267 
268 static void enetc_get_ethtool_stats(struct net_device *ndev,
269 				    struct ethtool_stats *stats, u64 *data)
270 {
271 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
272 	struct enetc_hw *hw = &priv->si->hw;
273 	int i, o = 0;
274 
275 	for (i = 0; i < ARRAY_SIZE(enetc_si_counters); i++)
276 		data[o++] = enetc_rd64(hw, enetc_si_counters[i].reg);
277 
278 	for (i = 0; i < priv->num_tx_rings; i++) {
279 		data[o++] = priv->tx_ring[i]->stats.packets;
280 		data[o++] = priv->tx_ring[i]->stats.xdp_tx;
281 		data[o++] = priv->tx_ring[i]->stats.xdp_tx_drops;
282 		data[o++] = priv->tx_ring[i]->stats.win_drop;
283 	}
284 
285 	for (i = 0; i < priv->num_rx_rings; i++) {
286 		data[o++] = priv->rx_ring[i]->stats.packets;
287 		data[o++] = priv->rx_ring[i]->stats.rx_alloc_errs;
288 		data[o++] = priv->rx_ring[i]->stats.xdp_drops;
289 		data[o++] = priv->rx_ring[i]->stats.recycles;
290 		data[o++] = priv->rx_ring[i]->stats.recycle_failures;
291 		data[o++] = priv->rx_ring[i]->stats.xdp_redirect;
292 		data[o++] = priv->rx_ring[i]->stats.xdp_redirect_failures;
293 	}
294 
295 	if (!enetc_si_is_pf(priv->si))
296 		return;
297 
298 	for (i = 0; i < ARRAY_SIZE(enetc_port_counters); i++)
299 		data[o++] = enetc_port_rd(hw, enetc_port_counters[i].reg);
300 }
301 
302 static void enetc_get_pause_stats(struct net_device *ndev,
303 				  struct ethtool_pause_stats *pause_stats)
304 {
305 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
306 	struct enetc_hw *hw = &priv->si->hw;
307 
308 	pause_stats->tx_pause_frames = enetc_port_rd(hw, ENETC_PM_TXPF(0));
309 	pause_stats->rx_pause_frames = enetc_port_rd(hw, ENETC_PM_RXPF(0));
310 }
311 
312 static void enetc_mac_stats(struct enetc_hw *hw, int mac,
313 			    struct ethtool_eth_mac_stats *s)
314 {
315 	s->FramesTransmittedOK = enetc_port_rd(hw, ENETC_PM_TFRM(mac));
316 	s->SingleCollisionFrames = enetc_port_rd(hw, ENETC_PM_TSCOL(mac));
317 	s->MultipleCollisionFrames = enetc_port_rd(hw, ENETC_PM_TMCOL(mac));
318 	s->FramesReceivedOK = enetc_port_rd(hw, ENETC_PM_RFRM(mac));
319 	s->FrameCheckSequenceErrors = enetc_port_rd(hw, ENETC_PM_RFCS(mac));
320 	s->AlignmentErrors = enetc_port_rd(hw, ENETC_PM_RALN(mac));
321 	s->OctetsTransmittedOK = enetc_port_rd(hw, ENETC_PM_TEOCT(mac));
322 	s->FramesWithDeferredXmissions = enetc_port_rd(hw, ENETC_PM_TDFR(mac));
323 	s->LateCollisions = enetc_port_rd(hw, ENETC_PM_TLCOL(mac));
324 	s->FramesAbortedDueToXSColls = enetc_port_rd(hw, ENETC_PM_TECOL(mac));
325 	s->FramesLostDueToIntMACXmitError = enetc_port_rd(hw, ENETC_PM_TERR(mac));
326 	s->CarrierSenseErrors = enetc_port_rd(hw, ENETC_PM_TCRSE(mac));
327 	s->OctetsReceivedOK = enetc_port_rd(hw, ENETC_PM_REOCT(mac));
328 	s->FramesLostDueToIntMACRcvError = enetc_port_rd(hw, ENETC_PM_RDRNTP(mac));
329 	s->MulticastFramesXmittedOK = enetc_port_rd(hw, ENETC_PM_TMCA(mac));
330 	s->BroadcastFramesXmittedOK = enetc_port_rd(hw, ENETC_PM_TBCA(mac));
331 	s->MulticastFramesReceivedOK = enetc_port_rd(hw, ENETC_PM_RMCA(mac));
332 	s->BroadcastFramesReceivedOK = enetc_port_rd(hw, ENETC_PM_RBCA(mac));
333 }
334 
335 static void enetc_ctrl_stats(struct enetc_hw *hw, int mac,
336 			     struct ethtool_eth_ctrl_stats *s)
337 {
338 	s->MACControlFramesTransmitted = enetc_port_rd(hw, ENETC_PM_TCNP(mac));
339 	s->MACControlFramesReceived = enetc_port_rd(hw, ENETC_PM_RCNP(mac));
340 }
341 
342 static const struct ethtool_rmon_hist_range enetc_rmon_ranges[] = {
343 	{   64,   64 },
344 	{   65,  127 },
345 	{  128,  255 },
346 	{  256,  511 },
347 	{  512, 1023 },
348 	{ 1024, 1522 },
349 	{ 1523, ENETC_MAC_MAXFRM_SIZE },
350 	{},
351 };
352 
353 static void enetc_rmon_stats(struct enetc_hw *hw, int mac,
354 			     struct ethtool_rmon_stats *s,
355 			     const struct ethtool_rmon_hist_range **ranges)
356 {
357 	s->undersize_pkts = enetc_port_rd(hw, ENETC_PM_RUND(mac));
358 	s->oversize_pkts = enetc_port_rd(hw, ENETC_PM_ROVR(mac));
359 	s->fragments = enetc_port_rd(hw, ENETC_PM_RFRG(mac));
360 	s->jabbers = enetc_port_rd(hw, ENETC_PM_RJBR(mac));
361 
362 	s->hist[0] = enetc_port_rd(hw, ENETC_PM_R64(mac));
363 	s->hist[1] = enetc_port_rd(hw, ENETC_PM_R127(mac));
364 	s->hist[2] = enetc_port_rd(hw, ENETC_PM_R255(mac));
365 	s->hist[3] = enetc_port_rd(hw, ENETC_PM_R511(mac));
366 	s->hist[4] = enetc_port_rd(hw, ENETC_PM_R1023(mac));
367 	s->hist[5] = enetc_port_rd(hw, ENETC_PM_R1522(mac));
368 	s->hist[6] = enetc_port_rd(hw, ENETC_PM_R1523X(mac));
369 
370 	s->hist_tx[0] = enetc_port_rd(hw, ENETC_PM_T64(mac));
371 	s->hist_tx[1] = enetc_port_rd(hw, ENETC_PM_T127(mac));
372 	s->hist_tx[2] = enetc_port_rd(hw, ENETC_PM_T255(mac));
373 	s->hist_tx[3] = enetc_port_rd(hw, ENETC_PM_T511(mac));
374 	s->hist_tx[4] = enetc_port_rd(hw, ENETC_PM_T1023(mac));
375 	s->hist_tx[5] = enetc_port_rd(hw, ENETC_PM_T1522(mac));
376 	s->hist_tx[6] = enetc_port_rd(hw, ENETC_PM_T1523X(mac));
377 
378 	*ranges = enetc_rmon_ranges;
379 }
380 
381 static void enetc_get_eth_mac_stats(struct net_device *ndev,
382 				    struct ethtool_eth_mac_stats *mac_stats)
383 {
384 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
385 	struct enetc_hw *hw = &priv->si->hw;
386 
387 	enetc_mac_stats(hw, 0, mac_stats);
388 }
389 
390 static void enetc_get_eth_ctrl_stats(struct net_device *ndev,
391 				     struct ethtool_eth_ctrl_stats *ctrl_stats)
392 {
393 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
394 	struct enetc_hw *hw = &priv->si->hw;
395 
396 	enetc_ctrl_stats(hw, 0, ctrl_stats);
397 }
398 
399 static void enetc_get_rmon_stats(struct net_device *ndev,
400 				 struct ethtool_rmon_stats *rmon_stats,
401 				 const struct ethtool_rmon_hist_range **ranges)
402 {
403 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
404 	struct enetc_hw *hw = &priv->si->hw;
405 
406 	enetc_rmon_stats(hw, 0, rmon_stats, ranges);
407 }
408 
409 #define ENETC_RSSHASH_L3 (RXH_L2DA | RXH_VLAN | RXH_L3_PROTO | RXH_IP_SRC | \
410 			  RXH_IP_DST)
411 #define ENETC_RSSHASH_L4 (ENETC_RSSHASH_L3 | RXH_L4_B_0_1 | RXH_L4_B_2_3)
412 static int enetc_get_rsshash(struct ethtool_rxnfc *rxnfc)
413 {
414 	static const u32 rsshash[] = {
415 			[TCP_V4_FLOW]    = ENETC_RSSHASH_L4,
416 			[UDP_V4_FLOW]    = ENETC_RSSHASH_L4,
417 			[SCTP_V4_FLOW]   = ENETC_RSSHASH_L4,
418 			[AH_ESP_V4_FLOW] = ENETC_RSSHASH_L3,
419 			[IPV4_FLOW]      = ENETC_RSSHASH_L3,
420 			[TCP_V6_FLOW]    = ENETC_RSSHASH_L4,
421 			[UDP_V6_FLOW]    = ENETC_RSSHASH_L4,
422 			[SCTP_V6_FLOW]   = ENETC_RSSHASH_L4,
423 			[AH_ESP_V6_FLOW] = ENETC_RSSHASH_L3,
424 			[IPV6_FLOW]      = ENETC_RSSHASH_L3,
425 			[ETHER_FLOW]     = 0,
426 	};
427 
428 	if (rxnfc->flow_type >= ARRAY_SIZE(rsshash))
429 		return -EINVAL;
430 
431 	rxnfc->data = rsshash[rxnfc->flow_type];
432 
433 	return 0;
434 }
435 
436 /* current HW spec does byte reversal on everything including MAC addresses */
437 static void ether_addr_copy_swap(u8 *dst, const u8 *src)
438 {
439 	int i;
440 
441 	for (i = 0; i < ETH_ALEN; i++)
442 		dst[i] = src[ETH_ALEN - i - 1];
443 }
444 
445 static int enetc_set_cls_entry(struct enetc_si *si,
446 			       struct ethtool_rx_flow_spec *fs, bool en)
447 {
448 	struct ethtool_tcpip4_spec *l4ip4_h, *l4ip4_m;
449 	struct ethtool_usrip4_spec *l3ip4_h, *l3ip4_m;
450 	struct ethhdr *eth_h, *eth_m;
451 	struct enetc_cmd_rfse rfse = { {0} };
452 
453 	if (!en)
454 		goto done;
455 
456 	switch (fs->flow_type & 0xff) {
457 	case TCP_V4_FLOW:
458 		l4ip4_h = &fs->h_u.tcp_ip4_spec;
459 		l4ip4_m = &fs->m_u.tcp_ip4_spec;
460 		goto l4ip4;
461 	case UDP_V4_FLOW:
462 		l4ip4_h = &fs->h_u.udp_ip4_spec;
463 		l4ip4_m = &fs->m_u.udp_ip4_spec;
464 		goto l4ip4;
465 	case SCTP_V4_FLOW:
466 		l4ip4_h = &fs->h_u.sctp_ip4_spec;
467 		l4ip4_m = &fs->m_u.sctp_ip4_spec;
468 l4ip4:
469 		rfse.sip_h[0] = l4ip4_h->ip4src;
470 		rfse.sip_m[0] = l4ip4_m->ip4src;
471 		rfse.dip_h[0] = l4ip4_h->ip4dst;
472 		rfse.dip_m[0] = l4ip4_m->ip4dst;
473 		rfse.sport_h = ntohs(l4ip4_h->psrc);
474 		rfse.sport_m = ntohs(l4ip4_m->psrc);
475 		rfse.dport_h = ntohs(l4ip4_h->pdst);
476 		rfse.dport_m = ntohs(l4ip4_m->pdst);
477 		if (l4ip4_m->tos)
478 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
479 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
480 		rfse.ethtype_m = 0xffff;
481 		break;
482 	case IP_USER_FLOW:
483 		l3ip4_h = &fs->h_u.usr_ip4_spec;
484 		l3ip4_m = &fs->m_u.usr_ip4_spec;
485 
486 		rfse.sip_h[0] = l3ip4_h->ip4src;
487 		rfse.sip_m[0] = l3ip4_m->ip4src;
488 		rfse.dip_h[0] = l3ip4_h->ip4dst;
489 		rfse.dip_m[0] = l3ip4_m->ip4dst;
490 		if (l3ip4_m->tos)
491 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
492 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
493 		rfse.ethtype_m = 0xffff;
494 		break;
495 	case ETHER_FLOW:
496 		eth_h = &fs->h_u.ether_spec;
497 		eth_m = &fs->m_u.ether_spec;
498 
499 		ether_addr_copy_swap(rfse.smac_h, eth_h->h_source);
500 		ether_addr_copy_swap(rfse.smac_m, eth_m->h_source);
501 		ether_addr_copy_swap(rfse.dmac_h, eth_h->h_dest);
502 		ether_addr_copy_swap(rfse.dmac_m, eth_m->h_dest);
503 		rfse.ethtype_h = ntohs(eth_h->h_proto);
504 		rfse.ethtype_m = ntohs(eth_m->h_proto);
505 		break;
506 	default:
507 		return -EOPNOTSUPP;
508 	}
509 
510 	rfse.mode |= ENETC_RFSE_EN;
511 	if (fs->ring_cookie != RX_CLS_FLOW_DISC) {
512 		rfse.mode |= ENETC_RFSE_MODE_BD;
513 		rfse.result = fs->ring_cookie;
514 	}
515 done:
516 	return enetc_set_fs_entry(si, &rfse, fs->location);
517 }
518 
519 static int enetc_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc,
520 			   u32 *rule_locs)
521 {
522 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
523 	int i, j;
524 
525 	switch (rxnfc->cmd) {
526 	case ETHTOOL_GRXRINGS:
527 		rxnfc->data = priv->num_rx_rings;
528 		break;
529 	case ETHTOOL_GRXFH:
530 		/* get RSS hash config */
531 		return enetc_get_rsshash(rxnfc);
532 	case ETHTOOL_GRXCLSRLCNT:
533 		/* total number of entries */
534 		rxnfc->data = priv->si->num_fs_entries;
535 		/* number of entries in use */
536 		rxnfc->rule_cnt = 0;
537 		for (i = 0; i < priv->si->num_fs_entries; i++)
538 			if (priv->cls_rules[i].used)
539 				rxnfc->rule_cnt++;
540 		break;
541 	case ETHTOOL_GRXCLSRULE:
542 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
543 			return -EINVAL;
544 
545 		/* get entry x */
546 		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
547 		break;
548 	case ETHTOOL_GRXCLSRLALL:
549 		/* total number of entries */
550 		rxnfc->data = priv->si->num_fs_entries;
551 		/* array of indexes of used entries */
552 		j = 0;
553 		for (i = 0; i < priv->si->num_fs_entries; i++) {
554 			if (!priv->cls_rules[i].used)
555 				continue;
556 			if (j == rxnfc->rule_cnt)
557 				return -EMSGSIZE;
558 			rule_locs[j++] = i;
559 		}
560 		/* number of entries in use */
561 		rxnfc->rule_cnt = j;
562 		break;
563 	default:
564 		return -EOPNOTSUPP;
565 	}
566 
567 	return 0;
568 }
569 
570 static int enetc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc)
571 {
572 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
573 	int err;
574 
575 	switch (rxnfc->cmd) {
576 	case ETHTOOL_SRXCLSRLINS:
577 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
578 			return -EINVAL;
579 
580 		if (rxnfc->fs.ring_cookie >= priv->num_rx_rings &&
581 		    rxnfc->fs.ring_cookie != RX_CLS_FLOW_DISC)
582 			return -EINVAL;
583 
584 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, true);
585 		if (err)
586 			return err;
587 		priv->cls_rules[rxnfc->fs.location].fs = rxnfc->fs;
588 		priv->cls_rules[rxnfc->fs.location].used = 1;
589 		break;
590 	case ETHTOOL_SRXCLSRLDEL:
591 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
592 			return -EINVAL;
593 
594 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, false);
595 		if (err)
596 			return err;
597 		priv->cls_rules[rxnfc->fs.location].used = 0;
598 		break;
599 	default:
600 		return -EOPNOTSUPP;
601 	}
602 
603 	return 0;
604 }
605 
606 static u32 enetc_get_rxfh_key_size(struct net_device *ndev)
607 {
608 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
609 
610 	/* return the size of the RX flow hash key.  PF only */
611 	return (priv->si->hw.port) ? ENETC_RSSHASH_KEY_SIZE : 0;
612 }
613 
614 static u32 enetc_get_rxfh_indir_size(struct net_device *ndev)
615 {
616 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
617 
618 	/* return the size of the RX flow hash indirection table */
619 	return priv->si->num_rss;
620 }
621 
622 static int enetc_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key,
623 			  u8 *hfunc)
624 {
625 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
626 	struct enetc_hw *hw = &priv->si->hw;
627 	int err = 0, i;
628 
629 	/* return hash function */
630 	if (hfunc)
631 		*hfunc = ETH_RSS_HASH_TOP;
632 
633 	/* return hash key */
634 	if (key && hw->port)
635 		for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
636 			((u32 *)key)[i] = enetc_port_rd(hw, ENETC_PRSSK(i));
637 
638 	/* return RSS table */
639 	if (indir)
640 		err = enetc_get_rss_table(priv->si, indir, priv->si->num_rss);
641 
642 	return err;
643 }
644 
645 void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes)
646 {
647 	int i;
648 
649 	for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
650 		enetc_port_wr(hw, ENETC_PRSSK(i), ((u32 *)bytes)[i]);
651 }
652 
653 static int enetc_set_rxfh(struct net_device *ndev, const u32 *indir,
654 			  const u8 *key, const u8 hfunc)
655 {
656 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
657 	struct enetc_hw *hw = &priv->si->hw;
658 	int err = 0;
659 
660 	/* set hash key, if PF */
661 	if (key && hw->port)
662 		enetc_set_rss_key(hw, key);
663 
664 	/* set RSS table */
665 	if (indir)
666 		err = enetc_set_rss_table(priv->si, indir, priv->si->num_rss);
667 
668 	return err;
669 }
670 
671 static void enetc_get_ringparam(struct net_device *ndev,
672 				struct ethtool_ringparam *ring,
673 				struct kernel_ethtool_ringparam *kernel_ring,
674 				struct netlink_ext_ack *extack)
675 {
676 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
677 
678 	ring->rx_pending = priv->rx_bd_count;
679 	ring->tx_pending = priv->tx_bd_count;
680 
681 	/* do some h/w sanity checks for BDR length */
682 	if (netif_running(ndev)) {
683 		struct enetc_hw *hw = &priv->si->hw;
684 		u32 val = enetc_rxbdr_rd(hw, 0, ENETC_RBLENR);
685 
686 		if (val != priv->rx_bd_count)
687 			netif_err(priv, hw, ndev, "RxBDR[RBLENR] = %d!\n", val);
688 
689 		val = enetc_txbdr_rd(hw, 0, ENETC_TBLENR);
690 
691 		if (val != priv->tx_bd_count)
692 			netif_err(priv, hw, ndev, "TxBDR[TBLENR] = %d!\n", val);
693 	}
694 }
695 
696 static int enetc_get_coalesce(struct net_device *ndev,
697 			      struct ethtool_coalesce *ic,
698 			      struct kernel_ethtool_coalesce *kernel_coal,
699 			      struct netlink_ext_ack *extack)
700 {
701 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
702 	struct enetc_int_vector *v = priv->int_vector[0];
703 
704 	ic->tx_coalesce_usecs = enetc_cycles_to_usecs(priv->tx_ictt);
705 	ic->rx_coalesce_usecs = enetc_cycles_to_usecs(v->rx_ictt);
706 
707 	ic->tx_max_coalesced_frames = ENETC_TXIC_PKTTHR;
708 	ic->rx_max_coalesced_frames = ENETC_RXIC_PKTTHR;
709 
710 	ic->use_adaptive_rx_coalesce = priv->ic_mode & ENETC_IC_RX_ADAPTIVE;
711 
712 	return 0;
713 }
714 
715 static int enetc_set_coalesce(struct net_device *ndev,
716 			      struct ethtool_coalesce *ic,
717 			      struct kernel_ethtool_coalesce *kernel_coal,
718 			      struct netlink_ext_ack *extack)
719 {
720 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
721 	u32 rx_ictt, tx_ictt;
722 	int i, ic_mode;
723 	bool changed;
724 
725 	tx_ictt = enetc_usecs_to_cycles(ic->tx_coalesce_usecs);
726 	rx_ictt = enetc_usecs_to_cycles(ic->rx_coalesce_usecs);
727 
728 	if (ic->rx_max_coalesced_frames != ENETC_RXIC_PKTTHR)
729 		return -EOPNOTSUPP;
730 
731 	if (ic->tx_max_coalesced_frames != ENETC_TXIC_PKTTHR)
732 		return -EOPNOTSUPP;
733 
734 	ic_mode = ENETC_IC_NONE;
735 	if (ic->use_adaptive_rx_coalesce) {
736 		ic_mode |= ENETC_IC_RX_ADAPTIVE;
737 		rx_ictt = 0x1;
738 	} else {
739 		ic_mode |= rx_ictt ? ENETC_IC_RX_MANUAL : 0;
740 	}
741 
742 	ic_mode |= tx_ictt ? ENETC_IC_TX_MANUAL : 0;
743 
744 	/* commit the settings */
745 	changed = (ic_mode != priv->ic_mode) || (priv->tx_ictt != tx_ictt);
746 
747 	priv->ic_mode = ic_mode;
748 	priv->tx_ictt = tx_ictt;
749 
750 	for (i = 0; i < priv->bdr_int_num; i++) {
751 		struct enetc_int_vector *v = priv->int_vector[i];
752 
753 		v->rx_ictt = rx_ictt;
754 		v->rx_dim_en = !!(ic_mode & ENETC_IC_RX_ADAPTIVE);
755 	}
756 
757 	if (netif_running(ndev) && changed) {
758 		/* reconfigure the operation mode of h/w interrupts,
759 		 * traffic needs to be paused in the process
760 		 */
761 		enetc_stop(ndev);
762 		enetc_start(ndev);
763 	}
764 
765 	return 0;
766 }
767 
768 static int enetc_get_ts_info(struct net_device *ndev,
769 			     struct ethtool_ts_info *info)
770 {
771 	int *phc_idx;
772 
773 	phc_idx = symbol_get(enetc_phc_index);
774 	if (phc_idx) {
775 		info->phc_index = *phc_idx;
776 		symbol_put(enetc_phc_index);
777 	} else {
778 		info->phc_index = -1;
779 	}
780 
781 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
782 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
783 				SOF_TIMESTAMPING_RX_HARDWARE |
784 				SOF_TIMESTAMPING_RAW_HARDWARE |
785 				SOF_TIMESTAMPING_TX_SOFTWARE |
786 				SOF_TIMESTAMPING_RX_SOFTWARE |
787 				SOF_TIMESTAMPING_SOFTWARE;
788 
789 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
790 			 (1 << HWTSTAMP_TX_ON) |
791 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
792 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
793 			   (1 << HWTSTAMP_FILTER_ALL);
794 #else
795 	info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
796 				SOF_TIMESTAMPING_TX_SOFTWARE |
797 				SOF_TIMESTAMPING_SOFTWARE;
798 #endif
799 	return 0;
800 }
801 
802 static void enetc_get_wol(struct net_device *dev,
803 			  struct ethtool_wolinfo *wol)
804 {
805 	wol->supported = 0;
806 	wol->wolopts = 0;
807 
808 	if (dev->phydev)
809 		phy_ethtool_get_wol(dev->phydev, wol);
810 }
811 
812 static int enetc_set_wol(struct net_device *dev,
813 			 struct ethtool_wolinfo *wol)
814 {
815 	int ret;
816 
817 	if (!dev->phydev)
818 		return -EOPNOTSUPP;
819 
820 	ret = phy_ethtool_set_wol(dev->phydev, wol);
821 	if (!ret)
822 		device_set_wakeup_enable(&dev->dev, wol->wolopts);
823 
824 	return ret;
825 }
826 
827 static void enetc_get_pauseparam(struct net_device *dev,
828 				 struct ethtool_pauseparam *pause)
829 {
830 	struct enetc_ndev_priv *priv = netdev_priv(dev);
831 
832 	phylink_ethtool_get_pauseparam(priv->phylink, pause);
833 }
834 
835 static int enetc_set_pauseparam(struct net_device *dev,
836 				struct ethtool_pauseparam *pause)
837 {
838 	struct enetc_ndev_priv *priv = netdev_priv(dev);
839 
840 	return phylink_ethtool_set_pauseparam(priv->phylink, pause);
841 }
842 
843 static int enetc_get_link_ksettings(struct net_device *dev,
844 				    struct ethtool_link_ksettings *cmd)
845 {
846 	struct enetc_ndev_priv *priv = netdev_priv(dev);
847 
848 	if (!priv->phylink)
849 		return -EOPNOTSUPP;
850 
851 	return phylink_ethtool_ksettings_get(priv->phylink, cmd);
852 }
853 
854 static int enetc_set_link_ksettings(struct net_device *dev,
855 				    const struct ethtool_link_ksettings *cmd)
856 {
857 	struct enetc_ndev_priv *priv = netdev_priv(dev);
858 
859 	if (!priv->phylink)
860 		return -EOPNOTSUPP;
861 
862 	return phylink_ethtool_ksettings_set(priv->phylink, cmd);
863 }
864 
865 static const struct ethtool_ops enetc_pf_ethtool_ops = {
866 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
867 				     ETHTOOL_COALESCE_MAX_FRAMES |
868 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
869 	.get_regs_len = enetc_get_reglen,
870 	.get_regs = enetc_get_regs,
871 	.get_sset_count = enetc_get_sset_count,
872 	.get_strings = enetc_get_strings,
873 	.get_ethtool_stats = enetc_get_ethtool_stats,
874 	.get_pause_stats = enetc_get_pause_stats,
875 	.get_rmon_stats = enetc_get_rmon_stats,
876 	.get_eth_ctrl_stats = enetc_get_eth_ctrl_stats,
877 	.get_eth_mac_stats = enetc_get_eth_mac_stats,
878 	.get_rxnfc = enetc_get_rxnfc,
879 	.set_rxnfc = enetc_set_rxnfc,
880 	.get_rxfh_key_size = enetc_get_rxfh_key_size,
881 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
882 	.get_rxfh = enetc_get_rxfh,
883 	.set_rxfh = enetc_set_rxfh,
884 	.get_ringparam = enetc_get_ringparam,
885 	.get_coalesce = enetc_get_coalesce,
886 	.set_coalesce = enetc_set_coalesce,
887 	.get_link_ksettings = enetc_get_link_ksettings,
888 	.set_link_ksettings = enetc_set_link_ksettings,
889 	.get_link = ethtool_op_get_link,
890 	.get_ts_info = enetc_get_ts_info,
891 	.get_wol = enetc_get_wol,
892 	.set_wol = enetc_set_wol,
893 	.get_pauseparam = enetc_get_pauseparam,
894 	.set_pauseparam = enetc_set_pauseparam,
895 };
896 
897 static const struct ethtool_ops enetc_vf_ethtool_ops = {
898 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
899 				     ETHTOOL_COALESCE_MAX_FRAMES |
900 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
901 	.get_regs_len = enetc_get_reglen,
902 	.get_regs = enetc_get_regs,
903 	.get_sset_count = enetc_get_sset_count,
904 	.get_strings = enetc_get_strings,
905 	.get_ethtool_stats = enetc_get_ethtool_stats,
906 	.get_rxnfc = enetc_get_rxnfc,
907 	.set_rxnfc = enetc_set_rxnfc,
908 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
909 	.get_rxfh = enetc_get_rxfh,
910 	.set_rxfh = enetc_set_rxfh,
911 	.get_ringparam = enetc_get_ringparam,
912 	.get_coalesce = enetc_get_coalesce,
913 	.set_coalesce = enetc_set_coalesce,
914 	.get_link = ethtool_op_get_link,
915 	.get_ts_info = enetc_get_ts_info,
916 };
917 
918 void enetc_set_ethtool_ops(struct net_device *ndev)
919 {
920 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
921 
922 	if (enetc_si_is_pf(priv->si))
923 		ndev->ethtool_ops = &enetc_pf_ethtool_ops;
924 	else
925 		ndev->ethtool_ops = &enetc_vf_ethtool_ops;
926 }
927