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