1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_cryptodev_pmd.h>
11 #include "rte_zuc_pmd_private.h"
13 static const struct rte_cryptodev_capabilities zuc_pmd_capabilities[] = {
15 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
17 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
19 .algo = RTE_CRYPTO_AUTH_ZUC_EIA3,
40 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
42 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
44 .algo = RTE_CRYPTO_CIPHER_ZUC_EEA3,
59 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
62 /** Configure device */
64 zuc_pmd_config(__rte_unused struct rte_cryptodev *dev,
65 __rte_unused struct rte_cryptodev_config *config)
72 zuc_pmd_start(__rte_unused struct rte_cryptodev *dev)
79 zuc_pmd_stop(__rte_unused struct rte_cryptodev *dev)
85 zuc_pmd_close(__rte_unused struct rte_cryptodev *dev)
91 /** Get device statistics */
93 zuc_pmd_stats_get(struct rte_cryptodev *dev,
94 struct rte_cryptodev_stats *stats)
98 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
99 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
101 stats->enqueued_count += qp->qp_stats.enqueued_count;
102 stats->dequeued_count += qp->qp_stats.dequeued_count;
104 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
105 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
109 /** Reset device statistics */
111 zuc_pmd_stats_reset(struct rte_cryptodev *dev)
115 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
116 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
118 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
123 /** Get device info */
125 zuc_pmd_info_get(struct rte_cryptodev *dev,
126 struct rte_cryptodev_info *dev_info)
128 struct zuc_private *internals = dev->data->dev_private;
130 if (dev_info != NULL) {
131 dev_info->driver_id = dev->driver_id;
132 dev_info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
133 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
134 dev_info->feature_flags = dev->feature_flags;
135 dev_info->capabilities = zuc_pmd_capabilities;
139 /** Release queue pair */
141 zuc_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
143 if (dev->data->queue_pairs[qp_id] != NULL) {
144 rte_free(dev->data->queue_pairs[qp_id]);
145 dev->data->queue_pairs[qp_id] = NULL;
150 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
152 zuc_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
155 unsigned n = snprintf(qp->name, sizeof(qp->name),
157 dev->data->dev_id, qp->id);
159 if (n >= sizeof(qp->name))
165 /** Create a ring to place processed ops on */
166 static struct rte_ring *
167 zuc_pmd_qp_create_processed_ops_ring(struct zuc_qp *qp,
168 unsigned ring_size, int socket_id)
172 r = rte_ring_lookup(qp->name);
174 if (rte_ring_get_size(r) >= ring_size) {
175 ZUC_LOG_INFO("Reusing existing ring %s"
176 " for processed packets",
181 ZUC_LOG_ERR("Unable to reuse existing ring %s"
182 " for processed packets",
187 return rte_ring_create(qp->name, ring_size, socket_id,
188 RING_F_SP_ENQ | RING_F_SC_DEQ);
191 /** Setup a queue pair */
193 zuc_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
194 const struct rte_cryptodev_qp_conf *qp_conf,
195 int socket_id, struct rte_mempool *session_pool)
197 struct zuc_qp *qp = NULL;
199 /* Free memory prior to re-allocation if needed. */
200 if (dev->data->queue_pairs[qp_id] != NULL)
201 zuc_pmd_qp_release(dev, qp_id);
203 /* Allocate the queue pair data structure. */
204 qp = rte_zmalloc_socket("ZUC PMD Queue Pair", sizeof(*qp),
205 RTE_CACHE_LINE_SIZE, socket_id);
210 dev->data->queue_pairs[qp_id] = qp;
212 if (zuc_pmd_qp_set_unique_name(dev, qp))
213 goto qp_setup_cleanup;
215 qp->processed_ops = zuc_pmd_qp_create_processed_ops_ring(qp,
216 qp_conf->nb_descriptors, socket_id);
217 if (qp->processed_ops == NULL)
218 goto qp_setup_cleanup;
220 qp->sess_mp = session_pool;
222 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
233 /** Start queue pair */
235 zuc_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
236 __rte_unused uint16_t queue_pair_id)
241 /** Stop queue pair */
243 zuc_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
244 __rte_unused uint16_t queue_pair_id)
249 /** Return the number of allocated queue pairs */
251 zuc_pmd_qp_count(struct rte_cryptodev *dev)
253 return dev->data->nb_queue_pairs;
256 /** Returns the size of the ZUC session structure */
258 zuc_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
260 return sizeof(struct zuc_session);
263 /** Configure a ZUC session from a crypto xform chain */
265 zuc_pmd_session_configure(struct rte_cryptodev *dev __rte_unused,
266 struct rte_crypto_sym_xform *xform,
267 struct rte_cryptodev_sym_session *sess,
268 struct rte_mempool *mempool)
270 void *sess_private_data;
273 if (unlikely(sess == NULL)) {
274 ZUC_LOG_ERR("invalid session struct");
278 if (rte_mempool_get(mempool, &sess_private_data)) {
280 "Couldn't get object from session mempool");
284 ret = zuc_set_session_parameters(sess_private_data, xform);
286 ZUC_LOG_ERR("failed configure session parameters");
288 /* Return session to mempool */
289 rte_mempool_put(mempool, sess_private_data);
293 set_session_private_data(sess, dev->driver_id,
299 /** Clear the memory of session so it doesn't leave key material behind */
301 zuc_pmd_session_clear(struct rte_cryptodev *dev,
302 struct rte_cryptodev_sym_session *sess)
304 uint8_t index = dev->driver_id;
305 void *sess_priv = get_session_private_data(sess, index);
307 /* Zero out the whole structure */
309 memset(sess_priv, 0, sizeof(struct zuc_session));
310 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
311 set_session_private_data(sess, index, NULL);
312 rte_mempool_put(sess_mp, sess_priv);
316 struct rte_cryptodev_ops zuc_pmd_ops = {
317 .dev_configure = zuc_pmd_config,
318 .dev_start = zuc_pmd_start,
319 .dev_stop = zuc_pmd_stop,
320 .dev_close = zuc_pmd_close,
322 .stats_get = zuc_pmd_stats_get,
323 .stats_reset = zuc_pmd_stats_reset,
325 .dev_infos_get = zuc_pmd_info_get,
327 .queue_pair_setup = zuc_pmd_qp_setup,
328 .queue_pair_release = zuc_pmd_qp_release,
329 .queue_pair_start = zuc_pmd_qp_start,
330 .queue_pair_stop = zuc_pmd_qp_stop,
331 .queue_pair_count = zuc_pmd_qp_count,
333 .session_get_size = zuc_pmd_session_get_size,
334 .session_configure = zuc_pmd_session_configure,
335 .session_clear = zuc_pmd_session_clear
338 struct rte_cryptodev_ops *rte_zuc_pmd_ops = &zuc_pmd_ops;