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