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>
63 #include <rte_mempool.h>
64 #include <rte_malloc.h>
66 #include <rte_errno.h>
67 #include <rte_spinlock.h>
68 #include <rte_string_fns.h>
70 #include "rte_crypto.h"
71 #include "rte_cryptodev.h"
72 #include "rte_cryptodev_pmd.h"
74 struct rte_cryptodev rte_crypto_devices[RTE_CRYPTO_MAX_DEVS];
76 struct rte_cryptodev *rte_cryptodevs = &rte_crypto_devices[0];
78 static struct rte_cryptodev_global cryptodev_globals = {
79 .devs = &rte_crypto_devices[0],
82 .max_devs = RTE_CRYPTO_MAX_DEVS
85 struct rte_cryptodev_global *rte_cryptodev_globals = &cryptodev_globals;
87 /* spinlock for crypto device callbacks */
88 static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
92 * The user application callback description.
94 * It contains callback address to be registered by user application,
95 * the pointer to the parameters for callback, and the event type.
97 struct rte_cryptodev_callback {
98 TAILQ_ENTRY(rte_cryptodev_callback) next; /**< Callbacks list */
99 rte_cryptodev_cb_fn cb_fn; /**< Callback address */
100 void *cb_arg; /**< Parameter for callback */
101 enum rte_cryptodev_event_type event; /**< Interrupt event type */
102 uint32_t active; /**< Callback is executing */
107 rte_cryptodev_get_feature_name(uint64_t flag)
110 case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
111 return "SYMMETRIC_CRYPTO";
112 case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
113 return "ASYMMETRIC_CRYPTO";
114 case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
115 return "SYM_OPERATION_CHAINING";
116 case RTE_CRYPTODEV_FF_CPU_SSE:
118 case RTE_CRYPTODEV_FF_CPU_AVX:
120 case RTE_CRYPTODEV_FF_CPU_AVX2:
122 case RTE_CRYPTODEV_FF_CPU_AESNI:
124 case RTE_CRYPTODEV_FF_HW_ACCELERATED:
125 return "HW_ACCELERATED";
134 rte_cryptodev_create_vdev(const char *name, const char *args)
136 return rte_eal_vdev_init(name, args);
140 rte_cryptodev_get_dev_id(const char *name) {
146 for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
147 if ((strcmp(rte_cryptodev_globals->devs[i].data->name, name)
149 (rte_cryptodev_globals->devs[i].attached ==
150 RTE_CRYPTODEV_ATTACHED))
157 rte_cryptodev_count(void)
159 return rte_cryptodev_globals->nb_devs;
163 rte_cryptodev_count_devtype(enum rte_cryptodev_type type)
165 uint8_t i, dev_count = 0;
167 for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
168 if (rte_cryptodev_globals->devs[i].dev_type == type &&
169 rte_cryptodev_globals->devs[i].attached ==
170 RTE_CRYPTODEV_ATTACHED)
177 rte_cryptodev_socket_id(uint8_t dev_id)
179 struct rte_cryptodev *dev;
181 if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
184 dev = rte_cryptodev_pmd_get_dev(dev_id);
186 return dev->data->socket_id;
190 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
193 char mz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
194 const struct rte_memzone *mz;
197 /* generate memzone name */
198 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
199 if (n >= (int)sizeof(mz_name))
202 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
203 mz = rte_memzone_reserve(mz_name,
204 sizeof(struct rte_cryptodev_data),
207 mz = rte_memzone_lookup(mz_name);
213 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
214 memset(*data, 0, sizeof(struct rte_cryptodev_data));
220 rte_cryptodev_find_free_device_index(void)
224 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
225 if (rte_crypto_devices[dev_id].attached ==
226 RTE_CRYPTODEV_DETACHED)
229 return RTE_CRYPTO_MAX_DEVS;
232 struct rte_cryptodev *
233 rte_cryptodev_pmd_allocate(const char *name, enum pmd_type type, int socket_id)
235 struct rte_cryptodev *cryptodev;
238 if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
239 CDEV_LOG_ERR("Crypto device with name %s already "
244 dev_id = rte_cryptodev_find_free_device_index();
245 if (dev_id == RTE_CRYPTO_MAX_DEVS) {
246 CDEV_LOG_ERR("Reached maximum number of crypto devices");
250 cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
252 if (cryptodev->data == NULL) {
253 struct rte_cryptodev_data *cryptodev_data =
254 cryptodev_globals.data[dev_id];
256 int retval = rte_cryptodev_data_alloc(dev_id, &cryptodev_data,
259 if (retval < 0 || cryptodev_data == NULL)
262 cryptodev->data = cryptodev_data;
264 snprintf(cryptodev->data->name, RTE_CRYPTODEV_NAME_MAX_LEN,
267 cryptodev->data->dev_id = dev_id;
268 cryptodev->data->socket_id = socket_id;
269 cryptodev->data->dev_started = 0;
271 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
272 cryptodev->pmd_type = type;
274 cryptodev_globals.nb_devs++;
281 rte_cryptodev_create_unique_device_name(char *name, size_t size,
282 struct rte_pci_device *pci_dev)
286 if ((name == NULL) || (pci_dev == NULL))
289 ret = snprintf(name, size, "%d:%d.%d",
290 pci_dev->addr.bus, pci_dev->addr.devid,
291 pci_dev->addr.function);
298 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
302 if (cryptodev == NULL)
305 ret = rte_cryptodev_close(cryptodev->data->dev_id);
309 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
310 cryptodev_globals.nb_devs--;
314 struct rte_cryptodev *
315 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
318 struct rte_cryptodev *cryptodev;
320 /* allocate device structure */
321 cryptodev = rte_cryptodev_pmd_allocate(name, PMD_VDEV, socket_id);
322 if (cryptodev == NULL)
325 /* allocate private device structure */
326 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
327 cryptodev->data->dev_private =
328 rte_zmalloc_socket("cryptodev device private",
333 if (cryptodev->data->dev_private == NULL)
334 rte_panic("Cannot allocate memzone for private device"
338 /* initialise user call-back tail queue */
339 TAILQ_INIT(&(cryptodev->link_intr_cbs));
345 rte_cryptodev_init(struct rte_pci_driver *pci_drv,
346 struct rte_pci_device *pci_dev)
348 struct rte_cryptodev_driver *cryptodrv;
349 struct rte_cryptodev *cryptodev;
351 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
355 cryptodrv = (struct rte_cryptodev_driver *)pci_drv;
356 if (cryptodrv == NULL)
359 /* Create unique Crypto device name using PCI address */
360 rte_cryptodev_create_unique_device_name(cryptodev_name,
361 sizeof(cryptodev_name), pci_dev);
363 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, PMD_PDEV,
365 if (cryptodev == NULL)
368 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
369 cryptodev->data->dev_private =
371 "cryptodev private structure",
372 cryptodrv->dev_private_size,
376 if (cryptodev->data->dev_private == NULL)
377 rte_panic("Cannot allocate memzone for private "
381 cryptodev->pci_dev = pci_dev;
382 cryptodev->driver = cryptodrv;
384 /* init user callbacks */
385 TAILQ_INIT(&(cryptodev->link_intr_cbs));
387 /* Invoke PMD device initialization function */
388 retval = (*cryptodrv->cryptodev_init)(cryptodrv, cryptodev);
392 CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
393 " failed", pci_drv->name,
394 (unsigned) pci_dev->id.vendor_id,
395 (unsigned) pci_dev->id.device_id);
397 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
398 rte_free(cryptodev->data->dev_private);
400 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
401 cryptodev_globals.nb_devs--;
407 rte_cryptodev_uninit(struct rte_pci_device *pci_dev)
409 const struct rte_cryptodev_driver *cryptodrv;
410 struct rte_cryptodev *cryptodev;
411 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
417 /* Create unique device name using PCI address */
418 rte_cryptodev_create_unique_device_name(cryptodev_name,
419 sizeof(cryptodev_name), pci_dev);
421 cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
422 if (cryptodev == NULL)
425 cryptodrv = (const struct rte_cryptodev_driver *)pci_dev->driver;
426 if (cryptodrv == NULL)
429 /* Invoke PMD device uninit function */
430 if (*cryptodrv->cryptodev_uninit) {
431 ret = (*cryptodrv->cryptodev_uninit)(cryptodrv, cryptodev);
436 /* free crypto device */
437 rte_cryptodev_pmd_release_device(cryptodev);
439 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
440 rte_free(cryptodev->data->dev_private);
442 cryptodev->pci_dev = NULL;
443 cryptodev->driver = NULL;
444 cryptodev->data = NULL;
450 rte_cryptodev_pmd_driver_register(struct rte_cryptodev_driver *cryptodrv,
453 /* Call crypto device initialization directly if device is virtual */
454 if (type == PMD_VDEV)
455 return rte_cryptodev_init((struct rte_pci_driver *)cryptodrv,
459 * Register PCI driver for physical device intialisation during
462 cryptodrv->pci_drv.devinit = rte_cryptodev_init;
463 cryptodrv->pci_drv.devuninit = rte_cryptodev_uninit;
465 rte_eal_pci_register(&cryptodrv->pci_drv);
472 rte_cryptodev_queue_pair_count(uint8_t dev_id)
474 struct rte_cryptodev *dev;
476 dev = &rte_crypto_devices[dev_id];
477 return dev->data->nb_queue_pairs;
481 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
484 struct rte_cryptodev_info dev_info;
488 if ((dev == NULL) || (nb_qpairs < 1)) {
489 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
494 CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
495 nb_qpairs, dev->data->dev_id);
497 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
499 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
500 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
502 if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
503 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
504 nb_qpairs, dev->data->dev_id);
508 if (dev->data->queue_pairs == NULL) { /* first time configuration */
509 dev->data->queue_pairs = rte_zmalloc_socket(
510 "cryptodev->queue_pairs",
511 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
512 RTE_CACHE_LINE_SIZE, socket_id);
514 if (dev->data->queue_pairs == NULL) {
515 dev->data->nb_queue_pairs = 0;
516 CDEV_LOG_ERR("failed to get memory for qp meta data, "
521 } else { /* re-configure */
523 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
525 qp = dev->data->queue_pairs;
527 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
530 for (i = nb_qpairs; i < old_nb_queues; i++) {
531 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
536 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
537 RTE_CACHE_LINE_SIZE);
539 CDEV_LOG_ERR("failed to realloc qp meta data,"
540 " nb_queues %u", nb_qpairs);
544 if (nb_qpairs > old_nb_queues) {
545 uint16_t new_qs = nb_qpairs - old_nb_queues;
547 memset(qp + old_nb_queues, 0,
548 sizeof(qp[0]) * new_qs);
551 dev->data->queue_pairs = qp;
554 dev->data->nb_queue_pairs = nb_qpairs;
559 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
561 struct rte_cryptodev *dev;
563 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
564 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
568 dev = &rte_crypto_devices[dev_id];
569 if (queue_pair_id >= dev->data->nb_queue_pairs) {
570 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
574 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
576 return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
581 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
583 struct rte_cryptodev *dev;
585 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
586 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
590 dev = &rte_crypto_devices[dev_id];
591 if (queue_pair_id >= dev->data->nb_queue_pairs) {
592 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
596 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
598 return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
603 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
604 unsigned nb_objs, unsigned obj_cache_size, int socket_id);
607 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
609 struct rte_cryptodev *dev;
612 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
613 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
617 dev = &rte_crypto_devices[dev_id];
619 if (dev->data->dev_started) {
621 "device %d must be stopped to allow configuration", dev_id);
625 /* Setup new number of queue pairs and reconfigure device. */
626 diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
629 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
634 /* Setup Session mempool for device */
635 return rte_cryptodev_sym_session_pool_create(dev,
636 config->session_mp.nb_objs,
637 config->session_mp.cache_size,
643 rte_cryptodev_start(uint8_t dev_id)
645 struct rte_cryptodev *dev;
648 CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
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];
657 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
659 if (dev->data->dev_started != 0) {
660 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
665 diag = (*dev->dev_ops->dev_start)(dev);
667 dev->data->dev_started = 1;
675 rte_cryptodev_stop(uint8_t dev_id)
677 struct rte_cryptodev *dev;
679 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
680 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
684 dev = &rte_crypto_devices[dev_id];
686 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
688 if (dev->data->dev_started == 0) {
689 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
694 dev->data->dev_started = 0;
695 (*dev->dev_ops->dev_stop)(dev);
699 rte_cryptodev_close(uint8_t dev_id)
701 struct rte_cryptodev *dev;
704 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
705 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
709 dev = &rte_crypto_devices[dev_id];
711 /* Device must be stopped before it can be closed */
712 if (dev->data->dev_started == 1) {
713 CDEV_LOG_ERR("Device %u must be stopped before closing",
718 /* We can't close the device if there are outstanding sessions in use */
719 if (dev->data->session_pool != NULL) {
720 if (!rte_mempool_full(dev->data->session_pool)) {
721 CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
722 "has sessions still in use, free "
723 "all sessions before calling close",
729 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
730 retval = (*dev->dev_ops->dev_close)(dev);
739 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
740 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
742 struct rte_cryptodev *dev;
744 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
745 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
749 dev = &rte_crypto_devices[dev_id];
750 if (queue_pair_id >= dev->data->nb_queue_pairs) {
751 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
755 if (dev->data->dev_started) {
757 "device %d must be stopped to allow configuration", dev_id);
761 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
763 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
769 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
771 struct rte_cryptodev *dev;
773 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
774 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
779 CDEV_LOG_ERR("Invalid stats ptr");
783 dev = &rte_crypto_devices[dev_id];
784 memset(stats, 0, sizeof(*stats));
786 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
787 (*dev->dev_ops->stats_get)(dev, stats);
792 rte_cryptodev_stats_reset(uint8_t dev_id)
794 struct rte_cryptodev *dev;
796 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
797 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
801 dev = &rte_crypto_devices[dev_id];
803 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
804 (*dev->dev_ops->stats_reset)(dev);
809 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
811 struct rte_cryptodev *dev;
813 if (dev_id >= cryptodev_globals.nb_devs) {
814 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
818 dev = &rte_crypto_devices[dev_id];
820 memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
822 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
823 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
825 dev_info->pci_dev = dev->pci_dev;
827 dev_info->driver_name = dev->driver->pci_drv.name;
832 rte_cryptodev_callback_register(uint8_t dev_id,
833 enum rte_cryptodev_event_type event,
834 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
836 struct rte_cryptodev *dev;
837 struct rte_cryptodev_callback *user_cb;
842 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
843 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
847 dev = &rte_crypto_devices[dev_id];
848 rte_spinlock_lock(&rte_cryptodev_cb_lock);
850 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
851 if (user_cb->cb_fn == cb_fn &&
852 user_cb->cb_arg == cb_arg &&
853 user_cb->event == event) {
858 /* create a new callback. */
859 if (user_cb == NULL) {
860 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
861 sizeof(struct rte_cryptodev_callback), 0);
862 if (user_cb != NULL) {
863 user_cb->cb_fn = cb_fn;
864 user_cb->cb_arg = cb_arg;
865 user_cb->event = event;
866 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
870 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
871 return (user_cb == NULL) ? -ENOMEM : 0;
875 rte_cryptodev_callback_unregister(uint8_t dev_id,
876 enum rte_cryptodev_event_type event,
877 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
880 struct rte_cryptodev *dev;
881 struct rte_cryptodev_callback *cb, *next;
886 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
887 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
891 dev = &rte_crypto_devices[dev_id];
892 rte_spinlock_lock(&rte_cryptodev_cb_lock);
895 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
897 next = TAILQ_NEXT(cb, next);
899 if (cb->cb_fn != cb_fn || cb->event != event ||
900 (cb->cb_arg != (void *)-1 &&
901 cb->cb_arg != cb_arg))
905 * if this callback is not executing right now,
908 if (cb->active == 0) {
909 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
916 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
921 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
922 enum rte_cryptodev_event_type event)
924 struct rte_cryptodev_callback *cb_lst;
925 struct rte_cryptodev_callback dev_cb;
927 rte_spinlock_lock(&rte_cryptodev_cb_lock);
928 TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
929 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
933 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
934 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
936 rte_spinlock_lock(&rte_cryptodev_cb_lock);
939 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
944 rte_cryptodev_sym_session_init(struct rte_mempool *mp,
947 __rte_unused unsigned i)
949 struct rte_cryptodev_sym_session *sess = _sess;
950 struct rte_cryptodev *dev = opaque_arg;
952 memset(sess, 0, mp->elt_size);
954 sess->dev_id = dev->data->dev_id;
955 sess->dev_type = dev->dev_type;
958 if (dev->dev_ops->session_initialize)
959 (*dev->dev_ops->session_initialize)(mp, sess->_private);
963 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
964 unsigned nb_objs, unsigned obj_cache_size, int socket_id)
966 char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
967 unsigned priv_sess_size;
969 unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
971 if (n > sizeof(mp_name)) {
972 CDEV_LOG_ERR("Unable to create unique name for session mempool");
976 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_get_size, -ENOTSUP);
977 priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
978 if (priv_sess_size == 0) {
979 CDEV_LOG_ERR("%s returned and invalid private session size ",
984 unsigned elt_size = sizeof(struct rte_cryptodev_sym_session) +
987 dev->data->session_pool = rte_mempool_lookup(mp_name);
988 if (dev->data->session_pool != NULL) {
989 if ((dev->data->session_pool->elt_size != elt_size) ||
990 (dev->data->session_pool->cache_size <
992 (dev->data->session_pool->size < nb_objs)) {
994 CDEV_LOG_ERR("%s mempool already exists with different"
995 " initialization parameters", mp_name);
996 dev->data->session_pool = NULL;
1000 dev->data->session_pool = rte_mempool_create(
1001 mp_name, /* mempool name */
1002 nb_objs, /* number of elements*/
1003 elt_size, /* element size*/
1004 obj_cache_size, /* Cache size*/
1005 0, /* private data size */
1006 NULL, /* obj initialization constructor */
1007 NULL, /* obj initialization constructor arg */
1008 rte_cryptodev_sym_session_init,
1009 /**< obj constructor*/
1010 dev, /* obj constructor arg */
1011 socket_id, /* socket id */
1014 if (dev->data->session_pool == NULL) {
1015 CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
1020 CDEV_LOG_DEBUG("%s mempool created!", mp_name);
1024 struct rte_cryptodev_sym_session *
1025 rte_cryptodev_sym_session_create(uint8_t dev_id,
1026 struct rte_crypto_sym_xform *xform)
1028 struct rte_cryptodev *dev;
1029 struct rte_cryptodev_sym_session *sess;
1032 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1033 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1037 dev = &rte_crypto_devices[dev_id];
1039 /* Allocate a session structure from the session pool */
1040 if (rte_mempool_get(dev->data->session_pool, &_sess)) {
1041 CDEV_LOG_ERR("Couldn't get object from session mempool");
1045 sess = (struct rte_cryptodev_sym_session *)_sess;
1047 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1048 if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1050 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1053 /* Return session to mempool */
1054 rte_mempool_put(sess->mp, _sess);
1061 struct rte_cryptodev_sym_session *
1062 rte_cryptodev_sym_session_free(uint8_t dev_id,
1063 struct rte_cryptodev_sym_session *sess)
1065 struct rte_cryptodev *dev;
1067 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1068 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1072 dev = &rte_crypto_devices[dev_id];
1074 /* Check the session belongs to this device type */
1075 if (sess->dev_type != dev->dev_type)
1078 /* Let device implementation clear session material */
1079 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_clear, sess);
1080 dev->dev_ops->session_clear(dev, (void *)sess->_private);
1082 /* Return session to mempool */
1083 rte_mempool_put(sess->mp, (void *)sess);
1088 /** Initialise rte_crypto_op mempool element */
1090 rte_crypto_op_init(struct rte_mempool *mempool,
1093 __rte_unused unsigned i)
1095 struct rte_crypto_op *op = _op_data;
1096 enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1098 memset(_op_data, 0, mempool->elt_size);
1100 __rte_crypto_op_reset(op, type);
1102 op->phys_addr = rte_mem_virt2phy(_op_data);
1103 op->mempool = mempool;
1107 struct rte_mempool *
1108 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
1109 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
1112 struct rte_crypto_op_pool_private *priv;
1114 unsigned elt_size = sizeof(struct rte_crypto_op) +
1115 sizeof(struct rte_crypto_sym_op) +
1118 /* lookup mempool in case already allocated */
1119 struct rte_mempool *mp = rte_mempool_lookup(name);
1122 priv = (struct rte_crypto_op_pool_private *)
1123 rte_mempool_get_priv(mp);
1125 if (mp->elt_size != elt_size ||
1126 mp->cache_size < cache_size ||
1127 mp->size < nb_elts ||
1128 priv->priv_size < priv_size) {
1130 CDEV_LOG_ERR("Mempool %s already exists but with "
1131 "incompatible parameters", name);
1137 mp = rte_mempool_create(
1142 sizeof(struct rte_crypto_op_pool_private),
1151 CDEV_LOG_ERR("Failed to create mempool %s", name);
1155 priv = (struct rte_crypto_op_pool_private *)
1156 rte_mempool_get_priv(mp);
1158 priv->priv_size = priv_size;