4 * Copyright (C) Semihalf 2017. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Semihalf nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_common.h>
36 #include <rte_malloc.h>
37 #include <rte_cryptodev_pmd.h>
39 #include "rte_mrvl_pmd_private.h"
42 * Capabilities list to be used in reporting to DPDK.
44 static const struct rte_cryptodev_capabilities
45 mrvl_crypto_pmd_capabilities[] = {
47 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
49 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
51 .algo = RTE_CRYPTO_AUTH_MD5_HMAC,
67 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
69 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
71 .algo = RTE_CRYPTO_AUTH_MD5,
87 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
89 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
91 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
107 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
109 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
111 .algo = RTE_CRYPTO_AUTH_SHA1,
127 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
129 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
131 .algo = RTE_CRYPTO_AUTH_SHA224,
147 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
149 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
151 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
167 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
169 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
171 .algo = RTE_CRYPTO_AUTH_SHA256,
187 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
189 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
191 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
207 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
209 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
211 .algo = RTE_CRYPTO_AUTH_SHA384,
227 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
229 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
231 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
247 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
249 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
251 .algo = RTE_CRYPTO_AUTH_SHA512,
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_AEAD,
311 .algo = RTE_CRYPTO_AEAD_AES_GCM,
336 { /* AES GMAC (AUTH) */
337 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
339 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
341 .algo = RTE_CRYPTO_AUTH_AES_GMAC,
362 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
364 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
366 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
382 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
384 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
386 .algo = RTE_CRYPTO_CIPHER_3DES_CTR,
402 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
407 * Configure device (PMD ops callback).
409 * @param dev Pointer to the device structure.
410 * @param config Pointer to configuration structure.
411 * @returns 0. Always.
414 mrvl_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
415 __rte_unused struct rte_cryptodev_config *config)
421 * Start device (PMD ops callback).
423 * @param dev Pointer to the device structure.
424 * @returns 0. Always.
427 mrvl_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
433 * Stop device (PMD ops callback).
435 * @param dev Pointer to the device structure.
436 * @returns 0. Always.
439 mrvl_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
444 * Get device statistics (PMD ops callback).
446 * @param dev Pointer to the device structure.
447 * @param stats Pointer to statistics structure [out].
450 mrvl_crypto_pmd_stats_get(struct rte_cryptodev *dev,
451 struct rte_cryptodev_stats *stats)
455 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
456 struct mrvl_crypto_qp *qp = dev->data->queue_pairs[qp_id];
458 stats->enqueued_count += qp->stats.enqueued_count;
459 stats->dequeued_count += qp->stats.dequeued_count;
461 stats->enqueue_err_count += qp->stats.enqueue_err_count;
462 stats->dequeue_err_count += qp->stats.dequeue_err_count;
467 * Reset device statistics (PMD ops callback).
469 * @param dev Pointer to the device structure.
472 mrvl_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
476 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
477 struct mrvl_crypto_qp *qp = dev->data->queue_pairs[qp_id];
479 memset(&qp->stats, 0, sizeof(qp->stats));
484 * Get device info (PMD ops callback).
486 * @param dev Pointer to the device structure.
487 * @param dev_info Pointer to the device info structure [out].
490 mrvl_crypto_pmd_info_get(struct rte_cryptodev *dev,
491 struct rte_cryptodev_info *dev_info)
493 struct mrvl_crypto_private *internals = dev->data->dev_private;
495 if (dev_info != NULL) {
496 dev_info->driver_id = dev->driver_id;
497 dev_info->feature_flags = dev->feature_flags;
498 dev_info->capabilities = mrvl_crypto_pmd_capabilities;
499 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
500 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
505 * Release queue pair (PMD ops callback).
507 * @param dev Pointer to the device structure.
508 * @param qp_id ID of Queue Pair to release.
509 * @returns 0. Always.
512 mrvl_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
514 struct mrvl_crypto_qp *qp =
515 (struct mrvl_crypto_qp *)dev->data->queue_pairs[qp_id];
517 if (dev->data->queue_pairs[qp_id] != NULL) {
518 sam_cio_flush(qp->cio);
519 sam_cio_deinit(qp->cio);
520 rte_free(dev->data->queue_pairs[qp_id]);
521 dev->data->queue_pairs[qp_id] = NULL;
528 * Close device (PMD ops callback).
530 * @param dev Pointer to the device structure.
531 * @returns 0. Always.
534 mrvl_crypto_pmd_close(struct rte_cryptodev *dev)
538 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++)
539 mrvl_crypto_pmd_qp_release(dev, qp_id);
545 * Setup a queue pair (PMD ops callback).
547 * @param dev Pointer to the device structure.
548 * @param qp_id ID of the Queue Pair.
549 * @param qp_conf Queue pair configuration (nb of descriptors).
550 * @param socket_id NUMA socket to allocate memory on.
551 * @returns 0 upon success, negative value otherwise.
554 mrvl_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
555 const struct rte_cryptodev_qp_conf *qp_conf,
556 int socket_id, struct rte_mempool *session_pool)
558 struct mrvl_crypto_qp *qp = NULL;
559 char match[RTE_CRYPTODEV_NAME_LEN];
562 /* Allocate the queue pair data structure. */
563 qp = rte_zmalloc_socket("MRVL Crypto PMD Queue Pair", sizeof(*qp),
564 RTE_CACHE_LINE_SIZE, socket_id);
568 /* Free old qp prior setup if needed. */
569 if (dev->data->queue_pairs[qp_id] != NULL)
570 mrvl_crypto_pmd_qp_release(dev, qp_id);
572 do { /* Error handling block */
575 * This extra check is necessary due to a bug in
578 int num = sam_get_num_inst();
580 MRVL_CRYPTO_LOG_ERR("No crypto engines detected.\n");
585 * In case two crypto engines are enabled qps will
586 * be evenly spread among them. Even and odd qps will
587 * be handled by cio-0 and cio-1 respectively. qp-cio mapping
588 * will look as follows:
591 * cio-x:y: cio-0:0, cio-1:0, cio-0:1, cio-1:1
594 * cio-x:y: cio-0:2, cio-1:2, cio-0:3, cio-1:3
596 * In case just one engine is enabled mapping will look as
599 * cio-x:y: cio-0:0, cio-0:1, cio-0:2, cio-0:3
601 n = snprintf(match, sizeof(match), "cio-%u:%u",
602 qp_id % num, qp_id / num);
604 if (n >= sizeof(match))
607 qp->cio_params.match = match;
608 qp->cio_params.size = qp_conf->nb_descriptors;
610 if (sam_cio_init(&qp->cio_params, &qp->cio) < 0)
613 qp->sess_mp = session_pool;
615 memset(&qp->stats, 0, sizeof(qp->stats));
616 dev->data->queue_pairs[qp_id] = qp;
624 /** Start queue pair (PMD ops callback) - not supported.
626 * @param dev Pointer to the device structure.
627 * @param qp_id ID of the Queue Pair.
628 * @returns -ENOTSUP. Always.
631 mrvl_crypto_pmd_qp_start(__rte_unused struct rte_cryptodev *dev,
632 __rte_unused uint16_t queue_pair_id)
637 /** Stop queue pair (PMD ops callback) - not supported.
639 * @param dev Pointer to the device structure.
640 * @param qp_id ID of the Queue Pair.
641 * @returns -ENOTSUP. Always.
644 mrvl_crypto_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev,
645 __rte_unused uint16_t queue_pair_id)
650 /** Return the number of allocated queue pairs (PMD ops callback).
652 * @param dev Pointer to the device structure.
653 * @returns Number of allocated queue pairs.
656 mrvl_crypto_pmd_qp_count(struct rte_cryptodev *dev)
658 return dev->data->nb_queue_pairs;
661 /** Returns the size of the session structure (PMD ops callback).
663 * @param dev Pointer to the device structure [Unused].
664 * @returns Size of Marvell crypto session.
667 mrvl_crypto_pmd_session_get_size(__rte_unused struct rte_cryptodev *dev)
669 return sizeof(struct mrvl_crypto_session);
672 /** Configure the session from a crypto xform chain (PMD ops callback).
674 * @param dev Pointer to the device structure.
675 * @param xform Pointer to the crytpo configuration structure.
676 * @param sess Pointer to the empty session structure.
677 * @returns 0 upon success, negative value otherwise.
680 mrvl_crypto_pmd_session_configure(__rte_unused struct rte_cryptodev *dev,
681 struct rte_crypto_sym_xform *xform,
682 struct rte_cryptodev_sym_session *sess,
683 struct rte_mempool *mp)
685 struct mrvl_crypto_session *mrvl_sess;
686 void *sess_private_data;
690 MRVL_CRYPTO_LOG_ERR("Invalid session struct.");
694 if (rte_mempool_get(mp, &sess_private_data)) {
695 CDEV_LOG_ERR("Couldn't get object from session mempool.");
699 ret = mrvl_crypto_set_session_parameters(sess_private_data, xform);
701 MRVL_CRYPTO_LOG_ERR("Failed to configure session parameters.");
703 /* Return session to mempool */
704 rte_mempool_put(mp, sess_private_data);
708 set_session_private_data(sess, dev->driver_id, sess_private_data);
710 mrvl_sess = (struct mrvl_crypto_session *)sess_private_data;
711 if (sam_session_create(&mrvl_sess->sam_sess_params,
712 &mrvl_sess->sam_sess) < 0) {
713 MRVL_CRYPTO_LOG_DBG("Failed to create session!");
721 * Clear the memory of session so it doesn't leave key material behind.
723 * @param dev Pointer to the device structure.
724 * @returns 0. Always.
727 mrvl_crypto_pmd_session_clear(struct rte_cryptodev *dev,
728 struct rte_cryptodev_sym_session *sess)
731 uint8_t index = dev->driver_id;
732 void *sess_priv = get_session_private_data(sess, index);
734 /* Zero out the whole structure */
736 struct mrvl_crypto_session *mrvl_sess =
737 (struct mrvl_crypto_session *)sess_priv;
739 if (mrvl_sess->sam_sess &&
740 sam_session_destroy(mrvl_sess->sam_sess) < 0) {
741 MRVL_CRYPTO_LOG_INFO("Error while destroying session!");
744 memset(sess, 0, sizeof(struct mrvl_crypto_session));
745 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
746 set_session_private_data(sess, index, NULL);
747 rte_mempool_put(sess_mp, sess_priv);
752 * PMD handlers for crypto ops.
754 static struct rte_cryptodev_ops mrvl_crypto_pmd_ops = {
755 .dev_configure = mrvl_crypto_pmd_config,
756 .dev_start = mrvl_crypto_pmd_start,
757 .dev_stop = mrvl_crypto_pmd_stop,
758 .dev_close = mrvl_crypto_pmd_close,
760 .dev_infos_get = mrvl_crypto_pmd_info_get,
762 .stats_get = mrvl_crypto_pmd_stats_get,
763 .stats_reset = mrvl_crypto_pmd_stats_reset,
765 .queue_pair_setup = mrvl_crypto_pmd_qp_setup,
766 .queue_pair_release = mrvl_crypto_pmd_qp_release,
767 .queue_pair_start = mrvl_crypto_pmd_qp_start,
768 .queue_pair_stop = mrvl_crypto_pmd_qp_stop,
769 .queue_pair_count = mrvl_crypto_pmd_qp_count,
771 .session_get_size = mrvl_crypto_pmd_session_get_size,
772 .session_configure = mrvl_crypto_pmd_session_configure,
773 .session_clear = mrvl_crypto_pmd_session_clear
776 struct rte_cryptodev_ops *rte_mrvl_crypto_pmd_ops = &mrvl_crypto_pmd_ops;