xref: /dpdk/drivers/bus/platform/platform_params.c (revision e99981af34632ecce3bac82d05db97b08308f9b5)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2023 Marvell.
3  */
4 
5 #include <string.h>
6 #include <errno.h>
7 
8 #include <rte_bus.h>
9 #include <rte_common.h>
10 #include <rte_dev.h>
11 #include <rte_errno.h>
12 #include <rte_kvargs.h>
13 #include <rte_vfio.h>
14 
15 #include "bus_platform_driver.h"
16 #include "private.h"
17 
18 #ifdef VFIO_PRESENT
19 
20 enum platform_params {
21 	RTE_PLATFORM_PARAM_NAME,
22 };
23 
24 static const char * const platform_params_keys[] = {
25 	[RTE_PLATFORM_PARAM_NAME] = "name",
26 	NULL
27 };
28 
29 static int
30 platform_dev_match(const struct rte_device *dev, const void *_kvlist)
31 {
32 	const char *key = platform_params_keys[RTE_PLATFORM_PARAM_NAME];
33 	const struct rte_kvargs *kvlist = _kvlist;
34 	const char *name;
35 
36 	/* no kvlist arg, all devices match */
37 	if (kvlist == NULL)
38 		return 0;
39 
40 	/* if key is present in kvlist and does not match, filter device */
41 	name = rte_kvargs_get(kvlist, key);
42 	if (name != NULL && strcmp(name, dev->name))
43 		return -1;
44 
45 	return 0;
46 }
47 
48 void *
49 platform_bus_dev_iterate(const void *start, const char *str,
50 			 const struct rte_dev_iterator *it __rte_unused)
51 {
52 	rte_bus_find_device_t find_device;
53 	struct rte_kvargs *kvargs = NULL;
54 	struct rte_device *dev;
55 
56 	if (str != NULL) {
57 		kvargs = rte_kvargs_parse(str, platform_params_keys);
58 		if (!kvargs) {
59 			PLATFORM_LOG_LINE(ERR, "cannot parse argument list %s", str);
60 			rte_errno = EINVAL;
61 			return NULL;
62 		}
63 	}
64 
65 	find_device = platform_bus.bus.find_device;
66 	if (find_device == NULL) {
67 		rte_kvargs_free(kvargs);
68 		return NULL;
69 	}
70 
71 	dev = platform_bus.bus.find_device(start, platform_dev_match, kvargs);
72 	rte_kvargs_free(kvargs);
73 
74 	return dev;
75 }
76 
77 #endif /* VFIO_PRESENT */
78