New upstream version 18.08
[deb_dpdk.git] / drivers / bus / vdev / rte_bus_vdev.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 RehiveTech. All rights reserved.
3  */
4
5 #ifndef RTE_VDEV_H
6 #define RTE_VDEV_H
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 #include <sys/queue.h>
13 #include <rte_dev.h>
14 #include <rte_devargs.h>
15
16 struct rte_vdev_device {
17         TAILQ_ENTRY(rte_vdev_device) next;      /**< Next attached vdev */
18         struct rte_device device;               /**< Inherit core device */
19 };
20
21 /**
22  * @internal
23  * Helper macro for drivers that need to convert to struct rte_vdev_device.
24  */
25 #define RTE_DEV_TO_VDEV(ptr) \
26         container_of(ptr, struct rte_vdev_device, device)
27
28 #define RTE_DEV_TO_VDEV_CONST(ptr) \
29         container_of(ptr, const struct rte_vdev_device, device)
30
31 static inline const char *
32 rte_vdev_device_name(const struct rte_vdev_device *dev)
33 {
34         if (dev && dev->device.name)
35                 return dev->device.name;
36         return NULL;
37 }
38
39 static inline const char *
40 rte_vdev_device_args(const struct rte_vdev_device *dev)
41 {
42         if (dev && dev->device.devargs)
43                 return dev->device.devargs->args;
44         return "";
45 }
46
47 /** Double linked list of virtual device drivers. */
48 TAILQ_HEAD(vdev_driver_list, rte_vdev_driver);
49
50 /**
51  * Probe function called for each virtual device driver once.
52  */
53 typedef int (rte_vdev_probe_t)(struct rte_vdev_device *dev);
54
55 /**
56  * Remove function called for each virtual device driver once.
57  */
58 typedef int (rte_vdev_remove_t)(struct rte_vdev_device *dev);
59
60 /**
61  * A virtual device driver abstraction.
62  */
63 struct rte_vdev_driver {
64         TAILQ_ENTRY(rte_vdev_driver) next; /**< Next in list. */
65         struct rte_driver driver;      /**< Inherited general driver. */
66         rte_vdev_probe_t *probe;       /**< Virtual device probe function. */
67         rte_vdev_remove_t *remove;     /**< Virtual device remove function. */
68 };
69
70 /**
71  * Register a virtual device driver.
72  *
73  * @param driver
74  *   A pointer to a rte_vdev_driver structure describing the driver
75  *   to be registered.
76  */
77 void rte_vdev_register(struct rte_vdev_driver *driver);
78
79 /**
80  * Unregister a virtual device driver.
81  *
82  * @param driver
83  *   A pointer to a rte_vdev_driver structure describing the driver
84  *   to be unregistered.
85  */
86 void rte_vdev_unregister(struct rte_vdev_driver *driver);
87
88 #define RTE_PMD_REGISTER_VDEV(nm, vdrv)\
89 static const char *vdrvinit_ ## nm ## _alias;\
90 RTE_INIT(vdrvinitfn_ ##vdrv)\
91 {\
92         (vdrv).driver.name = RTE_STR(nm);\
93         (vdrv).driver.alias = vdrvinit_ ## nm ## _alias;\
94         rte_vdev_register(&vdrv);\
95 } \
96 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
97
98 #define RTE_PMD_REGISTER_ALIAS(nm, alias)\
99 static const char *vdrvinit_ ## nm ## _alias = RTE_STR(alias)
100
101 typedef void (*rte_vdev_scan_callback)(void *user_arg);
102
103 /**
104  * Add a callback to be called on vdev scan
105  * before reading the devargs list.
106  *
107  * This function cannot be called in a scan callback
108  * because of deadlock.
109  *
110  * @param callback
111  *   The function to be called which can update the devargs list.
112  * @param user_arg
113  *   An opaque pointer passed to callback.
114  * @return
115  *   0 on success, negative on error
116  */
117 int
118 rte_vdev_add_custom_scan(rte_vdev_scan_callback callback, void *user_arg);
119
120 /**
121  * Remove a registered scan callback.
122  *
123  * This function cannot be called in a scan callback
124  * because of deadlock.
125  *
126  * @param callback
127  *   The registered function to be removed.
128  * @param user_arg
129  *   The associated opaque pointer or (void*)-1 for any.
130  * @return
131  *   0 on success
132  */
133 int
134 rte_vdev_remove_custom_scan(rte_vdev_scan_callback callback, void *user_arg);
135
136 /**
137  * Initialize a driver specified by name.
138  *
139  * @param name
140  *   The pointer to a driver name to be initialized.
141  * @param args
142  *   The pointer to arguments used by driver initialization.
143  * @return
144  *  0 on success, negative on error
145  */
146 int rte_vdev_init(const char *name, const char *args);
147
148 /**
149  * Uninitalize a driver specified by name.
150  *
151  * @param name
152  *   The pointer to a driver name to be initialized.
153  * @return
154  *  0 on success, negative on error
155  */
156 int rte_vdev_uninit(const char *name);
157
158 #ifdef __cplusplus
159 }
160 #endif
161
162 #endif