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