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