1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
7 #include <rte_common.h>
8 #include <rte_cryptodev_pmd.h>
9 #include <rte_malloc.h>
11 #include "ccp_pmd_private.h"
13 #include "ccp_crypto.h"
15 static const struct rte_cryptodev_capabilities ccp_pmd_capabilities[] = {
17 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
19 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
21 .algo = RTE_CRYPTO_AUTH_SHA1,
38 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
40 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
42 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
59 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
61 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
63 .algo = RTE_CRYPTO_AUTH_SHA224,
80 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
82 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
84 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
101 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
103 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
105 .algo = RTE_CRYPTO_AUTH_SHA256,
122 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
124 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
126 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
143 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
145 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
147 .algo = RTE_CRYPTO_AUTH_SHA384,
164 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
166 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
168 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
185 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
187 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
189 .algo = RTE_CRYPTO_AUTH_SHA512,
206 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
208 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
210 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
227 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
229 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
231 .algo = RTE_CRYPTO_AUTH_AES_CMAC,
247 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
249 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
251 .algo = RTE_CRYPTO_CIPHER_AES_ECB,
267 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
269 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
271 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
287 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
289 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
291 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
307 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
309 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
311 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
327 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
329 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
331 .algo = RTE_CRYPTO_AEAD_AES_GCM,
356 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
360 ccp_pmd_config(struct rte_cryptodev *dev __rte_unused,
361 struct rte_cryptodev_config *config __rte_unused)
367 ccp_pmd_start(struct rte_cryptodev *dev)
369 return ccp_dev_start(dev);
373 ccp_pmd_stop(struct rte_cryptodev *dev __rte_unused)
379 ccp_pmd_close(struct rte_cryptodev *dev __rte_unused)
385 ccp_pmd_stats_get(struct rte_cryptodev *dev,
386 struct rte_cryptodev_stats *stats)
390 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
391 struct ccp_qp *qp = dev->data->queue_pairs[qp_id];
393 stats->enqueued_count += qp->qp_stats.enqueued_count;
394 stats->dequeued_count += qp->qp_stats.dequeued_count;
396 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
397 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
403 ccp_pmd_stats_reset(struct rte_cryptodev *dev)
407 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
408 struct ccp_qp *qp = dev->data->queue_pairs[qp_id];
410 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
415 ccp_pmd_info_get(struct rte_cryptodev *dev,
416 struct rte_cryptodev_info *dev_info)
418 struct ccp_private *internals = dev->data->dev_private;
420 if (dev_info != NULL) {
421 dev_info->driver_id = dev->driver_id;
422 dev_info->feature_flags = dev->feature_flags;
423 dev_info->capabilities = ccp_pmd_capabilities;
424 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
425 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
430 ccp_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
434 if (dev->data->queue_pairs[qp_id] != NULL) {
435 qp = (struct ccp_qp *)dev->data->queue_pairs[qp_id];
436 rte_ring_free(qp->processed_pkts);
437 rte_mempool_free(qp->batch_mp);
439 dev->data->queue_pairs[qp_id] = NULL;
445 ccp_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
448 unsigned int n = snprintf(qp->name, sizeof(qp->name),
450 dev->data->dev_id, qp->id);
452 if (n > sizeof(qp->name))
458 static struct rte_ring *
459 ccp_pmd_qp_create_batch_info_ring(struct ccp_qp *qp,
460 unsigned int ring_size, int socket_id)
464 r = rte_ring_lookup(qp->name);
466 if (r->size >= ring_size) {
468 "Reusing ring %s for processed packets",
473 "Unable to reuse ring %s for processed packets",
478 return rte_ring_create(qp->name, ring_size, socket_id,
479 RING_F_SP_ENQ | RING_F_SC_DEQ);
483 ccp_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
484 const struct rte_cryptodev_qp_conf *qp_conf,
485 int socket_id, struct rte_mempool *session_pool)
487 struct ccp_private *internals = dev->data->dev_private;
491 if (qp_id >= internals->max_nb_qpairs) {
492 CCP_LOG_ERR("Invalid qp_id %u, should be less than %u",
493 qp_id, internals->max_nb_qpairs);
497 /* Free memory prior to re-allocation if needed. */
498 if (dev->data->queue_pairs[qp_id] != NULL)
499 ccp_pmd_qp_release(dev, qp_id);
501 /* Allocate the queue pair data structure. */
502 qp = rte_zmalloc_socket("CCP Crypto PMD Queue Pair", sizeof(*qp),
503 RTE_CACHE_LINE_SIZE, socket_id);
505 CCP_LOG_ERR("Failed to allocate queue pair memory");
511 dev->data->queue_pairs[qp_id] = qp;
513 retval = ccp_pmd_qp_set_unique_name(dev, qp);
515 CCP_LOG_ERR("Failed to create unique name for ccp qp");
516 goto qp_setup_cleanup;
519 qp->processed_pkts = ccp_pmd_qp_create_batch_info_ring(qp,
520 qp_conf->nb_descriptors, socket_id);
521 if (qp->processed_pkts == NULL) {
522 CCP_LOG_ERR("Failed to create batch info ring");
523 goto qp_setup_cleanup;
526 qp->sess_mp = session_pool;
528 /* mempool for batch info */
529 qp->batch_mp = rte_mempool_create(
531 qp_conf->nb_descriptors,
532 sizeof(struct ccp_batch_info),
534 0, NULL, NULL, NULL, NULL,
536 if (qp->batch_mp == NULL)
537 goto qp_setup_cleanup;
538 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
542 dev->data->queue_pairs[qp_id] = NULL;
549 ccp_pmd_qp_start(struct rte_cryptodev *dev __rte_unused,
550 uint16_t queue_pair_id __rte_unused)
556 ccp_pmd_qp_stop(struct rte_cryptodev *dev __rte_unused,
557 uint16_t queue_pair_id __rte_unused)
563 ccp_pmd_qp_count(struct rte_cryptodev *dev)
565 return dev->data->nb_queue_pairs;
569 ccp_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
571 return sizeof(struct ccp_session);
575 ccp_pmd_session_configure(struct rte_cryptodev *dev,
576 struct rte_crypto_sym_xform *xform,
577 struct rte_cryptodev_sym_session *sess,
578 struct rte_mempool *mempool)
581 void *sess_private_data;
583 if (unlikely(sess == NULL || xform == NULL)) {
584 CCP_LOG_ERR("Invalid session struct or xform");
588 if (rte_mempool_get(mempool, &sess_private_data)) {
589 CCP_LOG_ERR("Couldn't get object from session mempool");
592 ret = ccp_set_session_parameters(sess_private_data, xform);
594 CCP_LOG_ERR("failed configure session parameters");
596 /* Return session to mempool */
597 rte_mempool_put(mempool, sess_private_data);
600 set_session_private_data(sess, dev->driver_id,
607 ccp_pmd_session_clear(struct rte_cryptodev *dev,
608 struct rte_cryptodev_sym_session *sess)
610 uint8_t index = dev->driver_id;
611 void *sess_priv = get_session_private_data(sess, index);
614 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
616 rte_mempool_put(sess_mp, sess_priv);
617 memset(sess_priv, 0, sizeof(struct ccp_session));
618 set_session_private_data(sess, index, NULL);
622 struct rte_cryptodev_ops ccp_ops = {
623 .dev_configure = ccp_pmd_config,
624 .dev_start = ccp_pmd_start,
625 .dev_stop = ccp_pmd_stop,
626 .dev_close = ccp_pmd_close,
628 .stats_get = ccp_pmd_stats_get,
629 .stats_reset = ccp_pmd_stats_reset,
631 .dev_infos_get = ccp_pmd_info_get,
633 .queue_pair_setup = ccp_pmd_qp_setup,
634 .queue_pair_release = ccp_pmd_qp_release,
635 .queue_pair_start = ccp_pmd_qp_start,
636 .queue_pair_stop = ccp_pmd_qp_stop,
637 .queue_pair_count = ccp_pmd_qp_count,
639 .session_get_size = ccp_pmd_session_get_size,
640 .session_configure = ccp_pmd_session_configure,
641 .session_clear = ccp_pmd_session_clear,
644 struct rte_cryptodev_ops *ccp_pmd_ops = &ccp_ops;