1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2018, Microsoft Corporation.
23 #include <sys/queue.h>
27 #include <rte_compat.h>
29 #include <rte_debug.h>
30 #include <rte_interrupts.h>
32 #include <rte_vmbus_reg.h>
34 /* Forward declarations */
35 struct rte_vmbus_device;
36 struct rte_vmbus_driver;
39 struct vmbus_mon_page;
41 TAILQ_HEAD(rte_vmbus_device_list, rte_vmbus_device);
42 TAILQ_HEAD(rte_vmbus_driver_list, rte_vmbus_driver);
45 #define FOREACH_DEVICE_ON_VMBUS(p) \
46 TAILQ_FOREACH(p, &(rte_vmbus_bus.device_list), next)
48 #define FOREACH_DRIVER_ON_VMBUS(p) \
49 TAILQ_FOREACH(p, &(rte_vmbus_bus.driver_list), next)
51 /** Maximum number of VMBUS resources. */
59 #define VMBUS_MAX_RESOURCE 5
62 * A structure describing a VMBUS device.
64 struct rte_vmbus_device {
65 TAILQ_ENTRY(rte_vmbus_device) next; /**< Next probed VMBUS device */
66 const struct rte_vmbus_driver *driver; /**< Associated driver */
67 struct rte_device device; /**< Inherit core device */
68 rte_uuid_t device_id; /**< VMBUS device id */
69 rte_uuid_t class_id; /**< VMBUS device type */
70 uint32_t relid; /**< id for primary */
71 uint8_t monitor_id; /**< monitor page */
72 int uio_num; /**< UIO device number */
73 uint32_t *int_page; /**< VMBUS interrupt page */
74 struct vmbus_channel *primary; /**< VMBUS primary channel */
75 struct vmbus_mon_page *monitor_page; /**< VMBUS monitor page */
77 struct rte_intr_handle intr_handle; /**< Interrupt handle */
78 struct rte_mem_resource resource[VMBUS_MAX_RESOURCE];
82 * Initialization function for the driver called during VMBUS probing.
84 typedef int (vmbus_probe_t)(struct rte_vmbus_driver *,
85 struct rte_vmbus_device *);
88 * Initialization function for the driver called during hot plugging.
90 typedef int (vmbus_remove_t)(struct rte_vmbus_device *);
93 * A structure describing a VMBUS driver.
95 struct rte_vmbus_driver {
96 TAILQ_ENTRY(rte_vmbus_driver) next; /**< Next in list. */
97 struct rte_driver driver;
98 struct rte_vmbus_bus *bus; /**< VM bus reference. */
99 vmbus_probe_t *probe; /**< Device Probe function. */
100 vmbus_remove_t *remove; /**< Device Remove function. */
102 const rte_uuid_t *id_table; /**< ID table. */
107 * Structure describing the VM bus
109 struct rte_vmbus_bus {
110 struct rte_bus bus; /**< Inherit the generic class */
111 struct rte_vmbus_device_list device_list; /**< List of devices */
112 struct rte_vmbus_driver_list driver_list; /**< List of drivers */
116 * Scan the content of the VMBUS bus, and the devices in the devices
120 * 0 on success, negative on error
122 int rte_vmbus_scan(void);
125 * Probe the VMBUS bus
131 int rte_vmbus_probe(void);
134 * Map the VMBUS device resources in user space virtual memory address
137 * A pointer to a rte_vmbus_device structure describing the device
141 * 0 on success, negative on error and positive if no driver
142 * is found for the device.
144 int rte_vmbus_map_device(struct rte_vmbus_device *dev);
150 * A pointer to a rte_vmbus_device structure describing the device
153 void rte_vmbus_unmap_device(struct rte_vmbus_device *dev);
156 * Get connection to primary VMBUS channel
159 * A pointer to a rte_vmbus_device structure describing the device
161 * A pointer to a VMBUS channel pointer that will be filled.
163 * - 0 Success; channel opened.
164 * - -ENOMEM: Not enough memory available.
165 * - -EINVAL: Regions could not be mapped.
167 int rte_vmbus_chan_open(struct rte_vmbus_device *device,
168 struct vmbus_channel **chan);
171 * Free connection to VMBUS channel
176 void rte_vmbus_chan_close(struct vmbus_channel *chan);
179 * Gets the maximum number of channels supported on device
182 * A pointer to a rte_vmbus_device structure describing the device
184 * Number of channels available.
186 int rte_vmbus_max_channels(const struct rte_vmbus_device *device);
189 * Get a connection to new secondary vmbus channel
192 * A pointer to primary VMBUS channel
194 * A pointer to a secondary VMBUS channel pointer that will be filled.
196 * - 0 Success; channel opened.
197 * - -ENOMEM: Not enough memory available.
198 * - -EINVAL: Regions could not be mapped.
200 int rte_vmbus_subchan_open(struct vmbus_channel *primary,
201 struct vmbus_channel **new_chan);
204 * Disable IRQ for device
209 void rte_vmbus_irq_mask(struct rte_vmbus_device *device);
212 * Enable IRQ for device
217 void rte_vmbus_irq_unmask(struct rte_vmbus_device *device);
220 * Read (and wait) for IRQ
225 int rte_vmbus_irq_read(struct rte_vmbus_device *device);
228 * Test if channel is empty
231 * Pointer to vmbus_channel structure.
233 * Return true if no data present in incoming ring.
235 bool rte_vmbus_chan_rx_empty(const struct vmbus_channel *channel);
238 * Send the specified buffer on the given channel
241 * Pointer to vmbus_channel structure.
243 * Type of packet that is being send e.g. negotiate, time
246 * Pointer to the buffer to send
248 * Number of bytes of data to send
250 * Identifier of the request
252 * Message type inband, rxbuf, gpa
254 * Is host signal tx is required (optional)
256 * Sends data in buffer directly to hyper-v via the vmbus
258 int rte_vmbus_chan_send(struct vmbus_channel *channel, uint16_t type,
259 void *data, uint32_t dlen,
260 uint64_t xact, uint32_t flags, bool *need_sig);
263 * Explicitly signal host that data is available
266 * Pointer to vmbus_channel structure.
268 * Used when batching multiple sends and only signaling host
269 * after the last send.
271 void rte_vmbus_chan_signal_tx(const struct vmbus_channel *channel);
273 /* Structure for scatter/gather I/O */
278 #define MAX_PAGE_BUFFER_COUNT 32
281 * Send a scattered buffer on the given channel
284 * Pointer to vmbus_channel structure.
286 * Type of packet that is being send e.g. negotiate, time
289 * Array of buffers to send
291 * Number of elements in iov
293 * Pointer to the buffer additional data to send
295 * Maximum size of what the the buffer will hold
297 * Identifier of the request
299 * Message type inband, rxbuf, gpa
301 * Is host signal tx is required (optional)
303 * Sends data in buffer directly to hyper-v via the vmbus
305 int rte_vmbus_chan_send_sglist(struct vmbus_channel *channel,
306 struct vmbus_gpa gpa[], uint32_t gpacnt,
307 void *data, uint32_t dlen,
308 uint64_t xact, bool *need_sig);
310 * Receive response to request on the given channel
311 * skips the channel header.
314 * Pointer to vmbus_channel structure.
316 * Pointer to the buffer you want to receive the data into.
318 * Pointer to size of receive buffer (in/out)
320 * Pointer to received transaction_id
322 * On success, returns 0
323 * On failure, returns negative errno.
325 int rte_vmbus_chan_recv(struct vmbus_channel *chan,
326 void *data, uint32_t *len,
327 uint64_t *request_id);
330 * Receive response to request on the given channel
331 * includes the channel header.
334 * Pointer to vmbus_channel structure.
336 * Pointer to the buffer you want to receive the data into.
338 * Pointer to size of receive buffer (in/out)
340 * On success, returns 0
341 * On failure, returns negative errno.
343 int rte_vmbus_chan_recv_raw(struct vmbus_channel *chan,
344 void *data, uint32_t *len);
347 * Determine sub channel index of the given channel
350 * Pointer to vmbus_channel structure.
352 * Sub channel index (0 for primary)
354 uint16_t rte_vmbus_sub_channel_index(const struct vmbus_channel *chan);
357 * Register a VMBUS driver.
360 * A pointer to a rte_vmbus_driver structure describing the driver
363 void rte_vmbus_register(struct rte_vmbus_driver *driver);
366 * For debug dump contents of ring buffer.
369 * Pointer to vmbus_channel structure.
371 void rte_vmbus_chan_dump(FILE *f, const struct vmbus_channel *chan);
374 * Unregister a VMBUS driver.
377 * A pointer to a rte_vmbus_driver structure describing the driver
378 * to be unregistered.
380 void rte_vmbus_unregister(struct rte_vmbus_driver *driver);
382 /** Helper for VMBUS device registration from driver instance */
383 #define RTE_PMD_REGISTER_VMBUS(nm, vmbus_drv) \
384 RTE_INIT(vmbusinitfn_ ##nm); \
385 static void vmbusinitfn_ ##nm(void) \
387 (vmbus_drv).driver.name = RTE_STR(nm); \
388 rte_vmbus_register(&vmbus_drv); \
390 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
396 #endif /* _VMBUS_H_ */