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