1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
6 #include <rte_common.h>
7 #include <rte_compressdev_pmd.h>
8 #include <rte_malloc.h>
10 #include "isal_compress_pmd_private.h"
12 static const struct rte_compressdev_capabilities isal_pmd_capabilities[] = {
14 .algo = RTE_COMP_ALGO_DEFLATE,
15 .comp_feature_flags = RTE_COMP_FF_OOP_SGL_IN_SGL_OUT |
16 RTE_COMP_FF_OOP_SGL_IN_LB_OUT |
17 RTE_COMP_FF_OOP_LB_IN_SGL_OUT |
18 RTE_COMP_FF_SHAREABLE_PRIV_XFORM |
19 RTE_COMP_FF_HUFFMAN_FIXED |
20 RTE_COMP_FF_HUFFMAN_DYNAMIC,
27 RTE_COMP_END_OF_CAPABILITIES_LIST()
30 /** Configure device */
32 isal_comp_pmd_config(struct rte_compressdev *dev,
33 struct rte_compressdev_config *config)
37 char mp_name[RTE_COMPRESSDEV_NAME_MAX_LEN];
38 unsigned int elt_size = sizeof(struct isal_priv_xform);
39 struct isal_comp_private *internals = dev->data->dev_private;
41 n = snprintf(mp_name, sizeof(mp_name), "compdev_%d_xform_mp",
43 if (n > sizeof(mp_name)) {
45 "Unable to create unique name for xform mempool");
49 internals->priv_xform_mp = rte_mempool_lookup(mp_name);
51 if (internals->priv_xform_mp != NULL) {
52 if (((internals->priv_xform_mp)->elt_size != elt_size) ||
53 ((internals->priv_xform_mp)->size <
54 config->max_nb_priv_xforms)) {
56 ISAL_PMD_LOG(ERR, "%s mempool already exists with different"
57 " initialization parameters", mp_name);
58 internals->priv_xform_mp = NULL;
61 } else { /* First time configuration */
62 internals->priv_xform_mp = rte_mempool_create(
63 mp_name, /* mempool name */
64 /* number of elements*/
65 config->max_nb_priv_xforms,
66 elt_size, /* element size*/
68 0, /* private data size */
69 NULL, /* obj initialization constructor */
70 NULL, /* obj initialization constructor arg */
71 NULL, /**< obj constructor*/
72 NULL, /* obj constructor arg */
73 config->socket_id, /* socket id */
77 if (internals->priv_xform_mp == NULL) {
78 ISAL_PMD_LOG(ERR, "%s mempool allocation failed", mp_name);
82 dev->data->dev_private = internals;
89 isal_comp_pmd_start(__rte_unused struct rte_compressdev *dev)
96 isal_comp_pmd_stop(__rte_unused struct rte_compressdev *dev)
102 isal_comp_pmd_close(struct rte_compressdev *dev)
104 /* Free private data */
105 struct isal_comp_private *internals = dev->data->dev_private;
107 rte_mempool_free(internals->priv_xform_mp);
111 /** Get device statistics */
113 isal_comp_pmd_stats_get(struct rte_compressdev *dev,
114 struct rte_compressdev_stats *stats)
118 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
119 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
121 stats->enqueued_count += qp->qp_stats.enqueued_count;
122 stats->dequeued_count += qp->qp_stats.dequeued_count;
124 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
125 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
129 /** Get device info */
131 isal_comp_pmd_info_get(struct rte_compressdev *dev __rte_unused,
132 struct rte_compressdev_info *dev_info)
134 if (dev_info != NULL) {
135 dev_info->capabilities = isal_pmd_capabilities;
136 dev_info->feature_flags = RTE_COMPDEV_FF_CPU_AVX512 |
137 RTE_COMPDEV_FF_CPU_AVX2 |
138 RTE_COMPDEV_FF_CPU_AVX |
139 RTE_COMPDEV_FF_CPU_SSE;
143 /** Reset device statistics */
145 isal_comp_pmd_stats_reset(struct rte_compressdev *dev)
149 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
150 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
151 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
155 /** Release queue pair */
157 isal_comp_pmd_qp_release(struct rte_compressdev *dev, uint16_t qp_id)
159 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
164 if (qp->stream != NULL)
165 rte_free(qp->stream);
167 if (qp->stream->level_buf != NULL)
168 rte_free(qp->stream->level_buf);
170 if (qp->state != NULL)
173 if (qp->processed_pkts != NULL)
174 rte_ring_free(qp->processed_pkts);
177 dev->data->queue_pairs[qp_id] = NULL;
182 /** Create a ring to place process packets on */
183 static struct rte_ring *
184 isal_comp_pmd_qp_create_processed_pkts_ring(struct isal_comp_qp *qp,
185 unsigned int ring_size, int socket_id)
189 r = rte_ring_lookup(qp->name);
191 if (rte_ring_get_size(r) >= ring_size) {
193 "Reusing existing ring %s for processed packets",
199 "Unable to reuse existing ring %s"
200 " for processed packets",
205 return rte_ring_create(qp->name, ring_size, socket_id,
206 RING_F_SP_ENQ | RING_F_SC_DEQ);
209 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
211 isal_comp_pmd_qp_set_unique_name(struct rte_compressdev *dev,
212 struct isal_comp_qp *qp)
214 unsigned int n = snprintf(qp->name, sizeof(qp->name),
215 "isal_compression_pmd_%u_qp_%u",
216 dev->data->dev_id, qp->id);
218 if (n >= sizeof(qp->name))
224 /* Setup a queue pair */
226 isal_comp_pmd_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
227 uint32_t max_inflight_ops, int socket_id)
229 struct isal_comp_qp *qp = NULL;
232 /* Free memory prior to re-allocation if needed. */
233 if (dev->data->queue_pairs[qp_id] != NULL)
234 isal_comp_pmd_qp_release(dev, qp_id);
236 /* Allocate the queue pair data structure. */
237 qp = rte_zmalloc_socket("Isa-l compression PMD Queue Pair", sizeof(*qp),
238 RTE_CACHE_LINE_SIZE, socket_id);
240 ISAL_PMD_LOG(ERR, "Failed to allocate queue pair memory");
244 /* Initialize memory for compression stream structure */
245 qp->stream = rte_zmalloc_socket("Isa-l compression stream ",
246 sizeof(struct isal_zstream), RTE_CACHE_LINE_SIZE,
249 /* Initialize memory for compression level buffer */
250 qp->stream->level_buf = rte_zmalloc_socket("Isa-l compression lev_buf",
251 ISAL_DEF_LVL3_DEFAULT, RTE_CACHE_LINE_SIZE,
254 /* Initialize memory for decompression state structure */
255 qp->state = rte_zmalloc_socket("Isa-l decompression state",
256 sizeof(struct inflate_state), RTE_CACHE_LINE_SIZE,
260 dev->data->queue_pairs[qp_id] = qp;
262 retval = isal_comp_pmd_qp_set_unique_name(dev, qp);
264 ISAL_PMD_LOG(ERR, "Failed to create unique name for isal "
265 "compression device");
266 goto qp_setup_cleanup;
269 qp->processed_pkts = isal_comp_pmd_qp_create_processed_pkts_ring(qp,
270 max_inflight_ops, socket_id);
271 if (qp->processed_pkts == NULL) {
272 ISAL_PMD_LOG(ERR, "Failed to create unique name for isal "
273 "compression device");
274 goto qp_setup_cleanup;
277 qp->num_free_elements = rte_ring_free_count(qp->processed_pkts);
279 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
289 /** Set private xform data*/
291 isal_comp_pmd_priv_xform_create(struct rte_compressdev *dev,
292 const struct rte_comp_xform *xform, void **priv_xform)
295 struct isal_comp_private *internals = dev->data->dev_private;
298 ISAL_PMD_LOG(ERR, "Invalid Xform struct");
302 if (rte_mempool_get(internals->priv_xform_mp, priv_xform)) {
304 "Couldn't get object from private xform mempool");
308 ret = isal_comp_set_priv_xform_parameters(*priv_xform, xform);
310 ISAL_PMD_LOG(ERR, "Failed to configure private xform parameters");
312 /* Return private xform to mempool */
313 rte_mempool_put(internals->priv_xform_mp, priv_xform);
319 /** Clear memory of the private xform so it doesn't leave key material behind */
321 isal_comp_pmd_priv_xform_free(struct rte_compressdev *dev, void *priv_xform)
323 struct isal_comp_private *internals = dev->data->dev_private;
325 /* Zero out the whole structure */
327 memset(priv_xform, 0, sizeof(struct isal_priv_xform));
328 rte_mempool_put(internals->priv_xform_mp, priv_xform);
333 struct rte_compressdev_ops isal_pmd_ops = {
334 .dev_configure = isal_comp_pmd_config,
335 .dev_start = isal_comp_pmd_start,
336 .dev_stop = isal_comp_pmd_stop,
337 .dev_close = isal_comp_pmd_close,
339 .stats_get = isal_comp_pmd_stats_get,
340 .stats_reset = isal_comp_pmd_stats_reset,
342 .dev_infos_get = isal_comp_pmd_info_get,
344 .queue_pair_setup = isal_comp_pmd_qp_setup,
345 .queue_pair_release = isal_comp_pmd_qp_release,
347 .private_xform_create = isal_comp_pmd_priv_xform_create,
348 .private_xform_free = isal_comp_pmd_priv_xform_free,
351 struct rte_compressdev_ops *isal_compress_pmd_ops = &isal_pmd_ops;