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