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"
49 #include "mlx5_flow.h"
51 /* Device parameter to enable RX completion queue compression. */
52 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
54 /* Device parameter to enable Multi-Packet Rx queue. */
55 #define MLX5_RX_MPRQ_EN "mprq_en"
57 /* Device parameter to configure log 2 of the number of strides for MPRQ. */
58 #define MLX5_RX_MPRQ_LOG_STRIDE_NUM "mprq_log_stride_num"
60 /* Device parameter to limit the size of memcpy'd packet for MPRQ. */
61 #define MLX5_RX_MPRQ_MAX_MEMCPY_LEN "mprq_max_memcpy_len"
63 /* Device parameter to set the minimum number of Rx queues to enable MPRQ. */
64 #define MLX5_RXQS_MIN_MPRQ "rxqs_min_mprq"
66 /* Device parameter to configure inline send. */
67 #define MLX5_TXQ_INLINE "txq_inline"
70 * Device parameter to configure the number of TX queues threshold for
71 * enabling inline send.
73 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
75 /* Device parameter to enable multi-packet send WQEs. */
76 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
78 /* Device parameter to include 2 dsegs in the title WQEBB. */
79 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
81 /* Device parameter to limit the size of inlining packet. */
82 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
84 /* Device parameter to enable hardware Tx vector. */
85 #define MLX5_TX_VEC_EN "tx_vec_en"
87 /* Device parameter to enable hardware Rx vector. */
88 #define MLX5_RX_VEC_EN "rx_vec_en"
90 /* Allow L3 VXLAN flow creation. */
91 #define MLX5_L3_VXLAN_EN "l3_vxlan_en"
93 /* Activate DV flow steering. */
94 #define MLX5_DV_FLOW_EN "dv_flow_en"
96 /* Activate Netlink support in VF mode. */
97 #define MLX5_VF_NL_EN "vf_nl_en"
99 /* Select port representors to instantiate. */
100 #define MLX5_REPRESENTOR "representor"
102 #ifndef HAVE_IBV_MLX5_MOD_MPW
103 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
104 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
107 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
108 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
111 static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
113 /* Shared memory between primary and secondary processes. */
114 struct mlx5_shared_data *mlx5_shared_data;
116 /* Spinlock for mlx5_shared_data allocation. */
117 static rte_spinlock_t mlx5_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
119 /** Driver-specific log messages type. */
123 * Prepare shared data between primary and secondary process.
126 mlx5_prepare_shared_data(void)
128 const struct rte_memzone *mz;
130 rte_spinlock_lock(&mlx5_shared_data_lock);
131 if (mlx5_shared_data == NULL) {
132 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
133 /* Allocate shared memory. */
134 mz = rte_memzone_reserve(MZ_MLX5_PMD_SHARED_DATA,
135 sizeof(*mlx5_shared_data),
138 /* Lookup allocated shared memory. */
139 mz = rte_memzone_lookup(MZ_MLX5_PMD_SHARED_DATA);
142 rte_panic("Cannot allocate mlx5 shared data\n");
143 mlx5_shared_data = mz->addr;
144 /* Initialize shared data. */
145 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
146 LIST_INIT(&mlx5_shared_data->mem_event_cb_list);
147 rte_rwlock_init(&mlx5_shared_data->mem_event_rwlock);
149 rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
150 mlx5_mr_mem_event_cb, NULL);
152 rte_spinlock_unlock(&mlx5_shared_data_lock);
156 * Retrieve integer value from environment variable.
159 * Environment variable name.
162 * Integer value, 0 if the variable is not set.
165 mlx5_getenv_int(const char *name)
167 const char *val = getenv(name);
175 * Verbs callback to allocate a memory. This function should allocate the space
176 * according to the size provided residing inside a huge page.
177 * Please note that all allocation must respect the alignment from libmlx5
178 * (i.e. currently sysconf(_SC_PAGESIZE)).
181 * The size in bytes of the memory to allocate.
183 * A pointer to the callback data.
186 * Allocated buffer, NULL otherwise and rte_errno is set.
189 mlx5_alloc_verbs_buf(size_t size, void *data)
191 struct priv *priv = data;
193 size_t alignment = sysconf(_SC_PAGESIZE);
194 unsigned int socket = SOCKET_ID_ANY;
196 if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
197 const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
199 socket = ctrl->socket;
200 } else if (priv->verbs_alloc_ctx.type ==
201 MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
202 const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
204 socket = ctrl->socket;
206 assert(data != NULL);
207 ret = rte_malloc_socket(__func__, size, alignment, socket);
214 * Verbs callback to free a memory.
217 * A pointer to the memory to free.
219 * A pointer to the callback data.
222 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
224 assert(data != NULL);
229 * DPDK callback to close the device.
231 * Destroy all queues and objects, free memory.
234 * Pointer to Ethernet device structure.
237 mlx5_dev_close(struct rte_eth_dev *dev)
239 struct priv *priv = dev->data->dev_private;
243 DRV_LOG(DEBUG, "port %u closing device \"%s\"",
245 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
246 /* In case mlx5_dev_stop() has not been called. */
247 mlx5_dev_interrupt_handler_uninstall(dev);
248 mlx5_traffic_disable(dev);
249 mlx5_flow_flush(dev, NULL);
250 /* Prevent crashes when queues are still in use. */
251 dev->rx_pkt_burst = removed_rx_burst;
252 dev->tx_pkt_burst = removed_tx_burst;
253 if (priv->rxqs != NULL) {
254 /* XXX race condition if mlx5_rx_burst() is still running. */
256 for (i = 0; (i != priv->rxqs_n); ++i)
257 mlx5_rxq_release(dev, i);
261 if (priv->txqs != NULL) {
262 /* XXX race condition if mlx5_tx_burst() is still running. */
264 for (i = 0; (i != priv->txqs_n); ++i)
265 mlx5_txq_release(dev, i);
269 mlx5_mprq_free_mp(dev);
270 mlx5_mr_release(dev);
271 if (priv->pd != NULL) {
272 assert(priv->ctx != NULL);
273 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
274 claim_zero(mlx5_glue->close_device(priv->ctx));
276 assert(priv->ctx == NULL);
277 if (priv->rss_conf.rss_key != NULL)
278 rte_free(priv->rss_conf.rss_key);
279 if (priv->reta_idx != NULL)
280 rte_free(priv->reta_idx);
281 if (priv->primary_socket)
282 mlx5_socket_uninit(dev);
284 mlx5_nl_mac_addr_flush(dev);
285 if (priv->nl_socket_route >= 0)
286 close(priv->nl_socket_route);
287 if (priv->nl_socket_rdma >= 0)
288 close(priv->nl_socket_rdma);
289 if (priv->mnl_socket)
290 mlx5_flow_tcf_socket_destroy(priv->mnl_socket);
291 ret = mlx5_hrxq_ibv_verify(dev);
293 DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
295 ret = mlx5_ind_table_ibv_verify(dev);
297 DRV_LOG(WARNING, "port %u some indirection table still remain",
299 ret = mlx5_rxq_ibv_verify(dev);
301 DRV_LOG(WARNING, "port %u some Verbs Rx queue still remain",
303 ret = mlx5_rxq_verify(dev);
305 DRV_LOG(WARNING, "port %u some Rx queues still remain",
307 ret = mlx5_txq_ibv_verify(dev);
309 DRV_LOG(WARNING, "port %u some Verbs Tx queue still remain",
311 ret = mlx5_txq_verify(dev);
313 DRV_LOG(WARNING, "port %u some Tx queues still remain",
315 ret = mlx5_flow_verify(dev);
317 DRV_LOG(WARNING, "port %u some flows still remain",
319 if (priv->domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
321 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
324 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
327 rte_eth_devices[port_id[i]].data->dev_private;
330 opriv->domain_id != priv->domain_id ||
331 &rte_eth_devices[port_id[i]] == dev)
336 claim_zero(rte_eth_switch_domain_free(priv->domain_id));
338 memset(priv, 0, sizeof(*priv));
339 priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
342 const struct eth_dev_ops mlx5_dev_ops = {
343 .dev_configure = mlx5_dev_configure,
344 .dev_start = mlx5_dev_start,
345 .dev_stop = mlx5_dev_stop,
346 .dev_set_link_down = mlx5_set_link_down,
347 .dev_set_link_up = mlx5_set_link_up,
348 .dev_close = mlx5_dev_close,
349 .promiscuous_enable = mlx5_promiscuous_enable,
350 .promiscuous_disable = mlx5_promiscuous_disable,
351 .allmulticast_enable = mlx5_allmulticast_enable,
352 .allmulticast_disable = mlx5_allmulticast_disable,
353 .link_update = mlx5_link_update,
354 .stats_get = mlx5_stats_get,
355 .stats_reset = mlx5_stats_reset,
356 .xstats_get = mlx5_xstats_get,
357 .xstats_reset = mlx5_xstats_reset,
358 .xstats_get_names = mlx5_xstats_get_names,
359 .dev_infos_get = mlx5_dev_infos_get,
360 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
361 .vlan_filter_set = mlx5_vlan_filter_set,
362 .rx_queue_setup = mlx5_rx_queue_setup,
363 .tx_queue_setup = mlx5_tx_queue_setup,
364 .rx_queue_release = mlx5_rx_queue_release,
365 .tx_queue_release = mlx5_tx_queue_release,
366 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
367 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
368 .mac_addr_remove = mlx5_mac_addr_remove,
369 .mac_addr_add = mlx5_mac_addr_add,
370 .mac_addr_set = mlx5_mac_addr_set,
371 .set_mc_addr_list = mlx5_set_mc_addr_list,
372 .mtu_set = mlx5_dev_set_mtu,
373 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
374 .vlan_offload_set = mlx5_vlan_offload_set,
375 .reta_update = mlx5_dev_rss_reta_update,
376 .reta_query = mlx5_dev_rss_reta_query,
377 .rss_hash_update = mlx5_rss_hash_update,
378 .rss_hash_conf_get = mlx5_rss_hash_conf_get,
379 .filter_ctrl = mlx5_dev_filter_ctrl,
380 .rx_descriptor_status = mlx5_rx_descriptor_status,
381 .tx_descriptor_status = mlx5_tx_descriptor_status,
382 .rx_queue_intr_enable = mlx5_rx_intr_enable,
383 .rx_queue_intr_disable = mlx5_rx_intr_disable,
384 .is_removed = mlx5_is_removed,
387 static const struct eth_dev_ops mlx5_dev_sec_ops = {
388 .stats_get = mlx5_stats_get,
389 .stats_reset = mlx5_stats_reset,
390 .xstats_get = mlx5_xstats_get,
391 .xstats_reset = mlx5_xstats_reset,
392 .xstats_get_names = mlx5_xstats_get_names,
393 .dev_infos_get = mlx5_dev_infos_get,
394 .rx_descriptor_status = mlx5_rx_descriptor_status,
395 .tx_descriptor_status = mlx5_tx_descriptor_status,
398 /* Available operators in flow isolated mode. */
399 const struct eth_dev_ops mlx5_dev_ops_isolate = {
400 .dev_configure = mlx5_dev_configure,
401 .dev_start = mlx5_dev_start,
402 .dev_stop = mlx5_dev_stop,
403 .dev_set_link_down = mlx5_set_link_down,
404 .dev_set_link_up = mlx5_set_link_up,
405 .dev_close = mlx5_dev_close,
406 .promiscuous_enable = mlx5_promiscuous_enable,
407 .promiscuous_disable = mlx5_promiscuous_disable,
408 .allmulticast_enable = mlx5_allmulticast_enable,
409 .allmulticast_disable = mlx5_allmulticast_disable,
410 .link_update = mlx5_link_update,
411 .stats_get = mlx5_stats_get,
412 .stats_reset = mlx5_stats_reset,
413 .xstats_get = mlx5_xstats_get,
414 .xstats_reset = mlx5_xstats_reset,
415 .xstats_get_names = mlx5_xstats_get_names,
416 .dev_infos_get = mlx5_dev_infos_get,
417 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
418 .vlan_filter_set = mlx5_vlan_filter_set,
419 .rx_queue_setup = mlx5_rx_queue_setup,
420 .tx_queue_setup = mlx5_tx_queue_setup,
421 .rx_queue_release = mlx5_rx_queue_release,
422 .tx_queue_release = mlx5_tx_queue_release,
423 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
424 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
425 .mac_addr_remove = mlx5_mac_addr_remove,
426 .mac_addr_add = mlx5_mac_addr_add,
427 .mac_addr_set = mlx5_mac_addr_set,
428 .set_mc_addr_list = mlx5_set_mc_addr_list,
429 .mtu_set = mlx5_dev_set_mtu,
430 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
431 .vlan_offload_set = mlx5_vlan_offload_set,
432 .filter_ctrl = mlx5_dev_filter_ctrl,
433 .rx_descriptor_status = mlx5_rx_descriptor_status,
434 .tx_descriptor_status = mlx5_tx_descriptor_status,
435 .rx_queue_intr_enable = mlx5_rx_intr_enable,
436 .rx_queue_intr_disable = mlx5_rx_intr_disable,
437 .is_removed = mlx5_is_removed,
441 * Verify and store value for device argument.
444 * Key argument to verify.
446 * Value associated with key.
451 * 0 on success, a negative errno value otherwise and rte_errno is set.
454 mlx5_args_check(const char *key, const char *val, void *opaque)
456 struct mlx5_dev_config *config = opaque;
459 /* No-op, port representors are processed in mlx5_dev_spawn(). */
460 if (!strcmp(MLX5_REPRESENTOR, key))
463 tmp = strtoul(val, NULL, 0);
466 DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
469 if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
470 config->cqe_comp = !!tmp;
471 } else if (strcmp(MLX5_RX_MPRQ_EN, key) == 0) {
472 config->mprq.enabled = !!tmp;
473 } else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_NUM, key) == 0) {
474 config->mprq.stride_num_n = tmp;
475 } else if (strcmp(MLX5_RX_MPRQ_MAX_MEMCPY_LEN, key) == 0) {
476 config->mprq.max_memcpy_len = tmp;
477 } else if (strcmp(MLX5_RXQS_MIN_MPRQ, key) == 0) {
478 config->mprq.min_rxqs_num = tmp;
479 } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
480 config->txq_inline = tmp;
481 } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
482 config->txqs_inline = tmp;
483 } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
485 } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
486 config->mpw_hdr_dseg = !!tmp;
487 } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
488 config->inline_max_packet_sz = tmp;
489 } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
490 config->tx_vec_en = !!tmp;
491 } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
492 config->rx_vec_en = !!tmp;
493 } else if (strcmp(MLX5_L3_VXLAN_EN, key) == 0) {
494 config->l3_vxlan_en = !!tmp;
495 } else if (strcmp(MLX5_VF_NL_EN, key) == 0) {
496 config->vf_nl_en = !!tmp;
497 } else if (strcmp(MLX5_DV_FLOW_EN, key) == 0) {
498 config->dv_flow_en = !!tmp;
500 DRV_LOG(WARNING, "%s: unknown parameter", key);
508 * Parse device parameters.
511 * Pointer to device configuration structure.
513 * Device arguments structure.
516 * 0 on success, a negative errno value otherwise and rte_errno is set.
519 mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
521 const char **params = (const char *[]){
522 MLX5_RXQ_CQE_COMP_EN,
524 MLX5_RX_MPRQ_LOG_STRIDE_NUM,
525 MLX5_RX_MPRQ_MAX_MEMCPY_LEN,
528 MLX5_TXQS_MIN_INLINE,
530 MLX5_TXQ_MPW_HDR_DSEG_EN,
531 MLX5_TXQ_MAX_INLINE_LEN,
540 struct rte_kvargs *kvlist;
546 /* Following UGLY cast is done to pass checkpatch. */
547 kvlist = rte_kvargs_parse(devargs->args, params);
550 /* Process parameters. */
551 for (i = 0; (params[i] != NULL); ++i) {
552 if (rte_kvargs_count(kvlist, params[i])) {
553 ret = rte_kvargs_process(kvlist, params[i],
554 mlx5_args_check, config);
557 rte_kvargs_free(kvlist);
562 rte_kvargs_free(kvlist);
566 static struct rte_pci_driver mlx5_driver;
569 * Reserved UAR address space for TXQ UAR(hw doorbell) mapping, process
570 * local resource used by both primary and secondary to avoid duplicate
572 * The space has to be available on both primary and secondary process,
573 * TXQ UAR maps to this area using fixed mmap w/o double check.
575 static void *uar_base;
578 find_lower_va_bound(const struct rte_memseg_list *msl,
579 const struct rte_memseg *ms, void *arg)
588 *addr = RTE_MIN(*addr, ms->addr);
594 * Reserve UAR address space for primary process.
597 * Pointer to Ethernet device.
600 * 0 on success, a negative errno value otherwise and rte_errno is set.
603 mlx5_uar_init_primary(struct rte_eth_dev *dev)
605 struct priv *priv = dev->data->dev_private;
606 void *addr = (void *)0;
608 if (uar_base) { /* UAR address space mapped. */
609 priv->uar_base = uar_base;
612 /* find out lower bound of hugepage segments */
613 rte_memseg_walk(find_lower_va_bound, &addr);
615 /* keep distance to hugepages to minimize potential conflicts. */
616 addr = RTE_PTR_SUB(addr, (uintptr_t)(MLX5_UAR_OFFSET + MLX5_UAR_SIZE));
617 /* anonymous mmap, no real memory consumption. */
618 addr = mmap(addr, MLX5_UAR_SIZE,
619 PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
620 if (addr == MAP_FAILED) {
622 "port %u failed to reserve UAR address space, please"
623 " adjust MLX5_UAR_SIZE or try --base-virtaddr",
628 /* Accept either same addr or a new addr returned from mmap if target
631 DRV_LOG(INFO, "port %u reserved UAR address space: %p",
632 dev->data->port_id, addr);
633 priv->uar_base = addr; /* for primary and secondary UAR re-mmap. */
634 uar_base = addr; /* process local, don't reserve again. */
639 * Reserve UAR address space for secondary process, align with
643 * Pointer to Ethernet device.
646 * 0 on success, a negative errno value otherwise and rte_errno is set.
649 mlx5_uar_init_secondary(struct rte_eth_dev *dev)
651 struct priv *priv = dev->data->dev_private;
654 assert(priv->uar_base);
655 if (uar_base) { /* already reserved. */
656 assert(uar_base == priv->uar_base);
659 /* anonymous mmap, no real memory consumption. */
660 addr = mmap(priv->uar_base, MLX5_UAR_SIZE,
661 PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
662 if (addr == MAP_FAILED) {
663 DRV_LOG(ERR, "port %u UAR mmap failed: %p size: %llu",
664 dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
668 if (priv->uar_base != addr) {
670 "port %u UAR address %p size %llu occupied, please"
671 " adjust MLX5_UAR_OFFSET or try EAL parameter"
673 dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
677 uar_base = addr; /* process local, don't reserve again */
678 DRV_LOG(INFO, "port %u reserved UAR address space: %p",
679 dev->data->port_id, addr);
684 * Spawn an Ethernet device from Verbs information.
687 * Backing DPDK device.
691 * If nonzero, enable VF-specific features.
692 * @param[in] switch_info
693 * Switch properties of Ethernet device.
696 * A valid Ethernet device object on success, NULL otherwise and rte_errno
697 * is set. The following error is defined:
699 * EBUSY: device is not supposed to be spawned.
701 static struct rte_eth_dev *
702 mlx5_dev_spawn(struct rte_device *dpdk_dev,
703 struct ibv_device *ibv_dev,
705 const struct mlx5_switch_info *switch_info)
707 struct ibv_context *ctx;
708 struct ibv_device_attr_ex attr;
709 struct ibv_port_attr port_attr;
710 struct ibv_pd *pd = NULL;
711 struct mlx5dv_context dv_attr = { .comp_mask = 0 };
712 struct mlx5_dev_config config = {
714 .mps = MLX5_ARG_UNSET,
718 .txq_inline = MLX5_ARG_UNSET,
719 .txqs_inline = MLX5_ARG_UNSET,
720 .inline_max_packet_sz = MLX5_ARG_UNSET,
724 .stride_num_n = MLX5_MPRQ_STRIDE_NUM_N,
725 .max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN,
726 .min_rxqs_num = MLX5_MPRQ_MIN_RXQS,
729 struct rte_eth_dev *eth_dev = NULL;
730 struct priv *priv = NULL;
733 unsigned int cqe_comp;
734 unsigned int tunnel_en = 0;
735 unsigned int mpls_en = 0;
736 unsigned int swp = 0;
737 unsigned int mprq = 0;
738 unsigned int mprq_min_stride_size_n = 0;
739 unsigned int mprq_max_stride_size_n = 0;
740 unsigned int mprq_min_stride_num_n = 0;
741 unsigned int mprq_max_stride_num_n = 0;
742 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
743 struct ibv_counter_set_description cs_desc = { .counter_type = 0 };
745 struct ether_addr mac;
746 char name[RTE_ETH_NAME_MAX_LEN];
747 int own_domain_id = 0;
750 /* Determine if this port representor is supposed to be spawned. */
751 if (switch_info->representor && dpdk_dev->devargs) {
752 struct rte_eth_devargs eth_da;
754 err = rte_eth_devargs_parse(dpdk_dev->devargs->args, ð_da);
757 DRV_LOG(ERR, "failed to process device arguments: %s",
758 strerror(rte_errno));
761 for (i = 0; i < eth_da.nb_representor_ports; ++i)
762 if (eth_da.representor_ports[i] ==
763 (uint16_t)switch_info->port_name)
765 if (i == eth_da.nb_representor_ports) {
770 /* Prepare shared data between primary and secondary process. */
771 mlx5_prepare_shared_data();
773 ctx = mlx5_glue->open_device(ibv_dev);
775 rte_errno = errno ? errno : ENODEV;
778 #ifdef HAVE_IBV_MLX5_MOD_SWP
779 dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
782 * Multi-packet send is supported by ConnectX-4 Lx PF as well
783 * as all ConnectX-5 devices.
785 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
786 dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
788 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
789 dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
791 mlx5_glue->dv_query_device(ctx, &dv_attr);
792 if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
793 if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
794 DRV_LOG(DEBUG, "enhanced MPW is supported");
795 mps = MLX5_MPW_ENHANCED;
797 DRV_LOG(DEBUG, "MPW is supported");
801 DRV_LOG(DEBUG, "MPW isn't supported");
802 mps = MLX5_MPW_DISABLED;
804 #ifdef HAVE_IBV_MLX5_MOD_SWP
805 if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
806 swp = dv_attr.sw_parsing_caps.sw_parsing_offloads;
807 DRV_LOG(DEBUG, "SWP support: %u", swp);
810 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
811 if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
812 struct mlx5dv_striding_rq_caps mprq_caps =
813 dv_attr.striding_rq_caps;
815 DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %d",
816 mprq_caps.min_single_stride_log_num_of_bytes);
817 DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %d",
818 mprq_caps.max_single_stride_log_num_of_bytes);
819 DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %d",
820 mprq_caps.min_single_wqe_log_num_of_strides);
821 DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %d",
822 mprq_caps.max_single_wqe_log_num_of_strides);
823 DRV_LOG(DEBUG, "\tsupported_qpts: %d",
824 mprq_caps.supported_qpts);
825 DRV_LOG(DEBUG, "device supports Multi-Packet RQ");
827 mprq_min_stride_size_n =
828 mprq_caps.min_single_stride_log_num_of_bytes;
829 mprq_max_stride_size_n =
830 mprq_caps.max_single_stride_log_num_of_bytes;
831 mprq_min_stride_num_n =
832 mprq_caps.min_single_wqe_log_num_of_strides;
833 mprq_max_stride_num_n =
834 mprq_caps.max_single_wqe_log_num_of_strides;
835 config.mprq.stride_num_n = RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
836 mprq_min_stride_num_n);
839 if (RTE_CACHE_LINE_SIZE == 128 &&
840 !(dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
844 config.cqe_comp = cqe_comp;
845 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
846 if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
847 tunnel_en = ((dv_attr.tunnel_offloads_caps &
848 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
849 (dv_attr.tunnel_offloads_caps &
850 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE));
852 DRV_LOG(DEBUG, "tunnel offloading is %ssupported",
853 tunnel_en ? "" : "not ");
856 "tunnel offloading disabled due to old OFED/rdma-core version");
858 config.tunnel_en = tunnel_en;
859 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
860 mpls_en = ((dv_attr.tunnel_offloads_caps &
861 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
862 (dv_attr.tunnel_offloads_caps &
863 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
864 DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported",
865 mpls_en ? "" : "not ");
867 DRV_LOG(WARNING, "MPLS over GRE/UDP tunnel offloading disabled due to"
868 " old OFED/rdma-core version or firmware configuration");
870 config.mpls_en = mpls_en;
871 err = mlx5_glue->query_device_ex(ctx, NULL, &attr);
873 DEBUG("ibv_query_device_ex() failed");
876 if (!switch_info->representor)
877 rte_strlcpy(name, dpdk_dev->name, sizeof(name));
879 snprintf(name, sizeof(name), "%s_representor_%u",
880 dpdk_dev->name, switch_info->port_name);
881 DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
882 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
883 eth_dev = rte_eth_dev_attach_secondary(name);
884 if (eth_dev == NULL) {
885 DRV_LOG(ERR, "can not attach rte ethdev");
890 eth_dev->device = dpdk_dev;
891 eth_dev->dev_ops = &mlx5_dev_sec_ops;
892 err = mlx5_uar_init_secondary(eth_dev);
897 /* Receive command fd from primary process */
898 err = mlx5_socket_connect(eth_dev);
903 /* Remap UAR for Tx queues. */
904 err = mlx5_tx_uar_remap(eth_dev, err);
910 * Ethdev pointer is still required as input since
911 * the primary device is not accessible from the
914 eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
915 eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
916 claim_zero(mlx5_glue->close_device(ctx));
919 /* Check port status. */
920 err = mlx5_glue->query_port(ctx, 1, &port_attr);
922 DRV_LOG(ERR, "port query failed: %s", strerror(err));
925 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
926 DRV_LOG(ERR, "port is not configured in Ethernet mode");
930 if (port_attr.state != IBV_PORT_ACTIVE)
931 DRV_LOG(DEBUG, "port is not active: \"%s\" (%d)",
932 mlx5_glue->port_state_str(port_attr.state),
934 /* Allocate protection domain. */
935 pd = mlx5_glue->alloc_pd(ctx);
937 DRV_LOG(ERR, "PD allocation failure");
941 priv = rte_zmalloc("ethdev private structure",
943 RTE_CACHE_LINE_SIZE);
945 DRV_LOG(ERR, "priv allocation failure");
950 strncpy(priv->ibdev_name, priv->ctx->device->name,
951 sizeof(priv->ibdev_name));
952 strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
953 sizeof(priv->ibdev_path));
954 priv->device_attr = attr;
956 priv->mtu = ETHER_MTU;
958 /* Initialize UAR access locks for 32bit implementations. */
959 rte_spinlock_init(&priv->uar_lock_cq);
960 for (i = 0; i < MLX5_UAR_PAGE_NUM_MAX; i++)
961 rte_spinlock_init(&priv->uar_lock[i]);
963 /* Some internal functions rely on Netlink sockets, open them now. */
964 priv->nl_socket_rdma = mlx5_nl_init(NETLINK_RDMA);
965 priv->nl_socket_route = mlx5_nl_init(NETLINK_ROUTE);
967 priv->representor = !!switch_info->representor;
968 priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
969 priv->representor_id =
970 switch_info->representor ? switch_info->port_name : -1;
972 * Look for sibling devices in order to reuse their switch domain
973 * if any, otherwise allocate one.
975 i = mlx5_dev_to_port_id(dpdk_dev, NULL, 0);
979 i = RTE_MIN(mlx5_dev_to_port_id(dpdk_dev, port_id, i), i);
981 const struct priv *opriv =
982 rte_eth_devices[port_id[i]].data->dev_private;
986 RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
988 priv->domain_id = opriv->domain_id;
992 if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
993 err = rte_eth_switch_domain_alloc(&priv->domain_id);
996 DRV_LOG(ERR, "unable to allocate switch domain: %s",
997 strerror(rte_errno));
1002 err = mlx5_args(&config, dpdk_dev->devargs);
1005 DRV_LOG(ERR, "failed to process device arguments: %s",
1006 strerror(rte_errno));
1009 config.hw_csum = !!(attr.device_cap_flags_ex & IBV_DEVICE_RAW_IP_CSUM);
1010 DRV_LOG(DEBUG, "checksum offloading is %ssupported",
1011 (config.hw_csum ? "" : "not "));
1012 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
1013 config.flow_counter_en = !!attr.max_counter_sets;
1014 mlx5_glue->describe_counter_set(ctx, 0, &cs_desc);
1015 DRV_LOG(DEBUG, "counter type = %d, num of cs = %ld, attributes = %d",
1016 cs_desc.counter_type, cs_desc.num_of_cs,
1017 cs_desc.attributes);
1019 config.ind_table_max_size =
1020 attr.rss_caps.max_rwq_indirection_table_size;
1022 * Remove this check once DPDK supports larger/variable
1023 * indirection tables.
1025 if (config.ind_table_max_size > (unsigned int)ETH_RSS_RETA_SIZE_512)
1026 config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
1027 DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
1028 config.ind_table_max_size);
1029 config.hw_vlan_strip = !!(attr.raw_packet_caps &
1030 IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
1031 DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
1032 (config.hw_vlan_strip ? "" : "not "));
1033 config.hw_fcs_strip = !!(attr.raw_packet_caps &
1034 IBV_RAW_PACKET_CAP_SCATTER_FCS);
1035 DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
1036 (config.hw_fcs_strip ? "" : "not "));
1037 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
1038 config.hw_padding = !!attr.rx_pad_end_addr_align;
1040 DRV_LOG(DEBUG, "hardware Rx end alignment padding is %ssupported",
1041 (config.hw_padding ? "" : "not "));
1042 config.tso = (attr.tso_caps.max_tso > 0 &&
1043 (attr.tso_caps.supported_qpts &
1044 (1 << IBV_QPT_RAW_PACKET)));
1046 config.tso_max_payload_sz = attr.tso_caps.max_tso;
1048 * MPW is disabled by default, while the Enhanced MPW is enabled
1051 if (config.mps == MLX5_ARG_UNSET)
1052 config.mps = (mps == MLX5_MPW_ENHANCED) ? MLX5_MPW_ENHANCED :
1055 config.mps = config.mps ? mps : MLX5_MPW_DISABLED;
1056 DRV_LOG(INFO, "%sMPS is %s",
1057 config.mps == MLX5_MPW_ENHANCED ? "enhanced " : "",
1058 config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
1059 if (config.cqe_comp && !cqe_comp) {
1060 DRV_LOG(WARNING, "Rx CQE compression isn't supported");
1061 config.cqe_comp = 0;
1063 if (config.mprq.enabled && mprq) {
1064 if (config.mprq.stride_num_n > mprq_max_stride_num_n ||
1065 config.mprq.stride_num_n < mprq_min_stride_num_n) {
1066 config.mprq.stride_num_n =
1067 RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
1068 mprq_min_stride_num_n);
1070 "the number of strides"
1071 " for Multi-Packet RQ is out of range,"
1072 " setting default value (%u)",
1073 1 << config.mprq.stride_num_n);
1075 config.mprq.min_stride_size_n = mprq_min_stride_size_n;
1076 config.mprq.max_stride_size_n = mprq_max_stride_size_n;
1077 } else if (config.mprq.enabled && !mprq) {
1078 DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
1079 config.mprq.enabled = 0;
1081 eth_dev = rte_eth_dev_allocate(name);
1082 if (eth_dev == NULL) {
1083 DRV_LOG(ERR, "can not allocate rte ethdev");
1087 if (priv->representor)
1088 eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
1089 eth_dev->data->dev_private = priv;
1090 priv->dev_data = eth_dev->data;
1091 eth_dev->data->mac_addrs = priv->mac;
1092 eth_dev->device = dpdk_dev;
1093 err = mlx5_uar_init_primary(eth_dev);
1098 /* Configure the first MAC address by default. */
1099 if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1101 "port %u cannot get MAC address, is mlx5_en"
1102 " loaded? (errno: %s)",
1103 eth_dev->data->port_id, strerror(rte_errno));
1108 "port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1109 eth_dev->data->port_id,
1110 mac.addr_bytes[0], mac.addr_bytes[1],
1111 mac.addr_bytes[2], mac.addr_bytes[3],
1112 mac.addr_bytes[4], mac.addr_bytes[5]);
1115 char ifname[IF_NAMESIZE];
1117 if (mlx5_get_ifname(eth_dev, &ifname) == 0)
1118 DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1119 eth_dev->data->port_id, ifname);
1121 DRV_LOG(DEBUG, "port %u ifname is unknown",
1122 eth_dev->data->port_id);
1125 /* Get actual MTU if possible. */
1126 err = mlx5_get_mtu(eth_dev, &priv->mtu);
1131 DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1133 /* Initialize burst functions to prevent crashes before link-up. */
1134 eth_dev->rx_pkt_burst = removed_rx_burst;
1135 eth_dev->tx_pkt_burst = removed_tx_burst;
1136 eth_dev->dev_ops = &mlx5_dev_ops;
1137 /* Register MAC address. */
1138 claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1139 if (vf && config.vf_nl_en)
1140 mlx5_nl_mac_addr_sync(eth_dev);
1141 priv->mnl_socket = mlx5_flow_tcf_socket_create();
1142 if (!priv->mnl_socket) {
1145 "flow rules relying on switch offloads will not be"
1146 " supported: cannot open libmnl socket: %s",
1147 strerror(rte_errno));
1149 struct rte_flow_error error;
1150 unsigned int ifindex = mlx5_ifindex(eth_dev);
1155 "cannot retrieve network interface index";
1157 err = mlx5_flow_tcf_init(priv->mnl_socket, ifindex,
1162 "flow rules relying on switch offloads will"
1163 " not be supported: %s: %s",
1164 error.message, strerror(rte_errno));
1165 mlx5_flow_tcf_socket_destroy(priv->mnl_socket);
1166 priv->mnl_socket = NULL;
1169 TAILQ_INIT(&priv->flows);
1170 TAILQ_INIT(&priv->ctrl_flows);
1171 /* Hint libmlx5 to use PMD allocator for data plane resources */
1172 struct mlx5dv_ctx_allocators alctr = {
1173 .alloc = &mlx5_alloc_verbs_buf,
1174 .free = &mlx5_free_verbs_buf,
1177 mlx5_glue->dv_set_context_attr(ctx, MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
1178 (void *)((uintptr_t)&alctr));
1179 /* Bring Ethernet device up. */
1180 DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1181 eth_dev->data->port_id);
1182 mlx5_set_link_up(eth_dev);
1184 * Even though the interrupt handler is not installed yet,
1185 * interrupts will still trigger on the asyn_fd from
1186 * Verbs context returned by ibv_open_device().
1188 mlx5_link_update(eth_dev, 0);
1189 /* Store device configuration on private structure. */
1190 priv->config = config;
1191 /* Supported Verbs flow priority number detection. */
1192 err = mlx5_flow_discover_priorities(eth_dev);
1195 priv->config.flow_prio = err;
1197 * Once the device is added to the list of memory event
1198 * callback, its global MR cache table cannot be expanded
1199 * on the fly because of deadlock. If it overflows, lookup
1200 * should be done by searching MR list linearly, which is slow.
1202 err = mlx5_mr_btree_init(&priv->mr.cache,
1203 MLX5_MR_BTREE_CACHE_N * 2,
1204 eth_dev->device->numa_node);
1209 /* Add device to memory callback list. */
1210 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
1211 LIST_INSERT_HEAD(&mlx5_shared_data->mem_event_cb_list,
1212 priv, mem_event_cb);
1213 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
1217 if (priv->nl_socket_route >= 0)
1218 close(priv->nl_socket_route);
1219 if (priv->nl_socket_rdma >= 0)
1220 close(priv->nl_socket_rdma);
1221 if (priv->mnl_socket)
1222 mlx5_flow_tcf_socket_destroy(priv->mnl_socket);
1224 claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1228 claim_zero(mlx5_glue->dealloc_pd(pd));
1230 rte_eth_dev_release_port(eth_dev);
1232 claim_zero(mlx5_glue->close_device(ctx));
1238 /** Data associated with devices to spawn. */
1239 struct mlx5_dev_spawn_data {
1240 unsigned int ifindex; /**< Network interface index. */
1241 struct mlx5_switch_info info; /**< Switch information. */
1242 struct ibv_device *ibv_dev; /**< Associated IB device. */
1243 struct rte_eth_dev *eth_dev; /**< Associated Ethernet device. */
1247 * Comparison callback to sort device data.
1249 * This is meant to be used with qsort().
1252 * Pointer to pointer to first data object.
1254 * Pointer to pointer to second data object.
1257 * 0 if both objects are equal, less than 0 if the first argument is less
1258 * than the second, greater than 0 otherwise.
1261 mlx5_dev_spawn_data_cmp(const void *a, const void *b)
1263 const struct mlx5_switch_info *si_a =
1264 &((const struct mlx5_dev_spawn_data *)a)->info;
1265 const struct mlx5_switch_info *si_b =
1266 &((const struct mlx5_dev_spawn_data *)b)->info;
1269 /* Master device first. */
1270 ret = si_b->master - si_a->master;
1273 /* Then representor devices. */
1274 ret = si_b->representor - si_a->representor;
1277 /* Unidentified devices come last in no specific order. */
1278 if (!si_a->representor)
1280 /* Order representors by name. */
1281 return si_a->port_name - si_b->port_name;
1285 * DPDK callback to register a PCI device.
1287 * This function spawns Ethernet devices out of a given PCI device.
1289 * @param[in] pci_drv
1290 * PCI driver structure (mlx5_driver).
1291 * @param[in] pci_dev
1292 * PCI device information.
1295 * 0 on success, a negative errno value otherwise and rte_errno is set.
1298 mlx5_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1299 struct rte_pci_device *pci_dev)
1301 struct ibv_device **ibv_list;
1306 assert(pci_drv == &mlx5_driver);
1308 ibv_list = mlx5_glue->get_device_list(&ret);
1310 rte_errno = errno ? errno : ENOSYS;
1311 DRV_LOG(ERR, "cannot list devices, is ib_uverbs loaded?");
1315 struct ibv_device *ibv_match[ret + 1];
1318 struct rte_pci_addr pci_addr;
1320 DRV_LOG(DEBUG, "checking device \"%s\"", ibv_list[ret]->name);
1321 if (mlx5_ibv_device_to_pci_addr(ibv_list[ret], &pci_addr))
1323 if (pci_dev->addr.domain != pci_addr.domain ||
1324 pci_dev->addr.bus != pci_addr.bus ||
1325 pci_dev->addr.devid != pci_addr.devid ||
1326 pci_dev->addr.function != pci_addr.function)
1328 DRV_LOG(INFO, "PCI information matches for device \"%s\"",
1329 ibv_list[ret]->name);
1330 ibv_match[n++] = ibv_list[ret];
1332 ibv_match[n] = NULL;
1334 struct mlx5_dev_spawn_data list[n];
1335 int nl_route = n ? mlx5_nl_init(NETLINK_ROUTE) : -1;
1336 int nl_rdma = n ? mlx5_nl_init(NETLINK_RDMA) : -1;
1341 * The existence of several matching entries (n > 1) means port
1342 * representors have been instantiated. No existing Verbs call nor
1343 * /sys entries can tell them apart, this can only be done through
1344 * Netlink calls assuming kernel drivers are recent enough to
1347 * In the event of identification failure through Netlink, try again
1348 * through sysfs, then either:
1350 * 1. No device matches (n == 0), complain and bail out.
1351 * 2. A single IB device matches (n == 1) and is not a representor,
1352 * assume no switch support.
1353 * 3. Otherwise no safe assumptions can be made; complain louder and
1356 for (i = 0; i != n; ++i) {
1357 list[i].ibv_dev = ibv_match[i];
1358 list[i].eth_dev = NULL;
1360 list[i].ifindex = 0;
1362 list[i].ifindex = mlx5_nl_ifindex
1363 (nl_rdma, list[i].ibv_dev->name);
1366 mlx5_nl_switch_info(nl_route, list[i].ifindex,
1368 ((!list[i].info.representor && !list[i].info.master) &&
1369 mlx5_sysfs_switch_info(list[i].ifindex, &list[i].info))) {
1370 list[i].ifindex = 0;
1371 memset(&list[i].info, 0, sizeof(list[i].info));
1379 /* Count unidentified devices. */
1380 for (u = 0, i = 0; i != n; ++i)
1381 if (!list[i].info.master && !list[i].info.representor)
1384 if (n == 1 && u == 1) {
1386 DRV_LOG(INFO, "no switch support detected");
1390 "unable to tell which of the matching devices"
1391 " is the master (lack of kernel support?)");
1396 * Sort list to probe devices in natural order for users convenience
1397 * (i.e. master first, then representors from lowest to highest ID).
1400 qsort(list, n, sizeof(*list), mlx5_dev_spawn_data_cmp);
1401 switch (pci_dev->id.device_id) {
1402 case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1403 case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1404 case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1405 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1411 for (i = 0; i != n; ++i) {
1414 list[i].eth_dev = mlx5_dev_spawn
1415 (&pci_dev->device, list[i].ibv_dev, vf, &list[i].info);
1416 if (!list[i].eth_dev) {
1417 if (rte_errno != EBUSY)
1419 /* Device is disabled, ignore it. */
1422 restore = list[i].eth_dev->data->dev_flags;
1423 rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
1424 /* Restore non-PCI flags cleared by the above call. */
1425 list[i].eth_dev->data->dev_flags |= restore;
1426 rte_eth_dev_probing_finish(list[i].eth_dev);
1428 mlx5_glue->free_device_list(ibv_list);
1431 "no Verbs device matches PCI device " PCI_PRI_FMT ","
1432 " are kernel drivers loaded?",
1433 pci_dev->addr.domain, pci_dev->addr.bus,
1434 pci_dev->addr.devid, pci_dev->addr.function);
1437 } else if (i != n) {
1439 "probe of PCI device " PCI_PRI_FMT " aborted after"
1440 " encountering an error: %s",
1441 pci_dev->addr.domain, pci_dev->addr.bus,
1442 pci_dev->addr.devid, pci_dev->addr.function,
1443 strerror(rte_errno));
1447 if (!list[i].eth_dev)
1449 mlx5_dev_close(list[i].eth_dev);
1450 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1451 rte_free(list[i].eth_dev->data->dev_private);
1452 claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
1454 /* Restore original error. */
1462 static const struct rte_pci_id mlx5_pci_id_map[] = {
1464 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1465 PCI_DEVICE_ID_MELLANOX_CONNECTX4)
1468 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1469 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
1472 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1473 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
1476 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1477 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
1480 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1481 PCI_DEVICE_ID_MELLANOX_CONNECTX5)
1484 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1485 PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
1488 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1489 PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
1492 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1493 PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
1496 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1497 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
1500 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1501 PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
1508 static struct rte_pci_driver mlx5_driver = {
1510 .name = MLX5_DRIVER_NAME
1512 .id_table = mlx5_pci_id_map,
1513 .probe = mlx5_pci_probe,
1514 .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
1517 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1520 * Suffix RTE_EAL_PMD_PATH with "-glue".
1522 * This function performs a sanity check on RTE_EAL_PMD_PATH before
1523 * suffixing its last component.
1526 * Output buffer, should be large enough otherwise NULL is returned.
1531 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
1534 mlx5_glue_path(char *buf, size_t size)
1536 static const char *const bad[] = { "/", ".", "..", NULL };
1537 const char *path = RTE_EAL_PMD_PATH;
1538 size_t len = strlen(path);
1542 while (len && path[len - 1] == '/')
1544 for (off = len; off && path[off - 1] != '/'; --off)
1546 for (i = 0; bad[i]; ++i)
1547 if (!strncmp(path + off, bad[i], (int)(len - off)))
1549 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1550 if (i == -1 || (size_t)i >= size)
1555 "unable to append \"-glue\" to last component of"
1556 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1557 " please re-configure DPDK");
1562 * Initialization routine for run-time dependency on rdma-core.
1565 mlx5_glue_init(void)
1567 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1568 const char *path[] = {
1570 * A basic security check is necessary before trusting
1571 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1573 (geteuid() == getuid() && getegid() == getgid() ?
1574 getenv("MLX5_GLUE_PATH") : NULL),
1576 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1577 * variant, otherwise let dlopen() look up libraries on its
1580 (*RTE_EAL_PMD_PATH ?
1581 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
1584 void *handle = NULL;
1588 while (!handle && i != RTE_DIM(path)) {
1597 end = strpbrk(path[i], ":;");
1599 end = path[i] + strlen(path[i]);
1600 len = end - path[i];
1605 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
1607 (!len || *(end - 1) == '/') ? "" : "/");
1610 if (sizeof(name) != (size_t)ret + 1)
1612 DRV_LOG(DEBUG, "looking for rdma-core glue as \"%s\"",
1614 handle = dlopen(name, RTLD_LAZY);
1625 DRV_LOG(WARNING, "cannot load glue library: %s", dlmsg);
1628 sym = dlsym(handle, "mlx5_glue");
1629 if (!sym || !*sym) {
1633 DRV_LOG(ERR, "cannot resolve glue symbol: %s", dlmsg);
1642 "cannot initialize PMD due to missing run-time dependency on"
1643 " rdma-core libraries (libibverbs, libmlx5)");
1650 * Driver initialization routine.
1652 RTE_INIT(rte_mlx5_pmd_init)
1654 /* Initialize driver log type. */
1655 mlx5_logtype = rte_log_register("pmd.net.mlx5");
1656 if (mlx5_logtype >= 0)
1657 rte_log_set_level(mlx5_logtype, RTE_LOG_NOTICE);
1659 /* Build the static tables for Verbs conversion. */
1660 mlx5_set_ptype_table();
1661 mlx5_set_cksum_table();
1662 mlx5_set_swp_types_table();
1664 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1665 * huge pages. Calling ibv_fork_init() during init allows
1666 * applications to use fork() safely for purposes other than
1667 * using this PMD, which is not supported in forked processes.
1669 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1670 /* Match the size of Rx completion entry to the size of a cacheline. */
1671 if (RTE_CACHE_LINE_SIZE == 128)
1672 setenv("MLX5_CQE_SIZE", "128", 0);
1674 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
1675 * cleanup all the Verbs resources even when the device was removed.
1677 setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
1678 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1679 if (mlx5_glue_init())
1684 /* Glue structure must not contain any NULL pointers. */
1688 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
1689 assert(((const void *const *)mlx5_glue)[i]);
1692 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
1694 "rdma-core glue \"%s\" mismatch: \"%s\" is required",
1695 mlx5_glue->version, MLX5_GLUE_VERSION);
1698 mlx5_glue->fork_init();
1699 rte_pci_register(&mlx5_driver);
1702 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1703 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1704 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");