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