cregit-Linux how code gets into the kernel

Release 4.7 drivers/hv/hv_util.c

Directory: drivers/hv
/*
 * 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>
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sysctl.h>
#include <linux/reboot.h>
#include <linux/hyperv.h>

#include "hyperv_vmbus.h"


#define SD_MAJOR	3

#define SD_MINOR	0

#define SD_VERSION	(SD_MAJOR << 16 | SD_MINOR)


#define SD_WS2008_MAJOR		1

#define SD_WS2008_VERSION	(SD_WS2008_MAJOR << 16 | SD_MINOR)


#define TS_MAJOR	3

#define TS_MINOR	0

#define TS_VERSION	(TS_MAJOR << 16 | TS_MINOR)


#define TS_WS2008_MAJOR		1

#define TS_WS2008_VERSION	(TS_WS2008_MAJOR << 16 | TS_MINOR)


#define HB_MAJOR	3

#define HB_MINOR 0

#define HB_VERSION	(HB_MAJOR << 16 | HB_MINOR)


#define HB_WS2008_MAJOR	1

#define HB_WS2008_VERSION	(HB_WS2008_MAJOR << 16 | HB_MINOR)


static int sd_srv_version;

static int ts_srv_version;

static int hb_srv_version;

static int util_fw_version;

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,
};


static struct hv_util_service util_vss = {
	.util_cb = hv_vss_onchannelcallback,
	.util_init = hv_vss_init,
	.util_deinit = hv_vss_deinit,
};


static struct hv_util_service util_fcopy = {
	.util_cb = hv_fcopy_onchannelcallback,
	.util_init = hv_fcopy_init,
	.util_deinit = hv_fcopy_deinit,
};


static void perform_shutdown(struct work_struct *dummy) { orderly_poweroff(true); }

Contributors

PersonTokensPropCommitsCommitProp
ky srinivasanky srinivasan16100.00%1100.00%
Total16100.00%1100.00%

/* * Perform the shutdown operation in a thread context. */ static DECLARE_WORK(shutdown_work, perform_shutdown);
static void shutdown_onchannelcallback(void *context) { struct vmbus_channel *channel = context; u32 recvlen; u64 requestid; bool execute_shutdown = false; u8 *shut_txf_buf = util_shutdown.recv_buffer; struct shutdown_msg_data *shutdown_msg; struct icmsg_hdr *icmsghdrp; struct icmsg_negotiate *negop = NULL; vmbus_recvpacket(channel, shut_txf_buf, PAGE_SIZE, &recvlen, &requestid); if (recvlen > 0) { icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[ sizeof(struct vmbuspipe_hdr)]; if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { vmbus_prep_negotiate_resp(icmsghdrp, negop, shut_txf_buf, util_fw_version, sd_srv_version); } else { shutdown_msg = (struct shutdown_msg_data *)&shut_txf_buf[ sizeof(struct vmbuspipe_hdr) + sizeof(struct icmsg_hdr)]; switch (shutdown_msg->flags) { case 0: case 1: icmsghdrp->status = HV_S_OK; execute_shutdown = true; pr_info("Shutdown request received -" " graceful shutdown initiated\n"); break; default: icmsghdrp->status = HV_E_FAIL; execute_shutdown = false; pr_info("Shutdown request received -" " Invalid request\n"); break; } } icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; vmbus_sendpacket(channel, shut_txf_buf, recvlen, requestid, VM_PKT_DATA_INBAND, 0); } if (execute_shutdown == true) schedule_work(&shutdown_work); }

Contributors

PersonTokensPropCommitsCommitProp
hank janssenhank janssen20289.38%321.43%
ky srinivasanky srinivasan156.64%428.57%
greg kroah-hartmangreg kroah-hartman52.21%428.57%
haiyang zhanghaiyang zhang31.33%214.29%
peter senna tschudinpeter senna tschudin10.44%17.14%
Total226100.00%14100.00%

