hv_util.c 8.9 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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,
};
51

52
static void shutdown_onchannelcallback(void *context)
53 54
{
	struct vmbus_channel *channel = context;
55
	u32 recvlen;
56 57
	u64 requestid;
	u8  execute_shutdown = false;
58
	u8  *shut_txf_buf = util_shutdown.recv_buffer;
59 60 61 62 63 64

	struct shutdown_msg_data *shutdown_msg;

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

65 66
	vmbus_recvpacket(channel, shut_txf_buf,
			 PAGE_SIZE, &recvlen, &requestid);
67 68

	if (recvlen > 0) {
69
		icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
70 71 72
			sizeof(struct vmbuspipe_hdr)];

		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
73 74
			vmbus_prep_negotiate_resp(icmsghdrp, negop,
					shut_txf_buf, MAX_SRV_VER, MAX_SRV_VER);
75
		} else {
76 77 78 79
			shutdown_msg =
				(struct shutdown_msg_data *)&shut_txf_buf[
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct icmsg_hdr)];
80 81 82 83 84 85 86

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

87
				pr_info("Shutdown request received -"
88
					    " graceful shutdown initiated\n");
89 90 91 92 93
				break;
			default:
				icmsghdrp->status = HV_E_FAIL;
				execute_shutdown = false;

94 95
				pr_info("Shutdown request received -"
					    " Invalid request\n");
96
				break;
97
			}
98 99 100 101 102
		}

		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
			| ICMSGHDRFLAG_RESPONSE;

103
		vmbus_sendpacket(channel, shut_txf_buf,
104
				       recvlen, requestid,
105
				       VM_PKT_DATA_INBAND, 0);
106 107 108
	}

	if (execute_shutdown == true)
109
		orderly_poweroff(true);
110 111
}

112
/*
113
 * Set guest time to host UTC time.
114
 */
115
static inline void do_adj_guesttime(u64 hosttime)
116 117 118 119 120 121 122
{
	s64 host_tns;
	struct timespec host_ts;

	host_tns = (hosttime - WLTIMEDELTA) * 100;
	host_ts = ns_to_timespec(host_tns);

123 124 125
	do_settimeofday(&host_ts);
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/*
 * Set the host time in a process context.
 */

struct adj_time_work {
	struct work_struct work;
	u64	host_time;
};

static void hv_set_host_time(struct work_struct *work)
{
	struct adj_time_work	*wrk;

	wrk = container_of(work, struct adj_time_work, work);
	do_adj_guesttime(wrk->host_time);
	kfree(wrk);
}

144 145 146 147 148 149 150 151 152 153 154 155 156
/*
 * 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
 * loaded after hv_vmbus, the first message is usually missed. The other
 * thing is, systime is automatically set to emulated hardware clock which may
 * not be UTC time or in the same time zone. So, to override these effects, we
 * use the first 50 time samples for initial system time setting.
 */
static inline void adj_guesttime(u64 hosttime, u8 flags)
{
157
	struct adj_time_work    *wrk;
158 159
	static s32 scnt = 50;

160 161 162 163 164
	wrk = kmalloc(sizeof(struct adj_time_work), GFP_ATOMIC);
	if (wrk == NULL)
		return;

	wrk->host_time = hosttime;
165
	if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
166 167
		INIT_WORK(&wrk->work, hv_set_host_time);
		schedule_work(&wrk->work);
168 169 170
		return;
	}

171
	if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
172
		scnt--;
173 174 175 176
		INIT_WORK(&wrk->work, hv_set_host_time);
		schedule_work(&wrk->work);
	} else
		kfree(wrk);
177 178 179 180 181 182 183 184
}

/*
 * Time Sync Channel message handler.
 */
static void timesync_onchannelcallback(void *context)
{
	struct vmbus_channel *channel = context;
185
	u32 recvlen;
186 187 188
	u64 requestid;
	struct icmsg_hdr *icmsghdrp;
	struct ictimesync_data *timedatap;
189
	u8 *time_txf_buf = util_timesynch.recv_buffer;
190

191 192
	vmbus_recvpacket(channel, time_txf_buf,
			 PAGE_SIZE, &recvlen, &requestid);
193 194

	if (recvlen > 0) {
195
		icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
196 197 198
				sizeof(struct vmbuspipe_hdr)];

		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
199 200
			vmbus_prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf,
						MAX_SRV_VER, MAX_SRV_VER);
