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