xref: /dpdk/lib/cryptodev/rte_cryptodev.c (revision 877cb3e374261ad57116c7d8c05fae1eb53f0e13)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020 Intel Corporation
3  */
4 
5 #include <sys/queue.h>
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <stdint.h>
12 #include <inttypes.h>
13 
14 #include <rte_log.h>
15 #include <rte_debug.h>
16 #include <dev_driver.h>
17 #include <rte_memory.h>
18 #include <rte_memcpy.h>
19 #include <rte_memzone.h>
20 #include <rte_eal.h>
21 #include <rte_common.h>
22 #include <rte_mempool.h>
23 #include <rte_malloc.h>
24 #include <rte_errno.h>
25 #include <rte_spinlock.h>
26 #include <rte_string_fns.h>
27 #include <rte_telemetry.h>
28 
29 #include "rte_crypto.h"
30 #include "rte_cryptodev.h"
31 #include "cryptodev_pmd.h"
32 #include "cryptodev_trace.h"
33 
34 static uint8_t nb_drivers;
35 
36 static struct rte_cryptodev rte_crypto_devices[RTE_CRYPTO_MAX_DEVS];
37 
38 struct rte_cryptodev *rte_cryptodevs = rte_crypto_devices;
39 
40 static struct rte_cryptodev_global cryptodev_globals = {
41 		.devs			= rte_crypto_devices,
42 		.data			= { NULL },
43 		.nb_devs		= 0
44 };
45 
46 /* Public fastpath APIs. */
47 struct rte_crypto_fp_ops rte_crypto_fp_ops[RTE_CRYPTO_MAX_DEVS];
48 
49 /* spinlock for crypto device callbacks */
50 static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
51 
52 /**
53  * The user application callback description.
54  *
55  * It contains callback address to be registered by user application,
56  * the pointer to the parameters for callback, and the event type.
57  */
58 struct rte_cryptodev_callback {
59 	TAILQ_ENTRY(rte_cryptodev_callback) next; /**< Callbacks list */
60 	rte_cryptodev_cb_fn cb_fn;		/**< Callback address */
61 	void *cb_arg;				/**< Parameter for callback */
62 	enum rte_cryptodev_event_type event;	/**< Interrupt event type */
63 	uint32_t active;			/**< Callback is executing */
64 };
65 
66 /**
67  * The crypto cipher algorithm strings identifiers.
68  * Not to be used in application directly.
69  * Application can use rte_cryptodev_get_cipher_algo_string().
70  */
71 static const char *
72 crypto_cipher_algorithm_strings[] = {
73 	[RTE_CRYPTO_CIPHER_3DES_CBC]	= "3des-cbc",
74 	[RTE_CRYPTO_CIPHER_3DES_ECB]	= "3des-ecb",
75 	[RTE_CRYPTO_CIPHER_3DES_CTR]	= "3des-ctr",
76 
77 	[RTE_CRYPTO_CIPHER_AES_CBC]	= "aes-cbc",
78 	[RTE_CRYPTO_CIPHER_AES_CTR]	= "aes-ctr",
79 	[RTE_CRYPTO_CIPHER_AES_DOCSISBPI]	= "aes-docsisbpi",
80 	[RTE_CRYPTO_CIPHER_AES_ECB]	= "aes-ecb",
81 	[RTE_CRYPTO_CIPHER_AES_F8]	= "aes-f8",
82 	[RTE_CRYPTO_CIPHER_AES_XTS]	= "aes-xts",
83 
84 	[RTE_CRYPTO_CIPHER_ARC4]	= "arc4",
85 
86 	[RTE_CRYPTO_CIPHER_DES_CBC]     = "des-cbc",
87 	[RTE_CRYPTO_CIPHER_DES_DOCSISBPI]	= "des-docsisbpi",
88 
89 	[RTE_CRYPTO_CIPHER_NULL]	= "null",
90 
91 	[RTE_CRYPTO_CIPHER_KASUMI_F8]	= "kasumi-f8",
92 	[RTE_CRYPTO_CIPHER_SNOW3G_UEA2]	= "snow3g-uea2",
93 	[RTE_CRYPTO_CIPHER_ZUC_EEA3]	= "zuc-eea3",
94 	[RTE_CRYPTO_CIPHER_SM4_ECB]	= "sm4-ecb",
95 	[RTE_CRYPTO_CIPHER_SM4_CBC]	= "sm4-cbc",
96 	[RTE_CRYPTO_CIPHER_SM4_CTR]	= "sm4-ctr",
97 	[RTE_CRYPTO_CIPHER_SM4_CFB]	= "sm4-cfb",
98 	[RTE_CRYPTO_CIPHER_SM4_OFB]	= "sm4-ofb"
99 };
100 
101 /**
102  * The crypto cipher operation strings identifiers.
103  * It could be used in application command line.
104  */
105 const char *
106 rte_crypto_cipher_operation_strings[] = {
107 		[RTE_CRYPTO_CIPHER_OP_ENCRYPT]	= "encrypt",
108 		[RTE_CRYPTO_CIPHER_OP_DECRYPT]	= "decrypt"
109 };
110 
111 /**
112  * The crypto auth algorithm strings identifiers.
113  * Not to be used in application directly.
114  * Application can use rte_cryptodev_get_auth_algo_string().
115  */
116 static const char *
117 crypto_auth_algorithm_strings[] = {
118 	[RTE_CRYPTO_AUTH_AES_CBC_MAC]	= "aes-cbc-mac",
119 	[RTE_CRYPTO_AUTH_AES_CMAC]	= "aes-cmac",
120 	[RTE_CRYPTO_AUTH_AES_GMAC]	= "aes-gmac",
121 	[RTE_CRYPTO_AUTH_AES_XCBC_MAC]	= "aes-xcbc-mac",
122 
123 	[RTE_CRYPTO_AUTH_MD5]		= "md5",
124 	[RTE_CRYPTO_AUTH_MD5_HMAC]	= "md5-hmac",
125 
126 	[RTE_CRYPTO_AUTH_NULL]		= "null",
127 
128 	[RTE_CRYPTO_AUTH_SHA1]		= "sha1",
129 	[RTE_CRYPTO_AUTH_SHA1_HMAC]	= "sha1-hmac",
130 
131 	[RTE_CRYPTO_AUTH_SHA224]	= "sha2-224",
132 	[RTE_CRYPTO_AUTH_SHA224_HMAC]	= "sha2-224-hmac",
133 	[RTE_CRYPTO_AUTH_SHA256]	= "sha2-256",
134 	[RTE_CRYPTO_AUTH_SHA256_HMAC]	= "sha2-256-hmac",
135 	[RTE_CRYPTO_AUTH_SHA384]	= "sha2-384",
136 	[RTE_CRYPTO_AUTH_SHA384_HMAC]	= "sha2-384-hmac",
137 	[RTE_CRYPTO_AUTH_SHA512]	= "sha2-512",
138 	[RTE_CRYPTO_AUTH_SHA512_HMAC]	= "sha2-512-hmac",
139 
140 	[RTE_CRYPTO_AUTH_SHA3_224]	= "sha3-224",
141 	[RTE_CRYPTO_AUTH_SHA3_224_HMAC] = "sha3-224-hmac",
142 	[RTE_CRYPTO_AUTH_SHA3_256]	= "sha3-256",
143 	[RTE_CRYPTO_AUTH_SHA3_256_HMAC] = "sha3-256-hmac",
144 	[RTE_CRYPTO_AUTH_SHA3_384]	= "sha3-384",
145 	[RTE_CRYPTO_AUTH_SHA3_384_HMAC] = "sha3-384-hmac",
146 	[RTE_CRYPTO_AUTH_SHA3_512]	= "sha3-512",
147 	[RTE_CRYPTO_AUTH_SHA3_512_HMAC]	= "sha3-512-hmac",
148 
149 	[RTE_CRYPTO_AUTH_KASUMI_F9]	= "kasumi-f9",
150 	[RTE_CRYPTO_AUTH_SNOW3G_UIA2]	= "snow3g-uia2",
151 	[RTE_CRYPTO_AUTH_ZUC_EIA3]	= "zuc-eia3",
152 	[RTE_CRYPTO_AUTH_SM3]		= "sm3",
153 	[RTE_CRYPTO_AUTH_SM3_HMAC]	= "sm3-hmac",
154 
155 	[RTE_CRYPTO_AUTH_SHAKE_128]	 = "shake-128",
156 	[RTE_CRYPTO_AUTH_SHAKE_256]	 = "shake-256",
157 };
158 
159 /**
160  * The crypto AEAD algorithm strings identifiers.
161  * Not to be used in application directly.
162  * Application can use rte_cryptodev_get_aead_algo_string().
163  */
164 static const char *
165 crypto_aead_algorithm_strings[] = {
166 	[RTE_CRYPTO_AEAD_AES_CCM]	= "aes-ccm",
167 	[RTE_CRYPTO_AEAD_AES_GCM]	= "aes-gcm",
168 	[RTE_CRYPTO_AEAD_CHACHA20_POLY1305] = "chacha20-poly1305"
169 };
170 
171 
172 /**
173  * The crypto AEAD operation strings identifiers.
174  * It could be used in application command line.
175  */
176 const char *
177 rte_crypto_aead_operation_strings[] = {
178 	[RTE_CRYPTO_AEAD_OP_ENCRYPT]	= "encrypt",
179 	[RTE_CRYPTO_AEAD_OP_DECRYPT]	= "decrypt"
180 };
181 
182 /**
183  * Asymmetric crypto transform operation strings identifiers.
184  * Not to be used in application directly.
185  * Application can use rte_cryptodev_asym_get_xform_string().
186  */
187 static const char *
188 crypto_asym_xform_strings[] = {
189 	[RTE_CRYPTO_ASYM_XFORM_NONE]	= "none",
190 	[RTE_CRYPTO_ASYM_XFORM_RSA]	= "rsa",
191 	[RTE_CRYPTO_ASYM_XFORM_MODEX]	= "modexp",
192 	[RTE_CRYPTO_ASYM_XFORM_MODINV]	= "modinv",
193 	[RTE_CRYPTO_ASYM_XFORM_DH]	= "dh",
194 	[RTE_CRYPTO_ASYM_XFORM_DSA]	= "dsa",
195 	[RTE_CRYPTO_ASYM_XFORM_ECDSA]	= "ecdsa",
196 	[RTE_CRYPTO_ASYM_XFORM_ECPM]	= "ecpm",
197 	[RTE_CRYPTO_ASYM_XFORM_SM2]	= "sm2",
198 };
199 
200 /**
201  * Asymmetric crypto operation strings identifiers.
202  */
203 const char *rte_crypto_asym_op_strings[] = {
204 	[RTE_CRYPTO_ASYM_OP_ENCRYPT]	= "encrypt",
205 	[RTE_CRYPTO_ASYM_OP_DECRYPT]	= "decrypt",
206 	[RTE_CRYPTO_ASYM_OP_SIGN]	= "sign",
207 	[RTE_CRYPTO_ASYM_OP_VERIFY]	= "verify"
208 };
209 
210 /**
211  * Asymmetric crypto key exchange operation strings identifiers.
212  */
213 const char *rte_crypto_asym_ke_strings[] = {
214 	[RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE] = "priv_key_generate",
215 	[RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE] = "pub_key_generate",
216 	[RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE] = "sharedsecret_compute",
217 	[RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY] = "pub_ec_key_verify"
218 };
219 
220 struct rte_cryptodev_sym_session_pool_private_data {
221 	uint16_t sess_data_sz;
222 	/**< driver session data size */
223 	uint16_t user_data_sz;
224 	/**< session user data will be placed after sess_data */
225 };
226 
227 /**
228  * The private data structure stored in the asym session mempool private data.
229  */
230 struct rte_cryptodev_asym_session_pool_private_data {
231 	uint16_t max_priv_session_sz;
232 	/**< Size of private session data used when creating mempool */
233 	uint16_t user_data_sz;
234 	/**< Session user data will be placed after sess_private_data */
235 };
236 
237 int
238 rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
239 		const char *algo_string)
240 {
241 	unsigned int i;
242 	int ret = -1;	/* Invalid string */
243 
244 	for (i = 1; i < RTE_DIM(crypto_cipher_algorithm_strings); i++) {
245 		if (strcmp(algo_string, crypto_cipher_algorithm_strings[i]) == 0) {
246 			*algo_enum = (enum rte_crypto_cipher_algorithm) i;
247 			ret = 0;
248 			break;
249 		}
250 	}
251 
252 	rte_cryptodev_trace_get_cipher_algo_enum(algo_string, *algo_enum, ret);
253 
254 	return ret;
255 }
256 
257 int
258 rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
259 		const char *algo_string)
260 {
261 	unsigned int i;
262 	int ret = -1;	/* Invalid string */
263 
264 	for (i = 1; i < RTE_DIM(crypto_auth_algorithm_strings); i++) {
265 		if (strcmp(algo_string, crypto_auth_algorithm_strings[i]) == 0) {
266 			*algo_enum = (enum rte_crypto_auth_algorithm) i;
267 			ret = 0;
268 			break;
269 		}
270 	}
271 
272 	rte_cryptodev_trace_get_auth_algo_enum(algo_string, *algo_enum, ret);
273 
274 	return ret;
275 }
276 
277 int
278 rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
279 		const char *algo_string)
280 {
281 	unsigned int i;
282 	int ret = -1;	/* Invalid string */
283 
284 	for (i = 1; i < RTE_DIM(crypto_aead_algorithm_strings); i++) {
285 		if (strcmp(algo_string, crypto_aead_algorithm_strings[i]) == 0) {
286 			*algo_enum = (enum rte_crypto_aead_algorithm) i;
287 			ret = 0;
288 			break;
289 		}
290 	}
291 
292 	rte_cryptodev_trace_get_aead_algo_enum(algo_string, *algo_enum, ret);
293 
294 	return ret;
295 }
296 
297 int
298 rte_cryptodev_asym_get_xform_enum(enum rte_crypto_asym_xform_type *xform_enum,
299 		const char *xform_string)
300 {
301 	unsigned int i;
302 	int ret = -1;	/* Invalid string */
303 
304 	for (i = 1; i < RTE_DIM(crypto_asym_xform_strings); i++) {
305 		if (strcmp(xform_string,
306 			crypto_asym_xform_strings[i]) == 0) {
307 			*xform_enum = (enum rte_crypto_asym_xform_type) i;
308 			ret = 0;
309 			break;
310 		}
311 	}
312 
313 	rte_cryptodev_trace_asym_get_xform_enum(xform_string, *xform_enum, ret);
314 
315 	return ret;
316 }
317 
318 const char *
319 rte_cryptodev_get_cipher_algo_string(enum rte_crypto_cipher_algorithm algo_enum)
320 {
321 	const char *alg_str = NULL;
322 
323 	if ((unsigned int)algo_enum < RTE_DIM(crypto_cipher_algorithm_strings))
324 		alg_str = crypto_cipher_algorithm_strings[algo_enum];
325 
326 	rte_cryptodev_trace_get_cipher_algo_string(algo_enum, alg_str);
327 
328 	return alg_str;
329 }
330 
331 const char *
332 rte_cryptodev_get_auth_algo_string(enum rte_crypto_auth_algorithm algo_enum)
333 {
334 	const char *alg_str = NULL;
335 
336 	if ((unsigned int)algo_enum < RTE_DIM(crypto_auth_algorithm_strings))
337 		alg_str = crypto_auth_algorithm_strings[algo_enum];
338 
339 	rte_cryptodev_trace_get_auth_algo_string(algo_enum, alg_str);
340 
341 	return alg_str;
342 }
343 
344 const char *
345 rte_cryptodev_get_aead_algo_string(enum rte_crypto_aead_algorithm algo_enum)
346 {
347 	const char *alg_str = NULL;
348 
349 	if ((unsigned int)algo_enum < RTE_DIM(crypto_aead_algorithm_strings))
350 		alg_str = crypto_aead_algorithm_strings[algo_enum];
351 
352 	rte_cryptodev_trace_get_aead_algo_string(algo_enum, alg_str);
353 
354 	return alg_str;
355 }
356 
357 const char *
358 rte_cryptodev_asym_get_xform_string(enum rte_crypto_asym_xform_type xform_enum)
359 {
360 	const char *xform_str = NULL;
361 
362 	if ((unsigned int)xform_enum < RTE_DIM(crypto_asym_xform_strings))
363 		xform_str = crypto_asym_xform_strings[xform_enum];
364 
365 	rte_cryptodev_trace_asym_get_xform_string(xform_enum, xform_str);
366 
367 	return xform_str;
368 }
369 
370 /**
371  * The crypto auth operation strings identifiers.
372  * It could be used in application command line.
373  */
374 const char *
375 rte_crypto_auth_operation_strings[] = {
376 		[RTE_CRYPTO_AUTH_OP_VERIFY]	= "verify",
377 		[RTE_CRYPTO_AUTH_OP_GENERATE]	= "generate"
378 };
379 
380 const struct rte_cryptodev_symmetric_capability *
381 rte_cryptodev_sym_capability_get(uint8_t dev_id,
382 		const struct rte_cryptodev_sym_capability_idx *idx)
383 {
384 	const struct rte_cryptodev_capabilities *capability;
385 	const struct rte_cryptodev_symmetric_capability *sym_capability = NULL;
386 	struct rte_cryptodev_info dev_info;
387 	int i = 0;
388 
389 	rte_cryptodev_info_get(dev_id, &dev_info);
390 
391 	while ((capability = &dev_info.capabilities[i++])->op !=
392 			RTE_CRYPTO_OP_TYPE_UNDEFINED) {
393 		if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
394 			continue;
395 
396 		if (capability->sym.xform_type != idx->type)
397 			continue;
398 
399 		if (idx->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
400 			capability->sym.auth.algo == idx->algo.auth) {
401 			sym_capability = &capability->sym;
402 			break;
403 		}
404 
405 		if (idx->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
406 			capability->sym.cipher.algo == idx->algo.cipher) {
407 			sym_capability = &capability->sym;
408 			break;
409 		}
410 
411 		if (idx->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
412 				capability->sym.aead.algo == idx->algo.aead) {
413 			sym_capability = &capability->sym;
414 			break;
415 		}
416 	}
417 
418 	rte_cryptodev_trace_sym_capability_get(dev_id, dev_info.driver_name,
419 		dev_info.driver_id, idx->type, sym_capability);
420 
421 	return sym_capability;
422 }
423 
424 static int
425 param_range_check(uint16_t size, const struct rte_crypto_param_range *range)
426 {
427 	unsigned int next_size;
428 
429 	/* Check lower/upper bounds */
430 	if (size < range->min)
431 		return -1;
432 
433 	if (size > range->max)
434 		return -1;
435 
436 	/* If range is actually only one value, size is correct */
437 	if (range->increment == 0)
438 		return 0;
439 
440 	/* Check if value is one of the supported sizes */
441 	for (next_size = range->min; next_size <= range->max;
442 			next_size += range->increment)
443 		if (size == next_size)
444 			return 0;
445 
446 	return -1;
447 }
448 
449 const struct rte_cryptodev_asymmetric_xform_capability *
450 rte_cryptodev_asym_capability_get(uint8_t dev_id,
451 		const struct rte_cryptodev_asym_capability_idx *idx)
452 {
453 	const struct rte_cryptodev_capabilities *capability;
454 	const struct rte_cryptodev_asymmetric_xform_capability *asym_cap = NULL;
455 	struct rte_cryptodev_info dev_info;
456 	unsigned int i = 0;
457 
458 	memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
459 	rte_cryptodev_info_get(dev_id, &dev_info);
460 
461 	while ((capability = &dev_info.capabilities[i++])->op !=
462 			RTE_CRYPTO_OP_TYPE_UNDEFINED) {
463 		if (capability->op != RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
464 			continue;
465 
466 		if (capability->asym.xform_capa.xform_type == idx->type) {
467 			asym_cap = &capability->asym.xform_capa;
468 			break;
469 		}
470 	}
471 
472 	rte_cryptodev_trace_asym_capability_get(dev_info.driver_name,
473 		dev_info.driver_id, idx->type, asym_cap);
474 
475 	return asym_cap;
476 };
477 
478 int
479 rte_cryptodev_sym_capability_check_cipher(
480 		const struct rte_cryptodev_symmetric_capability *capability,
481 		uint16_t key_size, uint16_t iv_size)
482 {
483 	int ret = 0; /* success */
484 
485 	if (param_range_check(key_size, &capability->cipher.key_size) != 0) {
486 		ret = -1;
487 		goto done;
488 	}
489 
490 	if (param_range_check(iv_size, &capability->cipher.iv_size) != 0)
491 		ret = -1;
492 
493 done:
494 	rte_cryptodev_trace_sym_capability_check_cipher(capability, key_size,
495 		iv_size, ret);
496 
497 	return ret;
498 }
499 
500 int
501 rte_cryptodev_sym_capability_check_auth(
502 		const struct rte_cryptodev_symmetric_capability *capability,
503 		uint16_t key_size, uint16_t digest_size, uint16_t iv_size)
504 {
505 	int ret = 0; /* success */
506 
507 	if (param_range_check(key_size, &capability->auth.key_size) != 0) {
508 		ret = -1;
509 		goto done;
510 	}
511 
512 	if (param_range_check(digest_size,
513 		&capability->auth.digest_size) != 0) {
514 		ret = -1;
515 		goto done;
516 	}
517 
518 	if (param_range_check(iv_size, &capability->auth.iv_size) != 0)
519 		ret = -1;
520 
521 done:
522 	rte_cryptodev_trace_sym_capability_check_auth(capability, key_size,
523 		digest_size, iv_size, ret);
524 
525 	return ret;
526 }
527 
528 int
529 rte_cryptodev_sym_capability_check_aead(
530 		const struct rte_cryptodev_symmetric_capability *capability,
531 		uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
532 		uint16_t iv_size)
533 {
534 	int ret = 0; /* success */
535 
536 	if (param_range_check(key_size, &capability->aead.key_size) != 0) {
537 		ret = -1;
538 		goto done;
539 	}
540 
541 	if (param_range_check(digest_size,
542 		&capability->aead.digest_size) != 0) {
543 		ret = -1;
544 		goto done;
545 	}
546 
547 	if (param_range_check(aad_size, &capability->aead.aad_size) != 0) {
548 		ret = -1;
549 		goto done;
550 	}
551 
552 	if (param_range_check(iv_size, &capability->aead.iv_size) != 0)
553 		ret = -1;
554 
555 done:
556 	rte_cryptodev_trace_sym_capability_check_aead(capability, key_size,
557 		digest_size, aad_size, iv_size, ret);
558 
559 	return ret;
560 }
561 
562 int
563 rte_cryptodev_asym_xform_capability_check_optype(
564 	const struct rte_cryptodev_asymmetric_xform_capability *capability,
565 	enum rte_crypto_asym_op_type op_type)
566 {
567 	int ret = 0;
568 
569 	if (capability->op_types & (1 << op_type))
570 		ret = 1;
571 
572 	rte_cryptodev_trace_asym_xform_capability_check_optype(
573 		capability->op_types, op_type, ret);
574 
575 	return ret;
576 }
577 
578 int
579 rte_cryptodev_asym_xform_capability_check_modlen(
580 	const struct rte_cryptodev_asymmetric_xform_capability *capability,
581 	uint16_t modlen)
582 {
583 	int ret = 0; /* success */
584 
585 	/* no need to check for limits, if min or max = 0 */
586 	if (capability->modlen.min != 0) {
587 		if (modlen < capability->modlen.min) {
588 			ret = -1;
589 			goto done;
590 		}
591 	}
592 
593 	if (capability->modlen.max != 0) {
594 		if (modlen > capability->modlen.max) {
595 			ret = -1;
596 			goto done;
597 		}
598 	}
599 
600 	/* in any case, check if given modlen is module increment */
601 	if (capability->modlen.increment != 0) {
602 		if (modlen % (capability->modlen.increment))
603 			ret = -1;
604 	}
605 
606 done:
607 	rte_cryptodev_trace_asym_xform_capability_check_modlen(capability,
608 		modlen, ret);
609 
610 	return ret;
611 }
612 
613 bool
614 rte_cryptodev_asym_xform_capability_check_hash(
615 	const struct rte_cryptodev_asymmetric_xform_capability *capability,
616 	enum rte_crypto_auth_algorithm hash)
617 {
618 	bool ret = false;
619 
620 	if (capability->hash_algos & (1 << hash))
621 		ret = true;
622 
623 	rte_cryptodev_trace_asym_xform_capability_check_hash(
624 		capability->hash_algos, hash, ret);
625 
626 	return ret;
627 }
628 
629 /* spinlock for crypto device enq callbacks */
630 static rte_spinlock_t rte_cryptodev_callback_lock = RTE_SPINLOCK_INITIALIZER;
631 
632 static void
633 cryptodev_cb_cleanup(struct rte_cryptodev *dev)
634 {
635 	struct rte_cryptodev_cb_rcu *list;
636 	struct rte_cryptodev_cb *cb, *next;
637 	uint16_t qp_id;
638 
639 	if (dev->enq_cbs == NULL && dev->deq_cbs == NULL)
640 		return;
641 
642 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
643 		list = &dev->enq_cbs[qp_id];
644 		cb = list->next;
645 		while (cb != NULL) {
646 			next = cb->next;
647 			rte_free(cb);
648 			cb = next;
649 		}
650 
651 		rte_free(list->qsbr);
652 	}
653 
654 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
655 		list = &dev->deq_cbs[qp_id];
656 		cb = list->next;
657 		while (cb != NULL) {
658 			next = cb->next;
659 			rte_free(cb);
660 			cb = next;
661 		}
662 
663 		rte_free(list->qsbr);
664 	}
665 
666 	rte_free(dev->enq_cbs);
667 	dev->enq_cbs = NULL;
668 	rte_free(dev->deq_cbs);
669 	dev->deq_cbs = NULL;
670 }
671 
672 static int
673 cryptodev_cb_init(struct rte_cryptodev *dev)
674 {
675 	struct rte_cryptodev_cb_rcu *list;
676 	struct rte_rcu_qsbr *qsbr;
677 	uint16_t qp_id;
678 	size_t size;
679 
680 	/* Max thread set to 1, as one DP thread accessing a queue-pair */
681 	const uint32_t max_threads = 1;
682 
683 	dev->enq_cbs = rte_zmalloc(NULL,
684 				   sizeof(struct rte_cryptodev_cb_rcu) *
685 				   dev->data->nb_queue_pairs, 0);
686 	if (dev->enq_cbs == NULL) {
687 		CDEV_LOG_ERR("Failed to allocate memory for enq callbacks");
688 		return -ENOMEM;
689 	}
690 
691 	dev->deq_cbs = rte_zmalloc(NULL,
692 				   sizeof(struct rte_cryptodev_cb_rcu) *
693 				   dev->data->nb_queue_pairs, 0);
694 	if (dev->deq_cbs == NULL) {
695 		CDEV_LOG_ERR("Failed to allocate memory for deq callbacks");
696 		rte_free(dev->enq_cbs);
697 		return -ENOMEM;
698 	}
699 
700 	/* Create RCU QSBR variable */
701 	size = rte_rcu_qsbr_get_memsize(max_threads);
702 
703 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
704 		list = &dev->enq_cbs[qp_id];
705 		qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
706 		if (qsbr == NULL) {
707 			CDEV_LOG_ERR("Failed to allocate memory for RCU on "
708 				"queue_pair_id=%d", qp_id);
709 			goto cb_init_err;
710 		}
711 
712 		if (rte_rcu_qsbr_init(qsbr, max_threads)) {
713 			CDEV_LOG_ERR("Failed to initialize for RCU on "
714 				"queue_pair_id=%d", qp_id);
715 			goto cb_init_err;
716 		}
717 
718 		list->qsbr = qsbr;
719 	}
720 
721 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
722 		list = &dev->deq_cbs[qp_id];
723 		qsbr = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
724 		if (qsbr == NULL) {
725 			CDEV_LOG_ERR("Failed to allocate memory for RCU on "
726 				"queue_pair_id=%d", qp_id);
727 			goto cb_init_err;
728 		}
729 
730 		if (rte_rcu_qsbr_init(qsbr, max_threads)) {
731 			CDEV_LOG_ERR("Failed to initialize for RCU on "
732 				"queue_pair_id=%d", qp_id);
733 			goto cb_init_err;
734 		}
735 
736 		list->qsbr = qsbr;
737 	}
738 
739 	return 0;
740 
741 cb_init_err:
742 	cryptodev_cb_cleanup(dev);
743 	return -ENOMEM;
744 }
745 
746 const char *
747 rte_cryptodev_get_feature_name(uint64_t flag)
748 {
749 	rte_cryptodev_trace_get_feature_name(flag);
750 
751 	switch (flag) {
752 	case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
753 		return "SYMMETRIC_CRYPTO";
754 	case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
755 		return "ASYMMETRIC_CRYPTO";
756 	case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
757 		return "SYM_OPERATION_CHAINING";
758 	case RTE_CRYPTODEV_FF_CPU_SSE:
759 		return "CPU_SSE";
760 	case RTE_CRYPTODEV_FF_CPU_AVX:
761 		return "CPU_AVX";
762 	case RTE_CRYPTODEV_FF_CPU_AVX2:
763 		return "CPU_AVX2";
764 	case RTE_CRYPTODEV_FF_CPU_AVX512:
765 		return "CPU_AVX512";
766 	case RTE_CRYPTODEV_FF_CPU_AESNI:
767 		return "CPU_AESNI";
768 	case RTE_CRYPTODEV_FF_HW_ACCELERATED:
769 		return "HW_ACCELERATED";
770 	case RTE_CRYPTODEV_FF_IN_PLACE_SGL:
771 		return "IN_PLACE_SGL";
772 	case RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT:
773 		return "OOP_SGL_IN_SGL_OUT";
774 	case RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT:
775 		return "OOP_SGL_IN_LB_OUT";
776 	case RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT:
777 		return "OOP_LB_IN_SGL_OUT";
778 	case RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT:
779 		return "OOP_LB_IN_LB_OUT";
780 	case RTE_CRYPTODEV_FF_CPU_NEON:
781 		return "CPU_NEON";
782 	case RTE_CRYPTODEV_FF_CPU_ARM_CE:
783 		return "CPU_ARM_CE";
784 	case RTE_CRYPTODEV_FF_SECURITY:
785 		return "SECURITY_PROTOCOL";
786 	case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP:
787 		return "RSA_PRIV_OP_KEY_EXP";
788 	case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT:
789 		return "RSA_PRIV_OP_KEY_QT";
790 	case RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED:
791 		return "DIGEST_ENCRYPTED";
792 	case RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO:
793 		return "SYM_CPU_CRYPTO";
794 	case RTE_CRYPTODEV_FF_ASYM_SESSIONLESS:
795 		return "ASYM_SESSIONLESS";
796 	case RTE_CRYPTODEV_FF_SYM_SESSIONLESS:
797 		return "SYM_SESSIONLESS";
798 	case RTE_CRYPTODEV_FF_NON_BYTE_ALIGNED_DATA:
799 		return "NON_BYTE_ALIGNED_DATA";
800 	case RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS:
801 		return "CIPHER_MULTIPLE_DATA_UNITS";
802 	case RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY:
803 		return "CIPHER_WRAPPED_KEY";
804 	default:
805 		return NULL;
806 	}
807 }
808 
809 struct rte_cryptodev *
810 rte_cryptodev_pmd_get_dev(uint8_t dev_id)
811 {
812 	return &cryptodev_globals.devs[dev_id];
813 }
814 
815 struct rte_cryptodev *
816 rte_cryptodev_pmd_get_named_dev(const char *name)
817 {
818 	struct rte_cryptodev *dev;
819 	unsigned int i;
820 
821 	if (name == NULL)
822 		return NULL;
823 
824 	for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
825 		dev = &cryptodev_globals.devs[i];
826 
827 		if ((dev->attached == RTE_CRYPTODEV_ATTACHED) &&
828 				(strcmp(dev->data->name, name) == 0))
829 			return dev;
830 	}
831 
832 	return NULL;
833 }
834 
835 static inline uint8_t
836 rte_cryptodev_is_valid_device_data(uint8_t dev_id)
837 {
838 	if (dev_id >= RTE_CRYPTO_MAX_DEVS ||
839 			rte_crypto_devices[dev_id].data == NULL)
840 		return 0;
841 
842 	return 1;
843 }
844 
845 unsigned int
846 rte_cryptodev_is_valid_dev(uint8_t dev_id)
847 {
848 	struct rte_cryptodev *dev = NULL;
849 	unsigned int ret = 1;
850 
851 	if (!rte_cryptodev_is_valid_device_data(dev_id)) {
852 		ret = 0;
853 		goto done;
854 	}
855 
856 	dev = rte_cryptodev_pmd_get_dev(dev_id);
857 	if (dev->attached != RTE_CRYPTODEV_ATTACHED)
858 		ret = 0;
859 
860 done:
861 	rte_cryptodev_trace_is_valid_dev(dev_id, ret);
862 
863 	return ret;
864 }
865 
866 int
867 rte_cryptodev_get_dev_id(const char *name)
868 {
869 	unsigned i;
870 	int ret = -1;
871 
872 	if (name == NULL)
873 		return -1;
874 
875 	for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
876 		if (!rte_cryptodev_is_valid_device_data(i))
877 			continue;
878 		if ((strcmp(cryptodev_globals.devs[i].data->name, name)
879 				== 0) &&
880 				(cryptodev_globals.devs[i].attached ==
881 						RTE_CRYPTODEV_ATTACHED)) {
882 			ret = (int)i;
883 			break;
884 		}
885 	}
886 
887 	rte_cryptodev_trace_get_dev_id(name, ret);
888 
889 	return ret;
890 }
891 
892 uint8_t
893 rte_cryptodev_count(void)
894 {
895 	rte_cryptodev_trace_count(cryptodev_globals.nb_devs);
896 
897 	return cryptodev_globals.nb_devs;
898 }
899 
900 uint8_t
901 rte_cryptodev_device_count_by_driver(uint8_t driver_id)
902 {
903 	uint8_t i, dev_count = 0;
904 
905 	for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++)
906 		if (cryptodev_globals.devs[i].driver_id == driver_id &&
907 			cryptodev_globals.devs[i].attached ==
908 					RTE_CRYPTODEV_ATTACHED)
909 			dev_count++;
910 
911 	rte_cryptodev_trace_device_count_by_driver(driver_id, dev_count);
912 
913 	return dev_count;
914 }
915 
916 uint8_t
917 rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
918 	uint8_t nb_devices)
919 {
920 	uint8_t i, count = 0;
921 	struct rte_cryptodev *devs = cryptodev_globals.devs;
922 
923 	for (i = 0; i < RTE_CRYPTO_MAX_DEVS && count < nb_devices; i++) {
924 		if (!rte_cryptodev_is_valid_device_data(i))
925 			continue;
926 
927 		if (devs[i].attached == RTE_CRYPTODEV_ATTACHED) {
928 			int cmp;
929 
930 			cmp = strncmp(devs[i].device->driver->name,
931 					driver_name,
932 					strlen(driver_name) + 1);
933 
934 			if (cmp == 0)
935 				devices[count++] = devs[i].data->dev_id;
936 		}
937 	}
938 
939 	rte_cryptodev_trace_devices_get(driver_name, count);
940 
941 	return count;
942 }
943 
944 void *
945 rte_cryptodev_get_sec_ctx(uint8_t dev_id)
946 {
947 	void *sec_ctx = NULL;
948 
949 	if (dev_id < RTE_CRYPTO_MAX_DEVS &&
950 			(rte_crypto_devices[dev_id].feature_flags &
951 			RTE_CRYPTODEV_FF_SECURITY))
952 		sec_ctx = rte_crypto_devices[dev_id].security_ctx;
953 
954 	rte_cryptodev_trace_get_sec_ctx(dev_id, sec_ctx);
955 
956 	return sec_ctx;
957 }
958 
959 int
960 rte_cryptodev_socket_id(uint8_t dev_id)
961 {
962 	struct rte_cryptodev *dev;
963 
964 	if (!rte_cryptodev_is_valid_dev(dev_id))
965 		return -1;
966 
967 	dev = rte_cryptodev_pmd_get_dev(dev_id);
968 
969 	rte_cryptodev_trace_socket_id(dev_id, dev->data->name,
970 		dev->data->socket_id);
971 	return dev->data->socket_id;
972 }
973 
974 static inline int
975 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
976 		int socket_id)
977 {
978 	char mz_name[RTE_MEMZONE_NAMESIZE];
979 	const struct rte_memzone *mz;
980 	int n;
981 
982 	/* generate memzone name */
983 	n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
984 	if (n >= (int)sizeof(mz_name))
985 		return -EINVAL;
986 
987 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
988 		mz = rte_memzone_reserve(mz_name,
989 				sizeof(struct rte_cryptodev_data),
990 				socket_id, 0);
991 		CDEV_LOG_DEBUG("PRIMARY:reserved memzone for %s (%p)",
992 				mz_name, mz);
993 	} else {
994 		mz = rte_memzone_lookup(mz_name);
995 		CDEV_LOG_DEBUG("SECONDARY:looked up memzone for %s (%p)",
996 				mz_name, mz);
997 	}
998 
999 	if (mz == NULL)
1000 		return -ENOMEM;
1001 
1002 	*data = mz->addr;
1003 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1004 		memset(*data, 0, sizeof(struct rte_cryptodev_data));
1005 
1006 	return 0;
1007 }
1008 
1009 static inline int
1010 rte_cryptodev_data_free(uint8_t dev_id, struct rte_cryptodev_data **data)
1011 {
1012 	char mz_name[RTE_MEMZONE_NAMESIZE];
1013 	const struct rte_memzone *mz;
1014 	int n;
1015 
1016 	/* generate memzone name */
1017 	n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
1018 	if (n >= (int)sizeof(mz_name))
1019 		return -EINVAL;
1020 
1021 	mz = rte_memzone_lookup(mz_name);
1022 	if (mz == NULL)
1023 		return -ENOMEM;
1024 
1025 	RTE_ASSERT(*data == mz->addr);
1026 	*data = NULL;
1027 
1028 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1029 		CDEV_LOG_DEBUG("PRIMARY:free memzone of %s (%p)",
1030 				mz_name, mz);
1031 		return rte_memzone_free(mz);
1032 	} else {
1033 		CDEV_LOG_DEBUG("SECONDARY:don't free memzone of %s (%p)",
1034 				mz_name, mz);
1035 	}
1036 
1037 	return 0;
1038 }
1039 
1040 static uint8_t
1041 rte_cryptodev_find_free_device_index(void)
1042 {
1043 	uint8_t dev_id;
1044 
1045 	for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
1046 		if (rte_crypto_devices[dev_id].attached ==
1047 				RTE_CRYPTODEV_DETACHED)
1048 			return dev_id;
1049 	}
1050 	return RTE_CRYPTO_MAX_DEVS;
1051 }
1052 
1053 struct rte_cryptodev *
1054 rte_cryptodev_pmd_allocate(const char *name, int socket_id)
1055 {
1056 	struct rte_cryptodev *cryptodev;
1057 	uint8_t dev_id;
1058 
1059 	if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
1060 		CDEV_LOG_ERR("Crypto device with name %s already "
1061 				"allocated!", name);
1062 		return NULL;
1063 	}
1064 
1065 	dev_id = rte_cryptodev_find_free_device_index();
1066 	if (dev_id == RTE_CRYPTO_MAX_DEVS) {
1067 		CDEV_LOG_ERR("Reached maximum number of crypto devices");
1068 		return NULL;
1069 	}
1070 
1071 	cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
1072 
1073 	if (cryptodev->data == NULL) {
1074 		struct rte_cryptodev_data **cryptodev_data =
1075 				&cryptodev_globals.data[dev_id];
1076 
1077 		int retval = rte_cryptodev_data_alloc(dev_id, cryptodev_data,
1078 				socket_id);
1079 
1080 		if (retval < 0 || *cryptodev_data == NULL)
1081 			return NULL;
1082 
1083 		cryptodev->data = *cryptodev_data;
1084 
1085 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1086 			strlcpy(cryptodev->data->name, name,
1087 				RTE_CRYPTODEV_NAME_MAX_LEN);
1088 
1089 			cryptodev->data->dev_id = dev_id;
1090 			cryptodev->data->socket_id = socket_id;
1091 			cryptodev->data->dev_started = 0;
1092 			CDEV_LOG_DEBUG("PRIMARY:init data");
1093 		}
1094 
1095 		CDEV_LOG_DEBUG("Data for %s: dev_id %d, socket %d, started %d",
1096 				cryptodev->data->name,
1097 				cryptodev->data->dev_id,
1098 				cryptodev->data->socket_id,
1099 				cryptodev->data->dev_started);
1100 
1101 		/* init user callbacks */
1102 		TAILQ_INIT(&(cryptodev->link_intr_cbs));
1103 
1104 		cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
1105 
1106 		cryptodev_globals.nb_devs++;
1107 	}
1108 
1109 	return cryptodev;
1110 }
1111 
1112 int
1113 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
1114 {
1115 	int ret;
1116 	uint8_t dev_id;
1117 
1118 	if (cryptodev == NULL)
1119 		return -EINVAL;
1120 
1121 	dev_id = cryptodev->data->dev_id;
1122 
1123 	cryptodev_fp_ops_reset(rte_crypto_fp_ops + dev_id);
1124 
1125 	/* Close device only if device operations have been set */
1126 	if (cryptodev->dev_ops) {
1127 		ret = rte_cryptodev_close(dev_id);
1128 		if (ret < 0)
1129 			return ret;
1130 	}
1131 
1132 	ret = rte_cryptodev_data_free(dev_id, &cryptodev_globals.data[dev_id]);
1133 	if (ret < 0)
1134 		return ret;
1135 
1136 	cryptodev->attached = RTE_CRYPTODEV_DETACHED;
1137 	cryptodev_globals.nb_devs--;
1138 	return 0;
1139 }
1140 
1141 uint16_t
1142 rte_cryptodev_queue_pair_count(uint8_t dev_id)
1143 {
1144 	struct rte_cryptodev *dev;
1145 
1146 	if (!rte_cryptodev_is_valid_device_data(dev_id)) {
1147 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1148 		return 0;
1149 	}
1150 
1151 	dev = &rte_crypto_devices[dev_id];
1152 	rte_cryptodev_trace_queue_pair_count(dev, dev->data->name,
1153 		dev->data->socket_id, dev->data->dev_id,
1154 		dev->data->nb_queue_pairs);
1155 
1156 	return dev->data->nb_queue_pairs;
1157 }
1158 
1159 static int
1160 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
1161 		int socket_id)
1162 {
1163 	struct rte_cryptodev_info dev_info;
1164 	void **qp;
1165 	unsigned i;
1166 
1167 	if ((dev == NULL) || (nb_qpairs < 1)) {
1168 		CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
1169 							dev, nb_qpairs);
1170 		return -EINVAL;
1171 	}
1172 
1173 	CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
1174 			nb_qpairs, dev->data->dev_id);
1175 
1176 	memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
1177 
1178 	if (*dev->dev_ops->dev_infos_get == NULL)
1179 		return -ENOTSUP;
1180 	(*dev->dev_ops->dev_infos_get)(dev, &dev_info);
1181 
1182 	if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
1183 		CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
1184 				nb_qpairs, dev->data->dev_id);
1185 	    return -EINVAL;
1186 	}
1187 
1188 	if (dev->data->queue_pairs == NULL) { /* first time configuration */
1189 		dev->data->queue_pairs = rte_zmalloc_socket(
1190 				"cryptodev->queue_pairs",
1191 				sizeof(dev->data->queue_pairs[0]) *
1192 				dev_info.max_nb_queue_pairs,
1193 				RTE_CACHE_LINE_SIZE, socket_id);
1194 
1195 		if (dev->data->queue_pairs == NULL) {
1196 			dev->data->nb_queue_pairs = 0;
1197 			CDEV_LOG_ERR("failed to get memory for qp meta data, "
1198 							"nb_queues %u",
1199 							nb_qpairs);
1200 			return -(ENOMEM);
1201 		}
1202 	} else { /* re-configure */
1203 		int ret;
1204 		uint16_t old_nb_queues = dev->data->nb_queue_pairs;
1205 
1206 		qp = dev->data->queue_pairs;
1207 
1208 		if (*dev->dev_ops->queue_pair_release == NULL)
1209 			return -ENOTSUP;
1210 
1211 		for (i = nb_qpairs; i < old_nb_queues; i++) {
1212 			ret = (*dev->dev_ops->queue_pair_release)(dev, i);
1213 			if (ret < 0)
1214 				return ret;
1215 			qp[i] = NULL;
1216 		}
1217 
1218 	}
1219 	dev->data->nb_queue_pairs = nb_qpairs;
1220 	return 0;
1221 }
1222 
1223 int
1224 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
1225 {
1226 	struct rte_cryptodev *dev;
1227 	int diag;
1228 
1229 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1230 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1231 		return -EINVAL;
1232 	}
1233 
1234 	dev = &rte_crypto_devices[dev_id];
1235 
1236 	if (dev->data->dev_started) {
1237 		CDEV_LOG_ERR(
1238 		    "device %d must be stopped to allow configuration", dev_id);
1239 		return -EBUSY;
1240 	}
1241 
1242 	if (*dev->dev_ops->dev_configure == NULL)
1243 		return -ENOTSUP;
1244 
1245 	rte_spinlock_lock(&rte_cryptodev_callback_lock);
1246 	cryptodev_cb_cleanup(dev);
1247 	rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1248 
1249 	/* Setup new number of queue pairs and reconfigure device. */
1250 	diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
1251 			config->socket_id);
1252 	if (diag != 0) {
1253 		CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
1254 				dev_id, diag);
1255 		return diag;
1256 	}
1257 
1258 	rte_spinlock_lock(&rte_cryptodev_callback_lock);
1259 	diag = cryptodev_cb_init(dev);
1260 	rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1261 	if (diag) {
1262 		CDEV_LOG_ERR("Callback init failed for dev_id=%d", dev_id);
1263 		return diag;
1264 	}
1265 
1266 	rte_cryptodev_trace_configure(dev_id, config);
1267 	return (*dev->dev_ops->dev_configure)(dev, config);
1268 }
1269 
1270 int
1271 rte_cryptodev_start(uint8_t dev_id)
1272 {
1273 	struct rte_cryptodev *dev;
1274 	int diag;
1275 
1276 	CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
1277 
1278 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1279 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1280 		return -EINVAL;
1281 	}
1282 
1283 	dev = &rte_crypto_devices[dev_id];
1284 
1285 	if (*dev->dev_ops->dev_start == NULL)
1286 		return -ENOTSUP;
1287 
1288 	if (dev->data->dev_started != 0) {
1289 		CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
1290 			dev_id);
1291 		return 0;
1292 	}
1293 
1294 	diag = (*dev->dev_ops->dev_start)(dev);
1295 	/* expose selection of PMD fast-path functions */
1296 	cryptodev_fp_ops_set(rte_crypto_fp_ops + dev_id, dev);
1297 
1298 	rte_cryptodev_trace_start(dev_id, diag);
1299 	if (diag == 0)
1300 		dev->data->dev_started = 1;
1301 	else
1302 		return diag;
1303 
1304 	return 0;
1305 }
1306 
1307 void
1308 rte_cryptodev_stop(uint8_t dev_id)
1309 {
1310 	struct rte_cryptodev *dev;
1311 
1312 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1313 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1314 		return;
1315 	}
1316 
1317 	dev = &rte_crypto_devices[dev_id];
1318 
1319 	if (*dev->dev_ops->dev_stop == NULL)
1320 		return;
1321 
1322 	if (dev->data->dev_started == 0) {
1323 		CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
1324 			dev_id);
1325 		return;
1326 	}
1327 
1328 	/* point fast-path functions to dummy ones */
1329 	cryptodev_fp_ops_reset(rte_crypto_fp_ops + dev_id);
1330 
1331 	(*dev->dev_ops->dev_stop)(dev);
1332 	rte_cryptodev_trace_stop(dev_id);
1333 	dev->data->dev_started = 0;
1334 }
1335 
1336 int
1337 rte_cryptodev_close(uint8_t dev_id)
1338 {
1339 	struct rte_cryptodev *dev;
1340 	int retval;
1341 
1342 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1343 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1344 		return -1;
1345 	}
1346 
1347 	dev = &rte_crypto_devices[dev_id];
1348 
1349 	/* Device must be stopped before it can be closed */
1350 	if (dev->data->dev_started == 1) {
1351 		CDEV_LOG_ERR("Device %u must be stopped before closing",
1352 				dev_id);
1353 		return -EBUSY;
1354 	}
1355 
1356 	/* We can't close the device if there are outstanding sessions in use */
1357 	if (dev->data->session_pool != NULL) {
1358 		if (!rte_mempool_full(dev->data->session_pool)) {
1359 			CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
1360 					"has sessions still in use, free "
1361 					"all sessions before calling close",
1362 					(unsigned)dev_id);
1363 			return -EBUSY;
1364 		}
1365 	}
1366 
1367 	if (*dev->dev_ops->dev_close == NULL)
1368 		return -ENOTSUP;
1369 	retval = (*dev->dev_ops->dev_close)(dev);
1370 	rte_cryptodev_trace_close(dev_id, retval);
1371 
1372 	if (retval < 0)
1373 		return retval;
1374 
1375 	return 0;
1376 }
1377 
1378 int
1379 rte_cryptodev_get_qp_status(uint8_t dev_id, uint16_t queue_pair_id)
1380 {
1381 	struct rte_cryptodev *dev;
1382 	int ret = 0;
1383 
1384 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1385 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1386 		ret = -EINVAL;
1387 		goto done;
1388 	}
1389 
1390 	dev = &rte_crypto_devices[dev_id];
1391 	if (queue_pair_id >= dev->data->nb_queue_pairs) {
1392 		CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1393 		ret = -EINVAL;
1394 		goto done;
1395 	}
1396 	void **qps = dev->data->queue_pairs;
1397 
1398 	if (qps[queue_pair_id])	{
1399 		CDEV_LOG_DEBUG("qp %d on dev %d is initialised",
1400 			queue_pair_id, dev_id);
1401 		ret = 1;
1402 		goto done;
1403 	}
1404 
1405 	CDEV_LOG_DEBUG("qp %d on dev %d is not initialised",
1406 		queue_pair_id, dev_id);
1407 
1408 done:
1409 	rte_cryptodev_trace_get_qp_status(dev_id, queue_pair_id, ret);
1410 
1411 	return ret;
1412 }
1413 
1414 static uint8_t
1415 rte_cryptodev_sym_is_valid_session_pool(struct rte_mempool *mp,
1416 	uint32_t sess_priv_size)
1417 {
1418 	struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1419 
1420 	if (!mp)
1421 		return 0;
1422 
1423 	pool_priv = rte_mempool_get_priv(mp);
1424 
1425 	if (!pool_priv || mp->private_data_size < sizeof(*pool_priv) ||
1426 			pool_priv->sess_data_sz < sess_priv_size)
1427 		return 0;
1428 
1429 	return 1;
1430 }
1431 
1432 int
1433 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
1434 		const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
1435 
1436 {
1437 	struct rte_cryptodev *dev;
1438 
1439 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1440 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1441 		return -EINVAL;
1442 	}
1443 
1444 	dev = &rte_crypto_devices[dev_id];
1445 	if (queue_pair_id >= dev->data->nb_queue_pairs) {
1446 		CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1447 		return -EINVAL;
1448 	}
1449 
1450 	if (!qp_conf) {
1451 		CDEV_LOG_ERR("qp_conf cannot be NULL");
1452 		return -EINVAL;
1453 	}
1454 
1455 	if (qp_conf->mp_session) {
1456 		struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1457 
1458 		pool_priv = rte_mempool_get_priv(qp_conf->mp_session);
1459 		if (!pool_priv || qp_conf->mp_session->private_data_size <
1460 				sizeof(*pool_priv)) {
1461 			CDEV_LOG_ERR("Invalid mempool");
1462 			return -EINVAL;
1463 		}
1464 
1465 		if (!rte_cryptodev_sym_is_valid_session_pool(qp_conf->mp_session,
1466 					rte_cryptodev_sym_get_private_session_size(dev_id))) {
1467 			CDEV_LOG_ERR("Invalid mempool");
1468 			return -EINVAL;
1469 		}
1470 	}
1471 
1472 	if (dev->data->dev_started) {
1473 		CDEV_LOG_ERR(
1474 		    "device %d must be stopped to allow configuration", dev_id);
1475 		return -EBUSY;
1476 	}
1477 
1478 	if (*dev->dev_ops->queue_pair_setup == NULL)
1479 		return -ENOTSUP;
1480 
1481 	rte_cryptodev_trace_queue_pair_setup(dev_id, queue_pair_id, qp_conf);
1482 	return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
1483 			socket_id);
1484 }
1485 
1486 struct rte_cryptodev_cb *
1487 rte_cryptodev_add_enq_callback(uint8_t dev_id,
1488 			       uint16_t qp_id,
1489 			       rte_cryptodev_callback_fn cb_fn,
1490 			       void *cb_arg)
1491 {
1492 	struct rte_cryptodev *dev;
1493 	struct rte_cryptodev_cb_rcu *list;
1494 	struct rte_cryptodev_cb *cb, *tail;
1495 
1496 	if (!cb_fn) {
1497 		CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
1498 		rte_errno = EINVAL;
1499 		return NULL;
1500 	}
1501 
1502 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1503 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1504 		rte_errno = ENODEV;
1505 		return NULL;
1506 	}
1507 
1508 	dev = &rte_crypto_devices[dev_id];
1509 	if (qp_id >= dev->data->nb_queue_pairs) {
1510 		CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1511 		rte_errno = ENODEV;
1512 		return NULL;
1513 	}
1514 
1515 	cb = rte_zmalloc(NULL, sizeof(*cb), 0);
1516 	if (cb == NULL) {
1517 		CDEV_LOG_ERR("Failed to allocate memory for callback on "
1518 			     "dev=%d, queue_pair_id=%d", dev_id, qp_id);
1519 		rte_errno = ENOMEM;
1520 		return NULL;
1521 	}
1522 
1523 	rte_spinlock_lock(&rte_cryptodev_callback_lock);
1524 
1525 	cb->fn = cb_fn;
1526 	cb->arg = cb_arg;
1527 
1528 	/* Add the callbacks in fifo order. */
1529 	list = &dev->enq_cbs[qp_id];
1530 	tail = list->next;
1531 
1532 	if (tail) {
1533 		while (tail->next)
1534 			tail = tail->next;
1535 		/* Stores to cb->fn and cb->param should complete before
1536 		 * cb is visible to data plane.
1537 		 */
1538 		__atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
1539 	} else {
1540 		/* Stores to cb->fn and cb->param should complete before
1541 		 * cb is visible to data plane.
1542 		 */
1543 		__atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
1544 	}
1545 
1546 	rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1547 
1548 	rte_cryptodev_trace_add_enq_callback(dev_id, qp_id, cb_fn);
1549 	return cb;
1550 }
1551 
1552 int
1553 rte_cryptodev_remove_enq_callback(uint8_t dev_id,
1554 				  uint16_t qp_id,
1555 				  struct rte_cryptodev_cb *cb)
1556 {
1557 	struct rte_cryptodev *dev;
1558 	struct rte_cryptodev_cb **prev_cb, *curr_cb;
1559 	struct rte_cryptodev_cb_rcu *list;
1560 	int ret;
1561 
1562 	ret = -EINVAL;
1563 
1564 	if (!cb) {
1565 		CDEV_LOG_ERR("Callback is NULL");
1566 		return -EINVAL;
1567 	}
1568 
1569 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1570 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1571 		return -ENODEV;
1572 	}
1573 
1574 	rte_cryptodev_trace_remove_enq_callback(dev_id, qp_id, cb->fn);
1575 
1576 	dev = &rte_crypto_devices[dev_id];
1577 	if (qp_id >= dev->data->nb_queue_pairs) {
1578 		CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1579 		return -ENODEV;
1580 	}
1581 
1582 	rte_spinlock_lock(&rte_cryptodev_callback_lock);
1583 	if (dev->enq_cbs == NULL) {
1584 		CDEV_LOG_ERR("Callback not initialized");
1585 		goto cb_err;
1586 	}
1587 
1588 	list = &dev->enq_cbs[qp_id];
1589 	if (list == NULL) {
1590 		CDEV_LOG_ERR("Callback list is NULL");
1591 		goto cb_err;
1592 	}
1593 
1594 	if (list->qsbr == NULL) {
1595 		CDEV_LOG_ERR("Rcu qsbr is NULL");
1596 		goto cb_err;
1597 	}
1598 
1599 	prev_cb = &list->next;
1600 	for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1601 		curr_cb = *prev_cb;
1602 		if (curr_cb == cb) {
1603 			/* Remove the user cb from the callback list. */
1604 			__atomic_store_n(prev_cb, curr_cb->next,
1605 				__ATOMIC_RELAXED);
1606 			ret = 0;
1607 			break;
1608 		}
1609 	}
1610 
1611 	if (!ret) {
1612 		/* Call sync with invalid thread id as this is part of
1613 		 * control plane API
1614 		 */
1615 		rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1616 		rte_free(cb);
1617 	}
1618 
1619 cb_err:
1620 	rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1621 	return ret;
1622 }
1623 
1624 struct rte_cryptodev_cb *
1625 rte_cryptodev_add_deq_callback(uint8_t dev_id,
1626 			       uint16_t qp_id,
1627 			       rte_cryptodev_callback_fn cb_fn,
1628 			       void *cb_arg)
1629 {
1630 	struct rte_cryptodev *dev;
1631 	struct rte_cryptodev_cb_rcu *list;
1632 	struct rte_cryptodev_cb *cb, *tail;
1633 
1634 	if (!cb_fn) {
1635 		CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
1636 		rte_errno = EINVAL;
1637 		return NULL;
1638 	}
1639 
1640 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1641 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1642 		rte_errno = ENODEV;
1643 		return NULL;
1644 	}
1645 
1646 	dev = &rte_crypto_devices[dev_id];
1647 	if (qp_id >= dev->data->nb_queue_pairs) {
1648 		CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1649 		rte_errno = ENODEV;
1650 		return NULL;
1651 	}
1652 
1653 	cb = rte_zmalloc(NULL, sizeof(*cb), 0);
1654 	if (cb == NULL) {
1655 		CDEV_LOG_ERR("Failed to allocate memory for callback on "
1656 			     "dev=%d, queue_pair_id=%d", dev_id, qp_id);
1657 		rte_errno = ENOMEM;
1658 		return NULL;
1659 	}
1660 
1661 	rte_spinlock_lock(&rte_cryptodev_callback_lock);
1662 
1663 	cb->fn = cb_fn;
1664 	cb->arg = cb_arg;
1665 
1666 	/* Add the callbacks in fifo order. */
1667 	list = &dev->deq_cbs[qp_id];
1668 	tail = list->next;
1669 
1670 	if (tail) {
1671 		while (tail->next)
1672 			tail = tail->next;
1673 		/* Stores to cb->fn and cb->param should complete before
1674 		 * cb is visible to data plane.
1675 		 */
1676 		__atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
1677 	} else {
1678 		/* Stores to cb->fn and cb->param should complete before
1679 		 * cb is visible to data plane.
1680 		 */
1681 		__atomic_store_n(&list->next, cb, __ATOMIC_RELEASE);
1682 	}
1683 
1684 	rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1685 
1686 	rte_cryptodev_trace_add_deq_callback(dev_id, qp_id, cb_fn);
1687 
1688 	return cb;
1689 }
1690 
1691 int
1692 rte_cryptodev_remove_deq_callback(uint8_t dev_id,
1693 				  uint16_t qp_id,
1694 				  struct rte_cryptodev_cb *cb)
1695 {
1696 	struct rte_cryptodev *dev;
1697 	struct rte_cryptodev_cb **prev_cb, *curr_cb;
1698 	struct rte_cryptodev_cb_rcu *list;
1699 	int ret;
1700 
1701 	ret = -EINVAL;
1702 
1703 	if (!cb) {
1704 		CDEV_LOG_ERR("Callback is NULL");
1705 		return -EINVAL;
1706 	}
1707 
1708 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1709 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1710 		return -ENODEV;
1711 	}
1712 
1713 	rte_cryptodev_trace_remove_deq_callback(dev_id, qp_id, cb->fn);
1714 
1715 	dev = &rte_crypto_devices[dev_id];
1716 	if (qp_id >= dev->data->nb_queue_pairs) {
1717 		CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1718 		return -ENODEV;
1719 	}
1720 
1721 	rte_spinlock_lock(&rte_cryptodev_callback_lock);
1722 	if (dev->enq_cbs == NULL) {
1723 		CDEV_LOG_ERR("Callback not initialized");
1724 		goto cb_err;
1725 	}
1726 
1727 	list = &dev->deq_cbs[qp_id];
1728 	if (list == NULL) {
1729 		CDEV_LOG_ERR("Callback list is NULL");
1730 		goto cb_err;
1731 	}
1732 
1733 	if (list->qsbr == NULL) {
1734 		CDEV_LOG_ERR("Rcu qsbr is NULL");
1735 		goto cb_err;
1736 	}
1737 
1738 	prev_cb = &list->next;
1739 	for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1740 		curr_cb = *prev_cb;
1741 		if (curr_cb == cb) {
1742 			/* Remove the user cb from the callback list. */
1743 			__atomic_store_n(prev_cb, curr_cb->next,
1744 				__ATOMIC_RELAXED);
1745 			ret = 0;
1746 			break;
1747 		}
1748 	}
1749 
1750 	if (!ret) {
1751 		/* Call sync with invalid thread id as this is part of
1752 		 * control plane API
1753 		 */
1754 		rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1755 		rte_free(cb);
1756 	}
1757 
1758 cb_err:
1759 	rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1760 	return ret;
1761 }
1762 
1763 int
1764 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
1765 {
1766 	struct rte_cryptodev *dev;
1767 
1768 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1769 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1770 		return -ENODEV;
1771 	}
1772 
1773 	if (stats == NULL) {
1774 		CDEV_LOG_ERR("Invalid stats ptr");
1775 		return -EINVAL;
1776 	}
1777 
1778 	dev = &rte_crypto_devices[dev_id];
1779 	memset(stats, 0, sizeof(*stats));
1780 
1781 	if (*dev->dev_ops->stats_get == NULL)
1782 		return -ENOTSUP;
1783 	(*dev->dev_ops->stats_get)(dev, stats);
1784 
1785 	rte_cryptodev_trace_stats_get(dev_id, stats);
1786 	return 0;
1787 }
1788 
1789 void
1790 rte_cryptodev_stats_reset(uint8_t dev_id)
1791 {
1792 	struct rte_cryptodev *dev;
1793 
1794 	rte_cryptodev_trace_stats_reset(dev_id);
1795 
1796 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1797 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1798 		return;
1799 	}
1800 
1801 	dev = &rte_crypto_devices[dev_id];
1802 
1803 	if (*dev->dev_ops->stats_reset == NULL)
1804 		return;
1805 	(*dev->dev_ops->stats_reset)(dev);
1806 }
1807 
1808 void
1809 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
1810 {
1811 	struct rte_cryptodev *dev;
1812 
1813 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1814 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1815 		return;
1816 	}
1817 
1818 	dev = &rte_crypto_devices[dev_id];
1819 
1820 	memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
1821 
1822 	if (*dev->dev_ops->dev_infos_get == NULL)
1823 		return;
1824 	(*dev->dev_ops->dev_infos_get)(dev, dev_info);
1825 
1826 	dev_info->driver_name = dev->device->driver->name;
1827 	dev_info->device = dev->device;
1828 
1829 	rte_cryptodev_trace_info_get(dev_id, dev_info->driver_name);
1830 
1831 }
1832 
1833 int
1834 rte_cryptodev_callback_register(uint8_t dev_id,
1835 			enum rte_cryptodev_event_type event,
1836 			rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1837 {
1838 	struct rte_cryptodev *dev;
1839 	struct rte_cryptodev_callback *user_cb;
1840 
1841 	if (!cb_fn)
1842 		return -EINVAL;
1843 
1844 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1845 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1846 		return -EINVAL;
1847 	}
1848 
1849 	dev = &rte_crypto_devices[dev_id];
1850 	rte_spinlock_lock(&rte_cryptodev_cb_lock);
1851 
1852 	TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
1853 		if (user_cb->cb_fn == cb_fn &&
1854 			user_cb->cb_arg == cb_arg &&
1855 			user_cb->event == event) {
1856 			break;
1857 		}
1858 	}
1859 
1860 	/* create a new callback. */
1861 	if (user_cb == NULL) {
1862 		user_cb = rte_zmalloc("INTR_USER_CALLBACK",
1863 				sizeof(struct rte_cryptodev_callback), 0);
1864 		if (user_cb != NULL) {
1865 			user_cb->cb_fn = cb_fn;
1866 			user_cb->cb_arg = cb_arg;
1867 			user_cb->event = event;
1868 			TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
1869 		}
1870 	}
1871 
1872 	rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1873 
1874 	rte_cryptodev_trace_callback_register(dev_id, event, cb_fn);
1875 	return (user_cb == NULL) ? -ENOMEM : 0;
1876 }
1877 
1878 int
1879 rte_cryptodev_callback_unregister(uint8_t dev_id,
1880 			enum rte_cryptodev_event_type event,
1881 			rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1882 {
1883 	int ret;
1884 	struct rte_cryptodev *dev;
1885 	struct rte_cryptodev_callback *cb, *next;
1886 
1887 	if (!cb_fn)
1888 		return -EINVAL;
1889 
1890 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1891 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1892 		return -EINVAL;
1893 	}
1894 
1895 	dev = &rte_crypto_devices[dev_id];
1896 	rte_spinlock_lock(&rte_cryptodev_cb_lock);
1897 
1898 	ret = 0;
1899 	for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
1900 
1901 		next = TAILQ_NEXT(cb, next);
1902 
1903 		if (cb->cb_fn != cb_fn || cb->event != event ||
1904 				(cb->cb_arg != (void *)-1 &&
1905 				cb->cb_arg != cb_arg))
1906 			continue;
1907 
1908 		/*
1909 		 * if this callback is not executing right now,
1910 		 * then remove it.
1911 		 */
1912 		if (cb->active == 0) {
1913 			TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
1914 			rte_free(cb);
1915 		} else {
1916 			ret = -EAGAIN;
1917 		}
1918 	}
1919 
1920 	rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1921 
1922 	rte_cryptodev_trace_callback_unregister(dev_id, event, cb_fn);
1923 	return ret;
1924 }
1925 
1926 void
1927 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1928 	enum rte_cryptodev_event_type event)
1929 {
1930 	struct rte_cryptodev_callback *cb_lst;
1931 	struct rte_cryptodev_callback dev_cb;
1932 
1933 	rte_spinlock_lock(&rte_cryptodev_cb_lock);
1934 	TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1935 		if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1936 			continue;
1937 		dev_cb = *cb_lst;
1938 		cb_lst->active = 1;
1939 		rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1940 		dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1941 						dev_cb.cb_arg);
1942 		rte_spinlock_lock(&rte_cryptodev_cb_lock);
1943 		cb_lst->active = 0;
1944 	}
1945 	rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1946 }
1947 
1948 int
1949 rte_cryptodev_queue_pair_event_error_query(uint8_t dev_id, uint16_t qp_id)
1950 {
1951 	struct rte_cryptodev *dev;
1952 
1953 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1954 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1955 		return -EINVAL;
1956 	}
1957 	dev = &rte_crypto_devices[dev_id];
1958 
1959 	if (qp_id >= dev->data->nb_queue_pairs)
1960 		return -EINVAL;
1961 	if (*dev->dev_ops->queue_pair_event_error_query == NULL)
1962 		return -ENOTSUP;
1963 
1964 	return dev->dev_ops->queue_pair_event_error_query(dev, qp_id);
1965 }
1966 
1967 struct rte_mempool *
1968 rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
1969 	uint32_t elt_size, uint32_t cache_size, uint16_t user_data_size,
1970 	int socket_id)
1971 {
1972 	struct rte_mempool *mp;
1973 	struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1974 	uint32_t obj_sz;
1975 
1976 	obj_sz = sizeof(struct rte_cryptodev_sym_session) + elt_size + user_data_size;
1977 
1978 	obj_sz = RTE_ALIGN_CEIL(obj_sz, RTE_CACHE_LINE_SIZE);
1979 	mp = rte_mempool_create(name, nb_elts, obj_sz, cache_size,
1980 			(uint32_t)(sizeof(*pool_priv)), NULL, NULL,
1981 			NULL, NULL,
1982 			socket_id, 0);
1983 	if (mp == NULL) {
1984 		CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d",
1985 			__func__, name, rte_errno);
1986 		return NULL;
1987 	}
1988 
1989 	pool_priv = rte_mempool_get_priv(mp);
1990 	if (!pool_priv) {
1991 		CDEV_LOG_ERR("%s(name=%s) failed to get private data",
1992 			__func__, name);
1993 		rte_mempool_free(mp);
1994 		return NULL;
1995 	}
1996 
1997 	pool_priv->sess_data_sz = elt_size;
1998 	pool_priv->user_data_sz = user_data_size;
1999 
2000 	rte_cryptodev_trace_sym_session_pool_create(name, nb_elts,
2001 		elt_size, cache_size, user_data_size, mp);
2002 	return mp;
2003 }
2004 
2005 struct rte_mempool *
2006 rte_cryptodev_asym_session_pool_create(const char *name, uint32_t nb_elts,
2007 	uint32_t cache_size, uint16_t user_data_size, int socket_id)
2008 {
2009 	struct rte_mempool *mp;
2010 	struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
2011 	uint32_t obj_sz, obj_sz_aligned;
2012 	uint8_t dev_id;
2013 	unsigned int priv_sz, max_priv_sz = 0;
2014 
2015 	for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
2016 		if (rte_cryptodev_is_valid_dev(dev_id)) {
2017 			priv_sz = rte_cryptodev_asym_get_private_session_size(dev_id);
2018 			if (priv_sz > max_priv_sz)
2019 				max_priv_sz = priv_sz;
2020 		}
2021 	if (max_priv_sz == 0) {
2022 		CDEV_LOG_INFO("Could not set max private session size");
2023 		return NULL;
2024 	}
2025 
2026 	obj_sz = rte_cryptodev_asym_get_header_session_size() + max_priv_sz +
2027 			user_data_size;
2028 	obj_sz_aligned =  RTE_ALIGN_CEIL(obj_sz, RTE_CACHE_LINE_SIZE);
2029 
2030 	mp = rte_mempool_create(name, nb_elts, obj_sz_aligned, cache_size,
2031 			(uint32_t)(sizeof(*pool_priv)),
2032 			NULL, NULL, NULL, NULL,
2033 			socket_id, 0);
2034 	if (mp == NULL) {
2035 		CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d",
2036 			__func__, name, rte_errno);
2037 		return NULL;
2038 	}
2039 
2040 	pool_priv = rte_mempool_get_priv(mp);
2041 	if (!pool_priv) {
2042 		CDEV_LOG_ERR("%s(name=%s) failed to get private data",
2043 			__func__, name);
2044 		rte_mempool_free(mp);
2045 		return NULL;
2046 	}
2047 	pool_priv->max_priv_session_sz = max_priv_sz;
2048 	pool_priv->user_data_sz = user_data_size;
2049 
2050 	rte_cryptodev_trace_asym_session_pool_create(name, nb_elts,
2051 		user_data_size, cache_size, mp);
2052 	return mp;
2053 }
2054 
2055 void *
2056 rte_cryptodev_sym_session_create(uint8_t dev_id,
2057 		struct rte_crypto_sym_xform *xforms,
2058 		struct rte_mempool *mp)
2059 {
2060 	struct rte_cryptodev *dev;
2061 	struct rte_cryptodev_sym_session *sess;
2062 	struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
2063 	uint32_t sess_priv_sz;
2064 	int ret;
2065 
2066 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2067 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2068 		rte_errno = EINVAL;
2069 		return NULL;
2070 	}
2071 
2072 	if (xforms == NULL) {
2073 		CDEV_LOG_ERR("Invalid xform\n");
2074 		rte_errno = EINVAL;
2075 		return NULL;
2076 	}
2077 
2078 	sess_priv_sz = rte_cryptodev_sym_get_private_session_size(dev_id);
2079 	if (!rte_cryptodev_sym_is_valid_session_pool(mp, sess_priv_sz)) {
2080 		CDEV_LOG_ERR("Invalid mempool");
2081 		rte_errno = EINVAL;
2082 		return NULL;
2083 	}
2084 
2085 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2086 
2087 	/* Allocate a session structure from the session pool */
2088 	if (rte_mempool_get(mp, (void **)&sess)) {
2089 		CDEV_LOG_ERR("couldn't get object from session mempool");
2090 		rte_errno = ENOMEM;
2091 		return NULL;
2092 	}
2093 
2094 	pool_priv = rte_mempool_get_priv(mp);
2095 	sess->driver_id = dev->driver_id;
2096 	sess->sess_data_sz = pool_priv->sess_data_sz;
2097 	sess->user_data_sz = pool_priv->user_data_sz;
2098 	sess->driver_priv_data_iova = rte_mempool_virt2iova(sess) +
2099 		offsetof(struct rte_cryptodev_sym_session, driver_priv_data);
2100 
2101 	if (dev->dev_ops->sym_session_configure == NULL) {
2102 		rte_errno = ENOTSUP;
2103 		goto error_exit;
2104 	}
2105 	memset(sess->driver_priv_data, 0, pool_priv->sess_data_sz + pool_priv->user_data_sz);
2106 
2107 	ret = dev->dev_ops->sym_session_configure(dev, xforms, sess);
2108 	if (ret < 0) {
2109 		rte_errno = -ret;
2110 		goto error_exit;
2111 	}
2112 	sess->driver_id = dev->driver_id;
2113 
2114 	rte_cryptodev_trace_sym_session_create(dev_id, sess, xforms, mp);
2115 
2116 	return (void *)sess;
2117 error_exit:
2118 	rte_mempool_put(mp, (void *)sess);
2119 	return NULL;
2120 }
2121 
2122 int
2123 rte_cryptodev_asym_session_create(uint8_t dev_id,
2124 		struct rte_crypto_asym_xform *xforms, struct rte_mempool *mp,
2125 		void **session)
2126 {
2127 	struct rte_cryptodev_asym_session *sess;
2128 	uint32_t session_priv_data_sz;
2129 	struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
2130 	unsigned int session_header_size =
2131 			rte_cryptodev_asym_get_header_session_size();
2132 	struct rte_cryptodev *dev;
2133 	int ret;
2134 
2135 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2136 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2137 		return -EINVAL;
2138 	}
2139 
2140 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2141 
2142 	if (dev == NULL)
2143 		return -EINVAL;
2144 
2145 	if (!mp) {
2146 		CDEV_LOG_ERR("invalid mempool");
2147 		return -EINVAL;
2148 	}
2149 
2150 	session_priv_data_sz = rte_cryptodev_asym_get_private_session_size(
2151 			dev_id);
2152 	pool_priv = rte_mempool_get_priv(mp);
2153 
2154 	if (pool_priv->max_priv_session_sz < session_priv_data_sz) {
2155 		CDEV_LOG_DEBUG(
2156 			"The private session data size used when creating the mempool is smaller than this device's private session data.");
2157 		return -EINVAL;
2158 	}
2159 
2160 	/* Verify if provided mempool can hold elements big enough. */
2161 	if (mp->elt_size < session_header_size + session_priv_data_sz) {
2162 		CDEV_LOG_ERR(
2163 			"mempool elements too small to hold session objects");
2164 		return -EINVAL;
2165 	}
2166 
2167 	/* Allocate a session structure from the session pool */
2168 	if (rte_mempool_get(mp, session)) {
2169 		CDEV_LOG_ERR("couldn't get object from session mempool");
2170 		return -ENOMEM;
2171 	}
2172 
2173 	sess = *session;
2174 	sess->driver_id = dev->driver_id;
2175 	sess->user_data_sz = pool_priv->user_data_sz;
2176 	sess->max_priv_data_sz = pool_priv->max_priv_session_sz;
2177 
2178 	/* Clear device session pointer.*/
2179 	memset(sess->sess_private_data, 0, session_priv_data_sz + sess->user_data_sz);
2180 
2181 	if (*dev->dev_ops->asym_session_configure == NULL)
2182 		return -ENOTSUP;
2183 
2184 	if (sess->sess_private_data[0] == 0) {
2185 		ret = dev->dev_ops->asym_session_configure(dev, xforms, sess);
2186 		if (ret < 0) {
2187 			CDEV_LOG_ERR(
2188 				"dev_id %d failed to configure session details",
2189 				dev_id);
2190 			return ret;
2191 		}
2192 	}
2193 
2194 	rte_cryptodev_trace_asym_session_create(dev_id, xforms, mp, sess);
2195 	return 0;
2196 }
2197 
2198 int
2199 rte_cryptodev_sym_session_free(uint8_t dev_id, void *_sess)
2200 {
2201 	struct rte_cryptodev *dev;
2202 	struct rte_mempool *sess_mp;
2203 	struct rte_cryptodev_sym_session *sess = _sess;
2204 	struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
2205 
2206 	if (sess == NULL)
2207 		return -EINVAL;
2208 
2209 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2210 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2211 		return -EINVAL;
2212 	}
2213 
2214 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2215 
2216 	if (dev == NULL || sess == NULL)
2217 		return -EINVAL;
2218 
2219 	sess_mp = rte_mempool_from_obj(sess);
2220 	if (!sess_mp)
2221 		return -EINVAL;
2222 	pool_priv = rte_mempool_get_priv(sess_mp);
2223 
2224 	if (sess->driver_id != dev->driver_id) {
2225 		CDEV_LOG_ERR("Session created by driver %u but freed by %u",
2226 			sess->driver_id, dev->driver_id);
2227 		return -EINVAL;
2228 	}
2229 
2230 	if (*dev->dev_ops->sym_session_clear == NULL)
2231 		return -ENOTSUP;
2232 
2233 	dev->dev_ops->sym_session_clear(dev, sess);
2234 
2235 	memset(sess->driver_priv_data, 0, pool_priv->sess_data_sz + pool_priv->user_data_sz);
2236 
2237 	/* Return session to mempool */
2238 	rte_mempool_put(sess_mp, sess);
2239 
2240 	rte_cryptodev_trace_sym_session_free(dev_id, sess);
2241 	return 0;
2242 }
2243 
2244 int
2245 rte_cryptodev_asym_session_free(uint8_t dev_id, void *sess)
2246 {
2247 	struct rte_mempool *sess_mp;
2248 	struct rte_cryptodev *dev;
2249 
2250 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2251 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2252 		return -EINVAL;
2253 	}
2254 
2255 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2256 
2257 	if (dev == NULL || sess == NULL)
2258 		return -EINVAL;
2259 
2260 	if (*dev->dev_ops->asym_session_clear == NULL)
2261 		return -ENOTSUP;
2262 
2263 	dev->dev_ops->asym_session_clear(dev, sess);
2264 
2265 	rte_free(((struct rte_cryptodev_asym_session *)sess)->event_mdata);
2266 
2267 	/* Return session to mempool */
2268 	sess_mp = rte_mempool_from_obj(sess);
2269 	rte_mempool_put(sess_mp, sess);
2270 
2271 	rte_cryptodev_trace_asym_session_free(dev_id, sess);
2272 	return 0;
2273 }
2274 
2275 unsigned int
2276 rte_cryptodev_asym_get_header_session_size(void)
2277 {
2278 	return sizeof(struct rte_cryptodev_asym_session);
2279 }
2280 
2281 unsigned int
2282 rte_cryptodev_sym_get_private_session_size(uint8_t dev_id)
2283 {
2284 	struct rte_cryptodev *dev;
2285 	unsigned int priv_sess_size;
2286 
2287 	if (!rte_cryptodev_is_valid_dev(dev_id))
2288 		return 0;
2289 
2290 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2291 
2292 	if (*dev->dev_ops->sym_session_get_size == NULL)
2293 		return 0;
2294 
2295 	priv_sess_size = (*dev->dev_ops->sym_session_get_size)(dev);
2296 
2297 	rte_cryptodev_trace_sym_get_private_session_size(dev_id,
2298 		priv_sess_size);
2299 
2300 	return priv_sess_size;
2301 }
2302 
2303 unsigned int
2304 rte_cryptodev_asym_get_private_session_size(uint8_t dev_id)
2305 {
2306 	struct rte_cryptodev *dev;
2307 	unsigned int priv_sess_size;
2308 
2309 	if (!rte_cryptodev_is_valid_dev(dev_id))
2310 		return 0;
2311 
2312 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2313 
2314 	if (*dev->dev_ops->asym_session_get_size == NULL)
2315 		return 0;
2316 
2317 	priv_sess_size = (*dev->dev_ops->asym_session_get_size)(dev);
2318 
2319 	rte_cryptodev_trace_asym_get_private_session_size(dev_id,
2320 		priv_sess_size);
2321 
2322 	return priv_sess_size;
2323 }
2324 
2325 int
2326 rte_cryptodev_sym_session_set_user_data(void *_sess, void *data,
2327 		uint16_t size)
2328 {
2329 	struct rte_cryptodev_sym_session *sess = _sess;
2330 
2331 	if (sess == NULL)
2332 		return -EINVAL;
2333 
2334 	if (sess->user_data_sz < size)
2335 		return -ENOMEM;
2336 
2337 	rte_memcpy(sess->driver_priv_data + sess->sess_data_sz, data, size);
2338 
2339 	rte_cryptodev_trace_sym_session_set_user_data(sess, data, size);
2340 
2341 	return 0;
2342 }
2343 
2344 void *
2345 rte_cryptodev_sym_session_get_user_data(void *_sess)
2346 {
2347 	struct rte_cryptodev_sym_session *sess = _sess;
2348 	void *data = NULL;
2349 
2350 	if (sess == NULL || sess->user_data_sz == 0)
2351 		return NULL;
2352 
2353 	data = (void *)(sess->driver_priv_data + sess->sess_data_sz);
2354 
2355 	rte_cryptodev_trace_sym_session_get_user_data(sess, data);
2356 
2357 	return data;
2358 }
2359 
2360 int
2361 rte_cryptodev_asym_session_set_user_data(void *session, void *data, uint16_t size)
2362 {
2363 	struct rte_cryptodev_asym_session *sess = session;
2364 	if (sess == NULL)
2365 		return -EINVAL;
2366 
2367 	if (sess->user_data_sz < size)
2368 		return -ENOMEM;
2369 
2370 	rte_memcpy(sess->sess_private_data +
2371 			sess->max_priv_data_sz,
2372 			data, size);
2373 
2374 	rte_cryptodev_trace_asym_session_set_user_data(sess, data, size);
2375 
2376 	return 0;
2377 }
2378 
2379 void *
2380 rte_cryptodev_asym_session_get_user_data(void *session)
2381 {
2382 	struct rte_cryptodev_asym_session *sess = session;
2383 	void *data = NULL;
2384 
2385 	if (sess == NULL || sess->user_data_sz == 0)
2386 		return NULL;
2387 
2388 	data = (void *)(sess->sess_private_data + sess->max_priv_data_sz);
2389 
2390 	rte_cryptodev_trace_asym_session_get_user_data(sess, data);
2391 
2392 	return data;
2393 }
2394 
2395 static inline void
2396 sym_crypto_fill_status(struct rte_crypto_sym_vec *vec, int32_t errnum)
2397 {
2398 	uint32_t i;
2399 	for (i = 0; i < vec->num; i++)
2400 		vec->status[i] = errnum;
2401 }
2402 
2403 uint32_t
2404 rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
2405 	void *_sess, union rte_crypto_sym_ofs ofs,
2406 	struct rte_crypto_sym_vec *vec)
2407 {
2408 	struct rte_cryptodev *dev;
2409 	struct rte_cryptodev_sym_session *sess = _sess;
2410 
2411 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2412 		sym_crypto_fill_status(vec, EINVAL);
2413 		return 0;
2414 	}
2415 
2416 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2417 
2418 	if (*dev->dev_ops->sym_cpu_process == NULL ||
2419 		!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO)) {
2420 		sym_crypto_fill_status(vec, ENOTSUP);
2421 		return 0;
2422 	}
2423 
2424 	rte_cryptodev_trace_sym_cpu_crypto_process(dev_id, sess);
2425 
2426 	return dev->dev_ops->sym_cpu_process(dev, sess, ofs, vec);
2427 }
2428 
2429 int
2430 rte_cryptodev_get_raw_dp_ctx_size(uint8_t dev_id)
2431 {
2432 	struct rte_cryptodev *dev;
2433 	int32_t size = sizeof(struct rte_crypto_raw_dp_ctx);
2434 	int32_t priv_size;
2435 
2436 	if (!rte_cryptodev_is_valid_dev(dev_id))
2437 		return -EINVAL;
2438 
2439 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2440 
2441 	if (*dev->dev_ops->sym_get_raw_dp_ctx_size == NULL ||
2442 		!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)) {
2443 		return -ENOTSUP;
2444 	}
2445 
2446 	priv_size = (*dev->dev_ops->sym_get_raw_dp_ctx_size)(dev);
2447 	if (priv_size < 0)
2448 		return -ENOTSUP;
2449 
2450 	rte_cryptodev_trace_get_raw_dp_ctx_size(dev_id);
2451 
2452 	return RTE_ALIGN_CEIL((size + priv_size), 8);
2453 }
2454 
2455 int
2456 rte_cryptodev_configure_raw_dp_ctx(uint8_t dev_id, uint16_t qp_id,
2457 	struct rte_crypto_raw_dp_ctx *ctx,
2458 	enum rte_crypto_op_sess_type sess_type,
2459 	union rte_cryptodev_session_ctx session_ctx,
2460 	uint8_t is_update)
2461 {
2462 	struct rte_cryptodev *dev;
2463 
2464 	if (!rte_cryptodev_get_qp_status(dev_id, qp_id))
2465 		return -EINVAL;
2466 
2467 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2468 	if (!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)
2469 			|| dev->dev_ops->sym_configure_raw_dp_ctx == NULL)
2470 		return -ENOTSUP;
2471 
2472 	rte_cryptodev_trace_configure_raw_dp_ctx(dev_id, qp_id, sess_type);
2473 
2474 	return (*dev->dev_ops->sym_configure_raw_dp_ctx)(dev, qp_id, ctx,
2475 			sess_type, session_ctx, is_update);
2476 }
2477 
2478 int
2479 rte_cryptodev_session_event_mdata_set(uint8_t dev_id, void *sess,
2480 	enum rte_crypto_op_type op_type,
2481 	enum rte_crypto_op_sess_type sess_type,
2482 	void *ev_mdata,
2483 	uint16_t size)
2484 {
2485 	struct rte_cryptodev *dev;
2486 
2487 	if (sess == NULL || ev_mdata == NULL)
2488 		return -EINVAL;
2489 
2490 	if (!rte_cryptodev_is_valid_dev(dev_id))
2491 		goto skip_pmd_op;
2492 
2493 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2494 	if (dev->dev_ops->session_ev_mdata_set == NULL)
2495 		goto skip_pmd_op;
2496 
2497 	rte_cryptodev_trace_session_event_mdata_set(dev_id, sess, op_type,
2498 		sess_type, ev_mdata, size);
2499 
2500 	return (*dev->dev_ops->session_ev_mdata_set)(dev, sess, op_type,
2501 			sess_type, ev_mdata);
2502 
2503 skip_pmd_op:
2504 	if (op_type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
2505 		return rte_cryptodev_sym_session_set_user_data(sess, ev_mdata,
2506 				size);
2507 	else if (op_type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
2508 		struct rte_cryptodev_asym_session *s = sess;
2509 
2510 		if (s->event_mdata == NULL) {
2511 			s->event_mdata = rte_malloc(NULL, size, 0);
2512 			if (s->event_mdata == NULL)
2513 				return -ENOMEM;
2514 		}
2515 		rte_memcpy(s->event_mdata, ev_mdata, size);
2516 
2517 		return 0;
2518 	} else
2519 		return -ENOTSUP;
2520 }
2521 
2522 uint32_t
2523 rte_cryptodev_raw_enqueue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2524 	struct rte_crypto_sym_vec *vec, union rte_crypto_sym_ofs ofs,
2525 	void **user_data, int *enqueue_status)
2526 {
2527 	return (*ctx->enqueue_burst)(ctx->qp_data, ctx->drv_ctx_data, vec,
2528 			ofs, user_data, enqueue_status);
2529 }
2530 
2531 int
2532 rte_cryptodev_raw_enqueue_done(struct rte_crypto_raw_dp_ctx *ctx,
2533 		uint32_t n)
2534 {
2535 	return (*ctx->enqueue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2536 }
2537 
2538 uint32_t
2539 rte_cryptodev_raw_dequeue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2540 	rte_cryptodev_raw_get_dequeue_count_t get_dequeue_count,
2541 	uint32_t max_nb_to_dequeue,
2542 	rte_cryptodev_raw_post_dequeue_t post_dequeue,
2543 	void **out_user_data, uint8_t is_user_data_array,
2544 	uint32_t *n_success_jobs, int *status)
2545 {
2546 	return (*ctx->dequeue_burst)(ctx->qp_data, ctx->drv_ctx_data,
2547 		get_dequeue_count, max_nb_to_dequeue, post_dequeue,
2548 		out_user_data, is_user_data_array, n_success_jobs, status);
2549 }
2550 
2551 int
2552 rte_cryptodev_raw_dequeue_done(struct rte_crypto_raw_dp_ctx *ctx,
2553 		uint32_t n)
2554 {
2555 	return (*ctx->dequeue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2556 }
2557 
2558 /** Initialise rte_crypto_op mempool element */
2559 static void
2560 rte_crypto_op_init(struct rte_mempool *mempool,
2561 		void *opaque_arg,
2562 		void *_op_data,
2563 		__rte_unused unsigned i)
2564 {
2565 	struct rte_crypto_op *op = _op_data;
2566 	enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
2567 
2568 	memset(_op_data, 0, mempool->elt_size);
2569 
2570 	__rte_crypto_op_reset(op, type);
2571 
2572 	op->phys_addr = rte_mem_virt2iova(_op_data);
2573 	op->mempool = mempool;
2574 }
2575 
2576 
2577 struct rte_mempool *
2578 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
2579 		unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
2580 		int socket_id)
2581 {
2582 	struct rte_crypto_op_pool_private *priv;
2583 
2584 	unsigned elt_size = sizeof(struct rte_crypto_op) +
2585 			priv_size;
2586 
2587 	if (type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
2588 		elt_size += sizeof(struct rte_crypto_sym_op);
2589 	} else if (type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
2590 		elt_size += sizeof(struct rte_crypto_asym_op);
2591 	} else if (type == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
2592 		elt_size += RTE_MAX(sizeof(struct rte_crypto_sym_op),
2593 		                    sizeof(struct rte_crypto_asym_op));
2594 	} else {
2595 		CDEV_LOG_ERR("Invalid op_type");
2596 		return NULL;
2597 	}
2598 
2599 	/* lookup mempool in case already allocated */
2600 	struct rte_mempool *mp = rte_mempool_lookup(name);
2601 
2602 	if (mp != NULL) {
2603 		priv = (struct rte_crypto_op_pool_private *)
2604 				rte_mempool_get_priv(mp);
2605 
2606 		if (mp->elt_size != elt_size ||
2607 				mp->cache_size < cache_size ||
2608 				mp->size < nb_elts ||
2609 				priv->priv_size <  priv_size) {
2610 			mp = NULL;
2611 			CDEV_LOG_ERR("Mempool %s already exists but with "
2612 					"incompatible parameters", name);
2613 			return NULL;
2614 		}
2615 		return mp;
2616 	}
2617 
2618 	mp = rte_mempool_create(
2619 			name,
2620 			nb_elts,
2621 			elt_size,
2622 			cache_size,
2623 			sizeof(struct rte_crypto_op_pool_private),
2624 			NULL,
2625 			NULL,
2626 			rte_crypto_op_init,
2627 			&type,
2628 			socket_id,
2629 			0);
2630 
2631 	if (mp == NULL) {
2632 		CDEV_LOG_ERR("Failed to create mempool %s", name);
2633 		return NULL;
2634 	}
2635 
2636 	priv = (struct rte_crypto_op_pool_private *)
2637 			rte_mempool_get_priv(mp);
2638 
2639 	priv->priv_size = priv_size;
2640 	priv->type = type;
2641 
2642 	rte_cryptodev_trace_op_pool_create(name, socket_id, type, nb_elts, mp);
2643 	return mp;
2644 }
2645 
2646 int
2647 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix)
2648 {
2649 	struct rte_cryptodev *dev = NULL;
2650 	uint32_t i = 0;
2651 
2652 	if (name == NULL)
2653 		return -EINVAL;
2654 
2655 	for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
2656 		int ret = snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
2657 				"%s_%u", dev_name_prefix, i);
2658 
2659 		if (ret < 0)
2660 			return ret;
2661 
2662 		dev = rte_cryptodev_pmd_get_named_dev(name);
2663 		if (!dev)
2664 			return 0;
2665 	}
2666 
2667 	return -1;
2668 }
2669 
2670 TAILQ_HEAD(cryptodev_driver_list, cryptodev_driver);
2671 
2672 static struct cryptodev_driver_list cryptodev_driver_list =
2673 	TAILQ_HEAD_INITIALIZER(cryptodev_driver_list);
2674 
2675 int
2676 rte_cryptodev_driver_id_get(const char *name)
2677 {
2678 	struct cryptodev_driver *driver;
2679 	const char *driver_name;
2680 	int driver_id = -1;
2681 
2682 	if (name == NULL) {
2683 		RTE_LOG(DEBUG, CRYPTODEV, "name pointer NULL");
2684 		return -1;
2685 	}
2686 
2687 	TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
2688 		driver_name = driver->driver->name;
2689 		if (strncmp(driver_name, name, strlen(driver_name) + 1) == 0) {
2690 			driver_id = driver->id;
2691 			break;
2692 		}
2693 	}
2694 
2695 	rte_cryptodev_trace_driver_id_get(name, driver_id);
2696 
2697 	return driver_id;
2698 }
2699 
2700 const char *
2701 rte_cryptodev_name_get(uint8_t dev_id)
2702 {
2703 	struct rte_cryptodev *dev;
2704 
2705 	if (!rte_cryptodev_is_valid_device_data(dev_id)) {
2706 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2707 		return NULL;
2708 	}
2709 
2710 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2711 	if (dev == NULL)
2712 		return NULL;
2713 
2714 	rte_cryptodev_trace_name_get(dev_id, dev->data->name);
2715 
2716 	return dev->data->name;
2717 }
2718 
2719 const char *
2720 rte_cryptodev_driver_name_get(uint8_t driver_id)
2721 {
2722 	struct cryptodev_driver *driver;
2723 
2724 	TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
2725 		if (driver->id == driver_id) {
2726 			rte_cryptodev_trace_driver_name_get(driver_id,
2727 				driver->driver->name);
2728 			return driver->driver->name;
2729 		}
2730 	}
2731 	return NULL;
2732 }
2733 
2734 uint8_t
2735 rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
2736 		const struct rte_driver *drv)
2737 {
2738 	crypto_drv->driver = drv;
2739 	crypto_drv->id = nb_drivers;
2740 
2741 	TAILQ_INSERT_TAIL(&cryptodev_driver_list, crypto_drv, next);
2742 
2743 	rte_cryptodev_trace_allocate_driver(drv->name);
2744 
2745 	return nb_drivers++;
2746 }
2747 
2748 RTE_INIT(cryptodev_init_fp_ops)
2749 {
2750 	uint32_t i;
2751 
2752 	for (i = 0; i != RTE_DIM(rte_crypto_fp_ops); i++)
2753 		cryptodev_fp_ops_reset(rte_crypto_fp_ops + i);
2754 }
2755 
2756 static int
2757 cryptodev_handle_dev_list(const char *cmd __rte_unused,
2758 		const char *params __rte_unused,
2759 		struct rte_tel_data *d)
2760 {
2761 	int dev_id;
2762 
2763 	if (rte_cryptodev_count() < 1)
2764 		return -EINVAL;
2765 
2766 	rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
2767 	for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
2768 		if (rte_cryptodev_is_valid_dev(dev_id))
2769 			rte_tel_data_add_array_int(d, dev_id);
2770 
2771 	return 0;
2772 }
2773 
2774 static int
2775 cryptodev_handle_dev_info(const char *cmd __rte_unused,
2776 		const char *params, struct rte_tel_data *d)
2777 {
2778 	struct rte_cryptodev_info cryptodev_info;
2779 	int dev_id;
2780 	char *end_param;
2781 
2782 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2783 		return -EINVAL;
2784 
2785 	dev_id = strtoul(params, &end_param, 0);
2786 	if (*end_param != '\0')
2787 		CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2788 	if (!rte_cryptodev_is_valid_dev(dev_id))
2789 		return -EINVAL;
2790 
2791 	rte_cryptodev_info_get(dev_id, &cryptodev_info);
2792 
2793 	rte_tel_data_start_dict(d);
2794 	rte_tel_data_add_dict_string(d, "device_name",
2795 		cryptodev_info.device->name);
2796 	rte_tel_data_add_dict_uint(d, "max_nb_queue_pairs",
2797 		cryptodev_info.max_nb_queue_pairs);
2798 
2799 	return 0;
2800 }
2801 
2802 #define ADD_DICT_STAT(s) rte_tel_data_add_dict_uint(d, #s, cryptodev_stats.s)
2803 
2804 static int
2805 cryptodev_handle_dev_stats(const char *cmd __rte_unused,
2806 		const char *params,
2807 		struct rte_tel_data *d)
2808 {
2809 	struct rte_cryptodev_stats cryptodev_stats;
2810 	int dev_id, ret;
2811 	char *end_param;
2812 
2813 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2814 		return -EINVAL;
2815 
2816 	dev_id = strtoul(params, &end_param, 0);
2817 	if (*end_param != '\0')
2818 		CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2819 	if (!rte_cryptodev_is_valid_dev(dev_id))
2820 		return -EINVAL;
2821 
2822 	ret = rte_cryptodev_stats_get(dev_id, &cryptodev_stats);
2823 	if (ret < 0)
2824 		return ret;
2825 
2826 	rte_tel_data_start_dict(d);
2827 	ADD_DICT_STAT(enqueued_count);
2828 	ADD_DICT_STAT(dequeued_count);
2829 	ADD_DICT_STAT(enqueue_err_count);
2830 	ADD_DICT_STAT(dequeue_err_count);
2831 
2832 	return 0;
2833 }
2834 
2835 #define CRYPTO_CAPS_SZ                                             \
2836 	(RTE_ALIGN_CEIL(sizeof(struct rte_cryptodev_capabilities), \
2837 					sizeof(uint64_t)) /        \
2838 	 sizeof(uint64_t))
2839 
2840 static int
2841 crypto_caps_array(struct rte_tel_data *d,
2842 		  const struct rte_cryptodev_capabilities *capabilities)
2843 {
2844 	const struct rte_cryptodev_capabilities *dev_caps;
2845 	uint64_t caps_val[CRYPTO_CAPS_SZ];
2846 	unsigned int i = 0, j;
2847 
2848 	rte_tel_data_start_array(d, RTE_TEL_UINT_VAL);
2849 
2850 	while ((dev_caps = &capabilities[i++])->op !=
2851 			RTE_CRYPTO_OP_TYPE_UNDEFINED) {
2852 		memset(&caps_val, 0, CRYPTO_CAPS_SZ * sizeof(caps_val[0]));
2853 		rte_memcpy(caps_val, dev_caps, sizeof(capabilities[0]));
2854 		for (j = 0; j < CRYPTO_CAPS_SZ; j++)
2855 			rte_tel_data_add_array_uint(d, caps_val[j]);
2856 	}
2857 
2858 	return i;
2859 }
2860 
2861 static int
2862 cryptodev_handle_dev_caps(const char *cmd __rte_unused, const char *params,
2863 			  struct rte_tel_data *d)
2864 {
2865 	struct rte_cryptodev_info dev_info;
2866 	struct rte_tel_data *crypto_caps;
2867 	int crypto_caps_n;
2868 	char *end_param;
2869 	int dev_id;
2870 
2871 	if (!params || strlen(params) == 0 || !isdigit(*params))
2872 		return -EINVAL;
2873 
2874 	dev_id = strtoul(params, &end_param, 0);
2875 	if (*end_param != '\0')
2876 		CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2877 	if (!rte_cryptodev_is_valid_dev(dev_id))
2878 		return -EINVAL;
2879 
2880 	rte_tel_data_start_dict(d);
2881 	crypto_caps = rte_tel_data_alloc();
2882 	if (!crypto_caps)
2883 		return -ENOMEM;
2884 
2885 	rte_cryptodev_info_get(dev_id, &dev_info);
2886 	crypto_caps_n = crypto_caps_array(crypto_caps, dev_info.capabilities);
2887 	rte_tel_data_add_dict_container(d, "crypto_caps", crypto_caps, 0);
2888 	rte_tel_data_add_dict_int(d, "crypto_caps_n", crypto_caps_n);
2889 
2890 	return 0;
2891 }
2892 
2893 RTE_INIT(cryptodev_init_telemetry)
2894 {
2895 	rte_telemetry_register_cmd("/cryptodev/info", cryptodev_handle_dev_info,
2896 			"Returns information for a cryptodev. Parameters: int dev_id");
2897 	rte_telemetry_register_cmd("/cryptodev/list",
2898 			cryptodev_handle_dev_list,
2899 			"Returns list of available crypto devices by IDs. No parameters.");
2900 	rte_telemetry_register_cmd("/cryptodev/stats",
2901 			cryptodev_handle_dev_stats,
2902 			"Returns the stats for a cryptodev. Parameters: int dev_id");
2903 	rte_telemetry_register_cmd("/cryptodev/caps",
2904 			cryptodev_handle_dev_caps,
2905 			"Returns the capabilities for a cryptodev. Parameters: int dev_id");
2906 }
2907