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