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