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 */
105 #define RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG ("max_nb_queue_pairs")
106 #define RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG ("max_nb_sessions")
107 #define RTE_CRYPTODEV_VDEV_SOCKET_ID ("socket_id")
109 static const char *cryptodev_vdev_valid_params[] = {
110 RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
111 RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
112 RTE_CRYPTODEV_VDEV_SOCKET_ID
116 number_of_sockets(void)
120 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
122 for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
123 if (sockets < ms[i].socket_id)
124 sockets = ms[i].socket_id;
127 /* Number of sockets = maximum socket_id + 1 */
131 /** Parse integer from integer argument */
133 parse_integer_arg(const char *key __rte_unused,
134 const char *value, void *extra_args)
136 int *i = (int *) extra_args;
140 CDEV_LOG_ERR("Argument has to be positive.");
148 rte_cryptodev_parse_vdev_init_params(struct rte_crypto_vdev_init_params *params,
149 const char *input_args)
151 struct rte_kvargs *kvlist = NULL;
158 kvlist = rte_kvargs_parse(input_args,
159 cryptodev_vdev_valid_params);
163 ret = rte_kvargs_process(kvlist,
164 RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
166 ¶ms->max_nb_queue_pairs);
170 ret = rte_kvargs_process(kvlist,
171 RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
173 ¶ms->max_nb_sessions);
177 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
183 if (params->socket_id >= number_of_sockets()) {
184 CDEV_LOG_ERR("Invalid socket id specified to create "
185 "the virtual crypto device on");
191 rte_kvargs_free(kvlist);
196 rte_cryptodev_get_feature_name(uint64_t flag)
199 case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
200 return "SYMMETRIC_CRYPTO";
201 case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
202 return "ASYMMETRIC_CRYPTO";
203 case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
204 return "SYM_OPERATION_CHAINING";
205 case RTE_CRYPTODEV_FF_CPU_SSE:
207 case RTE_CRYPTODEV_FF_CPU_AVX:
209 case RTE_CRYPTODEV_FF_CPU_AVX2:
211 case RTE_CRYPTODEV_FF_CPU_AESNI:
213 case RTE_CRYPTODEV_FF_HW_ACCELERATED:
214 return "HW_ACCELERATED";
223 rte_cryptodev_create_vdev(const char *name, const char *args)
225 return rte_eal_vdev_init(name, args);
229 rte_cryptodev_get_dev_id(const char *name) {
235 for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
236 if ((strcmp(rte_cryptodev_globals->devs[i].data->name, name)
238 (rte_cryptodev_globals->devs[i].attached ==
239 RTE_CRYPTODEV_ATTACHED))
246 rte_cryptodev_count(void)
248 return rte_cryptodev_globals->nb_devs;
252 rte_cryptodev_count_devtype(enum rte_cryptodev_type type)
254 uint8_t i, dev_count = 0;
256 for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
257 if (rte_cryptodev_globals->devs[i].dev_type == type &&
258 rte_cryptodev_globals->devs[i].attached ==
259 RTE_CRYPTODEV_ATTACHED)
266 rte_cryptodev_socket_id(uint8_t dev_id)
268 struct rte_cryptodev *dev;
270 if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
273 dev = rte_cryptodev_pmd_get_dev(dev_id);
275 return dev->data->socket_id;
279 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
282 char mz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
283 const struct rte_memzone *mz;
286 /* generate memzone name */
287 n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
288 if (n >= (int)sizeof(mz_name))
291 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
292 mz = rte_memzone_reserve(mz_name,
293 sizeof(struct rte_cryptodev_data),
296 mz = rte_memzone_lookup(mz_name);
302 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
303 memset(*data, 0, sizeof(struct rte_cryptodev_data));
309 rte_cryptodev_find_free_device_index(void)
313 for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
314 if (rte_crypto_devices[dev_id].attached ==
315 RTE_CRYPTODEV_DETACHED)
318 return RTE_CRYPTO_MAX_DEVS;
321 struct rte_cryptodev *
322 rte_cryptodev_pmd_allocate(const char *name, enum pmd_type type, int socket_id)
324 struct rte_cryptodev *cryptodev;
327 if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
328 CDEV_LOG_ERR("Crypto device with name %s already "
333 dev_id = rte_cryptodev_find_free_device_index();
334 if (dev_id == RTE_CRYPTO_MAX_DEVS) {
335 CDEV_LOG_ERR("Reached maximum number of crypto devices");
339 cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
341 if (cryptodev->data == NULL) {
342 struct rte_cryptodev_data *cryptodev_data =
343 cryptodev_globals.data[dev_id];
345 int retval = rte_cryptodev_data_alloc(dev_id, &cryptodev_data,
348 if (retval < 0 || cryptodev_data == NULL)
351 cryptodev->data = cryptodev_data;
353 snprintf(cryptodev->data->name, RTE_CRYPTODEV_NAME_MAX_LEN,
356 cryptodev->data->dev_id = dev_id;
357 cryptodev->data->socket_id = socket_id;
358 cryptodev->data->dev_started = 0;
360 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
361 cryptodev->pmd_type = type;
363 cryptodev_globals.nb_devs++;
370 rte_cryptodev_create_unique_device_name(char *name, size_t size,
371 struct rte_pci_device *pci_dev)
375 if ((name == NULL) || (pci_dev == NULL))
378 ret = snprintf(name, size, "%d:%d.%d",
379 pci_dev->addr.bus, pci_dev->addr.devid,
380 pci_dev->addr.function);
387 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
391 if (cryptodev == NULL)
394 ret = rte_cryptodev_close(cryptodev->data->dev_id);
398 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
399 cryptodev_globals.nb_devs--;
403 struct rte_cryptodev *
404 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
407 struct rte_cryptodev *cryptodev;
409 /* allocate device structure */
410 cryptodev = rte_cryptodev_pmd_allocate(name, PMD_VDEV, socket_id);
411 if (cryptodev == NULL)
414 /* allocate private device structure */
415 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
416 cryptodev->data->dev_private =
417 rte_zmalloc_socket("cryptodev device private",
422 if (cryptodev->data->dev_private == NULL)
423 rte_panic("Cannot allocate memzone for private device"
427 /* initialise user call-back tail queue */
428 TAILQ_INIT(&(cryptodev->link_intr_cbs));
434 rte_cryptodev_init(struct rte_pci_driver *pci_drv,
435 struct rte_pci_device *pci_dev)
437 struct rte_cryptodev_driver *cryptodrv;
438 struct rte_cryptodev *cryptodev;
440 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
444 cryptodrv = (struct rte_cryptodev_driver *)pci_drv;
445 if (cryptodrv == NULL)
448 /* Create unique Crypto device name using PCI address */
449 rte_cryptodev_create_unique_device_name(cryptodev_name,
450 sizeof(cryptodev_name), pci_dev);
452 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, PMD_PDEV,
454 if (cryptodev == NULL)
457 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
458 cryptodev->data->dev_private =
460 "cryptodev private structure",
461 cryptodrv->dev_private_size,
465 if (cryptodev->data->dev_private == NULL)
466 rte_panic("Cannot allocate memzone for private "
470 cryptodev->pci_dev = pci_dev;
471 cryptodev->driver = cryptodrv;
473 /* init user callbacks */
474 TAILQ_INIT(&(cryptodev->link_intr_cbs));
476 /* Invoke PMD device initialization function */
477 retval = (*cryptodrv->cryptodev_init)(cryptodrv, cryptodev);
481 CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
482 " failed", pci_drv->name,
483 (unsigned) pci_dev->id.vendor_id,
484 (unsigned) pci_dev->id.device_id);
486 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
487 rte_free(cryptodev->data->dev_private);
489 cryptodev->attached = RTE_CRYPTODEV_DETACHED;
490 cryptodev_globals.nb_devs--;
496 rte_cryptodev_uninit(struct rte_pci_device *pci_dev)
498 const struct rte_cryptodev_driver *cryptodrv;
499 struct rte_cryptodev *cryptodev;
500 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
506 /* Create unique device name using PCI address */
507 rte_cryptodev_create_unique_device_name(cryptodev_name,
508 sizeof(cryptodev_name), pci_dev);
510 cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
511 if (cryptodev == NULL)
514 cryptodrv = (const struct rte_cryptodev_driver *)pci_dev->driver;
515 if (cryptodrv == NULL)
518 /* Invoke PMD device uninit function */
519 if (*cryptodrv->cryptodev_uninit) {
520 ret = (*cryptodrv->cryptodev_uninit)(cryptodrv, cryptodev);
525 /* free crypto device */
526 rte_cryptodev_pmd_release_device(cryptodev);
528 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
529 rte_free(cryptodev->data->dev_private);
531 cryptodev->pci_dev = NULL;
532 cryptodev->driver = NULL;
533 cryptodev->data = NULL;
539 rte_cryptodev_pmd_driver_register(struct rte_cryptodev_driver *cryptodrv,
542 /* Call crypto device initialization directly if device is virtual */
543 if (type == PMD_VDEV)
544 return rte_cryptodev_init((struct rte_pci_driver *)cryptodrv,
548 * Register PCI driver for physical device intialisation during
551 cryptodrv->pci_drv.devinit = rte_cryptodev_init;
552 cryptodrv->pci_drv.devuninit = rte_cryptodev_uninit;
554 rte_eal_pci_register(&cryptodrv->pci_drv);
561 rte_cryptodev_queue_pair_count(uint8_t dev_id)
563 struct rte_cryptodev *dev;
565 dev = &rte_crypto_devices[dev_id];
566 return dev->data->nb_queue_pairs;
570 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
573 struct rte_cryptodev_info dev_info;
577 if ((dev == NULL) || (nb_qpairs < 1)) {
578 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
583 CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
584 nb_qpairs, dev->data->dev_id);
586 memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
588 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
589 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
591 if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
592 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
593 nb_qpairs, dev->data->dev_id);
597 if (dev->data->queue_pairs == NULL) { /* first time configuration */
598 dev->data->queue_pairs = rte_zmalloc_socket(
599 "cryptodev->queue_pairs",
600 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
601 RTE_CACHE_LINE_SIZE, socket_id);
603 if (dev->data->queue_pairs == NULL) {
604 dev->data->nb_queue_pairs = 0;
605 CDEV_LOG_ERR("failed to get memory for qp meta data, "
610 } else { /* re-configure */
612 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
614 qp = dev->data->queue_pairs;
616 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
619 for (i = nb_qpairs; i < old_nb_queues; i++) {
620 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
625 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
626 RTE_CACHE_LINE_SIZE);
628 CDEV_LOG_ERR("failed to realloc qp meta data,"
629 " nb_queues %u", nb_qpairs);
633 if (nb_qpairs > old_nb_queues) {
634 uint16_t new_qs = nb_qpairs - old_nb_queues;
636 memset(qp + old_nb_queues, 0,
637 sizeof(qp[0]) * new_qs);
640 dev->data->queue_pairs = qp;
643 dev->data->nb_queue_pairs = nb_qpairs;
648 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
650 struct rte_cryptodev *dev;
652 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
653 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
657 dev = &rte_crypto_devices[dev_id];
658 if (queue_pair_id >= dev->data->nb_queue_pairs) {
659 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
663 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
665 return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
670 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
672 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];
680 if (queue_pair_id >= dev->data->nb_queue_pairs) {
681 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
685 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
687 return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
692 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
693 unsigned nb_objs, unsigned obj_cache_size, int socket_id);
696 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
698 struct rte_cryptodev *dev;
701 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
702 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
706 dev = &rte_crypto_devices[dev_id];
708 if (dev->data->dev_started) {
710 "device %d must be stopped to allow configuration", dev_id);
714 /* Setup new number of queue pairs and reconfigure device. */
715 diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
718 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
723 /* Setup Session mempool for device */
724 return rte_cryptodev_sym_session_pool_create(dev,
725 config->session_mp.nb_objs,
726 config->session_mp.cache_size,
732 rte_cryptodev_start(uint8_t dev_id)
734 struct rte_cryptodev *dev;
737 CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
739 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
740 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
744 dev = &rte_crypto_devices[dev_id];
746 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
748 if (dev->data->dev_started != 0) {
749 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
754 diag = (*dev->dev_ops->dev_start)(dev);
756 dev->data->dev_started = 1;
764 rte_cryptodev_stop(uint8_t dev_id)
766 struct rte_cryptodev *dev;
768 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
769 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
773 dev = &rte_crypto_devices[dev_id];
775 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
777 if (dev->data->dev_started == 0) {
778 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
783 dev->data->dev_started = 0;
784 (*dev->dev_ops->dev_stop)(dev);
788 rte_cryptodev_close(uint8_t dev_id)
790 struct rte_cryptodev *dev;
793 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
794 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
798 dev = &rte_crypto_devices[dev_id];
800 /* Device must be stopped before it can be closed */
801 if (dev->data->dev_started == 1) {
802 CDEV_LOG_ERR("Device %u must be stopped before closing",
807 /* We can't close the device if there are outstanding sessions in use */
808 if (dev->data->session_pool != NULL) {
809 if (!rte_mempool_full(dev->data->session_pool)) {
810 CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
811 "has sessions still in use, free "
812 "all sessions before calling close",
818 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
819 retval = (*dev->dev_ops->dev_close)(dev);
828 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
829 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
831 struct rte_cryptodev *dev;
833 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
834 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
838 dev = &rte_crypto_devices[dev_id];
839 if (queue_pair_id >= dev->data->nb_queue_pairs) {
840 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
844 if (dev->data->dev_started) {
846 "device %d must be stopped to allow configuration", dev_id);
850 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
852 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
858 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
860 struct rte_cryptodev *dev;
862 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
863 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
868 CDEV_LOG_ERR("Invalid stats ptr");
872 dev = &rte_crypto_devices[dev_id];
873 memset(stats, 0, sizeof(*stats));
875 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
876 (*dev->dev_ops->stats_get)(dev, stats);
881 rte_cryptodev_stats_reset(uint8_t dev_id)
883 struct rte_cryptodev *dev;
885 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
886 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
890 dev = &rte_crypto_devices[dev_id];
892 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
893 (*dev->dev_ops->stats_reset)(dev);
898 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
900 struct rte_cryptodev *dev;
902 if (dev_id >= cryptodev_globals.nb_devs) {
903 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
907 dev = &rte_crypto_devices[dev_id];
909 memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
911 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
912 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
914 dev_info->pci_dev = dev->pci_dev;
916 dev_info->driver_name = dev->driver->pci_drv.name;
921 rte_cryptodev_callback_register(uint8_t dev_id,
922 enum rte_cryptodev_event_type event,
923 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
925 struct rte_cryptodev *dev;
926 struct rte_cryptodev_callback *user_cb;
931 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
932 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
936 dev = &rte_crypto_devices[dev_id];
937 rte_spinlock_lock(&rte_cryptodev_cb_lock);
939 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
940 if (user_cb->cb_fn == cb_fn &&
941 user_cb->cb_arg == cb_arg &&
942 user_cb->event == event) {
947 /* create a new callback. */
948 if (user_cb == NULL) {
949 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
950 sizeof(struct rte_cryptodev_callback), 0);
951 if (user_cb != NULL) {
952 user_cb->cb_fn = cb_fn;
953 user_cb->cb_arg = cb_arg;
954 user_cb->event = event;
955 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
959 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
960 return (user_cb == NULL) ? -ENOMEM : 0;
964 rte_cryptodev_callback_unregister(uint8_t dev_id,
965 enum rte_cryptodev_event_type event,
966 rte_cryptodev_cb_fn cb_fn, void *cb_arg)
969 struct rte_cryptodev *dev;
970 struct rte_cryptodev_callback *cb, *next;
975 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
976 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
980 dev = &rte_crypto_devices[dev_id];
981 rte_spinlock_lock(&rte_cryptodev_cb_lock);
984 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
986 next = TAILQ_NEXT(cb, next);
988 if (cb->cb_fn != cb_fn || cb->event != event ||
989 (cb->cb_arg != (void *)-1 &&
990 cb->cb_arg != cb_arg))
994 * if this callback is not executing right now,
997 if (cb->active == 0) {
998 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
1005 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1010 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1011 enum rte_cryptodev_event_type event)
1013 struct rte_cryptodev_callback *cb_lst;
1014 struct rte_cryptodev_callback dev_cb;
1016 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1017 TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1018 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1022 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1023 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1025 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1028 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1033 rte_cryptodev_sym_session_init(struct rte_mempool *mp,
1036 __rte_unused unsigned i)
1038 struct rte_cryptodev_sym_session *sess = _sess;
1039 struct rte_cryptodev *dev = opaque_arg;
1041 memset(sess, 0, mp->elt_size);
1043 sess->dev_id = dev->data->dev_id;
1044 sess->dev_type = dev->dev_type;
1047 if (dev->dev_ops->session_initialize)
1048 (*dev->dev_ops->session_initialize)(mp, sess);
1052 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
1053 unsigned nb_objs, unsigned obj_cache_size, int socket_id)
1055 char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1056 unsigned priv_sess_size;
1058 unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
1060 if (n > sizeof(mp_name)) {
1061 CDEV_LOG_ERR("Unable to create unique name for session mempool");
1065 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_get_size, -ENOTSUP);
1066 priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
1067 if (priv_sess_size == 0) {
1068 CDEV_LOG_ERR("%s returned and invalid private session size ",
1073 unsigned elt_size = sizeof(struct rte_cryptodev_sym_session) +
1076 dev->data->session_pool = rte_mempool_lookup(mp_name);
1077 if (dev->data->session_pool != NULL) {
1078 if ((dev->data->session_pool->elt_size != elt_size) ||
1079 (dev->data->session_pool->cache_size <
1081 (dev->data->session_pool->size < nb_objs)) {
1083 CDEV_LOG_ERR("%s mempool already exists with different"
1084 " initialization parameters", mp_name);
1085 dev->data->session_pool = NULL;
1089 dev->data->session_pool = rte_mempool_create(
1090 mp_name, /* mempool name */
1091 nb_objs, /* number of elements*/
1092 elt_size, /* element size*/
1093 obj_cache_size, /* Cache size*/
1094 0, /* private data size */
1095 NULL, /* obj initialization constructor */
1096 NULL, /* obj initialization constructor arg */
1097 rte_cryptodev_sym_session_init,
1098 /**< obj constructor*/
1099 dev, /* obj constructor arg */
1100 socket_id, /* socket id */
1103 if (dev->data->session_pool == NULL) {
1104 CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
1109 CDEV_LOG_DEBUG("%s mempool created!", mp_name);
1113 struct rte_cryptodev_sym_session *
1114 rte_cryptodev_sym_session_create(uint8_t dev_id,
1115 struct rte_crypto_sym_xform *xform)
1117 struct rte_cryptodev *dev;
1118 struct rte_cryptodev_sym_session *sess;
1121 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1122 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1126 dev = &rte_crypto_devices[dev_id];
1128 /* Allocate a session structure from the session pool */
1129 if (rte_mempool_get(dev->data->session_pool, &_sess)) {
1130 CDEV_LOG_ERR("Couldn't get object from session mempool");
1134 sess = (struct rte_cryptodev_sym_session *)_sess;
1136 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1137 if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1139 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1142 /* Return session to mempool */
1143 rte_mempool_put(sess->mp, _sess);
1150 struct rte_cryptodev_sym_session *
1151 rte_cryptodev_sym_session_free(uint8_t dev_id,
1152 struct rte_cryptodev_sym_session *sess)
1154 struct rte_cryptodev *dev;
1156 if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1157 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1161 dev = &rte_crypto_devices[dev_id];
1163 /* Check the session belongs to this device type */
1164 if (sess->dev_type != dev->dev_type)
1167 /* Let device implementation clear session material */
1168 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_clear, sess);
1169 dev->dev_ops->session_clear(dev, (void *)sess->_private);
1171 /* Return session to mempool */
1172 rte_mempool_put(sess->mp, (void *)sess);
1177 /** Initialise rte_crypto_op mempool element */
1179 rte_crypto_op_init(struct rte_mempool *mempool,
1182 __rte_unused unsigned i)
1184 struct rte_crypto_op *op = _op_data;
1185 enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1187 memset(_op_data, 0, mempool->elt_size);
1189 __rte_crypto_op_reset(op, type);
1191 op->phys_addr = rte_mem_virt2phy(_op_data);
1192 op->mempool = mempool;
1196 struct rte_mempool *
1197 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
1198 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
1201 struct rte_crypto_op_pool_private *priv;
1203 unsigned elt_size = sizeof(struct rte_crypto_op) +
1204 sizeof(struct rte_crypto_sym_op) +
1207 /* lookup mempool in case already allocated */
1208 struct rte_mempool *mp = rte_mempool_lookup(name);
1211 priv = (struct rte_crypto_op_pool_private *)
1212 rte_mempool_get_priv(mp);
1214 if (mp->elt_size != elt_size ||
1215 mp->cache_size < cache_size ||
1216 mp->size < nb_elts ||
1217 priv->priv_size < priv_size) {
1219 CDEV_LOG_ERR("Mempool %s already exists but with "
1220 "incompatible parameters", name);
1226 mp = rte_mempool_create(
1231 sizeof(struct rte_crypto_op_pool_private),
1240 CDEV_LOG_ERR("Failed to create mempool %s", name);
1244 priv = (struct rte_crypto_op_pool_private *)
1245 rte_mempool_get_priv(mp);
1247 priv->priv_size = priv_size;