Linux Audio

Check our new training course

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