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