vmci_context.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * VMware VMCI driver (vmciContext.h)
  4. *
  5. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  6. */
  7. #ifndef _VMCI_CONTEXT_H_
  8. #define _VMCI_CONTEXT_H_
  9. #include <linux/vmw_vmci_defs.h>
  10. #include <linux/atomic.h>
  11. #include <linux/kref.h>
  12. #include <linux/types.h>
  13. #include <linux/wait.h>
  14. #include "vmci_handle_array.h"
  15. #include "vmci_datagram.h"
  16. /* Used to determine what checkpoint state to get and set. */
  17. enum {
  18. VMCI_NOTIFICATION_CPT_STATE = 1,
  19. VMCI_WELLKNOWN_CPT_STATE = 2,
  20. VMCI_DG_OUT_STATE = 3,
  21. VMCI_DG_IN_STATE = 4,
  22. VMCI_DG_IN_SIZE_STATE = 5,
  23. VMCI_DOORBELL_CPT_STATE = 6,
  24. };
  25. /* Host specific struct used for signalling */
  26. struct vmci_host {
  27. wait_queue_head_t wait_queue;
  28. };
  29. struct vmci_handle_list {
  30. struct list_head node;
  31. struct vmci_handle handle;
  32. };
  33. struct vmci_ctx {
  34. struct list_head list_item; /* For global VMCI list. */
  35. u32 cid;
  36. struct kref kref;
  37. struct list_head datagram_queue; /* Head of per VM queue. */
  38. u32 pending_datagrams;
  39. size_t datagram_queue_size; /* Size of datagram queue in bytes. */
  40. /*
  41. * Version of the code that created
  42. * this context; e.g., VMX.
  43. */
  44. int user_version;
  45. spinlock_t lock; /* Locks callQueue and handle_arrays. */
  46. /*
  47. * queue_pairs attached to. The array of
  48. * handles for queue pairs is accessed
  49. * from the code for QP API, and there
  50. * it is protected by the QP lock. It
  51. * is also accessed from the context
  52. * clean up path, which does not
  53. * require a lock. VMCILock is not
  54. * used to protect the QP array field.
  55. */
  56. struct vmci_handle_arr *queue_pair_array;
  57. /* Doorbells created by context. */
  58. struct vmci_handle_arr *doorbell_array;
  59. /* Doorbells pending for context. */
  60. struct vmci_handle_arr *pending_doorbell_array;
  61. /* Contexts current context is subscribing to. */
  62. struct list_head notifier_list;
  63. unsigned int n_notifiers;
  64. struct vmci_host host_context;
  65. u32 priv_flags;
  66. const struct cred *cred;
  67. bool *notify; /* Notify flag pointer - hosted only. */
  68. struct page *notify_page; /* Page backing the notify UVA. */
  69. };
  70. /* VMCINotifyAddRemoveInfo: Used to add/remove remote context notifications. */
  71. struct vmci_ctx_info {
  72. u32 remote_cid;
  73. int result;
  74. };
  75. /* VMCICptBufInfo: Used to set/get current context's checkpoint state. */
  76. struct vmci_ctx_chkpt_buf_info {
  77. u64 cpt_buf;
  78. u32 cpt_type;
  79. u32 buf_size;
  80. s32 result;
  81. u32 _pad;
  82. };
  83. /*
  84. * VMCINotificationReceiveInfo: Used to recieve pending notifications
  85. * for doorbells and queue pairs.
  86. */
  87. struct vmci_ctx_notify_recv_info {
  88. u64 db_handle_buf_uva;
  89. u64 db_handle_buf_size;
  90. u64 qp_handle_buf_uva;
  91. u64 qp_handle_buf_size;
  92. s32 result;
  93. u32 _pad;
  94. };
  95. /*
  96. * Utilility function that checks whether two entities are allowed
  97. * to interact. If one of them is restricted, the other one must
  98. * be trusted.
  99. */
  100. static inline bool vmci_deny_interaction(u32 part_one, u32 part_two)
  101. {
  102. return ((part_one & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
  103. !(part_two & VMCI_PRIVILEGE_FLAG_TRUSTED)) ||
  104. ((part_two & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
  105. !(part_one & VMCI_PRIVILEGE_FLAG_TRUSTED));
  106. }
  107. struct vmci_ctx *vmci_ctx_create(u32 cid, u32 flags,
  108. uintptr_t event_hnd, int version,
  109. const struct cred *cred);
  110. void vmci_ctx_destroy(struct vmci_ctx *context);
  111. bool vmci_ctx_supports_host_qp(struct vmci_ctx *context);
  112. int vmci_ctx_enqueue_datagram(u32 cid, struct vmci_datagram *dg);
  113. int vmci_ctx_dequeue_datagram(struct vmci_ctx *context,
  114. size_t *max_size, struct vmci_datagram **dg);
  115. int vmci_ctx_pending_datagrams(u32 cid, u32 *pending);
  116. struct vmci_ctx *vmci_ctx_get(u32 cid);
  117. void vmci_ctx_put(struct vmci_ctx *context);
  118. bool vmci_ctx_exists(u32 cid);
  119. int vmci_ctx_add_notification(u32 context_id, u32 remote_cid);
  120. int vmci_ctx_remove_notification(u32 context_id, u32 remote_cid);
  121. int vmci_ctx_get_chkpt_state(u32 context_id, u32 cpt_type,
  122. u32 *num_cids, void **cpt_buf_ptr);
  123. int vmci_ctx_set_chkpt_state(u32 context_id, u32 cpt_type,
  124. u32 num_cids, void *cpt_buf);
  125. int vmci_ctx_qp_create(struct vmci_ctx *context, struct vmci_handle handle);
  126. int vmci_ctx_qp_destroy(struct vmci_ctx *context, struct vmci_handle handle);
  127. bool vmci_ctx_qp_exists(struct vmci_ctx *context, struct vmci_handle handle);
  128. void vmci_ctx_check_signal_notify(struct vmci_ctx *context);
  129. void vmci_ctx_unset_notify(struct vmci_ctx *context);
  130. int vmci_ctx_dbell_create(u32 context_id, struct vmci_handle handle);
  131. int vmci_ctx_dbell_destroy(u32 context_id, struct vmci_handle handle);
  132. int vmci_ctx_dbell_destroy_all(u32 context_id);
  133. int vmci_ctx_notify_dbell(u32 cid, struct vmci_handle handle,
  134. u32 src_priv_flags);
  135. int vmci_ctx_rcv_notifications_get(u32 context_id, struct vmci_handle_arr
  136. **db_handle_array, struct vmci_handle_arr
  137. **qp_handle_array);
  138. void vmci_ctx_rcv_notifications_release(u32 context_id, struct vmci_handle_arr
  139. *db_handle_array, struct vmci_handle_arr
  140. *qp_handle_array, bool success);
  141. static inline u32 vmci_ctx_get_id(struct vmci_ctx *context)
  142. {
  143. if (!context)
  144. return VMCI_INVALID_ID;
  145. return context->cid;
  146. }
  147. #endif /* _VMCI_CONTEXT_H_ */