mlx4_en: Reporting HW revision in ethtool -i
[pandora-kernel.git] / drivers / net / 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
38 #include "mlx4_en.h"
39 #include "en_port.h"
40
41
42 static void
43 mlx4_en_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
44 {
45         struct mlx4_en_priv *priv = netdev_priv(dev);
46         struct mlx4_en_dev *mdev = priv->mdev;
47
48         switch (mdev->dev->rev_id) {
49         case 0xa0:
50                 if (dev->dev_id >= MLX4_EN_CX3_LOW_ID && dev->dev_id <= MLX4_EN_CX3_HIGH_ID)
51                         sprintf(drvinfo->driver, DRV_NAME " (%s_CX-3)", mdev->dev->board_id);
52                 else
53                         sprintf(drvinfo->driver, DRV_NAME " (%s_CX)", mdev->dev->board_id);
54                 break;
55         case 0xb0:
56                 sprintf(drvinfo->driver, DRV_NAME " (%s_CX-2)", mdev->dev->board_id);
57                 break;
58         default:
59                 sprintf(drvinfo->driver, DRV_NAME " (%s)", mdev->dev->board_id);
60                 break;
61         }
62         strncpy(drvinfo->version, DRV_VERSION " (" DRV_RELDATE ")", 32);
63         sprintf(drvinfo->fw_version, "%d.%d.%d",
64                 (u16) (mdev->dev->caps.fw_ver >> 32),
65                 (u16) ((mdev->dev->caps.fw_ver >> 16) & 0xffff),
66                 (u16) (mdev->dev->caps.fw_ver & 0xffff));
67         strncpy(drvinfo->bus_info, pci_name(mdev->dev->pdev), 32);
68         drvinfo->n_stats = 0;
69         drvinfo->regdump_len = 0;
70         drvinfo->eedump_len = 0;
71 }
72
73 static u32 mlx4_en_get_tso(struct net_device *dev)
74 {
75         return (dev->features & NETIF_F_TSO) != 0;
76 }
77
78 static int mlx4_en_set_tso(struct net_device *dev, u32 data)
79 {
80         struct mlx4_en_priv *priv = netdev_priv(dev);
81
82         if (data) {
83                 if (!priv->mdev->LSO_support)
84                         return -EPERM;
85                 dev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
86         } else
87                 dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
88         return 0;
89 }
90
91 static u32 mlx4_en_get_rx_csum(struct net_device *dev)
92 {
93         struct mlx4_en_priv *priv = netdev_priv(dev);
94         return priv->rx_csum;
95 }
96
97 static int mlx4_en_set_rx_csum(struct net_device *dev, u32 data)
98 {
99         struct mlx4_en_priv *priv = netdev_priv(dev);
100         priv->rx_csum = (data != 0);
101         return 0;
102 }
103
104 static const char main_strings[][ETH_GSTRING_LEN] = {
105         "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
106         "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
107         "rx_length_errors", "rx_over_errors", "rx_crc_errors",
108         "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
109         "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
110         "tx_heartbeat_errors", "tx_window_errors",
111
112         /* port statistics */
113         "tso_packets",
114         "queue_stopped", "wake_queue", "tx_timeout", "rx_alloc_failed",
115         "rx_csum_good", "rx_csum_none", "tx_chksum_offload",
116
117         /* packet statistics */
118         "broadcast", "rx_prio_0", "rx_prio_1", "rx_prio_2", "rx_prio_3",
119         "rx_prio_4", "rx_prio_5", "rx_prio_6", "rx_prio_7", "tx_prio_0",
120         "tx_prio_1", "tx_prio_2", "tx_prio_3", "tx_prio_4", "tx_prio_5",
121         "tx_prio_6", "tx_prio_7",
122 };
123 #define NUM_MAIN_STATS  21
124 #define NUM_ALL_STATS   (NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PKT_STATS + NUM_PERF_STATS)
125
126 static const char mlx4_en_test_names[][ETH_GSTRING_LEN]= {
127         "Interupt Test",
128         "Link Test",
129         "Speed Test",
130         "Register Test",
131         "Loopback Test",
132 };
133
134 static u32 mlx4_en_get_msglevel(struct net_device *dev)
135 {
136         return ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable;
137 }
138
139 static void mlx4_en_set_msglevel(struct net_device *dev, u32 val)
140 {
141         ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable = val;
142 }
143
144 static void mlx4_en_get_wol(struct net_device *netdev,
145                             struct ethtool_wolinfo *wol)
146 {
147         struct mlx4_en_priv *priv = netdev_priv(netdev);
148         int err = 0;
149         u64 config = 0;
150
151         if (!priv->mdev->dev->caps.wol) {
152                 wol->supported = 0;
153                 wol->wolopts = 0;
154                 return;
155         }
156
157         err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
158         if (err) {
159                 en_err(priv, "Failed to get WoL information\n");
160                 return;
161         }
162
163         if (config & MLX4_EN_WOL_MAGIC)
164                 wol->supported = WAKE_MAGIC;
165         else
166                 wol->supported = 0;
167
168         if (config & MLX4_EN_WOL_ENABLED)
169                 wol->wolopts = WAKE_MAGIC;
170         else
171                 wol->wolopts = 0;
172 }
173
174 static int mlx4_en_set_wol(struct net_device *netdev,
175                             struct ethtool_wolinfo *wol)
176 {
177         struct mlx4_en_priv *priv = netdev_priv(netdev);
178         u64 config = 0;
179         int err = 0;
180
181         if (!priv->mdev->dev->caps.wol)
182                 return -EOPNOTSUPP;
183
184         if (wol->supported & ~WAKE_MAGIC)
185                 return -EINVAL;
186
187         err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
188         if (err) {
189                 en_err(priv, "Failed to get WoL info, unable to modify\n");
190                 return err;
191         }
192
193         if (wol->wolopts & WAKE_MAGIC) {
194                 config |= MLX4_EN_WOL_DO_MODIFY | MLX4_EN_WOL_ENABLED |
195                                 MLX4_EN_WOL_MAGIC;
196         } else {
197                 config &= ~(MLX4_EN_WOL_ENABLED | MLX4_EN_WOL_MAGIC);
198                 config |= MLX4_EN_WOL_DO_MODIFY;
199         }
200
201         err = mlx4_wol_write(priv->mdev->dev, config, priv->port);
202         if (err)
203                 en_err(priv, "Failed to set WoL information\n");
204
205         return err;
206 }
207
208 static int mlx4_en_get_sset_count(struct net_device *dev, int sset)
209 {
210         struct mlx4_en_priv *priv = netdev_priv(dev);
211
212         switch (sset) {
213         case ETH_SS_STATS:
214                 return NUM_ALL_STATS +
215                         (priv->tx_ring_num + priv->rx_ring_num) * 2;
216         case ETH_SS_TEST:
217                 return MLX4_EN_NUM_SELF_TEST - !(priv->mdev->dev->caps.loopback_support) * 2;
218         default:
219                 return -EOPNOTSUPP;
220         }
221 }
222
223 static void mlx4_en_get_ethtool_stats(struct net_device *dev,
224                 struct ethtool_stats *stats, uint64_t *data)
225 {
226         struct mlx4_en_priv *priv = netdev_priv(dev);
227         int index = 0;
228         int i;
229
230         spin_lock_bh(&priv->stats_lock);
231
232         for (i = 0; i < NUM_MAIN_STATS; i++)
233                 data[index++] = ((unsigned long *) &priv->stats)[i];
234         for (i = 0; i < NUM_PORT_STATS; i++)
235                 data[index++] = ((unsigned long *) &priv->port_stats)[i];
236         for (i = 0; i < priv->tx_ring_num; i++) {
237                 data[index++] = priv->tx_ring[i].packets;
238                 data[index++] = priv->tx_ring[i].bytes;
239         }
240         for (i = 0; i < priv->rx_ring_num; i++) {
241                 data[index++] = priv->rx_ring[i].packets;
242                 data[index++] = priv->rx_ring[i].bytes;
243         }
244         for (i = 0; i < NUM_PKT_STATS; i++)
245                 data[index++] = ((unsigned long *) &priv->pkstats)[i];
246         spin_unlock_bh(&priv->stats_lock);
247
248 }
249
250 static void mlx4_en_self_test(struct net_device *dev,
251                               struct ethtool_test *etest, u64 *buf)
252 {
253         mlx4_en_ex_selftest(dev, &etest->flags, buf);
254 }
255
256 static void mlx4_en_get_strings(struct net_device *dev,
257                                 uint32_t stringset, uint8_t *data)
258 {
259         struct mlx4_en_priv *priv = netdev_priv(dev);
260         int index = 0;
261         int i;
262
263         switch (stringset) {
264         case ETH_SS_TEST:
265                 for (i = 0; i < MLX4_EN_NUM_SELF_TEST - 2; i++)
266                         strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
267                 if (priv->mdev->dev->caps.loopback_support)
268                         for (; i < MLX4_EN_NUM_SELF_TEST; i++)
269                                 strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
270                 break;
271
272         case ETH_SS_STATS:
273                 /* Add main counters */
274                 for (i = 0; i < NUM_MAIN_STATS; i++)
275                         strcpy(data + (index++) * ETH_GSTRING_LEN, main_strings[i]);
276                 for (i = 0; i< NUM_PORT_STATS; i++)
277                         strcpy(data + (index++) * ETH_GSTRING_LEN,
278                         main_strings[i + NUM_MAIN_STATS]);
279                 for (i = 0; i < priv->tx_ring_num; i++) {
280                         sprintf(data + (index++) * ETH_GSTRING_LEN,
281                                 "tx%d_packets", i);
282                         sprintf(data + (index++) * ETH_GSTRING_LEN,
283                                 "tx%d_bytes", i);
284                 }
285                 for (i = 0; i < priv->rx_ring_num; i++) {
286                         sprintf(data + (index++) * ETH_GSTRING_LEN,
287                                 "rx%d_packets", i);
288                         sprintf(data + (index++) * ETH_GSTRING_LEN,
289                                 "rx%d_bytes", i);
290                 }
291                 for (i = 0; i< NUM_PKT_STATS; i++)
292                         strcpy(data + (index++) * ETH_GSTRING_LEN,
293                         main_strings[i + NUM_MAIN_STATS + NUM_PORT_STATS]);
294                 break;
295         }
296 }
297
298 static int mlx4_en_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
299 {
300         struct mlx4_en_priv *priv = netdev_priv(dev);
301         int trans_type;
302
303         cmd->autoneg = AUTONEG_DISABLE;
304         cmd->supported = SUPPORTED_10000baseT_Full;
305         cmd->advertising = ADVERTISED_10000baseT_Full;
306
307         if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
308                 return -ENOMEM;
309
310         trans_type = priv->port_state.transciver;
311         if (netif_carrier_ok(dev)) {
312                 cmd->speed = priv->port_state.link_speed;
313                 cmd->duplex = DUPLEX_FULL;
314         } else {
315                 cmd->speed = -1;
316                 cmd->duplex = -1;
317         }
318
319         if (trans_type > 0 && trans_type <= 0xC) {
320                 cmd->port = PORT_FIBRE;
321                 cmd->transceiver = XCVR_EXTERNAL;
322                 cmd->supported |= SUPPORTED_FIBRE;
323                 cmd->advertising |= ADVERTISED_FIBRE;
324         } else if (trans_type == 0x80 || trans_type == 0) {
325                 cmd->port = PORT_TP;
326                 cmd->transceiver = XCVR_INTERNAL;
327                 cmd->supported |= SUPPORTED_TP;
328                 cmd->advertising |= ADVERTISED_TP;
329         } else  {
330                 cmd->port = -1;
331                 cmd->transceiver = -1;
332         }
333         return 0;
334 }
335
336 static int mlx4_en_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
337 {
338         if ((cmd->autoneg == AUTONEG_ENABLE) ||
339             (cmd->speed != SPEED_10000) || (cmd->duplex != DUPLEX_FULL))
340                 return -EINVAL;
341
342         /* Nothing to change */
343         return 0;
344 }
345
346 static int mlx4_en_get_coalesce(struct net_device *dev,
347                               struct ethtool_coalesce *coal)
348 {
349         struct mlx4_en_priv *priv = netdev_priv(dev);
350
351         coal->tx_coalesce_usecs = 0;
352         coal->tx_max_coalesced_frames = 0;
353         coal->rx_coalesce_usecs = priv->rx_usecs;
354         coal->rx_max_coalesced_frames = priv->rx_frames;
355
356         coal->pkt_rate_low = priv->pkt_rate_low;
357         coal->rx_coalesce_usecs_low = priv->rx_usecs_low;
358         coal->pkt_rate_high = priv->pkt_rate_high;
359         coal->rx_coalesce_usecs_high = priv->rx_usecs_high;
360         coal->rate_sample_interval = priv->sample_interval;
361         coal->use_adaptive_rx_coalesce = priv->adaptive_rx_coal;
362         return 0;
363 }
364
365 static int mlx4_en_set_coalesce(struct net_device *dev,
366                               struct ethtool_coalesce *coal)
367 {
368         struct mlx4_en_priv *priv = netdev_priv(dev);
369         int err, i;
370
371         priv->rx_frames = (coal->rx_max_coalesced_frames ==
372                            MLX4_EN_AUTO_CONF) ?
373                                 MLX4_EN_RX_COAL_TARGET :
374                                 coal->rx_max_coalesced_frames;
375         priv->rx_usecs = (coal->rx_coalesce_usecs ==
376                           MLX4_EN_AUTO_CONF) ?
377                                 MLX4_EN_RX_COAL_TIME :
378                                 coal->rx_coalesce_usecs;
379
380         /* Set adaptive coalescing params */
381         priv->pkt_rate_low = coal->pkt_rate_low;
382         priv->rx_usecs_low = coal->rx_coalesce_usecs_low;
383         priv->pkt_rate_high = coal->pkt_rate_high;
384         priv->rx_usecs_high = coal->rx_coalesce_usecs_high;
385         priv->sample_interval = coal->rate_sample_interval;
386         priv->adaptive_rx_coal = coal->use_adaptive_rx_coalesce;
387         priv->last_moder_time = MLX4_EN_AUTO_CONF;
388         if (priv->adaptive_rx_coal)
389                 return 0;
390
391         for (i = 0; i < priv->rx_ring_num; i++) {
392                 priv->rx_cq[i].moder_cnt = priv->rx_frames;
393                 priv->rx_cq[i].moder_time = priv->rx_usecs;
394                 err = mlx4_en_set_cq_moder(priv, &priv->rx_cq[i]);
395                 if (err)
396                         return err;
397         }
398         return 0;
399 }
400
401 static int mlx4_en_set_pauseparam(struct net_device *dev,
402                                 struct ethtool_pauseparam *pause)
403 {
404         struct mlx4_en_priv *priv = netdev_priv(dev);
405         struct mlx4_en_dev *mdev = priv->mdev;
406         int err;
407
408         priv->prof->tx_pause = pause->tx_pause != 0;
409         priv->prof->rx_pause = pause->rx_pause != 0;
410         err = mlx4_SET_PORT_general(mdev->dev, priv->port,
411                                     priv->rx_skb_size + ETH_FCS_LEN,
412                                     priv->prof->tx_pause,
413                                     priv->prof->tx_ppp,
414                                     priv->prof->rx_pause,
415                                     priv->prof->rx_ppp);
416         if (err)
417                 en_err(priv, "Failed setting pause params\n");
418
419         return err;
420 }
421
422 static void mlx4_en_get_pauseparam(struct net_device *dev,
423                                  struct ethtool_pauseparam *pause)
424 {
425         struct mlx4_en_priv *priv = netdev_priv(dev);
426
427         pause->tx_pause = priv->prof->tx_pause;
428         pause->rx_pause = priv->prof->rx_pause;
429 }
430
431 static int mlx4_en_set_ringparam(struct net_device *dev,
432                                  struct ethtool_ringparam *param)
433 {
434         struct mlx4_en_priv *priv = netdev_priv(dev);
435         struct mlx4_en_dev *mdev = priv->mdev;
436         u32 rx_size, tx_size;
437         int port_up = 0;
438         int err = 0;
439
440         if (param->rx_jumbo_pending || param->rx_mini_pending)
441                 return -EINVAL;
442
443         rx_size = roundup_pow_of_two(param->rx_pending);
444         rx_size = max_t(u32, rx_size, MLX4_EN_MIN_RX_SIZE);
445         rx_size = min_t(u32, rx_size, MLX4_EN_MAX_RX_SIZE);
446         tx_size = roundup_pow_of_two(param->tx_pending);
447         tx_size = max_t(u32, tx_size, MLX4_EN_MIN_TX_SIZE);
448         tx_size = min_t(u32, tx_size, MLX4_EN_MAX_TX_SIZE);
449
450         if (rx_size == (priv->port_up ? priv->rx_ring[0].actual_size :
451                                         priv->rx_ring[0].size) &&
452             tx_size == priv->tx_ring[0].size)
453                 return 0;
454
455         mutex_lock(&mdev->state_lock);
456         if (priv->port_up) {
457                 port_up = 1;
458                 mlx4_en_stop_port(dev);
459         }
460
461         mlx4_en_free_resources(priv, true);
462
463         priv->prof->tx_ring_size = tx_size;
464         priv->prof->rx_ring_size = rx_size;
465
466         err = mlx4_en_alloc_resources(priv);
467         if (err) {
468                 en_err(priv, "Failed reallocating port resources\n");
469                 goto out;
470         }
471         if (port_up) {
472                 err = mlx4_en_start_port(dev);
473                 if (err)
474                         en_err(priv, "Failed starting port\n");
475         }
476
477 out:
478         mutex_unlock(&mdev->state_lock);
479         return err;
480 }
481
482 static void mlx4_en_get_ringparam(struct net_device *dev,
483                                   struct ethtool_ringparam *param)
484 {
485         struct mlx4_en_priv *priv = netdev_priv(dev);
486
487         memset(param, 0, sizeof(*param));
488         param->rx_max_pending = MLX4_EN_MAX_RX_SIZE;
489         param->tx_max_pending = MLX4_EN_MAX_TX_SIZE;
490         param->rx_pending = priv->port_up ?
491                 priv->rx_ring[0].actual_size : priv->rx_ring[0].size;
492         param->tx_pending = priv->tx_ring[0].size;
493 }
494
495 const struct ethtool_ops mlx4_en_ethtool_ops = {
496         .get_drvinfo = mlx4_en_get_drvinfo,
497         .get_settings = mlx4_en_get_settings,
498         .set_settings = mlx4_en_set_settings,
499 #ifdef NETIF_F_TSO
500         .get_tso = mlx4_en_get_tso,
501         .set_tso = mlx4_en_set_tso,
502 #endif
503         .get_sg = ethtool_op_get_sg,
504         .set_sg = ethtool_op_set_sg,
505         .get_link = ethtool_op_get_link,
506         .get_rx_csum = mlx4_en_get_rx_csum,
507         .set_rx_csum = mlx4_en_set_rx_csum,
508         .get_tx_csum = ethtool_op_get_tx_csum,
509         .set_tx_csum = ethtool_op_set_tx_ipv6_csum,
510         .get_strings = mlx4_en_get_strings,
511         .get_sset_count = mlx4_en_get_sset_count,
512         .get_ethtool_stats = mlx4_en_get_ethtool_stats,
513         .self_test = mlx4_en_self_test,
514         .get_wol = mlx4_en_get_wol,
515         .set_wol = mlx4_en_set_wol,
516         .get_msglevel = mlx4_en_get_msglevel,
517         .set_msglevel = mlx4_en_set_msglevel,
518         .get_coalesce = mlx4_en_get_coalesce,
519         .set_coalesce = mlx4_en_set_coalesce,
520         .get_pauseparam = mlx4_en_get_pauseparam,
521         .set_pauseparam = mlx4_en_set_pauseparam,
522         .get_ringparam = mlx4_en_get_ringparam,
523         .set_ringparam = mlx4_en_set_ringparam,
524         .get_flags = ethtool_op_get_flags,
525 };
526
527
528
529
530