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) { | |
62 | DPRINT_DBG(VMBUS, "shutdown packet: len=%d, requestid=%lld", | |
63 | recvlen, requestid); | |
64 | ||
45241e50 | 65 | icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[ |
c88c4e4c HJ |
66 | sizeof(struct vmbuspipe_hdr)]; |
67 | ||
68 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
45241e50 | 69 | prep_negotiate_resp(icmsghdrp, negop, shut_txf_buf); |
c88c4e4c | 70 | } else { |
45241e50 HJ |
71 | shutdown_msg = |
72 | (struct shutdown_msg_data *)&shut_txf_buf[ | |
73 | sizeof(struct vmbuspipe_hdr) + | |
74 | sizeof(struct icmsg_hdr)]; | |
c88c4e4c HJ |
75 | |
76 | switch (shutdown_msg->flags) { | |
77 | case 0: | |
78 | case 1: | |
79 | icmsghdrp->status = HV_S_OK; | |
80 | execute_shutdown = true; | |
81 | ||
82 | DPRINT_INFO(VMBUS, "Shutdown request received -" | |
25985edc | 83 | " graceful shutdown initiated"); |
c88c4e4c HJ |
84 | break; |
85 | default: | |
86 | icmsghdrp->status = HV_E_FAIL; | |
87 | execute_shutdown = false; | |
88 | ||
89 | DPRINT_INFO(VMBUS, "Shutdown request received -" | |
90 | " Invalid request"); | |
91 | break; | |
92 | }; | |
93 | } | |
94 | ||
95 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
96 | | ICMSGHDRFLAG_RESPONSE; | |
97 | ||
45241e50 | 98 | vmbus_sendpacket(channel, shut_txf_buf, |
c88c4e4c | 99 | recvlen, requestid, |
415f2287 | 100 | VM_PKT_DATA_INBAND, 0); |
c88c4e4c HJ |
101 | } |
102 | ||
c88c4e4c | 103 | if (execute_shutdown == true) |
9e629075 | 104 | orderly_poweroff(false); |
c88c4e4c HJ |
105 | } |
106 | ||
39c4e9c3 | 107 | /* |
e9ec3603 | 108 | * Set guest time to host UTC time. |
39c4e9c3 | 109 | */ |
e9ec3603 | 110 | static inline void do_adj_guesttime(u64 hosttime) |
39c4e9c3 HZ |
111 | { |
112 | s64 host_tns; | |
113 | struct timespec host_ts; | |
39c4e9c3 HZ |
114 | |
115 | host_tns = (hosttime - WLTIMEDELTA) * 100; | |
116 | host_ts = ns_to_timespec(host_tns); | |
117 | ||
e9ec3603 HZ |
118 | do_settimeofday(&host_ts); |
119 | } | |
120 | ||
121 | /* | |
122 | * Synchronize time with host after reboot, restore, etc. | |
123 | * | |
124 | * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM. | |
125 | * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time | |
126 | * message after the timesync channel is opened. Since the hv_utils module is | |
127 | * loaded after hv_vmbus, the first message is usually missed. The other | |
128 | * thing is, systime is automatically set to emulated hardware clock which may | |
129 | * not be UTC time or in the same time zone. So, to override these effects, we | |
130 | * use the first 50 time samples for initial system time setting. | |
131 | */ | |
132 | static inline void adj_guesttime(u64 hosttime, u8 flags) | |
133 | { | |
134 | static s32 scnt = 50; | |
135 | ||
39c4e9c3 | 136 | if ((flags & ICTIMESYNCFLAG_SYNC) != 0) { |
e9ec3603 | 137 | do_adj_guesttime(hosttime); |
39c4e9c3 HZ |
138 | return; |
139 | } | |
140 | ||
e9ec3603 | 141 | if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) { |
39c4e9c3 | 142 | scnt--; |
e9ec3603 | 143 | do_adj_guesttime(hosttime); |
39c4e9c3 | 144 | } |
39c4e9c3 HZ |
145 | } |
146 | ||
147 | /* | |
148 | * Time Sync Channel message handler. | |
149 | */ | |
150 | static void timesync_onchannelcallback(void *context) | |
151 | { | |
152 | struct vmbus_channel *channel = context; | |
45241e50 | 153 | u32 recvlen; |
39c4e9c3 HZ |
154 | u64 requestid; |
155 | struct icmsg_hdr *icmsghdrp; | |
156 | struct ictimesync_data *timedatap; | |
157 | ||
45241e50 HJ |
158 | vmbus_recvpacket(channel, time_txf_buf, |
159 | PAGE_SIZE, &recvlen, &requestid); | |
39c4e9c3 HZ |
160 | |
161 | if (recvlen > 0) { | |
162 | DPRINT_DBG(VMBUS, "timesync packet: recvlen=%d, requestid=%lld", | |
163 | recvlen, requestid); | |
164 | ||
45241e50 | 165 | icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[ |
39c4e9c3 HZ |
166 | sizeof(struct vmbuspipe_hdr)]; |
167 | ||
168 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
45241e50 | 169 | prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf); |
39c4e9c3 | 170 | } else { |
45241e50 | 171 | timedatap = (struct ictimesync_data *)&time_txf_buf[ |
39c4e9c3 HZ |
172 | sizeof(struct vmbuspipe_hdr) + |
173 | sizeof(struct icmsg_hdr)]; | |
174 | adj_guesttime(timedatap->parenttime, timedatap->flags); | |
175 | } | |
176 | ||
177 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
178 | | ICMSGHDRFLAG_RESPONSE; | |
179 | ||
45241e50 | 180 | vmbus_sendpacket(channel, time_txf_buf, |
39c4e9c3 | 181 | recvlen, requestid, |
415f2287 | 182 | VM_PKT_DATA_INBAND, 0); |
39c4e9c3 | 183 | } |
39c4e9c3 HZ |
184 | } |
185 | ||
9153f7b9 HJ |
186 | /* |
187 | * Heartbeat functionality. | |
188 | * Every two seconds, Hyper-V send us a heartbeat request message. | |
189 | * we respond to this message, and Hyper-V knows we are alive. | |
190 | */ | |
191 | static void heartbeat_onchannelcallback(void *context) | |
192 | { | |
193 | struct vmbus_channel *channel = context; | |
45241e50 | 194 | u32 recvlen; |
9153f7b9 HJ |
195 | u64 requestid; |
196 | struct icmsg_hdr *icmsghdrp; | |
197 | struct heartbeat_msg_data *heartbeat_msg; | |
198 | ||
45241e50 HJ |
199 | vmbus_recvpacket(channel, hbeat_txf_buf, |
200 | PAGE_SIZE, &recvlen, &requestid); | |
9153f7b9 HJ |
201 | |
202 | if (recvlen > 0) { | |
203 | DPRINT_DBG(VMBUS, "heartbeat packet: len=%d, requestid=%lld", | |
204 | recvlen, requestid); | |
205 | ||
45241e50 | 206 | icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[ |
9153f7b9 HJ |
207 | sizeof(struct vmbuspipe_hdr)]; |
208 | ||
209 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
45241e50 | 210 | prep_negotiate_resp(icmsghdrp, NULL, hbeat_txf_buf); |
9153f7b9 | 211 | } else { |
45241e50 HJ |
212 | heartbeat_msg = |
213 | (struct heartbeat_msg_data *)&hbeat_txf_buf[ | |
214 | sizeof(struct vmbuspipe_hdr) + | |
215 | sizeof(struct icmsg_hdr)]; | |
9153f7b9 HJ |
216 | |
217 | DPRINT_DBG(VMBUS, "heartbeat seq = %lld", | |
218 | heartbeat_msg->seq_num); | |
219 | ||
220 | heartbeat_msg->seq_num += 1; | |
221 | } | |
222 | ||
223 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
224 | | ICMSGHDRFLAG_RESPONSE; | |
225 | ||
45241e50 | 226 | vmbus_sendpacket(channel, hbeat_txf_buf, |
9153f7b9 | 227 | recvlen, requestid, |
415f2287 | 228 | VM_PKT_DATA_INBAND, 0); |
9153f7b9 | 229 | } |
9153f7b9 | 230 | } |
39c4e9c3 | 231 | |
d750785f HZ |
232 | static const struct pci_device_id __initconst |
233 | hv_utils_pci_table[] __maybe_unused = { | |
234 | { PCI_DEVICE(0x1414, 0x5353) }, /* Hyper-V emulated VGA controller */ | |
235 | { 0 } | |
236 | }; | |
237 | MODULE_DEVICE_TABLE(pci, hv_utils_pci_table); | |
238 | ||
239 | ||
240 | static const struct dmi_system_id __initconst | |
241 | hv_utils_dmi_table[] __maybe_unused = { | |
242 | { | |
243 | .ident = "Hyper-V", | |
244 | .matches = { | |
245 | DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"), | |
246 | DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"), | |
247 | DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"), | |
248 | }, | |
249 | }, | |
250 | { }, | |
251 | }; | |
252 | MODULE_DEVICE_TABLE(dmi, hv_utils_dmi_table); | |
253 | ||
254 | ||
c88c4e4c HJ |
255 | static int __init init_hyperv_utils(void) |
256 | { | |
257 | printk(KERN_INFO "Registering HyperV Utility Driver\n"); | |
258 | ||
245ba56a KS |
259 | if (hv_kvp_init()) |
260 | return -ENODEV; | |
261 | ||
262 | ||
d750785f HZ |
263 | if (!dmi_check_system(hv_utils_dmi_table)) |
264 | return -ENODEV; | |
265 | ||
45241e50 HJ |
266 | shut_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
267 | time_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
268 | hbeat_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
269 | ||
270 | if (!shut_txf_buf || !time_txf_buf || !hbeat_txf_buf) { | |
271 | printk(KERN_INFO | |
272 | "Unable to allocate memory for receive buffer\n"); | |
273 | kfree(shut_txf_buf); | |
274 | kfree(time_txf_buf); | |
275 | kfree(hbeat_txf_buf); | |
276 | return -ENOMEM; | |
277 | } | |
278 | ||
c50f7fb2 | 279 | hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback = |
c88c4e4c HJ |
280 | &shutdown_onchannelcallback; |
281 | hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback; | |
282 | ||
c50f7fb2 | 283 | hv_cb_utils[HV_TIMESYNC_MSG].channel->onchannel_callback = |
39c4e9c3 HZ |
284 | ×ync_onchannelcallback; |
285 | hv_cb_utils[HV_TIMESYNC_MSG].callback = ×ync_onchannelcallback; | |
286 | ||
c50f7fb2 | 287 | hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback = |
9153f7b9 HJ |
288 | &heartbeat_onchannelcallback; |
289 | hv_cb_utils[HV_HEARTBEAT_MSG].callback = &heartbeat_onchannelcallback; | |
290 | ||
245ba56a KS |
291 | hv_cb_utils[HV_KVP_MSG].channel->onchannel_callback = |
292 | &hv_kvp_onchannelcallback; | |
293 | ||
294 | ||
295 | ||
c88c4e4c HJ |
296 | return 0; |
297 | } | |
298 | ||
299 | static void exit_hyperv_utils(void) | |
300 | { | |
301 | printk(KERN_INFO "De-Registered HyperV Utility Driver\n"); | |
302 | ||
c50f7fb2 | 303 | hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback = |
c88c4e4c HJ |
304 | &chn_cb_negotiate; |
305 | hv_cb_utils[HV_SHUTDOWN_MSG].callback = &chn_cb_negotiate; | |
39c4e9c3 | 306 | |
c50f7fb2 | 307 | hv_cb_utils[HV_TIMESYNC_MSG].channel->onchannel_callback = |
39c4e9c3 HZ |
308 | &chn_cb_negotiate; |
309 | hv_cb_utils[HV_TIMESYNC_MSG].callback = &chn_cb_negotiate; | |
9153f7b9 | 310 | |
c50f7fb2 | 311 | hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback = |
9153f7b9 HJ |
312 | &chn_cb_negotiate; |
313 | hv_cb_utils[HV_HEARTBEAT_MSG].callback = &chn_cb_negotiate; | |
45241e50 | 314 | |
245ba56a KS |
315 | hv_cb_utils[HV_KVP_MSG].channel->onchannel_callback = |
316 | &chn_cb_negotiate; | |
317 | hv_kvp_deinit(); | |
318 | ||
45241e50 HJ |
319 | kfree(shut_txf_buf); |
320 | kfree(time_txf_buf); | |
321 | kfree(hbeat_txf_buf); | |
c88c4e4c HJ |
322 | } |
323 | ||
324 | module_init(init_hyperv_utils); | |
325 | module_exit(exit_hyperv_utils); | |
326 | ||
327 | MODULE_DESCRIPTION("Hyper-V Utilities"); | |
328 | MODULE_VERSION(HV_DRV_VERSION); | |
329 | MODULE_LICENSE("GPL"); |