1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2012 6WIND S.A.
3 * Copyright 2012 Mellanox Technologies, Ltd
8 * mlx4 driver initialization.
20 #ifdef RTE_IBVERBS_LINK_DLOPEN
24 /* Verbs headers do not support -pedantic. */
26 #pragma GCC diagnostic ignored "-Wpedantic"
28 #include <infiniband/verbs.h>
30 #pragma GCC diagnostic error "-Wpedantic"
33 #include <rte_common.h>
35 #include <rte_errno.h>
36 #include <ethdev_driver.h>
37 #include <ethdev_pci.h>
38 #include <rte_ether.h>
40 #include <rte_interrupts.h>
41 #include <rte_kvargs.h>
42 #include <rte_malloc.h>
46 #include "mlx4_glue.h"
47 #include "mlx4_flow.h"
49 #include "mlx4_rxtx.h"
50 #include "mlx4_utils.h"
53 const struct mlx4_glue *mlx4_glue;
56 static const char *MZ_MLX4_PMD_SHARED_DATA = "mlx4_pmd_shared_data";
58 /* Shared memory between primary and secondary processes. */
59 struct mlx4_shared_data *mlx4_shared_data;
61 /* Spinlock for mlx4_shared_data allocation. */
62 static rte_spinlock_t mlx4_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
64 /* Process local data for secondary processes. */
65 static struct mlx4_local_data mlx4_local_data;
67 /** Configuration structure for device arguments. */
70 uint32_t present; /**< Bit-field for existing ports. */
71 uint32_t enabled; /**< Bit-field for user-enabled ports. */
74 /** Whether memseg should be extended for MR creation. */
77 /* Available parameters list. */
78 const char *pmd_mlx4_init_params[] = {
80 MLX4_MR_EXT_MEMSEG_EN_KVARG,
84 static int mlx4_dev_stop(struct rte_eth_dev *dev);
87 * Initialize shared data between primary and secondary process.
89 * A memzone is reserved by primary process and secondary processes attach to
93 * 0 on success, a negative errno value otherwise and rte_errno is set.
96 mlx4_init_shared_data(void)
98 const struct rte_memzone *mz;
101 rte_spinlock_lock(&mlx4_shared_data_lock);
102 if (mlx4_shared_data == NULL) {
103 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
104 /* Allocate shared memory. */
105 mz = rte_memzone_reserve(MZ_MLX4_PMD_SHARED_DATA,
106 sizeof(*mlx4_shared_data),
109 ERROR("Cannot allocate mlx4 shared data\n");
113 mlx4_shared_data = mz->addr;
114 memset(mlx4_shared_data, 0, sizeof(*mlx4_shared_data));
115 rte_spinlock_init(&mlx4_shared_data->lock);
117 /* Lookup allocated shared memory. */
118 mz = rte_memzone_lookup(MZ_MLX4_PMD_SHARED_DATA);
120 ERROR("Cannot attach mlx4 shared data\n");
124 mlx4_shared_data = mz->addr;
125 memset(&mlx4_local_data, 0, sizeof(mlx4_local_data));
129 rte_spinlock_unlock(&mlx4_shared_data_lock);
133 #ifdef HAVE_IBV_MLX4_BUF_ALLOCATORS
135 * Verbs callback to allocate a memory. This function should allocate the space
136 * according to the size provided residing inside a huge page.
137 * Please note that all allocation must respect the alignment from libmlx4
138 * (i.e. currently sysconf(_SC_PAGESIZE)).
141 * The size in bytes of the memory to allocate.
143 * A pointer to the callback data.
146 * Allocated buffer, NULL otherwise and rte_errno is set.
149 mlx4_alloc_verbs_buf(size_t size, void *data)
151 struct mlx4_priv *priv = data;
153 size_t alignment = sysconf(_SC_PAGESIZE);
154 unsigned int socket = SOCKET_ID_ANY;
156 if (priv->verbs_alloc_ctx.type == MLX4_VERBS_ALLOC_TYPE_TX_QUEUE) {
157 const struct txq *txq = priv->verbs_alloc_ctx.obj;
159 socket = txq->socket;
160 } else if (priv->verbs_alloc_ctx.type ==
161 MLX4_VERBS_ALLOC_TYPE_RX_QUEUE) {
162 const struct rxq *rxq = priv->verbs_alloc_ctx.obj;
164 socket = rxq->socket;
166 MLX4_ASSERT(data != NULL);
167 ret = rte_malloc_socket(__func__, size, alignment, socket);
174 * Verbs callback to free a memory.
177 * A pointer to the memory to free.
179 * A pointer to the callback data.
182 mlx4_free_verbs_buf(void *ptr, void *data __rte_unused)
184 MLX4_ASSERT(data != NULL);
190 * Initialize process private data structure.
193 * Pointer to Ethernet device structure.
196 * 0 on success, a negative errno value otherwise and rte_errno is set.
199 mlx4_proc_priv_init(struct rte_eth_dev *dev)
201 struct mlx4_proc_priv *ppriv;
205 * UAR register table follows the process private structure. BlueFlame
206 * registers for Tx queues are stored in the table.
208 ppriv_size = sizeof(struct mlx4_proc_priv) +
209 dev->data->nb_tx_queues * sizeof(void *);
210 ppriv = rte_malloc_socket("mlx4_proc_priv", ppriv_size,
211 RTE_CACHE_LINE_SIZE, dev->device->numa_node);
216 ppriv->uar_table_sz = ppriv_size;
217 dev->process_private = ppriv;
222 * Un-initialize process private data structure.
225 * Pointer to Ethernet device structure.
228 mlx4_proc_priv_uninit(struct rte_eth_dev *dev)
230 if (!dev->process_private)
232 rte_free(dev->process_private);
233 dev->process_private = NULL;
237 * DPDK callback for Ethernet device configuration.
240 * Pointer to Ethernet device structure.
243 * 0 on success, negative errno value otherwise and rte_errno is set.
246 mlx4_dev_configure(struct rte_eth_dev *dev)
248 struct mlx4_priv *priv = dev->data->dev_private;
249 struct rte_flow_error error;
252 /* Prepare internal flow rules. */
253 ret = mlx4_flow_sync(priv, &error);
255 ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
256 " flow error type %d, cause %p, message: %s",
257 -ret, strerror(-ret), error.type, error.cause,
258 error.message ? error.message : "(unspecified)");
261 ret = mlx4_intr_install(priv);
263 ERROR("%p: interrupt handler installation failed",
267 ret = mlx4_proc_priv_init(dev);
269 ERROR("%p: process private data allocation failed",
278 * DPDK callback to start the device.
280 * Simulate device start by initializing common RSS resources and attaching
281 * all configured flows.
284 * Pointer to Ethernet device structure.
287 * 0 on success, negative errno value otherwise and rte_errno is set.
290 mlx4_dev_start(struct rte_eth_dev *dev)
292 struct mlx4_priv *priv = dev->data->dev_private;
293 struct rte_flow_error error;
298 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
300 ret = mlx4_rss_init(priv);
302 ERROR("%p: cannot initialize RSS resources: %s",
303 (void *)dev, strerror(-ret));
306 #ifdef RTE_LIBRTE_MLX4_DEBUG
307 mlx4_mr_dump_dev(dev);
309 ret = mlx4_rxq_intr_enable(priv);
311 ERROR("%p: interrupt handler installation failed",
315 ret = mlx4_flow_sync(priv, &error);
317 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
318 " flow error type %d, cause %p, message: %s",
320 -ret, strerror(-ret), error.type, error.cause,
321 error.message ? error.message : "(unspecified)");
325 dev->tx_pkt_burst = mlx4_tx_burst;
326 dev->rx_pkt_burst = mlx4_rx_burst;
327 /* Enable datapath on secondary process. */
328 mlx4_mp_req_start_rxtx(dev);
336 * DPDK callback to stop the device.
338 * Simulate device stop by detaching all configured flows.
341 * Pointer to Ethernet device structure.
344 mlx4_dev_stop(struct rte_eth_dev *dev)
346 struct mlx4_priv *priv = dev->data->dev_private;
350 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
352 dev->tx_pkt_burst = mlx4_tx_burst_removed;
353 dev->rx_pkt_burst = mlx4_rx_burst_removed;
355 /* Disable datapath on secondary process. */
356 mlx4_mp_req_stop_rxtx(dev);
357 mlx4_flow_sync(priv, NULL);
358 mlx4_rxq_intr_disable(priv);
359 mlx4_rss_deinit(priv);
365 * DPDK callback to close the device.
367 * Destroy all queues and objects, free memory.
370 * Pointer to Ethernet device structure.
373 mlx4_dev_close(struct rte_eth_dev *dev)
375 struct mlx4_priv *priv = dev->data->dev_private;
378 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
379 rte_eth_dev_release_port(dev);
382 DEBUG("%p: closing device \"%s\"",
384 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
385 dev->rx_pkt_burst = mlx4_rx_burst_removed;
386 dev->tx_pkt_burst = mlx4_tx_burst_removed;
388 /* Disable datapath on secondary process. */
389 mlx4_mp_req_stop_rxtx(dev);
390 mlx4_flow_clean(priv);
391 mlx4_rss_deinit(priv);
392 for (i = 0; i != dev->data->nb_rx_queues; ++i)
393 mlx4_rx_queue_release(dev->data->rx_queues[i]);
394 for (i = 0; i != dev->data->nb_tx_queues; ++i)
395 mlx4_tx_queue_release(dev->data->tx_queues[i]);
396 mlx4_proc_priv_uninit(dev);
397 mlx4_mr_release(dev);
398 if (priv->pd != NULL) {
399 MLX4_ASSERT(priv->ctx != NULL);
400 claim_zero(mlx4_glue->dealloc_pd(priv->pd));
401 claim_zero(mlx4_glue->close_device(priv->ctx));
403 MLX4_ASSERT(priv->ctx == NULL);
404 mlx4_intr_uninstall(priv);
405 memset(priv, 0, sizeof(*priv));
406 /* mac_addrs must not be freed because part of dev_private */
407 dev->data->mac_addrs = NULL;
411 static const struct eth_dev_ops mlx4_dev_ops = {
412 .dev_configure = mlx4_dev_configure,
413 .dev_start = mlx4_dev_start,
414 .dev_stop = mlx4_dev_stop,
415 .dev_set_link_down = mlx4_dev_set_link_down,
416 .dev_set_link_up = mlx4_dev_set_link_up,
417 .dev_close = mlx4_dev_close,
418 .link_update = mlx4_link_update,
419 .promiscuous_enable = mlx4_promiscuous_enable,
420 .promiscuous_disable = mlx4_promiscuous_disable,
421 .allmulticast_enable = mlx4_allmulticast_enable,
422 .allmulticast_disable = mlx4_allmulticast_disable,
423 .mac_addr_remove = mlx4_mac_addr_remove,
424 .mac_addr_add = mlx4_mac_addr_add,
425 .mac_addr_set = mlx4_mac_addr_set,
426 .set_mc_addr_list = mlx4_set_mc_addr_list,
427 .stats_get = mlx4_stats_get,
428 .stats_reset = mlx4_stats_reset,
429 .fw_version_get = mlx4_fw_version_get,
430 .dev_infos_get = mlx4_dev_infos_get,
431 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
432 .vlan_filter_set = mlx4_vlan_filter_set,
433 .rx_queue_setup = mlx4_rx_queue_setup,
434 .tx_queue_setup = mlx4_tx_queue_setup,
435 .rx_queue_release = mlx4_rx_queue_release,
436 .tx_queue_release = mlx4_tx_queue_release,
437 .flow_ctrl_get = mlx4_flow_ctrl_get,
438 .flow_ctrl_set = mlx4_flow_ctrl_set,
439 .mtu_set = mlx4_mtu_set,
440 .filter_ctrl = mlx4_filter_ctrl,
441 .rx_queue_intr_enable = mlx4_rx_intr_enable,
442 .rx_queue_intr_disable = mlx4_rx_intr_disable,
443 .is_removed = mlx4_is_removed,
446 /* Available operations from secondary process. */
447 static const struct eth_dev_ops mlx4_dev_sec_ops = {
448 .stats_get = mlx4_stats_get,
449 .stats_reset = mlx4_stats_reset,
450 .fw_version_get = mlx4_fw_version_get,
451 .dev_infos_get = mlx4_dev_infos_get,
455 * Get PCI information from struct ibv_device.
458 * Pointer to Ethernet device structure.
459 * @param[out] pci_addr
460 * PCI bus address output buffer.
463 * 0 on success, negative errno value otherwise and rte_errno is set.
466 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
467 struct rte_pci_addr *pci_addr)
471 MKSTR(path, "%s/device/uevent", device->ibdev_path);
473 file = fopen(path, "rb");
478 while (fgets(line, sizeof(line), file) == line) {
479 size_t len = strlen(line);
482 /* Truncate long lines. */
483 if (len == (sizeof(line) - 1))
484 while (line[(len - 1)] != '\n') {
488 line[(len - 1)] = ret;
490 /* Extract information. */
493 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
497 &pci_addr->function) == 4) {
506 * Verify and store value for device argument.
509 * Key argument to verify.
511 * Value associated with key.
512 * @param[in, out] conf
513 * Shared configuration data.
516 * 0 on success, negative errno value otherwise and rte_errno is set.
519 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
524 tmp = strtoul(val, NULL, 0);
527 WARN("%s: \"%s\" is not a valid integer", key, val);
530 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
531 uint32_t ports = rte_log2_u32(conf->ports.present + 1);
534 ERROR("port index %lu outside range [0,%" PRIu32 ")",
538 if (!(conf->ports.present & (1 << tmp))) {
540 ERROR("invalid port index %lu", tmp);
543 conf->ports.enabled |= 1 << tmp;
544 } else if (strcmp(MLX4_MR_EXT_MEMSEG_EN_KVARG, key) == 0) {
545 conf->mr_ext_memseg_en = !!tmp;
548 WARN("%s: unknown parameter", key);
555 * Parse device parameters.
558 * Device arguments structure.
561 * 0 on success, negative errno value otherwise and rte_errno is set.
564 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
566 struct rte_kvargs *kvlist;
567 unsigned int arg_count;
573 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
574 if (kvlist == NULL) {
576 ERROR("failed to parse kvargs");
579 /* Process parameters. */
580 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
581 arg_count = rte_kvargs_count(kvlist, pmd_mlx4_init_params[i]);
582 while (arg_count-- > 0) {
583 ret = rte_kvargs_process(kvlist,
584 pmd_mlx4_init_params[i],
585 (int (*)(const char *,
595 rte_kvargs_free(kvlist);
600 * Interpret RSS capabilities reported by device.
602 * This function returns the set of usable Verbs RSS hash fields, kernel
603 * quirks taken into account.
608 * Verbs protection domain.
609 * @param device_attr_ex
610 * Extended device attributes to interpret.
613 * Usable RSS hash fields mask in Verbs format.
616 mlx4_hw_rss_sup(struct ibv_context *ctx, struct ibv_pd *pd,
617 struct ibv_device_attr_ex *device_attr_ex)
619 uint64_t hw_rss_sup = device_attr_ex->rss_caps.rx_hash_fields_mask;
620 struct ibv_cq *cq = NULL;
621 struct ibv_wq *wq = NULL;
622 struct ibv_rwq_ind_table *ind = NULL;
623 struct ibv_qp *qp = NULL;
626 WARN("no RSS capabilities reported; disabling support for UDP"
627 " RSS and inner VXLAN RSS");
628 return IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4 |
629 IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6 |
630 IBV_RX_HASH_SRC_PORT_TCP | IBV_RX_HASH_DST_PORT_TCP;
632 if (!(hw_rss_sup & IBV_RX_HASH_INNER))
635 * Although reported as supported, missing code in some Linux
636 * versions (v4.15, v4.16) prevents the creation of hash QPs with
639 * There is no choice but to attempt to instantiate a temporary RSS
640 * context in order to confirm its support.
642 cq = mlx4_glue->create_cq(ctx, 1, NULL, NULL, 0);
643 wq = cq ? mlx4_glue->create_wq
645 &(struct ibv_wq_init_attr){
646 .wq_type = IBV_WQT_RQ,
652 ind = wq ? mlx4_glue->create_rwq_ind_table
654 &(struct ibv_rwq_ind_table_init_attr){
655 .log_ind_tbl_size = 0,
659 qp = ind ? mlx4_glue->create_qp_ex
661 &(struct ibv_qp_init_attr_ex){
663 (IBV_QP_INIT_ATTR_PD |
664 IBV_QP_INIT_ATTR_RX_HASH |
665 IBV_QP_INIT_ATTR_IND_TABLE),
666 .qp_type = IBV_QPT_RAW_PACKET,
670 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
671 .rx_hash_key_len = MLX4_RSS_HASH_KEY_SIZE,
672 .rx_hash_key = mlx4_rss_hash_key_default,
673 .rx_hash_fields_mask = hw_rss_sup,
677 WARN("disabling unusable inner RSS capability due to kernel"
679 hw_rss_sup &= ~IBV_RX_HASH_INNER;
681 claim_zero(mlx4_glue->destroy_qp(qp));
684 claim_zero(mlx4_glue->destroy_rwq_ind_table(ind));
686 claim_zero(mlx4_glue->destroy_wq(wq));
688 claim_zero(mlx4_glue->destroy_cq(cq));
692 static struct rte_pci_driver mlx4_driver;
695 * PMD global initialization.
697 * Independent from individual device, this function initializes global
698 * per-PMD data structures distinguishing primary and secondary processes.
699 * Hence, each initialization is called once per a process.
702 * 0 on success, a negative errno value otherwise and rte_errno is set.
707 struct mlx4_shared_data *sd;
708 struct mlx4_local_data *ld = &mlx4_local_data;
711 if (mlx4_init_shared_data())
713 sd = mlx4_shared_data;
715 rte_spinlock_lock(&sd->lock);
716 switch (rte_eal_process_type()) {
717 case RTE_PROC_PRIMARY:
720 LIST_INIT(&sd->mem_event_cb_list);
721 rte_rwlock_init(&sd->mem_event_rwlock);
722 rte_mem_event_callback_register("MLX4_MEM_EVENT_CB",
723 mlx4_mr_mem_event_cb, NULL);
724 ret = mlx4_mp_init_primary();
729 case RTE_PROC_SECONDARY:
732 ret = mlx4_mp_init_secondary();
742 rte_spinlock_unlock(&sd->lock);
747 * DPDK callback to register a PCI device.
749 * This function creates an Ethernet device for each port of a given
753 * PCI driver structure (mlx4_driver).
755 * PCI device information.
758 * 0 on success, negative errno value otherwise and rte_errno is set.
761 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
763 struct ibv_device **list;
764 struct ibv_device *ibv_dev;
766 struct ibv_context *attr_ctx = NULL;
767 struct ibv_device_attr device_attr;
768 struct ibv_device_attr_ex device_attr_ex;
769 struct mlx4_conf conf = {
771 .mr_ext_memseg_en = 1,
775 char ifname[IF_NAMESIZE];
778 err = mlx4_init_once();
780 ERROR("unable to init PMD global data: %s",
781 strerror(rte_errno));
784 MLX4_ASSERT(pci_drv == &mlx4_driver);
785 list = mlx4_glue->get_device_list(&i);
788 MLX4_ASSERT(rte_errno);
789 if (rte_errno == ENOSYS)
790 ERROR("cannot list devices, is ib_uverbs loaded?");
795 * For each listed device, check related sysfs entry against
796 * the provided PCI ID.
799 struct rte_pci_addr pci_addr;
802 DEBUG("checking device \"%s\"", list[i]->name);
803 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
805 if ((pci_dev->addr.domain != pci_addr.domain) ||
806 (pci_dev->addr.bus != pci_addr.bus) ||
807 (pci_dev->addr.devid != pci_addr.devid) ||
808 (pci_dev->addr.function != pci_addr.function))
810 vf = (pci_dev->id.device_id ==
811 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
812 INFO("PCI information matches, using device \"%s\" (VF: %s)",
813 list[i]->name, (vf ? "true" : "false"));
814 attr_ctx = mlx4_glue->open_device(list[i]);
818 if (attr_ctx == NULL) {
819 mlx4_glue->free_device_list(list);
823 ERROR("cannot access device, is mlx4_ib loaded?");
827 ERROR("cannot use device, are drivers up to date?");
830 MLX4_ASSERT(err > 0);
835 DEBUG("device opened");
836 if (mlx4_glue->query_device(attr_ctx, &device_attr)) {
840 INFO("%u port(s) detected", device_attr.phys_port_cnt);
841 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
842 if (mlx4_args(pci_dev->device.devargs, &conf)) {
843 ERROR("failed to process device arguments");
847 /* Use all ports when none are defined */
848 if (!conf.ports.enabled)
849 conf.ports.enabled = conf.ports.present;
850 /* Retrieve extended device attributes. */
851 if (mlx4_glue->query_device_ex(attr_ctx, NULL, &device_attr_ex)) {
855 MLX4_ASSERT(device_attr.max_sge >= MLX4_MAX_SGE);
856 for (i = 0; i < device_attr.phys_port_cnt; i++) {
857 uint32_t port = i + 1; /* ports are indexed from one */
858 struct ibv_context *ctx = NULL;
859 struct ibv_port_attr port_attr;
860 struct ibv_pd *pd = NULL;
861 struct mlx4_priv *priv = NULL;
862 struct rte_eth_dev *eth_dev = NULL;
863 struct rte_ether_addr mac;
864 char name[RTE_ETH_NAME_MAX_LEN];
866 /* If port is not enabled, skip. */
867 if (!(conf.ports.enabled & (1 << i)))
869 DEBUG("using port %u", port);
870 ctx = mlx4_glue->open_device(ibv_dev);
875 snprintf(name, sizeof(name), "%s port %u",
876 mlx4_glue->get_device_name(ibv_dev), port);
877 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
878 eth_dev = rte_eth_dev_attach_secondary(name);
879 if (eth_dev == NULL) {
880 ERROR("can not attach rte ethdev");
885 priv = eth_dev->data->dev_private;
886 if (!priv->verbs_alloc_ctx.enabled) {
887 ERROR("secondary process is not supported"
888 " due to lack of external allocator"
894 eth_dev->device = &pci_dev->device;
895 eth_dev->dev_ops = &mlx4_dev_sec_ops;
896 err = mlx4_proc_priv_init(eth_dev);
899 /* Receive command fd from primary process. */
900 err = mlx4_mp_req_verbs_cmd_fd(eth_dev);
905 /* Remap UAR for Tx queues. */
906 err = mlx4_tx_uar_init_secondary(eth_dev, err);
912 * Ethdev pointer is still required as input since
913 * the primary device is not accessible from the
916 eth_dev->tx_pkt_burst = mlx4_tx_burst;
917 eth_dev->rx_pkt_burst = mlx4_rx_burst;
918 claim_zero(mlx4_glue->close_device(ctx));
919 rte_eth_copy_pci_info(eth_dev, pci_dev);
920 rte_eth_dev_probing_finish(eth_dev);
923 /* Check port status. */
924 err = mlx4_glue->query_port(ctx, port, &port_attr);
927 ERROR("port query failed: %s", strerror(err));
930 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
932 ERROR("port %d is not configured in Ethernet mode",
936 if (port_attr.state != IBV_PORT_ACTIVE)
937 DEBUG("port %d is not active: \"%s\" (%d)",
938 port, mlx4_glue->port_state_str(port_attr.state),
940 /* Make asynchronous FD non-blocking to handle interrupts. */
941 err = mlx4_fd_set_non_blocking(ctx->async_fd);
943 ERROR("cannot make asynchronous FD non-blocking: %s",
947 /* Allocate protection domain. */
948 pd = mlx4_glue->alloc_pd(ctx);
951 ERROR("PD allocation failure");
954 /* from rte_ethdev.c */
955 priv = rte_zmalloc("ethdev private structure",
957 RTE_CACHE_LINE_SIZE);
960 ERROR("priv allocation failure");
964 priv->device_attr = device_attr;
967 priv->mtu = RTE_ETHER_MTU;
969 priv->hw_csum = !!(device_attr.device_cap_flags &
970 IBV_DEVICE_RAW_IP_CSUM);
971 DEBUG("checksum offloading is %ssupported",
972 (priv->hw_csum ? "" : "not "));
973 /* Only ConnectX-3 Pro supports tunneling. */
974 priv->hw_csum_l2tun =
976 (device_attr.vendor_part_id ==
977 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO);
978 DEBUG("L2 tunnel checksum offloads are %ssupported",
979 priv->hw_csum_l2tun ? "" : "not ");
980 priv->hw_rss_sup = mlx4_hw_rss_sup(priv->ctx, priv->pd,
982 DEBUG("supported RSS hash fields mask: %016" PRIx64,
984 priv->hw_rss_max_qps =
985 device_attr_ex.rss_caps.max_rwq_indirection_table_size;
986 DEBUG("MAX RSS queues %d", priv->hw_rss_max_qps);
987 priv->hw_fcs_strip = !!(device_attr_ex.raw_packet_caps &
988 IBV_RAW_PACKET_CAP_SCATTER_FCS);
989 DEBUG("FCS stripping toggling is %ssupported",
990 priv->hw_fcs_strip ? "" : "not ");
992 ((device_attr_ex.tso_caps.max_tso > 0) &&
993 (device_attr_ex.tso_caps.supported_qpts &
994 (1 << IBV_QPT_RAW_PACKET)));
996 priv->tso_max_payload_sz =
997 device_attr_ex.tso_caps.max_tso;
998 DEBUG("TSO is %ssupported",
999 priv->tso ? "" : "not ");
1000 priv->mr_ext_memseg_en = conf.mr_ext_memseg_en;
1001 /* Configure the first MAC address by default. */
1002 err = mlx4_get_mac(priv, &mac.addr_bytes);
1004 ERROR("cannot get MAC address, is mlx4_en loaded?"
1005 " (error: %s)", strerror(err));
1008 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1010 mac.addr_bytes[0], mac.addr_bytes[1],
1011 mac.addr_bytes[2], mac.addr_bytes[3],
1012 mac.addr_bytes[4], mac.addr_bytes[5]);
1013 /* Register MAC address. */
1016 if (mlx4_get_ifname(priv, &ifname) == 0) {
1017 DEBUG("port %u ifname is \"%s\"",
1018 priv->port, ifname);
1019 priv->if_index = if_nametoindex(ifname);
1021 DEBUG("port %u ifname is unknown", priv->port);
1024 /* Get actual MTU if possible. */
1025 mlx4_mtu_get(priv, &priv->mtu);
1026 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
1027 eth_dev = rte_eth_dev_allocate(name);
1028 if (eth_dev == NULL) {
1030 ERROR("can not allocate rte ethdev");
1033 eth_dev->data->dev_private = priv;
1034 eth_dev->data->mac_addrs = priv->mac;
1035 eth_dev->device = &pci_dev->device;
1036 rte_eth_copy_pci_info(eth_dev, pci_dev);
1037 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1038 /* Initialize local interrupt handle for current port. */
1039 memset(&priv->intr_handle, 0, sizeof(struct rte_intr_handle));
1040 priv->intr_handle.fd = -1;
1041 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1043 * Override ethdev interrupt handle pointer with private
1044 * handle instead of that of the parent PCI device used by
1045 * default. This prevents it from being shared between all
1046 * ports of the same PCI device since each of them is
1047 * associated its own Verbs context.
1049 * Rx interrupts in particular require this as the PMD has
1050 * no control over the registration of queue interrupts
1051 * besides setting up eth_dev->intr_handle, the rest is
1052 * handled by rte_intr_rx_ctl().
1054 eth_dev->intr_handle = &priv->intr_handle;
1055 priv->dev_data = eth_dev->data;
1056 eth_dev->dev_ops = &mlx4_dev_ops;
1057 #ifdef HAVE_IBV_MLX4_BUF_ALLOCATORS
1058 /* Hint libmlx4 to use PMD allocator for data plane resources */
1059 err = mlx4_glue->dv_set_context_attr
1060 (ctx, MLX4DV_SET_CTX_ATTR_BUF_ALLOCATORS,
1061 (void *)((uintptr_t)&(struct mlx4dv_ctx_allocators){
1062 .alloc = &mlx4_alloc_verbs_buf,
1063 .free = &mlx4_free_verbs_buf,
1067 WARN("Verbs external allocator is not supported");
1069 priv->verbs_alloc_ctx.enabled = 1;
1071 /* Bring Ethernet device up. */
1072 DEBUG("forcing Ethernet interface up");
1073 mlx4_dev_set_link_up(eth_dev);
1074 /* Update link status once if waiting for LSC. */
1075 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1076 mlx4_link_update(eth_dev, 0);
1078 * Once the device is added to the list of memory event
1079 * callback, its global MR cache table cannot be expanded
1080 * on the fly because of deadlock. If it overflows, lookup
1081 * should be done by searching MR list linearly, which is slow.
1083 err = mlx4_mr_btree_init(&priv->mr.cache,
1084 MLX4_MR_BTREE_CACHE_N * 2,
1085 eth_dev->device->numa_node);
1087 /* rte_errno is already set. */
1090 /* Add device to memory callback list. */
1091 rte_rwlock_write_lock(&mlx4_shared_data->mem_event_rwlock);
1092 LIST_INSERT_HEAD(&mlx4_shared_data->mem_event_cb_list,
1093 priv, mem_event_cb);
1094 rte_rwlock_write_unlock(&mlx4_shared_data->mem_event_rwlock);
1095 rte_eth_dev_probing_finish(eth_dev);
1099 if (eth_dev != NULL)
1100 eth_dev->data->dev_private = NULL;
1102 claim_zero(mlx4_glue->dealloc_pd(pd));
1104 claim_zero(mlx4_glue->close_device(ctx));
1105 if (eth_dev != NULL) {
1106 /* mac_addrs must not be freed because part of dev_private */
1107 eth_dev->data->mac_addrs = NULL;
1108 rte_eth_dev_release_port(eth_dev);
1113 * XXX if something went wrong in the loop above, there is a resource
1114 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
1115 * long as the dpdk does not provide a way to deallocate a ethdev and a
1116 * way to enumerate the registered ethdevs to free the previous ones.
1120 claim_zero(mlx4_glue->close_device(attr_ctx));
1122 mlx4_glue->free_device_list(list);
1129 * DPDK callback to remove a PCI device.
1131 * This function removes all Ethernet devices belong to a given PCI device.
1133 * @param[in] pci_dev
1134 * Pointer to the PCI device.
1137 * 0 on success, the function cannot fail.
1140 mlx4_pci_remove(struct rte_pci_device *pci_dev)
1145 RTE_ETH_FOREACH_DEV_OF(port_id, &pci_dev->device) {
1147 * mlx4_dev_close() is not registered to secondary process,
1148 * call the close function explicitly for secondary process.
1150 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1151 ret |= mlx4_dev_close(&rte_eth_devices[port_id]);
1153 ret |= rte_eth_dev_close(port_id);
1155 return ret == 0 ? 0 : -EIO;
1158 static const struct rte_pci_id mlx4_pci_id_map[] = {
1160 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1161 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
1164 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1165 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
1168 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1169 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
1176 static struct rte_pci_driver mlx4_driver = {
1178 .name = MLX4_DRIVER_NAME
1180 .id_table = mlx4_pci_id_map,
1181 .probe = mlx4_pci_probe,
1182 .remove = mlx4_pci_remove,
1183 .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
1186 #ifdef RTE_IBVERBS_LINK_DLOPEN
1189 * Suffix RTE_EAL_PMD_PATH with "-glue".
1191 * This function performs a sanity check on RTE_EAL_PMD_PATH before
1192 * suffixing its last component.
1195 * Output buffer, should be large enough otherwise NULL is returned.
1200 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
1203 mlx4_glue_path(char *buf, size_t size)
1205 static const char *const bad[] = { "/", ".", "..", NULL };
1206 const char *path = RTE_EAL_PMD_PATH;
1207 size_t len = strlen(path);
1211 while (len && path[len - 1] == '/')
1213 for (off = len; off && path[off - 1] != '/'; --off)
1215 for (i = 0; bad[i]; ++i)
1216 if (!strncmp(path + off, bad[i], (int)(len - off)))
1218 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1219 if (i == -1 || (size_t)i >= size)
1223 ERROR("unable to append \"-glue\" to last component of"
1224 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1225 " please re-configure DPDK");
1230 * Initialization routine for run-time dependency on rdma-core.
1233 mlx4_glue_init(void)
1235 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1236 const char *path[] = {
1238 * A basic security check is necessary before trusting
1239 * MLX4_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1241 (geteuid() == getuid() && getegid() == getgid() ?
1242 getenv("MLX4_GLUE_PATH") : NULL),
1244 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1245 * variant, otherwise let dlopen() look up libraries on its
1248 (*RTE_EAL_PMD_PATH ?
1249 mlx4_glue_path(glue_path, sizeof(glue_path)) : ""),
1252 void *handle = NULL;
1256 while (!handle && i != RTE_DIM(path)) {
1265 end = strpbrk(path[i], ":;");
1267 end = path[i] + strlen(path[i]);
1268 len = end - path[i];
1273 ret = snprintf(name, sizeof(name), "%.*s%s" MLX4_GLUE,
1275 (!len || *(end - 1) == '/') ? "" : "/");
1278 if (sizeof(name) != (size_t)ret + 1)
1280 DEBUG("looking for rdma-core glue as \"%s\"", name);
1281 handle = dlopen(name, RTLD_LAZY);
1292 WARN("cannot load glue library: %s", dlmsg);
1295 sym = dlsym(handle, "mlx4_glue");
1296 if (!sym || !*sym) {
1300 ERROR("cannot resolve glue symbol: %s", dlmsg);
1308 WARN("cannot initialize PMD due to missing run-time"
1309 " dependency on rdma-core libraries (libibverbs,"
1316 /* Initialize driver log type. */
1317 RTE_LOG_REGISTER(mlx4_logtype, pmd.net.mlx4, NOTICE)
1320 * Driver initialization routine.
1322 RTE_INIT(rte_mlx4_pmd_init)
1325 * MLX4_DEVICE_FATAL_CLEANUP tells ibv_destroy functions we
1326 * want to get success errno value in case of calling them
1327 * when the device was removed.
1329 setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
1331 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1332 * huge pages. Calling ibv_fork_init() during init allows
1333 * applications to use fork() safely for purposes other than
1334 * using this PMD, which is not supported in forked processes.
1336 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1337 #ifdef RTE_IBVERBS_LINK_DLOPEN
1338 if (mlx4_glue_init())
1340 MLX4_ASSERT(mlx4_glue);
1342 #ifdef RTE_LIBRTE_MLX4_DEBUG
1343 /* Glue structure must not contain any NULL pointers. */
1347 for (i = 0; i != sizeof(*mlx4_glue) / sizeof(void *); ++i)
1348 MLX4_ASSERT(((const void *const *)mlx4_glue)[i]);
1351 if (strcmp(mlx4_glue->version, MLX4_GLUE_VERSION)) {
1352 ERROR("rdma-core glue \"%s\" mismatch: \"%s\" is required",
1353 mlx4_glue->version, MLX4_GLUE_VERSION);
1356 mlx4_glue->fork_init();
1357 rte_pci_register(&mlx4_driver);
1360 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
1361 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
1362 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
1363 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");