Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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