You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vmbus.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #ifndef _IPXE_VMBUS_H
  2. #define _IPXE_VMBUS_H
  3. /** @file
  4. *
  5. * Hyper-V virtual machine bus
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <byteswap.h>
  10. #include <ipxe/uuid.h>
  11. #include <ipxe/device.h>
  12. #include <ipxe/tables.h>
  13. #include <ipxe/uaccess.h>
  14. #include <ipxe/iobuf.h>
  15. #include <ipxe/hyperv.h>
  16. /** VMBus message connection ID */
  17. #define VMBUS_MESSAGE_ID 1
  18. /** VMBus event connection ID */
  19. #define VMBUS_EVENT_ID 2
  20. /** VMBus message type */
  21. #define VMBUS_MESSAGE_TYPE 1
  22. /** VMBus message synthetic interrupt */
  23. #define VMBUS_MESSAGE_SINT 2
  24. /** VMBus version number */
  25. union vmbus_version {
  26. /** Raw version */
  27. uint32_t raw;
  28. /** Major/minor version */
  29. struct {
  30. /** Minor version */
  31. uint16_t minor;
  32. /** Major version */
  33. uint16_t major;
  34. };
  35. } __attribute__ (( packed ));
  36. /** Known VMBus protocol versions */
  37. enum vmbus_raw_version {
  38. /** Windows Server 2008 */
  39. VMBUS_VERSION_WS2008 = ( ( 0 << 16 ) | ( 13 << 0 ) ),
  40. /** Windows 7 */
  41. VMBUS_VERSION_WIN7 = ( ( 1 << 16 ) | ( 1 << 0 ) ),
  42. /** Windows 8 */
  43. VMBUS_VERSION_WIN8 = ( ( 2 << 16 ) | ( 4 << 0 ) ),
  44. /** Windows 8.1 */
  45. VMBUS_VERSION_WIN8_1 = ( ( 3 << 16 ) | ( 0 << 0 ) ),
  46. };
  47. /** Guest physical address range descriptor */
  48. struct vmbus_gpa_range {
  49. /** Byte count */
  50. uint32_t len;
  51. /** Starting byte offset */
  52. uint32_t offset;
  53. /** Page frame numbers
  54. *
  55. * The length of this array is implied by the byte count and
  56. * starting offset.
  57. */
  58. uint64_t pfn[0];
  59. } __attribute__ (( packed ));
  60. /** VMBus message header */
  61. struct vmbus_message_header {
  62. /** Message type */
  63. uint32_t type;
  64. /** Reserved */
  65. uint32_t reserved;
  66. } __attribute__ (( packed ));
  67. /** VMBus message types */
  68. enum vmbus_message_type {
  69. VMBUS_OFFER_CHANNEL = 1,
  70. VMBUS_REQUEST_OFFERS = 3,
  71. VMBUS_ALL_OFFERS_DELIVERED = 4,
  72. VMBUS_OPEN_CHANNEL = 5,
  73. VMBUS_OPEN_CHANNEL_RESULT = 6,
  74. VMBUS_CLOSE_CHANNEL = 7,
  75. VMBUS_GPADL_HEADER = 8,
  76. VMBUS_GPADL_CREATED = 10,
  77. VMBUS_GPADL_TEARDOWN = 11,
  78. VMBUS_GPADL_TORNDOWN = 12,
  79. VMBUS_INITIATE_CONTACT = 14,
  80. VMBUS_VERSION_RESPONSE = 15,
  81. VMBUS_UNLOAD = 16,
  82. VMBUS_UNLOAD_RESPONSE = 17,
  83. };
  84. /** VMBus "offer channel" message */
  85. struct vmbus_offer_channel {
  86. /** Message header */
  87. struct vmbus_message_header header;
  88. /** Channel type */
  89. union uuid type;
  90. /** Channel instance */
  91. union uuid instance;
  92. /** Reserved */
  93. uint8_t reserved_a[16];
  94. /** Flags */
  95. uint16_t flags;
  96. /** Reserved */
  97. uint8_t reserved_b[2];
  98. /** User data */
  99. uint8_t data[120];
  100. /** Reserved */
  101. uint8_t reserved_c[4];
  102. /** Channel ID */
  103. uint32_t channel;
  104. /** Monitor ID */
  105. uint8_t monitor;
  106. /** Monitor exists */
  107. uint8_t monitored;
  108. /** Reserved */
  109. uint8_t reserved[2];
  110. /** Connection ID */
  111. uint32_t connection;
  112. } __attribute__ (( packed ));
  113. /** VMBus "open channel" message */
  114. struct vmbus_open_channel {
  115. /** Message header */
  116. struct vmbus_message_header header;
  117. /** Channel ID */
  118. uint32_t channel;
  119. /** Open ID */
  120. uint32_t id;
  121. /** Ring buffer GPADL ID */
  122. uint32_t gpadl;
  123. /** Reserved */
  124. uint32_t reserved;
  125. /** Outbound ring buffer size (in pages) */
  126. uint32_t out_pages;
  127. /** User-specific data */
  128. uint8_t data[120];
  129. } __attribute__ (( packed ));
  130. /** VMBus "open channel result" message */
  131. struct vmbus_open_channel_result {
  132. /** Message header */
  133. struct vmbus_message_header header;
  134. /** Channel ID */
  135. uint32_t channel;
  136. /** Open ID */
  137. uint32_t id;
  138. /** Status */
  139. uint32_t status;
  140. } __attribute__ (( packed ));
  141. /** VMBus "close channel" message */
  142. struct vmbus_close_channel {
  143. /** Message header */
  144. struct vmbus_message_header header;
  145. /** Channel ID */
  146. uint32_t channel;
  147. } __attribute__ (( packed ));
  148. /** VMBus "GPADL header" message */
  149. struct vmbus_gpadl_header {
  150. /** Message header */
  151. struct vmbus_message_header header;
  152. /** Channel ID */
  153. uint32_t channel;
  154. /** GPADL ID */
  155. uint32_t gpadl;
  156. /** Length of range descriptors */
  157. uint16_t range_len;
  158. /** Number of range descriptors */
  159. uint16_t range_count;
  160. /** Range descriptors */
  161. struct vmbus_gpa_range range[0];
  162. } __attribute__ (( packed ));
  163. /** VMBus "GPADL created" message */
  164. struct vmbus_gpadl_created {
  165. /** Message header */
  166. struct vmbus_message_header header;
  167. /** Channel ID */
  168. uint32_t channel;
  169. /** GPADL ID */
  170. uint32_t gpadl;
  171. /** Creation status */
  172. uint32_t status;
  173. } __attribute__ (( packed ));
  174. /** VMBus "GPADL teardown" message */
  175. struct vmbus_gpadl_teardown {
  176. /** Message header */
  177. struct vmbus_message_header header;
  178. /** Channel ID */
  179. uint32_t channel;
  180. /** GPADL ID */
  181. uint32_t gpadl;
  182. } __attribute__ (( packed ));
  183. /** VMBus "GPADL torndown" message */
  184. struct vmbus_gpadl_torndown {
  185. /** Message header */
  186. struct vmbus_message_header header;
  187. /** GPADL ID */
  188. uint32_t gpadl;
  189. } __attribute__ (( packed ));
  190. /** VMBus "initiate contact" message */
  191. struct vmbus_initiate_contact {
  192. /** Message header */
  193. struct vmbus_message_header header;
  194. /** Requested version */
  195. union vmbus_version version;
  196. /** Target virtual CPU */
  197. uint32_t vcpu;
  198. /** Interrupt page base address */
  199. uint64_t intr;
  200. /** Parent to child monitor page base address */
  201. uint64_t monitor_in;
  202. /** Child to parent monitor page base address */
  203. uint64_t monitor_out;
  204. } __attribute__ (( packed ));
  205. /** VMBus "version response" message */
  206. struct vmbus_version_response {
  207. /** Message header */
  208. struct vmbus_message_header header;
  209. /** Version is supported */
  210. uint8_t supported;
  211. /** Reserved */
  212. uint8_t reserved[3];
  213. /** Version */
  214. union vmbus_version version;
  215. } __attribute__ (( packed ));
  216. /** VMBus message */
  217. union vmbus_message {
  218. /** Common message header */
  219. struct vmbus_message_header header;
  220. /** "Offer channel" message */
  221. struct vmbus_offer_channel offer;
  222. /** "Open channel" message */
  223. struct vmbus_open_channel open;
  224. /** "Open channel result" message */
  225. struct vmbus_open_channel_result opened;
  226. /** "Close channel" message */
  227. struct vmbus_close_channel close;
  228. /** "GPADL header" message */
  229. struct vmbus_gpadl_header gpadlhdr;
  230. /** "GPADL created" message */
  231. struct vmbus_gpadl_created created;
  232. /** "GPADL teardown" message */
  233. struct vmbus_gpadl_teardown teardown;
  234. /** "GPADL torndown" message */
  235. struct vmbus_gpadl_torndown torndown;
  236. /** "Initiate contact" message */
  237. struct vmbus_initiate_contact initiate;
  238. /** "Version response" message */
  239. struct vmbus_version_response version;
  240. };
  241. /** VMBus packet header */
  242. struct vmbus_packet_header {
  243. /** Type */
  244. uint16_t type;
  245. /** Length of packet header (in quadwords) */
  246. uint16_t hdr_qlen;
  247. /** Length of packet (in quadwords) */
  248. uint16_t qlen;
  249. /** Flags */
  250. uint16_t flags;
  251. /** Transaction ID
  252. *
  253. * This is an opaque token: we therefore treat it as
  254. * native-endian and don't worry about byte-swapping.
  255. */
  256. uint64_t xid;
  257. } __attribute__ (( packed ));
  258. /** VMBus packet types */
  259. enum vmbus_packet_type {
  260. VMBUS_DATA_INBAND = 6,
  261. VMBUS_DATA_XFER_PAGES = 7,
  262. VMBUS_DATA_GPA_DIRECT = 9,
  263. VMBUS_CANCELLATION = 10,
  264. VMBUS_COMPLETION = 11,
  265. };
  266. /** VMBus packet flags */
  267. enum vmbus_packet_flags {
  268. VMBUS_COMPLETION_REQUESTED = 0x0001,
  269. };
  270. /** VMBus GPA direct header */
  271. struct vmbus_gpa_direct_header {
  272. /** Packet header */
  273. struct vmbus_packet_header header;
  274. /** Reserved */
  275. uint32_t reserved;
  276. /** Number of range descriptors */
  277. uint32_t range_count;
  278. /** Range descriptors */
  279. struct vmbus_gpa_range range[0];
  280. } __attribute__ (( packed ));
  281. /** VMBus transfer page range */
  282. struct vmbus_xfer_page_range {
  283. /** Length */
  284. uint32_t len;
  285. /** Offset */
  286. uint32_t offset;
  287. } __attribute__ (( packed ));
  288. /** VMBus transfer page header */
  289. struct vmbus_xfer_page_header {
  290. /** Packet header */
  291. struct vmbus_packet_header header;
  292. /** Page set ID */
  293. uint16_t pageset;
  294. /** Sender owns page set */
  295. uint8_t owner;
  296. /** Reserved */
  297. uint8_t reserved;
  298. /** Number of range descriptors */
  299. uint32_t range_count;
  300. /** Range descriptors */
  301. struct vmbus_xfer_page_range range[0];
  302. } __attribute__ (( packed ));
  303. /** Maximum expected size of VMBus packet header */
  304. #define VMBUS_PACKET_MAX_HEADER_LEN 64
  305. /** VMBus maximum-sized packet header */
  306. union vmbus_packet_header_max {
  307. /** Common header */
  308. struct vmbus_packet_header header;
  309. /** GPA direct header */
  310. struct vmbus_gpa_direct_header gpa;
  311. /** Transfer page header */
  312. struct vmbus_xfer_page_header xfer;
  313. /** Padding to maximum supported size */
  314. uint8_t padding[VMBUS_PACKET_MAX_HEADER_LEN];
  315. } __attribute__ (( packed ));
  316. /** VMBus packet footer */
  317. struct vmbus_packet_footer {
  318. /** Reserved */
  319. uint32_t reserved;
  320. /** Producer index of the first byte of the packet */
  321. uint32_t prod;
  322. } __attribute__ (( packed ));
  323. /** VMBus ring buffer
  324. *
  325. * This is the structure of the each of the ring buffers created when
  326. * a VMBus channel is opened.
  327. */
  328. struct vmbus_ring {
  329. /** Producer index (modulo ring length) */
  330. uint32_t prod;
  331. /** Consumer index (modulo ring length) */
  332. uint32_t cons;
  333. /** Interrupt mask */
  334. uint32_t intr_mask;
  335. /** Reserved */
  336. uint8_t reserved[4084];
  337. /** Ring buffer contents */
  338. uint8_t data[0];
  339. } __attribute__ (( packed ));
  340. /** VMBus interrupt page */
  341. struct vmbus_interrupt {
  342. /** Inbound interrupts */
  343. uint8_t in[ PAGE_SIZE / 2 ];
  344. /** Outbound interrupts */
  345. uint8_t out[ PAGE_SIZE / 2 ];
  346. } __attribute__ (( packed ));
  347. /** A virtual machine bus */
  348. struct vmbus {
  349. /** Interrupt page */
  350. struct vmbus_interrupt *intr;
  351. /** Inbound notifications */
  352. struct hv_monitor *monitor_in;
  353. /** Outbound notifications */
  354. struct hv_monitor *monitor_out;
  355. /** Received message buffer */
  356. const union vmbus_message *message;
  357. };
  358. struct vmbus_device;
  359. /** VMBus channel operations */
  360. struct vmbus_channel_operations {
  361. /**
  362. * Handle received control packet
  363. *
  364. * @v vmdev VMBus device
  365. * @v xid Transaction ID
  366. * @v data Data
  367. * @v len Length of data
  368. * @ret rc Return status code
  369. */
  370. int ( * recv_control ) ( struct vmbus_device *vmdev, uint64_t xid,
  371. const void *data, size_t len );
  372. /**
  373. * Handle received data packet
  374. *
  375. * @v vmdev VMBus device
  376. * @v xid Transaction ID
  377. * @v data Data
  378. * @v len Length of data
  379. * @v list List of I/O buffers
  380. * @ret rc Return status code
  381. *
  382. * This function takes ownership of the I/O buffer. It should
  383. * eventually call vmbus_send_completion() to indicate to the
  384. * host that the buffer can be reused.
  385. */
  386. int ( * recv_data ) ( struct vmbus_device *vmdev, uint64_t xid,
  387. const void *data, size_t len,
  388. struct list_head *list );
  389. /**
  390. * Handle received completion packet
  391. *
  392. * @v vmdev VMBus device
  393. * @v xid Transaction ID
  394. * @v data Data
  395. * @v len Length of data
  396. * @ret rc Return status code
  397. */
  398. int ( * recv_completion ) ( struct vmbus_device *vmdev, uint64_t xid,
  399. const void *data, size_t len );
  400. /**
  401. * Handle received cancellation packet
  402. *
  403. * @v vmdev VMBus device
  404. * @v xid Transaction ID
  405. * @ret rc Return status code
  406. */
  407. int ( * recv_cancellation ) ( struct vmbus_device *vmdev,
  408. uint64_t xid );
  409. };
  410. struct vmbus_xfer_pages;
  411. /** VMBus transfer page set operations */
  412. struct vmbus_xfer_pages_operations {
  413. /**
  414. * Copy data from transfer page
  415. *
  416. * @v pages Transfer page set
  417. * @v data Data buffer
  418. * @v offset Offset within page set
  419. * @v len Length within page set
  420. * @ret rc Return status code
  421. */
  422. int ( * copy ) ( struct vmbus_xfer_pages *pages, void *data,
  423. size_t offset, size_t len );
  424. };
  425. /** VMBus transfer page set */
  426. struct vmbus_xfer_pages {
  427. /** List of all transfer page sets */
  428. struct list_head list;
  429. /** Page set ID (in protocol byte order) */
  430. uint16_t pageset;
  431. /** Page set operations */
  432. struct vmbus_xfer_pages_operations *op;
  433. };
  434. /** A VMBus device */
  435. struct vmbus_device {
  436. /** Generic iPXE device */
  437. struct device dev;
  438. /** Hyper-V hypervisor */
  439. struct hv_hypervisor *hv;
  440. /** Channel instance */
  441. union uuid instance;
  442. /** Channel ID */
  443. unsigned int channel;
  444. /** Monitor ID */
  445. unsigned int monitor;
  446. /** Signal channel
  447. *
  448. * @v vmdev VMBus device
  449. */
  450. void ( * signal ) ( struct vmbus_device *vmdev );
  451. /** Outbound ring buffer length */
  452. uint32_t out_len;
  453. /** Inbound ring buffer length */
  454. uint32_t in_len;
  455. /** Outbound ring buffer */
  456. struct vmbus_ring *out;
  457. /** Inbound ring buffer */
  458. struct vmbus_ring *in;
  459. /** Ring buffer GPADL ID */
  460. unsigned int gpadl;
  461. /** Channel operations */
  462. struct vmbus_channel_operations *op;
  463. /** Maximum expected data packet length */
  464. size_t mtu;
  465. /** Packet buffer */
  466. void *packet;
  467. /** List of transfer page sets */
  468. struct list_head pages;
  469. /** Driver */
  470. struct vmbus_driver *driver;
  471. /** Driver-private data */
  472. void *priv;
  473. };
  474. /** A VMBus device driver */
  475. struct vmbus_driver {
  476. /** Name */
  477. const char *name;
  478. /** Device type */
  479. union uuid type;
  480. /** Probe device
  481. *
  482. * @v vmdev VMBus device
  483. * @ret rc Return status code
  484. */
  485. int ( * probe ) ( struct vmbus_device *vmdev );
  486. /** Reset device
  487. *
  488. * @v vmdev VMBus device
  489. * @ret rc Return status code
  490. */
  491. int ( * reset ) ( struct vmbus_device *vmdev );
  492. /** Remove device
  493. *
  494. * @v vmdev VMBus device
  495. */
  496. void ( * remove ) ( struct vmbus_device *vmdev );
  497. };
  498. /** VMBus device driver table */
  499. #define VMBUS_DRIVERS __table ( struct vmbus_driver, "vmbus_drivers" )
  500. /** Declare a VMBus device driver */
  501. #define __vmbus_driver __table_entry ( VMBUS_DRIVERS, 01 )
  502. /**
  503. * Set VMBus device driver-private data
  504. *
  505. * @v vmdev VMBus device
  506. * @v priv Private data
  507. */
  508. static inline void vmbus_set_drvdata ( struct vmbus_device *vmdev, void *priv ){
  509. vmdev->priv = priv;
  510. }
  511. /**
  512. * Get VMBus device driver-private data
  513. *
  514. * @v vmdev VMBus device
  515. * @ret priv Private data
  516. */
  517. static inline void * vmbus_get_drvdata ( struct vmbus_device *vmdev ) {
  518. return vmdev->priv;
  519. }
  520. /** Construct VMBus type */
  521. #define VMBUS_TYPE( a, b, c, d, e0, e1, e2, e3, e4, e5 ) { \
  522. .canonical = { \
  523. cpu_to_le32 ( a ), cpu_to_le16 ( b ), \
  524. cpu_to_le16 ( c ), cpu_to_be16 ( d ), \
  525. { e0, e1, e2, e3, e4, e5 } \
  526. } }
  527. /**
  528. * Check if data is present in ring buffer
  529. *
  530. * @v vmdev VMBus device
  531. * @v has_data Data is present
  532. */
  533. static inline __attribute__ (( always_inline )) int
  534. vmbus_has_data ( struct vmbus_device *vmdev ) {
  535. return ( vmdev->in->prod != vmdev->in->cons );
  536. }
  537. /**
  538. * Register transfer page set
  539. *
  540. * @v vmdev VMBus device
  541. * @v pages Transfer page set
  542. * @ret rc Return status code
  543. */
  544. static inline __attribute__ (( always_inline )) int
  545. vmbus_register_pages ( struct vmbus_device *vmdev,
  546. struct vmbus_xfer_pages *pages ) {
  547. list_add ( &pages->list, &vmdev->pages );
  548. return 0;
  549. }
  550. /**
  551. * Unregister transfer page set
  552. *
  553. * @v vmdev VMBus device
  554. * @v pages Transfer page set
  555. */
  556. static inline __attribute__ (( always_inline )) void
  557. vmbus_unregister_pages ( struct vmbus_device *vmdev,
  558. struct vmbus_xfer_pages *pages ) {
  559. list_check_contains_entry ( pages, &vmdev->pages, list );
  560. list_del ( &pages->list );
  561. }
  562. extern unsigned int vmbus_obsolete_gpadl;
  563. /**
  564. * Check if GPADL is obsolete
  565. *
  566. * @v gpadl GPADL ID
  567. * @v is_obsolete GPADL ID is obsolete
  568. *
  569. * Check if GPADL is obsolete (i.e. was created before the most recent
  570. * Hyper-V reset).
  571. */
  572. static inline __attribute__ (( always_inline )) int
  573. vmbus_gpadl_is_obsolete ( unsigned int gpadl ) {
  574. return ( gpadl <= vmbus_obsolete_gpadl );
  575. }
  576. extern int vmbus_establish_gpadl ( struct vmbus_device *vmdev, userptr_t data,
  577. size_t len );
  578. extern int vmbus_gpadl_teardown ( struct vmbus_device *vmdev,
  579. unsigned int gpadl );
  580. extern int vmbus_open ( struct vmbus_device *vmdev,
  581. struct vmbus_channel_operations *op,
  582. size_t out_len, size_t in_len, size_t mtu );
  583. extern void vmbus_close ( struct vmbus_device *vmdev );
  584. extern int vmbus_send_control ( struct vmbus_device *vmdev, uint64_t xid,
  585. const void *data, size_t len );
  586. extern int vmbus_send_data ( struct vmbus_device *vmdev, uint64_t xid,
  587. const void *data, size_t len,
  588. struct io_buffer *iobuf );
  589. extern int vmbus_send_completion ( struct vmbus_device *vmdev, uint64_t xid,
  590. const void *data, size_t len );
  591. extern int vmbus_send_cancellation ( struct vmbus_device *vmdev, uint64_t xid );
  592. extern int vmbus_poll ( struct vmbus_device *vmdev );
  593. extern void vmbus_dump_channel ( struct vmbus_device *vmdev );
  594. extern int vmbus_probe ( struct hv_hypervisor *hv, struct device *parent );
  595. extern int vmbus_reset ( struct hv_hypervisor *hv, struct device *parent );
  596. extern void vmbus_remove ( struct hv_hypervisor *hv, struct device *parent );
  597. #endif /* _IPXE_VMBUS_H */