1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VMware VMCI Driver
4  *
5  * Copyright (C) 2012 VMware, Inc. All rights reserved.
6  */
7 
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>
14 
15 #include "vmci_driver.h"
16 #include "vmci_event.h"
17 
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)");
22 
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)");
27 
28 static bool vmci_guest_personality_initialized;
29 static bool vmci_host_personality_initialized;
30 
31 /*
32  * vmci_get_context_id() - Gets the current context ID.
33  *
34  * Returns the current context ID.  Note that since this is accessed only
35  * from code running in the host, this always returns the host context ID.
36  */
37 u32 vmci_get_context_id(void)
38 {
39 	if (vmci_guest_code_active())
40 		return vmci_get_vm_context_id();
41 	else if (vmci_host_code_active())
42 		return VMCI_HOST_CONTEXT_ID;
43 
44 	return VMCI_INVALID_ID;
45 }
46 EXPORT_SYMBOL_GPL(vmci_get_context_id);
47 
48 static int __init vmci_drv_init(void)
49 {
50 	int vmci_err;
51 	int error;
52 
53 	vmci_err = vmci_event_init();
54 	if (vmci_err < VMCI_SUCCESS) {
55 		pr_err("Failed to initialize VMCIEvent (result=%d)\n",
56 		       vmci_err);
57 		return -EINVAL;
58 	}
59 
60 	if (!vmci_disable_guest) {
61 		error = vmci_guest_init();
62 		if (error) {
63 			pr_warn("Failed to initialize guest personality (err=%d)\n",
64 				error);
65 		} else {
66 			vmci_guest_personality_initialized = true;
67 			pr_info("Guest personality initialized and is %s\n",
68 				vmci_guest_code_active() ?
69 				"active" : "inactive");
70 		}
71 	}
72 
73 	if (!vmci_disable_host) {
74 		error = vmci_host_init();
75 		if (error) {
76 			pr_warn("Unable to initialize host personality (err=%d)\n",
77 				error);
78 		} else {
79 			vmci_host_personality_initialized = true;
80 			pr_info("Initialized host personality\n");
81 		}
82 	}
83 
84 	if (!vmci_guest_personality_initialized &&
85 	    !vmci_host_personality_initialized) {
86 		vmci_event_exit();
87 		return -ENODEV;
88 	}
89 
90 	return 0;
91 }
92 module_init(vmci_drv_init);
93 
94 static void __exit vmci_drv_exit(void)
95 {
96 	if (vmci_guest_personality_initialized)
97 		vmci_guest_exit();
98 
99 	if (vmci_host_personality_initialized)
100 		vmci_host_exit();
101 
102 	vmci_event_exit();
103 }
104 module_exit(vmci_drv_exit);
105 
106 MODULE_AUTHOR("VMware, Inc.");
107 MODULE_DESCRIPTION("VMware Virtual Machine Communication Interface.");
108 MODULE_VERSION("1.1.6.0-k");
109 MODULE_LICENSE("GPL v2");
110