compress/isal: add stats related ops
[dpdk.git] / drivers / compress / isal / isal_compress_pmd_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <rte_common.h>
6 #include <rte_compressdev_pmd.h>
7 #include <rte_malloc.h>
8
9 #include "isal_compress_pmd_private.h"
10
11 static const struct rte_compressdev_capabilities isal_pmd_capabilities[] = {
12         RTE_COMP_END_OF_CAPABILITIES_LIST()
13 };
14
15 /** Configure device */
16 static int
17 isal_comp_pmd_config(struct rte_compressdev *dev,
18                 struct rte_compressdev_config *config)
19 {
20         int ret = 0;
21         unsigned int n;
22         char mp_name[RTE_COMPRESSDEV_NAME_MAX_LEN];
23         unsigned int elt_size = sizeof(struct isal_priv_xform);
24         struct isal_comp_private *internals = dev->data->dev_private;
25
26         n = snprintf(mp_name, sizeof(mp_name), "compdev_%d_xform_mp",
27                         dev->data->dev_id);
28         if (n > sizeof(mp_name)) {
29                 ISAL_PMD_LOG(ERR,
30                         "Unable to create unique name for xform mempool");
31                 return -ENOMEM;
32         }
33
34         internals->priv_xform_mp = rte_mempool_lookup(mp_name);
35
36         if (internals->priv_xform_mp != NULL) {
37                 if (((internals->priv_xform_mp)->elt_size != elt_size) ||
38                                 ((internals->priv_xform_mp)->size <
39                                         config->max_nb_priv_xforms)) {
40
41                         ISAL_PMD_LOG(ERR, "%s mempool already exists with different"
42                                 " initialization parameters", mp_name);
43                         internals->priv_xform_mp = NULL;
44                         return -ENOMEM;
45                 }
46         } else { /* First time configuration */
47                 internals->priv_xform_mp = rte_mempool_create(
48                                 mp_name, /* mempool name */
49                                 /* number of elements*/
50                                 config->max_nb_priv_xforms,
51                                 elt_size, /* element size*/
52                                 0, /* Cache size*/
53                                 0, /* private data size */
54                                 NULL, /* obj initialization constructor */
55                                 NULL, /* obj initialization constructor arg */
56                                 NULL, /**< obj constructor*/
57                                 NULL, /* obj constructor arg */
58                                 config->socket_id, /* socket id */
59                                 0); /* flags */
60         }
61
62         if (internals->priv_xform_mp == NULL) {
63                 ISAL_PMD_LOG(ERR, "%s mempool allocation failed", mp_name);
64                 return -ENOMEM;
65         }
66
67         dev->data->dev_private = internals;
68
69         return ret;
70 }
71
72 /** Start device */
73 static int
74 isal_comp_pmd_start(__rte_unused struct rte_compressdev *dev)
75 {
76         return 0;
77 }
78
79 /** Stop device */
80 static void
81 isal_comp_pmd_stop(__rte_unused struct rte_compressdev *dev)
82 {
83 }
84
85 /** Close device */
86 static int
87 isal_comp_pmd_close(struct rte_compressdev *dev)
88 {
89         /* Free private data */
90         struct isal_comp_private *internals = dev->data->dev_private;
91
92         rte_mempool_free(internals->priv_xform_mp);
93         return 0;
94 }
95
96 /** Get device statistics */
97 static void
98 isal_comp_pmd_stats_get(struct rte_compressdev *dev,
99                 struct rte_compressdev_stats *stats)
100 {
101         uint16_t qp_id;
102
103         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
104                 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
105
106                 stats->enqueued_count += qp->qp_stats.enqueued_count;
107                 stats->dequeued_count += qp->qp_stats.dequeued_count;
108
109                 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
110                 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
111         }
112 }
113
114 /** Get device info */
115 static void
116 isal_comp_pmd_info_get(struct rte_compressdev *dev __rte_unused,
117                 struct rte_compressdev_info *dev_info)
118 {
119         if (dev_info != NULL) {
120                 dev_info->capabilities = isal_pmd_capabilities;
121                 dev_info->feature_flags = RTE_COMPDEV_FF_CPU_AVX512 |
122                                 RTE_COMPDEV_FF_CPU_AVX2 |
123                                 RTE_COMPDEV_FF_CPU_AVX |
124                                 RTE_COMPDEV_FF_CPU_SSE;
125         }
126 }
127
128 /** Reset device statistics */
129 static void
130 isal_comp_pmd_stats_reset(struct rte_compressdev *dev)
131 {
132         uint16_t qp_id;
133
134         for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
135                 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
136                 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
137         }
138 }
139
140 /** Release queue pair */
141 static int
142 isal_comp_pmd_qp_release(struct rte_compressdev *dev, uint16_t qp_id)
143 {
144         struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
145
146         if (qp == NULL)
147                 return -EINVAL;
148
149         if (dev->data->queue_pairs[qp_id] != NULL)
150                 rte_free(dev->data->queue_pairs[qp_id]);
151
152         return 0;
153 }
154
155 /** Create a ring to place process packets on */
156 static struct rte_ring *
157 isal_comp_pmd_qp_create_processed_pkts_ring(struct isal_comp_qp *qp,
158                 unsigned int ring_size, int socket_id)
159 {
160         struct rte_ring *r;
161
162         r = rte_ring_lookup(qp->name);
163         if (r) {
164                 if (rte_ring_get_size(r) >= ring_size) {
165                         ISAL_PMD_LOG(DEBUG,
166                                 "Reusing existing ring %s for processed packets",
167                                 qp->name);
168                         return r;
169                 }
170
171                         ISAL_PMD_LOG(ERR,
172                                 "Unable to reuse existing ring %s"
173                                 " for processed packets",
174                          qp->name);
175                 return NULL;
176         }
177
178         return rte_ring_create(qp->name, ring_size, socket_id,
179                         RING_F_SP_ENQ | RING_F_SC_DEQ);
180 }
181
182 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
183 static int
184 isal_comp_pmd_qp_set_unique_name(struct rte_compressdev *dev,
185 struct isal_comp_qp *qp)
186 {
187         unsigned int n = snprintf(qp->name, sizeof(qp->name),
188                         "isal_compression_pmd_%u_qp_%u",
189                         dev->data->dev_id, qp->id);
190
191         if (n >= sizeof(qp->name))
192                 return -1;
193
194         return 0;
195 }
196
197 /* Setup a queue pair */
198 static int
199 isal_comp_pmd_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
200                 uint32_t max_inflight_ops, int socket_id)
201 {
202         struct isal_comp_qp *qp = NULL;
203         int retval;
204
205         /* Free memory prior to re-allocation if needed. */
206         if (dev->data->queue_pairs[qp_id] != NULL)
207                 isal_comp_pmd_qp_release(dev, qp_id);
208
209         /* Allocate the queue pair data structure. */
210         qp = rte_zmalloc_socket("Isa-l compression PMD Queue Pair", sizeof(*qp),
211                                         RTE_CACHE_LINE_SIZE, socket_id);
212         if (qp == NULL) {
213                 ISAL_PMD_LOG(ERR, "Failed to allocate queue pair memory");
214                 return (-ENOMEM);
215         }
216
217         qp->id = qp_id;
218         dev->data->queue_pairs[qp_id] = qp;
219
220         retval = isal_comp_pmd_qp_set_unique_name(dev, qp);
221         if (retval) {
222                 ISAL_PMD_LOG(ERR, "Failed to create unique name for isal "
223                                 "compression device");
224                 goto qp_setup_cleanup;
225         }
226
227         qp->processed_pkts = isal_comp_pmd_qp_create_processed_pkts_ring(qp,
228                         max_inflight_ops, socket_id);
229         if (qp->processed_pkts == NULL) {
230                 ISAL_PMD_LOG(ERR, "Failed to create unique name for isal "
231                                 "compression device");
232                 goto qp_setup_cleanup;
233         }
234
235         qp->num_free_elements = rte_ring_free_count(qp->processed_pkts);
236
237         memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
238         return 0;
239
240 qp_setup_cleanup:
241         if (qp)
242                 rte_free(qp);
243
244         return -1;
245 }
246
247 /** Set private xform data*/
248 static int
249 isal_comp_pmd_priv_xform_create(struct rte_compressdev *dev,
250                         const struct rte_comp_xform *xform, void **priv_xform)
251 {
252         int ret;
253         struct isal_comp_private *internals = dev->data->dev_private;
254
255         if (xform == NULL) {
256                 ISAL_PMD_LOG(ERR, "Invalid Xform struct");
257                 return -EINVAL;
258         }
259
260         if (rte_mempool_get(internals->priv_xform_mp, priv_xform)) {
261                 ISAL_PMD_LOG(ERR,
262                         "Couldn't get object from private xform mempool");
263                 return -ENOMEM;
264         }
265
266         ret = isal_comp_set_priv_xform_parameters(*priv_xform, xform);
267         if (ret != 0) {
268                 ISAL_PMD_LOG(ERR, "Failed to configure private xform parameters");
269
270                 /* Return private xform to mempool */
271                 rte_mempool_put(internals->priv_xform_mp, priv_xform);
272                 return ret;
273         }
274         return 0;
275 }
276
277 /** Clear memory of the private xform so it doesn't leave key material behind */
278 static int
279 isal_comp_pmd_priv_xform_free(struct rte_compressdev *dev, void *priv_xform)
280 {
281         struct isal_comp_private *internals = dev->data->dev_private;
282
283         /* Zero out the whole structure */
284         if (priv_xform) {
285                 memset(priv_xform, 0, sizeof(struct isal_priv_xform));
286                 rte_mempool_put(internals->priv_xform_mp, priv_xform);
287         }
288         return 0;
289 }
290
291 struct rte_compressdev_ops isal_pmd_ops = {
292                 .dev_configure          = isal_comp_pmd_config,
293                 .dev_start              = isal_comp_pmd_start,
294                 .dev_stop               = isal_comp_pmd_stop,
295                 .dev_close              = isal_comp_pmd_close,
296
297                 .stats_get              = isal_comp_pmd_stats_get,
298                 .stats_reset            = isal_comp_pmd_stats_reset,
299
300                 .dev_infos_get          = isal_comp_pmd_info_get,
301
302                 .queue_pair_setup       = isal_comp_pmd_qp_setup,
303                 .queue_pair_release     = isal_comp_pmd_qp_release,
304
305                 .private_xform_create   = isal_comp_pmd_priv_xform_create,
306                 .private_xform_free     = isal_comp_pmd_priv_xform_free,
307 };
308
309 struct rte_compressdev_ops *isal_compress_pmd_ops = &isal_pmd_ops;