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