dpaa2-ethtool.c 23.5 KB
Newer Older
1
// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 3
/* Copyright 2014-2016 Freescale Semiconductor Inc.
 * Copyright 2016 NXP
4
 * Copyright 2020 NXP
5 6
 */

7
#include <linux/net_tstamp.h>
8
#include <linux/nospec.h>
9

10 11 12 13
#include "dpni.h"	/* DPNI_LINK_OPT_* */
#include "dpaa2-eth.h"

/* To be kept in sync with DPNI statistics */
14
static char dpaa2_ethtool_stats[][ETH_GSTRING_LEN] = {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	"[hw] rx frames",
	"[hw] rx bytes",
	"[hw] rx mcast frames",
	"[hw] rx mcast bytes",
	"[hw] rx bcast frames",
	"[hw] rx bcast bytes",
	"[hw] tx frames",
	"[hw] tx bytes",
	"[hw] tx mcast frames",
	"[hw] tx mcast bytes",
	"[hw] tx bcast frames",
	"[hw] tx bcast bytes",
	"[hw] rx filtered frames",
	"[hw] rx discarded frames",
	"[hw] rx nobuffer discards",
	"[hw] tx discarded frames",
	"[hw] tx confirmed frames",
32 33 34 35 36
	"[hw] tx dequeued bytes",
	"[hw] tx dequeued frames",
	"[hw] tx rejected bytes",
	"[hw] tx rejected frames",
	"[hw] tx pending frames",
37 38 39 40
};

#define DPAA2_ETH_NUM_STATS	ARRAY_SIZE(dpaa2_ethtool_stats)

41
static char dpaa2_ethtool_extras[][ETH_GSTRING_LEN] = {
42
	/* per-cpu stats */
43 44 45 46
	"[drv] tx conf frames",
	"[drv] tx conf bytes",
	"[drv] tx sg frames",
	"[drv] tx sg bytes",
47 48
	"[drv] tx tso frames",
	"[drv] tx tso bytes",
49 50
	"[drv] rx sg frames",
	"[drv] rx sg bytes",
51 52
	"[drv] tx converted sg frames",
	"[drv] tx converted sg bytes",
53
	"[drv] enqueue portal busy",
54
	/* Channel stats */
55 56 57
	"[drv] dequeue portal busy",
	"[drv] channel pull errors",
	"[drv] cdan",
58 59 60
	"[drv] xdp drop",
	"[drv] xdp tx",
	"[drv] xdp tx errors",
61
	"[drv] xdp redirect",
62 63 64 65 66 67
	/* FQ stats */
	"[qbman] rx pending frames",
	"[qbman] rx pending bytes",
	"[qbman] tx conf pending frames",
	"[qbman] tx conf pending bytes",
	"[qbman] buffer count",
68 69 70 71
};

#define DPAA2_ETH_NUM_EXTRA_STATS	ARRAY_SIZE(dpaa2_ethtool_extras)

72 73 74
static void dpaa2_eth_get_drvinfo(struct net_device *net_dev,
				  struct ethtool_drvinfo *drvinfo)
{
75 76
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);

77
	strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
78

79 80
	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
		 "%u.%u", priv->dpni_ver_major, priv->dpni_ver_minor);
81

82
	strscpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
83 84 85
		sizeof(drvinfo->bus_info));
}

86 87 88 89
static int dpaa2_eth_nway_reset(struct net_device *net_dev)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);

90
	if (dpaa2_eth_is_type_phy(priv))
91 92 93 94 95
		return phylink_ethtool_nway_reset(priv->mac->phylink);

	return -EOPNOTSUPP;
}

96 97 98 99 100 101
static int
dpaa2_eth_get_link_ksettings(struct net_device *net_dev,
			     struct ethtool_link_ksettings *link_settings)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);

102
	if (dpaa2_eth_is_type_phy(priv))
103 104 105
		return phylink_ethtool_ksettings_get(priv->mac->phylink,
						     link_settings);

106
	link_settings->base.autoneg = AUTONEG_DISABLE;
107
	if (!(priv->link_state.options & DPNI_LINK_OPT_HALF_DUPLEX))
108
		link_settings->base.duplex = DUPLEX_FULL;
109
	link_settings->base.speed = priv->link_state.rate;
110

111
	return 0;
112 113
}

