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