vmci_context.h 5.4 KB

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