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