114 115 116 117 118 119
static int
dpaa2_eth_set_link_ksettings(struct net_device *net_dev,
			     const struct ethtool_link_ksettings *link_settings)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);

120
	if (!dpaa2_eth_is_type_phy(priv))
121 122 123 124 125
		return -ENOTSUPP;

	return phylink_ethtool_ksettings_set(priv->mac->phylink, link_settings);
}

126 127 128 129 130 131
static void dpaa2_eth_get_pauseparam(struct net_device *net_dev,
				     struct ethtool_pauseparam *pause)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	u64 link_options = priv->link_state.options;

132
	if (dpaa2_eth_is_type_phy(priv)) {
133 134 135 136
		phylink_ethtool_get_pauseparam(priv->mac->phylink, pause);
		return;
	}

137 138
	pause->rx_pause = dpaa2_eth_rx_pause_enabled(link_options);
	pause->tx_pause = dpaa2_eth_tx_pause_enabled(link_options);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	pause->autoneg = AUTONEG_DISABLE;
}

static int dpaa2_eth_set_pauseparam(struct net_device *net_dev,
				    struct ethtool_pauseparam *pause)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	struct dpni_link_cfg cfg = {0};
	int err;

	if (!dpaa2_eth_has_pause_support(priv)) {
		netdev_info(net_dev, "No pause frame support for DPNI version < %d.%d\n",
			    DPNI_PAUSE_VER_MAJOR, DPNI_PAUSE_VER_MINOR);
		return -EOPNOTSUPP;
	}

155
	if (dpaa2_eth_is_type_phy(priv))
156 157
		return phylink_ethtool_set_pauseparam(priv->mac->phylink,
						      pause);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	if (pause->autoneg)
		return -EOPNOTSUPP;

	cfg.rate = priv->link_state.rate;
	cfg.options = priv->link_state.options;
	if (pause->rx_pause)
		cfg.options |= DPNI_LINK_OPT_PAUSE;
	else
		cfg.options &= ~DPNI_LINK_OPT_PAUSE;
	if (!!pause->rx_pause ^ !!pause->tx_pause)
		cfg.options |= DPNI_LINK_OPT_ASYM_PAUSE;
	else
		cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;

	if (cfg.options == priv->link_state.options)
		return 0;

	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &cfg);
	if (err) {
		netdev_err(net_dev, "dpni_set_link_state failed\n");
		return err;
	}

	priv->link_state.options = cfg.options;

	return 0;
}

186 187 188
static void dpaa2_eth_get_strings(struct net_device *netdev, u32 stringset,
				  u8 *data)
{
189
	struct dpaa2_eth_priv *priv = netdev_priv(netdev);
190 191 192 193 194 195
	u8 *p = data;
	int i;

	switch (stringset) {
	case ETH_SS_STATS:
		for (i = 0; i < DPAA2_ETH_NUM_STATS; i++) {
196
			strscpy(p, dpaa2_ethtool_stats[i], ETH_GSTRING_LEN);
197 198
			p += ETH_GSTRING_LEN;
		}
199
		for (i = 0; i < DPAA2_ETH_NUM_EXTRA_STATS; i++) {
200
			strscpy(p, dpaa2_ethtool_extras[i], ETH_GSTRING_LEN);
201 202
			p += ETH_GSTRING_LEN;
		}
203
		if (dpaa2_eth_has_mac(priv))
204
			dpaa2_mac_get_strings(p);
205 206 207 208 209 210
		break;
	}
}

static int dpaa2_eth_get_sset_count(struct net_device *net_dev, int sset)
{
211 212 213
	int num_ss_stats = DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS;
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);

214 215
	switch (sset) {
	case ETH_SS_STATS: /* ethtool_get_stats(), ethtool_get_drvinfo() */
216
		if (dpaa2_eth_has_mac(priv))
217 218
			num_ss_stats += dpaa2_mac_get_sset_count();
		return num_ss_stats;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	default:
		return -EOPNOTSUPP;
	}
}

/** Fill in hardware counters, as returned by MC.
 */
