Linux Audio

Check our new training course

Loading...
v6.13.7
  1/*
  2 * AMD 10Gb Ethernet driver
  3 *
  4 * This file is available to you under your choice of the following two
  5 * licenses:
  6 *
  7 * License 1: GPLv2
  8 *
  9 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
 10 *
 11 * This file is free software; you may copy, redistribute and/or modify
 12 * it under the terms of the GNU General Public License as published by
 13 * the Free Software Foundation, either version 2 of the License, or (at
 14 * your option) any later version.
 15 *
 16 * This file is distributed in the hope that it will be useful, but
 17 * WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 19 * General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU General Public License
 22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 23 *
 24 * This file incorporates work covered by the following copyright and
 25 * permission notice:
 26 *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
 27 *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
 28 *     Inc. unless otherwise expressly agreed to in writing between Synopsys
 29 *     and you.
 30 *
 31 *     The Software IS NOT an item of Licensed Software or Licensed Product
 32 *     under any End User Software License Agreement or Agreement for Licensed
 33 *     Product with Synopsys or any supplement thereto.  Permission is hereby
 34 *     granted, free of charge, to any person obtaining a copy of this software
 35 *     annotated with this license and the Software, to deal in the Software
 36 *     without restriction, including without limitation the rights to use,
 37 *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 38 *     of the Software, and to permit persons to whom the Software is furnished
 39 *     to do so, subject to the following conditions:
 40 *
 41 *     The above copyright notice and this permission notice shall be included
 42 *     in all copies or substantial portions of the Software.
 43 *
 44 *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
 45 *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 46 *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 47 *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
 48 *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 49 *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 50 *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 51 *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 52 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 53 *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 54 *     THE POSSIBILITY OF SUCH DAMAGE.
 55 *
 56 *
 57 * License 2: Modified BSD
 58 *
 59 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
 60 * All rights reserved.
 61 *
 62 * Redistribution and use in source and binary forms, with or without
 63 * modification, are permitted provided that the following conditions are met:
 64 *     * Redistributions of source code must retain the above copyright
 65 *       notice, this list of conditions and the following disclaimer.
 66 *     * Redistributions in binary form must reproduce the above copyright
 67 *       notice, this list of conditions and the following disclaimer in the
 68 *       documentation and/or other materials provided with the distribution.
 69 *     * Neither the name of Advanced Micro Devices, Inc. nor the
 70 *       names of its contributors may be used to endorse or promote products
 71 *       derived from this software without specific prior written permission.
 72 *
 73 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 74 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 76 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 77 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 78 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 79 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 80 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 81 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 82 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 83 *
 84 * This file incorporates work covered by the following copyright and
 85 * permission notice:
 86 *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
 87 *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
 88 *     Inc. unless otherwise expressly agreed to in writing between Synopsys
 89 *     and you.
 90 *
 91 *     The Software IS NOT an item of Licensed Software or Licensed Product
 92 *     under any End User Software License Agreement or Agreement for Licensed
 93 *     Product with Synopsys or any supplement thereto.  Permission is hereby
 94 *     granted, free of charge, to any person obtaining a copy of this software
 95 *     annotated with this license and the Software, to deal in the Software
 96 *     without restriction, including without limitation the rights to use,
 97 *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 98 *     of the Software, and to permit persons to whom the Software is furnished
 99 *     to do so, subject to the following conditions:
100 *
101 *     The above copyright notice and this permission notice shall be included
102 *     in all copies or substantial portions of the Software.
103 *
104 *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105 *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106 *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107 *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108 *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109 *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110 *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111 *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113 *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114 *     THE POSSIBILITY OF SUCH DAMAGE.
115 */
116
117#include <linux/spinlock.h>
118#include <linux/phy.h>
119#include <linux/net_tstamp.h>
120
121#include "xgbe.h"
122#include "xgbe-common.h"
123
124struct xgbe_stats {
125	char stat_string[ETH_GSTRING_LEN];
126	int stat_size;
127	int stat_offset;
128};
129
130#define XGMAC_MMC_STAT(_string, _var)				\
131	{ _string,						\
132	  sizeof_field(struct xgbe_mmc_stats, _var),		\
133	  offsetof(struct xgbe_prv_data, mmc_stats._var),	\
134	}
135
136#define XGMAC_EXT_STAT(_string, _var)				\
137	{ _string,						\
138	  sizeof_field(struct xgbe_ext_stats, _var),		\
139	  offsetof(struct xgbe_prv_data, ext_stats._var),	\
140	}
141
142static const struct xgbe_stats xgbe_gstring_stats[] = {
143	XGMAC_MMC_STAT("tx_bytes", txoctetcount_gb),
144	XGMAC_MMC_STAT("tx_packets", txframecount_gb),
145	XGMAC_MMC_STAT("tx_unicast_packets", txunicastframes_gb),
146	XGMAC_MMC_STAT("tx_broadcast_packets", txbroadcastframes_gb),
147	XGMAC_MMC_STAT("tx_multicast_packets", txmulticastframes_gb),
148	XGMAC_MMC_STAT("tx_vlan_packets", txvlanframes_g),
149	XGMAC_EXT_STAT("tx_vxlan_packets", tx_vxlan_packets),
150	XGMAC_EXT_STAT("tx_tso_packets", tx_tso_packets),
151	XGMAC_MMC_STAT("tx_64_byte_packets", tx64octets_gb),
152	XGMAC_MMC_STAT("tx_65_to_127_byte_packets", tx65to127octets_gb),
153	XGMAC_MMC_STAT("tx_128_to_255_byte_packets", tx128to255octets_gb),
154	XGMAC_MMC_STAT("tx_256_to_511_byte_packets", tx256to511octets_gb),
155	XGMAC_MMC_STAT("tx_512_to_1023_byte_packets", tx512to1023octets_gb),
156	XGMAC_MMC_STAT("tx_1024_to_max_byte_packets", tx1024tomaxoctets_gb),
157	XGMAC_MMC_STAT("tx_underflow_errors", txunderflowerror),
158	XGMAC_MMC_STAT("tx_pause_frames", txpauseframes),
159
160	XGMAC_MMC_STAT("rx_bytes", rxoctetcount_gb),
161	XGMAC_MMC_STAT("rx_packets", rxframecount_gb),
162	XGMAC_MMC_STAT("rx_unicast_packets", rxunicastframes_g),
163	XGMAC_MMC_STAT("rx_broadcast_packets", rxbroadcastframes_g),
164	XGMAC_MMC_STAT("rx_multicast_packets", rxmulticastframes_g),
165	XGMAC_MMC_STAT("rx_vlan_packets", rxvlanframes_gb),
166	XGMAC_EXT_STAT("rx_vxlan_packets", rx_vxlan_packets),
167	XGMAC_MMC_STAT("rx_64_byte_packets", rx64octets_gb),
168	XGMAC_MMC_STAT("rx_65_to_127_byte_packets", rx65to127octets_gb),
169	XGMAC_MMC_STAT("rx_128_to_255_byte_packets", rx128to255octets_gb),
170	XGMAC_MMC_STAT("rx_256_to_511_byte_packets", rx256to511octets_gb),
171	XGMAC_MMC_STAT("rx_512_to_1023_byte_packets", rx512to1023octets_gb),
172	XGMAC_MMC_STAT("rx_1024_to_max_byte_packets", rx1024tomaxoctets_gb),
173	XGMAC_MMC_STAT("rx_undersize_packets", rxundersize_g),
174	XGMAC_MMC_STAT("rx_oversize_packets", rxoversize_g),
175	XGMAC_MMC_STAT("rx_crc_errors", rxcrcerror),
176	XGMAC_MMC_STAT("rx_crc_errors_small_packets", rxrunterror),
177	XGMAC_MMC_STAT("rx_crc_errors_giant_packets", rxjabbererror),
178	XGMAC_MMC_STAT("rx_length_errors", rxlengtherror),
179	XGMAC_MMC_STAT("rx_out_of_range_errors", rxoutofrangetype),
180	XGMAC_MMC_STAT("rx_fifo_overflow_errors", rxfifooverflow),
181	XGMAC_MMC_STAT("rx_watchdog_errors", rxwatchdogerror),
182	XGMAC_EXT_STAT("rx_csum_errors", rx_csum_errors),
183	XGMAC_EXT_STAT("rx_vxlan_csum_errors", rx_vxlan_csum_errors),
184	XGMAC_MMC_STAT("rx_pause_frames", rxpauseframes),
185	XGMAC_EXT_STAT("rx_split_header_packets", rx_split_header_packets),
186	XGMAC_EXT_STAT("rx_buffer_unavailable", rx_buffer_unavailable),
187};
188
189#define XGBE_STATS_COUNT	ARRAY_SIZE(xgbe_gstring_stats)
190
191static void xgbe_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
192{
193	struct xgbe_prv_data *pdata = netdev_priv(netdev);
194	int i;
195
196	switch (stringset) {
197	case ETH_SS_STATS:
198		for (i = 0; i < XGBE_STATS_COUNT; i++)
199			ethtool_puts(&data, xgbe_gstring_stats[i].stat_string);
200
 
 
201		for (i = 0; i < pdata->tx_ring_count; i++) {
202			ethtool_sprintf(&data, "txq_%u_packets", i);
203			ethtool_sprintf(&data, "txq_%u_bytes", i);
 
 
204		}
205
206		for (i = 0; i < pdata->rx_ring_count; i++) {
207			ethtool_sprintf(&data, "rxq_%u_packets", i);
208			ethtool_sprintf(&data, "rxq_%u_bytes", i);
 
 
209		}
210
211		break;
212	}
213}
214
215static void xgbe_get_ethtool_stats(struct net_device *netdev,
216				   struct ethtool_stats *stats, u64 *data)
217{
218	struct xgbe_prv_data *pdata = netdev_priv(netdev);
219	u8 *stat;
220	int i;
221
222	pdata->hw_if.read_mmc_stats(pdata);
223	for (i = 0; i < XGBE_STATS_COUNT; i++) {
224		stat = (u8 *)pdata + xgbe_gstring_stats[i].stat_offset;
225		*data++ = *(u64 *)stat;
226	}
227	for (i = 0; i < pdata->tx_ring_count; i++) {
228		*data++ = pdata->ext_stats.txq_packets[i];
229		*data++ = pdata->ext_stats.txq_bytes[i];
230	}
231	for (i = 0; i < pdata->rx_ring_count; i++) {
232		*data++ = pdata->ext_stats.rxq_packets[i];
233		*data++ = pdata->ext_stats.rxq_bytes[i];
234	}
235}
236
237static int xgbe_get_sset_count(struct net_device *netdev, int stringset)
238{
239	struct xgbe_prv_data *pdata = netdev_priv(netdev);
240	int ret;
241
242	switch (stringset) {
243	case ETH_SS_STATS:
244		ret = XGBE_STATS_COUNT +
245		      (pdata->tx_ring_count * 2) +
246		      (pdata->rx_ring_count * 2);
247		break;
248
249	default:
250		ret = -EOPNOTSUPP;
251	}
252
253	return ret;
254}
255
256static void xgbe_get_pauseparam(struct net_device *netdev,
257				struct ethtool_pauseparam *pause)
258{
259	struct xgbe_prv_data *pdata = netdev_priv(netdev);
260
261	pause->autoneg = pdata->phy.pause_autoneg;
262	pause->tx_pause = pdata->phy.tx_pause;
263	pause->rx_pause = pdata->phy.rx_pause;
264}
265
266static int xgbe_set_pauseparam(struct net_device *netdev,
267			       struct ethtool_pauseparam *pause)
268{
269	struct xgbe_prv_data *pdata = netdev_priv(netdev);
270	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
271	int ret = 0;
272
273	if (pause->autoneg && (pdata->phy.autoneg != AUTONEG_ENABLE)) {
274		netdev_err(netdev,
275			   "autoneg disabled, pause autoneg not available\n");
276		return -EINVAL;
277	}
278
279	pdata->phy.pause_autoneg = pause->autoneg;
280	pdata->phy.tx_pause = pause->tx_pause;
281	pdata->phy.rx_pause = pause->rx_pause;
282
283	XGBE_CLR_ADV(lks, Pause);
284	XGBE_CLR_ADV(lks, Asym_Pause);
285
286	if (pause->rx_pause) {
287		XGBE_SET_ADV(lks, Pause);
288		XGBE_SET_ADV(lks, Asym_Pause);
289	}
290
291	if (pause->tx_pause) {
292		/* Equivalent to XOR of Asym_Pause */
293		if (XGBE_ADV(lks, Asym_Pause))
294			XGBE_CLR_ADV(lks, Asym_Pause);
295		else
296			XGBE_SET_ADV(lks, Asym_Pause);
297	}
298
299	if (netif_running(netdev))
300		ret = pdata->phy_if.phy_config_aneg(pdata);
301
302	return ret;
303}
304
305static int xgbe_get_link_ksettings(struct net_device *netdev,
306				   struct ethtool_link_ksettings *cmd)
307{
308	struct xgbe_prv_data *pdata = netdev_priv(netdev);
309	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
310
311	cmd->base.phy_address = pdata->phy.address;
312
313	if (netif_carrier_ok(netdev)) {
314		cmd->base.speed = pdata->phy.speed;
315		cmd->base.duplex = pdata->phy.duplex;
316	} else {
317		cmd->base.speed = SPEED_UNKNOWN;
318		cmd->base.duplex = DUPLEX_UNKNOWN;
319	}
320
321	cmd->base.autoneg = pdata->phy.autoneg;
 
 
 
322	cmd->base.port = PORT_NONE;
323
324	XGBE_LM_COPY(cmd, supported, lks, supported);
325	XGBE_LM_COPY(cmd, advertising, lks, advertising);
326	XGBE_LM_COPY(cmd, lp_advertising, lks, lp_advertising);
327
328	return 0;
329}
330
331static int xgbe_set_link_ksettings(struct net_device *netdev,
332				   const struct ethtool_link_ksettings *cmd)
333{
334	struct xgbe_prv_data *pdata = netdev_priv(netdev);
335	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
336	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
337	u32 speed;
338	int ret;
339
340	speed = cmd->base.speed;
341
342	if (cmd->base.phy_address != pdata->phy.address) {
343		netdev_err(netdev, "invalid phy address %hhu\n",
344			   cmd->base.phy_address);
345		return -EINVAL;
346	}
347
348	if ((cmd->base.autoneg != AUTONEG_ENABLE) &&
349	    (cmd->base.autoneg != AUTONEG_DISABLE)) {
350		netdev_err(netdev, "unsupported autoneg %hhu\n",
351			   cmd->base.autoneg);
352		return -EINVAL;
353	}
354
355	if (cmd->base.autoneg == AUTONEG_DISABLE) {
356		if (!pdata->phy_if.phy_valid_speed(pdata, speed)) {
357			netdev_err(netdev, "unsupported speed %u\n", speed);
358			return -EINVAL;
359		}
360
361		if (cmd->base.duplex != DUPLEX_FULL) {
362			netdev_err(netdev, "unsupported duplex %hhu\n",
363				   cmd->base.duplex);
364			return -EINVAL;
365		}
366	}
367
368	netif_dbg(pdata, link, netdev,
369		  "requested advertisement 0x%*pb, phy supported 0x%*pb\n",
370		  __ETHTOOL_LINK_MODE_MASK_NBITS, cmd->link_modes.advertising,
371		  __ETHTOOL_LINK_MODE_MASK_NBITS, lks->link_modes.supported);
372
373	linkmode_and(advertising, cmd->link_modes.advertising,
374		     lks->link_modes.supported);
 
375
376	if ((cmd->base.autoneg == AUTONEG_ENABLE) &&
377	    bitmap_empty(advertising, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
378		netdev_err(netdev,
379			   "unsupported requested advertisement\n");
380		return -EINVAL;
381	}
382
383	ret = 0;
384	pdata->phy.autoneg = cmd->base.autoneg;
385	pdata->phy.speed = speed;
386	pdata->phy.duplex = cmd->base.duplex;
387	linkmode_copy(lks->link_modes.advertising, advertising);
 
388
389	if (cmd->base.autoneg == AUTONEG_ENABLE)
390		XGBE_SET_ADV(lks, Autoneg);
391	else
392		XGBE_CLR_ADV(lks, Autoneg);
393
394	if (netif_running(netdev))
395		ret = pdata->phy_if.phy_config_aneg(pdata);
396
397	return ret;
398}
399
400static void xgbe_get_drvinfo(struct net_device *netdev,
401			     struct ethtool_drvinfo *drvinfo)
402{
403	struct xgbe_prv_data *pdata = netdev_priv(netdev);
404	struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
405
406	strscpy(drvinfo->driver, XGBE_DRV_NAME, sizeof(drvinfo->driver));
407	strscpy(drvinfo->bus_info, dev_name(pdata->dev),
408		sizeof(drvinfo->bus_info));
409	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d.%d.%d",
410		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, USERVER),
411		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, DEVID),
412		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, SNPSVER));
413}
414
415static u32 xgbe_get_msglevel(struct net_device *netdev)
416{
417	struct xgbe_prv_data *pdata = netdev_priv(netdev);
418
419	return pdata->msg_enable;
420}
421
422static void xgbe_set_msglevel(struct net_device *netdev, u32 msglevel)
423{
424	struct xgbe_prv_data *pdata = netdev_priv(netdev);
425
426	pdata->msg_enable = msglevel;
427}
428
429static int xgbe_get_coalesce(struct net_device *netdev,
430			     struct ethtool_coalesce *ec,
431			     struct kernel_ethtool_coalesce *kernel_coal,
432			     struct netlink_ext_ack *extack)
433{
434	struct xgbe_prv_data *pdata = netdev_priv(netdev);
435
436	memset(ec, 0, sizeof(struct ethtool_coalesce));
437
438	ec->rx_coalesce_usecs = pdata->rx_usecs;
439	ec->rx_max_coalesced_frames = pdata->rx_frames;
440
441	ec->tx_max_coalesced_frames = pdata->tx_frames;
442
443	return 0;
444}
445
446static int xgbe_set_coalesce(struct net_device *netdev,
447			     struct ethtool_coalesce *ec,
448			     struct kernel_ethtool_coalesce *kernel_coal,
449			     struct netlink_ext_ack *extack)
450{
451	struct xgbe_prv_data *pdata = netdev_priv(netdev);
452	struct xgbe_hw_if *hw_if = &pdata->hw_if;
453	unsigned int rx_frames, rx_riwt, rx_usecs;
454	unsigned int tx_frames;
455
456	rx_riwt = hw_if->usec_to_riwt(pdata, ec->rx_coalesce_usecs);
457	rx_usecs = ec->rx_coalesce_usecs;
458	rx_frames = ec->rx_max_coalesced_frames;
459
460	/* Use smallest possible value if conversion resulted in zero */
461	if (rx_usecs && !rx_riwt)
462		rx_riwt = 1;
463
464	/* Check the bounds of values for Rx */
465	if (rx_riwt > XGMAC_MAX_DMA_RIWT) {
466		netdev_err(netdev, "rx-usec is limited to %d usecs\n",
467			   hw_if->riwt_to_usec(pdata, XGMAC_MAX_DMA_RIWT));
468		return -EINVAL;
469	}
470	if (rx_frames > pdata->rx_desc_count) {
471		netdev_err(netdev, "rx-frames is limited to %d frames\n",
472			   pdata->rx_desc_count);
473		return -EINVAL;
474	}
475
476	tx_frames = ec->tx_max_coalesced_frames;
477
478	/* Check the bounds of values for Tx */
479	if (tx_frames > pdata->tx_desc_count) {
480		netdev_err(netdev, "tx-frames is limited to %d frames\n",
481			   pdata->tx_desc_count);
482		return -EINVAL;
483	}
484
485	pdata->rx_riwt = rx_riwt;
486	pdata->rx_usecs = rx_usecs;
487	pdata->rx_frames = rx_frames;
488	hw_if->config_rx_coalesce(pdata);
489
490	pdata->tx_frames = tx_frames;
491	hw_if->config_tx_coalesce(pdata);
492
493	return 0;
494}
495
496static int xgbe_get_rxnfc(struct net_device *netdev,
497			  struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
498{
499	struct xgbe_prv_data *pdata = netdev_priv(netdev);
500
501	switch (rxnfc->cmd) {
502	case ETHTOOL_GRXRINGS:
503		rxnfc->data = pdata->rx_ring_count;
504		break;
505	default:
506		return -EOPNOTSUPP;
507	}
508
509	return 0;
510}
511
512static u32 xgbe_get_rxfh_key_size(struct net_device *netdev)
513{
514	struct xgbe_prv_data *pdata = netdev_priv(netdev);
515
516	return sizeof(pdata->rss_key);
517}
518
519static u32 xgbe_get_rxfh_indir_size(struct net_device *netdev)
520{
521	struct xgbe_prv_data *pdata = netdev_priv(netdev);
522
523	return ARRAY_SIZE(pdata->rss_table);
524}
525
526static int xgbe_get_rxfh(struct net_device *netdev,
527			 struct ethtool_rxfh_param *rxfh)
528{
529	struct xgbe_prv_data *pdata = netdev_priv(netdev);
530	unsigned int i;
531
532	if (rxfh->indir) {
533		for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
534			rxfh->indir[i] = XGMAC_GET_BITS(pdata->rss_table[i],
535							MAC_RSSDR, DMCH);
536	}
537
538	if (rxfh->key)
539		memcpy(rxfh->key, pdata->rss_key, sizeof(pdata->rss_key));
540
541	rxfh->hfunc = ETH_RSS_HASH_TOP;
 
542
543	return 0;
544}
545
546static int xgbe_set_rxfh(struct net_device *netdev,
547			 struct ethtool_rxfh_param *rxfh,
548			 struct netlink_ext_ack *extack)
549{
550	struct xgbe_prv_data *pdata = netdev_priv(netdev);
551	struct xgbe_hw_if *hw_if = &pdata->hw_if;
552	unsigned int ret;
553
554	if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
555	    rxfh->hfunc != ETH_RSS_HASH_TOP) {
556		netdev_err(netdev, "unsupported hash function\n");
557		return -EOPNOTSUPP;
558	}
559
560	if (rxfh->indir) {
561		ret = hw_if->set_rss_lookup_table(pdata, rxfh->indir);
562		if (ret)
563			return ret;
564	}
565
566	if (rxfh->key) {
567		ret = hw_if->set_rss_hash_key(pdata, rxfh->key);
568		if (ret)
569			return ret;
570	}
571
572	return 0;
573}
574
575static int xgbe_get_ts_info(struct net_device *netdev,
576			    struct kernel_ethtool_ts_info *ts_info)
577{
578	struct xgbe_prv_data *pdata = netdev_priv(netdev);
579
580	ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
 
 
581				   SOF_TIMESTAMPING_TX_HARDWARE |
582				   SOF_TIMESTAMPING_RX_HARDWARE |
583				   SOF_TIMESTAMPING_RAW_HARDWARE;
584
585	if (pdata->ptp_clock)
586		ts_info->phc_index = ptp_clock_index(pdata->ptp_clock);
 
 
587
588	ts_info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
589	ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
590			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
591			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
592			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
593			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
594			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
595			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
596			      (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
597			      (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
598			      (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
599			      (1 << HWTSTAMP_FILTER_ALL);
600
601	return 0;
602}
603
604static int xgbe_get_module_info(struct net_device *netdev,
605				struct ethtool_modinfo *modinfo)
606{
607	struct xgbe_prv_data *pdata = netdev_priv(netdev);
608
609	return pdata->phy_if.module_info(pdata, modinfo);
610}
611
612static int xgbe_get_module_eeprom(struct net_device *netdev,
613				  struct ethtool_eeprom *eeprom, u8 *data)
614{
615	struct xgbe_prv_data *pdata = netdev_priv(netdev);
616
617	return pdata->phy_if.module_eeprom(pdata, eeprom, data);
618}
619
620static void
621xgbe_get_ringparam(struct net_device *netdev,
622		   struct ethtool_ringparam *ringparam,
623		   struct kernel_ethtool_ringparam *kernel_ringparam,
624		   struct netlink_ext_ack *extack)
625{
626	struct xgbe_prv_data *pdata = netdev_priv(netdev);
627
628	ringparam->rx_max_pending = XGBE_RX_DESC_CNT_MAX;
629	ringparam->tx_max_pending = XGBE_TX_DESC_CNT_MAX;
630	ringparam->rx_pending = pdata->rx_desc_count;
631	ringparam->tx_pending = pdata->tx_desc_count;
632}
633
634static int xgbe_set_ringparam(struct net_device *netdev,
635			      struct ethtool_ringparam *ringparam,
636			      struct kernel_ethtool_ringparam *kernel_ringparam,
637			      struct netlink_ext_ack *extack)
638{
639	struct xgbe_prv_data *pdata = netdev_priv(netdev);
640	unsigned int rx, tx;
641
642	if (ringparam->rx_mini_pending || ringparam->rx_jumbo_pending) {
643		netdev_err(netdev, "unsupported ring parameter\n");
644		return -EINVAL;
645	}
646
647	if ((ringparam->rx_pending < XGBE_RX_DESC_CNT_MIN) ||
648	    (ringparam->rx_pending > XGBE_RX_DESC_CNT_MAX)) {
649		netdev_err(netdev,
650			   "rx ring parameter must be between %u and %u\n",
651			   XGBE_RX_DESC_CNT_MIN, XGBE_RX_DESC_CNT_MAX);
652		return -EINVAL;
653	}
654
655	if ((ringparam->tx_pending < XGBE_TX_DESC_CNT_MIN) ||
656	    (ringparam->tx_pending > XGBE_TX_DESC_CNT_MAX)) {
657		netdev_err(netdev,
658			   "tx ring parameter must be between %u and %u\n",
659			   XGBE_TX_DESC_CNT_MIN, XGBE_TX_DESC_CNT_MAX);
660		return -EINVAL;
661	}
662
663	rx = __rounddown_pow_of_two(ringparam->rx_pending);
664	if (rx != ringparam->rx_pending)
665		netdev_notice(netdev,
666			      "rx ring parameter rounded to power of two: %u\n",
667			      rx);
668
669	tx = __rounddown_pow_of_two(ringparam->tx_pending);
670	if (tx != ringparam->tx_pending)
671		netdev_notice(netdev,
672			      "tx ring parameter rounded to power of two: %u\n",
673			      tx);
674
675	if ((rx == pdata->rx_desc_count) &&
676	    (tx == pdata->tx_desc_count))
677		goto out;
678
679	pdata->rx_desc_count = rx;
680	pdata->tx_desc_count = tx;
681
682	xgbe_restart_dev(pdata);
683
684out:
685	return 0;
686}
687
688static void xgbe_get_channels(struct net_device *netdev,
689			      struct ethtool_channels *channels)
690{
691	struct xgbe_prv_data *pdata = netdev_priv(netdev);
692	unsigned int rx, tx, combined;
693
694	/* Calculate maximums allowed:
695	 *   - Take into account the number of available IRQs
696	 *   - Do not take into account the number of online CPUs so that
697	 *     the user can over-subscribe if desired
698	 *   - Tx is additionally limited by the number of hardware queues
699	 */
700	rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
701	rx = min(rx, pdata->channel_irq_count);
702	tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
703	tx = min(tx, pdata->channel_irq_count);
704	tx = min(tx, pdata->tx_max_q_count);
705
706	combined = min(rx, tx);
707
708	channels->max_combined = combined;
709	channels->max_rx = rx ? rx - 1 : 0;
710	channels->max_tx = tx ? tx - 1 : 0;
711
712	/* Get current settings based on device state */
713	rx = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
714	tx = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
715
716	combined = min(rx, tx);
717	rx -= combined;
718	tx -= combined;
719
720	channels->combined_count = combined;
721	channels->rx_count = rx;
722	channels->tx_count = tx;
723}
724
725static void xgbe_print_set_channels_input(struct net_device *netdev,
726					  struct ethtool_channels *channels)
727{
728	netdev_err(netdev, "channel inputs: combined=%u, rx-only=%u, tx-only=%u\n",
729		   channels->combined_count, channels->rx_count,
730		   channels->tx_count);
731}
732
733static int xgbe_set_channels(struct net_device *netdev,
734			     struct ethtool_channels *channels)
735{
736	struct xgbe_prv_data *pdata = netdev_priv(netdev);
737	unsigned int rx, rx_curr, tx, tx_curr, combined;
738
739	/* Calculate maximums allowed:
740	 *   - Take into account the number of available IRQs
741	 *   - Do not take into account the number of online CPUs so that
742	 *     the user can over-subscribe if desired
743	 *   - Tx is additionally limited by the number of hardware queues
744	 */
745	rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
746	rx = min(rx, pdata->channel_irq_count);
747	tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
748	tx = min(tx, pdata->tx_max_q_count);
749	tx = min(tx, pdata->channel_irq_count);
750
751	combined = min(rx, tx);
752
753	/* Should not be setting other count */
754	if (channels->other_count) {
755		netdev_err(netdev,
756			   "other channel count must be zero\n");
757		return -EINVAL;
758	}
759
760	/* Require at least one Combined (Rx and Tx) channel */
761	if (!channels->combined_count) {
762		netdev_err(netdev,
763			   "at least one combined Rx/Tx channel is required\n");
764		xgbe_print_set_channels_input(netdev, channels);
765		return -EINVAL;
766	}
767
768	/* Check combined channels */
769	if (channels->combined_count > combined) {
770		netdev_err(netdev,
771			   "combined channel count cannot exceed %u\n",
772			   combined);
773		xgbe_print_set_channels_input(netdev, channels);
774		return -EINVAL;
775	}
776
777	/* Can have some Rx-only or Tx-only channels, but not both */
778	if (channels->rx_count && channels->tx_count) {
779		netdev_err(netdev,
780			   "cannot specify both Rx-only and Tx-only channels\n");
781		xgbe_print_set_channels_input(netdev, channels);
782		return -EINVAL;
783	}
784
785	/* Check that we don't exceed the maximum number of channels */
786	if ((channels->combined_count + channels->rx_count) > rx) {
787		netdev_err(netdev,
788			   "total Rx channels (%u) requested exceeds maximum available (%u)\n",
789			   channels->combined_count + channels->rx_count, rx);
790		xgbe_print_set_channels_input(netdev, channels);
791		return -EINVAL;
792	}
793
794	if ((channels->combined_count + channels->tx_count) > tx) {
795		netdev_err(netdev,
796			   "total Tx channels (%u) requested exceeds maximum available (%u)\n",
797			   channels->combined_count + channels->tx_count, tx);
798		xgbe_print_set_channels_input(netdev, channels);
799		return -EINVAL;
800	}
801
802	rx = channels->combined_count + channels->rx_count;
803	tx = channels->combined_count + channels->tx_count;
804
805	rx_curr = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
806	tx_curr = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
807
808	if ((rx == rx_curr) && (tx == tx_curr))
809		goto out;
810
811	pdata->new_rx_ring_count = rx;
812	pdata->new_tx_ring_count = tx;
813
814	xgbe_full_restart_dev(pdata);
815
816out:
817	return 0;
818}
819
820static const struct ethtool_ops xgbe_ethtool_ops = {
821	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
822				     ETHTOOL_COALESCE_MAX_FRAMES,
823	.get_drvinfo = xgbe_get_drvinfo,
824	.get_msglevel = xgbe_get_msglevel,
825	.set_msglevel = xgbe_set_msglevel,
826	.get_link = ethtool_op_get_link,
827	.get_coalesce = xgbe_get_coalesce,
828	.set_coalesce = xgbe_set_coalesce,
829	.get_pauseparam = xgbe_get_pauseparam,
830	.set_pauseparam = xgbe_set_pauseparam,
831	.get_strings = xgbe_get_strings,
832	.get_ethtool_stats = xgbe_get_ethtool_stats,
833	.get_sset_count = xgbe_get_sset_count,
834	.get_rxnfc = xgbe_get_rxnfc,
835	.get_rxfh_key_size = xgbe_get_rxfh_key_size,
836	.get_rxfh_indir_size = xgbe_get_rxfh_indir_size,
837	.get_rxfh = xgbe_get_rxfh,
838	.set_rxfh = xgbe_set_rxfh,
839	.get_ts_info = xgbe_get_ts_info,
840	.get_link_ksettings = xgbe_get_link_ksettings,
841	.set_link_ksettings = xgbe_set_link_ksettings,
842	.get_module_info = xgbe_get_module_info,
843	.get_module_eeprom = xgbe_get_module_eeprom,
844	.get_ringparam = xgbe_get_ringparam,
845	.set_ringparam = xgbe_set_ringparam,
846	.get_channels = xgbe_get_channels,
847	.set_channels = xgbe_set_channels,
848};
849
850const struct ethtool_ops *xgbe_get_ethtool_ops(void)
851{
852	return &xgbe_ethtool_ops;
853}
v5.9
  1/*
  2 * AMD 10Gb Ethernet driver
  3 *
  4 * This file is available to you under your choice of the following two
  5 * licenses:
  6 *
  7 * License 1: GPLv2
  8 *
  9 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
 10 *
 11 * This file is free software; you may copy, redistribute and/or modify
 12 * it under the terms of the GNU General Public License as published by
 13 * the Free Software Foundation, either version 2 of the License, or (at
 14 * your option) any later version.
 15 *
 16 * This file is distributed in the hope that it will be useful, but
 17 * WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 19 * General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU General Public License
 22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 23 *
 24 * This file incorporates work covered by the following copyright and
 25 * permission notice:
 26 *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
 27 *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
 28 *     Inc. unless otherwise expressly agreed to in writing between Synopsys
 29 *     and you.
 30 *
 31 *     The Software IS NOT an item of Licensed Software or Licensed Product
 32 *     under any End User Software License Agreement or Agreement for Licensed
 33 *     Product with Synopsys or any supplement thereto.  Permission is hereby
 34 *     granted, free of charge, to any person obtaining a copy of this software
 35 *     annotated with this license and the Software, to deal in the Software
 36 *     without restriction, including without limitation the rights to use,
 37 *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 38 *     of the Software, and to permit persons to whom the Software is furnished
 39 *     to do so, subject to the following conditions:
 40 *
 41 *     The above copyright notice and this permission notice shall be included
 42 *     in all copies or substantial portions of the Software.
 43 *
 44 *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
 45 *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 46 *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 47 *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
 48 *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 49 *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 50 *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 51 *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 52 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 53 *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 54 *     THE POSSIBILITY OF SUCH DAMAGE.
 55 *
 56 *
 57 * License 2: Modified BSD
 58 *
 59 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
 60 * All rights reserved.
 61 *
 62 * Redistribution and use in source and binary forms, with or without
 63 * modification, are permitted provided that the following conditions are met:
 64 *     * Redistributions of source code must retain the above copyright
 65 *       notice, this list of conditions and the following disclaimer.
 66 *     * Redistributions in binary form must reproduce the above copyright
 67 *       notice, this list of conditions and the following disclaimer in the
 68 *       documentation and/or other materials provided with the distribution.
 69 *     * Neither the name of Advanced Micro Devices, Inc. nor the
 70 *       names of its contributors may be used to endorse or promote products
 71 *       derived from this software without specific prior written permission.
 72 *
 73 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 74 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 76 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 77 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 78 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 79 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 80 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 81 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 82 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 83 *
 84 * This file incorporates work covered by the following copyright and
 85 * permission notice:
 86 *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
 87 *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
 88 *     Inc. unless otherwise expressly agreed to in writing between Synopsys
 89 *     and you.
 90 *
 91 *     The Software IS NOT an item of Licensed Software or Licensed Product
 92 *     under any End User Software License Agreement or Agreement for Licensed
 93 *     Product with Synopsys or any supplement thereto.  Permission is hereby
 94 *     granted, free of charge, to any person obtaining a copy of this software
 95 *     annotated with this license and the Software, to deal in the Software
 96 *     without restriction, including without limitation the rights to use,
 97 *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 98 *     of the Software, and to permit persons to whom the Software is furnished
 99 *     to do so, subject to the following conditions:
100 *
101 *     The above copyright notice and this permission notice shall be included
102 *     in all copies or substantial portions of the Software.
103 *
104 *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105 *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106 *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107 *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108 *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109 *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110 *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111 *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113 *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114 *     THE POSSIBILITY OF SUCH DAMAGE.
115 */
116
117#include <linux/spinlock.h>
118#include <linux/phy.h>
119#include <linux/net_tstamp.h>
120
121#include "xgbe.h"
122#include "xgbe-common.h"
123
124struct xgbe_stats {
125	char stat_string[ETH_GSTRING_LEN];
126	int stat_size;
127	int stat_offset;
128};
129
130#define XGMAC_MMC_STAT(_string, _var)				\
131	{ _string,						\
132	  sizeof_field(struct xgbe_mmc_stats, _var),		\
133	  offsetof(struct xgbe_prv_data, mmc_stats._var),	\
134	}
135
136#define XGMAC_EXT_STAT(_string, _var)				\
137	{ _string,						\
138	  sizeof_field(struct xgbe_ext_stats, _var),		\
139	  offsetof(struct xgbe_prv_data, ext_stats._var),	\
140	}
141
142static const struct xgbe_stats xgbe_gstring_stats[] = {
143	XGMAC_MMC_STAT("tx_bytes", txoctetcount_gb),
144	XGMAC_MMC_STAT("tx_packets", txframecount_gb),
145	XGMAC_MMC_STAT("tx_unicast_packets", txunicastframes_gb),
146	XGMAC_MMC_STAT("tx_broadcast_packets", txbroadcastframes_gb),
147	XGMAC_MMC_STAT("tx_multicast_packets", txmulticastframes_gb),
148	XGMAC_MMC_STAT("tx_vlan_packets", txvlanframes_g),
149	XGMAC_EXT_STAT("tx_vxlan_packets", tx_vxlan_packets),
150	XGMAC_EXT_STAT("tx_tso_packets", tx_tso_packets),
151	XGMAC_MMC_STAT("tx_64_byte_packets", tx64octets_gb),
152	XGMAC_MMC_STAT("tx_65_to_127_byte_packets", tx65to127octets_gb),
153	XGMAC_MMC_STAT("tx_128_to_255_byte_packets", tx128to255octets_gb),
154	XGMAC_MMC_STAT("tx_256_to_511_byte_packets", tx256to511octets_gb),
155	XGMAC_MMC_STAT("tx_512_to_1023_byte_packets", tx512to1023octets_gb),
156	XGMAC_MMC_STAT("tx_1024_to_max_byte_packets", tx1024tomaxoctets_gb),
157	XGMAC_MMC_STAT("tx_underflow_errors", txunderflowerror),
158	XGMAC_MMC_STAT("tx_pause_frames", txpauseframes),
159
160	XGMAC_MMC_STAT("rx_bytes", rxoctetcount_gb),
161	XGMAC_MMC_STAT("rx_packets", rxframecount_gb),
162	XGMAC_MMC_STAT("rx_unicast_packets", rxunicastframes_g),
163	XGMAC_MMC_STAT("rx_broadcast_packets", rxbroadcastframes_g),
164	XGMAC_MMC_STAT("rx_multicast_packets", rxmulticastframes_g),
165	XGMAC_MMC_STAT("rx_vlan_packets", rxvlanframes_gb),
166	XGMAC_EXT_STAT("rx_vxlan_packets", rx_vxlan_packets),
167	XGMAC_MMC_STAT("rx_64_byte_packets", rx64octets_gb),
168	XGMAC_MMC_STAT("rx_65_to_127_byte_packets", rx65to127octets_gb),
169	XGMAC_MMC_STAT("rx_128_to_255_byte_packets", rx128to255octets_gb),
170	XGMAC_MMC_STAT("rx_256_to_511_byte_packets", rx256to511octets_gb),
171	XGMAC_MMC_STAT("rx_512_to_1023_byte_packets", rx512to1023octets_gb),
172	XGMAC_MMC_STAT("rx_1024_to_max_byte_packets", rx1024tomaxoctets_gb),
173	XGMAC_MMC_STAT("rx_undersize_packets", rxundersize_g),
174	XGMAC_MMC_STAT("rx_oversize_packets", rxoversize_g),
175	XGMAC_MMC_STAT("rx_crc_errors", rxcrcerror),
176	XGMAC_MMC_STAT("rx_crc_errors_small_packets", rxrunterror),
177	XGMAC_MMC_STAT("rx_crc_errors_giant_packets", rxjabbererror),
178	XGMAC_MMC_STAT("rx_length_errors", rxlengtherror),
179	XGMAC_MMC_STAT("rx_out_of_range_errors", rxoutofrangetype),
180	XGMAC_MMC_STAT("rx_fifo_overflow_errors", rxfifooverflow),
181	XGMAC_MMC_STAT("rx_watchdog_errors", rxwatchdogerror),
182	XGMAC_EXT_STAT("rx_csum_errors", rx_csum_errors),
183	XGMAC_EXT_STAT("rx_vxlan_csum_errors", rx_vxlan_csum_errors),
184	XGMAC_MMC_STAT("rx_pause_frames", rxpauseframes),
185	XGMAC_EXT_STAT("rx_split_header_packets", rx_split_header_packets),
186	XGMAC_EXT_STAT("rx_buffer_unavailable", rx_buffer_unavailable),
187};
188
189#define XGBE_STATS_COUNT	ARRAY_SIZE(xgbe_gstring_stats)
190
191static void xgbe_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
192{
193	struct xgbe_prv_data *pdata = netdev_priv(netdev);
194	int i;
195
196	switch (stringset) {
197	case ETH_SS_STATS:
198		for (i = 0; i < XGBE_STATS_COUNT; i++) {
199			memcpy(data, xgbe_gstring_stats[i].stat_string,
200			       ETH_GSTRING_LEN);
201			data += ETH_GSTRING_LEN;
202		}
203		for (i = 0; i < pdata->tx_ring_count; i++) {
204			sprintf(data, "txq_%u_packets", i);
205			data += ETH_GSTRING_LEN;
206			sprintf(data, "txq_%u_bytes", i);
207			data += ETH_GSTRING_LEN;
208		}
 
209		for (i = 0; i < pdata->rx_ring_count; i++) {
210			sprintf(data, "rxq_%u_packets", i);
211			data += ETH_GSTRING_LEN;
212			sprintf(data, "rxq_%u_bytes", i);
213			data += ETH_GSTRING_LEN;
214		}
 
215		break;
216	}
217}
218
219static void xgbe_get_ethtool_stats(struct net_device *netdev,
220				   struct ethtool_stats *stats, u64 *data)
221{
222	struct xgbe_prv_data *pdata = netdev_priv(netdev);
223	u8 *stat;
224	int i;
225
226	pdata->hw_if.read_mmc_stats(pdata);
227	for (i = 0; i < XGBE_STATS_COUNT; i++) {
228		stat = (u8 *)pdata + xgbe_gstring_stats[i].stat_offset;
229		*data++ = *(u64 *)stat;
230	}
231	for (i = 0; i < pdata->tx_ring_count; i++) {
232		*data++ = pdata->ext_stats.txq_packets[i];
233		*data++ = pdata->ext_stats.txq_bytes[i];
234	}
235	for (i = 0; i < pdata->rx_ring_count; i++) {
236		*data++ = pdata->ext_stats.rxq_packets[i];
237		*data++ = pdata->ext_stats.rxq_bytes[i];
238	}
239}
240
241static int xgbe_get_sset_count(struct net_device *netdev, int stringset)
242{
243	struct xgbe_prv_data *pdata = netdev_priv(netdev);
244	int ret;
245
246	switch (stringset) {
247	case ETH_SS_STATS:
248		ret = XGBE_STATS_COUNT +
249		      (pdata->tx_ring_count * 2) +
250		      (pdata->rx_ring_count * 2);
251		break;
252
253	default:
254		ret = -EOPNOTSUPP;
255	}
256
257	return ret;
258}
259
260static void xgbe_get_pauseparam(struct net_device *netdev,
261				struct ethtool_pauseparam *pause)
262{
263	struct xgbe_prv_data *pdata = netdev_priv(netdev);
264
265	pause->autoneg = pdata->phy.pause_autoneg;
266	pause->tx_pause = pdata->phy.tx_pause;
267	pause->rx_pause = pdata->phy.rx_pause;
268}
269
270static int xgbe_set_pauseparam(struct net_device *netdev,
271			       struct ethtool_pauseparam *pause)
272{
273	struct xgbe_prv_data *pdata = netdev_priv(netdev);
274	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
275	int ret = 0;
276
277	if (pause->autoneg && (pdata->phy.autoneg != AUTONEG_ENABLE)) {
278		netdev_err(netdev,
279			   "autoneg disabled, pause autoneg not available\n");
280		return -EINVAL;
281	}
282
283	pdata->phy.pause_autoneg = pause->autoneg;
284	pdata->phy.tx_pause = pause->tx_pause;
285	pdata->phy.rx_pause = pause->rx_pause;
286
287	XGBE_CLR_ADV(lks, Pause);
288	XGBE_CLR_ADV(lks, Asym_Pause);
289
290	if (pause->rx_pause) {
291		XGBE_SET_ADV(lks, Pause);
292		XGBE_SET_ADV(lks, Asym_Pause);
293	}
294
295	if (pause->tx_pause) {
296		/* Equivalent to XOR of Asym_Pause */
297		if (XGBE_ADV(lks, Asym_Pause))
298			XGBE_CLR_ADV(lks, Asym_Pause);
299		else
300			XGBE_SET_ADV(lks, Asym_Pause);
301	}
302
303	if (netif_running(netdev))
304		ret = pdata->phy_if.phy_config_aneg(pdata);
305
306	return ret;
307}
308
309static int xgbe_get_link_ksettings(struct net_device *netdev,
310				   struct ethtool_link_ksettings *cmd)
311{
312	struct xgbe_prv_data *pdata = netdev_priv(netdev);
313	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
314
315	cmd->base.phy_address = pdata->phy.address;
316
 
 
 
 
 
 
 
 
317	cmd->base.autoneg = pdata->phy.autoneg;
318	cmd->base.speed = pdata->phy.speed;
319	cmd->base.duplex = pdata->phy.duplex;
320
321	cmd->base.port = PORT_NONE;
322
323	XGBE_LM_COPY(cmd, supported, lks, supported);
324	XGBE_LM_COPY(cmd, advertising, lks, advertising);
325	XGBE_LM_COPY(cmd, lp_advertising, lks, lp_advertising);
326
327	return 0;
328}
329
330static int xgbe_set_link_ksettings(struct net_device *netdev,
331				   const struct ethtool_link_ksettings *cmd)
332{
333	struct xgbe_prv_data *pdata = netdev_priv(netdev);
334	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
335	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
336	u32 speed;
337	int ret;
338
339	speed = cmd->base.speed;
340
341	if (cmd->base.phy_address != pdata->phy.address) {
342		netdev_err(netdev, "invalid phy address %hhu\n",
343			   cmd->base.phy_address);
344		return -EINVAL;
345	}
346
347	if ((cmd->base.autoneg != AUTONEG_ENABLE) &&
348	    (cmd->base.autoneg != AUTONEG_DISABLE)) {
349		netdev_err(netdev, "unsupported autoneg %hhu\n",
350			   cmd->base.autoneg);
351		return -EINVAL;
352	}
353
354	if (cmd->base.autoneg == AUTONEG_DISABLE) {
355		if (!pdata->phy_if.phy_valid_speed(pdata, speed)) {
356			netdev_err(netdev, "unsupported speed %u\n", speed);
357			return -EINVAL;
358		}
359
360		if (cmd->base.duplex != DUPLEX_FULL) {
361			netdev_err(netdev, "unsupported duplex %hhu\n",
362				   cmd->base.duplex);
363			return -EINVAL;
364		}
365	}
366
367	netif_dbg(pdata, link, netdev,
368		  "requested advertisement 0x%*pb, phy supported 0x%*pb\n",
369		  __ETHTOOL_LINK_MODE_MASK_NBITS, cmd->link_modes.advertising,
370		  __ETHTOOL_LINK_MODE_MASK_NBITS, lks->link_modes.supported);
371
372	bitmap_and(advertising,
373		   cmd->link_modes.advertising, lks->link_modes.supported,
374		   __ETHTOOL_LINK_MODE_MASK_NBITS);
375
376	if ((cmd->base.autoneg == AUTONEG_ENABLE) &&
377	    bitmap_empty(advertising, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
378		netdev_err(netdev,
379			   "unsupported requested advertisement\n");
380		return -EINVAL;
381	}
382
383	ret = 0;
384	pdata->phy.autoneg = cmd->base.autoneg;
385	pdata->phy.speed = speed;
386	pdata->phy.duplex = cmd->base.duplex;
387	bitmap_copy(lks->link_modes.advertising, advertising,
388		    __ETHTOOL_LINK_MODE_MASK_NBITS);
389
390	if (cmd->base.autoneg == AUTONEG_ENABLE)
391		XGBE_SET_ADV(lks, Autoneg);
392	else
393		XGBE_CLR_ADV(lks, Autoneg);
394
395	if (netif_running(netdev))
396		ret = pdata->phy_if.phy_config_aneg(pdata);
397
398	return ret;
399}
400
401static void xgbe_get_drvinfo(struct net_device *netdev,
402			     struct ethtool_drvinfo *drvinfo)
403{
404	struct xgbe_prv_data *pdata = netdev_priv(netdev);
405	struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
406
407	strlcpy(drvinfo->driver, XGBE_DRV_NAME, sizeof(drvinfo->driver));
408	strlcpy(drvinfo->bus_info, dev_name(pdata->dev),
409		sizeof(drvinfo->bus_info));
410	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d.%d.%d",
411		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, USERVER),
412		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, DEVID),
413		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, SNPSVER));
414}
415
416static u32 xgbe_get_msglevel(struct net_device *netdev)
417{
418	struct xgbe_prv_data *pdata = netdev_priv(netdev);
419
420	return pdata->msg_enable;
421}
422
423static void xgbe_set_msglevel(struct net_device *netdev, u32 msglevel)
424{
425	struct xgbe_prv_data *pdata = netdev_priv(netdev);
426
427	pdata->msg_enable = msglevel;
428}
429
430static int xgbe_get_coalesce(struct net_device *netdev,
431			     struct ethtool_coalesce *ec)
 
 
432{
433	struct xgbe_prv_data *pdata = netdev_priv(netdev);
434
435	memset(ec, 0, sizeof(struct ethtool_coalesce));
436
437	ec->rx_coalesce_usecs = pdata->rx_usecs;
438	ec->rx_max_coalesced_frames = pdata->rx_frames;
439
440	ec->tx_max_coalesced_frames = pdata->tx_frames;
441
442	return 0;
443}
444
445static int xgbe_set_coalesce(struct net_device *netdev,
446			     struct ethtool_coalesce *ec)
 
 
447{
448	struct xgbe_prv_data *pdata = netdev_priv(netdev);
449	struct xgbe_hw_if *hw_if = &pdata->hw_if;
450	unsigned int rx_frames, rx_riwt, rx_usecs;
451	unsigned int tx_frames;
452
453	rx_riwt = hw_if->usec_to_riwt(pdata, ec->rx_coalesce_usecs);
454	rx_usecs = ec->rx_coalesce_usecs;
455	rx_frames = ec->rx_max_coalesced_frames;
456
457	/* Use smallest possible value if conversion resulted in zero */
458	if (rx_usecs && !rx_riwt)
459		rx_riwt = 1;
460
461	/* Check the bounds of values for Rx */
462	if (rx_riwt > XGMAC_MAX_DMA_RIWT) {
463		netdev_err(netdev, "rx-usec is limited to %d usecs\n",
464			   hw_if->riwt_to_usec(pdata, XGMAC_MAX_DMA_RIWT));
465		return -EINVAL;
466	}
467	if (rx_frames > pdata->rx_desc_count) {
468		netdev_err(netdev, "rx-frames is limited to %d frames\n",
469			   pdata->rx_desc_count);
470		return -EINVAL;
471	}
472
473	tx_frames = ec->tx_max_coalesced_frames;
474
475	/* Check the bounds of values for Tx */
476	if (tx_frames > pdata->tx_desc_count) {
477		netdev_err(netdev, "tx-frames is limited to %d frames\n",
478			   pdata->tx_desc_count);
479		return -EINVAL;
480	}
481
482	pdata->rx_riwt = rx_riwt;
483	pdata->rx_usecs = rx_usecs;
484	pdata->rx_frames = rx_frames;
485	hw_if->config_rx_coalesce(pdata);
486
487	pdata->tx_frames = tx_frames;
488	hw_if->config_tx_coalesce(pdata);
489
490	return 0;
491}
492
493static int xgbe_get_rxnfc(struct net_device *netdev,
494			  struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
495{
496	struct xgbe_prv_data *pdata = netdev_priv(netdev);
497
498	switch (rxnfc->cmd) {
499	case ETHTOOL_GRXRINGS:
500		rxnfc->data = pdata->rx_ring_count;
501		break;
502	default:
503		return -EOPNOTSUPP;
504	}
505
506	return 0;
507}
508
509static u32 xgbe_get_rxfh_key_size(struct net_device *netdev)
510{
511	struct xgbe_prv_data *pdata = netdev_priv(netdev);
512
513	return sizeof(pdata->rss_key);
514}
515
516static u32 xgbe_get_rxfh_indir_size(struct net_device *netdev)
517{
518	struct xgbe_prv_data *pdata = netdev_priv(netdev);
519
520	return ARRAY_SIZE(pdata->rss_table);
521}
522
523static int xgbe_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
524			 u8 *hfunc)
525{
526	struct xgbe_prv_data *pdata = netdev_priv(netdev);
527	unsigned int i;
528
529	if (indir) {
530		for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
531			indir[i] = XGMAC_GET_BITS(pdata->rss_table[i],
532						  MAC_RSSDR, DMCH);
533	}
534
535	if (key)
536		memcpy(key, pdata->rss_key, sizeof(pdata->rss_key));
537
538	if (hfunc)
539		*hfunc = ETH_RSS_HASH_TOP;
540
541	return 0;
542}
543
544static int xgbe_set_rxfh(struct net_device *netdev, const u32 *indir,
545			 const u8 *key, const u8 hfunc)
 
