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