21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/sysctl.h>
28 #include <linux/reboot.h>
31 static void shutdown_onchannelcallback(
void *
context);
32 static struct hv_util_service util_shutdown = {
33 .util_cb = shutdown_onchannelcallback,
36 static void timesync_onchannelcallback(
void *
context);
37 static struct hv_util_service util_timesynch = {
38 .util_cb = timesync_onchannelcallback,
41 static void heartbeat_onchannelcallback(
void *
context);
42 static struct hv_util_service util_heartbeat = {
43 .util_cb = heartbeat_onchannelcallback,
46 static struct hv_util_service util_kvp = {
52 static void shutdown_onchannelcallback(
void *
context)
57 u8 execute_shutdown =
false;
58 u8 *shut_txf_buf = util_shutdown.recv_buffer;
60 struct shutdown_msg_data *shutdown_msg;
62 struct icmsg_hdr *icmsghdrp;
63 struct icmsg_negotiate *negop =
NULL;
69 icmsghdrp = (
struct icmsg_hdr *)&shut_txf_buf[
70 sizeof(
struct vmbuspipe_hdr)];
72 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
74 shut_txf_buf, MAX_SRV_VER, MAX_SRV_VER);
77 (
struct shutdown_msg_data *)&shut_txf_buf[
78 sizeof(
struct vmbuspipe_hdr) +
79 sizeof(
struct icmsg_hdr)];
81 switch (shutdown_msg->flags) {
85 execute_shutdown =
true;
87 pr_info(
"Shutdown request received -"
88 " graceful shutdown initiated\n");
92 execute_shutdown =
false;
94 pr_info(
"Shutdown request received -"
95 " Invalid request\n");
100 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
101 | ICMSGHDRFLAG_RESPONSE;
105 VM_PKT_DATA_INBAND, 0);
108 if (execute_shutdown ==
true)
115 static inline void do_adj_guesttime(
u64 hosttime)
120 host_tns = (hosttime - WLTIMEDELTA) * 100;
155 static inline void adj_guesttime(
u64 hosttime,
u8 flags)
165 if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
171 if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
182 static void timesync_onchannelcallback(
void *context)
184 struct vmbus_channel *channel =
context;
187 struct icmsg_hdr *icmsghdrp;
188 struct ictimesync_data *timedatap;
189 u8 *time_txf_buf = util_timesynch.recv_buffer;
195 icmsghdrp = (
struct icmsg_hdr *)&time_txf_buf[
196 sizeof(
struct vmbuspipe_hdr)];
198 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
200 MAX_SRV_VER, MAX_SRV_VER);
202 timedatap = (
struct ictimesync_data *)&time_txf_buf[
203 sizeof(
struct vmbuspipe_hdr) +
204 sizeof(
struct icmsg_hdr)];
205 adj_guesttime(timedatap->parenttime, timedatap->flags);
208 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
209 | ICMSGHDRFLAG_RESPONSE;
213 VM_PKT_DATA_INBAND, 0);
222 static void heartbeat_onchannelcallback(
void *context)
224 struct vmbus_channel *channel =
context;
227 struct icmsg_hdr *icmsghdrp;
228 struct heartbeat_msg_data *heartbeat_msg;
229 u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
235 icmsghdrp = (
struct icmsg_hdr *)&hbeat_txf_buf[
236 sizeof(
struct vmbuspipe_hdr)];
238 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
240 hbeat_txf_buf, MAX_SRV_VER, MAX_SRV_VER);
243 (
struct heartbeat_msg_data *)&hbeat_txf_buf[
244 sizeof(
struct vmbuspipe_hdr) +
245 sizeof(
struct icmsg_hdr)];
247 heartbeat_msg->seq_num += 1;
250 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
251 | ICMSGHDRFLAG_RESPONSE;
255 VM_PKT_DATA_INBAND, 0);
259 static int util_probe(
struct hv_device *
dev,
262 struct hv_util_service *srv =
263 (
struct hv_util_service *)dev_id->driver_data;
267 if (!srv->recv_buffer)
269 if (srv->util_init) {
270 ret = srv->util_init(srv);
278 srv->util_cb, dev->channel);
282 hv_set_drvdata(dev, srv);
286 if (srv->util_deinit)
289 kfree(srv->recv_buffer);
293 static int util_remove(
struct hv_device *dev)
295 struct hv_util_service *srv = hv_get_drvdata(dev);
298 if (srv->util_deinit)
300 kfree(srv->recv_buffer);
307 { VMBUS_DEVICE(0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
308 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB)
309 .driver_data = (
unsigned long)&util_shutdown },
311 { VMBUS_DEVICE(0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
312 0
xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf)
313 .driver_data = (
unsigned long)&util_timesynch },
315 { VMBUS_DEVICE(0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
316 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d)
317 .driver_data = (
unsigned long)&util_heartbeat },
319 { VMBUS_DEVICE(0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
320 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6)
321 .driver_data = (
unsigned long)&util_kvp },
328 static struct hv_driver util_drv = {
330 .id_table = id_table,
332 .remove = util_remove,
335 static int __init init_hyperv_utils(
void)
337 pr_info(
"Registering HyperV Utility Driver\n");
339 return vmbus_driver_register(&util_drv);
342 static void exit_hyperv_utils(
void)
344 pr_info(
"De-Registered HyperV Utility Driver\n");