vmbus.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * QEMU Hyper-V VMBus
  3. *
  4. * Copyright (c) 2017-2018 Virtuozzo International GmbH.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #ifndef HW_HYPERV_VMBUS_H
  10. #define HW_HYPERV_VMBUS_H
  11. #include "system/system.h"
  12. #include "system/dma.h"
  13. #include "hw/qdev-core.h"
  14. #include "migration/vmstate.h"
  15. #include "hw/hyperv/vmbus-proto.h"
  16. #include "qemu/uuid.h"
  17. #include "qom/object.h"
  18. #define TYPE_VMBUS_DEVICE "vmbus-dev"
  19. OBJECT_DECLARE_TYPE(VMBusDevice, VMBusDeviceClass,
  20. VMBUS_DEVICE)
  21. #define TYPE_VMBUS "vmbus"
  22. OBJECT_DECLARE_SIMPLE_TYPE(VMBus, VMBUS)
  23. /*
  24. * Object wrapping a GPADL -- GPA Descriptor List -- an array of guest physical
  25. * pages, to be used for various buffers shared between the host and the guest.
  26. */
  27. typedef struct VMBusGpadl VMBusGpadl;
  28. /*
  29. * VMBus channel -- a pair of ring buffers for either direction, placed within
  30. * one GPADL, and the associated notification means.
  31. */
  32. typedef struct VMBusChannel VMBusChannel;
  33. /*
  34. * Base class for VMBus devices. Includes one or more channels. Identified by
  35. * class GUID and instance GUID.
  36. */
  37. typedef void(*VMBusChannelNotifyCb)(struct VMBusChannel *chan);
  38. struct VMBusDeviceClass {
  39. DeviceClass parent;
  40. QemuUUID classid;
  41. QemuUUID instanceid; /* Fixed UUID for singleton devices */
  42. uint16_t channel_flags;
  43. uint16_t mmio_size_mb;
  44. /* Extensions to standard device callbacks */
  45. void (*vmdev_realize)(VMBusDevice *vdev, Error **errp);
  46. void (*vmdev_unrealize)(VMBusDevice *vdev);
  47. void (*vmdev_reset)(VMBusDevice *vdev);
  48. /*
  49. * Calculate the number of channels based on the device properties. Called
  50. * at realize time.
  51. **/
  52. uint16_t (*num_channels)(VMBusDevice *vdev);
  53. /*
  54. * Device-specific actions to complete the otherwise successful process of
  55. * opening a channel.
  56. * Return 0 on success, -errno on failure.
  57. */
  58. int (*open_channel)(VMBusChannel *chan);
  59. /*
  60. * Device-specific actions to perform before closing a channel.
  61. */
  62. void (*close_channel)(VMBusChannel *chan);
  63. /*
  64. * Main device worker; invoked in response to notifications from either
  65. * side, when there's work to do with the data in the channel ring buffers.
  66. */
  67. VMBusChannelNotifyCb chan_notify_cb;
  68. };
  69. struct VMBusDevice {
  70. DeviceState parent;
  71. QemuUUID instanceid;
  72. uint16_t num_channels;
  73. VMBusChannel *channels;
  74. AddressSpace *dma_as;
  75. };
  76. extern const VMStateDescription vmstate_vmbus_dev;
  77. /*
  78. * A unit of work parsed out of a message in the receive (i.e. guest->host)
  79. * ring buffer of a channel. It's supposed to be subclassed (through
  80. * embedding) by the specific devices.
  81. */
  82. typedef struct VMBusChanReq {
  83. VMBusChannel *chan;
  84. uint16_t pkt_type;
  85. uint32_t msglen;
  86. void *msg;
  87. uint64_t transaction_id;
  88. bool need_comp;
  89. QEMUSGList sgl;
  90. } VMBusChanReq;
  91. VMBusDevice *vmbus_channel_device(VMBusChannel *chan);
  92. VMBusChannel *vmbus_device_channel(VMBusDevice *dev, uint32_t chan_idx);
  93. uint32_t vmbus_channel_idx(VMBusChannel *chan);
  94. bool vmbus_channel_is_open(VMBusChannel *chan);
  95. /*
  96. * Notify (on guest's behalf) the host side of the channel that there's data in
  97. * the ringbuffer to process.
  98. */
  99. void vmbus_channel_notify_host(VMBusChannel *chan);
  100. /*
  101. * Reserve space for a packet in the send (i.e. host->guest) ringbuffer. If
  102. * there isn't enough room, indicate that to the guest, to be notified when it
  103. * becomes available.
  104. * Return 0 on success, negative errno on failure.
  105. * The ringbuffer indices are NOT updated, the requested space indicator may.
  106. */
  107. int vmbus_channel_reserve(VMBusChannel *chan,
  108. uint32_t desclen, uint32_t msglen);
  109. /*
  110. * Send a packet to the guest. The space for the packet MUST be reserved
  111. * first.
  112. * Return total number of bytes placed in the send ringbuffer on success,
  113. * negative errno on failure.
  114. * The ringbuffer indices are updated on success, and the guest is signaled if
  115. * needed.
  116. */
  117. ssize_t vmbus_channel_send(VMBusChannel *chan, uint16_t pkt_type,
  118. void *desc, uint32_t desclen,
  119. void *msg, uint32_t msglen,
  120. bool need_comp, uint64_t transaction_id);
  121. /*
  122. * Prepare to fetch a batch of packets from the receive ring buffer.
  123. * Return 0 on success, negative errno on failure.
  124. */
  125. int vmbus_channel_recv_start(VMBusChannel *chan);
  126. /*
  127. * Shortcut for a common case of sending a simple completion packet with no
  128. * auxiliary descriptors.
  129. */
  130. ssize_t vmbus_channel_send_completion(VMBusChanReq *req,
  131. void *msg, uint32_t msglen);
  132. /*
  133. * Peek at the receive (i.e. guest->host) ring buffer and extract a unit of
  134. * work (a device-specific subclass of VMBusChanReq) from a packet if there's
  135. * one.
  136. * Return an allocated buffer, containing the request of @size with filled
  137. * VMBusChanReq at the beginning, followed by the message payload, or NULL on
  138. * failure.
  139. * The ringbuffer indices are NOT updated, nor is the private copy of the read
  140. * index.
  141. */
  142. void *vmbus_channel_recv_peek(VMBusChannel *chan, uint32_t size);
  143. /*
  144. * Update the private copy of the read index once the preceding peek is deemed
  145. * successful.
  146. * The ringbuffer indices are NOT updated.
  147. */
  148. void vmbus_channel_recv_pop(VMBusChannel *chan);
  149. /*
  150. * Propagate the private copy of the read index into the receive ring buffer,
  151. * and thus complete the reception of a series of packets. Notify guest if
  152. * needed.
  153. * Return the number of bytes popped off the receive ring buffer by the
  154. * preceding recv_peek/recv_pop calls on success, negative errno on failure.
  155. */
  156. ssize_t vmbus_channel_recv_done(VMBusChannel *chan);
  157. /*
  158. * Free the request allocated by vmbus_channel_recv_peek, together with its
  159. * fields.
  160. */
  161. void vmbus_free_req(void *req);
  162. /*
  163. * Find and reference a GPADL by @gpadl_id.
  164. * If not found return NULL.
  165. */
  166. VMBusGpadl *vmbus_get_gpadl(VMBusChannel *chan, uint32_t gpadl_id);
  167. /*
  168. * Unreference @gpadl. If the reference count drops to zero, free it.
  169. * @gpadl may be NULL, in which case nothing is done.
  170. */
  171. void vmbus_put_gpadl(VMBusGpadl *gpadl);
  172. /*
  173. * Calculate total length in bytes of @gpadl.
  174. * @gpadl must be valid.
  175. */
  176. uint32_t vmbus_gpadl_len(VMBusGpadl *gpadl);
  177. /*
  178. * Copy data from @iov to @gpadl at offset @off.
  179. * Return the number of bytes copied, or a negative status on failure.
  180. */
  181. ssize_t vmbus_iov_to_gpadl(VMBusChannel *chan, VMBusGpadl *gpadl, uint32_t off,
  182. const struct iovec *iov, size_t iov_cnt);
  183. /*
  184. * Map SGList contained in the request @req, at offset @off and no more than
  185. * @len bytes, for io in direction @dir, and populate @iov with the mapped
  186. * iovecs.
  187. * Return the number of iovecs mapped, or negative status on failure.
  188. */
  189. int vmbus_map_sgl(VMBusChanReq *req, DMADirection dir, struct iovec *iov,
  190. unsigned iov_cnt, size_t len, size_t off);
  191. /*
  192. * Unmap *iov mapped with vmbus_map_sgl, marking the number of bytes @accessed.
  193. */
  194. void vmbus_unmap_sgl(VMBusChanReq *req, DMADirection dir, struct iovec *iov,
  195. unsigned iov_cnt, size_t accessed);
  196. #endif