common/mlx5: add UMEM reg/dereg functions on Windows
[dpdk.git] / drivers / common / mlx5 / windows / mlx5_common_os.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdio.h>
8
9 #include <rte_mempool.h>
10 #include <rte_malloc.h>
11 #include <rte_errno.h>
12
13 #include "mlx5_devx_cmds.h"
14 #include "mlx5_common_utils.h"
15 #include "mlx5_common.h"
16
17 /**
18  * Initialization routine for run-time dependency on external lib
19  */
20 void
21 mlx5_glue_constructor(void)
22 {
23 }
24
25 /**
26  * Allocate PD. Given a devx context object
27  * return an mlx5-pd object.
28  *
29  * @param[in] ctx
30  *   Pointer to context.
31  *
32  * @return
33  *    The mlx5_pd if pd is valid, NULL and errno otherwise.
34  */
35 void *
36 mlx5_os_alloc_pd(void *ctx)
37 {
38         struct mlx5_pd *ppd =  mlx5_malloc(MLX5_MEM_ZERO,
39                 sizeof(struct mlx5_pd), 0, SOCKET_ID_ANY);
40         if (!ppd)
41                 return NULL;
42
43         struct mlx5_devx_obj *obj = mlx5_devx_cmd_alloc_pd(ctx);
44         if (!obj) {
45                 mlx5_free(ppd);
46                 return NULL;
47         }
48         ppd->obj = obj;
49         ppd->pdn = obj->id;
50         ppd->devx_ctx = ctx;
51         return ppd;
52 }
53
54 /**
55  * Release PD. Releases a given mlx5_pd object
56  *
57  * @param[in] pd
58  *   Pointer to mlx5_pd.
59  *
60  * @return
61  *    Zero if pd is released successfully, negative number otherwise.
62  */
63 int
64 mlx5_os_dealloc_pd(void *pd)
65 {
66         if (!pd)
67                 return -EINVAL;
68         mlx5_devx_cmd_destroy(((struct mlx5_pd *)pd)->obj);
69         mlx5_free(pd);
70         return 0;
71 }
72
73 /**
74  * Register umem.
75  *
76  * @param[in] ctx
77  *   Pointer to context.
78  * @param[in] addr
79  *   Pointer to memory start address.
80  * @param[in] size
81  *   Size of the memory to register.
82  * @param[out] access
83  *   UMEM access type
84  *
85  * @return
86  *   umem on successful registration, NULL and errno otherwise
87  */
88 void *
89 mlx5_os_umem_reg(void *ctx, void *addr, size_t size, uint32_t access)
90 {
91         struct mlx5_devx_umem *umem;
92
93         umem = mlx5_malloc(MLX5_MEM_ZERO,
94                 (sizeof(*umem)), 0, SOCKET_ID_ANY);
95         if (!umem) {
96                 errno = ENOMEM;
97                 return NULL;
98         }
99         umem->umem_hdl = mlx5_glue->devx_umem_reg(ctx, addr, size, access,
100                 &umem->umem_id);
101         if (!umem->umem_hdl) {
102                 mlx5_free(umem);
103                 return NULL;
104         }
105         umem->addr = addr;
106         return umem;
107 }
108
109 /**
110  * Deregister umem.
111  *
112  * @param[in] pumem
113  *   Pointer to umem.
114  *
115  * @return
116  *   0 on successful release, negative number otherwise
117  */
118 int
119 mlx5_os_umem_dereg(void *pumem)
120 {
121         struct mlx5_devx_umem *umem;
122         int err = 0;
123
124         if (!pumem)
125                 return err;
126         umem = pumem;
127         if (umem->umem_hdl)
128                 err = mlx5_glue->devx_umem_dereg(umem->umem_hdl);
129         mlx5_free(umem);
130         return err;
131 }