1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
4 /* ethtool support for iavf */
7 #include <linux/uaccess.h>
9 /* ethtool statistics helpers */
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
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
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.
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.
34 char stat_string[ETH_GSTRING_LEN];
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.
43 #define IAVF_STAT(_type, _name, _stat) { \
44 .stat_string = _name, \
45 .sizeof_stat = sizeof_field(_type, _stat), \
46 .stat_offset = offsetof(_type, _stat) \
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)
53 /* Stats associated with a Tx or Rx ring */
54 static 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),
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
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.
70 iavf_add_one_ethtool_stat(u64 *data, void *pointer,
71 const struct iavf_stats *stat)
76 /* ensure that the ethtool data buffer is zero'd for any stats
77 * which don't have a valid pointer.
83 p = (char *)pointer + stat->stat_offset;
84 switch (stat->sizeof_stat) {
98 WARN_ONCE(1, "unexpected stat size for %s",
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
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
118 __iavf_add_ethtool_stats(u64 **data, void *pointer,
119 const struct iavf_stats stats[],
120 const unsigned int size)
124 for (i = 0; i < size; i++)
125 iavf_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
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
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.
138 * The parameter @stats is evaluated twice, so parameters with side effects
141 #define iavf_add_ethtool_stats(data, pointer, stats) \
142 __iavf_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
145 * iavf_add_queue_stats - copy queue statistics into supplied buffer
146 * @data: ethtool stats buffer
147 * @ring: the ring to copy
149 * Queue statistics must be copied while protected by
150 * u64_stats_fetch_begin, 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.
156 * This function expects to be called while under rcu_read_lock().
159 iavf_add_queue_stats(u64 **data, struct iavf_ring *ring)
161 const unsigned int size = ARRAY_SIZE(iavf_gstrings_queue_stats);
162 const struct iavf_stats *stats = iavf_gstrings_queue_stats;
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. But first, make sure our ring is
169 * non-null before attempting to access its syncp.
172 start = !ring ? 0 : u64_stats_fetch_begin(&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(&ring->syncp, start));
177 /* Once we successfully copy the stats in, update the data pointer */
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
187 * Format and copy the strings described by stats into the buffer pointed at
190 static void __iavf_add_stat_strings(u8 **p, const struct iavf_stats stats[],
191 const unsigned int size, ...)
195 for (i = 0; i < size; i++) {
198 va_start(args, size);
199 vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
200 *p += ETH_GSTRING_LEN;
206 * iavf_add_stat_strings - copy stat strings into ethtool buffer
207 * @p: ethtool supplied buffer
208 * @stats: stat definitions array
210 * Format and copy the strings described by the const static stats value into
211 * the buffer pointed at by p.
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.
217 #define iavf_add_stat_strings(p, stats, ...) \
218 __iavf_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
220 #define VF_STAT(_name, _stat) \
221 IAVF_STAT(struct iavf_adapter, _name, _stat)
223 static 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),
238 #define IAVF_STATS_LEN ARRAY_SIZE(iavf_gstrings_stats)
240 #define IAVF_QUEUE_STATS_LEN ARRAY_SIZE(iavf_gstrings_queue_stats)
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.
247 struct iavf_priv_flags {
248 char flag_string[ETH_GSTRING_LEN];
253 #define IAVF_PRIV_FLAG(_name, _flag, _read_only) { \
254 .flag_string = _name, \
256 .read_only = _read_only, \
259 static const struct iavf_priv_flags iavf_gstrings_priv_flags[] = {
260 IAVF_PRIV_FLAG("legacy-rx", IAVF_FLAG_LEGACY_RX, 0),
263 #define IAVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(iavf_gstrings_priv_flags)
266 * iavf_get_link_ksettings - Get Link Speed and Duplex settings
267 * @netdev: network interface device structure
268 * @cmd: ethtool command
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.
273 static int iavf_get_link_ksettings(struct net_device *netdev,
274 struct ethtool_link_ksettings *cmd)
276 struct iavf_adapter *adapter = netdev_priv(netdev);
278 ethtool_link_ksettings_zero_link_mode(cmd, supported);
279 cmd->base.autoneg = AUTONEG_DISABLE;
280 cmd->base.port = PORT_NONE;
281 cmd->base.duplex = DUPLEX_FULL;
283 if (ADV_LINK_SUPPORT(adapter)) {
284 if (adapter->link_speed_mbps &&
285 adapter->link_speed_mbps < U32_MAX)
286 cmd->base.speed = adapter->link_speed_mbps;
288 cmd->base.speed = SPEED_UNKNOWN;
293 switch (adapter->link_speed) {
294 case VIRTCHNL_LINK_SPEED_40GB:
295 cmd->base.speed = SPEED_40000;
297 case VIRTCHNL_LINK_SPEED_25GB:
298 cmd->base.speed = SPEED_25000;
300 case VIRTCHNL_LINK_SPEED_20GB:
301 cmd->base.speed = SPEED_20000;
303 case VIRTCHNL_LINK_SPEED_10GB:
304 cmd->base.speed = SPEED_10000;
306 case VIRTCHNL_LINK_SPEED_5GB:
307 cmd->base.speed = SPEED_5000;
309 case VIRTCHNL_LINK_SPEED_2_5GB:
310 cmd->base.speed = SPEED_2500;
312 case VIRTCHNL_LINK_SPEED_1GB:
313 cmd->base.speed = SPEED_1000;
315 case VIRTCHNL_LINK_SPEED_100MB:
316 cmd->base.speed = SPEED_100;
326 * iavf_get_sset_count - Get length of string set
327 * @netdev: network interface device structure
328 * @sset: id of string set
330 * Reports size of various string tables.
332 static int iavf_get_sset_count(struct net_device *netdev, int sset)
334 /* Report the maximum number queues, even if not every queue is
335 * currently configured. Since allocation of queues is in pairs,
336 * use netdev->real_num_tx_queues * 2. The real_num_tx_queues is set
337 * at device creation and never changes.
340 if (sset == ETH_SS_STATS)
341 return IAVF_STATS_LEN +
342 (IAVF_QUEUE_STATS_LEN * 2 *
343 netdev->real_num_tx_queues);
344 else if (sset == ETH_SS_PRIV_FLAGS)
345 return IAVF_PRIV_FLAGS_STR_LEN;
351 * iavf_get_ethtool_stats - report device statistics
352 * @netdev: network interface device structure
353 * @stats: ethtool statistics structure
354 * @data: pointer to data buffer
356 * All statistics are added to the data buffer as an array of u64.
358 static void iavf_get_ethtool_stats(struct net_device *netdev,
359 struct ethtool_stats *stats, u64 *data)
361 struct iavf_adapter *adapter = netdev_priv(netdev);
364 /* Explicitly request stats refresh */
365 iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_REQUEST_STATS);
367 iavf_add_ethtool_stats(&data, adapter, iavf_gstrings_stats);
370 /* As num_active_queues describe both tx and rx queues, we can use
371 * it to iterate over rings' stats.
373 for (i = 0; i < adapter->num_active_queues; i++) {
374 struct iavf_ring *ring;
377 ring = &adapter->tx_rings[i];
378 iavf_add_queue_stats(&data, ring);
381 ring = &adapter->rx_rings[i];
382 iavf_add_queue_stats(&data, ring);
388 * iavf_get_priv_flag_strings - Get private flag strings
389 * @netdev: network interface device structure
390 * @data: buffer for string data
392 * Builds the private flags string table
394 static void iavf_get_priv_flag_strings(struct net_device *netdev, u8 *data)
398 for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++)
399 ethtool_sprintf(&data, "%s",
400 iavf_gstrings_priv_flags[i].flag_string);
404 * iavf_get_stat_strings - Get stat strings
405 * @netdev: network interface device structure
406 * @data: buffer for string data
408 * Builds the statistics string table
410 static void iavf_get_stat_strings(struct net_device *netdev, u8 *data)
414 iavf_add_stat_strings(&data, iavf_gstrings_stats);
416 /* Queues are always allocated in pairs, so we just use
417 * real_num_tx_queues for both Tx and Rx queues.
419 for (i = 0; i < netdev->real_num_tx_queues; i++) {
420 iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
422 iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
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
433 * Builds string tables for various string sets
435 static void iavf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
439 iavf_get_stat_strings(netdev, data);
441 case ETH_SS_PRIV_FLAGS:
442 iavf_get_priv_flag_strings(netdev, data);
450 * iavf_get_priv_flags - report device private flags
451 * @netdev: network interface device structure
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
457 * Returns a u32 bitmap of flags.
459 static u32 iavf_get_priv_flags(struct net_device *netdev)
461 struct iavf_adapter *adapter = netdev_priv(netdev);
462 u32 i, ret_flags = 0;
464 for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
465 const struct iavf_priv_flags *priv_flags;
467 priv_flags = &iavf_gstrings_priv_flags[i];
469 if (priv_flags->flag & adapter->flags)
477 * iavf_set_priv_flags - set private flags
478 * @netdev: network interface device structure
479 * @flags: bit flags to be set
481 static int iavf_set_priv_flags(struct net_device *netdev, u32 flags)
483 struct iavf_adapter *adapter = netdev_priv(netdev);
484 u32 orig_flags, new_flags, changed_flags;
488 orig_flags = READ_ONCE(adapter->flags);
489 new_flags = orig_flags;
491 for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
492 const struct iavf_priv_flags *priv_flags;
494 priv_flags = &iavf_gstrings_priv_flags[i];
497 new_flags |= priv_flags->flag;
499 new_flags &= ~(priv_flags->flag);
501 if (priv_flags->read_only &&
502 ((orig_flags ^ new_flags) & ~BIT(i)))
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
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
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");
523 changed_flags = orig_flags ^ new_flags;
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
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);
536 netdev_warn(netdev, "Changing private flags timeout or interrupted waiting for reset");
544 * iavf_get_msglevel - Get debug message level
545 * @netdev: network interface device structure
547 * Returns current debug message level.
549 static u32 iavf_get_msglevel(struct net_device *netdev)
551 struct iavf_adapter *adapter = netdev_priv(netdev);
553 return adapter->msg_enable;
557 * iavf_set_msglevel - Set debug message level
558 * @netdev: network interface device structure
559 * @data: message level
561 * Set current debug message level. Higher values cause the driver to
564 static void iavf_set_msglevel(struct net_device *netdev, u32 data)
566 struct iavf_adapter *adapter = netdev_priv(netdev);
568 if (IAVF_DEBUG_USER & data)
569 adapter->hw.debug_mask = data;
570 adapter->msg_enable = data;
574 * iavf_get_drvinfo - Get driver info
575 * @netdev: network interface device structure
576 * @drvinfo: ethool driver info structure
578 * Returns information about the driver and device for display to the user.
580 static void iavf_get_drvinfo(struct net_device *netdev,
581 struct ethtool_drvinfo *drvinfo)
583 struct iavf_adapter *adapter = netdev_priv(netdev);
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;
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
598 * Returns current ring parameters. TX and RX rings are reported separately,
599 * but the number of rings is not reported.
601 static 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)
606 struct iavf_adapter *adapter = netdev_priv(netdev);
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;
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
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.
624 static 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)
629 struct iavf_adapter *adapter = netdev_priv(netdev);
630 u32 new_rx_count, new_tx_count;
633 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
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);
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",
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",
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");
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;
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;
675 if (netif_running(netdev)) {
676 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
677 ret = iavf_wait_for_reset(adapter);
679 netdev_warn(netdev, "Changing ring parameters timeout or interrupted waiting for reset");
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
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.
695 static int __iavf_get_coalesce(struct net_device *netdev,
696 struct ethtool_coalesce *ec, int queue)
698 struct iavf_adapter *adapter = netdev_priv(netdev);
699 struct iavf_ring *rx_ring, *tx_ring;
701 /* Rx and Tx usecs per queue value. If user doesn't specify the
702 * queue, return queue 0's value to represent.
706 else if (queue >= adapter->num_active_queues)
709 rx_ring = &adapter->rx_rings[queue];
710 tx_ring = &adapter->tx_rings[queue];
712 if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
713 ec->use_adaptive_rx_coalesce = 1;
715 if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
716 ec->use_adaptive_tx_coalesce = 1;
718 ec->rx_coalesce_usecs = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
719 ec->tx_coalesce_usecs = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
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
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.
736 static 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)
741 return __iavf_get_coalesce(netdev, ec, -1);
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
750 * Read specific queue's coalesce settings.
752 static int iavf_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
753 struct ethtool_coalesce *ec)
755 return __iavf_get_coalesce(netdev, ec, queue);
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
764 * Change the ITR settings for a specific queue.
766 static int iavf_set_itr_per_queue(struct iavf_adapter *adapter,
767 struct ethtool_coalesce *ec, int queue)
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;
774 itr_setting = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
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");
783 itr_setting = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
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");
792 rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
793 tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
795 rx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
796 if (!ec->use_adaptive_rx_coalesce)
797 rx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
799 tx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
800 if (!ec->use_adaptive_tx_coalesce)
801 tx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
803 q_vector = rx_ring->q_vector;
804 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
806 q_vector = tx_ring->q_vector;
807 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
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.
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
822 * Sets the coalesce settings for a particular queue.
824 static int __iavf_set_coalesce(struct net_device *netdev,
825 struct ethtool_coalesce *ec, int queue)
827 struct iavf_adapter *adapter = netdev_priv(netdev);
830 if (ec->rx_coalesce_usecs > IAVF_MAX_ITR) {
831 netif_info(adapter, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
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");
838 /* Rx and Tx usecs has per queue value. If user doesn't specify the
839 * queue, apply to all queues.
842 for (i = 0; i < adapter->num_active_queues; i++)
843 if (iavf_set_itr_per_queue(adapter, ec, i))
845 } else if (queue < adapter->num_active_queues) {
846 if (iavf_set_itr_per_queue(adapter, ec, queue))
849 netif_info(adapter, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
850 adapter->num_active_queues - 1);
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
864 * Change current coalescing settings for every queue.
866 static 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)
871 return __iavf_set_coalesce(netdev, ec, -1);
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
880 * Modifies a specific queue's coalesce settings.
882 static int iavf_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
883 struct ethtool_coalesce *ec)
885 return __iavf_set_coalesce(netdev, ec, queue);
889 * iavf_fltr_to_ethtool_flow - convert filter type values to ethtool
891 * @flow: filter type to be converted
893 * Returns the corresponding ethtool flow type.
895 static int iavf_fltr_to_ethtool_flow(enum iavf_fdir_flow_type flow)
898 case IAVF_FDIR_FLOW_IPV4_TCP:
900 case IAVF_FDIR_FLOW_IPV4_UDP:
902 case IAVF_FDIR_FLOW_IPV4_SCTP:
904 case IAVF_FDIR_FLOW_IPV4_AH:
906 case IAVF_FDIR_FLOW_IPV4_ESP:
908 case IAVF_FDIR_FLOW_IPV4_OTHER:
909 return IPV4_USER_FLOW;
910 case IAVF_FDIR_FLOW_IPV6_TCP:
912 case IAVF_FDIR_FLOW_IPV6_UDP:
914 case IAVF_FDIR_FLOW_IPV6_SCTP:
916 case IAVF_FDIR_FLOW_IPV6_AH:
918 case IAVF_FDIR_FLOW_IPV6_ESP:
920 case IAVF_FDIR_FLOW_IPV6_OTHER:
921 return IPV6_USER_FLOW;
922 case IAVF_FDIR_FLOW_NON_IP_L2:
925 /* 0 is undefined ethtool flow */
931 * iavf_ethtool_flow_to_fltr - convert ethtool flow type to filter enum
932 * @eth: Ethtool flow type to be converted
936 static enum iavf_fdir_flow_type iavf_ethtool_flow_to_fltr(int eth)
940 return IAVF_FDIR_FLOW_IPV4_TCP;
942 return IAVF_FDIR_FLOW_IPV4_UDP;
944 return IAVF_FDIR_FLOW_IPV4_SCTP;
946 return IAVF_FDIR_FLOW_IPV4_AH;
948 return IAVF_FDIR_FLOW_IPV4_ESP;
950 return IAVF_FDIR_FLOW_IPV4_OTHER;
952 return IAVF_FDIR_FLOW_IPV6_TCP;
954 return IAVF_FDIR_FLOW_IPV6_UDP;
956 return IAVF_FDIR_FLOW_IPV6_SCTP;
958 return IAVF_FDIR_FLOW_IPV6_AH;
960 return IAVF_FDIR_FLOW_IPV6_ESP;
962 return IAVF_FDIR_FLOW_IPV6_OTHER;
964 return IAVF_FDIR_FLOW_NON_IP_L2;
966 return IAVF_FDIR_FLOW_NONE;
971 * iavf_is_mask_valid - check mask field set
972 * @mask: full mask to check
973 * @field: field for which mask should be valid
975 * If the mask is fully set return true. If it is not valid for field return
978 static bool iavf_is_mask_valid(u64 mask, u64 field)
980 return (mask & field) == field;
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
988 * Returns 0 on success, negative error value on failure
991 iavf_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
992 struct iavf_fdir_fltr *fltr)
994 struct iavf_flex_word *flex;
997 if (!(fsp->flow_type & FLOW_EXT))
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]);
1008 if (!value || !mask)
1011 if (!iavf_is_mask_valid(mask, IAVF_USERDEF_FLEX_FLTR_M))
1014 /* 504 is the maximum value for offsets, and offset is measured
1015 * from the start of the MAC address.
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 = (value & IAVF_USERDEF_FLEX_OFFS_M) >>
1021 IAVF_USERDEF_FLEX_OFFS_S;
1022 if (flex->offset > IAVF_USERDEF_FLEX_MAX_OFFS_VAL)
1026 fltr->flex_cnt = cnt;
1032 * iavf_fill_rx_flow_ext_data - fill the additional data
1033 * @fsp: pointer to ethtool Rx flow specification
1034 * @fltr: pointer to Flow Director filter to get additional data
1037 iavf_fill_rx_flow_ext_data(struct ethtool_rx_flow_spec *fsp,
1038 struct iavf_fdir_fltr *fltr)
1040 if (!fltr->ext_mask.usr_def[0] && !fltr->ext_mask.usr_def[1])
1043 fsp->flow_type |= FLOW_EXT;
1045 memcpy(fsp->h_ext.data, fltr->ext_data.usr_def, sizeof(fsp->h_ext.data));
1046 memcpy(fsp->m_ext.data, fltr->ext_mask.usr_def, sizeof(fsp->m_ext.data));
1050 * iavf_get_ethtool_fdir_entry - fill ethtool structure with Flow Director filter data
1051 * @adapter: the VF adapter structure that contains filter list
1052 * @cmd: ethtool command data structure to receive the filter data
1054 * Returns 0 as expected for success by ethtool
1057 iavf_get_ethtool_fdir_entry(struct iavf_adapter *adapter,
1058 struct ethtool_rxnfc *cmd)
1060 struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1061 struct iavf_fdir_fltr *rule = NULL;
1064 if (!FDIR_FLTR_SUPPORT(adapter))
1067 spin_lock_bh(&adapter->fdir_fltr_lock);
1069 rule = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1075 fsp->flow_type = iavf_fltr_to_ethtool_flow(rule->flow_type);
1077 memset(&fsp->m_u, 0, sizeof(fsp->m_u));
1078 memset(&fsp->m_ext, 0, sizeof(fsp->m_ext));
1080 switch (fsp->flow_type) {
1084 fsp->h_u.tcp_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1085 fsp->h_u.tcp_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1086 fsp->h_u.tcp_ip4_spec.psrc = rule->ip_data.src_port;
1087 fsp->h_u.tcp_ip4_spec.pdst = rule->ip_data.dst_port;
1088 fsp->h_u.tcp_ip4_spec.tos = rule->ip_data.tos;
1089 fsp->m_u.tcp_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1090 fsp->m_u.tcp_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1091 fsp->m_u.tcp_ip4_spec.psrc = rule->ip_mask.src_port;
1092 fsp->m_u.tcp_ip4_spec.pdst = rule->ip_mask.dst_port;
1093 fsp->m_u.tcp_ip4_spec.tos = rule->ip_mask.tos;
1097 fsp->h_u.ah_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1098 fsp->h_u.ah_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1099 fsp->h_u.ah_ip4_spec.spi = rule->ip_data.spi;
1100 fsp->h_u.ah_ip4_spec.tos = rule->ip_data.tos;
1101 fsp->m_u.ah_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1102 fsp->m_u.ah_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1103 fsp->m_u.ah_ip4_spec.spi = rule->ip_mask.spi;
1104 fsp->m_u.ah_ip4_spec.tos = rule->ip_mask.tos;
1106 case IPV4_USER_FLOW:
1107 fsp->h_u.usr_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1108 fsp->h_u.usr_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1109 fsp->h_u.usr_ip4_spec.l4_4_bytes = rule->ip_data.l4_header;
1110 fsp->h_u.usr_ip4_spec.tos = rule->ip_data.tos;
1111 fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
1112 fsp->h_u.usr_ip4_spec.proto = rule->ip_data.proto;
1113 fsp->m_u.usr_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1114 fsp->m_u.usr_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1115 fsp->m_u.usr_ip4_spec.l4_4_bytes = rule->ip_mask.l4_header;
1116 fsp->m_u.usr_ip4_spec.tos = rule->ip_mask.tos;
1117 fsp->m_u.usr_ip4_spec.ip_ver = 0xFF;
1118 fsp->m_u.usr_ip4_spec.proto = rule->ip_mask.proto;
1123 memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1124 sizeof(struct in6_addr));
1125 memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1126 sizeof(struct in6_addr));
1127 fsp->h_u.tcp_ip6_spec.psrc = rule->ip_data.src_port;
1128 fsp->h_u.tcp_ip6_spec.pdst = rule->ip_data.dst_port;
1129 fsp->h_u.tcp_ip6_spec.tclass = rule->ip_data.tclass;
1130 memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1131 sizeof(struct in6_addr));
1132 memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1133 sizeof(struct in6_addr));
1134 fsp->m_u.tcp_ip6_spec.psrc = rule->ip_mask.src_port;
1135 fsp->m_u.tcp_ip6_spec.pdst = rule->ip_mask.dst_port;
1136 fsp->m_u.tcp_ip6_spec.tclass = rule->ip_mask.tclass;
1140 memcpy(fsp->h_u.ah_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1141 sizeof(struct in6_addr));
1142 memcpy(fsp->h_u.ah_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1143 sizeof(struct in6_addr));
1144 fsp->h_u.ah_ip6_spec.spi = rule->ip_data.spi;
1145 fsp->h_u.ah_ip6_spec.tclass = rule->ip_data.tclass;
1146 memcpy(fsp->m_u.ah_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1147 sizeof(struct in6_addr));
1148 memcpy(fsp->m_u.ah_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1149 sizeof(struct in6_addr));
1150 fsp->m_u.ah_ip6_spec.spi = rule->ip_mask.spi;
1151 fsp->m_u.ah_ip6_spec.tclass = rule->ip_mask.tclass;
1153 case IPV6_USER_FLOW:
1154 memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1155 sizeof(struct in6_addr));
1156 memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1157 sizeof(struct in6_addr));
1158 fsp->h_u.usr_ip6_spec.l4_4_bytes = rule->ip_data.l4_header;
1159 fsp->h_u.usr_ip6_spec.tclass = rule->ip_data.tclass;
1160 fsp->h_u.usr_ip6_spec.l4_proto = rule->ip_data.proto;
1161 memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1162 sizeof(struct in6_addr));
1163 memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1164 sizeof(struct in6_addr));
1165 fsp->m_u.usr_ip6_spec.l4_4_bytes = rule->ip_mask.l4_header;
1166 fsp->m_u.usr_ip6_spec.tclass = rule->ip_mask.tclass;
1167 fsp->m_u.usr_ip6_spec.l4_proto = rule->ip_mask.proto;
1170 fsp->h_u.ether_spec.h_proto = rule->eth_data.etype;
1171 fsp->m_u.ether_spec.h_proto = rule->eth_mask.etype;
1178 iavf_fill_rx_flow_ext_data(fsp, rule);
1180 if (rule->action == VIRTCHNL_ACTION_DROP)
1181 fsp->ring_cookie = RX_CLS_FLOW_DISC;
1183 fsp->ring_cookie = rule->q_index;
1186 spin_unlock_bh(&adapter->fdir_fltr_lock);
1191 * iavf_get_fdir_fltr_ids - fill buffer with filter IDs of active filters
1192 * @adapter: the VF adapter structure containing the filter list
1193 * @cmd: ethtool command data structure
1194 * @rule_locs: ethtool array passed in from OS to receive filter IDs
1196 * Returns 0 as expected for success by ethtool
1199 iavf_get_fdir_fltr_ids(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd,
1202 struct iavf_fdir_fltr *fltr;
1203 unsigned int cnt = 0;
1206 if (!FDIR_FLTR_SUPPORT(adapter))
1209 cmd->data = IAVF_MAX_FDIR_FILTERS;
1211 spin_lock_bh(&adapter->fdir_fltr_lock);
1213 list_for_each_entry(fltr, &adapter->fdir_list_head, list) {
1214 if (cnt == cmd->rule_cnt) {
1218 rule_locs[cnt] = fltr->loc;
1223 spin_unlock_bh(&adapter->fdir_fltr_lock);
1225 cmd->rule_cnt = cnt;
1231 * iavf_add_fdir_fltr_info - Set the input set for Flow Director filter
1232 * @adapter: pointer to the VF adapter structure
1233 * @fsp: pointer to ethtool Rx flow specification
1234 * @fltr: filter structure
1237 iavf_add_fdir_fltr_info(struct iavf_adapter *adapter, struct ethtool_rx_flow_spec *fsp,
1238 struct iavf_fdir_fltr *fltr)
1240 u32 flow_type, q_index = 0;
1241 enum virtchnl_action act;
1244 if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
1245 act = VIRTCHNL_ACTION_DROP;
1247 q_index = fsp->ring_cookie;
1248 if (q_index >= adapter->num_active_queues)
1251 act = VIRTCHNL_ACTION_QUEUE;
1255 fltr->loc = fsp->location;
1256 fltr->q_index = q_index;
1258 if (fsp->flow_type & FLOW_EXT) {
1259 memcpy(fltr->ext_data.usr_def, fsp->h_ext.data,
1260 sizeof(fltr->ext_data.usr_def));
1261 memcpy(fltr->ext_mask.usr_def, fsp->m_ext.data,
1262 sizeof(fltr->ext_mask.usr_def));
1265 flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
1266 fltr->flow_type = iavf_ethtool_flow_to_fltr(flow_type);
1268 switch (flow_type) {
1272 fltr->ip_data.v4_addrs.src_ip = fsp->h_u.tcp_ip4_spec.ip4src;
1273 fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
1274 fltr->ip_data.src_port = fsp->h_u.tcp_ip4_spec.psrc;
1275 fltr->ip_data.dst_port = fsp->h_u.tcp_ip4_spec.pdst;
1276 fltr->ip_data.tos = fsp->h_u.tcp_ip4_spec.tos;
1277 fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.tcp_ip4_spec.ip4src;
1278 fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.tcp_ip4_spec.ip4dst;
1279 fltr->ip_mask.src_port = fsp->m_u.tcp_ip4_spec.psrc;
1280 fltr->ip_mask.dst_port = fsp->m_u.tcp_ip4_spec.pdst;
1281 fltr->ip_mask.tos = fsp->m_u.tcp_ip4_spec.tos;
1286 fltr->ip_data.v4_addrs.src_ip = fsp->h_u.ah_ip4_spec.ip4src;
1287 fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.ah_ip4_spec.ip4dst;
1288 fltr->ip_data.spi = fsp->h_u.ah_ip4_spec.spi;
1289 fltr->ip_data.tos = fsp->h_u.ah_ip4_spec.tos;
1290 fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.ah_ip4_spec.ip4src;
1291 fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.ah_ip4_spec.ip4dst;
1292 fltr->ip_mask.spi = fsp->m_u.ah_ip4_spec.spi;
1293 fltr->ip_mask.tos = fsp->m_u.ah_ip4_spec.tos;
1296 case IPV4_USER_FLOW:
1297 fltr->ip_data.v4_addrs.src_ip = fsp->h_u.usr_ip4_spec.ip4src;
1298 fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.usr_ip4_spec.ip4dst;
1299 fltr->ip_data.l4_header = fsp->h_u.usr_ip4_spec.l4_4_bytes;
1300 fltr->ip_data.tos = fsp->h_u.usr_ip4_spec.tos;
1301 fltr->ip_data.proto = fsp->h_u.usr_ip4_spec.proto;
1302 fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.usr_ip4_spec.ip4src;
1303 fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.usr_ip4_spec.ip4dst;
1304 fltr->ip_mask.l4_header = fsp->m_u.usr_ip4_spec.l4_4_bytes;
1305 fltr->ip_mask.tos = fsp->m_u.usr_ip4_spec.tos;
1306 fltr->ip_mask.proto = fsp->m_u.usr_ip4_spec.proto;
1312 memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1313 sizeof(struct in6_addr));
1314 memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1315 sizeof(struct in6_addr));
1316 fltr->ip_data.src_port = fsp->h_u.tcp_ip6_spec.psrc;
1317 fltr->ip_data.dst_port = fsp->h_u.tcp_ip6_spec.pdst;
1318 fltr->ip_data.tclass = fsp->h_u.tcp_ip6_spec.tclass;
1319 memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1320 sizeof(struct in6_addr));
1321 memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1322 sizeof(struct in6_addr));
1323 fltr->ip_mask.src_port = fsp->m_u.tcp_ip6_spec.psrc;
1324 fltr->ip_mask.dst_port = fsp->m_u.tcp_ip6_spec.pdst;
1325 fltr->ip_mask.tclass = fsp->m_u.tcp_ip6_spec.tclass;
1330 memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.ah_ip6_spec.ip6src,
1331 sizeof(struct in6_addr));
1332 memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.ah_ip6_spec.ip6dst,
1333 sizeof(struct in6_addr));
1334 fltr->ip_data.spi = fsp->h_u.ah_ip6_spec.spi;
1335 fltr->ip_data.tclass = fsp->h_u.ah_ip6_spec.tclass;
1336 memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.ah_ip6_spec.ip6src,
1337 sizeof(struct in6_addr));
1338 memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.ah_ip6_spec.ip6dst,
1339 sizeof(struct in6_addr));
1340 fltr->ip_mask.spi = fsp->m_u.ah_ip6_spec.spi;
1341 fltr->ip_mask.tclass = fsp->m_u.ah_ip6_spec.tclass;
1344 case IPV6_USER_FLOW:
1345 memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1346 sizeof(struct in6_addr));
1347 memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1348 sizeof(struct in6_addr));
1349 fltr->ip_data.l4_header = fsp->h_u.usr_ip6_spec.l4_4_bytes;
1350 fltr->ip_data.tclass = fsp->h_u.usr_ip6_spec.tclass;
1351 fltr->ip_data.proto = fsp->h_u.usr_ip6_spec.l4_proto;
1352 memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1353 sizeof(struct in6_addr));
1354 memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1355 sizeof(struct in6_addr));
1356 fltr->ip_mask.l4_header = fsp->m_u.usr_ip6_spec.l4_4_bytes;
1357 fltr->ip_mask.tclass = fsp->m_u.usr_ip6_spec.tclass;
1358 fltr->ip_mask.proto = fsp->m_u.usr_ip6_spec.l4_proto;
1362 fltr->eth_data.etype = fsp->h_u.ether_spec.h_proto;
1363 fltr->eth_mask.etype = fsp->m_u.ether_spec.h_proto;
1366 /* not doing un-parsed flow types */
1370 err = iavf_validate_fdir_fltr_masks(adapter, fltr);
1374 if (iavf_fdir_is_dup_fltr(adapter, fltr))
1377 err = iavf_parse_rx_flow_user_data(fsp, fltr);
1381 return iavf_fill_fdir_add_msg(adapter, fltr);
1385 * iavf_add_fdir_ethtool - add Flow Director filter
1386 * @adapter: pointer to the VF adapter structure
1387 * @cmd: command to add Flow Director filter
1389 * Returns 0 on success and negative values for failure
1391 static int iavf_add_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1393 struct ethtool_rx_flow_spec *fsp = &cmd->fs;
1394 struct iavf_fdir_fltr *fltr;
1398 if (!FDIR_FLTR_SUPPORT(adapter))
1401 if (fsp->flow_type & FLOW_MAC_EXT)
1404 spin_lock_bh(&adapter->fdir_fltr_lock);
1405 if (adapter->fdir_active_fltr >= IAVF_MAX_FDIR_FILTERS) {
1406 spin_unlock_bh(&adapter->fdir_fltr_lock);
1407 dev_err(&adapter->pdev->dev,
1408 "Unable to add Flow Director filter because VF reached the limit of max allowed filters (%u)\n",
1409 IAVF_MAX_FDIR_FILTERS);
1413 if (iavf_find_fdir_fltr_by_loc(adapter, fsp->location)) {
1414 dev_err(&adapter->pdev->dev, "Failed to add Flow Director filter, it already exists\n");
1415 spin_unlock_bh(&adapter->fdir_fltr_lock);
1418 spin_unlock_bh(&adapter->fdir_fltr_lock);
1420 fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1424 while (!mutex_trylock(&adapter->crit_lock)) {
1432 err = iavf_add_fdir_fltr_info(adapter, fsp, fltr);
1436 spin_lock_bh(&adapter->fdir_fltr_lock);
1437 iavf_fdir_list_add_fltr(adapter, fltr);
1438 adapter->fdir_active_fltr++;
1439 fltr->state = IAVF_FDIR_FLTR_ADD_REQUEST;
1440 spin_unlock_bh(&adapter->fdir_fltr_lock);
1442 iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ADD_FDIR_FILTER);
1448 mutex_unlock(&adapter->crit_lock);
1453 * iavf_del_fdir_ethtool - delete Flow Director filter
1454 * @adapter: pointer to the VF adapter structure
1455 * @cmd: command to delete Flow Director filter
1457 * Returns 0 on success and negative values for failure
1459 static int iavf_del_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1461 struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1462 struct iavf_fdir_fltr *fltr = NULL;
1465 if (!FDIR_FLTR_SUPPORT(adapter))
1468 spin_lock_bh(&adapter->fdir_fltr_lock);
1469 fltr = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1471 if (fltr->state == IAVF_FDIR_FLTR_ACTIVE) {
1472 fltr->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1476 } else if (adapter->fdir_active_fltr) {
1479 spin_unlock_bh(&adapter->fdir_fltr_lock);
1481 if (fltr && fltr->state == IAVF_FDIR_FLTR_DEL_REQUEST)
1482 iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_DEL_FDIR_FILTER);
1488 * iavf_adv_rss_parse_hdrs - parses headers from RSS hash input
1489 * @cmd: ethtool rxnfc command
1491 * This function parses the rxnfc command and returns intended
1492 * header types for RSS configuration
1494 static u32 iavf_adv_rss_parse_hdrs(struct ethtool_rxnfc *cmd)
1496 u32 hdrs = IAVF_ADV_RSS_FLOW_SEG_HDR_NONE;
1498 switch (cmd->flow_type) {
1500 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1501 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1504 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1505 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1508 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1509 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1512 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1513 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1516 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1517 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1520 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1521 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1531 * iavf_adv_rss_parse_hash_flds - parses hash fields from RSS hash input
1532 * @cmd: ethtool rxnfc command
1534 * This function parses the rxnfc command and returns intended hash fields for
1537 static u64 iavf_adv_rss_parse_hash_flds(struct ethtool_rxnfc *cmd)
1539 u64 hfld = IAVF_ADV_RSS_HASH_INVALID;
1541 if (cmd->data & RXH_IP_SRC || cmd->data & RXH_IP_DST) {
1542 switch (cmd->flow_type) {
1546 if (cmd->data & RXH_IP_SRC)
1547 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_SA;
1548 if (cmd->data & RXH_IP_DST)
1549 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_DA;
1554 if (cmd->data & RXH_IP_SRC)
1555 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_SA;
1556 if (cmd->data & RXH_IP_DST)
1557 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_DA;
1564 if (cmd->data & RXH_L4_B_0_1 || cmd->data & RXH_L4_B_2_3) {
1565 switch (cmd->flow_type) {
1568 if (cmd->data & RXH_L4_B_0_1)
1569 hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT;
1570 if (cmd->data & RXH_L4_B_2_3)
1571 hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT;
1575 if (cmd->data & RXH_L4_B_0_1)
1576 hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT;
1577 if (cmd->data & RXH_L4_B_2_3)
1578 hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT;
1582 if (cmd->data & RXH_L4_B_0_1)
1583 hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT;
1584 if (cmd->data & RXH_L4_B_2_3)
1585 hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT;
1596 * iavf_set_adv_rss_hash_opt - Enable/Disable flow types for RSS hash
1597 * @adapter: pointer to the VF adapter structure
1598 * @cmd: ethtool rxnfc command
1600 * Returns Success if the flow input set is supported.
1603 iavf_set_adv_rss_hash_opt(struct iavf_adapter *adapter,
1604 struct ethtool_rxnfc *cmd)
1606 struct iavf_adv_rss *rss_old, *rss_new;
1607 bool rss_new_add = false;
1608 int count = 50, err = 0;
1612 if (!ADV_RSS_SUPPORT(adapter))
1615 hdrs = iavf_adv_rss_parse_hdrs(cmd);
1616 if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1619 hash_flds = iavf_adv_rss_parse_hash_flds(cmd);
1620 if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1623 rss_new = kzalloc(sizeof(*rss_new), GFP_KERNEL);
1627 if (iavf_fill_adv_rss_cfg_msg(&rss_new->cfg_msg, hdrs, hash_flds)) {
1632 while (!mutex_trylock(&adapter->crit_lock)) {
1641 spin_lock_bh(&adapter->adv_rss_lock);
1642 rss_old = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1644 if (rss_old->state != IAVF_ADV_RSS_ACTIVE) {
1646 } else if (rss_old->hash_flds != hash_flds) {
1647 rss_old->state = IAVF_ADV_RSS_ADD_REQUEST;
1648 rss_old->hash_flds = hash_flds;
1649 memcpy(&rss_old->cfg_msg, &rss_new->cfg_msg,
1650 sizeof(rss_new->cfg_msg));
1656 rss_new->state = IAVF_ADV_RSS_ADD_REQUEST;
1657 rss_new->packet_hdrs = hdrs;
1658 rss_new->hash_flds = hash_flds;
1659 list_add_tail(&rss_new->list, &adapter->adv_rss_list_head);
1661 spin_unlock_bh(&adapter->adv_rss_lock);
1664 iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ADD_ADV_RSS_CFG);
1666 mutex_unlock(&adapter->crit_lock);
1675 * iavf_get_adv_rss_hash_opt - Retrieve hash fields for a given flow-type
1676 * @adapter: pointer to the VF adapter structure
1677 * @cmd: ethtool rxnfc command
1679 * Returns Success if the flow input set is supported.
1682 iavf_get_adv_rss_hash_opt(struct iavf_adapter *adapter,
1683 struct ethtool_rxnfc *cmd)
1685 struct iavf_adv_rss *rss;
1689 if (!ADV_RSS_SUPPORT(adapter))
1694 hdrs = iavf_adv_rss_parse_hdrs(cmd);
1695 if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1698 spin_lock_bh(&adapter->adv_rss_lock);
1699 rss = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1701 hash_flds = rss->hash_flds;
1703 hash_flds = IAVF_ADV_RSS_HASH_INVALID;
1704 spin_unlock_bh(&adapter->adv_rss_lock);
1706 if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1709 if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_SA |
1710 IAVF_ADV_RSS_HASH_FLD_IPV6_SA))
1711 cmd->data |= (u64)RXH_IP_SRC;
1713 if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_DA |
1714 IAVF_ADV_RSS_HASH_FLD_IPV6_DA))
1715 cmd->data |= (u64)RXH_IP_DST;
1717 if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT |
1718 IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT |
1719 IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT))
1720 cmd->data |= (u64)RXH_L4_B_0_1;
1722 if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT |
1723 IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT |
1724 IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT))
1725 cmd->data |= (u64)RXH_L4_B_2_3;
1731 * iavf_set_rxnfc - command to set Rx flow rules.
1732 * @netdev: network interface device structure
1733 * @cmd: ethtool rxnfc command
1735 * Returns 0 for success and negative values for errors
1737 static int iavf_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1739 struct iavf_adapter *adapter = netdev_priv(netdev);
1740 int ret = -EOPNOTSUPP;
1743 case ETHTOOL_SRXCLSRLINS:
1744 ret = iavf_add_fdir_ethtool(adapter, cmd);
1746 case ETHTOOL_SRXCLSRLDEL:
1747 ret = iavf_del_fdir_ethtool(adapter, cmd);
1750 ret = iavf_set_adv_rss_hash_opt(adapter, cmd);
1760 * iavf_get_rxnfc - command to get RX flow classification rules
1761 * @netdev: network interface device structure
1762 * @cmd: ethtool rxnfc command
1763 * @rule_locs: pointer to store rule locations
1765 * Returns Success if the command is supported.
1767 static int iavf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1770 struct iavf_adapter *adapter = netdev_priv(netdev);
1771 int ret = -EOPNOTSUPP;
1774 case ETHTOOL_GRXRINGS:
1775 cmd->data = adapter->num_active_queues;
1778 case ETHTOOL_GRXCLSRLCNT:
1779 if (!FDIR_FLTR_SUPPORT(adapter))
1781 spin_lock_bh(&adapter->fdir_fltr_lock);
1782 cmd->rule_cnt = adapter->fdir_active_fltr;
1783 spin_unlock_bh(&adapter->fdir_fltr_lock);
1784 cmd->data = IAVF_MAX_FDIR_FILTERS;
1787 case ETHTOOL_GRXCLSRULE:
1788 ret = iavf_get_ethtool_fdir_entry(adapter, cmd);
1790 case ETHTOOL_GRXCLSRLALL:
1791 ret = iavf_get_fdir_fltr_ids(adapter, cmd, (u32 *)rule_locs);
1794 ret = iavf_get_adv_rss_hash_opt(adapter, cmd);
1803 * iavf_get_channels: get the number of channels supported by the device
1804 * @netdev: network interface device structure
1805 * @ch: channel information structure
1807 * For the purposes of our device, we only use combined channels, i.e. a tx/rx
1808 * queue pair. Report one extra channel to match our "other" MSI-X vector.
1810 static void iavf_get_channels(struct net_device *netdev,
1811 struct ethtool_channels *ch)
1813 struct iavf_adapter *adapter = netdev_priv(netdev);
1815 /* Report maximum channels */
1816 ch->max_combined = adapter->vsi_res->num_queue_pairs;
1818 ch->max_other = NONQ_VECS;
1819 ch->other_count = NONQ_VECS;
1821 ch->combined_count = adapter->num_active_queues;
1825 * iavf_set_channels: set the new channel count
1826 * @netdev: network interface device structure
1827 * @ch: channel information structure
1829 * Negotiate a new number of channels with the PF then do a reset. During
1830 * reset we'll realloc queues and fix the RSS table. Returns 0 on success,
1831 * negative on failure.
1833 static int iavf_set_channels(struct net_device *netdev,
1834 struct ethtool_channels *ch)
1836 struct iavf_adapter *adapter = netdev_priv(netdev);
1837 u32 num_req = ch->combined_count;
1840 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1842 dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
1846 /* All of these should have already been checked by ethtool before this
1847 * even gets to us, but just to be sure.
1849 if (num_req == 0 || num_req > adapter->vsi_res->num_queue_pairs)
1852 if (num_req == adapter->num_active_queues)
1855 if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
1858 adapter->num_req_queues = num_req;
1859 adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1860 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
1862 ret = iavf_wait_for_reset(adapter);
1864 netdev_warn(netdev, "Changing channel count timeout or interrupted waiting for reset");
1870 * iavf_get_rxfh_key_size - get the RSS hash key size
1871 * @netdev: network interface device structure
1873 * Returns the table size.
1875 static u32 iavf_get_rxfh_key_size(struct net_device *netdev)
1877 struct iavf_adapter *adapter = netdev_priv(netdev);
1879 return adapter->rss_key_size;
1883 * iavf_get_rxfh_indir_size - get the rx flow hash indirection table size
1884 * @netdev: network interface device structure
1886 * Returns the table size.
1888 static u32 iavf_get_rxfh_indir_size(struct net_device *netdev)
1890 struct iavf_adapter *adapter = netdev_priv(netdev);
1892 return adapter->rss_lut_size;
1896 * iavf_get_rxfh - get the rx flow hash indirection table
1897 * @netdev: network interface device structure
1898 * @indir: indirection table
1900 * @hfunc: hash function in use
1902 * Reads the indirection table directly from the hardware. Always returns 0.
1904 static int iavf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1907 struct iavf_adapter *adapter = netdev_priv(netdev);
1911 *hfunc = ETH_RSS_HASH_TOP;
1913 memcpy(key, adapter->rss_key, adapter->rss_key_size);
1916 /* Each 32 bits pointed by 'indir' is stored with a lut entry */
1917 for (i = 0; i < adapter->rss_lut_size; i++)
1918 indir[i] = (u32)adapter->rss_lut[i];
1924 * iavf_set_rxfh - set the rx flow hash indirection table
1925 * @netdev: network interface device structure
1926 * @indir: indirection table
1928 * @hfunc: hash function to use
1930 * Returns -EINVAL if the table specifies an invalid queue id, otherwise
1931 * returns 0 after programming the table.
1933 static int iavf_set_rxfh(struct net_device *netdev, const u32 *indir,
1934 const u8 *key, const u8 hfunc)
1936 struct iavf_adapter *adapter = netdev_priv(netdev);
1939 /* Only support toeplitz hash function */
1940 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1947 memcpy(adapter->rss_key, key, adapter->rss_key_size);
1950 /* Each 32 bits pointed by 'indir' is stored with a lut entry */
1951 for (i = 0; i < adapter->rss_lut_size; i++)
1952 adapter->rss_lut[i] = (u8)(indir[i]);
1955 return iavf_config_rss(adapter);
1958 static const struct ethtool_ops iavf_ethtool_ops = {
1959 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1960 ETHTOOL_COALESCE_USE_ADAPTIVE,
1961 .get_drvinfo = iavf_get_drvinfo,
1962 .get_link = ethtool_op_get_link,
1963 .get_ringparam = iavf_get_ringparam,
1964 .set_ringparam = iavf_set_ringparam,
1965 .get_strings = iavf_get_strings,
1966 .get_ethtool_stats = iavf_get_ethtool_stats,
1967 .get_sset_count = iavf_get_sset_count,
1968 .get_priv_flags = iavf_get_priv_flags,
1969 .set_priv_flags = iavf_set_priv_flags,
1970 .get_msglevel = iavf_get_msglevel,
1971 .set_msglevel = iavf_set_msglevel,
1972 .get_coalesce = iavf_get_coalesce,
1973 .set_coalesce = iavf_set_coalesce,
1974 .get_per_queue_coalesce = iavf_get_per_queue_coalesce,
1975 .set_per_queue_coalesce = iavf_set_per_queue_coalesce,
1976 .set_rxnfc = iavf_set_rxnfc,
1977 .get_rxnfc = iavf_get_rxnfc,
1978 .get_rxfh_indir_size = iavf_get_rxfh_indir_size,
1979 .get_rxfh = iavf_get_rxfh,
1980 .set_rxfh = iavf_set_rxfh,
1981 .get_channels = iavf_get_channels,
1982 .set_channels = iavf_set_channels,
1983 .get_rxfh_key_size = iavf_get_rxfh_key_size,
1984 .get_link_ksettings = iavf_get_link_ksettings,
1988 * iavf_set_ethtool_ops - Initialize ethtool ops struct
1989 * @netdev: network interface device structure
1991 * Sets ethtool ops struct in our netdev so that ethtool can call
1994 void iavf_set_ethtool_ops(struct net_device *netdev)
1996 netdev->ethtool_ops = &iavf_ethtool_ops;