blob: cb82233541b196a53177007ebb4520a9e4112442 [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>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070029#include <linux/hyperv.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070030
K. Y. Srinivasan67413352013-07-02 10:31:30 -070031#define SHUTDOWN_MAJOR 3
32#define SHUTDOWN_MINOR 0
33#define SHUTDOWN_MAJOR_MINOR (SHUTDOWN_MAJOR << 16 | SHUTDOWN_MINOR)
34
35#define TIMESYNCH_MAJOR 3
36#define TIMESYNCH_MINOR 0
37#define TIMESYNCH_MAJOR_MINOR (TIMESYNCH_MAJOR << 16 | TIMESYNCH_MINOR)
38
39#define HEARTBEAT_MAJOR 3
40#define HEARTBEAT_MINOR 0
41#define HEARTBEAT_MAJOR_MINOR (HEARTBEAT_MAJOR << 16 | HEARTBEAT_MINOR)
42
K. Y. Srinivasana29b6432011-09-18 10:31:33 -070043static void shutdown_onchannelcallback(void *context);
44static struct hv_util_service util_shutdown = {
45 .util_cb = shutdown_onchannelcallback,
46};
47
48static void timesync_onchannelcallback(void *context);
49static struct hv_util_service util_timesynch = {
50 .util_cb = timesync_onchannelcallback,
51};
52
53static void heartbeat_onchannelcallback(void *context);
54static struct hv_util_service util_heartbeat = {
55 .util_cb = heartbeat_onchannelcallback,
56};
57
58static struct hv_util_service util_kvp = {
59 .util_cb = hv_kvp_onchannelcallback,
60 .util_init = hv_kvp_init,
61 .util_deinit = hv_kvp_deinit,
62};
Hank Janssenc88c4e42010-05-04 15:55:05 -070063
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070064static struct hv_util_service util_vss = {
65 .util_cb = hv_vss_onchannelcallback,
66 .util_init = hv_vss_init,
67 .util_deinit = hv_vss_deinit,
68};
69
K. Y. Srinivasan3dd6cb42013-01-23 17:42:45 -080070static void perform_shutdown(struct work_struct *dummy)
71{
72 orderly_poweroff(true);
73}
74
75/*
76 * Perform the shutdown operation in a thread context.
77 */
78static DECLARE_WORK(shutdown_work, perform_shutdown);
79
Greg Kroah-Hartman66109442010-05-04 14:31:18 -070080static void shutdown_onchannelcallback(void *context)
Hank Janssenc88c4e42010-05-04 15:55:05 -070081{
82 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -080083 u32 recvlen;
Hank Janssenc88c4e42010-05-04 15:55:05 -070084 u64 requestid;
85 u8 execute_shutdown = false;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -070086 u8 *shut_txf_buf = util_shutdown.recv_buffer;
Hank Janssenc88c4e42010-05-04 15:55:05 -070087
88 struct shutdown_msg_data *shutdown_msg;
89
90 struct icmsg_hdr *icmsghdrp;
91 struct icmsg_negotiate *negop = NULL;
92
Hank Janssen45241e52010-12-13 16:23:36 -080093 vmbus_recvpacket(channel, shut_txf_buf,
94 PAGE_SIZE, &recvlen, &requestid);
Hank Janssenc88c4e42010-05-04 15:55:05 -070095
96 if (recvlen > 0) {
Hank Janssen45241e52010-12-13 16:23:36 -080097 icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
Hank Janssenc88c4e42010-05-04 15:55:05 -070098 sizeof(struct vmbuspipe_hdr)];
99
100 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700101 vmbus_prep_negotiate_resp(icmsghdrp, negop,
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700102 shut_txf_buf, UTIL_FW_MAJOR_MINOR,
103 SHUTDOWN_MAJOR_MINOR);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700104 } else {
Hank Janssen45241e52010-12-13 16:23:36 -0800105 shutdown_msg =
106 (struct shutdown_msg_data *)&shut_txf_buf[
107 sizeof(struct vmbuspipe_hdr) +
108 sizeof(struct icmsg_hdr)];
Hank Janssenc88c4e42010-05-04 15:55:05 -0700109
110 switch (shutdown_msg->flags) {
111 case 0:
112 case 1:
113 icmsghdrp->status = HV_S_OK;
114 execute_shutdown = true;
115
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700116 pr_info("Shutdown request received -"
Greg Kroah-Hartman32235b02011-04-13 12:14:05 -0700117 " graceful shutdown initiated\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700118 break;
119 default:
120 icmsghdrp->status = HV_E_FAIL;
121 execute_shutdown = false;
122
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700123 pr_info("Shutdown request received -"
124 " Invalid request\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700125 break;
Joe Perches95cd17c2011-04-10 14:31:35 -0700126 }
Hank Janssenc88c4e42010-05-04 15:55:05 -0700127 }
128
129 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
130 | ICMSGHDRFLAG_RESPONSE;
131
Hank Janssen45241e52010-12-13 16:23:36 -0800132 vmbus_sendpacket(channel, shut_txf_buf,
Hank Janssenc88c4e42010-05-04 15:55:05 -0700133 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800134 VM_PKT_DATA_INBAND, 0);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700135 }
136
Hank Janssenc88c4e42010-05-04 15:55:05 -0700137 if (execute_shutdown == true)
K. Y. Srinivasan3dd6cb42013-01-23 17:42:45 -0800138 schedule_work(&shutdown_work);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700139}
140
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000141/*
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000142 * Set guest time to host UTC time.
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000143 */
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000144static inline void do_adj_guesttime(u64 hosttime)
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000145{
146 s64 host_tns;
147 struct timespec host_ts;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000148
149 host_tns = (hosttime - WLTIMEDELTA) * 100;
150 host_ts = ns_to_timespec(host_tns);
151
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000152 do_settimeofday(&host_ts);
153}
154
155/*
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700156 * Set the host time in a process context.
157 */
158
159struct adj_time_work {
160 struct work_struct work;
161 u64 host_time;
162};
163
164static void hv_set_host_time(struct work_struct *work)
165{
166 struct adj_time_work *wrk;
167
168 wrk = container_of(work, struct adj_time_work, work);
169 do_adj_guesttime(wrk->host_time);
170 kfree(wrk);
171}
172
173/*
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000174 * Synchronize time with host after reboot, restore, etc.
175 *
176 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
177 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
178 * message after the timesync channel is opened. Since the hv_utils module is
179 * loaded after hv_vmbus, the first message is usually missed. The other
180 * thing is, systime is automatically set to emulated hardware clock which may
181 * not be UTC time or in the same time zone. So, to override these effects, we
182 * use the first 50 time samples for initial system time setting.
183 */
184static inline void adj_guesttime(u64 hosttime, u8 flags)
185{
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700186 struct adj_time_work *wrk;
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000187 static s32 scnt = 50;
188
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700189 wrk = kmalloc(sizeof(struct adj_time_work), GFP_ATOMIC);
190 if (wrk == NULL)
191 return;
192
193 wrk->host_time = hosttime;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000194 if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700195 INIT_WORK(&wrk->work, hv_set_host_time);
196 schedule_work(&wrk->work);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000197 return;
198 }
199
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000200 if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000201 scnt--;
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700202 INIT_WORK(&wrk->work, hv_set_host_time);
203 schedule_work(&wrk->work);
204 } else
205 kfree(wrk);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000206}
207
208/*
209 * Time Sync Channel message handler.
210 */
211static void timesync_onchannelcallback(void *context)
212{
213 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -0800214 u32 recvlen;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000215 u64 requestid;
216 struct icmsg_hdr *icmsghdrp;
217 struct ictimesync_data *timedatap;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700218 u8 *time_txf_buf = util_timesynch.recv_buffer;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000219
Hank Janssen45241e52010-12-13 16:23:36 -0800220 vmbus_recvpacket(channel, time_txf_buf,
221 PAGE_SIZE, &recvlen, &requestid);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000222
223 if (recvlen > 0) {
Hank Janssen45241e52010-12-13 16:23:36 -0800224 icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000225 sizeof(struct vmbuspipe_hdr)];
226
227 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700228 vmbus_prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf,
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700229 UTIL_FW_MAJOR_MINOR,
230 TIMESYNCH_MAJOR_MINOR);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000231 } else {
Hank Janssen45241e52010-12-13 16:23:36 -0800232 timedatap = (struct ictimesync_data *)&time_txf_buf[
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000233 sizeof(struct vmbuspipe_hdr) +
234 sizeof(struct icmsg_hdr)];
235 adj_guesttime(timedatap->parenttime, timedatap->flags);
236 }
237
238 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
239 | ICMSGHDRFLAG_RESPONSE;
240
Hank Janssen45241e52010-12-13 16:23:36 -0800241 vmbus_sendpacket(channel, time_txf_buf,
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000242 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800243 VM_PKT_DATA_INBAND, 0);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000244 }
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000245}
246
Hank Janssen9153f7b2010-05-15 14:39:58 -0700247/*
248 * Heartbeat functionality.
249 * Every two seconds, Hyper-V send us a heartbeat request message.
250 * we respond to this message, and Hyper-V knows we are alive.
251 */
252static void heartbeat_onchannelcallback(void *context)
253{
254 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -0800255 u32 recvlen;
Hank Janssen9153f7b2010-05-15 14:39:58 -0700256 u64 requestid;
257 struct icmsg_hdr *icmsghdrp;
258 struct heartbeat_msg_data *heartbeat_msg;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700259 u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
Hank Janssen9153f7b2010-05-15 14:39:58 -0700260
Hank Janssen45241e52010-12-13 16:23:36 -0800261 vmbus_recvpacket(channel, hbeat_txf_buf,
262 PAGE_SIZE, &recvlen, &requestid);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700263
264 if (recvlen > 0) {
Hank Janssen45241e52010-12-13 16:23:36 -0800265 icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
Hank Janssen9153f7b2010-05-15 14:39:58 -0700266 sizeof(struct vmbuspipe_hdr)];
267
268 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700269 vmbus_prep_negotiate_resp(icmsghdrp, NULL,
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700270 hbeat_txf_buf, UTIL_FW_MAJOR_MINOR,
271 HEARTBEAT_MAJOR_MINOR);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700272 } else {
Hank Janssen45241e52010-12-13 16:23:36 -0800273 heartbeat_msg =
274 (struct heartbeat_msg_data *)&hbeat_txf_buf[
275 sizeof(struct vmbuspipe_hdr) +
276 sizeof(struct icmsg_hdr)];
Hank Janssen9153f7b2010-05-15 14:39:58 -0700277
Hank Janssen9153f7b2010-05-15 14:39:58 -0700278 heartbeat_msg->seq_num += 1;
279 }
280
281 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
282 | ICMSGHDRFLAG_RESPONSE;
283
Hank Janssen45241e52010-12-13 16:23:36 -0800284 vmbus_sendpacket(channel, hbeat_txf_buf,
Hank Janssen9153f7b2010-05-15 14:39:58 -0700285 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800286 VM_PKT_DATA_INBAND, 0);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700287 }
Hank Janssen9153f7b2010-05-15 14:39:58 -0700288}
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000289
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700290static int util_probe(struct hv_device *dev,
291 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700292{
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700293 struct hv_util_service *srv =
294 (struct hv_util_service *)dev_id->driver_data;
295 int ret;
296
K. Y. Srinivasan9b595782012-08-13 10:06:51 -0700297 srv->recv_buffer = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700298 if (!srv->recv_buffer)
299 return -ENOMEM;
300 if (srv->util_init) {
301 ret = srv->util_init(srv);
302 if (ret) {
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700303 ret = -ENODEV;
304 goto error1;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700305 }
306 }
307
K. Y. Srinivasan7ae3e032012-12-01 06:46:34 -0800308 /*
309 * The set of services managed by the util driver are not performance
310 * critical and do not need batched reading. Furthermore, some services
311 * such as KVP can only handle one message from the host at a time.
312 * Turn off batched reading for all util drivers before we open the
313 * channel.
314 */
315
316 set_channel_read_state(dev->channel, false);
317
K. Y. Srinivasan9b595782012-08-13 10:06:51 -0700318 ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700319 srv->util_cb, dev->channel);
320 if (ret)
321 goto error;
322
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700323 hv_set_drvdata(dev, srv);
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700324 return 0;
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700325
326error:
327 if (srv->util_deinit)
328 srv->util_deinit();
329error1:
330 kfree(srv->recv_buffer);
331 return ret;
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700332}
333
334static int util_remove(struct hv_device *dev)
335{
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700336 struct hv_util_service *srv = hv_get_drvdata(dev);
337
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700338 vmbus_close(dev->channel);
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700339 if (srv->util_deinit)
340 srv->util_deinit();
341 kfree(srv->recv_buffer);
342
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700343 return 0;
344}
345
346static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700347 /* Shutdown guid */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -0800348 { HV_SHUTDOWN_GUID,
349 .driver_data = (unsigned long)&util_shutdown
350 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700351 /* Time synch guid */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -0800352 { HV_TS_GUID,
353 .driver_data = (unsigned long)&util_timesynch
354 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700355 /* Heartbeat guid */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -0800356 { HV_HEART_BEAT_GUID,
357 .driver_data = (unsigned long)&util_heartbeat
358 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700359 /* KVP guid */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -0800360 { HV_KVP_GUID,
361 .driver_data = (unsigned long)&util_kvp
362 },
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700363 /* VSS GUID */
364 { HV_VSS_GUID,
365 .driver_data = (unsigned long)&util_vss
366 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700367 { },
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700368};
369
370MODULE_DEVICE_TABLE(vmbus, id_table);
371
372/* The one and only one */
373static struct hv_driver util_drv = {
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700374 .name = "hv_util",
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700375 .id_table = id_table,
376 .probe = util_probe,
377 .remove = util_remove,
378};
379
Hank Janssenc88c4e42010-05-04 15:55:05 -0700380static int __init init_hyperv_utils(void)
381{
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700382 pr_info("Registering HyperV Utility Driver\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700383
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700384 return vmbus_driver_register(&util_drv);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700385}
386
387static void exit_hyperv_utils(void)
388{
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700389 pr_info("De-Registered HyperV Utility Driver\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700390
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700391 vmbus_driver_unregister(&util_drv);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700392}
393
394module_init(init_hyperv_utils);
395module_exit(exit_hyperv_utils);
396
397MODULE_DESCRIPTION("Hyper-V Utilities");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700398MODULE_LICENSE("GPL");