New upstream version 18.11-rc1
[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, "User specified device name = %s\n",
96                                 params->name);
97                 name = params->name;
98         }
99
100         COMPRESSDEV_LOG(INFO, "Creating compressdev %s\n", name);
101
102         COMPRESSDEV_LOG(INFO, "Init parameters - name: %s, socket id: %d",
103                         name, params->socket_id);
104
105         /* allocate device structure */
106         compressdev = rte_compressdev_pmd_allocate(name, params->socket_id);
107         if (compressdev == NULL) {
108                 COMPRESSDEV_LOG(ERR, "Failed to allocate comp device %s", name);
109                 return NULL;
110         }
111
112         /* allocate private device structure */
113         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
114                 compressdev->data->dev_private =
115                                 rte_zmalloc_socket("compressdev device private",
116                                                 private_data_size,
117                                                 RTE_CACHE_LINE_SIZE,
118                                                 params->socket_id);
119
120                 if (compressdev->data->dev_private == NULL) {
121                         COMPRESSDEV_LOG(ERR,
122                                         "Cannot allocate memory for compressdev"
123                                         " %s private data", name);
124
125                         rte_compressdev_pmd_release_device(compressdev);
126                         return NULL;
127                 }
128         }
129
130         compressdev->device = device;
131
132         return compressdev;
133 }
134
135 int __rte_experimental
136 rte_compressdev_pmd_destroy(struct rte_compressdev *compressdev)
137 {
138         int retval;
139
140         COMPRESSDEV_LOG(INFO, "Closing comp device %s",
141                         compressdev->device->name);
142
143         /* free comp device */
144         retval = rte_compressdev_pmd_release_device(compressdev);
145         if (retval)
146                 return retval;
147
148         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
149                 rte_free(compressdev->data->dev_private);
150
151         compressdev->device = NULL;
152         compressdev->data = NULL;
153
154         return 0;
155 }