common/mlx5: add alloc/dealloc PD 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 }