xref: /linux/drivers/net/ethernet/freescale/dpaa2/dpaa2-ethtool.c (revision 3313206827678f6f036eca601a51f6c4524b559a)
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright 2016-2022 NXP
4  */
5 
6 #include <linux/net_tstamp.h>
7 #include <linux/nospec.h>
8 
9 #include "dpni.h"	/* DPNI_LINK_OPT_* */
10 #include "dpaa2-eth.h"
11 
12 /* To be kept in sync with DPNI statistics */
13 static char dpaa2_ethtool_stats[][ETH_GSTRING_LEN] = {
14 	"[hw] rx frames",
15 	"[hw] rx bytes",
16 	"[hw] rx mcast frames",
17 	"[hw] rx mcast bytes",
18 	"[hw] rx bcast frames",
19 	"[hw] rx bcast bytes",
20 	"[hw] tx frames",
21 	"[hw] tx bytes",
22 	"[hw] tx mcast frames",
23 	"[hw] tx mcast bytes",
24 	"[hw] tx bcast frames",
25 	"[hw] tx bcast bytes",
26 	"[hw] rx filtered frames",
27 	"[hw] rx discarded frames",
28 	"[hw] rx nobuffer discards",
29 	"[hw] tx discarded frames",
30 	"[hw] tx confirmed frames",
31 	"[hw] tx dequeued bytes",
32 	"[hw] tx dequeued frames",
33 	"[hw] tx rejected bytes",
34 	"[hw] tx rejected frames",
35 	"[hw] tx pending frames",
36 };
37 
38 #define DPAA2_ETH_NUM_STATS	ARRAY_SIZE(dpaa2_ethtool_stats)
39 
40 static char dpaa2_ethtool_extras[][ETH_GSTRING_LEN] = {
41 	/* per-cpu stats */
42 	"[drv] tx conf frames",
43 	"[drv] tx conf bytes",
44 	"[drv] tx sg frames",
45 	"[drv] tx sg bytes",
46 	"[drv] tx tso frames",
47 	"[drv] tx tso bytes",
48 	"[drv] rx sg frames",
49 	"[drv] rx sg bytes",
50 	"[drv] tx converted sg frames",
51 	"[drv] tx converted sg bytes",
52 	"[drv] enqueue portal busy",
53 	/* Channel stats */
54 	"[drv] dequeue portal busy",
55 	"[drv] channel pull errors",
56 	"[drv] cdan",
57 	"[drv] xdp drop",
58 	"[drv] xdp tx",
59 	"[drv] xdp tx errors",
60 	"[drv] xdp redirect",
61 	/* FQ stats */
62 	"[qbman] rx pending frames",
63 	"[qbman] rx pending bytes",
64 	"[qbman] tx conf pending frames",
65 	"[qbman] tx conf pending bytes",
66 	"[qbman] buffer count",
67 };
68 
69 #define DPAA2_ETH_NUM_EXTRA_STATS	ARRAY_SIZE(dpaa2_ethtool_extras)
70 
71 static void dpaa2_eth_get_drvinfo(struct net_device *net_dev,
72 				  struct ethtool_drvinfo *drvinfo)
73 {
74 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
75 
76 	strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
77 
78 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
79 		 "%u.%u", priv->dpni_ver_major, priv->dpni_ver_minor);
80 
81 	strscpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
82 		sizeof(drvinfo->bus_info));
83 }
84 
85 static int dpaa2_eth_nway_reset(struct net_device *net_dev)
86 {
87 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
88 
89 	if (dpaa2_eth_is_type_phy(priv))
90 		return phylink_ethtool_nway_reset(priv->mac->phylink);
91 
92 	return -EOPNOTSUPP;
93 }
94 
95 static int
96 dpaa2_eth_get_link_ksettings(struct net_device *net_dev,
97 			     struct ethtool_link_ksettings *link_settings)
98 {
99 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
100 
101 	if (dpaa2_eth_is_type_phy(priv))
102 		return phylink_ethtool_ksettings_get(priv->mac->phylink,
103 						     link_settings);
104 
105 	link_settings->base.autoneg = AUTONEG_DISABLE;
106 	if (!(priv->link_state.options & DPNI_LINK_OPT_HALF_DUPLEX))
107 		link_settings->base.duplex = DUPLEX_FULL;
108 	link_settings->base.speed = priv->link_state.rate;
109 
110 	return 0;
111 }
112 
113 static int
114 dpaa2_eth_set_link_ksettings(struct net_device *net_dev,
115 			     const struct ethtool_link_ksettings *link_settings)
116 {
117 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
118 
119 	if (!dpaa2_eth_is_type_phy(priv))
120 		return -ENOTSUPP;
121 
122 	return phylink_ethtool_ksettings_set(priv->mac->phylink, link_settings);
123 }
124 
125 static void dpaa2_eth_get_pauseparam(struct net_device *net_dev,
126 				     struct ethtool_pauseparam *pause)
127 {
128 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
129 	u64 link_options = priv->link_state.options;
130 
131 	if (dpaa2_eth_is_type_phy(priv)) {
132 		phylink_ethtool_get_pauseparam(priv->mac->phylink, pause);
133 		return;
134 	}
135 
136 	pause->rx_pause = dpaa2_eth_rx_pause_enabled(link_options);
137 	pause->tx_pause = dpaa2_eth_tx_pause_enabled(link_options);
138 	pause->autoneg = AUTONEG_DISABLE;
139 }
140 
141 static int dpaa2_eth_set_pauseparam(struct net_device *net_dev,
142 				    struct ethtool_pauseparam *pause)
143 {
144 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
145 	struct dpni_link_cfg cfg = {0};
146 	int err;
147 
148 	if (!dpaa2_eth_has_pause_support(priv)) {
149 		netdev_info(net_dev, "No pause frame support for DPNI version < %d.%d\n",
150 			    DPNI_PAUSE_VER_MAJOR, DPNI_PAUSE_VER_MINOR);
151 		return -EOPNOTSUPP;
152 	}
153 
154 	if (dpaa2_eth_is_type_phy(priv))
155 		return phylink_ethtool_set_pauseparam(priv->mac->phylink,
156 						      pause);
157 	if (pause->autoneg)
158 		return -EOPNOTSUPP;
159 
160 	cfg.rate = priv->link_state.rate;
161 	cfg.options = priv->link_state.options;
162 	if (pause->rx_pause)
163 		cfg.options |= DPNI_LINK_OPT_PAUSE;
164 	else
165 		cfg.options &= ~DPNI_LINK_OPT_PAUSE;
166 	if (!!pause->rx_pause ^ !!pause->tx_pause)
167 		cfg.options |= DPNI_LINK_OPT_ASYM_PAUSE;
168 	else
169 		cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
170 
171 	if (cfg.options == priv->link_state.options)
172 		return 0;
173 
174 	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &cfg);
175 	if (err) {
176 		netdev_err(net_dev, "dpni_set_link_state failed\n");
177 		return err;
178 	}
179 
180 	priv->link_state.options = cfg.options;
181 
182 	return 0;
183 }
184 
185 static void dpaa2_eth_get_strings(struct net_device *netdev, u32 stringset,
186 				  u8 *data)
187 {
188 	struct dpaa2_eth_priv *priv = netdev_priv(netdev);
189 	u8 *p = data;
190 	int i;
191 
192 	switch (stringset) {
193 	case ETH_SS_STATS:
194 		for (i = 0; i < DPAA2_ETH_NUM_STATS; i++) {
195 			strscpy(p, dpaa2_ethtool_stats[i], ETH_GSTRING_LEN);
196 			p += ETH_GSTRING_LEN;
197 		}
198 		for (i = 0; i < DPAA2_ETH_NUM_EXTRA_STATS; i++) {
199 			strscpy(p, dpaa2_ethtool_extras[i], ETH_GSTRING_LEN);
200 			p += ETH_GSTRING_LEN;
201 		}
202 		if (dpaa2_eth_has_mac(priv))
203 			dpaa2_mac_get_strings(p);
204 		break;
205 	}
206 }
207 
208 static int dpaa2_eth_get_sset_count(struct net_device *net_dev, int sset)
209 {
210 	int num_ss_stats = DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS;
211 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
212 
213 	switch (sset) {
214 	case ETH_SS_STATS: /* ethtool_get_stats(), ethtool_get_drvinfo() */
215 		if (dpaa2_eth_has_mac(priv))
216 			num_ss_stats += dpaa2_mac_get_sset_count();
217 		return num_ss_stats;
218 	default:
219 		return -EOPNOTSUPP;
220 	}
221 }
222 
223 /** Fill in hardware counters, as returned by MC.
224  */
225 static void dpaa2_eth_get_ethtool_stats(struct net_device *net_dev,
226 					struct ethtool_stats *stats,
227 					u64 *data)
228 {
229 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
230 	union dpni_statistics dpni_stats;
231 	int dpni_stats_page_size[DPNI_STATISTICS_CNT] = {
232 		sizeof(dpni_stats.page_0),
233 		sizeof(dpni_stats.page_1),
234 		sizeof(dpni_stats.page_2),
235 		sizeof(dpni_stats.page_3),
236 		sizeof(dpni_stats.page_4),
237 		sizeof(dpni_stats.page_5),
238 		sizeof(dpni_stats.page_6),
239 	};
240 	u32 fcnt_rx_total = 0, fcnt_tx_total = 0;
241 	u32 bcnt_rx_total = 0, bcnt_tx_total = 0;
242 	struct dpaa2_eth_ch_stats *ch_stats;
243 	struct dpaa2_eth_drv_stats *extras;
244 	int j, k, err, num_cnt, i = 0;
245 	u32 fcnt, bcnt;
246 	u32 buf_cnt;
247 
248 	memset(data, 0,
249 	       sizeof(u64) * (DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS));
250 
251 	/* Print standard counters, from DPNI statistics */
252 	for (j = 0; j <= 6; j++) {
253 		/* We're not interested in pages 4 & 5 for now */
254 		if (j == 4 || j == 5)
255 			continue;
256 		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token,
257 					  j, &dpni_stats);
258 		if (err == -EINVAL)
259 			/* Older firmware versions don't support all pages */
260 			memset(&dpni_stats, 0, sizeof(dpni_stats));
261 		else if (err)
262 			netdev_warn(net_dev, "dpni_get_stats(%d) failed\n", j);
263 
264 		num_cnt = dpni_stats_page_size[j] / sizeof(u64);
265 		for (k = 0; k < num_cnt; k++)
266 			*(data + i++) = dpni_stats.raw.counter[k];
267 	}
268 
269 	/* Print per-cpu extra stats */
270 	for_each_online_cpu(k) {
271 		extras = per_cpu_ptr(priv->percpu_extras, k);
272 		for (j = 0; j < sizeof(*extras) / sizeof(__u64); j++)
273 			*((__u64 *)data + i + j) += *((__u64 *)extras + j);
274 	}
275 	i += j;
276 
277 	/* Per-channel stats */
278 	for (k = 0; k < priv->num_channels; k++) {
279 		ch_stats = &priv->channel[k]->stats;
280 		for (j = 0; j < DPAA2_ETH_CH_STATS; j++)
281 			*((__u64 *)data + i + j) += *((__u64 *)ch_stats + j);
282 	}
283 	i += j;
284 
285 	for (j = 0; j < priv->num_fqs; j++) {
286 		/* Print FQ instantaneous counts */
287 		err = dpaa2_io_query_fq_count(NULL, priv->fq[j].fqid,
288 					      &fcnt, &bcnt);
289 		if (err) {
290 			netdev_warn(net_dev, "FQ query error %d", err);
291 			return;
292 		}
293 
294 		if (priv->fq[j].type == DPAA2_TX_CONF_FQ) {
295 			fcnt_tx_total += fcnt;
296 			bcnt_tx_total += bcnt;
297 		} else {
298 			fcnt_rx_total += fcnt;
299 			bcnt_rx_total += bcnt;
300 		}
301 	}
302 
303 	*(data + i++) = fcnt_rx_total;
304 	*(data + i++) = bcnt_rx_total;
305 	*(data + i++) = fcnt_tx_total;
306 	*(data + i++) = bcnt_tx_total;
307 
308 	err = dpaa2_io_query_bp_count(NULL, priv->bpid, &buf_cnt);
309 	if (err) {
310 		netdev_warn(net_dev, "Buffer count query error %d\n", err);
311 		return;
312 	}
313 	*(data + i++) = buf_cnt;
314 
315 	if (dpaa2_eth_has_mac(priv))
316 		dpaa2_mac_get_ethtool_stats(priv->mac, data + i);
317 }
318 
319 static int dpaa2_eth_prep_eth_rule(struct ethhdr *eth_value, struct ethhdr *eth_mask,
320 				   void *key, void *mask, u64 *fields)
321 {
322 	int off;
323 
324 	if (eth_mask->h_proto) {
325 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
326 		*(__be16 *)(key + off) = eth_value->h_proto;
327 		*(__be16 *)(mask + off) = eth_mask->h_proto;
328 		*fields |= DPAA2_ETH_DIST_ETHTYPE;
329 	}
330 
331 	if (!is_zero_ether_addr(eth_mask->h_source)) {
332 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_SA);
333 		ether_addr_copy(key + off, eth_value->h_source);
334 		ether_addr_copy(mask + off, eth_mask->h_source);
335 		*fields |= DPAA2_ETH_DIST_ETHSRC;
336 	}
337 
338 	if (!is_zero_ether_addr(eth_mask->h_dest)) {
339 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
340 		ether_addr_copy(key + off, eth_value->h_dest);
341 		ether_addr_copy(mask + off, eth_mask->h_dest);
342 		*fields |= DPAA2_ETH_DIST_ETHDST;
343 	}
344 
345 	return 0;
346 }
347 
348 static int dpaa2_eth_prep_uip_rule(struct ethtool_usrip4_spec *uip_value,
349 				   struct ethtool_usrip4_spec *uip_mask,
350 				   void *key, void *mask, u64 *fields)
351 {
352 	int off;
353 	u32 tmp_value, tmp_mask;
354 
355 	if (uip_mask->tos || uip_mask->ip_ver)
356 		return -EOPNOTSUPP;
357 
358 	if (uip_mask->ip4src) {
359 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
360 		*(__be32 *)(key + off) = uip_value->ip4src;
361 		*(__be32 *)(mask + off) = uip_mask->ip4src;
362 		*fields |= DPAA2_ETH_DIST_IPSRC;
363 	}
364 
365 	if (uip_mask->ip4dst) {
366 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
367 		*(__be32 *)(key + off) = uip_value->ip4dst;
368 		*(__be32 *)(mask + off) = uip_mask->ip4dst;
369 		*fields |= DPAA2_ETH_DIST_IPDST;
370 	}
371 
372 	if (uip_mask->proto) {
373 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
374 		*(u8 *)(key + off) = uip_value->proto;
375 		*(u8 *)(mask + off) = uip_mask->proto;
376 		*fields |= DPAA2_ETH_DIST_IPPROTO;
377 	}
378 
379 	if (uip_mask->l4_4_bytes) {
380 		tmp_value = be32_to_cpu(uip_value->l4_4_bytes);
381 		tmp_mask = be32_to_cpu(uip_mask->l4_4_bytes);
382 
383 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
384 		*(__be16 *)(key + off) = htons(tmp_value >> 16);
385 		*(__be16 *)(mask + off) = htons(tmp_mask >> 16);
386 		*fields |= DPAA2_ETH_DIST_L4SRC;
387 
388 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
389 		*(__be16 *)(key + off) = htons(tmp_value & 0xFFFF);
390 		*(__be16 *)(mask + off) = htons(tmp_mask & 0xFFFF);
391 		*fields |= DPAA2_ETH_DIST_L4DST;
392 	}
393 
394 	/* Only apply the rule for IPv4 frames */
395 	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
396 	*(__be16 *)(key + off) = htons(ETH_P_IP);
397 	*(__be16 *)(mask + off) = htons(0xFFFF);
398 	*fields |= DPAA2_ETH_DIST_ETHTYPE;
399 
400 	return 0;
401 }
402 
403 static int dpaa2_eth_prep_l4_rule(struct ethtool_tcpip4_spec *l4_value,
404 				  struct ethtool_tcpip4_spec *l4_mask,
405 				  void *key, void *mask, u8 l4_proto, u64 *fields)
406 {
407 	int off;
408 
409 	if (l4_mask->tos)
410 		return -EOPNOTSUPP;
411 
412 	if (l4_mask->ip4src) {
413 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
414 		*(__be32 *)(key + off) = l4_value->ip4src;
415 		*(__be32 *)(mask + off) = l4_mask->ip4src;
416 		*fields |= DPAA2_ETH_DIST_IPSRC;
417 	}
418 
419 	if (l4_mask->ip4dst) {
420 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
421 		*(__be32 *)(key + off) = l4_value->ip4dst;
422 		*(__be32 *)(mask + off) = l4_mask->ip4dst;
423 		*fields |= DPAA2_ETH_DIST_IPDST;
424 	}
425 
426 	if (l4_mask->psrc) {
427 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
428 		*(__be16 *)(key + off) = l4_value->psrc;
429 		*(__be16 *)(mask + off) = l4_mask->psrc;
430 		*fields |= DPAA2_ETH_DIST_L4SRC;
431 	}
432 
433 	if (l4_mask->pdst) {
434 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
435 		*(__be16 *)(key + off) = l4_value->pdst;
436 		*(__be16 *)(mask + off) = l4_mask->pdst;
437 		*fields |= DPAA2_ETH_DIST_L4DST;
438 	}
439 
440 	/* Only apply the rule for IPv4 frames with the specified L4 proto */
441 	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
442 	*(__be16 *)(key + off) = htons(ETH_P_IP);
443 	*(__be16 *)(mask + off) = htons(0xFFFF);
444 	*fields |= DPAA2_ETH_DIST_ETHTYPE;
445 
446 	off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
447 	*(u8 *)(key + off) = l4_proto;
448 	*(u8 *)(mask + off) = 0xFF;
449 	*fields |= DPAA2_ETH_DIST_IPPROTO;
450 
451 	return 0;
452 }
453 
454 static int dpaa2_eth_prep_ext_rule(struct ethtool_flow_ext *ext_value,
455 				   struct ethtool_flow_ext *ext_mask,
456 				   void *key, void *mask, u64 *fields)
457 {
458 	int off;
459 
460 	if (ext_mask->vlan_etype)
461 		return -EOPNOTSUPP;
462 
463 	if (ext_mask->vlan_tci) {
464 		off = dpaa2_eth_cls_fld_off(NET_PROT_VLAN, NH_FLD_VLAN_TCI);
465 		*(__be16 *)(key + off) = ext_value->vlan_tci;
466 		*(__be16 *)(mask + off) = ext_mask->vlan_tci;
467 		*fields |= DPAA2_ETH_DIST_VLAN;
468 	}
469 
470 	return 0;
471 }
472 
473 static int dpaa2_eth_prep_mac_ext_rule(struct ethtool_flow_ext *ext_value,
474 				       struct ethtool_flow_ext *ext_mask,
475 				       void *key, void *mask, u64 *fields)
476 {
477 	int off;
478 
479 	if (!is_zero_ether_addr(ext_mask->h_dest)) {
480 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
481 		ether_addr_copy(key + off, ext_value->h_dest);
482 		ether_addr_copy(mask + off, ext_mask->h_dest);
483 		*fields |= DPAA2_ETH_DIST_ETHDST;
484 	}
485 
486 	return 0;
487 }
488 
489 static int dpaa2_eth_prep_cls_rule(struct ethtool_rx_flow_spec *fs, void *key,
490 				   void *mask, u64 *fields)
491 {
492 	int err;
493 
494 	switch (fs->flow_type & 0xFF) {
495 	case ETHER_FLOW:
496 		err = dpaa2_eth_prep_eth_rule(&fs->h_u.ether_spec, &fs->m_u.ether_spec,
497 					      key, mask, fields);
498 		break;
499 	case IP_USER_FLOW:
500 		err = dpaa2_eth_prep_uip_rule(&fs->h_u.usr_ip4_spec,
501 					      &fs->m_u.usr_ip4_spec, key, mask, fields);
502 		break;
503 	case TCP_V4_FLOW:
504 		err = dpaa2_eth_prep_l4_rule(&fs->h_u.tcp_ip4_spec, &fs->m_u.tcp_ip4_spec,
505 					     key, mask, IPPROTO_TCP, fields);
506 		break;
507 	case UDP_V4_FLOW:
508 		err = dpaa2_eth_prep_l4_rule(&fs->h_u.udp_ip4_spec, &fs->m_u.udp_ip4_spec,
509 					     key, mask, IPPROTO_UDP, fields);
510 		break;
511 	case SCTP_V4_FLOW:
512 		err = dpaa2_eth_prep_l4_rule(&fs->h_u.sctp_ip4_spec,
513 					     &fs->m_u.sctp_ip4_spec, key, mask,
514 					     IPPROTO_SCTP, fields);
515 		break;
516 	default:
517 		return -EOPNOTSUPP;
518 	}
519 
520 	if (err)
521 		return err;
522 
523 	if (fs->flow_type & FLOW_EXT) {
524 		err = dpaa2_eth_prep_ext_rule(&fs->h_ext, &fs->m_ext, key, mask, fields);
525 		if (err)
526 			return err;
527 	}
528 
529 	if (fs->flow_type & FLOW_MAC_EXT) {
530 		err = dpaa2_eth_prep_mac_ext_rule(&fs->h_ext, &fs->m_ext, key,
531 						  mask, fields);
532 		if (err)
533 			return err;
534 	}
535 
536 	return 0;
537 }
538 
539 static int dpaa2_eth_do_cls_rule(struct net_device *net_dev,
540 				 struct ethtool_rx_flow_spec *fs,
541 				 bool add)
542 {
543 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
544 	struct device *dev = net_dev->dev.parent;
545 	struct dpni_rule_cfg rule_cfg = { 0 };
546 	struct dpni_fs_action_cfg fs_act = { 0 };
547 	dma_addr_t key_iova;
548 	u64 fields = 0;
549 	void *key_buf;
550 	int i, err;
551 
552 	if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
553 	    fs->ring_cookie >= dpaa2_eth_queue_count(priv))
554 		return -EINVAL;
555 
556 	rule_cfg.key_size = dpaa2_eth_cls_key_size(DPAA2_ETH_DIST_ALL);
557 
558 	/* allocate twice the key size, for the actual key and for mask */
559 	key_buf = kzalloc(rule_cfg.key_size * 2, GFP_KERNEL);
560 	if (!key_buf)
561 		return -ENOMEM;
562 
563 	/* Fill the key and mask memory areas */
564 	err = dpaa2_eth_prep_cls_rule(fs, key_buf, key_buf + rule_cfg.key_size, &fields);
565 	if (err)
566 		goto free_mem;
567 
568 	if (!dpaa2_eth_fs_mask_enabled(priv)) {
569 		/* Masking allows us to configure a maximal key during init and
570 		 * use it for all flow steering rules. Without it, we include
571 		 * in the key only the fields actually used, so we need to
572 		 * extract the others from the final key buffer.
573 		 *
574 		 * Program the FS key if needed, or return error if previously
575 		 * set key can't be used for the current rule. User needs to
576 		 * delete existing rules in this case to allow for the new one.
577 		 */
578 		if (!priv->rx_cls_fields) {
579 			err = dpaa2_eth_set_cls(net_dev, fields);
580 			if (err)
581 				goto free_mem;
582 
583 			priv->rx_cls_fields = fields;
584 		} else if (priv->rx_cls_fields != fields) {
585 			netdev_err(net_dev, "No support for multiple FS keys, need to delete existing rules\n");
586 			err = -EOPNOTSUPP;
587 			goto free_mem;
588 		}
589 
590 		dpaa2_eth_cls_trim_rule(key_buf, fields);
591 		rule_cfg.key_size = dpaa2_eth_cls_key_size(fields);
592 	}
593 
594 	key_iova = dma_map_single(dev, key_buf, rule_cfg.key_size * 2,
595 				  DMA_TO_DEVICE);
596 	if (dma_mapping_error(dev, key_iova)) {
597 		err = -ENOMEM;
598 		goto free_mem;
599 	}
600 
601 	rule_cfg.key_iova = key_iova;
602 	if (dpaa2_eth_fs_mask_enabled(priv))
603 		rule_cfg.mask_iova = key_iova + rule_cfg.key_size;
604 
605 	if (add) {
606 		if (fs->ring_cookie == RX_CLS_FLOW_DISC)
607 			fs_act.options |= DPNI_FS_OPT_DISCARD;
608 		else
609 			fs_act.flow_id = fs->ring_cookie;
610 	}
611 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
612 		if (add)
613 			err = dpni_add_fs_entry(priv->mc_io, 0, priv->mc_token,
614 						i, fs->location, &rule_cfg,
615 						&fs_act);
616 		else
617 			err = dpni_remove_fs_entry(priv->mc_io, 0,
618 						   priv->mc_token, i,
619 						   &rule_cfg);
620 		if (err || priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
621 			break;
622 	}
623 
624 	dma_unmap_single(dev, key_iova, rule_cfg.key_size * 2, DMA_TO_DEVICE);
625 
626 free_mem:
627 	kfree(key_buf);
628 
629 	return err;
630 }
631 
632 static int dpaa2_eth_num_cls_rules(struct dpaa2_eth_priv *priv)
633 {
634 	int i, rules = 0;
635 
636 	for (i = 0; i < dpaa2_eth_fs_count(priv); i++)
637 		if (priv->cls_rules[i].in_use)
638 			rules++;
639 
640 	return rules;
641 }
642 
643 static int dpaa2_eth_update_cls_rule(struct net_device *net_dev,
644 				     struct ethtool_rx_flow_spec *new_fs,
645 				     unsigned int location)
646 {
647 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
648 	struct dpaa2_eth_cls_rule *rule;
649 	int err = -EINVAL;
650 
651 	if (!priv->rx_cls_enabled)
652 		return -EOPNOTSUPP;
653 
654 	if (location >= dpaa2_eth_fs_count(priv))
655 		return -EINVAL;
656 
657 	rule = &priv->cls_rules[location];
658 
659 	/* If a rule is present at the specified location, delete it. */
660 	if (rule->in_use) {
661 		err = dpaa2_eth_do_cls_rule(net_dev, &rule->fs, false);
662 		if (err)
663 			return err;
664 
665 		rule->in_use = 0;
666 
667 		if (!dpaa2_eth_fs_mask_enabled(priv) &&
668 		    !dpaa2_eth_num_cls_rules(priv))
669 			priv->rx_cls_fields = 0;
670 	}
671 
672 	/* If no new entry to add, return here */
673 	if (!new_fs)
674 		return err;
675 
676 	err = dpaa2_eth_do_cls_rule(net_dev, new_fs, true);
677 	if (err)
678 		return err;
679 
680 	rule->in_use = 1;
681 	rule->fs = *new_fs;
682 
683 	return 0;
684 }
685 
686 static int dpaa2_eth_get_rxnfc(struct net_device *net_dev,
687 			       struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
688 {
689 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
690 	int max_rules = dpaa2_eth_fs_count(priv);
691 	int i, j = 0;
692 
693 	switch (rxnfc->cmd) {
694 	case ETHTOOL_GRXFH:
695 		/* we purposely ignore cmd->flow_type for now, because the
696 		 * classifier only supports a single set of fields for all
697 		 * protocols
698 		 */
699 		rxnfc->data = priv->rx_hash_fields;
700 		break;
701 	case ETHTOOL_GRXRINGS:
702 		rxnfc->data = dpaa2_eth_queue_count(priv);
703 		break;
704 	case ETHTOOL_GRXCLSRLCNT:
705 		rxnfc->rule_cnt = 0;
706 		rxnfc->rule_cnt = dpaa2_eth_num_cls_rules(priv);
707 		rxnfc->data = max_rules;
708 		break;
709 	case ETHTOOL_GRXCLSRULE:
710 		if (rxnfc->fs.location >= max_rules)
711 			return -EINVAL;
712 		rxnfc->fs.location = array_index_nospec(rxnfc->fs.location,
713 							max_rules);
714 		if (!priv->cls_rules[rxnfc->fs.location].in_use)
715 			return -EINVAL;
716 		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
717 		break;
718 	case ETHTOOL_GRXCLSRLALL:
719 		for (i = 0; i < max_rules; i++) {
720 			if (!priv->cls_rules[i].in_use)
721 				continue;
722 			if (j == rxnfc->rule_cnt)
723 				return -EMSGSIZE;
724 			rule_locs[j++] = i;
725 		}
726 		rxnfc->rule_cnt = j;
727 		rxnfc->data = max_rules;
728 		break;
729 	default:
730 		return -EOPNOTSUPP;
731 	}
732 
733 	return 0;
734 }
735 
736 static int dpaa2_eth_set_rxnfc(struct net_device *net_dev,
737 			       struct ethtool_rxnfc *rxnfc)
738 {
739 	int err = 0;
740 
741 	switch (rxnfc->cmd) {
742 	case ETHTOOL_SRXFH:
743 		if ((rxnfc->data & DPAA2_RXH_SUPPORTED) != rxnfc->data)
744 			return -EOPNOTSUPP;
745 		err = dpaa2_eth_set_hash(net_dev, rxnfc->data);
746 		break;
747 	case ETHTOOL_SRXCLSRLINS:
748 		err = dpaa2_eth_update_cls_rule(net_dev, &rxnfc->fs, rxnfc->fs.location);
749 		break;
750 	case ETHTOOL_SRXCLSRLDEL:
751 		err = dpaa2_eth_update_cls_rule(net_dev, NULL, rxnfc->fs.location);
752 		break;
753 	default:
754 		err = -EOPNOTSUPP;
755 	}
756 
757 	return err;
758 }
759 
760 int dpaa2_phc_index = -1;
761 EXPORT_SYMBOL(dpaa2_phc_index);
762 
763 static int dpaa2_eth_get_ts_info(struct net_device *dev,
764 				 struct ethtool_ts_info *info)
765 {
766 	if (!dpaa2_ptp)
767 		return ethtool_op_get_ts_info(dev, info);
768 
769 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
770 				SOF_TIMESTAMPING_RX_HARDWARE |
771 				SOF_TIMESTAMPING_RAW_HARDWARE;
772 
773 	info->phc_index = dpaa2_phc_index;
774 
775 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
776 			 (1 << HWTSTAMP_TX_ON) |
777 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
778 
779 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
780 			   (1 << HWTSTAMP_FILTER_ALL);
781 	return 0;
782 }
783 
784 static int dpaa2_eth_get_tunable(struct net_device *net_dev,
785 				 const struct ethtool_tunable *tuna,
786 				 void *data)
787 {
788 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
789 	int err = 0;
790 
791 	switch (tuna->id) {
792 	case ETHTOOL_RX_COPYBREAK:
793 		*(u32 *)data = priv->rx_copybreak;
794 		break;
795 	default:
796 		err = -EOPNOTSUPP;
797 		break;
798 	}
799 
800 	return err;
801 }
802 
803 static int dpaa2_eth_set_tunable(struct net_device *net_dev,
804 				 const struct ethtool_tunable *tuna,
805 				 const void *data)
806 {
807 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
808 	int err = 0;
809 
810 	switch (tuna->id) {
811 	case ETHTOOL_RX_COPYBREAK:
812 		priv->rx_copybreak = *(u32 *)data;
813 		break;
814 	default:
815 		err = -EOPNOTSUPP;
816 		break;
817 	}
818 
819 	return err;
820 }
821 
822 static int dpaa2_eth_get_coalesce(struct net_device *dev,
823 				  struct ethtool_coalesce *ic,
824 				  struct kernel_ethtool_coalesce *kernel_coal,
825 				  struct netlink_ext_ack *extack)
826 {
827 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
828 	struct dpaa2_io *dpio = priv->channel[0]->dpio;
829 
830 	dpaa2_io_get_irq_coalescing(dpio, &ic->rx_coalesce_usecs);
831 	ic->use_adaptive_rx_coalesce = dpaa2_io_get_adaptive_coalescing(dpio);
832 
833 	return 0;
834 }
835 
836 static int dpaa2_eth_set_coalesce(struct net_device *dev,
837 				  struct ethtool_coalesce *ic,
838 				  struct kernel_ethtool_coalesce *kernel_coal,
839 				  struct netlink_ext_ack *extack)
840 {
841 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
842 	struct dpaa2_io *dpio;
843 	int prev_adaptive;
844 	u32 prev_rx_usecs;
845 	int i, j, err;
846 
847 	/* Keep track of the previous value, just in case we fail */
848 	dpio = priv->channel[0]->dpio;
849 	dpaa2_io_get_irq_coalescing(dpio, &prev_rx_usecs);
850 	prev_adaptive = dpaa2_io_get_adaptive_coalescing(dpio);
851 
852 	/* Setup new value for rx coalescing */
853 	for (i = 0; i < priv->num_channels; i++) {
854 		dpio = priv->channel[i]->dpio;
855 
856 		dpaa2_io_set_adaptive_coalescing(dpio,
857 						 ic->use_adaptive_rx_coalesce);
858 		err = dpaa2_io_set_irq_coalescing(dpio, ic->rx_coalesce_usecs);
859 		if (err)
860 			goto restore_rx_usecs;
861 	}
862 
863 	return 0;
864 
865 restore_rx_usecs:
866 	for (j = 0; j < i; j++) {
867 		dpio = priv->channel[j]->dpio;
868 
869 		dpaa2_io_set_irq_coalescing(dpio, prev_rx_usecs);
870 		dpaa2_io_set_adaptive_coalescing(dpio, prev_adaptive);
871 	}
872 
873 	return err;
874 }
875 
876 static void dpaa2_eth_get_channels(struct net_device *net_dev,
877 				   struct ethtool_channels *channels)
878 {
879 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
880 	int queue_count = dpaa2_eth_queue_count(priv);
881 
882 	channels->max_rx = queue_count;
883 	channels->max_tx = queue_count;
884 	channels->rx_count = queue_count;
885 	channels->tx_count = queue_count;
886 
887 	/* Tx confirmation and Rx error */
888 	channels->max_other = queue_count + 1;
889 	channels->max_combined = channels->max_rx +
890 				 channels->max_tx +
891 				 channels->max_other;
892 	/* Tx conf and Rx err */
893 	channels->other_count = queue_count + 1;
894 	channels->combined_count = channels->rx_count +
895 				   channels->tx_count +
896 				   channels->other_count;
897 }
898 
899 const struct ethtool_ops dpaa2_ethtool_ops = {
900 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
901 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
902 	.get_drvinfo = dpaa2_eth_get_drvinfo,
903 	.nway_reset = dpaa2_eth_nway_reset,
904 	.get_link = ethtool_op_get_link,
905 	.get_link_ksettings = dpaa2_eth_get_link_ksettings,
906 	.set_link_ksettings = dpaa2_eth_set_link_ksettings,
907 	.get_pauseparam = dpaa2_eth_get_pauseparam,
908 	.set_pauseparam = dpaa2_eth_set_pauseparam,
909 	.get_sset_count = dpaa2_eth_get_sset_count,
910 	.get_ethtool_stats = dpaa2_eth_get_ethtool_stats,
911 	.get_strings = dpaa2_eth_get_strings,
912 	.get_rxnfc = dpaa2_eth_get_rxnfc,
913 	.set_rxnfc = dpaa2_eth_set_rxnfc,
914 	.get_ts_info = dpaa2_eth_get_ts_info,
915 	.get_tunable = dpaa2_eth_get_tunable,
916 	.set_tunable = dpaa2_eth_set_tunable,
917 	.get_coalesce = dpaa2_eth_get_coalesce,
918 	.set_coalesce = dpaa2_eth_set_coalesce,
919 	.get_channels = dpaa2_eth_get_channels,
920 };
921