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