Drivers: hv: balloon: add a fall through comment to hv_memory_notifier()
[linux-block.git] / drivers / hv / hv_util.c
CommitLineData
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 */
af7a5b6e
HJ
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
c88c4e4c
HJ
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/sysctl.h>
9e629075 28#include <linux/reboot.h>
46a97191 29#include <linux/hyperv.h>
305f7549 30#include <asm/mshyperv.h>
c88c4e4c 31
01325476 32#include "hyperv_vmbus.h"
6741335b 33
3a491605
S
34#define SD_MAJOR 3
35#define SD_MINOR 0
36#define SD_VERSION (SD_MAJOR << 16 | SD_MINOR)
6741335b 37
abeda47e
AN
38#define SD_MAJOR_1 1
39#define SD_VERSION_1 (SD_MAJOR_1 << 16 | SD_MINOR)
3a491605 40
8e1d2607 41#define TS_MAJOR 4
3a491605
S
42#define TS_MINOR 0
43#define TS_VERSION (TS_MAJOR << 16 | TS_MINOR)
44
abeda47e
AN
45#define TS_MAJOR_1 1
46#define TS_VERSION_1 (TS_MAJOR_1 << 16 | TS_MINOR)
3a491605 47
8e1d2607
AN
48#define TS_MAJOR_3 3
49#define TS_VERSION_3 (TS_MAJOR_3 << 16 | TS_MINOR)
50
3a491605 51#define HB_MAJOR 3
abeda47e 52#define HB_MINOR 0
3a491605
S
53#define HB_VERSION (HB_MAJOR << 16 | HB_MINOR)
54
abeda47e
AN
55#define HB_MAJOR_1 1
56#define HB_VERSION_1 (HB_MAJOR_1 << 16 | HB_MINOR)
3a491605
S
57
58static int sd_srv_version;
59static int ts_srv_version;
60static int hb_srv_version;
61static int util_fw_version;
6741335b 62
a29b643c
S
63static void shutdown_onchannelcallback(void *context);
64static struct hv_util_service util_shutdown = {
65 .util_cb = shutdown_onchannelcallback,
66};
67
3ba1eb17
V
68static int hv_timesync_init(struct hv_util_service *srv);
69static void hv_timesync_deinit(void);
70
a29b643c
S
71static void timesync_onchannelcallback(void *context);
72static struct hv_util_service util_timesynch = {
73 .util_cb = timesync_onchannelcallback,
3ba1eb17
V
74 .util_init = hv_timesync_init,
75 .util_deinit = hv_timesync_deinit,
a29b643c
S
76};
77
78static void heartbeat_onchannelcallback(void *context);
79static struct hv_util_service util_heartbeat = {
80 .util_cb = heartbeat_onchannelcallback,
81};
82
83static struct hv_util_service util_kvp = {
84 .util_cb = hv_kvp_onchannelcallback,
85 .util_init = hv_kvp_init,
86 .util_deinit = hv_kvp_deinit,
87};
c88c4e4c 88
96dd86fa
S
89static struct hv_util_service util_vss = {
90 .util_cb = hv_vss_onchannelcallback,
91 .util_init = hv_vss_init,
92 .util_deinit = hv_vss_deinit,
93};
94
01325476
S
95static struct hv_util_service util_fcopy = {
96 .util_cb = hv_fcopy_onchannelcallback,
97 .util_init = hv_fcopy_init,
98 .util_deinit = hv_fcopy_deinit,
99};
100
3dd6cb49
S
101static void perform_shutdown(struct work_struct *dummy)
102{
103 orderly_poweroff(true);
104}
105
106/*
107 * Perform the shutdown operation in a thread context.
108 */
109static DECLARE_WORK(shutdown_work, perform_shutdown);
110
6610944a 111static void shutdown_onchannelcallback(void *context)
c88c4e4c
HJ
112{
113 struct vmbus_channel *channel = context;
45241e50 114 u32 recvlen;
c88c4e4c 115 u64 requestid;
31734634 116 bool execute_shutdown = false;
a29b643c 117 u8 *shut_txf_buf = util_shutdown.recv_buffer;
c88c4e4c
HJ
118
119 struct shutdown_msg_data *shutdown_msg;
120
121 struct icmsg_hdr *icmsghdrp;
122 struct icmsg_negotiate *negop = NULL;
123
45241e50
HJ
124 vmbus_recvpacket(channel, shut_txf_buf,
125 PAGE_SIZE, &recvlen, &requestid);
c88c4e4c
HJ
126
127 if (recvlen > 0) {
45241e50 128 icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
c88c4e4c
HJ
129 sizeof(struct vmbuspipe_hdr)];
130
131 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
c836d0ab 132 vmbus_prep_negotiate_resp(icmsghdrp, negop,
3a491605
S
133 shut_txf_buf, util_fw_version,
134 sd_srv_version);
c88c4e4c 135 } else {
45241e50
HJ
136 shutdown_msg =
137 (struct shutdown_msg_data *)&shut_txf_buf[
138 sizeof(struct vmbuspipe_hdr) +
139 sizeof(struct icmsg_hdr)];
c88c4e4c
HJ
140
141 switch (shutdown_msg->flags) {
142 case 0:
143 case 1:
144 icmsghdrp->status = HV_S_OK;
145 execute_shutdown = true;
146
af7a5b6e 147 pr_info("Shutdown request received -"
32235b07 148 " graceful shutdown initiated\n");
c88c4e4c
HJ
149 break;
150 default:
151 icmsghdrp->status = HV_E_FAIL;
152 execute_shutdown = false;
153
af7a5b6e
HJ
154 pr_info("Shutdown request received -"
155 " Invalid request\n");
c88c4e4c 156 break;
95cd17c9 157 }
c88c4e4c
HJ
158 }
159
160 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
161 | ICMSGHDRFLAG_RESPONSE;
162
45241e50 163 vmbus_sendpacket(channel, shut_txf_buf,
c88c4e4c 164 recvlen, requestid,
415f2287 165 VM_PKT_DATA_INBAND, 0);
c88c4e4c
HJ
166 }
167
c88c4e4c 168 if (execute_shutdown == true)
3dd6cb49 169 schedule_work(&shutdown_work);
c88c4e4c
HJ
170}
171
95ff7cde
S
172/*
173 * Set the host time in a process context.
174 */
175
176struct adj_time_work {
177 struct work_struct work;
178 u64 host_time;
8e1d2607
AN
179 u64 ref_time;
180 u8 flags;
95ff7cde
S
181};
182
183static void hv_set_host_time(struct work_struct *work)
184{
185 struct adj_time_work *wrk;
8e1d2607
AN
186 s64 host_tns;
187 u64 newtime;
17244623 188 struct timespec64 host_ts;
95ff7cde
S
189
190 wrk = container_of(work, struct adj_time_work, work);
8e1d2607
AN
191
192 newtime = wrk->host_time;
193 if (ts_srv_version > TS_VERSION_3) {
194 /*
195 * Some latency has been introduced since Hyper-V generated
196 * its time sample. Take that latency into account before
197 * using TSC reference time sample from Hyper-V.
198 *
199 * This sample is given by TimeSync v4 and above hosts.
200 */
201 u64 current_tick;
202
305f7549 203 hv_get_current_tick(current_tick);
8e1d2607
AN
204 newtime += (current_tick - wrk->ref_time);
205 }
206 host_tns = (newtime - WLTIMEDELTA) * 100;
17244623 207 host_ts = ns_to_timespec64(host_tns);
8e1d2607 208
17244623 209 do_settimeofday64(&host_ts);
95ff7cde
S
210}
211
e9ec3603
HZ
212/*
213 * Synchronize time with host after reboot, restore, etc.
214 *
215 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
216 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
217 * message after the timesync channel is opened. Since the hv_utils module is
2e338f7e
AN
218 * loaded after hv_vmbus, the first message is usually missed. This bit is
219 * considered a hard request to discipline the clock.
220 *
221 * ICTIMESYNCFLAG_SAMPLE bit indicates a time sample from host. This is
222 * typically used as a hint to the guest. The guest is under no obligation
223 * to discipline the clock.
e9ec3603 224 */
3ba1eb17 225static struct adj_time_work wrk;
8e1d2607 226static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 flags)
e9ec3603 227{
e9ec3603 228
3ba1eb17
V
229 /*
230 * This check is safe since we are executing in the
231 * interrupt context and time synch messages arre always
232 * delivered on the same CPU.
233 */
234 if (work_pending(&wrk.work))
95ff7cde
S
235 return;
236
3ba1eb17
V
237 wrk.host_time = hosttime;
238 wrk.ref_time = reftime;
239 wrk.flags = flags;
2e338f7e 240 if ((flags & (ICTIMESYNCFLAG_SYNC | ICTIMESYNCFLAG_SAMPLE)) != 0) {
3ba1eb17
V
241 schedule_work(&wrk.work);
242 }
39c4e9c3
HZ
243}
244
245/*
246 * Time Sync Channel message handler.
247 */
248static void timesync_onchannelcallback(void *context)
249{
250 struct vmbus_channel *channel = context;
45241e50 251 u32 recvlen;
39c4e9c3
HZ
252 u64 requestid;
253 struct icmsg_hdr *icmsghdrp;
254 struct ictimesync_data *timedatap;
8e1d2607 255 struct ictimesync_ref_data *refdata;
a29b643c 256 u8 *time_txf_buf = util_timesynch.recv_buffer;
3a491605 257 struct icmsg_negotiate *negop = NULL;
39c4e9c3 258
45241e50
HJ
259 vmbus_recvpacket(channel, time_txf_buf,
260 PAGE_SIZE, &recvlen, &requestid);
39c4e9c3
HZ
261
262 if (recvlen > 0) {
45241e50 263 icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
39c4e9c3
HZ
264 sizeof(struct vmbuspipe_hdr)];
265
266 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
3a491605
S
267 vmbus_prep_negotiate_resp(icmsghdrp, negop,
268 time_txf_buf,
269 util_fw_version,
270 ts_srv_version);
8e1d2607
AN
271 pr_info("Using TimeSync version %d.%d\n",
272 ts_srv_version >> 16, ts_srv_version & 0xFFFF);
39c4e9c3 273 } else {
8e1d2607
AN
274 if (ts_srv_version > TS_VERSION_3) {
275 refdata = (struct ictimesync_ref_data *)
276 &time_txf_buf[
277 sizeof(struct vmbuspipe_hdr) +
278 sizeof(struct icmsg_hdr)];
279
280 adj_guesttime(refdata->parenttime,
281 refdata->vmreferencetime,
282 refdata->flags);
283 } else {
284 timedatap = (struct ictimesync_data *)
285 &time_txf_buf[
286 sizeof(struct vmbuspipe_hdr) +
287 sizeof(struct icmsg_hdr)];
288 adj_guesttime(timedatap->parenttime,
289 0,
290 timedatap->flags);
291 }
39c4e9c3
HZ
292 }
293
294 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
295 | ICMSGHDRFLAG_RESPONSE;
296
45241e50 297 vmbus_sendpacket(channel, time_txf_buf,
39c4e9c3 298 recvlen, requestid,
415f2287 299 VM_PKT_DATA_INBAND, 0);
39c4e9c3 300 }
39c4e9c3
HZ
301}
302
9153f7b9
HJ
303/*
304 * Heartbeat functionality.
305 * Every two seconds, Hyper-V send us a heartbeat request message.
306 * we respond to this message, and Hyper-V knows we are alive.
307 */
308static void heartbeat_onchannelcallback(void *context)
309{
310 struct vmbus_channel *channel = context;
45241e50 311 u32 recvlen;
9153f7b9
HJ
312 u64 requestid;
313 struct icmsg_hdr *icmsghdrp;
314 struct heartbeat_msg_data *heartbeat_msg;
a29b643c 315 u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
3a491605 316 struct icmsg_negotiate *negop = NULL;
9153f7b9 317
407a3aee
LL
318 while (1) {
319
320 vmbus_recvpacket(channel, hbeat_txf_buf,
321 PAGE_SIZE, &recvlen, &requestid);
322
323 if (!recvlen)
324 break;
9153f7b9 325
45241e50 326 icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
9153f7b9
HJ
327 sizeof(struct vmbuspipe_hdr)];
328
329 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
3a491605
S
330 vmbus_prep_negotiate_resp(icmsghdrp, negop,
331 hbeat_txf_buf, util_fw_version,
332 hb_srv_version);
9153f7b9 333 } else {
45241e50
HJ
334 heartbeat_msg =
335 (struct heartbeat_msg_data *)&hbeat_txf_buf[
336 sizeof(struct vmbuspipe_hdr) +
337 sizeof(struct icmsg_hdr)];
9153f7b9 338
9153f7b9
HJ
339 heartbeat_msg->seq_num += 1;
340 }
341
342 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
343 | ICMSGHDRFLAG_RESPONSE;
344
45241e50 345 vmbus_sendpacket(channel, hbeat_txf_buf,
9153f7b9 346 recvlen, requestid,
415f2287 347 VM_PKT_DATA_INBAND, 0);
9153f7b9 348 }
9153f7b9 349}
39c4e9c3 350
84946899
S
351static int util_probe(struct hv_device *dev,
352 const struct hv_vmbus_device_id *dev_id)
283f2129 353{
a29b643c
S
354 struct hv_util_service *srv =
355 (struct hv_util_service *)dev_id->driver_data;
356 int ret;
357
9bd2d0df 358 srv->recv_buffer = kmalloc(PAGE_SIZE * 4, GFP_KERNEL);
a29b643c
S
359 if (!srv->recv_buffer)
360 return -ENOMEM;
b9830d12 361 srv->channel = dev->channel;
a29b643c
S
362 if (srv->util_init) {
363 ret = srv->util_init(srv);
364 if (ret) {
4e65f6e8
S
365 ret = -ENODEV;
366 goto error1;
a29b643c
S
367 }
368 }
369
7ae3e035
S
370 /*
371 * The set of services managed by the util driver are not performance
372 * critical and do not need batched reading. Furthermore, some services
373 * such as KVP can only handle one message from the host at a time.
374 * Turn off batched reading for all util drivers before we open the
375 * channel.
376 */
377
378 set_channel_read_state(dev->channel, false);
379
a29b643c 380 hv_set_drvdata(dev, srv);
18965663 381
3a491605
S
382 /*
383 * Based on the host; initialize the framework and
384 * service version numbers we will negotiate.
385 */
386 switch (vmbus_proto_version) {
387 case (VERSION_WS2008):
388 util_fw_version = UTIL_WS2K8_FW_VERSION;
abeda47e
AN
389 sd_srv_version = SD_VERSION_1;
390 ts_srv_version = TS_VERSION_1;
391 hb_srv_version = HB_VERSION_1;
3a491605 392 break;
3da0401b
AN
393 case VERSION_WIN7:
394 case VERSION_WIN8:
395 case VERSION_WIN8_1:
3a491605
S
396 util_fw_version = UTIL_FW_VERSION;
397 sd_srv_version = SD_VERSION;
3da0401b 398 ts_srv_version = TS_VERSION_3;
3a491605 399 hb_srv_version = HB_VERSION;
8e1d2607 400 break;
3da0401b 401 case VERSION_WIN10:
8e1d2607
AN
402 default:
403 util_fw_version = UTIL_FW_VERSION;
404 sd_srv_version = SD_VERSION;
3da0401b 405 ts_srv_version = TS_VERSION;
8e1d2607 406 hb_srv_version = HB_VERSION;
3a491605
S
407 }
408
18965663
DC
409 ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
410 srv->util_cb, dev->channel);
411 if (ret)
412 goto error;
413
283f2129 414 return 0;
4e65f6e8
S
415
416error:
417 if (srv->util_deinit)
418 srv->util_deinit();
419error1:
420 kfree(srv->recv_buffer);
421 return ret;
283f2129
S
422}
423
424static int util_remove(struct hv_device *dev)
425{
a29b643c
S
426 struct hv_util_service *srv = hv_get_drvdata(dev);
427
428 if (srv->util_deinit)
429 srv->util_deinit();
5380b383 430 vmbus_close(dev->channel);
a29b643c
S
431 kfree(srv->recv_buffer);
432
283f2129
S
433 return 0;
434}
435
436static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4 437 /* Shutdown guid */
d13984e5
S
438 { HV_SHUTDOWN_GUID,
439 .driver_data = (unsigned long)&util_shutdown
440 },
c45cf2d4 441 /* Time synch guid */
d13984e5
S
442 { HV_TS_GUID,
443 .driver_data = (unsigned long)&util_timesynch
444 },
c45cf2d4 445 /* Heartbeat guid */
d13984e5
S
446 { HV_HEART_BEAT_GUID,
447 .driver_data = (unsigned long)&util_heartbeat
448 },
c45cf2d4 449 /* KVP guid */
d13984e5
S
450 { HV_KVP_GUID,
451 .driver_data = (unsigned long)&util_kvp
452 },
96dd86fa
S
453 /* VSS GUID */
454 { HV_VSS_GUID,
455 .driver_data = (unsigned long)&util_vss
456 },
01325476
S
457 /* File copy GUID */
458 { HV_FCOPY_GUID,
459 .driver_data = (unsigned long)&util_fcopy
460 },
c45cf2d4 461 { },
283f2129
S
462};
463
464MODULE_DEVICE_TABLE(vmbus, id_table);
465
466/* The one and only one */
467static struct hv_driver util_drv = {
768fa219 468 .name = "hv_util",
283f2129
S
469 .id_table = id_table,
470 .probe = util_probe,
471 .remove = util_remove,
472};
473
3ba1eb17
V
474static int hv_timesync_init(struct hv_util_service *srv)
475{
476 INIT_WORK(&wrk.work, hv_set_host_time);
477 return 0;
478}
479
480static void hv_timesync_deinit(void)
481{
482 cancel_work_sync(&wrk.work);
483}
484
c88c4e4c
HJ
485static int __init init_hyperv_utils(void)
486{
af7a5b6e 487 pr_info("Registering HyperV Utility Driver\n");
c88c4e4c 488
4e65f6e8 489 return vmbus_driver_register(&util_drv);
c88c4e4c
HJ
490}
491
492static void exit_hyperv_utils(void)
493{
af7a5b6e 494 pr_info("De-Registered HyperV Utility Driver\n");
c88c4e4c 495
768fa219 496 vmbus_driver_unregister(&util_drv);
c88c4e4c
HJ
497}
498
499module_init(init_hyperv_utils);
500module_exit(exit_hyperv_utils);
501
502MODULE_DESCRIPTION("Hyper-V Utilities");
c88c4e4c 503MODULE_LICENSE("GPL");