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