Linux Audio

Check our new training course

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