xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_ethtool.c (revision a1b2f04ea527397fcacacd09e0d690927feef429)
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 ice_port_info *pi = np->vsi->port_info;
1720 	struct ethtool_link_ksettings cap_ksettings;
1721 	struct ice_link_status *link_info;
1722 	struct ice_vsi *vsi = np->vsi;
1723 	bool unrecog_phy_high = false;
1724 	bool unrecog_phy_low = false;
1725 
1726 	link_info = &vsi->port_info->phy.link_info;
1727 
1728 	/* Initialize supported and advertised settings based on PHY settings */
1729 	switch (link_info->phy_type_low) {
1730 	case ICE_PHY_TYPE_LOW_100BASE_TX:
1731 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1732 		ethtool_link_ksettings_add_link_mode(ks, supported,
1733 						     100baseT_Full);
1734 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1735 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1736 						     100baseT_Full);
1737 		break;
1738 	case ICE_PHY_TYPE_LOW_100M_SGMII:
1739 		ethtool_link_ksettings_add_link_mode(ks, supported,
1740 						     100baseT_Full);
1741 		break;
1742 	case ICE_PHY_TYPE_LOW_1000BASE_T:
1743 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1744 		ethtool_link_ksettings_add_link_mode(ks, supported,
1745 						     1000baseT_Full);
1746 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1747 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1748 						     1000baseT_Full);
1749 		break;
1750 	case ICE_PHY_TYPE_LOW_1G_SGMII:
1751 		ethtool_link_ksettings_add_link_mode(ks, supported,
1752 						     1000baseT_Full);
1753 		break;
1754 	case ICE_PHY_TYPE_LOW_1000BASE_SX:
1755 	case ICE_PHY_TYPE_LOW_1000BASE_LX:
1756 		ethtool_link_ksettings_add_link_mode(ks, supported,
1757 						     1000baseX_Full);
1758 		break;
1759 	case ICE_PHY_TYPE_LOW_1000BASE_KX:
1760 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1761 		ethtool_link_ksettings_add_link_mode(ks, supported,
1762 						     1000baseKX_Full);
1763 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1764 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1765 						     1000baseKX_Full);
1766 		break;
1767 	case ICE_PHY_TYPE_LOW_2500BASE_T:
1768 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1769 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1770 		ethtool_link_ksettings_add_link_mode(ks, supported,
1771 						     2500baseT_Full);
1772 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1773 						     2500baseT_Full);
1774 		break;
1775 	case ICE_PHY_TYPE_LOW_2500BASE_X:
1776 		ethtool_link_ksettings_add_link_mode(ks, supported,
1777 						     2500baseX_Full);
1778 		break;
1779 	case ICE_PHY_TYPE_LOW_2500BASE_KX:
1780 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1781 		ethtool_link_ksettings_add_link_mode(ks, supported,
1782 						     2500baseX_Full);
1783 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1784 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1785 						     2500baseX_Full);
1786 		break;
1787 	case ICE_PHY_TYPE_LOW_5GBASE_T:
1788 	case ICE_PHY_TYPE_LOW_5GBASE_KR:
1789 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1790 		ethtool_link_ksettings_add_link_mode(ks, supported,
1791 						     5000baseT_Full);
1792 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1793 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1794 						     5000baseT_Full);
1795 		break;
1796 	case ICE_PHY_TYPE_LOW_10GBASE_T:
1797 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1798 		ethtool_link_ksettings_add_link_mode(ks, supported,
1799 						     10000baseT_Full);
1800 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1801 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1802 						     10000baseT_Full);
1803 		break;
1804 	case ICE_PHY_TYPE_LOW_10G_SFI_DA:
1805 	case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
1806 	case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
1807 		ethtool_link_ksettings_add_link_mode(ks, supported,
1808 						     10000baseT_Full);
1809 		break;
1810 	case ICE_PHY_TYPE_LOW_10GBASE_SR:
1811 		ethtool_link_ksettings_add_link_mode(ks, supported,
1812 						     10000baseSR_Full);
1813 		break;
1814 	case ICE_PHY_TYPE_LOW_10GBASE_LR:
1815 		ethtool_link_ksettings_add_link_mode(ks, supported,
1816 						     10000baseLR_Full);
1817 		break;
1818 	case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
1819 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1820 		ethtool_link_ksettings_add_link_mode(ks, supported,
1821 						     10000baseKR_Full);
1822 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1823 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1824 						     10000baseKR_Full);
1825 		break;
1826 	case ICE_PHY_TYPE_LOW_25GBASE_T:
1827 	case ICE_PHY_TYPE_LOW_25GBASE_CR:
1828 	case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
1829 	case ICE_PHY_TYPE_LOW_25GBASE_CR1:
1830 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1831 		ethtool_link_ksettings_add_link_mode(ks, supported,
1832 						     25000baseCR_Full);
1833 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1834 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1835 						     25000baseCR_Full);
1836 		break;
1837 	case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
1838 	case ICE_PHY_TYPE_LOW_25G_AUI_C2C:
1839 		ethtool_link_ksettings_add_link_mode(ks, supported,
1840 						     25000baseCR_Full);
1841 		break;
1842 	case ICE_PHY_TYPE_LOW_25GBASE_SR:
1843 	case ICE_PHY_TYPE_LOW_25GBASE_LR:
1844 		ethtool_link_ksettings_add_link_mode(ks, supported,
1845 						     25000baseSR_Full);
1846 		break;
1847 	case ICE_PHY_TYPE_LOW_25GBASE_KR:
1848 	case ICE_PHY_TYPE_LOW_25GBASE_KR1:
1849 	case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
1850 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1851 		ethtool_link_ksettings_add_link_mode(ks, supported,
1852 						     25000baseKR_Full);
1853 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1854 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1855 						     25000baseKR_Full);
1856 		break;
1857 	case ICE_PHY_TYPE_LOW_40GBASE_CR4:
1858 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1859 		ethtool_link_ksettings_add_link_mode(ks, supported,
1860 						     40000baseCR4_Full);
1861 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1862 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1863 						     40000baseCR4_Full);
1864 		break;
1865 	case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
1866 	case ICE_PHY_TYPE_LOW_40G_XLAUI:
1867 		ethtool_link_ksettings_add_link_mode(ks, supported,
1868 						     40000baseCR4_Full);
1869 		break;
1870 	case ICE_PHY_TYPE_LOW_40GBASE_SR4:
1871 		ethtool_link_ksettings_add_link_mode(ks, supported,
1872 						     40000baseSR4_Full);
1873 		break;
1874 	case ICE_PHY_TYPE_LOW_40GBASE_LR4:
1875 		ethtool_link_ksettings_add_link_mode(ks, supported,
1876 						     40000baseLR4_Full);
1877 		break;
1878 	case ICE_PHY_TYPE_LOW_40GBASE_KR4:
1879 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1880 		ethtool_link_ksettings_add_link_mode(ks, supported,
1881 						     40000baseKR4_Full);
1882 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1883 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1884 						     40000baseKR4_Full);
1885 		break;
1886 	case ICE_PHY_TYPE_LOW_50GBASE_CR2:
1887 	case ICE_PHY_TYPE_LOW_50GBASE_CP:
1888 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1889 		ethtool_link_ksettings_add_link_mode(ks, supported,
1890 						     50000baseCR2_Full);
1891 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1892 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1893 						     50000baseCR2_Full);
1894 		break;
1895 	case ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC:
1896 	case ICE_PHY_TYPE_LOW_50G_LAUI2:
1897 	case ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC:
1898 	case ICE_PHY_TYPE_LOW_50G_AUI2:
1899 	case ICE_PHY_TYPE_LOW_50GBASE_SR:
1900 	case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
1901 	case ICE_PHY_TYPE_LOW_50G_AUI1:
1902 		ethtool_link_ksettings_add_link_mode(ks, supported,
1903 						     50000baseCR2_Full);
1904 		break;
1905 	case ICE_PHY_TYPE_LOW_50GBASE_KR2:
1906 	case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
1907 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1908 		ethtool_link_ksettings_add_link_mode(ks, supported,
1909 						     50000baseKR2_Full);
1910 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1911 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1912 						     50000baseKR2_Full);
1913 		break;
1914 	case ICE_PHY_TYPE_LOW_50GBASE_SR2:
1915 	case ICE_PHY_TYPE_LOW_50GBASE_LR2:
1916 	case ICE_PHY_TYPE_LOW_50GBASE_FR:
1917 	case ICE_PHY_TYPE_LOW_50GBASE_LR:
1918 		ethtool_link_ksettings_add_link_mode(ks, supported,
1919 						     50000baseSR2_Full);
1920 		break;
1921 	case ICE_PHY_TYPE_LOW_100GBASE_CR4:
1922 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1923 		ethtool_link_ksettings_add_link_mode(ks, supported,
1924 						     100000baseCR4_Full);
1925 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1926 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1927 						     100000baseCR4_Full);
1928 		break;
1929 	case ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC:
1930 	case ICE_PHY_TYPE_LOW_100G_CAUI4:
1931 	case ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC:
1932 	case ICE_PHY_TYPE_LOW_100G_AUI4:
1933 	case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
1934 		ethtool_link_ksettings_add_link_mode(ks, supported,
1935 						     100000baseCR4_Full);
1936 		break;
1937 	case ICE_PHY_TYPE_LOW_100GBASE_CP2:
1938 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1939 		ethtool_link_ksettings_add_link_mode(ks, supported,
1940 						     100000baseCR4_Full);
1941 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1942 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1943 						     100000baseCR4_Full);
1944 		break;
1945 	case ICE_PHY_TYPE_LOW_100GBASE_SR4:
1946 	case ICE_PHY_TYPE_LOW_100GBASE_SR2:
1947 		ethtool_link_ksettings_add_link_mode(ks, supported,
1948 						     100000baseSR4_Full);
1949 		break;
1950 	case ICE_PHY_TYPE_LOW_100GBASE_LR4:
1951 	case ICE_PHY_TYPE_LOW_100GBASE_DR:
1952 		ethtool_link_ksettings_add_link_mode(ks, supported,
1953 						     100000baseLR4_ER4_Full);
1954 		break;
1955 	case ICE_PHY_TYPE_LOW_100GBASE_KR4:
1956 	case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
1957 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1958 		ethtool_link_ksettings_add_link_mode(ks, supported,
1959 						     100000baseKR4_Full);
1960 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1961 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1962 						     100000baseKR4_Full);
1963 		break;
1964 	default:
1965 		unrecog_phy_low = true;
1966 	}
1967 
1968 	switch (link_info->phy_type_high) {
1969 	case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
1970 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1971 		ethtool_link_ksettings_add_link_mode(ks, supported,
1972 						     100000baseKR4_Full);
1973 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1974 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1975 						     100000baseKR4_Full);
1976 		break;
1977 	case ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC:
1978 	case ICE_PHY_TYPE_HIGH_100G_CAUI2:
1979 	case ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC:
1980 	case ICE_PHY_TYPE_HIGH_100G_AUI2:
1981 		ethtool_link_ksettings_add_link_mode(ks, supported,
1982 						     100000baseCR4_Full);
1983 		break;
1984 	default:
1985 		unrecog_phy_high = true;
1986 	}
1987 
1988 	if (unrecog_phy_low && unrecog_phy_high) {
1989 		/* if we got here and link is up something bad is afoot */
1990 		netdev_info(netdev,
1991 			    "WARNING: Unrecognized PHY_Low (0x%llx).\n",
1992 			    (u64)link_info->phy_type_low);
1993 		netdev_info(netdev,
1994 			    "WARNING: Unrecognized PHY_High (0x%llx).\n",
1995 			    (u64)link_info->phy_type_high);
1996 	}
1997 
1998 	/* Now that we've worked out everything that could be supported by the
1999 	 * current PHY type, get what is supported by the NVM and intersect
2000 	 * them to get what is truly supported
2001 	 */
2002 	memset(&cap_ksettings, 0, sizeof(cap_ksettings));
2003 	ice_phy_type_to_ethtool(netdev, &cap_ksettings);
2004 	ethtool_intersect_link_masks(ks, &cap_ksettings);
2005 
2006 	switch (link_info->link_speed) {
2007 	case ICE_AQ_LINK_SPEED_100GB:
2008 		ks->base.speed = SPEED_100000;
2009 		break;
2010 	case ICE_AQ_LINK_SPEED_50GB:
2011 		ks->base.speed = SPEED_50000;
2012 		break;
2013 	case ICE_AQ_LINK_SPEED_40GB:
2014 		ks->base.speed = SPEED_40000;
2015 		break;
2016 	case ICE_AQ_LINK_SPEED_25GB:
2017 		ks->base.speed = SPEED_25000;
2018 		break;
2019 	case ICE_AQ_LINK_SPEED_20GB:
2020 		ks->base.speed = SPEED_20000;
2021 		break;
2022 	case ICE_AQ_LINK_SPEED_10GB:
2023 		ks->base.speed = SPEED_10000;
2024 		break;
2025 	case ICE_AQ_LINK_SPEED_5GB:
2026 		ks->base.speed = SPEED_5000;
2027 		break;
2028 	case ICE_AQ_LINK_SPEED_2500MB:
2029 		ks->base.speed = SPEED_2500;
2030 		break;
2031 	case ICE_AQ_LINK_SPEED_1000MB:
2032 		ks->base.speed = SPEED_1000;
2033 		break;
2034 	case ICE_AQ_LINK_SPEED_100MB:
2035 		ks->base.speed = SPEED_100;
2036 		break;
2037 	default:
2038 		netdev_info(netdev,
2039 			    "WARNING: Unrecognized link_speed (0x%x).\n",
2040 			    link_info->link_speed);
2041 		break;
2042 	}
2043 	ks->base.duplex = DUPLEX_FULL;
2044 
2045 	if (link_info->an_info & ICE_AQ_AN_COMPLETED)
2046 		ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
2047 						     Autoneg);
2048 
2049 	/* Set flow control negotiated Rx/Tx pause */
2050 	switch (pi->fc.current_mode) {
2051 	case ICE_FC_FULL:
2052 		ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
2053 		break;
2054 	case ICE_FC_TX_PAUSE:
2055 		ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
2056 		ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
2057 						     Asym_Pause);
2058 		break;
2059 	case ICE_FC_RX_PAUSE:
2060 		ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
2061 						     Asym_Pause);
2062 		break;
2063 	case ICE_FC_PFC:
2064 		/* fall through */
2065 	default:
2066 		ethtool_link_ksettings_del_link_mode(ks, lp_advertising, Pause);
2067 		ethtool_link_ksettings_del_link_mode(ks, lp_advertising,
2068 						     Asym_Pause);
2069 		break;
2070 	}
2071 }
2072 
2073 /**
2074  * ice_get_settings_link_down - Get the Link settings when link is down
2075  * @ks: ethtool ksettings to fill in
2076  * @netdev: network interface device structure
2077  *
2078  * Reports link settings that can be determined when link is down
2079  */
2080 static void
2081 ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
2082 			   struct net_device *netdev)
2083 {
2084 	/* link is down and the driver needs to fall back on
2085 	 * supported PHY types to figure out what info to display
2086 	 */
2087 	ice_phy_type_to_ethtool(netdev, ks);
2088 
2089 	/* With no link, speed and duplex are unknown */
2090 	ks->base.speed = SPEED_UNKNOWN;
2091 	ks->base.duplex = DUPLEX_UNKNOWN;
2092 }
2093 
2094 /**
2095  * ice_get_link_ksettings - Get Link Speed and Duplex settings
2096  * @netdev: network interface device structure
2097  * @ks: ethtool ksettings
2098  *
2099  * Reports speed/duplex settings based on media_type
2100  */
2101 static int
2102 ice_get_link_ksettings(struct net_device *netdev,
2103 		       struct ethtool_link_ksettings *ks)
2104 {
2105 	struct ice_netdev_priv *np = netdev_priv(netdev);
2106 	struct ice_aqc_get_phy_caps_data *caps;
2107 	struct ice_link_status *hw_link_info;
2108 	struct ice_vsi *vsi = np->vsi;
2109 	enum ice_status status;
2110 	int err = 0;
2111 
2112 	ethtool_link_ksettings_zero_link_mode(ks, supported);
2113 	ethtool_link_ksettings_zero_link_mode(ks, advertising);
2114 	ethtool_link_ksettings_zero_link_mode(ks, lp_advertising);
2115 	hw_link_info = &vsi->port_info->phy.link_info;
2116 
2117 	/* set speed and duplex */
2118 	if (hw_link_info->link_info & ICE_AQ_LINK_UP)
2119 		ice_get_settings_link_up(ks, netdev);
2120 	else
2121 		ice_get_settings_link_down(ks, netdev);
2122 
2123 	/* set autoneg settings */
2124 	ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
2125 		AUTONEG_ENABLE : AUTONEG_DISABLE;
2126 
2127 	/* set media type settings */
2128 	switch (vsi->port_info->phy.media_type) {
2129 	case ICE_MEDIA_FIBER:
2130 		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
2131 		ks->base.port = PORT_FIBRE;
2132 		break;
2133 	case ICE_MEDIA_BASET:
2134 		ethtool_link_ksettings_add_link_mode(ks, supported, TP);
2135 		ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
2136 		ks->base.port = PORT_TP;
2137 		break;
2138 	case ICE_MEDIA_BACKPLANE:
2139 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
2140 		ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
2141 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
2142 		ethtool_link_ksettings_add_link_mode(ks, advertising,
2143 						     Backplane);
2144 		ks->base.port = PORT_NONE;
2145 		break;
2146 	case ICE_MEDIA_DA:
2147 		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
2148 		ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
2149 		ks->base.port = PORT_DA;
2150 		break;
2151 	default:
2152 		ks->base.port = PORT_OTHER;
2153 		break;
2154 	}
2155 
2156 	/* flow control is symmetric and always supported */
2157 	ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
2158 
2159 	caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
2160 	if (!caps)
2161 		return -ENOMEM;
2162 
2163 	status = ice_aq_get_phy_caps(vsi->port_info, false,
2164 				     ICE_AQC_REPORT_SW_CFG, caps, NULL);
2165 	if (status) {
2166 		err = -EIO;
2167 		goto done;
2168 	}
2169 
2170 	/* Set the advertised flow control based on the PHY capability */
2171 	if ((caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) &&
2172 	    (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)) {
2173 		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2174 		ethtool_link_ksettings_add_link_mode(ks, advertising,
2175 						     Asym_Pause);
2176 	} else if (caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) {
2177 		ethtool_link_ksettings_add_link_mode(ks, advertising,
2178 						     Asym_Pause);
2179 	} else if (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE) {
2180 		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2181 		ethtool_link_ksettings_add_link_mode(ks, advertising,
2182 						     Asym_Pause);
2183 	} else {
2184 		ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
2185 		ethtool_link_ksettings_del_link_mode(ks, advertising,
2186 						     Asym_Pause);
2187 	}
2188 
2189 	/* Set advertised FEC modes based on PHY capability */
2190 	ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_NONE);
2191 
2192 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
2193 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
2194 		ethtool_link_ksettings_add_link_mode(ks, advertising,
2195 						     FEC_BASER);
2196 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
2197 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
2198 		ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
2199 
2200 	status = ice_aq_get_phy_caps(vsi->port_info, false,
2201 				     ICE_AQC_REPORT_TOPO_CAP, caps, NULL);
2202 	if (status) {
2203 		err = -EIO;
2204 		goto done;
2205 	}
2206 
2207 	/* Set supported FEC modes based on PHY capability */
2208 	ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
2209 
2210 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
2211 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN)
2212 		ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
2213 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
2214 		ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
2215 
2216 done:
2217 	devm_kfree(&vsi->back->pdev->dev, caps);
2218 	return err;
2219 }
2220 
2221 /**
2222  * ice_ksettings_find_adv_link_speed - Find advertising link speed
2223  * @ks: ethtool ksettings
2224  */
2225 static u16
2226 ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
2227 {
2228 	u16 adv_link_speed = 0;
2229 
2230 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2231 						  100baseT_Full))
2232 		adv_link_speed |= ICE_AQ_LINK_SPEED_100MB;
2233 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2234 						  1000baseX_Full))
2235 		adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2236 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2237 						  1000baseT_Full) ||
2238 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2239 						  1000baseKX_Full))
2240 		adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2241 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2242 						  2500baseT_Full))
2243 		adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2244 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2245 						  2500baseX_Full))
2246 		adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2247 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2248 						  5000baseT_Full))
2249 		adv_link_speed |= ICE_AQ_LINK_SPEED_5GB;
2250 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2251 						  10000baseT_Full) ||
2252 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2253 						  10000baseKR_Full))
2254 		adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2255 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2256 						  10000baseSR_Full) ||
2257 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2258 						  10000baseLR_Full))
2259 		adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2260 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2261 						  25000baseCR_Full) ||
2262 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2263 						  25000baseSR_Full) ||
2264 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2265 						  25000baseKR_Full))
2266 		adv_link_speed |= ICE_AQ_LINK_SPEED_25GB;
2267 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2268 						  40000baseCR4_Full) ||
2269 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2270 						  40000baseSR4_Full) ||
2271 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2272 						  40000baseLR4_Full) ||
2273 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2274 						  40000baseKR4_Full))
2275 		adv_link_speed |= ICE_AQ_LINK_SPEED_40GB;
2276 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2277 						  50000baseCR2_Full) ||
2278 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2279 						  50000baseKR2_Full))
2280 		adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2281 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2282 						  50000baseSR2_Full))
2283 		adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2284 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2285 						  100000baseCR4_Full) ||
2286 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2287 						  100000baseSR4_Full) ||
2288 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2289 						  100000baseLR4_ER4_Full) ||
2290 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
2291 						  100000baseKR4_Full))
2292 		adv_link_speed |= ICE_AQ_LINK_SPEED_100GB;
2293 
2294 	return adv_link_speed;
2295 }
2296 
2297 /**
2298  * ice_setup_autoneg
2299  * @p: port info
2300  * @ks: ethtool_link_ksettings
2301  * @config: configuration that will be sent down to FW
2302  * @autoneg_enabled: autonegotiation is enabled or not
2303  * @autoneg_changed: will there a change in autonegotiation
2304  * @netdev: network interface device structure
2305  *
2306  * Setup PHY autonegotiation feature
2307  */
2308 static int
2309 ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
2310 		  struct ice_aqc_set_phy_cfg_data *config,
2311 		  u8 autoneg_enabled, u8 *autoneg_changed,
2312 		  struct net_device *netdev)
2313 {
2314 	int err = 0;
2315 
2316 	*autoneg_changed = 0;
2317 
2318 	/* Check autoneg */
2319 	if (autoneg_enabled == AUTONEG_ENABLE) {
2320 		/* If autoneg was not already enabled */
2321 		if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
2322 			/* If autoneg is not supported, return error */
2323 			if (!ethtool_link_ksettings_test_link_mode(ks,
2324 								   supported,
2325 								   Autoneg)) {
2326 				netdev_info(netdev, "Autoneg not supported on this phy.\n");
2327 				err = -EINVAL;
2328 			} else {
2329 				/* Autoneg is allowed to change */
2330 				config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2331 				*autoneg_changed = 1;
2332 			}
2333 		}
2334 	} else {
2335 		/* If autoneg is currently enabled */
2336 		if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
2337 			/* If autoneg is supported 10GBASE_T is the only PHY
2338 			 * that can disable it, so otherwise return error
2339 			 */
2340 			if (ethtool_link_ksettings_test_link_mode(ks,
2341 								  supported,
2342 								  Autoneg)) {
2343 				netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
2344 				err = -EINVAL;
2345 			} else {
2346 				/* Autoneg is allowed to change */
2347 				config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2348 				*autoneg_changed = 1;
2349 			}
2350 		}
2351 	}
2352 
2353 	return err;
2354 }
2355 
2356 /**
2357  * ice_set_link_ksettings - Set Speed and Duplex
2358  * @netdev: network interface device structure
2359  * @ks: ethtool ksettings
2360  *
2361  * Set speed/duplex per media_types advertised/forced
2362  */
2363 static int
2364 ice_set_link_ksettings(struct net_device *netdev,
2365 		       const struct ethtool_link_ksettings *ks)
2366 {
2367 	u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT, lport = 0;
2368 	struct ice_netdev_priv *np = netdev_priv(netdev);
2369 	struct ethtool_link_ksettings safe_ks, copy_ks;
2370 	struct ice_aqc_get_phy_caps_data *abilities;
2371 	u16 adv_link_speed, curr_link_speed, idx;
2372 	struct ice_aqc_set_phy_cfg_data config;
2373 	struct ice_pf *pf = np->vsi->back;
2374 	struct ice_port_info *p;
2375 	u8 autoneg_changed = 0;
2376 	enum ice_status status;
2377 	u64 phy_type_high;
2378 	u64 phy_type_low;
2379 	int err = 0;
2380 	bool linkup;
2381 
2382 	p = np->vsi->port_info;
2383 
2384 	if (!p)
2385 		return -EOPNOTSUPP;
2386 
2387 	/* Check if this is LAN VSI */
2388 	ice_for_each_vsi(pf, idx)
2389 		if (pf->vsi[idx]->type == ICE_VSI_PF) {
2390 			if (np->vsi != pf->vsi[idx])
2391 				return -EOPNOTSUPP;
2392 			break;
2393 		}
2394 
2395 	if (p->phy.media_type != ICE_MEDIA_BASET &&
2396 	    p->phy.media_type != ICE_MEDIA_FIBER &&
2397 	    p->phy.media_type != ICE_MEDIA_BACKPLANE &&
2398 	    p->phy.media_type != ICE_MEDIA_DA &&
2399 	    p->phy.link_info.link_info & ICE_AQ_LINK_UP)
2400 		return -EOPNOTSUPP;
2401 
2402 	/* copy the ksettings to copy_ks to avoid modifying the original */
2403 	memcpy(&copy_ks, ks, sizeof(copy_ks));
2404 
2405 	/* save autoneg out of ksettings */
2406 	autoneg = copy_ks.base.autoneg;
2407 
2408 	memset(&safe_ks, 0, sizeof(safe_ks));
2409 
2410 	/* Get link modes supported by hardware.*/
2411 	ice_phy_type_to_ethtool(netdev, &safe_ks);
2412 
2413 	/* and check against modes requested by user.
2414 	 * Return an error if unsupported mode was set.
2415 	 */
2416 	if (!bitmap_subset(copy_ks.link_modes.advertising,
2417 			   safe_ks.link_modes.supported,
2418 			   __ETHTOOL_LINK_MODE_MASK_NBITS))
2419 		return -EINVAL;
2420 
2421 	/* get our own copy of the bits to check against */
2422 	memset(&safe_ks, 0, sizeof(safe_ks));
2423 	safe_ks.base.cmd = copy_ks.base.cmd;
2424 	safe_ks.base.link_mode_masks_nwords =
2425 		copy_ks.base.link_mode_masks_nwords;
2426 	ice_get_link_ksettings(netdev, &safe_ks);
2427 
2428 	/* set autoneg back to what it currently is */
2429 	copy_ks.base.autoneg = safe_ks.base.autoneg;
2430 	/* we don't compare the speed */
2431 	copy_ks.base.speed = safe_ks.base.speed;
2432 
2433 	/* If copy_ks.base and safe_ks.base are not the same now, then they are
2434 	 * trying to set something that we do not support.
2435 	 */
2436 	if (memcmp(&copy_ks.base, &safe_ks.base, sizeof(copy_ks.base)))
2437 		return -EOPNOTSUPP;
2438 
2439 	while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
2440 		timeout--;
2441 		if (!timeout)
2442 			return -EBUSY;
2443 		usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
2444 	}
2445 
2446 	abilities = devm_kzalloc(&pf->pdev->dev, sizeof(*abilities),
2447 				 GFP_KERNEL);
2448 	if (!abilities)
2449 		return -ENOMEM;
2450 
2451 	/* Get the current PHY config */
2452 	status = ice_aq_get_phy_caps(p, false, ICE_AQC_REPORT_SW_CFG, abilities,
2453 				     NULL);
2454 	if (status) {
2455 		err = -EAGAIN;
2456 		goto done;
2457 	}
2458 
2459 	/* Copy abilities to config in case autoneg is not set below */
2460 	memset(&config, 0, sizeof(config));
2461 	config.caps = abilities->caps & ~ICE_AQC_PHY_AN_MODE;
2462 	if (abilities->caps & ICE_AQC_PHY_AN_MODE)
2463 		config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2464 
2465 	/* Check autoneg */
2466 	err = ice_setup_autoneg(p, &safe_ks, &config, autoneg, &autoneg_changed,
2467 				netdev);
2468 
2469 	if (err)
2470 		goto done;
2471 
2472 	/* Call to get the current link speed */
2473 	p->phy.get_link_info = true;
2474 	status = ice_get_link_status(p, &linkup);
2475 	if (status) {
2476 		err = -EAGAIN;
2477 		goto done;
2478 	}
2479 
2480 	curr_link_speed = p->phy.link_info.link_speed;
2481 	adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
2482 
2483 	/* If speed didn't get set, set it to what it currently is.
2484 	 * This is needed because if advertise is 0 (as it is when autoneg
2485 	 * is disabled) then speed won't get set.
2486 	 */
2487 	if (!adv_link_speed)
2488 		adv_link_speed = curr_link_speed;
2489 
2490 	/* Convert the advertise link speeds to their corresponded PHY_TYPE */
2491 	ice_update_phy_type(&phy_type_low, &phy_type_high, adv_link_speed);
2492 
2493 	if (!autoneg_changed && adv_link_speed == curr_link_speed) {
2494 		netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
2495 		goto done;
2496 	}
2497 
2498 	/* copy over the rest of the abilities */
2499 	config.low_power_ctrl = abilities->low_power_ctrl;
2500 	config.eee_cap = abilities->eee_cap;
2501 	config.eeer_value = abilities->eeer_value;
2502 	config.link_fec_opt = abilities->link_fec_options;
2503 
2504 	/* save the requested speeds */
2505 	p->phy.link_info.req_speeds = adv_link_speed;
2506 
2507 	/* set link and auto negotiation so changes take effect */
2508 	config.caps |= ICE_AQ_PHY_ENA_LINK;
2509 
2510 	if (phy_type_low || phy_type_high) {
2511 		config.phy_type_high = cpu_to_le64(phy_type_high) &
2512 			abilities->phy_type_high;
2513 		config.phy_type_low = cpu_to_le64(phy_type_low) &
2514 			abilities->phy_type_low;
2515 	} else {
2516 		err = -EAGAIN;
2517 		netdev_info(netdev, "Nothing changed. No PHY_TYPE is corresponded to advertised link speed.\n");
2518 		goto done;
2519 	}
2520 
2521 	/* If link is up put link down */
2522 	if (p->phy.link_info.link_info & ICE_AQ_LINK_UP) {
2523 		/* Tell the OS link is going down, the link will go
2524 		 * back up when fw says it is ready asynchronously
2525 		 */
2526 		ice_print_link_msg(np->vsi, false);
2527 		netif_carrier_off(netdev);
2528 		netif_tx_stop_all_queues(netdev);
2529 	}
2530 
2531 	/* make the aq call */
2532 	status = ice_aq_set_phy_cfg(&pf->hw, lport, &config, NULL);
2533 	if (status) {
2534 		netdev_info(netdev, "Set phy config failed,\n");
2535 		err = -EAGAIN;
2536 	}
2537 
2538 done:
2539 	devm_kfree(&pf->pdev->dev, abilities);
2540 	clear_bit(__ICE_CFG_BUSY, pf->state);
2541 
2542 	return err;
2543 }
2544 
2545 /**
2546  * ice_get_rxnfc - command to get Rx flow classification rules
2547  * @netdev: network interface device structure
2548  * @cmd: ethtool rxnfc command
2549  * @rule_locs: buffer to rturn Rx flow classification rules
2550  *
2551  * Returns Success if the command is supported.
2552  */
2553 static int
2554 ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2555 	      u32 __always_unused *rule_locs)
2556 {
2557 	struct ice_netdev_priv *np = netdev_priv(netdev);
2558 	struct ice_vsi *vsi = np->vsi;
2559 	int ret = -EOPNOTSUPP;
2560 
2561 	switch (cmd->cmd) {
2562 	case ETHTOOL_GRXRINGS:
2563 		cmd->data = vsi->rss_size;
2564 		ret = 0;
2565 		break;
2566 	default:
2567 		break;
2568 	}
2569 
2570 	return ret;
2571 }
2572 
2573 static void
2574 ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2575 {
2576 	struct ice_netdev_priv *np = netdev_priv(netdev);
2577 	struct ice_vsi *vsi = np->vsi;
2578 
2579 	ring->rx_max_pending = ICE_MAX_NUM_DESC;
2580 	ring->tx_max_pending = ICE_MAX_NUM_DESC;
2581 	ring->rx_pending = vsi->rx_rings[0]->count;
2582 	ring->tx_pending = vsi->tx_rings[0]->count;
2583 
2584 	/* Rx mini and jumbo rings are not supported */
2585 	ring->rx_mini_max_pending = 0;
2586 	ring->rx_jumbo_max_pending = 0;
2587 	ring->rx_mini_pending = 0;
2588 	ring->rx_jumbo_pending = 0;
2589 }
2590 
2591 static int
2592 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2593 {
2594 	struct ice_ring *tx_rings = NULL, *rx_rings = NULL;
2595 	struct ice_netdev_priv *np = netdev_priv(netdev);
2596 	struct ice_vsi *vsi = np->vsi;
2597 	struct ice_pf *pf = vsi->back;
2598 	int i, timeout = 50, err = 0;
2599 	u32 new_rx_cnt, new_tx_cnt;
2600 
2601 	if (ring->tx_pending > ICE_MAX_NUM_DESC ||
2602 	    ring->tx_pending < ICE_MIN_NUM_DESC ||
2603 	    ring->rx_pending > ICE_MAX_NUM_DESC ||
2604 	    ring->rx_pending < ICE_MIN_NUM_DESC) {
2605 		netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
2606 			   ring->tx_pending, ring->rx_pending,
2607 			   ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
2608 			   ICE_REQ_DESC_MULTIPLE);
2609 		return -EINVAL;
2610 	}
2611 
2612 	new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
2613 	if (new_tx_cnt != ring->tx_pending)
2614 		netdev_info(netdev,
2615 			    "Requested Tx descriptor count rounded up to %d\n",
2616 			    new_tx_cnt);
2617 	new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
2618 	if (new_rx_cnt != ring->rx_pending)
2619 		netdev_info(netdev,
2620 			    "Requested Rx descriptor count rounded up to %d\n",
2621 			    new_rx_cnt);
2622 
2623 	/* if nothing to do return success */
2624 	if (new_tx_cnt == vsi->tx_rings[0]->count &&
2625 	    new_rx_cnt == vsi->rx_rings[0]->count) {
2626 		netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
2627 		return 0;
2628 	}
2629 
2630 	while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
2631 		timeout--;
2632 		if (!timeout)
2633 			return -EBUSY;
2634 		usleep_range(1000, 2000);
2635 	}
2636 
2637 	/* set for the next time the netdev is started */
2638 	if (!netif_running(vsi->netdev)) {
2639 		for (i = 0; i < vsi->alloc_txq; i++)
2640 			vsi->tx_rings[i]->count = new_tx_cnt;
2641 		for (i = 0; i < vsi->alloc_rxq; i++)
2642 			vsi->rx_rings[i]->count = new_rx_cnt;
2643 		netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
2644 		goto done;
2645 	}
2646 
2647 	if (new_tx_cnt == vsi->tx_rings[0]->count)
2648 		goto process_rx;
2649 
2650 	/* alloc updated Tx resources */
2651 	netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
2652 		    vsi->tx_rings[0]->count, new_tx_cnt);
2653 
2654 	tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
2655 				sizeof(*tx_rings), GFP_KERNEL);
2656 	if (!tx_rings) {
2657 		err = -ENOMEM;
2658 		goto done;
2659 	}
2660 
2661 	for (i = 0; i < vsi->alloc_txq; i++) {
2662 		/* clone ring and setup updated count */
2663 		tx_rings[i] = *vsi->tx_rings[i];
2664 		tx_rings[i].count = new_tx_cnt;
2665 		tx_rings[i].desc = NULL;
2666 		tx_rings[i].tx_buf = NULL;
2667 		err = ice_setup_tx_ring(&tx_rings[i]);
2668 		if (err) {
2669 			while (i) {
2670 				i--;
2671 				ice_clean_tx_ring(&tx_rings[i]);
2672 			}
2673 			devm_kfree(&pf->pdev->dev, tx_rings);
2674 			goto done;
2675 		}
2676 	}
2677 
2678 process_rx:
2679 	if (new_rx_cnt == vsi->rx_rings[0]->count)
2680 		goto process_link;
2681 
2682 	/* alloc updated Rx resources */
2683 	netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
2684 		    vsi->rx_rings[0]->count, new_rx_cnt);
2685 
2686 	rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
2687 				sizeof(*rx_rings), GFP_KERNEL);
2688 	if (!rx_rings) {
2689 		err = -ENOMEM;
2690 		goto done;
2691 	}
2692 
2693 	for (i = 0; i < vsi->alloc_rxq; i++) {
2694 		/* clone ring and setup updated count */
2695 		rx_rings[i] = *vsi->rx_rings[i];
2696 		rx_rings[i].count = new_rx_cnt;
2697 		rx_rings[i].desc = NULL;
2698 		rx_rings[i].rx_buf = NULL;
2699 		/* this is to allow wr32 to have something to write to
2700 		 * during early allocation of Rx buffers
2701 		 */
2702 		rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
2703 
2704 		err = ice_setup_rx_ring(&rx_rings[i]);
2705 		if (err)
2706 			goto rx_unwind;
2707 
2708 		/* allocate Rx buffers */
2709 		err = ice_alloc_rx_bufs(&rx_rings[i],
2710 					ICE_DESC_UNUSED(&rx_rings[i]));
2711 rx_unwind:
2712 		if (err) {
2713 			while (i) {
2714 				i--;
2715 				ice_free_rx_ring(&rx_rings[i]);
2716 			}
2717 			devm_kfree(&pf->pdev->dev, rx_rings);
2718 			err = -ENOMEM;
2719 			goto free_tx;
2720 		}
2721 	}
2722 
2723 process_link:
2724 	/* Bring interface down, copy in the new ring info, then restore the
2725 	 * interface. if VSI is up, bring it down and then back up
2726 	 */
2727 	if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
2728 		ice_down(vsi);
2729 
2730 		if (tx_rings) {
2731 			for (i = 0; i < vsi->alloc_txq; i++) {
2732 				ice_free_tx_ring(vsi->tx_rings[i]);
2733 				*vsi->tx_rings[i] = tx_rings[i];
2734 			}
2735 			devm_kfree(&pf->pdev->dev, tx_rings);
2736 		}
2737 
2738 		if (rx_rings) {
2739 			for (i = 0; i < vsi->alloc_rxq; i++) {
2740 				ice_free_rx_ring(vsi->rx_rings[i]);
2741 				/* copy the real tail offset */
2742 				rx_rings[i].tail = vsi->rx_rings[i]->tail;
2743 				/* this is to fake out the allocation routine
2744 				 * into thinking it has to realloc everything
2745 				 * but the recycling logic will let us re-use
2746 				 * the buffers allocated above
2747 				 */
2748 				rx_rings[i].next_to_use = 0;
2749 				rx_rings[i].next_to_clean = 0;
2750 				rx_rings[i].next_to_alloc = 0;
2751 				*vsi->rx_rings[i] = rx_rings[i];
2752 			}
2753 			devm_kfree(&pf->pdev->dev, rx_rings);
2754 		}
2755 
2756 		ice_up(vsi);
2757 	}
2758 	goto done;
2759 
2760 free_tx:
2761 	/* error cleanup if the Rx allocations failed after getting Tx */
2762 	if (tx_rings) {
2763 		for (i = 0; i < vsi->alloc_txq; i++)
2764 			ice_free_tx_ring(&tx_rings[i]);
2765 		devm_kfree(&pf->pdev->dev, tx_rings);
2766 	}
2767 
2768 done:
2769 	clear_bit(__ICE_CFG_BUSY, pf->state);
2770 	return err;
2771 }
2772 
2773 static int ice_nway_reset(struct net_device *netdev)
2774 {
2775 	/* restart autonegotiation */
2776 	struct ice_netdev_priv *np = netdev_priv(netdev);
2777 	struct ice_vsi *vsi = np->vsi;
2778 	struct ice_port_info *pi;
2779 	enum ice_status status;
2780 
2781 	pi = vsi->port_info;
2782 	/* If VSI state is up, then restart autoneg with link up */
2783 	if (!test_bit(__ICE_DOWN, vsi->back->state))
2784 		status = ice_aq_set_link_restart_an(pi, true, NULL);
2785 	else
2786 		status = ice_aq_set_link_restart_an(pi, false, NULL);
2787 
2788 	if (status) {
2789 		netdev_info(netdev, "link restart failed, err %d aq_err %d\n",
2790 			    status, pi->hw->adminq.sq_last_status);
2791 		return -EIO;
2792 	}
2793 
2794 	return 0;
2795 }
2796 
2797 /**
2798  * ice_get_pauseparam - Get Flow Control status
2799  * @netdev: network interface device structure
2800  * @pause: ethernet pause (flow control) parameters
2801  *
2802  * Get requested flow control status from PHY capability.
2803  * If autoneg is true, then ethtool will send the ETHTOOL_GSET ioctl which
2804  * is handled by ice_get_link_ksettings. ice_get_link_ksettings will report
2805  * the negotiated Rx/Tx pause via lp_advertising.
2806  */
2807 static void
2808 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2809 {
2810 	struct ice_netdev_priv *np = netdev_priv(netdev);
2811 	struct ice_port_info *pi = np->vsi->port_info;
2812 	struct ice_aqc_get_phy_caps_data *pcaps;
2813 	struct ice_vsi *vsi = np->vsi;
2814 	struct ice_dcbx_cfg *dcbx_cfg;
2815 	enum ice_status status;
2816 
2817 	/* Initialize pause params */
2818 	pause->rx_pause = 0;
2819 	pause->tx_pause = 0;
2820 
2821 	dcbx_cfg = &pi->local_dcbx_cfg;
2822 
2823 	pcaps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*pcaps),
2824 			     GFP_KERNEL);
2825 	if (!pcaps)
2826 		return;
2827 
2828 	/* Get current PHY config */
2829 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
2830 				     NULL);
2831 	if (status)
2832 		goto out;
2833 
2834 	pause->autoneg = ((pcaps->caps & ICE_AQC_PHY_AN_MODE) ?
2835 			AUTONEG_ENABLE : AUTONEG_DISABLE);
2836 
2837 	if (dcbx_cfg->pfc.pfcena)
2838 		/* PFC enabled so report LFC as off */
2839 		goto out;
2840 
2841 	if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
2842 		pause->tx_pause = 1;
2843 	if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
2844 		pause->rx_pause = 1;
2845 
2846 out:
2847 	devm_kfree(&vsi->back->pdev->dev, pcaps);
2848 }
2849 
2850 /**
2851  * ice_set_pauseparam - Set Flow Control parameter
2852  * @netdev: network interface device structure
2853  * @pause: return Tx/Rx flow control status
2854  */
2855 static int
2856 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2857 {
2858 	struct ice_netdev_priv *np = netdev_priv(netdev);
2859 	struct ice_link_status *hw_link_info;
2860 	struct ice_pf *pf = np->vsi->back;
2861 	struct ice_dcbx_cfg *dcbx_cfg;
2862 	struct ice_vsi *vsi = np->vsi;
2863 	struct ice_hw *hw = &pf->hw;
2864 	struct ice_port_info *pi;
2865 	enum ice_status status;
2866 	u8 aq_failures;
2867 	bool link_up;
2868 	int err = 0;
2869 
2870 	pi = vsi->port_info;
2871 	hw_link_info = &pi->phy.link_info;
2872 	dcbx_cfg = &pi->local_dcbx_cfg;
2873 	link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
2874 
2875 	/* Changing the port's flow control is not supported if this isn't the
2876 	 * PF VSI
2877 	 */
2878 	if (vsi->type != ICE_VSI_PF) {
2879 		netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
2880 		return -EOPNOTSUPP;
2881 	}
2882 
2883 	if (pause->autoneg != (hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
2884 		netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
2885 		return -EOPNOTSUPP;
2886 	}
2887 
2888 	/* If we have link and don't have autoneg */
2889 	if (!test_bit(__ICE_DOWN, pf->state) &&
2890 	    !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
2891 		/* Send message that it might not necessarily work*/
2892 		netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
2893 	}
2894 
2895 	if (dcbx_cfg->pfc.pfcena) {
2896 		netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
2897 		return -EOPNOTSUPP;
2898 	}
2899 	if (pause->rx_pause && pause->tx_pause)
2900 		pi->fc.req_mode = ICE_FC_FULL;
2901 	else if (pause->rx_pause && !pause->tx_pause)
2902 		pi->fc.req_mode = ICE_FC_RX_PAUSE;
2903 	else if (!pause->rx_pause && pause->tx_pause)
2904 		pi->fc.req_mode = ICE_FC_TX_PAUSE;
2905 	else if (!pause->rx_pause && !pause->tx_pause)
2906 		pi->fc.req_mode = ICE_FC_NONE;
2907 	else
2908 		return -EINVAL;
2909 
2910 	/* Tell the OS link is going down, the link will go back up when fw
2911 	 * says it is ready asynchronously
2912 	 */
2913 	ice_print_link_msg(vsi, false);
2914 	netif_carrier_off(netdev);
2915 	netif_tx_stop_all_queues(netdev);
2916 
2917 	/* Set the FC mode and only restart AN if link is up */
2918 	status = ice_set_fc(pi, &aq_failures, link_up);
2919 
2920 	if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
2921 		netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %d\n",
2922 			    status, hw->adminq.sq_last_status);
2923 		err = -EAGAIN;
2924 	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
2925 		netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %d\n",
2926 			    status, hw->adminq.sq_last_status);
2927 		err = -EAGAIN;
2928 	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
2929 		netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %d\n",
2930 			    status, hw->adminq.sq_last_status);
2931 		err = -EAGAIN;
2932 	}
2933 
2934 	if (!test_bit(__ICE_DOWN, pf->state)) {
2935 		/* Give it a little more time to try to come back. If still
2936 		 * down, restart autoneg link or reinitialize the interface.
2937 		 */
2938 		msleep(75);
2939 		if (!test_bit(__ICE_DOWN, pf->state))
2940 			return ice_nway_reset(netdev);
2941 
2942 		ice_down(vsi);
2943 		ice_up(vsi);
2944 	}
2945 
2946 	return err;
2947 }
2948 
2949 /**
2950  * ice_get_rxfh_key_size - get the RSS hash key size
2951  * @netdev: network interface device structure
2952  *
2953  * Returns the table size.
2954  */
2955 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
2956 {
2957 	return ICE_VSIQF_HKEY_ARRAY_SIZE;
2958 }
2959 
2960 /**
2961  * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
2962  * @netdev: network interface device structure
2963  *
2964  * Returns the table size.
2965  */
2966 static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
2967 {
2968 	struct ice_netdev_priv *np = netdev_priv(netdev);
2969 
2970 	return np->vsi->rss_table_size;
2971 }
2972 
2973 /**
2974  * ice_get_rxfh - get the Rx flow hash indirection table
2975  * @netdev: network interface device structure
2976  * @indir: indirection table
2977  * @key: hash key
2978  * @hfunc: hash function
2979  *
2980  * Reads the indirection table directly from the hardware.
2981  */
2982 static int
2983 ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
2984 {
2985 	struct ice_netdev_priv *np = netdev_priv(netdev);
2986 	struct ice_vsi *vsi = np->vsi;
2987 	struct ice_pf *pf = vsi->back;
2988 	int ret = 0, i;
2989 	u8 *lut;
2990 
2991 	if (hfunc)
2992 		*hfunc = ETH_RSS_HASH_TOP;
2993 
2994 	if (!indir)
2995 		return 0;
2996 
2997 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2998 		/* RSS not supported return error here */
2999 		netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3000 		return -EIO;
3001 	}
3002 
3003 	lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
3004 	if (!lut)
3005 		return -ENOMEM;
3006 
3007 	if (ice_get_rss(vsi, key, lut, vsi->rss_table_size)) {
3008 		ret = -EIO;
3009 		goto out;
3010 	}
3011 
3012 	for (i = 0; i < vsi->rss_table_size; i++)
3013 		indir[i] = (u32)(lut[i]);
3014 
3015 out:
3016 	devm_kfree(&pf->pdev->dev, lut);
3017 	return ret;
3018 }
3019 
3020 /**
3021  * ice_set_rxfh - set the Rx flow hash indirection table
3022  * @netdev: network interface device structure
3023  * @indir: indirection table
3024  * @key: hash key
3025  * @hfunc: hash function
3026  *
3027  * Returns -EINVAL if the table specifies an invalid queue ID, otherwise
3028  * returns 0 after programming the table.
3029  */
3030 static int
3031 ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key,
3032 	     const u8 hfunc)
3033 {
3034 	struct ice_netdev_priv *np = netdev_priv(netdev);
3035 	struct ice_vsi *vsi = np->vsi;
3036 	struct ice_pf *pf = vsi->back;
3037 	u8 *seed = NULL;
3038 
3039 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3040 		return -EOPNOTSUPP;
3041 
3042 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3043 		/* RSS not supported return error here */
3044 		netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3045 		return -EIO;
3046 	}
3047 
3048 	if (key) {
3049 		if (!vsi->rss_hkey_user) {
3050 			vsi->rss_hkey_user =
3051 				devm_kzalloc(&pf->pdev->dev,
3052 					     ICE_VSIQF_HKEY_ARRAY_SIZE,
3053 					     GFP_KERNEL);
3054 			if (!vsi->rss_hkey_user)
3055 				return -ENOMEM;
3056 		}
3057 		memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
3058 		seed = vsi->rss_hkey_user;
3059 	}
3060 
3061 	if (!vsi->rss_lut_user) {
3062 		vsi->rss_lut_user = devm_kzalloc(&pf->pdev->dev,
3063 						 vsi->rss_table_size,
3064 						 GFP_KERNEL);
3065 		if (!vsi->rss_lut_user)
3066 			return -ENOMEM;
3067 	}
3068 
3069 	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
3070 	if (indir) {
3071 		int i;
3072 
3073 		for (i = 0; i < vsi->rss_table_size; i++)
3074 			vsi->rss_lut_user[i] = (u8)(indir[i]);
3075 	} else {
3076 		ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
3077 				 vsi->rss_size);
3078 	}
3079 
3080 	if (ice_set_rss(vsi, seed, vsi->rss_lut_user, vsi->rss_table_size))
3081 		return -EIO;
3082 
3083 	return 0;
3084 }
3085 
3086 enum ice_container_type {
3087 	ICE_RX_CONTAINER,
3088 	ICE_TX_CONTAINER,
3089 };
3090 
3091 /**
3092  * ice_get_rc_coalesce - get ITR values for specific ring container
3093  * @ec: ethtool structure to fill with driver's coalesce settings
3094  * @c_type: container type, Rx or Tx
3095  * @rc: ring container that the ITR values will come from
3096  *
3097  * Query the device for ice_ring_container specific ITR values. This is
3098  * done per ice_ring_container because each q_vector can have 1 or more rings
3099  * and all of said ring(s) will have the same ITR values.
3100  *
3101  * Returns 0 on success, negative otherwise.
3102  */
3103 static int
3104 ice_get_rc_coalesce(struct ethtool_coalesce *ec, enum ice_container_type c_type,
3105 		    struct ice_ring_container *rc)
3106 {
3107 	struct ice_pf *pf;
3108 
3109 	if (!rc->ring)
3110 		return -EINVAL;
3111 
3112 	pf = rc->ring->vsi->back;
3113 
3114 	switch (c_type) {
3115 	case ICE_RX_CONTAINER:
3116 		ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
3117 		ec->rx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3118 		ec->rx_coalesce_usecs_high = rc->ring->q_vector->intrl;
3119 		break;
3120 	case ICE_TX_CONTAINER:
3121 		ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
3122 		ec->tx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3123 		break;
3124 	default:
3125 		dev_dbg(&pf->pdev->dev, "Invalid c_type %d\n", c_type);
3126 		return -EINVAL;
3127 	}
3128 
3129 	return 0;
3130 }
3131 
3132 /**
3133  * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings
3134  * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings
3135  * @ec: coalesce settings to program the device with
3136  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3137  *
3138  * Return 0 on success, and negative under the following conditions:
3139  * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed.
3140  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3141  */
3142 static int
3143 ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3144 {
3145 	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3146 		if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
3147 					&vsi->rx_rings[q_num]->q_vector->rx))
3148 			return -EINVAL;
3149 		if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
3150 					&vsi->tx_rings[q_num]->q_vector->tx))
3151 			return -EINVAL;
3152 	} else if (q_num < vsi->num_rxq) {
3153 		if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
3154 					&vsi->rx_rings[q_num]->q_vector->rx))
3155 			return -EINVAL;
3156 	} else if (q_num < vsi->num_txq) {
3157 		if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
3158 					&vsi->tx_rings[q_num]->q_vector->tx))
3159 			return -EINVAL;
3160 	} else {
3161 		return -EINVAL;
3162 	}
3163 
3164 	return 0;
3165 }
3166 
3167 /**
3168  * __ice_get_coalesce - get ITR/INTRL values for the device
3169  * @netdev: pointer to the netdev associated with this query
3170  * @ec: ethtool structure to fill with driver's coalesce settings
3171  * @q_num: queue number to get the coalesce settings for
3172  *
3173  * If the caller passes in a negative q_num then we return coalesce settings
3174  * based on queue number 0, else use the actual q_num passed in.
3175  */
3176 static int
3177 __ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3178 		   int q_num)
3179 {
3180 	struct ice_netdev_priv *np = netdev_priv(netdev);
3181 	struct ice_vsi *vsi = np->vsi;
3182 
3183 	if (q_num < 0)
3184 		q_num = 0;
3185 
3186 	if (ice_get_q_coalesce(vsi, ec, q_num))
3187 		return -EINVAL;
3188 
3189 	if (q_num < vsi->num_txq)
3190 		ec->tx_max_coalesced_frames_irq = vsi->work_lmt;
3191 
3192 	if (q_num < vsi->num_rxq)
3193 		ec->rx_max_coalesced_frames_irq = vsi->work_lmt;
3194 
3195 	return 0;
3196 }
3197 
3198 static int
3199 ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
3200 {
3201 	return __ice_get_coalesce(netdev, ec, -1);
3202 }
3203 
3204 static int
3205 ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
3206 		       struct ethtool_coalesce *ec)
3207 {
3208 	return __ice_get_coalesce(netdev, ec, q_num);
3209 }
3210 
3211 /**
3212  * ice_set_rc_coalesce - set ITR values for specific ring container
3213  * @c_type: container type, Rx or Tx
3214  * @ec: ethtool structure from user to update ITR settings
3215  * @rc: ring container that the ITR values will come from
3216  * @vsi: VSI associated to the ring container
3217  *
3218  * Set specific ITR values. This is done per ice_ring_container because each
3219  * q_vector can have 1 or more rings and all of said ring(s) will have the same
3220  * ITR values.
3221  *
3222  * Returns 0 on success, negative otherwise.
3223  */
3224 static int
3225 ice_set_rc_coalesce(enum ice_container_type c_type, struct ethtool_coalesce *ec,
3226 		    struct ice_ring_container *rc, struct ice_vsi *vsi)
3227 {
3228 	struct ice_pf *pf = vsi->back;
3229 	u16 itr_setting;
3230 
3231 	if (!rc->ring)
3232 		return -EINVAL;
3233 
3234 	itr_setting = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3235 
3236 	switch (c_type) {
3237 	case ICE_RX_CONTAINER:
3238 		if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL ||
3239 		    (ec->rx_coalesce_usecs_high &&
3240 		     ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) {
3241 			netdev_info(vsi->netdev,
3242 				    "Invalid value, rx-usecs-high valid values are 0 (disabled), %d-%d\n",
3243 				    pf->hw.intrl_gran, ICE_MAX_INTRL);
3244 			return -EINVAL;
3245 		}
3246 
3247 		if (ec->rx_coalesce_usecs_high != rc->ring->q_vector->intrl) {
3248 			rc->ring->q_vector->intrl = ec->rx_coalesce_usecs_high;
3249 			wr32(&pf->hw, GLINT_RATE(rc->ring->q_vector->reg_idx),
3250 			     ice_intrl_usec_to_reg(ec->rx_coalesce_usecs_high,
3251 						   pf->hw.intrl_gran));
3252 		}
3253 
3254 		if (ec->rx_coalesce_usecs != itr_setting &&
3255 		    ec->use_adaptive_rx_coalesce) {
3256 			netdev_info(vsi->netdev,
3257 				    "Rx interrupt throttling cannot be changed if adaptive-rx is enabled\n");
3258 			return -EINVAL;
3259 		}
3260 
3261 		if (ec->rx_coalesce_usecs > ICE_ITR_MAX) {
3262 			netdev_info(vsi->netdev,
3263 				    "Invalid value, rx-usecs range is 0-%d\n",
3264 				   ICE_ITR_MAX);
3265 			return -EINVAL;
3266 		}
3267 
3268 		if (ec->use_adaptive_rx_coalesce) {
3269 			rc->itr_setting |= ICE_ITR_DYNAMIC;
3270 		} else {
3271 			rc->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
3272 			rc->target_itr = ITR_TO_REG(rc->itr_setting);
3273 		}
3274 		break;
3275 	case ICE_TX_CONTAINER:
3276 		if (ec->tx_coalesce_usecs_high) {
3277 			netdev_info(vsi->netdev,
3278 				    "setting tx-usecs-high is not supported\n");
3279 			return -EINVAL;
3280 		}
3281 
3282 		if (ec->tx_coalesce_usecs != itr_setting &&
3283 		    ec->use_adaptive_tx_coalesce) {
3284 			netdev_info(vsi->netdev,
3285 				    "Tx interrupt throttling cannot be changed if adaptive-tx is enabled\n");
3286 			return -EINVAL;
3287 		}
3288 
3289 		if (ec->tx_coalesce_usecs > ICE_ITR_MAX) {
3290 			netdev_info(vsi->netdev,
3291 				    "Invalid value, tx-usecs range is 0-%d\n",
3292 				   ICE_ITR_MAX);
3293 			return -EINVAL;
3294 		}
3295 
3296 		if (ec->use_adaptive_tx_coalesce) {
3297 			rc->itr_setting |= ICE_ITR_DYNAMIC;
3298 		} else {
3299 			rc->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
3300 			rc->target_itr = ITR_TO_REG(rc->itr_setting);
3301 		}
3302 		break;
3303 	default:
3304 		dev_dbg(&pf->pdev->dev, "Invalid container type %d\n", c_type);
3305 		return -EINVAL;
3306 	}
3307 
3308 	return 0;
3309 }
3310 
3311 /**
3312  * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings
3313  * @vsi: VSI associated to the queue that need updating
3314  * @ec: coalesce settings to program the device with
3315  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3316  *
3317  * Return 0 on success, and negative under the following conditions:
3318  * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed.
3319  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3320  */
3321 static int
3322 ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3323 {
3324 	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3325 		if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
3326 					&vsi->rx_rings[q_num]->q_vector->rx,
3327 					vsi))
3328 			return -EINVAL;
3329 
3330 		if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
3331 					&vsi->tx_rings[q_num]->q_vector->tx,
3332 					vsi))
3333 			return -EINVAL;
3334 	} else if (q_num < vsi->num_rxq) {
3335 		if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
3336 					&vsi->rx_rings[q_num]->q_vector->rx,
3337 					vsi))
3338 			return -EINVAL;
3339 	} else if (q_num < vsi->num_txq) {
3340 		if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
3341 					&vsi->tx_rings[q_num]->q_vector->tx,
3342 					vsi))
3343 			return -EINVAL;
3344 	} else {
3345 		return -EINVAL;
3346 	}
3347 
3348 	return 0;
3349 }
3350 
3351 /**
3352  * __ice_set_coalesce - set ITR/INTRL values for the device
3353  * @netdev: pointer to the netdev associated with this query
3354  * @ec: ethtool structure to fill with driver's coalesce settings
3355  * @q_num: queue number to get the coalesce settings for
3356  *
3357  * If the caller passes in a negative q_num then we set the coalesce settings
3358  * for all Tx/Rx queues, else use the actual q_num passed in.
3359  */
3360 static int
3361 __ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3362 		   int q_num)
3363 {
3364 	struct ice_netdev_priv *np = netdev_priv(netdev);
3365 	struct ice_vsi *vsi = np->vsi;
3366 
3367 	if (q_num < 0) {
3368 		int i;
3369 
3370 		ice_for_each_q_vector(vsi, i) {
3371 			if (ice_set_q_coalesce(vsi, ec, i))
3372 				return -EINVAL;
3373 		}
3374 		goto set_work_lmt;
3375 	}
3376 
3377 	if (ice_set_q_coalesce(vsi, ec, q_num))
3378 		return -EINVAL;
3379 
3380 set_work_lmt:
3381 
3382 	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
3383 		vsi->work_lmt = max(ec->tx_max_coalesced_frames_irq,
3384 				    ec->rx_max_coalesced_frames_irq);
3385 
3386 	return 0;
3387 }
3388 
3389 static int
3390 ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
3391 {
3392 	return __ice_set_coalesce(netdev, ec, -1);
3393 }
3394 
3395 static int
3396 ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
3397 		       struct ethtool_coalesce *ec)
3398 {
3399 	return __ice_set_coalesce(netdev, ec, q_num);
3400 }
3401 
3402 static const struct ethtool_ops ice_ethtool_ops = {
3403 	.get_link_ksettings	= ice_get_link_ksettings,
3404 	.set_link_ksettings	= ice_set_link_ksettings,
3405 	.get_drvinfo            = ice_get_drvinfo,
3406 	.get_regs_len           = ice_get_regs_len,
3407 	.get_regs               = ice_get_regs,
3408 	.get_msglevel           = ice_get_msglevel,
3409 	.set_msglevel           = ice_set_msglevel,
3410 	.self_test		= ice_self_test,
3411 	.get_link		= ethtool_op_get_link,
3412 	.get_eeprom_len		= ice_get_eeprom_len,
3413 	.get_eeprom		= ice_get_eeprom,
3414 	.get_coalesce		= ice_get_coalesce,
3415 	.set_coalesce		= ice_set_coalesce,
3416 	.get_strings		= ice_get_strings,
3417 	.set_phys_id		= ice_set_phys_id,
3418 	.get_ethtool_stats      = ice_get_ethtool_stats,
3419 	.get_priv_flags		= ice_get_priv_flags,
3420 	.set_priv_flags		= ice_set_priv_flags,
3421 	.get_sset_count		= ice_get_sset_count,
3422 	.get_rxnfc		= ice_get_rxnfc,
3423 	.get_ringparam		= ice_get_ringparam,
3424 	.set_ringparam		= ice_set_ringparam,
3425 	.nway_reset		= ice_nway_reset,
3426 	.get_pauseparam		= ice_get_pauseparam,
3427 	.set_pauseparam		= ice_set_pauseparam,
3428 	.get_rxfh_key_size	= ice_get_rxfh_key_size,
3429 	.get_rxfh_indir_size	= ice_get_rxfh_indir_size,
3430 	.get_rxfh		= ice_get_rxfh,
3431 	.set_rxfh		= ice_set_rxfh,
3432 	.get_ts_info		= ethtool_op_get_ts_info,
3433 	.get_per_queue_coalesce = ice_get_per_q_coalesce,
3434 	.set_per_queue_coalesce = ice_set_per_q_coalesce,
3435 	.get_fecparam		= ice_get_fecparam,
3436 	.set_fecparam		= ice_set_fecparam,
3437 };
3438 
3439 /**
3440  * ice_set_ethtool_ops - setup netdev ethtool ops
3441  * @netdev: network interface device structure
3442  *
3443  * setup netdev ethtool ops with ice specific ops
3444  */
3445 void ice_set_ethtool_ops(struct net_device *netdev)
3446 {
3447 	netdev->ethtool_ops = &ice_ethtool_ops;
3448 }
3449