static void dpaa2_eth_get_ethtool_stats(struct net_device *net_dev,
					struct ethtool_stats *stats,
					u64 *data)
{
	int i = 0;
	int j, k, err;
	int num_cnt;
	union dpni_statistics dpni_stats;
234 235 236 237
	u32 fcnt, bcnt;
	u32 fcnt_rx_total = 0, fcnt_tx_total = 0;
	u32 bcnt_rx_total = 0, bcnt_tx_total = 0;
	u32 buf_cnt;
238
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
239 240
	struct dpaa2_eth_drv_stats *extras;
	struct dpaa2_eth_ch_stats *ch_stats;
241 242 243 244
	int dpni_stats_page_size[DPNI_STATISTICS_CNT] = {
		sizeof(dpni_stats.page_0),
		sizeof(dpni_stats.page_1),
		sizeof(dpni_stats.page_2),
245 246 247 248
		sizeof(dpni_stats.page_3),
		sizeof(dpni_stats.page_4),
		sizeof(dpni_stats.page_5),
		sizeof(dpni_stats.page_6),
249
	};
250

251 252
	memset(data, 0,
	       sizeof(u64) * (DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS));
253 254

	/* Print standard counters, from DPNI statistics */
255 256 257 258
	for (j = 0; j <= 6; j++) {
		/* We're not interested in pages 4 & 5 for now */
		if (j == 4 || j == 5)
			continue;
259 260
		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token,
					  j, &dpni_stats);
261 262 263
		if (err == -EINVAL)
			/* Older firmware versions don't support all pages */
			memset(&dpni_stats, 0, sizeof(dpni_stats));
264
		else if (err)
265
			netdev_warn(net_dev, "dpni_get_stats(%d) failed\n", j);
266 267

		num_cnt = dpni_stats_page_size[j] / sizeof(u64);
268 269 270
		for (k = 0; k < num_cnt; k++)
			*(data + i++) = dpni_stats.raw.counter[k];
	}
271 272 273 274 275 276 277 278 279

	/* Print per-cpu extra stats */
	for_each_online_cpu(k) {
		extras = per_cpu_ptr(priv->percpu_extras, k);
		for (j = 0; j < sizeof(*extras) / sizeof(__u64); j++)
			*((__u64 *)data + i + j) += *((__u64 *)extras + j);
	}
	i += j;

280 281 282
	/* Per-channel stats */
	for (k = 0; k < priv->num_channels; k++) {
		ch_stats = &priv->channel[k]->stats;
I
Ioana Ciornei 已提交
283
		for (j = 0; j < DPAA2_ETH_CH_STATS; j++)
284
			*((__u64 *)data + i + j) += *((__u64 *)ch_stats + j);
285
	}
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	i += j;

	for (j = 0; j < priv->num_fqs; j++) {
		/* Print FQ instantaneous counts */
		err = dpaa2_io_query_fq_count(NULL, priv->fq[j].fqid,
					      &fcnt, &bcnt);
		if (err) {
			netdev_warn(net_dev, "FQ query error %d", err);
			return;
		}

		if (priv->fq[j].type == DPAA2_TX_CONF_FQ) {
			fcnt_tx_total += fcnt;
			bcnt_tx_total += bcnt;
		} else {
			fcnt_rx_total += fcnt;
			bcnt_rx_total += bcnt;
		}
	}

	*(data + i++) = fcnt_rx_total;
	*(data + i++) = bcnt_rx_total;
	*(data + i++) = fcnt_tx_total;
	*(data + i++) = bcnt_tx_total;

	err = dpaa2_io_query_bp_count(NULL, priv->bpid, &buf_cnt);
	if (err) {
		netdev_warn(net_dev, "Buffer count query error %d\n", err);
		return;
	}
	*(data + i++) = buf_cnt;
317

318
	if (dpaa2_eth_has_mac(priv))
319
		dpaa2_mac_get_ethtool_stats(priv->mac, data + i);
320 321
}

322 323
static int dpaa2_eth_prep_eth_rule(struct ethhdr *eth_value, struct ethhdr *eth_mask,
				   void *key, void *mask, u64 *fields)
324 325 326 327 328 329 330
{
	int off;

	if (eth_mask->h_proto) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
		*(__be16 *)(key + off) = eth_value->h_proto;
		*(__be16 *)(mask + off) = eth_mask->h_proto;
331
		*fields |= DPAA2_ETH_DIST_ETHTYPE;
332 333 334 335 336 337
	}

	if (!is_zero_ether_addr(eth_mask->h_source)) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_SA);
		ether_addr_copy(key + off, eth_value->h_source);
		ether_addr_copy(mask + off, eth_mask->h_source);
