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