4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <rte_malloc.h>
35 #include "rte_cryptodev_vdev.h"
36 #include "rte_cryptodev_pci.h"
37 #include "rte_cryptodev_pmd.h"
40 * Parse name from argument
43 rte_cryptodev_pmd_parse_name_arg(const char *key __rte_unused,
44 const char *value, void *extra_args)
46 struct rte_cryptodev_pmd_init_params *params = extra_args;
49 n = snprintf(params->name, RTE_CRYPTODEV_NAME_MAX_LEN, "%s", value);
50 if (n >= RTE_CRYPTODEV_NAME_MAX_LEN)
57 * Parse unsigned integer from argument
60 rte_cryptodev_pmd_parse_uint_arg(const char *key __rte_unused,
61 const char *value, void *extra_args)
67 i = strtol(value, &end, 10);
68 if (*end != 0 || errno != 0 || i < 0)
71 *((uint32_t *)extra_args) = i;
76 rte_cryptodev_pmd_parse_input_args(
77 struct rte_cryptodev_pmd_init_params *params,
80 struct rte_kvargs *kvlist = NULL;
87 kvlist = rte_kvargs_parse(args, cryptodev_pmd_valid_params);
91 ret = rte_kvargs_process(kvlist,
92 RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
93 &rte_cryptodev_pmd_parse_uint_arg,
94 ¶ms->max_nb_queue_pairs);
98 ret = rte_kvargs_process(kvlist,
99 RTE_CRYPTODEV_PMD_MAX_NB_SESS_ARG,
100 &rte_cryptodev_pmd_parse_uint_arg,
101 ¶ms->max_nb_sessions);
105 ret = rte_kvargs_process(kvlist,
106 RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
107 &rte_cryptodev_pmd_parse_uint_arg,
112 ret = rte_kvargs_process(kvlist,
113 RTE_CRYPTODEV_PMD_NAME_ARG,
114 &rte_cryptodev_pmd_parse_name_arg,
121 rte_kvargs_free(kvlist);
125 struct rte_cryptodev *
126 rte_cryptodev_pmd_create(const char *name,
127 struct rte_device *device,
128 struct rte_cryptodev_pmd_init_params *params)
130 struct rte_cryptodev *cryptodev;
132 if (params->name[0] != '\0') {
133 CDEV_LOG_INFO("[%s] User specified device name = %s\n",
134 device->driver->name, params->name);
138 CDEV_LOG_INFO("[%s] - Creating cryptodev %s\n",
139 device->driver->name, name);
141 CDEV_LOG_INFO("[%s] - Initialisation parameters - name: %s,"
142 "socket id: %d, max queue pairs: %u, max sessions: %u",
143 device->driver->name, name,
144 params->socket_id, params->max_nb_queue_pairs,
145 params->max_nb_sessions);
147 /* allocate device structure */
148 cryptodev = rte_cryptodev_pmd_allocate(name, params->socket_id);
149 if (cryptodev == NULL) {
150 CDEV_LOG_ERR("[%s] Failed to allocate crypto device for %s",
151 device->driver->name, name);
155 /* allocate private device structure */
156 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
157 cryptodev->data->dev_private =
158 rte_zmalloc_socket("cryptodev device private",
159 params->private_data_size,
163 if (cryptodev->data->dev_private == NULL) {
164 CDEV_LOG_ERR("[%s] Cannot allocate memory for "
165 "cryptodev %s private data",
166 device->driver->name, name);
168 rte_cryptodev_pmd_release_device(cryptodev);
173 cryptodev->device = device;
175 /* initialise user call-back tail queue */
176 TAILQ_INIT(&(cryptodev->link_intr_cbs));
182 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev)
186 CDEV_LOG_INFO("[%s] Closing crypto device %s",
187 cryptodev->device->driver->name,
188 cryptodev->device->name);
190 /* free crypto device */
191 retval = rte_cryptodev_pmd_release_device(cryptodev);
195 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
196 rte_free(cryptodev->data->dev_private);
199 cryptodev->device = NULL;
200 cryptodev->data = NULL;
206 * Parse name from argument
209 rte_cryptodev_vdev_parse_name_arg(const char *key __rte_unused,
210 const char *value, void *extra_args)
212 struct rte_crypto_vdev_init_params *params = extra_args;
214 if (strlen(value) >= RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
215 CDEV_LOG_ERR("Invalid name %s, should be less than "
217 RTE_CRYPTODEV_NAME_MAX_LEN - 1);
221 strncpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
227 * Parse integer from argument
230 rte_cryptodev_vdev_parse_integer_arg(const char *key __rte_unused,
231 const char *value, void *extra_args)
237 CDEV_LOG_ERR("Argument has to be positive.");
244 struct rte_cryptodev *
245 rte_cryptodev_vdev_pmd_init(const char *name, size_t dev_private_size,
246 int socket_id, struct rte_vdev_device *vdev)
248 struct rte_cryptodev *cryptodev;
250 /* allocate device structure */
251 cryptodev = rte_cryptodev_pmd_allocate(name, socket_id);
252 if (cryptodev == NULL)
255 /* allocate private device structure */
256 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
257 cryptodev->data->dev_private =
258 rte_zmalloc_socket("cryptodev device private",
263 if (cryptodev->data->dev_private == NULL)
264 rte_panic("Cannot allocate memzone for private device"
268 cryptodev->device = &vdev->device;
274 rte_cryptodev_vdev_parse_init_params(struct rte_crypto_vdev_init_params *params,
275 const char *input_args)
277 struct rte_kvargs *kvlist = NULL;
284 kvlist = rte_kvargs_parse(input_args,
285 cryptodev_vdev_valid_params);
289 ret = rte_kvargs_process(kvlist,
290 RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
291 &rte_cryptodev_vdev_parse_integer_arg,
292 ¶ms->max_nb_queue_pairs);
296 ret = rte_kvargs_process(kvlist,
297 RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
298 &rte_cryptodev_vdev_parse_integer_arg,
299 ¶ms->max_nb_sessions);
303 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
304 &rte_cryptodev_vdev_parse_integer_arg,
309 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_NAME,
310 &rte_cryptodev_vdev_parse_name_arg,
317 rte_kvargs_free(kvlist);
322 rte_cryptodev_pci_generic_probe(struct rte_pci_device *pci_dev,
323 size_t private_data_size,
324 cryptodev_pci_init_t dev_init)
326 struct rte_cryptodev *cryptodev;
328 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
332 rte_pci_device_name(&pci_dev->addr, cryptodev_name,
333 sizeof(cryptodev_name));
335 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
336 if (cryptodev == NULL)
339 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
340 cryptodev->data->dev_private =
342 "cryptodev private structure",
347 if (cryptodev->data->dev_private == NULL)
348 rte_panic("Cannot allocate memzone for private "
352 cryptodev->device = &pci_dev->device;
354 /* Invoke PMD device initialization function */
355 RTE_FUNC_PTR_OR_ERR_RET(*dev_init, -EINVAL);
356 retval = dev_init(cryptodev);
360 CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
361 " failed", pci_dev->device.driver->name,
362 (unsigned int) pci_dev->id.vendor_id,
363 (unsigned int) pci_dev->id.device_id);
365 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
366 rte_free(cryptodev->data->dev_private);
368 /* free crypto device */
369 rte_cryptodev_pmd_release_device(cryptodev);
375 rte_cryptodev_pci_generic_remove(struct rte_pci_device *pci_dev,
376 cryptodev_pci_uninit_t dev_uninit)
378 struct rte_cryptodev *cryptodev;
379 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
385 rte_pci_device_name(&pci_dev->addr, cryptodev_name,
386 sizeof(cryptodev_name));
388 cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
389 if (cryptodev == NULL)
392 /* Invoke PMD device uninit function */
394 ret = dev_uninit(cryptodev);
399 /* free crypto device */
400 rte_cryptodev_pmd_release_device(cryptodev);
402 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
403 rte_free(cryptodev->data->dev_private);
405 cryptodev->device = NULL;
406 cryptodev->data = NULL;