338
		*fields |= DPAA2_ETH_DIST_ETHSRC;
339 340 341 342 343 344
	}

	if (!is_zero_ether_addr(eth_mask->h_dest)) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
		ether_addr_copy(key + off, eth_value->h_dest);
		ether_addr_copy(mask + off, eth_mask->h_dest);
345
		*fields |= DPAA2_ETH_DIST_ETHDST;
346 347 348 349 350
	}

	return 0;
}

351 352 353
static int dpaa2_eth_prep_uip_rule(struct ethtool_usrip4_spec *uip_value,
				   struct ethtool_usrip4_spec *uip_mask,
				   void *key, void *mask, u64 *fields)
354 355 356 357 358 359 360 361 362 363 364
{
	int off;
	u32 tmp_value, tmp_mask;

	if (uip_mask->tos || uip_mask->ip_ver)
		return -EOPNOTSUPP;

	if (uip_mask->ip4src) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
		*(__be32 *)(key + off) = uip_value->ip4src;
		*(__be32 *)(mask + off) = uip_mask->ip4src;
365
		*fields |= DPAA2_ETH_DIST_IPSRC;
366 367 368 369 370 371
	}

	if (uip_mask->ip4dst) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
		*(__be32 *)(key + off) = uip_value->ip4dst;
		*(__be32 *)(mask + off) = uip_mask->ip4dst;
372
		*fields |= DPAA2_ETH_DIST_IPDST;
373 374 375 376 377 378
	}

	if (uip_mask->proto) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
		*(u8 *)(key + off) = uip_value->proto;
		*(u8 *)(mask + off) = uip_mask->proto;
379
		*fields |= DPAA2_ETH_DIST_IPPROTO;
380 381 382 383 384 385 386 387 388
	}

	if (uip_mask->l4_4_bytes) {
		tmp_value = be32_to_cpu(uip_value->l4_4_bytes);
		tmp_mask = be32_to_cpu(uip_mask->l4_4_bytes);

		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
		*(__be16 *)(key + off) = htons(tmp_value >> 16);
		*(__be16 *)(mask + off) = htons(tmp_mask >> 16);
389
		*fields |= DPAA2_ETH_DIST_L4SRC;
390 391 392 393

		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
		*(__be16 *)(key + off) = htons(tmp_value & 0xFFFF);
		*(__be16 *)(mask + off) = htons(tmp_mask & 0xFFFF);
394
		*fields |= DPAA2_ETH_DIST_L4DST;
395 396 397 398 399 400
	}

	/* Only apply the rule for IPv4 frames */
	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
	*(__be16 *)(key + off) = htons(ETH_P_IP);
	*(__be16 *)(mask + off) = htons(0xFFFF);
401
	*fields |= DPAA2_ETH_DIST_ETHTYPE;
402 403 404 405

	return 0;
}

406 407 408
static int dpaa2_eth_prep_l4_rule(struct ethtool_tcpip4_spec *l4_value,
				  struct ethtool_tcpip4_spec *l4_mask,
				  void *key, void *mask, u8 l4_proto, u64 *fields)
409 410 411 412 413 414 415 416 417 418
{
	int off;

	if (l4_mask->tos)
		return -EOPNOTSUPP;

	if (l4_mask->ip4src) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
		*(__be32 *)(key + off) = l4_value->ip4src;
		*(__be32 *)(mask + off) = l4_mask->ip4src;
419
		*fields |= DPAA2_ETH_DIST_IPSRC;
420 421 422 423 424 425
	}

	if (l4_mask->ip4dst) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
		*(__be32 *)(key + off) = l4_value->ip4dst;
		*(__be32 *)(mask + off) = l4_mask->ip4dst;
426
		*fields |= DPAA2_ETH_DIST_IPDST;
427 428 429 430 431 432
	}

	if (l4_mask->psrc) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
		*(__be16 *)(key + off) = l4_value->psrc;
		*(__be16 *)(mask + off) = l4_mask->psrc;
433
		*fields |= DPAA2_ETH_DIST_L4SRC;
434 435 436 437 438 439
	}

	if (l4_mask->pdst) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
		*(__be16 *)(key + off) = l4_value->pdst;
		*(__be16 *)(mask + off) = l4_mask->pdst;
440
		*fields |= DPAA2_ETH_DIST_L4DST;
