2010-05-04 22:55:05 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
*/
|
2011-03-29 20:58:49 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2010-05-04 22:55:05 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/sysctl.h>
|
2010-05-04 15:26:23 +00:00
|
|
|
#include <linux/reboot.h>
|
2010-05-19 15:56:28 +00:00
|
|
|
#include <linux/dmi.h>
|
|
|
|
#include <linux/pci.h>
|
2010-05-04 22:55:05 +00:00
|
|
|
|
|
|
|
#include "logging.h"
|
2011-02-11 18:00:12 +00:00
|
|
|
#include "hv_api.h"
|
2010-05-04 22:55:05 +00:00
|
|
|
#include "vmbus.h"
|
2010-05-06 05:50:11 +00:00
|
|
|
#include "vmbus_packet_format.h"
|
2010-05-06 05:47:13 +00:00
|
|
|
#include "vmbus_channel_interface.h"
|
2010-05-06 05:52:28 +00:00
|
|
|
#include "version_info.h"
|
2010-05-06 05:18:38 +00:00
|
|
|
#include "channel.h"
|
2010-05-06 05:45:25 +00:00
|
|
|
#include "vmbus_private.h"
|
2010-05-06 05:43:02 +00:00
|
|
|
#include "vmbus_api.h"
|
2010-05-04 22:55:05 +00:00
|
|
|
#include "utils.h"
|
2010-12-17 01:54:16 +00:00
|
|
|
#include "hv_kvp.h"
|
2010-05-04 22:55:05 +00:00
|
|
|
|
2010-12-14 00:23:36 +00:00
|
|
|
static u8 *shut_txf_buf;
|
|
|
|
static u8 *time_txf_buf;
|
|
|
|
static u8 *hbeat_txf_buf;
|
2010-05-04 22:55:05 +00:00
|
|
|
|
2010-05-04 21:31:18 +00:00
|
|
|
static void shutdown_onchannelcallback(void *context)
|
2010-05-04 22:55:05 +00:00
|
|
|
{
|
|
|
|
struct vmbus_channel *channel = context;
|
2010-12-14 00:23:36 +00:00
|
|
|
u32 recvlen;
|
2010-05-04 22:55:05 +00:00
|
|
|
u64 requestid;
|
|
|
|
u8 execute_shutdown = false;
|
|
|
|
|
|
|
|
struct shutdown_msg_data *shutdown_msg;
|
|
|
|
|
|
|
|
struct icmsg_hdr *icmsghdrp;
|
|
|
|
struct icmsg_negotiate *negop = NULL;
|
|
|
|
|
2010-12-14 00:23:36 +00:00
|
|
|
vmbus_recvpacket(channel, shut_txf_buf,
|
|
|
|
PAGE_SIZE, &recvlen, &requestid);
|
2010-05-04 22:55:05 +00:00
|
|
|
|
|
|
|
if (recvlen > 0) {
|
2010-12-14 00:23:36 +00:00
|
|
|
icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
|
2010-05-04 22:55:05 +00:00
|
|
|
sizeof(struct vmbuspipe_hdr)];
|
|
|
|
|
|
|
|
if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
|
2010-12-14 00:23:36 +00:00
|
|
|
prep_negotiate_resp(icmsghdrp, negop, shut_txf_buf);
|
2010-05-04 22:55:05 +00:00
|
|
|
} else {
|
2010-12-14 00:23:36 +00:00
|
|
|
shutdown_msg =
|
|
|
|
(struct shutdown_msg_data *)&shut_txf_buf[
|
|
|
|
sizeof(struct vmbuspipe_hdr) +
|
|
|
|
sizeof(struct icmsg_hdr)];
|
2010-05-04 22:55:05 +00:00
|
|
|
|
|
|
|
switch (shutdown_msg->flags) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
icmsghdrp->status = HV_S_OK;
|
|
|
|
execute_shutdown = true;
|
|
|
|
|
2011-03-29 20:58:49 +00:00
|
|
|
pr_info("Shutdown request received -"
|
2011-04-13 19:14:05 +00:00
|
|
|
" graceful shutdown initiated\n");
|
2010-05-04 22:55:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
icmsghdrp->status = HV_E_FAIL;
|
|
|
|
execute_shutdown = false;
|
|
|
|
|
2011-03-29 20:58:49 +00:00
|
|
|
pr_info("Shutdown request received -"
|
|
|
|
" Invalid request\n");
|
2010-05-04 22:55:05 +00:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
|
|
|
|
| ICMSGHDRFLAG_RESPONSE;
|
|
|
|
|
2010-12-14 00:23:36 +00:00
|
|
|
vmbus_sendpacket(channel, shut_txf_buf,
|
2010-05-04 22:55:05 +00:00
|
|
|
recvlen, requestid,
|
2011-01-26 20:12:13 +00:00
|
|
|
VM_PKT_DATA_INBAND, 0);
|
2010-05-04 22:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (execute_shutdown == true)
|
2010-05-04 15:26:23 +00:00
|
|
|
orderly_poweroff(false);
|
2010-05-04 22:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 19:23:46 +00:00
|
|
|
/*
|
2010-05-11 15:11:24 +00:00
|
|
|
* Set guest time to host UTC time.
|
2010-05-05 19:23:46 +00:00
|
|
|
*/
|
2010-05-11 15:11:24 +00:00
|
|
|
static inline void do_adj_guesttime(u64 hosttime)
|
2010-05-05 19:23:46 +00:00
|
|
|
{
|
|
|
|
s64 host_tns;
|
|
|
|
struct timespec host_ts;
|
|
|
|
|
|
|
|
host_tns = (hosttime - WLTIMEDELTA) * 100;
|
|
|
|
host_ts = ns_to_timespec(host_tns);
|
|
|
|
|
2010-05-11 15:11:24 +00:00
|
|
|
do_settimeofday(&host_ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
static s32 scnt = 50;
|
|
|
|
|
2010-05-05 19:23:46 +00:00
|
|
|
if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
|
2010-05-11 15:11:24 +00:00
|
|
|
do_adj_guesttime(hosttime);
|
2010-05-05 19:23:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-11 15:11:24 +00:00
|
|
|
if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
|
2010-05-05 19:23:46 +00:00
|
|
|
scnt--;
|
2010-05-11 15:11:24 +00:00
|
|
|
do_adj_guesttime(hosttime);
|
2010-05-05 19:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Time Sync Channel message handler.
|
|
|
|
*/
|
|
|
|
static void timesync_onchannelcallback(void *context)
|
|
|
|
{
|
|
|
|
struct vmbus_channel *channel = context;
|
2010-12-14 00:23:36 +00:00
|
|
|
u32 recvlen;
|
2010-05-05 19:23:46 +00:00
|
|
|
u64 requestid;
|
|
|
|
struct icmsg_hdr *icmsghdrp;
|
|
|
|
struct ictimesync_data *timedatap;
|
|
|
|
|
2010-12-14 00:23:36 +00:00
|
|
|
vmbus_recvpacket(channel, time_txf_buf,
|
|
|
|
PAGE_SIZE, &recvlen, &requestid);
|
2010-05-05 19:23:46 +00:00
|
|
|
|
|
|
|
if (recvlen > 0) {
|
2010-12-14 00:23:36 +00:00
|
|
|
icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
|
2010-05-05 19:23:46 +00:00
|
|
|
sizeof(struct vmbuspipe_hdr)];
|
|
|
|
|
|
|
|
if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
|
2010-12-14 00:23:36 +00:00
|
|
|
prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf);
|
2010-05-05 19:23:46 +00:00
|
|
|
} else {
|
2010-12-14 00:23:36 +00:00
|
|
|
timedatap = (struct ictimesync_data *)&time_txf_buf[
|
2010-05-05 19:23:46 +00:00
|
|
|
sizeof(struct vmbuspipe_hdr) +
|
|
|
|
sizeof(struct icmsg_hdr)];
|
|
|
|
adj_guesttime(timedatap->parenttime, timedatap->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
|
|
|
|
| ICMSGHDRFLAG_RESPONSE;
|
|
|
|
|
2010-12-14 00:23:36 +00:00
|
|
|
vmbus_sendpacket(channel, time_txf_buf,
|
2010-05-05 19:23:46 +00:00
|
|
|
recvlen, requestid,
|
2011-01-26 20:12:13 +00:00
|
|
|
VM_PKT_DATA_INBAND, 0);
|
2010-05-05 19:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-15 21:39:58 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2010-12-14 00:23:36 +00:00
|
|
|
u32 recvlen;
|
2010-05-15 21:39:58 +00:00
|
|
|
u64 requestid;
|
|
|
|
struct icmsg_hdr *icmsghdrp;
|
|
|
|
struct heartbeat_msg_data *heartbeat_msg;
|
|
|
|
|
2010-12-14 00:23:36 +00:00
|
|
|
vmbus_recvpacket(channel, hbeat_txf_buf,
|
|
|
|
PAGE_SIZE, &recvlen, &requestid);
|
2010-05-15 21:39:58 +00:00
|
|
|
|
|
|
|
if (recvlen > 0) {
|
2010-12-14 00:23:36 +00:00
|
|
|
icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
|
2010-05-15 21:39:58 +00:00
|
|
|
sizeof(struct vmbuspipe_hdr)];
|
|
|
|
|
|
|
|
if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
|
2010-12-14 00:23:36 +00:00
|
|
|
prep_negotiate_resp(icmsghdrp, NULL, hbeat_txf_buf);
|
2010-05-15 21:39:58 +00:00
|
|
|
} else {
|
2010-12-14 00:23:36 +00:00
|
|
|
heartbeat_msg =
|
|
|
|
(struct heartbeat_msg_data *)&hbeat_txf_buf[
|
|
|
|
sizeof(struct vmbuspipe_hdr) +
|
|
|
|
sizeof(struct icmsg_hdr)];
|
2010-05-15 21:39:58 +00:00
|
|
|
|
|
|
|
heartbeat_msg->seq_num += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
|
|
|
|
| ICMSGHDRFLAG_RESPONSE;
|
|
|
|
|
2010-12-14 00:23:36 +00:00
|
|
|
vmbus_sendpacket(channel, hbeat_txf_buf,
|
2010-05-15 21:39:58 +00:00
|
|
|
recvlen, requestid,
|
2011-01-26 20:12:13 +00:00
|
|
|
VM_PKT_DATA_INBAND, 0);
|
2010-05-15 21:39:58 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-05 19:23:46 +00:00
|
|
|
|
2010-05-19 15:56:28 +00:00
|
|
|
static const struct pci_device_id __initconst
|
|
|
|
hv_utils_pci_table[] __maybe_unused = {
|
|
|
|
{ PCI_DEVICE(0x1414, 0x5353) }, /* Hyper-V emulated VGA controller */
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(pci, hv_utils_pci_table);
|
|
|
|
|
|
|
|
|
|
|
|
static const struct dmi_system_id __initconst
|
|
|
|
hv_utils_dmi_table[] __maybe_unused = {
|
|
|
|
{
|
|
|
|
.ident = "Hyper-V",
|
|
|
|
.matches = {
|
|
|
|
DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
|
|
|
|
DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(dmi, hv_utils_dmi_table);
|
|
|
|
|
|
|
|
|
2010-05-04 22:55:05 +00:00
|
|
|
static int __init init_hyperv_utils(void)
|
|
|
|
{
|
2011-03-29 20:58:49 +00:00
|
|
|
pr_info("Registering HyperV Utility Driver\n");
|
2010-05-04 22:55:05 +00:00
|
|
|
|
2010-12-17 01:54:16 +00:00
|
|
|
if (hv_kvp_init())
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
|
2010-05-19 15:56:28 +00:00
|
|
|
if (!dmi_check_system(hv_utils_dmi_table))
|
|
|
|
return -ENODEV;
|
|
|
|
|
2010-12-14 00:23:36 +00:00
|
|
|
shut_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
|
|
|
|
time_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
|
|
|
|
hbeat_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!shut_txf_buf || !time_txf_buf || !hbeat_txf_buf) {
|
2011-03-29 20:58:49 +00:00
|
|
|
pr_info("Unable to allocate memory for receive buffer\n");
|
2010-12-14 00:23:36 +00:00
|
|
|
kfree(shut_txf_buf);
|
|
|
|
kfree(time_txf_buf);
|
|
|
|
kfree(hbeat_txf_buf);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2010-11-08 22:04:38 +00:00
|
|
|
hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback =
|
2010-05-04 22:55:05 +00:00
|
|
|
&shutdown_onchannelcallback;
|
|
|
|
hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback;
|
|
|
|
|
2010-11-08 22:04:38 +00:00
|
|
|
hv_cb_utils[HV_TIMESYNC_MSG].channel->onchannel_callback =
|
2010-05-05 19:23:46 +00:00
|
|
|
×ync_onchannelcallback;
|
|
|
|
hv_cb_utils[HV_TIMESYNC_MSG].callback = ×ync_onchannelcallback;
|
|
|
|
|
2010-11-08 22:04:38 +00:00
|
|
|
hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback =
|
2010-05-15 21:39:58 +00:00
|
|
|
&heartbeat_onchannelcallback;
|
|
|
|
hv_cb_utils[HV_HEARTBEAT_MSG].callback = &heartbeat_onchannelcallback;
|
|
|
|
|
2010-12-17 01:54:16 +00:00
|
|
|
hv_cb_utils[HV_KVP_MSG].channel->onchannel_callback =
|
|
|
|
&hv_kvp_onchannelcallback;
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-04 22:55:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void exit_hyperv_utils(void)
|
|
|
|
{
|
2011-03-29 20:58:49 +00:00
|
|
|
pr_info("De-Registered HyperV Utility Driver\n");
|
2010-05-04 22:55:05 +00:00
|
|
|
|
2010-11-08 22:04:38 +00:00
|
|
|
hv_cb_utils[HV_SHUTDOWN_MSG].channel->onchannel_callback =
|
2010-05-04 22:55:05 +00:00
|
|
|
&chn_cb_negotiate;
|
|
|
|
hv_cb_utils[HV_SHUTDOWN_MSG].callback = &chn_cb_negotiate;
|
2010-05-05 19:23:46 +00:00
|
|
|
|
2010-11-08 22:04:38 +00:00
|
|
|
hv_cb_utils[HV_TIMESYNC_MSG].channel->onchannel_callback =
|
2010-05-05 19:23:46 +00:00
|
|
|
&chn_cb_negotiate;
|
|
|
|
hv_cb_utils[HV_TIMESYNC_MSG].callback = &chn_cb_negotiate;
|
2010-05-15 21:39:58 +00:00
|
|
|
|
2010-11-08 22:04:38 +00:00
|
|
|
hv_cb_utils[HV_HEARTBEAT_MSG].channel->onchannel_callback =
|
2010-05-15 21:39:58 +00:00
|
|
|
&chn_cb_negotiate;
|
|
|
|
hv_cb_utils[HV_HEARTBEAT_MSG].callback = &chn_cb_negotiate;
|
2010-12-14 00:23:36 +00:00
|
|
|
|
2010-12-17 01:54:16 +00:00
|
|
|
hv_cb_utils[HV_KVP_MSG].channel->onchannel_callback =
|
|
|
|
&chn_cb_negotiate;
|
|
|
|
hv_kvp_deinit();
|
|
|
|
|
2010-12-14 00:23:36 +00:00
|
|
|
kfree(shut_txf_buf);
|
|
|
|
kfree(time_txf_buf);
|
|
|
|
kfree(hbeat_txf_buf);
|
2010-05-04 22:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(init_hyperv_utils);
|
|
|
|
module_exit(exit_hyperv_utils);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Hyper-V Utilities");
|
|
|
|
MODULE_VERSION(HV_DRV_VERSION);
|
|
|
|
MODULE_LICENSE("GPL");
|