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,
101 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
103 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
105 .algo = RTE_CRYPTO_AUTH_SHA224,
121 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
123 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
125 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
141 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
143 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
145 .algo = RTE_CRYPTO_AUTH_SHA256,
161 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
163 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
165 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
181 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
183 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
185 .algo = RTE_CRYPTO_AUTH_SHA384,
201 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
203 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
205 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
221 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
223 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
225 .algo = RTE_CRYPTO_AUTH_SHA512,
241 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
243 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
245 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
261 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
263 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
265 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
281 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
283 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
285 .algo = RTE_CRYPTO_AEAD_AES_GCM,
310 { /* AES GMAC (AUTH) */
311 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
313 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
315 .algo = RTE_CRYPTO_AUTH_AES_GMAC,
336 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
338 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
340 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
356 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
358 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
360 .algo = RTE_CRYPTO_CIPHER_3DES_CTR,
376 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
381 * Configure device (PMD ops callback).
383 * @param dev Pointer to the device structure.
384 * @param config Pointer to configuration structure.
385 * @returns 0. Always.
388 mrvl_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev,
389 __rte_unused struct rte_cryptodev_config *config)
395 * Start device (PMD ops callback).
397 * @param dev Pointer to the device structure.
398 * @returns 0. Always.
401 mrvl_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev)
407 * Stop device (PMD ops callback).
409 * @param dev Pointer to the device structure.
410 * @returns 0. Always.
413 mrvl_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev)
418 * Get device statistics (PMD ops callback).
420 * @param dev Pointer to the device structure.
421 * @param stats Pointer to statistics structure [out].
424 mrvl_crypto_pmd_stats_get(struct rte_cryptodev *dev,
425 struct rte_cryptodev_stats *stats)
429 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
430 struct mrvl_crypto_qp *qp = dev->data->queue_pairs[qp_id];
432 stats->enqueued_count += qp->stats.enqueued_count;
433 stats->dequeued_count += qp->stats.dequeued_count;
435 stats->enqueue_err_count += qp->stats.enqueue_err_count;
436 stats->dequeue_err_count += qp->stats.dequeue_err_count;
441 * Reset device statistics (PMD ops callback).
443 * @param dev Pointer to the device structure.
446 mrvl_crypto_pmd_stats_reset(struct rte_cryptodev *dev)
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 memset(&qp->stats, 0, sizeof(qp->stats));
458 * Get device info (PMD ops callback).
460 * @param dev Pointer to the device structure.
461 * @param dev_info Pointer to the device info structure [out].
464 mrvl_crypto_pmd_info_get(struct rte_cryptodev *dev,
465 struct rte_cryptodev_info *dev_info)
467 struct mrvl_crypto_private *internals = dev->data->dev_private;
469 if (dev_info != NULL) {
470 dev_info->driver_id = dev->driver_id;
471 dev_info->feature_flags = dev->feature_flags;
472 dev_info->capabilities = mrvl_crypto_pmd_capabilities;
473 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs;
474 dev_info->sym.max_nb_sessions = internals->max_nb_sessions;
479 * Release queue pair (PMD ops callback).
481 * @param dev Pointer to the device structure.
482 * @param qp_id ID of Queue Pair to release.
483 * @returns 0. Always.
486 mrvl_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id)
488 struct mrvl_crypto_qp *qp =
489 (struct mrvl_crypto_qp *)dev->data->queue_pairs[qp_id];
491 if (dev->data->queue_pairs[qp_id] != NULL) {
492 sam_cio_flush(qp->cio);
493 sam_cio_deinit(qp->cio);
494 rte_free(dev->data->queue_pairs[qp_id]);
495 dev->data->queue_pairs[qp_id] = NULL;
502 * Close device (PMD ops callback).
504 * @param dev Pointer to the device structure.
505 * @returns 0. Always.
508 mrvl_crypto_pmd_close(struct rte_cryptodev *dev)
512 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++)
513 mrvl_crypto_pmd_qp_release(dev, qp_id);
519 * Setup a queue pair (PMD ops callback).
521 * @param dev Pointer to the device structure.
522 * @param qp_id ID of the Queue Pair.
523 * @param qp_conf Queue pair configuration (nb of descriptors).
524 * @param socket_id NUMA socket to allocate memory on.
525 * @returns 0 upon success, negative value otherwise.
528 mrvl_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
529 const struct rte_cryptodev_qp_conf *qp_conf,
530 int socket_id, struct rte_mempool *session_pool)
532 struct mrvl_crypto_qp *qp = NULL;
533 char match[RTE_CRYPTODEV_NAME_MAX_LEN];
536 /* Allocate the queue pair data structure. */
537 qp = rte_zmalloc_socket("MRVL Crypto PMD Queue Pair", sizeof(*qp),
538 RTE_CACHE_LINE_SIZE, socket_id);
542 /* Free old qp prior setup if needed. */
543 if (dev->data->queue_pairs[qp_id] != NULL)
544 mrvl_crypto_pmd_qp_release(dev, qp_id);
546 do { /* Error handling block */
549 * This extra check is necessary due to a bug in
552 int num = sam_get_num_inst();
554 MRVL_CRYPTO_LOG_ERR("No crypto engines detected.\n");
559 * In case two crypto engines are enabled qps will
560 * be evenly spread among them. Even and odd qps will
561 * be handled by cio-0 and cio-1 respectively. qp-cio mapping
562 * will look as follows:
565 * cio-x:y: cio-0:0, cio-1:0, cio-0:1, cio-1:1
568 * cio-x:y: cio-0:2, cio-1:2, cio-0:3, cio-1:3
570 * In case just one engine is enabled mapping will look as
573 * cio-x:y: cio-0:0, cio-0:1, cio-0:2, cio-0:3
575 n = snprintf(match, sizeof(match), "cio-%u:%u",
576 qp_id % num, qp_id / num);
578 if (n >= sizeof(match))
581 qp->cio_params.match = match;
582 qp->cio_params.size = qp_conf->nb_descriptors;
584 if (sam_cio_init(&qp->cio_params, &qp->cio) < 0)
587 qp->sess_mp = session_pool;
589 memset(&qp->stats, 0, sizeof(qp->stats));
590 dev->data->queue_pairs[qp_id] = qp;
598 /** Return the number of allocated queue pairs (PMD ops callback).
600 * @param dev Pointer to the device structure.
601 * @returns Number of allocated queue pairs.
604 mrvl_crypto_pmd_qp_count(struct rte_cryptodev *dev)
606 return dev->data->nb_queue_pairs;
609 /** Returns the size of the session structure (PMD ops callback).
611 * @param dev Pointer to the device structure [Unused].
612 * @returns Size of Marvell crypto session.
615 mrvl_crypto_pmd_sym_session_get_size(__rte_unused struct rte_cryptodev *dev)
617 return sizeof(struct mrvl_crypto_session);
620 /** Configure the session from a crypto xform chain (PMD ops callback).
622 * @param dev Pointer to the device structure.
623 * @param xform Pointer to the crytpo configuration structure.
624 * @param sess Pointer to the empty session structure.
625 * @returns 0 upon success, negative value otherwise.
628 mrvl_crypto_pmd_sym_session_configure(__rte_unused struct rte_cryptodev *dev,
629 struct rte_crypto_sym_xform *xform,
630 struct rte_cryptodev_sym_session *sess,
631 struct rte_mempool *mp)
633 struct mrvl_crypto_session *mrvl_sess;
634 void *sess_private_data;
638 MRVL_CRYPTO_LOG_ERR("Invalid session struct.");
642 if (rte_mempool_get(mp, &sess_private_data)) {
643 CDEV_LOG_ERR("Couldn't get object from session mempool.");
647 ret = mrvl_crypto_set_session_parameters(sess_private_data, xform);
649 MRVL_CRYPTO_LOG_ERR("Failed to configure session parameters.");
651 /* Return session to mempool */
652 rte_mempool_put(mp, sess_private_data);
656 set_sym_session_private_data(sess, dev->driver_id, sess_private_data);
658 mrvl_sess = (struct mrvl_crypto_session *)sess_private_data;
659 if (sam_session_create(&mrvl_sess->sam_sess_params,
660 &mrvl_sess->sam_sess) < 0) {
661 MRVL_CRYPTO_LOG_DBG("Failed to create session!");
669 * Clear the memory of session so it doesn't leave key material behind.
671 * @param dev Pointer to the device structure.
672 * @returns 0. Always.
675 mrvl_crypto_pmd_sym_session_clear(struct rte_cryptodev *dev,
676 struct rte_cryptodev_sym_session *sess)
679 uint8_t index = dev->driver_id;
680 void *sess_priv = get_sym_session_private_data(sess, index);
682 /* Zero out the whole structure */
684 struct mrvl_crypto_session *mrvl_sess =
685 (struct mrvl_crypto_session *)sess_priv;
687 if (mrvl_sess->sam_sess &&
688 sam_session_destroy(mrvl_sess->sam_sess) < 0) {
689 MRVL_CRYPTO_LOG_INFO("Error while destroying session!");
692 memset(sess, 0, sizeof(struct mrvl_crypto_session));
693 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
694 set_sym_session_private_data(sess, index, NULL);
695 rte_mempool_put(sess_mp, sess_priv);
700 * PMD handlers for crypto ops.
702 static struct rte_cryptodev_ops mrvl_crypto_pmd_ops = {
703 .dev_configure = mrvl_crypto_pmd_config,
704 .dev_start = mrvl_crypto_pmd_start,
705 .dev_stop = mrvl_crypto_pmd_stop,
706 .dev_close = mrvl_crypto_pmd_close,
708 .dev_infos_get = mrvl_crypto_pmd_info_get,
710 .stats_get = mrvl_crypto_pmd_stats_get,
711 .stats_reset = mrvl_crypto_pmd_stats_reset,
713 .queue_pair_setup = mrvl_crypto_pmd_qp_setup,
714 .queue_pair_release = mrvl_crypto_pmd_qp_release,
715 .queue_pair_count = mrvl_crypto_pmd_qp_count,
717 .sym_session_get_size = mrvl_crypto_pmd_sym_session_get_size,
718 .sym_session_configure = mrvl_crypto_pmd_sym_session_configure,
719 .sym_session_clear = mrvl_crypto_pmd_sym_session_clear
722 struct rte_cryptodev_ops *rte_mrvl_crypto_pmd_ops = &mrvl_crypto_pmd_ops;