1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Cavium Networks
7 #include <rte_common.h>
8 #include <rte_malloc.h>
10 #include "zlib_pmd_private.h"
12 static const struct rte_compressdev_capabilities zlib_pmd_capabilities[] = {
14 .algo = RTE_COMP_ALGO_DEFLATE,
15 .comp_feature_flags = (RTE_COMP_FF_NONCOMPRESSED_BLOCKS |
16 RTE_COMP_FF_HUFFMAN_FIXED |
17 RTE_COMP_FF_HUFFMAN_DYNAMIC),
25 RTE_COMP_END_OF_CAPABILITIES_LIST()
29 /** Configure device */
31 zlib_pmd_config(struct rte_compressdev *dev,
32 struct rte_compressdev_config *config)
34 struct rte_mempool *mp;
35 char mp_name[RTE_MEMPOOL_NAMESIZE];
36 struct zlib_private *internals = dev->data->dev_private;
38 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
39 "stream_mp_%u", dev->data->dev_id);
42 mp = rte_mempool_create(mp_name,
43 config->max_nb_priv_xforms +
44 config->max_nb_streams,
45 sizeof(struct zlib_priv_xform),
46 0, 0, NULL, NULL, NULL,
47 NULL, config->socket_id,
50 ZLIB_PMD_ERR("Cannot create private xform pool on "
51 "socket %d\n", config->socket_id);
61 zlib_pmd_start(__rte_unused struct rte_compressdev *dev)
68 zlib_pmd_stop(__rte_unused struct rte_compressdev *dev)
74 zlib_pmd_close(struct rte_compressdev *dev)
76 struct zlib_private *internals = dev->data->dev_private;
77 rte_mempool_free(internals->mp);
82 /** Get device statistics */
84 zlib_pmd_stats_get(struct rte_compressdev *dev,
85 struct rte_compressdev_stats *stats)
89 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
90 struct zlib_qp *qp = dev->data->queue_pairs[qp_id];
92 stats->enqueued_count += qp->qp_stats.enqueued_count;
93 stats->dequeued_count += qp->qp_stats.dequeued_count;
95 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
96 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
100 /** Reset device statistics */
102 zlib_pmd_stats_reset(struct rte_compressdev *dev)
106 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
107 struct zlib_qp *qp = dev->data->queue_pairs[qp_id];
109 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
113 /** Get device info */
115 zlib_pmd_info_get(struct rte_compressdev *dev,
116 struct rte_compressdev_info *dev_info)
118 if (dev_info != NULL) {
119 dev_info->driver_name = dev->device->name;
120 dev_info->feature_flags = dev->feature_flags;
121 dev_info->capabilities = zlib_pmd_capabilities;
125 /** Release queue pair */
127 zlib_pmd_qp_release(struct rte_compressdev *dev, uint16_t qp_id)
129 struct zlib_qp *qp = dev->data->queue_pairs[qp_id];
132 rte_ring_free(qp->processed_pkts);
134 dev->data->queue_pairs[qp_id] = NULL;
139 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
141 zlib_pmd_qp_set_unique_name(struct rte_compressdev *dev,
144 unsigned int n = snprintf(qp->name, sizeof(qp->name),
146 dev->data->dev_id, qp->id);
148 if (n >= sizeof(qp->name))
154 /** Create a ring to place process packets on */
155 static struct rte_ring *
156 zlib_pmd_qp_create_processed_pkts_ring(struct zlib_qp *qp,
157 unsigned int ring_size, int socket_id)
159 struct rte_ring *r = qp->processed_pkts;
162 if (rte_ring_get_size(r) >= ring_size) {
163 ZLIB_PMD_INFO("Reusing existing ring %s for processed"
164 " packets", qp->name);
168 ZLIB_PMD_ERR("Unable to reuse existing ring %s for processed"
169 " packets", qp->name);
173 return rte_ring_create(qp->name, ring_size, socket_id,
177 /** Setup a queue pair */
179 zlib_pmd_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
180 uint32_t max_inflight_ops, int socket_id)
182 struct zlib_qp *qp = NULL;
184 /* Free memory prior to re-allocation if needed. */
185 if (dev->data->queue_pairs[qp_id] != NULL)
186 zlib_pmd_qp_release(dev, qp_id);
188 /* Allocate the queue pair data structure. */
189 qp = rte_zmalloc_socket("ZLIB PMD Queue Pair", sizeof(*qp),
190 RTE_CACHE_LINE_SIZE, socket_id);
195 dev->data->queue_pairs[qp_id] = qp;
197 if (zlib_pmd_qp_set_unique_name(dev, qp))
198 goto qp_setup_cleanup;
200 qp->processed_pkts = zlib_pmd_qp_create_processed_pkts_ring(qp,
201 max_inflight_ops, socket_id);
202 if (qp->processed_pkts == NULL)
203 goto qp_setup_cleanup;
205 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
216 /** Configure stream */
218 zlib_pmd_stream_create(struct rte_compressdev *dev,
219 const struct rte_comp_xform *xform,
223 struct zlib_stream *stream;
224 struct zlib_private *internals = dev->data->dev_private;
227 ZLIB_PMD_ERR("invalid xform struct");
231 if (rte_mempool_get(internals->mp, zstream)) {
232 ZLIB_PMD_ERR("Couldn't get object from session mempool");
235 stream = *((struct zlib_stream **)zstream);
237 ret = zlib_set_stream_parameters(xform, stream);
240 ZLIB_PMD_ERR("failed configure session parameters");
242 memset(stream, 0, sizeof(struct zlib_stream));
243 /* Return session to mempool */
244 rte_mempool_put(internals->mp, stream);
251 /** Configure private xform */
253 zlib_pmd_private_xform_create(struct rte_compressdev *dev,
254 const struct rte_comp_xform *xform,
255 void **private_xform)
257 return zlib_pmd_stream_create(dev, xform, private_xform);
260 /** Clear the memory of stream so it doesn't leave key material behind */
262 zlib_pmd_stream_free(__rte_unused struct rte_compressdev *dev,
265 struct zlib_stream *stream = (struct zlib_stream *)zstream;
269 stream->free(&stream->strm);
270 /* Zero out the whole structure */
271 memset(stream, 0, sizeof(struct zlib_stream));
272 struct rte_mempool *mp = rte_mempool_from_obj(stream);
273 rte_mempool_put(mp, stream);
278 /** Clear the memory of stream so it doesn't leave key material behind */
280 zlib_pmd_private_xform_free(struct rte_compressdev *dev,
283 return zlib_pmd_stream_free(dev, private_xform);
286 struct rte_compressdev_ops zlib_pmd_ops = {
287 .dev_configure = zlib_pmd_config,
288 .dev_start = zlib_pmd_start,
289 .dev_stop = zlib_pmd_stop,
290 .dev_close = zlib_pmd_close,
292 .stats_get = zlib_pmd_stats_get,
293 .stats_reset = zlib_pmd_stats_reset,
295 .dev_infos_get = zlib_pmd_info_get,
297 .queue_pair_setup = zlib_pmd_qp_setup,
298 .queue_pair_release = zlib_pmd_qp_release,
300 .private_xform_create = zlib_pmd_private_xform_create,
301 .private_xform_free = zlib_pmd_private_xform_free,
303 .stream_create = NULL,
307 struct rte_compressdev_ops *rte_zlib_pmd_ops = &zlib_pmd_ops;