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;
535 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
536 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
540 dev = &rte_crypto_devices[dev_id];
541 if (queue_pair_id >= dev->data->nb_queue_pairs) {
542 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
546 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
548 return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
553 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
555 struct rte_cryptodev *dev;
557 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
558 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
562 dev = &rte_crypto_devices[dev_id];
563 if (queue_pair_id >= dev->data->nb_queue_pairs) {
564 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
568 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
570 return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
575 rte_crypto_session_pool_create(struct rte_cryptodev *dev, unsigned nb_objs,
576 unsigned obj_cache_size, int socket_id);
579 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
581 struct rte_cryptodev *dev;
584 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
585 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
589 dev = &rte_crypto_devices[dev_id];
591 if (dev->data->dev_started) {
593 "device %d must be stopped to allow configuration", dev_id);
597 /* Setup new number of queue pairs and reconfigure device. */
598 diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
601 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
606 /* Setup Session mempool for device */
607 return rte_crypto_session_pool_create(dev, config->session_mp.nb_objs,
608 config->session_mp.cache_size, config->socket_id);
613 rte_cryptodev_start(uint8_t dev_id)
615 struct rte_cryptodev *dev;
618 CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
620 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
621 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
625 dev = &rte_crypto_devices[dev_id];
627 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
629 if (dev->data->dev_started != 0) {
630 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
635 diag = (*dev->dev_ops->dev_start)(dev);
637 dev->data->dev_started = 1;
645 rte_cryptodev_stop(uint8_t dev_id)
647 struct rte_cryptodev *dev;
649 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
650 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
654 dev = &rte_crypto_devices[dev_id];
656 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
658 if (dev->data->dev_started == 0) {
659 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
664 dev->data->dev_started = 0;
665 (*dev->dev_ops->dev_stop)(dev);
669 rte_cryptodev_close(uint8_t dev_id)
671 struct rte_cryptodev *dev;
674 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
675 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
679 dev = &rte_crypto_devices[dev_id];
681 /* Device must be stopped before it can be closed */
682 if (dev->data->dev_started == 1) {
683 CDEV_LOG_ERR("Device %u must be stopped before closing",
688 /* We can't close the device if there are outstanding sessions in use */
689 if (dev->data->session_pool != NULL) {
690 if (!rte_mempool_full(dev->data->session_pool)) {
691 CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
692 "has sessions still in use, free "
693 "all sessions before calling close",
699 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
700 retval = (*dev->dev_ops->dev_close)(dev);
709 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
710 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
712 struct rte_cryptodev *dev;
714 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
715 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
719 dev = &rte_crypto_devices[dev_id];
720 if (queue_pair_id >= dev->data->nb_queue_pairs) {
721 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
725 if (dev->data->dev_started) {
727 "device %d must be stopped to allow configuration", dev_id);
731 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
733 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
739 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
741 struct rte_cryptodev *dev;
743 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
744 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
749 CDEV_LOG_ERR("Invalid stats ptr");
753 dev = &rte_crypto_devices[dev_id];
754 memset(stats, 0, sizeof(*stats));
756 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
757 (*dev->dev_ops->stats_get)(dev, stats);
762 rte_cryptodev_stats_reset(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->stats_reset);
774 (*dev->dev_ops->stats_reset)(dev);
779 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
781 struct rte_cryptodev *dev;
783 if (dev_id >= cryptodev_globals.nb_devs) {
784 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
788 dev = &rte_crypto_devices[dev_id];
790 memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
792 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
793 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
795 dev_info->pci_dev = dev->pci_dev;
797 dev_info->driver_name = dev->driver->pci_drv.name;
802 rte_cryptodev_callback_register(uint8_t dev_id,
803 enum rte_cryptodev_event_type event,
804 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
806 struct rte_cryptodev *dev;
807 struct rte_cryptodev_callback *user_cb;
812 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
813 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
817 dev = &rte_crypto_devices[dev_id];
818 rte_spinlock_lock(&rte_cryptodev_cb_lock);
820 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
821 if (user_cb->cb_fn == cb_fn &&
822 user_cb->cb_arg == cb_arg &&
823 user_cb->event == event) {
828 /* create a new callback. */
829 if (user_cb == NULL) {
830 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
831 sizeof(struct rte_cryptodev_callback), 0);
832 if (user_cb != NULL) {
833 user_cb->cb_fn = cb_fn;
834 user_cb->cb_arg = cb_arg;
835 user_cb->event = event;
836 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
840 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
841 return (user_cb == NULL) ? -ENOMEM : 0;
845 rte_cryptodev_callback_unregister(uint8_t dev_id,
846 enum rte_cryptodev_event_type event,
847 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
850 struct rte_cryptodev *dev;
851 struct rte_cryptodev_callback *cb, *next;
856 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
857 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
861 dev = &rte_crypto_devices[dev_id];
862 rte_spinlock_lock(&rte_cryptodev_cb_lock);
865 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
867 next = TAILQ_NEXT(cb, next);
869 if (cb->cb_fn != cb_fn || cb->event != event ||
870 (cb->cb_arg != (void *)-1 &&
871 cb->cb_arg != cb_arg))
875 * if this callback is not executing right now,
878 if (cb->active == 0) {
879 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
886 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
891 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
892 enum rte_cryptodev_event_type event)
894 struct rte_cryptodev_callback *cb_lst;
895 struct rte_cryptodev_callback dev_cb;
897 rte_spinlock_lock(&rte_cryptodev_cb_lock);
898 TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
899 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
903 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
904 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
906 rte_spinlock_lock(&rte_cryptodev_cb_lock);
909 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
914 rte_crypto_session_init(struct rte_mempool *mp,
917 __rte_unused unsigned i)
919 struct rte_cryptodev_session *sess = _sess;
920 struct rte_cryptodev *dev = opaque_arg;
922 memset(sess, 0, mp->elt_size);
924 sess->dev_id = dev->data->dev_id;
925 sess->type = dev->dev_type;
928 if (dev->dev_ops->session_initialize)
929 (*dev->dev_ops->session_initialize)(mp, sess->_private);
933 rte_crypto_session_pool_create(struct rte_cryptodev *dev, unsigned nb_objs,
934 unsigned obj_cache_size, int socket_id)
936 char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
937 unsigned priv_sess_size;
939 unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
941 if (n > sizeof(mp_name)) {
942 CDEV_LOG_ERR("Unable to create unique name for session mempool");
946 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_get_size, -ENOTSUP);
947 priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
948 if (priv_sess_size == 0) {
949 CDEV_LOG_ERR("%s returned and invalid private session size ",
954 unsigned elt_size = sizeof(struct rte_cryptodev_session) +
957 dev->data->session_pool = rte_mempool_lookup(mp_name);
958 if (dev->data->session_pool != NULL) {
959 if ((dev->data->session_pool->elt_size != elt_size) ||
960 (dev->data->session_pool->cache_size <
962 (dev->data->session_pool->size < nb_objs)) {
964 CDEV_LOG_ERR("%s mempool already exists with different"
965 " initialization parameters", mp_name);
966 dev->data->session_pool = NULL;
970 dev->data->session_pool = rte_mempool_create(
971 mp_name, /* mempool name */
972 nb_objs, /* number of elements*/
973 elt_size, /* element size*/
974 obj_cache_size, /* Cache size*/
975 0, /* private data size */
976 NULL, /* obj initialization constructor */
977 NULL, /* obj initialization constructor arg */
978 rte_crypto_session_init, /* obj constructor */
979 dev, /* obj constructor arg */
980 socket_id, /* socket id */
983 if (dev->data->session_pool == NULL) {
984 CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
989 CDEV_LOG_DEBUG("%s mempool created!", mp_name);
993 struct rte_cryptodev_session *
994 rte_cryptodev_session_create(uint8_t dev_id, struct rte_crypto_xform *xform)
996 struct rte_cryptodev *dev;
997 struct rte_cryptodev_session *sess;
1000 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1001 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1005 dev = &rte_crypto_devices[dev_id];
1007 /* Allocate a session structure from the session pool */
1008 if (rte_mempool_get(dev->data->session_pool, &_sess)) {
1009 CDEV_LOG_ERR("Couldn't get object from session mempool");
1013 sess = (struct rte_cryptodev_session *)_sess;
1015 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1016 if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1018 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1021 /* Return session to mempool */
1022 rte_mempool_put(sess->mp, _sess);
1029 struct rte_cryptodev_session *
1030 rte_cryptodev_session_free(uint8_t dev_id, struct rte_cryptodev_session *sess)
1032 struct rte_cryptodev *dev;
1034 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1035 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1039 dev = &rte_crypto_devices[dev_id];
1041 /* Check the session belongs to this device type */
1042 if (sess->type != dev->dev_type)
1045 /* Let device implementation clear session material */
1046 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_clear, sess);
1047 dev->dev_ops->session_clear(dev, (void *)sess->_private);
1049 /* Return session to mempool */
1050 rte_mempool_put(sess->mp, (void *)sess);