441 442 443 444 445 446
	}

	/* Only apply the rule for IPv4 frames with the specified L4 proto */
	off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
	*(__be16 *)(key + off) = htons(ETH_P_IP);
	*(__be16 *)(mask + off) = htons(0xFFFF);
447
	*fields |= DPAA2_ETH_DIST_ETHTYPE;
448 449 450 451

	off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
	*(u8 *)(key + off) = l4_proto;
	*(u8 *)(mask + off) = 0xFF;
452
	*fields |= DPAA2_ETH_DIST_IPPROTO;
453 454 455 456

	return 0;
}

457 458 459
static int dpaa2_eth_prep_ext_rule(struct ethtool_flow_ext *ext_value,
				   struct ethtool_flow_ext *ext_mask,
				   void *key, void *mask, u64 *fields)
460 461 462 463 464 465 466 467 468 469
{
	int off;

	if (ext_mask->vlan_etype)
		return -EOPNOTSUPP;

	if (ext_mask->vlan_tci) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_VLAN, NH_FLD_VLAN_TCI);
		*(__be16 *)(key + off) = ext_value->vlan_tci;
		*(__be16 *)(mask + off) = ext_mask->vlan_tci;
470
		*fields |= DPAA2_ETH_DIST_VLAN;
471 472 473 474 475
	}

	return 0;
}

476 477 478
static int dpaa2_eth_prep_mac_ext_rule(struct ethtool_flow_ext *ext_value,
				       struct ethtool_flow_ext *ext_mask,
				       void *key, void *mask, u64 *fields)
479 480 481 482 483 484 485
{
	int off;

	if (!is_zero_ether_addr(ext_mask->h_dest)) {
		off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
		ether_addr_copy(key + off, ext_value->h_dest);
		ether_addr_copy(mask + off, ext_mask->h_dest);
486
		*fields |= DPAA2_ETH_DIST_ETHDST;
487 488 489 490 491
	}

	return 0;
}

492 493
static int dpaa2_eth_prep_cls_rule(struct ethtool_rx_flow_spec *fs, void *key,
				   void *mask, u64 *fields)
494 495 496 497 498
{
	int err;

	switch (fs->flow_type & 0xFF) {
	case ETHER_FLOW:
499 500
		err = dpaa2_eth_prep_eth_rule(&fs->h_u.ether_spec, &fs->m_u.ether_spec,
					      key, mask, fields);
501 502
		break;
	case IP_USER_FLOW:
503 504
		err = dpaa2_eth_prep_uip_rule(&fs->h_u.usr_ip4_spec,
					      &fs->m_u.usr_ip4_spec, key, mask, fields);
505 506
		break;
	case TCP_V4_FLOW:
507 508
		err = dpaa2_eth_prep_l4_rule(&fs->h_u.tcp_ip4_spec, &fs->m_u.tcp_ip4_spec,
					     key, mask, IPPROTO_TCP, fields);
509 510
		break;
	case UDP_V4_FLOW:
511 512
		err = dpaa2_eth_prep_l4_rule(&fs->h_u.udp_ip4_spec, &fs->m_u.udp_ip4_spec,
					     key, mask, IPPROTO_UDP, fields);
513 514
		break;
	case SCTP_V4_FLOW:
515 516 517
		err = dpaa2_eth_prep_l4_rule(&fs->h_u.sctp_ip4_spec,
					     &fs->m_u.sctp_ip4_spec, key, mask,
					     IPPROTO_SCTP, fields);
518 519 520 521 522 523 524 525 526
		break;
	default:
		return -EOPNOTSUPP;
	}

	if (err)
		return err;

	if (fs->flow_type & FLOW_EXT) {
527
		err = dpaa2_eth_prep_ext_rule(&fs->h_ext, &fs->m_ext, key, mask, fields);
528 529 530 531 532
		if (err)
			return err;
	}

	if (fs->flow_type & FLOW_MAC_EXT) {
533 534
		err = dpaa2_eth_prep_mac_ext_rule(&fs->h_ext, &fs->m_ext, key,
						  mask, fields);
535 536 537 538 539 540 541
		if (err)
			return err;
	}

	return 0;
}

542 543 544
static int dpaa2_eth_do_cls_rule(struct net_device *net_dev,
				 struct ethtool_rx_flow_spec *fs,
				 bool add)
