1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
5 #include <rte_common.h>
6 #include <rte_compressdev_pmd.h>
7 #include <rte_malloc.h>
9 #include "isal_compress_pmd_private.h"
11 static const struct rte_compressdev_capabilities isal_pmd_capabilities[] = {
12 RTE_COMP_END_OF_CAPABILITIES_LIST()
15 /** Configure device */
17 isal_comp_pmd_config(struct rte_compressdev *dev,
18 struct rte_compressdev_config *config)
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;
26 n = snprintf(mp_name, sizeof(mp_name), "compdev_%d_xform_mp",
28 if (n > sizeof(mp_name)) {
30 "Unable to create unique name for xform mempool");
34 internals->priv_xform_mp = rte_mempool_lookup(mp_name);
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)) {
41 ISAL_PMD_LOG(ERR, "%s mempool already exists with different"
42 " initialization parameters", mp_name);
43 internals->priv_xform_mp = NULL;
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*/
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 */
62 if (internals->priv_xform_mp == NULL) {
63 ISAL_PMD_LOG(ERR, "%s mempool allocation failed", mp_name);
67 dev->data->dev_private = internals;
74 isal_comp_pmd_start(__rte_unused struct rte_compressdev *dev)
81 isal_comp_pmd_stop(__rte_unused struct rte_compressdev *dev)
87 isal_comp_pmd_close(struct rte_compressdev *dev)
89 /* Free private data */
90 struct isal_comp_private *internals = dev->data->dev_private;
92 rte_mempool_free(internals->priv_xform_mp);
96 /** Get device statistics */
98 isal_comp_pmd_stats_get(struct rte_compressdev *dev,
99 struct rte_compressdev_stats *stats)
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];
106 stats->enqueued_count += qp->qp_stats.enqueued_count;
107 stats->dequeued_count += qp->qp_stats.dequeued_count;
109 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
110 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
114 /** Get device info */
116 isal_comp_pmd_info_get(struct rte_compressdev *dev __rte_unused,
117 struct rte_compressdev_info *dev_info)
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;
128 /** Reset device statistics */
130 isal_comp_pmd_stats_reset(struct rte_compressdev *dev)
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));
140 /** Release queue pair */
142 isal_comp_pmd_qp_release(struct rte_compressdev *dev, uint16_t qp_id)
144 struct isal_comp_qp *qp = dev->data->queue_pairs[qp_id];
149 if (dev->data->queue_pairs[qp_id] != NULL)
150 rte_free(dev->data->queue_pairs[qp_id]);
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)
162 r = rte_ring_lookup(qp->name);
164 if (rte_ring_get_size(r) >= ring_size) {
166 "Reusing existing ring %s for processed packets",
172 "Unable to reuse existing ring %s"
173 " for processed packets",
178 return rte_ring_create(qp->name, ring_size, socket_id,
179 RING_F_SP_ENQ | RING_F_SC_DEQ);
182 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
184 isal_comp_pmd_qp_set_unique_name(struct rte_compressdev *dev,
185 struct isal_comp_qp *qp)
187 unsigned int n = snprintf(qp->name, sizeof(qp->name),
188 "isal_compression_pmd_%u_qp_%u",
189 dev->data->dev_id, qp->id);
191 if (n >= sizeof(qp->name))
197 /* Setup a queue pair */
199 isal_comp_pmd_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
200 uint32_t max_inflight_ops, int socket_id)
202 struct isal_comp_qp *qp = NULL;
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);
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);
213 ISAL_PMD_LOG(ERR, "Failed to allocate queue pair memory");
218 dev->data->queue_pairs[qp_id] = qp;
220 retval = isal_comp_pmd_qp_set_unique_name(dev, qp);
222 ISAL_PMD_LOG(ERR, "Failed to create unique name for isal "
223 "compression device");
224 goto qp_setup_cleanup;
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;
235 qp->num_free_elements = rte_ring_free_count(qp->processed_pkts);
237 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
247 /** Set private xform data*/
249 isal_comp_pmd_priv_xform_create(struct rte_compressdev *dev,
250 const struct rte_comp_xform *xform, void **priv_xform)
253 struct isal_comp_private *internals = dev->data->dev_private;
256 ISAL_PMD_LOG(ERR, "Invalid Xform struct");
260 if (rte_mempool_get(internals->priv_xform_mp, priv_xform)) {
262 "Couldn't get object from private xform mempool");
266 ret = isal_comp_set_priv_xform_parameters(*priv_xform, xform);
268 ISAL_PMD_LOG(ERR, "Failed to configure private xform parameters");
270 /* Return private xform to mempool */
271 rte_mempool_put(internals->priv_xform_mp, priv_xform);
277 /** Clear memory of the private xform so it doesn't leave key material behind */
279 isal_comp_pmd_priv_xform_free(struct rte_compressdev *dev, void *priv_xform)
281 struct isal_comp_private *internals = dev->data->dev_private;
283 /* Zero out the whole structure */
285 memset(priv_xform, 0, sizeof(struct isal_priv_xform));
286 rte_mempool_put(internals->priv_xform_mp, priv_xform);
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,
297 .stats_get = isal_comp_pmd_stats_get,
298 .stats_reset = isal_comp_pmd_stats_reset,
300 .dev_infos_get = isal_comp_pmd_info_get,
302 .queue_pair_setup = isal_comp_pmd_qp_setup,
303 .queue_pair_release = isal_comp_pmd_qp_release,
305 .private_xform_create = isal_comp_pmd_priv_xform_create,
306 .private_xform_free = isal_comp_pmd_priv_xform_free,
309 struct rte_compressdev_ops *isal_compress_pmd_ops = &isal_pmd_ops;