2 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/kernel.h>
35 #include <linux/ethtool.h>
36 #include <linux/netdevice.h>
37 #include <linux/mlx4/driver.h>
44 #define EN_ETHTOOL_QP_ATTACH (1ull << 63)
45 #define EN_ETHTOOL_SHORT_MASK cpu_to_be16(0xffff)
46 #define EN_ETHTOOL_WORD_MASK cpu_to_be32(0xffffffff)
48 static int mlx4_en_moderation_update(struct mlx4_en_priv *priv)
53 for (i = 0; i < priv->tx_ring_num; i++) {
54 priv->tx_cq[i]->moder_cnt = priv->tx_frames;
55 priv->tx_cq[i]->moder_time = priv->tx_usecs;
57 err = mlx4_en_set_cq_moder(priv, priv->tx_cq[i]);
63 if (priv->adaptive_rx_coal)
66 for (i = 0; i < priv->rx_ring_num; i++) {
67 priv->rx_cq[i]->moder_cnt = priv->rx_frames;
68 priv->rx_cq[i]->moder_time = priv->rx_usecs;
69 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
71 err = mlx4_en_set_cq_moder(priv, priv->rx_cq[i]);
81 mlx4_en_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
83 struct mlx4_en_priv *priv = netdev_priv(dev);
84 struct mlx4_en_dev *mdev = priv->mdev;
86 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
87 strlcpy(drvinfo->version, DRV_VERSION " (" DRV_RELDATE ")",
88 sizeof(drvinfo->version));
89 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
91 (u16) (mdev->dev->caps.fw_ver >> 32),
92 (u16) ((mdev->dev->caps.fw_ver >> 16) & 0xffff),
93 (u16) (mdev->dev->caps.fw_ver & 0xffff));
94 strlcpy(drvinfo->bus_info, pci_name(mdev->dev->pdev),
95 sizeof(drvinfo->bus_info));
97 drvinfo->regdump_len = 0;
98 drvinfo->eedump_len = 0;
101 static const char mlx4_en_priv_flags[][ETH_GSTRING_LEN] = {
105 static const char main_strings[][ETH_GSTRING_LEN] = {
106 "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
107 "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
108 "rx_length_errors", "rx_over_errors", "rx_crc_errors",
109 "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
110 "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
111 "tx_heartbeat_errors", "tx_window_errors",
113 /* port statistics */
115 "queue_stopped", "wake_queue", "tx_timeout", "rx_alloc_failed",
116 "rx_csum_good", "rx_csum_none", "tx_chksum_offload",
118 /* packet statistics */
119 "broadcast", "rx_prio_0", "rx_prio_1", "rx_prio_2", "rx_prio_3",
120 "rx_prio_4", "rx_prio_5", "rx_prio_6", "rx_prio_7", "tx_prio_0",
121 "tx_prio_1", "tx_prio_2", "tx_prio_3", "tx_prio_4", "tx_prio_5",
122 "tx_prio_6", "tx_prio_7",
124 #define NUM_MAIN_STATS 21
125 #define NUM_ALL_STATS (NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PKT_STATS + NUM_PERF_STATS)
127 static const char mlx4_en_test_names[][ETH_GSTRING_LEN]= {
135 static u32 mlx4_en_get_msglevel(struct net_device *dev)
137 return ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable;
140 static void mlx4_en_set_msglevel(struct net_device *dev, u32 val)
142 ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable = val;
145 static void mlx4_en_get_wol(struct net_device *netdev,
146 struct ethtool_wolinfo *wol)
148 struct mlx4_en_priv *priv = netdev_priv(netdev);
153 if ((priv->port < 1) || (priv->port > 2)) {
154 en_err(priv, "Failed to get WoL information\n");
158 mask = (priv->port == 1) ? MLX4_DEV_CAP_FLAG_WOL_PORT1 :
159 MLX4_DEV_CAP_FLAG_WOL_PORT2;
161 if (!(priv->mdev->dev->caps.flags & mask)) {
167 err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
169 en_err(priv, "Failed to get WoL information\n");
173 if (config & MLX4_EN_WOL_MAGIC)
174 wol->supported = WAKE_MAGIC;
178 if (config & MLX4_EN_WOL_ENABLED)
179 wol->wolopts = WAKE_MAGIC;
184 static int mlx4_en_set_wol(struct net_device *netdev,
185 struct ethtool_wolinfo *wol)
187 struct mlx4_en_priv *priv = netdev_priv(netdev);
192 if ((priv->port < 1) || (priv->port > 2))
195 mask = (priv->port == 1) ? MLX4_DEV_CAP_FLAG_WOL_PORT1 :
196 MLX4_DEV_CAP_FLAG_WOL_PORT2;
198 if (!(priv->mdev->dev->caps.flags & mask))
201 if (wol->supported & ~WAKE_MAGIC)
204 err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
206 en_err(priv, "Failed to get WoL info, unable to modify\n");
210 if (wol->wolopts & WAKE_MAGIC) {
211 config |= MLX4_EN_WOL_DO_MODIFY | MLX4_EN_WOL_ENABLED |
214 config &= ~(MLX4_EN_WOL_ENABLED | MLX4_EN_WOL_MAGIC);
215 config |= MLX4_EN_WOL_DO_MODIFY;
218 err = mlx4_wol_write(priv->mdev->dev, config, priv->port);
220 en_err(priv, "Failed to set WoL information\n");
225 static int mlx4_en_get_sset_count(struct net_device *dev, int sset)
227 struct mlx4_en_priv *priv = netdev_priv(dev);
228 int bit_count = hweight64(priv->stats_bitmap);
232 return (priv->stats_bitmap ? bit_count : NUM_ALL_STATS) +
233 (priv->tx_ring_num * 2) +
234 #ifdef CONFIG_NET_RX_BUSY_POLL
235 (priv->rx_ring_num * 5);
237 (priv->rx_ring_num * 2);
240 return MLX4_EN_NUM_SELF_TEST - !(priv->mdev->dev->caps.flags
241 & MLX4_DEV_CAP_FLAG_UC_LOOPBACK) * 2;
242 case ETH_SS_PRIV_FLAGS:
243 return ARRAY_SIZE(mlx4_en_priv_flags);
249 static void mlx4_en_get_ethtool_stats(struct net_device *dev,
250 struct ethtool_stats *stats, uint64_t *data)
252 struct mlx4_en_priv *priv = netdev_priv(dev);
256 spin_lock_bh(&priv->stats_lock);
258 if (!(priv->stats_bitmap)) {
259 for (i = 0; i < NUM_MAIN_STATS; i++)
261 ((unsigned long *) &priv->stats)[i];
262 for (i = 0; i < NUM_PORT_STATS; i++)
264 ((unsigned long *) &priv->port_stats)[i];
265 for (i = 0; i < NUM_PKT_STATS; i++)
267 ((unsigned long *) &priv->pkstats)[i];
269 for (i = 0; i < NUM_MAIN_STATS; i++) {
270 if ((priv->stats_bitmap >> j) & 1)
272 ((unsigned long *) &priv->stats)[i];
275 for (i = 0; i < NUM_PORT_STATS; i++) {
276 if ((priv->stats_bitmap >> j) & 1)
278 ((unsigned long *) &priv->port_stats)[i];
282 for (i = 0; i < priv->tx_ring_num; i++) {
283 data[index++] = priv->tx_ring[i]->packets;
284 data[index++] = priv->tx_ring[i]->bytes;
286 for (i = 0; i < priv->rx_ring_num; i++) {
287 data[index++] = priv->rx_ring[i]->packets;
288 data[index++] = priv->rx_ring[i]->bytes;
289 #ifdef CONFIG_NET_RX_BUSY_POLL
290 data[index++] = priv->rx_ring[i]->yields;
291 data[index++] = priv->rx_ring[i]->misses;
292 data[index++] = priv->rx_ring[i]->cleaned;
295 spin_unlock_bh(&priv->stats_lock);
299 static void mlx4_en_self_test(struct net_device *dev,
300 struct ethtool_test *etest, u64 *buf)
302 mlx4_en_ex_selftest(dev, &etest->flags, buf);
305 static void mlx4_en_get_strings(struct net_device *dev,
306 uint32_t stringset, uint8_t *data)
308 struct mlx4_en_priv *priv = netdev_priv(dev);
314 for (i = 0; i < MLX4_EN_NUM_SELF_TEST - 2; i++)
315 strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
316 if (priv->mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UC_LOOPBACK)
317 for (; i < MLX4_EN_NUM_SELF_TEST; i++)
318 strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
322 /* Add main counters */
323 if (!priv->stats_bitmap) {
324 for (i = 0; i < NUM_MAIN_STATS; i++)
325 strcpy(data + (index++) * ETH_GSTRING_LEN,
327 for (i = 0; i < NUM_PORT_STATS; i++)
328 strcpy(data + (index++) * ETH_GSTRING_LEN,
331 for (i = 0; i < NUM_PKT_STATS; i++)
332 strcpy(data + (index++) * ETH_GSTRING_LEN,
337 for (i = 0; i < NUM_MAIN_STATS + NUM_PORT_STATS; i++) {
338 if ((priv->stats_bitmap >> i) & 1) {
340 (index++) * ETH_GSTRING_LEN,
343 if (!(priv->stats_bitmap >> i))
346 for (i = 0; i < priv->tx_ring_num; i++) {
347 sprintf(data + (index++) * ETH_GSTRING_LEN,
349 sprintf(data + (index++) * ETH_GSTRING_LEN,
352 for (i = 0; i < priv->rx_ring_num; i++) {
353 sprintf(data + (index++) * ETH_GSTRING_LEN,
355 sprintf(data + (index++) * ETH_GSTRING_LEN,
357 #ifdef CONFIG_NET_RX_BUSY_POLL
358 sprintf(data + (index++) * ETH_GSTRING_LEN,
359 "rx%d_napi_yield", i);
360 sprintf(data + (index++) * ETH_GSTRING_LEN,
362 sprintf(data + (index++) * ETH_GSTRING_LEN,
367 case ETH_SS_PRIV_FLAGS:
368 for (i = 0; i < ARRAY_SIZE(mlx4_en_priv_flags); i++)
369 strcpy(data + i * ETH_GSTRING_LEN,
370 mlx4_en_priv_flags[i]);
376 static int mlx4_en_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
378 struct mlx4_en_priv *priv = netdev_priv(dev);
381 cmd->autoneg = AUTONEG_DISABLE;
382 cmd->supported = SUPPORTED_10000baseT_Full;
383 cmd->advertising = ADVERTISED_10000baseT_Full;
385 if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
388 trans_type = priv->port_state.transciver;
389 if (netif_carrier_ok(dev)) {
390 ethtool_cmd_speed_set(cmd, priv->port_state.link_speed);
391 cmd->duplex = DUPLEX_FULL;
393 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
394 cmd->duplex = DUPLEX_UNKNOWN;
397 if (trans_type > 0 && trans_type <= 0xC) {
398 cmd->port = PORT_FIBRE;
399 cmd->transceiver = XCVR_EXTERNAL;
400 cmd->supported |= SUPPORTED_FIBRE;
401 cmd->advertising |= ADVERTISED_FIBRE;
402 } else if (trans_type == 0x80 || trans_type == 0) {
404 cmd->transceiver = XCVR_INTERNAL;
405 cmd->supported |= SUPPORTED_TP;
406 cmd->advertising |= ADVERTISED_TP;
409 cmd->transceiver = -1;
414 static int mlx4_en_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
416 if ((cmd->autoneg == AUTONEG_ENABLE) ||
417 (ethtool_cmd_speed(cmd) != SPEED_10000) ||
418 (cmd->duplex != DUPLEX_FULL))
421 /* Nothing to change */
425 static int mlx4_en_get_coalesce(struct net_device *dev,
426 struct ethtool_coalesce *coal)
428 struct mlx4_en_priv *priv = netdev_priv(dev);
430 coal->tx_coalesce_usecs = priv->tx_usecs;
431 coal->tx_max_coalesced_frames = priv->tx_frames;
432 coal->tx_max_coalesced_frames_irq = priv->tx_work_limit;
434 coal->rx_coalesce_usecs = priv->rx_usecs;
435 coal->rx_max_coalesced_frames = priv->rx_frames;
437 coal->pkt_rate_low = priv->pkt_rate_low;
438 coal->rx_coalesce_usecs_low = priv->rx_usecs_low;
439 coal->pkt_rate_high = priv->pkt_rate_high;
440 coal->rx_coalesce_usecs_high = priv->rx_usecs_high;
441 coal->rate_sample_interval = priv->sample_interval;
442 coal->use_adaptive_rx_coalesce = priv->adaptive_rx_coal;
447 static int mlx4_en_set_coalesce(struct net_device *dev,
448 struct ethtool_coalesce *coal)
450 struct mlx4_en_priv *priv = netdev_priv(dev);
452 if (!coal->tx_max_coalesced_frames_irq)
455 priv->rx_frames = (coal->rx_max_coalesced_frames ==
457 MLX4_EN_RX_COAL_TARGET :
458 coal->rx_max_coalesced_frames;
459 priv->rx_usecs = (coal->rx_coalesce_usecs ==
461 MLX4_EN_RX_COAL_TIME :
462 coal->rx_coalesce_usecs;
464 /* Setting TX coalescing parameters */
465 if (coal->tx_coalesce_usecs != priv->tx_usecs ||
466 coal->tx_max_coalesced_frames != priv->tx_frames) {
467 priv->tx_usecs = coal->tx_coalesce_usecs;
468 priv->tx_frames = coal->tx_max_coalesced_frames;
471 /* Set adaptive coalescing params */
472 priv->pkt_rate_low = coal->pkt_rate_low;
473 priv->rx_usecs_low = coal->rx_coalesce_usecs_low;
474 priv->pkt_rate_high = coal->pkt_rate_high;
475 priv->rx_usecs_high = coal->rx_coalesce_usecs_high;
476 priv->sample_interval = coal->rate_sample_interval;
477 priv->adaptive_rx_coal = coal->use_adaptive_rx_coalesce;
478 priv->tx_work_limit = coal->tx_max_coalesced_frames_irq;
480 return mlx4_en_moderation_update(priv);
483 static int mlx4_en_set_pauseparam(struct net_device *dev,
484 struct ethtool_pauseparam *pause)
486 struct mlx4_en_priv *priv = netdev_priv(dev);
487 struct mlx4_en_dev *mdev = priv->mdev;
490 priv->prof->tx_pause = pause->tx_pause != 0;
491 priv->prof->rx_pause = pause->rx_pause != 0;
492 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
493 priv->rx_skb_size + ETH_FCS_LEN,
494 priv->prof->tx_pause,
496 priv->prof->rx_pause,
499 en_err(priv, "Failed setting pause params\n");
504 static void mlx4_en_get_pauseparam(struct net_device *dev,
505 struct ethtool_pauseparam *pause)
507 struct mlx4_en_priv *priv = netdev_priv(dev);
509 pause->tx_pause = priv->prof->tx_pause;
510 pause->rx_pause = priv->prof->rx_pause;
513 static int mlx4_en_set_ringparam(struct net_device *dev,
514 struct ethtool_ringparam *param)
516 struct mlx4_en_priv *priv = netdev_priv(dev);
517 struct mlx4_en_dev *mdev = priv->mdev;
518 u32 rx_size, tx_size;
522 if (param->rx_jumbo_pending || param->rx_mini_pending)
525 rx_size = roundup_pow_of_two(param->rx_pending);
526 rx_size = max_t(u32, rx_size, MLX4_EN_MIN_RX_SIZE);
527 rx_size = min_t(u32, rx_size, MLX4_EN_MAX_RX_SIZE);
528 tx_size = roundup_pow_of_two(param->tx_pending);
529 tx_size = max_t(u32, tx_size, MLX4_EN_MIN_TX_SIZE);
530 tx_size = min_t(u32, tx_size, MLX4_EN_MAX_TX_SIZE);
532 if (rx_size == (priv->port_up ? priv->rx_ring[0]->actual_size :
533 priv->rx_ring[0]->size) &&
534 tx_size == priv->tx_ring[0]->size)
537 mutex_lock(&mdev->state_lock);
540 mlx4_en_stop_port(dev, 1);
543 mlx4_en_free_resources(priv);
545 priv->prof->tx_ring_size = tx_size;
546 priv->prof->rx_ring_size = rx_size;
548 err = mlx4_en_alloc_resources(priv);
550 en_err(priv, "Failed reallocating port resources\n");
554 err = mlx4_en_start_port(dev);
556 en_err(priv, "Failed starting port\n");
559 err = mlx4_en_moderation_update(priv);
562 mutex_unlock(&mdev->state_lock);
566 static void mlx4_en_get_ringparam(struct net_device *dev,
567 struct ethtool_ringparam *param)
569 struct mlx4_en_priv *priv = netdev_priv(dev);
571 memset(param, 0, sizeof(*param));
572 param->rx_max_pending = MLX4_EN_MAX_RX_SIZE;
573 param->tx_max_pending = MLX4_EN_MAX_TX_SIZE;
574 param->rx_pending = priv->port_up ?
575 priv->rx_ring[0]->actual_size : priv->rx_ring[0]->size;
576 param->tx_pending = priv->tx_ring[0]->size;
579 static u32 mlx4_en_get_rxfh_indir_size(struct net_device *dev)
581 struct mlx4_en_priv *priv = netdev_priv(dev);
583 return priv->rx_ring_num;
586 static int mlx4_en_get_rxfh(struct net_device *dev, u32 *ring_index, u8 *key)
588 struct mlx4_en_priv *priv = netdev_priv(dev);
589 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
591 size_t n = priv->rx_ring_num;
594 rss_rings = priv->prof->rss_rings ?: priv->rx_ring_num;
597 ring_index[n] = rss_map->qps[n % rss_rings].qpn -
604 static int mlx4_en_set_rxfh(struct net_device *dev, const u32 *ring_index,
607 struct mlx4_en_priv *priv = netdev_priv(dev);
608 struct mlx4_en_dev *mdev = priv->mdev;
614 /* Calculate RSS table size and make sure flows are spread evenly
617 for (i = 0; i < priv->rx_ring_num; i++) {
618 if (i > 0 && !ring_index[i] && !rss_rings)
621 if (ring_index[i] != (i % (rss_rings ?: priv->rx_ring_num)))
626 rss_rings = priv->rx_ring_num;
628 /* RSS table size must be an order of 2 */
629 if (!is_power_of_2(rss_rings))
632 mutex_lock(&mdev->state_lock);
635 mlx4_en_stop_port(dev, 1);
638 priv->prof->rss_rings = rss_rings;
641 err = mlx4_en_start_port(dev);
643 en_err(priv, "Failed starting port\n");
646 mutex_unlock(&mdev->state_lock);
650 #define all_zeros_or_all_ones(field) \
651 ((field) == 0 || (field) == (__force typeof(field))-1)
653 static int mlx4_en_validate_flow(struct net_device *dev,
654 struct ethtool_rxnfc *cmd)
656 struct ethtool_usrip4_spec *l3_mask;
657 struct ethtool_tcpip4_spec *l4_mask;
658 struct ethhdr *eth_mask;
660 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
663 if (cmd->fs.flow_type & FLOW_MAC_EXT) {
664 /* dest mac mask must be ff:ff:ff:ff:ff:ff */
665 if (!is_broadcast_ether_addr(cmd->fs.m_ext.h_dest))
669 switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
672 if (cmd->fs.m_u.tcp_ip4_spec.tos)
674 l4_mask = &cmd->fs.m_u.tcp_ip4_spec;
675 /* don't allow mask which isn't all 0 or 1 */
676 if (!all_zeros_or_all_ones(l4_mask->ip4src) ||
677 !all_zeros_or_all_ones(l4_mask->ip4dst) ||
678 !all_zeros_or_all_ones(l4_mask->psrc) ||
679 !all_zeros_or_all_ones(l4_mask->pdst))
683 l3_mask = &cmd->fs.m_u.usr_ip4_spec;
684 if (l3_mask->l4_4_bytes || l3_mask->tos || l3_mask->proto ||
685 cmd->fs.h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4 ||
686 (!l3_mask->ip4src && !l3_mask->ip4dst) ||
687 !all_zeros_or_all_ones(l3_mask->ip4src) ||
688 !all_zeros_or_all_ones(l3_mask->ip4dst))
692 eth_mask = &cmd->fs.m_u.ether_spec;
693 /* source mac mask must not be set */
694 if (!is_zero_ether_addr(eth_mask->h_source))
697 /* dest mac mask must be ff:ff:ff:ff:ff:ff */
698 if (!is_broadcast_ether_addr(eth_mask->h_dest))
701 if (!all_zeros_or_all_ones(eth_mask->h_proto))
708 if ((cmd->fs.flow_type & FLOW_EXT)) {
709 if (cmd->fs.m_ext.vlan_etype ||
710 !((cmd->fs.m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK)) ==
712 (cmd->fs.m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK)) ==
713 cpu_to_be16(VLAN_VID_MASK)))
716 if (cmd->fs.m_ext.vlan_tci) {
717 if (be16_to_cpu(cmd->fs.h_ext.vlan_tci) >= VLAN_N_VID)
726 static int mlx4_en_ethtool_add_mac_rule(struct ethtool_rxnfc *cmd,
727 struct list_head *rule_list_h,
728 struct mlx4_spec_list *spec_l2,
732 __be64 mac_msk = cpu_to_be64(MLX4_MAC_MASK << 16);
734 spec_l2->id = MLX4_NET_TRANS_RULE_ID_ETH;
735 memcpy(spec_l2->eth.dst_mac_msk, &mac_msk, ETH_ALEN);
736 memcpy(spec_l2->eth.dst_mac, mac, ETH_ALEN);
738 if ((cmd->fs.flow_type & FLOW_EXT) &&
739 (cmd->fs.m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK))) {
740 spec_l2->eth.vlan_id = cmd->fs.h_ext.vlan_tci;
741 spec_l2->eth.vlan_id_msk = cpu_to_be16(VLAN_VID_MASK);
744 list_add_tail(&spec_l2->list, rule_list_h);
749 static int mlx4_en_ethtool_add_mac_rule_by_ipv4(struct mlx4_en_priv *priv,
750 struct ethtool_rxnfc *cmd,
751 struct list_head *rule_list_h,
752 struct mlx4_spec_list *spec_l2,
756 unsigned char mac[ETH_ALEN];
758 if (!ipv4_is_multicast(ipv4_dst)) {
759 if (cmd->fs.flow_type & FLOW_MAC_EXT)
760 memcpy(&mac, cmd->fs.h_ext.h_dest, ETH_ALEN);
762 memcpy(&mac, priv->dev->dev_addr, ETH_ALEN);
764 ip_eth_mc_map(ipv4_dst, mac);
767 return mlx4_en_ethtool_add_mac_rule(cmd, rule_list_h, spec_l2, &mac[0]);
773 static int add_ip_rule(struct mlx4_en_priv *priv,
774 struct ethtool_rxnfc *cmd,
775 struct list_head *list_h)
778 struct mlx4_spec_list *spec_l2 = NULL;
779 struct mlx4_spec_list *spec_l3 = NULL;
780 struct ethtool_usrip4_spec *l3_mask = &cmd->fs.m_u.usr_ip4_spec;
782 spec_l3 = kzalloc(sizeof(*spec_l3), GFP_KERNEL);
783 spec_l2 = kzalloc(sizeof(*spec_l2), GFP_KERNEL);
784 if (!spec_l2 || !spec_l3) {
789 err = mlx4_en_ethtool_add_mac_rule_by_ipv4(priv, cmd, list_h, spec_l2,
791 usr_ip4_spec.ip4dst);
794 spec_l3->id = MLX4_NET_TRANS_RULE_ID_IPV4;
795 spec_l3->ipv4.src_ip = cmd->fs.h_u.usr_ip4_spec.ip4src;
797 spec_l3->ipv4.src_ip_msk = EN_ETHTOOL_WORD_MASK;
798 spec_l3->ipv4.dst_ip = cmd->fs.h_u.usr_ip4_spec.ip4dst;
800 spec_l3->ipv4.dst_ip_msk = EN_ETHTOOL_WORD_MASK;
801 list_add_tail(&spec_l3->list, list_h);
811 static int add_tcp_udp_rule(struct mlx4_en_priv *priv,
812 struct ethtool_rxnfc *cmd,
813 struct list_head *list_h, int proto)
816 struct mlx4_spec_list *spec_l2 = NULL;
817 struct mlx4_spec_list *spec_l3 = NULL;
818 struct mlx4_spec_list *spec_l4 = NULL;
819 struct ethtool_tcpip4_spec *l4_mask = &cmd->fs.m_u.tcp_ip4_spec;
821 spec_l2 = kzalloc(sizeof(*spec_l2), GFP_KERNEL);
822 spec_l3 = kzalloc(sizeof(*spec_l3), GFP_KERNEL);
823 spec_l4 = kzalloc(sizeof(*spec_l4), GFP_KERNEL);
824 if (!spec_l2 || !spec_l3 || !spec_l4) {
829 spec_l3->id = MLX4_NET_TRANS_RULE_ID_IPV4;
831 if (proto == TCP_V4_FLOW) {
832 err = mlx4_en_ethtool_add_mac_rule_by_ipv4(priv, cmd, list_h,
835 tcp_ip4_spec.ip4dst);
838 spec_l4->id = MLX4_NET_TRANS_RULE_ID_TCP;
839 spec_l3->ipv4.src_ip = cmd->fs.h_u.tcp_ip4_spec.ip4src;
840 spec_l3->ipv4.dst_ip = cmd->fs.h_u.tcp_ip4_spec.ip4dst;
841 spec_l4->tcp_udp.src_port = cmd->fs.h_u.tcp_ip4_spec.psrc;
842 spec_l4->tcp_udp.dst_port = cmd->fs.h_u.tcp_ip4_spec.pdst;
844 err = mlx4_en_ethtool_add_mac_rule_by_ipv4(priv, cmd, list_h,
847 udp_ip4_spec.ip4dst);
850 spec_l4->id = MLX4_NET_TRANS_RULE_ID_UDP;
851 spec_l3->ipv4.src_ip = cmd->fs.h_u.udp_ip4_spec.ip4src;
852 spec_l3->ipv4.dst_ip = cmd->fs.h_u.udp_ip4_spec.ip4dst;
853 spec_l4->tcp_udp.src_port = cmd->fs.h_u.udp_ip4_spec.psrc;
854 spec_l4->tcp_udp.dst_port = cmd->fs.h_u.udp_ip4_spec.pdst;
858 spec_l3->ipv4.src_ip_msk = EN_ETHTOOL_WORD_MASK;
860 spec_l3->ipv4.dst_ip_msk = EN_ETHTOOL_WORD_MASK;
863 spec_l4->tcp_udp.src_port_msk = EN_ETHTOOL_SHORT_MASK;
865 spec_l4->tcp_udp.dst_port_msk = EN_ETHTOOL_SHORT_MASK;
867 list_add_tail(&spec_l3->list, list_h);
868 list_add_tail(&spec_l4->list, list_h);
879 static int mlx4_en_ethtool_to_net_trans_rule(struct net_device *dev,
880 struct ethtool_rxnfc *cmd,
881 struct list_head *rule_list_h)
884 struct ethhdr *eth_spec;
885 struct mlx4_spec_list *spec_l2;
886 struct mlx4_en_priv *priv = netdev_priv(dev);
888 err = mlx4_en_validate_flow(dev, cmd);
892 switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
894 spec_l2 = kzalloc(sizeof(*spec_l2), GFP_KERNEL);
898 eth_spec = &cmd->fs.h_u.ether_spec;
899 mlx4_en_ethtool_add_mac_rule(cmd, rule_list_h, spec_l2,
900 ð_spec->h_dest[0]);
901 spec_l2->eth.ether_type = eth_spec->h_proto;
902 if (eth_spec->h_proto)
903 spec_l2->eth.ether_type_enable = 1;
906 err = add_ip_rule(priv, cmd, rule_list_h);
909 err = add_tcp_udp_rule(priv, cmd, rule_list_h, TCP_V4_FLOW);
912 err = add_tcp_udp_rule(priv, cmd, rule_list_h, UDP_V4_FLOW);
919 static int mlx4_en_flow_replace(struct net_device *dev,
920 struct ethtool_rxnfc *cmd)
923 struct mlx4_en_priv *priv = netdev_priv(dev);
924 struct ethtool_flow_id *loc_rule;
925 struct mlx4_spec_list *spec, *tmp_spec;
929 struct mlx4_net_trans_rule rule = {
930 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
933 .promisc_mode = MLX4_FS_REGULAR,
936 rule.port = priv->port;
937 rule.priority = MLX4_DOMAIN_ETHTOOL | cmd->fs.location;
938 INIT_LIST_HEAD(&rule.list);
940 /* Allow direct QP attaches if the EN_ETHTOOL_QP_ATTACH flag is set */
941 if (cmd->fs.ring_cookie == RX_CLS_FLOW_DISC)
942 qpn = priv->drop_qp.qpn;
943 else if (cmd->fs.ring_cookie & EN_ETHTOOL_QP_ATTACH) {
944 qpn = cmd->fs.ring_cookie & (EN_ETHTOOL_QP_ATTACH - 1);
946 if (cmd->fs.ring_cookie >= priv->rx_ring_num) {
947 en_warn(priv, "rxnfc: RX ring (%llu) doesn't exist\n",
948 cmd->fs.ring_cookie);
951 qpn = priv->rss_map.qps[cmd->fs.ring_cookie].qpn;
953 en_warn(priv, "rxnfc: RX ring (%llu) is inactive\n",
954 cmd->fs.ring_cookie);
959 err = mlx4_en_ethtool_to_net_trans_rule(dev, cmd, &rule.list);
963 loc_rule = &priv->ethtool_rules[cmd->fs.location];
965 err = mlx4_flow_detach(priv->mdev->dev, loc_rule->id);
967 en_err(priv, "Fail to detach network rule at location %d. registration id = %llx\n",
968 cmd->fs.location, loc_rule->id);
972 memset(&loc_rule->flow_spec, 0,
973 sizeof(struct ethtool_rx_flow_spec));
974 list_del(&loc_rule->list);
976 err = mlx4_flow_attach(priv->mdev->dev, &rule, ®_id);
978 en_err(priv, "Fail to attach network rule at location %d\n",
982 loc_rule->id = reg_id;
983 memcpy(&loc_rule->flow_spec, &cmd->fs,
984 sizeof(struct ethtool_rx_flow_spec));
985 list_add_tail(&loc_rule->list, &priv->ethtool_list);
988 list_for_each_entry_safe(spec, tmp_spec, &rule.list, list) {
989 list_del(&spec->list);
995 static int mlx4_en_flow_detach(struct net_device *dev,
996 struct ethtool_rxnfc *cmd)
999 struct ethtool_flow_id *rule;
1000 struct mlx4_en_priv *priv = netdev_priv(dev);
1002 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
1005 rule = &priv->ethtool_rules[cmd->fs.location];
1011 err = mlx4_flow_detach(priv->mdev->dev, rule->id);
1013 en_err(priv, "Fail to detach network rule at location %d. registration id = 0x%llx\n",
1014 cmd->fs.location, rule->id);
1018 memset(&rule->flow_spec, 0, sizeof(struct ethtool_rx_flow_spec));
1019 list_del(&rule->list);
1025 static int mlx4_en_get_flow(struct net_device *dev, struct ethtool_rxnfc *cmd,
1029 struct ethtool_flow_id *rule;
1030 struct mlx4_en_priv *priv = netdev_priv(dev);
1032 if (loc < 0 || loc >= MAX_NUM_OF_FS_RULES)
1035 rule = &priv->ethtool_rules[loc];
1037 memcpy(&cmd->fs, &rule->flow_spec,
1038 sizeof(struct ethtool_rx_flow_spec));
1045 static int mlx4_en_get_num_flows(struct mlx4_en_priv *priv)
1049 for (i = 0; i < MAX_NUM_OF_FS_RULES; i++) {
1050 if (priv->ethtool_rules[i].id)
1057 static int mlx4_en_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1060 struct mlx4_en_priv *priv = netdev_priv(dev);
1061 struct mlx4_en_dev *mdev = priv->mdev;
1063 int i = 0, priority = 0;
1065 if ((cmd->cmd == ETHTOOL_GRXCLSRLCNT ||
1066 cmd->cmd == ETHTOOL_GRXCLSRULE ||
1067 cmd->cmd == ETHTOOL_GRXCLSRLALL) &&
1068 (mdev->dev->caps.steering_mode !=
1069 MLX4_STEERING_MODE_DEVICE_MANAGED || !priv->port_up))
1073 case ETHTOOL_GRXRINGS:
1074 cmd->data = priv->rx_ring_num;
1076 case ETHTOOL_GRXCLSRLCNT:
1077 cmd->rule_cnt = mlx4_en_get_num_flows(priv);
1079 case ETHTOOL_GRXCLSRULE:
1080 err = mlx4_en_get_flow(dev, cmd, cmd->fs.location);
1082 case ETHTOOL_GRXCLSRLALL:
1083 while ((!err || err == -ENOENT) && priority < cmd->rule_cnt) {
1084 err = mlx4_en_get_flow(dev, cmd, i);
1086 rule_locs[priority++] = i;
1099 static int mlx4_en_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1102 struct mlx4_en_priv *priv = netdev_priv(dev);
1103 struct mlx4_en_dev *mdev = priv->mdev;
1105 if (mdev->dev->caps.steering_mode !=
1106 MLX4_STEERING_MODE_DEVICE_MANAGED || !priv->port_up)
1110 case ETHTOOL_SRXCLSRLINS:
1111 err = mlx4_en_flow_replace(dev, cmd);
1113 case ETHTOOL_SRXCLSRLDEL:
1114 err = mlx4_en_flow_detach(dev, cmd);
1117 en_warn(priv, "Unsupported ethtool command. (%d)\n", cmd->cmd);
1124 static void mlx4_en_get_channels(struct net_device *dev,
1125 struct ethtool_channels *channel)
1127 struct mlx4_en_priv *priv = netdev_priv(dev);
1129 memset(channel, 0, sizeof(*channel));
1131 channel->max_rx = MAX_RX_RINGS;
1132 channel->max_tx = MLX4_EN_MAX_TX_RING_P_UP;
1134 channel->rx_count = priv->rx_ring_num;
1135 channel->tx_count = priv->tx_ring_num / MLX4_EN_NUM_UP;
1138 static int mlx4_en_set_channels(struct net_device *dev,
1139 struct ethtool_channels *channel)
1141 struct mlx4_en_priv *priv = netdev_priv(dev);
1142 struct mlx4_en_dev *mdev = priv->mdev;
1146 if (channel->other_count || channel->combined_count ||
1147 channel->tx_count > MLX4_EN_MAX_TX_RING_P_UP ||
1148 channel->rx_count > MAX_RX_RINGS ||
1149 !channel->tx_count || !channel->rx_count)
1152 mutex_lock(&mdev->state_lock);
1153 if (priv->port_up) {
1155 mlx4_en_stop_port(dev, 1);
1158 mlx4_en_free_resources(priv);
1160 priv->num_tx_rings_p_up = channel->tx_count;
1161 priv->tx_ring_num = channel->tx_count * MLX4_EN_NUM_UP;
1162 priv->rx_ring_num = channel->rx_count;
1164 err = mlx4_en_alloc_resources(priv);
1166 en_err(priv, "Failed reallocating port resources\n");
1170 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
1171 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
1174 mlx4_en_setup_tc(dev, MLX4_EN_NUM_UP);
1176 en_warn(priv, "Using %d TX rings\n", priv->tx_ring_num);
1177 en_warn(priv, "Using %d RX rings\n", priv->rx_ring_num);
1180 err = mlx4_en_start_port(dev);
1182 en_err(priv, "Failed starting port\n");
1185 err = mlx4_en_moderation_update(priv);
1188 mutex_unlock(&mdev->state_lock);
1192 static int mlx4_en_get_ts_info(struct net_device *dev,
1193 struct ethtool_ts_info *info)
1195 struct mlx4_en_priv *priv = netdev_priv(dev);
1196 struct mlx4_en_dev *mdev = priv->mdev;
1199 ret = ethtool_op_get_ts_info(dev, info);
1203 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS) {
1204 info->so_timestamping |=
1205 SOF_TIMESTAMPING_TX_HARDWARE |
1206 SOF_TIMESTAMPING_RX_HARDWARE |
1207 SOF_TIMESTAMPING_RAW_HARDWARE;
1210 (1 << HWTSTAMP_TX_OFF) |
1211 (1 << HWTSTAMP_TX_ON);
1214 (1 << HWTSTAMP_FILTER_NONE) |
1215 (1 << HWTSTAMP_FILTER_ALL);
1217 if (mdev->ptp_clock)
1218 info->phc_index = ptp_clock_index(mdev->ptp_clock);
1224 static int mlx4_en_set_priv_flags(struct net_device *dev, u32 flags)
1226 struct mlx4_en_priv *priv = netdev_priv(dev);
1227 bool bf_enabled_new = !!(flags & MLX4_EN_PRIV_FLAGS_BLUEFLAME);
1228 bool bf_enabled_old = !!(priv->pflags & MLX4_EN_PRIV_FLAGS_BLUEFLAME);
1231 if (bf_enabled_new == bf_enabled_old)
1232 return 0; /* Nothing to do */
1234 if (bf_enabled_new) {
1235 bool bf_supported = true;
1237 for (i = 0; i < priv->tx_ring_num; i++)
1238 bf_supported &= priv->tx_ring[i]->bf_alloced;
1240 if (!bf_supported) {
1241 en_err(priv, "BlueFlame is not supported\n");
1245 priv->pflags |= MLX4_EN_PRIV_FLAGS_BLUEFLAME;
1247 priv->pflags &= ~MLX4_EN_PRIV_FLAGS_BLUEFLAME;
1250 for (i = 0; i < priv->tx_ring_num; i++)
1251 priv->tx_ring[i]->bf_enabled = bf_enabled_new;
1253 en_info(priv, "BlueFlame %s\n",
1254 bf_enabled_new ? "Enabled" : "Disabled");
1259 static u32 mlx4_en_get_priv_flags(struct net_device *dev)
1261 struct mlx4_en_priv *priv = netdev_priv(dev);
1263 return priv->pflags;
1267 const struct ethtool_ops mlx4_en_ethtool_ops = {
1268 .get_drvinfo = mlx4_en_get_drvinfo,
1269 .get_settings = mlx4_en_get_settings,
1270 .set_settings = mlx4_en_set_settings,
1271 .get_link = ethtool_op_get_link,
1272 .get_strings = mlx4_en_get_strings,
1273 .get_sset_count = mlx4_en_get_sset_count,
1274 .get_ethtool_stats = mlx4_en_get_ethtool_stats,
1275 .self_test = mlx4_en_self_test,
1276 .get_wol = mlx4_en_get_wol,
1277 .set_wol = mlx4_en_set_wol,
1278 .get_msglevel = mlx4_en_get_msglevel,
1279 .set_msglevel = mlx4_en_set_msglevel,
1280 .get_coalesce = mlx4_en_get_coalesce,
1281 .set_coalesce = mlx4_en_set_coalesce,
1282 .get_pauseparam = mlx4_en_get_pauseparam,
1283 .set_pauseparam = mlx4_en_set_pauseparam,
1284 .get_ringparam = mlx4_en_get_ringparam,
1285 .set_ringparam = mlx4_en_set_ringparam,
1286 .get_rxnfc = mlx4_en_get_rxnfc,
1287 .set_rxnfc = mlx4_en_set_rxnfc,
1288 .get_rxfh_indir_size = mlx4_en_get_rxfh_indir_size,
1289 .get_rxfh = mlx4_en_get_rxfh,
1290 .set_rxfh = mlx4_en_set_rxfh,
1291 .get_channels = mlx4_en_get_channels,
1292 .set_channels = mlx4_en_set_channels,
1293 .get_ts_info = mlx4_en_get_ts_info,
1294 .set_priv_flags = mlx4_en_set_priv_flags,
1295 .get_priv_flags = mlx4_en_get_priv_flags,