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_SHAREABLE_PRIV_XFORM |
16 RTE_COMP_FF_HUFFMAN_FIXED |
17 RTE_COMP_FF_HUFFMAN_DYNAMIC,
24 RTE_COMP_END_OF_CAPABILITIES_LIST()
27 /** Configure device */
29 isal_comp_pmd_config(struct rte_compressdev *dev,
30 struct rte_compressdev_config *config)
34 char mp_name[RTE_COMPRESSDEV_NAME_MAX_LEN];
35 unsigned int elt_size = sizeof(struct isal_priv_xform);
36 struct isal_comp_private *internals = dev->data->dev_private;
38 n = snprintf(mp_name, sizeof(mp_name), "compdev_%d_xform_mp",
40 if (n > sizeof(mp_name)) {
42 "Unable to create unique name for xform mempool");
46 internals->priv_xform_mp = rte_mempool_lookup(mp_name);
48 if (internals->priv_xform_mp != NULL) {
49 if (((internals->priv_xform_mp)->elt_size != elt_size) ||
50 ((internals->priv_xform_mp)->size <
51 config->max_nb_priv_xforms)) {
53 ISAL_PMD_LOG(ERR, "%s mempool already exists with different"
54 " initialization parameters", mp_name);
55 internals->priv_xform_mp = NULL;
58 } else { /* First time configuration */
59 internals->priv_xform_mp = rte_mempool_create(
60 mp_name, /* mempool name */
61 /* number of elements*/
62 config->max_nb_priv_xforms,
63 elt_size, /* element size*/
65 0, /* private data size */
66 NULL, /* obj initialization constructor */
67 NULL, /* obj initialization constructor arg */
68 NULL, /**< obj constructor*/
69 NULL, /* obj constructor arg */
70 config->socket_id, /* socket id */
74 if (internals->priv_xform_mp == NULL) {
75 ISAL_PMD_LOG(ERR, "%s mempool allocation failed", mp_name);
79 dev->data->dev_private = internals;
86 isal_comp_pmd_start(__rte_unused struct rte_compressdev *dev)
93 isal_comp_pmd_stop(__rte_unused struct rte_compressdev *dev)
99 isal_comp_pmd_close(struct rte_compressdev *dev)
101 /* Free private data */
102 struct isal_comp_private *internals = dev->data->dev_private;
104 rte_mempool_free(internals->priv_xform_mp);
108 /** Get device statistics */
110 isal_comp_pmd_stats_get(struct rte_compressdev *dev,
111 struct rte_compressdev_stats *stats)
115 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
116 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
118 stats->enqueued_count += qp->qp_stats.enqueued_count;
119 stats->dequeued_count += qp->qp_stats.dequeued_count;
121 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
122 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
126 /** Get device info */
128 isal_comp_pmd_info_get(struct rte_compressdev *dev __rte_unused,
129 struct rte_compressdev_info *dev_info)
131 if (dev_info != NULL) {
132 dev_info->capabilities = isal_pmd_capabilities;
133 dev_info->feature_flags = RTE_COMPDEV_FF_CPU_AVX512 |
134 RTE_COMPDEV_FF_CPU_AVX2 |
135 RTE_COMPDEV_FF_CPU_AVX |
136 RTE_COMPDEV_FF_CPU_SSE;
140 /** Reset device statistics */
142 isal_comp_pmd_stats_reset(struct rte_compressdev *dev)
146 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
147 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
148 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
152 /** Release queue pair */
154 isal_comp_pmd_qp_release(struct rte_compressdev *dev, uint16_t qp_id)
156 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
161 if (qp->stream != NULL)
162 rte_free(qp->stream);
164 if (qp->stream->level_buf != NULL)
165 rte_free(qp->stream->level_buf);
167 if (qp->state != NULL)
170 if (dev->data->queue_pairs[qp_id] != NULL)
171 rte_free(dev->data->queue_pairs[qp_id]);
176 /** Create a ring to place process packets on */
177 static struct rte_ring *
178 isal_comp_pmd_qp_create_processed_pkts_ring(struct isal_comp_qp *qp,
179 unsigned int ring_size, int socket_id)
183 r = rte_ring_lookup(qp->name);
185 if (rte_ring_get_size(r) >= ring_size) {
187 "Reusing existing ring %s for processed packets",
193 "Unable to reuse existing ring %s"
194 " for processed packets",
199 return rte_ring_create(qp->name, ring_size, socket_id,
200 RING_F_SP_ENQ | RING_F_SC_DEQ);
203 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
205 isal_comp_pmd_qp_set_unique_name(struct rte_compressdev *dev,
206 struct isal_comp_qp *qp)
208 unsigned int n = snprintf(qp->name, sizeof(qp->name),
209 "isal_compression_pmd_%u_qp_%u",
210 dev->data->dev_id, qp->id);
212 if (n >= sizeof(qp->name))
218 /* Setup a queue pair */
220 isal_comp_pmd_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
221 uint32_t max_inflight_ops, int socket_id)
223 struct isal_comp_qp *qp = NULL;
226 /* Free memory prior to re-allocation if needed. */
227 if (dev->data->queue_pairs[qp_id] != NULL)
228 isal_comp_pmd_qp_release(dev, qp_id);
230 /* Allocate the queue pair data structure. */
231 qp = rte_zmalloc_socket("Isa-l compression PMD Queue Pair", sizeof(*qp),
232 RTE_CACHE_LINE_SIZE, socket_id);
234 ISAL_PMD_LOG(ERR, "Failed to allocate queue pair memory");
238 /* Initialize memory for compression stream structure */
239 qp->stream = rte_zmalloc_socket("Isa-l compression stream ",
240 sizeof(struct isal_zstream), RTE_CACHE_LINE_SIZE,
243 /* Initialize memory for compression level buffer */
244 qp->stream->level_buf = rte_zmalloc_socket("Isa-l compression lev_buf",
245 ISAL_DEF_LVL3_DEFAULT, RTE_CACHE_LINE_SIZE,
248 /* Initialize memory for decompression state structure */
249 qp->state = rte_zmalloc_socket("Isa-l decompression state",
250 sizeof(struct inflate_state), RTE_CACHE_LINE_SIZE,
254 dev->data->queue_pairs[qp_id] = qp;
256 retval = isal_comp_pmd_qp_set_unique_name(dev, qp);
258 ISAL_PMD_LOG(ERR, "Failed to create unique name for isal "
259 "compression device");
260 goto qp_setup_cleanup;
263 qp->processed_pkts = isal_comp_pmd_qp_create_processed_pkts_ring(qp,
264 max_inflight_ops, socket_id);
265 if (qp->processed_pkts == NULL) {
266 ISAL_PMD_LOG(ERR, "Failed to create unique name for isal "
267 "compression device");
268 goto qp_setup_cleanup;
271 qp->num_free_elements = rte_ring_free_count(qp->processed_pkts);
273 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
283 /** Set private xform data*/
285 isal_comp_pmd_priv_xform_create(struct rte_compressdev *dev,
286 const struct rte_comp_xform *xform, void **priv_xform)
289 struct isal_comp_private *internals = dev->data->dev_private;
292 ISAL_PMD_LOG(ERR, "Invalid Xform struct");
296 if (rte_mempool_get(internals->priv_xform_mp, priv_xform)) {
298 "Couldn't get object from private xform mempool");
302 ret = isal_comp_set_priv_xform_parameters(*priv_xform, xform);
304 ISAL_PMD_LOG(ERR, "Failed to configure private xform parameters");
306 /* Return private xform to mempool */
307 rte_mempool_put(internals->priv_xform_mp, priv_xform);
313 /** Clear memory of the private xform so it doesn't leave key material behind */
315 isal_comp_pmd_priv_xform_free(struct rte_compressdev *dev, void *priv_xform)
317 struct isal_comp_private *internals = dev->data->dev_private;
319 /* Zero out the whole structure */
321 memset(priv_xform, 0, sizeof(struct isal_priv_xform));
322 rte_mempool_put(internals->priv_xform_mp, priv_xform);
327 struct rte_compressdev_ops isal_pmd_ops = {
328 .dev_configure = isal_comp_pmd_config,
329 .dev_start = isal_comp_pmd_start,
330 .dev_stop = isal_comp_pmd_stop,
331 .dev_close = isal_comp_pmd_close,
333 .stats_get = isal_comp_pmd_stats_get,
334 .stats_reset = isal_comp_pmd_stats_reset,
336 .dev_infos_get = isal_comp_pmd_info_get,
338 .queue_pair_setup = isal_comp_pmd_qp_setup,
339 .queue_pair_release = isal_comp_pmd_qp_release,
341 .private_xform_create = isal_comp_pmd_priv_xform_create,
342 .private_xform_free = isal_comp_pmd_priv_xform_free,
345 struct rte_compressdev_ops *isal_compress_pmd_ops = &isal_pmd_ops;