hv_util.c 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2010, Microsoft Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 * Authors:
 *   Haiyang Zhang <haiyangz@microsoft.com>
 *   Hank Janssen  <hjanssen@microsoft.com>
 */
21 22
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

23 24 25 26 27
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sysctl.h>
28
#include <linux/reboot.h>
29
#include <linux/hyperv.h>
30

31
#include "hyperv_vmbus.h"
32

33 34 35
#define SD_MAJOR	3
#define SD_MINOR	0
#define SD_VERSION	(SD_MAJOR << 16 | SD_MINOR)
36

37 38
#define SD_MAJOR_1	1
#define SD_VERSION_1	(SD_MAJOR_1 << 16 | SD_MINOR)
39

40
#define TS_MAJOR	4
41 42 43
#define TS_MINOR	0
#define TS_VERSION	(TS_MAJOR << 16 | TS_MINOR)

44 45
#define TS_MAJOR_1	1
#define TS_VERSION_1	(TS_MAJOR_1 << 16 | TS_MINOR)
46

47 48 49
#define TS_MAJOR_3	3
#define TS_VERSION_3	(TS_MAJOR_3 << 16 | TS_MINOR)

50
#define HB_MAJOR	3
51
#define HB_MINOR	0
52 53
#define HB_VERSION	(HB_MAJOR << 16 | HB_MINOR)

54 55
#define HB_MAJOR_1	1
#define HB_VERSION_1	(HB_MAJOR_1 << 16 | HB_MINOR)
56 57 58 59 60

static int sd_srv_version;
static int ts_srv_version;
static int hb_srv_version;
static int util_fw_version;
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
static void shutdown_onchannelcallback(void *context);
static struct hv_util_service util_shutdown = {
	.util_cb = shutdown_onchannelcallback,
};

static void timesync_onchannelcallback(void *context);
static struct hv_util_service util_timesynch = {
	.util_cb = timesync_onchannelcallback,
};

static void heartbeat_onchannelcallback(void *context);
static struct hv_util_service util_heartbeat = {
	.util_cb = heartbeat_onchannelcallback,
};

static struct hv_util_service util_kvp = {
	.util_cb = hv_kvp_onchannelcallback,
	.util_init = hv_kvp_init,
	.util_deinit = hv_kvp_deinit,
};
82

83 84 85 86 87 88
static struct hv_util_service util_vss = {
	.util_cb = hv_vss_onchannelcallback,
	.util_init = hv_vss_init,
	.util_deinit = hv_vss_deinit,
};

89 90 91 92 93 94
static struct hv_util_service util_fcopy = {
	.util_cb = hv_fcopy_onchannelcallback,
	.util_init = hv_fcopy_init,
	.util_deinit = hv_fcopy_deinit,
};

95 96 97 98 99 100 101 102 103 104
static void perform_shutdown(struct work_struct *dummy)
{
	orderly_poweroff(true);
}

/*
 * Perform the shutdown operation in a thread context.
 */
static DECLARE_WORK(shutdown_work, perform_shutdown);

105
static void shutdown_onchannelcallback(void *context)
106 107
{
	struct vmbus_channel *channel = context;
108
	u32 recvlen;
109
	u64 requestid;
110
	bool execute_shutdown = false;
111
	u8  *shut_txf_buf = util_shutdown.recv_buffer;
112 113 114 115 116 117

	struct shutdown_msg_data *shutdown_msg;

	struct icmsg_hdr *icmsghdrp;
	struct icmsg_negotiate *negop = NULL;

118 119
	vmbus_recvpacket(channel, shut_txf_buf,
			 PAGE_SIZE, &recvlen, &requestid);
120 121

	if (recvlen > 0) {
122
		icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
123 124 125
			sizeof(struct vmbuspipe_hdr)];

		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
126
			vmbus_prep_negotiate_resp(icmsghdrp, negop,
127 128
					shut_txf_buf, util_fw_version,
					sd_srv_version);
129
		} else {
130 131 132 133
			shutdown_msg =
				(struct shutdown_msg_data *)&shut_txf_buf[
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct icmsg_hdr)];
134 135 136 137 138 139 140

			switch (shutdown_msg->flags) {
			case 0:
			case 1:
				icmsghdrp->status = HV_S_OK;
				execute_shutdown = true;

141
				pr_info("Shutdown request received -"
142
					    " graceful shutdown initiated\n");
143 144 145 146 147
				break;
			default:
				icmsghdrp->status = HV_E_FAIL;
				execute_shutdown = false;

148 149
				pr_info("Shutdown request received -"
					    " Invalid request\n");
150
				break;
151
			}
152 153 154 155 156
		}

		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
			| ICMSGHDRFLAG_RESPONSE;

157
		vmbus_sendpacket(channel, shut_txf_buf,
158
				       recvlen, requestid,
159
				       VM_PKT_DATA_INBAND, 0);
160 161 162
	}

	if (execute_shutdown == true)
163
		schedule_work(&shutdown_work);
164 165
}

166 167 168 169 170 171 172
/*
 * Set the host time in a process context.
 */

