4 * Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
34 #include <sys/queue.h>
43 #include <netinet/in.h>
45 #include <rte_byteorder.h>
47 #include <rte_debug.h>
49 #include <rte_interrupts.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_launch.h>
55 #include <rte_tailq.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_common.h>
62 #include <rte_mempool.h>
63 #include <rte_malloc.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
67 #include <rte_string_fns.h>
69 #include "rte_crypto.h"
70 #include "rte_cryptodev.h"
71 #include "rte_cryptodev_pmd.h"
73 struct rte_cryptodev rte_crypto_devices[RTE_CRYPTO_MAX_DEVS];
75 struct rte_cryptodev *rte_cryptodevs = &rte_crypto_devices[0];
77 static struct rte_cryptodev_global cryptodev_globals = {
78 .devs = &rte_crypto_devices[0],
81 .max_devs = RTE_CRYPTO_MAX_DEVS
84 struct rte_cryptodev_global *rte_cryptodev_globals = &cryptodev_globals;
86 /* spinlock for crypto device callbacks */
87 static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
91 * The user application callback description.
93 * It contains callback address to be registered by user application,
94 * the pointer to the parameters for callback, and the event type.
96 struct rte_cryptodev_callback {
97 TAILQ_ENTRY(rte_cryptodev_callback) next; /**< Callbacks list */
98 rte_cryptodev_cb_fn cb_fn; /**< Callback address */
99 void *cb_arg; /**< Parameter for callback */
100 enum rte_cryptodev_event_type event; /**< Interrupt event type */
101 uint32_t active; /**< Callback is executing */
104 #define RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG ("max_nb_queue_pairs")
105 #define RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG ("max_nb_sessions")
106 #define RTE_CRYPTODEV_VDEV_SOCKET_ID ("socket_id")
108 static const char *cryptodev_vdev_valid_params[] = {
109 RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
110 RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
111 RTE_CRYPTODEV_VDEV_SOCKET_ID
115 number_of_sockets(void)
119 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
121 for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
122 if (sockets < ms[i].socket_id)
123 sockets = ms[i].socket_id;
126 /* Number of sockets = maximum socket_id + 1 */
130 /** Parse integer from integer argument */
132 parse_integer_arg(const char *key __rte_unused,
133 const char *value, void *extra_args)
135 int *i = (int *) extra_args;
139 CDEV_LOG_ERR("Argument has to be positive.");
147 rte_cryptodev_parse_vdev_init_params(struct rte_crypto_vdev_init_params *params,
148 const char *input_args)
150 struct rte_kvargs *kvlist = NULL;
157 kvlist = rte_kvargs_parse(input_args,
158 cryptodev_vdev_valid_params);
162 ret = rte_kvargs_process(kvlist,
163 RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
165 ¶ms->max_nb_queue_pairs);
169 ret = rte_kvargs_process(kvlist,
170 RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
172 ¶ms->max_nb_sessions);
176 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
182 if (params->socket_id >= number_of_sockets()) {
183 CDEV_LOG_ERR("Invalid socket id specified to create "
184 "the virtual crypto device on");
190 rte_kvargs_free(kvlist);
195 rte_cryptodev_get_feature_name(uint64_t flag)
198 case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
199 return "SYMMETRIC_CRYPTO";
200 case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
201 return "ASYMMETRIC_CRYPTO";
202 case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
203 return "SYM_OPERATION_CHAINING";
204 case RTE_CRYPTODEV_FF_CPU_SSE:
206 case RTE_CRYPTODEV_FF_CPU_AVX:
208 case RTE_CRYPTODEV_FF_CPU_AVX2:
210 case RTE_CRYPTODEV_FF_CPU_AESNI:
212 case RTE_CRYPTODEV_FF_HW_ACCELERATED:
213 return "HW_ACCELERATED";
222 rte_cryptodev_create_vdev(const char *name, const char *args)
224 return rte_eal_vdev_init(name, args);
228 rte_cryptodev_get_dev_id(const char *name) {
234 for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
235 if ((strcmp(rte_cryptodev_globals->devs[i].data->name, name)
237 (rte_cryptodev_globals->devs[i].attached ==
238 RTE_CRYPTODEV_ATTACHED))
245 rte_cryptodev_count(void)
247 return rte_cryptodev_globals->nb_devs;
251 rte_cryptodev_count_devtype(enum rte_cryptodev_type type)
253 uint8_t i, dev_count = 0;
255 for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
256 if (rte_cryptodev_globals->devs[i].dev_type == type &&
257 rte_cryptodev_globals->devs[i].attached ==
258 RTE_CRYPTODEV_ATTACHED)
265 rte_cryptodev_socket_id(uint8_t dev_id)
267 struct rte_cryptodev *dev;
269 if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
272 dev = rte_cryptodev_pmd_get_dev(dev_id);
274 return dev->data->socket_id;
278 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
281 char mz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
282 const struct rte_memzone *mz;
285 /* generate memzone name */
286 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
287 if (n >= (int)sizeof(mz_name))
290 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
291 mz = rte_memzone_reserve(mz_name,
292 sizeof(struct rte_cryptodev_data),
295 mz = rte_memzone_lookup(mz_name);
301 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
302 memset(*data, 0, sizeof(struct rte_cryptodev_data));
308 rte_cryptodev_find_free_device_index(void)
312 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
313 if (rte_crypto_devices[dev_id].attached ==
314 RTE_CRYPTODEV_DETACHED)
317 return RTE_CRYPTO_MAX_DEVS;
320 struct rte_cryptodev *
321 rte_cryptodev_pmd_allocate(const char *name, int socket_id)
323 struct rte_cryptodev *cryptodev;
326 if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
327 CDEV_LOG_ERR("Crypto device with name %s already "
332 dev_id = rte_cryptodev_find_free_device_index();
333 if (dev_id == RTE_CRYPTO_MAX_DEVS) {
334 CDEV_LOG_ERR("Reached maximum number of crypto devices");
338 cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
340 if (cryptodev->data == NULL) {
341 struct rte_cryptodev_data *cryptodev_data =
342 cryptodev_globals.data[dev_id];
344 int retval = rte_cryptodev_data_alloc(dev_id, &cryptodev_data,
347 if (retval < 0 || cryptodev_data == NULL)
350 cryptodev->data = cryptodev_data;
352 snprintf(cryptodev->data->name, RTE_CRYPTODEV_NAME_MAX_LEN,
355 cryptodev->data->dev_id = dev_id;
356 cryptodev->data->socket_id = socket_id;
357 cryptodev->data->dev_started = 0;
359 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
361 cryptodev_globals.nb_devs++;
368 rte_cryptodev_create_unique_device_name(char *name, size_t size,
369 struct rte_pci_device *pci_dev)
373 if ((name == NULL) || (pci_dev == NULL))
376 ret = snprintf(name, size, "%d:%d.%d",
377 pci_dev->addr.bus, pci_dev->addr.devid,
378 pci_dev->addr.function);
385 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
389 if (cryptodev == NULL)
392 ret = rte_cryptodev_close(cryptodev->data->dev_id);
396 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
397 cryptodev_globals.nb_devs--;
401 struct rte_cryptodev *
402 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
405 struct rte_cryptodev *cryptodev;
407 /* allocate device structure */
408 cryptodev = rte_cryptodev_pmd_allocate(name, socket_id);
409 if (cryptodev == NULL)
412 /* allocate private device structure */
413 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
414 cryptodev->data->dev_private =
415 rte_zmalloc_socket("cryptodev device private",
420 if (cryptodev->data->dev_private == NULL)
421 rte_panic("Cannot allocate memzone for private device"
425 /* initialise user call-back tail queue */
426 TAILQ_INIT(&(cryptodev->link_intr_cbs));
432 rte_cryptodev_pci_probe(struct rte_pci_driver *pci_drv,
433 struct rte_pci_device *pci_dev)
435 struct rte_cryptodev_driver *cryptodrv;
436 struct rte_cryptodev *cryptodev;
438 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
442 cryptodrv = (struct rte_cryptodev_driver *)pci_drv;
443 if (cryptodrv == NULL)
446 /* Create unique Crypto device name using PCI address */
447 rte_cryptodev_create_unique_device_name(cryptodev_name,
448 sizeof(cryptodev_name), pci_dev);
450 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
451 if (cryptodev == NULL)
454 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
455 cryptodev->data->dev_private =
457 "cryptodev private structure",
458 cryptodrv->dev_private_size,
462 if (cryptodev->data->dev_private == NULL)
463 rte_panic("Cannot allocate memzone for private "
467 cryptodev->pci_dev = pci_dev;
468 cryptodev->driver = cryptodrv;
470 /* init user callbacks */
471 TAILQ_INIT(&(cryptodev->link_intr_cbs));
473 /* Invoke PMD device initialization function */
474 retval = (*cryptodrv->cryptodev_init)(cryptodrv, cryptodev);
478 CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
479 " failed", pci_drv->name,
480 (unsigned) pci_dev->id.vendor_id,
481 (unsigned) pci_dev->id.device_id);
483 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
484 rte_free(cryptodev->data->dev_private);
486 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
487 cryptodev_globals.nb_devs--;
493 rte_cryptodev_pci_remove(struct rte_pci_device *pci_dev)
495 const struct rte_cryptodev_driver *cryptodrv;
496 struct rte_cryptodev *cryptodev;
497 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
503 /* Create unique device name using PCI address */
504 rte_cryptodev_create_unique_device_name(cryptodev_name,
505 sizeof(cryptodev_name), pci_dev);
507 cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
508 if (cryptodev == NULL)
511 cryptodrv = (const struct rte_cryptodev_driver *)pci_dev->driver;
512 if (cryptodrv == NULL)
515 /* Invoke PMD device uninit function */
516 if (*cryptodrv->cryptodev_uninit) {
517 ret = (*cryptodrv->cryptodev_uninit)(cryptodrv, cryptodev);
522 /* free crypto device */
523 rte_cryptodev_pmd_release_device(cryptodev);
525 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
526 rte_free(cryptodev->data->dev_private);
528 cryptodev->pci_dev = NULL;
529 cryptodev->driver = NULL;
530 cryptodev->data = NULL;
536 rte_cryptodev_pmd_driver_register(struct rte_cryptodev_driver *cryptodrv,
539 /* Call crypto device initialization directly if device is virtual */
540 if (type == PMD_VDEV)
541 return rte_cryptodev_pci_probe(
542 (struct rte_pci_driver *)cryptodrv,
546 * Register PCI driver for physical device intialisation during
549 cryptodrv->pci_drv.probe = rte_cryptodev_pci_probe;
550 cryptodrv->pci_drv.remove = rte_cryptodev_pci_remove;
552 rte_eal_pci_register(&cryptodrv->pci_drv);
559 rte_cryptodev_queue_pair_count(uint8_t dev_id)
561 struct rte_cryptodev *dev;
563 dev = &rte_crypto_devices[dev_id];
564 return dev->data->nb_queue_pairs;
568 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
571 struct rte_cryptodev_info dev_info;
575 if ((dev == NULL) || (nb_qpairs < 1)) {
576 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
581 CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
582 nb_qpairs, dev->data->dev_id);
584 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
586 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
587 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
589 if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
590 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
591 nb_qpairs, dev->data->dev_id);
595 if (dev->data->queue_pairs == NULL) { /* first time configuration */
596 dev->data->queue_pairs = rte_zmalloc_socket(
597 "cryptodev->queue_pairs",
598 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
599 RTE_CACHE_LINE_SIZE, socket_id);
601 if (dev->data->queue_pairs == NULL) {
602 dev->data->nb_queue_pairs = 0;
603 CDEV_LOG_ERR("failed to get memory for qp meta data, "
608 } else { /* re-configure */
610 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
612 qp = dev->data->queue_pairs;
614 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
617 for (i = nb_qpairs; i < old_nb_queues; i++) {
618 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
623 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
624 RTE_CACHE_LINE_SIZE);
626 CDEV_LOG_ERR("failed to realloc qp meta data,"
627 " nb_queues %u", nb_qpairs);
631 if (nb_qpairs > old_nb_queues) {
632 uint16_t new_qs = nb_qpairs - old_nb_queues;
634 memset(qp + old_nb_queues, 0,
635 sizeof(qp[0]) * new_qs);
638 dev->data->queue_pairs = qp;
641 dev->data->nb_queue_pairs = nb_qpairs;
646 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
648 struct rte_cryptodev *dev;
650 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
651 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
655 dev = &rte_crypto_devices[dev_id];
656 if (queue_pair_id >= dev->data->nb_queue_pairs) {
657 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
661 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
663 return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
668 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
670 struct rte_cryptodev *dev;
672 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
673 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
677 dev = &rte_crypto_devices[dev_id];
678 if (queue_pair_id >= dev->data->nb_queue_pairs) {
679 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
683 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
685 return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
690 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
691 unsigned nb_objs, unsigned obj_cache_size, int socket_id);
694 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
696 struct rte_cryptodev *dev;
699 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
700 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
704 dev = &rte_crypto_devices[dev_id];
706 if (dev->data->dev_started) {
708 "device %d must be stopped to allow configuration", dev_id);
712 /* Setup new number of queue pairs and reconfigure device. */
713 diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
716 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
721 /* Setup Session mempool for device */
722 return rte_cryptodev_sym_session_pool_create(dev,
723 config->session_mp.nb_objs,
724 config->session_mp.cache_size,
730 rte_cryptodev_start(uint8_t dev_id)
732 struct rte_cryptodev *dev;
735 CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
737 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
738 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
742 dev = &rte_crypto_devices[dev_id];
744 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
746 if (dev->data->dev_started != 0) {
747 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
752 diag = (*dev->dev_ops->dev_start)(dev);
754 dev->data->dev_started = 1;
762 rte_cryptodev_stop(uint8_t dev_id)
764 struct rte_cryptodev *dev;
766 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
767 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
771 dev = &rte_crypto_devices[dev_id];
773 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
775 if (dev->data->dev_started == 0) {
776 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
781 dev->data->dev_started = 0;
782 (*dev->dev_ops->dev_stop)(dev);
786 rte_cryptodev_close(uint8_t dev_id)
788 struct rte_cryptodev *dev;
791 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
792 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
796 dev = &rte_crypto_devices[dev_id];
798 /* Device must be stopped before it can be closed */
799 if (dev->data->dev_started == 1) {
800 CDEV_LOG_ERR("Device %u must be stopped before closing",
805 /* We can't close the device if there are outstanding sessions in use */
806 if (dev->data->session_pool != NULL) {
807 if (!rte_mempool_full(dev->data->session_pool)) {
808 CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
809 "has sessions still in use, free "
810 "all sessions before calling close",
816 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
817 retval = (*dev->dev_ops->dev_close)(dev);
826 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
827 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
829 struct rte_cryptodev *dev;
831 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
832 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
836 dev = &rte_crypto_devices[dev_id];
837 if (queue_pair_id >= dev->data->nb_queue_pairs) {
838 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
842 if (dev->data->dev_started) {
844 "device %d must be stopped to allow configuration", dev_id);
848 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
850 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
856 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
858 struct rte_cryptodev *dev;
860 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
861 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
866 CDEV_LOG_ERR("Invalid stats ptr");
870 dev = &rte_crypto_devices[dev_id];
871 memset(stats, 0, sizeof(*stats));
873 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
874 (*dev->dev_ops->stats_get)(dev, stats);
879 rte_cryptodev_stats_reset(uint8_t dev_id)
881 struct rte_cryptodev *dev;
883 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
884 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
888 dev = &rte_crypto_devices[dev_id];
890 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
891 (*dev->dev_ops->stats_reset)(dev);
896 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
898 struct rte_cryptodev *dev;
900 if (dev_id >= cryptodev_globals.nb_devs) {
901 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
905 dev = &rte_crypto_devices[dev_id];
907 memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
909 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
910 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
912 dev_info->pci_dev = dev->pci_dev;
914 dev_info->driver_name = dev->driver->pci_drv.name;
919 rte_cryptodev_callback_register(uint8_t dev_id,
920 enum rte_cryptodev_event_type event,
921 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
923 struct rte_cryptodev *dev;
924 struct rte_cryptodev_callback *user_cb;
929 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
930 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
934 dev = &rte_crypto_devices[dev_id];
935 rte_spinlock_lock(&rte_cryptodev_cb_lock);
937 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
938 if (user_cb->cb_fn == cb_fn &&
939 user_cb->cb_arg == cb_arg &&
940 user_cb->event == event) {
945 /* create a new callback. */
946 if (user_cb == NULL) {
947 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
948 sizeof(struct rte_cryptodev_callback), 0);
949 if (user_cb != NULL) {
950 user_cb->cb_fn = cb_fn;
951 user_cb->cb_arg = cb_arg;
952 user_cb->event = event;
953 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
957 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
958 return (user_cb == NULL) ? -ENOMEM : 0;
962 rte_cryptodev_callback_unregister(uint8_t dev_id,
963 enum rte_cryptodev_event_type event,
964 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
967 struct rte_cryptodev *dev;
968 struct rte_cryptodev_callback *cb, *next;
973 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
974 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
978 dev = &rte_crypto_devices[dev_id];
979 rte_spinlock_lock(&rte_cryptodev_cb_lock);
982 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
984 next = TAILQ_NEXT(cb, next);
986 if (cb->cb_fn != cb_fn || cb->event != event ||
987 (cb->cb_arg != (void *)-1 &&
988 cb->cb_arg != cb_arg))
992 * if this callback is not executing right now,
995 if (cb->active == 0) {
996 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
1003 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1008 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1009 enum rte_cryptodev_event_type event)
1011 struct rte_cryptodev_callback *cb_lst;
1012 struct rte_cryptodev_callback dev_cb;
1014 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1015 TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1016 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1020 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1021 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1023 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1026 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1031 rte_cryptodev_sym_session_init(struct rte_mempool *mp,
1034 __rte_unused unsigned i)
1036 struct rte_cryptodev_sym_session *sess = _sess;
1037 struct rte_cryptodev *dev = opaque_arg;
1039 memset(sess, 0, mp->elt_size);
1041 sess->dev_id = dev->data->dev_id;
1042 sess->dev_type = dev->dev_type;
1045 if (dev->dev_ops->session_initialize)
1046 (*dev->dev_ops->session_initialize)(mp, sess);
1050 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
1051 unsigned nb_objs, unsigned obj_cache_size, int socket_id)
1053 char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1054 unsigned priv_sess_size;
1056 unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
1058 if (n > sizeof(mp_name)) {
1059 CDEV_LOG_ERR("Unable to create unique name for session mempool");
1063 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_get_size, -ENOTSUP);
1064 priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
1065 if (priv_sess_size == 0) {
1066 CDEV_LOG_ERR("%s returned and invalid private session size ",
1071 unsigned elt_size = sizeof(struct rte_cryptodev_sym_session) +
1074 dev->data->session_pool = rte_mempool_lookup(mp_name);
1075 if (dev->data->session_pool != NULL) {
1076 if ((dev->data->session_pool->elt_size != elt_size) ||
1077 (dev->data->session_pool->cache_size <
1079 (dev->data->session_pool->size < nb_objs)) {
1081 CDEV_LOG_ERR("%s mempool already exists with different"
1082 " initialization parameters", mp_name);
1083 dev->data->session_pool = NULL;
1087 dev->data->session_pool = rte_mempool_create(
1088 mp_name, /* mempool name */
1089 nb_objs, /* number of elements*/
1090 elt_size, /* element size*/
1091 obj_cache_size, /* Cache size*/
1092 0, /* private data size */
1093 NULL, /* obj initialization constructor */
1094 NULL, /* obj initialization constructor arg */
1095 rte_cryptodev_sym_session_init,
1096 /**< obj constructor*/
1097 dev, /* obj constructor arg */
1098 socket_id, /* socket id */
1101 if (dev->data->session_pool == NULL) {
1102 CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
1107 CDEV_LOG_DEBUG("%s mempool created!", mp_name);
1111 struct rte_cryptodev_sym_session *
1112 rte_cryptodev_sym_session_create(uint8_t dev_id,
1113 struct rte_crypto_sym_xform *xform)
1115 struct rte_cryptodev *dev;
1116 struct rte_cryptodev_sym_session *sess;
1119 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1120 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1124 dev = &rte_crypto_devices[dev_id];
1126 /* Allocate a session structure from the session pool */
1127 if (rte_mempool_get(dev->data->session_pool, &_sess)) {
1128 CDEV_LOG_ERR("Couldn't get object from session mempool");
1132 sess = (struct rte_cryptodev_sym_session *)_sess;
1134 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1135 if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1137 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1140 /* Return session to mempool */
1141 rte_mempool_put(sess->mp, _sess);
1148 struct rte_cryptodev_sym_session *
1149 rte_cryptodev_sym_session_free(uint8_t dev_id,
1150 struct rte_cryptodev_sym_session *sess)
1152 struct rte_cryptodev *dev;
1154 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1155 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1159 dev = &rte_crypto_devices[dev_id];
1161 /* Check the session belongs to this device type */
1162 if (sess->dev_type != dev->dev_type)
1165 /* Let device implementation clear session material */
1166 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_clear, sess);
1167 dev->dev_ops->session_clear(dev, (void *)sess->_private);
1169 /* Return session to mempool */
1170 rte_mempool_put(sess->mp, (void *)sess);
1175 /** Initialise rte_crypto_op mempool element */
1177 rte_crypto_op_init(struct rte_mempool *mempool,
1180 __rte_unused unsigned i)
1182 struct rte_crypto_op *op = _op_data;
1183 enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1185 memset(_op_data, 0, mempool->elt_size);
1187 __rte_crypto_op_reset(op, type);
1189 op->phys_addr = rte_mem_virt2phy(_op_data);
1190 op->mempool = mempool;
1194 struct rte_mempool *
1195 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
1196 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
1199 struct rte_crypto_op_pool_private *priv;
1201 unsigned elt_size = sizeof(struct rte_crypto_op) +
1202 sizeof(struct rte_crypto_sym_op) +
1205 /* lookup mempool in case already allocated */
1206 struct rte_mempool *mp = rte_mempool_lookup(name);
1209 priv = (struct rte_crypto_op_pool_private *)
1210 rte_mempool_get_priv(mp);
1212 if (mp->elt_size != elt_size ||
1213 mp->cache_size < cache_size ||
1214 mp->size < nb_elts ||
1215 priv->priv_size < priv_size) {
1217 CDEV_LOG_ERR("Mempool %s already exists but with "
1218 "incompatible parameters", name);
1224 mp = rte_mempool_create(
1229 sizeof(struct rte_crypto_op_pool_private),
1238 CDEV_LOG_ERR("Failed to create mempool %s", name);
1242 priv = (struct rte_crypto_op_pool_private *)
1243 rte_mempool_get_priv(mp);
1245 priv->priv_size = priv_size;