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