crypto/qat: fix HMAC supported digest sizes
[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         return cryptodev;
105 }
106
107 int
108 rte_cryptodev_vdev_parse_init_params(struct rte_crypto_vdev_init_params *params,
109                 const char *input_args)
110 {
111         struct rte_kvargs *kvlist = NULL;
112         int ret = 0;
113
114         if (params == NULL)
115                 return -EINVAL;
116
117         if (input_args) {
118                 kvlist = rte_kvargs_parse(input_args,
119                                 cryptodev_vdev_valid_params);
120                 if (kvlist == NULL)
121                         return -1;
122
123                 ret = rte_kvargs_process(kvlist,
124                                         RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
125                                         &rte_cryptodev_vdev_parse_integer_arg,
126                                         &params->max_nb_queue_pairs);
127                 if (ret < 0)
128                         goto free_kvlist;
129
130                 ret = rte_kvargs_process(kvlist,
131                                         RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
132                                         &rte_cryptodev_vdev_parse_integer_arg,
133                                         &params->max_nb_sessions);
134                 if (ret < 0)
135                         goto free_kvlist;
136
137                 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
138                                         &rte_cryptodev_vdev_parse_integer_arg,
139                                         &params->socket_id);
140                 if (ret < 0)
141                         goto free_kvlist;
142
143                 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_NAME,
144                                         &rte_cryptodev_vdev_parse_name_arg,
145                                         params);
146                 if (ret < 0)
147                         goto free_kvlist;
148         }
149
150 free_kvlist:
151         rte_kvargs_free(kvlist);
152         return ret;
153 }
154
155 int
156 rte_cryptodev_pci_generic_probe(struct rte_pci_device *pci_dev,
157                         size_t private_data_size,
158                         cryptodev_pci_init_t dev_init)
159 {
160         struct rte_cryptodev *cryptodev;
161
162         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
163
164         int retval;
165
166         rte_pci_device_name(&pci_dev->addr, cryptodev_name,
167                         sizeof(cryptodev_name));
168
169         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
170         if (cryptodev == NULL)
171                 return -ENOMEM;
172
173         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
174                 cryptodev->data->dev_private =
175                                 rte_zmalloc_socket(
176                                                 "cryptodev private structure",
177                                                 private_data_size,
178                                                 RTE_CACHE_LINE_SIZE,
179                                                 rte_socket_id());
180
181                 if (cryptodev->data->dev_private == NULL)
182                         rte_panic("Cannot allocate memzone for private "
183                                         "device data");
184         }
185
186         cryptodev->device = &pci_dev->device;
187
188         /* Invoke PMD device initialization function */
189         RTE_FUNC_PTR_OR_ERR_RET(*dev_init, -EINVAL);
190         retval = dev_init(cryptodev);
191         if (retval == 0)
192                 return 0;
193
194         CDEV_LOG_ERR("driver %s: crypto_dev_init(vendor_id=0x%x device_id=0x%x)"
195                         " failed", pci_dev->device.driver->name,
196                         (unsigned int) pci_dev->id.vendor_id,
197                         (unsigned int) pci_dev->id.device_id);
198
199         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
200                 rte_free(cryptodev->data->dev_private);
201
202         /* free crypto device */
203         rte_cryptodev_pmd_release_device(cryptodev);
204
205         return -ENXIO;
206 }
207
208 int
209 rte_cryptodev_pci_generic_remove(struct rte_pci_device *pci_dev,
210                 cryptodev_pci_uninit_t dev_uninit)
211 {
212         struct rte_cryptodev *cryptodev;
213         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
214         int ret;
215
216         if (pci_dev == NULL)
217                 return -EINVAL;
218
219         rte_pci_device_name(&pci_dev->addr, cryptodev_name,
220                         sizeof(cryptodev_name));
221
222         cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
223         if (cryptodev == NULL)
224                 return -ENODEV;
225
226         /* Invoke PMD device uninit function */
227         if (dev_uninit) {
228                 ret = dev_uninit(cryptodev);
229                 if (ret)
230                         return ret;
231         }
232
233         /* free crypto device */
234         rte_cryptodev_pmd_release_device(cryptodev);
235
236         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
237                 rte_free(cryptodev->data->dev_private);
238
239         cryptodev->device = NULL;
240         cryptodev->data = NULL;
241
242         return 0;
243 }