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