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 */
116 "queue_stopped", "wake_queue", "tx_timeout", "rx_alloc_failed",
117 "rx_csum_good", "rx_csum_none", "tx_chksum_offload",
119 /* packet statistics */
120 "broadcast", "rx_prio_0", "rx_prio_1", "rx_prio_2", "rx_prio_3",
121 "rx_prio_4", "rx_prio_5", "rx_prio_6", "rx_prio_7", "tx_prio_0",
122 "tx_prio_1", "tx_prio_2", "tx_prio_3", "tx_prio_4", "tx_prio_5",
123 "tx_prio_6", "tx_prio_7",
125 #define NUM_MAIN_STATS 21
126 #define NUM_ALL_STATS (NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PKT_STATS + NUM_PERF_STATS)
128 static const char mlx4_en_test_names[][ETH_GSTRING_LEN]= {
136 static u32 mlx4_en_get_msglevel(struct net_device *dev)
138 return ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable;
141 static void mlx4_en_set_msglevel(struct net_device *dev, u32 val)
143 ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable = val;
146 static void mlx4_en_get_wol(struct net_device *netdev,
147 struct ethtool_wolinfo *wol)
149 struct mlx4_en_priv *priv = netdev_priv(netdev);
154 if ((priv->port < 1) || (priv->port > 2)) {
155 en_err(priv, "Failed to get WoL information\n");
159 mask = (priv->port == 1) ? MLX4_DEV_CAP_FLAG_WOL_PORT1 :
160 MLX4_DEV_CAP_FLAG_WOL_PORT2;
162 if (!(priv->mdev->dev->caps.flags & mask)) {
168 err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
170 en_err(priv, "Failed to get WoL information\n");
174 if (config & MLX4_EN_WOL_MAGIC)
175 wol->supported = WAKE_MAGIC;
179 if (config & MLX4_EN_WOL_ENABLED)
180 wol->wolopts = WAKE_MAGIC;
185 static int mlx4_en_set_wol(struct net_device *netdev,
186 struct ethtool_wolinfo *wol)
188 struct mlx4_en_priv *priv = netdev_priv(netdev);
193 if ((priv->port < 1) || (priv->port > 2))
196 mask = (priv->port == 1) ? MLX4_DEV_CAP_FLAG_WOL_PORT1 :
197 MLX4_DEV_CAP_FLAG_WOL_PORT2;
199 if (!(priv->mdev->dev->caps.flags & mask))
202 if (wol->supported & ~WAKE_MAGIC)
205 err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
207 en_err(priv, "Failed to get WoL info, unable to modify\n");
211 if (wol->wolopts & WAKE_MAGIC) {
212 config |= MLX4_EN_WOL_DO_MODIFY | MLX4_EN_WOL_ENABLED |
215 config &= ~(MLX4_EN_WOL_ENABLED | MLX4_EN_WOL_MAGIC);
216 config |= MLX4_EN_WOL_DO_MODIFY;
219 err = mlx4_wol_write(priv->mdev->dev, config, priv->port);
221 en_err(priv, "Failed to set WoL information\n");
226 static int mlx4_en_get_sset_count(struct net_device *dev, int sset)
228 struct mlx4_en_priv *priv = netdev_priv(dev);
229 int bit_count = hweight64(priv->stats_bitmap);
233 return (priv->stats_bitmap ? bit_count : NUM_ALL_STATS) +
234 (priv->tx_ring_num * 2) +
235 #ifdef CONFIG_NET_RX_BUSY_POLL
236 (priv->rx_ring_num * 5);
238 (priv->rx_ring_num * 2);
241 return MLX4_EN_NUM_SELF_TEST - !(priv->mdev->dev->caps.flags
242 & MLX4_DEV_CAP_FLAG_UC_LOOPBACK) * 2;
243 case ETH_SS_PRIV_FLAGS:
244 return ARRAY_SIZE(mlx4_en_priv_flags);
250 static void mlx4_en_get_ethtool_stats(struct net_device *dev,
251 struct ethtool_stats *stats, uint64_t *data)
253 struct mlx4_en_priv *priv = netdev_priv(dev);
257 spin_lock_bh(&priv->stats_lock);
259 if (!(priv->stats_bitmap)) {
260 for (i = 0; i < NUM_MAIN_STATS; i++)
262 ((unsigned long *) &priv->stats)[i];
263 for (i = 0; i < NUM_PORT_STATS; i++)
265 ((unsigned long *) &priv->port_stats)[i];
266 for (i = 0; i < NUM_PKT_STATS; i++)
268 ((unsigned long *) &priv->pkstats)[i];
270 for (i = 0; i < NUM_MAIN_STATS; i++) {
271 if ((priv->stats_bitmap >> j) & 1)
273 ((unsigned long *) &priv->stats)[i];
276 for (i = 0; i < NUM_PORT_STATS; i++) {
277 if ((priv->stats_bitmap >> j) & 1)
279 ((unsigned long *) &priv->port_stats)[i];
283 for (i = 0; i < priv->tx_ring_num; i++) {
284 data[index++] = priv->tx_ring[i]->packets;
285 data[index++] = priv->tx_ring[i]->bytes;
287 for (i = 0; i < priv->rx_ring_num; i++) {
288 data[index++] = priv->rx_ring[i]->packets;
289 data[index++] = priv->rx_ring[i]->bytes;
290 #ifdef CONFIG_NET_RX_BUSY_POLL
291 data[index++] = priv->rx_ring[i]->yields;
292 data[index++] = priv->rx_ring[i]->misses;
293 data[index++] = priv->rx_ring[i]->cleaned;
296 spin_unlock_bh(&priv->stats_lock);
300 static void mlx4_en_self_test(struct net_device *dev,
301 struct ethtool_test *etest, u64 *buf)
303 mlx4_en_ex_selftest(dev, &etest->flags, buf);
306 static void mlx4_en_get_strings(struct net_device *dev,
307 uint32_t stringset, uint8_t *data)
309 struct mlx4_en_priv *priv = netdev_priv(dev);
315 for (i = 0; i < MLX4_EN_NUM_SELF_TEST - 2; i++)
316 strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
317 if (priv->mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UC_LOOPBACK)
318 for (; i < MLX4_EN_NUM_SELF_TEST; i++)
319 strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
323 /* Add main counters */
324 if (!priv->stats_bitmap) {
325 for (i = 0; i < NUM_MAIN_STATS; i++)
326 strcpy(data + (index++) * ETH_GSTRING_LEN,
328 for (i = 0; i < NUM_PORT_STATS; i++)
329 strcpy(data + (index++) * ETH_GSTRING_LEN,
332 for (i = 0; i < NUM_PKT_STATS; i++)
333 strcpy(data + (index++) * ETH_GSTRING_LEN,
338 for (i = 0; i < NUM_MAIN_STATS + NUM_PORT_STATS; i++) {
339 if ((priv->stats_bitmap >> i) & 1) {
341 (index++) * ETH_GSTRING_LEN,
344 if (!(priv->stats_bitmap >> i))
347 for (i = 0; i < priv->tx_ring_num; i++) {
348 sprintf(data + (index++) * ETH_GSTRING_LEN,
350 sprintf(data + (index++) * ETH_GSTRING_LEN,
353 for (i = 0; i < priv->rx_ring_num; i++) {
354 sprintf(data + (index++) * ETH_GSTRING_LEN,
356 sprintf(data + (index++) * ETH_GSTRING_LEN,
358 #ifdef CONFIG_NET_RX_BUSY_POLL
359 sprintf(data + (index++) * ETH_GSTRING_LEN,
360 "rx%d_napi_yield", i);
361 sprintf(data + (index++) * ETH_GSTRING_LEN,
363 sprintf(data + (index++) * ETH_GSTRING_LEN,
368 case ETH_SS_PRIV_FLAGS:
369 for (i = 0; i < ARRAY_SIZE(mlx4_en_priv_flags); i++)
370 strcpy(data + i * ETH_GSTRING_LEN,
371 mlx4_en_priv_flags[i]);
377 static int mlx4_en_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
379 struct mlx4_en_priv *priv = netdev_priv(dev);
382 cmd->autoneg = AUTONEG_DISABLE;
383 cmd->supported = SUPPORTED_10000baseT_Full;
384 cmd->advertising = ADVERTISED_10000baseT_Full;
386 if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
389 trans_type = priv->port_state.transciver;
390 if (netif_carrier_ok(dev)) {
391 ethtool_cmd_speed_set(cmd, priv->port_state.link_speed);
392 cmd->duplex = DUPLEX_FULL;
394 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
395 cmd->duplex = DUPLEX_UNKNOWN;
398 if (trans_type > 0 && trans_type <= 0xC) {
399 cmd->port = PORT_FIBRE;
400 cmd->transceiver = XCVR_EXTERNAL;
401 cmd->supported |= SUPPORTED_FIBRE;
402 cmd->advertising |= ADVERTISED_FIBRE;
403 } else if (trans_type == 0x80 || trans_type == 0) {
405 cmd->transceiver = XCVR_INTERNAL;
406 cmd->supported |= SUPPORTED_TP;
407 cmd->advertising |= ADVERTISED_TP;
410 cmd->transceiver = -1;
415 static int mlx4_en_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
417 if ((cmd->autoneg == AUTONEG_ENABLE) ||
418 (ethtool_cmd_speed(cmd) != SPEED_10000) ||
419 (cmd->duplex != DUPLEX_FULL))
422 /* Nothing to change */
426 static int mlx4_en_get_coalesce(struct net_device *dev,
427 struct ethtool_coalesce *coal)
429 struct mlx4_en_priv *priv = netdev_priv(dev);
431 coal->tx_coalesce_usecs = priv->tx_usecs;
432 coal->tx_max_coalesced_frames = priv->tx_frames;
433 coal->tx_max_coalesced_frames_irq = priv->tx_work_limit;
435 coal->rx_coalesce_usecs = priv->rx_usecs;
436 coal->rx_max_coalesced_frames = priv->rx_frames;
438 coal->pkt_rate_low = priv->pkt_rate_low;
439 coal->rx_coalesce_usecs_low = priv->rx_usecs_low;
440 coal->pkt_rate_high = priv->pkt_rate_high;
441 coal->rx_coalesce_usecs_high = priv->rx_usecs_high;
442 coal->rate_sample_interval = priv->sample_interval;
443 coal->use_adaptive_rx_coalesce = priv->adaptive_rx_coal;
448 static int mlx4_en_set_coalesce(struct net_device *dev,
449 struct ethtool_coalesce *coal)
451 struct mlx4_en_priv *priv = netdev_priv(dev);
453 if (!coal->tx_max_coalesced_frames_irq)
456 priv->rx_frames = (coal->rx_max_coalesced_frames ==
458 MLX4_EN_RX_COAL_TARGET :
459 coal->rx_max_coalesced_frames;
460 priv->rx_usecs = (coal->rx_coalesce_usecs ==
462 MLX4_EN_RX_COAL_TIME :
463 coal->rx_coalesce_usecs;
465 /* Setting TX coalescing parameters */
466 if (coal->tx_coalesce_usecs != priv->tx_usecs ||
467 coal->tx_max_coalesced_frames != priv->tx_frames) {
468 priv->tx_usecs = coal->tx_coalesce_usecs;
469 priv->tx_frames = coal->tx_max_coalesced_frames;
472 /* Set adaptive coalescing params */
473 priv->pkt_rate_low = coal->pkt_rate_low;
474 priv->rx_usecs_low = coal->rx_coalesce_usecs_low;
475 priv->pkt_rate_high = coal->pkt_rate_high;
476 priv->rx_usecs_high = coal->rx_coalesce_usecs_high;
477 priv->sample_interval = coal->rate_sample_interval;
478 priv->adaptive_rx_coal = coal->use_adaptive_rx_coalesce;
479 priv->tx_work_limit = coal->tx_max_coalesced_frames_irq;
481 return mlx4_en_moderation_update(priv);
484 static int mlx4_en_set_pauseparam(struct net_device *dev,
485 struct ethtool_pauseparam *pause)
487 struct mlx4_en_priv *priv = netdev_priv(dev);
488 struct mlx4_en_dev *mdev = priv->mdev;
494 priv->prof->tx_pause = pause->tx_pause != 0;
495 priv->prof->rx_pause = pause->rx_pause != 0;
496 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
497 priv->rx_skb_size + ETH_FCS_LEN,
498 priv->prof->tx_pause,
500 priv->prof->rx_pause,
503 en_err(priv, "Failed setting pause params\n");
508 static void mlx4_en_get_pauseparam(struct net_device *dev,
509 struct ethtool_pauseparam *pause)
511 struct mlx4_en_priv *priv = netdev_priv(dev);
513 pause->tx_pause = priv->prof->tx_pause;
514 pause->rx_pause = priv->prof->rx_pause;
517 static int mlx4_en_set_ringparam(struct net_device *dev,
518 struct ethtool_ringparam *param)
520 struct mlx4_en_priv *priv = netdev_priv(dev);
521 struct mlx4_en_dev *mdev = priv->mdev;
522 u32 rx_size, tx_size;
526 if (param->rx_jumbo_pending || param->rx_mini_pending)
529 rx_size = roundup_pow_of_two(param->rx_pending);
530 rx_size = max_t(u32, rx_size, MLX4_EN_MIN_RX_SIZE);
531 rx_size = min_t(u32, rx_size, MLX4_EN_MAX_RX_SIZE);
532 tx_size = roundup_pow_of_two(param->tx_pending);
533 tx_size = max_t(u32, tx_size, MLX4_EN_MIN_TX_SIZE);
534 tx_size = min_t(u32, tx_size, MLX4_EN_MAX_TX_SIZE);
536 if (rx_size == (priv->port_up ? priv->rx_ring[0]->actual_size :
537 priv->rx_ring[0]->size) &&
538 tx_size == priv->tx_ring[0]->size)
541 mutex_lock(&mdev->state_lock);
544 mlx4_en_stop_port(dev, 1);
547 mlx4_en_free_resources(priv);
549 priv->prof->tx_ring_size = tx_size;
550 priv->prof->rx_ring_size = rx_size;
552 err = mlx4_en_alloc_resources(priv);
554 en_err(priv, "Failed reallocating port resources\n");
558 err = mlx4_en_start_port(dev);
560 en_err(priv, "Failed starting port\n");
563 err = mlx4_en_moderation_update(priv);
566 mutex_unlock(&mdev->state_lock);
570 static void mlx4_en_get_ringparam(struct net_device *dev,
571 struct ethtool_ringparam *param)
573 struct mlx4_en_priv *priv = netdev_priv(dev);
575 memset(param, 0, sizeof(*param));
576 param->rx_max_pending = MLX4_EN_MAX_RX_SIZE;
577 param->tx_max_pending = MLX4_EN_MAX_TX_SIZE;
578 param->rx_pending = priv->port_up ?
579 priv->rx_ring[0]->actual_size : priv->rx_ring[0]->size;
580 param->tx_pending = priv->tx_ring[0]->size;
583 static u32 mlx4_en_get_rxfh_indir_size(struct net_device *dev)
585 struct mlx4_en_priv *priv = netdev_priv(dev);
587 return priv->rx_ring_num;
590 static int mlx4_en_get_rxfh(struct net_device *dev, u32 *ring_index, u8 *key)
592 struct mlx4_en_priv *priv = netdev_priv(dev);
593 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
595 size_t n = priv->rx_ring_num;
598 rss_rings = priv->prof->rss_rings ?: priv->rx_ring_num;
601 ring_index[n] = rss_map->qps[n % rss_rings].qpn -
608 static int mlx4_en_set_rxfh(struct net_device *dev, const u32 *ring_index,
611 struct mlx4_en_priv *priv = netdev_priv(dev);
612 struct mlx4_en_dev *mdev = priv->mdev;
618 /* Calculate RSS table size and make sure flows are spread evenly
621 for (i = 0; i < priv->rx_ring_num; i++) {
622 if (i > 0 && !ring_index[i] && !rss_rings)
625 if (ring_index[i] != (i % (rss_rings ?: priv->rx_ring_num)))
630 rss_rings = priv->rx_ring_num;
632 /* RSS table size must be an order of 2 */
633 if (!is_power_of_2(rss_rings))
636 mutex_lock(&mdev->state_lock);
639 mlx4_en_stop_port(dev, 1);
642 priv->prof->rss_rings = rss_rings;
645 err = mlx4_en_start_port(dev);
647 en_err(priv, "Failed starting port\n");
650 mutex_unlock(&mdev->state_lock);
654 #define all_zeros_or_all_ones(field) \
655 ((field) == 0 || (field) == (__force typeof(field))-1)
657 static int mlx4_en_validate_flow(struct net_device *dev,
658 struct ethtool_rxnfc *cmd)
660 struct ethtool_usrip4_spec *l3_mask;
661 struct ethtool_tcpip4_spec *l4_mask;
662 struct ethhdr *eth_mask;
664 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
667 if (cmd->fs.flow_type & FLOW_MAC_EXT) {
668 /* dest mac mask must be ff:ff:ff:ff:ff:ff */
669 if (!is_broadcast_ether_addr(cmd->fs.m_ext.h_dest))
673 switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
676 if (cmd->fs.m_u.tcp_ip4_spec.tos)
678 l4_mask = &cmd->fs.m_u.tcp_ip4_spec;
679 /* don't allow mask which isn't all 0 or 1 */
680 if (!all_zeros_or_all_ones(l4_mask->ip4src) ||
681 !all_zeros_or_all_ones(l4_mask->ip4dst) ||
682 !all_zeros_or_all_ones(l4_mask->psrc) ||
683 !all_zeros_or_all_ones(l4_mask->pdst))
687 l3_mask = &cmd->fs.m_u.usr_ip4_spec;
688 if (l3_mask->l4_4_bytes || l3_mask->tos || l3_mask->proto ||
689 cmd->fs.h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4 ||
690 (!l3_mask->ip4src && !l3_mask->ip4dst) ||
691 !all_zeros_or_all_ones(l3_mask->ip4src) ||
692 !all_zeros_or_all_ones(l3_mask->ip4dst))
696 eth_mask = &cmd->fs.m_u.ether_spec;
697 /* source mac mask must not be set */
698 if (!is_zero_ether_addr(eth_mask->h_source))
701 /* dest mac mask must be ff:ff:ff:ff:ff:ff */
702 if (!is_broadcast_ether_addr(eth_mask->h_dest))
705 if (!all_zeros_or_all_ones(eth_mask->h_proto))
712 if ((cmd->fs.flow_type & FLOW_EXT)) {
713 if (cmd->fs.m_ext.vlan_etype ||
714 !((cmd->fs.m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK)) ==
716 (cmd->fs.m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK)) ==
717 cpu_to_be16(VLAN_VID_MASK)))
720 if (cmd->fs.m_ext.vlan_tci) {
721 if (be16_to_cpu(cmd->fs.h_ext.vlan_tci) >= VLAN_N_VID)
730 static int mlx4_en_ethtool_add_mac_rule(struct ethtool_rxnfc *cmd,
731 struct list_head *rule_list_h,
732 struct mlx4_spec_list *spec_l2,
736 __be64 mac_msk = cpu_to_be64(MLX4_MAC_MASK << 16);
738 spec_l2->id = MLX4_NET_TRANS_RULE_ID_ETH;
739 memcpy(spec_l2->eth.dst_mac_msk, &mac_msk, ETH_ALEN);
740 memcpy(spec_l2->eth.dst_mac, mac, ETH_ALEN);
742 if ((cmd->fs.flow_type & FLOW_EXT) &&
743 (cmd->fs.m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK))) {
744 spec_l2->eth.vlan_id = cmd->fs.h_ext.vlan_tci;
745 spec_l2->eth.vlan_id_msk = cpu_to_be16(VLAN_VID_MASK);
748 list_add_tail(&spec_l2->list, rule_list_h);
753 static int mlx4_en_ethtool_add_mac_rule_by_ipv4(struct mlx4_en_priv *priv,
754 struct ethtool_rxnfc *cmd,
755 struct list_head *rule_list_h,
756 struct mlx4_spec_list *spec_l2,
760 unsigned char mac[ETH_ALEN];
762 if (!ipv4_is_multicast(ipv4_dst)) {
763 if (cmd->fs.flow_type & FLOW_MAC_EXT)
764 memcpy(&mac, cmd->fs.h_ext.h_dest, ETH_ALEN);
766 memcpy(&mac, priv->dev->dev_addr, ETH_ALEN);
768 ip_eth_mc_map(ipv4_dst, mac);
771 return mlx4_en_ethtool_add_mac_rule(cmd, rule_list_h, spec_l2, &mac[0]);
777 static int add_ip_rule(struct mlx4_en_priv *priv,
778 struct ethtool_rxnfc *cmd,
779 struct list_head *list_h)
782 struct mlx4_spec_list *spec_l2 = NULL;
783 struct mlx4_spec_list *spec_l3 = NULL;
784 struct ethtool_usrip4_spec *l3_mask = &cmd->fs.m_u.usr_ip4_spec;
786 spec_l3 = kzalloc(sizeof(*spec_l3), GFP_KERNEL);
787 spec_l2 = kzalloc(sizeof(*spec_l2), GFP_KERNEL);
788 if (!spec_l2 || !spec_l3) {
793 err = mlx4_en_ethtool_add_mac_rule_by_ipv4(priv, cmd, list_h, spec_l2,
795 usr_ip4_spec.ip4dst);
798 spec_l3->id = MLX4_NET_TRANS_RULE_ID_IPV4;
799 spec_l3->ipv4.src_ip = cmd->fs.h_u.usr_ip4_spec.ip4src;
801 spec_l3->ipv4.src_ip_msk = EN_ETHTOOL_WORD_MASK;
802 spec_l3->ipv4.dst_ip = cmd->fs.h_u.usr_ip4_spec.ip4dst;
804 spec_l3->ipv4.dst_ip_msk = EN_ETHTOOL_WORD_MASK;
805 list_add_tail(&spec_l3->list, list_h);
815 static int add_tcp_udp_rule(struct mlx4_en_priv *priv,
816 struct ethtool_rxnfc *cmd,
817 struct list_head *list_h, int proto)
820 struct mlx4_spec_list *spec_l2 = NULL;
821 struct mlx4_spec_list *spec_l3 = NULL;
822 struct mlx4_spec_list *spec_l4 = NULL;
823 struct ethtool_tcpip4_spec *l4_mask = &cmd->fs.m_u.tcp_ip4_spec;
825 spec_l2 = kzalloc(sizeof(*spec_l2), GFP_KERNEL);
826 spec_l3 = kzalloc(sizeof(*spec_l3), GFP_KERNEL);
827 spec_l4 = kzalloc(sizeof(*spec_l4), GFP_KERNEL);
828 if (!spec_l2 || !spec_l3 || !spec_l4) {
833 spec_l3->id = MLX4_NET_TRANS_RULE_ID_IPV4;
835 if (proto == TCP_V4_FLOW) {
836 err = mlx4_en_ethtool_add_mac_rule_by_ipv4(priv, cmd, list_h,
839 tcp_ip4_spec.ip4dst);
842 spec_l4->id = MLX4_NET_TRANS_RULE_ID_TCP;
843 spec_l3->ipv4.src_ip = cmd->fs.h_u.tcp_ip4_spec.ip4src;
844 spec_l3->ipv4.dst_ip = cmd->fs.h_u.tcp_ip4_spec.ip4dst;
845 spec_l4->tcp_udp.src_port = cmd->fs.h_u.tcp_ip4_spec.psrc;
846 spec_l4->tcp_udp.dst_port = cmd->fs.h_u.tcp_ip4_spec.pdst;
848 err = mlx4_en_ethtool_add_mac_rule_by_ipv4(priv, cmd, list_h,
851 udp_ip4_spec.ip4dst);
854 spec_l4->id = MLX4_NET_TRANS_RULE_ID_UDP;
855 spec_l3->ipv4.src_ip = cmd->fs.h_u.udp_ip4_spec.ip4src;
856 spec_l3->ipv4.dst_ip = cmd->fs.h_u.udp_ip4_spec.ip4dst;
857 spec_l4->tcp_udp.src_port = cmd->fs.h_u.udp_ip4_spec.psrc;
858 spec_l4->tcp_udp.dst_port = cmd->fs.h_u.udp_ip4_spec.pdst;
862 spec_l3->ipv4.src_ip_msk = EN_ETHTOOL_WORD_MASK;
864 spec_l3->ipv4.dst_ip_msk = EN_ETHTOOL_WORD_MASK;
867 spec_l4->tcp_udp.src_port_msk = EN_ETHTOOL_SHORT_MASK;
869 spec_l4->tcp_udp.dst_port_msk = EN_ETHTOOL_SHORT_MASK;
871 list_add_tail(&spec_l3->list, list_h);
872 list_add_tail(&spec_l4->list, list_h);
883 static int mlx4_en_ethtool_to_net_trans_rule(struct net_device *dev,
884 struct ethtool_rxnfc *cmd,
885 struct list_head *rule_list_h)
888 struct ethhdr *eth_spec;
889 struct mlx4_spec_list *spec_l2;
890 struct mlx4_en_priv *priv = netdev_priv(dev);
892 err = mlx4_en_validate_flow(dev, cmd);
896 switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
898 spec_l2 = kzalloc(sizeof(*spec_l2), GFP_KERNEL);
902 eth_spec = &cmd->fs.h_u.ether_spec;
903 mlx4_en_ethtool_add_mac_rule(cmd, rule_list_h, spec_l2,
904 ð_spec->h_dest[0]);
905 spec_l2->eth.ether_type = eth_spec->h_proto;
906 if (eth_spec->h_proto)
907 spec_l2->eth.ether_type_enable = 1;
910 err = add_ip_rule(priv, cmd, rule_list_h);
913 err = add_tcp_udp_rule(priv, cmd, rule_list_h, TCP_V4_FLOW);
916 err = add_tcp_udp_rule(priv, cmd, rule_list_h, UDP_V4_FLOW);
923 static int mlx4_en_flow_replace(struct net_device *dev,
924 struct ethtool_rxnfc *cmd)
927 struct mlx4_en_priv *priv = netdev_priv(dev);
928 struct ethtool_flow_id *loc_rule;
929 struct mlx4_spec_list *spec, *tmp_spec;
933 struct mlx4_net_trans_rule rule = {
934 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
937 .promisc_mode = MLX4_FS_REGULAR,
940 rule.port = priv->port;
941 rule.priority = MLX4_DOMAIN_ETHTOOL | cmd->fs.location;
942 INIT_LIST_HEAD(&rule.list);
944 /* Allow direct QP attaches if the EN_ETHTOOL_QP_ATTACH flag is set */
945 if (cmd->fs.ring_cookie == RX_CLS_FLOW_DISC)
946 qpn = priv->drop_qp.qpn;
947 else if (cmd->fs.ring_cookie & EN_ETHTOOL_QP_ATTACH) {
948 qpn = cmd->fs.ring_cookie & (EN_ETHTOOL_QP_ATTACH - 1);
950 if (cmd->fs.ring_cookie >= priv->rx_ring_num) {
951 en_warn(priv, "rxnfc: RX ring (%llu) doesn't exist\n",
952 cmd->fs.ring_cookie);
955 qpn = priv->rss_map.qps[cmd->fs.ring_cookie].qpn;
957 en_warn(priv, "rxnfc: RX ring (%llu) is inactive\n",
958 cmd->fs.ring_cookie);
963 err = mlx4_en_ethtool_to_net_trans_rule(dev, cmd, &rule.list);
967 loc_rule = &priv->ethtool_rules[cmd->fs.location];
969 err = mlx4_flow_detach(priv->mdev->dev, loc_rule->id);
971 en_err(priv, "Fail to detach network rule at location %d. registration id = %llx\n",
972 cmd->fs.location, loc_rule->id);
976 memset(&loc_rule->flow_spec, 0,
977 sizeof(struct ethtool_rx_flow_spec));
978 list_del(&loc_rule->list);
980 err = mlx4_flow_attach(priv->mdev->dev, &rule, ®_id);
982 en_err(priv, "Fail to attach network rule at location %d\n",
986 loc_rule->id = reg_id;
987 memcpy(&loc_rule->flow_spec, &cmd->fs,
988 sizeof(struct ethtool_rx_flow_spec));
989 list_add_tail(&loc_rule->list, &priv->ethtool_list);
992 list_for_each_entry_safe(spec, tmp_spec, &rule.list, list) {
993 list_del(&spec->list);
999 static int mlx4_en_flow_detach(struct net_device *dev,
1000 struct ethtool_rxnfc *cmd)
1003 struct ethtool_flow_id *rule;
1004 struct mlx4_en_priv *priv = netdev_priv(dev);
1006 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
1009 rule = &priv->ethtool_rules[cmd->fs.location];
1015 err = mlx4_flow_detach(priv->mdev->dev, rule->id);
1017 en_err(priv, "Fail to detach network rule at location %d. registration id = 0x%llx\n",
1018 cmd->fs.location, rule->id);
1022 memset(&rule->flow_spec, 0, sizeof(struct ethtool_rx_flow_spec));
1023 list_del(&rule->list);
1029 static int mlx4_en_get_flow(struct net_device *dev, struct ethtool_rxnfc *cmd,
1033 struct ethtool_flow_id *rule;
1034 struct mlx4_en_priv *priv = netdev_priv(dev);
1036 if (loc < 0 || loc >= MAX_NUM_OF_FS_RULES)
1039 rule = &priv->ethtool_rules[loc];
1041 memcpy(&cmd->fs, &rule->flow_spec,
1042 sizeof(struct ethtool_rx_flow_spec));
1049 static int mlx4_en_get_num_flows(struct mlx4_en_priv *priv)
1053 for (i = 0; i < MAX_NUM_OF_FS_RULES; i++) {
1054 if (priv->ethtool_rules[i].id)
1061 static int mlx4_en_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1064 struct mlx4_en_priv *priv = netdev_priv(dev);
1065 struct mlx4_en_dev *mdev = priv->mdev;
1067 int i = 0, priority = 0;
1069 if ((cmd->cmd == ETHTOOL_GRXCLSRLCNT ||
1070 cmd->cmd == ETHTOOL_GRXCLSRULE ||
1071 cmd->cmd == ETHTOOL_GRXCLSRLALL) &&
1072 (mdev->dev->caps.steering_mode !=
1073 MLX4_STEERING_MODE_DEVICE_MANAGED || !priv->port_up))
1077 case ETHTOOL_GRXRINGS:
1078 cmd->data = priv->rx_ring_num;
1080 case ETHTOOL_GRXCLSRLCNT:
1081 cmd->rule_cnt = mlx4_en_get_num_flows(priv);
1083 case ETHTOOL_GRXCLSRULE:
1084 err = mlx4_en_get_flow(dev, cmd, cmd->fs.location);
1086 case ETHTOOL_GRXCLSRLALL:
1087 while ((!err || err == -ENOENT) && priority < cmd->rule_cnt) {
1088 err = mlx4_en_get_flow(dev, cmd, i);
1090 rule_locs[priority++] = i;
1103 static int mlx4_en_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1106 struct mlx4_en_priv *priv = netdev_priv(dev);
1107 struct mlx4_en_dev *mdev = priv->mdev;
1109 if (mdev->dev->caps.steering_mode !=
1110 MLX4_STEERING_MODE_DEVICE_MANAGED || !priv->port_up)
1114 case ETHTOOL_SRXCLSRLINS:
1115 err = mlx4_en_flow_replace(dev, cmd);
1117 case ETHTOOL_SRXCLSRLDEL:
1118 err = mlx4_en_flow_detach(dev, cmd);
1121 en_warn(priv, "Unsupported ethtool command. (%d)\n", cmd->cmd);
1128 static void mlx4_en_get_channels(struct net_device *dev,
1129 struct ethtool_channels *channel)
1131 struct mlx4_en_priv *priv = netdev_priv(dev);
1133 memset(channel, 0, sizeof(*channel));
1135 channel->max_rx = MAX_RX_RINGS;
1136 channel->max_tx = MLX4_EN_MAX_TX_RING_P_UP;
1138 channel->rx_count = priv->rx_ring_num;
1139 channel->tx_count = priv->tx_ring_num / MLX4_EN_NUM_UP;
1142 static int mlx4_en_set_channels(struct net_device *dev,
1143 struct ethtool_channels *channel)
1145 struct mlx4_en_priv *priv = netdev_priv(dev);
1146 struct mlx4_en_dev *mdev = priv->mdev;
1150 if (channel->other_count || channel->combined_count ||
1151 channel->tx_count > MLX4_EN_MAX_TX_RING_P_UP ||
1152 channel->rx_count > MAX_RX_RINGS ||
1153 !channel->tx_count || !channel->rx_count)
1156 mutex_lock(&mdev->state_lock);
1157 if (priv->port_up) {
1159 mlx4_en_stop_port(dev, 1);
1162 mlx4_en_free_resources(priv);
1164 priv->num_tx_rings_p_up = channel->tx_count;
1165 priv->tx_ring_num = channel->tx_count * MLX4_EN_NUM_UP;
1166 priv->rx_ring_num = channel->rx_count;
1168 err = mlx4_en_alloc_resources(priv);
1170 en_err(priv, "Failed reallocating port resources\n");
1174 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
1175 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
1178 mlx4_en_setup_tc(dev, MLX4_EN_NUM_UP);
1180 en_warn(priv, "Using %d TX rings\n", priv->tx_ring_num);
1181 en_warn(priv, "Using %d RX rings\n", priv->rx_ring_num);
1184 err = mlx4_en_start_port(dev);
1186 en_err(priv, "Failed starting port\n");
1189 err = mlx4_en_moderation_update(priv);
1192 mutex_unlock(&mdev->state_lock);
1196 static int mlx4_en_get_ts_info(struct net_device *dev,
1197 struct ethtool_ts_info *info)
1199 struct mlx4_en_priv *priv = netdev_priv(dev);
1200 struct mlx4_en_dev *mdev = priv->mdev;
1203 ret = ethtool_op_get_ts_info(dev, info);
1207 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS) {
1208 info->so_timestamping |=
1209 SOF_TIMESTAMPING_TX_HARDWARE |
1210 SOF_TIMESTAMPING_RX_HARDWARE |
1211 SOF_TIMESTAMPING_RAW_HARDWARE;
1214 (1 << HWTSTAMP_TX_OFF) |
1215 (1 << HWTSTAMP_TX_ON);
1218 (1 << HWTSTAMP_FILTER_NONE) |
1219 (1 << HWTSTAMP_FILTER_ALL);
1221 if (mdev->ptp_clock)
1222 info->phc_index = ptp_clock_index(mdev->ptp_clock);
1228 static int mlx4_en_set_priv_flags(struct net_device *dev, u32 flags)
1230 struct mlx4_en_priv *priv = netdev_priv(dev);
1231 bool bf_enabled_new = !!(flags & MLX4_EN_PRIV_FLAGS_BLUEFLAME);
1232 bool bf_enabled_old = !!(priv->pflags & MLX4_EN_PRIV_FLAGS_BLUEFLAME);
1235 if (bf_enabled_new == bf_enabled_old)
1236 return 0; /* Nothing to do */
1238 if (bf_enabled_new) {
1239 bool bf_supported = true;
1241 for (i = 0; i < priv->tx_ring_num; i++)
1242 bf_supported &= priv->tx_ring[i]->bf_alloced;
1244 if (!bf_supported) {
1245 en_err(priv, "BlueFlame is not supported\n");
1249 priv->pflags |= MLX4_EN_PRIV_FLAGS_BLUEFLAME;
1251 priv->pflags &= ~MLX4_EN_PRIV_FLAGS_BLUEFLAME;
1254 for (i = 0; i < priv->tx_ring_num; i++)
1255 priv->tx_ring[i]->bf_enabled = bf_enabled_new;
1257 en_info(priv, "BlueFlame %s\n",
1258 bf_enabled_new ? "Enabled" : "Disabled");
1263 static u32 mlx4_en_get_priv_flags(struct net_device *dev)
1265 struct mlx4_en_priv *priv = netdev_priv(dev);
1267 return priv->pflags;
1270 static int mlx4_en_get_tunable(struct net_device *dev,
1271 const struct ethtool_tunable *tuna,
1274 const struct mlx4_en_priv *priv = netdev_priv(dev);
1278 case ETHTOOL_TX_COPYBREAK:
1279 *(u32 *)data = priv->prof->inline_thold;
1289 static int mlx4_en_set_tunable(struct net_device *dev,
1290 const struct ethtool_tunable *tuna,
1293 struct mlx4_en_priv *priv = netdev_priv(dev);
1297 case ETHTOOL_TX_COPYBREAK:
1299 if (val < MIN_PKT_LEN || val > MAX_INLINE)
1302 priv->prof->inline_thold = val;
1313 const struct ethtool_ops mlx4_en_ethtool_ops = {
1314 .get_drvinfo = mlx4_en_get_drvinfo,
1315 .get_settings = mlx4_en_get_settings,
1316 .set_settings = mlx4_en_set_settings,
1317 .get_link = ethtool_op_get_link,
1318 .get_strings = mlx4_en_get_strings,
1319 .get_sset_count = mlx4_en_get_sset_count,
1320 .get_ethtool_stats = mlx4_en_get_ethtool_stats,
1321 .self_test = mlx4_en_self_test,
1322 .get_wol = mlx4_en_get_wol,
1323 .set_wol = mlx4_en_set_wol,
1324 .get_msglevel = mlx4_en_get_msglevel,
1325 .set_msglevel = mlx4_en_set_msglevel,
1326 .get_coalesce = mlx4_en_get_coalesce,
1327 .set_coalesce = mlx4_en_set_coalesce,
1328 .get_pauseparam = mlx4_en_get_pauseparam,
1329 .set_pauseparam = mlx4_en_set_pauseparam,
1330 .get_ringparam = mlx4_en_get_ringparam,
1331 .set_ringparam = mlx4_en_set_ringparam,
1332 .get_rxnfc = mlx4_en_get_rxnfc,
1333 .set_rxnfc = mlx4_en_set_rxnfc,
1334 .get_rxfh_indir_size = mlx4_en_get_rxfh_indir_size,
1335 .get_rxfh = mlx4_en_get_rxfh,
1336 .set_rxfh = mlx4_en_set_rxfh,
1337 .get_channels = mlx4_en_get_channels,
1338 .set_channels = mlx4_en_set_channels,
1339 .get_ts_info = mlx4_en_get_ts_info,
1340 .set_priv_flags = mlx4_en_set_priv_flags,
1341 .get_priv_flags = mlx4_en_get_priv_flags,
1342 .get_tunable = mlx4_en_get_tunable,
1343 .set_tunable = mlx4_en_set_tunable,