ethtool, net/mlx4_en: Cable info, get_module_info/eeprom ethtool support
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx4 / en_ethtool.c
1 /*
2  * Copyright (c) 2007 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
34 #include <linux/kernel.h>
35 #include <linux/ethtool.h>
36 #include <linux/netdevice.h>
37 #include <linux/mlx4/driver.h>
38 #include <linux/mlx4/device.h>
39 #include <linux/in.h>
40 #include <net/ip.h>
41
42 #include "mlx4_en.h"
43 #include "en_port.h"
44
45 #define EN_ETHTOOL_QP_ATTACH (1ull << 63)
46 #define EN_ETHTOOL_SHORT_MASK cpu_to_be16(0xffff)
47 #define EN_ETHTOOL_WORD_MASK  cpu_to_be32(0xffffffff)
48
49 static int mlx4_en_moderation_update(struct mlx4_en_priv *priv)
50 {
51         int i;
52         int err = 0;
53
54         for (i = 0; i < priv->tx_ring_num; i++) {
55                 priv->tx_cq[i]->moder_cnt = priv->tx_frames;
56                 priv->tx_cq[i]->moder_time = priv->tx_usecs;
57                 if (priv->port_up) {
58                         err = mlx4_en_set_cq_moder(priv, priv->tx_cq[i]);
59                         if (err)
60                                 return err;
61                 }
62         }
63
64         if (priv->adaptive_rx_coal)
65                 return 0;
66
67         for (i = 0; i < priv->rx_ring_num; i++) {
68                 priv->rx_cq[i]->moder_cnt = priv->rx_frames;
69                 priv->rx_cq[i]->moder_time = priv->rx_usecs;
70                 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
71                 if (priv->port_up) {
72                         err = mlx4_en_set_cq_moder(priv, priv->rx_cq[i]);
73                         if (err)
74                                 return err;
75                 }
76         }
77
78         return err;
79 }
80
81 static void
82 mlx4_en_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
83 {
84         struct mlx4_en_priv *priv = netdev_priv(dev);
85         struct mlx4_en_dev *mdev = priv->mdev;
86
87         strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
88         strlcpy(drvinfo->version, DRV_VERSION " (" DRV_RELDATE ")",
89                 sizeof(drvinfo->version));
90         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
91                 "%d.%d.%d",
92                 (u16) (mdev->dev->caps.fw_ver >> 32),
93                 (u16) ((mdev->dev->caps.fw_ver >> 16) & 0xffff),
94                 (u16) (mdev->dev->caps.fw_ver & 0xffff));
95         strlcpy(drvinfo->bus_info, pci_name(mdev->dev->pdev),
96                 sizeof(drvinfo->bus_info));
97         drvinfo->n_stats = 0;
98         drvinfo->regdump_len = 0;
99         drvinfo->eedump_len = 0;
100 }
101
102 static const char mlx4_en_priv_flags[][ETH_GSTRING_LEN] = {
103         "blueflame",
104 };
105
106 static const char main_strings[][ETH_GSTRING_LEN] = {
107         "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
108         "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
109         "rx_length_errors", "rx_over_errors", "rx_crc_errors",
110         "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
111         "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
112         "tx_heartbeat_errors", "tx_window_errors",
113
114         /* port statistics */
115         "tso_packets",
116         "xmit_more",
117         "queue_stopped", "wake_queue", "tx_timeout", "rx_alloc_failed",
118         "rx_csum_good", "rx_csum_none", "tx_chksum_offload",
119
120         /* packet statistics */
121         "broadcast", "rx_prio_0", "rx_prio_1", "rx_prio_2", "rx_prio_3",
122         "rx_prio_4", "rx_prio_5", "rx_prio_6", "rx_prio_7", "tx_prio_0",
123         "tx_prio_1", "tx_prio_2", "tx_prio_3", "tx_prio_4", "tx_prio_5",
124         "tx_prio_6", "tx_prio_7",
125 };
126 #define NUM_MAIN_STATS  21
127 #define NUM_ALL_STATS   (NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PKT_STATS + NUM_PERF_STATS)
128
129 static const char mlx4_en_test_names[][ETH_GSTRING_LEN]= {
130         "Interrupt Test",
131         "Link Test",
132         "Speed Test",
133         "Register Test",
134         "Loopback Test",
135 };
136
137 static u32 mlx4_en_get_msglevel(struct net_device *dev)
138 {
139         return ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable;
140 }
141
142 static void mlx4_en_set_msglevel(struct net_device *dev, u32 val)
143 {
144         ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable = val;
145 }
146
147 static void mlx4_en_get_wol(struct net_device *netdev,
148                             struct ethtool_wolinfo *wol)
149 {
150         struct mlx4_en_priv *priv = netdev_priv(netdev);
151         int err = 0;
152         u64 config = 0;
153         u64 mask;
154
155         if ((priv->port < 1) || (priv->port > 2)) {
156                 en_err(priv, "Failed to get WoL information\n");
157                 return;
158         }
159
160         mask = (priv->port == 1) ? MLX4_DEV_CAP_FLAG_WOL_PORT1 :
161                 MLX4_DEV_CAP_FLAG_WOL_PORT2;
162
163         if (!(priv->mdev->dev->caps.flags & mask)) {
164                 wol->supported = 0;
165                 wol->wolopts = 0;
166                 return;
167         }
168
169         err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
170         if (err) {
171                 en_err(priv, "Failed to get WoL information\n");
172                 return;
173         }
174
175         if (config & MLX4_EN_WOL_MAGIC)
176                 wol->supported = WAKE_MAGIC;
177         else
178                 wol->supported = 0;
179
180         if (config & MLX4_EN_WOL_ENABLED)
181                 wol->wolopts = WAKE_MAGIC;
182         else
183                 wol->wolopts = 0;
184 }
185
186 static int mlx4_en_set_wol(struct net_device *netdev,
187                             struct ethtool_wolinfo *wol)
188 {
189         struct mlx4_en_priv *priv = netdev_priv(netdev);
190         u64 config = 0;
191         int err = 0;
192         u64 mask;
193
194         if ((priv->port < 1) || (priv->port > 2))
195                 return -EOPNOTSUPP;
196
197         mask = (priv->port == 1) ? MLX4_DEV_CAP_FLAG_WOL_PORT1 :
198                 MLX4_DEV_CAP_FLAG_WOL_PORT2;
199
200         if (!(priv->mdev->dev->caps.flags & mask))
201                 return -EOPNOTSUPP;
202
203         if (wol->supported & ~WAKE_MAGIC)
204                 return -EINVAL;
205
206         err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
207         if (err) {
208                 en_err(priv, "Failed to get WoL info, unable to modify\n");
209                 return err;
210         }
211
212         if (wol->wolopts & WAKE_MAGIC) {
213                 config |= MLX4_EN_WOL_DO_MODIFY | MLX4_EN_WOL_ENABLED |
214                                 MLX4_EN_WOL_MAGIC;
215         } else {
216                 config &= ~(MLX4_EN_WOL_ENABLED | MLX4_EN_WOL_MAGIC);
217                 config |= MLX4_EN_WOL_DO_MODIFY;
218         }
219
220         err = mlx4_wol_write(priv->mdev->dev, config, priv->port);
221         if (err)
222                 en_err(priv, "Failed to set WoL information\n");
223
224         return err;
225 }
226
227 static int mlx4_en_get_sset_count(struct net_device *dev, int sset)
228 {
229         struct mlx4_en_priv *priv = netdev_priv(dev);
230         int bit_count = hweight64(priv->stats_bitmap);
231
232         switch (sset) {
233         case ETH_SS_STATS:
234                 return (priv->stats_bitmap ? bit_count : NUM_ALL_STATS) +
235                         (priv->tx_ring_num * 2) +
236 #ifdef CONFIG_NET_RX_BUSY_POLL
237                         (priv->rx_ring_num * 5);
238 #else
239                         (priv->rx_ring_num * 2);
240 #endif
241         case ETH_SS_TEST:
242                 return MLX4_EN_NUM_SELF_TEST - !(priv->mdev->dev->caps.flags
243                                         & MLX4_DEV_CAP_FLAG_UC_LOOPBACK) * 2;
244         case ETH_SS_PRIV_FLAGS:
245                 return ARRAY_SIZE(mlx4_en_priv_flags);
246         default:
247                 return -EOPNOTSUPP;
248         }
249 }
250
251 static void mlx4_en_get_ethtool_stats(struct net_device *dev,
252                 struct ethtool_stats *stats, uint64_t *data)
253 {
254         struct mlx4_en_priv *priv = netdev_priv(dev);
255         int index = 0;
256         int i, j = 0;
257
258         spin_lock_bh(&priv->stats_lock);
259
260         if (!(priv->stats_bitmap)) {
261                 for (i = 0; i < NUM_MAIN_STATS; i++)
262                         data[index++] =
263                                 ((unsigned long *) &priv->stats)[i];
264                 for (i = 0; i < NUM_PORT_STATS; i++)
265                         data[index++] =
266                                 ((unsigned long *) &priv->port_stats)[i];
267                 for (i = 0; i < NUM_PKT_STATS; i++)
268                         data[index++] =
269                                 ((unsigned long *) &priv->pkstats)[i];
270         } else {
271                 for (i = 0; i < NUM_MAIN_STATS; i++) {
272                         if ((priv->stats_bitmap >> j) & 1)
273                                 data[index++] =
274                                 ((unsigned long *) &priv->stats)[i];
275                         j++;
276                 }
277                 for (i = 0; i < NUM_PORT_STATS; i++) {
278                         if ((priv->stats_bitmap >> j) & 1)
279                                 data[index++] =
280                                 ((unsigned long *) &priv->port_stats)[i];
281                         j++;
282                 }
283         }
284         for (i = 0; i < priv->tx_ring_num; i++) {
285                 data[index++] = priv->tx_ring[i]->packets;
286                 data[index++] = priv->tx_ring[i]->bytes;
287         }
288         for (i = 0; i < priv->rx_ring_num; i++) {
289                 data[index++] = priv->rx_ring[i]->packets;
290                 data[index++] = priv->rx_ring[i]->bytes;
291 #ifdef CONFIG_NET_RX_BUSY_POLL
292                 data[index++] = priv->rx_ring[i]->yields;
293                 data[index++] = priv->rx_ring[i]->misses;
294                 data[index++] = priv->rx_ring[i]->cleaned;
295 #endif
296         }
297         spin_unlock_bh(&priv->stats_lock);
298
299 }
300
301 static void mlx4_en_self_test(struct net_device *dev,
302                               struct ethtool_test *etest, u64 *buf)
303 {
304         mlx4_en_ex_selftest(dev, &etest->flags, buf);
305 }
306
307 static void mlx4_en_get_strings(struct net_device *dev,
308                                 uint32_t stringset, uint8_t *data)
309 {
310         struct mlx4_en_priv *priv = netdev_priv(dev);
311         int index = 0;
312         int i;
313
314         switch (stringset) {
315         case ETH_SS_TEST:
316                 for (i = 0; i < MLX4_EN_NUM_SELF_TEST - 2; i++)
317                         strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
318                 if (priv->mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UC_LOOPBACK)
319                         for (; i < MLX4_EN_NUM_SELF_TEST; i++)
320                                 strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
321                 break;
322
323         case ETH_SS_STATS:
324                 /* Add main counters */
325                 if (!priv->stats_bitmap) {
326                         for (i = 0; i < NUM_MAIN_STATS; i++)
327                                 strcpy(data + (index++) * ETH_GSTRING_LEN,
328                                         main_strings[i]);
329                         for (i = 0; i < NUM_PORT_STATS; i++)
330                                 strcpy(data + (index++) * ETH_GSTRING_LEN,
331                                         main_strings[i +
332                                         NUM_MAIN_STATS]);
333                         for (i = 0; i < NUM_PKT_STATS; i++)
334                                 strcpy(data + (index++) * ETH_GSTRING_LEN,
335                                         main_strings[i +
336                                         NUM_MAIN_STATS +
337                                         NUM_PORT_STATS]);
338                 } else
339                         for (i = 0; i < NUM_MAIN_STATS + NUM_PORT_STATS; i++) {
340                                 if ((priv->stats_bitmap >> i) & 1) {
341                                         strcpy(data +
342                                                (index++) * ETH_GSTRING_LEN,
343                                                main_strings[i]);
344                                 }
345                                 if (!(priv->stats_bitmap >> i))
346                                         break;
347                         }
348                 for (i = 0; i < priv->tx_ring_num; i++) {
349                         sprintf(data + (index++) * ETH_GSTRING_LEN,
350                                 "tx%d_packets", i);
351                         sprintf(data + (index++) * ETH_GSTRING_LEN,
352                                 "tx%d_bytes", i);
353                 }
354                 for (i = 0; i < priv->rx_ring_num; i++) {
355                         sprintf(data + (index++) * ETH_GSTRING_LEN,
356                                 "rx%d_packets", i);
357                         sprintf(data + (index++) * ETH_GSTRING_LEN,
358                                 "rx%d_bytes", i);
359 #ifdef CONFIG_NET_RX_BUSY_POLL
360                         sprintf(data + (index++) * ETH_GSTRING_LEN,
361                                 "rx%d_napi_yield", i);
362                         sprintf(data + (index++) * ETH_GSTRING_LEN,
363                                 "rx%d_misses", i);
364                         sprintf(data + (index++) * ETH_GSTRING_LEN,
365                                 "rx%d_cleaned", i);
366 #endif
367                 }
368                 break;
369         case ETH_SS_PRIV_FLAGS:
370                 for (i = 0; i < ARRAY_SIZE(mlx4_en_priv_flags); i++)
371                         strcpy(data + i * ETH_GSTRING_LEN,
372                                mlx4_en_priv_flags[i]);
373                 break;
374
375         }
376 }
377
378 static int mlx4_en_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
379 {
380         struct mlx4_en_priv *priv = netdev_priv(dev);
381         int trans_type;
382
383         cmd->autoneg = AUTONEG_DISABLE;
384         cmd->supported = SUPPORTED_10000baseT_Full;
385         cmd->advertising = ADVERTISED_10000baseT_Full;
386
387         if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
388                 return -ENOMEM;
389
390         trans_type = priv->port_state.transciver;
391         if (netif_carrier_ok(dev)) {
392                 ethtool_cmd_speed_set(cmd, priv->port_state.link_speed);
393                 cmd->duplex = DUPLEX_FULL;
394         } else {
395                 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
396                 cmd->duplex = DUPLEX_UNKNOWN;
397         }
398
399         if (trans_type > 0 && trans_type <= 0xC) {
400                 cmd->port = PORT_FIBRE;
401                 cmd->transceiver = XCVR_EXTERNAL;
402                 cmd->supported |= SUPPORTED_FIBRE;
403                 cmd->advertising |= ADVERTISED_FIBRE;
404         } else if (trans_type == 0x80 || trans_type == 0) {
405                 cmd->port = PORT_TP;
406                 cmd->transceiver = XCVR_INTERNAL;
407                 cmd->supported |= SUPPORTED_TP;
408                 cmd->advertising |= ADVERTISED_TP;
409         } else  {
410                 cmd->port = -1;
411                 cmd->transceiver = -1;
412         }
413         return 0;
414 }
415
416 static int mlx4_en_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
417 {
418         if ((cmd->autoneg == AUTONEG_ENABLE) ||
419             (ethtool_cmd_speed(cmd) != SPEED_10000) ||
420             (cmd->duplex != DUPLEX_FULL))
421                 return -EINVAL;
422
423         /* Nothing to change */
424         return 0;
425 }
426
427 static int mlx4_en_get_coalesce(struct net_device *dev,
428                               struct ethtool_coalesce *coal)
429 {
430         struct mlx4_en_priv *priv = netdev_priv(dev);
431
432         coal->tx_coalesce_usecs = priv->tx_usecs;
433         coal->tx_max_coalesced_frames = priv->tx_frames;
434         coal->tx_max_coalesced_frames_irq = priv->tx_work_limit;
435
436         coal->rx_coalesce_usecs = priv->rx_usecs;
437         coal->rx_max_coalesced_frames = priv->rx_frames;
438
439         coal->pkt_rate_low = priv->pkt_rate_low;
440         coal->rx_coalesce_usecs_low = priv->rx_usecs_low;
441         coal->pkt_rate_high = priv->pkt_rate_high;
442         coal->rx_coalesce_usecs_high = priv->rx_usecs_high;
443         coal->rate_sample_interval = priv->sample_interval;
444         coal->use_adaptive_rx_coalesce = priv->adaptive_rx_coal;
445
446         return 0;
447 }
448
449 static int mlx4_en_set_coalesce(struct net_device *dev,
450                               struct ethtool_coalesce *coal)
451 {
452         struct mlx4_en_priv *priv = netdev_priv(dev);
453
454         if (!coal->tx_max_coalesced_frames_irq)
455                 return -EINVAL;
456
457         priv->rx_frames = (coal->rx_max_coalesced_frames ==
458                            MLX4_EN_AUTO_CONF) ?
459                                 MLX4_EN_RX_COAL_TARGET :
460                                 coal->rx_max_coalesced_frames;
461         priv->rx_usecs = (coal->rx_coalesce_usecs ==
462                           MLX4_EN_AUTO_CONF) ?
463                                 MLX4_EN_RX_COAL_TIME :
464                                 coal->rx_coalesce_usecs;
465
466         /* Setting TX coalescing parameters */
467         if (coal->tx_coalesce_usecs != priv->tx_usecs ||
468             coal->tx_max_coalesced_frames != priv->tx_frames) {
469                 priv->tx_usecs = coal->tx_coalesce_usecs;
470                 priv->tx_frames = coal->tx_max_coalesced_frames;
471         }
472
473         /* Set adaptive coalescing params */
474         priv->pkt_rate_low = coal->pkt_rate_low;
475         priv->rx_usecs_low = coal->rx_coalesce_usecs_low;
476         priv->pkt_rate_high = coal->pkt_rate_high;
477         priv->rx_usecs_high = coal->rx_coalesce_usecs_high;
478         priv->sample_interval = coal->rate_sample_interval;
479         priv->adaptive_rx_coal = coal->use_adaptive_rx_coalesce;
480         priv->tx_work_limit = coal->tx_max_coalesced_frames_irq;
481
482         return mlx4_en_moderation_update(priv);
483 }
484
485 static int mlx4_en_set_pauseparam(struct net_device *dev,
486                                 struct ethtool_pauseparam *pause)
487 {
488         struct mlx4_en_priv *priv = netdev_priv(dev);
489         struct mlx4_en_dev *mdev = priv->mdev;
490         int err;
491
492         if (pause->autoneg)
493                 return -EINVAL;
494
495         priv->prof->tx_pause = pause->tx_pause != 0;
496         priv->prof->rx_pause = pause->rx_pause != 0;
497         err = mlx4_SET_PORT_general(mdev->dev, priv->port,
498                                     priv->rx_skb_size + ETH_FCS_LEN,
499                                     priv->prof->tx_pause,
500                                     priv->prof->tx_ppp,
501                                     priv->prof->rx_pause,
502                                     priv->prof->rx_ppp);
503         if (err)
504                 en_err(priv, "Failed setting pause params\n");
505
506         return err;
507 }
508
509 static void mlx4_en_get_pauseparam(struct net_device *dev,
510                                  struct ethtool_pauseparam *pause)
511 {
512         struct mlx4_en_priv *priv = netdev_priv(dev);
513
514         pause->tx_pause = priv->prof->tx_pause;
515         pause->rx_pause = priv->prof->rx_pause;
516 }
517
518 static int mlx4_en_set_ringparam(struct net_device *dev,
519                                  struct ethtool_ringparam *param)
520 {
521         struct mlx4_en_priv *priv = netdev_priv(dev);
522         struct mlx4_en_dev *mdev = priv->mdev;
523         u32 rx_size, tx_size;
524         int port_up = 0;
525         int err = 0;
526
527         if (param->rx_jumbo_pending || param->rx_mini_pending)
528                 return -EINVAL;
529
530         rx_size = roundup_pow_of_two(param->rx_pending);
531         rx_size = max_t(u32, rx_size, MLX4_EN_MIN_RX_SIZE);
532         rx_size = min_t(u32, rx_size, MLX4_EN_MAX_RX_SIZE);
533         tx_size = roundup_pow_of_two(param->tx_pending);
534         tx_size = max_t(u32, tx_size, MLX4_EN_MIN_TX_SIZE);
535         tx_size = min_t(u32, tx_size, MLX4_EN_MAX_TX_SIZE);
536
537         if (rx_size == (priv->port_up ? priv->rx_ring[0]->actual_size :
538                                         priv->rx_ring[0]->size) &&
539             tx_size == priv->tx_ring[0]->size)
540                 return 0;
541
542         mutex_lock(&mdev->state_lock);
543         if (priv->port_up) {
544                 port_up = 1;
545                 mlx4_en_stop_port(dev, 1);
546         }
547
548         mlx4_en_free_resources(priv);
549
550         priv->prof->tx_ring_size = tx_size;
551         priv->prof->rx_ring_size = rx_size;
552
553         err = mlx4_en_alloc_resources(priv);
554         if (err) {
555                 en_err(priv, "Failed reallocating port resources\n");
556                 goto out;
557         }
558         if (port_up) {
559                 err = mlx4_en_start_port(dev);
560                 if (err)
561                         en_err(priv, "Failed starting port\n");
562         }
563
564         err = mlx4_en_moderation_update(priv);
565
566 out:
567         mutex_unlock(&mdev->state_lock);
568         return err;
569 }
570
571 static void mlx4_en_get_ringparam(struct net_device *dev,
572                                   struct ethtool_ringparam *param)
573 {
574         struct mlx4_en_priv *priv = netdev_priv(dev);
575
576         memset(param, 0, sizeof(*param));
577         param->rx_max_pending = MLX4_EN_MAX_RX_SIZE;
578         param->tx_max_pending = MLX4_EN_MAX_TX_SIZE;
579         param->rx_pending = priv->port_up ?
580                 priv->rx_ring[0]->actual_size : priv->rx_ring[0]->size;
581         param->tx_pending = priv->tx_ring[0]->size;
582 }
583
584 static u32 mlx4_en_get_rxfh_indir_size(struct net_device *dev)
585 {
586         struct mlx4_en_priv *priv = netdev_priv(dev);
587
588         return priv->rx_ring_num;
589 }
590
591 static int mlx4_en_get_rxfh(struct net_device *dev, u32 *ring_index, u8 *key)
592 {
593         struct mlx4_en_priv *priv = netdev_priv(dev);
594         struct mlx4_en_rss_map *rss_map = &priv->rss_map;
595         int rss_rings;
596         size_t n = priv->rx_ring_num;
597         int err = 0;
598
599         rss_rings = priv->prof->rss_rings ?: priv->rx_ring_num;
600
601         while (n--) {
602                 ring_index[n] = rss_map->qps[n % rss_rings].qpn -
603                         rss_map->base_qpn;
604         }
605
606         return err;
607 }
608
609 static int mlx4_en_set_rxfh(struct net_device *dev, const u32 *ring_index,
610                             const u8 *key)
611 {
612         struct mlx4_en_priv *priv = netdev_priv(dev);
613         struct mlx4_en_dev *mdev = priv->mdev;
614         int port_up = 0;
615         int err = 0;
616         int i;
617         int rss_rings = 0;
618
619         /* Calculate RSS table size and make sure flows are spread evenly
620          * between rings
621          */
622         for (i = 0; i < priv->rx_ring_num; i++) {
623                 if (i > 0 && !ring_index[i] && !rss_rings)
624                         rss_rings = i;
625
626                 if (ring_index[i] != (i % (rss_rings ?: priv->rx_ring_num)))
627                         return -EINVAL;
628         }
629
630         if (!rss_rings)
631                 rss_rings = priv->rx_ring_num;
632
633         /* RSS table size must be an order of 2 */
634         if (!is_power_of_2(rss_rings))
635                 return -EINVAL;
636
637         mutex_lock(&mdev->state_lock);
638         if (priv->port_up) {
639                 port_up = 1;
640                 mlx4_en_stop_port(dev, 1);
641         }
642
643         priv->prof->rss_rings = rss_rings;
644
645         if (port_up) {
646                 err = mlx4_en_start_port(dev);
647                 if (err)
648                         en_err(priv, "Failed starting port\n");
649         }
650
651         mutex_unlock(&mdev->state_lock);
652         return err;
653 }
654
655 #define all_zeros_or_all_ones(field)            \
656         ((field) == 0 || (field) == (__force typeof(field))-1)
657
658 static int mlx4_en_validate_flow(struct net_device *dev,
659                                  struct ethtool_rxnfc *cmd)
660 {
661         struct ethtool_usrip4_spec *l3_mask;
662         struct ethtool_tcpip4_spec *l4_mask;
663         struct ethhdr *eth_mask;
664
665         if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
666                 return -EINVAL;
667
668         if (cmd->fs.flow_type & FLOW_MAC_EXT) {
669                 /* dest mac mask must be ff:ff:ff:ff:ff:ff */
670                 if (!is_broadcast_ether_addr(cmd->fs.m_ext.h_dest))
671                         return -EINVAL;
672         }
673
674         switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
675         case TCP_V4_FLOW:
676         case UDP_V4_FLOW:
677                 if (cmd->fs.m_u.tcp_ip4_spec.tos)
678                         return -EINVAL;
679                 l4_mask = &cmd->fs.m_u.tcp_ip4_spec;
680                 /* don't allow mask which isn't all 0 or 1 */
681                 if (!all_zeros_or_all_ones(l4_mask->ip4src) ||
682                     !all_zeros_or_all_ones(l4_mask->ip4dst) ||
683                     !all_zeros_or_all_ones(l4_mask->psrc) ||
684                     !all_zeros_or_all_ones(l4_mask->pdst))
685                         return -EINVAL;
686                 break;
687         case IP_USER_FLOW:
688                 l3_mask = &cmd->fs.m_u.usr_ip4_spec;
689                 if (l3_mask->l4_4_bytes || l3_mask->tos || l3_mask->proto ||
690                     cmd->fs.h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4 ||
691                     (!l3_mask->ip4src && !l3_mask->ip4dst) ||
692                     !all_zeros_or_all_ones(l3_mask->ip4src) ||
693                     !all_zeros_or_all_ones(l3_mask->ip4dst))
694                         return -EINVAL;
695                 break;
696         case ETHER_FLOW:
697                 eth_mask = &cmd->fs.m_u.ether_spec;
698                 /* source mac mask must not be set */
699                 if (!is_zero_ether_addr(eth_mask->h_source))
700                         return -EINVAL;
701
702                 /* dest mac mask must be ff:ff:ff:ff:ff:ff */
703                 if (!is_broadcast_ether_addr(eth_mask->h_dest))
704                         return -EINVAL;
705
706                 if (!all_zeros_or_all_ones(eth_mask->h_proto))
707                         return -EINVAL;
708                 break;
709         default:
710                 return -EINVAL;
711         }
712
713         if ((cmd->fs.flow_type & FLOW_EXT)) {
714                 if (cmd->fs.m_ext.vlan_etype ||
715                     !((cmd->fs.m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK)) ==
716                       0 ||
717                       (cmd->fs.m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK)) ==
718                       cpu_to_be16(VLAN_VID_MASK)))
719                         return -EINVAL;
720
721                 if (cmd->fs.m_ext.vlan_tci) {
722                         if (be16_to_cpu(cmd->fs.h_ext.vlan_tci) >= VLAN_N_VID)
723                                 return -EINVAL;
724
725                 }
726         }
727
728         return 0;
729 }
730
731 static int mlx4_en_ethtool_add_mac_rule(struct ethtool_rxnfc *cmd,
732                                         struct list_head *rule_list_h,
733                                         struct mlx4_spec_list *spec_l2,
734                                         unsigned char *mac)
735 {
736         int err = 0;
737         __be64 mac_msk = cpu_to_be64(MLX4_MAC_MASK << 16);
738
739         spec_l2->id = MLX4_NET_TRANS_RULE_ID_ETH;
740         memcpy(spec_l2->eth.dst_mac_msk, &mac_msk, ETH_ALEN);
741         memcpy(spec_l2->eth.dst_mac, mac, ETH_ALEN);
742
743         if ((cmd->fs.flow_type & FLOW_EXT) &&
744             (cmd->fs.m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK))) {
745                 spec_l2->eth.vlan_id = cmd->fs.h_ext.vlan_tci;
746                 spec_l2->eth.vlan_id_msk = cpu_to_be16(VLAN_VID_MASK);
747         }
748
749         list_add_tail(&spec_l2->list, rule_list_h);
750
751         return err;
752 }
753
754 static int mlx4_en_ethtool_add_mac_rule_by_ipv4(struct mlx4_en_priv *priv,
755                                                 struct ethtool_rxnfc *cmd,
756                                                 struct list_head *rule_list_h,
757                                                 struct mlx4_spec_list *spec_l2,
758                                                 __be32 ipv4_dst)
759 {
760 #ifdef CONFIG_INET
761         unsigned char mac[ETH_ALEN];
762
763         if (!ipv4_is_multicast(ipv4_dst)) {
764                 if (cmd->fs.flow_type & FLOW_MAC_EXT)
765                         memcpy(&mac, cmd->fs.h_ext.h_dest, ETH_ALEN);
766                 else
767                         memcpy(&mac, priv->dev->dev_addr, ETH_ALEN);
768         } else {
769                 ip_eth_mc_map(ipv4_dst, mac);
770         }
771
772         return mlx4_en_ethtool_add_mac_rule(cmd, rule_list_h, spec_l2, &mac[0]);
773 #else
774         return -EINVAL;
775 #endif
776 }
777
778 static int add_ip_rule(struct mlx4_en_priv *priv,
779                        struct ethtool_rxnfc *cmd,
780                        struct list_head *list_h)
781 {
782         int err;
783         struct mlx4_spec_list *spec_l2 = NULL;
784         struct mlx4_spec_list *spec_l3 = NULL;
785         struct ethtool_usrip4_spec *l3_mask = &cmd->fs.m_u.usr_ip4_spec;
786
787         spec_l3 = kzalloc(sizeof(*spec_l3), GFP_KERNEL);
788         spec_l2 = kzalloc(sizeof(*spec_l2), GFP_KERNEL);
789         if (!spec_l2 || !spec_l3) {
790                 err = -ENOMEM;
791                 goto free_spec;
792         }
793
794         err = mlx4_en_ethtool_add_mac_rule_by_ipv4(priv, cmd, list_h, spec_l2,
795                                                    cmd->fs.h_u.
796                                                    usr_ip4_spec.ip4dst);
797         if (err)
798                 goto free_spec;
799         spec_l3->id = MLX4_NET_TRANS_RULE_ID_IPV4;
800         spec_l3->ipv4.src_ip = cmd->fs.h_u.usr_ip4_spec.ip4src;
801         if (l3_mask->ip4src)
802                 spec_l3->ipv4.src_ip_msk = EN_ETHTOOL_WORD_MASK;
803         spec_l3->ipv4.dst_ip = cmd->fs.h_u.usr_ip4_spec.ip4dst;
804         if (l3_mask->ip4dst)
805                 spec_l3->ipv4.dst_ip_msk = EN_ETHTOOL_WORD_MASK;
806         list_add_tail(&spec_l3->list, list_h);
807
808         return 0;
809
810 free_spec:
811         kfree(spec_l2);
812         kfree(spec_l3);
813         return err;
814 }
815
816 static int add_tcp_udp_rule(struct mlx4_en_priv *priv,
817                              struct ethtool_rxnfc *cmd,
818                              struct list_head *list_h, int proto)
819 {
820         int err;
821         struct mlx4_spec_list *spec_l2 = NULL;
822         struct mlx4_spec_list *spec_l3 = NULL;
823         struct mlx4_spec_list *spec_l4 = NULL;
824         struct ethtool_tcpip4_spec *l4_mask = &cmd->fs.m_u.tcp_ip4_spec;
825
826         spec_l2 = kzalloc(sizeof(*spec_l2), GFP_KERNEL);
827         spec_l3 = kzalloc(sizeof(*spec_l3), GFP_KERNEL);
828         spec_l4 = kzalloc(sizeof(*spec_l4), GFP_KERNEL);
829         if (!spec_l2 || !spec_l3 || !spec_l4) {
830                 err = -ENOMEM;
831                 goto free_spec;
832         }
833
834         spec_l3->id = MLX4_NET_TRANS_RULE_ID_IPV4;
835
836         if (proto == TCP_V4_FLOW) {
837                 err = mlx4_en_ethtool_add_mac_rule_by_ipv4(priv, cmd, list_h,
838                                                            spec_l2,
839                                                            cmd->fs.h_u.
840                                                            tcp_ip4_spec.ip4dst);
841                 if (err)
842                         goto free_spec;
843                 spec_l4->id = MLX4_NET_TRANS_RULE_ID_TCP;
844                 spec_l3->ipv4.src_ip = cmd->fs.h_u.tcp_ip4_spec.ip4src;
845                 spec_l3->ipv4.dst_ip = cmd->fs.h_u.tcp_ip4_spec.ip4dst;
846                 spec_l4->tcp_udp.src_port = cmd->fs.h_u.tcp_ip4_spec.psrc;
847                 spec_l4->tcp_udp.dst_port = cmd->fs.h_u.tcp_ip4_spec.pdst;
848         } else {
849                 err = mlx4_en_ethtool_add_mac_rule_by_ipv4(priv, cmd, list_h,
850                                                            spec_l2,
851                                                            cmd->fs.h_u.
852                                                            udp_ip4_spec.ip4dst);
853                 if (err)
854                         goto free_spec;
855                 spec_l4->id = MLX4_NET_TRANS_RULE_ID_UDP;
856                 spec_l3->ipv4.src_ip = cmd->fs.h_u.udp_ip4_spec.ip4src;
857                 spec_l3->ipv4.dst_ip = cmd->fs.h_u.udp_ip4_spec.ip4dst;
858                 spec_l4->tcp_udp.src_port = cmd->fs.h_u.udp_ip4_spec.psrc;
859                 spec_l4->tcp_udp.dst_port = cmd->fs.h_u.udp_ip4_spec.pdst;
860         }
861
862         if (l4_mask->ip4src)
863                 spec_l3->ipv4.src_ip_msk = EN_ETHTOOL_WORD_MASK;
864         if (l4_mask->ip4dst)
865                 spec_l3->ipv4.dst_ip_msk = EN_ETHTOOL_WORD_MASK;
866
867         if (l4_mask->psrc)
868                 spec_l4->tcp_udp.src_port_msk = EN_ETHTOOL_SHORT_MASK;
869         if (l4_mask->pdst)
870                 spec_l4->tcp_udp.dst_port_msk = EN_ETHTOOL_SHORT_MASK;
871
872         list_add_tail(&spec_l3->list, list_h);
873         list_add_tail(&spec_l4->list, list_h);
874
875         return 0;
876
877 free_spec:
878         kfree(spec_l2);
879         kfree(spec_l3);
880         kfree(spec_l4);
881         return err;
882 }
883
884 static int mlx4_en_ethtool_to_net_trans_rule(struct net_device *dev,
885                                              struct ethtool_rxnfc *cmd,
886                                              struct list_head *rule_list_h)
887 {
888         int err;
889         struct ethhdr *eth_spec;
890         struct mlx4_spec_list *spec_l2;
891         struct mlx4_en_priv *priv = netdev_priv(dev);
892
893         err = mlx4_en_validate_flow(dev, cmd);
894         if (err)
895                 return err;
896
897         switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
898         case ETHER_FLOW:
899                 spec_l2 = kzalloc(sizeof(*spec_l2), GFP_KERNEL);
900                 if (!spec_l2)
901                         return -ENOMEM;
902
903                 eth_spec = &cmd->fs.h_u.ether_spec;
904                 mlx4_en_ethtool_add_mac_rule(cmd, rule_list_h, spec_l2,
905                                              &eth_spec->h_dest[0]);
906                 spec_l2->eth.ether_type = eth_spec->h_proto;
907                 if (eth_spec->h_proto)
908                         spec_l2->eth.ether_type_enable = 1;
909                 break;
910         case IP_USER_FLOW:
911                 err = add_ip_rule(priv, cmd, rule_list_h);
912                 break;
913         case TCP_V4_FLOW:
914                 err = add_tcp_udp_rule(priv, cmd, rule_list_h, TCP_V4_FLOW);
915                 break;
916         case UDP_V4_FLOW:
917                 err = add_tcp_udp_rule(priv, cmd, rule_list_h, UDP_V4_FLOW);
918                 break;
919         }
920
921         return err;
922 }
923
924 static int mlx4_en_flow_replace(struct net_device *dev,
925                                 struct ethtool_rxnfc *cmd)
926 {
927         int err;
928         struct mlx4_en_priv *priv = netdev_priv(dev);
929         struct ethtool_flow_id *loc_rule;
930         struct mlx4_spec_list *spec, *tmp_spec;
931         u32 qpn;
932         u64 reg_id;
933
934         struct mlx4_net_trans_rule rule = {
935                 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
936                 .exclusive = 0,
937                 .allow_loopback = 1,
938                 .promisc_mode = MLX4_FS_REGULAR,
939         };
940
941         rule.port = priv->port;
942         rule.priority = MLX4_DOMAIN_ETHTOOL | cmd->fs.location;
943         INIT_LIST_HEAD(&rule.list);
944
945         /* Allow direct QP attaches if the EN_ETHTOOL_QP_ATTACH flag is set */
946         if (cmd->fs.ring_cookie == RX_CLS_FLOW_DISC)
947                 qpn = priv->drop_qp.qpn;
948         else if (cmd->fs.ring_cookie & EN_ETHTOOL_QP_ATTACH) {
949                 qpn = cmd->fs.ring_cookie & (EN_ETHTOOL_QP_ATTACH - 1);
950         } else {
951                 if (cmd->fs.ring_cookie >= priv->rx_ring_num) {
952                         en_warn(priv, "rxnfc: RX ring (%llu) doesn't exist\n",
953                                 cmd->fs.ring_cookie);
954                         return -EINVAL;
955                 }
956                 qpn = priv->rss_map.qps[cmd->fs.ring_cookie].qpn;
957                 if (!qpn) {
958                         en_warn(priv, "rxnfc: RX ring (%llu) is inactive\n",
959                                 cmd->fs.ring_cookie);
960                         return -EINVAL;
961                 }
962         }
963         rule.qpn = qpn;
964         err = mlx4_en_ethtool_to_net_trans_rule(dev, cmd, &rule.list);
965         if (err)
966                 goto out_free_list;
967
968         loc_rule = &priv->ethtool_rules[cmd->fs.location];
969         if (loc_rule->id) {
970                 err = mlx4_flow_detach(priv->mdev->dev, loc_rule->id);
971                 if (err) {
972                         en_err(priv, "Fail to detach network rule at location %d. registration id = %llx\n",
973                                cmd->fs.location, loc_rule->id);
974                         goto out_free_list;
975                 }
976                 loc_rule->id = 0;
977                 memset(&loc_rule->flow_spec, 0,
978                        sizeof(struct ethtool_rx_flow_spec));
979                 list_del(&loc_rule->list);
980         }
981         err = mlx4_flow_attach(priv->mdev->dev, &rule, &reg_id);
982         if (err) {
983                 en_err(priv, "Fail to attach network rule at location %d\n",
984                        cmd->fs.location);
985                 goto out_free_list;
986         }
987         loc_rule->id = reg_id;
988         memcpy(&loc_rule->flow_spec, &cmd->fs,
989                sizeof(struct ethtool_rx_flow_spec));
990         list_add_tail(&loc_rule->list, &priv->ethtool_list);
991
992 out_free_list:
993         list_for_each_entry_safe(spec, tmp_spec, &rule.list, list) {
994                 list_del(&spec->list);
995                 kfree(spec);
996         }
997         return err;
998 }
999
1000 static int mlx4_en_flow_detach(struct net_device *dev,
1001                                struct ethtool_rxnfc *cmd)
1002 {
1003         int err = 0;
1004         struct ethtool_flow_id *rule;
1005         struct mlx4_en_priv *priv = netdev_priv(dev);
1006
1007         if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
1008                 return -EINVAL;
1009
1010         rule = &priv->ethtool_rules[cmd->fs.location];
1011         if (!rule->id) {
1012                 err =  -ENOENT;
1013                 goto out;
1014         }
1015
1016         err = mlx4_flow_detach(priv->mdev->dev, rule->id);
1017         if (err) {
1018                 en_err(priv, "Fail to detach network rule at location %d. registration id = 0x%llx\n",
1019                        cmd->fs.location, rule->id);
1020                 goto out;
1021         }
1022         rule->id = 0;
1023         memset(&rule->flow_spec, 0, sizeof(struct ethtool_rx_flow_spec));
1024         list_del(&rule->list);
1025 out:
1026         return err;
1027
1028 }
1029
1030 static int mlx4_en_get_flow(struct net_device *dev, struct ethtool_rxnfc *cmd,
1031                             int loc)
1032 {
1033         int err = 0;
1034         struct ethtool_flow_id *rule;
1035         struct mlx4_en_priv *priv = netdev_priv(dev);
1036
1037         if (loc < 0 || loc >= MAX_NUM_OF_FS_RULES)
1038                 return -EINVAL;
1039
1040         rule = &priv->ethtool_rules[loc];
1041         if (rule->id)
1042                 memcpy(&cmd->fs, &rule->flow_spec,
1043                        sizeof(struct ethtool_rx_flow_spec));
1044         else
1045                 err = -ENOENT;
1046
1047         return err;
1048 }
1049
1050 static int mlx4_en_get_num_flows(struct mlx4_en_priv *priv)
1051 {
1052
1053         int i, res = 0;
1054         for (i = 0; i < MAX_NUM_OF_FS_RULES; i++) {
1055                 if (priv->ethtool_rules[i].id)
1056                         res++;
1057         }
1058         return res;
1059
1060 }
1061
1062 static int mlx4_en_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1063                              u32 *rule_locs)
1064 {
1065         struct mlx4_en_priv *priv = netdev_priv(dev);
1066         struct mlx4_en_dev *mdev = priv->mdev;
1067         int err = 0;
1068         int i = 0, priority = 0;
1069
1070         if ((cmd->cmd == ETHTOOL_GRXCLSRLCNT ||
1071              cmd->cmd == ETHTOOL_GRXCLSRULE ||
1072              cmd->cmd == ETHTOOL_GRXCLSRLALL) &&
1073             (mdev->dev->caps.steering_mode !=
1074              MLX4_STEERING_MODE_DEVICE_MANAGED || !priv->port_up))
1075                 return -EINVAL;
1076
1077         switch (cmd->cmd) {
1078         case ETHTOOL_GRXRINGS:
1079                 cmd->data = priv->rx_ring_num;
1080                 break;
1081         case ETHTOOL_GRXCLSRLCNT:
1082                 cmd->rule_cnt = mlx4_en_get_num_flows(priv);
1083                 break;
1084         case ETHTOOL_GRXCLSRULE:
1085                 err = mlx4_en_get_flow(dev, cmd, cmd->fs.location);
1086                 break;
1087         case ETHTOOL_GRXCLSRLALL:
1088                 while ((!err || err == -ENOENT) && priority < cmd->rule_cnt) {
1089                         err = mlx4_en_get_flow(dev, cmd, i);
1090                         if (!err)
1091                                 rule_locs[priority++] = i;
1092                         i++;
1093                 }
1094                 err = 0;
1095                 break;
1096         default:
1097                 err = -EOPNOTSUPP;
1098                 break;
1099         }
1100
1101         return err;
1102 }
1103
1104 static int mlx4_en_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1105 {
1106         int err = 0;
1107         struct mlx4_en_priv *priv = netdev_priv(dev);
1108         struct mlx4_en_dev *mdev = priv->mdev;
1109
1110         if (mdev->dev->caps.steering_mode !=
1111             MLX4_STEERING_MODE_DEVICE_MANAGED || !priv->port_up)
1112                 return -EINVAL;
1113
1114         switch (cmd->cmd) {
1115         case ETHTOOL_SRXCLSRLINS:
1116                 err = mlx4_en_flow_replace(dev, cmd);
1117                 break;
1118         case ETHTOOL_SRXCLSRLDEL:
1119                 err = mlx4_en_flow_detach(dev, cmd);
1120                 break;
1121         default:
1122                 en_warn(priv, "Unsupported ethtool command. (%d)\n", cmd->cmd);
1123                 return -EINVAL;
1124         }
1125
1126         return err;
1127 }
1128
1129 static void mlx4_en_get_channels(struct net_device *dev,
1130                                  struct ethtool_channels *channel)
1131 {
1132         struct mlx4_en_priv *priv = netdev_priv(dev);
1133
1134         memset(channel, 0, sizeof(*channel));
1135
1136         channel->max_rx = MAX_RX_RINGS;
1137         channel->max_tx = MLX4_EN_MAX_TX_RING_P_UP;
1138
1139         channel->rx_count = priv->rx_ring_num;
1140         channel->tx_count = priv->tx_ring_num / MLX4_EN_NUM_UP;
1141 }
1142
1143 static int mlx4_en_set_channels(struct net_device *dev,
1144                                 struct ethtool_channels *channel)
1145 {
1146         struct mlx4_en_priv *priv = netdev_priv(dev);
1147         struct mlx4_en_dev *mdev = priv->mdev;
1148         int port_up = 0;
1149         int err = 0;
1150
1151         if (channel->other_count || channel->combined_count ||
1152             channel->tx_count > MLX4_EN_MAX_TX_RING_P_UP ||
1153             channel->rx_count > MAX_RX_RINGS ||
1154             !channel->tx_count || !channel->rx_count)
1155                 return -EINVAL;
1156
1157         mutex_lock(&mdev->state_lock);
1158         if (priv->port_up) {
1159                 port_up = 1;
1160                 mlx4_en_stop_port(dev, 1);
1161         }
1162
1163         mlx4_en_free_resources(priv);
1164
1165         priv->num_tx_rings_p_up = channel->tx_count;
1166         priv->tx_ring_num = channel->tx_count * MLX4_EN_NUM_UP;
1167         priv->rx_ring_num = channel->rx_count;
1168
1169         err = mlx4_en_alloc_resources(priv);
1170         if (err) {
1171                 en_err(priv, "Failed reallocating port resources\n");
1172                 goto out;
1173         }
1174
1175         netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
1176         netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
1177
1178         if (dev->num_tc)
1179                 mlx4_en_setup_tc(dev, MLX4_EN_NUM_UP);
1180
1181         en_warn(priv, "Using %d TX rings\n", priv->tx_ring_num);
1182         en_warn(priv, "Using %d RX rings\n", priv->rx_ring_num);
1183
1184         if (port_up) {
1185                 err = mlx4_en_start_port(dev);
1186                 if (err)
1187                         en_err(priv, "Failed starting port\n");
1188         }
1189
1190         err = mlx4_en_moderation_update(priv);
1191
1192 out:
1193         mutex_unlock(&mdev->state_lock);
1194         return err;
1195 }
1196
1197 static int mlx4_en_get_ts_info(struct net_device *dev,
1198                                struct ethtool_ts_info *info)
1199 {
1200         struct mlx4_en_priv *priv = netdev_priv(dev);
1201         struct mlx4_en_dev *mdev = priv->mdev;
1202         int ret;
1203
1204         ret = ethtool_op_get_ts_info(dev, info);
1205         if (ret)
1206                 return ret;
1207
1208         if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS) {
1209                 info->so_timestamping |=
1210                         SOF_TIMESTAMPING_TX_HARDWARE |
1211                         SOF_TIMESTAMPING_RX_HARDWARE |
1212                         SOF_TIMESTAMPING_RAW_HARDWARE;
1213
1214                 info->tx_types =
1215                         (1 << HWTSTAMP_TX_OFF) |
1216                         (1 << HWTSTAMP_TX_ON);
1217
1218                 info->rx_filters =
1219                         (1 << HWTSTAMP_FILTER_NONE) |
1220                         (1 << HWTSTAMP_FILTER_ALL);
1221
1222                 if (mdev->ptp_clock)
1223                         info->phc_index = ptp_clock_index(mdev->ptp_clock);
1224         }
1225
1226         return ret;
1227 }
1228
1229 static int mlx4_en_set_priv_flags(struct net_device *dev, u32 flags)
1230 {
1231         struct mlx4_en_priv *priv = netdev_priv(dev);
1232         bool bf_enabled_new = !!(flags & MLX4_EN_PRIV_FLAGS_BLUEFLAME);
1233         bool bf_enabled_old = !!(priv->pflags & MLX4_EN_PRIV_FLAGS_BLUEFLAME);
1234         int i;
1235
1236         if (bf_enabled_new == bf_enabled_old)
1237                 return 0; /* Nothing to do */
1238
1239         if (bf_enabled_new) {
1240                 bool bf_supported = true;
1241
1242                 for (i = 0; i < priv->tx_ring_num; i++)
1243                         bf_supported &= priv->tx_ring[i]->bf_alloced;
1244
1245                 if (!bf_supported) {
1246                         en_err(priv, "BlueFlame is not supported\n");
1247                         return -EINVAL;
1248                 }
1249
1250                 priv->pflags |= MLX4_EN_PRIV_FLAGS_BLUEFLAME;
1251         } else {
1252                 priv->pflags &= ~MLX4_EN_PRIV_FLAGS_BLUEFLAME;
1253         }
1254
1255         for (i = 0; i < priv->tx_ring_num; i++)
1256                 priv->tx_ring[i]->bf_enabled = bf_enabled_new;
1257
1258         en_info(priv, "BlueFlame %s\n",
1259                 bf_enabled_new ?  "Enabled" : "Disabled");
1260
1261         return 0;
1262 }
1263
1264 static u32 mlx4_en_get_priv_flags(struct net_device *dev)
1265 {
1266         struct mlx4_en_priv *priv = netdev_priv(dev);
1267
1268         return priv->pflags;
1269 }
1270
1271 static int mlx4_en_get_tunable(struct net_device *dev,
1272                                const struct ethtool_tunable *tuna,
1273                                void *data)
1274 {
1275         const struct mlx4_en_priv *priv = netdev_priv(dev);
1276         int ret = 0;
1277
1278         switch (tuna->id) {
1279         case ETHTOOL_TX_COPYBREAK:
1280                 *(u32 *)data = priv->prof->inline_thold;
1281                 break;
1282         default:
1283                 ret = -EINVAL;
1284                 break;
1285         }
1286
1287         return ret;
1288 }
1289
1290 static int mlx4_en_set_tunable(struct net_device *dev,
1291                                const struct ethtool_tunable *tuna,
1292                                const void *data)
1293 {
1294         struct mlx4_en_priv *priv = netdev_priv(dev);
1295         int val, ret = 0;
1296
1297         switch (tuna->id) {
1298         case ETHTOOL_TX_COPYBREAK:
1299                 val = *(u32 *)data;
1300                 if (val < MIN_PKT_LEN || val > MAX_INLINE)
1301                         ret = -EINVAL;
1302                 else
1303                         priv->prof->inline_thold = val;
1304                 break;
1305         default:
1306                 ret = -EINVAL;
1307                 break;
1308         }
1309
1310         return ret;
1311 }
1312
1313 static int mlx4_en_get_module_info(struct net_device *dev,
1314                                    struct ethtool_modinfo *modinfo)
1315 {
1316         struct mlx4_en_priv *priv = netdev_priv(dev);
1317         struct mlx4_en_dev *mdev = priv->mdev;
1318         int ret;
1319         u8 data[4];
1320
1321         /* Read first 2 bytes to get Module & REV ID */
1322         ret = mlx4_get_module_info(mdev->dev, priv->port,
1323                                    0/*offset*/, 2/*size*/, data);
1324         if (ret < 2)
1325                 return -EIO;
1326
1327         switch (data[0] /* identifier */) {
1328         case MLX4_MODULE_ID_QSFP:
1329                 modinfo->type = ETH_MODULE_SFF_8436;
1330                 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1331                 break;
1332         case MLX4_MODULE_ID_QSFP_PLUS:
1333                 if (data[1] >= 0x3) { /* revision id */
1334                         modinfo->type = ETH_MODULE_SFF_8636;
1335                         modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1336                 } else {
1337                         modinfo->type = ETH_MODULE_SFF_8436;
1338                         modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1339                 }
1340                 break;
1341         case MLX4_MODULE_ID_QSFP28:
1342                 modinfo->type = ETH_MODULE_SFF_8636;
1343                 modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1344                 break;
1345         case MLX4_MODULE_ID_SFP:
1346                 modinfo->type = ETH_MODULE_SFF_8472;
1347                 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1348                 break;
1349         default:
1350                 return -ENOSYS;
1351         }
1352
1353         return 0;
1354 }
1355
1356 static int mlx4_en_get_module_eeprom(struct net_device *dev,
1357                                      struct ethtool_eeprom *ee,
1358                                      u8 *data)
1359 {
1360         struct mlx4_en_priv *priv = netdev_priv(dev);
1361         struct mlx4_en_dev *mdev = priv->mdev;
1362         int offset = ee->offset;
1363         int i = 0, ret;
1364
1365         if (ee->len == 0)
1366                 return -EINVAL;
1367
1368         memset(data, 0, ee->len);
1369
1370         while (i < ee->len) {
1371                 en_dbg(DRV, priv,
1372                        "mlx4_get_module_info i(%d) offset(%d) len(%d)\n",
1373                        i, offset, ee->len - i);
1374
1375                 ret = mlx4_get_module_info(mdev->dev, priv->port,
1376                                            offset, ee->len - i, data + i);
1377
1378                 if (!ret) /* Done reading */
1379                         return 0;
1380
1381                 if (ret < 0) {
1382                         en_err(priv,
1383                                "mlx4_get_module_info i(%d) offset(%d) bytes_to_read(%d) - FAILED (0x%x)\n",
1384                                i, offset, ee->len - i, ret);
1385                         return 0;
1386                 }
1387
1388                 i += ret;
1389                 offset += ret;
1390         }
1391         return 0;
1392 }
1393
1394 const struct ethtool_ops mlx4_en_ethtool_ops = {
1395         .get_drvinfo = mlx4_en_get_drvinfo,
1396         .get_settings = mlx4_en_get_settings,
1397         .set_settings = mlx4_en_set_settings,
1398         .get_link = ethtool_op_get_link,
1399         .get_strings = mlx4_en_get_strings,
1400         .get_sset_count = mlx4_en_get_sset_count,
1401         .get_ethtool_stats = mlx4_en_get_ethtool_stats,
1402         .self_test = mlx4_en_self_test,
1403         .get_wol = mlx4_en_get_wol,
1404         .set_wol = mlx4_en_set_wol,
1405         .get_msglevel = mlx4_en_get_msglevel,
1406         .set_msglevel = mlx4_en_set_msglevel,
1407         .get_coalesce = mlx4_en_get_coalesce,
1408         .set_coalesce = mlx4_en_set_coalesce,
1409         .get_pauseparam = mlx4_en_get_pauseparam,
1410         .set_pauseparam = mlx4_en_set_pauseparam,
1411         .get_ringparam = mlx4_en_get_ringparam,
1412         .set_ringparam = mlx4_en_set_ringparam,
1413         .get_rxnfc = mlx4_en_get_rxnfc,
1414         .set_rxnfc = mlx4_en_set_rxnfc,
1415         .get_rxfh_indir_size = mlx4_en_get_rxfh_indir_size,
1416         .get_rxfh = mlx4_en_get_rxfh,
1417         .set_rxfh = mlx4_en_set_rxfh,
1418         .get_channels = mlx4_en_get_channels,
1419         .set_channels = mlx4_en_set_channels,
1420         .get_ts_info = mlx4_en_get_ts_info,
1421         .set_priv_flags = mlx4_en_set_priv_flags,
1422         .get_priv_flags = mlx4_en_get_priv_flags,
1423         .get_tunable            = mlx4_en_get_tunable,
1424         .set_tunable            = mlx4_en_set_tunable,
1425         .get_module_info = mlx4_en_get_module_info,
1426         .get_module_eeprom = mlx4_en_get_module_eeprom
1427 };
1428
1429
1430
1431
1432