vhost/crypto: add missing user protocol flag
[dpdk.git] / lib / librte_cryptodev / rte_cryptodev_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <rte_string_fns.h>
6 #include <rte_malloc.h>
7
8 #include "rte_cryptodev_pmd.h"
9
10 /**
11  * Parse name from argument
12  */
13 static int
14 rte_cryptodev_pmd_parse_name_arg(const char *key __rte_unused,
15                 const char *value, void *extra_args)
16 {
17         struct rte_cryptodev_pmd_init_params *params = extra_args;
18         int n;
19
20         n = strlcpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
21         if (n >= RTE_CRYPTODEV_NAME_MAX_LEN)
22                 return -EINVAL;
23
24         return 0;
25 }
26
27 /**
28  * Parse unsigned integer from argument
29  */
30 static int
31 rte_cryptodev_pmd_parse_uint_arg(const char *key __rte_unused,
32                 const char *value, void *extra_args)
33 {
34         int i;
35         char *end;
36         errno = 0;
37
38         i = strtol(value, &end, 10);
39         if (*end != 0 || errno != 0 || i < 0)
40                 return -EINVAL;
41
42         *((uint32_t *)extra_args) = i;
43         return 0;
44 }
45
46 int
47 rte_cryptodev_pmd_parse_input_args(
48                 struct rte_cryptodev_pmd_init_params *params,
49                 const char *args)
50 {
51         struct rte_kvargs *kvlist = NULL;
52         int ret = 0;
53
54         if (params == NULL)
55                 return -EINVAL;
56
57         if (args) {
58                 kvlist = rte_kvargs_parse(args, cryptodev_pmd_valid_params);
59                 if (kvlist == NULL)
60                         return -EINVAL;
61
62                 ret = rte_kvargs_process(kvlist,
63                                 RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
64                                 &rte_cryptodev_pmd_parse_uint_arg,
65                                 &params->max_nb_queue_pairs);
66                 if (ret < 0)
67                         goto free_kvlist;
68
69                 ret = rte_kvargs_process(kvlist,
70                                 RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
71                                 &rte_cryptodev_pmd_parse_uint_arg,
72                                 &params->socket_id);
73                 if (ret < 0)
74                         goto free_kvlist;
75
76                 ret = rte_kvargs_process(kvlist,
77                                 RTE_CRYPTODEV_PMD_NAME_ARG,
78                                 &rte_cryptodev_pmd_parse_name_arg,
79                                 params);
80                 if (ret < 0)
81                         goto free_kvlist;
82         }
83
84 free_kvlist:
85         rte_kvargs_free(kvlist);
86         return ret;
87 }
88
89 struct rte_cryptodev *
90 rte_cryptodev_pmd_create(const char *name,
91                 struct rte_device *device,
92                 struct rte_cryptodev_pmd_init_params *params)
93 {
94         struct rte_cryptodev *cryptodev;
95
96         if (params->name[0] != '\0') {
97                 CDEV_LOG_INFO("User specified device name = %s\n", params->name);
98                 name = params->name;
99         }
100
101         CDEV_LOG_INFO("Creating cryptodev %s\n", name);
102
103         CDEV_LOG_INFO("Initialisation parameters - name: %s,"
104                         "socket id: %d, max queue pairs: %u",
105                         name, params->socket_id, params->max_nb_queue_pairs);
106
107         /* allocate device structure */
108         cryptodev = rte_cryptodev_pmd_allocate(name, params->socket_id);
109         if (cryptodev == NULL) {
110                 CDEV_LOG_ERR("Failed to allocate crypto device for %s", name);
111                 return NULL;
112         }
113
114         /* allocate private device structure */
115         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
116                 cryptodev->data->dev_private =
117                                 rte_zmalloc_socket("cryptodev device private",
118                                                 params->private_data_size,
119                                                 RTE_CACHE_LINE_SIZE,
120                                                 params->socket_id);
121
122                 if (cryptodev->data->dev_private == NULL) {
123                         CDEV_LOG_ERR("Cannot allocate memory for cryptodev %s"
124                                         " private data", name);
125
126                         rte_cryptodev_pmd_release_device(cryptodev);
127                         return NULL;
128                 }
129         }
130
131         cryptodev->device = device;
132
133         /* initialise user call-back tail queue */
134         TAILQ_INIT(&(cryptodev->link_intr_cbs));
135
136         return cryptodev;
137 }
138
139 int
140 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev)
141 {
142         int retval;
143
144         CDEV_LOG_INFO("Closing crypto device %s", cryptodev->device->name);
145
146         /* free crypto device */
147         retval = rte_cryptodev_pmd_release_device(cryptodev);
148         if (retval)
149                 return retval;
150
151         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
152                 rte_free(cryptodev->data->dev_private);
153
154
155         cryptodev->device = NULL;
156         cryptodev->data = NULL;
157
158         return 0;
159 }