blob: c164b54b4cd774ee37b562315a91026088f62ff5 [file] [log] [blame]
Hank Janssenc88c4e42010-05-04 15:55:05 -07001/*
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 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Hank Janssenc88c4e42010-05-04 15:55:05 -070023#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/sysctl.h>
Greg Kroah-Hartman9e629072010-05-04 08:26:23 -070028#include <linux/reboot.h>
Haiyang Zhangd7507852010-05-19 15:56:28 +000029#include <linux/dmi.h>
30#include <linux/pci.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070031
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070032#include "hyperv.h"
Ky Srinivasan245ba562010-12-16 18:54:16 -070033#include "hv_kvp.h"
Hank Janssenc88c4e42010-05-04 15:55:05 -070034
Hank Janssen45241e52010-12-13 16:23:36 -080035static u8 *shut_txf_buf;
36static u8 *time_txf_buf;
37static u8 *hbeat_txf_buf;
Hank Janssenc88c4e42010-05-04 15:55:05 -070038
Greg Kroah-Hartman66109442010-05-04 14:31:18 -070039static void shutdown_onchannelcallback(void *context)
Hank Janssenc88c4e42010-05-04 15:55:05 -070040{
41 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -080042 u32 recvlen;
Hank Janssenc88c4e42010-05-04 15:55:05 -070043 u64 requestid;
44 u8 execute_shutdown = false;
45
46 struct shutdown_msg_data *shutdown_msg;
47
48 struct icmsg_hdr *icmsghdrp;
49 struct icmsg_negotiate *negop = NULL;
50
Hank Janssen45241e52010-12-13 16:23:36 -080051 vmbus_recvpacket(channel, shut_txf_buf,
52 PAGE_SIZE, &recvlen, &requestid);
Hank Janssenc88c4e42010-05-04 15:55:05 -070053
54 if (recvlen > 0) {
Hank Janssen45241e52010-12-13 16:23:36 -080055 icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
Hank Janssenc88c4e42010-05-04 15:55:05 -070056 sizeof(struct vmbuspipe_hdr)];
57
58 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
Hank Janssen45241e52010-12-13 16:23:36 -080059 prep_negotiate_resp(icmsghdrp, negop, shut_txf_buf);
Hank Janssenc88c4e42010-05-04 15:55:05 -070060 } else {
Hank Janssen45241e52010-12-13 16:23:36 -080061 shutdown_msg =
62 (struct shutdown_msg_data *)&shut_txf_buf[
63 sizeof(struct vmbuspipe_hdr) +
64 sizeof(struct icmsg_hdr)];
Hank Janssenc88c4e42010-05-04 15:55:05 -070065
66 switch (shutdown_msg->flags) {
67 case 0:
68 case 1:
69 icmsghdrp->status = HV_S_OK;
70 execute_shutdown = true;
71
Hank Janssenaf7a5b62011-03-29 13:58:49 -070072 pr_info("Shutdown request received -"
Greg Kroah-Hartman32235b02011-04-13 12:14:05 -070073 " graceful shutdown initiated\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -070074 break;
75 default:
76 icmsghdrp->status = HV_E_FAIL;
77 execute_shutdown = false;
78
Hank Janssenaf7a5b62011-03-29 13:58:49 -070079 pr_info("Shutdown request received -"
80 " Invalid request\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -070081 break;
Joe Perches95cd17c2011-04-10 14:31:35 -070082 }
Hank Janssenc88c4e42010-05-04 15:55:05 -070083 }
84
85 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
86 | ICMSGHDRFLAG_RESPONSE;
87
Hank Janssen45241e52010-12-13 16:23:36 -080088 vmbus_sendpacket(channel, shut_txf_buf,
Hank Janssenc88c4e42010-05-04 15:55:05 -070089 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -080090 VM_PKT_DATA_INBAND, 0);
Hank Janssenc88c4e42010-05-04 15:55:05 -070091 }
92
Hank Janssenc88c4e42010-05-04 15:55:05 -070093 if (execute_shutdown == true)
Greg Kroah-Hartman9e629072010-05-04 08:26:23 -070094 orderly_poweroff(false);
Hank Janssenc88c4e42010-05-04 15:55:05 -070095}
96
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +000097/*
Haiyang Zhange9ec3602010-05-11 15:11:24 +000098 * Set guest time to host UTC time.
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +000099 */
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000100static inline void do_adj_guesttime(u64 hosttime)
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000101{
102 s64 host_tns;
103 struct timespec host_ts;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000104
105 host_tns = (hosttime - WLTIMEDELTA) * 100;
106 host_ts = ns_to_timespec(host_tns);
107
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000108 do_settimeofday(&host_ts);
109}
110
111/*
112 * Synchronize time with host after reboot, restore, etc.
113 *
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.
121 */
122static inline void adj_guesttime(u64 hosttime, u8 flags)
123{
124 static s32 scnt = 50;
125
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000126 if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000127 do_adj_guesttime(hosttime);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000128 return;
129 }
130
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000131 if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000132 scnt--;
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000133 do_adj_guesttime(hosttime);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000134 }
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000135}
136
137/*
138 * Time Sync Channel message handler.
139 */
140static void timesync_onchannelcallback(void *context)
141{
142 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -0800143 u32 recvlen;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000144 u64 requestid;
145 struct icmsg_hdr *icmsghdrp;
146 struct ictimesync_data *timedatap;
147
Hank Janssen45241e52010-12-13 16:23:36 -0800148 vmbus_recvpacket(channel, time_txf_buf,
149 PAGE_SIZE, &recvlen, &requestid);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000150
151 if (recvlen > 0) {
Hank Janssen45241e52010-12-13 16:23:36 -0800152 icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000153 sizeof(struct vmbuspipe_hdr)];
154
155 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
Hank Janssen45241e52010-12-13 16:23:36 -0800156 prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000157 } else {
Hank Janssen45241e52010-12-13 16:23:36 -0800158 timedatap = (struct ictimesync_data *)&time_txf_buf[
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000159 sizeof(struct vmbuspipe_hdr) +
160 sizeof(struct icmsg_hdr)];
161 adj_guesttime(timedatap->parenttime, timedatap->flags);
162 }
163
164 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
165 | ICMSGHDRFLAG_RESPONSE;
166
Hank Janssen45241e52010-12-13 16:23:36 -0800167 vmbus_sendpacket(channel, time_txf_buf,
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000168 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800169 VM_PKT_DATA_INBAND, 0);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000170 }
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000171}
172
Hank Janssen9153f7b2010-05-15 14:39:58 -0700173/*
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.
177 */
178static void heartbeat_onchannelcallback(void *context)
179{
180 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -0800181 u32 recvlen;
Hank Janssen9153f7b2010-05-15 14:39:58 -0700182 u64 requestid;
183 struct icmsg_hdr *icmsghdrp;
184 struct heartbeat_msg_data *heartbeat_msg;
185
Hank Janssen45241e52010-12-13 16:23:36 -0800186 vmbus_recvpacket(channel, hbeat_txf_buf,
187 PAGE_SIZE, &recvlen, &requestid);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700188
189 if (recvlen > 0) {
Hank Janssen45241e52010-12-13 16:23:36 -0800190 icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
Hank Janssen9153f7b2010-05-15 14:39:58 -0700191 sizeof(struct vmbuspipe_hdr)];
192
193 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
Hank Janssen45241e52010-12-13 16:23:36 -0800194 prep_negotiate_resp(icmsghdrp, NULL, hbeat_txf_buf);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700195 } else {
Hank Janssen45241e52010-12-13 16:23:36 -0800196 heartbeat_msg =
197 (struct heartbeat_msg_data *)&hbeat_txf_buf[
198 sizeof(struct vmbuspipe_hdr) +
199 sizeof(struct icmsg_hdr)];
Hank Janssen9153f7b2010-05-15 14:39:58 -0700200
Hank Janssen9153f7b2010-05-15 14:39:58 -0700201 heartbeat_msg->seq_num += 1;
202 }
203
204 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
205 | ICMSGHDRFLAG_RESPONSE;
206
Hank Janssen45241e52010-12-13 16:23:36 -0800207 vmbus_sendpacket(channel, hbeat_txf_buf,
Hank Janssen9153f7b2010-05-15 14:39:58 -0700208 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800209 VM_PKT_DATA_INBAND, 0);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700210 }
Hank Janssen9153f7b2010-05-15 14:39:58 -0700211}
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000212
Haiyang Zhangd7507852010-05-19 15:56:28 +0000213static const struct pci_device_id __initconst
214hv_utils_pci_table[] __maybe_unused = {
215 { PCI_DEVICE(0x1414, 0x5353) }, /* Hyper-V emulated VGA controller */
216 { 0 }
217};
218MODULE_DEVICE_TABLE(pci, hv_utils_pci_table);
219
220
221static const struct dmi_system_id __initconst
222hv_utils_dmi_table[] __maybe_unused = {
223 {
224 .ident = "Hyper-V",
225 .matches = {
226 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
227 DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
228 DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
229 },
230 },
231 { },
232};
233MODULE_DEVICE_TABLE(dmi, hv_utils_dmi_table);
234
235
Hank Janssenc88c4e42010-05-04 15:55:05 -0700236static int __init init_hyperv_utils(void)
237{
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700238 pr_info("Registering HyperV Utility Driver\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700239
Ky Srinivasan245ba562010-12-16 18:54:16 -0700240 if (hv_kvp_init())
241 return -ENODEV;
242
243
Haiyang Zhangd7507852010-05-19 15:56:28 +0000244 if (!dmi_check_system(hv_utils_dmi_table))
245 return -ENODEV;
246
Hank Janssen45241e52010-12-13 16:23:36 -0800247 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);
250
251 if (!shut_txf_buf || !time_txf_buf || !hbeat_txf_buf) {
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700252 pr_info("Unable to allocate memory for receive buffer\n");
Hank Janssen45241e52010-12-13 16:23:36 -0800253 kfree(shut_txf_buf);
254 kfree(time_txf_buf);
255 kfree(hbeat_txf_buf);
256 return -ENOMEM;
257 }
258
Hank Janssenc88c4e42010-05-04 15:55:05 -0700259 hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback;
260
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000261 hv_cb_utils[HV_TIMESYNC_MSG].callback = &timesync_onchannelcallback;
262
Hank Janssen9153f7b2010-05-15 14:39:58 -0700263 hv_cb_utils[HV_HEARTBEAT_MSG].callback = &heartbeat_onchannelcallback;
264
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700265 hv_cb_utils[HV_KVP_MSG].callback = &hv_kvp_onchannelcallback;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700266
Hank Janssenc88c4e42010-05-04 15:55:05 -0700267 return 0;
268}
269
270static void exit_hyperv_utils(void)
271{
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700272 pr_info("De-Registered HyperV Utility Driver\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700273
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700274 if (hv_cb_utils[HV_SHUTDOWN_MSG].channel != NULL)
275 hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback =
276 &chn_cb_negotiate;
277 hv_cb_utils[HV_SHUTDOWN_MSG].callback = NULL;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000278
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700279 if (hv_cb_utils[HV_TIMESYNC_MSG].channel != NULL)
280 hv_cb_utils[HV_TIMESYNC_MSG].channel->onchannel_callback =
281 &chn_cb_negotiate;
282 hv_cb_utils[HV_TIMESYNC_MSG].callback = NULL;
Hank Janssen9153f7b2010-05-15 14:39:58 -0700283
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700284 if (hv_cb_utils[HV_HEARTBEAT_MSG].channel != NULL)
285 hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback =
286 &chn_cb_negotiate;
287 hv_cb_utils[HV_HEARTBEAT_MSG].callback = NULL;
Hank Janssen45241e52010-12-13 16:23:36 -0800288
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700289 if (hv_cb_utils[HV_KVP_MSG].channel != NULL)
290 hv_cb_utils[HV_KVP_MSG].channel->onchannel_callback =
291 &chn_cb_negotiate;
292 hv_cb_utils[HV_KVP_MSG].callback = NULL;
293
Ky Srinivasan245ba562010-12-16 18:54:16 -0700294 hv_kvp_deinit();
295
Hank Janssen45241e52010-12-13 16:23:36 -0800296 kfree(shut_txf_buf);
297 kfree(time_txf_buf);
298 kfree(hbeat_txf_buf);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700299}
300
301module_init(init_hyperv_utils);
302module_exit(exit_hyperv_utils);
303
304MODULE_DESCRIPTION("Hyper-V Utilities");
305MODULE_VERSION(HV_DRV_VERSION);
306MODULE_LICENSE("GPL");