1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2012 VMware, Inc. All rights reserved.
8 #include <linux/vmw_vmci_defs.h>
9 #include <linux/vmw_vmci_api.h>
10 #include <linux/atomic.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include "vmci_driver.h"
16 #include "vmci_event.h"
18 static bool vmci_disable_host
;
19 module_param_named(disable_host
, vmci_disable_host
, bool, 0);
20 MODULE_PARM_DESC(disable_host
,
21 "Disable driver host personality (default=enabled)");
23 static bool vmci_disable_guest
;
24 module_param_named(disable_guest
, vmci_disable_guest
, bool, 0);
25 MODULE_PARM_DESC(disable_guest
,
26 "Disable driver guest personality (default=enabled)");
28 static bool vmci_guest_personality_initialized
;
29 static bool vmci_host_personality_initialized
;
31 static DEFINE_MUTEX(vmci_vsock_mutex
); /* protects vmci_vsock_transport_cb */
32 static vmci_vsock_cb vmci_vsock_transport_cb
;
33 static bool vmci_vsock_cb_host_called
;
36 * vmci_get_context_id() - Gets the current context ID.
38 * Returns the current context ID. Note that since this is accessed only
39 * from code running in the host, this always returns the host context ID.
41 u32
vmci_get_context_id(void)
43 if (vmci_guest_code_active())
44 return vmci_get_vm_context_id();
45 else if (vmci_host_code_active())
46 return VMCI_HOST_CONTEXT_ID
;
48 return VMCI_INVALID_ID
;
50 EXPORT_SYMBOL_GPL(vmci_get_context_id
);
53 * vmci_register_vsock_callback() - Register the VSOCK vmci_transport callback.
55 * The callback will be called when the first host or guest becomes active,
56 * or if they are already active when this function is called.
57 * To unregister the callback, call this function with NULL parameter.
59 * Returns 0 on success. -EBUSY if a callback is already registered.
61 int vmci_register_vsock_callback(vmci_vsock_cb callback
)
65 mutex_lock(&vmci_vsock_mutex
);
67 if (vmci_vsock_transport_cb
&& callback
) {
72 vmci_vsock_transport_cb
= callback
;
74 if (!vmci_vsock_transport_cb
) {
75 vmci_vsock_cb_host_called
= false;
79 if (vmci_guest_code_active())
80 vmci_vsock_transport_cb(false);
82 if (vmci_host_users() > 0) {
83 vmci_vsock_cb_host_called
= true;
84 vmci_vsock_transport_cb(true);
88 mutex_unlock(&vmci_vsock_mutex
);
91 EXPORT_SYMBOL_GPL(vmci_register_vsock_callback
);
93 void vmci_call_vsock_callback(bool is_host
)
95 mutex_lock(&vmci_vsock_mutex
);
97 if (!vmci_vsock_transport_cb
)
100 /* In the host, this function could be called multiple times,
101 * but we want to register it only once.
104 if (vmci_vsock_cb_host_called
)
107 vmci_vsock_cb_host_called
= true;
110 vmci_vsock_transport_cb(is_host
);
112 mutex_unlock(&vmci_vsock_mutex
);
115 static int __init
vmci_drv_init(void)
120 vmci_err
= vmci_event_init();
121 if (vmci_err
< VMCI_SUCCESS
) {
122 pr_err("Failed to initialize VMCIEvent (result=%d)\n",
127 if (!vmci_disable_guest
) {
128 error
= vmci_guest_init();
130 pr_warn("Failed to initialize guest personality (err=%d)\n",
133 vmci_guest_personality_initialized
= true;
134 pr_info("Guest personality initialized and is %s\n",
135 vmci_guest_code_active() ?
136 "active" : "inactive");
140 if (!vmci_disable_host
) {
141 error
= vmci_host_init();
143 pr_warn("Unable to initialize host personality (err=%d)\n",
146 vmci_host_personality_initialized
= true;
147 pr_info("Initialized host personality\n");
151 if (!vmci_guest_personality_initialized
&&
152 !vmci_host_personality_initialized
) {
159 module_init(vmci_drv_init
);
161 static void __exit
vmci_drv_exit(void)
163 if (vmci_guest_personality_initialized
)
166 if (vmci_host_personality_initialized
)
171 module_exit(vmci_drv_exit
);
173 MODULE_AUTHOR("VMware, Inc.");
174 MODULE_DESCRIPTION("VMware Virtual Machine Communication Interface.");
175 MODULE_VERSION("1.1.6.0-k");
176 MODULE_LICENSE("GPL v2");