linux/drivers/net/ethernet/intel/ice/ice_ethtool.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2018, Intel Corporation. */
   3
   4/* ethtool support for ice */
   5
   6#include "ice.h"
   7#include "ice_flow.h"
   8#include "ice_lib.h"
   9#include "ice_dcb_lib.h"
  10
  11struct ice_stats {
  12        char stat_string[ETH_GSTRING_LEN];
  13        int sizeof_stat;
  14        int stat_offset;
  15};
  16
  17#define ICE_STAT(_type, _name, _stat) { \
  18        .stat_string = _name, \
  19        .sizeof_stat = sizeof_field(_type, _stat), \
  20        .stat_offset = offsetof(_type, _stat) \
  21}
  22
  23#define ICE_VSI_STAT(_name, _stat) \
  24                ICE_STAT(struct ice_vsi, _name, _stat)
  25#define ICE_PF_STAT(_name, _stat) \
  26                ICE_STAT(struct ice_pf, _name, _stat)
  27
  28static int ice_q_stats_len(struct net_device *netdev)
  29{
  30        struct ice_netdev_priv *np = netdev_priv(netdev);
  31
  32        return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) *
  33                (sizeof(struct ice_q_stats) / sizeof(u64)));
  34}
  35
  36#define ICE_PF_STATS_LEN        ARRAY_SIZE(ice_gstrings_pf_stats)
  37#define ICE_VSI_STATS_LEN       ARRAY_SIZE(ice_gstrings_vsi_stats)
  38
  39#define ICE_PFC_STATS_LEN ( \
  40                (sizeof_field(struct ice_pf, stats.priority_xoff_rx) + \
  41                 sizeof_field(struct ice_pf, stats.priority_xon_rx) + \
  42                 sizeof_field(struct ice_pf, stats.priority_xoff_tx) + \
  43                 sizeof_field(struct ice_pf, stats.priority_xon_tx)) \
  44                 / sizeof(u64))
  45#define ICE_ALL_STATS_LEN(n)    (ICE_PF_STATS_LEN + ICE_PFC_STATS_LEN + \
  46                                 ICE_VSI_STATS_LEN + ice_q_stats_len(n))
  47
  48static const struct ice_stats ice_gstrings_vsi_stats[] = {
  49        ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
  50        ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
  51        ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
  52        ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
  53        ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
  54        ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
  55        ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes),
  56        ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes),
  57        ICE_VSI_STAT("rx_dropped", eth_stats.rx_discards),
  58        ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
  59        ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed),
  60        ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
  61        ICE_VSI_STAT("tx_errors", eth_stats.tx_errors),
  62        ICE_VSI_STAT("tx_linearize", tx_linearize),
  63};
  64
  65enum ice_ethtool_test_id {
  66        ICE_ETH_TEST_REG = 0,
  67        ICE_ETH_TEST_EEPROM,
  68        ICE_ETH_TEST_INTR,
  69        ICE_ETH_TEST_LOOP,
  70        ICE_ETH_TEST_LINK,
  71};
  72
  73static const char ice_gstrings_test[][ETH_GSTRING_LEN] = {
  74        "Register test  (offline)",
  75        "EEPROM test    (offline)",
  76        "Interrupt test (offline)",
  77        "Loopback test  (offline)",
  78        "Link test   (on/offline)",
  79};
  80
  81#define ICE_TEST_LEN (sizeof(ice_gstrings_test) / ETH_GSTRING_LEN)
  82
  83/* These PF_STATs might look like duplicates of some NETDEV_STATs,
  84 * but they aren't. This device is capable of supporting multiple
  85 * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual
  86 * netdevs whereas the PF_STATs are for the physical function that's
  87 * hosting these netdevs.
  88 *
  89 * The PF_STATs are appended to the netdev stats only when ethtool -S
  90 * is queried on the base PF netdev.
  91 */
  92static const struct ice_stats ice_gstrings_pf_stats[] = {
  93        ICE_PF_STAT("rx_bytes.nic", stats.eth.rx_bytes),
  94        ICE_PF_STAT("tx_bytes.nic", stats.eth.tx_bytes),
  95        ICE_PF_STAT("rx_unicast.nic", stats.eth.rx_unicast),
  96        ICE_PF_STAT("tx_unicast.nic", stats.eth.tx_unicast),
  97        ICE_PF_STAT("rx_multicast.nic", stats.eth.rx_multicast),
  98        ICE_PF_STAT("tx_multicast.nic", stats.eth.tx_multicast),
  99        ICE_PF_STAT("rx_broadcast.nic", stats.eth.rx_broadcast),
 100        ICE_PF_STAT("tx_broadcast.nic", stats.eth.tx_broadcast),
 101        ICE_PF_STAT("tx_errors.nic", stats.eth.tx_errors),
 102        ICE_PF_STAT("rx_size_64.nic", stats.rx_size_64),
 103        ICE_PF_STAT("tx_size_64.nic", stats.tx_size_64),
 104        ICE_PF_STAT("rx_size_127.nic", stats.rx_size_127),
 105        ICE_PF_STAT("tx_size_127.nic", stats.tx_size_127),
 106        ICE_PF_STAT("rx_size_255.nic", stats.rx_size_255),
 107        ICE_PF_STAT("tx_size_255.nic", stats.tx_size_255),
 108        ICE_PF_STAT("rx_size_511.nic", stats.rx_size_511),
 109        ICE_PF_STAT("tx_size_511.nic", stats.tx_size_511),
 110        ICE_PF_STAT("rx_size_1023.nic", stats.rx_size_1023),
 111        ICE_PF_STAT("tx_size_1023.nic", stats.tx_size_1023),
 112        ICE_PF_STAT("rx_size_1522.nic", stats.rx_size_1522),
 113        ICE_PF_STAT("tx_size_1522.nic", stats.tx_size_1522),
 114        ICE_PF_STAT("rx_size_big.nic", stats.rx_size_big),
 115        ICE_PF_STAT("tx_size_big.nic", stats.tx_size_big),
 116        ICE_PF_STAT("link_xon_rx.nic", stats.link_xon_rx),
 117        ICE_PF_STAT("link_xon_tx.nic", stats.link_xon_tx),
 118        ICE_PF_STAT("link_xoff_rx.nic", stats.link_xoff_rx),
 119        ICE_PF_STAT("link_xoff_tx.nic", stats.link_xoff_tx),
 120        ICE_PF_STAT("tx_dropped_link_down.nic", stats.tx_dropped_link_down),
 121        ICE_PF_STAT("rx_undersize.nic", stats.rx_undersize),
 122        ICE_PF_STAT("rx_fragments.nic", stats.rx_fragments),
 123        ICE_PF_STAT("rx_oversize.nic", stats.rx_oversize),
 124        ICE_PF_STAT("rx_jabber.nic", stats.rx_jabber),
 125        ICE_PF_STAT("rx_csum_bad.nic", hw_csum_rx_error),
 126        ICE_PF_STAT("rx_length_errors.nic", stats.rx_len_errors),
 127        ICE_PF_STAT("rx_dropped.nic", stats.eth.rx_discards),
 128        ICE_PF_STAT("rx_crc_errors.nic", stats.crc_errors),
 129        ICE_PF_STAT("illegal_bytes.nic", stats.illegal_bytes),
 130        ICE_PF_STAT("mac_local_faults.nic", stats.mac_local_faults),
 131        ICE_PF_STAT("mac_remote_faults.nic", stats.mac_remote_faults),
 132};
 133
 134static const u32 ice_regs_dump_list[] = {
 135        PFGEN_STATE,
 136        PRTGEN_STATUS,
 137        QRX_CTRL(0),
 138        QINT_TQCTL(0),
 139        QINT_RQCTL(0),
 140        PFINT_OICR_ENA,
 141        QRX_ITR(0),
 142        PF0INT_ITR_0(0),
 143        PF0INT_ITR_1(0),
 144        PF0INT_ITR_2(0),
 145};
 146
 147struct ice_priv_flag {
 148        char name[ETH_GSTRING_LEN];
 149        u32 bitno;                      /* bit position in pf->flags */
 150};
 151
 152#define ICE_PRIV_FLAG(_name, _bitno) { \
 153        .name = _name, \
 154        .bitno = _bitno, \
 155}
 156
 157static const struct ice_priv_flag ice_gstrings_priv_flags[] = {
 158        ICE_PRIV_FLAG("link-down-on-close", ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA),
 159        ICE_PRIV_FLAG("fw-lldp-agent", ICE_FLAG_FW_LLDP_AGENT),
 160        ICE_PRIV_FLAG("mdd-auto-reset-vf", ICE_FLAG_MDD_AUTO_RESET_VF),
 161        ICE_PRIV_FLAG("legacy-rx", ICE_FLAG_LEGACY_RX),
 162};
 163
 164#define ICE_PRIV_FLAG_ARRAY_SIZE        ARRAY_SIZE(ice_gstrings_priv_flags)
 165
 166static void
 167ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
 168{
 169        struct ice_netdev_priv *np = netdev_priv(netdev);
 170        struct ice_vsi *vsi = np->vsi;
 171        struct ice_pf *pf = vsi->back;
 172        struct ice_hw *hw = &pf->hw;
 173        struct ice_orom_info *orom;
 174        struct ice_nvm_info *nvm;
 175
 176        nvm = &hw->nvm;
 177        orom = &nvm->orom;
 178
 179        strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
 180        strscpy(drvinfo->version, ice_drv_ver, sizeof(drvinfo->version));
 181
 182        /* Display NVM version (from which the firmware version can be
 183         * determined) which contains more pertinent information.
 184         */
 185        snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 186                 "%x.%02x 0x%x %d.%d.%d", nvm->major_ver, nvm->minor_ver,
 187                 nvm->eetrack, orom->major, orom->build, orom->patch);
 188
 189        strscpy(drvinfo->bus_info, pci_name(pf->pdev),
 190                sizeof(drvinfo->bus_info));
 191        drvinfo->n_priv_flags = ICE_PRIV_FLAG_ARRAY_SIZE;
 192}
 193
 194static int ice_get_regs_len(struct net_device __always_unused *netdev)
 195{
 196        return sizeof(ice_regs_dump_list);
 197}
 198
 199static void
 200ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
 201{
 202        struct ice_netdev_priv *np = netdev_priv(netdev);
 203        struct ice_pf *pf = np->vsi->back;
 204        struct ice_hw *hw = &pf->hw;
 205        u32 *regs_buf = (u32 *)p;
 206        int i;
 207
 208        regs->version = 1;
 209
 210        for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i)
 211                regs_buf[i] = rd32(hw, ice_regs_dump_list[i]);
 212}
 213
 214static u32 ice_get_msglevel(struct net_device *netdev)
 215{
 216        struct ice_netdev_priv *np = netdev_priv(netdev);
 217        struct ice_pf *pf = np->vsi->back;
 218
 219#ifndef CONFIG_DYNAMIC_DEBUG
 220        if (pf->hw.debug_mask)
 221                netdev_info(netdev, "hw debug_mask: 0x%llX\n",
 222                            pf->hw.debug_mask);
 223#endif /* !CONFIG_DYNAMIC_DEBUG */
 224
 225        return pf->msg_enable;
 226}
 227
 228static void ice_set_msglevel(struct net_device *netdev, u32 data)
 229{
 230        struct ice_netdev_priv *np = netdev_priv(netdev);
 231        struct ice_pf *pf = np->vsi->back;
 232
 233#ifndef CONFIG_DYNAMIC_DEBUG
 234        if (ICE_DBG_USER & data)
 235                pf->hw.debug_mask = data;
 236        else
 237                pf->msg_enable = data;
 238#else
 239        pf->msg_enable = data;
 240#endif /* !CONFIG_DYNAMIC_DEBUG */
 241}
 242
 243static int ice_get_eeprom_len(struct net_device *netdev)
 244{
 245        struct ice_netdev_priv *np = netdev_priv(netdev);
 246        struct ice_pf *pf = np->vsi->back;
 247
 248        return (int)pf->hw.nvm.flash_size;
 249}
 250
 251static int
 252ice_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
 253               u8 *bytes)
 254{
 255        struct ice_netdev_priv *np = netdev_priv(netdev);
 256        struct ice_vsi *vsi = np->vsi;
 257        struct ice_pf *pf = vsi->back;
 258        struct ice_hw *hw = &pf->hw;
 259        enum ice_status status;
 260        struct device *dev;
 261        int ret = 0;
 262        u8 *buf;
 263
 264        dev = ice_pf_to_dev(pf);
 265
 266        eeprom->magic = hw->vendor_id | (hw->device_id << 16);
 267        netdev_dbg(netdev, "GEEPROM cmd 0x%08x, offset 0x%08x, len 0x%08x\n",
 268                   eeprom->cmd, eeprom->offset, eeprom->len);
 269
 270        buf = kzalloc(eeprom->len, GFP_KERNEL);
 271        if (!buf)
 272                return -ENOMEM;
 273
 274        status = ice_acquire_nvm(hw, ICE_RES_READ);
 275        if (status) {
 276                dev_err(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
 277                        status, hw->adminq.sq_last_status);
 278                ret = -EIO;
 279                goto out;
 280        }
 281
 282        status = ice_read_flat_nvm(hw, eeprom->offset, &eeprom->len, buf,
 283                                   false);
 284        if (status) {
 285                dev_err(dev, "ice_read_flat_nvm failed, err %d aq_err %d\n",
 286                        status, hw->adminq.sq_last_status);
 287                ret = -EIO;
 288                goto release;
 289        }
 290
 291        memcpy(bytes, buf, eeprom->len);
 292release:
 293        ice_release_nvm(hw);
 294out:
 295        kfree(buf);
 296        return ret;
 297}
 298
 299/**
 300 * ice_active_vfs - check if there are any active VFs
 301 * @pf: board private structure
 302 *
 303 * Returns true if an active VF is found, otherwise returns false
 304 */
 305static bool ice_active_vfs(struct ice_pf *pf)
 306{
 307        int i;
 308
 309        ice_for_each_vf(pf, i) {
 310                struct ice_vf *vf = &pf->vf[i];
 311
 312                if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
 313                        return true;
 314        }
 315
 316        return false;
 317}
 318
 319/**
 320 * ice_link_test - perform a link test on a given net_device
 321 * @netdev: network interface device structure
 322 *
 323 * This function performs one of the self-tests required by ethtool.
 324 * Returns 0 on success, non-zero on failure.
 325 */
 326static u64 ice_link_test(struct net_device *netdev)
 327{
 328        struct ice_netdev_priv *np = netdev_priv(netdev);
 329        enum ice_status status;
 330        bool link_up = false;
 331
 332        netdev_info(netdev, "link test\n");
 333        status = ice_get_link_status(np->vsi->port_info, &link_up);
 334        if (status) {
 335                netdev_err(netdev, "link query error, status = %d\n", status);
 336                return 1;
 337        }
 338
 339        if (!link_up)
 340                return 2;
 341
 342        return 0;
 343}
 344
 345/**
 346 * ice_eeprom_test - perform an EEPROM test on a given net_device
 347 * @netdev: network interface device structure
 348 *
 349 * This function performs one of the self-tests required by ethtool.
 350 * Returns 0 on success, non-zero on failure.
 351 */
 352static u64 ice_eeprom_test(struct net_device *netdev)
 353{
 354        struct ice_netdev_priv *np = netdev_priv(netdev);
 355        struct ice_pf *pf = np->vsi->back;
 356
 357        netdev_info(netdev, "EEPROM test\n");
 358        return !!(ice_nvm_validate_checksum(&pf->hw));
 359}
 360
 361/**
 362 * ice_reg_pattern_test
 363 * @hw: pointer to the HW struct
 364 * @reg: reg to be tested
 365 * @mask: bits to be touched
 366 */
 367static int ice_reg_pattern_test(struct ice_hw *hw, u32 reg, u32 mask)
 368{
 369        struct ice_pf *pf = (struct ice_pf *)hw->back;
 370        struct device *dev = ice_pf_to_dev(pf);
 371        static const u32 patterns[] = {
 372                0x5A5A5A5A, 0xA5A5A5A5,
 373                0x00000000, 0xFFFFFFFF
 374        };
 375        u32 val, orig_val;
 376        int i;
 377
 378        orig_val = rd32(hw, reg);
 379        for (i = 0; i < ARRAY_SIZE(patterns); ++i) {
 380                u32 pattern = patterns[i] & mask;
 381
 382                wr32(hw, reg, pattern);
 383                val = rd32(hw, reg);
 384                if (val == pattern)
 385                        continue;
 386                dev_err(dev, "%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n"
 387                        , __func__, reg, pattern, val);
 388                return 1;
 389        }
 390
 391        wr32(hw, reg, orig_val);
 392        val = rd32(hw, reg);
 393        if (val != orig_val) {
 394                dev_err(dev, "%s: reg restore test failed - reg 0x%08x orig 0x%08x val 0x%08x\n"
 395                        , __func__, reg, orig_val, val);
 396                return 1;
 397        }
 398
 399        return 0;
 400}
 401
 402/**
 403 * ice_reg_test - perform a register test on a given net_device
 404 * @netdev: network interface device structure
 405 *
 406 * This function performs one of the self-tests required by ethtool.
 407 * Returns 0 on success, non-zero on failure.
 408 */
 409static u64 ice_reg_test(struct net_device *netdev)
 410{
 411        struct ice_netdev_priv *np = netdev_priv(netdev);
 412        struct ice_hw *hw = np->vsi->port_info->hw;
 413        u32 int_elements = hw->func_caps.common_cap.num_msix_vectors ?
 414                hw->func_caps.common_cap.num_msix_vectors - 1 : 1;
 415        struct ice_diag_reg_test_info {
 416                u32 address;
 417                u32 mask;
 418                u32 elem_num;
 419                u32 elem_size;
 420        } ice_reg_list[] = {
 421                {GLINT_ITR(0, 0), 0x00000fff, int_elements,
 422                        GLINT_ITR(0, 1) - GLINT_ITR(0, 0)},
 423                {GLINT_ITR(1, 0), 0x00000fff, int_elements,
 424                        GLINT_ITR(1, 1) - GLINT_ITR(1, 0)},
 425                {GLINT_ITR(0, 0), 0x00000fff, int_elements,
 426                        GLINT_ITR(2, 1) - GLINT_ITR(2, 0)},
 427                {GLINT_CTL, 0xffff0001, 1, 0}
 428        };
 429        int i;
 430
 431        netdev_dbg(netdev, "Register test\n");
 432        for (i = 0; i < ARRAY_SIZE(ice_reg_list); ++i) {
 433                u32 j;
 434
 435                for (j = 0; j < ice_reg_list[i].elem_num; ++j) {
 436                        u32 mask = ice_reg_list[i].mask;
 437                        u32 reg = ice_reg_list[i].address +
 438                                (j * ice_reg_list[i].elem_size);
 439
 440                        /* bail on failure (non-zero return) */
 441                        if (ice_reg_pattern_test(hw, reg, mask))
 442                                return 1;
 443                }
 444        }
 445
 446        return 0;
 447}
 448
 449/**
 450 * ice_lbtest_prepare_rings - configure Tx/Rx test rings
 451 * @vsi: pointer to the VSI structure
 452 *
 453 * Function configures rings of a VSI for loopback test without
 454 * enabling interrupts or informing the kernel about new queues.
 455 *
 456 * Returns 0 on success, negative on failure.
 457 */
 458static int ice_lbtest_prepare_rings(struct ice_vsi *vsi)
 459{
 460        int status;
 461
 462        status = ice_vsi_setup_tx_rings(vsi);
 463        if (status)
 464                goto err_setup_tx_ring;
 465
 466        status = ice_vsi_setup_rx_rings(vsi);
 467        if (status)
 468                goto err_setup_rx_ring;
 469
 470        status = ice_vsi_cfg(vsi);
 471        if (status)
 472                goto err_setup_rx_ring;
 473
 474        status = ice_vsi_start_all_rx_rings(vsi);
 475        if (status)
 476                goto err_start_rx_ring;
 477
 478        return status;
 479
 480err_start_rx_ring:
 481        ice_vsi_free_rx_rings(vsi);
 482err_setup_rx_ring:
 483        ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
 484err_setup_tx_ring:
 485        ice_vsi_free_tx_rings(vsi);
 486
 487        return status;
 488}
 489
 490/**
 491 * ice_lbtest_disable_rings - disable Tx/Rx test rings after loopback test
 492 * @vsi: pointer to the VSI structure
 493 *
 494 * Function stops and frees VSI rings after a loopback test.
 495 * Returns 0 on success, negative on failure.
 496 */
 497static int ice_lbtest_disable_rings(struct ice_vsi *vsi)
 498{
 499        int status;
 500
 501        status = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
 502        if (status)
 503                netdev_err(vsi->netdev, "Failed to stop Tx rings, VSI %d error %d\n",
 504                           vsi->vsi_num, status);
 505
 506        status = ice_vsi_stop_all_rx_rings(vsi);
 507        if (status)
 508                netdev_err(vsi->netdev, "Failed to stop Rx rings, VSI %d error %d\n",
 509                           vsi->vsi_num, status);
 510
 511        ice_vsi_free_tx_rings(vsi);
 512        ice_vsi_free_rx_rings(vsi);
 513
 514        return status;
 515}
 516
 517/**
 518 * ice_lbtest_create_frame - create test packet
 519 * @pf: pointer to the PF structure
 520 * @ret_data: allocated frame buffer
 521 * @size: size of the packet data
 522 *
 523 * Function allocates a frame with a test pattern on specific offsets.
 524 * Returns 0 on success, non-zero on failure.
 525 */
 526static int ice_lbtest_create_frame(struct ice_pf *pf, u8 **ret_data, u16 size)
 527{
 528        u8 *data;
 529
 530        if (!pf)
 531                return -EINVAL;
 532
 533        data = devm_kzalloc(ice_pf_to_dev(pf), size, GFP_KERNEL);
 534        if (!data)
 535                return -ENOMEM;
 536
 537        /* Since the ethernet test frame should always be at least
 538         * 64 bytes long, fill some octets in the payload with test data.
 539         */
 540        memset(data, 0xFF, size);
 541        data[32] = 0xDE;
 542        data[42] = 0xAD;
 543        data[44] = 0xBE;
 544        data[46] = 0xEF;
 545
 546        *ret_data = data;
 547
 548        return 0;
 549}
 550
 551/**
 552 * ice_lbtest_check_frame - verify received loopback frame
 553 * @frame: pointer to the raw packet data
 554 *
 555 * Function verifies received test frame with a pattern.
 556 * Returns true if frame matches the pattern, false otherwise.
 557 */
 558static bool ice_lbtest_check_frame(u8 *frame)
 559{
 560        /* Validate bytes of a frame under offsets chosen earlier */
 561        if (frame[32] == 0xDE &&
 562            frame[42] == 0xAD &&
 563            frame[44] == 0xBE &&
 564            frame[46] == 0xEF &&
 565            frame[48] == 0xFF)
 566                return true;
 567
 568        return false;
 569}
 570
 571/**
 572 * ice_diag_send - send test frames to the test ring
 573 * @tx_ring: pointer to the transmit ring
 574 * @data: pointer to the raw packet data
 575 * @size: size of the packet to send
 576 *
 577 * Function sends loopback packets on a test Tx ring.
 578 */
 579static int ice_diag_send(struct ice_ring *tx_ring, u8 *data, u16 size)
 580{
 581        struct ice_tx_desc *tx_desc;
 582        struct ice_tx_buf *tx_buf;
 583        dma_addr_t dma;
 584        u64 td_cmd;
 585
 586        tx_desc = ICE_TX_DESC(tx_ring, tx_ring->next_to_use);
 587        tx_buf = &tx_ring->tx_buf[tx_ring->next_to_use];
 588
 589        dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
 590        if (dma_mapping_error(tx_ring->dev, dma))
 591                return -EINVAL;
 592
 593        tx_desc->buf_addr = cpu_to_le64(dma);
 594
 595        /* These flags are required for a descriptor to be pushed out */
 596        td_cmd = (u64)(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS);
 597        tx_desc->cmd_type_offset_bsz =
 598                cpu_to_le64(ICE_TX_DESC_DTYPE_DATA |
 599                            (td_cmd << ICE_TXD_QW1_CMD_S) |
 600                            ((u64)0 << ICE_TXD_QW1_OFFSET_S) |
 601                            ((u64)size << ICE_TXD_QW1_TX_BUF_SZ_S) |
 602                            ((u64)0 << ICE_TXD_QW1_L2TAG1_S));
 603
 604        tx_buf->next_to_watch = tx_desc;
 605
 606        /* Force memory write to complete before letting h/w know
 607         * there are new descriptors to fetch.
 608         */
 609        wmb();
 610
 611        tx_ring->next_to_use++;
 612        if (tx_ring->next_to_use >= tx_ring->count)
 613                tx_ring->next_to_use = 0;
 614
 615        writel_relaxed(tx_ring->next_to_use, tx_ring->tail);
 616
 617        /* Wait until the packets get transmitted to the receive queue. */
 618        usleep_range(1000, 2000);
 619        dma_unmap_single(tx_ring->dev, dma, size, DMA_TO_DEVICE);
 620
 621        return 0;
 622}
 623
 624#define ICE_LB_FRAME_SIZE 64
 625/**
 626 * ice_lbtest_receive_frames - receive and verify test frames
 627 * @rx_ring: pointer to the receive ring
 628 *
 629 * Function receives loopback packets and verify their correctness.
 630 * Returns number of received valid frames.
 631 */
 632static int ice_lbtest_receive_frames(struct ice_ring *rx_ring)
 633{
 634        struct ice_rx_buf *rx_buf;
 635        int valid_frames, i;
 636        u8 *received_buf;
 637
 638        valid_frames = 0;
 639
 640        for (i = 0; i < rx_ring->count; i++) {
 641                union ice_32b_rx_flex_desc *rx_desc;
 642
 643                rx_desc = ICE_RX_DESC(rx_ring, i);
 644
 645                if (!(rx_desc->wb.status_error0 &
 646                    cpu_to_le16(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS)))
 647                        continue;
 648
 649                rx_buf = &rx_ring->rx_buf[i];
 650                received_buf = page_address(rx_buf->page) + rx_buf->page_offset;
 651
 652                if (ice_lbtest_check_frame(received_buf))
 653                        valid_frames++;
 654        }
 655
 656        return valid_frames;
 657}
 658
 659/**
 660 * ice_loopback_test - perform a loopback test on a given net_device
 661 * @netdev: network interface device structure
 662 *
 663 * This function performs one of the self-tests required by ethtool.
 664 * Returns 0 on success, non-zero on failure.
 665 */
 666static u64 ice_loopback_test(struct net_device *netdev)
 667{
 668        struct ice_netdev_priv *np = netdev_priv(netdev);
 669        struct ice_vsi *orig_vsi = np->vsi, *test_vsi;
 670        struct ice_pf *pf = orig_vsi->back;
 671        struct ice_ring *tx_ring, *rx_ring;
 672        u8 broadcast[ETH_ALEN], ret = 0;
 673        int num_frames, valid_frames;
 674        LIST_HEAD(tmp_list);
 675        struct device *dev;
 676        u8 *tx_frame;
 677        int i;
 678
 679        dev = ice_pf_to_dev(pf);
 680        netdev_info(netdev, "loopback test\n");
 681
 682        test_vsi = ice_lb_vsi_setup(pf, pf->hw.port_info);
 683        if (!test_vsi) {
 684                netdev_err(netdev, "Failed to create a VSI for the loopback test\n");
 685                return 1;
 686        }
 687
 688        test_vsi->netdev = netdev;
 689        tx_ring = test_vsi->tx_rings[0];
 690        rx_ring = test_vsi->rx_rings[0];
 691
 692        if (ice_lbtest_prepare_rings(test_vsi)) {
 693                ret = 2;
 694                goto lbtest_vsi_close;
 695        }
 696
 697        if (ice_alloc_rx_bufs(rx_ring, rx_ring->count)) {
 698                ret = 3;
 699                goto lbtest_rings_dis;
 700        }
 701
 702        /* Enable MAC loopback in firmware */
 703        if (ice_aq_set_mac_loopback(&pf->hw, true, NULL)) {
 704                ret = 4;
 705                goto lbtest_mac_dis;
 706        }
 707
 708        /* Test VSI needs to receive broadcast packets */
 709        eth_broadcast_addr(broadcast);
 710        if (ice_add_mac_to_list(test_vsi, &tmp_list, broadcast)) {
 711                ret = 5;
 712                goto lbtest_mac_dis;
 713        }
 714
 715        if (ice_add_mac(&pf->hw, &tmp_list)) {
 716                ret = 6;
 717                goto free_mac_list;
 718        }
 719
 720        if (ice_lbtest_create_frame(pf, &tx_frame, ICE_LB_FRAME_SIZE)) {
 721                ret = 7;
 722                goto remove_mac_filters;
 723        }
 724
 725        num_frames = min_t(int, tx_ring->count, 32);
 726        for (i = 0; i < num_frames; i++) {
 727                if (ice_diag_send(tx_ring, tx_frame, ICE_LB_FRAME_SIZE)) {
 728                        ret = 8;
 729                        goto lbtest_free_frame;
 730                }
 731        }
 732
 733        valid_frames = ice_lbtest_receive_frames(rx_ring);
 734        if (!valid_frames)
 735                ret = 9;
 736        else if (valid_frames != num_frames)
 737                ret = 10;
 738
 739lbtest_free_frame:
 740        devm_kfree(dev, tx_frame);
 741remove_mac_filters:
 742        if (ice_remove_mac(&pf->hw, &tmp_list))
 743                netdev_err(netdev, "Could not remove MAC filter for the test VSI\n");
 744free_mac_list:
 745        ice_free_fltr_list(dev, &tmp_list);
 746lbtest_mac_dis:
 747        /* Disable MAC loopback after the test is completed. */
 748        if (ice_aq_set_mac_loopback(&pf->hw, false, NULL))
 749                netdev_err(netdev, "Could not disable MAC loopback\n");
 750lbtest_rings_dis:
 751        if (ice_lbtest_disable_rings(test_vsi))
 752                netdev_err(netdev, "Could not disable test rings\n");
 753lbtest_vsi_close:
 754        test_vsi->netdev = NULL;
 755        if (ice_vsi_release(test_vsi))
 756                netdev_err(netdev, "Failed to remove the test VSI\n");
 757
 758        return ret;
 759}
 760
 761/**
 762 * ice_intr_test - perform an interrupt test on a given net_device
 763 * @netdev: network interface device structure
 764 *
 765 * This function performs one of the self-tests required by ethtool.
 766 * Returns 0 on success, non-zero on failure.
 767 */
 768static u64 ice_intr_test(struct net_device *netdev)
 769{
 770        struct ice_netdev_priv *np = netdev_priv(netdev);
 771        struct ice_pf *pf = np->vsi->back;
 772        u16 swic_old = pf->sw_int_count;
 773
 774        netdev_info(netdev, "interrupt test\n");
 775
 776        wr32(&pf->hw, GLINT_DYN_CTL(pf->oicr_idx),
 777             GLINT_DYN_CTL_SW_ITR_INDX_M |
 778             GLINT_DYN_CTL_INTENA_MSK_M |
 779             GLINT_DYN_CTL_SWINT_TRIG_M);
 780
 781        usleep_range(1000, 2000);
 782        return (swic_old == pf->sw_int_count);
 783}
 784
 785/**
 786 * ice_self_test - handler function for performing a self-test by ethtool
 787 * @netdev: network interface device structure
 788 * @eth_test: ethtool_test structure
 789 * @data: required by ethtool.self_test
 790 *
 791 * This function is called after invoking 'ethtool -t devname' command where
 792 * devname is the name of the network device on which ethtool should operate.
 793 * It performs a set of self-tests to check if a device works properly.
 794 */
 795static void
 796ice_self_test(struct net_device *netdev, struct ethtool_test *eth_test,
 797              u64 *data)
 798{
 799        struct ice_netdev_priv *np = netdev_priv(netdev);
 800        bool if_running = netif_running(netdev);
 801        struct ice_pf *pf = np->vsi->back;
 802        struct device *dev;
 803
 804        dev = ice_pf_to_dev(pf);
 805
 806        if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
 807                netdev_info(netdev, "offline testing starting\n");
 808
 809                set_bit(__ICE_TESTING, pf->state);
 810
 811                if (ice_active_vfs(pf)) {
 812                        dev_warn(dev, "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
 813                        data[ICE_ETH_TEST_REG] = 1;
 814                        data[ICE_ETH_TEST_EEPROM] = 1;
 815                        data[ICE_ETH_TEST_INTR] = 1;
 816                        data[ICE_ETH_TEST_LOOP] = 1;
 817                        data[ICE_ETH_TEST_LINK] = 1;
 818                        eth_test->flags |= ETH_TEST_FL_FAILED;
 819                        clear_bit(__ICE_TESTING, pf->state);
 820                        goto skip_ol_tests;
 821                }
 822                /* If the device is online then take it offline */
 823                if (if_running)
 824                        /* indicate we're in test mode */
 825                        ice_stop(netdev);
 826
 827                data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
 828                data[ICE_ETH_TEST_EEPROM] = ice_eeprom_test(netdev);
 829                data[ICE_ETH_TEST_INTR] = ice_intr_test(netdev);
 830                data[ICE_ETH_TEST_LOOP] = ice_loopback_test(netdev);
 831                data[ICE_ETH_TEST_REG] = ice_reg_test(netdev);
 832
 833                if (data[ICE_ETH_TEST_LINK] ||
 834                    data[ICE_ETH_TEST_EEPROM] ||
 835                    data[ICE_ETH_TEST_LOOP] ||
 836                    data[ICE_ETH_TEST_INTR] ||
 837                    data[ICE_ETH_TEST_REG])
 838                        eth_test->flags |= ETH_TEST_FL_FAILED;
 839
 840                clear_bit(__ICE_TESTING, pf->state);
 841
 842                if (if_running) {
 843                        int status = ice_open(netdev);
 844
 845                        if (status) {
 846                                dev_err(dev, "Could not open device %s, err %d\n",
 847                                        pf->int_name, status);
 848                        }
 849                }
 850        } else {
 851                /* Online tests */
 852                netdev_info(netdev, "online testing starting\n");
 853
 854                data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
 855                if (data[ICE_ETH_TEST_LINK])
 856                        eth_test->flags |= ETH_TEST_FL_FAILED;
 857
 858                /* Offline only tests, not run in online; pass by default */
 859                data[ICE_ETH_TEST_REG] = 0;
 860                data[ICE_ETH_TEST_EEPROM] = 0;
 861                data[ICE_ETH_TEST_INTR] = 0;
 862                data[ICE_ETH_TEST_LOOP] = 0;
 863        }
 864
 865skip_ol_tests:
 866        netdev_info(netdev, "testing finished\n");
 867}
 868
 869static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
 870{
 871        struct ice_netdev_priv *np = netdev_priv(netdev);
 872        struct ice_vsi *vsi = np->vsi;
 873        char *p = (char *)data;
 874        unsigned int i;
 875
 876        switch (stringset) {
 877        case ETH_SS_STATS:
 878                for (i = 0; i < ICE_VSI_STATS_LEN; i++) {
 879                        snprintf(p, ETH_GSTRING_LEN, "%s",
 880                                 ice_gstrings_vsi_stats[i].stat_string);
 881                        p += ETH_GSTRING_LEN;
 882                }
 883
 884                ice_for_each_alloc_txq(vsi, i) {
 885                        snprintf(p, ETH_GSTRING_LEN,
 886                                 "tx_queue_%u_packets", i);
 887                        p += ETH_GSTRING_LEN;
 888                        snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_bytes", i);
 889                        p += ETH_GSTRING_LEN;
 890                }
 891
 892                ice_for_each_alloc_rxq(vsi, i) {
 893                        snprintf(p, ETH_GSTRING_LEN,
 894                                 "rx_queue_%u_packets", i);
 895                        p += ETH_GSTRING_LEN;
 896                        snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_bytes", i);
 897                        p += ETH_GSTRING_LEN;
 898                }
 899
 900                if (vsi->type != ICE_VSI_PF)
 901                        return;
 902
 903                for (i = 0; i < ICE_PF_STATS_LEN; i++) {
 904                        snprintf(p, ETH_GSTRING_LEN, "%s",
 905                                 ice_gstrings_pf_stats[i].stat_string);
 906                        p += ETH_GSTRING_LEN;
 907                }
 908
 909                for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
 910                        snprintf(p, ETH_GSTRING_LEN,
 911                                 "tx_priority_%u_xon.nic", i);
 912                        p += ETH_GSTRING_LEN;
 913                        snprintf(p, ETH_GSTRING_LEN,
 914                                 "tx_priority_%u_xoff.nic", i);
 915                        p += ETH_GSTRING_LEN;
 916                }
 917                for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
 918                        snprintf(p, ETH_GSTRING_LEN,
 919                                 "rx_priority_%u_xon.nic", i);
 920                        p += ETH_GSTRING_LEN;
 921                        snprintf(p, ETH_GSTRING_LEN,
 922                                 "rx_priority_%u_xoff.nic", i);
 923                        p += ETH_GSTRING_LEN;
 924                }
 925                break;
 926        case ETH_SS_TEST:
 927                memcpy(data, ice_gstrings_test, ICE_TEST_LEN * ETH_GSTRING_LEN);
 928                break;
 929        case ETH_SS_PRIV_FLAGS:
 930                for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
 931                        snprintf(p, ETH_GSTRING_LEN, "%s",
 932                                 ice_gstrings_priv_flags[i].name);
 933                        p += ETH_GSTRING_LEN;
 934                }
 935                break;
 936        default:
 937                break;
 938        }
 939}
 940
 941static int
 942ice_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
 943{
 944        struct ice_netdev_priv *np = netdev_priv(netdev);
 945        bool led_active;
 946
 947        switch (state) {
 948        case ETHTOOL_ID_ACTIVE:
 949                led_active = true;
 950                break;
 951        case ETHTOOL_ID_INACTIVE:
 952                led_active = false;
 953                break;
 954        default:
 955                return -EINVAL;
 956        }
 957
 958        if (ice_aq_set_port_id_led(np->vsi->port_info, !led_active, NULL))
 959                return -EIO;
 960
 961        return 0;
 962}
 963
 964/**
 965 * ice_set_fec_cfg - Set link FEC options
 966 * @netdev: network interface device structure
 967 * @req_fec: FEC mode to configure
 968 */
 969static int ice_set_fec_cfg(struct net_device *netdev, enum ice_fec_mode req_fec)
 970{
 971        struct ice_netdev_priv *np = netdev_priv(netdev);
 972        struct ice_aqc_set_phy_cfg_data config = { 0 };
 973        struct ice_aqc_get_phy_caps_data *caps;
 974        struct ice_vsi *vsi = np->vsi;
 975        u8 sw_cfg_caps, sw_cfg_fec;
 976        struct ice_port_info *pi;
 977        enum ice_status status;
 978        int err = 0;
 979
 980        pi = vsi->port_info;
 981        if (!pi)
 982                return -EOPNOTSUPP;
 983
 984        /* Changing the FEC parameters is not supported if not the PF VSI */
 985        if (vsi->type != ICE_VSI_PF) {
 986                netdev_info(netdev, "Changing FEC parameters only supported for PF VSI\n");
 987                return -EOPNOTSUPP;
 988        }
 989
 990        /* Get last SW configuration */
 991        caps = kzalloc(sizeof(*caps), GFP_KERNEL);
 992        if (!caps)
 993                return -ENOMEM;
 994
 995        status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG,
 996                                     caps, NULL);
 997        if (status) {
 998                err = -EAGAIN;
 999                goto done;
1000        }
1001
1002        /* Copy SW configuration returned from PHY caps to PHY config */
1003        ice_copy_phy_caps_to_cfg(caps, &config);
1004        sw_cfg_caps = caps->caps;
1005        sw_cfg_fec = caps->link_fec_options;
1006
1007        /* Get toloplogy caps, then copy PHY FEC topoloy caps to PHY config */
1008        memset(caps, 0, sizeof(*caps));
1009
1010        status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP,
1011                                     caps, NULL);
1012        if (status) {
1013                err = -EAGAIN;
1014                goto done;
1015        }
1016
1017        config.caps |= (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC);
1018        config.link_fec_opt = caps->link_fec_options;
1019
1020        ice_cfg_phy_fec(&config, req_fec);
1021
1022        /* If FEC mode has changed, then set PHY configuration and enable AN. */
1023        if ((config.caps & ICE_AQ_PHY_ENA_AUTO_FEC) !=
1024            (sw_cfg_caps & ICE_AQC_PHY_EN_AUTO_FEC) ||
1025            config.link_fec_opt != sw_cfg_fec) {
1026                if (caps->caps & ICE_AQC_PHY_AN_MODE)
1027                        config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1028
1029                status = ice_aq_set_phy_cfg(pi->hw, pi->lport, &config, NULL);
1030
1031                if (status)
1032                        err = -EAGAIN;
1033        }
1034
1035done:
1036        kfree(caps);
1037        return err;
1038}
1039
1040/**
1041 * ice_set_fecparam - Set FEC link options
1042 * @netdev: network interface device structure
1043 * @fecparam: Ethtool structure to retrieve FEC parameters
1044 */
1045static int
1046ice_set_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1047{
1048        struct ice_netdev_priv *np = netdev_priv(netdev);
1049        struct ice_vsi *vsi = np->vsi;
1050        enum ice_fec_mode fec;
1051
1052        switch (fecparam->fec) {
1053        case ETHTOOL_FEC_AUTO:
1054                fec = ICE_FEC_AUTO;
1055                break;
1056        case ETHTOOL_FEC_RS:
1057                fec = ICE_FEC_RS;
1058                break;
1059        case ETHTOOL_FEC_BASER:
1060                fec = ICE_FEC_BASER;
1061                break;
1062        case ETHTOOL_FEC_OFF:
1063        case ETHTOOL_FEC_NONE:
1064                fec = ICE_FEC_NONE;
1065                break;
1066        default:
1067                dev_warn(ice_pf_to_dev(vsi->back), "Unsupported FEC mode: %d\n",
1068                         fecparam->fec);
1069                return -EINVAL;
1070        }
1071
1072        return ice_set_fec_cfg(netdev, fec);
1073}
1074
1075/**
1076 * ice_get_fecparam - Get link FEC options
1077 * @netdev: network interface device structure
1078 * @fecparam: Ethtool structure to retrieve FEC parameters
1079 */
1080static int
1081ice_get_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1082{
1083        struct ice_netdev_priv *np = netdev_priv(netdev);
1084        struct ice_aqc_get_phy_caps_data *caps;
1085        struct ice_link_status *link_info;
1086        struct ice_vsi *vsi = np->vsi;
1087        struct ice_port_info *pi;
1088        enum ice_status status;
1089        int err = 0;
1090
1091        pi = vsi->port_info;
1092
1093        if (!pi)
1094                return -EOPNOTSUPP;
1095        link_info = &pi->phy.link_info;
1096
1097        /* Set FEC mode based on negotiated link info */
1098        switch (link_info->fec_info) {
1099        case ICE_AQ_LINK_25G_KR_FEC_EN:
1100                fecparam->active_fec = ETHTOOL_FEC_BASER;
1101                break;
1102        case ICE_AQ_LINK_25G_RS_528_FEC_EN:
1103        case ICE_AQ_LINK_25G_RS_544_FEC_EN:
1104                fecparam->active_fec = ETHTOOL_FEC_RS;
1105                break;
1106        default:
1107                fecparam->active_fec = ETHTOOL_FEC_OFF;
1108                break;
1109        }
1110
1111        caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1112        if (!caps)
1113                return -ENOMEM;
1114
1115        status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP,
1116                                     caps, NULL);
1117        if (status) {
1118                err = -EAGAIN;
1119                goto done;
1120        }
1121
1122        /* Set supported/configured FEC modes based on PHY capability */
1123        if (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC)
1124                fecparam->fec |= ETHTOOL_FEC_AUTO;
1125        if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
1126            caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
1127            caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN ||
1128            caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
1129                fecparam->fec |= ETHTOOL_FEC_BASER;
1130        if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
1131            caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ ||
1132            caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
1133                fecparam->fec |= ETHTOOL_FEC_RS;
1134        if (caps->link_fec_options == 0)
1135                fecparam->fec |= ETHTOOL_FEC_OFF;
1136
1137done:
1138        kfree(caps);
1139        return err;
1140}
1141
1142/**
1143 * ice_nway_reset - restart autonegotiation
1144 * @netdev: network interface device structure
1145 */
1146static int ice_nway_reset(struct net_device *netdev)
1147{
1148        struct ice_netdev_priv *np = netdev_priv(netdev);
1149        struct ice_vsi *vsi = np->vsi;
1150        struct ice_port_info *pi;
1151        enum ice_status status;
1152
1153        pi = vsi->port_info;
1154        /* If VSI state is up, then restart autoneg with link up */
1155        if (!test_bit(__ICE_DOWN, vsi->back->state))
1156                status = ice_aq_set_link_restart_an(pi, true, NULL);
1157        else
1158                status = ice_aq_set_link_restart_an(pi, false, NULL);
1159
1160        if (status) {
1161                netdev_info(netdev, "link restart failed, err %d aq_err %d\n",
1162                            status, pi->hw->adminq.sq_last_status);
1163                return -EIO;
1164        }
1165
1166        return 0;
1167}
1168
1169/**
1170 * ice_get_priv_flags - report device private flags
1171 * @netdev: network interface device structure
1172 *
1173 * The get string set count and the string set should be matched for each
1174 * flag returned.  Add new strings for each flag to the ice_gstrings_priv_flags
1175 * array.
1176 *
1177 * Returns a u32 bitmap of flags.
1178 */
1179static u32 ice_get_priv_flags(struct net_device *netdev)
1180{
1181        struct ice_netdev_priv *np = netdev_priv(netdev);
1182        struct ice_vsi *vsi = np->vsi;
1183        struct ice_pf *pf = vsi->back;
1184        u32 i, ret_flags = 0;
1185
1186        for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1187                const struct ice_priv_flag *priv_flag;
1188
1189                priv_flag = &ice_gstrings_priv_flags[i];
1190
1191                if (test_bit(priv_flag->bitno, pf->flags))
1192                        ret_flags |= BIT(i);
1193        }
1194
1195        return ret_flags;
1196}
1197
1198/**
1199 * ice_set_priv_flags - set private flags
1200 * @netdev: network interface device structure
1201 * @flags: bit flags to be set
1202 */
1203static int ice_set_priv_flags(struct net_device *netdev, u32 flags)
1204{
1205        struct ice_netdev_priv *np = netdev_priv(netdev);
1206        DECLARE_BITMAP(change_flags, ICE_PF_FLAGS_NBITS);
1207        DECLARE_BITMAP(orig_flags, ICE_PF_FLAGS_NBITS);
1208        struct ice_vsi *vsi = np->vsi;
1209        struct ice_pf *pf = vsi->back;
1210        struct device *dev;
1211        int ret = 0;
1212        u32 i;
1213
1214        if (flags > BIT(ICE_PRIV_FLAG_ARRAY_SIZE))
1215                return -EINVAL;
1216
1217        dev = ice_pf_to_dev(pf);
1218        set_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1219
1220        bitmap_copy(orig_flags, pf->flags, ICE_PF_FLAGS_NBITS);
1221        for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1222                const struct ice_priv_flag *priv_flag;
1223
1224                priv_flag = &ice_gstrings_priv_flags[i];
1225
1226                if (flags & BIT(i))
1227                        set_bit(priv_flag->bitno, pf->flags);
1228                else
1229                        clear_bit(priv_flag->bitno, pf->flags);
1230        }
1231
1232        bitmap_xor(change_flags, pf->flags, orig_flags, ICE_PF_FLAGS_NBITS);
1233
1234        if (test_bit(ICE_FLAG_FW_LLDP_AGENT, change_flags)) {
1235                if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags)) {
1236                        enum ice_status status;
1237
1238                        /* Disable FW LLDP engine */
1239                        status = ice_cfg_lldp_mib_change(&pf->hw, false);
1240
1241                        /* If unregistering for LLDP events fails, this is
1242                         * not an error state, as there shouldn't be any
1243                         * events to respond to.
1244                         */
1245                        if (status)
1246                                dev_info(dev, "Failed to unreg for LLDP events\n");
1247
1248                        /* The AQ call to stop the FW LLDP agent will generate
1249                         * an error if the agent is already stopped.
1250                         */
1251                        status = ice_aq_stop_lldp(&pf->hw, true, true, NULL);
1252                        if (status)
1253                                dev_warn(dev, "Fail to stop LLDP agent\n");
1254                        /* Use case for having the FW LLDP agent stopped
1255                         * will likely not need DCB, so failure to init is
1256                         * not a concern of ethtool
1257                         */
1258                        status = ice_init_pf_dcb(pf, true);
1259                        if (status)
1260                                dev_warn(dev, "Fail to init DCB\n");
1261                } else {
1262                        enum ice_status status;
1263                        bool dcbx_agent_status;
1264
1265                        /* AQ command to start FW LLDP agent will return an
1266                         * error if the agent is already started
1267                         */
1268                        status = ice_aq_start_lldp(&pf->hw, true, NULL);
1269                        if (status)
1270                                dev_warn(dev, "Fail to start LLDP Agent\n");
1271
1272                        /* AQ command to start FW DCBX agent will fail if
1273                         * the agent is already started
1274                         */
1275                        status = ice_aq_start_stop_dcbx(&pf->hw, true,
1276                                                        &dcbx_agent_status,
1277                                                        NULL);
1278                        if (status)
1279                                dev_dbg(dev, "Failed to start FW DCBX\n");
1280
1281                        dev_info(dev, "FW DCBX agent is %s\n",
1282                                 dcbx_agent_status ? "ACTIVE" : "DISABLED");
1283
1284                        /* Failure to configure MIB change or init DCB is not
1285                         * relevant to ethtool.  Print notification that
1286                         * registration/init failed but do not return error
1287                         * state to ethtool
1288                         */
1289                        status = ice_init_pf_dcb(pf, true);
1290                        if (status)
1291                                dev_dbg(dev, "Fail to init DCB\n");
1292
1293                        /* Remove rule to direct LLDP packets to default VSI.
1294                         * The FW LLDP engine will now be consuming them.
1295                         */
1296                        ice_cfg_sw_lldp(vsi, false, false);
1297
1298                        /* Register for MIB change events */
1299                        status = ice_cfg_lldp_mib_change(&pf->hw, true);
1300                        if (status)
1301                                dev_dbg(dev, "Fail to enable MIB change events\n");
1302
1303                        ice_nway_reset(netdev);
1304                }
1305        }
1306        if (test_bit(ICE_FLAG_LEGACY_RX, change_flags)) {
1307                /* down and up VSI so that changes of Rx cfg are reflected. */
1308                ice_down(vsi);
1309                ice_up(vsi);
1310        }
1311        clear_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1312        return ret;
1313}
1314
1315static int ice_get_sset_count(struct net_device *netdev, int sset)
1316{
1317        switch (sset) {
1318        case ETH_SS_STATS:
1319                /* The number (and order) of strings reported *must* remain
1320                 * constant for a given netdevice. This function must not
1321                 * report a different number based on run time parameters
1322                 * (such as the number of queues in use, or the setting of
1323                 * a private ethtool flag). This is due to the nature of the
1324                 * ethtool stats API.
1325                 *
1326                 * Userspace programs such as ethtool must make 3 separate
1327                 * ioctl requests, one for size, one for the strings, and
1328                 * finally one for the stats. Since these cross into
1329                 * userspace, changes to the number or size could result in
1330                 * undefined memory access or incorrect string<->value
1331                 * correlations for statistics.
1332                 *
1333                 * Even if it appears to be safe, changes to the size or
1334                 * order of strings will suffer from race conditions and are
1335                 * not safe.
1336                 */
1337                return ICE_ALL_STATS_LEN(netdev);
1338        case ETH_SS_TEST:
1339                return ICE_TEST_LEN;
1340        case ETH_SS_PRIV_FLAGS:
1341                return ICE_PRIV_FLAG_ARRAY_SIZE;
1342        default:
1343                return -EOPNOTSUPP;
1344        }
1345}
1346
1347static void
1348ice_get_ethtool_stats(struct net_device *netdev,
1349                      struct ethtool_stats __always_unused *stats, u64 *data)
1350{
1351        struct ice_netdev_priv *np = netdev_priv(netdev);
1352        struct ice_vsi *vsi = np->vsi;
1353        struct ice_pf *pf = vsi->back;
1354        struct ice_ring *ring;
1355        unsigned int j;
1356        int i = 0;
1357        char *p;
1358
1359        ice_update_pf_stats(pf);
1360        ice_update_vsi_stats(vsi);
1361
1362        for (j = 0; j < ICE_VSI_STATS_LEN; j++) {
1363                p = (char *)vsi + ice_gstrings_vsi_stats[j].stat_offset;
1364                data[i++] = (ice_gstrings_vsi_stats[j].sizeof_stat ==
1365                             sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1366        }
1367
1368        /* populate per queue stats */
1369        rcu_read_lock();
1370
1371        ice_for_each_alloc_txq(vsi, j) {
1372                ring = READ_ONCE(vsi->tx_rings[j]);
1373                if (ring) {
1374                        data[i++] = ring->stats.pkts;
1375                        data[i++] = ring->stats.bytes;
1376                } else {
1377                        data[i++] = 0;
1378                        data[i++] = 0;
1379                }
1380        }
1381
1382        ice_for_each_alloc_rxq(vsi, j) {
1383                ring = READ_ONCE(vsi->rx_rings[j]);
1384                if (ring) {
1385                        data[i++] = ring->stats.pkts;
1386                        data[i++] = ring->stats.bytes;
1387                } else {
1388                        data[i++] = 0;
1389                        data[i++] = 0;
1390                }
1391        }
1392
1393        rcu_read_unlock();
1394
1395        if (vsi->type != ICE_VSI_PF)
1396                return;
1397
1398        for (j = 0; j < ICE_PF_STATS_LEN; j++) {
1399                p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset;
1400                data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat ==
1401                             sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1402        }
1403
1404        for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1405                data[i++] = pf->stats.priority_xon_tx[j];
1406                data[i++] = pf->stats.priority_xoff_tx[j];
1407        }
1408
1409        for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1410                data[i++] = pf->stats.priority_xon_rx[j];
1411                data[i++] = pf->stats.priority_xoff_rx[j];
1412        }
1413}
1414
1415/**
1416 * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes
1417 * @netdev: network interface device structure
1418 * @ks: ethtool link ksettings struct to fill out
1419 */
1420static void
1421ice_phy_type_to_ethtool(struct net_device *netdev,
1422                        struct ethtool_link_ksettings *ks)
1423{
1424        struct ice_netdev_priv *np = netdev_priv(netdev);
1425        struct ice_link_status *hw_link_info;
1426        bool need_add_adv_mode = false;
1427        struct ice_vsi *vsi = np->vsi;
1428        u64 phy_types_high;
1429        u64 phy_types_low;
1430
1431        hw_link_info = &vsi->port_info->phy.link_info;
1432        phy_types_low = vsi->port_info->phy.phy_type_low;
1433        phy_types_high = vsi->port_info->phy.phy_type_high;
1434
1435        ethtool_link_ksettings_zero_link_mode(ks, supported);
1436        ethtool_link_ksettings_zero_link_mode(ks, advertising);
1437
1438        if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
1439            phy_types_low & ICE_PHY_TYPE_LOW_100M_SGMII) {
1440                ethtool_link_ksettings_add_link_mode(ks, supported,
1441                                                     100baseT_Full);
1442                if (!hw_link_info->req_speeds ||
1443                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100MB)
1444                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1445                                                             100baseT_Full);
1446        }
1447        if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
1448            phy_types_low & ICE_PHY_TYPE_LOW_1G_SGMII) {
1449                ethtool_link_ksettings_add_link_mode(ks, supported,
1450                                                     1000baseT_Full);
1451                if (!hw_link_info->req_speeds ||
1452                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
1453                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1454                                                             1000baseT_Full);
1455        }
1456        if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX) {
1457                ethtool_link_ksettings_add_link_mode(ks, supported,
1458                                                     1000baseKX_Full);
1459                if (!hw_link_info->req_speeds ||
1460                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
1461                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1462                                                             1000baseKX_Full);
1463        }
1464        if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_SX ||
1465            phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_LX) {
1466                ethtool_link_ksettings_add_link_mode(ks, supported,
1467                                                     1000baseX_Full);
1468                if (!hw_link_info->req_speeds ||
1469                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
1470                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1471                                                             1000baseX_Full);
1472        }
1473        if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T) {
1474                ethtool_link_ksettings_add_link_mode(ks, supported,
1475                                                     2500baseT_Full);
1476                if (!hw_link_info->req_speeds ||
1477                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
1478                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1479                                                             2500baseT_Full);
1480        }
1481        if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_X ||
1482            phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX) {
1483                ethtool_link_ksettings_add_link_mode(ks, supported,
1484                                                     2500baseX_Full);
1485                if (!hw_link_info->req_speeds ||
1486                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
1487                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1488                                                             2500baseX_Full);
1489        }
1490        if (phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
1491            phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR) {
1492                ethtool_link_ksettings_add_link_mode(ks, supported,
1493                                                     5000baseT_Full);
1494                if (!hw_link_info->req_speeds ||
1495                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_5GB)
1496                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1497                                                             5000baseT_Full);
1498        }
1499        if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
1500            phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_DA ||
1501            phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC ||
1502            phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_C2C) {
1503                ethtool_link_ksettings_add_link_mode(ks, supported,
1504                                                     10000baseT_Full);
1505                if (!hw_link_info->req_speeds ||
1506                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1507                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1508                                                             10000baseT_Full);
1509        }
1510        if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1) {
1511                ethtool_link_ksettings_add_link_mode(ks, supported,
1512                                                     10000baseKR_Full);
1513                if (!hw_link_info->req_speeds ||
1514                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1515                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1516                                                             10000baseKR_Full);
1517        }
1518        if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_SR) {
1519                ethtool_link_ksettings_add_link_mode(ks, supported,
1520                                                     10000baseSR_Full);
1521                if (!hw_link_info->req_speeds ||
1522                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1523                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1524                                                             10000baseSR_Full);
1525        }
1526        if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_LR) {
1527                ethtool_link_ksettings_add_link_mode(ks, supported,
1528                                                     10000baseLR_Full);
1529                if (!hw_link_info->req_speeds ||
1530                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1531                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1532                                                             10000baseLR_Full);
1533        }
1534        if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
1535            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
1536            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
1537            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
1538            phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC ||
1539            phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_C2C) {
1540                ethtool_link_ksettings_add_link_mode(ks, supported,
1541                                                     25000baseCR_Full);
1542                if (!hw_link_info->req_speeds ||
1543                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
1544                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1545                                                             25000baseCR_Full);
1546        }
1547        if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_SR ||
1548            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_LR) {
1549                ethtool_link_ksettings_add_link_mode(ks, supported,
1550                                                     25000baseSR_Full);
1551                if (!hw_link_info->req_speeds ||
1552                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
1553                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1554                                                             25000baseSR_Full);
1555        }
1556        if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
1557            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
1558            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1) {
1559                ethtool_link_ksettings_add_link_mode(ks, supported,
1560                                                     25000baseKR_Full);
1561                if (!hw_link_info->req_speeds ||
1562                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
1563                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1564                                                             25000baseKR_Full);
1565        }
1566        if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
1567                ethtool_link_ksettings_add_link_mode(ks, supported,
1568                                                     40000baseKR4_Full);
1569                if (!hw_link_info->req_speeds ||
1570                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1571                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1572                                                             40000baseKR4_Full);
1573        }
1574        if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
1575            phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC ||
1576            phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI) {
1577                ethtool_link_ksettings_add_link_mode(ks, supported,
1578                                                     40000baseCR4_Full);
1579                if (!hw_link_info->req_speeds ||
1580                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1581                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1582                                                             40000baseCR4_Full);
1583        }
1584        if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_SR4) {
1585                ethtool_link_ksettings_add_link_mode(ks, supported,
1586                                                     40000baseSR4_Full);
1587                if (!hw_link_info->req_speeds ||
1588                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1589                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1590                                                             40000baseSR4_Full);
1591        }
1592        if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_LR4) {
1593                ethtool_link_ksettings_add_link_mode(ks, supported,
1594                                                     40000baseLR4_Full);
1595                if (!hw_link_info->req_speeds ||
1596                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1597                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1598                                                             40000baseLR4_Full);
1599        }
1600        if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 ||
1601            phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC ||
1602            phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2 ||
1603            phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC ||
1604            phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2 ||
1605            phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP ||
1606            phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR ||
1607            phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC ||
1608            phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1) {
1609                ethtool_link_ksettings_add_link_mode(ks, supported,
1610                                                     50000baseCR2_Full);
1611                if (!hw_link_info->req_speeds ||
1612                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
1613                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1614                                                             50000baseCR2_Full);
1615        }
1616        if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 ||
1617            phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) {
1618                ethtool_link_ksettings_add_link_mode(ks, supported,
1619                                                     50000baseKR2_Full);
1620                if (!hw_link_info->req_speeds ||
1621                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
1622                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1623                                                             50000baseKR2_Full);
1624        }
1625        if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR2 ||
1626            phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR2 ||
1627            phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_FR ||
1628            phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR) {
1629                ethtool_link_ksettings_add_link_mode(ks, supported,
1630                                                     50000baseSR2_Full);
1631                if (!hw_link_info->req_speeds ||
1632                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
1633                        ethtool_link_ksettings_add_link_mode(ks, advertising,
1634                                                             50000baseSR2_Full);
1635        }
1636        if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 ||
1637            phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC ||
1638            phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4 ||
1639            phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC ||
1640            phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4 ||
1641            phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 ||
1642            phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2  ||
1643            phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC ||
1644            phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2 ||
1645            phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC ||
1646            phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2) {
1647                ethtool_link_ksettings_add_link_mode(ks, supported,
1648                                                     100000baseCR4_Full);
1649                if (!hw_link_info->req_speeds ||
1650                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1651                        need_add_adv_mode = true;
1652        }
1653        if (need_add_adv_mode) {
1654                need_add_adv_mode = false;
1655                ethtool_link_ksettings_add_link_mode(ks, advertising,
1656                                                     100000baseCR4_Full);
1657        }
1658        if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR4 ||
1659            phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR2) {
1660                ethtool_link_ksettings_add_link_mode(ks, supported,
1661                                                     100000baseSR4_Full);
1662                if (!hw_link_info->req_speeds ||
1663                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1664                        need_add_adv_mode = true;
1665        }
1666        if (need_add_adv_mode) {
1667                need_add_adv_mode = false;
1668                ethtool_link_ksettings_add_link_mode(ks, advertising,
1669                                                     100000baseSR4_Full);
1670        }
1671        if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_LR4 ||
1672            phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_DR) {
1673                ethtool_link_ksettings_add_link_mode(ks, supported,
1674                                                     100000baseLR4_ER4_Full);
1675                if (!hw_link_info->req_speeds ||
1676                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1677                        need_add_adv_mode = true;
1678        }
1679        if (need_add_adv_mode) {
1680                need_add_adv_mode = false;
1681                ethtool_link_ksettings_add_link_mode(ks, advertising,
1682                                                     100000baseLR4_ER4_Full);
1683        }
1684        if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 ||
1685            phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 ||
1686            phy_types_high & ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4) {
1687                ethtool_link_ksettings_add_link_mode(ks, supported,
1688                                                     100000baseKR4_Full);
1689                if (!hw_link_info->req_speeds ||
1690                    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1691                        need_add_adv_mode = true;
1692        }
1693        if (need_add_adv_mode)
1694                ethtool_link_ksettings_add_link_mode(ks, advertising,
1695                                                     100000baseKR4_Full);
1696
1697        /* Autoneg PHY types */
1698        if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
1699            phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
1700            phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX ||
1701            phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T ||
1702            phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX ||
1703            phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
1704            phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR ||
1705            phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
1706            phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 ||
1707            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
1708            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
1709            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
1710            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
1711            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
1712            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
1713            phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1 ||
1714            phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
1715            phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
1716                ethtool_link_ksettings_add_link_mode(ks, supported,
1717                                                     Autoneg);
1718                ethtool_link_ksettings_add_link_mode(ks, advertising,
1719                                                     Autoneg);
1720        }
1721        if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 ||
1722            phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 ||
1723            phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP ||
1724            phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) {
1725                ethtool_link_ksettings_add_link_mode(ks, supported,
1726                                                     Autoneg);
1727                ethtool_link_ksettings_add_link_mode(ks, advertising,
1728                                                     Autoneg);
1729        }
1730        if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 ||
1731            phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 ||
1732            phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 ||
1733            phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2) {
1734                ethtool_link_ksettings_add_link_mode(ks, supported,
1735                                                     Autoneg);
1736                ethtool_link_ksettings_add_link_mode(ks, advertising,
1737                                                     Autoneg);
1738        }
1739}
1740
1741#define TEST_SET_BITS_TIMEOUT   50
1742#define TEST_SET_BITS_SLEEP_MAX 2000
1743#define TEST_SET_BITS_SLEEP_MIN 1000
1744
1745/**
1746 * ice_get_settings_link_up - Get Link settings for when link is up
1747 * @ks: ethtool ksettings to fill in
1748 * @netdev: network interface device structure
1749 */
1750static void
1751ice_get_settings_link_up(struct ethtool_link_ksettings *ks,
1752                         struct net_device *netdev)
1753{
1754        struct ice_netdev_priv *np = netdev_priv(netdev);
1755        struct ice_port_info *pi = np->vsi->port_info;
1756        struct ice_link_status *link_info;
1757        struct ice_vsi *vsi = np->vsi;
1758
1759        link_info = &vsi->port_info->phy.link_info;
1760
1761        /* Get supported and advertised settings from PHY ability with media */
1762        ice_phy_type_to_ethtool(netdev, ks);
1763
1764        switch (link_info->link_speed) {
1765        case ICE_AQ_LINK_SPEED_100GB:
1766                ks->base.speed = SPEED_100000;
1767                break;
1768        case ICE_AQ_LINK_SPEED_50GB:
1769                ks->base.speed = SPEED_50000;
1770                break;
1771        case ICE_AQ_LINK_SPEED_40GB:
1772                ks->base.speed = SPEED_40000;
1773                break;
1774        case ICE_AQ_LINK_SPEED_25GB:
1775                ks->base.speed = SPEED_25000;
1776                break;
1777        case ICE_AQ_LINK_SPEED_20GB:
1778                ks->base.speed = SPEED_20000;
1779                break;
1780        case ICE_AQ_LINK_SPEED_10GB:
1781                ks->base.speed = SPEED_10000;
1782                break;
1783        case ICE_AQ_LINK_SPEED_5GB:
1784                ks->base.speed = SPEED_5000;
1785                break;
1786        case ICE_AQ_LINK_SPEED_2500MB:
1787                ks->base.speed = SPEED_2500;
1788                break;
1789        case ICE_AQ_LINK_SPEED_1000MB:
1790                ks->base.speed = SPEED_1000;
1791                break;
1792        case ICE_AQ_LINK_SPEED_100MB:
1793                ks->base.speed = SPEED_100;
1794                break;
1795        default:
1796                netdev_info(netdev, "WARNING: Unrecognized link_speed (0x%x).\n",
1797                            link_info->link_speed);
1798                break;
1799        }
1800        ks->base.duplex = DUPLEX_FULL;
1801
1802        if (link_info->an_info & ICE_AQ_AN_COMPLETED)
1803                ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
1804                                                     Autoneg);
1805
1806        /* Set flow control negotiated Rx/Tx pause */
1807        switch (pi->fc.current_mode) {
1808        case ICE_FC_FULL:
1809                ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
1810                break;
1811        case ICE_FC_TX_PAUSE:
1812                ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
1813                ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
1814                                                     Asym_Pause);
1815                break;
1816        case ICE_FC_RX_PAUSE:
1817                ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
1818                                                     Asym_Pause);
1819                break;
1820        case ICE_FC_PFC:
1821        default:
1822                ethtool_link_ksettings_del_link_mode(ks, lp_advertising, Pause);
1823                ethtool_link_ksettings_del_link_mode(ks, lp_advertising,
1824                                                     Asym_Pause);
1825                break;
1826        }
1827}
1828
1829/**
1830 * ice_get_settings_link_down - Get the Link settings when link is down
1831 * @ks: ethtool ksettings to fill in
1832 * @netdev: network interface device structure
1833 *
1834 * Reports link settings that can be determined when link is down
1835 */
1836static void
1837ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
1838                           struct net_device *netdev)
1839{
1840        /* link is down and the driver needs to fall back on
1841         * supported PHY types to figure out what info to display
1842         */
1843        ice_phy_type_to_ethtool(netdev, ks);
1844
1845        /* With no link, speed and duplex are unknown */
1846        ks->base.speed = SPEED_UNKNOWN;
1847        ks->base.duplex = DUPLEX_UNKNOWN;
1848}
1849
1850/**
1851 * ice_get_link_ksettings - Get Link Speed and Duplex settings
1852 * @netdev: network interface device structure
1853 * @ks: ethtool ksettings
1854 *
1855 * Reports speed/duplex settings based on media_type
1856 */
1857static int
1858ice_get_link_ksettings(struct net_device *netdev,
1859                       struct ethtool_link_ksettings *ks)
1860{
1861        struct ice_netdev_priv *np = netdev_priv(netdev);
1862        struct ice_aqc_get_phy_caps_data *caps;
1863        struct ice_link_status *hw_link_info;
1864        struct ice_vsi *vsi = np->vsi;
1865        enum ice_status status;
1866        int err = 0;
1867
1868        ethtool_link_ksettings_zero_link_mode(ks, supported);
1869        ethtool_link_ksettings_zero_link_mode(ks, advertising);
1870        ethtool_link_ksettings_zero_link_mode(ks, lp_advertising);
1871        hw_link_info = &vsi->port_info->phy.link_info;
1872
1873        /* set speed and duplex */
1874        if (hw_link_info->link_info & ICE_AQ_LINK_UP)
1875                ice_get_settings_link_up(ks, netdev);
1876        else
1877                ice_get_settings_link_down(ks, netdev);
1878
1879        /* set autoneg settings */
1880        ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
1881                AUTONEG_ENABLE : AUTONEG_DISABLE;
1882
1883        /* set media type settings */
1884        switch (vsi->port_info->phy.media_type) {
1885        case ICE_MEDIA_FIBER:
1886                ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1887                ks->base.port = PORT_FIBRE;
1888                break;
1889        case ICE_MEDIA_BASET:
1890                ethtool_link_ksettings_add_link_mode(ks, supported, TP);
1891                ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
1892                ks->base.port = PORT_TP;
1893                break;
1894        case ICE_MEDIA_BACKPLANE:
1895                ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1896                ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
1897                ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1898                ethtool_link_ksettings_add_link_mode(ks, advertising,
1899                                                     Backplane);
1900                ks->base.port = PORT_NONE;
1901                break;
1902        case ICE_MEDIA_DA:
1903                ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1904                ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
1905                ks->base.port = PORT_DA;
1906                break;
1907        default:
1908                ks->base.port = PORT_OTHER;
1909                break;
1910        }
1911
1912        /* flow control is symmetric and always supported */
1913        ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
1914
1915        caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1916        if (!caps)
1917                return -ENOMEM;
1918
1919        status = ice_aq_get_phy_caps(vsi->port_info, false,
1920                                     ICE_AQC_REPORT_SW_CFG, caps, NULL);
1921        if (status) {
1922                err = -EIO;
1923                goto done;
1924        }
1925
1926        /* Set the advertised flow control based on the PHY capability */
1927        if ((caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) &&
1928            (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)) {
1929                ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1930                ethtool_link_ksettings_add_link_mode(ks, advertising,
1931                                                     Asym_Pause);
1932        } else if (caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) {
1933                ethtool_link_ksettings_add_link_mode(ks, advertising,
1934                                                     Asym_Pause);
1935        } else if (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE) {
1936                ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1937                ethtool_link_ksettings_add_link_mode(ks, advertising,
1938                                                     Asym_Pause);
1939        } else {
1940                ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
1941                ethtool_link_ksettings_del_link_mode(ks, advertising,
1942                                                     Asym_Pause);
1943        }
1944
1945        /* Set advertised FEC modes based on PHY capability */
1946        ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_NONE);
1947
1948        if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
1949            caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
1950                ethtool_link_ksettings_add_link_mode(ks, advertising,
1951                                                     FEC_BASER);
1952        if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
1953            caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
1954                ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
1955
1956        status = ice_aq_get_phy_caps(vsi->port_info, false,
1957                                     ICE_AQC_REPORT_TOPO_CAP, caps, NULL);
1958        if (status) {
1959                err = -EIO;
1960                goto done;
1961        }
1962
1963        /* Set supported FEC modes based on PHY capability */
1964        ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
1965
1966        if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
1967            caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN)
1968                ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
1969        if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
1970                ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
1971
1972done:
1973        kfree(caps);
1974        return err;
1975}
1976
1977/**
1978 * ice_ksettings_find_adv_link_speed - Find advertising link speed
1979 * @ks: ethtool ksettings
1980 */
1981static u16
1982ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
1983{
1984        u16 adv_link_speed = 0;
1985
1986        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1987                                                  100baseT_Full))
1988                adv_link_speed |= ICE_AQ_LINK_SPEED_100MB;
1989        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1990                                                  1000baseX_Full))
1991                adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
1992        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1993                                                  1000baseT_Full) ||
1994            ethtool_link_ksettings_test_link_mode(ks, advertising,
1995                                                  1000baseKX_Full))
1996                adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
1997        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1998                                                  2500baseT_Full))
1999                adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2000        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2001                                                  2500baseX_Full))
2002                adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2003        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2004                                                  5000baseT_Full))
2005                adv_link_speed |= ICE_AQ_LINK_SPEED_5GB;
2006        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2007                                                  10000baseT_Full) ||
2008            ethtool_link_ksettings_test_link_mode(ks, advertising,
2009                                                  10000baseKR_Full))
2010                adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2011        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2012                                                  10000baseSR_Full) ||
2013            ethtool_link_ksettings_test_link_mode(ks, advertising,
2014                                                  10000baseLR_Full))
2015                adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2016        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2017                                                  25000baseCR_Full) ||
2018            ethtool_link_ksettings_test_link_mode(ks, advertising,
2019                                                  25000baseSR_Full) ||
2020            ethtool_link_ksettings_test_link_mode(ks, advertising,
2021                                                  25000baseKR_Full))
2022                adv_link_speed |= ICE_AQ_LINK_SPEED_25GB;
2023        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2024                                                  40000baseCR4_Full) ||
2025            ethtool_link_ksettings_test_link_mode(ks, advertising,
2026                                                  40000baseSR4_Full) ||
2027            ethtool_link_ksettings_test_link_mode(ks, advertising,
2028                                                  40000baseLR4_Full) ||
2029            ethtool_link_ksettings_test_link_mode(ks, advertising,
2030                                                  40000baseKR4_Full))
2031                adv_link_speed |= ICE_AQ_LINK_SPEED_40GB;
2032        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2033                                                  50000baseCR2_Full) ||
2034            ethtool_link_ksettings_test_link_mode(ks, advertising,
2035                                                  50000baseKR2_Full))
2036                adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2037        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2038                                                  50000baseSR2_Full))
2039                adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2040        if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2041                                                  100000baseCR4_Full) ||
2042            ethtool_link_ksettings_test_link_mode(ks, advertising,
2043                                                  100000baseSR4_Full) ||
2044            ethtool_link_ksettings_test_link_mode(ks, advertising,
2045                                                  100000baseLR4_ER4_Full) ||
2046            ethtool_link_ksettings_test_link_mode(ks, advertising,
2047                                                  100000baseKR4_Full))
2048                adv_link_speed |= ICE_AQ_LINK_SPEED_100GB;
2049
2050        return adv_link_speed;
2051}
2052
2053/**
2054 * ice_setup_autoneg
2055 * @p: port info
2056 * @ks: ethtool_link_ksettings
2057 * @config: configuration that will be sent down to FW
2058 * @autoneg_enabled: autonegotiation is enabled or not
2059 * @autoneg_changed: will there a change in autonegotiation
2060 * @netdev: network interface device structure
2061 *
2062 * Setup PHY autonegotiation feature
2063 */
2064static int
2065ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
2066                  struct ice_aqc_set_phy_cfg_data *config,
2067                  u8 autoneg_enabled, u8 *autoneg_changed,
2068                  struct net_device *netdev)
2069{
2070        int err = 0;
2071
2072        *autoneg_changed = 0;
2073
2074        /* Check autoneg */
2075        if (autoneg_enabled == AUTONEG_ENABLE) {
2076                /* If autoneg was not already enabled */
2077                if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
2078                        /* If autoneg is not supported, return error */
2079                        if (!ethtool_link_ksettings_test_link_mode(ks,
2080                                                                   supported,
2081                                                                   Autoneg)) {
2082                                netdev_info(netdev, "Autoneg not supported on this phy.\n");
2083                                err = -EINVAL;
2084                        } else {
2085                                /* Autoneg is allowed to change */
2086                                config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2087                                *autoneg_changed = 1;
2088                        }
2089                }
2090        } else {
2091                /* If autoneg is currently enabled */
2092                if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
2093                        /* If autoneg is supported 10GBASE_T is the only PHY
2094                         * that can disable it, so otherwise return error
2095                         */
2096                        if (ethtool_link_ksettings_test_link_mode(ks,
2097                                                                  supported,
2098                                                                  Autoneg)) {
2099                                netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
2100                                err = -EINVAL;
2101                        } else {
2102                                /* Autoneg is allowed to change */
2103                                config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2104                                *autoneg_changed = 1;
2105                        }
2106                }
2107        }
2108
2109        return err;
2110}
2111
2112/**
2113 * ice_set_link_ksettings - Set Speed and Duplex
2114 * @netdev: network interface device structure
2115 * @ks: ethtool ksettings
2116 *
2117 * Set speed/duplex per media_types advertised/forced
2118 */
2119static int
2120ice_set_link_ksettings(struct net_device *netdev,
2121                       const struct ethtool_link_ksettings *ks)
2122{
2123        u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT, lport = 0;
2124        struct ice_netdev_priv *np = netdev_priv(netdev);
2125        struct ethtool_link_ksettings safe_ks, copy_ks;
2126        struct ice_aqc_get_phy_caps_data *abilities;
2127        u16 adv_link_speed, curr_link_speed, idx;
2128        struct ice_aqc_set_phy_cfg_data config;
2129        struct ice_pf *pf = np->vsi->back;
2130        struct ice_port_info *p;
2131        u8 autoneg_changed = 0;
2132        enum ice_status status;
2133        u64 phy_type_high;
2134        u64 phy_type_low;
2135        int err = 0;
2136        bool linkup;
2137
2138        p = np->vsi->port_info;
2139
2140        if (!p)
2141                return -EOPNOTSUPP;
2142
2143        /* Check if this is LAN VSI */
2144        ice_for_each_vsi(pf, idx)
2145                if (pf->vsi[idx]->type == ICE_VSI_PF) {
2146                        if (np->vsi != pf->vsi[idx])
2147                                return -EOPNOTSUPP;
2148                        break;
2149                }
2150
2151        if (p->phy.media_type != ICE_MEDIA_BASET &&
2152            p->phy.media_type != ICE_MEDIA_FIBER &&
2153            p->phy.media_type != ICE_MEDIA_BACKPLANE &&
2154            p->phy.media_type != ICE_MEDIA_DA &&
2155            p->phy.link_info.link_info & ICE_AQ_LINK_UP)
2156                return -EOPNOTSUPP;
2157
2158        /* copy the ksettings to copy_ks to avoid modifying the original */
2159        memcpy(&copy_ks, ks, sizeof(copy_ks));
2160
2161        /* save autoneg out of ksettings */
2162        autoneg = copy_ks.base.autoneg;
2163
2164        memset(&safe_ks, 0, sizeof(safe_ks));
2165
2166        /* Get link modes supported by hardware.*/
2167        ice_phy_type_to_ethtool(netdev, &safe_ks);
2168
2169        /* and check against modes requested by user.
2170         * Return an error if unsupported mode was set.
2171         */
2172        if (!bitmap_subset(copy_ks.link_modes.advertising,
2173                           safe_ks.link_modes.supported,
2174                           __ETHTOOL_LINK_MODE_MASK_NBITS))
2175                return -EINVAL;
2176
2177        /* get our own copy of the bits to check against */
2178        memset(&safe_ks, 0, sizeof(safe_ks));
2179        safe_ks.base.cmd = copy_ks.base.cmd;
2180        safe_ks.base.link_mode_masks_nwords =
2181                copy_ks.base.link_mode_masks_nwords;
2182        ice_get_link_ksettings(netdev, &safe_ks);
2183
2184        /* set autoneg back to what it currently is */
2185        copy_ks.base.autoneg = safe_ks.base.autoneg;
2186        /* we don't compare the speed */
2187        copy_ks.base.speed = safe_ks.base.speed;
2188
2189        /* If copy_ks.base and safe_ks.base are not the same now, then they are
2190         * trying to set something that we do not support.
2191         */
2192        if (memcmp(&copy_ks.base, &safe_ks.base, sizeof(copy_ks.base)))
2193                return -EOPNOTSUPP;
2194
2195        while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
2196                timeout--;
2197                if (!timeout)
2198                        return -EBUSY;
2199                usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
2200        }
2201
2202        abilities = kzalloc(sizeof(*abilities), GFP_KERNEL);
2203        if (!abilities)
2204                return -ENOMEM;
2205
2206        /* Get the current PHY config */
2207        status = ice_aq_get_phy_caps(p, false, ICE_AQC_REPORT_SW_CFG, abilities,
2208                                     NULL);
2209        if (status) {
2210                err = -EAGAIN;
2211                goto done;
2212        }
2213
2214        /* Copy abilities to config in case autoneg is not set below */
2215        memset(&config, 0, sizeof(config));
2216        config.caps = abilities->caps & ~ICE_AQC_PHY_AN_MODE;
2217        if (abilities->caps & ICE_AQC_PHY_AN_MODE)
2218                config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2219
2220        /* Check autoneg */
2221        err = ice_setup_autoneg(p, &safe_ks, &config, autoneg, &autoneg_changed,
2222                                netdev);
2223
2224        if (err)
2225                goto done;
2226
2227        /* Call to get the current link speed */
2228        p->phy.get_link_info = true;
2229        status = ice_get_link_status(p, &linkup);
2230        if (status) {
2231                err = -EAGAIN;
2232                goto done;
2233        }
2234
2235        curr_link_speed = p->phy.link_info.link_speed;
2236        adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
2237
2238        /* If speed didn't get set, set it to what it currently is.
2239         * This is needed because if advertise is 0 (as it is when autoneg
2240         * is disabled) then speed won't get set.
2241         */
2242        if (!adv_link_speed)
2243                adv_link_speed = curr_link_speed;
2244
2245        /* Convert the advertise link speeds to their corresponded PHY_TYPE */
2246        ice_update_phy_type(&phy_type_low, &phy_type_high, adv_link_speed);
2247
2248        if (!autoneg_changed && adv_link_speed == curr_link_speed) {
2249                netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
2250                goto done;
2251        }
2252
2253        /* copy over the rest of the abilities */
2254        config.low_power_ctrl = abilities->low_power_ctrl;
2255        config.eee_cap = abilities->eee_cap;
2256        config.eeer_value = abilities->eeer_value;
2257        config.link_fec_opt = abilities->link_fec_options;
2258
2259        /* save the requested speeds */
2260        p->phy.link_info.req_speeds = adv_link_speed;
2261
2262        /* set link and auto negotiation so changes take effect */
2263        config.caps |= ICE_AQ_PHY_ENA_LINK;
2264
2265        if (phy_type_low || phy_type_high) {
2266                config.phy_type_high = cpu_to_le64(phy_type_high) &
2267                        abilities->phy_type_high;
2268                config.phy_type_low = cpu_to_le64(phy_type_low) &
2269                        abilities->phy_type_low;
2270        } else {
2271                err = -EAGAIN;
2272                netdev_info(netdev, "Nothing changed. No PHY_TYPE is corresponded to advertised link speed.\n");
2273                goto done;
2274        }
2275
2276        /* If link is up put link down */
2277        if (p->phy.link_info.link_info & ICE_AQ_LINK_UP) {
2278                /* Tell the OS link is going down, the link will go
2279                 * back up when fw says it is ready asynchronously
2280                 */
2281                ice_print_link_msg(np->vsi, false);
2282                netif_carrier_off(netdev);
2283                netif_tx_stop_all_queues(netdev);
2284        }
2285
2286        /* make the aq call */
2287        status = ice_aq_set_phy_cfg(&pf->hw, lport, &config, NULL);
2288        if (status) {
2289                netdev_info(netdev, "Set phy config failed,\n");
2290                err = -EAGAIN;
2291        }
2292
2293done:
2294        kfree(abilities);
2295        clear_bit(__ICE_CFG_BUSY, pf->state);
2296
2297        return err;
2298}
2299
2300/**
2301 * ice_parse_hdrs - parses headers from RSS hash input
2302 * @nfc: ethtool rxnfc command
2303 *
2304 * This function parses the rxnfc command and returns intended
2305 * header types for RSS configuration
2306 */
2307static u32 ice_parse_hdrs(struct ethtool_rxnfc *nfc)
2308{
2309        u32 hdrs = ICE_FLOW_SEG_HDR_NONE;
2310
2311        switch (nfc->flow_type) {
2312        case TCP_V4_FLOW:
2313                hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4;
2314                break;
2315        case UDP_V4_FLOW:
2316                hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4;
2317                break;
2318        case SCTP_V4_FLOW:
2319                hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4;
2320                break;
2321        case TCP_V6_FLOW:
2322                hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6;
2323                break;
2324        case UDP_V6_FLOW:
2325                hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6;
2326                break;
2327        case SCTP_V6_FLOW:
2328                hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6;
2329                break;
2330        default:
2331                break;
2332        }
2333        return hdrs;
2334}
2335
2336#define ICE_FLOW_HASH_FLD_IPV4_SA       BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)
2337#define ICE_FLOW_HASH_FLD_IPV6_SA       BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)
2338#define ICE_FLOW_HASH_FLD_IPV4_DA       BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)
2339#define ICE_FLOW_HASH_FLD_IPV6_DA       BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)
2340#define ICE_FLOW_HASH_FLD_TCP_SRC_PORT  BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)
2341#define ICE_FLOW_HASH_FLD_TCP_DST_PORT  BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)
2342#define ICE_FLOW_HASH_FLD_UDP_SRC_PORT  BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)
2343#define ICE_FLOW_HASH_FLD_UDP_DST_PORT  BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)
2344#define ICE_FLOW_HASH_FLD_SCTP_SRC_PORT \
2345        BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)
2346#define ICE_FLOW_HASH_FLD_SCTP_DST_PORT \
2347        BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)
2348
2349/**
2350 * ice_parse_hash_flds - parses hash fields from RSS hash input
2351 * @nfc: ethtool rxnfc command
2352 *
2353 * This function parses the rxnfc command and returns intended
2354 * hash fields for RSS configuration
2355 */
2356static u64 ice_parse_hash_flds(struct ethtool_rxnfc *nfc)
2357{
2358        u64 hfld = ICE_HASH_INVALID;
2359
2360        if (nfc->data & RXH_IP_SRC || nfc->data & RXH_IP_DST) {
2361                switch (nfc->flow_type) {
2362                case TCP_V4_FLOW:
2363                case UDP_V4_FLOW:
2364                case SCTP_V4_FLOW:
2365                        if (nfc->data & RXH_IP_SRC)
2366                                hfld |= ICE_FLOW_HASH_FLD_IPV4_SA;
2367                        if (nfc->data & RXH_IP_DST)
2368                                hfld |= ICE_FLOW_HASH_FLD_IPV4_DA;
2369                        break;
2370                case TCP_V6_FLOW:
2371                case UDP_V6_FLOW:
2372                case SCTP_V6_FLOW:
2373                        if (nfc->data & RXH_IP_SRC)
2374                                hfld |= ICE_FLOW_HASH_FLD_IPV6_SA;
2375                        if (nfc->data & RXH_IP_DST)
2376                                hfld |= ICE_FLOW_HASH_FLD_IPV6_DA;
2377                        break;
2378                default:
2379                        break;
2380                }
2381        }
2382
2383        if (nfc->data & RXH_L4_B_0_1 || nfc->data & RXH_L4_B_2_3) {
2384                switch (nfc->flow_type) {
2385                case TCP_V4_FLOW:
2386                case TCP_V6_FLOW:
2387                        if (nfc->data & RXH_L4_B_0_1)
2388                                hfld |= ICE_FLOW_HASH_FLD_TCP_SRC_PORT;
2389                        if (nfc->data & RXH_L4_B_2_3)
2390                                hfld |= ICE_FLOW_HASH_FLD_TCP_DST_PORT;
2391                        break;
2392                case UDP_V4_FLOW:
2393                case UDP_V6_FLOW:
2394                        if (nfc->data & RXH_L4_B_0_1)
2395                                hfld |= ICE_FLOW_HASH_FLD_UDP_SRC_PORT;
2396                        if (nfc->data & RXH_L4_B_2_3)
2397                                hfld |= ICE_FLOW_HASH_FLD_UDP_DST_PORT;
2398                        break;
2399                case SCTP_V4_FLOW:
2400                case SCTP_V6_FLOW:
2401                        if (nfc->data & RXH_L4_B_0_1)
2402                                hfld |= ICE_FLOW_HASH_FLD_SCTP_SRC_PORT;
2403                        if (nfc->data & RXH_L4_B_2_3)
2404                                hfld |= ICE_FLOW_HASH_FLD_SCTP_DST_PORT;
2405                        break;
2406                default:
2407                        break;
2408                }
2409        }
2410
2411        return hfld;
2412}
2413
2414/**
2415 * ice_set_rss_hash_opt - Enable/Disable flow types for RSS hash
2416 * @vsi: the VSI being configured
2417 * @nfc: ethtool rxnfc command
2418 *
2419 * Returns Success if the flow input set is supported.
2420 */
2421static int
2422ice_set_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
2423{
2424        struct ice_pf *pf = vsi->back;
2425        enum ice_status status;
2426        struct device *dev;
2427        u64 hashed_flds;
2428        u32 hdrs;
2429
2430        dev = ice_pf_to_dev(pf);
2431        if (ice_is_safe_mode(pf)) {
2432                dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
2433                        vsi->vsi_num);
2434                return -EINVAL;
2435        }
2436
2437        hashed_flds = ice_parse_hash_flds(nfc);
2438        if (hashed_flds == ICE_HASH_INVALID) {
2439                dev_dbg(dev, "Invalid hash fields, vsi num = %d\n",
2440                        vsi->vsi_num);
2441                return -EINVAL;
2442        }
2443
2444        hdrs = ice_parse_hdrs(nfc);
2445        if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
2446                dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
2447                        vsi->vsi_num);
2448                return -EINVAL;
2449        }
2450
2451        status = ice_add_rss_cfg(&pf->hw, vsi->idx, hashed_flds, hdrs);
2452        if (status) {
2453                dev_dbg(dev, "ice_add_rss_cfg failed, vsi num = %d, error = %d\n",
2454                        vsi->vsi_num, status);
2455                return -EINVAL;
2456        }
2457
2458        return 0;
2459}
2460
2461/**
2462 * ice_get_rss_hash_opt - Retrieve hash fields for a given flow-type
2463 * @vsi: the VSI being configured
2464 * @nfc: ethtool rxnfc command
2465 */
2466static void
2467ice_get_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
2468{
2469        struct ice_pf *pf = vsi->back;
2470        struct device *dev;
2471        u64 hash_flds;
2472        u32 hdrs;
2473
2474        dev = ice_pf_to_dev(pf);
2475
2476        nfc->data = 0;
2477        if (ice_is_safe_mode(pf)) {
2478                dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
2479                        vsi->vsi_num);
2480                return;
2481        }
2482
2483        hdrs = ice_parse_hdrs(nfc);
2484        if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
2485                dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
2486                        vsi->vsi_num);
2487                return;
2488        }
2489
2490        hash_flds = ice_get_rss_cfg(&pf->hw, vsi->idx, hdrs);
2491        if (hash_flds == ICE_HASH_INVALID) {
2492                dev_dbg(dev, "No hash fields found for the given header type, vsi num = %d\n",
2493                        vsi->vsi_num);
2494                return;
2495        }
2496
2497        if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_SA ||
2498            hash_flds & ICE_FLOW_HASH_FLD_IPV6_SA)
2499                nfc->data |= (u64)RXH_IP_SRC;
2500
2501        if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_DA ||
2502            hash_flds & ICE_FLOW_HASH_FLD_IPV6_DA)
2503                nfc->data |= (u64)RXH_IP_DST;
2504
2505        if (hash_flds & ICE_FLOW_HASH_FLD_TCP_SRC_PORT ||
2506            hash_flds & ICE_FLOW_HASH_FLD_UDP_SRC_PORT ||
2507            hash_flds & ICE_FLOW_HASH_FLD_SCTP_SRC_PORT)
2508                nfc->data |= (u64)RXH_L4_B_0_1;
2509
2510        if (hash_flds & ICE_FLOW_HASH_FLD_TCP_DST_PORT ||
2511            hash_flds & ICE_FLOW_HASH_FLD_UDP_DST_PORT ||
2512            hash_flds & ICE_FLOW_HASH_FLD_SCTP_DST_PORT)
2513                nfc->data |= (u64)RXH_L4_B_2_3;
2514}
2515
2516/**
2517 * ice_set_rxnfc - command to set Rx flow rules.
2518 * @netdev: network interface device structure
2519 * @cmd: ethtool rxnfc command
2520 *
2521 * Returns 0 for success and negative values for errors
2522 */
2523static int ice_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
2524{
2525        struct ice_netdev_priv *np = netdev_priv(netdev);
2526        struct ice_vsi *vsi = np->vsi;
2527
2528        switch (cmd->cmd) {
2529        case ETHTOOL_SRXFH:
2530                return ice_set_rss_hash_opt(vsi, cmd);
2531        default:
2532                break;
2533        }
2534        return -EOPNOTSUPP;
2535}
2536
2537/**
2538 * ice_get_rxnfc - command to get Rx flow classification rules
2539 * @netdev: network interface device structure
2540 * @cmd: ethtool rxnfc command
2541 * @rule_locs: buffer to rturn Rx flow classification rules
2542 *
2543 * Returns Success if the command is supported.
2544 */
2545static int
2546ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2547              u32 __always_unused *rule_locs)
2548{
2549        struct ice_netdev_priv *np = netdev_priv(netdev);
2550        struct ice_vsi *vsi = np->vsi;
2551        int ret = -EOPNOTSUPP;
2552
2553        switch (cmd->cmd) {
2554        case ETHTOOL_GRXRINGS:
2555                cmd->data = vsi->rss_size;
2556                ret = 0;
2557                break;
2558        case ETHTOOL_GRXFH:
2559                ice_get_rss_hash_opt(vsi, cmd);
2560                ret = 0;
2561                break;
2562        default:
2563                break;
2564        }
2565
2566        return ret;
2567}
2568
2569static void
2570ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2571{
2572        struct ice_netdev_priv *np = netdev_priv(netdev);
2573        struct ice_vsi *vsi = np->vsi;
2574
2575        ring->rx_max_pending = ICE_MAX_NUM_DESC;
2576        ring->tx_max_pending = ICE_MAX_NUM_DESC;
2577        ring->rx_pending = vsi->rx_rings[0]->count;
2578        ring->tx_pending = vsi->tx_rings[0]->count;
2579
2580        /* Rx mini and jumbo rings are not supported */
2581        ring->rx_mini_max_pending = 0;
2582        ring->rx_jumbo_max_pending = 0;
2583        ring->rx_mini_pending = 0;
2584        ring->rx_jumbo_pending = 0;
2585}
2586
2587static int
2588ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2589{
2590        struct ice_ring *tx_rings = NULL, *rx_rings = NULL;
2591        struct ice_netdev_priv *np = netdev_priv(netdev);
2592        struct ice_ring *xdp_rings = NULL;
2593        struct ice_vsi *vsi = np->vsi;
2594        struct ice_pf *pf = vsi->back;
2595        int i, timeout = 50, err = 0;
2596        u32 new_rx_cnt, new_tx_cnt;
2597
2598        if (ring->tx_pending > ICE_MAX_NUM_DESC ||
2599            ring->tx_pending < ICE_MIN_NUM_DESC ||
2600            ring->rx_pending > ICE_MAX_NUM_DESC ||
2601            ring->rx_pending < ICE_MIN_NUM_DESC) {
2602                netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
2603                           ring->tx_pending, ring->rx_pending,
2604                           ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
2605                           ICE_REQ_DESC_MULTIPLE);
2606                return -EINVAL;
2607        }
2608
2609        new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
2610        if (new_tx_cnt != ring->tx_pending)
2611                netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n",
2612                            new_tx_cnt);
2613        new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
2614        if (new_rx_cnt != ring->rx_pending)
2615                netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n",
2616                            new_rx_cnt);
2617
2618        /* if nothing to do return success */
2619        if (new_tx_cnt == vsi->tx_rings[0]->count &&
2620            new_rx_cnt == vsi->rx_rings[0]->count) {
2621                netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
2622                return 0;
2623        }
2624
2625        /* If there is a AF_XDP UMEM attached to any of Rx rings,
2626         * disallow changing the number of descriptors -- regardless
2627         * if the netdev is running or not.
2628         */
2629        if (ice_xsk_any_rx_ring_ena(vsi))
2630                return -EBUSY;
2631
2632        while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
2633                timeout--;
2634                if (!timeout)
2635                        return -EBUSY;
2636                usleep_range(1000, 2000);
2637        }
2638
2639        /* set for the next time the netdev is started */
2640        if (!netif_running(vsi->netdev)) {
2641                for (i = 0; i < vsi->alloc_txq; i++)
2642                        vsi->tx_rings[i]->count = new_tx_cnt;
2643                for (i = 0; i < vsi->alloc_rxq; i++)
2644                        vsi->rx_rings[i]->count = new_rx_cnt;
2645                if (ice_is_xdp_ena_vsi(vsi))
2646                        for (i = 0; i < vsi->num_xdp_txq; i++)
2647                                vsi->xdp_rings[i]->count = new_tx_cnt;
2648                vsi->num_tx_desc = new_tx_cnt;
2649                vsi->num_rx_desc = new_rx_cnt;
2650                netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
2651                goto done;
2652        }
2653
2654        if (new_tx_cnt == vsi->tx_rings[0]->count)
2655                goto process_rx;
2656
2657        /* alloc updated Tx resources */
2658        netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
2659                    vsi->tx_rings[0]->count, new_tx_cnt);
2660
2661        tx_rings = kcalloc(vsi->num_txq, sizeof(*tx_rings), GFP_KERNEL);
2662        if (!tx_rings) {
2663                err = -ENOMEM;
2664                goto done;
2665        }
2666
2667        ice_for_each_txq(vsi, i) {
2668                /* clone ring and setup updated count */
2669                tx_rings[i] = *vsi->tx_rings[i];
2670                tx_rings[i].count = new_tx_cnt;
2671                tx_rings[i].desc = NULL;
2672                tx_rings[i].tx_buf = NULL;
2673                err = ice_setup_tx_ring(&tx_rings[i]);
2674                if (err) {
2675                        while (i--)
2676                                ice_clean_tx_ring(&tx_rings[i]);
2677                        kfree(tx_rings);
2678                        goto done;
2679                }
2680        }
2681
2682        if (!ice_is_xdp_ena_vsi(vsi))
2683                goto process_rx;
2684
2685        /* alloc updated XDP resources */
2686        netdev_info(netdev, "Changing XDP descriptor count from %d to %d\n",
2687                    vsi->xdp_rings[0]->count, new_tx_cnt);
2688
2689        xdp_rings = kcalloc(vsi->num_xdp_txq, sizeof(*xdp_rings), GFP_KERNEL);
2690        if (!xdp_rings) {
2691                err = -ENOMEM;
2692                goto free_tx;
2693        }
2694
2695        for (i = 0; i < vsi->num_xdp_txq; i++) {
2696                /* clone ring and setup updated count */
2697                xdp_rings[i] = *vsi->xdp_rings[i];
2698                xdp_rings[i].count = new_tx_cnt;
2699                xdp_rings[i].desc = NULL;
2700                xdp_rings[i].tx_buf = NULL;
2701                err = ice_setup_tx_ring(&xdp_rings[i]);
2702                if (err) {
2703                        while (i--)
2704                                ice_clean_tx_ring(&xdp_rings[i]);
2705                        kfree(xdp_rings);
2706                        goto free_tx;
2707                }
2708                ice_set_ring_xdp(&xdp_rings[i]);
2709        }
2710
2711process_rx:
2712        if (new_rx_cnt == vsi->rx_rings[0]->count)
2713                goto process_link;
2714
2715        /* alloc updated Rx resources */
2716        netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
2717                    vsi->rx_rings[0]->count, new_rx_cnt);
2718
2719        rx_rings = kcalloc(vsi->num_rxq, sizeof(*rx_rings), GFP_KERNEL);
2720        if (!rx_rings) {
2721                err = -ENOMEM;
2722                goto done;
2723        }
2724
2725        ice_for_each_rxq(vsi, i) {
2726                /* clone ring and setup updated count */
2727                rx_rings[i] = *vsi->rx_rings[i];
2728                rx_rings[i].count = new_rx_cnt;
2729                rx_rings[i].desc = NULL;
2730                rx_rings[i].rx_buf = NULL;
2731                /* this is to allow wr32 to have something to write to
2732                 * during early allocation of Rx buffers
2733                 */
2734                rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
2735
2736                err = ice_setup_rx_ring(&rx_rings[i]);
2737                if (err)
2738                        goto rx_unwind;
2739
2740                /* allocate Rx buffers */
2741                err = ice_alloc_rx_bufs(&rx_rings[i],
2742                                        ICE_DESC_UNUSED(&rx_rings[i]));
2743rx_unwind:
2744                if (err) {
2745                        while (i) {
2746                                i--;
2747                                ice_free_rx_ring(&rx_rings[i]);
2748                        }
2749                        kfree(rx_rings);
2750                        err = -ENOMEM;
2751                        goto free_tx;
2752                }
2753        }
2754
2755process_link:
2756        /* Bring interface down, copy in the new ring info, then restore the
2757         * interface. if VSI is up, bring it down and then back up
2758         */
2759        if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
2760                ice_down(vsi);
2761
2762                if (tx_rings) {
2763                        ice_for_each_txq(vsi, i) {
2764                                ice_free_tx_ring(vsi->tx_rings[i]);
2765                                *vsi->tx_rings[i] = tx_rings[i];
2766                        }
2767                        kfree(tx_rings);
2768                }
2769
2770                if (rx_rings) {
2771                        ice_for_each_rxq(vsi, i) {
2772                                ice_free_rx_ring(vsi->rx_rings[i]);
2773                                /* copy the real tail offset */
2774                                rx_rings[i].tail = vsi->rx_rings[i]->tail;
2775                                /* this is to fake out the allocation routine
2776                                 * into thinking it has to realloc everything
2777                                 * but the recycling logic will let us re-use
2778                                 * the buffers allocated above
2779                                 */
2780                                rx_rings[i].next_to_use = 0;
2781                                rx_rings[i].next_to_clean = 0;
2782                                rx_rings[i].next_to_alloc = 0;
2783                                *vsi->rx_rings[i] = rx_rings[i];
2784                        }
2785                        kfree(rx_rings);
2786                }
2787
2788                if (xdp_rings) {
2789                        for (i = 0; i < vsi->num_xdp_txq; i++) {
2790                                ice_free_tx_ring(vsi->xdp_rings[i]);
2791                                *vsi->xdp_rings[i] = xdp_rings[i];
2792                        }
2793                        kfree(xdp_rings);
2794                }
2795
2796                vsi->num_tx_desc = new_tx_cnt;
2797                vsi->num_rx_desc = new_rx_cnt;
2798                ice_up(vsi);
2799        }
2800        goto done;
2801
2802free_tx:
2803        /* error cleanup if the Rx allocations failed after getting Tx */
2804        if (tx_rings) {
2805                ice_for_each_txq(vsi, i)
2806                        ice_free_tx_ring(&tx_rings[i]);
2807                kfree(tx_rings);
2808        }
2809
2810done:
2811        clear_bit(__ICE_CFG_BUSY, pf->state);
2812        return err;
2813}
2814
2815/**
2816 * ice_get_pauseparam - Get Flow Control status
2817 * @netdev: network interface device structure
2818 * @pause: ethernet pause (flow control) parameters
2819 *
2820 * Get requested flow control status from PHY capability.
2821 * If autoneg is true, then ethtool will send the ETHTOOL_GSET ioctl which
2822 * is handled by ice_get_link_ksettings. ice_get_link_ksettings will report
2823 * the negotiated Rx/Tx pause via lp_advertising.
2824 */
2825static void
2826ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2827{
2828        struct ice_netdev_priv *np = netdev_priv(netdev);
2829        struct ice_port_info *pi = np->vsi->port_info;
2830        struct ice_aqc_get_phy_caps_data *pcaps;
2831        struct ice_dcbx_cfg *dcbx_cfg;
2832        enum ice_status status;
2833
2834        /* Initialize pause params */
2835        pause->rx_pause = 0;
2836        pause->tx_pause = 0;
2837
2838        dcbx_cfg = &pi->local_dcbx_cfg;
2839
2840        pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2841        if (!pcaps)
2842                return;
2843
2844        /* Get current PHY config */
2845        status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
2846                                     NULL);
2847        if (status)
2848                goto out;
2849
2850        pause->autoneg = ((pcaps->caps & ICE_AQC_PHY_AN_MODE) ?
2851                        AUTONEG_ENABLE : AUTONEG_DISABLE);
2852
2853        if (dcbx_cfg->pfc.pfcena)
2854                /* PFC enabled so report LFC as off */
2855                goto out;
2856
2857        if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
2858                pause->tx_pause = 1;
2859        if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
2860                pause->rx_pause = 1;
2861
2862out:
2863        kfree(pcaps);
2864}
2865
2866/**
2867 * ice_set_pauseparam - Set Flow Control parameter
2868 * @netdev: network interface device structure
2869 * @pause: return Tx/Rx flow control status
2870 */
2871static int
2872ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2873{
2874        struct ice_netdev_priv *np = netdev_priv(netdev);
2875        struct ice_aqc_get_phy_caps_data *pcaps;
2876        struct ice_link_status *hw_link_info;
2877        struct ice_pf *pf = np->vsi->back;
2878        struct ice_dcbx_cfg *dcbx_cfg;
2879        struct ice_vsi *vsi = np->vsi;
2880        struct ice_hw *hw = &pf->hw;
2881        struct ice_port_info *pi;
2882        enum ice_status status;
2883        u8 aq_failures;
2884        bool link_up;
2885        int err = 0;
2886        u32 is_an;
2887
2888        pi = vsi->port_info;
2889        hw_link_info = &pi->phy.link_info;
2890        dcbx_cfg = &pi->local_dcbx_cfg;
2891        link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
2892
2893        /* Changing the port's flow control is not supported if this isn't the
2894         * PF VSI
2895         */
2896        if (vsi->type != ICE_VSI_PF) {
2897                netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
2898                return -EOPNOTSUPP;
2899        }
2900
2901        /* Get pause param reports configured and negotiated flow control pause
2902         * when ETHTOOL_GLINKSETTINGS is defined. Since ETHTOOL_GLINKSETTINGS is
2903         * defined get pause param pause->autoneg reports SW configured setting,
2904         * so compare pause->autoneg with SW configured to prevent the user from
2905         * using set pause param to chance autoneg.
2906         */
2907        pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2908        if (!pcaps)
2909                return -ENOMEM;
2910
2911        /* Get current PHY config */
2912        status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
2913                                     NULL);
2914        if (status) {
2915                kfree(pcaps);
2916                return -EIO;
2917        }
2918
2919        is_an = ((pcaps->caps & ICE_AQC_PHY_AN_MODE) ?
2920                        AUTONEG_ENABLE : AUTONEG_DISABLE);
2921
2922        kfree(pcaps);
2923
2924        if (pause->autoneg != is_an) {
2925                netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
2926                return -EOPNOTSUPP;
2927        }
2928
2929        /* If we have link and don't have autoneg */
2930        if (!test_bit(__ICE_DOWN, pf->state) &&
2931            !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
2932                /* Send message that it might not necessarily work*/
2933                netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
2934        }
2935
2936        if (dcbx_cfg->pfc.pfcena) {
2937                netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
2938                return -EOPNOTSUPP;
2939        }
2940        if (pause->rx_pause && pause->tx_pause)
2941                pi->fc.req_mode = ICE_FC_FULL;
2942        else if (pause->rx_pause && !pause->tx_pause)
2943                pi->fc.req_mode = ICE_FC_RX_PAUSE;
2944        else if (!pause->rx_pause && pause->tx_pause)
2945                pi->fc.req_mode = ICE_FC_TX_PAUSE;
2946        else if (!pause->rx_pause && !pause->tx_pause)
2947                pi->fc.req_mode = ICE_FC_NONE;
2948        else
2949                return -EINVAL;
2950
2951        /* Set the FC mode and only restart AN if link is up */
2952        status = ice_set_fc(pi, &aq_failures, link_up);
2953
2954        if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
2955                netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %d\n",
2956                            status, hw->adminq.sq_last_status);
2957                err = -EAGAIN;
2958        } else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
2959                netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %d\n",
2960                            status, hw->adminq.sq_last_status);
2961                err = -EAGAIN;
2962        } else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
2963                netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %d\n",
2964                            status, hw->adminq.sq_last_status);
2965                err = -EAGAIN;
2966        }
2967
2968        if (!test_bit(__ICE_DOWN, pf->state)) {
2969                /* Give it a little more time to try to come back. If still
2970                 * down, restart autoneg link or reinitialize the interface.
2971                 */
2972                msleep(75);
2973                if (!test_bit(__ICE_DOWN, pf->state))
2974                        return ice_nway_reset(netdev);
2975
2976                ice_down(vsi);
2977                ice_up(vsi);
2978        }
2979
2980        return err;
2981}
2982
2983/**
2984 * ice_get_rxfh_key_size - get the RSS hash key size
2985 * @netdev: network interface device structure
2986 *
2987 * Returns the table size.
2988 */
2989static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
2990{
2991        return ICE_VSIQF_HKEY_ARRAY_SIZE;
2992}
2993
2994/**
2995 * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
2996 * @netdev: network interface device structure
2997 *
2998 * Returns the table size.
2999 */
3000static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
3001{
3002        struct ice_netdev_priv *np = netdev_priv(netdev);
3003
3004        return np->vsi->rss_table_size;
3005}
3006
3007/**
3008 * ice_get_rxfh - get the Rx flow hash indirection table
3009 * @netdev: network interface device structure
3010 * @indir: indirection table
3011 * @key: hash key
3012 * @hfunc: hash function
3013 *
3014 * Reads the indirection table directly from the hardware.
3015 */
3016static int
3017ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
3018{
3019        struct ice_netdev_priv *np = netdev_priv(netdev);
3020        struct ice_vsi *vsi = np->vsi;
3021        struct ice_pf *pf = vsi->back;
3022        int ret = 0, i;
3023        u8 *lut;
3024
3025        if (hfunc)
3026                *hfunc = ETH_RSS_HASH_TOP;
3027
3028        if (!indir)
3029                return 0;
3030
3031        if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3032                /* RSS not supported return error here */
3033                netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3034                return -EIO;
3035        }
3036
3037        lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3038        if (!lut)
3039                return -ENOMEM;
3040
3041        if (ice_get_rss(vsi, key, lut, vsi->rss_table_size)) {
3042                ret = -EIO;
3043                goto out;
3044        }
3045
3046        for (i = 0; i < vsi->rss_table_size; i++)
3047                indir[i] = (u32)(lut[i]);
3048
3049out:
3050        kfree(lut);
3051        return ret;
3052}
3053
3054/**
3055 * ice_set_rxfh - set the Rx flow hash indirection table
3056 * @netdev: network interface device structure
3057 * @indir: indirection table
3058 * @key: hash key
3059 * @hfunc: hash function
3060 *
3061 * Returns -EINVAL if the table specifies an invalid queue ID, otherwise
3062 * returns 0 after programming the table.
3063 */
3064static int
3065ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key,
3066             const u8 hfunc)
3067{
3068        struct ice_netdev_priv *np = netdev_priv(netdev);
3069        struct ice_vsi *vsi = np->vsi;
3070        struct ice_pf *pf = vsi->back;
3071        struct device *dev;
3072        u8 *seed = NULL;
3073
3074        dev = ice_pf_to_dev(pf);
3075        if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3076                return -EOPNOTSUPP;
3077
3078        if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3079                /* RSS not supported return error here */
3080                netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3081                return -EIO;
3082        }
3083
3084        if (key) {
3085                if (!vsi->rss_hkey_user) {
3086                        vsi->rss_hkey_user =
3087                                devm_kzalloc(dev, ICE_VSIQF_HKEY_ARRAY_SIZE,
3088                                             GFP_KERNEL);
3089                        if (!vsi->rss_hkey_user)
3090                                return -ENOMEM;
3091                }
3092                memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
3093                seed = vsi->rss_hkey_user;
3094        }
3095
3096        if (!vsi->rss_lut_user) {
3097                vsi->rss_lut_user = devm_kzalloc(dev, vsi->rss_table_size,
3098                                                 GFP_KERNEL);
3099                if (!vsi->rss_lut_user)
3100                        return -ENOMEM;
3101        }
3102
3103        /* Each 32 bits pointed by 'indir' is stored with a lut entry */
3104        if (indir) {
3105                int i;
3106
3107                for (i = 0; i < vsi->rss_table_size; i++)
3108                        vsi->rss_lut_user[i] = (u8)(indir[i]);
3109        } else {
3110                ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
3111                                 vsi->rss_size);
3112        }
3113
3114        if (ice_set_rss(vsi, seed, vsi->rss_lut_user, vsi->rss_table_size))
3115                return -EIO;
3116
3117        return 0;
3118}
3119
3120/**
3121 * ice_get_max_txq - return the maximum number of Tx queues for in a PF
3122 * @pf: PF structure
3123 */
3124static int ice_get_max_txq(struct ice_pf *pf)
3125{
3126        return min_t(int, num_online_cpus(),
3127                     pf->hw.func_caps.common_cap.num_txq);
3128}
3129
3130/**
3131 * ice_get_max_rxq - return the maximum number of Rx queues for in a PF
3132 * @pf: PF structure
3133 */
3134static int ice_get_max_rxq(struct ice_pf *pf)
3135{
3136        return min_t(int, num_online_cpus(),
3137                     pf->hw.func_caps.common_cap.num_rxq);
3138}
3139
3140/**
3141 * ice_get_combined_cnt - return the current number of combined channels
3142 * @vsi: PF VSI pointer
3143 *
3144 * Go through all queue vectors and count ones that have both Rx and Tx ring
3145 * attached
3146 */
3147static u32 ice_get_combined_cnt(struct ice_vsi *vsi)
3148{
3149        u32 combined = 0;
3150        int q_idx;
3151
3152        ice_for_each_q_vector(vsi, q_idx) {
3153                struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3154
3155                if (q_vector->rx.ring && q_vector->tx.ring)
3156                        combined++;
3157        }
3158
3159        return combined;
3160}
3161
3162/**
3163 * ice_get_channels - get the current and max supported channels
3164 * @dev: network interface device structure
3165 * @ch: ethtool channel data structure
3166 */
3167static void
3168ice_get_channels(struct net_device *dev, struct ethtool_channels *ch)
3169{
3170        struct ice_netdev_priv *np = netdev_priv(dev);
3171        struct ice_vsi *vsi = np->vsi;
3172        struct ice_pf *pf = vsi->back;
3173
3174        /* check to see if VSI is active */
3175        if (test_bit(__ICE_DOWN, vsi->state))
3176                return;
3177
3178        /* report maximum channels */
3179        ch->max_rx = ice_get_max_rxq(pf);
3180        ch->max_tx = ice_get_max_txq(pf);
3181        ch->max_combined = min_t(int, ch->max_rx, ch->max_tx);
3182
3183        /* report current channels */
3184        ch->combined_count = ice_get_combined_cnt(vsi);
3185        ch->rx_count = vsi->num_rxq - ch->combined_count;
3186        ch->tx_count = vsi->num_txq - ch->combined_count;
3187}
3188
3189/**
3190 * ice_vsi_set_dflt_rss_lut - set default RSS LUT with requested RSS size
3191 * @vsi: VSI to reconfigure RSS LUT on
3192 * @req_rss_size: requested range of queue numbers for hashing
3193 *
3194 * Set the VSI's RSS parameters, configure the RSS LUT based on these.
3195 */
3196static int ice_vsi_set_dflt_rss_lut(struct ice_vsi *vsi, int req_rss_size)
3197{
3198        struct ice_pf *pf = vsi->back;
3199        enum ice_status status;
3200        struct device *dev;
3201        struct ice_hw *hw;
3202        int err = 0;
3203        u8 *lut;
3204
3205        dev = ice_pf_to_dev(pf);
3206        hw = &pf->hw;
3207
3208        if (!req_rss_size)
3209                return -EINVAL;
3210
3211        lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3212        if (!lut)
3213                return -ENOMEM;
3214
3215        /* set RSS LUT parameters */
3216        if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3217                vsi->rss_size = 1;
3218        } else {
3219                struct ice_hw_common_caps *caps = &hw->func_caps.common_cap;
3220
3221                vsi->rss_size = min_t(int, req_rss_size,
3222                                      BIT(caps->rss_table_entry_width));
3223        }
3224
3225        /* create/set RSS LUT */
3226        ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
3227        status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type, lut,
3228                                    vsi->rss_table_size);
3229        if (status) {
3230                dev_err(dev, "Cannot set RSS lut, err %d aq_err %d\n",
3231                        status, hw->adminq.rq_last_status);
3232                err = -EIO;
3233        }
3234
3235        kfree(lut);
3236        return err;
3237}
3238
3239/**
3240 * ice_set_channels - set the number channels
3241 * @dev: network interface device structure
3242 * @ch: ethtool channel data structure
3243 */
3244static int ice_set_channels(struct net_device *dev, struct ethtool_channels *ch)
3245{
3246        struct ice_netdev_priv *np = netdev_priv(dev);
3247        struct ice_vsi *vsi = np->vsi;
3248        struct ice_pf *pf = vsi->back;
3249        int new_rx = 0, new_tx = 0;
3250        u32 curr_combined;
3251
3252        /* do not support changing channels in Safe Mode */
3253        if (ice_is_safe_mode(pf)) {
3254                netdev_err(dev, "Changing channel in Safe Mode is not supported\n");
3255                return -EOPNOTSUPP;
3256        }
3257        /* do not support changing other_count */
3258        if (ch->other_count)
3259                return -EINVAL;
3260
3261        curr_combined = ice_get_combined_cnt(vsi);
3262
3263        /* these checks are for cases where user didn't specify a particular
3264         * value on cmd line but we get non-zero value anyway via
3265         * get_channels(); look at ethtool.c in ethtool repository (the user
3266         * space part), particularly, do_schannels() routine
3267         */
3268        if (ch->rx_count == vsi->num_rxq - curr_combined)
3269                ch->rx_count = 0;
3270        if (ch->tx_count == vsi->num_txq - curr_combined)
3271                ch->tx_count = 0;
3272        if (ch->combined_count == curr_combined)
3273                ch->combined_count = 0;
3274
3275        if (!(ch->combined_count || (ch->rx_count && ch->tx_count))) {
3276                netdev_err(dev, "Please specify at least 1 Rx and 1 Tx channel\n");
3277                return -EINVAL;
3278        }
3279
3280        new_rx = ch->combined_count + ch->rx_count;
3281        new_tx = ch->combined_count + ch->tx_count;
3282
3283        if (new_rx > ice_get_max_rxq(pf)) {
3284                netdev_err(dev, "Maximum allowed Rx channels is %d\n",
3285                           ice_get_max_rxq(pf));
3286                return -EINVAL;
3287        }
3288        if (new_tx > ice_get_max_txq(pf)) {
3289                netdev_err(dev, "Maximum allowed Tx channels is %d\n",
3290                           ice_get_max_txq(pf));
3291                return -EINVAL;
3292        }
3293
3294        ice_vsi_recfg_qs(vsi, new_rx, new_tx);
3295
3296        if (new_rx && !netif_is_rxfh_configured(dev))
3297                return ice_vsi_set_dflt_rss_lut(vsi, new_rx);
3298
3299        return 0;
3300}
3301
3302enum ice_container_type {
3303        ICE_RX_CONTAINER,
3304        ICE_TX_CONTAINER,
3305};
3306
3307/**
3308 * ice_get_rc_coalesce - get ITR values for specific ring container
3309 * @ec: ethtool structure to fill with driver's coalesce settings
3310 * @c_type: container type, Rx or Tx
3311 * @rc: ring container that the ITR values will come from
3312 *
3313 * Query the device for ice_ring_container specific ITR values. This is
3314 * done per ice_ring_container because each q_vector can have 1 or more rings
3315 * and all of said ring(s) will have the same ITR values.
3316 *
3317 * Returns 0 on success, negative otherwise.
3318 */
3319static int
3320ice_get_rc_coalesce(struct ethtool_coalesce *ec, enum ice_container_type c_type,
3321                    struct ice_ring_container *rc)
3322{
3323        struct ice_pf *pf;
3324
3325        if (!rc->ring)
3326                return -EINVAL;
3327
3328        pf = rc->ring->vsi->back;
3329
3330        switch (c_type) {
3331        case ICE_RX_CONTAINER:
3332                ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
3333                ec->rx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3334                ec->rx_coalesce_usecs_high = rc->ring->q_vector->intrl;
3335                break;
3336        case ICE_TX_CONTAINER:
3337                ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
3338                ec->tx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3339                break;
3340        default:
3341                dev_dbg(ice_pf_to_dev(pf), "Invalid c_type %d\n", c_type);
3342                return -EINVAL;
3343        }
3344
3345        return 0;
3346}
3347
3348/**
3349 * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings
3350 * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings
3351 * @ec: coalesce settings to program the device with
3352 * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3353 *
3354 * Return 0 on success, and negative under the following conditions:
3355 * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed.
3356 * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3357 */
3358static int
3359ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3360{
3361        if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3362                if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
3363                                        &vsi->rx_rings[q_num]->q_vector->rx))
3364                        return -EINVAL;
3365                if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
3366                                        &vsi->tx_rings[q_num]->q_vector->tx))
3367                        return -EINVAL;
3368        } else if (q_num < vsi->num_rxq) {
3369                if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
3370                                        &vsi->rx_rings[q_num]->q_vector->rx))
3371                        return -EINVAL;
3372        } else if (q_num < vsi->num_txq) {
3373                if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
3374                                        &vsi->tx_rings[q_num]->q_vector->tx))
3375                        return -EINVAL;
3376        } else {
3377                return -EINVAL;
3378        }
3379
3380        return 0;
3381}
3382
3383/**
3384 * __ice_get_coalesce - get ITR/INTRL values for the device
3385 * @netdev: pointer to the netdev associated with this query
3386 * @ec: ethtool structure to fill with driver's coalesce settings
3387 * @q_num: queue number to get the coalesce settings for
3388 *
3389 * If the caller passes in a negative q_num then we return coalesce settings
3390 * based on queue number 0, else use the actual q_num passed in.
3391 */
3392static int
3393__ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3394                   int q_num)
3395{
3396        struct ice_netdev_priv *np = netdev_priv(netdev);
3397        struct ice_vsi *vsi = np->vsi;
3398
3399        if (q_num < 0)
3400                q_num = 0;
3401
3402        if (ice_get_q_coalesce(vsi, ec, q_num))
3403                return -EINVAL;
3404
3405        return 0;
3406}
3407
3408static int
3409ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
3410{
3411        return __ice_get_coalesce(netdev, ec, -1);
3412}
3413
3414static int
3415ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
3416                       struct ethtool_coalesce *ec)
3417{
3418        return __ice_get_coalesce(netdev, ec, q_num);
3419}
3420
3421/**
3422 * ice_set_rc_coalesce - set ITR values for specific ring container
3423 * @c_type: container type, Rx or Tx
3424 * @ec: ethtool structure from user to update ITR settings
3425 * @rc: ring container that the ITR values will come from
3426 * @vsi: VSI associated to the ring container
3427 *
3428 * Set specific ITR values. This is done per ice_ring_container because each
3429 * q_vector can have 1 or more rings and all of said ring(s) will have the same
3430 * ITR values.
3431 *
3432 * Returns 0 on success, negative otherwise.
3433 */
3434static int
3435ice_set_rc_coalesce(enum ice_container_type c_type, struct ethtool_coalesce *ec,
3436                    struct ice_ring_container *rc, struct ice_vsi *vsi)
3437{
3438        const char *c_type_str = (c_type == ICE_RX_CONTAINER) ? "rx" : "tx";
3439        u32 use_adaptive_coalesce, coalesce_usecs;
3440        struct ice_pf *pf = vsi->back;
3441        u16 itr_setting;
3442
3443        if (!rc->ring)
3444                return -EINVAL;
3445
3446        switch (c_type) {
3447        case ICE_RX_CONTAINER:
3448                if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL ||
3449                    (ec->rx_coalesce_usecs_high &&
3450                     ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) {
3451                        netdev_info(vsi->netdev, "Invalid value, %s-usecs-high valid values are 0 (disabled), %d-%d\n",
3452                                    c_type_str, pf->hw.intrl_gran,
3453                                    ICE_MAX_INTRL);
3454                        return -EINVAL;
3455                }
3456                if (ec->rx_coalesce_usecs_high != rc->ring->q_vector->intrl) {
3457                        rc->ring->q_vector->intrl = ec->rx_coalesce_usecs_high;
3458                        wr32(&pf->hw, GLINT_RATE(rc->ring->q_vector->reg_idx),
3459                             ice_intrl_usec_to_reg(ec->rx_coalesce_usecs_high,
3460                                                   pf->hw.intrl_gran));
3461                }
3462
3463                use_adaptive_coalesce = ec->use_adaptive_rx_coalesce;
3464                coalesce_usecs = ec->rx_coalesce_usecs;
3465
3466                break;
3467        case ICE_TX_CONTAINER:
3468                use_adaptive_coalesce = ec->use_adaptive_tx_coalesce;
3469                coalesce_usecs = ec->tx_coalesce_usecs;
3470
3471                break;
3472        default:
3473                dev_dbg(ice_pf_to_dev(pf), "Invalid container type %d\n",
3474                        c_type);
3475                return -EINVAL;
3476        }
3477
3478        itr_setting = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3479        if (coalesce_usecs != itr_setting && use_adaptive_coalesce) {
3480                netdev_info(vsi->netdev, "%s interrupt throttling cannot be changed if adaptive-%s is enabled\n",
3481                            c_type_str, c_type_str);
3482                return -EINVAL;
3483        }
3484
3485        if (coalesce_usecs > ICE_ITR_MAX) {
3486                netdev_info(vsi->netdev, "Invalid value, %s-usecs range is 0-%d\n",
3487                            c_type_str, ICE_ITR_MAX);
3488                return -EINVAL;
3489        }
3490
3491        if (use_adaptive_coalesce) {
3492                rc->itr_setting |= ICE_ITR_DYNAMIC;
3493        } else {
3494                /* save the user set usecs */
3495                rc->itr_setting = coalesce_usecs;
3496                /* device ITR granularity is in 2 usec increments */
3497                rc->target_itr = ITR_REG_ALIGN(rc->itr_setting);
3498        }
3499
3500        return 0;
3501}
3502
3503/**
3504 * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings
3505 * @vsi: VSI associated to the queue that need updating
3506 * @ec: coalesce settings to program the device with
3507 * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3508 *
3509 * Return 0 on success, and negative under the following conditions:
3510 * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed.
3511 * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3512 */
3513static int
3514ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3515{
3516        if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3517                if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
3518                                        &vsi->rx_rings[q_num]->q_vector->rx,
3519                                        vsi))
3520                        return -EINVAL;
3521
3522                if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
3523                                        &vsi->tx_rings[q_num]->q_vector->tx,
3524                                        vsi))
3525                        return -EINVAL;
3526        } else if (q_num < vsi->num_rxq) {
3527                if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
3528                                        &vsi->rx_rings[q_num]->q_vector->rx,
3529                                        vsi))
3530                        return -EINVAL;
3531        } else if (q_num < vsi->num_txq) {
3532                if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
3533                                        &vsi->tx_rings[q_num]->q_vector->tx,
3534                                        vsi))
3535                        return -EINVAL;
3536        } else {
3537                return -EINVAL;
3538        }
3539
3540        return 0;
3541}
3542
3543/**
3544 * ice_print_if_odd_usecs - print message if user tries to set odd [tx|rx]-usecs
3545 * @netdev: netdev used for print
3546 * @itr_setting: previous user setting
3547 * @use_adaptive_coalesce: if adaptive coalesce is enabled or being enabled
3548 * @coalesce_usecs: requested value of [tx|rx]-usecs
3549 * @c_type_str: either "rx" or "tx" to match user set field of [tx|rx]-usecs
3550 */
3551static void
3552ice_print_if_odd_usecs(struct net_device *netdev, u16 itr_setting,
3553                       u32 use_adaptive_coalesce, u32 coalesce_usecs,
3554                       const char *c_type_str)
3555{
3556        if (use_adaptive_coalesce)
3557                return;
3558
3559        itr_setting = ITR_TO_REG(itr_setting);
3560
3561        if (itr_setting != coalesce_usecs && (coalesce_usecs % 2))
3562                netdev_info(netdev, "User set %s-usecs to %d, device only supports even values. Rounding down and attempting to set %s-usecs to %d\n",
3563                            c_type_str, coalesce_usecs, c_type_str,
3564                            ITR_REG_ALIGN(coalesce_usecs));
3565}
3566
3567/**
3568 * __ice_set_coalesce - set ITR/INTRL values for the device
3569 * @netdev: pointer to the netdev associated with this query
3570 * @ec: ethtool structure to fill with driver's coalesce settings
3571 * @q_num: queue number to get the coalesce settings for
3572 *
3573 * If the caller passes in a negative q_num then we set the coalesce settings
3574 * for all Tx/Rx queues, else use the actual q_num passed in.
3575 */
3576static int
3577__ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3578                   int q_num)
3579{
3580        struct ice_netdev_priv *np = netdev_priv(netdev);
3581        struct ice_vsi *vsi = np->vsi;
3582
3583        if (q_num < 0) {
3584                struct ice_q_vector *q_vector = vsi->q_vectors[0];
3585                int v_idx;
3586
3587                if (q_vector) {
3588                        ice_print_if_odd_usecs(netdev, q_vector->rx.itr_setting,
3589                                               ec->use_adaptive_rx_coalesce,
3590                                               ec->rx_coalesce_usecs, "rx");
3591
3592                        ice_print_if_odd_usecs(netdev, q_vector->tx.itr_setting,
3593                                               ec->use_adaptive_tx_coalesce,
3594                                               ec->tx_coalesce_usecs, "tx");
3595                }
3596
3597                ice_for_each_q_vector(vsi, v_idx) {
3598                        /* In some cases if DCB is configured the num_[rx|tx]q
3599                         * can be less than vsi->num_q_vectors. This check
3600                         * accounts for that so we don't report a false failure
3601                         */
3602                        if (v_idx >= vsi->num_rxq && v_idx >= vsi->num_txq)
3603                                goto set_complete;
3604
3605                        if (ice_set_q_coalesce(vsi, ec, v_idx))
3606                                return -EINVAL;
3607                }
3608                goto set_complete;
3609        }
3610
3611        if (ice_set_q_coalesce(vsi, ec, q_num))
3612                return -EINVAL;
3613
3614set_complete:
3615
3616        return 0;
3617}
3618
3619static int
3620ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
3621{
3622        return __ice_set_coalesce(netdev, ec, -1);
3623}
3624
3625static int
3626ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
3627                       struct ethtool_coalesce *ec)
3628{
3629        return __ice_set_coalesce(netdev, ec, q_num);
3630}
3631
3632#define ICE_I2C_EEPROM_DEV_ADDR         0xA0
3633#define ICE_I2C_EEPROM_DEV_ADDR2        0xA2
3634#define ICE_MODULE_TYPE_SFP             0x03
3635#define ICE_MODULE_TYPE_QSFP_PLUS       0x0D
3636#define ICE_MODULE_TYPE_QSFP28          0x11
3637#define ICE_MODULE_SFF_ADDR_MODE        0x04
3638#define ICE_MODULE_SFF_DIAG_CAPAB       0x40
3639#define ICE_MODULE_REVISION_ADDR        0x01
3640#define ICE_MODULE_SFF_8472_COMP        0x5E
3641#define ICE_MODULE_SFF_8472_SWAP        0x5C
3642#define ICE_MODULE_QSFP_MAX_LEN         640
3643
3644/**
3645 * ice_get_module_info - get SFF module type and revision information
3646 * @netdev: network interface device structure
3647 * @modinfo: module EEPROM size and layout information structure
3648 */
3649static int
3650ice_get_module_info(struct net_device *netdev,
3651                    struct ethtool_modinfo *modinfo)
3652{
3653        struct ice_netdev_priv *np = netdev_priv(netdev);
3654        struct ice_vsi *vsi = np->vsi;
3655        struct ice_pf *pf = vsi->back;
3656        struct ice_hw *hw = &pf->hw;
3657        enum ice_status status;
3658        u8 sff8472_comp = 0;
3659        u8 sff8472_swap = 0;
3660        u8 sff8636_rev = 0;
3661        u8 value = 0;
3662
3663        status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 0x00, 0x00,
3664                                   0, &value, 1, 0, NULL);
3665        if (status)
3666                return -EIO;
3667
3668        switch (value) {
3669        case ICE_MODULE_TYPE_SFP:
3670                status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
3671                                           ICE_MODULE_SFF_8472_COMP, 0x00, 0,
3672                                           &sff8472_comp, 1, 0, NULL);
3673                if (status)
3674                        return -EIO;
3675                status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
3676                                           ICE_MODULE_SFF_8472_SWAP, 0x00, 0,
3677                                           &sff8472_swap, 1, 0, NULL);
3678                if (status)
3679                        return -EIO;
3680
3681                if (sff8472_swap & ICE_MODULE_SFF_ADDR_MODE) {
3682                        modinfo->type = ETH_MODULE_SFF_8079;
3683                        modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
3684                } else if (sff8472_comp &&
3685                           (sff8472_swap & ICE_MODULE_SFF_DIAG_CAPAB)) {
3686                        modinfo->type = ETH_MODULE_SFF_8472;
3687                        modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
3688                } else {
3689                        modinfo->type = ETH_MODULE_SFF_8079;
3690                        modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
3691                }
3692                break;
3693        case ICE_MODULE_TYPE_QSFP_PLUS:
3694        case ICE_MODULE_TYPE_QSFP28:
3695                status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
3696                                           ICE_MODULE_REVISION_ADDR, 0x00, 0,
3697                                           &sff8636_rev, 1, 0, NULL);
3698                if (status)
3699                        return -EIO;
3700                /* Check revision compliance */
3701                if (sff8636_rev > 0x02) {
3702                        /* Module is SFF-8636 compliant */
3703                        modinfo->type = ETH_MODULE_SFF_8636;
3704                        modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
3705                } else {
3706                        modinfo->type = ETH_MODULE_SFF_8436;
3707                        modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
3708                }
3709                break;
3710        default:
3711                netdev_warn(netdev, "SFF Module Type not recognized.\n");
3712                return -EINVAL;
3713        }
3714        return 0;
3715}
3716
3717/**
3718 * ice_get_module_eeprom - fill buffer with SFF EEPROM contents
3719 * @netdev: network interface device structure
3720 * @ee: EEPROM dump request structure
3721 * @data: buffer to be filled with EEPROM contents
3722 */
3723static int
3724ice_get_module_eeprom(struct net_device *netdev,
3725                      struct ethtool_eeprom *ee, u8 *data)
3726{
3727        struct ice_netdev_priv *np = netdev_priv(netdev);
3728        u8 addr = ICE_I2C_EEPROM_DEV_ADDR;
3729        struct ice_vsi *vsi = np->vsi;
3730        struct ice_pf *pf = vsi->back;
3731        struct ice_hw *hw = &pf->hw;
3732        enum ice_status status;
3733        bool is_sfp = false;
3734        u16 offset = 0;
3735        u8 value = 0;
3736        u8 page = 0;
3737        int i;
3738
3739        status = ice_aq_sff_eeprom(hw, 0, addr, offset, page, 0,
3740                                   &value, 1, 0, NULL);
3741        if (status)
3742                return -EIO;
3743
3744        if (!ee || !ee->len || !data)
3745                return -EINVAL;
3746
3747        if (value == ICE_MODULE_TYPE_SFP)
3748                is_sfp = true;
3749
3750        for (i = 0; i < ee->len; i++) {
3751                offset = i + ee->offset;
3752
3753                /* Check if we need to access the other memory page */
3754                if (is_sfp) {
3755                        if (offset >= ETH_MODULE_SFF_8079_LEN) {
3756                                offset -= ETH_MODULE_SFF_8079_LEN;
3757                                addr = ICE_I2C_EEPROM_DEV_ADDR2;
3758                        }
3759                } else {
3760                        while (offset >= ETH_MODULE_SFF_8436_LEN) {
3761                                /* Compute memory page number and offset. */
3762                                offset -= ETH_MODULE_SFF_8436_LEN / 2;
3763                                page++;
3764                        }
3765                }
3766
3767                status = ice_aq_sff_eeprom(hw, 0, addr, offset, page, !is_sfp,
3768                                           &value, 1, 0, NULL);
3769                if (status)
3770                        value = 0;
3771                data[i] = value;
3772        }
3773        return 0;
3774}
3775
3776static const struct ethtool_ops ice_ethtool_ops = {
3777        .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3778                                     ETHTOOL_COALESCE_USE_ADAPTIVE |
3779                                     ETHTOOL_COALESCE_RX_USECS_HIGH,
3780        .get_link_ksettings     = ice_get_link_ksettings,
3781        .set_link_ksettings     = ice_set_link_ksettings,
3782        .get_drvinfo            = ice_get_drvinfo,
3783        .get_regs_len           = ice_get_regs_len,
3784        .get_regs               = ice_get_regs,
3785        .get_msglevel           = ice_get_msglevel,
3786        .set_msglevel           = ice_set_msglevel,
3787        .self_test              = ice_self_test,
3788        .get_link               = ethtool_op_get_link,
3789        .get_eeprom_len         = ice_get_eeprom_len,
3790        .get_eeprom             = ice_get_eeprom,
3791        .get_coalesce           = ice_get_coalesce,
3792        .set_coalesce           = ice_set_coalesce,
3793        .get_strings            = ice_get_strings,
3794        .set_phys_id            = ice_set_phys_id,
3795        .get_ethtool_stats      = ice_get_ethtool_stats,
3796        .get_priv_flags         = ice_get_priv_flags,
3797        .set_priv_flags         = ice_set_priv_flags,
3798        .get_sset_count         = ice_get_sset_count,
3799        .get_rxnfc              = ice_get_rxnfc,
3800        .set_rxnfc              = ice_set_rxnfc,
3801        .get_ringparam          = ice_get_ringparam,
3802        .set_ringparam          = ice_set_ringparam,
3803        .nway_reset             = ice_nway_reset,
3804        .get_pauseparam         = ice_get_pauseparam,
3805        .set_pauseparam         = ice_set_pauseparam,
3806        .get_rxfh_key_size      = ice_get_rxfh_key_size,
3807        .get_rxfh_indir_size    = ice_get_rxfh_indir_size,
3808        .get_rxfh               = ice_get_rxfh,
3809        .set_rxfh               = ice_set_rxfh,
3810        .get_channels           = ice_get_channels,
3811        .set_channels           = ice_set_channels,
3812        .get_ts_info            = ethtool_op_get_ts_info,
3813        .get_per_queue_coalesce = ice_get_per_q_coalesce,
3814        .set_per_queue_coalesce = ice_set_per_q_coalesce,
3815        .get_fecparam           = ice_get_fecparam,
3816        .set_fecparam           = ice_set_fecparam,
3817        .get_module_info        = ice_get_module_info,
3818        .get_module_eeprom      = ice_get_module_eeprom,
3819};
3820
3821static const struct ethtool_ops ice_ethtool_safe_mode_ops = {
3822        .get_link_ksettings     = ice_get_link_ksettings,
3823        .set_link_ksettings     = ice_set_link_ksettings,
3824        .get_drvinfo            = ice_get_drvinfo,
3825        .get_regs_len           = ice_get_regs_len,
3826        .get_regs               = ice_get_regs,
3827        .get_msglevel           = ice_get_msglevel,
3828        .set_msglevel           = ice_set_msglevel,
3829        .get_link               = ethtool_op_get_link,
3830        .get_eeprom_len         = ice_get_eeprom_len,
3831        .get_eeprom             = ice_get_eeprom,
3832        .get_strings            = ice_get_strings,
3833        .get_ethtool_stats      = ice_get_ethtool_stats,
3834        .get_sset_count         = ice_get_sset_count,
3835        .get_ringparam          = ice_get_ringparam,
3836        .set_ringparam          = ice_set_ringparam,
3837        .nway_reset             = ice_nway_reset,
3838        .get_channels           = ice_get_channels,
3839};
3840
3841/**
3842 * ice_set_ethtool_safe_mode_ops - setup safe mode ethtool ops
3843 * @netdev: network interface device structure
3844 */
3845void ice_set_ethtool_safe_mode_ops(struct net_device *netdev)
3846{
3847        netdev->ethtool_ops = &ice_ethtool_safe_mode_ops;
3848}
3849
3850/**
3851 * ice_set_ethtool_ops - setup netdev ethtool ops
3852 * @netdev: network interface device structure
3853 *
3854 * setup netdev ethtool ops with ice specific ops
3855 */
3856void ice_set_ethtool_ops(struct net_device *netdev)
3857{
3858        netdev->ethtool_ops = &ice_ethtool_ops;
3859}
3860