1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Cavium, Inc
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_cryptodev_pmd.h>
11 #include "armv8_crypto_defs.h"
13 #include "rte_armv8_pmd_private.h"
15 static const struct rte_cryptodev_capabilities
16 armv8_crypto_pmd_capabilities[] = {
18 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
20 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
22 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
39 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
41 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
43 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
60 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
62 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
64 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
80 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
84 /** Configure device */
86 armv8_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
87 __rte_unused struct rte_cryptodev_config *config)
94 armv8_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
101 armv8_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
107 armv8_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev)
113 /** Get device statistics */
115 armv8_crypto_pmd_stats_get(struct rte_cryptodev *dev,
116 struct rte_cryptodev_stats *stats)
120 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
121 struct armv8_crypto_qp *qp = dev->data->queue_pairs[qp_id];
123 stats->enqueued_count += qp->stats.enqueued_count;
124 stats->dequeued_count += qp->stats.dequeued_count;
126 stats->enqueue_err_count += qp->stats.enqueue_err_count;
127 stats->dequeue_err_count += qp->stats.dequeue_err_count;
131 /** Reset device statistics */
133 armv8_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
137 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
138 struct armv8_crypto_qp *qp = dev->data->queue_pairs[qp_id];
140 memset(&qp->stats, 0, sizeof(qp->stats));
145 /** Get device info */
147 armv8_crypto_pmd_info_get(struct rte_cryptodev *dev,
148 struct rte_cryptodev_info *dev_info)
150 struct armv8_crypto_private *internals = dev->data->dev_private;
152 if (dev_info != NULL) {
153 dev_info->driver_id = dev->driver_id;
154 dev_info->feature_flags = dev->feature_flags;
155 dev_info->capabilities = armv8_crypto_pmd_capabilities;
156 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
157 /* No limit of number of sessions */
158 dev_info->sym.max_nb_sessions = 0;
162 /** Release queue pair */
164 armv8_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
167 if (dev->data->queue_pairs[qp_id] != NULL) {
168 rte_free(dev->data->queue_pairs[qp_id]);
169 dev->data->queue_pairs[qp_id] = NULL;
175 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */
177 armv8_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
178 struct armv8_crypto_qp *qp)
182 n = snprintf(qp->name, sizeof(qp->name), "armv8_crypto_pmd_%u_qp_%u",
183 dev->data->dev_id, qp->id);
185 if (n >= sizeof(qp->name))
192 /** Create a ring to place processed operations on */
193 static struct rte_ring *
194 armv8_crypto_pmd_qp_create_processed_ops_ring(struct armv8_crypto_qp *qp,
195 unsigned int ring_size, int socket_id)
199 r = rte_ring_lookup(qp->name);
201 if (rte_ring_get_size(r) >= ring_size) {
202 ARMV8_CRYPTO_LOG_INFO(
203 "Reusing existing ring %s for processed ops",
208 ARMV8_CRYPTO_LOG_ERR(
209 "Unable to reuse existing ring %s for processed ops",
214 return rte_ring_create(qp->name, ring_size, socket_id,
215 RING_F_SP_ENQ | RING_F_SC_DEQ);
219 /** Setup a queue pair */
221 armv8_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
222 const struct rte_cryptodev_qp_conf *qp_conf,
225 struct armv8_crypto_qp *qp = NULL;
227 /* Free memory prior to re-allocation if needed. */
228 if (dev->data->queue_pairs[qp_id] != NULL)
229 armv8_crypto_pmd_qp_release(dev, qp_id);
231 /* Allocate the queue pair data structure. */
232 qp = rte_zmalloc_socket("ARMv8 PMD Queue Pair", sizeof(*qp),
233 RTE_CACHE_LINE_SIZE, socket_id);
238 dev->data->queue_pairs[qp_id] = qp;
240 if (armv8_crypto_pmd_qp_set_unique_name(dev, qp) != 0)
241 goto qp_setup_cleanup;
243 qp->processed_ops = armv8_crypto_pmd_qp_create_processed_ops_ring(qp,
244 qp_conf->nb_descriptors, socket_id);
245 if (qp->processed_ops == NULL)
246 goto qp_setup_cleanup;
248 qp->sess_mp = qp_conf->mp_session;
249 qp->sess_mp_priv = qp_conf->mp_session_private;
251 memset(&qp->stats, 0, sizeof(qp->stats));
262 /** Return the number of allocated queue pairs */
264 armv8_crypto_pmd_qp_count(struct rte_cryptodev *dev)
266 return dev->data->nb_queue_pairs;
269 /** Returns the size of the session structure */
271 armv8_crypto_pmd_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
273 return sizeof(struct armv8_crypto_session);
276 /** Configure the session from a crypto xform chain */
278 armv8_crypto_pmd_sym_session_configure(struct rte_cryptodev *dev,
279 struct rte_crypto_sym_xform *xform,
280 struct rte_cryptodev_sym_session *sess,
281 struct rte_mempool *mempool)
283 void *sess_private_data;
286 if (unlikely(sess == NULL)) {
287 ARMV8_CRYPTO_LOG_ERR("invalid session struct");
291 if (rte_mempool_get(mempool, &sess_private_data)) {
293 "Couldn't get object from session mempool");
297 ret = armv8_crypto_set_session_parameters(sess_private_data, xform);
299 ARMV8_CRYPTO_LOG_ERR("failed configure session parameters");
301 /* Return session to mempool */
302 rte_mempool_put(mempool, sess_private_data);
306 set_sym_session_private_data(sess, dev->driver_id,
312 /** Clear the memory of session so it doesn't leave key material behind */
314 armv8_crypto_pmd_sym_session_clear(struct rte_cryptodev *dev,
315 struct rte_cryptodev_sym_session *sess)
317 uint8_t index = dev->driver_id;
318 void *sess_priv = get_sym_session_private_data(sess, index);
320 /* Zero out the whole structure */
322 memset(sess_priv, 0, sizeof(struct armv8_crypto_session));
323 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
324 set_sym_session_private_data(sess, index, NULL);
325 rte_mempool_put(sess_mp, sess_priv);
329 struct rte_cryptodev_ops armv8_crypto_pmd_ops = {
330 .dev_configure = armv8_crypto_pmd_config,
331 .dev_start = armv8_crypto_pmd_start,
332 .dev_stop = armv8_crypto_pmd_stop,
333 .dev_close = armv8_crypto_pmd_close,
335 .stats_get = armv8_crypto_pmd_stats_get,
336 .stats_reset = armv8_crypto_pmd_stats_reset,
338 .dev_infos_get = armv8_crypto_pmd_info_get,
340 .queue_pair_setup = armv8_crypto_pmd_qp_setup,
341 .queue_pair_release = armv8_crypto_pmd_qp_release,
342 .queue_pair_count = armv8_crypto_pmd_qp_count,
344 .sym_session_get_size = armv8_crypto_pmd_sym_session_get_size,
345 .sym_session_configure = armv8_crypto_pmd_sym_session_configure,
346 .sym_session_clear = armv8_crypto_pmd_sym_session_clear
349 struct rte_cryptodev_ops *rte_armv8_crypto_pmd_ops = &armv8_crypto_pmd_ops;