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