]>
Commit | Line | Data |
---|---|---|
c88c4e4c HJ |
1 | /* |
2 | * Copyright (c) 2010, Microsoft Corporation. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
15 | * Place - Suite 330, Boston, MA 02111-1307 USA. | |
16 | * | |
17 | * Authors: | |
18 | * Haiyang Zhang <haiyangz@microsoft.com> | |
19 | * Hank Janssen <hjanssen@microsoft.com> | |
20 | */ | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/sysctl.h> | |
9e629075 | 26 | #include <linux/reboot.h> |
c88c4e4c HJ |
27 | #include <linux/version.h> |
28 | ||
29 | #include "logging.h" | |
30 | #include "osd.h" | |
31 | #include "vmbus.h" | |
32 | #include "VmbusPacketFormat.h" | |
33 | #include "VmbusChannelInterface.h" | |
34 | #include "VersionInfo.h" | |
35 | #include "Channel.h" | |
36 | #include "VmbusPrivate.h" | |
37 | #include "VmbusApi.h" | |
38 | #include "utils.h" | |
39 | ||
40 | ||
41 | void shutdown_onchannelcallback(void *context) | |
42 | { | |
43 | struct vmbus_channel *channel = context; | |
44 | u8 *buf; | |
45 | u32 buflen, recvlen; | |
46 | u64 requestid; | |
47 | u8 execute_shutdown = false; | |
48 | ||
49 | struct shutdown_msg_data *shutdown_msg; | |
50 | ||
51 | struct icmsg_hdr *icmsghdrp; | |
52 | struct icmsg_negotiate *negop = NULL; | |
53 | ||
54 | DPRINT_ENTER(VMBUS); | |
55 | ||
56 | buflen = PAGE_SIZE; | |
57 | buf = kmalloc(buflen, GFP_ATOMIC); | |
58 | ||
59 | VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid); | |
60 | ||
61 | if (recvlen > 0) { | |
62 | DPRINT_DBG(VMBUS, "shutdown packet: len=%d, requestid=%lld", | |
63 | recvlen, requestid); | |
64 | ||
65 | icmsghdrp = (struct icmsg_hdr *)&buf[ | |
66 | sizeof(struct vmbuspipe_hdr)]; | |
67 | ||
68 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
69 | prep_negotiate_resp(icmsghdrp, negop, buf); | |
70 | } else { | |
71 | shutdown_msg = (struct shutdown_msg_data *)&buf[ | |
72 | sizeof(struct vmbuspipe_hdr) + | |
73 | sizeof(struct icmsg_hdr)]; | |
74 | ||
75 | switch (shutdown_msg->flags) { | |
76 | case 0: | |
77 | case 1: | |
78 | icmsghdrp->status = HV_S_OK; | |
79 | execute_shutdown = true; | |
80 | ||
81 | DPRINT_INFO(VMBUS, "Shutdown request received -" | |
82 | " gracefull shutdown initiated"); | |
83 | break; | |
84 | default: | |
85 | icmsghdrp->status = HV_E_FAIL; | |
86 | execute_shutdown = false; | |
87 | ||
88 | DPRINT_INFO(VMBUS, "Shutdown request received -" | |
89 | " Invalid request"); | |
90 | break; | |
91 | }; | |
92 | } | |
93 | ||
94 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
95 | | ICMSGHDRFLAG_RESPONSE; | |
96 | ||
97 | VmbusChannelSendPacket(channel, buf, | |
98 | recvlen, requestid, | |
99 | VmbusPacketTypeDataInBand, 0); | |
100 | } | |
101 | ||
102 | kfree(buf); | |
103 | ||
104 | DPRINT_EXIT(VMBUS); | |
105 | ||
106 | if (execute_shutdown == true) | |
9e629075 | 107 | orderly_poweroff(false); |
c88c4e4c HJ |
108 | } |
109 | ||
110 | static int __init init_hyperv_utils(void) | |
111 | { | |
112 | printk(KERN_INFO "Registering HyperV Utility Driver\n"); | |
113 | ||
114 | hv_cb_utils[HV_SHUTDOWN_MSG].channel->OnChannelCallback = | |
115 | &shutdown_onchannelcallback; | |
116 | hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback; | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
121 | static void exit_hyperv_utils(void) | |
122 | { | |
123 | printk(KERN_INFO "De-Registered HyperV Utility Driver\n"); | |
124 | ||
125 | hv_cb_utils[HV_SHUTDOWN_MSG].channel->OnChannelCallback = | |
126 | &chn_cb_negotiate; | |
127 | hv_cb_utils[HV_SHUTDOWN_MSG].callback = &chn_cb_negotiate; | |
128 | } | |
129 | ||
130 | module_init(init_hyperv_utils); | |
131 | module_exit(exit_hyperv_utils); | |
132 | ||
133 | MODULE_DESCRIPTION("Hyper-V Utilities"); | |
134 | MODULE_VERSION(HV_DRV_VERSION); | |
135 | MODULE_LICENSE("GPL"); |