Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright(c) 2013 - 2018 Intel Corporation. */
0003 
0004 /* ethtool support for iavf */
0005 #include "iavf.h"
0006 
0007 #include <linux/uaccess.h>
0008 
0009 /* ethtool statistics helpers */
0010 
0011 /**
0012  * struct iavf_stats - definition for an ethtool statistic
0013  * @stat_string: statistic name to display in ethtool -S output
0014  * @sizeof_stat: the sizeof() the stat, must be no greater than sizeof(u64)
0015  * @stat_offset: offsetof() the stat from a base pointer
0016  *
0017  * This structure defines a statistic to be added to the ethtool stats buffer.
0018  * It defines a statistic as offset from a common base pointer. Stats should
0019  * be defined in constant arrays using the IAVF_STAT macro, with every element
0020  * of the array using the same _type for calculating the sizeof_stat and
0021  * stat_offset.
0022  *
0023  * The @sizeof_stat is expected to be sizeof(u8), sizeof(u16), sizeof(u32) or
0024  * sizeof(u64). Other sizes are not expected and will produce a WARN_ONCE from
0025  * the iavf_add_ethtool_stat() helper function.
0026  *
0027  * The @stat_string is interpreted as a format string, allowing formatted
0028  * values to be inserted while looping over multiple structures for a given
0029  * statistics array. Thus, every statistic string in an array should have the
0030  * same type and number of format specifiers, to be formatted by variadic
0031  * arguments to the iavf_add_stat_string() helper function.
0032  **/
0033 struct iavf_stats {
0034     char stat_string[ETH_GSTRING_LEN];
0035     int sizeof_stat;
0036     int stat_offset;
0037 };
0038 
0039 /* Helper macro to define an iavf_stat structure with proper size and type.
0040  * Use this when defining constant statistics arrays. Note that @_type expects
0041  * only a type name and is used multiple times.
0042  */
0043 #define IAVF_STAT(_type, _name, _stat) { \
0044     .stat_string = _name, \
0045     .sizeof_stat = sizeof_field(_type, _stat), \
0046     .stat_offset = offsetof(_type, _stat) \
0047 }
0048 
0049 /* Helper macro for defining some statistics related to queues */
0050 #define IAVF_QUEUE_STAT(_name, _stat) \
0051     IAVF_STAT(struct iavf_ring, _name, _stat)
0052 
0053 /* Stats associated with a Tx or Rx ring */
0054 static const struct iavf_stats iavf_gstrings_queue_stats[] = {
0055     IAVF_QUEUE_STAT("%s-%u.packets", stats.packets),
0056     IAVF_QUEUE_STAT("%s-%u.bytes", stats.bytes),
0057 };
0058 
0059 /**
0060  * iavf_add_one_ethtool_stat - copy the stat into the supplied buffer
0061  * @data: location to store the stat value
0062  * @pointer: basis for where to copy from
0063  * @stat: the stat definition
0064  *
0065  * Copies the stat data defined by the pointer and stat structure pair into
0066  * the memory supplied as data. Used to implement iavf_add_ethtool_stats and
0067  * iavf_add_queue_stats. If the pointer is null, data will be zero'd.
0068  */
0069 static void
0070 iavf_add_one_ethtool_stat(u64 *data, void *pointer,
0071               const struct iavf_stats *stat)
0072 {
0073     char *p;
0074 
0075     if (!pointer) {
0076         /* ensure that the ethtool data buffer is zero'd for any stats
0077          * which don't have a valid pointer.
0078          */
0079         *data = 0;
0080         return;
0081     }
0082 
0083     p = (char *)pointer + stat->stat_offset;
0084     switch (stat->sizeof_stat) {
0085     case sizeof(u64):
0086         *data = *((u64 *)p);
0087         break;
0088     case sizeof(u32):
0089         *data = *((u32 *)p);
0090         break;
0091     case sizeof(u16):
0092         *data = *((u16 *)p);
0093         break;
0094     case sizeof(u8):
0095         *data = *((u8 *)p);
0096         break;
0097     default:
0098         WARN_ONCE(1, "unexpected stat size for %s",
0099               stat->stat_string);
0100         *data = 0;
0101     }
0102 }
0103 
0104 /**
0105  * __iavf_add_ethtool_stats - copy stats into the ethtool supplied buffer
0106  * @data: ethtool stats buffer
0107  * @pointer: location to copy stats from
0108  * @stats: array of stats to copy
0109  * @size: the size of the stats definition
0110  *
0111  * Copy the stats defined by the stats array using the pointer as a base into
0112  * the data buffer supplied by ethtool. Updates the data pointer to point to
0113  * the next empty location for successive calls to __iavf_add_ethtool_stats.
0114  * If pointer is null, set the data values to zero and update the pointer to
0115  * skip these stats.
0116  **/
0117 static void
0118 __iavf_add_ethtool_stats(u64 **data, void *pointer,
0119              const struct iavf_stats stats[],
0120              const unsigned int size)
0121 {
0122     unsigned int i;
0123 
0124     for (i = 0; i < size; i++)
0125         iavf_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
0126 }
0127 
0128 /**
0129  * iavf_add_ethtool_stats - copy stats into ethtool supplied buffer
0130  * @data: ethtool stats buffer
0131  * @pointer: location where stats are stored
0132  * @stats: static const array of stat definitions
0133  *
0134  * Macro to ease the use of __iavf_add_ethtool_stats by taking a static
0135  * constant stats array and passing the ARRAY_SIZE(). This avoids typos by
0136  * ensuring that we pass the size associated with the given stats array.
0137  *
0138  * The parameter @stats is evaluated twice, so parameters with side effects
0139  * should be avoided.
0140  **/
0141 #define iavf_add_ethtool_stats(data, pointer, stats) \
0142     __iavf_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
0143 
0144 /**
0145  * iavf_add_queue_stats - copy queue statistics into supplied buffer
0146  * @data: ethtool stats buffer
0147  * @ring: the ring to copy
0148  *
0149  * Queue statistics must be copied while protected by
0150  * u64_stats_fetch_begin_irq, so we can't directly use iavf_add_ethtool_stats.
0151  * Assumes that queue stats are defined in iavf_gstrings_queue_stats. If the
0152  * ring pointer is null, zero out the queue stat values and update the data
0153  * pointer. Otherwise safely copy the stats from the ring into the supplied
0154  * buffer and update the data pointer when finished.
0155  *
0156  * This function expects to be called while under rcu_read_lock().
0157  **/
0158 static void
0159 iavf_add_queue_stats(u64 **data, struct iavf_ring *ring)
0160 {
0161     const unsigned int size = ARRAY_SIZE(iavf_gstrings_queue_stats);
0162     const struct iavf_stats *stats = iavf_gstrings_queue_stats;
0163     unsigned int start;
0164     unsigned int i;
0165 
0166     /* To avoid invalid statistics values, ensure that we keep retrying
0167      * the copy until we get a consistent value according to
0168      * u64_stats_fetch_retry_irq. But first, make sure our ring is
0169      * non-null before attempting to access its syncp.
0170      */
0171     do {
0172         start = !ring ? 0 : u64_stats_fetch_begin_irq(&ring->syncp);
0173         for (i = 0; i < size; i++)
0174             iavf_add_one_ethtool_stat(&(*data)[i], ring, &stats[i]);
0175     } while (ring && u64_stats_fetch_retry_irq(&ring->syncp, start));
0176 
0177     /* Once we successfully copy the stats in, update the data pointer */
0178     *data += size;
0179 }
0180 
0181 /**
0182  * __iavf_add_stat_strings - copy stat strings into ethtool buffer
0183  * @p: ethtool supplied buffer
0184  * @stats: stat definitions array
0185  * @size: size of the stats array
0186  *
0187  * Format and copy the strings described by stats into the buffer pointed at
0188  * by p.
0189  **/
0190 static void __iavf_add_stat_strings(u8 **p, const struct iavf_stats stats[],
0191                     const unsigned int size, ...)
0192 {
0193     unsigned int i;
0194 
0195     for (i = 0; i < size; i++) {
0196         va_list args;
0197 
0198         va_start(args, size);
0199         vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
0200         *p += ETH_GSTRING_LEN;
0201         va_end(args);
0202     }
0203 }
0204 
0205 /**
0206  * iavf_add_stat_strings - copy stat strings into ethtool buffer
0207  * @p: ethtool supplied buffer
0208  * @stats: stat definitions array
0209  *
0210  * Format and copy the strings described by the const static stats value into
0211  * the buffer pointed at by p.
0212  *
0213  * The parameter @stats is evaluated twice, so parameters with side effects
0214  * should be avoided. Additionally, stats must be an array such that
0215  * ARRAY_SIZE can be called on it.
0216  **/
0217 #define iavf_add_stat_strings(p, stats, ...) \
0218     __iavf_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
0219 
0220 #define VF_STAT(_name, _stat) \
0221     IAVF_STAT(struct iavf_adapter, _name, _stat)
0222 
0223 static const struct iavf_stats iavf_gstrings_stats[] = {
0224     VF_STAT("rx_bytes", current_stats.rx_bytes),
0225     VF_STAT("rx_unicast", current_stats.rx_unicast),
0226     VF_STAT("rx_multicast", current_stats.rx_multicast),
0227     VF_STAT("rx_broadcast", current_stats.rx_broadcast),
0228     VF_STAT("rx_discards", current_stats.rx_discards),
0229     VF_STAT("rx_unknown_protocol", current_stats.rx_unknown_protocol),
0230     VF_STAT("tx_bytes", current_stats.tx_bytes),
0231     VF_STAT("tx_unicast", current_stats.tx_unicast),
0232     VF_STAT("tx_multicast", current_stats.tx_multicast),
0233     VF_STAT("tx_broadcast", current_stats.tx_broadcast),
0234     VF_STAT("tx_discards", current_stats.tx_discards),
0235     VF_STAT("tx_errors", current_stats.tx_errors),
0236 };
0237 
0238 #define IAVF_STATS_LEN  ARRAY_SIZE(iavf_gstrings_stats)
0239 
0240 #define IAVF_QUEUE_STATS_LEN    ARRAY_SIZE(iavf_gstrings_queue_stats)
0241 
0242 /* For now we have one and only one private flag and it is only defined
0243  * when we have support for the SKIP_CPU_SYNC DMA attribute.  Instead
0244  * of leaving all this code sitting around empty we will strip it unless
0245  * our one private flag is actually available.
0246  */
0247 struct iavf_priv_flags {
0248     char flag_string[ETH_GSTRING_LEN];
0249     u32 flag;
0250     bool read_only;
0251 };
0252 
0253 #define IAVF_PRIV_FLAG(_name, _flag, _read_only) { \
0254     .flag_string = _name, \
0255     .flag = _flag, \
0256     .read_only = _read_only, \
0257 }
0258 
0259 static const struct iavf_priv_flags iavf_gstrings_priv_flags[] = {
0260     IAVF_PRIV_FLAG("legacy-rx", IAVF_FLAG_LEGACY_RX, 0),
0261 };
0262 
0263 #define IAVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(iavf_gstrings_priv_flags)
0264 
0265 /**
0266  * iavf_get_link_ksettings - Get Link Speed and Duplex settings
0267  * @netdev: network interface device structure
0268  * @cmd: ethtool command
0269  *
0270  * Reports speed/duplex settings. Because this is a VF, we don't know what
0271  * kind of link we really have, so we fake it.
0272  **/
0273 static int iavf_get_link_ksettings(struct net_device *netdev,
0274                    struct ethtool_link_ksettings *cmd)
0275 {
0276     struct iavf_adapter *adapter = netdev_priv(netdev);
0277 
0278     ethtool_link_ksettings_zero_link_mode(cmd, supported);
0279     cmd->base.autoneg = AUTONEG_DISABLE;
0280     cmd->base.port = PORT_NONE;
0281     cmd->base.duplex = DUPLEX_FULL;
0282 
0283     if (ADV_LINK_SUPPORT(adapter)) {
0284         if (adapter->link_speed_mbps &&
0285             adapter->link_speed_mbps < U32_MAX)
0286             cmd->base.speed = adapter->link_speed_mbps;
0287         else
0288             cmd->base.speed = SPEED_UNKNOWN;
0289 
0290         return 0;
0291     }
0292 
0293     switch (adapter->link_speed) {
0294     case VIRTCHNL_LINK_SPEED_40GB:
0295         cmd->base.speed = SPEED_40000;
0296         break;
0297     case VIRTCHNL_LINK_SPEED_25GB:
0298         cmd->base.speed = SPEED_25000;
0299         break;
0300     case VIRTCHNL_LINK_SPEED_20GB:
0301         cmd->base.speed = SPEED_20000;
0302         break;
0303     case VIRTCHNL_LINK_SPEED_10GB:
0304         cmd->base.speed = SPEED_10000;
0305         break;
0306     case VIRTCHNL_LINK_SPEED_5GB:
0307         cmd->base.speed = SPEED_5000;
0308         break;
0309     case VIRTCHNL_LINK_SPEED_2_5GB:
0310         cmd->base.speed = SPEED_2500;
0311         break;
0312     case VIRTCHNL_LINK_SPEED_1GB:
0313         cmd->base.speed = SPEED_1000;
0314         break;
0315     case VIRTCHNL_LINK_SPEED_100MB:
0316         cmd->base.speed = SPEED_100;
0317         break;
0318     default:
0319         break;
0320     }
0321 
0322     return 0;
0323 }
0324 
0325 /**
0326  * iavf_get_sset_count - Get length of string set
0327  * @netdev: network interface device structure
0328  * @sset: id of string set
0329  *
0330  * Reports size of various string tables.
0331  **/
0332 static int iavf_get_sset_count(struct net_device *netdev, int sset)
0333 {
0334     /* Report the maximum number queues, even if not every queue is
0335      * currently configured. Since allocation of queues is in pairs,
0336      * use netdev->real_num_tx_queues * 2. The real_num_tx_queues is set
0337      * at device creation and never changes.
0338      */
0339 
0340     if (sset == ETH_SS_STATS)
0341         return IAVF_STATS_LEN +
0342             (IAVF_QUEUE_STATS_LEN * 2 *
0343              netdev->real_num_tx_queues);
0344     else if (sset == ETH_SS_PRIV_FLAGS)
0345         return IAVF_PRIV_FLAGS_STR_LEN;
0346     else
0347         return -EINVAL;
0348 }
0349 
0350 /**
0351  * iavf_get_ethtool_stats - report device statistics
0352  * @netdev: network interface device structure
0353  * @stats: ethtool statistics structure
0354  * @data: pointer to data buffer
0355  *
0356  * All statistics are added to the data buffer as an array of u64.
0357  **/
0358 static void iavf_get_ethtool_stats(struct net_device *netdev,
0359                    struct ethtool_stats *stats, u64 *data)
0360 {
0361     struct iavf_adapter *adapter = netdev_priv(netdev);
0362     unsigned int i;
0363 
0364     /* Explicitly request stats refresh */
0365     iavf_schedule_request_stats(adapter);
0366 
0367     iavf_add_ethtool_stats(&data, adapter, iavf_gstrings_stats);
0368 
0369     rcu_read_lock();
0370     /* As num_active_queues describe both tx and rx queues, we can use
0371      * it to iterate over rings' stats.
0372      */
0373     for (i = 0; i < adapter->num_active_queues; i++) {
0374         struct iavf_ring *ring;
0375 
0376         /* Tx rings stats */
0377         ring = &adapter->tx_rings[i];
0378         iavf_add_queue_stats(&data, ring);
0379 
0380         /* Rx rings stats */
0381         ring = &adapter->rx_rings[i];
0382         iavf_add_queue_stats(&data, ring);
0383     }
0384     rcu_read_unlock();
0385 }
0386 
0387 /**
0388  * iavf_get_priv_flag_strings - Get private flag strings
0389  * @netdev: network interface device structure
0390  * @data: buffer for string data
0391  *
0392  * Builds the private flags string table
0393  **/
0394 static void iavf_get_priv_flag_strings(struct net_device *netdev, u8 *data)
0395 {
0396     unsigned int i;
0397 
0398     for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
0399         snprintf(data, ETH_GSTRING_LEN, "%s",
0400              iavf_gstrings_priv_flags[i].flag_string);
0401         data += ETH_GSTRING_LEN;
0402     }
0403 }
0404 
0405 /**
0406  * iavf_get_stat_strings - Get stat strings
0407  * @netdev: network interface device structure
0408  * @data: buffer for string data
0409  *
0410  * Builds the statistics string table
0411  **/
0412 static void iavf_get_stat_strings(struct net_device *netdev, u8 *data)
0413 {
0414     unsigned int i;
0415 
0416     iavf_add_stat_strings(&data, iavf_gstrings_stats);
0417 
0418     /* Queues are always allocated in pairs, so we just use
0419      * real_num_tx_queues for both Tx and Rx queues.
0420      */
0421     for (i = 0; i < netdev->real_num_tx_queues; i++) {
0422         iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
0423                       "tx", i);
0424         iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
0425                       "rx", i);
0426     }
0427 }
0428 
0429 /**
0430  * iavf_get_strings - Get string set
0431  * @netdev: network interface device structure
0432  * @sset: id of string set
0433  * @data: buffer for string data
0434  *
0435  * Builds string tables for various string sets
0436  **/
0437 static void iavf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
0438 {
0439     switch (sset) {
0440     case ETH_SS_STATS:
0441         iavf_get_stat_strings(netdev, data);
0442         break;
0443     case ETH_SS_PRIV_FLAGS:
0444         iavf_get_priv_flag_strings(netdev, data);
0445         break;
0446     default:
0447         break;
0448     }
0449 }
0450 
0451 /**
0452  * iavf_get_priv_flags - report device private flags
0453  * @netdev: network interface device structure
0454  *
0455  * The get string set count and the string set should be matched for each
0456  * flag returned.  Add new strings for each flag to the iavf_gstrings_priv_flags
0457  * array.
0458  *
0459  * Returns a u32 bitmap of flags.
0460  **/
0461 static u32 iavf_get_priv_flags(struct net_device *netdev)
0462 {
0463     struct iavf_adapter *adapter = netdev_priv(netdev);
0464     u32 i, ret_flags = 0;
0465 
0466     for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
0467         const struct iavf_priv_flags *priv_flags;
0468 
0469         priv_flags = &iavf_gstrings_priv_flags[i];
0470 
0471         if (priv_flags->flag & adapter->flags)
0472             ret_flags |= BIT(i);
0473     }
0474 
0475     return ret_flags;
0476 }
0477 
0478 /**
0479  * iavf_set_priv_flags - set private flags
0480  * @netdev: network interface device structure
0481  * @flags: bit flags to be set
0482  **/
0483 static int iavf_set_priv_flags(struct net_device *netdev, u32 flags)
0484 {
0485     struct iavf_adapter *adapter = netdev_priv(netdev);
0486     u32 orig_flags, new_flags, changed_flags;
0487     u32 i;
0488 
0489     orig_flags = READ_ONCE(adapter->flags);
0490     new_flags = orig_flags;
0491 
0492     for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
0493         const struct iavf_priv_flags *priv_flags;
0494 
0495         priv_flags = &iavf_gstrings_priv_flags[i];
0496 
0497         if (flags & BIT(i))
0498             new_flags |= priv_flags->flag;
0499         else
0500             new_flags &= ~(priv_flags->flag);
0501 
0502         if (priv_flags->read_only &&
0503             ((orig_flags ^ new_flags) & ~BIT(i)))
0504             return -EOPNOTSUPP;
0505     }
0506 
0507     /* Before we finalize any flag changes, any checks which we need to
0508      * perform to determine if the new flags will be supported should go
0509      * here...
0510      */
0511 
0512     /* Compare and exchange the new flags into place. If we failed, that
0513      * is if cmpxchg returns anything but the old value, this means
0514      * something else must have modified the flags variable since we
0515      * copied it. We'll just punt with an error and log something in the
0516      * message buffer.
0517      */
0518     if (cmpxchg(&adapter->flags, orig_flags, new_flags) != orig_flags) {
0519         dev_warn(&adapter->pdev->dev,
0520              "Unable to update adapter->flags as it was modified by another thread...\n");
0521         return -EAGAIN;
0522     }
0523 
0524     changed_flags = orig_flags ^ new_flags;
0525 
0526     /* Process any additional changes needed as a result of flag changes.
0527      * The changed_flags value reflects the list of bits that were changed
0528      * in the code above.
0529      */
0530 
0531     /* issue a reset to force legacy-rx change to take effect */
0532     if (changed_flags & IAVF_FLAG_LEGACY_RX) {
0533         if (netif_running(netdev)) {
0534             adapter->flags |= IAVF_FLAG_RESET_NEEDED;
0535             queue_work(iavf_wq, &adapter->reset_task);
0536         }
0537     }
0538 
0539     return 0;
0540 }
0541 
0542 /**
0543  * iavf_get_msglevel - Get debug message level
0544  * @netdev: network interface device structure
0545  *
0546  * Returns current debug message level.
0547  **/
0548 static u32 iavf_get_msglevel(struct net_device *netdev)
0549 {
0550     struct iavf_adapter *adapter = netdev_priv(netdev);
0551 
0552     return adapter->msg_enable;
0553 }
0554 
0555 /**
0556  * iavf_set_msglevel - Set debug message level
0557  * @netdev: network interface device structure
0558  * @data: message level
0559  *
0560  * Set current debug message level. Higher values cause the driver to
0561  * be noisier.
0562  **/
0563 static void iavf_set_msglevel(struct net_device *netdev, u32 data)
0564 {
0565     struct iavf_adapter *adapter = netdev_priv(netdev);
0566 
0567     if (IAVF_DEBUG_USER & data)
0568         adapter->hw.debug_mask = data;
0569     adapter->msg_enable = data;
0570 }
0571 
0572 /**
0573  * iavf_get_drvinfo - Get driver info
0574  * @netdev: network interface device structure
0575  * @drvinfo: ethool driver info structure
0576  *
0577  * Returns information about the driver and device for display to the user.
0578  **/
0579 static void iavf_get_drvinfo(struct net_device *netdev,
0580                  struct ethtool_drvinfo *drvinfo)
0581 {
0582     struct iavf_adapter *adapter = netdev_priv(netdev);
0583 
0584     strlcpy(drvinfo->driver, iavf_driver_name, 32);
0585     strlcpy(drvinfo->fw_version, "N/A", 4);
0586     strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
0587     drvinfo->n_priv_flags = IAVF_PRIV_FLAGS_STR_LEN;
0588 }
0589 
0590 /**
0591  * iavf_get_ringparam - Get ring parameters
0592  * @netdev: network interface device structure
0593  * @ring: ethtool ringparam structure
0594  * @kernel_ring: ethtool extenal ringparam structure
0595  * @extack: netlink extended ACK report struct
0596  *
0597  * Returns current ring parameters. TX and RX rings are reported separately,
0598  * but the number of rings is not reported.
0599  **/
0600 static void iavf_get_ringparam(struct net_device *netdev,
0601                    struct ethtool_ringparam *ring,
0602                    struct kernel_ethtool_ringparam *kernel_ring,
0603                    struct netlink_ext_ack *extack)
0604 {
0605     struct iavf_adapter *adapter = netdev_priv(netdev);
0606 
0607     ring->rx_max_pending = IAVF_MAX_RXD;
0608     ring->tx_max_pending = IAVF_MAX_TXD;
0609     ring->rx_pending = adapter->rx_desc_count;
0610     ring->tx_pending = adapter->tx_desc_count;
0611 }
0612 
0613 /**
0614  * iavf_set_ringparam - Set ring parameters
0615  * @netdev: network interface device structure
0616  * @ring: ethtool ringparam structure
0617  * @kernel_ring: ethtool external ringparam structure
0618  * @extack: netlink extended ACK report struct
0619  *
0620  * Sets ring parameters. TX and RX rings are controlled separately, but the
0621  * number of rings is not specified, so all rings get the same settings.
0622  **/
0623 static int iavf_set_ringparam(struct net_device *netdev,
0624                   struct ethtool_ringparam *ring,
0625                   struct kernel_ethtool_ringparam *kernel_ring,
0626                   struct netlink_ext_ack *extack)
0627 {
0628     struct iavf_adapter *adapter = netdev_priv(netdev);
0629     u32 new_rx_count, new_tx_count;
0630 
0631     if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
0632         return -EINVAL;
0633 
0634     if (ring->tx_pending > IAVF_MAX_TXD ||
0635         ring->tx_pending < IAVF_MIN_TXD ||
0636         ring->rx_pending > IAVF_MAX_RXD ||
0637         ring->rx_pending < IAVF_MIN_RXD) {
0638         netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
0639                ring->tx_pending, ring->rx_pending, IAVF_MIN_TXD,
0640                IAVF_MAX_RXD, IAVF_REQ_DESCRIPTOR_MULTIPLE);
0641         return -EINVAL;
0642     }
0643 
0644     new_tx_count = ALIGN(ring->tx_pending, IAVF_REQ_DESCRIPTOR_MULTIPLE);
0645     if (new_tx_count != ring->tx_pending)
0646         netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n",
0647                 new_tx_count);
0648 
0649     new_rx_count = ALIGN(ring->rx_pending, IAVF_REQ_DESCRIPTOR_MULTIPLE);
0650     if (new_rx_count != ring->rx_pending)
0651         netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n",
0652                 new_rx_count);
0653 
0654     /* if nothing to do return success */
0655     if ((new_tx_count == adapter->tx_desc_count) &&
0656         (new_rx_count == adapter->rx_desc_count)) {
0657         netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
0658         return 0;
0659     }
0660 
0661     if (new_tx_count != adapter->tx_desc_count) {
0662         netdev_dbg(netdev, "Changing Tx descriptor count from %d to %d\n",
0663                adapter->tx_desc_count, new_tx_count);
0664         adapter->tx_desc_count = new_tx_count;
0665     }
0666 
0667     if (new_rx_count != adapter->rx_desc_count) {
0668         netdev_dbg(netdev, "Changing Rx descriptor count from %d to %d\n",
0669                adapter->rx_desc_count, new_rx_count);
0670         adapter->rx_desc_count = new_rx_count;
0671     }
0672 
0673     if (netif_running(netdev)) {
0674         adapter->flags |= IAVF_FLAG_RESET_NEEDED;
0675         queue_work(iavf_wq, &adapter->reset_task);
0676     }
0677 
0678     return 0;
0679 }
0680 
0681 /**
0682  * __iavf_get_coalesce - get per-queue coalesce settings
0683  * @netdev: the netdev to check
0684  * @ec: ethtool coalesce data structure
0685  * @queue: which queue to pick
0686  *
0687  * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
0688  * are per queue. If queue is <0 then we default to queue 0 as the
0689  * representative value.
0690  **/
0691 static int __iavf_get_coalesce(struct net_device *netdev,
0692                    struct ethtool_coalesce *ec, int queue)
0693 {
0694     struct iavf_adapter *adapter = netdev_priv(netdev);
0695     struct iavf_ring *rx_ring, *tx_ring;
0696 
0697     /* Rx and Tx usecs per queue value. If user doesn't specify the
0698      * queue, return queue 0's value to represent.
0699      */
0700     if (queue < 0)
0701         queue = 0;
0702     else if (queue >= adapter->num_active_queues)
0703         return -EINVAL;
0704 
0705     rx_ring = &adapter->rx_rings[queue];
0706     tx_ring = &adapter->tx_rings[queue];
0707 
0708     if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
0709         ec->use_adaptive_rx_coalesce = 1;
0710 
0711     if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
0712         ec->use_adaptive_tx_coalesce = 1;
0713 
0714     ec->rx_coalesce_usecs = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
0715     ec->tx_coalesce_usecs = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
0716 
0717     return 0;
0718 }
0719 
0720 /**
0721  * iavf_get_coalesce - Get interrupt coalescing settings
0722  * @netdev: network interface device structure
0723  * @ec: ethtool coalesce structure
0724  * @kernel_coal: ethtool CQE mode setting structure
0725  * @extack: extack for reporting error messages
0726  *
0727  * Returns current coalescing settings. This is referred to elsewhere in the
0728  * driver as Interrupt Throttle Rate, as this is how the hardware describes
0729  * this functionality. Note that if per-queue settings have been modified this
0730  * only represents the settings of queue 0.
0731  **/
0732 static int iavf_get_coalesce(struct net_device *netdev,
0733                  struct ethtool_coalesce *ec,
0734                  struct kernel_ethtool_coalesce *kernel_coal,
0735                  struct netlink_ext_ack *extack)
0736 {
0737     return __iavf_get_coalesce(netdev, ec, -1);
0738 }
0739 
0740 /**
0741  * iavf_get_per_queue_coalesce - get coalesce values for specific queue
0742  * @netdev: netdev to read
0743  * @ec: coalesce settings from ethtool
0744  * @queue: the queue to read
0745  *
0746  * Read specific queue's coalesce settings.
0747  **/
0748 static int iavf_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
0749                        struct ethtool_coalesce *ec)
0750 {
0751     return __iavf_get_coalesce(netdev, ec, queue);
0752 }
0753 
0754 /**
0755  * iavf_set_itr_per_queue - set ITR values for specific queue
0756  * @adapter: the VF adapter struct to set values for
0757  * @ec: coalesce settings from ethtool
0758  * @queue: the queue to modify
0759  *
0760  * Change the ITR settings for a specific queue.
0761  **/
0762 static int iavf_set_itr_per_queue(struct iavf_adapter *adapter,
0763                   struct ethtool_coalesce *ec, int queue)
0764 {
0765     struct iavf_ring *rx_ring = &adapter->rx_rings[queue];
0766     struct iavf_ring *tx_ring = &adapter->tx_rings[queue];
0767     struct iavf_q_vector *q_vector;
0768     u16 itr_setting;
0769 
0770     itr_setting = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
0771 
0772     if (ec->rx_coalesce_usecs != itr_setting &&
0773         ec->use_adaptive_rx_coalesce) {
0774         netif_info(adapter, drv, adapter->netdev,
0775                "Rx interrupt throttling cannot be changed if adaptive-rx is enabled\n");
0776         return -EINVAL;
0777     }
0778 
0779     itr_setting = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
0780 
0781     if (ec->tx_coalesce_usecs != itr_setting &&
0782         ec->use_adaptive_tx_coalesce) {
0783         netif_info(adapter, drv, adapter->netdev,
0784                "Tx interrupt throttling cannot be changed if adaptive-tx is enabled\n");
0785         return -EINVAL;
0786     }
0787 
0788     rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
0789     tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
0790 
0791     rx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
0792     if (!ec->use_adaptive_rx_coalesce)
0793         rx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
0794 
0795     tx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
0796     if (!ec->use_adaptive_tx_coalesce)
0797         tx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
0798 
0799     q_vector = rx_ring->q_vector;
0800     q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
0801 
0802     q_vector = tx_ring->q_vector;
0803     q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
0804 
0805     /* The interrupt handler itself will take care of programming
0806      * the Tx and Rx ITR values based on the values we have entered
0807      * into the q_vector, no need to write the values now.
0808      */
0809     return 0;
0810 }
0811 
0812 /**
0813  * __iavf_set_coalesce - set coalesce settings for particular queue
0814  * @netdev: the netdev to change
0815  * @ec: ethtool coalesce settings
0816  * @queue: the queue to change
0817  *
0818  * Sets the coalesce settings for a particular queue.
0819  **/
0820 static int __iavf_set_coalesce(struct net_device *netdev,
0821                    struct ethtool_coalesce *ec, int queue)
0822 {
0823     struct iavf_adapter *adapter = netdev_priv(netdev);
0824     int i;
0825 
0826     if (ec->rx_coalesce_usecs == 0) {
0827         if (ec->use_adaptive_rx_coalesce)
0828             netif_info(adapter, drv, netdev, "rx-usecs=0, need to disable adaptive-rx for a complete disable\n");
0829     } else if ((ec->rx_coalesce_usecs < IAVF_MIN_ITR) ||
0830            (ec->rx_coalesce_usecs > IAVF_MAX_ITR)) {
0831         netif_info(adapter, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
0832         return -EINVAL;
0833     } else if (ec->tx_coalesce_usecs == 0) {
0834         if (ec->use_adaptive_tx_coalesce)
0835             netif_info(adapter, drv, netdev, "tx-usecs=0, need to disable adaptive-tx for a complete disable\n");
0836     } else if ((ec->tx_coalesce_usecs < IAVF_MIN_ITR) ||
0837            (ec->tx_coalesce_usecs > IAVF_MAX_ITR)) {
0838         netif_info(adapter, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
0839         return -EINVAL;
0840     }
0841 
0842     /* Rx and Tx usecs has per queue value. If user doesn't specify the
0843      * queue, apply to all queues.
0844      */
0845     if (queue < 0) {
0846         for (i = 0; i < adapter->num_active_queues; i++)
0847             if (iavf_set_itr_per_queue(adapter, ec, i))
0848                 return -EINVAL;
0849     } else if (queue < adapter->num_active_queues) {
0850         if (iavf_set_itr_per_queue(adapter, ec, queue))
0851             return -EINVAL;
0852     } else {
0853         netif_info(adapter, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
0854                adapter->num_active_queues - 1);
0855         return -EINVAL;
0856     }
0857 
0858     return 0;
0859 }
0860 
0861 /**
0862  * iavf_set_coalesce - Set interrupt coalescing settings
0863  * @netdev: network interface device structure
0864  * @ec: ethtool coalesce structure
0865  * @kernel_coal: ethtool CQE mode setting structure
0866  * @extack: extack for reporting error messages
0867  *
0868  * Change current coalescing settings for every queue.
0869  **/
0870 static int iavf_set_coalesce(struct net_device *netdev,
0871                  struct ethtool_coalesce *ec,
0872                  struct kernel_ethtool_coalesce *kernel_coal,
0873                  struct netlink_ext_ack *extack)
0874 {
0875     return __iavf_set_coalesce(netdev, ec, -1);
0876 }
0877 
0878 /**
0879  * iavf_set_per_queue_coalesce - set specific queue's coalesce settings
0880  * @netdev: the netdev to change
0881  * @ec: ethtool's coalesce settings
0882  * @queue: the queue to modify
0883  *
0884  * Modifies a specific queue's coalesce settings.
0885  */
0886 static int iavf_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
0887                        struct ethtool_coalesce *ec)
0888 {
0889     return __iavf_set_coalesce(netdev, ec, queue);
0890 }
0891 
0892 /**
0893  * iavf_fltr_to_ethtool_flow - convert filter type values to ethtool
0894  * flow type values
0895  * @flow: filter type to be converted
0896  *
0897  * Returns the corresponding ethtool flow type.
0898  */
0899 static int iavf_fltr_to_ethtool_flow(enum iavf_fdir_flow_type flow)
0900 {
0901     switch (flow) {
0902     case IAVF_FDIR_FLOW_IPV4_TCP:
0903         return TCP_V4_FLOW;
0904     case IAVF_FDIR_FLOW_IPV4_UDP:
0905         return UDP_V4_FLOW;
0906     case IAVF_FDIR_FLOW_IPV4_SCTP:
0907         return SCTP_V4_FLOW;
0908     case IAVF_FDIR_FLOW_IPV4_AH:
0909         return AH_V4_FLOW;
0910     case IAVF_FDIR_FLOW_IPV4_ESP:
0911         return ESP_V4_FLOW;
0912     case IAVF_FDIR_FLOW_IPV4_OTHER:
0913         return IPV4_USER_FLOW;
0914     case IAVF_FDIR_FLOW_IPV6_TCP:
0915         return TCP_V6_FLOW;
0916     case IAVF_FDIR_FLOW_IPV6_UDP:
0917         return UDP_V6_FLOW;
0918     case IAVF_FDIR_FLOW_IPV6_SCTP:
0919         return SCTP_V6_FLOW;
0920     case IAVF_FDIR_FLOW_IPV6_AH:
0921         return AH_V6_FLOW;
0922     case IAVF_FDIR_FLOW_IPV6_ESP:
0923         return ESP_V6_FLOW;
0924     case IAVF_FDIR_FLOW_IPV6_OTHER:
0925         return IPV6_USER_FLOW;
0926     case IAVF_FDIR_FLOW_NON_IP_L2:
0927         return ETHER_FLOW;
0928     default:
0929         /* 0 is undefined ethtool flow */
0930         return 0;
0931     }
0932 }
0933 
0934 /**
0935  * iavf_ethtool_flow_to_fltr - convert ethtool flow type to filter enum
0936  * @eth: Ethtool flow type to be converted
0937  *
0938  * Returns flow enum
0939  */
0940 static enum iavf_fdir_flow_type iavf_ethtool_flow_to_fltr(int eth)
0941 {
0942     switch (eth) {
0943     case TCP_V4_FLOW:
0944         return IAVF_FDIR_FLOW_IPV4_TCP;
0945     case UDP_V4_FLOW:
0946         return IAVF_FDIR_FLOW_IPV4_UDP;
0947     case SCTP_V4_FLOW:
0948         return IAVF_FDIR_FLOW_IPV4_SCTP;
0949     case AH_V4_FLOW:
0950         return IAVF_FDIR_FLOW_IPV4_AH;
0951     case ESP_V4_FLOW:
0952         return IAVF_FDIR_FLOW_IPV4_ESP;
0953     case IPV4_USER_FLOW:
0954         return IAVF_FDIR_FLOW_IPV4_OTHER;
0955     case TCP_V6_FLOW:
0956         return IAVF_FDIR_FLOW_IPV6_TCP;
0957     case UDP_V6_FLOW:
0958         return IAVF_FDIR_FLOW_IPV6_UDP;
0959     case SCTP_V6_FLOW:
0960         return IAVF_FDIR_FLOW_IPV6_SCTP;
0961     case AH_V6_FLOW:
0962         return IAVF_FDIR_FLOW_IPV6_AH;
0963     case ESP_V6_FLOW:
0964         return IAVF_FDIR_FLOW_IPV6_ESP;
0965     case IPV6_USER_FLOW:
0966         return IAVF_FDIR_FLOW_IPV6_OTHER;
0967     case ETHER_FLOW:
0968         return IAVF_FDIR_FLOW_NON_IP_L2;
0969     default:
0970         return IAVF_FDIR_FLOW_NONE;
0971     }
0972 }
0973 
0974 /**
0975  * iavf_is_mask_valid - check mask field set
0976  * @mask: full mask to check
0977  * @field: field for which mask should be valid
0978  *
0979  * If the mask is fully set return true. If it is not valid for field return
0980  * false.
0981  */
0982 static bool iavf_is_mask_valid(u64 mask, u64 field)
0983 {
0984     return (mask & field) == field;
0985 }
0986 
0987 /**
0988  * iavf_parse_rx_flow_user_data - deconstruct user-defined data
0989  * @fsp: pointer to ethtool Rx flow specification
0990  * @fltr: pointer to Flow Director filter for userdef data storage
0991  *
0992  * Returns 0 on success, negative error value on failure
0993  */
0994 static int
0995 iavf_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
0996                  struct iavf_fdir_fltr *fltr)
0997 {
0998     struct iavf_flex_word *flex;
0999     int i, cnt = 0;
1000 
1001     if (!(fsp->flow_type & FLOW_EXT))
1002         return 0;
1003 
1004     for (i = 0; i < IAVF_FLEX_WORD_NUM; i++) {
1005 #define IAVF_USERDEF_FLEX_WORD_M    GENMASK(15, 0)
1006 #define IAVF_USERDEF_FLEX_OFFS_S    16
1007 #define IAVF_USERDEF_FLEX_OFFS_M    GENMASK(31, IAVF_USERDEF_FLEX_OFFS_S)
1008 #define IAVF_USERDEF_FLEX_FLTR_M    GENMASK(31, 0)
1009         u32 value = be32_to_cpu(fsp->h_ext.data[i]);
1010         u32 mask = be32_to_cpu(fsp->m_ext.data[i]);
1011 
1012         if (!value || !mask)
1013             continue;
1014 
1015         if (!iavf_is_mask_valid(mask, IAVF_USERDEF_FLEX_FLTR_M))
1016             return -EINVAL;
1017 
1018         /* 504 is the maximum value for offsets, and offset is measured
1019          * from the start of the MAC address.
1020          */
1021 #define IAVF_USERDEF_FLEX_MAX_OFFS_VAL 504
1022         flex = &fltr->flex_words[cnt++];
1023         flex->word = value & IAVF_USERDEF_FLEX_WORD_M;
1024         flex->offset = (value & IAVF_USERDEF_FLEX_OFFS_M) >>
1025                  IAVF_USERDEF_FLEX_OFFS_S;
1026         if (flex->offset > IAVF_USERDEF_FLEX_MAX_OFFS_VAL)
1027             return -EINVAL;
1028     }
1029 
1030     fltr->flex_cnt = cnt;
1031 
1032     return 0;
1033 }
1034 
1035 /**
1036  * iavf_fill_rx_flow_ext_data - fill the additional data
1037  * @fsp: pointer to ethtool Rx flow specification
1038  * @fltr: pointer to Flow Director filter to get additional data
1039  */
1040 static void
1041 iavf_fill_rx_flow_ext_data(struct ethtool_rx_flow_spec *fsp,
1042                struct iavf_fdir_fltr *fltr)
1043 {
1044     if (!fltr->ext_mask.usr_def[0] && !fltr->ext_mask.usr_def[1])
1045         return;
1046 
1047     fsp->flow_type |= FLOW_EXT;
1048 
1049     memcpy(fsp->h_ext.data, fltr->ext_data.usr_def, sizeof(fsp->h_ext.data));
1050     memcpy(fsp->m_ext.data, fltr->ext_mask.usr_def, sizeof(fsp->m_ext.data));
1051 }
1052 
1053 /**
1054  * iavf_get_ethtool_fdir_entry - fill ethtool structure with Flow Director filter data
1055  * @adapter: the VF adapter structure that contains filter list
1056  * @cmd: ethtool command data structure to receive the filter data
1057  *
1058  * Returns 0 as expected for success by ethtool
1059  */
1060 static int
1061 iavf_get_ethtool_fdir_entry(struct iavf_adapter *adapter,
1062                 struct ethtool_rxnfc *cmd)
1063 {
1064     struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1065     struct iavf_fdir_fltr *rule = NULL;
1066     int ret = 0;
1067 
1068     if (!FDIR_FLTR_SUPPORT(adapter))
1069         return -EOPNOTSUPP;
1070 
1071     spin_lock_bh(&adapter->fdir_fltr_lock);
1072 
1073     rule = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1074     if (!rule) {
1075         ret = -EINVAL;
1076         goto release_lock;
1077     }
1078 
1079     fsp->flow_type = iavf_fltr_to_ethtool_flow(rule->flow_type);
1080 
1081     memset(&fsp->m_u, 0, sizeof(fsp->m_u));
1082     memset(&fsp->m_ext, 0, sizeof(fsp->m_ext));
1083 
1084     switch (fsp->flow_type) {
1085     case TCP_V4_FLOW:
1086     case UDP_V4_FLOW:
1087     case SCTP_V4_FLOW:
1088         fsp->h_u.tcp_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1089         fsp->h_u.tcp_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1090         fsp->h_u.tcp_ip4_spec.psrc = rule->ip_data.src_port;
1091         fsp->h_u.tcp_ip4_spec.pdst = rule->ip_data.dst_port;
1092         fsp->h_u.tcp_ip4_spec.tos = rule->ip_data.tos;
1093         fsp->m_u.tcp_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1094         fsp->m_u.tcp_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1095         fsp->m_u.tcp_ip4_spec.psrc = rule->ip_mask.src_port;
1096         fsp->m_u.tcp_ip4_spec.pdst = rule->ip_mask.dst_port;
1097         fsp->m_u.tcp_ip4_spec.tos = rule->ip_mask.tos;
1098         break;
1099     case AH_V4_FLOW:
1100     case ESP_V4_FLOW:
1101         fsp->h_u.ah_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1102         fsp->h_u.ah_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1103         fsp->h_u.ah_ip4_spec.spi = rule->ip_data.spi;
1104         fsp->h_u.ah_ip4_spec.tos = rule->ip_data.tos;
1105         fsp->m_u.ah_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1106         fsp->m_u.ah_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1107         fsp->m_u.ah_ip4_spec.spi = rule->ip_mask.spi;
1108         fsp->m_u.ah_ip4_spec.tos = rule->ip_mask.tos;
1109         break;
1110     case IPV4_USER_FLOW:
1111         fsp->h_u.usr_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1112         fsp->h_u.usr_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1113         fsp->h_u.usr_ip4_spec.l4_4_bytes = rule->ip_data.l4_header;
1114         fsp->h_u.usr_ip4_spec.tos = rule->ip_data.tos;
1115         fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
1116         fsp->h_u.usr_ip4_spec.proto = rule->ip_data.proto;
1117         fsp->m_u.usr_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1118         fsp->m_u.usr_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1119         fsp->m_u.usr_ip4_spec.l4_4_bytes = rule->ip_mask.l4_header;
1120         fsp->m_u.usr_ip4_spec.tos = rule->ip_mask.tos;
1121         fsp->m_u.usr_ip4_spec.ip_ver = 0xFF;
1122         fsp->m_u.usr_ip4_spec.proto = rule->ip_mask.proto;
1123         break;
1124     case TCP_V6_FLOW:
1125     case UDP_V6_FLOW:
1126     case SCTP_V6_FLOW:
1127         memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1128                sizeof(struct in6_addr));
1129         memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1130                sizeof(struct in6_addr));
1131         fsp->h_u.tcp_ip6_spec.psrc = rule->ip_data.src_port;
1132         fsp->h_u.tcp_ip6_spec.pdst = rule->ip_data.dst_port;
1133         fsp->h_u.tcp_ip6_spec.tclass = rule->ip_data.tclass;
1134         memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1135                sizeof(struct in6_addr));
1136         memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1137                sizeof(struct in6_addr));
1138         fsp->m_u.tcp_ip6_spec.psrc = rule->ip_mask.src_port;
1139         fsp->m_u.tcp_ip6_spec.pdst = rule->ip_mask.dst_port;
1140         fsp->m_u.tcp_ip6_spec.tclass = rule->ip_mask.tclass;
1141         break;
1142     case AH_V6_FLOW:
1143     case ESP_V6_FLOW:
1144         memcpy(fsp->h_u.ah_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1145                sizeof(struct in6_addr));
1146         memcpy(fsp->h_u.ah_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1147                sizeof(struct in6_addr));
1148         fsp->h_u.ah_ip6_spec.spi = rule->ip_data.spi;
1149         fsp->h_u.ah_ip6_spec.tclass = rule->ip_data.tclass;
1150         memcpy(fsp->m_u.ah_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1151                sizeof(struct in6_addr));
1152         memcpy(fsp->m_u.ah_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1153                sizeof(struct in6_addr));
1154         fsp->m_u.ah_ip6_spec.spi = rule->ip_mask.spi;
1155         fsp->m_u.ah_ip6_spec.tclass = rule->ip_mask.tclass;
1156         break;
1157     case IPV6_USER_FLOW:
1158         memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1159                sizeof(struct in6_addr));
1160         memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1161                sizeof(struct in6_addr));
1162         fsp->h_u.usr_ip6_spec.l4_4_bytes = rule->ip_data.l4_header;
1163         fsp->h_u.usr_ip6_spec.tclass = rule->ip_data.tclass;
1164         fsp->h_u.usr_ip6_spec.l4_proto = rule->ip_data.proto;
1165         memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1166                sizeof(struct in6_addr));
1167         memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1168                sizeof(struct in6_addr));
1169         fsp->m_u.usr_ip6_spec.l4_4_bytes = rule->ip_mask.l4_header;
1170         fsp->m_u.usr_ip6_spec.tclass = rule->ip_mask.tclass;
1171         fsp->m_u.usr_ip6_spec.l4_proto = rule->ip_mask.proto;
1172         break;
1173     case ETHER_FLOW:
1174         fsp->h_u.ether_spec.h_proto = rule->eth_data.etype;
1175         fsp->m_u.ether_spec.h_proto = rule->eth_mask.etype;
1176         break;
1177     default:
1178         ret = -EINVAL;
1179         break;
1180     }
1181 
1182     iavf_fill_rx_flow_ext_data(fsp, rule);
1183 
1184     if (rule->action == VIRTCHNL_ACTION_DROP)
1185         fsp->ring_cookie = RX_CLS_FLOW_DISC;
1186     else
1187         fsp->ring_cookie = rule->q_index;
1188 
1189 release_lock:
1190     spin_unlock_bh(&adapter->fdir_fltr_lock);
1191     return ret;
1192 }
1193 
1194 /**
1195  * iavf_get_fdir_fltr_ids - fill buffer with filter IDs of active filters
1196  * @adapter: the VF adapter structure containing the filter list
1197  * @cmd: ethtool command data structure
1198  * @rule_locs: ethtool array passed in from OS to receive filter IDs
1199  *
1200  * Returns 0 as expected for success by ethtool
1201  */
1202 static int
1203 iavf_get_fdir_fltr_ids(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd,
1204                u32 *rule_locs)
1205 {
1206     struct iavf_fdir_fltr *fltr;
1207     unsigned int cnt = 0;
1208     int val = 0;
1209 
1210     if (!FDIR_FLTR_SUPPORT(adapter))
1211         return -EOPNOTSUPP;
1212 
1213     cmd->data = IAVF_MAX_FDIR_FILTERS;
1214 
1215     spin_lock_bh(&adapter->fdir_fltr_lock);
1216 
1217     list_for_each_entry(fltr, &adapter->fdir_list_head, list) {
1218         if (cnt == cmd->rule_cnt) {
1219             val = -EMSGSIZE;
1220             goto release_lock;
1221         }
1222         rule_locs[cnt] = fltr->loc;
1223         cnt++;
1224     }
1225 
1226 release_lock:
1227     spin_unlock_bh(&adapter->fdir_fltr_lock);
1228     if (!val)
1229         cmd->rule_cnt = cnt;
1230 
1231     return val;
1232 }
1233 
1234 /**
1235  * iavf_add_fdir_fltr_info - Set the input set for Flow Director filter
1236  * @adapter: pointer to the VF adapter structure
1237  * @fsp: pointer to ethtool Rx flow specification
1238  * @fltr: filter structure
1239  */
1240 static int
1241 iavf_add_fdir_fltr_info(struct iavf_adapter *adapter, struct ethtool_rx_flow_spec *fsp,
1242             struct iavf_fdir_fltr *fltr)
1243 {
1244     u32 flow_type, q_index = 0;
1245     enum virtchnl_action act;
1246     int err;
1247 
1248     if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
1249         act = VIRTCHNL_ACTION_DROP;
1250     } else {
1251         q_index = fsp->ring_cookie;
1252         if (q_index >= adapter->num_active_queues)
1253             return -EINVAL;
1254 
1255         act = VIRTCHNL_ACTION_QUEUE;
1256     }
1257 
1258     fltr->action = act;
1259     fltr->loc = fsp->location;
1260     fltr->q_index = q_index;
1261 
1262     if (fsp->flow_type & FLOW_EXT) {
1263         memcpy(fltr->ext_data.usr_def, fsp->h_ext.data,
1264                sizeof(fltr->ext_data.usr_def));
1265         memcpy(fltr->ext_mask.usr_def, fsp->m_ext.data,
1266                sizeof(fltr->ext_mask.usr_def));
1267     }
1268 
1269     flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
1270     fltr->flow_type = iavf_ethtool_flow_to_fltr(flow_type);
1271 
1272     switch (flow_type) {
1273     case TCP_V4_FLOW:
1274     case UDP_V4_FLOW:
1275     case SCTP_V4_FLOW:
1276         fltr->ip_data.v4_addrs.src_ip = fsp->h_u.tcp_ip4_spec.ip4src;
1277         fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
1278         fltr->ip_data.src_port = fsp->h_u.tcp_ip4_spec.psrc;
1279         fltr->ip_data.dst_port = fsp->h_u.tcp_ip4_spec.pdst;
1280         fltr->ip_data.tos = fsp->h_u.tcp_ip4_spec.tos;
1281         fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.tcp_ip4_spec.ip4src;
1282         fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.tcp_ip4_spec.ip4dst;
1283         fltr->ip_mask.src_port = fsp->m_u.tcp_ip4_spec.psrc;
1284         fltr->ip_mask.dst_port = fsp->m_u.tcp_ip4_spec.pdst;
1285         fltr->ip_mask.tos = fsp->m_u.tcp_ip4_spec.tos;
1286         break;
1287     case AH_V4_FLOW:
1288     case ESP_V4_FLOW:
1289         fltr->ip_data.v4_addrs.src_ip = fsp->h_u.ah_ip4_spec.ip4src;
1290         fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.ah_ip4_spec.ip4dst;
1291         fltr->ip_data.spi = fsp->h_u.ah_ip4_spec.spi;
1292         fltr->ip_data.tos = fsp->h_u.ah_ip4_spec.tos;
1293         fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.ah_ip4_spec.ip4src;
1294         fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.ah_ip4_spec.ip4dst;
1295         fltr->ip_mask.spi = fsp->m_u.ah_ip4_spec.spi;
1296         fltr->ip_mask.tos = fsp->m_u.ah_ip4_spec.tos;
1297         break;
1298     case IPV4_USER_FLOW:
1299         fltr->ip_data.v4_addrs.src_ip = fsp->h_u.usr_ip4_spec.ip4src;
1300         fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.usr_ip4_spec.ip4dst;
1301         fltr->ip_data.l4_header = fsp->h_u.usr_ip4_spec.l4_4_bytes;
1302         fltr->ip_data.tos = fsp->h_u.usr_ip4_spec.tos;
1303         fltr->ip_data.proto = fsp->h_u.usr_ip4_spec.proto;
1304         fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.usr_ip4_spec.ip4src;
1305         fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.usr_ip4_spec.ip4dst;
1306         fltr->ip_mask.l4_header = fsp->m_u.usr_ip4_spec.l4_4_bytes;
1307         fltr->ip_mask.tos = fsp->m_u.usr_ip4_spec.tos;
1308         fltr->ip_mask.proto = fsp->m_u.usr_ip4_spec.proto;
1309         break;
1310     case TCP_V6_FLOW:
1311     case UDP_V6_FLOW:
1312     case SCTP_V6_FLOW:
1313         memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1314                sizeof(struct in6_addr));
1315         memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1316                sizeof(struct in6_addr));
1317         fltr->ip_data.src_port = fsp->h_u.tcp_ip6_spec.psrc;
1318         fltr->ip_data.dst_port = fsp->h_u.tcp_ip6_spec.pdst;
1319         fltr->ip_data.tclass = fsp->h_u.tcp_ip6_spec.tclass;
1320         memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1321                sizeof(struct in6_addr));
1322         memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1323                sizeof(struct in6_addr));
1324         fltr->ip_mask.src_port = fsp->m_u.tcp_ip6_spec.psrc;
1325         fltr->ip_mask.dst_port = fsp->m_u.tcp_ip6_spec.pdst;
1326         fltr->ip_mask.tclass = fsp->m_u.tcp_ip6_spec.tclass;
1327         break;
1328     case AH_V6_FLOW:
1329     case ESP_V6_FLOW:
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;
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         break;
1359     case ETHER_FLOW:
1360         fltr->eth_data.etype = fsp->h_u.ether_spec.h_proto;
1361         fltr->eth_mask.etype = fsp->m_u.ether_spec.h_proto;
1362         break;
1363     default:
1364         /* not doing un-parsed flow types */
1365         return -EINVAL;
1366     }
1367 
1368     if (iavf_fdir_is_dup_fltr(adapter, fltr))
1369         return -EEXIST;
1370 
1371     err = iavf_parse_rx_flow_user_data(fsp, fltr);
1372     if (err)
1373         return err;
1374 
1375     return iavf_fill_fdir_add_msg(adapter, fltr);
1376 }
1377 
1378 /**
1379  * iavf_add_fdir_ethtool - add Flow Director filter
1380  * @adapter: pointer to the VF adapter structure
1381  * @cmd: command to add Flow Director filter
1382  *
1383  * Returns 0 on success and negative values for failure
1384  */
1385 static int iavf_add_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1386 {
1387     struct ethtool_rx_flow_spec *fsp = &cmd->fs;
1388     struct iavf_fdir_fltr *fltr;
1389     int count = 50;
1390     int err;
1391 
1392     if (!FDIR_FLTR_SUPPORT(adapter))
1393         return -EOPNOTSUPP;
1394 
1395     if (fsp->flow_type & FLOW_MAC_EXT)
1396         return -EINVAL;
1397 
1398     if (adapter->fdir_active_fltr >= IAVF_MAX_FDIR_FILTERS) {
1399         dev_err(&adapter->pdev->dev,
1400             "Unable to add Flow Director filter because VF reached the limit of max allowed filters (%u)\n",
1401             IAVF_MAX_FDIR_FILTERS);
1402         return -ENOSPC;
1403     }
1404 
1405     spin_lock_bh(&adapter->fdir_fltr_lock);
1406     if (iavf_find_fdir_fltr_by_loc(adapter, fsp->location)) {
1407         dev_err(&adapter->pdev->dev, "Failed to add Flow Director filter, it already exists\n");
1408         spin_unlock_bh(&adapter->fdir_fltr_lock);
1409         return -EEXIST;
1410     }
1411     spin_unlock_bh(&adapter->fdir_fltr_lock);
1412 
1413     fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1414     if (!fltr)
1415         return -ENOMEM;
1416 
1417     while (!mutex_trylock(&adapter->crit_lock)) {
1418         if (--count == 0) {
1419             kfree(fltr);
1420             return -EINVAL;
1421         }
1422         udelay(1);
1423     }
1424 
1425     err = iavf_add_fdir_fltr_info(adapter, fsp, fltr);
1426     if (err)
1427         goto ret;
1428 
1429     spin_lock_bh(&adapter->fdir_fltr_lock);
1430     iavf_fdir_list_add_fltr(adapter, fltr);
1431     adapter->fdir_active_fltr++;
1432     fltr->state = IAVF_FDIR_FLTR_ADD_REQUEST;
1433     adapter->aq_required |= IAVF_FLAG_AQ_ADD_FDIR_FILTER;
1434     spin_unlock_bh(&adapter->fdir_fltr_lock);
1435 
1436     mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1437 
1438 ret:
1439     if (err && fltr)
1440         kfree(fltr);
1441 
1442     mutex_unlock(&adapter->crit_lock);
1443     return err;
1444 }
1445 
1446 /**
1447  * iavf_del_fdir_ethtool - delete Flow Director filter
1448  * @adapter: pointer to the VF adapter structure
1449  * @cmd: command to delete Flow Director filter
1450  *
1451  * Returns 0 on success and negative values for failure
1452  */
1453 static int iavf_del_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1454 {
1455     struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1456     struct iavf_fdir_fltr *fltr = NULL;
1457     int err = 0;
1458 
1459     if (!FDIR_FLTR_SUPPORT(adapter))
1460         return -EOPNOTSUPP;
1461 
1462     spin_lock_bh(&adapter->fdir_fltr_lock);
1463     fltr = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1464     if (fltr) {
1465         if (fltr->state == IAVF_FDIR_FLTR_ACTIVE) {
1466             fltr->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1467             adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1468         } else {
1469             err = -EBUSY;
1470         }
1471     } else if (adapter->fdir_active_fltr) {
1472         err = -EINVAL;
1473     }
1474     spin_unlock_bh(&adapter->fdir_fltr_lock);
1475 
1476     if (fltr && fltr->state == IAVF_FDIR_FLTR_DEL_REQUEST)
1477         mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1478 
1479     return err;
1480 }
1481 
1482 /**
1483  * iavf_adv_rss_parse_hdrs - parses headers from RSS hash input
1484  * @cmd: ethtool rxnfc command
1485  *
1486  * This function parses the rxnfc command and returns intended
1487  * header types for RSS configuration
1488  */
1489 static u32 iavf_adv_rss_parse_hdrs(struct ethtool_rxnfc *cmd)
1490 {
1491     u32 hdrs = IAVF_ADV_RSS_FLOW_SEG_HDR_NONE;
1492 
1493     switch (cmd->flow_type) {
1494     case TCP_V4_FLOW:
1495         hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1496             IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1497         break;
1498     case UDP_V4_FLOW:
1499         hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1500             IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1501         break;
1502     case SCTP_V4_FLOW:
1503         hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1504             IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1505         break;
1506     case TCP_V6_FLOW:
1507         hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1508             IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1509         break;
1510     case UDP_V6_FLOW:
1511         hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1512             IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1513         break;
1514     case SCTP_V6_FLOW:
1515         hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1516             IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1517         break;
1518     default:
1519         break;
1520     }
1521 
1522     return hdrs;
1523 }
1524 
1525 /**
1526  * iavf_adv_rss_parse_hash_flds - parses hash fields from RSS hash input
1527  * @cmd: ethtool rxnfc command
1528  *
1529  * This function parses the rxnfc command and returns intended hash fields for
1530  * RSS configuration
1531  */
1532 static u64 iavf_adv_rss_parse_hash_flds(struct ethtool_rxnfc *cmd)
1533 {
1534     u64 hfld = IAVF_ADV_RSS_HASH_INVALID;
1535 
1536     if (cmd->data & RXH_IP_SRC || cmd->data & RXH_IP_DST) {
1537         switch (cmd->flow_type) {
1538         case TCP_V4_FLOW:
1539         case UDP_V4_FLOW:
1540         case SCTP_V4_FLOW:
1541             if (cmd->data & RXH_IP_SRC)
1542                 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_SA;
1543             if (cmd->data & RXH_IP_DST)
1544                 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_DA;
1545             break;
1546         case TCP_V6_FLOW:
1547         case UDP_V6_FLOW:
1548         case SCTP_V6_FLOW:
1549             if (cmd->data & RXH_IP_SRC)
1550                 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_SA;
1551             if (cmd->data & RXH_IP_DST)
1552                 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_DA;
1553             break;
1554         default:
1555             break;
1556         }
1557     }
1558 
1559     if (cmd->data & RXH_L4_B_0_1 || cmd->data & RXH_L4_B_2_3) {
1560         switch (cmd->flow_type) {
1561         case TCP_V4_FLOW:
1562         case TCP_V6_FLOW:
1563             if (cmd->data & RXH_L4_B_0_1)
1564                 hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT;
1565             if (cmd->data & RXH_L4_B_2_3)
1566                 hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT;
1567             break;
1568         case UDP_V4_FLOW:
1569         case UDP_V6_FLOW:
1570             if (cmd->data & RXH_L4_B_0_1)
1571                 hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT;
1572             if (cmd->data & RXH_L4_B_2_3)
1573                 hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT;
1574             break;
1575         case SCTP_V4_FLOW:
1576         case SCTP_V6_FLOW:
1577             if (cmd->data & RXH_L4_B_0_1)
1578                 hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT;
1579             if (cmd->data & RXH_L4_B_2_3)
1580                 hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT;
1581             break;
1582         default:
1583             break;
1584         }
1585     }
1586 
1587     return hfld;
1588 }
1589 
1590 /**
1591  * iavf_set_adv_rss_hash_opt - Enable/Disable flow types for RSS hash
1592  * @adapter: pointer to the VF adapter structure
1593  * @cmd: ethtool rxnfc command
1594  *
1595  * Returns Success if the flow input set is supported.
1596  */
1597 static int
1598 iavf_set_adv_rss_hash_opt(struct iavf_adapter *adapter,
1599               struct ethtool_rxnfc *cmd)
1600 {
1601     struct iavf_adv_rss *rss_old, *rss_new;
1602     bool rss_new_add = false;
1603     int count = 50, err = 0;
1604     u64 hash_flds;
1605     u32 hdrs;
1606 
1607     if (!ADV_RSS_SUPPORT(adapter))
1608         return -EOPNOTSUPP;
1609 
1610     hdrs = iavf_adv_rss_parse_hdrs(cmd);
1611     if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1612         return -EINVAL;
1613 
1614     hash_flds = iavf_adv_rss_parse_hash_flds(cmd);
1615     if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1616         return -EINVAL;
1617 
1618     rss_new = kzalloc(sizeof(*rss_new), GFP_KERNEL);
1619     if (!rss_new)
1620         return -ENOMEM;
1621 
1622     if (iavf_fill_adv_rss_cfg_msg(&rss_new->cfg_msg, hdrs, hash_flds)) {
1623         kfree(rss_new);
1624         return -EINVAL;
1625     }
1626 
1627     while (!mutex_trylock(&adapter->crit_lock)) {
1628         if (--count == 0) {
1629             kfree(rss_new);
1630             return -EINVAL;
1631         }
1632 
1633         udelay(1);
1634     }
1635 
1636     spin_lock_bh(&adapter->adv_rss_lock);
1637     rss_old = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1638     if (rss_old) {
1639         if (rss_old->state != IAVF_ADV_RSS_ACTIVE) {
1640             err = -EBUSY;
1641         } else if (rss_old->hash_flds != hash_flds) {
1642             rss_old->state = IAVF_ADV_RSS_ADD_REQUEST;
1643             rss_old->hash_flds = hash_flds;
1644             memcpy(&rss_old->cfg_msg, &rss_new->cfg_msg,
1645                    sizeof(rss_new->cfg_msg));
1646             adapter->aq_required |= IAVF_FLAG_AQ_ADD_ADV_RSS_CFG;
1647         } else {
1648             err = -EEXIST;
1649         }
1650     } else {
1651         rss_new_add = true;
1652         rss_new->state = IAVF_ADV_RSS_ADD_REQUEST;
1653         rss_new->packet_hdrs = hdrs;
1654         rss_new->hash_flds = hash_flds;
1655         list_add_tail(&rss_new->list, &adapter->adv_rss_list_head);
1656         adapter->aq_required |= IAVF_FLAG_AQ_ADD_ADV_RSS_CFG;
1657     }
1658     spin_unlock_bh(&adapter->adv_rss_lock);
1659 
1660     if (!err)
1661         mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1662 
1663     mutex_unlock(&adapter->crit_lock);
1664 
1665     if (!rss_new_add)
1666         kfree(rss_new);
1667 
1668     return err;
1669 }
1670 
1671 /**
1672  * iavf_get_adv_rss_hash_opt - Retrieve hash fields for a given flow-type
1673  * @adapter: pointer to the VF adapter structure
1674  * @cmd: ethtool rxnfc command
1675  *
1676  * Returns Success if the flow input set is supported.
1677  */
1678 static int
1679 iavf_get_adv_rss_hash_opt(struct iavf_adapter *adapter,
1680               struct ethtool_rxnfc *cmd)
1681 {
1682     struct iavf_adv_rss *rss;
1683     u64 hash_flds;
1684     u32 hdrs;
1685 
1686     if (!ADV_RSS_SUPPORT(adapter))
1687         return -EOPNOTSUPP;
1688 
1689     cmd->data = 0;
1690 
1691     hdrs = iavf_adv_rss_parse_hdrs(cmd);
1692     if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1693         return -EINVAL;
1694 
1695     spin_lock_bh(&adapter->adv_rss_lock);
1696     rss = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1697     if (rss)
1698         hash_flds = rss->hash_flds;
1699     else
1700         hash_flds = IAVF_ADV_RSS_HASH_INVALID;
1701     spin_unlock_bh(&adapter->adv_rss_lock);
1702 
1703     if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1704         return -EINVAL;
1705 
1706     if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_SA |
1707              IAVF_ADV_RSS_HASH_FLD_IPV6_SA))
1708         cmd->data |= (u64)RXH_IP_SRC;
1709 
1710     if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_DA |
1711              IAVF_ADV_RSS_HASH_FLD_IPV6_DA))
1712         cmd->data |= (u64)RXH_IP_DST;
1713 
1714     if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT |
1715              IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT |
1716              IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT))
1717         cmd->data |= (u64)RXH_L4_B_0_1;
1718 
1719     if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT |
1720              IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT |
1721              IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT))
1722         cmd->data |= (u64)RXH_L4_B_2_3;
1723 
1724     return 0;
1725 }
1726 
1727 /**
1728  * iavf_set_rxnfc - command to set Rx flow rules.
1729  * @netdev: network interface device structure
1730  * @cmd: ethtool rxnfc command
1731  *
1732  * Returns 0 for success and negative values for errors
1733  */
1734 static int iavf_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1735 {
1736     struct iavf_adapter *adapter = netdev_priv(netdev);
1737     int ret = -EOPNOTSUPP;
1738 
1739     switch (cmd->cmd) {
1740     case ETHTOOL_SRXCLSRLINS:
1741         ret = iavf_add_fdir_ethtool(adapter, cmd);
1742         break;
1743     case ETHTOOL_SRXCLSRLDEL:
1744         ret = iavf_del_fdir_ethtool(adapter, cmd);
1745         break;
1746     case ETHTOOL_SRXFH:
1747         ret = iavf_set_adv_rss_hash_opt(adapter, cmd);
1748         break;
1749     default:
1750         break;
1751     }
1752 
1753     return ret;
1754 }
1755 
1756 /**
1757  * iavf_get_rxnfc - command to get RX flow classification rules
1758  * @netdev: network interface device structure
1759  * @cmd: ethtool rxnfc command
1760  * @rule_locs: pointer to store rule locations
1761  *
1762  * Returns Success if the command is supported.
1763  **/
1764 static int iavf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1765               u32 *rule_locs)
1766 {
1767     struct iavf_adapter *adapter = netdev_priv(netdev);
1768     int ret = -EOPNOTSUPP;
1769 
1770     switch (cmd->cmd) {
1771     case ETHTOOL_GRXRINGS:
1772         cmd->data = adapter->num_active_queues;
1773         ret = 0;
1774         break;
1775     case ETHTOOL_GRXCLSRLCNT:
1776         if (!FDIR_FLTR_SUPPORT(adapter))
1777             break;
1778         cmd->rule_cnt = adapter->fdir_active_fltr;
1779         cmd->data = IAVF_MAX_FDIR_FILTERS;
1780         ret = 0;
1781         break;
1782     case ETHTOOL_GRXCLSRULE:
1783         ret = iavf_get_ethtool_fdir_entry(adapter, cmd);
1784         break;
1785     case ETHTOOL_GRXCLSRLALL:
1786         ret = iavf_get_fdir_fltr_ids(adapter, cmd, (u32 *)rule_locs);
1787         break;
1788     case ETHTOOL_GRXFH:
1789         ret = iavf_get_adv_rss_hash_opt(adapter, cmd);
1790         break;
1791     default:
1792         break;
1793     }
1794 
1795     return ret;
1796 }
1797 /**
1798  * iavf_get_channels: get the number of channels supported by the device
1799  * @netdev: network interface device structure
1800  * @ch: channel information structure
1801  *
1802  * For the purposes of our device, we only use combined channels, i.e. a tx/rx
1803  * queue pair. Report one extra channel to match our "other" MSI-X vector.
1804  **/
1805 static void iavf_get_channels(struct net_device *netdev,
1806                   struct ethtool_channels *ch)
1807 {
1808     struct iavf_adapter *adapter = netdev_priv(netdev);
1809 
1810     /* Report maximum channels */
1811     ch->max_combined = adapter->vsi_res->num_queue_pairs;
1812 
1813     ch->max_other = NONQ_VECS;
1814     ch->other_count = NONQ_VECS;
1815 
1816     ch->combined_count = adapter->num_active_queues;
1817 }
1818 
1819 /**
1820  * iavf_set_channels: set the new channel count
1821  * @netdev: network interface device structure
1822  * @ch: channel information structure
1823  *
1824  * Negotiate a new number of channels with the PF then do a reset.  During
1825  * reset we'll realloc queues and fix the RSS table.  Returns 0 on success,
1826  * negative on failure.
1827  **/
1828 static int iavf_set_channels(struct net_device *netdev,
1829                  struct ethtool_channels *ch)
1830 {
1831     struct iavf_adapter *adapter = netdev_priv(netdev);
1832     u32 num_req = ch->combined_count;
1833     int i;
1834 
1835     if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1836         adapter->num_tc) {
1837         dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
1838         return -EINVAL;
1839     }
1840 
1841     /* All of these should have already been checked by ethtool before this
1842      * even gets to us, but just to be sure.
1843      */
1844     if (num_req == 0 || num_req > adapter->vsi_res->num_queue_pairs)
1845         return -EINVAL;
1846 
1847     if (num_req == adapter->num_active_queues)
1848         return 0;
1849 
1850     if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
1851         return -EINVAL;
1852 
1853     adapter->num_req_queues = num_req;
1854     adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1855     iavf_schedule_reset(adapter);
1856 
1857     /* wait for the reset is done */
1858     for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
1859         msleep(IAVF_RESET_WAIT_MS);
1860         if (adapter->flags & IAVF_FLAG_RESET_PENDING)
1861             continue;
1862         break;
1863     }
1864     if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
1865         adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1866         adapter->num_active_queues = num_req;
1867         return -EOPNOTSUPP;
1868     }
1869 
1870     return 0;
1871 }
1872 
1873 /**
1874  * iavf_get_rxfh_key_size - get the RSS hash key size
1875  * @netdev: network interface device structure
1876  *
1877  * Returns the table size.
1878  **/
1879 static u32 iavf_get_rxfh_key_size(struct net_device *netdev)
1880 {
1881     struct iavf_adapter *adapter = netdev_priv(netdev);
1882 
1883     return adapter->rss_key_size;
1884 }
1885 
1886 /**
1887  * iavf_get_rxfh_indir_size - get the rx flow hash indirection table size
1888  * @netdev: network interface device structure
1889  *
1890  * Returns the table size.
1891  **/
1892 static u32 iavf_get_rxfh_indir_size(struct net_device *netdev)
1893 {
1894     struct iavf_adapter *adapter = netdev_priv(netdev);
1895 
1896     return adapter->rss_lut_size;
1897 }
1898 
1899 /**
1900  * iavf_get_rxfh - get the rx flow hash indirection table
1901  * @netdev: network interface device structure
1902  * @indir: indirection table
1903  * @key: hash key
1904  * @hfunc: hash function in use
1905  *
1906  * Reads the indirection table directly from the hardware. Always returns 0.
1907  **/
1908 static int iavf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1909              u8 *hfunc)
1910 {
1911     struct iavf_adapter *adapter = netdev_priv(netdev);
1912     u16 i;
1913 
1914     if (hfunc)
1915         *hfunc = ETH_RSS_HASH_TOP;
1916     if (key)
1917         memcpy(key, adapter->rss_key, adapter->rss_key_size);
1918 
1919     if (indir)
1920         /* Each 32 bits pointed by 'indir' is stored with a lut entry */
1921         for (i = 0; i < adapter->rss_lut_size; i++)
1922             indir[i] = (u32)adapter->rss_lut[i];
1923 
1924     return 0;
1925 }
1926 
1927 /**
1928  * iavf_set_rxfh - set the rx flow hash indirection table
1929  * @netdev: network interface device structure
1930  * @indir: indirection table
1931  * @key: hash key
1932  * @hfunc: hash function to use
1933  *
1934  * Returns -EINVAL if the table specifies an invalid queue id, otherwise
1935  * returns 0 after programming the table.
1936  **/
1937 static int iavf_set_rxfh(struct net_device *netdev, const u32 *indir,
1938              const u8 *key, const u8 hfunc)
1939 {
1940     struct iavf_adapter *adapter = netdev_priv(netdev);
1941     u16 i;
1942 
1943     /* Only support toeplitz hash function */
1944     if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1945         return -EOPNOTSUPP;
1946 
1947     if (!key && !indir)
1948         return 0;
1949 
1950     if (key)
1951         memcpy(adapter->rss_key, key, adapter->rss_key_size);
1952 
1953     if (indir) {
1954         /* Each 32 bits pointed by 'indir' is stored with a lut entry */
1955         for (i = 0; i < adapter->rss_lut_size; i++)
1956             adapter->rss_lut[i] = (u8)(indir[i]);
1957     }
1958 
1959     return iavf_config_rss(adapter);
1960 }
1961 
1962 static const struct ethtool_ops iavf_ethtool_ops = {
1963     .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1964                      ETHTOOL_COALESCE_USE_ADAPTIVE,
1965     .get_drvinfo        = iavf_get_drvinfo,
1966     .get_link       = ethtool_op_get_link,
1967     .get_ringparam      = iavf_get_ringparam,
1968     .set_ringparam      = iavf_set_ringparam,
1969     .get_strings        = iavf_get_strings,
1970     .get_ethtool_stats  = iavf_get_ethtool_stats,
1971     .get_sset_count     = iavf_get_sset_count,
1972     .get_priv_flags     = iavf_get_priv_flags,
1973     .set_priv_flags     = iavf_set_priv_flags,
1974     .get_msglevel       = iavf_get_msglevel,
1975     .set_msglevel       = iavf_set_msglevel,
1976     .get_coalesce       = iavf_get_coalesce,
1977     .set_coalesce       = iavf_set_coalesce,
1978     .get_per_queue_coalesce = iavf_get_per_queue_coalesce,
1979     .set_per_queue_coalesce = iavf_set_per_queue_coalesce,
1980     .set_rxnfc      = iavf_set_rxnfc,
1981     .get_rxnfc      = iavf_get_rxnfc,
1982     .get_rxfh_indir_size    = iavf_get_rxfh_indir_size,
1983     .get_rxfh       = iavf_get_rxfh,
1984     .set_rxfh       = iavf_set_rxfh,
1985     .get_channels       = iavf_get_channels,
1986     .set_channels       = iavf_set_channels,
1987     .get_rxfh_key_size  = iavf_get_rxfh_key_size,
1988     .get_link_ksettings = iavf_get_link_ksettings,
1989 };
1990 
1991 /**
1992  * iavf_set_ethtool_ops - Initialize ethtool ops struct
1993  * @netdev: network interface device structure
1994  *
1995  * Sets ethtool ops struct in our netdev so that ethtool can call
1996  * our functions.
1997  **/
1998 void iavf_set_ethtool_ops(struct net_device *netdev)
1999 {
2000     netdev->ethtool_ops = &iavf_ethtool_ops;
2001 }