Commit | Line | Data |
---|---|---|
c88c4e4c HJ |
1 | /* |
2 | * Copyright (c) 2010, Microsoft Corporation. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
15 | * Place - Suite 330, Boston, MA 02111-1307 USA. | |
16 | * | |
17 | * Authors: | |
18 | * Haiyang Zhang <haiyangz@microsoft.com> | |
19 | * Hank Janssen <hjanssen@microsoft.com> | |
20 | */ | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/sysctl.h> | |
9e629075 | 26 | #include <linux/reboot.h> |
d750785f HZ |
27 | #include <linux/dmi.h> |
28 | #include <linux/pci.h> | |
c88c4e4c HJ |
29 | |
30 | #include "logging.h" | |
e3fe0bb6 | 31 | #include "hv_api.h" |
c88c4e4c | 32 | #include "vmbus.h" |
f8e5add2 | 33 | #include "vmbus_packet_format.h" |
0e0ab5f5 | 34 | #include "vmbus_channel_interface.h" |
2d82f6c7 | 35 | #include "version_info.h" |
4df90be5 | 36 | #include "channel.h" |
72daf320 | 37 | #include "vmbus_private.h" |
447fc67e | 38 | #include "vmbus_api.h" |
c88c4e4c | 39 | #include "utils.h" |
245ba56a | 40 | #include "hv_kvp.h" |
c88c4e4c | 41 | |
45241e50 HJ |
42 | static u8 *shut_txf_buf; |
43 | static u8 *time_txf_buf; | |
44 | static u8 *hbeat_txf_buf; | |
c88c4e4c | 45 | |
6610944a | 46 | static void shutdown_onchannelcallback(void *context) |
c88c4e4c HJ |
47 | { |
48 | struct vmbus_channel *channel = context; | |
45241e50 | 49 | u32 recvlen; |
c88c4e4c HJ |
50 | u64 requestid; |
51 | u8 execute_shutdown = false; | |
52 | ||
53 | struct shutdown_msg_data *shutdown_msg; | |
54 | ||
55 | struct icmsg_hdr *icmsghdrp; | |
56 | struct icmsg_negotiate *negop = NULL; | |
57 | ||
45241e50 HJ |
58 | vmbus_recvpacket(channel, shut_txf_buf, |
59 | PAGE_SIZE, &recvlen, &requestid); | |
c88c4e4c HJ |
60 | |
61 | if (recvlen > 0) { | |
45241e50 | 62 | icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[ |
c88c4e4c HJ |
63 | sizeof(struct vmbuspipe_hdr)]; |
64 | ||
65 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
45241e50 | 66 | prep_negotiate_resp(icmsghdrp, negop, shut_txf_buf); |
c88c4e4c | 67 | } else { |
45241e50 HJ |
68 | shutdown_msg = |
69 | (struct shutdown_msg_data *)&shut_txf_buf[ | |
70 | sizeof(struct vmbuspipe_hdr) + | |
71 | sizeof(struct icmsg_hdr)]; | |
c88c4e4c HJ |
72 | |
73 | switch (shutdown_msg->flags) { | |
74 | case 0: | |
75 | case 1: | |
76 | icmsghdrp->status = HV_S_OK; | |
77 | execute_shutdown = true; | |
78 | ||
79 | DPRINT_INFO(VMBUS, "Shutdown request received -" | |
80 | " gracefull shutdown initiated"); | |
81 | break; | |
82 | default: | |
83 | icmsghdrp->status = HV_E_FAIL; | |
84 | execute_shutdown = false; | |
85 | ||
86 | DPRINT_INFO(VMBUS, "Shutdown request received -" | |
87 | " Invalid request"); | |
88 | break; | |
89 | }; | |
90 | } | |
91 | ||
92 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
93 | | ICMSGHDRFLAG_RESPONSE; | |
94 | ||
45241e50 | 95 | vmbus_sendpacket(channel, shut_txf_buf, |
c88c4e4c | 96 | recvlen, requestid, |
415f2287 | 97 | VM_PKT_DATA_INBAND, 0); |
c88c4e4c HJ |
98 | } |
99 | ||
c88c4e4c | 100 | if (execute_shutdown == true) |
9e629075 | 101 | orderly_poweroff(false); |
c88c4e4c HJ |
102 | } |
103 | ||
39c4e9c3 | 104 | /* |
e9ec3603 | 105 | * Set guest time to host UTC time. |
39c4e9c3 | 106 | */ |
e9ec3603 | 107 | static inline void do_adj_guesttime(u64 hosttime) |
39c4e9c3 HZ |
108 | { |
109 | s64 host_tns; | |
110 | struct timespec host_ts; | |
39c4e9c3 HZ |
111 | |
112 | host_tns = (hosttime - WLTIMEDELTA) * 100; | |
113 | host_ts = ns_to_timespec(host_tns); | |
114 | ||
e9ec3603 HZ |
115 | do_settimeofday(&host_ts); |
116 | } | |
117 | ||
118 | /* | |
119 | * Synchronize time with host after reboot, restore, etc. | |
120 | * | |
121 | * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM. | |
122 | * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time | |
123 | * message after the timesync channel is opened. Since the hv_utils module is | |
124 | * loaded after hv_vmbus, the first message is usually missed. The other | |
125 | * thing is, systime is automatically set to emulated hardware clock which may | |
126 | * not be UTC time or in the same time zone. So, to override these effects, we | |
127 | * use the first 50 time samples for initial system time setting. | |
128 | */ | |
129 | static inline void adj_guesttime(u64 hosttime, u8 flags) | |
130 | { | |
131 | static s32 scnt = 50; | |
132 | ||
39c4e9c3 | 133 | if ((flags & ICTIMESYNCFLAG_SYNC) != 0) { |
e9ec3603 | 134 | do_adj_guesttime(hosttime); |
39c4e9c3 HZ |
135 | return; |
136 | } | |
137 | ||
e9ec3603 | 138 | if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) { |
39c4e9c3 | 139 | scnt--; |
e9ec3603 | 140 | do_adj_guesttime(hosttime); |
39c4e9c3 | 141 | } |
39c4e9c3 HZ |
142 | } |
143 | ||
144 | /* | |
145 | * Time Sync Channel message handler. | |
146 | */ | |
147 | static void timesync_onchannelcallback(void *context) | |
148 | { | |
149 | struct vmbus_channel *channel = context; | |
45241e50 | 150 | u32 recvlen; |
39c4e9c3 HZ |
151 | u64 requestid; |
152 | struct icmsg_hdr *icmsghdrp; | |
153 | struct ictimesync_data *timedatap; | |
154 | ||
45241e50 HJ |
155 | vmbus_recvpacket(channel, time_txf_buf, |
156 | PAGE_SIZE, &recvlen, &requestid); | |
39c4e9c3 HZ |
157 | |
158 | if (recvlen > 0) { | |
45241e50 | 159 | icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[ |
39c4e9c3 HZ |
160 | sizeof(struct vmbuspipe_hdr)]; |
161 | ||
162 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
45241e50 | 163 | prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf); |
39c4e9c3 | 164 | } else { |
45241e50 | 165 | timedatap = (struct ictimesync_data *)&time_txf_buf[ |
39c4e9c3 HZ |
166 | sizeof(struct vmbuspipe_hdr) + |
167 | sizeof(struct icmsg_hdr)]; | |
168 | adj_guesttime(timedatap->parenttime, timedatap->flags); | |
169 | } | |
170 | ||
171 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
172 | | ICMSGHDRFLAG_RESPONSE; | |
173 | ||
45241e50 | 174 | vmbus_sendpacket(channel, time_txf_buf, |
39c4e9c3 | 175 | recvlen, requestid, |
415f2287 | 176 | VM_PKT_DATA_INBAND, 0); |
39c4e9c3 | 177 | } |
39c4e9c3 HZ |
178 | } |
179 | ||
9153f7b9 HJ |
180 | /* |
181 | * Heartbeat functionality. | |
182 | * Every two seconds, Hyper-V send us a heartbeat request message. | |
183 | * we respond to this message, and Hyper-V knows we are alive. | |
184 | */ | |
185 | static void heartbeat_onchannelcallback(void *context) | |
186 | { | |
187 | struct vmbus_channel *channel = context; | |
45241e50 | 188 | u32 recvlen; |
9153f7b9 HJ |
189 | u64 requestid; |
190 | struct icmsg_hdr *icmsghdrp; | |
191 | struct heartbeat_msg_data *heartbeat_msg; | |
192 | ||
45241e50 HJ |
193 | vmbus_recvpacket(channel, hbeat_txf_buf, |
194 | PAGE_SIZE, &recvlen, &requestid); | |
9153f7b9 HJ |
195 | |
196 | if (recvlen > 0) { | |
45241e50 | 197 | icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[ |
9153f7b9 HJ |
198 | sizeof(struct vmbuspipe_hdr)]; |
199 | ||
200 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
45241e50 | 201 | prep_negotiate_resp(icmsghdrp, NULL, hbeat_txf_buf); |
9153f7b9 | 202 | } else { |
45241e50 HJ |
203 | heartbeat_msg = |
204 | (struct heartbeat_msg_data *)&hbeat_txf_buf[ | |
205 | sizeof(struct vmbuspipe_hdr) + | |
206 | sizeof(struct icmsg_hdr)]; | |
9153f7b9 | 207 | |
9153f7b9 HJ |
208 | heartbeat_msg->seq_num += 1; |
209 | } | |
210 | ||
211 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
212 | | ICMSGHDRFLAG_RESPONSE; | |
213 | ||
45241e50 | 214 | vmbus_sendpacket(channel, hbeat_txf_buf, |
9153f7b9 | 215 | recvlen, requestid, |
415f2287 | 216 | VM_PKT_DATA_INBAND, 0); |
9153f7b9 | 217 | } |
9153f7b9 | 218 | } |
39c4e9c3 | 219 | |
d750785f HZ |
220 | static const struct pci_device_id __initconst |
221 | hv_utils_pci_table[] __maybe_unused = { | |
222 | { PCI_DEVICE(0x1414, 0x5353) }, /* Hyper-V emulated VGA controller */ | |
223 | { 0 } | |
224 | }; | |
225 | MODULE_DEVICE_TABLE(pci, hv_utils_pci_table); | |
226 | ||
227 | ||
228 | static const struct dmi_system_id __initconst | |
229 | hv_utils_dmi_table[] __maybe_unused = { | |
230 | { | |
231 | .ident = "Hyper-V", | |
232 | .matches = { | |
233 | DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"), | |
234 | DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"), | |
235 | DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"), | |
236 | }, | |
237 | }, | |
238 | { }, | |
239 | }; | |
240 | MODULE_DEVICE_TABLE(dmi, hv_utils_dmi_table); | |
241 | ||
242 | ||
c88c4e4c HJ |
243 | static int __init init_hyperv_utils(void) |
244 | { | |
245 | printk(KERN_INFO "Registering HyperV Utility Driver\n"); | |
246 | ||
245ba56a KS |
247 | if (hv_kvp_init()) |
248 | return -ENODEV; | |
249 | ||
250 | ||
d750785f HZ |
251 | if (!dmi_check_system(hv_utils_dmi_table)) |
252 | return -ENODEV; | |
253 | ||
45241e50 HJ |
254 | shut_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
255 | time_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
256 | hbeat_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
257 | ||
258 | if (!shut_txf_buf || !time_txf_buf || !hbeat_txf_buf) { | |
259 | printk(KERN_INFO | |
260 | "Unable to allocate memory for receive buffer\n"); | |
261 | kfree(shut_txf_buf); | |
262 | kfree(time_txf_buf); | |
263 | kfree(hbeat_txf_buf); | |
264 | return -ENOMEM; | |
265 | } | |
266 | ||
c50f7fb2 | 267 | hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback = |
c88c4e4c HJ |
268 | &shutdown_onchannelcallback; |
269 | hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback; | |
270 | ||
c50f7fb2 | 271 | hv_cb_utils[HV_TIMESYNC_MSG].channel->onchannel_callback = |
39c4e9c3 HZ |
272 | ×ync_onchannelcallback; |
273 | hv_cb_utils[HV_TIMESYNC_MSG].callback = ×ync_onchannelcallback; | |
274 | ||
c50f7fb2 | 275 | hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback = |
9153f7b9 HJ |
276 | &heartbeat_onchannelcallback; |
277 | hv_cb_utils[HV_HEARTBEAT_MSG].callback = &heartbeat_onchannelcallback; | |
278 | ||
245ba56a KS |
279 | hv_cb_utils[HV_KVP_MSG].channel->onchannel_callback = |
280 | &hv_kvp_onchannelcallback; | |
281 | ||
282 | ||
283 | ||
c88c4e4c HJ |
284 | return 0; |
285 | } | |
286 | ||
287 | static void exit_hyperv_utils(void) | |
288 | { | |
289 | printk(KERN_INFO "De-Registered HyperV Utility Driver\n"); | |
290 | ||
c50f7fb2 | 291 | hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback = |
c88c4e4c HJ |
292 | &chn_cb_negotiate; |
293 | hv_cb_utils[HV_SHUTDOWN_MSG].callback = &chn_cb_negotiate; | |
39c4e9c3 | 294 | |
c50f7fb2 | 295 | hv_cb_utils[HV_TIMESYNC_MSG].channel->onchannel_callback = |
39c4e9c3 HZ |
296 | &chn_cb_negotiate; |
297 | hv_cb_utils[HV_TIMESYNC_MSG].callback = &chn_cb_negotiate; | |
9153f7b9 | 298 | |
c50f7fb2 | 299 | hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback = |
9153f7b9 HJ |
300 | &chn_cb_negotiate; |
301 | hv_cb_utils[HV_HEARTBEAT_MSG].callback = &chn_cb_negotiate; | |
45241e50 | 302 | |
245ba56a KS |
303 | hv_cb_utils[HV_KVP_MSG].channel->onchannel_callback = |
304 | &chn_cb_negotiate; | |
305 | hv_kvp_deinit(); | |
306 | ||
45241e50 HJ |
307 | kfree(shut_txf_buf); |
308 | kfree(time_txf_buf); | |
309 | kfree(hbeat_txf_buf); | |
c88c4e4c HJ |
310 | } |
311 | ||
312 | module_init(init_hyperv_utils); | |
313 | module_exit(exit_hyperv_utils); | |
314 | ||
315 | MODULE_DESCRIPTION("Hyper-V Utilities"); | |
316 | MODULE_VERSION(HV_DRV_VERSION); | |
317 | MODULE_LICENSE("GPL"); |