/* * Set guest time to host UTC time. */
static inline void do_adj_guesttime(u64 hosttime) { s64 host_tns; struct timespec host_ts; host_tns = (hosttime - WLTIMEDELTA) * 100; host_ts = ns_to_timespec(host_tns); do_settimeofday(&host_ts); }

Contributors

PersonTokensPropCommitsCommitProp
haiyang zhanghaiyang zhang3997.50%266.67%
greg kroah-hartmangreg kroah-hartman12.50%133.33%
Total40100.00%3100.00%

/* * 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); }

Contributors

PersonTokensPropCommitsCommitProp
ky srinivasanky srinivasan40100.00%1100.00%
Total40100.00%1100.00%

/* * 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) { struct adj_time_work *wrk; static s32 scnt = 50; wrk = kmalloc(sizeof(struct adj_time_work), GFP_ATOMIC); if (wrk == NULL) return; wrk->host_time = hosttime; if ((flags & ICTIMESYNCFLAG_SYNC) != 0) { INIT_WORK(&wrk->work, hv_set_host_time); schedule_work(&wrk->work); return; } if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) { scnt--; INIT_WORK(&wrk->work, hv_set_host_time); schedule_work(&wrk->work); } else kfree(wrk); }

Contributors

PersonTokensPropCommitsCommitProp
ky srinivasanky srinivasan6754.03%133.33%
haiyang zhanghaiyang zhang5745.97%266.67%
Total124100.00%3100.00%

/* * Time Sync Channel message handler. */
static void timesync_onchannelcallback(void *context) { struct vmbus_channel *channel = context; u32 recvlen; u64 requestid; struct icmsg_hdr *icmsghdrp; struct ictimesync_data *timedatap; u8 *time_txf_buf = util_timesynch.recv_buffer; struct icmsg_negotiate *negop = NULL; vmbus_recvpacket(channel, time_txf_buf, PAGE_SIZE, &recvlen, &requestid); if (recvlen > 0) { icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[ sizeof(struct vmbuspipe_hdr)]; if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { vmbus_prep_negotiate_resp(icmsghdrp, negop, time_txf_buf, util_fw_version, ts_srv_version); } else { timedatap = (struct ictimesync_data *)&time_txf_buf[ sizeof(struct vmbuspipe_hdr) + sizeof(struct icmsg_hdr)]; adj_guesttime(timedatap->parenttime, timedatap->flags); } icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; vmbus_sendpacket(channel, time_txf_buf, recvlen, requestid, VM_PKT_DATA_INBAND, 0); } }

Contributors

PersonTokensPropCommitsCommitProp
haiyang zhanghaiyang zhang14484.21%337.50%
ky srinivasanky srinivasan2011.70%337.50%
hank janssenhank janssen63.51%112.50%
greg kroah-hartmangreg kroah-hartman10.58%112.50%
Total171100.00%8100.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; u32 recvlen; u64 requestid; struct icmsg_hdr *icmsghdrp; struct heartbeat_msg_data *heartbeat_msg; u8 *hbeat_txf_buf = util_heartbeat.recv_buffer; struct icmsg_negotiate *negop = NULL; vmbus_recvpacket(channel, hbeat_txf_buf, PAGE_SIZE, &recvlen, &requestid); if (recvlen > 0) { icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[ sizeof(struct vmbuspipe_hdr)]; if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { vmbus_prep_negotiate_resp(icmsghdrp, negop, hbeat_txf_buf, util_fw_version, hb_srv_version); } else { heartbeat_msg = (struct heartbeat_msg_data *)&hbeat_txf_buf[ sizeof(struct vmbuspipe_hdr) + sizeof(struct icmsg_hdr)]; heartbeat_msg->seq_num += 1; } icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; vmbus_sendpacket(channel, hbeat_txf_buf, recvlen, requestid, VM_PKT_DATA_INBAND, 0); } }

Contributors

PersonTokensPropCommitsCommitProp
hank janssenhank janssen14285.54%225.00%
ky srinivasanky srinivasan2012.05%337.50%
haiyang zhanghaiyang zhang31.81%225.00%
greg kroah-hartmangreg kroah-hartman10.60%112.50%
Total166100.00%8100.00%


