c4be3941957ec23eb5f05a721bf2165ad12eaf81
[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
35 static void mlx5e_get_drvinfo(struct net_device *dev,
36                               struct ethtool_drvinfo *drvinfo)
37 {
38         struct mlx5e_priv *priv = netdev_priv(dev);
39         struct mlx5_core_dev *mdev = priv->mdev;
40
41         strlcpy(drvinfo->driver, DRIVER_NAME, sizeof(drvinfo->driver));
42         strlcpy(drvinfo->version, DRIVER_VERSION " (" DRIVER_RELDATE ")",
43                 sizeof(drvinfo->version));
44         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
45                  "%d.%d.%d",
46                  fw_rev_maj(mdev), fw_rev_min(mdev), fw_rev_sub(mdev));
47         strlcpy(drvinfo->bus_info, pci_name(mdev->pdev),
48                 sizeof(drvinfo->bus_info));
49 }
50
51 static const struct {
52         u32 supported;
53         u32 advertised;
54         u32 speed;
55 } ptys2ethtool_table[MLX5E_LINK_MODES_NUMBER] = {
56         [MLX5E_1000BASE_CX_SGMII] = {
57                 .supported  = SUPPORTED_1000baseKX_Full,
58                 .advertised = ADVERTISED_1000baseKX_Full,
59                 .speed      = 1000,
60         },
61         [MLX5E_1000BASE_KX] = {
62                 .supported  = SUPPORTED_1000baseKX_Full,
63                 .advertised = ADVERTISED_1000baseKX_Full,
64                 .speed      = 1000,
65         },
66         [MLX5E_10GBASE_CX4] = {
67                 .supported  = SUPPORTED_10000baseKX4_Full,
68                 .advertised = ADVERTISED_10000baseKX4_Full,
69                 .speed      = 10000,
70         },
71         [MLX5E_10GBASE_KX4] = {
72                 .supported  = SUPPORTED_10000baseKX4_Full,
73                 .advertised = ADVERTISED_10000baseKX4_Full,
74                 .speed      = 10000,
75         },
76         [MLX5E_10GBASE_KR] = {
77                 .supported  = SUPPORTED_10000baseKR_Full,
78                 .advertised = ADVERTISED_10000baseKR_Full,
79                 .speed      = 10000,
80         },
81         [MLX5E_20GBASE_KR2] = {
82                 .supported  = SUPPORTED_20000baseKR2_Full,
83                 .advertised = ADVERTISED_20000baseKR2_Full,
84                 .speed      = 20000,
85         },
86         [MLX5E_40GBASE_CR4] = {
87                 .supported  = SUPPORTED_40000baseCR4_Full,
88                 .advertised = ADVERTISED_40000baseCR4_Full,
89                 .speed      = 40000,
90         },
91         [MLX5E_40GBASE_KR4] = {
92                 .supported  = SUPPORTED_40000baseKR4_Full,
93                 .advertised = ADVERTISED_40000baseKR4_Full,
94                 .speed      = 40000,
95         },
96         [MLX5E_56GBASE_R4] = {
97                 .supported  = SUPPORTED_56000baseKR4_Full,
98                 .advertised = ADVERTISED_56000baseKR4_Full,
99                 .speed      = 56000,
100         },
101         [MLX5E_10GBASE_CR] = {
102                 .supported  = SUPPORTED_10000baseKR_Full,
103                 .advertised = ADVERTISED_10000baseKR_Full,
104                 .speed      = 10000,
105         },
106         [MLX5E_10GBASE_SR] = {
107                 .supported  = SUPPORTED_10000baseKR_Full,
108                 .advertised = ADVERTISED_10000baseKR_Full,
109                 .speed      = 10000,
110         },
111         [MLX5E_10GBASE_ER] = {
112                 .supported  = SUPPORTED_10000baseKR_Full,
113                 .advertised = ADVERTISED_10000baseKR_Full,
114                 .speed      = 10000,
115         },
116         [MLX5E_40GBASE_SR4] = {
117                 .supported  = SUPPORTED_40000baseSR4_Full,
118                 .advertised = ADVERTISED_40000baseSR4_Full,
119                 .speed      = 40000,
120         },
121         [MLX5E_40GBASE_LR4] = {
122                 .supported  = SUPPORTED_40000baseLR4_Full,
123                 .advertised = ADVERTISED_40000baseLR4_Full,
124                 .speed      = 40000,
125         },
126         [MLX5E_100GBASE_CR4] = {
127                 .speed      = 100000,
128         },
129         [MLX5E_100GBASE_SR4] = {
130                 .speed      = 100000,
131         },
132         [MLX5E_100GBASE_KR4] = {
133                 .speed      = 100000,
134         },
135         [MLX5E_100GBASE_LR4] = {
136                 .speed      = 100000,
137         },
138         [MLX5E_100BASE_TX]   = {
139                 .speed      = 100,
140         },
141         [MLX5E_1000BASE_T]    = {
142                 .supported  = SUPPORTED_1000baseT_Full,
143                 .advertised = ADVERTISED_1000baseT_Full,
144                 .speed      = 1000,
145         },
146         [MLX5E_10GBASE_T]    = {
147                 .supported  = SUPPORTED_10000baseT_Full,
148                 .advertised = ADVERTISED_10000baseT_Full,
149                 .speed      = 1000,
150         },
151         [MLX5E_25GBASE_CR]   = {
152                 .speed      = 25000,
153         },
154         [MLX5E_25GBASE_KR]   = {
155                 .speed      = 25000,
156         },
157         [MLX5E_25GBASE_SR]   = {
158                 .speed      = 25000,
159         },
160         [MLX5E_50GBASE_CR2]  = {
161                 .speed      = 50000,
162         },
163         [MLX5E_50GBASE_KR2]  = {
164                 .speed      = 50000,
165         },
166 };
167
168 static unsigned long mlx5e_query_pfc_combined(struct mlx5e_priv *priv)
169 {
170         struct mlx5_core_dev *mdev = priv->mdev;
171         u8 pfc_en_tx;
172         u8 pfc_en_rx;
173         int err;
174
175         err = mlx5_query_port_pfc(mdev, &pfc_en_tx, &pfc_en_rx);
176
177         return err ? 0 : pfc_en_tx | pfc_en_rx;
178 }
179
180 #define MLX5E_NUM_Q_CNTRS(priv) (NUM_Q_COUNTERS * (!!priv->q_counter))
181 #define MLX5E_NUM_RQ_STATS(priv) \
182         (NUM_RQ_STATS * priv->params.num_channels * \
183          test_bit(MLX5E_STATE_OPENED, &priv->state))
184 #define MLX5E_NUM_SQ_STATS(priv) \
185         (NUM_SQ_STATS * priv->params.num_channels * priv->params.num_tc * \
186          test_bit(MLX5E_STATE_OPENED, &priv->state))
187 #define MLX5E_NUM_PFC_COUNTERS(priv) hweight8(mlx5e_query_pfc_combined(priv))
188
189 static int mlx5e_get_sset_count(struct net_device *dev, int sset)
190 {
191         struct mlx5e_priv *priv = netdev_priv(dev);
192
193         switch (sset) {
194         case ETH_SS_STATS:
195                 return NUM_SW_COUNTERS +
196                        MLX5E_NUM_Q_CNTRS(priv) +
197                        NUM_VPORT_COUNTERS + NUM_PPORT_COUNTERS +
198                        MLX5E_NUM_RQ_STATS(priv) +
199                        MLX5E_NUM_SQ_STATS(priv) +
200                        MLX5E_NUM_PFC_COUNTERS(priv);
201         case ETH_SS_PRIV_FLAGS:
202                 return ARRAY_SIZE(mlx5e_priv_flags);
203         /* fallthrough */
204         default:
205                 return -EOPNOTSUPP;
206         }
207 }
208
209 static void mlx5e_fill_stats_strings(struct mlx5e_priv *priv, uint8_t *data)
210 {
211         int i, j, tc, prio, idx = 0;
212         unsigned long pfc_combined;
213
214         /* SW counters */
215         for (i = 0; i < NUM_SW_COUNTERS; i++)
216                 strcpy(data + (idx++) * ETH_GSTRING_LEN, sw_stats_desc[i].name);
217
218         /* Q counters */
219         for (i = 0; i < MLX5E_NUM_Q_CNTRS(priv); i++)
220                 strcpy(data + (idx++) * ETH_GSTRING_LEN, q_stats_desc[i].name);
221
222         /* VPORT counters */
223         for (i = 0; i < NUM_VPORT_COUNTERS; i++)
224                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
225                        vport_stats_desc[i].name);
226
227         /* PPORT counters */
228         for (i = 0; i < NUM_PPORT_802_3_COUNTERS; i++)
229                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
230                        pport_802_3_stats_desc[i].name);
231
232         for (i = 0; i < NUM_PPORT_2863_COUNTERS; i++)
233                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
234                        pport_2863_stats_desc[i].name);
235
236         for (i = 0; i < NUM_PPORT_2819_COUNTERS; i++)
237                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
238                        pport_2819_stats_desc[i].name);
239
240         for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
241                 for (i = 0; i < NUM_PPORT_PER_PRIO_TRAFFIC_COUNTERS; i++)
242                         sprintf(data + (idx++) * ETH_GSTRING_LEN, "prio%d_%s",
243                                 prio,
244                                 pport_per_prio_traffic_stats_desc[i].name);
245         }
246
247         pfc_combined = mlx5e_query_pfc_combined(priv);
248         for_each_set_bit(prio, &pfc_combined, NUM_PPORT_PRIO) {
249                 for (i = 0; i < NUM_PPORT_PER_PRIO_PFC_COUNTERS; i++) {
250                         sprintf(data + (idx++) * ETH_GSTRING_LEN, "prio%d_%s",
251                                 prio, pport_per_prio_pfc_stats_desc[i].name);
252                 }
253         }
254
255         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
256                 return;
257
258         /* per channel counters */
259         for (i = 0; i < priv->params.num_channels; i++)
260                 for (j = 0; j < NUM_RQ_STATS; j++)
261                         sprintf(data + (idx++) * ETH_GSTRING_LEN, "rx%d_%s", i,
262                                 rq_stats_desc[j].name);
263
264         for (tc = 0; tc < priv->params.num_tc; tc++)
265                 for (i = 0; i < priv->params.num_channels; i++)
266                         for (j = 0; j < NUM_SQ_STATS; j++)
267                                 sprintf(data + (idx++) * ETH_GSTRING_LEN,
268                                         "tx%d_%s",
269                                         priv->channeltc_to_txq_map[i][tc],
270                                         sq_stats_desc[j].name);
271 }
272
273 static void mlx5e_get_strings(struct net_device *dev,
274                               uint32_t stringset, uint8_t *data)
275 {
276         struct mlx5e_priv *priv = netdev_priv(dev);
277         int i;
278
279         switch (stringset) {
280         case ETH_SS_PRIV_FLAGS:
281                 for (i = 0; i < ARRAY_SIZE(mlx5e_priv_flags); i++)
282                         strcpy(data + i * ETH_GSTRING_LEN, mlx5e_priv_flags[i]);
283                 break;
284
285         case ETH_SS_TEST:
286                 break;
287
288         case ETH_SS_STATS:
289                 mlx5e_fill_stats_strings(priv, data);
290                 break;
291         }
292 }
293
294 static void mlx5e_get_ethtool_stats(struct net_device *dev,
295                                     struct ethtool_stats *stats, u64 *data)
296 {
297         struct mlx5e_priv *priv = netdev_priv(dev);
298         int i, j, tc, prio, idx = 0;
299         unsigned long pfc_combined;
300
301         if (!data)
302                 return;
303
304         mutex_lock(&priv->state_lock);
305         if (test_bit(MLX5E_STATE_OPENED, &priv->state))
306                 mlx5e_update_stats(priv);
307         mutex_unlock(&priv->state_lock);
308
309         for (i = 0; i < NUM_SW_COUNTERS; i++)
310                 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
311                                                    sw_stats_desc, i);
312
313         for (i = 0; i < MLX5E_NUM_Q_CNTRS(priv); i++)
314                 data[idx++] = MLX5E_READ_CTR32_CPU(&priv->stats.qcnt,
315                                                    q_stats_desc, i);
316
317         for (i = 0; i < NUM_VPORT_COUNTERS; i++)
318                 data[idx++] = MLX5E_READ_CTR64_BE(priv->stats.vport.query_vport_out,
319                                                   vport_stats_desc, i);
320
321         for (i = 0; i < NUM_PPORT_802_3_COUNTERS; i++)
322                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.IEEE_802_3_counters,
323                                                   pport_802_3_stats_desc, i);
324
325         for (i = 0; i < NUM_PPORT_2863_COUNTERS; i++)
326                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.RFC_2863_counters,
327                                                   pport_2863_stats_desc, i);
328
329         for (i = 0; i < NUM_PPORT_2819_COUNTERS; i++)
330                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.RFC_2819_counters,
331                                                   pport_2819_stats_desc, i);
332
333         for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
334                 for (i = 0; i < NUM_PPORT_PER_PRIO_TRAFFIC_COUNTERS; i++)
335                         data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.per_prio_counters[prio],
336                                                  pport_per_prio_traffic_stats_desc, i);
337         }
338
339         pfc_combined = mlx5e_query_pfc_combined(priv);
340         for_each_set_bit(prio, &pfc_combined, NUM_PPORT_PRIO) {
341                 for (i = 0; i < NUM_PPORT_PER_PRIO_PFC_COUNTERS; i++) {
342                         data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.per_prio_counters[prio],
343                                                           pport_per_prio_pfc_stats_desc, i);
344                 }
345         }
346
347         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
348                 return;
349
350         /* per channel counters */
351         for (i = 0; i < priv->params.num_channels; i++)
352                 for (j = 0; j < NUM_RQ_STATS; j++)
353                         data[idx++] =
354                                MLX5E_READ_CTR64_CPU(&priv->channel[i]->rq.stats,
355                                                     rq_stats_desc, j);
356
357         for (tc = 0; tc < priv->params.num_tc; tc++)
358                 for (i = 0; i < priv->params.num_channels; i++)
359                         for (j = 0; j < NUM_SQ_STATS; j++)
360                                 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->channel[i]->sq[tc].stats,
361                                                                    sq_stats_desc, j);
362 }
363
364 static void mlx5e_get_ringparam(struct net_device *dev,
365                                 struct ethtool_ringparam *param)
366 {
367         struct mlx5e_priv *priv = netdev_priv(dev);
368         int rq_wq_type = priv->params.rq_wq_type;
369
370         param->rx_max_pending = 1 << mlx5_max_log_rq_size(rq_wq_type);
371         param->tx_max_pending = 1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE;
372         param->rx_pending     = 1 << priv->params.log_rq_size;
373         param->tx_pending     = 1 << priv->params.log_sq_size;
374 }
375
376 static int mlx5e_set_ringparam(struct net_device *dev,
377                                struct ethtool_ringparam *param)
378 {
379         struct mlx5e_priv *priv = netdev_priv(dev);
380         bool was_opened;
381         int rq_wq_type = priv->params.rq_wq_type;
382         u16 min_rx_wqes;
383         u8 log_rq_size;
384         u8 log_sq_size;
385         int err = 0;
386
387         if (param->rx_jumbo_pending) {
388                 netdev_info(dev, "%s: rx_jumbo_pending not supported\n",
389                             __func__);
390                 return -EINVAL;
391         }
392         if (param->rx_mini_pending) {
393                 netdev_info(dev, "%s: rx_mini_pending not supported\n",
394                             __func__);
395                 return -EINVAL;
396         }
397         if (param->rx_pending < (1 << mlx5_min_log_rq_size(rq_wq_type))) {
398                 netdev_info(dev, "%s: rx_pending (%d) < min (%d)\n",
399                             __func__, param->rx_pending,
400                             1 << mlx5_min_log_rq_size(rq_wq_type));
401                 return -EINVAL;
402         }
403         if (param->rx_pending > (1 << mlx5_max_log_rq_size(rq_wq_type))) {
404                 netdev_info(dev, "%s: rx_pending (%d) > max (%d)\n",
405                             __func__, param->rx_pending,
406                             1 << mlx5_max_log_rq_size(rq_wq_type));
407                 return -EINVAL;
408         }
409         if (param->tx_pending < (1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)) {
410                 netdev_info(dev, "%s: tx_pending (%d) < min (%d)\n",
411                             __func__, param->tx_pending,
412                             1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE);
413                 return -EINVAL;
414         }
415         if (param->tx_pending > (1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE)) {
416                 netdev_info(dev, "%s: tx_pending (%d) > max (%d)\n",
417                             __func__, param->tx_pending,
418                             1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE);
419                 return -EINVAL;
420         }
421
422         log_rq_size = order_base_2(param->rx_pending);
423         log_sq_size = order_base_2(param->tx_pending);
424         min_rx_wqes = mlx5_min_rx_wqes(rq_wq_type, param->rx_pending);
425
426         if (log_rq_size == priv->params.log_rq_size &&
427             log_sq_size == priv->params.log_sq_size &&
428             min_rx_wqes == priv->params.min_rx_wqes)
429                 return 0;
430
431         mutex_lock(&priv->state_lock);
432
433         was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
434         if (was_opened)
435                 mlx5e_close_locked(dev);
436
437         priv->params.log_rq_size = log_rq_size;
438         priv->params.log_sq_size = log_sq_size;
439         priv->params.min_rx_wqes = min_rx_wqes;
440
441         if (was_opened)
442                 err = mlx5e_open_locked(dev);
443
444         mutex_unlock(&priv->state_lock);
445
446         return err;
447 }
448
449 static void mlx5e_get_channels(struct net_device *dev,
450                                struct ethtool_channels *ch)
451 {
452         struct mlx5e_priv *priv = netdev_priv(dev);
453
454         ch->max_combined   = mlx5e_get_max_num_channels(priv->mdev);
455         ch->combined_count = priv->params.num_channels;
456 }
457
458 static int mlx5e_set_channels(struct net_device *dev,
459                               struct ethtool_channels *ch)
460 {
461         struct mlx5e_priv *priv = netdev_priv(dev);
462         int ncv = mlx5e_get_max_num_channels(priv->mdev);
463         unsigned int count = ch->combined_count;
464         bool arfs_enabled;
465         bool was_opened;
466         int err = 0;
467
468         if (!count) {
469                 netdev_info(dev, "%s: combined_count=0 not supported\n",
470                             __func__);
471                 return -EINVAL;
472         }
473         if (ch->rx_count || ch->tx_count) {
474                 netdev_info(dev, "%s: separate rx/tx count not supported\n",
475                             __func__);
476                 return -EINVAL;
477         }
478         if (count > ncv) {
479                 netdev_info(dev, "%s: count (%d) > max (%d)\n",
480                             __func__, count, ncv);
481                 return -EINVAL;
482         }
483
484         if (priv->params.num_channels == count)
485                 return 0;
486
487         mutex_lock(&priv->state_lock);
488
489         was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
490         if (was_opened)
491                 mlx5e_close_locked(dev);
492
493         arfs_enabled = dev->features & NETIF_F_NTUPLE;
494         if (arfs_enabled)
495                 mlx5e_arfs_disable(priv);
496
497         priv->params.num_channels = count;
498         mlx5e_build_default_indir_rqt(priv->mdev, priv->params.indirection_rqt,
499                                       MLX5E_INDIR_RQT_SIZE, count);
500
501         if (was_opened)
502                 err = mlx5e_open_locked(dev);
503         if (err)
504                 goto out;
505
506         if (arfs_enabled) {
507                 err = mlx5e_arfs_enable(priv);
508                 if (err)
509                         netdev_err(dev, "%s: mlx5e_arfs_enable failed: %d\n",
510                                    __func__, err);
511         }
512
513 out:
514         mutex_unlock(&priv->state_lock);
515
516         return err;
517 }
518
519 static int mlx5e_get_coalesce(struct net_device *netdev,
520                               struct ethtool_coalesce *coal)
521 {
522         struct mlx5e_priv *priv = netdev_priv(netdev);
523
524         if (!MLX5_CAP_GEN(priv->mdev, cq_moderation))
525                 return -ENOTSUPP;
526
527         coal->rx_coalesce_usecs       = priv->params.rx_cq_moderation.usec;
528         coal->rx_max_coalesced_frames = priv->params.rx_cq_moderation.pkts;
529         coal->tx_coalesce_usecs       = priv->params.tx_cq_moderation.usec;
530         coal->tx_max_coalesced_frames = priv->params.tx_cq_moderation.pkts;
531         coal->use_adaptive_rx_coalesce = priv->params.rx_am_enabled;
532
533         return 0;
534 }
535
536 static int mlx5e_set_coalesce(struct net_device *netdev,
537                               struct ethtool_coalesce *coal)
538 {
539         struct mlx5e_priv *priv    = netdev_priv(netdev);
540         struct mlx5_core_dev *mdev = priv->mdev;
541         struct mlx5e_channel *c;
542         bool restart =
543                 !!coal->use_adaptive_rx_coalesce != priv->params.rx_am_enabled;
544         bool was_opened;
545         int err = 0;
546         int tc;
547         int i;
548
549         if (!MLX5_CAP_GEN(mdev, cq_moderation))
550                 return -ENOTSUPP;
551
552         mutex_lock(&priv->state_lock);
553
554         was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
555         if (was_opened && restart) {
556                 mlx5e_close_locked(netdev);
557                 priv->params.rx_am_enabled = !!coal->use_adaptive_rx_coalesce;
558         }
559
560         priv->params.tx_cq_moderation.usec = coal->tx_coalesce_usecs;
561         priv->params.tx_cq_moderation.pkts = coal->tx_max_coalesced_frames;
562         priv->params.rx_cq_moderation.usec = coal->rx_coalesce_usecs;
563         priv->params.rx_cq_moderation.pkts = coal->rx_max_coalesced_frames;
564
565         if (!was_opened || restart)
566                 goto out;
567
568         for (i = 0; i < priv->params.num_channels; ++i) {
569                 c = priv->channel[i];
570
571                 for (tc = 0; tc < c->num_tc; tc++) {
572                         mlx5_core_modify_cq_moderation(mdev,
573                                                 &c->sq[tc].cq.mcq,
574                                                 coal->tx_coalesce_usecs,
575                                                 coal->tx_max_coalesced_frames);
576                 }
577
578                 mlx5_core_modify_cq_moderation(mdev, &c->rq.cq.mcq,
579                                                coal->rx_coalesce_usecs,
580                                                coal->rx_max_coalesced_frames);
581         }
582
583 out:
584         if (was_opened && restart)
585                 err = mlx5e_open_locked(netdev);
586
587         mutex_unlock(&priv->state_lock);
588         return err;
589 }
590
591 static u32 ptys2ethtool_supported_link(u32 eth_proto_cap)
592 {
593         int i;
594         u32 supported_modes = 0;
595
596         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
597                 if (eth_proto_cap & MLX5E_PROT_MASK(i))
598                         supported_modes |= ptys2ethtool_table[i].supported;
599         }
600         return supported_modes;
601 }
602
603 static u32 ptys2ethtool_adver_link(u32 eth_proto_cap)
604 {
605         int i;
606         u32 advertising_modes = 0;
607
608         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
609                 if (eth_proto_cap & MLX5E_PROT_MASK(i))
610                         advertising_modes |= ptys2ethtool_table[i].advertised;
611         }
612         return advertising_modes;
613 }
614
615 static u32 ptys2ethtool_supported_port(u32 eth_proto_cap)
616 {
617         if (eth_proto_cap & (MLX5E_PROT_MASK(MLX5E_10GBASE_CR)
618                            | MLX5E_PROT_MASK(MLX5E_10GBASE_SR)
619                            | MLX5E_PROT_MASK(MLX5E_40GBASE_CR4)
620                            | MLX5E_PROT_MASK(MLX5E_40GBASE_SR4)
621                            | MLX5E_PROT_MASK(MLX5E_100GBASE_SR4)
622                            | MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII))) {
623                 return SUPPORTED_FIBRE;
624         }
625
626         if (eth_proto_cap & (MLX5E_PROT_MASK(MLX5E_100GBASE_KR4)
627                            | MLX5E_PROT_MASK(MLX5E_40GBASE_KR4)
628                            | MLX5E_PROT_MASK(MLX5E_10GBASE_KR)
629                            | MLX5E_PROT_MASK(MLX5E_10GBASE_KX4)
630                            | MLX5E_PROT_MASK(MLX5E_1000BASE_KX))) {
631                 return SUPPORTED_Backplane;
632         }
633         return 0;
634 }
635
636 int mlx5e_get_max_linkspeed(struct mlx5_core_dev *mdev, u32 *speed)
637 {
638         u32 max_speed = 0;
639         u32 proto_cap;
640         int err;
641         int i;
642
643         err = mlx5_query_port_proto_cap(mdev, &proto_cap, MLX5_PTYS_EN);
644         if (err)
645                 return err;
646
647         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i)
648                 if (proto_cap & MLX5E_PROT_MASK(i))
649                         max_speed = max(max_speed, ptys2ethtool_table[i].speed);
650
651         *speed = max_speed;
652         return 0;
653 }
654
655 static void get_speed_duplex(struct net_device *netdev,
656                              u32 eth_proto_oper,
657                              struct ethtool_cmd *cmd)
658 {
659         int i;
660         u32 speed = SPEED_UNKNOWN;
661         u8 duplex = DUPLEX_UNKNOWN;
662
663         if (!netif_carrier_ok(netdev))
664                 goto out;
665
666         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
667                 if (eth_proto_oper & MLX5E_PROT_MASK(i)) {
668                         speed = ptys2ethtool_table[i].speed;
669                         duplex = DUPLEX_FULL;
670                         break;
671                 }
672         }
673 out:
674         ethtool_cmd_speed_set(cmd, speed);
675         cmd->duplex = duplex;
676 }
677
678 static void get_supported(u32 eth_proto_cap, u32 *supported)
679 {
680         *supported |= ptys2ethtool_supported_port(eth_proto_cap);
681         *supported |= ptys2ethtool_supported_link(eth_proto_cap);
682         *supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
683 }
684
685 static void get_advertising(u32 eth_proto_cap, u8 tx_pause,
686                             u8 rx_pause, u32 *advertising)
687 {
688         *advertising |= ptys2ethtool_adver_link(eth_proto_cap);
689         *advertising |= tx_pause ? ADVERTISED_Pause : 0;
690         *advertising |= (tx_pause ^ rx_pause) ? ADVERTISED_Asym_Pause : 0;
691 }
692
693 static u8 get_connector_port(u32 eth_proto)
694 {
695         if (eth_proto & (MLX5E_PROT_MASK(MLX5E_10GBASE_SR)
696                          | MLX5E_PROT_MASK(MLX5E_40GBASE_SR4)
697                          | MLX5E_PROT_MASK(MLX5E_100GBASE_SR4)
698                          | MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII))) {
699                         return PORT_FIBRE;
700         }
701
702         if (eth_proto & (MLX5E_PROT_MASK(MLX5E_40GBASE_CR4)
703                          | MLX5E_PROT_MASK(MLX5E_10GBASE_CR)
704                          | MLX5E_PROT_MASK(MLX5E_100GBASE_CR4))) {
705                         return PORT_DA;
706         }
707
708         if (eth_proto & (MLX5E_PROT_MASK(MLX5E_10GBASE_KX4)
709                          | MLX5E_PROT_MASK(MLX5E_10GBASE_KR)
710                          | MLX5E_PROT_MASK(MLX5E_40GBASE_KR4)
711                          | MLX5E_PROT_MASK(MLX5E_100GBASE_KR4))) {
712                         return PORT_NONE;
713         }
714
715         return PORT_OTHER;
716 }
717
718 static void get_lp_advertising(u32 eth_proto_lp, u32 *lp_advertising)
719 {
720         *lp_advertising = ptys2ethtool_adver_link(eth_proto_lp);
721 }
722
723 static int mlx5e_get_settings(struct net_device *netdev,
724                               struct ethtool_cmd *cmd)
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)];
729         u32 eth_proto_cap;
730         u32 eth_proto_admin;
731         u32 eth_proto_lp;
732         u32 eth_proto_oper;
733         int err;
734
735         err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 1);
736
737         if (err) {
738                 netdev_err(netdev, "%s: query port ptys failed: %d\n",
739                            __func__, err);
740                 goto err_query_ptys;
741         }
742
743         eth_proto_cap   = MLX5_GET(ptys_reg, out, eth_proto_capability);
744         eth_proto_admin = MLX5_GET(ptys_reg, out, eth_proto_admin);
745         eth_proto_oper  = MLX5_GET(ptys_reg, out, eth_proto_oper);
746         eth_proto_lp    = MLX5_GET(ptys_reg, out, eth_proto_lp_advertise);
747
748         cmd->supported   = 0;
749         cmd->advertising = 0;
750
751         get_supported(eth_proto_cap, &cmd->supported);
752         get_advertising(eth_proto_admin, 0, 0, &cmd->advertising);
753         get_speed_duplex(netdev, eth_proto_oper, cmd);
754
755         eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
756
757         cmd->port = get_connector_port(eth_proto_oper);
758         get_lp_advertising(eth_proto_lp, &cmd->lp_advertising);
759
760         cmd->transceiver = XCVR_INTERNAL;
761
762 err_query_ptys:
763         return err;
764 }
765
766 static u32 mlx5e_ethtool2ptys_adver_link(u32 link_modes)
767 {
768         u32 i, ptys_modes = 0;
769
770         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
771                 if (ptys2ethtool_table[i].advertised & link_modes)
772                         ptys_modes |= MLX5E_PROT_MASK(i);
773         }
774
775         return ptys_modes;
776 }
777
778 static u32 mlx5e_ethtool2ptys_speed_link(u32 speed)
779 {
780         u32 i, speed_links = 0;
781
782         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
783                 if (ptys2ethtool_table[i].speed == speed)
784                         speed_links |= MLX5E_PROT_MASK(i);
785         }
786
787         return speed_links;
788 }
789
790 static int mlx5e_set_settings(struct net_device *netdev,
791                               struct ethtool_cmd *cmd)
792 {
793         struct mlx5e_priv *priv    = netdev_priv(netdev);
794         struct mlx5_core_dev *mdev = priv->mdev;
795         u32 link_modes;
796         u32 speed;
797         u32 eth_proto_cap, eth_proto_admin;
798         enum mlx5_port_status ps;
799         int err;
800
801         speed = ethtool_cmd_speed(cmd);
802
803         link_modes = cmd->autoneg == AUTONEG_ENABLE ?
804                 mlx5e_ethtool2ptys_adver_link(cmd->advertising) :
805                 mlx5e_ethtool2ptys_speed_link(speed);
806
807         err = mlx5_query_port_proto_cap(mdev, &eth_proto_cap, MLX5_PTYS_EN);
808         if (err) {
809                 netdev_err(netdev, "%s: query port eth proto cap failed: %d\n",
810                            __func__, err);
811                 goto out;
812         }
813
814         link_modes = link_modes & eth_proto_cap;
815         if (!link_modes) {
816                 netdev_err(netdev, "%s: Not supported link mode(s) requested",
817                            __func__);
818                 err = -EINVAL;
819                 goto out;
820         }
821
822         err = mlx5_query_port_proto_admin(mdev, &eth_proto_admin, MLX5_PTYS_EN);
823         if (err) {
824                 netdev_err(netdev, "%s: query port eth proto admin failed: %d\n",
825                            __func__, err);
826                 goto out;
827         }
828
829         if (link_modes == eth_proto_admin)
830                 goto out;
831
832         mlx5_query_port_admin_status(mdev, &ps);
833         if (ps == MLX5_PORT_UP)
834                 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
835         mlx5_set_port_proto(mdev, link_modes, MLX5_PTYS_EN);
836         if (ps == MLX5_PORT_UP)
837                 mlx5_set_port_admin_status(mdev, MLX5_PORT_UP);
838
839 out:
840         return err;
841 }
842
843 static u32 mlx5e_get_rxfh_key_size(struct net_device *netdev)
844 {
845         struct mlx5e_priv *priv = netdev_priv(netdev);
846
847         return sizeof(priv->params.toeplitz_hash_key);
848 }
849
850 static u32 mlx5e_get_rxfh_indir_size(struct net_device *netdev)
851 {
852         return MLX5E_INDIR_RQT_SIZE;
853 }
854
855 static int mlx5e_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
856                           u8 *hfunc)
857 {
858         struct mlx5e_priv *priv = netdev_priv(netdev);
859
860         if (indir)
861                 memcpy(indir, priv->params.indirection_rqt,
862                        sizeof(priv->params.indirection_rqt));
863
864         if (key)
865                 memcpy(key, priv->params.toeplitz_hash_key,
866                        sizeof(priv->params.toeplitz_hash_key));
867
868         if (hfunc)
869                 *hfunc = priv->params.rss_hfunc;
870
871         return 0;
872 }
873
874 static void mlx5e_modify_tirs_hash(struct mlx5e_priv *priv, void *in, int inlen)
875 {
876         struct mlx5_core_dev *mdev = priv->mdev;
877         void *tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
878         int i;
879
880         MLX5_SET(modify_tir_in, in, bitmask.hash, 1);
881         mlx5e_build_tir_ctx_hash(tirc, priv);
882
883         for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
884                 mlx5_core_modify_tir(mdev, priv->indir_tirn[i], in, inlen);
885 }
886
887 static int mlx5e_set_rxfh(struct net_device *dev, const u32 *indir,
888                           const u8 *key, const u8 hfunc)
889 {
890         struct mlx5e_priv *priv = netdev_priv(dev);
891         int inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
892         void *in;
893
894         if ((hfunc != ETH_RSS_HASH_NO_CHANGE) &&
895             (hfunc != ETH_RSS_HASH_XOR) &&
896             (hfunc != ETH_RSS_HASH_TOP))
897                 return -EINVAL;
898
899         in = mlx5_vzalloc(inlen);
900         if (!in)
901                 return -ENOMEM;
902
903         mutex_lock(&priv->state_lock);
904
905         if (indir) {
906                 u32 rqtn = priv->indir_rqtn;
907
908                 memcpy(priv->params.indirection_rqt, indir,
909                        sizeof(priv->params.indirection_rqt));
910                 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
911         }
912
913         if (key)
914                 memcpy(priv->params.toeplitz_hash_key, key,
915                        sizeof(priv->params.toeplitz_hash_key));
916
917         if (hfunc != ETH_RSS_HASH_NO_CHANGE)
918                 priv->params.rss_hfunc = hfunc;
919
920         mlx5e_modify_tirs_hash(priv, in, inlen);
921
922         mutex_unlock(&priv->state_lock);
923
924         kvfree(in);
925
926         return 0;
927 }
928
929 static int mlx5e_get_rxnfc(struct net_device *netdev,
930                            struct ethtool_rxnfc *info, u32 *rule_locs)
931 {
932         struct mlx5e_priv *priv = netdev_priv(netdev);
933         int err = 0;
934
935         switch (info->cmd) {
936         case ETHTOOL_GRXRINGS:
937                 info->data = priv->params.num_channels;
938                 break;
939         default:
940                 err = -EOPNOTSUPP;
941                 break;
942         }
943
944         return err;
945 }
946
947 static int mlx5e_get_tunable(struct net_device *dev,
948                              const struct ethtool_tunable *tuna,
949                              void *data)
950 {
951         const struct mlx5e_priv *priv = netdev_priv(dev);
952         int err = 0;
953
954         switch (tuna->id) {
955         case ETHTOOL_TX_COPYBREAK:
956                 *(u32 *)data = priv->params.tx_max_inline;
957                 break;
958         default:
959                 err = -EINVAL;
960                 break;
961         }
962
963         return err;
964 }
965
966 static int mlx5e_set_tunable(struct net_device *dev,
967                              const struct ethtool_tunable *tuna,
968                              const void *data)
969 {
970         struct mlx5e_priv *priv = netdev_priv(dev);
971         struct mlx5_core_dev *mdev = priv->mdev;
972         bool was_opened;
973         u32 val;
974         int err = 0;
975
976         switch (tuna->id) {
977         case ETHTOOL_TX_COPYBREAK:
978                 val = *(u32 *)data;
979                 if (val > mlx5e_get_max_inline_cap(mdev)) {
980                         err = -EINVAL;
981                         break;
982                 }
983
984                 mutex_lock(&priv->state_lock);
985
986                 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
987                 if (was_opened)
988                         mlx5e_close_locked(dev);
989
990                 priv->params.tx_max_inline = val;
991
992                 if (was_opened)
993                         err = mlx5e_open_locked(dev);
994
995                 mutex_unlock(&priv->state_lock);
996                 break;
997         default:
998                 err = -EINVAL;
999                 break;
1000         }
1001
1002         return err;
1003 }
1004
1005 static void mlx5e_get_pauseparam(struct net_device *netdev,
1006                                  struct ethtool_pauseparam *pauseparam)
1007 {
1008         struct mlx5e_priv *priv    = netdev_priv(netdev);
1009         struct mlx5_core_dev *mdev = priv->mdev;
1010         int err;
1011
1012         err = mlx5_query_port_pause(mdev, &pauseparam->rx_pause,
1013                                     &pauseparam->tx_pause);
1014         if (err) {
1015                 netdev_err(netdev, "%s: mlx5_query_port_pause failed:0x%x\n",
1016                            __func__, err);
1017         }
1018 }
1019
1020 static int mlx5e_set_pauseparam(struct net_device *netdev,
1021                                 struct ethtool_pauseparam *pauseparam)
1022 {
1023         struct mlx5e_priv *priv    = netdev_priv(netdev);
1024         struct mlx5_core_dev *mdev = priv->mdev;
1025         int err;
1026
1027         if (pauseparam->autoneg)
1028                 return -EINVAL;
1029
1030         err = mlx5_set_port_pause(mdev,
1031                                   pauseparam->rx_pause ? 1 : 0,
1032                                   pauseparam->tx_pause ? 1 : 0);
1033         if (err) {
1034                 netdev_err(netdev, "%s: mlx5_set_port_pause failed:0x%x\n",
1035                            __func__, err);
1036         }
1037
1038         return err;
1039 }
1040
1041 static int mlx5e_get_ts_info(struct net_device *dev,
1042                              struct ethtool_ts_info *info)
1043 {
1044         struct mlx5e_priv *priv = netdev_priv(dev);
1045         int ret;
1046
1047         ret = ethtool_op_get_ts_info(dev, info);
1048         if (ret)
1049                 return ret;
1050
1051         info->phc_index = priv->tstamp.ptp ?
1052                           ptp_clock_index(priv->tstamp.ptp) : -1;
1053
1054         if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
1055                 return 0;
1056
1057         info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
1058                                  SOF_TIMESTAMPING_RX_HARDWARE |
1059                                  SOF_TIMESTAMPING_RAW_HARDWARE;
1060
1061         info->tx_types = (BIT(1) << HWTSTAMP_TX_OFF) |
1062                          (BIT(1) << HWTSTAMP_TX_ON);
1063
1064         info->rx_filters = (BIT(1) << HWTSTAMP_FILTER_NONE) |
1065                            (BIT(1) << HWTSTAMP_FILTER_ALL);
1066
1067         return 0;
1068 }
1069
1070 static __u32 mlx5e_get_wol_supported(struct mlx5_core_dev *mdev)
1071 {
1072         __u32 ret = 0;
1073
1074         if (MLX5_CAP_GEN(mdev, wol_g))
1075                 ret |= WAKE_MAGIC;
1076
1077         if (MLX5_CAP_GEN(mdev, wol_s))
1078                 ret |= WAKE_MAGICSECURE;
1079
1080         if (MLX5_CAP_GEN(mdev, wol_a))
1081                 ret |= WAKE_ARP;
1082
1083         if (MLX5_CAP_GEN(mdev, wol_b))
1084                 ret |= WAKE_BCAST;
1085
1086         if (MLX5_CAP_GEN(mdev, wol_m))
1087                 ret |= WAKE_MCAST;
1088
1089         if (MLX5_CAP_GEN(mdev, wol_u))
1090                 ret |= WAKE_UCAST;
1091
1092         if (MLX5_CAP_GEN(mdev, wol_p))
1093                 ret |= WAKE_PHY;
1094
1095         return ret;
1096 }
1097
1098 static __u32 mlx5e_refomrat_wol_mode_mlx5_to_linux(u8 mode)
1099 {
1100         __u32 ret = 0;
1101
1102         if (mode & MLX5_WOL_MAGIC)
1103                 ret |= WAKE_MAGIC;
1104
1105         if (mode & MLX5_WOL_SECURED_MAGIC)
1106                 ret |= WAKE_MAGICSECURE;
1107
1108         if (mode & MLX5_WOL_ARP)
1109                 ret |= WAKE_ARP;
1110
1111         if (mode & MLX5_WOL_BROADCAST)
1112                 ret |= WAKE_BCAST;
1113
1114         if (mode & MLX5_WOL_MULTICAST)
1115                 ret |= WAKE_MCAST;
1116
1117         if (mode & MLX5_WOL_UNICAST)
1118                 ret |= WAKE_UCAST;
1119
1120         if (mode & MLX5_WOL_PHY_ACTIVITY)
1121                 ret |= WAKE_PHY;
1122
1123         return ret;
1124 }
1125
1126 static u8 mlx5e_refomrat_wol_mode_linux_to_mlx5(__u32 mode)
1127 {
1128         u8 ret = 0;
1129
1130         if (mode & WAKE_MAGIC)
1131                 ret |= MLX5_WOL_MAGIC;
1132
1133         if (mode & WAKE_MAGICSECURE)
1134                 ret |= MLX5_WOL_SECURED_MAGIC;
1135
1136         if (mode & WAKE_ARP)
1137                 ret |= MLX5_WOL_ARP;
1138
1139         if (mode & WAKE_BCAST)
1140                 ret |= MLX5_WOL_BROADCAST;
1141
1142         if (mode & WAKE_MCAST)
1143                 ret |= MLX5_WOL_MULTICAST;
1144
1145         if (mode & WAKE_UCAST)
1146                 ret |= MLX5_WOL_UNICAST;
1147
1148         if (mode & WAKE_PHY)
1149                 ret |= MLX5_WOL_PHY_ACTIVITY;
1150
1151         return ret;
1152 }
1153
1154 static void mlx5e_get_wol(struct net_device *netdev,
1155                           struct ethtool_wolinfo *wol)
1156 {
1157         struct mlx5e_priv *priv = netdev_priv(netdev);
1158         struct mlx5_core_dev *mdev = priv->mdev;
1159         u8 mlx5_wol_mode;
1160         int err;
1161
1162         memset(wol, 0, sizeof(*wol));
1163
1164         wol->supported = mlx5e_get_wol_supported(mdev);
1165         if (!wol->supported)
1166                 return;
1167
1168         err = mlx5_query_port_wol(mdev, &mlx5_wol_mode);
1169         if (err)
1170                 return;
1171
1172         wol->wolopts = mlx5e_refomrat_wol_mode_mlx5_to_linux(mlx5_wol_mode);
1173 }
1174
1175 static int mlx5e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1176 {
1177         struct mlx5e_priv *priv = netdev_priv(netdev);
1178         struct mlx5_core_dev *mdev = priv->mdev;
1179         __u32 wol_supported = mlx5e_get_wol_supported(mdev);
1180         u32 mlx5_wol_mode;
1181
1182         if (!wol_supported)
1183                 return -ENOTSUPP;
1184
1185         if (wol->wolopts & ~wol_supported)
1186                 return -EINVAL;
1187
1188         mlx5_wol_mode = mlx5e_refomrat_wol_mode_linux_to_mlx5(wol->wolopts);
1189
1190         return mlx5_set_port_wol(mdev, mlx5_wol_mode);
1191 }
1192
1193 static int mlx5e_set_phys_id(struct net_device *dev,
1194                              enum ethtool_phys_id_state state)
1195 {
1196         struct mlx5e_priv *priv = netdev_priv(dev);
1197         struct mlx5_core_dev *mdev = priv->mdev;
1198         u16 beacon_duration;
1199
1200         if (!MLX5_CAP_GEN(mdev, beacon_led))
1201                 return -EOPNOTSUPP;
1202
1203         switch (state) {
1204         case ETHTOOL_ID_ACTIVE:
1205                 beacon_duration = MLX5_BEACON_DURATION_INF;
1206                 break;
1207         case ETHTOOL_ID_INACTIVE:
1208                 beacon_duration = MLX5_BEACON_DURATION_OFF;
1209                 break;
1210         default:
1211                 return -EOPNOTSUPP;
1212         }
1213
1214         return mlx5_set_port_beacon(mdev, beacon_duration);
1215 }
1216
1217 static int mlx5e_get_module_info(struct net_device *netdev,
1218                                  struct ethtool_modinfo *modinfo)
1219 {
1220         struct mlx5e_priv *priv = netdev_priv(netdev);
1221         struct mlx5_core_dev *dev = priv->mdev;
1222         int size_read = 0;
1223         u8 data[4];
1224
1225         size_read = mlx5_query_module_eeprom(dev, 0, 2, data);
1226         if (size_read < 2)
1227                 return -EIO;
1228
1229         /* data[0] = identifier byte */
1230         switch (data[0]) {
1231         case MLX5_MODULE_ID_QSFP:
1232                 modinfo->type       = ETH_MODULE_SFF_8436;
1233                 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1234                 break;
1235         case MLX5_MODULE_ID_QSFP_PLUS:
1236         case MLX5_MODULE_ID_QSFP28:
1237                 /* data[1] = revision id */
1238                 if (data[0] == MLX5_MODULE_ID_QSFP28 || data[1] >= 0x3) {
1239                         modinfo->type       = ETH_MODULE_SFF_8636;
1240                         modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1241                 } else {
1242                         modinfo->type       = ETH_MODULE_SFF_8436;
1243                         modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1244                 }
1245                 break;
1246         case MLX5_MODULE_ID_SFP:
1247                 modinfo->type       = ETH_MODULE_SFF_8472;
1248                 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1249                 break;
1250         default:
1251                 netdev_err(priv->netdev, "%s: cable type not recognized:0x%x\n",
1252                            __func__, data[0]);
1253                 return -EINVAL;
1254         }
1255
1256         return 0;
1257 }
1258
1259 static int mlx5e_get_module_eeprom(struct net_device *netdev,
1260                                    struct ethtool_eeprom *ee,
1261                                    u8 *data)
1262 {
1263         struct mlx5e_priv *priv = netdev_priv(netdev);
1264         struct mlx5_core_dev *mdev = priv->mdev;
1265         int offset = ee->offset;
1266         int size_read;
1267         int i = 0;
1268
1269         if (!ee->len)
1270                 return -EINVAL;
1271
1272         memset(data, 0, ee->len);
1273
1274         while (i < ee->len) {
1275                 size_read = mlx5_query_module_eeprom(mdev, offset, ee->len - i,
1276                                                      data + i);
1277
1278                 if (!size_read)
1279                         /* Done reading */
1280                         return 0;
1281
1282                 if (size_read < 0) {
1283                         netdev_err(priv->netdev, "%s: mlx5_query_eeprom failed:0x%x\n",
1284                                    __func__, size_read);
1285                         return 0;
1286                 }
1287
1288                 i += size_read;
1289                 offset += size_read;
1290         }
1291
1292         return 0;
1293 }
1294
1295 typedef int (*mlx5e_pflag_handler)(struct net_device *netdev, bool enable);
1296
1297 static int set_pflag_rx_cqe_based_moder(struct net_device *netdev, bool enable)
1298 {
1299         struct mlx5e_priv *priv = netdev_priv(netdev);
1300         struct mlx5_core_dev *mdev = priv->mdev;
1301         bool rx_mode_changed;
1302         u8 rx_cq_period_mode;
1303         int err = 0;
1304         bool reset;
1305
1306         rx_cq_period_mode = enable ?
1307                 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
1308                 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1309         rx_mode_changed = rx_cq_period_mode != priv->params.rx_cq_period_mode;
1310
1311         if (rx_cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE &&
1312             !MLX5_CAP_GEN(mdev, cq_period_start_from_cqe))
1313                 return -ENOTSUPP;
1314
1315         if (!rx_mode_changed)
1316                 return 0;
1317
1318         reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
1319         if (reset)
1320                 mlx5e_close_locked(netdev);
1321
1322         mlx5e_set_rx_cq_mode_params(&priv->params, rx_cq_period_mode);
1323
1324         if (reset)
1325                 err = mlx5e_open_locked(netdev);
1326
1327         return err;
1328 }
1329
1330 static int mlx5e_handle_pflag(struct net_device *netdev,
1331                               u32 wanted_flags,
1332                               enum mlx5e_priv_flag flag,
1333                               mlx5e_pflag_handler pflag_handler)
1334 {
1335         struct mlx5e_priv *priv = netdev_priv(netdev);
1336         bool enable = !!(wanted_flags & flag);
1337         u32 changes = wanted_flags ^ priv->pflags;
1338         int err;
1339
1340         if (!(changes & flag))
1341                 return 0;
1342
1343         err = pflag_handler(netdev, enable);
1344         if (err) {
1345                 netdev_err(netdev, "%s private flag 0x%x failed err %d\n",
1346                            enable ? "Enable" : "Disable", flag, err);
1347                 return err;
1348         }
1349
1350         MLX5E_SET_PRIV_FLAG(priv, flag, enable);
1351         return 0;
1352 }
1353
1354 static int mlx5e_set_priv_flags(struct net_device *netdev, u32 pflags)
1355 {
1356         struct mlx5e_priv *priv = netdev_priv(netdev);
1357         int err;
1358
1359         mutex_lock(&priv->state_lock);
1360
1361         err = mlx5e_handle_pflag(netdev, pflags,
1362                                  MLX5E_PFLAG_RX_CQE_BASED_MODER,
1363                                  set_pflag_rx_cqe_based_moder);
1364
1365         mutex_unlock(&priv->state_lock);
1366         return err ? -EINVAL : 0;
1367 }
1368
1369 static u32 mlx5e_get_priv_flags(struct net_device *netdev)
1370 {
1371         struct mlx5e_priv *priv = netdev_priv(netdev);
1372
1373         return priv->pflags;
1374 }
1375
1376 const struct ethtool_ops mlx5e_ethtool_ops = {
1377         .get_drvinfo       = mlx5e_get_drvinfo,
1378         .get_link          = ethtool_op_get_link,
1379         .get_strings       = mlx5e_get_strings,
1380         .get_sset_count    = mlx5e_get_sset_count,
1381         .get_ethtool_stats = mlx5e_get_ethtool_stats,
1382         .get_ringparam     = mlx5e_get_ringparam,
1383         .set_ringparam     = mlx5e_set_ringparam,
1384         .get_channels      = mlx5e_get_channels,
1385         .set_channels      = mlx5e_set_channels,
1386         .get_coalesce      = mlx5e_get_coalesce,
1387         .set_coalesce      = mlx5e_set_coalesce,
1388         .get_settings      = mlx5e_get_settings,
1389         .set_settings      = mlx5e_set_settings,
1390         .get_rxfh_key_size   = mlx5e_get_rxfh_key_size,
1391         .get_rxfh_indir_size = mlx5e_get_rxfh_indir_size,
1392         .get_rxfh          = mlx5e_get_rxfh,
1393         .set_rxfh          = mlx5e_set_rxfh,
1394         .get_rxnfc         = mlx5e_get_rxnfc,
1395         .get_tunable       = mlx5e_get_tunable,
1396         .set_tunable       = mlx5e_set_tunable,
1397         .get_pauseparam    = mlx5e_get_pauseparam,
1398         .set_pauseparam    = mlx5e_set_pauseparam,
1399         .get_ts_info       = mlx5e_get_ts_info,
1400         .set_phys_id       = mlx5e_set_phys_id,
1401         .get_wol           = mlx5e_get_wol,
1402         .set_wol           = mlx5e_set_wol,
1403         .get_module_info   = mlx5e_get_module_info,
1404         .get_module_eeprom = mlx5e_get_module_eeprom,
1405         .get_priv_flags    = mlx5e_get_priv_flags,
1406         .set_priv_flags    = mlx5e_set_priv_flags
1407 };