New upstream version 18.08
[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("[%s] User specified device name = %s\n",
97                                 device->driver->name, params->name);
98                 name = params->name;
99         }
100
101         CDEV_LOG_INFO("[%s] - Creating cryptodev %s\n",
102                         device->driver->name, name);
103
104         CDEV_LOG_INFO("[%s] - Initialisation parameters - name: %s,"
105                         "socket id: %d, max queue pairs: %u",
106                         device->driver->name, name,
107                         params->socket_id, params->max_nb_queue_pairs);
108
109         /* allocate device structure */
110         cryptodev = rte_cryptodev_pmd_allocate(name, params->socket_id);
111         if (cryptodev == NULL) {
112                 CDEV_LOG_ERR("[%s] Failed to allocate crypto device for %s",
113                                 device->driver->name, name);
114                 return NULL;
115         }
116
117         /* allocate private device structure */
118         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
119                 cryptodev->data->dev_private =
120                                 rte_zmalloc_socket("cryptodev device private",
121                                                 params->private_data_size,
122                                                 RTE_CACHE_LINE_SIZE,
123                                                 params->socket_id);
124
125                 if (cryptodev->data->dev_private == NULL) {
126                         CDEV_LOG_ERR("[%s] Cannot allocate memory for "
127                                         "cryptodev %s private data",
128                                         device->driver->name, name);
129
130                         rte_cryptodev_pmd_release_device(cryptodev);
131                         return NULL;
132                 }
133         }
134
135         cryptodev->device = device;
136
137         /* initialise user call-back tail queue */
138         TAILQ_INIT(&(cryptodev->link_intr_cbs));
139
140         return cryptodev;
141 }
142
143 int
144 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev)
145 {
146         int retval;
147
148         CDEV_LOG_INFO("[%s] Closing crypto device %s",
149                         cryptodev->device->driver->name,
150                         cryptodev->device->name);
151
152         /* free crypto device */
153         retval = rte_cryptodev_pmd_release_device(cryptodev);
154         if (retval)
155                 return retval;
156
157         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
158                 rte_free(cryptodev->data->dev_private);
159
160
161         cryptodev->device = NULL;
162         cryptodev->data = NULL;
163
164         return 0;
165 }