Imported Upstream version 16.11.1
[deb_dpdk.git] / lib / librte_cryptodev / rte_cryptodev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <errno.h>
41 #include <stdint.h>
42 #include <inttypes.h>
43 #include <netinet/in.h>
44
45 #include <rte_byteorder.h>
46 #include <rte_log.h>
47 #include <rte_debug.h>
48 #include <rte_dev.h>
49 #include <rte_interrupts.h>
50 #include <rte_pci.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_launch.h>
55 #include <rte_tailq.h>
56 #include <rte_eal.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_common.h>
62 #include <rte_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 {
230         unsigned i;
231
232         if (name == NULL)
233                 return -1;
234
235         for (i = 0; i < rte_cryptodev_globals->nb_devs; i++)
236                 if ((strcmp(rte_cryptodev_globals->devs[i].data->name, name)
237                                 == 0) &&
238                                 (rte_cryptodev_globals->devs[i].attached ==
239                                                 RTE_CRYPTODEV_ATTACHED))
240                         return i;
241
242         return -1;
243 }
244
245 uint8_t
246 rte_cryptodev_count(void)
247 {
248         return rte_cryptodev_globals->nb_devs;
249 }
250
251 uint8_t
252 rte_cryptodev_count_devtype(enum rte_cryptodev_type type)
253 {
254         uint8_t i, dev_count = 0;
255
256         for (i = 0; i < rte_cryptodev_globals->max_devs; i++)
257                 if (rte_cryptodev_globals->devs[i].dev_type == type &&
258                         rte_cryptodev_globals->devs[i].attached ==
259                                         RTE_CRYPTODEV_ATTACHED)
260                         dev_count++;
261
262         return dev_count;
263 }
264
265 int
266 rte_cryptodev_socket_id(uint8_t dev_id)
267 {
268         struct rte_cryptodev *dev;
269
270         if (!rte_cryptodev_pmd_is_valid_dev(dev_id))
271                 return -1;
272
273         dev = rte_cryptodev_pmd_get_dev(dev_id);
274
275         return dev->data->socket_id;
276 }
277
278 static inline int
279 rte_cryptodev_data_alloc(uint8_t dev_id, struct rte_cryptodev_data **data,
280                 int socket_id)
281 {
282         char mz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
283         const struct rte_memzone *mz;
284         int n;
285
286         /* generate memzone name */
287         n = snprintf(mz_name, sizeof(mz_name), "rte_cryptodev_data_%u", dev_id);
288         if (n >= (int)sizeof(mz_name))
289                 return -EINVAL;
290
291         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
292                 mz = rte_memzone_reserve(mz_name,
293                                 sizeof(struct rte_cryptodev_data),
294                                 socket_id, 0);
295         } else
296                 mz = rte_memzone_lookup(mz_name);
297
298         if (mz == NULL)
299                 return -ENOMEM;
300
301         *data = mz->addr;
302         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
303                 memset(*data, 0, sizeof(struct rte_cryptodev_data));
304
305         return 0;
306 }
307
308 static uint8_t
309 rte_cryptodev_find_free_device_index(void)
310 {
311         uint8_t dev_id;
312
313         for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++) {
314                 if (rte_crypto_devices[dev_id].attached ==
315                                 RTE_CRYPTODEV_DETACHED)
316                         return dev_id;
317         }
318         return RTE_CRYPTO_MAX_DEVS;
319 }
320
321 struct rte_cryptodev *
322 rte_cryptodev_pmd_allocate(const char *name, int socket_id)
323 {
324         struct rte_cryptodev *cryptodev;
325         uint8_t dev_id;
326
327         if (rte_cryptodev_pmd_get_named_dev(name) != NULL) {
328                 CDEV_LOG_ERR("Crypto device with name %s already "
329                                 "allocated!", name);
330                 return NULL;
331         }
332
333         dev_id = rte_cryptodev_find_free_device_index();
334         if (dev_id == RTE_CRYPTO_MAX_DEVS) {
335                 CDEV_LOG_ERR("Reached maximum number of crypto devices");
336                 return NULL;
337         }
338
339         cryptodev = rte_cryptodev_pmd_get_dev(dev_id);
340
341         if (cryptodev->data == NULL) {
342                 struct rte_cryptodev_data *cryptodev_data =
343                                 cryptodev_globals.data[dev_id];
344
345                 int retval = rte_cryptodev_data_alloc(dev_id, &cryptodev_data,
346                                 socket_id);
347
348                 if (retval < 0 || cryptodev_data == NULL)
349                         return NULL;
350
351                 cryptodev->data = cryptodev_data;
352
353                 snprintf(cryptodev->data->name, RTE_CRYPTODEV_NAME_MAX_LEN,
354                                 "%s", name);
355
356                 cryptodev->data->dev_id = dev_id;
357                 cryptodev->data->socket_id = socket_id;
358                 cryptodev->data->dev_started = 0;
359
360                 cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
361
362                 cryptodev_globals.nb_devs++;
363         }
364
365         return cryptodev;
366 }
367
368 int
369 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev)
370 {
371         int ret;
372
373         if (cryptodev == NULL)
374                 return -EINVAL;
375
376         ret = rte_cryptodev_close(cryptodev->data->dev_id);
377         if (ret < 0)
378                 return ret;
379
380         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
381         cryptodev_globals.nb_devs--;
382         return 0;
383 }
384
385 struct rte_cryptodev *
386 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
387                 int socket_id)
388 {
389         struct rte_cryptodev *cryptodev;
390
391         /* allocate device structure */
392         cryptodev = rte_cryptodev_pmd_allocate(name, socket_id);
393         if (cryptodev == NULL)
394                 return NULL;
395
396         /* allocate private device structure */
397         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
398                 cryptodev->data->dev_private =
399                                 rte_zmalloc_socket("cryptodev device private",
400                                                 dev_private_size,
401                                                 RTE_CACHE_LINE_SIZE,
402                                                 socket_id);
403
404                 if (cryptodev->data->dev_private == NULL)
405                         rte_panic("Cannot allocate memzone for private device"
406                                         " data");
407         }
408
409         /* initialise user call-back tail queue */
410         TAILQ_INIT(&(cryptodev->link_intr_cbs));
411
412         return cryptodev;
413 }
414
415 int
416 rte_cryptodev_pci_probe(struct rte_pci_driver *pci_drv,
417                         struct rte_pci_device *pci_dev)
418 {
419         struct rte_cryptodev_driver *cryptodrv;
420         struct rte_cryptodev *cryptodev;
421
422         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
423
424         int retval;
425
426         cryptodrv = (struct rte_cryptodev_driver *)pci_drv;
427         if (cryptodrv == NULL)
428                 return -ENODEV;
429
430         rte_eal_pci_device_name(&pci_dev->addr, cryptodev_name,
431                         sizeof(cryptodev_name));
432
433         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
434         if (cryptodev == NULL)
435                 return -ENOMEM;
436
437         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
438                 cryptodev->data->dev_private =
439                                 rte_zmalloc_socket(
440                                                 "cryptodev private structure",
441                                                 cryptodrv->dev_private_size,
442                                                 RTE_CACHE_LINE_SIZE,
443                                                 rte_socket_id());
444
445                 if (cryptodev->data->dev_private == NULL)
446                         rte_panic("Cannot allocate memzone for private "
447                                         "device data");
448         }
449
450         cryptodev->pci_dev = pci_dev;
451         cryptodev->driver = cryptodrv;
452
453         /* init user callbacks */
454         TAILQ_INIT(&(cryptodev->link_intr_cbs));
455
456         /* Invoke PMD device initialization function */
457         retval = (*cryptodrv->cryptodev_init)(cryptodrv, cryptodev);
458         if (retval == 0)
459                 return 0;
460
461         CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
462                         " failed", pci_drv->driver.name,
463                         (unsigned) pci_dev->id.vendor_id,
464                         (unsigned) pci_dev->id.device_id);
465
466         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
467                 rte_free(cryptodev->data->dev_private);
468
469         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
470         cryptodev_globals.nb_devs--;
471
472         return -ENXIO;
473 }
474
475 int
476 rte_cryptodev_pci_remove(struct rte_pci_device *pci_dev)
477 {
478         const struct rte_cryptodev_driver *cryptodrv;
479         struct rte_cryptodev *cryptodev;
480         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
481         int ret;
482
483         if (pci_dev == NULL)
484                 return -EINVAL;
485
486         rte_eal_pci_device_name(&pci_dev->addr, cryptodev_name,
487                         sizeof(cryptodev_name));
488
489         cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
490         if (cryptodev == NULL)
491                 return -ENODEV;
492
493         cryptodrv = (const struct rte_cryptodev_driver *)pci_dev->driver;
494         if (cryptodrv == NULL)
495                 return -ENODEV;
496
497         /* Invoke PMD device uninit function */
498         if (*cryptodrv->cryptodev_uninit) {
499                 ret = (*cryptodrv->cryptodev_uninit)(cryptodrv, cryptodev);
500                 if (ret)
501                         return ret;
502         }
503
504         /* free crypto device */
505         rte_cryptodev_pmd_release_device(cryptodev);
506
507         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
508                 rte_free(cryptodev->data->dev_private);
509
510         cryptodev->pci_dev = NULL;
511         cryptodev->driver = NULL;
512         cryptodev->data = NULL;
513
514         return 0;
515 }
516
517 uint16_t
518 rte_cryptodev_queue_pair_count(uint8_t dev_id)
519 {
520         struct rte_cryptodev *dev;
521
522         dev = &rte_crypto_devices[dev_id];
523         return dev->data->nb_queue_pairs;
524 }
525
526 static int
527 rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
528                 int socket_id)
529 {
530         struct rte_cryptodev_info dev_info;
531         void **qp;
532         unsigned i;
533
534         if ((dev == NULL) || (nb_qpairs < 1)) {
535                 CDEV_LOG_ERR("invalid param: dev %p, nb_queues %u",
536                                                         dev, nb_qpairs);
537                 return -EINVAL;
538         }
539
540         CDEV_LOG_DEBUG("Setup %d queues pairs on device %u",
541                         nb_qpairs, dev->data->dev_id);
542
543         memset(&dev_info, 0, sizeof(struct rte_cryptodev_info));
544
545         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
546         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
547
548         if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
549                 CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
550                                 nb_qpairs, dev->data->dev_id);
551             return -EINVAL;
552         }
553
554         if (dev->data->queue_pairs == NULL) { /* first time configuration */
555                 dev->data->queue_pairs = rte_zmalloc_socket(
556                                 "cryptodev->queue_pairs",
557                                 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
558                                 RTE_CACHE_LINE_SIZE, socket_id);
559
560                 if (dev->data->queue_pairs == NULL) {
561                         dev->data->nb_queue_pairs = 0;
562                         CDEV_LOG_ERR("failed to get memory for qp meta data, "
563                                                         "nb_queues %u",
564                                                         nb_qpairs);
565                         return -(ENOMEM);
566                 }
567         } else { /* re-configure */
568                 int ret;
569                 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
570
571                 qp = dev->data->queue_pairs;
572
573                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
574                                 -ENOTSUP);
575
576                 for (i = nb_qpairs; i < old_nb_queues; i++) {
577                         ret = (*dev->dev_ops->queue_pair_release)(dev, i);
578                         if (ret < 0)
579                                 return ret;
580                 }
581
582                 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
583                                 RTE_CACHE_LINE_SIZE);
584                 if (qp == NULL) {
585                         CDEV_LOG_ERR("failed to realloc qp meta data,"
586                                                 " nb_queues %u", nb_qpairs);
587                         return -(ENOMEM);
588                 }
589
590                 if (nb_qpairs > old_nb_queues) {
591                         uint16_t new_qs = nb_qpairs - old_nb_queues;
592
593                         memset(qp + old_nb_queues, 0,
594                                 sizeof(qp[0]) * new_qs);
595                 }
596
597                 dev->data->queue_pairs = qp;
598
599         }
600         dev->data->nb_queue_pairs = nb_qpairs;
601         return 0;
602 }
603
604 int
605 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
606 {
607         struct rte_cryptodev *dev;
608
609         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
610                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
611                 return -EINVAL;
612         }
613
614         dev = &rte_crypto_devices[dev_id];
615         if (queue_pair_id >= dev->data->nb_queue_pairs) {
616                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
617                 return -EINVAL;
618         }
619
620         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_start, -ENOTSUP);
621
622         return dev->dev_ops->queue_pair_start(dev, queue_pair_id);
623
624 }
625
626 int
627 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
628 {
629         struct rte_cryptodev *dev;
630
631         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
632                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
633                 return -EINVAL;
634         }
635
636         dev = &rte_crypto_devices[dev_id];
637         if (queue_pair_id >= dev->data->nb_queue_pairs) {
638                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
639                 return -EINVAL;
640         }
641
642         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_stop, -ENOTSUP);
643
644         return dev->dev_ops->queue_pair_stop(dev, queue_pair_id);
645
646 }
647
648 static int
649 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
650                 unsigned nb_objs, unsigned obj_cache_size, int socket_id);
651
652 int
653 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
654 {
655         struct rte_cryptodev *dev;
656         int diag;
657
658         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
659                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
660                 return -EINVAL;
661         }
662
663         dev = &rte_crypto_devices[dev_id];
664
665         if (dev->data->dev_started) {
666                 CDEV_LOG_ERR(
667                     "device %d must be stopped to allow configuration", dev_id);
668                 return -EBUSY;
669         }
670
671         /* Setup new number of queue pairs and reconfigure device. */
672         diag = rte_cryptodev_queue_pairs_config(dev, config->nb_queue_pairs,
673                         config->socket_id);
674         if (diag != 0) {
675                 CDEV_LOG_ERR("dev%d rte_crypto_dev_queue_pairs_config = %d",
676                                 dev_id, diag);
677                 return diag;
678         }
679
680         /* Setup Session mempool for device */
681         return rte_cryptodev_sym_session_pool_create(dev,
682                         config->session_mp.nb_objs,
683                         config->session_mp.cache_size,
684                         config->socket_id);
685 }
686
687
688 int
689 rte_cryptodev_start(uint8_t dev_id)
690 {
691         struct rte_cryptodev *dev;
692         int diag;
693
694         CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
695
696         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
697                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
698                 return -EINVAL;
699         }
700
701         dev = &rte_crypto_devices[dev_id];
702
703         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
704
705         if (dev->data->dev_started != 0) {
706                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already started",
707                         dev_id);
708                 return 0;
709         }
710
711         diag = (*dev->dev_ops->dev_start)(dev);
712         if (diag == 0)
713                 dev->data->dev_started = 1;
714         else
715                 return diag;
716
717         return 0;
718 }
719
720 void
721 rte_cryptodev_stop(uint8_t dev_id)
722 {
723         struct rte_cryptodev *dev;
724
725         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
726                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
727                 return;
728         }
729
730         dev = &rte_crypto_devices[dev_id];
731
732         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
733
734         if (dev->data->dev_started == 0) {
735                 CDEV_LOG_ERR("Device with dev_id=%" PRIu8 " already stopped",
736                         dev_id);
737                 return;
738         }
739
740         dev->data->dev_started = 0;
741         (*dev->dev_ops->dev_stop)(dev);
742 }
743
744 int
745 rte_cryptodev_close(uint8_t dev_id)
746 {
747         struct rte_cryptodev *dev;
748         int retval;
749
750         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
751                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
752                 return -1;
753         }
754
755         dev = &rte_crypto_devices[dev_id];
756
757         /* Device must be stopped before it can be closed */
758         if (dev->data->dev_started == 1) {
759                 CDEV_LOG_ERR("Device %u must be stopped before closing",
760                                 dev_id);
761                 return -EBUSY;
762         }
763
764         /* We can't close the device if there are outstanding sessions in use */
765         if (dev->data->session_pool != NULL) {
766                 if (!rte_mempool_full(dev->data->session_pool)) {
767                         CDEV_LOG_ERR("dev_id=%u close failed, session mempool "
768                                         "has sessions still in use, free "
769                                         "all sessions before calling close",
770                                         (unsigned)dev_id);
771                         return -EBUSY;
772                 }
773         }
774
775         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
776         retval = (*dev->dev_ops->dev_close)(dev);
777
778         if (retval < 0)
779                 return retval;
780
781         return 0;
782 }
783
784 int
785 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
786                 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id)
787 {
788         struct rte_cryptodev *dev;
789
790         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
791                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
792                 return -EINVAL;
793         }
794
795         dev = &rte_crypto_devices[dev_id];
796         if (queue_pair_id >= dev->data->nb_queue_pairs) {
797                 CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
798                 return -EINVAL;
799         }
800
801         if (dev->data->dev_started) {
802                 CDEV_LOG_ERR(
803                     "device %d must be stopped to allow configuration", dev_id);
804                 return -EBUSY;
805         }
806
807         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
808
809         return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id, qp_conf,
810                         socket_id);
811 }
812
813
814 int
815 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
816 {
817         struct rte_cryptodev *dev;
818
819         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
820                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
821                 return -ENODEV;
822         }
823
824         if (stats == NULL) {
825                 CDEV_LOG_ERR("Invalid stats ptr");
826                 return -EINVAL;
827         }
828
829         dev = &rte_crypto_devices[dev_id];
830         memset(stats, 0, sizeof(*stats));
831
832         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
833         (*dev->dev_ops->stats_get)(dev, stats);
834         return 0;
835 }
836
837 void
838 rte_cryptodev_stats_reset(uint8_t dev_id)
839 {
840         struct rte_cryptodev *dev;
841
842         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
843                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
844                 return;
845         }
846
847         dev = &rte_crypto_devices[dev_id];
848
849         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
850         (*dev->dev_ops->stats_reset)(dev);
851 }
852
853
854 void
855 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info)
856 {
857         struct rte_cryptodev *dev;
858
859         if (dev_id >= cryptodev_globals.nb_devs) {
860                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
861                 return;
862         }
863
864         dev = &rte_crypto_devices[dev_id];
865
866         memset(dev_info, 0, sizeof(struct rte_cryptodev_info));
867
868         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
869         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
870
871         dev_info->pci_dev = dev->pci_dev;
872         if (dev->driver)
873                 dev_info->driver_name = dev->driver->pci_drv.driver.name;
874 }
875
876
877 int
878 rte_cryptodev_callback_register(uint8_t dev_id,
879                         enum rte_cryptodev_event_type event,
880                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
881 {
882         struct rte_cryptodev *dev;
883         struct rte_cryptodev_callback *user_cb;
884
885         if (!cb_fn)
886                 return -EINVAL;
887
888         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
889                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
890                 return -EINVAL;
891         }
892
893         dev = &rte_crypto_devices[dev_id];
894         rte_spinlock_lock(&rte_cryptodev_cb_lock);
895
896         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
897                 if (user_cb->cb_fn == cb_fn &&
898                         user_cb->cb_arg == cb_arg &&
899                         user_cb->event == event) {
900                         break;
901                 }
902         }
903
904         /* create a new callback. */
905         if (user_cb == NULL) {
906                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
907                                 sizeof(struct rte_cryptodev_callback), 0);
908                 if (user_cb != NULL) {
909                         user_cb->cb_fn = cb_fn;
910                         user_cb->cb_arg = cb_arg;
911                         user_cb->event = event;
912                         TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
913                 }
914         }
915
916         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
917         return (user_cb == NULL) ? -ENOMEM : 0;
918 }
919
920 int
921 rte_cryptodev_callback_unregister(uint8_t dev_id,
922                         enum rte_cryptodev_event_type event,
923                         rte_cryptodev_cb_fn cb_fn, void *cb_arg)
924 {
925         int ret;
926         struct rte_cryptodev *dev;
927         struct rte_cryptodev_callback *cb, *next;
928
929         if (!cb_fn)
930                 return -EINVAL;
931
932         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
933                 CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
934                 return -EINVAL;
935         }
936
937         dev = &rte_crypto_devices[dev_id];
938         rte_spinlock_lock(&rte_cryptodev_cb_lock);
939
940         ret = 0;
941         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
942
943                 next = TAILQ_NEXT(cb, next);
944
945                 if (cb->cb_fn != cb_fn || cb->event != event ||
946                                 (cb->cb_arg != (void *)-1 &&
947                                 cb->cb_arg != cb_arg))
948                         continue;
949
950                 /*
951                  * if this callback is not executing right now,
952                  * then remove it.
953                  */
954                 if (cb->active == 0) {
955                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
956                         rte_free(cb);
957                 } else {
958                         ret = -EAGAIN;
959                 }
960         }
961
962         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
963         return ret;
964 }
965
966 void
967 rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
968         enum rte_cryptodev_event_type event)
969 {
970         struct rte_cryptodev_callback *cb_lst;
971         struct rte_cryptodev_callback dev_cb;
972
973         rte_spinlock_lock(&rte_cryptodev_cb_lock);
974         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
975                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
976                         continue;
977                 dev_cb = *cb_lst;
978                 cb_lst->active = 1;
979                 rte_spinlock_unlock(&rte_cryptodev_cb_lock);
980                 dev_cb.cb_fn(dev->data->dev_id, dev_cb.event,
981                                                 dev_cb.cb_arg);
982                 rte_spinlock_lock(&rte_cryptodev_cb_lock);
983                 cb_lst->active = 0;
984         }
985         rte_spinlock_unlock(&rte_cryptodev_cb_lock);
986 }
987
988
989 static void
990 rte_cryptodev_sym_session_init(struct rte_mempool *mp,
991                 void *opaque_arg,
992                 void *_sess,
993                 __rte_unused unsigned i)
994 {
995         struct rte_cryptodev_sym_session *sess = _sess;
996         struct rte_cryptodev *dev = opaque_arg;
997
998         memset(sess, 0, mp->elt_size);
999
1000         sess->dev_id = dev->data->dev_id;
1001         sess->dev_type = dev->dev_type;
1002         sess->mp = mp;
1003
1004         if (dev->dev_ops->session_initialize)
1005                 (*dev->dev_ops->session_initialize)(mp, sess);
1006 }
1007
1008 static int
1009 rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
1010                 unsigned nb_objs, unsigned obj_cache_size, int socket_id)
1011 {
1012         char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1013         unsigned priv_sess_size;
1014
1015         unsigned n = snprintf(mp_name, sizeof(mp_name), "cdev_%d_sess_mp",
1016                         dev->data->dev_id);
1017         if (n > sizeof(mp_name)) {
1018                 CDEV_LOG_ERR("Unable to create unique name for session mempool");
1019                 return -ENOMEM;
1020         }
1021
1022         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_get_size, -ENOTSUP);
1023         priv_sess_size = (*dev->dev_ops->session_get_size)(dev);
1024         if (priv_sess_size == 0) {
1025                 CDEV_LOG_ERR("%s returned and invalid private session size ",
1026                                                 dev->data->name);
1027                 return -ENOMEM;
1028         }
1029
1030         unsigned elt_size = sizeof(struct rte_cryptodev_sym_session) +
1031                         priv_sess_size;
1032
1033         dev->data->session_pool = rte_mempool_lookup(mp_name);
1034         if (dev->data->session_pool != NULL) {
1035                 if ((dev->data->session_pool->elt_size != elt_size) ||
1036                                 (dev->data->session_pool->cache_size <
1037                                 obj_cache_size) ||
1038                                 (dev->data->session_pool->size < nb_objs)) {
1039
1040                         CDEV_LOG_ERR("%s mempool already exists with different"
1041                                         " initialization parameters", mp_name);
1042                         dev->data->session_pool = NULL;
1043                         return -ENOMEM;
1044                 }
1045         } else {
1046                 dev->data->session_pool = rte_mempool_create(
1047                                 mp_name, /* mempool name */
1048                                 nb_objs, /* number of elements*/
1049                                 elt_size, /* element size*/
1050                                 obj_cache_size, /* Cache size*/
1051                                 0, /* private data size */
1052                                 NULL, /* obj initialization constructor */
1053                                 NULL, /* obj initialization constructor arg */
1054                                 rte_cryptodev_sym_session_init,
1055                                 /**< obj constructor*/
1056                                 dev, /* obj constructor arg */
1057                                 socket_id, /* socket id */
1058                                 0); /* flags */
1059
1060                 if (dev->data->session_pool == NULL) {
1061                         CDEV_LOG_ERR("%s mempool allocation failed", mp_name);
1062                         return -ENOMEM;
1063                 }
1064         }
1065
1066         CDEV_LOG_DEBUG("%s mempool created!", mp_name);
1067         return 0;
1068 }
1069
1070 struct rte_cryptodev_sym_session *
1071 rte_cryptodev_sym_session_create(uint8_t dev_id,
1072                 struct rte_crypto_sym_xform *xform)
1073 {
1074         struct rte_cryptodev *dev;
1075         struct rte_cryptodev_sym_session *sess;
1076         void *_sess;
1077
1078         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1079                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1080                 return NULL;
1081         }
1082
1083         dev = &rte_crypto_devices[dev_id];
1084
1085         /* Allocate a session structure from the session pool */
1086         if (rte_mempool_get(dev->data->session_pool, &_sess)) {
1087                 CDEV_LOG_ERR("Couldn't get object from session mempool");
1088                 return NULL;
1089         }
1090
1091         sess = (struct rte_cryptodev_sym_session *)_sess;
1092
1093         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
1094         if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
1095                         NULL) {
1096                 CDEV_LOG_ERR("dev_id %d failed to configure session details",
1097                                 dev_id);
1098
1099                 /* Return session to mempool */
1100                 rte_mempool_put(sess->mp, _sess);
1101                 return NULL;
1102         }
1103
1104         return sess;
1105 }
1106
1107 struct rte_cryptodev_sym_session *
1108 rte_cryptodev_sym_session_free(uint8_t dev_id,
1109                 struct rte_cryptodev_sym_session *sess)
1110 {
1111         struct rte_cryptodev *dev;
1112
1113         if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
1114                 CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
1115                 return sess;
1116         }
1117
1118         dev = &rte_crypto_devices[dev_id];
1119
1120         /* Check the session belongs to this device type */
1121         if (sess->dev_type != dev->dev_type)
1122                 return sess;
1123
1124         /* Let device implementation clear session material */
1125         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_clear, sess);
1126         dev->dev_ops->session_clear(dev, (void *)sess->_private);
1127
1128         /* Return session to mempool */
1129         rte_mempool_put(sess->mp, (void *)sess);
1130
1131         return NULL;
1132 }
1133
1134 /** Initialise rte_crypto_op mempool element */
1135 static void
1136 rte_crypto_op_init(struct rte_mempool *mempool,
1137                 void *opaque_arg,
1138                 void *_op_data,
1139                 __rte_unused unsigned i)
1140 {
1141         struct rte_crypto_op *op = _op_data;
1142         enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
1143
1144         memset(_op_data, 0, mempool->elt_size);
1145
1146         __rte_crypto_op_reset(op, type);
1147
1148         op->phys_addr = rte_mem_virt2phy(_op_data);
1149         op->mempool = mempool;
1150 }
1151
1152
1153 struct rte_mempool *
1154 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
1155                 unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
1156                 int socket_id)
1157 {
1158         struct rte_crypto_op_pool_private *priv;
1159
1160         unsigned elt_size = sizeof(struct rte_crypto_op) +
1161                         sizeof(struct rte_crypto_sym_op) +
1162                         priv_size;
1163
1164         /* lookup mempool in case already allocated */
1165         struct rte_mempool *mp = rte_mempool_lookup(name);
1166
1167         if (mp != NULL) {
1168                 priv = (struct rte_crypto_op_pool_private *)
1169                                 rte_mempool_get_priv(mp);
1170
1171                 if (mp->elt_size != elt_size ||
1172                                 mp->cache_size < cache_size ||
1173                                 mp->size < nb_elts ||
1174                                 priv->priv_size <  priv_size) {
1175                         mp = NULL;
1176                         CDEV_LOG_ERR("Mempool %s already exists but with "
1177                                         "incompatible parameters", name);
1178                         return NULL;
1179                 }
1180                 return mp;
1181         }
1182
1183         mp = rte_mempool_create(
1184                         name,
1185                         nb_elts,
1186                         elt_size,
1187                         cache_size,
1188                         sizeof(struct rte_crypto_op_pool_private),
1189                         NULL,
1190                         NULL,
1191                         rte_crypto_op_init,
1192                         &type,
1193                         socket_id,
1194                         0);
1195
1196         if (mp == NULL) {
1197                 CDEV_LOG_ERR("Failed to create mempool %s", name);
1198                 return NULL;
1199         }
1200
1201         priv = (struct rte_crypto_op_pool_private *)
1202                         rte_mempool_get_priv(mp);
1203
1204         priv->priv_size = priv_size;
1205         priv->type = type;
1206
1207         return mp;
1208 }