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 #define CCP_BASE_SYM_CRYPTO_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_SHA3_224, \
121 { /* SHA3-224 HMAC*/ \
122 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
124 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
126 .algo = RTE_CRYPTO_AUTH_SHA3_224_HMAC, \
143 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
145 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
147 .algo = RTE_CRYPTO_AUTH_SHA256, \
163 { /* SHA256 HMAC */ \
164 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
166 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
168 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC, \
185 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
187 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
189 .algo = RTE_CRYPTO_AUTH_SHA3_256, \
205 { /* SHA3-256-HMAC */ \
206 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
208 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
210 .algo = RTE_CRYPTO_AUTH_SHA3_256_HMAC, \
227 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
229 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
231 .algo = RTE_CRYPTO_AUTH_SHA384, \
247 { /* SHA384 HMAC */ \
248 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
250 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
252 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC, \
269 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
271 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
273 .algo = RTE_CRYPTO_AUTH_SHA3_384, \
289 { /* SHA3-384-HMAC */ \
290 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
292 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
294 .algo = RTE_CRYPTO_AUTH_SHA3_384_HMAC, \
311 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
313 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
315 .algo = RTE_CRYPTO_AUTH_SHA512, \
331 { /* SHA512 HMAC */ \
332 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
334 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
336 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC, \
353 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
355 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
357 .algo = RTE_CRYPTO_AUTH_SHA3_512, \
373 { /* SHA3-512-HMAC */ \
374 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
376 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
378 .algo = RTE_CRYPTO_AUTH_SHA3_512_HMAC, \
395 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
397 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
399 .algo = RTE_CRYPTO_AUTH_AES_CMAC, \
415 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
417 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, \
419 .algo = RTE_CRYPTO_CIPHER_AES_ECB, \
435 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
437 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, \
439 .algo = RTE_CRYPTO_CIPHER_AES_CBC, \
455 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
457 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, \
459 .algo = RTE_CRYPTO_CIPHER_AES_CTR, \
475 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
477 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, \
479 .algo = RTE_CRYPTO_CIPHER_3DES_CBC, \
495 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
497 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD, \
499 .algo = RTE_CRYPTO_AEAD_AES_GCM, \
525 #define CCP_EXTRA_SYM_CRYPTO_CAPABILITIES \
527 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, \
529 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, \
531 .algo = RTE_CRYPTO_AUTH_MD5_HMAC, \
548 static const struct rte_cryptodev_capabilities ccp_crypto_cap[] = {
549 CCP_BASE_SYM_CRYPTO_CAPABILITIES,
550 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
553 static const struct rte_cryptodev_capabilities ccp_crypto_cap_complete[] = {
554 CCP_EXTRA_SYM_CRYPTO_CAPABILITIES,
555 CCP_BASE_SYM_CRYPTO_CAPABILITIES,
556 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
560 ccp_pmd_config(struct rte_cryptodev *dev __rte_unused,
561 struct rte_cryptodev_config *config __rte_unused)
567 ccp_pmd_start(struct rte_cryptodev *dev)
569 return ccp_dev_start(dev);
573 ccp_pmd_stop(struct rte_cryptodev *dev __rte_unused)
579 ccp_pmd_close(struct rte_cryptodev *dev __rte_unused)
585 ccp_pmd_stats_get(struct rte_cryptodev *dev,
586 struct rte_cryptodev_stats *stats)
590 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
591 struct ccp_qp *qp = dev->data->queue_pairs[qp_id];
593 stats->enqueued_count += qp->qp_stats.enqueued_count;
594 stats->dequeued_count += qp->qp_stats.dequeued_count;
596 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count;
597 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count;
603 ccp_pmd_stats_reset(struct rte_cryptodev *dev)
607 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
608 struct ccp_qp *qp = dev->data->queue_pairs[qp_id];
610 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
615 ccp_pmd_info_get(struct rte_cryptodev *dev,
616 struct rte_cryptodev_info *dev_info)
618 struct ccp_private *internals = dev->data->dev_private;
620 if (dev_info != NULL) {
621 dev_info->driver_id = dev->driver_id;
622 dev_info->feature_flags = dev->feature_flags;
623 dev_info->capabilities = ccp_crypto_cap;
624 if (internals->auth_opt == 1)
625 dev_info->capabilities = ccp_crypto_cap_complete;
626 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
627 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
632 ccp_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
636 if (dev->data->queue_pairs[qp_id] != NULL) {
637 qp = (struct ccp_qp *)dev->data->queue_pairs[qp_id];
638 rte_ring_free(qp->processed_pkts);
639 rte_mempool_free(qp->batch_mp);
641 dev->data->queue_pairs[qp_id] = NULL;
647 ccp_pmd_qp_set_unique_name(struct rte_cryptodev *dev,
650 unsigned int n = snprintf(qp->name, sizeof(qp->name),
652 dev->data->dev_id, qp->id);
654 if (n > sizeof(qp->name))
660 static struct rte_ring *
661 ccp_pmd_qp_create_batch_info_ring(struct ccp_qp *qp,
662 unsigned int ring_size, int socket_id)
666 r = rte_ring_lookup(qp->name);
668 if (r->size >= ring_size) {
670 "Reusing ring %s for processed packets",
675 "Unable to reuse ring %s for processed packets",
680 return rte_ring_create(qp->name, ring_size, socket_id,
681 RING_F_SP_ENQ | RING_F_SC_DEQ);
685 ccp_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
686 const struct rte_cryptodev_qp_conf *qp_conf,
687 int socket_id, struct rte_mempool *session_pool)
689 struct ccp_private *internals = dev->data->dev_private;
693 if (qp_id >= internals->max_nb_qpairs) {
694 CCP_LOG_ERR("Invalid qp_id %u, should be less than %u",
695 qp_id, internals->max_nb_qpairs);
699 /* Free memory prior to re-allocation if needed. */
700 if (dev->data->queue_pairs[qp_id] != NULL)
701 ccp_pmd_qp_release(dev, qp_id);
703 /* Allocate the queue pair data structure. */
704 qp = rte_zmalloc_socket("CCP Crypto PMD Queue Pair", sizeof(*qp),
705 RTE_CACHE_LINE_SIZE, socket_id);
707 CCP_LOG_ERR("Failed to allocate queue pair memory");
713 dev->data->queue_pairs[qp_id] = qp;
715 retval = ccp_pmd_qp_set_unique_name(dev, qp);
717 CCP_LOG_ERR("Failed to create unique name for ccp qp");
718 goto qp_setup_cleanup;
721 qp->processed_pkts = ccp_pmd_qp_create_batch_info_ring(qp,
722 qp_conf->nb_descriptors, socket_id);
723 if (qp->processed_pkts == NULL) {
724 CCP_LOG_ERR("Failed to create batch info ring");
725 goto qp_setup_cleanup;
728 qp->sess_mp = session_pool;
730 /* mempool for batch info */
731 qp->batch_mp = rte_mempool_create(
733 qp_conf->nb_descriptors,
734 sizeof(struct ccp_batch_info),
736 0, NULL, NULL, NULL, NULL,
738 if (qp->batch_mp == NULL)
739 goto qp_setup_cleanup;
740 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats));
744 dev->data->queue_pairs[qp_id] = NULL;
751 ccp_pmd_qp_start(struct rte_cryptodev *dev __rte_unused,
752 uint16_t queue_pair_id __rte_unused)
758 ccp_pmd_qp_stop(struct rte_cryptodev *dev __rte_unused,
759 uint16_t queue_pair_id __rte_unused)
765 ccp_pmd_qp_count(struct rte_cryptodev *dev)
767 return dev->data->nb_queue_pairs;
771 ccp_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused)
773 return sizeof(struct ccp_session);
777 ccp_pmd_session_configure(struct rte_cryptodev *dev,
778 struct rte_crypto_sym_xform *xform,
779 struct rte_cryptodev_sym_session *sess,
780 struct rte_mempool *mempool)
783 void *sess_private_data;
784 struct ccp_private *internals;
786 if (unlikely(sess == NULL || xform == NULL)) {
787 CCP_LOG_ERR("Invalid session struct or xform");
791 if (rte_mempool_get(mempool, &sess_private_data)) {
792 CCP_LOG_ERR("Couldn't get object from session mempool");
795 internals = (struct ccp_private *)dev->data->dev_private;
796 ret = ccp_set_session_parameters(sess_private_data, xform, internals);
798 CCP_LOG_ERR("failed configure session parameters");
800 /* Return session to mempool */
801 rte_mempool_put(mempool, sess_private_data);
804 set_session_private_data(sess, dev->driver_id,
811 ccp_pmd_session_clear(struct rte_cryptodev *dev,
812 struct rte_cryptodev_sym_session *sess)
814 uint8_t index = dev->driver_id;
815 void *sess_priv = get_session_private_data(sess, index);
818 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
820 rte_mempool_put(sess_mp, sess_priv);
821 memset(sess_priv, 0, sizeof(struct ccp_session));
822 set_session_private_data(sess, index, NULL);
826 struct rte_cryptodev_ops ccp_ops = {
827 .dev_configure = ccp_pmd_config,
828 .dev_start = ccp_pmd_start,
829 .dev_stop = ccp_pmd_stop,
830 .dev_close = ccp_pmd_close,
832 .stats_get = ccp_pmd_stats_get,
833 .stats_reset = ccp_pmd_stats_reset,
835 .dev_infos_get = ccp_pmd_info_get,
837 .queue_pair_setup = ccp_pmd_qp_setup,
838 .queue_pair_release = ccp_pmd_qp_release,
839 .queue_pair_start = ccp_pmd_qp_start,
840 .queue_pair_stop = ccp_pmd_qp_stop,
841 .queue_pair_count = ccp_pmd_qp_count,
843 .session_get_size = ccp_pmd_session_get_size,
844 .session_configure = ccp_pmd_session_configure,
845 .session_clear = ccp_pmd_session_clear,
848 struct rte_cryptodev_ops *ccp_pmd_ops = &ccp_ops;