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