• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
124 struct 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 
142 static 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 
xgbe_get_strings(struct net_device * netdev,u32 stringset,u8 * data)191 static 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 
xgbe_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)219 static 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 
xgbe_get_sset_count(struct net_device * netdev,int stringset)241 static 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 
xgbe_get_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * pause)260 static 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 
xgbe_set_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * pause)270 static 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 
xgbe_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * cmd)309 static 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 	if (netif_carrier_ok(netdev)) {
318 		cmd->base.speed = pdata->phy.speed;
319 		cmd->base.duplex = pdata->phy.duplex;
320 	} else {
321 		cmd->base.speed = SPEED_UNKNOWN;
322 		cmd->base.duplex = DUPLEX_UNKNOWN;
323 	}
324 
325 	cmd->base.autoneg = pdata->phy.autoneg;
326 	cmd->base.port = PORT_NONE;
327 
328 	XGBE_LM_COPY(cmd, supported, lks, supported);
329 	XGBE_LM_COPY(cmd, advertising, lks, advertising);
330 	XGBE_LM_COPY(cmd, lp_advertising, lks, lp_advertising);
331 
332 	return 0;
333 }
334 
xgbe_set_link_ksettings(struct net_device * netdev,const struct ethtool_link_ksettings * cmd)335 static int xgbe_set_link_ksettings(struct net_device *netdev,
336 				   const struct ethtool_link_ksettings *cmd)
337 {
338 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
339 	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
340 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
341 	u32 speed;
342 	int ret;
343 
344 	speed = cmd->base.speed;
345 
346 	if (cmd->base.phy_address != pdata->phy.address) {
347 		netdev_err(netdev, "invalid phy address %hhu\n",
348 			   cmd->base.phy_address);
349 		return -EINVAL;
350 	}
351 
352 	if ((cmd->base.autoneg != AUTONEG_ENABLE) &&
353 	    (cmd->base.autoneg != AUTONEG_DISABLE)) {
354 		netdev_err(netdev, "unsupported autoneg %hhu\n",
355 			   cmd->base.autoneg);
356 		return -EINVAL;
357 	}
358 
359 	if (cmd->base.autoneg == AUTONEG_DISABLE) {
360 		if (!pdata->phy_if.phy_valid_speed(pdata, speed)) {
361 			netdev_err(netdev, "unsupported speed %u\n", speed);
362 			return -EINVAL;
363 		}
364 
365 		if (cmd->base.duplex != DUPLEX_FULL) {
366 			netdev_err(netdev, "unsupported duplex %hhu\n",
367 				   cmd->base.duplex);
368 			return -EINVAL;
369 		}
370 	}
371 
372 	netif_dbg(pdata, link, netdev,
373 		  "requested advertisement 0x%*pb, phy supported 0x%*pb\n",
374 		  __ETHTOOL_LINK_MODE_MASK_NBITS, cmd->link_modes.advertising,
375 		  __ETHTOOL_LINK_MODE_MASK_NBITS, lks->link_modes.supported);
376 
377 	bitmap_and(advertising,
378 		   cmd->link_modes.advertising, lks->link_modes.supported,
379 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
380 
381 	if ((cmd->base.autoneg == AUTONEG_ENABLE) &&
382 	    bitmap_empty(advertising, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
383 		netdev_err(netdev,
384 			   "unsupported requested advertisement\n");
385 		return -EINVAL;
386 	}
387 
388 	ret = 0;
389 	pdata->phy.autoneg = cmd->base.autoneg;
390 	pdata->phy.speed = speed;
391 	pdata->phy.duplex = cmd->base.duplex;
392 	bitmap_copy(lks->link_modes.advertising, advertising,
393 		    __ETHTOOL_LINK_MODE_MASK_NBITS);
394 
395 	if (cmd->base.autoneg == AUTONEG_ENABLE)
396 		XGBE_SET_ADV(lks, Autoneg);
397 	else
398 		XGBE_CLR_ADV(lks, Autoneg);
399 
400 	if (netif_running(netdev))
401 		ret = pdata->phy_if.phy_config_aneg(pdata);
402 
403 	return ret;
404 }
405 
xgbe_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)406 static void xgbe_get_drvinfo(struct net_device *netdev,
407 			     struct ethtool_drvinfo *drvinfo)
408 {
409 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
410 	struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
411 
412 	strlcpy(drvinfo->driver, XGBE_DRV_NAME, sizeof(drvinfo->driver));
413 	strlcpy(drvinfo->bus_info, dev_name(pdata->dev),
414 		sizeof(drvinfo->bus_info));
415 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d.%d.%d",
416 		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, USERVER),
417 		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, DEVID),
418 		 XGMAC_GET_BITS(hw_feat->version, MAC_VR, SNPSVER));
419 }
420 
xgbe_get_msglevel(struct net_device * netdev)421 static u32 xgbe_get_msglevel(struct net_device *netdev)
422 {
423 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
424 
425 	return pdata->msg_enable;
426 }
427 
xgbe_set_msglevel(struct net_device * netdev,u32 msglevel)428 static void xgbe_set_msglevel(struct net_device *netdev, u32 msglevel)
429 {
430 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
431 
432 	pdata->msg_enable = msglevel;
433 }
434 
xgbe_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)435 static int xgbe_get_coalesce(struct net_device *netdev,
436 			     struct ethtool_coalesce *ec,
437 			     struct kernel_ethtool_coalesce *kernel_coal,
438 			     struct netlink_ext_ack *extack)
439 {
440 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
441 
442 	memset(ec, 0, sizeof(struct ethtool_coalesce));
443 
444 	ec->rx_coalesce_usecs = pdata->rx_usecs;
445 	ec->rx_max_coalesced_frames = pdata->rx_frames;
446 
447 	ec->tx_max_coalesced_frames = pdata->tx_frames;
448 
449 	return 0;
450 }
451 
xgbe_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)452 static int xgbe_set_coalesce(struct net_device *netdev,
453 			     struct ethtool_coalesce *ec,
454 			     struct kernel_ethtool_coalesce *kernel_coal,
455 			     struct netlink_ext_ack *extack)
456 {
457 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
458 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
459 	unsigned int rx_frames, rx_riwt, rx_usecs;
460 	unsigned int tx_frames;
461 
462 	rx_riwt = hw_if->usec_to_riwt(pdata, ec->rx_coalesce_usecs);
463 	rx_usecs = ec->rx_coalesce_usecs;
464 	rx_frames = ec->rx_max_coalesced_frames;
465 
466 	/* Use smallest possible value if conversion resulted in zero */
467 	if (rx_usecs && !rx_riwt)
468 		rx_riwt = 1;
469 
470 	/* Check the bounds of values for Rx */
471 	if (rx_riwt > XGMAC_MAX_DMA_RIWT) {
472 		netdev_err(netdev, "rx-usec is limited to %d usecs\n",
473 			   hw_if->riwt_to_usec(pdata, XGMAC_MAX_DMA_RIWT));
474 		return -EINVAL;
475 	}
476 	if (rx_frames > pdata->rx_desc_count) {
477 		netdev_err(netdev, "rx-frames is limited to %d frames\n",
478 			   pdata->rx_desc_count);
479 		return -EINVAL;
480 	}
481 
482 	tx_frames = ec->tx_max_coalesced_frames;
483 
484 	/* Check the bounds of values for Tx */
485 	if (tx_frames > pdata->tx_desc_count) {
486 		netdev_err(netdev, "tx-frames is limited to %d frames\n",
487 			   pdata->tx_desc_count);
488 		return -EINVAL;
489 	}
490 
491 	pdata->rx_riwt = rx_riwt;
492 	pdata->rx_usecs = rx_usecs;
493 	pdata->rx_frames = rx_frames;
494 	hw_if->config_rx_coalesce(pdata);
495 
496 	pdata->tx_frames = tx_frames;
497 	hw_if->config_tx_coalesce(pdata);
498 
499 	return 0;
500 }
501 
xgbe_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * rxnfc,u32 * rule_locs)502 static int xgbe_get_rxnfc(struct net_device *netdev,
503 			  struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
504 {
505 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
506 
507 	switch (rxnfc->cmd) {
508 	case ETHTOOL_GRXRINGS:
509 		rxnfc->data = pdata->rx_ring_count;
510 		break;
511 	default:
512 		return -EOPNOTSUPP;
513 	}
514 
515 	return 0;
516 }
517 
xgbe_get_rxfh_key_size(struct net_device * netdev)518 static u32 xgbe_get_rxfh_key_size(struct net_device *netdev)
519 {
520 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
521 
522 	return sizeof(pdata->rss_key);
523 }
524 
xgbe_get_rxfh_indir_size(struct net_device * netdev)525 static u32 xgbe_get_rxfh_indir_size(struct net_device *netdev)
526 {
527 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
528 
529 	return ARRAY_SIZE(pdata->rss_table);
530 }
531 
xgbe_get_rxfh(struct net_device * netdev,u32 * indir,u8 * key,u8 * hfunc)532 static int xgbe_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
533 			 u8 *hfunc)
534 {
535 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
536 	unsigned int i;
537 
538 	if (indir) {
539 		for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
540 			indir[i] = XGMAC_GET_BITS(pdata->rss_table[i],
541 						  MAC_RSSDR, DMCH);
542 	}
543 
544 	if (key)
545 		memcpy(key, pdata->rss_key, sizeof(pdata->rss_key));
546 
547 	if (hfunc)
548 		*hfunc = ETH_RSS_HASH_TOP;
549 
550 	return 0;
551 }
552 
xgbe_set_rxfh(struct net_device * netdev,const u32 * indir,const u8 * key,const u8 hfunc)553 static int xgbe_set_rxfh(struct net_device *netdev, const u32 *indir,
554 			 const u8 *key, const u8 hfunc)
555 {
556 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
557 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
558 	unsigned int ret;
559 
560 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) {
561 		netdev_err(netdev, "unsupported hash function\n");
562 		return -EOPNOTSUPP;
563 	}
564 
565 	if (indir) {
566 		ret = hw_if->set_rss_lookup_table(pdata, indir);
567 		if (ret)
568 			return ret;
569 	}
570 
571 	if (key) {
572 		ret = hw_if->set_rss_hash_key(pdata, key);
573 		if (ret)
574 			return ret;
575 	}
576 
577 	return 0;
578 }
579 
xgbe_get_ts_info(struct net_device * netdev,struct ethtool_ts_info * ts_info)580 static int xgbe_get_ts_info(struct net_device *netdev,
581 			    struct ethtool_ts_info *ts_info)
582 {
583 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
584 
585 	ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
586 				   SOF_TIMESTAMPING_RX_SOFTWARE |
587 				   SOF_TIMESTAMPING_SOFTWARE |
588 				   SOF_TIMESTAMPING_TX_HARDWARE |
589 				   SOF_TIMESTAMPING_RX_HARDWARE |
590 				   SOF_TIMESTAMPING_RAW_HARDWARE;
591 
592 	if (pdata->ptp_clock)
593 		ts_info->phc_index = ptp_clock_index(pdata->ptp_clock);
594 	else
595 		ts_info->phc_index = -1;
596 
597 	ts_info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
598 	ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
599 			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
600 			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
601 			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
602 			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
603 			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
604 			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
605 			      (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
606 			      (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
607 			      (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
608 			      (1 << HWTSTAMP_FILTER_ALL);
609 
610 	return 0;
611 }
612 
xgbe_get_module_info(struct net_device * netdev,struct ethtool_modinfo * modinfo)613 static int xgbe_get_module_info(struct net_device *netdev,
614 				struct ethtool_modinfo *modinfo)
615 {
616 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
617 
618 	return pdata->phy_if.module_info(pdata, modinfo);
619 }
620 
xgbe_get_module_eeprom(struct net_device * netdev,struct ethtool_eeprom * eeprom,u8 * data)621 static int xgbe_get_module_eeprom(struct net_device *netdev,
622 				  struct ethtool_eeprom *eeprom, u8 *data)
623 {
624 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
625 
626 	return pdata->phy_if.module_eeprom(pdata, eeprom, data);
627 }
628 
xgbe_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ringparam)629 static void xgbe_get_ringparam(struct net_device *netdev,
630 			       struct ethtool_ringparam *ringparam)
631 {
632 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
633 
634 	ringparam->rx_max_pending = XGBE_RX_DESC_CNT_MAX;
635 	ringparam->tx_max_pending = XGBE_TX_DESC_CNT_MAX;
636 	ringparam->rx_pending = pdata->rx_desc_count;
637 	ringparam->tx_pending = pdata->tx_desc_count;
638 }
639 
xgbe_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ringparam)640 static int xgbe_set_ringparam(struct net_device *netdev,
641 			      struct ethtool_ringparam *ringparam)
642 {
643 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
644 	unsigned int rx, tx;
645 
646 	if (ringparam->rx_mini_pending || ringparam->rx_jumbo_pending) {
647 		netdev_err(netdev, "unsupported ring parameter\n");
648 		return -EINVAL;
649 	}
650 
651 	if ((ringparam->rx_pending < XGBE_RX_DESC_CNT_MIN) ||
652 	    (ringparam->rx_pending > XGBE_RX_DESC_CNT_MAX)) {
653 		netdev_err(netdev,
654 			   "rx ring parameter must be between %u and %u\n",
655 			   XGBE_RX_DESC_CNT_MIN, XGBE_RX_DESC_CNT_MAX);
656 		return -EINVAL;
657 	}
658 
659 	if ((ringparam->tx_pending < XGBE_TX_DESC_CNT_MIN) ||
660 	    (ringparam->tx_pending > XGBE_TX_DESC_CNT_MAX)) {
661 		netdev_err(netdev,
662 			   "tx ring parameter must be between %u and %u\n",
663 			   XGBE_TX_DESC_CNT_MIN, XGBE_TX_DESC_CNT_MAX);
664 		return -EINVAL;
665 	}
666 
667 	rx = __rounddown_pow_of_two(ringparam->rx_pending);
668 	if (rx != ringparam->rx_pending)
669 		netdev_notice(netdev,
670 			      "rx ring parameter rounded to power of two: %u\n",
671 			      rx);
672 
673 	tx = __rounddown_pow_of_two(ringparam->tx_pending);
674 	if (tx != ringparam->tx_pending)
675 		netdev_notice(netdev,
676 			      "tx ring parameter rounded to power of two: %u\n",
677 			      tx);
678 
679 	if ((rx == pdata->rx_desc_count) &&
680 	    (tx == pdata->tx_desc_count))
681 		goto out;
682 
683 	pdata->rx_desc_count = rx;
684 	pdata->tx_desc_count = tx;
685 
686 	xgbe_restart_dev(pdata);
687 
688 out:
689 	return 0;
690 }
691 
xgbe_get_channels(struct net_device * netdev,struct ethtool_channels * channels)692 static void xgbe_get_channels(struct net_device *netdev,
693 			      struct ethtool_channels *channels)
694 {
695 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
696 	unsigned int rx, tx, combined;
697 
698 	/* Calculate maximums allowed:
699 	 *   - Take into account the number of available IRQs
700 	 *   - Do not take into account the number of online CPUs so that
701 	 *     the user can over-subscribe if desired
702 	 *   - Tx is additionally limited by the number of hardware queues
703 	 */
704 	rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
705 	rx = min(rx, pdata->channel_irq_count);
706 	tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
707 	tx = min(tx, pdata->channel_irq_count);
708 	tx = min(tx, pdata->tx_max_q_count);
709 
710 	combined = min(rx, tx);
711 
712 	channels->max_combined = combined;
713 	channels->max_rx = rx ? rx - 1 : 0;
714 	channels->max_tx = tx ? tx - 1 : 0;
715 
716 	/* Get current settings based on device state */
717 	rx = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
718 	tx = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
719 
720 	combined = min(rx, tx);
721 	rx -= combined;
722 	tx -= combined;
723 
724 	channels->combined_count = combined;
725 	channels->rx_count = rx;
726 	channels->tx_count = tx;
727 }
728 
xgbe_print_set_channels_input(struct net_device * netdev,struct ethtool_channels * channels)729 static void xgbe_print_set_channels_input(struct net_device *netdev,
730 					  struct ethtool_channels *channels)
731 {
732 	netdev_err(netdev, "channel inputs: combined=%u, rx-only=%u, tx-only=%u\n",
733 		   channels->combined_count, channels->rx_count,
734 		   channels->tx_count);
735 }
736 
xgbe_set_channels(struct net_device * netdev,struct ethtool_channels * channels)737 static int xgbe_set_channels(struct net_device *netdev,
738 			     struct ethtool_channels *channels)
739 {
740 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
741 	unsigned int rx, rx_curr, tx, tx_curr, combined;
742 
743 	/* Calculate maximums allowed:
744 	 *   - Take into account the number of available IRQs
745 	 *   - Do not take into account the number of online CPUs so that
746 	 *     the user can over-subscribe if desired
747 	 *   - Tx is additionally limited by the number of hardware queues
748 	 */
749 	rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
750 	rx = min(rx, pdata->channel_irq_count);
751 	tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
752 	tx = min(tx, pdata->tx_max_q_count);
753 	tx = min(tx, pdata->channel_irq_count);
754 
755 	combined = min(rx, tx);
756 
757 	/* Should not be setting other count */
758 	if (channels->other_count) {
759 		netdev_err(netdev,
760 			   "other channel count must be zero\n");
761 		return -EINVAL;
762 	}
763 
764 	/* Require at least one Combined (Rx and Tx) channel */
765 	if (!channels->combined_count) {
766 		netdev_err(netdev,
767 			   "at least one combined Rx/Tx channel is required\n");
768 		xgbe_print_set_channels_input(netdev, channels);
769 		return -EINVAL;
770 	}
771 
772 	/* Check combined channels */
773 	if (channels->combined_count > combined) {
774 		netdev_err(netdev,
775 			   "combined channel count cannot exceed %u\n",
776 			   combined);
777 		xgbe_print_set_channels_input(netdev, channels);
778 		return -EINVAL;
779 	}
780 
781 	/* Can have some Rx-only or Tx-only channels, but not both */
782 	if (channels->rx_count && channels->tx_count) {
783 		netdev_err(netdev,
784 			   "cannot specify both Rx-only and Tx-only channels\n");
785 		xgbe_print_set_channels_input(netdev, channels);
786 		return -EINVAL;
787 	}
788 
789 	/* Check that we don't exceed the maximum number of channels */
790 	if ((channels->combined_count + channels->rx_count) > rx) {
791 		netdev_err(netdev,
792 			   "total Rx channels (%u) requested exceeds maximum available (%u)\n",
793 			   channels->combined_count + channels->rx_count, rx);
794 		xgbe_print_set_channels_input(netdev, channels);
795 		return -EINVAL;
796 	}
797 
798 	if ((channels->combined_count + channels->tx_count) > tx) {
799 		netdev_err(netdev,
800 			   "total Tx channels (%u) requested exceeds maximum available (%u)\n",
801 			   channels->combined_count + channels->tx_count, tx);
802 		xgbe_print_set_channels_input(netdev, channels);
803 		return -EINVAL;
804 	}
805 
806 	rx = channels->combined_count + channels->rx_count;
807 	tx = channels->combined_count + channels->tx_count;
808 
809 	rx_curr = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
810 	tx_curr = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
811 
812 	if ((rx == rx_curr) && (tx == tx_curr))
813 		goto out;
814 
815 	pdata->new_rx_ring_count = rx;
816 	pdata->new_tx_ring_count = tx;
817 
818 	xgbe_full_restart_dev(pdata);
819 
820 out:
821 	return 0;
822 }
823 
824 static const struct ethtool_ops xgbe_ethtool_ops = {
825 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
826 				     ETHTOOL_COALESCE_MAX_FRAMES,
827 	.get_drvinfo = xgbe_get_drvinfo,
828 	.get_msglevel = xgbe_get_msglevel,
829 	.set_msglevel = xgbe_set_msglevel,
830 	.get_link = ethtool_op_get_link,
831 	.get_coalesce = xgbe_get_coalesce,
832 	.set_coalesce = xgbe_set_coalesce,
833 	.get_pauseparam = xgbe_get_pauseparam,
834 	.set_pauseparam = xgbe_set_pauseparam,
835 	.get_strings = xgbe_get_strings,
836 	.get_ethtool_stats = xgbe_get_ethtool_stats,
837 	.get_sset_count = xgbe_get_sset_count,
838 	.get_rxnfc = xgbe_get_rxnfc,
839 	.get_rxfh_key_size = xgbe_get_rxfh_key_size,
840 	.get_rxfh_indir_size = xgbe_get_rxfh_indir_size,
841 	.get_rxfh = xgbe_get_rxfh,
842 	.set_rxfh = xgbe_set_rxfh,
843 	.get_ts_info = xgbe_get_ts_info,
844 	.get_link_ksettings = xgbe_get_link_ksettings,
845 	.set_link_ksettings = xgbe_set_link_ksettings,
846 	.get_module_info = xgbe_get_module_info,
847 	.get_module_eeprom = xgbe_get_module_eeprom,
848 	.get_ringparam = xgbe_get_ringparam,
849 	.set_ringparam = xgbe_set_ringparam,
850 	.get_channels = xgbe_get_channels,
851 	.set_channels = xgbe_set_channels,
852 };
853 
xgbe_get_ethtool_ops(void)854 const struct ethtool_ops *xgbe_get_ethtool_ops(void)
855 {
856 	return &xgbe_ethtool_ops;
857 }
858