1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2012 6WIND S.A.
3 * Copyright 2012 Mellanox
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[] = {
65 * DPDK callback for Ethernet device configuration.
68 * Pointer to Ethernet device structure.
71 * 0 on success, negative errno value otherwise and rte_errno is set.
74 mlx4_dev_configure(struct rte_eth_dev *dev)
76 struct priv *priv = dev->data->dev_private;
77 struct rte_flow_error error;
80 /* Prepare internal flow rules. */
81 ret = mlx4_flow_sync(priv, &error);
83 ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
84 " flow error type %d, cause %p, message: %s",
85 -ret, strerror(-ret), error.type, error.cause,
86 error.message ? error.message : "(unspecified)");
89 ret = mlx4_intr_install(priv);
91 ERROR("%p: interrupt handler installation failed",
98 * DPDK callback to start the device.
100 * Simulate device start by initializing common RSS resources and attaching
101 * all configured flows.
104 * Pointer to Ethernet device structure.
107 * 0 on success, negative errno value otherwise and rte_errno is set.
110 mlx4_dev_start(struct rte_eth_dev *dev)
112 struct priv *priv = dev->data->dev_private;
113 struct rte_flow_error error;
118 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
120 ret = mlx4_rss_init(priv);
122 ERROR("%p: cannot initialize RSS resources: %s",
123 (void *)dev, strerror(-ret));
126 ret = mlx4_rxq_intr_enable(priv);
128 ERROR("%p: interrupt handler installation failed",
132 ret = mlx4_flow_sync(priv, &error);
134 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
135 " flow error type %d, cause %p, message: %s",
137 -ret, strerror(-ret), error.type, error.cause,
138 error.message ? error.message : "(unspecified)");
142 dev->tx_pkt_burst = mlx4_tx_burst;
143 dev->rx_pkt_burst = mlx4_rx_burst;
152 * DPDK callback to stop the device.
154 * Simulate device stop by detaching all configured flows.
157 * Pointer to Ethernet device structure.
160 mlx4_dev_stop(struct rte_eth_dev *dev)
162 struct priv *priv = dev->data->dev_private;
166 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
168 dev->tx_pkt_burst = mlx4_tx_burst_removed;
169 dev->rx_pkt_burst = mlx4_rx_burst_removed;
171 mlx4_flow_sync(priv, NULL);
172 mlx4_rxq_intr_disable(priv);
173 mlx4_rss_deinit(priv);
177 * DPDK callback to close the device.
179 * Destroy all queues and objects, free memory.
182 * Pointer to Ethernet device structure.
185 mlx4_dev_close(struct rte_eth_dev *dev)
187 struct priv *priv = dev->data->dev_private;
190 DEBUG("%p: closing device \"%s\"",
192 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
193 dev->rx_pkt_burst = mlx4_rx_burst_removed;
194 dev->tx_pkt_burst = mlx4_tx_burst_removed;
196 mlx4_flow_clean(priv);
197 for (i = 0; i != dev->data->nb_rx_queues; ++i)
198 mlx4_rx_queue_release(dev->data->rx_queues[i]);
199 for (i = 0; i != dev->data->nb_tx_queues; ++i)
200 mlx4_tx_queue_release(dev->data->tx_queues[i]);
201 if (priv->pd != NULL) {
202 assert(priv->ctx != NULL);
203 claim_zero(mlx4_glue->dealloc_pd(priv->pd));
204 claim_zero(mlx4_glue->close_device(priv->ctx));
206 assert(priv->ctx == NULL);
207 mlx4_intr_uninstall(priv);
208 memset(priv, 0, sizeof(*priv));
211 static const struct eth_dev_ops mlx4_dev_ops = {
212 .dev_configure = mlx4_dev_configure,
213 .dev_start = mlx4_dev_start,
214 .dev_stop = mlx4_dev_stop,
215 .dev_set_link_down = mlx4_dev_set_link_down,
216 .dev_set_link_up = mlx4_dev_set_link_up,
217 .dev_close = mlx4_dev_close,
218 .link_update = mlx4_link_update,
219 .promiscuous_enable = mlx4_promiscuous_enable,
220 .promiscuous_disable = mlx4_promiscuous_disable,
221 .allmulticast_enable = mlx4_allmulticast_enable,
222 .allmulticast_disable = mlx4_allmulticast_disable,
223 .mac_addr_remove = mlx4_mac_addr_remove,
224 .mac_addr_add = mlx4_mac_addr_add,
225 .mac_addr_set = mlx4_mac_addr_set,
226 .stats_get = mlx4_stats_get,
227 .stats_reset = mlx4_stats_reset,
228 .dev_infos_get = mlx4_dev_infos_get,
229 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
230 .vlan_filter_set = mlx4_vlan_filter_set,
231 .rx_queue_setup = mlx4_rx_queue_setup,
232 .tx_queue_setup = mlx4_tx_queue_setup,
233 .rx_queue_release = mlx4_rx_queue_release,
234 .tx_queue_release = mlx4_tx_queue_release,
235 .flow_ctrl_get = mlx4_flow_ctrl_get,
236 .flow_ctrl_set = mlx4_flow_ctrl_set,
237 .mtu_set = mlx4_mtu_set,
238 .filter_ctrl = mlx4_filter_ctrl,
239 .rx_queue_intr_enable = mlx4_rx_intr_enable,
240 .rx_queue_intr_disable = mlx4_rx_intr_disable,
241 .is_removed = mlx4_is_removed,
245 * Get PCI information from struct ibv_device.
248 * Pointer to Ethernet device structure.
249 * @param[out] pci_addr
250 * PCI bus address output buffer.
253 * 0 on success, negative errno value otherwise and rte_errno is set.
256 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
257 struct rte_pci_addr *pci_addr)
261 MKSTR(path, "%s/device/uevent", device->ibdev_path);
263 file = fopen(path, "rb");
268 while (fgets(line, sizeof(line), file) == line) {
269 size_t len = strlen(line);
272 /* Truncate long lines. */
273 if (len == (sizeof(line) - 1))
274 while (line[(len - 1)] != '\n') {
278 line[(len - 1)] = ret;
280 /* Extract information. */
283 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
287 &pci_addr->function) == 4) {
297 * Verify and store value for device argument.
300 * Key argument to verify.
302 * Value associated with key.
303 * @param[in, out] conf
304 * Shared configuration data.
307 * 0 on success, negative errno value otherwise and rte_errno is set.
310 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
315 tmp = strtoul(val, NULL, 0);
318 WARN("%s: \"%s\" is not a valid integer", key, val);
321 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
322 uint32_t ports = rte_log2_u32(conf->ports.present + 1);
325 ERROR("port index %lu outside range [0,%" PRIu32 ")",
329 if (!(conf->ports.present & (1 << tmp))) {
331 ERROR("invalid port index %lu", tmp);
334 conf->ports.enabled |= 1 << tmp;
337 WARN("%s: unknown parameter", key);
344 * Parse device parameters.
347 * Device arguments structure.
350 * 0 on success, negative errno value otherwise and rte_errno is set.
353 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
355 struct rte_kvargs *kvlist;
356 unsigned int arg_count;
362 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
363 if (kvlist == NULL) {
365 ERROR("failed to parse kvargs");
368 /* Process parameters. */
369 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
370 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
371 while (arg_count-- > 0) {
372 ret = rte_kvargs_process(kvlist,
374 (int (*)(const char *,
384 rte_kvargs_free(kvlist);
388 static struct rte_pci_driver mlx4_driver;
391 * DPDK callback to register a PCI device.
393 * This function creates an Ethernet device for each port of a given
397 * PCI driver structure (mlx4_driver).
399 * PCI device information.
402 * 0 on success, negative errno value otherwise and rte_errno is set.
405 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
407 struct ibv_device **list;
408 struct ibv_device *ibv_dev;
410 struct ibv_context *attr_ctx = NULL;
411 struct ibv_device_attr device_attr;
412 struct ibv_device_attr_ex device_attr_ex;
413 struct mlx4_conf conf = {
420 assert(pci_drv == &mlx4_driver);
421 list = mlx4_glue->get_device_list(&i);
425 if (rte_errno == ENOSYS)
426 ERROR("cannot list devices, is ib_uverbs loaded?");
431 * For each listed device, check related sysfs entry against
432 * the provided PCI ID.
435 struct rte_pci_addr pci_addr;
438 DEBUG("checking device \"%s\"", list[i]->name);
439 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
441 if ((pci_dev->addr.domain != pci_addr.domain) ||
442 (pci_dev->addr.bus != pci_addr.bus) ||
443 (pci_dev->addr.devid != pci_addr.devid) ||
444 (pci_dev->addr.function != pci_addr.function))
446 vf = (pci_dev->id.device_id ==
447 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
448 INFO("PCI information matches, using device \"%s\" (VF: %s)",
449 list[i]->name, (vf ? "true" : "false"));
450 attr_ctx = mlx4_glue->open_device(list[i]);
454 if (attr_ctx == NULL) {
455 mlx4_glue->free_device_list(list);
459 ERROR("cannot access device, is mlx4_ib loaded?");
463 ERROR("cannot use device, are drivers up to date?");
471 DEBUG("device opened");
472 if (mlx4_glue->query_device(attr_ctx, &device_attr)) {
476 INFO("%u port(s) detected", device_attr.phys_port_cnt);
477 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
478 if (mlx4_args(pci_dev->device.devargs, &conf)) {
479 ERROR("failed to process device arguments");
483 /* Use all ports when none are defined */
484 if (!conf.ports.enabled)
485 conf.ports.enabled = conf.ports.present;
486 /* Retrieve extended device attributes. */
487 if (mlx4_glue->query_device_ex(attr_ctx, NULL, &device_attr_ex)) {
491 assert(device_attr.max_sge >= MLX4_MAX_SGE);
492 for (i = 0; i < device_attr.phys_port_cnt; i++) {
493 uint32_t port = i + 1; /* ports are indexed from one */
494 struct ibv_context *ctx = NULL;
495 struct ibv_port_attr port_attr;
496 struct ibv_pd *pd = NULL;
497 struct priv *priv = NULL;
498 struct rte_eth_dev *eth_dev = NULL;
499 struct ether_addr mac;
501 /* If port is not enabled, skip. */
502 if (!(conf.ports.enabled & (1 << i)))
504 DEBUG("using port %u", port);
505 ctx = mlx4_glue->open_device(ibv_dev);
510 /* Check port status. */
511 err = mlx4_glue->query_port(ctx, port, &port_attr);
514 ERROR("port query failed: %s", strerror(rte_errno));
517 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
519 ERROR("port %d is not configured in Ethernet mode",
523 if (port_attr.state != IBV_PORT_ACTIVE)
524 DEBUG("port %d is not active: \"%s\" (%d)",
525 port, mlx4_glue->port_state_str(port_attr.state),
527 /* Make asynchronous FD non-blocking to handle interrupts. */
528 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
529 ERROR("cannot make asynchronous FD non-blocking: %s",
530 strerror(rte_errno));
533 /* Allocate protection domain. */
534 pd = mlx4_glue->alloc_pd(ctx);
537 ERROR("PD allocation failure");
540 /* from rte_ethdev.c */
541 priv = rte_zmalloc("ethdev private structure",
543 RTE_CACHE_LINE_SIZE);
546 ERROR("priv allocation failure");
550 priv->device_attr = device_attr;
553 priv->mtu = ETHER_MTU;
555 priv->hw_csum = !!(device_attr.device_cap_flags &
556 IBV_DEVICE_RAW_IP_CSUM);
557 DEBUG("checksum offloading is %ssupported",
558 (priv->hw_csum ? "" : "not "));
559 /* Only ConnectX-3 Pro supports tunneling. */
560 priv->hw_csum_l2tun =
562 (device_attr.vendor_part_id ==
563 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO);
564 DEBUG("L2 tunnel checksum offloads are %ssupported",
565 (priv->hw_csum_l2tun ? "" : "not "));
566 priv->hw_rss_sup = device_attr_ex.rss_caps.rx_hash_fields_mask;
567 if (!priv->hw_rss_sup) {
568 WARN("no RSS capabilities reported; disabling support"
569 " for UDP RSS and inner VXLAN RSS");
570 /* Fake support for all possible RSS hash fields. */
571 priv->hw_rss_sup = ~UINT64_C(0);
572 priv->hw_rss_sup = mlx4_conv_rss_hf(priv, -1);
573 /* Filter out known unsupported fields. */
575 ~(uint64_t)(IBV_RX_HASH_SRC_PORT_UDP |
576 IBV_RX_HASH_DST_PORT_UDP |
579 DEBUG("supported RSS hash fields mask: %016" PRIx64,
581 /* Configure the first MAC address by default. */
582 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
583 ERROR("cannot get MAC address, is mlx4_en loaded?"
584 " (rte_errno: %s)", strerror(rte_errno));
587 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
589 mac.addr_bytes[0], mac.addr_bytes[1],
590 mac.addr_bytes[2], mac.addr_bytes[3],
591 mac.addr_bytes[4], mac.addr_bytes[5]);
592 /* Register MAC address. */
596 char ifname[IF_NAMESIZE];
598 if (mlx4_get_ifname(priv, &ifname) == 0)
599 DEBUG("port %u ifname is \"%s\"",
602 DEBUG("port %u ifname is unknown", priv->port);
605 /* Get actual MTU if possible. */
606 mlx4_mtu_get(priv, &priv->mtu);
607 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
608 /* from rte_ethdev.c */
610 char name[RTE_ETH_NAME_MAX_LEN];
612 snprintf(name, sizeof(name), "%s port %u",
613 mlx4_glue->get_device_name(ibv_dev), port);
614 eth_dev = rte_eth_dev_allocate(name);
616 if (eth_dev == NULL) {
617 ERROR("can not allocate rte ethdev");
621 eth_dev->data->dev_private = priv;
622 eth_dev->data->mac_addrs = priv->mac;
623 eth_dev->device = &pci_dev->device;
624 rte_eth_copy_pci_info(eth_dev, pci_dev);
625 eth_dev->device->driver = &mlx4_driver.driver;
626 /* Initialize local interrupt handle for current port. */
627 priv->intr_handle = (struct rte_intr_handle){
629 .type = RTE_INTR_HANDLE_EXT,
632 * Override ethdev interrupt handle pointer with private
633 * handle instead of that of the parent PCI device used by
634 * default. This prevents it from being shared between all
635 * ports of the same PCI device since each of them is
636 * associated its own Verbs context.
638 * Rx interrupts in particular require this as the PMD has
639 * no control over the registration of queue interrupts
640 * besides setting up eth_dev->intr_handle, the rest is
641 * handled by rte_intr_rx_ctl().
643 eth_dev->intr_handle = &priv->intr_handle;
645 eth_dev->dev_ops = &mlx4_dev_ops;
646 /* Bring Ethernet device up. */
647 DEBUG("forcing Ethernet interface up");
648 mlx4_dev_set_link_up(priv->dev);
649 /* Update link status once if waiting for LSC. */
650 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
651 mlx4_link_update(eth_dev, 0);
656 claim_zero(mlx4_glue->dealloc_pd(pd));
658 claim_zero(mlx4_glue->close_device(ctx));
660 rte_eth_dev_release_port(eth_dev);
663 if (i == device_attr.phys_port_cnt)
666 * XXX if something went wrong in the loop above, there is a resource
667 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
668 * long as the dpdk does not provide a way to deallocate a ethdev and a
669 * way to enumerate the registered ethdevs to free the previous ones.
673 claim_zero(mlx4_glue->close_device(attr_ctx));
675 mlx4_glue->free_device_list(list);
676 assert(rte_errno >= 0);
680 static const struct rte_pci_id mlx4_pci_id_map[] = {
682 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
683 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
686 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
687 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
690 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
691 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
698 static struct rte_pci_driver mlx4_driver = {
700 .name = MLX4_DRIVER_NAME
702 .id_table = mlx4_pci_id_map,
703 .probe = mlx4_pci_probe,
704 .drv_flags = RTE_PCI_DRV_INTR_LSC |
705 RTE_PCI_DRV_INTR_RMV,
708 #ifdef RTE_LIBRTE_MLX4_DLOPEN_DEPS
711 * Initialization routine for run-time dependency on rdma-core.
716 const char *path[] = {
718 * A basic security check is necessary before trusting
719 * MLX4_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
721 (geteuid() == getuid() && getegid() == getgid() ?
722 getenv("MLX4_GLUE_PATH") : NULL),
730 while (!handle && i != RTE_DIM(path)) {
739 end = strpbrk(path[i], ":;");
741 end = path[i] + strlen(path[i]);
747 ret = snprintf(name, sizeof(name), "%.*s%s" MLX4_GLUE,
749 (!len || *(end - 1) == '/') ? "" : "/");
752 if (sizeof(name) != (size_t)ret + 1)
754 DEBUG("looking for rdma-core glue as \"%s\"", name);
755 handle = dlopen(name, RTLD_LAZY);
766 WARN("cannot load glue library: %s", dlmsg);
769 sym = dlsym(handle, "mlx4_glue");
774 ERROR("cannot resolve glue symbol: %s", dlmsg);
782 WARN("cannot initialize PMD due to missing run-time"
783 " dependency on rdma-core libraries (libibverbs,"
791 * Driver initialization routine.
793 RTE_INIT(rte_mlx4_pmd_init);
795 rte_mlx4_pmd_init(void)
798 * MLX4_DEVICE_FATAL_CLEANUP tells ibv_destroy functions we
799 * want to get success errno value in case of calling them
800 * when the device was removed.
802 setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
804 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
805 * huge pages. Calling ibv_fork_init() during init allows
806 * applications to use fork() safely for purposes other than
807 * using this PMD, which is not supported in forked processes.
809 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
810 #ifdef RTE_LIBRTE_MLX4_DLOPEN_DEPS
811 if (mlx4_glue_init())
816 /* Glue structure must not contain any NULL pointers. */
820 for (i = 0; i != sizeof(*mlx4_glue) / sizeof(void *); ++i)
821 assert(((const void *const *)mlx4_glue)[i]);
824 if (strcmp(mlx4_glue->version, MLX4_GLUE_VERSION)) {
825 ERROR("rdma-core glue \"%s\" mismatch: \"%s\" is required",
826 mlx4_glue->version, MLX4_GLUE_VERSION);
829 mlx4_glue->fork_init();
830 rte_pci_register(&mlx4_driver);
833 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
834 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
835 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
836 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");