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