1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2012 6WIND S.A.
3 * Copyright 2012 Mellanox Technologies, Ltd
8 * mlx4 driver initialization.
22 /* Verbs headers do not support -pedantic. */
24 #pragma GCC diagnostic ignored "-Wpedantic"
26 #include <infiniband/verbs.h>
28 #pragma GCC diagnostic error "-Wpedantic"
31 #include <rte_common.h>
32 #include <rte_config.h>
34 #include <rte_errno.h>
35 #include <rte_ethdev_driver.h>
36 #include <rte_ethdev_pci.h>
37 #include <rte_ether.h>
39 #include <rte_interrupts.h>
40 #include <rte_kvargs.h>
41 #include <rte_malloc.h>
45 #include "mlx4_glue.h"
46 #include "mlx4_flow.h"
47 #include "mlx4_rxtx.h"
48 #include "mlx4_utils.h"
50 /** Configuration structure for device arguments. */
53 uint32_t present; /**< Bit-field for existing ports. */
54 uint32_t enabled; /**< Bit-field for user-enabled ports. */
58 /* Available parameters list. */
59 const char *pmd_mlx4_init_params[] = {
64 static void mlx4_dev_stop(struct rte_eth_dev *dev);
67 * DPDK callback for Ethernet device configuration.
70 * Pointer to Ethernet device structure.
73 * 0 on success, negative errno value otherwise and rte_errno is set.
76 mlx4_dev_configure(struct rte_eth_dev *dev)
78 struct priv *priv = dev->data->dev_private;
79 struct rte_flow_error error;
82 /* Prepare internal flow rules. */
83 ret = mlx4_flow_sync(priv, &error);
85 ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
86 " flow error type %d, cause %p, message: %s",
87 -ret, strerror(-ret), error.type, error.cause,
88 error.message ? error.message : "(unspecified)");
91 ret = mlx4_intr_install(priv);
93 ERROR("%p: interrupt handler installation failed",
100 * DPDK callback to start the device.
102 * Simulate device start by initializing common RSS resources and attaching
103 * all configured flows.
106 * Pointer to Ethernet device structure.
109 * 0 on success, negative errno value otherwise and rte_errno is set.
112 mlx4_dev_start(struct rte_eth_dev *dev)
114 struct priv *priv = dev->data->dev_private;
115 struct rte_flow_error error;
120 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
122 ret = mlx4_rss_init(priv);
124 ERROR("%p: cannot initialize RSS resources: %s",
125 (void *)dev, strerror(-ret));
128 ret = mlx4_rxq_intr_enable(priv);
130 ERROR("%p: interrupt handler installation failed",
134 ret = mlx4_flow_sync(priv, &error);
136 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
137 " flow error type %d, cause %p, message: %s",
139 -ret, strerror(-ret), error.type, error.cause,
140 error.message ? error.message : "(unspecified)");
144 dev->tx_pkt_burst = mlx4_tx_burst;
145 dev->rx_pkt_burst = mlx4_rx_burst;
153 * DPDK callback to stop the device.
155 * Simulate device stop by detaching all configured flows.
158 * Pointer to Ethernet device structure.
161 mlx4_dev_stop(struct rte_eth_dev *dev)
163 struct priv *priv = dev->data->dev_private;
167 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
169 dev->tx_pkt_burst = mlx4_tx_burst_removed;
170 dev->rx_pkt_burst = mlx4_rx_burst_removed;
172 mlx4_flow_sync(priv, NULL);
173 mlx4_rxq_intr_disable(priv);
174 mlx4_rss_deinit(priv);
178 * DPDK callback to close the device.
180 * Destroy all queues and objects, free memory.
183 * Pointer to Ethernet device structure.
186 mlx4_dev_close(struct rte_eth_dev *dev)
188 struct priv *priv = dev->data->dev_private;
191 DEBUG("%p: closing device \"%s\"",
193 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
194 dev->rx_pkt_burst = mlx4_rx_burst_removed;
195 dev->tx_pkt_burst = mlx4_tx_burst_removed;
197 mlx4_flow_clean(priv);
198 mlx4_rss_deinit(priv);
199 for (i = 0; i != dev->data->nb_rx_queues; ++i)
200 mlx4_rx_queue_release(dev->data->rx_queues[i]);
201 for (i = 0; i != dev->data->nb_tx_queues; ++i)
202 mlx4_tx_queue_release(dev->data->tx_queues[i]);
203 if (priv->pd != NULL) {
204 assert(priv->ctx != NULL);
205 claim_zero(mlx4_glue->dealloc_pd(priv->pd));
206 claim_zero(mlx4_glue->close_device(priv->ctx));
208 assert(priv->ctx == NULL);
209 mlx4_intr_uninstall(priv);
210 memset(priv, 0, sizeof(*priv));
213 static const struct eth_dev_ops mlx4_dev_ops = {
214 .dev_configure = mlx4_dev_configure,
215 .dev_start = mlx4_dev_start,
216 .dev_stop = mlx4_dev_stop,
217 .dev_set_link_down = mlx4_dev_set_link_down,
218 .dev_set_link_up = mlx4_dev_set_link_up,
219 .dev_close = mlx4_dev_close,
220 .link_update = mlx4_link_update,
221 .promiscuous_enable = mlx4_promiscuous_enable,
222 .promiscuous_disable = mlx4_promiscuous_disable,
223 .allmulticast_enable = mlx4_allmulticast_enable,
224 .allmulticast_disable = mlx4_allmulticast_disable,
225 .mac_addr_remove = mlx4_mac_addr_remove,
226 .mac_addr_add = mlx4_mac_addr_add,
227 .mac_addr_set = mlx4_mac_addr_set,
228 .stats_get = mlx4_stats_get,
229 .stats_reset = mlx4_stats_reset,
230 .dev_infos_get = mlx4_dev_infos_get,
231 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
232 .vlan_filter_set = mlx4_vlan_filter_set,
233 .rx_queue_setup = mlx4_rx_queue_setup,
234 .tx_queue_setup = mlx4_tx_queue_setup,
235 .rx_queue_release = mlx4_rx_queue_release,
236 .tx_queue_release = mlx4_tx_queue_release,
237 .flow_ctrl_get = mlx4_flow_ctrl_get,
238 .flow_ctrl_set = mlx4_flow_ctrl_set,
239 .mtu_set = mlx4_mtu_set,
240 .filter_ctrl = mlx4_filter_ctrl,
241 .rx_queue_intr_enable = mlx4_rx_intr_enable,
242 .rx_queue_intr_disable = mlx4_rx_intr_disable,
243 .is_removed = mlx4_is_removed,
247 * Get PCI information from struct ibv_device.
250 * Pointer to Ethernet device structure.
251 * @param[out] pci_addr
252 * PCI bus address output buffer.
255 * 0 on success, negative errno value otherwise and rte_errno is set.
258 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
259 struct rte_pci_addr *pci_addr)
263 MKSTR(path, "%s/device/uevent", device->ibdev_path);
265 file = fopen(path, "rb");
270 while (fgets(line, sizeof(line), file) == line) {
271 size_t len = strlen(line);
274 /* Truncate long lines. */
275 if (len == (sizeof(line) - 1))
276 while (line[(len - 1)] != '\n') {
280 line[(len - 1)] = ret;
282 /* Extract information. */
285 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
289 &pci_addr->function) == 4) {
299 * Verify and store value for device argument.
302 * Key argument to verify.
304 * Value associated with key.
305 * @param[in, out] conf
306 * Shared configuration data.
309 * 0 on success, negative errno value otherwise and rte_errno is set.
312 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
317 tmp = strtoul(val, NULL, 0);
320 WARN("%s: \"%s\" is not a valid integer", key, val);
323 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
324 uint32_t ports = rte_log2_u32(conf->ports.present + 1);
327 ERROR("port index %lu outside range [0,%" PRIu32 ")",
331 if (!(conf->ports.present & (1 << tmp))) {
333 ERROR("invalid port index %lu", tmp);
336 conf->ports.enabled |= 1 << tmp;
339 WARN("%s: unknown parameter", key);
346 * Parse device parameters.
349 * Device arguments structure.
352 * 0 on success, negative errno value otherwise and rte_errno is set.
355 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
357 struct rte_kvargs *kvlist;
358 unsigned int arg_count;
364 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
365 if (kvlist == NULL) {
367 ERROR("failed to parse kvargs");
370 /* Process parameters. */
371 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
372 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
373 while (arg_count-- > 0) {
374 ret = rte_kvargs_process(kvlist,
376 (int (*)(const char *,
386 rte_kvargs_free(kvlist);
390 static struct rte_pci_driver mlx4_driver;
393 * DPDK callback to register a PCI device.
395 * This function creates an Ethernet device for each port of a given
399 * PCI driver structure (mlx4_driver).
401 * PCI device information.
404 * 0 on success, negative errno value otherwise and rte_errno is set.
407 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
409 struct ibv_device **list;
410 struct ibv_device *ibv_dev;
412 struct ibv_context *attr_ctx = NULL;
413 struct ibv_device_attr device_attr;
414 struct ibv_device_attr_ex device_attr_ex;
415 struct mlx4_conf conf = {
422 assert(pci_drv == &mlx4_driver);
423 list = mlx4_glue->get_device_list(&i);
427 if (rte_errno == ENOSYS)
428 ERROR("cannot list devices, is ib_uverbs loaded?");
433 * For each listed device, check related sysfs entry against
434 * the provided PCI ID.
437 struct rte_pci_addr pci_addr;
440 DEBUG("checking device \"%s\"", list[i]->name);
441 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
443 if ((pci_dev->addr.domain != pci_addr.domain) ||
444 (pci_dev->addr.bus != pci_addr.bus) ||
445 (pci_dev->addr.devid != pci_addr.devid) ||
446 (pci_dev->addr.function != pci_addr.function))
448 vf = (pci_dev->id.device_id ==
449 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
450 INFO("PCI information matches, using device \"%s\" (VF: %s)",
451 list[i]->name, (vf ? "true" : "false"));
452 attr_ctx = mlx4_glue->open_device(list[i]);
456 if (attr_ctx == NULL) {
457 mlx4_glue->free_device_list(list);
461 ERROR("cannot access device, is mlx4_ib loaded?");
465 ERROR("cannot use device, are drivers up to date?");
473 DEBUG("device opened");
474 if (mlx4_glue->query_device(attr_ctx, &device_attr)) {
478 INFO("%u port(s) detected", device_attr.phys_port_cnt);
479 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
480 if (mlx4_args(pci_dev->device.devargs, &conf)) {
481 ERROR("failed to process device arguments");
485 /* Use all ports when none are defined */
486 if (!conf.ports.enabled)
487 conf.ports.enabled = conf.ports.present;
488 /* Retrieve extended device attributes. */
489 if (mlx4_glue->query_device_ex(attr_ctx, NULL, &device_attr_ex)) {
493 assert(device_attr.max_sge >= MLX4_MAX_SGE);
494 for (i = 0; i < device_attr.phys_port_cnt; i++) {
495 uint32_t port = i + 1; /* ports are indexed from one */
496 struct ibv_context *ctx = NULL;
497 struct ibv_port_attr port_attr;
498 struct ibv_pd *pd = NULL;
499 struct priv *priv = NULL;
500 struct rte_eth_dev *eth_dev = NULL;
501 struct ether_addr mac;
503 /* If port is not enabled, skip. */
504 if (!(conf.ports.enabled & (1 << i)))
506 DEBUG("using port %u", port);
507 ctx = mlx4_glue->open_device(ibv_dev);
512 /* Check port status. */
513 err = mlx4_glue->query_port(ctx, port, &port_attr);
516 ERROR("port query failed: %s", strerror(rte_errno));
519 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
521 ERROR("port %d is not configured in Ethernet mode",
525 if (port_attr.state != IBV_PORT_ACTIVE)
526 DEBUG("port %d is not active: \"%s\" (%d)",
527 port, mlx4_glue->port_state_str(port_attr.state),
529 /* Make asynchronous FD non-blocking to handle interrupts. */
530 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
531 ERROR("cannot make asynchronous FD non-blocking: %s",
532 strerror(rte_errno));
535 /* Allocate protection domain. */
536 pd = mlx4_glue->alloc_pd(ctx);
539 ERROR("PD allocation failure");
542 /* from rte_ethdev.c */
543 priv = rte_zmalloc("ethdev private structure",
545 RTE_CACHE_LINE_SIZE);
548 ERROR("priv allocation failure");
552 priv->device_attr = device_attr;
555 priv->mtu = ETHER_MTU;
557 priv->hw_csum = !!(device_attr.device_cap_flags &
558 IBV_DEVICE_RAW_IP_CSUM);
559 DEBUG("checksum offloading is %ssupported",
560 (priv->hw_csum ? "" : "not "));
561 /* Only ConnectX-3 Pro supports tunneling. */
562 priv->hw_csum_l2tun =
564 (device_attr.vendor_part_id ==
565 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO);
566 DEBUG("L2 tunnel checksum offloads are %ssupported",
567 priv->hw_csum_l2tun ? "" : "not ");
568 priv->hw_rss_sup = device_attr_ex.rss_caps.rx_hash_fields_mask;
569 if (!priv->hw_rss_sup) {
570 WARN("no RSS capabilities reported; disabling support"
571 " for UDP RSS and inner VXLAN RSS");
573 IBV_RX_HASH_SRC_IPV4 |
574 IBV_RX_HASH_DST_IPV4 |
575 IBV_RX_HASH_SRC_IPV6 |
576 IBV_RX_HASH_DST_IPV6 |
577 IBV_RX_HASH_SRC_PORT_TCP |
578 IBV_RX_HASH_DST_PORT_TCP;
580 DEBUG("supported RSS hash fields mask: %016" PRIx64,
582 priv->hw_fcs_strip = !!(device_attr_ex.raw_packet_caps &
583 IBV_RAW_PACKET_CAP_SCATTER_FCS);
584 DEBUG("FCS stripping toggling is %ssupported",
585 priv->hw_fcs_strip ? "" : "not ");
586 /* Configure the first MAC address by default. */
587 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
588 ERROR("cannot get MAC address, is mlx4_en loaded?"
589 " (rte_errno: %s)", strerror(rte_errno));
592 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
594 mac.addr_bytes[0], mac.addr_bytes[1],
595 mac.addr_bytes[2], mac.addr_bytes[3],
596 mac.addr_bytes[4], mac.addr_bytes[5]);
597 /* Register MAC address. */
601 char ifname[IF_NAMESIZE];
603 if (mlx4_get_ifname(priv, &ifname) == 0)
604 DEBUG("port %u ifname is \"%s\"",
607 DEBUG("port %u ifname is unknown", priv->port);
610 /* Get actual MTU if possible. */
611 mlx4_mtu_get(priv, &priv->mtu);
612 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
613 /* from rte_ethdev.c */
615 char name[RTE_ETH_NAME_MAX_LEN];
617 snprintf(name, sizeof(name), "%s port %u",
618 mlx4_glue->get_device_name(ibv_dev), port);
619 eth_dev = rte_eth_dev_allocate(name);
621 if (eth_dev == NULL) {
622 ERROR("can not allocate rte ethdev");
626 eth_dev->data->dev_private = priv;
627 eth_dev->data->mac_addrs = priv->mac;
628 eth_dev->device = &pci_dev->device;
629 rte_eth_copy_pci_info(eth_dev, pci_dev);
630 eth_dev->device->driver = &mlx4_driver.driver;
631 /* Initialize local interrupt handle for current port. */
632 priv->intr_handle = (struct rte_intr_handle){
634 .type = RTE_INTR_HANDLE_EXT,
637 * Override ethdev interrupt handle pointer with private
638 * handle instead of that of the parent PCI device used by
639 * default. This prevents it from being shared between all
640 * ports of the same PCI device since each of them is
641 * associated its own Verbs context.
643 * Rx interrupts in particular require this as the PMD has
644 * no control over the registration of queue interrupts
645 * besides setting up eth_dev->intr_handle, the rest is
646 * handled by rte_intr_rx_ctl().
648 eth_dev->intr_handle = &priv->intr_handle;
650 eth_dev->dev_ops = &mlx4_dev_ops;
651 /* Bring Ethernet device up. */
652 DEBUG("forcing Ethernet interface up");
653 mlx4_dev_set_link_up(priv->dev);
654 /* Update link status once if waiting for LSC. */
655 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
656 mlx4_link_update(eth_dev, 0);
661 claim_zero(mlx4_glue->dealloc_pd(pd));
663 claim_zero(mlx4_glue->close_device(ctx));
665 rte_eth_dev_release_port(eth_dev);
668 if (i == device_attr.phys_port_cnt)
671 * XXX if something went wrong in the loop above, there is a resource
672 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
673 * long as the dpdk does not provide a way to deallocate a ethdev and a
674 * way to enumerate the registered ethdevs to free the previous ones.
678 claim_zero(mlx4_glue->close_device(attr_ctx));
680 mlx4_glue->free_device_list(list);
681 assert(rte_errno >= 0);
685 static const struct rte_pci_id mlx4_pci_id_map[] = {
687 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
688 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
691 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
692 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
695 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
696 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
703 static struct rte_pci_driver mlx4_driver = {
705 .name = MLX4_DRIVER_NAME
707 .id_table = mlx4_pci_id_map,
708 .probe = mlx4_pci_probe,
709 .drv_flags = RTE_PCI_DRV_INTR_LSC |
710 RTE_PCI_DRV_INTR_RMV,
713 #ifdef RTE_LIBRTE_MLX4_DLOPEN_DEPS
716 * Suffix RTE_EAL_PMD_PATH with "-glue".
718 * This function performs a sanity check on RTE_EAL_PMD_PATH before
719 * suffixing its last component.
722 * Output buffer, should be large enough otherwise NULL is returned.
727 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
730 mlx4_glue_path(char *buf, size_t size)
732 static const char *const bad[] = { "/", ".", "..", NULL };
733 const char *path = RTE_EAL_PMD_PATH;
734 size_t len = strlen(path);
738 while (len && path[len - 1] == '/')
740 for (off = len; off && path[off - 1] != '/'; --off)
742 for (i = 0; bad[i]; ++i)
743 if (!strncmp(path + off, bad[i], (int)(len - off)))
745 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
746 if (i == -1 || (size_t)i >= size)
750 ERROR("unable to append \"-glue\" to last component of"
751 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
752 " please re-configure DPDK");
757 * Initialization routine for run-time dependency on rdma-core.
762 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
763 const char *path[] = {
765 * A basic security check is necessary before trusting
766 * MLX4_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
768 (geteuid() == getuid() && getegid() == getgid() ?
769 getenv("MLX4_GLUE_PATH") : NULL),
771 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
772 * variant, otherwise let dlopen() look up libraries on its
776 mlx4_glue_path(glue_path, sizeof(glue_path)) : ""),
783 while (!handle && i != RTE_DIM(path)) {
792 end = strpbrk(path[i], ":;");
794 end = path[i] + strlen(path[i]);
800 ret = snprintf(name, sizeof(name), "%.*s%s" MLX4_GLUE,
802 (!len || *(end - 1) == '/') ? "" : "/");
805 if (sizeof(name) != (size_t)ret + 1)
807 DEBUG("looking for rdma-core glue as \"%s\"", name);
808 handle = dlopen(name, RTLD_LAZY);
819 WARN("cannot load glue library: %s", dlmsg);
822 sym = dlsym(handle, "mlx4_glue");
827 ERROR("cannot resolve glue symbol: %s", dlmsg);
835 WARN("cannot initialize PMD due to missing run-time"
836 " dependency on rdma-core libraries (libibverbs,"
844 * Driver initialization routine.
846 RTE_INIT(rte_mlx4_pmd_init);
848 rte_mlx4_pmd_init(void)
851 * MLX4_DEVICE_FATAL_CLEANUP tells ibv_destroy functions we
852 * want to get success errno value in case of calling them
853 * when the device was removed.
855 setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
857 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
858 * huge pages. Calling ibv_fork_init() during init allows
859 * applications to use fork() safely for purposes other than
860 * using this PMD, which is not supported in forked processes.
862 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
863 #ifdef RTE_LIBRTE_MLX4_DLOPEN_DEPS
864 if (mlx4_glue_init())
869 /* Glue structure must not contain any NULL pointers. */
873 for (i = 0; i != sizeof(*mlx4_glue) / sizeof(void *); ++i)
874 assert(((const void *const *)mlx4_glue)[i]);
877 if (strcmp(mlx4_glue->version, MLX4_GLUE_VERSION)) {
878 ERROR("rdma-core glue \"%s\" mismatch: \"%s\" is required",
879 mlx4_glue->version, MLX4_GLUE_VERSION);
882 mlx4_glue->fork_init();
883 rte_pci_register(&mlx4_driver);
886 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
887 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
888 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
889 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");