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 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/sysctl.h>
26 #include <linux/reboot.h>
31 #include "vmbus_packet_format.h"
32 #include "vmbus_channel_interface.h"
33 #include "version_info.h"
35 #include "vmbus_private.h"
36 #include "vmbus_api.h"
40 static void shutdown_onchannelcallback(void *context)
42 struct vmbus_channel *channel = context;
46 u8 execute_shutdown = false;
48 struct shutdown_msg_data *shutdown_msg;
50 struct icmsg_hdr *icmsghdrp;
51 struct icmsg_negotiate *negop = NULL;
54 buf = kmalloc(buflen, GFP_ATOMIC);
56 VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid);
59 DPRINT_DBG(VMBUS, "shutdown packet: len=%d, requestid=%lld",
62 icmsghdrp = (struct icmsg_hdr *)&buf[
63 sizeof(struct vmbuspipe_hdr)];
65 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
66 prep_negotiate_resp(icmsghdrp, negop, buf);
68 shutdown_msg = (struct shutdown_msg_data *)&buf[
69 sizeof(struct vmbuspipe_hdr) +
70 sizeof(struct icmsg_hdr)];
72 switch (shutdown_msg->flags) {
75 icmsghdrp->status = HV_S_OK;
76 execute_shutdown = true;
78 DPRINT_INFO(VMBUS, "Shutdown request received -"
79 " gracefull shutdown initiated");
82 icmsghdrp->status = HV_E_FAIL;
83 execute_shutdown = false;
85 DPRINT_INFO(VMBUS, "Shutdown request received -"
91 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
92 | ICMSGHDRFLAG_RESPONSE;
94 VmbusChannelSendPacket(channel, buf,
96 VmbusPacketTypeDataInBand, 0);
103 if (execute_shutdown == true)
104 orderly_poweroff(false);
108 * Set guest time to host UTC time.
110 static inline void do_adj_guesttime(u64 hosttime)
113 struct timespec host_ts;
115 host_tns = (hosttime - WLTIMEDELTA) * 100;
116 host_ts = ns_to_timespec(host_tns);
118 do_settimeofday(&host_ts);
122 * Synchronize time with host after reboot, restore, etc.
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.
132 static inline void adj_guesttime(u64 hosttime, u8 flags)
134 static s32 scnt = 50;
136 if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
137 do_adj_guesttime(hosttime);
141 if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
143 do_adj_guesttime(hosttime);
148 * Time Sync Channel message handler.
150 static void timesync_onchannelcallback(void *context)
152 struct vmbus_channel *channel = context;
156 struct icmsg_hdr *icmsghdrp;
157 struct ictimesync_data *timedatap;
160 buf = kmalloc(buflen, GFP_ATOMIC);
162 VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid);
165 DPRINT_DBG(VMBUS, "timesync packet: recvlen=%d, requestid=%lld",
168 icmsghdrp = (struct icmsg_hdr *)&buf[
169 sizeof(struct vmbuspipe_hdr)];
171 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
172 prep_negotiate_resp(icmsghdrp, NULL, buf);
174 timedatap = (struct ictimesync_data *)&buf[
175 sizeof(struct vmbuspipe_hdr) +
176 sizeof(struct icmsg_hdr)];
177 adj_guesttime(timedatap->parenttime, timedatap->flags);
180 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
181 | ICMSGHDRFLAG_RESPONSE;
183 VmbusChannelSendPacket(channel, buf,
185 VmbusPacketTypeDataInBand, 0);
194 * Heartbeat functionality.
195 * Every two seconds, Hyper-V send us a heartbeat request message.
196 * we respond to this message, and Hyper-V knows we are alive.
198 static void heartbeat_onchannelcallback(void *context)
200 struct vmbus_channel *channel = context;
204 struct icmsg_hdr *icmsghdrp;
205 struct heartbeat_msg_data *heartbeat_msg;
208 buf = kmalloc(buflen, GFP_ATOMIC);
210 VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid);
213 DPRINT_DBG(VMBUS, "heartbeat packet: len=%d, requestid=%lld",
216 icmsghdrp = (struct icmsg_hdr *)&buf[
217 sizeof(struct vmbuspipe_hdr)];
219 icmsghdrp = (struct icmsg_hdr *)&buf[
220 sizeof(struct vmbuspipe_hdr)];
222 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
223 prep_negotiate_resp(icmsghdrp, NULL, buf);
225 heartbeat_msg = (struct heartbeat_msg_data *)&buf[
226 sizeof(struct vmbuspipe_hdr) +
227 sizeof(struct icmsg_hdr)];
229 DPRINT_DBG(VMBUS, "heartbeat seq = %lld",
230 heartbeat_msg->seq_num);
232 heartbeat_msg->seq_num += 1;
235 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
236 | ICMSGHDRFLAG_RESPONSE;
238 VmbusChannelSendPacket(channel, buf,
240 VmbusPacketTypeDataInBand, 0);
248 static int __init init_hyperv_utils(void)
250 printk(KERN_INFO "Registering HyperV Utility Driver\n");
252 hv_cb_utils[HV_SHUTDOWN_MSG].channel->OnChannelCallback =
253 &shutdown_onchannelcallback;
254 hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback;
256 hv_cb_utils[HV_TIMESYNC_MSG].channel->OnChannelCallback =
257 ×ync_onchannelcallback;
258 hv_cb_utils[HV_TIMESYNC_MSG].callback = ×ync_onchannelcallback;
260 hv_cb_utils[HV_HEARTBEAT_MSG].channel->OnChannelCallback =
261 &heartbeat_onchannelcallback;
262 hv_cb_utils[HV_HEARTBEAT_MSG].callback = &heartbeat_onchannelcallback;
267 static void exit_hyperv_utils(void)
269 printk(KERN_INFO "De-Registered HyperV Utility Driver\n");
271 hv_cb_utils[HV_SHUTDOWN_MSG].channel->OnChannelCallback =
273 hv_cb_utils[HV_SHUTDOWN_MSG].callback = &chn_cb_negotiate;
275 hv_cb_utils[HV_TIMESYNC_MSG].channel->OnChannelCallback =
277 hv_cb_utils[HV_TIMESYNC_MSG].callback = &chn_cb_negotiate;
279 hv_cb_utils[HV_HEARTBEAT_MSG].channel->OnChannelCallback =
281 hv_cb_utils[HV_HEARTBEAT_MSG].callback = &chn_cb_negotiate;
284 module_init(init_hyperv_utils);
285 module_exit(exit_hyperv_utils);
287 MODULE_DESCRIPTION("Hyper-V Utilities");
288 MODULE_VERSION(HV_DRV_VERSION);
289 MODULE_LICENSE("GPL");