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 
8 #include "dpni.h"	/* DPNI_LINK_OPT_* */
9 #include "dpaa2-eth.h"
10 
11 /* To be kept in sync with DPNI statistics */
12 static char dpaa2_ethtool_stats[][ETH_GSTRING_LEN] = {
13 	"[hw] rx frames",
14 	"[hw] rx bytes",
15 	"[hw] rx mcast frames",
16 	"[hw] rx mcast bytes",
17 	"[hw] rx bcast frames",
18 	"[hw] rx bcast bytes",
19 	"[hw] tx frames",
20 	"[hw] tx bytes",
21 	"[hw] tx mcast frames",
22 	"[hw] tx mcast bytes",
23 	"[hw] tx bcast frames",
24 	"[hw] tx bcast bytes",
25 	"[hw] rx filtered frames",
26 	"[hw] rx discarded frames",
27 	"[hw] rx nobuffer discards",
28 	"[hw] tx discarded frames",
29 	"[hw] tx confirmed frames",
30 };
31 
32 #define DPAA2_ETH_NUM_STATS	ARRAY_SIZE(dpaa2_ethtool_stats)
33 
34 static char dpaa2_ethtool_extras[][ETH_GSTRING_LEN] = {
35 	/* per-cpu stats */
36 	"[drv] tx conf frames",
37 	"[drv] tx conf bytes",
38 	"[drv] tx sg frames",
39 	"[drv] tx sg bytes",
40 	"[drv] tx realloc frames",
41 	"[drv] rx sg frames",
42 	"[drv] rx sg bytes",
43 	"[drv] enqueue portal busy",
44 	/* Channel stats */
45 	"[drv] dequeue portal busy",
46 	"[drv] channel pull errors",
47 	"[drv] cdan",
48 	"[drv] xdp drop",
49 	"[drv] xdp tx",
50 	"[drv] xdp tx errors",
51 	"[drv] xdp redirect",
52 	/* FQ stats */
53 	"[qbman] rx pending frames",
54 	"[qbman] rx pending bytes",
55 	"[qbman] tx conf pending frames",
56 	"[qbman] tx conf pending bytes",
57 	"[qbman] buffer count",
58 };
59 
60 #define DPAA2_ETH_NUM_EXTRA_STATS	ARRAY_SIZE(dpaa2_ethtool_extras)
61 
62 static void dpaa2_eth_get_drvinfo(struct net_device *net_dev,
63 				  struct ethtool_drvinfo *drvinfo)
64 {
65 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
66 
67 	strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
68 
69 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
70 		 "%u.%u", priv->dpni_ver_major, priv->dpni_ver_minor);
71 
72 	strlcpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
73 		sizeof(drvinfo->bus_info));
74 }
75 
76 static int
77 dpaa2_eth_get_link_ksettings(struct net_device *net_dev,
78 			     struct ethtool_link_ksettings *link_settings)
79 {
80 	struct dpni_link_state state = {0};
81 	int err = 0;
82 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
83 
84 	err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
85 	if (err) {
86 		netdev_err(net_dev, "ERROR %d getting link state\n", err);
87 		goto out;
88 	}
89 
90 	/* At the moment, we have no way of interrogating the DPMAC
91 	 * from the DPNI side - and for that matter there may exist
92 	 * no DPMAC at all. So for now we just don't report anything
93 	 * beyond the DPNI attributes.
94 	 */
95 	if (state.options & DPNI_LINK_OPT_AUTONEG)
96 		link_settings->base.autoneg = AUTONEG_ENABLE;
97 	if (!(state.options & DPNI_LINK_OPT_HALF_DUPLEX))
98 		link_settings->base.duplex = DUPLEX_FULL;
99 	link_settings->base.speed = state.rate;
100 
101 out:
102 	return err;
103 }
104 
105 #define DPNI_DYNAMIC_LINK_SET_VER_MAJOR		7
106 #define DPNI_DYNAMIC_LINK_SET_VER_MINOR		1
107 static int
108 dpaa2_eth_set_link_ksettings(struct net_device *net_dev,
109 			     const struct ethtool_link_ksettings *link_settings)
110 {
111 	struct dpni_link_cfg cfg = {0};
112 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
113 	int err = 0;
114 
115 	/* If using an older MC version, the DPNI must be down
116 	 * in order to be able to change link settings. Taking steps to let
117 	 * the user know that.
118 	 */
119 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_DYNAMIC_LINK_SET_VER_MAJOR,
120 				   DPNI_DYNAMIC_LINK_SET_VER_MINOR) < 0) {
121 		if (netif_running(net_dev)) {
122 			netdev_info(net_dev, "Interface must be brought down first.\n");
123 			return -EACCES;
124 		}
125 	}
126 
127 	cfg.rate = link_settings->base.speed;
128 	if (link_settings->base.autoneg == AUTONEG_ENABLE)
129 		cfg.options |= DPNI_LINK_OPT_AUTONEG;
130 	else
131 		cfg.options &= ~DPNI_LINK_OPT_AUTONEG;
132 	if (link_settings->base.duplex  == DUPLEX_HALF)
133 		cfg.options |= DPNI_LINK_OPT_HALF_DUPLEX;
134 	else
135 		cfg.options &= ~DPNI_LINK_OPT_HALF_DUPLEX;
136 
137 	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &cfg);
138 	if (err)
139 		/* ethtool will be loud enough if we return an error; no point
140 		 * in putting our own error message on the console by default
141 		 */
142 		netdev_dbg(net_dev, "ERROR %d setting link cfg\n", err);
143 
144 	return err;
145 }
146 
147 static void dpaa2_eth_get_strings(struct net_device *netdev, u32 stringset,
148 				  u8 *data)
149 {
150 	u8 *p = data;
151 	int i;
152 
153 	switch (stringset) {
154 	case ETH_SS_STATS:
155 		for (i = 0; i < DPAA2_ETH_NUM_STATS; i++) {
156 			strlcpy(p, dpaa2_ethtool_stats[i], ETH_GSTRING_LEN);
157 			p += ETH_GSTRING_LEN;
158 		}
159 		for (i = 0; i < DPAA2_ETH_NUM_EXTRA_STATS; i++) {
160 			strlcpy(p, dpaa2_ethtool_extras[i], ETH_GSTRING_LEN);
161 			p += ETH_GSTRING_LEN;
162 		}
163 		break;
164 	}
165 }
166 
167 static int dpaa2_eth_get_sset_count(struct net_device *net_dev, int sset)
168 {
169 	switch (sset) {
170 	case ETH_SS_STATS: /* ethtool_get_stats(), ethtool_get_drvinfo() */
171 		return DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS;
172 	default:
173 		return -EOPNOTSUPP;
174 	}
175 }
176 
177 /** Fill in hardware counters, as returned by MC.
178  */
179 static void dpaa2_eth_get_ethtool_stats(struct net_device *net_dev,
180 					struct ethtool_stats *stats,
181 					u64 *data)
182 {
183 	int i = 0;
184 	int j, k, err;
185 	int num_cnt;
186 	union dpni_statistics dpni_stats;
187 	u32 fcnt, bcnt;
188 	u32 fcnt_rx_total = 0, fcnt_tx_total = 0;
189 	u32 bcnt_rx_total = 0, bcnt_tx_total = 0;
190 	u32 buf_cnt;
191 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
192 	struct dpaa2_eth_drv_stats *extras;
193 	struct dpaa2_eth_ch_stats *ch_stats;
194 
195 	memset(data, 0,
196 	       sizeof(u64) * (DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS));
197 
198 	/* Print standard counters, from DPNI statistics */
199 	for (j = 0; j <= 2; j++) {
200 		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token,
201 					  j, &dpni_stats);
202 		if (err != 0)
203 			netdev_warn(net_dev, "dpni_get_stats(%d) failed\n", j);
204 		switch (j) {
205 		case 0:
206 			num_cnt = sizeof(dpni_stats.page_0) / sizeof(u64);
207 			break;
208 		case 1:
209 			num_cnt = sizeof(dpni_stats.page_1) / sizeof(u64);
210 			break;
211 		case 2:
212 			num_cnt = sizeof(dpni_stats.page_2) / sizeof(u64);
213 			break;
214 		}
215 		for (k = 0; k < num_cnt; k++)
216 			*(data + i++) = dpni_stats.raw.counter[k];
217 	}
218 
219 	/* Print per-cpu extra stats */
220 	for_each_online_cpu(k) {
221 		extras = per_cpu_ptr(priv->percpu_extras, k);
222 		for (j = 0; j < sizeof(*extras) / sizeof(__u64); j++)
223 			*((__u64 *)data + i + j) += *((__u64 *)extras + j);
224 	}
225 	i += j;
226 
227 	/* Per-channel stats */
228 	for (k = 0; k < priv->num_channels; k++) {
229 		ch_stats = &priv->channel[k]->stats;
230 		for (j = 0; j < sizeof(*ch_stats) / sizeof(__u64); j++)
231 			*((__u64 *)data + i + j) += *((__u64 *)ch_stats + j);
232 	}
233 	i += j;
234 
235 	for (j = 0; j < priv->num_fqs; j++) {
236 		/* Print FQ instantaneous counts */
237 		err = dpaa2_io_query_fq_count(NULL, priv->fq[j].fqid,
238 					      &fcnt, &bcnt);
239 		if (err) {
240 			netdev_warn(net_dev, "FQ query error %d", err);
241 			return;
242 		}
243 
244 		if (priv->fq[j].type == DPAA2_TX_CONF_FQ) {
245 			fcnt_tx_total += fcnt;
246 			bcnt_tx_total += bcnt;
247 		} else {
248 			fcnt_rx_total += fcnt;
249 			bcnt_rx_total += bcnt;
250 		}
251 	}
252 
253 	*(data + i++) = fcnt_rx_total;
254 	*(data + i++) = bcnt_rx_total;
255 	*(data + i++) = fcnt_tx_total;
256 	*(data + i++) = bcnt_tx_total;
257 
258 	err = dpaa2_io_query_bp_count(NULL, priv->bpid, &buf_cnt);
259 	if (err) {
260 		netdev_warn(net_dev, "Buffer count query error %d\n", err);
261 		return;
262 	}
263 	*(data + i++) = buf_cnt;
264 }
265 
266 static int prep_eth_rule(struct ethhdr *eth_value, struct ethhdr *eth_mask,
267 			 void *key, void *mask)
268 {
269 	int off;
270 
271 	if (eth_mask->h_proto) {
272 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
273 		*(__be16 *)(key + off) = eth_value->h_proto;
274 		*(__be16 *)(mask + off) = eth_mask->h_proto;
275 	}
276 
277 	if (!is_zero_ether_addr(eth_mask->h_source)) {
278 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_SA);
279 		ether_addr_copy(key + off, eth_value->h_source);
280 		ether_addr_copy(mask + off, eth_mask->h_source);
281 	}
282 
283 	if (!is_zero_ether_addr(eth_mask->h_dest)) {
284 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
285 		ether_addr_copy(key + off, eth_value->h_dest);
286 		ether_addr_copy(mask + off, eth_mask->h_dest);
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)
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 	}
307 
308 	if (uip_mask->ip4dst) {
309 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
310 		*(__be32 *)(key + off) = uip_value->ip4dst;
311 		*(__be32 *)(mask + off) = uip_mask->ip4dst;
312 	}
313 
314 	if (uip_mask->proto) {
315 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
316 		*(u8 *)(key + off) = uip_value->proto;
317 		*(u8 *)(mask + off) = uip_mask->proto;
318 	}
319 
320 	if (uip_mask->l4_4_bytes) {
321 		tmp_value = be32_to_cpu(uip_value->l4_4_bytes);
322 		tmp_mask = be32_to_cpu(uip_mask->l4_4_bytes);
323 
324 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
325 		*(__be16 *)(key + off) = htons(tmp_value >> 16);
326 		*(__be16 *)(mask + off) = htons(tmp_mask >> 16);
327 
328 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
329 		*(__be16 *)(key + off) = htons(tmp_value & 0xFFFF);
330 		*(__be16 *)(mask + off) = htons(tmp_mask & 0xFFFF);
331 	}
332 
333 	/* Only apply the rule for IPv4 frames */
334 	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
335 	*(__be16 *)(key + off) = htons(ETH_P_IP);
336 	*(__be16 *)(mask + off) = htons(0xFFFF);
337 
338 	return 0;
339 }
340 
341 static int prep_l4_rule(struct ethtool_tcpip4_spec *l4_value,
342 			struct ethtool_tcpip4_spec *l4_mask,
343 			void *key, void *mask, u8 l4_proto)
344 {
345 	int off;
346 
347 	if (l4_mask->tos)
348 		return -EOPNOTSUPP;
349 
350 	if (l4_mask->ip4src) {
351 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
352 		*(__be32 *)(key + off) = l4_value->ip4src;
353 		*(__be32 *)(mask + off) = l4_mask->ip4src;
354 	}
355 
356 	if (l4_mask->ip4dst) {
357 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
358 		*(__be32 *)(key + off) = l4_value->ip4dst;
359 		*(__be32 *)(mask + off) = l4_mask->ip4dst;
360 	}
361 
362 	if (l4_mask->psrc) {
363 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
364 		*(__be16 *)(key + off) = l4_value->psrc;
365 		*(__be16 *)(mask + off) = l4_mask->psrc;
366 	}
367 
368 	if (l4_mask->pdst) {
369 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
370 		*(__be16 *)(key + off) = l4_value->pdst;
371 		*(__be16 *)(mask + off) = l4_mask->pdst;
372 	}
373 
374 	/* Only apply the rule for IPv4 frames with the specified L4 proto */
375 	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
376 	*(__be16 *)(key + off) = htons(ETH_P_IP);
377 	*(__be16 *)(mask + off) = htons(0xFFFF);
378 
379 	off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
380 	*(u8 *)(key + off) = l4_proto;
381 	*(u8 *)(mask + off) = 0xFF;
382 
383 	return 0;
384 }
385 
386 static int prep_ext_rule(struct ethtool_flow_ext *ext_value,
387 			 struct ethtool_flow_ext *ext_mask,
388 			 void *key, void *mask)
389 {
390 	int off;
391 
392 	if (ext_mask->vlan_etype)
393 		return -EOPNOTSUPP;
394 
395 	if (ext_mask->vlan_tci) {
396 		off = dpaa2_eth_cls_fld_off(NET_PROT_VLAN, NH_FLD_VLAN_TCI);
397 		*(__be16 *)(key + off) = ext_value->vlan_tci;
398 		*(__be16 *)(mask + off) = ext_mask->vlan_tci;
399 	}
400 
401 	return 0;
402 }
403 
404 static int prep_mac_ext_rule(struct ethtool_flow_ext *ext_value,
405 			     struct ethtool_flow_ext *ext_mask,
406 			     void *key, void *mask)
407 {
408 	int off;
409 
410 	if (!is_zero_ether_addr(ext_mask->h_dest)) {
411 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
412 		ether_addr_copy(key + off, ext_value->h_dest);
413 		ether_addr_copy(mask + off, ext_mask->h_dest);
414 	}
415 
416 	return 0;
417 }
418 
419 static int prep_cls_rule(struct ethtool_rx_flow_spec *fs, void *key, void *mask)
420 {
421 	int err;
422 
423 	switch (fs->flow_type & 0xFF) {
424 	case ETHER_FLOW:
425 		err = prep_eth_rule(&fs->h_u.ether_spec, &fs->m_u.ether_spec,
426 				    key, mask);
427 		break;
428 	case IP_USER_FLOW:
429 		err = prep_uip_rule(&fs->h_u.usr_ip4_spec,
430 				    &fs->m_u.usr_ip4_spec, key, mask);
431 		break;
432 	case TCP_V4_FLOW:
433 		err = prep_l4_rule(&fs->h_u.tcp_ip4_spec, &fs->m_u.tcp_ip4_spec,
434 				   key, mask, IPPROTO_TCP);
435 		break;
436 	case UDP_V4_FLOW:
437 		err = prep_l4_rule(&fs->h_u.udp_ip4_spec, &fs->m_u.udp_ip4_spec,
438 				   key, mask, IPPROTO_UDP);
439 		break;
440 	case SCTP_V4_FLOW:
441 		err = prep_l4_rule(&fs->h_u.sctp_ip4_spec,
442 				   &fs->m_u.sctp_ip4_spec, key, mask,
443 				   IPPROTO_SCTP);
444 		break;
445 	default:
446 		return -EOPNOTSUPP;
447 	}
448 
449 	if (err)
450 		return err;
451 
452 	if (fs->flow_type & FLOW_EXT) {
453 		err = prep_ext_rule(&fs->h_ext, &fs->m_ext, key, mask);
454 		if (err)
455 			return err;
456 	}
457 
458 	if (fs->flow_type & FLOW_MAC_EXT) {
459 		err = prep_mac_ext_rule(&fs->h_ext, &fs->m_ext, key, mask);
460 		if (err)
461 			return err;
462 	}
463 
464 	return 0;
465 }
466 
467 static int do_cls_rule(struct net_device *net_dev,
468 		       struct ethtool_rx_flow_spec *fs,
469 		       bool add)
470 {
471 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
472 	struct device *dev = net_dev->dev.parent;
473 	struct dpni_rule_cfg rule_cfg = { 0 };
474 	struct dpni_fs_action_cfg fs_act = { 0 };
475 	dma_addr_t key_iova;
476 	void *key_buf;
477 	int err;
478 
479 	if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
480 	    fs->ring_cookie >= dpaa2_eth_queue_count(priv))
481 		return -EINVAL;
482 
483 	rule_cfg.key_size = dpaa2_eth_cls_key_size();
484 
485 	/* allocate twice the key size, for the actual key and for mask */
486 	key_buf = kzalloc(rule_cfg.key_size * 2, GFP_KERNEL);
487 	if (!key_buf)
488 		return -ENOMEM;
489 
490 	/* Fill the key and mask memory areas */
491 	err = prep_cls_rule(fs, key_buf, key_buf + rule_cfg.key_size);
492 	if (err)
493 		goto free_mem;
494 
495 	key_iova = dma_map_single(dev, key_buf, rule_cfg.key_size * 2,
496 				  DMA_TO_DEVICE);
497 	if (dma_mapping_error(dev, key_iova)) {
498 		err = -ENOMEM;
499 		goto free_mem;
500 	}
501 
502 	rule_cfg.key_iova = key_iova;
503 	rule_cfg.mask_iova = key_iova + rule_cfg.key_size;
504 
505 	if (add) {
506 		if (fs->ring_cookie == RX_CLS_FLOW_DISC)
507 			fs_act.options |= DPNI_FS_OPT_DISCARD;
508 		else
509 			fs_act.flow_id = fs->ring_cookie;
510 		err = dpni_add_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
511 					fs->location, &rule_cfg, &fs_act);
512 	} else {
513 		err = dpni_remove_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
514 					   &rule_cfg);
515 	}
516 
517 	dma_unmap_single(dev, key_iova, rule_cfg.key_size * 2, DMA_TO_DEVICE);
518 
519 free_mem:
520 	kfree(key_buf);
521 
522 	return err;
523 }
524 
525 static int update_cls_rule(struct net_device *net_dev,
526 			   struct ethtool_rx_flow_spec *new_fs,
527 			   int location)
528 {
529 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
530 	struct dpaa2_eth_cls_rule *rule;
531 	int err = -EINVAL;
532 
533 	if (!priv->rx_cls_enabled)
534 		return -EOPNOTSUPP;
535 
536 	if (location >= dpaa2_eth_fs_count(priv))
537 		return -EINVAL;
538 
539 	rule = &priv->cls_rules[location];
540 
541 	/* If a rule is present at the specified location, delete it. */
542 	if (rule->in_use) {
543 		err = do_cls_rule(net_dev, &rule->fs, false);
544 		if (err)
545 			return err;
546 
547 		rule->in_use = 0;
548 	}
549 
550 	/* If no new entry to add, return here */
551 	if (!new_fs)
552 		return err;
553 
554 	err = do_cls_rule(net_dev, new_fs, true);
555 	if (err)
556 		return err;
557 
558 	rule->in_use = 1;
559 	rule->fs = *new_fs;
560 
561 	return 0;
562 }
563 
564 static int dpaa2_eth_get_rxnfc(struct net_device *net_dev,
565 			       struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
566 {
567 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
568 	int max_rules = dpaa2_eth_fs_count(priv);
569 	int i, j = 0;
570 
571 	switch (rxnfc->cmd) {
572 	case ETHTOOL_GRXFH:
573 		/* we purposely ignore cmd->flow_type for now, because the
574 		 * classifier only supports a single set of fields for all
575 		 * protocols
576 		 */
577 		rxnfc->data = priv->rx_hash_fields;
578 		break;
579 	case ETHTOOL_GRXRINGS:
580 		rxnfc->data = dpaa2_eth_queue_count(priv);
581 		break;
582 	case ETHTOOL_GRXCLSRLCNT:
583 		rxnfc->rule_cnt = 0;
584 		for (i = 0; i < max_rules; i++)
585 			if (priv->cls_rules[i].in_use)
586 				rxnfc->rule_cnt++;
587 		rxnfc->data = max_rules;
588 		break;
589 	case ETHTOOL_GRXCLSRULE:
590 		if (rxnfc->fs.location >= max_rules)
591 			return -EINVAL;
592 		if (!priv->cls_rules[rxnfc->fs.location].in_use)
593 			return -EINVAL;
594 		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
595 		break;
596 	case ETHTOOL_GRXCLSRLALL:
597 		for (i = 0; i < max_rules; i++) {
598 			if (!priv->cls_rules[i].in_use)
599 				continue;
600 			if (j == rxnfc->rule_cnt)
601 				return -EMSGSIZE;
602 			rule_locs[j++] = i;
603 		}
604 		rxnfc->rule_cnt = j;
605 		rxnfc->data = max_rules;
606 		break;
607 	default:
608 		return -EOPNOTSUPP;
609 	}
610 
611 	return 0;
612 }
613 
614 static int dpaa2_eth_set_rxnfc(struct net_device *net_dev,
615 			       struct ethtool_rxnfc *rxnfc)
616 {
617 	int err = 0;
618 
619 	switch (rxnfc->cmd) {
620 	case ETHTOOL_SRXFH:
621 		if ((rxnfc->data & DPAA2_RXH_SUPPORTED) != rxnfc->data)
622 			return -EOPNOTSUPP;
623 		err = dpaa2_eth_set_hash(net_dev, rxnfc->data);
624 		break;
625 	case ETHTOOL_SRXCLSRLINS:
626 		err = update_cls_rule(net_dev, &rxnfc->fs, rxnfc->fs.location);
627 		break;
628 	case ETHTOOL_SRXCLSRLDEL:
629 		err = update_cls_rule(net_dev, NULL, rxnfc->fs.location);
630 		break;
631 	default:
632 		err = -EOPNOTSUPP;
633 	}
634 
635 	return err;
636 }
637 
638 int dpaa2_phc_index = -1;
639 EXPORT_SYMBOL(dpaa2_phc_index);
640 
641 static int dpaa2_eth_get_ts_info(struct net_device *dev,
642 				 struct ethtool_ts_info *info)
643 {
644 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
645 				SOF_TIMESTAMPING_RX_HARDWARE |
646 				SOF_TIMESTAMPING_RAW_HARDWARE;
647 
648 	info->phc_index = dpaa2_phc_index;
649 
650 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
651 			 (1 << HWTSTAMP_TX_ON);
652 
653 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
654 			   (1 << HWTSTAMP_FILTER_ALL);
655 	return 0;
656 }
657 
658 const struct ethtool_ops dpaa2_ethtool_ops = {
659 	.get_drvinfo = dpaa2_eth_get_drvinfo,
660 	.get_link = ethtool_op_get_link,
661 	.get_link_ksettings = dpaa2_eth_get_link_ksettings,
662 	.set_link_ksettings = dpaa2_eth_set_link_ksettings,
663 	.get_sset_count = dpaa2_eth_get_sset_count,
664 	.get_ethtool_stats = dpaa2_eth_get_ethtool_stats,
665 	.get_strings = dpaa2_eth_get_strings,
666 	.get_rxnfc = dpaa2_eth_get_rxnfc,
667 	.set_rxnfc = dpaa2_eth_set_rxnfc,
668 	.get_ts_info = dpaa2_eth_get_ts_info,
669 };
670