caa9db8720ac3fb2fff3dadc058d4f365772d7cd
[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, 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
361                 cryptodev_globals.nb_devs++;
362         }
363
364         return cryptodev;
365 }
366
367 static inline int
368 rte_cryptodev_create_unique_device_name(char *name, size_t size,
369                 struct rte_pci_device *pci_dev)
370 {
371         int ret;
372
373         if ((name == NULL) || (pci_dev == NULL))
374                 return -EINVAL;
375
376         ret = snprintf(name, size, "%d:%d.%d",
377                         pci_dev->addr.bus, pci_dev->addr.devid,
378                         pci_dev->addr.function);
379         if (ret < 0)
380                 return ret;
381         return 0;
382 }
383
384 int
385 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
386 {
387         int ret;
388
389         if (cryptodev == NULL)
390                 return -EINVAL;
391
392         ret = rte_cryptodev_close(cryptodev->data->dev_id);
393         if (ret < 0)
394                 return ret;
395
396         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
397         cryptodev_globals.nb_devs--;
398         return 0;
399 }
400
401 struct rte_cryptodev *
402 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
403                 int socket_id)
404 {
405         struct rte_cryptodev *cryptodev;
406
407         /* allocate device structure */
408         cryptodev = rte_cryptodev_pmd_allocate(name, socket_id);
409         if (cryptodev == NULL)
410                 return NULL;
411
412         /* allocate private device structure */
413         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
414                 cryptodev->data->dev_private =
415                                 rte_zmalloc_socket("cryptodev device private",
416                                                 dev_private_size,
417                                                 RTE_CACHE_LINE_SIZE,
418                                                 socket_id);
419
420                 if (cryptodev->data->dev_private == NULL)
421                         rte_panic("Cannot allocate memzone for private device"
422                                         " data");
423         }
424
425         /* initialise user call-back tail queue */
426         TAILQ_INIT(&(cryptodev->link_intr_cbs));
427
428         return cryptodev;
429 }
430
431 int
432 rte_cryptodev_pci_probe(struct rte_pci_driver *pci_drv,
433                         struct rte_pci_device *pci_dev)
434 {
435         struct rte_cryptodev_driver *cryptodrv;
436         struct rte_cryptodev *cryptodev;
437
438         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
439
440         int retval;
441
442         cryptodrv = (struct rte_cryptodev_driver *)pci_drv;
443         if (cryptodrv == NULL)
444                 return -ENODEV;
445
446         /* Create unique Crypto device name using PCI address */
447         rte_cryptodev_create_unique_device_name(cryptodev_name,
448                         sizeof(cryptodev_name), pci_dev);
449
450         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
451         if (cryptodev == NULL)
452                 return -ENOMEM;
453
454         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
455                 cryptodev->data->dev_private =
456                                 rte_zmalloc_socket(
457                                                 "cryptodev private structure",
458                                                 cryptodrv->dev_private_size,
459                                                 RTE_CACHE_LINE_SIZE,
460                                                 rte_socket_id());
461
462                 if (cryptodev->data->dev_private == NULL)
463                         rte_panic("Cannot allocate memzone for private "
464                                         "device data");
465         }
466
467         cryptodev->pci_dev = pci_dev;
468         cryptodev->driver = cryptodrv;
469
470         /* init user callbacks */
471         TAILQ_INIT(&(cryptodev->link_intr_cbs));
472
473         /* Invoke PMD device initialization function */
474         retval = (*cryptodrv->cryptodev_init)(cryptodrv, cryptodev);
475         if (retval == 0)
476                 return 0;
477
478         CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
479                         " failed", pci_drv->name,
480                         (unsigned) pci_dev->id.vendor_id,
481                         (unsigned) pci_dev->id.device_id);
482
483         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
484                 rte_free(cryptodev->data->dev_private);
485
486         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
487         cryptodev_globals.nb_devs--;
488
489         return -ENXIO;
490 }
491
492 int
493 rte_cryptodev_pci_remove(struct rte_pci_device *pci_dev)
494 {
495         const struct rte_cryptodev_driver *cryptodrv;
496         struct rte_cryptodev *cryptodev;
497         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
498         int ret;
499
500         if (pci_dev == NULL)
501                 return -EINVAL;
502
503         /* Create unique device name using PCI address */
504         rte_cryptodev_create_unique_device_name(cryptodev_name,
505                         sizeof(cryptodev_name), pci_dev);
506
507         cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
508         if (cryptodev == NULL)
509                 return -ENODEV;
510
511         cryptodrv = (const struct rte_cryptodev_driver *)pci_dev->driver;
512         if (cryptodrv == NULL)
513                 return -ENODEV;
514
515         /* Invoke PMD device uninit function */
516         if (*cryptodrv->cryptodev_uninit) {
517                 ret = (*cryptodrv->cryptodev_uninit)(cryptodrv, cryptodev);
518                 if (ret)
519                         return ret;
520         }
521
522         /* free crypto device */
523         rte_cryptodev_pmd_release_device(cryptodev);
524
525         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
526                 rte_free(cryptodev->data->dev_private);
527
528         cryptodev->pci_dev = NULL;
529         cryptodev->driver = NULL;
530         cryptodev->data = NULL;
531
532         return 0;
533 }
534
535 int
536 rte_cryptodev_pmd_driver_register(struct rte_cryptodev_driver *cryptodrv,
537                 enum pmd_type type)
538 {
539         /* Call crypto device initialization directly if device is virtual */
540         if (type == PMD_VDEV)
541                 return rte_cryptodev_pci_probe(
542                                 (struct rte_pci_driver *)cryptodrv,
543                                 NULL);
544
545         /*
546          * Register PCI driver for physical device intialisation during
547          * PCI probing
548          */
549         cryptodrv->pci_drv.probe = rte_cryptodev_pci_probe;
550         cryptodrv->pci_drv.remove = rte_cryptodev_pci_remove;
551
552         rte_eal_pci_register(&cryptodrv->pci_drv);
553
554         return 0;
555 }
556
557
558 uint16_t
559 rte_cryptodev_queue_pair_count(uint8_t dev_id)
560 {
561         struct rte_cryptodev *dev;
562
563         dev = &rte_crypto_devices[dev_id];
564         return dev->data->nb_queue_pairs;
565 }
566
567 static int
568 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
569                 int socket_id)
570 {
571         struct rte_cryptodev_info dev_info;
572         void **qp;
573         unsigned i;
574
575         if ((dev == NULL) || (nb_qpairs < 1)) {
576                 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
577                                                         dev, nb_qpairs);
578                 return -EINVAL;
579         }
580
581         CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
582                         nb_qpairs, dev->data->dev_id);
583
584         memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
585
586         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
587         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
588
589         if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
590                 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
591                                 nb_qpairs, dev->data->dev_id);
592             return -EINVAL;
593         }
594
595         if (dev->data->queue_pairs == NULL) { /* first time configuration */
596                 dev->data->queue_pairs = rte_zmalloc_socket(
597                                 "cryptodev->queue_pairs",
598                                 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
599                                 RTE_CACHE_LINE_SIZE, socket_id);
600
601                 if (dev->data->queue_pairs == NULL) {
602                         dev->data->nb_queue_pairs = 0;
603                         CDEV_LOG_ERR("failed to get memory for qp meta data, "
604                                                         "nb_queues %u",
605                                                         nb_qpairs);
606                         return -(ENOMEM);
607                 }
608         } else { /* re-configure */
609                 int ret;
610                 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
611
612                 qp = dev->data->queue_pairs;
613
614                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
615                                 -ENOTSUP);
616
617                 for (i = nb_qpairs; i < old_nb_queues; i++) {
618                         ret = (*dev->dev_ops->queue_pair_release)(dev, i);
619                         if (ret < 0)
620                                 return ret;
621                 }
622
623                 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
624                                 RTE_CACHE_LINE_SIZE);
625                 if (qp == NULL) {
626                         CDEV_LOG_ERR("failed to realloc qp meta data,"
627                                                 " nb_queues %u", nb_qpairs);
628                         return -(ENOMEM);
629                 }
630
631                 if (nb_qpairs > old_nb_queues) {
632                         uint16_t new_qs = nb_qpairs - old_nb_queues;
633
634                         memset(qp + old_nb_queues, 0,
635                                 sizeof(qp[0]) * new_qs);
636                 }
637
638                 dev->data->queue_pairs = qp;
639
640         }
641         dev->data->nb_queue_pairs = nb_qpairs;
642         return 0;
643 }
644
645 int
646 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
647 {
648         struct rte_cryptodev *dev;
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         if (queue_pair_id >= dev->data->nb_queue_pairs) {
657                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
658                 return -EINVAL;
659         }
660
661         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
662
663         return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
664
665 }
666
667 int
668 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
669 {
670         struct rte_cryptodev *dev;
671
672         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
673                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
674                 return -EINVAL;
675         }
676
677         dev = &rte_crypto_devices[dev_id];
678         if (queue_pair_id >= dev->data->nb_queue_pairs) {
679                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
680                 return -EINVAL;
681         }
682
683         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
684
685         return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
686
687 }
688
689 static int
690 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
691                 unsigned nb_objs, unsigned obj_cache_size, int socket_id);
692
693 int
694 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
695 {
696         struct rte_cryptodev *dev;
697         int diag;
698
699         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
700                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
701                 return -EINVAL;
702         }
703
704         dev = &rte_crypto_devices[dev_id];
705
706         if (dev->data->dev_started) {
707                 CDEV_LOG_ERR(
708                     "device %d must be stopped to allow configuration", dev_id);
709                 return -EBUSY;
710         }
711
712         /* Setup new number of queue pairs and reconfigure device. */
713         diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
714                         config->socket_id);
715         if (diag != 0) {
716                 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
717                                 dev_id, diag);
718                 return diag;
719         }
720
721         /* Setup Session mempool for device */
722         return rte_cryptodev_sym_session_pool_create(dev,
723                         config->session_mp.nb_objs,
724                         config->session_mp.cache_size,
725                         config->socket_id);
726 }
727
728
729 int
730 rte_cryptodev_start(uint8_t dev_id)
731 {
732         struct rte_cryptodev *dev;
733         int diag;
734
735         CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
736
737         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
738                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
739                 return -EINVAL;
740         }
741
742         dev = &rte_crypto_devices[dev_id];
743
744         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
745
746         if (dev->data->dev_started != 0) {
747                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
748                         dev_id);
749                 return 0;
750         }
751
752         diag = (*dev->dev_ops->dev_start)(dev);
753         if (diag == 0)
754                 dev->data->dev_started = 1;
755         else
756                 return diag;
757
758         return 0;
759 }
760
761 void
762 rte_cryptodev_stop(uint8_t dev_id)
763 {
764         struct rte_cryptodev *dev;
765
766         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
767                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
768                 return;
769         }
770
771         dev = &rte_crypto_devices[dev_id];
772
773         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
774
775         if (dev->data->dev_started == 0) {
776                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
777                         dev_id);
778                 return;
779         }
780
781         dev->data->dev_started = 0;
782         (*dev->dev_ops->dev_stop)(dev);
783 }
784
785 int
786 rte_cryptodev_close(uint8_t dev_id)
787 {
788         struct rte_cryptodev *dev;
789         int retval;
790
791         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
792                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
793                 return -1;
794         }
795
796         dev = &rte_crypto_devices[dev_id];
797
798         /* Device must be stopped before it can be closed */
799         if (dev->data->dev_started == 1) {
800                 CDEV_LOG_ERR("Device %u must be stopped before closing",
801                                 dev_id);
802                 return -EBUSY;
803         }
804
805         /* We can't close the device if there are outstanding sessions in use */
806         if (dev->data->session_pool != NULL) {
807                 if (!rte_mempool_full(dev->data->session_pool)) {
808                         CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
809                                         "has sessions still in use, free "
810                                         "all sessions before calling close",
811                                         (unsigned)dev_id);
812                         return -EBUSY;
813                 }
814         }
815
816         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
817         retval = (*dev->dev_ops->dev_close)(dev);
818
819         if (retval < 0)
820                 return retval;
821
822         return 0;
823 }
824
825 int
826 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
827                 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
828 {
829         struct rte_cryptodev *dev;
830
831         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
832                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
833                 return -EINVAL;
834         }
835
836         dev = &rte_crypto_devices[dev_id];
837         if (queue_pair_id >= dev->data->nb_queue_pairs) {
838                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
839                 return -EINVAL;
840         }
841
842         if (dev->data->dev_started) {
843                 CDEV_LOG_ERR(
844                     "device %d must be stopped to allow configuration", dev_id);
845                 return -EBUSY;
846         }
847
848         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
849
850         return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
851                         socket_id);
852 }
853
854
855 int
856 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
857 {
858         struct rte_cryptodev *dev;
859
860         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
861                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
862                 return -ENODEV;
863         }
864
865         if (stats == NULL) {
866                 CDEV_LOG_ERR("Invalid stats ptr");
867                 return -EINVAL;
868         }
869
870         dev = &rte_crypto_devices[dev_id];
871         memset(stats, 0, sizeof(*stats));
872
873         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
874         (*dev->dev_ops->stats_get)(dev, stats);
875         return 0;
876 }
877
878 void
879 rte_cryptodev_stats_reset(uint8_t dev_id)
880 {
881         struct rte_cryptodev *dev;
882
883         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
884                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
885                 return;
886         }
887
888         dev = &rte_crypto_devices[dev_id];
889
890         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
891         (*dev->dev_ops->stats_reset)(dev);
892 }
893
894
895 void
896 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
897 {
898         struct rte_cryptodev *dev;
899
900         if (dev_id >= cryptodev_globals.nb_devs) {
901                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
902                 return;
903         }
904
905         dev = &rte_crypto_devices[dev_id];
906
907         memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
908
909         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
910         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
911
912         dev_info->pci_dev = dev->pci_dev;
913         if (dev->driver)
914                 dev_info->driver_name = dev->driver->pci_drv.name;
915 }
916
917
918 int
919 rte_cryptodev_callback_register(uint8_t dev_id,
920                         enum rte_cryptodev_event_type event,
921                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
922 {
923         struct rte_cryptodev *dev;
924         struct rte_cryptodev_callback *user_cb;
925
926         if (!cb_fn)
927                 return -EINVAL;
928
929         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
930                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
931                 return -EINVAL;
932         }
933
934         dev = &rte_crypto_devices[dev_id];
935         rte_spinlock_lock(&rte_cryptodev_cb_lock);
936
937         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
938                 if (user_cb->cb_fn == cb_fn &&
939                         user_cb->cb_arg == cb_arg &&
940                         user_cb->event == event) {
941                         break;
942                 }
943         }
944
945         /* create a new callback. */
946         if (user_cb == NULL) {
947                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
948                                 sizeof(struct rte_cryptodev_callback), 0);
949                 if (user_cb != NULL) {
950                         user_cb->cb_fn = cb_fn;
951                         user_cb->cb_arg = cb_arg;
952                         user_cb->event = event;
953                         TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
954                 }
955         }
956
957         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
958         return (user_cb == NULL) ? -ENOMEM : 0;
959 }
960
961 int
962 rte_cryptodev_callback_unregister(uint8_t dev_id,
963                         enum rte_cryptodev_event_type event,
964                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
965 {
966         int ret;
967         struct rte_cryptodev *dev;
968         struct rte_cryptodev_callback *cb, *next;
969
970         if (!cb_fn)
971                 return -EINVAL;
972
973         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
974                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
975                 return -EINVAL;
976         }
977
978         dev = &rte_crypto_devices[dev_id];
979         rte_spinlock_lock(&rte_cryptodev_cb_lock);
980
981         ret = 0;
982         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
983
984                 next = TAILQ_NEXT(cb, next);
985
986                 if (cb->cb_fn != cb_fn || cb->event != event ||
987                                 (cb->cb_arg != (void *)-1 &&
988                                 cb->cb_arg != cb_arg))
989                         continue;
990
991                 /*
992                  * if this callback is not executing right now,
993                  * then remove it.
994                  */
995                 if (cb->active == 0) {
996                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
997                         rte_free(cb);
998                 } else {
999                         ret = -EAGAIN;
1000                 }
1001         }
1002
1003         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1004         return ret;
1005 }
1006
1007 void
1008 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
1009         enum rte_cryptodev_event_type event)
1010 {
1011         struct rte_cryptodev_callback *cb_lst;
1012         struct rte_cryptodev_callback dev_cb;
1013
1014         rte_spinlock_lock(&rte_cryptodev_cb_lock);
1015         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
1016                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1017                         continue;
1018                 dev_cb = *cb_lst;
1019                 cb_lst->active = 1;
1020                 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1021                 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
1022                                                 dev_cb.cb_arg);
1023                 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1024                 cb_lst->active = 0;
1025         }
1026         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1027 }
1028
1029
1030 static void
1031 rte_cryptodev_sym_session_init(struct rte_mempool *mp,
1032                 void *opaque_arg,
1033                 void *_sess,
1034                 __rte_unused unsigned i)
1035 {
1036         struct rte_cryptodev_sym_session *sess = _sess;
1037         struct rte_cryptodev *dev = opaque_arg;
1038
1039         memset(sess, 0, mp->elt_size);
1040
1041         sess->dev_id = dev->data->dev_id;
1042         sess->dev_type = dev->dev_type;
1043         sess->mp = mp;
1044
1045         if (dev->dev_ops->session_initialize)
1046                 (*dev->dev_ops->session_initialize)(mp, sess);
1047 }
1048
1049 static int
1050 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
1051                 unsigned nb_objs, unsigned obj_cache_size, int socket_id)
1052 {
1053         char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1054         unsigned priv_sess_size;
1055
1056         unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
1057                         dev->data->dev_id);
1058         if (n > sizeof(mp_name)) {
1059                 CDEV_LOG_ERR("Unable to create unique name for session mempool");
1060                 return -ENOMEM;
1061         }
1062
1063         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_get_size, -ENOTSUP);
1064         priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
1065         if (priv_sess_size == 0) {
1066                 CDEV_LOG_ERR("%s returned and invalid private session size ",
1067                                                 dev->data->name);
1068                 return -ENOMEM;
1069         }
1070
1071         unsigned elt_size = sizeof(struct rte_cryptodev_sym_session) +
1072                         priv_sess_size;
1073
1074         dev->data->session_pool = rte_mempool_lookup(mp_name);
1075         if (dev->data->session_pool != NULL) {
1076                 if ((dev->data->session_pool->elt_size != elt_size) ||
1077                                 (dev->data->session_pool->cache_size <
1078                                 obj_cache_size) ||
1079                                 (dev->data->session_pool->size < nb_objs)) {
1080
1081                         CDEV_LOG_ERR("%s mempool already exists with different"
1082                                         " initialization parameters", mp_name);
1083                         dev->data->session_pool = NULL;
1084                         return -ENOMEM;
1085                 }
1086         } else {
1087                 dev->data->session_pool = rte_mempool_create(
1088                                 mp_name, /* mempool name */
1089                                 nb_objs, /* number of elements*/
1090                                 elt_size, /* element size*/
1091                                 obj_cache_size, /* Cache size*/
1092                                 0, /* private data size */
1093                                 NULL, /* obj initialization constructor */
1094                                 NULL, /* obj initialization constructor arg */
1095                                 rte_cryptodev_sym_session_init,
1096                                 /**< obj constructor*/
1097                                 dev, /* obj constructor arg */
1098                                 socket_id, /* socket id */
1099                                 0); /* flags */
1100
1101                 if (dev->data->session_pool == NULL) {
1102                         CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
1103                         return -ENOMEM;
1104                 }
1105         }
1106
1107         CDEV_LOG_DEBUG("%s mempool created!", mp_name);
1108         return 0;
1109 }
1110
1111 struct rte_cryptodev_sym_session *
1112 rte_cryptodev_sym_session_create(uint8_t dev_id,
1113                 struct rte_crypto_sym_xform *xform)
1114 {
1115         struct rte_cryptodev *dev;
1116         struct rte_cryptodev_sym_session *sess;
1117         void *_sess;
1118
1119         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1120                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1121                 return NULL;
1122         }
1123
1124         dev = &rte_crypto_devices[dev_id];
1125
1126         /* Allocate a session structure from the session pool */
1127         if (rte_mempool_get(dev->data->session_pool, &_sess)) {
1128                 CDEV_LOG_ERR("Couldn't get object from session mempool");
1129                 return NULL;
1130         }
1131
1132         sess = (struct rte_cryptodev_sym_session *)_sess;
1133
1134         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1135         if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1136                         NULL) {
1137                 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1138                                 dev_id);
1139
1140                 /* Return session to mempool */
1141                 rte_mempool_put(sess->mp, _sess);
1142                 return NULL;
1143         }
1144
1145         return sess;
1146 }
1147
1148 struct rte_cryptodev_sym_session *
1149 rte_cryptodev_sym_session_free(uint8_t dev_id,
1150                 struct rte_cryptodev_sym_session *sess)
1151 {
1152         struct rte_cryptodev *dev;
1153
1154         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1155                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1156                 return sess;
1157         }
1158
1159         dev = &rte_crypto_devices[dev_id];
1160
1161         /* Check the session belongs to this device type */
1162         if (sess->dev_type != dev->dev_type)
1163                 return sess;
1164
1165         /* Let device implementation clear session material */
1166         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_clear, sess);
1167         dev->dev_ops->session_clear(dev, (void *)sess->_private);
1168
1169         /* Return session to mempool */
1170         rte_mempool_put(sess->mp, (void *)sess);
1171
1172         return NULL;
1173 }
1174
1175 /** Initialise rte_crypto_op mempool element */
1176 static void
1177 rte_crypto_op_init(struct rte_mempool *mempool,
1178                 void *opaque_arg,
1179                 void *_op_data,
1180                 __rte_unused unsigned i)
1181 {
1182         struct rte_crypto_op *op = _op_data;
1183         enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1184
1185         memset(_op_data, 0, mempool->elt_size);
1186
1187         __rte_crypto_op_reset(op, type);
1188
1189         op->phys_addr = rte_mem_virt2phy(_op_data);
1190         op->mempool = mempool;
1191 }
1192
1193
1194 struct rte_mempool *
1195 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
1196                 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
1197                 int socket_id)
1198 {
1199         struct rte_crypto_op_pool_private *priv;
1200
1201         unsigned elt_size = sizeof(struct rte_crypto_op) +
1202                         sizeof(struct rte_crypto_sym_op) +
1203                         priv_size;
1204
1205         /* lookup mempool in case already allocated */
1206         struct rte_mempool *mp = rte_mempool_lookup(name);
1207
1208         if (mp != NULL) {
1209                 priv = (struct rte_crypto_op_pool_private *)
1210                                 rte_mempool_get_priv(mp);
1211
1212                 if (mp->elt_size != elt_size ||
1213                                 mp->cache_size < cache_size ||
1214                                 mp->size < nb_elts ||
1215                                 priv->priv_size <  priv_size) {
1216                         mp = NULL;
1217                         CDEV_LOG_ERR("Mempool %s already exists but with "
1218                                         "incompatible parameters", name);
1219                         return NULL;
1220                 }
1221                 return mp;
1222         }
1223
1224         mp = rte_mempool_create(
1225                         name,
1226                         nb_elts,
1227                         elt_size,
1228                         cache_size,
1229                         sizeof(struct rte_crypto_op_pool_private),
1230                         NULL,
1231                         NULL,
1232                         rte_crypto_op_init,
1233                         &type,
1234                         socket_id,
1235                         0);
1236
1237         if (mp == NULL) {
1238                 CDEV_LOG_ERR("Failed to create mempool %s", name);
1239                 return NULL;
1240         }
1241
1242         priv = (struct rte_crypto_op_pool_private *)
1243                         rte_mempool_get_priv(mp);
1244
1245         priv->priv_size = priv_size;
1246         priv->type = type;
1247
1248         return mp;
1249 }