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 "snow3g_pmd_private.h"
13 static const struct rte_cryptodev_capabilities snow3g_pmd_capabilities[] = {
14 { /* SNOW 3G (UIA2) */
15 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
17 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
19 .algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2,
39 { /* SNOW 3G (UEA2) */
40 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
42 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
44 .algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2,
59 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
62 /** Configure device */
64 snow3g_pmd_config(__rte_unused struct rte_cryptodev *dev,
65 __rte_unused struct rte_cryptodev_config *config)
72 snow3g_pmd_start(__rte_unused struct rte_cryptodev *dev)
79 snow3g_pmd_stop(__rte_unused struct rte_cryptodev *dev)
85 snow3g_pmd_close(__rte_unused struct rte_cryptodev *dev)
91 /** Get device statistics */
93 snow3g_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 snow3g_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 snow3g_pmd_stats_reset(struct rte_cryptodev *dev)
115 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
116 struct snow3g_qp *qp = dev->data->queue_pairs[qp_id];
118 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
123 /** Get device info */
125 snow3g_pmd_info_get(struct rte_cryptodev *dev,
126 struct rte_cryptodev_info *dev_info)
128 struct snow3g_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 = snow3g_pmd_capabilities;
140 /** Release queue pair */
142 snow3g_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
144 if (dev->data->queue_pairs[qp_id] != NULL) {
145 struct snow3g_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 snow3g_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
159 struct snow3g_qp *qp)
161 unsigned n = snprintf(qp->name, sizeof(qp->name),
162 "snow3g_pmd_%u_qp_%u",
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 snow3g_pmd_qp_create_processed_ops_ring(struct snow3g_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 SNOW3G_LOG(INFO, "Reusing existing ring %s"
182 " for processed packets",
187 SNOW3G_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 snow3g_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
200 const struct rte_cryptodev_qp_conf *qp_conf,
203 struct snow3g_qp *qp = NULL;
204 struct snow3g_private *internals = dev->data->dev_private;
206 /* Free memory prior to re-allocation if needed. */
207 if (dev->data->queue_pairs[qp_id] != NULL)
208 snow3g_pmd_qp_release(dev, qp_id);
210 /* Allocate the queue pair data structure. */
211 qp = rte_zmalloc_socket("SNOW 3G PMD Queue Pair", sizeof(*qp),
212 RTE_CACHE_LINE_SIZE, socket_id);
217 dev->data->queue_pairs[qp_id] = qp;
219 if (snow3g_pmd_qp_set_unique_name(dev, qp))
220 goto qp_setup_cleanup;
222 qp->processed_ops = snow3g_pmd_qp_create_processed_ops_ring(qp,
223 qp_conf->nb_descriptors, socket_id);
224 if (qp->processed_ops == NULL)
225 goto qp_setup_cleanup;
227 qp->mgr = internals->mgr;
228 qp->sess_mp = qp_conf->mp_session;
229 qp->sess_mp_priv = qp_conf->mp_session_private;
231 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
242 /** Returns the size of the SNOW 3G session structure */
244 snow3g_pmd_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
246 return sizeof(struct snow3g_session);
249 /** Configure a SNOW 3G session from a crypto xform chain */
251 snow3g_pmd_sym_session_configure(struct rte_cryptodev *dev,
252 struct rte_crypto_sym_xform *xform,
253 struct rte_cryptodev_sym_session *sess,
254 struct rte_mempool *mempool)
256 void *sess_private_data;
258 struct snow3g_private *internals = dev->data->dev_private;
260 if (unlikely(sess == NULL)) {
261 SNOW3G_LOG(ERR, "invalid session struct");
265 if (rte_mempool_get(mempool, &sess_private_data)) {
267 "Couldn't get object from session mempool");
271 ret = snow3g_set_session_parameters(internals->mgr,
272 sess_private_data, xform);
274 SNOW3G_LOG(ERR, "failed configure session parameters");
276 /* Return session to mempool */
277 rte_mempool_put(mempool, sess_private_data);
281 set_sym_session_private_data(sess, dev->driver_id,
287 /** Clear the memory of session so it doesn't leave key material behind */
289 snow3g_pmd_sym_session_clear(struct rte_cryptodev *dev,
290 struct rte_cryptodev_sym_session *sess)
292 uint8_t index = dev->driver_id;
293 void *sess_priv = get_sym_session_private_data(sess, index);
295 /* Zero out the whole structure */
297 memset(sess_priv, 0, sizeof(struct snow3g_session));
298 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
299 set_sym_session_private_data(sess, index, NULL);
300 rte_mempool_put(sess_mp, sess_priv);
304 struct rte_cryptodev_ops snow3g_pmd_ops = {
305 .dev_configure = snow3g_pmd_config,
306 .dev_start = snow3g_pmd_start,
307 .dev_stop = snow3g_pmd_stop,
308 .dev_close = snow3g_pmd_close,
310 .stats_get = snow3g_pmd_stats_get,
311 .stats_reset = snow3g_pmd_stats_reset,
313 .dev_infos_get = snow3g_pmd_info_get,
315 .queue_pair_setup = snow3g_pmd_qp_setup,
316 .queue_pair_release = snow3g_pmd_qp_release,
318 .sym_session_get_size = snow3g_pmd_sym_session_get_size,
319 .sym_session_configure = snow3g_pmd_sym_session_configure,
320 .sym_session_clear = snow3g_pmd_sym_session_clear
323 struct rte_cryptodev_ops *rte_snow3g_pmd_ops = &snow3g_pmd_ops;