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_PM0_REOCT,  "MAC rx ethernet octets" },
129 	{ ENETC_PM0_RALN,   "MAC rx alignment errors" },
130 	{ ENETC_PM0_RXPF,   "MAC rx valid pause frames" },
131 	{ ENETC_PM0_RFRM,   "MAC rx valid frames" },
132 	{ ENETC_PM0_RFCS,   "MAC rx fcs errors" },
133 	{ ENETC_PM0_RVLAN,  "MAC rx VLAN frames" },
134 	{ ENETC_PM0_RERR,   "MAC rx frame errors" },
135 	{ ENETC_PM0_RUCA,   "MAC rx unicast frames" },
136 	{ ENETC_PM0_RMCA,   "MAC rx multicast frames" },
137 	{ ENETC_PM0_RBCA,   "MAC rx broadcast frames" },
138 	{ ENETC_PM0_RDRP,   "MAC rx dropped packets" },
139 	{ ENETC_PM0_RPKT,   "MAC rx packets" },
140 	{ ENETC_PM0_RUND,   "MAC rx undersized packets" },
141 	{ ENETC_PM0_R64,    "MAC rx 64 byte packets" },
142 	{ ENETC_PM0_R127,   "MAC rx 65-127 byte packets" },
143 	{ ENETC_PM0_R255,   "MAC rx 128-255 byte packets" },
144 	{ ENETC_PM0_R511,   "MAC rx 256-511 byte packets" },
145 	{ ENETC_PM0_R1023,  "MAC rx 512-1023 byte packets" },
146 	{ ENETC_PM0_R1522,  "MAC rx 1024-1522 byte packets" },
147 	{ ENETC_PM0_R1523X, "MAC rx 1523 to max-octet packets" },
148 	{ ENETC_PM0_ROVR,   "MAC rx oversized packets" },
149 	{ ENETC_PM0_RJBR,   "MAC rx jabber packets" },
150 	{ ENETC_PM0_RFRG,   "MAC rx fragment packets" },
151 	{ ENETC_PM0_RCNP,   "MAC rx control packets" },
152 	{ ENETC_PM0_RDRNTP, "MAC rx fifo drop" },
153 	{ ENETC_PM0_TEOCT,  "MAC tx ethernet octets" },
154 	{ ENETC_PM0_TOCT,   "MAC tx octets" },
155 	{ ENETC_PM0_TCRSE,  "MAC tx carrier sense errors" },
156 	{ ENETC_PM0_TXPF,   "MAC tx valid pause frames" },
157 	{ ENETC_PM0_TFRM,   "MAC tx frames" },
158 	{ ENETC_PM0_TFCS,   "MAC tx fcs errors" },
159 	{ ENETC_PM0_TVLAN,  "MAC tx VLAN frames" },
160 	{ ENETC_PM0_TERR,   "MAC tx frame errors" },
161 	{ ENETC_PM0_TUCA,   "MAC tx unicast frames" },
162 	{ ENETC_PM0_TMCA,   "MAC tx multicast frames" },
163 	{ ENETC_PM0_TBCA,   "MAC tx broadcast frames" },
164 	{ ENETC_PM0_TPKT,   "MAC tx packets" },
165 	{ ENETC_PM0_TUND,   "MAC tx undersized packets" },
166 	{ ENETC_PM0_T64,    "MAC tx 64 byte packets" },
167 	{ ENETC_PM0_T127,   "MAC tx 65-127 byte packets" },
168 	{ ENETC_PM0_T255,   "MAC tx 128-255 byte packets" },
169 	{ ENETC_PM0_T511,   "MAC tx 256-511 byte packets" },
170 	{ ENETC_PM0_T1023,  "MAC tx 512-1023 byte packets" },
171 	{ ENETC_PM0_T1522,  "MAC tx 1024-1522 byte packets" },
172 	{ ENETC_PM0_T1523X, "MAC tx 1523 to max-octet packets" },
173 	{ ENETC_PM0_TCNP,   "MAC tx control packets" },
174 	{ ENETC_PM0_TDFR,   "MAC tx deferred packets" },
175 	{ ENETC_PM0_TMCOL,  "MAC tx multiple collisions" },
176 	{ ENETC_PM0_TSCOL,  "MAC tx single collisions" },
177 	{ ENETC_PM0_TLCOL,  "MAC tx late collisions" },
178 	{ ENETC_PM0_TECOL,  "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 			strlcpy(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 			strlcpy(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 #define ENETC_RSSHASH_L3 (RXH_L2DA | RXH_VLAN | RXH_L3_PROTO | RXH_IP_SRC | \
305 			  RXH_IP_DST)
306 #define ENETC_RSSHASH_L4 (ENETC_RSSHASH_L3 | RXH_L4_B_0_1 | RXH_L4_B_2_3)
307 static int enetc_get_rsshash(struct ethtool_rxnfc *rxnfc)
308 {
309 	static const u32 rsshash[] = {
310 			[TCP_V4_FLOW]    = ENETC_RSSHASH_L4,
311 			[UDP_V4_FLOW]    = ENETC_RSSHASH_L4,
312 			[SCTP_V4_FLOW]   = ENETC_RSSHASH_L4,
313 			[AH_ESP_V4_FLOW] = ENETC_RSSHASH_L3,
314 			[IPV4_FLOW]      = ENETC_RSSHASH_L3,
315 			[TCP_V6_FLOW]    = ENETC_RSSHASH_L4,
316 			[UDP_V6_FLOW]    = ENETC_RSSHASH_L4,
317 			[SCTP_V6_FLOW]   = ENETC_RSSHASH_L4,
318 			[AH_ESP_V6_FLOW] = ENETC_RSSHASH_L3,
319 			[IPV6_FLOW]      = ENETC_RSSHASH_L3,
320 			[ETHER_FLOW]     = 0,
321 	};
322 
323 	if (rxnfc->flow_type >= ARRAY_SIZE(rsshash))
324 		return -EINVAL;
325 
326 	rxnfc->data = rsshash[rxnfc->flow_type];
327 
328 	return 0;
329 }
330 
331 /* current HW spec does byte reversal on everything including MAC addresses */
332 static void ether_addr_copy_swap(u8 *dst, const u8 *src)
333 {
334 	int i;
335 
336 	for (i = 0; i < ETH_ALEN; i++)
337 		dst[i] = src[ETH_ALEN - i - 1];
338 }
339 
340 static int enetc_set_cls_entry(struct enetc_si *si,
341 			       struct ethtool_rx_flow_spec *fs, bool en)
342 {
343 	struct ethtool_tcpip4_spec *l4ip4_h, *l4ip4_m;
344 	struct ethtool_usrip4_spec *l3ip4_h, *l3ip4_m;
345 	struct ethhdr *eth_h, *eth_m;
346 	struct enetc_cmd_rfse rfse = { {0} };
347 
348 	if (!en)
349 		goto done;
350 
351 	switch (fs->flow_type & 0xff) {
352 	case TCP_V4_FLOW:
353 		l4ip4_h = &fs->h_u.tcp_ip4_spec;
354 		l4ip4_m = &fs->m_u.tcp_ip4_spec;
355 		goto l4ip4;
356 	case UDP_V4_FLOW:
357 		l4ip4_h = &fs->h_u.udp_ip4_spec;
358 		l4ip4_m = &fs->m_u.udp_ip4_spec;
359 		goto l4ip4;
360 	case SCTP_V4_FLOW:
361 		l4ip4_h = &fs->h_u.sctp_ip4_spec;
362 		l4ip4_m = &fs->m_u.sctp_ip4_spec;
363 l4ip4:
364 		rfse.sip_h[0] = l4ip4_h->ip4src;
365 		rfse.sip_m[0] = l4ip4_m->ip4src;
366 		rfse.dip_h[0] = l4ip4_h->ip4dst;
367 		rfse.dip_m[0] = l4ip4_m->ip4dst;
368 		rfse.sport_h = ntohs(l4ip4_h->psrc);
369 		rfse.sport_m = ntohs(l4ip4_m->psrc);
370 		rfse.dport_h = ntohs(l4ip4_h->pdst);
371 		rfse.dport_m = ntohs(l4ip4_m->pdst);
372 		if (l4ip4_m->tos)
373 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
374 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
375 		rfse.ethtype_m = 0xffff;
376 		break;
377 	case IP_USER_FLOW:
378 		l3ip4_h = &fs->h_u.usr_ip4_spec;
379 		l3ip4_m = &fs->m_u.usr_ip4_spec;
380 
381 		rfse.sip_h[0] = l3ip4_h->ip4src;
382 		rfse.sip_m[0] = l3ip4_m->ip4src;
383 		rfse.dip_h[0] = l3ip4_h->ip4dst;
384 		rfse.dip_m[0] = l3ip4_m->ip4dst;
385 		if (l3ip4_m->tos)
386 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
387 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
388 		rfse.ethtype_m = 0xffff;
389 		break;
390 	case ETHER_FLOW:
391 		eth_h = &fs->h_u.ether_spec;
392 		eth_m = &fs->m_u.ether_spec;
393 
394 		ether_addr_copy_swap(rfse.smac_h, eth_h->h_source);
395 		ether_addr_copy_swap(rfse.smac_m, eth_m->h_source);
396 		ether_addr_copy_swap(rfse.dmac_h, eth_h->h_dest);
397 		ether_addr_copy_swap(rfse.dmac_m, eth_m->h_dest);
398 		rfse.ethtype_h = ntohs(eth_h->h_proto);
399 		rfse.ethtype_m = ntohs(eth_m->h_proto);
400 		break;
401 	default:
402 		return -EOPNOTSUPP;
403 	}
404 
405 	rfse.mode |= ENETC_RFSE_EN;
406 	if (fs->ring_cookie != RX_CLS_FLOW_DISC) {
407 		rfse.mode |= ENETC_RFSE_MODE_BD;
408 		rfse.result = fs->ring_cookie;
409 	}
410 done:
411 	return enetc_set_fs_entry(si, &rfse, fs->location);
412 }
413 
414 static int enetc_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc,
415 			   u32 *rule_locs)
416 {
417 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
418 	int i, j;
419 
420 	switch (rxnfc->cmd) {
421 	case ETHTOOL_GRXRINGS:
422 		rxnfc->data = priv->num_rx_rings;
423 		break;
424 	case ETHTOOL_GRXFH:
425 		/* get RSS hash config */
426 		return enetc_get_rsshash(rxnfc);
427 	case ETHTOOL_GRXCLSRLCNT:
428 		/* total number of entries */
429 		rxnfc->data = priv->si->num_fs_entries;
430 		/* number of entries in use */
431 		rxnfc->rule_cnt = 0;
432 		for (i = 0; i < priv->si->num_fs_entries; i++)
433 			if (priv->cls_rules[i].used)
434 				rxnfc->rule_cnt++;
435 		break;
436 	case ETHTOOL_GRXCLSRULE:
437 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
438 			return -EINVAL;
439 
440 		/* get entry x */
441 		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
442 		break;
443 	case ETHTOOL_GRXCLSRLALL:
444 		/* total number of entries */
445 		rxnfc->data = priv->si->num_fs_entries;
446 		/* array of indexes of used entries */
447 		j = 0;
448 		for (i = 0; i < priv->si->num_fs_entries; i++) {
449 			if (!priv->cls_rules[i].used)
450 				continue;
451 			if (j == rxnfc->rule_cnt)
452 				return -EMSGSIZE;
453 			rule_locs[j++] = i;
454 		}
455 		/* number of entries in use */
456 		rxnfc->rule_cnt = j;
457 		break;
458 	default:
459 		return -EOPNOTSUPP;
460 	}
461 
462 	return 0;
463 }
464 
465 static int enetc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc)
466 {
467 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
468 	int err;
469 
470 	switch (rxnfc->cmd) {
471 	case ETHTOOL_SRXCLSRLINS:
472 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
473 			return -EINVAL;
474 
475 		if (rxnfc->fs.ring_cookie >= priv->num_rx_rings &&
476 		    rxnfc->fs.ring_cookie != RX_CLS_FLOW_DISC)
477 			return -EINVAL;
478 
479 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, true);
480 		if (err)
481 			return err;
482 		priv->cls_rules[rxnfc->fs.location].fs = rxnfc->fs;
483 		priv->cls_rules[rxnfc->fs.location].used = 1;
484 		break;
485 	case ETHTOOL_SRXCLSRLDEL:
486 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
487 			return -EINVAL;
488 
489 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, false);
490 		if (err)
491 			return err;
492 		priv->cls_rules[rxnfc->fs.location].used = 0;
493 		break;
494 	default:
495 		return -EOPNOTSUPP;
496 	}
497 
498 	return 0;
499 }
500 
501 static u32 enetc_get_rxfh_key_size(struct net_device *ndev)
502 {
503 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
504 
505 	/* return the size of the RX flow hash key.  PF only */
506 	return (priv->si->hw.port) ? ENETC_RSSHASH_KEY_SIZE : 0;
507 }
508 
509 static u32 enetc_get_rxfh_indir_size(struct net_device *ndev)
510 {
511 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
512 
513 	/* return the size of the RX flow hash indirection table */
514 	return priv->si->num_rss;
515 }
516 
517 static int enetc_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key,
518 			  u8 *hfunc)
519 {
520 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
521 	struct enetc_hw *hw = &priv->si->hw;
522 	int err = 0, i;
523 
524 	/* return hash function */
525 	if (hfunc)
526 		*hfunc = ETH_RSS_HASH_TOP;
527 
528 	/* return hash key */
529 	if (key && hw->port)
530 		for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
531 			((u32 *)key)[i] = enetc_port_rd(hw, ENETC_PRSSK(i));
532 
533 	/* return RSS table */
534 	if (indir)
535 		err = enetc_get_rss_table(priv->si, indir, priv->si->num_rss);
536 
537 	return err;
538 }
539 
540 void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes)
541 {
542 	int i;
543 
544 	for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
545 		enetc_port_wr(hw, ENETC_PRSSK(i), ((u32 *)bytes)[i]);
546 }
547 
548 static int enetc_set_rxfh(struct net_device *ndev, const u32 *indir,
549 			  const u8 *key, const u8 hfunc)
550 {
551 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
552 	struct enetc_hw *hw = &priv->si->hw;
553 	int err = 0;
554 
555 	/* set hash key, if PF */
556 	if (key && hw->port)
557 		enetc_set_rss_key(hw, key);
558 
559 	/* set RSS table */
560 	if (indir)
561 		err = enetc_set_rss_table(priv->si, indir, priv->si->num_rss);
562 
563 	return err;
564 }
565 
566 static void enetc_get_ringparam(struct net_device *ndev,
567 				struct ethtool_ringparam *ring,
568 				struct kernel_ethtool_ringparam *kernel_ring,
569 				struct netlink_ext_ack *extack)
570 {
571 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
572 
573 	ring->rx_pending = priv->rx_bd_count;
574 	ring->tx_pending = priv->tx_bd_count;
575 
576 	/* do some h/w sanity checks for BDR length */
577 	if (netif_running(ndev)) {
578 		struct enetc_hw *hw = &priv->si->hw;
579 		u32 val = enetc_rxbdr_rd(hw, 0, ENETC_RBLENR);
580 
581 		if (val != priv->rx_bd_count)
582 			netif_err(priv, hw, ndev, "RxBDR[RBLENR] = %d!\n", val);
583 
584 		val = enetc_txbdr_rd(hw, 0, ENETC_TBLENR);
585 
586 		if (val != priv->tx_bd_count)
587 			netif_err(priv, hw, ndev, "TxBDR[TBLENR] = %d!\n", val);
588 	}
589 }
590 
591 static int enetc_get_coalesce(struct net_device *ndev,
592 			      struct ethtool_coalesce *ic,
593 			      struct kernel_ethtool_coalesce *kernel_coal,
594 			      struct netlink_ext_ack *extack)
595 {
596 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
597 	struct enetc_int_vector *v = priv->int_vector[0];
598 
599 	ic->tx_coalesce_usecs = enetc_cycles_to_usecs(priv->tx_ictt);
600 	ic->rx_coalesce_usecs = enetc_cycles_to_usecs(v->rx_ictt);
601 
602 	ic->tx_max_coalesced_frames = ENETC_TXIC_PKTTHR;
603 	ic->rx_max_coalesced_frames = ENETC_RXIC_PKTTHR;
604 
605 	ic->use_adaptive_rx_coalesce = priv->ic_mode & ENETC_IC_RX_ADAPTIVE;
606 
607 	return 0;
608 }
609 
610 static int enetc_set_coalesce(struct net_device *ndev,
611 			      struct ethtool_coalesce *ic,
612 			      struct kernel_ethtool_coalesce *kernel_coal,
613 			      struct netlink_ext_ack *extack)
614 {
615 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
616 	u32 rx_ictt, tx_ictt;
617 	int i, ic_mode;
618 	bool changed;
619 
620 	tx_ictt = enetc_usecs_to_cycles(ic->tx_coalesce_usecs);
621 	rx_ictt = enetc_usecs_to_cycles(ic->rx_coalesce_usecs);
622 
623 	if (ic->rx_max_coalesced_frames != ENETC_RXIC_PKTTHR)
624 		return -EOPNOTSUPP;
625 
626 	if (ic->tx_max_coalesced_frames != ENETC_TXIC_PKTTHR)
627 		return -EOPNOTSUPP;
628 
629 	ic_mode = ENETC_IC_NONE;
630 	if (ic->use_adaptive_rx_coalesce) {
631 		ic_mode |= ENETC_IC_RX_ADAPTIVE;
632 		rx_ictt = 0x1;
633 	} else {
634 		ic_mode |= rx_ictt ? ENETC_IC_RX_MANUAL : 0;
635 	}
636 
637 	ic_mode |= tx_ictt ? ENETC_IC_TX_MANUAL : 0;
638 
639 	/* commit the settings */
640 	changed = (ic_mode != priv->ic_mode) || (priv->tx_ictt != tx_ictt);
641 
642 	priv->ic_mode = ic_mode;
643 	priv->tx_ictt = tx_ictt;
644 
645 	for (i = 0; i < priv->bdr_int_num; i++) {
646 		struct enetc_int_vector *v = priv->int_vector[i];
647 
648 		v->rx_ictt = rx_ictt;
649 		v->rx_dim_en = !!(ic_mode & ENETC_IC_RX_ADAPTIVE);
650 	}
651 
652 	if (netif_running(ndev) && changed) {
653 		/* reconfigure the operation mode of h/w interrupts,
654 		 * traffic needs to be paused in the process
655 		 */
656 		enetc_stop(ndev);
657 		enetc_start(ndev);
658 	}
659 
660 	return 0;
661 }
662 
663 static int enetc_get_ts_info(struct net_device *ndev,
664 			     struct ethtool_ts_info *info)
665 {
666 	int *phc_idx;
667 
668 	phc_idx = symbol_get(enetc_phc_index);
669 	if (phc_idx) {
670 		info->phc_index = *phc_idx;
671 		symbol_put(enetc_phc_index);
672 	} else {
673 		info->phc_index = -1;
674 	}
675 
676 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
677 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
678 				SOF_TIMESTAMPING_RX_HARDWARE |
679 				SOF_TIMESTAMPING_RAW_HARDWARE |
680 				SOF_TIMESTAMPING_TX_SOFTWARE |
681 				SOF_TIMESTAMPING_RX_SOFTWARE |
682 				SOF_TIMESTAMPING_SOFTWARE;
683 
684 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
685 			 (1 << HWTSTAMP_TX_ON) |
686 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
687 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
688 			   (1 << HWTSTAMP_FILTER_ALL);
689 #else
690 	info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
691 				SOF_TIMESTAMPING_TX_SOFTWARE |
692 				SOF_TIMESTAMPING_SOFTWARE;
693 #endif
694 	return 0;
695 }
696 
697 static void enetc_get_wol(struct net_device *dev,
698 			  struct ethtool_wolinfo *wol)
699 {
700 	wol->supported = 0;
701 	wol->wolopts = 0;
702 
703 	if (dev->phydev)
704 		phy_ethtool_get_wol(dev->phydev, wol);
705 }
706 
707 static int enetc_set_wol(struct net_device *dev,
708 			 struct ethtool_wolinfo *wol)
709 {
710 	int ret;
711 
712 	if (!dev->phydev)
713 		return -EOPNOTSUPP;
714 
715 	ret = phy_ethtool_set_wol(dev->phydev, wol);
716 	if (!ret)
717 		device_set_wakeup_enable(&dev->dev, wol->wolopts);
718 
719 	return ret;
720 }
721 
722 static void enetc_get_pauseparam(struct net_device *dev,
723 				 struct ethtool_pauseparam *pause)
724 {
725 	struct enetc_ndev_priv *priv = netdev_priv(dev);
726 
727 	phylink_ethtool_get_pauseparam(priv->phylink, pause);
728 }
729 
730 static int enetc_set_pauseparam(struct net_device *dev,
731 				struct ethtool_pauseparam *pause)
732 {
733 	struct enetc_ndev_priv *priv = netdev_priv(dev);
734 
735 	return phylink_ethtool_set_pauseparam(priv->phylink, pause);
736 }
737 
738 static int enetc_get_link_ksettings(struct net_device *dev,
739 				    struct ethtool_link_ksettings *cmd)
740 {
741 	struct enetc_ndev_priv *priv = netdev_priv(dev);
742 
743 	if (!priv->phylink)
744 		return -EOPNOTSUPP;
745 
746 	return phylink_ethtool_ksettings_get(priv->phylink, cmd);
747 }
748 
749 static int enetc_set_link_ksettings(struct net_device *dev,
750 				    const struct ethtool_link_ksettings *cmd)
751 {
752 	struct enetc_ndev_priv *priv = netdev_priv(dev);
753 
754 	if (!priv->phylink)
755 		return -EOPNOTSUPP;
756 
757 	return phylink_ethtool_ksettings_set(priv->phylink, cmd);
758 }
759 
760 static const struct ethtool_ops enetc_pf_ethtool_ops = {
761 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
762 				     ETHTOOL_COALESCE_MAX_FRAMES |
763 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
764 	.get_regs_len = enetc_get_reglen,
765 	.get_regs = enetc_get_regs,
766 	.get_sset_count = enetc_get_sset_count,
767 	.get_strings = enetc_get_strings,
768 	.get_ethtool_stats = enetc_get_ethtool_stats,
769 	.get_rxnfc = enetc_get_rxnfc,
770 	.set_rxnfc = enetc_set_rxnfc,
771 	.get_rxfh_key_size = enetc_get_rxfh_key_size,
772 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
773 	.get_rxfh = enetc_get_rxfh,
774 	.set_rxfh = enetc_set_rxfh,
775 	.get_ringparam = enetc_get_ringparam,
776 	.get_coalesce = enetc_get_coalesce,
777 	.set_coalesce = enetc_set_coalesce,
778 	.get_link_ksettings = enetc_get_link_ksettings,
779 	.set_link_ksettings = enetc_set_link_ksettings,
780 	.get_link = ethtool_op_get_link,
781 	.get_ts_info = enetc_get_ts_info,
782 	.get_wol = enetc_get_wol,
783 	.set_wol = enetc_set_wol,
784 	.get_pauseparam = enetc_get_pauseparam,
785 	.set_pauseparam = enetc_set_pauseparam,
786 };
787 
788 static const struct ethtool_ops enetc_vf_ethtool_ops = {
789 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
790 				     ETHTOOL_COALESCE_MAX_FRAMES |
791 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
792 	.get_regs_len = enetc_get_reglen,
793 	.get_regs = enetc_get_regs,
794 	.get_sset_count = enetc_get_sset_count,
795 	.get_strings = enetc_get_strings,
796 	.get_ethtool_stats = enetc_get_ethtool_stats,
797 	.get_rxnfc = enetc_get_rxnfc,
798 	.set_rxnfc = enetc_set_rxnfc,
799 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
800 	.get_rxfh = enetc_get_rxfh,
801 	.set_rxfh = enetc_set_rxfh,
802 	.get_ringparam = enetc_get_ringparam,
803 	.get_coalesce = enetc_get_coalesce,
804 	.set_coalesce = enetc_set_coalesce,
805 	.get_link = ethtool_op_get_link,
806 	.get_ts_info = enetc_get_ts_info,
807 };
808 
809 void enetc_set_ethtool_ops(struct net_device *ndev)
810 {
811 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
812 
813 	if (enetc_si_is_pf(priv->si))
814 		ndev->ethtool_ops = &enetc_pf_ethtool_ops;
815 	else
816 		ndev->ethtool_ops = &enetc_vf_ethtool_ops;
817 }
818