struct adj_time_work {
	struct work_struct work;
	u64	host_time;
173 174
	u64	ref_time;
	u8	flags;
175 176 177 178 179
};

static void hv_set_host_time(struct work_struct *work)
{
	struct adj_time_work	*wrk;
180 181 182
	s64 host_tns;
	u64 newtime;
	struct timespec host_ts;
183 184

	wrk = container_of(work, struct adj_time_work, work);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	newtime = wrk->host_time;
	if (ts_srv_version > TS_VERSION_3) {
		/*
		 * Some latency has been introduced since Hyper-V generated
		 * its time sample. Take that latency into account before
		 * using TSC reference time sample from Hyper-V.
		 *
		 * This sample is given by TimeSync v4 and above hosts.
		 */
		u64 current_tick;

		rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
		newtime += (current_tick - wrk->ref_time);
	}
	host_tns = (newtime - WLTIMEDELTA) * 100;
	host_ts = ns_to_timespec(host_tns);

	do_settimeofday(&host_ts);
204 205 206
	kfree(wrk);
}

207 208 209 210 211 212
/*
 * Synchronize time with host after reboot, restore, etc.
 *
 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
 * message after the timesync channel is opened. Since the hv_utils module is
213 214 215 216 217 218
 * loaded after hv_vmbus, the first message is usually missed. This bit is
 * considered a hard request to discipline the clock.
 *
 * ICTIMESYNCFLAG_SAMPLE bit indicates a time sample from host. This is
 * typically used as a hint to the guest. The guest is under no obligation
 * to discipline the clock.
219
 */
220
static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 flags)
221
{
222
	struct adj_time_work    *wrk;
223

224 225 226 227 228
	wrk = kmalloc(sizeof(struct adj_time_work), GFP_ATOMIC);
	if (wrk == NULL)
		return;

	wrk->host_time = hosttime;
229 230
	wrk->ref_time = reftime;
	wrk->flags = flags;
231
	if ((flags & (ICTIMESYNCFLAG_SYNC | ICTIMESYNCFLAG_SAMPLE)) != 0) {
232 233 234 235
		INIT_WORK(&wrk->work, hv_set_host_time);
		schedule_work(&wrk->work);
	} else
		kfree(wrk);
236 237 238 239 240 241 242 243
}

/*
 * Time Sync Channel message handler.
 */
static void timesync_onchannelcallback(void *context)
{
	struct vmbus_channel *channel = context;
244
	u32 recvlen;
245 246 247
	u64 requestid;
	struct icmsg_hdr *icmsghdrp;
	struct ictimesync_data *timedatap;
248
	struct ictimesync_ref_data *refdata;
249
	u8 *time_txf_buf = util_timesynch.recv_buffer;
250
	struct icmsg_negotiate *negop = NULL;
251

252 253
	vmbus_recvpacket(channel, time_txf_buf,
			 PAGE_SIZE, &recvlen, &requestid);
254 255

	if (recvlen > 0) {
256
		icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
257 258 259
				sizeof(struct vmbuspipe_hdr)];

		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
260 261 262 263
			vmbus_prep_negotiate_resp(icmsghdrp, negop,
						time_txf_buf,
						util_fw_version,
						ts_srv_version);
264 265
			pr_info("Using TimeSync version %d.%d\n",
				ts_srv_version >> 16, ts_srv_version & 0xFFFF);
266
		} else {
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
			if (ts_srv_version > TS_VERSION_3) {
				refdata = (struct ictimesync_ref_data *)
					&time_txf_buf[
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct icmsg_hdr)];

				adj_guesttime(refdata->parenttime,
						refdata->vmreferencetime,
						refdata->flags);
			} else {
				timedatap = (struct ictimesync_data *)
					&time_txf_buf[
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct icmsg_hdr)];
				adj_guesttime(timedatap->parenttime,
						0,
						timedatap->flags);
			}
285 286 287 288 289
		}

		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
			| ICMSGHDRFLAG_RESPONSE;

290
		vmbus_sendpacket(channel, time_txf_buf,
291
				recvlen, requestid,
292
				VM_PKT_DATA_INBAND, 0);
293 294 295
	}
}

296 297 298 299 300 301 302 303
/*
 * Heartbeat functionality.
 * Every two seconds, Hyper-V send us a heartbeat request message.
 * we respond to this message, and Hyper-V knows we are alive.
 */
static void heartbeat_onchannelcallback(void *context)
{
	struct vmbus_channel *channel = context;
304
	u32 recvlen;
305 306 307
	u64 requestid;
	struct icmsg_hdr *icmsghdrp;
	struct heartbeat_msg_data *heartbeat_msg;
308
	u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
309
	struct icmsg_negotiate *negop = NULL;
310

311 312
	vmbus_recvpacket(channel, hbeat_txf_buf,
			 PAGE_SIZE, &recvlen, &requestid);
313 314

	if (recvlen > 0) {
315
		icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
316 317 318
				sizeof(struct vmbuspipe_hdr)];

		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
319 320 321
			vmbus_prep_negotiate_resp(icmsghdrp, negop,
				hbeat_txf_buf, util_fw_version,
				hb_srv_version);
322
		} else {
323 324 325 326
			heartbeat_msg =
				(struct heartbeat_msg_data *)&hbeat_txf_buf[
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct icmsg_hdr)];
327 328 329 330 331 332 333

			heartbeat_msg->seq_num += 1;
		}

		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
			| ICMSGHDRFLAG_RESPONSE;

