ea75562e9e247fb0230c9a2ffce17c6c0fdee4d6
[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_mempool.h>
63 #include <rte_malloc.h>
64 #include <rte_mbuf.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
67 #include <rte_string_fns.h>
68
69 #include "rte_crypto.h"
70 #include "rte_cryptodev.h"
71 #include "rte_cryptodev_pmd.h"
72
73 struct rte_cryptodev rte_crypto_devices[RTE_CRYPTO_MAX_DEVS];
74
75 struct rte_cryptodev *rte_cryptodevs = &rte_crypto_devices[0];
76
77 static struct rte_cryptodev_global cryptodev_globals = {
78                 .devs                   = &rte_crypto_devices[0],
79                 .data                   = { NULL },
80                 .nb_devs                = 0,
81                 .max_devs               = RTE_CRYPTO_MAX_DEVS
82 };
83
84 struct rte_cryptodev_global *rte_cryptodev_globals = &cryptodev_globals;
85
86 /* spinlock for crypto device callbacks */
87 static rte_spinlock_t rte_cryptodev_cb_lock = RTE_SPINLOCK_INITIALIZER;
88
89
90 /**
91  * The user application callback description.
92  *
93  * It contains callback address to be registered by user application,
94  * the pointer to the parameters for callback, and the event type.
95  */
96 struct rte_cryptodev_callback {
97         TAILQ_ENTRY(rte_cryptodev_callback) next; /**< Callbacks list */
98         rte_cryptodev_cb_fn cb_fn;              /**< Callback address */
99         void *cb_arg;                           /**< Parameter for callback */
100         enum rte_cryptodev_event_type event;    /**< Interrupt event type */
101         uint32_t active;                        /**< Callback is executing */
102 };
103
104 #define RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG                ("max_nb_queue_pairs")
105 #define RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG              ("max_nb_sessions")
106 #define RTE_CRYPTODEV_VDEV_SOCKET_ID                    ("socket_id")
107
108 static const char *cryptodev_vdev_valid_params[] = {
109         RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
110         RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
111         RTE_CRYPTODEV_VDEV_SOCKET_ID
112 };
113
114 static uint8_t
115 number_of_sockets(void)
116 {
117         int sockets = 0;
118         int i;
119         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
120
121         for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
122                 if (sockets < ms[i].socket_id)
123                         sockets = ms[i].socket_id;
124         }
125
126         /* Number of sockets = maximum socket_id + 1 */
127         return ++sockets;
128 }
129
130 /** Parse integer from integer argument */
131 static int
132 parse_integer_arg(const char *key __rte_unused,
133                 const char *value, void *extra_args)
134 {
135         int *i = (int *) extra_args;
136
137         *i = atoi(value);
138         if (*i < 0) {
139                 CDEV_LOG_ERR("Argument has to be positive.");
140                 return -1;
141         }
142
143         return 0;
144 }
145
146 int
147 rte_cryptodev_parse_vdev_init_params(struct rte_crypto_vdev_init_params *params,
148                 const char *input_args)
149 {
150         struct rte_kvargs *kvlist = NULL;
151         int ret = 0;
152
153         if (params == NULL)
154                 return -EINVAL;
155
156         if (input_args) {
157                 kvlist = rte_kvargs_parse(input_args,
158                                 cryptodev_vdev_valid_params);
159                 if (kvlist == NULL)
160                         return -1;
161
162                 ret = rte_kvargs_process(kvlist,
163                                         RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
164                                         &parse_integer_arg,
165                                         &params->max_nb_queue_pairs);
166                 if (ret < 0)
167                         goto free_kvlist;
168
169                 ret = rte_kvargs_process(kvlist,
170                                         RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
171                                         &parse_integer_arg,
172                                         &params->max_nb_sessions);
173                 if (ret < 0)
174                         goto free_kvlist;
175
176                 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
177                                         &parse_integer_arg,
178                                         &params->socket_id);
179                 if (ret < 0)
180                         goto free_kvlist;
181
182                 if (params->socket_id >= number_of_sockets()) {
183                         CDEV_LOG_ERR("Invalid socket id specified to create "
184                                 "the virtual crypto device on");
185                         goto free_kvlist;
186                 }
187         }
188
189 free_kvlist:
190         rte_kvargs_free(kvlist);
191         return ret;
192 }
193
194 const char *
195 rte_cryptodev_get_feature_name(uint64_t flag)
196 {
197         switch (flag) {
198         case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
199                 return "SYMMETRIC_CRYPTO";
200         case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
201                 return "ASYMMETRIC_CRYPTO";
202         case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
203                 return "SYM_OPERATION_CHAINING";
204         case RTE_CRYPTODEV_FF_CPU_SSE:
205                 return "CPU_SSE";
206         case RTE_CRYPTODEV_FF_CPU_AVX:
207                 return "CPU_AVX";
208         case RTE_CRYPTODEV_FF_CPU_AVX2:
209                 return "CPU_AVX2";
210         case RTE_CRYPTODEV_FF_CPU_AESNI:
211                 return "CPU_AESNI";
212         case RTE_CRYPTODEV_FF_HW_ACCELERATED:
213                 return "HW_ACCELERATED";
214
215         default:
216                 return NULL;
217         }
218 }
219
220
221 int
222 rte_cryptodev_create_vdev(const char *name, const char *args)
223 {
224         return rte_eal_vdev_init(name, args);
225 }
226
227 int
228 rte_cryptodev_get_dev_id(const char *name) {
229         unsigned i;
230
231         if (name == NULL)
232                 return -1;
233
234         for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
235                 if ((strcmp(rte_cryptodev_globals->devs[i].data->name, name)
236                                 == 0) &&
237                                 (rte_cryptodev_globals->devs[i].attached ==
238                                                 RTE_CRYPTODEV_ATTACHED))
239                         return i;
240
241         return -1;
242 }
243
244 uint8_t
245 rte_cryptodev_count(void)
246 {
247         return rte_cryptodev_globals->nb_devs;
248 }
249
250 uint8_t
251 rte_cryptodev_count_devtype(enum rte_cryptodev_type type)
252 {
253         uint8_t i, dev_count = 0;
254
255         for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
256                 if (rte_cryptodev_globals->devs[i].dev_type == type &&
257                         rte_cryptodev_globals->devs[i].attached ==
258                                         RTE_CRYPTODEV_ATTACHED)
259                         dev_count++;
260
261         return dev_count;
262 }
263
264 int
265 rte_cryptodev_socket_id(uint8_t dev_id)
266 {
267         struct rte_cryptodev *dev;
268
269         if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
270                 return -1;
271
272         dev = rte_cryptodev_pmd_get_dev(dev_id);
273
274         return dev->data->socket_id;
275 }
276
277 static inline int
278 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
279                 int socket_id)
280 {
281         char mz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
282         const struct rte_memzone *mz;
283         int n;
284
285         /* generate memzone name */
286         n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
287         if (n >= (int)sizeof(mz_name))
288                 return -EINVAL;
289
290         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
291                 mz = rte_memzone_reserve(mz_name,
292                                 sizeof(struct rte_cryptodev_data),
293                                 socket_id, 0);
294         } else
295                 mz = rte_memzone_lookup(mz_name);
296
297         if (mz == NULL)
298                 return -ENOMEM;
299
300         *data = mz->addr;
301         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
302                 memset(*data, 0, sizeof(struct rte_cryptodev_data));
303
304         return 0;
305 }
306
307 static uint8_t
308 rte_cryptodev_find_free_device_index(void)
309 {
310         uint8_t dev_id;
311
312         for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
313                 if (rte_crypto_devices[dev_id].attached ==
314                                 RTE_CRYPTODEV_DETACHED)
315                         return dev_id;
316         }
317         return RTE_CRYPTO_MAX_DEVS;
318 }
319
320 struct rte_cryptodev *
321 rte_cryptodev_pmd_allocate(const char *name, enum pmd_type type, int socket_id)
322 {
323         struct rte_cryptodev *cryptodev;
324         uint8_t dev_id;
325
326         if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
327                 CDEV_LOG_ERR("Crypto device with name %s already "
328                                 "allocated!", name);
329                 return NULL;
330         }
331
332         dev_id = rte_cryptodev_find_free_device_index();
333         if (dev_id == RTE_CRYPTO_MAX_DEVS) {
334                 CDEV_LOG_ERR("Reached maximum number of crypto devices");
335                 return NULL;
336         }
337
338         cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
339
340         if (cryptodev->data == NULL) {
341                 struct rte_cryptodev_data *cryptodev_data =
342                                 cryptodev_globals.data[dev_id];
343
344                 int retval = rte_cryptodev_data_alloc(dev_id, &cryptodev_data,
345                                 socket_id);
346
347                 if (retval < 0 || cryptodev_data == NULL)
348                         return NULL;
349
350                 cryptodev->data = cryptodev_data;
351
352                 snprintf(cryptodev->data->name, RTE_CRYPTODEV_NAME_MAX_LEN,
353                                 "%s", name);
354
355                 cryptodev->data->dev_id = dev_id;
356                 cryptodev->data->socket_id = socket_id;
357                 cryptodev->data->dev_started = 0;
358
359                 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
360                 cryptodev->pmd_type = type;
361
362                 cryptodev_globals.nb_devs++;
363         }
364
365         return cryptodev;
366 }
367
368 static inline int
369 rte_cryptodev_create_unique_device_name(char *name, size_t size,
370                 struct rte_pci_device *pci_dev)
371 {
372         int ret;
373
374         if ((name == NULL) || (pci_dev == NULL))
375                 return -EINVAL;
376
377         ret = snprintf(name, size, "%d:%d.%d",
378                         pci_dev->addr.bus, pci_dev->addr.devid,
379                         pci_dev->addr.function);
380         if (ret < 0)
381                 return ret;
382         return 0;
383 }
384
385 int
386 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
387 {
388         int ret;
389
390         if (cryptodev == NULL)
391                 return -EINVAL;
392
393         ret = rte_cryptodev_close(cryptodev->data->dev_id);
394         if (ret < 0)
395                 return ret;
396
397         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
398         cryptodev_globals.nb_devs--;
399         return 0;
400 }
401
402 struct rte_cryptodev *
403 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
404                 int socket_id)
405 {
406         struct rte_cryptodev *cryptodev;
407
408         /* allocate device structure */
409         cryptodev = rte_cryptodev_pmd_allocate(name, PMD_VDEV, socket_id);
410         if (cryptodev == NULL)
411                 return NULL;
412
413         /* allocate private device structure */
414         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
415                 cryptodev->data->dev_private =
416                                 rte_zmalloc_socket("cryptodev device private",
417                                                 dev_private_size,
418                                                 RTE_CACHE_LINE_SIZE,
419                                                 socket_id);
420
421                 if (cryptodev->data->dev_private == NULL)
422                         rte_panic("Cannot allocate memzone for private device"
423                                         " data");
424         }
425
426         /* initialise user call-back tail queue */
427         TAILQ_INIT(&(cryptodev->link_intr_cbs));
428
429         return cryptodev;
430 }
431
432 static int
433 rte_cryptodev_init(struct rte_pci_driver *pci_drv,
434                 struct rte_pci_device *pci_dev)
435 {
436         struct rte_cryptodev_driver *cryptodrv;
437         struct rte_cryptodev *cryptodev;
438
439         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
440
441         int retval;
442
443         cryptodrv = (struct rte_cryptodev_driver *)pci_drv;
444         if (cryptodrv == NULL)
445                 return -ENODEV;
446
447         /* Create unique Crypto device name using PCI address */
448         rte_cryptodev_create_unique_device_name(cryptodev_name,
449                         sizeof(cryptodev_name), pci_dev);
450
451         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, PMD_PDEV,
452                         rte_socket_id());
453         if (cryptodev == NULL)
454                 return -ENOMEM;
455
456         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
457                 cryptodev->data->dev_private =
458                                 rte_zmalloc_socket(
459                                                 "cryptodev private structure",
460                                                 cryptodrv->dev_private_size,
461                                                 RTE_CACHE_LINE_SIZE,
462                                                 rte_socket_id());
463
464                 if (cryptodev->data->dev_private == NULL)
465                         rte_panic("Cannot allocate memzone for private "
466                                         "device data");
467         }
468
469         cryptodev->pci_dev = pci_dev;
470         cryptodev->driver = cryptodrv;
471
472         /* init user callbacks */
473         TAILQ_INIT(&(cryptodev->link_intr_cbs));
474
475         /* Invoke PMD device initialization function */
476         retval = (*cryptodrv->cryptodev_init)(cryptodrv, cryptodev);
477         if (retval == 0)
478                 return 0;
479
480         CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
481                         " failed", pci_drv->name,
482                         (unsigned) pci_dev->id.vendor_id,
483                         (unsigned) pci_dev->id.device_id);
484
485         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
486                 rte_free(cryptodev->data->dev_private);
487
488         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
489         cryptodev_globals.nb_devs--;
490
491         return -ENXIO;
492 }
493
494 static int
495 rte_cryptodev_uninit(struct rte_pci_device *pci_dev)
496 {
497         const struct rte_cryptodev_driver *cryptodrv;
498         struct rte_cryptodev *cryptodev;
499         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
500         int ret;
501
502         if (pci_dev == NULL)
503                 return -EINVAL;
504
505         /* Create unique device name using PCI address */
506         rte_cryptodev_create_unique_device_name(cryptodev_name,
507                         sizeof(cryptodev_name), pci_dev);
508
509         cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
510         if (cryptodev == NULL)
511                 return -ENODEV;
512
513         cryptodrv = (const struct rte_cryptodev_driver *)pci_dev->driver;
514         if (cryptodrv == NULL)
515                 return -ENODEV;
516
517         /* Invoke PMD device uninit function */
518         if (*cryptodrv->cryptodev_uninit) {
519                 ret = (*cryptodrv->cryptodev_uninit)(cryptodrv, cryptodev);
520                 if (ret)
521                         return ret;
522         }
523
524         /* free crypto device */
525         rte_cryptodev_pmd_release_device(cryptodev);
526
527         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
528                 rte_free(cryptodev->data->dev_private);
529
530         cryptodev->pci_dev = NULL;
531         cryptodev->driver = NULL;
532         cryptodev->data = NULL;
533
534         return 0;
535 }
536
537 int
538 rte_cryptodev_pmd_driver_register(struct rte_cryptodev_driver *cryptodrv,
539                 enum pmd_type type)
540 {
541         /* Call crypto device initialization directly if device is virtual */
542         if (type == PMD_VDEV)
543                 return rte_cryptodev_init((struct rte_pci_driver *)cryptodrv,
544                                 NULL);
545
546         /*
547          * Register PCI driver for physical device intialisation during
548          * PCI probing
549          */
550         cryptodrv->pci_drv.probe = rte_cryptodev_init;
551         cryptodrv->pci_drv.remove = rte_cryptodev_uninit;
552
553         rte_eal_pci_register(&cryptodrv->pci_drv);
554
555         return 0;
556 }
557
558
559 uint16_t
560 rte_cryptodev_queue_pair_count(uint8_t dev_id)
561 {
562         struct rte_cryptodev *dev;
563
564         dev = &rte_crypto_devices[dev_id];
565         return dev->data->nb_queue_pairs;
566 }
567
568 static int
569 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
570                 int socket_id)
571 {
572         struct rte_cryptodev_info dev_info;
573         void **qp;
574         unsigned i;
575
576         if ((dev == NULL) || (nb_qpairs < 1)) {
577                 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
578                                                         dev, nb_qpairs);
579                 return -EINVAL;
580         }
581
582         CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
583                         nb_qpairs, dev->data->dev_id);
584
585         memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
586
587         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
588         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
589
590         if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
591                 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
592                                 nb_qpairs, dev->data->dev_id);
593             return -EINVAL;
594         }
595
596         if (dev->data->queue_pairs == NULL) { /* first time configuration */
597                 dev->data->queue_pairs = rte_zmalloc_socket(
598                                 "cryptodev->queue_pairs",
599                                 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
600                                 RTE_CACHE_LINE_SIZE, socket_id);
601
602                 if (dev->data->queue_pairs == NULL) {
603                         dev->data->nb_queue_pairs = 0;
604                         CDEV_LOG_ERR("failed to get memory for qp meta data, "
605                                                         "nb_queues %u",
606                                                         nb_qpairs);
607                         return -(ENOMEM);
608                 }
609         } else { /* re-configure */
610                 int ret;
611                 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
612
613                 qp = dev->data->queue_pairs;
614
615                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
616                                 -ENOTSUP);
617
618                 for (i = nb_qpairs; i < old_nb_queues; i++) {
619                         ret = (*dev->dev_ops->queue_pair_release)(dev, i);
620                         if (ret < 0)
621                                 return ret;
622                 }
623
624                 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
625                                 RTE_CACHE_LINE_SIZE);
626                 if (qp == NULL) {
627                         CDEV_LOG_ERR("failed to realloc qp meta data,"
628                                                 " nb_queues %u", nb_qpairs);
629                         return -(ENOMEM);
630                 }
631
632                 if (nb_qpairs > old_nb_queues) {
633                         uint16_t new_qs = nb_qpairs - old_nb_queues;
634
635                         memset(qp + old_nb_queues, 0,
636                                 sizeof(qp[0]) * new_qs);
637                 }
638
639                 dev->data->queue_pairs = qp;
640
641         }
642         dev->data->nb_queue_pairs = nb_qpairs;
643         return 0;
644 }
645
646 int
647 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
648 {
649         struct rte_cryptodev *dev;
650
651         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
652                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
653                 return -EINVAL;
654         }
655
656         dev = &rte_crypto_devices[dev_id];
657         if (queue_pair_id >= dev->data->nb_queue_pairs) {
658                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
659                 return -EINVAL;
660         }
661
662         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
663
664         return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
665
666 }
667
668 int
669 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
670 {
671         struct rte_cryptodev *dev;
672
673         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
674                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
675                 return -EINVAL;
676         }
677
678         dev = &rte_crypto_devices[dev_id];
679         if (queue_pair_id >= dev->data->nb_queue_pairs) {
680                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
681                 return -EINVAL;
682         }
683
684         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
685
686         return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
687
688 }
689
690 static int
691 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
692                 unsigned nb_objs, unsigned obj_cache_size, int socket_id);
693
694 int
695 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
696 {
697         struct rte_cryptodev *dev;
698         int diag;
699
700         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
701                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
702                 return -EINVAL;
703         }
704
705         dev = &rte_crypto_devices[dev_id];
706
707         if (dev->data->dev_started) {
708                 CDEV_LOG_ERR(
709                     "device %d must be stopped to allow configuration", dev_id);
710                 return -EBUSY;
711         }
712
713         /* Setup new number of queue pairs and reconfigure device. */
714         diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
715                         config->socket_id);
716         if (diag != 0) {
717                 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
718                                 dev_id, diag);
719                 return diag;
720         }
721
722         /* Setup Session mempool for device */
723         return rte_cryptodev_sym_session_pool_create(dev,
724                         config->session_mp.nb_objs,
725                         config->session_mp.cache_size,
726                         config->socket_id);
727 }
728
729
730 int
731 rte_cryptodev_start(uint8_t dev_id)
732 {
733         struct rte_cryptodev *dev;
734         int diag;
735
736         CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
737
738         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
739                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
740                 return -EINVAL;
741         }
742
743         dev = &rte_crypto_devices[dev_id];
744
745         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
746
747         if (dev->data->dev_started != 0) {
748                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
749                         dev_id);
750                 return 0;
751         }
752
753         diag = (*dev->dev_ops->dev_start)(dev);
754         if (diag == 0)
755                 dev->data->dev_started = 1;
756         else
757                 return diag;
758
759         return 0;
760 }
761
762 void
763 rte_cryptodev_stop(uint8_t dev_id)
764 {
765         struct rte_cryptodev *dev;
766
767         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
768                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
769                 return;
770         }
771
772         dev = &rte_crypto_devices[dev_id];
773
774         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
775
776         if (dev->data->dev_started == 0) {
777                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
778                         dev_id);
779                 return;
780         }
781
782         dev->data->dev_started = 0;
783         (*dev->dev_ops->dev_stop)(dev);
784 }
785
786 int
787 rte_cryptodev_close(uint8_t dev_id)
788 {
789         struct rte_cryptodev *dev;
790         int retval;
791
792         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
793                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
794                 return -1;
795         }
796
797         dev = &rte_crypto_devices[dev_id];
798
799         /* Device must be stopped before it can be closed */
800         if (dev->data->dev_started == 1) {
801                 CDEV_LOG_ERR("Device %u must be stopped before closing",
802                                 dev_id);
803                 return -EBUSY;
804         }
805
806         /* We can't close the device if there are outstanding sessions in use */
807         if (dev->data->session_pool != NULL) {
808                 if (!rte_mempool_full(dev->data->session_pool)) {
809                         CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
810                                         "has sessions still in use, free "
811                                         "all sessions before calling close",
812                                         (unsigned)dev_id);
813                         return -EBUSY;
814                 }
815         }
816
817         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
818         retval = (*dev->dev_ops->dev_close)(dev);
819
820         if (retval < 0)
821                 return retval;
822
823         return 0;
824 }
825
826 int
827 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
828                 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
829 {
830         struct rte_cryptodev *dev;
831
832         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
833                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
834                 return -EINVAL;
835         }
836
837         dev = &rte_crypto_devices[dev_id];
838         if (queue_pair_id >= dev->data->nb_queue_pairs) {
839                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
840                 return -EINVAL;
841         }
842
843         if (dev->data->dev_started) {
844                 CDEV_LOG_ERR(
845                     "device %d must be stopped to allow configuration", dev_id);
846                 return -EBUSY;
847         }
848
849         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
850
851         return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
852                         socket_id);
853 }
854
855
856 int
857 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
858 {
859         struct rte_cryptodev *dev;
860
861         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
862                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
863                 return -ENODEV;
864         }
865
866         if (stats == NULL) {
867                 CDEV_LOG_ERR("Invalid stats ptr");
868                 return -EINVAL;
869         }
870
871         dev = &rte_crypto_devices[dev_id];
872         memset(stats, 0, sizeof(*stats));
873
874         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
875         (*dev->dev_ops->stats_get)(dev, stats);
876         return 0;
877 }
878
879 void
880 rte_cryptodev_stats_reset(uint8_t dev_id)
881 {
882         struct rte_cryptodev *dev;
883
884         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
885                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
886                 return;
887         }
888
889         dev = &rte_crypto_devices[dev_id];
890
891         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
892         (*dev->dev_ops->stats_reset)(dev);
893 }
894
895
896 void
897 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
898 {
899         struct rte_cryptodev *dev;
900
901         if (dev_id >= cryptodev_globals.nb_devs) {
902                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
903                 return;
904         }
905
906         dev = &rte_crypto_devices[dev_id];
907
908         memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
909
910         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
911         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
912
913         dev_info->pci_dev = dev->pci_dev;
914         if (dev->driver)
915                 dev_info->driver_name = dev->driver->pci_drv.name;
916 }
917
918
919 int
920 rte_cryptodev_callback_register(uint8_t dev_id,
921                         enum rte_cryptodev_event_type event,
922                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
923 {
924         struct rte_cryptodev *dev;
925         struct rte_cryptodev_callback *user_cb;
926
927         if (!cb_fn)
928                 return -EINVAL;
929
930         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
931                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
932                 return -EINVAL;
933         }
934
935         dev = &rte_crypto_devices[dev_id];
936         rte_spinlock_lock(&rte_cryptodev_cb_lock);
937
938         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
939                 if (user_cb->cb_fn == cb_fn &&
940                         user_cb->cb_arg == cb_arg &&
941                         user_cb->event == event) {
942                         break;
943                 }
944         }
945
946         /* create a new callback. */
947         if (user_cb == NULL) {
948                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
949                                 sizeof(struct rte_cryptodev_callback), 0);
950                 if (user_cb != NULL) {
951                         user_cb->cb_fn = cb_fn;
952                         user_cb->cb_arg = cb_arg;
953                         user_cb->event = event;
954                         TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
955                 }
956         }
957
958         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
959         return (user_cb == NULL) ? -ENOMEM : 0;
960 }
961
962 int
963 rte_cryptodev_callback_unregister(uint8_t dev_id,
964                         enum rte_cryptodev_event_type event,
965                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
966 {
967         int ret;
968         struct rte_cryptodev *dev;
969         struct rte_cryptodev_callback *cb, *next;
970
971         if (!cb_fn)
972                 return -EINVAL;
973
974         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
975                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
976                 return -EINVAL;
977         }
978
979         dev = &rte_crypto_devices[dev_id];
980         rte_spinlock_lock(&rte_cryptodev_cb_lock);
981
982         ret = 0;
983         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
984
985                 next = TAILQ_NEXT(cb, next);
986
987                 if (cb->cb_fn != cb_fn || cb->event != event ||
988                                 (cb->cb_arg != (void *)-1 &&
989                                 cb->cb_arg != cb_arg))
990                         continue;
991
992                 /*
993                  * if this callback is not executing right now,
994                  * then remove it.
995                  */
996                 if (cb->active == 0) {
997                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
998                         rte_free(cb);
999                 } else {
1000                         ret = -EAGAIN;
1001                 }
1002         }
1003
1004         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1005         return ret;
1006 }
1007
1008 void
1009 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1010         enum rte_cryptodev_event_type event)
1011 {
1012         struct rte_cryptodev_callback *cb_lst;
1013         struct rte_cryptodev_callback dev_cb;
1014
1015         rte_spinlock_lock(&rte_cryptodev_cb_lock);
1016         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1017                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1018                         continue;
1019                 dev_cb = *cb_lst;
1020                 cb_lst->active = 1;
1021                 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1022                 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1023                                                 dev_cb.cb_arg);
1024                 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1025                 cb_lst->active = 0;
1026         }
1027         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1028 }
1029
1030
1031 static void
1032 rte_cryptodev_sym_session_init(struct rte_mempool *mp,
1033                 void *opaque_arg,
1034                 void *_sess,
1035                 __rte_unused unsigned i)
1036 {
1037         struct rte_cryptodev_sym_session *sess = _sess;
1038         struct rte_cryptodev *dev = opaque_arg;
1039
1040         memset(sess, 0, mp->elt_size);
1041
1042         sess->dev_id = dev->data->dev_id;
1043         sess->dev_type = dev->dev_type;
1044         sess->mp = mp;
1045
1046         if (dev->dev_ops->session_initialize)
1047                 (*dev->dev_ops->session_initialize)(mp, sess);
1048 }
1049
1050 static int
1051 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
1052                 unsigned nb_objs, unsigned obj_cache_size, int socket_id)
1053 {
1054         char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1055         unsigned priv_sess_size;
1056
1057         unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
1058                         dev->data->dev_id);
1059         if (n > sizeof(mp_name)) {
1060                 CDEV_LOG_ERR("Unable to create unique name for session mempool");
1061                 return -ENOMEM;
1062         }
1063
1064         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_get_size, -ENOTSUP);
1065         priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
1066         if (priv_sess_size == 0) {
1067                 CDEV_LOG_ERR("%s returned and invalid private session size ",
1068                                                 dev->data->name);
1069                 return -ENOMEM;
1070         }
1071
1072         unsigned elt_size = sizeof(struct rte_cryptodev_sym_session) +
1073                         priv_sess_size;
1074
1075         dev->data->session_pool = rte_mempool_lookup(mp_name);
1076         if (dev->data->session_pool != NULL) {
1077                 if ((dev->data->session_pool->elt_size != elt_size) ||
1078                                 (dev->data->session_pool->cache_size <
1079                                 obj_cache_size) ||
1080                                 (dev->data->session_pool->size < nb_objs)) {
1081
1082                         CDEV_LOG_ERR("%s mempool already exists with different"
1083                                         " initialization parameters", mp_name);
1084                         dev->data->session_pool = NULL;
1085                         return -ENOMEM;
1086                 }
1087         } else {
1088                 dev->data->session_pool = rte_mempool_create(
1089                                 mp_name, /* mempool name */
1090                                 nb_objs, /* number of elements*/
1091                                 elt_size, /* element size*/
1092                                 obj_cache_size, /* Cache size*/
1093                                 0, /* private data size */
1094                                 NULL, /* obj initialization constructor */
1095                                 NULL, /* obj initialization constructor arg */
1096                                 rte_cryptodev_sym_session_init,
1097                                 /**< obj constructor*/
1098                                 dev, /* obj constructor arg */
1099                                 socket_id, /* socket id */
1100                                 0); /* flags */
1101
1102                 if (dev->data->session_pool == NULL) {
1103                         CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
1104                         return -ENOMEM;
1105                 }
1106         }
1107
1108         CDEV_LOG_DEBUG("%s mempool created!", mp_name);
1109         return 0;
1110 }
1111
1112 struct rte_cryptodev_sym_session *
1113 rte_cryptodev_sym_session_create(uint8_t dev_id,
1114                 struct rte_crypto_sym_xform *xform)
1115 {
1116         struct rte_cryptodev *dev;
1117         struct rte_cryptodev_sym_session *sess;
1118         void *_sess;
1119
1120         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1121                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1122                 return NULL;
1123         }
1124
1125         dev = &rte_crypto_devices[dev_id];
1126
1127         /* Allocate a session structure from the session pool */
1128         if (rte_mempool_get(dev->data->session_pool, &_sess)) {
1129                 CDEV_LOG_ERR("Couldn't get object from session mempool");
1130                 return NULL;
1131         }
1132
1133         sess = (struct rte_cryptodev_sym_session *)_sess;
1134
1135         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1136         if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1137                         NULL) {
1138                 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1139                                 dev_id);
1140
1141                 /* Return session to mempool */
1142                 rte_mempool_put(sess->mp, _sess);
1143                 return NULL;
1144         }
1145
1146         return sess;
1147 }
1148
1149 struct rte_cryptodev_sym_session *
1150 rte_cryptodev_sym_session_free(uint8_t dev_id,
1151                 struct rte_cryptodev_sym_session *sess)
1152 {
1153         struct rte_cryptodev *dev;
1154
1155         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1156                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1157                 return sess;
1158         }
1159
1160         dev = &rte_crypto_devices[dev_id];
1161
1162         /* Check the session belongs to this device type */
1163         if (sess->dev_type != dev->dev_type)
1164                 return sess;
1165
1166         /* Let device implementation clear session material */
1167         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_clear, sess);
1168         dev->dev_ops->session_clear(dev, (void *)sess->_private);
1169
1170         /* Return session to mempool */
1171         rte_mempool_put(sess->mp, (void *)sess);
1172
1173         return NULL;
1174 }
1175
1176 /** Initialise rte_crypto_op mempool element */
1177 static void
1178 rte_crypto_op_init(struct rte_mempool *mempool,
1179                 void *opaque_arg,
1180                 void *_op_data,
1181                 __rte_unused unsigned i)
1182 {
1183         struct rte_crypto_op *op = _op_data;
1184         enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1185
1186         memset(_op_data, 0, mempool->elt_size);
1187
1188         __rte_crypto_op_reset(op, type);
1189
1190         op->phys_addr = rte_mem_virt2phy(_op_data);
1191         op->mempool = mempool;
1192 }
1193
1194
1195 struct rte_mempool *
1196 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
1197                 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
1198                 int socket_id)
1199 {
1200         struct rte_crypto_op_pool_private *priv;
1201
1202         unsigned elt_size = sizeof(struct rte_crypto_op) +
1203                         sizeof(struct rte_crypto_sym_op) +
1204                         priv_size;
1205
1206         /* lookup mempool in case already allocated */
1207         struct rte_mempool *mp = rte_mempool_lookup(name);
1208
1209         if (mp != NULL) {
1210                 priv = (struct rte_crypto_op_pool_private *)
1211                                 rte_mempool_get_priv(mp);
1212
1213                 if (mp->elt_size != elt_size ||
1214                                 mp->cache_size < cache_size ||
1215                                 mp->size < nb_elts ||
1216                                 priv->priv_size <  priv_size) {
1217                         mp = NULL;
1218                         CDEV_LOG_ERR("Mempool %s already exists but with "
1219                                         "incompatible parameters", name);
1220                         return NULL;
1221                 }
1222                 return mp;
1223         }
1224
1225         mp = rte_mempool_create(
1226                         name,
1227                         nb_elts,
1228                         elt_size,
1229                         cache_size,
1230                         sizeof(struct rte_crypto_op_pool_private),
1231                         NULL,
1232                         NULL,
1233                         rte_crypto_op_init,
1234                         &type,
1235                         socket_id,
1236                         0);
1237
1238         if (mp == NULL) {
1239                 CDEV_LOG_ERR("Failed to create mempool %s", name);
1240                 return NULL;
1241         }
1242
1243         priv = (struct rte_crypto_op_pool_private *)
1244                         rte_mempool_get_priv(mp);
1245
1246         priv->priv_size = priv_size;
1247         priv->type = type;
1248
1249         return mp;
1250 }