1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright 2016 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 };
32 
33 #define DPAA2_ETH_NUM_STATS	ARRAY_SIZE(dpaa2_ethtool_stats)
34 
35 static char dpaa2_ethtool_extras[][ETH_GSTRING_LEN] = {
36 	/* per-cpu stats */
37 	"[drv] tx conf frames",
38 	"[drv] tx conf bytes",
39 	"[drv] tx sg frames",
40 	"[drv] tx sg bytes",
41 	"[drv] tx realloc frames",
42 	"[drv] rx sg frames",
43 	"[drv] rx sg bytes",
44 	"[drv] enqueue portal busy",
45 	/* Channel stats */
46 	"[drv] dequeue portal busy",
47 	"[drv] channel pull errors",
48 	"[drv] cdan",
49 	"[drv] xdp drop",
50 	"[drv] xdp tx",
51 	"[drv] xdp tx errors",
52 	"[drv] xdp redirect",
53 	/* FQ stats */
54 	"[qbman] rx pending frames",
55 	"[qbman] rx pending bytes",
56 	"[qbman] tx conf pending frames",
57 	"[qbman] tx conf pending bytes",
58 	"[qbman] buffer count",
59 };
60 
61 #define DPAA2_ETH_NUM_EXTRA_STATS	ARRAY_SIZE(dpaa2_ethtool_extras)
62 
63 static void dpaa2_eth_get_drvinfo(struct net_device *net_dev,
64 				  struct ethtool_drvinfo *drvinfo)
65 {
66 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
67 
68 	strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
69 
70 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
71 		 "%u.%u", priv->dpni_ver_major, priv->dpni_ver_minor);
72 
73 	strlcpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
74 		sizeof(drvinfo->bus_info));
75 }
76 
77 static int
78 dpaa2_eth_get_link_ksettings(struct net_device *net_dev,
79 			     struct ethtool_link_ksettings *link_settings)
80 {
81 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
82 
83 	link_settings->base.autoneg = AUTONEG_DISABLE;
84 	if (!(priv->link_state.options & DPNI_LINK_OPT_HALF_DUPLEX))
85 		link_settings->base.duplex = DUPLEX_FULL;
86 	link_settings->base.speed = priv->link_state.rate;
87 
88 	return 0;
89 }
90 
91 static void dpaa2_eth_get_pauseparam(struct net_device *net_dev,
92 				     struct ethtool_pauseparam *pause)
93 {
94 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
95 	u64 link_options = priv->link_state.options;
96 
97 	pause->rx_pause = !!(link_options & DPNI_LINK_OPT_PAUSE);
98 	pause->tx_pause = pause->rx_pause ^
99 			  !!(link_options & DPNI_LINK_OPT_ASYM_PAUSE);
100 	pause->autoneg = AUTONEG_DISABLE;
101 }
102 
103 static int dpaa2_eth_set_pauseparam(struct net_device *net_dev,
104 				    struct ethtool_pauseparam *pause)
105 {
106 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
107 	struct dpni_link_cfg cfg = {0};
108 	int err;
109 
110 	if (!dpaa2_eth_has_pause_support(priv)) {
111 		netdev_info(net_dev, "No pause frame support for DPNI version < %d.%d\n",
112 			    DPNI_PAUSE_VER_MAJOR, DPNI_PAUSE_VER_MINOR);
113 		return -EOPNOTSUPP;
114 	}
115 
116 	if (pause->autoneg)
117 		return -EOPNOTSUPP;
118 
119 	cfg.rate = priv->link_state.rate;
120 	cfg.options = priv->link_state.options;
121 	if (pause->rx_pause)
122 		cfg.options |= DPNI_LINK_OPT_PAUSE;
123 	else
124 		cfg.options &= ~DPNI_LINK_OPT_PAUSE;
125 	if (!!pause->rx_pause ^ !!pause->tx_pause)
126 		cfg.options |= DPNI_LINK_OPT_ASYM_PAUSE;
127 	else
128 		cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
129 
130 	if (cfg.options == priv->link_state.options)
131 		return 0;
132 
133 	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &cfg);
134 	if (err) {
135 		netdev_err(net_dev, "dpni_set_link_state failed\n");
136 		return err;
137 	}
138 
139 	priv->link_state.options = cfg.options;
140 
141 	return 0;
142 }
143 
144 static void dpaa2_eth_get_strings(struct net_device *netdev, u32 stringset,
145 				  u8 *data)
146 {
147 	u8 *p = data;
148 	int i;
149 
150 	switch (stringset) {
151 	case ETH_SS_STATS:
152 		for (i = 0; i < DPAA2_ETH_NUM_STATS; i++) {
153 			strlcpy(p, dpaa2_ethtool_stats[i], ETH_GSTRING_LEN);
154 			p += ETH_GSTRING_LEN;
155 		}
156 		for (i = 0; i < DPAA2_ETH_NUM_EXTRA_STATS; i++) {
157 			strlcpy(p, dpaa2_ethtool_extras[i], ETH_GSTRING_LEN);
158 			p += ETH_GSTRING_LEN;
159 		}
160 		break;
161 	}
162 }
163 
164 static int dpaa2_eth_get_sset_count(struct net_device *net_dev, int sset)
165 {
166 	switch (sset) {
167 	case ETH_SS_STATS: /* ethtool_get_stats(), ethtool_get_drvinfo() */
168 		return DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS;
169 	default:
170 		return -EOPNOTSUPP;
171 	}
172 }
173 
174 /** Fill in hardware counters, as returned by MC.
175  */
176 static void dpaa2_eth_get_ethtool_stats(struct net_device *net_dev,
177 					struct ethtool_stats *stats,
178 					u64 *data)
179 {
180 	int i = 0;
181 	int j, k, err;
182 	int num_cnt;
183 	union dpni_statistics dpni_stats;
184 	u32 fcnt, bcnt;
185 	u32 fcnt_rx_total = 0, fcnt_tx_total = 0;
186 	u32 bcnt_rx_total = 0, bcnt_tx_total = 0;
187 	u32 buf_cnt;
188 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
189 	struct dpaa2_eth_drv_stats *extras;
190 	struct dpaa2_eth_ch_stats *ch_stats;
191 
192 	memset(data, 0,
193 	       sizeof(u64) * (DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS));
194 
195 	/* Print standard counters, from DPNI statistics */
196 	for (j = 0; j <= 2; j++) {
197 		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token,
198 					  j, &dpni_stats);
199 		if (err != 0)
200 			netdev_warn(net_dev, "dpni_get_stats(%d) failed\n", j);
201 		switch (j) {
202 		case 0:
203 			num_cnt = sizeof(dpni_stats.page_0) / sizeof(u64);
204 			break;
205 		case 1:
206 			num_cnt = sizeof(dpni_stats.page_1) / sizeof(u64);
207 			break;
208 		case 2:
209 			num_cnt = sizeof(dpni_stats.page_2) / sizeof(u64);
210 			break;
211 		}
212 		for (k = 0; k < num_cnt; k++)
213 			*(data + i++) = dpni_stats.raw.counter[k];
214 	}
215 
216 	/* Print per-cpu extra stats */
217 	for_each_online_cpu(k) {
218 		extras = per_cpu_ptr(priv->percpu_extras, k);
219 		for (j = 0; j < sizeof(*extras) / sizeof(__u64); j++)
220 			*((__u64 *)data + i + j) += *((__u64 *)extras + j);
221 	}
222 	i += j;
223 
224 	/* Per-channel stats */
225 	for (k = 0; k < priv->num_channels; k++) {
226 		ch_stats = &priv->channel[k]->stats;
227 		for (j = 0; j < sizeof(*ch_stats) / sizeof(__u64); j++)
228 			*((__u64 *)data + i + j) += *((__u64 *)ch_stats + j);
229 	}
230 	i += j;
231 
232 	for (j = 0; j < priv->num_fqs; j++) {
233 		/* Print FQ instantaneous counts */
234 		err = dpaa2_io_query_fq_count(NULL, priv->fq[j].fqid,
235 					      &fcnt, &bcnt);
236 		if (err) {
237 			netdev_warn(net_dev, "FQ query error %d", err);
238 			return;
239 		}
240 
241 		if (priv->fq[j].type == DPAA2_TX_CONF_FQ) {
242 			fcnt_tx_total += fcnt;
243 			bcnt_tx_total += bcnt;
244 		} else {
245 			fcnt_rx_total += fcnt;
246 			bcnt_rx_total += bcnt;
247 		}
248 	}
249 
250 	*(data + i++) = fcnt_rx_total;
251 	*(data + i++) = bcnt_rx_total;
252 	*(data + i++) = fcnt_tx_total;
253 	*(data + i++) = bcnt_tx_total;
254 
255 	err = dpaa2_io_query_bp_count(NULL, priv->bpid, &buf_cnt);
256 	if (err) {
257 		netdev_warn(net_dev, "Buffer count query error %d\n", err);
258 		return;
259 	}
260 	*(data + i++) = buf_cnt;
261 }
262 
263 static int prep_eth_rule(struct ethhdr *eth_value, struct ethhdr *eth_mask,
264 			 void *key, void *mask, u64 *fields)
265 {
266 	int off;
267 
268 	if (eth_mask->h_proto) {
269 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
270 		*(__be16 *)(key + off) = eth_value->h_proto;
271 		*(__be16 *)(mask + off) = eth_mask->h_proto;
272 		*fields |= DPAA2_ETH_DIST_ETHTYPE;
273 	}
274 
275 	if (!is_zero_ether_addr(eth_mask->h_source)) {
276 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_SA);
277 		ether_addr_copy(key + off, eth_value->h_source);
278 		ether_addr_copy(mask + off, eth_mask->h_source);
279 		*fields |= DPAA2_ETH_DIST_ETHSRC;
280 	}
281 
282 	if (!is_zero_ether_addr(eth_mask->h_dest)) {
283 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
284 		ether_addr_copy(key + off, eth_value->h_dest);
285 		ether_addr_copy(mask + off, eth_mask->h_dest);
286 		*fields |= DPAA2_ETH_DIST_ETHDST;
287 	}
288 
289 	return 0;
290 }
291 
292 static int prep_uip_rule(struct ethtool_usrip4_spec *uip_value,
293 			 struct ethtool_usrip4_spec *uip_mask,
294 			 void *key, void *mask, u64 *fields)
295 {
296 	int off;
297 	u32 tmp_value, tmp_mask;
298 
299 	if (uip_mask->tos || uip_mask->ip_ver)
300 		return -EOPNOTSUPP;
301 
302 	if (uip_mask->ip4src) {
303 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
304 		*(__be32 *)(key + off) = uip_value->ip4src;
305 		*(__be32 *)(mask + off) = uip_mask->ip4src;
306 		*fields |= DPAA2_ETH_DIST_IPSRC;
307 	}
308 
309 	if (uip_mask->ip4dst) {
310 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
311 		*(__be32 *)(key + off) = uip_value->ip4dst;
312 		*(__be32 *)(mask + off) = uip_mask->ip4dst;
313 		*fields |= DPAA2_ETH_DIST_IPDST;
314 	}
315 
316 	if (uip_mask->proto) {
317 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
318 		*(u8 *)(key + off) = uip_value->proto;
319 		*(u8 *)(mask + off) = uip_mask->proto;
320 		*fields |= DPAA2_ETH_DIST_IPPROTO;
321 	}
322 
323 	if (uip_mask->l4_4_bytes) {
324 		tmp_value = be32_to_cpu(uip_value->l4_4_bytes);
325 		tmp_mask = be32_to_cpu(uip_mask->l4_4_bytes);
326 
327 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
328 		*(__be16 *)(key + off) = htons(tmp_value >> 16);
329 		*(__be16 *)(mask + off) = htons(tmp_mask >> 16);
330 		*fields |= DPAA2_ETH_DIST_L4SRC;
331 
332 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
333 		*(__be16 *)(key + off) = htons(tmp_value & 0xFFFF);
334 		*(__be16 *)(mask + off) = htons(tmp_mask & 0xFFFF);
335 		*fields |= DPAA2_ETH_DIST_L4DST;
336 	}
337 
338 	/* Only apply the rule for IPv4 frames */
339 	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
340 	*(__be16 *)(key + off) = htons(ETH_P_IP);
341 	*(__be16 *)(mask + off) = htons(0xFFFF);
342 	*fields |= DPAA2_ETH_DIST_ETHTYPE;
343 
344 	return 0;
345 }
346 
347 static int prep_l4_rule(struct ethtool_tcpip4_spec *l4_value,
348 			struct ethtool_tcpip4_spec *l4_mask,
349 			void *key, void *mask, u8 l4_proto, u64 *fields)
350 {
351 	int off;
352 
353 	if (l4_mask->tos)
354 		return -EOPNOTSUPP;
355 
356 	if (l4_mask->ip4src) {
357 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
358 		*(__be32 *)(key + off) = l4_value->ip4src;
359 		*(__be32 *)(mask + off) = l4_mask->ip4src;
360 		*fields |= DPAA2_ETH_DIST_IPSRC;
361 	}
362 
363 	if (l4_mask->ip4dst) {
364 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
365 		*(__be32 *)(key + off) = l4_value->ip4dst;
366 		*(__be32 *)(mask + off) = l4_mask->ip4dst;
367 		*fields |= DPAA2_ETH_DIST_IPDST;
368 	}
369 
370 	if (l4_mask->psrc) {
371 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
372 		*(__be16 *)(key + off) = l4_value->psrc;
373 		*(__be16 *)(mask + off) = l4_mask->psrc;
374 		*fields |= DPAA2_ETH_DIST_L4SRC;
375 	}
376 
377 	if (l4_mask->pdst) {
378 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
379 		*(__be16 *)(key + off) = l4_value->pdst;
380 		*(__be16 *)(mask + off) = l4_mask->pdst;
381 		*fields |= DPAA2_ETH_DIST_L4DST;
382 	}
383 
384 	/* Only apply the rule for IPv4 frames with the specified L4 proto */
385 	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
386 	*(__be16 *)(key + off) = htons(ETH_P_IP);
387 	*(__be16 *)(mask + off) = htons(0xFFFF);
388 	*fields |= DPAA2_ETH_DIST_ETHTYPE;
389 
390 	off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
391 	*(u8 *)(key + off) = l4_proto;
392 	*(u8 *)(mask + off) = 0xFF;
393 	*fields |= DPAA2_ETH_DIST_IPPROTO;
394 
395 	return 0;
396 }
397 
398 static int prep_ext_rule(struct ethtool_flow_ext *ext_value,
399 			 struct ethtool_flow_ext *ext_mask,
400 			 void *key, void *mask, u64 *fields)
401 {
402 	int off;
403 
404 	if (ext_mask->vlan_etype)
405 		return -EOPNOTSUPP;
406 
407 	if (ext_mask->vlan_tci) {
408 		off = dpaa2_eth_cls_fld_off(NET_PROT_VLAN, NH_FLD_VLAN_TCI);
409 		*(__be16 *)(key + off) = ext_value->vlan_tci;
410 		*(__be16 *)(mask + off) = ext_mask->vlan_tci;
411 		*fields |= DPAA2_ETH_DIST_VLAN;
412 	}
413 
414 	return 0;
415 }
416 
417 static int prep_mac_ext_rule(struct ethtool_flow_ext *ext_value,
418 			     struct ethtool_flow_ext *ext_mask,
419 			     void *key, void *mask, u64 *fields)
420 {
421 	int off;
422 
423 	if (!is_zero_ether_addr(ext_mask->h_dest)) {
424 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
425 		ether_addr_copy(key + off, ext_value->h_dest);
426 		ether_addr_copy(mask + off, ext_mask->h_dest);
427 		*fields |= DPAA2_ETH_DIST_ETHDST;
428 	}
429 
430 	return 0;
431 }
432 
433 static int prep_cls_rule(struct ethtool_rx_flow_spec *fs, void *key, void *mask,
434 			 u64 *fields)
435 {
436 	int err;
437 
438 	switch (fs->flow_type & 0xFF) {
439 	case ETHER_FLOW:
440 		err = prep_eth_rule(&fs->h_u.ether_spec, &fs->m_u.ether_spec,
441 				    key, mask, fields);
442 		break;
443 	case IP_USER_FLOW:
444 		err = prep_uip_rule(&fs->h_u.usr_ip4_spec,
445 				    &fs->m_u.usr_ip4_spec, key, mask, fields);
446 		break;
447 	case TCP_V4_FLOW:
448 		err = prep_l4_rule(&fs->h_u.tcp_ip4_spec, &fs->m_u.tcp_ip4_spec,
449 				   key, mask, IPPROTO_TCP, fields);
450 		break;
451 	case UDP_V4_FLOW:
452 		err = prep_l4_rule(&fs->h_u.udp_ip4_spec, &fs->m_u.udp_ip4_spec,
453 				   key, mask, IPPROTO_UDP, fields);
454 		break;
455 	case SCTP_V4_FLOW:
456 		err = prep_l4_rule(&fs->h_u.sctp_ip4_spec,
457 				   &fs->m_u.sctp_ip4_spec, key, mask,
458 				   IPPROTO_SCTP, fields);
459 		break;
460 	default:
461 		return -EOPNOTSUPP;
462 	}
463 
464 	if (err)
465 		return err;
466 
467 	if (fs->flow_type & FLOW_EXT) {
468 		err = prep_ext_rule(&fs->h_ext, &fs->m_ext, key, mask, fields);
469 		if (err)
470 			return err;
471 	}
472 
473 	if (fs->flow_type & FLOW_MAC_EXT) {
474 		err = prep_mac_ext_rule(&fs->h_ext, &fs->m_ext, key, mask,
475 					fields);
476 		if (err)
477 			return err;
478 	}
479 
480 	return 0;
481 }
482 
483 static int do_cls_rule(struct net_device *net_dev,
484 		       struct ethtool_rx_flow_spec *fs,
485 		       bool add)
486 {
487 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
488 	struct device *dev = net_dev->dev.parent;
489 	struct dpni_rule_cfg rule_cfg = { 0 };
490 	struct dpni_fs_action_cfg fs_act = { 0 };
491 	dma_addr_t key_iova;
492 	u64 fields = 0;
493 	void *key_buf;
494 	int err;
495 
496 	if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
497 	    fs->ring_cookie >= dpaa2_eth_queue_count(priv))
498 		return -EINVAL;
499 
500 	rule_cfg.key_size = dpaa2_eth_cls_key_size(DPAA2_ETH_DIST_ALL);
501 
502 	/* allocate twice the key size, for the actual key and for mask */
503 	key_buf = kzalloc(rule_cfg.key_size * 2, GFP_KERNEL);
504 	if (!key_buf)
505 		return -ENOMEM;
506 
507 	/* Fill the key and mask memory areas */
508 	err = prep_cls_rule(fs, key_buf, key_buf + rule_cfg.key_size, &fields);
509 	if (err)
510 		goto free_mem;
511 
512 	if (!dpaa2_eth_fs_mask_enabled(priv)) {
513 		/* Masking allows us to configure a maximal key during init and
514 		 * use it for all flow steering rules. Without it, we include
515 		 * in the key only the fields actually used, so we need to
516 		 * extract the others from the final key buffer.
517 		 *
518 		 * Program the FS key if needed, or return error if previously
519 		 * set key can't be used for the current rule. User needs to
520 		 * delete existing rules in this case to allow for the new one.
521 		 */
522 		if (!priv->rx_cls_fields) {
523 			err = dpaa2_eth_set_cls(net_dev, fields);
524 			if (err)
525 				goto free_mem;
526 
527 			priv->rx_cls_fields = fields;
528 		} else if (priv->rx_cls_fields != fields) {
529 			netdev_err(net_dev, "No support for multiple FS keys, need to delete existing rules\n");
530 			err = -EOPNOTSUPP;
531 			goto free_mem;
532 		}
533 
534 		dpaa2_eth_cls_trim_rule(key_buf, fields);
535 		rule_cfg.key_size = dpaa2_eth_cls_key_size(fields);
536 	}
537 
538 	key_iova = dma_map_single(dev, key_buf, rule_cfg.key_size * 2,
539 				  DMA_TO_DEVICE);
540 	if (dma_mapping_error(dev, key_iova)) {
541 		err = -ENOMEM;
542 		goto free_mem;
543 	}
544 
545 	rule_cfg.key_iova = key_iova;
546 	if (dpaa2_eth_fs_mask_enabled(priv))
547 		rule_cfg.mask_iova = key_iova + rule_cfg.key_size;
548 
549 	if (add) {
550 		if (fs->ring_cookie == RX_CLS_FLOW_DISC)
551 			fs_act.options |= DPNI_FS_OPT_DISCARD;
552 		else
553 			fs_act.flow_id = fs->ring_cookie;
554 		err = dpni_add_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
555 					fs->location, &rule_cfg, &fs_act);
556 	} else {
557 		err = dpni_remove_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
558 					   &rule_cfg);
559 	}
560 
561 	dma_unmap_single(dev, key_iova, rule_cfg.key_size * 2, DMA_TO_DEVICE);
562 
563 free_mem:
564 	kfree(key_buf);
565 
566 	return err;
567 }
568 
569 static int num_rules(struct dpaa2_eth_priv *priv)
570 {
571 	int i, rules = 0;
572 
573 	for (i = 0; i < dpaa2_eth_fs_count(priv); i++)
574 		if (priv->cls_rules[i].in_use)
575 			rules++;
576 
577 	return rules;
578 }
579 
580 static int update_cls_rule(struct net_device *net_dev,
581 			   struct ethtool_rx_flow_spec *new_fs,
582 			   int location)
583 {
584 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
585 	struct dpaa2_eth_cls_rule *rule;
586 	int err = -EINVAL;
587 
588 	if (!priv->rx_cls_enabled)
589 		return -EOPNOTSUPP;
590 
591 	if (location >= dpaa2_eth_fs_count(priv))
592 		return -EINVAL;
593 
594 	rule = &priv->cls_rules[location];
595 
596 	/* If a rule is present at the specified location, delete it. */
597 	if (rule->in_use) {
598 		err = do_cls_rule(net_dev, &rule->fs, false);
599 		if (err)
600 			return err;
601 
602 		rule->in_use = 0;
603 
604 		if (!dpaa2_eth_fs_mask_enabled(priv) && !num_rules(priv))
605 			priv->rx_cls_fields = 0;
606 	}
607 
608 	/* If no new entry to add, return here */
609 	if (!new_fs)
610 		return err;
611 
612 	err = do_cls_rule(net_dev, new_fs, true);
613 	if (err)
614 		return err;
615 
616 	rule->in_use = 1;
617 	rule->fs = *new_fs;
618 
619 	return 0;
620 }
621 
622 static int dpaa2_eth_get_rxnfc(struct net_device *net_dev,
623 			       struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
624 {
625 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
626 	int max_rules = dpaa2_eth_fs_count(priv);
627 	int i, j = 0;
628 
629 	switch (rxnfc->cmd) {
630 	case ETHTOOL_GRXFH:
631 		/* we purposely ignore cmd->flow_type for now, because the
632 		 * classifier only supports a single set of fields for all
633 		 * protocols
634 		 */
635 		rxnfc->data = priv->rx_hash_fields;
636 		break;
637 	case ETHTOOL_GRXRINGS:
638 		rxnfc->data = dpaa2_eth_queue_count(priv);
639 		break;
640 	case ETHTOOL_GRXCLSRLCNT:
641 		rxnfc->rule_cnt = 0;
642 		rxnfc->rule_cnt = num_rules(priv);
643 		rxnfc->data = max_rules;
644 		break;
645 	case ETHTOOL_GRXCLSRULE:
646 		if (rxnfc->fs.location >= max_rules)
647 			return -EINVAL;
648 		rxnfc->fs.location = array_index_nospec(rxnfc->fs.location,
649 							max_rules);
650 		if (!priv->cls_rules[rxnfc->fs.location].in_use)
651 			return -EINVAL;
652 		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
653 		break;
654 	case ETHTOOL_GRXCLSRLALL:
655 		for (i = 0; i < max_rules; i++) {
656 			if (!priv->cls_rules[i].in_use)
657 				continue;
658 			if (j == rxnfc->rule_cnt)
659 				return -EMSGSIZE;
660 			rule_locs[j++] = i;
661 		}
662 		rxnfc->rule_cnt = j;
663 		rxnfc->data = max_rules;
664 		break;
665 	default:
666 		return -EOPNOTSUPP;
667 	}
668 
669 	return 0;
670 }
671 
672 static int dpaa2_eth_set_rxnfc(struct net_device *net_dev,
673 			       struct ethtool_rxnfc *rxnfc)
674 {
675 	int err = 0;
676 
677 	switch (rxnfc->cmd) {
678 	case ETHTOOL_SRXFH:
679 		if ((rxnfc->data & DPAA2_RXH_SUPPORTED) != rxnfc->data)
680 			return -EOPNOTSUPP;
681 		err = dpaa2_eth_set_hash(net_dev, rxnfc->data);
682 		break;
683 	case ETHTOOL_SRXCLSRLINS:
684 		err = update_cls_rule(net_dev, &rxnfc->fs, rxnfc->fs.location);
685 		break;
686 	case ETHTOOL_SRXCLSRLDEL:
687 		err = update_cls_rule(net_dev, NULL, rxnfc->fs.location);
688 		break;
689 	default:
690 		err = -EOPNOTSUPP;
691 	}
692 
693 	return err;
694 }
695 
696 int dpaa2_phc_index = -1;
697 EXPORT_SYMBOL(dpaa2_phc_index);
698 
699 static int dpaa2_eth_get_ts_info(struct net_device *dev,
700 				 struct ethtool_ts_info *info)
701 {
702 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
703 				SOF_TIMESTAMPING_RX_HARDWARE |
704 				SOF_TIMESTAMPING_RAW_HARDWARE;
705 
706 	info->phc_index = dpaa2_phc_index;
707 
708 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
709 			 (1 << HWTSTAMP_TX_ON);
710 
711 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
712 			   (1 << HWTSTAMP_FILTER_ALL);
713 	return 0;
714 }
715 
716 const struct ethtool_ops dpaa2_ethtool_ops = {
717 	.get_drvinfo = dpaa2_eth_get_drvinfo,
718 	.get_link = ethtool_op_get_link,
719 	.get_link_ksettings = dpaa2_eth_get_link_ksettings,
720 	.get_pauseparam = dpaa2_eth_get_pauseparam,
721 	.set_pauseparam = dpaa2_eth_set_pauseparam,
722 	.get_sset_count = dpaa2_eth_get_sset_count,
723 	.get_ethtool_stats = dpaa2_eth_get_ethtool_stats,
724 	.get_strings = dpaa2_eth_get_strings,
725 	.get_rxnfc = dpaa2_eth_get_rxnfc,
726 	.set_rxnfc = dpaa2_eth_set_rxnfc,
727 	.get_ts_info = dpaa2_eth_get_ts_info,
728 };
729