4 * Copyright(c) 2015 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 */
106 rte_cryptodev_create_vdev(const char *name, const char *args)
108 return rte_eal_vdev_init(name, args);
112 rte_cryptodev_get_dev_id(const char *name) {
118 for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
119 if ((strcmp(rte_cryptodev_globals->devs[i].data->name, name)
121 (rte_cryptodev_globals->devs[i].attached ==
122 RTE_CRYPTODEV_ATTACHED))
129 rte_cryptodev_count(void)
131 return rte_cryptodev_globals->nb_devs;
135 rte_cryptodev_count_devtype(enum rte_cryptodev_type type)
137 uint8_t i, dev_count = 0;
139 for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
140 if (rte_cryptodev_globals->devs[i].dev_type == type &&
141 rte_cryptodev_globals->devs[i].attached ==
142 RTE_CRYPTODEV_ATTACHED)
149 rte_cryptodev_socket_id(uint8_t dev_id)
151 struct rte_cryptodev *dev;
153 if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
156 dev = rte_cryptodev_pmd_get_dev(dev_id);
158 return dev->data->socket_id;
162 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
165 char mz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
166 const struct rte_memzone *mz;
169 /* generate memzone name */
170 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
171 if (n >= (int)sizeof(mz_name))
174 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
175 mz = rte_memzone_reserve(mz_name,
176 sizeof(struct rte_cryptodev_data),
179 mz = rte_memzone_lookup(mz_name);
185 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
186 memset(*data, 0, sizeof(struct rte_cryptodev_data));
192 rte_cryptodev_find_free_device_index(void)
196 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
197 if (rte_crypto_devices[dev_id].attached ==
198 RTE_CRYPTODEV_DETACHED)
201 return RTE_CRYPTO_MAX_DEVS;
204 struct rte_cryptodev *
205 rte_cryptodev_pmd_allocate(const char *name, enum pmd_type type, int socket_id)
207 struct rte_cryptodev *cryptodev;
210 if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
211 CDEV_LOG_ERR("Crypto device with name %s already "
216 dev_id = rte_cryptodev_find_free_device_index();
217 if (dev_id == RTE_CRYPTO_MAX_DEVS) {
218 CDEV_LOG_ERR("Reached maximum number of crypto devices");
222 cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
224 if (cryptodev->data == NULL) {
225 struct rte_cryptodev_data *cryptodev_data =
226 cryptodev_globals.data[dev_id];
228 int retval = rte_cryptodev_data_alloc(dev_id, &cryptodev_data,
231 if (retval < 0 || cryptodev_data == NULL)
234 cryptodev->data = cryptodev_data;
236 snprintf(cryptodev->data->name, RTE_CRYPTODEV_NAME_MAX_LEN,
239 cryptodev->data->dev_id = dev_id;
240 cryptodev->data->socket_id = socket_id;
241 cryptodev->data->dev_started = 0;
243 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
244 cryptodev->pmd_type = type;
246 cryptodev_globals.nb_devs++;
253 rte_cryptodev_create_unique_device_name(char *name, size_t size,
254 struct rte_pci_device *pci_dev)
258 if ((name == NULL) || (pci_dev == NULL))
261 ret = snprintf(name, size, "%d:%d.%d",
262 pci_dev->addr.bus, pci_dev->addr.devid,
263 pci_dev->addr.function);
270 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
274 if (cryptodev == NULL)
277 ret = rte_cryptodev_close(cryptodev->data->dev_id);
281 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
282 cryptodev_globals.nb_devs--;
286 struct rte_cryptodev *
287 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
290 struct rte_cryptodev *cryptodev;
292 /* allocate device structure */
293 cryptodev = rte_cryptodev_pmd_allocate(name, PMD_VDEV, socket_id);
294 if (cryptodev == NULL)
297 /* allocate private device structure */
298 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
299 cryptodev->data->dev_private =
300 rte_zmalloc_socket("cryptodev device private",
305 if (cryptodev->data->dev_private == NULL)
306 rte_panic("Cannot allocate memzone for private device"
310 /* initialise user call-back tail queue */
311 TAILQ_INIT(&(cryptodev->link_intr_cbs));
317 rte_cryptodev_init(struct rte_pci_driver *pci_drv,
318 struct rte_pci_device *pci_dev)
320 struct rte_cryptodev_driver *cryptodrv;
321 struct rte_cryptodev *cryptodev;
323 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
327 cryptodrv = (struct rte_cryptodev_driver *)pci_drv;
328 if (cryptodrv == NULL)
331 /* Create unique Crypto device name using PCI address */
332 rte_cryptodev_create_unique_device_name(cryptodev_name,
333 sizeof(cryptodev_name), pci_dev);
335 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, PMD_PDEV,
337 if (cryptodev == NULL)
340 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
341 cryptodev->data->dev_private =
343 "cryptodev private structure",
344 cryptodrv->dev_private_size,
348 if (cryptodev->data->dev_private == NULL)
349 rte_panic("Cannot allocate memzone for private "
353 cryptodev->pci_dev = pci_dev;
354 cryptodev->driver = cryptodrv;
356 /* init user callbacks */
357 TAILQ_INIT(&(cryptodev->link_intr_cbs));
359 /* Invoke PMD device initialization function */
360 retval = (*cryptodrv->cryptodev_init)(cryptodrv, cryptodev);
364 CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
365 " failed", pci_drv->name,
366 (unsigned) pci_dev->id.vendor_id,
367 (unsigned) pci_dev->id.device_id);
369 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
370 rte_free(cryptodev->data->dev_private);
372 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
373 cryptodev_globals.nb_devs--;
379 rte_cryptodev_uninit(struct rte_pci_device *pci_dev)
381 const struct rte_cryptodev_driver *cryptodrv;
382 struct rte_cryptodev *cryptodev;
383 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
389 /* Create unique device name using PCI address */
390 rte_cryptodev_create_unique_device_name(cryptodev_name,
391 sizeof(cryptodev_name), pci_dev);
393 cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
394 if (cryptodev == NULL)
397 cryptodrv = (const struct rte_cryptodev_driver *)pci_dev->driver;
398 if (cryptodrv == NULL)
401 /* Invoke PMD device uninit function */
402 if (*cryptodrv->cryptodev_uninit) {
403 ret = (*cryptodrv->cryptodev_uninit)(cryptodrv, cryptodev);
408 /* free crypto device */
409 rte_cryptodev_pmd_release_device(cryptodev);
411 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
412 rte_free(cryptodev->data->dev_private);
414 cryptodev->pci_dev = NULL;
415 cryptodev->driver = NULL;
416 cryptodev->data = NULL;
422 rte_cryptodev_pmd_driver_register(struct rte_cryptodev_driver *cryptodrv,
425 /* Call crypto device initialization directly if device is virtual */
426 if (type == PMD_VDEV)
427 return rte_cryptodev_init((struct rte_pci_driver *)cryptodrv,
431 * Register PCI driver for physical device intialisation during
434 cryptodrv->pci_drv.devinit = rte_cryptodev_init;
435 cryptodrv->pci_drv.devuninit = rte_cryptodev_uninit;
437 rte_eal_pci_register(&cryptodrv->pci_drv);
444 rte_cryptodev_queue_pair_count(uint8_t dev_id)
446 struct rte_cryptodev *dev;
448 dev = &rte_crypto_devices[dev_id];
449 return dev->data->nb_queue_pairs;
453 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
456 struct rte_cryptodev_info dev_info;
460 if ((dev == NULL) || (nb_qpairs < 1)) {
461 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
466 CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
467 nb_qpairs, dev->data->dev_id);
469 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
471 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
472 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
474 if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
475 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
476 nb_qpairs, dev->data->dev_id);
480 if (dev->data->queue_pairs == NULL) { /* first time configuration */
481 dev->data->queue_pairs = rte_zmalloc_socket(
482 "cryptodev->queue_pairs",
483 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
484 RTE_CACHE_LINE_SIZE, socket_id);
486 if (dev->data->queue_pairs == NULL) {
487 dev->data->nb_queue_pairs = 0;
488 CDEV_LOG_ERR("failed to get memory for qp meta data, "
493 } else { /* re-configure */
495 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
497 qp = dev->data->queue_pairs;
499 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
502 for (i = nb_qpairs; i < old_nb_queues; i++) {
503 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
508 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
509 RTE_CACHE_LINE_SIZE);
511 CDEV_LOG_ERR("failed to realloc qp meta data,"
512 " nb_queues %u", nb_qpairs);
516 if (nb_qpairs > old_nb_queues) {
517 uint16_t new_qs = nb_qpairs - old_nb_queues;
519 memset(qp + old_nb_queues, 0,
520 sizeof(qp[0]) * new_qs);
523 dev->data->queue_pairs = qp;
526 dev->data->nb_queue_pairs = nb_qpairs;
531 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
533 struct rte_cryptodev *dev;
536 * This function is only safe when called from the primary process
537 * in a multi-process setup
539 RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
541 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
542 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
546 dev = &rte_crypto_devices[dev_id];
547 if (queue_pair_id >= dev->data->nb_queue_pairs) {
548 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
552 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
554 return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
559 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
561 struct rte_cryptodev *dev;
564 * This function is only safe when called from the primary process
565 * in a multi-process setup
567 RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
569 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
570 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
574 dev = &rte_crypto_devices[dev_id];
575 if (queue_pair_id >= dev->data->nb_queue_pairs) {
576 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
580 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
582 return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
587 rte_crypto_session_pool_create(struct rte_cryptodev *dev, unsigned nb_objs,
588 unsigned obj_cache_size, int socket_id);
591 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
593 struct rte_cryptodev *dev;
597 * This function is only safe when called from the primary process
598 * in a multi-process setup
600 RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
602 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
603 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
607 dev = &rte_crypto_devices[dev_id];
609 if (dev->data->dev_started) {
611 "device %d must be stopped to allow configuration", dev_id);
615 /* Setup new number of queue pairs and reconfigure device. */
616 diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
619 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
624 /* Setup Session mempool for device */
625 return rte_crypto_session_pool_create(dev, config->session_mp.nb_objs,
626 config->session_mp.cache_size, config->socket_id);
631 rte_cryptodev_start(uint8_t dev_id)
633 struct rte_cryptodev *dev;
636 CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
639 * This function is only safe when called from the primary process
640 * in a multi-process setup
642 RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
644 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
645 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
649 dev = &rte_crypto_devices[dev_id];
651 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
653 if (dev->data->dev_started != 0) {
654 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
659 diag = (*dev->dev_ops->dev_start)(dev);
661 dev->data->dev_started = 1;
669 rte_cryptodev_stop(uint8_t dev_id)
671 struct rte_cryptodev *dev;
674 * This function is only safe when called from the primary process
675 * in a multi-process setup
677 RTE_PROC_PRIMARY_OR_RET();
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;
705 * This function is only safe when called from the primary process
706 * in a multi-process setup
708 RTE_PROC_PRIMARY_OR_ERR_RET(-EINVAL);
710 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
711 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
715 dev = &rte_crypto_devices[dev_id];
717 /* Device must be stopped before it can be closed */
718 if (dev->data->dev_started == 1) {
719 CDEV_LOG_ERR("Device %u must be stopped before closing",
724 /* We can't close the device if there are outstanding sessions in use */
725 if (dev->data->session_pool != NULL) {
726 if (!rte_mempool_full(dev->data->session_pool)) {
727 CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
728 "has sessions still in use, free "
729 "all sessions before calling close",
735 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
736 retval = (*dev->dev_ops->dev_close)(dev);
745 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
746 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
748 struct rte_cryptodev *dev;
751 * This function is only safe when called from the primary process
752 * in a multi-process setup
754 RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
756 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
757 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
761 dev = &rte_crypto_devices[dev_id];
762 if (queue_pair_id >= dev->data->nb_queue_pairs) {
763 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
767 if (dev->data->dev_started) {
769 "device %d must be stopped to allow configuration", dev_id);
773 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
775 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
781 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
783 struct rte_cryptodev *dev;
785 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
786 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
791 CDEV_LOG_ERR("Invalid stats ptr");
795 dev = &rte_crypto_devices[dev_id];
796 memset(stats, 0, sizeof(*stats));
798 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
799 (*dev->dev_ops->stats_get)(dev, stats);
804 rte_cryptodev_stats_reset(uint8_t dev_id)
806 struct rte_cryptodev *dev;
808 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
809 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
813 dev = &rte_crypto_devices[dev_id];
815 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
816 (*dev->dev_ops->stats_reset)(dev);
821 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
823 struct rte_cryptodev *dev;
825 if (dev_id >= cryptodev_globals.nb_devs) {
826 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
830 dev = &rte_crypto_devices[dev_id];
832 memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
834 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
835 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
837 dev_info->pci_dev = dev->pci_dev;
839 dev_info->driver_name = dev->driver->pci_drv.name;
844 rte_cryptodev_callback_register(uint8_t dev_id,
845 enum rte_cryptodev_event_type event,
846 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
848 struct rte_cryptodev *dev;
849 struct rte_cryptodev_callback *user_cb;
854 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
855 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
859 dev = &rte_crypto_devices[dev_id];
860 rte_spinlock_lock(&rte_cryptodev_cb_lock);
862 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
863 if (user_cb->cb_fn == cb_fn &&
864 user_cb->cb_arg == cb_arg &&
865 user_cb->event == event) {
870 /* create a new callback. */
871 if (user_cb == NULL) {
872 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
873 sizeof(struct rte_cryptodev_callback), 0);
874 if (user_cb != NULL) {
875 user_cb->cb_fn = cb_fn;
876 user_cb->cb_arg = cb_arg;
877 user_cb->event = event;
878 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
882 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
883 return ((user_cb == NULL) ? -ENOMEM : 0);
887 rte_cryptodev_callback_unregister(uint8_t dev_id,
888 enum rte_cryptodev_event_type event,
889 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
892 struct rte_cryptodev *dev;
893 struct rte_cryptodev_callback *cb, *next;
898 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
899 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
903 dev = &rte_crypto_devices[dev_id];
904 rte_spinlock_lock(&rte_cryptodev_cb_lock);
907 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
909 next = TAILQ_NEXT(cb, next);
911 if (cb->cb_fn != cb_fn || cb->event != event ||
912 (cb->cb_arg != (void *)-1 &&
913 cb->cb_arg != cb_arg))
917 * if this callback is not executing right now,
920 if (cb->active == 0) {
921 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
928 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
933 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
934 enum rte_cryptodev_event_type event)
936 struct rte_cryptodev_callback *cb_lst;
937 struct rte_cryptodev_callback dev_cb;
939 rte_spinlock_lock(&rte_cryptodev_cb_lock);
940 TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
941 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
945 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
946 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
948 rte_spinlock_lock(&rte_cryptodev_cb_lock);
951 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
956 rte_crypto_session_init(struct rte_mempool *mp,
959 __rte_unused unsigned i)
961 struct rte_cryptodev_session *sess = _sess;
962 struct rte_cryptodev *dev = opaque_arg;
964 memset(sess, 0, mp->elt_size);
966 sess->dev_id = dev->data->dev_id;
967 sess->type = dev->dev_type;
970 if (dev->dev_ops->session_initialize)
971 (*dev->dev_ops->session_initialize)(mp, sess->_private);
975 rte_crypto_session_pool_create(struct rte_cryptodev *dev, unsigned nb_objs,
976 unsigned obj_cache_size, int socket_id)
978 char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
979 unsigned priv_sess_size;
981 unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
983 if (n > sizeof(mp_name)) {
984 CDEV_LOG_ERR("Unable to create unique name for session mempool");
988 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_get_size, -ENOTSUP);
989 priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
990 if (priv_sess_size == 0) {
991 CDEV_LOG_ERR("%s returned and invalid private session size ",
996 unsigned elt_size = sizeof(struct rte_cryptodev_session) +
999 dev->data->session_pool = rte_mempool_lookup(mp_name);
1000 if (dev->data->session_pool != NULL) {
1001 if ((dev->data->session_pool->elt_size != elt_size) ||
1002 (dev->data->session_pool->cache_size <
1004 (dev->data->session_pool->size < nb_objs)) {
1006 CDEV_LOG_ERR("%s mempool already exists with different"
1007 " initialization parameters", mp_name);
1008 dev->data->session_pool = NULL;
1012 dev->data->session_pool = rte_mempool_create(
1013 mp_name, /* mempool name */
1014 nb_objs, /* number of elements*/
1015 elt_size, /* element size*/
1016 obj_cache_size, /* Cache size*/
1017 0, /* private data size */
1018 NULL, /* obj initialization constructor */
1019 NULL, /* obj initialization constructor arg */
1020 rte_crypto_session_init, /* obj constructor */
1021 dev, /* obj constructor arg */
1022 socket_id, /* socket id */
1025 if (dev->data->session_pool == NULL) {
1026 CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
1031 CDEV_LOG_DEBUG("%s mempool created!", mp_name);
1035 struct rte_cryptodev_session *
1036 rte_cryptodev_session_create(uint8_t dev_id, struct rte_crypto_xform *xform)
1038 struct rte_cryptodev *dev;
1039 struct rte_cryptodev_session *sess;
1041 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1042 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1046 dev = &rte_crypto_devices[dev_id];
1048 /* Allocate a session structure from the session pool */
1049 if (rte_mempool_get(dev->data->session_pool, (void **)&sess)) {
1050 CDEV_LOG_ERR("Couldn't get object from session mempool");
1054 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1055 if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1057 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1060 /* Return session to mempool */
1061 rte_mempool_put(sess->mp, (void *)sess);
1068 struct rte_cryptodev_session *
1069 rte_cryptodev_session_free(uint8_t dev_id, struct rte_cryptodev_session *sess)
1071 struct rte_cryptodev *dev;
1073 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1074 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1078 dev = &rte_crypto_devices[dev_id];
1080 /* Check the session belongs to this device type */
1081 if (sess->type != dev->dev_type)
1084 /* Let device implementation clear session material */
1085 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_clear, sess);
1086 dev->dev_ops->session_clear(dev, (void *)sess->_private);
1088 /* Return session to mempool */
1089 rte_mempool_put(sess->mp, (void *)sess);