xref: /dpdk/lib/compressdev/rte_compressdev_pmd.c (revision ae282b0611c33aa73a01ee6137d116155053b835)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2017-2018 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
572b452c5SDmitry Kozlyuk #include <stdlib.h>
672b452c5SDmitry Kozlyuk 
799a2dd95SBruce Richardson #include <rte_string_fns.h>
899a2dd95SBruce Richardson #include <rte_malloc.h>
999a2dd95SBruce Richardson #include <rte_kvargs.h>
101acb7f54SDavid Marchand #include <dev_driver.h>
1199a2dd95SBruce Richardson #include <rte_eal.h>
1299a2dd95SBruce Richardson 
1399a2dd95SBruce Richardson #include "rte_compressdev_internal.h"
1499a2dd95SBruce Richardson #include "rte_compressdev_pmd.h"
1599a2dd95SBruce Richardson 
1699a2dd95SBruce Richardson /**
1799a2dd95SBruce Richardson  * Parse name from argument
1899a2dd95SBruce Richardson  */
1999a2dd95SBruce Richardson static int
rte_compressdev_pmd_parse_name_arg(const char * key __rte_unused,const char * value,void * extra_args)2099a2dd95SBruce Richardson rte_compressdev_pmd_parse_name_arg(const char *key __rte_unused,
2199a2dd95SBruce Richardson 		const char *value, void *extra_args)
2299a2dd95SBruce Richardson {
2399a2dd95SBruce Richardson 	struct rte_compressdev_pmd_init_params *params = extra_args;
2499a2dd95SBruce Richardson 	int n;
2599a2dd95SBruce Richardson 
26bb271824SChengwen Feng 	if (value == NULL || extra_args == NULL)
27bb271824SChengwen Feng 		return -EINVAL;
28bb271824SChengwen Feng 
2999a2dd95SBruce Richardson 	n = strlcpy(params->name, value, RTE_COMPRESSDEV_NAME_MAX_LEN);
3099a2dd95SBruce Richardson 	if (n >= RTE_COMPRESSDEV_NAME_MAX_LEN)
3199a2dd95SBruce Richardson 		return -EINVAL;
3299a2dd95SBruce Richardson 
3399a2dd95SBruce Richardson 	return 0;
3499a2dd95SBruce Richardson }
3599a2dd95SBruce Richardson 
3699a2dd95SBruce Richardson /**
3799a2dd95SBruce Richardson  * Parse unsigned integer from argument
3899a2dd95SBruce Richardson  */
3999a2dd95SBruce Richardson static int
rte_compressdev_pmd_parse_uint_arg(const char * key __rte_unused,const char * value,void * extra_args)4099a2dd95SBruce Richardson rte_compressdev_pmd_parse_uint_arg(const char *key __rte_unused,
4199a2dd95SBruce Richardson 		const char *value, void *extra_args)
4299a2dd95SBruce Richardson {
4399a2dd95SBruce Richardson 	int i;
4499a2dd95SBruce Richardson 	char *end;
4599a2dd95SBruce Richardson 
46bb271824SChengwen Feng 	if (value == NULL || extra_args == NULL)
47bb271824SChengwen Feng 		return -EINVAL;
48bb271824SChengwen Feng 
4999a2dd95SBruce Richardson 	errno = 0;
5099a2dd95SBruce Richardson 	i = strtol(value, &end, 10);
5199a2dd95SBruce Richardson 	if (*end != 0 || errno != 0 || i < 0)
5299a2dd95SBruce Richardson 		return -EINVAL;
5399a2dd95SBruce Richardson 
5499a2dd95SBruce Richardson 	*((uint32_t *)extra_args) = i;
5599a2dd95SBruce Richardson 	return 0;
5699a2dd95SBruce Richardson }
5799a2dd95SBruce Richardson 
5899a2dd95SBruce Richardson int
rte_compressdev_pmd_parse_input_args(struct rte_compressdev_pmd_init_params * params,const char * args)5999a2dd95SBruce Richardson rte_compressdev_pmd_parse_input_args(
6099a2dd95SBruce Richardson 		struct rte_compressdev_pmd_init_params *params,
6199a2dd95SBruce Richardson 		const char *args)
6299a2dd95SBruce Richardson {
6399a2dd95SBruce Richardson 	struct rte_kvargs *kvlist = NULL;
6499a2dd95SBruce Richardson 	int ret = 0;
6599a2dd95SBruce Richardson 
6699a2dd95SBruce Richardson 	if (params == NULL)
6799a2dd95SBruce Richardson 		return -EINVAL;
6899a2dd95SBruce Richardson 
6999a2dd95SBruce Richardson 	if (args) {
7099a2dd95SBruce Richardson 		kvlist = rte_kvargs_parse(args,	compressdev_pmd_valid_params);
7199a2dd95SBruce Richardson 		if (kvlist == NULL)
7299a2dd95SBruce Richardson 			return -EINVAL;
7399a2dd95SBruce Richardson 
7499a2dd95SBruce Richardson 		ret = rte_kvargs_process(kvlist,
7599a2dd95SBruce Richardson 				RTE_COMPRESSDEV_PMD_SOCKET_ID_ARG,
7699a2dd95SBruce Richardson 				&rte_compressdev_pmd_parse_uint_arg,
7799a2dd95SBruce Richardson 				&params->socket_id);
7899a2dd95SBruce Richardson 		if (ret < 0)
7999a2dd95SBruce Richardson 			goto free_kvlist;
8099a2dd95SBruce Richardson 
8199a2dd95SBruce Richardson 		ret = rte_kvargs_process(kvlist,
8299a2dd95SBruce Richardson 				RTE_COMPRESSDEV_PMD_NAME_ARG,
8399a2dd95SBruce Richardson 				&rte_compressdev_pmd_parse_name_arg,
8499a2dd95SBruce Richardson 				params);
8599a2dd95SBruce Richardson 		if (ret < 0)
8699a2dd95SBruce Richardson 			goto free_kvlist;
8799a2dd95SBruce Richardson 	}
8899a2dd95SBruce Richardson 
8999a2dd95SBruce Richardson free_kvlist:
9099a2dd95SBruce Richardson 	rte_kvargs_free(kvlist);
9199a2dd95SBruce Richardson 	return ret;
9299a2dd95SBruce Richardson }
9399a2dd95SBruce Richardson 
9499a2dd95SBruce 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)9599a2dd95SBruce Richardson rte_compressdev_pmd_create(const char *name,
9699a2dd95SBruce Richardson 		struct rte_device *device,
9799a2dd95SBruce Richardson 		size_t private_data_size,
9899a2dd95SBruce Richardson 		struct rte_compressdev_pmd_init_params *params)
9999a2dd95SBruce Richardson {
10099a2dd95SBruce Richardson 	struct rte_compressdev *compressdev;
10199a2dd95SBruce Richardson 
10299a2dd95SBruce Richardson 	if (params->name[0] != '\0') {
103*ae282b06SDavid Marchand 		COMPRESSDEV_LOG(INFO, "User specified device name = %s",
10499a2dd95SBruce Richardson 				params->name);
10599a2dd95SBruce Richardson 		name = params->name;
10699a2dd95SBruce Richardson 	}
10799a2dd95SBruce Richardson 
108*ae282b06SDavid Marchand 	COMPRESSDEV_LOG(INFO, "Creating compressdev %s", name);
10999a2dd95SBruce Richardson 
11099a2dd95SBruce Richardson 	COMPRESSDEV_LOG(INFO, "Init parameters - name: %s, socket id: %d",
11199a2dd95SBruce Richardson 			name, params->socket_id);
11299a2dd95SBruce Richardson 
11399a2dd95SBruce Richardson 	/* allocate device structure */
11499a2dd95SBruce Richardson 	compressdev = rte_compressdev_pmd_allocate(name, params->socket_id);
11599a2dd95SBruce Richardson 	if (compressdev == NULL) {
11699a2dd95SBruce Richardson 		COMPRESSDEV_LOG(ERR, "Failed to allocate comp device %s", name);
11799a2dd95SBruce Richardson 		return NULL;
11899a2dd95SBruce Richardson 	}
11999a2dd95SBruce Richardson 
12099a2dd95SBruce Richardson 	/* allocate private device structure */
12199a2dd95SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
12299a2dd95SBruce Richardson 		compressdev->data->dev_private =
12399a2dd95SBruce Richardson 				rte_zmalloc_socket("compressdev device private",
12499a2dd95SBruce Richardson 						private_data_size,
12599a2dd95SBruce Richardson 						RTE_CACHE_LINE_SIZE,
12699a2dd95SBruce Richardson 						params->socket_id);
12799a2dd95SBruce Richardson 
12899a2dd95SBruce Richardson 		if (compressdev->data->dev_private == NULL) {
12999a2dd95SBruce Richardson 			COMPRESSDEV_LOG(ERR,
13099a2dd95SBruce Richardson 					"Cannot allocate memory for compressdev"
13199a2dd95SBruce Richardson 					" %s private data", name);
13299a2dd95SBruce Richardson 
13399a2dd95SBruce Richardson 			rte_compressdev_pmd_release_device(compressdev);
13499a2dd95SBruce Richardson 			return NULL;
13599a2dd95SBruce Richardson 		}
13699a2dd95SBruce Richardson 	}
13799a2dd95SBruce Richardson 
13899a2dd95SBruce Richardson 	compressdev->device = device;
13999a2dd95SBruce Richardson 
14099a2dd95SBruce Richardson 	return compressdev;
14199a2dd95SBruce Richardson }
14299a2dd95SBruce Richardson 
14399a2dd95SBruce Richardson int
rte_compressdev_pmd_destroy(struct rte_compressdev * compressdev)14499a2dd95SBruce Richardson rte_compressdev_pmd_destroy(struct rte_compressdev *compressdev)
14599a2dd95SBruce Richardson {
14699a2dd95SBruce Richardson 	int retval;
14799a2dd95SBruce Richardson 
14899a2dd95SBruce Richardson 	COMPRESSDEV_LOG(INFO, "Closing comp device %s",
14999a2dd95SBruce Richardson 			compressdev->device->name);
15099a2dd95SBruce Richardson 
15199a2dd95SBruce Richardson 	/* free comp device */
15299a2dd95SBruce Richardson 	retval = rte_compressdev_pmd_release_device(compressdev);
15399a2dd95SBruce Richardson 	if (retval)
15499a2dd95SBruce Richardson 		return retval;
15599a2dd95SBruce Richardson 
15699a2dd95SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
15799a2dd95SBruce Richardson 		rte_free(compressdev->data->dev_private);
15899a2dd95SBruce Richardson 
15999a2dd95SBruce Richardson 	compressdev->device = NULL;
16099a2dd95SBruce Richardson 	compressdev->data = NULL;
16199a2dd95SBruce Richardson 
16299a2dd95SBruce Richardson 	return 0;
16399a2dd95SBruce Richardson }
164