net/mlx5e: Introduce net device priv flags infrastructure
[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
532         return 0;
533 }
534
535 static int mlx5e_set_coalesce(struct net_device *netdev,
536                               struct ethtool_coalesce *coal)
537 {
538         struct mlx5e_priv *priv    = netdev_priv(netdev);
539         struct mlx5_core_dev *mdev = priv->mdev;
540         struct mlx5e_channel *c;
541         int tc;
542         int i;
543
544         if (!MLX5_CAP_GEN(mdev, cq_moderation))
545                 return -ENOTSUPP;
546
547         mutex_lock(&priv->state_lock);
548         priv->params.tx_cq_moderation_usec = coal->tx_coalesce_usecs;
549         priv->params.tx_cq_moderation_pkts = coal->tx_max_coalesced_frames;
550         priv->params.rx_cq_moderation_usec = coal->rx_coalesce_usecs;
551         priv->params.rx_cq_moderation_pkts = coal->rx_max_coalesced_frames;
552
553         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
554                 goto out;
555
556         for (i = 0; i < priv->params.num_channels; ++i) {
557                 c = priv->channel[i];
558
559                 for (tc = 0; tc < c->num_tc; tc++) {
560                         mlx5_core_modify_cq_moderation(mdev,
561                                                 &c->sq[tc].cq.mcq,
562                                                 coal->tx_coalesce_usecs,
563                                                 coal->tx_max_coalesced_frames);
564                 }
565
566                 mlx5_core_modify_cq_moderation(mdev, &c->rq.cq.mcq,
567                                                coal->rx_coalesce_usecs,
568                                                coal->rx_max_coalesced_frames);
569         }
570
571 out:
572         mutex_unlock(&priv->state_lock);
573         return 0;
574 }
575
576 static u32 ptys2ethtool_supported_link(u32 eth_proto_cap)
577 {
578         int i;
579         u32 supported_modes = 0;
580
581         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
582                 if (eth_proto_cap & MLX5E_PROT_MASK(i))
583                         supported_modes |= ptys2ethtool_table[i].supported;
584         }
585         return supported_modes;
586 }
587
588 static u32 ptys2ethtool_adver_link(u32 eth_proto_cap)
589 {
590         int i;
591         u32 advertising_modes = 0;
592
593         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
594                 if (eth_proto_cap & MLX5E_PROT_MASK(i))
595                         advertising_modes |= ptys2ethtool_table[i].advertised;
596         }
597         return advertising_modes;
598 }
599
600 static u32 ptys2ethtool_supported_port(u32 eth_proto_cap)
601 {
602         if (eth_proto_cap & (MLX5E_PROT_MASK(MLX5E_10GBASE_CR)
603                            | MLX5E_PROT_MASK(MLX5E_10GBASE_SR)
604                            | MLX5E_PROT_MASK(MLX5E_40GBASE_CR4)
605                            | MLX5E_PROT_MASK(MLX5E_40GBASE_SR4)
606                            | MLX5E_PROT_MASK(MLX5E_100GBASE_SR4)
607                            | MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII))) {
608                 return SUPPORTED_FIBRE;
609         }
610
611         if (eth_proto_cap & (MLX5E_PROT_MASK(MLX5E_100GBASE_KR4)
612                            | MLX5E_PROT_MASK(MLX5E_40GBASE_KR4)
613                            | MLX5E_PROT_MASK(MLX5E_10GBASE_KR)
614                            | MLX5E_PROT_MASK(MLX5E_10GBASE_KX4)
615                            | MLX5E_PROT_MASK(MLX5E_1000BASE_KX))) {
616                 return SUPPORTED_Backplane;
617         }
618         return 0;
619 }
620
621 int mlx5e_get_max_linkspeed(struct mlx5_core_dev *mdev, u32 *speed)
622 {
623         u32 max_speed = 0;
624         u32 proto_cap;
625         int err;
626         int i;
627
628         err = mlx5_query_port_proto_cap(mdev, &proto_cap, MLX5_PTYS_EN);
629         if (err)
630                 return err;
631
632         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i)
633                 if (proto_cap & MLX5E_PROT_MASK(i))
634                         max_speed = max(max_speed, ptys2ethtool_table[i].speed);
635
636         *speed = max_speed;
637         return 0;
638 }
639
640 static void get_speed_duplex(struct net_device *netdev,
641                              u32 eth_proto_oper,
642                              struct ethtool_cmd *cmd)
643 {
644         int i;
645         u32 speed = SPEED_UNKNOWN;
646         u8 duplex = DUPLEX_UNKNOWN;
647
648         if (!netif_carrier_ok(netdev))
649                 goto out;
650
651         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
652                 if (eth_proto_oper & MLX5E_PROT_MASK(i)) {
653                         speed = ptys2ethtool_table[i].speed;
654                         duplex = DUPLEX_FULL;
655                         break;
656                 }
657         }
658 out:
659         ethtool_cmd_speed_set(cmd, speed);
660         cmd->duplex = duplex;
661 }
662
663 static void get_supported(u32 eth_proto_cap, u32 *supported)
664 {
665         *supported |= ptys2ethtool_supported_port(eth_proto_cap);
666         *supported |= ptys2ethtool_supported_link(eth_proto_cap);
667         *supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
668 }
669
670 static void get_advertising(u32 eth_proto_cap, u8 tx_pause,
671                             u8 rx_pause, u32 *advertising)
672 {
673         *advertising |= ptys2ethtool_adver_link(eth_proto_cap);
674         *advertising |= tx_pause ? ADVERTISED_Pause : 0;
675         *advertising |= (tx_pause ^ rx_pause) ? ADVERTISED_Asym_Pause : 0;
676 }
677
678 static u8 get_connector_port(u32 eth_proto)
679 {
680         if (eth_proto & (MLX5E_PROT_MASK(MLX5E_10GBASE_SR)
681                          | MLX5E_PROT_MASK(MLX5E_40GBASE_SR4)
682                          | MLX5E_PROT_MASK(MLX5E_100GBASE_SR4)
683                          | MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII))) {
684                         return PORT_FIBRE;
685         }
686
687         if (eth_proto & (MLX5E_PROT_MASK(MLX5E_40GBASE_CR4)
688                          | MLX5E_PROT_MASK(MLX5E_10GBASE_CR)
689                          | MLX5E_PROT_MASK(MLX5E_100GBASE_CR4))) {
690                         return PORT_DA;
691         }
692
693         if (eth_proto & (MLX5E_PROT_MASK(MLX5E_10GBASE_KX4)
694                          | MLX5E_PROT_MASK(MLX5E_10GBASE_KR)
695                          | MLX5E_PROT_MASK(MLX5E_40GBASE_KR4)
696                          | MLX5E_PROT_MASK(MLX5E_100GBASE_KR4))) {
697                         return PORT_NONE;
698         }
699
700         return PORT_OTHER;
701 }
702
703 static void get_lp_advertising(u32 eth_proto_lp, u32 *lp_advertising)
704 {
705         *lp_advertising = ptys2ethtool_adver_link(eth_proto_lp);
706 }
707
708 static int mlx5e_get_settings(struct net_device *netdev,
709                               struct ethtool_cmd *cmd)
710 {
711         struct mlx5e_priv *priv    = netdev_priv(netdev);
712         struct mlx5_core_dev *mdev = priv->mdev;
713         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
714         u32 eth_proto_cap;
715         u32 eth_proto_admin;
716         u32 eth_proto_lp;
717         u32 eth_proto_oper;
718         int err;
719
720         err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 1);
721
722         if (err) {
723                 netdev_err(netdev, "%s: query port ptys failed: %d\n",
724                            __func__, err);
725                 goto err_query_ptys;
726         }
727
728         eth_proto_cap   = MLX5_GET(ptys_reg, out, eth_proto_capability);
729         eth_proto_admin = MLX5_GET(ptys_reg, out, eth_proto_admin);
730         eth_proto_oper  = MLX5_GET(ptys_reg, out, eth_proto_oper);
731         eth_proto_lp    = MLX5_GET(ptys_reg, out, eth_proto_lp_advertise);
732
733         cmd->supported   = 0;
734         cmd->advertising = 0;
735
736         get_supported(eth_proto_cap, &cmd->supported);
737         get_advertising(eth_proto_admin, 0, 0, &cmd->advertising);
738         get_speed_duplex(netdev, eth_proto_oper, cmd);
739
740         eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
741
742         cmd->port = get_connector_port(eth_proto_oper);
743         get_lp_advertising(eth_proto_lp, &cmd->lp_advertising);
744
745         cmd->transceiver = XCVR_INTERNAL;
746
747 err_query_ptys:
748         return err;
749 }
750
751 static u32 mlx5e_ethtool2ptys_adver_link(u32 link_modes)
752 {
753         u32 i, ptys_modes = 0;
754
755         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
756                 if (ptys2ethtool_table[i].advertised & link_modes)
757                         ptys_modes |= MLX5E_PROT_MASK(i);
758         }
759
760         return ptys_modes;
761 }
762
763 static u32 mlx5e_ethtool2ptys_speed_link(u32 speed)
764 {
765         u32 i, speed_links = 0;
766
767         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
768                 if (ptys2ethtool_table[i].speed == speed)
769                         speed_links |= MLX5E_PROT_MASK(i);
770         }
771
772         return speed_links;
773 }
774
775 static int mlx5e_set_settings(struct net_device *netdev,
776                               struct ethtool_cmd *cmd)
777 {
778         struct mlx5e_priv *priv    = netdev_priv(netdev);
779         struct mlx5_core_dev *mdev = priv->mdev;
780         u32 link_modes;
781         u32 speed;
782         u32 eth_proto_cap, eth_proto_admin;
783         enum mlx5_port_status ps;
784         int err;
785
786         speed = ethtool_cmd_speed(cmd);
787
788         link_modes = cmd->autoneg == AUTONEG_ENABLE ?
789                 mlx5e_ethtool2ptys_adver_link(cmd->advertising) :
790                 mlx5e_ethtool2ptys_speed_link(speed);
791
792         err = mlx5_query_port_proto_cap(mdev, &eth_proto_cap, MLX5_PTYS_EN);
793         if (err) {
794                 netdev_err(netdev, "%s: query port eth proto cap failed: %d\n",
795                            __func__, err);
796                 goto out;
797         }
798
799         link_modes = link_modes & eth_proto_cap;
800         if (!link_modes) {
801                 netdev_err(netdev, "%s: Not supported link mode(s) requested",
802                            __func__);
803                 err = -EINVAL;
804                 goto out;
805         }
806
807         err = mlx5_query_port_proto_admin(mdev, &eth_proto_admin, MLX5_PTYS_EN);
808         if (err) {
809                 netdev_err(netdev, "%s: query port eth proto admin failed: %d\n",
810                            __func__, err);
811                 goto out;
812         }
813
814         if (link_modes == eth_proto_admin)
815                 goto out;
816
817         mlx5_query_port_admin_status(mdev, &ps);
818         if (ps == MLX5_PORT_UP)
819                 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
820         mlx5_set_port_proto(mdev, link_modes, MLX5_PTYS_EN);
821         if (ps == MLX5_PORT_UP)
822                 mlx5_set_port_admin_status(mdev, MLX5_PORT_UP);
823
824 out:
825         return err;
826 }
827
828 static u32 mlx5e_get_rxfh_key_size(struct net_device *netdev)
829 {
830         struct mlx5e_priv *priv = netdev_priv(netdev);
831
832         return sizeof(priv->params.toeplitz_hash_key);
833 }
834
835 static u32 mlx5e_get_rxfh_indir_size(struct net_device *netdev)
836 {
837         return MLX5E_INDIR_RQT_SIZE;
838 }
839
840 static int mlx5e_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
841                           u8 *hfunc)
842 {
843         struct mlx5e_priv *priv = netdev_priv(netdev);
844
845         if (indir)
846                 memcpy(indir, priv->params.indirection_rqt,
847                        sizeof(priv->params.indirection_rqt));
848
849         if (key)
850                 memcpy(key, priv->params.toeplitz_hash_key,
851                        sizeof(priv->params.toeplitz_hash_key));
852
853         if (hfunc)
854                 *hfunc = priv->params.rss_hfunc;
855
856         return 0;
857 }
858
859 static void mlx5e_modify_tirs_hash(struct mlx5e_priv *priv, void *in, int inlen)
860 {
861         struct mlx5_core_dev *mdev = priv->mdev;
862         void *tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
863         int i;
864
865         MLX5_SET(modify_tir_in, in, bitmask.hash, 1);
866         mlx5e_build_tir_ctx_hash(tirc, priv);
867
868         for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
869                 mlx5_core_modify_tir(mdev, priv->indir_tirn[i], in, inlen);
870 }
871
872 static int mlx5e_set_rxfh(struct net_device *dev, const u32 *indir,
873                           const u8 *key, const u8 hfunc)
874 {
875         struct mlx5e_priv *priv = netdev_priv(dev);
876         int inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
877         void *in;
878
879         if ((hfunc != ETH_RSS_HASH_NO_CHANGE) &&
880             (hfunc != ETH_RSS_HASH_XOR) &&
881             (hfunc != ETH_RSS_HASH_TOP))
882                 return -EINVAL;
883
884         in = mlx5_vzalloc(inlen);
885         if (!in)
886                 return -ENOMEM;
887
888         mutex_lock(&priv->state_lock);
889
890         if (indir) {
891                 u32 rqtn = priv->indir_rqtn;
892
893                 memcpy(priv->params.indirection_rqt, indir,
894                        sizeof(priv->params.indirection_rqt));
895                 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
896         }
897
898         if (key)
899                 memcpy(priv->params.toeplitz_hash_key, key,
900                        sizeof(priv->params.toeplitz_hash_key));
901
902         if (hfunc != ETH_RSS_HASH_NO_CHANGE)
903                 priv->params.rss_hfunc = hfunc;
904
905         mlx5e_modify_tirs_hash(priv, in, inlen);
906
907         mutex_unlock(&priv->state_lock);
908
909         kvfree(in);
910
911         return 0;
912 }
913
914 static int mlx5e_get_rxnfc(struct net_device *netdev,
915                            struct ethtool_rxnfc *info, u32 *rule_locs)
916 {
917         struct mlx5e_priv *priv = netdev_priv(netdev);
918         int err = 0;
919
920         switch (info->cmd) {
921         case ETHTOOL_GRXRINGS:
922                 info->data = priv->params.num_channels;
923                 break;
924         default:
925                 err = -EOPNOTSUPP;
926                 break;
927         }
928
929         return err;
930 }
931
932 static int mlx5e_get_tunable(struct net_device *dev,
933                              const struct ethtool_tunable *tuna,
934                              void *data)
935 {
936         const struct mlx5e_priv *priv = netdev_priv(dev);
937         int err = 0;
938
939         switch (tuna->id) {
940         case ETHTOOL_TX_COPYBREAK:
941                 *(u32 *)data = priv->params.tx_max_inline;
942                 break;
943         default:
944                 err = -EINVAL;
945                 break;
946         }
947
948         return err;
949 }
950
951 static int mlx5e_set_tunable(struct net_device *dev,
952                              const struct ethtool_tunable *tuna,
953                              const void *data)
954 {
955         struct mlx5e_priv *priv = netdev_priv(dev);
956         struct mlx5_core_dev *mdev = priv->mdev;
957         bool was_opened;
958         u32 val;
959         int err = 0;
960
961         switch (tuna->id) {
962         case ETHTOOL_TX_COPYBREAK:
963                 val = *(u32 *)data;
964                 if (val > mlx5e_get_max_inline_cap(mdev)) {
965                         err = -EINVAL;
966                         break;
967                 }
968
969                 mutex_lock(&priv->state_lock);
970
971                 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
972                 if (was_opened)
973                         mlx5e_close_locked(dev);
974
975                 priv->params.tx_max_inline = val;
976
977                 if (was_opened)
978                         err = mlx5e_open_locked(dev);
979
980                 mutex_unlock(&priv->state_lock);
981                 break;
982         default:
983                 err = -EINVAL;
984                 break;
985         }
986
987         return err;
988 }
989
990 static void mlx5e_get_pauseparam(struct net_device *netdev,
991                                  struct ethtool_pauseparam *pauseparam)
992 {
993         struct mlx5e_priv *priv    = netdev_priv(netdev);
994         struct mlx5_core_dev *mdev = priv->mdev;
995         int err;
996
997         err = mlx5_query_port_pause(mdev, &pauseparam->rx_pause,
998                                     &pauseparam->tx_pause);
999         if (err) {
1000                 netdev_err(netdev, "%s: mlx5_query_port_pause failed:0x%x\n",
1001                            __func__, err);
1002         }
1003 }
1004
1005 static int mlx5e_set_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         if (pauseparam->autoneg)
1013                 return -EINVAL;
1014
1015         err = mlx5_set_port_pause(mdev,
1016                                   pauseparam->rx_pause ? 1 : 0,
1017                                   pauseparam->tx_pause ? 1 : 0);
1018         if (err) {
1019                 netdev_err(netdev, "%s: mlx5_set_port_pause failed:0x%x\n",
1020                            __func__, err);
1021         }
1022
1023         return err;
1024 }
1025
1026 static int mlx5e_get_ts_info(struct net_device *dev,
1027                              struct ethtool_ts_info *info)
1028 {
1029         struct mlx5e_priv *priv = netdev_priv(dev);
1030         int ret;
1031
1032         ret = ethtool_op_get_ts_info(dev, info);
1033         if (ret)
1034                 return ret;
1035
1036         info->phc_index = priv->tstamp.ptp ?
1037                           ptp_clock_index(priv->tstamp.ptp) : -1;
1038
1039         if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
1040                 return 0;
1041
1042         info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
1043                                  SOF_TIMESTAMPING_RX_HARDWARE |
1044                                  SOF_TIMESTAMPING_RAW_HARDWARE;
1045
1046         info->tx_types = (BIT(1) << HWTSTAMP_TX_OFF) |
1047                          (BIT(1) << HWTSTAMP_TX_ON);
1048
1049         info->rx_filters = (BIT(1) << HWTSTAMP_FILTER_NONE) |
1050                            (BIT(1) << HWTSTAMP_FILTER_ALL);
1051
1052         return 0;
1053 }
1054
1055 static __u32 mlx5e_get_wol_supported(struct mlx5_core_dev *mdev)
1056 {
1057         __u32 ret = 0;
1058
1059         if (MLX5_CAP_GEN(mdev, wol_g))
1060                 ret |= WAKE_MAGIC;
1061
1062         if (MLX5_CAP_GEN(mdev, wol_s))
1063                 ret |= WAKE_MAGICSECURE;
1064
1065         if (MLX5_CAP_GEN(mdev, wol_a))
1066                 ret |= WAKE_ARP;
1067
1068         if (MLX5_CAP_GEN(mdev, wol_b))
1069                 ret |= WAKE_BCAST;
1070
1071         if (MLX5_CAP_GEN(mdev, wol_m))
1072                 ret |= WAKE_MCAST;
1073
1074         if (MLX5_CAP_GEN(mdev, wol_u))
1075                 ret |= WAKE_UCAST;
1076
1077         if (MLX5_CAP_GEN(mdev, wol_p))
1078                 ret |= WAKE_PHY;
1079
1080         return ret;
1081 }
1082
1083 static __u32 mlx5e_refomrat_wol_mode_mlx5_to_linux(u8 mode)
1084 {
1085         __u32 ret = 0;
1086
1087         if (mode & MLX5_WOL_MAGIC)
1088                 ret |= WAKE_MAGIC;
1089
1090         if (mode & MLX5_WOL_SECURED_MAGIC)
1091                 ret |= WAKE_MAGICSECURE;
1092
1093         if (mode & MLX5_WOL_ARP)
1094                 ret |= WAKE_ARP;
1095
1096         if (mode & MLX5_WOL_BROADCAST)
1097                 ret |= WAKE_BCAST;
1098
1099         if (mode & MLX5_WOL_MULTICAST)
1100                 ret |= WAKE_MCAST;
1101
1102         if (mode & MLX5_WOL_UNICAST)
1103                 ret |= WAKE_UCAST;
1104
1105         if (mode & MLX5_WOL_PHY_ACTIVITY)
1106                 ret |= WAKE_PHY;
1107
1108         return ret;
1109 }
1110
1111 static u8 mlx5e_refomrat_wol_mode_linux_to_mlx5(__u32 mode)
1112 {
1113         u8 ret = 0;
1114
1115         if (mode & WAKE_MAGIC)
1116                 ret |= MLX5_WOL_MAGIC;
1117
1118         if (mode & WAKE_MAGICSECURE)
1119                 ret |= MLX5_WOL_SECURED_MAGIC;
1120
1121         if (mode & WAKE_ARP)
1122                 ret |= MLX5_WOL_ARP;
1123
1124         if (mode & WAKE_BCAST)
1125                 ret |= MLX5_WOL_BROADCAST;
1126
1127         if (mode & WAKE_MCAST)
1128                 ret |= MLX5_WOL_MULTICAST;
1129
1130         if (mode & WAKE_UCAST)
1131                 ret |= MLX5_WOL_UNICAST;
1132
1133         if (mode & WAKE_PHY)
1134                 ret |= MLX5_WOL_PHY_ACTIVITY;
1135
1136         return ret;
1137 }
1138
1139 static void mlx5e_get_wol(struct net_device *netdev,
1140                           struct ethtool_wolinfo *wol)
1141 {
1142         struct mlx5e_priv *priv = netdev_priv(netdev);
1143         struct mlx5_core_dev *mdev = priv->mdev;
1144         u8 mlx5_wol_mode;
1145         int err;
1146
1147         memset(wol, 0, sizeof(*wol));
1148
1149         wol->supported = mlx5e_get_wol_supported(mdev);
1150         if (!wol->supported)
1151                 return;
1152
1153         err = mlx5_query_port_wol(mdev, &mlx5_wol_mode);
1154         if (err)
1155                 return;
1156
1157         wol->wolopts = mlx5e_refomrat_wol_mode_mlx5_to_linux(mlx5_wol_mode);
1158 }
1159
1160 static int mlx5e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1161 {
1162         struct mlx5e_priv *priv = netdev_priv(netdev);
1163         struct mlx5_core_dev *mdev = priv->mdev;
1164         __u32 wol_supported = mlx5e_get_wol_supported(mdev);
1165         u32 mlx5_wol_mode;
1166
1167         if (!wol_supported)
1168                 return -ENOTSUPP;
1169
1170         if (wol->wolopts & ~wol_supported)
1171                 return -EINVAL;
1172
1173         mlx5_wol_mode = mlx5e_refomrat_wol_mode_linux_to_mlx5(wol->wolopts);
1174
1175         return mlx5_set_port_wol(mdev, mlx5_wol_mode);
1176 }
1177
1178 static int mlx5e_set_phys_id(struct net_device *dev,
1179                              enum ethtool_phys_id_state state)
1180 {
1181         struct mlx5e_priv *priv = netdev_priv(dev);
1182         struct mlx5_core_dev *mdev = priv->mdev;
1183         u16 beacon_duration;
1184
1185         if (!MLX5_CAP_GEN(mdev, beacon_led))
1186                 return -EOPNOTSUPP;
1187
1188         switch (state) {
1189         case ETHTOOL_ID_ACTIVE:
1190                 beacon_duration = MLX5_BEACON_DURATION_INF;
1191                 break;
1192         case ETHTOOL_ID_INACTIVE:
1193                 beacon_duration = MLX5_BEACON_DURATION_OFF;
1194                 break;
1195         default:
1196                 return -EOPNOTSUPP;
1197         }
1198
1199         return mlx5_set_port_beacon(mdev, beacon_duration);
1200 }
1201
1202 static int mlx5e_get_module_info(struct net_device *netdev,
1203                                  struct ethtool_modinfo *modinfo)
1204 {
1205         struct mlx5e_priv *priv = netdev_priv(netdev);
1206         struct mlx5_core_dev *dev = priv->mdev;
1207         int size_read = 0;
1208         u8 data[4];
1209
1210         size_read = mlx5_query_module_eeprom(dev, 0, 2, data);
1211         if (size_read < 2)
1212                 return -EIO;
1213
1214         /* data[0] = identifier byte */
1215         switch (data[0]) {
1216         case MLX5_MODULE_ID_QSFP:
1217                 modinfo->type       = ETH_MODULE_SFF_8436;
1218                 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1219                 break;
1220         case MLX5_MODULE_ID_QSFP_PLUS:
1221         case MLX5_MODULE_ID_QSFP28:
1222                 /* data[1] = revision id */
1223                 if (data[0] == MLX5_MODULE_ID_QSFP28 || data[1] >= 0x3) {
1224                         modinfo->type       = ETH_MODULE_SFF_8636;
1225                         modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1226                 } else {
1227                         modinfo->type       = ETH_MODULE_SFF_8436;
1228                         modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1229                 }
1230                 break;
1231         case MLX5_MODULE_ID_SFP:
1232                 modinfo->type       = ETH_MODULE_SFF_8472;
1233                 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1234                 break;
1235         default:
1236                 netdev_err(priv->netdev, "%s: cable type not recognized:0x%x\n",
1237                            __func__, data[0]);
1238                 return -EINVAL;
1239         }
1240
1241         return 0;
1242 }
1243
1244 static int mlx5e_get_module_eeprom(struct net_device *netdev,
1245                                    struct ethtool_eeprom *ee,
1246                                    u8 *data)
1247 {
1248         struct mlx5e_priv *priv = netdev_priv(netdev);
1249         struct mlx5_core_dev *mdev = priv->mdev;
1250         int offset = ee->offset;
1251         int size_read;
1252         int i = 0;
1253
1254         if (!ee->len)
1255                 return -EINVAL;
1256
1257         memset(data, 0, ee->len);
1258
1259         while (i < ee->len) {
1260                 size_read = mlx5_query_module_eeprom(mdev, offset, ee->len - i,
1261                                                      data + i);
1262
1263                 if (!size_read)
1264                         /* Done reading */
1265                         return 0;
1266
1267                 if (size_read < 0) {
1268                         netdev_err(priv->netdev, "%s: mlx5_query_eeprom failed:0x%x\n",
1269                                    __func__, size_read);
1270                         return 0;
1271                 }
1272
1273                 i += size_read;
1274                 offset += size_read;
1275         }
1276
1277         return 0;
1278 }
1279
1280 typedef int (*mlx5e_pflag_handler)(struct net_device *netdev, bool enable);
1281
1282 static int set_pflag_nop(struct net_device *netdev, bool enable)
1283 {
1284         return 0;
1285 }
1286
1287 static int mlx5e_handle_pflag(struct net_device *netdev,
1288                               u32 wanted_flags,
1289                               enum mlx5e_priv_flag flag,
1290                               mlx5e_pflag_handler pflag_handler)
1291 {
1292         struct mlx5e_priv *priv = netdev_priv(netdev);
1293         bool enable = !!(wanted_flags & flag);
1294         u32 changes = wanted_flags ^ priv->pflags;
1295         int err;
1296
1297         if (!(changes & flag))
1298                 return 0;
1299
1300         err = pflag_handler(netdev, enable);
1301         if (err) {
1302                 netdev_err(netdev, "%s private flag 0x%x failed err %d\n",
1303                            enable ? "Enable" : "Disable", flag, err);
1304                 return err;
1305         }
1306
1307         MLX5E_SET_PRIV_FLAG(priv, flag, enable);
1308         return 0;
1309 }
1310
1311 static int mlx5e_set_priv_flags(struct net_device *netdev, u32 pflags)
1312 {
1313         struct mlx5e_priv *priv = netdev_priv(netdev);
1314         int err;
1315
1316         mutex_lock(&priv->state_lock);
1317
1318         err = mlx5e_handle_pflag(netdev, pflags, MLX5E_PFLAG_NOP,
1319                                  set_pflag_nop);
1320
1321         mutex_unlock(&priv->state_lock);
1322         return err ? -EINVAL : 0;
1323 }
1324
1325 static u32 mlx5e_get_priv_flags(struct net_device *netdev)
1326 {
1327         struct mlx5e_priv *priv = netdev_priv(netdev);
1328
1329         return priv->pflags;
1330 }
1331
1332 const struct ethtool_ops mlx5e_ethtool_ops = {
1333         .get_drvinfo       = mlx5e_get_drvinfo,
1334         .get_link          = ethtool_op_get_link,
1335         .get_strings       = mlx5e_get_strings,
1336         .get_sset_count    = mlx5e_get_sset_count,
1337         .get_ethtool_stats = mlx5e_get_ethtool_stats,
1338         .get_ringparam     = mlx5e_get_ringparam,
1339         .set_ringparam     = mlx5e_set_ringparam,
1340         .get_channels      = mlx5e_get_channels,
1341         .set_channels      = mlx5e_set_channels,
1342         .get_coalesce      = mlx5e_get_coalesce,
1343         .set_coalesce      = mlx5e_set_coalesce,
1344         .get_settings      = mlx5e_get_settings,
1345         .set_settings      = mlx5e_set_settings,
1346         .get_rxfh_key_size   = mlx5e_get_rxfh_key_size,
1347         .get_rxfh_indir_size = mlx5e_get_rxfh_indir_size,
1348         .get_rxfh          = mlx5e_get_rxfh,
1349         .set_rxfh          = mlx5e_set_rxfh,
1350         .get_rxnfc         = mlx5e_get_rxnfc,
1351         .get_tunable       = mlx5e_get_tunable,
1352         .set_tunable       = mlx5e_set_tunable,
1353         .get_pauseparam    = mlx5e_get_pauseparam,
1354         .set_pauseparam    = mlx5e_set_pauseparam,
1355         .get_ts_info       = mlx5e_get_ts_info,
1356         .set_phys_id       = mlx5e_set_phys_id,
1357         .get_wol           = mlx5e_get_wol,
1358         .set_wol           = mlx5e_set_wol,
1359         .get_module_info   = mlx5e_get_module_info,
1360         .get_module_eeprom = mlx5e_get_module_eeprom,
1361         .get_priv_flags    = mlx5e_get_priv_flags,
1362         .set_priv_flags    = mlx5e_set_priv_flags
1363 };