f591711dfc6b7ac153068f892d05375daf6a21b9
[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 uint16_t
536 rte_cryptodev_queue_pair_count(uint8_t dev_id)
537 {
538         struct rte_cryptodev *dev;
539
540         dev = &rte_crypto_devices[dev_id];
541         return dev->data->nb_queue_pairs;
542 }
543
544 static int
545 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
546                 int socket_id)
547 {
548         struct rte_cryptodev_info dev_info;
549         void **qp;
550         unsigned i;
551
552         if ((dev == NULL) || (nb_qpairs < 1)) {
553                 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
554                                                         dev, nb_qpairs);
555                 return -EINVAL;
556         }
557
558         CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
559                         nb_qpairs, dev->data->dev_id);
560
561         memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
562
563         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
564         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
565
566         if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
567                 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
568                                 nb_qpairs, dev->data->dev_id);
569             return -EINVAL;
570         }
571
572         if (dev->data->queue_pairs == NULL) { /* first time configuration */
573                 dev->data->queue_pairs = rte_zmalloc_socket(
574                                 "cryptodev->queue_pairs",
575                                 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
576                                 RTE_CACHE_LINE_SIZE, socket_id);
577
578                 if (dev->data->queue_pairs == NULL) {
579                         dev->data->nb_queue_pairs = 0;
580                         CDEV_LOG_ERR("failed to get memory for qp meta data, "
581                                                         "nb_queues %u",
582                                                         nb_qpairs);
583                         return -(ENOMEM);
584                 }
585         } else { /* re-configure */
586                 int ret;
587                 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
588
589                 qp = dev->data->queue_pairs;
590
591                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
592                                 -ENOTSUP);
593
594                 for (i = nb_qpairs; i < old_nb_queues; i++) {
595                         ret = (*dev->dev_ops->queue_pair_release)(dev, i);
596                         if (ret < 0)
597                                 return ret;
598                 }
599
600                 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
601                                 RTE_CACHE_LINE_SIZE);
602                 if (qp == NULL) {
603                         CDEV_LOG_ERR("failed to realloc qp meta data,"
604                                                 " nb_queues %u", nb_qpairs);
605                         return -(ENOMEM);
606                 }
607
608                 if (nb_qpairs > old_nb_queues) {
609                         uint16_t new_qs = nb_qpairs - old_nb_queues;
610
611                         memset(qp + old_nb_queues, 0,
612                                 sizeof(qp[0]) * new_qs);
613                 }
614
615                 dev->data->queue_pairs = qp;
616
617         }
618         dev->data->nb_queue_pairs = nb_qpairs;
619         return 0;
620 }
621
622 int
623 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
624 {
625         struct rte_cryptodev *dev;
626
627         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
628                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
629                 return -EINVAL;
630         }
631
632         dev = &rte_crypto_devices[dev_id];
633         if (queue_pair_id >= dev->data->nb_queue_pairs) {
634                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
635                 return -EINVAL;
636         }
637
638         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
639
640         return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
641
642 }
643
644 int
645 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
646 {
647         struct rte_cryptodev *dev;
648
649         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
650                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
651                 return -EINVAL;
652         }
653
654         dev = &rte_crypto_devices[dev_id];
655         if (queue_pair_id >= dev->data->nb_queue_pairs) {
656                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
657                 return -EINVAL;
658         }
659
660         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
661
662         return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
663
664 }
665
666 static int
667 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
668                 unsigned nb_objs, unsigned obj_cache_size, int socket_id);
669
670 int
671 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
672 {
673         struct rte_cryptodev *dev;
674         int diag;
675
676         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
677                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
678                 return -EINVAL;
679         }
680
681         dev = &rte_crypto_devices[dev_id];
682
683         if (dev->data->dev_started) {
684                 CDEV_LOG_ERR(
685                     "device %d must be stopped to allow configuration", dev_id);
686                 return -EBUSY;
687         }
688
689         /* Setup new number of queue pairs and reconfigure device. */
690         diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
691                         config->socket_id);
692         if (diag != 0) {
693                 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
694                                 dev_id, diag);
695                 return diag;
696         }
697
698         /* Setup Session mempool for device */
699         return rte_cryptodev_sym_session_pool_create(dev,
700                         config->session_mp.nb_objs,
701                         config->session_mp.cache_size,
702                         config->socket_id);
703 }
704
705
706 int
707 rte_cryptodev_start(uint8_t dev_id)
708 {
709         struct rte_cryptodev *dev;
710         int diag;
711
712         CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
713
714         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
715                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
716                 return -EINVAL;
717         }
718
719         dev = &rte_crypto_devices[dev_id];
720
721         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
722
723         if (dev->data->dev_started != 0) {
724                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
725                         dev_id);
726                 return 0;
727         }
728
729         diag = (*dev->dev_ops->dev_start)(dev);
730         if (diag == 0)
731                 dev->data->dev_started = 1;
732         else
733                 return diag;
734
735         return 0;
736 }
737
738 void
739 rte_cryptodev_stop(uint8_t dev_id)
740 {
741         struct rte_cryptodev *dev;
742
743         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
744                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
745                 return;
746         }
747
748         dev = &rte_crypto_devices[dev_id];
749
750         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
751
752         if (dev->data->dev_started == 0) {
753                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
754                         dev_id);
755                 return;
756         }
757
758         dev->data->dev_started = 0;
759         (*dev->dev_ops->dev_stop)(dev);
760 }
761
762 int
763 rte_cryptodev_close(uint8_t dev_id)
764 {
765         struct rte_cryptodev *dev;
766         int retval;
767
768         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
769                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
770                 return -1;
771         }
772
773         dev = &rte_crypto_devices[dev_id];
774
775         /* Device must be stopped before it can be closed */
776         if (dev->data->dev_started == 1) {
777                 CDEV_LOG_ERR("Device %u must be stopped before closing",
778                                 dev_id);
779                 return -EBUSY;
780         }
781
782         /* We can't close the device if there are outstanding sessions in use */
783         if (dev->data->session_pool != NULL) {
784                 if (!rte_mempool_full(dev->data->session_pool)) {
785                         CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
786                                         "has sessions still in use, free "
787                                         "all sessions before calling close",
788                                         (unsigned)dev_id);
789                         return -EBUSY;
790                 }
791         }
792
793         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
794         retval = (*dev->dev_ops->dev_close)(dev);
795
796         if (retval < 0)
797                 return retval;
798
799         return 0;
800 }
801
802 int
803 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
804                 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
805 {
806         struct rte_cryptodev *dev;
807
808         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
809                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
810                 return -EINVAL;
811         }
812
813         dev = &rte_crypto_devices[dev_id];
814         if (queue_pair_id >= dev->data->nb_queue_pairs) {
815                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
816                 return -EINVAL;
817         }
818
819         if (dev->data->dev_started) {
820                 CDEV_LOG_ERR(
821                     "device %d must be stopped to allow configuration", dev_id);
822                 return -EBUSY;
823         }
824
825         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
826
827         return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
828                         socket_id);
829 }
830
831
832 int
833 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
834 {
835         struct rte_cryptodev *dev;
836
837         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
838                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
839                 return -ENODEV;
840         }
841
842         if (stats == NULL) {
843                 CDEV_LOG_ERR("Invalid stats ptr");
844                 return -EINVAL;
845         }
846
847         dev = &rte_crypto_devices[dev_id];
848         memset(stats, 0, sizeof(*stats));
849
850         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
851         (*dev->dev_ops->stats_get)(dev, stats);
852         return 0;
853 }
854
855 void
856 rte_cryptodev_stats_reset(uint8_t dev_id)
857 {
858         struct rte_cryptodev *dev;
859
860         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
861                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
862                 return;
863         }
864
865         dev = &rte_crypto_devices[dev_id];
866
867         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
868         (*dev->dev_ops->stats_reset)(dev);
869 }
870
871
872 void
873 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
874 {
875         struct rte_cryptodev *dev;
876
877         if (dev_id >= cryptodev_globals.nb_devs) {
878                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
879                 return;
880         }
881
882         dev = &rte_crypto_devices[dev_id];
883
884         memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
885
886         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
887         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
888
889         dev_info->pci_dev = dev->pci_dev;
890         if (dev->driver)
891                 dev_info->driver_name = dev->driver->pci_drv.name;
892 }
893
894
895 int
896 rte_cryptodev_callback_register(uint8_t dev_id,
897                         enum rte_cryptodev_event_type event,
898                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
899 {
900         struct rte_cryptodev *dev;
901         struct rte_cryptodev_callback *user_cb;
902
903         if (!cb_fn)
904                 return -EINVAL;
905
906         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
907                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
908                 return -EINVAL;
909         }
910
911         dev = &rte_crypto_devices[dev_id];
912         rte_spinlock_lock(&rte_cryptodev_cb_lock);
913
914         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
915                 if (user_cb->cb_fn == cb_fn &&
916                         user_cb->cb_arg == cb_arg &&
917                         user_cb->event == event) {
918                         break;
919                 }
920         }
921
922         /* create a new callback. */
923         if (user_cb == NULL) {
924                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
925                                 sizeof(struct rte_cryptodev_callback), 0);
926                 if (user_cb != NULL) {
927                         user_cb->cb_fn = cb_fn;
928                         user_cb->cb_arg = cb_arg;
929                         user_cb->event = event;
930                         TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
931                 }
932         }
933
934         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
935         return (user_cb == NULL) ? -ENOMEM : 0;
936 }
937
938 int
939 rte_cryptodev_callback_unregister(uint8_t dev_id,
940                         enum rte_cryptodev_event_type event,
941                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
942 {
943         int ret;
944         struct rte_cryptodev *dev;
945         struct rte_cryptodev_callback *cb, *next;
946
947         if (!cb_fn)
948                 return -EINVAL;
949
950         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
951                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
952                 return -EINVAL;
953         }
954
955         dev = &rte_crypto_devices[dev_id];
956         rte_spinlock_lock(&rte_cryptodev_cb_lock);
957
958         ret = 0;
959         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
960
961                 next = TAILQ_NEXT(cb, next);
962
963                 if (cb->cb_fn != cb_fn || cb->event != event ||
964                                 (cb->cb_arg != (void *)-1 &&
965                                 cb->cb_arg != cb_arg))
966                         continue;
967
968                 /*
969                  * if this callback is not executing right now,
970                  * then remove it.
971                  */
972                 if (cb->active == 0) {
973                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
974                         rte_free(cb);
975                 } else {
976                         ret = -EAGAIN;
977                 }
978         }
979
980         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
981         return ret;
982 }
983
984 void
985 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
986         enum rte_cryptodev_event_type event)
987 {
988         struct rte_cryptodev_callback *cb_lst;
989         struct rte_cryptodev_callback dev_cb;
990
991         rte_spinlock_lock(&rte_cryptodev_cb_lock);
992         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
993                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
994                         continue;
995                 dev_cb = *cb_lst;
996                 cb_lst->active = 1;
997                 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
998                 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
999                                                 dev_cb.cb_arg);
1000                 rte_spinlock_lock(&rte_cryptodev_cb_lock);
1001                 cb_lst->active = 0;
1002         }
1003         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
1004 }
1005
1006
1007 static void
1008 rte_cryptodev_sym_session_init(struct rte_mempool *mp,
1009                 void *opaque_arg,
1010                 void *_sess,
1011                 __rte_unused unsigned i)
1012 {
1013         struct rte_cryptodev_sym_session *sess = _sess;
1014         struct rte_cryptodev *dev = opaque_arg;
1015
1016         memset(sess, 0, mp->elt_size);
1017
1018         sess->dev_id = dev->data->dev_id;
1019         sess->dev_type = dev->dev_type;
1020         sess->mp = mp;
1021
1022         if (dev->dev_ops->session_initialize)
1023                 (*dev->dev_ops->session_initialize)(mp, sess);
1024 }
1025
1026 static int
1027 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
1028                 unsigned nb_objs, unsigned obj_cache_size, int socket_id)
1029 {
1030         char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1031         unsigned priv_sess_size;
1032
1033         unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
1034                         dev->data->dev_id);
1035         if (n > sizeof(mp_name)) {
1036                 CDEV_LOG_ERR("Unable to create unique name for session mempool");
1037                 return -ENOMEM;
1038         }
1039
1040         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_get_size, -ENOTSUP);
1041         priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
1042         if (priv_sess_size == 0) {
1043                 CDEV_LOG_ERR("%s returned and invalid private session size ",
1044                                                 dev->data->name);
1045                 return -ENOMEM;
1046         }
1047
1048         unsigned elt_size = sizeof(struct rte_cryptodev_sym_session) +
1049                         priv_sess_size;
1050
1051         dev->data->session_pool = rte_mempool_lookup(mp_name);
1052         if (dev->data->session_pool != NULL) {
1053                 if ((dev->data->session_pool->elt_size != elt_size) ||
1054                                 (dev->data->session_pool->cache_size <
1055                                 obj_cache_size) ||
1056                                 (dev->data->session_pool->size < nb_objs)) {
1057
1058                         CDEV_LOG_ERR("%s mempool already exists with different"
1059                                         " initialization parameters", mp_name);
1060                         dev->data->session_pool = NULL;
1061                         return -ENOMEM;
1062                 }
1063         } else {
1064                 dev->data->session_pool = rte_mempool_create(
1065                                 mp_name, /* mempool name */
1066                                 nb_objs, /* number of elements*/
1067                                 elt_size, /* element size*/
1068                                 obj_cache_size, /* Cache size*/
1069                                 0, /* private data size */
1070                                 NULL, /* obj initialization constructor */
1071                                 NULL, /* obj initialization constructor arg */
1072                                 rte_cryptodev_sym_session_init,
1073                                 /**< obj constructor*/
1074                                 dev, /* obj constructor arg */
1075                                 socket_id, /* socket id */
1076                                 0); /* flags */
1077
1078                 if (dev->data->session_pool == NULL) {
1079                         CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
1080                         return -ENOMEM;
1081                 }
1082         }
1083
1084         CDEV_LOG_DEBUG("%s mempool created!", mp_name);
1085         return 0;
1086 }
1087
1088 struct rte_cryptodev_sym_session *
1089 rte_cryptodev_sym_session_create(uint8_t dev_id,
1090                 struct rte_crypto_sym_xform *xform)
1091 {
1092         struct rte_cryptodev *dev;
1093         struct rte_cryptodev_sym_session *sess;
1094         void *_sess;
1095
1096         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1097                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1098                 return NULL;
1099         }
1100
1101         dev = &rte_crypto_devices[dev_id];
1102
1103         /* Allocate a session structure from the session pool */
1104         if (rte_mempool_get(dev->data->session_pool, &_sess)) {
1105                 CDEV_LOG_ERR("Couldn't get object from session mempool");
1106                 return NULL;
1107         }
1108
1109         sess = (struct rte_cryptodev_sym_session *)_sess;
1110
1111         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1112         if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1113                         NULL) {
1114                 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1115                                 dev_id);
1116
1117                 /* Return session to mempool */
1118                 rte_mempool_put(sess->mp, _sess);
1119                 return NULL;
1120         }
1121
1122         return sess;
1123 }
1124
1125 struct rte_cryptodev_sym_session *
1126 rte_cryptodev_sym_session_free(uint8_t dev_id,
1127                 struct rte_cryptodev_sym_session *sess)
1128 {
1129         struct rte_cryptodev *dev;
1130
1131         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1132                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1133                 return sess;
1134         }
1135
1136         dev = &rte_crypto_devices[dev_id];
1137
1138         /* Check the session belongs to this device type */
1139         if (sess->dev_type != dev->dev_type)
1140                 return sess;
1141
1142         /* Let device implementation clear session material */
1143         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_clear, sess);
1144         dev->dev_ops->session_clear(dev, (void *)sess->_private);
1145
1146         /* Return session to mempool */
1147         rte_mempool_put(sess->mp, (void *)sess);
1148
1149         return NULL;
1150 }
1151
1152 /** Initialise rte_crypto_op mempool element */
1153 static void
1154 rte_crypto_op_init(struct rte_mempool *mempool,
1155                 void *opaque_arg,
1156                 void *_op_data,
1157                 __rte_unused unsigned i)
1158 {
1159         struct rte_crypto_op *op = _op_data;
1160         enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1161
1162         memset(_op_data, 0, mempool->elt_size);
1163
1164         __rte_crypto_op_reset(op, type);
1165
1166         op->phys_addr = rte_mem_virt2phy(_op_data);
1167         op->mempool = mempool;
1168 }
1169
1170
1171 struct rte_mempool *
1172 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
1173                 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
1174                 int socket_id)
1175 {
1176         struct rte_crypto_op_pool_private *priv;
1177
1178         unsigned elt_size = sizeof(struct rte_crypto_op) +
1179                         sizeof(struct rte_crypto_sym_op) +
1180                         priv_size;
1181
1182         /* lookup mempool in case already allocated */
1183         struct rte_mempool *mp = rte_mempool_lookup(name);
1184
1185         if (mp != NULL) {
1186                 priv = (struct rte_crypto_op_pool_private *)
1187                                 rte_mempool_get_priv(mp);
1188
1189                 if (mp->elt_size != elt_size ||
1190                                 mp->cache_size < cache_size ||
1191                                 mp->size < nb_elts ||
1192                                 priv->priv_size <  priv_size) {
1193                         mp = NULL;
1194                         CDEV_LOG_ERR("Mempool %s already exists but with "
1195                                         "incompatible parameters", name);
1196                         return NULL;
1197                 }
1198                 return mp;
1199         }
1200
1201         mp = rte_mempool_create(
1202                         name,
1203                         nb_elts,
1204                         elt_size,
1205                         cache_size,
1206                         sizeof(struct rte_crypto_op_pool_private),
1207                         NULL,
1208                         NULL,
1209                         rte_crypto_op_init,
1210                         &type,
1211                         socket_id,
1212                         0);
1213
1214         if (mp == NULL) {
1215                 CDEV_LOG_ERR("Failed to create mempool %s", name);
1216                 return NULL;
1217         }
1218
1219         priv = (struct rte_crypto_op_pool_private *)
1220                         rte_mempool_get_priv(mp);
1221
1222         priv->priv_size = priv_size;
1223         priv->type = type;
1224
1225         return mp;
1226 }