]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/ethernet/netronome/nfp/nfp_net_ethtool.c
nfp: report port type in ethtool
[mirror_ubuntu-bionic-kernel.git] / drivers / net / ethernet / netronome / nfp / nfp_net_ethtool.c
CommitLineData
4c352362 1/*
2633beb9 2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4c352362
JK
3 *
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34/*
35 * nfp_net_ethtool.c
36 * Netronome network device driver: ethtool support
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
40 * Brad Petrus <brad.petrus@netronome.com>
41 */
42
9ff304bf 43#include <linux/bitfield.h>
4c352362
JK
44#include <linux/kernel.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/interrupt.h>
48#include <linux/pci.h>
49#include <linux/ethtool.h>
50
bd5ca062 51#include "nfpcore/nfp.h"
21d529d5 52#include "nfpcore/nfp_nsp_eth.h"
4c352362
JK
53#include "nfp_net_ctrl.h"
54#include "nfp_net.h"
55
af623682
JK
56enum nfp_dump_diag {
57 NFP_DUMP_NSP_DIAG = 0,
58};
59
4c352362
JK
60/* Support for stats. Returns netdev, driver, and device stats */
61enum { NETDEV_ET_STATS, NFP_NET_DRV_ET_STATS, NFP_NET_DEV_ET_STATS };
62struct _nfp_net_et_stats {
63 char name[ETH_GSTRING_LEN];
64 int type;
65 int sz;
66 int off;
67};
68
69#define NN_ET_NETDEV_STAT(m) NETDEV_ET_STATS, \
70 FIELD_SIZEOF(struct net_device_stats, m), \
71 offsetof(struct net_device_stats, m)
72/* For stats in the control BAR (other than Q stats) */
73#define NN_ET_DEV_STAT(m) NFP_NET_DEV_ET_STATS, \
74 sizeof(u64), \
75 (m)
76static const struct _nfp_net_et_stats nfp_net_et_stats[] = {
77 /* netdev stats */
78 {"rx_packets", NN_ET_NETDEV_STAT(rx_packets)},
79 {"tx_packets", NN_ET_NETDEV_STAT(tx_packets)},
80 {"rx_bytes", NN_ET_NETDEV_STAT(rx_bytes)},
81 {"tx_bytes", NN_ET_NETDEV_STAT(tx_bytes)},
82 {"rx_errors", NN_ET_NETDEV_STAT(rx_errors)},
83 {"tx_errors", NN_ET_NETDEV_STAT(tx_errors)},
84 {"rx_dropped", NN_ET_NETDEV_STAT(rx_dropped)},
85 {"tx_dropped", NN_ET_NETDEV_STAT(tx_dropped)},
86 {"multicast", NN_ET_NETDEV_STAT(multicast)},
87 {"collisions", NN_ET_NETDEV_STAT(collisions)},
88 {"rx_over_errors", NN_ET_NETDEV_STAT(rx_over_errors)},
89 {"rx_crc_errors", NN_ET_NETDEV_STAT(rx_crc_errors)},
90 {"rx_frame_errors", NN_ET_NETDEV_STAT(rx_frame_errors)},
91 {"rx_fifo_errors", NN_ET_NETDEV_STAT(rx_fifo_errors)},
92 {"rx_missed_errors", NN_ET_NETDEV_STAT(rx_missed_errors)},
93 {"tx_aborted_errors", NN_ET_NETDEV_STAT(tx_aborted_errors)},
94 {"tx_carrier_errors", NN_ET_NETDEV_STAT(tx_carrier_errors)},
95 {"tx_fifo_errors", NN_ET_NETDEV_STAT(tx_fifo_errors)},
96 /* Stats from the device */
97 {"dev_rx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_DISCARDS)},
98 {"dev_rx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_ERRORS)},
99 {"dev_rx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_OCTETS)},
100 {"dev_rx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_UC_OCTETS)},
101 {"dev_rx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_OCTETS)},
102 {"dev_rx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_OCTETS)},
103 {"dev_rx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_FRAMES)},
104 {"dev_rx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_FRAMES)},
105 {"dev_rx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_FRAMES)},
106
107 {"dev_tx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_DISCARDS)},
108 {"dev_tx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_ERRORS)},
109 {"dev_tx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_OCTETS)},
110 {"dev_tx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_UC_OCTETS)},
111 {"dev_tx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_OCTETS)},
112 {"dev_tx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_OCTETS)},
113 {"dev_tx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_FRAMES)},
114 {"dev_tx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_FRAMES)},
115 {"dev_tx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_FRAMES)},
66860beb
JK
116
117 {"bpf_pass_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP0_FRAMES)},
118 {"bpf_pass_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP0_BYTES)},
119 /* see comments in outro functions in nfp_bpf_jit.c to find out
120 * how different BPF modes use app-specific counters
121 */
122 {"bpf_app1_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP1_FRAMES)},
123 {"bpf_app1_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP1_BYTES)},
124 {"bpf_app2_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP2_FRAMES)},
125 {"bpf_app2_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP2_BYTES)},
126 {"bpf_app3_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP3_FRAMES)},
127 {"bpf_app3_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP3_BYTES)},
4c352362
JK
128};
129
130#define NN_ET_GLOBAL_STATS_LEN ARRAY_SIZE(nfp_net_et_stats)
79c12a75 131#define NN_ET_RVEC_STATS_LEN (nn->dp.num_r_vecs * 3)
4c352362 132#define NN_ET_RVEC_GATHER_STATS 7
79c12a75 133#define NN_ET_QUEUE_STATS_LEN ((nn->dp.num_tx_rings + nn->dp.num_rx_rings) * 2)
4c352362
JK
134#define NN_ET_STATS_LEN (NN_ET_GLOBAL_STATS_LEN + NN_ET_RVEC_GATHER_STATS + \
135 NN_ET_RVEC_STATS_LEN + NN_ET_QUEUE_STATS_LEN)
136
bd5ca062
JK
137static void nfp_net_get_nspinfo(struct nfp_net *nn, char *version)
138{
139 struct nfp_nsp *nsp;
140
141 if (!nn->cpp)
142 return;
143
144 nsp = nfp_nsp_open(nn->cpp);
145 if (IS_ERR(nsp))
146 return;
147
148 snprintf(version, ETHTOOL_FWVERS_LEN, "sp:%hu.%hu",
149 nfp_nsp_get_abi_ver_major(nsp),
150 nfp_nsp_get_abi_ver_minor(nsp));
151
152 nfp_nsp_close(nsp);
153}
154
4c352362
JK
155static void nfp_net_get_drvinfo(struct net_device *netdev,
156 struct ethtool_drvinfo *drvinfo)
157{
bd5ca062 158 char nsp_version[ETHTOOL_FWVERS_LEN] = {};
4c352362
JK
159 struct nfp_net *nn = netdev_priv(netdev);
160
2633beb9
JK
161 strlcpy(drvinfo->driver, nn->pdev->driver->name,
162 sizeof(drvinfo->driver));
163 strlcpy(drvinfo->version, nfp_driver_version, sizeof(drvinfo->version));
4c352362 164
bd5ca062 165 nfp_net_get_nspinfo(nn, nsp_version);
4c352362 166 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
bd5ca062 167 "%d.%d.%d.%d %s",
4c352362 168 nn->fw_ver.resv, nn->fw_ver.class,
bd5ca062 169 nn->fw_ver.major, nn->fw_ver.minor, nsp_version);
4c352362
JK
170 strlcpy(drvinfo->bus_info, pci_name(nn->pdev),
171 sizeof(drvinfo->bus_info));
172
173 drvinfo->n_stats = NN_ET_STATS_LEN;
174 drvinfo->regdump_len = NFP_NET_CFG_BAR_SZ;
175}
176
265aeb51
JK
177/**
178 * nfp_net_get_link_ksettings - Get Link Speed settings
179 * @netdev: network interface device structure
180 * @cmd: ethtool command
181 *
182 * Reports speed settings based on info in the BAR provided by the fw.
183 */
184static int
185nfp_net_get_link_ksettings(struct net_device *netdev,
186 struct ethtool_link_ksettings *cmd)
187{
188 static const u32 ls_to_ethtool[] = {
189 [NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED] = 0,
190 [NFP_NET_CFG_STS_LINK_RATE_UNKNOWN] = SPEED_UNKNOWN,
191 [NFP_NET_CFG_STS_LINK_RATE_1G] = SPEED_1000,
192 [NFP_NET_CFG_STS_LINK_RATE_10G] = SPEED_10000,
193 [NFP_NET_CFG_STS_LINK_RATE_25G] = SPEED_25000,
194 [NFP_NET_CFG_STS_LINK_RATE_40G] = SPEED_40000,
195 [NFP_NET_CFG_STS_LINK_RATE_50G] = SPEED_50000,
196 [NFP_NET_CFG_STS_LINK_RATE_100G] = SPEED_100000,
197 };
198 struct nfp_net *nn = netdev_priv(netdev);
199 u32 sts, ls;
200
201 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
202 cmd->base.port = PORT_OTHER;
203 cmd->base.speed = SPEED_UNKNOWN;
204 cmd->base.duplex = DUPLEX_UNKNOWN;
205
42b1e6aa
JK
206 if (nn->eth_port)
207 cmd->base.autoneg = nn->eth_port->aneg != NFP_ANEG_DISABLED ?
208 AUTONEG_ENABLE : AUTONEG_DISABLE;
209
265aeb51
JK
210 if (!netif_carrier_ok(netdev))
211 return 0;
212
21d529d5
JK
213 /* Use link speed from ETH table if available, otherwise try the BAR */
214 if (nn->eth_port && nfp_net_link_changed_read_clear(nn))
215 nfp_net_refresh_port_config(nn);
216 /* Separate if - on FW error the port could've disappeared from table */
217 if (nn->eth_port) {
9f9e0da5 218 cmd->base.port = nn->eth_port->port_type;
21d529d5
JK
219 cmd->base.speed = nn->eth_port->speed;
220 cmd->base.duplex = DUPLEX_FULL;
221 return 0;
222 }
223
265aeb51
JK
224 sts = nn_readl(nn, NFP_NET_CFG_STS);
225
226 ls = FIELD_GET(NFP_NET_CFG_STS_LINK_RATE, sts);
227 if (ls == NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED)
228 return -EOPNOTSUPP;
229
230 if (ls == NFP_NET_CFG_STS_LINK_RATE_UNKNOWN ||
231 ls >= ARRAY_SIZE(ls_to_ethtool))
232 return 0;
233
234 cmd->base.speed = ls_to_ethtool[sts];
235 cmd->base.duplex = DUPLEX_FULL;
236
237 return 0;
238}
239
4c352362
JK
240static void nfp_net_get_ringparam(struct net_device *netdev,
241 struct ethtool_ringparam *ring)
242{
243 struct nfp_net *nn = netdev_priv(netdev);
244
245 ring->rx_max_pending = NFP_NET_MAX_RX_DESCS;
246 ring->tx_max_pending = NFP_NET_MAX_TX_DESCS;
79c12a75
JK
247 ring->rx_pending = nn->dp.rxd_cnt;
248 ring->tx_pending = nn->dp.txd_cnt;
4c352362
JK
249}
250
68453c7a
JK
251static int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
252{
783496b0 253 struct nfp_net_dp *dp;
68453c7a 254
783496b0
JK
255 dp = nfp_net_clone_dp(nn);
256 if (!dp)
257 return -ENOMEM;
258
892a7f70
JK
259 dp->rxd_cnt = rxd_cnt;
260 dp->txd_cnt = txd_cnt;
261
262 return nfp_net_ring_reconfig(nn, dp);
68453c7a
JK
263}
264
4c352362
JK
265static int nfp_net_set_ringparam(struct net_device *netdev,
266 struct ethtool_ringparam *ring)
267{
268 struct nfp_net *nn = netdev_priv(netdev);
269 u32 rxd_cnt, txd_cnt;
270
4c352362
JK
271 /* We don't have separate queues/rings for small/large frames. */
272 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
273 return -EINVAL;
274
275 /* Round up to supported values */
276 rxd_cnt = roundup_pow_of_two(ring->rx_pending);
4c352362 277 txd_cnt = roundup_pow_of_two(ring->tx_pending);
4c352362 278
cc7c0333
JK
279 if (rxd_cnt < NFP_NET_MIN_RX_DESCS || rxd_cnt > NFP_NET_MAX_RX_DESCS ||
280 txd_cnt < NFP_NET_MIN_TX_DESCS || txd_cnt > NFP_NET_MAX_TX_DESCS)
281 return -EINVAL;
282
79c12a75 283 if (nn->dp.rxd_cnt == rxd_cnt && nn->dp.txd_cnt == txd_cnt)
cc7c0333 284 return 0;
4c352362 285
cc7c0333 286 nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n",
79c12a75 287 nn->dp.rxd_cnt, rxd_cnt, nn->dp.txd_cnt, txd_cnt);
4c352362 288
cc7c0333 289 return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt);
4c352362
JK
290}
291
292static void nfp_net_get_strings(struct net_device *netdev,
293 u32 stringset, u8 *data)
294{
295 struct nfp_net *nn = netdev_priv(netdev);
296 u8 *p = data;
297 int i;
298
299 switch (stringset) {
300 case ETH_SS_STATS:
301 for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
302 memcpy(p, nfp_net_et_stats[i].name, ETH_GSTRING_LEN);
303 p += ETH_GSTRING_LEN;
304 }
79c12a75 305 for (i = 0; i < nn->dp.num_r_vecs; i++) {
4c352362
JK
306 sprintf(p, "rvec_%u_rx_pkts", i);
307 p += ETH_GSTRING_LEN;
308 sprintf(p, "rvec_%u_tx_pkts", i);
309 p += ETH_GSTRING_LEN;
310 sprintf(p, "rvec_%u_tx_busy", i);
311 p += ETH_GSTRING_LEN;
312 }
313 strncpy(p, "hw_rx_csum_ok", ETH_GSTRING_LEN);
314 p += ETH_GSTRING_LEN;
315 strncpy(p, "hw_rx_csum_inner_ok", ETH_GSTRING_LEN);
316 p += ETH_GSTRING_LEN;
317 strncpy(p, "hw_rx_csum_err", ETH_GSTRING_LEN);
318 p += ETH_GSTRING_LEN;
319 strncpy(p, "hw_tx_csum", ETH_GSTRING_LEN);
320 p += ETH_GSTRING_LEN;
321 strncpy(p, "hw_tx_inner_csum", ETH_GSTRING_LEN);
322 p += ETH_GSTRING_LEN;
323 strncpy(p, "tx_gather", ETH_GSTRING_LEN);
324 p += ETH_GSTRING_LEN;
325 strncpy(p, "tx_lso", ETH_GSTRING_LEN);
326 p += ETH_GSTRING_LEN;
79c12a75 327 for (i = 0; i < nn->dp.num_tx_rings; i++) {
4c352362
JK
328 sprintf(p, "txq_%u_pkts", i);
329 p += ETH_GSTRING_LEN;
330 sprintf(p, "txq_%u_bytes", i);
331 p += ETH_GSTRING_LEN;
332 }
79c12a75 333 for (i = 0; i < nn->dp.num_rx_rings; i++) {
4c352362
JK
334 sprintf(p, "rxq_%u_pkts", i);
335 p += ETH_GSTRING_LEN;
336 sprintf(p, "rxq_%u_bytes", i);
337 p += ETH_GSTRING_LEN;
338 }
339 break;
340 }
341}
342
343static void nfp_net_get_stats(struct net_device *netdev,
344 struct ethtool_stats *stats, u64 *data)
345{
346 u64 gathered_stats[NN_ET_RVEC_GATHER_STATS] = {};
347 struct nfp_net *nn = netdev_priv(netdev);
348 struct rtnl_link_stats64 *netdev_stats;
349 struct rtnl_link_stats64 temp = {};
350 u64 tmp[NN_ET_RVEC_GATHER_STATS];
351 u8 __iomem *io_p;
352 int i, j, k;
353 u8 *p;
354
355 netdev_stats = dev_get_stats(netdev, &temp);
356
357 for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
358 switch (nfp_net_et_stats[i].type) {
359 case NETDEV_ET_STATS:
360 p = (char *)netdev_stats + nfp_net_et_stats[i].off;
361 data[i] = nfp_net_et_stats[i].sz == sizeof(u64) ?
362 *(u64 *)p : *(u32 *)p;
363 break;
364
365 case NFP_NET_DEV_ET_STATS:
d2b84397 366 io_p = nn->dp.ctrl_bar + nfp_net_et_stats[i].off;
4c352362
JK
367 data[i] = readq(io_p);
368 break;
369 }
370 }
79c12a75 371 for (j = 0; j < nn->dp.num_r_vecs; j++) {
4c352362
JK
372 unsigned int start;
373
374 do {
375 start = u64_stats_fetch_begin(&nn->r_vecs[j].rx_sync);
376 data[i++] = nn->r_vecs[j].rx_pkts;
377 tmp[0] = nn->r_vecs[j].hw_csum_rx_ok;
378 tmp[1] = nn->r_vecs[j].hw_csum_rx_inner_ok;
379 tmp[2] = nn->r_vecs[j].hw_csum_rx_error;
380 } while (u64_stats_fetch_retry(&nn->r_vecs[j].rx_sync, start));
381
382 do {
383 start = u64_stats_fetch_begin(&nn->r_vecs[j].tx_sync);
384 data[i++] = nn->r_vecs[j].tx_pkts;
385 data[i++] = nn->r_vecs[j].tx_busy;
386 tmp[3] = nn->r_vecs[j].hw_csum_tx;
387 tmp[4] = nn->r_vecs[j].hw_csum_tx_inner;
388 tmp[5] = nn->r_vecs[j].tx_gather;
389 tmp[6] = nn->r_vecs[j].tx_lso;
390 } while (u64_stats_fetch_retry(&nn->r_vecs[j].tx_sync, start));
391
392 for (k = 0; k < NN_ET_RVEC_GATHER_STATS; k++)
393 gathered_stats[k] += tmp[k];
394 }
395 for (j = 0; j < NN_ET_RVEC_GATHER_STATS; j++)
396 data[i++] = gathered_stats[j];
79c12a75 397 for (j = 0; j < nn->dp.num_tx_rings; j++) {
d2b84397 398 io_p = nn->dp.ctrl_bar + NFP_NET_CFG_TXR_STATS(j);
4c352362 399 data[i++] = readq(io_p);
d2b84397 400 io_p = nn->dp.ctrl_bar + NFP_NET_CFG_TXR_STATS(j) + 8;
4c352362
JK
401 data[i++] = readq(io_p);
402 }
79c12a75 403 for (j = 0; j < nn->dp.num_rx_rings; j++) {
d2b84397 404 io_p = nn->dp.ctrl_bar + NFP_NET_CFG_RXR_STATS(j);
4c352362 405 data[i++] = readq(io_p);
d2b84397 406 io_p = nn->dp.ctrl_bar + NFP_NET_CFG_RXR_STATS(j) + 8;
4c352362
JK
407 data[i++] = readq(io_p);
408 }
409}
410
411static int nfp_net_get_sset_count(struct net_device *netdev, int sset)
412{
413 struct nfp_net *nn = netdev_priv(netdev);
414
415 switch (sset) {
416 case ETH_SS_STATS:
417 return NN_ET_STATS_LEN;
418 default:
419 return -EOPNOTSUPP;
420 }
421}
422
423/* RX network flow classification (RSS, filters, etc)
424 */
425static u32 ethtool_flow_to_nfp_flag(u32 flow_type)
426{
427 static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = {
428 [TCP_V4_FLOW] = NFP_NET_CFG_RSS_IPV4_TCP,
429 [TCP_V6_FLOW] = NFP_NET_CFG_RSS_IPV6_TCP,
430 [UDP_V4_FLOW] = NFP_NET_CFG_RSS_IPV4_UDP,
431 [UDP_V6_FLOW] = NFP_NET_CFG_RSS_IPV6_UDP,
432 [IPV4_FLOW] = NFP_NET_CFG_RSS_IPV4,
433 [IPV6_FLOW] = NFP_NET_CFG_RSS_IPV6,
434 };
435
436 if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp))
437 return 0;
438
439 return xlate_ethtool_to_nfp[flow_type];
440}
441
442static int nfp_net_get_rss_hash_opts(struct nfp_net *nn,
443 struct ethtool_rxnfc *cmd)
444{
445 u32 nfp_rss_flag;
446
447 cmd->data = 0;
448
449 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
450 return -EOPNOTSUPP;
451
452 nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type);
453 if (!nfp_rss_flag)
454 return -EINVAL;
455
456 cmd->data |= RXH_IP_SRC | RXH_IP_DST;
457 if (nn->rss_cfg & nfp_rss_flag)
458 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
459
460 return 0;
461}
462
463static int nfp_net_get_rxnfc(struct net_device *netdev,
464 struct ethtool_rxnfc *cmd, u32 *rule_locs)
465{
466 struct nfp_net *nn = netdev_priv(netdev);
467
468 switch (cmd->cmd) {
469 case ETHTOOL_GRXRINGS:
79c12a75 470 cmd->data = nn->dp.num_rx_rings;
4c352362
JK
471 return 0;
472 case ETHTOOL_GRXFH:
473 return nfp_net_get_rss_hash_opts(nn, cmd);
474 default:
475 return -EOPNOTSUPP;
476 }
477}
478
479static int nfp_net_set_rss_hash_opt(struct nfp_net *nn,
480 struct ethtool_rxnfc *nfc)
481{
482 u32 new_rss_cfg = nn->rss_cfg;
483 u32 nfp_rss_flag;
484 int err;
485
486 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
487 return -EOPNOTSUPP;
488
489 /* RSS only supports IP SA/DA and L4 src/dst ports */
490 if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
491 RXH_L4_B_0_1 | RXH_L4_B_2_3))
492 return -EINVAL;
493
494 /* We need at least the IP SA/DA fields for hashing */
495 if (!(nfc->data & RXH_IP_SRC) ||
496 !(nfc->data & RXH_IP_DST))
497 return -EINVAL;
498
499 nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type);
500 if (!nfp_rss_flag)
501 return -EINVAL;
502
503 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
504 case 0:
505 new_rss_cfg &= ~nfp_rss_flag;
506 break;
507 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
508 new_rss_cfg |= nfp_rss_flag;
509 break;
510 default:
511 return -EINVAL;
512 }
513
9ff304bf 514 new_rss_cfg |= FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc);
4c352362
JK
515 new_rss_cfg |= NFP_NET_CFG_RSS_MASK;
516
517 if (new_rss_cfg == nn->rss_cfg)
518 return 0;
519
d2b84397 520 writel(new_rss_cfg, nn->dp.ctrl_bar + NFP_NET_CFG_RSS_CTRL);
4c352362
JK
521 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
522 if (err)
523 return err;
524
525 nn->rss_cfg = new_rss_cfg;
526
527 nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg);
528 return 0;
529}
530
531static int nfp_net_set_rxnfc(struct net_device *netdev,
532 struct ethtool_rxnfc *cmd)
533{
534 struct nfp_net *nn = netdev_priv(netdev);
535
536 switch (cmd->cmd) {
537 case ETHTOOL_SRXFH:
538 return nfp_net_set_rss_hash_opt(nn, cmd);
539 default:
540 return -EOPNOTSUPP;
541 }
542}
543
544static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev)
545{
546 struct nfp_net *nn = netdev_priv(netdev);
547
548 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
549 return 0;
550
551 return ARRAY_SIZE(nn->rss_itbl);
552}
553
554static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev)
555{
9ff304bf
JK
556 struct nfp_net *nn = netdev_priv(netdev);
557
558 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
559 return -EOPNOTSUPP;
560
561 return nfp_net_rss_key_sz(nn);
4c352362
JK
562}
563
564static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
565 u8 *hfunc)
566{
567 struct nfp_net *nn = netdev_priv(netdev);
568 int i;
569
570 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
571 return -EOPNOTSUPP;
572
573 if (indir)
574 for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
575 indir[i] = nn->rss_itbl[i];
576 if (key)
9ff304bf
JK
577 memcpy(key, nn->rss_key, nfp_net_rss_key_sz(nn));
578 if (hfunc) {
579 *hfunc = nn->rss_hfunc;
580 if (*hfunc >= 1 << ETH_RSS_HASH_FUNCS_COUNT)
581 *hfunc = ETH_RSS_HASH_UNKNOWN;
582 }
4c352362
JK
583
584 return 0;
585}
586
587static int nfp_net_set_rxfh(struct net_device *netdev,
588 const u32 *indir, const u8 *key,
589 const u8 hfunc)
590{
591 struct nfp_net *nn = netdev_priv(netdev);
592 int i;
593
594 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
9ff304bf 595 !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == nn->rss_hfunc))
4c352362
JK
596 return -EOPNOTSUPP;
597
598 if (!key && !indir)
599 return 0;
600
601 if (key) {
9ff304bf 602 memcpy(nn->rss_key, key, nfp_net_rss_key_sz(nn));
4c352362
JK
603 nfp_net_rss_write_key(nn);
604 }
605 if (indir) {
606 for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
607 nn->rss_itbl[i] = indir[i];
608
609 nfp_net_rss_write_itbl(nn);
610 }
611
612 return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
613}
614
615/* Dump BAR registers
616 */
617static int nfp_net_get_regs_len(struct net_device *netdev)
618{
619 return NFP_NET_CFG_BAR_SZ;
620}
621
622static void nfp_net_get_regs(struct net_device *netdev,
623 struct ethtool_regs *regs, void *p)
624{
625 struct nfp_net *nn = netdev_priv(netdev);
626 u32 *regs_buf = p;
627 int i;
628
629 regs->version = nn_readl(nn, NFP_NET_CFG_VERSION);
630
631 for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++)
d2b84397 632 regs_buf[i] = readl(nn->dp.ctrl_bar + (i * sizeof(u32)));
4c352362
JK
633}
634
635static int nfp_net_get_coalesce(struct net_device *netdev,
636 struct ethtool_coalesce *ec)
637{
638 struct nfp_net *nn = netdev_priv(netdev);
639
640 if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
641 return -EINVAL;
642
643 ec->rx_coalesce_usecs = nn->rx_coalesce_usecs;
644 ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames;
645 ec->tx_coalesce_usecs = nn->tx_coalesce_usecs;
646 ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames;
647
648 return 0;
649}
650
af623682
JK
651/* Other debug dumps
652 */
653static int
654nfp_dump_nsp_diag(struct nfp_net *nn, struct ethtool_dump *dump, void *buffer)
655{
656 struct nfp_resource *res;
657 int ret;
658
659 if (!nn->cpp)
660 return -EOPNOTSUPP;
661
662 dump->version = 1;
663 dump->flag = NFP_DUMP_NSP_DIAG;
664
665 res = nfp_resource_acquire(nn->cpp, NFP_RESOURCE_NSP_DIAG);
666 if (IS_ERR(res))
667 return PTR_ERR(res);
668
669 if (buffer) {
670 if (dump->len != nfp_resource_size(res)) {
671 ret = -EINVAL;
672 goto exit_release;
673 }
674
675 ret = nfp_cpp_read(nn->cpp, nfp_resource_cpp_id(res),
676 nfp_resource_address(res),
677 buffer, dump->len);
678 if (ret != dump->len)
679 ret = ret < 0 ? ret : -EIO;
680 else
681 ret = 0;
682 } else {
683 dump->len = nfp_resource_size(res);
684 ret = 0;
685 }
686exit_release:
687 nfp_resource_release(res);
688
689 return ret;
690}
691
692static int nfp_net_set_dump(struct net_device *netdev, struct ethtool_dump *val)
693{
694 struct nfp_net *nn = netdev_priv(netdev);
695
696 if (!nn->cpp)
697 return -EOPNOTSUPP;
698
699 if (val->flag != NFP_DUMP_NSP_DIAG)
700 return -EINVAL;
701
702 nn->ethtool_dump_flag = val->flag;
703
704 return 0;
705}
706
707static int
708nfp_net_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
709{
710 return nfp_dump_nsp_diag(netdev_priv(netdev), dump, NULL);
711}
712
713static int
714nfp_net_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
715 void *buffer)
716{
717 return nfp_dump_nsp_diag(netdev_priv(netdev), dump, buffer);
718}
719
4c352362
JK
720static int nfp_net_set_coalesce(struct net_device *netdev,
721 struct ethtool_coalesce *ec)
722{
723 struct nfp_net *nn = netdev_priv(netdev);
724 unsigned int factor;
725
726 if (ec->rx_coalesce_usecs_irq ||
727 ec->rx_max_coalesced_frames_irq ||
728 ec->tx_coalesce_usecs_irq ||
729 ec->tx_max_coalesced_frames_irq ||
730 ec->stats_block_coalesce_usecs ||
731 ec->use_adaptive_rx_coalesce ||
732 ec->use_adaptive_tx_coalesce ||
733 ec->pkt_rate_low ||
734 ec->rx_coalesce_usecs_low ||
735 ec->rx_max_coalesced_frames_low ||
736 ec->tx_coalesce_usecs_low ||
737 ec->tx_max_coalesced_frames_low ||
738 ec->pkt_rate_high ||
739 ec->rx_coalesce_usecs_high ||
740 ec->rx_max_coalesced_frames_high ||
741 ec->tx_coalesce_usecs_high ||
742 ec->tx_max_coalesced_frames_high ||
743 ec->rate_sample_interval)
744 return -ENOTSUPP;
745
746 /* Compute factor used to convert coalesce '_usecs' parameters to
747 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
748 * count.
749 */
750 factor = nn->me_freq_mhz / 16;
751
752 /* Each pair of (usecs, max_frames) fields specifies that interrupts
753 * should be coalesced until
754 * (usecs > 0 && time_since_first_completion >= usecs) ||
755 * (max_frames > 0 && completed_frames >= max_frames)
756 *
757 * It is illegal to set both usecs and max_frames to zero as this would
758 * cause interrupts to never be generated. To disable coalescing, set
759 * usecs = 0 and max_frames = 1.
760 *
761 * Some implementations ignore the value of max_frames and use the
762 * condition time_since_first_completion >= usecs
763 */
764
765 if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
766 return -EINVAL;
767
768 /* ensure valid configuration */
769 if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames)
770 return -EINVAL;
771
772 if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames)
773 return -EINVAL;
774
775 if (ec->rx_coalesce_usecs * factor >= ((1 << 16) - 1))
776 return -EINVAL;
777
778 if (ec->tx_coalesce_usecs * factor >= ((1 << 16) - 1))
779 return -EINVAL;
780
781 if (ec->rx_max_coalesced_frames >= ((1 << 16) - 1))
782 return -EINVAL;
783
784 if (ec->tx_max_coalesced_frames >= ((1 << 16) - 1))
785 return -EINVAL;
786
787 /* configuration is valid */
788 nn->rx_coalesce_usecs = ec->rx_coalesce_usecs;
789 nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames;
790 nn->tx_coalesce_usecs = ec->tx_coalesce_usecs;
791 nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames;
792
793 /* write configuration to device */
794 nfp_net_coalesce_write_cfg(nn);
795 return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
796}
797
81cc2e43
JK
798static void nfp_net_get_channels(struct net_device *netdev,
799 struct ethtool_channels *channel)
800{
801 struct nfp_net *nn = netdev_priv(netdev);
ecd63a02
JK
802 unsigned int num_tx_rings;
803
79c12a75
JK
804 num_tx_rings = nn->dp.num_tx_rings;
805 if (nn->dp.xdp_prog)
806 num_tx_rings -= nn->dp.num_rx_rings;
81cc2e43
JK
807
808 channel->max_rx = min(nn->max_rx_rings, nn->max_r_vecs);
809 channel->max_tx = min(nn->max_tx_rings, nn->max_r_vecs);
810 channel->max_combined = min(channel->max_rx, channel->max_tx);
811 channel->max_other = NFP_NET_NON_Q_VECTORS;
79c12a75
JK
812 channel->combined_count = min(nn->dp.num_rx_rings, num_tx_rings);
813 channel->rx_count = nn->dp.num_rx_rings - channel->combined_count;
ecd63a02 814 channel->tx_count = num_tx_rings - channel->combined_count;
81cc2e43
JK
815 channel->other_count = NFP_NET_NON_Q_VECTORS;
816}
817
164d1e9e
JK
818static int nfp_net_set_num_rings(struct nfp_net *nn, unsigned int total_rx,
819 unsigned int total_tx)
820{
783496b0 821 struct nfp_net_dp *dp;
164d1e9e 822
783496b0
JK
823 dp = nfp_net_clone_dp(nn);
824 if (!dp)
825 return -ENOMEM;
826
892a7f70
JK
827 dp->num_rx_rings = total_rx;
828 dp->num_tx_rings = total_tx;
829 /* nfp_net_check_config() will catch num_tx_rings > nn->max_tx_rings */
830 if (dp->xdp_prog)
831 dp->num_tx_rings += total_rx;
832
833 return nfp_net_ring_reconfig(nn, dp);
164d1e9e
JK
834}
835
836static int nfp_net_set_channels(struct net_device *netdev,
837 struct ethtool_channels *channel)
838{
839 struct nfp_net *nn = netdev_priv(netdev);
840 unsigned int total_rx, total_tx;
841
842 /* Reject unsupported */
843 if (!channel->combined_count ||
844 channel->other_count != NFP_NET_NON_Q_VECTORS ||
845 (channel->rx_count && channel->tx_count))
846 return -EINVAL;
847
848 total_rx = channel->combined_count + channel->rx_count;
849 total_tx = channel->combined_count + channel->tx_count;
850
851 if (total_rx > min(nn->max_rx_rings, nn->max_r_vecs) ||
852 total_tx > min(nn->max_tx_rings, nn->max_r_vecs))
853 return -EINVAL;
854
855 return nfp_net_set_num_rings(nn, total_rx, total_tx);
856}
857
4c352362
JK
858static const struct ethtool_ops nfp_net_ethtool_ops = {
859 .get_drvinfo = nfp_net_get_drvinfo,
2370def2 860 .get_link = ethtool_op_get_link,
4c352362
JK
861 .get_ringparam = nfp_net_get_ringparam,
862 .set_ringparam = nfp_net_set_ringparam,
863 .get_strings = nfp_net_get_strings,
864 .get_ethtool_stats = nfp_net_get_stats,
865 .get_sset_count = nfp_net_get_sset_count,
866 .get_rxnfc = nfp_net_get_rxnfc,
867 .set_rxnfc = nfp_net_set_rxnfc,
868 .get_rxfh_indir_size = nfp_net_get_rxfh_indir_size,
869 .get_rxfh_key_size = nfp_net_get_rxfh_key_size,
870 .get_rxfh = nfp_net_get_rxfh,
871 .set_rxfh = nfp_net_set_rxfh,
872 .get_regs_len = nfp_net_get_regs_len,
873 .get_regs = nfp_net_get_regs,
af623682
JK
874 .set_dump = nfp_net_set_dump,
875 .get_dump_flag = nfp_net_get_dump_flag,
876 .get_dump_data = nfp_net_get_dump_data,
4c352362
JK
877 .get_coalesce = nfp_net_get_coalesce,
878 .set_coalesce = nfp_net_set_coalesce,
81cc2e43 879 .get_channels = nfp_net_get_channels,
164d1e9e 880 .set_channels = nfp_net_set_channels,
265aeb51 881 .get_link_ksettings = nfp_net_get_link_ksettings,
4c352362
JK
882};
883
884void nfp_net_set_ethtool_ops(struct net_device *netdev)
885{
886 netdev->ethtool_ops = &nfp_net_ethtool_ops;
887}