net/mlx5e: Move mlx5e_priv_flags into en_ethtool.c
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_ethtool.c
1 /*
2  * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include "en.h"
34 #include "en/port.h"
35 #include "lib/clock.h"
36
37 void mlx5e_ethtool_get_drvinfo(struct mlx5e_priv *priv,
38                                struct ethtool_drvinfo *drvinfo)
39 {
40         struct mlx5_core_dev *mdev = priv->mdev;
41
42         strlcpy(drvinfo->driver, DRIVER_NAME, sizeof(drvinfo->driver));
43         strlcpy(drvinfo->version, DRIVER_VERSION,
44                 sizeof(drvinfo->version));
45         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
46                  "%d.%d.%04d (%.16s)",
47                  fw_rev_maj(mdev), fw_rev_min(mdev), fw_rev_sub(mdev),
48                  mdev->board_id);
49         strlcpy(drvinfo->bus_info, pci_name(mdev->pdev),
50                 sizeof(drvinfo->bus_info));
51 }
52
53 static void mlx5e_get_drvinfo(struct net_device *dev,
54                               struct ethtool_drvinfo *drvinfo)
55 {
56         struct mlx5e_priv *priv = netdev_priv(dev);
57
58         mlx5e_ethtool_get_drvinfo(priv, drvinfo);
59 }
60
61 struct ptys2ethtool_config {
62         __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
63         __ETHTOOL_DECLARE_LINK_MODE_MASK(advertised);
64 };
65
66 static struct ptys2ethtool_config ptys2ethtool_table[MLX5E_LINK_MODES_NUMBER];
67
68 #define MLX5_BUILD_PTYS2ETHTOOL_CONFIG(reg_, ...)                       \
69         ({                                                              \
70                 struct ptys2ethtool_config *cfg;                        \
71                 const unsigned int modes[] = { __VA_ARGS__ };           \
72                 unsigned int i;                                         \
73                 cfg = &ptys2ethtool_table[reg_];                        \
74                 bitmap_zero(cfg->supported,                             \
75                             __ETHTOOL_LINK_MODE_MASK_NBITS);            \
76                 bitmap_zero(cfg->advertised,                            \
77                             __ETHTOOL_LINK_MODE_MASK_NBITS);            \
78                 for (i = 0 ; i < ARRAY_SIZE(modes) ; ++i) {             \
79                         __set_bit(modes[i], cfg->supported);            \
80                         __set_bit(modes[i], cfg->advertised);           \
81                 }                                                       \
82         })
83
84 void mlx5e_build_ptys2ethtool_map(void)
85 {
86         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_1000BASE_CX_SGMII,
87                                        ETHTOOL_LINK_MODE_1000baseKX_Full_BIT);
88         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_1000BASE_KX,
89                                        ETHTOOL_LINK_MODE_1000baseKX_Full_BIT);
90         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_10GBASE_CX4,
91                                        ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT);
92         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_10GBASE_KX4,
93                                        ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT);
94         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_10GBASE_KR,
95                                        ETHTOOL_LINK_MODE_10000baseKR_Full_BIT);
96         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_20GBASE_KR2,
97                                        ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT);
98         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_40GBASE_CR4,
99                                        ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT);
100         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_40GBASE_KR4,
101                                        ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT);
102         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_56GBASE_R4,
103                                        ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT);
104         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_10GBASE_CR,
105                                        ETHTOOL_LINK_MODE_10000baseKR_Full_BIT);
106         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_10GBASE_SR,
107                                        ETHTOOL_LINK_MODE_10000baseKR_Full_BIT);
108         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_10GBASE_ER,
109                                        ETHTOOL_LINK_MODE_10000baseKR_Full_BIT);
110         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_40GBASE_SR4,
111                                        ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT);
112         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_40GBASE_LR4,
113                                        ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT);
114         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_50GBASE_SR2,
115                                        ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT);
116         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_100GBASE_CR4,
117                                        ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT);
118         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_100GBASE_SR4,
119                                        ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT);
120         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_100GBASE_KR4,
121                                        ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT);
122         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_100GBASE_LR4,
123                                        ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT);
124         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_10GBASE_T,
125                                        ETHTOOL_LINK_MODE_10000baseT_Full_BIT);
126         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_25GBASE_CR,
127                                        ETHTOOL_LINK_MODE_25000baseCR_Full_BIT);
128         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_25GBASE_KR,
129                                        ETHTOOL_LINK_MODE_25000baseKR_Full_BIT);
130         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_25GBASE_SR,
131                                        ETHTOOL_LINK_MODE_25000baseSR_Full_BIT);
132         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_50GBASE_CR2,
133                                        ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT);
134         MLX5_BUILD_PTYS2ETHTOOL_CONFIG(MLX5E_50GBASE_KR2,
135                                        ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT);
136 }
137
138 static const char mlx5e_priv_flags[][ETH_GSTRING_LEN] = {
139         "rx_cqe_moder",
140         "tx_cqe_moder",
141         "rx_cqe_compress",
142         "rx_striding_rq",
143 };
144
145 int mlx5e_ethtool_get_sset_count(struct mlx5e_priv *priv, int sset)
146 {
147         int i, num_stats = 0;
148
149         switch (sset) {
150         case ETH_SS_STATS:
151                 for (i = 0; i < mlx5e_num_stats_grps; i++)
152                         num_stats += mlx5e_stats_grps[i].get_num_stats(priv);
153                 return num_stats;
154         case ETH_SS_PRIV_FLAGS:
155                 return ARRAY_SIZE(mlx5e_priv_flags);
156         case ETH_SS_TEST:
157                 return mlx5e_self_test_num(priv);
158         /* fallthrough */
159         default:
160                 return -EOPNOTSUPP;
161         }
162 }
163
164 static int mlx5e_get_sset_count(struct net_device *dev, int sset)
165 {
166         struct mlx5e_priv *priv = netdev_priv(dev);
167
168         return mlx5e_ethtool_get_sset_count(priv, sset);
169 }
170
171 static void mlx5e_fill_stats_strings(struct mlx5e_priv *priv, u8 *data)
172 {
173         int i, idx = 0;
174
175         for (i = 0; i < mlx5e_num_stats_grps; i++)
176                 idx = mlx5e_stats_grps[i].fill_strings(priv, data, idx);
177 }
178
179 void mlx5e_ethtool_get_strings(struct mlx5e_priv *priv, u32 stringset, u8 *data)
180 {
181         int i;
182
183         switch (stringset) {
184         case ETH_SS_PRIV_FLAGS:
185                 for (i = 0; i < ARRAY_SIZE(mlx5e_priv_flags); i++)
186                         strcpy(data + i * ETH_GSTRING_LEN, mlx5e_priv_flags[i]);
187                 break;
188
189         case ETH_SS_TEST:
190                 for (i = 0; i < mlx5e_self_test_num(priv); i++)
191                         strcpy(data + i * ETH_GSTRING_LEN,
192                                mlx5e_self_tests[i]);
193                 break;
194
195         case ETH_SS_STATS:
196                 mlx5e_fill_stats_strings(priv, data);
197                 break;
198         }
199 }
200
201 static void mlx5e_get_strings(struct net_device *dev, u32 stringset, u8 *data)
202 {
203         struct mlx5e_priv *priv = netdev_priv(dev);
204
205         mlx5e_ethtool_get_strings(priv, stringset, data);
206 }
207
208 void mlx5e_ethtool_get_ethtool_stats(struct mlx5e_priv *priv,
209                                      struct ethtool_stats *stats, u64 *data)
210 {
211         int i, idx = 0;
212
213         mutex_lock(&priv->state_lock);
214         mlx5e_update_stats(priv);
215         mutex_unlock(&priv->state_lock);
216
217         for (i = 0; i < mlx5e_num_stats_grps; i++)
218                 idx = mlx5e_stats_grps[i].fill_stats(priv, data, idx);
219 }
220
221 static void mlx5e_get_ethtool_stats(struct net_device *dev,
222                                     struct ethtool_stats *stats,
223                                     u64 *data)
224 {
225         struct mlx5e_priv *priv = netdev_priv(dev);
226
227         mlx5e_ethtool_get_ethtool_stats(priv, stats, data);
228 }
229
230 void mlx5e_ethtool_get_ringparam(struct mlx5e_priv *priv,
231                                  struct ethtool_ringparam *param)
232 {
233         param->rx_max_pending = 1 << MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE;
234         param->tx_max_pending = 1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE;
235         param->rx_pending     = 1 << priv->channels.params.log_rq_mtu_frames;
236         param->tx_pending     = 1 << priv->channels.params.log_sq_size;
237 }
238
239 static void mlx5e_get_ringparam(struct net_device *dev,
240                                 struct ethtool_ringparam *param)
241 {
242         struct mlx5e_priv *priv = netdev_priv(dev);
243
244         mlx5e_ethtool_get_ringparam(priv, param);
245 }
246
247 int mlx5e_ethtool_set_ringparam(struct mlx5e_priv *priv,
248                                 struct ethtool_ringparam *param)
249 {
250         struct mlx5e_channels new_channels = {};
251         u8 log_rq_size;
252         u8 log_sq_size;
253         int err = 0;
254
255         if (param->rx_jumbo_pending) {
256                 netdev_info(priv->netdev, "%s: rx_jumbo_pending not supported\n",
257                             __func__);
258                 return -EINVAL;
259         }
260         if (param->rx_mini_pending) {
261                 netdev_info(priv->netdev, "%s: rx_mini_pending not supported\n",
262                             __func__);
263                 return -EINVAL;
264         }
265
266         if (param->rx_pending < (1 << MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE)) {
267                 netdev_info(priv->netdev, "%s: rx_pending (%d) < min (%d)\n",
268                             __func__, param->rx_pending,
269                             1 << MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE);
270                 return -EINVAL;
271         }
272
273         if (param->tx_pending < (1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)) {
274                 netdev_info(priv->netdev, "%s: tx_pending (%d) < min (%d)\n",
275                             __func__, param->tx_pending,
276                             1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE);
277                 return -EINVAL;
278         }
279
280         log_rq_size = order_base_2(param->rx_pending);
281         log_sq_size = order_base_2(param->tx_pending);
282
283         if (log_rq_size == priv->channels.params.log_rq_mtu_frames &&
284             log_sq_size == priv->channels.params.log_sq_size)
285                 return 0;
286
287         mutex_lock(&priv->state_lock);
288
289         new_channels.params = priv->channels.params;
290         new_channels.params.log_rq_mtu_frames = log_rq_size;
291         new_channels.params.log_sq_size = log_sq_size;
292
293         if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
294                 priv->channels.params = new_channels.params;
295                 goto unlock;
296         }
297
298         err = mlx5e_open_channels(priv, &new_channels);
299         if (err)
300                 goto unlock;
301
302         mlx5e_switch_priv_channels(priv, &new_channels, NULL);
303
304 unlock:
305         mutex_unlock(&priv->state_lock);
306
307         return err;
308 }
309
310 static int mlx5e_set_ringparam(struct net_device *dev,
311                                struct ethtool_ringparam *param)
312 {
313         struct mlx5e_priv *priv = netdev_priv(dev);
314
315         return mlx5e_ethtool_set_ringparam(priv, param);
316 }
317
318 void mlx5e_ethtool_get_channels(struct mlx5e_priv *priv,
319                                 struct ethtool_channels *ch)
320 {
321         ch->max_combined   = priv->profile->max_nch(priv->mdev);
322         ch->combined_count = priv->channels.params.num_channels;
323 }
324
325 static void mlx5e_get_channels(struct net_device *dev,
326                                struct ethtool_channels *ch)
327 {
328         struct mlx5e_priv *priv = netdev_priv(dev);
329
330         mlx5e_ethtool_get_channels(priv, ch);
331 }
332
333 int mlx5e_ethtool_set_channels(struct mlx5e_priv *priv,
334                                struct ethtool_channels *ch)
335 {
336         unsigned int count = ch->combined_count;
337         struct mlx5e_channels new_channels = {};
338         bool arfs_enabled;
339         int err = 0;
340
341         if (!count) {
342                 netdev_info(priv->netdev, "%s: combined_count=0 not supported\n",
343                             __func__);
344                 return -EINVAL;
345         }
346
347         if (priv->channels.params.num_channels == count)
348                 return 0;
349
350         mutex_lock(&priv->state_lock);
351
352         new_channels.params = priv->channels.params;
353         new_channels.params.num_channels = count;
354         if (!netif_is_rxfh_configured(priv->netdev))
355                 mlx5e_build_default_indir_rqt(new_channels.params.indirection_rqt,
356                                               MLX5E_INDIR_RQT_SIZE, count);
357
358         if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
359                 priv->channels.params = new_channels.params;
360                 goto out;
361         }
362
363         /* Create fresh channels with new parameters */
364         err = mlx5e_open_channels(priv, &new_channels);
365         if (err)
366                 goto out;
367
368         arfs_enabled = priv->netdev->features & NETIF_F_NTUPLE;
369         if (arfs_enabled)
370                 mlx5e_arfs_disable(priv);
371
372         /* Switch to new channels, set new parameters and close old ones */
373         mlx5e_switch_priv_channels(priv, &new_channels, NULL);
374
375         if (arfs_enabled) {
376                 err = mlx5e_arfs_enable(priv);
377                 if (err)
378                         netdev_err(priv->netdev, "%s: mlx5e_arfs_enable failed: %d\n",
379                                    __func__, err);
380         }
381
382 out:
383         mutex_unlock(&priv->state_lock);
384
385         return err;
386 }
387
388 static int mlx5e_set_channels(struct net_device *dev,
389                               struct ethtool_channels *ch)
390 {
391         struct mlx5e_priv *priv = netdev_priv(dev);
392
393         return mlx5e_ethtool_set_channels(priv, ch);
394 }
395
396 int mlx5e_ethtool_get_coalesce(struct mlx5e_priv *priv,
397                                struct ethtool_coalesce *coal)
398 {
399         struct net_dim_cq_moder *rx_moder, *tx_moder;
400
401         if (!MLX5_CAP_GEN(priv->mdev, cq_moderation))
402                 return -EOPNOTSUPP;
403
404         rx_moder = &priv->channels.params.rx_cq_moderation;
405         coal->rx_coalesce_usecs         = rx_moder->usec;
406         coal->rx_max_coalesced_frames   = rx_moder->pkts;
407         coal->use_adaptive_rx_coalesce  = priv->channels.params.rx_dim_enabled;
408
409         tx_moder = &priv->channels.params.tx_cq_moderation;
410         coal->tx_coalesce_usecs         = tx_moder->usec;
411         coal->tx_max_coalesced_frames   = tx_moder->pkts;
412         coal->use_adaptive_tx_coalesce  = priv->channels.params.tx_dim_enabled;
413
414         return 0;
415 }
416
417 static int mlx5e_get_coalesce(struct net_device *netdev,
418                               struct ethtool_coalesce *coal)
419 {
420         struct mlx5e_priv *priv = netdev_priv(netdev);
421
422         return mlx5e_ethtool_get_coalesce(priv, coal);
423 }
424
425 #define MLX5E_MAX_COAL_TIME             MLX5_MAX_CQ_PERIOD
426 #define MLX5E_MAX_COAL_FRAMES           MLX5_MAX_CQ_COUNT
427
428 static void
429 mlx5e_set_priv_channels_coalesce(struct mlx5e_priv *priv, struct ethtool_coalesce *coal)
430 {
431         struct mlx5_core_dev *mdev = priv->mdev;
432         int tc;
433         int i;
434
435         for (i = 0; i < priv->channels.num; ++i) {
436                 struct mlx5e_channel *c = priv->channels.c[i];
437
438                 for (tc = 0; tc < c->num_tc; tc++) {
439                         mlx5_core_modify_cq_moderation(mdev,
440                                                 &c->sq[tc].cq.mcq,
441                                                 coal->tx_coalesce_usecs,
442                                                 coal->tx_max_coalesced_frames);
443                 }
444
445                 mlx5_core_modify_cq_moderation(mdev, &c->rq.cq.mcq,
446                                                coal->rx_coalesce_usecs,
447                                                coal->rx_max_coalesced_frames);
448         }
449 }
450
451 int mlx5e_ethtool_set_coalesce(struct mlx5e_priv *priv,
452                                struct ethtool_coalesce *coal)
453 {
454         struct net_dim_cq_moder *rx_moder, *tx_moder;
455         struct mlx5_core_dev *mdev = priv->mdev;
456         struct mlx5e_channels new_channels = {};
457         int err = 0;
458         bool reset;
459
460         if (!MLX5_CAP_GEN(mdev, cq_moderation))
461                 return -EOPNOTSUPP;
462
463         if (coal->tx_coalesce_usecs > MLX5E_MAX_COAL_TIME ||
464             coal->rx_coalesce_usecs > MLX5E_MAX_COAL_TIME) {
465                 netdev_info(priv->netdev, "%s: maximum coalesce time supported is %lu usecs\n",
466                             __func__, MLX5E_MAX_COAL_TIME);
467                 return -ERANGE;
468         }
469
470         if (coal->tx_max_coalesced_frames > MLX5E_MAX_COAL_FRAMES ||
471             coal->rx_max_coalesced_frames > MLX5E_MAX_COAL_FRAMES) {
472                 netdev_info(priv->netdev, "%s: maximum coalesced frames supported is %lu\n",
473                             __func__, MLX5E_MAX_COAL_FRAMES);
474                 return -ERANGE;
475         }
476
477         mutex_lock(&priv->state_lock);
478         new_channels.params = priv->channels.params;
479
480         rx_moder          = &new_channels.params.rx_cq_moderation;
481         rx_moder->usec    = coal->rx_coalesce_usecs;
482         rx_moder->pkts    = coal->rx_max_coalesced_frames;
483         new_channels.params.rx_dim_enabled = !!coal->use_adaptive_rx_coalesce;
484
485         tx_moder          = &new_channels.params.tx_cq_moderation;
486         tx_moder->usec    = coal->tx_coalesce_usecs;
487         tx_moder->pkts    = coal->tx_max_coalesced_frames;
488         new_channels.params.tx_dim_enabled = !!coal->use_adaptive_tx_coalesce;
489
490         if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
491                 priv->channels.params = new_channels.params;
492                 goto out;
493         }
494         /* we are opened */
495
496         reset = (!!coal->use_adaptive_rx_coalesce != priv->channels.params.rx_dim_enabled) ||
497                 (!!coal->use_adaptive_tx_coalesce != priv->channels.params.tx_dim_enabled);
498
499         if (!reset) {
500                 mlx5e_set_priv_channels_coalesce(priv, coal);
501                 priv->channels.params = new_channels.params;
502                 goto out;
503         }
504
505         /* open fresh channels with new coal parameters */
506         err = mlx5e_open_channels(priv, &new_channels);
507         if (err)
508                 goto out;
509
510         mlx5e_switch_priv_channels(priv, &new_channels, NULL);
511
512 out:
513         mutex_unlock(&priv->state_lock);
514         return err;
515 }
516
517 static int mlx5e_set_coalesce(struct net_device *netdev,
518                               struct ethtool_coalesce *coal)
519 {
520         struct mlx5e_priv *priv    = netdev_priv(netdev);
521
522         return mlx5e_ethtool_set_coalesce(priv, coal);
523 }
524
525 static void ptys2ethtool_supported_link(unsigned long *supported_modes,
526                                         u32 eth_proto_cap)
527 {
528         unsigned long proto_cap = eth_proto_cap;
529         int proto;
530
531         for_each_set_bit(proto, &proto_cap, MLX5E_LINK_MODES_NUMBER)
532                 bitmap_or(supported_modes, supported_modes,
533                           ptys2ethtool_table[proto].supported,
534                           __ETHTOOL_LINK_MODE_MASK_NBITS);
535 }
536
537 static void ptys2ethtool_adver_link(unsigned long *advertising_modes,
538                                     u32 eth_proto_cap)
539 {
540         unsigned long proto_cap = eth_proto_cap;
541         int proto;
542
543         for_each_set_bit(proto, &proto_cap, MLX5E_LINK_MODES_NUMBER)
544                 bitmap_or(advertising_modes, advertising_modes,
545                           ptys2ethtool_table[proto].advertised,
546                           __ETHTOOL_LINK_MODE_MASK_NBITS);
547 }
548
549 static void ptys2ethtool_supported_advertised_port(struct ethtool_link_ksettings *link_ksettings,
550                                                    u32 eth_proto_cap,
551                                                    u8 connector_type)
552 {
553         if (!connector_type || connector_type >= MLX5E_CONNECTOR_TYPE_NUMBER) {
554                 if (eth_proto_cap & (MLX5E_PROT_MASK(MLX5E_10GBASE_CR)
555                                    | MLX5E_PROT_MASK(MLX5E_10GBASE_SR)
556                                    | MLX5E_PROT_MASK(MLX5E_40GBASE_CR4)
557                                    | MLX5E_PROT_MASK(MLX5E_40GBASE_SR4)
558                                    | MLX5E_PROT_MASK(MLX5E_100GBASE_SR4)
559                                    | MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII))) {
560                         ethtool_link_ksettings_add_link_mode(link_ksettings,
561                                                              supported,
562                                                              FIBRE);
563                         ethtool_link_ksettings_add_link_mode(link_ksettings,
564                                                              advertising,
565                                                              FIBRE);
566                 }
567
568                 if (eth_proto_cap & (MLX5E_PROT_MASK(MLX5E_100GBASE_KR4)
569                                    | MLX5E_PROT_MASK(MLX5E_40GBASE_KR4)
570                                    | MLX5E_PROT_MASK(MLX5E_10GBASE_KR)
571                                    | MLX5E_PROT_MASK(MLX5E_10GBASE_KX4)
572                                    | MLX5E_PROT_MASK(MLX5E_1000BASE_KX))) {
573                         ethtool_link_ksettings_add_link_mode(link_ksettings,
574                                                              supported,
575                                                              Backplane);
576                         ethtool_link_ksettings_add_link_mode(link_ksettings,
577                                                              advertising,
578                                                              Backplane);
579                 }
580                 return;
581         }
582
583         switch (connector_type) {
584         case MLX5E_PORT_TP:
585                 ethtool_link_ksettings_add_link_mode(link_ksettings,
586                                                      supported, TP);
587                 ethtool_link_ksettings_add_link_mode(link_ksettings,
588                                                      advertising, TP);
589                 break;
590         case MLX5E_PORT_AUI:
591                 ethtool_link_ksettings_add_link_mode(link_ksettings,
592                                                      supported, AUI);
593                 ethtool_link_ksettings_add_link_mode(link_ksettings,
594                                                      advertising, AUI);
595                 break;
596         case MLX5E_PORT_BNC:
597                 ethtool_link_ksettings_add_link_mode(link_ksettings,
598                                                      supported, BNC);
599                 ethtool_link_ksettings_add_link_mode(link_ksettings,
600                                                      advertising, BNC);
601                 break;
602         case MLX5E_PORT_MII:
603                 ethtool_link_ksettings_add_link_mode(link_ksettings,
604                                                      supported, MII);
605                 ethtool_link_ksettings_add_link_mode(link_ksettings,
606                                                      advertising, MII);
607                 break;
608         case MLX5E_PORT_FIBRE:
609                 ethtool_link_ksettings_add_link_mode(link_ksettings,
610                                                      supported, FIBRE);
611                 ethtool_link_ksettings_add_link_mode(link_ksettings,
612                                                      advertising, FIBRE);
613                 break;
614         case MLX5E_PORT_DA:
615                 ethtool_link_ksettings_add_link_mode(link_ksettings,
616                                                      supported, Backplane);
617                 ethtool_link_ksettings_add_link_mode(link_ksettings,
618                                                      advertising, Backplane);
619                 break;
620         case MLX5E_PORT_NONE:
621         case MLX5E_PORT_OTHER:
622         default:
623                 break;
624         }
625 }
626
627 static void get_speed_duplex(struct net_device *netdev,
628                              u32 eth_proto_oper,
629                              struct ethtool_link_ksettings *link_ksettings)
630 {
631         u32 speed = SPEED_UNKNOWN;
632         u8 duplex = DUPLEX_UNKNOWN;
633
634         if (!netif_carrier_ok(netdev))
635                 goto out;
636
637         speed = mlx5e_port_ptys2speed(eth_proto_oper);
638         if (!speed) {
639                 speed = SPEED_UNKNOWN;
640                 goto out;
641         }
642
643         duplex = DUPLEX_FULL;
644
645 out:
646         link_ksettings->base.speed = speed;
647         link_ksettings->base.duplex = duplex;
648 }
649
650 static void get_supported(u32 eth_proto_cap,
651                           struct ethtool_link_ksettings *link_ksettings)
652 {
653         unsigned long *supported = link_ksettings->link_modes.supported;
654
655         ptys2ethtool_supported_link(supported, eth_proto_cap);
656         ethtool_link_ksettings_add_link_mode(link_ksettings, supported, Pause);
657 }
658
659 static void get_advertising(u32 eth_proto_cap, u8 tx_pause,
660                             u8 rx_pause,
661                             struct ethtool_link_ksettings *link_ksettings)
662 {
663         unsigned long *advertising = link_ksettings->link_modes.advertising;
664
665         ptys2ethtool_adver_link(advertising, eth_proto_cap);
666         if (rx_pause)
667                 ethtool_link_ksettings_add_link_mode(link_ksettings, advertising, Pause);
668         if (tx_pause ^ rx_pause)
669                 ethtool_link_ksettings_add_link_mode(link_ksettings, advertising, Asym_Pause);
670 }
671
672 static int ptys2connector_type[MLX5E_CONNECTOR_TYPE_NUMBER] = {
673                 [MLX5E_PORT_UNKNOWN]            = PORT_OTHER,
674                 [MLX5E_PORT_NONE]               = PORT_NONE,
675                 [MLX5E_PORT_TP]                 = PORT_TP,
676                 [MLX5E_PORT_AUI]                = PORT_AUI,
677                 [MLX5E_PORT_BNC]                = PORT_BNC,
678                 [MLX5E_PORT_MII]                = PORT_MII,
679                 [MLX5E_PORT_FIBRE]              = PORT_FIBRE,
680                 [MLX5E_PORT_DA]                 = PORT_DA,
681                 [MLX5E_PORT_OTHER]              = PORT_OTHER,
682         };
683
684 static u8 get_connector_port(u32 eth_proto, u8 connector_type)
685 {
686         if (connector_type && connector_type < MLX5E_CONNECTOR_TYPE_NUMBER)
687                 return ptys2connector_type[connector_type];
688
689         if (eth_proto &
690             (MLX5E_PROT_MASK(MLX5E_10GBASE_SR)   |
691              MLX5E_PROT_MASK(MLX5E_40GBASE_SR4)  |
692              MLX5E_PROT_MASK(MLX5E_100GBASE_SR4) |
693              MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII))) {
694                 return PORT_FIBRE;
695         }
696
697         if (eth_proto &
698             (MLX5E_PROT_MASK(MLX5E_40GBASE_CR4) |
699              MLX5E_PROT_MASK(MLX5E_10GBASE_CR)  |
700              MLX5E_PROT_MASK(MLX5E_100GBASE_CR4))) {
701                 return PORT_DA;
702         }
703
704         if (eth_proto &
705             (MLX5E_PROT_MASK(MLX5E_10GBASE_KX4) |
706              MLX5E_PROT_MASK(MLX5E_10GBASE_KR)  |
707              MLX5E_PROT_MASK(MLX5E_40GBASE_KR4) |
708              MLX5E_PROT_MASK(MLX5E_100GBASE_KR4))) {
709                 return PORT_NONE;
710         }
711
712         return PORT_OTHER;
713 }
714
715 static void get_lp_advertising(u32 eth_proto_lp,
716                                struct ethtool_link_ksettings *link_ksettings)
717 {
718         unsigned long *lp_advertising = link_ksettings->link_modes.lp_advertising;
719
720         ptys2ethtool_adver_link(lp_advertising, eth_proto_lp);
721 }
722
723 static int mlx5e_get_link_ksettings(struct net_device *netdev,
724                                     struct ethtool_link_ksettings *link_ksettings)
725 {
726         struct mlx5e_priv *priv    = netdev_priv(netdev);
727         struct mlx5_core_dev *mdev = priv->mdev;
728         u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
729         u32 rx_pause = 0;
730         u32 tx_pause = 0;
731         u32 eth_proto_cap;
732         u32 eth_proto_admin;
733         u32 eth_proto_lp;
734         u32 eth_proto_oper;
735         u8 an_disable_admin;
736         u8 an_status;
737         u8 connector_type;
738         int err;
739
740         err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 1);
741         if (err) {
742                 netdev_err(netdev, "%s: query port ptys failed: %d\n",
743                            __func__, err);
744                 goto err_query_ptys;
745         }
746
747         eth_proto_cap    = MLX5_GET(ptys_reg, out, eth_proto_capability);
748         eth_proto_admin  = MLX5_GET(ptys_reg, out, eth_proto_admin);
749         eth_proto_oper   = MLX5_GET(ptys_reg, out, eth_proto_oper);
750         eth_proto_lp     = MLX5_GET(ptys_reg, out, eth_proto_lp_advertise);
751         an_disable_admin = MLX5_GET(ptys_reg, out, an_disable_admin);
752         an_status        = MLX5_GET(ptys_reg, out, an_status);
753         connector_type   = MLX5_GET(ptys_reg, out, connector_type);
754
755         mlx5_query_port_pause(mdev, &rx_pause, &tx_pause);
756
757         ethtool_link_ksettings_zero_link_mode(link_ksettings, supported);
758         ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising);
759
760         get_supported(eth_proto_cap, link_ksettings);
761         get_advertising(eth_proto_admin, tx_pause, rx_pause, link_ksettings);
762         get_speed_duplex(netdev, eth_proto_oper, link_ksettings);
763
764         eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
765
766         link_ksettings->base.port = get_connector_port(eth_proto_oper,
767                                                        connector_type);
768         ptys2ethtool_supported_advertised_port(link_ksettings, eth_proto_admin,
769                                                connector_type);
770         get_lp_advertising(eth_proto_lp, link_ksettings);
771
772         if (an_status == MLX5_AN_COMPLETE)
773                 ethtool_link_ksettings_add_link_mode(link_ksettings,
774                                                      lp_advertising, Autoneg);
775
776         link_ksettings->base.autoneg = an_disable_admin ? AUTONEG_DISABLE :
777                                                           AUTONEG_ENABLE;
778         ethtool_link_ksettings_add_link_mode(link_ksettings, supported,
779                                              Autoneg);
780         if (!an_disable_admin)
781                 ethtool_link_ksettings_add_link_mode(link_ksettings,
782                                                      advertising, Autoneg);
783
784 err_query_ptys:
785         return err;
786 }
787
788 static u32 mlx5e_ethtool2ptys_adver_link(const unsigned long *link_modes)
789 {
790         u32 i, ptys_modes = 0;
791
792         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
793                 if (bitmap_intersects(ptys2ethtool_table[i].advertised,
794                                       link_modes,
795                                       __ETHTOOL_LINK_MODE_MASK_NBITS))
796                         ptys_modes |= MLX5E_PROT_MASK(i);
797         }
798
799         return ptys_modes;
800 }
801
802 static int mlx5e_set_link_ksettings(struct net_device *netdev,
803                                     const struct ethtool_link_ksettings *link_ksettings)
804 {
805         struct mlx5e_priv *priv    = netdev_priv(netdev);
806         struct mlx5_core_dev *mdev = priv->mdev;
807         u32 eth_proto_cap, eth_proto_admin;
808         bool an_changes = false;
809         u8 an_disable_admin;
810         u8 an_disable_cap;
811         bool an_disable;
812         u32 link_modes;
813         u8 an_status;
814         u32 speed;
815         int err;
816
817         speed = link_ksettings->base.speed;
818
819         link_modes = link_ksettings->base.autoneg == AUTONEG_ENABLE ?
820                 mlx5e_ethtool2ptys_adver_link(link_ksettings->link_modes.advertising) :
821                 mlx5e_port_speed2linkmodes(speed);
822
823         err = mlx5_query_port_proto_cap(mdev, &eth_proto_cap, MLX5_PTYS_EN);
824         if (err) {
825                 netdev_err(netdev, "%s: query port eth proto cap failed: %d\n",
826                            __func__, err);
827                 goto out;
828         }
829
830         link_modes = link_modes & eth_proto_cap;
831         if (!link_modes) {
832                 netdev_err(netdev, "%s: Not supported link mode(s) requested",
833                            __func__);
834                 err = -EINVAL;
835                 goto out;
836         }
837
838         err = mlx5_query_port_proto_admin(mdev, &eth_proto_admin, MLX5_PTYS_EN);
839         if (err) {
840                 netdev_err(netdev, "%s: query port eth proto admin failed: %d\n",
841                            __func__, err);
842                 goto out;
843         }
844
845         mlx5_query_port_autoneg(mdev, MLX5_PTYS_EN, &an_status,
846                                 &an_disable_cap, &an_disable_admin);
847
848         an_disable = link_ksettings->base.autoneg == AUTONEG_DISABLE;
849         an_changes = ((!an_disable && an_disable_admin) ||
850                       (an_disable && !an_disable_admin));
851
852         if (!an_changes && link_modes == eth_proto_admin)
853                 goto out;
854
855         mlx5_set_port_ptys(mdev, an_disable, link_modes, MLX5_PTYS_EN);
856         mlx5_toggle_port_link(mdev);
857
858 out:
859         return err;
860 }
861
862 static u32 mlx5e_get_rxfh_key_size(struct net_device *netdev)
863 {
864         struct mlx5e_priv *priv = netdev_priv(netdev);
865
866         return sizeof(priv->channels.params.toeplitz_hash_key);
867 }
868
869 static u32 mlx5e_get_rxfh_indir_size(struct net_device *netdev)
870 {
871         return MLX5E_INDIR_RQT_SIZE;
872 }
873
874 static int mlx5e_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
875                           u8 *hfunc)
876 {
877         struct mlx5e_priv *priv = netdev_priv(netdev);
878
879         if (indir)
880                 memcpy(indir, priv->channels.params.indirection_rqt,
881                        sizeof(priv->channels.params.indirection_rqt));
882
883         if (key)
884                 memcpy(key, priv->channels.params.toeplitz_hash_key,
885                        sizeof(priv->channels.params.toeplitz_hash_key));
886
887         if (hfunc)
888                 *hfunc = priv->channels.params.rss_hfunc;
889
890         return 0;
891 }
892
893 static void mlx5e_modify_tirs_hash(struct mlx5e_priv *priv, void *in, int inlen)
894 {
895         void *tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
896         struct mlx5_core_dev *mdev = priv->mdev;
897         int ctxlen = MLX5_ST_SZ_BYTES(tirc);
898         int tt;
899
900         MLX5_SET(modify_tir_in, in, bitmask.hash, 1);
901
902         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
903                 memset(tirc, 0, ctxlen);
904                 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
905                 mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in, inlen);
906         }
907
908         if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
909                 return;
910
911         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
912                 memset(tirc, 0, ctxlen);
913                 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, true);
914                 mlx5_core_modify_tir(mdev, priv->inner_indir_tir[tt].tirn, in, inlen);
915         }
916 }
917
918 static int mlx5e_set_rxfh(struct net_device *dev, const u32 *indir,
919                           const u8 *key, const u8 hfunc)
920 {
921         struct mlx5e_priv *priv = netdev_priv(dev);
922         int inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
923         bool hash_changed = false;
924         void *in;
925
926         if ((hfunc != ETH_RSS_HASH_NO_CHANGE) &&
927             (hfunc != ETH_RSS_HASH_XOR) &&
928             (hfunc != ETH_RSS_HASH_TOP))
929                 return -EINVAL;
930
931         in = kvzalloc(inlen, GFP_KERNEL);
932         if (!in)
933                 return -ENOMEM;
934
935         mutex_lock(&priv->state_lock);
936
937         if (hfunc != ETH_RSS_HASH_NO_CHANGE &&
938             hfunc != priv->channels.params.rss_hfunc) {
939                 priv->channels.params.rss_hfunc = hfunc;
940                 hash_changed = true;
941         }
942
943         if (indir) {
944                 memcpy(priv->channels.params.indirection_rqt, indir,
945                        sizeof(priv->channels.params.indirection_rqt));
946
947                 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
948                         u32 rqtn = priv->indir_rqt.rqtn;
949                         struct mlx5e_redirect_rqt_param rrp = {
950                                 .is_rss = true,
951                                 {
952                                         .rss = {
953                                                 .hfunc = priv->channels.params.rss_hfunc,
954                                                 .channels  = &priv->channels,
955                                         },
956                                 },
957                         };
958
959                         mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
960                 }
961         }
962
963         if (key) {
964                 memcpy(priv->channels.params.toeplitz_hash_key, key,
965                        sizeof(priv->channels.params.toeplitz_hash_key));
966                 hash_changed = hash_changed ||
967                                priv->channels.params.rss_hfunc == ETH_RSS_HASH_TOP;
968         }
969
970         if (hash_changed)
971                 mlx5e_modify_tirs_hash(priv, in, inlen);
972
973         mutex_unlock(&priv->state_lock);
974
975         kvfree(in);
976
977         return 0;
978 }
979
980 #define MLX5E_PFC_PREVEN_AUTO_TOUT_MSEC         100
981 #define MLX5E_PFC_PREVEN_TOUT_MAX_MSEC          8000
982 #define MLX5E_PFC_PREVEN_MINOR_PRECENT          85
983 #define MLX5E_PFC_PREVEN_TOUT_MIN_MSEC          80
984 #define MLX5E_DEVICE_STALL_MINOR_WATERMARK(critical_tout) \
985         max_t(u16, MLX5E_PFC_PREVEN_TOUT_MIN_MSEC, \
986               (critical_tout * MLX5E_PFC_PREVEN_MINOR_PRECENT) / 100)
987
988 static int mlx5e_get_pfc_prevention_tout(struct net_device *netdev,
989                                          u16 *pfc_prevention_tout)
990 {
991         struct mlx5e_priv *priv    = netdev_priv(netdev);
992         struct mlx5_core_dev *mdev = priv->mdev;
993
994         if (!MLX5_CAP_PCAM_FEATURE((priv)->mdev, pfcc_mask) ||
995             !MLX5_CAP_DEBUG((priv)->mdev, stall_detect))
996                 return -EOPNOTSUPP;
997
998         return mlx5_query_port_stall_watermark(mdev, pfc_prevention_tout, NULL);
999 }
1000
1001 static int mlx5e_set_pfc_prevention_tout(struct net_device *netdev,
1002                                          u16 pfc_preven)
1003 {
1004         struct mlx5e_priv *priv = netdev_priv(netdev);
1005         struct mlx5_core_dev *mdev = priv->mdev;
1006         u16 critical_tout;
1007         u16 minor;
1008
1009         if (!MLX5_CAP_PCAM_FEATURE((priv)->mdev, pfcc_mask) ||
1010             !MLX5_CAP_DEBUG((priv)->mdev, stall_detect))
1011                 return -EOPNOTSUPP;
1012
1013         critical_tout = (pfc_preven == PFC_STORM_PREVENTION_AUTO) ?
1014                         MLX5E_PFC_PREVEN_AUTO_TOUT_MSEC :
1015                         pfc_preven;
1016
1017         if (critical_tout != PFC_STORM_PREVENTION_DISABLE &&
1018             (critical_tout > MLX5E_PFC_PREVEN_TOUT_MAX_MSEC ||
1019              critical_tout < MLX5E_PFC_PREVEN_TOUT_MIN_MSEC)) {
1020                 netdev_info(netdev, "%s: pfc prevention tout not in range (%d-%d)\n",
1021                             __func__, MLX5E_PFC_PREVEN_TOUT_MIN_MSEC,
1022                             MLX5E_PFC_PREVEN_TOUT_MAX_MSEC);
1023                 return -EINVAL;
1024         }
1025
1026         minor = MLX5E_DEVICE_STALL_MINOR_WATERMARK(critical_tout);
1027         return mlx5_set_port_stall_watermark(mdev, critical_tout,
1028                                              minor);
1029 }
1030
1031 static int mlx5e_get_tunable(struct net_device *dev,
1032                              const struct ethtool_tunable *tuna,
1033                              void *data)
1034 {
1035         int err;
1036
1037         switch (tuna->id) {
1038         case ETHTOOL_PFC_PREVENTION_TOUT:
1039                 err = mlx5e_get_pfc_prevention_tout(dev, data);
1040                 break;
1041         default:
1042                 err = -EINVAL;
1043                 break;
1044         }
1045
1046         return err;
1047 }
1048
1049 static int mlx5e_set_tunable(struct net_device *dev,
1050                              const struct ethtool_tunable *tuna,
1051                              const void *data)
1052 {
1053         struct mlx5e_priv *priv = netdev_priv(dev);
1054         int err;
1055
1056         mutex_lock(&priv->state_lock);
1057
1058         switch (tuna->id) {
1059         case ETHTOOL_PFC_PREVENTION_TOUT:
1060                 err = mlx5e_set_pfc_prevention_tout(dev, *(u16 *)data);
1061                 break;
1062         default:
1063                 err = -EINVAL;
1064                 break;
1065         }
1066
1067         mutex_unlock(&priv->state_lock);
1068         return err;
1069 }
1070
1071 static void mlx5e_get_pauseparam(struct net_device *netdev,
1072                                  struct ethtool_pauseparam *pauseparam)
1073 {
1074         struct mlx5e_priv *priv    = netdev_priv(netdev);
1075         struct mlx5_core_dev *mdev = priv->mdev;
1076         int err;
1077
1078         err = mlx5_query_port_pause(mdev, &pauseparam->rx_pause,
1079                                     &pauseparam->tx_pause);
1080         if (err) {
1081                 netdev_err(netdev, "%s: mlx5_query_port_pause failed:0x%x\n",
1082                            __func__, err);
1083         }
1084 }
1085
1086 static int mlx5e_set_pauseparam(struct net_device *netdev,
1087                                 struct ethtool_pauseparam *pauseparam)
1088 {
1089         struct mlx5e_priv *priv    = netdev_priv(netdev);
1090         struct mlx5_core_dev *mdev = priv->mdev;
1091         int err;
1092
1093         if (pauseparam->autoneg)
1094                 return -EINVAL;
1095
1096         err = mlx5_set_port_pause(mdev,
1097                                   pauseparam->rx_pause ? 1 : 0,
1098                                   pauseparam->tx_pause ? 1 : 0);
1099         if (err) {
1100                 netdev_err(netdev, "%s: mlx5_set_port_pause failed:0x%x\n",
1101                            __func__, err);
1102         }
1103
1104         return err;
1105 }
1106
1107 int mlx5e_ethtool_get_ts_info(struct mlx5e_priv *priv,
1108                               struct ethtool_ts_info *info)
1109 {
1110         struct mlx5_core_dev *mdev = priv->mdev;
1111         int ret;
1112
1113         ret = ethtool_op_get_ts_info(priv->netdev, info);
1114         if (ret)
1115                 return ret;
1116
1117         info->phc_index = mlx5_clock_get_ptp_index(mdev);
1118
1119         if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz) ||
1120             info->phc_index == -1)
1121                 return 0;
1122
1123         info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
1124                                  SOF_TIMESTAMPING_RX_HARDWARE |
1125                                  SOF_TIMESTAMPING_RAW_HARDWARE;
1126
1127         info->tx_types = BIT(HWTSTAMP_TX_OFF) |
1128                          BIT(HWTSTAMP_TX_ON);
1129
1130         info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1131                            BIT(HWTSTAMP_FILTER_ALL);
1132
1133         return 0;
1134 }
1135
1136 static int mlx5e_get_ts_info(struct net_device *dev,
1137                              struct ethtool_ts_info *info)
1138 {
1139         struct mlx5e_priv *priv = netdev_priv(dev);
1140
1141         return mlx5e_ethtool_get_ts_info(priv, info);
1142 }
1143
1144 static __u32 mlx5e_get_wol_supported(struct mlx5_core_dev *mdev)
1145 {
1146         __u32 ret = 0;
1147
1148         if (MLX5_CAP_GEN(mdev, wol_g))
1149                 ret |= WAKE_MAGIC;
1150
1151         if (MLX5_CAP_GEN(mdev, wol_s))
1152                 ret |= WAKE_MAGICSECURE;
1153
1154         if (MLX5_CAP_GEN(mdev, wol_a))
1155                 ret |= WAKE_ARP;
1156
1157         if (MLX5_CAP_GEN(mdev, wol_b))
1158                 ret |= WAKE_BCAST;
1159
1160         if (MLX5_CAP_GEN(mdev, wol_m))
1161                 ret |= WAKE_MCAST;
1162
1163         if (MLX5_CAP_GEN(mdev, wol_u))
1164                 ret |= WAKE_UCAST;
1165
1166         if (MLX5_CAP_GEN(mdev, wol_p))
1167                 ret |= WAKE_PHY;
1168
1169         return ret;
1170 }
1171
1172 static __u32 mlx5e_refomrat_wol_mode_mlx5_to_linux(u8 mode)
1173 {
1174         __u32 ret = 0;
1175
1176         if (mode & MLX5_WOL_MAGIC)
1177                 ret |= WAKE_MAGIC;
1178
1179         if (mode & MLX5_WOL_SECURED_MAGIC)
1180                 ret |= WAKE_MAGICSECURE;
1181
1182         if (mode & MLX5_WOL_ARP)
1183                 ret |= WAKE_ARP;
1184
1185         if (mode & MLX5_WOL_BROADCAST)
1186                 ret |= WAKE_BCAST;
1187
1188         if (mode & MLX5_WOL_MULTICAST)
1189                 ret |= WAKE_MCAST;
1190
1191         if (mode & MLX5_WOL_UNICAST)
1192                 ret |= WAKE_UCAST;
1193
1194         if (mode & MLX5_WOL_PHY_ACTIVITY)
1195                 ret |= WAKE_PHY;
1196
1197         return ret;
1198 }
1199
1200 static u8 mlx5e_refomrat_wol_mode_linux_to_mlx5(__u32 mode)
1201 {
1202         u8 ret = 0;
1203
1204         if (mode & WAKE_MAGIC)
1205                 ret |= MLX5_WOL_MAGIC;
1206
1207         if (mode & WAKE_MAGICSECURE)
1208                 ret |= MLX5_WOL_SECURED_MAGIC;
1209
1210         if (mode & WAKE_ARP)
1211                 ret |= MLX5_WOL_ARP;
1212
1213         if (mode & WAKE_BCAST)
1214                 ret |= MLX5_WOL_BROADCAST;
1215
1216         if (mode & WAKE_MCAST)
1217                 ret |= MLX5_WOL_MULTICAST;
1218
1219         if (mode & WAKE_UCAST)
1220                 ret |= MLX5_WOL_UNICAST;
1221
1222         if (mode & WAKE_PHY)
1223                 ret |= MLX5_WOL_PHY_ACTIVITY;
1224
1225         return ret;
1226 }
1227
1228 static void mlx5e_get_wol(struct net_device *netdev,
1229                           struct ethtool_wolinfo *wol)
1230 {
1231         struct mlx5e_priv *priv = netdev_priv(netdev);
1232         struct mlx5_core_dev *mdev = priv->mdev;
1233         u8 mlx5_wol_mode;
1234         int err;
1235
1236         memset(wol, 0, sizeof(*wol));
1237
1238         wol->supported = mlx5e_get_wol_supported(mdev);
1239         if (!wol->supported)
1240                 return;
1241
1242         err = mlx5_query_port_wol(mdev, &mlx5_wol_mode);
1243         if (err)
1244                 return;
1245
1246         wol->wolopts = mlx5e_refomrat_wol_mode_mlx5_to_linux(mlx5_wol_mode);
1247 }
1248
1249 static int mlx5e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1250 {
1251         struct mlx5e_priv *priv = netdev_priv(netdev);
1252         struct mlx5_core_dev *mdev = priv->mdev;
1253         __u32 wol_supported = mlx5e_get_wol_supported(mdev);
1254         u32 mlx5_wol_mode;
1255
1256         if (!wol_supported)
1257                 return -EOPNOTSUPP;
1258
1259         if (wol->wolopts & ~wol_supported)
1260                 return -EINVAL;
1261
1262         mlx5_wol_mode = mlx5e_refomrat_wol_mode_linux_to_mlx5(wol->wolopts);
1263
1264         return mlx5_set_port_wol(mdev, mlx5_wol_mode);
1265 }
1266
1267 static u32 mlx5e_get_msglevel(struct net_device *dev)
1268 {
1269         return ((struct mlx5e_priv *)netdev_priv(dev))->msglevel;
1270 }
1271
1272 static void mlx5e_set_msglevel(struct net_device *dev, u32 val)
1273 {
1274         ((struct mlx5e_priv *)netdev_priv(dev))->msglevel = val;
1275 }
1276
1277 static int mlx5e_set_phys_id(struct net_device *dev,
1278                              enum ethtool_phys_id_state state)
1279 {
1280         struct mlx5e_priv *priv = netdev_priv(dev);
1281         struct mlx5_core_dev *mdev = priv->mdev;
1282         u16 beacon_duration;
1283
1284         if (!MLX5_CAP_GEN(mdev, beacon_led))
1285                 return -EOPNOTSUPP;
1286
1287         switch (state) {
1288         case ETHTOOL_ID_ACTIVE:
1289                 beacon_duration = MLX5_BEACON_DURATION_INF;
1290                 break;
1291         case ETHTOOL_ID_INACTIVE:
1292                 beacon_duration = MLX5_BEACON_DURATION_OFF;
1293                 break;
1294         default:
1295                 return -EOPNOTSUPP;
1296         }
1297
1298         return mlx5_set_port_beacon(mdev, beacon_duration);
1299 }
1300
1301 static int mlx5e_get_module_info(struct net_device *netdev,
1302                                  struct ethtool_modinfo *modinfo)
1303 {
1304         struct mlx5e_priv *priv = netdev_priv(netdev);
1305         struct mlx5_core_dev *dev = priv->mdev;
1306         int size_read = 0;
1307         u8 data[4];
1308
1309         size_read = mlx5_query_module_eeprom(dev, 0, 2, data);
1310         if (size_read < 2)
1311                 return -EIO;
1312
1313         /* data[0] = identifier byte */
1314         switch (data[0]) {
1315         case MLX5_MODULE_ID_QSFP:
1316                 modinfo->type       = ETH_MODULE_SFF_8436;
1317                 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1318                 break;
1319         case MLX5_MODULE_ID_QSFP_PLUS:
1320         case MLX5_MODULE_ID_QSFP28:
1321                 /* data[1] = revision id */
1322                 if (data[0] == MLX5_MODULE_ID_QSFP28 || data[1] >= 0x3) {
1323                         modinfo->type       = ETH_MODULE_SFF_8636;
1324                         modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1325                 } else {
1326                         modinfo->type       = ETH_MODULE_SFF_8436;
1327                         modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1328                 }
1329                 break;
1330         case MLX5_MODULE_ID_SFP:
1331                 modinfo->type       = ETH_MODULE_SFF_8472;
1332                 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1333                 break;
1334         default:
1335                 netdev_err(priv->netdev, "%s: cable type not recognized:0x%x\n",
1336                            __func__, data[0]);
1337                 return -EINVAL;
1338         }
1339
1340         return 0;
1341 }
1342
1343 static int mlx5e_get_module_eeprom(struct net_device *netdev,
1344                                    struct ethtool_eeprom *ee,
1345                                    u8 *data)
1346 {
1347         struct mlx5e_priv *priv = netdev_priv(netdev);
1348         struct mlx5_core_dev *mdev = priv->mdev;
1349         int offset = ee->offset;
1350         int size_read;
1351         int i = 0;
1352
1353         if (!ee->len)
1354                 return -EINVAL;
1355
1356         memset(data, 0, ee->len);
1357
1358         while (i < ee->len) {
1359                 size_read = mlx5_query_module_eeprom(mdev, offset, ee->len - i,
1360                                                      data + i);
1361
1362                 if (!size_read)
1363                         /* Done reading */
1364                         return 0;
1365
1366                 if (size_read < 0) {
1367                         netdev_err(priv->netdev, "%s: mlx5_query_eeprom failed:0x%x\n",
1368                                    __func__, size_read);
1369                         return 0;
1370                 }
1371
1372                 i += size_read;
1373                 offset += size_read;
1374         }
1375
1376         return 0;
1377 }
1378
1379 typedef int (*mlx5e_pflag_handler)(struct net_device *netdev, bool enable);
1380
1381 static int set_pflag_cqe_based_moder(struct net_device *netdev, bool enable,
1382                                      bool is_rx_cq)
1383 {
1384         struct mlx5e_priv *priv = netdev_priv(netdev);
1385         struct mlx5_core_dev *mdev = priv->mdev;
1386         struct mlx5e_channels new_channels = {};
1387         bool mode_changed;
1388         u8 cq_period_mode, current_cq_period_mode;
1389         int err = 0;
1390
1391         cq_period_mode = enable ?
1392                 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
1393                 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1394         current_cq_period_mode = is_rx_cq ?
1395                 priv->channels.params.rx_cq_moderation.cq_period_mode :
1396                 priv->channels.params.tx_cq_moderation.cq_period_mode;
1397         mode_changed = cq_period_mode != current_cq_period_mode;
1398
1399         if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE &&
1400             !MLX5_CAP_GEN(mdev, cq_period_start_from_cqe))
1401                 return -EOPNOTSUPP;
1402
1403         if (!mode_changed)
1404                 return 0;
1405
1406         new_channels.params = priv->channels.params;
1407         if (is_rx_cq)
1408                 mlx5e_set_rx_cq_mode_params(&new_channels.params, cq_period_mode);
1409         else
1410                 mlx5e_set_tx_cq_mode_params(&new_channels.params, cq_period_mode);
1411
1412         if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
1413                 priv->channels.params = new_channels.params;
1414                 return 0;
1415         }
1416
1417         err = mlx5e_open_channels(priv, &new_channels);
1418         if (err)
1419                 return err;
1420
1421         mlx5e_switch_priv_channels(priv, &new_channels, NULL);
1422         return 0;
1423 }
1424
1425 static int set_pflag_tx_cqe_based_moder(struct net_device *netdev, bool enable)
1426 {
1427         return set_pflag_cqe_based_moder(netdev, enable, false);
1428 }
1429
1430 static int set_pflag_rx_cqe_based_moder(struct net_device *netdev, bool enable)
1431 {
1432         return set_pflag_cqe_based_moder(netdev, enable, true);
1433 }
1434
1435 int mlx5e_modify_rx_cqe_compression_locked(struct mlx5e_priv *priv, bool new_val)
1436 {
1437         bool curr_val = MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS);
1438         struct mlx5e_channels new_channels = {};
1439         int err = 0;
1440
1441         if (!MLX5_CAP_GEN(priv->mdev, cqe_compression))
1442                 return new_val ? -EOPNOTSUPP : 0;
1443
1444         if (curr_val == new_val)
1445                 return 0;
1446
1447         new_channels.params = priv->channels.params;
1448         MLX5E_SET_PFLAG(&new_channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS, new_val);
1449
1450         if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
1451                 priv->channels.params = new_channels.params;
1452                 return 0;
1453         }
1454
1455         err = mlx5e_open_channels(priv, &new_channels);
1456         if (err)
1457                 return err;
1458
1459         mlx5e_switch_priv_channels(priv, &new_channels, NULL);
1460         mlx5e_dbg(DRV, priv, "MLX5E: RxCqeCmprss was turned %s\n",
1461                   MLX5E_GET_PFLAG(&priv->channels.params,
1462                                   MLX5E_PFLAG_RX_CQE_COMPRESS) ? "ON" : "OFF");
1463
1464         return 0;
1465 }
1466
1467 static int set_pflag_rx_cqe_compress(struct net_device *netdev,
1468                                      bool enable)
1469 {
1470         struct mlx5e_priv *priv = netdev_priv(netdev);
1471         struct mlx5_core_dev *mdev = priv->mdev;
1472
1473         if (!MLX5_CAP_GEN(mdev, cqe_compression))
1474                 return -EOPNOTSUPP;
1475
1476         if (enable && priv->tstamp.rx_filter != HWTSTAMP_FILTER_NONE) {
1477                 netdev_err(netdev, "Can't enable cqe compression while timestamping is enabled.\n");
1478                 return -EINVAL;
1479         }
1480
1481         mlx5e_modify_rx_cqe_compression_locked(priv, enable);
1482         priv->channels.params.rx_cqe_compress_def = enable;
1483
1484         return 0;
1485 }
1486
1487 static int set_pflag_rx_striding_rq(struct net_device *netdev, bool enable)
1488 {
1489         struct mlx5e_priv *priv = netdev_priv(netdev);
1490         struct mlx5_core_dev *mdev = priv->mdev;
1491         struct mlx5e_channels new_channels = {};
1492         int err;
1493
1494         if (enable) {
1495                 if (!mlx5e_check_fragmented_striding_rq_cap(mdev))
1496                         return -EOPNOTSUPP;
1497                 if (!mlx5e_striding_rq_possible(mdev, &priv->channels.params))
1498                         return -EINVAL;
1499         } else if (priv->channels.params.lro_en) {
1500                 netdev_warn(netdev, "Can't set legacy RQ with LRO, disable LRO first\n");
1501                 return -EINVAL;
1502         }
1503
1504         new_channels.params = priv->channels.params;
1505
1506         MLX5E_SET_PFLAG(&new_channels.params, MLX5E_PFLAG_RX_STRIDING_RQ, enable);
1507         mlx5e_set_rq_type(mdev, &new_channels.params);
1508
1509         if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
1510                 priv->channels.params = new_channels.params;
1511                 return 0;
1512         }
1513
1514         err = mlx5e_open_channels(priv, &new_channels);
1515         if (err)
1516                 return err;
1517
1518         mlx5e_switch_priv_channels(priv, &new_channels, NULL);
1519         return 0;
1520 }
1521
1522 static int mlx5e_handle_pflag(struct net_device *netdev,
1523                               u32 wanted_flags,
1524                               enum mlx5e_priv_flag flag,
1525                               mlx5e_pflag_handler pflag_handler)
1526 {
1527         struct mlx5e_priv *priv = netdev_priv(netdev);
1528         bool enable = !!(wanted_flags & flag);
1529         u32 changes = wanted_flags ^ priv->channels.params.pflags;
1530         int err;
1531
1532         if (!(changes & flag))
1533                 return 0;
1534
1535         err = pflag_handler(netdev, enable);
1536         if (err) {
1537                 netdev_err(netdev, "%s private flag 0x%x failed err %d\n",
1538                            enable ? "Enable" : "Disable", flag, err);
1539                 return err;
1540         }
1541
1542         MLX5E_SET_PFLAG(&priv->channels.params, flag, enable);
1543         return 0;
1544 }
1545
1546 static int mlx5e_set_priv_flags(struct net_device *netdev, u32 pflags)
1547 {
1548         struct mlx5e_priv *priv = netdev_priv(netdev);
1549         int err;
1550
1551         mutex_lock(&priv->state_lock);
1552         err = mlx5e_handle_pflag(netdev, pflags,
1553                                  MLX5E_PFLAG_RX_CQE_BASED_MODER,
1554                                  set_pflag_rx_cqe_based_moder);
1555         if (err)
1556                 goto out;
1557
1558         err = mlx5e_handle_pflag(netdev, pflags,
1559                                  MLX5E_PFLAG_TX_CQE_BASED_MODER,
1560                                  set_pflag_tx_cqe_based_moder);
1561         if (err)
1562                 goto out;
1563
1564         err = mlx5e_handle_pflag(netdev, pflags,
1565                                  MLX5E_PFLAG_RX_CQE_COMPRESS,
1566                                  set_pflag_rx_cqe_compress);
1567         if (err)
1568                 goto out;
1569
1570         err = mlx5e_handle_pflag(netdev, pflags,
1571                                  MLX5E_PFLAG_RX_STRIDING_RQ,
1572                                  set_pflag_rx_striding_rq);
1573
1574 out:
1575         mutex_unlock(&priv->state_lock);
1576
1577         /* Need to fix some features.. */
1578         netdev_update_features(netdev);
1579
1580         return err;
1581 }
1582
1583 static u32 mlx5e_get_priv_flags(struct net_device *netdev)
1584 {
1585         struct mlx5e_priv *priv = netdev_priv(netdev);
1586
1587         return priv->channels.params.pflags;
1588 }
1589
1590 int mlx5e_ethtool_flash_device(struct mlx5e_priv *priv,
1591                                struct ethtool_flash *flash)
1592 {
1593         struct mlx5_core_dev *mdev = priv->mdev;
1594         struct net_device *dev = priv->netdev;
1595         const struct firmware *fw;
1596         int err;
1597
1598         if (flash->region != ETHTOOL_FLASH_ALL_REGIONS)
1599                 return -EOPNOTSUPP;
1600
1601         err = request_firmware_direct(&fw, flash->data, &dev->dev);
1602         if (err)
1603                 return err;
1604
1605         dev_hold(dev);
1606         rtnl_unlock();
1607
1608         err = mlx5_firmware_flash(mdev, fw);
1609         release_firmware(fw);
1610
1611         rtnl_lock();
1612         dev_put(dev);
1613         return err;
1614 }
1615
1616 static int mlx5e_flash_device(struct net_device *dev,
1617                               struct ethtool_flash *flash)
1618 {
1619         struct mlx5e_priv *priv = netdev_priv(dev);
1620
1621         return mlx5e_ethtool_flash_device(priv, flash);
1622 }
1623
1624 const struct ethtool_ops mlx5e_ethtool_ops = {
1625         .get_drvinfo       = mlx5e_get_drvinfo,
1626         .get_link          = ethtool_op_get_link,
1627         .get_strings       = mlx5e_get_strings,
1628         .get_sset_count    = mlx5e_get_sset_count,
1629         .get_ethtool_stats = mlx5e_get_ethtool_stats,
1630         .get_ringparam     = mlx5e_get_ringparam,
1631         .set_ringparam     = mlx5e_set_ringparam,
1632         .get_channels      = mlx5e_get_channels,
1633         .set_channels      = mlx5e_set_channels,
1634         .get_coalesce      = mlx5e_get_coalesce,
1635         .set_coalesce      = mlx5e_set_coalesce,
1636         .get_link_ksettings  = mlx5e_get_link_ksettings,
1637         .set_link_ksettings  = mlx5e_set_link_ksettings,
1638         .get_rxfh_key_size   = mlx5e_get_rxfh_key_size,
1639         .get_rxfh_indir_size = mlx5e_get_rxfh_indir_size,
1640         .get_rxfh          = mlx5e_get_rxfh,
1641         .set_rxfh          = mlx5e_set_rxfh,
1642 #ifdef CONFIG_MLX5_EN_RXNFC
1643         .get_rxnfc         = mlx5e_get_rxnfc,
1644         .set_rxnfc         = mlx5e_set_rxnfc,
1645 #endif
1646         .flash_device      = mlx5e_flash_device,
1647         .get_tunable       = mlx5e_get_tunable,
1648         .set_tunable       = mlx5e_set_tunable,
1649         .get_pauseparam    = mlx5e_get_pauseparam,
1650         .set_pauseparam    = mlx5e_set_pauseparam,
1651         .get_ts_info       = mlx5e_get_ts_info,
1652         .set_phys_id       = mlx5e_set_phys_id,
1653         .get_wol           = mlx5e_get_wol,
1654         .set_wol           = mlx5e_set_wol,
1655         .get_module_info   = mlx5e_get_module_info,
1656         .get_module_eeprom = mlx5e_get_module_eeprom,
1657         .get_priv_flags    = mlx5e_get_priv_flags,
1658         .set_priv_flags    = mlx5e_set_priv_flags,
1659         .self_test         = mlx5e_self_test,
1660         .get_msglevel      = mlx5e_get_msglevel,
1661         .set_msglevel      = mlx5e_set_msglevel,
1662 };