f1649d54347571a0e096df65ddddf4e1ec01e2a6
[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_100BASE_T]    = {
142                 .supported  = SUPPORTED_100baseT_Full,
143                 .advertised = ADVERTISED_100baseT_Full,
144                 .speed      = 100,
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 #define MLX5E_NUM_Q_CNTRS(priv) (NUM_Q_COUNTERS * (!!priv->q_counter))
169 #define MLX5E_NUM_RQ_STATS(priv) \
170         (NUM_RQ_STATS * priv->params.num_channels * \
171          test_bit(MLX5E_STATE_OPENED, &priv->state))
172 #define MLX5E_NUM_SQ_STATS(priv) \
173         (NUM_SQ_STATS * priv->params.num_channels * priv->params.num_tc * \
174          test_bit(MLX5E_STATE_OPENED, &priv->state))
175
176 static int mlx5e_get_sset_count(struct net_device *dev, int sset)
177 {
178         struct mlx5e_priv *priv = netdev_priv(dev);
179
180         switch (sset) {
181         case ETH_SS_STATS:
182                 return NUM_SW_COUNTERS +
183                        MLX5E_NUM_Q_CNTRS(priv) +
184                        NUM_VPORT_COUNTERS + NUM_PPORT_COUNTERS +
185                        MLX5E_NUM_RQ_STATS(priv) +
186                        MLX5E_NUM_SQ_STATS(priv);
187         /* fallthrough */
188         default:
189                 return -EOPNOTSUPP;
190         }
191 }
192
193 static void mlx5e_fill_stats_strings(struct mlx5e_priv *priv, uint8_t *data)
194 {
195         int i, j, tc, idx = 0;
196
197         /* SW counters */
198         for (i = 0; i < NUM_SW_COUNTERS; i++)
199                 strcpy(data + (idx++) * ETH_GSTRING_LEN, sw_stats_desc[i].name);
200
201         /* Q counters */
202         for (i = 0; i < MLX5E_NUM_Q_CNTRS(priv); i++)
203                 strcpy(data + (idx++) * ETH_GSTRING_LEN, q_stats_desc[i].name);
204
205         /* VPORT counters */
206         for (i = 0; i < NUM_VPORT_COUNTERS; i++)
207                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
208                        vport_stats_desc[i].name);
209
210         /* PPORT counters */
211         for (i = 0; i < NUM_PPORT_802_3_COUNTERS; i++)
212                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
213                        pport_802_3_stats_desc[i].name);
214
215         for (i = 0; i < NUM_PPORT_2863_COUNTERS; i++)
216                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
217                        pport_2863_stats_desc[i].name);
218
219         for (i = 0; i < NUM_PPORT_2819_COUNTERS; i++)
220                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
221                        pport_2819_stats_desc[i].name);
222
223         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
224                 return;
225
226         /* per channel counters */
227         for (i = 0; i < priv->params.num_channels; i++)
228                 for (j = 0; j < NUM_RQ_STATS; j++)
229                         sprintf(data + (idx++) * ETH_GSTRING_LEN, "rx%d_%s", i,
230                                 rq_stats_desc[j].name);
231
232         for (tc = 0; tc < priv->params.num_tc; tc++)
233                 for (i = 0; i < priv->params.num_channels; i++)
234                         for (j = 0; j < NUM_SQ_STATS; j++)
235                                 sprintf(data + (idx++) * ETH_GSTRING_LEN,
236                                         "tx%d_%s",
237                                         priv->channeltc_to_txq_map[i][tc],
238                                         sq_stats_desc[j].name);
239 }
240
241 static void mlx5e_get_strings(struct net_device *dev,
242                               uint32_t stringset, uint8_t *data)
243 {
244         struct mlx5e_priv *priv = netdev_priv(dev);
245
246         switch (stringset) {
247         case ETH_SS_PRIV_FLAGS:
248                 break;
249
250         case ETH_SS_TEST:
251                 break;
252
253         case ETH_SS_STATS:
254                 mlx5e_fill_stats_strings(priv, data);
255                 break;
256         }
257 }
258
259 static void mlx5e_get_ethtool_stats(struct net_device *dev,
260                                     struct ethtool_stats *stats, u64 *data)
261 {
262         struct mlx5e_priv *priv = netdev_priv(dev);
263         int i, j, tc, idx = 0;
264
265         if (!data)
266                 return;
267
268         mutex_lock(&priv->state_lock);
269         if (test_bit(MLX5E_STATE_OPENED, &priv->state))
270                 mlx5e_update_stats(priv);
271         mutex_unlock(&priv->state_lock);
272
273         for (i = 0; i < NUM_SW_COUNTERS; i++)
274                 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
275                                                    sw_stats_desc, i);
276
277         for (i = 0; i < MLX5E_NUM_Q_CNTRS(priv); i++)
278                 data[idx++] = MLX5E_READ_CTR32_CPU(&priv->stats.qcnt,
279                                                    q_stats_desc, i);
280
281         for (i = 0; i < NUM_VPORT_COUNTERS; i++)
282                 data[idx++] = MLX5E_READ_CTR64_BE(priv->stats.vport.query_vport_out,
283                                                   vport_stats_desc, i);
284
285         for (i = 0; i < NUM_PPORT_802_3_COUNTERS; i++)
286                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.IEEE_802_3_counters,
287                                                   pport_802_3_stats_desc, i);
288
289         for (i = 0; i < NUM_PPORT_2863_COUNTERS; i++)
290                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.RFC_2863_counters,
291                                                   pport_2863_stats_desc, i);
292
293         for (i = 0; i < NUM_PPORT_2819_COUNTERS; i++)
294                 data[idx++] = MLX5E_READ_CTR64_BE(&priv->stats.pport.RFC_2819_counters,
295                                                   pport_2819_stats_desc, i);
296
297         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
298                 return;
299
300         /* per channel counters */
301         for (i = 0; i < priv->params.num_channels; i++)
302                 for (j = 0; j < NUM_RQ_STATS; j++)
303                         data[idx++] =
304                                MLX5E_READ_CTR64_CPU(&priv->channel[i]->rq.stats,
305                                                     rq_stats_desc, j);
306
307         for (tc = 0; tc < priv->params.num_tc; tc++)
308                 for (i = 0; i < priv->params.num_channels; i++)
309                         for (j = 0; j < NUM_SQ_STATS; j++)
310                                 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->channel[i]->sq[tc].stats,
311                                                                    sq_stats_desc, j);
312 }
313
314 static void mlx5e_get_ringparam(struct net_device *dev,
315                                 struct ethtool_ringparam *param)
316 {
317         struct mlx5e_priv *priv = netdev_priv(dev);
318         int rq_wq_type = priv->params.rq_wq_type;
319
320         param->rx_max_pending = 1 << mlx5_max_log_rq_size(rq_wq_type);
321         param->tx_max_pending = 1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE;
322         param->rx_pending     = 1 << priv->params.log_rq_size;
323         param->tx_pending     = 1 << priv->params.log_sq_size;
324 }
325
326 static int mlx5e_set_ringparam(struct net_device *dev,
327                                struct ethtool_ringparam *param)
328 {
329         struct mlx5e_priv *priv = netdev_priv(dev);
330         bool was_opened;
331         int rq_wq_type = priv->params.rq_wq_type;
332         u16 min_rx_wqes;
333         u8 log_rq_size;
334         u8 log_sq_size;
335         int err = 0;
336
337         if (param->rx_jumbo_pending) {
338                 netdev_info(dev, "%s: rx_jumbo_pending not supported\n",
339                             __func__);
340                 return -EINVAL;
341         }
342         if (param->rx_mini_pending) {
343                 netdev_info(dev, "%s: rx_mini_pending not supported\n",
344                             __func__);
345                 return -EINVAL;
346         }
347         if (param->rx_pending < (1 << mlx5_min_log_rq_size(rq_wq_type))) {
348                 netdev_info(dev, "%s: rx_pending (%d) < min (%d)\n",
349                             __func__, param->rx_pending,
350                             1 << mlx5_min_log_rq_size(rq_wq_type));
351                 return -EINVAL;
352         }
353         if (param->rx_pending > (1 << mlx5_max_log_rq_size(rq_wq_type))) {
354                 netdev_info(dev, "%s: rx_pending (%d) > max (%d)\n",
355                             __func__, param->rx_pending,
356                             1 << mlx5_max_log_rq_size(rq_wq_type));
357                 return -EINVAL;
358         }
359         if (param->tx_pending < (1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)) {
360                 netdev_info(dev, "%s: tx_pending (%d) < min (%d)\n",
361                             __func__, param->tx_pending,
362                             1 << MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE);
363                 return -EINVAL;
364         }
365         if (param->tx_pending > (1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE)) {
366                 netdev_info(dev, "%s: tx_pending (%d) > max (%d)\n",
367                             __func__, param->tx_pending,
368                             1 << MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE);
369                 return -EINVAL;
370         }
371
372         log_rq_size = order_base_2(param->rx_pending);
373         log_sq_size = order_base_2(param->tx_pending);
374         min_rx_wqes = mlx5_min_rx_wqes(rq_wq_type, param->rx_pending);
375
376         if (log_rq_size == priv->params.log_rq_size &&
377             log_sq_size == priv->params.log_sq_size &&
378             min_rx_wqes == priv->params.min_rx_wqes)
379                 return 0;
380
381         mutex_lock(&priv->state_lock);
382
383         was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
384         if (was_opened)
385                 mlx5e_close_locked(dev);
386
387         priv->params.log_rq_size = log_rq_size;
388         priv->params.log_sq_size = log_sq_size;
389         priv->params.min_rx_wqes = min_rx_wqes;
390
391         if (was_opened)
392                 err = mlx5e_open_locked(dev);
393
394         mutex_unlock(&priv->state_lock);
395
396         return err;
397 }
398
399 static void mlx5e_get_channels(struct net_device *dev,
400                                struct ethtool_channels *ch)
401 {
402         struct mlx5e_priv *priv = netdev_priv(dev);
403
404         ch->max_combined   = mlx5e_get_max_num_channels(priv->mdev);
405         ch->combined_count = priv->params.num_channels;
406 }
407
408 static int mlx5e_set_channels(struct net_device *dev,
409                               struct ethtool_channels *ch)
410 {
411         struct mlx5e_priv *priv = netdev_priv(dev);
412         int ncv = mlx5e_get_max_num_channels(priv->mdev);
413         unsigned int count = ch->combined_count;
414         bool was_opened;
415         int err = 0;
416
417         if (!count) {
418                 netdev_info(dev, "%s: combined_count=0 not supported\n",
419                             __func__);
420                 return -EINVAL;
421         }
422         if (ch->rx_count || ch->tx_count) {
423                 netdev_info(dev, "%s: separate rx/tx count not supported\n",
424                             __func__);
425                 return -EINVAL;
426         }
427         if (count > ncv) {
428                 netdev_info(dev, "%s: count (%d) > max (%d)\n",
429                             __func__, count, ncv);
430                 return -EINVAL;
431         }
432
433         if (priv->params.num_channels == count)
434                 return 0;
435
436         mutex_lock(&priv->state_lock);
437
438         was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
439         if (was_opened)
440                 mlx5e_close_locked(dev);
441
442         priv->params.num_channels = count;
443         mlx5e_build_default_indir_rqt(priv->mdev, priv->params.indirection_rqt,
444                                       MLX5E_INDIR_RQT_SIZE, count);
445
446         if (was_opened)
447                 err = mlx5e_open_locked(dev);
448
449         mutex_unlock(&priv->state_lock);
450
451         return err;
452 }
453
454 static int mlx5e_get_coalesce(struct net_device *netdev,
455                               struct ethtool_coalesce *coal)
456 {
457         struct mlx5e_priv *priv = netdev_priv(netdev);
458
459         if (!MLX5_CAP_GEN(priv->mdev, cq_moderation))
460                 return -ENOTSUPP;
461
462         coal->rx_coalesce_usecs       = priv->params.rx_cq_moderation_usec;
463         coal->rx_max_coalesced_frames = priv->params.rx_cq_moderation_pkts;
464         coal->tx_coalesce_usecs       = priv->params.tx_cq_moderation_usec;
465         coal->tx_max_coalesced_frames = priv->params.tx_cq_moderation_pkts;
466
467         return 0;
468 }
469
470 static int mlx5e_set_coalesce(struct net_device *netdev,
471                               struct ethtool_coalesce *coal)
472 {
473         struct mlx5e_priv *priv    = netdev_priv(netdev);
474         struct mlx5_core_dev *mdev = priv->mdev;
475         struct mlx5e_channel *c;
476         int tc;
477         int i;
478
479         if (!MLX5_CAP_GEN(mdev, cq_moderation))
480                 return -ENOTSUPP;
481
482         mutex_lock(&priv->state_lock);
483         priv->params.tx_cq_moderation_usec = coal->tx_coalesce_usecs;
484         priv->params.tx_cq_moderation_pkts = coal->tx_max_coalesced_frames;
485         priv->params.rx_cq_moderation_usec = coal->rx_coalesce_usecs;
486         priv->params.rx_cq_moderation_pkts = coal->rx_max_coalesced_frames;
487
488         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
489                 goto out;
490
491         for (i = 0; i < priv->params.num_channels; ++i) {
492                 c = priv->channel[i];
493
494                 for (tc = 0; tc < c->num_tc; tc++) {
495                         mlx5_core_modify_cq_moderation(mdev,
496                                                 &c->sq[tc].cq.mcq,
497                                                 coal->tx_coalesce_usecs,
498                                                 coal->tx_max_coalesced_frames);
499                 }
500
501                 mlx5_core_modify_cq_moderation(mdev, &c->rq.cq.mcq,
502                                                coal->rx_coalesce_usecs,
503                                                coal->rx_max_coalesced_frames);
504         }
505
506 out:
507         mutex_unlock(&priv->state_lock);
508         return 0;
509 }
510
511 static u32 ptys2ethtool_supported_link(u32 eth_proto_cap)
512 {
513         int i;
514         u32 supported_modes = 0;
515
516         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
517                 if (eth_proto_cap & MLX5E_PROT_MASK(i))
518                         supported_modes |= ptys2ethtool_table[i].supported;
519         }
520         return supported_modes;
521 }
522
523 static u32 ptys2ethtool_adver_link(u32 eth_proto_cap)
524 {
525         int i;
526         u32 advertising_modes = 0;
527
528         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
529                 if (eth_proto_cap & MLX5E_PROT_MASK(i))
530                         advertising_modes |= ptys2ethtool_table[i].advertised;
531         }
532         return advertising_modes;
533 }
534
535 static u32 ptys2ethtool_supported_port(u32 eth_proto_cap)
536 {
537         if (eth_proto_cap & (MLX5E_PROT_MASK(MLX5E_10GBASE_CR)
538                            | MLX5E_PROT_MASK(MLX5E_10GBASE_SR)
539                            | MLX5E_PROT_MASK(MLX5E_40GBASE_CR4)
540                            | MLX5E_PROT_MASK(MLX5E_40GBASE_SR4)
541                            | MLX5E_PROT_MASK(MLX5E_100GBASE_SR4)
542                            | MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII))) {
543                 return SUPPORTED_FIBRE;
544         }
545
546         if (eth_proto_cap & (MLX5E_PROT_MASK(MLX5E_100GBASE_KR4)
547                            | MLX5E_PROT_MASK(MLX5E_40GBASE_KR4)
548                            | MLX5E_PROT_MASK(MLX5E_10GBASE_KR)
549                            | MLX5E_PROT_MASK(MLX5E_10GBASE_KX4)
550                            | MLX5E_PROT_MASK(MLX5E_1000BASE_KX))) {
551                 return SUPPORTED_Backplane;
552         }
553         return 0;
554 }
555
556 static void get_speed_duplex(struct net_device *netdev,
557                              u32 eth_proto_oper,
558                              struct ethtool_cmd *cmd)
559 {
560         int i;
561         u32 speed = SPEED_UNKNOWN;
562         u8 duplex = DUPLEX_UNKNOWN;
563
564         if (!netif_carrier_ok(netdev))
565                 goto out;
566
567         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
568                 if (eth_proto_oper & MLX5E_PROT_MASK(i)) {
569                         speed = ptys2ethtool_table[i].speed;
570                         duplex = DUPLEX_FULL;
571                         break;
572                 }
573         }
574 out:
575         ethtool_cmd_speed_set(cmd, speed);
576         cmd->duplex = duplex;
577 }
578
579 static void get_supported(u32 eth_proto_cap, u32 *supported)
580 {
581         *supported |= ptys2ethtool_supported_port(eth_proto_cap);
582         *supported |= ptys2ethtool_supported_link(eth_proto_cap);
583         *supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
584 }
585
586 static void get_advertising(u32 eth_proto_cap, u8 tx_pause,
587                             u8 rx_pause, u32 *advertising)
588 {
589         *advertising |= ptys2ethtool_adver_link(eth_proto_cap);
590         *advertising |= tx_pause ? ADVERTISED_Pause : 0;
591         *advertising |= (tx_pause ^ rx_pause) ? ADVERTISED_Asym_Pause : 0;
592 }
593
594 static u8 get_connector_port(u32 eth_proto)
595 {
596         if (eth_proto & (MLX5E_PROT_MASK(MLX5E_10GBASE_SR)
597                          | MLX5E_PROT_MASK(MLX5E_40GBASE_SR4)
598                          | MLX5E_PROT_MASK(MLX5E_100GBASE_SR4)
599                          | MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII))) {
600                         return PORT_FIBRE;
601         }
602
603         if (eth_proto & (MLX5E_PROT_MASK(MLX5E_40GBASE_CR4)
604                          | MLX5E_PROT_MASK(MLX5E_10GBASE_CR)
605                          | MLX5E_PROT_MASK(MLX5E_100GBASE_CR4))) {
606                         return PORT_DA;
607         }
608
609         if (eth_proto & (MLX5E_PROT_MASK(MLX5E_10GBASE_KX4)
610                          | MLX5E_PROT_MASK(MLX5E_10GBASE_KR)
611                          | MLX5E_PROT_MASK(MLX5E_40GBASE_KR4)
612                          | MLX5E_PROT_MASK(MLX5E_100GBASE_KR4))) {
613                         return PORT_NONE;
614         }
615
616         return PORT_OTHER;
617 }
618
619 static void get_lp_advertising(u32 eth_proto_lp, u32 *lp_advertising)
620 {
621         *lp_advertising = ptys2ethtool_adver_link(eth_proto_lp);
622 }
623
624 static int mlx5e_get_settings(struct net_device *netdev,
625                               struct ethtool_cmd *cmd)
626 {
627         struct mlx5e_priv *priv    = netdev_priv(netdev);
628         struct mlx5_core_dev *mdev = priv->mdev;
629         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
630         u32 eth_proto_cap;
631         u32 eth_proto_admin;
632         u32 eth_proto_lp;
633         u32 eth_proto_oper;
634         int err;
635
636         err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 1);
637
638         if (err) {
639                 netdev_err(netdev, "%s: query port ptys failed: %d\n",
640                            __func__, err);
641                 goto err_query_ptys;
642         }
643
644         eth_proto_cap   = MLX5_GET(ptys_reg, out, eth_proto_capability);
645         eth_proto_admin = MLX5_GET(ptys_reg, out, eth_proto_admin);
646         eth_proto_oper  = MLX5_GET(ptys_reg, out, eth_proto_oper);
647         eth_proto_lp    = MLX5_GET(ptys_reg, out, eth_proto_lp_advertise);
648
649         cmd->supported   = 0;
650         cmd->advertising = 0;
651
652         get_supported(eth_proto_cap, &cmd->supported);
653         get_advertising(eth_proto_admin, 0, 0, &cmd->advertising);
654         get_speed_duplex(netdev, eth_proto_oper, cmd);
655
656         eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
657
658         cmd->port = get_connector_port(eth_proto_oper);
659         get_lp_advertising(eth_proto_lp, &cmd->lp_advertising);
660
661         cmd->transceiver = XCVR_INTERNAL;
662
663 err_query_ptys:
664         return err;
665 }
666
667 static u32 mlx5e_ethtool2ptys_adver_link(u32 link_modes)
668 {
669         u32 i, ptys_modes = 0;
670
671         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
672                 if (ptys2ethtool_table[i].advertised & link_modes)
673                         ptys_modes |= MLX5E_PROT_MASK(i);
674         }
675
676         return ptys_modes;
677 }
678
679 static u32 mlx5e_ethtool2ptys_speed_link(u32 speed)
680 {
681         u32 i, speed_links = 0;
682
683         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
684                 if (ptys2ethtool_table[i].speed == speed)
685                         speed_links |= MLX5E_PROT_MASK(i);
686         }
687
688         return speed_links;
689 }
690
691 static int mlx5e_set_settings(struct net_device *netdev,
692                               struct ethtool_cmd *cmd)
693 {
694         struct mlx5e_priv *priv    = netdev_priv(netdev);
695         struct mlx5_core_dev *mdev = priv->mdev;
696         u32 link_modes;
697         u32 speed;
698         u32 eth_proto_cap, eth_proto_admin;
699         enum mlx5_port_status ps;
700         int err;
701
702         speed = ethtool_cmd_speed(cmd);
703
704         link_modes = cmd->autoneg == AUTONEG_ENABLE ?
705                 mlx5e_ethtool2ptys_adver_link(cmd->advertising) :
706                 mlx5e_ethtool2ptys_speed_link(speed);
707
708         err = mlx5_query_port_proto_cap(mdev, &eth_proto_cap, MLX5_PTYS_EN);
709         if (err) {
710                 netdev_err(netdev, "%s: query port eth proto cap failed: %d\n",
711                            __func__, err);
712                 goto out;
713         }
714
715         link_modes = link_modes & eth_proto_cap;
716         if (!link_modes) {
717                 netdev_err(netdev, "%s: Not supported link mode(s) requested",
718                            __func__);
719                 err = -EINVAL;
720                 goto out;
721         }
722
723         err = mlx5_query_port_proto_admin(mdev, &eth_proto_admin, MLX5_PTYS_EN);
724         if (err) {
725                 netdev_err(netdev, "%s: query port eth proto admin failed: %d\n",
726                            __func__, err);
727                 goto out;
728         }
729
730         if (link_modes == eth_proto_admin)
731                 goto out;
732
733         mlx5_query_port_admin_status(mdev, &ps);
734         if (ps == MLX5_PORT_UP)
735                 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
736         mlx5_set_port_proto(mdev, link_modes, MLX5_PTYS_EN);
737         if (ps == MLX5_PORT_UP)
738                 mlx5_set_port_admin_status(mdev, MLX5_PORT_UP);
739
740 out:
741         return err;
742 }
743
744 static u32 mlx5e_get_rxfh_key_size(struct net_device *netdev)
745 {
746         struct mlx5e_priv *priv = netdev_priv(netdev);
747
748         return sizeof(priv->params.toeplitz_hash_key);
749 }
750
751 static u32 mlx5e_get_rxfh_indir_size(struct net_device *netdev)
752 {
753         return MLX5E_INDIR_RQT_SIZE;
754 }
755
756 static int mlx5e_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
757                           u8 *hfunc)
758 {
759         struct mlx5e_priv *priv = netdev_priv(netdev);
760
761         if (indir)
762                 memcpy(indir, priv->params.indirection_rqt,
763                        sizeof(priv->params.indirection_rqt));
764
765         if (key)
766                 memcpy(key, priv->params.toeplitz_hash_key,
767                        sizeof(priv->params.toeplitz_hash_key));
768
769         if (hfunc)
770                 *hfunc = priv->params.rss_hfunc;
771
772         return 0;
773 }
774
775 static void mlx5e_modify_tirs_hash(struct mlx5e_priv *priv, void *in, int inlen)
776 {
777         struct mlx5_core_dev *mdev = priv->mdev;
778         void *tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
779         int i;
780
781         MLX5_SET(modify_tir_in, in, bitmask.hash, 1);
782         mlx5e_build_tir_ctx_hash(tirc, priv);
783
784         for (i = 0; i < MLX5E_NUM_TT; i++)
785                 if (IS_HASHING_TT(i))
786                         mlx5_core_modify_tir(mdev, priv->tirn[i], in, inlen);
787 }
788
789 static int mlx5e_set_rxfh(struct net_device *dev, const u32 *indir,
790                           const u8 *key, const u8 hfunc)
791 {
792         struct mlx5e_priv *priv = netdev_priv(dev);
793         int inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
794         void *in;
795
796         if ((hfunc != ETH_RSS_HASH_NO_CHANGE) &&
797             (hfunc != ETH_RSS_HASH_XOR) &&
798             (hfunc != ETH_RSS_HASH_TOP))
799                 return -EINVAL;
800
801         in = mlx5_vzalloc(inlen);
802         if (!in)
803                 return -ENOMEM;
804
805         mutex_lock(&priv->state_lock);
806
807         if (indir) {
808                 memcpy(priv->params.indirection_rqt, indir,
809                        sizeof(priv->params.indirection_rqt));
810                 mlx5e_redirect_rqt(priv, MLX5E_INDIRECTION_RQT);
811         }
812
813         if (key)
814                 memcpy(priv->params.toeplitz_hash_key, key,
815                        sizeof(priv->params.toeplitz_hash_key));
816
817         if (hfunc != ETH_RSS_HASH_NO_CHANGE)
818                 priv->params.rss_hfunc = hfunc;
819
820         mlx5e_modify_tirs_hash(priv, in, inlen);
821
822         mutex_unlock(&priv->state_lock);
823
824         kvfree(in);
825
826         return 0;
827 }
828
829 static int mlx5e_get_rxnfc(struct net_device *netdev,
830                            struct ethtool_rxnfc *info, u32 *rule_locs)
831 {
832         struct mlx5e_priv *priv = netdev_priv(netdev);
833         int err = 0;
834
835         switch (info->cmd) {
836         case ETHTOOL_GRXRINGS:
837                 info->data = priv->params.num_channels;
838                 break;
839         default:
840                 err = -EOPNOTSUPP;
841                 break;
842         }
843
844         return err;
845 }
846
847 static int mlx5e_get_tunable(struct net_device *dev,
848                              const struct ethtool_tunable *tuna,
849                              void *data)
850 {
851         const struct mlx5e_priv *priv = netdev_priv(dev);
852         int err = 0;
853
854         switch (tuna->id) {
855         case ETHTOOL_TX_COPYBREAK:
856                 *(u32 *)data = priv->params.tx_max_inline;
857                 break;
858         default:
859                 err = -EINVAL;
860                 break;
861         }
862
863         return err;
864 }
865
866 static int mlx5e_set_tunable(struct net_device *dev,
867                              const struct ethtool_tunable *tuna,
868                              const void *data)
869 {
870         struct mlx5e_priv *priv = netdev_priv(dev);
871         struct mlx5_core_dev *mdev = priv->mdev;
872         bool was_opened;
873         u32 val;
874         int err = 0;
875
876         switch (tuna->id) {
877         case ETHTOOL_TX_COPYBREAK:
878                 val = *(u32 *)data;
879                 if (val > mlx5e_get_max_inline_cap(mdev)) {
880                         err = -EINVAL;
881                         break;
882                 }
883
884                 mutex_lock(&priv->state_lock);
885
886                 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
887                 if (was_opened)
888                         mlx5e_close_locked(dev);
889
890                 priv->params.tx_max_inline = val;
891
892                 if (was_opened)
893                         err = mlx5e_open_locked(dev);
894
895                 mutex_unlock(&priv->state_lock);
896                 break;
897         default:
898                 err = -EINVAL;
899                 break;
900         }
901
902         return err;
903 }
904
905 static void mlx5e_get_pauseparam(struct net_device *netdev,
906                                  struct ethtool_pauseparam *pauseparam)
907 {
908         struct mlx5e_priv *priv    = netdev_priv(netdev);
909         struct mlx5_core_dev *mdev = priv->mdev;
910         int err;
911
912         err = mlx5_query_port_pause(mdev, &pauseparam->rx_pause,
913                                     &pauseparam->tx_pause);
914         if (err) {
915                 netdev_err(netdev, "%s: mlx5_query_port_pause failed:0x%x\n",
916                            __func__, err);
917         }
918 }
919
920 static int mlx5e_set_pauseparam(struct net_device *netdev,
921                                 struct ethtool_pauseparam *pauseparam)
922 {
923         struct mlx5e_priv *priv    = netdev_priv(netdev);
924         struct mlx5_core_dev *mdev = priv->mdev;
925         int err;
926
927         if (pauseparam->autoneg)
928                 return -EINVAL;
929
930         err = mlx5_set_port_pause(mdev,
931                                   pauseparam->rx_pause ? 1 : 0,
932                                   pauseparam->tx_pause ? 1 : 0);
933         if (err) {
934                 netdev_err(netdev, "%s: mlx5_set_port_pause failed:0x%x\n",
935                            __func__, err);
936         }
937
938         return err;
939 }
940
941 static int mlx5e_get_ts_info(struct net_device *dev,
942                              struct ethtool_ts_info *info)
943 {
944         struct mlx5e_priv *priv = netdev_priv(dev);
945         int ret;
946
947         ret = ethtool_op_get_ts_info(dev, info);
948         if (ret)
949                 return ret;
950
951         info->phc_index = priv->tstamp.ptp ?
952                           ptp_clock_index(priv->tstamp.ptp) : -1;
953
954         if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
955                 return 0;
956
957         info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
958                                  SOF_TIMESTAMPING_RX_HARDWARE |
959                                  SOF_TIMESTAMPING_RAW_HARDWARE;
960
961         info->tx_types = (BIT(1) << HWTSTAMP_TX_OFF) |
962                          (BIT(1) << HWTSTAMP_TX_ON);
963
964         info->rx_filters = (BIT(1) << HWTSTAMP_FILTER_NONE) |
965                            (BIT(1) << HWTSTAMP_FILTER_ALL);
966
967         return 0;
968 }
969
970 static __u32 mlx5e_get_wol_supported(struct mlx5_core_dev *mdev)
971 {
972         __u32 ret = 0;
973
974         if (MLX5_CAP_GEN(mdev, wol_g))
975                 ret |= WAKE_MAGIC;
976
977         if (MLX5_CAP_GEN(mdev, wol_s))
978                 ret |= WAKE_MAGICSECURE;
979
980         if (MLX5_CAP_GEN(mdev, wol_a))
981                 ret |= WAKE_ARP;
982
983         if (MLX5_CAP_GEN(mdev, wol_b))
984                 ret |= WAKE_BCAST;
985
986         if (MLX5_CAP_GEN(mdev, wol_m))
987                 ret |= WAKE_MCAST;
988
989         if (MLX5_CAP_GEN(mdev, wol_u))
990                 ret |= WAKE_UCAST;
991
992         if (MLX5_CAP_GEN(mdev, wol_p))
993                 ret |= WAKE_PHY;
994
995         return ret;
996 }
997
998 static __u32 mlx5e_refomrat_wol_mode_mlx5_to_linux(u8 mode)
999 {
1000         __u32 ret = 0;
1001
1002         if (mode & MLX5_WOL_MAGIC)
1003                 ret |= WAKE_MAGIC;
1004
1005         if (mode & MLX5_WOL_SECURED_MAGIC)
1006                 ret |= WAKE_MAGICSECURE;
1007
1008         if (mode & MLX5_WOL_ARP)
1009                 ret |= WAKE_ARP;
1010
1011         if (mode & MLX5_WOL_BROADCAST)
1012                 ret |= WAKE_BCAST;
1013
1014         if (mode & MLX5_WOL_MULTICAST)
1015                 ret |= WAKE_MCAST;
1016
1017         if (mode & MLX5_WOL_UNICAST)
1018                 ret |= WAKE_UCAST;
1019
1020         if (mode & MLX5_WOL_PHY_ACTIVITY)
1021                 ret |= WAKE_PHY;
1022
1023         return ret;
1024 }
1025
1026 static u8 mlx5e_refomrat_wol_mode_linux_to_mlx5(__u32 mode)
1027 {
1028         u8 ret = 0;
1029
1030         if (mode & WAKE_MAGIC)
1031                 ret |= MLX5_WOL_MAGIC;
1032
1033         if (mode & WAKE_MAGICSECURE)
1034                 ret |= MLX5_WOL_SECURED_MAGIC;
1035
1036         if (mode & WAKE_ARP)
1037                 ret |= MLX5_WOL_ARP;
1038
1039         if (mode & WAKE_BCAST)
1040                 ret |= MLX5_WOL_BROADCAST;
1041
1042         if (mode & WAKE_MCAST)
1043                 ret |= MLX5_WOL_MULTICAST;
1044
1045         if (mode & WAKE_UCAST)
1046                 ret |= MLX5_WOL_UNICAST;
1047
1048         if (mode & WAKE_PHY)
1049                 ret |= MLX5_WOL_PHY_ACTIVITY;
1050
1051         return ret;
1052 }
1053
1054 static void mlx5e_get_wol(struct net_device *netdev,
1055                           struct ethtool_wolinfo *wol)
1056 {
1057         struct mlx5e_priv *priv = netdev_priv(netdev);
1058         struct mlx5_core_dev *mdev = priv->mdev;
1059         u8 mlx5_wol_mode;
1060         int err;
1061
1062         memset(wol, 0, sizeof(*wol));
1063
1064         wol->supported = mlx5e_get_wol_supported(mdev);
1065         if (!wol->supported)
1066                 return;
1067
1068         err = mlx5_query_port_wol(mdev, &mlx5_wol_mode);
1069         if (err)
1070                 return;
1071
1072         wol->wolopts = mlx5e_refomrat_wol_mode_mlx5_to_linux(mlx5_wol_mode);
1073 }
1074
1075 static int mlx5e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1076 {
1077         struct mlx5e_priv *priv = netdev_priv(netdev);
1078         struct mlx5_core_dev *mdev = priv->mdev;
1079         __u32 wol_supported = mlx5e_get_wol_supported(mdev);
1080         u32 mlx5_wol_mode;
1081
1082         if (!wol_supported)
1083                 return -ENOTSUPP;
1084
1085         if (wol->wolopts & ~wol_supported)
1086                 return -EINVAL;
1087
1088         mlx5_wol_mode = mlx5e_refomrat_wol_mode_linux_to_mlx5(wol->wolopts);
1089
1090         return mlx5_set_port_wol(mdev, mlx5_wol_mode);
1091 }
1092
1093 const struct ethtool_ops mlx5e_ethtool_ops = {
1094         .get_drvinfo       = mlx5e_get_drvinfo,
1095         .get_link          = ethtool_op_get_link,
1096         .get_strings       = mlx5e_get_strings,
1097         .get_sset_count    = mlx5e_get_sset_count,
1098         .get_ethtool_stats = mlx5e_get_ethtool_stats,
1099         .get_ringparam     = mlx5e_get_ringparam,
1100         .set_ringparam     = mlx5e_set_ringparam,
1101         .get_channels      = mlx5e_get_channels,
1102         .set_channels      = mlx5e_set_channels,
1103         .get_coalesce      = mlx5e_get_coalesce,
1104         .set_coalesce      = mlx5e_set_coalesce,
1105         .get_settings      = mlx5e_get_settings,
1106         .set_settings      = mlx5e_set_settings,
1107         .get_rxfh_key_size   = mlx5e_get_rxfh_key_size,
1108         .get_rxfh_indir_size = mlx5e_get_rxfh_indir_size,
1109         .get_rxfh          = mlx5e_get_rxfh,
1110         .set_rxfh          = mlx5e_set_rxfh,
1111         .get_rxnfc         = mlx5e_get_rxnfc,
1112         .get_tunable       = mlx5e_get_tunable,
1113         .set_tunable       = mlx5e_set_tunable,
1114         .get_pauseparam    = mlx5e_get_pauseparam,
1115         .set_pauseparam    = mlx5e_set_pauseparam,
1116         .get_ts_info       = mlx5e_get_ts_info,
1117         .get_wol           = mlx5e_get_wol,
1118         .set_wol           = mlx5e_set_wol,
1119 };