test/compress: replace test vector
[dpdk.git] / lib / librte_cryptodev / rte_cryptodev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020 Intel Corporation
3  */
4
5 #include <sys/types.h>
6 #include <sys/queue.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <errno.h>
13 #include <stdint.h>
14 #include <inttypes.h>
15 #include <netinet/in.h>
16
17 #include <rte_byteorder.h>
18 #include <rte_log.h>
19 #include <rte_debug.h>
20 #include <rte_dev.h>
21 #include <rte_interrupts.h>
22 #include <rte_memory.h>
23 #include <rte_memcpy.h>
24 #include <rte_memzone.h>
25 #include <rte_launch.h>
26 #include <rte_tailq.h>
27 #include <rte_eal.h>
28 #include <rte_per_lcore.h>
29 #include <rte_lcore.h>
30 #include <rte_atomic.h>
31 #include <rte_branch_prediction.h>
32 #include <rte_common.h>
33 #include <rte_mempool.h>
34 #include <rte_malloc.h>
35 #include <rte_mbuf.h>
36 #include <rte_errno.h>
37 #include <rte_spinlock.h>
38 #include <rte_string_fns.h>
39
40 #include "rte_crypto.h"
41 #include "rte_cryptodev.h"
42 #include "rte_cryptodev_pmd.h"
43
44 static uint8_t nb_drivers;
45
46 static struct rte_cryptodev rte_crypto_devices[RTE_CRYPTO_MAX_DEVS];
47
48 struct rte_cryptodev *rte_cryptodevs = rte_crypto_devices;
49
50 static struct rte_cryptodev_global cryptodev_globals = {
51                 .devs                   = rte_crypto_devices,
52                 .data                   = { NULL },
53                 .nb_devs                = 0
54 };
55
56 /* spinlock for crypto device callbacks */
57 static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
58
59
60 /**
61  * The user application callback description.
62  *
63  * It contains callback address to be registered by user application,
64  * the pointer to the parameters for callback, and the event type.
65  */
66 struct rte_cryptodev_callback {
67         TAILQ_ENTRY(rte_cryptodev_callback) next; /**< Callbacks list */
68         rte_cryptodev_cb_fn cb_fn;              /**< Callback address */
69         void *cb_arg;                           /**< Parameter for callback */
70         enum rte_cryptodev_event_type event;    /**< Interrupt event type */
71         uint32_t active;                        /**< Callback is executing */
72 };
73
74 /**
75  * The crypto cipher algorithm strings identifiers.
76  * It could be used in application command line.
77  */
78 const char *
79 rte_crypto_cipher_algorithm_strings[] = {
80         [RTE_CRYPTO_CIPHER_3DES_CBC]    = "3des-cbc",
81         [RTE_CRYPTO_CIPHER_3DES_ECB]    = "3des-ecb",
82         [RTE_CRYPTO_CIPHER_3DES_CTR]    = "3des-ctr",
83
84         [RTE_CRYPTO_CIPHER_AES_CBC]     = "aes-cbc",
85         [RTE_CRYPTO_CIPHER_AES_CTR]     = "aes-ctr",
86         [RTE_CRYPTO_CIPHER_AES_DOCSISBPI]       = "aes-docsisbpi",
87         [RTE_CRYPTO_CIPHER_AES_ECB]     = "aes-ecb",
88         [RTE_CRYPTO_CIPHER_AES_F8]      = "aes-f8",
89         [RTE_CRYPTO_CIPHER_AES_XTS]     = "aes-xts",
90
91         [RTE_CRYPTO_CIPHER_ARC4]        = "arc4",
92
93         [RTE_CRYPTO_CIPHER_DES_CBC]     = "des-cbc",
94         [RTE_CRYPTO_CIPHER_DES_DOCSISBPI]       = "des-docsisbpi",
95
96         [RTE_CRYPTO_CIPHER_NULL]        = "null",
97
98         [RTE_CRYPTO_CIPHER_KASUMI_F8]   = "kasumi-f8",
99         [RTE_CRYPTO_CIPHER_SNOW3G_UEA2] = "snow3g-uea2",
100         [RTE_CRYPTO_CIPHER_ZUC_EEA3]    = "zuc-eea3"
101 };
102
103 /**
104  * The crypto cipher operation strings identifiers.
105  * It could be used in application command line.
106  */
107 const char *
108 rte_crypto_cipher_operation_strings[] = {
109                 [RTE_CRYPTO_CIPHER_OP_ENCRYPT]  = "encrypt",
110                 [RTE_CRYPTO_CIPHER_OP_DECRYPT]  = "decrypt"
111 };
112
113 /**
114  * The crypto auth algorithm strings identifiers.
115  * It could be used in application command line.
116  */
117 const char *
118 rte_crypto_auth_algorithm_strings[] = {
119         [RTE_CRYPTO_AUTH_AES_CBC_MAC]   = "aes-cbc-mac",
120         [RTE_CRYPTO_AUTH_AES_CMAC]      = "aes-cmac",
121         [RTE_CRYPTO_AUTH_AES_GMAC]      = "aes-gmac",
122         [RTE_CRYPTO_AUTH_AES_XCBC_MAC]  = "aes-xcbc-mac",
123
124         [RTE_CRYPTO_AUTH_MD5]           = "md5",
125         [RTE_CRYPTO_AUTH_MD5_HMAC]      = "md5-hmac",
126
127         [RTE_CRYPTO_AUTH_NULL]          = "null",
128
129         [RTE_CRYPTO_AUTH_SHA1]          = "sha1",
130         [RTE_CRYPTO_AUTH_SHA1_HMAC]     = "sha1-hmac",
131
132         [RTE_CRYPTO_AUTH_SHA224]        = "sha2-224",
133         [RTE_CRYPTO_AUTH_SHA224_HMAC]   = "sha2-224-hmac",
134         [RTE_CRYPTO_AUTH_SHA256]        = "sha2-256",
135         [RTE_CRYPTO_AUTH_SHA256_HMAC]   = "sha2-256-hmac",
136         [RTE_CRYPTO_AUTH_SHA384]        = "sha2-384",
137         [RTE_CRYPTO_AUTH_SHA384_HMAC]   = "sha2-384-hmac",
138         [RTE_CRYPTO_AUTH_SHA512]        = "sha2-512",
139         [RTE_CRYPTO_AUTH_SHA512_HMAC]   = "sha2-512-hmac",
140
141         [RTE_CRYPTO_AUTH_KASUMI_F9]     = "kasumi-f9",
142         [RTE_CRYPTO_AUTH_SNOW3G_UIA2]   = "snow3g-uia2",
143         [RTE_CRYPTO_AUTH_ZUC_EIA3]      = "zuc-eia3"
144 };
145
146 /**
147  * The crypto AEAD algorithm strings identifiers.
148  * It could be used in application command line.
149  */
150 const char *
151 rte_crypto_aead_algorithm_strings[] = {
152         [RTE_CRYPTO_AEAD_AES_CCM]       = "aes-ccm",
153         [RTE_CRYPTO_AEAD_AES_GCM]       = "aes-gcm",
154 };
155
156 /**
157  * The crypto AEAD operation strings identifiers.
158  * It could be used in application command line.
159  */
160 const char *
161 rte_crypto_aead_operation_strings[] = {
162         [RTE_CRYPTO_AEAD_OP_ENCRYPT]    = "encrypt",
163         [RTE_CRYPTO_AEAD_OP_DECRYPT]    = "decrypt"
164 };
165
166 /**
167  * Asymmetric crypto transform operation strings identifiers.
168  */
169 const char *rte_crypto_asym_xform_strings[] = {
170         [RTE_CRYPTO_ASYM_XFORM_NONE]    = "none",
171         [RTE_CRYPTO_ASYM_XFORM_RSA]     = "rsa",
172         [RTE_CRYPTO_ASYM_XFORM_MODEX]   = "modexp",
173         [RTE_CRYPTO_ASYM_XFORM_MODINV]  = "modinv",
174         [RTE_CRYPTO_ASYM_XFORM_DH]      = "dh",
175         [RTE_CRYPTO_ASYM_XFORM_DSA]     = "dsa",
176         [RTE_CRYPTO_ASYM_XFORM_ECDSA]   = "ecdsa",
177         [RTE_CRYPTO_ASYM_XFORM_ECPM]    = "ecpm",
178 };
179
180 /**
181  * Asymmetric crypto operation strings identifiers.
182  */
183 const char *rte_crypto_asym_op_strings[] = {
184         [RTE_CRYPTO_ASYM_OP_ENCRYPT]    = "encrypt",
185         [RTE_CRYPTO_ASYM_OP_DECRYPT]    = "decrypt",
186         [RTE_CRYPTO_ASYM_OP_SIGN]       = "sign",
187         [RTE_CRYPTO_ASYM_OP_VERIFY]     = "verify",
188         [RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE]       = "priv_key_generate",
189         [RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE] = "pub_key_generate",
190         [RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE] = "sharedsecret_compute",
191 };
192
193 /**
194  * The private data structure stored in the session mempool private data.
195  */
196 struct rte_cryptodev_sym_session_pool_private_data {
197         uint16_t nb_drivers;
198         /**< number of elements in sess_data array */
199         uint16_t user_data_sz;
200         /**< session user data will be placed after sess_data */
201 };
202
203 int
204 rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
205                 const char *algo_string)
206 {
207         unsigned int i;
208
209         for (i = 1; i < RTE_DIM(rte_crypto_cipher_algorithm_strings); i++) {
210                 if (strcmp(algo_string, rte_crypto_cipher_algorithm_strings[i]) == 0) {
211                         *algo_enum = (enum rte_crypto_cipher_algorithm) i;
212                         return 0;
213                 }
214         }
215
216         /* Invalid string */
217         return -1;
218 }
219
220 int
221 rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
222                 const char *algo_string)
223 {
224         unsigned int i;
225
226         for (i = 1; i < RTE_DIM(rte_crypto_auth_algorithm_strings); i++) {
227                 if (strcmp(algo_string, rte_crypto_auth_algorithm_strings[i]) == 0) {
228                         *algo_enum = (enum rte_crypto_auth_algorithm) i;
229                         return 0;
230                 }
231         }
232
233         /* Invalid string */
234         return -1;
235 }
236
237 int
238 rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
239                 const char *algo_string)
240 {
241         unsigned int i;
242
243         for (i = 1; i < RTE_DIM(rte_crypto_aead_algorithm_strings); i++) {
244                 if (strcmp(algo_string, rte_crypto_aead_algorithm_strings[i]) == 0) {
245                         *algo_enum = (enum rte_crypto_aead_algorithm) i;
246                         return 0;
247                 }
248         }
249
250         /* Invalid string */
251         return -1;
252 }
253
254 int
255 rte_cryptodev_asym_get_xform_enum(enum rte_crypto_asym_xform_type *xform_enum,
256                 const char *xform_string)
257 {
258         unsigned int i;
259
260         for (i = 1; i < RTE_DIM(rte_crypto_asym_xform_strings); i++) {
261                 if (strcmp(xform_string,
262                         rte_crypto_asym_xform_strings[i]) == 0) {
263                         *xform_enum = (enum rte_crypto_asym_xform_type) i;
264                         return 0;
265                 }
266         }
267
268         /* Invalid string */
269         return -1;
270 }
271
272 /**
273  * The crypto auth operation strings identifiers.
274  * It could be used in application command line.
275  */
276 const char *
277 rte_crypto_auth_operation_strings[] = {
278                 [RTE_CRYPTO_AUTH_OP_VERIFY]     = "verify",
279                 [RTE_CRYPTO_AUTH_OP_GENERATE]   = "generate"
280 };
281
282 const struct rte_cryptodev_symmetric_capability *
283 rte_cryptodev_sym_capability_get(uint8_t dev_id,
284                 const struct rte_cryptodev_sym_capability_idx *idx)
285 {
286         const struct rte_cryptodev_capabilities *capability;
287         struct rte_cryptodev_info dev_info;
288         int i = 0;
289
290         rte_cryptodev_info_get(dev_id, &dev_info);
291
292         while ((capability = &dev_info.capabilities[i++])->op !=
293                         RTE_CRYPTO_OP_TYPE_UNDEFINED) {
294                 if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
295                         continue;
296
297                 if (capability->sym.xform_type != idx->type)
298                         continue;
299
300                 if (idx->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
301                         capability->sym.auth.algo == idx->algo.auth)
302                         return &capability->sym;
303
304                 if (idx->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
305                         capability->sym.cipher.algo == idx->algo.cipher)
306                         return &capability->sym;
307
308                 if (idx->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
309                                 capability->sym.aead.algo == idx->algo.aead)
310                         return &capability->sym;
311         }
312
313         return NULL;
314
315 }
316
317 static int
318 param_range_check(uint16_t size, const struct rte_crypto_param_range *range)
319 {
320         unsigned int next_size;
321
322         /* Check lower/upper bounds */
323         if (size < range->min)
324                 return -1;
325
326         if (size > range->max)
327                 return -1;
328
329         /* If range is actually only one value, size is correct */
330         if (range->increment == 0)
331                 return 0;
332
333         /* Check if value is one of the supported sizes */
334         for (next_size = range->min; next_size <= range->max;
335                         next_size += range->increment)
336                 if (size == next_size)
337                         return 0;
338
339         return -1;
340 }
341
342 const struct rte_cryptodev_asymmetric_xform_capability *
343 rte_cryptodev_asym_capability_get(uint8_t dev_id,
344                 const struct rte_cryptodev_asym_capability_idx *idx)
345 {
346         const struct rte_cryptodev_capabilities *capability;
347         struct rte_cryptodev_info dev_info;
348         unsigned int i = 0;
349
350         memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
351         rte_cryptodev_info_get(dev_id, &dev_info);
352
353         while ((capability = &dev_info.capabilities[i++])->op !=
354                         RTE_CRYPTO_OP_TYPE_UNDEFINED) {
355                 if (capability->op != RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
356                         continue;
357
358                 if (capability->asym.xform_capa.xform_type == idx->type)
359                         return &capability->asym.xform_capa;
360         }
361         return NULL;
362 };
363
364 int
365 rte_cryptodev_sym_capability_check_cipher(
366                 const struct rte_cryptodev_symmetric_capability *capability,
367                 uint16_t key_size, uint16_t iv_size)
368 {
369         if (param_range_check(key_size, &capability->cipher.key_size) != 0)
370                 return -1;
371
372         if (param_range_check(iv_size, &capability->cipher.iv_size) != 0)
373                 return -1;
374
375         return 0;
376 }
377
378 int
379 rte_cryptodev_sym_capability_check_auth(
380                 const struct rte_cryptodev_symmetric_capability *capability,
381                 uint16_t key_size, uint16_t digest_size, uint16_t iv_size)
382 {
383         if (param_range_check(key_size, &capability->auth.key_size) != 0)
384                 return -1;
385
386         if (param_range_check(digest_size, &capability->auth.digest_size) != 0)
387                 return -1;
388
389         if (param_range_check(iv_size, &capability->auth.iv_size) != 0)
390                 return -1;
391
392         return 0;
393 }
394
395 int
396 rte_cryptodev_sym_capability_check_aead(
397                 const struct rte_cryptodev_symmetric_capability *capability,
398                 uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
399                 uint16_t iv_size)
400 {
401         if (param_range_check(key_size, &capability->aead.key_size) != 0)
402                 return -1;
403
404         if (param_range_check(digest_size, &capability->aead.digest_size) != 0)
405                 return -1;
406
407         if (param_range_check(aad_size, &capability->aead.aad_size) != 0)
408                 return -1;
409
410         if (param_range_check(iv_size, &capability->aead.iv_size) != 0)
411                 return -1;
412
413         return 0;
414 }
415 int
416 rte_cryptodev_asym_xform_capability_check_optype(
417         const struct rte_cryptodev_asymmetric_xform_capability *capability,
418         enum rte_crypto_asym_op_type op_type)
419 {
420         if (capability->op_types & (1 << op_type))
421                 return 1;
422
423         return 0;
424 }
425
426 int
427 rte_cryptodev_asym_xform_capability_check_modlen(
428         const struct rte_cryptodev_asymmetric_xform_capability *capability,
429         uint16_t modlen)
430 {
431         /* no need to check for limits, if min or max = 0 */
432         if (capability->modlen.min != 0) {
433                 if (modlen < capability->modlen.min)
434                         return -1;
435         }
436
437         if (capability->modlen.max != 0) {
438                 if (modlen > capability->modlen.max)
439                         return -1;
440         }
441
442         /* in any case, check if given modlen is module increment */
443         if (capability->modlen.increment != 0) {
444                 if (modlen % (capability->modlen.increment))
445                         return -1;
446         }
447
448         return 0;
449 }
450
451
452 const char *
453 rte_cryptodev_get_feature_name(uint64_t flag)
454 {
455         switch (flag) {
456         case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
457                 return "SYMMETRIC_CRYPTO";
458         case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
459                 return "ASYMMETRIC_CRYPTO";
460         case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
461                 return "SYM_OPERATION_CHAINING";
462         case RTE_CRYPTODEV_FF_CPU_SSE:
463                 return "CPU_SSE";
464         case RTE_CRYPTODEV_FF_CPU_AVX:
465                 return "CPU_AVX";
466         case RTE_CRYPTODEV_FF_CPU_AVX2:
467                 return "CPU_AVX2";
468         case RTE_CRYPTODEV_FF_CPU_AVX512:
469                 return "CPU_AVX512";
470         case RTE_CRYPTODEV_FF_CPU_AESNI:
471                 return "CPU_AESNI";
472         case RTE_CRYPTODEV_FF_HW_ACCELERATED:
473                 return "HW_ACCELERATED";
474         case RTE_CRYPTODEV_FF_IN_PLACE_SGL:
475                 return "IN_PLACE_SGL";
476         case RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT:
477                 return "OOP_SGL_IN_SGL_OUT";
478         case RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT:
479                 return "OOP_SGL_IN_LB_OUT";
480         case RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT:
481                 return "OOP_LB_IN_SGL_OUT";
482         case RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT:
483                 return "OOP_LB_IN_LB_OUT";
484         case RTE_CRYPTODEV_FF_CPU_NEON:
485                 return "CPU_NEON";
486         case RTE_CRYPTODEV_FF_CPU_ARM_CE:
487                 return "CPU_ARM_CE";
488         case RTE_CRYPTODEV_FF_SECURITY:
489                 return "SECURITY_PROTOCOL";
490         case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP:
491                 return "RSA_PRIV_OP_KEY_EXP";
492         case RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT:
493                 return "RSA_PRIV_OP_KEY_QT";
494         case RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED:
495                 return "DIGEST_ENCRYPTED";
496         case RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO:
497                 return "SYM_CPU_CRYPTO";
498         default:
499                 return NULL;
500         }
501 }
502
503 struct rte_cryptodev *
504 rte_cryptodev_pmd_get_dev(uint8_t dev_id)
505 {
506         return &cryptodev_globals.devs[dev_id];
507 }
508
509 struct rte_cryptodev *
510 rte_cryptodev_pmd_get_named_dev(const char *name)
511 {
512         struct rte_cryptodev *dev;
513         unsigned int i;
514
515         if (name == NULL)
516                 return NULL;
517
518         for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
519                 dev = &cryptodev_globals.devs[i];
520
521                 if ((dev->attached == RTE_CRYPTODEV_ATTACHED) &&
522                                 (strcmp(dev->data->name, name) == 0))
523                         return dev;
524         }
525
526         return NULL;
527 }
528
529 static inline uint8_t
530 rte_cryptodev_is_valid_device_data(uint8_t dev_id)
531 {
532         if (rte_crypto_devices[dev_id].data == NULL)
533                 return 0;
534
535         return 1;
536 }
537
538 unsigned int
539 rte_cryptodev_pmd_is_valid_dev(uint8_t dev_id)
540 {
541         struct rte_cryptodev *dev = NULL;
542
543         if (!rte_cryptodev_is_valid_device_data(dev_id))
544                 return 0;
545
546         dev = rte_cryptodev_pmd_get_dev(dev_id);
547         if (dev->attached != RTE_CRYPTODEV_ATTACHED)
548                 return 0;
549         else
550                 return 1;
551 }
552
553
554 int
555 rte_cryptodev_get_dev_id(const char *name)
556 {
557         unsigned i;
558
559         if (name == NULL)
560                 return -1;
561
562         for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
563                 if (!rte_cryptodev_is_valid_device_data(i))
564                         continue;
565                 if ((strcmp(cryptodev_globals.devs[i].data->name, name)
566                                 == 0) &&
567                                 (cryptodev_globals.devs[i].attached ==
568                                                 RTE_CRYPTODEV_ATTACHED))
569                         return i;
570         }
571
572         return -1;
573 }
574
575 uint8_t
576 rte_cryptodev_count(void)
577 {
578         return cryptodev_globals.nb_devs;
579 }
580
581 uint8_t
582 rte_cryptodev_device_count_by_driver(uint8_t driver_id)
583 {
584         uint8_t i, dev_count = 0;
585
586         for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++)
587                 if (cryptodev_globals.devs[i].driver_id == driver_id &&
588                         cryptodev_globals.devs[i].attached ==
589                                         RTE_CRYPTODEV_ATTACHED)
590                         dev_count++;
591
592         return dev_count;
593 }
594
595 uint8_t
596 rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
597         uint8_t nb_devices)
598 {
599         uint8_t i, count = 0;
600         struct rte_cryptodev *devs = cryptodev_globals.devs;
601
602         for (i = 0; i < RTE_CRYPTO_MAX_DEVS && count < nb_devices; i++) {
603                 if (!rte_cryptodev_is_valid_device_data(i))
604                         continue;
605
606                 if (devs[i].attached == RTE_CRYPTODEV_ATTACHED) {
607                         int cmp;
608
609                         cmp = strncmp(devs[i].device->driver->name,
610                                         driver_name,
611                                         strlen(driver_name) + 1);
612
613                         if (cmp == 0)
614                                 devices[count++] = devs[i].data->dev_id;
615                 }
616         }
617
618         return count;
619 }
620
621 void *
622 rte_cryptodev_get_sec_ctx(uint8_t dev_id)
623 {
624         if (rte_crypto_devices[dev_id].feature_flags &
625                         RTE_CRYPTODEV_FF_SECURITY)
626                 return rte_crypto_devices[dev_id].security_ctx;
627
628         return NULL;
629 }
630
631 int
632 rte_cryptodev_socket_id(uint8_t dev_id)
633 {
634         struct rte_cryptodev *dev;
635
636         if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
637                 return -1;
638
639         dev = rte_cryptodev_pmd_get_dev(dev_id);
640
641         return dev->data->socket_id;
642 }
643
644 static inline int
645 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
646                 int socket_id)
647 {
648         char mz_name[RTE_MEMZONE_NAMESIZE];
649         const struct rte_memzone *mz;
650         int n;
651
652         /* generate memzone name */
653         n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
654         if (n >= (int)sizeof(mz_name))
655                 return -EINVAL;
656
657         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
658                 mz = rte_memzone_reserve(mz_name,
659                                 sizeof(struct rte_cryptodev_data),
660                                 socket_id, 0);
661         } else
662                 mz = rte_memzone_lookup(mz_name);
663
664         if (mz == NULL)
665                 return -ENOMEM;
666
667         *data = mz->addr;
668         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
669                 memset(*data, 0, sizeof(struct rte_cryptodev_data));
670
671         return 0;
672 }
673
674 static inline int
675 rte_cryptodev_data_free(uint8_t dev_id, struct rte_cryptodev_data **data)
676 {
677         char mz_name[RTE_MEMZONE_NAMESIZE];
678         const struct rte_memzone *mz;
679         int n;
680
681         /* generate memzone name */
682         n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
683         if (n >= (int)sizeof(mz_name))
684                 return -EINVAL;
685
686         mz = rte_memzone_lookup(mz_name);
687         if (mz == NULL)
688                 return -ENOMEM;
689
690         RTE_ASSERT(*data == mz->addr);
691         *data = NULL;
692
693         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
694                 return rte_memzone_free(mz);
695
696         return 0;
697 }
698
699 static uint8_t
700 rte_cryptodev_find_free_device_index(void)
701 {
702         uint8_t dev_id;
703
704         for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
705                 if (rte_crypto_devices[dev_id].attached ==
706                                 RTE_CRYPTODEV_DETACHED)
707                         return dev_id;
708         }
709         return RTE_CRYPTO_MAX_DEVS;
710 }
711
712 struct rte_cryptodev *
713 rte_cryptodev_pmd_allocate(const char *name, int socket_id)
714 {
715         struct rte_cryptodev *cryptodev;
716         uint8_t dev_id;
717
718         if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
719                 CDEV_LOG_ERR("Crypto device with name %s already "
720                                 "allocated!", name);
721                 return NULL;
722         }
723
724         dev_id = rte_cryptodev_find_free_device_index();
725         if (dev_id == RTE_CRYPTO_MAX_DEVS) {
726                 CDEV_LOG_ERR("Reached maximum number of crypto devices");
727                 return NULL;
728         }
729
730         cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
731
732         if (cryptodev->data == NULL) {
733                 struct rte_cryptodev_data **cryptodev_data =
734                                 &cryptodev_globals.data[dev_id];
735
736                 int retval = rte_cryptodev_data_alloc(dev_id, cryptodev_data,
737                                 socket_id);
738
739                 if (retval < 0 || *cryptodev_data == NULL)
740                         return NULL;
741
742                 cryptodev->data = *cryptodev_data;
743
744                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
745                         strlcpy(cryptodev->data->name, name,
746                                 RTE_CRYPTODEV_NAME_MAX_LEN);
747
748                         cryptodev->data->dev_id = dev_id;
749                         cryptodev->data->socket_id = socket_id;
750                         cryptodev->data->dev_started = 0;
751                 }
752
753                 /* init user callbacks */
754                 TAILQ_INIT(&(cryptodev->link_intr_cbs));
755
756                 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
757
758                 cryptodev_globals.nb_devs++;
759         }
760
761         return cryptodev;
762 }
763
764 int
765 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
766 {
767         int ret;
768         uint8_t dev_id;
769
770         if (cryptodev == NULL)
771                 return -EINVAL;
772
773         dev_id = cryptodev->data->dev_id;
774
775         /* Close device only if device operations have been set */
776         if (cryptodev->dev_ops) {
777                 ret = rte_cryptodev_close(dev_id);
778                 if (ret < 0)
779                         return ret;
780         }
781
782         ret = rte_cryptodev_data_free(dev_id, &cryptodev_globals.data[dev_id]);
783         if (ret < 0)
784                 return ret;
785
786         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
787         cryptodev_globals.nb_devs--;
788         return 0;
789 }
790
791 uint16_t
792 rte_cryptodev_queue_pair_count(uint8_t dev_id)
793 {
794         struct rte_cryptodev *dev;
795
796         dev = &rte_crypto_devices[dev_id];
797         return dev->data->nb_queue_pairs;
798 }
799
800 static int
801 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
802                 int socket_id)
803 {
804         struct rte_cryptodev_info dev_info;
805         void **qp;
806         unsigned i;
807
808         if ((dev == NULL) || (nb_qpairs < 1)) {
809                 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
810                                                         dev, nb_qpairs);
811                 return -EINVAL;
812         }
813
814         CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
815                         nb_qpairs, dev->data->dev_id);
816
817         memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
818
819         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
820         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
821
822         if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
823                 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
824                                 nb_qpairs, dev->data->dev_id);
825             return -EINVAL;
826         }
827
828         if (dev->data->queue_pairs == NULL) { /* first time configuration */
829                 dev->data->queue_pairs = rte_zmalloc_socket(
830                                 "cryptodev->queue_pairs",
831                                 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
832                                 RTE_CACHE_LINE_SIZE, socket_id);
833
834                 if (dev->data->queue_pairs == NULL) {
835                         dev->data->nb_queue_pairs = 0;
836                         CDEV_LOG_ERR("failed to get memory for qp meta data, "
837                                                         "nb_queues %u",
838                                                         nb_qpairs);
839                         return -(ENOMEM);
840                 }
841         } else { /* re-configure */
842                 int ret;
843                 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
844
845                 qp = dev->data->queue_pairs;
846
847                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
848                                 -ENOTSUP);
849
850                 for (i = nb_qpairs; i < old_nb_queues; i++) {
851                         ret = (*dev->dev_ops->queue_pair_release)(dev, i);
852                         if (ret < 0)
853                                 return ret;
854                 }
855
856                 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
857                                 RTE_CACHE_LINE_SIZE);
858                 if (qp == NULL) {
859                         CDEV_LOG_ERR("failed to realloc qp meta data,"
860                                                 " nb_queues %u", nb_qpairs);
861                         return -(ENOMEM);
862                 }
863
864                 if (nb_qpairs > old_nb_queues) {
865                         uint16_t new_qs = nb_qpairs - old_nb_queues;
866
867                         memset(qp + old_nb_queues, 0,
868                                 sizeof(qp[0]) * new_qs);
869                 }
870
871                 dev->data->queue_pairs = qp;
872
873         }
874         dev->data->nb_queue_pairs = nb_qpairs;
875         return 0;
876 }
877
878 int
879 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
880 {
881         struct rte_cryptodev *dev;
882         int diag;
883
884         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
885                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
886                 return -EINVAL;
887         }
888
889         dev = &rte_crypto_devices[dev_id];
890
891         if (dev->data->dev_started) {
892                 CDEV_LOG_ERR(
893                     "device %d must be stopped to allow configuration", dev_id);
894                 return -EBUSY;
895         }
896
897         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
898
899         /* Setup new number of queue pairs and reconfigure device. */
900         diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
901                         config->socket_id);
902         if (diag != 0) {
903                 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
904                                 dev_id, diag);
905                 return diag;
906         }
907
908         return (*dev->dev_ops->dev_configure)(dev, config);
909 }
910
911
912 int
913 rte_cryptodev_start(uint8_t dev_id)
914 {
915         struct rte_cryptodev *dev;
916         int diag;
917
918         CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
919
920         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
921                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
922                 return -EINVAL;
923         }
924
925         dev = &rte_crypto_devices[dev_id];
926
927         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
928
929         if (dev->data->dev_started != 0) {
930                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
931                         dev_id);
932                 return 0;
933         }
934
935         diag = (*dev->dev_ops->dev_start)(dev);
936         if (diag == 0)
937                 dev->data->dev_started = 1;
938         else
939                 return diag;
940
941         return 0;
942 }
943
944 void
945 rte_cryptodev_stop(uint8_t dev_id)
946 {
947         struct rte_cryptodev *dev;
948
949         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
950                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
951                 return;
952         }
953
954         dev = &rte_crypto_devices[dev_id];
955
956         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
957
958         if (dev->data->dev_started == 0) {
959                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
960                         dev_id);
961                 return;
962         }
963
964         (*dev->dev_ops->dev_stop)(dev);
965         dev->data->dev_started = 0;
966 }
967
968 int
969 rte_cryptodev_close(uint8_t dev_id)
970 {
971         struct rte_cryptodev *dev;
972         int retval;
973
974         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
975                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
976                 return -1;
977         }
978
979         dev = &rte_crypto_devices[dev_id];
980
981         /* Device must be stopped before it can be closed */
982         if (dev->data->dev_started == 1) {
983                 CDEV_LOG_ERR("Device %u must be stopped before closing",
984                                 dev_id);
985                 return -EBUSY;
986         }
987
988         /* We can't close the device if there are outstanding sessions in use */
989         if (dev->data->session_pool != NULL) {
990                 if (!rte_mempool_full(dev->data->session_pool)) {
991                         CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
992                                         "has sessions still in use, free "
993                                         "all sessions before calling close",
994                                         (unsigned)dev_id);
995                         return -EBUSY;
996                 }
997         }
998
999         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
1000         retval = (*dev->dev_ops->dev_close)(dev);
1001
1002         if (retval < 0)
1003                 return retval;
1004
1005         return 0;
1006 }
1007
1008 int
1009 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
1010                 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
1011
1012 {
1013         struct rte_cryptodev *dev;
1014
1015         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1016                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1017                 return -EINVAL;
1018         }
1019
1020         dev = &rte_crypto_devices[dev_id];
1021         if (queue_pair_id >= dev->data->nb_queue_pairs) {
1022                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
1023                 return -EINVAL;
1024         }
1025
1026         if (!qp_conf) {
1027                 CDEV_LOG_ERR("qp_conf cannot be NULL\n");
1028                 return -EINVAL;
1029         }
1030
1031         if ((qp_conf->mp_session && !qp_conf->mp_session_private) ||
1032                         (!qp_conf->mp_session && qp_conf->mp_session_private)) {
1033                 CDEV_LOG_ERR("Invalid mempools\n");
1034                 return -EINVAL;
1035         }
1036
1037         if (qp_conf->mp_session) {
1038                 struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1039                 uint32_t obj_size = qp_conf->mp_session->elt_size;
1040                 uint32_t obj_priv_size = qp_conf->mp_session_private->elt_size;
1041                 struct rte_cryptodev_sym_session s = {0};
1042
1043                 pool_priv = rte_mempool_get_priv(qp_conf->mp_session);
1044                 if (!pool_priv || qp_conf->mp_session->private_data_size <
1045                                 sizeof(*pool_priv)) {
1046                         CDEV_LOG_ERR("Invalid mempool\n");
1047                         return -EINVAL;
1048                 }
1049
1050                 s.nb_drivers = pool_priv->nb_drivers;
1051                 s.user_data_sz = pool_priv->user_data_sz;
1052
1053                 if ((rte_cryptodev_sym_get_existing_header_session_size(&s) >
1054                         obj_size) || (s.nb_drivers <= dev->driver_id) ||
1055                         rte_cryptodev_sym_get_private_session_size(dev_id) >
1056                                 obj_priv_size) {
1057                         CDEV_LOG_ERR("Invalid mempool\n");
1058                         return -EINVAL;
1059                 }
1060         }
1061
1062         if (dev->data->dev_started) {
1063                 CDEV_LOG_ERR(
1064                     "device %d must be stopped to allow configuration", dev_id);
1065                 return -EBUSY;
1066         }
1067
1068         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
1069
1070         return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
1071                         socket_id);
1072 }
1073
1074
1075 int
1076 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
1077 {
1078         struct rte_cryptodev *dev;
1079
1080         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1081                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1082                 return -ENODEV;
1083         }
1084
1085         if (stats == NULL) {
1086                 CDEV_LOG_ERR("Invalid stats ptr");
1087                 return -EINVAL;
1088         }
1089
1090         dev = &rte_crypto_devices[dev_id];
1091         memset(stats, 0, sizeof(*stats));
1092
1093         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1094         (*dev->dev_ops->stats_get)(dev, stats);
1095         return 0;
1096 }
1097
1098 void
1099 rte_cryptodev_stats_reset(uint8_t dev_id)
1100 {
1101         struct rte_cryptodev *dev;
1102
1103         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1104                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1105                 return;
1106         }
1107
1108         dev = &rte_crypto_devices[dev_id];
1109
1110         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
1111         (*dev->dev_ops->stats_reset)(dev);
1112 }
1113
1114
1115 void
1116 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
1117 {
1118         struct rte_cryptodev *dev;
1119
1120         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1121                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1122                 return;
1123         }
1124
1125         dev = &rte_crypto_devices[dev_id];
1126
1127         memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
1128
1129         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1130         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1131
1132         dev_info->driver_name = dev->device->driver->name;
1133         dev_info->device = dev->device;
1134 }
1135
1136
1137 int
1138 rte_cryptodev_callback_register(uint8_t dev_id,
1139                         enum rte_cryptodev_event_type event,
1140                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1141 {
1142         struct rte_cryptodev *dev;
1143         struct rte_cryptodev_callback *user_cb;
1144
1145         if (!cb_fn)
1146                 return -EINVAL;
1147
1148         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1149                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1150                 return -EINVAL;
1151         }
1152
1153         dev = &rte_crypto_devices[dev_id];
1154         rte_spinlock_lock(&rte_cryptodev_cb_lock);
1155
1156         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
1157                 if (user_cb->cb_fn == cb_fn &&
1158                         user_cb->cb_arg == cb_arg &&
1159                         user_cb->event == event) {
1160                         break;
1161                 }
1162         }
1163
1164         /* create a new callback. */
1165         if (user_cb == NULL) {
1166                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
1167                                 sizeof(struct rte_cryptodev_callback), 0);
1168                 if (user_cb != NULL) {
1169                         user_cb->cb_fn = cb_fn;
1170                         user_cb->cb_arg = cb_arg;
1171                         user_cb->event = event;
1172                         TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
1173                 }
1174         }
1175
1176         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1177         return (user_cb == NULL) ? -ENOMEM : 0;
1178 }
1179
1180 int
1181 rte_cryptodev_callback_unregister(uint8_t dev_id,
1182                         enum rte_cryptodev_event_type event,
1183                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
1184 {
1185         int ret;
1186         struct rte_cryptodev *dev;
1187         struct rte_cryptodev_callback *cb, *next;
1188
1189         if (!cb_fn)
1190                 return -EINVAL;
1191
1192         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1193                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
1194                 return -EINVAL;
1195         }
1196
1197         dev = &rte_crypto_devices[dev_id];
1198         rte_spinlock_lock(&rte_cryptodev_cb_lock);
1199
1200         ret = 0;
1201         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
1202
1203                 next = TAILQ_NEXT(cb, next);
1204
1205                 if (cb->cb_fn != cb_fn || cb->event != event ||
1206                                 (cb->cb_arg != (void *)-1 &&
1207                                 cb->cb_arg != cb_arg))
1208                         continue;
1209
1210                 /*
1211                  * if this callback is not executing right now,
1212                  * then remove it.
1213                  */
1214                 if (cb->active == 0) {
1215                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
1216                         rte_free(cb);
1217                 } else {
1218                         ret = -EAGAIN;
1219                 }
1220         }
1221
1222         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1223         return ret;
1224 }
1225
1226 void
1227 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1228         enum rte_cryptodev_event_type event)
1229 {
1230         struct rte_cryptodev_callback *cb_lst;
1231         struct rte_cryptodev_callback dev_cb;
1232
1233         rte_spinlock_lock(&rte_cryptodev_cb_lock);
1234         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1235                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1236                         continue;
1237                 dev_cb = *cb_lst;
1238                 cb_lst->active = 1;
1239                 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1240                 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1241                                                 dev_cb.cb_arg);
1242                 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1243                 cb_lst->active = 0;
1244         }
1245         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1246 }
1247
1248
1249 int
1250 rte_cryptodev_sym_session_init(uint8_t dev_id,
1251                 struct rte_cryptodev_sym_session *sess,
1252                 struct rte_crypto_sym_xform *xforms,
1253                 struct rte_mempool *mp)
1254 {
1255         struct rte_cryptodev *dev;
1256         uint32_t sess_priv_sz = rte_cryptodev_sym_get_private_session_size(
1257                         dev_id);
1258         uint8_t index;
1259         int ret;
1260
1261         dev = rte_cryptodev_pmd_get_dev(dev_id);
1262
1263         if (sess == NULL || xforms == NULL || dev == NULL)
1264                 return -EINVAL;
1265
1266         if (mp->elt_size < sess_priv_sz)
1267                 return -EINVAL;
1268
1269         index = dev->driver_id;
1270         if (index >= sess->nb_drivers)
1271                 return -EINVAL;
1272
1273         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_configure, -ENOTSUP);
1274
1275         if (sess->sess_data[index].refcnt == 0) {
1276                 ret = dev->dev_ops->sym_session_configure(dev, xforms,
1277                                                         sess, mp);
1278                 if (ret < 0) {
1279                         CDEV_LOG_ERR(
1280                                 "dev_id %d failed to configure session details",
1281                                 dev_id);
1282                         return ret;
1283                 }
1284         }
1285
1286         sess->sess_data[index].refcnt++;
1287         return 0;
1288 }
1289
1290 int
1291 rte_cryptodev_asym_session_init(uint8_t dev_id,
1292                 struct rte_cryptodev_asym_session *sess,
1293                 struct rte_crypto_asym_xform *xforms,
1294                 struct rte_mempool *mp)
1295 {
1296         struct rte_cryptodev *dev;
1297         uint8_t index;
1298         int ret;
1299
1300         dev = rte_cryptodev_pmd_get_dev(dev_id);
1301
1302         if (sess == NULL || xforms == NULL || dev == NULL)
1303                 return -EINVAL;
1304
1305         index = dev->driver_id;
1306
1307         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_configure,
1308                                 -ENOTSUP);
1309
1310         if (sess->sess_private_data[index] == NULL) {
1311                 ret = dev->dev_ops->asym_session_configure(dev,
1312                                                         xforms,
1313                                                         sess, mp);
1314                 if (ret < 0) {
1315                         CDEV_LOG_ERR(
1316                                 "dev_id %d failed to configure session details",
1317                                 dev_id);
1318                         return ret;
1319                 }
1320         }
1321
1322         return 0;
1323 }
1324
1325 struct rte_mempool *
1326 rte_cryptodev_sym_session_pool_create(const char *name, uint32_t nb_elts,
1327         uint32_t elt_size, uint32_t cache_size, uint16_t user_data_size,
1328         int socket_id)
1329 {
1330         struct rte_mempool *mp;
1331         struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1332         uint32_t obj_sz;
1333
1334         obj_sz = rte_cryptodev_sym_get_header_session_size() + user_data_size;
1335         if (obj_sz > elt_size)
1336                 CDEV_LOG_INFO("elt_size %u is expanded to %u\n", elt_size,
1337                                 obj_sz);
1338         else
1339                 obj_sz = elt_size;
1340
1341         mp = rte_mempool_create(name, nb_elts, obj_sz, cache_size,
1342                         (uint32_t)(sizeof(*pool_priv)),
1343                         NULL, NULL, NULL, NULL,
1344                         socket_id, 0);
1345         if (mp == NULL) {
1346                 CDEV_LOG_ERR("%s(name=%s) failed, rte_errno=%d\n",
1347                         __func__, name, rte_errno);
1348                 return NULL;
1349         }
1350
1351         pool_priv = rte_mempool_get_priv(mp);
1352         if (!pool_priv) {
1353                 CDEV_LOG_ERR("%s(name=%s) failed to get private data\n",
1354                         __func__, name);
1355                 rte_mempool_free(mp);
1356                 return NULL;
1357         }
1358
1359         pool_priv->nb_drivers = nb_drivers;
1360         pool_priv->user_data_sz = user_data_size;
1361
1362         return mp;
1363 }
1364
1365 static unsigned int
1366 rte_cryptodev_sym_session_data_size(struct rte_cryptodev_sym_session *sess)
1367 {
1368         return (sizeof(sess->sess_data[0]) * sess->nb_drivers) +
1369                         sess->user_data_sz;
1370 }
1371
1372 struct rte_cryptodev_sym_session *
1373 rte_cryptodev_sym_session_create(struct rte_mempool *mp)
1374 {
1375         struct rte_cryptodev_sym_session *sess;
1376         struct rte_cryptodev_sym_session_pool_private_data *pool_priv;
1377
1378         if (!mp) {
1379                 CDEV_LOG_ERR("Invalid mempool\n");
1380                 return NULL;
1381         }
1382
1383         pool_priv = rte_mempool_get_priv(mp);
1384
1385         if (!pool_priv || mp->private_data_size < sizeof(*pool_priv)) {
1386                 CDEV_LOG_ERR("Invalid mempool\n");
1387                 return NULL;
1388         }
1389
1390         /* Allocate a session structure from the session pool */
1391         if (rte_mempool_get(mp, (void **)&sess)) {
1392                 CDEV_LOG_ERR("couldn't get object from session mempool");
1393                 return NULL;
1394         }
1395
1396         sess->nb_drivers = pool_priv->nb_drivers;
1397         sess->user_data_sz = pool_priv->user_data_sz;
1398         sess->opaque_data = 0;
1399
1400         /* Clear device session pointer.
1401          * Include the flag indicating presence of user data
1402          */
1403         memset(sess->sess_data, 0,
1404                         rte_cryptodev_sym_session_data_size(sess));
1405
1406         return sess;
1407 }
1408
1409 struct rte_cryptodev_asym_session *
1410 rte_cryptodev_asym_session_create(struct rte_mempool *mp)
1411 {
1412         struct rte_cryptodev_asym_session *sess;
1413
1414         /* Allocate a session structure from the session pool */
1415         if (rte_mempool_get(mp, (void **)&sess)) {
1416                 CDEV_LOG_ERR("couldn't get object from session mempool");
1417                 return NULL;
1418         }
1419
1420         /* Clear device session pointer.
1421          * Include the flag indicating presence of private data
1422          */
1423         memset(sess, 0, (sizeof(void *) * nb_drivers) + sizeof(uint8_t));
1424
1425         return sess;
1426 }
1427
1428 int
1429 rte_cryptodev_sym_session_clear(uint8_t dev_id,
1430                 struct rte_cryptodev_sym_session *sess)
1431 {
1432         struct rte_cryptodev *dev;
1433         uint8_t driver_id;
1434
1435         dev = rte_cryptodev_pmd_get_dev(dev_id);
1436
1437         if (dev == NULL || sess == NULL)
1438                 return -EINVAL;
1439
1440         driver_id = dev->driver_id;
1441         if (sess->sess_data[driver_id].refcnt == 0)
1442                 return 0;
1443         if (--sess->sess_data[driver_id].refcnt != 0)
1444                 return -EBUSY;
1445
1446         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->sym_session_clear, -ENOTSUP);
1447
1448         dev->dev_ops->sym_session_clear(dev, sess);
1449
1450         return 0;
1451 }
1452
1453 int
1454 rte_cryptodev_asym_session_clear(uint8_t dev_id,
1455                 struct rte_cryptodev_asym_session *sess)
1456 {
1457         struct rte_cryptodev *dev;
1458
1459         dev = rte_cryptodev_pmd_get_dev(dev_id);
1460
1461         if (dev == NULL || sess == NULL)
1462                 return -EINVAL;
1463
1464         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->asym_session_clear, -ENOTSUP);
1465
1466         dev->dev_ops->asym_session_clear(dev, sess);
1467
1468         return 0;
1469 }
1470
1471 int
1472 rte_cryptodev_sym_session_free(struct rte_cryptodev_sym_session *sess)
1473 {
1474         uint8_t i;
1475         struct rte_mempool *sess_mp;
1476
1477         if (sess == NULL)
1478                 return -EINVAL;
1479
1480         /* Check that all device private data has been freed */
1481         for (i = 0; i < sess->nb_drivers; i++) {
1482                 if (sess->sess_data[i].refcnt != 0)
1483                         return -EBUSY;
1484         }
1485
1486         /* Return session to mempool */
1487         sess_mp = rte_mempool_from_obj(sess);
1488         rte_mempool_put(sess_mp, sess);
1489
1490         return 0;
1491 }
1492
1493 int
1494 rte_cryptodev_asym_session_free(struct rte_cryptodev_asym_session *sess)
1495 {
1496         uint8_t i;
1497         void *sess_priv;
1498         struct rte_mempool *sess_mp;
1499
1500         if (sess == NULL)
1501                 return -EINVAL;
1502
1503         /* Check that all device private data has been freed */
1504         for (i = 0; i < nb_drivers; i++) {
1505                 sess_priv = get_asym_session_private_data(sess, i);
1506                 if (sess_priv != NULL)
1507                         return -EBUSY;
1508         }
1509
1510         /* Return session to mempool */
1511         sess_mp = rte_mempool_from_obj(sess);
1512         rte_mempool_put(sess_mp, sess);
1513
1514         return 0;
1515 }
1516
1517 unsigned int
1518 rte_cryptodev_sym_get_header_session_size(void)
1519 {
1520         /*
1521          * Header contains pointers to the private data of all registered
1522          * drivers and all necessary information to ensure safely clear
1523          * or free al session.
1524          */
1525         struct rte_cryptodev_sym_session s = {0};
1526
1527         s.nb_drivers = nb_drivers;
1528
1529         return (unsigned int)(sizeof(s) +
1530                         rte_cryptodev_sym_session_data_size(&s));
1531 }
1532
1533 unsigned int
1534 rte_cryptodev_sym_get_existing_header_session_size(
1535                 struct rte_cryptodev_sym_session *sess)
1536 {
1537         if (!sess)
1538                 return 0;
1539         else
1540                 return (unsigned int)(sizeof(*sess) +
1541                                 rte_cryptodev_sym_session_data_size(sess));
1542 }
1543
1544 unsigned int
1545 rte_cryptodev_asym_get_header_session_size(void)
1546 {
1547         /*
1548          * Header contains pointers to the private data
1549          * of all registered drivers, and a flag which
1550          * indicates presence of private data
1551          */
1552         return ((sizeof(void *) * nb_drivers) + sizeof(uint8_t));
1553 }
1554
1555 unsigned int
1556 rte_cryptodev_sym_get_private_session_size(uint8_t dev_id)
1557 {
1558         struct rte_cryptodev *dev;
1559         unsigned int priv_sess_size;
1560
1561         if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
1562                 return 0;
1563
1564         dev = rte_cryptodev_pmd_get_dev(dev_id);
1565
1566         if (*dev->dev_ops->sym_session_get_size == NULL)
1567                 return 0;
1568
1569         priv_sess_size = (*dev->dev_ops->sym_session_get_size)(dev);
1570
1571         return priv_sess_size;
1572 }
1573
1574 unsigned int
1575 rte_cryptodev_asym_get_private_session_size(uint8_t dev_id)
1576 {
1577         struct rte_cryptodev *dev;
1578         unsigned int header_size = sizeof(void *) * nb_drivers;
1579         unsigned int priv_sess_size;
1580
1581         if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
1582                 return 0;
1583
1584         dev = rte_cryptodev_pmd_get_dev(dev_id);
1585
1586         if (*dev->dev_ops->asym_session_get_size == NULL)
1587                 return 0;
1588
1589         priv_sess_size = (*dev->dev_ops->asym_session_get_size)(dev);
1590         if (priv_sess_size < header_size)
1591                 return header_size;
1592
1593         return priv_sess_size;
1594
1595 }
1596
1597 int
1598 rte_cryptodev_sym_session_set_user_data(
1599                                         struct rte_cryptodev_sym_session *sess,
1600                                         void *data,
1601                                         uint16_t size)
1602 {
1603         if (sess == NULL)
1604                 return -EINVAL;
1605
1606         if (sess->user_data_sz < size)
1607                 return -ENOMEM;
1608
1609         rte_memcpy(sess->sess_data + sess->nb_drivers, data, size);
1610         return 0;
1611 }
1612
1613 void *
1614 rte_cryptodev_sym_session_get_user_data(
1615                                         struct rte_cryptodev_sym_session *sess)
1616 {
1617         if (sess == NULL || sess->user_data_sz == 0)
1618                 return NULL;
1619
1620         return (void *)(sess->sess_data + sess->nb_drivers);
1621 }
1622
1623 static inline void
1624 sym_crypto_fill_status(struct rte_crypto_sym_vec *vec, int32_t errnum)
1625 {
1626         uint32_t i;
1627         for (i = 0; i < vec->num; i++)
1628                 vec->status[i] = errnum;
1629 }
1630
1631 uint32_t
1632 rte_cryptodev_sym_cpu_crypto_process(uint8_t dev_id,
1633         struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs ofs,
1634         struct rte_crypto_sym_vec *vec)
1635 {
1636         struct rte_cryptodev *dev;
1637
1638         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1639                 sym_crypto_fill_status(vec, EINVAL);
1640                 return 0;
1641         }
1642
1643         dev = rte_cryptodev_pmd_get_dev(dev_id);
1644
1645         if (*dev->dev_ops->sym_cpu_process == NULL ||
1646                 !(dev->feature_flags & RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO)) {
1647                 sym_crypto_fill_status(vec, ENOTSUP);
1648                 return 0;
1649         }
1650
1651         return dev->dev_ops->sym_cpu_process(dev, sess, ofs, vec);
1652 }
1653
1654 /** Initialise rte_crypto_op mempool element */
1655 static void
1656 rte_crypto_op_init(struct rte_mempool *mempool,
1657                 void *opaque_arg,
1658                 void *_op_data,
1659                 __rte_unused unsigned i)
1660 {
1661         struct rte_crypto_op *op = _op_data;
1662         enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1663
1664         memset(_op_data, 0, mempool->elt_size);
1665
1666         __rte_crypto_op_reset(op, type);
1667
1668         op->phys_addr = rte_mem_virt2iova(_op_data);
1669         op->mempool = mempool;
1670 }
1671
1672
1673 struct rte_mempool *
1674 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
1675                 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
1676                 int socket_id)
1677 {
1678         struct rte_crypto_op_pool_private *priv;
1679
1680         unsigned elt_size = sizeof(struct rte_crypto_op) +
1681                         priv_size;
1682
1683         if (type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
1684                 elt_size += sizeof(struct rte_crypto_sym_op);
1685         } else if (type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
1686                 elt_size += sizeof(struct rte_crypto_asym_op);
1687         } else if (type == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1688                 elt_size += RTE_MAX(sizeof(struct rte_crypto_sym_op),
1689                                     sizeof(struct rte_crypto_asym_op));
1690         } else {
1691                 CDEV_LOG_ERR("Invalid op_type\n");
1692                 return NULL;
1693         }
1694
1695         /* lookup mempool in case already allocated */
1696         struct rte_mempool *mp = rte_mempool_lookup(name);
1697
1698         if (mp != NULL) {
1699                 priv = (struct rte_crypto_op_pool_private *)
1700                                 rte_mempool_get_priv(mp);
1701
1702                 if (mp->elt_size != elt_size ||
1703                                 mp->cache_size < cache_size ||
1704                                 mp->size < nb_elts ||
1705                                 priv->priv_size <  priv_size) {
1706                         mp = NULL;
1707                         CDEV_LOG_ERR("Mempool %s already exists but with "
1708                                         "incompatible parameters", name);
1709                         return NULL;
1710                 }
1711                 return mp;
1712         }
1713
1714         mp = rte_mempool_create(
1715                         name,
1716                         nb_elts,
1717                         elt_size,
1718                         cache_size,
1719                         sizeof(struct rte_crypto_op_pool_private),
1720                         NULL,
1721                         NULL,
1722                         rte_crypto_op_init,
1723                         &type,
1724                         socket_id,
1725                         0);
1726
1727         if (mp == NULL) {
1728                 CDEV_LOG_ERR("Failed to create mempool %s", name);
1729                 return NULL;
1730         }
1731
1732         priv = (struct rte_crypto_op_pool_private *)
1733                         rte_mempool_get_priv(mp);
1734
1735         priv->priv_size = priv_size;
1736         priv->type = type;
1737
1738         return mp;
1739 }
1740
1741 int
1742 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix)
1743 {
1744         struct rte_cryptodev *dev = NULL;
1745         uint32_t i = 0;
1746
1747         if (name == NULL)
1748                 return -EINVAL;
1749
1750         for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) {
1751                 int ret = snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN,
1752                                 "%s_%u", dev_name_prefix, i);
1753
1754                 if (ret < 0)
1755                         return ret;
1756
1757                 dev = rte_cryptodev_pmd_get_named_dev(name);
1758                 if (!dev)
1759                         return 0;
1760         }
1761
1762         return -1;
1763 }
1764
1765 TAILQ_HEAD(cryptodev_driver_list, cryptodev_driver);
1766
1767 static struct cryptodev_driver_list cryptodev_driver_list =
1768         TAILQ_HEAD_INITIALIZER(cryptodev_driver_list);
1769
1770 int
1771 rte_cryptodev_driver_id_get(const char *name)
1772 {
1773         struct cryptodev_driver *driver;
1774         const char *driver_name;
1775
1776         if (name == NULL) {
1777                 RTE_LOG(DEBUG, CRYPTODEV, "name pointer NULL");
1778                 return -1;
1779         }
1780
1781         TAILQ_FOREACH(driver, &cryptodev_driver_list, next) {
1782                 driver_name = driver->driver->name;
1783                 if (strncmp(driver_name, name, strlen(driver_name) + 1) == 0)
1784                         return driver->id;
1785         }
1786         return -1;
1787 }
1788
1789 const char *
1790 rte_cryptodev_name_get(uint8_t dev_id)
1791 {
1792         struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(dev_id);
1793
1794         if (dev == NULL)
1795                 return NULL;
1796
1797         return dev->data->name;
1798 }
1799
1800 const char *
1801 rte_cryptodev_driver_name_get(uint8_t driver_id)
1802 {
1803         struct cryptodev_driver *driver;
1804
1805         TAILQ_FOREACH(driver, &cryptodev_driver_list, next)
1806                 if (driver->id == driver_id)
1807                         return driver->driver->name;
1808         return NULL;
1809 }
1810
1811 uint8_t
1812 rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
1813                 const struct rte_driver *drv)
1814 {
1815         crypto_drv->driver = drv;
1816         crypto_drv->id = nb_drivers;
1817
1818         TAILQ_INSERT_TAIL(&cryptodev_driver_list, crypto_drv, next);
1819
1820         return nb_drivers++;
1821 }