New upstream version 18.11-rc1
[deb_dpdk.git] / examples / ip_pipeline / cryptodev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #include <rte_cryptodev.h>
9 #include <rte_cryptodev_pmd.h>
10 #include <rte_string_fns.h>
11
12 #include "cryptodev.h"
13
14 static struct cryptodev_list cryptodev_list;
15
16 int
17 cryptodev_init(void)
18 {
19         TAILQ_INIT(&cryptodev_list);
20
21         return 0;
22 }
23
24 struct cryptodev *
25 cryptodev_find(const char *name)
26 {
27         struct cryptodev *cryptodev;
28
29         if (name == NULL)
30                 return NULL;
31
32         TAILQ_FOREACH(cryptodev, &cryptodev_list, node)
33                 if (strcmp(cryptodev->name, name) == 0)
34                         return cryptodev;
35
36         return NULL;
37 }
38
39 struct cryptodev *
40 cryptodev_next(struct cryptodev *cryptodev)
41 {
42         return (cryptodev == NULL) ?
43                         TAILQ_FIRST(&cryptodev_list) :
44                         TAILQ_NEXT(cryptodev, node);
45 }
46
47 struct cryptodev *
48 cryptodev_create(const char *name, struct cryptodev_params *params)
49 {
50         struct rte_cryptodev_info dev_info;
51         struct rte_cryptodev_config dev_conf;
52         struct rte_cryptodev_qp_conf queue_conf;
53         struct cryptodev *cryptodev;
54         uint32_t dev_id, i;
55         uint32_t socket_id;
56         int status;
57
58         /* Check input params */
59         if ((name == NULL) ||
60                 cryptodev_find(name) ||
61                 (params->n_queues == 0) ||
62                 (params->queue_size == 0))
63                 return NULL;
64
65         if (params->dev_name) {
66                 status = rte_cryptodev_get_dev_id(params->dev_name);
67                 if (status == -1)
68                         return NULL;
69
70                 dev_id = (uint32_t)status;
71         } else {
72                 if (rte_cryptodev_pmd_is_valid_dev(params->dev_id) == 0)
73                         return NULL;
74
75                 dev_id = params->dev_id;
76         }
77
78         socket_id = rte_cryptodev_socket_id(dev_id);
79         rte_cryptodev_info_get(dev_id, &dev_info);
80
81         if (dev_info.max_nb_queue_pairs < params->n_queues)
82                 return NULL;
83         if (dev_info.feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED)
84                 return NULL;
85
86         dev_conf.socket_id = socket_id;
87         dev_conf.nb_queue_pairs = params->n_queues;
88
89         status = rte_cryptodev_configure(dev_id, &dev_conf);
90         if (status < 0)
91                 return NULL;
92
93         queue_conf.nb_descriptors = params->queue_size;
94         for (i = 0; i < params->n_queues; i++) {
95                 status = rte_cryptodev_queue_pair_setup(dev_id, i,
96                                 &queue_conf, socket_id, NULL);
97                 if (status < 0)
98                         return NULL;
99         }
100
101         if (rte_cryptodev_start(dev_id) < 0)
102                 return NULL;
103
104         cryptodev = calloc(1, sizeof(struct cryptodev));
105         if (cryptodev == NULL) {
106                 rte_cryptodev_stop(dev_id);
107                 return NULL;
108         }
109
110         strlcpy(cryptodev->name, name, sizeof(cryptodev->name));
111         cryptodev->dev_id = dev_id;
112         cryptodev->n_queues = params->n_queues;
113
114         TAILQ_INSERT_TAIL(&cryptodev_list, cryptodev, node);
115
116         return cryptodev;
117 }