Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright(c) 2013 - 2018 Intel Corporation. */
   3
   4/* ethtool support for iavf */
   5#include "iavf.h"
   6
   7#include <linux/uaccess.h>
   8
   9/* ethtool statistics helpers */
  10
  11/**
  12 * struct iavf_stats - definition for an ethtool statistic
  13 * @stat_string: statistic name to display in ethtool -S output
  14 * @sizeof_stat: the sizeof() the stat, must be no greater than sizeof(u64)
  15 * @stat_offset: offsetof() the stat from a base pointer
  16 *
  17 * This structure defines a statistic to be added to the ethtool stats buffer.
  18 * It defines a statistic as offset from a common base pointer. Stats should
  19 * be defined in constant arrays using the IAVF_STAT macro, with every element
  20 * of the array using the same _type for calculating the sizeof_stat and
  21 * stat_offset.
  22 *
  23 * The @sizeof_stat is expected to be sizeof(u8), sizeof(u16), sizeof(u32) or
  24 * sizeof(u64). Other sizes are not expected and will produce a WARN_ONCE from
  25 * the iavf_add_ethtool_stat() helper function.
  26 *
  27 * The @stat_string is interpreted as a format string, allowing formatted
  28 * values to be inserted while looping over multiple structures for a given
  29 * statistics array. Thus, every statistic string in an array should have the
  30 * same type and number of format specifiers, to be formatted by variadic
  31 * arguments to the iavf_add_stat_string() helper function.
  32 **/
  33struct iavf_stats {
  34	char stat_string[ETH_GSTRING_LEN];
  35	int sizeof_stat;
  36	int stat_offset;
  37};
  38
  39/* Helper macro to define an iavf_stat structure with proper size and type.
  40 * Use this when defining constant statistics arrays. Note that @_type expects
  41 * only a type name and is used multiple times.
  42 */
  43#define IAVF_STAT(_type, _name, _stat) { \
  44	.stat_string = _name, \
  45	.sizeof_stat = FIELD_SIZEOF(_type, _stat), \
  46	.stat_offset = offsetof(_type, _stat) \
  47}
  48
  49/* Helper macro for defining some statistics related to queues */
  50#define IAVF_QUEUE_STAT(_name, _stat) \
  51	IAVF_STAT(struct iavf_ring, _name, _stat)
  52
  53/* Stats associated with a Tx or Rx ring */
  54static const struct iavf_stats iavf_gstrings_queue_stats[] = {
  55	IAVF_QUEUE_STAT("%s-%u.packets", stats.packets),
  56	IAVF_QUEUE_STAT("%s-%u.bytes", stats.bytes),
  57};
  58
  59/**
  60 * iavf_add_one_ethtool_stat - copy the stat into the supplied buffer
  61 * @data: location to store the stat value
  62 * @pointer: basis for where to copy from
  63 * @stat: the stat definition
  64 *
  65 * Copies the stat data defined by the pointer and stat structure pair into
  66 * the memory supplied as data. Used to implement iavf_add_ethtool_stats and
  67 * iavf_add_queue_stats. If the pointer is null, data will be zero'd.
  68 */
  69static void
  70iavf_add_one_ethtool_stat(u64 *data, void *pointer,
  71			  const struct iavf_stats *stat)
  72{
  73	char *p;
  74
  75	if (!pointer) {
  76		/* ensure that the ethtool data buffer is zero'd for any stats
  77		 * which don't have a valid pointer.
  78		 */
  79		*data = 0;
  80		return;
  81	}
  82
  83	p = (char *)pointer + stat->stat_offset;
  84	switch (stat->sizeof_stat) {
  85	case sizeof(u64):
  86		*data = *((u64 *)p);
  87		break;
  88	case sizeof(u32):
  89		*data = *((u32 *)p);
  90		break;
  91	case sizeof(u16):
  92		*data = *((u16 *)p);
  93		break;
  94	case sizeof(u8):
  95		*data = *((u8 *)p);
  96		break;
  97	default:
  98		WARN_ONCE(1, "unexpected stat size for %s",
  99			  stat->stat_string);
 100		*data = 0;
 101	}
 102}
 103
 104/**
 105 * __iavf_add_ethtool_stats - copy stats into the ethtool supplied buffer
 106 * @data: ethtool stats buffer
 107 * @pointer: location to copy stats from
 108 * @stats: array of stats to copy
 109 * @size: the size of the stats definition
 110 *
 111 * Copy the stats defined by the stats array using the pointer as a base into
 112 * the data buffer supplied by ethtool. Updates the data pointer to point to
 113 * the next empty location for successive calls to __iavf_add_ethtool_stats.
 114 * If pointer is null, set the data values to zero and update the pointer to
 115 * skip these stats.
 116 **/
 117static void
 118__iavf_add_ethtool_stats(u64 **data, void *pointer,
 119			 const struct iavf_stats stats[],
 120			 const unsigned int size)
 121{
 122	unsigned int i;
 123
 124	for (i = 0; i < size; i++)
 125		iavf_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
 126}
 127
 128/**
 129 * iavf_add_ethtool_stats - copy stats into ethtool supplied buffer
 130 * @data: ethtool stats buffer
 131 * @pointer: location where stats are stored
 132 * @stats: static const array of stat definitions
 133 *
 134 * Macro to ease the use of __iavf_add_ethtool_stats by taking a static
 135 * constant stats array and passing the ARRAY_SIZE(). This avoids typos by
 136 * ensuring that we pass the size associated with the given stats array.
 137 *
 138 * The parameter @stats is evaluated twice, so parameters with side effects
 139 * should be avoided.
 140 **/
 141#define iavf_add_ethtool_stats(data, pointer, stats) \
 142	__iavf_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
 143
 144/**
 145 * iavf_add_queue_stats - copy queue statistics into supplied buffer
 146 * @data: ethtool stats buffer
 147 * @ring: the ring to copy
 148 *
 149 * Queue statistics must be copied while protected by
 150 * u64_stats_fetch_begin_irq, so we can't directly use iavf_add_ethtool_stats.
 151 * Assumes that queue stats are defined in iavf_gstrings_queue_stats. If the
 152 * ring pointer is null, zero out the queue stat values and update the data
 153 * pointer. Otherwise safely copy the stats from the ring into the supplied
 154 * buffer and update the data pointer when finished.
 155 *
 156 * This function expects to be called while under rcu_read_lock().
 157 **/
 158static void
 159iavf_add_queue_stats(u64 **data, struct iavf_ring *ring)
 160{
 161	const unsigned int size = ARRAY_SIZE(iavf_gstrings_queue_stats);
 162	const struct iavf_stats *stats = iavf_gstrings_queue_stats;
 163	unsigned int start;
 164	unsigned int i;
 165
 166	/* To avoid invalid statistics values, ensure that we keep retrying
 167	 * the copy until we get a consistent value according to
 168	 * u64_stats_fetch_retry_irq. But first, make sure our ring is
 169	 * non-null before attempting to access its syncp.
 170	 */
 171	do {
 172		start = !ring ? 0 : u64_stats_fetch_begin_irq(&ring->syncp);
 173		for (i = 0; i < size; i++)
 174			iavf_add_one_ethtool_stat(&(*data)[i], ring, &stats[i]);
 175	} while (ring && u64_stats_fetch_retry_irq(&ring->syncp, start));
 176
 177	/* Once we successfully copy the stats in, update the data pointer */
 178	*data += size;
 179}
 180
 181/**
 182 * __iavf_add_stat_strings - copy stat strings into ethtool buffer
 183 * @p: ethtool supplied buffer
 184 * @stats: stat definitions array
 185 * @size: size of the stats array
 186 *
 187 * Format and copy the strings described by stats into the buffer pointed at
 188 * by p.
 189 **/
 190static void __iavf_add_stat_strings(u8 **p, const struct iavf_stats stats[],
 191				    const unsigned int size, ...)
 192{
 193	unsigned int i;
 194
 195	for (i = 0; i < size; i++) {
 196		va_list args;
 197
 198		va_start(args, size);
 199		vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
 200		*p += ETH_GSTRING_LEN;
 201		va_end(args);
 202	}
 203}
 204
 205/**
 206 * iavf_add_stat_strings - copy stat strings into ethtool buffer
 207 * @p: ethtool supplied buffer
 208 * @stats: stat definitions array
 209 *
 210 * Format and copy the strings described by the const static stats value into
 211 * the buffer pointed at by p.
 212 *
 213 * The parameter @stats is evaluated twice, so parameters with side effects
 214 * should be avoided. Additionally, stats must be an array such that
 215 * ARRAY_SIZE can be called on it.
 216 **/
 217#define iavf_add_stat_strings(p, stats, ...) \
 218	__iavf_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
 219
 220#define VF_STAT(_name, _stat) \
 221	IAVF_STAT(struct iavf_adapter, _name, _stat)
 222
 223static const struct iavf_stats iavf_gstrings_stats[] = {
 224	VF_STAT("rx_bytes", current_stats.rx_bytes),
 225	VF_STAT("rx_unicast", current_stats.rx_unicast),
 226	VF_STAT("rx_multicast", current_stats.rx_multicast),
 227	VF_STAT("rx_broadcast", current_stats.rx_broadcast),
 228	VF_STAT("rx_discards", current_stats.rx_discards),
 229	VF_STAT("rx_unknown_protocol", current_stats.rx_unknown_protocol),
 230	VF_STAT("tx_bytes", current_stats.tx_bytes),
 231	VF_STAT("tx_unicast", current_stats.tx_unicast),
 232	VF_STAT("tx_multicast", current_stats.tx_multicast),
 233	VF_STAT("tx_broadcast", current_stats.tx_broadcast),
 234	VF_STAT("tx_discards", current_stats.tx_discards),
 235	VF_STAT("tx_errors", current_stats.tx_errors),
 236};
 237
 238#define IAVF_STATS_LEN	ARRAY_SIZE(iavf_gstrings_stats)
 239
 240#define IAVF_QUEUE_STATS_LEN	ARRAY_SIZE(iavf_gstrings_queue_stats)
 241
 242/* For now we have one and only one private flag and it is only defined
 243 * when we have support for the SKIP_CPU_SYNC DMA attribute.  Instead
 244 * of leaving all this code sitting around empty we will strip it unless
 245 * our one private flag is actually available.
 246 */
 247struct iavf_priv_flags {
 248	char flag_string[ETH_GSTRING_LEN];
 249	u32 flag;
 250	bool read_only;
 251};
 252
 253#define IAVF_PRIV_FLAG(_name, _flag, _read_only) { \
 254	.flag_string = _name, \
 255	.flag = _flag, \
 256	.read_only = _read_only, \
 257}
 258
 259static const struct iavf_priv_flags iavf_gstrings_priv_flags[] = {
 260	IAVF_PRIV_FLAG("legacy-rx", IAVF_FLAG_LEGACY_RX, 0),
 261};
 262
 263#define IAVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(iavf_gstrings_priv_flags)
 264
 265/**
 266 * iavf_get_link_ksettings - Get Link Speed and Duplex settings
 267 * @netdev: network interface device structure
 268 * @cmd: ethtool command
 269 *
 270 * Reports speed/duplex settings. Because this is a VF, we don't know what
 271 * kind of link we really have, so we fake it.
 272 **/
 273static int iavf_get_link_ksettings(struct net_device *netdev,
 274				   struct ethtool_link_ksettings *cmd)
 275{
 276	struct iavf_adapter *adapter = netdev_priv(netdev);
 277
 278	ethtool_link_ksettings_zero_link_mode(cmd, supported);
 279	cmd->base.autoneg = AUTONEG_DISABLE;
 280	cmd->base.port = PORT_NONE;
 281	/* Set speed and duplex */
 282	switch (adapter->link_speed) {
 283	case IAVF_LINK_SPEED_40GB:
 284		cmd->base.speed = SPEED_40000;
 285		break;
 286	case IAVF_LINK_SPEED_25GB:
 287#ifdef SPEED_25000
 288		cmd->base.speed = SPEED_25000;
 289#else
 290		netdev_info(netdev,
 291			    "Speed is 25G, display not supported by this version of ethtool.\n");
 292#endif
 293		break;
 294	case IAVF_LINK_SPEED_20GB:
 295		cmd->base.speed = SPEED_20000;
 296		break;
 297	case IAVF_LINK_SPEED_10GB:
 298		cmd->base.speed = SPEED_10000;
 299		break;
 300	case IAVF_LINK_SPEED_1GB:
 301		cmd->base.speed = SPEED_1000;
 302		break;
 303	case IAVF_LINK_SPEED_100MB:
 304		cmd->base.speed = SPEED_100;
 305		break;
 306	default:
 307		break;
 308	}
 309	cmd->base.duplex = DUPLEX_FULL;
 310
 311	return 0;
 312}
 313
 314/**
 315 * iavf_get_sset_count - Get length of string set
 316 * @netdev: network interface device structure
 317 * @sset: id of string set
 318 *
 319 * Reports size of various string tables.
 320 **/
 321static int iavf_get_sset_count(struct net_device *netdev, int sset)
 322{
 323	if (sset == ETH_SS_STATS)
 324		return IAVF_STATS_LEN +
 325			(IAVF_QUEUE_STATS_LEN * 2 * IAVF_MAX_REQ_QUEUES);
 326	else if (sset == ETH_SS_PRIV_FLAGS)
 327		return IAVF_PRIV_FLAGS_STR_LEN;
 328	else
 329		return -EINVAL;
 330}
 331
 332/**
 333 * iavf_get_ethtool_stats - report device statistics
 334 * @netdev: network interface device structure
 335 * @stats: ethtool statistics structure
 336 * @data: pointer to data buffer
 337 *
 338 * All statistics are added to the data buffer as an array of u64.
 339 **/
 340static void iavf_get_ethtool_stats(struct net_device *netdev,
 341				   struct ethtool_stats *stats, u64 *data)
 342{
 343	struct iavf_adapter *adapter = netdev_priv(netdev);
 344	unsigned int i;
 345
 346	iavf_add_ethtool_stats(&data, adapter, iavf_gstrings_stats);
 347
 348	rcu_read_lock();
 349	for (i = 0; i < IAVF_MAX_REQ_QUEUES; i++) {
 350		struct iavf_ring *ring;
 351
 352		/* Avoid accessing un-allocated queues */
 353		ring = (i < adapter->num_active_queues ?
 354			&adapter->tx_rings[i] : NULL);
 355		iavf_add_queue_stats(&data, ring);
 356
 357		/* Avoid accessing un-allocated queues */
 358		ring = (i < adapter->num_active_queues ?
 359			&adapter->rx_rings[i] : NULL);
 360		iavf_add_queue_stats(&data, ring);
 361	}
 362	rcu_read_unlock();
 363}
 364
 365/**
 366 * iavf_get_priv_flag_strings - Get private flag strings
 367 * @netdev: network interface device structure
 368 * @data: buffer for string data
 369 *
 370 * Builds the private flags string table
 371 **/
 372static void iavf_get_priv_flag_strings(struct net_device *netdev, u8 *data)
 373{
 374	unsigned int i;
 375
 376	for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
 377		snprintf(data, ETH_GSTRING_LEN, "%s",
 378			 iavf_gstrings_priv_flags[i].flag_string);
 379		data += ETH_GSTRING_LEN;
 380	}
 381}
 382
 383/**
 384 * iavf_get_stat_strings - Get stat strings
 385 * @netdev: network interface device structure
 386 * @data: buffer for string data
 387 *
 388 * Builds the statistics string table
 389 **/
 390static void iavf_get_stat_strings(struct net_device *netdev, u8 *data)
 391{
 392	unsigned int i;
 393
 394	iavf_add_stat_strings(&data, iavf_gstrings_stats);
 395
 396	/* Queues are always allocated in pairs, so we just use num_tx_queues
 397	 * for both Tx and Rx queues.
 398	 */
 399	for (i = 0; i < netdev->num_tx_queues; i++) {
 400		iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
 401				      "tx", i);
 402		iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
 403				      "rx", i);
 404	}
 405}
 406
 407/**
 408 * iavf_get_strings - Get string set
 409 * @netdev: network interface device structure
 410 * @sset: id of string set
 411 * @data: buffer for string data
 412 *
 413 * Builds string tables for various string sets
 414 **/
 415static void iavf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
 416{
 417	switch (sset) {
 418	case ETH_SS_STATS:
 419		iavf_get_stat_strings(netdev, data);
 420		break;
 421	case ETH_SS_PRIV_FLAGS:
 422		iavf_get_priv_flag_strings(netdev, data);
 423		break;
 424	default:
 425		break;
 426	}
 427}
 428
 429/**
 430 * iavf_get_priv_flags - report device private flags
 431 * @netdev: network interface device structure
 432 *
 433 * The get string set count and the string set should be matched for each
 434 * flag returned.  Add new strings for each flag to the iavf_gstrings_priv_flags
 435 * array.
 436 *
 437 * Returns a u32 bitmap of flags.
 438 **/
 439static u32 iavf_get_priv_flags(struct net_device *netdev)
 440{
 441	struct iavf_adapter *adapter = netdev_priv(netdev);
 442	u32 i, ret_flags = 0;
 443
 444	for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
 445		const struct iavf_priv_flags *priv_flags;
 446
 447		priv_flags = &iavf_gstrings_priv_flags[i];
 448
 449		if (priv_flags->flag & adapter->flags)
 450			ret_flags |= BIT(i);
 451	}
 452
 453	return ret_flags;
 454}
 455
 456/**
 457 * iavf_set_priv_flags - set private flags
 458 * @netdev: network interface device structure
 459 * @flags: bit flags to be set
 460 **/
 461static int iavf_set_priv_flags(struct net_device *netdev, u32 flags)
 462{
 463	struct iavf_adapter *adapter = netdev_priv(netdev);
 464	u32 orig_flags, new_flags, changed_flags;
 465	u32 i;
 466
 467	orig_flags = READ_ONCE(adapter->flags);
 468	new_flags = orig_flags;
 469
 470	for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
 471		const struct iavf_priv_flags *priv_flags;
 472
 473		priv_flags = &iavf_gstrings_priv_flags[i];
 474
 475		if (flags & BIT(i))
 476			new_flags |= priv_flags->flag;
 477		else
 478			new_flags &= ~(priv_flags->flag);
 479
 480		if (priv_flags->read_only &&
 481		    ((orig_flags ^ new_flags) & ~BIT(i)))
 482			return -EOPNOTSUPP;
 483	}
 484
 485	/* Before we finalize any flag changes, any checks which we need to
 486	 * perform to determine if the new flags will be supported should go
 487	 * here...
 488	 */
 489
 490	/* Compare and exchange the new flags into place. If we failed, that
 491	 * is if cmpxchg returns anything but the old value, this means
 492	 * something else must have modified the flags variable since we
 493	 * copied it. We'll just punt with an error and log something in the
 494	 * message buffer.
 495	 */
 496	if (cmpxchg(&adapter->flags, orig_flags, new_flags) != orig_flags) {
 497		dev_warn(&adapter->pdev->dev,
 498			 "Unable to update adapter->flags as it was modified by another thread...\n");
 499		return -EAGAIN;
 500	}
 501
 502	changed_flags = orig_flags ^ new_flags;
 503
 504	/* Process any additional changes needed as a result of flag changes.
 505	 * The changed_flags value reflects the list of bits that were changed
 506	 * in the code above.
 507	 */
 508
 509	/* issue a reset to force legacy-rx change to take effect */
 510	if (changed_flags & IAVF_FLAG_LEGACY_RX) {
 511		if (netif_running(netdev)) {
 512			adapter->flags |= IAVF_FLAG_RESET_NEEDED;
 513			queue_work(iavf_wq, &adapter->reset_task);
 514		}
 515	}
 516
 517	return 0;
 518}
 519
 520/**
 521 * iavf_get_msglevel - Get debug message level
 522 * @netdev: network interface device structure
 523 *
 524 * Returns current debug message level.
 525 **/
 526static u32 iavf_get_msglevel(struct net_device *netdev)
 527{
 528	struct iavf_adapter *adapter = netdev_priv(netdev);
 529
 530	return adapter->msg_enable;
 531}
 532
 533/**
 534 * iavf_set_msglevel - Set debug message level
 535 * @netdev: network interface device structure
 536 * @data: message level
 537 *
 538 * Set current debug message level. Higher values cause the driver to
 539 * be noisier.
 540 **/
 541static void iavf_set_msglevel(struct net_device *netdev, u32 data)
 542{
 543	struct iavf_adapter *adapter = netdev_priv(netdev);
 544
 545	if (IAVF_DEBUG_USER & data)
 546		adapter->hw.debug_mask = data;
 547	adapter->msg_enable = data;
 548}
 549
 550/**
 551 * iavf_get_drvinfo - Get driver info
 552 * @netdev: network interface device structure
 553 * @drvinfo: ethool driver info structure
 554 *
 555 * Returns information about the driver and device for display to the user.
 556 **/
 557static void iavf_get_drvinfo(struct net_device *netdev,
 558			     struct ethtool_drvinfo *drvinfo)
 559{
 560	struct iavf_adapter *adapter = netdev_priv(netdev);
 561
 562	strlcpy(drvinfo->driver, iavf_driver_name, 32);
 563	strlcpy(drvinfo->version, iavf_driver_version, 32);
 564	strlcpy(drvinfo->fw_version, "N/A", 4);
 565	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
 566	drvinfo->n_priv_flags = IAVF_PRIV_FLAGS_STR_LEN;
 567}
 568
 569/**
 570 * iavf_get_ringparam - Get ring parameters
 571 * @netdev: network interface device structure
 572 * @ring: ethtool ringparam structure
 573 *
 574 * Returns current ring parameters. TX and RX rings are reported separately,
 575 * but the number of rings is not reported.
 576 **/
 577static void iavf_get_ringparam(struct net_device *netdev,
 578			       struct ethtool_ringparam *ring)
 579{
 580	struct iavf_adapter *adapter = netdev_priv(netdev);
 581
 582	ring->rx_max_pending = IAVF_MAX_RXD;
 583	ring->tx_max_pending = IAVF_MAX_TXD;
 584	ring->rx_pending = adapter->rx_desc_count;
 585	ring->tx_pending = adapter->tx_desc_count;
 586}
 587
 588/**
 589 * iavf_set_ringparam - Set ring parameters
 590 * @netdev: network interface device structure
 591 * @ring: ethtool ringparam structure
 592 *
 593 * Sets ring parameters. TX and RX rings are controlled separately, but the
 594 * number of rings is not specified, so all rings get the same settings.
 595 **/
 596static int iavf_set_ringparam(struct net_device *netdev,
 597			      struct ethtool_ringparam *ring)
 598{
 599	struct iavf_adapter *adapter = netdev_priv(netdev);
 600	u32 new_rx_count, new_tx_count;
 601
 602	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
 603		return -EINVAL;
 604
 605	new_tx_count = clamp_t(u32, ring->tx_pending,
 606			       IAVF_MIN_TXD,
 607			       IAVF_MAX_TXD);
 608	new_tx_count = ALIGN(new_tx_count, IAVF_REQ_DESCRIPTOR_MULTIPLE);
 609
 610	new_rx_count = clamp_t(u32, ring->rx_pending,
 611			       IAVF_MIN_RXD,
 612			       IAVF_MAX_RXD);
 613	new_rx_count = ALIGN(new_rx_count, IAVF_REQ_DESCRIPTOR_MULTIPLE);
 614
 615	/* if nothing to do return success */
 616	if ((new_tx_count == adapter->tx_desc_count) &&
 617	    (new_rx_count == adapter->rx_desc_count))
 618		return 0;
 619
 620	adapter->tx_desc_count = new_tx_count;
 621	adapter->rx_desc_count = new_rx_count;
 622
 623	if (netif_running(netdev)) {
 624		adapter->flags |= IAVF_FLAG_RESET_NEEDED;
 625		queue_work(iavf_wq, &adapter->reset_task);
 626	}
 627
 628	return 0;
 629}
 630
 631/**
 632 * __iavf_get_coalesce - get per-queue coalesce settings
 633 * @netdev: the netdev to check
 634 * @ec: ethtool coalesce data structure
 635 * @queue: which queue to pick
 636 *
 637 * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
 638 * are per queue. If queue is <0 then we default to queue 0 as the
 639 * representative value.
 640 **/
 641static int __iavf_get_coalesce(struct net_device *netdev,
 642			       struct ethtool_coalesce *ec, int queue)
 643{
 644	struct iavf_adapter *adapter = netdev_priv(netdev);
 645	struct iavf_vsi *vsi = &adapter->vsi;
 646	struct iavf_ring *rx_ring, *tx_ring;
 647
 648	ec->tx_max_coalesced_frames = vsi->work_limit;
 649	ec->rx_max_coalesced_frames = vsi->work_limit;
 650
 651	/* Rx and Tx usecs per queue value. If user doesn't specify the
 652	 * queue, return queue 0's value to represent.
 653	 */
 654	if (queue < 0)
 655		queue = 0;
 656	else if (queue >= adapter->num_active_queues)
 657		return -EINVAL;
 658
 659	rx_ring = &adapter->rx_rings[queue];
 660	tx_ring = &adapter->tx_rings[queue];
 661
 662	if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
 663		ec->use_adaptive_rx_coalesce = 1;
 664
 665	if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
 666		ec->use_adaptive_tx_coalesce = 1;
 667
 668	ec->rx_coalesce_usecs = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
 669	ec->tx_coalesce_usecs = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
 670
 671	return 0;
 672}
 673
 674/**
 675 * iavf_get_coalesce - Get interrupt coalescing settings
 676 * @netdev: network interface device structure
 677 * @ec: ethtool coalesce structure
 678 *
 679 * Returns current coalescing settings. This is referred to elsewhere in the
 680 * driver as Interrupt Throttle Rate, as this is how the hardware describes
 681 * this functionality. Note that if per-queue settings have been modified this
 682 * only represents the settings of queue 0.
 683 **/
 684static int iavf_get_coalesce(struct net_device *netdev,
 685			     struct ethtool_coalesce *ec)
 686{
 687	return __iavf_get_coalesce(netdev, ec, -1);
 688}
 689
 690/**
 691 * iavf_get_per_queue_coalesce - get coalesce values for specific queue
 692 * @netdev: netdev to read
 693 * @ec: coalesce settings from ethtool
 694 * @queue: the queue to read
 695 *
 696 * Read specific queue's coalesce settings.
 697 **/
 698static int iavf_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
 699				       struct ethtool_coalesce *ec)
 700{
 701	return __iavf_get_coalesce(netdev, ec, queue);
 702}
 703
 704/**
 705 * iavf_set_itr_per_queue - set ITR values for specific queue
 706 * @adapter: the VF adapter struct to set values for
 707 * @ec: coalesce settings from ethtool
 708 * @queue: the queue to modify
 709 *
 710 * Change the ITR settings for a specific queue.
 711 **/
 712static void iavf_set_itr_per_queue(struct iavf_adapter *adapter,
 713				   struct ethtool_coalesce *ec, int queue)
 714{
 715	struct iavf_ring *rx_ring = &adapter->rx_rings[queue];
 716	struct iavf_ring *tx_ring = &adapter->tx_rings[queue];
 717	struct iavf_q_vector *q_vector;
 718
 719	rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
 720	tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
 721
 722	rx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
 723	if (!ec->use_adaptive_rx_coalesce)
 724		rx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
 725
 726	tx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
 727	if (!ec->use_adaptive_tx_coalesce)
 728		tx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
 729
 730	q_vector = rx_ring->q_vector;
 731	q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
 732
 733	q_vector = tx_ring->q_vector;
 734	q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
 735
 736	/* The interrupt handler itself will take care of programming
 737	 * the Tx and Rx ITR values based on the values we have entered
 738	 * into the q_vector, no need to write the values now.
 739	 */
 740}
 741
 742/**
 743 * __iavf_set_coalesce - set coalesce settings for particular queue
 744 * @netdev: the netdev to change
 745 * @ec: ethtool coalesce settings
 746 * @queue: the queue to change
 747 *
 748 * Sets the coalesce settings for a particular queue.
 749 **/
 750static int __iavf_set_coalesce(struct net_device *netdev,
 751			       struct ethtool_coalesce *ec, int queue)
 752{
 753	struct iavf_adapter *adapter = netdev_priv(netdev);
 754	struct iavf_vsi *vsi = &adapter->vsi;
 755	int i;
 756
 757	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
 758		vsi->work_limit = ec->tx_max_coalesced_frames_irq;
 759
 760	if (ec->rx_coalesce_usecs == 0) {
 761		if (ec->use_adaptive_rx_coalesce)
 762			netif_info(adapter, drv, netdev, "rx-usecs=0, need to disable adaptive-rx for a complete disable\n");
 763	} else if ((ec->rx_coalesce_usecs < IAVF_MIN_ITR) ||
 764		   (ec->rx_coalesce_usecs > IAVF_MAX_ITR)) {
 765		netif_info(adapter, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
 766		return -EINVAL;
 767	} else if (ec->tx_coalesce_usecs == 0) {
 768		if (ec->use_adaptive_tx_coalesce)
 769			netif_info(adapter, drv, netdev, "tx-usecs=0, need to disable adaptive-tx for a complete disable\n");
 770	} else if ((ec->tx_coalesce_usecs < IAVF_MIN_ITR) ||
 771		   (ec->tx_coalesce_usecs > IAVF_MAX_ITR)) {
 772		netif_info(adapter, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
 773		return -EINVAL;
 774	}
 775
 776	/* Rx and Tx usecs has per queue value. If user doesn't specify the
 777	 * queue, apply to all queues.
 778	 */
 779	if (queue < 0) {
 780		for (i = 0; i < adapter->num_active_queues; i++)
 781			iavf_set_itr_per_queue(adapter, ec, i);
 782	} else if (queue < adapter->num_active_queues) {
 783		iavf_set_itr_per_queue(adapter, ec, queue);
 784	} else {
 785		netif_info(adapter, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
 786			   adapter->num_active_queues - 1);
 787		return -EINVAL;
 788	}
 789
 790	return 0;
 791}
 792
 793/**
 794 * iavf_set_coalesce - Set interrupt coalescing settings
 795 * @netdev: network interface device structure
 796 * @ec: ethtool coalesce structure
 797 *
 798 * Change current coalescing settings for every queue.
 799 **/
 800static int iavf_set_coalesce(struct net_device *netdev,
 801			     struct ethtool_coalesce *ec)
 802{
 803	return __iavf_set_coalesce(netdev, ec, -1);
 804}
 805
 806/**
 807 * iavf_set_per_queue_coalesce - set specific queue's coalesce settings
 808 * @netdev: the netdev to change
 809 * @ec: ethtool's coalesce settings
 810 * @queue: the queue to modify
 811 *
 812 * Modifies a specific queue's coalesce settings.
 813 */
 814static int iavf_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
 815				       struct ethtool_coalesce *ec)
 816{
 817	return __iavf_set_coalesce(netdev, ec, queue);
 818}
 819
 820/**
 821 * iavf_get_rxnfc - command to get RX flow classification rules
 822 * @netdev: network interface device structure
 823 * @cmd: ethtool rxnfc command
 824 * @rule_locs: pointer to store rule locations
 825 *
 826 * Returns Success if the command is supported.
 827 **/
 828static int iavf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
 829			  u32 *rule_locs)
 830{
 831	struct iavf_adapter *adapter = netdev_priv(netdev);
 832	int ret = -EOPNOTSUPP;
 833
 834	switch (cmd->cmd) {
 835	case ETHTOOL_GRXRINGS:
 836		cmd->data = adapter->num_active_queues;
 837		ret = 0;
 838		break;
 839	case ETHTOOL_GRXFH:
 840		netdev_info(netdev,
 841			    "RSS hash info is not available to vf, use pf.\n");
 842		break;
 843	default:
 844		break;
 845	}
 846
 847	return ret;
 848}
 849/**
 850 * iavf_get_channels: get the number of channels supported by the device
 851 * @netdev: network interface device structure
 852 * @ch: channel information structure
 853 *
 854 * For the purposes of our device, we only use combined channels, i.e. a tx/rx
 855 * queue pair. Report one extra channel to match our "other" MSI-X vector.
 856 **/
 857static void iavf_get_channels(struct net_device *netdev,
 858			      struct ethtool_channels *ch)
 859{
 860	struct iavf_adapter *adapter = netdev_priv(netdev);
 861
 862	/* Report maximum channels */
 863	ch->max_combined = IAVF_MAX_REQ_QUEUES;
 864
 865	ch->max_other = NONQ_VECS;
 866	ch->other_count = NONQ_VECS;
 867
 868	ch->combined_count = adapter->num_active_queues;
 869}
 870
 871/**
 872 * iavf_set_channels: set the new channel count
 873 * @netdev: network interface device structure
 874 * @ch: channel information structure
 875 *
 876 * Negotiate a new number of channels with the PF then do a reset.  During
 877 * reset we'll realloc queues and fix the RSS table.  Returns 0 on success,
 878 * negative on failure.
 879 **/
 880static int iavf_set_channels(struct net_device *netdev,
 881			     struct ethtool_channels *ch)
 882{
 883	struct iavf_adapter *adapter = netdev_priv(netdev);
 884	int num_req = ch->combined_count;
 885
 886	if (num_req != adapter->num_active_queues &&
 887	    !(adapter->vf_res->vf_cap_flags &
 888	      VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)) {
 889		dev_info(&adapter->pdev->dev, "PF is not capable of queue negotiation.\n");
 890		return -EINVAL;
 891	}
 892
 893	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
 894	    adapter->num_tc) {
 895		dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
 896		return -EINVAL;
 897	}
 898
 899	/* All of these should have already been checked by ethtool before this
 900	 * even gets to us, but just to be sure.
 901	 */
 902	if (num_req <= 0 || num_req > IAVF_MAX_REQ_QUEUES)
 903		return -EINVAL;
 904
 905	if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
 906		return -EINVAL;
 907
 908	adapter->num_req_queues = num_req;
 909	return iavf_request_queues(adapter, num_req);
 910}
 911
 912/**
 913 * iavf_get_rxfh_key_size - get the RSS hash key size
 914 * @netdev: network interface device structure
 915 *
 916 * Returns the table size.
 917 **/
 918static u32 iavf_get_rxfh_key_size(struct net_device *netdev)
 919{
 920	struct iavf_adapter *adapter = netdev_priv(netdev);
 921
 922	return adapter->rss_key_size;
 923}
 924
 925/**
 926 * iavf_get_rxfh_indir_size - get the rx flow hash indirection table size
 927 * @netdev: network interface device structure
 928 *
 929 * Returns the table size.
 930 **/
 931static u32 iavf_get_rxfh_indir_size(struct net_device *netdev)
 932{
 933	struct iavf_adapter *adapter = netdev_priv(netdev);
 934
 935	return adapter->rss_lut_size;
 936}
 937
 938/**
 939 * iavf_get_rxfh - get the rx flow hash indirection table
 940 * @netdev: network interface device structure
 941 * @indir: indirection table
 942 * @key: hash key
 943 * @hfunc: hash function in use
 944 *
 945 * Reads the indirection table directly from the hardware. Always returns 0.
 946 **/
 947static int iavf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
 948			 u8 *hfunc)
 949{
 950	struct iavf_adapter *adapter = netdev_priv(netdev);
 951	u16 i;
 952
 953	if (hfunc)
 954		*hfunc = ETH_RSS_HASH_TOP;
 955	if (!indir)
 956		return 0;
 957
 958	memcpy(key, adapter->rss_key, adapter->rss_key_size);
 959
 960	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
 961	for (i = 0; i < adapter->rss_lut_size; i++)
 962		indir[i] = (u32)adapter->rss_lut[i];
 963
 964	return 0;
 965}
 966
 967/**
 968 * iavf_set_rxfh - set the rx flow hash indirection table
 969 * @netdev: network interface device structure
 970 * @indir: indirection table
 971 * @key: hash key
 972 * @hfunc: hash function to use
 973 *
 974 * Returns -EINVAL if the table specifies an inavlid queue id, otherwise
 975 * returns 0 after programming the table.
 976 **/
 977static int iavf_set_rxfh(struct net_device *netdev, const u32 *indir,
 978			 const u8 *key, const u8 hfunc)
 979{
 980	struct iavf_adapter *adapter = netdev_priv(netdev);
 981	u16 i;
 982
 983	/* We do not allow change in unsupported parameters */
 984	if (key ||
 985	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
 986		return -EOPNOTSUPP;
 987	if (!indir)
 988		return 0;
 989
 990	if (key)
 991		memcpy(adapter->rss_key, key, adapter->rss_key_size);
 992
 993	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
 994	for (i = 0; i < adapter->rss_lut_size; i++)
 995		adapter->rss_lut[i] = (u8)(indir[i]);
 996
 997	return iavf_config_rss(adapter);
 998}
 999
1000static const struct ethtool_ops iavf_ethtool_ops = {
1001	.get_drvinfo		= iavf_get_drvinfo,
1002	.get_link		= ethtool_op_get_link,
1003	.get_ringparam		= iavf_get_ringparam,
1004	.set_ringparam		= iavf_set_ringparam,
1005	.get_strings		= iavf_get_strings,
1006	.get_ethtool_stats	= iavf_get_ethtool_stats,
1007	.get_sset_count		= iavf_get_sset_count,
1008	.get_priv_flags		= iavf_get_priv_flags,
1009	.set_priv_flags		= iavf_set_priv_flags,
1010	.get_msglevel		= iavf_get_msglevel,
1011	.set_msglevel		= iavf_set_msglevel,
1012	.get_coalesce		= iavf_get_coalesce,
1013	.set_coalesce		= iavf_set_coalesce,
1014	.get_per_queue_coalesce = iavf_get_per_queue_coalesce,
1015	.set_per_queue_coalesce = iavf_set_per_queue_coalesce,
1016	.get_rxnfc		= iavf_get_rxnfc,
1017	.get_rxfh_indir_size	= iavf_get_rxfh_indir_size,
1018	.get_rxfh		= iavf_get_rxfh,
1019	.set_rxfh		= iavf_set_rxfh,
1020	.get_channels		= iavf_get_channels,
1021	.set_channels		= iavf_set_channels,
1022	.get_rxfh_key_size	= iavf_get_rxfh_key_size,
1023	.get_link_ksettings	= iavf_get_link_ksettings,
1024};
1025
1026/**
1027 * iavf_set_ethtool_ops - Initialize ethtool ops struct
1028 * @netdev: network interface device structure
1029 *
1030 * Sets ethtool ops struct in our netdev so that ethtool can call
1031 * our functions.
1032 **/
1033void iavf_set_ethtool_ops(struct net_device *netdev)
1034{
1035	netdev->ethtool_ops = &iavf_ethtool_ops;
1036}