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