vmbus_bufring.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #ifndef _VMBUS_BUF_H_
  3. #define _VMBUS_BUF_H_
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #define __packed __attribute__((__packed__))
  7. #define unlikely(x) __builtin_expect(!!(x), 0)
  8. #define ICMSGHDRFLAG_TRANSACTION 1
  9. #define ICMSGHDRFLAG_REQUEST 2
  10. #define ICMSGHDRFLAG_RESPONSE 4
  11. #define IC_VERSION_NEGOTIATION_MAX_VER_COUNT 100
  12. #define ICMSG_HDR (sizeof(struct vmbuspipe_hdr) + sizeof(struct icmsg_hdr))
  13. #define ICMSG_NEGOTIATE_PKT_SIZE(icframe_vercnt, icmsg_vercnt) \
  14. (ICMSG_HDR + sizeof(struct icmsg_negotiate) + \
  15. (((icframe_vercnt) + (icmsg_vercnt)) * sizeof(struct ic_version)))
  16. /*
  17. * Channel packets
  18. */
  19. /* Channel packet flags */
  20. #define VMBUS_CHANPKT_TYPE_INBAND 0x0006
  21. #define VMBUS_CHANPKT_TYPE_RXBUF 0x0007
  22. #define VMBUS_CHANPKT_TYPE_GPA 0x0009
  23. #define VMBUS_CHANPKT_TYPE_COMP 0x000b
  24. #define VMBUS_CHANPKT_FLAG_NONE 0
  25. #define VMBUS_CHANPKT_FLAG_RC 0x0001 /* report completion */
  26. #define VMBUS_CHANPKT_SIZE_SHIFT 3
  27. #define VMBUS_CHANPKT_SIZE_ALIGN BIT(VMBUS_CHANPKT_SIZE_SHIFT)
  28. #define VMBUS_CHANPKT_HLEN_MIN \
  29. (sizeof(struct vmbus_chanpkt_hdr) >> VMBUS_CHANPKT_SIZE_SHIFT)
  30. /*
  31. * Buffer ring
  32. */
  33. struct vmbus_bufring {
  34. volatile uint32_t windex;
  35. volatile uint32_t rindex;
  36. /*
  37. * Interrupt mask {0,1}
  38. *
  39. * For TX bufring, host set this to 1, when it is processing
  40. * the TX bufring, so that we can safely skip the TX event
  41. * notification to host.
  42. *
  43. * For RX bufring, once this is set to 1 by us, host will not
  44. * further dispatch interrupts to us, even if there are data
  45. * pending on the RX bufring. This effectively disables the
  46. * interrupt of the channel to which this RX bufring is attached.
  47. */
  48. volatile uint32_t imask;
  49. /*
  50. * Win8 uses some of the reserved bits to implement
  51. * interrupt driven flow management. On the send side
  52. * we can request that the receiver interrupt the sender
  53. * when the ring transitions from being full to being able
  54. * to handle a message of size "pending_send_sz".
  55. *
  56. * Add necessary state for this enhancement.
  57. */
  58. volatile uint32_t pending_send;
  59. uint32_t reserved1[12];
  60. union {
  61. struct {
  62. uint32_t feat_pending_send_sz:1;
  63. };
  64. uint32_t value;
  65. } feature_bits;
  66. /* Pad it to rte_mem_page_size() so that data starts on page boundary */
  67. uint8_t reserved2[4028];
  68. /*
  69. * Ring data starts here + RingDataStartOffset
  70. * !!! DO NOT place any fields below this !!!
  71. */
  72. uint8_t data[];
  73. } __packed;
  74. struct vmbus_br {
  75. struct vmbus_bufring *vbr;
  76. uint32_t dsize;
  77. uint32_t windex; /* next available location */
  78. };
  79. struct vmbus_chanpkt_hdr {
  80. uint16_t type; /* VMBUS_CHANPKT_TYPE_ */
  81. uint16_t hlen; /* header len, in 8 bytes */
  82. uint16_t tlen; /* total len, in 8 bytes */
  83. uint16_t flags; /* VMBUS_CHANPKT_FLAG_ */
  84. uint64_t xactid;
  85. } __packed;
  86. struct vmbus_chanpkt {
  87. struct vmbus_chanpkt_hdr hdr;
  88. } __packed;
  89. struct vmbuspipe_hdr {
  90. unsigned int flags;
  91. unsigned int msgsize;
  92. } __packed;
  93. struct ic_version {
  94. unsigned short major;
  95. unsigned short minor;
  96. } __packed;
  97. struct icmsg_negotiate {
  98. unsigned short icframe_vercnt;
  99. unsigned short icmsg_vercnt;
  100. unsigned int reserved;
  101. struct ic_version icversion_data[]; /* any size array */
  102. } __packed;
  103. struct icmsg_hdr {
  104. struct ic_version icverframe;
  105. unsigned short icmsgtype;
  106. struct ic_version icvermsg;
  107. unsigned short icmsgsize;
  108. unsigned int status;
  109. unsigned char ictransaction_id;
  110. unsigned char icflags;
  111. unsigned char reserved[2];
  112. } __packed;
  113. int rte_vmbus_chan_recv_raw(struct vmbus_br *rxbr, void *data, uint32_t *len);
  114. int rte_vmbus_chan_send(struct vmbus_br *txbr, uint16_t type, void *data,
  115. uint32_t dlen, uint32_t flags);
  116. void vmbus_br_setup(struct vmbus_br *br, void *buf, unsigned int blen);
  117. void *vmbus_uio_map(int *fd, int size);
  118. /* Amount of space available for write */
  119. static inline uint32_t vmbus_br_availwrite(const struct vmbus_br *br, uint32_t windex)
  120. {
  121. uint32_t rindex = br->vbr->rindex;
  122. if (windex >= rindex)
  123. return br->dsize - (windex - rindex);
  124. else
  125. return rindex - windex;
  126. }
  127. static inline uint32_t vmbus_br_availread(const struct vmbus_br *br)
  128. {
  129. return br->dsize - vmbus_br_availwrite(br, br->vbr->windex);
  130. }
  131. #endif /* !_VMBUS_BUF_H_ */