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