From 58a17853b5991092da557538d5acc7bc365ba12e Mon Sep 17 00:00:00 2001 From: Ophir Munk Date: Tue, 16 Jun 2020 09:44:44 +0000 Subject: [PATCH] common/mlx5: export memory region Verbs operations The glue verbs operations reg_mr and dereg_mr are wrapped and exported in functions mlx5_common_verbs_reg_mr and mlx5_common_verbs_dereg_mr respectively. The exported functions are added to a new file linux/mlx5_common_verbs.c. Signed-off-by: Ophir Munk Acked-by: Matan Azrad --- drivers/common/mlx5/Makefile | 1 + drivers/common/mlx5/linux/meson.build | 1 + drivers/common/mlx5/linux/mlx5_common_verbs.c | 87 +++++++++++++++++++ drivers/common/mlx5/mlx5_common_mr.c | 27 ++---- drivers/common/mlx5/mlx5_common_mr.h | 8 +- .../common/mlx5/rte_common_mlx5_version.map | 3 + 6 files changed, 104 insertions(+), 23 deletions(-) create mode 100644 drivers/common/mlx5/linux/mlx5_common_verbs.c diff --git a/drivers/common/mlx5/Makefile b/drivers/common/mlx5/Makefile index 4948fef647..622bde4ff4 100644 --- a/drivers/common/mlx5/Makefile +++ b/drivers/common/mlx5/Makefile @@ -18,6 +18,7 @@ SRCS-y += mlx5_devx_cmds.c SRCS-y += mlx5_common.c SRCS-y += linux/mlx5_common_os.c SRCS-y += linux/mlx5_nl.c +SRCS-y += linux/mlx5_common_verbs.c SRCS-y += mlx5_common_mp.c SRCS-y += mlx5_common_mr.c ifeq ($(CONFIG_RTE_IBVERBS_LINK_DLOPEN),y) diff --git a/drivers/common/mlx5/linux/meson.build b/drivers/common/mlx5/linux/meson.build index ce065d9346..638bb2b855 100644 --- a/drivers/common/mlx5/linux/meson.build +++ b/drivers/common/mlx5/linux/meson.build @@ -47,6 +47,7 @@ endif sources += files('mlx5_nl.c') sources += files('mlx5_common_os.c') +sources += files('mlx5_common_verbs.c') if not dlopen_ibverbs sources += files('mlx5_glue.c') endif diff --git a/drivers/common/mlx5/linux/mlx5_common_verbs.c b/drivers/common/mlx5/linux/mlx5_common_verbs.c new file mode 100644 index 0000000000..a2fc7a36bd --- /dev/null +++ b/drivers/common/mlx5/linux/mlx5_common_verbs.c @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2020 Mellanox Technologies, Ltd + */ + +#include +#include +#include +#include +#include +#include +#include + +/* Verbs header. */ +/* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */ +#include "mlx5_autoconf.h" +#ifdef PEDANTIC +#pragma GCC diagnostic ignored "-Wpedantic" +#endif +#ifdef HAVE_INFINIBAND_VERBS_H +#include +#endif +#ifdef HAVE_INFINIBAND_MLX5DV_H +#include +#endif +#ifdef PEDANTIC +#pragma GCC diagnostic error "-Wpedantic" +#endif + +#include +#include +#include + +/** + * Register mr. Given protection domain pointer, pointer to addr and length + * register the memory region. + * + * @param[in] pd + * Pointer to protection domain context. + * @param[in] addr + * Pointer to memory start address. + * @param[in] length + * Length of the memory to register. + * @param[out] pmd_mr + * pmd_mr struct set with lkey, address, length and pointer to mr object + * + * @return + * 0 on successful registration, -1 otherwise + */ +int +mlx5_common_verbs_reg_mr(void *pd, void *addr, size_t length, + struct mlx5_pmd_mr *pmd_mr) +{ + struct ibv_mr *ibv_mr; + + memset(pmd_mr, 0, sizeof(*pmd_mr)); + ibv_mr = mlx5_glue->reg_mr(pd, addr, length, + IBV_ACCESS_LOCAL_WRITE | + (haswell_broadwell_cpu ? 0 : + IBV_ACCESS_RELAXED_ORDERING)); + if (!ibv_mr) + return -1; + + *pmd_mr = (struct mlx5_pmd_mr){ + .lkey = ibv_mr->lkey, + .addr = ibv_mr->addr, + .len = ibv_mr->length, + .obj = (void *)ibv_mr, + }; + return 0; +} + +/** + * Deregister mr. Given the mlx5 pmd MR - deregister the MR + * + * @param[in] pmd_mr + * pmd_mr struct set with lkey, address, length and pointer to mr object + * + */ +void +mlx5_common_verbs_dereg_mr(struct mlx5_pmd_mr *pmd_mr) +{ + if (pmd_mr && pmd_mr->obj != NULL) { + claim_zero(mlx5_glue->dereg_mr(pmd_mr->obj)); + memset(pmd_mr, 0, sizeof(*pmd_mr)); + } +} + diff --git a/drivers/common/mlx5/mlx5_common_mr.c b/drivers/common/mlx5/mlx5_common_mr.c index c68be542ae..4ce79753de 100644 --- a/drivers/common/mlx5/mlx5_common_mr.c +++ b/drivers/common/mlx5/mlx5_common_mr.c @@ -440,8 +440,7 @@ mr_free(struct mlx5_mr *mr) if (mr == NULL) return; DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr); - if (mr->pmd_mr.obj != NULL) - claim_zero(mlx5_glue->dereg_mr(mr->pmd_mr.obj)); + mlx5_common_verbs_dereg_mr(&mr->pmd_mr); if (mr->ms_bmp != NULL) rte_bitmap_free(mr->ms_bmp); rte_free(mr); @@ -598,7 +597,6 @@ mlx5_mr_create_primary(void *pd, uint32_t ms_n; uint32_t n; size_t len; - struct ibv_mr *ibv_mr; DRV_LOG(DEBUG, "Creating a MR using address (%p)", (void *)addr); /* @@ -767,20 +765,13 @@ alloc_resources: * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket() * through mlx5_alloc_verbs_buf(). */ - ibv_mr = mlx5_glue->reg_mr(pd, (void *)data.start, len, - IBV_ACCESS_LOCAL_WRITE | - (haswell_broadwell_cpu ? 0 : - IBV_ACCESS_RELAXED_ORDERING)); - if (ibv_mr == NULL) { + mlx5_common_verbs_reg_mr(pd, (void *)data.start, len, &mr->pmd_mr); + if (mr->pmd_mr.obj == NULL) { DEBUG("Fail to create an MR for address (%p)", (void *)addr); rte_errno = EINVAL; goto err_mrlock; } - mr->pmd_mr.lkey = ibv_mr->lkey; - mr->pmd_mr.addr = ibv_mr->addr; - mr->pmd_mr.len = ibv_mr->length; - mr->pmd_mr.obj = ibv_mr; MLX5_ASSERT((uintptr_t)mr->pmd_mr.addr == data.start); MLX5_ASSERT(mr->pmd_mr.len); LIST_INSERT_HEAD(&share_cache->mr_list, mr, mr); @@ -1039,7 +1030,6 @@ mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl) struct mlx5_mr * mlx5_create_mr_ext(void *pd, uintptr_t addr, size_t len, int socket_id) { - struct ibv_mr *ibv_mr; struct mlx5_mr *mr = NULL; mr = rte_zmalloc_socket(NULL, @@ -1048,21 +1038,14 @@ mlx5_create_mr_ext(void *pd, uintptr_t addr, size_t len, int socket_id) RTE_CACHE_LINE_SIZE, socket_id); if (mr == NULL) return NULL; - ibv_mr = mlx5_glue->reg_mr(pd, (void *)addr, len, - IBV_ACCESS_LOCAL_WRITE | - (haswell_broadwell_cpu ? 0 : - IBV_ACCESS_RELAXED_ORDERING)); - if (ibv_mr == NULL) { + mlx5_common_verbs_reg_mr(pd, (void *)addr, len, &mr->pmd_mr); + if (mr->pmd_mr.obj == NULL) { DRV_LOG(WARNING, "Fail to create MR for address (%p)", (void *)addr); rte_free(mr); return NULL; } - mr->pmd_mr.lkey = ibv_mr->lkey; - mr->pmd_mr.addr = ibv_mr->addr; - mr->pmd_mr.len = ibv_mr->length; - mr->pmd_mr.obj = ibv_mr; mr->msl = NULL; /* Mark it is external memory. */ mr->ms_bmp = NULL; mr->ms_n = 1; diff --git a/drivers/common/mlx5/mlx5_common_mr.h b/drivers/common/mlx5/mlx5_common_mr.h index 6f2dcec2a5..cdb8acfab1 100644 --- a/drivers/common/mlx5/mlx5_common_mr.h +++ b/drivers/common/mlx5/mlx5_common_mr.h @@ -163,5 +163,11 @@ mlx5_mr_create_primary(void *pd, struct mlx5_mr_share_cache *share_cache, struct mr_cache_entry *entry, uintptr_t addr, unsigned int mr_ext_memseg_en); - +__rte_internal +int +mlx5_common_verbs_reg_mr(void *pd, void *addr, size_t length, + struct mlx5_pmd_mr *pmd_mr); +__rte_internal +void +mlx5_common_verbs_dereg_mr(struct mlx5_pmd_mr *pmd_mr); #endif /* RTE_PMD_MLX5_COMMON_MR_H_ */ diff --git a/drivers/common/mlx5/rte_common_mlx5_version.map b/drivers/common/mlx5/rte_common_mlx5_version.map index 350e77140c..68f1207125 100644 --- a/drivers/common/mlx5/rte_common_mlx5_version.map +++ b/drivers/common/mlx5/rte_common_mlx5_version.map @@ -3,6 +3,9 @@ INTERNAL { mlx5_class_get; + mlx5_common_verbs_reg_mr; + mlx5_common_verbs_dereg_mr; + mlx5_create_mr_ext; mlx5_dev_to_pci_addr; -- 2.20.1