6 * Hyper-V virtual machine bus
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
13 #include <ipxe/uuid.h>
14 #include <ipxe/device.h>
15 #include <ipxe/tables.h>
16 #include <ipxe/uaccess.h>
17 #include <ipxe/iobuf.h>
18 #include <ipxe/hyperv.h>
20 /** VMBus message connection ID */
21 #define VMBUS_MESSAGE_ID 1
23 /** VMBus event connection ID */
24 #define VMBUS_EVENT_ID 2
26 /** VMBus message type */
27 #define VMBUS_MESSAGE_TYPE 1
29 /** VMBus message synthetic interrupt */
30 #define VMBUS_MESSAGE_SINT 2
32 /** VMBus version number */
36 /** Major/minor version */
43 } __attribute__ (( packed ));
45 /** Known VMBus protocol versions */
46 enum vmbus_raw_version {
47 /** Windows Server 2008 */
48 VMBUS_VERSION_WS2008 = ( ( 0 << 16 ) | ( 13 << 0 ) ),
50 VMBUS_VERSION_WIN7 = ( ( 1 << 16 ) | ( 1 << 0 ) ),
52 VMBUS_VERSION_WIN8 = ( ( 2 << 16 ) | ( 4 << 0 ) ),
54 VMBUS_VERSION_WIN8_1 = ( ( 3 << 16 ) | ( 0 << 0 ) ),
57 /** Guest physical address range descriptor */
58 struct vmbus_gpa_range {
61 /** Starting byte offset */
63 /** Page frame numbers
65 * The length of this array is implied by the byte count and
69 } __attribute__ (( packed ));
71 /** VMBus message header */
72 struct vmbus_message_header {
77 } __attribute__ (( packed ));
79 /** VMBus message types */
80 enum vmbus_message_type {
81 VMBUS_OFFER_CHANNEL = 1,
82 VMBUS_REQUEST_OFFERS = 3,
83 VMBUS_ALL_OFFERS_DELIVERED = 4,
84 VMBUS_OPEN_CHANNEL = 5,
85 VMBUS_OPEN_CHANNEL_RESULT = 6,
86 VMBUS_CLOSE_CHANNEL = 7,
87 VMBUS_GPADL_HEADER = 8,
88 VMBUS_GPADL_CREATED = 10,
89 VMBUS_GPADL_TEARDOWN = 11,
90 VMBUS_GPADL_TORNDOWN = 12,
91 VMBUS_INITIATE_CONTACT = 14,
92 VMBUS_VERSION_RESPONSE = 15,
94 VMBUS_UNLOAD_RESPONSE = 17,
97 /** VMBus "offer channel" message */
98 struct vmbus_offer_channel {
100 struct vmbus_message_header header;
103 /** Channel instance */
106 uint8_t reserved_a[16];
110 uint8_t reserved_b[2];
114 uint8_t reserved_c[4];
119 /** Monitor exists */
125 } __attribute__ (( packed ));
127 /** VMBus "open channel" message */
128 struct vmbus_open_channel {
129 /** Message header */
130 struct vmbus_message_header header;
135 /** Ring buffer GPADL ID */
139 /** Outbound ring buffer size (in pages) */
141 /** User-specific data */
143 } __attribute__ (( packed ));
145 /** VMBus "open channel result" message */
146 struct vmbus_open_channel_result {
147 /** Message header */
148 struct vmbus_message_header header;
155 } __attribute__ (( packed ));
157 /** VMBus "close channel" message */
158 struct vmbus_close_channel {
159 /** Message header */
160 struct vmbus_message_header header;
163 } __attribute__ (( packed ));
165 /** VMBus "GPADL header" message */
166 struct vmbus_gpadl_header {
167 /** Message header */
168 struct vmbus_message_header header;
173 /** Length of range descriptors */
175 /** Number of range descriptors */
176 uint16_t range_count;
177 /** Range descriptors */
178 struct vmbus_gpa_range range[0];
179 } __attribute__ (( packed ));
181 /** VMBus "GPADL created" message */
182 struct vmbus_gpadl_created {
183 /** Message header */
184 struct vmbus_message_header header;
189 /** Creation status */
191 } __attribute__ (( packed ));
193 /** VMBus "GPADL teardown" message */
194 struct vmbus_gpadl_teardown {
195 /** Message header */
196 struct vmbus_message_header header;
201 } __attribute__ (( packed ));
203 /** VMBus "GPADL torndown" message */
204 struct vmbus_gpadl_torndown {
205 /** Message header */
206 struct vmbus_message_header header;
209 } __attribute__ (( packed ));
211 /** VMBus "initiate contact" message */
212 struct vmbus_initiate_contact {
213 /** Message header */
214 struct vmbus_message_header header;
215 /** Requested version */
216 union vmbus_version version;
217 /** Target virtual CPU */
219 /** Interrupt page base address */
221 /** Parent to child monitor page base address */
223 /** Child to parent monitor page base address */
224 uint64_t monitor_out;
225 } __attribute__ (( packed ));
227 /** VMBus "version response" message */
228 struct vmbus_version_response {
229 /** Message header */
230 struct vmbus_message_header header;
231 /** Version is supported */
236 union vmbus_version version;
237 } __attribute__ (( packed ));
240 union vmbus_message {
241 /** Common message header */
242 struct vmbus_message_header header;
243 /** "Offer channel" message */
244 struct vmbus_offer_channel offer;
245 /** "Open channel" message */
246 struct vmbus_open_channel open;
247 /** "Open channel result" message */
248 struct vmbus_open_channel_result opened;
249 /** "Close channel" message */
250 struct vmbus_close_channel close;
251 /** "GPADL header" message */
252 struct vmbus_gpadl_header gpadlhdr;
253 /** "GPADL created" message */
254 struct vmbus_gpadl_created created;
255 /** "GPADL teardown" message */
256 struct vmbus_gpadl_teardown teardown;
257 /** "GPADL torndown" message */
258 struct vmbus_gpadl_torndown torndown;
259 /** "Initiate contact" message */
260 struct vmbus_initiate_contact initiate;
261 /** "Version response" message */
262 struct vmbus_version_response version;
265 /** VMBus packet header */
266 struct vmbus_packet_header {
269 /** Length of packet header (in quadwords) */
271 /** Length of packet (in quadwords) */
277 * This is an opaque token: we therefore treat it as
278 * native-endian and don't worry about byte-swapping.
281 } __attribute__ (( packed ));
283 /** VMBus packet types */
284 enum vmbus_packet_type {
285 VMBUS_DATA_INBAND = 6,
286 VMBUS_DATA_XFER_PAGES = 7,
287 VMBUS_DATA_GPA_DIRECT = 9,
288 VMBUS_CANCELLATION = 10,
289 VMBUS_COMPLETION = 11,
292 /** VMBus packet flags */
293 enum vmbus_packet_flags {
294 VMBUS_COMPLETION_REQUESTED = 0x0001,
297 /** VMBus GPA direct header */
298 struct vmbus_gpa_direct_header {
300 struct vmbus_packet_header header;
303 /** Number of range descriptors */
304 uint32_t range_count;
305 /** Range descriptors */
306 struct vmbus_gpa_range range[0];
307 } __attribute__ (( packed ));
309 /** VMBus transfer page range */
310 struct vmbus_xfer_page_range {
315 } __attribute__ (( packed ));
317 /** VMBus transfer page header */
318 struct vmbus_xfer_page_header {
320 struct vmbus_packet_header header;
323 /** Sender owns page set */
327 /** Number of range descriptors */
328 uint32_t range_count;
329 /** Range descriptors */
330 struct vmbus_xfer_page_range range[0];
331 } __attribute__ (( packed ));
333 /** Maximum expected size of VMBus packet header */
334 #define VMBUS_PACKET_MAX_HEADER_LEN 64
336 /** VMBus maximum-sized packet header */
337 union vmbus_packet_header_max {
339 struct vmbus_packet_header header;
340 /** GPA direct header */
341 struct vmbus_gpa_direct_header gpa;
342 /** Transfer page header */
343 struct vmbus_xfer_page_header xfer;
344 /** Padding to maximum supported size */
345 uint8_t padding[VMBUS_PACKET_MAX_HEADER_LEN];
346 } __attribute__ (( packed ));
348 /** VMBus packet footer */
349 struct vmbus_packet_footer {
352 /** Producer index of the first byte of the packet */
354 } __attribute__ (( packed ));
356 /** VMBus ring buffer
358 * This is the structure of the each of the ring buffers created when
359 * a VMBus channel is opened.
362 /** Producer index (modulo ring length) */
364 /** Consumer index (modulo ring length) */
366 /** Interrupt mask */
369 uint8_t reserved[4084];
370 /** Ring buffer contents */
372 } __attribute__ (( packed ));
374 /** VMBus interrupt page */
375 struct vmbus_interrupt {
376 /** Inbound interrupts */
377 uint8_t in[ PAGE_SIZE / 2 ];
378 /** Outbound interrupts */
379 uint8_t out[ PAGE_SIZE / 2 ];
380 } __attribute__ (( packed ));
382 /** A virtual machine bus */
384 /** Interrupt page */
385 struct vmbus_interrupt *intr;
386 /** Inbound notifications */
387 struct hv_monitor *monitor_in;
388 /** Outbound notifications */
389 struct hv_monitor *monitor_out;
390 /** Received message buffer */
391 const union vmbus_message *message;
396 /** VMBus channel operations */
397 struct vmbus_channel_operations {
399 * Handle received control packet
401 * @v vmdev VMBus device
402 * @v xid Transaction ID
404 * @v len Length of data
405 * @ret rc Return status code
407 int ( * recv_control ) ( struct vmbus_device *vmdev, uint64_t xid,
408 const void *data, size_t len );
410 * Handle received data packet
412 * @v vmdev VMBus device
413 * @v xid Transaction ID
415 * @v len Length of data
416 * @v list List of I/O buffers
417 * @ret rc Return status code
419 * This function takes ownership of the I/O buffer. It should
420 * eventually call vmbus_send_completion() to indicate to the
421 * host that the buffer can be reused.
423 int ( * recv_data ) ( struct vmbus_device *vmdev, uint64_t xid,
424 const void *data, size_t len,
425 struct list_head *list );
427 * Handle received completion packet
429 * @v vmdev VMBus device
430 * @v xid Transaction ID
432 * @v len Length of data
433 * @ret rc Return status code
435 int ( * recv_completion ) ( struct vmbus_device *vmdev, uint64_t xid,
436 const void *data, size_t len );
438 * Handle received cancellation packet
440 * @v vmdev VMBus device
441 * @v xid Transaction ID
442 * @ret rc Return status code
444 int ( * recv_cancellation ) ( struct vmbus_device *vmdev,
448 struct vmbus_xfer_pages;
450 /** VMBus transfer page set operations */
451 struct vmbus_xfer_pages_operations {
453 * Copy data from transfer page
455 * @v pages Transfer page set
456 * @v data Data buffer
457 * @v offset Offset within page set
458 * @v len Length within page set
459 * @ret rc Return status code
461 int ( * copy ) ( struct vmbus_xfer_pages *pages, void *data,
462 size_t offset, size_t len );
465 /** VMBus transfer page set */
466 struct vmbus_xfer_pages {
467 /** List of all transfer page sets */
468 struct list_head list;
469 /** Page set ID (in protocol byte order) */
471 /** Page set operations */
472 struct vmbus_xfer_pages_operations *op;
475 /** A VMBus device */
476 struct vmbus_device {
477 /** Generic iPXE device */
479 /** Hyper-V hypervisor */
480 struct hv_hypervisor *hv;
483 unsigned int channel;
485 unsigned int monitor;
488 * @v vmdev VMBus device
490 void ( * signal ) ( struct vmbus_device *vmdev );
492 /** Outbound ring buffer length */
494 /** Inbound ring buffer length */
496 /** Outbound ring buffer */
497 struct vmbus_ring *out;
498 /** Inbound ring buffer */
499 struct vmbus_ring *in;
500 /** Ring buffer GPADL ID */
503 /** Channel operations */
504 struct vmbus_channel_operations *op;
505 /** Maximum expected data packet length */
509 /** List of transfer page sets */
510 struct list_head pages;
513 struct vmbus_driver *driver;
514 /** Driver-private data */
518 /** A VMBus device driver */
519 struct vmbus_driver {
526 * @v vmdev VMBus device
527 * @ret rc Return status code
529 int ( * probe ) ( struct vmbus_device *vmdev );
532 * @v vmdev VMBus device
534 void ( * remove ) ( struct vmbus_device *vmdev );
537 /** VMBus device driver table */
538 #define VMBUS_DRIVERS __table ( struct vmbus_driver, "vmbus_drivers" )
540 /** Declare a VMBus device driver */
541 #define __vmbus_driver __table_entry ( VMBUS_DRIVERS, 01 )
544 * Set VMBus device driver-private data
546 * @v vmdev VMBus device
547 * @v priv Private data
549 static inline void vmbus_set_drvdata ( struct vmbus_device *vmdev, void *priv ){
554 * Get VMBus device driver-private data
556 * @v vmdev VMBus device
557 * @ret priv Private data
559 static inline void * vmbus_get_drvdata ( struct vmbus_device *vmdev ) {
563 /** Construct VMBus type */
564 #define VMBUS_TYPE( a, b, c, d, e0, e1, e2, e3, e4, e5 ) { \
566 cpu_to_le32 ( a ), cpu_to_le16 ( b ), \
567 cpu_to_le16 ( c ), cpu_to_be16 ( d ), \
568 { e0, e1, e2, e3, e4, e5 } \
572 * Check if data is present in ring buffer
574 * @v vmdev VMBus device
575 * @v has_data Data is present
577 static inline __attribute__ (( always_inline )) int
578 vmbus_has_data ( struct vmbus_device *vmdev ) {
580 return ( vmdev->in->prod != vmdev->in->cons );
584 * Register transfer page set
586 * @v vmdev VMBus device
587 * @v pages Transfer page set
588 * @ret rc Return status code
590 static inline __attribute__ (( always_inline )) int
591 vmbus_register_pages ( struct vmbus_device *vmdev,
592 struct vmbus_xfer_pages *pages ) {
594 list_add ( &pages->list, &vmdev->pages );
599 * Unregister transfer page set
601 * @v vmdev VMBus device
602 * @v pages Transfer page set
604 static inline __attribute__ (( always_inline )) void
605 vmbus_unregister_pages ( struct vmbus_device *vmdev,
606 struct vmbus_xfer_pages *pages ) {
608 list_check_contains_entry ( pages, &vmdev->pages, list );
609 list_del ( &pages->list );
612 extern int vmbus_establish_gpadl ( struct vmbus_device *vmdev, userptr_t data,
614 extern int vmbus_gpadl_teardown ( struct vmbus_device *vmdev,
615 unsigned int gpadl );
616 extern int vmbus_open ( struct vmbus_device *vmdev,
617 struct vmbus_channel_operations *op,
618 size_t out_len, size_t in_len, size_t mtu );
619 extern void vmbus_close ( struct vmbus_device *vmdev );
620 extern int vmbus_send_control ( struct vmbus_device *vmdev, uint64_t xid,
621 const void *data, size_t len );
622 extern int vmbus_send_data ( struct vmbus_device *vmdev, uint64_t xid,
623 const void *data, size_t len,
624 struct io_buffer *iobuf );
625 extern int vmbus_send_completion ( struct vmbus_device *vmdev, uint64_t xid,
626 const void *data, size_t len );
627 extern int vmbus_send_cancellation ( struct vmbus_device *vmdev, uint64_t xid );
628 extern int vmbus_poll ( struct vmbus_device *vmdev );
629 extern void vmbus_dump_channel ( struct vmbus_device *vmdev );
631 extern int vmbus_probe ( struct hv_hypervisor *hv, struct device *parent );
632 extern void vmbus_remove ( struct hv_hypervisor *hv, struct device *parent );
634 #endif /* _IPXE_VMBUS_H */