blob: 876d44a8a8616aa82fd4eb9325de6979b85c6621 [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>
Hank Janssenc88c4e42010-05-04 15:55:05 -070029
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070030#include "hyperv.h"
Ky Srinivasan245ba562010-12-16 18:54:16 -070031#include "hv_kvp.h"
Hank Janssenc88c4e42010-05-04 15:55:05 -070032
Hank Janssen45241e52010-12-13 16:23:36 -080033static u8 *shut_txf_buf;
34static u8 *time_txf_buf;
35static u8 *hbeat_txf_buf;
Hank Janssenc88c4e42010-05-04 15:55:05 -070036
Greg Kroah-Hartman66109442010-05-04 14:31:18 -070037static void shutdown_onchannelcallback(void *context)
Hank Janssenc88c4e42010-05-04 15:55:05 -070038{
39 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -080040 u32 recvlen;
Hank Janssenc88c4e42010-05-04 15:55:05 -070041 u64 requestid;
42 u8 execute_shutdown = false;
43
44 struct shutdown_msg_data *shutdown_msg;
45
46 struct icmsg_hdr *icmsghdrp;
47 struct icmsg_negotiate *negop = NULL;
48
Hank Janssen45241e52010-12-13 16:23:36 -080049 vmbus_recvpacket(channel, shut_txf_buf,
50 PAGE_SIZE, &recvlen, &requestid);
Hank Janssenc88c4e42010-05-04 15:55:05 -070051
52 if (recvlen > 0) {
Hank Janssen45241e52010-12-13 16:23:36 -080053 icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
Hank Janssenc88c4e42010-05-04 15:55:05 -070054 sizeof(struct vmbuspipe_hdr)];
55
56 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
Hank Janssen45241e52010-12-13 16:23:36 -080057 prep_negotiate_resp(icmsghdrp, negop, shut_txf_buf);
Hank Janssenc88c4e42010-05-04 15:55:05 -070058 } else {
Hank Janssen45241e52010-12-13 16:23:36 -080059 shutdown_msg =
60 (struct shutdown_msg_data *)&shut_txf_buf[
61 sizeof(struct vmbuspipe_hdr) +
62 sizeof(struct icmsg_hdr)];
Hank Janssenc88c4e42010-05-04 15:55:05 -070063
64 switch (shutdown_msg->flags) {
65 case 0:
66 case 1:
67 icmsghdrp->status = HV_S_OK;
68 execute_shutdown = true;
69
Hank Janssenaf7a5b62011-03-29 13:58:49 -070070 pr_info("Shutdown request received -"
Greg Kroah-Hartman32235b02011-04-13 12:14:05 -070071 " graceful shutdown initiated\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -070072 break;
73 default:
74 icmsghdrp->status = HV_E_FAIL;
75 execute_shutdown = false;
76
Hank Janssenaf7a5b62011-03-29 13:58:49 -070077 pr_info("Shutdown request received -"
78 " Invalid request\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -070079 break;
Joe Perches95cd17c2011-04-10 14:31:35 -070080 }
Hank Janssenc88c4e42010-05-04 15:55:05 -070081 }
82
83 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
84 | ICMSGHDRFLAG_RESPONSE;
85
Hank Janssen45241e52010-12-13 16:23:36 -080086 vmbus_sendpacket(channel, shut_txf_buf,
Hank Janssenc88c4e42010-05-04 15:55:05 -070087 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -080088 VM_PKT_DATA_INBAND, 0);
Hank Janssenc88c4e42010-05-04 15:55:05 -070089 }
90
Hank Janssenc88c4e42010-05-04 15:55:05 -070091 if (execute_shutdown == true)
K. Y. Srinivasanae2a0b42011-08-27 11:31:30 -070092 orderly_poweroff(true);
Hank Janssenc88c4e42010-05-04 15:55:05 -070093}
94
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +000095/*
Haiyang Zhange9ec3602010-05-11 15:11:24 +000096 * Set guest time to host UTC time.
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +000097 */
Haiyang Zhange9ec3602010-05-11 15:11:24 +000098static inline void do_adj_guesttime(u64 hosttime)
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +000099{
100 s64 host_tns;
101 struct timespec host_ts;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000102
103 host_tns = (hosttime - WLTIMEDELTA) * 100;
104 host_ts = ns_to_timespec(host_tns);
105
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000106 do_settimeofday(&host_ts);
107}
108
109/*
110 * Synchronize time with host after reboot, restore, etc.
111 *
112 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
113 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
114 * message after the timesync channel is opened. Since the hv_utils module is
115 * loaded after hv_vmbus, the first message is usually missed. The other
116 * thing is, systime is automatically set to emulated hardware clock which may
117 * not be UTC time or in the same time zone. So, to override these effects, we
118 * use the first 50 time samples for initial system time setting.
119 */
120static inline void adj_guesttime(u64 hosttime, u8 flags)
121{
122 static s32 scnt = 50;
123
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000124 if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000125 do_adj_guesttime(hosttime);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000126 return;
127 }
128
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000129 if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000130 scnt--;
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000131 do_adj_guesttime(hosttime);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000132 }
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000133}
134
135/*
136 * Time Sync Channel message handler.
137 */
138static void timesync_onchannelcallback(void *context)
139{
140 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -0800141 u32 recvlen;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000142 u64 requestid;
143 struct icmsg_hdr *icmsghdrp;
144 struct ictimesync_data *timedatap;
145
Hank Janssen45241e52010-12-13 16:23:36 -0800146 vmbus_recvpacket(channel, time_txf_buf,
147 PAGE_SIZE, &recvlen, &requestid);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000148
149 if (recvlen > 0) {
Hank Janssen45241e52010-12-13 16:23:36 -0800150 icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000151 sizeof(struct vmbuspipe_hdr)];
152
153 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
Hank Janssen45241e52010-12-13 16:23:36 -0800154 prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000155 } else {
Hank Janssen45241e52010-12-13 16:23:36 -0800156 timedatap = (struct ictimesync_data *)&time_txf_buf[
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000157 sizeof(struct vmbuspipe_hdr) +
158 sizeof(struct icmsg_hdr)];
159 adj_guesttime(timedatap->parenttime, timedatap->flags);
160 }
161
162 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
163 | ICMSGHDRFLAG_RESPONSE;
164
Hank Janssen45241e52010-12-13 16:23:36 -0800165 vmbus_sendpacket(channel, time_txf_buf,
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000166 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800167 VM_PKT_DATA_INBAND, 0);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000168 }
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000169}
170
Hank Janssen9153f7b2010-05-15 14:39:58 -0700171/*
172 * Heartbeat functionality.
173 * Every two seconds, Hyper-V send us a heartbeat request message.
174 * we respond to this message, and Hyper-V knows we are alive.
175 */
176static void heartbeat_onchannelcallback(void *context)
177{
178 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -0800179 u32 recvlen;
Hank Janssen9153f7b2010-05-15 14:39:58 -0700180 u64 requestid;
181 struct icmsg_hdr *icmsghdrp;
182 struct heartbeat_msg_data *heartbeat_msg;
183
Hank Janssen45241e52010-12-13 16:23:36 -0800184 vmbus_recvpacket(channel, hbeat_txf_buf,
185 PAGE_SIZE, &recvlen, &requestid);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700186
187 if (recvlen > 0) {
Hank Janssen45241e52010-12-13 16:23:36 -0800188 icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
Hank Janssen9153f7b2010-05-15 14:39:58 -0700189 sizeof(struct vmbuspipe_hdr)];
190
191 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
Hank Janssen45241e52010-12-13 16:23:36 -0800192 prep_negotiate_resp(icmsghdrp, NULL, hbeat_txf_buf);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700193 } else {
Hank Janssen45241e52010-12-13 16:23:36 -0800194 heartbeat_msg =
195 (struct heartbeat_msg_data *)&hbeat_txf_buf[
196 sizeof(struct vmbuspipe_hdr) +
197 sizeof(struct icmsg_hdr)];
Hank Janssen9153f7b2010-05-15 14:39:58 -0700198
Hank Janssen9153f7b2010-05-15 14:39:58 -0700199 heartbeat_msg->seq_num += 1;
200 }
201
202 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
203 | ICMSGHDRFLAG_RESPONSE;
204
Hank Janssen45241e52010-12-13 16:23:36 -0800205 vmbus_sendpacket(channel, hbeat_txf_buf,
Hank Janssen9153f7b2010-05-15 14:39:58 -0700206 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800207 VM_PKT_DATA_INBAND, 0);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700208 }
Hank Janssen9153f7b2010-05-15 14:39:58 -0700209}
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000210
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700211/*
212 * The devices managed by the util driver don't need any additional
213 * setup.
214 */
215static int util_probe(struct hv_device *dev)
216{
217 return 0;
218}
219
220static int util_remove(struct hv_device *dev)
221{
222 return 0;
223}
224
225static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700226 /* Shutdown guid */
227 { VMBUS_DEVICE(0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
228 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB) },
229 /* Time synch guid */
230 { VMBUS_DEVICE(0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
231 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf) },
232 /* Heartbeat guid */
233 { VMBUS_DEVICE(0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
234 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d) },
235 /* KVP guid */
236 { VMBUS_DEVICE(0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
237 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6) },
238 { },
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700239};
240
241MODULE_DEVICE_TABLE(vmbus, id_table);
242
243/* The one and only one */
244static struct hv_driver util_drv = {
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700245 .name = "hv_util",
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700246 .id_table = id_table,
247 .probe = util_probe,
248 .remove = util_remove,
249};
250
Hank Janssenc88c4e42010-05-04 15:55:05 -0700251static int __init init_hyperv_utils(void)
252{
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700253 pr_info("Registering HyperV Utility Driver\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700254
Ky Srinivasan245ba562010-12-16 18:54:16 -0700255 if (hv_kvp_init())
256 return -ENODEV;
257
258
Hank Janssen45241e52010-12-13 16:23:36 -0800259 shut_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
260 time_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
261 hbeat_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
262
263 if (!shut_txf_buf || !time_txf_buf || !hbeat_txf_buf) {
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700264 pr_info("Unable to allocate memory for receive buffer\n");
Hank Janssen45241e52010-12-13 16:23:36 -0800265 kfree(shut_txf_buf);
266 kfree(time_txf_buf);
267 kfree(hbeat_txf_buf);
268 return -ENOMEM;
269 }
270
Hank Janssenc88c4e42010-05-04 15:55:05 -0700271 hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback;
272
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000273 hv_cb_utils[HV_TIMESYNC_MSG].callback = &timesync_onchannelcallback;
274
Hank Janssen9153f7b2010-05-15 14:39:58 -0700275 hv_cb_utils[HV_HEARTBEAT_MSG].callback = &heartbeat_onchannelcallback;
276
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700277 hv_cb_utils[HV_KVP_MSG].callback = &hv_kvp_onchannelcallback;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700278
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700279 return vmbus_driver_register(&util_drv);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700280}
281
282static void exit_hyperv_utils(void)
283{
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700284 pr_info("De-Registered HyperV Utility Driver\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700285
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700286 if (hv_cb_utils[HV_SHUTDOWN_MSG].channel != NULL)
287 hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback =
288 &chn_cb_negotiate;
289 hv_cb_utils[HV_SHUTDOWN_MSG].callback = NULL;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000290
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700291 if (hv_cb_utils[HV_TIMESYNC_MSG].channel != NULL)
292 hv_cb_utils[HV_TIMESYNC_MSG].channel->onchannel_callback =
293 &chn_cb_negotiate;
294 hv_cb_utils[HV_TIMESYNC_MSG].callback = NULL;
Hank Janssen9153f7b2010-05-15 14:39:58 -0700295
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700296 if (hv_cb_utils[HV_HEARTBEAT_MSG].channel != NULL)
297 hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback =
298 &chn_cb_negotiate;
299 hv_cb_utils[HV_HEARTBEAT_MSG].callback = NULL;
Hank Janssen45241e52010-12-13 16:23:36 -0800300
K. Y. Srinivasanb9d8e352011-05-16 06:44:36 -0700301 if (hv_cb_utils[HV_KVP_MSG].channel != NULL)
302 hv_cb_utils[HV_KVP_MSG].channel->onchannel_callback =
303 &chn_cb_negotiate;
304 hv_cb_utils[HV_KVP_MSG].callback = NULL;
305
Ky Srinivasan245ba562010-12-16 18:54:16 -0700306 hv_kvp_deinit();
307
Hank Janssen45241e52010-12-13 16:23:36 -0800308 kfree(shut_txf_buf);
309 kfree(time_txf_buf);
310 kfree(hbeat_txf_buf);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700311 vmbus_driver_unregister(&util_drv);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700312}
313
314module_init(init_hyperv_utils);
315module_exit(exit_hyperv_utils);
316
317MODULE_DESCRIPTION("Hyper-V Utilities");
318MODULE_VERSION(HV_DRV_VERSION);
319MODULE_LICENSE("GPL");