cryptodev: move vdev functions to a separate file
[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_pmd.h"
37
38 /**
39  * Parse name from argument
40  */
41 static int
42 rte_cryptodev_vdev_parse_name_arg(const char *key __rte_unused,
43                 const char *value, void *extra_args)
44 {
45         struct rte_crypto_vdev_init_params *params = extra_args;
46
47         if (strlen(value) >= RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
48                 CDEV_LOG_ERR("Invalid name %s, should be less than "
49                                 "%u bytes", value,
50                                 RTE_CRYPTODEV_NAME_MAX_LEN - 1);
51                 return -1;
52         }
53
54         strncpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
55
56         return 0;
57 }
58
59 /**
60  * Parse integer from argument
61  */
62 static int
63 rte_cryptodev_vdev_parse_integer_arg(const char *key __rte_unused,
64                 const char *value, void *extra_args)
65 {
66         int *i = extra_args;
67
68         *i = atoi(value);
69         if (*i < 0) {
70                 CDEV_LOG_ERR("Argument has to be positive.");
71                 return -1;
72         }
73
74         return 0;
75 }
76
77 struct rte_cryptodev *
78 rte_cryptodev_vdev_pmd_init(const char *name, size_t dev_private_size,
79                 int socket_id, struct rte_vdev_device *vdev)
80 {
81         struct rte_cryptodev *cryptodev;
82
83         /* allocate device structure */
84         cryptodev = rte_cryptodev_pmd_allocate(name, socket_id);
85         if (cryptodev == NULL)
86                 return NULL;
87
88         /* allocate private device structure */
89         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
90                 cryptodev->data->dev_private =
91                                 rte_zmalloc_socket("cryptodev device private",
92                                                 dev_private_size,
93                                                 RTE_CACHE_LINE_SIZE,
94                                                 socket_id);
95
96                 if (cryptodev->data->dev_private == NULL)
97                         rte_panic("Cannot allocate memzone for private device"
98                                         " data");
99         }
100
101         cryptodev->device = &vdev->device;
102
103         /* initialise user call-back tail queue */
104         TAILQ_INIT(&(cryptodev->link_intr_cbs));
105
106         return cryptodev;
107 }
108
109 int
110 rte_cryptodev_vdev_parse_init_params(struct rte_crypto_vdev_init_params *params,
111                 const char *input_args)
112 {
113         struct rte_kvargs *kvlist = NULL;
114         int ret = 0;
115
116         if (params == NULL)
117                 return -EINVAL;
118
119         if (input_args) {
120                 kvlist = rte_kvargs_parse(input_args,
121                                 cryptodev_vdev_valid_params);
122                 if (kvlist == NULL)
123                         return -1;
124
125                 ret = rte_kvargs_process(kvlist,
126                                         RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
127                                         &rte_cryptodev_vdev_parse_integer_arg,
128                                         &params->max_nb_queue_pairs);
129                 if (ret < 0)
130                         goto free_kvlist;
131
132                 ret = rte_kvargs_process(kvlist,
133                                         RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
134                                         &rte_cryptodev_vdev_parse_integer_arg,
135                                         &params->max_nb_sessions);
136                 if (ret < 0)
137                         goto free_kvlist;
138
139                 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
140                                         &rte_cryptodev_vdev_parse_integer_arg,
141                                         &params->socket_id);
142                 if (ret < 0)
143                         goto free_kvlist;
144
145                 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_NAME,
146                                         &rte_cryptodev_vdev_parse_name_arg,
147                                         params);
148                 if (ret < 0)
149                         goto free_kvlist;
150         }
151
152 free_kvlist:
153         rte_kvargs_free(kvlist);
154         return ret;
155 }