545 546 547 548 549 550
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	struct device *dev = net_dev->dev.parent;
	struct dpni_rule_cfg rule_cfg = { 0 };
	struct dpni_fs_action_cfg fs_act = { 0 };
	dma_addr_t key_iova;
551
	u64 fields = 0;
552
	void *key_buf;
553
	int i, err;
554 555 556 557 558

	if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
	    fs->ring_cookie >= dpaa2_eth_queue_count(priv))
		return -EINVAL;

559
	rule_cfg.key_size = dpaa2_eth_cls_key_size(DPAA2_ETH_DIST_ALL);
560 561 562 563 564 565 566

	/* allocate twice the key size, for the actual key and for mask */
	key_buf = kzalloc(rule_cfg.key_size * 2, GFP_KERNEL);
	if (!key_buf)
		return -ENOMEM;

	/* Fill the key and mask memory areas */
567
	err = dpaa2_eth_prep_cls_rule(fs, key_buf, key_buf + rule_cfg.key_size, &fields);
568 569 570
	if (err)
		goto free_mem;

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	if (!dpaa2_eth_fs_mask_enabled(priv)) {
		/* Masking allows us to configure a maximal key during init and
		 * use it for all flow steering rules. Without it, we include
		 * in the key only the fields actually used, so we need to
		 * extract the others from the final key buffer.
		 *
		 * Program the FS key if needed, or return error if previously
		 * set key can't be used for the current rule. User needs to
		 * delete existing rules in this case to allow for the new one.
		 */
		if (!priv->rx_cls_fields) {
			err = dpaa2_eth_set_cls(net_dev, fields);
			if (err)
				goto free_mem;

			priv->rx_cls_fields = fields;
		} else if (priv->rx_cls_fields != fields) {
			netdev_err(net_dev, "No support for multiple FS keys, need to delete existing rules\n");
			err = -EOPNOTSUPP;
			goto free_mem;
		}

		dpaa2_eth_cls_trim_rule(key_buf, fields);
		rule_cfg.key_size = dpaa2_eth_cls_key_size(fields);
	}

597 598 599 600 601 602 603 604
	key_iova = dma_map_single(dev, key_buf, rule_cfg.key_size * 2,
				  DMA_TO_DEVICE);
	if (dma_mapping_error(dev, key_iova)) {
		err = -ENOMEM;
		goto free_mem;
	}

	rule_cfg.key_iova = key_iova;
605 606
	if (dpaa2_eth_fs_mask_enabled(priv))
		rule_cfg.mask_iova = key_iova + rule_cfg.key_size;
607 608 609 610 611 612

	if (add) {
		if (fs->ring_cookie == RX_CLS_FLOW_DISC)
			fs_act.options |= DPNI_FS_OPT_DISCARD;
		else
			fs_act.flow_id = fs->ring_cookie;
613 614 615 616 617 618 619 620 621 622
	}
	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
		if (add)
			err = dpni_add_fs_entry(priv->mc_io, 0, priv->mc_token,
						i, fs->location, &rule_cfg,
						&fs_act);
		else
			err = dpni_remove_fs_entry(priv->mc_io, 0,
						   priv->mc_token, i,
						   &rule_cfg);
623
		if (err || priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
624
			break;
625 626 627 628 629 630 631 632 633 634
	}

	dma_unmap_single(dev, key_iova, rule_cfg.key_size * 2, DMA_TO_DEVICE);

free_mem:
	kfree(key_buf);

	return err;
}

635
static int dpaa2_eth_num_cls_rules(struct dpaa2_eth_priv *priv)
636 637 638 639 640 641 642 643 644 645
{
	int i, rules = 0;

	for (i = 0; i < dpaa2_eth_fs_count(priv); i++)
		if (priv->cls_rules[i].in_use)
			rules++;

	return rules;
}

646 647 648
static int dpaa2_eth_update_cls_rule(struct net_device *net_dev,
				     struct ethtool_rx_flow_spec *new_fs,
				     unsigned int location)
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	struct dpaa2_eth_cls_rule *rule;
	int err = -EINVAL;

	if (!priv->rx_cls_enabled)
		return -EOPNOTSUPP;

	if (location >= dpaa2_eth_fs_count(priv))
		return -EINVAL;

	rule = &priv->cls_rules[location];

	/* If a rule is present at the specified location, delete it. */
	if (rule->in_use) {
664
		err = dpaa2_eth_do_cls_rule(net_dev, &rule->fs, false);
665 666 667 668
		if (err)
			return err;

		rule->in_use = 0;
669

670 671
		if (!dpaa2_eth_fs_mask_enabled(priv) &&
		    !dpaa2_eth_num_cls_rules(priv))
672
			priv->rx_cls_fields = 0;
673 674 675 676 677 678
	}

	/* If no new entry to add, return here */
	if (!new_fs)
		return err;