201
		} else {
202
			timedatap = (struct ictimesync_data *)&time_txf_buf[
203 204 205 206 207 208 209 210
				sizeof(struct vmbuspipe_hdr) +
				sizeof(struct icmsg_hdr)];
			adj_guesttime(timedatap->parenttime, timedatap->flags);
		}

		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
			| ICMSGHDRFLAG_RESPONSE;

211
		vmbus_sendpacket(channel, time_txf_buf,
212
				recvlen, requestid,
213
				VM_PKT_DATA_INBAND, 0);
214 215 216
	}
}

217 218 219 220 221 222 223 224
/*
 * 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;
225
	u32 recvlen;
226 227 228
	u64 requestid;
	struct icmsg_hdr *icmsghdrp;
	struct heartbeat_msg_data *heartbeat_msg;
229
	u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
230

231 232
	vmbus_recvpacket(channel, hbeat_txf_buf,
			 PAGE_SIZE, &recvlen, &requestid);
233 234

	if (recvlen > 0) {
235
		icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
236 237 238
				sizeof(struct vmbuspipe_hdr)];

		if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
239 240
			vmbus_prep_negotiate_resp(icmsghdrp, NULL,
				hbeat_txf_buf, MAX_SRV_VER, MAX_SRV_VER);
241
		} else {
242 243 244 245
			heartbeat_msg =
				(struct heartbeat_msg_data *)&hbeat_txf_buf[
					sizeof(struct vmbuspipe_hdr) +
					sizeof(struct icmsg_hdr)];
246 247 248 249 250 251 252

			heartbeat_msg->seq_num += 1;
		}

		icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
			| ICMSGHDRFLAG_RESPONSE;

253
		vmbus_sendpacket(channel, hbeat_txf_buf,
254
				       recvlen, requestid,
255
				       VM_PKT_DATA_INBAND, 0);
256 257
	}
}
258

259 260
static int util_probe(struct hv_device *dev,
			const struct hv_vmbus_device_id *dev_id)
261
{
262 263 264 265
	struct hv_util_service *srv =
		(struct hv_util_service *)dev_id->driver_data;
	int ret;

266
	srv->recv_buffer = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
267 268 269 270 271
	if (!srv->recv_buffer)
		return -ENOMEM;
	if (srv->util_init) {
		ret = srv->util_init(srv);
		if (ret) {
272 273
			ret = -ENODEV;
			goto error1;
274 275 276
		}
	}

277 278 279 280 281 282 283 284 285 286
	/*
	 * 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);

287
	ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
288 289 290 291
			srv->util_cb, dev->channel);
	if (ret)
		goto error;

292
	hv_set_drvdata(dev, srv);
293
	return 0;
294 295 296 297 298 299 300

error:
	if (srv->util_deinit)
		srv->util_deinit();
error1:
	kfree(srv->recv_buffer);
	return ret;
301 302 303 304
}

static int util_remove(struct hv_device *dev)
{
305 306
	struct hv_util_service *srv = hv_get_drvdata(dev);

307
	vmbus_close(dev->channel);
308 309 310 311
	if (srv->util_deinit)
		srv->util_deinit();
	kfree(srv->recv_buffer);

312 313 314 315
	return 0;
}

static const struct hv_vmbus_device_id id_table[] = {
316
	/* Shutdown guid */
317 318 319
	{ HV_SHUTDOWN_GUID,
	  .driver_data = (unsigned long)&util_shutdown
	},
320
	/* Time synch guid */
321 322 323
	{ HV_TS_GUID,
	  .driver_data = (unsigned long)&util_timesynch
	},
324
	/* Heartbeat guid */
325 326 327
	{ HV_HEART_BEAT_GUID,
	  .driver_data = (unsigned long)&util_heartbeat
	},
328
	/* KVP guid */
329 330 331
	{ HV_KVP_GUID,
	  .driver_data = (unsigned long)&util_kvp
	},
332
	{ },
333 334 335 336 337 338
};

MODULE_DEVICE_TABLE(vmbus, id_table);

/* The one and only one */
static  struct hv_driver util_drv = {
339
	.name = "hv_util",
340 341 342 343 344
	.id_table = id_table,
	.probe =  util_probe,
	.remove =  util_remove,
};

345 346
static int __init init_hyperv_utils(void)
{
347
	pr_info("Registering HyperV Utility Driver\n");
348

349
	return vmbus_driver_register(&util_drv);
350 351 352 353
}

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

356
	vmbus_driver_unregister(&util_drv);
357 358 359 360 361 362 363 364
}

module_init(init_hyperv_utils);
module_exit(exit_hyperv_utils);

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