546{
547	struct xgbe_prv_data *pdata = netdev_priv(netdev);
548	struct xgbe_hw_if *hw_if = &pdata->hw_if;
549	unsigned int ret;
550
551	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) {
 
552		netdev_err(netdev, "unsupported hash function\n");
553		return -EOPNOTSUPP;
554	}
555
556	if (indir) {
557		ret = hw_if->set_rss_lookup_table(pdata, indir);
558		if (ret)
559			return ret;
560	}
561
562	if (key) {
563		ret = hw_if->set_rss_hash_key(pdata, key);
564		if (ret)
565			return ret;
566	}
567
568	return 0;
569}
570
571static int xgbe_get_ts_info(struct net_device *netdev,
572			    struct ethtool_ts_info *ts_info)
573{
574	struct xgbe_prv_data *pdata = netdev_priv(netdev);
575
576	ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
577				   SOF_TIMESTAMPING_RX_SOFTWARE |
578				   SOF_TIMESTAMPING_SOFTWARE |
579				   SOF_TIMESTAMPING_TX_HARDWARE |
580				   SOF_TIMESTAMPING_RX_HARDWARE |
581				   SOF_TIMESTAMPING_RAW_HARDWARE;
582
583	if (pdata->ptp_clock)
584		ts_info->phc_index = ptp_clock_index(pdata->ptp_clock);
585	else
586		ts_info->phc_index = -1;
587
588	ts_info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
589	ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
590			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
591			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
592			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
593			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
594			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
595			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
596			      (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
597			      (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
598			      (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
599			      (1 << HWTSTAMP_FILTER_ALL);
600
601	return 0;
602}
603
604static int xgbe_get_module_info(struct net_device *netdev,
605				struct ethtool_modinfo *modinfo)
606{
607	struct xgbe_prv_data *pdata = netdev_priv(netdev);
608
609	return pdata->phy_if.module_info(pdata, modinfo);
610}
611
612static int xgbe_get_module_eeprom(struct net_device *netdev,
613				  struct ethtool_eeprom *eeprom, u8 *data)
614{
615	struct xgbe_prv_data *pdata = netdev_priv(netdev);
616
617	return pdata->phy_if.module_eeprom(pdata, eeprom, data);
618}
619
620static void xgbe_get_ringparam(struct net_device *netdev,
621			       struct ethtool_ringparam *ringparam)
 
 
 
622{
623	struct xgbe_prv_data *pdata = netdev_priv(netdev);
624
625	ringparam->rx_max_pending = XGBE_RX_DESC_CNT_MAX;
626	ringparam->tx_max_pending = XGBE_TX_DESC_CNT_MAX;
627	ringparam->rx_pending = pdata->rx_desc_count;
628	ringparam->tx_pending = pdata->tx_desc_count;
629}
630
631static int xgbe_set_ringparam(struct net_device *netdev,
632			      struct ethtool_ringparam *ringparam)
 
 
633{
634	struct xgbe_prv_data *pdata = netdev_priv(netdev);
635	unsigned int rx, tx;
636
637	if (ringparam->rx_mini_pending || ringparam->rx_jumbo_pending) {
638		netdev_err(netdev, "unsupported ring parameter\n");
639		return -EINVAL;
640	}
641
642	if ((ringparam->rx_pending < XGBE_RX_DESC_CNT_MIN) ||
643	    (ringparam->rx_pending > XGBE_RX_DESC_CNT_MAX)) {
644		netdev_err(netdev,
645			   "rx ring parameter must be between %u and %u\n",
646			   XGBE_RX_DESC_CNT_MIN, XGBE_RX_DESC_CNT_MAX);
647		return -EINVAL;
648	}
649
650	if ((ringparam->tx_pending < XGBE_TX_DESC_CNT_MIN) ||
651	    (ringparam->tx_pending > XGBE_TX_DESC_CNT_MAX)) {
652		netdev_err(netdev,
653			   "tx ring parameter must be between %u and %u\n",
654			   XGBE_TX_DESC_CNT_MIN, XGBE_TX_DESC_CNT_MAX);
655		return -EINVAL;
656	}
657
658	rx = __rounddown_pow_of_two(ringparam->rx_pending);
659	if (rx != ringparam->rx_pending)
660		netdev_notice(netdev,
661			      "rx ring parameter rounded to power of two: %u\n",
662			      rx);
663
664	tx = __rounddown_pow_of_two(ringparam->tx_pending);
665	if (tx != ringparam->tx_pending)
666		netdev_notice(netdev,
667			      "tx ring parameter rounded to power of two: %u\n",
668			      tx);
669
670	if ((rx == pdata->rx_desc_count) &&
671	    (tx == pdata->tx_desc_count))
672		goto out;
673
674	pdata->rx_desc_count = rx;
675	pdata->tx_desc_count = tx;
676
677	xgbe_restart_dev(pdata);
678
679out:
680	return 0;
681}
682
683static void xgbe_get_channels(struct net_device *netdev,
684			      struct ethtool_channels *channels)
685{
686	struct xgbe_prv_data *pdata = netdev_priv(netdev);
687	unsigned int rx, tx, combined;
688
689	/* Calculate maximums allowed:
690	 *   - Take into account the number of available IRQs
691	 *   - Do not take into account the number of online CPUs so that
692	 *     the user can over-subscribe if desired
693	 *   - Tx is additionally limited by the number of hardware queues
694	 */
695	rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
696	rx = min(rx, pdata->channel_irq_count);
697	tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
698	tx = min(tx, pdata->channel_irq_count);
699	tx = min(tx, pdata->tx_max_q_count);
700
701	combined = min(rx, tx);
702
703	channels->max_combined = combined;
704	channels->max_rx = rx ? rx - 1 : 0;
705	channels->max_tx = tx ? tx - 1 : 0;
706
707	/* Get current settings based on device state */
708	rx = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
709	tx = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
710
711	combined = min(rx, tx);
712	rx -= combined;
713	tx -= combined;
714
715	channels->combined_count = combined;
716	channels->rx_count = rx;
717	channels->tx_count = tx;
718}
719
720static void xgbe_print_set_channels_input(struct net_device *netdev,
721					  struct ethtool_channels *channels)
722{
723	netdev_err(netdev, "channel inputs: combined=%u, rx-only=%u, tx-only=%u\n",
724		   channels->combined_count, channels->rx_count,
725		   channels->tx_count);
726}
727
728static int xgbe_set_channels(struct net_device *netdev,
729			     struct ethtool_channels *channels)
730{
731	struct xgbe_prv_data *pdata = netdev_priv(netdev);
732	unsigned int rx, rx_curr, tx, tx_curr, combined;
733
734	/* Calculate maximums allowed:
735	 *   - Take into account the number of available IRQs
736	 *   - Do not take into account the number of online CPUs so that
737	 *     the user can over-subscribe if desired
738	 *   - Tx is additionally limited by the number of hardware queues
739	 */
740	rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
741	rx = min(rx, pdata->channel_irq_count);
742	tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
743	tx = min(tx, pdata->tx_max_q_count);
744	tx = min(tx, pdata->channel_irq_count);
745
746	combined = min(rx, tx);
747
748	/* Should not be setting other count */
749	if (channels->other_count) {
750		netdev_err(netdev,
751			   "other channel count must be zero\n");
752		return -EINVAL;
753	}
754
755	/* Require at least one Combined (Rx and Tx) channel */
756	if (!channels->combined_count) {
757		netdev_err(netdev,
758			   "at least one combined Rx/Tx channel is required\n");
759		xgbe_print_set_channels_input(netdev, channels);
760		return -EINVAL;
761	}
762
763	/* Check combined channels */
764	if (channels->combined_count > combined) {
765		netdev_err(netdev,
766			   "combined channel count cannot exceed %u\n",
767			   combined);
768		xgbe_print_set_channels_input(netdev, channels);
769		return -EINVAL;
770	}
771
772	/* Can have some Rx-only or Tx-only channels, but not both */
773	if (channels->rx_count && channels->tx_count) {
774		netdev_err(netdev,
775			   "cannot specify both Rx-only and Tx-only channels\n");
776		xgbe_print_set_channels_input(netdev, channels);
777		return -EINVAL;
778	}
779
780	/* Check that we don't exceed the maximum number of channels */
781	if ((channels->combined_count + channels->rx_count) > rx) {
782		netdev_err(netdev,
783			   "total Rx channels (%u) requested exceeds maximum available (%u)\n",
784			   channels->combined_count + channels->rx_count, rx);
785		xgbe_print_set_channels_input(netdev, channels);
786		return -EINVAL;
787	}
788
789	if ((channels->combined_count + channels->tx_count) > tx) {
790		netdev_err(netdev,
791			   "total Tx channels (%u) requested exceeds maximum available (%u)\n",
792			   channels->combined_count + channels->tx_count, tx);
793		xgbe_print_set_channels_input(netdev, channels);
794		return -EINVAL;
795	}
796
797	rx = channels->combined_count + channels->rx_count;
798	tx = channels->combined_count + channels->tx_count;
799
800	rx_curr = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
801	tx_curr = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
802
803	if ((rx == rx_curr) && (tx == tx_curr))
804		goto out;
805
806	pdata->new_rx_ring_count = rx;
807	pdata->new_tx_ring_count = tx;
808
809	xgbe_full_restart_dev(pdata);
810
811out:
812	return 0;
813}
814
815static const struct ethtool_ops xgbe_ethtool_ops = {
816	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
817				     ETHTOOL_COALESCE_MAX_FRAMES,
818	.get_drvinfo = xgbe_get_drvinfo,
819	.get_msglevel = xgbe_get_msglevel,
820	.set_msglevel = xgbe_set_msglevel,
821	.get_link = ethtool_op_get_link,
822	.get_coalesce = xgbe_get_coalesce,
823	.set_coalesce = xgbe_set_coalesce,
824	.get_pauseparam = xgbe_get_pauseparam,
825	.set_pauseparam = xgbe_set_pauseparam,
826	.get_strings = xgbe_get_strings,
827	.get_ethtool_stats = xgbe_get_ethtool_stats,
828	.get_sset_count = xgbe_get_sset_count,
829	.get_rxnfc = xgbe_get_rxnfc,
830	.get_rxfh_key_size = xgbe_get_rxfh_key_size,
831	.get_rxfh_indir_size = xgbe_get_rxfh_indir_size,
832	.get_rxfh = xgbe_get_rxfh,
833	.set_rxfh = xgbe_set_rxfh,
834	.get_ts_info = xgbe_get_ts_info,
835	.get_link_ksettings = xgbe_get_link_ksettings,
836	.set_link_ksettings = xgbe_set_link_ksettings,
837	.get_module_info = xgbe_get_module_info,
838	.get_module_eeprom = xgbe_get_module_eeprom,
839	.get_ringparam = xgbe_get_ringparam,
840	.set_ringparam = xgbe_set_ringparam,
841	.get_channels = xgbe_get_channels,
842	.set_channels = xgbe_set_channels,
843};
844
845const struct ethtool_ops *xgbe_get_ethtool_ops(void)
846{
847	return &xgbe_ethtool_ops;
848}