1*2d9fd380Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2*2d9fd380Sjfb8856606  * Copyright 2014 6WIND S.A.
3*2d9fd380Sjfb8856606  */
4*2d9fd380Sjfb8856606 
5*2d9fd380Sjfb8856606 #ifndef _RTE_DEVARGS_H_
6*2d9fd380Sjfb8856606 #define _RTE_DEVARGS_H_
7*2d9fd380Sjfb8856606 
8*2d9fd380Sjfb8856606 /**
9*2d9fd380Sjfb8856606  * @file
10*2d9fd380Sjfb8856606  *
11*2d9fd380Sjfb8856606  * RTE devargs: list of devices and their user arguments
12*2d9fd380Sjfb8856606  *
13*2d9fd380Sjfb8856606  * This file stores a list of devices and their arguments given by
14*2d9fd380Sjfb8856606  * the user when a DPDK application is started. These devices can be PCI
15*2d9fd380Sjfb8856606  * devices or virtual devices. These devices are stored at startup in a
16*2d9fd380Sjfb8856606  * list of rte_devargs structures.
17*2d9fd380Sjfb8856606  */
18*2d9fd380Sjfb8856606 
19*2d9fd380Sjfb8856606 #ifdef __cplusplus
20*2d9fd380Sjfb8856606 extern "C" {
21*2d9fd380Sjfb8856606 #endif
22*2d9fd380Sjfb8856606 
23*2d9fd380Sjfb8856606 #include <stdio.h>
24*2d9fd380Sjfb8856606 #include <sys/queue.h>
25*2d9fd380Sjfb8856606 #include <rte_compat.h>
26*2d9fd380Sjfb8856606 #include <rte_bus.h>
27*2d9fd380Sjfb8856606 
28*2d9fd380Sjfb8856606 /**
29*2d9fd380Sjfb8856606  * Type of generic device
30*2d9fd380Sjfb8856606  */
31*2d9fd380Sjfb8856606 enum rte_devtype {
32*2d9fd380Sjfb8856606 	RTE_DEVTYPE_ALLOWED,
33*2d9fd380Sjfb8856606 	RTE_DEVTYPE_BLOCKED,
34*2d9fd380Sjfb8856606 	RTE_DEVTYPE_VIRTUAL,
35*2d9fd380Sjfb8856606 };
36*2d9fd380Sjfb8856606 
37*2d9fd380Sjfb8856606 /* Backwards compatibility will be removed later */
38*2d9fd380Sjfb8856606 #define RTE_DEVTYPE_WHITELISTED_PCI \
39*2d9fd380Sjfb8856606 	RTE_DEPRECATED(RTE_DEVTYPE_WHITELISTED_PCI) RTE_DEVTYPE_ALLOWED
40*2d9fd380Sjfb8856606 #define RTE_DEVTYPE_BLACKLISTED_PCI \
41*2d9fd380Sjfb8856606 	RTE_DEPRECATED(RTE_DEVTYPE_BLACKLISTED_PCI) RTE_DEVTYPE_BLOCKED
42*2d9fd380Sjfb8856606 
43*2d9fd380Sjfb8856606 /**
44*2d9fd380Sjfb8856606  * Structure that stores a device given by the user with its arguments
45*2d9fd380Sjfb8856606  *
46*2d9fd380Sjfb8856606  * A user device is a physical or a virtual device given by the user to
47*2d9fd380Sjfb8856606  * the DPDK application at startup through command line arguments.
48*2d9fd380Sjfb8856606  *
49*2d9fd380Sjfb8856606  * The structure stores the configuration of the device, its PCI
50*2d9fd380Sjfb8856606  * identifier if it's a PCI device or the driver name if it's a virtual
51*2d9fd380Sjfb8856606  * device.
52*2d9fd380Sjfb8856606  */
53*2d9fd380Sjfb8856606 struct rte_devargs {
54*2d9fd380Sjfb8856606 	/** Next in list. */
55*2d9fd380Sjfb8856606 	TAILQ_ENTRY(rte_devargs) next;
56*2d9fd380Sjfb8856606 	/** Type of device. */
57*2d9fd380Sjfb8856606 	enum rte_devtype type;
58*2d9fd380Sjfb8856606 	/** Device policy. */
59*2d9fd380Sjfb8856606 	enum rte_dev_policy policy;
60*2d9fd380Sjfb8856606 	/** Name of the device. */
61*2d9fd380Sjfb8856606 	char name[RTE_DEV_NAME_MAX_LEN];
62*2d9fd380Sjfb8856606 	RTE_STD_C11
63*2d9fd380Sjfb8856606 	union {
64*2d9fd380Sjfb8856606 	/** Arguments string as given by user or "" for no argument. */
65*2d9fd380Sjfb8856606 		char *args;
66*2d9fd380Sjfb8856606 		const char *drv_str;
67*2d9fd380Sjfb8856606 	};
68*2d9fd380Sjfb8856606 	struct rte_bus *bus; /**< bus handle. */
69*2d9fd380Sjfb8856606 	struct rte_class *cls; /**< class handle. */
70*2d9fd380Sjfb8856606 	const char *bus_str; /**< bus-related part of device string. */
71*2d9fd380Sjfb8856606 	const char *cls_str; /**< class-related part of device string. */
72*2d9fd380Sjfb8856606 	const char *data; /**< Device string storage. */
73*2d9fd380Sjfb8856606 };
74*2d9fd380Sjfb8856606 
75*2d9fd380Sjfb8856606 /**
76*2d9fd380Sjfb8856606  * Parse a device string.
77*2d9fd380Sjfb8856606  *
78*2d9fd380Sjfb8856606  * Verify that a bus is capable of handling the device passed
79*2d9fd380Sjfb8856606  * in argument. Store which bus will handle the device, its name
80*2d9fd380Sjfb8856606  * and the eventual device parameters.
81*2d9fd380Sjfb8856606  *
82*2d9fd380Sjfb8856606  * The syntax is:
83*2d9fd380Sjfb8856606  *
84*2d9fd380Sjfb8856606  *     bus:device_identifier,arg1=val1,arg2=val2
85*2d9fd380Sjfb8856606  *
86*2d9fd380Sjfb8856606  * where "bus:" is the bus name followed by any character separator.
87*2d9fd380Sjfb8856606  * The bus name is optional. If no bus name is specified, each bus
88*2d9fd380Sjfb8856606  * will attempt to recognize the device identifier. The first one
89*2d9fd380Sjfb8856606  * to succeed will be used.
90*2d9fd380Sjfb8856606  *
91*2d9fd380Sjfb8856606  * Examples:
92*2d9fd380Sjfb8856606  *
93*2d9fd380Sjfb8856606  *     pci:0000:05.00.0,arg=val
94*2d9fd380Sjfb8856606  *     05.00.0,arg=val
95*2d9fd380Sjfb8856606  *     vdev:net_ring0
96*2d9fd380Sjfb8856606  *
97*2d9fd380Sjfb8856606  * @param da
98*2d9fd380Sjfb8856606  *   The devargs structure holding the device information.
99*2d9fd380Sjfb8856606  *
100*2d9fd380Sjfb8856606  * @param dev
101*2d9fd380Sjfb8856606  *   String describing a device.
102*2d9fd380Sjfb8856606  *
103*2d9fd380Sjfb8856606  * @return
104*2d9fd380Sjfb8856606  *   - 0 on success.
105*2d9fd380Sjfb8856606  *   - Negative errno on error.
106*2d9fd380Sjfb8856606  */
107*2d9fd380Sjfb8856606 int
108*2d9fd380Sjfb8856606 rte_devargs_parse(struct rte_devargs *da, const char *dev);
109*2d9fd380Sjfb8856606 
110*2d9fd380Sjfb8856606 /**
111*2d9fd380Sjfb8856606  * Parse a device string.
112*2d9fd380Sjfb8856606  *
113*2d9fd380Sjfb8856606  * Verify that a bus is capable of handling the device passed
114*2d9fd380Sjfb8856606  * in argument. Store which bus will handle the device, its name
115*2d9fd380Sjfb8856606  * and the eventual device parameters.
116*2d9fd380Sjfb8856606  *
117*2d9fd380Sjfb8856606  * The device string is built with a printf-like syntax.
118*2d9fd380Sjfb8856606  *
119*2d9fd380Sjfb8856606  * The syntax is:
120*2d9fd380Sjfb8856606  *
121*2d9fd380Sjfb8856606  *     bus:device_identifier,arg1=val1,arg2=val2
122*2d9fd380Sjfb8856606  *
123*2d9fd380Sjfb8856606  * where "bus:" is the bus name followed by any character separator.
124*2d9fd380Sjfb8856606  * The bus name is optional. If no bus name is specified, each bus
125*2d9fd380Sjfb8856606  * will attempt to recognize the device identifier. The first one
126*2d9fd380Sjfb8856606  * to succeed will be used.
127*2d9fd380Sjfb8856606  *
128*2d9fd380Sjfb8856606  * Examples:
129*2d9fd380Sjfb8856606  *
130*2d9fd380Sjfb8856606  *     pci:0000:05.00.0,arg=val
131*2d9fd380Sjfb8856606  *     05.00.0,arg=val
132*2d9fd380Sjfb8856606  *     vdev:net_ring0
133*2d9fd380Sjfb8856606  *
134*2d9fd380Sjfb8856606  * @param da
135*2d9fd380Sjfb8856606  *   The devargs structure holding the device information.
136*2d9fd380Sjfb8856606  * @param format
137*2d9fd380Sjfb8856606  *   Format string describing a device.
138*2d9fd380Sjfb8856606  *
139*2d9fd380Sjfb8856606  * @return
140*2d9fd380Sjfb8856606  *   - 0 on success.
141*2d9fd380Sjfb8856606  *   - Negative errno on error.
142*2d9fd380Sjfb8856606  */
143*2d9fd380Sjfb8856606 int
144*2d9fd380Sjfb8856606 rte_devargs_parsef(struct rte_devargs *da,
145*2d9fd380Sjfb8856606 		   const char *format, ...)
146*2d9fd380Sjfb8856606 __rte_format_printf(2, 0);
147*2d9fd380Sjfb8856606 
148*2d9fd380Sjfb8856606 /**
149*2d9fd380Sjfb8856606  * Insert an rte_devargs in the global list.
150*2d9fd380Sjfb8856606  *
151*2d9fd380Sjfb8856606  * @param da
152*2d9fd380Sjfb8856606  *  The devargs structure to insert.
153*2d9fd380Sjfb8856606  *  If a devargs for the same device is already inserted,
154*2d9fd380Sjfb8856606  *  it will be updated and returned. It means *da pointer can change.
155*2d9fd380Sjfb8856606  *
156*2d9fd380Sjfb8856606  * @return
157*2d9fd380Sjfb8856606  *   - 0 on success
158*2d9fd380Sjfb8856606  *   - Negative on error.
159*2d9fd380Sjfb8856606  */
160*2d9fd380Sjfb8856606 int
161*2d9fd380Sjfb8856606 rte_devargs_insert(struct rte_devargs **da);
162*2d9fd380Sjfb8856606 
163*2d9fd380Sjfb8856606 /**
164*2d9fd380Sjfb8856606  * Add a device to the user device list
165*2d9fd380Sjfb8856606  * See rte_devargs_parse() for details.
166*2d9fd380Sjfb8856606  *
167*2d9fd380Sjfb8856606  * @param devtype
168*2d9fd380Sjfb8856606  *   The type of the device.
169*2d9fd380Sjfb8856606  * @param devargs_str
170*2d9fd380Sjfb8856606  *   The arguments as given by the user.
171*2d9fd380Sjfb8856606  *
172*2d9fd380Sjfb8856606  * @return
173*2d9fd380Sjfb8856606  *   - 0 on success
174*2d9fd380Sjfb8856606  *   - A negative value on error
175*2d9fd380Sjfb8856606  */
176*2d9fd380Sjfb8856606 int rte_devargs_add(enum rte_devtype devtype, const char *devargs_str);
177*2d9fd380Sjfb8856606 
178*2d9fd380Sjfb8856606 /**
179*2d9fd380Sjfb8856606  * Remove a device from the user device list.
180*2d9fd380Sjfb8856606  * Its resources are freed.
181*2d9fd380Sjfb8856606  * If the devargs cannot be found, nothing happens.
182*2d9fd380Sjfb8856606  *
183*2d9fd380Sjfb8856606  * @param devargs
184*2d9fd380Sjfb8856606  *   The instance or a copy of devargs to remove.
185*2d9fd380Sjfb8856606  *
186*2d9fd380Sjfb8856606  * @return
187*2d9fd380Sjfb8856606  *   0 on success.
188*2d9fd380Sjfb8856606  *   <0 on error.
189*2d9fd380Sjfb8856606  *   >0 if the devargs was not within the user device list.
190*2d9fd380Sjfb8856606  */
191*2d9fd380Sjfb8856606 int rte_devargs_remove(struct rte_devargs *devargs);
192*2d9fd380Sjfb8856606 
193*2d9fd380Sjfb8856606 /**
194*2d9fd380Sjfb8856606  * Count the number of user devices of a specified type
195*2d9fd380Sjfb8856606  *
196*2d9fd380Sjfb8856606  * @param devtype
197*2d9fd380Sjfb8856606  *   The type of the devices to counted.
198*2d9fd380Sjfb8856606  *
199*2d9fd380Sjfb8856606  * @return
200*2d9fd380Sjfb8856606  *   The number of devices.
201*2d9fd380Sjfb8856606  */
202*2d9fd380Sjfb8856606 unsigned int
203*2d9fd380Sjfb8856606 rte_devargs_type_count(enum rte_devtype devtype);
204*2d9fd380Sjfb8856606 
205*2d9fd380Sjfb8856606 /**
206*2d9fd380Sjfb8856606  * This function dumps the list of user device and their arguments.
207*2d9fd380Sjfb8856606  *
208*2d9fd380Sjfb8856606  * @param f
209*2d9fd380Sjfb8856606  *   A pointer to a file for output
210*2d9fd380Sjfb8856606  */
211*2d9fd380Sjfb8856606 void rte_devargs_dump(FILE *f);
212*2d9fd380Sjfb8856606 
213*2d9fd380Sjfb8856606 /**
214*2d9fd380Sjfb8856606  * Find next rte_devargs matching the provided bus name.
215*2d9fd380Sjfb8856606  *
216*2d9fd380Sjfb8856606  * @param busname
217*2d9fd380Sjfb8856606  *   Limit the iteration to devargs related to buses
218*2d9fd380Sjfb8856606  *   matching this name.
219*2d9fd380Sjfb8856606  *   Will return any next rte_devargs if NULL.
220*2d9fd380Sjfb8856606  *
221*2d9fd380Sjfb8856606  * @param start
222*2d9fd380Sjfb8856606  *   Starting iteration point. The iteration will start at
223*2d9fd380Sjfb8856606  *   the first rte_devargs if NULL.
224*2d9fd380Sjfb8856606  *
225*2d9fd380Sjfb8856606  * @return
226*2d9fd380Sjfb8856606  *   Next rte_devargs entry matching the requested bus,
227*2d9fd380Sjfb8856606  *   NULL if there is none.
228*2d9fd380Sjfb8856606  */
229*2d9fd380Sjfb8856606 struct rte_devargs *
230*2d9fd380Sjfb8856606 rte_devargs_next(const char *busname, const struct rte_devargs *start);
231*2d9fd380Sjfb8856606 
232*2d9fd380Sjfb8856606 /**
233*2d9fd380Sjfb8856606  * Iterate over all rte_devargs for a specific bus.
234*2d9fd380Sjfb8856606  */
235*2d9fd380Sjfb8856606 #define RTE_EAL_DEVARGS_FOREACH(busname, da) \
236*2d9fd380Sjfb8856606 	for (da = rte_devargs_next(busname, NULL); \
237*2d9fd380Sjfb8856606 	     da != NULL; \
238*2d9fd380Sjfb8856606 	     da = rte_devargs_next(busname, da)) \
239*2d9fd380Sjfb8856606 
240*2d9fd380Sjfb8856606 #ifdef __cplusplus
241*2d9fd380Sjfb8856606 }
242*2d9fd380Sjfb8856606 #endif
243*2d9fd380Sjfb8856606 
244*2d9fd380Sjfb8856606 #endif /* _RTE_DEVARGS_H_ */
245