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