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 
8 struct ice_stats {
9 	char stat_string[ETH_GSTRING_LEN];
10 	int sizeof_stat;
11 	int stat_offset;
12 };
13 
14 #define ICE_STAT(_type, _name, _stat) { \
15 	.stat_string = _name, \
16 	.sizeof_stat = FIELD_SIZEOF(_type, _stat), \
17 	.stat_offset = offsetof(_type, _stat) \
18 }
19 
20 #define ICE_VSI_STAT(_name, _stat) \
21 		ICE_STAT(struct ice_vsi, _name, _stat)
22 #define ICE_PF_STAT(_name, _stat) \
23 		ICE_STAT(struct ice_pf, _name, _stat)
24 
25 static int ice_q_stats_len(struct net_device *netdev)
26 {
27 	struct ice_netdev_priv *np = netdev_priv(netdev);
28 
29 	return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) *
30 		(sizeof(struct ice_q_stats) / sizeof(u64)));
31 }
32 
33 #define ICE_PF_STATS_LEN	ARRAY_SIZE(ice_gstrings_pf_stats)
34 #define ICE_VSI_STATS_LEN	ARRAY_SIZE(ice_gstrings_vsi_stats)
35 
36 #define ICE_ALL_STATS_LEN(n)	(ICE_PF_STATS_LEN + ICE_VSI_STATS_LEN + \
37 				 ice_q_stats_len(n))
38 
39 static const struct ice_stats ice_gstrings_vsi_stats[] = {
40 	ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
41 	ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
42 	ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
43 	ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
44 	ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
45 	ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
46 	ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes),
47 	ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes),
48 	ICE_VSI_STAT("rx_discards", eth_stats.rx_discards),
49 	ICE_VSI_STAT("tx_errors", eth_stats.tx_errors),
50 	ICE_VSI_STAT("tx_linearize", tx_linearize),
51 	ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
52 	ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed),
53 	ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
54 };
55 
56 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
57  * but they aren't. This device is capable of supporting multiple
58  * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual
59  * netdevs whereas the PF_STATs are for the physical function that's
60  * hosting these netdevs.
61  *
62  * The PF_STATs are appended to the netdev stats only when ethtool -S
63  * is queried on the base PF netdev.
64  */
65 static const struct ice_stats ice_gstrings_pf_stats[] = {
66 	ICE_PF_STAT("port.tx_bytes", stats.eth.tx_bytes),
67 	ICE_PF_STAT("port.rx_bytes", stats.eth.rx_bytes),
68 	ICE_PF_STAT("port.tx_unicast", stats.eth.tx_unicast),
69 	ICE_PF_STAT("port.rx_unicast", stats.eth.rx_unicast),
70 	ICE_PF_STAT("port.tx_multicast", stats.eth.tx_multicast),
71 	ICE_PF_STAT("port.rx_multicast", stats.eth.rx_multicast),
72 	ICE_PF_STAT("port.tx_broadcast", stats.eth.tx_broadcast),
73 	ICE_PF_STAT("port.rx_broadcast", stats.eth.rx_broadcast),
74 	ICE_PF_STAT("port.tx_errors", stats.eth.tx_errors),
75 	ICE_PF_STAT("port.tx_size_64", stats.tx_size_64),
76 	ICE_PF_STAT("port.rx_size_64", stats.rx_size_64),
77 	ICE_PF_STAT("port.tx_size_127", stats.tx_size_127),
78 	ICE_PF_STAT("port.rx_size_127", stats.rx_size_127),
79 	ICE_PF_STAT("port.tx_size_255", stats.tx_size_255),
80 	ICE_PF_STAT("port.rx_size_255", stats.rx_size_255),
81 	ICE_PF_STAT("port.tx_size_511", stats.tx_size_511),
82 	ICE_PF_STAT("port.rx_size_511", stats.rx_size_511),
83 	ICE_PF_STAT("port.tx_size_1023", stats.tx_size_1023),
84 	ICE_PF_STAT("port.rx_size_1023", stats.rx_size_1023),
85 	ICE_PF_STAT("port.tx_size_1522", stats.tx_size_1522),
86 	ICE_PF_STAT("port.rx_size_1522", stats.rx_size_1522),
87 	ICE_PF_STAT("port.tx_size_big", stats.tx_size_big),
88 	ICE_PF_STAT("port.rx_size_big", stats.rx_size_big),
89 	ICE_PF_STAT("port.link_xon_tx", stats.link_xon_tx),
90 	ICE_PF_STAT("port.link_xon_rx", stats.link_xon_rx),
91 	ICE_PF_STAT("port.link_xoff_tx", stats.link_xoff_tx),
92 	ICE_PF_STAT("port.link_xoff_rx", stats.link_xoff_rx),
93 	ICE_PF_STAT("port.tx_dropped_link_down", stats.tx_dropped_link_down),
94 	ICE_PF_STAT("port.rx_undersize", stats.rx_undersize),
95 	ICE_PF_STAT("port.rx_fragments", stats.rx_fragments),
96 	ICE_PF_STAT("port.rx_oversize", stats.rx_oversize),
97 	ICE_PF_STAT("port.rx_jabber", stats.rx_jabber),
98 	ICE_PF_STAT("port.rx_csum_bad", hw_csum_rx_error),
99 	ICE_PF_STAT("port.rx_length_errors", stats.rx_len_errors),
100 	ICE_PF_STAT("port.rx_dropped", stats.eth.rx_discards),
101 	ICE_PF_STAT("port.rx_crc_errors", stats.crc_errors),
102 	ICE_PF_STAT("port.illegal_bytes", stats.illegal_bytes),
103 	ICE_PF_STAT("port.mac_local_faults", stats.mac_local_faults),
104 	ICE_PF_STAT("port.mac_remote_faults", stats.mac_remote_faults),
105 };
106 
107 static const u32 ice_regs_dump_list[] = {
108 	PFGEN_STATE,
109 	PRTGEN_STATUS,
110 	QRX_CTRL(0),
111 	QINT_TQCTL(0),
112 	QINT_RQCTL(0),
113 	PFINT_OICR_ENA,
114 	QRX_ITR(0),
115 };
116 
117 struct ice_priv_flag {
118 	char name[ETH_GSTRING_LEN];
119 	u32 bitno;			/* bit position in pf->flags */
120 };
121 
122 #define ICE_PRIV_FLAG(_name, _bitno) { \
123 	.name = _name, \
124 	.bitno = _bitno, \
125 }
126 
127 static const struct ice_priv_flag ice_gstrings_priv_flags[] = {
128 	ICE_PRIV_FLAG("link-down-on-close", ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA),
129 };
130 
131 #define ICE_PRIV_FLAG_ARRAY_SIZE	ARRAY_SIZE(ice_gstrings_priv_flags)
132 
133 /**
134  * ice_nvm_version_str - format the NVM version strings
135  * @hw: ptr to the hardware info
136  */
137 static char *ice_nvm_version_str(struct ice_hw *hw)
138 {
139 	static char buf[ICE_ETHTOOL_FWVER_LEN];
140 	u8 ver, patch;
141 	u32 full_ver;
142 	u16 build;
143 
144 	full_ver = hw->nvm.oem_ver;
145 	ver = (u8)((full_ver & ICE_OEM_VER_MASK) >> ICE_OEM_VER_SHIFT);
146 	build = (u16)((full_ver & ICE_OEM_VER_BUILD_MASK) >>
147 		      ICE_OEM_VER_BUILD_SHIFT);
148 	patch = (u8)(full_ver & ICE_OEM_VER_PATCH_MASK);
149 
150 	snprintf(buf, sizeof(buf), "%x.%02x 0x%x %d.%d.%d",
151 		 (hw->nvm.ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT,
152 		 (hw->nvm.ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT,
153 		 hw->nvm.eetrack, ver, build, patch);
154 
155 	return buf;
156 }
157 
158 static void
159 ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
160 {
161 	struct ice_netdev_priv *np = netdev_priv(netdev);
162 	struct ice_vsi *vsi = np->vsi;
163 	struct ice_pf *pf = vsi->back;
164 
165 	strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
166 	strlcpy(drvinfo->version, ice_drv_ver, sizeof(drvinfo->version));
167 	strlcpy(drvinfo->fw_version, ice_nvm_version_str(&pf->hw),
168 		sizeof(drvinfo->fw_version));
169 	strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
170 		sizeof(drvinfo->bus_info));
171 	drvinfo->n_priv_flags = ICE_PRIV_FLAG_ARRAY_SIZE;
172 }
173 
174 static int ice_get_regs_len(struct net_device __always_unused *netdev)
175 {
176 	return sizeof(ice_regs_dump_list);
177 }
178 
179 static void
180 ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
181 {
182 	struct ice_netdev_priv *np = netdev_priv(netdev);
183 	struct ice_pf *pf = np->vsi->back;
184 	struct ice_hw *hw = &pf->hw;
185 	u32 *regs_buf = (u32 *)p;
186 	int i;
187 
188 	regs->version = 1;
189 
190 	for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i)
191 		regs_buf[i] = rd32(hw, ice_regs_dump_list[i]);
192 }
193 
194 static u32 ice_get_msglevel(struct net_device *netdev)
195 {
196 	struct ice_netdev_priv *np = netdev_priv(netdev);
197 	struct ice_pf *pf = np->vsi->back;
198 
199 #ifndef CONFIG_DYNAMIC_DEBUG
200 	if (pf->hw.debug_mask)
201 		netdev_info(netdev, "hw debug_mask: 0x%llX\n",
202 			    pf->hw.debug_mask);
203 #endif /* !CONFIG_DYNAMIC_DEBUG */
204 
205 	return pf->msg_enable;
206 }
207 
208 static void ice_set_msglevel(struct net_device *netdev, u32 data)
209 {
210 	struct ice_netdev_priv *np = netdev_priv(netdev);
211 	struct ice_pf *pf = np->vsi->back;
212 
213 #ifndef CONFIG_DYNAMIC_DEBUG
214 	if (ICE_DBG_USER & data)
215 		pf->hw.debug_mask = data;
216 	else
217 		pf->msg_enable = data;
218 #else
219 	pf->msg_enable = data;
220 #endif /* !CONFIG_DYNAMIC_DEBUG */
221 }
222 
223 static int ice_get_eeprom_len(struct net_device *netdev)
224 {
225 	struct ice_netdev_priv *np = netdev_priv(netdev);
226 	struct ice_pf *pf = np->vsi->back;
227 
228 	return (int)(pf->hw.nvm.sr_words * sizeof(u16));
229 }
230 
231 static int
232 ice_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
233 	       u8 *bytes)
234 {
235 	struct ice_netdev_priv *np = netdev_priv(netdev);
236 	u16 first_word, last_word, nwords;
237 	struct ice_vsi *vsi = np->vsi;
238 	struct ice_pf *pf = vsi->back;
239 	struct ice_hw *hw = &pf->hw;
240 	enum ice_status status;
241 	struct device *dev;
242 	int ret = 0;
243 	u16 *buf;
244 
245 	dev = &pf->pdev->dev;
246 
247 	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
248 
249 	first_word = eeprom->offset >> 1;
250 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
251 	nwords = last_word - first_word + 1;
252 
253 	buf = devm_kcalloc(dev, nwords, sizeof(u16), GFP_KERNEL);
254 	if (!buf)
255 		return -ENOMEM;
256 
257 	status = ice_read_sr_buf(hw, first_word, &nwords, buf);
258 	if (status) {
259 		dev_err(dev, "ice_read_sr_buf failed, err %d aq_err %d\n",
260 			status, hw->adminq.sq_last_status);
261 		eeprom->len = sizeof(u16) * nwords;
262 		ret = -EIO;
263 		goto out;
264 	}
265 
266 	memcpy(bytes, (u8 *)buf + (eeprom->offset & 1), eeprom->len);
267 out:
268 	devm_kfree(dev, buf);
269 	return ret;
270 }
271 
272 static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
273 {
274 	struct ice_netdev_priv *np = netdev_priv(netdev);
275 	struct ice_vsi *vsi = np->vsi;
276 	char *p = (char *)data;
277 	unsigned int i;
278 
279 	switch (stringset) {
280 	case ETH_SS_STATS:
281 		for (i = 0; i < ICE_VSI_STATS_LEN; i++) {
282 			snprintf(p, ETH_GSTRING_LEN, "%s",
283 				 ice_gstrings_vsi_stats[i].stat_string);
284 			p += ETH_GSTRING_LEN;
285 		}
286 
287 		ice_for_each_alloc_txq(vsi, i) {
288 			snprintf(p, ETH_GSTRING_LEN,
289 				 "tx-queue-%u.tx_packets", i);
290 			p += ETH_GSTRING_LEN;
291 			snprintf(p, ETH_GSTRING_LEN, "tx-queue-%u.tx_bytes", i);
292 			p += ETH_GSTRING_LEN;
293 		}
294 
295 		ice_for_each_alloc_rxq(vsi, i) {
296 			snprintf(p, ETH_GSTRING_LEN,
297 				 "rx-queue-%u.rx_packets", i);
298 			p += ETH_GSTRING_LEN;
299 			snprintf(p, ETH_GSTRING_LEN, "rx-queue-%u.rx_bytes", i);
300 			p += ETH_GSTRING_LEN;
301 		}
302 
303 		if (vsi->type != ICE_VSI_PF)
304 			return;
305 
306 		for (i = 0; i < ICE_PF_STATS_LEN; i++) {
307 			snprintf(p, ETH_GSTRING_LEN, "%s",
308 				 ice_gstrings_pf_stats[i].stat_string);
309 			p += ETH_GSTRING_LEN;
310 		}
311 
312 		break;
313 	case ETH_SS_PRIV_FLAGS:
314 		for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
315 			snprintf(p, ETH_GSTRING_LEN, "%s",
316 				 ice_gstrings_priv_flags[i].name);
317 			p += ETH_GSTRING_LEN;
318 		}
319 		break;
320 	default:
321 		break;
322 	}
323 }
324 
325 static int
326 ice_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
327 {
328 	struct ice_netdev_priv *np = netdev_priv(netdev);
329 	bool led_active;
330 
331 	switch (state) {
332 	case ETHTOOL_ID_ACTIVE:
333 		led_active = true;
334 		break;
335 	case ETHTOOL_ID_INACTIVE:
336 		led_active = false;
337 		break;
338 	default:
339 		return -EINVAL;
340 	}
341 
342 	if (ice_aq_set_port_id_led(np->vsi->port_info, !led_active, NULL))
343 		return -EIO;
344 
345 	return 0;
346 }
347 
348 /**
349  * ice_get_priv_flags - report device private flags
350  * @netdev: network interface device structure
351  *
352  * The get string set count and the string set should be matched for each
353  * flag returned.  Add new strings for each flag to the ice_gstrings_priv_flags
354  * array.
355  *
356  * Returns a u32 bitmap of flags.
357  */
358 static u32 ice_get_priv_flags(struct net_device *netdev)
359 {
360 	struct ice_netdev_priv *np = netdev_priv(netdev);
361 	struct ice_vsi *vsi = np->vsi;
362 	struct ice_pf *pf = vsi->back;
363 	u32 i, ret_flags = 0;
364 
365 	for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
366 		const struct ice_priv_flag *priv_flag;
367 
368 		priv_flag = &ice_gstrings_priv_flags[i];
369 
370 		if (test_bit(priv_flag->bitno, pf->flags))
371 			ret_flags |= BIT(i);
372 	}
373 
374 	return ret_flags;
375 }
376 
377 /**
378  * ice_set_priv_flags - set private flags
379  * @netdev: network interface device structure
380  * @flags: bit flags to be set
381  */
382 static int ice_set_priv_flags(struct net_device *netdev, u32 flags)
383 {
384 	struct ice_netdev_priv *np = netdev_priv(netdev);
385 	struct ice_vsi *vsi = np->vsi;
386 	struct ice_pf *pf = vsi->back;
387 	u32 i;
388 
389 	if (flags > BIT(ICE_PRIV_FLAG_ARRAY_SIZE))
390 		return -EINVAL;
391 
392 	for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
393 		const struct ice_priv_flag *priv_flag;
394 
395 		priv_flag = &ice_gstrings_priv_flags[i];
396 
397 		if (flags & BIT(i))
398 			set_bit(priv_flag->bitno, pf->flags);
399 		else
400 			clear_bit(priv_flag->bitno, pf->flags);
401 	}
402 
403 	return 0;
404 }
405 
406 static int ice_get_sset_count(struct net_device *netdev, int sset)
407 {
408 	switch (sset) {
409 	case ETH_SS_STATS:
410 		/* The number (and order) of strings reported *must* remain
411 		 * constant for a given netdevice. This function must not
412 		 * report a different number based on run time parameters
413 		 * (such as the number of queues in use, or the setting of
414 		 * a private ethtool flag). This is due to the nature of the
415 		 * ethtool stats API.
416 		 *
417 		 * Userspace programs such as ethtool must make 3 separate
418 		 * ioctl requests, one for size, one for the strings, and
419 		 * finally one for the stats. Since these cross into
420 		 * userspace, changes to the number or size could result in
421 		 * undefined memory access or incorrect string<->value
422 		 * correlations for statistics.
423 		 *
424 		 * Even if it appears to be safe, changes to the size or
425 		 * order of strings will suffer from race conditions and are
426 		 * not safe.
427 		 */
428 		return ICE_ALL_STATS_LEN(netdev);
429 	case ETH_SS_PRIV_FLAGS:
430 		return ICE_PRIV_FLAG_ARRAY_SIZE;
431 	default:
432 		return -EOPNOTSUPP;
433 	}
434 }
435 
436 static void
437 ice_get_ethtool_stats(struct net_device *netdev,
438 		      struct ethtool_stats __always_unused *stats, u64 *data)
439 {
440 	struct ice_netdev_priv *np = netdev_priv(netdev);
441 	struct ice_vsi *vsi = np->vsi;
442 	struct ice_pf *pf = vsi->back;
443 	struct ice_ring *ring;
444 	unsigned int j = 0;
445 	int i = 0;
446 	char *p;
447 
448 	for (j = 0; j < ICE_VSI_STATS_LEN; j++) {
449 		p = (char *)vsi + ice_gstrings_vsi_stats[j].stat_offset;
450 		data[i++] = (ice_gstrings_vsi_stats[j].sizeof_stat ==
451 			    sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
452 	}
453 
454 	/* populate per queue stats */
455 	rcu_read_lock();
456 
457 	ice_for_each_alloc_txq(vsi, j) {
458 		ring = READ_ONCE(vsi->tx_rings[j]);
459 		if (ring) {
460 			data[i++] = ring->stats.pkts;
461 			data[i++] = ring->stats.bytes;
462 		} else {
463 			data[i++] = 0;
464 			data[i++] = 0;
465 		}
466 	}
467 
468 	ice_for_each_alloc_rxq(vsi, j) {
469 		ring = READ_ONCE(vsi->rx_rings[j]);
470 		if (ring) {
471 			data[i++] = ring->stats.pkts;
472 			data[i++] = ring->stats.bytes;
473 		} else {
474 			data[i++] = 0;
475 			data[i++] = 0;
476 		}
477 	}
478 
479 	rcu_read_unlock();
480 
481 	if (vsi->type != ICE_VSI_PF)
482 		return;
483 
484 	for (j = 0; j < ICE_PF_STATS_LEN; j++) {
485 		p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset;
486 		data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat ==
487 			     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
488 	}
489 }
490 
491 /**
492  * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes
493  * @netdev: network interface device structure
494  * @ks: ethtool link ksettings struct to fill out
495  */
496 static void
497 ice_phy_type_to_ethtool(struct net_device *netdev,
498 			struct ethtool_link_ksettings *ks)
499 {
500 	struct ice_netdev_priv *np = netdev_priv(netdev);
501 	struct ice_link_status *hw_link_info;
502 	bool need_add_adv_mode = false;
503 	struct ice_vsi *vsi = np->vsi;
504 	u64 phy_types_high;
505 	u64 phy_types_low;
506 
507 	hw_link_info = &vsi->port_info->phy.link_info;
508 	phy_types_low = vsi->port_info->phy.phy_type_low;
509 	phy_types_high = vsi->port_info->phy.phy_type_high;
510 
511 	ethtool_link_ksettings_zero_link_mode(ks, supported);
512 	ethtool_link_ksettings_zero_link_mode(ks, advertising);
513 
514 	if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
515 	    phy_types_low & ICE_PHY_TYPE_LOW_100M_SGMII) {
516 		ethtool_link_ksettings_add_link_mode(ks, supported,
517 						     100baseT_Full);
518 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100MB)
519 			ethtool_link_ksettings_add_link_mode(ks, advertising,
520 							     100baseT_Full);
521 	}
522 	if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
523 	    phy_types_low & ICE_PHY_TYPE_LOW_1G_SGMII) {
524 		ethtool_link_ksettings_add_link_mode(ks, supported,
525 						     1000baseT_Full);
526 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
527 			ethtool_link_ksettings_add_link_mode(ks, advertising,
528 							     1000baseT_Full);
529 	}
530 	if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX) {
531 		ethtool_link_ksettings_add_link_mode(ks, supported,
532 						     1000baseKX_Full);
533 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
534 			ethtool_link_ksettings_add_link_mode(ks, advertising,
535 							     1000baseKX_Full);
536 	}
537 	if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_SX ||
538 	    phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_LX) {
539 		ethtool_link_ksettings_add_link_mode(ks, supported,
540 						     1000baseX_Full);
541 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
542 			ethtool_link_ksettings_add_link_mode(ks, advertising,
543 							     1000baseX_Full);
544 	}
545 	if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T) {
546 		ethtool_link_ksettings_add_link_mode(ks, supported,
547 						     2500baseT_Full);
548 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
549 			ethtool_link_ksettings_add_link_mode(ks, advertising,
550 							     2500baseT_Full);
551 	}
552 	if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_X ||
553 	    phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX) {
554 		ethtool_link_ksettings_add_link_mode(ks, supported,
555 						     2500baseX_Full);
556 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
557 			ethtool_link_ksettings_add_link_mode(ks, advertising,
558 							     2500baseX_Full);
559 	}
560 	if (phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
561 	    phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR) {
562 		ethtool_link_ksettings_add_link_mode(ks, supported,
563 						     5000baseT_Full);
564 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_5GB)
565 			ethtool_link_ksettings_add_link_mode(ks, advertising,
566 							     5000baseT_Full);
567 	}
568 	if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
569 	    phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_DA ||
570 	    phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC ||
571 	    phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_C2C) {
572 		ethtool_link_ksettings_add_link_mode(ks, supported,
573 						     10000baseT_Full);
574 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
575 			ethtool_link_ksettings_add_link_mode(ks, advertising,
576 							     10000baseT_Full);
577 	}
578 	if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1) {
579 		ethtool_link_ksettings_add_link_mode(ks, supported,
580 						     10000baseKR_Full);
581 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
582 			ethtool_link_ksettings_add_link_mode(ks, advertising,
583 							     10000baseKR_Full);
584 	}
585 	if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_SR) {
586 		ethtool_link_ksettings_add_link_mode(ks, supported,
587 						     10000baseSR_Full);
588 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
589 			ethtool_link_ksettings_add_link_mode(ks, advertising,
590 							     10000baseSR_Full);
591 	}
592 	if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_LR) {
593 		ethtool_link_ksettings_add_link_mode(ks, supported,
594 						     10000baseLR_Full);
595 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
596 			ethtool_link_ksettings_add_link_mode(ks, advertising,
597 							     10000baseLR_Full);
598 	}
599 	if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
600 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
601 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
602 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
603 	    phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC ||
604 	    phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_C2C) {
605 		ethtool_link_ksettings_add_link_mode(ks, supported,
606 						     25000baseCR_Full);
607 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
608 			ethtool_link_ksettings_add_link_mode(ks, advertising,
609 							     25000baseCR_Full);
610 	}
611 	if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_SR ||
612 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_LR) {
613 		ethtool_link_ksettings_add_link_mode(ks, supported,
614 						     25000baseSR_Full);
615 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
616 			ethtool_link_ksettings_add_link_mode(ks, advertising,
617 							     25000baseSR_Full);
618 	}
619 	if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
620 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
621 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1) {
622 		ethtool_link_ksettings_add_link_mode(ks, supported,
623 						     25000baseKR_Full);
624 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
625 			ethtool_link_ksettings_add_link_mode(ks, advertising,
626 							     25000baseKR_Full);
627 	}
628 	if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
629 		ethtool_link_ksettings_add_link_mode(ks, supported,
630 						     40000baseKR4_Full);
631 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
632 			ethtool_link_ksettings_add_link_mode(ks, advertising,
633 							     40000baseKR4_Full);
634 	}
635 	if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
636 	    phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC ||
637 	    phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI) {
638 		ethtool_link_ksettings_add_link_mode(ks, supported,
639 						     40000baseCR4_Full);
640 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
641 			ethtool_link_ksettings_add_link_mode(ks, advertising,
642 							     40000baseCR4_Full);
643 	}
644 	if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_SR4) {
645 		ethtool_link_ksettings_add_link_mode(ks, supported,
646 						     40000baseSR4_Full);
647 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
648 			ethtool_link_ksettings_add_link_mode(ks, advertising,
649 							     40000baseSR4_Full);
650 	}
651 	if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_LR4) {
652 		ethtool_link_ksettings_add_link_mode(ks, supported,
653 						     40000baseLR4_Full);
654 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
655 			ethtool_link_ksettings_add_link_mode(ks, advertising,
656 							     40000baseLR4_Full);
657 	}
658 	if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 ||
659 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC ||
660 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2 ||
661 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC ||
662 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2 ||
663 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP ||
664 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR ||
665 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC ||
666 	    phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1) {
667 		ethtool_link_ksettings_add_link_mode(ks, supported,
668 						     50000baseCR2_Full);
669 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
670 			ethtool_link_ksettings_add_link_mode(ks, advertising,
671 							     50000baseCR2_Full);
672 	}
673 	if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 ||
674 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) {
675 		ethtool_link_ksettings_add_link_mode(ks, supported,
676 						     50000baseKR2_Full);
677 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
678 			ethtool_link_ksettings_add_link_mode(ks, advertising,
679 							     50000baseKR2_Full);
680 	}
681 	if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR2 ||
682 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR2 ||
683 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_FR ||
684 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR) {
685 		ethtool_link_ksettings_add_link_mode(ks, supported,
686 						     50000baseSR2_Full);
687 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
688 			ethtool_link_ksettings_add_link_mode(ks, advertising,
689 							     50000baseSR2_Full);
690 	}
691 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 ||
692 	    phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC ||
693 	    phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4 ||
694 	    phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC ||
695 	    phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4 ||
696 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 ||
697 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2  ||
698 	    phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC ||
699 	    phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2 ||
700 	    phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC ||
701 	    phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2) {
702 		ethtool_link_ksettings_add_link_mode(ks, supported,
703 						     100000baseCR4_Full);
704 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
705 			need_add_adv_mode = true;
706 	}
707 	if (need_add_adv_mode) {
708 		need_add_adv_mode = false;
709 		ethtool_link_ksettings_add_link_mode(ks, advertising,
710 						     100000baseCR4_Full);
711 	}
712 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR4 ||
713 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR2) {
714 		ethtool_link_ksettings_add_link_mode(ks, supported,
715 						     100000baseSR4_Full);
716 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
717 			need_add_adv_mode = true;
718 	}
719 	if (need_add_adv_mode) {
720 		need_add_adv_mode = false;
721 		ethtool_link_ksettings_add_link_mode(ks, advertising,
722 						     100000baseSR4_Full);
723 	}
724 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_LR4 ||
725 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_DR) {
726 		ethtool_link_ksettings_add_link_mode(ks, supported,
727 						     100000baseLR4_ER4_Full);
728 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
729 			need_add_adv_mode = true;
730 	}
731 	if (need_add_adv_mode) {
732 		need_add_adv_mode = false;
733 		ethtool_link_ksettings_add_link_mode(ks, advertising,
734 						     100000baseLR4_ER4_Full);
735 	}
736 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 ||
737 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 ||
738 	    phy_types_high & ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4) {
739 		ethtool_link_ksettings_add_link_mode(ks, supported,
740 						     100000baseKR4_Full);
741 		if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
742 			need_add_adv_mode = true;
743 	}
744 	if (need_add_adv_mode)
745 		ethtool_link_ksettings_add_link_mode(ks, advertising,
746 						     100000baseKR4_Full);
747 
748 	/* Autoneg PHY types */
749 	if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
750 	    phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
751 	    phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX ||
752 	    phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T ||
753 	    phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX ||
754 	    phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
755 	    phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR ||
756 	    phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
757 	    phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 ||
758 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
759 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
760 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
761 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
762 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
763 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
764 	    phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1 ||
765 	    phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
766 	    phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
767 		ethtool_link_ksettings_add_link_mode(ks, supported,
768 						     Autoneg);
769 		ethtool_link_ksettings_add_link_mode(ks, advertising,
770 						     Autoneg);
771 	}
772 	if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 ||
773 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 ||
774 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP ||
775 	    phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) {
776 		ethtool_link_ksettings_add_link_mode(ks, supported,
777 						     Autoneg);
778 		ethtool_link_ksettings_add_link_mode(ks, advertising,
779 						     Autoneg);
780 	}
781 	if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 ||
782 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 ||
783 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 ||
784 	    phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2) {
785 		ethtool_link_ksettings_add_link_mode(ks, supported,
786 						     Autoneg);
787 		ethtool_link_ksettings_add_link_mode(ks, advertising,
788 						     Autoneg);
789 	}
790 }
791 
792 #define TEST_SET_BITS_TIMEOUT	50
793 #define TEST_SET_BITS_SLEEP_MAX	2000
794 #define TEST_SET_BITS_SLEEP_MIN	1000
795 
796 /**
797  * ice_get_settings_link_up - Get Link settings for when link is up
798  * @ks: ethtool ksettings to fill in
799  * @netdev: network interface device structure
800  */
801 static void
802 ice_get_settings_link_up(struct ethtool_link_ksettings *ks,
803 			 struct net_device *netdev)
804 {
805 	struct ice_netdev_priv *np = netdev_priv(netdev);
806 	struct ethtool_link_ksettings cap_ksettings;
807 	struct ice_link_status *link_info;
808 	struct ice_vsi *vsi = np->vsi;
809 	bool unrecog_phy_high = false;
810 	bool unrecog_phy_low = false;
811 
812 	link_info = &vsi->port_info->phy.link_info;
813 
814 	/* Initialize supported and advertised settings based on phy settings */
815 	switch (link_info->phy_type_low) {
816 	case ICE_PHY_TYPE_LOW_100BASE_TX:
817 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
818 		ethtool_link_ksettings_add_link_mode(ks, supported,
819 						     100baseT_Full);
820 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
821 		ethtool_link_ksettings_add_link_mode(ks, advertising,
822 						     100baseT_Full);
823 		break;
824 	case ICE_PHY_TYPE_LOW_100M_SGMII:
825 		ethtool_link_ksettings_add_link_mode(ks, supported,
826 						     100baseT_Full);
827 		break;
828 	case ICE_PHY_TYPE_LOW_1000BASE_T:
829 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
830 		ethtool_link_ksettings_add_link_mode(ks, supported,
831 						     1000baseT_Full);
832 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
833 		ethtool_link_ksettings_add_link_mode(ks, advertising,
834 						     1000baseT_Full);
835 		break;
836 	case ICE_PHY_TYPE_LOW_1G_SGMII:
837 		ethtool_link_ksettings_add_link_mode(ks, supported,
838 						     1000baseT_Full);
839 		break;
840 	case ICE_PHY_TYPE_LOW_1000BASE_SX:
841 	case ICE_PHY_TYPE_LOW_1000BASE_LX:
842 		ethtool_link_ksettings_add_link_mode(ks, supported,
843 						     1000baseX_Full);
844 		break;
845 	case ICE_PHY_TYPE_LOW_1000BASE_KX:
846 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
847 		ethtool_link_ksettings_add_link_mode(ks, supported,
848 						     1000baseKX_Full);
849 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
850 		ethtool_link_ksettings_add_link_mode(ks, advertising,
851 						     1000baseKX_Full);
852 		break;
853 	case ICE_PHY_TYPE_LOW_2500BASE_T:
854 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
855 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
856 		ethtool_link_ksettings_add_link_mode(ks, supported,
857 						     2500baseT_Full);
858 		ethtool_link_ksettings_add_link_mode(ks, advertising,
859 						     2500baseT_Full);
860 		break;
861 	case ICE_PHY_TYPE_LOW_2500BASE_X:
862 		ethtool_link_ksettings_add_link_mode(ks, supported,
863 						     2500baseX_Full);
864 		break;
865 	case ICE_PHY_TYPE_LOW_2500BASE_KX:
866 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
867 		ethtool_link_ksettings_add_link_mode(ks, supported,
868 						     2500baseX_Full);
869 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
870 		ethtool_link_ksettings_add_link_mode(ks, advertising,
871 						     2500baseX_Full);
872 		break;
873 	case ICE_PHY_TYPE_LOW_5GBASE_T:
874 	case ICE_PHY_TYPE_LOW_5GBASE_KR:
875 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
876 		ethtool_link_ksettings_add_link_mode(ks, supported,
877 						     5000baseT_Full);
878 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
879 		ethtool_link_ksettings_add_link_mode(ks, advertising,
880 						     5000baseT_Full);
881 		break;
882 	case ICE_PHY_TYPE_LOW_10GBASE_T:
883 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
884 		ethtool_link_ksettings_add_link_mode(ks, supported,
885 						     10000baseT_Full);
886 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
887 		ethtool_link_ksettings_add_link_mode(ks, advertising,
888 						     10000baseT_Full);
889 		break;
890 	case ICE_PHY_TYPE_LOW_10G_SFI_DA:
891 	case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
892 	case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
893 		ethtool_link_ksettings_add_link_mode(ks, supported,
894 						     10000baseT_Full);
895 		break;
896 	case ICE_PHY_TYPE_LOW_10GBASE_SR:
897 		ethtool_link_ksettings_add_link_mode(ks, supported,
898 						     10000baseSR_Full);
899 		break;
900 	case ICE_PHY_TYPE_LOW_10GBASE_LR:
901 		ethtool_link_ksettings_add_link_mode(ks, supported,
902 						     10000baseLR_Full);
903 		break;
904 	case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
905 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
906 		ethtool_link_ksettings_add_link_mode(ks, supported,
907 						     10000baseKR_Full);
908 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
909 		ethtool_link_ksettings_add_link_mode(ks, advertising,
910 						     10000baseKR_Full);
911 		break;
912 	case ICE_PHY_TYPE_LOW_25GBASE_T:
913 	case ICE_PHY_TYPE_LOW_25GBASE_CR:
914 	case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
915 	case ICE_PHY_TYPE_LOW_25GBASE_CR1:
916 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
917 		ethtool_link_ksettings_add_link_mode(ks, supported,
918 						     25000baseCR_Full);
919 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
920 		ethtool_link_ksettings_add_link_mode(ks, advertising,
921 						     25000baseCR_Full);
922 		break;
923 	case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
924 		ethtool_link_ksettings_add_link_mode(ks, supported,
925 						     25000baseCR_Full);
926 		break;
927 	case ICE_PHY_TYPE_LOW_25GBASE_SR:
928 	case ICE_PHY_TYPE_LOW_25GBASE_LR:
929 		ethtool_link_ksettings_add_link_mode(ks, supported,
930 						     25000baseSR_Full);
931 		break;
932 	case ICE_PHY_TYPE_LOW_25GBASE_KR:
933 	case ICE_PHY_TYPE_LOW_25GBASE_KR1:
934 	case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
935 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
936 		ethtool_link_ksettings_add_link_mode(ks, supported,
937 						     25000baseKR_Full);
938 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
939 		ethtool_link_ksettings_add_link_mode(ks, advertising,
940 						     25000baseKR_Full);
941 		break;
942 	case ICE_PHY_TYPE_LOW_40GBASE_CR4:
943 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
944 		ethtool_link_ksettings_add_link_mode(ks, supported,
945 						     40000baseCR4_Full);
946 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
947 		ethtool_link_ksettings_add_link_mode(ks, advertising,
948 						     40000baseCR4_Full);
949 		break;
950 	case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
951 	case ICE_PHY_TYPE_LOW_40G_XLAUI:
952 		ethtool_link_ksettings_add_link_mode(ks, supported,
953 						     40000baseCR4_Full);
954 		break;
955 	case ICE_PHY_TYPE_LOW_40GBASE_SR4:
956 		ethtool_link_ksettings_add_link_mode(ks, supported,
957 						     40000baseSR4_Full);
958 		break;
959 	case ICE_PHY_TYPE_LOW_40GBASE_LR4:
960 		ethtool_link_ksettings_add_link_mode(ks, supported,
961 						     40000baseLR4_Full);
962 		break;
963 	case ICE_PHY_TYPE_LOW_40GBASE_KR4:
964 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
965 		ethtool_link_ksettings_add_link_mode(ks, supported,
966 						     40000baseKR4_Full);
967 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
968 		ethtool_link_ksettings_add_link_mode(ks, advertising,
969 						     40000baseKR4_Full);
970 		break;
971 	case ICE_PHY_TYPE_LOW_50GBASE_CR2:
972 	case ICE_PHY_TYPE_LOW_50GBASE_CP:
973 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
974 		ethtool_link_ksettings_add_link_mode(ks, supported,
975 						     50000baseCR2_Full);
976 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
977 		ethtool_link_ksettings_add_link_mode(ks, advertising,
978 						     50000baseCR2_Full);
979 		break;
980 	case ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC:
981 	case ICE_PHY_TYPE_LOW_50G_LAUI2:
982 	case ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC:
983 	case ICE_PHY_TYPE_LOW_50G_AUI2:
984 	case ICE_PHY_TYPE_LOW_50GBASE_SR:
985 	case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
986 	case ICE_PHY_TYPE_LOW_50G_AUI1:
987 		ethtool_link_ksettings_add_link_mode(ks, supported,
988 						     50000baseCR2_Full);
989 		break;
990 	case ICE_PHY_TYPE_LOW_50GBASE_KR2:
991 	case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
992 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
993 		ethtool_link_ksettings_add_link_mode(ks, supported,
994 						     50000baseKR2_Full);
995 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
996 		ethtool_link_ksettings_add_link_mode(ks, advertising,
997 						     50000baseKR2_Full);
998 		break;
999 	case ICE_PHY_TYPE_LOW_50GBASE_SR2:
1000 	case ICE_PHY_TYPE_LOW_50GBASE_LR2:
1001 	case ICE_PHY_TYPE_LOW_50GBASE_FR:
1002 	case ICE_PHY_TYPE_LOW_50GBASE_LR:
1003 		ethtool_link_ksettings_add_link_mode(ks, supported,
1004 						     50000baseSR2_Full);
1005 		break;
1006 	case ICE_PHY_TYPE_LOW_100GBASE_CR4:
1007 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1008 		ethtool_link_ksettings_add_link_mode(ks, supported,
1009 						     100000baseCR4_Full);
1010 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1011 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1012 						     100000baseCR4_Full);
1013 		break;
1014 	case ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC:
1015 	case ICE_PHY_TYPE_LOW_100G_CAUI4:
1016 	case ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC:
1017 	case ICE_PHY_TYPE_LOW_100G_AUI4:
1018 	case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
1019 		ethtool_link_ksettings_add_link_mode(ks, supported,
1020 						     100000baseCR4_Full);
1021 		break;
1022 	case ICE_PHY_TYPE_LOW_100GBASE_CP2:
1023 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1024 		ethtool_link_ksettings_add_link_mode(ks, supported,
1025 						     100000baseCR4_Full);
1026 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1027 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1028 						     100000baseCR4_Full);
1029 		break;
1030 	case ICE_PHY_TYPE_LOW_100GBASE_SR4:
1031 	case ICE_PHY_TYPE_LOW_100GBASE_SR2:
1032 		ethtool_link_ksettings_add_link_mode(ks, supported,
1033 						     100000baseSR4_Full);
1034 		break;
1035 	case ICE_PHY_TYPE_LOW_100GBASE_LR4:
1036 	case ICE_PHY_TYPE_LOW_100GBASE_DR:
1037 		ethtool_link_ksettings_add_link_mode(ks, supported,
1038 						     100000baseLR4_ER4_Full);
1039 		break;
1040 	case ICE_PHY_TYPE_LOW_100GBASE_KR4:
1041 	case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
1042 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1043 		ethtool_link_ksettings_add_link_mode(ks, supported,
1044 						     100000baseKR4_Full);
1045 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1046 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1047 						     100000baseKR4_Full);
1048 		break;
1049 	default:
1050 		unrecog_phy_low = true;
1051 	}
1052 
1053 	switch (link_info->phy_type_high) {
1054 	case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
1055 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1056 		ethtool_link_ksettings_add_link_mode(ks, supported,
1057 						     100000baseKR4_Full);
1058 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1059 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1060 						     100000baseKR4_Full);
1061 		break;
1062 	case ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC:
1063 	case ICE_PHY_TYPE_HIGH_100G_CAUI2:
1064 	case ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC:
1065 	case ICE_PHY_TYPE_HIGH_100G_AUI2:
1066 		ethtool_link_ksettings_add_link_mode(ks, supported,
1067 						     100000baseCR4_Full);
1068 		break;
1069 	default:
1070 		unrecog_phy_high = true;
1071 	}
1072 
1073 	if (unrecog_phy_low && unrecog_phy_high) {
1074 		/* if we got here and link is up something bad is afoot */
1075 		netdev_info(netdev,
1076 			    "WARNING: Unrecognized PHY_Low (0x%llx).\n",
1077 			    (u64)link_info->phy_type_low);
1078 		netdev_info(netdev,
1079 			    "WARNING: Unrecognized PHY_High (0x%llx).\n",
1080 			    (u64)link_info->phy_type_high);
1081 	}
1082 
1083 	/* Now that we've worked out everything that could be supported by the
1084 	 * current PHY type, get what is supported by the NVM and intersect
1085 	 * them to get what is truly supported
1086 	 */
1087 	memset(&cap_ksettings, 0, sizeof(cap_ksettings));
1088 	ice_phy_type_to_ethtool(netdev, &cap_ksettings);
1089 	ethtool_intersect_link_masks(ks, &cap_ksettings);
1090 
1091 	switch (link_info->link_speed) {
1092 	case ICE_AQ_LINK_SPEED_100GB:
1093 		ks->base.speed = SPEED_100000;
1094 		break;
1095 	case ICE_AQ_LINK_SPEED_50GB:
1096 		ks->base.speed = SPEED_50000;
1097 		break;
1098 	case ICE_AQ_LINK_SPEED_40GB:
1099 		ks->base.speed = SPEED_40000;
1100 		break;
1101 	case ICE_AQ_LINK_SPEED_25GB:
1102 		ks->base.speed = SPEED_25000;
1103 		break;
1104 	case ICE_AQ_LINK_SPEED_20GB:
1105 		ks->base.speed = SPEED_20000;
1106 		break;
1107 	case ICE_AQ_LINK_SPEED_10GB:
1108 		ks->base.speed = SPEED_10000;
1109 		break;
1110 	case ICE_AQ_LINK_SPEED_5GB:
1111 		ks->base.speed = SPEED_5000;
1112 		break;
1113 	case ICE_AQ_LINK_SPEED_2500MB:
1114 		ks->base.speed = SPEED_2500;
1115 		break;
1116 	case ICE_AQ_LINK_SPEED_1000MB:
1117 		ks->base.speed = SPEED_1000;
1118 		break;
1119 	case ICE_AQ_LINK_SPEED_100MB:
1120 		ks->base.speed = SPEED_100;
1121 		break;
1122 	default:
1123 		netdev_info(netdev,
1124 			    "WARNING: Unrecognized link_speed (0x%x).\n",
1125 			    link_info->link_speed);
1126 		break;
1127 	}
1128 	ks->base.duplex = DUPLEX_FULL;
1129 }
1130 
1131 /**
1132  * ice_get_settings_link_down - Get the Link settings when link is down
1133  * @ks: ethtool ksettings to fill in
1134  * @netdev: network interface device structure
1135  *
1136  * Reports link settings that can be determined when link is down
1137  */
1138 static void
1139 ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
1140 			   struct net_device __always_unused *netdev)
1141 {
1142 	/* link is down and the driver needs to fall back on
1143 	 * supported phy types to figure out what info to display
1144 	 */
1145 	ice_phy_type_to_ethtool(netdev, ks);
1146 
1147 	/* With no link, speed and duplex are unknown */
1148 	ks->base.speed = SPEED_UNKNOWN;
1149 	ks->base.duplex = DUPLEX_UNKNOWN;
1150 }
1151 
1152 /**
1153  * ice_get_link_ksettings - Get Link Speed and Duplex settings
1154  * @netdev: network interface device structure
1155  * @ks: ethtool ksettings
1156  *
1157  * Reports speed/duplex settings based on media_type
1158  */
1159 static int
1160 ice_get_link_ksettings(struct net_device *netdev,
1161 		       struct ethtool_link_ksettings *ks)
1162 {
1163 	struct ice_netdev_priv *np = netdev_priv(netdev);
1164 	struct ice_link_status *hw_link_info;
1165 	struct ice_vsi *vsi = np->vsi;
1166 
1167 	ethtool_link_ksettings_zero_link_mode(ks, supported);
1168 	ethtool_link_ksettings_zero_link_mode(ks, advertising);
1169 	hw_link_info = &vsi->port_info->phy.link_info;
1170 
1171 	/* set speed and duplex */
1172 	if (hw_link_info->link_info & ICE_AQ_LINK_UP)
1173 		ice_get_settings_link_up(ks, netdev);
1174 	else
1175 		ice_get_settings_link_down(ks, netdev);
1176 
1177 	/* set autoneg settings */
1178 	ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
1179 		AUTONEG_ENABLE : AUTONEG_DISABLE;
1180 
1181 	/* set media type settings */
1182 	switch (vsi->port_info->phy.media_type) {
1183 	case ICE_MEDIA_FIBER:
1184 		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1185 		ks->base.port = PORT_FIBRE;
1186 		break;
1187 	case ICE_MEDIA_BASET:
1188 		ethtool_link_ksettings_add_link_mode(ks, supported, TP);
1189 		ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
1190 		ks->base.port = PORT_TP;
1191 		break;
1192 	case ICE_MEDIA_BACKPLANE:
1193 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1194 		ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
1195 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1196 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1197 						     Backplane);
1198 		ks->base.port = PORT_NONE;
1199 		break;
1200 	case ICE_MEDIA_DA:
1201 		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1202 		ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
1203 		ks->base.port = PORT_DA;
1204 		break;
1205 	default:
1206 		ks->base.port = PORT_OTHER;
1207 		break;
1208 	}
1209 
1210 	/* flow control is symmetric and always supported */
1211 	ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
1212 
1213 	switch (vsi->port_info->fc.req_mode) {
1214 	case ICE_FC_FULL:
1215 		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1216 		break;
1217 	case ICE_FC_TX_PAUSE:
1218 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1219 						     Asym_Pause);
1220 		break;
1221 	case ICE_FC_RX_PAUSE:
1222 		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1223 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1224 						     Asym_Pause);
1225 		break;
1226 	case ICE_FC_PFC:
1227 	default:
1228 		ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
1229 		ethtool_link_ksettings_del_link_mode(ks, advertising,
1230 						     Asym_Pause);
1231 		break;
1232 	}
1233 
1234 	return 0;
1235 }
1236 
1237 /**
1238  * ice_ksettings_find_adv_link_speed - Find advertising link speed
1239  * @ks: ethtool ksettings
1240  */
1241 static u16
1242 ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
1243 {
1244 	u16 adv_link_speed = 0;
1245 
1246 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1247 						  100baseT_Full))
1248 		adv_link_speed |= ICE_AQ_LINK_SPEED_100MB;
1249 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1250 						  1000baseX_Full))
1251 		adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
1252 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1253 						  1000baseT_Full) ||
1254 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1255 						  1000baseKX_Full))
1256 		adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
1257 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1258 						  2500baseT_Full))
1259 		adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
1260 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1261 						  2500baseX_Full))
1262 		adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
1263 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1264 						  5000baseT_Full))
1265 		adv_link_speed |= ICE_AQ_LINK_SPEED_5GB;
1266 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1267 						  10000baseT_Full) ||
1268 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1269 						  10000baseKR_Full))
1270 		adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
1271 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1272 						  10000baseSR_Full) ||
1273 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1274 						  10000baseLR_Full))
1275 		adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
1276 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1277 						  25000baseCR_Full) ||
1278 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1279 						  25000baseSR_Full) ||
1280 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1281 						  25000baseKR_Full))
1282 		adv_link_speed |= ICE_AQ_LINK_SPEED_25GB;
1283 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1284 						  40000baseCR4_Full) ||
1285 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1286 						  40000baseSR4_Full) ||
1287 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1288 						  40000baseLR4_Full) ||
1289 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1290 						  40000baseKR4_Full))
1291 		adv_link_speed |= ICE_AQ_LINK_SPEED_40GB;
1292 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1293 						  50000baseCR2_Full) ||
1294 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1295 						  50000baseKR2_Full))
1296 		adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
1297 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1298 						  50000baseSR2_Full))
1299 		adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
1300 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1301 						  100000baseCR4_Full) ||
1302 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1303 						  100000baseSR4_Full) ||
1304 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1305 						  100000baseLR4_ER4_Full) ||
1306 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1307 						  100000baseKR4_Full))
1308 		adv_link_speed |= ICE_AQ_LINK_SPEED_100GB;
1309 
1310 	return adv_link_speed;
1311 }
1312 
1313 /**
1314  * ice_setup_autoneg
1315  * @p: port info
1316  * @ks: ethtool_link_ksettings
1317  * @config: configuration that will be sent down to FW
1318  * @autoneg_enabled: autonegotiation is enabled or not
1319  * @autoneg_changed: will there a change in autonegotiation
1320  * @netdev: network interface device structure
1321  *
1322  * Setup PHY autonegotiation feature
1323  */
1324 static int
1325 ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
1326 		  struct ice_aqc_set_phy_cfg_data *config,
1327 		  u8 autoneg_enabled, u8 *autoneg_changed,
1328 		  struct net_device *netdev)
1329 {
1330 	int err = 0;
1331 
1332 	*autoneg_changed = 0;
1333 
1334 	/* Check autoneg */
1335 	if (autoneg_enabled == AUTONEG_ENABLE) {
1336 		/* If autoneg was not already enabled */
1337 		if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
1338 			/* If autoneg is not supported, return error */
1339 			if (!ethtool_link_ksettings_test_link_mode(ks,
1340 								   supported,
1341 								   Autoneg)) {
1342 				netdev_info(netdev, "Autoneg not supported on this phy.\n");
1343 				err = -EINVAL;
1344 			} else {
1345 				/* Autoneg is allowed to change */
1346 				config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1347 				*autoneg_changed = 1;
1348 			}
1349 		}
1350 	} else {
1351 		/* If autoneg is currently enabled */
1352 		if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
1353 			/* If autoneg is supported 10GBASE_T is the only phy
1354 			 * that can disable it, so otherwise return error
1355 			 */
1356 			if (ethtool_link_ksettings_test_link_mode(ks,
1357 								  supported,
1358 								  Autoneg)) {
1359 				netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
1360 				err = -EINVAL;
1361 			} else {
1362 				/* Autoneg is allowed to change */
1363 				config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1364 				*autoneg_changed = 1;
1365 			}
1366 		}
1367 	}
1368 
1369 	return err;
1370 }
1371 
1372 /**
1373  * ice_set_link_ksettings - Set Speed and Duplex
1374  * @netdev: network interface device structure
1375  * @ks: ethtool ksettings
1376  *
1377  * Set speed/duplex per media_types advertised/forced
1378  */
1379 static int
1380 ice_set_link_ksettings(struct net_device *netdev,
1381 		       const struct ethtool_link_ksettings *ks)
1382 {
1383 	u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT, lport = 0;
1384 	struct ice_netdev_priv *np = netdev_priv(netdev);
1385 	struct ethtool_link_ksettings safe_ks, copy_ks;
1386 	struct ice_aqc_get_phy_caps_data *abilities;
1387 	u16 adv_link_speed, curr_link_speed, idx;
1388 	struct ice_aqc_set_phy_cfg_data config;
1389 	struct ice_pf *pf = np->vsi->back;
1390 	struct ice_port_info *p;
1391 	u8 autoneg_changed = 0;
1392 	enum ice_status status;
1393 	u64 phy_type_high;
1394 	u64 phy_type_low;
1395 	int err = 0;
1396 	bool linkup;
1397 
1398 	p = np->vsi->port_info;
1399 
1400 	if (!p)
1401 		return -EOPNOTSUPP;
1402 
1403 	/* Check if this is lan vsi */
1404 	ice_for_each_vsi(pf, idx)
1405 		if (pf->vsi[idx]->type == ICE_VSI_PF) {
1406 			if (np->vsi != pf->vsi[idx])
1407 				return -EOPNOTSUPP;
1408 			break;
1409 		}
1410 
1411 	if (p->phy.media_type != ICE_MEDIA_BASET &&
1412 	    p->phy.media_type != ICE_MEDIA_FIBER &&
1413 	    p->phy.media_type != ICE_MEDIA_BACKPLANE &&
1414 	    p->phy.media_type != ICE_MEDIA_DA &&
1415 	    p->phy.link_info.link_info & ICE_AQ_LINK_UP)
1416 		return -EOPNOTSUPP;
1417 
1418 	/* copy the ksettings to copy_ks to avoid modifying the original */
1419 	memcpy(&copy_ks, ks, sizeof(copy_ks));
1420 
1421 	/* save autoneg out of ksettings */
1422 	autoneg = copy_ks.base.autoneg;
1423 
1424 	memset(&safe_ks, 0, sizeof(safe_ks));
1425 
1426 	/* Get link modes supported by hardware.*/
1427 	ice_phy_type_to_ethtool(netdev, &safe_ks);
1428 
1429 	/* and check against modes requested by user.
1430 	 * Return an error if unsupported mode was set.
1431 	 */
1432 	if (!bitmap_subset(copy_ks.link_modes.advertising,
1433 			   safe_ks.link_modes.supported,
1434 			   __ETHTOOL_LINK_MODE_MASK_NBITS))
1435 		return -EINVAL;
1436 
1437 	/* get our own copy of the bits to check against */
1438 	memset(&safe_ks, 0, sizeof(safe_ks));
1439 	safe_ks.base.cmd = copy_ks.base.cmd;
1440 	safe_ks.base.link_mode_masks_nwords =
1441 		copy_ks.base.link_mode_masks_nwords;
1442 	ice_get_link_ksettings(netdev, &safe_ks);
1443 
1444 	/* set autoneg back to what it currently is */
1445 	copy_ks.base.autoneg = safe_ks.base.autoneg;
1446 	/* we don't compare the speed */
1447 	copy_ks.base.speed = safe_ks.base.speed;
1448 
1449 	/* If copy_ks.base and safe_ks.base are not the same now, then they are
1450 	 * trying to set something that we do not support.
1451 	 */
1452 	if (memcmp(&copy_ks.base, &safe_ks.base, sizeof(copy_ks.base)))
1453 		return -EOPNOTSUPP;
1454 
1455 	while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
1456 		timeout--;
1457 		if (!timeout)
1458 			return -EBUSY;
1459 		usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
1460 	}
1461 
1462 	abilities = devm_kzalloc(&pf->pdev->dev, sizeof(*abilities),
1463 				 GFP_KERNEL);
1464 	if (!abilities)
1465 		return -ENOMEM;
1466 
1467 	/* Get the current phy config */
1468 	status = ice_aq_get_phy_caps(p, false, ICE_AQC_REPORT_SW_CFG, abilities,
1469 				     NULL);
1470 	if (status) {
1471 		err = -EAGAIN;
1472 		goto done;
1473 	}
1474 
1475 	/* Copy abilities to config in case autoneg is not set below */
1476 	memset(&config, 0, sizeof(config));
1477 	config.caps = abilities->caps & ~ICE_AQC_PHY_AN_MODE;
1478 	if (abilities->caps & ICE_AQC_PHY_AN_MODE)
1479 		config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1480 
1481 	/* Check autoneg */
1482 	err = ice_setup_autoneg(p, &safe_ks, &config, autoneg, &autoneg_changed,
1483 				netdev);
1484 
1485 	if (err)
1486 		goto done;
1487 
1488 	/* Call to get the current link speed */
1489 	p->phy.get_link_info = true;
1490 	status = ice_get_link_status(p, &linkup);
1491 	if (status) {
1492 		err = -EAGAIN;
1493 		goto done;
1494 	}
1495 
1496 	curr_link_speed = p->phy.link_info.link_speed;
1497 	adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
1498 
1499 	/* If speed didn't get set, set it to what it currently is.
1500 	 * This is needed because if advertise is 0 (as it is when autoneg
1501 	 * is disabled) then speed won't get set.
1502 	 */
1503 	if (!adv_link_speed)
1504 		adv_link_speed = curr_link_speed;
1505 
1506 	/* Convert the advertise link speeds to their corresponded PHY_TYPE */
1507 	ice_update_phy_type(&phy_type_low, &phy_type_high, adv_link_speed);
1508 
1509 	if (!autoneg_changed && adv_link_speed == curr_link_speed) {
1510 		netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
1511 		goto done;
1512 	}
1513 
1514 	/* copy over the rest of the abilities */
1515 	config.low_power_ctrl = abilities->low_power_ctrl;
1516 	config.eee_cap = abilities->eee_cap;
1517 	config.eeer_value = abilities->eeer_value;
1518 	config.link_fec_opt = abilities->link_fec_options;
1519 
1520 	/* save the requested speeds */
1521 	p->phy.link_info.req_speeds = adv_link_speed;
1522 
1523 	/* set link and auto negotiation so changes take effect */
1524 	config.caps |= ICE_AQ_PHY_ENA_LINK;
1525 
1526 	if (phy_type_low || phy_type_high) {
1527 		config.phy_type_high = cpu_to_le64(phy_type_high) &
1528 			abilities->phy_type_high;
1529 		config.phy_type_low = cpu_to_le64(phy_type_low) &
1530 			abilities->phy_type_low;
1531 	} else {
1532 		err = -EAGAIN;
1533 		netdev_info(netdev, "Nothing changed. No PHY_TYPE is corresponded to advertised link speed.\n");
1534 		goto done;
1535 	}
1536 
1537 	/* If link is up put link down */
1538 	if (p->phy.link_info.link_info & ICE_AQ_LINK_UP) {
1539 		/* Tell the OS link is going down, the link will go
1540 		 * back up when fw says it is ready asynchronously
1541 		 */
1542 		ice_print_link_msg(np->vsi, false);
1543 		netif_carrier_off(netdev);
1544 		netif_tx_stop_all_queues(netdev);
1545 	}
1546 
1547 	/* make the aq call */
1548 	status = ice_aq_set_phy_cfg(&pf->hw, lport, &config, NULL);
1549 	if (status) {
1550 		netdev_info(netdev, "Set phy config failed,\n");
1551 		err = -EAGAIN;
1552 	}
1553 
1554 done:
1555 	devm_kfree(&pf->pdev->dev, abilities);
1556 	clear_bit(__ICE_CFG_BUSY, pf->state);
1557 
1558 	return err;
1559 }
1560 
1561 /**
1562  * ice_get_rxnfc - command to get RX flow classification rules
1563  * @netdev: network interface device structure
1564  * @cmd: ethtool rxnfc command
1565  * @rule_locs: buffer to rturn Rx flow classification rules
1566  *
1567  * Returns Success if the command is supported.
1568  */
1569 static int
1570 ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1571 	      u32 __always_unused *rule_locs)
1572 {
1573 	struct ice_netdev_priv *np = netdev_priv(netdev);
1574 	struct ice_vsi *vsi = np->vsi;
1575 	int ret = -EOPNOTSUPP;
1576 
1577 	switch (cmd->cmd) {
1578 	case ETHTOOL_GRXRINGS:
1579 		cmd->data = vsi->rss_size;
1580 		ret = 0;
1581 		break;
1582 	default:
1583 		break;
1584 	}
1585 
1586 	return ret;
1587 }
1588 
1589 static void
1590 ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
1591 {
1592 	struct ice_netdev_priv *np = netdev_priv(netdev);
1593 	struct ice_vsi *vsi = np->vsi;
1594 
1595 	ring->rx_max_pending = ICE_MAX_NUM_DESC;
1596 	ring->tx_max_pending = ICE_MAX_NUM_DESC;
1597 	ring->rx_pending = vsi->rx_rings[0]->count;
1598 	ring->tx_pending = vsi->tx_rings[0]->count;
1599 
1600 	/* Rx mini and jumbo rings are not supported */
1601 	ring->rx_mini_max_pending = 0;
1602 	ring->rx_jumbo_max_pending = 0;
1603 	ring->rx_mini_pending = 0;
1604 	ring->rx_jumbo_pending = 0;
1605 }
1606 
1607 static int
1608 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
1609 {
1610 	struct ice_ring *tx_rings = NULL, *rx_rings = NULL;
1611 	struct ice_netdev_priv *np = netdev_priv(netdev);
1612 	struct ice_vsi *vsi = np->vsi;
1613 	struct ice_pf *pf = vsi->back;
1614 	int i, timeout = 50, err = 0;
1615 	u32 new_rx_cnt, new_tx_cnt;
1616 
1617 	if (ring->tx_pending > ICE_MAX_NUM_DESC ||
1618 	    ring->tx_pending < ICE_MIN_NUM_DESC ||
1619 	    ring->rx_pending > ICE_MAX_NUM_DESC ||
1620 	    ring->rx_pending < ICE_MIN_NUM_DESC) {
1621 		netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
1622 			   ring->tx_pending, ring->rx_pending,
1623 			   ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
1624 			   ICE_REQ_DESC_MULTIPLE);
1625 		return -EINVAL;
1626 	}
1627 
1628 	new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
1629 	if (new_tx_cnt != ring->tx_pending)
1630 		netdev_info(netdev,
1631 			    "Requested Tx descriptor count rounded up to %d\n",
1632 			    new_tx_cnt);
1633 	new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
1634 	if (new_rx_cnt != ring->rx_pending)
1635 		netdev_info(netdev,
1636 			    "Requested Rx descriptor count rounded up to %d\n",
1637 			    new_rx_cnt);
1638 
1639 	/* if nothing to do return success */
1640 	if (new_tx_cnt == vsi->tx_rings[0]->count &&
1641 	    new_rx_cnt == vsi->rx_rings[0]->count) {
1642 		netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
1643 		return 0;
1644 	}
1645 
1646 	while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
1647 		timeout--;
1648 		if (!timeout)
1649 			return -EBUSY;
1650 		usleep_range(1000, 2000);
1651 	}
1652 
1653 	/* set for the next time the netdev is started */
1654 	if (!netif_running(vsi->netdev)) {
1655 		for (i = 0; i < vsi->alloc_txq; i++)
1656 			vsi->tx_rings[i]->count = new_tx_cnt;
1657 		for (i = 0; i < vsi->alloc_rxq; i++)
1658 			vsi->rx_rings[i]->count = new_rx_cnt;
1659 		netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
1660 		goto done;
1661 	}
1662 
1663 	if (new_tx_cnt == vsi->tx_rings[0]->count)
1664 		goto process_rx;
1665 
1666 	/* alloc updated Tx resources */
1667 	netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
1668 		    vsi->tx_rings[0]->count, new_tx_cnt);
1669 
1670 	tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
1671 				sizeof(*tx_rings), GFP_KERNEL);
1672 	if (!tx_rings) {
1673 		err = -ENOMEM;
1674 		goto done;
1675 	}
1676 
1677 	for (i = 0; i < vsi->alloc_txq; i++) {
1678 		/* clone ring and setup updated count */
1679 		tx_rings[i] = *vsi->tx_rings[i];
1680 		tx_rings[i].count = new_tx_cnt;
1681 		tx_rings[i].desc = NULL;
1682 		tx_rings[i].tx_buf = NULL;
1683 		err = ice_setup_tx_ring(&tx_rings[i]);
1684 		if (err) {
1685 			while (i) {
1686 				i--;
1687 				ice_clean_tx_ring(&tx_rings[i]);
1688 			}
1689 			devm_kfree(&pf->pdev->dev, tx_rings);
1690 			goto done;
1691 		}
1692 	}
1693 
1694 process_rx:
1695 	if (new_rx_cnt == vsi->rx_rings[0]->count)
1696 		goto process_link;
1697 
1698 	/* alloc updated Rx resources */
1699 	netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
1700 		    vsi->rx_rings[0]->count, new_rx_cnt);
1701 
1702 	rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
1703 				sizeof(*rx_rings), GFP_KERNEL);
1704 	if (!rx_rings) {
1705 		err = -ENOMEM;
1706 		goto done;
1707 	}
1708 
1709 	for (i = 0; i < vsi->alloc_rxq; i++) {
1710 		/* clone ring and setup updated count */
1711 		rx_rings[i] = *vsi->rx_rings[i];
1712 		rx_rings[i].count = new_rx_cnt;
1713 		rx_rings[i].desc = NULL;
1714 		rx_rings[i].rx_buf = NULL;
1715 		/* this is to allow wr32 to have something to write to
1716 		 * during early allocation of Rx buffers
1717 		 */
1718 		rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
1719 
1720 		err = ice_setup_rx_ring(&rx_rings[i]);
1721 		if (err)
1722 			goto rx_unwind;
1723 
1724 		/* allocate Rx buffers */
1725 		err = ice_alloc_rx_bufs(&rx_rings[i],
1726 					ICE_DESC_UNUSED(&rx_rings[i]));
1727 rx_unwind:
1728 		if (err) {
1729 			while (i) {
1730 				i--;
1731 				ice_free_rx_ring(&rx_rings[i]);
1732 			}
1733 			devm_kfree(&pf->pdev->dev, rx_rings);
1734 			err = -ENOMEM;
1735 			goto free_tx;
1736 		}
1737 	}
1738 
1739 process_link:
1740 	/* Bring interface down, copy in the new ring info, then restore the
1741 	 * interface. if VSI is up, bring it down and then back up
1742 	 */
1743 	if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
1744 		ice_down(vsi);
1745 
1746 		if (tx_rings) {
1747 			for (i = 0; i < vsi->alloc_txq; i++) {
1748 				ice_free_tx_ring(vsi->tx_rings[i]);
1749 				*vsi->tx_rings[i] = tx_rings[i];
1750 			}
1751 			devm_kfree(&pf->pdev->dev, tx_rings);
1752 		}
1753 
1754 		if (rx_rings) {
1755 			for (i = 0; i < vsi->alloc_rxq; i++) {
1756 				ice_free_rx_ring(vsi->rx_rings[i]);
1757 				/* copy the real tail offset */
1758 				rx_rings[i].tail = vsi->rx_rings[i]->tail;
1759 				/* this is to fake out the allocation routine
1760 				 * into thinking it has to realloc everything
1761 				 * but the recycling logic will let us re-use
1762 				 * the buffers allocated above
1763 				 */
1764 				rx_rings[i].next_to_use = 0;
1765 				rx_rings[i].next_to_clean = 0;
1766 				rx_rings[i].next_to_alloc = 0;
1767 				*vsi->rx_rings[i] = rx_rings[i];
1768 			}
1769 			devm_kfree(&pf->pdev->dev, rx_rings);
1770 		}
1771 
1772 		ice_up(vsi);
1773 	}
1774 	goto done;
1775 
1776 free_tx:
1777 	/* error cleanup if the Rx allocations failed after getting Tx */
1778 	if (tx_rings) {
1779 		for (i = 0; i < vsi->alloc_txq; i++)
1780 			ice_free_tx_ring(&tx_rings[i]);
1781 		devm_kfree(&pf->pdev->dev, tx_rings);
1782 	}
1783 
1784 done:
1785 	clear_bit(__ICE_CFG_BUSY, pf->state);
1786 	return err;
1787 }
1788 
1789 static int ice_nway_reset(struct net_device *netdev)
1790 {
1791 	/* restart autonegotiation */
1792 	struct ice_netdev_priv *np = netdev_priv(netdev);
1793 	struct ice_vsi *vsi = np->vsi;
1794 	struct ice_port_info *pi;
1795 	enum ice_status status;
1796 
1797 	pi = vsi->port_info;
1798 	/* If VSI state is up, then restart autoneg with link up */
1799 	if (!test_bit(__ICE_DOWN, vsi->back->state))
1800 		status = ice_aq_set_link_restart_an(pi, true, NULL);
1801 	else
1802 		status = ice_aq_set_link_restart_an(pi, false, NULL);
1803 
1804 	if (status) {
1805 		netdev_info(netdev, "link restart failed, err %d aq_err %d\n",
1806 			    status, pi->hw->adminq.sq_last_status);
1807 		return -EIO;
1808 	}
1809 
1810 	return 0;
1811 }
1812 
1813 /**
1814  * ice_get_pauseparam - Get Flow Control status
1815  * @netdev: network interface device structure
1816  * @pause: ethernet pause (flow control) parameters
1817  */
1818 static void
1819 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
1820 {
1821 	struct ice_netdev_priv *np = netdev_priv(netdev);
1822 	struct ice_port_info *pi = np->vsi->port_info;
1823 	struct ice_aqc_get_phy_caps_data *pcaps;
1824 	struct ice_vsi *vsi = np->vsi;
1825 	enum ice_status status;
1826 
1827 	/* Initialize pause params */
1828 	pause->rx_pause = 0;
1829 	pause->tx_pause = 0;
1830 
1831 	pcaps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*pcaps),
1832 			     GFP_KERNEL);
1833 	if (!pcaps)
1834 		return;
1835 
1836 	/* Get current phy config */
1837 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1838 				     NULL);
1839 	if (status)
1840 		goto out;
1841 
1842 	pause->autoneg = ((pcaps->caps & ICE_AQC_PHY_AN_MODE) ?
1843 			AUTONEG_ENABLE : AUTONEG_DISABLE);
1844 
1845 	if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
1846 		pause->tx_pause = 1;
1847 	if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
1848 		pause->rx_pause = 1;
1849 
1850 out:
1851 	devm_kfree(&vsi->back->pdev->dev, pcaps);
1852 }
1853 
1854 /**
1855  * ice_set_pauseparam - Set Flow Control parameter
1856  * @netdev: network interface device structure
1857  * @pause: return Tx/Rx flow control status
1858  */
1859 static int
1860 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
1861 {
1862 	struct ice_netdev_priv *np = netdev_priv(netdev);
1863 	struct ice_link_status *hw_link_info;
1864 	struct ice_pf *pf = np->vsi->back;
1865 	struct ice_vsi *vsi = np->vsi;
1866 	struct ice_hw *hw = &pf->hw;
1867 	struct ice_port_info *pi;
1868 	enum ice_status status;
1869 	u8 aq_failures;
1870 	bool link_up;
1871 	int err = 0;
1872 
1873 	pi = vsi->port_info;
1874 	hw_link_info = &pi->phy.link_info;
1875 	link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
1876 
1877 	/* Changing the port's flow control is not supported if this isn't the
1878 	 * PF VSI
1879 	 */
1880 	if (vsi->type != ICE_VSI_PF) {
1881 		netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
1882 		return -EOPNOTSUPP;
1883 	}
1884 
1885 	if (pause->autoneg != (hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
1886 		netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
1887 		return -EOPNOTSUPP;
1888 	}
1889 
1890 	/* If we have link and don't have autoneg */
1891 	if (!test_bit(__ICE_DOWN, pf->state) &&
1892 	    !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
1893 		/* Send message that it might not necessarily work*/
1894 		netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
1895 	}
1896 
1897 	if (pause->rx_pause && pause->tx_pause)
1898 		pi->fc.req_mode = ICE_FC_FULL;
1899 	else if (pause->rx_pause && !pause->tx_pause)
1900 		pi->fc.req_mode = ICE_FC_RX_PAUSE;
1901 	else if (!pause->rx_pause && pause->tx_pause)
1902 		pi->fc.req_mode = ICE_FC_TX_PAUSE;
1903 	else if (!pause->rx_pause && !pause->tx_pause)
1904 		pi->fc.req_mode = ICE_FC_NONE;
1905 	else
1906 		return -EINVAL;
1907 
1908 	/* Tell the OS link is going down, the link will go back up when fw
1909 	 * says it is ready asynchronously
1910 	 */
1911 	ice_print_link_msg(vsi, false);
1912 	netif_carrier_off(netdev);
1913 	netif_tx_stop_all_queues(netdev);
1914 
1915 	/* Set the FC mode and only restart AN if link is up */
1916 	status = ice_set_fc(pi, &aq_failures, link_up);
1917 
1918 	if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
1919 		netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %d\n",
1920 			    status, hw->adminq.sq_last_status);
1921 		err = -EAGAIN;
1922 	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
1923 		netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %d\n",
1924 			    status, hw->adminq.sq_last_status);
1925 		err = -EAGAIN;
1926 	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
1927 		netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %d\n",
1928 			    status, hw->adminq.sq_last_status);
1929 		err = -EAGAIN;
1930 	}
1931 
1932 	if (!test_bit(__ICE_DOWN, pf->state)) {
1933 		/* Give it a little more time to try to come back. If still
1934 		 * down, restart autoneg link or reinitialize the interface.
1935 		 */
1936 		msleep(75);
1937 		if (!test_bit(__ICE_DOWN, pf->state))
1938 			return ice_nway_reset(netdev);
1939 
1940 		ice_down(vsi);
1941 		ice_up(vsi);
1942 	}
1943 
1944 	return err;
1945 }
1946 
1947 /**
1948  * ice_get_rxfh_key_size - get the RSS hash key size
1949  * @netdev: network interface device structure
1950  *
1951  * Returns the table size.
1952  */
1953 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
1954 {
1955 	return ICE_VSIQF_HKEY_ARRAY_SIZE;
1956 }
1957 
1958 /**
1959  * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
1960  * @netdev: network interface device structure
1961  *
1962  * Returns the table size.
1963  */
1964 static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
1965 {
1966 	struct ice_netdev_priv *np = netdev_priv(netdev);
1967 
1968 	return np->vsi->rss_table_size;
1969 }
1970 
1971 /**
1972  * ice_get_rxfh - get the Rx flow hash indirection table
1973  * @netdev: network interface device structure
1974  * @indir: indirection table
1975  * @key: hash key
1976  * @hfunc: hash function
1977  *
1978  * Reads the indirection table directly from the hardware.
1979  */
1980 static int
1981 ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1982 {
1983 	struct ice_netdev_priv *np = netdev_priv(netdev);
1984 	struct ice_vsi *vsi = np->vsi;
1985 	struct ice_pf *pf = vsi->back;
1986 	int ret = 0, i;
1987 	u8 *lut;
1988 
1989 	if (hfunc)
1990 		*hfunc = ETH_RSS_HASH_TOP;
1991 
1992 	if (!indir)
1993 		return 0;
1994 
1995 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
1996 		/* RSS not supported return error here */
1997 		netdev_warn(netdev, "RSS is not configured on this VSI!\n");
1998 		return -EIO;
1999 	}
2000 
2001 	lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
2002 	if (!lut)
2003 		return -ENOMEM;
2004 
2005 	if (ice_get_rss(vsi, key, lut, vsi->rss_table_size)) {
2006 		ret = -EIO;
2007 		goto out;
2008 	}
2009 
2010 	for (i = 0; i < vsi->rss_table_size; i++)
2011 		indir[i] = (u32)(lut[i]);
2012 
2013 out:
2014 	devm_kfree(&pf->pdev->dev, lut);
2015 	return ret;
2016 }
2017 
2018 /**
2019  * ice_set_rxfh - set the Rx flow hash indirection table
2020  * @netdev: network interface device structure
2021  * @indir: indirection table
2022  * @key: hash key
2023  * @hfunc: hash function
2024  *
2025  * Returns -EINVAL if the table specifies an invalid queue id, otherwise
2026  * returns 0 after programming the table.
2027  */
2028 static int
2029 ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key,
2030 	     const u8 hfunc)
2031 {
2032 	struct ice_netdev_priv *np = netdev_priv(netdev);
2033 	struct ice_vsi *vsi = np->vsi;
2034 	struct ice_pf *pf = vsi->back;
2035 	u8 *seed = NULL;
2036 
2037 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
2038 		return -EOPNOTSUPP;
2039 
2040 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2041 		/* RSS not supported return error here */
2042 		netdev_warn(netdev, "RSS is not configured on this VSI!\n");
2043 		return -EIO;
2044 	}
2045 
2046 	if (key) {
2047 		if (!vsi->rss_hkey_user) {
2048 			vsi->rss_hkey_user =
2049 				devm_kzalloc(&pf->pdev->dev,
2050 					     ICE_VSIQF_HKEY_ARRAY_SIZE,
2051 					     GFP_KERNEL);
2052 			if (!vsi->rss_hkey_user)
2053 				return -ENOMEM;
2054 		}
2055 		memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
2056 		seed = vsi->rss_hkey_user;
2057 	}
2058 
2059 	if (!vsi->rss_lut_user) {
2060 		vsi->rss_lut_user = devm_kzalloc(&pf->pdev->dev,
2061 						 vsi->rss_table_size,
2062 						 GFP_KERNEL);
2063 		if (!vsi->rss_lut_user)
2064 			return -ENOMEM;
2065 	}
2066 
2067 	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
2068 	if (indir) {
2069 		int i;
2070 
2071 		for (i = 0; i < vsi->rss_table_size; i++)
2072 			vsi->rss_lut_user[i] = (u8)(indir[i]);
2073 	} else {
2074 		ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
2075 				 vsi->rss_size);
2076 	}
2077 
2078 	if (ice_set_rss(vsi, seed, vsi->rss_lut_user, vsi->rss_table_size))
2079 		return -EIO;
2080 
2081 	return 0;
2082 }
2083 
2084 enum ice_container_type {
2085 	ICE_RX_CONTAINER,
2086 	ICE_TX_CONTAINER,
2087 };
2088 
2089 /**
2090  * ice_get_rc_coalesce - get ITR values for specific ring container
2091  * @ec: ethtool structure to fill with driver's coalesce settings
2092  * @c_type: container type, RX or TX
2093  * @rc: ring container that the ITR values will come from
2094  *
2095  * Query the device for ice_ring_container specific ITR values. This is
2096  * done per ice_ring_container because each q_vector can have 1 or more rings
2097  * and all of said ring(s) will have the same ITR values.
2098  *
2099  * Returns 0 on success, negative otherwise.
2100  */
2101 static int
2102 ice_get_rc_coalesce(struct ethtool_coalesce *ec, enum ice_container_type c_type,
2103 		    struct ice_ring_container *rc)
2104 {
2105 	struct ice_pf *pf = rc->ring->vsi->back;
2106 
2107 	switch (c_type) {
2108 	case ICE_RX_CONTAINER:
2109 		ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
2110 		ec->rx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
2111 		break;
2112 	case ICE_TX_CONTAINER:
2113 		ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
2114 		ec->tx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
2115 		break;
2116 	default:
2117 		dev_dbg(&pf->pdev->dev, "Invalid c_type %d\n", c_type);
2118 		return -EINVAL;
2119 	}
2120 
2121 	return 0;
2122 }
2123 
2124 /**
2125  * __ice_get_coalesce - get ITR/INTRL values for the device
2126  * @netdev: pointer to the netdev associated with this query
2127  * @ec: ethtool structure to fill with driver's coalesce settings
2128  * @q_num: queue number to get the coalesce settings for
2129  */
2130 static int
2131 __ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
2132 		   int q_num)
2133 {
2134 	struct ice_netdev_priv *np = netdev_priv(netdev);
2135 	int tx = -EINVAL, rx = -EINVAL;
2136 	struct ice_vsi *vsi = np->vsi;
2137 
2138 	if (q_num < 0) {
2139 		rx = ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
2140 					 &vsi->rx_rings[0]->q_vector->rx);
2141 		tx = ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
2142 					 &vsi->tx_rings[0]->q_vector->tx);
2143 
2144 		goto update_coalesced_frames;
2145 	}
2146 
2147 	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
2148 		rx = ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
2149 					 &vsi->rx_rings[q_num]->q_vector->rx);
2150 		tx = ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
2151 					 &vsi->tx_rings[q_num]->q_vector->tx);
2152 	} else if (q_num < vsi->num_rxq) {
2153 		rx = ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
2154 					 &vsi->rx_rings[q_num]->q_vector->rx);
2155 	} else if (q_num < vsi->num_txq) {
2156 		tx = ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
2157 					 &vsi->tx_rings[q_num]->q_vector->tx);
2158 	} else {
2159 		/* q_num is invalid for both Rx and Tx queues */
2160 		return -EINVAL;
2161 	}
2162 
2163 update_coalesced_frames:
2164 	/* either q_num is invalid for both Rx and Tx queues or setting coalesce
2165 	 * failed completely
2166 	 */
2167 	if (tx && rx)
2168 		return -EINVAL;
2169 
2170 	if (q_num < vsi->num_txq)
2171 		ec->tx_max_coalesced_frames_irq = vsi->work_lmt;
2172 
2173 	if (q_num < vsi->num_rxq)
2174 		ec->rx_max_coalesced_frames_irq = vsi->work_lmt;
2175 
2176 	return 0;
2177 }
2178 
2179 static int
2180 ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
2181 {
2182 	return __ice_get_coalesce(netdev, ec, -1);
2183 }
2184 
2185 static int
2186 ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
2187 		       struct ethtool_coalesce *ec)
2188 {
2189 	return __ice_get_coalesce(netdev, ec, q_num);
2190 }
2191 
2192 /**
2193  * ice_set_rc_coalesce - set ITR values for specific ring container
2194  * @c_type: container type, RX or TX
2195  * @ec: ethtool structure from user to update ITR settings
2196  * @rc: ring container that the ITR values will come from
2197  * @vsi: VSI associated to the ring container
2198  *
2199  * Set specific ITR values. This is done per ice_ring_container because each
2200  * q_vector can have 1 or more rings and all of said ring(s) will have the same
2201  * ITR values.
2202  *
2203  * Returns 0 on success, negative otherwise.
2204  */
2205 static int
2206 ice_set_rc_coalesce(enum ice_container_type c_type, struct ethtool_coalesce *ec,
2207 		    struct ice_ring_container *rc, struct ice_vsi *vsi)
2208 {
2209 	struct ice_pf *pf = vsi->back;
2210 	u16 itr_setting;
2211 
2212 	if (!rc->ring)
2213 		return -EINVAL;
2214 
2215 	itr_setting = rc->itr_setting & ~ICE_ITR_DYNAMIC;
2216 
2217 	switch (c_type) {
2218 	case ICE_RX_CONTAINER:
2219 		if (ec->rx_coalesce_usecs != itr_setting &&
2220 		    ec->use_adaptive_rx_coalesce) {
2221 			netdev_info(vsi->netdev,
2222 				    "Rx interrupt throttling cannot be changed if adaptive-rx is enabled\n");
2223 			return -EINVAL;
2224 		}
2225 
2226 		if (ec->rx_coalesce_usecs > ICE_ITR_MAX) {
2227 			netdev_info(vsi->netdev,
2228 				    "Invalid value, rx-usecs range is 0-%d\n",
2229 				   ICE_ITR_MAX);
2230 			return -EINVAL;
2231 		}
2232 
2233 		if (ec->use_adaptive_rx_coalesce) {
2234 			rc->itr_setting |= ICE_ITR_DYNAMIC;
2235 		} else {
2236 			rc->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
2237 			rc->target_itr = ITR_TO_REG(rc->itr_setting);
2238 		}
2239 		break;
2240 	case ICE_TX_CONTAINER:
2241 		if (ec->tx_coalesce_usecs != itr_setting &&
2242 		    ec->use_adaptive_tx_coalesce) {
2243 			netdev_info(vsi->netdev,
2244 				    "Tx interrupt throttling cannot be changed if adaptive-tx is enabled\n");
2245 			return -EINVAL;
2246 		}
2247 
2248 		if (ec->tx_coalesce_usecs > ICE_ITR_MAX) {
2249 			netdev_info(vsi->netdev,
2250 				    "Invalid value, tx-usecs range is 0-%d\n",
2251 				   ICE_ITR_MAX);
2252 			return -EINVAL;
2253 		}
2254 
2255 		if (ec->use_adaptive_tx_coalesce) {
2256 			rc->itr_setting |= ICE_ITR_DYNAMIC;
2257 		} else {
2258 			rc->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
2259 			rc->target_itr = ITR_TO_REG(rc->itr_setting);
2260 		}
2261 		break;
2262 	default:
2263 		dev_dbg(&pf->pdev->dev, "Invalid container type %d\n", c_type);
2264 		return -EINVAL;
2265 	}
2266 
2267 	return 0;
2268 }
2269 
2270 static int
2271 __ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
2272 		   int q_num)
2273 {
2274 	struct ice_netdev_priv *np = netdev_priv(netdev);
2275 	int rx = -EINVAL, tx = -EINVAL;
2276 	struct ice_vsi *vsi = np->vsi;
2277 
2278 	if (q_num < 0) {
2279 		int i;
2280 
2281 		ice_for_each_q_vector(vsi, i) {
2282 			struct ice_q_vector *q_vector = vsi->q_vectors[i];
2283 
2284 			if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
2285 						&q_vector->rx, vsi) ||
2286 			    ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
2287 						&q_vector->tx, vsi))
2288 				return -EINVAL;
2289 		}
2290 
2291 		goto set_work_lmt;
2292 	}
2293 
2294 	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
2295 		rx = ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
2296 					 &vsi->rx_rings[q_num]->q_vector->rx,
2297 					 vsi);
2298 		tx = ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
2299 					 &vsi->tx_rings[q_num]->q_vector->tx,
2300 					 vsi);
2301 	} else if (q_num < vsi->num_rxq) {
2302 		rx = ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
2303 					 &vsi->rx_rings[q_num]->q_vector->rx,
2304 					 vsi);
2305 	} else if (q_num < vsi->num_txq) {
2306 		tx  = ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
2307 					  &vsi->tx_rings[q_num]->q_vector->tx,
2308 					  vsi);
2309 	}
2310 
2311 	/* either q_num is invalid for both Rx and Tx queues or setting coalesce
2312 	 * failed completely
2313 	 */
2314 	if (rx && tx)
2315 		return -EINVAL;
2316 
2317 set_work_lmt:
2318 	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
2319 		vsi->work_lmt = max(ec->tx_max_coalesced_frames_irq,
2320 				    ec->rx_max_coalesced_frames_irq);
2321 
2322 	return 0;
2323 }
2324 
2325 static int
2326 ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
2327 {
2328 	return __ice_set_coalesce(netdev, ec, -1);
2329 }
2330 
2331 static int
2332 ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
2333 		       struct ethtool_coalesce *ec)
2334 {
2335 	return __ice_set_coalesce(netdev, ec, q_num);
2336 }
2337 
2338 static const struct ethtool_ops ice_ethtool_ops = {
2339 	.get_link_ksettings	= ice_get_link_ksettings,
2340 	.set_link_ksettings	= ice_set_link_ksettings,
2341 	.get_drvinfo            = ice_get_drvinfo,
2342 	.get_regs_len           = ice_get_regs_len,
2343 	.get_regs               = ice_get_regs,
2344 	.get_msglevel           = ice_get_msglevel,
2345 	.set_msglevel           = ice_set_msglevel,
2346 	.get_link		= ethtool_op_get_link,
2347 	.get_eeprom_len		= ice_get_eeprom_len,
2348 	.get_eeprom		= ice_get_eeprom,
2349 	.get_coalesce		= ice_get_coalesce,
2350 	.set_coalesce		= ice_set_coalesce,
2351 	.get_strings		= ice_get_strings,
2352 	.set_phys_id		= ice_set_phys_id,
2353 	.get_ethtool_stats      = ice_get_ethtool_stats,
2354 	.get_priv_flags		= ice_get_priv_flags,
2355 	.set_priv_flags		= ice_set_priv_flags,
2356 	.get_sset_count		= ice_get_sset_count,
2357 	.get_rxnfc		= ice_get_rxnfc,
2358 	.get_ringparam		= ice_get_ringparam,
2359 	.set_ringparam		= ice_set_ringparam,
2360 	.nway_reset		= ice_nway_reset,
2361 	.get_pauseparam		= ice_get_pauseparam,
2362 	.set_pauseparam		= ice_set_pauseparam,
2363 	.get_rxfh_key_size	= ice_get_rxfh_key_size,
2364 	.get_rxfh_indir_size	= ice_get_rxfh_indir_size,
2365 	.get_rxfh		= ice_get_rxfh,
2366 	.set_rxfh		= ice_set_rxfh,
2367 	.get_ts_info		= ethtool_op_get_ts_info,
2368 	.get_per_queue_coalesce = ice_get_per_q_coalesce,
2369 	.set_per_queue_coalesce = ice_set_per_q_coalesce,
2370 };
2371 
2372 /**
2373  * ice_set_ethtool_ops - setup netdev ethtool ops
2374  * @netdev: network interface device structure
2375  *
2376  * setup netdev ethtool ops with ice specific ops
2377  */
2378 void ice_set_ethtool_ops(struct net_device *netdev)
2379 {
2380 	netdev->ethtool_ops = &ice_ethtool_ops;
2381 }
2382