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 | |
28 | #include "logging.h" | |
29 | #include "osd.h" | |
30 | #include "vmbus.h" | |
31 | #include "VmbusPacketFormat.h" | |
0e0ab5f5 | 32 | #include "vmbus_channel_interface.h" |
c88c4e4c | 33 | #include "VersionInfo.h" |
4df90be5 | 34 | #include "channel.h" |
72daf320 | 35 | #include "vmbus_private.h" |
447fc67e | 36 | #include "vmbus_api.h" |
c88c4e4c HJ |
37 | #include "utils.h" |
38 | ||
39 | ||
6610944a | 40 | static void shutdown_onchannelcallback(void *context) |
c88c4e4c HJ |
41 | { |
42 | struct vmbus_channel *channel = context; | |
43 | u8 *buf; | |
44 | u32 buflen, recvlen; | |
45 | u64 requestid; | |
46 | u8 execute_shutdown = false; | |
47 | ||
48 | struct shutdown_msg_data *shutdown_msg; | |
49 | ||
50 | struct icmsg_hdr *icmsghdrp; | |
51 | struct icmsg_negotiate *negop = NULL; | |
52 | ||
53 | DPRINT_ENTER(VMBUS); | |
54 | ||
55 | buflen = PAGE_SIZE; | |
56 | buf = kmalloc(buflen, GFP_ATOMIC); | |
57 | ||
58 | VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid); | |
59 | ||
60 | if (recvlen > 0) { | |
61 | DPRINT_DBG(VMBUS, "shutdown packet: len=%d, requestid=%lld", | |
62 | recvlen, requestid); | |
63 | ||
64 | icmsghdrp = (struct icmsg_hdr *)&buf[ | |
65 | sizeof(struct vmbuspipe_hdr)]; | |
66 | ||
67 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
68 | prep_negotiate_resp(icmsghdrp, negop, buf); | |
69 | } else { | |
70 | shutdown_msg = (struct shutdown_msg_data *)&buf[ | |
71 | sizeof(struct vmbuspipe_hdr) + | |
72 | sizeof(struct icmsg_hdr)]; | |
73 | ||
74 | switch (shutdown_msg->flags) { | |
75 | case 0: | |
76 | case 1: | |
77 | icmsghdrp->status = HV_S_OK; | |
78 | execute_shutdown = true; | |
79 | ||
80 | DPRINT_INFO(VMBUS, "Shutdown request received -" | |
81 | " gracefull shutdown initiated"); | |
82 | break; | |
83 | default: | |
84 | icmsghdrp->status = HV_E_FAIL; | |
85 | execute_shutdown = false; | |
86 | ||
87 | DPRINT_INFO(VMBUS, "Shutdown request received -" | |
88 | " Invalid request"); | |
89 | break; | |
90 | }; | |
91 | } | |
92 | ||
93 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
94 | | ICMSGHDRFLAG_RESPONSE; | |
95 | ||
96 | VmbusChannelSendPacket(channel, buf, | |
97 | recvlen, requestid, | |
98 | VmbusPacketTypeDataInBand, 0); | |
99 | } | |
100 | ||
101 | kfree(buf); | |
102 | ||
103 | DPRINT_EXIT(VMBUS); | |
104 | ||
105 | if (execute_shutdown == true) | |
9e629075 | 106 | orderly_poweroff(false); |
c88c4e4c HJ |
107 | } |
108 | ||
39c4e9c3 HZ |
109 | |
110 | /* | |
111 | * Synchronize time with host after reboot, restore, etc. | |
112 | */ | |
733371df | 113 | static void adj_guesttime(u64 hosttime, u8 flags) |
39c4e9c3 HZ |
114 | { |
115 | s64 host_tns; | |
116 | struct timespec host_ts; | |
117 | static s32 scnt = 50; | |
118 | ||
119 | host_tns = (hosttime - WLTIMEDELTA) * 100; | |
120 | host_ts = ns_to_timespec(host_tns); | |
121 | ||
122 | if ((flags & ICTIMESYNCFLAG_SYNC) != 0) { | |
123 | do_settimeofday(&host_ts); | |
124 | return; | |
125 | } | |
126 | ||
127 | if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && | |
128 | scnt > 0) { | |
129 | scnt--; | |
130 | do_settimeofday(&host_ts); | |
131 | } | |
132 | ||
133 | return; | |
134 | } | |
135 | ||
136 | /* | |
137 | * Time Sync Channel message handler. | |
138 | */ | |
139 | static void timesync_onchannelcallback(void *context) | |
140 | { | |
141 | struct vmbus_channel *channel = context; | |
142 | u8 *buf; | |
143 | u32 buflen, recvlen; | |
144 | u64 requestid; | |
145 | struct icmsg_hdr *icmsghdrp; | |
146 | struct ictimesync_data *timedatap; | |
147 | ||
148 | DPRINT_ENTER(VMBUS); | |
149 | ||
150 | buflen = PAGE_SIZE; | |
151 | buf = kmalloc(buflen, GFP_ATOMIC); | |
152 | ||
153 | VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid); | |
154 | ||
155 | if (recvlen > 0) { | |
156 | DPRINT_DBG(VMBUS, "timesync packet: recvlen=%d, requestid=%lld", | |
157 | recvlen, requestid); | |
158 | ||
159 | icmsghdrp = (struct icmsg_hdr *)&buf[ | |
160 | sizeof(struct vmbuspipe_hdr)]; | |
161 | ||
162 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
163 | prep_negotiate_resp(icmsghdrp, NULL, buf); | |
164 | } else { | |
165 | timedatap = (struct ictimesync_data *)&buf[ | |
166 | sizeof(struct vmbuspipe_hdr) + | |
167 | sizeof(struct icmsg_hdr)]; | |
168 | adj_guesttime(timedatap->parenttime, timedatap->flags); | |
169 | } | |
170 | ||
171 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
172 | | ICMSGHDRFLAG_RESPONSE; | |
173 | ||
174 | VmbusChannelSendPacket(channel, buf, | |
175 | recvlen, requestid, | |
176 | VmbusPacketTypeDataInBand, 0); | |
177 | } | |
178 | ||
179 | kfree(buf); | |
180 | ||
181 | DPRINT_EXIT(VMBUS); | |
182 | } | |
183 | ||
184 | ||
c88c4e4c HJ |
185 | static int __init init_hyperv_utils(void) |
186 | { | |
187 | printk(KERN_INFO "Registering HyperV Utility Driver\n"); | |
188 | ||
189 | hv_cb_utils[HV_SHUTDOWN_MSG].channel->OnChannelCallback = | |
190 | &shutdown_onchannelcallback; | |
191 | hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback; | |
192 | ||
39c4e9c3 HZ |
193 | hv_cb_utils[HV_TIMESYNC_MSG].channel->OnChannelCallback = |
194 | ×ync_onchannelcallback; | |
195 | hv_cb_utils[HV_TIMESYNC_MSG].callback = ×ync_onchannelcallback; | |
196 | ||
c88c4e4c HJ |
197 | return 0; |
198 | } | |
199 | ||
200 | static void exit_hyperv_utils(void) | |
201 | { | |
202 | printk(KERN_INFO "De-Registered HyperV Utility Driver\n"); | |
203 | ||
204 | hv_cb_utils[HV_SHUTDOWN_MSG].channel->OnChannelCallback = | |
205 | &chn_cb_negotiate; | |
206 | hv_cb_utils[HV_SHUTDOWN_MSG].callback = &chn_cb_negotiate; | |
39c4e9c3 HZ |
207 | |
208 | hv_cb_utils[HV_TIMESYNC_MSG].channel->OnChannelCallback = | |
209 | &chn_cb_negotiate; | |
210 | hv_cb_utils[HV_TIMESYNC_MSG].callback = &chn_cb_negotiate; | |
c88c4e4c HJ |
211 | } |
212 | ||
213 | module_init(init_hyperv_utils); | |
214 | module_exit(exit_hyperv_utils); | |
215 | ||
216 | MODULE_DESCRIPTION("Hyper-V Utilities"); | |
217 | MODULE_VERSION(HV_DRV_VERSION); | |
218 | MODULE_LICENSE("GPL"); |