2 * Copyright (c) 2010, Microsoft Corporation.
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.
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
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.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/sysctl.h>
28 #include <linux/reboot.h>
29 #include <linux/dmi.h>
30 #include <linux/pci.h>
35 static u8
*shut_txf_buf
;
36 static u8
*time_txf_buf
;
37 static u8
*hbeat_txf_buf
;
39 static void shutdown_onchannelcallback(void *context
)
41 struct vmbus_channel
*channel
= context
;
44 u8 execute_shutdown
= false;
46 struct shutdown_msg_data
*shutdown_msg
;
48 struct icmsg_hdr
*icmsghdrp
;
49 struct icmsg_negotiate
*negop
= NULL
;
51 vmbus_recvpacket(channel
, shut_txf_buf
,
52 PAGE_SIZE
, &recvlen
, &requestid
);
55 icmsghdrp
= (struct icmsg_hdr
*)&shut_txf_buf
[
56 sizeof(struct vmbuspipe_hdr
)];
58 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
59 prep_negotiate_resp(icmsghdrp
, negop
, shut_txf_buf
);
62 (struct shutdown_msg_data
*)&shut_txf_buf
[
63 sizeof(struct vmbuspipe_hdr
) +
64 sizeof(struct icmsg_hdr
)];
66 switch (shutdown_msg
->flags
) {
69 icmsghdrp
->status
= HV_S_OK
;
70 execute_shutdown
= true;
72 pr_info("Shutdown request received -"
73 " graceful shutdown initiated\n");
76 icmsghdrp
->status
= HV_E_FAIL
;
77 execute_shutdown
= false;
79 pr_info("Shutdown request received -"
80 " Invalid request\n");
85 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
86 | ICMSGHDRFLAG_RESPONSE
;
88 vmbus_sendpacket(channel
, shut_txf_buf
,
90 VM_PKT_DATA_INBAND
, 0);
93 if (execute_shutdown
== true)
94 orderly_poweroff(false);
98 * Set guest time to host UTC time.
100 static inline void do_adj_guesttime(u64 hosttime
)
103 struct timespec host_ts
;
105 host_tns
= (hosttime
- WLTIMEDELTA
) * 100;
106 host_ts
= ns_to_timespec(host_tns
);
108 do_settimeofday(&host_ts
);
112 * Synchronize time with host after reboot, restore, etc.
114 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
115 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
116 * message after the timesync channel is opened. Since the hv_utils module is
117 * loaded after hv_vmbus, the first message is usually missed. The other
118 * thing is, systime is automatically set to emulated hardware clock which may
119 * not be UTC time or in the same time zone. So, to override these effects, we
120 * use the first 50 time samples for initial system time setting.
122 static inline void adj_guesttime(u64 hosttime
, u8 flags
)
124 static s32 scnt
= 50;
126 if ((flags
& ICTIMESYNCFLAG_SYNC
) != 0) {
127 do_adj_guesttime(hosttime
);
131 if ((flags
& ICTIMESYNCFLAG_SAMPLE
) != 0 && scnt
> 0) {
133 do_adj_guesttime(hosttime
);
138 * Time Sync Channel message handler.
140 static void timesync_onchannelcallback(void *context
)
142 struct vmbus_channel
*channel
= context
;
145 struct icmsg_hdr
*icmsghdrp
;
146 struct ictimesync_data
*timedatap
;
148 vmbus_recvpacket(channel
, time_txf_buf
,
149 PAGE_SIZE
, &recvlen
, &requestid
);
152 icmsghdrp
= (struct icmsg_hdr
*)&time_txf_buf
[
153 sizeof(struct vmbuspipe_hdr
)];
155 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
156 prep_negotiate_resp(icmsghdrp
, NULL
, time_txf_buf
);
158 timedatap
= (struct ictimesync_data
*)&time_txf_buf
[
159 sizeof(struct vmbuspipe_hdr
) +
160 sizeof(struct icmsg_hdr
)];
161 adj_guesttime(timedatap
->parenttime
, timedatap
->flags
);
164 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
165 | ICMSGHDRFLAG_RESPONSE
;
167 vmbus_sendpacket(channel
, time_txf_buf
,
169 VM_PKT_DATA_INBAND
, 0);
174 * Heartbeat functionality.
175 * Every two seconds, Hyper-V send us a heartbeat request message.
176 * we respond to this message, and Hyper-V knows we are alive.
178 static void heartbeat_onchannelcallback(void *context
)
180 struct vmbus_channel
*channel
= context
;
183 struct icmsg_hdr
*icmsghdrp
;
184 struct heartbeat_msg_data
*heartbeat_msg
;
186 vmbus_recvpacket(channel
, hbeat_txf_buf
,
187 PAGE_SIZE
, &recvlen
, &requestid
);
190 icmsghdrp
= (struct icmsg_hdr
*)&hbeat_txf_buf
[
191 sizeof(struct vmbuspipe_hdr
)];
193 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
194 prep_negotiate_resp(icmsghdrp
, NULL
, hbeat_txf_buf
);
197 (struct heartbeat_msg_data
*)&hbeat_txf_buf
[
198 sizeof(struct vmbuspipe_hdr
) +
199 sizeof(struct icmsg_hdr
)];
201 heartbeat_msg
->seq_num
+= 1;
204 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
205 | ICMSGHDRFLAG_RESPONSE
;
207 vmbus_sendpacket(channel
, hbeat_txf_buf
,
209 VM_PKT_DATA_INBAND
, 0);
213 static const struct pci_device_id __initconst
214 hv_utils_pci_table
[] __maybe_unused
= {
215 { PCI_DEVICE(0x1414, 0x5353) }, /* Hyper-V emulated VGA controller */
218 MODULE_DEVICE_TABLE(pci
, hv_utils_pci_table
);
221 static const struct dmi_system_id __initconst
222 hv_utils_dmi_table
[] __maybe_unused
= {
226 DMI_MATCH(DMI_SYS_VENDOR
, "Microsoft Corporation"),
227 DMI_MATCH(DMI_PRODUCT_NAME
, "Virtual Machine"),
228 DMI_MATCH(DMI_BOARD_NAME
, "Virtual Machine"),
233 MODULE_DEVICE_TABLE(dmi
, hv_utils_dmi_table
);
236 static int __init
init_hyperv_utils(void)
238 pr_info("Registering HyperV Utility Driver\n");
244 if (!dmi_check_system(hv_utils_dmi_table
))
247 shut_txf_buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
248 time_txf_buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
249 hbeat_txf_buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
251 if (!shut_txf_buf
|| !time_txf_buf
|| !hbeat_txf_buf
) {
252 pr_info("Unable to allocate memory for receive buffer\n");
255 kfree(hbeat_txf_buf
);
259 hv_cb_utils
[HV_SHUTDOWN_MSG
].callback
= &shutdown_onchannelcallback
;
261 hv_cb_utils
[HV_TIMESYNC_MSG
].callback
= ×ync_onchannelcallback
;
263 hv_cb_utils
[HV_HEARTBEAT_MSG
].callback
= &heartbeat_onchannelcallback
;
265 hv_cb_utils
[HV_KVP_MSG
].callback
= &hv_kvp_onchannelcallback
;
270 static void exit_hyperv_utils(void)
272 pr_info("De-Registered HyperV Utility Driver\n");
274 if (hv_cb_utils
[HV_SHUTDOWN_MSG
].channel
!= NULL
)
275 hv_cb_utils
[HV_SHUTDOWN_MSG
].channel
->onchannel_callback
=
277 hv_cb_utils
[HV_SHUTDOWN_MSG
].callback
= NULL
;
279 if (hv_cb_utils
[HV_TIMESYNC_MSG
].channel
!= NULL
)
280 hv_cb_utils
[HV_TIMESYNC_MSG
].channel
->onchannel_callback
=
282 hv_cb_utils
[HV_TIMESYNC_MSG
].callback
= NULL
;
284 if (hv_cb_utils
[HV_HEARTBEAT_MSG
].channel
!= NULL
)
285 hv_cb_utils
[HV_HEARTBEAT_MSG
].channel
->onchannel_callback
=
287 hv_cb_utils
[HV_HEARTBEAT_MSG
].callback
= NULL
;
289 if (hv_cb_utils
[HV_KVP_MSG
].channel
!= NULL
)
290 hv_cb_utils
[HV_KVP_MSG
].channel
->onchannel_callback
=
292 hv_cb_utils
[HV_KVP_MSG
].callback
= NULL
;
298 kfree(hbeat_txf_buf
);
301 module_init(init_hyperv_utils
);
302 module_exit(exit_hyperv_utils
);
304 MODULE_DESCRIPTION("Hyper-V Utilities");
305 MODULE_VERSION(HV_DRV_VERSION
);
306 MODULE_LICENSE("GPL");