679
	err = dpaa2_eth_do_cls_rule(net_dev, new_fs, true);
680 681 682 683 684 685 686 687 688
	if (err)
		return err;

	rule->in_use = 1;
	rule->fs = *new_fs;

	return 0;
}

689 690 691 692
static int dpaa2_eth_get_rxnfc(struct net_device *net_dev,
			       struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
693 694
	int max_rules = dpaa2_eth_fs_count(priv);
	int i, j = 0;
695 696 697 698 699 700 701 702 703 704 705 706

	switch (rxnfc->cmd) {
	case ETHTOOL_GRXFH:
		/* we purposely ignore cmd->flow_type for now, because the
		 * classifier only supports a single set of fields for all
		 * protocols
		 */
		rxnfc->data = priv->rx_hash_fields;
		break;
	case ETHTOOL_GRXRINGS:
		rxnfc->data = dpaa2_eth_queue_count(priv);
		break;
707 708
	case ETHTOOL_GRXCLSRLCNT:
		rxnfc->rule_cnt = 0;
709
		rxnfc->rule_cnt = dpaa2_eth_num_cls_rules(priv);
710 711 712 713 714
		rxnfc->data = max_rules;
		break;
	case ETHTOOL_GRXCLSRULE:
		if (rxnfc->fs.location >= max_rules)
			return -EINVAL;
715 716
		rxnfc->fs.location = array_index_nospec(rxnfc->fs.location,
							max_rules);
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
		if (!priv->cls_rules[rxnfc->fs.location].in_use)
			return -EINVAL;
		rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
		break;
	case ETHTOOL_GRXCLSRLALL:
		for (i = 0; i < max_rules; i++) {
			if (!priv->cls_rules[i].in_use)
				continue;
			if (j == rxnfc->rule_cnt)
				return -EMSGSIZE;
			rule_locs[j++] = i;
		}
		rxnfc->rule_cnt = j;
		rxnfc->data = max_rules;
		break;
732 733 734 735 736 737 738
	default:
		return -EOPNOTSUPP;
	}

	return 0;
}

739 740 741 742 743 744 745 746 747 748 749
static int dpaa2_eth_set_rxnfc(struct net_device *net_dev,
			       struct ethtool_rxnfc *rxnfc)
{
	int err = 0;

	switch (rxnfc->cmd) {
	case ETHTOOL_SRXFH:
		if ((rxnfc->data & DPAA2_RXH_SUPPORTED) != rxnfc->data)
			return -EOPNOTSUPP;
		err = dpaa2_eth_set_hash(net_dev, rxnfc->data);
		break;
750
	case ETHTOOL_SRXCLSRLINS:
751
		err = dpaa2_eth_update_cls_rule(net_dev, &rxnfc->fs, rxnfc->fs.location);
752 753
		break;
	case ETHTOOL_SRXCLSRLDEL:
754
		err = dpaa2_eth_update_cls_rule(net_dev, NULL, rxnfc->fs.location);
755
		break;
756 757 758 759 760 761 762
	default:
		err = -EOPNOTSUPP;
	}

	return err;
}

763 764 765 766 767 768
int dpaa2_phc_index = -1;
EXPORT_SYMBOL(dpaa2_phc_index);

static int dpaa2_eth_get_ts_info(struct net_device *dev,
				 struct ethtool_ts_info *info)
{
769 770 771
	if (!dpaa2_ptp)
		return ethtool_op_get_ts_info(dev, info);

772 773 774 775 776 777 778
	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
				SOF_TIMESTAMPING_RX_HARDWARE |
				SOF_TIMESTAMPING_RAW_HARDWARE;

	info->phc_index = dpaa2_phc_index;

	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
779 780
			 (1 << HWTSTAMP_TX_ON) |
			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
781 782 783 784 785 786

	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
			   (1 << HWTSTAMP_FILTER_ALL);
	return 0;
}

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
static int dpaa2_eth_get_tunable(struct net_device *net_dev,
				 const struct ethtool_tunable *tuna,
				 void *data)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	int err = 0;

	switch (tuna->id) {
	case ETHTOOL_RX_COPYBREAK:
		*(u32 *)data = priv->rx_copybreak;
		break;
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
}

