Linux Audio

Check our new training course

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