Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2018, Intel Corporation. */
0003 
0004 /* ethtool support for ice */
0005 
0006 #include "ice.h"
0007 #include "ice_flow.h"
0008 #include "ice_fltr.h"
0009 #include "ice_lib.h"
0010 #include "ice_dcb_lib.h"
0011 #include <net/dcbnl.h>
0012 
0013 struct ice_stats {
0014     char stat_string[ETH_GSTRING_LEN];
0015     int sizeof_stat;
0016     int stat_offset;
0017 };
0018 
0019 #define ICE_STAT(_type, _name, _stat) { \
0020     .stat_string = _name, \
0021     .sizeof_stat = sizeof_field(_type, _stat), \
0022     .stat_offset = offsetof(_type, _stat) \
0023 }
0024 
0025 #define ICE_VSI_STAT(_name, _stat) \
0026         ICE_STAT(struct ice_vsi, _name, _stat)
0027 #define ICE_PF_STAT(_name, _stat) \
0028         ICE_STAT(struct ice_pf, _name, _stat)
0029 
0030 static int ice_q_stats_len(struct net_device *netdev)
0031 {
0032     struct ice_netdev_priv *np = netdev_priv(netdev);
0033 
0034     return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) *
0035         (sizeof(struct ice_q_stats) / sizeof(u64)));
0036 }
0037 
0038 #define ICE_PF_STATS_LEN    ARRAY_SIZE(ice_gstrings_pf_stats)
0039 #define ICE_VSI_STATS_LEN   ARRAY_SIZE(ice_gstrings_vsi_stats)
0040 
0041 #define ICE_PFC_STATS_LEN ( \
0042         (sizeof_field(struct ice_pf, stats.priority_xoff_rx) + \
0043          sizeof_field(struct ice_pf, stats.priority_xon_rx) + \
0044          sizeof_field(struct ice_pf, stats.priority_xoff_tx) + \
0045          sizeof_field(struct ice_pf, stats.priority_xon_tx)) \
0046          / sizeof(u64))
0047 #define ICE_ALL_STATS_LEN(n)    (ICE_PF_STATS_LEN + ICE_PFC_STATS_LEN + \
0048                  ICE_VSI_STATS_LEN + ice_q_stats_len(n))
0049 
0050 static const struct ice_stats ice_gstrings_vsi_stats[] = {
0051     ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
0052     ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
0053     ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
0054     ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
0055     ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
0056     ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
0057     ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes),
0058     ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes),
0059     ICE_VSI_STAT("rx_dropped", eth_stats.rx_discards),
0060     ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
0061     ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed),
0062     ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
0063     ICE_VSI_STAT("tx_errors", eth_stats.tx_errors),
0064     ICE_VSI_STAT("tx_linearize", tx_linearize),
0065     ICE_VSI_STAT("tx_busy", tx_busy),
0066     ICE_VSI_STAT("tx_restart", tx_restart),
0067 };
0068 
0069 enum ice_ethtool_test_id {
0070     ICE_ETH_TEST_REG = 0,
0071     ICE_ETH_TEST_EEPROM,
0072     ICE_ETH_TEST_INTR,
0073     ICE_ETH_TEST_LOOP,
0074     ICE_ETH_TEST_LINK,
0075 };
0076 
0077 static const char ice_gstrings_test[][ETH_GSTRING_LEN] = {
0078     "Register test  (offline)",
0079     "EEPROM test    (offline)",
0080     "Interrupt test (offline)",
0081     "Loopback test  (offline)",
0082     "Link test   (on/offline)",
0083 };
0084 
0085 #define ICE_TEST_LEN (sizeof(ice_gstrings_test) / ETH_GSTRING_LEN)
0086 
0087 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
0088  * but they aren't. This device is capable of supporting multiple
0089  * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual
0090  * netdevs whereas the PF_STATs are for the physical function that's
0091  * hosting these netdevs.
0092  *
0093  * The PF_STATs are appended to the netdev stats only when ethtool -S
0094  * is queried on the base PF netdev.
0095  */
0096 static const struct ice_stats ice_gstrings_pf_stats[] = {
0097     ICE_PF_STAT("rx_bytes.nic", stats.eth.rx_bytes),
0098     ICE_PF_STAT("tx_bytes.nic", stats.eth.tx_bytes),
0099     ICE_PF_STAT("rx_unicast.nic", stats.eth.rx_unicast),
0100     ICE_PF_STAT("tx_unicast.nic", stats.eth.tx_unicast),
0101     ICE_PF_STAT("rx_multicast.nic", stats.eth.rx_multicast),
0102     ICE_PF_STAT("tx_multicast.nic", stats.eth.tx_multicast),
0103     ICE_PF_STAT("rx_broadcast.nic", stats.eth.rx_broadcast),
0104     ICE_PF_STAT("tx_broadcast.nic", stats.eth.tx_broadcast),
0105     ICE_PF_STAT("tx_errors.nic", stats.eth.tx_errors),
0106     ICE_PF_STAT("tx_timeout.nic", tx_timeout_count),
0107     ICE_PF_STAT("rx_size_64.nic", stats.rx_size_64),
0108     ICE_PF_STAT("tx_size_64.nic", stats.tx_size_64),
0109     ICE_PF_STAT("rx_size_127.nic", stats.rx_size_127),
0110     ICE_PF_STAT("tx_size_127.nic", stats.tx_size_127),
0111     ICE_PF_STAT("rx_size_255.nic", stats.rx_size_255),
0112     ICE_PF_STAT("tx_size_255.nic", stats.tx_size_255),
0113     ICE_PF_STAT("rx_size_511.nic", stats.rx_size_511),
0114     ICE_PF_STAT("tx_size_511.nic", stats.tx_size_511),
0115     ICE_PF_STAT("rx_size_1023.nic", stats.rx_size_1023),
0116     ICE_PF_STAT("tx_size_1023.nic", stats.tx_size_1023),
0117     ICE_PF_STAT("rx_size_1522.nic", stats.rx_size_1522),
0118     ICE_PF_STAT("tx_size_1522.nic", stats.tx_size_1522),
0119     ICE_PF_STAT("rx_size_big.nic", stats.rx_size_big),
0120     ICE_PF_STAT("tx_size_big.nic", stats.tx_size_big),
0121     ICE_PF_STAT("link_xon_rx.nic", stats.link_xon_rx),
0122     ICE_PF_STAT("link_xon_tx.nic", stats.link_xon_tx),
0123     ICE_PF_STAT("link_xoff_rx.nic", stats.link_xoff_rx),
0124     ICE_PF_STAT("link_xoff_tx.nic", stats.link_xoff_tx),
0125     ICE_PF_STAT("tx_dropped_link_down.nic", stats.tx_dropped_link_down),
0126     ICE_PF_STAT("rx_undersize.nic", stats.rx_undersize),
0127     ICE_PF_STAT("rx_fragments.nic", stats.rx_fragments),
0128     ICE_PF_STAT("rx_oversize.nic", stats.rx_oversize),
0129     ICE_PF_STAT("rx_jabber.nic", stats.rx_jabber),
0130     ICE_PF_STAT("rx_csum_bad.nic", hw_csum_rx_error),
0131     ICE_PF_STAT("rx_length_errors.nic", stats.rx_len_errors),
0132     ICE_PF_STAT("rx_dropped.nic", stats.eth.rx_discards),
0133     ICE_PF_STAT("rx_crc_errors.nic", stats.crc_errors),
0134     ICE_PF_STAT("illegal_bytes.nic", stats.illegal_bytes),
0135     ICE_PF_STAT("mac_local_faults.nic", stats.mac_local_faults),
0136     ICE_PF_STAT("mac_remote_faults.nic", stats.mac_remote_faults),
0137     ICE_PF_STAT("fdir_sb_match.nic", stats.fd_sb_match),
0138     ICE_PF_STAT("fdir_sb_status.nic", stats.fd_sb_status),
0139 };
0140 
0141 static const u32 ice_regs_dump_list[] = {
0142     PFGEN_STATE,
0143     PRTGEN_STATUS,
0144     QRX_CTRL(0),
0145     QINT_TQCTL(0),
0146     QINT_RQCTL(0),
0147     PFINT_OICR_ENA,
0148     QRX_ITR(0),
0149 };
0150 
0151 struct ice_priv_flag {
0152     char name[ETH_GSTRING_LEN];
0153     u32 bitno;          /* bit position in pf->flags */
0154 };
0155 
0156 #define ICE_PRIV_FLAG(_name, _bitno) { \
0157     .name = _name, \
0158     .bitno = _bitno, \
0159 }
0160 
0161 static const struct ice_priv_flag ice_gstrings_priv_flags[] = {
0162     ICE_PRIV_FLAG("link-down-on-close", ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA),
0163     ICE_PRIV_FLAG("fw-lldp-agent", ICE_FLAG_FW_LLDP_AGENT),
0164     ICE_PRIV_FLAG("vf-true-promisc-support",
0165               ICE_FLAG_VF_TRUE_PROMISC_ENA),
0166     ICE_PRIV_FLAG("mdd-auto-reset-vf", ICE_FLAG_MDD_AUTO_RESET_VF),
0167     ICE_PRIV_FLAG("vf-vlan-pruning", ICE_FLAG_VF_VLAN_PRUNING),
0168     ICE_PRIV_FLAG("legacy-rx", ICE_FLAG_LEGACY_RX),
0169 };
0170 
0171 #define ICE_PRIV_FLAG_ARRAY_SIZE    ARRAY_SIZE(ice_gstrings_priv_flags)
0172 
0173 static void
0174 __ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo,
0175           struct ice_vsi *vsi)
0176 {
0177     struct ice_pf *pf = vsi->back;
0178     struct ice_hw *hw = &pf->hw;
0179     struct ice_orom_info *orom;
0180     struct ice_nvm_info *nvm;
0181 
0182     nvm = &hw->flash.nvm;
0183     orom = &hw->flash.orom;
0184 
0185     strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
0186 
0187     /* Display NVM version (from which the firmware version can be
0188      * determined) which contains more pertinent information.
0189      */
0190     snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
0191          "%x.%02x 0x%x %d.%d.%d", nvm->major, nvm->minor,
0192          nvm->eetrack, orom->major, orom->build, orom->patch);
0193 
0194     strscpy(drvinfo->bus_info, pci_name(pf->pdev),
0195         sizeof(drvinfo->bus_info));
0196 }
0197 
0198 static void
0199 ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
0200 {
0201     struct ice_netdev_priv *np = netdev_priv(netdev);
0202 
0203     __ice_get_drvinfo(netdev, drvinfo, np->vsi);
0204     drvinfo->n_priv_flags = ICE_PRIV_FLAG_ARRAY_SIZE;
0205 }
0206 
0207 static int ice_get_regs_len(struct net_device __always_unused *netdev)
0208 {
0209     return sizeof(ice_regs_dump_list);
0210 }
0211 
0212 static void
0213 ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
0214 {
0215     struct ice_netdev_priv *np = netdev_priv(netdev);
0216     struct ice_pf *pf = np->vsi->back;
0217     struct ice_hw *hw = &pf->hw;
0218     u32 *regs_buf = (u32 *)p;
0219     unsigned int i;
0220 
0221     regs->version = 1;
0222 
0223     for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i)
0224         regs_buf[i] = rd32(hw, ice_regs_dump_list[i]);
0225 }
0226 
0227 static u32 ice_get_msglevel(struct net_device *netdev)
0228 {
0229     struct ice_netdev_priv *np = netdev_priv(netdev);
0230     struct ice_pf *pf = np->vsi->back;
0231 
0232 #ifndef CONFIG_DYNAMIC_DEBUG
0233     if (pf->hw.debug_mask)
0234         netdev_info(netdev, "hw debug_mask: 0x%llX\n",
0235                 pf->hw.debug_mask);
0236 #endif /* !CONFIG_DYNAMIC_DEBUG */
0237 
0238     return pf->msg_enable;
0239 }
0240 
0241 static void ice_set_msglevel(struct net_device *netdev, u32 data)
0242 {
0243     struct ice_netdev_priv *np = netdev_priv(netdev);
0244     struct ice_pf *pf = np->vsi->back;
0245 
0246 #ifndef CONFIG_DYNAMIC_DEBUG
0247     if (ICE_DBG_USER & data)
0248         pf->hw.debug_mask = data;
0249     else
0250         pf->msg_enable = data;
0251 #else
0252     pf->msg_enable = data;
0253 #endif /* !CONFIG_DYNAMIC_DEBUG */
0254 }
0255 
0256 static int ice_get_eeprom_len(struct net_device *netdev)
0257 {
0258     struct ice_netdev_priv *np = netdev_priv(netdev);
0259     struct ice_pf *pf = np->vsi->back;
0260 
0261     return (int)pf->hw.flash.flash_size;
0262 }
0263 
0264 static int
0265 ice_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
0266            u8 *bytes)
0267 {
0268     struct ice_netdev_priv *np = netdev_priv(netdev);
0269     struct ice_vsi *vsi = np->vsi;
0270     struct ice_pf *pf = vsi->back;
0271     struct ice_hw *hw = &pf->hw;
0272     struct device *dev;
0273     int ret;
0274     u8 *buf;
0275 
0276     dev = ice_pf_to_dev(pf);
0277 
0278     eeprom->magic = hw->vendor_id | (hw->device_id << 16);
0279     netdev_dbg(netdev, "GEEPROM cmd 0x%08x, offset 0x%08x, len 0x%08x\n",
0280            eeprom->cmd, eeprom->offset, eeprom->len);
0281 
0282     buf = kzalloc(eeprom->len, GFP_KERNEL);
0283     if (!buf)
0284         return -ENOMEM;
0285 
0286     ret = ice_acquire_nvm(hw, ICE_RES_READ);
0287     if (ret) {
0288         dev_err(dev, "ice_acquire_nvm failed, err %d aq_err %s\n",
0289             ret, ice_aq_str(hw->adminq.sq_last_status));
0290         goto out;
0291     }
0292 
0293     ret = ice_read_flat_nvm(hw, eeprom->offset, &eeprom->len, buf,
0294                 false);
0295     if (ret) {
0296         dev_err(dev, "ice_read_flat_nvm failed, err %d aq_err %s\n",
0297             ret, ice_aq_str(hw->adminq.sq_last_status));
0298         goto release;
0299     }
0300 
0301     memcpy(bytes, buf, eeprom->len);
0302 release:
0303     ice_release_nvm(hw);
0304 out:
0305     kfree(buf);
0306     return ret;
0307 }
0308 
0309 /**
0310  * ice_active_vfs - check if there are any active VFs
0311  * @pf: board private structure
0312  *
0313  * Returns true if an active VF is found, otherwise returns false
0314  */
0315 static bool ice_active_vfs(struct ice_pf *pf)
0316 {
0317     bool active = false;
0318     struct ice_vf *vf;
0319     unsigned int bkt;
0320 
0321     rcu_read_lock();
0322     ice_for_each_vf_rcu(pf, bkt, vf) {
0323         if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
0324             active = true;
0325             break;
0326         }
0327     }
0328     rcu_read_unlock();
0329 
0330     return active;
0331 }
0332 
0333 /**
0334  * ice_link_test - perform a link test on a given net_device
0335  * @netdev: network interface device structure
0336  *
0337  * This function performs one of the self-tests required by ethtool.
0338  * Returns 0 on success, non-zero on failure.
0339  */
0340 static u64 ice_link_test(struct net_device *netdev)
0341 {
0342     struct ice_netdev_priv *np = netdev_priv(netdev);
0343     bool link_up = false;
0344     int status;
0345 
0346     netdev_info(netdev, "link test\n");
0347     status = ice_get_link_status(np->vsi->port_info, &link_up);
0348     if (status) {
0349         netdev_err(netdev, "link query error, status = %d\n",
0350                status);
0351         return 1;
0352     }
0353 
0354     if (!link_up)
0355         return 2;
0356 
0357     return 0;
0358 }
0359 
0360 /**
0361  * ice_eeprom_test - perform an EEPROM test on a given net_device
0362  * @netdev: network interface device structure
0363  *
0364  * This function performs one of the self-tests required by ethtool.
0365  * Returns 0 on success, non-zero on failure.
0366  */
0367 static u64 ice_eeprom_test(struct net_device *netdev)
0368 {
0369     struct ice_netdev_priv *np = netdev_priv(netdev);
0370     struct ice_pf *pf = np->vsi->back;
0371 
0372     netdev_info(netdev, "EEPROM test\n");
0373     return !!(ice_nvm_validate_checksum(&pf->hw));
0374 }
0375 
0376 /**
0377  * ice_reg_pattern_test
0378  * @hw: pointer to the HW struct
0379  * @reg: reg to be tested
0380  * @mask: bits to be touched
0381  */
0382 static int ice_reg_pattern_test(struct ice_hw *hw, u32 reg, u32 mask)
0383 {
0384     struct ice_pf *pf = (struct ice_pf *)hw->back;
0385     struct device *dev = ice_pf_to_dev(pf);
0386     static const u32 patterns[] = {
0387         0x5A5A5A5A, 0xA5A5A5A5,
0388         0x00000000, 0xFFFFFFFF
0389     };
0390     u32 val, orig_val;
0391     unsigned int i;
0392 
0393     orig_val = rd32(hw, reg);
0394     for (i = 0; i < ARRAY_SIZE(patterns); ++i) {
0395         u32 pattern = patterns[i] & mask;
0396 
0397         wr32(hw, reg, pattern);
0398         val = rd32(hw, reg);
0399         if (val == pattern)
0400             continue;
0401         dev_err(dev, "%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n"
0402             , __func__, reg, pattern, val);
0403         return 1;
0404     }
0405 
0406     wr32(hw, reg, orig_val);
0407     val = rd32(hw, reg);
0408     if (val != orig_val) {
0409         dev_err(dev, "%s: reg restore test failed - reg 0x%08x orig 0x%08x val 0x%08x\n"
0410             , __func__, reg, orig_val, val);
0411         return 1;
0412     }
0413 
0414     return 0;
0415 }
0416 
0417 /**
0418  * ice_reg_test - perform a register test on a given net_device
0419  * @netdev: network interface device structure
0420  *
0421  * This function performs one of the self-tests required by ethtool.
0422  * Returns 0 on success, non-zero on failure.
0423  */
0424 static u64 ice_reg_test(struct net_device *netdev)
0425 {
0426     struct ice_netdev_priv *np = netdev_priv(netdev);
0427     struct ice_hw *hw = np->vsi->port_info->hw;
0428     u32 int_elements = hw->func_caps.common_cap.num_msix_vectors ?
0429         hw->func_caps.common_cap.num_msix_vectors - 1 : 1;
0430     struct ice_diag_reg_test_info {
0431         u32 address;
0432         u32 mask;
0433         u32 elem_num;
0434         u32 elem_size;
0435     } ice_reg_list[] = {
0436         {GLINT_ITR(0, 0), 0x00000fff, int_elements,
0437             GLINT_ITR(0, 1) - GLINT_ITR(0, 0)},
0438         {GLINT_ITR(1, 0), 0x00000fff, int_elements,
0439             GLINT_ITR(1, 1) - GLINT_ITR(1, 0)},
0440         {GLINT_ITR(0, 0), 0x00000fff, int_elements,
0441             GLINT_ITR(2, 1) - GLINT_ITR(2, 0)},
0442         {GLINT_CTL, 0xffff0001, 1, 0}
0443     };
0444     unsigned int i;
0445 
0446     netdev_dbg(netdev, "Register test\n");
0447     for (i = 0; i < ARRAY_SIZE(ice_reg_list); ++i) {
0448         u32 j;
0449 
0450         for (j = 0; j < ice_reg_list[i].elem_num; ++j) {
0451             u32 mask = ice_reg_list[i].mask;
0452             u32 reg = ice_reg_list[i].address +
0453                 (j * ice_reg_list[i].elem_size);
0454 
0455             /* bail on failure (non-zero return) */
0456             if (ice_reg_pattern_test(hw, reg, mask))
0457                 return 1;
0458         }
0459     }
0460 
0461     return 0;
0462 }
0463 
0464 /**
0465  * ice_lbtest_prepare_rings - configure Tx/Rx test rings
0466  * @vsi: pointer to the VSI structure
0467  *
0468  * Function configures rings of a VSI for loopback test without
0469  * enabling interrupts or informing the kernel about new queues.
0470  *
0471  * Returns 0 on success, negative on failure.
0472  */
0473 static int ice_lbtest_prepare_rings(struct ice_vsi *vsi)
0474 {
0475     int status;
0476 
0477     status = ice_vsi_setup_tx_rings(vsi);
0478     if (status)
0479         goto err_setup_tx_ring;
0480 
0481     status = ice_vsi_setup_rx_rings(vsi);
0482     if (status)
0483         goto err_setup_rx_ring;
0484 
0485     status = ice_vsi_cfg(vsi);
0486     if (status)
0487         goto err_setup_rx_ring;
0488 
0489     status = ice_vsi_start_all_rx_rings(vsi);
0490     if (status)
0491         goto err_start_rx_ring;
0492 
0493     return status;
0494 
0495 err_start_rx_ring:
0496     ice_vsi_free_rx_rings(vsi);
0497 err_setup_rx_ring:
0498     ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
0499 err_setup_tx_ring:
0500     ice_vsi_free_tx_rings(vsi);
0501 
0502     return status;
0503 }
0504 
0505 /**
0506  * ice_lbtest_disable_rings - disable Tx/Rx test rings after loopback test
0507  * @vsi: pointer to the VSI structure
0508  *
0509  * Function stops and frees VSI rings after a loopback test.
0510  * Returns 0 on success, negative on failure.
0511  */
0512 static int ice_lbtest_disable_rings(struct ice_vsi *vsi)
0513 {
0514     int status;
0515 
0516     status = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
0517     if (status)
0518         netdev_err(vsi->netdev, "Failed to stop Tx rings, VSI %d error %d\n",
0519                vsi->vsi_num, status);
0520 
0521     status = ice_vsi_stop_all_rx_rings(vsi);
0522     if (status)
0523         netdev_err(vsi->netdev, "Failed to stop Rx rings, VSI %d error %d\n",
0524                vsi->vsi_num, status);
0525 
0526     ice_vsi_free_tx_rings(vsi);
0527     ice_vsi_free_rx_rings(vsi);
0528 
0529     return status;
0530 }
0531 
0532 /**
0533  * ice_lbtest_create_frame - create test packet
0534  * @pf: pointer to the PF structure
0535  * @ret_data: allocated frame buffer
0536  * @size: size of the packet data
0537  *
0538  * Function allocates a frame with a test pattern on specific offsets.
0539  * Returns 0 on success, non-zero on failure.
0540  */
0541 static int ice_lbtest_create_frame(struct ice_pf *pf, u8 **ret_data, u16 size)
0542 {
0543     u8 *data;
0544 
0545     if (!pf)
0546         return -EINVAL;
0547 
0548     data = devm_kzalloc(ice_pf_to_dev(pf), size, GFP_KERNEL);
0549     if (!data)
0550         return -ENOMEM;
0551 
0552     /* Since the ethernet test frame should always be at least
0553      * 64 bytes long, fill some octets in the payload with test data.
0554      */
0555     memset(data, 0xFF, size);
0556     data[32] = 0xDE;
0557     data[42] = 0xAD;
0558     data[44] = 0xBE;
0559     data[46] = 0xEF;
0560 
0561     *ret_data = data;
0562 
0563     return 0;
0564 }
0565 
0566 /**
0567  * ice_lbtest_check_frame - verify received loopback frame
0568  * @frame: pointer to the raw packet data
0569  *
0570  * Function verifies received test frame with a pattern.
0571  * Returns true if frame matches the pattern, false otherwise.
0572  */
0573 static bool ice_lbtest_check_frame(u8 *frame)
0574 {
0575     /* Validate bytes of a frame under offsets chosen earlier */
0576     if (frame[32] == 0xDE &&
0577         frame[42] == 0xAD &&
0578         frame[44] == 0xBE &&
0579         frame[46] == 0xEF &&
0580         frame[48] == 0xFF)
0581         return true;
0582 
0583     return false;
0584 }
0585 
0586 /**
0587  * ice_diag_send - send test frames to the test ring
0588  * @tx_ring: pointer to the transmit ring
0589  * @data: pointer to the raw packet data
0590  * @size: size of the packet to send
0591  *
0592  * Function sends loopback packets on a test Tx ring.
0593  */
0594 static int ice_diag_send(struct ice_tx_ring *tx_ring, u8 *data, u16 size)
0595 {
0596     struct ice_tx_desc *tx_desc;
0597     struct ice_tx_buf *tx_buf;
0598     dma_addr_t dma;
0599     u64 td_cmd;
0600 
0601     tx_desc = ICE_TX_DESC(tx_ring, tx_ring->next_to_use);
0602     tx_buf = &tx_ring->tx_buf[tx_ring->next_to_use];
0603 
0604     dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
0605     if (dma_mapping_error(tx_ring->dev, dma))
0606         return -EINVAL;
0607 
0608     tx_desc->buf_addr = cpu_to_le64(dma);
0609 
0610     /* These flags are required for a descriptor to be pushed out */
0611     td_cmd = (u64)(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS);
0612     tx_desc->cmd_type_offset_bsz =
0613         cpu_to_le64(ICE_TX_DESC_DTYPE_DATA |
0614                 (td_cmd << ICE_TXD_QW1_CMD_S) |
0615                 ((u64)0 << ICE_TXD_QW1_OFFSET_S) |
0616                 ((u64)size << ICE_TXD_QW1_TX_BUF_SZ_S) |
0617                 ((u64)0 << ICE_TXD_QW1_L2TAG1_S));
0618 
0619     tx_buf->next_to_watch = tx_desc;
0620 
0621     /* Force memory write to complete before letting h/w know
0622      * there are new descriptors to fetch.
0623      */
0624     wmb();
0625 
0626     tx_ring->next_to_use++;
0627     if (tx_ring->next_to_use >= tx_ring->count)
0628         tx_ring->next_to_use = 0;
0629 
0630     writel_relaxed(tx_ring->next_to_use, tx_ring->tail);
0631 
0632     /* Wait until the packets get transmitted to the receive queue. */
0633     usleep_range(1000, 2000);
0634     dma_unmap_single(tx_ring->dev, dma, size, DMA_TO_DEVICE);
0635 
0636     return 0;
0637 }
0638 
0639 #define ICE_LB_FRAME_SIZE 64
0640 /**
0641  * ice_lbtest_receive_frames - receive and verify test frames
0642  * @rx_ring: pointer to the receive ring
0643  *
0644  * Function receives loopback packets and verify their correctness.
0645  * Returns number of received valid frames.
0646  */
0647 static int ice_lbtest_receive_frames(struct ice_rx_ring *rx_ring)
0648 {
0649     struct ice_rx_buf *rx_buf;
0650     int valid_frames, i;
0651     u8 *received_buf;
0652 
0653     valid_frames = 0;
0654 
0655     for (i = 0; i < rx_ring->count; i++) {
0656         union ice_32b_rx_flex_desc *rx_desc;
0657 
0658         rx_desc = ICE_RX_DESC(rx_ring, i);
0659 
0660         if (!(rx_desc->wb.status_error0 &
0661             (cpu_to_le16(BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S)) |
0662              cpu_to_le16(BIT(ICE_RX_FLEX_DESC_STATUS0_EOF_S)))))
0663             continue;
0664 
0665         rx_buf = &rx_ring->rx_buf[i];
0666         received_buf = page_address(rx_buf->page) + rx_buf->page_offset;
0667 
0668         if (ice_lbtest_check_frame(received_buf))
0669             valid_frames++;
0670     }
0671 
0672     return valid_frames;
0673 }
0674 
0675 /**
0676  * ice_loopback_test - perform a loopback test on a given net_device
0677  * @netdev: network interface device structure
0678  *
0679  * This function performs one of the self-tests required by ethtool.
0680  * Returns 0 on success, non-zero on failure.
0681  */
0682 static u64 ice_loopback_test(struct net_device *netdev)
0683 {
0684     struct ice_netdev_priv *np = netdev_priv(netdev);
0685     struct ice_vsi *orig_vsi = np->vsi, *test_vsi;
0686     struct ice_pf *pf = orig_vsi->back;
0687     u8 broadcast[ETH_ALEN], ret = 0;
0688     int num_frames, valid_frames;
0689     struct ice_tx_ring *tx_ring;
0690     struct ice_rx_ring *rx_ring;
0691     struct device *dev;
0692     u8 *tx_frame;
0693     int i;
0694 
0695     dev = ice_pf_to_dev(pf);
0696     netdev_info(netdev, "loopback test\n");
0697 
0698     test_vsi = ice_lb_vsi_setup(pf, pf->hw.port_info);
0699     if (!test_vsi) {
0700         netdev_err(netdev, "Failed to create a VSI for the loopback test\n");
0701         return 1;
0702     }
0703 
0704     test_vsi->netdev = netdev;
0705     tx_ring = test_vsi->tx_rings[0];
0706     rx_ring = test_vsi->rx_rings[0];
0707 
0708     if (ice_lbtest_prepare_rings(test_vsi)) {
0709         ret = 2;
0710         goto lbtest_vsi_close;
0711     }
0712 
0713     if (ice_alloc_rx_bufs(rx_ring, rx_ring->count)) {
0714         ret = 3;
0715         goto lbtest_rings_dis;
0716     }
0717 
0718     /* Enable MAC loopback in firmware */
0719     if (ice_aq_set_mac_loopback(&pf->hw, true, NULL)) {
0720         ret = 4;
0721         goto lbtest_mac_dis;
0722     }
0723 
0724     /* Test VSI needs to receive broadcast packets */
0725     eth_broadcast_addr(broadcast);
0726     if (ice_fltr_add_mac(test_vsi, broadcast, ICE_FWD_TO_VSI)) {
0727         ret = 5;
0728         goto lbtest_mac_dis;
0729     }
0730 
0731     if (ice_lbtest_create_frame(pf, &tx_frame, ICE_LB_FRAME_SIZE)) {
0732         ret = 7;
0733         goto remove_mac_filters;
0734     }
0735 
0736     num_frames = min_t(int, tx_ring->count, 32);
0737     for (i = 0; i < num_frames; i++) {
0738         if (ice_diag_send(tx_ring, tx_frame, ICE_LB_FRAME_SIZE)) {
0739             ret = 8;
0740             goto lbtest_free_frame;
0741         }
0742     }
0743 
0744     valid_frames = ice_lbtest_receive_frames(rx_ring);
0745     if (!valid_frames)
0746         ret = 9;
0747     else if (valid_frames != num_frames)
0748         ret = 10;
0749 
0750 lbtest_free_frame:
0751     devm_kfree(dev, tx_frame);
0752 remove_mac_filters:
0753     if (ice_fltr_remove_mac(test_vsi, broadcast, ICE_FWD_TO_VSI))
0754         netdev_err(netdev, "Could not remove MAC filter for the test VSI\n");
0755 lbtest_mac_dis:
0756     /* Disable MAC loopback after the test is completed. */
0757     if (ice_aq_set_mac_loopback(&pf->hw, false, NULL))
0758         netdev_err(netdev, "Could not disable MAC loopback\n");
0759 lbtest_rings_dis:
0760     if (ice_lbtest_disable_rings(test_vsi))
0761         netdev_err(netdev, "Could not disable test rings\n");
0762 lbtest_vsi_close:
0763     test_vsi->netdev = NULL;
0764     if (ice_vsi_release(test_vsi))
0765         netdev_err(netdev, "Failed to remove the test VSI\n");
0766 
0767     return ret;
0768 }
0769 
0770 /**
0771  * ice_intr_test - perform an interrupt test on a given net_device
0772  * @netdev: network interface device structure
0773  *
0774  * This function performs one of the self-tests required by ethtool.
0775  * Returns 0 on success, non-zero on failure.
0776  */
0777 static u64 ice_intr_test(struct net_device *netdev)
0778 {
0779     struct ice_netdev_priv *np = netdev_priv(netdev);
0780     struct ice_pf *pf = np->vsi->back;
0781     u16 swic_old = pf->sw_int_count;
0782 
0783     netdev_info(netdev, "interrupt test\n");
0784 
0785     wr32(&pf->hw, GLINT_DYN_CTL(pf->oicr_idx),
0786          GLINT_DYN_CTL_SW_ITR_INDX_M |
0787          GLINT_DYN_CTL_INTENA_MSK_M |
0788          GLINT_DYN_CTL_SWINT_TRIG_M);
0789 
0790     usleep_range(1000, 2000);
0791     return (swic_old == pf->sw_int_count);
0792 }
0793 
0794 /**
0795  * ice_self_test - handler function for performing a self-test by ethtool
0796  * @netdev: network interface device structure
0797  * @eth_test: ethtool_test structure
0798  * @data: required by ethtool.self_test
0799  *
0800  * This function is called after invoking 'ethtool -t devname' command where
0801  * devname is the name of the network device on which ethtool should operate.
0802  * It performs a set of self-tests to check if a device works properly.
0803  */
0804 static void
0805 ice_self_test(struct net_device *netdev, struct ethtool_test *eth_test,
0806           u64 *data)
0807 {
0808     struct ice_netdev_priv *np = netdev_priv(netdev);
0809     bool if_running = netif_running(netdev);
0810     struct ice_pf *pf = np->vsi->back;
0811     struct device *dev;
0812 
0813     dev = ice_pf_to_dev(pf);
0814 
0815     if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
0816         netdev_info(netdev, "offline testing starting\n");
0817 
0818         set_bit(ICE_TESTING, pf->state);
0819 
0820         if (ice_active_vfs(pf)) {
0821             dev_warn(dev, "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
0822             data[ICE_ETH_TEST_REG] = 1;
0823             data[ICE_ETH_TEST_EEPROM] = 1;
0824             data[ICE_ETH_TEST_INTR] = 1;
0825             data[ICE_ETH_TEST_LOOP] = 1;
0826             data[ICE_ETH_TEST_LINK] = 1;
0827             eth_test->flags |= ETH_TEST_FL_FAILED;
0828             clear_bit(ICE_TESTING, pf->state);
0829             goto skip_ol_tests;
0830         }
0831         /* If the device is online then take it offline */
0832         if (if_running)
0833             /* indicate we're in test mode */
0834             ice_stop(netdev);
0835 
0836         data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
0837         data[ICE_ETH_TEST_EEPROM] = ice_eeprom_test(netdev);
0838         data[ICE_ETH_TEST_INTR] = ice_intr_test(netdev);
0839         data[ICE_ETH_TEST_LOOP] = ice_loopback_test(netdev);
0840         data[ICE_ETH_TEST_REG] = ice_reg_test(netdev);
0841 
0842         if (data[ICE_ETH_TEST_LINK] ||
0843             data[ICE_ETH_TEST_EEPROM] ||
0844             data[ICE_ETH_TEST_LOOP] ||
0845             data[ICE_ETH_TEST_INTR] ||
0846             data[ICE_ETH_TEST_REG])
0847             eth_test->flags |= ETH_TEST_FL_FAILED;
0848 
0849         clear_bit(ICE_TESTING, pf->state);
0850 
0851         if (if_running) {
0852             int status = ice_open(netdev);
0853 
0854             if (status) {
0855                 dev_err(dev, "Could not open device %s, err %d\n",
0856                     pf->int_name, status);
0857             }
0858         }
0859     } else {
0860         /* Online tests */
0861         netdev_info(netdev, "online testing starting\n");
0862 
0863         data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
0864         if (data[ICE_ETH_TEST_LINK])
0865             eth_test->flags |= ETH_TEST_FL_FAILED;
0866 
0867         /* Offline only tests, not run in online; pass by default */
0868         data[ICE_ETH_TEST_REG] = 0;
0869         data[ICE_ETH_TEST_EEPROM] = 0;
0870         data[ICE_ETH_TEST_INTR] = 0;
0871         data[ICE_ETH_TEST_LOOP] = 0;
0872     }
0873 
0874 skip_ol_tests:
0875     netdev_info(netdev, "testing finished\n");
0876 }
0877 
0878 static void
0879 __ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data,
0880           struct ice_vsi *vsi)
0881 {
0882     unsigned int i;
0883     u8 *p = data;
0884 
0885     switch (stringset) {
0886     case ETH_SS_STATS:
0887         for (i = 0; i < ICE_VSI_STATS_LEN; i++)
0888             ethtool_sprintf(&p,
0889                     ice_gstrings_vsi_stats[i].stat_string);
0890 
0891         if (ice_is_port_repr_netdev(netdev))
0892             return;
0893 
0894         ice_for_each_alloc_txq(vsi, i) {
0895             ethtool_sprintf(&p, "tx_queue_%u_packets", i);
0896             ethtool_sprintf(&p, "tx_queue_%u_bytes", i);
0897         }
0898 
0899         ice_for_each_alloc_rxq(vsi, i) {
0900             ethtool_sprintf(&p, "rx_queue_%u_packets", i);
0901             ethtool_sprintf(&p, "rx_queue_%u_bytes", i);
0902         }
0903 
0904         if (vsi->type != ICE_VSI_PF)
0905             return;
0906 
0907         for (i = 0; i < ICE_PF_STATS_LEN; i++)
0908             ethtool_sprintf(&p,
0909                     ice_gstrings_pf_stats[i].stat_string);
0910 
0911         for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
0912             ethtool_sprintf(&p, "tx_priority_%u_xon.nic", i);
0913             ethtool_sprintf(&p, "tx_priority_%u_xoff.nic", i);
0914         }
0915         for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
0916             ethtool_sprintf(&p, "rx_priority_%u_xon.nic", i);
0917             ethtool_sprintf(&p, "rx_priority_%u_xoff.nic", i);
0918         }
0919         break;
0920     case ETH_SS_TEST:
0921         memcpy(data, ice_gstrings_test, ICE_TEST_LEN * ETH_GSTRING_LEN);
0922         break;
0923     case ETH_SS_PRIV_FLAGS:
0924         for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++)
0925             ethtool_sprintf(&p, ice_gstrings_priv_flags[i].name);
0926         break;
0927     default:
0928         break;
0929     }
0930 }
0931 
0932 static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
0933 {
0934     struct ice_netdev_priv *np = netdev_priv(netdev);
0935 
0936     __ice_get_strings(netdev, stringset, data, np->vsi);
0937 }
0938 
0939 static int
0940 ice_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
0941 {
0942     struct ice_netdev_priv *np = netdev_priv(netdev);
0943     bool led_active;
0944 
0945     switch (state) {
0946     case ETHTOOL_ID_ACTIVE:
0947         led_active = true;
0948         break;
0949     case ETHTOOL_ID_INACTIVE:
0950         led_active = false;
0951         break;
0952     default:
0953         return -EINVAL;
0954     }
0955 
0956     if (ice_aq_set_port_id_led(np->vsi->port_info, !led_active, NULL))
0957         return -EIO;
0958 
0959     return 0;
0960 }
0961 
0962 /**
0963  * ice_set_fec_cfg - Set link FEC options
0964  * @netdev: network interface device structure
0965  * @req_fec: FEC mode to configure
0966  */
0967 static int ice_set_fec_cfg(struct net_device *netdev, enum ice_fec_mode req_fec)
0968 {
0969     struct ice_netdev_priv *np = netdev_priv(netdev);
0970     struct ice_aqc_set_phy_cfg_data config = { 0 };
0971     struct ice_vsi *vsi = np->vsi;
0972     struct ice_port_info *pi;
0973 
0974     pi = vsi->port_info;
0975     if (!pi)
0976         return -EOPNOTSUPP;
0977 
0978     /* Changing the FEC parameters is not supported if not the PF VSI */
0979     if (vsi->type != ICE_VSI_PF) {
0980         netdev_info(netdev, "Changing FEC parameters only supported for PF VSI\n");
0981         return -EOPNOTSUPP;
0982     }
0983 
0984     /* Proceed only if requesting different FEC mode */
0985     if (pi->phy.curr_user_fec_req == req_fec)
0986         return 0;
0987 
0988     /* Copy the current user PHY configuration. The current user PHY
0989      * configuration is initialized during probe from PHY capabilities
0990      * software mode, and updated on set PHY configuration.
0991      */
0992     memcpy(&config, &pi->phy.curr_user_phy_cfg, sizeof(config));
0993 
0994     ice_cfg_phy_fec(pi, &config, req_fec);
0995     config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
0996 
0997     if (ice_aq_set_phy_cfg(pi->hw, pi, &config, NULL))
0998         return -EAGAIN;
0999 
1000     /* Save requested FEC config */
1001     pi->phy.curr_user_fec_req = req_fec;
1002 
1003     return 0;
1004 }
1005 
1006 /**
1007  * ice_set_fecparam - Set FEC link options
1008  * @netdev: network interface device structure
1009  * @fecparam: Ethtool structure to retrieve FEC parameters
1010  */
1011 static int
1012 ice_set_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1013 {
1014     struct ice_netdev_priv *np = netdev_priv(netdev);
1015     struct ice_vsi *vsi = np->vsi;
1016     enum ice_fec_mode fec;
1017 
1018     switch (fecparam->fec) {
1019     case ETHTOOL_FEC_AUTO:
1020         fec = ICE_FEC_AUTO;
1021         break;
1022     case ETHTOOL_FEC_RS:
1023         fec = ICE_FEC_RS;
1024         break;
1025     case ETHTOOL_FEC_BASER:
1026         fec = ICE_FEC_BASER;
1027         break;
1028     case ETHTOOL_FEC_OFF:
1029     case ETHTOOL_FEC_NONE:
1030         fec = ICE_FEC_NONE;
1031         break;
1032     default:
1033         dev_warn(ice_pf_to_dev(vsi->back), "Unsupported FEC mode: %d\n",
1034              fecparam->fec);
1035         return -EINVAL;
1036     }
1037 
1038     return ice_set_fec_cfg(netdev, fec);
1039 }
1040 
1041 /**
1042  * ice_get_fecparam - Get link FEC options
1043  * @netdev: network interface device structure
1044  * @fecparam: Ethtool structure to retrieve FEC parameters
1045  */
1046 static int
1047 ice_get_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1048 {
1049     struct ice_netdev_priv *np = netdev_priv(netdev);
1050     struct ice_aqc_get_phy_caps_data *caps;
1051     struct ice_link_status *link_info;
1052     struct ice_vsi *vsi = np->vsi;
1053     struct ice_port_info *pi;
1054     int err;
1055 
1056     pi = vsi->port_info;
1057 
1058     if (!pi)
1059         return -EOPNOTSUPP;
1060     link_info = &pi->phy.link_info;
1061 
1062     /* Set FEC mode based on negotiated link info */
1063     switch (link_info->fec_info) {
1064     case ICE_AQ_LINK_25G_KR_FEC_EN:
1065         fecparam->active_fec = ETHTOOL_FEC_BASER;
1066         break;
1067     case ICE_AQ_LINK_25G_RS_528_FEC_EN:
1068     case ICE_AQ_LINK_25G_RS_544_FEC_EN:
1069         fecparam->active_fec = ETHTOOL_FEC_RS;
1070         break;
1071     default:
1072         fecparam->active_fec = ETHTOOL_FEC_OFF;
1073         break;
1074     }
1075 
1076     caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1077     if (!caps)
1078         return -ENOMEM;
1079 
1080     err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
1081                   caps, NULL);
1082     if (err)
1083         goto done;
1084 
1085     /* Set supported/configured FEC modes based on PHY capability */
1086     if (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC)
1087         fecparam->fec |= ETHTOOL_FEC_AUTO;
1088     if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
1089         caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
1090         caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN ||
1091         caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
1092         fecparam->fec |= ETHTOOL_FEC_BASER;
1093     if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
1094         caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ ||
1095         caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
1096         fecparam->fec |= ETHTOOL_FEC_RS;
1097     if (caps->link_fec_options == 0)
1098         fecparam->fec |= ETHTOOL_FEC_OFF;
1099 
1100 done:
1101     kfree(caps);
1102     return err;
1103 }
1104 
1105 /**
1106  * ice_nway_reset - restart autonegotiation
1107  * @netdev: network interface device structure
1108  */
1109 static int ice_nway_reset(struct net_device *netdev)
1110 {
1111     struct ice_netdev_priv *np = netdev_priv(netdev);
1112     struct ice_vsi *vsi = np->vsi;
1113     int err;
1114 
1115     /* If VSI state is up, then restart autoneg with link up */
1116     if (!test_bit(ICE_DOWN, vsi->back->state))
1117         err = ice_set_link(vsi, true);
1118     else
1119         err = ice_set_link(vsi, false);
1120 
1121     return err;
1122 }
1123 
1124 /**
1125  * ice_get_priv_flags - report device private flags
1126  * @netdev: network interface device structure
1127  *
1128  * The get string set count and the string set should be matched for each
1129  * flag returned.  Add new strings for each flag to the ice_gstrings_priv_flags
1130  * array.
1131  *
1132  * Returns a u32 bitmap of flags.
1133  */
1134 static u32 ice_get_priv_flags(struct net_device *netdev)
1135 {
1136     struct ice_netdev_priv *np = netdev_priv(netdev);
1137     struct ice_vsi *vsi = np->vsi;
1138     struct ice_pf *pf = vsi->back;
1139     u32 i, ret_flags = 0;
1140 
1141     for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1142         const struct ice_priv_flag *priv_flag;
1143 
1144         priv_flag = &ice_gstrings_priv_flags[i];
1145 
1146         if (test_bit(priv_flag->bitno, pf->flags))
1147             ret_flags |= BIT(i);
1148     }
1149 
1150     return ret_flags;
1151 }
1152 
1153 /**
1154  * ice_set_priv_flags - set private flags
1155  * @netdev: network interface device structure
1156  * @flags: bit flags to be set
1157  */
1158 static int ice_set_priv_flags(struct net_device *netdev, u32 flags)
1159 {
1160     struct ice_netdev_priv *np = netdev_priv(netdev);
1161     DECLARE_BITMAP(change_flags, ICE_PF_FLAGS_NBITS);
1162     DECLARE_BITMAP(orig_flags, ICE_PF_FLAGS_NBITS);
1163     struct ice_vsi *vsi = np->vsi;
1164     struct ice_pf *pf = vsi->back;
1165     struct device *dev;
1166     int ret = 0;
1167     u32 i;
1168 
1169     if (flags > BIT(ICE_PRIV_FLAG_ARRAY_SIZE))
1170         return -EINVAL;
1171 
1172     dev = ice_pf_to_dev(pf);
1173     set_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1174 
1175     bitmap_copy(orig_flags, pf->flags, ICE_PF_FLAGS_NBITS);
1176     for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1177         const struct ice_priv_flag *priv_flag;
1178 
1179         priv_flag = &ice_gstrings_priv_flags[i];
1180 
1181         if (flags & BIT(i))
1182             set_bit(priv_flag->bitno, pf->flags);
1183         else
1184             clear_bit(priv_flag->bitno, pf->flags);
1185     }
1186 
1187     bitmap_xor(change_flags, pf->flags, orig_flags, ICE_PF_FLAGS_NBITS);
1188 
1189     /* Do not allow change to link-down-on-close when Total Port Shutdown
1190      * is enabled.
1191      */
1192     if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, change_flags) &&
1193         test_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
1194         dev_err(dev, "Setting link-down-on-close not supported on this port\n");
1195         set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
1196         ret = -EINVAL;
1197         goto ethtool_exit;
1198     }
1199 
1200     if (test_bit(ICE_FLAG_FW_LLDP_AGENT, change_flags)) {
1201         if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags)) {
1202             int status;
1203 
1204             /* Disable FW LLDP engine */
1205             status = ice_cfg_lldp_mib_change(&pf->hw, false);
1206 
1207             /* If unregistering for LLDP events fails, this is
1208              * not an error state, as there shouldn't be any
1209              * events to respond to.
1210              */
1211             if (status)
1212                 dev_info(dev, "Failed to unreg for LLDP events\n");
1213 
1214             /* The AQ call to stop the FW LLDP agent will generate
1215              * an error if the agent is already stopped.
1216              */
1217             status = ice_aq_stop_lldp(&pf->hw, true, true, NULL);
1218             if (status)
1219                 dev_warn(dev, "Fail to stop LLDP agent\n");
1220             /* Use case for having the FW LLDP agent stopped
1221              * will likely not need DCB, so failure to init is
1222              * not a concern of ethtool
1223              */
1224             status = ice_init_pf_dcb(pf, true);
1225             if (status)
1226                 dev_warn(dev, "Fail to init DCB\n");
1227 
1228             pf->dcbx_cap &= ~DCB_CAP_DCBX_LLD_MANAGED;
1229             pf->dcbx_cap |= DCB_CAP_DCBX_HOST;
1230         } else {
1231             bool dcbx_agent_status;
1232             int status;
1233 
1234             if (ice_get_pfc_mode(pf) == ICE_QOS_MODE_DSCP) {
1235                 clear_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
1236                 dev_err(dev, "QoS in L3 DSCP mode, FW Agent not allowed to start\n");
1237                 ret = -EOPNOTSUPP;
1238                 goto ethtool_exit;
1239             }
1240 
1241             /* Remove rule to direct LLDP packets to default VSI.
1242              * The FW LLDP engine will now be consuming them.
1243              */
1244             ice_cfg_sw_lldp(vsi, false, false);
1245 
1246             /* AQ command to start FW LLDP agent will return an
1247              * error if the agent is already started
1248              */
1249             status = ice_aq_start_lldp(&pf->hw, true, NULL);
1250             if (status)
1251                 dev_warn(dev, "Fail to start LLDP Agent\n");
1252 
1253             /* AQ command to start FW DCBX agent will fail if
1254              * the agent is already started
1255              */
1256             status = ice_aq_start_stop_dcbx(&pf->hw, true,
1257                             &dcbx_agent_status,
1258                             NULL);
1259             if (status)
1260                 dev_dbg(dev, "Failed to start FW DCBX\n");
1261 
1262             dev_info(dev, "FW DCBX agent is %s\n",
1263                  dcbx_agent_status ? "ACTIVE" : "DISABLED");
1264 
1265             /* Failure to configure MIB change or init DCB is not
1266              * relevant to ethtool.  Print notification that
1267              * registration/init failed but do not return error
1268              * state to ethtool
1269              */
1270             status = ice_init_pf_dcb(pf, true);
1271             if (status)
1272                 dev_dbg(dev, "Fail to init DCB\n");
1273 
1274             /* Register for MIB change events */
1275             status = ice_cfg_lldp_mib_change(&pf->hw, true);
1276             if (status)
1277                 dev_dbg(dev, "Fail to enable MIB change events\n");
1278 
1279             pf->dcbx_cap &= ~DCB_CAP_DCBX_HOST;
1280             pf->dcbx_cap |= DCB_CAP_DCBX_LLD_MANAGED;
1281 
1282             ice_nway_reset(netdev);
1283         }
1284     }
1285     if (test_bit(ICE_FLAG_LEGACY_RX, change_flags)) {
1286         /* down and up VSI so that changes of Rx cfg are reflected. */
1287         if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
1288             ice_down(vsi);
1289             ice_up(vsi);
1290         }
1291     }
1292     /* don't allow modification of this flag when a single VF is in
1293      * promiscuous mode because it's not supported
1294      */
1295     if (test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, change_flags) &&
1296         ice_is_any_vf_in_unicast_promisc(pf)) {
1297         dev_err(dev, "Changing vf-true-promisc-support flag while VF(s) are in promiscuous mode not supported\n");
1298         /* toggle bit back to previous state */
1299         change_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags);
1300         ret = -EAGAIN;
1301     }
1302 
1303     if (test_bit(ICE_FLAG_VF_VLAN_PRUNING, change_flags) &&
1304         ice_has_vfs(pf)) {
1305         dev_err(dev, "vf-vlan-pruning: VLAN pruning cannot be changed while VFs are active.\n");
1306         /* toggle bit back to previous state */
1307         change_bit(ICE_FLAG_VF_VLAN_PRUNING, pf->flags);
1308         ret = -EOPNOTSUPP;
1309     }
1310 ethtool_exit:
1311     clear_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1312     return ret;
1313 }
1314 
1315 static 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 
1347 static void
1348 __ice_get_ethtool_stats(struct net_device *netdev,
1349             struct ethtool_stats __always_unused *stats, u64 *data,
1350             struct ice_vsi *vsi)
1351 {
1352     struct ice_pf *pf = vsi->back;
1353     struct ice_tx_ring *tx_ring;
1354     struct ice_rx_ring *rx_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     if (ice_is_port_repr_netdev(netdev))
1369         return;
1370 
1371     /* populate per queue stats */
1372     rcu_read_lock();
1373 
1374     ice_for_each_alloc_txq(vsi, j) {
1375         tx_ring = READ_ONCE(vsi->tx_rings[j]);
1376         if (tx_ring) {
1377             data[i++] = tx_ring->stats.pkts;
1378             data[i++] = tx_ring->stats.bytes;
1379         } else {
1380             data[i++] = 0;
1381             data[i++] = 0;
1382         }
1383     }
1384 
1385     ice_for_each_alloc_rxq(vsi, j) {
1386         rx_ring = READ_ONCE(vsi->rx_rings[j]);
1387         if (rx_ring) {
1388             data[i++] = rx_ring->stats.pkts;
1389             data[i++] = rx_ring->stats.bytes;
1390         } else {
1391             data[i++] = 0;
1392             data[i++] = 0;
1393         }
1394     }
1395 
1396     rcu_read_unlock();
1397 
1398     if (vsi->type != ICE_VSI_PF)
1399         return;
1400 
1401     for (j = 0; j < ICE_PF_STATS_LEN; j++) {
1402         p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset;
1403         data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat ==
1404                  sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1405     }
1406 
1407     for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1408         data[i++] = pf->stats.priority_xon_tx[j];
1409         data[i++] = pf->stats.priority_xoff_tx[j];
1410     }
1411 
1412     for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1413         data[i++] = pf->stats.priority_xon_rx[j];
1414         data[i++] = pf->stats.priority_xoff_rx[j];
1415     }
1416 }
1417 
1418 static void
1419 ice_get_ethtool_stats(struct net_device *netdev,
1420               struct ethtool_stats __always_unused *stats, u64 *data)
1421 {
1422     struct ice_netdev_priv *np = netdev_priv(netdev);
1423 
1424     __ice_get_ethtool_stats(netdev, stats, data, np->vsi);
1425 }
1426 
1427 #define ICE_PHY_TYPE_LOW_MASK_MIN_1G    (ICE_PHY_TYPE_LOW_100BASE_TX | \
1428                      ICE_PHY_TYPE_LOW_100M_SGMII)
1429 
1430 #define ICE_PHY_TYPE_LOW_MASK_MIN_25G   (ICE_PHY_TYPE_LOW_MASK_MIN_1G | \
1431                      ICE_PHY_TYPE_LOW_1000BASE_T | \
1432                      ICE_PHY_TYPE_LOW_1000BASE_SX | \
1433                      ICE_PHY_TYPE_LOW_1000BASE_LX | \
1434                      ICE_PHY_TYPE_LOW_1000BASE_KX | \
1435                      ICE_PHY_TYPE_LOW_1G_SGMII | \
1436                      ICE_PHY_TYPE_LOW_2500BASE_T | \
1437                      ICE_PHY_TYPE_LOW_2500BASE_X | \
1438                      ICE_PHY_TYPE_LOW_2500BASE_KX | \
1439                      ICE_PHY_TYPE_LOW_5GBASE_T | \
1440                      ICE_PHY_TYPE_LOW_5GBASE_KR | \
1441                      ICE_PHY_TYPE_LOW_10GBASE_T | \
1442                      ICE_PHY_TYPE_LOW_10G_SFI_DA | \
1443                      ICE_PHY_TYPE_LOW_10GBASE_SR | \
1444                      ICE_PHY_TYPE_LOW_10GBASE_LR | \
1445                      ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 | \
1446                      ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC | \
1447                      ICE_PHY_TYPE_LOW_10G_SFI_C2C)
1448 
1449 #define ICE_PHY_TYPE_LOW_MASK_100G  (ICE_PHY_TYPE_LOW_100GBASE_CR4 | \
1450                      ICE_PHY_TYPE_LOW_100GBASE_SR4 | \
1451                      ICE_PHY_TYPE_LOW_100GBASE_LR4 | \
1452                      ICE_PHY_TYPE_LOW_100GBASE_KR4 | \
1453                      ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC | \
1454                      ICE_PHY_TYPE_LOW_100G_CAUI4 | \
1455                      ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC | \
1456                      ICE_PHY_TYPE_LOW_100G_AUI4 | \
1457                      ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 | \
1458                      ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 | \
1459                      ICE_PHY_TYPE_LOW_100GBASE_CP2 | \
1460                      ICE_PHY_TYPE_LOW_100GBASE_SR2 | \
1461                      ICE_PHY_TYPE_LOW_100GBASE_DR)
1462 
1463 #define ICE_PHY_TYPE_HIGH_MASK_100G (ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4 | \
1464                      ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC |\
1465                      ICE_PHY_TYPE_HIGH_100G_CAUI2 | \
1466                      ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC | \
1467                      ICE_PHY_TYPE_HIGH_100G_AUI2)
1468 
1469 /**
1470  * ice_mask_min_supported_speeds
1471  * @phy_types_high: PHY type high
1472  * @phy_types_low: PHY type low to apply minimum supported speeds mask
1473  *
1474  * Apply minimum supported speeds mask to PHY type low. These are the speeds
1475  * for ethtool supported link mode.
1476  */
1477 static
1478 void ice_mask_min_supported_speeds(u64 phy_types_high, u64 *phy_types_low)
1479 {
1480     /* if QSFP connection with 100G speed, minimum supported speed is 25G */
1481     if (*phy_types_low & ICE_PHY_TYPE_LOW_MASK_100G ||
1482         phy_types_high & ICE_PHY_TYPE_HIGH_MASK_100G)
1483         *phy_types_low &= ~ICE_PHY_TYPE_LOW_MASK_MIN_25G;
1484     else
1485         *phy_types_low &= ~ICE_PHY_TYPE_LOW_MASK_MIN_1G;
1486 }
1487 
1488 #define ice_ethtool_advertise_link_mode(aq_link_speed, ethtool_link_mode)    \
1489     do {                                     \
1490         if (req_speeds & (aq_link_speed) ||              \
1491             (!req_speeds &&                      \
1492              (advert_phy_type_lo & phy_type_mask_lo ||           \
1493               advert_phy_type_hi & phy_type_mask_hi)))           \
1494             ethtool_link_ksettings_add_link_mode(ks, advertising,\
1495                             ethtool_link_mode);  \
1496     } while (0)
1497 
1498 /**
1499  * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes
1500  * @netdev: network interface device structure
1501  * @ks: ethtool link ksettings struct to fill out
1502  */
1503 static void
1504 ice_phy_type_to_ethtool(struct net_device *netdev,
1505             struct ethtool_link_ksettings *ks)
1506 {
1507     struct ice_netdev_priv *np = netdev_priv(netdev);
1508     struct ice_vsi *vsi = np->vsi;
1509     struct ice_pf *pf = vsi->back;
1510     u64 advert_phy_type_lo = 0;
1511     u64 advert_phy_type_hi = 0;
1512     u64 phy_type_mask_lo = 0;
1513     u64 phy_type_mask_hi = 0;
1514     u64 phy_types_high = 0;
1515     u64 phy_types_low = 0;
1516     u16 req_speeds;
1517 
1518     req_speeds = vsi->port_info->phy.link_info.req_speeds;
1519 
1520     /* Check if lenient mode is supported and enabled, or in strict mode.
1521      *
1522      * In lenient mode the Supported link modes are the PHY types without
1523      * media. The Advertising link mode is either 1. the user requested
1524      * speed, 2. the override PHY mask, or 3. the PHY types with media.
1525      *
1526      * In strict mode Supported link mode are the PHY type with media,
1527      * and Advertising link modes are the media PHY type or the speed
1528      * requested by user.
1529      */
1530     if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags)) {
1531         phy_types_low = le64_to_cpu(pf->nvm_phy_type_lo);
1532         phy_types_high = le64_to_cpu(pf->nvm_phy_type_hi);
1533 
1534         ice_mask_min_supported_speeds(phy_types_high, &phy_types_low);
1535         /* determine advertised modes based on link override only
1536          * if it's supported and if the FW doesn't abstract the
1537          * driver from having to account for link overrides
1538          */
1539         if (ice_fw_supports_link_override(&pf->hw) &&
1540             !ice_fw_supports_report_dflt_cfg(&pf->hw)) {
1541             struct ice_link_default_override_tlv *ldo;
1542 
1543             ldo = &pf->link_dflt_override;
1544             /* If override enabled and PHY mask set, then
1545              * Advertising link mode is the intersection of the PHY
1546              * types without media and the override PHY mask.
1547              */
1548             if (ldo->options & ICE_LINK_OVERRIDE_EN &&
1549                 (ldo->phy_type_low || ldo->phy_type_high)) {
1550                 advert_phy_type_lo =
1551                     le64_to_cpu(pf->nvm_phy_type_lo) &
1552                     ldo->phy_type_low;
1553                 advert_phy_type_hi =
1554                     le64_to_cpu(pf->nvm_phy_type_hi) &
1555                     ldo->phy_type_high;
1556             }
1557         }
1558     } else {
1559         /* strict mode */
1560         phy_types_low = vsi->port_info->phy.phy_type_low;
1561         phy_types_high = vsi->port_info->phy.phy_type_high;
1562     }
1563 
1564     /* If Advertising link mode PHY type is not using override PHY type,
1565      * then use PHY type with media.
1566      */
1567     if (!advert_phy_type_lo && !advert_phy_type_hi) {
1568         advert_phy_type_lo = vsi->port_info->phy.phy_type_low;
1569         advert_phy_type_hi = vsi->port_info->phy.phy_type_high;
1570     }
1571 
1572     ethtool_link_ksettings_zero_link_mode(ks, supported);
1573     ethtool_link_ksettings_zero_link_mode(ks, advertising);
1574 
1575     phy_type_mask_lo = ICE_PHY_TYPE_LOW_100BASE_TX |
1576                ICE_PHY_TYPE_LOW_100M_SGMII;
1577     if (phy_types_low & phy_type_mask_lo) {
1578         ethtool_link_ksettings_add_link_mode(ks, supported,
1579                              100baseT_Full);
1580 
1581         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100MB,
1582                         100baseT_Full);
1583     }
1584 
1585     phy_type_mask_lo = ICE_PHY_TYPE_LOW_1000BASE_T |
1586                ICE_PHY_TYPE_LOW_1G_SGMII;
1587     if (phy_types_low & phy_type_mask_lo) {
1588         ethtool_link_ksettings_add_link_mode(ks, supported,
1589                              1000baseT_Full);
1590         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_1000MB,
1591                         1000baseT_Full);
1592     }
1593 
1594     phy_type_mask_lo = ICE_PHY_TYPE_LOW_1000BASE_KX;
1595     if (phy_types_low & phy_type_mask_lo) {
1596         ethtool_link_ksettings_add_link_mode(ks, supported,
1597                              1000baseKX_Full);
1598         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_1000MB,
1599                         1000baseKX_Full);
1600     }
1601 
1602     phy_type_mask_lo = ICE_PHY_TYPE_LOW_1000BASE_SX |
1603                ICE_PHY_TYPE_LOW_1000BASE_LX;
1604     if (phy_types_low & phy_type_mask_lo) {
1605         ethtool_link_ksettings_add_link_mode(ks, supported,
1606                              1000baseX_Full);
1607         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_1000MB,
1608                         1000baseX_Full);
1609     }
1610 
1611     phy_type_mask_lo = ICE_PHY_TYPE_LOW_2500BASE_T;
1612     if (phy_types_low & phy_type_mask_lo) {
1613         ethtool_link_ksettings_add_link_mode(ks, supported,
1614                              2500baseT_Full);
1615         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_2500MB,
1616                         2500baseT_Full);
1617     }
1618 
1619     phy_type_mask_lo = ICE_PHY_TYPE_LOW_2500BASE_X |
1620                ICE_PHY_TYPE_LOW_2500BASE_KX;
1621     if (phy_types_low & phy_type_mask_lo) {
1622         ethtool_link_ksettings_add_link_mode(ks, supported,
1623                              2500baseX_Full);
1624         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_2500MB,
1625                         2500baseX_Full);
1626     }
1627 
1628     phy_type_mask_lo = ICE_PHY_TYPE_LOW_5GBASE_T |
1629                ICE_PHY_TYPE_LOW_5GBASE_KR;
1630     if (phy_types_low & phy_type_mask_lo) {
1631         ethtool_link_ksettings_add_link_mode(ks, supported,
1632                              5000baseT_Full);
1633         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_5GB,
1634                         5000baseT_Full);
1635     }
1636 
1637     phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_T |
1638                ICE_PHY_TYPE_LOW_10G_SFI_DA |
1639                ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC |
1640                ICE_PHY_TYPE_LOW_10G_SFI_C2C;
1641     if (phy_types_low & phy_type_mask_lo) {
1642         ethtool_link_ksettings_add_link_mode(ks, supported,
1643                              10000baseT_Full);
1644         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
1645                         10000baseT_Full);
1646     }
1647 
1648     phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_KR_CR1;
1649     if (phy_types_low & phy_type_mask_lo) {
1650         ethtool_link_ksettings_add_link_mode(ks, supported,
1651                              10000baseKR_Full);
1652         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
1653                         10000baseKR_Full);
1654     }
1655 
1656     phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_SR;
1657     if (phy_types_low & phy_type_mask_lo) {
1658         ethtool_link_ksettings_add_link_mode(ks, supported,
1659                              10000baseSR_Full);
1660         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
1661                         10000baseSR_Full);
1662     }
1663 
1664     phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_LR;
1665     if (phy_types_low & phy_type_mask_lo) {
1666         ethtool_link_ksettings_add_link_mode(ks, supported,
1667                              10000baseLR_Full);
1668         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
1669                         10000baseLR_Full);
1670     }
1671 
1672     phy_type_mask_lo = ICE_PHY_TYPE_LOW_25GBASE_T |
1673                ICE_PHY_TYPE_LOW_25GBASE_CR |
1674                ICE_PHY_TYPE_LOW_25GBASE_CR_S |
1675                ICE_PHY_TYPE_LOW_25GBASE_CR1 |
1676                ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC |
1677                ICE_PHY_TYPE_LOW_25G_AUI_C2C;
1678     if (phy_types_low & phy_type_mask_lo) {
1679         ethtool_link_ksettings_add_link_mode(ks, supported,
1680                              25000baseCR_Full);
1681         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_25GB,
1682                         25000baseCR_Full);
1683     }
1684 
1685     phy_type_mask_lo = ICE_PHY_TYPE_LOW_25GBASE_SR |
1686                ICE_PHY_TYPE_LOW_25GBASE_LR;
1687     if (phy_types_low & phy_type_mask_lo) {
1688         ethtool_link_ksettings_add_link_mode(ks, supported,
1689                              25000baseSR_Full);
1690         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_25GB,
1691                         25000baseSR_Full);
1692     }
1693 
1694     phy_type_mask_lo = ICE_PHY_TYPE_LOW_25GBASE_KR |
1695                ICE_PHY_TYPE_LOW_25GBASE_KR_S |
1696                ICE_PHY_TYPE_LOW_25GBASE_KR1;
1697     if (phy_types_low & phy_type_mask_lo) {
1698         ethtool_link_ksettings_add_link_mode(ks, supported,
1699                              25000baseKR_Full);
1700         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_25GB,
1701                         25000baseKR_Full);
1702     }
1703 
1704     phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_KR4;
1705     if (phy_types_low & phy_type_mask_lo) {
1706         ethtool_link_ksettings_add_link_mode(ks, supported,
1707                              40000baseKR4_Full);
1708         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
1709                         40000baseKR4_Full);
1710     }
1711 
1712     phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_CR4 |
1713                ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC |
1714                ICE_PHY_TYPE_LOW_40G_XLAUI;
1715     if (phy_types_low & phy_type_mask_lo) {
1716         ethtool_link_ksettings_add_link_mode(ks, supported,
1717                              40000baseCR4_Full);
1718         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
1719                         40000baseCR4_Full);
1720     }
1721 
1722     phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_SR4;
1723     if (phy_types_low & phy_type_mask_lo) {
1724         ethtool_link_ksettings_add_link_mode(ks, supported,
1725                              40000baseSR4_Full);
1726         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
1727                         40000baseSR4_Full);
1728     }
1729 
1730     phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_LR4;
1731     if (phy_types_low & phy_type_mask_lo) {
1732         ethtool_link_ksettings_add_link_mode(ks, supported,
1733                              40000baseLR4_Full);
1734         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
1735                         40000baseLR4_Full);
1736     }
1737 
1738     phy_type_mask_lo = ICE_PHY_TYPE_LOW_50GBASE_CR2 |
1739                ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC |
1740                ICE_PHY_TYPE_LOW_50G_LAUI2 |
1741                ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC |
1742                ICE_PHY_TYPE_LOW_50G_AUI2 |
1743                ICE_PHY_TYPE_LOW_50GBASE_CP |
1744                ICE_PHY_TYPE_LOW_50GBASE_SR |
1745                ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC |
1746                ICE_PHY_TYPE_LOW_50G_AUI1;
1747     if (phy_types_low & phy_type_mask_lo) {
1748         ethtool_link_ksettings_add_link_mode(ks, supported,
1749                              50000baseCR2_Full);
1750         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_50GB,
1751                         50000baseCR2_Full);
1752     }
1753 
1754     phy_type_mask_lo = ICE_PHY_TYPE_LOW_50GBASE_KR2 |
1755                ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4;
1756     if (phy_types_low & phy_type_mask_lo) {
1757         ethtool_link_ksettings_add_link_mode(ks, supported,
1758                              50000baseKR2_Full);
1759         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_50GB,
1760                         50000baseKR2_Full);
1761     }
1762 
1763     phy_type_mask_lo = ICE_PHY_TYPE_LOW_50GBASE_SR2 |
1764                ICE_PHY_TYPE_LOW_50GBASE_LR2 |
1765                ICE_PHY_TYPE_LOW_50GBASE_FR |
1766                ICE_PHY_TYPE_LOW_50GBASE_LR;
1767     if (phy_types_low & phy_type_mask_lo) {
1768         ethtool_link_ksettings_add_link_mode(ks, supported,
1769                              50000baseSR2_Full);
1770         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_50GB,
1771                         50000baseSR2_Full);
1772     }
1773 
1774     phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_CR4 |
1775                ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC |
1776                ICE_PHY_TYPE_LOW_100G_CAUI4 |
1777                ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC |
1778                ICE_PHY_TYPE_LOW_100G_AUI4 |
1779                ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 |
1780                ICE_PHY_TYPE_LOW_100GBASE_CP2;
1781     phy_type_mask_hi = ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC |
1782                ICE_PHY_TYPE_HIGH_100G_CAUI2 |
1783                ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC |
1784                ICE_PHY_TYPE_HIGH_100G_AUI2;
1785     if (phy_types_low & phy_type_mask_lo ||
1786         phy_types_high & phy_type_mask_hi) {
1787         ethtool_link_ksettings_add_link_mode(ks, supported,
1788                              100000baseCR4_Full);
1789         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
1790                         100000baseCR4_Full);
1791     }
1792 
1793     phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_SR4 |
1794                ICE_PHY_TYPE_LOW_100GBASE_SR2;
1795     if (phy_types_low & phy_type_mask_lo) {
1796         ethtool_link_ksettings_add_link_mode(ks, supported,
1797                              100000baseSR4_Full);
1798         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
1799                         100000baseSR4_Full);
1800     }
1801 
1802     phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_LR4 |
1803                ICE_PHY_TYPE_LOW_100GBASE_DR;
1804     if (phy_types_low & phy_type_mask_lo) {
1805         ethtool_link_ksettings_add_link_mode(ks, supported,
1806                              100000baseLR4_ER4_Full);
1807         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
1808                         100000baseLR4_ER4_Full);
1809     }
1810 
1811     phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_KR4 |
1812                ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4;
1813     phy_type_mask_hi = ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4;
1814     if (phy_types_low & phy_type_mask_lo ||
1815         phy_types_high & phy_type_mask_hi) {
1816         ethtool_link_ksettings_add_link_mode(ks, supported,
1817                              100000baseKR4_Full);
1818         ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
1819                         100000baseKR4_Full);
1820     }
1821 }
1822 
1823 #define TEST_SET_BITS_TIMEOUT   50
1824 #define TEST_SET_BITS_SLEEP_MAX 2000
1825 #define TEST_SET_BITS_SLEEP_MIN 1000
1826 
1827 /**
1828  * ice_get_settings_link_up - Get Link settings for when link is up
1829  * @ks: ethtool ksettings to fill in
1830  * @netdev: network interface device structure
1831  */
1832 static void
1833 ice_get_settings_link_up(struct ethtool_link_ksettings *ks,
1834              struct net_device *netdev)
1835 {
1836     struct ice_netdev_priv *np = netdev_priv(netdev);
1837     struct ice_port_info *pi = np->vsi->port_info;
1838     struct ice_link_status *link_info;
1839     struct ice_vsi *vsi = np->vsi;
1840 
1841     link_info = &vsi->port_info->phy.link_info;
1842 
1843     /* Get supported and advertised settings from PHY ability with media */
1844     ice_phy_type_to_ethtool(netdev, ks);
1845 
1846     switch (link_info->link_speed) {
1847     case ICE_AQ_LINK_SPEED_100GB:
1848         ks->base.speed = SPEED_100000;
1849         break;
1850     case ICE_AQ_LINK_SPEED_50GB:
1851         ks->base.speed = SPEED_50000;
1852         break;
1853     case ICE_AQ_LINK_SPEED_40GB:
1854         ks->base.speed = SPEED_40000;
1855         break;
1856     case ICE_AQ_LINK_SPEED_25GB:
1857         ks->base.speed = SPEED_25000;
1858         break;
1859     case ICE_AQ_LINK_SPEED_20GB:
1860         ks->base.speed = SPEED_20000;
1861         break;
1862     case ICE_AQ_LINK_SPEED_10GB:
1863         ks->base.speed = SPEED_10000;
1864         break;
1865     case ICE_AQ_LINK_SPEED_5GB:
1866         ks->base.speed = SPEED_5000;
1867         break;
1868     case ICE_AQ_LINK_SPEED_2500MB:
1869         ks->base.speed = SPEED_2500;
1870         break;
1871     case ICE_AQ_LINK_SPEED_1000MB:
1872         ks->base.speed = SPEED_1000;
1873         break;
1874     case ICE_AQ_LINK_SPEED_100MB:
1875         ks->base.speed = SPEED_100;
1876         break;
1877     default:
1878         netdev_info(netdev, "WARNING: Unrecognized link_speed (0x%x).\n",
1879                 link_info->link_speed);
1880         break;
1881     }
1882     ks->base.duplex = DUPLEX_FULL;
1883 
1884     if (link_info->an_info & ICE_AQ_AN_COMPLETED)
1885         ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
1886                              Autoneg);
1887 
1888     /* Set flow control negotiated Rx/Tx pause */
1889     switch (pi->fc.current_mode) {
1890     case ICE_FC_FULL:
1891         ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
1892         break;
1893     case ICE_FC_TX_PAUSE:
1894         ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
1895         ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
1896                              Asym_Pause);
1897         break;
1898     case ICE_FC_RX_PAUSE:
1899         ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
1900                              Asym_Pause);
1901         break;
1902     case ICE_FC_PFC:
1903     default:
1904         ethtool_link_ksettings_del_link_mode(ks, lp_advertising, Pause);
1905         ethtool_link_ksettings_del_link_mode(ks, lp_advertising,
1906                              Asym_Pause);
1907         break;
1908     }
1909 }
1910 
1911 /**
1912  * ice_get_settings_link_down - Get the Link settings when link is down
1913  * @ks: ethtool ksettings to fill in
1914  * @netdev: network interface device structure
1915  *
1916  * Reports link settings that can be determined when link is down
1917  */
1918 static void
1919 ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
1920                struct net_device *netdev)
1921 {
1922     /* link is down and the driver needs to fall back on
1923      * supported PHY types to figure out what info to display
1924      */
1925     ice_phy_type_to_ethtool(netdev, ks);
1926 
1927     /* With no link, speed and duplex are unknown */
1928     ks->base.speed = SPEED_UNKNOWN;
1929     ks->base.duplex = DUPLEX_UNKNOWN;
1930 }
1931 
1932 /**
1933  * ice_get_link_ksettings - Get Link Speed and Duplex settings
1934  * @netdev: network interface device structure
1935  * @ks: ethtool ksettings
1936  *
1937  * Reports speed/duplex settings based on media_type
1938  */
1939 static int
1940 ice_get_link_ksettings(struct net_device *netdev,
1941                struct ethtool_link_ksettings *ks)
1942 {
1943     struct ice_netdev_priv *np = netdev_priv(netdev);
1944     struct ice_aqc_get_phy_caps_data *caps;
1945     struct ice_link_status *hw_link_info;
1946     struct ice_vsi *vsi = np->vsi;
1947     int err;
1948 
1949     ethtool_link_ksettings_zero_link_mode(ks, supported);
1950     ethtool_link_ksettings_zero_link_mode(ks, advertising);
1951     ethtool_link_ksettings_zero_link_mode(ks, lp_advertising);
1952     hw_link_info = &vsi->port_info->phy.link_info;
1953 
1954     /* set speed and duplex */
1955     if (hw_link_info->link_info & ICE_AQ_LINK_UP)
1956         ice_get_settings_link_up(ks, netdev);
1957     else
1958         ice_get_settings_link_down(ks, netdev);
1959 
1960     /* set autoneg settings */
1961     ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
1962         AUTONEG_ENABLE : AUTONEG_DISABLE;
1963 
1964     /* set media type settings */
1965     switch (vsi->port_info->phy.media_type) {
1966     case ICE_MEDIA_FIBER:
1967         ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1968         ks->base.port = PORT_FIBRE;
1969         break;
1970     case ICE_MEDIA_BASET:
1971         ethtool_link_ksettings_add_link_mode(ks, supported, TP);
1972         ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
1973         ks->base.port = PORT_TP;
1974         break;
1975     case ICE_MEDIA_BACKPLANE:
1976         ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
1977         ethtool_link_ksettings_add_link_mode(ks, advertising,
1978                              Backplane);
1979         ks->base.port = PORT_NONE;
1980         break;
1981     case ICE_MEDIA_DA:
1982         ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1983         ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
1984         ks->base.port = PORT_DA;
1985         break;
1986     default:
1987         ks->base.port = PORT_OTHER;
1988         break;
1989     }
1990 
1991     /* flow control is symmetric and always supported */
1992     ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
1993 
1994     caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1995     if (!caps)
1996         return -ENOMEM;
1997 
1998     err = ice_aq_get_phy_caps(vsi->port_info, false,
1999                   ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
2000     if (err)
2001         goto done;
2002 
2003     /* Set the advertised flow control based on the PHY capability */
2004     if ((caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) &&
2005         (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)) {
2006         ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2007         ethtool_link_ksettings_add_link_mode(ks, advertising,
2008                              Asym_Pause);
2009     } else if (caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) {
2010         ethtool_link_ksettings_add_link_mode(ks, advertising,
2011                              Asym_Pause);
2012     } else if (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE) {
2013         ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2014         ethtool_link_ksettings_add_link_mode(ks, advertising,
2015                              Asym_Pause);
2016     } else {
2017         ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
2018         ethtool_link_ksettings_del_link_mode(ks, advertising,
2019                              Asym_Pause);
2020     }
2021 
2022     /* Set advertised FEC modes based on PHY capability */
2023     ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_NONE);
2024 
2025     if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
2026         caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
2027         ethtool_link_ksettings_add_link_mode(ks, advertising,
2028                              FEC_BASER);
2029     if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
2030         caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
2031         ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
2032 
2033     err = ice_aq_get_phy_caps(vsi->port_info, false,
2034                   ICE_AQC_REPORT_TOPO_CAP_MEDIA, caps, NULL);
2035     if (err)
2036         goto done;
2037 
2038     /* Set supported FEC modes based on PHY capability */
2039     ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
2040 
2041     if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
2042         caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN)
2043         ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
2044     if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
2045         ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
2046 
2047     /* Set supported and advertised autoneg */
2048     if (ice_is_phy_caps_an_enabled(caps)) {
2049         ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
2050         ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
2051     }
2052 
2053 done:
2054     kfree(caps);
2055     return err;
2056 }
2057 
2058 /**
2059  * ice_ksettings_find_adv_link_speed - Find advertising link speed
2060  * @ks: ethtool ksettings
2061  */
2062 static u16
2063 ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
2064 {
2065     u16 adv_link_speed = 0;
2066 
2067     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2068                           100baseT_Full))
2069         adv_link_speed |= ICE_AQ_LINK_SPEED_100MB;
2070     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2071                           1000baseX_Full))
2072         adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2073     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2074                           1000baseT_Full) ||
2075         ethtool_link_ksettings_test_link_mode(ks, advertising,
2076                           1000baseKX_Full))
2077         adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2078     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2079                           2500baseT_Full))
2080         adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2081     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2082                           2500baseX_Full))
2083         adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2084     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2085                           5000baseT_Full))
2086         adv_link_speed |= ICE_AQ_LINK_SPEED_5GB;
2087     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2088                           10000baseT_Full) ||
2089         ethtool_link_ksettings_test_link_mode(ks, advertising,
2090                           10000baseKR_Full))
2091         adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2092     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2093                           10000baseSR_Full) ||
2094         ethtool_link_ksettings_test_link_mode(ks, advertising,
2095                           10000baseLR_Full))
2096         adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2097     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2098                           25000baseCR_Full) ||
2099         ethtool_link_ksettings_test_link_mode(ks, advertising,
2100                           25000baseSR_Full) ||
2101         ethtool_link_ksettings_test_link_mode(ks, advertising,
2102                           25000baseKR_Full))
2103         adv_link_speed |= ICE_AQ_LINK_SPEED_25GB;
2104     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2105                           40000baseCR4_Full) ||
2106         ethtool_link_ksettings_test_link_mode(ks, advertising,
2107                           40000baseSR4_Full) ||
2108         ethtool_link_ksettings_test_link_mode(ks, advertising,
2109                           40000baseLR4_Full) ||
2110         ethtool_link_ksettings_test_link_mode(ks, advertising,
2111                           40000baseKR4_Full))
2112         adv_link_speed |= ICE_AQ_LINK_SPEED_40GB;
2113     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2114                           50000baseCR2_Full) ||
2115         ethtool_link_ksettings_test_link_mode(ks, advertising,
2116                           50000baseKR2_Full))
2117         adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2118     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2119                           50000baseSR2_Full))
2120         adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2121     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2122                           100000baseCR4_Full) ||
2123         ethtool_link_ksettings_test_link_mode(ks, advertising,
2124                           100000baseSR4_Full) ||
2125         ethtool_link_ksettings_test_link_mode(ks, advertising,
2126                           100000baseLR4_ER4_Full) ||
2127         ethtool_link_ksettings_test_link_mode(ks, advertising,
2128                           100000baseKR4_Full))
2129         adv_link_speed |= ICE_AQ_LINK_SPEED_100GB;
2130 
2131     return adv_link_speed;
2132 }
2133 
2134 /**
2135  * ice_setup_autoneg
2136  * @p: port info
2137  * @ks: ethtool_link_ksettings
2138  * @config: configuration that will be sent down to FW
2139  * @autoneg_enabled: autonegotiation is enabled or not
2140  * @autoneg_changed: will there a change in autonegotiation
2141  * @netdev: network interface device structure
2142  *
2143  * Setup PHY autonegotiation feature
2144  */
2145 static int
2146 ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
2147           struct ice_aqc_set_phy_cfg_data *config,
2148           u8 autoneg_enabled, u8 *autoneg_changed,
2149           struct net_device *netdev)
2150 {
2151     int err = 0;
2152 
2153     *autoneg_changed = 0;
2154 
2155     /* Check autoneg */
2156     if (autoneg_enabled == AUTONEG_ENABLE) {
2157         /* If autoneg was not already enabled */
2158         if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
2159             /* If autoneg is not supported, return error */
2160             if (!ethtool_link_ksettings_test_link_mode(ks,
2161                                    supported,
2162                                    Autoneg)) {
2163                 netdev_info(netdev, "Autoneg not supported on this phy.\n");
2164                 err = -EINVAL;
2165             } else {
2166                 /* Autoneg is allowed to change */
2167                 config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2168                 *autoneg_changed = 1;
2169             }
2170         }
2171     } else {
2172         /* If autoneg is currently enabled */
2173         if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
2174             /* If autoneg is supported 10GBASE_T is the only PHY
2175              * that can disable it, so otherwise return error
2176              */
2177             if (ethtool_link_ksettings_test_link_mode(ks,
2178                                   supported,
2179                                   Autoneg)) {
2180                 netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
2181                 err = -EINVAL;
2182             } else {
2183                 /* Autoneg is allowed to change */
2184                 config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2185                 *autoneg_changed = 1;
2186             }
2187         }
2188     }
2189 
2190     return err;
2191 }
2192 
2193 /**
2194  * ice_set_phy_type_from_speed - set phy_types based on speeds
2195  * and advertised modes
2196  * @ks: ethtool link ksettings struct
2197  * @phy_type_low: pointer to the lower part of phy_type
2198  * @phy_type_high: pointer to the higher part of phy_type
2199  * @adv_link_speed: targeted link speeds bitmap
2200  */
2201 static void
2202 ice_set_phy_type_from_speed(const struct ethtool_link_ksettings *ks,
2203                 u64 *phy_type_low, u64 *phy_type_high,
2204                 u16 adv_link_speed)
2205 {
2206     /* Handle 1000M speed in a special way because ice_update_phy_type
2207      * enables all link modes, but having mixed copper and optical
2208      * standards is not supported.
2209      */
2210     adv_link_speed &= ~ICE_AQ_LINK_SPEED_1000MB;
2211 
2212     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2213                           1000baseT_Full))
2214         *phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_T |
2215                  ICE_PHY_TYPE_LOW_1G_SGMII;
2216 
2217     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2218                           1000baseKX_Full))
2219         *phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_KX;
2220 
2221     if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2222                           1000baseX_Full))
2223         *phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_SX |
2224                  ICE_PHY_TYPE_LOW_1000BASE_LX;
2225 
2226     ice_update_phy_type(phy_type_low, phy_type_high, adv_link_speed);
2227 }
2228 
2229 /**
2230  * ice_set_link_ksettings - Set Speed and Duplex
2231  * @netdev: network interface device structure
2232  * @ks: ethtool ksettings
2233  *
2234  * Set speed/duplex per media_types advertised/forced
2235  */
2236 static int
2237 ice_set_link_ksettings(struct net_device *netdev,
2238                const struct ethtool_link_ksettings *ks)
2239 {
2240     struct ice_netdev_priv *np = netdev_priv(netdev);
2241     u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT;
2242     struct ethtool_link_ksettings copy_ks = *ks;
2243     struct ethtool_link_ksettings safe_ks = {};
2244     struct ice_aqc_get_phy_caps_data *phy_caps;
2245     struct ice_aqc_set_phy_cfg_data config;
2246     u16 adv_link_speed, curr_link_speed;
2247     struct ice_pf *pf = np->vsi->back;
2248     struct ice_port_info *pi;
2249     u8 autoneg_changed = 0;
2250     u64 phy_type_high = 0;
2251     u64 phy_type_low = 0;
2252     bool linkup;
2253     int err;
2254 
2255     pi = np->vsi->port_info;
2256 
2257     if (!pi)
2258         return -EIO;
2259 
2260     if (pi->phy.media_type != ICE_MEDIA_BASET &&
2261         pi->phy.media_type != ICE_MEDIA_FIBER &&
2262         pi->phy.media_type != ICE_MEDIA_BACKPLANE &&
2263         pi->phy.media_type != ICE_MEDIA_DA &&
2264         pi->phy.link_info.link_info & ICE_AQ_LINK_UP)
2265         return -EOPNOTSUPP;
2266 
2267     phy_caps = kzalloc(sizeof(*phy_caps), GFP_KERNEL);
2268     if (!phy_caps)
2269         return -ENOMEM;
2270 
2271     /* Get the PHY capabilities based on media */
2272     if (ice_fw_supports_report_dflt_cfg(pi->hw))
2273         err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2274                       phy_caps, NULL);
2275     else
2276         err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2277                       phy_caps, NULL);
2278     if (err)
2279         goto done;
2280 
2281     /* save autoneg out of ksettings */
2282     autoneg = copy_ks.base.autoneg;
2283 
2284     /* Get link modes supported by hardware.*/
2285     ice_phy_type_to_ethtool(netdev, &safe_ks);
2286 
2287     /* and check against modes requested by user.
2288      * Return an error if unsupported mode was set.
2289      */
2290     if (!bitmap_subset(copy_ks.link_modes.advertising,
2291                safe_ks.link_modes.supported,
2292                __ETHTOOL_LINK_MODE_MASK_NBITS)) {
2293         if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags))
2294             netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
2295         err = -EOPNOTSUPP;
2296         goto done;
2297     }
2298 
2299     /* get our own copy of the bits to check against */
2300     memset(&safe_ks, 0, sizeof(safe_ks));
2301     safe_ks.base.cmd = copy_ks.base.cmd;
2302     safe_ks.base.link_mode_masks_nwords =
2303         copy_ks.base.link_mode_masks_nwords;
2304     ice_get_link_ksettings(netdev, &safe_ks);
2305 
2306     /* set autoneg back to what it currently is */
2307     copy_ks.base.autoneg = safe_ks.base.autoneg;
2308     /* we don't compare the speed */
2309     copy_ks.base.speed = safe_ks.base.speed;
2310 
2311     /* If copy_ks.base and safe_ks.base are not the same now, then they are
2312      * trying to set something that we do not support.
2313      */
2314     if (memcmp(&copy_ks.base, &safe_ks.base, sizeof(copy_ks.base))) {
2315         err = -EOPNOTSUPP;
2316         goto done;
2317     }
2318 
2319     while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
2320         timeout--;
2321         if (!timeout) {
2322             err = -EBUSY;
2323             goto done;
2324         }
2325         usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
2326     }
2327 
2328     /* Copy the current user PHY configuration. The current user PHY
2329      * configuration is initialized during probe from PHY capabilities
2330      * software mode, and updated on set PHY configuration.
2331      */
2332     config = pi->phy.curr_user_phy_cfg;
2333 
2334     config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2335 
2336     /* Check autoneg */
2337     err = ice_setup_autoneg(pi, &safe_ks, &config, autoneg, &autoneg_changed,
2338                 netdev);
2339 
2340     if (err)
2341         goto done;
2342 
2343     /* Call to get the current link speed */
2344     pi->phy.get_link_info = true;
2345     err = ice_get_link_status(pi, &linkup);
2346     if (err)
2347         goto done;
2348 
2349     curr_link_speed = pi->phy.curr_user_speed_req;
2350     adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
2351 
2352     /* If speed didn't get set, set it to what it currently is.
2353      * This is needed because if advertise is 0 (as it is when autoneg
2354      * is disabled) then speed won't get set.
2355      */
2356     if (!adv_link_speed)
2357         adv_link_speed = curr_link_speed;
2358 
2359     /* Convert the advertise link speeds to their corresponded PHY_TYPE */
2360     ice_set_phy_type_from_speed(ks, &phy_type_low, &phy_type_high,
2361                     adv_link_speed);
2362 
2363     if (!autoneg_changed && adv_link_speed == curr_link_speed) {
2364         netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
2365         goto done;
2366     }
2367 
2368     /* save the requested speeds */
2369     pi->phy.link_info.req_speeds = adv_link_speed;
2370 
2371     /* set link and auto negotiation so changes take effect */
2372     config.caps |= ICE_AQ_PHY_ENA_LINK;
2373 
2374     /* check if there is a PHY type for the requested advertised speed */
2375     if (!(phy_type_low || phy_type_high)) {
2376         netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
2377         err = -EOPNOTSUPP;
2378         goto done;
2379     }
2380 
2381     /* intersect requested advertised speed PHY types with media PHY types
2382      * for set PHY configuration
2383      */
2384     config.phy_type_high = cpu_to_le64(phy_type_high) &
2385             phy_caps->phy_type_high;
2386     config.phy_type_low = cpu_to_le64(phy_type_low) &
2387             phy_caps->phy_type_low;
2388 
2389     if (!(config.phy_type_high || config.phy_type_low)) {
2390         /* If there is no intersection and lenient mode is enabled, then
2391          * intersect the requested advertised speed with NVM media type
2392          * PHY types.
2393          */
2394         if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags)) {
2395             config.phy_type_high = cpu_to_le64(phy_type_high) &
2396                            pf->nvm_phy_type_hi;
2397             config.phy_type_low = cpu_to_le64(phy_type_low) &
2398                           pf->nvm_phy_type_lo;
2399         } else {
2400             netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
2401             err = -EOPNOTSUPP;
2402             goto done;
2403         }
2404     }
2405 
2406     /* If link is up put link down */
2407     if (pi->phy.link_info.link_info & ICE_AQ_LINK_UP) {
2408         /* Tell the OS link is going down, the link will go
2409          * back up when fw says it is ready asynchronously
2410          */
2411         ice_print_link_msg(np->vsi, false);
2412         netif_carrier_off(netdev);
2413         netif_tx_stop_all_queues(netdev);
2414     }
2415 
2416     /* make the aq call */
2417     err = ice_aq_set_phy_cfg(&pf->hw, pi, &config, NULL);
2418     if (err) {
2419         netdev_info(netdev, "Set phy config failed,\n");
2420         goto done;
2421     }
2422 
2423     /* Save speed request */
2424     pi->phy.curr_user_speed_req = adv_link_speed;
2425 done:
2426     kfree(phy_caps);
2427     clear_bit(ICE_CFG_BUSY, pf->state);
2428 
2429     return err;
2430 }
2431 
2432 /**
2433  * ice_parse_hdrs - parses headers from RSS hash input
2434  * @nfc: ethtool rxnfc command
2435  *
2436  * This function parses the rxnfc command and returns intended
2437  * header types for RSS configuration
2438  */
2439 static u32 ice_parse_hdrs(struct ethtool_rxnfc *nfc)
2440 {
2441     u32 hdrs = ICE_FLOW_SEG_HDR_NONE;
2442 
2443     switch (nfc->flow_type) {
2444     case TCP_V4_FLOW:
2445         hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4;
2446         break;
2447     case UDP_V4_FLOW:
2448         hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4;
2449         break;
2450     case SCTP_V4_FLOW:
2451         hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4;
2452         break;
2453     case TCP_V6_FLOW:
2454         hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6;
2455         break;
2456     case UDP_V6_FLOW:
2457         hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6;
2458         break;
2459     case SCTP_V6_FLOW:
2460         hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6;
2461         break;
2462     default:
2463         break;
2464     }
2465     return hdrs;
2466 }
2467 
2468 #define ICE_FLOW_HASH_FLD_IPV4_SA   BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)
2469 #define ICE_FLOW_HASH_FLD_IPV6_SA   BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)
2470 #define ICE_FLOW_HASH_FLD_IPV4_DA   BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)
2471 #define ICE_FLOW_HASH_FLD_IPV6_DA   BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)
2472 #define ICE_FLOW_HASH_FLD_TCP_SRC_PORT  BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)
2473 #define ICE_FLOW_HASH_FLD_TCP_DST_PORT  BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)
2474 #define ICE_FLOW_HASH_FLD_UDP_SRC_PORT  BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)
2475 #define ICE_FLOW_HASH_FLD_UDP_DST_PORT  BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)
2476 #define ICE_FLOW_HASH_FLD_SCTP_SRC_PORT \
2477     BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)
2478 #define ICE_FLOW_HASH_FLD_SCTP_DST_PORT \
2479     BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)
2480 
2481 /**
2482  * ice_parse_hash_flds - parses hash fields from RSS hash input
2483  * @nfc: ethtool rxnfc command
2484  *
2485  * This function parses the rxnfc command and returns intended
2486  * hash fields for RSS configuration
2487  */
2488 static u64 ice_parse_hash_flds(struct ethtool_rxnfc *nfc)
2489 {
2490     u64 hfld = ICE_HASH_INVALID;
2491 
2492     if (nfc->data & RXH_IP_SRC || nfc->data & RXH_IP_DST) {
2493         switch (nfc->flow_type) {
2494         case TCP_V4_FLOW:
2495         case UDP_V4_FLOW:
2496         case SCTP_V4_FLOW:
2497             if (nfc->data & RXH_IP_SRC)
2498                 hfld |= ICE_FLOW_HASH_FLD_IPV4_SA;
2499             if (nfc->data & RXH_IP_DST)
2500                 hfld |= ICE_FLOW_HASH_FLD_IPV4_DA;
2501             break;
2502         case TCP_V6_FLOW:
2503         case UDP_V6_FLOW:
2504         case SCTP_V6_FLOW:
2505             if (nfc->data & RXH_IP_SRC)
2506                 hfld |= ICE_FLOW_HASH_FLD_IPV6_SA;
2507             if (nfc->data & RXH_IP_DST)
2508                 hfld |= ICE_FLOW_HASH_FLD_IPV6_DA;
2509             break;
2510         default:
2511             break;
2512         }
2513     }
2514 
2515     if (nfc->data & RXH_L4_B_0_1 || nfc->data & RXH_L4_B_2_3) {
2516         switch (nfc->flow_type) {
2517         case TCP_V4_FLOW:
2518         case TCP_V6_FLOW:
2519             if (nfc->data & RXH_L4_B_0_1)
2520                 hfld |= ICE_FLOW_HASH_FLD_TCP_SRC_PORT;
2521             if (nfc->data & RXH_L4_B_2_3)
2522                 hfld |= ICE_FLOW_HASH_FLD_TCP_DST_PORT;
2523             break;
2524         case UDP_V4_FLOW:
2525         case UDP_V6_FLOW:
2526             if (nfc->data & RXH_L4_B_0_1)
2527                 hfld |= ICE_FLOW_HASH_FLD_UDP_SRC_PORT;
2528             if (nfc->data & RXH_L4_B_2_3)
2529                 hfld |= ICE_FLOW_HASH_FLD_UDP_DST_PORT;
2530             break;
2531         case SCTP_V4_FLOW:
2532         case SCTP_V6_FLOW:
2533             if (nfc->data & RXH_L4_B_0_1)
2534                 hfld |= ICE_FLOW_HASH_FLD_SCTP_SRC_PORT;
2535             if (nfc->data & RXH_L4_B_2_3)
2536                 hfld |= ICE_FLOW_HASH_FLD_SCTP_DST_PORT;
2537             break;
2538         default:
2539             break;
2540         }
2541     }
2542 
2543     return hfld;
2544 }
2545 
2546 /**
2547  * ice_set_rss_hash_opt - Enable/Disable flow types for RSS hash
2548  * @vsi: the VSI being configured
2549  * @nfc: ethtool rxnfc command
2550  *
2551  * Returns Success if the flow input set is supported.
2552  */
2553 static int
2554 ice_set_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
2555 {
2556     struct ice_pf *pf = vsi->back;
2557     struct device *dev;
2558     u64 hashed_flds;
2559     int status;
2560     u32 hdrs;
2561 
2562     dev = ice_pf_to_dev(pf);
2563     if (ice_is_safe_mode(pf)) {
2564         dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
2565             vsi->vsi_num);
2566         return -EINVAL;
2567     }
2568 
2569     hashed_flds = ice_parse_hash_flds(nfc);
2570     if (hashed_flds == ICE_HASH_INVALID) {
2571         dev_dbg(dev, "Invalid hash fields, vsi num = %d\n",
2572             vsi->vsi_num);
2573         return -EINVAL;
2574     }
2575 
2576     hdrs = ice_parse_hdrs(nfc);
2577     if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
2578         dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
2579             vsi->vsi_num);
2580         return -EINVAL;
2581     }
2582 
2583     status = ice_add_rss_cfg(&pf->hw, vsi->idx, hashed_flds, hdrs);
2584     if (status) {
2585         dev_dbg(dev, "ice_add_rss_cfg failed, vsi num = %d, error = %d\n",
2586             vsi->vsi_num, status);
2587         return status;
2588     }
2589 
2590     return 0;
2591 }
2592 
2593 /**
2594  * ice_get_rss_hash_opt - Retrieve hash fields for a given flow-type
2595  * @vsi: the VSI being configured
2596  * @nfc: ethtool rxnfc command
2597  */
2598 static void
2599 ice_get_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
2600 {
2601     struct ice_pf *pf = vsi->back;
2602     struct device *dev;
2603     u64 hash_flds;
2604     u32 hdrs;
2605 
2606     dev = ice_pf_to_dev(pf);
2607 
2608     nfc->data = 0;
2609     if (ice_is_safe_mode(pf)) {
2610         dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
2611             vsi->vsi_num);
2612         return;
2613     }
2614 
2615     hdrs = ice_parse_hdrs(nfc);
2616     if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
2617         dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
2618             vsi->vsi_num);
2619         return;
2620     }
2621 
2622     hash_flds = ice_get_rss_cfg(&pf->hw, vsi->idx, hdrs);
2623     if (hash_flds == ICE_HASH_INVALID) {
2624         dev_dbg(dev, "No hash fields found for the given header type, vsi num = %d\n",
2625             vsi->vsi_num);
2626         return;
2627     }
2628 
2629     if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_SA ||
2630         hash_flds & ICE_FLOW_HASH_FLD_IPV6_SA)
2631         nfc->data |= (u64)RXH_IP_SRC;
2632 
2633     if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_DA ||
2634         hash_flds & ICE_FLOW_HASH_FLD_IPV6_DA)
2635         nfc->data |= (u64)RXH_IP_DST;
2636 
2637     if (hash_flds & ICE_FLOW_HASH_FLD_TCP_SRC_PORT ||
2638         hash_flds & ICE_FLOW_HASH_FLD_UDP_SRC_PORT ||
2639         hash_flds & ICE_FLOW_HASH_FLD_SCTP_SRC_PORT)
2640         nfc->data |= (u64)RXH_L4_B_0_1;
2641 
2642     if (hash_flds & ICE_FLOW_HASH_FLD_TCP_DST_PORT ||
2643         hash_flds & ICE_FLOW_HASH_FLD_UDP_DST_PORT ||
2644         hash_flds & ICE_FLOW_HASH_FLD_SCTP_DST_PORT)
2645         nfc->data |= (u64)RXH_L4_B_2_3;
2646 }
2647 
2648 /**
2649  * ice_set_rxnfc - command to set Rx flow rules.
2650  * @netdev: network interface device structure
2651  * @cmd: ethtool rxnfc command
2652  *
2653  * Returns 0 for success and negative values for errors
2654  */
2655 static int ice_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
2656 {
2657     struct ice_netdev_priv *np = netdev_priv(netdev);
2658     struct ice_vsi *vsi = np->vsi;
2659 
2660     switch (cmd->cmd) {
2661     case ETHTOOL_SRXCLSRLINS:
2662         return ice_add_fdir_ethtool(vsi, cmd);
2663     case ETHTOOL_SRXCLSRLDEL:
2664         return ice_del_fdir_ethtool(vsi, cmd);
2665     case ETHTOOL_SRXFH:
2666         return ice_set_rss_hash_opt(vsi, cmd);
2667     default:
2668         break;
2669     }
2670     return -EOPNOTSUPP;
2671 }
2672 
2673 /**
2674  * ice_get_rxnfc - command to get Rx flow classification rules
2675  * @netdev: network interface device structure
2676  * @cmd: ethtool rxnfc command
2677  * @rule_locs: buffer to rturn Rx flow classification rules
2678  *
2679  * Returns Success if the command is supported.
2680  */
2681 static int
2682 ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2683           u32 __always_unused *rule_locs)
2684 {
2685     struct ice_netdev_priv *np = netdev_priv(netdev);
2686     struct ice_vsi *vsi = np->vsi;
2687     int ret = -EOPNOTSUPP;
2688     struct ice_hw *hw;
2689 
2690     hw = &vsi->back->hw;
2691 
2692     switch (cmd->cmd) {
2693     case ETHTOOL_GRXRINGS:
2694         cmd->data = vsi->rss_size;
2695         ret = 0;
2696         break;
2697     case ETHTOOL_GRXCLSRLCNT:
2698         cmd->rule_cnt = hw->fdir_active_fltr;
2699         /* report total rule count */
2700         cmd->data = ice_get_fdir_cnt_all(hw);
2701         ret = 0;
2702         break;
2703     case ETHTOOL_GRXCLSRULE:
2704         ret = ice_get_ethtool_fdir_entry(hw, cmd);
2705         break;
2706     case ETHTOOL_GRXCLSRLALL:
2707         ret = ice_get_fdir_fltr_ids(hw, cmd, (u32 *)rule_locs);
2708         break;
2709     case ETHTOOL_GRXFH:
2710         ice_get_rss_hash_opt(vsi, cmd);
2711         ret = 0;
2712         break;
2713     default:
2714         break;
2715     }
2716 
2717     return ret;
2718 }
2719 
2720 static void
2721 ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
2722           struct kernel_ethtool_ringparam *kernel_ring,
2723           struct netlink_ext_ack *extack)
2724 {
2725     struct ice_netdev_priv *np = netdev_priv(netdev);
2726     struct ice_vsi *vsi = np->vsi;
2727 
2728     ring->rx_max_pending = ICE_MAX_NUM_DESC;
2729     ring->tx_max_pending = ICE_MAX_NUM_DESC;
2730     ring->rx_pending = vsi->rx_rings[0]->count;
2731     ring->tx_pending = vsi->tx_rings[0]->count;
2732 
2733     /* Rx mini and jumbo rings are not supported */
2734     ring->rx_mini_max_pending = 0;
2735     ring->rx_jumbo_max_pending = 0;
2736     ring->rx_mini_pending = 0;
2737     ring->rx_jumbo_pending = 0;
2738 }
2739 
2740 static int
2741 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
2742           struct kernel_ethtool_ringparam *kernel_ring,
2743           struct netlink_ext_ack *extack)
2744 {
2745     struct ice_netdev_priv *np = netdev_priv(netdev);
2746     struct ice_tx_ring *xdp_rings = NULL;
2747     struct ice_tx_ring *tx_rings = NULL;
2748     struct ice_rx_ring *rx_rings = NULL;
2749     struct ice_vsi *vsi = np->vsi;
2750     struct ice_pf *pf = vsi->back;
2751     int i, timeout = 50, err = 0;
2752     u16 new_rx_cnt, new_tx_cnt;
2753 
2754     if (ring->tx_pending > ICE_MAX_NUM_DESC ||
2755         ring->tx_pending < ICE_MIN_NUM_DESC ||
2756         ring->rx_pending > ICE_MAX_NUM_DESC ||
2757         ring->rx_pending < ICE_MIN_NUM_DESC) {
2758         netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
2759                ring->tx_pending, ring->rx_pending,
2760                ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
2761                ICE_REQ_DESC_MULTIPLE);
2762         return -EINVAL;
2763     }
2764 
2765     new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
2766     if (new_tx_cnt != ring->tx_pending)
2767         netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n",
2768                 new_tx_cnt);
2769     new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
2770     if (new_rx_cnt != ring->rx_pending)
2771         netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n",
2772                 new_rx_cnt);
2773 
2774     /* if nothing to do return success */
2775     if (new_tx_cnt == vsi->tx_rings[0]->count &&
2776         new_rx_cnt == vsi->rx_rings[0]->count) {
2777         netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
2778         return 0;
2779     }
2780 
2781     /* If there is a AF_XDP UMEM attached to any of Rx rings,
2782      * disallow changing the number of descriptors -- regardless
2783      * if the netdev is running or not.
2784      */
2785     if (ice_xsk_any_rx_ring_ena(vsi))
2786         return -EBUSY;
2787 
2788     while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
2789         timeout--;
2790         if (!timeout)
2791             return -EBUSY;
2792         usleep_range(1000, 2000);
2793     }
2794 
2795     /* set for the next time the netdev is started */
2796     if (!netif_running(vsi->netdev)) {
2797         ice_for_each_alloc_txq(vsi, i)
2798             vsi->tx_rings[i]->count = new_tx_cnt;
2799         ice_for_each_alloc_rxq(vsi, i)
2800             vsi->rx_rings[i]->count = new_rx_cnt;
2801         if (ice_is_xdp_ena_vsi(vsi))
2802             ice_for_each_xdp_txq(vsi, i)
2803                 vsi->xdp_rings[i]->count = new_tx_cnt;
2804         vsi->num_tx_desc = (u16)new_tx_cnt;
2805         vsi->num_rx_desc = (u16)new_rx_cnt;
2806         netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
2807         goto done;
2808     }
2809 
2810     if (new_tx_cnt == vsi->tx_rings[0]->count)
2811         goto process_rx;
2812 
2813     /* alloc updated Tx resources */
2814     netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
2815             vsi->tx_rings[0]->count, new_tx_cnt);
2816 
2817     tx_rings = kcalloc(vsi->num_txq, sizeof(*tx_rings), GFP_KERNEL);
2818     if (!tx_rings) {
2819         err = -ENOMEM;
2820         goto done;
2821     }
2822 
2823     ice_for_each_txq(vsi, i) {
2824         /* clone ring and setup updated count */
2825         tx_rings[i] = *vsi->tx_rings[i];
2826         tx_rings[i].count = new_tx_cnt;
2827         tx_rings[i].desc = NULL;
2828         tx_rings[i].tx_buf = NULL;
2829         err = ice_setup_tx_ring(&tx_rings[i]);
2830         if (err) {
2831             while (i--)
2832                 ice_clean_tx_ring(&tx_rings[i]);
2833             kfree(tx_rings);
2834             goto done;
2835         }
2836     }
2837 
2838     if (!ice_is_xdp_ena_vsi(vsi))
2839         goto process_rx;
2840 
2841     /* alloc updated XDP resources */
2842     netdev_info(netdev, "Changing XDP descriptor count from %d to %d\n",
2843             vsi->xdp_rings[0]->count, new_tx_cnt);
2844 
2845     xdp_rings = kcalloc(vsi->num_xdp_txq, sizeof(*xdp_rings), GFP_KERNEL);
2846     if (!xdp_rings) {
2847         err = -ENOMEM;
2848         goto free_tx;
2849     }
2850 
2851     ice_for_each_xdp_txq(vsi, i) {
2852         /* clone ring and setup updated count */
2853         xdp_rings[i] = *vsi->xdp_rings[i];
2854         xdp_rings[i].count = new_tx_cnt;
2855         xdp_rings[i].next_dd = ICE_RING_QUARTER(&xdp_rings[i]) - 1;
2856         xdp_rings[i].next_rs = ICE_RING_QUARTER(&xdp_rings[i]) - 1;
2857         xdp_rings[i].desc = NULL;
2858         xdp_rings[i].tx_buf = NULL;
2859         err = ice_setup_tx_ring(&xdp_rings[i]);
2860         if (err) {
2861             while (i--)
2862                 ice_clean_tx_ring(&xdp_rings[i]);
2863             kfree(xdp_rings);
2864             goto free_tx;
2865         }
2866         ice_set_ring_xdp(&xdp_rings[i]);
2867     }
2868 
2869 process_rx:
2870     if (new_rx_cnt == vsi->rx_rings[0]->count)
2871         goto process_link;
2872 
2873     /* alloc updated Rx resources */
2874     netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
2875             vsi->rx_rings[0]->count, new_rx_cnt);
2876 
2877     rx_rings = kcalloc(vsi->num_rxq, sizeof(*rx_rings), GFP_KERNEL);
2878     if (!rx_rings) {
2879         err = -ENOMEM;
2880         goto done;
2881     }
2882 
2883     ice_for_each_rxq(vsi, i) {
2884         /* clone ring and setup updated count */
2885         rx_rings[i] = *vsi->rx_rings[i];
2886         rx_rings[i].count = new_rx_cnt;
2887         rx_rings[i].desc = NULL;
2888         rx_rings[i].rx_buf = NULL;
2889         /* this is to allow wr32 to have something to write to
2890          * during early allocation of Rx buffers
2891          */
2892         rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
2893 
2894         err = ice_setup_rx_ring(&rx_rings[i]);
2895         if (err)
2896             goto rx_unwind;
2897 
2898         /* allocate Rx buffers */
2899         err = ice_alloc_rx_bufs(&rx_rings[i],
2900                     ICE_DESC_UNUSED(&rx_rings[i]));
2901 rx_unwind:
2902         if (err) {
2903             while (i) {
2904                 i--;
2905                 ice_free_rx_ring(&rx_rings[i]);
2906             }
2907             kfree(rx_rings);
2908             err = -ENOMEM;
2909             goto free_tx;
2910         }
2911     }
2912 
2913 process_link:
2914     /* Bring interface down, copy in the new ring info, then restore the
2915      * interface. if VSI is up, bring it down and then back up
2916      */
2917     if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
2918         ice_down(vsi);
2919 
2920         if (tx_rings) {
2921             ice_for_each_txq(vsi, i) {
2922                 ice_free_tx_ring(vsi->tx_rings[i]);
2923                 *vsi->tx_rings[i] = tx_rings[i];
2924             }
2925             kfree(tx_rings);
2926         }
2927 
2928         if (rx_rings) {
2929             ice_for_each_rxq(vsi, i) {
2930                 ice_free_rx_ring(vsi->rx_rings[i]);
2931                 /* copy the real tail offset */
2932                 rx_rings[i].tail = vsi->rx_rings[i]->tail;
2933                 /* this is to fake out the allocation routine
2934                  * into thinking it has to realloc everything
2935                  * but the recycling logic will let us re-use
2936                  * the buffers allocated above
2937                  */
2938                 rx_rings[i].next_to_use = 0;
2939                 rx_rings[i].next_to_clean = 0;
2940                 rx_rings[i].next_to_alloc = 0;
2941                 *vsi->rx_rings[i] = rx_rings[i];
2942             }
2943             kfree(rx_rings);
2944         }
2945 
2946         if (xdp_rings) {
2947             ice_for_each_xdp_txq(vsi, i) {
2948                 ice_free_tx_ring(vsi->xdp_rings[i]);
2949                 *vsi->xdp_rings[i] = xdp_rings[i];
2950             }
2951             kfree(xdp_rings);
2952         }
2953 
2954         vsi->num_tx_desc = new_tx_cnt;
2955         vsi->num_rx_desc = new_rx_cnt;
2956         ice_up(vsi);
2957     }
2958     goto done;
2959 
2960 free_tx:
2961     /* error cleanup if the Rx allocations failed after getting Tx */
2962     if (tx_rings) {
2963         ice_for_each_txq(vsi, i)
2964             ice_free_tx_ring(&tx_rings[i]);
2965         kfree(tx_rings);
2966     }
2967 
2968 done:
2969     clear_bit(ICE_CFG_BUSY, pf->state);
2970     return err;
2971 }
2972 
2973 /**
2974  * ice_get_pauseparam - Get Flow Control status
2975  * @netdev: network interface device structure
2976  * @pause: ethernet pause (flow control) parameters
2977  *
2978  * Get requested flow control status from PHY capability.
2979  * If autoneg is true, then ethtool will send the ETHTOOL_GSET ioctl which
2980  * is handled by ice_get_link_ksettings. ice_get_link_ksettings will report
2981  * the negotiated Rx/Tx pause via lp_advertising.
2982  */
2983 static void
2984 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2985 {
2986     struct ice_netdev_priv *np = netdev_priv(netdev);
2987     struct ice_port_info *pi = np->vsi->port_info;
2988     struct ice_aqc_get_phy_caps_data *pcaps;
2989     struct ice_dcbx_cfg *dcbx_cfg;
2990     int status;
2991 
2992     /* Initialize pause params */
2993     pause->rx_pause = 0;
2994     pause->tx_pause = 0;
2995 
2996     dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
2997 
2998     pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2999     if (!pcaps)
3000         return;
3001 
3002     /* Get current PHY config */
3003     status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
3004                      NULL);
3005     if (status)
3006         goto out;
3007 
3008     pause->autoneg = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
3009                                  AUTONEG_DISABLE;
3010 
3011     if (dcbx_cfg->pfc.pfcena)
3012         /* PFC enabled so report LFC as off */
3013         goto out;
3014 
3015     if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
3016         pause->tx_pause = 1;
3017     if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
3018         pause->rx_pause = 1;
3019 
3020 out:
3021     kfree(pcaps);
3022 }
3023 
3024 /**
3025  * ice_set_pauseparam - Set Flow Control parameter
3026  * @netdev: network interface device structure
3027  * @pause: return Tx/Rx flow control status
3028  */
3029 static int
3030 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
3031 {
3032     struct ice_netdev_priv *np = netdev_priv(netdev);
3033     struct ice_aqc_get_phy_caps_data *pcaps;
3034     struct ice_link_status *hw_link_info;
3035     struct ice_pf *pf = np->vsi->back;
3036     struct ice_dcbx_cfg *dcbx_cfg;
3037     struct ice_vsi *vsi = np->vsi;
3038     struct ice_hw *hw = &pf->hw;
3039     struct ice_port_info *pi;
3040     u8 aq_failures;
3041     bool link_up;
3042     u32 is_an;
3043     int err;
3044 
3045     pi = vsi->port_info;
3046     hw_link_info = &pi->phy.link_info;
3047     dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
3048     link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
3049 
3050     /* Changing the port's flow control is not supported if this isn't the
3051      * PF VSI
3052      */
3053     if (vsi->type != ICE_VSI_PF) {
3054         netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
3055         return -EOPNOTSUPP;
3056     }
3057 
3058     /* Get pause param reports configured and negotiated flow control pause
3059      * when ETHTOOL_GLINKSETTINGS is defined. Since ETHTOOL_GLINKSETTINGS is
3060      * defined get pause param pause->autoneg reports SW configured setting,
3061      * so compare pause->autoneg with SW configured to prevent the user from
3062      * using set pause param to chance autoneg.
3063      */
3064     pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
3065     if (!pcaps)
3066         return -ENOMEM;
3067 
3068     /* Get current PHY config */
3069     err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
3070                   NULL);
3071     if (err) {
3072         kfree(pcaps);
3073         return err;
3074     }
3075 
3076     is_an = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
3077                             AUTONEG_DISABLE;
3078 
3079     kfree(pcaps);
3080 
3081     if (pause->autoneg != is_an) {
3082         netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
3083         return -EOPNOTSUPP;
3084     }
3085 
3086     /* If we have link and don't have autoneg */
3087     if (!test_bit(ICE_DOWN, pf->state) &&
3088         !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
3089         /* Send message that it might not necessarily work*/
3090         netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
3091     }
3092 
3093     if (dcbx_cfg->pfc.pfcena) {
3094         netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
3095         return -EOPNOTSUPP;
3096     }
3097     if (pause->rx_pause && pause->tx_pause)
3098         pi->fc.req_mode = ICE_FC_FULL;
3099     else if (pause->rx_pause && !pause->tx_pause)
3100         pi->fc.req_mode = ICE_FC_RX_PAUSE;
3101     else if (!pause->rx_pause && pause->tx_pause)
3102         pi->fc.req_mode = ICE_FC_TX_PAUSE;
3103     else if (!pause->rx_pause && !pause->tx_pause)
3104         pi->fc.req_mode = ICE_FC_NONE;
3105     else
3106         return -EINVAL;
3107 
3108     /* Set the FC mode and only restart AN if link is up */
3109     err = ice_set_fc(pi, &aq_failures, link_up);
3110 
3111     if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
3112         netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %s\n",
3113                 err, ice_aq_str(hw->adminq.sq_last_status));
3114         err = -EAGAIN;
3115     } else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
3116         netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %s\n",
3117                 err, ice_aq_str(hw->adminq.sq_last_status));
3118         err = -EAGAIN;
3119     } else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
3120         netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %s\n",
3121                 err, ice_aq_str(hw->adminq.sq_last_status));
3122         err = -EAGAIN;
3123     }
3124 
3125     return err;
3126 }
3127 
3128 /**
3129  * ice_get_rxfh_key_size - get the RSS hash key size
3130  * @netdev: network interface device structure
3131  *
3132  * Returns the table size.
3133  */
3134 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
3135 {
3136     return ICE_VSIQF_HKEY_ARRAY_SIZE;
3137 }
3138 
3139 /**
3140  * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
3141  * @netdev: network interface device structure
3142  *
3143  * Returns the table size.
3144  */
3145 static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
3146 {
3147     struct ice_netdev_priv *np = netdev_priv(netdev);
3148 
3149     return np->vsi->rss_table_size;
3150 }
3151 
3152 static int
3153 ice_get_rxfh_context(struct net_device *netdev, u32 *indir,
3154              u8 *key, u8 *hfunc, u32 rss_context)
3155 {
3156     struct ice_netdev_priv *np = netdev_priv(netdev);
3157     struct ice_vsi *vsi = np->vsi;
3158     struct ice_pf *pf = vsi->back;
3159     u16 qcount, offset;
3160     int err, num_tc, i;
3161     u8 *lut;
3162 
3163     if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3164         netdev_warn(netdev, "RSS is not supported on this VSI!\n");
3165         return -EOPNOTSUPP;
3166     }
3167 
3168     if (rss_context && !ice_is_adq_active(pf)) {
3169         netdev_err(netdev, "RSS context cannot be non-zero when ADQ is not configured.\n");
3170         return -EINVAL;
3171     }
3172 
3173     qcount = vsi->mqprio_qopt.qopt.count[rss_context];
3174     offset = vsi->mqprio_qopt.qopt.offset[rss_context];
3175 
3176     if (rss_context && ice_is_adq_active(pf)) {
3177         num_tc = vsi->mqprio_qopt.qopt.num_tc;
3178         if (rss_context >= num_tc) {
3179             netdev_err(netdev, "RSS context:%d  > num_tc:%d\n",
3180                    rss_context, num_tc);
3181             return -EINVAL;
3182         }
3183         /* Use channel VSI of given TC */
3184         vsi = vsi->tc_map_vsi[rss_context];
3185     }
3186 
3187     if (hfunc)
3188         *hfunc = ETH_RSS_HASH_TOP;
3189 
3190     if (!indir)
3191         return 0;
3192 
3193     lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3194     if (!lut)
3195         return -ENOMEM;
3196 
3197     err = ice_get_rss_key(vsi, key);
3198     if (err)
3199         goto out;
3200 
3201     err = ice_get_rss_lut(vsi, lut, vsi->rss_table_size);
3202     if (err)
3203         goto out;
3204 
3205     if (ice_is_adq_active(pf)) {
3206         for (i = 0; i < vsi->rss_table_size; i++)
3207             indir[i] = offset + lut[i] % qcount;
3208         goto out;
3209     }
3210 
3211     for (i = 0; i < vsi->rss_table_size; i++)
3212         indir[i] = lut[i];
3213 
3214 out:
3215     kfree(lut);
3216     return err;
3217 }
3218 
3219 /**
3220  * ice_get_rxfh - get the Rx flow hash indirection table
3221  * @netdev: network interface device structure
3222  * @indir: indirection table
3223  * @key: hash key
3224  * @hfunc: hash function
3225  *
3226  * Reads the indirection table directly from the hardware.
3227  */
3228 static int
3229 ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
3230 {
3231     return ice_get_rxfh_context(netdev, indir, key, hfunc, 0);
3232 }
3233 
3234 /**
3235  * ice_set_rxfh - set the Rx flow hash indirection table
3236  * @netdev: network interface device structure
3237  * @indir: indirection table
3238  * @key: hash key
3239  * @hfunc: hash function
3240  *
3241  * Returns -EINVAL if the table specifies an invalid queue ID, otherwise
3242  * returns 0 after programming the table.
3243  */
3244 static int
3245 ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key,
3246          const u8 hfunc)
3247 {
3248     struct ice_netdev_priv *np = netdev_priv(netdev);
3249     struct ice_vsi *vsi = np->vsi;
3250     struct ice_pf *pf = vsi->back;
3251     struct device *dev;
3252     int err;
3253 
3254     dev = ice_pf_to_dev(pf);
3255     if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3256         return -EOPNOTSUPP;
3257 
3258     if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3259         /* RSS not supported return error here */
3260         netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3261         return -EIO;
3262     }
3263 
3264     if (ice_is_adq_active(pf)) {
3265         netdev_err(netdev, "Cannot change RSS params with ADQ configured.\n");
3266         return -EOPNOTSUPP;
3267     }
3268 
3269     if (key) {
3270         if (!vsi->rss_hkey_user) {
3271             vsi->rss_hkey_user =
3272                 devm_kzalloc(dev, ICE_VSIQF_HKEY_ARRAY_SIZE,
3273                          GFP_KERNEL);
3274             if (!vsi->rss_hkey_user)
3275                 return -ENOMEM;
3276         }
3277         memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
3278 
3279         err = ice_set_rss_key(vsi, vsi->rss_hkey_user);
3280         if (err)
3281             return err;
3282     }
3283 
3284     if (!vsi->rss_lut_user) {
3285         vsi->rss_lut_user = devm_kzalloc(dev, vsi->rss_table_size,
3286                          GFP_KERNEL);
3287         if (!vsi->rss_lut_user)
3288             return -ENOMEM;
3289     }
3290 
3291     /* Each 32 bits pointed by 'indir' is stored with a lut entry */
3292     if (indir) {
3293         int i;
3294 
3295         for (i = 0; i < vsi->rss_table_size; i++)
3296             vsi->rss_lut_user[i] = (u8)(indir[i]);
3297     } else {
3298         ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
3299                  vsi->rss_size);
3300     }
3301 
3302     err = ice_set_rss_lut(vsi, vsi->rss_lut_user, vsi->rss_table_size);
3303     if (err)
3304         return err;
3305 
3306     return 0;
3307 }
3308 
3309 static int
3310 ice_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
3311 {
3312     struct ice_pf *pf = ice_netdev_to_pf(dev);
3313 
3314     /* only report timestamping if PTP is enabled */
3315     if (!test_bit(ICE_FLAG_PTP, pf->flags))
3316         return ethtool_op_get_ts_info(dev, info);
3317 
3318     info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
3319                 SOF_TIMESTAMPING_RX_SOFTWARE |
3320                 SOF_TIMESTAMPING_SOFTWARE |
3321                 SOF_TIMESTAMPING_TX_HARDWARE |
3322                 SOF_TIMESTAMPING_RX_HARDWARE |
3323                 SOF_TIMESTAMPING_RAW_HARDWARE;
3324 
3325     info->phc_index = ice_get_ptp_clock_index(pf);
3326 
3327     info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
3328 
3329     info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
3330 
3331     return 0;
3332 }
3333 
3334 /**
3335  * ice_get_max_txq - return the maximum number of Tx queues for in a PF
3336  * @pf: PF structure
3337  */
3338 static int ice_get_max_txq(struct ice_pf *pf)
3339 {
3340     return min3(pf->num_lan_msix, (u16)num_online_cpus(),
3341             (u16)pf->hw.func_caps.common_cap.num_txq);
3342 }
3343 
3344 /**
3345  * ice_get_max_rxq - return the maximum number of Rx queues for in a PF
3346  * @pf: PF structure
3347  */
3348 static int ice_get_max_rxq(struct ice_pf *pf)
3349 {
3350     return min3(pf->num_lan_msix, (u16)num_online_cpus(),
3351             (u16)pf->hw.func_caps.common_cap.num_rxq);
3352 }
3353 
3354 /**
3355  * ice_get_combined_cnt - return the current number of combined channels
3356  * @vsi: PF VSI pointer
3357  *
3358  * Go through all queue vectors and count ones that have both Rx and Tx ring
3359  * attached
3360  */
3361 static u32 ice_get_combined_cnt(struct ice_vsi *vsi)
3362 {
3363     u32 combined = 0;
3364     int q_idx;
3365 
3366     ice_for_each_q_vector(vsi, q_idx) {
3367         struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3368 
3369         if (q_vector->rx.rx_ring && q_vector->tx.tx_ring)
3370             combined++;
3371     }
3372 
3373     return combined;
3374 }
3375 
3376 /**
3377  * ice_get_channels - get the current and max supported channels
3378  * @dev: network interface device structure
3379  * @ch: ethtool channel data structure
3380  */
3381 static void
3382 ice_get_channels(struct net_device *dev, struct ethtool_channels *ch)
3383 {
3384     struct ice_netdev_priv *np = netdev_priv(dev);
3385     struct ice_vsi *vsi = np->vsi;
3386     struct ice_pf *pf = vsi->back;
3387 
3388     /* report maximum channels */
3389     ch->max_rx = ice_get_max_rxq(pf);
3390     ch->max_tx = ice_get_max_txq(pf);
3391     ch->max_combined = min_t(int, ch->max_rx, ch->max_tx);
3392 
3393     /* report current channels */
3394     ch->combined_count = ice_get_combined_cnt(vsi);
3395     ch->rx_count = vsi->num_rxq - ch->combined_count;
3396     ch->tx_count = vsi->num_txq - ch->combined_count;
3397 
3398     /* report other queues */
3399     ch->other_count = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
3400     ch->max_other = ch->other_count;
3401 }
3402 
3403 /**
3404  * ice_get_valid_rss_size - return valid number of RSS queues
3405  * @hw: pointer to the HW structure
3406  * @new_size: requested RSS queues
3407  */
3408 static int ice_get_valid_rss_size(struct ice_hw *hw, int new_size)
3409 {
3410     struct ice_hw_common_caps *caps = &hw->func_caps.common_cap;
3411 
3412     return min_t(int, new_size, BIT(caps->rss_table_entry_width));
3413 }
3414 
3415 /**
3416  * ice_vsi_set_dflt_rss_lut - set default RSS LUT with requested RSS size
3417  * @vsi: VSI to reconfigure RSS LUT on
3418  * @req_rss_size: requested range of queue numbers for hashing
3419  *
3420  * Set the VSI's RSS parameters, configure the RSS LUT based on these.
3421  */
3422 static int ice_vsi_set_dflt_rss_lut(struct ice_vsi *vsi, int req_rss_size)
3423 {
3424     struct ice_pf *pf = vsi->back;
3425     struct device *dev;
3426     struct ice_hw *hw;
3427     int err;
3428     u8 *lut;
3429 
3430     dev = ice_pf_to_dev(pf);
3431     hw = &pf->hw;
3432 
3433     if (!req_rss_size)
3434         return -EINVAL;
3435 
3436     lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3437     if (!lut)
3438         return -ENOMEM;
3439 
3440     /* set RSS LUT parameters */
3441     if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3442         vsi->rss_size = 1;
3443     else
3444         vsi->rss_size = ice_get_valid_rss_size(hw, req_rss_size);
3445 
3446     /* create/set RSS LUT */
3447     ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
3448     err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
3449     if (err)
3450         dev_err(dev, "Cannot set RSS lut, err %d aq_err %s\n", err,
3451             ice_aq_str(hw->adminq.sq_last_status));
3452 
3453     kfree(lut);
3454     return err;
3455 }
3456 
3457 /**
3458  * ice_set_channels - set the number channels
3459  * @dev: network interface device structure
3460  * @ch: ethtool channel data structure
3461  */
3462 static int ice_set_channels(struct net_device *dev, struct ethtool_channels *ch)
3463 {
3464     struct ice_netdev_priv *np = netdev_priv(dev);
3465     struct ice_vsi *vsi = np->vsi;
3466     struct ice_pf *pf = vsi->back;
3467     int new_rx = 0, new_tx = 0;
3468     u32 curr_combined;
3469 
3470     /* do not support changing channels in Safe Mode */
3471     if (ice_is_safe_mode(pf)) {
3472         netdev_err(dev, "Changing channel in Safe Mode is not supported\n");
3473         return -EOPNOTSUPP;
3474     }
3475     /* do not support changing other_count */
3476     if (ch->other_count != (test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1U : 0U))
3477         return -EINVAL;
3478 
3479     if (ice_is_adq_active(pf)) {
3480         netdev_err(dev, "Cannot set channels with ADQ configured.\n");
3481         return -EOPNOTSUPP;
3482     }
3483 
3484     if (test_bit(ICE_FLAG_FD_ENA, pf->flags) && pf->hw.fdir_active_fltr) {
3485         netdev_err(dev, "Cannot set channels when Flow Director filters are active\n");
3486         return -EOPNOTSUPP;
3487     }
3488 
3489     curr_combined = ice_get_combined_cnt(vsi);
3490 
3491     /* these checks are for cases where user didn't specify a particular
3492      * value on cmd line but we get non-zero value anyway via
3493      * get_channels(); look at ethtool.c in ethtool repository (the user
3494      * space part), particularly, do_schannels() routine
3495      */
3496     if (ch->rx_count == vsi->num_rxq - curr_combined)
3497         ch->rx_count = 0;
3498     if (ch->tx_count == vsi->num_txq - curr_combined)
3499         ch->tx_count = 0;
3500     if (ch->combined_count == curr_combined)
3501         ch->combined_count = 0;
3502 
3503     if (!(ch->combined_count || (ch->rx_count && ch->tx_count))) {
3504         netdev_err(dev, "Please specify at least 1 Rx and 1 Tx channel\n");
3505         return -EINVAL;
3506     }
3507 
3508     new_rx = ch->combined_count + ch->rx_count;
3509     new_tx = ch->combined_count + ch->tx_count;
3510 
3511     if (new_rx < vsi->tc_cfg.numtc) {
3512         netdev_err(dev, "Cannot set less Rx channels, than Traffic Classes you have (%u)\n",
3513                vsi->tc_cfg.numtc);
3514         return -EINVAL;
3515     }
3516     if (new_tx < vsi->tc_cfg.numtc) {
3517         netdev_err(dev, "Cannot set less Tx channels, than Traffic Classes you have (%u)\n",
3518                vsi->tc_cfg.numtc);
3519         return -EINVAL;
3520     }
3521     if (new_rx > ice_get_max_rxq(pf)) {
3522         netdev_err(dev, "Maximum allowed Rx channels is %d\n",
3523                ice_get_max_rxq(pf));
3524         return -EINVAL;
3525     }
3526     if (new_tx > ice_get_max_txq(pf)) {
3527         netdev_err(dev, "Maximum allowed Tx channels is %d\n",
3528                ice_get_max_txq(pf));
3529         return -EINVAL;
3530     }
3531 
3532     ice_vsi_recfg_qs(vsi, new_rx, new_tx);
3533 
3534     if (!netif_is_rxfh_configured(dev))
3535         return ice_vsi_set_dflt_rss_lut(vsi, new_rx);
3536 
3537     /* Update rss_size due to change in Rx queues */
3538     vsi->rss_size = ice_get_valid_rss_size(&pf->hw, new_rx);
3539 
3540     return 0;
3541 }
3542 
3543 /**
3544  * ice_get_wol - get current Wake on LAN configuration
3545  * @netdev: network interface device structure
3546  * @wol: Ethtool structure to retrieve WoL settings
3547  */
3548 static void ice_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3549 {
3550     struct ice_netdev_priv *np = netdev_priv(netdev);
3551     struct ice_pf *pf = np->vsi->back;
3552 
3553     if (np->vsi->type != ICE_VSI_PF)
3554         netdev_warn(netdev, "Wake on LAN is not supported on this interface!\n");
3555 
3556     /* Get WoL settings based on the HW capability */
3557     if (ice_is_wol_supported(&pf->hw)) {
3558         wol->supported = WAKE_MAGIC;
3559         wol->wolopts = pf->wol_ena ? WAKE_MAGIC : 0;
3560     } else {
3561         wol->supported = 0;
3562         wol->wolopts = 0;
3563     }
3564 }
3565 
3566 /**
3567  * ice_set_wol - set Wake on LAN on supported device
3568  * @netdev: network interface device structure
3569  * @wol: Ethtool structure to set WoL
3570  */
3571 static int ice_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3572 {
3573     struct ice_netdev_priv *np = netdev_priv(netdev);
3574     struct ice_vsi *vsi = np->vsi;
3575     struct ice_pf *pf = vsi->back;
3576 
3577     if (vsi->type != ICE_VSI_PF || !ice_is_wol_supported(&pf->hw))
3578         return -EOPNOTSUPP;
3579 
3580     /* only magic packet is supported */
3581     if (wol->wolopts && wol->wolopts != WAKE_MAGIC)
3582         return -EOPNOTSUPP;
3583 
3584     /* Set WoL only if there is a new value */
3585     if (pf->wol_ena != !!wol->wolopts) {
3586         pf->wol_ena = !!wol->wolopts;
3587         device_set_wakeup_enable(ice_pf_to_dev(pf), pf->wol_ena);
3588         netdev_dbg(netdev, "WoL magic packet %sabled\n",
3589                pf->wol_ena ? "en" : "dis");
3590     }
3591 
3592     return 0;
3593 }
3594 
3595 /**
3596  * ice_get_rc_coalesce - get ITR values for specific ring container
3597  * @ec: ethtool structure to fill with driver's coalesce settings
3598  * @rc: ring container that the ITR values will come from
3599  *
3600  * Query the device for ice_ring_container specific ITR values. This is
3601  * done per ice_ring_container because each q_vector can have 1 or more rings
3602  * and all of said ring(s) will have the same ITR values.
3603  *
3604  * Returns 0 on success, negative otherwise.
3605  */
3606 static int
3607 ice_get_rc_coalesce(struct ethtool_coalesce *ec, struct ice_ring_container *rc)
3608 {
3609     if (!rc->rx_ring)
3610         return -EINVAL;
3611 
3612     switch (rc->type) {
3613     case ICE_RX_CONTAINER:
3614         ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc);
3615         ec->rx_coalesce_usecs = rc->itr_setting;
3616         ec->rx_coalesce_usecs_high = rc->rx_ring->q_vector->intrl;
3617         break;
3618     case ICE_TX_CONTAINER:
3619         ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc);
3620         ec->tx_coalesce_usecs = rc->itr_setting;
3621         break;
3622     default:
3623         dev_dbg(ice_pf_to_dev(rc->rx_ring->vsi->back), "Invalid c_type %d\n", rc->type);
3624         return -EINVAL;
3625     }
3626 
3627     return 0;
3628 }
3629 
3630 /**
3631  * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings
3632  * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings
3633  * @ec: coalesce settings to program the device with
3634  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3635  *
3636  * Return 0 on success, and negative under the following conditions:
3637  * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed.
3638  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3639  */
3640 static int
3641 ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3642 {
3643     if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3644         if (ice_get_rc_coalesce(ec,
3645                     &vsi->rx_rings[q_num]->q_vector->rx))
3646             return -EINVAL;
3647         if (ice_get_rc_coalesce(ec,
3648                     &vsi->tx_rings[q_num]->q_vector->tx))
3649             return -EINVAL;
3650     } else if (q_num < vsi->num_rxq) {
3651         if (ice_get_rc_coalesce(ec,
3652                     &vsi->rx_rings[q_num]->q_vector->rx))
3653             return -EINVAL;
3654     } else if (q_num < vsi->num_txq) {
3655         if (ice_get_rc_coalesce(ec,
3656                     &vsi->tx_rings[q_num]->q_vector->tx))
3657             return -EINVAL;
3658     } else {
3659         return -EINVAL;
3660     }
3661 
3662     return 0;
3663 }
3664 
3665 /**
3666  * __ice_get_coalesce - get ITR/INTRL values for the device
3667  * @netdev: pointer to the netdev associated with this query
3668  * @ec: ethtool structure to fill with driver's coalesce settings
3669  * @q_num: queue number to get the coalesce settings for
3670  *
3671  * If the caller passes in a negative q_num then we return coalesce settings
3672  * based on queue number 0, else use the actual q_num passed in.
3673  */
3674 static int
3675 __ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3676            int q_num)
3677 {
3678     struct ice_netdev_priv *np = netdev_priv(netdev);
3679     struct ice_vsi *vsi = np->vsi;
3680 
3681     if (q_num < 0)
3682         q_num = 0;
3683 
3684     if (ice_get_q_coalesce(vsi, ec, q_num))
3685         return -EINVAL;
3686 
3687     return 0;
3688 }
3689 
3690 static int ice_get_coalesce(struct net_device *netdev,
3691                 struct ethtool_coalesce *ec,
3692                 struct kernel_ethtool_coalesce *kernel_coal,
3693                 struct netlink_ext_ack *extack)
3694 {
3695     return __ice_get_coalesce(netdev, ec, -1);
3696 }
3697 
3698 static int
3699 ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
3700                struct ethtool_coalesce *ec)
3701 {
3702     return __ice_get_coalesce(netdev, ec, q_num);
3703 }
3704 
3705 /**
3706  * ice_set_rc_coalesce - set ITR values for specific ring container
3707  * @ec: ethtool structure from user to update ITR settings
3708  * @rc: ring container that the ITR values will come from
3709  * @vsi: VSI associated to the ring container
3710  *
3711  * Set specific ITR values. This is done per ice_ring_container because each
3712  * q_vector can have 1 or more rings and all of said ring(s) will have the same
3713  * ITR values.
3714  *
3715  * Returns 0 on success, negative otherwise.
3716  */
3717 static int
3718 ice_set_rc_coalesce(struct ethtool_coalesce *ec,
3719             struct ice_ring_container *rc, struct ice_vsi *vsi)
3720 {
3721     const char *c_type_str = (rc->type == ICE_RX_CONTAINER) ? "rx" : "tx";
3722     u32 use_adaptive_coalesce, coalesce_usecs;
3723     struct ice_pf *pf = vsi->back;
3724     u16 itr_setting;
3725 
3726     if (!rc->rx_ring)
3727         return -EINVAL;
3728 
3729     switch (rc->type) {
3730     case ICE_RX_CONTAINER:
3731     {
3732         struct ice_q_vector *q_vector = rc->rx_ring->q_vector;
3733 
3734         if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL ||
3735             (ec->rx_coalesce_usecs_high &&
3736              ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) {
3737             netdev_info(vsi->netdev, "Invalid value, %s-usecs-high valid values are 0 (disabled), %d-%d\n",
3738                     c_type_str, pf->hw.intrl_gran,
3739                     ICE_MAX_INTRL);
3740             return -EINVAL;
3741         }
3742         if (ec->rx_coalesce_usecs_high != q_vector->intrl &&
3743             (ec->use_adaptive_rx_coalesce || ec->use_adaptive_tx_coalesce)) {
3744             netdev_info(vsi->netdev, "Invalid value, %s-usecs-high cannot be changed if adaptive-tx or adaptive-rx is enabled\n",
3745                     c_type_str);
3746             return -EINVAL;
3747         }
3748         if (ec->rx_coalesce_usecs_high != q_vector->intrl)
3749             q_vector->intrl = ec->rx_coalesce_usecs_high;
3750 
3751         use_adaptive_coalesce = ec->use_adaptive_rx_coalesce;
3752         coalesce_usecs = ec->rx_coalesce_usecs;
3753 
3754         break;
3755     }
3756     case ICE_TX_CONTAINER:
3757         use_adaptive_coalesce = ec->use_adaptive_tx_coalesce;
3758         coalesce_usecs = ec->tx_coalesce_usecs;
3759 
3760         break;
3761     default:
3762         dev_dbg(ice_pf_to_dev(pf), "Invalid container type %d\n",
3763             rc->type);
3764         return -EINVAL;
3765     }
3766 
3767     itr_setting = rc->itr_setting;
3768     if (coalesce_usecs != itr_setting && use_adaptive_coalesce) {
3769         netdev_info(vsi->netdev, "%s interrupt throttling cannot be changed if adaptive-%s is enabled\n",
3770                 c_type_str, c_type_str);
3771         return -EINVAL;
3772     }
3773 
3774     if (coalesce_usecs > ICE_ITR_MAX) {
3775         netdev_info(vsi->netdev, "Invalid value, %s-usecs range is 0-%d\n",
3776                 c_type_str, ICE_ITR_MAX);
3777         return -EINVAL;
3778     }
3779 
3780     if (use_adaptive_coalesce) {
3781         rc->itr_mode = ITR_DYNAMIC;
3782     } else {
3783         rc->itr_mode = ITR_STATIC;
3784         /* store user facing value how it was set */
3785         rc->itr_setting = coalesce_usecs;
3786         /* write the change to the register */
3787         ice_write_itr(rc, coalesce_usecs);
3788         /* force writes to take effect immediately, the flush shouldn't
3789          * be done in the functions above because the intent is for
3790          * them to do lazy writes.
3791          */
3792         ice_flush(&pf->hw);
3793     }
3794 
3795     return 0;
3796 }
3797 
3798 /**
3799  * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings
3800  * @vsi: VSI associated to the queue that need updating
3801  * @ec: coalesce settings to program the device with
3802  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3803  *
3804  * Return 0 on success, and negative under the following conditions:
3805  * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed.
3806  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3807  */
3808 static int
3809 ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3810 {
3811     if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3812         if (ice_set_rc_coalesce(ec,
3813                     &vsi->rx_rings[q_num]->q_vector->rx,
3814                     vsi))
3815             return -EINVAL;
3816 
3817         if (ice_set_rc_coalesce(ec,
3818                     &vsi->tx_rings[q_num]->q_vector->tx,
3819                     vsi))
3820             return -EINVAL;
3821     } else if (q_num < vsi->num_rxq) {
3822         if (ice_set_rc_coalesce(ec,
3823                     &vsi->rx_rings[q_num]->q_vector->rx,
3824                     vsi))
3825             return -EINVAL;
3826     } else if (q_num < vsi->num_txq) {
3827         if (ice_set_rc_coalesce(ec,
3828                     &vsi->tx_rings[q_num]->q_vector->tx,
3829                     vsi))
3830             return -EINVAL;
3831     } else {
3832         return -EINVAL;
3833     }
3834 
3835     return 0;
3836 }
3837 
3838 /**
3839  * ice_print_if_odd_usecs - print message if user tries to set odd [tx|rx]-usecs
3840  * @netdev: netdev used for print
3841  * @itr_setting: previous user setting
3842  * @use_adaptive_coalesce: if adaptive coalesce is enabled or being enabled
3843  * @coalesce_usecs: requested value of [tx|rx]-usecs
3844  * @c_type_str: either "rx" or "tx" to match user set field of [tx|rx]-usecs
3845  */
3846 static void
3847 ice_print_if_odd_usecs(struct net_device *netdev, u16 itr_setting,
3848                u32 use_adaptive_coalesce, u32 coalesce_usecs,
3849                const char *c_type_str)
3850 {
3851     if (use_adaptive_coalesce)
3852         return;
3853 
3854     if (itr_setting != coalesce_usecs && (coalesce_usecs % 2))
3855         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",
3856                 c_type_str, coalesce_usecs, c_type_str,
3857                 ITR_REG_ALIGN(coalesce_usecs));
3858 }
3859 
3860 /**
3861  * __ice_set_coalesce - set ITR/INTRL values for the device
3862  * @netdev: pointer to the netdev associated with this query
3863  * @ec: ethtool structure to fill with driver's coalesce settings
3864  * @q_num: queue number to get the coalesce settings for
3865  *
3866  * If the caller passes in a negative q_num then we set the coalesce settings
3867  * for all Tx/Rx queues, else use the actual q_num passed in.
3868  */
3869 static int
3870 __ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3871            int q_num)
3872 {
3873     struct ice_netdev_priv *np = netdev_priv(netdev);
3874     struct ice_vsi *vsi = np->vsi;
3875 
3876     if (q_num < 0) {
3877         struct ice_q_vector *q_vector = vsi->q_vectors[0];
3878         int v_idx;
3879 
3880         if (q_vector) {
3881             ice_print_if_odd_usecs(netdev, q_vector->rx.itr_setting,
3882                            ec->use_adaptive_rx_coalesce,
3883                            ec->rx_coalesce_usecs, "rx");
3884 
3885             ice_print_if_odd_usecs(netdev, q_vector->tx.itr_setting,
3886                            ec->use_adaptive_tx_coalesce,
3887                            ec->tx_coalesce_usecs, "tx");
3888         }
3889 
3890         ice_for_each_q_vector(vsi, v_idx) {
3891             /* In some cases if DCB is configured the num_[rx|tx]q
3892              * can be less than vsi->num_q_vectors. This check
3893              * accounts for that so we don't report a false failure
3894              */
3895             if (v_idx >= vsi->num_rxq && v_idx >= vsi->num_txq)
3896                 goto set_complete;
3897 
3898             if (ice_set_q_coalesce(vsi, ec, v_idx))
3899                 return -EINVAL;
3900 
3901             ice_set_q_vector_intrl(vsi->q_vectors[v_idx]);
3902         }
3903         goto set_complete;
3904     }
3905 
3906     if (ice_set_q_coalesce(vsi, ec, q_num))
3907         return -EINVAL;
3908 
3909     ice_set_q_vector_intrl(vsi->q_vectors[q_num]);
3910 
3911 set_complete:
3912     return 0;
3913 }
3914 
3915 static int ice_set_coalesce(struct net_device *netdev,
3916                 struct ethtool_coalesce *ec,
3917                 struct kernel_ethtool_coalesce *kernel_coal,
3918                 struct netlink_ext_ack *extack)
3919 {
3920     return __ice_set_coalesce(netdev, ec, -1);
3921 }
3922 
3923 static int
3924 ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
3925                struct ethtool_coalesce *ec)
3926 {
3927     return __ice_set_coalesce(netdev, ec, q_num);
3928 }
3929 
3930 static void
3931 ice_repr_get_drvinfo(struct net_device *netdev,
3932              struct ethtool_drvinfo *drvinfo)
3933 {
3934     struct ice_repr *repr = ice_netdev_to_repr(netdev);
3935 
3936     if (ice_check_vf_ready_for_cfg(repr->vf))
3937         return;
3938 
3939     __ice_get_drvinfo(netdev, drvinfo, repr->src_vsi);
3940 }
3941 
3942 static void
3943 ice_repr_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
3944 {
3945     struct ice_repr *repr = ice_netdev_to_repr(netdev);
3946 
3947     /* for port representors only ETH_SS_STATS is supported */
3948     if (ice_check_vf_ready_for_cfg(repr->vf) ||
3949         stringset != ETH_SS_STATS)
3950         return;
3951 
3952     __ice_get_strings(netdev, stringset, data, repr->src_vsi);
3953 }
3954 
3955 static void
3956 ice_repr_get_ethtool_stats(struct net_device *netdev,
3957                struct ethtool_stats __always_unused *stats,
3958                u64 *data)
3959 {
3960     struct ice_repr *repr = ice_netdev_to_repr(netdev);
3961 
3962     if (ice_check_vf_ready_for_cfg(repr->vf))
3963         return;
3964 
3965     __ice_get_ethtool_stats(netdev, stats, data, repr->src_vsi);
3966 }
3967 
3968 static int ice_repr_get_sset_count(struct net_device *netdev, int sset)
3969 {
3970     switch (sset) {
3971     case ETH_SS_STATS:
3972         return ICE_VSI_STATS_LEN;
3973     default:
3974         return -EOPNOTSUPP;
3975     }
3976 }
3977 
3978 #define ICE_I2C_EEPROM_DEV_ADDR     0xA0
3979 #define ICE_I2C_EEPROM_DEV_ADDR2    0xA2
3980 #define ICE_MODULE_TYPE_SFP     0x03
3981 #define ICE_MODULE_TYPE_QSFP_PLUS   0x0D
3982 #define ICE_MODULE_TYPE_QSFP28      0x11
3983 #define ICE_MODULE_SFF_ADDR_MODE    0x04
3984 #define ICE_MODULE_SFF_DIAG_CAPAB   0x40
3985 #define ICE_MODULE_REVISION_ADDR    0x01
3986 #define ICE_MODULE_SFF_8472_COMP    0x5E
3987 #define ICE_MODULE_SFF_8472_SWAP    0x5C
3988 #define ICE_MODULE_QSFP_MAX_LEN     640
3989 
3990 /**
3991  * ice_get_module_info - get SFF module type and revision information
3992  * @netdev: network interface device structure
3993  * @modinfo: module EEPROM size and layout information structure
3994  */
3995 static int
3996 ice_get_module_info(struct net_device *netdev,
3997             struct ethtool_modinfo *modinfo)
3998 {
3999     struct ice_netdev_priv *np = netdev_priv(netdev);
4000     struct ice_vsi *vsi = np->vsi;
4001     struct ice_pf *pf = vsi->back;
4002     struct ice_hw *hw = &pf->hw;
4003     u8 sff8472_comp = 0;
4004     u8 sff8472_swap = 0;
4005     u8 sff8636_rev = 0;
4006     u8 value = 0;
4007     int status;
4008 
4009     status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 0x00, 0x00,
4010                    0, &value, 1, 0, NULL);
4011     if (status)
4012         return status;
4013 
4014     switch (value) {
4015     case ICE_MODULE_TYPE_SFP:
4016         status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
4017                        ICE_MODULE_SFF_8472_COMP, 0x00, 0,
4018                        &sff8472_comp, 1, 0, NULL);
4019         if (status)
4020             return status;
4021         status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
4022                        ICE_MODULE_SFF_8472_SWAP, 0x00, 0,
4023                        &sff8472_swap, 1, 0, NULL);
4024         if (status)
4025             return status;
4026 
4027         if (sff8472_swap & ICE_MODULE_SFF_ADDR_MODE) {
4028             modinfo->type = ETH_MODULE_SFF_8079;
4029             modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4030         } else if (sff8472_comp &&
4031                (sff8472_swap & ICE_MODULE_SFF_DIAG_CAPAB)) {
4032             modinfo->type = ETH_MODULE_SFF_8472;
4033             modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
4034         } else {
4035             modinfo->type = ETH_MODULE_SFF_8079;
4036             modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4037         }
4038         break;
4039     case ICE_MODULE_TYPE_QSFP_PLUS:
4040     case ICE_MODULE_TYPE_QSFP28:
4041         status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
4042                        ICE_MODULE_REVISION_ADDR, 0x00, 0,
4043                        &sff8636_rev, 1, 0, NULL);
4044         if (status)
4045             return status;
4046         /* Check revision compliance */
4047         if (sff8636_rev > 0x02) {
4048             /* Module is SFF-8636 compliant */
4049             modinfo->type = ETH_MODULE_SFF_8636;
4050             modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
4051         } else {
4052             modinfo->type = ETH_MODULE_SFF_8436;
4053             modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
4054         }
4055         break;
4056     default:
4057         netdev_warn(netdev, "SFF Module Type not recognized.\n");
4058         return -EINVAL;
4059     }
4060     return 0;
4061 }
4062 
4063 /**
4064  * ice_get_module_eeprom - fill buffer with SFF EEPROM contents
4065  * @netdev: network interface device structure
4066  * @ee: EEPROM dump request structure
4067  * @data: buffer to be filled with EEPROM contents
4068  */
4069 static int
4070 ice_get_module_eeprom(struct net_device *netdev,
4071               struct ethtool_eeprom *ee, u8 *data)
4072 {
4073     struct ice_netdev_priv *np = netdev_priv(netdev);
4074 #define SFF_READ_BLOCK_SIZE 8
4075     u8 value[SFF_READ_BLOCK_SIZE] = { 0 };
4076     u8 addr = ICE_I2C_EEPROM_DEV_ADDR;
4077     struct ice_vsi *vsi = np->vsi;
4078     struct ice_pf *pf = vsi->back;
4079     struct ice_hw *hw = &pf->hw;
4080     bool is_sfp = false;
4081     unsigned int i, j;
4082     u16 offset = 0;
4083     u8 page = 0;
4084     int status;
4085 
4086     if (!ee || !ee->len || !data)
4087         return -EINVAL;
4088 
4089     status = ice_aq_sff_eeprom(hw, 0, addr, offset, page, 0, value, 1, 0,
4090                    NULL);
4091     if (status)
4092         return status;
4093 
4094     if (value[0] == ICE_MODULE_TYPE_SFP)
4095         is_sfp = true;
4096 
4097     memset(data, 0, ee->len);
4098     for (i = 0; i < ee->len; i += SFF_READ_BLOCK_SIZE) {
4099         offset = i + ee->offset;
4100         page = 0;
4101 
4102         /* Check if we need to access the other memory page */
4103         if (is_sfp) {
4104             if (offset >= ETH_MODULE_SFF_8079_LEN) {
4105                 offset -= ETH_MODULE_SFF_8079_LEN;
4106                 addr = ICE_I2C_EEPROM_DEV_ADDR2;
4107             }
4108         } else {
4109             while (offset >= ETH_MODULE_SFF_8436_LEN) {
4110                 /* Compute memory page number and offset. */
4111                 offset -= ETH_MODULE_SFF_8436_LEN / 2;
4112                 page++;
4113             }
4114         }
4115 
4116         /* Bit 2 of EEPROM address 0x02 declares upper
4117          * pages are disabled on QSFP modules.
4118          * SFP modules only ever use page 0.
4119          */
4120         if (page == 0 || !(data[0x2] & 0x4)) {
4121             /* If i2c bus is busy due to slow page change or
4122              * link management access, call can fail. This is normal.
4123              * So we retry this a few times.
4124              */
4125             for (j = 0; j < 4; j++) {
4126                 status = ice_aq_sff_eeprom(hw, 0, addr, offset, page,
4127                                !is_sfp, value,
4128                                SFF_READ_BLOCK_SIZE,
4129                                0, NULL);
4130                 netdev_dbg(netdev, "SFF %02X %02X %02X %X = %02X%02X%02X%02X.%02X%02X%02X%02X (%X)\n",
4131                        addr, offset, page, is_sfp,
4132                        value[0], value[1], value[2], value[3],
4133                        value[4], value[5], value[6], value[7],
4134                        status);
4135                 if (status) {
4136                     usleep_range(1500, 2500);
4137                     memset(value, 0, SFF_READ_BLOCK_SIZE);
4138                     continue;
4139                 }
4140                 break;
4141             }
4142 
4143             /* Make sure we have enough room for the new block */
4144             if ((i + SFF_READ_BLOCK_SIZE) < ee->len)
4145                 memcpy(data + i, value, SFF_READ_BLOCK_SIZE);
4146         }
4147     }
4148     return 0;
4149 }
4150 
4151 static const struct ethtool_ops ice_ethtool_ops = {
4152     .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
4153                      ETHTOOL_COALESCE_USE_ADAPTIVE |
4154                      ETHTOOL_COALESCE_RX_USECS_HIGH,
4155     .get_link_ksettings = ice_get_link_ksettings,
4156     .set_link_ksettings = ice_set_link_ksettings,
4157     .get_drvinfo        = ice_get_drvinfo,
4158     .get_regs_len       = ice_get_regs_len,
4159     .get_regs       = ice_get_regs,
4160     .get_wol        = ice_get_wol,
4161     .set_wol        = ice_set_wol,
4162     .get_msglevel       = ice_get_msglevel,
4163     .set_msglevel       = ice_set_msglevel,
4164     .self_test      = ice_self_test,
4165     .get_link       = ethtool_op_get_link,
4166     .get_eeprom_len     = ice_get_eeprom_len,
4167     .get_eeprom     = ice_get_eeprom,
4168     .get_coalesce       = ice_get_coalesce,
4169     .set_coalesce       = ice_set_coalesce,
4170     .get_strings        = ice_get_strings,
4171     .set_phys_id        = ice_set_phys_id,
4172     .get_ethtool_stats      = ice_get_ethtool_stats,
4173     .get_priv_flags     = ice_get_priv_flags,
4174     .set_priv_flags     = ice_set_priv_flags,
4175     .get_sset_count     = ice_get_sset_count,
4176     .get_rxnfc      = ice_get_rxnfc,
4177     .set_rxnfc      = ice_set_rxnfc,
4178     .get_ringparam      = ice_get_ringparam,
4179     .set_ringparam      = ice_set_ringparam,
4180     .nway_reset     = ice_nway_reset,
4181     .get_pauseparam     = ice_get_pauseparam,
4182     .set_pauseparam     = ice_set_pauseparam,
4183     .get_rxfh_key_size  = ice_get_rxfh_key_size,
4184     .get_rxfh_indir_size    = ice_get_rxfh_indir_size,
4185     .get_rxfh_context   = ice_get_rxfh_context,
4186     .get_rxfh       = ice_get_rxfh,
4187     .set_rxfh       = ice_set_rxfh,
4188     .get_channels       = ice_get_channels,
4189     .set_channels       = ice_set_channels,
4190     .get_ts_info        = ice_get_ts_info,
4191     .get_per_queue_coalesce = ice_get_per_q_coalesce,
4192     .set_per_queue_coalesce = ice_set_per_q_coalesce,
4193     .get_fecparam       = ice_get_fecparam,
4194     .set_fecparam       = ice_set_fecparam,
4195     .get_module_info    = ice_get_module_info,
4196     .get_module_eeprom  = ice_get_module_eeprom,
4197 };
4198 
4199 static const struct ethtool_ops ice_ethtool_safe_mode_ops = {
4200     .get_link_ksettings = ice_get_link_ksettings,
4201     .set_link_ksettings = ice_set_link_ksettings,
4202     .get_drvinfo        = ice_get_drvinfo,
4203     .get_regs_len       = ice_get_regs_len,
4204     .get_regs       = ice_get_regs,
4205     .get_wol        = ice_get_wol,
4206     .set_wol        = ice_set_wol,
4207     .get_msglevel       = ice_get_msglevel,
4208     .set_msglevel       = ice_set_msglevel,
4209     .get_link       = ethtool_op_get_link,
4210     .get_eeprom_len     = ice_get_eeprom_len,
4211     .get_eeprom     = ice_get_eeprom,
4212     .get_strings        = ice_get_strings,
4213     .get_ethtool_stats  = ice_get_ethtool_stats,
4214     .get_sset_count     = ice_get_sset_count,
4215     .get_ringparam      = ice_get_ringparam,
4216     .set_ringparam      = ice_set_ringparam,
4217     .nway_reset     = ice_nway_reset,
4218     .get_channels       = ice_get_channels,
4219 };
4220 
4221 /**
4222  * ice_set_ethtool_safe_mode_ops - setup safe mode ethtool ops
4223  * @netdev: network interface device structure
4224  */
4225 void ice_set_ethtool_safe_mode_ops(struct net_device *netdev)
4226 {
4227     netdev->ethtool_ops = &ice_ethtool_safe_mode_ops;
4228 }
4229 
4230 static const struct ethtool_ops ice_ethtool_repr_ops = {
4231     .get_drvinfo        = ice_repr_get_drvinfo,
4232     .get_link       = ethtool_op_get_link,
4233     .get_strings        = ice_repr_get_strings,
4234     .get_ethtool_stats      = ice_repr_get_ethtool_stats,
4235     .get_sset_count     = ice_repr_get_sset_count,
4236 };
4237 
4238 /**
4239  * ice_set_ethtool_repr_ops - setup VF's port representor ethtool ops
4240  * @netdev: network interface device structure
4241  */
4242 void ice_set_ethtool_repr_ops(struct net_device *netdev)
4243 {
4244     netdev->ethtool_ops = &ice_ethtool_repr_ops;
4245 }
4246 
4247 /**
4248  * ice_set_ethtool_ops - setup netdev ethtool ops
4249  * @netdev: network interface device structure
4250  *
4251  * setup netdev ethtool ops with ice specific ops
4252  */
4253 void ice_set_ethtool_ops(struct net_device *netdev)
4254 {
4255     netdev->ethtool_ops = &ice_ethtool_ops;
4256 }