]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/net/ethernet/amd/xgbe/xgbe-ethtool.c
Merge tag 'kvm-x86-generic-6.8' of https://github.com/kvm-x86/linux into HEAD
[thirdparty/kernel/stable.git] / drivers / net / ethernet / amd / xgbe / xgbe-ethtool.c
CommitLineData
c5aa9e3b
LT
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 *
757e6aa3 9 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
c5aa9e3b
LT
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 *
757e6aa3 59 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
c5aa9e3b
LT
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>
23e4eef7 119#include <linux/net_tstamp.h>
c5aa9e3b
LT
120
121#include "xgbe.h"
122#include "xgbe-common.h"
123
c5aa9e3b
LT
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, \
c593642c 132 sizeof_field(struct xgbe_mmc_stats, _var), \
c5aa9e3b
LT
133 offsetof(struct xgbe_prv_data, mmc_stats._var), \
134 }
135
5452b2df
LT
136#define XGMAC_EXT_STAT(_string, _var) \
137 { _string, \
c593642c 138 sizeof_field(struct xgbe_ext_stats, _var), \
5452b2df
LT
139 offsetof(struct xgbe_prv_data, ext_stats._var), \
140 }
141
c5aa9e3b
LT
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),
3010608d 149 XGMAC_EXT_STAT("tx_vxlan_packets", tx_vxlan_packets),
5452b2df 150 XGMAC_EXT_STAT("tx_tso_packets", tx_tso_packets),
c5aa9e3b
LT
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),
3010608d 166 XGMAC_EXT_STAT("rx_vxlan_packets", rx_vxlan_packets),
c5aa9e3b
LT
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),
3010608d
LT
182 XGMAC_EXT_STAT("rx_csum_errors", rx_csum_errors),
183 XGMAC_EXT_STAT("rx_vxlan_csum_errors", rx_vxlan_csum_errors),
c5aa9e3b 184 XGMAC_MMC_STAT("rx_pause_frames", rxpauseframes),
5452b2df 185 XGMAC_EXT_STAT("rx_split_header_packets", rx_split_header_packets),
72c9ac4e 186 XGMAC_EXT_STAT("rx_buffer_unavailable", rx_buffer_unavailable),
c5aa9e3b 187};
a2ea14d7 188
c5aa9e3b
LT
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{
80a788c9 193 struct xgbe_prv_data *pdata = netdev_priv(netdev);
c5aa9e3b
LT
194 int i;
195
c5aa9e3b
LT
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 }
80a788c9
LT
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 }
c5aa9e3b
LT
215 break;
216 }
c5aa9e3b
LT
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
c5aa9e3b
LT
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 }
80a788c9
LT
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 }
c5aa9e3b
LT
239}
240
241static int xgbe_get_sset_count(struct net_device *netdev, int stringset)
242{
80a788c9 243 struct xgbe_prv_data *pdata = netdev_priv(netdev);
c5aa9e3b
LT
244 int ret;
245
c5aa9e3b
LT
246 switch (stringset) {
247 case ETH_SS_STATS:
80a788c9
LT
248 ret = XGBE_STATS_COUNT +
249 (pdata->tx_ring_count * 2) +
250 (pdata->rx_ring_count * 2);
c5aa9e3b
LT
251 break;
252
253 default:
254 ret = -EOPNOTSUPP;
255 }
256
c5aa9e3b
LT
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
c1ce2f77
LT
265 pause->autoneg = pdata->phy.pause_autoneg;
266 pause->tx_pause = pdata->phy.tx_pause;
267 pause->rx_pause = pdata->phy.rx_pause;
c5aa9e3b
LT
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);
85f9feb6 274 struct ethtool_link_ksettings *lks = &pdata->phy.lks;
c5aa9e3b
LT
275 int ret = 0;
276
e5dd8b81
LT
277 if (pause->autoneg && (pdata->phy.autoneg != AUTONEG_ENABLE)) {
278 netdev_err(netdev,
beef8516 279 "autoneg disabled, pause autoneg not available\n");
c1ce2f77 280 return -EINVAL;
e5dd8b81 281 }
c1ce2f77
LT
282
283 pdata->phy.pause_autoneg = pause->autoneg;
284 pdata->phy.tx_pause = pause->tx_pause;
285 pdata->phy.rx_pause = pause->rx_pause;
c5aa9e3b 286
85f9feb6
LT
287 XGBE_CLR_ADV(lks, Pause);
288 XGBE_CLR_ADV(lks, Asym_Pause);
c5aa9e3b 289
c1ce2f77 290 if (pause->rx_pause) {
85f9feb6
LT
291 XGBE_SET_ADV(lks, Pause);
292 XGBE_SET_ADV(lks, Asym_Pause);
c5aa9e3b
LT
293 }
294
85f9feb6
LT
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 }
c1ce2f77 302
c5aa9e3b 303 if (netif_running(netdev))
7c12aa08 304 ret = pdata->phy_if.phy_config_aneg(pdata);
c5aa9e3b 305
c5aa9e3b
LT
306 return ret;
307}
308
90fdd04e
PR
309static int xgbe_get_link_ksettings(struct net_device *netdev,
310 struct ethtool_link_ksettings *cmd)
c5aa9e3b
LT
311{
312 struct xgbe_prv_data *pdata = netdev_priv(netdev);
85f9feb6 313 struct ethtool_link_ksettings *lks = &pdata->phy.lks;
c5aa9e3b 314
90fdd04e 315 cmd->base.phy_address = pdata->phy.address;
7c12aa08 316
7a2323ac
RR
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 }
c5aa9e3b 324
7a2323ac 325 cmd->base.autoneg = pdata->phy.autoneg;
90fdd04e 326 cmd->base.port = PORT_NONE;
c5aa9e3b 327
85f9feb6
LT
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
7c12aa08 332 return 0;
c5aa9e3b
LT
333}
334
90fdd04e
PR
335static int xgbe_set_link_ksettings(struct net_device *netdev,
336 const struct ethtool_link_ksettings *cmd)
c5aa9e3b
LT
337{
338 struct xgbe_prv_data *pdata = netdev_priv(netdev);
85f9feb6
LT
339 struct ethtool_link_ksettings *lks = &pdata->phy.lks;
340 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
c5aa9e3b
LT
341 u32 speed;
342 int ret;
343
90fdd04e 344 speed = cmd->base.speed;
c5aa9e3b 345
90fdd04e 346 if (cmd->base.phy_address != pdata->phy.address) {
e5dd8b81 347 netdev_err(netdev, "invalid phy address %hhu\n",
90fdd04e 348 cmd->base.phy_address);
8c43a2cc 349 return -EINVAL;
e5dd8b81 350 }
c5aa9e3b 351
90fdd04e
PR
352 if ((cmd->base.autoneg != AUTONEG_ENABLE) &&
353 (cmd->base.autoneg != AUTONEG_DISABLE)) {
e5dd8b81 354 netdev_err(netdev, "unsupported autoneg %hhu\n",
90fdd04e 355 cmd->base.autoneg);
8c43a2cc 356 return -EINVAL;
e5dd8b81 357 }
c5aa9e3b 358
90fdd04e 359 if (cmd->base.autoneg == AUTONEG_DISABLE) {
e57f7a3f 360 if (!pdata->phy_if.phy_valid_speed(pdata, speed)) {
e5dd8b81 361 netdev_err(netdev, "unsupported speed %u\n", speed);
8c43a2cc 362 return -EINVAL;
f047604a
LT
363 }
364
90fdd04e 365 if (cmd->base.duplex != DUPLEX_FULL) {
e5dd8b81 366 netdev_err(netdev, "unsupported duplex %hhu\n",
90fdd04e 367 cmd->base.duplex);
8c43a2cc 368 return -EINVAL;
e5dd8b81 369 }
f047604a 370 }
c5aa9e3b 371
e5dd8b81 372 netif_dbg(pdata, link, netdev,
85f9feb6
LT
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
4973056c
SA
377 linkmode_and(advertising, cmd->link_modes.advertising,
378 lks->link_modes.supported);
e5dd8b81 379
85f9feb6
LT
380 if ((cmd->base.autoneg == AUTONEG_ENABLE) &&
381 bitmap_empty(advertising, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
e5dd8b81
LT
382 netdev_err(netdev,
383 "unsupported requested advertisement\n");
8c43a2cc 384 return -EINVAL;
e5dd8b81 385 }
c5aa9e3b
LT
386
387 ret = 0;
90fdd04e 388 pdata->phy.autoneg = cmd->base.autoneg;
7c12aa08 389 pdata->phy.speed = speed;
90fdd04e 390 pdata->phy.duplex = cmd->base.duplex;
4973056c 391 linkmode_copy(lks->link_modes.advertising, advertising);
c5aa9e3b 392
90fdd04e 393 if (cmd->base.autoneg == AUTONEG_ENABLE)
85f9feb6 394 XGBE_SET_ADV(lks, Autoneg);
c5aa9e3b 395 else
85f9feb6 396 XGBE_CLR_ADV(lks, Autoneg);
c5aa9e3b
LT
397
398 if (netif_running(netdev))
7c12aa08 399 ret = pdata->phy_if.phy_config_aneg(pdata);
c5aa9e3b 400
c5aa9e3b
LT
401 return ret;
402}
403
404static void xgbe_get_drvinfo(struct net_device *netdev,
405 struct ethtool_drvinfo *drvinfo)
406{
407 struct xgbe_prv_data *pdata = netdev_priv(netdev);
a9a4a2d9 408 struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
c5aa9e3b 409
f029c781
WS
410 strscpy(drvinfo->driver, XGBE_DRV_NAME, sizeof(drvinfo->driver));
411 strscpy(drvinfo->bus_info, dev_name(pdata->dev),
c5aa9e3b
LT
412 sizeof(drvinfo->bus_info));
413 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d.%d.%d",
a9a4a2d9
LT
414 XGMAC_GET_BITS(hw_feat->version, MAC_VR, USERVER),
415 XGMAC_GET_BITS(hw_feat->version, MAC_VR, DEVID),
416 XGMAC_GET_BITS(hw_feat->version, MAC_VR, SNPSVER));
c5aa9e3b
LT
417}
418
349fb2d7
LT
419static u32 xgbe_get_msglevel(struct net_device *netdev)
420{
421 struct xgbe_prv_data *pdata = netdev_priv(netdev);
422
423 return pdata->msg_enable;
424}
425
426static void xgbe_set_msglevel(struct net_device *netdev, u32 msglevel)
427{
428 struct xgbe_prv_data *pdata = netdev_priv(netdev);
429
430 pdata->msg_enable = msglevel;
431}
432
c5aa9e3b 433static int xgbe_get_coalesce(struct net_device *netdev,
f3ccfda1
YM
434 struct ethtool_coalesce *ec,
435 struct kernel_ethtool_coalesce *kernel_coal,
436 struct netlink_ext_ack *extack)
c5aa9e3b
LT
437{
438 struct xgbe_prv_data *pdata = netdev_priv(netdev);
c5aa9e3b 439
c5aa9e3b
LT
440 memset(ec, 0, sizeof(struct ethtool_coalesce));
441
4a57ebcc 442 ec->rx_coalesce_usecs = pdata->rx_usecs;
c5aa9e3b
LT
443 ec->rx_max_coalesced_frames = pdata->rx_frames;
444
c5aa9e3b
LT
445 ec->tx_max_coalesced_frames = pdata->tx_frames;
446
c5aa9e3b
LT
447 return 0;
448}
449
450static int xgbe_set_coalesce(struct net_device *netdev,
f3ccfda1
YM
451 struct ethtool_coalesce *ec,
452 struct kernel_ethtool_coalesce *kernel_coal,
453 struct netlink_ext_ack *extack)
c5aa9e3b
LT
454{
455 struct xgbe_prv_data *pdata = netdev_priv(netdev);
456 struct xgbe_hw_if *hw_if = &pdata->hw_if;
457 unsigned int rx_frames, rx_riwt, rx_usecs;
c635eaac 458 unsigned int tx_frames;
c5aa9e3b 459
c5aa9e3b 460 rx_riwt = hw_if->usec_to_riwt(pdata, ec->rx_coalesce_usecs);
4a57ebcc 461 rx_usecs = ec->rx_coalesce_usecs;
c5aa9e3b
LT
462 rx_frames = ec->rx_max_coalesced_frames;
463
464 /* Use smallest possible value if conversion resulted in zero */
4a57ebcc 465 if (rx_usecs && !rx_riwt)
c5aa9e3b
LT
466 rx_riwt = 1;
467
468 /* Check the bounds of values for Rx */
469 if (rx_riwt > XGMAC_MAX_DMA_RIWT) {
e5dd8b81
LT
470 netdev_err(netdev, "rx-usec is limited to %d usecs\n",
471 hw_if->riwt_to_usec(pdata, XGMAC_MAX_DMA_RIWT));
c5aa9e3b
LT
472 return -EINVAL;
473 }
4780b7ca 474 if (rx_frames > pdata->rx_desc_count) {
e5dd8b81
LT
475 netdev_err(netdev, "rx-frames is limited to %d frames\n",
476 pdata->rx_desc_count);
c5aa9e3b
LT
477 return -EINVAL;
478 }
479
c5aa9e3b
LT
480 tx_frames = ec->tx_max_coalesced_frames;
481
482 /* Check the bounds of values for Tx */
4780b7ca 483 if (tx_frames > pdata->tx_desc_count) {
e5dd8b81
LT
484 netdev_err(netdev, "tx-frames is limited to %d frames\n",
485 pdata->tx_desc_count);
c5aa9e3b
LT
486 return -EINVAL;
487 }
488
489 pdata->rx_riwt = rx_riwt;
4a57ebcc 490 pdata->rx_usecs = rx_usecs;
c5aa9e3b
LT
491 pdata->rx_frames = rx_frames;
492 hw_if->config_rx_coalesce(pdata);
493
c5aa9e3b
LT
494 pdata->tx_frames = tx_frames;
495 hw_if->config_tx_coalesce(pdata);
496
c5aa9e3b
LT
497 return 0;
498}
499
f6ac8628
LT
500static int xgbe_get_rxnfc(struct net_device *netdev,
501 struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
502{
503 struct xgbe_prv_data *pdata = netdev_priv(netdev);
504
505 switch (rxnfc->cmd) {
506 case ETHTOOL_GRXRINGS:
507 rxnfc->data = pdata->rx_ring_count;
508 break;
509 default:
510 return -EOPNOTSUPP;
511 }
512
513 return 0;
514}
515
516static u32 xgbe_get_rxfh_key_size(struct net_device *netdev)
517{
518 struct xgbe_prv_data *pdata = netdev_priv(netdev);
519
520 return sizeof(pdata->rss_key);
521}
522
523static u32 xgbe_get_rxfh_indir_size(struct net_device *netdev)
524{
525 struct xgbe_prv_data *pdata = netdev_priv(netdev);
526
527 return ARRAY_SIZE(pdata->rss_table);
528}
529
892311f6
EP
530static int xgbe_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
531 u8 *hfunc)
f6ac8628
LT
532{
533 struct xgbe_prv_data *pdata = netdev_priv(netdev);
534 unsigned int i;
535
536 if (indir) {
537 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
538 indir[i] = XGMAC_GET_BITS(pdata->rss_table[i],
539 MAC_RSSDR, DMCH);
540 }
541
542 if (key)
543 memcpy(key, pdata->rss_key, sizeof(pdata->rss_key));
544
892311f6
EP
545 if (hfunc)
546 *hfunc = ETH_RSS_HASH_TOP;
547
f6ac8628
LT
548 return 0;
549}
550
551static int xgbe_set_rxfh(struct net_device *netdev, const u32 *indir,
892311f6 552 const u8 *key, const u8 hfunc)
f6ac8628
LT
553{
554 struct xgbe_prv_data *pdata = netdev_priv(netdev);
555 struct xgbe_hw_if *hw_if = &pdata->hw_if;
556 unsigned int ret;
557
e5dd8b81
LT
558 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) {
559 netdev_err(netdev, "unsupported hash function\n");
892311f6 560 return -EOPNOTSUPP;
e5dd8b81 561 }
892311f6 562
f6ac8628
LT
563 if (indir) {
564 ret = hw_if->set_rss_lookup_table(pdata, indir);
565 if (ret)
566 return ret;
567 }
568
569 if (key) {
570 ret = hw_if->set_rss_hash_key(pdata, key);
571 if (ret)
572 return ret;
573 }
574
575 return 0;
576}
577
23e4eef7
LT
578static int xgbe_get_ts_info(struct net_device *netdev,
579 struct ethtool_ts_info *ts_info)
580{
581 struct xgbe_prv_data *pdata = netdev_priv(netdev);
582
583 ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
584 SOF_TIMESTAMPING_RX_SOFTWARE |
585 SOF_TIMESTAMPING_SOFTWARE |
586 SOF_TIMESTAMPING_TX_HARDWARE |
587 SOF_TIMESTAMPING_RX_HARDWARE |
588 SOF_TIMESTAMPING_RAW_HARDWARE;
589
590 if (pdata->ptp_clock)
591 ts_info->phc_index = ptp_clock_index(pdata->ptp_clock);
592 else
593 ts_info->phc_index = -1;
594
595 ts_info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
596 ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
597 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
598 (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
599 (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
600 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
601 (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
602 (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
603 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
604 (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
605 (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
606 (1 << HWTSTAMP_FILTER_ALL);
607
608 return 0;
609}
610
53a1024a
TL
611static int xgbe_get_module_info(struct net_device *netdev,
612 struct ethtool_modinfo *modinfo)
613{
614 struct xgbe_prv_data *pdata = netdev_priv(netdev);
615
616 return pdata->phy_if.module_info(pdata, modinfo);
617}
618
619static int xgbe_get_module_eeprom(struct net_device *netdev,
620 struct ethtool_eeprom *eeprom, u8 *data)
621{
622 struct xgbe_prv_data *pdata = netdev_priv(netdev);
623
624 return pdata->phy_if.module_eeprom(pdata, eeprom, data);
625}
626
74624944
HC
627static void
628xgbe_get_ringparam(struct net_device *netdev,
629 struct ethtool_ringparam *ringparam,
630 struct kernel_ethtool_ringparam *kernel_ringparam,
631 struct netlink_ext_ack *extack)
bab748de
TL
632{
633 struct xgbe_prv_data *pdata = netdev_priv(netdev);
634
635 ringparam->rx_max_pending = XGBE_RX_DESC_CNT_MAX;
636 ringparam->tx_max_pending = XGBE_TX_DESC_CNT_MAX;
637 ringparam->rx_pending = pdata->rx_desc_count;
638 ringparam->tx_pending = pdata->tx_desc_count;
639}
640
641static int xgbe_set_ringparam(struct net_device *netdev,
74624944
HC
642 struct ethtool_ringparam *ringparam,
643 struct kernel_ethtool_ringparam *kernel_ringparam,
644 struct netlink_ext_ack *extack)
bab748de
TL
645{
646 struct xgbe_prv_data *pdata = netdev_priv(netdev);
647 unsigned int rx, tx;
648
649 if (ringparam->rx_mini_pending || ringparam->rx_jumbo_pending) {
650 netdev_err(netdev, "unsupported ring parameter\n");
651 return -EINVAL;
652 }
653
654 if ((ringparam->rx_pending < XGBE_RX_DESC_CNT_MIN) ||
655 (ringparam->rx_pending > XGBE_RX_DESC_CNT_MAX)) {
656 netdev_err(netdev,
657 "rx ring parameter must be between %u and %u\n",
658 XGBE_RX_DESC_CNT_MIN, XGBE_RX_DESC_CNT_MAX);
659 return -EINVAL;
660 }
661
662 if ((ringparam->tx_pending < XGBE_TX_DESC_CNT_MIN) ||
663 (ringparam->tx_pending > XGBE_TX_DESC_CNT_MAX)) {
664 netdev_err(netdev,
665 "tx ring parameter must be between %u and %u\n",
666 XGBE_TX_DESC_CNT_MIN, XGBE_TX_DESC_CNT_MAX);
667 return -EINVAL;
668 }
669
670 rx = __rounddown_pow_of_two(ringparam->rx_pending);
671 if (rx != ringparam->rx_pending)
672 netdev_notice(netdev,
673 "rx ring parameter rounded to power of two: %u\n",
674 rx);
675
676 tx = __rounddown_pow_of_two(ringparam->tx_pending);
677 if (tx != ringparam->tx_pending)
678 netdev_notice(netdev,
679 "tx ring parameter rounded to power of two: %u\n",
680 tx);
681
682 if ((rx == pdata->rx_desc_count) &&
683 (tx == pdata->tx_desc_count))
684 goto out;
685
686 pdata->rx_desc_count = rx;
687 pdata->tx_desc_count = tx;
688
689 xgbe_restart_dev(pdata);
690
691out:
692 return 0;
693}
694
01b5277f
TL
695static void xgbe_get_channels(struct net_device *netdev,
696 struct ethtool_channels *channels)
697{
698 struct xgbe_prv_data *pdata = netdev_priv(netdev);
699 unsigned int rx, tx, combined;
700
701 /* Calculate maximums allowed:
702 * - Take into account the number of available IRQs
703 * - Do not take into account the number of online CPUs so that
704 * the user can over-subscribe if desired
705 * - Tx is additionally limited by the number of hardware queues
706 */
707 rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
708 rx = min(rx, pdata->channel_irq_count);
709 tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
710 tx = min(tx, pdata->channel_irq_count);
711 tx = min(tx, pdata->tx_max_q_count);
712
713 combined = min(rx, tx);
714
715 channels->max_combined = combined;
716 channels->max_rx = rx ? rx - 1 : 0;
717 channels->max_tx = tx ? tx - 1 : 0;
718
719 /* Get current settings based on device state */
720 rx = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
721 tx = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
722
723 combined = min(rx, tx);
724 rx -= combined;
725 tx -= combined;
726
727 channels->combined_count = combined;
728 channels->rx_count = rx;
729 channels->tx_count = tx;
730}
731
732static void xgbe_print_set_channels_input(struct net_device *netdev,
733 struct ethtool_channels *channels)
734{
735 netdev_err(netdev, "channel inputs: combined=%u, rx-only=%u, tx-only=%u\n",
736 channels->combined_count, channels->rx_count,
737 channels->tx_count);
738}
739
740static int xgbe_set_channels(struct net_device *netdev,
741 struct ethtool_channels *channels)
742{
743 struct xgbe_prv_data *pdata = netdev_priv(netdev);
744 unsigned int rx, rx_curr, tx, tx_curr, combined;
745
746 /* Calculate maximums allowed:
747 * - Take into account the number of available IRQs
748 * - Do not take into account the number of online CPUs so that
749 * the user can over-subscribe if desired
750 * - Tx is additionally limited by the number of hardware queues
751 */
752 rx = min(pdata->hw_feat.rx_ch_cnt, pdata->rx_max_channel_count);
753 rx = min(rx, pdata->channel_irq_count);
754 tx = min(pdata->hw_feat.tx_ch_cnt, pdata->tx_max_channel_count);
755 tx = min(tx, pdata->tx_max_q_count);
756 tx = min(tx, pdata->channel_irq_count);
757
758 combined = min(rx, tx);
759
760 /* Should not be setting other count */
761 if (channels->other_count) {
762 netdev_err(netdev,
763 "other channel count must be zero\n");
764 return -EINVAL;
765 }
766
767 /* Require at least one Combined (Rx and Tx) channel */
768 if (!channels->combined_count) {
769 netdev_err(netdev,
770 "at least one combined Rx/Tx channel is required\n");
771 xgbe_print_set_channels_input(netdev, channels);
772 return -EINVAL;
773 }
774
775 /* Check combined channels */
776 if (channels->combined_count > combined) {
777 netdev_err(netdev,
778 "combined channel count cannot exceed %u\n",
779 combined);
780 xgbe_print_set_channels_input(netdev, channels);
781 return -EINVAL;
782 }
783
784 /* Can have some Rx-only or Tx-only channels, but not both */
785 if (channels->rx_count && channels->tx_count) {
786 netdev_err(netdev,
787 "cannot specify both Rx-only and Tx-only channels\n");
788 xgbe_print_set_channels_input(netdev, channels);
789 return -EINVAL;
790 }
791
792 /* Check that we don't exceed the maximum number of channels */
793 if ((channels->combined_count + channels->rx_count) > rx) {
794 netdev_err(netdev,
795 "total Rx channels (%u) requested exceeds maximum available (%u)\n",
796 channels->combined_count + channels->rx_count, rx);
797 xgbe_print_set_channels_input(netdev, channels);
798 return -EINVAL;
799 }
800
801 if ((channels->combined_count + channels->tx_count) > tx) {
802 netdev_err(netdev,
803 "total Tx channels (%u) requested exceeds maximum available (%u)\n",
804 channels->combined_count + channels->tx_count, tx);
805 xgbe_print_set_channels_input(netdev, channels);
806 return -EINVAL;
807 }
808
809 rx = channels->combined_count + channels->rx_count;
810 tx = channels->combined_count + channels->tx_count;
811
812 rx_curr = pdata->new_rx_ring_count ? : pdata->rx_ring_count;
813 tx_curr = pdata->new_tx_ring_count ? : pdata->tx_ring_count;
814
815 if ((rx == rx_curr) && (tx == tx_curr))
816 goto out;
817
818 pdata->new_rx_ring_count = rx;
819 pdata->new_tx_ring_count = tx;
820
821 xgbe_full_restart_dev(pdata);
822
823out:
824 return 0;
825}
826
c5aa9e3b 827static const struct ethtool_ops xgbe_ethtool_ops = {
4a1ce010
JK
828 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
829 ETHTOOL_COALESCE_MAX_FRAMES,
c5aa9e3b 830 .get_drvinfo = xgbe_get_drvinfo,
349fb2d7
LT
831 .get_msglevel = xgbe_get_msglevel,
832 .set_msglevel = xgbe_set_msglevel,
c5aa9e3b
LT
833 .get_link = ethtool_op_get_link,
834 .get_coalesce = xgbe_get_coalesce,
835 .set_coalesce = xgbe_set_coalesce,
836 .get_pauseparam = xgbe_get_pauseparam,
837 .set_pauseparam = xgbe_set_pauseparam,
838 .get_strings = xgbe_get_strings,
839 .get_ethtool_stats = xgbe_get_ethtool_stats,
840 .get_sset_count = xgbe_get_sset_count,
f6ac8628
LT
841 .get_rxnfc = xgbe_get_rxnfc,
842 .get_rxfh_key_size = xgbe_get_rxfh_key_size,
843 .get_rxfh_indir_size = xgbe_get_rxfh_indir_size,
844 .get_rxfh = xgbe_get_rxfh,
845 .set_rxfh = xgbe_set_rxfh,
23e4eef7 846 .get_ts_info = xgbe_get_ts_info,
90fdd04e
PR
847 .get_link_ksettings = xgbe_get_link_ksettings,
848 .set_link_ksettings = xgbe_set_link_ksettings,
53a1024a
TL
849 .get_module_info = xgbe_get_module_info,
850 .get_module_eeprom = xgbe_get_module_eeprom,
bab748de
TL
851 .get_ringparam = xgbe_get_ringparam,
852 .set_ringparam = xgbe_set_ringparam,
01b5277f
TL
853 .get_channels = xgbe_get_channels,
854 .set_channels = xgbe_set_channels,
c5aa9e3b
LT
855};
856
ce0b15d1 857const struct ethtool_ops *xgbe_get_ethtool_ops(void)
c5aa9e3b 858{
ce0b15d1 859 return &xgbe_ethtool_ops;
c5aa9e3b 860}