1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2020 Intel Corporation
16 #include <rte_byteorder.h>
18 #include <rte_debug.h>
20 #include <rte_interrupts.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_memzone.h>
24 #include <rte_launch.h>
25 #include <rte_tailq.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_atomic.h>
30 #include <rte_branch_prediction.h>
31 #include <rte_common.h>
32 #include <rte_mempool.h>
33 #include <rte_malloc.h>
35 #include <rte_errno.h>
36 #include <rte_spinlock.h>
37 #include <rte_string_fns.h>
39 #include "rte_crypto.h"
40 #include "rte_cryptodev.h"
41 #include "cryptodev_pmd.h"
42 #include "rte_cryptodev_trace.h"
44 static uint8_t nb_drivers;
46 static struct rte_cryptodev rte_crypto_devices[RTE_CRYPTO_MAX_DEVS];
48 struct rte_cryptodev *rte_cryptodevs = rte_crypto_devices;
50 static struct rte_cryptodev_global cryptodev_globals = {
51 .devs = rte_crypto_devices,
56 /* spinlock for crypto device callbacks */
57 static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
60 * The user application callback description.
62 * It contains callback address to be registered by user application,
63 * the pointer to the parameters for callback, and the event type.
65 struct rte_cryptodev_callback {
66 TAILQ_ENTRY(rte_cryptodev_callback) next; /**< Callbacks list */
67 rte_cryptodev_cb_fn cb_fn; /**< Callback address */
68 void *cb_arg; /**< Parameter for callback */
69 enum rte_cryptodev_event_type event; /**< Interrupt event type */
70 uint32_t active; /**< Callback is executing */
74 * The crypto cipher algorithm strings identifiers.
75 * It could be used in application command line.
78 rte_crypto_cipher_algorithm_strings[] = {
79 [RTE_CRYPTO_CIPHER_3DES_CBC] = "3des-cbc",
80 [RTE_CRYPTO_CIPHER_3DES_ECB] = "3des-ecb",
81 [RTE_CRYPTO_CIPHER_3DES_CTR] = "3des-ctr",
83 [RTE_CRYPTO_CIPHER_AES_CBC] = "aes-cbc",
84 [RTE_CRYPTO_CIPHER_AES_CTR] = "aes-ctr",
85 [RTE_CRYPTO_CIPHER_AES_DOCSISBPI] = "aes-docsisbpi",
86 [RTE_CRYPTO_CIPHER_AES_ECB] = "aes-ecb",
87 [RTE_CRYPTO_CIPHER_AES_F8] = "aes-f8",
88 [RTE_CRYPTO_CIPHER_AES_XTS] = "aes-xts",
90 [RTE_CRYPTO_CIPHER_ARC4] = "arc4",
92 [RTE_CRYPTO_CIPHER_DES_CBC] = "des-cbc",
93 [RTE_CRYPTO_CIPHER_DES_DOCSISBPI] = "des-docsisbpi",
95 [RTE_CRYPTO_CIPHER_NULL] = "null",
97 [RTE_CRYPTO_CIPHER_KASUMI_F8] = "kasumi-f8",
98 [RTE_CRYPTO_CIPHER_SNOW3G_UEA2] = "snow3g-uea2",
99 [RTE_CRYPTO_CIPHER_ZUC_EEA3] = "zuc-eea3"
103 * The crypto cipher operation strings identifiers.
104 * It could be used in application command line.
107 rte_crypto_cipher_operation_strings[] = {
108 [RTE_CRYPTO_CIPHER_OP_ENCRYPT] = "encrypt",
109 [RTE_CRYPTO_CIPHER_OP_DECRYPT] = "decrypt"
113 * The crypto auth algorithm strings identifiers.
114 * It could be used in application command line.
117 rte_crypto_auth_algorithm_strings[] = {
118 [RTE_CRYPTO_AUTH_AES_CBC_MAC] = "aes-cbc-mac",
119 [RTE_CRYPTO_AUTH_AES_CMAC] = "aes-cmac",
120 [RTE_CRYPTO_AUTH_AES_GMAC] = "aes-gmac",
121 [RTE_CRYPTO_AUTH_AES_XCBC_MAC] = "aes-xcbc-mac",
123 [RTE_CRYPTO_AUTH_MD5] = "md5",
124 [RTE_CRYPTO_AUTH_MD5_HMAC] = "md5-hmac",
126 [RTE_CRYPTO_AUTH_NULL] = "null",
128 [RTE_CRYPTO_AUTH_SHA1] = "sha1",
129 [RTE_CRYPTO_AUTH_SHA1_HMAC] = "sha1-hmac",
131 [RTE_CRYPTO_AUTH_SHA224] = "sha2-224",
132 [RTE_CRYPTO_AUTH_SHA224_HMAC] = "sha2-224-hmac",
133 [RTE_CRYPTO_AUTH_SHA256] = "sha2-256",
134 [RTE_CRYPTO_AUTH_SHA256_HMAC] = "sha2-256-hmac",
135 [RTE_CRYPTO_AUTH_SHA384] = "sha2-384",
136 [RTE_CRYPTO_AUTH_SHA384_HMAC] = "sha2-384-hmac",
137 [RTE_CRYPTO_AUTH_SHA512] = "sha2-512",
138 [RTE_CRYPTO_AUTH_SHA512_HMAC] = "sha2-512-hmac",
140 [RTE_CRYPTO_AUTH_KASUMI_F9] = "kasumi-f9",
141 [RTE_CRYPTO_AUTH_SNOW3G_UIA2] = "snow3g-uia2",
142 [RTE_CRYPTO_AUTH_ZUC_EIA3] = "zuc-eia3"
146 * The crypto AEAD algorithm strings identifiers.
147 * It could be used in application command line.
150 rte_crypto_aead_algorithm_strings[] = {
151 [RTE_CRYPTO_AEAD_AES_CCM] = "aes-ccm",
152 [RTE_CRYPTO_AEAD_AES_GCM] = "aes-gcm",
153 [RTE_CRYPTO_AEAD_CHACHA20_POLY1305] = "chacha20-poly1305"
157 * The crypto AEAD operation strings identifiers.
158 * It could be used in application command line.
161 rte_crypto_aead_operation_strings[] = {
162 [RTE_CRYPTO_AEAD_OP_ENCRYPT] = "encrypt",
163 [RTE_CRYPTO_AEAD_OP_DECRYPT] = "decrypt"
167 * Asymmetric crypto transform operation strings identifiers.
169 const char *rte_crypto_asym_xform_strings[] = {
170 [RTE_CRYPTO_ASYM_XFORM_NONE] = "none",
171 [RTE_CRYPTO_ASYM_XFORM_RSA] = "rsa",
172 [RTE_CRYPTO_ASYM_XFORM_MODEX] = "modexp",
173 [RTE_CRYPTO_ASYM_XFORM_MODINV] = "modinv",
174 [RTE_CRYPTO_ASYM_XFORM_DH] = "dh",
175 [RTE_CRYPTO_ASYM_XFORM_DSA] = "dsa",
176 [RTE_CRYPTO_ASYM_XFORM_ECDSA] = "ecdsa",
177 [RTE_CRYPTO_ASYM_XFORM_ECPM] = "ecpm",
181 * Asymmetric crypto operation strings identifiers.
183 const char *rte_crypto_asym_op_strings[] = {
184 [RTE_CRYPTO_ASYM_OP_ENCRYPT] = "encrypt",
185 [RTE_CRYPTO_ASYM_OP_DECRYPT] = "decrypt",
186 [RTE_CRYPTO_ASYM_OP_SIGN] = "sign",
187 [RTE_CRYPTO_ASYM_OP_VERIFY] = "verify",
188 [RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE] = "priv_key_generate",
189 [RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE] = "pub_key_generate",
190 [RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE] = "sharedsecret_compute",
194 * The private data structure stored in the session mempool private data.
196 struct rte_cryptodev_sym_session_pool_private_data {
198 /**< number of elements in sess_data array */
199 uint16_t user_data_sz;
200 /**< session user data will be placed after sess_data */
204 rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
205 const char *algo_string)
209 for (i = 1; i < RTE_DIM(rte_crypto_cipher_algorithm_strings); i++) {
210 if (strcmp(algo_string, rte_crypto_cipher_algorithm_strings[i]) == 0) {
211 *algo_enum = (enum rte_crypto_cipher_algorithm) i;
221 rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
222 const char *algo_string)
226 for (i = 1; i < RTE_DIM(rte_crypto_auth_algorithm_strings); i++) {
227 if (strcmp(algo_string, rte_crypto_auth_algorithm_strings[i]) == 0) {
228 *algo_enum = (enum rte_crypto_auth_algorithm) i;
238 rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
239 const char *algo_string)
243 for (i = 1; i < RTE_DIM(rte_crypto_aead_algorithm_strings); i++) {
244 if (strcmp(algo_string, rte_crypto_aead_algorithm_strings[i]) == 0) {
245 *algo_enum = (enum rte_crypto_aead_algorithm) i;
255 rte_cryptodev_asym_get_xform_enum(enum rte_crypto_asym_xform_type *xform_enum,
256 const char *xform_string)
260 for (i = 1; i < RTE_DIM(rte_crypto_asym_xform_strings); i++) {
261 if (strcmp(xform_string,
262 rte_crypto_asym_xform_strings[i]) == 0) {
263 *xform_enum = (enum rte_crypto_asym_xform_type) i;
273 * The crypto auth operation strings identifiers.
274 * It could be used in application command line.
277 rte_crypto_auth_operation_strings[] = {
278 [RTE_CRYPTO_AUTH_OP_VERIFY] = "verify",
279 [RTE_CRYPTO_AUTH_OP_GENERATE] = "generate"
282 const struct rte_cryptodev_symmetric_capability *
283 rte_cryptodev_sym_capability_get(uint8_t dev_id,
284 const struct rte_cryptodev_sym_capability_idx *idx)
286 const struct rte_cryptodev_capabilities *capability;
287 struct rte_cryptodev_info dev_info;
290 rte_cryptodev_info_get(dev_id, &dev_info);
292 while ((capability = &dev_info.capabilities[i++])->op !=
293 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
294 if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
297 if (capability->sym.xform_type != idx->type)
300 if (idx->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
301 capability->sym.auth.algo == idx->algo.auth)
302 return &capability->sym;
304 if (idx->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
305 capability->sym.cipher.algo == idx->algo.cipher)
306 return &capability->sym;
308 if (idx->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
309 capability->sym.aead.algo == idx->algo.aead)
310 return &capability->sym;
317 param_range_check(uint16_t size, const struct rte_crypto_param_range *range)
319 unsigned int next_size;
321 /* Check lower/upper bounds */
322 if (size < range->min)
325 if (size > range->max)
328 /* If range is actually only one value, size is correct */
329 if (range->increment == 0)
332 /* Check if value is one of the supported sizes */
333 for (next_size = range->min; next_size <= range->max;
334 next_size += range->increment)
335 if (size == next_size)
341 const struct rte_cryptodev_asymmetric_xform_capability *
342 rte_cryptodev_asym_capability_get(uint8_t dev_id,
343 const struct rte_cryptodev_asym_capability_idx *idx)
345 const struct rte_cryptodev_capabilities *capability;
346 struct rte_cryptodev_info dev_info;
349 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
350 rte_cryptodev_info_get(dev_id, &dev_info);
352 while ((capability = &dev_info.capabilities[i++])->op !=
353 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
354 if (capability->op != RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
357 if (capability->asym.xform_capa.xform_type == idx->type)
358 return &capability->asym.xform_capa;
364 rte_cryptodev_sym_capability_check_cipher(
365 const struct rte_cryptodev_symmetric_capability *capability,
366 uint16_t key_size, uint16_t iv_size)
368 if (param_range_check(key_size, &capability->cipher.key_size) != 0)
371 if (param_range_check(iv_size, &capability->cipher.iv_size) != 0)
378 rte_cryptodev_sym_capability_check_auth(
379 const struct rte_cryptodev_symmetric_capability *capability,
380 uint16_t key_size, uint16_t digest_size, uint16_t iv_size)
382 if (param_range_check(key_size, &capability->auth.key_size) != 0)
385 if (param_range_check(digest_size, &capability->auth.digest_size) != 0)
388 if (param_range_check(iv_size, &capability->auth.iv_size) != 0)
395 rte_cryptodev_sym_capability_check_aead(
396 const struct rte_cryptodev_symmetric_capability *capability,
397 uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
400 if (param_range_check(key_size, &capability->aead.key_size) != 0)
403 if (param_range_check(digest_size, &capability->aead.digest_size) != 0)
406 if (param_range_check(aad_size, &capability->aead.aad_size) != 0)
409 if (param_range_check(iv_size, &capability->aead.iv_size) != 0)
415 rte_cryptodev_asym_xform_capability_check_optype(
416 const struct rte_cryptodev_asymmetric_xform_capability *capability,
417 enum rte_crypto_asym_op_type op_type)
419 if (capability->op_types & (1 << op_type))
426 rte_cryptodev_asym_xform_capability_check_modlen(
427 const struct rte_cryptodev_asymmetric_xform_capability *capability,
430 /* no need to check for limits, if min or max = 0 */
431 if (capability->modlen.min != 0) {
432 if (modlen < capability->modlen.min)
436 if (capability->modlen.max != 0) {
437 if (modlen > capability->modlen.max)
441 /* in any case, check if given modlen is module increment */
442 if (capability->modlen.increment != 0) {
443 if (modlen % (capability->modlen.increment))
450 /* spinlock for crypto device enq callbacks */
451 static rte_spinlock_t rte_cryptodev_callback_lock = RTE_SPINLOCK_INITIALIZER;
454 cryptodev_cb_cleanup(struct rte_cryptodev *dev)
456 struct rte_cryptodev_cb_rcu *list;
457 struct rte_cryptodev_cb *cb, *next;
460 if (dev->enq_cbs == NULL && dev->deq_cbs == NULL)
463 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
464 list = &dev->enq_cbs[qp_id];
472 rte_free(list->qsbr);
475 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
476 list = &dev->deq_cbs[qp_id];
484 rte_free(list->qsbr);
487 rte_free(dev->enq_cbs);
489 rte_free(dev->deq_cbs);
494 cryptodev_cb_init(struct rte_cryptodev *dev)
496 struct rte_cryptodev_cb_rcu *list;
497 struct rte_rcu_qsbr *qsbr;
501 /* Max thread set to 1, as one DP thread accessing a queue-pair */
502 const uint32_t max_threads = 1;
504 dev->enq_cbs = rte_zmalloc(NULL,
505 sizeof(struct rte_cryptodev_cb_rcu) *
506 dev->data->nb_queue_pairs, 0);
507 if (dev->enq_cbs == NULL) {
508 CDEV_LOG_ERR("Failed to allocate memory for enq callbacks");
512 dev->deq_cbs = rte_zmalloc(NULL,
513 sizeof(struct rte_cryptodev_cb_rcu) *
514 dev->data->nb_queue_pairs, 0);
515 if (dev->deq_cbs == NULL) {
516 CDEV_LOG_ERR("Failed to allocate memory for deq callbacks");
517 rte_free(dev->enq_cbs);
521 /* Create RCU QSBR variable */
522 size = rte_rcu_qsbr_get_memsize(max_threads);
524 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
525 list = &dev->enq_cbs[qp_id];
526 qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
528 CDEV_LOG_ERR("Failed to allocate memory for RCU on "
529 "queue_pair_id=%d", qp_id);
533 if (rte_rcu_qsbr_init(qsbr, max_threads)) {
534 CDEV_LOG_ERR("Failed to initialize for RCU on "
535 "queue_pair_id=%d", qp_id);
542 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
543 list = &dev->deq_cbs[qp_id];
544 qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
546 CDEV_LOG_ERR("Failed to allocate memory for RCU on "
547 "queue_pair_id=%d", qp_id);
551 if (rte_rcu_qsbr_init(qsbr, max_threads)) {
552 CDEV_LOG_ERR("Failed to initialize for RCU on "
553 "queue_pair_id=%d", qp_id);
563 cryptodev_cb_cleanup(dev);
568 rte_cryptodev_get_feature_name(uint64_t flag)
571 case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
572 return "SYMMETRIC_CRYPTO";
573 case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
574 return "ASYMMETRIC_CRYPTO";
575 case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
576 return "SYM_OPERATION_CHAINING";
577 case RTE_CRYPTODEV_FF_CPU_SSE:
579 case RTE_CRYPTODEV_FF_CPU_AVX:
581 case RTE_CRYPTODEV_FF_CPU_AVX2:
583 case RTE_CRYPTODEV_FF_CPU_AVX512:
585 case RTE_CRYPTODEV_FF_CPU_AESNI:
587 case RTE_CRYPTODEV_FF_HW_ACCELERATED:
588 return "HW_ACCELERATED";
589 case RTE_CRYPTODEV_FF_IN_PLACE_SGL:
590 return "IN_PLACE_SGL";
591 case RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT:
592 return "OOP_SGL_IN_SGL_OUT";
593 case RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT:
594 return "OOP_SGL_IN_LB_OUT";
595 case RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT:
596 return "OOP_LB_IN_SGL_OUT";
597 case RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT:
598 return "OOP_LB_IN_LB_OUT";
599 case RTE_CRYPTODEV_FF_CPU_NEON:
601 case RTE_CRYPTODEV_FF_CPU_ARM_CE:
603 case RTE_CRYPTODEV_FF_SECURITY:
604 return "SECURITY_PROTOCOL";
605 case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP:
606 return "RSA_PRIV_OP_KEY_EXP";
607 case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT:
608 return "RSA_PRIV_OP_KEY_QT";
609 case RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED:
610 return "DIGEST_ENCRYPTED";
611 case RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO:
612 return "SYM_CPU_CRYPTO";
613 case RTE_CRYPTODEV_FF_ASYM_SESSIONLESS:
614 return "ASYM_SESSIONLESS";
615 case RTE_CRYPTODEV_FF_SYM_SESSIONLESS:
616 return "SYM_SESSIONLESS";
617 case RTE_CRYPTODEV_FF_NON_BYTE_ALIGNED_DATA:
618 return "NON_BYTE_ALIGNED_DATA";
619 case RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS:
620 return "CIPHER_MULTIPLE_DATA_UNITS";
621 case RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY:
622 return "CIPHER_WRAPPED_KEY";
628 struct rte_cryptodev *
629 rte_cryptodev_pmd_get_dev(uint8_t dev_id)
631 return &cryptodev_globals.devs[dev_id];
634 struct rte_cryptodev *
635 rte_cryptodev_pmd_get_named_dev(const char *name)
637 struct rte_cryptodev *dev;
643 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
644 dev = &cryptodev_globals.devs[i];
646 if ((dev->attached == RTE_CRYPTODEV_ATTACHED) &&
647 (strcmp(dev->data->name, name) == 0))
654 static inline uint8_t
655 rte_cryptodev_is_valid_device_data(uint8_t dev_id)
657 if (dev_id >= RTE_CRYPTO_MAX_DEVS ||
658 rte_crypto_devices[dev_id].data == NULL)
665 rte_cryptodev_is_valid_dev(uint8_t dev_id)
667 struct rte_cryptodev *dev = NULL;
669 if (!rte_cryptodev_is_valid_device_data(dev_id))
672 dev = rte_cryptodev_pmd_get_dev(dev_id);
673 if (dev->attached != RTE_CRYPTODEV_ATTACHED)
681 rte_cryptodev_get_dev_id(const char *name)
688 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
689 if (!rte_cryptodev_is_valid_device_data(i))
691 if ((strcmp(cryptodev_globals.devs[i].data->name, name)
693 (cryptodev_globals.devs[i].attached ==
694 RTE_CRYPTODEV_ATTACHED))
702 rte_cryptodev_count(void)
704 return cryptodev_globals.nb_devs;
708 rte_cryptodev_device_count_by_driver(uint8_t driver_id)
710 uint8_t i, dev_count = 0;
712 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++)
713 if (cryptodev_globals.devs[i].driver_id == driver_id &&
714 cryptodev_globals.devs[i].attached ==
715 RTE_CRYPTODEV_ATTACHED)
722 rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
725 uint8_t i, count = 0;
726 struct rte_cryptodev *devs = cryptodev_globals.devs;
728 for (i = 0; i < RTE_CRYPTO_MAX_DEVS && count < nb_devices; i++) {
729 if (!rte_cryptodev_is_valid_device_data(i))
732 if (devs[i].attached == RTE_CRYPTODEV_ATTACHED) {
735 cmp = strncmp(devs[i].device->driver->name,
737 strlen(driver_name) + 1);
740 devices[count++] = devs[i].data->dev_id;
748 rte_cryptodev_get_sec_ctx(uint8_t dev_id)
750 if (dev_id < RTE_CRYPTO_MAX_DEVS &&
751 (rte_crypto_devices[dev_id].feature_flags &
752 RTE_CRYPTODEV_FF_SECURITY))
753 return rte_crypto_devices[dev_id].security_ctx;
759 rte_cryptodev_socket_id(uint8_t dev_id)
761 struct rte_cryptodev *dev;
763 if (!rte_cryptodev_is_valid_dev(dev_id))
766 dev = rte_cryptodev_pmd_get_dev(dev_id);
768 return dev->data->socket_id;
772 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
775 char mz_name[RTE_MEMZONE_NAMESIZE];
776 const struct rte_memzone *mz;
779 /* generate memzone name */
780 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
781 if (n >= (int)sizeof(mz_name))
784 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
785 mz = rte_memzone_reserve(mz_name,
786 sizeof(struct rte_cryptodev_data),
788 CDEV_LOG_DEBUG("PRIMARY:reserved memzone for %s (%p)",
791 mz = rte_memzone_lookup(mz_name);
792 CDEV_LOG_DEBUG("SECONDARY:looked up memzone for %s (%p)",
800 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
801 memset(*data, 0, sizeof(struct rte_cryptodev_data));
807 rte_cryptodev_data_free(uint8_t dev_id, struct rte_cryptodev_data **data)
809 char mz_name[RTE_MEMZONE_NAMESIZE];
810 const struct rte_memzone *mz;
813 /* generate memzone name */
814 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
815 if (n >= (int)sizeof(mz_name))
818 mz = rte_memzone_lookup(mz_name);
822 RTE_ASSERT(*data == mz->addr);
825 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
826 CDEV_LOG_DEBUG("PRIMARY:free memzone of %s (%p)",
828 return rte_memzone_free(mz);
830 CDEV_LOG_DEBUG("SECONDARY:don't free memzone of %s (%p)",
838 rte_cryptodev_find_free_device_index(void)
842 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
843 if (rte_crypto_devices[dev_id].attached ==
844 RTE_CRYPTODEV_DETACHED)
847 return RTE_CRYPTO_MAX_DEVS;
850 struct rte_cryptodev *
851 rte_cryptodev_pmd_allocate(const char *name, int socket_id)
853 struct rte_cryptodev *cryptodev;
856 if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
857 CDEV_LOG_ERR("Crypto device with name %s already "
862 dev_id = rte_cryptodev_find_free_device_index();
863 if (dev_id == RTE_CRYPTO_MAX_DEVS) {
864 CDEV_LOG_ERR("Reached maximum number of crypto devices");
868 cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
870 if (cryptodev->data == NULL) {
871 struct rte_cryptodev_data **cryptodev_data =
872 &cryptodev_globals.data[dev_id];
874 int retval = rte_cryptodev_data_alloc(dev_id, cryptodev_data,
877 if (retval < 0 || *cryptodev_data == NULL)
880 cryptodev->data = *cryptodev_data;
882 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
883 strlcpy(cryptodev->data->name, name,
884 RTE_CRYPTODEV_NAME_MAX_LEN);
886 cryptodev->data->dev_id = dev_id;
887 cryptodev->data->socket_id = socket_id;
888 cryptodev->data->dev_started = 0;
889 CDEV_LOG_DEBUG("PRIMARY:init data");
892 CDEV_LOG_DEBUG("Data for %s: dev_id %d, socket %d, started %d",
893 cryptodev->data->name,
894 cryptodev->data->dev_id,
895 cryptodev->data->socket_id,
896 cryptodev->data->dev_started);
898 /* init user callbacks */
899 TAILQ_INIT(&(cryptodev->link_intr_cbs));
901 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
903 cryptodev_globals.nb_devs++;
910 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
915 if (cryptodev == NULL)
918 dev_id = cryptodev->data->dev_id;
920 /* Close device only if device operations have been set */
921 if (cryptodev->dev_ops) {
922 ret = rte_cryptodev_close(dev_id);
927 ret = rte_cryptodev_data_free(dev_id, &cryptodev_globals.data[dev_id]);
931 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
932 cryptodev_globals.nb_devs--;
937 rte_cryptodev_queue_pair_count(uint8_t dev_id)
939 struct rte_cryptodev *dev;
941 if (!rte_cryptodev_is_valid_device_data(dev_id)) {
942 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
946 dev = &rte_crypto_devices[dev_id];
947 return dev->data->nb_queue_pairs;
951 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
954 struct rte_cryptodev_info dev_info;
958 if ((dev == NULL) || (nb_qpairs < 1)) {
959 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
964 CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
965 nb_qpairs, dev->data->dev_id);
967 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
969 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
970 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
972 if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
973 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
974 nb_qpairs, dev->data->dev_id);
978 if (dev->data->queue_pairs == NULL) { /* first time configuration */
979 dev->data->queue_pairs = rte_zmalloc_socket(
980 "cryptodev->queue_pairs",
981 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
982 RTE_CACHE_LINE_SIZE, socket_id);
984 if (dev->data->queue_pairs == NULL) {
985 dev->data->nb_queue_pairs = 0;
986 CDEV_LOG_ERR("failed to get memory for qp meta data, "
991 } else { /* re-configure */
993 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
995 qp = dev->data->queue_pairs;
997 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
1000 for (i = nb_qpairs; i < old_nb_queues; i++) {
1001 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
1006 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
1007 RTE_CACHE_LINE_SIZE);
1009 CDEV_LOG_ERR("failed to realloc qp meta data,"
1010 " nb_queues %u", nb_qpairs);
1014 if (nb_qpairs > old_nb_queues) {
1015 uint16_t new_qs = nb_qpairs - old_nb_queues;
1017 memset(qp + old_nb_queues, 0,
1018 sizeof(qp[0]) * new_qs);
1021 dev->data->queue_pairs = qp;
1024 dev->data->nb_queue_pairs = nb_qpairs;
1029 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
1031 struct rte_cryptodev *dev;
1034 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1035 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1039 dev = &rte_crypto_devices[dev_id];
1041 if (dev->data->dev_started) {
1043 "device %d must be stopped to allow configuration", dev_id);
1047 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
1049 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1050 cryptodev_cb_cleanup(dev);
1051 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1053 /* Setup new number of queue pairs and reconfigure device. */
1054 diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
1057 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
1062 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1063 diag = cryptodev_cb_init(dev);
1064 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1066 CDEV_LOG_ERR("Callback init failed for dev_id=%d", dev_id);
1070 rte_cryptodev_trace_configure(dev_id, config);
1071 return (*dev->dev_ops->dev_configure)(dev, config);
1075 rte_cryptodev_start(uint8_t dev_id)
1077 struct rte_cryptodev *dev;
1080 CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
1082 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1083 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1087 dev = &rte_crypto_devices[dev_id];
1089 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1091 if (dev->data->dev_started != 0) {
1092 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
1097 diag = (*dev->dev_ops->dev_start)(dev);
1098 rte_cryptodev_trace_start(dev_id, diag);
1100 dev->data->dev_started = 1;
1108 rte_cryptodev_stop(uint8_t dev_id)
1110 struct rte_cryptodev *dev;
1112 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1113 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1117 dev = &rte_crypto_devices[dev_id];
1119 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1121 if (dev->data->dev_started == 0) {
1122 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
1127 (*dev->dev_ops->dev_stop)(dev);
1128 rte_cryptodev_trace_stop(dev_id);
1129 dev->data->dev_started = 0;
1133 rte_cryptodev_close(uint8_t dev_id)
1135 struct rte_cryptodev *dev;
1138 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1139 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1143 dev = &rte_crypto_devices[dev_id];
1145 /* Device must be stopped before it can be closed */
1146 if (dev->data->dev_started == 1) {
1147 CDEV_LOG_ERR("Device %u must be stopped before closing",
1152 /* We can't close the device if there are outstanding sessions in use */
1153 if (dev->data->session_pool != NULL) {
1154 if (!rte_mempool_full(dev->data->session_pool)) {
1155 CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
1156 "has sessions still in use, free "
1157 "all sessions before calling close",
1163 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
1164 retval = (*dev->dev_ops->dev_close)(dev);
1165 rte_cryptodev_trace_close(dev_id, retval);
1174 rte_cryptodev_get_qp_status(uint8_t dev_id, uint16_t queue_pair_id)
1176 struct rte_cryptodev *dev;
1178 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1179 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1183 dev = &rte_crypto_devices[dev_id];
1184 if (queue_pair_id >= dev->data->nb_queue_pairs) {
1185 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1188 void **qps = dev->data->queue_pairs;
1190 if (qps[queue_pair_id]) {
1191 CDEV_LOG_DEBUG("qp %d on dev %d is initialised",
1192 queue_pair_id, dev_id);
1196 CDEV_LOG_DEBUG("qp %d on dev %d is not initialised",
1197 queue_pair_id, dev_id);
1203 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
1204 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
1207 struct rte_cryptodev *dev;
1209 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1210 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1214 dev = &rte_crypto_devices[dev_id];
1215 if (queue_pair_id >= dev->data->nb_queue_pairs) {
1216 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1221 CDEV_LOG_ERR("qp_conf cannot be NULL\n");
1225 if ((qp_conf->mp_session && !qp_conf->mp_session_private) ||
1226 (!qp_conf->mp_session && qp_conf->mp_session_private)) {
1227 CDEV_LOG_ERR("Invalid mempools\n");
1231 if (qp_conf->mp_session) {
1232 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1233 uint32_t obj_size = qp_conf->mp_session->elt_size;
1234 uint32_t obj_priv_size = qp_conf->mp_session_private->elt_size;
1235 struct rte_cryptodev_sym_session s = {0};
1237 pool_priv = rte_mempool_get_priv(qp_conf->mp_session);
1238 if (!pool_priv || qp_conf->mp_session->private_data_size <
1239 sizeof(*pool_priv)) {
1240 CDEV_LOG_ERR("Invalid mempool\n");
1244 s.nb_drivers = pool_priv->nb_drivers;
1245 s.user_data_sz = pool_priv->user_data_sz;
1247 if ((rte_cryptodev_sym_get_existing_header_session_size(&s) >
1248 obj_size) || (s.nb_drivers <= dev->driver_id) ||
1249 rte_cryptodev_sym_get_private_session_size(dev_id) >
1251 CDEV_LOG_ERR("Invalid mempool\n");
1256 if (dev->data->dev_started) {
1258 "device %d must be stopped to allow configuration", dev_id);
1262 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
1264 rte_cryptodev_trace_queue_pair_setup(dev_id, queue_pair_id, qp_conf);
1265 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
1269 struct rte_cryptodev_cb *
1270 rte_cryptodev_add_enq_callback(uint8_t dev_id,
1272 rte_cryptodev_callback_fn cb_fn,
1275 struct rte_cryptodev *dev;
1276 struct rte_cryptodev_cb_rcu *list;
1277 struct rte_cryptodev_cb *cb, *tail;
1280 CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
1285 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1286 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1291 dev = &rte_crypto_devices[dev_id];
1292 if (qp_id >= dev->data->nb_queue_pairs) {
1293 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1298 cb = rte_zmalloc(NULL, sizeof(*cb), 0);
1300 CDEV_LOG_ERR("Failed to allocate memory for callback on "
1301 "dev=%d, queue_pair_id=%d", dev_id, qp_id);
1306 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1311 /* Add the callbacks in fifo order. */
1312 list = &dev->enq_cbs[qp_id];
1318 /* Stores to cb->fn and cb->param should complete before
1319 * cb is visible to data plane.
1321 __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
1323 /* Stores to cb->fn and cb->param should complete before
1324 * cb is visible to data plane.
1326 __atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
1329 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1335 rte_cryptodev_remove_enq_callback(uint8_t dev_id,
1337 struct rte_cryptodev_cb *cb)
1339 struct rte_cryptodev *dev;
1340 struct rte_cryptodev_cb **prev_cb, *curr_cb;
1341 struct rte_cryptodev_cb_rcu *list;
1347 CDEV_LOG_ERR("Callback is NULL");
1351 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1352 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1356 dev = &rte_crypto_devices[dev_id];
1357 if (qp_id >= dev->data->nb_queue_pairs) {
1358 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1362 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1363 if (dev->enq_cbs == NULL) {
1364 CDEV_LOG_ERR("Callback not initialized");
1368 list = &dev->enq_cbs[qp_id];
1370 CDEV_LOG_ERR("Callback list is NULL");
1374 if (list->qsbr == NULL) {
1375 CDEV_LOG_ERR("Rcu qsbr is NULL");
1379 prev_cb = &list->next;
1380 for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1382 if (curr_cb == cb) {
1383 /* Remove the user cb from the callback list. */
1384 __atomic_store_n(prev_cb, curr_cb->next,
1392 /* Call sync with invalid thread id as this is part of
1395 rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1400 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1404 struct rte_cryptodev_cb *
1405 rte_cryptodev_add_deq_callback(uint8_t dev_id,
1407 rte_cryptodev_callback_fn cb_fn,
1410 struct rte_cryptodev *dev;
1411 struct rte_cryptodev_cb_rcu *list;
1412 struct rte_cryptodev_cb *cb, *tail;
1415 CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
1420 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1421 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1426 dev = &rte_crypto_devices[dev_id];
1427 if (qp_id >= dev->data->nb_queue_pairs) {
1428 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1433 cb = rte_zmalloc(NULL, sizeof(*cb), 0);
1435 CDEV_LOG_ERR("Failed to allocate memory for callback on "
1436 "dev=%d, queue_pair_id=%d", dev_id, qp_id);
1441 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1446 /* Add the callbacks in fifo order. */
1447 list = &dev->deq_cbs[qp_id];
1453 /* Stores to cb->fn and cb->param should complete before
1454 * cb is visible to data plane.
1456 __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
1458 /* Stores to cb->fn and cb->param should complete before
1459 * cb is visible to data plane.
1461 __atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
1464 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1470 rte_cryptodev_remove_deq_callback(uint8_t dev_id,
1472 struct rte_cryptodev_cb *cb)
1474 struct rte_cryptodev *dev;
1475 struct rte_cryptodev_cb **prev_cb, *curr_cb;
1476 struct rte_cryptodev_cb_rcu *list;
1482 CDEV_LOG_ERR("Callback is NULL");
1486 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1487 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1491 dev = &rte_crypto_devices[dev_id];
1492 if (qp_id >= dev->data->nb_queue_pairs) {
1493 CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1497 rte_spinlock_lock(&rte_cryptodev_callback_lock);
1498 if (dev->enq_cbs == NULL) {
1499 CDEV_LOG_ERR("Callback not initialized");
1503 list = &dev->deq_cbs[qp_id];
1505 CDEV_LOG_ERR("Callback list is NULL");
1509 if (list->qsbr == NULL) {
1510 CDEV_LOG_ERR("Rcu qsbr is NULL");
1514 prev_cb = &list->next;
1515 for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1517 if (curr_cb == cb) {
1518 /* Remove the user cb from the callback list. */
1519 __atomic_store_n(prev_cb, curr_cb->next,
1527 /* Call sync with invalid thread id as this is part of
1530 rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1535 rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1540 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
1542 struct rte_cryptodev *dev;
1544 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1545 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1549 if (stats == NULL) {
1550 CDEV_LOG_ERR("Invalid stats ptr");
1554 dev = &rte_crypto_devices[dev_id];
1555 memset(stats, 0, sizeof(*stats));
1557 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1558 (*dev->dev_ops->stats_get)(dev, stats);
1563 rte_cryptodev_stats_reset(uint8_t dev_id)
1565 struct rte_cryptodev *dev;
1567 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1568 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1572 dev = &rte_crypto_devices[dev_id];
1574 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
1575 (*dev->dev_ops->stats_reset)(dev);
1579 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
1581 struct rte_cryptodev *dev;
1583 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1584 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1588 dev = &rte_crypto_devices[dev_id];
1590 memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
1592 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1593 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1595 dev_info->driver_name = dev->device->driver->name;
1596 dev_info->device = dev->device;
1600 rte_cryptodev_callback_register(uint8_t dev_id,
1601 enum rte_cryptodev_event_type event,
1602 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1604 struct rte_cryptodev *dev;
1605 struct rte_cryptodev_callback *user_cb;
1610 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1611 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1615 dev = &rte_crypto_devices[dev_id];
1616 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1618 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
1619 if (user_cb->cb_fn == cb_fn &&
1620 user_cb->cb_arg == cb_arg &&
1621 user_cb->event == event) {
1626 /* create a new callback. */
1627 if (user_cb == NULL) {
1628 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
1629 sizeof(struct rte_cryptodev_callback), 0);
1630 if (user_cb != NULL) {
1631 user_cb->cb_fn = cb_fn;
1632 user_cb->cb_arg = cb_arg;
1633 user_cb->event = event;
1634 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
1638 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1639 return (user_cb == NULL) ? -ENOMEM : 0;
1643 rte_cryptodev_callback_unregister(uint8_t dev_id,
1644 enum rte_cryptodev_event_type event,
1645 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1648 struct rte_cryptodev *dev;
1649 struct rte_cryptodev_callback *cb, *next;
1654 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1655 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1659 dev = &rte_crypto_devices[dev_id];
1660 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1663 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
1665 next = TAILQ_NEXT(cb, next);
1667 if (cb->cb_fn != cb_fn || cb->event != event ||
1668 (cb->cb_arg != (void *)-1 &&
1669 cb->cb_arg != cb_arg))
1673 * if this callback is not executing right now,
1676 if (cb->active == 0) {
1677 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
1684 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1689 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1690 enum rte_cryptodev_event_type event)
1692 struct rte_cryptodev_callback *cb_lst;
1693 struct rte_cryptodev_callback dev_cb;
1695 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1696 TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1697 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1701 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1702 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1704 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1707 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1711 rte_cryptodev_sym_session_init(uint8_t dev_id,
1712 struct rte_cryptodev_sym_session *sess,
1713 struct rte_crypto_sym_xform *xforms,
1714 struct rte_mempool *mp)
1716 struct rte_cryptodev *dev;
1717 uint32_t sess_priv_sz = rte_cryptodev_sym_get_private_session_size(
1722 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1723 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1727 dev = rte_cryptodev_pmd_get_dev(dev_id);
1729 if (sess == NULL || xforms == NULL || dev == NULL || mp == NULL)
1732 if (mp->elt_size < sess_priv_sz)
1735 index = dev->driver_id;
1736 if (index >= sess->nb_drivers)
1739 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_configure, -ENOTSUP);
1741 if (sess->sess_data[index].refcnt == 0) {
1742 ret = dev->dev_ops->sym_session_configure(dev, xforms,
1746 "dev_id %d failed to configure session details",
1752 rte_cryptodev_trace_sym_session_init(dev_id, sess, xforms, mp);
1753 sess->sess_data[index].refcnt++;
1758 rte_cryptodev_asym_session_init(uint8_t dev_id,
1759 struct rte_cryptodev_asym_session *sess,
1760 struct rte_crypto_asym_xform *xforms,
1761 struct rte_mempool *mp)
1763 struct rte_cryptodev *dev;
1767 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1768 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1772 dev = rte_cryptodev_pmd_get_dev(dev_id);
1774 if (sess == NULL || xforms == NULL || dev == NULL)
1777 index = dev->driver_id;
1779 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_configure,
1782 if (sess->sess_private_data[index] == NULL) {
1783 ret = dev->dev_ops->asym_session_configure(dev,
1788 "dev_id %d failed to configure session details",
1794 rte_cryptodev_trace_asym_session_init(dev_id, sess, xforms, mp);
1798 struct rte_mempool *
1799 rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
1800 uint32_t elt_size, uint32_t cache_size, uint16_t user_data_size,
1803 struct rte_mempool *mp;
1804 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1807 obj_sz = rte_cryptodev_sym_get_header_session_size() + user_data_size;
1808 if (obj_sz > elt_size)
1809 CDEV_LOG_INFO("elt_size %u is expanded to %u\n", elt_size,
1814 mp = rte_mempool_create(name, nb_elts, obj_sz, cache_size,
1815 (uint32_t)(sizeof(*pool_priv)),
1816 NULL, NULL, NULL, NULL,
1819 CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d\n",
1820 __func__, name, rte_errno);
1824 pool_priv = rte_mempool_get_priv(mp);
1826 CDEV_LOG_ERR("%s(name=%s) failed to get private data\n",
1828 rte_mempool_free(mp);
1832 pool_priv->nb_drivers = nb_drivers;
1833 pool_priv->user_data_sz = user_data_size;
1835 rte_cryptodev_trace_sym_session_pool_create(name, nb_elts,
1836 elt_size, cache_size, user_data_size, mp);
1841 rte_cryptodev_sym_session_data_size(struct rte_cryptodev_sym_session *sess)
1843 return (sizeof(sess->sess_data[0]) * sess->nb_drivers) +
1848 rte_cryptodev_sym_is_valid_session_pool(struct rte_mempool *mp)
1850 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1855 pool_priv = rte_mempool_get_priv(mp);
1857 if (!pool_priv || mp->private_data_size < sizeof(*pool_priv) ||
1858 pool_priv->nb_drivers != nb_drivers ||
1860 rte_cryptodev_sym_get_header_session_size()
1861 + pool_priv->user_data_sz)
1867 struct rte_cryptodev_sym_session *
1868 rte_cryptodev_sym_session_create(struct rte_mempool *mp)
1870 struct rte_cryptodev_sym_session *sess;
1871 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1873 if (!rte_cryptodev_sym_is_valid_session_pool(mp)) {
1874 CDEV_LOG_ERR("Invalid mempool\n");
1878 pool_priv = rte_mempool_get_priv(mp);
1880 /* Allocate a session structure from the session pool */
1881 if (rte_mempool_get(mp, (void **)&sess)) {
1882 CDEV_LOG_ERR("couldn't get object from session mempool");
1886 sess->nb_drivers = pool_priv->nb_drivers;
1887 sess->user_data_sz = pool_priv->user_data_sz;
1888 sess->opaque_data = 0;
1890 /* Clear device session pointer.
1891 * Include the flag indicating presence of user data
1893 memset(sess->sess_data, 0,
1894 rte_cryptodev_sym_session_data_size(sess));
1896 rte_cryptodev_trace_sym_session_create(mp, sess);
1900 struct rte_cryptodev_asym_session *
1901 rte_cryptodev_asym_session_create(struct rte_mempool *mp)
1903 struct rte_cryptodev_asym_session *sess;
1904 unsigned int session_size =
1905 rte_cryptodev_asym_get_header_session_size();
1908 CDEV_LOG_ERR("invalid mempool\n");
1912 /* Verify if provided mempool can hold elements big enough. */
1913 if (mp->elt_size < session_size) {
1915 "mempool elements too small to hold session objects");
1919 /* Allocate a session structure from the session pool */
1920 if (rte_mempool_get(mp, (void **)&sess)) {
1921 CDEV_LOG_ERR("couldn't get object from session mempool");
1925 /* Clear device session pointer.
1926 * Include the flag indicating presence of private data
1928 memset(sess, 0, session_size);
1930 rte_cryptodev_trace_asym_session_create(mp, sess);
1935 rte_cryptodev_sym_session_clear(uint8_t dev_id,
1936 struct rte_cryptodev_sym_session *sess)
1938 struct rte_cryptodev *dev;
1941 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1942 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1946 dev = rte_cryptodev_pmd_get_dev(dev_id);
1948 if (dev == NULL || sess == NULL)
1951 driver_id = dev->driver_id;
1952 if (sess->sess_data[driver_id].refcnt == 0)
1954 if (--sess->sess_data[driver_id].refcnt != 0)
1957 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_clear, -ENOTSUP);
1959 dev->dev_ops->sym_session_clear(dev, sess);
1961 rte_cryptodev_trace_sym_session_clear(dev_id, sess);
1966 rte_cryptodev_asym_session_clear(uint8_t dev_id,
1967 struct rte_cryptodev_asym_session *sess)
1969 struct rte_cryptodev *dev;
1971 if (!rte_cryptodev_is_valid_dev(dev_id)) {
1972 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1976 dev = rte_cryptodev_pmd_get_dev(dev_id);
1978 if (dev == NULL || sess == NULL)
1981 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_clear, -ENOTSUP);
1983 dev->dev_ops->asym_session_clear(dev, sess);
1985 rte_cryptodev_trace_sym_session_clear(dev_id, sess);
1990 rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session *sess)
1993 struct rte_mempool *sess_mp;
1998 /* Check that all device private data has been freed */
1999 for (i = 0; i < sess->nb_drivers; i++) {
2000 if (sess->sess_data[i].refcnt != 0)
2004 /* Return session to mempool */
2005 sess_mp = rte_mempool_from_obj(sess);
2006 rte_mempool_put(sess_mp, sess);
2008 rte_cryptodev_trace_sym_session_free(sess);
2013 rte_cryptodev_asym_session_free(struct rte_cryptodev_asym_session *sess)
2017 struct rte_mempool *sess_mp;
2022 /* Check that all device private data has been freed */
2023 for (i = 0; i < nb_drivers; i++) {
2024 sess_priv = get_asym_session_private_data(sess, i);
2025 if (sess_priv != NULL)
2029 /* Return session to mempool */
2030 sess_mp = rte_mempool_from_obj(sess);
2031 rte_mempool_put(sess_mp, sess);
2033 rte_cryptodev_trace_asym_session_free(sess);
2038 rte_cryptodev_sym_get_header_session_size(void)
2041 * Header contains pointers to the private data of all registered
2042 * drivers and all necessary information to ensure safely clear
2043 * or free al session.
2045 struct rte_cryptodev_sym_session s = {0};
2047 s.nb_drivers = nb_drivers;
2049 return (unsigned int)(sizeof(s) +
2050 rte_cryptodev_sym_session_data_size(&s));
2054 rte_cryptodev_sym_get_existing_header_session_size(
2055 struct rte_cryptodev_sym_session *sess)
2060 return (unsigned int)(sizeof(*sess) +
2061 rte_cryptodev_sym_session_data_size(sess));
2065 rte_cryptodev_asym_get_header_session_size(void)
2068 * Header contains pointers to the private data
2069 * of all registered drivers, and a flag which
2070 * indicates presence of private data
2072 return ((sizeof(void *) * nb_drivers) + sizeof(uint8_t));
2076 rte_cryptodev_sym_get_private_session_size(uint8_t dev_id)
2078 struct rte_cryptodev *dev;
2079 unsigned int priv_sess_size;
2081 if (!rte_cryptodev_is_valid_dev(dev_id))
2084 dev = rte_cryptodev_pmd_get_dev(dev_id);
2086 if (*dev->dev_ops->sym_session_get_size == NULL)
2089 priv_sess_size = (*dev->dev_ops->sym_session_get_size)(dev);
2091 return priv_sess_size;
2095 rte_cryptodev_asym_get_private_session_size(uint8_t dev_id)
2097 struct rte_cryptodev *dev;
2098 unsigned int header_size = sizeof(void *) * nb_drivers;
2099 unsigned int priv_sess_size;
2101 if (!rte_cryptodev_is_valid_dev(dev_id))
2104 dev = rte_cryptodev_pmd_get_dev(dev_id);
2106 if (*dev->dev_ops->asym_session_get_size == NULL)
2109 priv_sess_size = (*dev->dev_ops->asym_session_get_size)(dev);
2110 if (priv_sess_size < header_size)
2113 return priv_sess_size;
2118 rte_cryptodev_sym_session_set_user_data(
2119 struct rte_cryptodev_sym_session *sess,
2126 if (sess->user_data_sz < size)
2129 rte_memcpy(sess->sess_data + sess->nb_drivers, data, size);
2134 rte_cryptodev_sym_session_get_user_data(
2135 struct rte_cryptodev_sym_session *sess)
2137 if (sess == NULL || sess->user_data_sz == 0)
2140 return (void *)(sess->sess_data + sess->nb_drivers);
2144 sym_crypto_fill_status(struct rte_crypto_sym_vec *vec, int32_t errnum)
2147 for (i = 0; i < vec->num; i++)
2148 vec->status[i] = errnum;
2152 rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
2153 struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs ofs,
2154 struct rte_crypto_sym_vec *vec)
2156 struct rte_cryptodev *dev;
2158 if (!rte_cryptodev_is_valid_dev(dev_id)) {
2159 sym_crypto_fill_status(vec, EINVAL);
2163 dev = rte_cryptodev_pmd_get_dev(dev_id);
2165 if (*dev->dev_ops->sym_cpu_process == NULL ||
2166 !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO)) {
2167 sym_crypto_fill_status(vec, ENOTSUP);
2171 return dev->dev_ops->sym_cpu_process(dev, sess, ofs, vec);
2175 rte_cryptodev_get_raw_dp_ctx_size(uint8_t dev_id)
2177 struct rte_cryptodev *dev;
2178 int32_t size = sizeof(struct rte_crypto_raw_dp_ctx);
2181 if (!rte_cryptodev_is_valid_dev(dev_id))
2184 dev = rte_cryptodev_pmd_get_dev(dev_id);
2186 if (*dev->dev_ops->sym_get_raw_dp_ctx_size == NULL ||
2187 !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)) {
2191 priv_size = (*dev->dev_ops->sym_get_raw_dp_ctx_size)(dev);
2195 return RTE_ALIGN_CEIL((size + priv_size), 8);
2199 rte_cryptodev_configure_raw_dp_ctx(uint8_t dev_id, uint16_t qp_id,
2200 struct rte_crypto_raw_dp_ctx *ctx,
2201 enum rte_crypto_op_sess_type sess_type,
2202 union rte_cryptodev_session_ctx session_ctx,
2205 struct rte_cryptodev *dev;
2207 if (!rte_cryptodev_get_qp_status(dev_id, qp_id))
2210 dev = rte_cryptodev_pmd_get_dev(dev_id);
2211 if (!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)
2212 || dev->dev_ops->sym_configure_raw_dp_ctx == NULL)
2215 return (*dev->dev_ops->sym_configure_raw_dp_ctx)(dev, qp_id, ctx,
2216 sess_type, session_ctx, is_update);
2220 rte_cryptodev_raw_enqueue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2221 struct rte_crypto_sym_vec *vec, union rte_crypto_sym_ofs ofs,
2222 void **user_data, int *enqueue_status)
2224 return (*ctx->enqueue_burst)(ctx->qp_data, ctx->drv_ctx_data, vec,
2225 ofs, user_data, enqueue_status);
2229 rte_cryptodev_raw_enqueue_done(struct rte_crypto_raw_dp_ctx *ctx,
2232 return (*ctx->enqueue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2236 rte_cryptodev_raw_dequeue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2237 rte_cryptodev_raw_get_dequeue_count_t get_dequeue_count,
2238 uint32_t max_nb_to_dequeue,
2239 rte_cryptodev_raw_post_dequeue_t post_dequeue,
2240 void **out_user_data, uint8_t is_user_data_array,
2241 uint32_t *n_success_jobs, int *status)
2243 return (*ctx->dequeue_burst)(ctx->qp_data, ctx->drv_ctx_data,
2244 get_dequeue_count, max_nb_to_dequeue, post_dequeue,
2245 out_user_data, is_user_data_array, n_success_jobs, status);
2249 rte_cryptodev_raw_dequeue_done(struct rte_crypto_raw_dp_ctx *ctx,
2252 return (*ctx->dequeue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2255 /** Initialise rte_crypto_op mempool element */
2257 rte_crypto_op_init(struct rte_mempool *mempool,
2260 __rte_unused unsigned i)
2262 struct rte_crypto_op *op = _op_data;
2263 enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
2265 memset(_op_data, 0, mempool->elt_size);
2267 __rte_crypto_op_reset(op, type);
2269 op->phys_addr = rte_mem_virt2iova(_op_data);
2270 op->mempool = mempool;
2274 struct rte_mempool *
2275 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
2276 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
2279 struct rte_crypto_op_pool_private *priv;
2281 unsigned elt_size = sizeof(struct rte_crypto_op) +
2284 if (type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
2285 elt_size += sizeof(struct rte_crypto_sym_op);
2286 } else if (type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
2287 elt_size += sizeof(struct rte_crypto_asym_op);
2288 } else if (type == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
2289 elt_size += RTE_MAX(sizeof(struct rte_crypto_sym_op),
2290 sizeof(struct rte_crypto_asym_op));
2292 CDEV_LOG_ERR("Invalid op_type\n");
2296 /* lookup mempool in case already allocated */
2297 struct rte_mempool *mp = rte_mempool_lookup(name);
2300 priv = (struct rte_crypto_op_pool_private *)
2301 rte_mempool_get_priv(mp);
2303 if (mp->elt_size != elt_size ||
2304 mp->cache_size < cache_size ||
2305 mp->size < nb_elts ||
2306 priv->priv_size < priv_size) {
2308 CDEV_LOG_ERR("Mempool %s already exists but with "
2309 "incompatible parameters", name);
2315 mp = rte_mempool_create(
2320 sizeof(struct rte_crypto_op_pool_private),
2329 CDEV_LOG_ERR("Failed to create mempool %s", name);
2333 priv = (struct rte_crypto_op_pool_private *)
2334 rte_mempool_get_priv(mp);
2336 priv->priv_size = priv_size;
2343 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix)
2345 struct rte_cryptodev *dev = NULL;
2351 for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
2352 int ret = snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
2353 "%s_%u", dev_name_prefix, i);
2358 dev = rte_cryptodev_pmd_get_named_dev(name);
2366 TAILQ_HEAD(cryptodev_driver_list, cryptodev_driver);
2368 static struct cryptodev_driver_list cryptodev_driver_list =
2369 TAILQ_HEAD_INITIALIZER(cryptodev_driver_list);
2372 rte_cryptodev_driver_id_get(const char *name)
2374 struct cryptodev_driver *driver;
2375 const char *driver_name;
2378 RTE_LOG(DEBUG, CRYPTODEV, "name pointer NULL");
2382 TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
2383 driver_name = driver->driver->name;
2384 if (strncmp(driver_name, name, strlen(driver_name) + 1) == 0)
2391 rte_cryptodev_name_get(uint8_t dev_id)
2393 struct rte_cryptodev *dev;
2395 if (!rte_cryptodev_is_valid_device_data(dev_id)) {
2396 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2400 dev = rte_cryptodev_pmd_get_dev(dev_id);
2404 return dev->data->name;
2408 rte_cryptodev_driver_name_get(uint8_t driver_id)
2410 struct cryptodev_driver *driver;
2412 TAILQ_FOREACH(driver, &cryptodev_driver_list, next)
2413 if (driver->id == driver_id)
2414 return driver->driver->name;
2419 rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
2420 const struct rte_driver *drv)
2422 crypto_drv->driver = drv;
2423 crypto_drv->id = nb_drivers;
2425 TAILQ_INSERT_TAIL(&cryptodev_driver_list, crypto_drv, next);
2427 return nb_drivers++;