1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2010, Microsoft Corporation.
4 *
5 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/sysctl.h>
16 #include <linux/reboot.h>
17 #include <linux/hyperv.h>
18 #include <linux/clockchips.h>
19 #include <linux/ptp_clock_kernel.h>
20 #include <asm/mshyperv.h>
21
22 #include "hyperv_vmbus.h"
23
24 #define SD_MAJOR 3
25 #define SD_MINOR 0
26 #define SD_MINOR_1 1
27 #define SD_MINOR_2 2
28 #define SD_VERSION_3_1 (SD_MAJOR << 16 | SD_MINOR_1)
29 #define SD_VERSION_3_2 (SD_MAJOR << 16 | SD_MINOR_2)
30 #define SD_VERSION (SD_MAJOR << 16 | SD_MINOR)
31
32 #define SD_MAJOR_1 1
33 #define SD_VERSION_1 (SD_MAJOR_1 << 16 | SD_MINOR)
34
35 #define TS_MAJOR 4
36 #define TS_MINOR 0
37 #define TS_VERSION (TS_MAJOR << 16 | TS_MINOR)
38
39 #define TS_MAJOR_1 1
40 #define TS_VERSION_1 (TS_MAJOR_1 << 16 | TS_MINOR)
41
42 #define TS_MAJOR_3 3
43 #define TS_VERSION_3 (TS_MAJOR_3 << 16 | TS_MINOR)
44
45 #define HB_MAJOR 3
46 #define HB_MINOR 0
47 #define HB_VERSION (HB_MAJOR << 16 | HB_MINOR)
48
49 #define HB_MAJOR_1 1
50 #define HB_VERSION_1 (HB_MAJOR_1 << 16 | HB_MINOR)
51
52 static int sd_srv_version;
53 static int ts_srv_version;
54 static int hb_srv_version;
55
56 #define SD_VER_COUNT 4
57 static const int sd_versions[] = {
58 SD_VERSION_3_2,
59 SD_VERSION_3_1,
60 SD_VERSION,
61 SD_VERSION_1
62 };
63
64 #define TS_VER_COUNT 3
65 static const int ts_versions[] = {
66 TS_VERSION,
67 TS_VERSION_3,
68 TS_VERSION_1
69 };
70
71 #define HB_VER_COUNT 2
72 static const int hb_versions[] = {
73 HB_VERSION,
74 HB_VERSION_1
75 };
76
77 #define FW_VER_COUNT 2
78 static const int fw_versions[] = {
79 UTIL_FW_VERSION,
80 UTIL_WS2K8_FW_VERSION
81 };
82
83 /*
84 * Send the "hibernate" udev event in a thread context.
85 */
86 struct hibernate_work_context {
87 struct work_struct work;
88 struct hv_device *dev;
89 };
90
91 static struct hibernate_work_context hibernate_context;
92 static bool hibernation_supported;
93
send_hibernate_uevent(struct work_struct * work)94 static void send_hibernate_uevent(struct work_struct *work)
95 {
96 char *uevent_env[2] = { "EVENT=hibernate", NULL };
97 struct hibernate_work_context *ctx;
98
99 ctx = container_of(work, struct hibernate_work_context, work);
100
101 kobject_uevent_env(&ctx->dev->device.kobj, KOBJ_CHANGE, uevent_env);
102
103 pr_info("Sent hibernation uevent\n");
104 }
105
hv_shutdown_init(struct hv_util_service * srv)106 static int hv_shutdown_init(struct hv_util_service *srv)
107 {
108 struct vmbus_channel *channel = srv->channel;
109
110 INIT_WORK(&hibernate_context.work, send_hibernate_uevent);
111 hibernate_context.dev = channel->device_obj;
112
113 hibernation_supported = hv_is_hibernation_supported();
114
115 return 0;
116 }
117
118 static void shutdown_onchannelcallback(void *context);
119 static struct hv_util_service util_shutdown = {
120 .util_cb = shutdown_onchannelcallback,
121 .util_init = hv_shutdown_init,
122 };
123
124 static int hv_timesync_init(struct hv_util_service *srv);
125 static int hv_timesync_pre_suspend(void);
126 static void hv_timesync_deinit(void);
127
128 static void timesync_onchannelcallback(void *context);
129 static struct hv_util_service util_timesynch = {
130 .util_cb = timesync_onchannelcallback,
131 .util_init = hv_timesync_init,
132 .util_pre_suspend = hv_timesync_pre_suspend,
133 .util_deinit = hv_timesync_deinit,
134 };
135
136 static void heartbeat_onchannelcallback(void *context);
137 static struct hv_util_service util_heartbeat = {
138 .util_cb = heartbeat_onchannelcallback,
139 };
140
141 static struct hv_util_service util_kvp = {
142 .util_cb = hv_kvp_onchannelcallback,
143 .util_init = hv_kvp_init,
144 .util_init_transport = hv_kvp_init_transport,
145 .util_pre_suspend = hv_kvp_pre_suspend,
146 .util_pre_resume = hv_kvp_pre_resume,
147 .util_deinit = hv_kvp_deinit,
148 };
149
150 static struct hv_util_service util_vss = {
151 .util_cb = hv_vss_onchannelcallback,
152 .util_init = hv_vss_init,
153 .util_init_transport = hv_vss_init_transport,
154 .util_pre_suspend = hv_vss_pre_suspend,
155 .util_pre_resume = hv_vss_pre_resume,
156 .util_deinit = hv_vss_deinit,
157 };
158
159 static struct hv_util_service util_fcopy = {
160 .util_cb = hv_fcopy_onchannelcallback,
161 .util_init = hv_fcopy_init,
162 .util_pre_suspend = hv_fcopy_pre_suspend,
163 .util_pre_resume = hv_fcopy_pre_resume,
164 .util_deinit = hv_fcopy_deinit,
165 };
166
perform_shutdown(struct work_struct * dummy)167 static void perform_shutdown(struct work_struct *dummy)
168 {
169 orderly_poweroff(true);
170 }
171
perform_restart(struct work_struct * dummy)172 static void perform_restart(struct work_struct *dummy)
173 {
174 orderly_reboot();
175 }
176
177 /*
178 * Perform the shutdown operation in a thread context.
179 */
180 static DECLARE_WORK(shutdown_work, perform_shutdown);
181
182 /*
183 * Perform the restart operation in a thread context.
184 */
185 static DECLARE_WORK(restart_work, perform_restart);
186
shutdown_onchannelcallback(void * context)187 static void shutdown_onchannelcallback(void *context)
188 {
189 struct vmbus_channel *channel = context;
190 struct work_struct *work = NULL;
191 u32 recvlen;
192 u64 requestid;
193 u8 *shut_txf_buf = util_shutdown.recv_buffer;
194
195 struct shutdown_msg_data *shutdown_msg;
196
197 struct icmsg_hdr *icmsghdrp;
198
199 if (vmbus_recvpacket(channel, shut_txf_buf, HV_HYP_PAGE_SIZE, &recvlen, &requestid)) {
200 pr_err_ratelimited("Shutdown request received. Could not read into shut txf buf\n");
201 return;
202 }
203
204 if (!recvlen)
205 return;
206
207 /* Ensure recvlen is big enough to read header data */
208 if (recvlen < ICMSG_HDR) {
209 pr_err_ratelimited("Shutdown request received. Packet length too small: %d\n",
210 recvlen);
211 return;
212 }
213
214 icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[sizeof(struct vmbuspipe_hdr)];
215
216 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
217 if (vmbus_prep_negotiate_resp(icmsghdrp,
218 shut_txf_buf, recvlen,
219 fw_versions, FW_VER_COUNT,
220 sd_versions, SD_VER_COUNT,
221 NULL, &sd_srv_version)) {
222 pr_info("Shutdown IC version %d.%d\n",
223 sd_srv_version >> 16,
224 sd_srv_version & 0xFFFF);
225 }
226 } else if (icmsghdrp->icmsgtype == ICMSGTYPE_SHUTDOWN) {
227 /* Ensure recvlen is big enough to contain shutdown_msg_data struct */
228 if (recvlen < ICMSG_HDR + sizeof(struct shutdown_msg_data)) {
229 pr_err_ratelimited("Invalid shutdown msg data. Packet length too small: %u\n",
230 recvlen);
231 return;
232 }
233
234 shutdown_msg = (struct shutdown_msg_data *)&shut_txf_buf[ICMSG_HDR];
235
236 /*
237 * shutdown_msg->flags can be 0(shut down), 2(reboot),
238 * or 4(hibernate). It may bitwise-OR 1, which means
239 * performing the request by force. Linux always tries
240 * to perform the request by force.
241 */
242 switch (shutdown_msg->flags) {
243 case 0:
244 case 1:
245 icmsghdrp->status = HV_S_OK;
246 work = &shutdown_work;
247 pr_info("Shutdown request received - graceful shutdown initiated\n");
248 break;
249 case 2:
250 case 3:
251 icmsghdrp->status = HV_S_OK;
252 work = &restart_work;
253 pr_info("Restart request received - graceful restart initiated\n");
254 break;
255 case 4:
256 case 5:
257 pr_info("Hibernation request received\n");
258 icmsghdrp->status = hibernation_supported ?
259 HV_S_OK : HV_E_FAIL;
260 if (hibernation_supported)
261 work = &hibernate_context.work;
262 break;
263 default:
264 icmsghdrp->status = HV_E_FAIL;
265 pr_info("Shutdown request received - Invalid request\n");
266 break;
267 }
268 } else {
269 icmsghdrp->status = HV_E_FAIL;
270 pr_err_ratelimited("Shutdown request received. Invalid msg type: %d\n",
271 icmsghdrp->icmsgtype);
272 }
273
274 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
275 | ICMSGHDRFLAG_RESPONSE;
276
277 vmbus_sendpacket(channel, shut_txf_buf,
278 recvlen, requestid,
279 VM_PKT_DATA_INBAND, 0);
280
281 if (work)
282 schedule_work(work);
283 }
284
285 /*
286 * Set the host time in a process context.
287 */
288 static struct work_struct adj_time_work;
289
290 /*
291 * The last time sample, received from the host. PTP device responds to
292 * requests by using this data and the current partition-wide time reference
293 * count.
294 */
295 static struct {
296 u64 host_time;
297 u64 ref_time;
298 spinlock_t lock;
299 } host_ts;
300
reftime_to_ns(u64 reftime)301 static inline u64 reftime_to_ns(u64 reftime)
302 {
303 return (reftime - WLTIMEDELTA) * 100;
304 }
305
306 /*
307 * Hard coded threshold for host timesync delay: 600 seconds
308 */
309 static const u64 HOST_TIMESYNC_DELAY_THRESH = 600 * (u64)NSEC_PER_SEC;
310
hv_get_adj_host_time(struct timespec64 * ts)311 static int hv_get_adj_host_time(struct timespec64 *ts)
312 {
313 u64 newtime, reftime, timediff_adj;
314 unsigned long flags;
315 int ret = 0;
316
317 spin_lock_irqsave(&host_ts.lock, flags);
318 reftime = hv_read_reference_counter();
319
320 /*
321 * We need to let the caller know that last update from host
322 * is older than the max allowable threshold. clock_gettime()
323 * and PTP ioctl do not have a documented error that we could
324 * return for this specific case. Use ESTALE to report this.
325 */
326 timediff_adj = reftime - host_ts.ref_time;
327 if (timediff_adj * 100 > HOST_TIMESYNC_DELAY_THRESH) {
328 pr_warn_once("TIMESYNC IC: Stale time stamp, %llu nsecs old\n",
329 (timediff_adj * 100));
330 ret = -ESTALE;
331 }
332
333 newtime = host_ts.host_time + timediff_adj;
334 *ts = ns_to_timespec64(reftime_to_ns(newtime));
335 spin_unlock_irqrestore(&host_ts.lock, flags);
336
337 return ret;
338 }
339
hv_set_host_time(struct work_struct * work)340 static void hv_set_host_time(struct work_struct *work)
341 {
342
343 struct timespec64 ts;
344
345 if (!hv_get_adj_host_time(&ts))
346 do_settimeofday64(&ts);
347 }
348
349 /*
350 * Synchronize time with host after reboot, restore, etc.
351 *
352 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
353 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
354 * message after the timesync channel is opened. Since the hv_utils module is
355 * loaded after hv_vmbus, the first message is usually missed. This bit is
356 * considered a hard request to discipline the clock.
357 *
358 * ICTIMESYNCFLAG_SAMPLE bit indicates a time sample from host. This is
359 * typically used as a hint to the guest. The guest is under no obligation
360 * to discipline the clock.
361 */
adj_guesttime(u64 hosttime,u64 reftime,u8 adj_flags)362 static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 adj_flags)
363 {
364 unsigned long flags;
365 u64 cur_reftime;
366
367 /*
368 * Save the adjusted time sample from the host and the snapshot
369 * of the current system time.
370 */
371 spin_lock_irqsave(&host_ts.lock, flags);
372
373 cur_reftime = hv_read_reference_counter();
374 host_ts.host_time = hosttime;
375 host_ts.ref_time = cur_reftime;
376
377 /*
378 * TimeSync v4 messages contain reference time (guest's Hyper-V
379 * clocksource read when the time sample was generated), we can
380 * improve the precision by adding the delta between now and the
381 * time of generation. For older protocols we set
382 * reftime == cur_reftime on call.
383 */
384 host_ts.host_time += (cur_reftime - reftime);
385
386 spin_unlock_irqrestore(&host_ts.lock, flags);
387
388 /* Schedule work to do do_settimeofday64() */
389 if (adj_flags & ICTIMESYNCFLAG_SYNC)
390 schedule_work(&adj_time_work);
391 }
392
393 /*
394 * Time Sync Channel message handler.
395 */
timesync_onchannelcallback(void * context)396 static void timesync_onchannelcallback(void *context)
397 {
398 struct vmbus_channel *channel = context;
399 u32 recvlen;
400 u64 requestid;
401 struct icmsg_hdr *icmsghdrp;
402 struct ictimesync_data *timedatap;
403 struct ictimesync_ref_data *refdata;
404 u8 *time_txf_buf = util_timesynch.recv_buffer;
405
406 /*
407 * Drain the ring buffer and use the last packet to update
408 * host_ts
409 */
410 while (1) {
411 int ret = vmbus_recvpacket(channel, time_txf_buf,
412 HV_HYP_PAGE_SIZE, &recvlen,
413 &requestid);
414 if (ret) {
415 pr_err_ratelimited("TimeSync IC pkt recv failed (Err: %d)\n",
416 ret);
417 break;
418 }
419
420 if (!recvlen)
421 break;
422
423 /* Ensure recvlen is big enough to read header data */
424 if (recvlen < ICMSG_HDR) {
425 pr_err_ratelimited("Timesync request received. Packet length too small: %d\n",
426 recvlen);
427 break;
428 }
429
430 icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
431 sizeof(struct vmbuspipe_hdr)];
432
433 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
434 if (vmbus_prep_negotiate_resp(icmsghdrp,
435 time_txf_buf, recvlen,
436 fw_versions, FW_VER_COUNT,
437 ts_versions, TS_VER_COUNT,
438 NULL, &ts_srv_version)) {
439 pr_info("TimeSync IC version %d.%d\n",
440 ts_srv_version >> 16,
441 ts_srv_version & 0xFFFF);
442 }
443 } else if (icmsghdrp->icmsgtype == ICMSGTYPE_TIMESYNC) {
444 if (ts_srv_version > TS_VERSION_3) {
445 /* Ensure recvlen is big enough to read ictimesync_ref_data */
446 if (recvlen < ICMSG_HDR + sizeof(struct ictimesync_ref_data)) {
447 pr_err_ratelimited("Invalid ictimesync ref data. Length too small: %u\n",
448 recvlen);
449 break;
450 }
451 refdata = (struct ictimesync_ref_data *)&time_txf_buf[ICMSG_HDR];
452
453 adj_guesttime(refdata->parenttime,
454 refdata->vmreferencetime,
455 refdata->flags);
456 } else {
457 /* Ensure recvlen is big enough to read ictimesync_data */
458 if (recvlen < ICMSG_HDR + sizeof(struct ictimesync_data)) {
459 pr_err_ratelimited("Invalid ictimesync data. Length too small: %u\n",
460 recvlen);
461 break;
462 }
463 timedatap = (struct ictimesync_data *)&time_txf_buf[ICMSG_HDR];
464
465 adj_guesttime(timedatap->parenttime,
466 hv_read_reference_counter(),
467 timedatap->flags);
468 }
469 } else {
470 icmsghdrp->status = HV_E_FAIL;
471 pr_err_ratelimited("Timesync request received. Invalid msg type: %d\n",
472 icmsghdrp->icmsgtype);
473 }
474
475 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
476 | ICMSGHDRFLAG_RESPONSE;
477
478 vmbus_sendpacket(channel, time_txf_buf,
479 recvlen, requestid,
480 VM_PKT_DATA_INBAND, 0);
481 }
482 }
483
484 /*
485 * Heartbeat functionality.
486 * Every two seconds, Hyper-V send us a heartbeat request message.
487 * we respond to this message, and Hyper-V knows we are alive.
488 */
heartbeat_onchannelcallback(void * context)489 static void heartbeat_onchannelcallback(void *context)
490 {
491 struct vmbus_channel *channel = context;
492 u32 recvlen;
493 u64 requestid;
494 struct icmsg_hdr *icmsghdrp;
495 struct heartbeat_msg_data *heartbeat_msg;
496 u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
497
498 while (1) {
499
500 if (vmbus_recvpacket(channel, hbeat_txf_buf, HV_HYP_PAGE_SIZE,
501 &recvlen, &requestid)) {
502 pr_err_ratelimited("Heartbeat request received. Could not read into hbeat txf buf\n");
503 return;
504 }
505
506 if (!recvlen)
507 break;
508
509 /* Ensure recvlen is big enough to read header data */
510 if (recvlen < ICMSG_HDR) {
511 pr_err_ratelimited("Heartbeat request received. Packet length too small: %d\n",
512 recvlen);
513 break;
514 }
515
516 icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
517 sizeof(struct vmbuspipe_hdr)];
518
519 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
520 if (vmbus_prep_negotiate_resp(icmsghdrp,
521 hbeat_txf_buf, recvlen,
522 fw_versions, FW_VER_COUNT,
523 hb_versions, HB_VER_COUNT,
524 NULL, &hb_srv_version)) {
525
526 pr_info("Heartbeat IC version %d.%d\n",
527 hb_srv_version >> 16,
528 hb_srv_version & 0xFFFF);
529 }
530 } else if (icmsghdrp->icmsgtype == ICMSGTYPE_HEARTBEAT) {
531 /*
532 * Ensure recvlen is big enough to read seq_num. Reserved area is not
533 * included in the check as the host may not fill it up entirely
534 */
535 if (recvlen < ICMSG_HDR + sizeof(u64)) {
536 pr_err_ratelimited("Invalid heartbeat msg data. Length too small: %u\n",
537 recvlen);
538 break;
539 }
540 heartbeat_msg = (struct heartbeat_msg_data *)&hbeat_txf_buf[ICMSG_HDR];
541
542 heartbeat_msg->seq_num += 1;
543 } else {
544 icmsghdrp->status = HV_E_FAIL;
545 pr_err_ratelimited("Heartbeat request received. Invalid msg type: %d\n",
546 icmsghdrp->icmsgtype);
547 }
548
549 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
550 | ICMSGHDRFLAG_RESPONSE;
551
552 vmbus_sendpacket(channel, hbeat_txf_buf,
553 recvlen, requestid,
554 VM_PKT_DATA_INBAND, 0);
555 }
556 }
557
558 #define HV_UTIL_RING_SEND_SIZE VMBUS_RING_SIZE(3 * HV_HYP_PAGE_SIZE)
559 #define HV_UTIL_RING_RECV_SIZE VMBUS_RING_SIZE(3 * HV_HYP_PAGE_SIZE)
560
util_probe(struct hv_device * dev,const struct hv_vmbus_device_id * dev_id)561 static int util_probe(struct hv_device *dev,
562 const struct hv_vmbus_device_id *dev_id)
563 {
564 struct hv_util_service *srv =
565 (struct hv_util_service *)dev_id->driver_data;
566 int ret;
567
568 srv->recv_buffer = kmalloc(HV_HYP_PAGE_SIZE * 4, GFP_KERNEL);
569 if (!srv->recv_buffer)
570 return -ENOMEM;
571 srv->channel = dev->channel;
572 if (srv->util_init) {
573 ret = srv->util_init(srv);
574 if (ret) {
575 ret = -ENODEV;
576 goto error1;
577 }
578 }
579
580 /*
581 * The set of services managed by the util driver are not performance
582 * critical and do not need batched reading. Furthermore, some services
583 * such as KVP can only handle one message from the host at a time.
584 * Turn off batched reading for all util drivers before we open the
585 * channel.
586 */
587 set_channel_read_mode(dev->channel, HV_CALL_DIRECT);
588
589 hv_set_drvdata(dev, srv);
590
591 ret = vmbus_open(dev->channel, HV_UTIL_RING_SEND_SIZE,
592 HV_UTIL_RING_RECV_SIZE, NULL, 0, srv->util_cb,
593 dev->channel);
594 if (ret)
595 goto error;
596
597 if (srv->util_init_transport) {
598 ret = srv->util_init_transport();
599 if (ret) {
600 vmbus_close(dev->channel);
601 goto error;
602 }
603 }
604 return 0;
605
606 error:
607 if (srv->util_deinit)
608 srv->util_deinit();
609 error1:
610 kfree(srv->recv_buffer);
611 return ret;
612 }
613
util_remove(struct hv_device * dev)614 static void util_remove(struct hv_device *dev)
615 {
616 struct hv_util_service *srv = hv_get_drvdata(dev);
617
618 if (srv->util_deinit)
619 srv->util_deinit();
620 vmbus_close(dev->channel);
621 kfree(srv->recv_buffer);
622 }
623
624 /*
625 * When we're in util_suspend(), all the userspace processes have been frozen
626 * (refer to hibernate() -> freeze_processes()). The userspace is thawed only
627 * after the whole resume procedure, including util_resume(), finishes.
628 */
util_suspend(struct hv_device * dev)629 static int util_suspend(struct hv_device *dev)
630 {
631 struct hv_util_service *srv = hv_get_drvdata(dev);
632 int ret = 0;
633
634 if (srv->util_pre_suspend) {
635 ret = srv->util_pre_suspend();
636 if (ret)
637 return ret;
638 }
639
640 vmbus_close(dev->channel);
641
642 return 0;
643 }
644
util_resume(struct hv_device * dev)645 static int util_resume(struct hv_device *dev)
646 {
647 struct hv_util_service *srv = hv_get_drvdata(dev);
648 int ret = 0;
649
650 if (srv->util_pre_resume) {
651 ret = srv->util_pre_resume();
652 if (ret)
653 return ret;
654 }
655
656 ret = vmbus_open(dev->channel, HV_UTIL_RING_SEND_SIZE,
657 HV_UTIL_RING_RECV_SIZE, NULL, 0, srv->util_cb,
658 dev->channel);
659 return ret;
660 }
661
662 static const struct hv_vmbus_device_id id_table[] = {
663 /* Shutdown guid */
664 { HV_SHUTDOWN_GUID,
665 .driver_data = (unsigned long)&util_shutdown
666 },
667 /* Time synch guid */
668 { HV_TS_GUID,
669 .driver_data = (unsigned long)&util_timesynch
670 },
671 /* Heartbeat guid */
672 { HV_HEART_BEAT_GUID,
673 .driver_data = (unsigned long)&util_heartbeat
674 },
675 /* KVP guid */
676 { HV_KVP_GUID,
677 .driver_data = (unsigned long)&util_kvp
678 },
679 /* VSS GUID */
680 { HV_VSS_GUID,
681 .driver_data = (unsigned long)&util_vss
682 },
683 /* File copy GUID */
684 { HV_FCOPY_GUID,
685 .driver_data = (unsigned long)&util_fcopy
686 },
687 { },
688 };
689
690 MODULE_DEVICE_TABLE(vmbus, id_table);
691
692 /* The one and only one */
693 static struct hv_driver util_drv = {
694 .name = "hv_utils",
695 .id_table = id_table,
696 .probe = util_probe,
697 .remove = util_remove,
698 .suspend = util_suspend,
699 .resume = util_resume,
700 .driver = {
701 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
702 },
703 };
704
hv_ptp_enable(struct ptp_clock_info * info,struct ptp_clock_request * request,int on)705 static int hv_ptp_enable(struct ptp_clock_info *info,
706 struct ptp_clock_request *request, int on)
707 {
708 return -EOPNOTSUPP;
709 }
710
hv_ptp_settime(struct ptp_clock_info * p,const struct timespec64 * ts)711 static int hv_ptp_settime(struct ptp_clock_info *p, const struct timespec64 *ts)
712 {
713 return -EOPNOTSUPP;
714 }
715
hv_ptp_adjfine(struct ptp_clock_info * ptp,long delta)716 static int hv_ptp_adjfine(struct ptp_clock_info *ptp, long delta)
717 {
718 return -EOPNOTSUPP;
719 }
hv_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)720 static int hv_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
721 {
722 return -EOPNOTSUPP;
723 }
724
hv_ptp_gettime(struct ptp_clock_info * info,struct timespec64 * ts)725 static int hv_ptp_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
726 {
727 return hv_get_adj_host_time(ts);
728 }
729
730 static struct ptp_clock_info ptp_hyperv_info = {
731 .name = "hyperv",
732 .enable = hv_ptp_enable,
733 .adjtime = hv_ptp_adjtime,
734 .adjfine = hv_ptp_adjfine,
735 .gettime64 = hv_ptp_gettime,
736 .settime64 = hv_ptp_settime,
737 .owner = THIS_MODULE,
738 };
739
740 static struct ptp_clock *hv_ptp_clock;
741
hv_timesync_init(struct hv_util_service * srv)742 static int hv_timesync_init(struct hv_util_service *srv)
743 {
744 spin_lock_init(&host_ts.lock);
745
746 INIT_WORK(&adj_time_work, hv_set_host_time);
747
748 /*
749 * ptp_clock_register() returns NULL when CONFIG_PTP_1588_CLOCK is
750 * disabled but the driver is still useful without the PTP device
751 * as it still handles the ICTIMESYNCFLAG_SYNC case.
752 */
753 hv_ptp_clock = ptp_clock_register(&ptp_hyperv_info, NULL);
754 if (IS_ERR_OR_NULL(hv_ptp_clock)) {
755 pr_err("cannot register PTP clock: %d\n",
756 PTR_ERR_OR_ZERO(hv_ptp_clock));
757 hv_ptp_clock = NULL;
758 }
759
760 return 0;
761 }
762
hv_timesync_cancel_work(void)763 static void hv_timesync_cancel_work(void)
764 {
765 cancel_work_sync(&adj_time_work);
766 }
767
hv_timesync_pre_suspend(void)768 static int hv_timesync_pre_suspend(void)
769 {
770 hv_timesync_cancel_work();
771 return 0;
772 }
773
hv_timesync_deinit(void)774 static void hv_timesync_deinit(void)
775 {
776 if (hv_ptp_clock)
777 ptp_clock_unregister(hv_ptp_clock);
778
779 hv_timesync_cancel_work();
780 }
781
init_hyperv_utils(void)782 static int __init init_hyperv_utils(void)
783 {
784 pr_info("Registering HyperV Utility Driver\n");
785
786 return vmbus_driver_register(&util_drv);
787 }
788
exit_hyperv_utils(void)789 static void exit_hyperv_utils(void)
790 {
791 pr_info("De-Registered HyperV Utility Driver\n");
792
793 vmbus_driver_unregister(&util_drv);
794 }
795
796 module_init(init_hyperv_utils);
797 module_exit(exit_hyperv_utils);
798
799 MODULE_DESCRIPTION("Hyper-V Utilities");
800 MODULE_LICENSE("GPL");
801