xref: /dpdk/drivers/common/mlx5/linux/mlx5_common_verbs.c (revision 10b71caecbe1cddcbb65c050ca775fba575e88db)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4 
5 #include <stddef.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <unistd.h>
10 #include <sys/mman.h>
11 #include <inttypes.h>
12 
13 #include "mlx5_autoconf.h"
14 #include <mlx5_glue.h>
15 #include <mlx5_common.h>
16 #include <mlx5_common_mr.h>
17 
18 /**
19  * Register mr. Given protection domain pointer, pointer to addr and length
20  * register the memory region.
21  *
22  * @param[in] pd
23  *   Pointer to protection domain context.
24  * @param[in] addr
25  *   Pointer to memory start address.
26  * @param[in] length
27  *   Length of the memory to register.
28  * @param[out] pmd_mr
29  *   pmd_mr struct set with lkey, address, length and pointer to mr object
30  *
31  * @return
32  *   0 on successful registration, -1 otherwise
33  */
34 int
35 mlx5_common_verbs_reg_mr(void *pd, void *addr, size_t length,
36 			 struct mlx5_pmd_mr *pmd_mr)
37 {
38 	struct ibv_mr *ibv_mr;
39 
40 	memset(pmd_mr, 0, sizeof(*pmd_mr));
41 	ibv_mr = mlx5_glue->reg_mr(pd, addr, length,
42 				   IBV_ACCESS_LOCAL_WRITE |
43 				   (haswell_broadwell_cpu ? 0 :
44 				   IBV_ACCESS_RELAXED_ORDERING));
45 	if (!ibv_mr)
46 		return -1;
47 
48 	*pmd_mr = (struct mlx5_pmd_mr){
49 		.lkey = ibv_mr->lkey,
50 		.addr = ibv_mr->addr,
51 		.len = ibv_mr->length,
52 		.obj = (void *)ibv_mr,
53 	};
54 	return 0;
55 }
56 
57 /**
58  * Deregister mr. Given the mlx5 pmd MR - deregister the MR
59  *
60  * @param[in] pmd_mr
61  *   pmd_mr struct set with lkey, address, length and pointer to mr object
62  *
63  */
64 void
65 mlx5_common_verbs_dereg_mr(struct mlx5_pmd_mr *pmd_mr)
66 {
67 	if (pmd_mr && pmd_mr->obj != NULL) {
68 		claim_zero(mlx5_glue->dereg_mr(pmd_mr->obj));
69 		memset(pmd_mr, 0, sizeof(*pmd_mr));
70 	}
71 }
72 
73