static int util_probe(struct hv_device *dev, const struct hv_vmbus_device_id *dev_id) { struct hv_util_service *srv = (struct hv_util_service *)dev_id->driver_data; int ret; srv->recv_buffer = kmalloc(PAGE_SIZE * 4, GFP_KERNEL); if (!srv->recv_buffer) return -ENOMEM; srv->channel = dev->channel; if (srv->util_init) { ret = srv->util_init(srv); if (ret) { ret = -ENODEV; goto error1; } } /* * 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); hv_set_drvdata(dev, srv); /* * 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; sd_srv_version = SD_WS2008_VERSION; ts_srv_version = TS_WS2008_VERSION; hb_srv_version = HB_WS2008_VERSION; break; default: util_fw_version = UTIL_FW_VERSION; sd_srv_version = SD_VERSION; ts_srv_version = TS_VERSION; hb_srv_version = HB_VERSION; } ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0, srv->util_cb, dev->channel); if (ret) goto error; return 0; error: if (srv->util_deinit) srv->util_deinit(); error1: kfree(srv->recv_buffer); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
ky srinivasanky srinivasan18883.93%990.00%
dexuan cuidexuan cui3616.07%110.00%
Total224100.00%10100.00%


static int util_remove(struct hv_device *dev) { struct hv_util_service *srv = hv_get_drvdata(dev); if (srv->util_deinit) srv->util_deinit(); vmbus_close(dev->channel); kfree(srv->recv_buffer); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
ky srinivasanky srinivasan49100.00%3100.00%
Total49100.00%3100.00%

static const struct hv_vmbus_device_id id_table[] = { /* Shutdown guid */ { HV_SHUTDOWN_GUID, .driver_data = (unsigned long)&util_shutdown }, /* Time synch guid */ { HV_TS_GUID, .driver_data = (unsigned long)&util_timesynch }, /* Heartbeat guid */ { HV_HEART_BEAT_GUID, .driver_data = (unsigned long)&util_heartbeat }, /* KVP guid */ { HV_KVP_GUID, .driver_data = (unsigned long)&util_kvp }, /* VSS GUID */ { HV_VSS_GUID, .driver_data = (unsigned long)&util_vss }, /* File copy GUID */ { HV_FCOPY_GUID, .driver_data = (unsigned long)&util_fcopy }, { }, }; MODULE_DEVICE_TABLE(vmbus, id_table); /* The one and only one */ static struct hv_driver util_drv = { .name = "hv_util", .id_table = id_table, .probe = util_probe, .remove = util_remove, };
static int __init init_hyperv_utils(void) { pr_info("Registering HyperV Utility Driver\n"); return vmbus_driver_register(&util_drv); }

Contributors

PersonTokensPropCommitsCommitProp
hank janssenhank janssen1676.19%360.00%
ky srinivasanky srinivasan523.81%240.00%
Total21100.00%5100.00%


static void exit_hyperv_utils(void) { pr_info("De-Registered HyperV Utility Driver\n"); vmbus_driver_unregister(&util_drv); }

Contributors

PersonTokensPropCommitsCommitProp
hank janssenhank janssen1368.42%250.00%
ky srinivasanky srinivasan526.32%125.00%
greg kroah-hartmangreg kroah-hartman15.26%125.00%
Total19100.00%4100.00%

module_init(init_hyperv_utils); module_exit(exit_hyperv_utils); MODULE_DESCRIPTION("Hyper-V Utilities"); MODULE_LICENSE("GPL");

Overall Contributors

PersonTokensPropCommitsCommitProp
ky srinivasanky srinivasan77851.35%1851.43%
hank janssenhank janssen43328.58%411.43%
haiyang zhanghaiyang zhang24916.44%411.43%
dexuan cuidexuan cui362.38%12.86%
greg kroah-hartmangreg kroah-hartman181.19%720.00%
peter senna tschudinpeter senna tschudin10.07%12.86%
Total1515100.00%35100.00%
Directory: drivers/hv
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}