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