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