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