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