199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2015-2020 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson
599a2dd95SBruce Richardson #include <sys/queue.h>
699a2dd95SBruce Richardson #include <ctype.h>
799a2dd95SBruce Richardson #include <stdio.h>
899a2dd95SBruce Richardson #include <stdlib.h>
999a2dd95SBruce Richardson #include <string.h>
1099a2dd95SBruce Richardson #include <errno.h>
1199a2dd95SBruce Richardson #include <stdint.h>
1299a2dd95SBruce Richardson #include <inttypes.h>
1399a2dd95SBruce Richardson
1499a2dd95SBruce Richardson #include <rte_log.h>
1599a2dd95SBruce Richardson #include <rte_debug.h>
1699a2dd95SBruce Richardson #include <rte_dev.h>
1799a2dd95SBruce Richardson #include <rte_memory.h>
1899a2dd95SBruce Richardson #include <rte_memcpy.h>
1999a2dd95SBruce Richardson #include <rte_memzone.h>
2099a2dd95SBruce Richardson #include <rte_eal.h>
2199a2dd95SBruce Richardson #include <rte_common.h>
2299a2dd95SBruce Richardson #include <rte_mempool.h>
2399a2dd95SBruce Richardson #include <rte_malloc.h>
2499a2dd95SBruce Richardson #include <rte_errno.h>
2599a2dd95SBruce Richardson #include <rte_spinlock.h>
2699a2dd95SBruce Richardson #include <rte_string_fns.h>
27d3d98f5cSRebecca Troy #include <rte_telemetry.h>
2899a2dd95SBruce Richardson
2999a2dd95SBruce Richardson #include "rte_crypto.h"
3099a2dd95SBruce Richardson #include "rte_cryptodev.h"
31af668035SAkhil Goyal #include "cryptodev_pmd.h"
3299a2dd95SBruce Richardson #include "rte_cryptodev_trace.h"
3399a2dd95SBruce Richardson
3499a2dd95SBruce Richardson static uint8_t nb_drivers;
3599a2dd95SBruce Richardson
3699a2dd95SBruce Richardson static struct rte_cryptodev rte_crypto_devices[RTE_CRYPTO_MAX_DEVS];
3799a2dd95SBruce Richardson
3899a2dd95SBruce Richardson struct rte_cryptodev *rte_cryptodevs = rte_crypto_devices;
3999a2dd95SBruce Richardson
4099a2dd95SBruce Richardson static struct rte_cryptodev_global cryptodev_globals = {
4199a2dd95SBruce Richardson .devs = rte_crypto_devices,
4299a2dd95SBruce Richardson .data = { NULL },
4399a2dd95SBruce Richardson .nb_devs = 0
4499a2dd95SBruce Richardson };
4599a2dd95SBruce Richardson
462fd66f75SAkhil Goyal /* Public fastpath APIs. */
472fd66f75SAkhil Goyal struct rte_crypto_fp_ops rte_crypto_fp_ops[RTE_CRYPTO_MAX_DEVS];
482fd66f75SAkhil Goyal
4999a2dd95SBruce Richardson /* spinlock for crypto device callbacks */
5099a2dd95SBruce Richardson static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
5199a2dd95SBruce Richardson
5299a2dd95SBruce Richardson /**
5399a2dd95SBruce Richardson * The user application callback description.
5499a2dd95SBruce Richardson *
5599a2dd95SBruce Richardson * It contains callback address to be registered by user application,
5699a2dd95SBruce Richardson * the pointer to the parameters for callback, and the event type.
5799a2dd95SBruce Richardson */
5899a2dd95SBruce Richardson struct rte_cryptodev_callback {
5999a2dd95SBruce Richardson TAILQ_ENTRY(rte_cryptodev_callback) next; /**< Callbacks list */
6099a2dd95SBruce Richardson rte_cryptodev_cb_fn cb_fn; /**< Callback address */
6199a2dd95SBruce Richardson void *cb_arg; /**< Parameter for callback */
6299a2dd95SBruce Richardson enum rte_cryptodev_event_type event; /**< Interrupt event type */
6399a2dd95SBruce Richardson uint32_t active; /**< Callback is executing */
6499a2dd95SBruce Richardson };
6599a2dd95SBruce Richardson
6699a2dd95SBruce Richardson /**
6799a2dd95SBruce Richardson * The crypto cipher algorithm strings identifiers.
6899a2dd95SBruce Richardson * It could be used in application command line.
6999a2dd95SBruce Richardson */
7099a2dd95SBruce Richardson const char *
7199a2dd95SBruce Richardson rte_crypto_cipher_algorithm_strings[] = {
7299a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_3DES_CBC] = "3des-cbc",
7399a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_3DES_ECB] = "3des-ecb",
7499a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_3DES_CTR] = "3des-ctr",
7599a2dd95SBruce Richardson
7699a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_AES_CBC] = "aes-cbc",
7799a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_AES_CTR] = "aes-ctr",
7899a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_AES_DOCSISBPI] = "aes-docsisbpi",
7999a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_AES_ECB] = "aes-ecb",
8099a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_AES_F8] = "aes-f8",
8199a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_AES_XTS] = "aes-xts",
8299a2dd95SBruce Richardson
8399a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_ARC4] = "arc4",
8499a2dd95SBruce Richardson
8599a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_DES_CBC] = "des-cbc",
8699a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_DES_DOCSISBPI] = "des-docsisbpi",
8799a2dd95SBruce Richardson
8899a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_NULL] = "null",
8999a2dd95SBruce Richardson
9099a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_KASUMI_F8] = "kasumi-f8",
9199a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_SNOW3G_UEA2] = "snow3g-uea2",
9299a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_ZUC_EEA3] = "zuc-eea3"
9399a2dd95SBruce Richardson };
9499a2dd95SBruce Richardson
9599a2dd95SBruce Richardson /**
9699a2dd95SBruce Richardson * The crypto cipher operation strings identifiers.
9799a2dd95SBruce Richardson * It could be used in application command line.
9899a2dd95SBruce Richardson */
9999a2dd95SBruce Richardson const char *
10099a2dd95SBruce Richardson rte_crypto_cipher_operation_strings[] = {
10199a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_OP_ENCRYPT] = "encrypt",
10299a2dd95SBruce Richardson [RTE_CRYPTO_CIPHER_OP_DECRYPT] = "decrypt"
10399a2dd95SBruce Richardson };
10499a2dd95SBruce Richardson
10599a2dd95SBruce Richardson /**
10699a2dd95SBruce Richardson * The crypto auth algorithm strings identifiers.
10799a2dd95SBruce Richardson * It could be used in application command line.
10899a2dd95SBruce Richardson */
10999a2dd95SBruce Richardson const char *
11099a2dd95SBruce Richardson rte_crypto_auth_algorithm_strings[] = {
11199a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_AES_CBC_MAC] = "aes-cbc-mac",
11299a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_AES_CMAC] = "aes-cmac",
11399a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_AES_GMAC] = "aes-gmac",
11499a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_AES_XCBC_MAC] = "aes-xcbc-mac",
11599a2dd95SBruce Richardson
11699a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_MD5] = "md5",
11799a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_MD5_HMAC] = "md5-hmac",
11899a2dd95SBruce Richardson
11999a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_NULL] = "null",
12099a2dd95SBruce Richardson
12199a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA1] = "sha1",
12299a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA1_HMAC] = "sha1-hmac",
12399a2dd95SBruce Richardson
12499a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA224] = "sha2-224",
12599a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA224_HMAC] = "sha2-224-hmac",
12699a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA256] = "sha2-256",
12799a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA256_HMAC] = "sha2-256-hmac",
12899a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA384] = "sha2-384",
12999a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA384_HMAC] = "sha2-384-hmac",
13099a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA512] = "sha2-512",
13199a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SHA512_HMAC] = "sha2-512-hmac",
13299a2dd95SBruce Richardson
13399a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_KASUMI_F9] = "kasumi-f9",
13499a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_SNOW3G_UIA2] = "snow3g-uia2",
13599a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_ZUC_EIA3] = "zuc-eia3"
13699a2dd95SBruce Richardson };
13799a2dd95SBruce Richardson
13899a2dd95SBruce Richardson /**
13999a2dd95SBruce Richardson * The crypto AEAD algorithm strings identifiers.
14099a2dd95SBruce Richardson * It could be used in application command line.
14199a2dd95SBruce Richardson */
14299a2dd95SBruce Richardson const char *
14399a2dd95SBruce Richardson rte_crypto_aead_algorithm_strings[] = {
14499a2dd95SBruce Richardson [RTE_CRYPTO_AEAD_AES_CCM] = "aes-ccm",
14599a2dd95SBruce Richardson [RTE_CRYPTO_AEAD_AES_GCM] = "aes-gcm",
14699a2dd95SBruce Richardson [RTE_CRYPTO_AEAD_CHACHA20_POLY1305] = "chacha20-poly1305"
14799a2dd95SBruce Richardson };
14899a2dd95SBruce Richardson
14999a2dd95SBruce Richardson /**
15099a2dd95SBruce Richardson * The crypto AEAD operation strings identifiers.
15199a2dd95SBruce Richardson * It could be used in application command line.
15299a2dd95SBruce Richardson */
15399a2dd95SBruce Richardson const char *
15499a2dd95SBruce Richardson rte_crypto_aead_operation_strings[] = {
15599a2dd95SBruce Richardson [RTE_CRYPTO_AEAD_OP_ENCRYPT] = "encrypt",
15699a2dd95SBruce Richardson [RTE_CRYPTO_AEAD_OP_DECRYPT] = "decrypt"
15799a2dd95SBruce Richardson };
15899a2dd95SBruce Richardson
15999a2dd95SBruce Richardson /**
16099a2dd95SBruce Richardson * Asymmetric crypto transform operation strings identifiers.
16199a2dd95SBruce Richardson */
16299a2dd95SBruce Richardson const char *rte_crypto_asym_xform_strings[] = {
16399a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_XFORM_NONE] = "none",
16499a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_XFORM_RSA] = "rsa",
16599a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_XFORM_MODEX] = "modexp",
16699a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_XFORM_MODINV] = "modinv",
16799a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_XFORM_DH] = "dh",
16899a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_XFORM_DSA] = "dsa",
16999a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_XFORM_ECDSA] = "ecdsa",
17099a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_XFORM_ECPM] = "ecpm",
17199a2dd95SBruce Richardson };
17299a2dd95SBruce Richardson
17399a2dd95SBruce Richardson /**
17499a2dd95SBruce Richardson * Asymmetric crypto operation strings identifiers.
17599a2dd95SBruce Richardson */
17699a2dd95SBruce Richardson const char *rte_crypto_asym_op_strings[] = {
17799a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_OP_ENCRYPT] = "encrypt",
17899a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_OP_DECRYPT] = "decrypt",
17999a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_OP_SIGN] = "sign",
18099a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_OP_VERIFY] = "verify",
18199a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE] = "priv_key_generate",
18299a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE] = "pub_key_generate",
18399a2dd95SBruce Richardson [RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE] = "sharedsecret_compute",
18499a2dd95SBruce Richardson };
18599a2dd95SBruce Richardson
18699a2dd95SBruce Richardson /**
1871f1e4b7cSCiara Power * The private data structure stored in the sym session mempool private data.
18899a2dd95SBruce Richardson */
18999a2dd95SBruce Richardson struct rte_cryptodev_sym_session_pool_private_data {
19099a2dd95SBruce Richardson uint16_t nb_drivers;
19199a2dd95SBruce Richardson /**< number of elements in sess_data array */
19299a2dd95SBruce Richardson uint16_t user_data_sz;
19399a2dd95SBruce Richardson /**< session user data will be placed after sess_data */
19499a2dd95SBruce Richardson };
19599a2dd95SBruce Richardson
1961f1e4b7cSCiara Power /**
1971f1e4b7cSCiara Power * The private data structure stored in the asym session mempool private data.
1981f1e4b7cSCiara Power */
1991f1e4b7cSCiara Power struct rte_cryptodev_asym_session_pool_private_data {
2001f1e4b7cSCiara Power uint16_t max_priv_session_sz;
2011f1e4b7cSCiara Power /**< Size of private session data used when creating mempool */
20292d55afeSCiara Power uint16_t user_data_sz;
20392d55afeSCiara Power /**< Session user data will be placed after sess_private_data */
2041f1e4b7cSCiara Power };
2051f1e4b7cSCiara Power
20699a2dd95SBruce Richardson int
rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm * algo_enum,const char * algo_string)20799a2dd95SBruce Richardson rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
20899a2dd95SBruce Richardson const char *algo_string)
20999a2dd95SBruce Richardson {
21099a2dd95SBruce Richardson unsigned int i;
21199a2dd95SBruce Richardson
21299a2dd95SBruce Richardson for (i = 1; i < RTE_DIM(rte_crypto_cipher_algorithm_strings); i++) {
21399a2dd95SBruce Richardson if (strcmp(algo_string, rte_crypto_cipher_algorithm_strings[i]) == 0) {
21499a2dd95SBruce Richardson *algo_enum = (enum rte_crypto_cipher_algorithm) i;
21599a2dd95SBruce Richardson return 0;
21699a2dd95SBruce Richardson }
21799a2dd95SBruce Richardson }
21899a2dd95SBruce Richardson
21999a2dd95SBruce Richardson /* Invalid string */
22099a2dd95SBruce Richardson return -1;
22199a2dd95SBruce Richardson }
22299a2dd95SBruce Richardson
22399a2dd95SBruce Richardson int
rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm * algo_enum,const char * algo_string)22499a2dd95SBruce Richardson rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
22599a2dd95SBruce Richardson const char *algo_string)
22699a2dd95SBruce Richardson {
22799a2dd95SBruce Richardson unsigned int i;
22899a2dd95SBruce Richardson
22999a2dd95SBruce Richardson for (i = 1; i < RTE_DIM(rte_crypto_auth_algorithm_strings); i++) {
23099a2dd95SBruce Richardson if (strcmp(algo_string, rte_crypto_auth_algorithm_strings[i]) == 0) {
23199a2dd95SBruce Richardson *algo_enum = (enum rte_crypto_auth_algorithm) i;
23299a2dd95SBruce Richardson return 0;
23399a2dd95SBruce Richardson }
23499a2dd95SBruce Richardson }
23599a2dd95SBruce Richardson
23699a2dd95SBruce Richardson /* Invalid string */
23799a2dd95SBruce Richardson return -1;
23899a2dd95SBruce Richardson }
23999a2dd95SBruce Richardson
24099a2dd95SBruce Richardson int
rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm * algo_enum,const char * algo_string)24199a2dd95SBruce Richardson rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
24299a2dd95SBruce Richardson const char *algo_string)
24399a2dd95SBruce Richardson {
24499a2dd95SBruce Richardson unsigned int i;
24599a2dd95SBruce Richardson
24699a2dd95SBruce Richardson for (i = 1; i < RTE_DIM(rte_crypto_aead_algorithm_strings); i++) {
24799a2dd95SBruce Richardson if (strcmp(algo_string, rte_crypto_aead_algorithm_strings[i]) == 0) {
24899a2dd95SBruce Richardson *algo_enum = (enum rte_crypto_aead_algorithm) i;
24999a2dd95SBruce Richardson return 0;
25099a2dd95SBruce Richardson }
25199a2dd95SBruce Richardson }
25299a2dd95SBruce Richardson
25399a2dd95SBruce Richardson /* Invalid string */
25499a2dd95SBruce Richardson return -1;
25599a2dd95SBruce Richardson }
25699a2dd95SBruce Richardson
25799a2dd95SBruce Richardson int
rte_cryptodev_asym_get_xform_enum(enum rte_crypto_asym_xform_type * xform_enum,const char * xform_string)25899a2dd95SBruce Richardson rte_cryptodev_asym_get_xform_enum(enum rte_crypto_asym_xform_type *xform_enum,
25999a2dd95SBruce Richardson const char *xform_string)
26099a2dd95SBruce Richardson {
26199a2dd95SBruce Richardson unsigned int i;
26299a2dd95SBruce Richardson
26399a2dd95SBruce Richardson for (i = 1; i < RTE_DIM(rte_crypto_asym_xform_strings); i++) {
26499a2dd95SBruce Richardson if (strcmp(xform_string,
26599a2dd95SBruce Richardson rte_crypto_asym_xform_strings[i]) == 0) {
26699a2dd95SBruce Richardson *xform_enum = (enum rte_crypto_asym_xform_type) i;
26799a2dd95SBruce Richardson return 0;
26899a2dd95SBruce Richardson }
26999a2dd95SBruce Richardson }
27099a2dd95SBruce Richardson
27199a2dd95SBruce Richardson /* Invalid string */
27299a2dd95SBruce Richardson return -1;
27399a2dd95SBruce Richardson }
27499a2dd95SBruce Richardson
27599a2dd95SBruce Richardson /**
27699a2dd95SBruce Richardson * The crypto auth operation strings identifiers.
27799a2dd95SBruce Richardson * It could be used in application command line.
27899a2dd95SBruce Richardson */
27999a2dd95SBruce Richardson const char *
28099a2dd95SBruce Richardson rte_crypto_auth_operation_strings[] = {
28199a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_OP_VERIFY] = "verify",
28299a2dd95SBruce Richardson [RTE_CRYPTO_AUTH_OP_GENERATE] = "generate"
28399a2dd95SBruce Richardson };
28499a2dd95SBruce Richardson
28599a2dd95SBruce Richardson const struct rte_cryptodev_symmetric_capability *
rte_cryptodev_sym_capability_get(uint8_t dev_id,const struct rte_cryptodev_sym_capability_idx * idx)28699a2dd95SBruce Richardson rte_cryptodev_sym_capability_get(uint8_t dev_id,
28799a2dd95SBruce Richardson const struct rte_cryptodev_sym_capability_idx *idx)
28899a2dd95SBruce Richardson {
28999a2dd95SBruce Richardson const struct rte_cryptodev_capabilities *capability;
29099a2dd95SBruce Richardson struct rte_cryptodev_info dev_info;
29199a2dd95SBruce Richardson int i = 0;
29299a2dd95SBruce Richardson
29399a2dd95SBruce Richardson rte_cryptodev_info_get(dev_id, &dev_info);
29499a2dd95SBruce Richardson
29599a2dd95SBruce Richardson while ((capability = &dev_info.capabilities[i++])->op !=
29699a2dd95SBruce Richardson RTE_CRYPTO_OP_TYPE_UNDEFINED) {
29799a2dd95SBruce Richardson if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
29899a2dd95SBruce Richardson continue;
29999a2dd95SBruce Richardson
30099a2dd95SBruce Richardson if (capability->sym.xform_type != idx->type)
30199a2dd95SBruce Richardson continue;
30299a2dd95SBruce Richardson
30399a2dd95SBruce Richardson if (idx->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
30499a2dd95SBruce Richardson capability->sym.auth.algo == idx->algo.auth)
30599a2dd95SBruce Richardson return &capability->sym;
30699a2dd95SBruce Richardson
30799a2dd95SBruce Richardson if (idx->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
30899a2dd95SBruce Richardson capability->sym.cipher.algo == idx->algo.cipher)
30999a2dd95SBruce Richardson return &capability->sym;
31099a2dd95SBruce Richardson
31199a2dd95SBruce Richardson if (idx->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
31299a2dd95SBruce Richardson capability->sym.aead.algo == idx->algo.aead)
31399a2dd95SBruce Richardson return &capability->sym;
31499a2dd95SBruce Richardson }
31599a2dd95SBruce Richardson
31699a2dd95SBruce Richardson return NULL;
31799a2dd95SBruce Richardson }
31899a2dd95SBruce Richardson
31999a2dd95SBruce Richardson static int
param_range_check(uint16_t size,const struct rte_crypto_param_range * range)32099a2dd95SBruce Richardson param_range_check(uint16_t size, const struct rte_crypto_param_range *range)
32199a2dd95SBruce Richardson {
32299a2dd95SBruce Richardson unsigned int next_size;
32399a2dd95SBruce Richardson
32499a2dd95SBruce Richardson /* Check lower/upper bounds */
32599a2dd95SBruce Richardson if (size < range->min)
32699a2dd95SBruce Richardson return -1;
32799a2dd95SBruce Richardson
32899a2dd95SBruce Richardson if (size > range->max)
32999a2dd95SBruce Richardson return -1;
33099a2dd95SBruce Richardson
33199a2dd95SBruce Richardson /* If range is actually only one value, size is correct */
33299a2dd95SBruce Richardson if (range->increment == 0)
33399a2dd95SBruce Richardson return 0;
33499a2dd95SBruce Richardson
33599a2dd95SBruce Richardson /* Check if value is one of the supported sizes */
33699a2dd95SBruce Richardson for (next_size = range->min; next_size <= range->max;
33799a2dd95SBruce Richardson next_size += range->increment)
33899a2dd95SBruce Richardson if (size == next_size)
33999a2dd95SBruce Richardson return 0;
34099a2dd95SBruce Richardson
34199a2dd95SBruce Richardson return -1;
34299a2dd95SBruce Richardson }
34399a2dd95SBruce Richardson
34499a2dd95SBruce Richardson const struct rte_cryptodev_asymmetric_xform_capability *
rte_cryptodev_asym_capability_get(uint8_t dev_id,const struct rte_cryptodev_asym_capability_idx * idx)34599a2dd95SBruce Richardson rte_cryptodev_asym_capability_get(uint8_t dev_id,
34699a2dd95SBruce Richardson const struct rte_cryptodev_asym_capability_idx *idx)
34799a2dd95SBruce Richardson {
34899a2dd95SBruce Richardson const struct rte_cryptodev_capabilities *capability;
34999a2dd95SBruce Richardson struct rte_cryptodev_info dev_info;
35099a2dd95SBruce Richardson unsigned int i = 0;
35199a2dd95SBruce Richardson
35299a2dd95SBruce Richardson memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
35399a2dd95SBruce Richardson rte_cryptodev_info_get(dev_id, &dev_info);
35499a2dd95SBruce Richardson
35599a2dd95SBruce Richardson while ((capability = &dev_info.capabilities[i++])->op !=
35699a2dd95SBruce Richardson RTE_CRYPTO_OP_TYPE_UNDEFINED) {
35799a2dd95SBruce Richardson if (capability->op != RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
35899a2dd95SBruce Richardson continue;
35999a2dd95SBruce Richardson
36099a2dd95SBruce Richardson if (capability->asym.xform_capa.xform_type == idx->type)
36199a2dd95SBruce Richardson return &capability->asym.xform_capa;
36299a2dd95SBruce Richardson }
36399a2dd95SBruce Richardson return NULL;
36499a2dd95SBruce Richardson };
36599a2dd95SBruce Richardson
36699a2dd95SBruce Richardson int
rte_cryptodev_sym_capability_check_cipher(const struct rte_cryptodev_symmetric_capability * capability,uint16_t key_size,uint16_t iv_size)36799a2dd95SBruce Richardson rte_cryptodev_sym_capability_check_cipher(
36899a2dd95SBruce Richardson const struct rte_cryptodev_symmetric_capability *capability,
36999a2dd95SBruce Richardson uint16_t key_size, uint16_t iv_size)
37099a2dd95SBruce Richardson {
37199a2dd95SBruce Richardson if (param_range_check(key_size, &capability->cipher.key_size) != 0)
37299a2dd95SBruce Richardson return -1;
37399a2dd95SBruce Richardson
37499a2dd95SBruce Richardson if (param_range_check(iv_size, &capability->cipher.iv_size) != 0)
37599a2dd95SBruce Richardson return -1;
37699a2dd95SBruce Richardson
37799a2dd95SBruce Richardson return 0;
37899a2dd95SBruce Richardson }
37999a2dd95SBruce Richardson
38099a2dd95SBruce Richardson int
rte_cryptodev_sym_capability_check_auth(const struct rte_cryptodev_symmetric_capability * capability,uint16_t key_size,uint16_t digest_size,uint16_t iv_size)38199a2dd95SBruce Richardson rte_cryptodev_sym_capability_check_auth(
38299a2dd95SBruce Richardson const struct rte_cryptodev_symmetric_capability *capability,
38399a2dd95SBruce Richardson uint16_t key_size, uint16_t digest_size, uint16_t iv_size)
38499a2dd95SBruce Richardson {
38599a2dd95SBruce Richardson if (param_range_check(key_size, &capability->auth.key_size) != 0)
38699a2dd95SBruce Richardson return -1;
38799a2dd95SBruce Richardson
38899a2dd95SBruce Richardson if (param_range_check(digest_size, &capability->auth.digest_size) != 0)
38999a2dd95SBruce Richardson return -1;
39099a2dd95SBruce Richardson
39199a2dd95SBruce Richardson if (param_range_check(iv_size, &capability->auth.iv_size) != 0)
39299a2dd95SBruce Richardson return -1;
39399a2dd95SBruce Richardson
39499a2dd95SBruce Richardson return 0;
39599a2dd95SBruce Richardson }
39699a2dd95SBruce Richardson
39799a2dd95SBruce Richardson int
rte_cryptodev_sym_capability_check_aead(const struct rte_cryptodev_symmetric_capability * capability,uint16_t key_size,uint16_t digest_size,uint16_t aad_size,uint16_t iv_size)39899a2dd95SBruce Richardson rte_cryptodev_sym_capability_check_aead(
39999a2dd95SBruce Richardson const struct rte_cryptodev_symmetric_capability *capability,
40099a2dd95SBruce Richardson uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
40199a2dd95SBruce Richardson uint16_t iv_size)
40299a2dd95SBruce Richardson {
40399a2dd95SBruce Richardson if (param_range_check(key_size, &capability->aead.key_size) != 0)
40499a2dd95SBruce Richardson return -1;
40599a2dd95SBruce Richardson
40699a2dd95SBruce Richardson if (param_range_check(digest_size, &capability->aead.digest_size) != 0)
40799a2dd95SBruce Richardson return -1;
40899a2dd95SBruce Richardson
40999a2dd95SBruce Richardson if (param_range_check(aad_size, &capability->aead.aad_size) != 0)
41099a2dd95SBruce Richardson return -1;
41199a2dd95SBruce Richardson
41299a2dd95SBruce Richardson if (param_range_check(iv_size, &capability->aead.iv_size) != 0)
41399a2dd95SBruce Richardson return -1;
41499a2dd95SBruce Richardson
41599a2dd95SBruce Richardson return 0;
41699a2dd95SBruce Richardson }
41799a2dd95SBruce Richardson int
rte_cryptodev_asym_xform_capability_check_optype(const struct rte_cryptodev_asymmetric_xform_capability * capability,enum rte_crypto_asym_op_type op_type)41899a2dd95SBruce Richardson rte_cryptodev_asym_xform_capability_check_optype(
41999a2dd95SBruce Richardson const struct rte_cryptodev_asymmetric_xform_capability *capability,
42099a2dd95SBruce Richardson enum rte_crypto_asym_op_type op_type)
42199a2dd95SBruce Richardson {
42299a2dd95SBruce Richardson if (capability->op_types & (1 << op_type))
42399a2dd95SBruce Richardson return 1;
42499a2dd95SBruce Richardson
42599a2dd95SBruce Richardson return 0;
42699a2dd95SBruce Richardson }
42799a2dd95SBruce Richardson
42899a2dd95SBruce Richardson int
rte_cryptodev_asym_xform_capability_check_modlen(const struct rte_cryptodev_asymmetric_xform_capability * capability,uint16_t modlen)42999a2dd95SBruce Richardson rte_cryptodev_asym_xform_capability_check_modlen(
43099a2dd95SBruce Richardson const struct rte_cryptodev_asymmetric_xform_capability *capability,
43199a2dd95SBruce Richardson uint16_t modlen)
43299a2dd95SBruce Richardson {
43399a2dd95SBruce Richardson /* no need to check for limits, if min or max = 0 */
43499a2dd95SBruce Richardson if (capability->modlen.min != 0) {
43599a2dd95SBruce Richardson if (modlen < capability->modlen.min)
43699a2dd95SBruce Richardson return -1;
43799a2dd95SBruce Richardson }
43899a2dd95SBruce Richardson
43999a2dd95SBruce Richardson if (capability->modlen.max != 0) {
44099a2dd95SBruce Richardson if (modlen > capability->modlen.max)
44199a2dd95SBruce Richardson return -1;
44299a2dd95SBruce Richardson }
44399a2dd95SBruce Richardson
44499a2dd95SBruce Richardson /* in any case, check if given modlen is module increment */
44599a2dd95SBruce Richardson if (capability->modlen.increment != 0) {
44699a2dd95SBruce Richardson if (modlen % (capability->modlen.increment))
44799a2dd95SBruce Richardson return -1;
44899a2dd95SBruce Richardson }
44999a2dd95SBruce Richardson
45099a2dd95SBruce Richardson return 0;
45199a2dd95SBruce Richardson }
45299a2dd95SBruce Richardson
45399a2dd95SBruce Richardson /* spinlock for crypto device enq callbacks */
45499a2dd95SBruce Richardson static rte_spinlock_t rte_cryptodev_callback_lock = RTE_SPINLOCK_INITIALIZER;
45599a2dd95SBruce Richardson
45699a2dd95SBruce Richardson static void
cryptodev_cb_cleanup(struct rte_cryptodev * dev)45799a2dd95SBruce Richardson cryptodev_cb_cleanup(struct rte_cryptodev *dev)
45899a2dd95SBruce Richardson {
45999a2dd95SBruce Richardson struct rte_cryptodev_cb_rcu *list;
46099a2dd95SBruce Richardson struct rte_cryptodev_cb *cb, *next;
46199a2dd95SBruce Richardson uint16_t qp_id;
46299a2dd95SBruce Richardson
46399a2dd95SBruce Richardson if (dev->enq_cbs == NULL && dev->deq_cbs == NULL)
46499a2dd95SBruce Richardson return;
46599a2dd95SBruce Richardson
46699a2dd95SBruce Richardson for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
46799a2dd95SBruce Richardson list = &dev->enq_cbs[qp_id];
46899a2dd95SBruce Richardson cb = list->next;
46999a2dd95SBruce Richardson while (cb != NULL) {
47099a2dd95SBruce Richardson next = cb->next;
47199a2dd95SBruce Richardson rte_free(cb);
47299a2dd95SBruce Richardson cb = next;
47399a2dd95SBruce Richardson }
47499a2dd95SBruce Richardson
47599a2dd95SBruce Richardson rte_free(list->qsbr);
47699a2dd95SBruce Richardson }
47799a2dd95SBruce Richardson
47899a2dd95SBruce Richardson for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
47999a2dd95SBruce Richardson list = &dev->deq_cbs[qp_id];
48099a2dd95SBruce Richardson cb = list->next;
48199a2dd95SBruce Richardson while (cb != NULL) {
48299a2dd95SBruce Richardson next = cb->next;
48399a2dd95SBruce Richardson rte_free(cb);
48499a2dd95SBruce Richardson cb = next;
48599a2dd95SBruce Richardson }
48699a2dd95SBruce Richardson
48799a2dd95SBruce Richardson rte_free(list->qsbr);
48899a2dd95SBruce Richardson }
48999a2dd95SBruce Richardson
49099a2dd95SBruce Richardson rte_free(dev->enq_cbs);
49199a2dd95SBruce Richardson dev->enq_cbs = NULL;
49299a2dd95SBruce Richardson rte_free(dev->deq_cbs);
49399a2dd95SBruce Richardson dev->deq_cbs = NULL;
49499a2dd95SBruce Richardson }
49599a2dd95SBruce Richardson
49699a2dd95SBruce Richardson static int
cryptodev_cb_init(struct rte_cryptodev * dev)49799a2dd95SBruce Richardson cryptodev_cb_init(struct rte_cryptodev *dev)
49899a2dd95SBruce Richardson {
49999a2dd95SBruce Richardson struct rte_cryptodev_cb_rcu *list;
50099a2dd95SBruce Richardson struct rte_rcu_qsbr *qsbr;
50199a2dd95SBruce Richardson uint16_t qp_id;
50299a2dd95SBruce Richardson size_t size;
50399a2dd95SBruce Richardson
50499a2dd95SBruce Richardson /* Max thread set to 1, as one DP thread accessing a queue-pair */
50599a2dd95SBruce Richardson const uint32_t max_threads = 1;
50699a2dd95SBruce Richardson
50799a2dd95SBruce Richardson dev->enq_cbs = rte_zmalloc(NULL,
50899a2dd95SBruce Richardson sizeof(struct rte_cryptodev_cb_rcu) *
50999a2dd95SBruce Richardson dev->data->nb_queue_pairs, 0);
51099a2dd95SBruce Richardson if (dev->enq_cbs == NULL) {
51199a2dd95SBruce Richardson CDEV_LOG_ERR("Failed to allocate memory for enq callbacks");
51299a2dd95SBruce Richardson return -ENOMEM;
51399a2dd95SBruce Richardson }
51499a2dd95SBruce Richardson
51599a2dd95SBruce Richardson dev->deq_cbs = rte_zmalloc(NULL,
51699a2dd95SBruce Richardson sizeof(struct rte_cryptodev_cb_rcu) *
51799a2dd95SBruce Richardson dev->data->nb_queue_pairs, 0);
51899a2dd95SBruce Richardson if (dev->deq_cbs == NULL) {
51999a2dd95SBruce Richardson CDEV_LOG_ERR("Failed to allocate memory for deq callbacks");
52099a2dd95SBruce Richardson rte_free(dev->enq_cbs);
52199a2dd95SBruce Richardson return -ENOMEM;
52299a2dd95SBruce Richardson }
52399a2dd95SBruce Richardson
52499a2dd95SBruce Richardson /* Create RCU QSBR variable */
52599a2dd95SBruce Richardson size = rte_rcu_qsbr_get_memsize(max_threads);
52699a2dd95SBruce Richardson
52799a2dd95SBruce Richardson for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
52899a2dd95SBruce Richardson list = &dev->enq_cbs[qp_id];
52999a2dd95SBruce Richardson qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
53099a2dd95SBruce Richardson if (qsbr == NULL) {
53199a2dd95SBruce Richardson CDEV_LOG_ERR("Failed to allocate memory for RCU on "
53299a2dd95SBruce Richardson "queue_pair_id=%d", qp_id);
53399a2dd95SBruce Richardson goto cb_init_err;
53499a2dd95SBruce Richardson }
53599a2dd95SBruce Richardson
53699a2dd95SBruce Richardson if (rte_rcu_qsbr_init(qsbr, max_threads)) {
53799a2dd95SBruce Richardson CDEV_LOG_ERR("Failed to initialize for RCU on "
53899a2dd95SBruce Richardson "queue_pair_id=%d", qp_id);
53999a2dd95SBruce Richardson goto cb_init_err;
54099a2dd95SBruce Richardson }
54199a2dd95SBruce Richardson
54299a2dd95SBruce Richardson list->qsbr = qsbr;
54399a2dd95SBruce Richardson }
54499a2dd95SBruce Richardson
54599a2dd95SBruce Richardson for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
54699a2dd95SBruce Richardson list = &dev->deq_cbs[qp_id];
54799a2dd95SBruce Richardson qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
54899a2dd95SBruce Richardson if (qsbr == NULL) {
54999a2dd95SBruce Richardson CDEV_LOG_ERR("Failed to allocate memory for RCU on "
55099a2dd95SBruce Richardson "queue_pair_id=%d", qp_id);
55199a2dd95SBruce Richardson goto cb_init_err;
55299a2dd95SBruce Richardson }
55399a2dd95SBruce Richardson
55499a2dd95SBruce Richardson if (rte_rcu_qsbr_init(qsbr, max_threads)) {
55599a2dd95SBruce Richardson CDEV_LOG_ERR("Failed to initialize for RCU on "
55699a2dd95SBruce Richardson "queue_pair_id=%d", qp_id);
55799a2dd95SBruce Richardson goto cb_init_err;
55899a2dd95SBruce Richardson }
55999a2dd95SBruce Richardson
56099a2dd95SBruce Richardson list->qsbr = qsbr;
56199a2dd95SBruce Richardson }
56299a2dd95SBruce Richardson
56399a2dd95SBruce Richardson return 0;
56499a2dd95SBruce Richardson
56599a2dd95SBruce Richardson cb_init_err:
56699a2dd95SBruce Richardson cryptodev_cb_cleanup(dev);
56799a2dd95SBruce Richardson return -ENOMEM;
56899a2dd95SBruce Richardson }
56999a2dd95SBruce Richardson
57099a2dd95SBruce Richardson const char *
rte_cryptodev_get_feature_name(uint64_t flag)57199a2dd95SBruce Richardson rte_cryptodev_get_feature_name(uint64_t flag)
57299a2dd95SBruce Richardson {
57399a2dd95SBruce Richardson switch (flag) {
57499a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
57599a2dd95SBruce Richardson return "SYMMETRIC_CRYPTO";
57699a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
57799a2dd95SBruce Richardson return "ASYMMETRIC_CRYPTO";
57899a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
57999a2dd95SBruce Richardson return "SYM_OPERATION_CHAINING";
58099a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_CPU_SSE:
58199a2dd95SBruce Richardson return "CPU_SSE";
58299a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_CPU_AVX:
58399a2dd95SBruce Richardson return "CPU_AVX";
58499a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_CPU_AVX2:
58599a2dd95SBruce Richardson return "CPU_AVX2";
58699a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_CPU_AVX512:
58799a2dd95SBruce Richardson return "CPU_AVX512";
58899a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_CPU_AESNI:
58999a2dd95SBruce Richardson return "CPU_AESNI";
59099a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_HW_ACCELERATED:
59199a2dd95SBruce Richardson return "HW_ACCELERATED";
59299a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_IN_PLACE_SGL:
59399a2dd95SBruce Richardson return "IN_PLACE_SGL";
59499a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT:
59599a2dd95SBruce Richardson return "OOP_SGL_IN_SGL_OUT";
59699a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT:
59799a2dd95SBruce Richardson return "OOP_SGL_IN_LB_OUT";
59899a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT:
59999a2dd95SBruce Richardson return "OOP_LB_IN_SGL_OUT";
60099a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT:
60199a2dd95SBruce Richardson return "OOP_LB_IN_LB_OUT";
60299a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_CPU_NEON:
60399a2dd95SBruce Richardson return "CPU_NEON";
60499a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_CPU_ARM_CE:
60599a2dd95SBruce Richardson return "CPU_ARM_CE";
60699a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_SECURITY:
60799a2dd95SBruce Richardson return "SECURITY_PROTOCOL";
60899a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP:
60999a2dd95SBruce Richardson return "RSA_PRIV_OP_KEY_EXP";
61099a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT:
61199a2dd95SBruce Richardson return "RSA_PRIV_OP_KEY_QT";
61299a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED:
61399a2dd95SBruce Richardson return "DIGEST_ENCRYPTED";
61499a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO:
61599a2dd95SBruce Richardson return "SYM_CPU_CRYPTO";
61699a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_ASYM_SESSIONLESS:
61799a2dd95SBruce Richardson return "ASYM_SESSIONLESS";
61899a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_SYM_SESSIONLESS:
61999a2dd95SBruce Richardson return "SYM_SESSIONLESS";
62099a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_NON_BYTE_ALIGNED_DATA:
62199a2dd95SBruce Richardson return "NON_BYTE_ALIGNED_DATA";
62299a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS:
62399a2dd95SBruce Richardson return "CIPHER_MULTIPLE_DATA_UNITS";
62499a2dd95SBruce Richardson case RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY:
62599a2dd95SBruce Richardson return "CIPHER_WRAPPED_KEY";
62699a2dd95SBruce Richardson default:
62799a2dd95SBruce Richardson return NULL;
62899a2dd95SBruce Richardson }
62999a2dd95SBruce Richardson }
63099a2dd95SBruce Richardson
63199a2dd95SBruce Richardson struct rte_cryptodev *
rte_cryptodev_pmd_get_dev(uint8_t dev_id)63299a2dd95SBruce Richardson rte_cryptodev_pmd_get_dev(uint8_t dev_id)
63399a2dd95SBruce Richardson {
63499a2dd95SBruce Richardson return &cryptodev_globals.devs[dev_id];
63599a2dd95SBruce Richardson }
63699a2dd95SBruce Richardson
63799a2dd95SBruce Richardson struct rte_cryptodev *
rte_cryptodev_pmd_get_named_dev(const char * name)63899a2dd95SBruce Richardson rte_cryptodev_pmd_get_named_dev(const char *name)
63999a2dd95SBruce Richardson {
64099a2dd95SBruce Richardson struct rte_cryptodev *dev;
64199a2dd95SBruce Richardson unsigned int i;
64299a2dd95SBruce Richardson
64399a2dd95SBruce Richardson if (name == NULL)
64499a2dd95SBruce Richardson return NULL;
64599a2dd95SBruce Richardson
64699a2dd95SBruce Richardson for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
64799a2dd95SBruce Richardson dev = &cryptodev_globals.devs[i];
64899a2dd95SBruce Richardson
64999a2dd95SBruce Richardson if ((dev->attached == RTE_CRYPTODEV_ATTACHED) &&
65099a2dd95SBruce Richardson (strcmp(dev->data->name, name) == 0))
65199a2dd95SBruce Richardson return dev;
65299a2dd95SBruce Richardson }
65399a2dd95SBruce Richardson
65499a2dd95SBruce Richardson return NULL;
65599a2dd95SBruce Richardson }
65699a2dd95SBruce Richardson
65799a2dd95SBruce Richardson static inline uint8_t
rte_cryptodev_is_valid_device_data(uint8_t dev_id)65899a2dd95SBruce Richardson rte_cryptodev_is_valid_device_data(uint8_t dev_id)
65999a2dd95SBruce Richardson {
66099a2dd95SBruce Richardson if (dev_id >= RTE_CRYPTO_MAX_DEVS ||
66199a2dd95SBruce Richardson rte_crypto_devices[dev_id].data == NULL)
66299a2dd95SBruce Richardson return 0;
66399a2dd95SBruce Richardson
66499a2dd95SBruce Richardson return 1;
66599a2dd95SBruce Richardson }
66699a2dd95SBruce Richardson
66799a2dd95SBruce Richardson unsigned int
rte_cryptodev_is_valid_dev(uint8_t dev_id)668e74abd48SAkhil Goyal rte_cryptodev_is_valid_dev(uint8_t dev_id)
66999a2dd95SBruce Richardson {
67099a2dd95SBruce Richardson struct rte_cryptodev *dev = NULL;
67199a2dd95SBruce Richardson
67299a2dd95SBruce Richardson if (!rte_cryptodev_is_valid_device_data(dev_id))
67399a2dd95SBruce Richardson return 0;
67499a2dd95SBruce Richardson
67599a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
67699a2dd95SBruce Richardson if (dev->attached != RTE_CRYPTODEV_ATTACHED)
67799a2dd95SBruce Richardson return 0;
67899a2dd95SBruce Richardson else
67999a2dd95SBruce Richardson return 1;
68099a2dd95SBruce Richardson }
68199a2dd95SBruce Richardson
68299a2dd95SBruce Richardson
68399a2dd95SBruce Richardson int
rte_cryptodev_get_dev_id(const char * name)68499a2dd95SBruce Richardson rte_cryptodev_get_dev_id(const char *name)
68599a2dd95SBruce Richardson {
68699a2dd95SBruce Richardson unsigned i;
68799a2dd95SBruce Richardson
68899a2dd95SBruce Richardson if (name == NULL)
68999a2dd95SBruce Richardson return -1;
69099a2dd95SBruce Richardson
69199a2dd95SBruce Richardson for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
69299a2dd95SBruce Richardson if (!rte_cryptodev_is_valid_device_data(i))
69399a2dd95SBruce Richardson continue;
69499a2dd95SBruce Richardson if ((strcmp(cryptodev_globals.devs[i].data->name, name)
69599a2dd95SBruce Richardson == 0) &&
69699a2dd95SBruce Richardson (cryptodev_globals.devs[i].attached ==
69799a2dd95SBruce Richardson RTE_CRYPTODEV_ATTACHED))
69899a2dd95SBruce Richardson return i;
69999a2dd95SBruce Richardson }
70099a2dd95SBruce Richardson
70199a2dd95SBruce Richardson return -1;
70299a2dd95SBruce Richardson }
70399a2dd95SBruce Richardson
70499a2dd95SBruce Richardson uint8_t
rte_cryptodev_count(void)70599a2dd95SBruce Richardson rte_cryptodev_count(void)
70699a2dd95SBruce Richardson {
70799a2dd95SBruce Richardson return cryptodev_globals.nb_devs;
70899a2dd95SBruce Richardson }
70999a2dd95SBruce Richardson
71099a2dd95SBruce Richardson uint8_t
rte_cryptodev_device_count_by_driver(uint8_t driver_id)71199a2dd95SBruce Richardson rte_cryptodev_device_count_by_driver(uint8_t driver_id)
71299a2dd95SBruce Richardson {
71399a2dd95SBruce Richardson uint8_t i, dev_count = 0;
71499a2dd95SBruce Richardson
71599a2dd95SBruce Richardson for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++)
71699a2dd95SBruce Richardson if (cryptodev_globals.devs[i].driver_id == driver_id &&
71799a2dd95SBruce Richardson cryptodev_globals.devs[i].attached ==
71899a2dd95SBruce Richardson RTE_CRYPTODEV_ATTACHED)
71999a2dd95SBruce Richardson dev_count++;
72099a2dd95SBruce Richardson
72199a2dd95SBruce Richardson return dev_count;
72299a2dd95SBruce Richardson }
72399a2dd95SBruce Richardson
72499a2dd95SBruce Richardson uint8_t
rte_cryptodev_devices_get(const char * driver_name,uint8_t * devices,uint8_t nb_devices)72599a2dd95SBruce Richardson rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
72699a2dd95SBruce Richardson uint8_t nb_devices)
72799a2dd95SBruce Richardson {
72899a2dd95SBruce Richardson uint8_t i, count = 0;
72999a2dd95SBruce Richardson struct rte_cryptodev *devs = cryptodev_globals.devs;
73099a2dd95SBruce Richardson
73199a2dd95SBruce Richardson for (i = 0; i < RTE_CRYPTO_MAX_DEVS && count < nb_devices; i++) {
73299a2dd95SBruce Richardson if (!rte_cryptodev_is_valid_device_data(i))
73399a2dd95SBruce Richardson continue;
73499a2dd95SBruce Richardson
73599a2dd95SBruce Richardson if (devs[i].attached == RTE_CRYPTODEV_ATTACHED) {
73699a2dd95SBruce Richardson int cmp;
73799a2dd95SBruce Richardson
73899a2dd95SBruce Richardson cmp = strncmp(devs[i].device->driver->name,
73999a2dd95SBruce Richardson driver_name,
74099a2dd95SBruce Richardson strlen(driver_name) + 1);
74199a2dd95SBruce Richardson
74299a2dd95SBruce Richardson if (cmp == 0)
74399a2dd95SBruce Richardson devices[count++] = devs[i].data->dev_id;
74499a2dd95SBruce Richardson }
74599a2dd95SBruce Richardson }
74699a2dd95SBruce Richardson
74799a2dd95SBruce Richardson return count;
74899a2dd95SBruce Richardson }
74999a2dd95SBruce Richardson
75099a2dd95SBruce Richardson void *
rte_cryptodev_get_sec_ctx(uint8_t dev_id)75199a2dd95SBruce Richardson rte_cryptodev_get_sec_ctx(uint8_t dev_id)
75299a2dd95SBruce Richardson {
75399a2dd95SBruce Richardson if (dev_id < RTE_CRYPTO_MAX_DEVS &&
75499a2dd95SBruce Richardson (rte_crypto_devices[dev_id].feature_flags &
75599a2dd95SBruce Richardson RTE_CRYPTODEV_FF_SECURITY))
75699a2dd95SBruce Richardson return rte_crypto_devices[dev_id].security_ctx;
75799a2dd95SBruce Richardson
75899a2dd95SBruce Richardson return NULL;
75999a2dd95SBruce Richardson }
76099a2dd95SBruce Richardson
76199a2dd95SBruce Richardson int
rte_cryptodev_socket_id(uint8_t dev_id)76299a2dd95SBruce Richardson rte_cryptodev_socket_id(uint8_t dev_id)
76399a2dd95SBruce Richardson {
76499a2dd95SBruce Richardson struct rte_cryptodev *dev;
76599a2dd95SBruce Richardson
766e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id))
76799a2dd95SBruce Richardson return -1;
76899a2dd95SBruce Richardson
76999a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
77099a2dd95SBruce Richardson
77199a2dd95SBruce Richardson return dev->data->socket_id;
77299a2dd95SBruce Richardson }
77399a2dd95SBruce Richardson
77499a2dd95SBruce Richardson static inline int
rte_cryptodev_data_alloc(uint8_t dev_id,struct rte_cryptodev_data ** data,int socket_id)77599a2dd95SBruce Richardson rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
77699a2dd95SBruce Richardson int socket_id)
77799a2dd95SBruce Richardson {
77899a2dd95SBruce Richardson char mz_name[RTE_MEMZONE_NAMESIZE];
77999a2dd95SBruce Richardson const struct rte_memzone *mz;
78099a2dd95SBruce Richardson int n;
78199a2dd95SBruce Richardson
78299a2dd95SBruce Richardson /* generate memzone name */
78399a2dd95SBruce Richardson n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
78499a2dd95SBruce Richardson if (n >= (int)sizeof(mz_name))
78599a2dd95SBruce Richardson return -EINVAL;
78699a2dd95SBruce Richardson
78799a2dd95SBruce Richardson if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
78899a2dd95SBruce Richardson mz = rte_memzone_reserve(mz_name,
78999a2dd95SBruce Richardson sizeof(struct rte_cryptodev_data),
79099a2dd95SBruce Richardson socket_id, 0);
79199a2dd95SBruce Richardson CDEV_LOG_DEBUG("PRIMARY:reserved memzone for %s (%p)",
79299a2dd95SBruce Richardson mz_name, mz);
79399a2dd95SBruce Richardson } else {
79499a2dd95SBruce Richardson mz = rte_memzone_lookup(mz_name);
79599a2dd95SBruce Richardson CDEV_LOG_DEBUG("SECONDARY:looked up memzone for %s (%p)",
79699a2dd95SBruce Richardson mz_name, mz);
79799a2dd95SBruce Richardson }
79899a2dd95SBruce Richardson
79999a2dd95SBruce Richardson if (mz == NULL)
80099a2dd95SBruce Richardson return -ENOMEM;
80199a2dd95SBruce Richardson
80299a2dd95SBruce Richardson *data = mz->addr;
80399a2dd95SBruce Richardson if (rte_eal_process_type() == RTE_PROC_PRIMARY)
80499a2dd95SBruce Richardson memset(*data, 0, sizeof(struct rte_cryptodev_data));
80599a2dd95SBruce Richardson
80699a2dd95SBruce Richardson return 0;
80799a2dd95SBruce Richardson }
80899a2dd95SBruce Richardson
80999a2dd95SBruce Richardson static inline int
rte_cryptodev_data_free(uint8_t dev_id,struct rte_cryptodev_data ** data)81099a2dd95SBruce Richardson rte_cryptodev_data_free(uint8_t dev_id, struct rte_cryptodev_data **data)
81199a2dd95SBruce Richardson {
81299a2dd95SBruce Richardson char mz_name[RTE_MEMZONE_NAMESIZE];
81399a2dd95SBruce Richardson const struct rte_memzone *mz;
81499a2dd95SBruce Richardson int n;
81599a2dd95SBruce Richardson
81699a2dd95SBruce Richardson /* generate memzone name */
81799a2dd95SBruce Richardson n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
81899a2dd95SBruce Richardson if (n >= (int)sizeof(mz_name))
81999a2dd95SBruce Richardson return -EINVAL;
82099a2dd95SBruce Richardson
82199a2dd95SBruce Richardson mz = rte_memzone_lookup(mz_name);
82299a2dd95SBruce Richardson if (mz == NULL)
82399a2dd95SBruce Richardson return -ENOMEM;
82499a2dd95SBruce Richardson
82599a2dd95SBruce Richardson RTE_ASSERT(*data == mz->addr);
82699a2dd95SBruce Richardson *data = NULL;
82799a2dd95SBruce Richardson
82899a2dd95SBruce Richardson if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
82999a2dd95SBruce Richardson CDEV_LOG_DEBUG("PRIMARY:free memzone of %s (%p)",
83099a2dd95SBruce Richardson mz_name, mz);
83199a2dd95SBruce Richardson return rte_memzone_free(mz);
83299a2dd95SBruce Richardson } else {
83399a2dd95SBruce Richardson CDEV_LOG_DEBUG("SECONDARY:don't free memzone of %s (%p)",
83499a2dd95SBruce Richardson mz_name, mz);
83599a2dd95SBruce Richardson }
83699a2dd95SBruce Richardson
83799a2dd95SBruce Richardson return 0;
83899a2dd95SBruce Richardson }
83999a2dd95SBruce Richardson
84099a2dd95SBruce Richardson static uint8_t
rte_cryptodev_find_free_device_index(void)84199a2dd95SBruce Richardson rte_cryptodev_find_free_device_index(void)
84299a2dd95SBruce Richardson {
84399a2dd95SBruce Richardson uint8_t dev_id;
84499a2dd95SBruce Richardson
84599a2dd95SBruce Richardson for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
84699a2dd95SBruce Richardson if (rte_crypto_devices[dev_id].attached ==
84799a2dd95SBruce Richardson RTE_CRYPTODEV_DETACHED)
84899a2dd95SBruce Richardson return dev_id;
84999a2dd95SBruce Richardson }
85099a2dd95SBruce Richardson return RTE_CRYPTO_MAX_DEVS;
85199a2dd95SBruce Richardson }
85299a2dd95SBruce Richardson
85399a2dd95SBruce Richardson struct rte_cryptodev *
rte_cryptodev_pmd_allocate(const char * name,int socket_id)85499a2dd95SBruce Richardson rte_cryptodev_pmd_allocate(const char *name, int socket_id)
85599a2dd95SBruce Richardson {
85699a2dd95SBruce Richardson struct rte_cryptodev *cryptodev;
85799a2dd95SBruce Richardson uint8_t dev_id;
85899a2dd95SBruce Richardson
85999a2dd95SBruce Richardson if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
86099a2dd95SBruce Richardson CDEV_LOG_ERR("Crypto device with name %s already "
86199a2dd95SBruce Richardson "allocated!", name);
86299a2dd95SBruce Richardson return NULL;
86399a2dd95SBruce Richardson }
86499a2dd95SBruce Richardson
86599a2dd95SBruce Richardson dev_id = rte_cryptodev_find_free_device_index();
86699a2dd95SBruce Richardson if (dev_id == RTE_CRYPTO_MAX_DEVS) {
86799a2dd95SBruce Richardson CDEV_LOG_ERR("Reached maximum number of crypto devices");
86899a2dd95SBruce Richardson return NULL;
86999a2dd95SBruce Richardson }
87099a2dd95SBruce Richardson
87199a2dd95SBruce Richardson cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
87299a2dd95SBruce Richardson
87399a2dd95SBruce Richardson if (cryptodev->data == NULL) {
87499a2dd95SBruce Richardson struct rte_cryptodev_data **cryptodev_data =
87599a2dd95SBruce Richardson &cryptodev_globals.data[dev_id];
87699a2dd95SBruce Richardson
87799a2dd95SBruce Richardson int retval = rte_cryptodev_data_alloc(dev_id, cryptodev_data,
87899a2dd95SBruce Richardson socket_id);
87999a2dd95SBruce Richardson
88099a2dd95SBruce Richardson if (retval < 0 || *cryptodev_data == NULL)
88199a2dd95SBruce Richardson return NULL;
88299a2dd95SBruce Richardson
88399a2dd95SBruce Richardson cryptodev->data = *cryptodev_data;
88499a2dd95SBruce Richardson
88599a2dd95SBruce Richardson if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
88699a2dd95SBruce Richardson strlcpy(cryptodev->data->name, name,
88799a2dd95SBruce Richardson RTE_CRYPTODEV_NAME_MAX_LEN);
88899a2dd95SBruce Richardson
88999a2dd95SBruce Richardson cryptodev->data->dev_id = dev_id;
89099a2dd95SBruce Richardson cryptodev->data->socket_id = socket_id;
89199a2dd95SBruce Richardson cryptodev->data->dev_started = 0;
89299a2dd95SBruce Richardson CDEV_LOG_DEBUG("PRIMARY:init data");
89399a2dd95SBruce Richardson }
89499a2dd95SBruce Richardson
89599a2dd95SBruce Richardson CDEV_LOG_DEBUG("Data for %s: dev_id %d, socket %d, started %d",
89699a2dd95SBruce Richardson cryptodev->data->name,
89799a2dd95SBruce Richardson cryptodev->data->dev_id,
89899a2dd95SBruce Richardson cryptodev->data->socket_id,
89999a2dd95SBruce Richardson cryptodev->data->dev_started);
90099a2dd95SBruce Richardson
90199a2dd95SBruce Richardson /* init user callbacks */
90299a2dd95SBruce Richardson TAILQ_INIT(&(cryptodev->link_intr_cbs));
90399a2dd95SBruce Richardson
90499a2dd95SBruce Richardson cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
90599a2dd95SBruce Richardson
90699a2dd95SBruce Richardson cryptodev_globals.nb_devs++;
90799a2dd95SBruce Richardson }
90899a2dd95SBruce Richardson
90999a2dd95SBruce Richardson return cryptodev;
91099a2dd95SBruce Richardson }
91199a2dd95SBruce Richardson
91299a2dd95SBruce Richardson int
rte_cryptodev_pmd_release_device(struct rte_cryptodev * cryptodev)91399a2dd95SBruce Richardson rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
91499a2dd95SBruce Richardson {
91599a2dd95SBruce Richardson int ret;
91699a2dd95SBruce Richardson uint8_t dev_id;
91799a2dd95SBruce Richardson
91899a2dd95SBruce Richardson if (cryptodev == NULL)
91999a2dd95SBruce Richardson return -EINVAL;
92099a2dd95SBruce Richardson
92199a2dd95SBruce Richardson dev_id = cryptodev->data->dev_id;
92299a2dd95SBruce Richardson
9232fd66f75SAkhil Goyal cryptodev_fp_ops_reset(rte_crypto_fp_ops + dev_id);
9242fd66f75SAkhil Goyal
92599a2dd95SBruce Richardson /* Close device only if device operations have been set */
92699a2dd95SBruce Richardson if (cryptodev->dev_ops) {
92799a2dd95SBruce Richardson ret = rte_cryptodev_close(dev_id);
92899a2dd95SBruce Richardson if (ret < 0)
92999a2dd95SBruce Richardson return ret;
93099a2dd95SBruce Richardson }
93199a2dd95SBruce Richardson
93299a2dd95SBruce Richardson ret = rte_cryptodev_data_free(dev_id, &cryptodev_globals.data[dev_id]);
93399a2dd95SBruce Richardson if (ret < 0)
93499a2dd95SBruce Richardson return ret;
93599a2dd95SBruce Richardson
93699a2dd95SBruce Richardson cryptodev->attached = RTE_CRYPTODEV_DETACHED;
93799a2dd95SBruce Richardson cryptodev_globals.nb_devs--;
93899a2dd95SBruce Richardson return 0;
93999a2dd95SBruce Richardson }
94099a2dd95SBruce Richardson
94199a2dd95SBruce Richardson uint16_t
rte_cryptodev_queue_pair_count(uint8_t dev_id)94299a2dd95SBruce Richardson rte_cryptodev_queue_pair_count(uint8_t dev_id)
94399a2dd95SBruce Richardson {
94499a2dd95SBruce Richardson struct rte_cryptodev *dev;
94599a2dd95SBruce Richardson
94699a2dd95SBruce Richardson if (!rte_cryptodev_is_valid_device_data(dev_id)) {
94799a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
94899a2dd95SBruce Richardson return 0;
94999a2dd95SBruce Richardson }
95099a2dd95SBruce Richardson
95199a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
95299a2dd95SBruce Richardson return dev->data->nb_queue_pairs;
95399a2dd95SBruce Richardson }
95499a2dd95SBruce Richardson
95599a2dd95SBruce Richardson static int
rte_cryptodev_queue_pairs_config(struct rte_cryptodev * dev,uint16_t nb_qpairs,int socket_id)95699a2dd95SBruce Richardson rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
95799a2dd95SBruce Richardson int socket_id)
95899a2dd95SBruce Richardson {
95999a2dd95SBruce Richardson struct rte_cryptodev_info dev_info;
96099a2dd95SBruce Richardson void **qp;
96199a2dd95SBruce Richardson unsigned i;
96299a2dd95SBruce Richardson
96399a2dd95SBruce Richardson if ((dev == NULL) || (nb_qpairs < 1)) {
96499a2dd95SBruce Richardson CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
96599a2dd95SBruce Richardson dev, nb_qpairs);
96699a2dd95SBruce Richardson return -EINVAL;
96799a2dd95SBruce Richardson }
96899a2dd95SBruce Richardson
96999a2dd95SBruce Richardson CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
97099a2dd95SBruce Richardson nb_qpairs, dev->data->dev_id);
97199a2dd95SBruce Richardson
97299a2dd95SBruce Richardson memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
97399a2dd95SBruce Richardson
97499a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
97599a2dd95SBruce Richardson (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
97699a2dd95SBruce Richardson
97799a2dd95SBruce Richardson if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
97899a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
97999a2dd95SBruce Richardson nb_qpairs, dev->data->dev_id);
98099a2dd95SBruce Richardson return -EINVAL;
98199a2dd95SBruce Richardson }
98299a2dd95SBruce Richardson
98399a2dd95SBruce Richardson if (dev->data->queue_pairs == NULL) { /* first time configuration */
98499a2dd95SBruce Richardson dev->data->queue_pairs = rte_zmalloc_socket(
98599a2dd95SBruce Richardson "cryptodev->queue_pairs",
9867f3876adSAkhil Goyal sizeof(dev->data->queue_pairs[0]) *
9877f3876adSAkhil Goyal dev_info.max_nb_queue_pairs,
98899a2dd95SBruce Richardson RTE_CACHE_LINE_SIZE, socket_id);
98999a2dd95SBruce Richardson
99099a2dd95SBruce Richardson if (dev->data->queue_pairs == NULL) {
99199a2dd95SBruce Richardson dev->data->nb_queue_pairs = 0;
99299a2dd95SBruce Richardson CDEV_LOG_ERR("failed to get memory for qp meta data, "
99399a2dd95SBruce Richardson "nb_queues %u",
99499a2dd95SBruce Richardson nb_qpairs);
99599a2dd95SBruce Richardson return -(ENOMEM);
99699a2dd95SBruce Richardson }
99799a2dd95SBruce Richardson } else { /* re-configure */
99899a2dd95SBruce Richardson int ret;
99999a2dd95SBruce Richardson uint16_t old_nb_queues = dev->data->nb_queue_pairs;
100099a2dd95SBruce Richardson
100199a2dd95SBruce Richardson qp = dev->data->queue_pairs;
100299a2dd95SBruce Richardson
100399a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
100499a2dd95SBruce Richardson -ENOTSUP);
100599a2dd95SBruce Richardson
100699a2dd95SBruce Richardson for (i = nb_qpairs; i < old_nb_queues; i++) {
100799a2dd95SBruce Richardson ret = (*dev->dev_ops->queue_pair_release)(dev, i);
100899a2dd95SBruce Richardson if (ret < 0)
100999a2dd95SBruce Richardson return ret;
10107f3876adSAkhil Goyal qp[i] = NULL;
101199a2dd95SBruce Richardson }
101299a2dd95SBruce Richardson
101399a2dd95SBruce Richardson }
101499a2dd95SBruce Richardson dev->data->nb_queue_pairs = nb_qpairs;
101599a2dd95SBruce Richardson return 0;
101699a2dd95SBruce Richardson }
101799a2dd95SBruce Richardson
101899a2dd95SBruce Richardson int
rte_cryptodev_configure(uint8_t dev_id,struct rte_cryptodev_config * config)101999a2dd95SBruce Richardson rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
102099a2dd95SBruce Richardson {
102199a2dd95SBruce Richardson struct rte_cryptodev *dev;
102299a2dd95SBruce Richardson int diag;
102399a2dd95SBruce Richardson
1024e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
102599a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
102699a2dd95SBruce Richardson return -EINVAL;
102799a2dd95SBruce Richardson }
102899a2dd95SBruce Richardson
102999a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
103099a2dd95SBruce Richardson
103199a2dd95SBruce Richardson if (dev->data->dev_started) {
103299a2dd95SBruce Richardson CDEV_LOG_ERR(
103399a2dd95SBruce Richardson "device %d must be stopped to allow configuration", dev_id);
103499a2dd95SBruce Richardson return -EBUSY;
103599a2dd95SBruce Richardson }
103699a2dd95SBruce Richardson
103799a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
103899a2dd95SBruce Richardson
103999a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_callback_lock);
104099a2dd95SBruce Richardson cryptodev_cb_cleanup(dev);
104199a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_callback_lock);
104299a2dd95SBruce Richardson
104399a2dd95SBruce Richardson /* Setup new number of queue pairs and reconfigure device. */
104499a2dd95SBruce Richardson diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
104599a2dd95SBruce Richardson config->socket_id);
104699a2dd95SBruce Richardson if (diag != 0) {
104799a2dd95SBruce Richardson CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
104899a2dd95SBruce Richardson dev_id, diag);
104999a2dd95SBruce Richardson return diag;
105099a2dd95SBruce Richardson }
105199a2dd95SBruce Richardson
105299a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_callback_lock);
105399a2dd95SBruce Richardson diag = cryptodev_cb_init(dev);
105499a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_callback_lock);
105599a2dd95SBruce Richardson if (diag) {
105699a2dd95SBruce Richardson CDEV_LOG_ERR("Callback init failed for dev_id=%d", dev_id);
105799a2dd95SBruce Richardson return diag;
105899a2dd95SBruce Richardson }
105999a2dd95SBruce Richardson
106099a2dd95SBruce Richardson rte_cryptodev_trace_configure(dev_id, config);
106199a2dd95SBruce Richardson return (*dev->dev_ops->dev_configure)(dev, config);
106299a2dd95SBruce Richardson }
106399a2dd95SBruce Richardson
106499a2dd95SBruce Richardson int
rte_cryptodev_start(uint8_t dev_id)106599a2dd95SBruce Richardson rte_cryptodev_start(uint8_t dev_id)
106699a2dd95SBruce Richardson {
106799a2dd95SBruce Richardson struct rte_cryptodev *dev;
106899a2dd95SBruce Richardson int diag;
106999a2dd95SBruce Richardson
107099a2dd95SBruce Richardson CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
107199a2dd95SBruce Richardson
1072e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
107399a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
107499a2dd95SBruce Richardson return -EINVAL;
107599a2dd95SBruce Richardson }
107699a2dd95SBruce Richardson
107799a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
107899a2dd95SBruce Richardson
107999a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
108099a2dd95SBruce Richardson
108199a2dd95SBruce Richardson if (dev->data->dev_started != 0) {
108299a2dd95SBruce Richardson CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
108399a2dd95SBruce Richardson dev_id);
108499a2dd95SBruce Richardson return 0;
108599a2dd95SBruce Richardson }
108699a2dd95SBruce Richardson
108799a2dd95SBruce Richardson diag = (*dev->dev_ops->dev_start)(dev);
10882fd66f75SAkhil Goyal /* expose selection of PMD fast-path functions */
10892fd66f75SAkhil Goyal cryptodev_fp_ops_set(rte_crypto_fp_ops + dev_id, dev);
10902fd66f75SAkhil Goyal
109199a2dd95SBruce Richardson rte_cryptodev_trace_start(dev_id, diag);
109299a2dd95SBruce Richardson if (diag == 0)
109399a2dd95SBruce Richardson dev->data->dev_started = 1;
109499a2dd95SBruce Richardson else
109599a2dd95SBruce Richardson return diag;
109699a2dd95SBruce Richardson
109799a2dd95SBruce Richardson return 0;
109899a2dd95SBruce Richardson }
109999a2dd95SBruce Richardson
110099a2dd95SBruce Richardson void
rte_cryptodev_stop(uint8_t dev_id)110199a2dd95SBruce Richardson rte_cryptodev_stop(uint8_t dev_id)
110299a2dd95SBruce Richardson {
110399a2dd95SBruce Richardson struct rte_cryptodev *dev;
110499a2dd95SBruce Richardson
1105e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
110699a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
110799a2dd95SBruce Richardson return;
110899a2dd95SBruce Richardson }
110999a2dd95SBruce Richardson
111099a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
111199a2dd95SBruce Richardson
111299a2dd95SBruce Richardson RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
111399a2dd95SBruce Richardson
111499a2dd95SBruce Richardson if (dev->data->dev_started == 0) {
111599a2dd95SBruce Richardson CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
111699a2dd95SBruce Richardson dev_id);
111799a2dd95SBruce Richardson return;
111899a2dd95SBruce Richardson }
111999a2dd95SBruce Richardson
11202fd66f75SAkhil Goyal /* point fast-path functions to dummy ones */
11212fd66f75SAkhil Goyal cryptodev_fp_ops_reset(rte_crypto_fp_ops + dev_id);
11222fd66f75SAkhil Goyal
112399a2dd95SBruce Richardson (*dev->dev_ops->dev_stop)(dev);
112499a2dd95SBruce Richardson rte_cryptodev_trace_stop(dev_id);
112599a2dd95SBruce Richardson dev->data->dev_started = 0;
112699a2dd95SBruce Richardson }
112799a2dd95SBruce Richardson
112899a2dd95SBruce Richardson int
rte_cryptodev_close(uint8_t dev_id)112999a2dd95SBruce Richardson rte_cryptodev_close(uint8_t dev_id)
113099a2dd95SBruce Richardson {
113199a2dd95SBruce Richardson struct rte_cryptodev *dev;
113299a2dd95SBruce Richardson int retval;
113399a2dd95SBruce Richardson
1134e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
113599a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
113699a2dd95SBruce Richardson return -1;
113799a2dd95SBruce Richardson }
113899a2dd95SBruce Richardson
113999a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
114099a2dd95SBruce Richardson
114199a2dd95SBruce Richardson /* Device must be stopped before it can be closed */
114299a2dd95SBruce Richardson if (dev->data->dev_started == 1) {
114399a2dd95SBruce Richardson CDEV_LOG_ERR("Device %u must be stopped before closing",
114499a2dd95SBruce Richardson dev_id);
114599a2dd95SBruce Richardson return -EBUSY;
114699a2dd95SBruce Richardson }
114799a2dd95SBruce Richardson
114899a2dd95SBruce Richardson /* We can't close the device if there are outstanding sessions in use */
114999a2dd95SBruce Richardson if (dev->data->session_pool != NULL) {
115099a2dd95SBruce Richardson if (!rte_mempool_full(dev->data->session_pool)) {
115199a2dd95SBruce Richardson CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
115299a2dd95SBruce Richardson "has sessions still in use, free "
115399a2dd95SBruce Richardson "all sessions before calling close",
115499a2dd95SBruce Richardson (unsigned)dev_id);
115599a2dd95SBruce Richardson return -EBUSY;
115699a2dd95SBruce Richardson }
115799a2dd95SBruce Richardson }
115899a2dd95SBruce Richardson
115999a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
116099a2dd95SBruce Richardson retval = (*dev->dev_ops->dev_close)(dev);
116199a2dd95SBruce Richardson rte_cryptodev_trace_close(dev_id, retval);
116299a2dd95SBruce Richardson
116399a2dd95SBruce Richardson if (retval < 0)
116499a2dd95SBruce Richardson return retval;
116599a2dd95SBruce Richardson
116699a2dd95SBruce Richardson return 0;
116799a2dd95SBruce Richardson }
116899a2dd95SBruce Richardson
116999a2dd95SBruce Richardson int
rte_cryptodev_get_qp_status(uint8_t dev_id,uint16_t queue_pair_id)117099a2dd95SBruce Richardson rte_cryptodev_get_qp_status(uint8_t dev_id, uint16_t queue_pair_id)
117199a2dd95SBruce Richardson {
117299a2dd95SBruce Richardson struct rte_cryptodev *dev;
117399a2dd95SBruce Richardson
1174e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
117599a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
117699a2dd95SBruce Richardson return -EINVAL;
117799a2dd95SBruce Richardson }
117899a2dd95SBruce Richardson
117999a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
118099a2dd95SBruce Richardson if (queue_pair_id >= dev->data->nb_queue_pairs) {
118199a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
118299a2dd95SBruce Richardson return -EINVAL;
118399a2dd95SBruce Richardson }
118499a2dd95SBruce Richardson void **qps = dev->data->queue_pairs;
118599a2dd95SBruce Richardson
118699a2dd95SBruce Richardson if (qps[queue_pair_id]) {
118799a2dd95SBruce Richardson CDEV_LOG_DEBUG("qp %d on dev %d is initialised",
118899a2dd95SBruce Richardson queue_pair_id, dev_id);
118999a2dd95SBruce Richardson return 1;
119099a2dd95SBruce Richardson }
119199a2dd95SBruce Richardson
119299a2dd95SBruce Richardson CDEV_LOG_DEBUG("qp %d on dev %d is not initialised",
119399a2dd95SBruce Richardson queue_pair_id, dev_id);
119499a2dd95SBruce Richardson
119599a2dd95SBruce Richardson return 0;
119699a2dd95SBruce Richardson }
119799a2dd95SBruce Richardson
119899a2dd95SBruce Richardson int
rte_cryptodev_queue_pair_setup(uint8_t dev_id,uint16_t queue_pair_id,const struct rte_cryptodev_qp_conf * qp_conf,int socket_id)119999a2dd95SBruce Richardson rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
120099a2dd95SBruce Richardson const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
120199a2dd95SBruce Richardson
120299a2dd95SBruce Richardson {
120399a2dd95SBruce Richardson struct rte_cryptodev *dev;
120499a2dd95SBruce Richardson
1205e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
120699a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
120799a2dd95SBruce Richardson return -EINVAL;
120899a2dd95SBruce Richardson }
120999a2dd95SBruce Richardson
121099a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
121199a2dd95SBruce Richardson if (queue_pair_id >= dev->data->nb_queue_pairs) {
121299a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
121399a2dd95SBruce Richardson return -EINVAL;
121499a2dd95SBruce Richardson }
121599a2dd95SBruce Richardson
121699a2dd95SBruce Richardson if (!qp_conf) {
121799a2dd95SBruce Richardson CDEV_LOG_ERR("qp_conf cannot be NULL\n");
121899a2dd95SBruce Richardson return -EINVAL;
121999a2dd95SBruce Richardson }
122099a2dd95SBruce Richardson
122199a2dd95SBruce Richardson if ((qp_conf->mp_session && !qp_conf->mp_session_private) ||
122299a2dd95SBruce Richardson (!qp_conf->mp_session && qp_conf->mp_session_private)) {
122399a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid mempools\n");
122499a2dd95SBruce Richardson return -EINVAL;
122599a2dd95SBruce Richardson }
122699a2dd95SBruce Richardson
122799a2dd95SBruce Richardson if (qp_conf->mp_session) {
122899a2dd95SBruce Richardson struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
122999a2dd95SBruce Richardson uint32_t obj_size = qp_conf->mp_session->elt_size;
123099a2dd95SBruce Richardson uint32_t obj_priv_size = qp_conf->mp_session_private->elt_size;
123199a2dd95SBruce Richardson struct rte_cryptodev_sym_session s = {0};
123299a2dd95SBruce Richardson
123399a2dd95SBruce Richardson pool_priv = rte_mempool_get_priv(qp_conf->mp_session);
123499a2dd95SBruce Richardson if (!pool_priv || qp_conf->mp_session->private_data_size <
123599a2dd95SBruce Richardson sizeof(*pool_priv)) {
123699a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid mempool\n");
123799a2dd95SBruce Richardson return -EINVAL;
123899a2dd95SBruce Richardson }
123999a2dd95SBruce Richardson
124099a2dd95SBruce Richardson s.nb_drivers = pool_priv->nb_drivers;
124199a2dd95SBruce Richardson s.user_data_sz = pool_priv->user_data_sz;
124299a2dd95SBruce Richardson
124399a2dd95SBruce Richardson if ((rte_cryptodev_sym_get_existing_header_session_size(&s) >
124499a2dd95SBruce Richardson obj_size) || (s.nb_drivers <= dev->driver_id) ||
124599a2dd95SBruce Richardson rte_cryptodev_sym_get_private_session_size(dev_id) >
124699a2dd95SBruce Richardson obj_priv_size) {
124799a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid mempool\n");
124899a2dd95SBruce Richardson return -EINVAL;
124999a2dd95SBruce Richardson }
125099a2dd95SBruce Richardson }
125199a2dd95SBruce Richardson
125299a2dd95SBruce Richardson if (dev->data->dev_started) {
125399a2dd95SBruce Richardson CDEV_LOG_ERR(
125499a2dd95SBruce Richardson "device %d must be stopped to allow configuration", dev_id);
125599a2dd95SBruce Richardson return -EBUSY;
125699a2dd95SBruce Richardson }
125799a2dd95SBruce Richardson
125899a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
125999a2dd95SBruce Richardson
126099a2dd95SBruce Richardson rte_cryptodev_trace_queue_pair_setup(dev_id, queue_pair_id, qp_conf);
126199a2dd95SBruce Richardson return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
126299a2dd95SBruce Richardson socket_id);
126399a2dd95SBruce Richardson }
126499a2dd95SBruce Richardson
126599a2dd95SBruce Richardson struct rte_cryptodev_cb *
rte_cryptodev_add_enq_callback(uint8_t dev_id,uint16_t qp_id,rte_cryptodev_callback_fn cb_fn,void * cb_arg)126699a2dd95SBruce Richardson rte_cryptodev_add_enq_callback(uint8_t dev_id,
126799a2dd95SBruce Richardson uint16_t qp_id,
126899a2dd95SBruce Richardson rte_cryptodev_callback_fn cb_fn,
126999a2dd95SBruce Richardson void *cb_arg)
127099a2dd95SBruce Richardson {
127199a2dd95SBruce Richardson struct rte_cryptodev *dev;
127299a2dd95SBruce Richardson struct rte_cryptodev_cb_rcu *list;
127399a2dd95SBruce Richardson struct rte_cryptodev_cb *cb, *tail;
127499a2dd95SBruce Richardson
127599a2dd95SBruce Richardson if (!cb_fn) {
127699a2dd95SBruce Richardson CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
127799a2dd95SBruce Richardson rte_errno = EINVAL;
127899a2dd95SBruce Richardson return NULL;
127999a2dd95SBruce Richardson }
128099a2dd95SBruce Richardson
1281e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
128299a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
128399a2dd95SBruce Richardson rte_errno = ENODEV;
128499a2dd95SBruce Richardson return NULL;
128599a2dd95SBruce Richardson }
128699a2dd95SBruce Richardson
128799a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
128899a2dd95SBruce Richardson if (qp_id >= dev->data->nb_queue_pairs) {
128999a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
129099a2dd95SBruce Richardson rte_errno = ENODEV;
129199a2dd95SBruce Richardson return NULL;
129299a2dd95SBruce Richardson }
129399a2dd95SBruce Richardson
129499a2dd95SBruce Richardson cb = rte_zmalloc(NULL, sizeof(*cb), 0);
129599a2dd95SBruce Richardson if (cb == NULL) {
129699a2dd95SBruce Richardson CDEV_LOG_ERR("Failed to allocate memory for callback on "
129799a2dd95SBruce Richardson "dev=%d, queue_pair_id=%d", dev_id, qp_id);
129899a2dd95SBruce Richardson rte_errno = ENOMEM;
129999a2dd95SBruce Richardson return NULL;
130099a2dd95SBruce Richardson }
130199a2dd95SBruce Richardson
130299a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_callback_lock);
130399a2dd95SBruce Richardson
130499a2dd95SBruce Richardson cb->fn = cb_fn;
130599a2dd95SBruce Richardson cb->arg = cb_arg;
130699a2dd95SBruce Richardson
130799a2dd95SBruce Richardson /* Add the callbacks in fifo order. */
130899a2dd95SBruce Richardson list = &dev->enq_cbs[qp_id];
130999a2dd95SBruce Richardson tail = list->next;
131099a2dd95SBruce Richardson
131199a2dd95SBruce Richardson if (tail) {
131299a2dd95SBruce Richardson while (tail->next)
131399a2dd95SBruce Richardson tail = tail->next;
131499a2dd95SBruce Richardson /* Stores to cb->fn and cb->param should complete before
131599a2dd95SBruce Richardson * cb is visible to data plane.
131699a2dd95SBruce Richardson */
131799a2dd95SBruce Richardson __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
131899a2dd95SBruce Richardson } else {
131999a2dd95SBruce Richardson /* Stores to cb->fn and cb->param should complete before
132099a2dd95SBruce Richardson * cb is visible to data plane.
132199a2dd95SBruce Richardson */
132299a2dd95SBruce Richardson __atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
132399a2dd95SBruce Richardson }
132499a2dd95SBruce Richardson
132599a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_callback_lock);
132699a2dd95SBruce Richardson
132799a2dd95SBruce Richardson return cb;
132899a2dd95SBruce Richardson }
132999a2dd95SBruce Richardson
133099a2dd95SBruce Richardson int
rte_cryptodev_remove_enq_callback(uint8_t dev_id,uint16_t qp_id,struct rte_cryptodev_cb * cb)133199a2dd95SBruce Richardson rte_cryptodev_remove_enq_callback(uint8_t dev_id,
133299a2dd95SBruce Richardson uint16_t qp_id,
133399a2dd95SBruce Richardson struct rte_cryptodev_cb *cb)
133499a2dd95SBruce Richardson {
133599a2dd95SBruce Richardson struct rte_cryptodev *dev;
133699a2dd95SBruce Richardson struct rte_cryptodev_cb **prev_cb, *curr_cb;
133799a2dd95SBruce Richardson struct rte_cryptodev_cb_rcu *list;
133899a2dd95SBruce Richardson int ret;
133999a2dd95SBruce Richardson
134099a2dd95SBruce Richardson ret = -EINVAL;
134199a2dd95SBruce Richardson
134299a2dd95SBruce Richardson if (!cb) {
134399a2dd95SBruce Richardson CDEV_LOG_ERR("Callback is NULL");
134499a2dd95SBruce Richardson return -EINVAL;
134599a2dd95SBruce Richardson }
134699a2dd95SBruce Richardson
1347e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
134899a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
134999a2dd95SBruce Richardson return -ENODEV;
135099a2dd95SBruce Richardson }
135199a2dd95SBruce Richardson
135299a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
135399a2dd95SBruce Richardson if (qp_id >= dev->data->nb_queue_pairs) {
135499a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
135599a2dd95SBruce Richardson return -ENODEV;
135699a2dd95SBruce Richardson }
135799a2dd95SBruce Richardson
135899a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_callback_lock);
135999a2dd95SBruce Richardson if (dev->enq_cbs == NULL) {
136099a2dd95SBruce Richardson CDEV_LOG_ERR("Callback not initialized");
136199a2dd95SBruce Richardson goto cb_err;
136299a2dd95SBruce Richardson }
136399a2dd95SBruce Richardson
136499a2dd95SBruce Richardson list = &dev->enq_cbs[qp_id];
136599a2dd95SBruce Richardson if (list == NULL) {
136699a2dd95SBruce Richardson CDEV_LOG_ERR("Callback list is NULL");
136799a2dd95SBruce Richardson goto cb_err;
136899a2dd95SBruce Richardson }
136999a2dd95SBruce Richardson
137099a2dd95SBruce Richardson if (list->qsbr == NULL) {
137199a2dd95SBruce Richardson CDEV_LOG_ERR("Rcu qsbr is NULL");
137299a2dd95SBruce Richardson goto cb_err;
137399a2dd95SBruce Richardson }
137499a2dd95SBruce Richardson
137599a2dd95SBruce Richardson prev_cb = &list->next;
137699a2dd95SBruce Richardson for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
137799a2dd95SBruce Richardson curr_cb = *prev_cb;
137899a2dd95SBruce Richardson if (curr_cb == cb) {
137999a2dd95SBruce Richardson /* Remove the user cb from the callback list. */
138099a2dd95SBruce Richardson __atomic_store_n(prev_cb, curr_cb->next,
138199a2dd95SBruce Richardson __ATOMIC_RELAXED);
138299a2dd95SBruce Richardson ret = 0;
138399a2dd95SBruce Richardson break;
138499a2dd95SBruce Richardson }
138599a2dd95SBruce Richardson }
138699a2dd95SBruce Richardson
138799a2dd95SBruce Richardson if (!ret) {
138899a2dd95SBruce Richardson /* Call sync with invalid thread id as this is part of
138999a2dd95SBruce Richardson * control plane API
139099a2dd95SBruce Richardson */
139199a2dd95SBruce Richardson rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
139299a2dd95SBruce Richardson rte_free(cb);
139399a2dd95SBruce Richardson }
139499a2dd95SBruce Richardson
139599a2dd95SBruce Richardson cb_err:
139699a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_callback_lock);
139799a2dd95SBruce Richardson return ret;
139899a2dd95SBruce Richardson }
139999a2dd95SBruce Richardson
140099a2dd95SBruce Richardson struct rte_cryptodev_cb *
rte_cryptodev_add_deq_callback(uint8_t dev_id,uint16_t qp_id,rte_cryptodev_callback_fn cb_fn,void * cb_arg)140199a2dd95SBruce Richardson rte_cryptodev_add_deq_callback(uint8_t dev_id,
140299a2dd95SBruce Richardson uint16_t qp_id,
140399a2dd95SBruce Richardson rte_cryptodev_callback_fn cb_fn,
140499a2dd95SBruce Richardson void *cb_arg)
140599a2dd95SBruce Richardson {
140699a2dd95SBruce Richardson struct rte_cryptodev *dev;
140799a2dd95SBruce Richardson struct rte_cryptodev_cb_rcu *list;
140899a2dd95SBruce Richardson struct rte_cryptodev_cb *cb, *tail;
140999a2dd95SBruce Richardson
141099a2dd95SBruce Richardson if (!cb_fn) {
141199a2dd95SBruce Richardson CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
141299a2dd95SBruce Richardson rte_errno = EINVAL;
141399a2dd95SBruce Richardson return NULL;
141499a2dd95SBruce Richardson }
141599a2dd95SBruce Richardson
1416e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
141799a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
141899a2dd95SBruce Richardson rte_errno = ENODEV;
141999a2dd95SBruce Richardson return NULL;
142099a2dd95SBruce Richardson }
142199a2dd95SBruce Richardson
142299a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
142399a2dd95SBruce Richardson if (qp_id >= dev->data->nb_queue_pairs) {
142499a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
142599a2dd95SBruce Richardson rte_errno = ENODEV;
142699a2dd95SBruce Richardson return NULL;
142799a2dd95SBruce Richardson }
142899a2dd95SBruce Richardson
142999a2dd95SBruce Richardson cb = rte_zmalloc(NULL, sizeof(*cb), 0);
143099a2dd95SBruce Richardson if (cb == NULL) {
143199a2dd95SBruce Richardson CDEV_LOG_ERR("Failed to allocate memory for callback on "
143299a2dd95SBruce Richardson "dev=%d, queue_pair_id=%d", dev_id, qp_id);
143399a2dd95SBruce Richardson rte_errno = ENOMEM;
143499a2dd95SBruce Richardson return NULL;
143599a2dd95SBruce Richardson }
143699a2dd95SBruce Richardson
143799a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_callback_lock);
143899a2dd95SBruce Richardson
143999a2dd95SBruce Richardson cb->fn = cb_fn;
144099a2dd95SBruce Richardson cb->arg = cb_arg;
144199a2dd95SBruce Richardson
144299a2dd95SBruce Richardson /* Add the callbacks in fifo order. */
144399a2dd95SBruce Richardson list = &dev->deq_cbs[qp_id];
144499a2dd95SBruce Richardson tail = list->next;
144599a2dd95SBruce Richardson
144699a2dd95SBruce Richardson if (tail) {
144799a2dd95SBruce Richardson while (tail->next)
144899a2dd95SBruce Richardson tail = tail->next;
144999a2dd95SBruce Richardson /* Stores to cb->fn and cb->param should complete before
145099a2dd95SBruce Richardson * cb is visible to data plane.
145199a2dd95SBruce Richardson */
145299a2dd95SBruce Richardson __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
145399a2dd95SBruce Richardson } else {
145499a2dd95SBruce Richardson /* Stores to cb->fn and cb->param should complete before
145599a2dd95SBruce Richardson * cb is visible to data plane.
145699a2dd95SBruce Richardson */
145799a2dd95SBruce Richardson __atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
145899a2dd95SBruce Richardson }
145999a2dd95SBruce Richardson
146099a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_callback_lock);
146199a2dd95SBruce Richardson
146299a2dd95SBruce Richardson return cb;
146399a2dd95SBruce Richardson }
146499a2dd95SBruce Richardson
146599a2dd95SBruce Richardson int
rte_cryptodev_remove_deq_callback(uint8_t dev_id,uint16_t qp_id,struct rte_cryptodev_cb * cb)146699a2dd95SBruce Richardson rte_cryptodev_remove_deq_callback(uint8_t dev_id,
146799a2dd95SBruce Richardson uint16_t qp_id,
146899a2dd95SBruce Richardson struct rte_cryptodev_cb *cb)
146999a2dd95SBruce Richardson {
147099a2dd95SBruce Richardson struct rte_cryptodev *dev;
147199a2dd95SBruce Richardson struct rte_cryptodev_cb **prev_cb, *curr_cb;
147299a2dd95SBruce Richardson struct rte_cryptodev_cb_rcu *list;
147399a2dd95SBruce Richardson int ret;
147499a2dd95SBruce Richardson
147599a2dd95SBruce Richardson ret = -EINVAL;
147699a2dd95SBruce Richardson
147799a2dd95SBruce Richardson if (!cb) {
147899a2dd95SBruce Richardson CDEV_LOG_ERR("Callback is NULL");
147999a2dd95SBruce Richardson return -EINVAL;
148099a2dd95SBruce Richardson }
148199a2dd95SBruce Richardson
1482e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
148399a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
148499a2dd95SBruce Richardson return -ENODEV;
148599a2dd95SBruce Richardson }
148699a2dd95SBruce Richardson
148799a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
148899a2dd95SBruce Richardson if (qp_id >= dev->data->nb_queue_pairs) {
148999a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
149099a2dd95SBruce Richardson return -ENODEV;
149199a2dd95SBruce Richardson }
149299a2dd95SBruce Richardson
149399a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_callback_lock);
149499a2dd95SBruce Richardson if (dev->enq_cbs == NULL) {
149599a2dd95SBruce Richardson CDEV_LOG_ERR("Callback not initialized");
149699a2dd95SBruce Richardson goto cb_err;
149799a2dd95SBruce Richardson }
149899a2dd95SBruce Richardson
149999a2dd95SBruce Richardson list = &dev->deq_cbs[qp_id];
150099a2dd95SBruce Richardson if (list == NULL) {
150199a2dd95SBruce Richardson CDEV_LOG_ERR("Callback list is NULL");
150299a2dd95SBruce Richardson goto cb_err;
150399a2dd95SBruce Richardson }
150499a2dd95SBruce Richardson
150599a2dd95SBruce Richardson if (list->qsbr == NULL) {
150699a2dd95SBruce Richardson CDEV_LOG_ERR("Rcu qsbr is NULL");
150799a2dd95SBruce Richardson goto cb_err;
150899a2dd95SBruce Richardson }
150999a2dd95SBruce Richardson
151099a2dd95SBruce Richardson prev_cb = &list->next;
151199a2dd95SBruce Richardson for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
151299a2dd95SBruce Richardson curr_cb = *prev_cb;
151399a2dd95SBruce Richardson if (curr_cb == cb) {
151499a2dd95SBruce Richardson /* Remove the user cb from the callback list. */
151599a2dd95SBruce Richardson __atomic_store_n(prev_cb, curr_cb->next,
151699a2dd95SBruce Richardson __ATOMIC_RELAXED);
151799a2dd95SBruce Richardson ret = 0;
151899a2dd95SBruce Richardson break;
151999a2dd95SBruce Richardson }
152099a2dd95SBruce Richardson }
152199a2dd95SBruce Richardson
152299a2dd95SBruce Richardson if (!ret) {
152399a2dd95SBruce Richardson /* Call sync with invalid thread id as this is part of
152499a2dd95SBruce Richardson * control plane API
152599a2dd95SBruce Richardson */
152699a2dd95SBruce Richardson rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
152799a2dd95SBruce Richardson rte_free(cb);
152899a2dd95SBruce Richardson }
152999a2dd95SBruce Richardson
153099a2dd95SBruce Richardson cb_err:
153199a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_callback_lock);
153299a2dd95SBruce Richardson return ret;
153399a2dd95SBruce Richardson }
153499a2dd95SBruce Richardson
153599a2dd95SBruce Richardson int
rte_cryptodev_stats_get(uint8_t dev_id,struct rte_cryptodev_stats * stats)153699a2dd95SBruce Richardson rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
153799a2dd95SBruce Richardson {
153899a2dd95SBruce Richardson struct rte_cryptodev *dev;
153999a2dd95SBruce Richardson
1540e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
154199a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
154299a2dd95SBruce Richardson return -ENODEV;
154399a2dd95SBruce Richardson }
154499a2dd95SBruce Richardson
154599a2dd95SBruce Richardson if (stats == NULL) {
154699a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid stats ptr");
154799a2dd95SBruce Richardson return -EINVAL;
154899a2dd95SBruce Richardson }
154999a2dd95SBruce Richardson
155099a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
155199a2dd95SBruce Richardson memset(stats, 0, sizeof(*stats));
155299a2dd95SBruce Richardson
155399a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
155499a2dd95SBruce Richardson (*dev->dev_ops->stats_get)(dev, stats);
155599a2dd95SBruce Richardson return 0;
155699a2dd95SBruce Richardson }
155799a2dd95SBruce Richardson
155899a2dd95SBruce Richardson void
rte_cryptodev_stats_reset(uint8_t dev_id)155999a2dd95SBruce Richardson rte_cryptodev_stats_reset(uint8_t dev_id)
156099a2dd95SBruce Richardson {
156199a2dd95SBruce Richardson struct rte_cryptodev *dev;
156299a2dd95SBruce Richardson
1563e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
156499a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
156599a2dd95SBruce Richardson return;
156699a2dd95SBruce Richardson }
156799a2dd95SBruce Richardson
156899a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
156999a2dd95SBruce Richardson
157099a2dd95SBruce Richardson RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
157199a2dd95SBruce Richardson (*dev->dev_ops->stats_reset)(dev);
157299a2dd95SBruce Richardson }
157399a2dd95SBruce Richardson
157499a2dd95SBruce Richardson void
rte_cryptodev_info_get(uint8_t dev_id,struct rte_cryptodev_info * dev_info)157599a2dd95SBruce Richardson rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
157699a2dd95SBruce Richardson {
157799a2dd95SBruce Richardson struct rte_cryptodev *dev;
157899a2dd95SBruce Richardson
1579e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
158099a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
158199a2dd95SBruce Richardson return;
158299a2dd95SBruce Richardson }
158399a2dd95SBruce Richardson
158499a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
158599a2dd95SBruce Richardson
158699a2dd95SBruce Richardson memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
158799a2dd95SBruce Richardson
158899a2dd95SBruce Richardson RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
158999a2dd95SBruce Richardson (*dev->dev_ops->dev_infos_get)(dev, dev_info);
159099a2dd95SBruce Richardson
159199a2dd95SBruce Richardson dev_info->driver_name = dev->device->driver->name;
159299a2dd95SBruce Richardson dev_info->device = dev->device;
159399a2dd95SBruce Richardson }
159499a2dd95SBruce Richardson
159599a2dd95SBruce Richardson int
rte_cryptodev_callback_register(uint8_t dev_id,enum rte_cryptodev_event_type event,rte_cryptodev_cb_fn cb_fn,void * cb_arg)159699a2dd95SBruce Richardson rte_cryptodev_callback_register(uint8_t dev_id,
159799a2dd95SBruce Richardson enum rte_cryptodev_event_type event,
159899a2dd95SBruce Richardson rte_cryptodev_cb_fn cb_fn, void *cb_arg)
159999a2dd95SBruce Richardson {
160099a2dd95SBruce Richardson struct rte_cryptodev *dev;
160199a2dd95SBruce Richardson struct rte_cryptodev_callback *user_cb;
160299a2dd95SBruce Richardson
160399a2dd95SBruce Richardson if (!cb_fn)
160499a2dd95SBruce Richardson return -EINVAL;
160599a2dd95SBruce Richardson
1606e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
160799a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
160899a2dd95SBruce Richardson return -EINVAL;
160999a2dd95SBruce Richardson }
161099a2dd95SBruce Richardson
161199a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
161299a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_cb_lock);
161399a2dd95SBruce Richardson
161499a2dd95SBruce Richardson TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
161599a2dd95SBruce Richardson if (user_cb->cb_fn == cb_fn &&
161699a2dd95SBruce Richardson user_cb->cb_arg == cb_arg &&
161799a2dd95SBruce Richardson user_cb->event == event) {
161899a2dd95SBruce Richardson break;
161999a2dd95SBruce Richardson }
162099a2dd95SBruce Richardson }
162199a2dd95SBruce Richardson
162299a2dd95SBruce Richardson /* create a new callback. */
162399a2dd95SBruce Richardson if (user_cb == NULL) {
162499a2dd95SBruce Richardson user_cb = rte_zmalloc("INTR_USER_CALLBACK",
162599a2dd95SBruce Richardson sizeof(struct rte_cryptodev_callback), 0);
162699a2dd95SBruce Richardson if (user_cb != NULL) {
162799a2dd95SBruce Richardson user_cb->cb_fn = cb_fn;
162899a2dd95SBruce Richardson user_cb->cb_arg = cb_arg;
162999a2dd95SBruce Richardson user_cb->event = event;
163099a2dd95SBruce Richardson TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
163199a2dd95SBruce Richardson }
163299a2dd95SBruce Richardson }
163399a2dd95SBruce Richardson
163499a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_cb_lock);
163599a2dd95SBruce Richardson return (user_cb == NULL) ? -ENOMEM : 0;
163699a2dd95SBruce Richardson }
163799a2dd95SBruce Richardson
163899a2dd95SBruce Richardson int
rte_cryptodev_callback_unregister(uint8_t dev_id,enum rte_cryptodev_event_type event,rte_cryptodev_cb_fn cb_fn,void * cb_arg)163999a2dd95SBruce Richardson rte_cryptodev_callback_unregister(uint8_t dev_id,
164099a2dd95SBruce Richardson enum rte_cryptodev_event_type event,
164199a2dd95SBruce Richardson rte_cryptodev_cb_fn cb_fn, void *cb_arg)
164299a2dd95SBruce Richardson {
164399a2dd95SBruce Richardson int ret;
164499a2dd95SBruce Richardson struct rte_cryptodev *dev;
164599a2dd95SBruce Richardson struct rte_cryptodev_callback *cb, *next;
164699a2dd95SBruce Richardson
164799a2dd95SBruce Richardson if (!cb_fn)
164899a2dd95SBruce Richardson return -EINVAL;
164999a2dd95SBruce Richardson
1650e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
165199a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
165299a2dd95SBruce Richardson return -EINVAL;
165399a2dd95SBruce Richardson }
165499a2dd95SBruce Richardson
165599a2dd95SBruce Richardson dev = &rte_crypto_devices[dev_id];
165699a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_cb_lock);
165799a2dd95SBruce Richardson
165899a2dd95SBruce Richardson ret = 0;
165999a2dd95SBruce Richardson for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
166099a2dd95SBruce Richardson
166199a2dd95SBruce Richardson next = TAILQ_NEXT(cb, next);
166299a2dd95SBruce Richardson
166399a2dd95SBruce Richardson if (cb->cb_fn != cb_fn || cb->event != event ||
166499a2dd95SBruce Richardson (cb->cb_arg != (void *)-1 &&
166599a2dd95SBruce Richardson cb->cb_arg != cb_arg))
166699a2dd95SBruce Richardson continue;
166799a2dd95SBruce Richardson
166899a2dd95SBruce Richardson /*
166999a2dd95SBruce Richardson * if this callback is not executing right now,
167099a2dd95SBruce Richardson * then remove it.
167199a2dd95SBruce Richardson */
167299a2dd95SBruce Richardson if (cb->active == 0) {
167399a2dd95SBruce Richardson TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
167499a2dd95SBruce Richardson rte_free(cb);
167599a2dd95SBruce Richardson } else {
167699a2dd95SBruce Richardson ret = -EAGAIN;
167799a2dd95SBruce Richardson }
167899a2dd95SBruce Richardson }
167999a2dd95SBruce Richardson
168099a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_cb_lock);
168199a2dd95SBruce Richardson return ret;
168299a2dd95SBruce Richardson }
168399a2dd95SBruce Richardson
168499a2dd95SBruce Richardson void
rte_cryptodev_pmd_callback_process(struct rte_cryptodev * dev,enum rte_cryptodev_event_type event)168599a2dd95SBruce Richardson rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
168699a2dd95SBruce Richardson enum rte_cryptodev_event_type event)
168799a2dd95SBruce Richardson {
168899a2dd95SBruce Richardson struct rte_cryptodev_callback *cb_lst;
168999a2dd95SBruce Richardson struct rte_cryptodev_callback dev_cb;
169099a2dd95SBruce Richardson
169199a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_cb_lock);
169299a2dd95SBruce Richardson TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
169399a2dd95SBruce Richardson if (cb_lst->cb_fn == NULL || cb_lst->event != event)
169499a2dd95SBruce Richardson continue;
169599a2dd95SBruce Richardson dev_cb = *cb_lst;
169699a2dd95SBruce Richardson cb_lst->active = 1;
169799a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_cb_lock);
169899a2dd95SBruce Richardson dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
169999a2dd95SBruce Richardson dev_cb.cb_arg);
170099a2dd95SBruce Richardson rte_spinlock_lock(&rte_cryptodev_cb_lock);
170199a2dd95SBruce Richardson cb_lst->active = 0;
170299a2dd95SBruce Richardson }
170399a2dd95SBruce Richardson rte_spinlock_unlock(&rte_cryptodev_cb_lock);
170499a2dd95SBruce Richardson }
170599a2dd95SBruce Richardson
170699a2dd95SBruce Richardson int
rte_cryptodev_sym_session_init(uint8_t dev_id,struct rte_cryptodev_sym_session * sess,struct rte_crypto_sym_xform * xforms,struct rte_mempool * mp)170799a2dd95SBruce Richardson rte_cryptodev_sym_session_init(uint8_t dev_id,
170899a2dd95SBruce Richardson struct rte_cryptodev_sym_session *sess,
170999a2dd95SBruce Richardson struct rte_crypto_sym_xform *xforms,
171099a2dd95SBruce Richardson struct rte_mempool *mp)
171199a2dd95SBruce Richardson {
171299a2dd95SBruce Richardson struct rte_cryptodev *dev;
171399a2dd95SBruce Richardson uint32_t sess_priv_sz = rte_cryptodev_sym_get_private_session_size(
171499a2dd95SBruce Richardson dev_id);
171599a2dd95SBruce Richardson uint8_t index;
171699a2dd95SBruce Richardson int ret;
171799a2dd95SBruce Richardson
1718e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
171999a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
172099a2dd95SBruce Richardson return -EINVAL;
172199a2dd95SBruce Richardson }
172299a2dd95SBruce Richardson
172399a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
172499a2dd95SBruce Richardson
172599a2dd95SBruce Richardson if (sess == NULL || xforms == NULL || dev == NULL || mp == NULL)
172699a2dd95SBruce Richardson return -EINVAL;
172799a2dd95SBruce Richardson
172899a2dd95SBruce Richardson if (mp->elt_size < sess_priv_sz)
172999a2dd95SBruce Richardson return -EINVAL;
173099a2dd95SBruce Richardson
173199a2dd95SBruce Richardson index = dev->driver_id;
173299a2dd95SBruce Richardson if (index >= sess->nb_drivers)
173399a2dd95SBruce Richardson return -EINVAL;
173499a2dd95SBruce Richardson
173599a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_configure, -ENOTSUP);
173699a2dd95SBruce Richardson
173799a2dd95SBruce Richardson if (sess->sess_data[index].refcnt == 0) {
173899a2dd95SBruce Richardson ret = dev->dev_ops->sym_session_configure(dev, xforms,
173999a2dd95SBruce Richardson sess, mp);
174099a2dd95SBruce Richardson if (ret < 0) {
174199a2dd95SBruce Richardson CDEV_LOG_ERR(
174299a2dd95SBruce Richardson "dev_id %d failed to configure session details",
174399a2dd95SBruce Richardson dev_id);
174499a2dd95SBruce Richardson return ret;
174599a2dd95SBruce Richardson }
174699a2dd95SBruce Richardson }
174799a2dd95SBruce Richardson
174899a2dd95SBruce Richardson rte_cryptodev_trace_sym_session_init(dev_id, sess, xforms, mp);
174999a2dd95SBruce Richardson sess->sess_data[index].refcnt++;
175099a2dd95SBruce Richardson return 0;
175199a2dd95SBruce Richardson }
175299a2dd95SBruce Richardson
175399a2dd95SBruce Richardson struct rte_mempool *
rte_cryptodev_sym_session_pool_create(const char * name,uint32_t nb_elts,uint32_t elt_size,uint32_t cache_size,uint16_t user_data_size,int socket_id)175499a2dd95SBruce Richardson rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
175599a2dd95SBruce Richardson uint32_t elt_size, uint32_t cache_size, uint16_t user_data_size,
175699a2dd95SBruce Richardson int socket_id)
175799a2dd95SBruce Richardson {
175899a2dd95SBruce Richardson struct rte_mempool *mp;
175999a2dd95SBruce Richardson struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
176099a2dd95SBruce Richardson uint32_t obj_sz;
176199a2dd95SBruce Richardson
176299a2dd95SBruce Richardson obj_sz = rte_cryptodev_sym_get_header_session_size() + user_data_size;
176399a2dd95SBruce Richardson if (obj_sz > elt_size)
176499a2dd95SBruce Richardson CDEV_LOG_INFO("elt_size %u is expanded to %u\n", elt_size,
176599a2dd95SBruce Richardson obj_sz);
176699a2dd95SBruce Richardson else
176799a2dd95SBruce Richardson obj_sz = elt_size;
176899a2dd95SBruce Richardson
176999a2dd95SBruce Richardson mp = rte_mempool_create(name, nb_elts, obj_sz, cache_size,
177099a2dd95SBruce Richardson (uint32_t)(sizeof(*pool_priv)),
177199a2dd95SBruce Richardson NULL, NULL, NULL, NULL,
177299a2dd95SBruce Richardson socket_id, 0);
177399a2dd95SBruce Richardson if (mp == NULL) {
177499a2dd95SBruce Richardson CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d\n",
177599a2dd95SBruce Richardson __func__, name, rte_errno);
177699a2dd95SBruce Richardson return NULL;
177799a2dd95SBruce Richardson }
177899a2dd95SBruce Richardson
177999a2dd95SBruce Richardson pool_priv = rte_mempool_get_priv(mp);
178099a2dd95SBruce Richardson if (!pool_priv) {
178199a2dd95SBruce Richardson CDEV_LOG_ERR("%s(name=%s) failed to get private data\n",
178299a2dd95SBruce Richardson __func__, name);
178399a2dd95SBruce Richardson rte_mempool_free(mp);
178499a2dd95SBruce Richardson return NULL;
178599a2dd95SBruce Richardson }
178699a2dd95SBruce Richardson
178799a2dd95SBruce Richardson pool_priv->nb_drivers = nb_drivers;
178899a2dd95SBruce Richardson pool_priv->user_data_sz = user_data_size;
178999a2dd95SBruce Richardson
179099a2dd95SBruce Richardson rte_cryptodev_trace_sym_session_pool_create(name, nb_elts,
179199a2dd95SBruce Richardson elt_size, cache_size, user_data_size, mp);
179299a2dd95SBruce Richardson return mp;
179399a2dd95SBruce Richardson }
179499a2dd95SBruce Richardson
17951f1e4b7cSCiara Power struct rte_mempool *
rte_cryptodev_asym_session_pool_create(const char * name,uint32_t nb_elts,uint32_t cache_size,uint16_t user_data_size,int socket_id)17961f1e4b7cSCiara Power rte_cryptodev_asym_session_pool_create(const char *name, uint32_t nb_elts,
179792d55afeSCiara Power uint32_t cache_size, uint16_t user_data_size, int socket_id)
17981f1e4b7cSCiara Power {
17991f1e4b7cSCiara Power struct rte_mempool *mp;
18001f1e4b7cSCiara Power struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
18011f1e4b7cSCiara Power uint32_t obj_sz, obj_sz_aligned;
1802*080c84cdSCiara Power uint8_t dev_id;
1803*080c84cdSCiara Power unsigned int priv_sz, max_priv_sz = 0;
18041f1e4b7cSCiara Power
18051f1e4b7cSCiara Power for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
18061f1e4b7cSCiara Power if (rte_cryptodev_is_valid_dev(dev_id)) {
18071f1e4b7cSCiara Power priv_sz = rte_cryptodev_asym_get_private_session_size(dev_id);
18081f1e4b7cSCiara Power if (priv_sz > max_priv_sz)
18091f1e4b7cSCiara Power max_priv_sz = priv_sz;
18101f1e4b7cSCiara Power }
18111f1e4b7cSCiara Power if (max_priv_sz == 0) {
18121f1e4b7cSCiara Power CDEV_LOG_INFO("Could not set max private session size\n");
18131f1e4b7cSCiara Power return NULL;
18141f1e4b7cSCiara Power }
18151f1e4b7cSCiara Power
181692d55afeSCiara Power obj_sz = rte_cryptodev_asym_get_header_session_size() + max_priv_sz +
181792d55afeSCiara Power user_data_size;
18181f1e4b7cSCiara Power obj_sz_aligned = RTE_ALIGN_CEIL(obj_sz, RTE_CACHE_LINE_SIZE);
18191f1e4b7cSCiara Power
18201f1e4b7cSCiara Power mp = rte_mempool_create(name, nb_elts, obj_sz_aligned, cache_size,
18211f1e4b7cSCiara Power (uint32_t)(sizeof(*pool_priv)),
18221f1e4b7cSCiara Power NULL, NULL, NULL, NULL,
18231f1e4b7cSCiara Power socket_id, 0);
18241f1e4b7cSCiara Power if (mp == NULL) {
18251f1e4b7cSCiara Power CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d\n",
18261f1e4b7cSCiara Power __func__, name, rte_errno);
18271f1e4b7cSCiara Power return NULL;
18281f1e4b7cSCiara Power }
18291f1e4b7cSCiara Power
18301f1e4b7cSCiara Power pool_priv = rte_mempool_get_priv(mp);
18311f1e4b7cSCiara Power if (!pool_priv) {
18321f1e4b7cSCiara Power CDEV_LOG_ERR("%s(name=%s) failed to get private data\n",
18331f1e4b7cSCiara Power __func__, name);
18341f1e4b7cSCiara Power rte_mempool_free(mp);
18351f1e4b7cSCiara Power return NULL;
18361f1e4b7cSCiara Power }
18371f1e4b7cSCiara Power pool_priv->max_priv_session_sz = max_priv_sz;
183892d55afeSCiara Power pool_priv->user_data_sz = user_data_size;
18391f1e4b7cSCiara Power
18401f1e4b7cSCiara Power rte_cryptodev_trace_asym_session_pool_create(name, nb_elts,
184192d55afeSCiara Power user_data_size, cache_size, mp);
18421f1e4b7cSCiara Power return mp;
18431f1e4b7cSCiara Power }
18441f1e4b7cSCiara Power
184599a2dd95SBruce Richardson static unsigned int
rte_cryptodev_sym_session_data_size(struct rte_cryptodev_sym_session * sess)184699a2dd95SBruce Richardson rte_cryptodev_sym_session_data_size(struct rte_cryptodev_sym_session *sess)
184799a2dd95SBruce Richardson {
184899a2dd95SBruce Richardson return (sizeof(sess->sess_data[0]) * sess->nb_drivers) +
184999a2dd95SBruce Richardson sess->user_data_sz;
185099a2dd95SBruce Richardson }
185199a2dd95SBruce Richardson
185299a2dd95SBruce Richardson static uint8_t
rte_cryptodev_sym_is_valid_session_pool(struct rte_mempool * mp)185399a2dd95SBruce Richardson rte_cryptodev_sym_is_valid_session_pool(struct rte_mempool *mp)
185499a2dd95SBruce Richardson {
185599a2dd95SBruce Richardson struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
185699a2dd95SBruce Richardson
185799a2dd95SBruce Richardson if (!mp)
185899a2dd95SBruce Richardson return 0;
185999a2dd95SBruce Richardson
186099a2dd95SBruce Richardson pool_priv = rte_mempool_get_priv(mp);
186199a2dd95SBruce Richardson
186299a2dd95SBruce Richardson if (!pool_priv || mp->private_data_size < sizeof(*pool_priv) ||
186399a2dd95SBruce Richardson pool_priv->nb_drivers != nb_drivers ||
186499a2dd95SBruce Richardson mp->elt_size <
186599a2dd95SBruce Richardson rte_cryptodev_sym_get_header_session_size()
186699a2dd95SBruce Richardson + pool_priv->user_data_sz)
186799a2dd95SBruce Richardson return 0;
186899a2dd95SBruce Richardson
186999a2dd95SBruce Richardson return 1;
187099a2dd95SBruce Richardson }
187199a2dd95SBruce Richardson
187299a2dd95SBruce Richardson struct rte_cryptodev_sym_session *
rte_cryptodev_sym_session_create(struct rte_mempool * mp)187399a2dd95SBruce Richardson rte_cryptodev_sym_session_create(struct rte_mempool *mp)
187499a2dd95SBruce Richardson {
187599a2dd95SBruce Richardson struct rte_cryptodev_sym_session *sess;
187699a2dd95SBruce Richardson struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
187799a2dd95SBruce Richardson
187899a2dd95SBruce Richardson if (!rte_cryptodev_sym_is_valid_session_pool(mp)) {
187999a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid mempool\n");
188099a2dd95SBruce Richardson return NULL;
188199a2dd95SBruce Richardson }
188299a2dd95SBruce Richardson
188399a2dd95SBruce Richardson pool_priv = rte_mempool_get_priv(mp);
188499a2dd95SBruce Richardson
188599a2dd95SBruce Richardson /* Allocate a session structure from the session pool */
188699a2dd95SBruce Richardson if (rte_mempool_get(mp, (void **)&sess)) {
188799a2dd95SBruce Richardson CDEV_LOG_ERR("couldn't get object from session mempool");
188899a2dd95SBruce Richardson return NULL;
188999a2dd95SBruce Richardson }
189099a2dd95SBruce Richardson
189199a2dd95SBruce Richardson sess->nb_drivers = pool_priv->nb_drivers;
189299a2dd95SBruce Richardson sess->user_data_sz = pool_priv->user_data_sz;
189399a2dd95SBruce Richardson sess->opaque_data = 0;
189499a2dd95SBruce Richardson
189599a2dd95SBruce Richardson /* Clear device session pointer.
189699a2dd95SBruce Richardson * Include the flag indicating presence of user data
189799a2dd95SBruce Richardson */
189899a2dd95SBruce Richardson memset(sess->sess_data, 0,
189999a2dd95SBruce Richardson rte_cryptodev_sym_session_data_size(sess));
190099a2dd95SBruce Richardson
190199a2dd95SBruce Richardson rte_cryptodev_trace_sym_session_create(mp, sess);
190299a2dd95SBruce Richardson return sess;
190399a2dd95SBruce Richardson }
190499a2dd95SBruce Richardson
1905757f40e2SCiara Power int
rte_cryptodev_asym_session_create(uint8_t dev_id,struct rte_crypto_asym_xform * xforms,struct rte_mempool * mp,void ** session)19061f1e4b7cSCiara Power rte_cryptodev_asym_session_create(uint8_t dev_id,
1907757f40e2SCiara Power struct rte_crypto_asym_xform *xforms, struct rte_mempool *mp,
1908757f40e2SCiara Power void **session)
190999a2dd95SBruce Richardson {
191099a2dd95SBruce Richardson struct rte_cryptodev_asym_session *sess;
19111f1e4b7cSCiara Power uint32_t session_priv_data_sz;
19121f1e4b7cSCiara Power struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
19131f1e4b7cSCiara Power unsigned int session_header_size =
191499a2dd95SBruce Richardson rte_cryptodev_asym_get_header_session_size();
19151f1e4b7cSCiara Power struct rte_cryptodev *dev;
19161f1e4b7cSCiara Power int ret;
19171f1e4b7cSCiara Power
19181f1e4b7cSCiara Power if (!rte_cryptodev_is_valid_dev(dev_id)) {
19191f1e4b7cSCiara Power CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1920757f40e2SCiara Power return -EINVAL;
19211f1e4b7cSCiara Power }
19221f1e4b7cSCiara Power
19231f1e4b7cSCiara Power dev = rte_cryptodev_pmd_get_dev(dev_id);
19241f1e4b7cSCiara Power
19251f1e4b7cSCiara Power if (dev == NULL)
1926757f40e2SCiara Power return -EINVAL;
192799a2dd95SBruce Richardson
192899a2dd95SBruce Richardson if (!mp) {
192999a2dd95SBruce Richardson CDEV_LOG_ERR("invalid mempool\n");
1930757f40e2SCiara Power return -EINVAL;
193199a2dd95SBruce Richardson }
193299a2dd95SBruce Richardson
19331f1e4b7cSCiara Power session_priv_data_sz = rte_cryptodev_asym_get_private_session_size(
19341f1e4b7cSCiara Power dev_id);
19351f1e4b7cSCiara Power pool_priv = rte_mempool_get_priv(mp);
19361f1e4b7cSCiara Power
19371f1e4b7cSCiara Power if (pool_priv->max_priv_session_sz < session_priv_data_sz) {
19381f1e4b7cSCiara Power CDEV_LOG_DEBUG(
19391f1e4b7cSCiara Power "The private session data size used when creating the mempool is smaller than this device's private session data.");
1940757f40e2SCiara Power return -EINVAL;
19411f1e4b7cSCiara Power }
19421f1e4b7cSCiara Power
194399a2dd95SBruce Richardson /* Verify if provided mempool can hold elements big enough. */
19441f1e4b7cSCiara Power if (mp->elt_size < session_header_size + session_priv_data_sz) {
194599a2dd95SBruce Richardson CDEV_LOG_ERR(
194699a2dd95SBruce Richardson "mempool elements too small to hold session objects");
1947757f40e2SCiara Power return -EINVAL;
194899a2dd95SBruce Richardson }
194999a2dd95SBruce Richardson
195099a2dd95SBruce Richardson /* Allocate a session structure from the session pool */
1951757f40e2SCiara Power if (rte_mempool_get(mp, session)) {
195299a2dd95SBruce Richardson CDEV_LOG_ERR("couldn't get object from session mempool");
1953757f40e2SCiara Power return -ENOMEM;
195499a2dd95SBruce Richardson }
195599a2dd95SBruce Richardson
1956757f40e2SCiara Power sess = *session;
19571f1e4b7cSCiara Power sess->driver_id = dev->driver_id;
195892d55afeSCiara Power sess->user_data_sz = pool_priv->user_data_sz;
19591f1e4b7cSCiara Power sess->max_priv_data_sz = pool_priv->max_priv_session_sz;
196099a2dd95SBruce Richardson
19611f1e4b7cSCiara Power /* Clear device session pointer.*/
196292d55afeSCiara Power memset(sess->sess_private_data, 0, session_priv_data_sz + sess->user_data_sz);
19631f1e4b7cSCiara Power
1964757f40e2SCiara Power RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_configure, -ENOTSUP);
19651f1e4b7cSCiara Power
19661f1e4b7cSCiara Power if (sess->sess_private_data[0] == 0) {
19671f1e4b7cSCiara Power ret = dev->dev_ops->asym_session_configure(dev, xforms, sess);
19681f1e4b7cSCiara Power if (ret < 0) {
19691f1e4b7cSCiara Power CDEV_LOG_ERR(
19701f1e4b7cSCiara Power "dev_id %d failed to configure session details",
19711f1e4b7cSCiara Power dev_id);
1972757f40e2SCiara Power return ret;
19731f1e4b7cSCiara Power }
19741f1e4b7cSCiara Power }
19751f1e4b7cSCiara Power
1976757f40e2SCiara Power rte_cryptodev_trace_asym_session_create(dev_id, xforms, mp, sess);
1977757f40e2SCiara Power return 0;
197899a2dd95SBruce Richardson }
197999a2dd95SBruce Richardson
198099a2dd95SBruce Richardson int
rte_cryptodev_sym_session_clear(uint8_t dev_id,struct rte_cryptodev_sym_session * sess)198199a2dd95SBruce Richardson rte_cryptodev_sym_session_clear(uint8_t dev_id,
198299a2dd95SBruce Richardson struct rte_cryptodev_sym_session *sess)
198399a2dd95SBruce Richardson {
198499a2dd95SBruce Richardson struct rte_cryptodev *dev;
198599a2dd95SBruce Richardson uint8_t driver_id;
198699a2dd95SBruce Richardson
1987e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
198899a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
198999a2dd95SBruce Richardson return -EINVAL;
199099a2dd95SBruce Richardson }
199199a2dd95SBruce Richardson
199299a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
199399a2dd95SBruce Richardson
199499a2dd95SBruce Richardson if (dev == NULL || sess == NULL)
199599a2dd95SBruce Richardson return -EINVAL;
199699a2dd95SBruce Richardson
199799a2dd95SBruce Richardson driver_id = dev->driver_id;
199899a2dd95SBruce Richardson if (sess->sess_data[driver_id].refcnt == 0)
199999a2dd95SBruce Richardson return 0;
200099a2dd95SBruce Richardson if (--sess->sess_data[driver_id].refcnt != 0)
200199a2dd95SBruce Richardson return -EBUSY;
200299a2dd95SBruce Richardson
200399a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_clear, -ENOTSUP);
200499a2dd95SBruce Richardson
200599a2dd95SBruce Richardson dev->dev_ops->sym_session_clear(dev, sess);
200699a2dd95SBruce Richardson
200799a2dd95SBruce Richardson rte_cryptodev_trace_sym_session_clear(dev_id, sess);
200899a2dd95SBruce Richardson return 0;
200999a2dd95SBruce Richardson }
201099a2dd95SBruce Richardson
201199a2dd95SBruce Richardson int
rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session * sess)201299a2dd95SBruce Richardson rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session *sess)
201399a2dd95SBruce Richardson {
201499a2dd95SBruce Richardson uint8_t i;
201599a2dd95SBruce Richardson struct rte_mempool *sess_mp;
201699a2dd95SBruce Richardson
201799a2dd95SBruce Richardson if (sess == NULL)
201899a2dd95SBruce Richardson return -EINVAL;
201999a2dd95SBruce Richardson
202099a2dd95SBruce Richardson /* Check that all device private data has been freed */
202199a2dd95SBruce Richardson for (i = 0; i < sess->nb_drivers; i++) {
202299a2dd95SBruce Richardson if (sess->sess_data[i].refcnt != 0)
202399a2dd95SBruce Richardson return -EBUSY;
202499a2dd95SBruce Richardson }
202599a2dd95SBruce Richardson
202699a2dd95SBruce Richardson /* Return session to mempool */
202799a2dd95SBruce Richardson sess_mp = rte_mempool_from_obj(sess);
202899a2dd95SBruce Richardson rte_mempool_put(sess_mp, sess);
202999a2dd95SBruce Richardson
203099a2dd95SBruce Richardson rte_cryptodev_trace_sym_session_free(sess);
203199a2dd95SBruce Richardson return 0;
203299a2dd95SBruce Richardson }
203399a2dd95SBruce Richardson
203499a2dd95SBruce Richardson int
rte_cryptodev_asym_session_free(uint8_t dev_id,void * sess)2035a29bb248SCiara Power rte_cryptodev_asym_session_free(uint8_t dev_id, void *sess)
203699a2dd95SBruce Richardson {
203799a2dd95SBruce Richardson struct rte_mempool *sess_mp;
20381f1e4b7cSCiara Power struct rte_cryptodev *dev;
203999a2dd95SBruce Richardson
20401f1e4b7cSCiara Power if (!rte_cryptodev_is_valid_dev(dev_id)) {
20411f1e4b7cSCiara Power CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
20421f1e4b7cSCiara Power return -EINVAL;
20431f1e4b7cSCiara Power }
20441f1e4b7cSCiara Power
20451f1e4b7cSCiara Power dev = rte_cryptodev_pmd_get_dev(dev_id);
20461f1e4b7cSCiara Power
20471f1e4b7cSCiara Power if (dev == NULL || sess == NULL)
204899a2dd95SBruce Richardson return -EINVAL;
204999a2dd95SBruce Richardson
20501f1e4b7cSCiara Power RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_clear, -ENOTSUP);
20511f1e4b7cSCiara Power
20521f1e4b7cSCiara Power dev->dev_ops->asym_session_clear(dev, sess);
205399a2dd95SBruce Richardson
205499a2dd95SBruce Richardson /* Return session to mempool */
205599a2dd95SBruce Richardson sess_mp = rte_mempool_from_obj(sess);
205699a2dd95SBruce Richardson rte_mempool_put(sess_mp, sess);
205799a2dd95SBruce Richardson
20581f1e4b7cSCiara Power rte_cryptodev_trace_asym_session_free(dev_id, sess);
205999a2dd95SBruce Richardson return 0;
206099a2dd95SBruce Richardson }
206199a2dd95SBruce Richardson
206299a2dd95SBruce Richardson unsigned int
rte_cryptodev_sym_get_header_session_size(void)206399a2dd95SBruce Richardson rte_cryptodev_sym_get_header_session_size(void)
206499a2dd95SBruce Richardson {
206599a2dd95SBruce Richardson /*
206699a2dd95SBruce Richardson * Header contains pointers to the private data of all registered
206799a2dd95SBruce Richardson * drivers and all necessary information to ensure safely clear
206899a2dd95SBruce Richardson * or free al session.
206999a2dd95SBruce Richardson */
207099a2dd95SBruce Richardson struct rte_cryptodev_sym_session s = {0};
207199a2dd95SBruce Richardson
207299a2dd95SBruce Richardson s.nb_drivers = nb_drivers;
207399a2dd95SBruce Richardson
207499a2dd95SBruce Richardson return (unsigned int)(sizeof(s) +
207599a2dd95SBruce Richardson rte_cryptodev_sym_session_data_size(&s));
207699a2dd95SBruce Richardson }
207799a2dd95SBruce Richardson
207899a2dd95SBruce Richardson unsigned int
rte_cryptodev_sym_get_existing_header_session_size(struct rte_cryptodev_sym_session * sess)207999a2dd95SBruce Richardson rte_cryptodev_sym_get_existing_header_session_size(
208099a2dd95SBruce Richardson struct rte_cryptodev_sym_session *sess)
208199a2dd95SBruce Richardson {
208299a2dd95SBruce Richardson if (!sess)
208399a2dd95SBruce Richardson return 0;
208499a2dd95SBruce Richardson else
208599a2dd95SBruce Richardson return (unsigned int)(sizeof(*sess) +
208699a2dd95SBruce Richardson rte_cryptodev_sym_session_data_size(sess));
208799a2dd95SBruce Richardson }
208899a2dd95SBruce Richardson
208999a2dd95SBruce Richardson unsigned int
rte_cryptodev_asym_get_header_session_size(void)209099a2dd95SBruce Richardson rte_cryptodev_asym_get_header_session_size(void)
209199a2dd95SBruce Richardson {
20921f1e4b7cSCiara Power return sizeof(struct rte_cryptodev_asym_session);
209399a2dd95SBruce Richardson }
209499a2dd95SBruce Richardson
209599a2dd95SBruce Richardson unsigned int
rte_cryptodev_sym_get_private_session_size(uint8_t dev_id)209699a2dd95SBruce Richardson rte_cryptodev_sym_get_private_session_size(uint8_t dev_id)
209799a2dd95SBruce Richardson {
209899a2dd95SBruce Richardson struct rte_cryptodev *dev;
209999a2dd95SBruce Richardson unsigned int priv_sess_size;
210099a2dd95SBruce Richardson
2101e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id))
210299a2dd95SBruce Richardson return 0;
210399a2dd95SBruce Richardson
210499a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
210599a2dd95SBruce Richardson
210699a2dd95SBruce Richardson if (*dev->dev_ops->sym_session_get_size == NULL)
210799a2dd95SBruce Richardson return 0;
210899a2dd95SBruce Richardson
210999a2dd95SBruce Richardson priv_sess_size = (*dev->dev_ops->sym_session_get_size)(dev);
211099a2dd95SBruce Richardson
211199a2dd95SBruce Richardson return priv_sess_size;
211299a2dd95SBruce Richardson }
211399a2dd95SBruce Richardson
211499a2dd95SBruce Richardson unsigned int
rte_cryptodev_asym_get_private_session_size(uint8_t dev_id)211599a2dd95SBruce Richardson rte_cryptodev_asym_get_private_session_size(uint8_t dev_id)
211699a2dd95SBruce Richardson {
211799a2dd95SBruce Richardson struct rte_cryptodev *dev;
211899a2dd95SBruce Richardson unsigned int priv_sess_size;
211999a2dd95SBruce Richardson
2120e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id))
212199a2dd95SBruce Richardson return 0;
212299a2dd95SBruce Richardson
212399a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
212499a2dd95SBruce Richardson
212599a2dd95SBruce Richardson if (*dev->dev_ops->asym_session_get_size == NULL)
212699a2dd95SBruce Richardson return 0;
212799a2dd95SBruce Richardson
212899a2dd95SBruce Richardson priv_sess_size = (*dev->dev_ops->asym_session_get_size)(dev);
212999a2dd95SBruce Richardson
213099a2dd95SBruce Richardson return priv_sess_size;
213199a2dd95SBruce Richardson }
213299a2dd95SBruce Richardson
213399a2dd95SBruce Richardson int
rte_cryptodev_sym_session_set_user_data(struct rte_cryptodev_sym_session * sess,void * data,uint16_t size)213499a2dd95SBruce Richardson rte_cryptodev_sym_session_set_user_data(
213599a2dd95SBruce Richardson struct rte_cryptodev_sym_session *sess,
213699a2dd95SBruce Richardson void *data,
213799a2dd95SBruce Richardson uint16_t size)
213899a2dd95SBruce Richardson {
213999a2dd95SBruce Richardson if (sess == NULL)
214099a2dd95SBruce Richardson return -EINVAL;
214199a2dd95SBruce Richardson
214299a2dd95SBruce Richardson if (sess->user_data_sz < size)
214399a2dd95SBruce Richardson return -ENOMEM;
214499a2dd95SBruce Richardson
214599a2dd95SBruce Richardson rte_memcpy(sess->sess_data + sess->nb_drivers, data, size);
214699a2dd95SBruce Richardson return 0;
214799a2dd95SBruce Richardson }
214899a2dd95SBruce Richardson
214999a2dd95SBruce Richardson void *
rte_cryptodev_sym_session_get_user_data(struct rte_cryptodev_sym_session * sess)215099a2dd95SBruce Richardson rte_cryptodev_sym_session_get_user_data(
215199a2dd95SBruce Richardson struct rte_cryptodev_sym_session *sess)
215299a2dd95SBruce Richardson {
215399a2dd95SBruce Richardson if (sess == NULL || sess->user_data_sz == 0)
215499a2dd95SBruce Richardson return NULL;
215599a2dd95SBruce Richardson
215699a2dd95SBruce Richardson return (void *)(sess->sess_data + sess->nb_drivers);
215799a2dd95SBruce Richardson }
215899a2dd95SBruce Richardson
215992d55afeSCiara Power int
rte_cryptodev_asym_session_set_user_data(void * session,void * data,uint16_t size)216092d55afeSCiara Power rte_cryptodev_asym_session_set_user_data(void *session, void *data, uint16_t size)
216192d55afeSCiara Power {
216292d55afeSCiara Power struct rte_cryptodev_asym_session *sess = session;
216392d55afeSCiara Power if (sess == NULL)
216492d55afeSCiara Power return -EINVAL;
216592d55afeSCiara Power
216692d55afeSCiara Power if (sess->user_data_sz < size)
216792d55afeSCiara Power return -ENOMEM;
216892d55afeSCiara Power
216992d55afeSCiara Power rte_memcpy(sess->sess_private_data +
217092d55afeSCiara Power sess->max_priv_data_sz,
217192d55afeSCiara Power data, size);
217292d55afeSCiara Power return 0;
217392d55afeSCiara Power }
217492d55afeSCiara Power
217592d55afeSCiara Power void *
rte_cryptodev_asym_session_get_user_data(void * session)217692d55afeSCiara Power rte_cryptodev_asym_session_get_user_data(void *session)
217792d55afeSCiara Power {
217892d55afeSCiara Power struct rte_cryptodev_asym_session *sess = session;
217992d55afeSCiara Power if (sess == NULL || sess->user_data_sz == 0)
218092d55afeSCiara Power return NULL;
218192d55afeSCiara Power
218292d55afeSCiara Power return (void *)(sess->sess_private_data +
218392d55afeSCiara Power sess->max_priv_data_sz);
218492d55afeSCiara Power }
218592d55afeSCiara Power
218699a2dd95SBruce Richardson static inline void
sym_crypto_fill_status(struct rte_crypto_sym_vec * vec,int32_t errnum)218799a2dd95SBruce Richardson sym_crypto_fill_status(struct rte_crypto_sym_vec *vec, int32_t errnum)
218899a2dd95SBruce Richardson {
218999a2dd95SBruce Richardson uint32_t i;
219099a2dd95SBruce Richardson for (i = 0; i < vec->num; i++)
219199a2dd95SBruce Richardson vec->status[i] = errnum;
219299a2dd95SBruce Richardson }
219399a2dd95SBruce Richardson
219499a2dd95SBruce Richardson uint32_t
rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,struct rte_cryptodev_sym_session * sess,union rte_crypto_sym_ofs ofs,struct rte_crypto_sym_vec * vec)219599a2dd95SBruce Richardson rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
219699a2dd95SBruce Richardson struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs ofs,
219799a2dd95SBruce Richardson struct rte_crypto_sym_vec *vec)
219899a2dd95SBruce Richardson {
219999a2dd95SBruce Richardson struct rte_cryptodev *dev;
220099a2dd95SBruce Richardson
2201e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id)) {
220299a2dd95SBruce Richardson sym_crypto_fill_status(vec, EINVAL);
220399a2dd95SBruce Richardson return 0;
220499a2dd95SBruce Richardson }
220599a2dd95SBruce Richardson
220699a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
220799a2dd95SBruce Richardson
220899a2dd95SBruce Richardson if (*dev->dev_ops->sym_cpu_process == NULL ||
220999a2dd95SBruce Richardson !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO)) {
221099a2dd95SBruce Richardson sym_crypto_fill_status(vec, ENOTSUP);
221199a2dd95SBruce Richardson return 0;
221299a2dd95SBruce Richardson }
221399a2dd95SBruce Richardson
221499a2dd95SBruce Richardson return dev->dev_ops->sym_cpu_process(dev, sess, ofs, vec);
221599a2dd95SBruce Richardson }
221699a2dd95SBruce Richardson
221799a2dd95SBruce Richardson int
rte_cryptodev_get_raw_dp_ctx_size(uint8_t dev_id)221899a2dd95SBruce Richardson rte_cryptodev_get_raw_dp_ctx_size(uint8_t dev_id)
221999a2dd95SBruce Richardson {
222099a2dd95SBruce Richardson struct rte_cryptodev *dev;
222199a2dd95SBruce Richardson int32_t size = sizeof(struct rte_crypto_raw_dp_ctx);
222299a2dd95SBruce Richardson int32_t priv_size;
222399a2dd95SBruce Richardson
2224e74abd48SAkhil Goyal if (!rte_cryptodev_is_valid_dev(dev_id))
222599a2dd95SBruce Richardson return -EINVAL;
222699a2dd95SBruce Richardson
222799a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
222899a2dd95SBruce Richardson
222999a2dd95SBruce Richardson if (*dev->dev_ops->sym_get_raw_dp_ctx_size == NULL ||
223099a2dd95SBruce Richardson !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)) {
223199a2dd95SBruce Richardson return -ENOTSUP;
223299a2dd95SBruce Richardson }
223399a2dd95SBruce Richardson
223499a2dd95SBruce Richardson priv_size = (*dev->dev_ops->sym_get_raw_dp_ctx_size)(dev);
223599a2dd95SBruce Richardson if (priv_size < 0)
223699a2dd95SBruce Richardson return -ENOTSUP;
223799a2dd95SBruce Richardson
223899a2dd95SBruce Richardson return RTE_ALIGN_CEIL((size + priv_size), 8);
223999a2dd95SBruce Richardson }
224099a2dd95SBruce Richardson
224199a2dd95SBruce Richardson int
rte_cryptodev_configure_raw_dp_ctx(uint8_t dev_id,uint16_t qp_id,struct rte_crypto_raw_dp_ctx * ctx,enum rte_crypto_op_sess_type sess_type,union rte_cryptodev_session_ctx session_ctx,uint8_t is_update)224299a2dd95SBruce Richardson rte_cryptodev_configure_raw_dp_ctx(uint8_t dev_id, uint16_t qp_id,
224399a2dd95SBruce Richardson struct rte_crypto_raw_dp_ctx *ctx,
224499a2dd95SBruce Richardson enum rte_crypto_op_sess_type sess_type,
224599a2dd95SBruce Richardson union rte_cryptodev_session_ctx session_ctx,
224699a2dd95SBruce Richardson uint8_t is_update)
224799a2dd95SBruce Richardson {
224899a2dd95SBruce Richardson struct rte_cryptodev *dev;
224999a2dd95SBruce Richardson
225099a2dd95SBruce Richardson if (!rte_cryptodev_get_qp_status(dev_id, qp_id))
225199a2dd95SBruce Richardson return -EINVAL;
225299a2dd95SBruce Richardson
225399a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
225499a2dd95SBruce Richardson if (!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)
225599a2dd95SBruce Richardson || dev->dev_ops->sym_configure_raw_dp_ctx == NULL)
225699a2dd95SBruce Richardson return -ENOTSUP;
225799a2dd95SBruce Richardson
225899a2dd95SBruce Richardson return (*dev->dev_ops->sym_configure_raw_dp_ctx)(dev, qp_id, ctx,
225999a2dd95SBruce Richardson sess_type, session_ctx, is_update);
226099a2dd95SBruce Richardson }
226199a2dd95SBruce Richardson
226299a2dd95SBruce Richardson uint32_t
rte_cryptodev_raw_enqueue_burst(struct rte_crypto_raw_dp_ctx * ctx,struct rte_crypto_sym_vec * vec,union rte_crypto_sym_ofs ofs,void ** user_data,int * enqueue_status)226399a2dd95SBruce Richardson rte_cryptodev_raw_enqueue_burst(struct rte_crypto_raw_dp_ctx *ctx,
226499a2dd95SBruce Richardson struct rte_crypto_sym_vec *vec, union rte_crypto_sym_ofs ofs,
226599a2dd95SBruce Richardson void **user_data, int *enqueue_status)
226699a2dd95SBruce Richardson {
226799a2dd95SBruce Richardson return (*ctx->enqueue_burst)(ctx->qp_data, ctx->drv_ctx_data, vec,
226899a2dd95SBruce Richardson ofs, user_data, enqueue_status);
226999a2dd95SBruce Richardson }
227099a2dd95SBruce Richardson
227199a2dd95SBruce Richardson int
rte_cryptodev_raw_enqueue_done(struct rte_crypto_raw_dp_ctx * ctx,uint32_t n)227299a2dd95SBruce Richardson rte_cryptodev_raw_enqueue_done(struct rte_crypto_raw_dp_ctx *ctx,
227399a2dd95SBruce Richardson uint32_t n)
227499a2dd95SBruce Richardson {
227599a2dd95SBruce Richardson return (*ctx->enqueue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
227699a2dd95SBruce Richardson }
227799a2dd95SBruce Richardson
227899a2dd95SBruce Richardson uint32_t
rte_cryptodev_raw_dequeue_burst(struct rte_crypto_raw_dp_ctx * ctx,rte_cryptodev_raw_get_dequeue_count_t get_dequeue_count,uint32_t max_nb_to_dequeue,rte_cryptodev_raw_post_dequeue_t post_dequeue,void ** out_user_data,uint8_t is_user_data_array,uint32_t * n_success_jobs,int * status)227999a2dd95SBruce Richardson rte_cryptodev_raw_dequeue_burst(struct rte_crypto_raw_dp_ctx *ctx,
228099a2dd95SBruce Richardson rte_cryptodev_raw_get_dequeue_count_t get_dequeue_count,
228199a2dd95SBruce Richardson uint32_t max_nb_to_dequeue,
228299a2dd95SBruce Richardson rte_cryptodev_raw_post_dequeue_t post_dequeue,
228399a2dd95SBruce Richardson void **out_user_data, uint8_t is_user_data_array,
228499a2dd95SBruce Richardson uint32_t *n_success_jobs, int *status)
228599a2dd95SBruce Richardson {
228699a2dd95SBruce Richardson return (*ctx->dequeue_burst)(ctx->qp_data, ctx->drv_ctx_data,
228799a2dd95SBruce Richardson get_dequeue_count, max_nb_to_dequeue, post_dequeue,
228899a2dd95SBruce Richardson out_user_data, is_user_data_array, n_success_jobs, status);
228999a2dd95SBruce Richardson }
229099a2dd95SBruce Richardson
229199a2dd95SBruce Richardson int
rte_cryptodev_raw_dequeue_done(struct rte_crypto_raw_dp_ctx * ctx,uint32_t n)229299a2dd95SBruce Richardson rte_cryptodev_raw_dequeue_done(struct rte_crypto_raw_dp_ctx *ctx,
229399a2dd95SBruce Richardson uint32_t n)
229499a2dd95SBruce Richardson {
229599a2dd95SBruce Richardson return (*ctx->dequeue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
229699a2dd95SBruce Richardson }
229799a2dd95SBruce Richardson
229899a2dd95SBruce Richardson /** Initialise rte_crypto_op mempool element */
229999a2dd95SBruce Richardson static void
rte_crypto_op_init(struct rte_mempool * mempool,void * opaque_arg,void * _op_data,__rte_unused unsigned i)230099a2dd95SBruce Richardson rte_crypto_op_init(struct rte_mempool *mempool,
230199a2dd95SBruce Richardson void *opaque_arg,
230299a2dd95SBruce Richardson void *_op_data,
230399a2dd95SBruce Richardson __rte_unused unsigned i)
230499a2dd95SBruce Richardson {
230599a2dd95SBruce Richardson struct rte_crypto_op *op = _op_data;
230699a2dd95SBruce Richardson enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
230799a2dd95SBruce Richardson
230899a2dd95SBruce Richardson memset(_op_data, 0, mempool->elt_size);
230999a2dd95SBruce Richardson
231099a2dd95SBruce Richardson __rte_crypto_op_reset(op, type);
231199a2dd95SBruce Richardson
231299a2dd95SBruce Richardson op->phys_addr = rte_mem_virt2iova(_op_data);
231399a2dd95SBruce Richardson op->mempool = mempool;
231499a2dd95SBruce Richardson }
231599a2dd95SBruce Richardson
231699a2dd95SBruce Richardson
231799a2dd95SBruce Richardson struct rte_mempool *
rte_crypto_op_pool_create(const char * name,enum rte_crypto_op_type type,unsigned nb_elts,unsigned cache_size,uint16_t priv_size,int socket_id)231899a2dd95SBruce Richardson rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
231999a2dd95SBruce Richardson unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
232099a2dd95SBruce Richardson int socket_id)
232199a2dd95SBruce Richardson {
232299a2dd95SBruce Richardson struct rte_crypto_op_pool_private *priv;
232399a2dd95SBruce Richardson
232499a2dd95SBruce Richardson unsigned elt_size = sizeof(struct rte_crypto_op) +
232599a2dd95SBruce Richardson priv_size;
232699a2dd95SBruce Richardson
232799a2dd95SBruce Richardson if (type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
232899a2dd95SBruce Richardson elt_size += sizeof(struct rte_crypto_sym_op);
232999a2dd95SBruce Richardson } else if (type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
233099a2dd95SBruce Richardson elt_size += sizeof(struct rte_crypto_asym_op);
233199a2dd95SBruce Richardson } else if (type == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
233299a2dd95SBruce Richardson elt_size += RTE_MAX(sizeof(struct rte_crypto_sym_op),
233399a2dd95SBruce Richardson sizeof(struct rte_crypto_asym_op));
233499a2dd95SBruce Richardson } else {
233599a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid op_type\n");
233699a2dd95SBruce Richardson return NULL;
233799a2dd95SBruce Richardson }
233899a2dd95SBruce Richardson
233999a2dd95SBruce Richardson /* lookup mempool in case already allocated */
234099a2dd95SBruce Richardson struct rte_mempool *mp = rte_mempool_lookup(name);
234199a2dd95SBruce Richardson
234299a2dd95SBruce Richardson if (mp != NULL) {
234399a2dd95SBruce Richardson priv = (struct rte_crypto_op_pool_private *)
234499a2dd95SBruce Richardson rte_mempool_get_priv(mp);
234599a2dd95SBruce Richardson
234699a2dd95SBruce Richardson if (mp->elt_size != elt_size ||
234799a2dd95SBruce Richardson mp->cache_size < cache_size ||
234899a2dd95SBruce Richardson mp->size < nb_elts ||
234999a2dd95SBruce Richardson priv->priv_size < priv_size) {
235099a2dd95SBruce Richardson mp = NULL;
235199a2dd95SBruce Richardson CDEV_LOG_ERR("Mempool %s already exists but with "
235299a2dd95SBruce Richardson "incompatible parameters", name);
235399a2dd95SBruce Richardson return NULL;
235499a2dd95SBruce Richardson }
235599a2dd95SBruce Richardson return mp;
235699a2dd95SBruce Richardson }
235799a2dd95SBruce Richardson
235899a2dd95SBruce Richardson mp = rte_mempool_create(
235999a2dd95SBruce Richardson name,
236099a2dd95SBruce Richardson nb_elts,
236199a2dd95SBruce Richardson elt_size,
236299a2dd95SBruce Richardson cache_size,
236399a2dd95SBruce Richardson sizeof(struct rte_crypto_op_pool_private),
236499a2dd95SBruce Richardson NULL,
236599a2dd95SBruce Richardson NULL,
236699a2dd95SBruce Richardson rte_crypto_op_init,
236799a2dd95SBruce Richardson &type,
236899a2dd95SBruce Richardson socket_id,
236999a2dd95SBruce Richardson 0);
237099a2dd95SBruce Richardson
237199a2dd95SBruce Richardson if (mp == NULL) {
237299a2dd95SBruce Richardson CDEV_LOG_ERR("Failed to create mempool %s", name);
237399a2dd95SBruce Richardson return NULL;
237499a2dd95SBruce Richardson }
237599a2dd95SBruce Richardson
237699a2dd95SBruce Richardson priv = (struct rte_crypto_op_pool_private *)
237799a2dd95SBruce Richardson rte_mempool_get_priv(mp);
237899a2dd95SBruce Richardson
237999a2dd95SBruce Richardson priv->priv_size = priv_size;
238099a2dd95SBruce Richardson priv->type = type;
238199a2dd95SBruce Richardson
238299a2dd95SBruce Richardson return mp;
238399a2dd95SBruce Richardson }
238499a2dd95SBruce Richardson
238599a2dd95SBruce Richardson int
rte_cryptodev_pmd_create_dev_name(char * name,const char * dev_name_prefix)238699a2dd95SBruce Richardson rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix)
238799a2dd95SBruce Richardson {
238899a2dd95SBruce Richardson struct rte_cryptodev *dev = NULL;
238999a2dd95SBruce Richardson uint32_t i = 0;
239099a2dd95SBruce Richardson
239199a2dd95SBruce Richardson if (name == NULL)
239299a2dd95SBruce Richardson return -EINVAL;
239399a2dd95SBruce Richardson
239499a2dd95SBruce Richardson for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
239599a2dd95SBruce Richardson int ret = snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
239699a2dd95SBruce Richardson "%s_%u", dev_name_prefix, i);
239799a2dd95SBruce Richardson
239899a2dd95SBruce Richardson if (ret < 0)
239999a2dd95SBruce Richardson return ret;
240099a2dd95SBruce Richardson
240199a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_named_dev(name);
240299a2dd95SBruce Richardson if (!dev)
240399a2dd95SBruce Richardson return 0;
240499a2dd95SBruce Richardson }
240599a2dd95SBruce Richardson
240699a2dd95SBruce Richardson return -1;
240799a2dd95SBruce Richardson }
240899a2dd95SBruce Richardson
240999a2dd95SBruce Richardson TAILQ_HEAD(cryptodev_driver_list, cryptodev_driver);
241099a2dd95SBruce Richardson
241199a2dd95SBruce Richardson static struct cryptodev_driver_list cryptodev_driver_list =
241299a2dd95SBruce Richardson TAILQ_HEAD_INITIALIZER(cryptodev_driver_list);
241399a2dd95SBruce Richardson
241499a2dd95SBruce Richardson int
rte_cryptodev_driver_id_get(const char * name)241599a2dd95SBruce Richardson rte_cryptodev_driver_id_get(const char *name)
241699a2dd95SBruce Richardson {
241799a2dd95SBruce Richardson struct cryptodev_driver *driver;
241899a2dd95SBruce Richardson const char *driver_name;
241999a2dd95SBruce Richardson
242099a2dd95SBruce Richardson if (name == NULL) {
242199a2dd95SBruce Richardson RTE_LOG(DEBUG, CRYPTODEV, "name pointer NULL");
242299a2dd95SBruce Richardson return -1;
242399a2dd95SBruce Richardson }
242499a2dd95SBruce Richardson
242599a2dd95SBruce Richardson TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
242699a2dd95SBruce Richardson driver_name = driver->driver->name;
242799a2dd95SBruce Richardson if (strncmp(driver_name, name, strlen(driver_name) + 1) == 0)
242899a2dd95SBruce Richardson return driver->id;
242999a2dd95SBruce Richardson }
243099a2dd95SBruce Richardson return -1;
243199a2dd95SBruce Richardson }
243299a2dd95SBruce Richardson
243399a2dd95SBruce Richardson const char *
rte_cryptodev_name_get(uint8_t dev_id)243499a2dd95SBruce Richardson rte_cryptodev_name_get(uint8_t dev_id)
243599a2dd95SBruce Richardson {
243699a2dd95SBruce Richardson struct rte_cryptodev *dev;
243799a2dd95SBruce Richardson
243899a2dd95SBruce Richardson if (!rte_cryptodev_is_valid_device_data(dev_id)) {
243999a2dd95SBruce Richardson CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
244099a2dd95SBruce Richardson return NULL;
244199a2dd95SBruce Richardson }
244299a2dd95SBruce Richardson
244399a2dd95SBruce Richardson dev = rte_cryptodev_pmd_get_dev(dev_id);
244499a2dd95SBruce Richardson if (dev == NULL)
244599a2dd95SBruce Richardson return NULL;
244699a2dd95SBruce Richardson
244799a2dd95SBruce Richardson return dev->data->name;
244899a2dd95SBruce Richardson }
244999a2dd95SBruce Richardson
245099a2dd95SBruce Richardson const char *
rte_cryptodev_driver_name_get(uint8_t driver_id)245199a2dd95SBruce Richardson rte_cryptodev_driver_name_get(uint8_t driver_id)
245299a2dd95SBruce Richardson {
245399a2dd95SBruce Richardson struct cryptodev_driver *driver;
245499a2dd95SBruce Richardson
245599a2dd95SBruce Richardson TAILQ_FOREACH(driver, &cryptodev_driver_list, next)
245699a2dd95SBruce Richardson if (driver->id == driver_id)
245799a2dd95SBruce Richardson return driver->driver->name;
245899a2dd95SBruce Richardson return NULL;
245999a2dd95SBruce Richardson }
246099a2dd95SBruce Richardson
246199a2dd95SBruce Richardson uint8_t
rte_cryptodev_allocate_driver(struct cryptodev_driver * crypto_drv,const struct rte_driver * drv)246299a2dd95SBruce Richardson rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
246399a2dd95SBruce Richardson const struct rte_driver *drv)
246499a2dd95SBruce Richardson {
246599a2dd95SBruce Richardson crypto_drv->driver = drv;
246699a2dd95SBruce Richardson crypto_drv->id = nb_drivers;
246799a2dd95SBruce Richardson
246899a2dd95SBruce Richardson TAILQ_INSERT_TAIL(&cryptodev_driver_list, crypto_drv, next);
246999a2dd95SBruce Richardson
247099a2dd95SBruce Richardson return nb_drivers++;
247199a2dd95SBruce Richardson }
24722fd66f75SAkhil Goyal
RTE_INIT(cryptodev_init_fp_ops)24732fd66f75SAkhil Goyal RTE_INIT(cryptodev_init_fp_ops)
24742fd66f75SAkhil Goyal {
24752fd66f75SAkhil Goyal uint32_t i;
24762fd66f75SAkhil Goyal
24772fd66f75SAkhil Goyal for (i = 0; i != RTE_DIM(rte_crypto_fp_ops); i++)
24782fd66f75SAkhil Goyal cryptodev_fp_ops_reset(rte_crypto_fp_ops + i);
24792fd66f75SAkhil Goyal }
2480d3d98f5cSRebecca Troy
2481d3d98f5cSRebecca Troy static int
cryptodev_handle_dev_list(const char * cmd __rte_unused,const char * params __rte_unused,struct rte_tel_data * d)2482d3d98f5cSRebecca Troy cryptodev_handle_dev_list(const char *cmd __rte_unused,
2483d3d98f5cSRebecca Troy const char *params __rte_unused,
2484d3d98f5cSRebecca Troy struct rte_tel_data *d)
2485d3d98f5cSRebecca Troy {
2486d3d98f5cSRebecca Troy int dev_id;
2487d3d98f5cSRebecca Troy
2488d3d98f5cSRebecca Troy if (rte_cryptodev_count() < 1)
2489d3d98f5cSRebecca Troy return -EINVAL;
2490d3d98f5cSRebecca Troy
2491d3d98f5cSRebecca Troy rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
2492d3d98f5cSRebecca Troy for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
2493d3d98f5cSRebecca Troy if (rte_cryptodev_is_valid_dev(dev_id))
2494d3d98f5cSRebecca Troy rte_tel_data_add_array_int(d, dev_id);
2495d3d98f5cSRebecca Troy
2496d3d98f5cSRebecca Troy return 0;
2497d3d98f5cSRebecca Troy }
2498d3d98f5cSRebecca Troy
2499d3d98f5cSRebecca Troy static int
cryptodev_handle_dev_info(const char * cmd __rte_unused,const char * params,struct rte_tel_data * d)2500d3d98f5cSRebecca Troy cryptodev_handle_dev_info(const char *cmd __rte_unused,
2501d3d98f5cSRebecca Troy const char *params, struct rte_tel_data *d)
2502d3d98f5cSRebecca Troy {
2503d3d98f5cSRebecca Troy struct rte_cryptodev_info cryptodev_info;
2504d3d98f5cSRebecca Troy int dev_id;
2505d3d98f5cSRebecca Troy char *end_param;
2506d3d98f5cSRebecca Troy
2507d3d98f5cSRebecca Troy if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2508d3d98f5cSRebecca Troy return -EINVAL;
2509d3d98f5cSRebecca Troy
2510d3d98f5cSRebecca Troy dev_id = strtoul(params, &end_param, 0);
2511d3d98f5cSRebecca Troy if (*end_param != '\0')
2512d3d98f5cSRebecca Troy CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2513d3d98f5cSRebecca Troy if (!rte_cryptodev_is_valid_dev(dev_id))
2514d3d98f5cSRebecca Troy return -EINVAL;
2515d3d98f5cSRebecca Troy
2516d3d98f5cSRebecca Troy rte_cryptodev_info_get(dev_id, &cryptodev_info);
2517d3d98f5cSRebecca Troy
2518d3d98f5cSRebecca Troy rte_tel_data_start_dict(d);
2519d3d98f5cSRebecca Troy rte_tel_data_add_dict_string(d, "device_name",
2520d3d98f5cSRebecca Troy cryptodev_info.device->name);
2521d3d98f5cSRebecca Troy rte_tel_data_add_dict_int(d, "max_nb_queue_pairs",
2522d3d98f5cSRebecca Troy cryptodev_info.max_nb_queue_pairs);
2523d3d98f5cSRebecca Troy
2524d3d98f5cSRebecca Troy return 0;
2525d3d98f5cSRebecca Troy }
2526d3d98f5cSRebecca Troy
2527d3d98f5cSRebecca Troy #define ADD_DICT_STAT(s) rte_tel_data_add_dict_u64(d, #s, cryptodev_stats.s)
2528d3d98f5cSRebecca Troy
2529d3d98f5cSRebecca Troy static int
cryptodev_handle_dev_stats(const char * cmd __rte_unused,const char * params,struct rte_tel_data * d)2530d3d98f5cSRebecca Troy cryptodev_handle_dev_stats(const char *cmd __rte_unused,
2531d3d98f5cSRebecca Troy const char *params,
2532d3d98f5cSRebecca Troy struct rte_tel_data *d)
2533d3d98f5cSRebecca Troy {
2534d3d98f5cSRebecca Troy struct rte_cryptodev_stats cryptodev_stats;
2535d3d98f5cSRebecca Troy int dev_id, ret;
2536d3d98f5cSRebecca Troy char *end_param;
2537d3d98f5cSRebecca Troy
2538d3d98f5cSRebecca Troy if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2539d3d98f5cSRebecca Troy return -EINVAL;
2540d3d98f5cSRebecca Troy
2541d3d98f5cSRebecca Troy dev_id = strtoul(params, &end_param, 0);
2542d3d98f5cSRebecca Troy if (*end_param != '\0')
2543d3d98f5cSRebecca Troy CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2544d3d98f5cSRebecca Troy if (!rte_cryptodev_is_valid_dev(dev_id))
2545d3d98f5cSRebecca Troy return -EINVAL;
2546d3d98f5cSRebecca Troy
2547d3d98f5cSRebecca Troy ret = rte_cryptodev_stats_get(dev_id, &cryptodev_stats);
2548d3d98f5cSRebecca Troy if (ret < 0)
2549d3d98f5cSRebecca Troy return ret;
2550d3d98f5cSRebecca Troy
2551d3d98f5cSRebecca Troy rte_tel_data_start_dict(d);
2552d3d98f5cSRebecca Troy ADD_DICT_STAT(enqueued_count);
2553d3d98f5cSRebecca Troy ADD_DICT_STAT(dequeued_count);
2554d3d98f5cSRebecca Troy ADD_DICT_STAT(enqueue_err_count);
2555d3d98f5cSRebecca Troy ADD_DICT_STAT(dequeue_err_count);
2556d3d98f5cSRebecca Troy
2557d3d98f5cSRebecca Troy return 0;
2558d3d98f5cSRebecca Troy }
2559d3d98f5cSRebecca Troy
25601c559ee8SGowrishankar Muthukrishnan #define CRYPTO_CAPS_SZ \
25611c559ee8SGowrishankar Muthukrishnan (RTE_ALIGN_CEIL(sizeof(struct rte_cryptodev_capabilities), \
25621c559ee8SGowrishankar Muthukrishnan sizeof(uint64_t)) / \
25631c559ee8SGowrishankar Muthukrishnan sizeof(uint64_t))
25641c559ee8SGowrishankar Muthukrishnan
25651c559ee8SGowrishankar Muthukrishnan static int
crypto_caps_array(struct rte_tel_data * d,const struct rte_cryptodev_capabilities * capabilities)25661c559ee8SGowrishankar Muthukrishnan crypto_caps_array(struct rte_tel_data *d,
25671c559ee8SGowrishankar Muthukrishnan const struct rte_cryptodev_capabilities *capabilities)
25681c559ee8SGowrishankar Muthukrishnan {
25691c559ee8SGowrishankar Muthukrishnan const struct rte_cryptodev_capabilities *dev_caps;
25701c559ee8SGowrishankar Muthukrishnan uint64_t caps_val[CRYPTO_CAPS_SZ];
25711c559ee8SGowrishankar Muthukrishnan unsigned int i = 0, j;
25721c559ee8SGowrishankar Muthukrishnan
25731c559ee8SGowrishankar Muthukrishnan rte_tel_data_start_array(d, RTE_TEL_U64_VAL);
25741c559ee8SGowrishankar Muthukrishnan
25751c559ee8SGowrishankar Muthukrishnan while ((dev_caps = &capabilities[i++])->op !=
25761c559ee8SGowrishankar Muthukrishnan RTE_CRYPTO_OP_TYPE_UNDEFINED) {
25771c559ee8SGowrishankar Muthukrishnan memset(&caps_val, 0, CRYPTO_CAPS_SZ * sizeof(caps_val[0]));
25781c559ee8SGowrishankar Muthukrishnan rte_memcpy(caps_val, dev_caps, sizeof(capabilities[0]));
25791c559ee8SGowrishankar Muthukrishnan for (j = 0; j < CRYPTO_CAPS_SZ; j++)
25801c559ee8SGowrishankar Muthukrishnan rte_tel_data_add_array_u64(d, caps_val[j]);
25811c559ee8SGowrishankar Muthukrishnan }
25821c559ee8SGowrishankar Muthukrishnan
25831c559ee8SGowrishankar Muthukrishnan return i;
25841c559ee8SGowrishankar Muthukrishnan }
25851c559ee8SGowrishankar Muthukrishnan
25861c559ee8SGowrishankar Muthukrishnan static int
cryptodev_handle_dev_caps(const char * cmd __rte_unused,const char * params,struct rte_tel_data * d)25871c559ee8SGowrishankar Muthukrishnan cryptodev_handle_dev_caps(const char *cmd __rte_unused, const char *params,
25881c559ee8SGowrishankar Muthukrishnan struct rte_tel_data *d)
25891c559ee8SGowrishankar Muthukrishnan {
25901c559ee8SGowrishankar Muthukrishnan struct rte_cryptodev_info dev_info;
25911c559ee8SGowrishankar Muthukrishnan struct rte_tel_data *crypto_caps;
25921c559ee8SGowrishankar Muthukrishnan int crypto_caps_n;
25931c559ee8SGowrishankar Muthukrishnan char *end_param;
25941c559ee8SGowrishankar Muthukrishnan int dev_id;
25951c559ee8SGowrishankar Muthukrishnan
25961c559ee8SGowrishankar Muthukrishnan if (!params || strlen(params) == 0 || !isdigit(*params))
25971c559ee8SGowrishankar Muthukrishnan return -EINVAL;
25981c559ee8SGowrishankar Muthukrishnan
25991c559ee8SGowrishankar Muthukrishnan dev_id = strtoul(params, &end_param, 0);
26001c559ee8SGowrishankar Muthukrishnan if (*end_param != '\0')
26011c559ee8SGowrishankar Muthukrishnan CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
26021c559ee8SGowrishankar Muthukrishnan if (!rte_cryptodev_is_valid_dev(dev_id))
26031c559ee8SGowrishankar Muthukrishnan return -EINVAL;
26041c559ee8SGowrishankar Muthukrishnan
26051c559ee8SGowrishankar Muthukrishnan rte_tel_data_start_dict(d);
26061c559ee8SGowrishankar Muthukrishnan crypto_caps = rte_tel_data_alloc();
26071c559ee8SGowrishankar Muthukrishnan if (!crypto_caps)
26081c559ee8SGowrishankar Muthukrishnan return -ENOMEM;
26091c559ee8SGowrishankar Muthukrishnan
26101c559ee8SGowrishankar Muthukrishnan rte_cryptodev_info_get(dev_id, &dev_info);
26111c559ee8SGowrishankar Muthukrishnan crypto_caps_n = crypto_caps_array(crypto_caps, dev_info.capabilities);
26121c559ee8SGowrishankar Muthukrishnan rte_tel_data_add_dict_container(d, "crypto_caps", crypto_caps, 0);
26131c559ee8SGowrishankar Muthukrishnan rte_tel_data_add_dict_int(d, "crypto_caps_n", crypto_caps_n);
26141c559ee8SGowrishankar Muthukrishnan
26151c559ee8SGowrishankar Muthukrishnan return 0;
26161c559ee8SGowrishankar Muthukrishnan }
26171c559ee8SGowrishankar Muthukrishnan
RTE_INIT(cryptodev_init_telemetry)2618d3d98f5cSRebecca Troy RTE_INIT(cryptodev_init_telemetry)
2619d3d98f5cSRebecca Troy {
2620d3d98f5cSRebecca Troy rte_telemetry_register_cmd("/cryptodev/info", cryptodev_handle_dev_info,
2621d3d98f5cSRebecca Troy "Returns information for a cryptodev. Parameters: int dev_id");
2622d3d98f5cSRebecca Troy rte_telemetry_register_cmd("/cryptodev/list",
2623d3d98f5cSRebecca Troy cryptodev_handle_dev_list,
2624d3d98f5cSRebecca Troy "Returns list of available crypto devices by IDs. No parameters.");
2625d3d98f5cSRebecca Troy rte_telemetry_register_cmd("/cryptodev/stats",
2626d3d98f5cSRebecca Troy cryptodev_handle_dev_stats,
2627d3d98f5cSRebecca Troy "Returns the stats for a cryptodev. Parameters: int dev_id");
26281c559ee8SGowrishankar Muthukrishnan rte_telemetry_register_cmd("/cryptodev/caps",
26291c559ee8SGowrishankar Muthukrishnan cryptodev_handle_dev_caps,
26301c559ee8SGowrishankar Muthukrishnan "Returns the capabilities for a cryptodev. Parameters: int dev_id");
2631d3d98f5cSRebecca Troy }
2632