1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
14 #include <linux/rtnetlink.h>
17 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
19 #pragma GCC diagnostic ignored "-Wpedantic"
21 #include <infiniband/verbs.h>
23 #pragma GCC diagnostic error "-Wpedantic"
26 #include <rte_malloc.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_ethdev_pci.h>
30 #include <rte_bus_pci.h>
31 #include <rte_common.h>
32 #include <rte_kvargs.h>
33 #include <rte_rwlock.h>
34 #include <rte_spinlock.h>
35 #include <rte_string_fns.h>
36 #include <rte_alarm.h>
38 #include <mlx5_glue.h>
39 #include <mlx5_devx_cmds.h>
40 #include <mlx5_common.h>
41 #include <mlx5_common_mp.h>
43 #include "mlx5_defs.h"
45 #include "mlx5_utils.h"
46 #include "mlx5_rxtx.h"
47 #include "mlx5_autoconf.h"
49 #include "mlx5_flow.h"
50 #include "rte_pmd_mlx5.h"
52 /* Device parameter to enable RX completion queue compression. */
53 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
55 /* Device parameter to enable RX completion entry padding to 128B. */
56 #define MLX5_RXQ_CQE_PAD_EN "rxq_cqe_pad_en"
58 /* Device parameter to enable padding Rx packet to cacheline size. */
59 #define MLX5_RXQ_PKT_PAD_EN "rxq_pkt_pad_en"
61 /* Device parameter to enable Multi-Packet Rx queue. */
62 #define MLX5_RX_MPRQ_EN "mprq_en"
64 /* Device parameter to configure log 2 of the number of strides for MPRQ. */
65 #define MLX5_RX_MPRQ_LOG_STRIDE_NUM "mprq_log_stride_num"
67 /* Device parameter to configure log 2 of the stride size for MPRQ. */
68 #define MLX5_RX_MPRQ_LOG_STRIDE_SIZE "mprq_log_stride_size"
70 /* Device parameter to limit the size of memcpy'd packet for MPRQ. */
71 #define MLX5_RX_MPRQ_MAX_MEMCPY_LEN "mprq_max_memcpy_len"
73 /* Device parameter to set the minimum number of Rx queues to enable MPRQ. */
74 #define MLX5_RXQS_MIN_MPRQ "rxqs_min_mprq"
76 /* Device parameter to configure inline send. Deprecated, ignored.*/
77 #define MLX5_TXQ_INLINE "txq_inline"
79 /* Device parameter to limit packet size to inline with ordinary SEND. */
80 #define MLX5_TXQ_INLINE_MAX "txq_inline_max"
82 /* Device parameter to configure minimal data size to inline. */
83 #define MLX5_TXQ_INLINE_MIN "txq_inline_min"
85 /* Device parameter to limit packet size to inline with Enhanced MPW. */
86 #define MLX5_TXQ_INLINE_MPW "txq_inline_mpw"
89 * Device parameter to configure the number of TX queues threshold for
90 * enabling inline send.
92 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
95 * Device parameter to configure the number of TX queues threshold for
96 * enabling vectorized Tx, deprecated, ignored (no vectorized Tx routines).
98 #define MLX5_TXQS_MAX_VEC "txqs_max_vec"
100 /* Device parameter to enable multi-packet send WQEs. */
101 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
104 * Device parameter to force doorbell register mapping
105 * to non-cahed region eliminating the extra write memory barrier.
107 #define MLX5_TX_DB_NC "tx_db_nc"
110 * Device parameter to include 2 dsegs in the title WQEBB.
111 * Deprecated, ignored.
113 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
116 * Device parameter to limit the size of inlining packet.
117 * Deprecated, ignored.
119 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
122 * Device parameter to enable hardware Tx vector.
123 * Deprecated, ignored (no vectorized Tx routines anymore).
125 #define MLX5_TX_VEC_EN "tx_vec_en"
127 /* Device parameter to enable hardware Rx vector. */
128 #define MLX5_RX_VEC_EN "rx_vec_en"
130 /* Allow L3 VXLAN flow creation. */
131 #define MLX5_L3_VXLAN_EN "l3_vxlan_en"
133 /* Activate DV E-Switch flow steering. */
134 #define MLX5_DV_ESW_EN "dv_esw_en"
136 /* Activate DV flow steering. */
137 #define MLX5_DV_FLOW_EN "dv_flow_en"
139 /* Enable extensive flow metadata support. */
140 #define MLX5_DV_XMETA_EN "dv_xmeta_en"
142 /* Activate Netlink support in VF mode. */
143 #define MLX5_VF_NL_EN "vf_nl_en"
145 /* Enable extending memsegs when creating a MR. */
146 #define MLX5_MR_EXT_MEMSEG_EN "mr_ext_memseg_en"
148 /* Select port representors to instantiate. */
149 #define MLX5_REPRESENTOR "representor"
151 /* Device parameter to configure the maximum number of dump files per queue. */
152 #define MLX5_MAX_DUMP_FILES_NUM "max_dump_files_num"
154 /* Configure timeout of LRO session (in microseconds). */
155 #define MLX5_LRO_TIMEOUT_USEC "lro_timeout_usec"
158 * Device parameter to configure the total data buffer size for a single
159 * hairpin queue (logarithm value).
161 #define MLX5_HP_BUF_SIZE "hp_buf_log_sz"
163 /* Flow memory reclaim mode. */
164 #define MLX5_RECLAIM_MEM "reclaim_mem_mode"
166 static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
168 /* Shared memory between primary and secondary processes. */
169 struct mlx5_shared_data *mlx5_shared_data;
171 /* Spinlock for mlx5_shared_data allocation. */
172 static rte_spinlock_t mlx5_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
174 /* Process local data for secondary processes. */
175 static struct mlx5_local_data mlx5_local_data;
176 /** Driver-specific log messages type. */
179 static LIST_HEAD(, mlx5_dev_ctx_shared) mlx5_dev_ctx_list =
180 LIST_HEAD_INITIALIZER();
181 static pthread_mutex_t mlx5_dev_ctx_list_mutex = PTHREAD_MUTEX_INITIALIZER;
183 static const struct mlx5_indexed_pool_config mlx5_ipool_cfg[] = {
184 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
186 .size = sizeof(struct mlx5_flow_dv_encap_decap_resource),
192 .malloc = rte_malloc_socket,
194 .type = "mlx5_encap_decap_ipool",
197 .size = sizeof(struct mlx5_flow_dv_push_vlan_action_resource),
203 .malloc = rte_malloc_socket,
205 .type = "mlx5_push_vlan_ipool",
208 .size = sizeof(struct mlx5_flow_dv_tag_resource),
214 .malloc = rte_malloc_socket,
216 .type = "mlx5_tag_ipool",
219 .size = sizeof(struct mlx5_flow_dv_port_id_action_resource),
225 .malloc = rte_malloc_socket,
227 .type = "mlx5_port_id_ipool",
230 .size = sizeof(struct mlx5_flow_tbl_data_entry),
236 .malloc = rte_malloc_socket,
238 .type = "mlx5_jump_ipool",
242 .size = sizeof(struct mlx5_flow_meter),
248 .malloc = rte_malloc_socket,
250 .type = "mlx5_meter_ipool",
253 .size = sizeof(struct mlx5_flow_mreg_copy_resource),
259 .malloc = rte_malloc_socket,
261 .type = "mlx5_mcp_ipool",
264 .size = (sizeof(struct mlx5_hrxq) + MLX5_RSS_HASH_KEY_LEN),
270 .malloc = rte_malloc_socket,
272 .type = "mlx5_hrxq_ipool",
276 * MLX5_IPOOL_MLX5_FLOW size varies for DV and VERBS flows.
277 * It set in run time according to PCI function configuration.
285 .malloc = rte_malloc_socket,
287 .type = "mlx5_flow_handle_ipool",
290 .size = sizeof(struct rte_flow),
294 .malloc = rte_malloc_socket,
296 .type = "rte_flow_ipool",
301 #define MLX5_FLOW_MIN_ID_POOL_SIZE 512
302 #define MLX5_ID_GENERATION_ARRAY_FACTOR 16
304 #define MLX5_FLOW_TABLE_HLIST_ARRAY_SIZE 4096
307 * Allocate ID pool structure.
310 * The maximum id can be allocated from the pool.
313 * Pointer to pool object, NULL value otherwise.
315 struct mlx5_flow_id_pool *
316 mlx5_flow_id_pool_alloc(uint32_t max_id)
318 struct mlx5_flow_id_pool *pool;
321 pool = rte_zmalloc("id pool allocation", sizeof(*pool),
322 RTE_CACHE_LINE_SIZE);
324 DRV_LOG(ERR, "can't allocate id pool");
328 mem = rte_zmalloc("", MLX5_FLOW_MIN_ID_POOL_SIZE * sizeof(uint32_t),
329 RTE_CACHE_LINE_SIZE);
331 DRV_LOG(ERR, "can't allocate mem for id pool");
335 pool->free_arr = mem;
336 pool->curr = pool->free_arr;
337 pool->last = pool->free_arr + MLX5_FLOW_MIN_ID_POOL_SIZE;
338 pool->base_index = 0;
339 pool->max_id = max_id;
347 * Release ID pool structure.
350 * Pointer to flow id pool object to free.
353 mlx5_flow_id_pool_release(struct mlx5_flow_id_pool *pool)
355 rte_free(pool->free_arr);
363 * Pointer to flow id pool.
368 * 0 on success, error value otherwise.
371 mlx5_flow_id_get(struct mlx5_flow_id_pool *pool, uint32_t *id)
373 if (pool->curr == pool->free_arr) {
374 if (pool->base_index == pool->max_id) {
376 DRV_LOG(ERR, "no free id");
379 *id = ++pool->base_index;
382 *id = *(--pool->curr);
390 * Pointer to flow id pool.
395 * 0 on success, error value otherwise.
398 mlx5_flow_id_release(struct mlx5_flow_id_pool *pool, uint32_t id)
404 if (pool->curr == pool->last) {
405 size = pool->curr - pool->free_arr;
406 size2 = size * MLX5_ID_GENERATION_ARRAY_FACTOR;
407 MLX5_ASSERT(size2 > size);
408 mem = rte_malloc("", size2 * sizeof(uint32_t), 0);
410 DRV_LOG(ERR, "can't allocate mem for id pool");
414 memcpy(mem, pool->free_arr, size * sizeof(uint32_t));
415 rte_free(pool->free_arr);
416 pool->free_arr = mem;
417 pool->curr = pool->free_arr + size;
418 pool->last = pool->free_arr + size2;
426 * Initialize the shared aging list information per port.
429 * Pointer to mlx5_dev_ctx_shared object.
432 mlx5_flow_aging_init(struct mlx5_dev_ctx_shared *sh)
435 struct mlx5_age_info *age_info;
437 for (i = 0; i < sh->max_port; i++) {
438 age_info = &sh->port[i].age_info;
440 TAILQ_INIT(&age_info->aged_counters);
441 rte_spinlock_init(&age_info->aged_sl);
442 MLX5_AGE_SET(age_info, MLX5_AGE_TRIGGER);
447 * Initialize the counters management structure.
450 * Pointer to mlx5_dev_ctx_shared object to free
453 mlx5_flow_counters_mng_init(struct mlx5_dev_ctx_shared *sh)
457 memset(&sh->cmng, 0, sizeof(sh->cmng));
458 TAILQ_INIT(&sh->cmng.flow_counters);
459 for (i = 0; i < MLX5_CCONT_TYPE_MAX; ++i) {
460 sh->cmng.ccont[i].min_id = MLX5_CNT_BATCH_OFFSET;
461 sh->cmng.ccont[i].max_id = -1;
462 sh->cmng.ccont[i].last_pool_idx = POOL_IDX_INVALID;
463 TAILQ_INIT(&sh->cmng.ccont[i].pool_list);
464 rte_spinlock_init(&sh->cmng.ccont[i].resize_sl);
469 * Destroy all the resources allocated for a counter memory management.
472 * Pointer to the memory management structure.
475 mlx5_flow_destroy_counter_stat_mem_mng(struct mlx5_counter_stats_mem_mng *mng)
477 uint8_t *mem = (uint8_t *)(uintptr_t)mng->raws[0].data;
479 LIST_REMOVE(mng, next);
480 claim_zero(mlx5_devx_cmd_destroy(mng->dm));
481 claim_zero(mlx5_glue->devx_umem_dereg(mng->umem));
486 * Close and release all the resources of the counters management.
489 * Pointer to mlx5_dev_ctx_shared object to free.
492 mlx5_flow_counters_mng_close(struct mlx5_dev_ctx_shared *sh)
494 struct mlx5_counter_stats_mem_mng *mng;
501 rte_eal_alarm_cancel(mlx5_flow_query_alarm, sh);
502 if (rte_errno != EINPROGRESS)
506 for (i = 0; i < MLX5_CCONT_TYPE_MAX; ++i) {
507 struct mlx5_flow_counter_pool *pool;
508 uint32_t batch = !!(i > 1);
510 if (!sh->cmng.ccont[i].pools)
512 pool = TAILQ_FIRST(&sh->cmng.ccont[i].pool_list);
514 if (batch && pool->min_dcs)
515 claim_zero(mlx5_devx_cmd_destroy
517 for (j = 0; j < MLX5_COUNTERS_PER_POOL; ++j) {
518 if (MLX5_POOL_GET_CNT(pool, j)->action)
520 (mlx5_glue->destroy_flow_action
523 if (!batch && MLX5_GET_POOL_CNT_EXT
525 claim_zero(mlx5_devx_cmd_destroy
526 (MLX5_GET_POOL_CNT_EXT
529 TAILQ_REMOVE(&sh->cmng.ccont[i].pool_list, pool, next);
531 pool = TAILQ_FIRST(&sh->cmng.ccont[i].pool_list);
533 rte_free(sh->cmng.ccont[i].pools);
535 mng = LIST_FIRST(&sh->cmng.mem_mngs);
537 mlx5_flow_destroy_counter_stat_mem_mng(mng);
538 mng = LIST_FIRST(&sh->cmng.mem_mngs);
540 memset(&sh->cmng, 0, sizeof(sh->cmng));
544 * Initialize the flow resources' indexed mempool.
547 * Pointer to mlx5_dev_ctx_shared object.
549 * Pointer to user dev config.
552 mlx5_flow_ipool_create(struct mlx5_dev_ctx_shared *sh,
553 const struct mlx5_dev_config *config)
556 struct mlx5_indexed_pool_config cfg;
558 for (i = 0; i < MLX5_IPOOL_MAX; ++i) {
559 cfg = mlx5_ipool_cfg[i];
564 * Set MLX5_IPOOL_MLX5_FLOW ipool size
565 * according to PCI function flow configuration.
567 case MLX5_IPOOL_MLX5_FLOW:
568 cfg.size = config->dv_flow_en ?
569 sizeof(struct mlx5_flow_handle) :
570 MLX5_FLOW_HANDLE_VERBS_SIZE;
573 if (config->reclaim_mode)
574 cfg.release_mem_en = 1;
575 sh->ipool[i] = mlx5_ipool_create(&cfg);
580 * Release the flow resources' indexed mempool.
583 * Pointer to mlx5_dev_ctx_shared object.
586 mlx5_flow_ipool_destroy(struct mlx5_dev_ctx_shared *sh)
590 for (i = 0; i < MLX5_IPOOL_MAX; ++i)
591 mlx5_ipool_destroy(sh->ipool[i]);
595 * Allocate shared device context. If there is multiport device the
596 * master and representors will share this context, if there is single
597 * port dedicated device, the context will be used by only given
598 * port due to unification.
600 * Routine first searches the context for the specified device name,
601 * if found the shared context assumed and reference counter is incremented.
602 * If no context found the new one is created and initialized with specified
603 * device context and parameters.
606 * Pointer to the device attributes (name, port, etc).
608 * Pointer to device configuration structure.
611 * Pointer to mlx5_dev_ctx_shared object on success,
612 * otherwise NULL and rte_errno is set.
614 struct mlx5_dev_ctx_shared *
615 mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
616 const struct mlx5_dev_config *config)
618 struct mlx5_dev_ctx_shared *sh;
621 struct mlx5_devx_tis_attr tis_attr = { 0 };
624 /* Secondary process should not create the shared context. */
625 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
626 pthread_mutex_lock(&mlx5_dev_ctx_list_mutex);
627 /* Search for IB context by device name. */
628 LIST_FOREACH(sh, &mlx5_dev_ctx_list, next) {
629 if (!strcmp(sh->ibdev_name,
630 mlx5_os_get_dev_device_name(spawn->phys_dev))) {
635 /* No device found, we have to create new shared context. */
636 MLX5_ASSERT(spawn->max_port);
637 sh = rte_zmalloc("ethdev shared ib context",
638 sizeof(struct mlx5_dev_ctx_shared) +
640 sizeof(struct mlx5_dev_shared_port),
641 RTE_CACHE_LINE_SIZE);
643 DRV_LOG(ERR, "shared context allocation failure");
647 err = mlx5_os_open_device(spawn, config, sh);
650 err = mlx5_os_get_dev_attr(sh->ctx, &sh->device_attr);
652 DRV_LOG(DEBUG, "mlx5_os_get_dev_attr() failed");
656 sh->max_port = spawn->max_port;
657 strncpy(sh->ibdev_name, mlx5_os_get_ctx_device_name(sh->ctx),
658 sizeof(sh->ibdev_name) - 1);
659 strncpy(sh->ibdev_path, mlx5_os_get_ctx_device_path(sh->ctx),
660 sizeof(sh->ibdev_path) - 1);
662 * Setting port_id to max unallowed value means
663 * there is no interrupt subhandler installed for
664 * the given port index i.
666 for (i = 0; i < sh->max_port; i++) {
667 sh->port[i].ih_port_id = RTE_MAX_ETHPORTS;
668 sh->port[i].devx_ih_port_id = RTE_MAX_ETHPORTS;
670 sh->pd = mlx5_glue->alloc_pd(sh->ctx);
671 if (sh->pd == NULL) {
672 DRV_LOG(ERR, "PD allocation failure");
677 err = mlx5_os_get_pdn(sh->pd, &sh->pdn);
679 DRV_LOG(ERR, "Fail to extract pdn from PD");
682 sh->td = mlx5_devx_cmd_create_td(sh->ctx);
684 DRV_LOG(ERR, "TD allocation failure");
688 tis_attr.transport_domain = sh->td->id;
689 sh->tis = mlx5_devx_cmd_create_tis(sh->ctx, &tis_attr);
691 DRV_LOG(ERR, "TIS allocation failure");
696 sh->flow_id_pool = mlx5_flow_id_pool_alloc
697 ((1 << HAIRPIN_FLOW_ID_BITS) - 1);
698 if (!sh->flow_id_pool) {
699 DRV_LOG(ERR, "can't create flow id pool");
704 * Once the device is added to the list of memory event
705 * callback, its global MR cache table cannot be expanded
706 * on the fly because of deadlock. If it overflows, lookup
707 * should be done by searching MR list linearly, which is slow.
709 * At this point the device is not added to the memory
710 * event list yet, context is just being created.
712 err = mlx5_mr_btree_init(&sh->share_cache.cache,
713 MLX5_MR_BTREE_CACHE_N * 2,
714 spawn->pci_dev->device.numa_node);
719 mlx5_os_set_reg_mr_cb(&sh->share_cache.reg_mr_cb,
720 &sh->share_cache.dereg_mr_cb);
721 mlx5_os_dev_shared_handler_install(sh);
722 sh->cnt_id_tbl = mlx5_l3t_create(MLX5_L3T_TYPE_DWORD);
723 if (!sh->cnt_id_tbl) {
727 mlx5_flow_aging_init(sh);
728 mlx5_flow_counters_mng_init(sh);
729 mlx5_flow_ipool_create(sh, config);
730 /* Add device to memory callback list. */
731 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
732 LIST_INSERT_HEAD(&mlx5_shared_data->mem_event_cb_list,
734 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
735 /* Add context to the global device list. */
736 LIST_INSERT_HEAD(&mlx5_dev_ctx_list, sh, next);
738 pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
741 pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
743 if (sh->cnt_id_tbl) {
744 mlx5_l3t_destroy(sh->cnt_id_tbl);
745 sh->cnt_id_tbl = NULL;
748 claim_zero(mlx5_devx_cmd_destroy(sh->tis));
750 claim_zero(mlx5_devx_cmd_destroy(sh->td));
752 claim_zero(mlx5_glue->dealloc_pd(sh->pd));
754 claim_zero(mlx5_glue->close_device(sh->ctx));
755 if (sh->flow_id_pool)
756 mlx5_flow_id_pool_release(sh->flow_id_pool);
758 MLX5_ASSERT(err > 0);
764 * Free shared IB device context. Decrement counter and if zero free
765 * all allocated resources and close handles.
768 * Pointer to mlx5_dev_ctx_shared object to free
771 mlx5_free_shared_dev_ctx(struct mlx5_dev_ctx_shared *sh)
773 pthread_mutex_lock(&mlx5_dev_ctx_list_mutex);
774 #ifdef RTE_LIBRTE_MLX5_DEBUG
775 /* Check the object presence in the list. */
776 struct mlx5_dev_ctx_shared *lctx;
778 LIST_FOREACH(lctx, &mlx5_dev_ctx_list, next)
783 DRV_LOG(ERR, "Freeing non-existing shared IB context");
788 MLX5_ASSERT(sh->refcnt);
789 /* Secondary process should not free the shared context. */
790 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
793 /* Remove from memory callback device list. */
794 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
795 LIST_REMOVE(sh, mem_event_cb);
796 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
797 /* Release created Memory Regions. */
798 mlx5_mr_release_cache(&sh->share_cache);
799 /* Remove context from the global device list. */
800 LIST_REMOVE(sh, next);
802 * Ensure there is no async event handler installed.
803 * Only primary process handles async device events.
805 mlx5_flow_counters_mng_close(sh);
806 mlx5_flow_ipool_destroy(sh);
807 mlx5_os_dev_shared_handler_uninstall(sh);
808 if (sh->cnt_id_tbl) {
809 mlx5_l3t_destroy(sh->cnt_id_tbl);
810 sh->cnt_id_tbl = NULL;
813 claim_zero(mlx5_glue->dealloc_pd(sh->pd));
815 claim_zero(mlx5_devx_cmd_destroy(sh->tis));
817 claim_zero(mlx5_devx_cmd_destroy(sh->td));
819 claim_zero(mlx5_glue->close_device(sh->ctx));
820 if (sh->flow_id_pool)
821 mlx5_flow_id_pool_release(sh->flow_id_pool);
824 pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
828 * Destroy table hash list and all the root entries per domain.
831 * Pointer to the private device data structure.
834 mlx5_free_table_hash_list(struct mlx5_priv *priv)
836 struct mlx5_dev_ctx_shared *sh = priv->sh;
837 struct mlx5_flow_tbl_data_entry *tbl_data;
838 union mlx5_flow_tbl_key table_key = {
846 struct mlx5_hlist_entry *pos;
850 pos = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64);
852 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
854 MLX5_ASSERT(tbl_data);
855 mlx5_hlist_remove(sh->flow_tbls, pos);
858 table_key.direction = 1;
859 pos = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64);
861 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
863 MLX5_ASSERT(tbl_data);
864 mlx5_hlist_remove(sh->flow_tbls, pos);
867 table_key.direction = 0;
868 table_key.domain = 1;
869 pos = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64);
871 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
873 MLX5_ASSERT(tbl_data);
874 mlx5_hlist_remove(sh->flow_tbls, pos);
877 mlx5_hlist_destroy(sh->flow_tbls, NULL, NULL);
881 * Initialize flow table hash list and create the root tables entry
885 * Pointer to the private device data structure.
888 * Zero on success, positive error code otherwise.
891 mlx5_alloc_table_hash_list(struct mlx5_priv *priv)
893 struct mlx5_dev_ctx_shared *sh = priv->sh;
894 char s[MLX5_HLIST_NAMESIZE];
898 snprintf(s, sizeof(s), "%s_flow_table", priv->sh->ibdev_name);
899 sh->flow_tbls = mlx5_hlist_create(s, MLX5_FLOW_TABLE_HLIST_ARRAY_SIZE);
900 if (!sh->flow_tbls) {
901 DRV_LOG(ERR, "flow tables with hash creation failed.");
905 #ifndef HAVE_MLX5DV_DR
907 * In case we have not DR support, the zero tables should be created
908 * because DV expect to see them even if they cannot be created by
911 union mlx5_flow_tbl_key table_key = {
919 struct mlx5_flow_tbl_data_entry *tbl_data = rte_zmalloc(NULL,
920 sizeof(*tbl_data), 0);
926 tbl_data->entry.key = table_key.v64;
927 err = mlx5_hlist_insert(sh->flow_tbls, &tbl_data->entry);
930 rte_atomic32_init(&tbl_data->tbl.refcnt);
931 rte_atomic32_inc(&tbl_data->tbl.refcnt);
932 table_key.direction = 1;
933 tbl_data = rte_zmalloc(NULL, sizeof(*tbl_data), 0);
938 tbl_data->entry.key = table_key.v64;
939 err = mlx5_hlist_insert(sh->flow_tbls, &tbl_data->entry);
942 rte_atomic32_init(&tbl_data->tbl.refcnt);
943 rte_atomic32_inc(&tbl_data->tbl.refcnt);
944 table_key.direction = 0;
945 table_key.domain = 1;
946 tbl_data = rte_zmalloc(NULL, sizeof(*tbl_data), 0);
951 tbl_data->entry.key = table_key.v64;
952 err = mlx5_hlist_insert(sh->flow_tbls, &tbl_data->entry);
955 rte_atomic32_init(&tbl_data->tbl.refcnt);
956 rte_atomic32_inc(&tbl_data->tbl.refcnt);
959 mlx5_free_table_hash_list(priv);
960 #endif /* HAVE_MLX5DV_DR */
965 * Initialize shared data between primary and secondary process.
967 * A memzone is reserved by primary process and secondary processes attach to
971 * 0 on success, a negative errno value otherwise and rte_errno is set.
974 mlx5_init_shared_data(void)
976 const struct rte_memzone *mz;
979 rte_spinlock_lock(&mlx5_shared_data_lock);
980 if (mlx5_shared_data == NULL) {
981 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
982 /* Allocate shared memory. */
983 mz = rte_memzone_reserve(MZ_MLX5_PMD_SHARED_DATA,
984 sizeof(*mlx5_shared_data),
988 "Cannot allocate mlx5 shared data");
992 mlx5_shared_data = mz->addr;
993 memset(mlx5_shared_data, 0, sizeof(*mlx5_shared_data));
994 rte_spinlock_init(&mlx5_shared_data->lock);
996 /* Lookup allocated shared memory. */
997 mz = rte_memzone_lookup(MZ_MLX5_PMD_SHARED_DATA);
1000 "Cannot attach mlx5 shared data");
1004 mlx5_shared_data = mz->addr;
1005 memset(&mlx5_local_data, 0, sizeof(mlx5_local_data));
1009 rte_spinlock_unlock(&mlx5_shared_data_lock);
1014 * Retrieve integer value from environment variable.
1017 * Environment variable name.
1020 * Integer value, 0 if the variable is not set.
1023 mlx5_getenv_int(const char *name)
1025 const char *val = getenv(name);
1033 * DPDK callback to add udp tunnel port
1036 * A pointer to eth_dev
1037 * @param[in] udp_tunnel
1038 * A pointer to udp tunnel
1041 * 0 on valid udp ports and tunnels, -ENOTSUP otherwise.
1044 mlx5_udp_tunnel_port_add(struct rte_eth_dev *dev __rte_unused,
1045 struct rte_eth_udp_tunnel *udp_tunnel)
1047 MLX5_ASSERT(udp_tunnel != NULL);
1048 if (udp_tunnel->prot_type == RTE_TUNNEL_TYPE_VXLAN &&
1049 udp_tunnel->udp_port == 4789)
1051 if (udp_tunnel->prot_type == RTE_TUNNEL_TYPE_VXLAN_GPE &&
1052 udp_tunnel->udp_port == 4790)
1058 * Initialize process private data structure.
1061 * Pointer to Ethernet device structure.
1064 * 0 on success, a negative errno value otherwise and rte_errno is set.
1067 mlx5_proc_priv_init(struct rte_eth_dev *dev)
1069 struct mlx5_priv *priv = dev->data->dev_private;
1070 struct mlx5_proc_priv *ppriv;
1074 * UAR register table follows the process private structure. BlueFlame
1075 * registers for Tx queues are stored in the table.
1078 sizeof(struct mlx5_proc_priv) + priv->txqs_n * sizeof(void *);
1079 ppriv = rte_malloc_socket("mlx5_proc_priv", ppriv_size,
1080 RTE_CACHE_LINE_SIZE, dev->device->numa_node);
1085 ppriv->uar_table_sz = ppriv_size;
1086 dev->process_private = ppriv;
1091 * Un-initialize process private data structure.
1094 * Pointer to Ethernet device structure.
1097 mlx5_proc_priv_uninit(struct rte_eth_dev *dev)
1099 if (!dev->process_private)
1101 rte_free(dev->process_private);
1102 dev->process_private = NULL;
1106 * DPDK callback to close the device.
1108 * Destroy all queues and objects, free memory.
1111 * Pointer to Ethernet device structure.
1114 mlx5_dev_close(struct rte_eth_dev *dev)
1116 struct mlx5_priv *priv = dev->data->dev_private;
1120 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1121 /* Check if process_private released. */
1122 if (!dev->process_private)
1124 mlx5_tx_uar_uninit_secondary(dev);
1125 mlx5_proc_priv_uninit(dev);
1126 rte_eth_dev_release_port(dev);
1131 DRV_LOG(DEBUG, "port %u closing device \"%s\"",
1133 ((priv->sh->ctx != NULL) ?
1134 mlx5_os_get_ctx_device_name(priv->sh->ctx) : ""));
1136 * If default mreg copy action is removed at the stop stage,
1137 * the search will return none and nothing will be done anymore.
1139 mlx5_flow_stop_default(dev);
1140 mlx5_traffic_disable(dev);
1142 * If all the flows are already flushed in the device stop stage,
1143 * then this will return directly without any action.
1145 mlx5_flow_list_flush(dev, &priv->flows, true);
1146 mlx5_flow_meter_flush(dev, NULL);
1147 /* Free the intermediate buffers for flow creation. */
1148 mlx5_flow_free_intermediate(dev);
1149 /* Prevent crashes when queues are still in use. */
1150 dev->rx_pkt_burst = removed_rx_burst;
1151 dev->tx_pkt_burst = removed_tx_burst;
1153 /* Disable datapath on secondary process. */
1154 mlx5_mp_req_stop_rxtx(dev);
1155 if (priv->rxqs != NULL) {
1156 /* XXX race condition if mlx5_rx_burst() is still running. */
1158 for (i = 0; (i != priv->rxqs_n); ++i)
1159 mlx5_rxq_release(dev, i);
1163 if (priv->txqs != NULL) {
1164 /* XXX race condition if mlx5_tx_burst() is still running. */
1166 for (i = 0; (i != priv->txqs_n); ++i)
1167 mlx5_txq_release(dev, i);
1171 mlx5_proc_priv_uninit(dev);
1172 if (priv->mreg_cp_tbl)
1173 mlx5_hlist_destroy(priv->mreg_cp_tbl, NULL, NULL);
1174 mlx5_mprq_free_mp(dev);
1175 mlx5_os_free_shared_dr(priv);
1176 if (priv->rss_conf.rss_key != NULL)
1177 rte_free(priv->rss_conf.rss_key);
1178 if (priv->reta_idx != NULL)
1179 rte_free(priv->reta_idx);
1180 if (priv->config.vf)
1181 mlx5_nl_mac_addr_flush(priv->nl_socket_route, mlx5_ifindex(dev),
1182 dev->data->mac_addrs,
1183 MLX5_MAX_MAC_ADDRESSES, priv->mac_own);
1184 if (priv->nl_socket_route >= 0)
1185 close(priv->nl_socket_route);
1186 if (priv->nl_socket_rdma >= 0)
1187 close(priv->nl_socket_rdma);
1188 if (priv->vmwa_context)
1189 mlx5_vlan_vmwa_exit(priv->vmwa_context);
1190 ret = mlx5_hrxq_verify(dev);
1192 DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
1193 dev->data->port_id);
1194 ret = mlx5_ind_table_obj_verify(dev);
1196 DRV_LOG(WARNING, "port %u some indirection table still remain",
1197 dev->data->port_id);
1198 ret = mlx5_rxq_obj_verify(dev);
1200 DRV_LOG(WARNING, "port %u some Rx queue objects still remain",
1201 dev->data->port_id);
1202 ret = mlx5_rxq_verify(dev);
1204 DRV_LOG(WARNING, "port %u some Rx queues still remain",
1205 dev->data->port_id);
1206 ret = mlx5_txq_obj_verify(dev);
1208 DRV_LOG(WARNING, "port %u some Verbs Tx queue still remain",
1209 dev->data->port_id);
1210 ret = mlx5_txq_verify(dev);
1212 DRV_LOG(WARNING, "port %u some Tx queues still remain",
1213 dev->data->port_id);
1214 ret = mlx5_flow_verify(dev);
1216 DRV_LOG(WARNING, "port %u some flows still remain",
1217 dev->data->port_id);
1219 * Free the shared context in last turn, because the cleanup
1220 * routines above may use some shared fields, like
1221 * mlx5_nl_mac_addr_flush() uses ibdev_path for retrieveing
1222 * ifindex if Netlink fails.
1224 mlx5_free_shared_dev_ctx(priv->sh);
1225 if (priv->domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
1229 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
1230 struct mlx5_priv *opriv =
1231 rte_eth_devices[port_id].data->dev_private;
1234 opriv->domain_id != priv->domain_id ||
1235 &rte_eth_devices[port_id] == dev)
1241 claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1243 memset(priv, 0, sizeof(*priv));
1244 priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
1246 * Reset mac_addrs to NULL such that it is not freed as part of
1247 * rte_eth_dev_release_port(). mac_addrs is part of dev_private so
1248 * it is freed when dev_private is freed.
1250 dev->data->mac_addrs = NULL;
1254 * Verify and store value for device argument.
1257 * Key argument to verify.
1259 * Value associated with key.
1264 * 0 on success, a negative errno value otherwise and rte_errno is set.
1267 mlx5_args_check(const char *key, const char *val, void *opaque)
1269 struct mlx5_dev_config *config = opaque;
1272 /* No-op, port representors are processed in mlx5_dev_spawn(). */
1273 if (!strcmp(MLX5_REPRESENTOR, key))
1276 tmp = strtoul(val, NULL, 0);
1279 DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
1282 if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
1283 config->cqe_comp = !!tmp;
1284 } else if (strcmp(MLX5_RXQ_CQE_PAD_EN, key) == 0) {
1285 config->cqe_pad = !!tmp;
1286 } else if (strcmp(MLX5_RXQ_PKT_PAD_EN, key) == 0) {
1287 config->hw_padding = !!tmp;
1288 } else if (strcmp(MLX5_RX_MPRQ_EN, key) == 0) {
1289 config->mprq.enabled = !!tmp;
1290 } else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_NUM, key) == 0) {
1291 config->mprq.stride_num_n = tmp;
1292 } else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_SIZE, key) == 0) {
1293 config->mprq.stride_size_n = tmp;
1294 } else if (strcmp(MLX5_RX_MPRQ_MAX_MEMCPY_LEN, key) == 0) {
1295 config->mprq.max_memcpy_len = tmp;
1296 } else if (strcmp(MLX5_RXQS_MIN_MPRQ, key) == 0) {
1297 config->mprq.min_rxqs_num = tmp;
1298 } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
1299 DRV_LOG(WARNING, "%s: deprecated parameter,"
1300 " converted to txq_inline_max", key);
1301 config->txq_inline_max = tmp;
1302 } else if (strcmp(MLX5_TXQ_INLINE_MAX, key) == 0) {
1303 config->txq_inline_max = tmp;
1304 } else if (strcmp(MLX5_TXQ_INLINE_MIN, key) == 0) {
1305 config->txq_inline_min = tmp;
1306 } else if (strcmp(MLX5_TXQ_INLINE_MPW, key) == 0) {
1307 config->txq_inline_mpw = tmp;
1308 } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
1309 config->txqs_inline = tmp;
1310 } else if (strcmp(MLX5_TXQS_MAX_VEC, key) == 0) {
1311 DRV_LOG(WARNING, "%s: deprecated parameter, ignored", key);
1312 } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
1313 config->mps = !!tmp;
1314 } else if (strcmp(MLX5_TX_DB_NC, key) == 0) {
1315 if (tmp != MLX5_TXDB_CACHED &&
1316 tmp != MLX5_TXDB_NCACHED &&
1317 tmp != MLX5_TXDB_HEURISTIC) {
1318 DRV_LOG(ERR, "invalid Tx doorbell "
1319 "mapping parameter");
1324 } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
1325 DRV_LOG(WARNING, "%s: deprecated parameter, ignored", key);
1326 } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
1327 DRV_LOG(WARNING, "%s: deprecated parameter,"
1328 " converted to txq_inline_mpw", key);
1329 config->txq_inline_mpw = tmp;
1330 } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
1331 DRV_LOG(WARNING, "%s: deprecated parameter, ignored", key);
1332 } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
1333 config->rx_vec_en = !!tmp;
1334 } else if (strcmp(MLX5_L3_VXLAN_EN, key) == 0) {
1335 config->l3_vxlan_en = !!tmp;
1336 } else if (strcmp(MLX5_VF_NL_EN, key) == 0) {
1337 config->vf_nl_en = !!tmp;
1338 } else if (strcmp(MLX5_DV_ESW_EN, key) == 0) {
1339 config->dv_esw_en = !!tmp;
1340 } else if (strcmp(MLX5_DV_FLOW_EN, key) == 0) {
1341 config->dv_flow_en = !!tmp;
1342 } else if (strcmp(MLX5_DV_XMETA_EN, key) == 0) {
1343 if (tmp != MLX5_XMETA_MODE_LEGACY &&
1344 tmp != MLX5_XMETA_MODE_META16 &&
1345 tmp != MLX5_XMETA_MODE_META32) {
1346 DRV_LOG(ERR, "invalid extensive "
1347 "metadata parameter");
1351 config->dv_xmeta_en = tmp;
1352 } else if (strcmp(MLX5_MR_EXT_MEMSEG_EN, key) == 0) {
1353 config->mr_ext_memseg_en = !!tmp;
1354 } else if (strcmp(MLX5_MAX_DUMP_FILES_NUM, key) == 0) {
1355 config->max_dump_files_num = tmp;
1356 } else if (strcmp(MLX5_LRO_TIMEOUT_USEC, key) == 0) {
1357 config->lro.timeout = tmp;
1358 } else if (strcmp(MLX5_CLASS_ARG_NAME, key) == 0) {
1359 DRV_LOG(DEBUG, "class argument is %s.", val);
1360 } else if (strcmp(MLX5_HP_BUF_SIZE, key) == 0) {
1361 config->log_hp_size = tmp;
1362 } else if (strcmp(MLX5_RECLAIM_MEM, key) == 0) {
1363 if (tmp != MLX5_RCM_NONE &&
1364 tmp != MLX5_RCM_LIGHT &&
1365 tmp != MLX5_RCM_AGGR) {
1366 DRV_LOG(ERR, "Unrecognize %s: \"%s\"", key, val);
1370 config->reclaim_mode = tmp;
1372 DRV_LOG(WARNING, "%s: unknown parameter", key);
1380 * Parse device parameters.
1383 * Pointer to device configuration structure.
1385 * Device arguments structure.
1388 * 0 on success, a negative errno value otherwise and rte_errno is set.
1391 mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
1393 const char **params = (const char *[]){
1394 MLX5_RXQ_CQE_COMP_EN,
1395 MLX5_RXQ_CQE_PAD_EN,
1396 MLX5_RXQ_PKT_PAD_EN,
1398 MLX5_RX_MPRQ_LOG_STRIDE_NUM,
1399 MLX5_RX_MPRQ_LOG_STRIDE_SIZE,
1400 MLX5_RX_MPRQ_MAX_MEMCPY_LEN,
1403 MLX5_TXQ_INLINE_MIN,
1404 MLX5_TXQ_INLINE_MAX,
1405 MLX5_TXQ_INLINE_MPW,
1406 MLX5_TXQS_MIN_INLINE,
1409 MLX5_TXQ_MPW_HDR_DSEG_EN,
1410 MLX5_TXQ_MAX_INLINE_LEN,
1419 MLX5_MR_EXT_MEMSEG_EN,
1421 MLX5_MAX_DUMP_FILES_NUM,
1422 MLX5_LRO_TIMEOUT_USEC,
1423 MLX5_CLASS_ARG_NAME,
1428 struct rte_kvargs *kvlist;
1432 if (devargs == NULL)
1434 /* Following UGLY cast is done to pass checkpatch. */
1435 kvlist = rte_kvargs_parse(devargs->args, params);
1436 if (kvlist == NULL) {
1440 /* Process parameters. */
1441 for (i = 0; (params[i] != NULL); ++i) {
1442 if (rte_kvargs_count(kvlist, params[i])) {
1443 ret = rte_kvargs_process(kvlist, params[i],
1444 mlx5_args_check, config);
1447 rte_kvargs_free(kvlist);
1452 rte_kvargs_free(kvlist);
1457 * PMD global initialization.
1459 * Independent from individual device, this function initializes global
1460 * per-PMD data structures distinguishing primary and secondary processes.
1461 * Hence, each initialization is called once per a process.
1464 * 0 on success, a negative errno value otherwise and rte_errno is set.
1467 mlx5_init_once(void)
1469 struct mlx5_shared_data *sd;
1470 struct mlx5_local_data *ld = &mlx5_local_data;
1473 if (mlx5_init_shared_data())
1475 sd = mlx5_shared_data;
1477 rte_spinlock_lock(&sd->lock);
1478 switch (rte_eal_process_type()) {
1479 case RTE_PROC_PRIMARY:
1482 LIST_INIT(&sd->mem_event_cb_list);
1483 rte_rwlock_init(&sd->mem_event_rwlock);
1484 rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
1485 mlx5_mr_mem_event_cb, NULL);
1486 ret = mlx5_mp_init_primary(MLX5_MP_NAME,
1487 mlx5_mp_primary_handle);
1490 sd->init_done = true;
1492 case RTE_PROC_SECONDARY:
1495 ret = mlx5_mp_init_secondary(MLX5_MP_NAME,
1496 mlx5_mp_secondary_handle);
1499 ++sd->secondary_cnt;
1500 ld->init_done = true;
1506 rte_spinlock_unlock(&sd->lock);
1511 * Configures the minimal amount of data to inline into WQE
1512 * while sending packets.
1514 * - the txq_inline_min has the maximal priority, if this
1515 * key is specified in devargs
1516 * - if DevX is enabled the inline mode is queried from the
1517 * device (HCA attributes and NIC vport context if needed).
1518 * - otherwise L2 mode (18 bytes) is assumed for ConnectX-4/4 Lx
1519 * and none (0 bytes) for other NICs
1522 * Verbs device parameters (name, port, switch_info) to spawn.
1524 * Device configuration parameters.
1527 mlx5_set_min_inline(struct mlx5_dev_spawn_data *spawn,
1528 struct mlx5_dev_config *config)
1530 if (config->txq_inline_min != MLX5_ARG_UNSET) {
1531 /* Application defines size of inlined data explicitly. */
1532 switch (spawn->pci_dev->id.device_id) {
1533 case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
1534 case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1535 if (config->txq_inline_min <
1536 (int)MLX5_INLINE_HSIZE_L2) {
1538 "txq_inline_mix aligned to minimal"
1539 " ConnectX-4 required value %d",
1540 (int)MLX5_INLINE_HSIZE_L2);
1541 config->txq_inline_min = MLX5_INLINE_HSIZE_L2;
1547 if (config->hca_attr.eth_net_offloads) {
1548 /* We have DevX enabled, inline mode queried successfully. */
1549 switch (config->hca_attr.wqe_inline_mode) {
1550 case MLX5_CAP_INLINE_MODE_L2:
1551 /* outer L2 header must be inlined. */
1552 config->txq_inline_min = MLX5_INLINE_HSIZE_L2;
1554 case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
1555 /* No inline data are required by NIC. */
1556 config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
1557 config->hw_vlan_insert =
1558 config->hca_attr.wqe_vlan_insert;
1559 DRV_LOG(DEBUG, "Tx VLAN insertion is supported");
1561 case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
1562 /* inline mode is defined by NIC vport context. */
1563 if (!config->hca_attr.eth_virt)
1565 switch (config->hca_attr.vport_inline_mode) {
1566 case MLX5_INLINE_MODE_NONE:
1567 config->txq_inline_min =
1568 MLX5_INLINE_HSIZE_NONE;
1570 case MLX5_INLINE_MODE_L2:
1571 config->txq_inline_min =
1572 MLX5_INLINE_HSIZE_L2;
1574 case MLX5_INLINE_MODE_IP:
1575 config->txq_inline_min =
1576 MLX5_INLINE_HSIZE_L3;
1578 case MLX5_INLINE_MODE_TCP_UDP:
1579 config->txq_inline_min =
1580 MLX5_INLINE_HSIZE_L4;
1582 case MLX5_INLINE_MODE_INNER_L2:
1583 config->txq_inline_min =
1584 MLX5_INLINE_HSIZE_INNER_L2;
1586 case MLX5_INLINE_MODE_INNER_IP:
1587 config->txq_inline_min =
1588 MLX5_INLINE_HSIZE_INNER_L3;
1590 case MLX5_INLINE_MODE_INNER_TCP_UDP:
1591 config->txq_inline_min =
1592 MLX5_INLINE_HSIZE_INNER_L4;
1598 * We get here if we are unable to deduce
1599 * inline data size with DevX. Try PCI ID
1600 * to determine old NICs.
1602 switch (spawn->pci_dev->id.device_id) {
1603 case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
1604 case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1605 case PCI_DEVICE_ID_MELLANOX_CONNECTX4LX:
1606 case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1607 config->txq_inline_min = MLX5_INLINE_HSIZE_L2;
1608 config->hw_vlan_insert = 0;
1610 case PCI_DEVICE_ID_MELLANOX_CONNECTX5:
1611 case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1612 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EX:
1613 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1615 * These NICs support VLAN insertion from WQE and
1616 * report the wqe_vlan_insert flag. But there is the bug
1617 * and PFC control may be broken, so disable feature.
1619 config->hw_vlan_insert = 0;
1620 config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
1623 config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
1627 DRV_LOG(DEBUG, "min tx inline configured: %d", config->txq_inline_min);
1631 * Configures the metadata mask fields in the shared context.
1634 * Pointer to Ethernet device.
1637 mlx5_set_metadata_mask(struct rte_eth_dev *dev)
1639 struct mlx5_priv *priv = dev->data->dev_private;
1640 struct mlx5_dev_ctx_shared *sh = priv->sh;
1641 uint32_t meta, mark, reg_c0;
1643 reg_c0 = ~priv->vport_meta_mask;
1644 switch (priv->config.dv_xmeta_en) {
1645 case MLX5_XMETA_MODE_LEGACY:
1647 mark = MLX5_FLOW_MARK_MASK;
1649 case MLX5_XMETA_MODE_META16:
1650 meta = reg_c0 >> rte_bsf32(reg_c0);
1651 mark = MLX5_FLOW_MARK_MASK;
1653 case MLX5_XMETA_MODE_META32:
1655 mark = (reg_c0 >> rte_bsf32(reg_c0)) & MLX5_FLOW_MARK_MASK;
1663 if (sh->dv_mark_mask && sh->dv_mark_mask != mark)
1664 DRV_LOG(WARNING, "metadata MARK mask mismatche %08X:%08X",
1665 sh->dv_mark_mask, mark);
1667 sh->dv_mark_mask = mark;
1668 if (sh->dv_meta_mask && sh->dv_meta_mask != meta)
1669 DRV_LOG(WARNING, "metadata META mask mismatche %08X:%08X",
1670 sh->dv_meta_mask, meta);
1672 sh->dv_meta_mask = meta;
1673 if (sh->dv_regc0_mask && sh->dv_regc0_mask != reg_c0)
1674 DRV_LOG(WARNING, "metadata reg_c0 mask mismatche %08X:%08X",
1675 sh->dv_meta_mask, reg_c0);
1677 sh->dv_regc0_mask = reg_c0;
1678 DRV_LOG(DEBUG, "metadata mode %u", priv->config.dv_xmeta_en);
1679 DRV_LOG(DEBUG, "metadata MARK mask %08X", sh->dv_mark_mask);
1680 DRV_LOG(DEBUG, "metadata META mask %08X", sh->dv_meta_mask);
1681 DRV_LOG(DEBUG, "metadata reg_c0 mask %08X", sh->dv_regc0_mask);
1685 * Allocate page of door-bells and register it using DevX API.
1688 * Pointer to Ethernet device.
1691 * Pointer to new page on success, NULL otherwise.
1693 static struct mlx5_devx_dbr_page *
1694 mlx5_alloc_dbr_page(struct rte_eth_dev *dev)
1696 struct mlx5_priv *priv = dev->data->dev_private;
1697 struct mlx5_devx_dbr_page *page;
1699 /* Allocate space for door-bell page and management data. */
1700 page = rte_calloc_socket(__func__, 1, sizeof(struct mlx5_devx_dbr_page),
1701 RTE_CACHE_LINE_SIZE, dev->device->numa_node);
1703 DRV_LOG(ERR, "port %u cannot allocate dbr page",
1704 dev->data->port_id);
1707 /* Register allocated memory. */
1708 page->umem = mlx5_glue->devx_umem_reg(priv->sh->ctx, page->dbrs,
1709 MLX5_DBR_PAGE_SIZE, 0);
1711 DRV_LOG(ERR, "port %u cannot umem reg dbr page",
1712 dev->data->port_id);
1720 * Find the next available door-bell, allocate new page if needed.
1723 * Pointer to Ethernet device.
1724 * @param [out] dbr_page
1725 * Door-bell page containing the page data.
1728 * Door-bell address offset on success, a negative error value otherwise.
1731 mlx5_get_dbr(struct rte_eth_dev *dev, struct mlx5_devx_dbr_page **dbr_page)
1733 struct mlx5_priv *priv = dev->data->dev_private;
1734 struct mlx5_devx_dbr_page *page = NULL;
1737 LIST_FOREACH(page, &priv->dbrpgs, next)
1738 if (page->dbr_count < MLX5_DBR_PER_PAGE)
1740 if (!page) { /* No page with free door-bell exists. */
1741 page = mlx5_alloc_dbr_page(dev);
1742 if (!page) /* Failed to allocate new page. */
1744 LIST_INSERT_HEAD(&priv->dbrpgs, page, next);
1746 /* Loop to find bitmap part with clear bit. */
1748 i < MLX5_DBR_BITMAP_SIZE && page->dbr_bitmap[i] == UINT64_MAX;
1751 /* Find the first clear bit. */
1752 MLX5_ASSERT(i < MLX5_DBR_BITMAP_SIZE);
1753 j = rte_bsf64(~page->dbr_bitmap[i]);
1754 page->dbr_bitmap[i] |= (UINT64_C(1) << j);
1757 return (((i * 64) + j) * sizeof(uint64_t));
1761 * Release a door-bell record.
1764 * Pointer to Ethernet device.
1765 * @param [in] umem_id
1766 * UMEM ID of page containing the door-bell record to release.
1767 * @param [in] offset
1768 * Offset of door-bell record in page.
1771 * 0 on success, a negative error value otherwise.
1774 mlx5_release_dbr(struct rte_eth_dev *dev, uint32_t umem_id, uint64_t offset)
1776 struct mlx5_priv *priv = dev->data->dev_private;
1777 struct mlx5_devx_dbr_page *page = NULL;
1780 LIST_FOREACH(page, &priv->dbrpgs, next)
1781 /* Find the page this address belongs to. */
1782 if (mlx5_os_get_umem_id(page->umem) == umem_id)
1787 if (!page->dbr_count) {
1788 /* Page not used, free it and remove from list. */
1789 LIST_REMOVE(page, next);
1791 ret = -mlx5_glue->devx_umem_dereg(page->umem);
1794 /* Mark in bitmap that this door-bell is not in use. */
1795 offset /= MLX5_DBR_SIZE;
1796 int i = offset / 64;
1797 int j = offset % 64;
1799 page->dbr_bitmap[i] &= ~(UINT64_C(1) << j);
1805 rte_pmd_mlx5_get_dyn_flag_names(char *names[], unsigned int n)
1807 static const char *const dynf_names[] = {
1808 RTE_PMD_MLX5_FINE_GRANULARITY_INLINE,
1809 RTE_MBUF_DYNFLAG_METADATA_NAME
1813 if (n < RTE_DIM(dynf_names))
1815 for (i = 0; i < RTE_DIM(dynf_names); i++) {
1816 if (names[i] == NULL)
1818 strcpy(names[i], dynf_names[i]);
1820 return RTE_DIM(dynf_names);
1824 * Comparison callback to sort device data.
1826 * This is meant to be used with qsort().
1829 * Pointer to pointer to first data object.
1831 * Pointer to pointer to second data object.
1834 * 0 if both objects are equal, less than 0 if the first argument is less
1835 * than the second, greater than 0 otherwise.
1838 mlx5_dev_check_sibling_config(struct mlx5_priv *priv,
1839 struct mlx5_dev_config *config)
1841 struct mlx5_dev_ctx_shared *sh = priv->sh;
1842 struct mlx5_dev_config *sh_conf = NULL;
1846 /* Nothing to compare for the single/first device. */
1847 if (sh->refcnt == 1)
1849 /* Find the device with shared context. */
1850 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
1851 struct mlx5_priv *opriv =
1852 rte_eth_devices[port_id].data->dev_private;
1854 if (opriv && opriv != priv && opriv->sh == sh) {
1855 sh_conf = &opriv->config;
1861 if (sh_conf->dv_flow_en ^ config->dv_flow_en) {
1862 DRV_LOG(ERR, "\"dv_flow_en\" configuration mismatch"
1863 " for shared %s context", sh->ibdev_name);
1867 if (sh_conf->dv_xmeta_en ^ config->dv_xmeta_en) {
1868 DRV_LOG(ERR, "\"dv_xmeta_en\" configuration mismatch"
1869 " for shared %s context", sh->ibdev_name);
1877 * Look for the ethernet device belonging to mlx5 driver.
1879 * @param[in] port_id
1880 * port_id to start looking for device.
1881 * @param[in] pci_dev
1882 * Pointer to the hint PCI device. When device is being probed
1883 * the its siblings (master and preceding representors might
1884 * not have assigned driver yet (because the mlx5_os_pci_probe()
1885 * is not completed yet, for this case match on hint PCI
1886 * device may be used to detect sibling device.
1889 * port_id of found device, RTE_MAX_ETHPORT if not found.
1892 mlx5_eth_find_next(uint16_t port_id, struct rte_pci_device *pci_dev)
1894 while (port_id < RTE_MAX_ETHPORTS) {
1895 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1897 if (dev->state != RTE_ETH_DEV_UNUSED &&
1899 (dev->device == &pci_dev->device ||
1900 (dev->device->driver &&
1901 dev->device->driver->name &&
1902 !strcmp(dev->device->driver->name, MLX5_DRIVER_NAME))))
1906 if (port_id >= RTE_MAX_ETHPORTS)
1907 return RTE_MAX_ETHPORTS;
1912 * DPDK callback to remove a PCI device.
1914 * This function removes all Ethernet devices belong to a given PCI device.
1916 * @param[in] pci_dev
1917 * Pointer to the PCI device.
1920 * 0 on success, the function cannot fail.
1923 mlx5_pci_remove(struct rte_pci_device *pci_dev)
1927 RTE_ETH_FOREACH_DEV_OF(port_id, &pci_dev->device) {
1929 * mlx5_dev_close() is not registered to secondary process,
1930 * call the close function explicitly for secondary process.
1932 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1933 mlx5_dev_close(&rte_eth_devices[port_id]);
1935 rte_eth_dev_close(port_id);
1940 static const struct rte_pci_id mlx5_pci_id_map[] = {
1942 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1943 PCI_DEVICE_ID_MELLANOX_CONNECTX4)
1946 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1947 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
1950 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1951 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
1954 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1955 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
1958 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1959 PCI_DEVICE_ID_MELLANOX_CONNECTX5)
1962 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1963 PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
1966 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1967 PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
1970 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1971 PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
1974 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1975 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
1978 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1979 PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
1982 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1983 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
1986 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1987 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
1990 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1991 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
1994 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1995 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
1998 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1999 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF)
2006 struct rte_pci_driver mlx5_driver = {
2008 .name = MLX5_DRIVER_NAME
2010 .id_table = mlx5_pci_id_map,
2011 .probe = mlx5_os_pci_probe,
2012 .remove = mlx5_pci_remove,
2013 .dma_map = mlx5_dma_map,
2014 .dma_unmap = mlx5_dma_unmap,
2015 .drv_flags = PCI_DRV_FLAGS,
2019 * Driver initialization routine.
2021 RTE_INIT(rte_mlx5_pmd_init)
2023 /* Initialize driver log type. */
2024 mlx5_logtype = rte_log_register("pmd.net.mlx5");
2025 if (mlx5_logtype >= 0)
2026 rte_log_set_level(mlx5_logtype, RTE_LOG_NOTICE);
2028 /* Build the static tables for Verbs conversion. */
2029 mlx5_set_ptype_table();
2030 mlx5_set_cksum_table();
2031 mlx5_set_swp_types_table();
2033 rte_pci_register(&mlx5_driver);
2036 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
2037 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
2038 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");