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