xref: /dpdk/lib/cryptodev/rte_cryptodev.c (revision 3c4898ef762eeb2578b9ae3d7f6e3a0e5cbca8c8)
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 		rte_atomic_store_explicit(&tail->next, cb, rte_memory_order_release);
1539 	} else {
1540 		/* Stores to cb->fn and cb->param should complete before
1541 		 * cb is visible to data plane.
1542 		 */
1543 		rte_atomic_store_explicit(&list->next, cb, rte_memory_order_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 	RTE_ATOMIC(struct rte_cryptodev_cb *) *prev_cb;
1559 	struct rte_cryptodev_cb *curr_cb;
1560 	struct rte_cryptodev_cb_rcu *list;
1561 	int ret;
1562 
1563 	ret = -EINVAL;
1564 
1565 	if (!cb) {
1566 		CDEV_LOG_ERR("Callback is NULL");
1567 		return -EINVAL;
1568 	}
1569 
1570 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1571 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1572 		return -ENODEV;
1573 	}
1574 
1575 	rte_cryptodev_trace_remove_enq_callback(dev_id, qp_id, cb->fn);
1576 
1577 	dev = &rte_crypto_devices[dev_id];
1578 	if (qp_id >= dev->data->nb_queue_pairs) {
1579 		CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1580 		return -ENODEV;
1581 	}
1582 
1583 	rte_spinlock_lock(&rte_cryptodev_callback_lock);
1584 	if (dev->enq_cbs == NULL) {
1585 		CDEV_LOG_ERR("Callback not initialized");
1586 		goto cb_err;
1587 	}
1588 
1589 	list = &dev->enq_cbs[qp_id];
1590 	if (list == NULL) {
1591 		CDEV_LOG_ERR("Callback list is NULL");
1592 		goto cb_err;
1593 	}
1594 
1595 	if (list->qsbr == NULL) {
1596 		CDEV_LOG_ERR("Rcu qsbr is NULL");
1597 		goto cb_err;
1598 	}
1599 
1600 	prev_cb = &list->next;
1601 	for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1602 		curr_cb = *prev_cb;
1603 		if (curr_cb == cb) {
1604 			/* Remove the user cb from the callback list. */
1605 			rte_atomic_store_explicit(prev_cb, curr_cb->next,
1606 				rte_memory_order_relaxed);
1607 			ret = 0;
1608 			break;
1609 		}
1610 	}
1611 
1612 	if (!ret) {
1613 		/* Call sync with invalid thread id as this is part of
1614 		 * control plane API
1615 		 */
1616 		rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1617 		rte_free(cb);
1618 	}
1619 
1620 cb_err:
1621 	rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1622 	return ret;
1623 }
1624 
1625 struct rte_cryptodev_cb *
1626 rte_cryptodev_add_deq_callback(uint8_t dev_id,
1627 			       uint16_t qp_id,
1628 			       rte_cryptodev_callback_fn cb_fn,
1629 			       void *cb_arg)
1630 {
1631 	struct rte_cryptodev *dev;
1632 	struct rte_cryptodev_cb_rcu *list;
1633 	struct rte_cryptodev_cb *cb, *tail;
1634 
1635 	if (!cb_fn) {
1636 		CDEV_LOG_ERR("Callback is NULL on dev_id=%d", dev_id);
1637 		rte_errno = EINVAL;
1638 		return NULL;
1639 	}
1640 
1641 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1642 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1643 		rte_errno = ENODEV;
1644 		return NULL;
1645 	}
1646 
1647 	dev = &rte_crypto_devices[dev_id];
1648 	if (qp_id >= dev->data->nb_queue_pairs) {
1649 		CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1650 		rte_errno = ENODEV;
1651 		return NULL;
1652 	}
1653 
1654 	cb = rte_zmalloc(NULL, sizeof(*cb), 0);
1655 	if (cb == NULL) {
1656 		CDEV_LOG_ERR("Failed to allocate memory for callback on "
1657 			     "dev=%d, queue_pair_id=%d", dev_id, qp_id);
1658 		rte_errno = ENOMEM;
1659 		return NULL;
1660 	}
1661 
1662 	rte_spinlock_lock(&rte_cryptodev_callback_lock);
1663 
1664 	cb->fn = cb_fn;
1665 	cb->arg = cb_arg;
1666 
1667 	/* Add the callbacks in fifo order. */
1668 	list = &dev->deq_cbs[qp_id];
1669 	tail = list->next;
1670 
1671 	if (tail) {
1672 		while (tail->next)
1673 			tail = tail->next;
1674 		/* Stores to cb->fn and cb->param should complete before
1675 		 * cb is visible to data plane.
1676 		 */
1677 		rte_atomic_store_explicit(&tail->next, cb, rte_memory_order_release);
1678 	} else {
1679 		/* Stores to cb->fn and cb->param should complete before
1680 		 * cb is visible to data plane.
1681 		 */
1682 		rte_atomic_store_explicit(&list->next, cb, rte_memory_order_release);
1683 	}
1684 
1685 	rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1686 
1687 	rte_cryptodev_trace_add_deq_callback(dev_id, qp_id, cb_fn);
1688 
1689 	return cb;
1690 }
1691 
1692 int
1693 rte_cryptodev_remove_deq_callback(uint8_t dev_id,
1694 				  uint16_t qp_id,
1695 				  struct rte_cryptodev_cb *cb)
1696 {
1697 	struct rte_cryptodev *dev;
1698 	RTE_ATOMIC(struct rte_cryptodev_cb *) *prev_cb;
1699 	struct rte_cryptodev_cb *curr_cb;
1700 	struct rte_cryptodev_cb_rcu *list;
1701 	int ret;
1702 
1703 	ret = -EINVAL;
1704 
1705 	if (!cb) {
1706 		CDEV_LOG_ERR("Callback is NULL");
1707 		return -EINVAL;
1708 	}
1709 
1710 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1711 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1712 		return -ENODEV;
1713 	}
1714 
1715 	rte_cryptodev_trace_remove_deq_callback(dev_id, qp_id, cb->fn);
1716 
1717 	dev = &rte_crypto_devices[dev_id];
1718 	if (qp_id >= dev->data->nb_queue_pairs) {
1719 		CDEV_LOG_ERR("Invalid queue_pair_id=%d", qp_id);
1720 		return -ENODEV;
1721 	}
1722 
1723 	rte_spinlock_lock(&rte_cryptodev_callback_lock);
1724 	if (dev->enq_cbs == NULL) {
1725 		CDEV_LOG_ERR("Callback not initialized");
1726 		goto cb_err;
1727 	}
1728 
1729 	list = &dev->deq_cbs[qp_id];
1730 	if (list == NULL) {
1731 		CDEV_LOG_ERR("Callback list is NULL");
1732 		goto cb_err;
1733 	}
1734 
1735 	if (list->qsbr == NULL) {
1736 		CDEV_LOG_ERR("Rcu qsbr is NULL");
1737 		goto cb_err;
1738 	}
1739 
1740 	prev_cb = &list->next;
1741 	for (; *prev_cb != NULL; prev_cb = &curr_cb->next) {
1742 		curr_cb = *prev_cb;
1743 		if (curr_cb == cb) {
1744 			/* Remove the user cb from the callback list. */
1745 			rte_atomic_store_explicit(prev_cb, curr_cb->next,
1746 				rte_memory_order_relaxed);
1747 			ret = 0;
1748 			break;
1749 		}
1750 	}
1751 
1752 	if (!ret) {
1753 		/* Call sync with invalid thread id as this is part of
1754 		 * control plane API
1755 		 */
1756 		rte_rcu_qsbr_synchronize(list->qsbr, RTE_QSBR_THRID_INVALID);
1757 		rte_free(cb);
1758 	}
1759 
1760 cb_err:
1761 	rte_spinlock_unlock(&rte_cryptodev_callback_lock);
1762 	return ret;
1763 }
1764 
1765 int
1766 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
1767 {
1768 	struct rte_cryptodev *dev;
1769 
1770 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1771 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1772 		return -ENODEV;
1773 	}
1774 
1775 	if (stats == NULL) {
1776 		CDEV_LOG_ERR("Invalid stats ptr");
1777 		return -EINVAL;
1778 	}
1779 
1780 	dev = &rte_crypto_devices[dev_id];
1781 	memset(stats, 0, sizeof(*stats));
1782 
1783 	if (*dev->dev_ops->stats_get == NULL)
1784 		return -ENOTSUP;
1785 	(*dev->dev_ops->stats_get)(dev, stats);
1786 
1787 	rte_cryptodev_trace_stats_get(dev_id, stats);
1788 	return 0;
1789 }
1790 
1791 void
1792 rte_cryptodev_stats_reset(uint8_t dev_id)
1793 {
1794 	struct rte_cryptodev *dev;
1795 
1796 	rte_cryptodev_trace_stats_reset(dev_id);
1797 
1798 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1799 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1800 		return;
1801 	}
1802 
1803 	dev = &rte_crypto_devices[dev_id];
1804 
1805 	if (*dev->dev_ops->stats_reset == NULL)
1806 		return;
1807 	(*dev->dev_ops->stats_reset)(dev);
1808 }
1809 
1810 void
1811 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
1812 {
1813 	struct rte_cryptodev *dev;
1814 
1815 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1816 		CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1817 		return;
1818 	}
1819 
1820 	dev = &rte_crypto_devices[dev_id];
1821 
1822 	memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
1823 
1824 	if (*dev->dev_ops->dev_infos_get == NULL)
1825 		return;
1826 	(*dev->dev_ops->dev_infos_get)(dev, dev_info);
1827 
1828 	dev_info->driver_name = dev->device->driver->name;
1829 	dev_info->device = dev->device;
1830 
1831 	rte_cryptodev_trace_info_get(dev_id, dev_info->driver_name);
1832 
1833 }
1834 
1835 int
1836 rte_cryptodev_callback_register(uint8_t dev_id,
1837 			enum rte_cryptodev_event_type event,
1838 			rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1839 {
1840 	struct rte_cryptodev *dev;
1841 	struct rte_cryptodev_callback *user_cb;
1842 
1843 	if (!cb_fn)
1844 		return -EINVAL;
1845 
1846 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1847 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1848 		return -EINVAL;
1849 	}
1850 
1851 	dev = &rte_crypto_devices[dev_id];
1852 	rte_spinlock_lock(&rte_cryptodev_cb_lock);
1853 
1854 	TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
1855 		if (user_cb->cb_fn == cb_fn &&
1856 			user_cb->cb_arg == cb_arg &&
1857 			user_cb->event == event) {
1858 			break;
1859 		}
1860 	}
1861 
1862 	/* create a new callback. */
1863 	if (user_cb == NULL) {
1864 		user_cb = rte_zmalloc("INTR_USER_CALLBACK",
1865 				sizeof(struct rte_cryptodev_callback), 0);
1866 		if (user_cb != NULL) {
1867 			user_cb->cb_fn = cb_fn;
1868 			user_cb->cb_arg = cb_arg;
1869 			user_cb->event = event;
1870 			TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
1871 		}
1872 	}
1873 
1874 	rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1875 
1876 	rte_cryptodev_trace_callback_register(dev_id, event, cb_fn);
1877 	return (user_cb == NULL) ? -ENOMEM : 0;
1878 }
1879 
1880 int
1881 rte_cryptodev_callback_unregister(uint8_t dev_id,
1882 			enum rte_cryptodev_event_type event,
1883 			rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1884 {
1885 	int ret;
1886 	struct rte_cryptodev *dev;
1887 	struct rte_cryptodev_callback *cb, *next;
1888 
1889 	if (!cb_fn)
1890 		return -EINVAL;
1891 
1892 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1893 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1894 		return -EINVAL;
1895 	}
1896 
1897 	dev = &rte_crypto_devices[dev_id];
1898 	rte_spinlock_lock(&rte_cryptodev_cb_lock);
1899 
1900 	ret = 0;
1901 	for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
1902 
1903 		next = TAILQ_NEXT(cb, next);
1904 
1905 		if (cb->cb_fn != cb_fn || cb->event != event ||
1906 				(cb->cb_arg != (void *)-1 &&
1907 				cb->cb_arg != cb_arg))
1908 			continue;
1909 
1910 		/*
1911 		 * if this callback is not executing right now,
1912 		 * then remove it.
1913 		 */
1914 		if (cb->active == 0) {
1915 			TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
1916 			rte_free(cb);
1917 		} else {
1918 			ret = -EAGAIN;
1919 		}
1920 	}
1921 
1922 	rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1923 
1924 	rte_cryptodev_trace_callback_unregister(dev_id, event, cb_fn);
1925 	return ret;
1926 }
1927 
1928 void
1929 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1930 	enum rte_cryptodev_event_type event)
1931 {
1932 	struct rte_cryptodev_callback *cb_lst;
1933 	struct rte_cryptodev_callback dev_cb;
1934 
1935 	rte_spinlock_lock(&rte_cryptodev_cb_lock);
1936 	TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1937 		if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1938 			continue;
1939 		dev_cb = *cb_lst;
1940 		cb_lst->active = 1;
1941 		rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1942 		dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1943 						dev_cb.cb_arg);
1944 		rte_spinlock_lock(&rte_cryptodev_cb_lock);
1945 		cb_lst->active = 0;
1946 	}
1947 	rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1948 }
1949 
1950 int
1951 rte_cryptodev_queue_pair_event_error_query(uint8_t dev_id, uint16_t qp_id)
1952 {
1953 	struct rte_cryptodev *dev;
1954 
1955 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
1956 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1957 		return -EINVAL;
1958 	}
1959 	dev = &rte_crypto_devices[dev_id];
1960 
1961 	if (qp_id >= dev->data->nb_queue_pairs)
1962 		return -EINVAL;
1963 	if (*dev->dev_ops->queue_pair_event_error_query == NULL)
1964 		return -ENOTSUP;
1965 
1966 	return dev->dev_ops->queue_pair_event_error_query(dev, qp_id);
1967 }
1968 
1969 struct rte_mempool *
1970 rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
1971 	uint32_t elt_size, uint32_t cache_size, uint16_t user_data_size,
1972 	int socket_id)
1973 {
1974 	struct rte_mempool *mp;
1975 	struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1976 	uint32_t obj_sz;
1977 
1978 	obj_sz = sizeof(struct rte_cryptodev_sym_session) + elt_size + user_data_size;
1979 
1980 	obj_sz = RTE_ALIGN_CEIL(obj_sz, RTE_CACHE_LINE_SIZE);
1981 	mp = rte_mempool_create(name, nb_elts, obj_sz, cache_size,
1982 			(uint32_t)(sizeof(*pool_priv)), NULL, NULL,
1983 			NULL, NULL,
1984 			socket_id, 0);
1985 	if (mp == NULL) {
1986 		CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d",
1987 			__func__, name, rte_errno);
1988 		return NULL;
1989 	}
1990 
1991 	pool_priv = rte_mempool_get_priv(mp);
1992 	if (!pool_priv) {
1993 		CDEV_LOG_ERR("%s(name=%s) failed to get private data",
1994 			__func__, name);
1995 		rte_mempool_free(mp);
1996 		return NULL;
1997 	}
1998 
1999 	pool_priv->sess_data_sz = elt_size;
2000 	pool_priv->user_data_sz = user_data_size;
2001 
2002 	rte_cryptodev_trace_sym_session_pool_create(name, nb_elts,
2003 		elt_size, cache_size, user_data_size, mp);
2004 	return mp;
2005 }
2006 
2007 struct rte_mempool *
2008 rte_cryptodev_asym_session_pool_create(const char *name, uint32_t nb_elts,
2009 	uint32_t cache_size, uint16_t user_data_size, int socket_id)
2010 {
2011 	struct rte_mempool *mp;
2012 	struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
2013 	uint32_t obj_sz, obj_sz_aligned;
2014 	uint8_t dev_id;
2015 	unsigned int priv_sz, max_priv_sz = 0;
2016 
2017 	for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
2018 		if (rte_cryptodev_is_valid_dev(dev_id)) {
2019 			priv_sz = rte_cryptodev_asym_get_private_session_size(dev_id);
2020 			if (priv_sz > max_priv_sz)
2021 				max_priv_sz = priv_sz;
2022 		}
2023 	if (max_priv_sz == 0) {
2024 		CDEV_LOG_INFO("Could not set max private session size");
2025 		return NULL;
2026 	}
2027 
2028 	obj_sz = rte_cryptodev_asym_get_header_session_size() + max_priv_sz +
2029 			user_data_size;
2030 	obj_sz_aligned =  RTE_ALIGN_CEIL(obj_sz, RTE_CACHE_LINE_SIZE);
2031 
2032 	mp = rte_mempool_create(name, nb_elts, obj_sz_aligned, cache_size,
2033 			(uint32_t)(sizeof(*pool_priv)),
2034 			NULL, NULL, NULL, NULL,
2035 			socket_id, 0);
2036 	if (mp == NULL) {
2037 		CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d",
2038 			__func__, name, rte_errno);
2039 		return NULL;
2040 	}
2041 
2042 	pool_priv = rte_mempool_get_priv(mp);
2043 	if (!pool_priv) {
2044 		CDEV_LOG_ERR("%s(name=%s) failed to get private data",
2045 			__func__, name);
2046 		rte_mempool_free(mp);
2047 		return NULL;
2048 	}
2049 	pool_priv->max_priv_session_sz = max_priv_sz;
2050 	pool_priv->user_data_sz = user_data_size;
2051 
2052 	rte_cryptodev_trace_asym_session_pool_create(name, nb_elts,
2053 		user_data_size, cache_size, mp);
2054 	return mp;
2055 }
2056 
2057 void *
2058 rte_cryptodev_sym_session_create(uint8_t dev_id,
2059 		struct rte_crypto_sym_xform *xforms,
2060 		struct rte_mempool *mp)
2061 {
2062 	struct rte_cryptodev *dev;
2063 	struct rte_cryptodev_sym_session *sess;
2064 	struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
2065 	uint32_t sess_priv_sz;
2066 	int ret;
2067 
2068 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2069 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2070 		rte_errno = EINVAL;
2071 		return NULL;
2072 	}
2073 
2074 	if (xforms == NULL) {
2075 		CDEV_LOG_ERR("Invalid xform\n");
2076 		rte_errno = EINVAL;
2077 		return NULL;
2078 	}
2079 
2080 	sess_priv_sz = rte_cryptodev_sym_get_private_session_size(dev_id);
2081 	if (!rte_cryptodev_sym_is_valid_session_pool(mp, sess_priv_sz)) {
2082 		CDEV_LOG_ERR("Invalid mempool");
2083 		rte_errno = EINVAL;
2084 		return NULL;
2085 	}
2086 
2087 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2088 
2089 	/* Allocate a session structure from the session pool */
2090 	if (rte_mempool_get(mp, (void **)&sess)) {
2091 		CDEV_LOG_ERR("couldn't get object from session mempool");
2092 		rte_errno = ENOMEM;
2093 		return NULL;
2094 	}
2095 
2096 	pool_priv = rte_mempool_get_priv(mp);
2097 	sess->driver_id = dev->driver_id;
2098 	sess->sess_data_sz = pool_priv->sess_data_sz;
2099 	sess->user_data_sz = pool_priv->user_data_sz;
2100 	sess->driver_priv_data_iova = rte_mempool_virt2iova(sess) +
2101 		offsetof(struct rte_cryptodev_sym_session, driver_priv_data);
2102 
2103 	if (dev->dev_ops->sym_session_configure == NULL) {
2104 		rte_errno = ENOTSUP;
2105 		goto error_exit;
2106 	}
2107 	memset(sess->driver_priv_data, 0, pool_priv->sess_data_sz + pool_priv->user_data_sz);
2108 
2109 	ret = dev->dev_ops->sym_session_configure(dev, xforms, sess);
2110 	if (ret < 0) {
2111 		rte_errno = -ret;
2112 		goto error_exit;
2113 	}
2114 	sess->driver_id = dev->driver_id;
2115 
2116 	rte_cryptodev_trace_sym_session_create(dev_id, sess, xforms, mp);
2117 
2118 	return (void *)sess;
2119 error_exit:
2120 	rte_mempool_put(mp, (void *)sess);
2121 	return NULL;
2122 }
2123 
2124 int
2125 rte_cryptodev_asym_session_create(uint8_t dev_id,
2126 		struct rte_crypto_asym_xform *xforms, struct rte_mempool *mp,
2127 		void **session)
2128 {
2129 	struct rte_cryptodev_asym_session *sess;
2130 	uint32_t session_priv_data_sz;
2131 	struct rte_cryptodev_asym_session_pool_private_data *pool_priv;
2132 	unsigned int session_header_size =
2133 			rte_cryptodev_asym_get_header_session_size();
2134 	struct rte_cryptodev *dev;
2135 	int ret;
2136 
2137 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2138 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2139 		return -EINVAL;
2140 	}
2141 
2142 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2143 
2144 	if (dev == NULL)
2145 		return -EINVAL;
2146 
2147 	if (!mp) {
2148 		CDEV_LOG_ERR("invalid mempool");
2149 		return -EINVAL;
2150 	}
2151 
2152 	session_priv_data_sz = rte_cryptodev_asym_get_private_session_size(
2153 			dev_id);
2154 	pool_priv = rte_mempool_get_priv(mp);
2155 
2156 	if (pool_priv->max_priv_session_sz < session_priv_data_sz) {
2157 		CDEV_LOG_DEBUG(
2158 			"The private session data size used when creating the mempool is smaller than this device's private session data.");
2159 		return -EINVAL;
2160 	}
2161 
2162 	/* Verify if provided mempool can hold elements big enough. */
2163 	if (mp->elt_size < session_header_size + session_priv_data_sz) {
2164 		CDEV_LOG_ERR(
2165 			"mempool elements too small to hold session objects");
2166 		return -EINVAL;
2167 	}
2168 
2169 	/* Allocate a session structure from the session pool */
2170 	if (rte_mempool_get(mp, session)) {
2171 		CDEV_LOG_ERR("couldn't get object from session mempool");
2172 		return -ENOMEM;
2173 	}
2174 
2175 	sess = *session;
2176 	sess->driver_id = dev->driver_id;
2177 	sess->user_data_sz = pool_priv->user_data_sz;
2178 	sess->max_priv_data_sz = pool_priv->max_priv_session_sz;
2179 
2180 	/* Clear device session pointer.*/
2181 	memset(sess->sess_private_data, 0, session_priv_data_sz + sess->user_data_sz);
2182 
2183 	if (*dev->dev_ops->asym_session_configure == NULL)
2184 		return -ENOTSUP;
2185 
2186 	if (sess->sess_private_data[0] == 0) {
2187 		ret = dev->dev_ops->asym_session_configure(dev, xforms, sess);
2188 		if (ret < 0) {
2189 			CDEV_LOG_ERR(
2190 				"dev_id %d failed to configure session details",
2191 				dev_id);
2192 			return ret;
2193 		}
2194 	}
2195 
2196 	rte_cryptodev_trace_asym_session_create(dev_id, xforms, mp, sess);
2197 	return 0;
2198 }
2199 
2200 int
2201 rte_cryptodev_sym_session_free(uint8_t dev_id, void *_sess)
2202 {
2203 	struct rte_cryptodev *dev;
2204 	struct rte_mempool *sess_mp;
2205 	struct rte_cryptodev_sym_session *sess = _sess;
2206 	struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
2207 
2208 	if (sess == NULL)
2209 		return -EINVAL;
2210 
2211 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2212 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2213 		return -EINVAL;
2214 	}
2215 
2216 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2217 
2218 	if (dev == NULL || sess == NULL)
2219 		return -EINVAL;
2220 
2221 	sess_mp = rte_mempool_from_obj(sess);
2222 	if (!sess_mp)
2223 		return -EINVAL;
2224 	pool_priv = rte_mempool_get_priv(sess_mp);
2225 
2226 	if (sess->driver_id != dev->driver_id) {
2227 		CDEV_LOG_ERR("Session created by driver %u but freed by %u",
2228 			sess->driver_id, dev->driver_id);
2229 		return -EINVAL;
2230 	}
2231 
2232 	if (*dev->dev_ops->sym_session_clear == NULL)
2233 		return -ENOTSUP;
2234 
2235 	dev->dev_ops->sym_session_clear(dev, sess);
2236 
2237 	memset(sess->driver_priv_data, 0, pool_priv->sess_data_sz + pool_priv->user_data_sz);
2238 
2239 	/* Return session to mempool */
2240 	rte_mempool_put(sess_mp, sess);
2241 
2242 	rte_cryptodev_trace_sym_session_free(dev_id, sess);
2243 	return 0;
2244 }
2245 
2246 int
2247 rte_cryptodev_asym_session_free(uint8_t dev_id, void *sess)
2248 {
2249 	struct rte_mempool *sess_mp;
2250 	struct rte_cryptodev *dev;
2251 
2252 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2253 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2254 		return -EINVAL;
2255 	}
2256 
2257 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2258 
2259 	if (dev == NULL || sess == NULL)
2260 		return -EINVAL;
2261 
2262 	if (*dev->dev_ops->asym_session_clear == NULL)
2263 		return -ENOTSUP;
2264 
2265 	dev->dev_ops->asym_session_clear(dev, sess);
2266 
2267 	rte_free(((struct rte_cryptodev_asym_session *)sess)->event_mdata);
2268 
2269 	/* Return session to mempool */
2270 	sess_mp = rte_mempool_from_obj(sess);
2271 	rte_mempool_put(sess_mp, sess);
2272 
2273 	rte_cryptodev_trace_asym_session_free(dev_id, sess);
2274 	return 0;
2275 }
2276 
2277 unsigned int
2278 rte_cryptodev_asym_get_header_session_size(void)
2279 {
2280 	return sizeof(struct rte_cryptodev_asym_session);
2281 }
2282 
2283 unsigned int
2284 rte_cryptodev_sym_get_private_session_size(uint8_t dev_id)
2285 {
2286 	struct rte_cryptodev *dev;
2287 	unsigned int priv_sess_size;
2288 
2289 	if (!rte_cryptodev_is_valid_dev(dev_id))
2290 		return 0;
2291 
2292 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2293 
2294 	if (*dev->dev_ops->sym_session_get_size == NULL)
2295 		return 0;
2296 
2297 	priv_sess_size = (*dev->dev_ops->sym_session_get_size)(dev);
2298 
2299 	rte_cryptodev_trace_sym_get_private_session_size(dev_id,
2300 		priv_sess_size);
2301 
2302 	return priv_sess_size;
2303 }
2304 
2305 unsigned int
2306 rte_cryptodev_asym_get_private_session_size(uint8_t dev_id)
2307 {
2308 	struct rte_cryptodev *dev;
2309 	unsigned int priv_sess_size;
2310 
2311 	if (!rte_cryptodev_is_valid_dev(dev_id))
2312 		return 0;
2313 
2314 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2315 
2316 	if (*dev->dev_ops->asym_session_get_size == NULL)
2317 		return 0;
2318 
2319 	priv_sess_size = (*dev->dev_ops->asym_session_get_size)(dev);
2320 
2321 	rte_cryptodev_trace_asym_get_private_session_size(dev_id,
2322 		priv_sess_size);
2323 
2324 	return priv_sess_size;
2325 }
2326 
2327 int
2328 rte_cryptodev_sym_session_set_user_data(void *_sess, void *data,
2329 		uint16_t size)
2330 {
2331 	struct rte_cryptodev_sym_session *sess = _sess;
2332 
2333 	if (sess == NULL)
2334 		return -EINVAL;
2335 
2336 	if (sess->user_data_sz < size)
2337 		return -ENOMEM;
2338 
2339 	rte_memcpy(sess->driver_priv_data + sess->sess_data_sz, data, size);
2340 
2341 	rte_cryptodev_trace_sym_session_set_user_data(sess, data, size);
2342 
2343 	return 0;
2344 }
2345 
2346 void *
2347 rte_cryptodev_sym_session_get_user_data(void *_sess)
2348 {
2349 	struct rte_cryptodev_sym_session *sess = _sess;
2350 	void *data = NULL;
2351 
2352 	if (sess == NULL || sess->user_data_sz == 0)
2353 		return NULL;
2354 
2355 	data = (void *)(sess->driver_priv_data + sess->sess_data_sz);
2356 
2357 	rte_cryptodev_trace_sym_session_get_user_data(sess, data);
2358 
2359 	return data;
2360 }
2361 
2362 int
2363 rte_cryptodev_asym_session_set_user_data(void *session, void *data, uint16_t size)
2364 {
2365 	struct rte_cryptodev_asym_session *sess = session;
2366 	if (sess == NULL)
2367 		return -EINVAL;
2368 
2369 	if (sess->user_data_sz < size)
2370 		return -ENOMEM;
2371 
2372 	rte_memcpy(sess->sess_private_data +
2373 			sess->max_priv_data_sz,
2374 			data, size);
2375 
2376 	rte_cryptodev_trace_asym_session_set_user_data(sess, data, size);
2377 
2378 	return 0;
2379 }
2380 
2381 void *
2382 rte_cryptodev_asym_session_get_user_data(void *session)
2383 {
2384 	struct rte_cryptodev_asym_session *sess = session;
2385 	void *data = NULL;
2386 
2387 	if (sess == NULL || sess->user_data_sz == 0)
2388 		return NULL;
2389 
2390 	data = (void *)(sess->sess_private_data + sess->max_priv_data_sz);
2391 
2392 	rte_cryptodev_trace_asym_session_get_user_data(sess, data);
2393 
2394 	return data;
2395 }
2396 
2397 static inline void
2398 sym_crypto_fill_status(struct rte_crypto_sym_vec *vec, int32_t errnum)
2399 {
2400 	uint32_t i;
2401 	for (i = 0; i < vec->num; i++)
2402 		vec->status[i] = errnum;
2403 }
2404 
2405 uint32_t
2406 rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
2407 	void *_sess, union rte_crypto_sym_ofs ofs,
2408 	struct rte_crypto_sym_vec *vec)
2409 {
2410 	struct rte_cryptodev *dev;
2411 	struct rte_cryptodev_sym_session *sess = _sess;
2412 
2413 	if (!rte_cryptodev_is_valid_dev(dev_id)) {
2414 		sym_crypto_fill_status(vec, EINVAL);
2415 		return 0;
2416 	}
2417 
2418 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2419 
2420 	if (*dev->dev_ops->sym_cpu_process == NULL ||
2421 		!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO)) {
2422 		sym_crypto_fill_status(vec, ENOTSUP);
2423 		return 0;
2424 	}
2425 
2426 	rte_cryptodev_trace_sym_cpu_crypto_process(dev_id, sess);
2427 
2428 	return dev->dev_ops->sym_cpu_process(dev, sess, ofs, vec);
2429 }
2430 
2431 int
2432 rte_cryptodev_get_raw_dp_ctx_size(uint8_t dev_id)
2433 {
2434 	struct rte_cryptodev *dev;
2435 	int32_t size = sizeof(struct rte_crypto_raw_dp_ctx);
2436 	int32_t priv_size;
2437 
2438 	if (!rte_cryptodev_is_valid_dev(dev_id))
2439 		return -EINVAL;
2440 
2441 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2442 
2443 	if (*dev->dev_ops->sym_get_raw_dp_ctx_size == NULL ||
2444 		!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)) {
2445 		return -ENOTSUP;
2446 	}
2447 
2448 	priv_size = (*dev->dev_ops->sym_get_raw_dp_ctx_size)(dev);
2449 	if (priv_size < 0)
2450 		return -ENOTSUP;
2451 
2452 	rte_cryptodev_trace_get_raw_dp_ctx_size(dev_id);
2453 
2454 	return RTE_ALIGN_CEIL((size + priv_size), 8);
2455 }
2456 
2457 int
2458 rte_cryptodev_configure_raw_dp_ctx(uint8_t dev_id, uint16_t qp_id,
2459 	struct rte_crypto_raw_dp_ctx *ctx,
2460 	enum rte_crypto_op_sess_type sess_type,
2461 	union rte_cryptodev_session_ctx session_ctx,
2462 	uint8_t is_update)
2463 {
2464 	struct rte_cryptodev *dev;
2465 
2466 	if (!rte_cryptodev_get_qp_status(dev_id, qp_id))
2467 		return -EINVAL;
2468 
2469 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2470 	if (!(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP)
2471 			|| dev->dev_ops->sym_configure_raw_dp_ctx == NULL)
2472 		return -ENOTSUP;
2473 
2474 	rte_cryptodev_trace_configure_raw_dp_ctx(dev_id, qp_id, sess_type);
2475 
2476 	return (*dev->dev_ops->sym_configure_raw_dp_ctx)(dev, qp_id, ctx,
2477 			sess_type, session_ctx, is_update);
2478 }
2479 
2480 int
2481 rte_cryptodev_session_event_mdata_set(uint8_t dev_id, void *sess,
2482 	enum rte_crypto_op_type op_type,
2483 	enum rte_crypto_op_sess_type sess_type,
2484 	void *ev_mdata,
2485 	uint16_t size)
2486 {
2487 	struct rte_cryptodev *dev;
2488 
2489 	if (sess == NULL || ev_mdata == NULL)
2490 		return -EINVAL;
2491 
2492 	if (!rte_cryptodev_is_valid_dev(dev_id))
2493 		goto skip_pmd_op;
2494 
2495 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2496 	if (dev->dev_ops->session_ev_mdata_set == NULL)
2497 		goto skip_pmd_op;
2498 
2499 	rte_cryptodev_trace_session_event_mdata_set(dev_id, sess, op_type,
2500 		sess_type, ev_mdata, size);
2501 
2502 	return (*dev->dev_ops->session_ev_mdata_set)(dev, sess, op_type,
2503 			sess_type, ev_mdata);
2504 
2505 skip_pmd_op:
2506 	if (op_type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
2507 		return rte_cryptodev_sym_session_set_user_data(sess, ev_mdata,
2508 				size);
2509 	else if (op_type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
2510 		struct rte_cryptodev_asym_session *s = sess;
2511 
2512 		if (s->event_mdata == NULL) {
2513 			s->event_mdata = rte_malloc(NULL, size, 0);
2514 			if (s->event_mdata == NULL)
2515 				return -ENOMEM;
2516 		}
2517 		rte_memcpy(s->event_mdata, ev_mdata, size);
2518 
2519 		return 0;
2520 	} else
2521 		return -ENOTSUP;
2522 }
2523 
2524 uint32_t
2525 rte_cryptodev_raw_enqueue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2526 	struct rte_crypto_sym_vec *vec, union rte_crypto_sym_ofs ofs,
2527 	void **user_data, int *enqueue_status)
2528 {
2529 	return (*ctx->enqueue_burst)(ctx->qp_data, ctx->drv_ctx_data, vec,
2530 			ofs, user_data, enqueue_status);
2531 }
2532 
2533 int
2534 rte_cryptodev_raw_enqueue_done(struct rte_crypto_raw_dp_ctx *ctx,
2535 		uint32_t n)
2536 {
2537 	return (*ctx->enqueue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2538 }
2539 
2540 uint32_t
2541 rte_cryptodev_raw_dequeue_burst(struct rte_crypto_raw_dp_ctx *ctx,
2542 	rte_cryptodev_raw_get_dequeue_count_t get_dequeue_count,
2543 	uint32_t max_nb_to_dequeue,
2544 	rte_cryptodev_raw_post_dequeue_t post_dequeue,
2545 	void **out_user_data, uint8_t is_user_data_array,
2546 	uint32_t *n_success_jobs, int *status)
2547 {
2548 	return (*ctx->dequeue_burst)(ctx->qp_data, ctx->drv_ctx_data,
2549 		get_dequeue_count, max_nb_to_dequeue, post_dequeue,
2550 		out_user_data, is_user_data_array, n_success_jobs, status);
2551 }
2552 
2553 int
2554 rte_cryptodev_raw_dequeue_done(struct rte_crypto_raw_dp_ctx *ctx,
2555 		uint32_t n)
2556 {
2557 	return (*ctx->dequeue_done)(ctx->qp_data, ctx->drv_ctx_data, n);
2558 }
2559 
2560 /** Initialise rte_crypto_op mempool element */
2561 static void
2562 rte_crypto_op_init(struct rte_mempool *mempool,
2563 		void *opaque_arg,
2564 		void *_op_data,
2565 		__rte_unused unsigned i)
2566 {
2567 	struct rte_crypto_op *op = _op_data;
2568 	enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
2569 
2570 	memset(_op_data, 0, mempool->elt_size);
2571 
2572 	__rte_crypto_op_reset(op, type);
2573 
2574 	op->phys_addr = rte_mem_virt2iova(_op_data);
2575 	op->mempool = mempool;
2576 }
2577 
2578 
2579 struct rte_mempool *
2580 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
2581 		unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
2582 		int socket_id)
2583 {
2584 	struct rte_crypto_op_pool_private *priv;
2585 
2586 	unsigned elt_size = sizeof(struct rte_crypto_op) +
2587 			priv_size;
2588 
2589 	if (type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
2590 		elt_size += sizeof(struct rte_crypto_sym_op);
2591 	} else if (type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
2592 		elt_size += sizeof(struct rte_crypto_asym_op);
2593 	} else if (type == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
2594 		elt_size += RTE_MAX(sizeof(struct rte_crypto_sym_op),
2595 		                    sizeof(struct rte_crypto_asym_op));
2596 	} else {
2597 		CDEV_LOG_ERR("Invalid op_type");
2598 		return NULL;
2599 	}
2600 
2601 	/* lookup mempool in case already allocated */
2602 	struct rte_mempool *mp = rte_mempool_lookup(name);
2603 
2604 	if (mp != NULL) {
2605 		priv = (struct rte_crypto_op_pool_private *)
2606 				rte_mempool_get_priv(mp);
2607 
2608 		if (mp->elt_size != elt_size ||
2609 				mp->cache_size < cache_size ||
2610 				mp->size < nb_elts ||
2611 				priv->priv_size <  priv_size) {
2612 			mp = NULL;
2613 			CDEV_LOG_ERR("Mempool %s already exists but with "
2614 					"incompatible parameters", name);
2615 			return NULL;
2616 		}
2617 		return mp;
2618 	}
2619 
2620 	mp = rte_mempool_create(
2621 			name,
2622 			nb_elts,
2623 			elt_size,
2624 			cache_size,
2625 			sizeof(struct rte_crypto_op_pool_private),
2626 			NULL,
2627 			NULL,
2628 			rte_crypto_op_init,
2629 			&type,
2630 			socket_id,
2631 			0);
2632 
2633 	if (mp == NULL) {
2634 		CDEV_LOG_ERR("Failed to create mempool %s", name);
2635 		return NULL;
2636 	}
2637 
2638 	priv = (struct rte_crypto_op_pool_private *)
2639 			rte_mempool_get_priv(mp);
2640 
2641 	priv->priv_size = priv_size;
2642 	priv->type = type;
2643 
2644 	rte_cryptodev_trace_op_pool_create(name, socket_id, type, nb_elts, mp);
2645 	return mp;
2646 }
2647 
2648 int
2649 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix)
2650 {
2651 	struct rte_cryptodev *dev = NULL;
2652 	uint32_t i = 0;
2653 
2654 	if (name == NULL)
2655 		return -EINVAL;
2656 
2657 	for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
2658 		int ret = snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
2659 				"%s_%u", dev_name_prefix, i);
2660 
2661 		if (ret < 0)
2662 			return ret;
2663 
2664 		dev = rte_cryptodev_pmd_get_named_dev(name);
2665 		if (!dev)
2666 			return 0;
2667 	}
2668 
2669 	return -1;
2670 }
2671 
2672 TAILQ_HEAD(cryptodev_driver_list, cryptodev_driver);
2673 
2674 static struct cryptodev_driver_list cryptodev_driver_list =
2675 	TAILQ_HEAD_INITIALIZER(cryptodev_driver_list);
2676 
2677 int
2678 rte_cryptodev_driver_id_get(const char *name)
2679 {
2680 	struct cryptodev_driver *driver;
2681 	const char *driver_name;
2682 	int driver_id = -1;
2683 
2684 	if (name == NULL) {
2685 		RTE_LOG(DEBUG, CRYPTODEV, "name pointer NULL");
2686 		return -1;
2687 	}
2688 
2689 	TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
2690 		driver_name = driver->driver->name;
2691 		if (strncmp(driver_name, name, strlen(driver_name) + 1) == 0) {
2692 			driver_id = driver->id;
2693 			break;
2694 		}
2695 	}
2696 
2697 	rte_cryptodev_trace_driver_id_get(name, driver_id);
2698 
2699 	return driver_id;
2700 }
2701 
2702 const char *
2703 rte_cryptodev_name_get(uint8_t dev_id)
2704 {
2705 	struct rte_cryptodev *dev;
2706 
2707 	if (!rte_cryptodev_is_valid_device_data(dev_id)) {
2708 		CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
2709 		return NULL;
2710 	}
2711 
2712 	dev = rte_cryptodev_pmd_get_dev(dev_id);
2713 	if (dev == NULL)
2714 		return NULL;
2715 
2716 	rte_cryptodev_trace_name_get(dev_id, dev->data->name);
2717 
2718 	return dev->data->name;
2719 }
2720 
2721 const char *
2722 rte_cryptodev_driver_name_get(uint8_t driver_id)
2723 {
2724 	struct cryptodev_driver *driver;
2725 
2726 	TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
2727 		if (driver->id == driver_id) {
2728 			rte_cryptodev_trace_driver_name_get(driver_id,
2729 				driver->driver->name);
2730 			return driver->driver->name;
2731 		}
2732 	}
2733 	return NULL;
2734 }
2735 
2736 uint8_t
2737 rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
2738 		const struct rte_driver *drv)
2739 {
2740 	crypto_drv->driver = drv;
2741 	crypto_drv->id = nb_drivers;
2742 
2743 	TAILQ_INSERT_TAIL(&cryptodev_driver_list, crypto_drv, next);
2744 
2745 	rte_cryptodev_trace_allocate_driver(drv->name);
2746 
2747 	return nb_drivers++;
2748 }
2749 
2750 RTE_INIT(cryptodev_init_fp_ops)
2751 {
2752 	uint32_t i;
2753 
2754 	for (i = 0; i != RTE_DIM(rte_crypto_fp_ops); i++)
2755 		cryptodev_fp_ops_reset(rte_crypto_fp_ops + i);
2756 }
2757 
2758 static int
2759 cryptodev_handle_dev_list(const char *cmd __rte_unused,
2760 		const char *params __rte_unused,
2761 		struct rte_tel_data *d)
2762 {
2763 	int dev_id;
2764 
2765 	if (rte_cryptodev_count() < 1)
2766 		return -EINVAL;
2767 
2768 	rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
2769 	for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
2770 		if (rte_cryptodev_is_valid_dev(dev_id))
2771 			rte_tel_data_add_array_int(d, dev_id);
2772 
2773 	return 0;
2774 }
2775 
2776 static int
2777 cryptodev_handle_dev_info(const char *cmd __rte_unused,
2778 		const char *params, struct rte_tel_data *d)
2779 {
2780 	struct rte_cryptodev_info cryptodev_info;
2781 	int dev_id;
2782 	char *end_param;
2783 
2784 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2785 		return -EINVAL;
2786 
2787 	dev_id = strtoul(params, &end_param, 0);
2788 	if (*end_param != '\0')
2789 		CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2790 	if (!rte_cryptodev_is_valid_dev(dev_id))
2791 		return -EINVAL;
2792 
2793 	rte_cryptodev_info_get(dev_id, &cryptodev_info);
2794 
2795 	rte_tel_data_start_dict(d);
2796 	rte_tel_data_add_dict_string(d, "device_name",
2797 		cryptodev_info.device->name);
2798 	rte_tel_data_add_dict_uint(d, "max_nb_queue_pairs",
2799 		cryptodev_info.max_nb_queue_pairs);
2800 
2801 	return 0;
2802 }
2803 
2804 #define ADD_DICT_STAT(s) rte_tel_data_add_dict_uint(d, #s, cryptodev_stats.s)
2805 
2806 static int
2807 cryptodev_handle_dev_stats(const char *cmd __rte_unused,
2808 		const char *params,
2809 		struct rte_tel_data *d)
2810 {
2811 	struct rte_cryptodev_stats cryptodev_stats;
2812 	int dev_id, ret;
2813 	char *end_param;
2814 
2815 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
2816 		return -EINVAL;
2817 
2818 	dev_id = strtoul(params, &end_param, 0);
2819 	if (*end_param != '\0')
2820 		CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2821 	if (!rte_cryptodev_is_valid_dev(dev_id))
2822 		return -EINVAL;
2823 
2824 	ret = rte_cryptodev_stats_get(dev_id, &cryptodev_stats);
2825 	if (ret < 0)
2826 		return ret;
2827 
2828 	rte_tel_data_start_dict(d);
2829 	ADD_DICT_STAT(enqueued_count);
2830 	ADD_DICT_STAT(dequeued_count);
2831 	ADD_DICT_STAT(enqueue_err_count);
2832 	ADD_DICT_STAT(dequeue_err_count);
2833 
2834 	return 0;
2835 }
2836 
2837 #define CRYPTO_CAPS_SZ                                             \
2838 	(RTE_ALIGN_CEIL(sizeof(struct rte_cryptodev_capabilities), \
2839 					sizeof(uint64_t)) /        \
2840 	 sizeof(uint64_t))
2841 
2842 static int
2843 crypto_caps_array(struct rte_tel_data *d,
2844 		  const struct rte_cryptodev_capabilities *capabilities)
2845 {
2846 	const struct rte_cryptodev_capabilities *dev_caps;
2847 	uint64_t caps_val[CRYPTO_CAPS_SZ];
2848 	unsigned int i = 0, j;
2849 
2850 	rte_tel_data_start_array(d, RTE_TEL_UINT_VAL);
2851 
2852 	while ((dev_caps = &capabilities[i++])->op !=
2853 			RTE_CRYPTO_OP_TYPE_UNDEFINED) {
2854 		memset(&caps_val, 0, CRYPTO_CAPS_SZ * sizeof(caps_val[0]));
2855 		rte_memcpy(caps_val, dev_caps, sizeof(capabilities[0]));
2856 		for (j = 0; j < CRYPTO_CAPS_SZ; j++)
2857 			rte_tel_data_add_array_uint(d, caps_val[j]);
2858 	}
2859 
2860 	return i;
2861 }
2862 
2863 static int
2864 cryptodev_handle_dev_caps(const char *cmd __rte_unused, const char *params,
2865 			  struct rte_tel_data *d)
2866 {
2867 	struct rte_cryptodev_info dev_info;
2868 	struct rte_tel_data *crypto_caps;
2869 	int crypto_caps_n;
2870 	char *end_param;
2871 	int dev_id;
2872 
2873 	if (!params || strlen(params) == 0 || !isdigit(*params))
2874 		return -EINVAL;
2875 
2876 	dev_id = strtoul(params, &end_param, 0);
2877 	if (*end_param != '\0')
2878 		CDEV_LOG_ERR("Extra parameters passed to command, ignoring");
2879 	if (!rte_cryptodev_is_valid_dev(dev_id))
2880 		return -EINVAL;
2881 
2882 	rte_tel_data_start_dict(d);
2883 	crypto_caps = rte_tel_data_alloc();
2884 	if (!crypto_caps)
2885 		return -ENOMEM;
2886 
2887 	rte_cryptodev_info_get(dev_id, &dev_info);
2888 	crypto_caps_n = crypto_caps_array(crypto_caps, dev_info.capabilities);
2889 	rte_tel_data_add_dict_container(d, "crypto_caps", crypto_caps, 0);
2890 	rte_tel_data_add_dict_int(d, "crypto_caps_n", crypto_caps_n);
2891 
2892 	return 0;
2893 }
2894 
2895 RTE_INIT(cryptodev_init_telemetry)
2896 {
2897 	rte_telemetry_register_cmd("/cryptodev/info", cryptodev_handle_dev_info,
2898 			"Returns information for a cryptodev. Parameters: int dev_id");
2899 	rte_telemetry_register_cmd("/cryptodev/list",
2900 			cryptodev_handle_dev_list,
2901 			"Returns list of available crypto devices by IDs. No parameters.");
2902 	rte_telemetry_register_cmd("/cryptodev/stats",
2903 			cryptodev_handle_dev_stats,
2904 			"Returns the stats for a cryptodev. Parameters: int dev_id");
2905 	rte_telemetry_register_cmd("/cryptodev/caps",
2906 			cryptodev_handle_dev_caps,
2907 			"Returns the capabilities for a cryptodev. Parameters: int dev_id");
2908 }
2909