mempool: use SPDX tags
[dpdk.git] / lib / librte_mempool / rte_mempool_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation.
3  * Copyright(c) 2016 6WIND S.A.
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <rte_mempool.h>
10 #include <rte_errno.h>
11 #include <rte_dev.h>
12
13 /* indirect jump table to support external memory pools. */
14 struct rte_mempool_ops_table rte_mempool_ops_table = {
15         .sl =  RTE_SPINLOCK_INITIALIZER,
16         .num_ops = 0
17 };
18
19 /* add a new ops struct in rte_mempool_ops_table, return its index. */
20 int
21 rte_mempool_register_ops(const struct rte_mempool_ops *h)
22 {
23         struct rte_mempool_ops *ops;
24         int16_t ops_index;
25
26         rte_spinlock_lock(&rte_mempool_ops_table.sl);
27
28         if (rte_mempool_ops_table.num_ops >=
29                         RTE_MEMPOOL_MAX_OPS_IDX) {
30                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
31                 RTE_LOG(ERR, MEMPOOL,
32                         "Maximum number of mempool ops structs exceeded\n");
33                 return -ENOSPC;
34         }
35
36         if (h->alloc == NULL || h->enqueue == NULL ||
37                         h->dequeue == NULL || h->get_count == NULL) {
38                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
39                 RTE_LOG(ERR, MEMPOOL,
40                         "Missing callback while registering mempool ops\n");
41                 return -EINVAL;
42         }
43
44         if (strlen(h->name) >= sizeof(ops->name) - 1) {
45                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
46                 RTE_LOG(DEBUG, EAL, "%s(): mempool_ops <%s>: name too long\n",
47                                 __func__, h->name);
48                 rte_errno = EEXIST;
49                 return -EEXIST;
50         }
51
52         ops_index = rte_mempool_ops_table.num_ops++;
53         ops = &rte_mempool_ops_table.ops[ops_index];
54         snprintf(ops->name, sizeof(ops->name), "%s", h->name);
55         ops->alloc = h->alloc;
56         ops->free = h->free;
57         ops->enqueue = h->enqueue;
58         ops->dequeue = h->dequeue;
59         ops->get_count = h->get_count;
60         ops->get_capabilities = h->get_capabilities;
61         ops->register_memory_area = h->register_memory_area;
62
63         rte_spinlock_unlock(&rte_mempool_ops_table.sl);
64
65         return ops_index;
66 }
67
68 /* wrapper to allocate an external mempool's private (pool) data. */
69 int
70 rte_mempool_ops_alloc(struct rte_mempool *mp)
71 {
72         struct rte_mempool_ops *ops;
73
74         ops = rte_mempool_get_ops(mp->ops_index);
75         return ops->alloc(mp);
76 }
77
78 /* wrapper to free an external pool ops. */
79 void
80 rte_mempool_ops_free(struct rte_mempool *mp)
81 {
82         struct rte_mempool_ops *ops;
83
84         ops = rte_mempool_get_ops(mp->ops_index);
85         if (ops->free == NULL)
86                 return;
87         ops->free(mp);
88 }
89
90 /* wrapper to get available objects in an external mempool. */
91 unsigned int
92 rte_mempool_ops_get_count(const struct rte_mempool *mp)
93 {
94         struct rte_mempool_ops *ops;
95
96         ops = rte_mempool_get_ops(mp->ops_index);
97         return ops->get_count(mp);
98 }
99
100 /* wrapper to get external mempool capabilities. */
101 int
102 rte_mempool_ops_get_capabilities(const struct rte_mempool *mp,
103                                         unsigned int *flags)
104 {
105         struct rte_mempool_ops *ops;
106
107         ops = rte_mempool_get_ops(mp->ops_index);
108
109         RTE_FUNC_PTR_OR_ERR_RET(ops->get_capabilities, -ENOTSUP);
110         return ops->get_capabilities(mp, flags);
111 }
112
113 /* wrapper to notify new memory area to external mempool */
114 int
115 rte_mempool_ops_register_memory_area(const struct rte_mempool *mp, char *vaddr,
116                                         rte_iova_t iova, size_t len)
117 {
118         struct rte_mempool_ops *ops;
119
120         ops = rte_mempool_get_ops(mp->ops_index);
121
122         RTE_FUNC_PTR_OR_ERR_RET(ops->register_memory_area, -ENOTSUP);
123         return ops->register_memory_area(mp, vaddr, iova, len);
124 }
125
126 /* sets mempool ops previously registered by rte_mempool_register_ops. */
127 int
128 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
129         void *pool_config)
130 {
131         struct rte_mempool_ops *ops = NULL;
132         unsigned i;
133
134         /* too late, the mempool is already populated. */
135         if (mp->flags & MEMPOOL_F_POOL_CREATED)
136                 return -EEXIST;
137
138         for (i = 0; i < rte_mempool_ops_table.num_ops; i++) {
139                 if (!strcmp(name,
140                                 rte_mempool_ops_table.ops[i].name)) {
141                         ops = &rte_mempool_ops_table.ops[i];
142                         break;
143                 }
144         }
145
146         if (ops == NULL)
147                 return -EINVAL;
148
149         mp->ops_index = i;
150         mp->pool_config = pool_config;
151         return 0;
152 }