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