xref: /linux/drivers/net/ethernet/freescale/dpaa2/dpaa2-ethtool.c (revision ae90a6f0d9c88e71971f9ecbba950c054009257a)
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 	int dpni_stats_page_size[DPNI_STATISTICS_CNT] = {
192 		sizeof(dpni_stats.page_0),
193 		sizeof(dpni_stats.page_1),
194 		sizeof(dpni_stats.page_2),
195 	};
196 
197 	memset(data, 0,
198 	       sizeof(u64) * (DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS));
199 
200 	/* Print standard counters, from DPNI statistics */
201 	for (j = 0; j <= 2; j++) {
202 		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token,
203 					  j, &dpni_stats);
204 		if (err != 0)
205 			netdev_warn(net_dev, "dpni_get_stats(%d) failed\n", j);
206 
207 		num_cnt = dpni_stats_page_size[j] / sizeof(u64);
208 		for (k = 0; k < num_cnt; k++)
209 			*(data + i++) = dpni_stats.raw.counter[k];
210 	}
211 
212 	/* Print per-cpu extra stats */
213 	for_each_online_cpu(k) {
214 		extras = per_cpu_ptr(priv->percpu_extras, k);
215 		for (j = 0; j < sizeof(*extras) / sizeof(__u64); j++)
216 			*((__u64 *)data + i + j) += *((__u64 *)extras + j);
217 	}
218 	i += j;
219 
220 	/* Per-channel stats */
221 	for (k = 0; k < priv->num_channels; k++) {
222 		ch_stats = &priv->channel[k]->stats;
223 		for (j = 0; j < sizeof(*ch_stats) / sizeof(__u64); j++)
224 			*((__u64 *)data + i + j) += *((__u64 *)ch_stats + j);
225 	}
226 	i += j;
227 
228 	for (j = 0; j < priv->num_fqs; j++) {
229 		/* Print FQ instantaneous counts */
230 		err = dpaa2_io_query_fq_count(NULL, priv->fq[j].fqid,
231 					      &fcnt, &bcnt);
232 		if (err) {
233 			netdev_warn(net_dev, "FQ query error %d", err);
234 			return;
235 		}
236 
237 		if (priv->fq[j].type == DPAA2_TX_CONF_FQ) {
238 			fcnt_tx_total += fcnt;
239 			bcnt_tx_total += bcnt;
240 		} else {
241 			fcnt_rx_total += fcnt;
242 			bcnt_rx_total += bcnt;
243 		}
244 	}
245 
246 	*(data + i++) = fcnt_rx_total;
247 	*(data + i++) = bcnt_rx_total;
248 	*(data + i++) = fcnt_tx_total;
249 	*(data + i++) = bcnt_tx_total;
250 
251 	err = dpaa2_io_query_bp_count(NULL, priv->bpid, &buf_cnt);
252 	if (err) {
253 		netdev_warn(net_dev, "Buffer count query error %d\n", err);
254 		return;
255 	}
256 	*(data + i++) = buf_cnt;
257 }
258 
259 static int prep_eth_rule(struct ethhdr *eth_value, struct ethhdr *eth_mask,
260 			 void *key, void *mask, u64 *fields)
261 {
262 	int off;
263 
264 	if (eth_mask->h_proto) {
265 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
266 		*(__be16 *)(key + off) = eth_value->h_proto;
267 		*(__be16 *)(mask + off) = eth_mask->h_proto;
268 		*fields |= DPAA2_ETH_DIST_ETHTYPE;
269 	}
270 
271 	if (!is_zero_ether_addr(eth_mask->h_source)) {
272 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_SA);
273 		ether_addr_copy(key + off, eth_value->h_source);
274 		ether_addr_copy(mask + off, eth_mask->h_source);
275 		*fields |= DPAA2_ETH_DIST_ETHSRC;
276 	}
277 
278 	if (!is_zero_ether_addr(eth_mask->h_dest)) {
279 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
280 		ether_addr_copy(key + off, eth_value->h_dest);
281 		ether_addr_copy(mask + off, eth_mask->h_dest);
282 		*fields |= DPAA2_ETH_DIST_ETHDST;
283 	}
284 
285 	return 0;
286 }
287 
288 static int prep_uip_rule(struct ethtool_usrip4_spec *uip_value,
289 			 struct ethtool_usrip4_spec *uip_mask,
290 			 void *key, void *mask, u64 *fields)
291 {
292 	int off;
293 	u32 tmp_value, tmp_mask;
294 
295 	if (uip_mask->tos || uip_mask->ip_ver)
296 		return -EOPNOTSUPP;
297 
298 	if (uip_mask->ip4src) {
299 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
300 		*(__be32 *)(key + off) = uip_value->ip4src;
301 		*(__be32 *)(mask + off) = uip_mask->ip4src;
302 		*fields |= DPAA2_ETH_DIST_IPSRC;
303 	}
304 
305 	if (uip_mask->ip4dst) {
306 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
307 		*(__be32 *)(key + off) = uip_value->ip4dst;
308 		*(__be32 *)(mask + off) = uip_mask->ip4dst;
309 		*fields |= DPAA2_ETH_DIST_IPDST;
310 	}
311 
312 	if (uip_mask->proto) {
313 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
314 		*(u8 *)(key + off) = uip_value->proto;
315 		*(u8 *)(mask + off) = uip_mask->proto;
316 		*fields |= DPAA2_ETH_DIST_IPPROTO;
317 	}
318 
319 	if (uip_mask->l4_4_bytes) {
320 		tmp_value = be32_to_cpu(uip_value->l4_4_bytes);
321 		tmp_mask = be32_to_cpu(uip_mask->l4_4_bytes);
322 
323 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
324 		*(__be16 *)(key + off) = htons(tmp_value >> 16);
325 		*(__be16 *)(mask + off) = htons(tmp_mask >> 16);
326 		*fields |= DPAA2_ETH_DIST_L4SRC;
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 		*fields |= DPAA2_ETH_DIST_L4DST;
332 	}
333 
334 	/* Only apply the rule for IPv4 frames */
335 	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
336 	*(__be16 *)(key + off) = htons(ETH_P_IP);
337 	*(__be16 *)(mask + off) = htons(0xFFFF);
338 	*fields |= DPAA2_ETH_DIST_ETHTYPE;
339 
340 	return 0;
341 }
342 
343 static int prep_l4_rule(struct ethtool_tcpip4_spec *l4_value,
344 			struct ethtool_tcpip4_spec *l4_mask,
345 			void *key, void *mask, u8 l4_proto, u64 *fields)
346 {
347 	int off;
348 
349 	if (l4_mask->tos)
350 		return -EOPNOTSUPP;
351 
352 	if (l4_mask->ip4src) {
353 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
354 		*(__be32 *)(key + off) = l4_value->ip4src;
355 		*(__be32 *)(mask + off) = l4_mask->ip4src;
356 		*fields |= DPAA2_ETH_DIST_IPSRC;
357 	}
358 
359 	if (l4_mask->ip4dst) {
360 		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
361 		*(__be32 *)(key + off) = l4_value->ip4dst;
362 		*(__be32 *)(mask + off) = l4_mask->ip4dst;
363 		*fields |= DPAA2_ETH_DIST_IPDST;
364 	}
365 
366 	if (l4_mask->psrc) {
367 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
368 		*(__be16 *)(key + off) = l4_value->psrc;
369 		*(__be16 *)(mask + off) = l4_mask->psrc;
370 		*fields |= DPAA2_ETH_DIST_L4SRC;
371 	}
372 
373 	if (l4_mask->pdst) {
374 		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
375 		*(__be16 *)(key + off) = l4_value->pdst;
376 		*(__be16 *)(mask + off) = l4_mask->pdst;
377 		*fields |= DPAA2_ETH_DIST_L4DST;
378 	}
379 
380 	/* Only apply the rule for IPv4 frames with the specified L4 proto */
381 	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
382 	*(__be16 *)(key + off) = htons(ETH_P_IP);
383 	*(__be16 *)(mask + off) = htons(0xFFFF);
384 	*fields |= DPAA2_ETH_DIST_ETHTYPE;
385 
386 	off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
387 	*(u8 *)(key + off) = l4_proto;
388 	*(u8 *)(mask + off) = 0xFF;
389 	*fields |= DPAA2_ETH_DIST_IPPROTO;
390 
391 	return 0;
392 }
393 
394 static int prep_ext_rule(struct ethtool_flow_ext *ext_value,
395 			 struct ethtool_flow_ext *ext_mask,
396 			 void *key, void *mask, u64 *fields)
397 {
398 	int off;
399 
400 	if (ext_mask->vlan_etype)
401 		return -EOPNOTSUPP;
402 
403 	if (ext_mask->vlan_tci) {
404 		off = dpaa2_eth_cls_fld_off(NET_PROT_VLAN, NH_FLD_VLAN_TCI);
405 		*(__be16 *)(key + off) = ext_value->vlan_tci;
406 		*(__be16 *)(mask + off) = ext_mask->vlan_tci;
407 		*fields |= DPAA2_ETH_DIST_VLAN;
408 	}
409 
410 	return 0;
411 }
412 
413 static int prep_mac_ext_rule(struct ethtool_flow_ext *ext_value,
414 			     struct ethtool_flow_ext *ext_mask,
415 			     void *key, void *mask, u64 *fields)
416 {
417 	int off;
418 
419 	if (!is_zero_ether_addr(ext_mask->h_dest)) {
420 		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
421 		ether_addr_copy(key + off, ext_value->h_dest);
422 		ether_addr_copy(mask + off, ext_mask->h_dest);
423 		*fields |= DPAA2_ETH_DIST_ETHDST;
424 	}
425 
426 	return 0;
427 }
428 
429 static int prep_cls_rule(struct ethtool_rx_flow_spec *fs, void *key, void *mask,
430 			 u64 *fields)
431 {
432 	int err;
433 
434 	switch (fs->flow_type & 0xFF) {
435 	case ETHER_FLOW:
436 		err = prep_eth_rule(&fs->h_u.ether_spec, &fs->m_u.ether_spec,
437 				    key, mask, fields);
438 		break;
439 	case IP_USER_FLOW:
440 		err = prep_uip_rule(&fs->h_u.usr_ip4_spec,
441 				    &fs->m_u.usr_ip4_spec, key, mask, fields);
442 		break;
443 	case TCP_V4_FLOW:
444 		err = prep_l4_rule(&fs->h_u.tcp_ip4_spec, &fs->m_u.tcp_ip4_spec,
445 				   key, mask, IPPROTO_TCP, fields);
446 		break;
447 	case UDP_V4_FLOW:
448 		err = prep_l4_rule(&fs->h_u.udp_ip4_spec, &fs->m_u.udp_ip4_spec,
449 				   key, mask, IPPROTO_UDP, fields);
450 		break;
451 	case SCTP_V4_FLOW:
452 		err = prep_l4_rule(&fs->h_u.sctp_ip4_spec,
453 				   &fs->m_u.sctp_ip4_spec, key, mask,
454 				   IPPROTO_SCTP, fields);
455 		break;
456 	default:
457 		return -EOPNOTSUPP;
458 	}
459 
460 	if (err)
461 		return err;
462 
463 	if (fs->flow_type & FLOW_EXT) {
464 		err = prep_ext_rule(&fs->h_ext, &fs->m_ext, key, mask, fields);
465 		if (err)
466 			return err;
467 	}
468 
469 	if (fs->flow_type & FLOW_MAC_EXT) {
470 		err = prep_mac_ext_rule(&fs->h_ext, &fs->m_ext, key, mask,
471 					fields);
472 		if (err)
473 			return err;
474 	}
475 
476 	return 0;
477 }
478 
479 static int do_cls_rule(struct net_device *net_dev,
480 		       struct ethtool_rx_flow_spec *fs,
481 		       bool add)
482 {
483 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
484 	struct device *dev = net_dev->dev.parent;
485 	struct dpni_rule_cfg rule_cfg = { 0 };
486 	struct dpni_fs_action_cfg fs_act = { 0 };
487 	dma_addr_t key_iova;
488 	u64 fields = 0;
489 	void *key_buf;
490 	int err;
491 
492 	if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
493 	    fs->ring_cookie >= dpaa2_eth_queue_count(priv))
494 		return -EINVAL;
495 
496 	rule_cfg.key_size = dpaa2_eth_cls_key_size(DPAA2_ETH_DIST_ALL);
497 
498 	/* allocate twice the key size, for the actual key and for mask */
499 	key_buf = kzalloc(rule_cfg.key_size * 2, GFP_KERNEL);
500 	if (!key_buf)
501 		return -ENOMEM;
502 
503 	/* Fill the key and mask memory areas */
504 	err = prep_cls_rule(fs, key_buf, key_buf + rule_cfg.key_size, &fields);
505 	if (err)
506 		goto free_mem;
507 
508 	if (!dpaa2_eth_fs_mask_enabled(priv)) {
509 		/* Masking allows us to configure a maximal key during init and
510 		 * use it for all flow steering rules. Without it, we include
511 		 * in the key only the fields actually used, so we need to
512 		 * extract the others from the final key buffer.
513 		 *
514 		 * Program the FS key if needed, or return error if previously
515 		 * set key can't be used for the current rule. User needs to
516 		 * delete existing rules in this case to allow for the new one.
517 		 */
518 		if (!priv->rx_cls_fields) {
519 			err = dpaa2_eth_set_cls(net_dev, fields);
520 			if (err)
521 				goto free_mem;
522 
523 			priv->rx_cls_fields = fields;
524 		} else if (priv->rx_cls_fields != fields) {
525 			netdev_err(net_dev, "No support for multiple FS keys, need to delete existing rules\n");
526 			err = -EOPNOTSUPP;
527 			goto free_mem;
528 		}
529 
530 		dpaa2_eth_cls_trim_rule(key_buf, fields);
531 		rule_cfg.key_size = dpaa2_eth_cls_key_size(fields);
532 	}
533 
534 	key_iova = dma_map_single(dev, key_buf, rule_cfg.key_size * 2,
535 				  DMA_TO_DEVICE);
536 	if (dma_mapping_error(dev, key_iova)) {
537 		err = -ENOMEM;
538 		goto free_mem;
539 	}
540 
541 	rule_cfg.key_iova = key_iova;
542 	if (dpaa2_eth_fs_mask_enabled(priv))
543 		rule_cfg.mask_iova = key_iova + rule_cfg.key_size;
544 
545 	if (add) {
546 		if (fs->ring_cookie == RX_CLS_FLOW_DISC)
547 			fs_act.options |= DPNI_FS_OPT_DISCARD;
548 		else
549 			fs_act.flow_id = fs->ring_cookie;
550 		err = dpni_add_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
551 					fs->location, &rule_cfg, &fs_act);
552 	} else {
553 		err = dpni_remove_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
554 					   &rule_cfg);
555 	}
556 
557 	dma_unmap_single(dev, key_iova, rule_cfg.key_size * 2, DMA_TO_DEVICE);
558 
559 free_mem:
560 	kfree(key_buf);
561 
562 	return err;
563 }
564 
565 static int num_rules(struct dpaa2_eth_priv *priv)
566 {
567 	int i, rules = 0;
568 
569 	for (i = 0; i < dpaa2_eth_fs_count(priv); i++)
570 		if (priv->cls_rules[i].in_use)
571 			rules++;
572 
573 	return rules;
574 }
575 
576 static int update_cls_rule(struct net_device *net_dev,
577 			   struct ethtool_rx_flow_spec *new_fs,
578 			   int location)
579 {
580 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
581 	struct dpaa2_eth_cls_rule *rule;
582 	int err = -EINVAL;
583 
584 	if (!priv->rx_cls_enabled)
585 		return -EOPNOTSUPP;
586 
587 	if (location >= dpaa2_eth_fs_count(priv))
588 		return -EINVAL;
589 
590 	rule = &priv->cls_rules[location];
591 
592 	/* If a rule is present at the specified location, delete it. */
593 	if (rule->in_use) {
594 		err = do_cls_rule(net_dev, &rule->fs, false);
595 		if (err)
596 			return err;
597 
598 		rule->in_use = 0;
599 
600 		if (!dpaa2_eth_fs_mask_enabled(priv) && !num_rules(priv))
601 			priv->rx_cls_fields = 0;
602 	}
603 
604 	/* If no new entry to add, return here */
605 	if (!new_fs)
606 		return err;
607 
608 	err = do_cls_rule(net_dev, new_fs, true);
609 	if (err)
610 		return err;
611 
612 	rule->in_use = 1;
613 	rule->fs = *new_fs;
614 
615 	return 0;
616 }
617 
618 static int dpaa2_eth_get_rxnfc(struct net_device *net_dev,
619 			       struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
620 {
621 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
622 	int max_rules = dpaa2_eth_fs_count(priv);
623 	int i, j = 0;
624 
625 	switch (rxnfc->cmd) {
626 	case ETHTOOL_GRXFH:
627 		/* we purposely ignore cmd->flow_type for now, because the
628 		 * classifier only supports a single set of fields for all
629 		 * protocols
630 		 */
631 		rxnfc->data = priv->rx_hash_fields;
632 		break;
633 	case ETHTOOL_GRXRINGS:
634 		rxnfc->data = dpaa2_eth_queue_count(priv);
635 		break;
636 	case ETHTOOL_GRXCLSRLCNT:
637 		rxnfc->rule_cnt = 0;
638 		rxnfc->rule_cnt = num_rules(priv);
639 		rxnfc->data = max_rules;
640 		break;
641 	case ETHTOOL_GRXCLSRULE:
642 		if (rxnfc->fs.location >= max_rules)
643 			return -EINVAL;
644 		rxnfc->fs.location = array_index_nospec(rxnfc->fs.location,
645 							max_rules);
646 		if (!priv->cls_rules[rxnfc->fs.location].in_use)
647 			return -EINVAL;
648 		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
649 		break;
650 	case ETHTOOL_GRXCLSRLALL:
651 		for (i = 0; i < max_rules; i++) {
652 			if (!priv->cls_rules[i].in_use)
653 				continue;
654 			if (j == rxnfc->rule_cnt)
655 				return -EMSGSIZE;
656 			rule_locs[j++] = i;
657 		}
658 		rxnfc->rule_cnt = j;
659 		rxnfc->data = max_rules;
660 		break;
661 	default:
662 		return -EOPNOTSUPP;
663 	}
664 
665 	return 0;
666 }
667 
668 static int dpaa2_eth_set_rxnfc(struct net_device *net_dev,
669 			       struct ethtool_rxnfc *rxnfc)
670 {
671 	int err = 0;
672 
673 	switch (rxnfc->cmd) {
674 	case ETHTOOL_SRXFH:
675 		if ((rxnfc->data & DPAA2_RXH_SUPPORTED) != rxnfc->data)
676 			return -EOPNOTSUPP;
677 		err = dpaa2_eth_set_hash(net_dev, rxnfc->data);
678 		break;
679 	case ETHTOOL_SRXCLSRLINS:
680 		err = update_cls_rule(net_dev, &rxnfc->fs, rxnfc->fs.location);
681 		break;
682 	case ETHTOOL_SRXCLSRLDEL:
683 		err = update_cls_rule(net_dev, NULL, rxnfc->fs.location);
684 		break;
685 	default:
686 		err = -EOPNOTSUPP;
687 	}
688 
689 	return err;
690 }
691 
692 int dpaa2_phc_index = -1;
693 EXPORT_SYMBOL(dpaa2_phc_index);
694 
695 static int dpaa2_eth_get_ts_info(struct net_device *dev,
696 				 struct ethtool_ts_info *info)
697 {
698 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
699 				SOF_TIMESTAMPING_RX_HARDWARE |
700 				SOF_TIMESTAMPING_RAW_HARDWARE;
701 
702 	info->phc_index = dpaa2_phc_index;
703 
704 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
705 			 (1 << HWTSTAMP_TX_ON);
706 
707 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
708 			   (1 << HWTSTAMP_FILTER_ALL);
709 	return 0;
710 }
711 
712 const struct ethtool_ops dpaa2_ethtool_ops = {
713 	.get_drvinfo = dpaa2_eth_get_drvinfo,
714 	.get_link = ethtool_op_get_link,
715 	.get_link_ksettings = dpaa2_eth_get_link_ksettings,
716 	.get_pauseparam = dpaa2_eth_get_pauseparam,
717 	.set_pauseparam = dpaa2_eth_set_pauseparam,
718 	.get_sset_count = dpaa2_eth_get_sset_count,
719 	.get_ethtool_stats = dpaa2_eth_get_ethtool_stats,
720 	.get_strings = dpaa2_eth_get_strings,
721 	.get_rxnfc = dpaa2_eth_get_rxnfc,
722 	.set_rxnfc = dpaa2_eth_set_rxnfc,
723 	.get_ts_info = dpaa2_eth_get_ts_info,
724 };
725