Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /****************************************************************************
0003  * Driver for Solarflare network controllers and boards
0004  * Copyright 2019 Solarflare Communications Inc.
0005  *
0006  * This program is free software; you can redistribute it and/or modify it
0007  * under the terms of the GNU General Public License version 2 as published
0008  * by the Free Software Foundation, incorporated herein by reference.
0009  */
0010 #include <linux/module.h>
0011 #include <linux/netdevice.h>
0012 #include "net_driver.h"
0013 #include "mcdi.h"
0014 #include "nic.h"
0015 #include "selftest.h"
0016 #include "rx_common.h"
0017 #include "ethtool_common.h"
0018 #include "mcdi_port_common.h"
0019 
0020 struct efx_sw_stat_desc {
0021     const char *name;
0022     enum {
0023         EFX_ETHTOOL_STAT_SOURCE_nic,
0024         EFX_ETHTOOL_STAT_SOURCE_channel,
0025         EFX_ETHTOOL_STAT_SOURCE_tx_queue
0026     } source;
0027     unsigned int offset;
0028     u64 (*get_stat)(void *field); /* Reader function */
0029 };
0030 
0031 /* Initialiser for a struct efx_sw_stat_desc with type-checking */
0032 #define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \
0033                 get_stat_function) {            \
0034     .name = #stat_name,                     \
0035     .source = EFX_ETHTOOL_STAT_SOURCE_##source_name,        \
0036     .offset = ((((field_type *) 0) ==               \
0037               &((struct efx_##source_name *)0)->field) ?    \
0038             offsetof(struct efx_##source_name, field) :     \
0039             offsetof(struct efx_##source_name, field)),     \
0040     .get_stat = get_stat_function,                  \
0041 }
0042 
0043 static u64 efx_get_uint_stat(void *field)
0044 {
0045     return *(unsigned int *)field;
0046 }
0047 
0048 static u64 efx_get_atomic_stat(void *field)
0049 {
0050     return atomic_read((atomic_t *) field);
0051 }
0052 
0053 #define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field)        \
0054     EFX_ETHTOOL_STAT(field, nic, field,         \
0055              atomic_t, efx_get_atomic_stat)
0056 
0057 #define EFX_ETHTOOL_UINT_CHANNEL_STAT(field)            \
0058     EFX_ETHTOOL_STAT(field, channel, n_##field,     \
0059              unsigned int, efx_get_uint_stat)
0060 #define EFX_ETHTOOL_UINT_CHANNEL_STAT_NO_N(field)       \
0061     EFX_ETHTOOL_STAT(field, channel, field,         \
0062              unsigned int, efx_get_uint_stat)
0063 
0064 #define EFX_ETHTOOL_UINT_TXQ_STAT(field)            \
0065     EFX_ETHTOOL_STAT(tx_##field, tx_queue, field,       \
0066              unsigned int, efx_get_uint_stat)
0067 
0068 static const struct efx_sw_stat_desc efx_sw_stat_desc[] = {
0069     EFX_ETHTOOL_UINT_TXQ_STAT(merge_events),
0070     EFX_ETHTOOL_UINT_TXQ_STAT(tso_bursts),
0071     EFX_ETHTOOL_UINT_TXQ_STAT(tso_long_headers),
0072     EFX_ETHTOOL_UINT_TXQ_STAT(tso_packets),
0073     EFX_ETHTOOL_UINT_TXQ_STAT(tso_fallbacks),
0074     EFX_ETHTOOL_UINT_TXQ_STAT(pushes),
0075     EFX_ETHTOOL_UINT_TXQ_STAT(pio_packets),
0076     EFX_ETHTOOL_UINT_TXQ_STAT(cb_packets),
0077     EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset),
0078     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc),
0079     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err),
0080     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err),
0081     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_inner_ip_hdr_chksum_err),
0082     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_inner_tcp_udp_chksum_err),
0083     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_outer_ip_hdr_chksum_err),
0084     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_outer_tcp_udp_chksum_err),
0085     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_eth_crc_err),
0086     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mcast_mismatch),
0087     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc),
0088     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_events),
0089     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_packets),
0090     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_drops),
0091     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_bad_drops),
0092     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_tx),
0093     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_redirect),
0094     EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mport_bad),
0095 #ifdef CONFIG_RFS_ACCEL
0096     EFX_ETHTOOL_UINT_CHANNEL_STAT_NO_N(rfs_filter_count),
0097     EFX_ETHTOOL_UINT_CHANNEL_STAT(rfs_succeeded),
0098     EFX_ETHTOOL_UINT_CHANNEL_STAT(rfs_failed),
0099 #endif
0100 };
0101 
0102 #define EFX_ETHTOOL_SW_STAT_COUNT ARRAY_SIZE(efx_sw_stat_desc)
0103 
0104 void efx_ethtool_get_drvinfo(struct net_device *net_dev,
0105                  struct ethtool_drvinfo *info)
0106 {
0107     struct efx_nic *efx = efx_netdev_priv(net_dev);
0108 
0109     strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
0110     efx_mcdi_print_fwver(efx, info->fw_version,
0111                  sizeof(info->fw_version));
0112     strlcpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info));
0113 }
0114 
0115 u32 efx_ethtool_get_msglevel(struct net_device *net_dev)
0116 {
0117     struct efx_nic *efx = efx_netdev_priv(net_dev);
0118 
0119     return efx->msg_enable;
0120 }
0121 
0122 void efx_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable)
0123 {
0124     struct efx_nic *efx = efx_netdev_priv(net_dev);
0125 
0126     efx->msg_enable = msg_enable;
0127 }
0128 
0129 void efx_ethtool_self_test(struct net_device *net_dev,
0130                struct ethtool_test *test, u64 *data)
0131 {
0132     struct efx_nic *efx = efx_netdev_priv(net_dev);
0133     struct efx_self_tests *efx_tests;
0134     bool already_up;
0135     int rc = -ENOMEM;
0136 
0137     efx_tests = kzalloc(sizeof(*efx_tests), GFP_KERNEL);
0138     if (!efx_tests)
0139         goto fail;
0140 
0141     if (!efx_net_active(efx->state)) {
0142         rc = -EBUSY;
0143         goto out;
0144     }
0145 
0146     netif_info(efx, drv, efx->net_dev, "starting %sline testing\n",
0147            (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
0148 
0149     /* We need rx buffers and interrupts. */
0150     already_up = (efx->net_dev->flags & IFF_UP);
0151     if (!already_up) {
0152         rc = dev_open(efx->net_dev, NULL);
0153         if (rc) {
0154             netif_err(efx, drv, efx->net_dev,
0155                   "failed opening device.\n");
0156             goto out;
0157         }
0158     }
0159 
0160     rc = efx_selftest(efx, efx_tests, test->flags);
0161 
0162     if (!already_up)
0163         dev_close(efx->net_dev);
0164 
0165     netif_info(efx, drv, efx->net_dev, "%s %sline self-tests\n",
0166            rc == 0 ? "passed" : "failed",
0167            (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
0168 
0169 out:
0170     efx_ethtool_fill_self_tests(efx, efx_tests, NULL, data);
0171     kfree(efx_tests);
0172 fail:
0173     if (rc)
0174         test->flags |= ETH_TEST_FL_FAILED;
0175 }
0176 
0177 void efx_ethtool_get_pauseparam(struct net_device *net_dev,
0178                 struct ethtool_pauseparam *pause)
0179 {
0180     struct efx_nic *efx = efx_netdev_priv(net_dev);
0181 
0182     pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX);
0183     pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX);
0184     pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO);
0185 }
0186 
0187 int efx_ethtool_set_pauseparam(struct net_device *net_dev,
0188                    struct ethtool_pauseparam *pause)
0189 {
0190     struct efx_nic *efx = efx_netdev_priv(net_dev);
0191     u8 wanted_fc, old_fc;
0192     u32 old_adv;
0193     int rc = 0;
0194 
0195     mutex_lock(&efx->mac_lock);
0196 
0197     wanted_fc = ((pause->rx_pause ? EFX_FC_RX : 0) |
0198              (pause->tx_pause ? EFX_FC_TX : 0) |
0199              (pause->autoneg ? EFX_FC_AUTO : 0));
0200 
0201     if ((wanted_fc & EFX_FC_TX) && !(wanted_fc & EFX_FC_RX)) {
0202         netif_dbg(efx, drv, efx->net_dev,
0203               "Flow control unsupported: tx ON rx OFF\n");
0204         rc = -EINVAL;
0205         goto out;
0206     }
0207 
0208     if ((wanted_fc & EFX_FC_AUTO) && !efx->link_advertising[0]) {
0209         netif_dbg(efx, drv, efx->net_dev,
0210               "Autonegotiation is disabled\n");
0211         rc = -EINVAL;
0212         goto out;
0213     }
0214 
0215     /* Hook for Falcon bug 11482 workaround */
0216     if (efx->type->prepare_enable_fc_tx &&
0217         (wanted_fc & EFX_FC_TX) && !(efx->wanted_fc & EFX_FC_TX))
0218         efx->type->prepare_enable_fc_tx(efx);
0219 
0220     old_adv = efx->link_advertising[0];
0221     old_fc = efx->wanted_fc;
0222     efx_link_set_wanted_fc(efx, wanted_fc);
0223     if (efx->link_advertising[0] != old_adv ||
0224         (efx->wanted_fc ^ old_fc) & EFX_FC_AUTO) {
0225         rc = efx_mcdi_port_reconfigure(efx);
0226         if (rc) {
0227             netif_err(efx, drv, efx->net_dev,
0228                   "Unable to advertise requested flow "
0229                   "control setting\n");
0230             goto out;
0231         }
0232     }
0233 
0234     /* Reconfigure the MAC. The PHY *may* generate a link state change event
0235      * if the user just changed the advertised capabilities, but there's no
0236      * harm doing this twice */
0237     efx_mac_reconfigure(efx, false);
0238 
0239 out:
0240     mutex_unlock(&efx->mac_lock);
0241 
0242     return rc;
0243 }
0244 
0245 /**
0246  * efx_fill_test - fill in an individual self-test entry
0247  * @test_index:     Index of the test
0248  * @strings:        Ethtool strings, or %NULL
0249  * @data:       Ethtool test results, or %NULL
0250  * @test:       Pointer to test result (used only if data != %NULL)
0251  * @unit_format:    Unit name format (e.g. "chan\%d")
0252  * @unit_id:        Unit id (e.g. 0 for "chan0")
0253  * @test_format:    Test name format (e.g. "loopback.\%s.tx.sent")
0254  * @test_id:        Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
0255  *
0256  * Fill in an individual self-test entry.
0257  */
0258 static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
0259               int *test, const char *unit_format, int unit_id,
0260               const char *test_format, const char *test_id)
0261 {
0262     char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
0263 
0264     /* Fill data value, if applicable */
0265     if (data)
0266         data[test_index] = *test;
0267 
0268     /* Fill string, if applicable */
0269     if (strings) {
0270         if (strchr(unit_format, '%'))
0271             snprintf(unit_str, sizeof(unit_str),
0272                  unit_format, unit_id);
0273         else
0274             strcpy(unit_str, unit_format);
0275         snprintf(test_str, sizeof(test_str), test_format, test_id);
0276         snprintf(strings + test_index * ETH_GSTRING_LEN,
0277              ETH_GSTRING_LEN,
0278              "%-6s %-24s", unit_str, test_str);
0279     }
0280 }
0281 
0282 #define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel
0283 #define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->label
0284 #define EFX_LOOPBACK_NAME(_mode, _counter)          \
0285     "loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode)
0286 
0287 /**
0288  * efx_fill_loopback_test - fill in a block of loopback self-test entries
0289  * @efx:        Efx NIC
0290  * @lb_tests:       Efx loopback self-test results structure
0291  * @mode:       Loopback test mode
0292  * @test_index:     Starting index of the test
0293  * @strings:        Ethtool strings, or %NULL
0294  * @data:       Ethtool test results, or %NULL
0295  *
0296  * Fill in a block of loopback self-test entries.  Return new test
0297  * index.
0298  */
0299 static int efx_fill_loopback_test(struct efx_nic *efx,
0300                   struct efx_loopback_self_tests *lb_tests,
0301                   enum efx_loopback_mode mode,
0302                   unsigned int test_index,
0303                   u8 *strings, u64 *data)
0304 {
0305     struct efx_channel *channel =
0306         efx_get_channel(efx, efx->tx_channel_offset);
0307     struct efx_tx_queue *tx_queue;
0308 
0309     efx_for_each_channel_tx_queue(tx_queue, channel) {
0310         efx_fill_test(test_index++, strings, data,
0311                   &lb_tests->tx_sent[tx_queue->label],
0312                   EFX_TX_QUEUE_NAME(tx_queue),
0313                   EFX_LOOPBACK_NAME(mode, "tx_sent"));
0314         efx_fill_test(test_index++, strings, data,
0315                   &lb_tests->tx_done[tx_queue->label],
0316                   EFX_TX_QUEUE_NAME(tx_queue),
0317                   EFX_LOOPBACK_NAME(mode, "tx_done"));
0318     }
0319     efx_fill_test(test_index++, strings, data,
0320               &lb_tests->rx_good,
0321               "rx", 0,
0322               EFX_LOOPBACK_NAME(mode, "rx_good"));
0323     efx_fill_test(test_index++, strings, data,
0324               &lb_tests->rx_bad,
0325               "rx", 0,
0326               EFX_LOOPBACK_NAME(mode, "rx_bad"));
0327 
0328     return test_index;
0329 }
0330 
0331 /**
0332  * efx_ethtool_fill_self_tests - get self-test details
0333  * @efx:        Efx NIC
0334  * @tests:      Efx self-test results structure, or %NULL
0335  * @strings:        Ethtool strings, or %NULL
0336  * @data:       Ethtool test results, or %NULL
0337  *
0338  * Get self-test number of strings, strings, and/or test results.
0339  * Return number of strings (== number of test results).
0340  *
0341  * The reason for merging these three functions is to make sure that
0342  * they can never be inconsistent.
0343  */
0344 int efx_ethtool_fill_self_tests(struct efx_nic *efx,
0345                 struct efx_self_tests *tests,
0346                 u8 *strings, u64 *data)
0347 {
0348     struct efx_channel *channel;
0349     unsigned int n = 0, i;
0350     enum efx_loopback_mode mode;
0351 
0352     efx_fill_test(n++, strings, data, &tests->phy_alive,
0353               "phy", 0, "alive", NULL);
0354     efx_fill_test(n++, strings, data, &tests->nvram,
0355               "core", 0, "nvram", NULL);
0356     efx_fill_test(n++, strings, data, &tests->interrupt,
0357               "core", 0, "interrupt", NULL);
0358 
0359     /* Event queues */
0360     efx_for_each_channel(channel, efx) {
0361         efx_fill_test(n++, strings, data,
0362                   &tests->eventq_dma[channel->channel],
0363                   EFX_CHANNEL_NAME(channel),
0364                   "eventq.dma", NULL);
0365         efx_fill_test(n++, strings, data,
0366                   &tests->eventq_int[channel->channel],
0367                   EFX_CHANNEL_NAME(channel),
0368                   "eventq.int", NULL);
0369     }
0370 
0371     efx_fill_test(n++, strings, data, &tests->memory,
0372               "core", 0, "memory", NULL);
0373     efx_fill_test(n++, strings, data, &tests->registers,
0374               "core", 0, "registers", NULL);
0375 
0376     for (i = 0; true; ++i) {
0377         const char *name;
0378 
0379         EFX_WARN_ON_PARANOID(i >= EFX_MAX_PHY_TESTS);
0380         name = efx_mcdi_phy_test_name(efx, i);
0381         if (name == NULL)
0382             break;
0383 
0384         efx_fill_test(n++, strings, data, &tests->phy_ext[i], "phy", 0, name, NULL);
0385     }
0386 
0387     /* Loopback tests */
0388     for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
0389         if (!(efx->loopback_modes & (1 << mode)))
0390             continue;
0391         n = efx_fill_loopback_test(efx,
0392                        &tests->loopback[mode], mode, n,
0393                        strings, data);
0394     }
0395 
0396     return n;
0397 }
0398 
0399 static size_t efx_describe_per_queue_stats(struct efx_nic *efx, u8 *strings)
0400 {
0401     size_t n_stats = 0;
0402     struct efx_channel *channel;
0403 
0404     efx_for_each_channel(channel, efx) {
0405         if (efx_channel_has_tx_queues(channel)) {
0406             n_stats++;
0407             if (strings != NULL) {
0408                 snprintf(strings, ETH_GSTRING_LEN,
0409                      "tx-%u.tx_packets",
0410                      channel->tx_queue[0].queue /
0411                      EFX_MAX_TXQ_PER_CHANNEL);
0412 
0413                 strings += ETH_GSTRING_LEN;
0414             }
0415         }
0416     }
0417     efx_for_each_channel(channel, efx) {
0418         if (efx_channel_has_rx_queue(channel)) {
0419             n_stats++;
0420             if (strings != NULL) {
0421                 snprintf(strings, ETH_GSTRING_LEN,
0422                      "rx-%d.rx_packets", channel->channel);
0423                 strings += ETH_GSTRING_LEN;
0424             }
0425         }
0426     }
0427     if (efx->xdp_tx_queue_count && efx->xdp_tx_queues) {
0428         unsigned short xdp;
0429 
0430         for (xdp = 0; xdp < efx->xdp_tx_queue_count; xdp++) {
0431             n_stats++;
0432             if (strings) {
0433                 snprintf(strings, ETH_GSTRING_LEN,
0434                      "tx-xdp-cpu-%hu.tx_packets", xdp);
0435                 strings += ETH_GSTRING_LEN;
0436             }
0437         }
0438     }
0439 
0440     return n_stats;
0441 }
0442 
0443 int efx_ethtool_get_sset_count(struct net_device *net_dev, int string_set)
0444 {
0445     struct efx_nic *efx = efx_netdev_priv(net_dev);
0446 
0447     switch (string_set) {
0448     case ETH_SS_STATS:
0449         return efx->type->describe_stats(efx, NULL) +
0450                EFX_ETHTOOL_SW_STAT_COUNT +
0451                efx_describe_per_queue_stats(efx, NULL) +
0452                efx_ptp_describe_stats(efx, NULL);
0453     case ETH_SS_TEST:
0454         return efx_ethtool_fill_self_tests(efx, NULL, NULL, NULL);
0455     default:
0456         return -EINVAL;
0457     }
0458 }
0459 
0460 void efx_ethtool_get_strings(struct net_device *net_dev,
0461                  u32 string_set, u8 *strings)
0462 {
0463     struct efx_nic *efx = efx_netdev_priv(net_dev);
0464     int i;
0465 
0466     switch (string_set) {
0467     case ETH_SS_STATS:
0468         strings += (efx->type->describe_stats(efx, strings) *
0469                 ETH_GSTRING_LEN);
0470         for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++)
0471             strlcpy(strings + i * ETH_GSTRING_LEN,
0472                 efx_sw_stat_desc[i].name, ETH_GSTRING_LEN);
0473         strings += EFX_ETHTOOL_SW_STAT_COUNT * ETH_GSTRING_LEN;
0474         strings += (efx_describe_per_queue_stats(efx, strings) *
0475                 ETH_GSTRING_LEN);
0476         efx_ptp_describe_stats(efx, strings);
0477         break;
0478     case ETH_SS_TEST:
0479         efx_ethtool_fill_self_tests(efx, NULL, strings, NULL);
0480         break;
0481     default:
0482         /* No other string sets */
0483         break;
0484     }
0485 }
0486 
0487 void efx_ethtool_get_stats(struct net_device *net_dev,
0488                struct ethtool_stats *stats,
0489                u64 *data)
0490 {
0491     struct efx_nic *efx = efx_netdev_priv(net_dev);
0492     const struct efx_sw_stat_desc *stat;
0493     struct efx_channel *channel;
0494     struct efx_tx_queue *tx_queue;
0495     struct efx_rx_queue *rx_queue;
0496     int i;
0497 
0498     spin_lock_bh(&efx->stats_lock);
0499 
0500     /* Get NIC statistics */
0501     data += efx->type->update_stats(efx, data, NULL);
0502 
0503     /* Get software statistics */
0504     for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++) {
0505         stat = &efx_sw_stat_desc[i];
0506         switch (stat->source) {
0507         case EFX_ETHTOOL_STAT_SOURCE_nic:
0508             data[i] = stat->get_stat((void *)efx + stat->offset);
0509             break;
0510         case EFX_ETHTOOL_STAT_SOURCE_channel:
0511             data[i] = 0;
0512             efx_for_each_channel(channel, efx)
0513                 data[i] += stat->get_stat((void *)channel +
0514                               stat->offset);
0515             break;
0516         case EFX_ETHTOOL_STAT_SOURCE_tx_queue:
0517             data[i] = 0;
0518             efx_for_each_channel(channel, efx) {
0519                 efx_for_each_channel_tx_queue(tx_queue, channel)
0520                     data[i] +=
0521                         stat->get_stat((void *)tx_queue
0522                                    + stat->offset);
0523             }
0524             break;
0525         }
0526     }
0527     data += EFX_ETHTOOL_SW_STAT_COUNT;
0528 
0529     spin_unlock_bh(&efx->stats_lock);
0530 
0531     efx_for_each_channel(channel, efx) {
0532         if (efx_channel_has_tx_queues(channel)) {
0533             *data = 0;
0534             efx_for_each_channel_tx_queue(tx_queue, channel) {
0535                 *data += tx_queue->tx_packets;
0536             }
0537             data++;
0538         }
0539     }
0540     efx_for_each_channel(channel, efx) {
0541         if (efx_channel_has_rx_queue(channel)) {
0542             *data = 0;
0543             efx_for_each_channel_rx_queue(rx_queue, channel) {
0544                 *data += rx_queue->rx_packets;
0545             }
0546             data++;
0547         }
0548     }
0549     if (efx->xdp_tx_queue_count && efx->xdp_tx_queues) {
0550         int xdp;
0551 
0552         for (xdp = 0; xdp < efx->xdp_tx_queue_count; xdp++) {
0553             data[0] = efx->xdp_tx_queues[xdp]->tx_packets;
0554             data++;
0555         }
0556     }
0557 
0558     efx_ptp_update_stats(efx, data);
0559 }
0560 
0561 /* This must be called with rtnl_lock held. */
0562 int efx_ethtool_get_link_ksettings(struct net_device *net_dev,
0563                    struct ethtool_link_ksettings *cmd)
0564 {
0565     struct efx_nic *efx = efx_netdev_priv(net_dev);
0566     struct efx_link_state *link_state = &efx->link_state;
0567 
0568     mutex_lock(&efx->mac_lock);
0569     efx_mcdi_phy_get_link_ksettings(efx, cmd);
0570     mutex_unlock(&efx->mac_lock);
0571 
0572     /* Both MACs support pause frames (bidirectional and respond-only) */
0573     ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
0574     ethtool_link_ksettings_add_link_mode(cmd, supported, Asym_Pause);
0575 
0576     if (LOOPBACK_INTERNAL(efx)) {
0577         cmd->base.speed = link_state->speed;
0578         cmd->base.duplex = link_state->fd ? DUPLEX_FULL : DUPLEX_HALF;
0579     }
0580 
0581     return 0;
0582 }
0583 
0584 /* This must be called with rtnl_lock held. */
0585 int efx_ethtool_set_link_ksettings(struct net_device *net_dev,
0586                    const struct ethtool_link_ksettings *cmd)
0587 {
0588     struct efx_nic *efx = efx_netdev_priv(net_dev);
0589     int rc;
0590 
0591     /* GMAC does not support 1000Mbps HD */
0592     if ((cmd->base.speed == SPEED_1000) &&
0593         (cmd->base.duplex != DUPLEX_FULL)) {
0594         netif_dbg(efx, drv, efx->net_dev,
0595               "rejecting unsupported 1000Mbps HD setting\n");
0596         return -EINVAL;
0597     }
0598 
0599     mutex_lock(&efx->mac_lock);
0600     rc = efx_mcdi_phy_set_link_ksettings(efx, cmd);
0601     mutex_unlock(&efx->mac_lock);
0602     return rc;
0603 }
0604 
0605 int efx_ethtool_get_fecparam(struct net_device *net_dev,
0606                  struct ethtool_fecparam *fecparam)
0607 {
0608     struct efx_nic *efx = efx_netdev_priv(net_dev);
0609     int rc;
0610 
0611     mutex_lock(&efx->mac_lock);
0612     rc = efx_mcdi_phy_get_fecparam(efx, fecparam);
0613     mutex_unlock(&efx->mac_lock);
0614 
0615     return rc;
0616 }
0617 
0618 int efx_ethtool_set_fecparam(struct net_device *net_dev,
0619                  struct ethtool_fecparam *fecparam)
0620 {
0621     struct efx_nic *efx = efx_netdev_priv(net_dev);
0622     int rc;
0623 
0624     mutex_lock(&efx->mac_lock);
0625     rc = efx_mcdi_phy_set_fecparam(efx, fecparam);
0626     mutex_unlock(&efx->mac_lock);
0627 
0628     return rc;
0629 }
0630 
0631 /* MAC address mask including only I/G bit */
0632 static const u8 mac_addr_ig_mask[ETH_ALEN] __aligned(2) = {0x01, 0, 0, 0, 0, 0};
0633 
0634 #define IP4_ADDR_FULL_MASK  ((__force __be32)~0)
0635 #define IP_PROTO_FULL_MASK  0xFF
0636 #define PORT_FULL_MASK      ((__force __be16)~0)
0637 #define ETHER_TYPE_FULL_MASK    ((__force __be16)~0)
0638 
0639 static inline void ip6_fill_mask(__be32 *mask)
0640 {
0641     mask[0] = mask[1] = mask[2] = mask[3] = ~(__be32)0;
0642 }
0643 
0644 static int efx_ethtool_get_class_rule(struct efx_nic *efx,
0645                       struct ethtool_rx_flow_spec *rule,
0646                       u32 *rss_context)
0647 {
0648     struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
0649     struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
0650     struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
0651     struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
0652     struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
0653     struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
0654     struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
0655     struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
0656     struct ethhdr *mac_entry = &rule->h_u.ether_spec;
0657     struct ethhdr *mac_mask = &rule->m_u.ether_spec;
0658     struct efx_filter_spec spec;
0659     int rc;
0660 
0661     rc = efx_filter_get_filter_safe(efx, EFX_FILTER_PRI_MANUAL,
0662                     rule->location, &spec);
0663     if (rc)
0664         return rc;
0665 
0666     if (spec.dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
0667         rule->ring_cookie = RX_CLS_FLOW_DISC;
0668     else
0669         rule->ring_cookie = spec.dmaq_id;
0670 
0671     if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
0672         spec.ether_type == htons(ETH_P_IP) &&
0673         (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
0674         (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
0675         !(spec.match_flags &
0676           ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
0677         EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
0678         EFX_FILTER_MATCH_IP_PROTO |
0679         EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
0680         rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
0681                    TCP_V4_FLOW : UDP_V4_FLOW);
0682         if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
0683             ip_entry->ip4dst = spec.loc_host[0];
0684             ip_mask->ip4dst = IP4_ADDR_FULL_MASK;
0685         }
0686         if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
0687             ip_entry->ip4src = spec.rem_host[0];
0688             ip_mask->ip4src = IP4_ADDR_FULL_MASK;
0689         }
0690         if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
0691             ip_entry->pdst = spec.loc_port;
0692             ip_mask->pdst = PORT_FULL_MASK;
0693         }
0694         if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
0695             ip_entry->psrc = spec.rem_port;
0696             ip_mask->psrc = PORT_FULL_MASK;
0697         }
0698     } else if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
0699         spec.ether_type == htons(ETH_P_IPV6) &&
0700         (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
0701         (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
0702         !(spec.match_flags &
0703           ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
0704         EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
0705         EFX_FILTER_MATCH_IP_PROTO |
0706         EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
0707         rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
0708                    TCP_V6_FLOW : UDP_V6_FLOW);
0709         if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
0710             memcpy(ip6_entry->ip6dst, spec.loc_host,
0711                    sizeof(ip6_entry->ip6dst));
0712             ip6_fill_mask(ip6_mask->ip6dst);
0713         }
0714         if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
0715             memcpy(ip6_entry->ip6src, spec.rem_host,
0716                    sizeof(ip6_entry->ip6src));
0717             ip6_fill_mask(ip6_mask->ip6src);
0718         }
0719         if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
0720             ip6_entry->pdst = spec.loc_port;
0721             ip6_mask->pdst = PORT_FULL_MASK;
0722         }
0723         if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
0724             ip6_entry->psrc = spec.rem_port;
0725             ip6_mask->psrc = PORT_FULL_MASK;
0726         }
0727     } else if (!(spec.match_flags &
0728              ~(EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG |
0729                EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_ETHER_TYPE |
0730                EFX_FILTER_MATCH_OUTER_VID))) {
0731         rule->flow_type = ETHER_FLOW;
0732         if (spec.match_flags &
0733             (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG)) {
0734             ether_addr_copy(mac_entry->h_dest, spec.loc_mac);
0735             if (spec.match_flags & EFX_FILTER_MATCH_LOC_MAC)
0736                 eth_broadcast_addr(mac_mask->h_dest);
0737             else
0738                 ether_addr_copy(mac_mask->h_dest,
0739                         mac_addr_ig_mask);
0740         }
0741         if (spec.match_flags & EFX_FILTER_MATCH_REM_MAC) {
0742             ether_addr_copy(mac_entry->h_source, spec.rem_mac);
0743             eth_broadcast_addr(mac_mask->h_source);
0744         }
0745         if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) {
0746             mac_entry->h_proto = spec.ether_type;
0747             mac_mask->h_proto = ETHER_TYPE_FULL_MASK;
0748         }
0749     } else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
0750            spec.ether_type == htons(ETH_P_IP) &&
0751            !(spec.match_flags &
0752              ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
0753                EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
0754                EFX_FILTER_MATCH_IP_PROTO))) {
0755         rule->flow_type = IPV4_USER_FLOW;
0756         uip_entry->ip_ver = ETH_RX_NFC_IP4;
0757         if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
0758             uip_mask->proto = IP_PROTO_FULL_MASK;
0759             uip_entry->proto = spec.ip_proto;
0760         }
0761         if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
0762             uip_entry->ip4dst = spec.loc_host[0];
0763             uip_mask->ip4dst = IP4_ADDR_FULL_MASK;
0764         }
0765         if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
0766             uip_entry->ip4src = spec.rem_host[0];
0767             uip_mask->ip4src = IP4_ADDR_FULL_MASK;
0768         }
0769     } else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
0770            spec.ether_type == htons(ETH_P_IPV6) &&
0771            !(spec.match_flags &
0772              ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
0773                EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
0774                EFX_FILTER_MATCH_IP_PROTO))) {
0775         rule->flow_type = IPV6_USER_FLOW;
0776         if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
0777             uip6_mask->l4_proto = IP_PROTO_FULL_MASK;
0778             uip6_entry->l4_proto = spec.ip_proto;
0779         }
0780         if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
0781             memcpy(uip6_entry->ip6dst, spec.loc_host,
0782                    sizeof(uip6_entry->ip6dst));
0783             ip6_fill_mask(uip6_mask->ip6dst);
0784         }
0785         if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
0786             memcpy(uip6_entry->ip6src, spec.rem_host,
0787                    sizeof(uip6_entry->ip6src));
0788             ip6_fill_mask(uip6_mask->ip6src);
0789         }
0790     } else {
0791         /* The above should handle all filters that we insert */
0792         WARN_ON(1);
0793         return -EINVAL;
0794     }
0795 
0796     if (spec.match_flags & EFX_FILTER_MATCH_OUTER_VID) {
0797         rule->flow_type |= FLOW_EXT;
0798         rule->h_ext.vlan_tci = spec.outer_vid;
0799         rule->m_ext.vlan_tci = htons(0xfff);
0800     }
0801 
0802     if (spec.flags & EFX_FILTER_FLAG_RX_RSS) {
0803         rule->flow_type |= FLOW_RSS;
0804         *rss_context = spec.rss_context;
0805     }
0806 
0807     return rc;
0808 }
0809 
0810 int efx_ethtool_get_rxnfc(struct net_device *net_dev,
0811               struct ethtool_rxnfc *info, u32 *rule_locs)
0812 {
0813     struct efx_nic *efx = efx_netdev_priv(net_dev);
0814     u32 rss_context = 0;
0815     s32 rc = 0;
0816 
0817     switch (info->cmd) {
0818     case ETHTOOL_GRXRINGS:
0819         info->data = efx->n_rx_channels;
0820         return 0;
0821 
0822     case ETHTOOL_GRXFH: {
0823         struct efx_rss_context *ctx = &efx->rss_context;
0824         __u64 data;
0825 
0826         mutex_lock(&efx->rss_lock);
0827         if (info->flow_type & FLOW_RSS && info->rss_context) {
0828             ctx = efx_find_rss_context_entry(efx, info->rss_context);
0829             if (!ctx) {
0830                 rc = -ENOENT;
0831                 goto out_unlock;
0832             }
0833         }
0834 
0835         data = 0;
0836         if (!efx_rss_active(ctx)) /* No RSS */
0837             goto out_setdata_unlock;
0838 
0839         switch (info->flow_type & ~FLOW_RSS) {
0840         case UDP_V4_FLOW:
0841         case UDP_V6_FLOW:
0842             if (ctx->rx_hash_udp_4tuple)
0843                 data = (RXH_L4_B_0_1 | RXH_L4_B_2_3 |
0844                     RXH_IP_SRC | RXH_IP_DST);
0845             else
0846                 data = RXH_IP_SRC | RXH_IP_DST;
0847             break;
0848         case TCP_V4_FLOW:
0849         case TCP_V6_FLOW:
0850             data = (RXH_L4_B_0_1 | RXH_L4_B_2_3 |
0851                 RXH_IP_SRC | RXH_IP_DST);
0852             break;
0853         case SCTP_V4_FLOW:
0854         case SCTP_V6_FLOW:
0855         case AH_ESP_V4_FLOW:
0856         case AH_ESP_V6_FLOW:
0857         case IPV4_FLOW:
0858         case IPV6_FLOW:
0859             data = RXH_IP_SRC | RXH_IP_DST;
0860             break;
0861         default:
0862             break;
0863         }
0864 out_setdata_unlock:
0865         info->data = data;
0866 out_unlock:
0867         mutex_unlock(&efx->rss_lock);
0868         return rc;
0869     }
0870 
0871     case ETHTOOL_GRXCLSRLCNT:
0872         info->data = efx_filter_get_rx_id_limit(efx);
0873         if (info->data == 0)
0874             return -EOPNOTSUPP;
0875         info->data |= RX_CLS_LOC_SPECIAL;
0876         info->rule_cnt =
0877             efx_filter_count_rx_used(efx, EFX_FILTER_PRI_MANUAL);
0878         return 0;
0879 
0880     case ETHTOOL_GRXCLSRULE:
0881         if (efx_filter_get_rx_id_limit(efx) == 0)
0882             return -EOPNOTSUPP;
0883         rc = efx_ethtool_get_class_rule(efx, &info->fs, &rss_context);
0884         if (rc < 0)
0885             return rc;
0886         if (info->fs.flow_type & FLOW_RSS)
0887             info->rss_context = rss_context;
0888         return 0;
0889 
0890     case ETHTOOL_GRXCLSRLALL:
0891         info->data = efx_filter_get_rx_id_limit(efx);
0892         if (info->data == 0)
0893             return -EOPNOTSUPP;
0894         rc = efx_filter_get_rx_ids(efx, EFX_FILTER_PRI_MANUAL,
0895                        rule_locs, info->rule_cnt);
0896         if (rc < 0)
0897             return rc;
0898         info->rule_cnt = rc;
0899         return 0;
0900 
0901     default:
0902         return -EOPNOTSUPP;
0903     }
0904 }
0905 
0906 static inline bool ip6_mask_is_full(__be32 mask[4])
0907 {
0908     return !~(mask[0] & mask[1] & mask[2] & mask[3]);
0909 }
0910 
0911 static inline bool ip6_mask_is_empty(__be32 mask[4])
0912 {
0913     return !(mask[0] | mask[1] | mask[2] | mask[3]);
0914 }
0915 
0916 static int efx_ethtool_set_class_rule(struct efx_nic *efx,
0917                       struct ethtool_rx_flow_spec *rule,
0918                       u32 rss_context)
0919 {
0920     struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
0921     struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
0922     struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
0923     struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
0924     struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
0925     struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
0926     struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
0927     struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
0928     u32 flow_type = rule->flow_type & ~(FLOW_EXT | FLOW_RSS);
0929     struct ethhdr *mac_entry = &rule->h_u.ether_spec;
0930     struct ethhdr *mac_mask = &rule->m_u.ether_spec;
0931     enum efx_filter_flags flags = 0;
0932     struct efx_filter_spec spec;
0933     int rc;
0934 
0935     /* Check that user wants us to choose the location */
0936     if (rule->location != RX_CLS_LOC_ANY)
0937         return -EINVAL;
0938 
0939     /* Range-check ring_cookie */
0940     if (rule->ring_cookie >= efx->n_rx_channels &&
0941         rule->ring_cookie != RX_CLS_FLOW_DISC)
0942         return -EINVAL;
0943 
0944     /* Check for unsupported extensions */
0945     if ((rule->flow_type & FLOW_EXT) &&
0946         (rule->m_ext.vlan_etype || rule->m_ext.data[0] ||
0947          rule->m_ext.data[1]))
0948         return -EINVAL;
0949 
0950     if (efx->rx_scatter)
0951         flags |= EFX_FILTER_FLAG_RX_SCATTER;
0952     if (rule->flow_type & FLOW_RSS)
0953         flags |= EFX_FILTER_FLAG_RX_RSS;
0954 
0955     efx_filter_init_rx(&spec, EFX_FILTER_PRI_MANUAL, flags,
0956                (rule->ring_cookie == RX_CLS_FLOW_DISC) ?
0957                EFX_FILTER_RX_DMAQ_ID_DROP : rule->ring_cookie);
0958 
0959     if (rule->flow_type & FLOW_RSS)
0960         spec.rss_context = rss_context;
0961 
0962     switch (flow_type) {
0963     case TCP_V4_FLOW:
0964     case UDP_V4_FLOW:
0965         spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
0966                     EFX_FILTER_MATCH_IP_PROTO);
0967         spec.ether_type = htons(ETH_P_IP);
0968         spec.ip_proto = flow_type == TCP_V4_FLOW ? IPPROTO_TCP
0969                              : IPPROTO_UDP;
0970         if (ip_mask->ip4dst) {
0971             if (ip_mask->ip4dst != IP4_ADDR_FULL_MASK)
0972                 return -EINVAL;
0973             spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
0974             spec.loc_host[0] = ip_entry->ip4dst;
0975         }
0976         if (ip_mask->ip4src) {
0977             if (ip_mask->ip4src != IP4_ADDR_FULL_MASK)
0978                 return -EINVAL;
0979             spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
0980             spec.rem_host[0] = ip_entry->ip4src;
0981         }
0982         if (ip_mask->pdst) {
0983             if (ip_mask->pdst != PORT_FULL_MASK)
0984                 return -EINVAL;
0985             spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
0986             spec.loc_port = ip_entry->pdst;
0987         }
0988         if (ip_mask->psrc) {
0989             if (ip_mask->psrc != PORT_FULL_MASK)
0990                 return -EINVAL;
0991             spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
0992             spec.rem_port = ip_entry->psrc;
0993         }
0994         if (ip_mask->tos)
0995             return -EINVAL;
0996         break;
0997 
0998     case TCP_V6_FLOW:
0999     case UDP_V6_FLOW:
1000         spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
1001                     EFX_FILTER_MATCH_IP_PROTO);
1002         spec.ether_type = htons(ETH_P_IPV6);
1003         spec.ip_proto = flow_type == TCP_V6_FLOW ? IPPROTO_TCP
1004                              : IPPROTO_UDP;
1005         if (!ip6_mask_is_empty(ip6_mask->ip6dst)) {
1006             if (!ip6_mask_is_full(ip6_mask->ip6dst))
1007                 return -EINVAL;
1008             spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1009             memcpy(spec.loc_host, ip6_entry->ip6dst, sizeof(spec.loc_host));
1010         }
1011         if (!ip6_mask_is_empty(ip6_mask->ip6src)) {
1012             if (!ip6_mask_is_full(ip6_mask->ip6src))
1013                 return -EINVAL;
1014             spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1015             memcpy(spec.rem_host, ip6_entry->ip6src, sizeof(spec.rem_host));
1016         }
1017         if (ip6_mask->pdst) {
1018             if (ip6_mask->pdst != PORT_FULL_MASK)
1019                 return -EINVAL;
1020             spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
1021             spec.loc_port = ip6_entry->pdst;
1022         }
1023         if (ip6_mask->psrc) {
1024             if (ip6_mask->psrc != PORT_FULL_MASK)
1025                 return -EINVAL;
1026             spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
1027             spec.rem_port = ip6_entry->psrc;
1028         }
1029         if (ip6_mask->tclass)
1030             return -EINVAL;
1031         break;
1032 
1033     case IPV4_USER_FLOW:
1034         if (uip_mask->l4_4_bytes || uip_mask->tos || uip_mask->ip_ver ||
1035             uip_entry->ip_ver != ETH_RX_NFC_IP4)
1036             return -EINVAL;
1037         spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1038         spec.ether_type = htons(ETH_P_IP);
1039         if (uip_mask->ip4dst) {
1040             if (uip_mask->ip4dst != IP4_ADDR_FULL_MASK)
1041                 return -EINVAL;
1042             spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1043             spec.loc_host[0] = uip_entry->ip4dst;
1044         }
1045         if (uip_mask->ip4src) {
1046             if (uip_mask->ip4src != IP4_ADDR_FULL_MASK)
1047                 return -EINVAL;
1048             spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1049             spec.rem_host[0] = uip_entry->ip4src;
1050         }
1051         if (uip_mask->proto) {
1052             if (uip_mask->proto != IP_PROTO_FULL_MASK)
1053                 return -EINVAL;
1054             spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1055             spec.ip_proto = uip_entry->proto;
1056         }
1057         break;
1058 
1059     case IPV6_USER_FLOW:
1060         if (uip6_mask->l4_4_bytes || uip6_mask->tclass)
1061             return -EINVAL;
1062         spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1063         spec.ether_type = htons(ETH_P_IPV6);
1064         if (!ip6_mask_is_empty(uip6_mask->ip6dst)) {
1065             if (!ip6_mask_is_full(uip6_mask->ip6dst))
1066                 return -EINVAL;
1067             spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1068             memcpy(spec.loc_host, uip6_entry->ip6dst, sizeof(spec.loc_host));
1069         }
1070         if (!ip6_mask_is_empty(uip6_mask->ip6src)) {
1071             if (!ip6_mask_is_full(uip6_mask->ip6src))
1072                 return -EINVAL;
1073             spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1074             memcpy(spec.rem_host, uip6_entry->ip6src, sizeof(spec.rem_host));
1075         }
1076         if (uip6_mask->l4_proto) {
1077             if (uip6_mask->l4_proto != IP_PROTO_FULL_MASK)
1078                 return -EINVAL;
1079             spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1080             spec.ip_proto = uip6_entry->l4_proto;
1081         }
1082         break;
1083 
1084     case ETHER_FLOW:
1085         if (!is_zero_ether_addr(mac_mask->h_dest)) {
1086             if (ether_addr_equal(mac_mask->h_dest,
1087                          mac_addr_ig_mask))
1088                 spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
1089             else if (is_broadcast_ether_addr(mac_mask->h_dest))
1090                 spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC;
1091             else
1092                 return -EINVAL;
1093             ether_addr_copy(spec.loc_mac, mac_entry->h_dest);
1094         }
1095         if (!is_zero_ether_addr(mac_mask->h_source)) {
1096             if (!is_broadcast_ether_addr(mac_mask->h_source))
1097                 return -EINVAL;
1098             spec.match_flags |= EFX_FILTER_MATCH_REM_MAC;
1099             ether_addr_copy(spec.rem_mac, mac_entry->h_source);
1100         }
1101         if (mac_mask->h_proto) {
1102             if (mac_mask->h_proto != ETHER_TYPE_FULL_MASK)
1103                 return -EINVAL;
1104             spec.match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
1105             spec.ether_type = mac_entry->h_proto;
1106         }
1107         break;
1108 
1109     default:
1110         return -EINVAL;
1111     }
1112 
1113     if ((rule->flow_type & FLOW_EXT) && rule->m_ext.vlan_tci) {
1114         if (rule->m_ext.vlan_tci != htons(0xfff))
1115             return -EINVAL;
1116         spec.match_flags |= EFX_FILTER_MATCH_OUTER_VID;
1117         spec.outer_vid = rule->h_ext.vlan_tci;
1118     }
1119 
1120     rc = efx_filter_insert_filter(efx, &spec, true);
1121     if (rc < 0)
1122         return rc;
1123 
1124     rule->location = rc;
1125     return 0;
1126 }
1127 
1128 int efx_ethtool_set_rxnfc(struct net_device *net_dev,
1129               struct ethtool_rxnfc *info)
1130 {
1131     struct efx_nic *efx = efx_netdev_priv(net_dev);
1132 
1133     if (efx_filter_get_rx_id_limit(efx) == 0)
1134         return -EOPNOTSUPP;
1135 
1136     switch (info->cmd) {
1137     case ETHTOOL_SRXCLSRLINS:
1138         return efx_ethtool_set_class_rule(efx, &info->fs,
1139                           info->rss_context);
1140 
1141     case ETHTOOL_SRXCLSRLDEL:
1142         return efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_MANUAL,
1143                          info->fs.location);
1144 
1145     default:
1146         return -EOPNOTSUPP;
1147     }
1148 }
1149 
1150 u32 efx_ethtool_get_rxfh_indir_size(struct net_device *net_dev)
1151 {
1152     struct efx_nic *efx = efx_netdev_priv(net_dev);
1153 
1154     if (efx->n_rx_channels == 1)
1155         return 0;
1156     return ARRAY_SIZE(efx->rss_context.rx_indir_table);
1157 }
1158 
1159 u32 efx_ethtool_get_rxfh_key_size(struct net_device *net_dev)
1160 {
1161     struct efx_nic *efx = efx_netdev_priv(net_dev);
1162 
1163     return efx->type->rx_hash_key_size;
1164 }
1165 
1166 int efx_ethtool_get_rxfh(struct net_device *net_dev, u32 *indir, u8 *key,
1167              u8 *hfunc)
1168 {
1169     struct efx_nic *efx = efx_netdev_priv(net_dev);
1170     int rc;
1171 
1172     rc = efx->type->rx_pull_rss_config(efx);
1173     if (rc)
1174         return rc;
1175 
1176     if (hfunc)
1177         *hfunc = ETH_RSS_HASH_TOP;
1178     if (indir)
1179         memcpy(indir, efx->rss_context.rx_indir_table,
1180                sizeof(efx->rss_context.rx_indir_table));
1181     if (key)
1182         memcpy(key, efx->rss_context.rx_hash_key,
1183                efx->type->rx_hash_key_size);
1184     return 0;
1185 }
1186 
1187 int efx_ethtool_set_rxfh(struct net_device *net_dev, const u32 *indir,
1188              const u8 *key, const u8 hfunc)
1189 {
1190     struct efx_nic *efx = efx_netdev_priv(net_dev);
1191 
1192     /* Hash function is Toeplitz, cannot be changed */
1193     if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1194         return -EOPNOTSUPP;
1195     if (!indir && !key)
1196         return 0;
1197 
1198     if (!key)
1199         key = efx->rss_context.rx_hash_key;
1200     if (!indir)
1201         indir = efx->rss_context.rx_indir_table;
1202 
1203     return efx->type->rx_push_rss_config(efx, true, indir, key);
1204 }
1205 
1206 int efx_ethtool_get_rxfh_context(struct net_device *net_dev, u32 *indir,
1207                  u8 *key, u8 *hfunc, u32 rss_context)
1208 {
1209     struct efx_nic *efx = efx_netdev_priv(net_dev);
1210     struct efx_rss_context *ctx;
1211     int rc = 0;
1212 
1213     if (!efx->type->rx_pull_rss_context_config)
1214         return -EOPNOTSUPP;
1215 
1216     mutex_lock(&efx->rss_lock);
1217     ctx = efx_find_rss_context_entry(efx, rss_context);
1218     if (!ctx) {
1219         rc = -ENOENT;
1220         goto out_unlock;
1221     }
1222     rc = efx->type->rx_pull_rss_context_config(efx, ctx);
1223     if (rc)
1224         goto out_unlock;
1225 
1226     if (hfunc)
1227         *hfunc = ETH_RSS_HASH_TOP;
1228     if (indir)
1229         memcpy(indir, ctx->rx_indir_table, sizeof(ctx->rx_indir_table));
1230     if (key)
1231         memcpy(key, ctx->rx_hash_key, efx->type->rx_hash_key_size);
1232 out_unlock:
1233     mutex_unlock(&efx->rss_lock);
1234     return rc;
1235 }
1236 
1237 int efx_ethtool_set_rxfh_context(struct net_device *net_dev,
1238                  const u32 *indir, const u8 *key,
1239                  const u8 hfunc, u32 *rss_context,
1240                  bool delete)
1241 {
1242     struct efx_nic *efx = efx_netdev_priv(net_dev);
1243     struct efx_rss_context *ctx;
1244     bool allocated = false;
1245     int rc;
1246 
1247     if (!efx->type->rx_push_rss_context_config)
1248         return -EOPNOTSUPP;
1249     /* Hash function is Toeplitz, cannot be changed */
1250     if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1251         return -EOPNOTSUPP;
1252 
1253     mutex_lock(&efx->rss_lock);
1254 
1255     if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
1256         if (delete) {
1257             /* alloc + delete == Nothing to do */
1258             rc = -EINVAL;
1259             goto out_unlock;
1260         }
1261         ctx = efx_alloc_rss_context_entry(efx);
1262         if (!ctx) {
1263             rc = -ENOMEM;
1264             goto out_unlock;
1265         }
1266         ctx->context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
1267         /* Initialise indir table and key to defaults */
1268         efx_set_default_rx_indir_table(efx, ctx);
1269         netdev_rss_key_fill(ctx->rx_hash_key, sizeof(ctx->rx_hash_key));
1270         allocated = true;
1271     } else {
1272         ctx = efx_find_rss_context_entry(efx, *rss_context);
1273         if (!ctx) {
1274             rc = -ENOENT;
1275             goto out_unlock;
1276         }
1277     }
1278 
1279     if (delete) {
1280         /* delete this context */
1281         rc = efx->type->rx_push_rss_context_config(efx, ctx, NULL, NULL);
1282         if (!rc)
1283             efx_free_rss_context_entry(ctx);
1284         goto out_unlock;
1285     }
1286 
1287     if (!key)
1288         key = ctx->rx_hash_key;
1289     if (!indir)
1290         indir = ctx->rx_indir_table;
1291 
1292     rc = efx->type->rx_push_rss_context_config(efx, ctx, indir, key);
1293     if (rc && allocated)
1294         efx_free_rss_context_entry(ctx);
1295     else
1296         *rss_context = ctx->user_id;
1297 out_unlock:
1298     mutex_unlock(&efx->rss_lock);
1299     return rc;
1300 }
1301 
1302 int efx_ethtool_reset(struct net_device *net_dev, u32 *flags)
1303 {
1304     struct efx_nic *efx = efx_netdev_priv(net_dev);
1305     int rc;
1306 
1307     rc = efx->type->map_reset_flags(flags);
1308     if (rc < 0)
1309         return rc;
1310 
1311     return efx_reset(efx, rc);
1312 }
1313 
1314 int efx_ethtool_get_module_eeprom(struct net_device *net_dev,
1315                   struct ethtool_eeprom *ee,
1316                   u8 *data)
1317 {
1318     struct efx_nic *efx = efx_netdev_priv(net_dev);
1319     int ret;
1320 
1321     mutex_lock(&efx->mac_lock);
1322     ret = efx_mcdi_phy_get_module_eeprom(efx, ee, data);
1323     mutex_unlock(&efx->mac_lock);
1324 
1325     return ret;
1326 }
1327 
1328 int efx_ethtool_get_module_info(struct net_device *net_dev,
1329                 struct ethtool_modinfo *modinfo)
1330 {
1331     struct efx_nic *efx = efx_netdev_priv(net_dev);
1332     int ret;
1333 
1334     mutex_lock(&efx->mac_lock);
1335     ret = efx_mcdi_phy_get_module_info(efx, modinfo);
1336     mutex_unlock(&efx->mac_lock);
1337 
1338     return ret;
1339 }