1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3 
4 /* ethtool support for ice */
5 
6 #include "ice.h"
7 #include "ice_lib.h"
8 #include "ice_dcb_lib.h"
9 
10 struct ice_stats {
11 	char stat_string[ETH_GSTRING_LEN];
12 	int sizeof_stat;
13 	int stat_offset;
14 };
15 
16 #define ICE_STAT(_type, _name, _stat) { \
17 	.stat_string = _name, \
18 	.sizeof_stat = FIELD_SIZEOF(_type, _stat), \
19 	.stat_offset = offsetof(_type, _stat) \
20 }
21 
22 #define ICE_VSI_STAT(_name, _stat) \
23 		ICE_STAT(struct ice_vsi, _name, _stat)
24 #define ICE_PF_STAT(_name, _stat) \
25 		ICE_STAT(struct ice_pf, _name, _stat)
26 
27 static int ice_q_stats_len(struct net_device *netdev)
28 {
29 	struct ice_netdev_priv *np = netdev_priv(netdev);
30 
31 	return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) *
32 		(sizeof(struct ice_q_stats) / sizeof(u64)));
33 }
34 
35 #define ICE_PF_STATS_LEN	ARRAY_SIZE(ice_gstrings_pf_stats)
36 #define ICE_VSI_STATS_LEN	ARRAY_SIZE(ice_gstrings_vsi_stats)
37 
38 #define ICE_PFC_STATS_LEN ( \
39 		(FIELD_SIZEOF(struct ice_pf, stats.priority_xoff_rx) + \
40 		 FIELD_SIZEOF(struct ice_pf, stats.priority_xon_rx) + \
41 		 FIELD_SIZEOF(struct ice_pf, stats.priority_xoff_tx) + \
42 		 FIELD_SIZEOF(struct ice_pf, stats.priority_xon_tx)) \
43 		 / sizeof(u64))
44 #define ICE_ALL_STATS_LEN(n)	(ICE_PF_STATS_LEN + ICE_PFC_STATS_LEN + \
45 				 ICE_VSI_STATS_LEN + ice_q_stats_len(n))
46 
47 static const struct ice_stats ice_gstrings_vsi_stats[] = {
48 	ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
49 	ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
50 	ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
51 	ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
52 	ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
53 	ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
54 	ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes),
55 	ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes),
56 	ICE_VSI_STAT("rx_dropped", eth_stats.rx_discards),
57 	ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
58 	ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed),
59 	ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
60 	ICE_VSI_STAT("tx_errors", eth_stats.tx_errors),
61 	ICE_VSI_STAT("tx_linearize", tx_linearize),
62 };
63 
64 enum ice_ethtool_test_id {
65 	ICE_ETH_TEST_REG = 0,
66 	ICE_ETH_TEST_EEPROM,
67 	ICE_ETH_TEST_INTR,
68 	ICE_ETH_TEST_LOOP,
69 	ICE_ETH_TEST_LINK,
70 };
71 
72 static const char ice_gstrings_test[][ETH_GSTRING_LEN] = {
73 	"Register test  (offline)",
74 	"EEPROM test    (offline)",
75 	"Interrupt test (offline)",
76 	"Loopback test  (offline)",
77 	"Link test   (on/offline)",
78 };
79 
80 #define ICE_TEST_LEN (sizeof(ice_gstrings_test) / ETH_GSTRING_LEN)
81 
82 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
83  * but they aren't. This device is capable of supporting multiple
84  * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual
85  * netdevs whereas the PF_STATs are for the physical function that's
86  * hosting these netdevs.
87  *
88  * The PF_STATs are appended to the netdev stats only when ethtool -S
89  * is queried on the base PF netdev.
90  */
91 static const struct ice_stats ice_gstrings_pf_stats[] = {
92 	ICE_PF_STAT("rx_bytes.nic", stats.eth.rx_bytes),
93 	ICE_PF_STAT("tx_bytes.nic", stats.eth.tx_bytes),
94 	ICE_PF_STAT("rx_unicast.nic", stats.eth.rx_unicast),
95 	ICE_PF_STAT("tx_unicast.nic", stats.eth.tx_unicast),
96 	ICE_PF_STAT("rx_multicast.nic", stats.eth.rx_multicast),
97 	ICE_PF_STAT("tx_multicast.nic", stats.eth.tx_multicast),
98 	ICE_PF_STAT("rx_broadcast.nic", stats.eth.rx_broadcast),
99 	ICE_PF_STAT("tx_broadcast.nic", stats.eth.tx_broadcast),
100 	ICE_PF_STAT("tx_errors.nic", stats.eth.tx_errors),
101 	ICE_PF_STAT("rx_size_64.nic", stats.rx_size_64),
102 	ICE_PF_STAT("tx_size_64.nic", stats.tx_size_64),
103 	ICE_PF_STAT("rx_size_127.nic", stats.rx_size_127),
104 	ICE_PF_STAT("tx_size_127.nic", stats.tx_size_127),
105 	ICE_PF_STAT("rx_size_255.nic", stats.rx_size_255),
106 	ICE_PF_STAT("tx_size_255.nic", stats.tx_size_255),
107 	ICE_PF_STAT("rx_size_511.nic", stats.rx_size_511),
108 	ICE_PF_STAT("tx_size_511.nic", stats.tx_size_511),
109 	ICE_PF_STAT("rx_size_1023.nic", stats.rx_size_1023),
110 	ICE_PF_STAT("tx_size_1023.nic", stats.tx_size_1023),
111 	ICE_PF_STAT("rx_size_1522.nic", stats.rx_size_1522),
112 	ICE_PF_STAT("tx_size_1522.nic", stats.tx_size_1522),
113 	ICE_PF_STAT("rx_size_big.nic", stats.rx_size_big),
114 	ICE_PF_STAT("tx_size_big.nic", stats.tx_size_big),
115 	ICE_PF_STAT("link_xon_rx.nic", stats.link_xon_rx),
116 	ICE_PF_STAT("link_xon_tx.nic", stats.link_xon_tx),
117 	ICE_PF_STAT("link_xoff_rx.nic", stats.link_xoff_rx),
118 	ICE_PF_STAT("link_xoff_tx.nic", stats.link_xoff_tx),
119 	ICE_PF_STAT("tx_dropped_link_down.nic", stats.tx_dropped_link_down),
120 	ICE_PF_STAT("rx_undersize.nic", stats.rx_undersize),
121 	ICE_PF_STAT("rx_fragments.nic", stats.rx_fragments),
122 	ICE_PF_STAT("rx_oversize.nic", stats.rx_oversize),
123 	ICE_PF_STAT("rx_jabber.nic", stats.rx_jabber),
124 	ICE_PF_STAT("rx_csum_bad.nic", hw_csum_rx_error),
125 	ICE_PF_STAT("rx_length_errors.nic", stats.rx_len_errors),
126 	ICE_PF_STAT("rx_dropped.nic", stats.eth.rx_discards),
127 	ICE_PF_STAT("rx_crc_errors.nic", stats.crc_errors),
128 	ICE_PF_STAT("illegal_bytes.nic", stats.illegal_bytes),
129 	ICE_PF_STAT("mac_local_faults.nic", stats.mac_local_faults),
130 	ICE_PF_STAT("mac_remote_faults.nic", stats.mac_remote_faults),
131 };
132 
133 static const u32 ice_regs_dump_list[] = {
134 	PFGEN_STATE,
135 	PRTGEN_STATUS,
136 	QRX_CTRL(0),
137 	QINT_TQCTL(0),
138 	QINT_RQCTL(0),
139 	PFINT_OICR_ENA,
140 	QRX_ITR(0),
141 	PF0INT_ITR_0(0),
142 	PF0INT_ITR_1(0),
143 	PF0INT_ITR_2(0),
144 };
145 
146 struct ice_priv_flag {
147 	char name[ETH_GSTRING_LEN];
148 	u32 bitno;			/* bit position in pf->flags */
149 };
150 
151 #define ICE_PRIV_FLAG(_name, _bitno) { \
152 	.name = _name, \
153 	.bitno = _bitno, \
154 }
155 
156 static const struct ice_priv_flag ice_gstrings_priv_flags[] = {
157 	ICE_PRIV_FLAG("link-down-on-close", ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA),
158 	ICE_PRIV_FLAG("enable-fw-lldp", ICE_FLAG_ENABLE_FW_LLDP),
159 };
160 
161 #define ICE_PRIV_FLAG_ARRAY_SIZE	ARRAY_SIZE(ice_gstrings_priv_flags)
162 
163 /**
164  * ice_nvm_version_str - format the NVM version strings
165  * @hw: ptr to the hardware info
166  */
167 static char *ice_nvm_version_str(struct ice_hw *hw)
168 {
169 	static char buf[ICE_ETHTOOL_FWVER_LEN];
170 	u8 ver, patch;
171 	u32 full_ver;
172 	u16 build;
173 
174 	full_ver = hw->nvm.oem_ver;
175 	ver = (u8)((full_ver & ICE_OEM_VER_MASK) >> ICE_OEM_VER_SHIFT);
176 	build = (u16)((full_ver & ICE_OEM_VER_BUILD_MASK) >>
177 		      ICE_OEM_VER_BUILD_SHIFT);
178 	patch = (u8)(full_ver & ICE_OEM_VER_PATCH_MASK);
179 
180 	snprintf(buf, sizeof(buf), "%x.%02x 0x%x %d.%d.%d",
181 		 (hw->nvm.ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT,
182 		 (hw->nvm.ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT,
183 		 hw->nvm.eetrack, ver, build, patch);
184 
185 	return buf;
186 }
187 
188 static void
189 ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
190 {
191 	struct ice_netdev_priv *np = netdev_priv(netdev);
192 	struct ice_vsi *vsi = np->vsi;
193 	struct ice_pf *pf = vsi->back;
194 
195 	strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
196 	strlcpy(drvinfo->version, ice_drv_ver, sizeof(drvinfo->version));
197 	strlcpy(drvinfo->fw_version, ice_nvm_version_str(&pf->hw),
198 		sizeof(drvinfo->fw_version));
199 	strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
200 		sizeof(drvinfo->bus_info));
201 	drvinfo->n_priv_flags = ICE_PRIV_FLAG_ARRAY_SIZE;
202 }
203 
204 static int ice_get_regs_len(struct net_device __always_unused *netdev)
205 {
206 	return sizeof(ice_regs_dump_list);
207 }
208 
209 static void
210 ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
211 {
212 	struct ice_netdev_priv *np = netdev_priv(netdev);
213 	struct ice_pf *pf = np->vsi->back;
214 	struct ice_hw *hw = &pf->hw;
215 	u32 *regs_buf = (u32 *)p;
216 	int i;
217 
218 	regs->version = 1;
219 
220 	for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i)
221 		regs_buf[i] = rd32(hw, ice_regs_dump_list[i]);
222 }
223 
224 static u32 ice_get_msglevel(struct net_device *netdev)
225 {
226 	struct ice_netdev_priv *np = netdev_priv(netdev);
227 	struct ice_pf *pf = np->vsi->back;
228 
229 #ifndef CONFIG_DYNAMIC_DEBUG
230 	if (pf->hw.debug_mask)
231 		netdev_info(netdev, "hw debug_mask: 0x%llX\n",
232 			    pf->hw.debug_mask);
233 #endif /* !CONFIG_DYNAMIC_DEBUG */
234 
235 	return pf->msg_enable;
236 }
237 
238 static void ice_set_msglevel(struct net_device *netdev, u32 data)
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 (ICE_DBG_USER & data)
245 		pf->hw.debug_mask = data;
246 	else
247 		pf->msg_enable = data;
248 #else
249 	pf->msg_enable = data;
250 #endif /* !CONFIG_DYNAMIC_DEBUG */
251 }
252 
253 static int ice_get_eeprom_len(struct net_device *netdev)
254 {
255 	struct ice_netdev_priv *np = netdev_priv(netdev);
256 	struct ice_pf *pf = np->vsi->back;
257 
258 	return (int)(pf->hw.nvm.sr_words * sizeof(u16));
259 }
260 
261 static int
262 ice_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
263 	       u8 *bytes)
264 {
265 	struct ice_netdev_priv *np = netdev_priv(netdev);
266 	u16 first_word, last_word, nwords;
267 	struct ice_vsi *vsi = np->vsi;
268 	struct ice_pf *pf = vsi->back;
269 	struct ice_hw *hw = &pf->hw;
270 	enum ice_status status;
271 	struct device *dev;
272 	int ret = 0;
273 	u16 *buf;
274 
275 	dev = &pf->pdev->dev;
276 
277 	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
278 
279 	first_word = eeprom->offset >> 1;
280 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
281 	nwords = last_word - first_word + 1;
282 
283 	buf = devm_kcalloc(dev, nwords, sizeof(u16), GFP_KERNEL);
284 	if (!buf)
285 		return -ENOMEM;
286 
287 	status = ice_read_sr_buf(hw, first_word, &nwords, buf);
288 	if (status) {
289 		dev_err(dev, "ice_read_sr_buf failed, err %d aq_err %d\n",
290 			status, hw->adminq.sq_last_status);
291 		eeprom->len = sizeof(u16) * nwords;
292 		ret = -EIO;
293 		goto out;
294 	}
295 
296 	memcpy(bytes, (u8 *)buf + (eeprom->offset & 1), eeprom->len);
297 out:
298 	devm_kfree(dev, buf);
299 	return ret;
300 }
301 
302 /**
303  * ice_active_vfs - check if there are any active VFs
304  * @pf: board private structure
305  *
306  * Returns true if an active VF is found, otherwise returns false
307  */
308 static bool ice_active_vfs(struct ice_pf *pf)
309 {
310 	struct ice_vf *vf = pf->vf;
311 	int i;
312 
313 	for (i = 0; i < pf->num_alloc_vfs; i++, vf++)
314 		if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
315 			return true;
316 	return false;
317 }
318 
319 /**
320  * ice_link_test - perform a link test on a given net_device
321  * @netdev: network interface device structure
322  *
323  * This function performs one of the self-tests required by ethtool.
324  * Returns 0 on success, non-zero on failure.
325  */
326 static u64 ice_link_test(struct net_device *netdev)
327 {
328 	struct ice_netdev_priv *np = netdev_priv(netdev);
329 	enum ice_status status;
330 	bool link_up = false;
331 
332 	netdev_info(netdev, "link test\n");
333 	status = ice_get_link_status(np->vsi->port_info, &link_up);
334 	if (status) {
335 		netdev_err(netdev, "link query error, status = %d\n", status);
336 		return 1;
337 	}
338 
339 	if (!link_up)
340 		return 2;
341 
342 	return 0;
343 }
344 
345 /**
346  * ice_eeprom_test - perform an EEPROM 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_eeprom_test(struct net_device *netdev)
353 {
354 	struct ice_netdev_priv *np = netdev_priv(netdev);
355 	struct ice_pf *pf = np->vsi->back;
356 
357 	netdev_info(netdev, "EEPROM test\n");
358 	return !!(ice_nvm_validate_checksum(&pf->hw));
359 }
360 
361 /**
362  * ice_reg_pattern_test
363  * @hw: pointer to the HW struct
364  * @reg: reg to be tested
365  * @mask: bits to be touched
366  */
367 static int ice_reg_pattern_test(struct ice_hw *hw, u32 reg, u32 mask)
368 {
369 	struct ice_pf *pf = (struct ice_pf *)hw->back;
370 	static const u32 patterns[] = {
371 		0x5A5A5A5A, 0xA5A5A5A5,
372 		0x00000000, 0xFFFFFFFF
373 	};
374 	u32 val, orig_val;
375 	int i;
376 
377 	orig_val = rd32(hw, reg);
378 	for (i = 0; i < ARRAY_SIZE(patterns); ++i) {
379 		u32 pattern = patterns[i] & mask;
380 
381 		wr32(hw, reg, pattern);
382 		val = rd32(hw, reg);
383 		if (val == pattern)
384 			continue;
385 		dev_err(&pf->pdev->dev,
386 			"%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n"
387 			, __func__, reg, pattern, val);
388 		return 1;
389 	}
390 
391 	wr32(hw, reg, orig_val);
392 	val = rd32(hw, reg);
393 	if (val != orig_val) {
394 		dev_err(&pf->pdev->dev,
395 			"%s: reg restore test failed - reg 0x%08x orig 0x%08x val 0x%08x\n"
396 			, __func__, reg, orig_val, val);
397 		return 1;
398 	}
399 
400 	return 0;
401 }
402 
403 /**
404  * ice_reg_test - perform a register test on a given net_device
405  * @netdev: network interface device structure
406  *
407  * This function performs one of the self-tests required by ethtool.
408  * Returns 0 on success, non-zero on failure.
409  */
410 static u64 ice_reg_test(struct net_device *netdev)
411 {
412 	struct ice_netdev_priv *np = netdev_priv(netdev);
413 	struct ice_hw *hw = np->vsi->port_info->hw;
414 	u32 int_elements = hw->func_caps.common_cap.num_msix_vectors ?
415 		hw->func_caps.common_cap.num_msix_vectors - 1 : 1;
416 	struct ice_diag_reg_test_info {
417 		u32 address;
418 		u32 mask;
419 		u32 elem_num;
420 		u32 elem_size;
421 	} ice_reg_list[] = {
422 		{GLINT_ITR(0, 0), 0x00000fff, int_elements,
423 			GLINT_ITR(0, 1) - GLINT_ITR(0, 0)},
424 		{GLINT_ITR(1, 0), 0x00000fff, int_elements,
425 			GLINT_ITR(1, 1) - GLINT_ITR(1, 0)},
426 		{GLINT_ITR(0, 0), 0x00000fff, int_elements,
427 			GLINT_ITR(2, 1) - GLINT_ITR(2, 0)},
428 		{GLINT_CTL, 0xffff0001, 1, 0}
429 	};
430 	int i;
431 
432 	netdev_dbg(netdev, "Register test\n");
433 	for (i = 0; i < ARRAY_SIZE(ice_reg_list); ++i) {
434 		u32 j;
435 
436 		for (j = 0; j < ice_reg_list[i].elem_num; ++j) {
437 			u32 mask = ice_reg_list[i].mask;
438 			u32 reg = ice_reg_list[i].address +
439 				(j * ice_reg_list[i].elem_size);
440 
441 			/* bail on failure (non-zero return) */
442 			if (ice_reg_pattern_test(hw, reg, mask))
443 				return 1;
444 		}
445 	}
446 
447 	return 0;
448 }
449 
450 /**
451  * ice_lbtest_prepare_rings - configure Tx/Rx test rings
452  * @vsi: pointer to the VSI structure
453  *
454  * Function configures rings of a VSI for loopback test without
455  * enabling interrupts or informing the kernel about new queues.
456  *
457  * Returns 0 on success, negative on failure.
458  */
459 static int ice_lbtest_prepare_rings(struct ice_vsi *vsi)
460 {
461 	int status;
462 
463 	status = ice_vsi_setup_tx_rings(vsi);
464 	if (status)
465 		goto err_setup_tx_ring;
466 
467 	status = ice_vsi_setup_rx_rings(vsi);
468 	if (status)
469 		goto err_setup_rx_ring;
470 
471 	status = ice_vsi_cfg(vsi);
472 	if (status)
473 		goto err_setup_rx_ring;
474 
475 	status = ice_vsi_start_rx_rings(vsi);
476 	if (status)
477 		goto err_start_rx_ring;
478 
479 	return status;
480 
481 err_start_rx_ring:
482 	ice_vsi_free_rx_rings(vsi);
483 err_setup_rx_ring:
484 	ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
485 err_setup_tx_ring:
486 	ice_vsi_free_tx_rings(vsi);
487 
488 	return status;
489 }
490 
491 /**
492  * ice_lbtest_disable_rings - disable Tx/Rx test rings after loopback test
493  * @vsi: pointer to the VSI structure
494  *
495  * Function stops and frees VSI rings after a loopback test.
496  * Returns 0 on success, negative on failure.
497  */
498 static int ice_lbtest_disable_rings(struct ice_vsi *vsi)
499 {
500 	int status;
501 
502 	status = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
503 	if (status)
504 		netdev_err(vsi->netdev, "Failed to stop Tx rings, VSI %d error %d\n",
505 			   vsi->vsi_num, status);
506 
507 	status = ice_vsi_stop_rx_rings(vsi);
508 	if (status)
509 		netdev_err(vsi->netdev, "Failed to stop Rx rings, VSI %d error %d\n",
510 			   vsi->vsi_num, status);
511 
512 	ice_vsi_free_tx_rings(vsi);
513 	ice_vsi_free_rx_rings(vsi);
514 
515 	return status;
516 }
517 
518 /**
519  * ice_lbtest_create_frame - create test packet
520  * @pf: pointer to the PF structure
521  * @ret_data: allocated frame buffer
522  * @size: size of the packet data
523  *
524  * Function allocates a frame with a test pattern on specific offsets.
525  * Returns 0 on success, non-zero on failure.
526  */
527 static int ice_lbtest_create_frame(struct ice_pf *pf, u8 **ret_data, u16 size)
528 {
529 	u8 *data;
530 
531 	if (!pf)
532 		return -EINVAL;
533 
534 	data = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
535 	if (!data)
536 		return -ENOMEM;
537 
538 	/* Since the ethernet test frame should always be at least
539 	 * 64 bytes long, fill some octets in the payload with test data.
540 	 */
541 	memset(data, 0xFF, size);
542 	data[32] = 0xDE;
543 	data[42] = 0xAD;
544 	data[44] = 0xBE;
545 	data[46] = 0xEF;
546 
547 	*ret_data = data;
548 
549 	return 0;
550 }
551 
552 /**
553  * ice_lbtest_check_frame - verify received loopback frame
554  * @frame: pointer to the raw packet data
555  *
556  * Function verifies received test frame with a pattern.
557  * Returns true if frame matches the pattern, false otherwise.
558  */
559 static bool ice_lbtest_check_frame(u8 *frame)
560 {
561 	/* Validate bytes of a frame under offsets chosen earlier */
562 	if (frame[32] == 0xDE &&
563 	    frame[42] == 0xAD &&
564 	    frame[44] == 0xBE &&
565 	    frame[46] == 0xEF &&
566 	    frame[48] == 0xFF)
567 		return true;
568 
569 	return false;
570 }
571 
572 /**
573  * ice_diag_send - send test frames to the test ring
574  * @tx_ring: pointer to the transmit ring
575  * @data: pointer to the raw packet data
576  * @size: size of the packet to send
577  *
578  * Function sends loopback packets on a test Tx ring.
579  */
580 static int ice_diag_send(struct ice_ring *tx_ring, u8 *data, u16 size)
581 {
582 	struct ice_tx_desc *tx_desc;
583 	struct ice_tx_buf *tx_buf;
584 	dma_addr_t dma;
585 	u64 td_cmd;
586 
587 	tx_desc = ICE_TX_DESC(tx_ring, tx_ring->next_to_use);
588 	tx_buf = &tx_ring->tx_buf[tx_ring->next_to_use];
589 
590 	dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
591 	if (dma_mapping_error(tx_ring->dev, dma))
592 		return -EINVAL;
593 
594 	tx_desc->buf_addr = cpu_to_le64(dma);
595 
596 	/* These flags are required for a descriptor to be pushed out */
597 	td_cmd = (u64)(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS);
598 	tx_desc->cmd_type_offset_bsz =
599 		cpu_to_le64(ICE_TX_DESC_DTYPE_DATA |
600 			    (td_cmd << ICE_TXD_QW1_CMD_S) |
601 			    ((u64)0 << ICE_TXD_QW1_OFFSET_S) |
602 			    ((u64)size << ICE_TXD_QW1_TX_BUF_SZ_S) |
603 			    ((u64)0 << ICE_TXD_QW1_L2TAG1_S));
604 
605 	tx_buf->next_to_watch = tx_desc;
606 
607 	/* Force memory write to complete before letting h/w know
608 	 * there are new descriptors to fetch.
609 	 */
610 	wmb();
611 
612 	tx_ring->next_to_use++;
613 	if (tx_ring->next_to_use >= tx_ring->count)
614 		tx_ring->next_to_use = 0;
615 
616 	writel_relaxed(tx_ring->next_to_use, tx_ring->tail);
617 
618 	/* Wait until the packets get transmitted to the receive queue. */
619 	usleep_range(1000, 2000);
620 	dma_unmap_single(tx_ring->dev, dma, size, DMA_TO_DEVICE);
621 
622 	return 0;
623 }
624 
625 #define ICE_LB_FRAME_SIZE 64
626 /**
627  * ice_lbtest_receive_frames - receive and verify test frames
628  * @rx_ring: pointer to the receive ring
629  *
630  * Function receives loopback packets and verify their correctness.
631  * Returns number of received valid frames.
632  */
633 static int ice_lbtest_receive_frames(struct ice_ring *rx_ring)
634 {
635 	struct ice_rx_buf *rx_buf;
636 	int valid_frames, i;
637 	u8 *received_buf;
638 
639 	valid_frames = 0;
640 
641 	for (i = 0; i < rx_ring->count; i++) {
642 		union ice_32b_rx_flex_desc *rx_desc;
643 
644 		rx_desc = ICE_RX_DESC(rx_ring, i);
645 
646 		if (!(rx_desc->wb.status_error0 &
647 		    cpu_to_le16(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS)))
648 			continue;
649 
650 		rx_buf = &rx_ring->rx_buf[i];
651 		received_buf = page_address(rx_buf->page);
652 
653 		if (ice_lbtest_check_frame(received_buf))
654 			valid_frames++;
655 	}
656 
657 	return valid_frames;
658 }
659 
660 /**
661  * ice_loopback_test - perform a loopback test on a given net_device
662  * @netdev: network interface device structure
663  *
664  * This function performs one of the self-tests required by ethtool.
665  * Returns 0 on success, non-zero on failure.
666  */
667 static u64 ice_loopback_test(struct net_device *netdev)
668 {
669 	struct ice_netdev_priv *np = netdev_priv(netdev);
670 	struct ice_vsi *orig_vsi = np->vsi, *test_vsi;
671 	struct ice_pf *pf = orig_vsi->back;
672 	struct ice_ring *tx_ring, *rx_ring;
673 	u8 broadcast[ETH_ALEN], ret = 0;
674 	int num_frames, valid_frames;
675 	LIST_HEAD(tmp_list);
676 	u8 *tx_frame;
677 	int i;
678 
679 	netdev_info(netdev, "loopback test\n");
680 
681 	test_vsi = ice_lb_vsi_setup(pf, pf->hw.port_info);
682 	if (!test_vsi) {
683 		netdev_err(netdev, "Failed to create a VSI for the loopback test");
684 		return 1;
685 	}
686 
687 	test_vsi->netdev = netdev;
688 	tx_ring = test_vsi->tx_rings[0];
689 	rx_ring = test_vsi->rx_rings[0];
690 
691 	if (ice_lbtest_prepare_rings(test_vsi)) {
692 		ret = 2;
693 		goto lbtest_vsi_close;
694 	}
695 
696 	if (ice_alloc_rx_bufs(rx_ring, rx_ring->count)) {
697 		ret = 3;
698 		goto lbtest_rings_dis;
699 	}
700 
701 	/* Enable MAC loopback in firmware */
702 	if (ice_aq_set_mac_loopback(&pf->hw, true, NULL)) {
703 		ret = 4;
704 		goto lbtest_mac_dis;
705 	}
706 
707 	/* Test VSI needs to receive broadcast packets */
708 	eth_broadcast_addr(broadcast);
709 	if (ice_add_mac_to_list(test_vsi, &tmp_list, broadcast)) {
710 		ret = 5;
711 		goto lbtest_mac_dis;
712 	}
713 
714 	if (ice_add_mac(&pf->hw, &tmp_list)) {
715 		ret = 6;
716 		goto free_mac_list;
717 	}
718 
719 	if (ice_lbtest_create_frame(pf, &tx_frame, ICE_LB_FRAME_SIZE)) {
720 		ret = 7;
721 		goto remove_mac_filters;
722 	}
723 
724 	num_frames = min_t(int, tx_ring->count, 32);
725 	for (i = 0; i < num_frames; i++) {
726 		if (ice_diag_send(tx_ring, tx_frame, ICE_LB_FRAME_SIZE)) {
727 			ret = 8;
728 			goto lbtest_free_frame;
729 		}
730 	}
731 
732 	valid_frames = ice_lbtest_receive_frames(rx_ring);
733 	if (!valid_frames)
734 		ret = 9;
735 	else if (valid_frames != num_frames)
736 		ret = 10;
737 
738 lbtest_free_frame:
739 	devm_kfree(&pf->pdev->dev, tx_frame);
740 remove_mac_filters:
741 	if (ice_remove_mac(&pf->hw, &tmp_list))
742 		netdev_err(netdev, "Could not remove MAC filter for the test VSI");
743 free_mac_list:
744 	ice_free_fltr_list(&pf->pdev->dev, &tmp_list);
745 lbtest_mac_dis:
746 	/* Disable MAC loopback after the test is completed. */
747 	if (ice_aq_set_mac_loopback(&pf->hw, false, NULL))
748 		netdev_err(netdev, "Could not disable MAC loopback\n");
749 lbtest_rings_dis:
750 	if (ice_lbtest_disable_rings(test_vsi))
751 		netdev_err(netdev, "Could not disable test rings\n");
752 lbtest_vsi_close:
753 	test_vsi->netdev = NULL;
754 	if (ice_vsi_release(test_vsi))
755 		netdev_err(netdev, "Failed to remove the test VSI");
756 
757 	return ret;
758 }
759 
760 /**
761  * ice_intr_test - perform an interrupt test on a given net_device
762  * @netdev: network interface device structure
763  *
764  * This function performs one of the self-tests required by ethtool.
765  * Returns 0 on success, non-zero on failure.
766  */
767 static u64 ice_intr_test(struct net_device *netdev)
768 {
769 	struct ice_netdev_priv *np = netdev_priv(netdev);
770 	struct ice_pf *pf = np->vsi->back;
771 	u16 swic_old = pf->sw_int_count;
772 
773 	netdev_info(netdev, "interrupt test\n");
774 
775 	wr32(&pf->hw, GLINT_DYN_CTL(pf->oicr_idx),
776 	     GLINT_DYN_CTL_SW_ITR_INDX_M |
777 	     GLINT_DYN_CTL_INTENA_MSK_M |
778 	     GLINT_DYN_CTL_SWINT_TRIG_M);
779 
780 	usleep_range(1000, 2000);
781 	return (swic_old == pf->sw_int_count);
782 }
783 
784 /**
785  * ice_self_test - handler function for performing a self-test by ethtool
786  * @netdev: network interface device structure
787  * @eth_test: ethtool_test structure
788  * @data: required by ethtool.self_test
789  *
790  * This function is called after invoking 'ethtool -t devname' command where
791  * devname is the name of the network device on which ethtool should operate.
792  * It performs a set of self-tests to check if a device works properly.
793  */
794 static void
795 ice_self_test(struct net_device *netdev, struct ethtool_test *eth_test,
796 	      u64 *data)
797 {
798 	struct ice_netdev_priv *np = netdev_priv(netdev);
799 	bool if_running = netif_running(netdev);
800 	struct ice_pf *pf = np->vsi->back;
801 
802 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
803 		netdev_info(netdev, "offline testing starting\n");
804 
805 		set_bit(__ICE_TESTING, pf->state);
806 
807 		if (ice_active_vfs(pf)) {
808 			dev_warn(&pf->pdev->dev,
809 				 "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
810 			data[ICE_ETH_TEST_REG] = 1;
811 			data[ICE_ETH_TEST_EEPROM] = 1;
812 			data[ICE_ETH_TEST_INTR] = 1;
813 			data[ICE_ETH_TEST_LOOP] = 1;
814 			data[ICE_ETH_TEST_LINK] = 1;
815 			eth_test->flags |= ETH_TEST_FL_FAILED;
816 			clear_bit(__ICE_TESTING, pf->state);
817 			goto skip_ol_tests;
818 		}
819 		/* If the device is online then take it offline */
820 		if (if_running)
821 			/* indicate we're in test mode */
822 			ice_stop(netdev);
823 
824 		data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
825 		data[ICE_ETH_TEST_EEPROM] = ice_eeprom_test(netdev);
826 		data[ICE_ETH_TEST_INTR] = ice_intr_test(netdev);
827 		data[ICE_ETH_TEST_LOOP] = ice_loopback_test(netdev);
828 		data[ICE_ETH_TEST_REG] = ice_reg_test(netdev);
829 
830 		if (data[ICE_ETH_TEST_LINK] ||
831 		    data[ICE_ETH_TEST_EEPROM] ||
832 		    data[ICE_ETH_TEST_LOOP] ||
833 		    data[ICE_ETH_TEST_INTR] ||
834 		    data[ICE_ETH_TEST_REG])
835 			eth_test->flags |= ETH_TEST_FL_FAILED;
836 
837 		clear_bit(__ICE_TESTING, pf->state);
838 
839 		if (if_running) {
840 			int status = ice_open(netdev);
841 
842 			if (status) {
843 				dev_err(&pf->pdev->dev,
844 					"Could not open device %s, err %d",
845 					pf->int_name, status);
846 			}
847 		}
848 	} else {
849 		/* Online tests */
850 		netdev_info(netdev, "online testing starting\n");
851 
852 		data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
853 		if (data[ICE_ETH_TEST_LINK])
854 			eth_test->flags |= ETH_TEST_FL_FAILED;
855 
856 		/* Offline only tests, not run in online; pass by default */
857 		data[ICE_ETH_TEST_REG] = 0;
858 		data[ICE_ETH_TEST_EEPROM] = 0;
859 		data[ICE_ETH_TEST_INTR] = 0;
860 		data[ICE_ETH_TEST_LOOP] = 0;
861 	}
862 
863 skip_ol_tests:
864 	netdev_info(netdev, "testing finished\n");
865 }
866 
867 static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
868 {
869 	struct ice_netdev_priv *np = netdev_priv(netdev);
870 	struct ice_vsi *vsi = np->vsi;
871 	char *p = (char *)data;
872 	unsigned int i;
873 
874 	switch (stringset) {
875 	case ETH_SS_STATS:
876 		for (i = 0; i < ICE_VSI_STATS_LEN; i++) {
877 			snprintf(p, ETH_GSTRING_LEN, "%s",
878 				 ice_gstrings_vsi_stats[i].stat_string);
879 			p += ETH_GSTRING_LEN;
880 		}
881 
882 		ice_for_each_alloc_txq(vsi, i) {
883 			snprintf(p, ETH_GSTRING_LEN,
884 				 "tx_queue_%u_packets", i);
885 			p += ETH_GSTRING_LEN;
886 			snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_bytes", i);
887 			p += ETH_GSTRING_LEN;
888 		}
889 
890 		ice_for_each_alloc_rxq(vsi, i) {
891 			snprintf(p, ETH_GSTRING_LEN,
892 				 "rx_queue_%u_packets", i);
893 			p += ETH_GSTRING_LEN;
894 			snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_bytes", i);
895 			p += ETH_GSTRING_LEN;
896 		}
897 
898 		if (vsi->type != ICE_VSI_PF)
899 			return;
900 
901 		for (i = 0; i < ICE_PF_STATS_LEN; i++) {
902 			snprintf(p, ETH_GSTRING_LEN, "%s",
903 				 ice_gstrings_pf_stats[i].stat_string);
904 			p += ETH_GSTRING_LEN;
905 		}
906 
907 		for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
908 			snprintf(p, ETH_GSTRING_LEN,
909 				 "tx_priority_%u_xon.nic", i);
910 			p += ETH_GSTRING_LEN;
911 			snprintf(p, ETH_GSTRING_LEN,
912 				 "tx_priority_%u_xoff.nic", i);
913 			p += ETH_GSTRING_LEN;
914 		}
915 		for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
916 			snprintf(p, ETH_GSTRING_LEN,
917 				 "rx_priority_%u_xon.nic", i);
918 			p += ETH_GSTRING_LEN;
919 			snprintf(p, ETH_GSTRING_LEN,
920 				 "rx_priority_%u_xoff.nic", i);
921 			p += ETH_GSTRING_LEN;
922 		}
923 		break;
924 	case ETH_SS_TEST:
925 		memcpy(data, ice_gstrings_test, ICE_TEST_LEN * ETH_GSTRING_LEN);
926 		break;
927 	case ETH_SS_PRIV_FLAGS:
928 		for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
929 			snprintf(p, ETH_GSTRING_LEN, "%s",
930 				 ice_gstrings_priv_flags[i].name);
931 			p += ETH_GSTRING_LEN;
932 		}
933 		break;
934 	default:
935 		break;
936 	}
937 }
938 
939 static int
940 ice_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
941 {
942 	struct ice_netdev_priv *np = netdev_priv(netdev);
943 	bool led_active;
944 
945 	switch (state) {
946 	case ETHTOOL_ID_ACTIVE:
947 		led_active = true;
948 		break;
949 	case ETHTOOL_ID_INACTIVE:
950 		led_active = false;
951 		break;
952 	default:
953 		return -EINVAL;
954 	}
955 
956 	if (ice_aq_set_port_id_led(np->vsi->port_info, !led_active, NULL))
957 		return -EIO;
958 
959 	return 0;
960 }
961 
962 /**
963  * ice_set_fec_cfg - Set link FEC options
964  * @netdev: network interface device structure
965  * @req_fec: FEC mode to configure
966  */
967 static int ice_set_fec_cfg(struct net_device *netdev, enum ice_fec_mode req_fec)
968 {
969 	struct ice_netdev_priv *np = netdev_priv(netdev);
970 	struct ice_aqc_set_phy_cfg_data config = { 0 };
971 	struct ice_aqc_get_phy_caps_data *caps;
972 	struct ice_vsi *vsi = np->vsi;
973 	u8 sw_cfg_caps, sw_cfg_fec;
974 	struct ice_port_info *pi;
975 	enum ice_status status;
976 	int err = 0;
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 	/* Get last SW configuration */
989 	caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
990 	if (!caps)
991 		return -ENOMEM;
992 
993 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG,
994 				     caps, NULL);
995 	if (status) {
996 		err = -EAGAIN;
997 		goto done;
998 	}
999 
1000 	/* Copy SW configuration returned from PHY caps to PHY config */
1001 	ice_copy_phy_caps_to_cfg(caps, &config);
1002 	sw_cfg_caps = caps->caps;
1003 	sw_cfg_fec = caps->link_fec_options;
1004 
1005 	/* Get toloplogy caps, then copy PHY FEC topoloy caps to PHY config */
1006 	memset(caps, 0, sizeof(*caps));
1007 
1008 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP,
1009 				     caps, NULL);
1010 	if (status) {
1011 		err = -EAGAIN;
1012 		goto done;
1013 	}
1014 
1015 	config.caps |= (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC);
1016 	config.link_fec_opt = caps->link_fec_options;
1017 
1018 	ice_cfg_phy_fec(&config, req_fec);
1019 
1020 	/* If FEC mode has changed, then set PHY configuration and enable AN. */
1021 	if ((config.caps & ICE_AQ_PHY_ENA_AUTO_FEC) !=
1022 	    (sw_cfg_caps & ICE_AQC_PHY_EN_AUTO_FEC) ||
1023 	    config.link_fec_opt != sw_cfg_fec) {
1024 		if (caps->caps & ICE_AQC_PHY_AN_MODE)
1025 			config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1026 
1027 		status = ice_aq_set_phy_cfg(pi->hw, pi->lport, &config, NULL);
1028 
1029 		if (status)
1030 			err = -EAGAIN;
1031 	}
1032 
1033 done:
1034 	devm_kfree(&vsi->back->pdev->dev, caps);
1035 	return err;
1036 }
1037 
1038 /**
1039  * ice_set_fecparam - Set FEC link options
1040  * @netdev: network interface device structure
1041  * @fecparam: Ethtool structure to retrieve FEC parameters
1042  */
1043 static int
1044 ice_set_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1045 {
1046 	struct ice_netdev_priv *np = netdev_priv(netdev);
1047 	struct ice_vsi *vsi = np->vsi;
1048 	enum ice_fec_mode fec;
1049 
1050 	switch (fecparam->fec) {
1051 	case ETHTOOL_FEC_AUTO:
1052 		fec = ICE_FEC_AUTO;
1053 		break;
1054 	case ETHTOOL_FEC_RS:
1055 		fec = ICE_FEC_RS;
1056 		break;
1057 	case ETHTOOL_FEC_BASER:
1058 		fec = ICE_FEC_BASER;
1059 		break;
1060 	case ETHTOOL_FEC_OFF:
1061 	case ETHTOOL_FEC_NONE:
1062 		fec = ICE_FEC_NONE;
1063 		break;
1064 	default:
1065 		dev_warn(&vsi->back->pdev->dev, "Unsupported FEC mode: %d\n",
1066 			 fecparam->fec);
1067 		return -EINVAL;
1068 	}
1069 
1070 	return ice_set_fec_cfg(netdev, fec);
1071 }
1072 
1073 /**
1074  * ice_get_fecparam - Get link FEC options
1075  * @netdev: network interface device structure
1076  * @fecparam: Ethtool structure to retrieve FEC parameters
1077  */
1078 static int
1079 ice_get_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1080 {
1081 	struct ice_netdev_priv *np = netdev_priv(netdev);
1082 	struct ice_aqc_get_phy_caps_data *caps;
1083 	struct ice_link_status *link_info;
1084 	struct ice_vsi *vsi = np->vsi;
1085 	struct ice_port_info *pi;
1086 	enum ice_status status;
1087 	int err = 0;
1088 
1089 	pi = vsi->port_info;
1090 
1091 	if (!pi)
1092 		return -EOPNOTSUPP;
1093 	link_info = &pi->phy.link_info;
1094 
1095 	/* Set FEC mode based on negotiated link info */
1096 	switch (link_info->fec_info) {
1097 	case ICE_AQ_LINK_25G_KR_FEC_EN:
1098 		fecparam->active_fec = ETHTOOL_FEC_BASER;
1099 		break;
1100 	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
1101 		/* fall through */
1102 	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
1103 		fecparam->active_fec = ETHTOOL_FEC_RS;
1104 		break;
1105 	default:
1106 		fecparam->active_fec = ETHTOOL_FEC_OFF;
1107 		break;
1108 	}
1109 
1110 	caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
1111 	if (!caps)
1112 		return -ENOMEM;
1113 
1114 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP,
1115 				     caps, NULL);
1116 	if (status) {
1117 		err = -EAGAIN;
1118 		goto done;
1119 	}
1120 
1121 	/* Set supported/configured FEC modes based on PHY capability */
1122 	if (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC)
1123 		fecparam->fec |= ETHTOOL_FEC_AUTO;
1124 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
1125 	    caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
1126 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN ||
1127 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
1128 		fecparam->fec |= ETHTOOL_FEC_BASER;
1129 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
1130 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ ||
1131 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
1132 		fecparam->fec |= ETHTOOL_FEC_RS;
1133 	if (caps->link_fec_options == 0)
1134 		fecparam->fec |= ETHTOOL_FEC_OFF;
1135 
1136 done:
1137 	devm_kfree(&vsi->back->pdev->dev, caps);
1138 	return err;
1139 }
1140 
1141 /**
1142  * ice_get_priv_flags - report device private flags
1143  * @netdev: network interface device structure
1144  *
1145  * The get string set count and the string set should be matched for each
1146  * flag returned.  Add new strings for each flag to the ice_gstrings_priv_flags
1147  * array.
1148  *
1149  * Returns a u32 bitmap of flags.
1150  */
1151 static u32 ice_get_priv_flags(struct net_device *netdev)
1152 {
1153 	struct ice_netdev_priv *np = netdev_priv(netdev);
1154 	struct ice_vsi *vsi = np->vsi;
1155 	struct ice_pf *pf = vsi->back;
1156 	u32 i, ret_flags = 0;
1157 
1158 	for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1159 		const struct ice_priv_flag *priv_flag;
1160 
1161 		priv_flag = &ice_gstrings_priv_flags[i];
1162 
1163 		if (test_bit(priv_flag->bitno, pf->flags))
1164 			ret_flags |= BIT(i);
1165 	}
1166 
1167 	return ret_flags;
1168 }
1169 
1170 /**
1171  * ice_set_priv_flags - set private flags
1172  * @netdev: network interface device structure
1173  * @flags: bit flags to be set
1174  */
1175 static int ice_set_priv_flags(struct net_device *netdev, u32 flags)
1176 {
1177 	struct ice_netdev_priv *np = netdev_priv(netdev);
1178 	DECLARE_BITMAP(change_flags, ICE_PF_FLAGS_NBITS);
1179 	DECLARE_BITMAP(orig_flags, ICE_PF_FLAGS_NBITS);
1180 	struct ice_vsi *vsi = np->vsi;
1181 	struct ice_pf *pf = vsi->back;
1182 	int ret = 0;
1183 	u32 i;
1184 
1185 	if (flags > BIT(ICE_PRIV_FLAG_ARRAY_SIZE))
1186 		return -EINVAL;
1187 
1188 	set_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1189 
1190 	bitmap_copy(orig_flags, pf->flags, ICE_PF_FLAGS_NBITS);
1191 	for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1192 		const struct ice_priv_flag *priv_flag;
1193 
1194 		priv_flag = &ice_gstrings_priv_flags[i];
1195 
1196 		if (flags & BIT(i))
1197 			set_bit(priv_flag->bitno, pf->flags);
1198 		else
1199 			clear_bit(priv_flag->bitno, pf->flags);
1200 	}
1201 
1202 	bitmap_xor(change_flags, pf->flags, orig_flags, ICE_PF_FLAGS_NBITS);
1203 
1204 	if (test_bit(ICE_FLAG_ENABLE_FW_LLDP, change_flags)) {
1205 		if (!test_bit(ICE_FLAG_ENABLE_FW_LLDP, pf->flags)) {
1206 			enum ice_status status;
1207 
1208 			/* Disable FW LLDP engine */
1209 			status = ice_aq_cfg_lldp_mib_change(&pf->hw, false,
1210 							    NULL);
1211 			/* If unregistering for LLDP events fails, this is
1212 			 * not an error state, as there shouldn't be any
1213 			 * events to respond to.
1214 			 */
1215 			if (status)
1216 				dev_info(&pf->pdev->dev,
1217 					 "Failed to unreg for LLDP events\n");
1218 
1219 			/* The AQ call to stop the FW LLDP agent will generate
1220 			 * an error if the agent is already stopped.
1221 			 */
1222 			status = ice_aq_stop_lldp(&pf->hw, true, true, NULL);
1223 			if (status)
1224 				dev_warn(&pf->pdev->dev,
1225 					 "Fail to stop LLDP agent\n");
1226 			/* Use case for having the FW LLDP agent stopped
1227 			 * will likely not need DCB, so failure to init is
1228 			 * not a concern of ethtool
1229 			 */
1230 			status = ice_init_pf_dcb(pf, true);
1231 			if (status)
1232 				dev_warn(&pf->pdev->dev, "Fail to init DCB\n");
1233 
1234 			/* Forward LLDP packets to default VSI so that they
1235 			 * are passed up the stack
1236 			 */
1237 			ice_cfg_sw_lldp(vsi, false, true);
1238 		} else {
1239 			enum ice_status status;
1240 			bool dcbx_agent_status;
1241 
1242 			/* AQ command to start FW LLDP agent will return an
1243 			 * error if the agent is already started
1244 			 */
1245 			status = ice_aq_start_lldp(&pf->hw, true, NULL);
1246 			if (status)
1247 				dev_warn(&pf->pdev->dev,
1248 					 "Fail to start LLDP Agent\n");
1249 
1250 			/* AQ command to start FW DCBX agent will fail if
1251 			 * the agent is already started
1252 			 */
1253 			status = ice_aq_start_stop_dcbx(&pf->hw, true,
1254 							&dcbx_agent_status,
1255 							NULL);
1256 			if (status)
1257 				dev_dbg(&pf->pdev->dev,
1258 					"Failed to start FW DCBX\n");
1259 
1260 			dev_info(&pf->pdev->dev, "FW DCBX agent is %s\n",
1261 				 dcbx_agent_status ? "ACTIVE" : "DISABLED");
1262 
1263 			/* Failure to configure MIB change or init DCB is not
1264 			 * relevant to ethtool.  Print notification that
1265 			 * registration/init failed but do not return error
1266 			 * state to ethtool
1267 			 */
1268 			status = ice_init_pf_dcb(pf, true);
1269 			if (status)
1270 				dev_dbg(&pf->pdev->dev, "Fail to init DCB\n");
1271 
1272 			/* Remove rule to direct LLDP packets to default VSI.
1273 			 * The FW LLDP engine will now be consuming them.
1274 			 */
1275 			ice_cfg_sw_lldp(vsi, false, false);
1276 		}
1277 	}
1278 	clear_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1279 	return ret;
1280 }
1281 
1282 static int ice_get_sset_count(struct net_device *netdev, int sset)
1283 {
1284 	switch (sset) {
1285 	case ETH_SS_STATS:
1286 		/* The number (and order) of strings reported *must* remain
1287 		 * constant for a given netdevice. This function must not
1288 		 * report a different number based on run time parameters
1289 		 * (such as the number of queues in use, or the setting of
1290 		 * a private ethtool flag). This is due to the nature of the
1291 		 * ethtool stats API.
1292 		 *
1293 		 * Userspace programs such as ethtool must make 3 separate
1294 		 * ioctl requests, one for size, one for the strings, and
1295 		 * finally one for the stats. Since these cross into
1296 		 * userspace, changes to the number or size could result in
1297 		 * undefined memory access or incorrect string<->value
1298 		 * correlations for statistics.
1299 		 *
1300 		 * Even if it appears to be safe, changes to the size or
1301 		 * order of strings will suffer from race conditions and are
1302 		 * not safe.
1303 		 */
1304 		return ICE_ALL_STATS_LEN(netdev);
1305 	case ETH_SS_TEST:
1306 		return ICE_TEST_LEN;
1307 	case ETH_SS_PRIV_FLAGS:
1308 		return ICE_PRIV_FLAG_ARRAY_SIZE;
1309 	default:
1310 		return -EOPNOTSUPP;
1311 	}
1312 }
1313 
1314 static void
1315 ice_get_ethtool_stats(struct net_device *netdev,
1316 		      struct ethtool_stats __always_unused *stats, u64 *data)
1317 {
1318 	struct ice_netdev_priv *np = netdev_priv(netdev);
1319 	struct ice_vsi *vsi = np->vsi;
1320 	struct ice_pf *pf = vsi->back;
1321 	struct ice_ring *ring;
1322 	unsigned int j = 0;
1323 	int i = 0;
1324 	char *p;
1325 
1326 	for (j = 0; j < ICE_VSI_STATS_LEN; j++) {
1327 		p = (char *)vsi + ice_gstrings_vsi_stats[j].stat_offset;
1328 		data[i++] = (ice_gstrings_vsi_stats[j].sizeof_stat ==
1329 			    sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1330 	}
1331 
1332 	/* populate per queue stats */
1333 	rcu_read_lock();
1334 
1335 	ice_for_each_alloc_txq(vsi, j) {
1336 		ring = READ_ONCE(vsi->tx_rings[j]);
1337 		if (ring) {
1338 			data[i++] = ring->stats.pkts;
1339 			data[i++] = ring->stats.bytes;
1340 		} else {
1341 			data[i++] = 0;
1342 			data[i++] = 0;
1343 		}
1344 	}
1345 
1346 	ice_for_each_alloc_rxq(vsi, j) {
1347 		ring = READ_ONCE(vsi->rx_rings[j]);
1348 		if (ring) {
1349 			data[i++] = ring->stats.pkts;
1350 			data[i++] = ring->stats.bytes;
1351 		} else {
1352 			data[i++] = 0;
1353 			data[i++] = 0;
1354 		}
1355 	}
1356 
1357 	rcu_read_unlock();
1358 
1359 	if (vsi->type != ICE_VSI_PF)
1360 		return;
1361 
1362 	for (j = 0; j < ICE_PF_STATS_LEN; j++) {
1363 		p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset;
1364 		data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat ==
1365 			     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1366 	}
1367 
1368 	for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1369 		data[i++] = pf->stats.priority_xon_tx[j];
1370 		data[i++] = pf->stats.priority_xoff_tx[j];
1371 	}
1372 
1373 	for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1374 		data[i++] = pf->stats.priority_xon_rx[j];
1375 		data[i++] = pf->stats.priority_xoff_rx[j];
1376 	}
1377 }
1378 
1379 /**
1380  * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes
1381  * @netdev: network interface device structure
1382  * @ks: ethtool link ksettings struct to fill out
1383  */
1384 static void
1385 ice_phy_type_to_ethtool(struct net_device *netdev,
1386 			struct ethtool_link_ksettings *ks)
1387 {
1388 	struct ice_netdev_priv *np = netdev_priv(netdev);
1389 	struct ice_link_status *hw_link_info;
1390 	bool need_add_adv_mode = false;
1391 	struct ice_vsi *vsi = np->vsi;
1392 	u64 phy_types_high;
1393 	u64 phy_types_low;
1394 
1395 	hw_link_info = &vsi->port_info->phy.link_info;
1396 	phy_types_low = vsi->port_info->phy.phy_type_low;
1397 	phy_types_high = vsi->port_info->phy.phy_type_high;
1398 
1399 	ethtool_link_ksettings_zero_link_mode(ks, supported);
1400 	ethtool_link_ksettings_zero_link_mode(ks, advertising);
1401 
1402 	if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
1403 	    phy_types_low & ICE_PHY_TYPE_LOW_100M_SGMII) {
1404 		ethtool_link_ksettings_add_link_mode(ks, supported,
1405 						     100baseT_Full);
1406 		if (!hw_link_info->req_speeds ||
1407 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100MB)
1408 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1409 							     100baseT_Full);
1410 	}
1411 	if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
1412 	    phy_types_low & ICE_PHY_TYPE_LOW_1G_SGMII) {
1413 		ethtool_link_ksettings_add_link_mode(ks, supported,
1414 						     1000baseT_Full);
1415 		if (!hw_link_info->req_speeds ||
1416 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
1417 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1418 							     1000baseT_Full);
1419 	}
1420 	if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX) {
1421 		ethtool_link_ksettings_add_link_mode(ks, supported,
1422 						     1000baseKX_Full);
1423 		if (!hw_link_info->req_speeds ||
1424 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
1425 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1426 							     1000baseKX_Full);
1427 	}
1428 	if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_SX ||
1429 	    phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_LX) {
1430 		ethtool_link_ksettings_add_link_mode(ks, supported,
1431 						     1000baseX_Full);
1432 		if (!hw_link_info->req_speeds ||
1433 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
1434 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1435 							     1000baseX_Full);
1436 	}
1437 	if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T) {
1438 		ethtool_link_ksettings_add_link_mode(ks, supported,
1439 						     2500baseT_Full);
1440 		if (!hw_link_info->req_speeds ||
1441 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
1442 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1443 							     2500baseT_Full);
1444 	}
1445 	if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_X ||
1446 	    phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX) {
1447 		ethtool_link_ksettings_add_link_mode(ks, supported,
1448 						     2500baseX_Full);
1449 		if (!hw_link_info->req_speeds ||
1450 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
1451 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1452 							     2500baseX_Full);
1453 	}
1454 	if (phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
1455 	    phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR) {
1456 		ethtool_link_ksettings_add_link_mode(ks, supported,
1457 						     5000baseT_Full);
1458 		if (!hw_link_info->req_speeds ||
1459 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_5GB)
1460 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1461 							     5000baseT_Full);
1462 	}
1463 	if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
1464 	    phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_DA ||
1465 	    phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC ||
1466 	    phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_C2C) {
1467 		ethtool_link_ksettings_add_link_mode(ks, supported,
1468 						     10000baseT_Full);
1469 		if (!hw_link_info->req_speeds ||
1470 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1471 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1472 							     10000baseT_Full);
1473 	}
1474 	if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1) {
1475 		ethtool_link_ksettings_add_link_mode(ks, supported,
1476 						     10000baseKR_Full);
1477 		if (!hw_link_info->req_speeds ||
1478 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1479 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1480 							     10000baseKR_Full);
1481 	}
1482 	if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_SR) {
1483 		ethtool_link_ksettings_add_link_mode(ks, supported,
1484 						     10000baseSR_Full);
1485 		if (!hw_link_info->req_speeds ||
1486 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1487 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1488 							     10000baseSR_Full);
1489 	}
1490 	if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_LR) {
1491 		ethtool_link_ksettings_add_link_mode(ks, supported,
1492 						     10000baseLR_Full);
1493 		if (!hw_link_info->req_speeds ||
1494 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1495 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1496 							     10000baseLR_Full);
1497 	}
1498 	if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
1499 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
1500 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
1501 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
1502 	    phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC ||
1503 	    phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_C2C) {
1504 		ethtool_link_ksettings_add_link_mode(ks, supported,
1505 						     25000baseCR_Full);
1506 		if (!hw_link_info->req_speeds ||
1507 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
1508 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1509 							     25000baseCR_Full);
1510 	}
1511 	if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_SR ||
1512 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_LR) {
1513 		ethtool_link_ksettings_add_link_mode(ks, supported,
1514 						     25000baseSR_Full);
1515 		if (!hw_link_info->req_speeds ||
1516 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
1517 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1518 							     25000baseSR_Full);
1519 	}
1520 	if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
1521 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
1522 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1) {
1523 		ethtool_link_ksettings_add_link_mode(ks, supported,
1524 						     25000baseKR_Full);
1525 		if (!hw_link_info->req_speeds ||
1526 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
1527 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1528 							     25000baseKR_Full);
1529 	}
1530 	if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
1531 		ethtool_link_ksettings_add_link_mode(ks, supported,
1532 						     40000baseKR4_Full);
1533 		if (!hw_link_info->req_speeds ||
1534 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1535 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1536 							     40000baseKR4_Full);
1537 	}
1538 	if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
1539 	    phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC ||
1540 	    phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI) {
1541 		ethtool_link_ksettings_add_link_mode(ks, supported,
1542 						     40000baseCR4_Full);
1543 		if (!hw_link_info->req_speeds ||
1544 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1545 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1546 							     40000baseCR4_Full);
1547 	}
1548 	if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_SR4) {
1549 		ethtool_link_ksettings_add_link_mode(ks, supported,
1550 						     40000baseSR4_Full);
1551 		if (!hw_link_info->req_speeds ||
1552 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1553 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1554 							     40000baseSR4_Full);
1555 	}
1556 	if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_LR4) {
1557 		ethtool_link_ksettings_add_link_mode(ks, supported,
1558 						     40000baseLR4_Full);
1559 		if (!hw_link_info->req_speeds ||
1560 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1561 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1562 							     40000baseLR4_Full);
1563 	}
1564 	if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 ||
1565 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC ||
1566 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2 ||
1567 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC ||
1568 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2 ||
1569 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP ||
1570 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR ||
1571 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC ||
1572 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1) {
1573 		ethtool_link_ksettings_add_link_mode(ks, supported,
1574 						     50000baseCR2_Full);
1575 		if (!hw_link_info->req_speeds ||
1576 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
1577 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1578 							     50000baseCR2_Full);
1579 	}
1580 	if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 ||
1581 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) {
1582 		ethtool_link_ksettings_add_link_mode(ks, supported,
1583 						     50000baseKR2_Full);
1584 		if (!hw_link_info->req_speeds ||
1585 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
1586 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1587 							     50000baseKR2_Full);
1588 	}
1589 	if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR2 ||
1590 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR2 ||
1591 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_FR ||
1592 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR) {
1593 		ethtool_link_ksettings_add_link_mode(ks, supported,
1594 						     50000baseSR2_Full);
1595 		if (!hw_link_info->req_speeds ||
1596 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
1597 			ethtool_link_ksettings_add_link_mode(ks, advertising,
1598 							     50000baseSR2_Full);
1599 	}
1600 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 ||
1601 	    phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC ||
1602 	    phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4 ||
1603 	    phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC ||
1604 	    phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4 ||
1605 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 ||
1606 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2  ||
1607 	    phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC ||
1608 	    phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2 ||
1609 	    phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC ||
1610 	    phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2) {
1611 		ethtool_link_ksettings_add_link_mode(ks, supported,
1612 						     100000baseCR4_Full);
1613 		if (!hw_link_info->req_speeds ||
1614 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1615 			need_add_adv_mode = true;
1616 	}
1617 	if (need_add_adv_mode) {
1618 		need_add_adv_mode = false;
1619 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1620 						     100000baseCR4_Full);
1621 	}
1622 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR4 ||
1623 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR2) {
1624 		ethtool_link_ksettings_add_link_mode(ks, supported,
1625 						     100000baseSR4_Full);
1626 		if (!hw_link_info->req_speeds ||
1627 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1628 			need_add_adv_mode = true;
1629 	}
1630 	if (need_add_adv_mode) {
1631 		need_add_adv_mode = false;
1632 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1633 						     100000baseSR4_Full);
1634 	}
1635 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_LR4 ||
1636 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_DR) {
1637 		ethtool_link_ksettings_add_link_mode(ks, supported,
1638 						     100000baseLR4_ER4_Full);
1639 		if (!hw_link_info->req_speeds ||
1640 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1641 			need_add_adv_mode = true;
1642 	}
1643 	if (need_add_adv_mode) {
1644 		need_add_adv_mode = false;
1645 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1646 						     100000baseLR4_ER4_Full);
1647 	}
1648 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 ||
1649 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 ||
1650 	    phy_types_high & ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4) {
1651 		ethtool_link_ksettings_add_link_mode(ks, supported,
1652 						     100000baseKR4_Full);
1653 		if (!hw_link_info->req_speeds ||
1654 		    hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1655 			need_add_adv_mode = true;
1656 	}
1657 	if (need_add_adv_mode)
1658 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1659 						     100000baseKR4_Full);
1660 
1661 	/* Autoneg PHY types */
1662 	if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
1663 	    phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
1664 	    phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX ||
1665 	    phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T ||
1666 	    phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX ||
1667 	    phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
1668 	    phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR ||
1669 	    phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
1670 	    phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 ||
1671 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
1672 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
1673 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
1674 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
1675 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
1676 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
1677 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1 ||
1678 	    phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
1679 	    phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
1680 		ethtool_link_ksettings_add_link_mode(ks, supported,
1681 						     Autoneg);
1682 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1683 						     Autoneg);
1684 	}
1685 	if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 ||
1686 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 ||
1687 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP ||
1688 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) {
1689 		ethtool_link_ksettings_add_link_mode(ks, supported,
1690 						     Autoneg);
1691 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1692 						     Autoneg);
1693 	}
1694 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 ||
1695 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 ||
1696 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 ||
1697 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2) {
1698 		ethtool_link_ksettings_add_link_mode(ks, supported,
1699 						     Autoneg);
1700 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1701 						     Autoneg);
1702 	}
1703 }
1704 
1705 #define TEST_SET_BITS_TIMEOUT	50
1706 #define TEST_SET_BITS_SLEEP_MAX	2000
1707 #define TEST_SET_BITS_SLEEP_MIN	1000
1708 
1709 /**
1710  * ice_get_settings_link_up - Get Link settings for when link is up
1711  * @ks: ethtool ksettings to fill in
1712  * @netdev: network interface device structure
1713  */
1714 static void
1715 ice_get_settings_link_up(struct ethtool_link_ksettings *ks,
1716 			 struct net_device *netdev)
1717 {
1718 	struct ice_netdev_priv *np = netdev_priv(netdev);
1719 	struct ethtool_link_ksettings cap_ksettings;
1720 	struct ice_link_status *link_info;
1721 	struct ice_vsi *vsi = np->vsi;
1722 	bool unrecog_phy_high = false;
1723 	bool unrecog_phy_low = false;
1724 
1725 	link_info = &vsi->port_info->phy.link_info;
1726 
1727 	/* Initialize supported and advertised settings based on PHY settings */
1728 	switch (link_info->phy_type_low) {
1729 	case ICE_PHY_TYPE_LOW_100BASE_TX:
1730 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1731 		ethtool_link_ksettings_add_link_mode(ks, supported,
1732 						     100baseT_Full);
1733 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1734 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1735 						     100baseT_Full);
1736 		break;
1737 	case ICE_PHY_TYPE_LOW_100M_SGMII:
1738 		ethtool_link_ksettings_add_link_mode(ks, supported,
1739 						     100baseT_Full);
1740 		break;
1741 	case ICE_PHY_TYPE_LOW_1000BASE_T:
1742 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1743 		ethtool_link_ksettings_add_link_mode(ks, supported,
1744 						     1000baseT_Full);
1745 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1746 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1747 						     1000baseT_Full);
1748 		break;
1749 	case ICE_PHY_TYPE_LOW_1G_SGMII:
1750 		ethtool_link_ksettings_add_link_mode(ks, supported,
1751 						     1000baseT_Full);
1752 		break;
1753 	case ICE_PHY_TYPE_LOW_1000BASE_SX:
1754 	case ICE_PHY_TYPE_LOW_1000BASE_LX:
1755 		ethtool_link_ksettings_add_link_mode(ks, supported,
1756 						     1000baseX_Full);
1757 		break;
1758 	case ICE_PHY_TYPE_LOW_1000BASE_KX:
1759 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1760 		ethtool_link_ksettings_add_link_mode(ks, supported,
1761 						     1000baseKX_Full);
1762 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1763 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1764 						     1000baseKX_Full);
1765 		break;
1766 	case ICE_PHY_TYPE_LOW_2500BASE_T:
1767 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1768 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1769 		ethtool_link_ksettings_add_link_mode(ks, supported,
1770 						     2500baseT_Full);
1771 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1772 						     2500baseT_Full);
1773 		break;
1774 	case ICE_PHY_TYPE_LOW_2500BASE_X:
1775 		ethtool_link_ksettings_add_link_mode(ks, supported,
1776 						     2500baseX_Full);
1777 		break;
1778 	case ICE_PHY_TYPE_LOW_2500BASE_KX:
1779 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1780 		ethtool_link_ksettings_add_link_mode(ks, supported,
1781 						     2500baseX_Full);
1782 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1783 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1784 						     2500baseX_Full);
1785 		break;
1786 	case ICE_PHY_TYPE_LOW_5GBASE_T:
1787 	case ICE_PHY_TYPE_LOW_5GBASE_KR:
1788 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1789 		ethtool_link_ksettings_add_link_mode(ks, supported,
1790 						     5000baseT_Full);
1791 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1792 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1793 						     5000baseT_Full);
1794 		break;
1795 	case ICE_PHY_TYPE_LOW_10GBASE_T:
1796 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1797 		ethtool_link_ksettings_add_link_mode(ks, supported,
1798 						     10000baseT_Full);
1799 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1800 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1801 						     10000baseT_Full);
1802 		break;
1803 	case ICE_PHY_TYPE_LOW_10G_SFI_DA:
1804 	case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
1805 	case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
1806 		ethtool_link_ksettings_add_link_mode(ks, supported,
1807 						     10000baseT_Full);
1808 		break;
1809 	case ICE_PHY_TYPE_LOW_10GBASE_SR:
1810 		ethtool_link_ksettings_add_link_mode(ks, supported,
1811 						     10000baseSR_Full);
1812 		break;
1813 	case ICE_PHY_TYPE_LOW_10GBASE_LR:
1814 		ethtool_link_ksettings_add_link_mode(ks, supported,
1815 						     10000baseLR_Full);
1816 		break;
1817 	case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
1818 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1819 		ethtool_link_ksettings_add_link_mode(ks, supported,
1820 						     10000baseKR_Full);
1821 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1822 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1823 						     10000baseKR_Full);
1824 		break;
1825 	case ICE_PHY_TYPE_LOW_25GBASE_T:
1826 	case ICE_PHY_TYPE_LOW_25GBASE_CR:
1827 	case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
1828 	case ICE_PHY_TYPE_LOW_25GBASE_CR1:
1829 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1830 		ethtool_link_ksettings_add_link_mode(ks, supported,
1831 						     25000baseCR_Full);
1832 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1833 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1834 						     25000baseCR_Full);
1835 		break;
1836 	case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
1837 	case ICE_PHY_TYPE_LOW_25G_AUI_C2C:
1838 		ethtool_link_ksettings_add_link_mode(ks, supported,
1839 						     25000baseCR_Full);
1840 		break;
1841 	case ICE_PHY_TYPE_LOW_25GBASE_SR:
1842 	case ICE_PHY_TYPE_LOW_25GBASE_LR:
1843 		ethtool_link_ksettings_add_link_mode(ks, supported,
1844 						     25000baseSR_Full);
1845 		break;
1846 	case ICE_PHY_TYPE_LOW_25GBASE_KR:
1847 	case ICE_PHY_TYPE_LOW_25GBASE_KR1:
1848 	case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
1849 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1850 		ethtool_link_ksettings_add_link_mode(ks, supported,
1851 						     25000baseKR_Full);
1852 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1853 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1854 						     25000baseKR_Full);
1855 		break;
1856 	case ICE_PHY_TYPE_LOW_40GBASE_CR4:
1857 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1858 		ethtool_link_ksettings_add_link_mode(ks, supported,
1859 						     40000baseCR4_Full);
1860 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1861 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1862 						     40000baseCR4_Full);
1863 		break;
1864 	case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
1865 	case ICE_PHY_TYPE_LOW_40G_XLAUI:
1866 		ethtool_link_ksettings_add_link_mode(ks, supported,
1867 						     40000baseCR4_Full);
1868 		break;
1869 	case ICE_PHY_TYPE_LOW_40GBASE_SR4:
1870 		ethtool_link_ksettings_add_link_mode(ks, supported,
1871 						     40000baseSR4_Full);
1872 		break;
1873 	case ICE_PHY_TYPE_LOW_40GBASE_LR4:
1874 		ethtool_link_ksettings_add_link_mode(ks, supported,
1875 						     40000baseLR4_Full);
1876 		break;
1877 	case ICE_PHY_TYPE_LOW_40GBASE_KR4:
1878 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1879 		ethtool_link_ksettings_add_link_mode(ks, supported,
1880 						     40000baseKR4_Full);
1881 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1882 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1883 						     40000baseKR4_Full);
1884 		break;
1885 	case ICE_PHY_TYPE_LOW_50GBASE_CR2:
1886 	case ICE_PHY_TYPE_LOW_50GBASE_CP:
1887 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1888 		ethtool_link_ksettings_add_link_mode(ks, supported,
1889 						     50000baseCR2_Full);
1890 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1891 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1892 						     50000baseCR2_Full);
1893 		break;
1894 	case ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC:
1895 	case ICE_PHY_TYPE_LOW_50G_LAUI2:
1896 	case ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC:
1897 	case ICE_PHY_TYPE_LOW_50G_AUI2:
1898 	case ICE_PHY_TYPE_LOW_50GBASE_SR:
1899 	case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
1900 	case ICE_PHY_TYPE_LOW_50G_AUI1:
1901 		ethtool_link_ksettings_add_link_mode(ks, supported,
1902 						     50000baseCR2_Full);
1903 		break;
1904 	case ICE_PHY_TYPE_LOW_50GBASE_KR2:
1905 	case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
1906 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1907 		ethtool_link_ksettings_add_link_mode(ks, supported,
1908 						     50000baseKR2_Full);
1909 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1910 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1911 						     50000baseKR2_Full);
1912 		break;
1913 	case ICE_PHY_TYPE_LOW_50GBASE_SR2:
1914 	case ICE_PHY_TYPE_LOW_50GBASE_LR2:
1915 	case ICE_PHY_TYPE_LOW_50GBASE_FR:
1916 	case ICE_PHY_TYPE_LOW_50GBASE_LR:
1917 		ethtool_link_ksettings_add_link_mode(ks, supported,
1918 						     50000baseSR2_Full);
1919 		break;
1920 	case ICE_PHY_TYPE_LOW_100GBASE_CR4:
1921 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1922 		ethtool_link_ksettings_add_link_mode(ks, supported,
1923 						     100000baseCR4_Full);
1924 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1925 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1926 						     100000baseCR4_Full);
1927 		break;
1928 	case ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC:
1929 	case ICE_PHY_TYPE_LOW_100G_CAUI4:
1930 	case ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC:
1931 	case ICE_PHY_TYPE_LOW_100G_AUI4:
1932 	case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
1933 		ethtool_link_ksettings_add_link_mode(ks, supported,
1934 						     100000baseCR4_Full);
1935 		break;
1936 	case ICE_PHY_TYPE_LOW_100GBASE_CP2:
1937 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1938 		ethtool_link_ksettings_add_link_mode(ks, supported,
1939 						     100000baseCR4_Full);
1940 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1941 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1942 						     100000baseCR4_Full);
1943 		break;
1944 	case ICE_PHY_TYPE_LOW_100GBASE_SR4:
1945 	case ICE_PHY_TYPE_LOW_100GBASE_SR2:
1946 		ethtool_link_ksettings_add_link_mode(ks, supported,
1947 						     100000baseSR4_Full);
1948 		break;
1949 	case ICE_PHY_TYPE_LOW_100GBASE_LR4:
1950 	case ICE_PHY_TYPE_LOW_100GBASE_DR:
1951 		ethtool_link_ksettings_add_link_mode(ks, supported,
1952 						     100000baseLR4_ER4_Full);
1953 		break;
1954 	case ICE_PHY_TYPE_LOW_100GBASE_KR4:
1955 	case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
1956 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1957 		ethtool_link_ksettings_add_link_mode(ks, supported,
1958 						     100000baseKR4_Full);
1959 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1960 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1961 						     100000baseKR4_Full);
1962 		break;
1963 	default:
1964 		unrecog_phy_low = true;
1965 	}
1966 
1967 	switch (link_info->phy_type_high) {
1968 	case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
1969 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1970 		ethtool_link_ksettings_add_link_mode(ks, supported,
1971 						     100000baseKR4_Full);
1972 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1973 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1974 						     100000baseKR4_Full);
1975 		break;
1976 	case ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC:
1977 	case ICE_PHY_TYPE_HIGH_100G_CAUI2:
1978 	case ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC:
1979 	case ICE_PHY_TYPE_HIGH_100G_AUI2:
1980 		ethtool_link_ksettings_add_link_mode(ks, supported,
1981 						     100000baseCR4_Full);
1982 		break;
1983 	default:
1984 		unrecog_phy_high = true;
1985 	}
1986 
1987 	if (unrecog_phy_low && unrecog_phy_high) {
1988 		/* if we got here and link is up something bad is afoot */
1989 		netdev_info(netdev,
1990 			    "WARNING: Unrecognized PHY_Low (0x%llx).\n",
1991 			    (u64)link_info->phy_type_low);
1992 		netdev_info(netdev,
1993 			    "WARNING: Unrecognized PHY_High (0x%llx).\n",
1994 			    (u64)link_info->phy_type_high);
1995 	}
1996 
1997 	/* Now that we've worked out everything that could be supported by the
1998 	 * current PHY type, get what is supported by the NVM and intersect
1999 	 * them to get what is truly supported
2000 	 */
2001 	memset(&cap_ksettings, 0, sizeof(cap_ksettings));
2002 	ice_phy_type_to_ethtool(netdev, &cap_ksettings);
2003 	ethtool_intersect_link_masks(ks, &cap_ksettings);
2004 
2005 	switch (link_info->link_speed) {
2006 	case ICE_AQ_LINK_SPEED_100GB:
2007 		ks->base.speed = SPEED_100000;
2008 		break;
2009 	case ICE_AQ_LINK_SPEED_50GB:
2010 		ks->base.speed = SPEED_50000;
2011 		break;
2012 	case ICE_AQ_LINK_SPEED_40GB:
2013 		ks->base.speed = SPEED_40000;
2014 		break;
2015 	case ICE_AQ_LINK_SPEED_25GB:
2016 		ks->base.speed = SPEED_25000;
2017 		break;
2018 	case ICE_AQ_LINK_SPEED_20GB:
2019 		ks->base.speed = SPEED_20000;
2020 		break;
2021 	case ICE_AQ_LINK_SPEED_10GB:
2022 		ks->base.speed = SPEED_10000;
2023 		break;
2024 	case ICE_AQ_LINK_SPEED_5GB:
2025 		ks->base.speed = SPEED_5000;
2026 		break;
2027 	case ICE_AQ_LINK_SPEED_2500MB:
2028 		ks->base.speed = SPEED_2500;
2029 		break;
2030 	case ICE_AQ_LINK_SPEED_1000MB:
2031 		ks->base.speed = SPEED_1000;
2032 		break;
2033 	case ICE_AQ_LINK_SPEED_100MB:
2034 		ks->base.speed = SPEED_100;
2035 		break;
2036 	default:
2037 		netdev_info(netdev,
2038 			    "WARNING: Unrecognized link_speed (0x%x).\n",
2039 			    link_info->link_speed);
2040 		break;
2041 	}
2042 	ks->base.duplex = DUPLEX_FULL;
2043 }
2044 
2045 /**
2046  * ice_get_settings_link_down - Get the Link settings when link is down
2047  * @ks: ethtool ksettings to fill in
2048  * @netdev: network interface device structure
2049  *
2050  * Reports link settings that can be determined when link is down
2051  */
2052 static void
2053 ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
2054 			   struct net_device *netdev)
2055 {
2056 	/* link is down and the driver needs to fall back on
2057 	 * supported PHY types to figure out what info to display
2058 	 */
2059 	ice_phy_type_to_ethtool(netdev, ks);
2060 
2061 	/* With no link, speed and duplex are unknown */
2062 	ks->base.speed = SPEED_UNKNOWN;
2063 	ks->base.duplex = DUPLEX_UNKNOWN;
2064 }
2065 
2066 /**
2067  * ice_get_link_ksettings - Get Link Speed and Duplex settings
2068  * @netdev: network interface device structure
2069  * @ks: ethtool ksettings
2070  *
2071  * Reports speed/duplex settings based on media_type
2072  */
2073 static int
2074 ice_get_link_ksettings(struct net_device *netdev,
2075 		       struct ethtool_link_ksettings *ks)
2076 {
2077 	struct ice_netdev_priv *np = netdev_priv(netdev);
2078 	struct ice_aqc_get_phy_caps_data *caps;
2079 	struct ice_link_status *hw_link_info;
2080 	struct ice_vsi *vsi = np->vsi;
2081 
2082 	ethtool_link_ksettings_zero_link_mode(ks, supported);
2083 	ethtool_link_ksettings_zero_link_mode(ks, advertising);
2084 	hw_link_info = &vsi->port_info->phy.link_info;
2085 
2086 	/* set speed and duplex */
2087 	if (hw_link_info->link_info & ICE_AQ_LINK_UP)
2088 		ice_get_settings_link_up(ks, netdev);
2089 	else
2090 		ice_get_settings_link_down(ks, netdev);
2091 
2092 	/* set autoneg settings */
2093 	ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
2094 		AUTONEG_ENABLE : AUTONEG_DISABLE;
2095 
2096 	/* set media type settings */
2097 	switch (vsi->port_info->phy.media_type) {
2098 	case ICE_MEDIA_FIBER:
2099 		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
2100 		ks->base.port = PORT_FIBRE;
2101 		break;
2102 	case ICE_MEDIA_BASET:
2103 		ethtool_link_ksettings_add_link_mode(ks, supported, TP);
2104 		ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
2105 		ks->base.port = PORT_TP;
2106 		break;
2107 	case ICE_MEDIA_BACKPLANE:
2108 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
2109 		ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
2110 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
2111 		ethtool_link_ksettings_add_link_mode(ks, advertising,
2112 						     Backplane);
2113 		ks->base.port = PORT_NONE;
2114 		break;
2115 	case ICE_MEDIA_DA:
2116 		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
2117 		ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
2118 		ks->base.port = PORT_DA;
2119 		break;
2120 	default:
2121 		ks->base.port = PORT_OTHER;
2122 		break;
2123 	}
2124 
2125 	/* flow control is symmetric and always supported */
2126 	ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
2127 
2128 	switch (vsi->port_info->fc.req_mode) {
2129 	case ICE_FC_FULL:
2130 		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2131 		break;
2132 	case ICE_FC_TX_PAUSE:
2133 		ethtool_link_ksettings_add_link_mode(ks, advertising,
2134 						     Asym_Pause);
2135 		break;
2136 	case ICE_FC_RX_PAUSE:
2137 		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2138 		ethtool_link_ksettings_add_link_mode(ks, advertising,
2139 						     Asym_Pause);
2140 		break;
2141 	case ICE_FC_PFC:
2142 	default:
2143 		ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
2144 		ethtool_link_ksettings_del_link_mode(ks, advertising,
2145 						     Asym_Pause);
2146 		break;
2147 	}
2148 
2149 	caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
2150 	if (!caps)
2151 		goto done;
2152 
2153 	if (ice_aq_get_phy_caps(vsi->port_info, false, ICE_AQC_REPORT_TOPO_CAP,
2154 				caps, NULL))
2155 		netdev_info(netdev, "Get phy capability failed.\n");
2156 
2157 	/* Set supported FEC modes based on PHY capability */
2158 	ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
2159 
2160 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
2161 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN)
2162 		ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
2163 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
2164 		ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
2165 
2166 	if (ice_aq_get_phy_caps(vsi->port_info, false, ICE_AQC_REPORT_SW_CFG,
2167 				caps, NULL))
2168 		netdev_info(netdev, "Get phy capability failed.\n");
2169 
2170 	/* Set advertised FEC modes based on PHY capability */
2171 	ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_NONE);
2172 
2173 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
2174 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
2175 		ethtool_link_ksettings_add_link_mode(ks, advertising,
2176 						     FEC_BASER);
2177 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
2178 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
2179 		ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
2180 
2181 done:
2182 	devm_kfree(&vsi->back->pdev->dev, caps);
2183 	return 0;
2184 }
2185 
2186 /**
2187  * ice_ksettings_find_adv_link_speed - Find advertising link speed
2188  * @ks: ethtool ksettings
2189  */
2190 static u16
2191 ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
2192 {
2193 	u16 adv_link_speed = 0;
2194 
2195 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2196 						  100baseT_Full))
2197 		adv_link_speed |= ICE_AQ_LINK_SPEED_100MB;
2198 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2199 						  1000baseX_Full))
2200 		adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2201 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2202 						  1000baseT_Full) ||
2203 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2204 						  1000baseKX_Full))
2205 		adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2206 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2207 						  2500baseT_Full))
2208 		adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2209 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2210 						  2500baseX_Full))
2211 		adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2212 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2213 						  5000baseT_Full))
2214 		adv_link_speed |= ICE_AQ_LINK_SPEED_5GB;
2215 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2216 						  10000baseT_Full) ||
2217 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2218 						  10000baseKR_Full))
2219 		adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2220 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2221 						  10000baseSR_Full) ||
2222 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2223 						  10000baseLR_Full))
2224 		adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2225 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2226 						  25000baseCR_Full) ||
2227 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2228 						  25000baseSR_Full) ||
2229 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2230 						  25000baseKR_Full))
2231 		adv_link_speed |= ICE_AQ_LINK_SPEED_25GB;
2232 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2233 						  40000baseCR4_Full) ||
2234 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2235 						  40000baseSR4_Full) ||
2236 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2237 						  40000baseLR4_Full) ||
2238 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2239 						  40000baseKR4_Full))
2240 		adv_link_speed |= ICE_AQ_LINK_SPEED_40GB;
2241 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2242 						  50000baseCR2_Full) ||
2243 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2244 						  50000baseKR2_Full))
2245 		adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2246 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2247 						  50000baseSR2_Full))
2248 		adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2249 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2250 						  100000baseCR4_Full) ||
2251 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2252 						  100000baseSR4_Full) ||
2253 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2254 						  100000baseLR4_ER4_Full) ||
2255 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2256 						  100000baseKR4_Full))
2257 		adv_link_speed |= ICE_AQ_LINK_SPEED_100GB;
2258 
2259 	return adv_link_speed;
2260 }
2261 
2262 /**
2263  * ice_setup_autoneg
2264  * @p: port info
2265  * @ks: ethtool_link_ksettings
2266  * @config: configuration that will be sent down to FW
2267  * @autoneg_enabled: autonegotiation is enabled or not
2268  * @autoneg_changed: will there a change in autonegotiation
2269  * @netdev: network interface device structure
2270  *
2271  * Setup PHY autonegotiation feature
2272  */
2273 static int
2274 ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
2275 		  struct ice_aqc_set_phy_cfg_data *config,
2276 		  u8 autoneg_enabled, u8 *autoneg_changed,
2277 		  struct net_device *netdev)
2278 {
2279 	int err = 0;
2280 
2281 	*autoneg_changed = 0;
2282 
2283 	/* Check autoneg */
2284 	if (autoneg_enabled == AUTONEG_ENABLE) {
2285 		/* If autoneg was not already enabled */
2286 		if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
2287 			/* If autoneg is not supported, return error */
2288 			if (!ethtool_link_ksettings_test_link_mode(ks,
2289 								   supported,
2290 								   Autoneg)) {
2291 				netdev_info(netdev, "Autoneg not supported on this phy.\n");
2292 				err = -EINVAL;
2293 			} else {
2294 				/* Autoneg is allowed to change */
2295 				config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2296 				*autoneg_changed = 1;
2297 			}
2298 		}
2299 	} else {
2300 		/* If autoneg is currently enabled */
2301 		if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
2302 			/* If autoneg is supported 10GBASE_T is the only PHY
2303 			 * that can disable it, so otherwise return error
2304 			 */
2305 			if (ethtool_link_ksettings_test_link_mode(ks,
2306 								  supported,
2307 								  Autoneg)) {
2308 				netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
2309 				err = -EINVAL;
2310 			} else {
2311 				/* Autoneg is allowed to change */
2312 				config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2313 				*autoneg_changed = 1;
2314 			}
2315 		}
2316 	}
2317 
2318 	return err;
2319 }
2320 
2321 /**
2322  * ice_set_link_ksettings - Set Speed and Duplex
2323  * @netdev: network interface device structure
2324  * @ks: ethtool ksettings
2325  *
2326  * Set speed/duplex per media_types advertised/forced
2327  */
2328 static int
2329 ice_set_link_ksettings(struct net_device *netdev,
2330 		       const struct ethtool_link_ksettings *ks)
2331 {
2332 	u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT, lport = 0;
2333 	struct ice_netdev_priv *np = netdev_priv(netdev);
2334 	struct ethtool_link_ksettings safe_ks, copy_ks;
2335 	struct ice_aqc_get_phy_caps_data *abilities;
2336 	u16 adv_link_speed, curr_link_speed, idx;
2337 	struct ice_aqc_set_phy_cfg_data config;
2338 	struct ice_pf *pf = np->vsi->back;
2339 	struct ice_port_info *p;
2340 	u8 autoneg_changed = 0;
2341 	enum ice_status status;
2342 	u64 phy_type_high;
2343 	u64 phy_type_low;
2344 	int err = 0;
2345 	bool linkup;
2346 
2347 	p = np->vsi->port_info;
2348 
2349 	if (!p)
2350 		return -EOPNOTSUPP;
2351 
2352 	/* Check if this is LAN VSI */
2353 	ice_for_each_vsi(pf, idx)
2354 		if (pf->vsi[idx]->type == ICE_VSI_PF) {
2355 			if (np->vsi != pf->vsi[idx])
2356 				return -EOPNOTSUPP;
2357 			break;
2358 		}
2359 
2360 	if (p->phy.media_type != ICE_MEDIA_BASET &&
2361 	    p->phy.media_type != ICE_MEDIA_FIBER &&
2362 	    p->phy.media_type != ICE_MEDIA_BACKPLANE &&
2363 	    p->phy.media_type != ICE_MEDIA_DA &&
2364 	    p->phy.link_info.link_info & ICE_AQ_LINK_UP)
2365 		return -EOPNOTSUPP;
2366 
2367 	/* copy the ksettings to copy_ks to avoid modifying the original */
2368 	memcpy(&copy_ks, ks, sizeof(copy_ks));
2369 
2370 	/* save autoneg out of ksettings */
2371 	autoneg = copy_ks.base.autoneg;
2372 
2373 	memset(&safe_ks, 0, sizeof(safe_ks));
2374 
2375 	/* Get link modes supported by hardware.*/
2376 	ice_phy_type_to_ethtool(netdev, &safe_ks);
2377 
2378 	/* and check against modes requested by user.
2379 	 * Return an error if unsupported mode was set.
2380 	 */
2381 	if (!bitmap_subset(copy_ks.link_modes.advertising,
2382 			   safe_ks.link_modes.supported,
2383 			   __ETHTOOL_LINK_MODE_MASK_NBITS))
2384 		return -EINVAL;
2385 
2386 	/* get our own copy of the bits to check against */
2387 	memset(&safe_ks, 0, sizeof(safe_ks));
2388 	safe_ks.base.cmd = copy_ks.base.cmd;
2389 	safe_ks.base.link_mode_masks_nwords =
2390 		copy_ks.base.link_mode_masks_nwords;
2391 	ice_get_link_ksettings(netdev, &safe_ks);
2392 
2393 	/* set autoneg back to what it currently is */
2394 	copy_ks.base.autoneg = safe_ks.base.autoneg;
2395 	/* we don't compare the speed */
2396 	copy_ks.base.speed = safe_ks.base.speed;
2397 
2398 	/* If copy_ks.base and safe_ks.base are not the same now, then they are
2399 	 * trying to set something that we do not support.
2400 	 */
2401 	if (memcmp(&copy_ks.base, &safe_ks.base, sizeof(copy_ks.base)))
2402 		return -EOPNOTSUPP;
2403 
2404 	while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
2405 		timeout--;
2406 		if (!timeout)
2407 			return -EBUSY;
2408 		usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
2409 	}
2410 
2411 	abilities = devm_kzalloc(&pf->pdev->dev, sizeof(*abilities),
2412 				 GFP_KERNEL);
2413 	if (!abilities)
2414 		return -ENOMEM;
2415 
2416 	/* Get the current PHY config */
2417 	status = ice_aq_get_phy_caps(p, false, ICE_AQC_REPORT_SW_CFG, abilities,
2418 				     NULL);
2419 	if (status) {
2420 		err = -EAGAIN;
2421 		goto done;
2422 	}
2423 
2424 	/* Copy abilities to config in case autoneg is not set below */
2425 	memset(&config, 0, sizeof(config));
2426 	config.caps = abilities->caps & ~ICE_AQC_PHY_AN_MODE;
2427 	if (abilities->caps & ICE_AQC_PHY_AN_MODE)
2428 		config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2429 
2430 	/* Check autoneg */
2431 	err = ice_setup_autoneg(p, &safe_ks, &config, autoneg, &autoneg_changed,
2432 				netdev);
2433 
2434 	if (err)
2435 		goto done;
2436 
2437 	/* Call to get the current link speed */
2438 	p->phy.get_link_info = true;
2439 	status = ice_get_link_status(p, &linkup);
2440 	if (status) {
2441 		err = -EAGAIN;
2442 		goto done;
2443 	}
2444 
2445 	curr_link_speed = p->phy.link_info.link_speed;
2446 	adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
2447 
2448 	/* If speed didn't get set, set it to what it currently is.
2449 	 * This is needed because if advertise is 0 (as it is when autoneg
2450 	 * is disabled) then speed won't get set.
2451 	 */
2452 	if (!adv_link_speed)
2453 		adv_link_speed = curr_link_speed;
2454 
2455 	/* Convert the advertise link speeds to their corresponded PHY_TYPE */
2456 	ice_update_phy_type(&phy_type_low, &phy_type_high, adv_link_speed);
2457 
2458 	if (!autoneg_changed && adv_link_speed == curr_link_speed) {
2459 		netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
2460 		goto done;
2461 	}
2462 
2463 	/* copy over the rest of the abilities */
2464 	config.low_power_ctrl = abilities->low_power_ctrl;
2465 	config.eee_cap = abilities->eee_cap;
2466 	config.eeer_value = abilities->eeer_value;
2467 	config.link_fec_opt = abilities->link_fec_options;
2468 
2469 	/* save the requested speeds */
2470 	p->phy.link_info.req_speeds = adv_link_speed;
2471 
2472 	/* set link and auto negotiation so changes take effect */
2473 	config.caps |= ICE_AQ_PHY_ENA_LINK;
2474 
2475 	if (phy_type_low || phy_type_high) {
2476 		config.phy_type_high = cpu_to_le64(phy_type_high) &
2477 			abilities->phy_type_high;
2478 		config.phy_type_low = cpu_to_le64(phy_type_low) &
2479 			abilities->phy_type_low;
2480 	} else {
2481 		err = -EAGAIN;
2482 		netdev_info(netdev, "Nothing changed. No PHY_TYPE is corresponded to advertised link speed.\n");
2483 		goto done;
2484 	}
2485 
2486 	/* If link is up put link down */
2487 	if (p->phy.link_info.link_info & ICE_AQ_LINK_UP) {
2488 		/* Tell the OS link is going down, the link will go
2489 		 * back up when fw says it is ready asynchronously
2490 		 */
2491 		ice_print_link_msg(np->vsi, false);
2492 		netif_carrier_off(netdev);
2493 		netif_tx_stop_all_queues(netdev);
2494 	}
2495 
2496 	/* make the aq call */
2497 	status = ice_aq_set_phy_cfg(&pf->hw, lport, &config, NULL);
2498 	if (status) {
2499 		netdev_info(netdev, "Set phy config failed,\n");
2500 		err = -EAGAIN;
2501 	}
2502 
2503 done:
2504 	devm_kfree(&pf->pdev->dev, abilities);
2505 	clear_bit(__ICE_CFG_BUSY, pf->state);
2506 
2507 	return err;
2508 }
2509 
2510 /**
2511  * ice_get_rxnfc - command to get Rx flow classification rules
2512  * @netdev: network interface device structure
2513  * @cmd: ethtool rxnfc command
2514  * @rule_locs: buffer to rturn Rx flow classification rules
2515  *
2516  * Returns Success if the command is supported.
2517  */
2518 static int
2519 ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2520 	      u32 __always_unused *rule_locs)
2521 {
2522 	struct ice_netdev_priv *np = netdev_priv(netdev);
2523 	struct ice_vsi *vsi = np->vsi;
2524 	int ret = -EOPNOTSUPP;
2525 
2526 	switch (cmd->cmd) {
2527 	case ETHTOOL_GRXRINGS:
2528 		cmd->data = vsi->rss_size;
2529 		ret = 0;
2530 		break;
2531 	default:
2532 		break;
2533 	}
2534 
2535 	return ret;
2536 }
2537 
2538 static void
2539 ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2540 {
2541 	struct ice_netdev_priv *np = netdev_priv(netdev);
2542 	struct ice_vsi *vsi = np->vsi;
2543 
2544 	ring->rx_max_pending = ICE_MAX_NUM_DESC;
2545 	ring->tx_max_pending = ICE_MAX_NUM_DESC;
2546 	ring->rx_pending = vsi->rx_rings[0]->count;
2547 	ring->tx_pending = vsi->tx_rings[0]->count;
2548 
2549 	/* Rx mini and jumbo rings are not supported */
2550 	ring->rx_mini_max_pending = 0;
2551 	ring->rx_jumbo_max_pending = 0;
2552 	ring->rx_mini_pending = 0;
2553 	ring->rx_jumbo_pending = 0;
2554 }
2555 
2556 static int
2557 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2558 {
2559 	struct ice_ring *tx_rings = NULL, *rx_rings = NULL;
2560 	struct ice_netdev_priv *np = netdev_priv(netdev);
2561 	struct ice_vsi *vsi = np->vsi;
2562 	struct ice_pf *pf = vsi->back;
2563 	int i, timeout = 50, err = 0;
2564 	u32 new_rx_cnt, new_tx_cnt;
2565 
2566 	if (ring->tx_pending > ICE_MAX_NUM_DESC ||
2567 	    ring->tx_pending < ICE_MIN_NUM_DESC ||
2568 	    ring->rx_pending > ICE_MAX_NUM_DESC ||
2569 	    ring->rx_pending < ICE_MIN_NUM_DESC) {
2570 		netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
2571 			   ring->tx_pending, ring->rx_pending,
2572 			   ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
2573 			   ICE_REQ_DESC_MULTIPLE);
2574 		return -EINVAL;
2575 	}
2576 
2577 	new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
2578 	if (new_tx_cnt != ring->tx_pending)
2579 		netdev_info(netdev,
2580 			    "Requested Tx descriptor count rounded up to %d\n",
2581 			    new_tx_cnt);
2582 	new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
2583 	if (new_rx_cnt != ring->rx_pending)
2584 		netdev_info(netdev,
2585 			    "Requested Rx descriptor count rounded up to %d\n",
2586 			    new_rx_cnt);
2587 
2588 	/* if nothing to do return success */
2589 	if (new_tx_cnt == vsi->tx_rings[0]->count &&
2590 	    new_rx_cnt == vsi->rx_rings[0]->count) {
2591 		netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
2592 		return 0;
2593 	}
2594 
2595 	while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
2596 		timeout--;
2597 		if (!timeout)
2598 			return -EBUSY;
2599 		usleep_range(1000, 2000);
2600 	}
2601 
2602 	/* set for the next time the netdev is started */
2603 	if (!netif_running(vsi->netdev)) {
2604 		for (i = 0; i < vsi->alloc_txq; i++)
2605 			vsi->tx_rings[i]->count = new_tx_cnt;
2606 		for (i = 0; i < vsi->alloc_rxq; i++)
2607 			vsi->rx_rings[i]->count = new_rx_cnt;
2608 		netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
2609 		goto done;
2610 	}
2611 
2612 	if (new_tx_cnt == vsi->tx_rings[0]->count)
2613 		goto process_rx;
2614 
2615 	/* alloc updated Tx resources */
2616 	netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
2617 		    vsi->tx_rings[0]->count, new_tx_cnt);
2618 
2619 	tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
2620 				sizeof(*tx_rings), GFP_KERNEL);
2621 	if (!tx_rings) {
2622 		err = -ENOMEM;
2623 		goto done;
2624 	}
2625 
2626 	for (i = 0; i < vsi->alloc_txq; i++) {
2627 		/* clone ring and setup updated count */
2628 		tx_rings[i] = *vsi->tx_rings[i];
2629 		tx_rings[i].count = new_tx_cnt;
2630 		tx_rings[i].desc = NULL;
2631 		tx_rings[i].tx_buf = NULL;
2632 		err = ice_setup_tx_ring(&tx_rings[i]);
2633 		if (err) {
2634 			while (i) {
2635 				i--;
2636 				ice_clean_tx_ring(&tx_rings[i]);
2637 			}
2638 			devm_kfree(&pf->pdev->dev, tx_rings);
2639 			goto done;
2640 		}
2641 	}
2642 
2643 process_rx:
2644 	if (new_rx_cnt == vsi->rx_rings[0]->count)
2645 		goto process_link;
2646 
2647 	/* alloc updated Rx resources */
2648 	netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
2649 		    vsi->rx_rings[0]->count, new_rx_cnt);
2650 
2651 	rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
2652 				sizeof(*rx_rings), GFP_KERNEL);
2653 	if (!rx_rings) {
2654 		err = -ENOMEM;
2655 		goto done;
2656 	}
2657 
2658 	for (i = 0; i < vsi->alloc_rxq; i++) {
2659 		/* clone ring and setup updated count */
2660 		rx_rings[i] = *vsi->rx_rings[i];
2661 		rx_rings[i].count = new_rx_cnt;
2662 		rx_rings[i].desc = NULL;
2663 		rx_rings[i].rx_buf = NULL;
2664 		/* this is to allow wr32 to have something to write to
2665 		 * during early allocation of Rx buffers
2666 		 */
2667 		rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
2668 
2669 		err = ice_setup_rx_ring(&rx_rings[i]);
2670 		if (err)
2671 			goto rx_unwind;
2672 
2673 		/* allocate Rx buffers */
2674 		err = ice_alloc_rx_bufs(&rx_rings[i],
2675 					ICE_DESC_UNUSED(&rx_rings[i]));
2676 rx_unwind:
2677 		if (err) {
2678 			while (i) {
2679 				i--;
2680 				ice_free_rx_ring(&rx_rings[i]);
2681 			}
2682 			devm_kfree(&pf->pdev->dev, rx_rings);
2683 			err = -ENOMEM;
2684 			goto free_tx;
2685 		}
2686 	}
2687 
2688 process_link:
2689 	/* Bring interface down, copy in the new ring info, then restore the
2690 	 * interface. if VSI is up, bring it down and then back up
2691 	 */
2692 	if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
2693 		ice_down(vsi);
2694 
2695 		if (tx_rings) {
2696 			for (i = 0; i < vsi->alloc_txq; i++) {
2697 				ice_free_tx_ring(vsi->tx_rings[i]);
2698 				*vsi->tx_rings[i] = tx_rings[i];
2699 			}
2700 			devm_kfree(&pf->pdev->dev, tx_rings);
2701 		}
2702 
2703 		if (rx_rings) {
2704 			for (i = 0; i < vsi->alloc_rxq; i++) {
2705 				ice_free_rx_ring(vsi->rx_rings[i]);
2706 				/* copy the real tail offset */
2707 				rx_rings[i].tail = vsi->rx_rings[i]->tail;
2708 				/* this is to fake out the allocation routine
2709 				 * into thinking it has to realloc everything
2710 				 * but the recycling logic will let us re-use
2711 				 * the buffers allocated above
2712 				 */
2713 				rx_rings[i].next_to_use = 0;
2714 				rx_rings[i].next_to_clean = 0;
2715 				rx_rings[i].next_to_alloc = 0;
2716 				*vsi->rx_rings[i] = rx_rings[i];
2717 			}
2718 			devm_kfree(&pf->pdev->dev, rx_rings);
2719 		}
2720 
2721 		ice_up(vsi);
2722 	}
2723 	goto done;
2724 
2725 free_tx:
2726 	/* error cleanup if the Rx allocations failed after getting Tx */
2727 	if (tx_rings) {
2728 		for (i = 0; i < vsi->alloc_txq; i++)
2729 			ice_free_tx_ring(&tx_rings[i]);
2730 		devm_kfree(&pf->pdev->dev, tx_rings);
2731 	}
2732 
2733 done:
2734 	clear_bit(__ICE_CFG_BUSY, pf->state);
2735 	return err;
2736 }
2737 
2738 static int ice_nway_reset(struct net_device *netdev)
2739 {
2740 	/* restart autonegotiation */
2741 	struct ice_netdev_priv *np = netdev_priv(netdev);
2742 	struct ice_vsi *vsi = np->vsi;
2743 	struct ice_port_info *pi;
2744 	enum ice_status status;
2745 
2746 	pi = vsi->port_info;
2747 	/* If VSI state is up, then restart autoneg with link up */
2748 	if (!test_bit(__ICE_DOWN, vsi->back->state))
2749 		status = ice_aq_set_link_restart_an(pi, true, NULL);
2750 	else
2751 		status = ice_aq_set_link_restart_an(pi, false, NULL);
2752 
2753 	if (status) {
2754 		netdev_info(netdev, "link restart failed, err %d aq_err %d\n",
2755 			    status, pi->hw->adminq.sq_last_status);
2756 		return -EIO;
2757 	}
2758 
2759 	return 0;
2760 }
2761 
2762 /**
2763  * ice_get_pauseparam - Get Flow Control status
2764  * @netdev: network interface device structure
2765  * @pause: ethernet pause (flow control) parameters
2766  */
2767 static void
2768 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2769 {
2770 	struct ice_netdev_priv *np = netdev_priv(netdev);
2771 	struct ice_port_info *pi = np->vsi->port_info;
2772 	struct ice_aqc_get_phy_caps_data *pcaps;
2773 	struct ice_vsi *vsi = np->vsi;
2774 	struct ice_dcbx_cfg *dcbx_cfg;
2775 	enum ice_status status;
2776 
2777 	/* Initialize pause params */
2778 	pause->rx_pause = 0;
2779 	pause->tx_pause = 0;
2780 
2781 	dcbx_cfg = &pi->local_dcbx_cfg;
2782 
2783 	pcaps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*pcaps),
2784 			     GFP_KERNEL);
2785 	if (!pcaps)
2786 		return;
2787 
2788 	/* Get current PHY config */
2789 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
2790 				     NULL);
2791 	if (status)
2792 		goto out;
2793 
2794 	pause->autoneg = ((pcaps->caps & ICE_AQC_PHY_AN_MODE) ?
2795 			AUTONEG_ENABLE : AUTONEG_DISABLE);
2796 
2797 	if (dcbx_cfg->pfc.pfcena)
2798 		/* PFC enabled so report LFC as off */
2799 		goto out;
2800 
2801 	if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
2802 		pause->tx_pause = 1;
2803 	if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
2804 		pause->rx_pause = 1;
2805 
2806 out:
2807 	devm_kfree(&vsi->back->pdev->dev, pcaps);
2808 }
2809 
2810 /**
2811  * ice_set_pauseparam - Set Flow Control parameter
2812  * @netdev: network interface device structure
2813  * @pause: return Tx/Rx flow control status
2814  */
2815 static int
2816 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2817 {
2818 	struct ice_netdev_priv *np = netdev_priv(netdev);
2819 	struct ice_link_status *hw_link_info;
2820 	struct ice_pf *pf = np->vsi->back;
2821 	struct ice_dcbx_cfg *dcbx_cfg;
2822 	struct ice_vsi *vsi = np->vsi;
2823 	struct ice_hw *hw = &pf->hw;
2824 	struct ice_port_info *pi;
2825 	enum ice_status status;
2826 	u8 aq_failures;
2827 	bool link_up;
2828 	int err = 0;
2829 
2830 	pi = vsi->port_info;
2831 	hw_link_info = &pi->phy.link_info;
2832 	dcbx_cfg = &pi->local_dcbx_cfg;
2833 	link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
2834 
2835 	/* Changing the port's flow control is not supported if this isn't the
2836 	 * PF VSI
2837 	 */
2838 	if (vsi->type != ICE_VSI_PF) {
2839 		netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
2840 		return -EOPNOTSUPP;
2841 	}
2842 
2843 	if (pause->autoneg != (hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
2844 		netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
2845 		return -EOPNOTSUPP;
2846 	}
2847 
2848 	/* If we have link and don't have autoneg */
2849 	if (!test_bit(__ICE_DOWN, pf->state) &&
2850 	    !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
2851 		/* Send message that it might not necessarily work*/
2852 		netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
2853 	}
2854 
2855 	if (dcbx_cfg->pfc.pfcena) {
2856 		netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
2857 		return -EOPNOTSUPP;
2858 	}
2859 	if (pause->rx_pause && pause->tx_pause)
2860 		pi->fc.req_mode = ICE_FC_FULL;
2861 	else if (pause->rx_pause && !pause->tx_pause)
2862 		pi->fc.req_mode = ICE_FC_RX_PAUSE;
2863 	else if (!pause->rx_pause && pause->tx_pause)
2864 		pi->fc.req_mode = ICE_FC_TX_PAUSE;
2865 	else if (!pause->rx_pause && !pause->tx_pause)
2866 		pi->fc.req_mode = ICE_FC_NONE;
2867 	else
2868 		return -EINVAL;
2869 
2870 	/* Tell the OS link is going down, the link will go back up when fw
2871 	 * says it is ready asynchronously
2872 	 */
2873 	ice_print_link_msg(vsi, false);
2874 	netif_carrier_off(netdev);
2875 	netif_tx_stop_all_queues(netdev);
2876 
2877 	/* Set the FC mode and only restart AN if link is up */
2878 	status = ice_set_fc(pi, &aq_failures, link_up);
2879 
2880 	if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
2881 		netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %d\n",
2882 			    status, hw->adminq.sq_last_status);
2883 		err = -EAGAIN;
2884 	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
2885 		netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %d\n",
2886 			    status, hw->adminq.sq_last_status);
2887 		err = -EAGAIN;
2888 	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
2889 		netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %d\n",
2890 			    status, hw->adminq.sq_last_status);
2891 		err = -EAGAIN;
2892 	}
2893 
2894 	if (!test_bit(__ICE_DOWN, pf->state)) {
2895 		/* Give it a little more time to try to come back. If still
2896 		 * down, restart autoneg link or reinitialize the interface.
2897 		 */
2898 		msleep(75);
2899 		if (!test_bit(__ICE_DOWN, pf->state))
2900 			return ice_nway_reset(netdev);
2901 
2902 		ice_down(vsi);
2903 		ice_up(vsi);
2904 	}
2905 
2906 	return err;
2907 }
2908 
2909 /**
2910  * ice_get_rxfh_key_size - get the RSS hash key size
2911  * @netdev: network interface device structure
2912  *
2913  * Returns the table size.
2914  */
2915 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
2916 {
2917 	return ICE_VSIQF_HKEY_ARRAY_SIZE;
2918 }
2919 
2920 /**
2921  * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
2922  * @netdev: network interface device structure
2923  *
2924  * Returns the table size.
2925  */
2926 static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
2927 {
2928 	struct ice_netdev_priv *np = netdev_priv(netdev);
2929 
2930 	return np->vsi->rss_table_size;
2931 }
2932 
2933 /**
2934  * ice_get_rxfh - get the Rx flow hash indirection table
2935  * @netdev: network interface device structure
2936  * @indir: indirection table
2937  * @key: hash key
2938  * @hfunc: hash function
2939  *
2940  * Reads the indirection table directly from the hardware.
2941  */
2942 static int
2943 ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
2944 {
2945 	struct ice_netdev_priv *np = netdev_priv(netdev);
2946 	struct ice_vsi *vsi = np->vsi;
2947 	struct ice_pf *pf = vsi->back;
2948 	int ret = 0, i;
2949 	u8 *lut;
2950 
2951 	if (hfunc)
2952 		*hfunc = ETH_RSS_HASH_TOP;
2953 
2954 	if (!indir)
2955 		return 0;
2956 
2957 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2958 		/* RSS not supported return error here */
2959 		netdev_warn(netdev, "RSS is not configured on this VSI!\n");
2960 		return -EIO;
2961 	}
2962 
2963 	lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
2964 	if (!lut)
2965 		return -ENOMEM;
2966 
2967 	if (ice_get_rss(vsi, key, lut, vsi->rss_table_size)) {
2968 		ret = -EIO;
2969 		goto out;
2970 	}
2971 
2972 	for (i = 0; i < vsi->rss_table_size; i++)
2973 		indir[i] = (u32)(lut[i]);
2974 
2975 out:
2976 	devm_kfree(&pf->pdev->dev, lut);
2977 	return ret;
2978 }
2979 
2980 /**
2981  * ice_set_rxfh - set the Rx flow hash indirection table
2982  * @netdev: network interface device structure
2983  * @indir: indirection table
2984  * @key: hash key
2985  * @hfunc: hash function
2986  *
2987  * Returns -EINVAL if the table specifies an invalid queue ID, otherwise
2988  * returns 0 after programming the table.
2989  */
2990 static int
2991 ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key,
2992 	     const u8 hfunc)
2993 {
2994 	struct ice_netdev_priv *np = netdev_priv(netdev);
2995 	struct ice_vsi *vsi = np->vsi;
2996 	struct ice_pf *pf = vsi->back;
2997 	u8 *seed = NULL;
2998 
2999 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3000 		return -EOPNOTSUPP;
3001 
3002 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3003 		/* RSS not supported return error here */
3004 		netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3005 		return -EIO;
3006 	}
3007 
3008 	if (key) {
3009 		if (!vsi->rss_hkey_user) {
3010 			vsi->rss_hkey_user =
3011 				devm_kzalloc(&pf->pdev->dev,
3012 					     ICE_VSIQF_HKEY_ARRAY_SIZE,
3013 					     GFP_KERNEL);
3014 			if (!vsi->rss_hkey_user)
3015 				return -ENOMEM;
3016 		}
3017 		memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
3018 		seed = vsi->rss_hkey_user;
3019 	}
3020 
3021 	if (!vsi->rss_lut_user) {
3022 		vsi->rss_lut_user = devm_kzalloc(&pf->pdev->dev,
3023 						 vsi->rss_table_size,
3024 						 GFP_KERNEL);
3025 		if (!vsi->rss_lut_user)
3026 			return -ENOMEM;
3027 	}
3028 
3029 	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
3030 	if (indir) {
3031 		int i;
3032 
3033 		for (i = 0; i < vsi->rss_table_size; i++)
3034 			vsi->rss_lut_user[i] = (u8)(indir[i]);
3035 	} else {
3036 		ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
3037 				 vsi->rss_size);
3038 	}
3039 
3040 	if (ice_set_rss(vsi, seed, vsi->rss_lut_user, vsi->rss_table_size))
3041 		return -EIO;
3042 
3043 	return 0;
3044 }
3045 
3046 enum ice_container_type {
3047 	ICE_RX_CONTAINER,
3048 	ICE_TX_CONTAINER,
3049 };
3050 
3051 /**
3052  * ice_get_rc_coalesce - get ITR values for specific ring container
3053  * @ec: ethtool structure to fill with driver's coalesce settings
3054  * @c_type: container type, Rx or Tx
3055  * @rc: ring container that the ITR values will come from
3056  *
3057  * Query the device for ice_ring_container specific ITR values. This is
3058  * done per ice_ring_container because each q_vector can have 1 or more rings
3059  * and all of said ring(s) will have the same ITR values.
3060  *
3061  * Returns 0 on success, negative otherwise.
3062  */
3063 static int
3064 ice_get_rc_coalesce(struct ethtool_coalesce *ec, enum ice_container_type c_type,
3065 		    struct ice_ring_container *rc)
3066 {
3067 	struct ice_pf *pf;
3068 
3069 	if (!rc->ring)
3070 		return -EINVAL;
3071 
3072 	pf = rc->ring->vsi->back;
3073 
3074 	switch (c_type) {
3075 	case ICE_RX_CONTAINER:
3076 		ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
3077 		ec->rx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3078 		ec->rx_coalesce_usecs_high = rc->ring->q_vector->intrl;
3079 		break;
3080 	case ICE_TX_CONTAINER:
3081 		ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
3082 		ec->tx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3083 		break;
3084 	default:
3085 		dev_dbg(&pf->pdev->dev, "Invalid c_type %d\n", c_type);
3086 		return -EINVAL;
3087 	}
3088 
3089 	return 0;
3090 }
3091 
3092 /**
3093  * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings
3094  * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings
3095  * @ec: coalesce settings to program the device with
3096  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3097  *
3098  * Return 0 on success, and negative under the following conditions:
3099  * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed.
3100  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3101  */
3102 static int
3103 ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3104 {
3105 	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3106 		if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
3107 					&vsi->rx_rings[q_num]->q_vector->rx))
3108 			return -EINVAL;
3109 		if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
3110 					&vsi->tx_rings[q_num]->q_vector->tx))
3111 			return -EINVAL;
3112 	} else if (q_num < vsi->num_rxq) {
3113 		if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
3114 					&vsi->rx_rings[q_num]->q_vector->rx))
3115 			return -EINVAL;
3116 	} else if (q_num < vsi->num_txq) {
3117 		if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
3118 					&vsi->tx_rings[q_num]->q_vector->tx))
3119 			return -EINVAL;
3120 	} else {
3121 		return -EINVAL;
3122 	}
3123 
3124 	return 0;
3125 }
3126 
3127 /**
3128  * __ice_get_coalesce - get ITR/INTRL values for the device
3129  * @netdev: pointer to the netdev associated with this query
3130  * @ec: ethtool structure to fill with driver's coalesce settings
3131  * @q_num: queue number to get the coalesce settings for
3132  *
3133  * If the caller passes in a negative q_num then we return coalesce settings
3134  * based on queue number 0, else use the actual q_num passed in.
3135  */
3136 static int
3137 __ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3138 		   int q_num)
3139 {
3140 	struct ice_netdev_priv *np = netdev_priv(netdev);
3141 	struct ice_vsi *vsi = np->vsi;
3142 
3143 	if (q_num < 0)
3144 		q_num = 0;
3145 
3146 	if (ice_get_q_coalesce(vsi, ec, q_num))
3147 		return -EINVAL;
3148 
3149 	if (q_num < vsi->num_txq)
3150 		ec->tx_max_coalesced_frames_irq = vsi->work_lmt;
3151 
3152 	if (q_num < vsi->num_rxq)
3153 		ec->rx_max_coalesced_frames_irq = vsi->work_lmt;
3154 
3155 	return 0;
3156 }
3157 
3158 static int
3159 ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
3160 {
3161 	return __ice_get_coalesce(netdev, ec, -1);
3162 }
3163 
3164 static int
3165 ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
3166 		       struct ethtool_coalesce *ec)
3167 {
3168 	return __ice_get_coalesce(netdev, ec, q_num);
3169 }
3170 
3171 /**
3172  * ice_set_rc_coalesce - set ITR values for specific ring container
3173  * @c_type: container type, Rx or Tx
3174  * @ec: ethtool structure from user to update ITR settings
3175  * @rc: ring container that the ITR values will come from
3176  * @vsi: VSI associated to the ring container
3177  *
3178  * Set specific ITR values. This is done per ice_ring_container because each
3179  * q_vector can have 1 or more rings and all of said ring(s) will have the same
3180  * ITR values.
3181  *
3182  * Returns 0 on success, negative otherwise.
3183  */
3184 static int
3185 ice_set_rc_coalesce(enum ice_container_type c_type, struct ethtool_coalesce *ec,
3186 		    struct ice_ring_container *rc, struct ice_vsi *vsi)
3187 {
3188 	struct ice_pf *pf = vsi->back;
3189 	u16 itr_setting;
3190 
3191 	if (!rc->ring)
3192 		return -EINVAL;
3193 
3194 	itr_setting = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3195 
3196 	switch (c_type) {
3197 	case ICE_RX_CONTAINER:
3198 		if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL ||
3199 		    (ec->rx_coalesce_usecs_high &&
3200 		     ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) {
3201 			netdev_info(vsi->netdev,
3202 				    "Invalid value, rx-usecs-high valid values are 0 (disabled), %d-%d\n",
3203 				    pf->hw.intrl_gran, ICE_MAX_INTRL);
3204 			return -EINVAL;
3205 		}
3206 
3207 		if (ec->rx_coalesce_usecs_high != rc->ring->q_vector->intrl) {
3208 			rc->ring->q_vector->intrl = ec->rx_coalesce_usecs_high;
3209 			wr32(&pf->hw, GLINT_RATE(rc->ring->q_vector->reg_idx),
3210 			     ice_intrl_usec_to_reg(ec->rx_coalesce_usecs_high,
3211 						   pf->hw.intrl_gran));
3212 		}
3213 
3214 		if (ec->rx_coalesce_usecs != itr_setting &&
3215 		    ec->use_adaptive_rx_coalesce) {
3216 			netdev_info(vsi->netdev,
3217 				    "Rx interrupt throttling cannot be changed if adaptive-rx is enabled\n");
3218 			return -EINVAL;
3219 		}
3220 
3221 		if (ec->rx_coalesce_usecs > ICE_ITR_MAX) {
3222 			netdev_info(vsi->netdev,
3223 				    "Invalid value, rx-usecs range is 0-%d\n",
3224 				   ICE_ITR_MAX);
3225 			return -EINVAL;
3226 		}
3227 
3228 		if (ec->use_adaptive_rx_coalesce) {
3229 			rc->itr_setting |= ICE_ITR_DYNAMIC;
3230 		} else {
3231 			rc->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
3232 			rc->target_itr = ITR_TO_REG(rc->itr_setting);
3233 		}
3234 		break;
3235 	case ICE_TX_CONTAINER:
3236 		if (ec->tx_coalesce_usecs_high) {
3237 			netdev_info(vsi->netdev,
3238 				    "setting tx-usecs-high is not supported\n");
3239 			return -EINVAL;
3240 		}
3241 
3242 		if (ec->tx_coalesce_usecs != itr_setting &&
3243 		    ec->use_adaptive_tx_coalesce) {
3244 			netdev_info(vsi->netdev,
3245 				    "Tx interrupt throttling cannot be changed if adaptive-tx is enabled\n");
3246 			return -EINVAL;
3247 		}
3248 
3249 		if (ec->tx_coalesce_usecs > ICE_ITR_MAX) {
3250 			netdev_info(vsi->netdev,
3251 				    "Invalid value, tx-usecs range is 0-%d\n",
3252 				   ICE_ITR_MAX);
3253 			return -EINVAL;
3254 		}
3255 
3256 		if (ec->use_adaptive_tx_coalesce) {
3257 			rc->itr_setting |= ICE_ITR_DYNAMIC;
3258 		} else {
3259 			rc->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
3260 			rc->target_itr = ITR_TO_REG(rc->itr_setting);
3261 		}
3262 		break;
3263 	default:
3264 		dev_dbg(&pf->pdev->dev, "Invalid container type %d\n", c_type);
3265 		return -EINVAL;
3266 	}
3267 
3268 	return 0;
3269 }
3270 
3271 /**
3272  * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings
3273  * @vsi: VSI associated to the queue that need updating
3274  * @ec: coalesce settings to program the device with
3275  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3276  *
3277  * Return 0 on success, and negative under the following conditions:
3278  * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed.
3279  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3280  */
3281 static int
3282 ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3283 {
3284 	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3285 		if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
3286 					&vsi->rx_rings[q_num]->q_vector->rx,
3287 					vsi))
3288 			return -EINVAL;
3289 
3290 		if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
3291 					&vsi->tx_rings[q_num]->q_vector->tx,
3292 					vsi))
3293 			return -EINVAL;
3294 	} else if (q_num < vsi->num_rxq) {
3295 		if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
3296 					&vsi->rx_rings[q_num]->q_vector->rx,
3297 					vsi))
3298 			return -EINVAL;
3299 	} else if (q_num < vsi->num_txq) {
3300 		if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
3301 					&vsi->tx_rings[q_num]->q_vector->tx,
3302 					vsi))
3303 			return -EINVAL;
3304 	} else {
3305 		return -EINVAL;
3306 	}
3307 
3308 	return 0;
3309 }
3310 
3311 /**
3312  * __ice_set_coalesce - set ITR/INTRL values for the device
3313  * @netdev: pointer to the netdev associated with this query
3314  * @ec: ethtool structure to fill with driver's coalesce settings
3315  * @q_num: queue number to get the coalesce settings for
3316  *
3317  * If the caller passes in a negative q_num then we set the coalesce settings
3318  * for all Tx/Rx queues, else use the actual q_num passed in.
3319  */
3320 static int
3321 __ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3322 		   int q_num)
3323 {
3324 	struct ice_netdev_priv *np = netdev_priv(netdev);
3325 	struct ice_vsi *vsi = np->vsi;
3326 
3327 	if (q_num < 0) {
3328 		int i;
3329 
3330 		ice_for_each_q_vector(vsi, i) {
3331 			if (ice_set_q_coalesce(vsi, ec, i))
3332 				return -EINVAL;
3333 		}
3334 		goto set_work_lmt;
3335 	}
3336 
3337 	if (ice_set_q_coalesce(vsi, ec, q_num))
3338 		return -EINVAL;
3339 
3340 set_work_lmt:
3341 
3342 	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
3343 		vsi->work_lmt = max(ec->tx_max_coalesced_frames_irq,
3344 				    ec->rx_max_coalesced_frames_irq);
3345 
3346 	return 0;
3347 }
3348 
3349 static int
3350 ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
3351 {
3352 	return __ice_set_coalesce(netdev, ec, -1);
3353 }
3354 
3355 static int
3356 ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
3357 		       struct ethtool_coalesce *ec)
3358 {
3359 	return __ice_set_coalesce(netdev, ec, q_num);
3360 }
3361 
3362 static const struct ethtool_ops ice_ethtool_ops = {
3363 	.get_link_ksettings	= ice_get_link_ksettings,
3364 	.set_link_ksettings	= ice_set_link_ksettings,
3365 	.get_drvinfo            = ice_get_drvinfo,
3366 	.get_regs_len           = ice_get_regs_len,
3367 	.get_regs               = ice_get_regs,
3368 	.get_msglevel           = ice_get_msglevel,
3369 	.set_msglevel           = ice_set_msglevel,
3370 	.self_test		= ice_self_test,
3371 	.get_link		= ethtool_op_get_link,
3372 	.get_eeprom_len		= ice_get_eeprom_len,
3373 	.get_eeprom		= ice_get_eeprom,
3374 	.get_coalesce		= ice_get_coalesce,
3375 	.set_coalesce		= ice_set_coalesce,
3376 	.get_strings		= ice_get_strings,
3377 	.set_phys_id		= ice_set_phys_id,
3378 	.get_ethtool_stats      = ice_get_ethtool_stats,
3379 	.get_priv_flags		= ice_get_priv_flags,
3380 	.set_priv_flags		= ice_set_priv_flags,
3381 	.get_sset_count		= ice_get_sset_count,
3382 	.get_rxnfc		= ice_get_rxnfc,
3383 	.get_ringparam		= ice_get_ringparam,
3384 	.set_ringparam		= ice_set_ringparam,
3385 	.nway_reset		= ice_nway_reset,
3386 	.get_pauseparam		= ice_get_pauseparam,
3387 	.set_pauseparam		= ice_set_pauseparam,
3388 	.get_rxfh_key_size	= ice_get_rxfh_key_size,
3389 	.get_rxfh_indir_size	= ice_get_rxfh_indir_size,
3390 	.get_rxfh		= ice_get_rxfh,
3391 	.set_rxfh		= ice_set_rxfh,
3392 	.get_ts_info		= ethtool_op_get_ts_info,
3393 	.get_per_queue_coalesce = ice_get_per_q_coalesce,
3394 	.set_per_queue_coalesce = ice_set_per_q_coalesce,
3395 	.get_fecparam		= ice_get_fecparam,
3396 	.set_fecparam		= ice_set_fecparam,
3397 };
3398 
3399 /**
3400  * ice_set_ethtool_ops - setup netdev ethtool ops
3401  * @netdev: network interface device structure
3402  *
3403  * setup netdev ethtool ops with ice specific ops
3404  */
3405 void ice_set_ethtool_ops(struct net_device *netdev)
3406 {
3407 	netdev->ethtool_ops = &ice_ethtool_ops;
3408 }
3409