linux/drivers/net/ethernet/intel/ixgbevf/ethtool.c
<<
>>
Prefs
   1/*******************************************************************************
   2
   3  Intel 82599 Virtual Function driver
   4  Copyright(c) 1999 - 2018 Intel Corporation.
   5
   6  This program is free software; you can redistribute it and/or modify it
   7  under the terms and conditions of the GNU General Public License,
   8  version 2, as published by the Free Software Foundation.
   9
  10  This program is distributed in the hope it will be useful, but WITHOUT
  11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13  more details.
  14
  15  You should have received a copy of the GNU General Public License along with
  16  this program; if not, see <http://www.gnu.org/licenses/>.
  17
  18  The full GNU General Public License is included in this distribution in
  19  the file called "COPYING".
  20
  21  Contact Information:
  22  e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24
  25*******************************************************************************/
  26
  27/* ethtool support for ixgbevf */
  28
  29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30
  31#include <linux/types.h>
  32#include <linux/module.h>
  33#include <linux/slab.h>
  34#include <linux/pci.h>
  35#include <linux/netdevice.h>
  36#include <linux/ethtool.h>
  37#include <linux/vmalloc.h>
  38#include <linux/if_vlan.h>
  39#include <linux/uaccess.h>
  40
  41#include "ixgbevf.h"
  42
  43#define IXGBE_ALL_RAR_ENTRIES 16
  44
  45enum {NETDEV_STATS, IXGBEVF_STATS};
  46
  47struct ixgbe_stats {
  48        char stat_string[ETH_GSTRING_LEN];
  49        int type;
  50        int sizeof_stat;
  51        int stat_offset;
  52};
  53
  54#define IXGBEVF_STAT(_name, _stat) { \
  55        .stat_string = _name, \
  56        .type = IXGBEVF_STATS, \
  57        .sizeof_stat = FIELD_SIZEOF(struct ixgbevf_adapter, _stat), \
  58        .stat_offset = offsetof(struct ixgbevf_adapter, _stat) \
  59}
  60
  61#define IXGBEVF_NETDEV_STAT(_net_stat) { \
  62        .stat_string = #_net_stat, \
  63        .type = NETDEV_STATS, \
  64        .sizeof_stat = FIELD_SIZEOF(struct net_device_stats, _net_stat), \
  65        .stat_offset = offsetof(struct net_device_stats, _net_stat) \
  66}
  67
  68static struct ixgbe_stats ixgbevf_gstrings_stats[] = {
  69        IXGBEVF_NETDEV_STAT(rx_packets),
  70        IXGBEVF_NETDEV_STAT(tx_packets),
  71        IXGBEVF_NETDEV_STAT(rx_bytes),
  72        IXGBEVF_NETDEV_STAT(tx_bytes),
  73        IXGBEVF_STAT("tx_busy", tx_busy),
  74        IXGBEVF_STAT("tx_restart_queue", restart_queue),
  75        IXGBEVF_STAT("tx_timeout_count", tx_timeout_count),
  76        IXGBEVF_NETDEV_STAT(multicast),
  77        IXGBEVF_STAT("rx_csum_offload_errors", hw_csum_rx_error),
  78        IXGBEVF_STAT("alloc_rx_page", alloc_rx_page),
  79        IXGBEVF_STAT("alloc_rx_page_failed", alloc_rx_page_failed),
  80        IXGBEVF_STAT("alloc_rx_buff_failed", alloc_rx_buff_failed),
  81        IXGBEVF_STAT("tx_ipsec", tx_ipsec),
  82        IXGBEVF_STAT("rx_ipsec", rx_ipsec),
  83};
  84
  85#define IXGBEVF_QUEUE_STATS_LEN ( \
  86        (((struct ixgbevf_adapter *)netdev_priv(netdev))->num_tx_queues + \
  87         ((struct ixgbevf_adapter *)netdev_priv(netdev))->num_xdp_queues + \
  88         ((struct ixgbevf_adapter *)netdev_priv(netdev))->num_rx_queues) * \
  89         (sizeof(struct ixgbevf_stats) / sizeof(u64)))
  90#define IXGBEVF_GLOBAL_STATS_LEN ARRAY_SIZE(ixgbevf_gstrings_stats)
  91
  92#define IXGBEVF_STATS_LEN (IXGBEVF_GLOBAL_STATS_LEN + IXGBEVF_QUEUE_STATS_LEN)
  93static const char ixgbe_gstrings_test[][ETH_GSTRING_LEN] = {
  94        "Register test  (offline)",
  95        "Link test   (on/offline)"
  96};
  97
  98#define IXGBEVF_TEST_LEN (sizeof(ixgbe_gstrings_test) / ETH_GSTRING_LEN)
  99
 100static const char ixgbevf_priv_flags_strings[][ETH_GSTRING_LEN] = {
 101#define IXGBEVF_PRIV_FLAGS_LEGACY_RX    BIT(0)
 102        "legacy-rx",
 103};
 104
 105#define IXGBEVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(ixgbevf_priv_flags_strings)
 106
 107static int ixgbevf_get_link_ksettings(struct net_device *netdev,
 108                                      struct ethtool_link_ksettings *cmd)
 109{
 110        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 111        struct ixgbe_hw *hw = &adapter->hw;
 112        u32 link_speed = 0;
 113        bool link_up;
 114
 115        ethtool_link_ksettings_zero_link_mode(cmd, supported);
 116        ethtool_link_ksettings_add_link_mode(cmd, supported, 10000baseT_Full);
 117        cmd->base.autoneg = AUTONEG_DISABLE;
 118        cmd->base.port = -1;
 119
 120        hw->mac.get_link_status = 1;
 121        hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
 122
 123        if (link_up) {
 124                __u32 speed = SPEED_10000;
 125
 126                switch (link_speed) {
 127                case IXGBE_LINK_SPEED_10GB_FULL:
 128                        speed = SPEED_10000;
 129                        break;
 130                case IXGBE_LINK_SPEED_1GB_FULL:
 131                        speed = SPEED_1000;
 132                        break;
 133                case IXGBE_LINK_SPEED_100_FULL:
 134                        speed = SPEED_100;
 135                        break;
 136                }
 137
 138                cmd->base.speed = speed;
 139                cmd->base.duplex = DUPLEX_FULL;
 140        } else {
 141                cmd->base.speed = SPEED_UNKNOWN;
 142                cmd->base.duplex = DUPLEX_UNKNOWN;
 143        }
 144
 145        return 0;
 146}
 147
 148static u32 ixgbevf_get_msglevel(struct net_device *netdev)
 149{
 150        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 151
 152        return adapter->msg_enable;
 153}
 154
 155static void ixgbevf_set_msglevel(struct net_device *netdev, u32 data)
 156{
 157        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 158
 159        adapter->msg_enable = data;
 160}
 161
 162#define IXGBE_GET_STAT(_A_, _R_) (_A_->stats._R_)
 163
 164static int ixgbevf_get_regs_len(struct net_device *netdev)
 165{
 166#define IXGBE_REGS_LEN 45
 167        return IXGBE_REGS_LEN * sizeof(u32);
 168}
 169
 170static void ixgbevf_get_regs(struct net_device *netdev,
 171                             struct ethtool_regs *regs,
 172                             void *p)
 173{
 174        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 175        struct ixgbe_hw *hw = &adapter->hw;
 176        u32 *regs_buff = p;
 177        u32 regs_len = ixgbevf_get_regs_len(netdev);
 178        u8 i;
 179
 180        memset(p, 0, regs_len);
 181
 182        /* generate a number suitable for ethtool's register version */
 183        regs->version = (1u << 24) | (hw->revision_id << 16) | hw->device_id;
 184
 185        /* General Registers */
 186        regs_buff[0] = IXGBE_READ_REG(hw, IXGBE_VFCTRL);
 187        regs_buff[1] = IXGBE_READ_REG(hw, IXGBE_VFSTATUS);
 188        regs_buff[2] = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
 189        regs_buff[3] = IXGBE_READ_REG(hw, IXGBE_VFRXMEMWRAP);
 190        regs_buff[4] = IXGBE_READ_REG(hw, IXGBE_VFFRTIMER);
 191
 192        /* Interrupt */
 193        /* don't read EICR because it can clear interrupt causes, instead
 194         * read EICS which is a shadow but doesn't clear EICR
 195         */
 196        regs_buff[5] = IXGBE_READ_REG(hw, IXGBE_VTEICS);
 197        regs_buff[6] = IXGBE_READ_REG(hw, IXGBE_VTEICS);
 198        regs_buff[7] = IXGBE_READ_REG(hw, IXGBE_VTEIMS);
 199        regs_buff[8] = IXGBE_READ_REG(hw, IXGBE_VTEIMC);
 200        regs_buff[9] = IXGBE_READ_REG(hw, IXGBE_VTEIAC);
 201        regs_buff[10] = IXGBE_READ_REG(hw, IXGBE_VTEIAM);
 202        regs_buff[11] = IXGBE_READ_REG(hw, IXGBE_VTEITR(0));
 203        regs_buff[12] = IXGBE_READ_REG(hw, IXGBE_VTIVAR(0));
 204        regs_buff[13] = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
 205
 206        /* Receive DMA */
 207        for (i = 0; i < 2; i++)
 208                regs_buff[14 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDBAL(i));
 209        for (i = 0; i < 2; i++)
 210                regs_buff[16 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDBAH(i));
 211        for (i = 0; i < 2; i++)
 212                regs_buff[18 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDLEN(i));
 213        for (i = 0; i < 2; i++)
 214                regs_buff[20 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDH(i));
 215        for (i = 0; i < 2; i++)
 216                regs_buff[22 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDT(i));
 217        for (i = 0; i < 2; i++)
 218                regs_buff[24 + i] = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i));
 219        for (i = 0; i < 2; i++)
 220                regs_buff[26 + i] = IXGBE_READ_REG(hw, IXGBE_VFSRRCTL(i));
 221
 222        /* Receive */
 223        regs_buff[28] = IXGBE_READ_REG(hw, IXGBE_VFPSRTYPE);
 224
 225        /* Transmit */
 226        for (i = 0; i < 2; i++)
 227                regs_buff[29 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDBAL(i));
 228        for (i = 0; i < 2; i++)
 229                regs_buff[31 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDBAH(i));
 230        for (i = 0; i < 2; i++)
 231                regs_buff[33 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDLEN(i));
 232        for (i = 0; i < 2; i++)
 233                regs_buff[35 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDH(i));
 234        for (i = 0; i < 2; i++)
 235                regs_buff[37 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDT(i));
 236        for (i = 0; i < 2; i++)
 237                regs_buff[39 + i] = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(i));
 238        for (i = 0; i < 2; i++)
 239                regs_buff[41 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDWBAL(i));
 240        for (i = 0; i < 2; i++)
 241                regs_buff[43 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDWBAH(i));
 242}
 243
 244static void ixgbevf_get_drvinfo(struct net_device *netdev,
 245                                struct ethtool_drvinfo *drvinfo)
 246{
 247        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 248
 249        strlcpy(drvinfo->driver, ixgbevf_driver_name, sizeof(drvinfo->driver));
 250        strlcpy(drvinfo->version, ixgbevf_driver_version,
 251                sizeof(drvinfo->version));
 252        strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
 253                sizeof(drvinfo->bus_info));
 254
 255        drvinfo->n_priv_flags = IXGBEVF_PRIV_FLAGS_STR_LEN;
 256}
 257
 258static void ixgbevf_get_ringparam(struct net_device *netdev,
 259                                  struct ethtool_ringparam *ring)
 260{
 261        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 262
 263        ring->rx_max_pending = IXGBEVF_MAX_RXD;
 264        ring->tx_max_pending = IXGBEVF_MAX_TXD;
 265        ring->rx_pending = adapter->rx_ring_count;
 266        ring->tx_pending = adapter->tx_ring_count;
 267}
 268
 269static int ixgbevf_set_ringparam(struct net_device *netdev,
 270                                 struct ethtool_ringparam *ring)
 271{
 272        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 273        struct ixgbevf_ring *tx_ring = NULL, *rx_ring = NULL;
 274        u32 new_rx_count, new_tx_count;
 275        int i, j, err = 0;
 276
 277        if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
 278                return -EINVAL;
 279
 280        new_tx_count = max_t(u32, ring->tx_pending, IXGBEVF_MIN_TXD);
 281        new_tx_count = min_t(u32, new_tx_count, IXGBEVF_MAX_TXD);
 282        new_tx_count = ALIGN(new_tx_count, IXGBE_REQ_TX_DESCRIPTOR_MULTIPLE);
 283
 284        new_rx_count = max_t(u32, ring->rx_pending, IXGBEVF_MIN_RXD);
 285        new_rx_count = min_t(u32, new_rx_count, IXGBEVF_MAX_RXD);
 286        new_rx_count = ALIGN(new_rx_count, IXGBE_REQ_RX_DESCRIPTOR_MULTIPLE);
 287
 288        /* if nothing to do return success */
 289        if ((new_tx_count == adapter->tx_ring_count) &&
 290            (new_rx_count == adapter->rx_ring_count))
 291                return 0;
 292
 293        while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
 294                usleep_range(1000, 2000);
 295
 296        if (!netif_running(adapter->netdev)) {
 297                for (i = 0; i < adapter->num_tx_queues; i++)
 298                        adapter->tx_ring[i]->count = new_tx_count;
 299                for (i = 0; i < adapter->num_xdp_queues; i++)
 300                        adapter->xdp_ring[i]->count = new_tx_count;
 301                for (i = 0; i < adapter->num_rx_queues; i++)
 302                        adapter->rx_ring[i]->count = new_rx_count;
 303                adapter->tx_ring_count = new_tx_count;
 304                adapter->xdp_ring_count = new_tx_count;
 305                adapter->rx_ring_count = new_rx_count;
 306                goto clear_reset;
 307        }
 308
 309        if (new_tx_count != adapter->tx_ring_count) {
 310                tx_ring = vmalloc(array_size(sizeof(*tx_ring),
 311                                             adapter->num_tx_queues +
 312                                                adapter->num_xdp_queues));
 313                if (!tx_ring) {
 314                        err = -ENOMEM;
 315                        goto clear_reset;
 316                }
 317
 318                for (i = 0; i < adapter->num_tx_queues; i++) {
 319                        /* clone ring and setup updated count */
 320                        tx_ring[i] = *adapter->tx_ring[i];
 321                        tx_ring[i].count = new_tx_count;
 322                        err = ixgbevf_setup_tx_resources(&tx_ring[i]);
 323                        if (err) {
 324                                while (i) {
 325                                        i--;
 326                                        ixgbevf_free_tx_resources(&tx_ring[i]);
 327                                }
 328
 329                                vfree(tx_ring);
 330                                tx_ring = NULL;
 331
 332                                goto clear_reset;
 333                        }
 334                }
 335
 336                for (j = 0; j < adapter->num_xdp_queues; i++, j++) {
 337                        /* clone ring and setup updated count */
 338                        tx_ring[i] = *adapter->xdp_ring[j];
 339                        tx_ring[i].count = new_tx_count;
 340                        err = ixgbevf_setup_tx_resources(&tx_ring[i]);
 341                        if (err) {
 342                                while (i) {
 343                                        i--;
 344                                        ixgbevf_free_tx_resources(&tx_ring[i]);
 345                                }
 346
 347                                vfree(tx_ring);
 348                                tx_ring = NULL;
 349
 350                                goto clear_reset;
 351                        }
 352                }
 353        }
 354
 355        if (new_rx_count != adapter->rx_ring_count) {
 356                rx_ring = vmalloc(array_size(sizeof(*rx_ring),
 357                                             adapter->num_rx_queues));
 358                if (!rx_ring) {
 359                        err = -ENOMEM;
 360                        goto clear_reset;
 361                }
 362
 363                for (i = 0; i < adapter->num_rx_queues; i++) {
 364                        /* clone ring and setup updated count */
 365                        rx_ring[i] = *adapter->rx_ring[i];
 366
 367                        /* Clear copied XDP RX-queue info */
 368                        memset(&rx_ring[i].xdp_rxq, 0,
 369                               sizeof(rx_ring[i].xdp_rxq));
 370
 371                        rx_ring[i].count = new_rx_count;
 372                        err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
 373                        if (err) {
 374                                while (i) {
 375                                        i--;
 376                                        ixgbevf_free_rx_resources(&rx_ring[i]);
 377                                }
 378
 379                                vfree(rx_ring);
 380                                rx_ring = NULL;
 381
 382                                goto clear_reset;
 383                        }
 384                }
 385        }
 386
 387        /* bring interface down to prepare for update */
 388        ixgbevf_down(adapter);
 389
 390        /* Tx */
 391        if (tx_ring) {
 392                for (i = 0; i < adapter->num_tx_queues; i++) {
 393                        ixgbevf_free_tx_resources(adapter->tx_ring[i]);
 394                        *adapter->tx_ring[i] = tx_ring[i];
 395                }
 396                adapter->tx_ring_count = new_tx_count;
 397
 398                for (j = 0; j < adapter->num_xdp_queues; i++, j++) {
 399                        ixgbevf_free_tx_resources(adapter->xdp_ring[j]);
 400                        *adapter->xdp_ring[j] = tx_ring[i];
 401                }
 402                adapter->xdp_ring_count = new_tx_count;
 403
 404                vfree(tx_ring);
 405                tx_ring = NULL;
 406        }
 407
 408        /* Rx */
 409        if (rx_ring) {
 410                for (i = 0; i < adapter->num_rx_queues; i++) {
 411                        ixgbevf_free_rx_resources(adapter->rx_ring[i]);
 412                        *adapter->rx_ring[i] = rx_ring[i];
 413                }
 414                adapter->rx_ring_count = new_rx_count;
 415
 416                vfree(rx_ring);
 417                rx_ring = NULL;
 418        }
 419
 420        /* restore interface using new values */
 421        ixgbevf_up(adapter);
 422
 423clear_reset:
 424        /* free Tx resources if Rx error is encountered */
 425        if (tx_ring) {
 426                for (i = 0;
 427                     i < adapter->num_tx_queues + adapter->num_xdp_queues; i++)
 428                        ixgbevf_free_tx_resources(&tx_ring[i]);
 429                vfree(tx_ring);
 430        }
 431
 432        clear_bit(__IXGBEVF_RESETTING, &adapter->state);
 433        return err;
 434}
 435
 436static int ixgbevf_get_sset_count(struct net_device *netdev, int stringset)
 437{
 438        switch (stringset) {
 439        case ETH_SS_TEST:
 440                return IXGBEVF_TEST_LEN;
 441        case ETH_SS_STATS:
 442                return IXGBEVF_STATS_LEN;
 443        case ETH_SS_PRIV_FLAGS:
 444                return IXGBEVF_PRIV_FLAGS_STR_LEN;
 445        default:
 446                return -EINVAL;
 447        }
 448}
 449
 450static void ixgbevf_get_ethtool_stats(struct net_device *netdev,
 451                                      struct ethtool_stats *stats, u64 *data)
 452{
 453        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 454        struct rtnl_link_stats64 temp;
 455        const struct rtnl_link_stats64 *net_stats;
 456        unsigned int start;
 457        struct ixgbevf_ring *ring;
 458        int i, j;
 459        char *p;
 460
 461        ixgbevf_update_stats(adapter);
 462        net_stats = dev_get_stats(netdev, &temp);
 463        for (i = 0; i < IXGBEVF_GLOBAL_STATS_LEN; i++) {
 464                switch (ixgbevf_gstrings_stats[i].type) {
 465                case NETDEV_STATS:
 466                        p = (char *)net_stats +
 467                                        ixgbevf_gstrings_stats[i].stat_offset;
 468                        break;
 469                case IXGBEVF_STATS:
 470                        p = (char *)adapter +
 471                                        ixgbevf_gstrings_stats[i].stat_offset;
 472                        break;
 473                default:
 474                        data[i] = 0;
 475                        continue;
 476                }
 477
 478                data[i] = (ixgbevf_gstrings_stats[i].sizeof_stat ==
 479                           sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
 480        }
 481
 482        /* populate Tx queue data */
 483        for (j = 0; j < adapter->num_tx_queues; j++) {
 484                ring = adapter->tx_ring[j];
 485                if (!ring) {
 486                        data[i++] = 0;
 487                        data[i++] = 0;
 488                        continue;
 489                }
 490
 491                do {
 492                        start = u64_stats_fetch_begin_irq(&ring->syncp);
 493                        data[i]   = ring->stats.packets;
 494                        data[i + 1] = ring->stats.bytes;
 495                } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
 496                i += 2;
 497        }
 498
 499        /* populate XDP queue data */
 500        for (j = 0; j < adapter->num_xdp_queues; j++) {
 501                ring = adapter->xdp_ring[j];
 502                if (!ring) {
 503                        data[i++] = 0;
 504                        data[i++] = 0;
 505                        continue;
 506                }
 507
 508                do {
 509                        start = u64_stats_fetch_begin_irq(&ring->syncp);
 510                        data[i] = ring->stats.packets;
 511                        data[i + 1] = ring->stats.bytes;
 512                } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
 513                i += 2;
 514        }
 515
 516        /* populate Rx queue data */
 517        for (j = 0; j < adapter->num_rx_queues; j++) {
 518                ring = adapter->rx_ring[j];
 519                if (!ring) {
 520                        data[i++] = 0;
 521                        data[i++] = 0;
 522                        continue;
 523                }
 524
 525                do {
 526                        start = u64_stats_fetch_begin_irq(&ring->syncp);
 527                        data[i]   = ring->stats.packets;
 528                        data[i + 1] = ring->stats.bytes;
 529                } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
 530                i += 2;
 531        }
 532}
 533
 534static void ixgbevf_get_strings(struct net_device *netdev, u32 stringset,
 535                                u8 *data)
 536{
 537        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 538        char *p = (char *)data;
 539        int i;
 540
 541        switch (stringset) {
 542        case ETH_SS_TEST:
 543                memcpy(data, *ixgbe_gstrings_test,
 544                       IXGBEVF_TEST_LEN * ETH_GSTRING_LEN);
 545                break;
 546        case ETH_SS_STATS:
 547                for (i = 0; i < IXGBEVF_GLOBAL_STATS_LEN; i++) {
 548                        memcpy(p, ixgbevf_gstrings_stats[i].stat_string,
 549                               ETH_GSTRING_LEN);
 550                        p += ETH_GSTRING_LEN;
 551                }
 552
 553                for (i = 0; i < adapter->num_tx_queues; i++) {
 554                        sprintf(p, "tx_queue_%u_packets", i);
 555                        p += ETH_GSTRING_LEN;
 556                        sprintf(p, "tx_queue_%u_bytes", i);
 557                        p += ETH_GSTRING_LEN;
 558                }
 559                for (i = 0; i < adapter->num_xdp_queues; i++) {
 560                        sprintf(p, "xdp_queue_%u_packets", i);
 561                        p += ETH_GSTRING_LEN;
 562                        sprintf(p, "xdp_queue_%u_bytes", i);
 563                        p += ETH_GSTRING_LEN;
 564                }
 565                for (i = 0; i < adapter->num_rx_queues; i++) {
 566                        sprintf(p, "rx_queue_%u_packets", i);
 567                        p += ETH_GSTRING_LEN;
 568                        sprintf(p, "rx_queue_%u_bytes", i);
 569                        p += ETH_GSTRING_LEN;
 570                }
 571                break;
 572        case ETH_SS_PRIV_FLAGS:
 573                memcpy(data, ixgbevf_priv_flags_strings,
 574                       IXGBEVF_PRIV_FLAGS_STR_LEN * ETH_GSTRING_LEN);
 575                break;
 576        }
 577}
 578
 579static int ixgbevf_link_test(struct ixgbevf_adapter *adapter, u64 *data)
 580{
 581        struct ixgbe_hw *hw = &adapter->hw;
 582        bool link_up;
 583        u32 link_speed = 0;
 584        *data = 0;
 585
 586        hw->mac.ops.check_link(hw, &link_speed, &link_up, true);
 587        if (!link_up)
 588                *data = 1;
 589
 590        return *data;
 591}
 592
 593/* ethtool register test data */
 594struct ixgbevf_reg_test {
 595        u16 reg;
 596        u8  array_len;
 597        u8  test_type;
 598        u32 mask;
 599        u32 write;
 600};
 601
 602/* In the hardware, registers are laid out either singly, in arrays
 603 * spaced 0x40 bytes apart, or in contiguous tables.  We assume
 604 * most tests take place on arrays or single registers (handled
 605 * as a single-element array) and special-case the tables.
 606 * Table tests are always pattern tests.
 607 *
 608 * We also make provision for some required setup steps by specifying
 609 * registers to be written without any read-back testing.
 610 */
 611
 612#define PATTERN_TEST    1
 613#define SET_READ_TEST   2
 614#define WRITE_NO_TEST   3
 615#define TABLE32_TEST    4
 616#define TABLE64_TEST_LO 5
 617#define TABLE64_TEST_HI 6
 618
 619/* default VF register test */
 620static const struct ixgbevf_reg_test reg_test_vf[] = {
 621        { IXGBE_VFRDBAL(0), 2, PATTERN_TEST, 0xFFFFFF80, 0xFFFFFF80 },
 622        { IXGBE_VFRDBAH(0), 2, PATTERN_TEST, 0xFFFFFFFF, 0xFFFFFFFF },
 623        { IXGBE_VFRDLEN(0), 2, PATTERN_TEST, 0x000FFF80, 0x000FFFFF },
 624        { IXGBE_VFRXDCTL(0), 2, WRITE_NO_TEST, 0, IXGBE_RXDCTL_ENABLE },
 625        { IXGBE_VFRDT(0), 2, PATTERN_TEST, 0x0000FFFF, 0x0000FFFF },
 626        { IXGBE_VFRXDCTL(0), 2, WRITE_NO_TEST, 0, 0 },
 627        { IXGBE_VFTDBAL(0), 2, PATTERN_TEST, 0xFFFFFF80, 0xFFFFFFFF },
 628        { IXGBE_VFTDBAH(0), 2, PATTERN_TEST, 0xFFFFFFFF, 0xFFFFFFFF },
 629        { IXGBE_VFTDLEN(0), 2, PATTERN_TEST, 0x000FFF80, 0x000FFF80 },
 630        { .reg = 0 }
 631};
 632
 633static const u32 register_test_patterns[] = {
 634        0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF
 635};
 636
 637static bool reg_pattern_test(struct ixgbevf_adapter *adapter, u64 *data,
 638                             int reg, u32 mask, u32 write)
 639{
 640        u32 pat, val, before;
 641
 642        if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
 643                *data = 1;
 644                return true;
 645        }
 646        for (pat = 0; pat < ARRAY_SIZE(register_test_patterns); pat++) {
 647                before = ixgbevf_read_reg(&adapter->hw, reg);
 648                ixgbe_write_reg(&adapter->hw, reg,
 649                                register_test_patterns[pat] & write);
 650                val = ixgbevf_read_reg(&adapter->hw, reg);
 651                if (val != (register_test_patterns[pat] & write & mask)) {
 652                        hw_dbg(&adapter->hw,
 653                               "pattern test reg %04X failed: got 0x%08X expected 0x%08X\n",
 654                               reg, val,
 655                               register_test_patterns[pat] & write & mask);
 656                        *data = reg;
 657                        ixgbe_write_reg(&adapter->hw, reg, before);
 658                        return true;
 659                }
 660                ixgbe_write_reg(&adapter->hw, reg, before);
 661        }
 662        return false;
 663}
 664
 665static bool reg_set_and_check(struct ixgbevf_adapter *adapter, u64 *data,
 666                              int reg, u32 mask, u32 write)
 667{
 668        u32 val, before;
 669
 670        if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
 671                *data = 1;
 672                return true;
 673        }
 674        before = ixgbevf_read_reg(&adapter->hw, reg);
 675        ixgbe_write_reg(&adapter->hw, reg, write & mask);
 676        val = ixgbevf_read_reg(&adapter->hw, reg);
 677        if ((write & mask) != (val & mask)) {
 678                pr_err("set/check reg %04X test failed: got 0x%08X expected 0x%08X\n",
 679                       reg, (val & mask), write & mask);
 680                *data = reg;
 681                ixgbe_write_reg(&adapter->hw, reg, before);
 682                return true;
 683        }
 684        ixgbe_write_reg(&adapter->hw, reg, before);
 685        return false;
 686}
 687
 688static int ixgbevf_reg_test(struct ixgbevf_adapter *adapter, u64 *data)
 689{
 690        const struct ixgbevf_reg_test *test;
 691        u32 i;
 692
 693        if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
 694                dev_err(&adapter->pdev->dev,
 695                        "Adapter removed - register test blocked\n");
 696                *data = 1;
 697                return 1;
 698        }
 699        test = reg_test_vf;
 700
 701        /* Perform the register test, looping through the test table
 702         * until we either fail or reach the null entry.
 703         */
 704        while (test->reg) {
 705                for (i = 0; i < test->array_len; i++) {
 706                        bool b = false;
 707
 708                        switch (test->test_type) {
 709                        case PATTERN_TEST:
 710                                b = reg_pattern_test(adapter, data,
 711                                                     test->reg + (i * 0x40),
 712                                                     test->mask,
 713                                                     test->write);
 714                                break;
 715                        case SET_READ_TEST:
 716                                b = reg_set_and_check(adapter, data,
 717                                                      test->reg + (i * 0x40),
 718                                                      test->mask,
 719                                                      test->write);
 720                                break;
 721                        case WRITE_NO_TEST:
 722                                ixgbe_write_reg(&adapter->hw,
 723                                                test->reg + (i * 0x40),
 724                                                test->write);
 725                                break;
 726                        case TABLE32_TEST:
 727                                b = reg_pattern_test(adapter, data,
 728                                                     test->reg + (i * 4),
 729                                                     test->mask,
 730                                                     test->write);
 731                                break;
 732                        case TABLE64_TEST_LO:
 733                                b = reg_pattern_test(adapter, data,
 734                                                     test->reg + (i * 8),
 735                                                     test->mask,
 736                                                     test->write);
 737                                break;
 738                        case TABLE64_TEST_HI:
 739                                b = reg_pattern_test(adapter, data,
 740                                                     test->reg + 4 + (i * 8),
 741                                                     test->mask,
 742                                                     test->write);
 743                                break;
 744                        }
 745                        if (b)
 746                                return 1;
 747                }
 748                test++;
 749        }
 750
 751        *data = 0;
 752        return *data;
 753}
 754
 755static void ixgbevf_diag_test(struct net_device *netdev,
 756                              struct ethtool_test *eth_test, u64 *data)
 757{
 758        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 759        bool if_running = netif_running(netdev);
 760
 761        if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
 762                dev_err(&adapter->pdev->dev,
 763                        "Adapter removed - test blocked\n");
 764                data[0] = 1;
 765                data[1] = 1;
 766                eth_test->flags |= ETH_TEST_FL_FAILED;
 767                return;
 768        }
 769        set_bit(__IXGBEVF_TESTING, &adapter->state);
 770        if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
 771                /* Offline tests */
 772
 773                hw_dbg(&adapter->hw, "offline testing starting\n");
 774
 775                /* Link test performed before hardware reset so autoneg doesn't
 776                 * interfere with test result
 777                 */
 778                if (ixgbevf_link_test(adapter, &data[1]))
 779                        eth_test->flags |= ETH_TEST_FL_FAILED;
 780
 781                if (if_running)
 782                        /* indicate we're in test mode */
 783                        ixgbevf_close(netdev);
 784                else
 785                        ixgbevf_reset(adapter);
 786
 787                hw_dbg(&adapter->hw, "register testing starting\n");
 788                if (ixgbevf_reg_test(adapter, &data[0]))
 789                        eth_test->flags |= ETH_TEST_FL_FAILED;
 790
 791                ixgbevf_reset(adapter);
 792
 793                clear_bit(__IXGBEVF_TESTING, &adapter->state);
 794                if (if_running)
 795                        ixgbevf_open(netdev);
 796        } else {
 797                hw_dbg(&adapter->hw, "online testing starting\n");
 798                /* Online tests */
 799                if (ixgbevf_link_test(adapter, &data[1]))
 800                        eth_test->flags |= ETH_TEST_FL_FAILED;
 801
 802                /* Online tests aren't run; pass by default */
 803                data[0] = 0;
 804
 805                clear_bit(__IXGBEVF_TESTING, &adapter->state);
 806        }
 807        msleep_interruptible(4 * 1000);
 808}
 809
 810static int ixgbevf_nway_reset(struct net_device *netdev)
 811{
 812        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 813
 814        if (netif_running(netdev))
 815                ixgbevf_reinit_locked(adapter);
 816
 817        return 0;
 818}
 819
 820static int ixgbevf_get_coalesce(struct net_device *netdev,
 821                                struct ethtool_coalesce *ec)
 822{
 823        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 824
 825        /* only valid if in constant ITR mode */
 826        if (adapter->rx_itr_setting <= 1)
 827                ec->rx_coalesce_usecs = adapter->rx_itr_setting;
 828        else
 829                ec->rx_coalesce_usecs = adapter->rx_itr_setting >> 2;
 830
 831        /* if in mixed Tx/Rx queues per vector mode, report only Rx settings */
 832        if (adapter->q_vector[0]->tx.count && adapter->q_vector[0]->rx.count)
 833                return 0;
 834
 835        /* only valid if in constant ITR mode */
 836        if (adapter->tx_itr_setting <= 1)
 837                ec->tx_coalesce_usecs = adapter->tx_itr_setting;
 838        else
 839                ec->tx_coalesce_usecs = adapter->tx_itr_setting >> 2;
 840
 841        return 0;
 842}
 843
 844static int ixgbevf_set_coalesce(struct net_device *netdev,
 845                                struct ethtool_coalesce *ec)
 846{
 847        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 848        struct ixgbevf_q_vector *q_vector;
 849        int num_vectors, i;
 850        u16 tx_itr_param, rx_itr_param;
 851
 852        /* don't accept Tx specific changes if we've got mixed RxTx vectors */
 853        if (adapter->q_vector[0]->tx.count &&
 854            adapter->q_vector[0]->rx.count && ec->tx_coalesce_usecs)
 855                return -EINVAL;
 856
 857        if ((ec->rx_coalesce_usecs > (IXGBE_MAX_EITR >> 2)) ||
 858            (ec->tx_coalesce_usecs > (IXGBE_MAX_EITR >> 2)))
 859                return -EINVAL;
 860
 861        if (ec->rx_coalesce_usecs > 1)
 862                adapter->rx_itr_setting = ec->rx_coalesce_usecs << 2;
 863        else
 864                adapter->rx_itr_setting = ec->rx_coalesce_usecs;
 865
 866        if (adapter->rx_itr_setting == 1)
 867                rx_itr_param = IXGBE_20K_ITR;
 868        else
 869                rx_itr_param = adapter->rx_itr_setting;
 870
 871        if (ec->tx_coalesce_usecs > 1)
 872                adapter->tx_itr_setting = ec->tx_coalesce_usecs << 2;
 873        else
 874                adapter->tx_itr_setting = ec->tx_coalesce_usecs;
 875
 876        if (adapter->tx_itr_setting == 1)
 877                tx_itr_param = IXGBE_12K_ITR;
 878        else
 879                tx_itr_param = adapter->tx_itr_setting;
 880
 881        num_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
 882
 883        for (i = 0; i < num_vectors; i++) {
 884                q_vector = adapter->q_vector[i];
 885                if (q_vector->tx.count && !q_vector->rx.count)
 886                        /* Tx only */
 887                        q_vector->itr = tx_itr_param;
 888                else
 889                        /* Rx only or mixed */
 890                        q_vector->itr = rx_itr_param;
 891                ixgbevf_write_eitr(q_vector);
 892        }
 893
 894        return 0;
 895}
 896
 897static int ixgbevf_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
 898                             u32 *rules __always_unused)
 899{
 900        struct ixgbevf_adapter *adapter = netdev_priv(dev);
 901
 902        switch (info->cmd) {
 903        case ETHTOOL_GRXRINGS:
 904                info->data = adapter->num_rx_queues;
 905                return 0;
 906        default:
 907                hw_dbg(&adapter->hw, "Command parameters not supported\n");
 908                return -EOPNOTSUPP;
 909        }
 910}
 911
 912static u32 ixgbevf_get_rxfh_indir_size(struct net_device *netdev)
 913{
 914        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 915
 916        if (adapter->hw.mac.type >= ixgbe_mac_X550_vf)
 917                return IXGBEVF_X550_VFRETA_SIZE;
 918
 919        return IXGBEVF_82599_RETA_SIZE;
 920}
 921
 922static u32 ixgbevf_get_rxfh_key_size(struct net_device *netdev)
 923{
 924        return IXGBEVF_RSS_HASH_KEY_SIZE;
 925}
 926
 927static int ixgbevf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
 928                            u8 *hfunc)
 929{
 930        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 931        int err = 0;
 932
 933        if (hfunc)
 934                *hfunc = ETH_RSS_HASH_TOP;
 935
 936        if (adapter->hw.mac.type >= ixgbe_mac_X550_vf) {
 937                if (key)
 938                        memcpy(key, adapter->rss_key,
 939                               ixgbevf_get_rxfh_key_size(netdev));
 940
 941                if (indir) {
 942                        int i;
 943
 944                        for (i = 0; i < IXGBEVF_X550_VFRETA_SIZE; i++)
 945                                indir[i] = adapter->rss_indir_tbl[i];
 946                }
 947        } else {
 948                /* If neither indirection table nor hash key was requested
 949                 *  - just return a success avoiding taking any locks.
 950                 */
 951                if (!indir && !key)
 952                        return 0;
 953
 954                spin_lock_bh(&adapter->mbx_lock);
 955                if (indir)
 956                        err = ixgbevf_get_reta_locked(&adapter->hw, indir,
 957                                                      adapter->num_rx_queues);
 958
 959                if (!err && key)
 960                        err = ixgbevf_get_rss_key_locked(&adapter->hw, key);
 961
 962                spin_unlock_bh(&adapter->mbx_lock);
 963        }
 964
 965        return err;
 966}
 967
 968static u32 ixgbevf_get_priv_flags(struct net_device *netdev)
 969{
 970        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 971        u32 priv_flags = 0;
 972
 973        if (adapter->flags & IXGBEVF_FLAGS_LEGACY_RX)
 974                priv_flags |= IXGBEVF_PRIV_FLAGS_LEGACY_RX;
 975
 976        return priv_flags;
 977}
 978
 979static int ixgbevf_set_priv_flags(struct net_device *netdev, u32 priv_flags)
 980{
 981        struct ixgbevf_adapter *adapter = netdev_priv(netdev);
 982        unsigned int flags = adapter->flags;
 983
 984        flags &= ~IXGBEVF_FLAGS_LEGACY_RX;
 985        if (priv_flags & IXGBEVF_PRIV_FLAGS_LEGACY_RX)
 986                flags |= IXGBEVF_FLAGS_LEGACY_RX;
 987
 988        if (flags != adapter->flags) {
 989                adapter->flags = flags;
 990
 991                /* reset interface to repopulate queues */
 992                if (netif_running(netdev))
 993                        ixgbevf_reinit_locked(adapter);
 994        }
 995
 996        return 0;
 997}
 998
 999static const struct ethtool_ops ixgbevf_ethtool_ops = {
1000        .get_drvinfo            = ixgbevf_get_drvinfo,
1001        .get_regs_len           = ixgbevf_get_regs_len,
1002        .get_regs               = ixgbevf_get_regs,
1003        .nway_reset             = ixgbevf_nway_reset,
1004        .get_link               = ethtool_op_get_link,
1005        .get_ringparam          = ixgbevf_get_ringparam,
1006        .set_ringparam          = ixgbevf_set_ringparam,
1007        .get_msglevel           = ixgbevf_get_msglevel,
1008        .set_msglevel           = ixgbevf_set_msglevel,
1009        .self_test              = ixgbevf_diag_test,
1010        .get_sset_count         = ixgbevf_get_sset_count,
1011        .get_strings            = ixgbevf_get_strings,
1012        .get_ethtool_stats      = ixgbevf_get_ethtool_stats,
1013        .get_coalesce           = ixgbevf_get_coalesce,
1014        .set_coalesce           = ixgbevf_set_coalesce,
1015        .get_rxnfc              = ixgbevf_get_rxnfc,
1016        .get_rxfh_indir_size    = ixgbevf_get_rxfh_indir_size,
1017        .get_rxfh_key_size      = ixgbevf_get_rxfh_key_size,
1018        .get_rxfh               = ixgbevf_get_rxfh,
1019        .get_link_ksettings     = ixgbevf_get_link_ksettings,
1020        .get_priv_flags         = ixgbevf_get_priv_flags,
1021        .set_priv_flags         = ixgbevf_set_priv_flags,
1022};
1023
1024void ixgbevf_set_ethtool_ops(struct net_device *netdev)
1025{
1026        netdev->ethtool_ops = &ixgbevf_ethtool_ops;
1027}
1028