xref: /dpdk/drivers/common/mlx5/linux/mlx5_common_verbs.c (revision ad435d3204736e8110cd3ecdf5363ae20f68e66f)
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 <rte_errno.h>
14 #include <rte_bus_pci.h>
15 
16 #include "mlx5_common_utils.h"
17 #include "mlx5_common_log.h"
18 #include "mlx5_common_private.h"
19 #include "mlx5_autoconf.h"
20 #include <mlx5_glue.h>
21 #include <mlx5_common.h>
22 #include <mlx5_common_mr.h>
23 
24 struct ibv_device *
25 mlx5_os_get_ibv_dev(const struct rte_device *dev)
26 {
27 	struct ibv_device *ibv = NULL;
28 
29 	if (mlx5_dev_is_pci(dev))
30 		ibv = mlx5_os_get_ibv_device(&RTE_DEV_TO_PCI_CONST(dev)->addr);
31 	if (ibv == NULL) {
32 		rte_errno = ENODEV;
33 		DRV_LOG(ERR, "Verbs device not found: %s", dev->name);
34 	}
35 	return ibv;
36 }
37 
38 /**
39  * Register mr. Given protection domain pointer, pointer to addr and length
40  * register the memory region.
41  *
42  * @param[in] pd
43  *   Pointer to protection domain context.
44  * @param[in] addr
45  *   Pointer to memory start address.
46  * @param[in] length
47  *   Length of the memory to register.
48  * @param[out] pmd_mr
49  *   pmd_mr struct set with lkey, address, length and pointer to mr object
50  *
51  * @return
52  *   0 on successful registration, -1 otherwise
53  */
54 int
55 mlx5_common_verbs_reg_mr(void *pd, void *addr, size_t length,
56 			 struct mlx5_pmd_mr *pmd_mr)
57 {
58 	struct ibv_mr *ibv_mr;
59 
60 	ibv_mr = mlx5_glue->reg_mr(pd, addr, length,
61 				   IBV_ACCESS_LOCAL_WRITE |
62 				   (haswell_broadwell_cpu ? 0 :
63 				   IBV_ACCESS_RELAXED_ORDERING));
64 	if (!ibv_mr)
65 		return -1;
66 
67 	*pmd_mr = (struct mlx5_pmd_mr){
68 		.lkey = ibv_mr->lkey,
69 		.addr = ibv_mr->addr,
70 		.len = ibv_mr->length,
71 		.obj = (void *)ibv_mr,
72 	};
73 	return 0;
74 }
75 
76 /**
77  * Deregister mr. Given the mlx5 pmd MR - deregister the MR
78  *
79  * @param[in] pmd_mr
80  *   pmd_mr struct set with lkey, address, length and pointer to mr object
81  *
82  */
83 void
84 mlx5_common_verbs_dereg_mr(struct mlx5_pmd_mr *pmd_mr)
85 {
86 	if (pmd_mr && pmd_mr->obj != NULL) {
87 		claim_zero(mlx5_glue->dereg_mr(pmd_mr->obj));
88 		memset(pmd_mr, 0, sizeof(*pmd_mr));
89 	}
90 }
91