Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux...
[linux-2.6] / drivers / net / mlx4 / en_params.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 #define MLX4_EN_PARM_INT(X, def_val, desc) \
42         static unsigned int X = def_val;\
43         module_param(X , uint, 0444); \
44         MODULE_PARM_DESC(X, desc);
45
46
47 /*
48  * Device scope module parameters
49  */
50
51
52 /* Use a XOR rathern than Toeplitz hash function for RSS */
53 MLX4_EN_PARM_INT(rss_xor, 0, "Use XOR hash function for RSS");
54
55 /* RSS hash type mask - default to <saddr, daddr, sport, dport> */
56 MLX4_EN_PARM_INT(rss_mask, 0xf, "RSS hash type bitmask");
57
58 /* Number of LRO sessions per Rx ring (rounded up to a power of two) */
59 MLX4_EN_PARM_INT(num_lro, MLX4_EN_MAX_LRO_DESCRIPTORS,
60                  "Number of LRO sessions per ring or disabled (0)");
61
62 /* Priority pausing */
63 MLX4_EN_PARM_INT(pfctx, 0, "Priority based Flow Control policy on TX[7:0]."
64                            " Per priority bit mask");
65 MLX4_EN_PARM_INT(pfcrx, 0, "Priority based Flow Control policy on RX[7:0]."
66                            " Per priority bit mask");
67
68 MLX4_EN_PARM_INT(rx_ring_num1, 0, "Number or Rx rings for port 1 (0 = #cores)");
69 MLX4_EN_PARM_INT(rx_ring_num2, 0, "Number or Rx rings for port 2 (0 = #cores)");
70
71 MLX4_EN_PARM_INT(tx_ring_size1, MLX4_EN_AUTO_CONF, "Tx ring size for port 1");
72 MLX4_EN_PARM_INT(tx_ring_size2, MLX4_EN_AUTO_CONF, "Tx ring size for port 2");
73 MLX4_EN_PARM_INT(rx_ring_size1, MLX4_EN_AUTO_CONF, "Rx ring size for port 1");
74 MLX4_EN_PARM_INT(rx_ring_size2, MLX4_EN_AUTO_CONF, "Rx ring size for port 2");
75
76
77 int mlx4_en_get_profile(struct mlx4_en_dev *mdev)
78 {
79         struct mlx4_en_profile *params = &mdev->profile;
80         int i;
81
82         params->rss_xor = (rss_xor != 0);
83         params->rss_mask = rss_mask & 0x1f;
84         params->num_lro = min_t(int, num_lro , MLX4_EN_MAX_LRO_DESCRIPTORS);
85         for (i = 1; i <= MLX4_MAX_PORTS; i++) {
86                 params->prof[i].rx_pause = 1;
87                 params->prof[i].rx_ppp = pfcrx;
88                 params->prof[i].tx_pause = 1;
89                 params->prof[i].tx_ppp = pfctx;
90         }
91         if (pfcrx || pfctx) {
92                 params->prof[1].tx_ring_num = MLX4_EN_TX_RING_NUM;
93                 params->prof[2].tx_ring_num = MLX4_EN_TX_RING_NUM;
94         } else {
95                 params->prof[1].tx_ring_num = 1;
96                 params->prof[2].tx_ring_num = 1;
97         }
98         params->prof[1].rx_ring_num = min_t(int, rx_ring_num1, MAX_RX_RINGS);
99         params->prof[2].rx_ring_num = min_t(int, rx_ring_num2, MAX_RX_RINGS);
100
101         if (tx_ring_size1 == MLX4_EN_AUTO_CONF)
102                 tx_ring_size1 = MLX4_EN_DEF_TX_RING_SIZE;
103         params->prof[1].tx_ring_size =
104                 (tx_ring_size1 < MLX4_EN_MIN_TX_SIZE) ?
105                  MLX4_EN_MIN_TX_SIZE : roundup_pow_of_two(tx_ring_size1);
106
107         if (tx_ring_size2 == MLX4_EN_AUTO_CONF)
108                 tx_ring_size2 = MLX4_EN_DEF_TX_RING_SIZE;
109         params->prof[2].tx_ring_size =
110                 (tx_ring_size2 < MLX4_EN_MIN_TX_SIZE) ?
111                  MLX4_EN_MIN_TX_SIZE : roundup_pow_of_two(tx_ring_size2);
112
113         if (rx_ring_size1 == MLX4_EN_AUTO_CONF)
114                 rx_ring_size1 = MLX4_EN_DEF_RX_RING_SIZE;
115         params->prof[1].rx_ring_size =
116                 (rx_ring_size1 < MLX4_EN_MIN_RX_SIZE) ?
117                  MLX4_EN_MIN_RX_SIZE : roundup_pow_of_two(rx_ring_size1);
118
119         if (rx_ring_size2 == MLX4_EN_AUTO_CONF)
120                 rx_ring_size2 = MLX4_EN_DEF_RX_RING_SIZE;
121         params->prof[2].rx_ring_size =
122                 (rx_ring_size2 < MLX4_EN_MIN_RX_SIZE) ?
123                  MLX4_EN_MIN_RX_SIZE : roundup_pow_of_two(rx_ring_size2);
124         return 0;
125 }
126
127
128 /*
129  * Ethtool support
130  */
131
132 static void mlx4_en_update_lro_stats(struct mlx4_en_priv *priv)
133 {
134         int i;
135
136         priv->port_stats.lro_aggregated = 0;
137         priv->port_stats.lro_flushed = 0;
138         priv->port_stats.lro_no_desc = 0;
139
140         for (i = 0; i < priv->rx_ring_num; i++) {
141                 priv->port_stats.lro_aggregated += priv->rx_ring[i].lro.stats.aggregated;
142                 priv->port_stats.lro_flushed += priv->rx_ring[i].lro.stats.flushed;
143                 priv->port_stats.lro_no_desc += priv->rx_ring[i].lro.stats.no_desc;
144         }
145 }
146
147 static void
148 mlx4_en_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
149 {
150         struct mlx4_en_priv *priv = netdev_priv(dev);
151         struct mlx4_en_dev *mdev = priv->mdev;
152
153         sprintf(drvinfo->driver, DRV_NAME " (%s)", mdev->dev->board_id);
154         strncpy(drvinfo->version, DRV_VERSION " (" DRV_RELDATE ")", 32);
155         sprintf(drvinfo->fw_version, "%d.%d.%d",
156                 (u16) (mdev->dev->caps.fw_ver >> 32),
157                 (u16) ((mdev->dev->caps.fw_ver >> 16) & 0xffff),
158                 (u16) (mdev->dev->caps.fw_ver & 0xffff));
159         strncpy(drvinfo->bus_info, pci_name(mdev->dev->pdev), 32);
160         drvinfo->n_stats = 0;
161         drvinfo->regdump_len = 0;
162         drvinfo->eedump_len = 0;
163 }
164
165 static u32 mlx4_en_get_tso(struct net_device *dev)
166 {
167         return (dev->features & NETIF_F_TSO) != 0;
168 }
169
170 static int mlx4_en_set_tso(struct net_device *dev, u32 data)
171 {
172         struct mlx4_en_priv *priv = netdev_priv(dev);
173
174         if (data) {
175                 if (!priv->mdev->LSO_support)
176                         return -EPERM;
177                 dev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
178         } else
179                 dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
180         return 0;
181 }
182
183 static u32 mlx4_en_get_rx_csum(struct net_device *dev)
184 {
185         struct mlx4_en_priv *priv = netdev_priv(dev);
186         return priv->rx_csum;
187 }
188
189 static int mlx4_en_set_rx_csum(struct net_device *dev, u32 data)
190 {
191         struct mlx4_en_priv *priv = netdev_priv(dev);
192         priv->rx_csum = (data != 0);
193         return 0;
194 }
195
196 static const char main_strings[][ETH_GSTRING_LEN] = {
197         "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
198         "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
199         "rx_length_errors", "rx_over_errors", "rx_crc_errors",
200         "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
201         "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
202         "tx_heartbeat_errors", "tx_window_errors",
203
204         /* port statistics */
205         "lro_aggregated", "lro_flushed", "lro_no_desc", "tso_packets",
206         "queue_stopped", "wake_queue", "tx_timeout", "rx_alloc_failed",
207         "rx_csum_good", "rx_csum_none", "tx_chksum_offload",
208
209         /* packet statistics */
210         "broadcast", "rx_prio_0", "rx_prio_1", "rx_prio_2", "rx_prio_3",
211         "rx_prio_4", "rx_prio_5", "rx_prio_6", "rx_prio_7", "tx_prio_0",
212         "tx_prio_1", "tx_prio_2", "tx_prio_3", "tx_prio_4", "tx_prio_5",
213         "tx_prio_6", "tx_prio_7",
214 };
215 #define NUM_MAIN_STATS  21
216 #define NUM_ALL_STATS   (NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PKT_STATS + NUM_PERF_STATS)
217
218 static u32 mlx4_en_get_msglevel(struct net_device *dev)
219 {
220         return ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable;
221 }
222
223 static void mlx4_en_set_msglevel(struct net_device *dev, u32 val)
224 {
225         ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable = val;
226 }
227
228 static void mlx4_en_get_wol(struct net_device *netdev,
229                             struct ethtool_wolinfo *wol)
230 {
231         wol->supported = 0;
232         wol->wolopts = 0;
233
234         return;
235 }
236
237 static int mlx4_en_get_sset_count(struct net_device *dev, int sset)
238 {
239         struct mlx4_en_priv *priv = netdev_priv(dev);
240
241         if (sset != ETH_SS_STATS)
242                 return -EOPNOTSUPP;
243
244         return NUM_ALL_STATS + (priv->tx_ring_num + priv->rx_ring_num) * 2;
245 }
246
247 static void mlx4_en_get_ethtool_stats(struct net_device *dev,
248                 struct ethtool_stats *stats, uint64_t *data)
249 {
250         struct mlx4_en_priv *priv = netdev_priv(dev);
251         int index = 0;
252         int i;
253
254         spin_lock_bh(&priv->stats_lock);
255
256         mlx4_en_update_lro_stats(priv);
257
258         for (i = 0; i < NUM_MAIN_STATS; i++)
259                 data[index++] = ((unsigned long *) &priv->stats)[i];
260         for (i = 0; i < NUM_PORT_STATS; i++)
261                 data[index++] = ((unsigned long *) &priv->port_stats)[i];
262         for (i = 0; i < priv->tx_ring_num; i++) {
263                 data[index++] = priv->tx_ring[i].packets;
264                 data[index++] = priv->tx_ring[i].bytes;
265         }
266         for (i = 0; i < priv->rx_ring_num; i++) {
267                 data[index++] = priv->rx_ring[i].packets;
268                 data[index++] = priv->rx_ring[i].bytes;
269         }
270         for (i = 0; i < NUM_PKT_STATS; i++)
271                 data[index++] = ((unsigned long *) &priv->pkstats)[i];
272         spin_unlock_bh(&priv->stats_lock);
273
274 }
275
276 static void mlx4_en_get_strings(struct net_device *dev,
277                                 uint32_t stringset, uint8_t *data)
278 {
279         struct mlx4_en_priv *priv = netdev_priv(dev);
280         int index = 0;
281         int i;
282
283         if (stringset != ETH_SS_STATS)
284                 return;
285
286         /* Add main counters */
287         for (i = 0; i < NUM_MAIN_STATS; i++)
288                 strcpy(data + (index++) * ETH_GSTRING_LEN, main_strings[i]);
289         for (i = 0; i < NUM_PORT_STATS; i++)
290                 strcpy(data + (index++) * ETH_GSTRING_LEN,
291                         main_strings[i + NUM_MAIN_STATS]);
292         for (i = 0; i < priv->tx_ring_num; i++) {
293                 sprintf(data + (index++) * ETH_GSTRING_LEN,
294                         "tx%d_packets", i);
295                 sprintf(data + (index++) * ETH_GSTRING_LEN,
296                         "tx%d_bytes", i);
297         }
298         for (i = 0; i < priv->rx_ring_num; i++) {
299                 sprintf(data + (index++) * ETH_GSTRING_LEN,
300                         "rx%d_packets", i);
301                 sprintf(data + (index++) * ETH_GSTRING_LEN,
302                         "rx%d_bytes", i);
303         }
304         for (i = 0; i < NUM_PKT_STATS; i++)
305                 strcpy(data + (index++) * ETH_GSTRING_LEN,
306                         main_strings[i + NUM_MAIN_STATS + NUM_PORT_STATS]);
307 }
308
309 static int mlx4_en_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
310 {
311         cmd->autoneg = AUTONEG_DISABLE;
312         cmd->supported = SUPPORTED_10000baseT_Full;
313         cmd->advertising = SUPPORTED_10000baseT_Full;
314         if (netif_carrier_ok(dev)) {
315                 cmd->speed = SPEED_10000;
316                 cmd->duplex = DUPLEX_FULL;
317         } else {
318                 cmd->speed = -1;
319                 cmd->duplex = -1;
320         }
321         return 0;
322 }
323
324 static int mlx4_en_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
325 {
326         if ((cmd->autoneg == AUTONEG_ENABLE) ||
327             (cmd->speed != SPEED_10000) || (cmd->duplex != DUPLEX_FULL))
328                 return -EINVAL;
329
330         /* Nothing to change */
331         return 0;
332 }
333
334 static int mlx4_en_get_coalesce(struct net_device *dev,
335                               struct ethtool_coalesce *coal)
336 {
337         struct mlx4_en_priv *priv = netdev_priv(dev);
338
339         coal->tx_coalesce_usecs = 0;
340         coal->tx_max_coalesced_frames = 0;
341         coal->rx_coalesce_usecs = priv->rx_usecs;
342         coal->rx_max_coalesced_frames = priv->rx_frames;
343
344         coal->pkt_rate_low = priv->pkt_rate_low;
345         coal->rx_coalesce_usecs_low = priv->rx_usecs_low;
346         coal->pkt_rate_high = priv->pkt_rate_high;
347         coal->rx_coalesce_usecs_high = priv->rx_usecs_high;
348         coal->rate_sample_interval = priv->sample_interval;
349         coal->use_adaptive_rx_coalesce = priv->adaptive_rx_coal;
350         return 0;
351 }
352
353 static int mlx4_en_set_coalesce(struct net_device *dev,
354                               struct ethtool_coalesce *coal)
355 {
356         struct mlx4_en_priv *priv = netdev_priv(dev);
357         int err, i;
358
359         priv->rx_frames = (coal->rx_max_coalesced_frames ==
360                            MLX4_EN_AUTO_CONF) ?
361                                 MLX4_EN_RX_COAL_TARGET /
362                                 priv->dev->mtu + 1 :
363                                 coal->rx_max_coalesced_frames;
364         priv->rx_usecs = (coal->rx_coalesce_usecs ==
365                           MLX4_EN_AUTO_CONF) ?
366                                 MLX4_EN_RX_COAL_TIME :
367                                 coal->rx_coalesce_usecs;
368
369         /* Set adaptive coalescing params */
370         priv->pkt_rate_low = coal->pkt_rate_low;
371         priv->rx_usecs_low = coal->rx_coalesce_usecs_low;
372         priv->pkt_rate_high = coal->pkt_rate_high;
373         priv->rx_usecs_high = coal->rx_coalesce_usecs_high;
374         priv->sample_interval = coal->rate_sample_interval;
375         priv->adaptive_rx_coal = coal->use_adaptive_rx_coalesce;
376         priv->last_moder_time = MLX4_EN_AUTO_CONF;
377         if (priv->adaptive_rx_coal)
378                 return 0;
379
380         for (i = 0; i < priv->rx_ring_num; i++) {
381                 priv->rx_cq[i].moder_cnt = priv->rx_frames;
382                 priv->rx_cq[i].moder_time = priv->rx_usecs;
383                 err = mlx4_en_set_cq_moder(priv, &priv->rx_cq[i]);
384                 if (err)
385                         return err;
386         }
387         return 0;
388 }
389
390 static int mlx4_en_set_pauseparam(struct net_device *dev,
391                                 struct ethtool_pauseparam *pause)
392 {
393         struct mlx4_en_priv *priv = netdev_priv(dev);
394         struct mlx4_en_dev *mdev = priv->mdev;
395         int err;
396
397         priv->prof->tx_pause = pause->tx_pause != 0;
398         priv->prof->rx_pause = pause->rx_pause != 0;
399         err = mlx4_SET_PORT_general(mdev->dev, priv->port,
400                                     priv->rx_skb_size + ETH_FCS_LEN,
401                                     priv->prof->tx_pause,
402                                     priv->prof->tx_ppp,
403                                     priv->prof->rx_pause,
404                                     priv->prof->rx_ppp);
405         if (err)
406                 mlx4_err(mdev, "Failed setting pause params to\n");
407
408         return err;
409 }
410
411 static void mlx4_en_get_pauseparam(struct net_device *dev,
412                                  struct ethtool_pauseparam *pause)
413 {
414         struct mlx4_en_priv *priv = netdev_priv(dev);
415
416         pause->tx_pause = priv->prof->tx_pause;
417         pause->rx_pause = priv->prof->rx_pause;
418 }
419
420 static void mlx4_en_get_ringparam(struct net_device *dev,
421                                   struct ethtool_ringparam *param)
422 {
423         struct mlx4_en_priv *priv = netdev_priv(dev);
424         struct mlx4_en_dev *mdev = priv->mdev;
425
426         memset(param, 0, sizeof(*param));
427         param->rx_max_pending = mdev->dev->caps.max_rq_sg;
428         param->tx_max_pending = mdev->dev->caps.max_sq_sg;
429         param->rx_pending = mdev->profile.prof[priv->port].rx_ring_size;
430         param->tx_pending = mdev->profile.prof[priv->port].tx_ring_size;
431 }
432
433 const struct ethtool_ops mlx4_en_ethtool_ops = {
434         .get_drvinfo = mlx4_en_get_drvinfo,
435         .get_settings = mlx4_en_get_settings,
436         .set_settings = mlx4_en_set_settings,
437 #ifdef NETIF_F_TSO
438         .get_tso = mlx4_en_get_tso,
439         .set_tso = mlx4_en_set_tso,
440 #endif
441         .get_sg = ethtool_op_get_sg,
442         .set_sg = ethtool_op_set_sg,
443         .get_link = ethtool_op_get_link,
444         .get_rx_csum = mlx4_en_get_rx_csum,
445         .set_rx_csum = mlx4_en_set_rx_csum,
446         .get_tx_csum = ethtool_op_get_tx_csum,
447         .set_tx_csum = ethtool_op_set_tx_ipv6_csum,
448         .get_strings = mlx4_en_get_strings,
449         .get_sset_count = mlx4_en_get_sset_count,
450         .get_ethtool_stats = mlx4_en_get_ethtool_stats,
451         .get_wol = mlx4_en_get_wol,
452         .get_msglevel = mlx4_en_get_msglevel,
453         .set_msglevel = mlx4_en_set_msglevel,
454         .get_coalesce = mlx4_en_get_coalesce,
455         .set_coalesce = mlx4_en_set_coalesce,
456         .get_pauseparam = mlx4_en_get_pauseparam,
457         .set_pauseparam = mlx4_en_set_pauseparam,
458         .get_ringparam = mlx4_en_get_ringparam,
459         .get_flags = ethtool_op_get_flags,
460         .set_flags = ethtool_op_set_flags,
461 };
462
463
464
465
466