aa4ea42591cc7a79abf841be2086cff0252e09a3
[deb_dpdk.git] / lib / librte_cryptodev / rte_cryptodev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <errno.h>
41 #include <stdint.h>
42 #include <inttypes.h>
43 #include <netinet/in.h>
44
45 #include <rte_byteorder.h>
46 #include <rte_log.h>
47 #include <rte_debug.h>
48 #include <rte_dev.h>
49 #include <rte_interrupts.h>
50 #include <rte_pci.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>
56 #include <rte_eal.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_common.h>
62 #include <rte_ring.h>
63 #include <rte_mempool.h>
64 #include <rte_malloc.h>
65 #include <rte_mbuf.h>
66 #include <rte_errno.h>
67 #include <rte_spinlock.h>
68 #include <rte_string_fns.h>
69
70 #include "rte_crypto.h"
71 #include "rte_cryptodev.h"
72 #include "rte_cryptodev_pmd.h"
73
74 struct rte_cryptodev rte_crypto_devices[RTE_CRYPTO_MAX_DEVS];
75
76 struct rte_cryptodev *rte_cryptodevs = &rte_crypto_devices[0];
77
78 static struct rte_cryptodev_global cryptodev_globals = {
79                 .devs                   = &rte_crypto_devices[0],
80                 .data                   = { NULL },
81                 .nb_devs                = 0,
82                 .max_devs               = RTE_CRYPTO_MAX_DEVS
83 };
84
85 struct rte_cryptodev_global *rte_cryptodev_globals = &cryptodev_globals;
86
87 /* spinlock for crypto device callbacks */
88 static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
89
90
91 /**
92  * The user application callback description.
93  *
94  * It contains callback address to be registered by user application,
95  * the pointer to the parameters for callback, and the event type.
96  */
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 */
103 };
104
105
106 const char *
107 rte_cryptodev_get_feature_name(uint64_t flag)
108 {
109         switch (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:
117                 return "CPU_SSE";
118         case RTE_CRYPTODEV_FF_CPU_AVX:
119                 return "CPU_AVX";
120         case RTE_CRYPTODEV_FF_CPU_AVX2:
121                 return "CPU_AVX2";
122         case RTE_CRYPTODEV_FF_CPU_AESNI:
123                 return "CPU_AESNI";
124         case RTE_CRYPTODEV_FF_HW_ACCELERATED:
125                 return "HW_ACCELERATED";
126
127         default:
128                 return NULL;
129         }
130 }
131
132
133 int
134 rte_cryptodev_create_vdev(const char *name, const char *args)
135 {
136         return rte_eal_vdev_init(name, args);
137 }
138
139 int
140 rte_cryptodev_get_dev_id(const char *name) {
141         unsigned i;
142
143         if (name == NULL)
144                 return -1;
145
146         for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
147                 if ((strcmp(rte_cryptodev_globals->devs[i].data->name, name)
148                                 == 0) &&
149                                 (rte_cryptodev_globals->devs[i].attached ==
150                                                 RTE_CRYPTODEV_ATTACHED))
151                         return i;
152
153         return -1;
154 }
155
156 uint8_t
157 rte_cryptodev_count(void)
158 {
159         return rte_cryptodev_globals->nb_devs;
160 }
161
162 uint8_t
163 rte_cryptodev_count_devtype(enum rte_cryptodev_type type)
164 {
165         uint8_t i, dev_count = 0;
166
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)
171                         dev_count++;
172
173         return dev_count;
174 }
175
176 int
177 rte_cryptodev_socket_id(uint8_t dev_id)
178 {
179         struct rte_cryptodev *dev;
180
181         if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
182                 return -1;
183
184         dev = rte_cryptodev_pmd_get_dev(dev_id);
185
186         return dev->data->socket_id;
187 }
188
189 static inline int
190 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
191                 int socket_id)
192 {
193         char mz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
194         const struct rte_memzone *mz;
195         int n;
196
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))
200                 return -EINVAL;
201
202         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
203                 mz = rte_memzone_reserve(mz_name,
204                                 sizeof(struct rte_cryptodev_data),
205                                 socket_id, 0);
206         } else
207                 mz = rte_memzone_lookup(mz_name);
208
209         if (mz == NULL)
210                 return -ENOMEM;
211
212         *data = mz->addr;
213         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
214                 memset(*data, 0, sizeof(struct rte_cryptodev_data));
215
216         return 0;
217 }
218
219 static uint8_t
220 rte_cryptodev_find_free_device_index(void)
221 {
222         uint8_t dev_id;
223
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)
227                         return dev_id;
228         }
229         return RTE_CRYPTO_MAX_DEVS;
230 }
231
232 struct rte_cryptodev *
233 rte_cryptodev_pmd_allocate(const char *name, enum pmd_type type, int socket_id)
234 {
235         struct rte_cryptodev *cryptodev;
236         uint8_t dev_id;
237
238         if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
239                 CDEV_LOG_ERR("Crypto device with name %s already "
240                                 "allocated!", name);
241                 return NULL;
242         }
243
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");
247                 return NULL;
248         }
249
250         cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
251
252         if (cryptodev->data == NULL) {
253                 struct rte_cryptodev_data *cryptodev_data =
254                                 cryptodev_globals.data[dev_id];
255
256                 int retval = rte_cryptodev_data_alloc(dev_id, &cryptodev_data,
257                                 socket_id);
258
259                 if (retval < 0 || cryptodev_data == NULL)
260                         return NULL;
261
262                 cryptodev->data = cryptodev_data;
263
264                 snprintf(cryptodev->data->name, RTE_CRYPTODEV_NAME_MAX_LEN,
265                                 "%s", name);
266
267                 cryptodev->data->dev_id = dev_id;
268                 cryptodev->data->socket_id = socket_id;
269                 cryptodev->data->dev_started = 0;
270
271                 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
272                 cryptodev->pmd_type = type;
273
274                 cryptodev_globals.nb_devs++;
275         }
276
277         return cryptodev;
278 }
279
280 static inline int
281 rte_cryptodev_create_unique_device_name(char *name, size_t size,
282                 struct rte_pci_device *pci_dev)
283 {
284         int ret;
285
286         if ((name == NULL) || (pci_dev == NULL))
287                 return -EINVAL;
288
289         ret = snprintf(name, size, "%d:%d.%d",
290                         pci_dev->addr.bus, pci_dev->addr.devid,
291                         pci_dev->addr.function);
292         if (ret < 0)
293                 return ret;
294         return 0;
295 }
296
297 int
298 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
299 {
300         int ret;
301
302         if (cryptodev == NULL)
303                 return -EINVAL;
304
305         ret = rte_cryptodev_close(cryptodev->data->dev_id);
306         if (ret < 0)
307                 return ret;
308
309         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
310         cryptodev_globals.nb_devs--;
311         return 0;
312 }
313
314 struct rte_cryptodev *
315 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
316                 int socket_id)
317 {
318         struct rte_cryptodev *cryptodev;
319
320         /* allocate device structure */
321         cryptodev = rte_cryptodev_pmd_allocate(name, PMD_VDEV, socket_id);
322         if (cryptodev == NULL)
323                 return NULL;
324
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",
329                                                 dev_private_size,
330                                                 RTE_CACHE_LINE_SIZE,
331                                                 socket_id);
332
333                 if (cryptodev->data->dev_private == NULL)
334                         rte_panic("Cannot allocate memzone for private device"
335                                         " data");
336         }
337
338         /* initialise user call-back tail queue */
339         TAILQ_INIT(&(cryptodev->link_intr_cbs));
340
341         return cryptodev;
342 }
343
344 static int
345 rte_cryptodev_init(struct rte_pci_driver *pci_drv,
346                 struct rte_pci_device *pci_dev)
347 {
348         struct rte_cryptodev_driver *cryptodrv;
349         struct rte_cryptodev *cryptodev;
350
351         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
352
353         int retval;
354
355         cryptodrv = (struct rte_cryptodev_driver *)pci_drv;
356         if (cryptodrv == NULL)
357                 return -ENODEV;
358
359         /* Create unique Crypto device name using PCI address */
360         rte_cryptodev_create_unique_device_name(cryptodev_name,
361                         sizeof(cryptodev_name), pci_dev);
362
363         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, PMD_PDEV,
364                         rte_socket_id());
365         if (cryptodev == NULL)
366                 return -ENOMEM;
367
368         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
369                 cryptodev->data->dev_private =
370                                 rte_zmalloc_socket(
371                                                 "cryptodev private structure",
372                                                 cryptodrv->dev_private_size,
373                                                 RTE_CACHE_LINE_SIZE,
374                                                 rte_socket_id());
375
376                 if (cryptodev->data->dev_private == NULL)
377                         rte_panic("Cannot allocate memzone for private "
378                                         "device data");
379         }
380
381         cryptodev->pci_dev = pci_dev;
382         cryptodev->driver = cryptodrv;
383
384         /* init user callbacks */
385         TAILQ_INIT(&(cryptodev->link_intr_cbs));
386
387         /* Invoke PMD device initialization function */
388         retval = (*cryptodrv->cryptodev_init)(cryptodrv, cryptodev);
389         if (retval == 0)
390                 return 0;
391
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);
396
397         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
398                 rte_free(cryptodev->data->dev_private);
399
400         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
401         cryptodev_globals.nb_devs--;
402
403         return -ENXIO;
404 }
405
406 static int
407 rte_cryptodev_uninit(struct rte_pci_device *pci_dev)
408 {
409         const struct rte_cryptodev_driver *cryptodrv;
410         struct rte_cryptodev *cryptodev;
411         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
412         int ret;
413
414         if (pci_dev == NULL)
415                 return -EINVAL;
416
417         /* Create unique device name using PCI address */
418         rte_cryptodev_create_unique_device_name(cryptodev_name,
419                         sizeof(cryptodev_name), pci_dev);
420
421         cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
422         if (cryptodev == NULL)
423                 return -ENODEV;
424
425         cryptodrv = (const struct rte_cryptodev_driver *)pci_dev->driver;
426         if (cryptodrv == NULL)
427                 return -ENODEV;
428
429         /* Invoke PMD device uninit function */
430         if (*cryptodrv->cryptodev_uninit) {
431                 ret = (*cryptodrv->cryptodev_uninit)(cryptodrv, cryptodev);
432                 if (ret)
433                         return ret;
434         }
435
436         /* free crypto device */
437         rte_cryptodev_pmd_release_device(cryptodev);
438
439         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
440                 rte_free(cryptodev->data->dev_private);
441
442         cryptodev->pci_dev = NULL;
443         cryptodev->driver = NULL;
444         cryptodev->data = NULL;
445
446         return 0;
447 }
448
449 int
450 rte_cryptodev_pmd_driver_register(struct rte_cryptodev_driver *cryptodrv,
451                 enum pmd_type type)
452 {
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,
456                                 NULL);
457
458         /*
459          * Register PCI driver for physical device intialisation during
460          * PCI probing
461          */
462         cryptodrv->pci_drv.devinit = rte_cryptodev_init;
463         cryptodrv->pci_drv.devuninit = rte_cryptodev_uninit;
464
465         rte_eal_pci_register(&cryptodrv->pci_drv);
466
467         return 0;
468 }
469
470
471 uint16_t
472 rte_cryptodev_queue_pair_count(uint8_t dev_id)
473 {
474         struct rte_cryptodev *dev;
475
476         dev = &rte_crypto_devices[dev_id];
477         return dev->data->nb_queue_pairs;
478 }
479
480 static int
481 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
482                 int socket_id)
483 {
484         struct rte_cryptodev_info dev_info;
485         void **qp;
486         unsigned i;
487
488         if ((dev == NULL) || (nb_qpairs < 1)) {
489                 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
490                                                         dev, nb_qpairs);
491                 return -EINVAL;
492         }
493
494         CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
495                         nb_qpairs, dev->data->dev_id);
496
497         memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
498
499         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
500         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
501
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);
505             return -EINVAL;
506         }
507
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);
513
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, "
517                                                         "nb_queues %u",
518                                                         nb_qpairs);
519                         return -(ENOMEM);
520                 }
521         } else { /* re-configure */
522                 int ret;
523                 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
524
525                 qp = dev->data->queue_pairs;
526
527                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
528                                 -ENOTSUP);
529
530                 for (i = nb_qpairs; i < old_nb_queues; i++) {
531                         ret = (*dev->dev_ops->queue_pair_release)(dev, i);
532                         if (ret < 0)
533                                 return ret;
534                 }
535
536                 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
537                                 RTE_CACHE_LINE_SIZE);
538                 if (qp == NULL) {
539                         CDEV_LOG_ERR("failed to realloc qp meta data,"
540                                                 " nb_queues %u", nb_qpairs);
541                         return -(ENOMEM);
542                 }
543
544                 if (nb_qpairs > old_nb_queues) {
545                         uint16_t new_qs = nb_qpairs - old_nb_queues;
546
547                         memset(qp + old_nb_queues, 0,
548                                 sizeof(qp[0]) * new_qs);
549                 }
550
551                 dev->data->queue_pairs = qp;
552
553         }
554         dev->data->nb_queue_pairs = nb_qpairs;
555         return 0;
556 }
557
558 int
559 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
560 {
561         struct rte_cryptodev *dev;
562
563         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
564                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
565                 return -EINVAL;
566         }
567
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);
571                 return -EINVAL;
572         }
573
574         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
575
576         return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
577
578 }
579
580 int
581 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
582 {
583         struct rte_cryptodev *dev;
584
585         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
586                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
587                 return -EINVAL;
588         }
589
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);
593                 return -EINVAL;
594         }
595
596         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
597
598         return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
599
600 }
601
602 static int
603 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
604                 unsigned nb_objs, unsigned obj_cache_size, int socket_id);
605
606 int
607 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
608 {
609         struct rte_cryptodev *dev;
610         int diag;
611
612         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
613                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
614                 return -EINVAL;
615         }
616
617         dev = &rte_crypto_devices[dev_id];
618
619         if (dev->data->dev_started) {
620                 CDEV_LOG_ERR(
621                     "device %d must be stopped to allow configuration", dev_id);
622                 return -EBUSY;
623         }
624
625         /* Setup new number of queue pairs and reconfigure device. */
626         diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
627                         config->socket_id);
628         if (diag != 0) {
629                 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
630                                 dev_id, diag);
631                 return diag;
632         }
633
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,
638                         config->socket_id);
639 }
640
641
642 int
643 rte_cryptodev_start(uint8_t dev_id)
644 {
645         struct rte_cryptodev *dev;
646         int diag;
647
648         CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
649
650         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
651                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
652                 return -EINVAL;
653         }
654
655         dev = &rte_crypto_devices[dev_id];
656
657         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
658
659         if (dev->data->dev_started != 0) {
660                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
661                         dev_id);
662                 return 0;
663         }
664
665         diag = (*dev->dev_ops->dev_start)(dev);
666         if (diag == 0)
667                 dev->data->dev_started = 1;
668         else
669                 return diag;
670
671         return 0;
672 }
673
674 void
675 rte_cryptodev_stop(uint8_t dev_id)
676 {
677         struct rte_cryptodev *dev;
678
679         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
680                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
681                 return;
682         }
683
684         dev = &rte_crypto_devices[dev_id];
685
686         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
687
688         if (dev->data->dev_started == 0) {
689                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
690                         dev_id);
691                 return;
692         }
693
694         dev->data->dev_started = 0;
695         (*dev->dev_ops->dev_stop)(dev);
696 }
697
698 int
699 rte_cryptodev_close(uint8_t dev_id)
700 {
701         struct rte_cryptodev *dev;
702         int retval;
703
704         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
705                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
706                 return -1;
707         }
708
709         dev = &rte_crypto_devices[dev_id];
710
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",
714                                 dev_id);
715                 return -EBUSY;
716         }
717
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",
724                                         (unsigned)dev_id);
725                         return -EBUSY;
726                 }
727         }
728
729         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
730         retval = (*dev->dev_ops->dev_close)(dev);
731
732         if (retval < 0)
733                 return retval;
734
735         return 0;
736 }
737
738 int
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)
741 {
742         struct rte_cryptodev *dev;
743
744         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
745                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
746                 return -EINVAL;
747         }
748
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);
752                 return -EINVAL;
753         }
754
755         if (dev->data->dev_started) {
756                 CDEV_LOG_ERR(
757                     "device %d must be stopped to allow configuration", dev_id);
758                 return -EBUSY;
759         }
760
761         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
762
763         return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
764                         socket_id);
765 }
766
767
768 int
769 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
770 {
771         struct rte_cryptodev *dev;
772
773         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
774                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
775                 return -ENODEV;
776         }
777
778         if (stats == NULL) {
779                 CDEV_LOG_ERR("Invalid stats ptr");
780                 return -EINVAL;
781         }
782
783         dev = &rte_crypto_devices[dev_id];
784         memset(stats, 0, sizeof(*stats));
785
786         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
787         (*dev->dev_ops->stats_get)(dev, stats);
788         return 0;
789 }
790
791 void
792 rte_cryptodev_stats_reset(uint8_t dev_id)
793 {
794         struct rte_cryptodev *dev;
795
796         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
797                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
798                 return;
799         }
800
801         dev = &rte_crypto_devices[dev_id];
802
803         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
804         (*dev->dev_ops->stats_reset)(dev);
805 }
806
807
808 void
809 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
810 {
811         struct rte_cryptodev *dev;
812
813         if (dev_id >= cryptodev_globals.nb_devs) {
814                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
815                 return;
816         }
817
818         dev = &rte_crypto_devices[dev_id];
819
820         memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
821
822         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
823         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
824
825         dev_info->pci_dev = dev->pci_dev;
826         if (dev->driver)
827                 dev_info->driver_name = dev->driver->pci_drv.name;
828 }
829
830
831 int
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)
835 {
836         struct rte_cryptodev *dev;
837         struct rte_cryptodev_callback *user_cb;
838
839         if (!cb_fn)
840                 return -EINVAL;
841
842         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
843                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
844                 return -EINVAL;
845         }
846
847         dev = &rte_crypto_devices[dev_id];
848         rte_spinlock_lock(&rte_cryptodev_cb_lock);
849
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) {
854                         break;
855                 }
856         }
857
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);
867                 }
868         }
869
870         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
871         return (user_cb == NULL) ? -ENOMEM : 0;
872 }
873
874 int
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)
878 {
879         int ret;
880         struct rte_cryptodev *dev;
881         struct rte_cryptodev_callback *cb, *next;
882
883         if (!cb_fn)
884                 return -EINVAL;
885
886         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
887                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
888                 return -EINVAL;
889         }
890
891         dev = &rte_crypto_devices[dev_id];
892         rte_spinlock_lock(&rte_cryptodev_cb_lock);
893
894         ret = 0;
895         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
896
897                 next = TAILQ_NEXT(cb, next);
898
899                 if (cb->cb_fn != cb_fn || cb->event != event ||
900                                 (cb->cb_arg != (void *)-1 &&
901                                 cb->cb_arg != cb_arg))
902                         continue;
903
904                 /*
905                  * if this callback is not executing right now,
906                  * then remove it.
907                  */
908                 if (cb->active == 0) {
909                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
910                         rte_free(cb);
911                 } else {
912                         ret = -EAGAIN;
913                 }
914         }
915
916         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
917         return ret;
918 }
919
920 void
921 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
922         enum rte_cryptodev_event_type event)
923 {
924         struct rte_cryptodev_callback *cb_lst;
925         struct rte_cryptodev_callback dev_cb;
926
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)
930                         continue;
931                 dev_cb = *cb_lst;
932                 cb_lst->active = 1;
933                 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
934                 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
935                                                 dev_cb.cb_arg);
936                 rte_spinlock_lock(&rte_cryptodev_cb_lock);
937                 cb_lst->active = 0;
938         }
939         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
940 }
941
942
943 static void
944 rte_cryptodev_sym_session_init(struct rte_mempool *mp,
945                 void *opaque_arg,
946                 void *_sess,
947                 __rte_unused unsigned i)
948 {
949         struct rte_cryptodev_sym_session *sess = _sess;
950         struct rte_cryptodev *dev = opaque_arg;
951
952         memset(sess, 0, mp->elt_size);
953
954         sess->dev_id = dev->data->dev_id;
955         sess->dev_type = dev->dev_type;
956         sess->mp = mp;
957
958         if (dev->dev_ops->session_initialize)
959                 (*dev->dev_ops->session_initialize)(mp, sess->_private);
960 }
961
962 static int
963 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
964                 unsigned nb_objs, unsigned obj_cache_size, int socket_id)
965 {
966         char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
967         unsigned priv_sess_size;
968
969         unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
970                         dev->data->dev_id);
971         if (n > sizeof(mp_name)) {
972                 CDEV_LOG_ERR("Unable to create unique name for session mempool");
973                 return -ENOMEM;
974         }
975
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 ",
980                                                 dev->data->name);
981                 return -ENOMEM;
982         }
983
984         unsigned elt_size = sizeof(struct rte_cryptodev_sym_session) +
985                         priv_sess_size;
986
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 <
991                                 obj_cache_size) ||
992                                 (dev->data->session_pool->size < nb_objs)) {
993
994                         CDEV_LOG_ERR("%s mempool already exists with different"
995                                         " initialization parameters", mp_name);
996                         dev->data->session_pool = NULL;
997                         return -ENOMEM;
998                 }
999         } else {
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 */
1012                                 0); /* flags */
1013
1014                 if (dev->data->session_pool == NULL) {
1015                         CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
1016                         return -ENOMEM;
1017                 }
1018         }
1019
1020         CDEV_LOG_DEBUG("%s mempool created!", mp_name);
1021         return 0;
1022 }
1023
1024 struct rte_cryptodev_sym_session *
1025 rte_cryptodev_sym_session_create(uint8_t dev_id,
1026                 struct rte_crypto_sym_xform *xform)
1027 {
1028         struct rte_cryptodev *dev;
1029         struct rte_cryptodev_sym_session *sess;
1030         void *_sess;
1031
1032         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1033                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1034                 return NULL;
1035         }
1036
1037         dev = &rte_crypto_devices[dev_id];
1038
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");
1042                 return NULL;
1043         }
1044
1045         sess = (struct rte_cryptodev_sym_session *)_sess;
1046
1047         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1048         if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1049                         NULL) {
1050                 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1051                                 dev_id);
1052
1053                 /* Return session to mempool */
1054                 rte_mempool_put(sess->mp, _sess);
1055                 return NULL;
1056         }
1057
1058         return sess;
1059 }
1060
1061 struct rte_cryptodev_sym_session *
1062 rte_cryptodev_sym_session_free(uint8_t dev_id,
1063                 struct rte_cryptodev_sym_session *sess)
1064 {
1065         struct rte_cryptodev *dev;
1066
1067         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1068                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1069                 return sess;
1070         }
1071
1072         dev = &rte_crypto_devices[dev_id];
1073
1074         /* Check the session belongs to this device type */
1075         if (sess->dev_type != dev->dev_type)
1076                 return sess;
1077
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);
1081
1082         /* Return session to mempool */
1083         rte_mempool_put(sess->mp, (void *)sess);
1084
1085         return NULL;
1086 }
1087
1088 /** Initialise rte_crypto_op mempool element */
1089 static void
1090 rte_crypto_op_init(struct rte_mempool *mempool,
1091                 void *opaque_arg,
1092                 void *_op_data,
1093                 __rte_unused unsigned i)
1094 {
1095         struct rte_crypto_op *op = _op_data;
1096         enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1097
1098         memset(_op_data, 0, mempool->elt_size);
1099
1100         __rte_crypto_op_reset(op, type);
1101
1102         op->phys_addr = rte_mem_virt2phy(_op_data);
1103         op->mempool = mempool;
1104 }
1105
1106
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,
1110                 int socket_id)
1111 {
1112         struct rte_crypto_op_pool_private *priv;
1113
1114         unsigned elt_size = sizeof(struct rte_crypto_op) +
1115                         sizeof(struct rte_crypto_sym_op) +
1116                         priv_size;
1117
1118         /* lookup mempool in case already allocated */
1119         struct rte_mempool *mp = rte_mempool_lookup(name);
1120
1121         if (mp != NULL) {
1122                 priv = (struct rte_crypto_op_pool_private *)
1123                                 rte_mempool_get_priv(mp);
1124
1125                 if (mp->elt_size != elt_size ||
1126                                 mp->cache_size < cache_size ||
1127                                 mp->size < nb_elts ||
1128                                 priv->priv_size <  priv_size) {
1129                         mp = NULL;
1130                         CDEV_LOG_ERR("Mempool %s already exists but with "
1131                                         "incompatible parameters", name);
1132                         return NULL;
1133                 }
1134                 return mp;
1135         }
1136
1137         mp = rte_mempool_create(
1138                         name,
1139                         nb_elts,
1140                         elt_size,
1141                         cache_size,
1142                         sizeof(struct rte_crypto_op_pool_private),
1143                         NULL,
1144                         NULL,
1145                         rte_crypto_op_init,
1146                         &type,
1147                         socket_id,
1148                         0);
1149
1150         if (mp == NULL) {
1151                 CDEV_LOG_ERR("Failed to create mempool %s", name);
1152                 return NULL;
1153         }
1154
1155         priv = (struct rte_crypto_op_pool_private *)
1156                         rte_mempool_get_priv(mp);
1157
1158         priv->priv_size = priv_size;
1159         priv->type = type;
1160
1161         return mp;
1162 }