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