net/cnxk: support device infos query
[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 #define PIPELINE_CRYPTO_SESSION_CACHE_SIZE      128
15
16 static struct cryptodev_list cryptodev_list;
17
18 int
19 cryptodev_init(void)
20 {
21         TAILQ_INIT(&cryptodev_list);
22
23         return 0;
24 }
25
26 struct cryptodev *
27 cryptodev_find(const char *name)
28 {
29         struct cryptodev *cryptodev;
30
31         if (name == NULL)
32                 return NULL;
33
34         TAILQ_FOREACH(cryptodev, &cryptodev_list, node)
35                 if (strcmp(cryptodev->name, name) == 0)
36                         return cryptodev;
37
38         return NULL;
39 }
40
41 struct cryptodev *
42 cryptodev_next(struct cryptodev *cryptodev)
43 {
44         return (cryptodev == NULL) ?
45                         TAILQ_FIRST(&cryptodev_list) :
46                         TAILQ_NEXT(cryptodev, node);
47 }
48
49 struct cryptodev *
50 cryptodev_create(const char *name, struct cryptodev_params *params)
51 {
52         struct rte_cryptodev_info dev_info;
53         struct rte_cryptodev_config dev_conf;
54         struct rte_cryptodev_qp_conf queue_conf;
55         struct cryptodev *cryptodev;
56         uint32_t dev_id, i;
57         uint32_t socket_id;
58         uint32_t cache_size;
59         char mp_name[NAME_SIZE];
60         int status;
61
62         /* Check input params */
63         if ((name == NULL) ||
64                 cryptodev_find(name) ||
65                 (params->n_queues == 0) ||
66                 (params->queue_size == 0) ||
67                 (params->session_pool_size == 0))
68                 return NULL;
69
70         if (params->dev_name) {
71                 status = rte_cryptodev_get_dev_id(params->dev_name);
72                 if (status == -1)
73                         return NULL;
74
75                 dev_id = (uint32_t)status;
76         } else {
77                 if (rte_cryptodev_pmd_is_valid_dev(params->dev_id) == 0)
78                         return NULL;
79
80                 dev_id = params->dev_id;
81         }
82
83         cache_size = (params->session_pool_size / 2 <
84                         PIPELINE_CRYPTO_SESSION_CACHE_SIZE) ?
85                                         (params->session_pool_size / 2) :
86                                         PIPELINE_CRYPTO_SESSION_CACHE_SIZE;
87
88         socket_id = rte_cryptodev_socket_id(dev_id);
89         rte_cryptodev_info_get(dev_id, &dev_info);
90
91         if (dev_info.max_nb_queue_pairs < params->n_queues)
92                 return NULL;
93
94         dev_conf.socket_id = socket_id;
95         dev_conf.nb_queue_pairs = params->n_queues;
96         dev_conf.ff_disable = 0;
97
98         status = rte_cryptodev_configure(dev_id, &dev_conf);
99         if (status < 0)
100                 return NULL;
101
102         queue_conf.nb_descriptors = params->queue_size;
103         for (i = 0; i < params->n_queues; i++) {
104                 status = rte_cryptodev_queue_pair_setup(dev_id, i,
105                                 &queue_conf, socket_id);
106                 if (status < 0)
107                         return NULL;
108         }
109
110         if (rte_cryptodev_start(dev_id) < 0)
111                 return NULL;
112
113         cryptodev = calloc(1, sizeof(struct cryptodev));
114         if (cryptodev == NULL) {
115                 rte_cryptodev_stop(dev_id);
116                 return NULL;
117         }
118
119         strlcpy(cryptodev->name, name, sizeof(cryptodev->name));
120         cryptodev->dev_id = dev_id;
121         cryptodev->n_queues = params->n_queues;
122
123         snprintf(mp_name, NAME_SIZE, "%s_mp%u", name, dev_id);
124         cryptodev->mp_create = rte_cryptodev_sym_session_pool_create(
125                         mp_name,
126                         params->session_pool_size,
127                         0,
128                         cache_size,
129                         0,
130                         socket_id);
131         if (!cryptodev->mp_create)
132                 goto error_exit;
133
134         snprintf(mp_name, NAME_SIZE, "%s_mp_priv%u", name, dev_id);
135         cryptodev->mp_init = rte_mempool_create(
136                         NULL,
137                         params->session_pool_size,
138                         rte_cryptodev_sym_get_private_session_size(dev_id),
139                         cache_size,
140                         0,
141                         NULL,
142                         NULL,
143                         NULL,
144                         NULL,
145                         socket_id,
146                         0);
147         if (!cryptodev->mp_init)
148                 goto error_exit;
149
150         TAILQ_INSERT_TAIL(&cryptodev_list, cryptodev, node);
151
152         return cryptodev;
153
154 error_exit:
155         if (cryptodev->mp_create)
156                 rte_mempool_free(cryptodev->mp_create);
157         if (cryptodev->mp_init)
158                 rte_mempool_free(cryptodev->mp_init);
159
160         free(cryptodev);
161
162         return NULL;
163 }