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