1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
16 #include <linux/netlink.h>
17 #include <linux/rtnetlink.h>
20 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
22 #pragma GCC diagnostic ignored "-Wpedantic"
24 #include <infiniband/verbs.h>
26 #pragma GCC diagnostic error "-Wpedantic"
29 #include <rte_malloc.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_ethdev_pci.h>
33 #include <rte_bus_pci.h>
34 #include <rte_common.h>
35 #include <rte_config.h>
36 #include <rte_eal_memconfig.h>
37 #include <rte_kvargs.h>
38 #include <rte_rwlock.h>
39 #include <rte_spinlock.h>
40 #include <rte_string_fns.h>
43 #include "mlx5_utils.h"
44 #include "mlx5_rxtx.h"
45 #include "mlx5_autoconf.h"
46 #include "mlx5_defs.h"
47 #include "mlx5_glue.h"
50 /* Device parameter to enable RX completion queue compression. */
51 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
53 /* Device parameter to enable Multi-Packet Rx queue. */
54 #define MLX5_RX_MPRQ_EN "mprq_en"
56 /* Device parameter to configure log 2 of the number of strides for MPRQ. */
57 #define MLX5_RX_MPRQ_LOG_STRIDE_NUM "mprq_log_stride_num"
59 /* Device parameter to limit the size of memcpy'd packet for MPRQ. */
60 #define MLX5_RX_MPRQ_MAX_MEMCPY_LEN "mprq_max_memcpy_len"
62 /* Device parameter to set the minimum number of Rx queues to enable MPRQ. */
63 #define MLX5_RXQS_MIN_MPRQ "rxqs_min_mprq"
65 /* Device parameter to configure inline send. */
66 #define MLX5_TXQ_INLINE "txq_inline"
69 * Device parameter to configure the number of TX queues threshold for
70 * enabling inline send.
72 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
74 /* Device parameter to enable multi-packet send WQEs. */
75 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
77 /* Device parameter to include 2 dsegs in the title WQEBB. */
78 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
80 /* Device parameter to limit the size of inlining packet. */
81 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
83 /* Device parameter to enable hardware Tx vector. */
84 #define MLX5_TX_VEC_EN "tx_vec_en"
86 /* Device parameter to enable hardware Rx vector. */
87 #define MLX5_RX_VEC_EN "rx_vec_en"
89 /* Allow L3 VXLAN flow creation. */
90 #define MLX5_L3_VXLAN_EN "l3_vxlan_en"
92 /* Activate Netlink support in VF mode. */
93 #define MLX5_VF_NL_EN "vf_nl_en"
95 /* Select port representors to instantiate. */
96 #define MLX5_REPRESENTOR "representor"
98 #ifndef HAVE_IBV_MLX5_MOD_MPW
99 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
100 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
103 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
104 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
107 static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
109 /* Shared memory between primary and secondary processes. */
110 struct mlx5_shared_data *mlx5_shared_data;
112 /* Spinlock for mlx5_shared_data allocation. */
113 static rte_spinlock_t mlx5_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
115 /** Driver-specific log messages type. */
119 * Prepare shared data between primary and secondary process.
122 mlx5_prepare_shared_data(void)
124 const struct rte_memzone *mz;
126 rte_spinlock_lock(&mlx5_shared_data_lock);
127 if (mlx5_shared_data == NULL) {
128 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
129 /* Allocate shared memory. */
130 mz = rte_memzone_reserve(MZ_MLX5_PMD_SHARED_DATA,
131 sizeof(*mlx5_shared_data),
134 /* Lookup allocated shared memory. */
135 mz = rte_memzone_lookup(MZ_MLX5_PMD_SHARED_DATA);
138 rte_panic("Cannot allocate mlx5 shared data\n");
139 mlx5_shared_data = mz->addr;
140 /* Initialize shared data. */
141 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
142 LIST_INIT(&mlx5_shared_data->mem_event_cb_list);
143 rte_rwlock_init(&mlx5_shared_data->mem_event_rwlock);
145 rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
146 mlx5_mr_mem_event_cb, NULL);
148 rte_spinlock_unlock(&mlx5_shared_data_lock);
152 * Retrieve integer value from environment variable.
155 * Environment variable name.
158 * Integer value, 0 if the variable is not set.
161 mlx5_getenv_int(const char *name)
163 const char *val = getenv(name);
171 * Verbs callback to allocate a memory. This function should allocate the space
172 * according to the size provided residing inside a huge page.
173 * Please note that all allocation must respect the alignment from libmlx5
174 * (i.e. currently sysconf(_SC_PAGESIZE)).
177 * The size in bytes of the memory to allocate.
179 * A pointer to the callback data.
182 * Allocated buffer, NULL otherwise and rte_errno is set.
185 mlx5_alloc_verbs_buf(size_t size, void *data)
187 struct priv *priv = data;
189 size_t alignment = sysconf(_SC_PAGESIZE);
190 unsigned int socket = SOCKET_ID_ANY;
192 if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
193 const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
195 socket = ctrl->socket;
196 } else if (priv->verbs_alloc_ctx.type ==
197 MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
198 const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
200 socket = ctrl->socket;
202 assert(data != NULL);
203 ret = rte_malloc_socket(__func__, size, alignment, socket);
210 * Verbs callback to free a memory.
213 * A pointer to the memory to free.
215 * A pointer to the callback data.
218 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
220 assert(data != NULL);
225 * DPDK callback to close the device.
227 * Destroy all queues and objects, free memory.
230 * Pointer to Ethernet device structure.
233 mlx5_dev_close(struct rte_eth_dev *dev)
235 struct priv *priv = dev->data->dev_private;
239 DRV_LOG(DEBUG, "port %u closing device \"%s\"",
241 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
242 /* In case mlx5_dev_stop() has not been called. */
243 mlx5_dev_interrupt_handler_uninstall(dev);
244 mlx5_traffic_disable(dev);
245 /* Prevent crashes when queues are still in use. */
246 dev->rx_pkt_burst = removed_rx_burst;
247 dev->tx_pkt_burst = removed_tx_burst;
248 if (priv->rxqs != NULL) {
249 /* XXX race condition if mlx5_rx_burst() is still running. */
251 for (i = 0; (i != priv->rxqs_n); ++i)
252 mlx5_rxq_release(dev, i);
256 if (priv->txqs != NULL) {
257 /* XXX race condition if mlx5_tx_burst() is still running. */
259 for (i = 0; (i != priv->txqs_n); ++i)
260 mlx5_txq_release(dev, i);
264 mlx5_mprq_free_mp(dev);
265 mlx5_mr_release(dev);
266 if (priv->pd != NULL) {
267 assert(priv->ctx != NULL);
268 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
269 claim_zero(mlx5_glue->close_device(priv->ctx));
271 assert(priv->ctx == NULL);
272 if (priv->rss_conf.rss_key != NULL)
273 rte_free(priv->rss_conf.rss_key);
274 if (priv->reta_idx != NULL)
275 rte_free(priv->reta_idx);
276 if (priv->primary_socket)
277 mlx5_socket_uninit(dev);
279 mlx5_nl_mac_addr_flush(dev);
280 if (priv->nl_socket_route >= 0)
281 close(priv->nl_socket_route);
282 if (priv->nl_socket_rdma >= 0)
283 close(priv->nl_socket_rdma);
284 ret = mlx5_hrxq_ibv_verify(dev);
286 DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
288 ret = mlx5_ind_table_ibv_verify(dev);
290 DRV_LOG(WARNING, "port %u some indirection table still remain",
292 ret = mlx5_rxq_ibv_verify(dev);
294 DRV_LOG(WARNING, "port %u some Verbs Rx queue still remain",
296 ret = mlx5_rxq_verify(dev);
298 DRV_LOG(WARNING, "port %u some Rx queues still remain",
300 ret = mlx5_txq_ibv_verify(dev);
302 DRV_LOG(WARNING, "port %u some Verbs Tx queue still remain",
304 ret = mlx5_txq_verify(dev);
306 DRV_LOG(WARNING, "port %u some Tx queues still remain",
308 ret = mlx5_flow_verify(dev);
310 DRV_LOG(WARNING, "port %u some flows still remain",
312 if (priv->domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
314 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
317 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
320 rte_eth_devices[port_id[i]].data->dev_private;
323 opriv->domain_id != priv->domain_id ||
324 &rte_eth_devices[port_id[i]] == dev)
329 claim_zero(rte_eth_switch_domain_free(priv->domain_id));
331 memset(priv, 0, sizeof(*priv));
332 priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
335 const struct eth_dev_ops mlx5_dev_ops = {
336 .dev_configure = mlx5_dev_configure,
337 .dev_start = mlx5_dev_start,
338 .dev_stop = mlx5_dev_stop,
339 .dev_set_link_down = mlx5_set_link_down,
340 .dev_set_link_up = mlx5_set_link_up,
341 .dev_close = mlx5_dev_close,
342 .promiscuous_enable = mlx5_promiscuous_enable,
343 .promiscuous_disable = mlx5_promiscuous_disable,
344 .allmulticast_enable = mlx5_allmulticast_enable,
345 .allmulticast_disable = mlx5_allmulticast_disable,
346 .link_update = mlx5_link_update,
347 .stats_get = mlx5_stats_get,
348 .stats_reset = mlx5_stats_reset,
349 .xstats_get = mlx5_xstats_get,
350 .xstats_reset = mlx5_xstats_reset,
351 .xstats_get_names = mlx5_xstats_get_names,
352 .dev_infos_get = mlx5_dev_infos_get,
353 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
354 .vlan_filter_set = mlx5_vlan_filter_set,
355 .rx_queue_setup = mlx5_rx_queue_setup,
356 .tx_queue_setup = mlx5_tx_queue_setup,
357 .rx_queue_release = mlx5_rx_queue_release,
358 .tx_queue_release = mlx5_tx_queue_release,
359 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
360 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
361 .mac_addr_remove = mlx5_mac_addr_remove,
362 .mac_addr_add = mlx5_mac_addr_add,
363 .mac_addr_set = mlx5_mac_addr_set,
364 .set_mc_addr_list = mlx5_set_mc_addr_list,
365 .mtu_set = mlx5_dev_set_mtu,
366 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
367 .vlan_offload_set = mlx5_vlan_offload_set,
368 .reta_update = mlx5_dev_rss_reta_update,
369 .reta_query = mlx5_dev_rss_reta_query,
370 .rss_hash_update = mlx5_rss_hash_update,
371 .rss_hash_conf_get = mlx5_rss_hash_conf_get,
372 .filter_ctrl = mlx5_dev_filter_ctrl,
373 .rx_descriptor_status = mlx5_rx_descriptor_status,
374 .tx_descriptor_status = mlx5_tx_descriptor_status,
375 .rx_queue_intr_enable = mlx5_rx_intr_enable,
376 .rx_queue_intr_disable = mlx5_rx_intr_disable,
377 .is_removed = mlx5_is_removed,
380 static const struct eth_dev_ops mlx5_dev_sec_ops = {
381 .stats_get = mlx5_stats_get,
382 .stats_reset = mlx5_stats_reset,
383 .xstats_get = mlx5_xstats_get,
384 .xstats_reset = mlx5_xstats_reset,
385 .xstats_get_names = mlx5_xstats_get_names,
386 .dev_infos_get = mlx5_dev_infos_get,
387 .rx_descriptor_status = mlx5_rx_descriptor_status,
388 .tx_descriptor_status = mlx5_tx_descriptor_status,
391 /* Available operators in flow isolated mode. */
392 const struct eth_dev_ops mlx5_dev_ops_isolate = {
393 .dev_configure = mlx5_dev_configure,
394 .dev_start = mlx5_dev_start,
395 .dev_stop = mlx5_dev_stop,
396 .dev_set_link_down = mlx5_set_link_down,
397 .dev_set_link_up = mlx5_set_link_up,
398 .dev_close = mlx5_dev_close,
399 .link_update = mlx5_link_update,
400 .stats_get = mlx5_stats_get,
401 .stats_reset = mlx5_stats_reset,
402 .xstats_get = mlx5_xstats_get,
403 .xstats_reset = mlx5_xstats_reset,
404 .xstats_get_names = mlx5_xstats_get_names,
405 .dev_infos_get = mlx5_dev_infos_get,
406 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
407 .vlan_filter_set = mlx5_vlan_filter_set,
408 .rx_queue_setup = mlx5_rx_queue_setup,
409 .tx_queue_setup = mlx5_tx_queue_setup,
410 .rx_queue_release = mlx5_rx_queue_release,
411 .tx_queue_release = mlx5_tx_queue_release,
412 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
413 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
414 .mac_addr_remove = mlx5_mac_addr_remove,
415 .mac_addr_add = mlx5_mac_addr_add,
416 .mac_addr_set = mlx5_mac_addr_set,
417 .set_mc_addr_list = mlx5_set_mc_addr_list,
418 .mtu_set = mlx5_dev_set_mtu,
419 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
420 .vlan_offload_set = mlx5_vlan_offload_set,
421 .filter_ctrl = mlx5_dev_filter_ctrl,
422 .rx_descriptor_status = mlx5_rx_descriptor_status,
423 .tx_descriptor_status = mlx5_tx_descriptor_status,
424 .rx_queue_intr_enable = mlx5_rx_intr_enable,
425 .rx_queue_intr_disable = mlx5_rx_intr_disable,
426 .is_removed = mlx5_is_removed,
430 * Verify and store value for device argument.
433 * Key argument to verify.
435 * Value associated with key.
440 * 0 on success, a negative errno value otherwise and rte_errno is set.
443 mlx5_args_check(const char *key, const char *val, void *opaque)
445 struct mlx5_dev_config *config = opaque;
448 /* No-op, port representors are processed in mlx5_dev_spawn(). */
449 if (!strcmp(MLX5_REPRESENTOR, key))
452 tmp = strtoul(val, NULL, 0);
455 DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
458 if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
459 config->cqe_comp = !!tmp;
460 } else if (strcmp(MLX5_RX_MPRQ_EN, key) == 0) {
461 config->mprq.enabled = !!tmp;
462 } else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_NUM, key) == 0) {
463 config->mprq.stride_num_n = tmp;
464 } else if (strcmp(MLX5_RX_MPRQ_MAX_MEMCPY_LEN, key) == 0) {
465 config->mprq.max_memcpy_len = tmp;
466 } else if (strcmp(MLX5_RXQS_MIN_MPRQ, key) == 0) {
467 config->mprq.min_rxqs_num = tmp;
468 } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
469 config->txq_inline = tmp;
470 } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
471 config->txqs_inline = tmp;
472 } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
473 config->mps = !!tmp ? config->mps : 0;
474 } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
475 config->mpw_hdr_dseg = !!tmp;
476 } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
477 config->inline_max_packet_sz = tmp;
478 } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
479 config->tx_vec_en = !!tmp;
480 } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
481 config->rx_vec_en = !!tmp;
482 } else if (strcmp(MLX5_L3_VXLAN_EN, key) == 0) {
483 config->l3_vxlan_en = !!tmp;
484 } else if (strcmp(MLX5_VF_NL_EN, key) == 0) {
485 config->vf_nl_en = !!tmp;
487 DRV_LOG(WARNING, "%s: unknown parameter", key);
495 * Parse device parameters.
498 * Pointer to device configuration structure.
500 * Device arguments structure.
503 * 0 on success, a negative errno value otherwise and rte_errno is set.
506 mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
508 const char **params = (const char *[]){
509 MLX5_RXQ_CQE_COMP_EN,
511 MLX5_RX_MPRQ_LOG_STRIDE_NUM,
512 MLX5_RX_MPRQ_MAX_MEMCPY_LEN,
515 MLX5_TXQS_MIN_INLINE,
517 MLX5_TXQ_MPW_HDR_DSEG_EN,
518 MLX5_TXQ_MAX_INLINE_LEN,
526 struct rte_kvargs *kvlist;
532 /* Following UGLY cast is done to pass checkpatch. */
533 kvlist = rte_kvargs_parse(devargs->args, params);
536 /* Process parameters. */
537 for (i = 0; (params[i] != NULL); ++i) {
538 if (rte_kvargs_count(kvlist, params[i])) {
539 ret = rte_kvargs_process(kvlist, params[i],
540 mlx5_args_check, config);
543 rte_kvargs_free(kvlist);
548 rte_kvargs_free(kvlist);
552 static struct rte_pci_driver mlx5_driver;
555 * Reserved UAR address space for TXQ UAR(hw doorbell) mapping, process
556 * local resource used by both primary and secondary to avoid duplicate
558 * The space has to be available on both primary and secondary process,
559 * TXQ UAR maps to this area using fixed mmap w/o double check.
561 static void *uar_base;
564 find_lower_va_bound(const struct rte_memseg_list *msl __rte_unused,
565 const struct rte_memseg *ms, void *arg)
572 *addr = RTE_MIN(*addr, ms->addr);
578 * Reserve UAR address space for primary process.
581 * Pointer to Ethernet device.
584 * 0 on success, a negative errno value otherwise and rte_errno is set.
587 mlx5_uar_init_primary(struct rte_eth_dev *dev)
589 struct priv *priv = dev->data->dev_private;
590 void *addr = (void *)0;
592 if (uar_base) { /* UAR address space mapped. */
593 priv->uar_base = uar_base;
596 /* find out lower bound of hugepage segments */
597 rte_memseg_walk(find_lower_va_bound, &addr);
599 /* keep distance to hugepages to minimize potential conflicts. */
600 addr = RTE_PTR_SUB(addr, MLX5_UAR_OFFSET + MLX5_UAR_SIZE);
601 /* anonymous mmap, no real memory consumption. */
602 addr = mmap(addr, MLX5_UAR_SIZE,
603 PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
604 if (addr == MAP_FAILED) {
606 "port %u failed to reserve UAR address space, please"
607 " adjust MLX5_UAR_SIZE or try --base-virtaddr",
612 /* Accept either same addr or a new addr returned from mmap if target
615 DRV_LOG(INFO, "port %u reserved UAR address space: %p",
616 dev->data->port_id, addr);
617 priv->uar_base = addr; /* for primary and secondary UAR re-mmap. */
618 uar_base = addr; /* process local, don't reserve again. */
623 * Reserve UAR address space for secondary process, align with
627 * Pointer to Ethernet device.
630 * 0 on success, a negative errno value otherwise and rte_errno is set.
633 mlx5_uar_init_secondary(struct rte_eth_dev *dev)
635 struct priv *priv = dev->data->dev_private;
638 assert(priv->uar_base);
639 if (uar_base) { /* already reserved. */
640 assert(uar_base == priv->uar_base);
643 /* anonymous mmap, no real memory consumption. */
644 addr = mmap(priv->uar_base, MLX5_UAR_SIZE,
645 PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
646 if (addr == MAP_FAILED) {
647 DRV_LOG(ERR, "port %u UAR mmap failed: %p size: %llu",
648 dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
652 if (priv->uar_base != addr) {
654 "port %u UAR address %p size %llu occupied, please"
655 " adjust MLX5_UAR_OFFSET or try EAL parameter"
657 dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
661 uar_base = addr; /* process local, don't reserve again */
662 DRV_LOG(INFO, "port %u reserved UAR address space: %p",
663 dev->data->port_id, addr);
668 * Spawn an Ethernet device from Verbs information.
671 * Backing DPDK device.
675 * If nonzero, enable VF-specific features.
676 * @param[in] switch_info
677 * Switch properties of Ethernet device.
680 * A valid Ethernet device object on success, NULL otherwise and rte_errno
681 * is set. The following error is defined:
683 * EBUSY: device is not supposed to be spawned.
685 static struct rte_eth_dev *
686 mlx5_dev_spawn(struct rte_device *dpdk_dev,
687 struct ibv_device *ibv_dev,
689 const struct mlx5_switch_info *switch_info)
691 struct ibv_context *ctx;
692 struct ibv_device_attr_ex attr;
693 struct ibv_port_attr port_attr;
694 struct ibv_pd *pd = NULL;
695 struct mlx5dv_context dv_attr = { .comp_mask = 0 };
696 struct mlx5_dev_config config = {
701 .txq_inline = MLX5_ARG_UNSET,
702 .txqs_inline = MLX5_ARG_UNSET,
703 .inline_max_packet_sz = MLX5_ARG_UNSET,
707 .stride_num_n = MLX5_MPRQ_STRIDE_NUM_N,
708 .max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN,
709 .min_rxqs_num = MLX5_MPRQ_MIN_RXQS,
712 struct rte_eth_dev *eth_dev = NULL;
713 struct priv *priv = NULL;
716 unsigned int cqe_comp;
717 unsigned int tunnel_en = 0;
718 unsigned int mpls_en = 0;
719 unsigned int swp = 0;
720 unsigned int verb_priorities = 0;
721 unsigned int mprq = 0;
722 unsigned int mprq_min_stride_size_n = 0;
723 unsigned int mprq_max_stride_size_n = 0;
724 unsigned int mprq_min_stride_num_n = 0;
725 unsigned int mprq_max_stride_num_n = 0;
726 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
727 struct ibv_counter_set_description cs_desc = { .counter_type = 0 };
729 struct ether_addr mac;
730 char name[RTE_ETH_NAME_MAX_LEN];
731 int own_domain_id = 0;
734 /* Determine if this port representor is supposed to be spawned. */
735 if (switch_info->representor && dpdk_dev->devargs) {
736 struct rte_eth_devargs eth_da;
738 err = rte_eth_devargs_parse(dpdk_dev->devargs->args, ð_da);
741 DRV_LOG(ERR, "failed to process device arguments: %s",
742 strerror(rte_errno));
745 for (i = 0; i < eth_da.nb_representor_ports; ++i)
746 if (eth_da.representor_ports[i] ==
747 (uint16_t)switch_info->port_name)
749 if (i == eth_da.nb_representor_ports) {
754 /* Prepare shared data between primary and secondary process. */
755 mlx5_prepare_shared_data();
757 ctx = mlx5_glue->open_device(ibv_dev);
759 rte_errno = errno ? errno : ENODEV;
762 #ifdef HAVE_IBV_MLX5_MOD_SWP
763 dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
766 * Multi-packet send is supported by ConnectX-4 Lx PF as well
767 * as all ConnectX-5 devices.
769 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
770 dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
772 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
773 dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
775 mlx5_glue->dv_query_device(ctx, &dv_attr);
776 if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
777 if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
778 DRV_LOG(DEBUG, "enhanced MPW is supported");
779 mps = MLX5_MPW_ENHANCED;
781 DRV_LOG(DEBUG, "MPW is supported");
785 DRV_LOG(DEBUG, "MPW isn't supported");
786 mps = MLX5_MPW_DISABLED;
789 #ifdef HAVE_IBV_MLX5_MOD_SWP
790 if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
791 swp = dv_attr.sw_parsing_caps.sw_parsing_offloads;
792 DRV_LOG(DEBUG, "SWP support: %u", swp);
795 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
796 if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
797 struct mlx5dv_striding_rq_caps mprq_caps =
798 dv_attr.striding_rq_caps;
800 DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %d",
801 mprq_caps.min_single_stride_log_num_of_bytes);
802 DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %d",
803 mprq_caps.max_single_stride_log_num_of_bytes);
804 DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %d",
805 mprq_caps.min_single_wqe_log_num_of_strides);
806 DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %d",
807 mprq_caps.max_single_wqe_log_num_of_strides);
808 DRV_LOG(DEBUG, "\tsupported_qpts: %d",
809 mprq_caps.supported_qpts);
810 DRV_LOG(DEBUG, "device supports Multi-Packet RQ");
812 mprq_min_stride_size_n =
813 mprq_caps.min_single_stride_log_num_of_bytes;
814 mprq_max_stride_size_n =
815 mprq_caps.max_single_stride_log_num_of_bytes;
816 mprq_min_stride_num_n =
817 mprq_caps.min_single_wqe_log_num_of_strides;
818 mprq_max_stride_num_n =
819 mprq_caps.max_single_wqe_log_num_of_strides;
820 config.mprq.stride_num_n = RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
821 mprq_min_stride_num_n);
824 if (RTE_CACHE_LINE_SIZE == 128 &&
825 !(dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
829 config.cqe_comp = cqe_comp;
830 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
831 if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
832 tunnel_en = ((dv_attr.tunnel_offloads_caps &
833 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
834 (dv_attr.tunnel_offloads_caps &
835 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE));
837 DRV_LOG(DEBUG, "tunnel offloading is %ssupported",
838 tunnel_en ? "" : "not ");
841 "tunnel offloading disabled due to old OFED/rdma-core version");
843 config.tunnel_en = tunnel_en;
844 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
845 mpls_en = ((dv_attr.tunnel_offloads_caps &
846 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
847 (dv_attr.tunnel_offloads_caps &
848 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
849 DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported",
850 mpls_en ? "" : "not ");
852 DRV_LOG(WARNING, "MPLS over GRE/UDP tunnel offloading disabled due to"
853 " old OFED/rdma-core version or firmware configuration");
855 config.mpls_en = mpls_en;
856 err = mlx5_glue->query_device_ex(ctx, NULL, &attr);
858 DEBUG("ibv_query_device_ex() failed");
861 if (!switch_info->representor)
862 rte_strlcpy(name, dpdk_dev->name, sizeof(name));
864 snprintf(name, sizeof(name), "%s_representor_%u",
865 dpdk_dev->name, switch_info->port_name);
866 DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
867 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
868 eth_dev = rte_eth_dev_attach_secondary(name);
869 if (eth_dev == NULL) {
870 DRV_LOG(ERR, "can not attach rte ethdev");
875 eth_dev->device = dpdk_dev;
876 eth_dev->dev_ops = &mlx5_dev_sec_ops;
877 err = mlx5_uar_init_secondary(eth_dev);
882 /* Receive command fd from primary process */
883 err = mlx5_socket_connect(eth_dev);
888 /* Remap UAR for Tx queues. */
889 err = mlx5_tx_uar_remap(eth_dev, err);
895 * Ethdev pointer is still required as input since
896 * the primary device is not accessible from the
899 eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
900 eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
901 claim_zero(mlx5_glue->close_device(ctx));
904 /* Check port status. */
905 err = mlx5_glue->query_port(ctx, 1, &port_attr);
907 DRV_LOG(ERR, "port query failed: %s", strerror(err));
910 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
911 DRV_LOG(ERR, "port is not configured in Ethernet mode");
915 if (port_attr.state != IBV_PORT_ACTIVE)
916 DRV_LOG(DEBUG, "port is not active: \"%s\" (%d)",
917 mlx5_glue->port_state_str(port_attr.state),
919 /* Allocate protection domain. */
920 pd = mlx5_glue->alloc_pd(ctx);
922 DRV_LOG(ERR, "PD allocation failure");
926 priv = rte_zmalloc("ethdev private structure",
928 RTE_CACHE_LINE_SIZE);
930 DRV_LOG(ERR, "priv allocation failure");
935 strncpy(priv->ibdev_name, priv->ctx->device->name,
936 sizeof(priv->ibdev_name));
937 strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
938 sizeof(priv->ibdev_path));
939 priv->device_attr = attr;
941 priv->mtu = ETHER_MTU;
942 /* Some internal functions rely on Netlink sockets, open them now. */
943 priv->nl_socket_rdma = mlx5_nl_init(0, NETLINK_RDMA);
944 priv->nl_socket_route = mlx5_nl_init(RTMGRP_LINK, NETLINK_ROUTE);
946 priv->representor = !!switch_info->representor;
947 priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
948 priv->representor_id =
949 switch_info->representor ? switch_info->port_name : -1;
951 * Look for sibling devices in order to reuse their switch domain
952 * if any, otherwise allocate one.
954 i = mlx5_dev_to_port_id(dpdk_dev, NULL, 0);
958 i = RTE_MIN(mlx5_dev_to_port_id(dpdk_dev, port_id, i), i);
960 const struct priv *opriv =
961 rte_eth_devices[port_id[i]].data->dev_private;
965 RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
967 priv->domain_id = opriv->domain_id;
971 if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
972 err = rte_eth_switch_domain_alloc(&priv->domain_id);
975 DRV_LOG(ERR, "unable to allocate switch domain: %s",
976 strerror(rte_errno));
981 err = mlx5_args(&config, dpdk_dev->devargs);
984 DRV_LOG(ERR, "failed to process device arguments: %s",
985 strerror(rte_errno));
988 config.hw_csum = !!(attr.device_cap_flags_ex & IBV_DEVICE_RAW_IP_CSUM);
989 DRV_LOG(DEBUG, "checksum offloading is %ssupported",
990 (config.hw_csum ? "" : "not "));
991 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
992 config.flow_counter_en = !!attr.max_counter_sets;
993 mlx5_glue->describe_counter_set(ctx, 0, &cs_desc);
994 DRV_LOG(DEBUG, "counter type = %d, num of cs = %ld, attributes = %d",
995 cs_desc.counter_type, cs_desc.num_of_cs,
998 config.ind_table_max_size =
999 attr.rss_caps.max_rwq_indirection_table_size;
1001 * Remove this check once DPDK supports larger/variable
1002 * indirection tables.
1004 if (config.ind_table_max_size > (unsigned int)ETH_RSS_RETA_SIZE_512)
1005 config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
1006 DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
1007 config.ind_table_max_size);
1008 config.hw_vlan_strip = !!(attr.raw_packet_caps &
1009 IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
1010 DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
1011 (config.hw_vlan_strip ? "" : "not "));
1012 config.hw_fcs_strip = !!(attr.raw_packet_caps &
1013 IBV_RAW_PACKET_CAP_SCATTER_FCS);
1014 DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
1015 (config.hw_fcs_strip ? "" : "not "));
1016 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
1017 config.hw_padding = !!attr.rx_pad_end_addr_align;
1019 DRV_LOG(DEBUG, "hardware Rx end alignment padding is %ssupported",
1020 (config.hw_padding ? "" : "not "));
1021 config.tso = (attr.tso_caps.max_tso > 0 &&
1022 (attr.tso_caps.supported_qpts &
1023 (1 << IBV_QPT_RAW_PACKET)));
1025 config.tso_max_payload_sz = attr.tso_caps.max_tso;
1026 if (config.mps && !mps) {
1028 "multi-packet send not supported on this device"
1029 " (" MLX5_TXQ_MPW_EN ")");
1033 DRV_LOG(INFO, "%sMPS is %s",
1034 config.mps == MLX5_MPW_ENHANCED ? "enhanced " : "",
1035 config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
1036 if (config.cqe_comp && !cqe_comp) {
1037 DRV_LOG(WARNING, "Rx CQE compression isn't supported");
1038 config.cqe_comp = 0;
1040 if (config.mprq.enabled && mprq) {
1041 if (config.mprq.stride_num_n > mprq_max_stride_num_n ||
1042 config.mprq.stride_num_n < mprq_min_stride_num_n) {
1043 config.mprq.stride_num_n =
1044 RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
1045 mprq_min_stride_num_n);
1047 "the number of strides"
1048 " for Multi-Packet RQ is out of range,"
1049 " setting default value (%u)",
1050 1 << config.mprq.stride_num_n);
1052 config.mprq.min_stride_size_n = mprq_min_stride_size_n;
1053 config.mprq.max_stride_size_n = mprq_max_stride_size_n;
1054 } else if (config.mprq.enabled && !mprq) {
1055 DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
1056 config.mprq.enabled = 0;
1058 eth_dev = rte_eth_dev_allocate(name);
1059 if (eth_dev == NULL) {
1060 DRV_LOG(ERR, "can not allocate rte ethdev");
1064 if (priv->representor)
1065 eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
1066 eth_dev->data->dev_private = priv;
1067 priv->dev_data = eth_dev->data;
1068 eth_dev->data->mac_addrs = priv->mac;
1069 eth_dev->device = dpdk_dev;
1070 eth_dev->device->driver = &mlx5_driver.driver;
1071 err = mlx5_uar_init_primary(eth_dev);
1076 /* Configure the first MAC address by default. */
1077 if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1079 "port %u cannot get MAC address, is mlx5_en"
1080 " loaded? (errno: %s)",
1081 eth_dev->data->port_id, strerror(rte_errno));
1086 "port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1087 eth_dev->data->port_id,
1088 mac.addr_bytes[0], mac.addr_bytes[1],
1089 mac.addr_bytes[2], mac.addr_bytes[3],
1090 mac.addr_bytes[4], mac.addr_bytes[5]);
1093 char ifname[IF_NAMESIZE];
1095 if (mlx5_get_ifname(eth_dev, &ifname) == 0)
1096 DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1097 eth_dev->data->port_id, ifname);
1099 DRV_LOG(DEBUG, "port %u ifname is unknown",
1100 eth_dev->data->port_id);
1103 /* Get actual MTU if possible. */
1104 err = mlx5_get_mtu(eth_dev, &priv->mtu);
1109 DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1111 /* Initialize burst functions to prevent crashes before link-up. */
1112 eth_dev->rx_pkt_burst = removed_rx_burst;
1113 eth_dev->tx_pkt_burst = removed_tx_burst;
1114 eth_dev->dev_ops = &mlx5_dev_ops;
1115 /* Register MAC address. */
1116 claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1117 if (vf && config.vf_nl_en)
1118 mlx5_nl_mac_addr_sync(eth_dev);
1119 TAILQ_INIT(&priv->flows);
1120 TAILQ_INIT(&priv->ctrl_flows);
1121 /* Hint libmlx5 to use PMD allocator for data plane resources */
1122 struct mlx5dv_ctx_allocators alctr = {
1123 .alloc = &mlx5_alloc_verbs_buf,
1124 .free = &mlx5_free_verbs_buf,
1127 mlx5_glue->dv_set_context_attr(ctx, MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
1128 (void *)((uintptr_t)&alctr));
1129 /* Bring Ethernet device up. */
1130 DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1131 eth_dev->data->port_id);
1132 mlx5_set_link_up(eth_dev);
1134 * Even though the interrupt handler is not installed yet,
1135 * interrupts will still trigger on the asyn_fd from
1136 * Verbs context returned by ibv_open_device().
1138 mlx5_link_update(eth_dev, 0);
1139 /* Store device configuration on private structure. */
1140 priv->config = config;
1141 /* Supported Verbs flow priority number detection. */
1142 if (verb_priorities == 0) {
1143 err = mlx5_verbs_max_prio(eth_dev);
1145 DRV_LOG(ERR, "port %u wrong Verbs flow priorities",
1146 eth_dev->data->port_id);
1149 verb_priorities = err;
1151 priv->config.max_verbs_prio = verb_priorities;
1153 * Once the device is added to the list of memory event
1154 * callback, its global MR cache table cannot be expanded
1155 * on the fly because of deadlock. If it overflows, lookup
1156 * should be done by searching MR list linearly, which is slow.
1158 err = mlx5_mr_btree_init(&priv->mr.cache,
1159 MLX5_MR_BTREE_CACHE_N * 2,
1160 eth_dev->device->numa_node);
1165 /* Add device to memory callback list. */
1166 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
1167 LIST_INSERT_HEAD(&mlx5_shared_data->mem_event_cb_list,
1168 priv, mem_event_cb);
1169 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
1173 if (priv->nl_socket_route >= 0)
1174 close(priv->nl_socket_route);
1175 if (priv->nl_socket_rdma >= 0)
1176 close(priv->nl_socket_rdma);
1178 claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1182 claim_zero(mlx5_glue->dealloc_pd(pd));
1184 rte_eth_dev_release_port(eth_dev);
1186 claim_zero(mlx5_glue->close_device(ctx));
1192 /** Data associated with devices to spawn. */
1193 struct mlx5_dev_spawn_data {
1194 unsigned int ifindex; /**< Network interface index. */
1195 struct mlx5_switch_info info; /**< Switch information. */
1196 struct ibv_device *ibv_dev; /**< Associated IB device. */
1197 struct rte_eth_dev *eth_dev; /**< Associated Ethernet device. */
1201 * Comparison callback to sort device data.
1203 * This is meant to be used with qsort().
1206 * Pointer to pointer to first data object.
1208 * Pointer to pointer to second data object.
1211 * 0 if both objects are equal, less than 0 if the first argument is less
1212 * than the second, greater than 0 otherwise.
1215 mlx5_dev_spawn_data_cmp(const void *a, const void *b)
1217 const struct mlx5_switch_info *si_a =
1218 &((const struct mlx5_dev_spawn_data *)a)->info;
1219 const struct mlx5_switch_info *si_b =
1220 &((const struct mlx5_dev_spawn_data *)b)->info;
1223 /* Master device first. */
1224 ret = si_b->master - si_a->master;
1227 /* Then representor devices. */
1228 ret = si_b->representor - si_a->representor;
1231 /* Unidentified devices come last in no specific order. */
1232 if (!si_a->representor)
1234 /* Order representors by name. */
1235 return si_a->port_name - si_b->port_name;
1239 * DPDK callback to register a PCI device.
1241 * This function spawns Ethernet devices out of a given PCI device.
1243 * @param[in] pci_drv
1244 * PCI driver structure (mlx5_driver).
1245 * @param[in] pci_dev
1246 * PCI device information.
1249 * 0 on success, a negative errno value otherwise and rte_errno is set.
1252 mlx5_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1253 struct rte_pci_device *pci_dev)
1255 struct ibv_device **ibv_list;
1260 assert(pci_drv == &mlx5_driver);
1262 ibv_list = mlx5_glue->get_device_list(&ret);
1264 rte_errno = errno ? errno : ENOSYS;
1265 DRV_LOG(ERR, "cannot list devices, is ib_uverbs loaded?");
1269 struct ibv_device *ibv_match[ret + 1];
1272 struct rte_pci_addr pci_addr;
1274 DRV_LOG(DEBUG, "checking device \"%s\"", ibv_list[ret]->name);
1275 if (mlx5_ibv_device_to_pci_addr(ibv_list[ret], &pci_addr))
1277 if (pci_dev->addr.domain != pci_addr.domain ||
1278 pci_dev->addr.bus != pci_addr.bus ||
1279 pci_dev->addr.devid != pci_addr.devid ||
1280 pci_dev->addr.function != pci_addr.function)
1282 DRV_LOG(INFO, "PCI information matches for device \"%s\"",
1283 ibv_list[ret]->name);
1284 ibv_match[n++] = ibv_list[ret];
1286 ibv_match[n] = NULL;
1288 struct mlx5_dev_spawn_data list[n];
1289 int nl_route = n ? mlx5_nl_init(0, NETLINK_ROUTE) : -1;
1290 int nl_rdma = n ? mlx5_nl_init(0, NETLINK_RDMA) : -1;
1295 * The existence of several matching entries (n > 1) means port
1296 * representors have been instantiated. No existing Verbs call nor
1297 * /sys entries can tell them apart, this can only be done through
1298 * Netlink calls assuming kernel drivers are recent enough to
1301 * In the event of identification failure through Netlink, either:
1303 * 1. No device matches (n == 0), complain and bail out.
1304 * 2. A single IB device matches (n == 1) and is not a representor,
1305 * assume no switch support.
1306 * 3. Otherwise no safe assumptions can be made; complain louder and
1309 for (i = 0; i != n; ++i) {
1310 list[i].ibv_dev = ibv_match[i];
1311 list[i].eth_dev = NULL;
1313 list[i].ifindex = 0;
1315 list[i].ifindex = mlx5_nl_ifindex
1316 (nl_rdma, list[i].ibv_dev->name);
1319 mlx5_nl_switch_info(nl_route, list[i].ifindex,
1321 list[i].ifindex = 0;
1322 memset(&list[i].info, 0, sizeof(list[i].info));
1330 /* Count unidentified devices. */
1331 for (u = 0, i = 0; i != n; ++i)
1332 if (!list[i].info.master && !list[i].info.representor)
1335 if (n == 1 && u == 1) {
1337 DRV_LOG(INFO, "no switch support detected");
1341 "unable to tell which of the matching devices"
1342 " is the master (lack of kernel support?)");
1347 * Sort list to probe devices in natural order for users convenience
1348 * (i.e. master first, then representors from lowest to highest ID).
1351 qsort(list, n, sizeof(*list), mlx5_dev_spawn_data_cmp);
1352 switch (pci_dev->id.device_id) {
1353 case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1354 case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1355 case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1356 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1362 for (i = 0; i != n; ++i) {
1365 list[i].eth_dev = mlx5_dev_spawn
1366 (&pci_dev->device, list[i].ibv_dev, vf, &list[i].info);
1367 if (!list[i].eth_dev) {
1368 if (rte_errno != EBUSY)
1370 /* Device is disabled, ignore it. */
1373 restore = list[i].eth_dev->data->dev_flags;
1374 rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
1375 /* Restore non-PCI flags cleared by the above call. */
1376 list[i].eth_dev->data->dev_flags |= restore;
1377 rte_eth_dev_probing_finish(list[i].eth_dev);
1379 mlx5_glue->free_device_list(ibv_list);
1382 "no Verbs device matches PCI device " PCI_PRI_FMT ","
1383 " are kernel drivers loaded?",
1384 pci_dev->addr.domain, pci_dev->addr.bus,
1385 pci_dev->addr.devid, pci_dev->addr.function);
1388 } else if (i != n) {
1390 "probe of PCI device " PCI_PRI_FMT " aborted after"
1391 " encountering an error: %s",
1392 pci_dev->addr.domain, pci_dev->addr.bus,
1393 pci_dev->addr.devid, pci_dev->addr.function,
1394 strerror(rte_errno));
1398 if (!list[i].eth_dev)
1400 mlx5_dev_close(list[i].eth_dev);
1401 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1402 rte_free(list[i].eth_dev->data->dev_private);
1403 claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
1405 /* Restore original error. */
1413 static const struct rte_pci_id mlx5_pci_id_map[] = {
1415 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1416 PCI_DEVICE_ID_MELLANOX_CONNECTX4)
1419 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1420 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
1423 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1424 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
1427 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1428 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
1431 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1432 PCI_DEVICE_ID_MELLANOX_CONNECTX5)
1435 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1436 PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
1439 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1440 PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
1443 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1444 PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
1447 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1448 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
1455 static struct rte_pci_driver mlx5_driver = {
1457 .name = MLX5_DRIVER_NAME
1459 .id_table = mlx5_pci_id_map,
1460 .probe = mlx5_pci_probe,
1461 .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
1464 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1467 * Suffix RTE_EAL_PMD_PATH with "-glue".
1469 * This function performs a sanity check on RTE_EAL_PMD_PATH before
1470 * suffixing its last component.
1473 * Output buffer, should be large enough otherwise NULL is returned.
1478 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
1481 mlx5_glue_path(char *buf, size_t size)
1483 static const char *const bad[] = { "/", ".", "..", NULL };
1484 const char *path = RTE_EAL_PMD_PATH;
1485 size_t len = strlen(path);
1489 while (len && path[len - 1] == '/')
1491 for (off = len; off && path[off - 1] != '/'; --off)
1493 for (i = 0; bad[i]; ++i)
1494 if (!strncmp(path + off, bad[i], (int)(len - off)))
1496 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1497 if (i == -1 || (size_t)i >= size)
1502 "unable to append \"-glue\" to last component of"
1503 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1504 " please re-configure DPDK");
1509 * Initialization routine for run-time dependency on rdma-core.
1512 mlx5_glue_init(void)
1514 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1515 const char *path[] = {
1517 * A basic security check is necessary before trusting
1518 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1520 (geteuid() == getuid() && getegid() == getgid() ?
1521 getenv("MLX5_GLUE_PATH") : NULL),
1523 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1524 * variant, otherwise let dlopen() look up libraries on its
1527 (*RTE_EAL_PMD_PATH ?
1528 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
1531 void *handle = NULL;
1535 while (!handle && i != RTE_DIM(path)) {
1544 end = strpbrk(path[i], ":;");
1546 end = path[i] + strlen(path[i]);
1547 len = end - path[i];
1552 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
1554 (!len || *(end - 1) == '/') ? "" : "/");
1557 if (sizeof(name) != (size_t)ret + 1)
1559 DRV_LOG(DEBUG, "looking for rdma-core glue as \"%s\"",
1561 handle = dlopen(name, RTLD_LAZY);
1572 DRV_LOG(WARNING, "cannot load glue library: %s", dlmsg);
1575 sym = dlsym(handle, "mlx5_glue");
1576 if (!sym || !*sym) {
1580 DRV_LOG(ERR, "cannot resolve glue symbol: %s", dlmsg);
1589 "cannot initialize PMD due to missing run-time dependency on"
1590 " rdma-core libraries (libibverbs, libmlx5)");
1597 * Driver initialization routine.
1599 RTE_INIT(rte_mlx5_pmd_init)
1601 /* Initialize driver log type. */
1602 mlx5_logtype = rte_log_register("pmd.net.mlx5");
1603 if (mlx5_logtype >= 0)
1604 rte_log_set_level(mlx5_logtype, RTE_LOG_NOTICE);
1606 /* Build the static tables for Verbs conversion. */
1607 mlx5_set_ptype_table();
1608 mlx5_set_cksum_table();
1609 mlx5_set_swp_types_table();
1611 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1612 * huge pages. Calling ibv_fork_init() during init allows
1613 * applications to use fork() safely for purposes other than
1614 * using this PMD, which is not supported in forked processes.
1616 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1617 /* Match the size of Rx completion entry to the size of a cacheline. */
1618 if (RTE_CACHE_LINE_SIZE == 128)
1619 setenv("MLX5_CQE_SIZE", "128", 0);
1621 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
1622 * cleanup all the Verbs resources even when the device was removed.
1624 setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
1625 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1626 if (mlx5_glue_init())
1631 /* Glue structure must not contain any NULL pointers. */
1635 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
1636 assert(((const void *const *)mlx5_glue)[i]);
1639 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
1641 "rdma-core glue \"%s\" mismatch: \"%s\" is required",
1642 mlx5_glue->version, MLX5_GLUE_VERSION);
1645 mlx5_glue->fork_init();
1646 rte_pci_register(&mlx5_driver);
1649 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1650 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1651 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");