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_R1518,  "MAC rx 1024-1518 byte packets" },
147 	{ ENETC_PM0_R1519X, "MAC rx 1519 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 frames" },
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_T127,   "MAC tx 65-127 byte packets" },
167 	{ ENETC_PM0_T1023,  "MAC tx 512-1023 byte packets" },
168 	{ ENETC_PM0_T1518,  "MAC tx 1024-1518 byte packets" },
169 	{ ENETC_PM0_TCNP,   "MAC tx control packets" },
170 	{ ENETC_PM0_TDFR,   "MAC tx deferred packets" },
171 	{ ENETC_PM0_TMCOL,  "MAC tx multiple collisions" },
172 	{ ENETC_PM0_TSCOL,  "MAC tx single collisions" },
173 	{ ENETC_PM0_TLCOL,  "MAC tx late collisions" },
174 	{ ENETC_PM0_TECOL,  "MAC tx excessive collisions" },
175 	{ ENETC_UFDMF,      "SI MAC nomatch u-cast discards" },
176 	{ ENETC_MFDMF,      "SI MAC nomatch m-cast discards" },
177 	{ ENETC_PBFDSIR,    "SI MAC nomatch b-cast discards" },
178 	{ ENETC_PUFDVFR,    "SI VLAN nomatch u-cast discards" },
179 	{ ENETC_PMFDVFR,    "SI VLAN nomatch m-cast discards" },
180 	{ ENETC_PBFDVFR,    "SI VLAN nomatch b-cast discards" },
181 	{ ENETC_PFDMSAPR,   "SI pruning discarded frames" },
182 	{ ENETC_PICDR(0),   "ICM DR0 discarded frames" },
183 	{ ENETC_PICDR(1),   "ICM DR1 discarded frames" },
184 	{ ENETC_PICDR(2),   "ICM DR2 discarded frames" },
185 	{ ENETC_PICDR(3),   "ICM DR3 discarded frames" },
186 };
187 
188 static const char rx_ring_stats[][ETH_GSTRING_LEN] = {
189 	"Rx ring %2d frames",
190 	"Rx ring %2d alloc errors",
191 };
192 
193 static const char tx_ring_stats[][ETH_GSTRING_LEN] = {
194 	"Tx ring %2d frames",
195 };
196 
197 static int enetc_get_sset_count(struct net_device *ndev, int sset)
198 {
199 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
200 	int len;
201 
202 	if (sset != ETH_SS_STATS)
203 		return -EOPNOTSUPP;
204 
205 	len = ARRAY_SIZE(enetc_si_counters) +
206 	      ARRAY_SIZE(tx_ring_stats) * priv->num_tx_rings +
207 	      ARRAY_SIZE(rx_ring_stats) * priv->num_rx_rings;
208 
209 	if (!enetc_si_is_pf(priv->si))
210 		return len;
211 
212 	len += ARRAY_SIZE(enetc_port_counters);
213 
214 	return len;
215 }
216 
217 static void enetc_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
218 {
219 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
220 	u8 *p = data;
221 	int i, j;
222 
223 	switch (stringset) {
224 	case ETH_SS_STATS:
225 		for (i = 0; i < ARRAY_SIZE(enetc_si_counters); i++) {
226 			strlcpy(p, enetc_si_counters[i].name, ETH_GSTRING_LEN);
227 			p += ETH_GSTRING_LEN;
228 		}
229 		for (i = 0; i < priv->num_tx_rings; i++) {
230 			for (j = 0; j < ARRAY_SIZE(tx_ring_stats); j++) {
231 				snprintf(p, ETH_GSTRING_LEN, tx_ring_stats[j],
232 					 i);
233 				p += ETH_GSTRING_LEN;
234 			}
235 		}
236 		for (i = 0; i < priv->num_rx_rings; i++) {
237 			for (j = 0; j < ARRAY_SIZE(rx_ring_stats); j++) {
238 				snprintf(p, ETH_GSTRING_LEN, rx_ring_stats[j],
239 					 i);
240 				p += ETH_GSTRING_LEN;
241 			}
242 		}
243 
244 		if (!enetc_si_is_pf(priv->si))
245 			break;
246 
247 		for (i = 0; i < ARRAY_SIZE(enetc_port_counters); i++) {
248 			strlcpy(p, enetc_port_counters[i].name,
249 				ETH_GSTRING_LEN);
250 			p += ETH_GSTRING_LEN;
251 		}
252 		break;
253 	}
254 }
255 
256 static void enetc_get_ethtool_stats(struct net_device *ndev,
257 				    struct ethtool_stats *stats, u64 *data)
258 {
259 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
260 	struct enetc_hw *hw = &priv->si->hw;
261 	int i, o = 0;
262 
263 	for (i = 0; i < ARRAY_SIZE(enetc_si_counters); i++)
264 		data[o++] = enetc_rd64(hw, enetc_si_counters[i].reg);
265 
266 	for (i = 0; i < priv->num_tx_rings; i++)
267 		data[o++] = priv->tx_ring[i]->stats.packets;
268 
269 	for (i = 0; i < priv->num_rx_rings; i++) {
270 		data[o++] = priv->rx_ring[i]->stats.packets;
271 		data[o++] = priv->rx_ring[i]->stats.rx_alloc_errs;
272 	}
273 
274 	if (!enetc_si_is_pf(priv->si))
275 		return;
276 
277 	for (i = 0; i < ARRAY_SIZE(enetc_port_counters); i++)
278 		data[o++] = enetc_port_rd(hw, enetc_port_counters[i].reg);
279 }
280 
281 #define ENETC_RSSHASH_L3 (RXH_L2DA | RXH_VLAN | RXH_L3_PROTO | RXH_IP_SRC | \
282 			  RXH_IP_DST)
283 #define ENETC_RSSHASH_L4 (ENETC_RSSHASH_L3 | RXH_L4_B_0_1 | RXH_L4_B_2_3)
284 static int enetc_get_rsshash(struct ethtool_rxnfc *rxnfc)
285 {
286 	static const u32 rsshash[] = {
287 			[TCP_V4_FLOW]    = ENETC_RSSHASH_L4,
288 			[UDP_V4_FLOW]    = ENETC_RSSHASH_L4,
289 			[SCTP_V4_FLOW]   = ENETC_RSSHASH_L4,
290 			[AH_ESP_V4_FLOW] = ENETC_RSSHASH_L3,
291 			[IPV4_FLOW]      = ENETC_RSSHASH_L3,
292 			[TCP_V6_FLOW]    = ENETC_RSSHASH_L4,
293 			[UDP_V6_FLOW]    = ENETC_RSSHASH_L4,
294 			[SCTP_V6_FLOW]   = ENETC_RSSHASH_L4,
295 			[AH_ESP_V6_FLOW] = ENETC_RSSHASH_L3,
296 			[IPV6_FLOW]      = ENETC_RSSHASH_L3,
297 			[ETHER_FLOW]     = 0,
298 	};
299 
300 	if (rxnfc->flow_type >= ARRAY_SIZE(rsshash))
301 		return -EINVAL;
302 
303 	rxnfc->data = rsshash[rxnfc->flow_type];
304 
305 	return 0;
306 }
307 
308 /* current HW spec does byte reversal on everything including MAC addresses */
309 static void ether_addr_copy_swap(u8 *dst, const u8 *src)
310 {
311 	int i;
312 
313 	for (i = 0; i < ETH_ALEN; i++)
314 		dst[i] = src[ETH_ALEN - i - 1];
315 }
316 
317 static int enetc_set_cls_entry(struct enetc_si *si,
318 			       struct ethtool_rx_flow_spec *fs, bool en)
319 {
320 	struct ethtool_tcpip4_spec *l4ip4_h, *l4ip4_m;
321 	struct ethtool_usrip4_spec *l3ip4_h, *l3ip4_m;
322 	struct ethhdr *eth_h, *eth_m;
323 	struct enetc_cmd_rfse rfse = { {0} };
324 
325 	if (!en)
326 		goto done;
327 
328 	switch (fs->flow_type & 0xff) {
329 	case TCP_V4_FLOW:
330 		l4ip4_h = &fs->h_u.tcp_ip4_spec;
331 		l4ip4_m = &fs->m_u.tcp_ip4_spec;
332 		goto l4ip4;
333 	case UDP_V4_FLOW:
334 		l4ip4_h = &fs->h_u.udp_ip4_spec;
335 		l4ip4_m = &fs->m_u.udp_ip4_spec;
336 		goto l4ip4;
337 	case SCTP_V4_FLOW:
338 		l4ip4_h = &fs->h_u.sctp_ip4_spec;
339 		l4ip4_m = &fs->m_u.sctp_ip4_spec;
340 l4ip4:
341 		rfse.sip_h[0] = l4ip4_h->ip4src;
342 		rfse.sip_m[0] = l4ip4_m->ip4src;
343 		rfse.dip_h[0] = l4ip4_h->ip4dst;
344 		rfse.dip_m[0] = l4ip4_m->ip4dst;
345 		rfse.sport_h = ntohs(l4ip4_h->psrc);
346 		rfse.sport_m = ntohs(l4ip4_m->psrc);
347 		rfse.dport_h = ntohs(l4ip4_h->pdst);
348 		rfse.dport_m = ntohs(l4ip4_m->pdst);
349 		if (l4ip4_m->tos)
350 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
351 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
352 		rfse.ethtype_m = 0xffff;
353 		break;
354 	case IP_USER_FLOW:
355 		l3ip4_h = &fs->h_u.usr_ip4_spec;
356 		l3ip4_m = &fs->m_u.usr_ip4_spec;
357 
358 		rfse.sip_h[0] = l3ip4_h->ip4src;
359 		rfse.sip_m[0] = l3ip4_m->ip4src;
360 		rfse.dip_h[0] = l3ip4_h->ip4dst;
361 		rfse.dip_m[0] = l3ip4_m->ip4dst;
362 		if (l3ip4_m->tos)
363 			netdev_warn(si->ndev, "ToS field is not supported and was ignored\n");
364 		rfse.ethtype_h = ETH_P_IP; /* IPv4 */
365 		rfse.ethtype_m = 0xffff;
366 		break;
367 	case ETHER_FLOW:
368 		eth_h = &fs->h_u.ether_spec;
369 		eth_m = &fs->m_u.ether_spec;
370 
371 		ether_addr_copy_swap(rfse.smac_h, eth_h->h_source);
372 		ether_addr_copy_swap(rfse.smac_m, eth_m->h_source);
373 		ether_addr_copy_swap(rfse.dmac_h, eth_h->h_dest);
374 		ether_addr_copy_swap(rfse.dmac_m, eth_m->h_dest);
375 		rfse.ethtype_h = ntohs(eth_h->h_proto);
376 		rfse.ethtype_m = ntohs(eth_m->h_proto);
377 		break;
378 	default:
379 		return -EOPNOTSUPP;
380 	}
381 
382 	rfse.mode |= ENETC_RFSE_EN;
383 	if (fs->ring_cookie != RX_CLS_FLOW_DISC) {
384 		rfse.mode |= ENETC_RFSE_MODE_BD;
385 		rfse.result = fs->ring_cookie;
386 	}
387 done:
388 	return enetc_set_fs_entry(si, &rfse, fs->location);
389 }
390 
391 static int enetc_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc,
392 			   u32 *rule_locs)
393 {
394 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
395 	int i, j;
396 
397 	switch (rxnfc->cmd) {
398 	case ETHTOOL_GRXRINGS:
399 		rxnfc->data = priv->num_rx_rings;
400 		break;
401 	case ETHTOOL_GRXFH:
402 		/* get RSS hash config */
403 		return enetc_get_rsshash(rxnfc);
404 	case ETHTOOL_GRXCLSRLCNT:
405 		/* total number of entries */
406 		rxnfc->data = priv->si->num_fs_entries;
407 		/* number of entries in use */
408 		rxnfc->rule_cnt = 0;
409 		for (i = 0; i < priv->si->num_fs_entries; i++)
410 			if (priv->cls_rules[i].used)
411 				rxnfc->rule_cnt++;
412 		break;
413 	case ETHTOOL_GRXCLSRULE:
414 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
415 			return -EINVAL;
416 
417 		/* get entry x */
418 		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
419 		break;
420 	case ETHTOOL_GRXCLSRLALL:
421 		/* total number of entries */
422 		rxnfc->data = priv->si->num_fs_entries;
423 		/* array of indexes of used entries */
424 		j = 0;
425 		for (i = 0; i < priv->si->num_fs_entries; i++) {
426 			if (!priv->cls_rules[i].used)
427 				continue;
428 			if (j == rxnfc->rule_cnt)
429 				return -EMSGSIZE;
430 			rule_locs[j++] = i;
431 		}
432 		/* number of entries in use */
433 		rxnfc->rule_cnt = j;
434 		break;
435 	default:
436 		return -EOPNOTSUPP;
437 	}
438 
439 	return 0;
440 }
441 
442 static int enetc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *rxnfc)
443 {
444 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
445 	int err;
446 
447 	switch (rxnfc->cmd) {
448 	case ETHTOOL_SRXCLSRLINS:
449 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
450 			return -EINVAL;
451 
452 		if (rxnfc->fs.ring_cookie >= priv->num_rx_rings &&
453 		    rxnfc->fs.ring_cookie != RX_CLS_FLOW_DISC)
454 			return -EINVAL;
455 
456 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, true);
457 		if (err)
458 			return err;
459 		priv->cls_rules[rxnfc->fs.location].fs = rxnfc->fs;
460 		priv->cls_rules[rxnfc->fs.location].used = 1;
461 		break;
462 	case ETHTOOL_SRXCLSRLDEL:
463 		if (rxnfc->fs.location >= priv->si->num_fs_entries)
464 			return -EINVAL;
465 
466 		err = enetc_set_cls_entry(priv->si, &rxnfc->fs, false);
467 		if (err)
468 			return err;
469 		priv->cls_rules[rxnfc->fs.location].used = 0;
470 		break;
471 	default:
472 		return -EOPNOTSUPP;
473 	}
474 
475 	return 0;
476 }
477 
478 static u32 enetc_get_rxfh_key_size(struct net_device *ndev)
479 {
480 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
481 
482 	/* return the size of the RX flow hash key.  PF only */
483 	return (priv->si->hw.port) ? ENETC_RSSHASH_KEY_SIZE : 0;
484 }
485 
486 static u32 enetc_get_rxfh_indir_size(struct net_device *ndev)
487 {
488 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
489 
490 	/* return the size of the RX flow hash indirection table */
491 	return priv->si->num_rss;
492 }
493 
494 static int enetc_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key,
495 			  u8 *hfunc)
496 {
497 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
498 	struct enetc_hw *hw = &priv->si->hw;
499 	int err = 0, i;
500 
501 	/* return hash function */
502 	if (hfunc)
503 		*hfunc = ETH_RSS_HASH_TOP;
504 
505 	/* return hash key */
506 	if (key && hw->port)
507 		for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
508 			((u32 *)key)[i] = enetc_port_rd(hw, ENETC_PRSSK(i));
509 
510 	/* return RSS table */
511 	if (indir)
512 		err = enetc_get_rss_table(priv->si, indir, priv->si->num_rss);
513 
514 	return err;
515 }
516 
517 void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes)
518 {
519 	int i;
520 
521 	for (i = 0; i < ENETC_RSSHASH_KEY_SIZE / 4; i++)
522 		enetc_port_wr(hw, ENETC_PRSSK(i), ((u32 *)bytes)[i]);
523 }
524 
525 static int enetc_set_rxfh(struct net_device *ndev, const u32 *indir,
526 			  const u8 *key, const u8 hfunc)
527 {
528 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
529 	struct enetc_hw *hw = &priv->si->hw;
530 	int err = 0;
531 
532 	/* set hash key, if PF */
533 	if (key && hw->port)
534 		enetc_set_rss_key(hw, key);
535 
536 	/* set RSS table */
537 	if (indir)
538 		err = enetc_set_rss_table(priv->si, indir, priv->si->num_rss);
539 
540 	return err;
541 }
542 
543 static void enetc_get_ringparam(struct net_device *ndev,
544 				struct ethtool_ringparam *ring)
545 {
546 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
547 
548 	ring->rx_pending = priv->rx_bd_count;
549 	ring->tx_pending = priv->tx_bd_count;
550 
551 	/* do some h/w sanity checks for BDR length */
552 	if (netif_running(ndev)) {
553 		struct enetc_hw *hw = &priv->si->hw;
554 		u32 val = enetc_rxbdr_rd(hw, 0, ENETC_RBLENR);
555 
556 		if (val != priv->rx_bd_count)
557 			netif_err(priv, hw, ndev, "RxBDR[RBLENR] = %d!\n", val);
558 
559 		val = enetc_txbdr_rd(hw, 0, ENETC_TBLENR);
560 
561 		if (val != priv->tx_bd_count)
562 			netif_err(priv, hw, ndev, "TxBDR[TBLENR] = %d!\n", val);
563 	}
564 }
565 
566 static int enetc_get_coalesce(struct net_device *ndev,
567 			      struct ethtool_coalesce *ic)
568 {
569 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
570 	struct enetc_int_vector *v = priv->int_vector[0];
571 
572 	ic->tx_coalesce_usecs = enetc_cycles_to_usecs(priv->tx_ictt);
573 	ic->rx_coalesce_usecs = enetc_cycles_to_usecs(v->rx_ictt);
574 
575 	ic->tx_max_coalesced_frames = ENETC_TXIC_PKTTHR;
576 	ic->rx_max_coalesced_frames = ENETC_RXIC_PKTTHR;
577 
578 	ic->use_adaptive_rx_coalesce = priv->ic_mode & ENETC_IC_RX_ADAPTIVE;
579 
580 	return 0;
581 }
582 
583 static int enetc_set_coalesce(struct net_device *ndev,
584 			      struct ethtool_coalesce *ic)
585 {
586 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
587 	u32 rx_ictt, tx_ictt;
588 	int i, ic_mode;
589 	bool changed;
590 
591 	tx_ictt = enetc_usecs_to_cycles(ic->tx_coalesce_usecs);
592 	rx_ictt = enetc_usecs_to_cycles(ic->rx_coalesce_usecs);
593 
594 	if (ic->rx_max_coalesced_frames != ENETC_RXIC_PKTTHR)
595 		return -EOPNOTSUPP;
596 
597 	if (ic->tx_max_coalesced_frames != ENETC_TXIC_PKTTHR)
598 		return -EOPNOTSUPP;
599 
600 	ic_mode = ENETC_IC_NONE;
601 	if (ic->use_adaptive_rx_coalesce) {
602 		ic_mode |= ENETC_IC_RX_ADAPTIVE;
603 		rx_ictt = 0x1;
604 	} else {
605 		ic_mode |= rx_ictt ? ENETC_IC_RX_MANUAL : 0;
606 	}
607 
608 	ic_mode |= tx_ictt ? ENETC_IC_TX_MANUAL : 0;
609 
610 	/* commit the settings */
611 	changed = (ic_mode != priv->ic_mode) || (priv->tx_ictt != tx_ictt);
612 
613 	priv->ic_mode = ic_mode;
614 	priv->tx_ictt = tx_ictt;
615 
616 	for (i = 0; i < priv->bdr_int_num; i++) {
617 		struct enetc_int_vector *v = priv->int_vector[i];
618 
619 		v->rx_ictt = rx_ictt;
620 		v->rx_dim_en = !!(ic_mode & ENETC_IC_RX_ADAPTIVE);
621 	}
622 
623 	if (netif_running(ndev) && changed) {
624 		/* reconfigure the operation mode of h/w interrupts,
625 		 * traffic needs to be paused in the process
626 		 */
627 		enetc_stop(ndev);
628 		enetc_start(ndev);
629 	}
630 
631 	return 0;
632 }
633 
634 static int enetc_get_ts_info(struct net_device *ndev,
635 			     struct ethtool_ts_info *info)
636 {
637 	int *phc_idx;
638 
639 	phc_idx = symbol_get(enetc_phc_index);
640 	if (phc_idx) {
641 		info->phc_index = *phc_idx;
642 		symbol_put(enetc_phc_index);
643 	} else {
644 		info->phc_index = -1;
645 	}
646 
647 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
648 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
649 				SOF_TIMESTAMPING_RX_HARDWARE |
650 				SOF_TIMESTAMPING_RAW_HARDWARE;
651 
652 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
653 			 (1 << HWTSTAMP_TX_ON);
654 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
655 			   (1 << HWTSTAMP_FILTER_ALL);
656 #else
657 	info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
658 				SOF_TIMESTAMPING_TX_SOFTWARE |
659 				SOF_TIMESTAMPING_SOFTWARE;
660 #endif
661 	return 0;
662 }
663 
664 static void enetc_get_wol(struct net_device *dev,
665 			  struct ethtool_wolinfo *wol)
666 {
667 	wol->supported = 0;
668 	wol->wolopts = 0;
669 
670 	if (dev->phydev)
671 		phy_ethtool_get_wol(dev->phydev, wol);
672 }
673 
674 static int enetc_set_wol(struct net_device *dev,
675 			 struct ethtool_wolinfo *wol)
676 {
677 	int ret;
678 
679 	if (!dev->phydev)
680 		return -EOPNOTSUPP;
681 
682 	ret = phy_ethtool_set_wol(dev->phydev, wol);
683 	if (!ret)
684 		device_set_wakeup_enable(&dev->dev, wol->wolopts);
685 
686 	return ret;
687 }
688 
689 static const struct ethtool_ops enetc_pf_ethtool_ops = {
690 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
691 				     ETHTOOL_COALESCE_MAX_FRAMES |
692 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
693 	.get_regs_len = enetc_get_reglen,
694 	.get_regs = enetc_get_regs,
695 	.get_sset_count = enetc_get_sset_count,
696 	.get_strings = enetc_get_strings,
697 	.get_ethtool_stats = enetc_get_ethtool_stats,
698 	.get_rxnfc = enetc_get_rxnfc,
699 	.set_rxnfc = enetc_set_rxnfc,
700 	.get_rxfh_key_size = enetc_get_rxfh_key_size,
701 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
702 	.get_rxfh = enetc_get_rxfh,
703 	.set_rxfh = enetc_set_rxfh,
704 	.get_ringparam = enetc_get_ringparam,
705 	.get_coalesce = enetc_get_coalesce,
706 	.set_coalesce = enetc_set_coalesce,
707 	.get_link_ksettings = phy_ethtool_get_link_ksettings,
708 	.set_link_ksettings = phy_ethtool_set_link_ksettings,
709 	.get_link = ethtool_op_get_link,
710 	.get_ts_info = enetc_get_ts_info,
711 	.get_wol = enetc_get_wol,
712 	.set_wol = enetc_set_wol,
713 };
714 
715 static const struct ethtool_ops enetc_vf_ethtool_ops = {
716 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
717 				     ETHTOOL_COALESCE_MAX_FRAMES |
718 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
719 	.get_regs_len = enetc_get_reglen,
720 	.get_regs = enetc_get_regs,
721 	.get_sset_count = enetc_get_sset_count,
722 	.get_strings = enetc_get_strings,
723 	.get_ethtool_stats = enetc_get_ethtool_stats,
724 	.get_rxnfc = enetc_get_rxnfc,
725 	.set_rxnfc = enetc_set_rxnfc,
726 	.get_rxfh_indir_size = enetc_get_rxfh_indir_size,
727 	.get_rxfh = enetc_get_rxfh,
728 	.set_rxfh = enetc_set_rxfh,
729 	.get_ringparam = enetc_get_ringparam,
730 	.get_coalesce = enetc_get_coalesce,
731 	.set_coalesce = enetc_set_coalesce,
732 	.get_link = ethtool_op_get_link,
733 	.get_ts_info = enetc_get_ts_info,
734 };
735 
736 void enetc_set_ethtool_ops(struct net_device *ndev)
737 {
738 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
739 
740 	if (enetc_si_is_pf(priv->si))
741 		ndev->ethtool_ops = &enetc_pf_ethtool_ops;
742 	else
743 		ndev->ethtool_ops = &enetc_vf_ethtool_ops;
744 }
745