334
		vmbus_sendpacket(channel, hbeat_txf_buf,
335
				       recvlen, requestid,
336
				       VM_PKT_DATA_INBAND, 0);
337 338
	}
}
339

340 341
static int util_probe(struct hv_device *dev,
			const struct hv_vmbus_device_id *dev_id)
342
{
343 344 345 346
	struct hv_util_service *srv =
		(struct hv_util_service *)dev_id->driver_data;
	int ret;

347
	srv->recv_buffer = kmalloc(PAGE_SIZE * 4, GFP_KERNEL);
348 349
	if (!srv->recv_buffer)
		return -ENOMEM;
350
	srv->channel = dev->channel;
351 352 353
	if (srv->util_init) {
		ret = srv->util_init(srv);
		if (ret) {
354 355
			ret = -ENODEV;
			goto error1;
356 357 358
		}
	}

359 360 361 362 363 364 365 366 367 368
	/*
	 * The set of services managed by the util driver are not performance
	 * critical and do not need batched reading. Furthermore, some services
	 * such as KVP can only handle one message from the host at a time.
	 * Turn off batched reading for all util drivers before we open the
	 * channel.
	 */

	set_channel_read_state(dev->channel, false);

369
	hv_set_drvdata(dev, srv);
370

371 372 373 374 375 376 377
	/*
	 * Based on the host; initialize the framework and
	 * service version numbers we will negotiate.
	 */
	switch (vmbus_proto_version) {
	case (VERSION_WS2008):
		util_fw_version = UTIL_WS2K8_FW_VERSION;
378 379 380
		sd_srv_version = SD_VERSION_1;
		ts_srv_version = TS_VERSION_1;
		hb_srv_version = HB_VERSION_1;
381
		break;
382
	case(VERSION_WIN10):
383 384 385 386
		util_fw_version = UTIL_FW_VERSION;
		sd_srv_version = SD_VERSION;
		ts_srv_version = TS_VERSION;
		hb_srv_version = HB_VERSION;
387 388 389 390 391 392
		break;
	default:
		util_fw_version = UTIL_FW_VERSION;
		sd_srv_version = SD_VERSION;
		ts_srv_version = TS_VERSION_3;
		hb_srv_version = HB_VERSION;
393 394
	}

395 396 397 398 399
	ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
			srv->util_cb, dev->channel);
	if (ret)
		goto error;

400
	return 0;
401 402 403 404 405 406 407

error:
	if (srv->util_deinit)
		srv->util_deinit();
error1:
	kfree(srv->recv_buffer);
	return ret;
408 409 410 411
}

static int util_remove(struct hv_device *dev)
{
412 413 414 415
	struct hv_util_service *srv = hv_get_drvdata(dev);

	if (srv->util_deinit)
		srv->util_deinit();
416
	vmbus_close(dev->channel);
417 418
	kfree(srv->recv_buffer);

419 420 421 422
	return 0;
}

static const struct hv_vmbus_device_id id_table[] = {
423
	/* Shutdown guid */
424 425 426
	{ HV_SHUTDOWN_GUID,
	  .driver_data = (unsigned long)&util_shutdown
	},
427
	/* Time synch guid */
428 429 430
	{ HV_TS_GUID,
	  .driver_data = (unsigned long)&util_timesynch
	},
431
	/* Heartbeat guid */
432 433 434
	{ HV_HEART_BEAT_GUID,
	  .driver_data = (unsigned long)&util_heartbeat
	},
435
	/* KVP guid */
436 437 438
	{ HV_KVP_GUID,
	  .driver_data = (unsigned long)&util_kvp
	},
439 440 441 442
	/* VSS GUID */
	{ HV_VSS_GUID,
	  .driver_data = (unsigned long)&util_vss
	},
443 444 445 446
	/* File copy GUID */
	{ HV_FCOPY_GUID,
	  .driver_data = (unsigned long)&util_fcopy
	},
447
	{ },
448 449 450 451 452 453
};

MODULE_DEVICE_TABLE(vmbus, id_table);

/* The one and only one */
static  struct hv_driver util_drv = {
454
	.name = "hv_util",
455 456 457 458 459
	.id_table = id_table,
	.probe =  util_probe,
	.remove =  util_remove,
};

460 461
static int __init init_hyperv_utils(void)
{
462
	pr_info("Registering HyperV Utility Driver\n");
463

464
	return vmbus_driver_register(&util_drv);
465 466 467 468
}

static void exit_hyperv_utils(void)
{
469
	pr_info("De-Registered HyperV Utility Driver\n");
470

471
	vmbus_driver_unregister(&util_drv);
472 473 474 475 476 477 478
}

module_init(init_hyperv_utils);
module_exit(exit_hyperv_utils);

MODULE_DESCRIPTION("Hyper-V Utilities");
MODULE_LICENSE("GPL");