locking/barriers: Don't use sizeof(void) in lockless_dereference()
[cascardo/linux.git] / drivers / net / ethernet / netronome / nfp / nfp_net_ethtool.c
1 /*
2  * Copyright (C) 2015 Netronome Systems, Inc.
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
43 #include <linux/version.h>
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
51 #include "nfp_net_ctrl.h"
52 #include "nfp_net.h"
53
54 /* Support for stats. Returns netdev, driver, and device stats */
55 enum { NETDEV_ET_STATS, NFP_NET_DRV_ET_STATS, NFP_NET_DEV_ET_STATS };
56 struct _nfp_net_et_stats {
57         char name[ETH_GSTRING_LEN];
58         int type;
59         int sz;
60         int off;
61 };
62
63 #define NN_ET_NETDEV_STAT(m) NETDEV_ET_STATS,                   \
64                 FIELD_SIZEOF(struct net_device_stats, m),       \
65                 offsetof(struct net_device_stats, m)
66 /* For stats in the control BAR (other than Q stats) */
67 #define NN_ET_DEV_STAT(m) NFP_NET_DEV_ET_STATS,                 \
68                 sizeof(u64),                                    \
69                 (m)
70 static const struct _nfp_net_et_stats nfp_net_et_stats[] = {
71         /* netdev stats */
72         {"rx_packets", NN_ET_NETDEV_STAT(rx_packets)},
73         {"tx_packets", NN_ET_NETDEV_STAT(tx_packets)},
74         {"rx_bytes", NN_ET_NETDEV_STAT(rx_bytes)},
75         {"tx_bytes", NN_ET_NETDEV_STAT(tx_bytes)},
76         {"rx_errors", NN_ET_NETDEV_STAT(rx_errors)},
77         {"tx_errors", NN_ET_NETDEV_STAT(tx_errors)},
78         {"rx_dropped", NN_ET_NETDEV_STAT(rx_dropped)},
79         {"tx_dropped", NN_ET_NETDEV_STAT(tx_dropped)},
80         {"multicast", NN_ET_NETDEV_STAT(multicast)},
81         {"collisions", NN_ET_NETDEV_STAT(collisions)},
82         {"rx_over_errors", NN_ET_NETDEV_STAT(rx_over_errors)},
83         {"rx_crc_errors", NN_ET_NETDEV_STAT(rx_crc_errors)},
84         {"rx_frame_errors", NN_ET_NETDEV_STAT(rx_frame_errors)},
85         {"rx_fifo_errors", NN_ET_NETDEV_STAT(rx_fifo_errors)},
86         {"rx_missed_errors", NN_ET_NETDEV_STAT(rx_missed_errors)},
87         {"tx_aborted_errors", NN_ET_NETDEV_STAT(tx_aborted_errors)},
88         {"tx_carrier_errors", NN_ET_NETDEV_STAT(tx_carrier_errors)},
89         {"tx_fifo_errors", NN_ET_NETDEV_STAT(tx_fifo_errors)},
90         /* Stats from the device */
91         {"dev_rx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_DISCARDS)},
92         {"dev_rx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_ERRORS)},
93         {"dev_rx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_OCTETS)},
94         {"dev_rx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_UC_OCTETS)},
95         {"dev_rx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_OCTETS)},
96         {"dev_rx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_OCTETS)},
97         {"dev_rx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_FRAMES)},
98         {"dev_rx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_FRAMES)},
99         {"dev_rx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_FRAMES)},
100
101         {"dev_tx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_DISCARDS)},
102         {"dev_tx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_ERRORS)},
103         {"dev_tx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_OCTETS)},
104         {"dev_tx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_UC_OCTETS)},
105         {"dev_tx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_OCTETS)},
106         {"dev_tx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_OCTETS)},
107         {"dev_tx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_FRAMES)},
108         {"dev_tx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_FRAMES)},
109         {"dev_tx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_FRAMES)},
110 };
111
112 #define NN_ET_GLOBAL_STATS_LEN ARRAY_SIZE(nfp_net_et_stats)
113 #define NN_ET_RVEC_STATS_LEN (nn->num_r_vecs * 3)
114 #define NN_ET_RVEC_GATHER_STATS 7
115 #define NN_ET_QUEUE_STATS_LEN ((nn->num_tx_rings + nn->num_rx_rings) * 2)
116 #define NN_ET_STATS_LEN (NN_ET_GLOBAL_STATS_LEN + NN_ET_RVEC_GATHER_STATS + \
117                          NN_ET_RVEC_STATS_LEN + NN_ET_QUEUE_STATS_LEN)
118
119 static void nfp_net_get_drvinfo(struct net_device *netdev,
120                                 struct ethtool_drvinfo *drvinfo)
121 {
122         struct nfp_net *nn = netdev_priv(netdev);
123
124         strlcpy(drvinfo->driver, nfp_net_driver_name, sizeof(drvinfo->driver));
125         strlcpy(drvinfo->version, nfp_net_driver_version,
126                 sizeof(drvinfo->version));
127
128         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
129                  "%d.%d.%d.%d",
130                  nn->fw_ver.resv, nn->fw_ver.class,
131                  nn->fw_ver.major, nn->fw_ver.minor);
132         strlcpy(drvinfo->bus_info, pci_name(nn->pdev),
133                 sizeof(drvinfo->bus_info));
134
135         drvinfo->n_stats = NN_ET_STATS_LEN;
136         drvinfo->regdump_len = NFP_NET_CFG_BAR_SZ;
137 }
138
139 static void nfp_net_get_ringparam(struct net_device *netdev,
140                                   struct ethtool_ringparam *ring)
141 {
142         struct nfp_net *nn = netdev_priv(netdev);
143
144         ring->rx_max_pending = NFP_NET_MAX_RX_DESCS;
145         ring->tx_max_pending = NFP_NET_MAX_TX_DESCS;
146         ring->rx_pending = nn->rxd_cnt;
147         ring->tx_pending = nn->txd_cnt;
148 }
149
150 static int nfp_net_set_ringparam(struct net_device *netdev,
151                                  struct ethtool_ringparam *ring)
152 {
153         struct nfp_net *nn = netdev_priv(netdev);
154         u32 rxd_cnt, txd_cnt;
155
156         /* We don't have separate queues/rings for small/large frames. */
157         if (ring->rx_mini_pending || ring->rx_jumbo_pending)
158                 return -EINVAL;
159
160         /* Round up to supported values */
161         rxd_cnt = roundup_pow_of_two(ring->rx_pending);
162         txd_cnt = roundup_pow_of_two(ring->tx_pending);
163
164         if (rxd_cnt < NFP_NET_MIN_RX_DESCS || rxd_cnt > NFP_NET_MAX_RX_DESCS ||
165             txd_cnt < NFP_NET_MIN_TX_DESCS || txd_cnt > NFP_NET_MAX_TX_DESCS)
166                 return -EINVAL;
167
168         if (nn->rxd_cnt == rxd_cnt && nn->txd_cnt == txd_cnt)
169                 return 0;
170
171         nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n",
172                nn->rxd_cnt, rxd_cnt, nn->txd_cnt, txd_cnt);
173
174         return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt);
175 }
176
177 static void nfp_net_get_strings(struct net_device *netdev,
178                                 u32 stringset, u8 *data)
179 {
180         struct nfp_net *nn = netdev_priv(netdev);
181         u8 *p = data;
182         int i;
183
184         switch (stringset) {
185         case ETH_SS_STATS:
186                 for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
187                         memcpy(p, nfp_net_et_stats[i].name, ETH_GSTRING_LEN);
188                         p += ETH_GSTRING_LEN;
189                 }
190                 for (i = 0; i < nn->num_r_vecs; i++) {
191                         sprintf(p, "rvec_%u_rx_pkts", i);
192                         p += ETH_GSTRING_LEN;
193                         sprintf(p, "rvec_%u_tx_pkts", i);
194                         p += ETH_GSTRING_LEN;
195                         sprintf(p, "rvec_%u_tx_busy", i);
196                         p += ETH_GSTRING_LEN;
197                 }
198                 strncpy(p, "hw_rx_csum_ok", ETH_GSTRING_LEN);
199                 p += ETH_GSTRING_LEN;
200                 strncpy(p, "hw_rx_csum_inner_ok", ETH_GSTRING_LEN);
201                 p += ETH_GSTRING_LEN;
202                 strncpy(p, "hw_rx_csum_err", ETH_GSTRING_LEN);
203                 p += ETH_GSTRING_LEN;
204                 strncpy(p, "hw_tx_csum", ETH_GSTRING_LEN);
205                 p += ETH_GSTRING_LEN;
206                 strncpy(p, "hw_tx_inner_csum", ETH_GSTRING_LEN);
207                 p += ETH_GSTRING_LEN;
208                 strncpy(p, "tx_gather", ETH_GSTRING_LEN);
209                 p += ETH_GSTRING_LEN;
210                 strncpy(p, "tx_lso", ETH_GSTRING_LEN);
211                 p += ETH_GSTRING_LEN;
212                 for (i = 0; i < nn->num_tx_rings; i++) {
213                         sprintf(p, "txq_%u_pkts", i);
214                         p += ETH_GSTRING_LEN;
215                         sprintf(p, "txq_%u_bytes", i);
216                         p += ETH_GSTRING_LEN;
217                 }
218                 for (i = 0; i < nn->num_rx_rings; i++) {
219                         sprintf(p, "rxq_%u_pkts", i);
220                         p += ETH_GSTRING_LEN;
221                         sprintf(p, "rxq_%u_bytes", i);
222                         p += ETH_GSTRING_LEN;
223                 }
224                 break;
225         }
226 }
227
228 static void nfp_net_get_stats(struct net_device *netdev,
229                               struct ethtool_stats *stats, u64 *data)
230 {
231         u64 gathered_stats[NN_ET_RVEC_GATHER_STATS] = {};
232         struct nfp_net *nn = netdev_priv(netdev);
233         struct rtnl_link_stats64 *netdev_stats;
234         struct rtnl_link_stats64 temp = {};
235         u64 tmp[NN_ET_RVEC_GATHER_STATS];
236         u8 __iomem *io_p;
237         int i, j, k;
238         u8 *p;
239
240         netdev_stats = dev_get_stats(netdev, &temp);
241
242         for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
243                 switch (nfp_net_et_stats[i].type) {
244                 case NETDEV_ET_STATS:
245                         p = (char *)netdev_stats + nfp_net_et_stats[i].off;
246                         data[i] = nfp_net_et_stats[i].sz == sizeof(u64) ?
247                                 *(u64 *)p : *(u32 *)p;
248                         break;
249
250                 case NFP_NET_DEV_ET_STATS:
251                         io_p = nn->ctrl_bar + nfp_net_et_stats[i].off;
252                         data[i] = readq(io_p);
253                         break;
254                 }
255         }
256         for (j = 0; j < nn->num_r_vecs; j++) {
257                 unsigned int start;
258
259                 do {
260                         start = u64_stats_fetch_begin(&nn->r_vecs[j].rx_sync);
261                         data[i++] = nn->r_vecs[j].rx_pkts;
262                         tmp[0] = nn->r_vecs[j].hw_csum_rx_ok;
263                         tmp[1] = nn->r_vecs[j].hw_csum_rx_inner_ok;
264                         tmp[2] = nn->r_vecs[j].hw_csum_rx_error;
265                 } while (u64_stats_fetch_retry(&nn->r_vecs[j].rx_sync, start));
266
267                 do {
268                         start = u64_stats_fetch_begin(&nn->r_vecs[j].tx_sync);
269                         data[i++] = nn->r_vecs[j].tx_pkts;
270                         data[i++] = nn->r_vecs[j].tx_busy;
271                         tmp[3] = nn->r_vecs[j].hw_csum_tx;
272                         tmp[4] = nn->r_vecs[j].hw_csum_tx_inner;
273                         tmp[5] = nn->r_vecs[j].tx_gather;
274                         tmp[6] = nn->r_vecs[j].tx_lso;
275                 } while (u64_stats_fetch_retry(&nn->r_vecs[j].tx_sync, start));
276
277                 for (k = 0; k < NN_ET_RVEC_GATHER_STATS; k++)
278                         gathered_stats[k] += tmp[k];
279         }
280         for (j = 0; j < NN_ET_RVEC_GATHER_STATS; j++)
281                 data[i++] = gathered_stats[j];
282         for (j = 0; j < nn->num_tx_rings; j++) {
283                 io_p = nn->ctrl_bar + NFP_NET_CFG_TXR_STATS(j);
284                 data[i++] = readq(io_p);
285                 io_p = nn->ctrl_bar + NFP_NET_CFG_TXR_STATS(j) + 8;
286                 data[i++] = readq(io_p);
287         }
288         for (j = 0; j < nn->num_rx_rings; j++) {
289                 io_p = nn->ctrl_bar + NFP_NET_CFG_RXR_STATS(j);
290                 data[i++] = readq(io_p);
291                 io_p = nn->ctrl_bar + NFP_NET_CFG_RXR_STATS(j) + 8;
292                 data[i++] = readq(io_p);
293         }
294 }
295
296 static int nfp_net_get_sset_count(struct net_device *netdev, int sset)
297 {
298         struct nfp_net *nn = netdev_priv(netdev);
299
300         switch (sset) {
301         case ETH_SS_STATS:
302                 return NN_ET_STATS_LEN;
303         default:
304                 return -EOPNOTSUPP;
305         }
306 }
307
308 /* RX network flow classification (RSS, filters, etc)
309  */
310 static u32 ethtool_flow_to_nfp_flag(u32 flow_type)
311 {
312         static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = {
313                 [TCP_V4_FLOW]   = NFP_NET_CFG_RSS_IPV4_TCP,
314                 [TCP_V6_FLOW]   = NFP_NET_CFG_RSS_IPV6_TCP,
315                 [UDP_V4_FLOW]   = NFP_NET_CFG_RSS_IPV4_UDP,
316                 [UDP_V6_FLOW]   = NFP_NET_CFG_RSS_IPV6_UDP,
317                 [IPV4_FLOW]     = NFP_NET_CFG_RSS_IPV4,
318                 [IPV6_FLOW]     = NFP_NET_CFG_RSS_IPV6,
319         };
320
321         if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp))
322                 return 0;
323
324         return xlate_ethtool_to_nfp[flow_type];
325 }
326
327 static int nfp_net_get_rss_hash_opts(struct nfp_net *nn,
328                                      struct ethtool_rxnfc *cmd)
329 {
330         u32 nfp_rss_flag;
331
332         cmd->data = 0;
333
334         if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
335                 return -EOPNOTSUPP;
336
337         nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type);
338         if (!nfp_rss_flag)
339                 return -EINVAL;
340
341         cmd->data |= RXH_IP_SRC | RXH_IP_DST;
342         if (nn->rss_cfg & nfp_rss_flag)
343                 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
344
345         return 0;
346 }
347
348 static int nfp_net_get_rxnfc(struct net_device *netdev,
349                              struct ethtool_rxnfc *cmd, u32 *rule_locs)
350 {
351         struct nfp_net *nn = netdev_priv(netdev);
352
353         switch (cmd->cmd) {
354         case ETHTOOL_GRXRINGS:
355                 cmd->data = nn->num_rx_rings;
356                 return 0;
357         case ETHTOOL_GRXFH:
358                 return nfp_net_get_rss_hash_opts(nn, cmd);
359         default:
360                 return -EOPNOTSUPP;
361         }
362 }
363
364 static int nfp_net_set_rss_hash_opt(struct nfp_net *nn,
365                                     struct ethtool_rxnfc *nfc)
366 {
367         u32 new_rss_cfg = nn->rss_cfg;
368         u32 nfp_rss_flag;
369         int err;
370
371         if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
372                 return -EOPNOTSUPP;
373
374         /* RSS only supports IP SA/DA and L4 src/dst ports  */
375         if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
376                           RXH_L4_B_0_1 | RXH_L4_B_2_3))
377                 return -EINVAL;
378
379         /* We need at least the IP SA/DA fields for hashing */
380         if (!(nfc->data & RXH_IP_SRC) ||
381             !(nfc->data & RXH_IP_DST))
382                 return -EINVAL;
383
384         nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type);
385         if (!nfp_rss_flag)
386                 return -EINVAL;
387
388         switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
389         case 0:
390                 new_rss_cfg &= ~nfp_rss_flag;
391                 break;
392         case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
393                 new_rss_cfg |= nfp_rss_flag;
394                 break;
395         default:
396                 return -EINVAL;
397         }
398
399         new_rss_cfg |= NFP_NET_CFG_RSS_TOEPLITZ;
400         new_rss_cfg |= NFP_NET_CFG_RSS_MASK;
401
402         if (new_rss_cfg == nn->rss_cfg)
403                 return 0;
404
405         writel(new_rss_cfg, nn->ctrl_bar + NFP_NET_CFG_RSS_CTRL);
406         err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
407         if (err)
408                 return err;
409
410         nn->rss_cfg = new_rss_cfg;
411
412         nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg);
413         return 0;
414 }
415
416 static int nfp_net_set_rxnfc(struct net_device *netdev,
417                              struct ethtool_rxnfc *cmd)
418 {
419         struct nfp_net *nn = netdev_priv(netdev);
420
421         switch (cmd->cmd) {
422         case ETHTOOL_SRXFH:
423                 return nfp_net_set_rss_hash_opt(nn, cmd);
424         default:
425                 return -EOPNOTSUPP;
426         }
427 }
428
429 static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev)
430 {
431         struct nfp_net *nn = netdev_priv(netdev);
432
433         if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
434                 return 0;
435
436         return ARRAY_SIZE(nn->rss_itbl);
437 }
438
439 static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev)
440 {
441         return NFP_NET_CFG_RSS_KEY_SZ;
442 }
443
444 static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
445                             u8 *hfunc)
446 {
447         struct nfp_net *nn = netdev_priv(netdev);
448         int i;
449
450         if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
451                 return -EOPNOTSUPP;
452
453         if (indir)
454                 for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
455                         indir[i] = nn->rss_itbl[i];
456         if (key)
457                 memcpy(key, nn->rss_key, NFP_NET_CFG_RSS_KEY_SZ);
458         if (hfunc)
459                 *hfunc = ETH_RSS_HASH_TOP;
460
461         return 0;
462 }
463
464 static int nfp_net_set_rxfh(struct net_device *netdev,
465                             const u32 *indir, const u8 *key,
466                             const u8 hfunc)
467 {
468         struct nfp_net *nn = netdev_priv(netdev);
469         int i;
470
471         if (!(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
472             !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == ETH_RSS_HASH_TOP))
473                 return -EOPNOTSUPP;
474
475         if (!key && !indir)
476                 return 0;
477
478         if (key) {
479                 memcpy(nn->rss_key, key, NFP_NET_CFG_RSS_KEY_SZ);
480                 nfp_net_rss_write_key(nn);
481         }
482         if (indir) {
483                 for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
484                         nn->rss_itbl[i] = indir[i];
485
486                 nfp_net_rss_write_itbl(nn);
487         }
488
489         return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
490 }
491
492 /* Dump BAR registers
493  */
494 static int nfp_net_get_regs_len(struct net_device *netdev)
495 {
496         return NFP_NET_CFG_BAR_SZ;
497 }
498
499 static void nfp_net_get_regs(struct net_device *netdev,
500                              struct ethtool_regs *regs, void *p)
501 {
502         struct nfp_net *nn = netdev_priv(netdev);
503         u32 *regs_buf = p;
504         int i;
505
506         regs->version = nn_readl(nn, NFP_NET_CFG_VERSION);
507
508         for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++)
509                 regs_buf[i] = readl(nn->ctrl_bar + (i * sizeof(u32)));
510 }
511
512 static int nfp_net_get_coalesce(struct net_device *netdev,
513                                 struct ethtool_coalesce *ec)
514 {
515         struct nfp_net *nn = netdev_priv(netdev);
516
517         if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
518                 return -EINVAL;
519
520         ec->rx_coalesce_usecs       = nn->rx_coalesce_usecs;
521         ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames;
522         ec->tx_coalesce_usecs       = nn->tx_coalesce_usecs;
523         ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames;
524
525         return 0;
526 }
527
528 static int nfp_net_set_coalesce(struct net_device *netdev,
529                                 struct ethtool_coalesce *ec)
530 {
531         struct nfp_net *nn = netdev_priv(netdev);
532         unsigned int factor;
533
534         if (ec->rx_coalesce_usecs_irq ||
535             ec->rx_max_coalesced_frames_irq ||
536             ec->tx_coalesce_usecs_irq ||
537             ec->tx_max_coalesced_frames_irq ||
538             ec->stats_block_coalesce_usecs ||
539             ec->use_adaptive_rx_coalesce ||
540             ec->use_adaptive_tx_coalesce ||
541             ec->pkt_rate_low ||
542             ec->rx_coalesce_usecs_low ||
543             ec->rx_max_coalesced_frames_low ||
544             ec->tx_coalesce_usecs_low ||
545             ec->tx_max_coalesced_frames_low ||
546             ec->pkt_rate_high ||
547             ec->rx_coalesce_usecs_high ||
548             ec->rx_max_coalesced_frames_high ||
549             ec->tx_coalesce_usecs_high ||
550             ec->tx_max_coalesced_frames_high ||
551             ec->rate_sample_interval)
552                 return -ENOTSUPP;
553
554         /* Compute factor used to convert coalesce '_usecs' parameters to
555          * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
556          * count.
557          */
558         factor = nn->me_freq_mhz / 16;
559
560         /* Each pair of (usecs, max_frames) fields specifies that interrupts
561          * should be coalesced until
562          *      (usecs > 0 && time_since_first_completion >= usecs) ||
563          *      (max_frames > 0 && completed_frames >= max_frames)
564          *
565          * It is illegal to set both usecs and max_frames to zero as this would
566          * cause interrupts to never be generated.  To disable coalescing, set
567          * usecs = 0 and max_frames = 1.
568          *
569          * Some implementations ignore the value of max_frames and use the
570          * condition time_since_first_completion >= usecs
571          */
572
573         if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
574                 return -EINVAL;
575
576         /* ensure valid configuration */
577         if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames)
578                 return -EINVAL;
579
580         if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames)
581                 return -EINVAL;
582
583         if (ec->rx_coalesce_usecs * factor >= ((1 << 16) - 1))
584                 return -EINVAL;
585
586         if (ec->tx_coalesce_usecs * factor >= ((1 << 16) - 1))
587                 return -EINVAL;
588
589         if (ec->rx_max_coalesced_frames >= ((1 << 16) - 1))
590                 return -EINVAL;
591
592         if (ec->tx_max_coalesced_frames >= ((1 << 16) - 1))
593                 return -EINVAL;
594
595         /* configuration is valid */
596         nn->rx_coalesce_usecs      = ec->rx_coalesce_usecs;
597         nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames;
598         nn->tx_coalesce_usecs      = ec->tx_coalesce_usecs;
599         nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames;
600
601         /* write configuration to device */
602         nfp_net_coalesce_write_cfg(nn);
603         return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
604 }
605
606 static const struct ethtool_ops nfp_net_ethtool_ops = {
607         .get_drvinfo            = nfp_net_get_drvinfo,
608         .get_link               = ethtool_op_get_link,
609         .get_ringparam          = nfp_net_get_ringparam,
610         .set_ringparam          = nfp_net_set_ringparam,
611         .get_strings            = nfp_net_get_strings,
612         .get_ethtool_stats      = nfp_net_get_stats,
613         .get_sset_count         = nfp_net_get_sset_count,
614         .get_rxnfc              = nfp_net_get_rxnfc,
615         .set_rxnfc              = nfp_net_set_rxnfc,
616         .get_rxfh_indir_size    = nfp_net_get_rxfh_indir_size,
617         .get_rxfh_key_size      = nfp_net_get_rxfh_key_size,
618         .get_rxfh               = nfp_net_get_rxfh,
619         .set_rxfh               = nfp_net_set_rxfh,
620         .get_regs_len           = nfp_net_get_regs_len,
621         .get_regs               = nfp_net_get_regs,
622         .get_coalesce           = nfp_net_get_coalesce,
623         .set_coalesce           = nfp_net_set_coalesce,
624 };
625
626 void nfp_net_set_ethtool_ops(struct net_device *netdev)
627 {
628         netdev->ethtool_ops = &nfp_net_ethtool_ops;
629 }