4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <linux/ethtool.h>
48 #include <linux/sockios.h>
51 /* DPDK headers don't like -pedantic. */
53 #pragma GCC diagnostic ignored "-Wpedantic"
55 #include <rte_atomic.h>
56 #include <rte_ethdev.h>
58 #include <rte_common.h>
59 #include <rte_interrupts.h>
60 #include <rte_alarm.h>
61 #include <rte_malloc.h>
63 #pragma GCC diagnostic error "-Wpedantic"
67 #include "mlx5_rxtx.h"
68 #include "mlx5_utils.h"
71 * Return private structure associated with an Ethernet device.
74 * Pointer to Ethernet device structure.
77 * Pointer to private structure.
80 mlx5_get_priv(struct rte_eth_dev *dev)
82 struct mlx5_secondary_data *sd;
84 if (!mlx5_is_secondary())
85 return dev->data->dev_private;
86 sd = &mlx5_secondary_data[dev->data->port_id];
87 return sd->data.dev_private;
91 * Check if running as a secondary process.
94 * Nonzero if running as a secondary process.
97 mlx5_is_secondary(void)
99 return rte_eal_process_type() != RTE_PROC_PRIMARY;
103 * Get interface name from private structure.
106 * Pointer to private structure.
108 * Interface name output buffer.
111 * 0 on success, -1 on failure and errno is set.
114 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
118 unsigned int dev_type = 0;
119 unsigned int dev_port_prev = ~0u;
120 char match[IF_NAMESIZE] = "";
123 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
129 while ((dent = readdir(dir)) != NULL) {
130 char *name = dent->d_name;
132 unsigned int dev_port;
135 if ((name[0] == '.') &&
136 ((name[1] == '\0') ||
137 ((name[1] == '.') && (name[2] == '\0'))))
140 MKSTR(path, "%s/device/net/%s/%s",
141 priv->ctx->device->ibdev_path, name,
142 (dev_type ? "dev_id" : "dev_port"));
144 file = fopen(path, "rb");
149 * Switch to dev_id when dev_port does not exist as
150 * is the case with Linux kernel versions < 3.15.
161 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
166 * Switch to dev_id when dev_port returns the same value for
167 * all ports. May happen when using a MOFED release older than
168 * 3.0 with a Linux kernel >= 3.15.
170 if (dev_port == dev_port_prev)
172 dev_port_prev = dev_port;
173 if (dev_port == (priv->port - 1u))
174 snprintf(match, sizeof(match), "%s", name);
177 if (match[0] == '\0')
179 strncpy(*ifname, match, sizeof(*ifname));
184 * Read from sysfs entry.
187 * Pointer to private structure.
189 * Entry name relative to sysfs path.
191 * Data output buffer.
196 * 0 on success, -1 on failure and errno is set.
199 priv_sysfs_read(const struct priv *priv, const char *entry,
200 char *buf, size_t size)
202 char ifname[IF_NAMESIZE];
207 if (priv_get_ifname(priv, &ifname))
210 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
213 file = fopen(path, "rb");
216 ret = fread(buf, 1, size, file);
218 if (((size_t)ret < size) && (ferror(file)))
228 * Write to sysfs entry.
231 * Pointer to private structure.
233 * Entry name relative to sysfs path.
240 * 0 on success, -1 on failure and errno is set.
243 priv_sysfs_write(const struct priv *priv, const char *entry,
244 char *buf, size_t size)
246 char ifname[IF_NAMESIZE];
251 if (priv_get_ifname(priv, &ifname))
254 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
257 file = fopen(path, "wb");
260 ret = fwrite(buf, 1, size, file);
262 if (((size_t)ret < size) || (ferror(file)))
272 * Get unsigned long sysfs property.
275 * Pointer to private structure.
277 * Entry name relative to sysfs path.
279 * Value output buffer.
282 * 0 on success, -1 on failure and errno is set.
285 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
288 unsigned long value_ret;
291 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
293 DEBUG("cannot read %s value from sysfs: %s",
294 name, strerror(errno));
297 value_str[ret] = '\0';
299 value_ret = strtoul(value_str, NULL, 0);
301 DEBUG("invalid %s value `%s': %s", name, value_str,
310 * Set unsigned long sysfs property.
313 * Pointer to private structure.
315 * Entry name relative to sysfs path.
320 * 0 on success, -1 on failure and errno is set.
323 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
326 MKSTR(value_str, "%lu", value);
328 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
330 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
331 name, value_str, value, strerror(errno));
338 * Perform ifreq ioctl() on associated Ethernet device.
341 * Pointer to private structure.
343 * Request number to pass to ioctl().
345 * Interface request structure output buffer.
348 * 0 on success, -1 on failure and errno is set.
351 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
353 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
358 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
359 ret = ioctl(sock, req, ifr);
365 * Return the number of active VFs for the current device.
368 * Pointer to private structure.
369 * @param[out] num_vfs
370 * Number of active VFs.
373 * 0 on success, -1 on failure and errno is set.
376 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
378 /* The sysfs entry name depends on the operating system. */
379 const char **name = (const char *[]){
380 "device/sriov_numvfs",
381 "device/mlx5_num_vfs",
387 unsigned long ulong_num_vfs;
389 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
391 *num_vfs = ulong_num_vfs;
392 } while (*(++name) && ret);
400 * Pointer to private structure.
402 * MTU value output buffer.
405 * 0 on success, -1 on failure and errno is set.
408 priv_get_mtu(struct priv *priv, uint16_t *mtu)
410 unsigned long ulong_mtu;
412 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
422 * Pointer to private structure.
427 * 0 on success, -1 on failure and errno is set.
430 priv_set_mtu(struct priv *priv, uint16_t mtu)
434 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
435 priv_get_mtu(priv, &new_mtu))
447 * Pointer to private structure.
449 * Bitmask for flags that must remain untouched.
451 * Bitmask for flags to modify.
454 * 0 on success, -1 on failure and errno is set.
457 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
461 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
464 tmp |= (flags & (~keep));
465 return priv_set_sysfs_ulong(priv, "flags", tmp);
469 * Ethernet device configuration.
471 * Prepare the driver for a given number of TX and RX queues.
474 * Pointer to Ethernet device structure.
477 * 0 on success, errno value on failure.
480 dev_configure(struct rte_eth_dev *dev)
482 struct priv *priv = dev->data->dev_private;
483 unsigned int rxqs_n = dev->data->nb_rx_queues;
484 unsigned int txqs_n = dev->data->nb_tx_queues;
487 unsigned int reta_idx_n;
489 priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
490 priv->rxqs = (void *)dev->data->rx_queues;
491 priv->txqs = (void *)dev->data->tx_queues;
492 if (txqs_n != priv->txqs_n) {
493 INFO("%p: TX queues number update: %u -> %u",
494 (void *)dev, priv->txqs_n, txqs_n);
495 priv->txqs_n = txqs_n;
497 if (rxqs_n > priv->ind_table_max_size) {
498 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
501 if (rxqs_n == priv->rxqs_n)
503 INFO("%p: RX queues number update: %u -> %u",
504 (void *)dev, priv->rxqs_n, rxqs_n);
505 priv->rxqs_n = rxqs_n;
506 /* If the requested number of RX queues is not a power of two, use the
507 * maximum indirection table size for better balancing.
508 * The result is always rounded to the next power of two. */
509 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
510 priv->ind_table_max_size :
512 if (priv_rss_reta_index_resize(priv, reta_idx_n))
514 /* When the number of RX queues is not a power of two, the remaining
515 * table entries are padded with reused WQs and hashes are not spread
517 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
518 (*priv->reta_idx)[i] = j;
526 * DPDK callback for Ethernet device configuration.
529 * Pointer to Ethernet device structure.
532 * 0 on success, negative errno value on failure.
535 mlx5_dev_configure(struct rte_eth_dev *dev)
537 struct priv *priv = dev->data->dev_private;
540 if (mlx5_is_secondary())
541 return -E_RTE_SECONDARY;
544 ret = dev_configure(dev);
551 * DPDK callback to get information about the device.
554 * Pointer to Ethernet device structure.
556 * Info structure output buffer.
559 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
561 struct priv *priv = mlx5_get_priv(dev);
563 char ifname[IF_NAMESIZE];
565 info->pci_dev = dev->pci_dev;
568 /* FIXME: we should ask the device for these values. */
569 info->min_rx_bufsize = 32;
570 info->max_rx_pktlen = 65536;
572 * Since we need one CQ per QP, the limit is the minimum number
573 * between the two values.
575 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
576 priv->device_attr.max_qp : priv->device_attr.max_cq);
577 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
580 info->max_rx_queues = max;
581 info->max_tx_queues = max;
582 info->max_mac_addrs = RTE_DIM(priv->mac);
583 info->rx_offload_capa =
585 (DEV_RX_OFFLOAD_IPV4_CKSUM |
586 DEV_RX_OFFLOAD_UDP_CKSUM |
587 DEV_RX_OFFLOAD_TCP_CKSUM) :
589 (priv->hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0);
591 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
593 info->tx_offload_capa |=
594 (DEV_TX_OFFLOAD_IPV4_CKSUM |
595 DEV_TX_OFFLOAD_UDP_CKSUM |
596 DEV_TX_OFFLOAD_TCP_CKSUM);
597 if (priv_get_ifname(priv, &ifname) == 0)
598 info->if_index = if_nametoindex(ifname);
599 /* FIXME: RETA update/query API expects the callee to know the size of
600 * the indirection table, for this PMD the size varies depending on
601 * the number of RX queues, it becomes impossible to find the correct
602 * size if it is not fixed.
603 * The API should be updated to solve this problem. */
604 info->reta_size = priv->ind_table_max_size;
605 info->hash_key_size = ((*priv->rss_conf) ?
606 (*priv->rss_conf)[0]->rss_key_len :
608 info->speed_capa = priv->link_speed_capa;
613 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
615 static const uint32_t ptypes[] = {
616 /* refers to rxq_cq_to_pkt_type() */
619 RTE_PTYPE_INNER_L3_IPV4,
620 RTE_PTYPE_INNER_L3_IPV6,
625 if (dev->rx_pkt_burst == mlx5_rx_burst)
631 * Retrieve physical link information (unlocked version using legacy ioctl).
634 * Pointer to Ethernet device structure.
635 * @param wait_to_complete
636 * Wait for request completion (ignored).
639 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
641 struct priv *priv = mlx5_get_priv(dev);
642 struct ethtool_cmd edata = {
643 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
646 struct rte_eth_link dev_link;
649 (void)wait_to_complete;
650 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
651 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
654 memset(&dev_link, 0, sizeof(dev_link));
655 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
656 (ifr.ifr_flags & IFF_RUNNING));
657 ifr.ifr_data = (void *)&edata;
658 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
659 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
663 link_speed = ethtool_cmd_speed(&edata);
664 if (link_speed == -1)
665 dev_link.link_speed = 0;
667 dev_link.link_speed = link_speed;
668 priv->link_speed_capa = 0;
669 if (edata.supported & SUPPORTED_Autoneg)
670 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
671 if (edata.supported & (SUPPORTED_1000baseT_Full |
672 SUPPORTED_1000baseKX_Full))
673 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
674 if (edata.supported & SUPPORTED_10000baseKR_Full)
675 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
676 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
677 SUPPORTED_40000baseCR4_Full |
678 SUPPORTED_40000baseSR4_Full |
679 SUPPORTED_40000baseLR4_Full))
680 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
681 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
682 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
683 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
684 ETH_LINK_SPEED_FIXED);
685 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
686 /* Link status changed. */
687 dev->data->dev_link = dev_link;
690 /* Link status is still the same. */
695 * Retrieve physical link information (unlocked version using new ioctl from
699 * Pointer to Ethernet device structure.
700 * @param wait_to_complete
701 * Wait for request completion (ignored).
704 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
706 #ifdef ETHTOOL_GLINKSETTINGS
707 struct priv *priv = mlx5_get_priv(dev);
708 struct ethtool_link_settings edata = {
709 .cmd = ETHTOOL_GLINKSETTINGS,
712 struct rte_eth_link dev_link;
715 (void)wait_to_complete;
716 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
717 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
720 memset(&dev_link, 0, sizeof(dev_link));
721 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
722 (ifr.ifr_flags & IFF_RUNNING));
723 ifr.ifr_data = (void *)&edata;
724 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
725 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
729 dev_link.link_speed = edata.speed;
730 sc = edata.link_mode_masks[0] |
731 ((uint64_t)edata.link_mode_masks[1] << 32);
732 priv->link_speed_capa = 0;
733 /* Link speeds available in kernel v4.5. */
734 if (sc & ETHTOOL_LINK_MODE_Autoneg_BIT)
735 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
736 if (sc & (ETHTOOL_LINK_MODE_1000baseT_Full_BIT |
737 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT))
738 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
739 if (sc & (ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT |
740 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT |
741 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT))
742 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
743 if (sc & (ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT |
744 ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT))
745 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
746 if (sc & (ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT |
747 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT |
748 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT |
749 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT))
750 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
751 if (sc & (ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT |
752 ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT |
753 ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT |
754 ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT))
755 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
756 /* Link speeds available in kernel v4.6. */
757 #ifdef HAVE_ETHTOOL_LINK_MODE_25G
758 if (sc & (ETHTOOL_LINK_MODE_25000baseCR_Full_BIT |
759 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT |
760 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT))
761 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
763 #ifdef HAVE_ETHTOOL_LINK_MODE_50G
764 if (sc & (ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT |
765 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT))
766 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
768 #ifdef HAVE_ETHTOOL_LINK_MODE_100G
769 if (sc & (ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT |
770 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT |
771 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT |
772 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT))
773 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
775 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
776 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
777 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
778 ETH_LINK_SPEED_FIXED);
779 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
780 /* Link status changed. */
781 dev->data->dev_link = dev_link;
786 (void)wait_to_complete;
788 /* Link status is still the same. */
793 * DPDK callback to retrieve physical link information (unlocked version).
796 * Pointer to Ethernet device structure.
797 * @param wait_to_complete
798 * Wait for request completion (ignored).
801 mlx5_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
805 ret = mlx5_link_update_unlocked_gs(dev, wait_to_complete);
807 ret = mlx5_link_update_unlocked_gset(dev, wait_to_complete);
812 * DPDK callback to retrieve physical link information.
815 * Pointer to Ethernet device structure.
816 * @param wait_to_complete
817 * Wait for request completion (ignored).
820 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
822 struct priv *priv = mlx5_get_priv(dev);
826 ret = mlx5_link_update_unlocked(dev, wait_to_complete);
832 * DPDK callback to change the MTU.
834 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
835 * received). Use this as a hint to enable/disable scattered packets support
836 * and improve performance when not needed.
837 * Since failure is not an option, reconfiguring queues on the fly is not
841 * Pointer to Ethernet device structure.
846 * 0 on success, negative errno value on failure.
849 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
851 struct priv *priv = dev->data->dev_private;
854 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
856 unsigned int max_frame_len;
858 int restart = priv->started;
860 if (mlx5_is_secondary())
861 return -E_RTE_SECONDARY;
864 /* Set kernel interface MTU first. */
865 if (priv_set_mtu(priv, mtu)) {
867 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
871 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
872 /* Temporarily replace RX handler with a fake one, assuming it has not
873 * been copied elsewhere. */
874 dev->rx_pkt_burst = removed_rx_burst;
875 /* Make sure everyone has left mlx5_rx_burst() and uses
876 * removed_rx_burst() instead. */
879 /* MTU does not include header and CRC. */
880 max_frame_len = ETHER_HDR_LEN + mtu + ETHER_CRC_LEN;
881 /* Check if at least one queue is going to need a SGE update. */
882 for (i = 0; i != priv->rxqs_n; ++i) {
883 struct rxq *rxq = (*priv->rxqs)[i];
885 unsigned int size = RTE_PKTMBUF_HEADROOM + max_frame_len;
890 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
891 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
893 * Determine the number of SGEs needed for a full packet
894 * and round it to the next power of two.
896 sges_n = log2above((size / mb_len) + !!(size % mb_len));
897 if (sges_n != rxq->sges_n)
901 * If all queues have the right number of SGEs, a simple rehash
902 * of their buffers is enough, otherwise SGE information can only
903 * be updated in a queue by recreating it. All resources that depend
904 * on queues (flows, indirection tables) must be recreated as well in
907 rehash = (i == priv->rxqs_n);
909 /* Clean up everything as with mlx5_dev_stop(). */
910 priv_special_flow_disable_all(priv);
911 priv_mac_addrs_disable(priv);
912 priv_destroy_hash_rxqs(priv);
913 priv_fdir_disable(priv);
914 priv_dev_interrupt_handler_uninstall(priv, dev);
917 /* Reconfigure each RX queue. */
918 for (i = 0; (i != priv->rxqs_n); ++i) {
919 struct rxq *rxq = (*priv->rxqs)[i];
920 struct rxq_ctrl *rxq_ctrl =
921 container_of(rxq, struct rxq_ctrl, rxq);
928 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
929 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
930 /* Toggle scattered support (sp) if necessary. */
931 sp = (max_frame_len > (mb_len - RTE_PKTMBUF_HEADROOM));
932 /* Provide new values to rxq_setup(). */
933 dev->data->dev_conf.rxmode.jumbo_frame = sp;
934 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
936 ret = rxq_rehash(dev, rxq_ctrl);
938 ret = rxq_ctrl_setup(dev, rxq_ctrl, 1 << rxq->elts_n,
939 rxq_ctrl->socket, NULL, rxq->mp);
942 /* Attempt to roll back in case of error. */
943 tmp = (mb_len << rxq->sges_n) - RTE_PKTMBUF_HEADROOM;
944 if (max_frame_len != tmp) {
948 /* Double fault, disable RX. */
952 * Use a safe RX burst function in case of error, otherwise mimic
956 ERROR("unable to reconfigure RX queues, RX disabled");
957 rx_func = removed_rx_burst;
958 } else if (restart &&
960 !priv_create_hash_rxqs(priv) &&
961 !priv_rehash_flows(priv)) {
962 if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_NONE)
963 priv_fdir_enable(priv);
964 priv_dev_interrupt_handler_install(priv, dev);
967 /* Burst functions can now be called again. */
969 dev->rx_pkt_burst = rx_func;
977 * DPDK callback to get flow control status.
980 * Pointer to Ethernet device structure.
981 * @param[out] fc_conf
982 * Flow control output buffer.
985 * 0 on success, negative errno value on failure.
988 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
990 struct priv *priv = dev->data->dev_private;
992 struct ethtool_pauseparam ethpause = {
993 .cmd = ETHTOOL_GPAUSEPARAM
997 if (mlx5_is_secondary())
998 return -E_RTE_SECONDARY;
1000 ifr.ifr_data = (void *)ðpause;
1002 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1004 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
1010 fc_conf->autoneg = ethpause.autoneg;
1011 if (ethpause.rx_pause && ethpause.tx_pause)
1012 fc_conf->mode = RTE_FC_FULL;
1013 else if (ethpause.rx_pause)
1014 fc_conf->mode = RTE_FC_RX_PAUSE;
1015 else if (ethpause.tx_pause)
1016 fc_conf->mode = RTE_FC_TX_PAUSE;
1018 fc_conf->mode = RTE_FC_NONE;
1028 * DPDK callback to modify flow control parameters.
1031 * Pointer to Ethernet device structure.
1032 * @param[in] fc_conf
1033 * Flow control parameters.
1036 * 0 on success, negative errno value on failure.
1039 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1041 struct priv *priv = dev->data->dev_private;
1043 struct ethtool_pauseparam ethpause = {
1044 .cmd = ETHTOOL_SPAUSEPARAM
1048 if (mlx5_is_secondary())
1049 return -E_RTE_SECONDARY;
1051 ifr.ifr_data = (void *)ðpause;
1052 ethpause.autoneg = fc_conf->autoneg;
1053 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1054 (fc_conf->mode & RTE_FC_RX_PAUSE))
1055 ethpause.rx_pause = 1;
1057 ethpause.rx_pause = 0;
1059 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1060 (fc_conf->mode & RTE_FC_TX_PAUSE))
1061 ethpause.tx_pause = 1;
1063 ethpause.tx_pause = 0;
1066 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1068 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1082 * Get PCI information from struct ibv_device.
1085 * Pointer to Ethernet device structure.
1086 * @param[out] pci_addr
1087 * PCI bus address output buffer.
1090 * 0 on success, -1 on failure and errno is set.
1093 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1094 struct rte_pci_addr *pci_addr)
1098 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1100 file = fopen(path, "rb");
1103 while (fgets(line, sizeof(line), file) == line) {
1104 size_t len = strlen(line);
1107 /* Truncate long lines. */
1108 if (len == (sizeof(line) - 1))
1109 while (line[(len - 1)] != '\n') {
1113 line[(len - 1)] = ret;
1115 /* Extract information. */
1118 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1122 &pci_addr->function) == 4) {
1132 * Link status handler.
1135 * Pointer to private structure.
1137 * Pointer to the rte_eth_dev structure.
1140 * Nonzero if the callback process can be called immediately.
1143 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
1145 struct ibv_async_event event;
1146 int port_change = 0;
1149 /* Read all message and acknowledge them. */
1151 if (ibv_get_async_event(priv->ctx, &event))
1154 if (event.event_type == IBV_EVENT_PORT_ACTIVE ||
1155 event.event_type == IBV_EVENT_PORT_ERR)
1158 DEBUG("event type %d on port %d not handled",
1159 event.event_type, event.element.port_num);
1160 ibv_ack_async_event(&event);
1163 if (port_change ^ priv->pending_alarm) {
1164 struct rte_eth_link *link = &dev->data->dev_link;
1166 priv->pending_alarm = 0;
1167 mlx5_link_update_unlocked(dev, 0);
1168 if (((link->link_speed == 0) && link->link_status) ||
1169 ((link->link_speed != 0) && !link->link_status)) {
1170 /* Inconsistent status, check again later. */
1171 priv->pending_alarm = 1;
1172 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1173 mlx5_dev_link_status_handler,
1182 * Handle delayed link status event.
1185 * Registered argument.
1188 mlx5_dev_link_status_handler(void *arg)
1190 struct rte_eth_dev *dev = arg;
1191 struct priv *priv = dev->data->dev_private;
1195 assert(priv->pending_alarm == 1);
1196 ret = priv_dev_link_status_handler(priv, dev);
1199 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1203 * Handle interrupts from the NIC.
1205 * @param[in] intr_handle
1206 * Interrupt handler.
1208 * Callback argument.
1211 mlx5_dev_interrupt_handler(struct rte_intr_handle *intr_handle, void *cb_arg)
1213 struct rte_eth_dev *dev = cb_arg;
1214 struct priv *priv = dev->data->dev_private;
1219 ret = priv_dev_link_status_handler(priv, dev);
1222 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1226 * Uninstall interrupt handler.
1229 * Pointer to private structure.
1231 * Pointer to the rte_eth_dev structure.
1234 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1236 if (!dev->data->dev_conf.intr_conf.lsc)
1238 rte_intr_callback_unregister(&priv->intr_handle,
1239 mlx5_dev_interrupt_handler,
1241 if (priv->pending_alarm)
1242 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1243 priv->pending_alarm = 0;
1244 priv->intr_handle.fd = 0;
1245 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1249 * Install interrupt handler.
1252 * Pointer to private structure.
1254 * Pointer to the rte_eth_dev structure.
1257 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1261 if (!dev->data->dev_conf.intr_conf.lsc)
1263 assert(priv->ctx->async_fd > 0);
1264 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1265 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1267 INFO("failed to change file descriptor async event queue");
1268 dev->data->dev_conf.intr_conf.lsc = 0;
1270 priv->intr_handle.fd = priv->ctx->async_fd;
1271 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1272 rte_intr_callback_register(&priv->intr_handle,
1273 mlx5_dev_interrupt_handler,
1279 * Change the link state (UP / DOWN).
1282 * Pointer to Ethernet device structure.
1284 * Nonzero for link up, otherwise link down.
1287 * 0 on success, errno value on failure.
1290 priv_set_link(struct priv *priv, int up)
1292 struct rte_eth_dev *dev = priv->dev;
1296 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1299 priv_select_tx_function(priv);
1300 priv_select_rx_function(priv);
1302 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1305 dev->rx_pkt_burst = removed_rx_burst;
1306 dev->tx_pkt_burst = removed_tx_burst;
1312 * DPDK callback to bring the link DOWN.
1315 * Pointer to Ethernet device structure.
1318 * 0 on success, errno value on failure.
1321 mlx5_set_link_down(struct rte_eth_dev *dev)
1323 struct priv *priv = dev->data->dev_private;
1327 err = priv_set_link(priv, 0);
1333 * DPDK callback to bring the link UP.
1336 * Pointer to Ethernet device structure.
1339 * 0 on success, errno value on failure.
1342 mlx5_set_link_up(struct rte_eth_dev *dev)
1344 struct priv *priv = dev->data->dev_private;
1348 err = priv_set_link(priv, 1);
1354 * Configure secondary process queues from a private data pointer (primary
1355 * or secondary) and update burst callbacks. Can take place only once.
1357 * All queues must have been previously created by the primary process to
1358 * avoid undefined behavior.
1361 * Private data pointer from either primary or secondary process.
1364 * Private data pointer from secondary process, NULL in case of error.
1367 mlx5_secondary_data_setup(struct priv *priv)
1369 unsigned int port_id = 0;
1370 struct mlx5_secondary_data *sd;
1373 unsigned int nb_tx_queues;
1374 unsigned int nb_rx_queues;
1377 /* priv must be valid at this point. */
1378 assert(priv != NULL);
1379 /* priv->dev must also be valid but may point to local memory from
1380 * another process, possibly with the same address and must not
1381 * be dereferenced yet. */
1382 assert(priv->dev != NULL);
1383 /* Determine port ID by finding out where priv comes from. */
1385 sd = &mlx5_secondary_data[port_id];
1386 rte_spinlock_lock(&sd->lock);
1387 /* Primary process? */
1388 if (sd->primary_priv == priv)
1390 /* Secondary process? */
1391 if (sd->data.dev_private == priv)
1393 rte_spinlock_unlock(&sd->lock);
1394 if (++port_id == RTE_DIM(mlx5_secondary_data))
1397 /* Switch to secondary private structure. If private data has already
1398 * been updated by another thread, there is nothing else to do. */
1399 priv = sd->data.dev_private;
1400 if (priv->dev->data == &sd->data)
1402 /* Sanity checks. Secondary private structure is supposed to point
1403 * to local eth_dev, itself still pointing to the shared device data
1404 * structure allocated by the primary process. */
1405 assert(sd->shared_dev_data != &sd->data);
1406 assert(sd->data.nb_tx_queues == 0);
1407 assert(sd->data.tx_queues == NULL);
1408 assert(sd->data.nb_rx_queues == 0);
1409 assert(sd->data.rx_queues == NULL);
1410 assert(priv != sd->primary_priv);
1411 assert(priv->dev->data == sd->shared_dev_data);
1412 assert(priv->txqs_n == 0);
1413 assert(priv->txqs == NULL);
1414 assert(priv->rxqs_n == 0);
1415 assert(priv->rxqs == NULL);
1416 nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
1417 nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
1418 /* Allocate local storage for queues. */
1419 tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
1420 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
1421 RTE_CACHE_LINE_SIZE);
1422 rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
1423 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
1424 RTE_CACHE_LINE_SIZE);
1425 if (tx_queues == NULL || rx_queues == NULL)
1427 /* Lock to prevent control operations during setup. */
1430 for (i = 0; i != nb_tx_queues; ++i) {
1431 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
1432 struct txq_ctrl *primary_txq_ctrl;
1433 struct txq_ctrl *txq_ctrl;
1435 if (primary_txq == NULL)
1437 primary_txq_ctrl = container_of(primary_txq,
1438 struct txq_ctrl, txq);
1439 txq_ctrl = rte_calloc_socket("TXQ", 1, sizeof(*txq_ctrl) +
1440 (1 << primary_txq->elts_n) *
1441 sizeof(struct rte_mbuf *), 0,
1442 primary_txq_ctrl->socket);
1443 if (txq_ctrl != NULL) {
1444 if (txq_ctrl_setup(priv->dev,
1446 1 << primary_txq->elts_n,
1447 primary_txq_ctrl->socket,
1449 txq_ctrl->txq.stats.idx =
1450 primary_txq->stats.idx;
1451 tx_queues[i] = &txq_ctrl->txq;
1457 txq_ctrl = tx_queues[--i];
1458 txq_cleanup(txq_ctrl);
1464 for (i = 0; i != nb_rx_queues; ++i) {
1465 struct rxq_ctrl *primary_rxq =
1466 container_of((*sd->primary_priv->rxqs)[i],
1467 struct rxq_ctrl, rxq);
1469 if (primary_rxq == NULL)
1471 /* Not supported yet. */
1472 rx_queues[i] = NULL;
1474 /* Update everything. */
1475 priv->txqs = (void *)tx_queues;
1476 priv->txqs_n = nb_tx_queues;
1477 priv->rxqs = (void *)rx_queues;
1478 priv->rxqs_n = nb_rx_queues;
1479 sd->data.rx_queues = rx_queues;
1480 sd->data.tx_queues = tx_queues;
1481 sd->data.nb_rx_queues = nb_rx_queues;
1482 sd->data.nb_tx_queues = nb_tx_queues;
1483 sd->data.dev_link = sd->shared_dev_data->dev_link;
1484 sd->data.mtu = sd->shared_dev_data->mtu;
1485 memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
1486 sizeof(sd->data.rx_queue_state));
1487 memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
1488 sizeof(sd->data.tx_queue_state));
1489 sd->data.dev_flags = sd->shared_dev_data->dev_flags;
1490 /* Use local data from now on. */
1492 priv->dev->data = &sd->data;
1494 priv_select_tx_function(priv);
1495 priv_select_rx_function(priv);
1498 /* More sanity checks. */
1499 assert(priv->dev->data == &sd->data);
1500 rte_spinlock_unlock(&sd->lock);
1504 rte_free(tx_queues);
1505 rte_free(rx_queues);
1506 rte_spinlock_unlock(&sd->lock);
1511 * Configure the TX function to use.
1514 * Pointer to private structure.
1517 priv_select_tx_function(struct priv *priv)
1519 priv->dev->tx_pkt_burst = mlx5_tx_burst;
1520 /* Display warning for unsupported configurations. */
1521 if (priv->sriov && priv->mps)
1522 WARN("multi-packet send WQE cannot be used on a SR-IOV setup");
1523 /* Select appropriate TX function. */
1524 if ((priv->sriov == 0) && priv->mps && priv->txq_inline) {
1525 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1526 DEBUG("selected MPW inline TX function");
1527 } else if ((priv->sriov == 0) && priv->mps) {
1528 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw;
1529 DEBUG("selected MPW TX function");
1534 * Configure the RX function to use.
1537 * Pointer to private structure.
1540 priv_select_rx_function(struct priv *priv)
1542 priv->dev->rx_pkt_burst = mlx5_rx_burst;