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 |
21 RTE_COMP_FF_CRC32_CHECKSUM |
22 RTE_COMP_FF_ADLER32_CHECKSUM,
29 RTE_COMP_END_OF_CAPABILITIES_LIST()
32 /** Configure device */
34 isal_comp_pmd_config(struct rte_compressdev *dev,
35 struct rte_compressdev_config *config)
39 char mp_name[RTE_COMPRESSDEV_NAME_MAX_LEN];
40 unsigned int elt_size = sizeof(struct isal_priv_xform);
41 struct isal_comp_private *internals = dev->data->dev_private;
43 n = snprintf(mp_name, sizeof(mp_name), "compdev_%d_xform_mp",
45 if (n > sizeof(mp_name)) {
47 "Unable to create unique name for xform mempool");
51 internals->priv_xform_mp = rte_mempool_lookup(mp_name);
53 if (internals->priv_xform_mp != NULL) {
54 if (((internals->priv_xform_mp)->elt_size != elt_size) ||
55 ((internals->priv_xform_mp)->size <
56 config->max_nb_priv_xforms)) {
58 ISAL_PMD_LOG(ERR, "%s mempool already exists with different"
59 " initialization parameters", mp_name);
60 internals->priv_xform_mp = NULL;
63 } else { /* First time configuration */
64 internals->priv_xform_mp = rte_mempool_create(
65 mp_name, /* mempool name */
66 /* number of elements*/
67 config->max_nb_priv_xforms,
68 elt_size, /* element size*/
70 0, /* private data size */
71 NULL, /* obj initialization constructor */
72 NULL, /* obj initialization constructor arg */
73 NULL, /**< obj constructor*/
74 NULL, /* obj constructor arg */
75 config->socket_id, /* socket id */
79 if (internals->priv_xform_mp == NULL) {
80 ISAL_PMD_LOG(ERR, "%s mempool allocation failed", mp_name);
84 dev->data->dev_private = internals;
91 isal_comp_pmd_start(__rte_unused struct rte_compressdev *dev)
98 isal_comp_pmd_stop(__rte_unused struct rte_compressdev *dev)
104 isal_comp_pmd_close(struct rte_compressdev *dev)
106 /* Free private data */
107 struct isal_comp_private *internals = dev->data->dev_private;
109 rte_mempool_free(internals->priv_xform_mp);
113 /** Get device statistics */
115 isal_comp_pmd_stats_get(struct rte_compressdev *dev,
116 struct rte_compressdev_stats *stats)
120 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
121 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
123 stats->enqueued_count += qp->qp_stats.enqueued_count;
124 stats->dequeued_count += qp->qp_stats.dequeued_count;
126 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
127 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
131 /** Get device info */
133 isal_comp_pmd_info_get(struct rte_compressdev *dev __rte_unused,
134 struct rte_compressdev_info *dev_info)
136 if (dev_info != NULL) {
137 dev_info->capabilities = isal_pmd_capabilities;
139 /* Check CPU for supported vector instruction and set
142 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
143 dev_info->feature_flags |= RTE_COMPDEV_FF_CPU_AVX512;
144 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
145 dev_info->feature_flags |= RTE_COMPDEV_FF_CPU_AVX2;
146 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
147 dev_info->feature_flags |= RTE_COMPDEV_FF_CPU_AVX;
149 dev_info->feature_flags |= RTE_COMPDEV_FF_CPU_SSE;
153 /** Reset device statistics */
155 isal_comp_pmd_stats_reset(struct rte_compressdev *dev)
159 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
160 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
161 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
165 /** Release queue pair */
167 isal_comp_pmd_qp_release(struct rte_compressdev *dev, uint16_t qp_id)
169 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
174 if (qp->stream != NULL)
175 rte_free(qp->stream);
177 if (qp->stream->level_buf != NULL)
178 rte_free(qp->stream->level_buf);
180 if (qp->state != NULL)
183 if (qp->processed_pkts != NULL)
184 rte_ring_free(qp->processed_pkts);
187 dev->data->queue_pairs[qp_id] = NULL;
192 /** Create a ring to place process packets on */
193 static struct rte_ring *
194 isal_comp_pmd_qp_create_processed_pkts_ring(struct isal_comp_qp *qp,
195 unsigned int ring_size, int socket_id)
199 r = rte_ring_lookup(qp->name);
201 if (rte_ring_get_size(r) >= ring_size) {
203 "Reusing existing ring %s for processed packets",
209 "Unable to reuse existing ring %s"
210 " for processed packets",
215 return rte_ring_create(qp->name, ring_size, socket_id,
216 RING_F_SP_ENQ | RING_F_SC_DEQ);
219 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
221 isal_comp_pmd_qp_set_unique_name(struct rte_compressdev *dev,
222 struct isal_comp_qp *qp)
224 unsigned int n = snprintf(qp->name, sizeof(qp->name),
225 "isal_compression_pmd_%u_qp_%u",
226 dev->data->dev_id, qp->id);
228 if (n >= sizeof(qp->name))
234 /* Setup a queue pair */
236 isal_comp_pmd_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
237 uint32_t max_inflight_ops, int socket_id)
239 struct isal_comp_qp *qp = NULL;
242 /* Free memory prior to re-allocation if needed. */
243 if (dev->data->queue_pairs[qp_id] != NULL)
244 isal_comp_pmd_qp_release(dev, qp_id);
246 /* Allocate the queue pair data structure. */
247 qp = rte_zmalloc_socket("Isa-l compression PMD Queue Pair", sizeof(*qp),
248 RTE_CACHE_LINE_SIZE, socket_id);
250 ISAL_PMD_LOG(ERR, "Failed to allocate queue pair memory");
254 /* Initialize memory for compression stream structure */
255 qp->stream = rte_zmalloc_socket("Isa-l compression stream ",
256 sizeof(struct isal_zstream), RTE_CACHE_LINE_SIZE,
259 /* Initialize memory for compression level buffer */
260 qp->stream->level_buf = rte_zmalloc_socket("Isa-l compression lev_buf",
261 ISAL_DEF_LVL3_DEFAULT, RTE_CACHE_LINE_SIZE,
264 /* Initialize memory for decompression state structure */
265 qp->state = rte_zmalloc_socket("Isa-l decompression state",
266 sizeof(struct inflate_state), RTE_CACHE_LINE_SIZE,
270 dev->data->queue_pairs[qp_id] = qp;
272 retval = isal_comp_pmd_qp_set_unique_name(dev, qp);
274 ISAL_PMD_LOG(ERR, "Failed to create unique name for isal "
275 "compression device");
276 goto qp_setup_cleanup;
279 qp->processed_pkts = isal_comp_pmd_qp_create_processed_pkts_ring(qp,
280 max_inflight_ops, socket_id);
281 if (qp->processed_pkts == NULL) {
282 ISAL_PMD_LOG(ERR, "Failed to create unique name for isal "
283 "compression device");
284 goto qp_setup_cleanup;
287 qp->num_free_elements = rte_ring_free_count(qp->processed_pkts);
289 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
299 /** Set private xform data*/
301 isal_comp_pmd_priv_xform_create(struct rte_compressdev *dev,
302 const struct rte_comp_xform *xform, void **priv_xform)
305 struct isal_comp_private *internals = dev->data->dev_private;
308 ISAL_PMD_LOG(ERR, "Invalid Xform struct");
312 if (rte_mempool_get(internals->priv_xform_mp, priv_xform)) {
314 "Couldn't get object from private xform mempool");
318 ret = isal_comp_set_priv_xform_parameters(*priv_xform, xform);
320 ISAL_PMD_LOG(ERR, "Failed to configure private xform parameters");
322 /* Return private xform to mempool */
323 rte_mempool_put(internals->priv_xform_mp, priv_xform);
329 /** Clear memory of the private xform so it doesn't leave key material behind */
331 isal_comp_pmd_priv_xform_free(struct rte_compressdev *dev, void *priv_xform)
333 struct isal_comp_private *internals = dev->data->dev_private;
335 /* Zero out the whole structure */
337 memset(priv_xform, 0, sizeof(struct isal_priv_xform));
338 rte_mempool_put(internals->priv_xform_mp, priv_xform);
343 struct rte_compressdev_ops isal_pmd_ops = {
344 .dev_configure = isal_comp_pmd_config,
345 .dev_start = isal_comp_pmd_start,
346 .dev_stop = isal_comp_pmd_stop,
347 .dev_close = isal_comp_pmd_close,
349 .stats_get = isal_comp_pmd_stats_get,
350 .stats_reset = isal_comp_pmd_stats_reset,
352 .dev_infos_get = isal_comp_pmd_info_get,
354 .queue_pair_setup = isal_comp_pmd_qp_setup,
355 .queue_pair_release = isal_comp_pmd_qp_release,
357 .private_xform_create = isal_comp_pmd_priv_xform_create,
358 .private_xform_free = isal_comp_pmd_priv_xform_free,
361 struct rte_compressdev_ops *isal_compress_pmd_ops = &isal_pmd_ops;