1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 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 /* No limit of number of sessions */
134 dev_info->sym.max_nb_sessions = 0;
135 dev_info->feature_flags = dev->feature_flags;
136 dev_info->capabilities = zuc_pmd_capabilities;
140 /** Release queue pair */
142 zuc_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
144 if (dev->data->queue_pairs[qp_id] != NULL) {
145 struct zuc_qp *qp = dev->data->queue_pairs[qp_id];
147 if (qp->processed_ops)
148 rte_ring_free(qp->processed_ops);
150 rte_free(dev->data->queue_pairs[qp_id]);
151 dev->data->queue_pairs[qp_id] = NULL;
156 /** set a unique name for the queue pair based on its name, dev_id and qp_id */
158 zuc_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
161 unsigned n = snprintf(qp->name, sizeof(qp->name),
163 dev->data->dev_id, qp->id);
165 if (n >= sizeof(qp->name))
171 /** Create a ring to place processed ops on */
172 static struct rte_ring *
173 zuc_pmd_qp_create_processed_ops_ring(struct zuc_qp *qp,
174 unsigned ring_size, int socket_id)
178 r = rte_ring_lookup(qp->name);
180 if (rte_ring_get_size(r) >= ring_size) {
181 ZUC_LOG(INFO, "Reusing existing ring %s"
182 " for processed packets",
187 ZUC_LOG(ERR, "Unable to reuse existing ring %s"
188 " for processed packets",
193 return rte_ring_create(qp->name, ring_size, socket_id,
194 RING_F_SP_ENQ | RING_F_SC_DEQ);
197 /** Setup a queue pair */
199 zuc_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
200 const struct rte_cryptodev_qp_conf *qp_conf,
203 struct zuc_qp *qp = NULL;
205 /* Free memory prior to re-allocation if needed. */
206 if (dev->data->queue_pairs[qp_id] != NULL)
207 zuc_pmd_qp_release(dev, qp_id);
209 /* Allocate the queue pair data structure. */
210 qp = rte_zmalloc_socket("ZUC PMD Queue Pair", sizeof(*qp),
211 RTE_CACHE_LINE_SIZE, socket_id);
216 dev->data->queue_pairs[qp_id] = qp;
218 if (zuc_pmd_qp_set_unique_name(dev, qp))
219 goto qp_setup_cleanup;
221 qp->processed_ops = zuc_pmd_qp_create_processed_ops_ring(qp,
222 qp_conf->nb_descriptors, socket_id);
223 if (qp->processed_ops == NULL)
224 goto qp_setup_cleanup;
226 qp->sess_mp = qp_conf->mp_session;
227 qp->sess_mp_priv = qp_conf->mp_session_private;
229 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
240 /** Return the number of allocated queue pairs */
242 zuc_pmd_qp_count(struct rte_cryptodev *dev)
244 return dev->data->nb_queue_pairs;
247 /** Returns the size of the ZUC session structure */
249 zuc_pmd_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
251 return sizeof(struct zuc_session);
254 /** Configure a ZUC session from a crypto xform chain */
256 zuc_pmd_sym_session_configure(struct rte_cryptodev *dev __rte_unused,
257 struct rte_crypto_sym_xform *xform,
258 struct rte_cryptodev_sym_session *sess,
259 struct rte_mempool *mempool)
261 void *sess_private_data;
264 if (unlikely(sess == NULL)) {
265 ZUC_LOG(ERR, "invalid session struct");
269 if (rte_mempool_get(mempool, &sess_private_data)) {
271 "Couldn't get object from session mempool");
276 ret = zuc_set_session_parameters(sess_private_data, xform);
278 ZUC_LOG(ERR, "failed configure session parameters");
280 /* Return session to mempool */
281 rte_mempool_put(mempool, sess_private_data);
285 set_sym_session_private_data(sess, dev->driver_id,
291 /** Clear the memory of session so it doesn't leave key material behind */
293 zuc_pmd_sym_session_clear(struct rte_cryptodev *dev,
294 struct rte_cryptodev_sym_session *sess)
296 uint8_t index = dev->driver_id;
297 void *sess_priv = get_sym_session_private_data(sess, index);
299 /* Zero out the whole structure */
301 memset(sess_priv, 0, sizeof(struct zuc_session));
302 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
303 set_sym_session_private_data(sess, index, NULL);
304 rte_mempool_put(sess_mp, sess_priv);
308 struct rte_cryptodev_ops zuc_pmd_ops = {
309 .dev_configure = zuc_pmd_config,
310 .dev_start = zuc_pmd_start,
311 .dev_stop = zuc_pmd_stop,
312 .dev_close = zuc_pmd_close,
314 .stats_get = zuc_pmd_stats_get,
315 .stats_reset = zuc_pmd_stats_reset,
317 .dev_infos_get = zuc_pmd_info_get,
319 .queue_pair_setup = zuc_pmd_qp_setup,
320 .queue_pair_release = zuc_pmd_qp_release,
321 .queue_pair_count = zuc_pmd_qp_count,
323 .sym_session_get_size = zuc_pmd_sym_session_get_size,
324 .sym_session_configure = zuc_pmd_sym_session_configure,
325 .sym_session_clear = zuc_pmd_sym_session_clear
328 struct rte_cryptodev_ops *rte_zuc_pmd_ops = &zuc_pmd_ops;