1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Marvell International Ltd.
3 * Copyright(c) 2017 Semihalf.
9 #include <rte_common.h>
10 #include <rte_malloc.h>
11 #include <rte_cryptodev_pmd.h>
13 #include "rte_mrvl_pmd_private.h"
16 * Capabilities list to be used in reporting to DPDK.
18 static const struct rte_cryptodev_capabilities
19 mrvl_crypto_pmd_capabilities[] = {
21 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
23 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
25 .algo = RTE_CRYPTO_AUTH_MD5_HMAC,
41 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
43 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
45 .algo = RTE_CRYPTO_AUTH_MD5,
61 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
63 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
65 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
81 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
83 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
85 .algo = RTE_CRYPTO_AUTH_SHA1,
102 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
104 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
106 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
122 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
124 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
126 .algo = RTE_CRYPTO_AUTH_SHA224,
142 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
144 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
146 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
162 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
164 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
166 .algo = RTE_CRYPTO_AUTH_SHA256,
182 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
184 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
186 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
202 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
204 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
206 .algo = RTE_CRYPTO_AUTH_SHA384,
222 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
224 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
226 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
242 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
244 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
246 .algo = RTE_CRYPTO_AUTH_SHA512,
262 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
264 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
266 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
282 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
284 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
286 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
302 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
304 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
306 .algo = RTE_CRYPTO_AEAD_AES_GCM,
331 { /* AES GMAC (AUTH) */
332 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
334 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
336 .algo = RTE_CRYPTO_AUTH_AES_GMAC,
357 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
359 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
361 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
377 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
379 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
381 .algo = RTE_CRYPTO_CIPHER_3DES_CTR,
397 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
402 * Configure device (PMD ops callback).
404 * @param dev Pointer to the device structure.
405 * @param config Pointer to configuration structure.
406 * @returns 0. Always.
409 mrvl_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
410 __rte_unused struct rte_cryptodev_config *config)
416 * Start device (PMD ops callback).
418 * @param dev Pointer to the device structure.
419 * @returns 0. Always.
422 mrvl_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
428 * Stop device (PMD ops callback).
430 * @param dev Pointer to the device structure.
431 * @returns 0. Always.
434 mrvl_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
439 * Get device statistics (PMD ops callback).
441 * @param dev Pointer to the device structure.
442 * @param stats Pointer to statistics structure [out].
445 mrvl_crypto_pmd_stats_get(struct rte_cryptodev *dev,
446 struct rte_cryptodev_stats *stats)
450 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
451 struct mrvl_crypto_qp *qp = dev->data->queue_pairs[qp_id];
453 stats->enqueued_count += qp->stats.enqueued_count;
454 stats->dequeued_count += qp->stats.dequeued_count;
456 stats->enqueue_err_count += qp->stats.enqueue_err_count;
457 stats->dequeue_err_count += qp->stats.dequeue_err_count;
462 * Reset device statistics (PMD ops callback).
464 * @param dev Pointer to the device structure.
467 mrvl_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
471 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
472 struct mrvl_crypto_qp *qp = dev->data->queue_pairs[qp_id];
474 memset(&qp->stats, 0, sizeof(qp->stats));
479 * Get device info (PMD ops callback).
481 * @param dev Pointer to the device structure.
482 * @param dev_info Pointer to the device info structure [out].
485 mrvl_crypto_pmd_info_get(struct rte_cryptodev *dev,
486 struct rte_cryptodev_info *dev_info)
488 struct mrvl_crypto_private *internals = dev->data->dev_private;
490 if (dev_info != NULL) {
491 dev_info->driver_id = dev->driver_id;
492 dev_info->feature_flags = dev->feature_flags;
493 dev_info->capabilities = mrvl_crypto_pmd_capabilities;
494 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
495 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
500 * Release queue pair (PMD ops callback).
502 * @param dev Pointer to the device structure.
503 * @param qp_id ID of Queue Pair to release.
504 * @returns 0. Always.
507 mrvl_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
509 struct mrvl_crypto_qp *qp =
510 (struct mrvl_crypto_qp *)dev->data->queue_pairs[qp_id];
512 if (dev->data->queue_pairs[qp_id] != NULL) {
513 sam_cio_flush(qp->cio);
514 sam_cio_deinit(qp->cio);
515 rte_free(dev->data->queue_pairs[qp_id]);
516 dev->data->queue_pairs[qp_id] = NULL;
523 * Close device (PMD ops callback).
525 * @param dev Pointer to the device structure.
526 * @returns 0. Always.
529 mrvl_crypto_pmd_close(struct rte_cryptodev *dev)
533 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++)
534 mrvl_crypto_pmd_qp_release(dev, qp_id);
540 * Setup a queue pair (PMD ops callback).
542 * @param dev Pointer to the device structure.
543 * @param qp_id ID of the Queue Pair.
544 * @param qp_conf Queue pair configuration (nb of descriptors).
545 * @param socket_id NUMA socket to allocate memory on.
546 * @returns 0 upon success, negative value otherwise.
549 mrvl_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
550 const struct rte_cryptodev_qp_conf *qp_conf,
551 int socket_id, struct rte_mempool *session_pool)
553 struct mrvl_crypto_qp *qp = NULL;
554 char match[RTE_CRYPTODEV_NAME_MAX_LEN];
557 /* Allocate the queue pair data structure. */
558 qp = rte_zmalloc_socket("MRVL Crypto PMD Queue Pair", sizeof(*qp),
559 RTE_CACHE_LINE_SIZE, socket_id);
563 /* Free old qp prior setup if needed. */
564 if (dev->data->queue_pairs[qp_id] != NULL)
565 mrvl_crypto_pmd_qp_release(dev, qp_id);
567 do { /* Error handling block */
570 * This extra check is necessary due to a bug in
573 int num = sam_get_num_inst();
575 MRVL_CRYPTO_LOG_ERR("No crypto engines detected.\n");
580 * In case two crypto engines are enabled qps will
581 * be evenly spread among them. Even and odd qps will
582 * be handled by cio-0 and cio-1 respectively. qp-cio mapping
583 * will look as follows:
586 * cio-x:y: cio-0:0, cio-1:0, cio-0:1, cio-1:1
589 * cio-x:y: cio-0:2, cio-1:2, cio-0:3, cio-1:3
591 * In case just one engine is enabled mapping will look as
594 * cio-x:y: cio-0:0, cio-0:1, cio-0:2, cio-0:3
596 n = snprintf(match, sizeof(match), "cio-%u:%u",
597 qp_id % num, qp_id / num);
599 if (n >= sizeof(match))
602 qp->cio_params.match = match;
603 qp->cio_params.size = qp_conf->nb_descriptors;
605 if (sam_cio_init(&qp->cio_params, &qp->cio) < 0)
608 qp->sess_mp = session_pool;
610 memset(&qp->stats, 0, sizeof(qp->stats));
611 dev->data->queue_pairs[qp_id] = qp;
619 /** Return the number of allocated queue pairs (PMD ops callback).
621 * @param dev Pointer to the device structure.
622 * @returns Number of allocated queue pairs.
625 mrvl_crypto_pmd_qp_count(struct rte_cryptodev *dev)
627 return dev->data->nb_queue_pairs;
630 /** Returns the size of the session structure (PMD ops callback).
632 * @param dev Pointer to the device structure [Unused].
633 * @returns Size of Marvell crypto session.
636 mrvl_crypto_pmd_sym_session_get_size(__rte_unused struct rte_cryptodev *dev)
638 return sizeof(struct mrvl_crypto_session);
641 /** Configure the session from a crypto xform chain (PMD ops callback).
643 * @param dev Pointer to the device structure.
644 * @param xform Pointer to the crytpo configuration structure.
645 * @param sess Pointer to the empty session structure.
646 * @returns 0 upon success, negative value otherwise.
649 mrvl_crypto_pmd_sym_session_configure(__rte_unused struct rte_cryptodev *dev,
650 struct rte_crypto_sym_xform *xform,
651 struct rte_cryptodev_sym_session *sess,
652 struct rte_mempool *mp)
654 struct mrvl_crypto_session *mrvl_sess;
655 void *sess_private_data;
659 MRVL_CRYPTO_LOG_ERR("Invalid session struct.");
663 if (rte_mempool_get(mp, &sess_private_data)) {
664 CDEV_LOG_ERR("Couldn't get object from session mempool.");
668 ret = mrvl_crypto_set_session_parameters(sess_private_data, xform);
670 MRVL_CRYPTO_LOG_ERR("Failed to configure session parameters.");
672 /* Return session to mempool */
673 rte_mempool_put(mp, sess_private_data);
677 set_sym_session_private_data(sess, dev->driver_id, sess_private_data);
679 mrvl_sess = (struct mrvl_crypto_session *)sess_private_data;
680 if (sam_session_create(&mrvl_sess->sam_sess_params,
681 &mrvl_sess->sam_sess) < 0) {
682 MRVL_CRYPTO_LOG_DBG("Failed to create session!");
690 * Clear the memory of session so it doesn't leave key material behind.
692 * @param dev Pointer to the device structure.
693 * @returns 0. Always.
696 mrvl_crypto_pmd_sym_session_clear(struct rte_cryptodev *dev,
697 struct rte_cryptodev_sym_session *sess)
700 uint8_t index = dev->driver_id;
701 void *sess_priv = get_sym_session_private_data(sess, index);
703 /* Zero out the whole structure */
705 struct mrvl_crypto_session *mrvl_sess =
706 (struct mrvl_crypto_session *)sess_priv;
708 if (mrvl_sess->sam_sess &&
709 sam_session_destroy(mrvl_sess->sam_sess) < 0) {
710 MRVL_CRYPTO_LOG_INFO("Error while destroying session!");
713 memset(sess, 0, sizeof(struct mrvl_crypto_session));
714 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
715 set_sym_session_private_data(sess, index, NULL);
716 rte_mempool_put(sess_mp, sess_priv);
721 * PMD handlers for crypto ops.
723 static struct rte_cryptodev_ops mrvl_crypto_pmd_ops = {
724 .dev_configure = mrvl_crypto_pmd_config,
725 .dev_start = mrvl_crypto_pmd_start,
726 .dev_stop = mrvl_crypto_pmd_stop,
727 .dev_close = mrvl_crypto_pmd_close,
729 .dev_infos_get = mrvl_crypto_pmd_info_get,
731 .stats_get = mrvl_crypto_pmd_stats_get,
732 .stats_reset = mrvl_crypto_pmd_stats_reset,
734 .queue_pair_setup = mrvl_crypto_pmd_qp_setup,
735 .queue_pair_release = mrvl_crypto_pmd_qp_release,
736 .queue_pair_count = mrvl_crypto_pmd_qp_count,
738 .sym_session_get_size = mrvl_crypto_pmd_sym_session_get_size,
739 .sym_session_configure = mrvl_crypto_pmd_sym_session_configure,
740 .sym_session_clear = mrvl_crypto_pmd_sym_session_clear
743 struct rte_cryptodev_ops *rte_mrvl_crypto_pmd_ops = &mrvl_crypto_pmd_ops;