a57faadcf325ef39a586d709cc52f99153439b46
[deb_dpdk.git] / lib / librte_cryptodev / rte_cryptodev_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 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 the copyright holder 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 <rte_malloc.h>
34
35 #include "rte_cryptodev_vdev.h"
36 #include "rte_cryptodev_pci.h"
37 #include "rte_cryptodev_pmd.h"
38
39 /**
40  * Parse name from argument
41  */
42 static int
43 rte_cryptodev_vdev_parse_name_arg(const char *key __rte_unused,
44                 const char *value, void *extra_args)
45 {
46         struct rte_crypto_vdev_init_params *params = extra_args;
47
48         if (strlen(value) >= RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
49                 CDEV_LOG_ERR("Invalid name %s, should be less than "
50                                 "%u bytes", value,
51                                 RTE_CRYPTODEV_NAME_MAX_LEN - 1);
52                 return -1;
53         }
54
55         strncpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
56
57         return 0;
58 }
59
60 /**
61  * Parse integer from argument
62  */
63 static int
64 rte_cryptodev_vdev_parse_integer_arg(const char *key __rte_unused,
65                 const char *value, void *extra_args)
66 {
67         int *i = extra_args;
68
69         *i = atoi(value);
70         if (*i < 0) {
71                 CDEV_LOG_ERR("Argument has to be positive.");
72                 return -1;
73         }
74
75         return 0;
76 }
77
78 struct rte_cryptodev *
79 rte_cryptodev_vdev_pmd_init(const char *name, size_t dev_private_size,
80                 int socket_id, struct rte_vdev_device *vdev)
81 {
82         struct rte_cryptodev *cryptodev;
83
84         /* allocate device structure */
85         cryptodev = rte_cryptodev_pmd_allocate(name, socket_id);
86         if (cryptodev == NULL)
87                 return NULL;
88
89         /* allocate private device structure */
90         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
91                 cryptodev->data->dev_private =
92                                 rte_zmalloc_socket("cryptodev device private",
93                                                 dev_private_size,
94                                                 RTE_CACHE_LINE_SIZE,
95                                                 socket_id);
96
97                 if (cryptodev->data->dev_private == NULL)
98                         rte_panic("Cannot allocate memzone for private device"
99                                         " data");
100         }
101
102         cryptodev->device = &vdev->device;
103
104         /* initialise user call-back tail queue */
105         TAILQ_INIT(&(cryptodev->link_intr_cbs));
106
107         return cryptodev;
108 }
109
110 int
111 rte_cryptodev_vdev_parse_init_params(struct rte_crypto_vdev_init_params *params,
112                 const char *input_args)
113 {
114         struct rte_kvargs *kvlist = NULL;
115         int ret = 0;
116
117         if (params == NULL)
118                 return -EINVAL;
119
120         if (input_args) {
121                 kvlist = rte_kvargs_parse(input_args,
122                                 cryptodev_vdev_valid_params);
123                 if (kvlist == NULL)
124                         return -1;
125
126                 ret = rte_kvargs_process(kvlist,
127                                         RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
128                                         &rte_cryptodev_vdev_parse_integer_arg,
129                                         &params->max_nb_queue_pairs);
130                 if (ret < 0)
131                         goto free_kvlist;
132
133                 ret = rte_kvargs_process(kvlist,
134                                         RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
135                                         &rte_cryptodev_vdev_parse_integer_arg,
136                                         &params->max_nb_sessions);
137                 if (ret < 0)
138                         goto free_kvlist;
139
140                 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
141                                         &rte_cryptodev_vdev_parse_integer_arg,
142                                         &params->socket_id);
143                 if (ret < 0)
144                         goto free_kvlist;
145
146                 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_NAME,
147                                         &rte_cryptodev_vdev_parse_name_arg,
148                                         params);
149                 if (ret < 0)
150                         goto free_kvlist;
151         }
152
153 free_kvlist:
154         rte_kvargs_free(kvlist);
155         return ret;
156 }
157
158 int
159 rte_cryptodev_pci_generic_probe(struct rte_pci_device *pci_dev,
160                         size_t private_data_size,
161                         cryptodev_pci_init_t dev_init)
162 {
163         struct rte_cryptodev *cryptodev;
164
165         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
166
167         int retval;
168
169         rte_pci_device_name(&pci_dev->addr, cryptodev_name,
170                         sizeof(cryptodev_name));
171
172         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
173         if (cryptodev == NULL)
174                 return -ENOMEM;
175
176         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
177                 cryptodev->data->dev_private =
178                                 rte_zmalloc_socket(
179                                                 "cryptodev private structure",
180                                                 private_data_size,
181                                                 RTE_CACHE_LINE_SIZE,
182                                                 rte_socket_id());
183
184                 if (cryptodev->data->dev_private == NULL)
185                         rte_panic("Cannot allocate memzone for private "
186                                         "device data");
187         }
188
189         cryptodev->device = &pci_dev->device;
190
191         /* init user callbacks */
192         TAILQ_INIT(&(cryptodev->link_intr_cbs));
193
194         /* Invoke PMD device initialization function */
195         RTE_FUNC_PTR_OR_ERR_RET(*dev_init, -EINVAL);
196         retval = dev_init(cryptodev);
197         if (retval == 0)
198                 return 0;
199
200         CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
201                         " failed", pci_dev->device.driver->name,
202                         (unsigned int) pci_dev->id.vendor_id,
203                         (unsigned int) pci_dev->id.device_id);
204
205         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
206                 rte_free(cryptodev->data->dev_private);
207
208         /* free crypto device */
209         rte_cryptodev_pmd_release_device(cryptodev);
210
211         return -ENXIO;
212 }
213
214 int
215 rte_cryptodev_pci_generic_remove(struct rte_pci_device *pci_dev,
216                 cryptodev_pci_uninit_t dev_uninit)
217 {
218         struct rte_cryptodev *cryptodev;
219         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
220         int ret;
221
222         if (pci_dev == NULL)
223                 return -EINVAL;
224
225         rte_pci_device_name(&pci_dev->addr, cryptodev_name,
226                         sizeof(cryptodev_name));
227
228         cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
229         if (cryptodev == NULL)
230                 return -ENODEV;
231
232         /* Invoke PMD device uninit function */
233         if (dev_uninit) {
234                 ret = dev_uninit(cryptodev);
235                 if (ret)
236                         return ret;
237         }
238
239         /* free crypto device */
240         rte_cryptodev_pmd_release_device(cryptodev);
241
242         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
243                 rte_free(cryptodev->data->dev_private);
244
245         cryptodev->device = NULL;
246         cryptodev->data = NULL;
247
248         return 0;
249 }