Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.5.6.
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright(c) 2013 - 2018 Intel Corporation. */
   3
   4#include <linux/bitfield.h>
   5#include <linux/uaccess.h>
   6
   7/* ethtool support for iavf */
   8#include "iavf.h"
   9
  10/* ethtool statistics helpers */
  11
  12/**
  13 * struct iavf_stats - definition for an ethtool statistic
  14 * @stat_string: statistic name to display in ethtool -S output
  15 * @sizeof_stat: the sizeof() the stat, must be no greater than sizeof(u64)
  16 * @stat_offset: offsetof() the stat from a base pointer
  17 *
  18 * This structure defines a statistic to be added to the ethtool stats buffer.
  19 * It defines a statistic as offset from a common base pointer. Stats should
  20 * be defined in constant arrays using the IAVF_STAT macro, with every element
  21 * of the array using the same _type for calculating the sizeof_stat and
  22 * stat_offset.
  23 *
  24 * The @sizeof_stat is expected to be sizeof(u8), sizeof(u16), sizeof(u32) or
  25 * sizeof(u64). Other sizes are not expected and will produce a WARN_ONCE from
  26 * the iavf_add_ethtool_stat() helper function.
  27 *
  28 * The @stat_string is interpreted as a format string, allowing formatted
  29 * values to be inserted while looping over multiple structures for a given
  30 * statistics array. Thus, every statistic string in an array should have the
  31 * same type and number of format specifiers, to be formatted by variadic
  32 * arguments to the iavf_add_stat_string() helper function.
  33 **/
  34struct iavf_stats {
  35	char stat_string[ETH_GSTRING_LEN];
  36	int sizeof_stat;
  37	int stat_offset;
  38};
  39
  40/* Helper macro to define an iavf_stat structure with proper size and type.
  41 * Use this when defining constant statistics arrays. Note that @_type expects
  42 * only a type name and is used multiple times.
  43 */
  44#define IAVF_STAT(_type, _name, _stat) { \
  45	.stat_string = _name, \
  46	.sizeof_stat = sizeof_field(_type, _stat), \
  47	.stat_offset = offsetof(_type, _stat) \
  48}
  49
  50/* Helper macro for defining some statistics related to queues */
  51#define IAVF_QUEUE_STAT(_name, _stat) \
  52	IAVF_STAT(struct iavf_ring, _name, _stat)
  53
  54/* Stats associated with a Tx or Rx ring */
  55static const struct iavf_stats iavf_gstrings_queue_stats[] = {
  56	IAVF_QUEUE_STAT("%s-%u.packets", stats.packets),
  57	IAVF_QUEUE_STAT("%s-%u.bytes", stats.bytes),
  58};
  59
  60/**
  61 * iavf_add_one_ethtool_stat - copy the stat into the supplied buffer
  62 * @data: location to store the stat value
  63 * @pointer: basis for where to copy from
  64 * @stat: the stat definition
  65 *
  66 * Copies the stat data defined by the pointer and stat structure pair into
  67 * the memory supplied as data. Used to implement iavf_add_ethtool_stats and
  68 * iavf_add_queue_stats. If the pointer is null, data will be zero'd.
  69 */
  70static void
  71iavf_add_one_ethtool_stat(u64 *data, void *pointer,
  72			  const struct iavf_stats *stat)
  73{
  74	char *p;
  75
  76	if (!pointer) {
  77		/* ensure that the ethtool data buffer is zero'd for any stats
  78		 * which don't have a valid pointer.
  79		 */
  80		*data = 0;
  81		return;
  82	}
  83
  84	p = (char *)pointer + stat->stat_offset;
  85	switch (stat->sizeof_stat) {
  86	case sizeof(u64):
  87		*data = *((u64 *)p);
  88		break;
  89	case sizeof(u32):
  90		*data = *((u32 *)p);
  91		break;
  92	case sizeof(u16):
  93		*data = *((u16 *)p);
  94		break;
  95	case sizeof(u8):
  96		*data = *((u8 *)p);
  97		break;
  98	default:
  99		WARN_ONCE(1, "unexpected stat size for %s",
 100			  stat->stat_string);
 101		*data = 0;
 102	}
 103}
 104
 105/**
 106 * __iavf_add_ethtool_stats - copy stats into the ethtool supplied buffer
 107 * @data: ethtool stats buffer
 108 * @pointer: location to copy stats from
 109 * @stats: array of stats to copy
 110 * @size: the size of the stats definition
 111 *
 112 * Copy the stats defined by the stats array using the pointer as a base into
 113 * the data buffer supplied by ethtool. Updates the data pointer to point to
 114 * the next empty location for successive calls to __iavf_add_ethtool_stats.
 115 * If pointer is null, set the data values to zero and update the pointer to
 116 * skip these stats.
 117 **/
 118static void
 119__iavf_add_ethtool_stats(u64 **data, void *pointer,
 120			 const struct iavf_stats stats[],
 121			 const unsigned int size)
 122{
 123	unsigned int i;
 124
 125	for (i = 0; i < size; i++)
 126		iavf_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
 127}
 128
 129/**
 130 * iavf_add_ethtool_stats - copy stats into ethtool supplied buffer
 131 * @data: ethtool stats buffer
 132 * @pointer: location where stats are stored
 133 * @stats: static const array of stat definitions
 134 *
 135 * Macro to ease the use of __iavf_add_ethtool_stats by taking a static
 136 * constant stats array and passing the ARRAY_SIZE(). This avoids typos by
 137 * ensuring that we pass the size associated with the given stats array.
 138 *
 139 * The parameter @stats is evaluated twice, so parameters with side effects
 140 * should be avoided.
 141 **/
 142#define iavf_add_ethtool_stats(data, pointer, stats) \
 143	__iavf_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
 144
 145/**
 146 * iavf_add_queue_stats - copy queue statistics into supplied buffer
 147 * @data: ethtool stats buffer
 148 * @ring: the ring to copy
 149 *
 150 * Queue statistics must be copied while protected by
 151 * u64_stats_fetch_begin, so we can't directly use iavf_add_ethtool_stats.
 152 * Assumes that queue stats are defined in iavf_gstrings_queue_stats. If the
 153 * ring pointer is null, zero out the queue stat values and update the data
 154 * pointer. Otherwise safely copy the stats from the ring into the supplied
 155 * buffer and update the data pointer when finished.
 156 *
 157 * This function expects to be called while under rcu_read_lock().
 158 **/
 159static void
 160iavf_add_queue_stats(u64 **data, struct iavf_ring *ring)
 161{
 162	const unsigned int size = ARRAY_SIZE(iavf_gstrings_queue_stats);
 163	const struct iavf_stats *stats = iavf_gstrings_queue_stats;
 164	unsigned int start;
 165	unsigned int i;
 166
 167	/* To avoid invalid statistics values, ensure that we keep retrying
 168	 * the copy until we get a consistent value according to
 169	 * u64_stats_fetch_retry. But first, make sure our ring is
 170	 * non-null before attempting to access its syncp.
 171	 */
 172	do {
 173		start = !ring ? 0 : u64_stats_fetch_begin(&ring->syncp);
 174		for (i = 0; i < size; i++)
 175			iavf_add_one_ethtool_stat(&(*data)[i], ring, &stats[i]);
 176	} while (ring && u64_stats_fetch_retry(&ring->syncp, start));
 177
 178	/* Once we successfully copy the stats in, update the data pointer */
 179	*data += size;
 180}
 181
 182/**
 183 * __iavf_add_stat_strings - copy stat strings into ethtool buffer
 184 * @p: ethtool supplied buffer
 185 * @stats: stat definitions array
 186 * @size: size of the stats array
 187 *
 188 * Format and copy the strings described by stats into the buffer pointed at
 189 * by p.
 190 **/
 191static void __iavf_add_stat_strings(u8 **p, const struct iavf_stats stats[],
 192				    const unsigned int size, ...)
 193{
 194	unsigned int i;
 195
 196	for (i = 0; i < size; i++) {
 197		va_list args;
 198
 199		va_start(args, size);
 200		vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
 201		*p += ETH_GSTRING_LEN;
 202		va_end(args);
 203	}
 204}
 205
 206/**
 207 * iavf_add_stat_strings - copy stat strings into ethtool buffer
 208 * @p: ethtool supplied buffer
 209 * @stats: stat definitions array
 210 *
 211 * Format and copy the strings described by the const static stats value into
 212 * the buffer pointed at by p.
 213 *
 214 * The parameter @stats is evaluated twice, so parameters with side effects
 215 * should be avoided. Additionally, stats must be an array such that
 216 * ARRAY_SIZE can be called on it.
 217 **/
 218#define iavf_add_stat_strings(p, stats, ...) \
 219	__iavf_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
 220
 221#define VF_STAT(_name, _stat) \
 222	IAVF_STAT(struct iavf_adapter, _name, _stat)
 223
 224static const struct iavf_stats iavf_gstrings_stats[] = {
 225	VF_STAT("rx_bytes", current_stats.rx_bytes),
 226	VF_STAT("rx_unicast", current_stats.rx_unicast),
 227	VF_STAT("rx_multicast", current_stats.rx_multicast),
 228	VF_STAT("rx_broadcast", current_stats.rx_broadcast),
 229	VF_STAT("rx_discards", current_stats.rx_discards),
 230	VF_STAT("rx_unknown_protocol", current_stats.rx_unknown_protocol),
 231	VF_STAT("tx_bytes", current_stats.tx_bytes),
 232	VF_STAT("tx_unicast", current_stats.tx_unicast),
 233	VF_STAT("tx_multicast", current_stats.tx_multicast),
 234	VF_STAT("tx_broadcast", current_stats.tx_broadcast),
 235	VF_STAT("tx_discards", current_stats.tx_discards),
 236	VF_STAT("tx_errors", current_stats.tx_errors),
 237};
 238
 239#define IAVF_STATS_LEN	ARRAY_SIZE(iavf_gstrings_stats)
 240
 241#define IAVF_QUEUE_STATS_LEN	ARRAY_SIZE(iavf_gstrings_queue_stats)
 242
 243/* For now we have one and only one private flag and it is only defined
 244 * when we have support for the SKIP_CPU_SYNC DMA attribute.  Instead
 245 * of leaving all this code sitting around empty we will strip it unless
 246 * our one private flag is actually available.
 247 */
 248struct iavf_priv_flags {
 249	char flag_string[ETH_GSTRING_LEN];
 250	u32 flag;
 251	bool read_only;
 252};
 253
 254#define IAVF_PRIV_FLAG(_name, _flag, _read_only) { \
 255	.flag_string = _name, \
 256	.flag = _flag, \
 257	.read_only = _read_only, \
 258}
 259
 260static const struct iavf_priv_flags iavf_gstrings_priv_flags[] = {
 261	IAVF_PRIV_FLAG("legacy-rx", IAVF_FLAG_LEGACY_RX, 0),
 262};
 263
 264#define IAVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(iavf_gstrings_priv_flags)
 265
 266/**
 267 * iavf_get_link_ksettings - Get Link Speed and Duplex settings
 268 * @netdev: network interface device structure
 269 * @cmd: ethtool command
 270 *
 271 * Reports speed/duplex settings. Because this is a VF, we don't know what
 272 * kind of link we really have, so we fake it.
 273 **/
 274static int iavf_get_link_ksettings(struct net_device *netdev,
 275				   struct ethtool_link_ksettings *cmd)
 276{
 277	struct iavf_adapter *adapter = netdev_priv(netdev);
 278
 279	ethtool_link_ksettings_zero_link_mode(cmd, supported);
 280	cmd->base.autoneg = AUTONEG_DISABLE;
 281	cmd->base.port = PORT_NONE;
 282	cmd->base.duplex = DUPLEX_FULL;
 283
 284	if (ADV_LINK_SUPPORT(adapter)) {
 285		if (adapter->link_speed_mbps &&
 286		    adapter->link_speed_mbps < U32_MAX)
 287			cmd->base.speed = adapter->link_speed_mbps;
 288		else
 289			cmd->base.speed = SPEED_UNKNOWN;
 290
 291		return 0;
 292	}
 293
 294	switch (adapter->link_speed) {
 295	case VIRTCHNL_LINK_SPEED_40GB:
 296		cmd->base.speed = SPEED_40000;
 297		break;
 298	case VIRTCHNL_LINK_SPEED_25GB:
 299		cmd->base.speed = SPEED_25000;
 300		break;
 301	case VIRTCHNL_LINK_SPEED_20GB:
 302		cmd->base.speed = SPEED_20000;
 303		break;
 304	case VIRTCHNL_LINK_SPEED_10GB:
 305		cmd->base.speed = SPEED_10000;
 306		break;
 307	case VIRTCHNL_LINK_SPEED_5GB:
 308		cmd->base.speed = SPEED_5000;
 309		break;
 310	case VIRTCHNL_LINK_SPEED_2_5GB:
 311		cmd->base.speed = SPEED_2500;
 312		break;
 313	case VIRTCHNL_LINK_SPEED_1GB:
 314		cmd->base.speed = SPEED_1000;
 315		break;
 316	case VIRTCHNL_LINK_SPEED_100MB:
 317		cmd->base.speed = SPEED_100;
 318		break;
 319	default:
 320		break;
 321	}
 322
 323	return 0;
 324}
 325
 326/**
 327 * iavf_get_sset_count - Get length of string set
 328 * @netdev: network interface device structure
 329 * @sset: id of string set
 330 *
 331 * Reports size of various string tables.
 332 **/
 333static int iavf_get_sset_count(struct net_device *netdev, int sset)
 334{
 335	/* Report the maximum number queues, even if not every queue is
 336	 * currently configured. Since allocation of queues is in pairs,
 337	 * use netdev->real_num_tx_queues * 2. The real_num_tx_queues is set
 338	 * at device creation and never changes.
 339	 */
 340
 341	if (sset == ETH_SS_STATS)
 342		return IAVF_STATS_LEN +
 343			(IAVF_QUEUE_STATS_LEN * 2 *
 344			 netdev->real_num_tx_queues);
 345	else if (sset == ETH_SS_PRIV_FLAGS)
 346		return IAVF_PRIV_FLAGS_STR_LEN;
 347	else
 348		return -EINVAL;
 349}
 350
 351/**
 352 * iavf_get_ethtool_stats - report device statistics
 353 * @netdev: network interface device structure
 354 * @stats: ethtool statistics structure
 355 * @data: pointer to data buffer
 356 *
 357 * All statistics are added to the data buffer as an array of u64.
 358 **/
 359static void iavf_get_ethtool_stats(struct net_device *netdev,
 360				   struct ethtool_stats *stats, u64 *data)
 361{
 362	struct iavf_adapter *adapter = netdev_priv(netdev);
 363	unsigned int i;
 364
 365	/* Explicitly request stats refresh */
 366	iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_REQUEST_STATS);
 367
 368	iavf_add_ethtool_stats(&data, adapter, iavf_gstrings_stats);
 369
 370	rcu_read_lock();
 371	/* As num_active_queues describe both tx and rx queues, we can use
 372	 * it to iterate over rings' stats.
 373	 */
 374	for (i = 0; i < adapter->num_active_queues; i++) {
 375		struct iavf_ring *ring;
 376
 377		/* Tx rings stats */
 378		ring = &adapter->tx_rings[i];
 379		iavf_add_queue_stats(&data, ring);
 380
 381		/* Rx rings stats */
 382		ring = &adapter->rx_rings[i];
 383		iavf_add_queue_stats(&data, ring);
 384	}
 385	rcu_read_unlock();
 386}
 387
 388/**
 389 * iavf_get_priv_flag_strings - Get private flag strings
 390 * @netdev: network interface device structure
 391 * @data: buffer for string data
 392 *
 393 * Builds the private flags string table
 394 **/
 395static void iavf_get_priv_flag_strings(struct net_device *netdev, u8 *data)
 396{
 397	unsigned int i;
 398
 399	for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++)
 400		ethtool_puts(&data, iavf_gstrings_priv_flags[i].flag_string);
 401}
 402
 403/**
 404 * iavf_get_stat_strings - Get stat strings
 405 * @netdev: network interface device structure
 406 * @data: buffer for string data
 407 *
 408 * Builds the statistics string table
 409 **/
 410static void iavf_get_stat_strings(struct net_device *netdev, u8 *data)
 411{
 412	unsigned int i;
 413
 414	iavf_add_stat_strings(&data, iavf_gstrings_stats);
 415
 416	/* Queues are always allocated in pairs, so we just use
 417	 * real_num_tx_queues for both Tx and Rx queues.
 418	 */
 419	for (i = 0; i < netdev->real_num_tx_queues; i++) {
 420		iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
 421				      "tx", i);
 422		iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
 423				      "rx", i);
 424	}
 425}
 426
 427/**
 428 * iavf_get_strings - Get string set
 429 * @netdev: network interface device structure
 430 * @sset: id of string set
 431 * @data: buffer for string data
 432 *
 433 * Builds string tables for various string sets
 434 **/
 435static void iavf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
 436{
 437	switch (sset) {
 438	case ETH_SS_STATS:
 439		iavf_get_stat_strings(netdev, data);
 440		break;
 441	case ETH_SS_PRIV_FLAGS:
 442		iavf_get_priv_flag_strings(netdev, data);
 443		break;
 444	default:
 445		break;
 446	}
 447}
 448
 449/**
 450 * iavf_get_priv_flags - report device private flags
 451 * @netdev: network interface device structure
 452 *
 453 * The get string set count and the string set should be matched for each
 454 * flag returned.  Add new strings for each flag to the iavf_gstrings_priv_flags
 455 * array.
 456 *
 457 * Returns a u32 bitmap of flags.
 458 **/
 459static u32 iavf_get_priv_flags(struct net_device *netdev)
 460{
 461	struct iavf_adapter *adapter = netdev_priv(netdev);
 462	u32 i, ret_flags = 0;
 463
 464	for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
 465		const struct iavf_priv_flags *priv_flags;
 466
 467		priv_flags = &iavf_gstrings_priv_flags[i];
 468
 469		if (priv_flags->flag & adapter->flags)
 470			ret_flags |= BIT(i);
 471	}
 472
 473	return ret_flags;
 474}
 475
 476/**
 477 * iavf_set_priv_flags - set private flags
 478 * @netdev: network interface device structure
 479 * @flags: bit flags to be set
 480 **/
 481static int iavf_set_priv_flags(struct net_device *netdev, u32 flags)
 482{
 483	struct iavf_adapter *adapter = netdev_priv(netdev);
 484	u32 orig_flags, new_flags, changed_flags;
 485	int ret = 0;
 486	u32 i;
 487
 488	orig_flags = READ_ONCE(adapter->flags);
 489	new_flags = orig_flags;
 490
 491	for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
 492		const struct iavf_priv_flags *priv_flags;
 493
 494		priv_flags = &iavf_gstrings_priv_flags[i];
 495
 496		if (flags & BIT(i))
 497			new_flags |= priv_flags->flag;
 498		else
 499			new_flags &= ~(priv_flags->flag);
 500
 501		if (priv_flags->read_only &&
 502		    ((orig_flags ^ new_flags) & ~BIT(i)))
 503			return -EOPNOTSUPP;
 504	}
 505
 506	/* Before we finalize any flag changes, any checks which we need to
 507	 * perform to determine if the new flags will be supported should go
 508	 * here...
 509	 */
 510
 511	/* Compare and exchange the new flags into place. If we failed, that
 512	 * is if cmpxchg returns anything but the old value, this means
 513	 * something else must have modified the flags variable since we
 514	 * copied it. We'll just punt with an error and log something in the
 515	 * message buffer.
 516	 */
 517	if (cmpxchg(&adapter->flags, orig_flags, new_flags) != orig_flags) {
 518		dev_warn(&adapter->pdev->dev,
 519			 "Unable to update adapter->flags as it was modified by another thread...\n");
 520		return -EAGAIN;
 521	}
 522
 523	changed_flags = orig_flags ^ new_flags;
 524
 525	/* Process any additional changes needed as a result of flag changes.
 526	 * The changed_flags value reflects the list of bits that were changed
 527	 * in the code above.
 528	 */
 529
 530	/* issue a reset to force legacy-rx change to take effect */
 531	if (changed_flags & IAVF_FLAG_LEGACY_RX) {
 532		if (netif_running(netdev)) {
 533			iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
 534			ret = iavf_wait_for_reset(adapter);
 535			if (ret)
 536				netdev_warn(netdev, "Changing private flags timeout or interrupted waiting for reset");
 537		}
 538	}
 539
 540	return ret;
 541}
 542
 543/**
 544 * iavf_get_msglevel - Get debug message level
 545 * @netdev: network interface device structure
 546 *
 547 * Returns current debug message level.
 548 **/
 549static u32 iavf_get_msglevel(struct net_device *netdev)
 550{
 551	struct iavf_adapter *adapter = netdev_priv(netdev);
 552
 553	return adapter->msg_enable;
 554}
 555
 556/**
 557 * iavf_set_msglevel - Set debug message level
 558 * @netdev: network interface device structure
 559 * @data: message level
 560 *
 561 * Set current debug message level. Higher values cause the driver to
 562 * be noisier.
 563 **/
 564static void iavf_set_msglevel(struct net_device *netdev, u32 data)
 565{
 566	struct iavf_adapter *adapter = netdev_priv(netdev);
 567
 568	if (IAVF_DEBUG_USER & data)
 569		adapter->hw.debug_mask = data;
 570	adapter->msg_enable = data;
 571}
 572
 573/**
 574 * iavf_get_drvinfo - Get driver info
 575 * @netdev: network interface device structure
 576 * @drvinfo: ethool driver info structure
 577 *
 578 * Returns information about the driver and device for display to the user.
 579 **/
 580static void iavf_get_drvinfo(struct net_device *netdev,
 581			     struct ethtool_drvinfo *drvinfo)
 582{
 583	struct iavf_adapter *adapter = netdev_priv(netdev);
 584
 585	strscpy(drvinfo->driver, iavf_driver_name, 32);
 586	strscpy(drvinfo->fw_version, "N/A", 4);
 587	strscpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
 588	drvinfo->n_priv_flags = IAVF_PRIV_FLAGS_STR_LEN;
 589}
 590
 591/**
 592 * iavf_get_ringparam - Get ring parameters
 593 * @netdev: network interface device structure
 594 * @ring: ethtool ringparam structure
 595 * @kernel_ring: ethtool extenal ringparam structure
 596 * @extack: netlink extended ACK report struct
 597 *
 598 * Returns current ring parameters. TX and RX rings are reported separately,
 599 * but the number of rings is not reported.
 600 **/
 601static void iavf_get_ringparam(struct net_device *netdev,
 602			       struct ethtool_ringparam *ring,
 603			       struct kernel_ethtool_ringparam *kernel_ring,
 604			       struct netlink_ext_ack *extack)
 605{
 606	struct iavf_adapter *adapter = netdev_priv(netdev);
 607
 608	ring->rx_max_pending = IAVF_MAX_RXD;
 609	ring->tx_max_pending = IAVF_MAX_TXD;
 610	ring->rx_pending = adapter->rx_desc_count;
 611	ring->tx_pending = adapter->tx_desc_count;
 612}
 613
 614/**
 615 * iavf_set_ringparam - Set ring parameters
 616 * @netdev: network interface device structure
 617 * @ring: ethtool ringparam structure
 618 * @kernel_ring: ethtool external ringparam structure
 619 * @extack: netlink extended ACK report struct
 620 *
 621 * Sets ring parameters. TX and RX rings are controlled separately, but the
 622 * number of rings is not specified, so all rings get the same settings.
 623 **/
 624static int iavf_set_ringparam(struct net_device *netdev,
 625			      struct ethtool_ringparam *ring,
 626			      struct kernel_ethtool_ringparam *kernel_ring,
 627			      struct netlink_ext_ack *extack)
 628{
 629	struct iavf_adapter *adapter = netdev_priv(netdev);
 630	u32 new_rx_count, new_tx_count;
 631	int ret = 0;
 632
 633	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
 634		return -EINVAL;
 635
 636	if (ring->tx_pending > IAVF_MAX_TXD ||
 637	    ring->tx_pending < IAVF_MIN_TXD ||
 638	    ring->rx_pending > IAVF_MAX_RXD ||
 639	    ring->rx_pending < IAVF_MIN_RXD) {
 640		netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
 641			   ring->tx_pending, ring->rx_pending, IAVF_MIN_TXD,
 642			   IAVF_MAX_RXD, IAVF_REQ_DESCRIPTOR_MULTIPLE);
 643		return -EINVAL;
 644	}
 645
 646	new_tx_count = ALIGN(ring->tx_pending, IAVF_REQ_DESCRIPTOR_MULTIPLE);
 647	if (new_tx_count != ring->tx_pending)
 648		netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n",
 649			    new_tx_count);
 650
 651	new_rx_count = ALIGN(ring->rx_pending, IAVF_REQ_DESCRIPTOR_MULTIPLE);
 652	if (new_rx_count != ring->rx_pending)
 653		netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n",
 654			    new_rx_count);
 655
 656	/* if nothing to do return success */
 657	if ((new_tx_count == adapter->tx_desc_count) &&
 658	    (new_rx_count == adapter->rx_desc_count)) {
 659		netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
 660		return 0;
 661	}
 662
 663	if (new_tx_count != adapter->tx_desc_count) {
 664		netdev_dbg(netdev, "Changing Tx descriptor count from %d to %d\n",
 665			   adapter->tx_desc_count, new_tx_count);
 666		adapter->tx_desc_count = new_tx_count;
 667	}
 668
 669	if (new_rx_count != adapter->rx_desc_count) {
 670		netdev_dbg(netdev, "Changing Rx descriptor count from %d to %d\n",
 671			   adapter->rx_desc_count, new_rx_count);
 672		adapter->rx_desc_count = new_rx_count;
 673	}
 674
 675	if (netif_running(netdev)) {
 676		iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
 677		ret = iavf_wait_for_reset(adapter);
 678		if (ret)
 679			netdev_warn(netdev, "Changing ring parameters timeout or interrupted waiting for reset");
 680	}
 681
 682	return ret;
 683}
 684
 685/**
 686 * __iavf_get_coalesce - get per-queue coalesce settings
 687 * @netdev: the netdev to check
 688 * @ec: ethtool coalesce data structure
 689 * @queue: which queue to pick
 690 *
 691 * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
 692 * are per queue. If queue is <0 then we default to queue 0 as the
 693 * representative value.
 694 **/
 695static int __iavf_get_coalesce(struct net_device *netdev,
 696			       struct ethtool_coalesce *ec, int queue)
 697{
 698	struct iavf_adapter *adapter = netdev_priv(netdev);
 699	struct iavf_ring *rx_ring, *tx_ring;
 700
 701	/* Rx and Tx usecs per queue value. If user doesn't specify the
 702	 * queue, return queue 0's value to represent.
 703	 */
 704	if (queue < 0)
 705		queue = 0;
 706	else if (queue >= adapter->num_active_queues)
 707		return -EINVAL;
 708
 709	rx_ring = &adapter->rx_rings[queue];
 710	tx_ring = &adapter->tx_rings[queue];
 711
 712	if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
 713		ec->use_adaptive_rx_coalesce = 1;
 714
 715	if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
 716		ec->use_adaptive_tx_coalesce = 1;
 717
 718	ec->rx_coalesce_usecs = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
 719	ec->tx_coalesce_usecs = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
 720
 721	return 0;
 722}
 723
 724/**
 725 * iavf_get_coalesce - Get interrupt coalescing settings
 726 * @netdev: network interface device structure
 727 * @ec: ethtool coalesce structure
 728 * @kernel_coal: ethtool CQE mode setting structure
 729 * @extack: extack for reporting error messages
 730 *
 731 * Returns current coalescing settings. This is referred to elsewhere in the
 732 * driver as Interrupt Throttle Rate, as this is how the hardware describes
 733 * this functionality. Note that if per-queue settings have been modified this
 734 * only represents the settings of queue 0.
 735 **/
 736static int iavf_get_coalesce(struct net_device *netdev,
 737			     struct ethtool_coalesce *ec,
 738			     struct kernel_ethtool_coalesce *kernel_coal,
 739			     struct netlink_ext_ack *extack)
 740{
 741	return __iavf_get_coalesce(netdev, ec, -1);
 742}
 743
 744/**
 745 * iavf_get_per_queue_coalesce - get coalesce values for specific queue
 746 * @netdev: netdev to read
 747 * @ec: coalesce settings from ethtool
 748 * @queue: the queue to read
 749 *
 750 * Read specific queue's coalesce settings.
 751 **/
 752static int iavf_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
 753				       struct ethtool_coalesce *ec)
 754{
 755	return __iavf_get_coalesce(netdev, ec, queue);
 756}
 757
 758/**
 759 * iavf_set_itr_per_queue - set ITR values for specific queue
 760 * @adapter: the VF adapter struct to set values for
 761 * @ec: coalesce settings from ethtool
 762 * @queue: the queue to modify
 763 *
 764 * Change the ITR settings for a specific queue.
 765 **/
 766static int iavf_set_itr_per_queue(struct iavf_adapter *adapter,
 767				  struct ethtool_coalesce *ec, int queue)
 768{
 769	struct iavf_ring *rx_ring = &adapter->rx_rings[queue];
 770	struct iavf_ring *tx_ring = &adapter->tx_rings[queue];
 771	struct iavf_q_vector *q_vector;
 772	u16 itr_setting;
 773
 774	itr_setting = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
 775
 776	if (ec->rx_coalesce_usecs != itr_setting &&
 777	    ec->use_adaptive_rx_coalesce) {
 778		netif_info(adapter, drv, adapter->netdev,
 779			   "Rx interrupt throttling cannot be changed if adaptive-rx is enabled\n");
 780		return -EINVAL;
 781	}
 782
 783	itr_setting = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
 784
 785	if (ec->tx_coalesce_usecs != itr_setting &&
 786	    ec->use_adaptive_tx_coalesce) {
 787		netif_info(adapter, drv, adapter->netdev,
 788			   "Tx interrupt throttling cannot be changed if adaptive-tx is enabled\n");
 789		return -EINVAL;
 790	}
 791
 792	rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
 793	tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
 794
 795	rx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
 796	if (!ec->use_adaptive_rx_coalesce)
 797		rx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
 798
 799	tx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
 800	if (!ec->use_adaptive_tx_coalesce)
 801		tx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
 802
 803	q_vector = rx_ring->q_vector;
 804	q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
 805
 806	q_vector = tx_ring->q_vector;
 807	q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
 808
 809	/* The interrupt handler itself will take care of programming
 810	 * the Tx and Rx ITR values based on the values we have entered
 811	 * into the q_vector, no need to write the values now.
 812	 */
 813	return 0;
 814}
 815
 816/**
 817 * __iavf_set_coalesce - set coalesce settings for particular queue
 818 * @netdev: the netdev to change
 819 * @ec: ethtool coalesce settings
 820 * @queue: the queue to change
 821 *
 822 * Sets the coalesce settings for a particular queue.
 823 **/
 824static int __iavf_set_coalesce(struct net_device *netdev,
 825			       struct ethtool_coalesce *ec, int queue)
 826{
 827	struct iavf_adapter *adapter = netdev_priv(netdev);
 828	int i;
 829
 830	if (ec->rx_coalesce_usecs > IAVF_MAX_ITR) {
 831		netif_info(adapter, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
 832		return -EINVAL;
 833	} else if (ec->tx_coalesce_usecs > IAVF_MAX_ITR) {
 834		netif_info(adapter, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
 835		return -EINVAL;
 836	}
 837
 838	/* Rx and Tx usecs has per queue value. If user doesn't specify the
 839	 * queue, apply to all queues.
 840	 */
 841	if (queue < 0) {
 842		for (i = 0; i < adapter->num_active_queues; i++)
 843			if (iavf_set_itr_per_queue(adapter, ec, i))
 844				return -EINVAL;
 845	} else if (queue < adapter->num_active_queues) {
 846		if (iavf_set_itr_per_queue(adapter, ec, queue))
 847			return -EINVAL;
 848	} else {
 849		netif_info(adapter, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
 850			   adapter->num_active_queues - 1);
 851		return -EINVAL;
 852	}
 853
 854	return 0;
 855}
 856
 857/**
 858 * iavf_set_coalesce - Set interrupt coalescing settings
 859 * @netdev: network interface device structure
 860 * @ec: ethtool coalesce structure
 861 * @kernel_coal: ethtool CQE mode setting structure
 862 * @extack: extack for reporting error messages
 863 *
 864 * Change current coalescing settings for every queue.
 865 **/
 866static int iavf_set_coalesce(struct net_device *netdev,
 867			     struct ethtool_coalesce *ec,
 868			     struct kernel_ethtool_coalesce *kernel_coal,
 869			     struct netlink_ext_ack *extack)
 870{
 871	return __iavf_set_coalesce(netdev, ec, -1);
 872}
 873
 874/**
 875 * iavf_set_per_queue_coalesce - set specific queue's coalesce settings
 876 * @netdev: the netdev to change
 877 * @ec: ethtool's coalesce settings
 878 * @queue: the queue to modify
 879 *
 880 * Modifies a specific queue's coalesce settings.
 881 */
 882static int iavf_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
 883				       struct ethtool_coalesce *ec)
 884{
 885	return __iavf_set_coalesce(netdev, ec, queue);
 886}
 887
 888/**
 889 * iavf_fltr_to_ethtool_flow - convert filter type values to ethtool
 890 * flow type values
 891 * @flow: filter type to be converted
 892 *
 893 * Returns the corresponding ethtool flow type.
 894 */
 895static int iavf_fltr_to_ethtool_flow(enum iavf_fdir_flow_type flow)
 896{
 897	switch (flow) {
 898	case IAVF_FDIR_FLOW_IPV4_TCP:
 899		return TCP_V4_FLOW;
 900	case IAVF_FDIR_FLOW_IPV4_UDP:
 901		return UDP_V4_FLOW;
 902	case IAVF_FDIR_FLOW_IPV4_SCTP:
 903		return SCTP_V4_FLOW;
 904	case IAVF_FDIR_FLOW_IPV4_AH:
 905		return AH_V4_FLOW;
 906	case IAVF_FDIR_FLOW_IPV4_ESP:
 907		return ESP_V4_FLOW;
 908	case IAVF_FDIR_FLOW_IPV4_OTHER:
 909		return IPV4_USER_FLOW;
 910	case IAVF_FDIR_FLOW_IPV6_TCP:
 911		return TCP_V6_FLOW;
 912	case IAVF_FDIR_FLOW_IPV6_UDP:
 913		return UDP_V6_FLOW;
 914	case IAVF_FDIR_FLOW_IPV6_SCTP:
 915		return SCTP_V6_FLOW;
 916	case IAVF_FDIR_FLOW_IPV6_AH:
 917		return AH_V6_FLOW;
 918	case IAVF_FDIR_FLOW_IPV6_ESP:
 919		return ESP_V6_FLOW;
 920	case IAVF_FDIR_FLOW_IPV6_OTHER:
 921		return IPV6_USER_FLOW;
 922	case IAVF_FDIR_FLOW_NON_IP_L2:
 923		return ETHER_FLOW;
 924	default:
 925		/* 0 is undefined ethtool flow */
 926		return 0;
 927	}
 928}
 929
 930/**
 931 * iavf_ethtool_flow_to_fltr - convert ethtool flow type to filter enum
 932 * @eth: Ethtool flow type to be converted
 933 *
 934 * Returns flow enum
 935 */
 936static enum iavf_fdir_flow_type iavf_ethtool_flow_to_fltr(int eth)
 937{
 938	switch (eth) {
 939	case TCP_V4_FLOW:
 940		return IAVF_FDIR_FLOW_IPV4_TCP;
 941	case UDP_V4_FLOW:
 942		return IAVF_FDIR_FLOW_IPV4_UDP;
 943	case SCTP_V4_FLOW:
 944		return IAVF_FDIR_FLOW_IPV4_SCTP;
 945	case AH_V4_FLOW:
 946		return IAVF_FDIR_FLOW_IPV4_AH;
 947	case ESP_V4_FLOW:
 948		return IAVF_FDIR_FLOW_IPV4_ESP;
 949	case IPV4_USER_FLOW:
 950		return IAVF_FDIR_FLOW_IPV4_OTHER;
 951	case TCP_V6_FLOW:
 952		return IAVF_FDIR_FLOW_IPV6_TCP;
 953	case UDP_V6_FLOW:
 954		return IAVF_FDIR_FLOW_IPV6_UDP;
 955	case SCTP_V6_FLOW:
 956		return IAVF_FDIR_FLOW_IPV6_SCTP;
 957	case AH_V6_FLOW:
 958		return IAVF_FDIR_FLOW_IPV6_AH;
 959	case ESP_V6_FLOW:
 960		return IAVF_FDIR_FLOW_IPV6_ESP;
 961	case IPV6_USER_FLOW:
 962		return IAVF_FDIR_FLOW_IPV6_OTHER;
 963	case ETHER_FLOW:
 964		return IAVF_FDIR_FLOW_NON_IP_L2;
 965	default:
 966		return IAVF_FDIR_FLOW_NONE;
 967	}
 968}
 969
 970/**
 971 * iavf_is_mask_valid - check mask field set
 972 * @mask: full mask to check
 973 * @field: field for which mask should be valid
 974 *
 975 * If the mask is fully set return true. If it is not valid for field return
 976 * false.
 977 */
 978static bool iavf_is_mask_valid(u64 mask, u64 field)
 979{
 980	return (mask & field) == field;
 981}
 982
 983/**
 984 * iavf_parse_rx_flow_user_data - deconstruct user-defined data
 985 * @fsp: pointer to ethtool Rx flow specification
 986 * @fltr: pointer to Flow Director filter for userdef data storage
 987 *
 988 * Returns 0 on success, negative error value on failure
 989 */
 990static int
 991iavf_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
 992			     struct iavf_fdir_fltr *fltr)
 993{
 994	struct iavf_flex_word *flex;
 995	int i, cnt = 0;
 996
 997	if (!(fsp->flow_type & FLOW_EXT))
 998		return 0;
 999
1000	for (i = 0; i < IAVF_FLEX_WORD_NUM; i++) {
1001#define IAVF_USERDEF_FLEX_WORD_M	GENMASK(15, 0)
1002#define IAVF_USERDEF_FLEX_OFFS_S	16
1003#define IAVF_USERDEF_FLEX_OFFS_M	GENMASK(31, IAVF_USERDEF_FLEX_OFFS_S)
1004#define IAVF_USERDEF_FLEX_FLTR_M	GENMASK(31, 0)
1005		u32 value = be32_to_cpu(fsp->h_ext.data[i]);
1006		u32 mask = be32_to_cpu(fsp->m_ext.data[i]);
1007
1008		if (!value || !mask)
1009			continue;
1010
1011		if (!iavf_is_mask_valid(mask, IAVF_USERDEF_FLEX_FLTR_M))
1012			return -EINVAL;
1013
1014		/* 504 is the maximum value for offsets, and offset is measured
1015		 * from the start of the MAC address.
1016		 */
1017#define IAVF_USERDEF_FLEX_MAX_OFFS_VAL 504
1018		flex = &fltr->flex_words[cnt++];
1019		flex->word = value & IAVF_USERDEF_FLEX_WORD_M;
1020		flex->offset = FIELD_GET(IAVF_USERDEF_FLEX_OFFS_M, value);
1021		if (flex->offset > IAVF_USERDEF_FLEX_MAX_OFFS_VAL)
1022			return -EINVAL;
1023	}
1024
1025	fltr->flex_cnt = cnt;
1026
1027	return 0;
1028}
1029
1030/**
1031 * iavf_fill_rx_flow_ext_data - fill the additional data
1032 * @fsp: pointer to ethtool Rx flow specification
1033 * @fltr: pointer to Flow Director filter to get additional data
1034 */
1035static void
1036iavf_fill_rx_flow_ext_data(struct ethtool_rx_flow_spec *fsp,
1037			   struct iavf_fdir_fltr *fltr)
1038{
1039	if (!fltr->ext_mask.usr_def[0] && !fltr->ext_mask.usr_def[1])
1040		return;
1041
1042	fsp->flow_type |= FLOW_EXT;
1043
1044	memcpy(fsp->h_ext.data, fltr->ext_data.usr_def, sizeof(fsp->h_ext.data));
1045	memcpy(fsp->m_ext.data, fltr->ext_mask.usr_def, sizeof(fsp->m_ext.data));
1046}
1047
1048/**
1049 * iavf_get_ethtool_fdir_entry - fill ethtool structure with Flow Director filter data
1050 * @adapter: the VF adapter structure that contains filter list
1051 * @cmd: ethtool command data structure to receive the filter data
1052 *
1053 * Returns 0 as expected for success by ethtool
1054 */
1055static int
1056iavf_get_ethtool_fdir_entry(struct iavf_adapter *adapter,
1057			    struct ethtool_rxnfc *cmd)
1058{
1059	struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1060	struct iavf_fdir_fltr *rule = NULL;
1061	int ret = 0;
1062
1063	if (!(adapter->flags & IAVF_FLAG_FDIR_ENABLED))
1064		return -EOPNOTSUPP;
1065
1066	spin_lock_bh(&adapter->fdir_fltr_lock);
1067
1068	rule = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1069	if (!rule) {
1070		ret = -EINVAL;
1071		goto release_lock;
1072	}
1073
1074	fsp->flow_type = iavf_fltr_to_ethtool_flow(rule->flow_type);
1075
1076	memset(&fsp->m_u, 0, sizeof(fsp->m_u));
1077	memset(&fsp->m_ext, 0, sizeof(fsp->m_ext));
1078
1079	switch (fsp->flow_type) {
1080	case TCP_V4_FLOW:
1081	case UDP_V4_FLOW:
1082	case SCTP_V4_FLOW:
1083		fsp->h_u.tcp_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1084		fsp->h_u.tcp_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1085		fsp->h_u.tcp_ip4_spec.psrc = rule->ip_data.src_port;
1086		fsp->h_u.tcp_ip4_spec.pdst = rule->ip_data.dst_port;
1087		fsp->h_u.tcp_ip4_spec.tos = rule->ip_data.tos;
1088		fsp->m_u.tcp_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1089		fsp->m_u.tcp_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1090		fsp->m_u.tcp_ip4_spec.psrc = rule->ip_mask.src_port;
1091		fsp->m_u.tcp_ip4_spec.pdst = rule->ip_mask.dst_port;
1092		fsp->m_u.tcp_ip4_spec.tos = rule->ip_mask.tos;
1093		break;
1094	case AH_V4_FLOW:
1095	case ESP_V4_FLOW:
1096		fsp->h_u.ah_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1097		fsp->h_u.ah_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1098		fsp->h_u.ah_ip4_spec.spi = rule->ip_data.spi;
1099		fsp->h_u.ah_ip4_spec.tos = rule->ip_data.tos;
1100		fsp->m_u.ah_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1101		fsp->m_u.ah_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1102		fsp->m_u.ah_ip4_spec.spi = rule->ip_mask.spi;
1103		fsp->m_u.ah_ip4_spec.tos = rule->ip_mask.tos;
1104		break;
1105	case IPV4_USER_FLOW:
1106		fsp->h_u.usr_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1107		fsp->h_u.usr_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1108		fsp->h_u.usr_ip4_spec.l4_4_bytes = rule->ip_data.l4_header;
1109		fsp->h_u.usr_ip4_spec.tos = rule->ip_data.tos;
1110		fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
1111		fsp->h_u.usr_ip4_spec.proto = rule->ip_data.proto;
1112		fsp->m_u.usr_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1113		fsp->m_u.usr_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1114		fsp->m_u.usr_ip4_spec.l4_4_bytes = rule->ip_mask.l4_header;
1115		fsp->m_u.usr_ip4_spec.tos = rule->ip_mask.tos;
1116		fsp->m_u.usr_ip4_spec.ip_ver = 0xFF;
1117		fsp->m_u.usr_ip4_spec.proto = rule->ip_mask.proto;
1118		break;
1119	case TCP_V6_FLOW:
1120	case UDP_V6_FLOW:
1121	case SCTP_V6_FLOW:
1122		memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1123		       sizeof(struct in6_addr));
1124		memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1125		       sizeof(struct in6_addr));
1126		fsp->h_u.tcp_ip6_spec.psrc = rule->ip_data.src_port;
1127		fsp->h_u.tcp_ip6_spec.pdst = rule->ip_data.dst_port;
1128		fsp->h_u.tcp_ip6_spec.tclass = rule->ip_data.tclass;
1129		memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1130		       sizeof(struct in6_addr));
1131		memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1132		       sizeof(struct in6_addr));
1133		fsp->m_u.tcp_ip6_spec.psrc = rule->ip_mask.src_port;
1134		fsp->m_u.tcp_ip6_spec.pdst = rule->ip_mask.dst_port;
1135		fsp->m_u.tcp_ip6_spec.tclass = rule->ip_mask.tclass;
1136		break;
1137	case AH_V6_FLOW:
1138	case ESP_V6_FLOW:
1139		memcpy(fsp->h_u.ah_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1140		       sizeof(struct in6_addr));
1141		memcpy(fsp->h_u.ah_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1142		       sizeof(struct in6_addr));
1143		fsp->h_u.ah_ip6_spec.spi = rule->ip_data.spi;
1144		fsp->h_u.ah_ip6_spec.tclass = rule->ip_data.tclass;
1145		memcpy(fsp->m_u.ah_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1146		       sizeof(struct in6_addr));
1147		memcpy(fsp->m_u.ah_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1148		       sizeof(struct in6_addr));
1149		fsp->m_u.ah_ip6_spec.spi = rule->ip_mask.spi;
1150		fsp->m_u.ah_ip6_spec.tclass = rule->ip_mask.tclass;
1151		break;
1152	case IPV6_USER_FLOW:
1153		memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1154		       sizeof(struct in6_addr));
1155		memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1156		       sizeof(struct in6_addr));
1157		fsp->h_u.usr_ip6_spec.l4_4_bytes = rule->ip_data.l4_header;
1158		fsp->h_u.usr_ip6_spec.tclass = rule->ip_data.tclass;
1159		fsp->h_u.usr_ip6_spec.l4_proto = rule->ip_data.proto;
1160		memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1161		       sizeof(struct in6_addr));
1162		memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1163		       sizeof(struct in6_addr));
1164		fsp->m_u.usr_ip6_spec.l4_4_bytes = rule->ip_mask.l4_header;
1165		fsp->m_u.usr_ip6_spec.tclass = rule->ip_mask.tclass;
1166		fsp->m_u.usr_ip6_spec.l4_proto = rule->ip_mask.proto;
1167		break;
1168	case ETHER_FLOW:
1169		fsp->h_u.ether_spec.h_proto = rule->eth_data.etype;
1170		fsp->m_u.ether_spec.h_proto = rule->eth_mask.etype;
1171		break;
1172	default:
1173		ret = -EINVAL;
1174		break;
1175	}
1176
1177	iavf_fill_rx_flow_ext_data(fsp, rule);
1178
1179	if (rule->action == VIRTCHNL_ACTION_DROP)
1180		fsp->ring_cookie = RX_CLS_FLOW_DISC;
1181	else
1182		fsp->ring_cookie = rule->q_index;
1183
1184release_lock:
1185	spin_unlock_bh(&adapter->fdir_fltr_lock);
1186	return ret;
1187}
1188
1189/**
1190 * iavf_get_fdir_fltr_ids - fill buffer with filter IDs of active filters
1191 * @adapter: the VF adapter structure containing the filter list
1192 * @cmd: ethtool command data structure
1193 * @rule_locs: ethtool array passed in from OS to receive filter IDs
1194 *
1195 * Returns 0 as expected for success by ethtool
1196 */
1197static int
1198iavf_get_fdir_fltr_ids(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd,
1199		       u32 *rule_locs)
1200{
1201	struct iavf_fdir_fltr *fltr;
1202	unsigned int cnt = 0;
1203	int val = 0;
1204
1205	if (!(adapter->flags & IAVF_FLAG_FDIR_ENABLED))
1206		return -EOPNOTSUPP;
1207
1208	cmd->data = IAVF_MAX_FDIR_FILTERS;
1209
1210	spin_lock_bh(&adapter->fdir_fltr_lock);
1211
1212	list_for_each_entry(fltr, &adapter->fdir_list_head, list) {
1213		if (cnt == cmd->rule_cnt) {
1214			val = -EMSGSIZE;
1215			goto release_lock;
1216		}
1217		rule_locs[cnt] = fltr->loc;
1218		cnt++;
1219	}
1220
1221release_lock:
1222	spin_unlock_bh(&adapter->fdir_fltr_lock);
1223	if (!val)
1224		cmd->rule_cnt = cnt;
1225
1226	return val;
1227}
1228
1229/**
1230 * iavf_add_fdir_fltr_info - Set the input set for Flow Director filter
1231 * @adapter: pointer to the VF adapter structure
1232 * @fsp: pointer to ethtool Rx flow specification
1233 * @fltr: filter structure
1234 */
1235static int
1236iavf_add_fdir_fltr_info(struct iavf_adapter *adapter, struct ethtool_rx_flow_spec *fsp,
1237			struct iavf_fdir_fltr *fltr)
1238{
1239	u32 flow_type, q_index = 0;
1240	enum virtchnl_action act;
1241	int err;
1242
1243	if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
1244		act = VIRTCHNL_ACTION_DROP;
1245	} else {
1246		q_index = fsp->ring_cookie;
1247		if (q_index >= adapter->num_active_queues)
1248			return -EINVAL;
1249
1250		act = VIRTCHNL_ACTION_QUEUE;
1251	}
1252
1253	fltr->action = act;
1254	fltr->loc = fsp->location;
1255	fltr->q_index = q_index;
1256
1257	if (fsp->flow_type & FLOW_EXT) {
1258		memcpy(fltr->ext_data.usr_def, fsp->h_ext.data,
1259		       sizeof(fltr->ext_data.usr_def));
1260		memcpy(fltr->ext_mask.usr_def, fsp->m_ext.data,
1261		       sizeof(fltr->ext_mask.usr_def));
1262	}
1263
1264	flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
1265	fltr->flow_type = iavf_ethtool_flow_to_fltr(flow_type);
1266
1267	switch (flow_type) {
1268	case TCP_V4_FLOW:
1269	case UDP_V4_FLOW:
1270	case SCTP_V4_FLOW:
1271		fltr->ip_data.v4_addrs.src_ip = fsp->h_u.tcp_ip4_spec.ip4src;
1272		fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
1273		fltr->ip_data.src_port = fsp->h_u.tcp_ip4_spec.psrc;
1274		fltr->ip_data.dst_port = fsp->h_u.tcp_ip4_spec.pdst;
1275		fltr->ip_data.tos = fsp->h_u.tcp_ip4_spec.tos;
1276		fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.tcp_ip4_spec.ip4src;
1277		fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.tcp_ip4_spec.ip4dst;
1278		fltr->ip_mask.src_port = fsp->m_u.tcp_ip4_spec.psrc;
1279		fltr->ip_mask.dst_port = fsp->m_u.tcp_ip4_spec.pdst;
1280		fltr->ip_mask.tos = fsp->m_u.tcp_ip4_spec.tos;
1281		fltr->ip_ver = 4;
1282		break;
1283	case AH_V4_FLOW:
1284	case ESP_V4_FLOW:
1285		fltr->ip_data.v4_addrs.src_ip = fsp->h_u.ah_ip4_spec.ip4src;
1286		fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.ah_ip4_spec.ip4dst;
1287		fltr->ip_data.spi = fsp->h_u.ah_ip4_spec.spi;
1288		fltr->ip_data.tos = fsp->h_u.ah_ip4_spec.tos;
1289		fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.ah_ip4_spec.ip4src;
1290		fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.ah_ip4_spec.ip4dst;
1291		fltr->ip_mask.spi = fsp->m_u.ah_ip4_spec.spi;
1292		fltr->ip_mask.tos = fsp->m_u.ah_ip4_spec.tos;
1293		fltr->ip_ver = 4;
1294		break;
1295	case IPV4_USER_FLOW:
1296		fltr->ip_data.v4_addrs.src_ip = fsp->h_u.usr_ip4_spec.ip4src;
1297		fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.usr_ip4_spec.ip4dst;
1298		fltr->ip_data.l4_header = fsp->h_u.usr_ip4_spec.l4_4_bytes;
1299		fltr->ip_data.tos = fsp->h_u.usr_ip4_spec.tos;
1300		fltr->ip_data.proto = fsp->h_u.usr_ip4_spec.proto;
1301		fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.usr_ip4_spec.ip4src;
1302		fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.usr_ip4_spec.ip4dst;
1303		fltr->ip_mask.l4_header = fsp->m_u.usr_ip4_spec.l4_4_bytes;
1304		fltr->ip_mask.tos = fsp->m_u.usr_ip4_spec.tos;
1305		fltr->ip_mask.proto = fsp->m_u.usr_ip4_spec.proto;
1306		fltr->ip_ver = 4;
1307		break;
1308	case TCP_V6_FLOW:
1309	case UDP_V6_FLOW:
1310	case SCTP_V6_FLOW:
1311		memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1312		       sizeof(struct in6_addr));
1313		memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1314		       sizeof(struct in6_addr));
1315		fltr->ip_data.src_port = fsp->h_u.tcp_ip6_spec.psrc;
1316		fltr->ip_data.dst_port = fsp->h_u.tcp_ip6_spec.pdst;
1317		fltr->ip_data.tclass = fsp->h_u.tcp_ip6_spec.tclass;
1318		memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1319		       sizeof(struct in6_addr));
1320		memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1321		       sizeof(struct in6_addr));
1322		fltr->ip_mask.src_port = fsp->m_u.tcp_ip6_spec.psrc;
1323		fltr->ip_mask.dst_port = fsp->m_u.tcp_ip6_spec.pdst;
1324		fltr->ip_mask.tclass = fsp->m_u.tcp_ip6_spec.tclass;
1325		fltr->ip_ver = 6;
1326		break;
1327	case AH_V6_FLOW:
1328	case ESP_V6_FLOW:
1329		memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.ah_ip6_spec.ip6src,
1330		       sizeof(struct in6_addr));
1331		memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.ah_ip6_spec.ip6dst,
1332		       sizeof(struct in6_addr));
1333		fltr->ip_data.spi = fsp->h_u.ah_ip6_spec.spi;
1334		fltr->ip_data.tclass = fsp->h_u.ah_ip6_spec.tclass;
1335		memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.ah_ip6_spec.ip6src,
1336		       sizeof(struct in6_addr));
1337		memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.ah_ip6_spec.ip6dst,
1338		       sizeof(struct in6_addr));
1339		fltr->ip_mask.spi = fsp->m_u.ah_ip6_spec.spi;
1340		fltr->ip_mask.tclass = fsp->m_u.ah_ip6_spec.tclass;
1341		fltr->ip_ver = 6;
1342		break;
1343	case IPV6_USER_FLOW:
1344		memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1345		       sizeof(struct in6_addr));
1346		memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1347		       sizeof(struct in6_addr));
1348		fltr->ip_data.l4_header = fsp->h_u.usr_ip6_spec.l4_4_bytes;
1349		fltr->ip_data.tclass = fsp->h_u.usr_ip6_spec.tclass;
1350		fltr->ip_data.proto = fsp->h_u.usr_ip6_spec.l4_proto;
1351		memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1352		       sizeof(struct in6_addr));
1353		memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1354		       sizeof(struct in6_addr));
1355		fltr->ip_mask.l4_header = fsp->m_u.usr_ip6_spec.l4_4_bytes;
1356		fltr->ip_mask.tclass = fsp->m_u.usr_ip6_spec.tclass;
1357		fltr->ip_mask.proto = fsp->m_u.usr_ip6_spec.l4_proto;
1358		fltr->ip_ver = 6;
1359		break;
1360	case ETHER_FLOW:
1361		fltr->eth_data.etype = fsp->h_u.ether_spec.h_proto;
1362		fltr->eth_mask.etype = fsp->m_u.ether_spec.h_proto;
1363		break;
1364	default:
1365		/* not doing un-parsed flow types */
1366		return -EINVAL;
1367	}
1368
1369	err = iavf_validate_fdir_fltr_masks(adapter, fltr);
1370	if (err)
1371		return err;
1372
1373	if (iavf_fdir_is_dup_fltr(adapter, fltr))
1374		return -EEXIST;
1375
1376	err = iavf_parse_rx_flow_user_data(fsp, fltr);
1377	if (err)
1378		return err;
1379
1380	return iavf_fill_fdir_add_msg(adapter, fltr);
1381}
1382
1383/**
1384 * iavf_add_fdir_ethtool - add Flow Director filter
1385 * @adapter: pointer to the VF adapter structure
1386 * @cmd: command to add Flow Director filter
1387 *
1388 * Returns 0 on success and negative values for failure
1389 */
1390static int iavf_add_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1391{
1392	struct ethtool_rx_flow_spec *fsp = &cmd->fs;
1393	struct iavf_fdir_fltr *fltr;
1394	int count = 50;
1395	int err;
1396
1397	if (!(adapter->flags & IAVF_FLAG_FDIR_ENABLED))
1398		return -EOPNOTSUPP;
1399
1400	if (fsp->flow_type & FLOW_MAC_EXT)
1401		return -EINVAL;
1402
1403	spin_lock_bh(&adapter->fdir_fltr_lock);
1404	if (adapter->fdir_active_fltr >= IAVF_MAX_FDIR_FILTERS) {
1405		spin_unlock_bh(&adapter->fdir_fltr_lock);
1406		dev_err(&adapter->pdev->dev,
1407			"Unable to add Flow Director filter because VF reached the limit of max allowed filters (%u)\n",
1408			IAVF_MAX_FDIR_FILTERS);
1409		return -ENOSPC;
1410	}
1411
1412	if (iavf_find_fdir_fltr_by_loc(adapter, fsp->location)) {
1413		dev_err(&adapter->pdev->dev, "Failed to add Flow Director filter, it already exists\n");
1414		spin_unlock_bh(&adapter->fdir_fltr_lock);
1415		return -EEXIST;
1416	}
1417	spin_unlock_bh(&adapter->fdir_fltr_lock);
1418
1419	fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1420	if (!fltr)
1421		return -ENOMEM;
1422
1423	while (!mutex_trylock(&adapter->crit_lock)) {
1424		if (--count == 0) {
1425			kfree(fltr);
1426			return -EINVAL;
1427		}
1428		udelay(1);
1429	}
1430
1431	err = iavf_add_fdir_fltr_info(adapter, fsp, fltr);
1432	if (err)
1433		goto ret;
1434
1435	spin_lock_bh(&adapter->fdir_fltr_lock);
1436	iavf_fdir_list_add_fltr(adapter, fltr);
1437	adapter->fdir_active_fltr++;
1438
1439	if (adapter->link_up)
1440		fltr->state = IAVF_FDIR_FLTR_ADD_REQUEST;
1441	else
1442		fltr->state = IAVF_FDIR_FLTR_INACTIVE;
1443	spin_unlock_bh(&adapter->fdir_fltr_lock);
1444
1445	if (adapter->link_up)
1446		iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ADD_FDIR_FILTER);
1447ret:
1448	if (err && fltr)
1449		kfree(fltr);
1450
1451	mutex_unlock(&adapter->crit_lock);
1452	return err;
1453}
1454
1455/**
1456 * iavf_del_fdir_ethtool - delete Flow Director filter
1457 * @adapter: pointer to the VF adapter structure
1458 * @cmd: command to delete Flow Director filter
1459 *
1460 * Returns 0 on success and negative values for failure
1461 */
1462static int iavf_del_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1463{
1464	struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1465	struct iavf_fdir_fltr *fltr = NULL;
1466	int err = 0;
1467
1468	if (!(adapter->flags & IAVF_FLAG_FDIR_ENABLED))
1469		return -EOPNOTSUPP;
1470
1471	spin_lock_bh(&adapter->fdir_fltr_lock);
1472	fltr = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1473	if (fltr) {
1474		if (fltr->state == IAVF_FDIR_FLTR_ACTIVE) {
1475			fltr->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1476		} else if (fltr->state == IAVF_FDIR_FLTR_INACTIVE) {
1477			list_del(&fltr->list);
1478			kfree(fltr);
1479			adapter->fdir_active_fltr--;
1480			fltr = NULL;
1481		} else {
1482			err = -EBUSY;
1483		}
1484	} else if (adapter->fdir_active_fltr) {
1485		err = -EINVAL;
1486	}
1487	spin_unlock_bh(&adapter->fdir_fltr_lock);
1488
1489	if (fltr && fltr->state == IAVF_FDIR_FLTR_DEL_REQUEST)
1490		iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_DEL_FDIR_FILTER);
1491
1492	return err;
1493}
1494
1495/**
1496 * iavf_adv_rss_parse_hdrs - parses headers from RSS hash input
1497 * @cmd: ethtool rxnfc command
1498 *
1499 * This function parses the rxnfc command and returns intended
1500 * header types for RSS configuration
1501 */
1502static u32 iavf_adv_rss_parse_hdrs(struct ethtool_rxnfc *cmd)
1503{
1504	u32 hdrs = IAVF_ADV_RSS_FLOW_SEG_HDR_NONE;
1505
1506	switch (cmd->flow_type) {
1507	case TCP_V4_FLOW:
1508		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1509			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1510		break;
1511	case UDP_V4_FLOW:
1512		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1513			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1514		break;
1515	case SCTP_V4_FLOW:
1516		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1517			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1518		break;
1519	case TCP_V6_FLOW:
1520		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1521			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1522		break;
1523	case UDP_V6_FLOW:
1524		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1525			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1526		break;
1527	case SCTP_V6_FLOW:
1528		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1529			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1530		break;
1531	default:
1532		break;
1533	}
1534
1535	return hdrs;
1536}
1537
1538/**
1539 * iavf_adv_rss_parse_hash_flds - parses hash fields from RSS hash input
1540 * @cmd: ethtool rxnfc command
1541 * @symm: true if Symmetric Topelitz is set
1542 *
1543 * This function parses the rxnfc command and returns intended hash fields for
1544 * RSS configuration
1545 */
1546static u64 iavf_adv_rss_parse_hash_flds(struct ethtool_rxnfc *cmd, bool symm)
1547{
1548	u64 hfld = IAVF_ADV_RSS_HASH_INVALID;
1549
1550	if (cmd->data & RXH_IP_SRC || cmd->data & RXH_IP_DST) {
1551		switch (cmd->flow_type) {
1552		case TCP_V4_FLOW:
1553		case UDP_V4_FLOW:
1554		case SCTP_V4_FLOW:
1555			if (cmd->data & RXH_IP_SRC)
1556				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_SA;
1557			if (cmd->data & RXH_IP_DST)
1558				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_DA;
1559			break;
1560		case TCP_V6_FLOW:
1561		case UDP_V6_FLOW:
1562		case SCTP_V6_FLOW:
1563			if (cmd->data & RXH_IP_SRC)
1564				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_SA;
1565			if (cmd->data & RXH_IP_DST)
1566				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_DA;
1567			break;
1568		default:
1569			break;
1570		}
1571	}
1572
1573	if (cmd->data & RXH_L4_B_0_1 || cmd->data & RXH_L4_B_2_3) {
1574		switch (cmd->flow_type) {
1575		case TCP_V4_FLOW:
1576		case TCP_V6_FLOW:
1577			if (cmd->data & RXH_L4_B_0_1)
1578				hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT;
1579			if (cmd->data & RXH_L4_B_2_3)
1580				hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT;
1581			break;
1582		case UDP_V4_FLOW:
1583		case UDP_V6_FLOW:
1584			if (cmd->data & RXH_L4_B_0_1)
1585				hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT;
1586			if (cmd->data & RXH_L4_B_2_3)
1587				hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT;
1588			break;
1589		case SCTP_V4_FLOW:
1590		case SCTP_V6_FLOW:
1591			if (cmd->data & RXH_L4_B_0_1)
1592				hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT;
1593			if (cmd->data & RXH_L4_B_2_3)
1594				hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT;
1595			break;
1596		default:
1597			break;
1598		}
1599	}
1600
1601	return hfld;
1602}
1603
1604/**
1605 * iavf_set_adv_rss_hash_opt - Enable/Disable flow types for RSS hash
1606 * @adapter: pointer to the VF adapter structure
1607 * @cmd: ethtool rxnfc command
1608 *
1609 * Returns Success if the flow input set is supported.
1610 */
1611static int
1612iavf_set_adv_rss_hash_opt(struct iavf_adapter *adapter,
1613			  struct ethtool_rxnfc *cmd)
1614{
1615	struct iavf_adv_rss *rss_old, *rss_new;
1616	bool rss_new_add = false;
1617	int count = 50, err = 0;
1618	bool symm = false;
1619	u64 hash_flds;
1620	u32 hdrs;
1621
1622	if (!ADV_RSS_SUPPORT(adapter))
1623		return -EOPNOTSUPP;
1624
1625	symm = !!(adapter->hfunc == VIRTCHNL_RSS_ALG_TOEPLITZ_SYMMETRIC);
1626
1627	hdrs = iavf_adv_rss_parse_hdrs(cmd);
1628	if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1629		return -EINVAL;
1630
1631	hash_flds = iavf_adv_rss_parse_hash_flds(cmd, symm);
1632	if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1633		return -EINVAL;
1634
1635	rss_new = kzalloc(sizeof(*rss_new), GFP_KERNEL);
1636	if (!rss_new)
1637		return -ENOMEM;
1638
1639	if (iavf_fill_adv_rss_cfg_msg(&rss_new->cfg_msg, hdrs, hash_flds,
1640				      symm)) {
1641		kfree(rss_new);
1642		return -EINVAL;
1643	}
1644
1645	while (!mutex_trylock(&adapter->crit_lock)) {
1646		if (--count == 0) {
1647			kfree(rss_new);
1648			return -EINVAL;
1649		}
1650
1651		udelay(1);
1652	}
1653
1654	spin_lock_bh(&adapter->adv_rss_lock);
1655	rss_old = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1656	if (rss_old) {
1657		if (rss_old->state != IAVF_ADV_RSS_ACTIVE) {
1658			err = -EBUSY;
1659		} else if (rss_old->hash_flds != hash_flds ||
1660			   rss_old->symm != symm) {
1661			rss_old->state = IAVF_ADV_RSS_ADD_REQUEST;
1662			rss_old->hash_flds = hash_flds;
1663			rss_old->symm = symm;
1664			memcpy(&rss_old->cfg_msg, &rss_new->cfg_msg,
1665			       sizeof(rss_new->cfg_msg));
1666		} else {
1667			err = -EEXIST;
1668		}
1669	} else {
1670		rss_new_add = true;
1671		rss_new->state = IAVF_ADV_RSS_ADD_REQUEST;
1672		rss_new->packet_hdrs = hdrs;
1673		rss_new->hash_flds = hash_flds;
1674		rss_new->symm = symm;
1675		list_add_tail(&rss_new->list, &adapter->adv_rss_list_head);
1676	}
1677	spin_unlock_bh(&adapter->adv_rss_lock);
1678
1679	if (!err)
1680		iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ADD_ADV_RSS_CFG);
1681
1682	mutex_unlock(&adapter->crit_lock);
1683
1684	if (!rss_new_add)
1685		kfree(rss_new);
1686
1687	return err;
1688}
1689
1690/**
1691 * iavf_get_adv_rss_hash_opt - Retrieve hash fields for a given flow-type
1692 * @adapter: pointer to the VF adapter structure
1693 * @cmd: ethtool rxnfc command
1694 *
1695 * Returns Success if the flow input set is supported.
1696 */
1697static int
1698iavf_get_adv_rss_hash_opt(struct iavf_adapter *adapter,
1699			  struct ethtool_rxnfc *cmd)
1700{
1701	struct iavf_adv_rss *rss;
1702	u64 hash_flds;
1703	u32 hdrs;
1704
1705	if (!ADV_RSS_SUPPORT(adapter))
1706		return -EOPNOTSUPP;
1707
1708	cmd->data = 0;
1709
1710	hdrs = iavf_adv_rss_parse_hdrs(cmd);
1711	if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1712		return -EINVAL;
1713
1714	spin_lock_bh(&adapter->adv_rss_lock);
1715	rss = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1716	if (rss)
1717		hash_flds = rss->hash_flds;
1718	else
1719		hash_flds = IAVF_ADV_RSS_HASH_INVALID;
1720	spin_unlock_bh(&adapter->adv_rss_lock);
1721
1722	if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1723		return -EINVAL;
1724
1725	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_SA |
1726			 IAVF_ADV_RSS_HASH_FLD_IPV6_SA))
1727		cmd->data |= (u64)RXH_IP_SRC;
1728
1729	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_DA |
1730			 IAVF_ADV_RSS_HASH_FLD_IPV6_DA))
1731		cmd->data |= (u64)RXH_IP_DST;
1732
1733	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT |
1734			 IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT |
1735			 IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT))
1736		cmd->data |= (u64)RXH_L4_B_0_1;
1737
1738	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT |
1739			 IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT |
1740			 IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT))
1741		cmd->data |= (u64)RXH_L4_B_2_3;
1742
1743	return 0;
1744}
1745
1746/**
1747 * iavf_set_rxnfc - command to set Rx flow rules.
1748 * @netdev: network interface device structure
1749 * @cmd: ethtool rxnfc command
1750 *
1751 * Returns 0 for success and negative values for errors
1752 */
1753static int iavf_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1754{
1755	struct iavf_adapter *adapter = netdev_priv(netdev);
1756	int ret = -EOPNOTSUPP;
1757
1758	switch (cmd->cmd) {
1759	case ETHTOOL_SRXCLSRLINS:
1760		ret = iavf_add_fdir_ethtool(adapter, cmd);
1761		break;
1762	case ETHTOOL_SRXCLSRLDEL:
1763		ret = iavf_del_fdir_ethtool(adapter, cmd);
1764		break;
1765	case ETHTOOL_SRXFH:
1766		ret = iavf_set_adv_rss_hash_opt(adapter, cmd);
1767		break;
1768	default:
1769		break;
1770	}
1771
1772	return ret;
1773}
1774
1775/**
1776 * iavf_get_rxnfc - command to get RX flow classification rules
1777 * @netdev: network interface device structure
1778 * @cmd: ethtool rxnfc command
1779 * @rule_locs: pointer to store rule locations
1780 *
1781 * Returns Success if the command is supported.
1782 **/
1783static int iavf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1784			  u32 *rule_locs)
1785{
1786	struct iavf_adapter *adapter = netdev_priv(netdev);
1787	int ret = -EOPNOTSUPP;
1788
1789	switch (cmd->cmd) {
1790	case ETHTOOL_GRXRINGS:
1791		cmd->data = adapter->num_active_queues;
1792		ret = 0;
1793		break;
1794	case ETHTOOL_GRXCLSRLCNT:
1795		if (!(adapter->flags & IAVF_FLAG_FDIR_ENABLED))
1796			break;
1797		spin_lock_bh(&adapter->fdir_fltr_lock);
1798		cmd->rule_cnt = adapter->fdir_active_fltr;
1799		spin_unlock_bh(&adapter->fdir_fltr_lock);
1800		cmd->data = IAVF_MAX_FDIR_FILTERS;
1801		ret = 0;
1802		break;
1803	case ETHTOOL_GRXCLSRULE:
1804		ret = iavf_get_ethtool_fdir_entry(adapter, cmd);
1805		break;
1806	case ETHTOOL_GRXCLSRLALL:
1807		ret = iavf_get_fdir_fltr_ids(adapter, cmd, (u32 *)rule_locs);
1808		break;
1809	case ETHTOOL_GRXFH:
1810		ret = iavf_get_adv_rss_hash_opt(adapter, cmd);
1811		break;
1812	default:
1813		break;
1814	}
1815
1816	return ret;
1817}
1818/**
1819 * iavf_get_channels: get the number of channels supported by the device
1820 * @netdev: network interface device structure
1821 * @ch: channel information structure
1822 *
1823 * For the purposes of our device, we only use combined channels, i.e. a tx/rx
1824 * queue pair. Report one extra channel to match our "other" MSI-X vector.
1825 **/
1826static void iavf_get_channels(struct net_device *netdev,
1827			      struct ethtool_channels *ch)
1828{
1829	struct iavf_adapter *adapter = netdev_priv(netdev);
1830
1831	/* Report maximum channels */
1832	ch->max_combined = adapter->vsi_res->num_queue_pairs;
1833
1834	ch->max_other = NONQ_VECS;
1835	ch->other_count = NONQ_VECS;
1836
1837	ch->combined_count = adapter->num_active_queues;
1838}
1839
1840/**
1841 * iavf_set_channels: set the new channel count
1842 * @netdev: network interface device structure
1843 * @ch: channel information structure
1844 *
1845 * Negotiate a new number of channels with the PF then do a reset.  During
1846 * reset we'll realloc queues and fix the RSS table.  Returns 0 on success,
1847 * negative on failure.
1848 **/
1849static int iavf_set_channels(struct net_device *netdev,
1850			     struct ethtool_channels *ch)
1851{
1852	struct iavf_adapter *adapter = netdev_priv(netdev);
1853	u32 num_req = ch->combined_count;
1854	int ret = 0;
1855
1856	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1857	    adapter->num_tc) {
1858		dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
1859		return -EINVAL;
1860	}
1861
1862	/* All of these should have already been checked by ethtool before this
1863	 * even gets to us, but just to be sure.
1864	 */
1865	if (num_req == 0 || num_req > adapter->vsi_res->num_queue_pairs)
1866		return -EINVAL;
1867
1868	if (num_req == adapter->num_active_queues)
1869		return 0;
1870
1871	if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
1872		return -EINVAL;
1873
1874	adapter->num_req_queues = num_req;
1875	adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1876	iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
1877
1878	ret = iavf_wait_for_reset(adapter);
1879	if (ret)
1880		netdev_warn(netdev, "Changing channel count timeout or interrupted waiting for reset");
1881
1882	return ret;
1883}
1884
1885/**
1886 * iavf_get_rxfh_key_size - get the RSS hash key size
1887 * @netdev: network interface device structure
1888 *
1889 * Returns the table size.
1890 **/
1891static u32 iavf_get_rxfh_key_size(struct net_device *netdev)
1892{
1893	struct iavf_adapter *adapter = netdev_priv(netdev);
1894
1895	return adapter->rss_key_size;
1896}
1897
1898/**
1899 * iavf_get_rxfh_indir_size - get the rx flow hash indirection table size
1900 * @netdev: network interface device structure
1901 *
1902 * Returns the table size.
1903 **/
1904static u32 iavf_get_rxfh_indir_size(struct net_device *netdev)
1905{
1906	struct iavf_adapter *adapter = netdev_priv(netdev);
1907
1908	return adapter->rss_lut_size;
1909}
1910
1911/**
1912 * iavf_get_rxfh - get the rx flow hash indirection table
1913 * @netdev: network interface device structure
1914 * @rxfh: pointer to param struct (indir, key, hfunc)
1915 *
1916 * Reads the indirection table directly from the hardware. Always returns 0.
1917 **/
1918static int iavf_get_rxfh(struct net_device *netdev,
1919			 struct ethtool_rxfh_param *rxfh)
1920{
1921	struct iavf_adapter *adapter = netdev_priv(netdev);
1922	u16 i;
1923
1924	rxfh->hfunc = ETH_RSS_HASH_TOP;
1925	if (adapter->hfunc == VIRTCHNL_RSS_ALG_TOEPLITZ_SYMMETRIC)
1926		rxfh->input_xfrm |= RXH_XFRM_SYM_XOR;
1927
1928	if (rxfh->key)
1929		memcpy(rxfh->key, adapter->rss_key, adapter->rss_key_size);
1930
1931	if (rxfh->indir)
1932		/* Each 32 bits pointed by 'indir' is stored with a lut entry */
1933		for (i = 0; i < adapter->rss_lut_size; i++)
1934			rxfh->indir[i] = (u32)adapter->rss_lut[i];
1935
1936	return 0;
1937}
1938
1939/**
1940 * iavf_set_rxfh - set the rx flow hash indirection table
1941 * @netdev: network interface device structure
1942 * @rxfh: pointer to param struct (indir, key, hfunc)
1943 * @extack: extended ACK from the Netlink message
1944 *
1945 * Returns -EINVAL if the table specifies an invalid queue id, otherwise
1946 * returns 0 after programming the table.
1947 **/
1948static int iavf_set_rxfh(struct net_device *netdev,
1949			 struct ethtool_rxfh_param *rxfh,
1950			 struct netlink_ext_ack *extack)
1951{
1952	struct iavf_adapter *adapter = netdev_priv(netdev);
1953	u16 i;
1954
1955	/* Only support toeplitz hash function */
1956	if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
1957	    rxfh->hfunc != ETH_RSS_HASH_TOP)
1958		return -EOPNOTSUPP;
1959
1960	if ((rxfh->input_xfrm & RXH_XFRM_SYM_XOR) &&
1961	    adapter->hfunc != VIRTCHNL_RSS_ALG_TOEPLITZ_SYMMETRIC) {
1962		if (!ADV_RSS_SUPPORT(adapter))
1963			return -EOPNOTSUPP;
1964		adapter->hfunc = VIRTCHNL_RSS_ALG_TOEPLITZ_SYMMETRIC;
1965		adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_HFUNC;
1966	} else if (!(rxfh->input_xfrm & RXH_XFRM_SYM_XOR) &&
1967		    adapter->hfunc != VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC) {
1968		adapter->hfunc = VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC;
1969		adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_HFUNC;
1970	}
1971
1972	if (!rxfh->key && !rxfh->indir)
1973		return 0;
1974
1975	if (rxfh->key)
1976		memcpy(adapter->rss_key, rxfh->key, adapter->rss_key_size);
1977
1978	if (rxfh->indir) {
1979		/* Each 32 bits pointed by 'indir' is stored with a lut entry */
1980		for (i = 0; i < adapter->rss_lut_size; i++)
1981			adapter->rss_lut[i] = (u8)(rxfh->indir[i]);
1982	}
1983
1984	return iavf_config_rss(adapter);
1985}
1986
1987static const struct ethtool_ops iavf_ethtool_ops = {
1988	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1989				     ETHTOOL_COALESCE_USE_ADAPTIVE,
1990	.cap_rss_sym_xor_supported = true,
1991	.get_drvinfo		= iavf_get_drvinfo,
1992	.get_link		= ethtool_op_get_link,
1993	.get_ringparam		= iavf_get_ringparam,
1994	.set_ringparam		= iavf_set_ringparam,
1995	.get_strings		= iavf_get_strings,
1996	.get_ethtool_stats	= iavf_get_ethtool_stats,
1997	.get_sset_count		= iavf_get_sset_count,
1998	.get_priv_flags		= iavf_get_priv_flags,
1999	.set_priv_flags		= iavf_set_priv_flags,
2000	.get_msglevel		= iavf_get_msglevel,
2001	.set_msglevel		= iavf_set_msglevel,
2002	.get_coalesce		= iavf_get_coalesce,
2003	.set_coalesce		= iavf_set_coalesce,
2004	.get_per_queue_coalesce = iavf_get_per_queue_coalesce,
2005	.set_per_queue_coalesce = iavf_set_per_queue_coalesce,
2006	.set_rxnfc		= iavf_set_rxnfc,
2007	.get_rxnfc		= iavf_get_rxnfc,
2008	.get_rxfh_indir_size	= iavf_get_rxfh_indir_size,
2009	.get_rxfh		= iavf_get_rxfh,
2010	.set_rxfh		= iavf_set_rxfh,
2011	.get_channels		= iavf_get_channels,
2012	.set_channels		= iavf_set_channels,
2013	.get_rxfh_key_size	= iavf_get_rxfh_key_size,
2014	.get_link_ksettings	= iavf_get_link_ksettings,
2015};
2016
2017/**
2018 * iavf_set_ethtool_ops - Initialize ethtool ops struct
2019 * @netdev: network interface device structure
2020 *
2021 * Sets ethtool ops struct in our netdev so that ethtool can call
2022 * our functions.
2023 **/
2024void iavf_set_ethtool_ops(struct net_device *netdev)
2025{
2026	netdev->ethtool_ops = &iavf_ethtool_ops;
2027}