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