]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - drivers/staging/hv/hv_utils.c
Staging: hv: remove DPRINT_EXIT macro
[lisovros/linux_canprio.git] / drivers / staging / hv / hv_utils.c
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>
26 #include <linux/reboot.h>
27
28 #include "logging.h"
29 #include "osd.h"
30 #include "vmbus.h"
31 #include "vmbus_packet_format.h"
32 #include "vmbus_channel_interface.h"
33 #include "version_info.h"
34 #include "channel.h"
35 #include "vmbus_private.h"
36 #include "vmbus_api.h"
37 #include "utils.h"
38
39
40 static void shutdown_onchannelcallback(void *context)
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         buflen = PAGE_SIZE;
54         buf = kmalloc(buflen, GFP_ATOMIC);
55
56         VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid);
57
58         if (recvlen > 0) {
59                 DPRINT_DBG(VMBUS, "shutdown packet: len=%d, requestid=%lld",
60                            recvlen, requestid);
61
62                 icmsghdrp = (struct icmsg_hdr *)&buf[
63                         sizeof(struct vmbuspipe_hdr)];
64
65                 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
66                         prep_negotiate_resp(icmsghdrp, negop, buf);
67                 } else {
68                         shutdown_msg = (struct shutdown_msg_data *)&buf[
69                                 sizeof(struct vmbuspipe_hdr) +
70                                 sizeof(struct icmsg_hdr)];
71
72                         switch (shutdown_msg->flags) {
73                         case 0:
74                         case 1:
75                                 icmsghdrp->status = HV_S_OK;
76                                 execute_shutdown = true;
77
78                                 DPRINT_INFO(VMBUS, "Shutdown request received -"
79                                             " gracefull shutdown initiated");
80                                 break;
81                         default:
82                                 icmsghdrp->status = HV_E_FAIL;
83                                 execute_shutdown = false;
84
85                                 DPRINT_INFO(VMBUS, "Shutdown request received -"
86                                             " Invalid request");
87                                 break;
88                         };
89                 }
90
91                 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
92                         | ICMSGHDRFLAG_RESPONSE;
93
94                 VmbusChannelSendPacket(channel, buf,
95                                        recvlen, requestid,
96                                        VmbusPacketTypeDataInBand, 0);
97         }
98
99         kfree(buf);
100
101         if (execute_shutdown == true)
102                 orderly_poweroff(false);
103 }
104
105 /*
106  * Set guest time to host UTC time.
107  */
108 static inline void do_adj_guesttime(u64 hosttime)
109 {
110         s64 host_tns;
111         struct timespec host_ts;
112
113         host_tns = (hosttime - WLTIMEDELTA) * 100;
114         host_ts = ns_to_timespec(host_tns);
115
116         do_settimeofday(&host_ts);
117 }
118
119 /*
120  * Synchronize time with host after reboot, restore, etc.
121  *
122  * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
123  * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
124  * message after the timesync channel is opened. Since the hv_utils module is
125  * loaded after hv_vmbus, the first message is usually missed. The other
126  * thing is, systime is automatically set to emulated hardware clock which may
127  * not be UTC time or in the same time zone. So, to override these effects, we
128  * use the first 50 time samples for initial system time setting.
129  */
130 static inline void adj_guesttime(u64 hosttime, u8 flags)
131 {
132         static s32 scnt = 50;
133
134         if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
135                 do_adj_guesttime(hosttime);
136                 return;
137         }
138
139         if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
140                 scnt--;
141                 do_adj_guesttime(hosttime);
142         }
143 }
144
145 /*
146  * Time Sync Channel message handler.
147  */
148 static void timesync_onchannelcallback(void *context)
149 {
150         struct vmbus_channel *channel = context;
151         u8 *buf;
152         u32 buflen, recvlen;
153         u64 requestid;
154         struct icmsg_hdr *icmsghdrp;
155         struct ictimesync_data *timedatap;
156
157         buflen = PAGE_SIZE;
158         buf = kmalloc(buflen, GFP_ATOMIC);
159
160         VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid);
161
162         if (recvlen > 0) {
163                 DPRINT_DBG(VMBUS, "timesync packet: recvlen=%d, requestid=%lld",
164                         recvlen, requestid);
165
166                 icmsghdrp = (struct icmsg_hdr *)&buf[
167                                 sizeof(struct vmbuspipe_hdr)];
168
169                 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
170                         prep_negotiate_resp(icmsghdrp, NULL, buf);
171                 } else {
172                         timedatap = (struct ictimesync_data *)&buf[
173                                 sizeof(struct vmbuspipe_hdr) +
174                                 sizeof(struct icmsg_hdr)];
175                         adj_guesttime(timedatap->parenttime, timedatap->flags);
176                 }
177
178                 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
179                         | ICMSGHDRFLAG_RESPONSE;
180
181                 VmbusChannelSendPacket(channel, buf,
182                                 recvlen, requestid,
183                                 VmbusPacketTypeDataInBand, 0);
184         }
185
186         kfree(buf);
187 }
188
189 /*
190  * Heartbeat functionality.
191  * Every two seconds, Hyper-V send us a heartbeat request message.
192  * we respond to this message, and Hyper-V knows we are alive.
193  */
194 static void heartbeat_onchannelcallback(void *context)
195 {
196         struct vmbus_channel *channel = context;
197         u8 *buf;
198         u32 buflen, recvlen;
199         u64 requestid;
200         struct icmsg_hdr *icmsghdrp;
201         struct heartbeat_msg_data *heartbeat_msg;
202
203         buflen = PAGE_SIZE;
204         buf = kmalloc(buflen, GFP_ATOMIC);
205
206         VmbusChannelRecvPacket(channel, buf, buflen, &recvlen, &requestid);
207
208         if (recvlen > 0) {
209                 DPRINT_DBG(VMBUS, "heartbeat packet: len=%d, requestid=%lld",
210                            recvlen, requestid);
211
212                 icmsghdrp = (struct icmsg_hdr *)&buf[
213                         sizeof(struct vmbuspipe_hdr)];
214
215                 icmsghdrp = (struct icmsg_hdr *)&buf[
216                                 sizeof(struct vmbuspipe_hdr)];
217
218                 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
219                         prep_negotiate_resp(icmsghdrp, NULL, buf);
220                 } else {
221                         heartbeat_msg = (struct heartbeat_msg_data *)&buf[
222                                 sizeof(struct vmbuspipe_hdr) +
223                                 sizeof(struct icmsg_hdr)];
224
225                         DPRINT_DBG(VMBUS, "heartbeat seq = %lld",
226                                    heartbeat_msg->seq_num);
227
228                         heartbeat_msg->seq_num += 1;
229                 }
230
231                 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
232                         | ICMSGHDRFLAG_RESPONSE;
233
234                 VmbusChannelSendPacket(channel, buf,
235                                        recvlen, requestid,
236                                        VmbusPacketTypeDataInBand, 0);
237         }
238
239         kfree(buf);
240 }
241
242 static int __init init_hyperv_utils(void)
243 {
244         printk(KERN_INFO "Registering HyperV Utility Driver\n");
245
246         hv_cb_utils[HV_SHUTDOWN_MSG].channel->OnChannelCallback =
247                 &shutdown_onchannelcallback;
248         hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback;
249
250         hv_cb_utils[HV_TIMESYNC_MSG].channel->OnChannelCallback =
251                 &timesync_onchannelcallback;
252         hv_cb_utils[HV_TIMESYNC_MSG].callback = &timesync_onchannelcallback;
253
254         hv_cb_utils[HV_HEARTBEAT_MSG].channel->OnChannelCallback =
255                 &heartbeat_onchannelcallback;
256         hv_cb_utils[HV_HEARTBEAT_MSG].callback = &heartbeat_onchannelcallback;
257
258         return 0;
259 }
260
261 static void exit_hyperv_utils(void)
262 {
263         printk(KERN_INFO "De-Registered HyperV Utility Driver\n");
264
265         hv_cb_utils[HV_SHUTDOWN_MSG].channel->OnChannelCallback =
266                 &chn_cb_negotiate;
267         hv_cb_utils[HV_SHUTDOWN_MSG].callback = &chn_cb_negotiate;
268
269         hv_cb_utils[HV_TIMESYNC_MSG].channel->OnChannelCallback =
270                 &chn_cb_negotiate;
271         hv_cb_utils[HV_TIMESYNC_MSG].callback = &chn_cb_negotiate;
272
273         hv_cb_utils[HV_HEARTBEAT_MSG].channel->OnChannelCallback =
274                 &chn_cb_negotiate;
275         hv_cb_utils[HV_HEARTBEAT_MSG].callback = &chn_cb_negotiate;
276 }
277
278 module_init(init_hyperv_utils);
279 module_exit(exit_hyperv_utils);
280
281 MODULE_DESCRIPTION("Hyper-V Utilities");
282 MODULE_VERSION(HV_DRV_VERSION);
283 MODULE_LICENSE("GPL");