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