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