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