static int dpaa2_eth_set_tunable(struct net_device *net_dev,
				 const struct ethtool_tunable *tuna,
				 const void *data)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	int err = 0;

	switch (tuna->id) {
	case ETHTOOL_RX_COPYBREAK:
		priv->rx_copybreak = *(u32 *)data;
		break;
	default:
		err = -EOPNOTSUPP;
		break;
	}

	return err;
}

825 826 827 828 829 830 831 832 833
static int dpaa2_eth_get_coalesce(struct net_device *dev,
				  struct ethtool_coalesce *ic,
				  struct kernel_ethtool_coalesce *kernel_coal,
				  struct netlink_ext_ack *extack)
{
	struct dpaa2_eth_priv *priv = netdev_priv(dev);
	struct dpaa2_io *dpio = priv->channel[0]->dpio;

	dpaa2_io_get_irq_coalescing(dpio, &ic->rx_coalesce_usecs);
834
	ic->use_adaptive_rx_coalesce = dpaa2_io_get_adaptive_coalescing(dpio);
835 836 837 838 839 840 841 842 843 844 845

	return 0;
}

static int dpaa2_eth_set_coalesce(struct net_device *dev,
				  struct ethtool_coalesce *ic,
				  struct kernel_ethtool_coalesce *kernel_coal,
				  struct netlink_ext_ack *extack)
{
	struct dpaa2_eth_priv *priv = netdev_priv(dev);
	struct dpaa2_io *dpio;
846
	int prev_adaptive;
847 848 849 850 851 852
	u32 prev_rx_usecs;
	int i, j, err;

	/* Keep track of the previous value, just in case we fail */
	dpio = priv->channel[0]->dpio;
	dpaa2_io_get_irq_coalescing(dpio, &prev_rx_usecs);
853
	prev_adaptive = dpaa2_io_get_adaptive_coalescing(dpio);
854 855 856 857 858

	/* Setup new value for rx coalescing */
	for (i = 0; i < priv->num_channels; i++) {
		dpio = priv->channel[i]->dpio;

859 860
		dpaa2_io_set_adaptive_coalescing(dpio,
						 ic->use_adaptive_rx_coalesce);
861 862 863 864 865 866 867 868 869 870 871 872
		err = dpaa2_io_set_irq_coalescing(dpio, ic->rx_coalesce_usecs);
		if (err)
			goto restore_rx_usecs;
	}

	return 0;

restore_rx_usecs:
	for (j = 0; j < i; j++) {
		dpio = priv->channel[j]->dpio;

		dpaa2_io_set_irq_coalescing(dpio, prev_rx_usecs);
873
		dpaa2_io_set_adaptive_coalescing(dpio, prev_adaptive);
874 875 876 877 878
	}

	return err;
}

879
const struct ethtool_ops dpaa2_ethtool_ops = {
880 881
	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
882
	.get_drvinfo = dpaa2_eth_get_drvinfo,
883
	.nway_reset = dpaa2_eth_nway_reset,
884 885
	.get_link = ethtool_op_get_link,
	.get_link_ksettings = dpaa2_eth_get_link_ksettings,
886
	.set_link_ksettings = dpaa2_eth_set_link_ksettings,
887 888
	.get_pauseparam = dpaa2_eth_get_pauseparam,
	.set_pauseparam = dpaa2_eth_set_pauseparam,
889 890 891 892
	.get_sset_count = dpaa2_eth_get_sset_count,
	.get_ethtool_stats = dpaa2_eth_get_ethtool_stats,
	.get_strings = dpaa2_eth_get_strings,
	.get_rxnfc = dpaa2_eth_get_rxnfc,
893
	.set_rxnfc = dpaa2_eth_set_rxnfc,
894
	.get_ts_info = dpaa2_eth_get_ts_info,
895 896
	.get_tunable = dpaa2_eth_get_tunable,
	.set_tunable = dpaa2_eth_set_tunable,
897 898
	.get_coalesce = dpaa2_eth_get_coalesce,
	.set_coalesce = dpaa2_eth_set_coalesce,
899
};