1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2018, Microsoft Corporation.
26 #include <rte_compat.h>
28 #include <rte_debug.h>
29 #include <rte_interrupts.h>
31 #include <rte_vmbus_reg.h>
33 /* Forward declarations */
34 struct rte_vmbus_device;
35 struct rte_vmbus_driver;
38 struct vmbus_mon_page;
40 RTE_TAILQ_HEAD(rte_vmbus_device_list, rte_vmbus_device);
41 RTE_TAILQ_HEAD(rte_vmbus_driver_list, rte_vmbus_driver);
44 #define FOREACH_DEVICE_ON_VMBUS(p) \
45 RTE_TAILQ_FOREACH(p, &(rte_vmbus_bus.device_list), next)
47 #define FOREACH_DRIVER_ON_VMBUS(p) \
48 RTE_TAILQ_FOREACH(p, &(rte_vmbus_bus.driver_list), next)
50 /** Maximum number of VMBUS resources. */
58 #define VMBUS_MAX_RESOURCE 5
61 * A structure describing a VMBUS device.
63 struct rte_vmbus_device {
64 RTE_TAILQ_ENTRY(rte_vmbus_device) next; /**< Next probed VMBUS device */
65 const struct rte_vmbus_driver *driver; /**< Associated driver */
66 struct rte_device device; /**< Inherit core device */
67 rte_uuid_t device_id; /**< VMBUS device id */
68 rte_uuid_t class_id; /**< VMBUS device type */
69 uint32_t relid; /**< id for primary */
70 uint8_t monitor_id; /**< monitor page */
71 int uio_num; /**< UIO device number */
72 uint32_t *int_page; /**< VMBUS interrupt page */
73 struct vmbus_channel *primary; /**< VMBUS primary channel */
74 struct vmbus_mon_page *monitor_page; /**< VMBUS monitor page */
76 struct rte_intr_handle *intr_handle; /**< Interrupt handle */
77 struct rte_mem_resource resource[VMBUS_MAX_RESOURCE];
81 * Initialization function for the driver called during VMBUS probing.
83 typedef int (vmbus_probe_t)(struct rte_vmbus_driver *,
84 struct rte_vmbus_device *);
87 * Initialization function for the driver called during hot plugging.
89 typedef int (vmbus_remove_t)(struct rte_vmbus_device *);
92 * A structure describing a VMBUS driver.
94 struct rte_vmbus_driver {
95 RTE_TAILQ_ENTRY(rte_vmbus_driver) next; /**< Next in list. */
96 struct rte_driver driver;
97 struct rte_vmbus_bus *bus; /**< VM bus reference. */
98 vmbus_probe_t *probe; /**< Device Probe function. */
99 vmbus_remove_t *remove; /**< Device Remove function. */
101 const rte_uuid_t *id_table; /**< ID table. */
106 * Structure describing the VM bus
108 struct rte_vmbus_bus {
109 struct rte_bus bus; /**< Inherit the generic class */
110 struct rte_vmbus_device_list device_list; /**< List of devices */
111 struct rte_vmbus_driver_list driver_list; /**< List of drivers */
115 * Scan the content of the VMBUS bus, and the devices in the devices
119 * 0 on success, negative on error
121 int rte_vmbus_scan(void);
124 * Probe the VMBUS bus
130 int rte_vmbus_probe(void);
133 * Map the VMBUS device resources in user space virtual memory address
136 * A pointer to a rte_vmbus_device structure describing the device
140 * 0 on success, negative on error and positive if no driver
141 * is found for the device.
143 int rte_vmbus_map_device(struct rte_vmbus_device *dev);
149 * A pointer to a rte_vmbus_device structure describing the device
152 void rte_vmbus_unmap_device(struct rte_vmbus_device *dev);
155 * Get connection to primary VMBUS channel
158 * A pointer to a rte_vmbus_device structure describing the device
160 * A pointer to a VMBUS channel pointer that will be filled.
162 * - 0 Success; channel opened.
163 * - -ENOMEM: Not enough memory available.
164 * - -EINVAL: Regions could not be mapped.
166 int rte_vmbus_chan_open(struct rte_vmbus_device *device,
167 struct vmbus_channel **chan);
170 * Free connection to VMBUS channel
175 void rte_vmbus_chan_close(struct vmbus_channel *chan);
178 * Gets the maximum number of channels supported on device
181 * A pointer to a rte_vmbus_device structure describing the device
183 * Number of channels available.
185 int rte_vmbus_max_channels(const struct rte_vmbus_device *device);
188 * Get a connection to new secondary vmbus channel
191 * A pointer to primary VMBUS channel
193 * A pointer to a secondary VMBUS channel pointer that will be filled.
195 * - 0 Success; channel opened.
196 * - -ENOMEM: Not enough memory available.
197 * - -EINVAL: Regions could not be mapped.
199 int rte_vmbus_subchan_open(struct vmbus_channel *primary,
200 struct vmbus_channel **new_chan);
203 * Disable IRQ for device
208 void rte_vmbus_irq_mask(struct rte_vmbus_device *device);
211 * Enable IRQ for device
216 void rte_vmbus_irq_unmask(struct rte_vmbus_device *device);
219 * Read (and wait) for IRQ
224 int rte_vmbus_irq_read(struct rte_vmbus_device *device);
227 * Test if channel is empty
230 * Pointer to vmbus_channel structure.
232 * Return true if no data present in incoming ring.
234 bool rte_vmbus_chan_rx_empty(const struct vmbus_channel *channel);
237 * Send the specified buffer on the given channel
240 * Pointer to vmbus_channel structure.
242 * Type of packet that is being send e.g. negotiate, time
245 * Pointer to the buffer to send
247 * Number of bytes of data to send
249 * Identifier of the request
251 * Message type inband, rxbuf, gpa
253 * Is host signal tx is required (optional)
255 * Sends data in buffer directly to hyper-v via the vmbus
257 int rte_vmbus_chan_send(struct vmbus_channel *channel, uint16_t type,
258 void *data, uint32_t dlen,
259 uint64_t xact, uint32_t flags, bool *need_sig);
262 * Explicitly signal host that data is available
265 * Pointer to vmbus_channel structure.
267 * Used when batching multiple sends and only signaling host
268 * after the last send.
270 void rte_vmbus_chan_signal_tx(const struct vmbus_channel *channel);
272 /* Structure for scatter/gather I/O */
277 #define MAX_PAGE_BUFFER_COUNT 32
280 * Send a scattered buffer on the given channel
283 * Pointer to vmbus_channel structure.
285 * Type of packet that is being send e.g. negotiate, time
288 * Array of buffers to send
290 * Number of elements in iov
292 * Pointer to the buffer additional data to send
294 * Maximum size of what the buffer will hold
296 * Identifier of the request
298 * Message type inband, rxbuf, gpa
300 * Is host signal tx is required (optional)
302 * Sends data in buffer directly to hyper-v via the vmbus
304 int rte_vmbus_chan_send_sglist(struct vmbus_channel *channel,
305 struct vmbus_gpa gpa[], uint32_t gpacnt,
306 void *data, uint32_t dlen,
307 uint64_t xact, bool *need_sig);
309 * Receive response to request on the given channel
310 * skips the channel header.
313 * Pointer to vmbus_channel structure.
315 * Pointer to the buffer you want to receive the data into.
317 * Pointer to size of receive buffer (in/out)
319 * Pointer to received transaction_id
321 * On success, returns 0
322 * On failure, returns negative errno.
324 int rte_vmbus_chan_recv(struct vmbus_channel *chan,
325 void *data, uint32_t *len,
326 uint64_t *request_id);
329 * Receive response to request on the given channel
330 * includes the channel header.
333 * Pointer to vmbus_channel structure.
335 * Pointer to the buffer you want to receive the data into.
337 * Pointer to size of receive buffer (in/out)
339 * On success, returns number of bytes read.
340 * On failure, returns negative errno.
342 int rte_vmbus_chan_recv_raw(struct vmbus_channel *chan,
343 void *data, uint32_t *len);
346 * Notify host of bytes read (after recv_raw)
347 * Signals host if required.
350 * Pointer to vmbus_channel structure.
352 * Number of bytes read since last signal
354 void rte_vmbus_chan_signal_read(struct vmbus_channel *chan, uint32_t bytes_read);
357 * Determine sub channel index of the given channel
360 * Pointer to vmbus_channel structure.
362 * Sub channel index (0 for primary)
364 uint16_t rte_vmbus_sub_channel_index(const struct vmbus_channel *chan);
367 * Set the host monitor latency hint
372 * Pointer to vmbus_channel structure.
374 * Approximate wait period between hypervisor examinations of
375 * the trigger page (in nanoseconds).
377 void rte_vmbus_set_latency(const struct rte_vmbus_device *dev,
378 const struct vmbus_channel *chan,
382 * Register a VMBUS driver.
385 * A pointer to a rte_vmbus_driver structure describing the driver
388 void rte_vmbus_register(struct rte_vmbus_driver *driver);
391 * For debug dump contents of ring buffer.
394 * Pointer to vmbus_channel structure.
396 void rte_vmbus_chan_dump(FILE *f, const struct vmbus_channel *chan);
399 * Unregister a VMBUS driver.
402 * A pointer to a rte_vmbus_driver structure describing the driver
403 * to be unregistered.
405 void rte_vmbus_unregister(struct rte_vmbus_driver *driver);
407 /** Helper for VMBUS device registration from driver instance */
408 #define RTE_PMD_REGISTER_VMBUS(nm, vmbus_drv) \
409 RTE_INIT(vmbusinitfn_ ##nm) \
411 (vmbus_drv).driver.name = RTE_STR(nm); \
412 rte_vmbus_register(&vmbus_drv); \
414 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
420 #endif /* _VMBUS_H_ */