New upstream version 18.08
[deb_dpdk.git] / lib / librte_compressdev / rte_compressdev_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017-2018 Intel Corporation
3  */
4
5 #include <rte_malloc.h>
6 #include <rte_kvargs.h>
7 #include <rte_eal.h>
8
9 #include "rte_compressdev_internal.h"
10 #include "rte_compressdev_pmd.h"
11
12 int compressdev_logtype;
13
14 /**
15  * Parse name from argument
16  */
17 static int
18 rte_compressdev_pmd_parse_name_arg(const char *key __rte_unused,
19                 const char *value, void *extra_args)
20 {
21         struct rte_compressdev_pmd_init_params *params = extra_args;
22         int n;
23
24         n = snprintf(params->name, RTE_COMPRESSDEV_NAME_MAX_LEN, "%s", value);
25         if (n >= RTE_COMPRESSDEV_NAME_MAX_LEN)
26                 return -EINVAL;
27
28         return 0;
29 }
30
31 /**
32  * Parse unsigned integer from argument
33  */
34 static int
35 rte_compressdev_pmd_parse_uint_arg(const char *key __rte_unused,
36                 const char *value, void *extra_args)
37 {
38         int i;
39         char *end;
40
41         errno = 0;
42         i = strtol(value, &end, 10);
43         if (*end != 0 || errno != 0 || i < 0)
44                 return -EINVAL;
45
46         *((uint32_t *)extra_args) = i;
47         return 0;
48 }
49
50 int __rte_experimental
51 rte_compressdev_pmd_parse_input_args(
52                 struct rte_compressdev_pmd_init_params *params,
53                 const char *args)
54 {
55         struct rte_kvargs *kvlist = NULL;
56         int ret = 0;
57
58         if (params == NULL)
59                 return -EINVAL;
60
61         if (args) {
62                 kvlist = rte_kvargs_parse(args, compressdev_pmd_valid_params);
63                 if (kvlist == NULL)
64                         return -EINVAL;
65
66                 ret = rte_kvargs_process(kvlist,
67                                 RTE_COMPRESSDEV_PMD_SOCKET_ID_ARG,
68                                 &rte_compressdev_pmd_parse_uint_arg,
69                                 &params->socket_id);
70                 if (ret < 0)
71                         goto free_kvlist;
72
73                 ret = rte_kvargs_process(kvlist,
74                                 RTE_COMPRESSDEV_PMD_NAME_ARG,
75                                 &rte_compressdev_pmd_parse_name_arg,
76                                 params);
77                 if (ret < 0)
78                         goto free_kvlist;
79         }
80
81 free_kvlist:
82         rte_kvargs_free(kvlist);
83         return ret;
84 }
85
86 struct rte_compressdev * __rte_experimental
87 rte_compressdev_pmd_create(const char *name,
88                 struct rte_device *device,
89                 size_t private_data_size,
90                 struct rte_compressdev_pmd_init_params *params)
91 {
92         struct rte_compressdev *compressdev;
93
94         if (params->name[0] != '\0') {
95                 COMPRESSDEV_LOG(INFO, "[%s] User specified device name = %s\n",
96                                 device->driver->name, params->name);
97                 name = params->name;
98         }
99
100         COMPRESSDEV_LOG(INFO, "[%s] - Creating compressdev %s\n",
101                         device->driver->name, name);
102
103         COMPRESSDEV_LOG(INFO,
104         "[%s] - Init parameters - name: %s, socket id: %d",
105                         device->driver->name, name,
106                         params->socket_id);
107
108         /* allocate device structure */
109         compressdev = rte_compressdev_pmd_allocate(name, params->socket_id);
110         if (compressdev == NULL) {
111                 COMPRESSDEV_LOG(ERR, "[%s] Failed to allocate comp device %s",
112                                 device->driver->name, name);
113                 return NULL;
114         }
115
116         /* allocate private device structure */
117         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
118                 compressdev->data->dev_private =
119                                 rte_zmalloc_socket("compressdev device private",
120                                                 private_data_size,
121                                                 RTE_CACHE_LINE_SIZE,
122                                                 params->socket_id);
123
124                 if (compressdev->data->dev_private == NULL) {
125                         COMPRESSDEV_LOG(ERR,
126                 "[%s] Cannot allocate memory for compressdev %s private data",
127                                         device->driver->name, name);
128
129                         rte_compressdev_pmd_release_device(compressdev);
130                         return NULL;
131                 }
132         }
133
134         compressdev->device = device;
135
136         return compressdev;
137 }
138
139 int __rte_experimental
140 rte_compressdev_pmd_destroy(struct rte_compressdev *compressdev)
141 {
142         int retval;
143
144         COMPRESSDEV_LOG(INFO, "[%s] Closing comp device %s",
145                         compressdev->device->driver->name,
146                         compressdev->device->name);
147
148         /* free comp device */
149         retval = rte_compressdev_pmd_release_device(compressdev);
150         if (retval)
151                 return retval;
152
153         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
154                 rte_free(compressdev->data->dev_private);
155
156         compressdev->device = NULL;
157         compressdev->data = NULL;
158
159         return 0;
160 }