xref: /linux/drivers/net/ethernet/amd/xgbe/xgbe-ethtool.c (revision fb7399cf2d0b33825b8039f95c45395c7deba25c)
1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-3-Clause)
2 /*
3  * Copyright (c) 2014-2025, Advanced Micro Devices, Inc.
4  * Copyright (c) 2014, Synopsys, Inc.
5  * All rights reserved
6  */
7 
8 #include <linux/spinlock.h>
9 #include <linux/phy.h>
10 #include <linux/net_tstamp.h>
11 
12 #include "xgbe.h"
13 #include "xgbe-common.h"
14 
15 struct xgbe_stats {
16 	char stat_string[ETH_GSTRING_LEN];
17 	int stat_size;
18 	int stat_offset;
19 };
20 
21 #define XGMAC_MMC_STAT(_string, _var)				\
22 	{ _string,						\
23 	  sizeof_field(struct xgbe_mmc_stats, _var),		\
24 	  offsetof(struct xgbe_prv_data, mmc_stats._var),	\
25 	}
26 
27 #define XGMAC_EXT_STAT(_string, _var)				\
28 	{ _string,						\
29 	  sizeof_field(struct xgbe_ext_stats, _var),		\
30 	  offsetof(struct xgbe_prv_data, ext_stats._var),	\
31 	}
32 
33 static const struct xgbe_stats xgbe_gstring_stats[] = {
34 	XGMAC_MMC_STAT("tx_bytes", txoctetcount_gb),
35 	XGMAC_MMC_STAT("tx_packets", txframecount_gb),
36 	XGMAC_MMC_STAT("tx_unicast_packets", txunicastframes_gb),
37 	XGMAC_MMC_STAT("tx_broadcast_packets", txbroadcastframes_gb),
38 	XGMAC_MMC_STAT("tx_multicast_packets", txmulticastframes_gb),
39 	XGMAC_MMC_STAT("tx_vlan_packets", txvlanframes_g),
40 	XGMAC_EXT_STAT("tx_vxlan_packets", tx_vxlan_packets),
41 	XGMAC_EXT_STAT("tx_tso_packets", tx_tso_packets),
42 	XGMAC_MMC_STAT("tx_64_byte_packets", tx64octets_gb),
43 	XGMAC_MMC_STAT("tx_65_to_127_byte_packets", tx65to127octets_gb),
44 	XGMAC_MMC_STAT("tx_128_to_255_byte_packets", tx128to255octets_gb),
45 	XGMAC_MMC_STAT("tx_256_to_511_byte_packets", tx256to511octets_gb),
46 	XGMAC_MMC_STAT("tx_512_to_1023_byte_packets", tx512to1023octets_gb),
47 	XGMAC_MMC_STAT("tx_1024_to_max_byte_packets", tx1024tomaxoctets_gb),
48 	XGMAC_MMC_STAT("tx_underflow_errors", txunderflowerror),
49 	XGMAC_MMC_STAT("tx_pause_frames", txpauseframes),
50 
51 	XGMAC_MMC_STAT("rx_bytes", rxoctetcount_gb),
52 	XGMAC_MMC_STAT("rx_packets", rxframecount_gb),
53 	XGMAC_MMC_STAT("rx_unicast_packets", rxunicastframes_g),
54 	XGMAC_MMC_STAT("rx_broadcast_packets", rxbroadcastframes_g),
55 	XGMAC_MMC_STAT("rx_multicast_packets", rxmulticastframes_g),
56 	XGMAC_MMC_STAT("rx_vlan_packets", rxvlanframes_gb),
57 	XGMAC_EXT_STAT("rx_vxlan_packets", rx_vxlan_packets),
58 	XGMAC_MMC_STAT("rx_64_byte_packets", rx64octets_gb),
59 	XGMAC_MMC_STAT("rx_65_to_127_byte_packets", rx65to127octets_gb),
60 	XGMAC_MMC_STAT("rx_128_to_255_byte_packets", rx128to255octets_gb),
61 	XGMAC_MMC_STAT("rx_256_to_511_byte_packets", rx256to511octets_gb),
62 	XGMAC_MMC_STAT("rx_512_to_1023_byte_packets", rx512to1023octets_gb),
63 	XGMAC_MMC_STAT("rx_1024_to_max_byte_packets", rx1024tomaxoctets_gb),
64 	XGMAC_MMC_STAT("rx_undersize_packets", rxundersize_g),
65 	XGMAC_MMC_STAT("rx_oversize_packets", rxoversize_g),
66 	XGMAC_MMC_STAT("rx_crc_errors", rxcrcerror),
67 	XGMAC_MMC_STAT("rx_crc_errors_small_packets", rxrunterror),
68 	XGMAC_MMC_STAT("rx_crc_errors_giant_packets", rxjabbererror),
69 	XGMAC_MMC_STAT("rx_length_errors", rxlengtherror),
70 	XGMAC_MMC_STAT("rx_out_of_range_errors", rxoutofrangetype),
71 	XGMAC_MMC_STAT("rx_fifo_overflow_errors", rxfifooverflow),
72 	XGMAC_MMC_STAT("rx_watchdog_errors", rxwatchdogerror),
73 	XGMAC_EXT_STAT("rx_csum_errors", rx_csum_errors),
74 	XGMAC_EXT_STAT("rx_vxlan_csum_errors", rx_vxlan_csum_errors),
75 	XGMAC_MMC_STAT("rx_pause_frames", rxpauseframes),
76 	XGMAC_EXT_STAT("rx_split_header_packets", rx_split_header_packets),
77 	XGMAC_EXT_STAT("rx_buffer_unavailable", rx_buffer_unavailable),
78 };
79 
80 #define XGBE_STATS_COUNT	ARRAY_SIZE(xgbe_gstring_stats)
81 
82 static void xgbe_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
83 {
84 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
85 	int i;
86 
87 	switch (stringset) {
88 	case ETH_SS_STATS:
89 		for (i = 0; i < XGBE_STATS_COUNT; i++)
90 			ethtool_puts(&data, xgbe_gstring_stats[i].stat_string);
91 
92 		for (i = 0; i < pdata->tx_ring_count; i++) {
93 			ethtool_sprintf(&data, "txq_%u_packets", i);
94 			ethtool_sprintf(&data, "txq_%u_bytes", i);
95 		}
96 
97 		for (i = 0; i < pdata->rx_ring_count; i++) {
98 			ethtool_sprintf(&data, "rxq_%u_packets", i);
99 			ethtool_sprintf(&data, "rxq_%u_bytes", i);
100 		}
101 
102 		break;
103 	}
104 }
105 
106 static void xgbe_get_ethtool_stats(struct net_device *netdev,
107 				   struct ethtool_stats *stats, u64 *data)
108 {
109 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
110 	u8 *stat;
111 	int i;
112 
113 	pdata->hw_if.read_mmc_stats(pdata);
114 	for (i = 0; i < XGBE_STATS_COUNT; i++) {
115 		stat = (u8 *)pdata + xgbe_gstring_stats[i].stat_offset;
116 		*data++ = *(u64 *)stat;
117 	}
118 	for (i = 0; i < pdata->tx_ring_count; i++) {
119 		*data++ = pdata->ext_stats.txq_packets[i];
120 		*data++ = pdata->ext_stats.txq_bytes[i];
121 	}
122 	for (i = 0; i < pdata->rx_ring_count; i++) {
123 		*data++ = pdata->ext_stats.rxq_packets[i];
124 		*data++ = pdata->ext_stats.rxq_bytes[i];
125 	}
126 }
127 
128 static int xgbe_get_sset_count(struct net_device *netdev, int stringset)
129 {
130 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
131 	int ret;
132 
133 	switch (stringset) {
134 	case ETH_SS_STATS:
135 		ret = XGBE_STATS_COUNT +
136 		      (pdata->tx_ring_count * 2) +
137 		      (pdata->rx_ring_count * 2);
138 		break;
139 
140 	default:
141 		ret = -EOPNOTSUPP;
142 	}
143 
144 	return ret;
145 }
146 
147 static void xgbe_get_pauseparam(struct net_device *netdev,
148 				struct ethtool_pauseparam *pause)
149 {
150 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
151 
152 	pause->autoneg = pdata->phy.pause_autoneg;
153 	pause->tx_pause = pdata->phy.tx_pause;
154 	pause->rx_pause = pdata->phy.rx_pause;
155 }
156 
157 static int xgbe_set_pauseparam(struct net_device *netdev,
158 			       struct ethtool_pauseparam *pause)
159 {
160 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
161 	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
162 	int ret = 0;
163 
164 	if (pause->autoneg && (pdata->phy.autoneg != AUTONEG_ENABLE)) {
165 		netdev_err(netdev,
166 			   "autoneg disabled, pause autoneg not available\n");
167 		return -EINVAL;
168 	}
169 
170 	pdata->phy.pause_autoneg = pause->autoneg;
171 	pdata->phy.tx_pause = pause->tx_pause;
172 	pdata->phy.rx_pause = pause->rx_pause;
173 
174 	XGBE_CLR_ADV(lks, Pause);
175 	XGBE_CLR_ADV(lks, Asym_Pause);
176 
177 	if (pause->rx_pause) {
178 		XGBE_SET_ADV(lks, Pause);
179 		XGBE_SET_ADV(lks, Asym_Pause);
180 	}
181 
182 	if (pause->tx_pause) {
183 		/* Equivalent to XOR of Asym_Pause */
184 		if (XGBE_ADV(lks, Asym_Pause))
185 			XGBE_CLR_ADV(lks, Asym_Pause);
186 		else
187 			XGBE_SET_ADV(lks, Asym_Pause);
188 	}
189 
190 	if (netif_running(netdev))
191 		ret = pdata->phy_if.phy_config_aneg(pdata);
192 
193 	return ret;
194 }
195 
196 static int xgbe_get_link_ksettings(struct net_device *netdev,
197 				   struct ethtool_link_ksettings *cmd)
198 {
199 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
200 	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
201 
202 	cmd->base.phy_address = pdata->phy.address;
203 
204 	if (netif_carrier_ok(netdev)) {
205 		cmd->base.speed = pdata->phy.speed;
206 		cmd->base.duplex = pdata->phy.duplex;
207 	} else {
208 		cmd->base.speed = SPEED_UNKNOWN;
209 		cmd->base.duplex = DUPLEX_UNKNOWN;
210 	}
211 
212 	cmd->base.autoneg = pdata->phy.autoneg;
213 	cmd->base.port = PORT_NONE;
214 
215 	XGBE_LM_COPY(cmd, supported, lks, supported);
216 	XGBE_LM_COPY(cmd, advertising, lks, advertising);
217 	XGBE_LM_COPY(cmd, lp_advertising, lks, lp_advertising);
218 
219 	return 0;
220 }
221 
222 static int xgbe_set_link_ksettings(struct net_device *netdev,
223 				   const struct ethtool_link_ksettings *cmd)
224 {
225 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
226 	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
227 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
228 	u32 speed;
229 	int ret;
230 
231 	speed = cmd->base.speed;
232 
233 	if (cmd->base.phy_address != pdata->phy.address) {
234 		netdev_err(netdev, "invalid phy address %hhu\n",
235 			   cmd->base.phy_address);
236 		return -EINVAL;
237 	}
238 
239 	if ((cmd->base.autoneg != AUTONEG_ENABLE) &&
240 	    (cmd->base.autoneg != AUTONEG_DISABLE)) {
241 		netdev_err(netdev, "unsupported autoneg %hhu\n",
242 			   cmd->base.autoneg);
243 		return -EINVAL;
244 	}
245 
246 	if (cmd->base.autoneg == AUTONEG_DISABLE) {
247 		if (!pdata->phy_if.phy_valid_speed(pdata, speed)) {
248 			netdev_err(netdev, "unsupported speed %u\n", speed);
249 			return -EINVAL;
250 		}
251 
252 		if (cmd->base.duplex != DUPLEX_FULL) {
253 			netdev_err(netdev, "unsupported duplex %hhu\n",
254 				   cmd->base.duplex);
255 			return -EINVAL;
256 		}
257 	}
258 
259 	netif_dbg(pdata, link, netdev,
260 		  "requested advertisement 0x%*pb, phy supported 0x%*pb\n",
261 		  __ETHTOOL_LINK_MODE_MASK_NBITS, cmd->link_modes.advertising,
262 		  __ETHTOOL_LINK_MODE_MASK_NBITS, lks->link_modes.supported);
263 
264 	linkmode_and(advertising, cmd->link_modes.advertising,
265 		     lks->link_modes.supported);
266 
267 	if ((cmd->base.autoneg == AUTONEG_ENABLE) &&
268 	    bitmap_empty(advertising, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
269 		netdev_err(netdev,
270 			   "unsupported requested advertisement\n");
271 		return -EINVAL;
272 	}
273 
274 	ret = 0;
275 	pdata->phy.autoneg = cmd->base.autoneg;
276 	pdata->phy.speed = speed;
277 	pdata->phy.duplex = cmd->base.duplex;
278 	linkmode_copy(lks->link_modes.advertising, advertising);
279 
280 	if (cmd->base.autoneg == AUTONEG_ENABLE)
281 		XGBE_SET_ADV(lks, Autoneg);
282 	else
283 		XGBE_CLR_ADV(lks, Autoneg);
284 
285 	if (netif_running(netdev))
286 		ret = pdata->phy_if.phy_config_aneg(pdata);
287 
288 	return ret;
289 }
290 
291 static void xgbe_get_drvinfo(struct net_device *netdev,
292 			     struct ethtool_drvinfo *drvinfo)
293 {
294 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
295 	struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
296 
297 	strscpy(drvinfo->driver, XGBE_DRV_NAME, sizeof(drvinfo->driver));
298 	strscpy(drvinfo->bus_info, dev_name(pdata->dev),
299 		sizeof(drvinfo->bus_info));
300 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d.%d.%d",
301 		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, USERVER),
302 		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, DEVID),
303 		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, SNPSVER));
304 }
305 
306 static u32 xgbe_get_msglevel(struct net_device *netdev)
307 {
308 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
309 
310 	return pdata->msg_enable;
311 }
312 
313 static void xgbe_set_msglevel(struct net_device *netdev, u32 msglevel)
314 {
315 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
316 
317 	pdata->msg_enable = msglevel;
318 }
319 
320 static int xgbe_get_coalesce(struct net_device *netdev,
321 			     struct ethtool_coalesce *ec,
322 			     struct kernel_ethtool_coalesce *kernel_coal,
323 			     struct netlink_ext_ack *extack)
324 {
325 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
326 
327 	memset(ec, 0, sizeof(struct ethtool_coalesce));
328 
329 	ec->rx_coalesce_usecs = pdata->rx_usecs;
330 	ec->rx_max_coalesced_frames = pdata->rx_frames;
331 
332 	ec->tx_max_coalesced_frames = pdata->tx_frames;
333 
334 	return 0;
335 }
336 
337 static int xgbe_set_coalesce(struct net_device *netdev,
338 			     struct ethtool_coalesce *ec,
339 			     struct kernel_ethtool_coalesce *kernel_coal,
340 			     struct netlink_ext_ack *extack)
341 {
342 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
343 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
344 	unsigned int rx_frames, rx_riwt, rx_usecs;
345 	unsigned int tx_frames;
346 
347 	rx_riwt = hw_if->usec_to_riwt(pdata, ec->rx_coalesce_usecs);
348 	rx_usecs = ec->rx_coalesce_usecs;
349 	rx_frames = ec->rx_max_coalesced_frames;
350 
351 	/* Use smallest possible value if conversion resulted in zero */
352 	if (rx_usecs && !rx_riwt)
353 		rx_riwt = 1;
354 
355 	/* Check the bounds of values for Rx */
356 	if (rx_riwt > XGMAC_MAX_DMA_RIWT) {
357 		netdev_err(netdev, "rx-usec is limited to %d usecs\n",
358 			   hw_if->riwt_to_usec(pdata, XGMAC_MAX_DMA_RIWT));
359 		return -EINVAL;
360 	}
361 	if (rx_frames > pdata->rx_desc_count) {
362 		netdev_err(netdev, "rx-frames is limited to %d frames\n",
363 			   pdata->rx_desc_count);
364 		return -EINVAL;
365 	}
366 
367 	tx_frames = ec->tx_max_coalesced_frames;
368 
369 	/* Check the bounds of values for Tx */
370 	if (tx_frames > pdata->tx_desc_count) {
371 		netdev_err(netdev, "tx-frames is limited to %d frames\n",
372 			   pdata->tx_desc_count);
373 		return -EINVAL;
374 	}
375 
376 	pdata->rx_riwt = rx_riwt;
377 	pdata->rx_usecs = rx_usecs;
378 	pdata->rx_frames = rx_frames;
379 	hw_if->config_rx_coalesce(pdata);
380 
381 	pdata->tx_frames = tx_frames;
382 	hw_if->config_tx_coalesce(pdata);
383 
384 	return 0;
385 }
386 
387 static int xgbe_get_rxnfc(struct net_device *netdev,
388 			  struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
389 {
390 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
391 
392 	switch (rxnfc->cmd) {
393 	case ETHTOOL_GRXRINGS:
394 		rxnfc->data = pdata->rx_ring_count;
395 		break;
396 	default:
397 		return -EOPNOTSUPP;
398 	}
399 
400 	return 0;
401 }
402 
403 static u32 xgbe_get_rxfh_key_size(struct net_device *netdev)
404 {
405 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
406 
407 	return sizeof(pdata->rss_key);
408 }
409 
410 static u32 xgbe_get_rxfh_indir_size(struct net_device *netdev)
411 {
412 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
413 
414 	return ARRAY_SIZE(pdata->rss_table);
415 }
416 
417 static int xgbe_get_rxfh(struct net_device *netdev,
418 			 struct ethtool_rxfh_param *rxfh)
419 {
420 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
421 	unsigned int i;
422 
423 	if (rxfh->indir) {
424 		for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
425 			rxfh->indir[i] = XGMAC_GET_BITS(pdata->rss_table[i],
426 							MAC_RSSDR, DMCH);
427 	}
428 
429 	if (rxfh->key)
430 		memcpy(rxfh->key, pdata->rss_key, sizeof(pdata->rss_key));
431 
432 	rxfh->hfunc = ETH_RSS_HASH_TOP;
433 
434 	return 0;
435 }
436 
437 static int xgbe_set_rxfh(struct net_device *netdev,
438 			 struct ethtool_rxfh_param *rxfh,
439 			 struct netlink_ext_ack *extack)
440 {
441 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
442 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
443 	unsigned int ret;
444 
445 	if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
446 	    rxfh->hfunc != ETH_RSS_HASH_TOP) {
447 		netdev_err(netdev, "unsupported hash function\n");
448 		return -EOPNOTSUPP;
449 	}
450 
451 	if (rxfh->indir) {
452 		ret = hw_if->set_rss_lookup_table(pdata, rxfh->indir);
453 		if (ret)
454 			return ret;
455 	}
456 
457 	if (rxfh->key) {
458 		ret = hw_if->set_rss_hash_key(pdata, rxfh->key);
459 		if (ret)
460 			return ret;
461 	}
462 
463 	return 0;
464 }
465 
466 static int xgbe_get_ts_info(struct net_device *netdev,
467 			    struct kernel_ethtool_ts_info *ts_info)
468 {
469 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
470 
471 	ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
472 				   SOF_TIMESTAMPING_TX_HARDWARE |
473 				   SOF_TIMESTAMPING_RX_HARDWARE |
474 				   SOF_TIMESTAMPING_RAW_HARDWARE;
475 
476 	if (pdata->ptp_clock)
477 		ts_info->phc_index = ptp_clock_index(pdata->ptp_clock);
478 
479 	ts_info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
480 	ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
481 			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
482 			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
483 			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
484 			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
485 			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
486 			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
487 			      (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
488 			      (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
489 			      (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
490 			      (1 << HWTSTAMP_FILTER_ALL);
491 
492 	return 0;
493 }
494 
495 static int xgbe_get_module_info(struct net_device *netdev,
496 				struct ethtool_modinfo *modinfo)
497 {
498 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
499 
500 	return pdata->phy_if.module_info(pdata, modinfo);
501 }
502 
503 static int xgbe_get_module_eeprom(struct net_device *netdev,
504 				  struct ethtool_eeprom *eeprom, u8 *data)
505 {
506 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
507 
508 	return pdata->phy_if.module_eeprom(pdata, eeprom, data);
509 }
510 
511 static void
512 xgbe_get_ringparam(struct net_device *netdev,
513 		   struct ethtool_ringparam *ringparam,
514 		   struct kernel_ethtool_ringparam *kernel_ringparam,
515 		   struct netlink_ext_ack *extack)
516 {
517 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
518 
519 	ringparam->rx_max_pending = XGBE_RX_DESC_CNT_MAX;
520 	ringparam->tx_max_pending = XGBE_TX_DESC_CNT_MAX;
521 	ringparam->rx_pending = pdata->rx_desc_count;
522 	ringparam->tx_pending = pdata->tx_desc_count;
523 }
524 
525 static int xgbe_set_ringparam(struct net_device *netdev,
526 			      struct ethtool_ringparam *ringparam,
527 			      struct kernel_ethtool_ringparam *kernel_ringparam,
528 			      struct netlink_ext_ack *extack)
529 {
530 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
531 	unsigned int rx, tx;
532 
533 	if (ringparam->rx_mini_pending || ringparam->rx_jumbo_pending) {
534 		netdev_err(netdev, "unsupported ring parameter\n");
535 		return -EINVAL;
536 	}
537 
538 	if ((ringparam->rx_pending < XGBE_RX_DESC_CNT_MIN) ||
539 	    (ringparam->rx_pending > XGBE_RX_DESC_CNT_MAX)) {
540 		netdev_err(netdev,
541 			   "rx ring parameter must be between %u and %u\n",
542 			   XGBE_RX_DESC_CNT_MIN, XGBE_RX_DESC_CNT_MAX);
543 		return -EINVAL;
544 	}
545 
546 	if ((ringparam->tx_pending < XGBE_TX_DESC_CNT_MIN) ||
547 	    (ringparam->tx_pending > XGBE_TX_DESC_CNT_MAX)) {
548 		netdev_err(netdev,
549 			   "tx ring parameter must be between %u and %u\n",
550 			   XGBE_TX_DESC_CNT_MIN, XGBE_TX_DESC_CNT_MAX);
551 		return -EINVAL;
552 	}
553 
554 	rx = __rounddown_pow_of_two(ringparam->rx_pending);
555 	if (rx != ringparam->rx_pending)
556 		netdev_notice(netdev,
557 			      "rx ring parameter rounded to power of two: %u\n",
558 			      rx);
559 
560 	tx = __rounddown_pow_of_two(ringparam->tx_pending);
561 	if (tx != ringparam->tx_pending)
562 		netdev_notice(netdev,
563 			      "tx ring parameter rounded to power of two: %u\n",
564 			      tx);
565 
566 	if ((rx == pdata->rx_desc_count) &&
567 	    (tx == pdata->tx_desc_count))
568 		goto out;
569 
570 	pdata->rx_desc_count = rx;
571 	pdata->tx_desc_count = tx;
572 
573 	xgbe_restart_dev(pdata);
574 
575 out:
576 	return 0;
577 }
578 
579 static void xgbe_get_channels(struct net_device *netdev,
580 			      struct ethtool_channels *channels)
581 {
582 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
583 	unsigned int rx, tx, combined;
584 
585 	/* Calculate maximums allowed:
586 	 *   - Take into account the number of available IRQs
587 	 *   - Do not take into account the number of online CPUs so that
588 	 *     the user can over-subscribe if desired
589 	 *   - Tx is additionally limited by the number of hardware queues
590 	 */
591 	rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
592 	rx = min(rx, pdata->channel_irq_count);
593 	tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
594 	tx = min(tx, pdata->channel_irq_count);
595 	tx = min(tx, pdata->tx_max_q_count);
596 
597 	combined = min(rx, tx);
598 
599 	channels->max_combined = combined;
600 	channels->max_rx = rx ? rx - 1 : 0;
601 	channels->max_tx = tx ? tx - 1 : 0;
602 
603 	/* Get current settings based on device state */
604 	rx = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
605 	tx = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
606 
607 	combined = min(rx, tx);
608 	rx -= combined;
609 	tx -= combined;
610 
611 	channels->combined_count = combined;
612 	channels->rx_count = rx;
613 	channels->tx_count = tx;
614 }
615 
616 static void xgbe_print_set_channels_input(struct net_device *netdev,
617 					  struct ethtool_channels *channels)
618 {
619 	netdev_err(netdev, "channel inputs: combined=%u, rx-only=%u, tx-only=%u\n",
620 		   channels->combined_count, channels->rx_count,
621 		   channels->tx_count);
622 }
623 
624 static int xgbe_set_channels(struct net_device *netdev,
625 			     struct ethtool_channels *channels)
626 {
627 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
628 	unsigned int rx, rx_curr, tx, tx_curr, combined;
629 
630 	/* Calculate maximums allowed:
631 	 *   - Take into account the number of available IRQs
632 	 *   - Do not take into account the number of online CPUs so that
633 	 *     the user can over-subscribe if desired
634 	 *   - Tx is additionally limited by the number of hardware queues
635 	 */
636 	rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
637 	rx = min(rx, pdata->channel_irq_count);
638 	tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
639 	tx = min(tx, pdata->tx_max_q_count);
640 	tx = min(tx, pdata->channel_irq_count);
641 
642 	combined = min(rx, tx);
643 
644 	/* Should not be setting other count */
645 	if (channels->other_count) {
646 		netdev_err(netdev,
647 			   "other channel count must be zero\n");
648 		return -EINVAL;
649 	}
650 
651 	/* Require at least one Combined (Rx and Tx) channel */
652 	if (!channels->combined_count) {
653 		netdev_err(netdev,
654 			   "at least one combined Rx/Tx channel is required\n");
655 		xgbe_print_set_channels_input(netdev, channels);
656 		return -EINVAL;
657 	}
658 
659 	/* Check combined channels */
660 	if (channels->combined_count > combined) {
661 		netdev_err(netdev,
662 			   "combined channel count cannot exceed %u\n",
663 			   combined);
664 		xgbe_print_set_channels_input(netdev, channels);
665 		return -EINVAL;
666 	}
667 
668 	/* Can have some Rx-only or Tx-only channels, but not both */
669 	if (channels->rx_count && channels->tx_count) {
670 		netdev_err(netdev,
671 			   "cannot specify both Rx-only and Tx-only channels\n");
672 		xgbe_print_set_channels_input(netdev, channels);
673 		return -EINVAL;
674 	}
675 
676 	/* Check that we don't exceed the maximum number of channels */
677 	if ((channels->combined_count + channels->rx_count) > rx) {
678 		netdev_err(netdev,
679 			   "total Rx channels (%u) requested exceeds maximum available (%u)\n",
680 			   channels->combined_count + channels->rx_count, rx);
681 		xgbe_print_set_channels_input(netdev, channels);
682 		return -EINVAL;
683 	}
684 
685 	if ((channels->combined_count + channels->tx_count) > tx) {
686 		netdev_err(netdev,
687 			   "total Tx channels (%u) requested exceeds maximum available (%u)\n",
688 			   channels->combined_count + channels->tx_count, tx);
689 		xgbe_print_set_channels_input(netdev, channels);
690 		return -EINVAL;
691 	}
692 
693 	rx = channels->combined_count + channels->rx_count;
694 	tx = channels->combined_count + channels->tx_count;
695 
696 	rx_curr = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
697 	tx_curr = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
698 
699 	if ((rx == rx_curr) && (tx == tx_curr))
700 		goto out;
701 
702 	pdata->new_rx_ring_count = rx;
703 	pdata->new_tx_ring_count = tx;
704 
705 	xgbe_full_restart_dev(pdata);
706 
707 out:
708 	return 0;
709 }
710 
711 static const struct ethtool_ops xgbe_ethtool_ops = {
712 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
713 				     ETHTOOL_COALESCE_MAX_FRAMES,
714 	.get_drvinfo = xgbe_get_drvinfo,
715 	.get_msglevel = xgbe_get_msglevel,
716 	.set_msglevel = xgbe_set_msglevel,
717 	.get_link = ethtool_op_get_link,
718 	.get_coalesce = xgbe_get_coalesce,
719 	.set_coalesce = xgbe_set_coalesce,
720 	.get_pauseparam = xgbe_get_pauseparam,
721 	.set_pauseparam = xgbe_set_pauseparam,
722 	.get_strings = xgbe_get_strings,
723 	.get_ethtool_stats = xgbe_get_ethtool_stats,
724 	.get_sset_count = xgbe_get_sset_count,
725 	.get_rxnfc = xgbe_get_rxnfc,
726 	.get_rxfh_key_size = xgbe_get_rxfh_key_size,
727 	.get_rxfh_indir_size = xgbe_get_rxfh_indir_size,
728 	.get_rxfh = xgbe_get_rxfh,
729 	.set_rxfh = xgbe_set_rxfh,
730 	.get_ts_info = xgbe_get_ts_info,
731 	.get_link_ksettings = xgbe_get_link_ksettings,
732 	.set_link_ksettings = xgbe_set_link_ksettings,
733 	.get_module_info = xgbe_get_module_info,
734 	.get_module_eeprom = xgbe_get_module_eeprom,
735 	.get_ringparam = xgbe_get_ringparam,
736 	.set_ringparam = xgbe_set_ringparam,
737 	.get_channels = xgbe_get_channels,
738 	.set_channels = xgbe_set_channels,
739 };
740 
741 const struct ethtool_ops *xgbe_get_ethtool_ops(void)
742 {
743 	return &xgbe_ethtool_ops;
744 }
745