1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
13 #include <rte_errno.h>
14 #include <rte_eal_paging.h>
16 #include "mlx5_common_utils.h"
17 #include "mlx5_common_log.h"
18 #include "mlx5_autoconf.h"
19 #include <mlx5_glue.h>
20 #include <mlx5_malloc.h>
21 #include <mlx5_common.h>
22 #include <mlx5_common_mr.h>
25 * Verbs callback to allocate a memory. This function should allocate the space
26 * according to the size provided residing inside a huge page.
27 * Please note that all allocation must respect the alignment from libmlx5
28 * (i.e. currently rte_mem_page_size()).
31 * The size in bytes of the memory to allocate.
33 * A pointer to the callback data.
36 * Allocated buffer, NULL otherwise and rte_errno is set.
39 mlx5_alloc_verbs_buf(size_t size, void *data)
41 struct rte_device *dev = data;
43 size_t alignment = rte_mem_page_size();
44 if (alignment == (size_t)-1) {
45 DRV_LOG(ERR, "Failed to get mem page size");
50 MLX5_ASSERT(data != NULL);
51 ret = mlx5_malloc(0, size, alignment, dev->numa_node);
58 * Verbs callback to free a memory.
61 * A pointer to the memory to free.
63 * A pointer to the callback data.
66 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
68 MLX5_ASSERT(data != NULL);
73 * Hint libmlx5 to use PMD allocator for data plane resources.
76 * Pointer to the generic device.
79 mlx5_set_context_attr(struct rte_device *dev, struct ibv_context *ctx)
81 struct mlx5dv_ctx_allocators allocator = {
82 .alloc = &mlx5_alloc_verbs_buf,
83 .free = &mlx5_free_verbs_buf,
87 /* Hint libmlx5 to use PMD allocator for data plane resources */
88 mlx5_glue->dv_set_context_attr(ctx, MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
89 (void *)((uintptr_t)&allocator));
93 * Register mr. Given protection domain pointer, pointer to addr and length
94 * register the memory region.
97 * Pointer to protection domain context.
99 * Pointer to memory start address.
101 * Length of the memory to register.
103 * pmd_mr struct set with lkey, address, length and pointer to mr object
106 * 0 on successful registration, -1 otherwise
109 mlx5_common_verbs_reg_mr(void *pd, void *addr, size_t length,
110 struct mlx5_pmd_mr *pmd_mr)
112 struct ibv_mr *ibv_mr;
114 ibv_mr = mlx5_glue->reg_mr(pd, addr, length,
115 IBV_ACCESS_LOCAL_WRITE |
116 (haswell_broadwell_cpu ? 0 :
117 IBV_ACCESS_RELAXED_ORDERING));
121 *pmd_mr = (struct mlx5_pmd_mr){
122 .lkey = ibv_mr->lkey,
123 .addr = ibv_mr->addr,
124 .len = ibv_mr->length,
125 .obj = (void *)ibv_mr,
131 * Deregister mr. Given the mlx5 pmd MR - deregister the MR
134 * pmd_mr struct set with lkey, address, length and pointer to mr object
138 mlx5_common_verbs_dereg_mr(struct mlx5_pmd_mr *pmd_mr)
140 if (pmd_mr && pmd_mr->obj != NULL) {
141 claim_zero(mlx5_glue->dereg_mr(pmd_mr->obj));
142 memset(pmd_mr, 0, sizeof(*pmd_mr));