blob: 34bbccf2450efbcaaa09391e6f4350466d409fb8 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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 <clocksource/hyperv_timer.h>
21#include <asm/mshyperv.h>
22
23#include "hyperv_vmbus.h"
24
25#define SD_MAJOR 3
26#define SD_MINOR 0
27#define SD_VERSION (SD_MAJOR << 16 | SD_MINOR)
28
29#define SD_MAJOR_1 1
30#define SD_VERSION_1 (SD_MAJOR_1 << 16 | SD_MINOR)
31
32#define TS_MAJOR 4
33#define TS_MINOR 0
34#define TS_VERSION (TS_MAJOR << 16 | TS_MINOR)
35
36#define TS_MAJOR_1 1
37#define TS_VERSION_1 (TS_MAJOR_1 << 16 | TS_MINOR)
38
39#define TS_MAJOR_3 3
40#define TS_VERSION_3 (TS_MAJOR_3 << 16 | TS_MINOR)
41
42#define HB_MAJOR 3
43#define HB_MINOR 0
44#define HB_VERSION (HB_MAJOR << 16 | HB_MINOR)
45
46#define HB_MAJOR_1 1
47#define HB_VERSION_1 (HB_MAJOR_1 << 16 | HB_MINOR)
48
49static int sd_srv_version;
50static int ts_srv_version;
51static int hb_srv_version;
52
53#define SD_VER_COUNT 2
54static const int sd_versions[] = {
55 SD_VERSION,
56 SD_VERSION_1
57};
58
59#define TS_VER_COUNT 3
60static const int ts_versions[] = {
61 TS_VERSION,
62 TS_VERSION_3,
63 TS_VERSION_1
64};
65
66#define HB_VER_COUNT 2
67static const int hb_versions[] = {
68 HB_VERSION,
69 HB_VERSION_1
70};
71
72#define FW_VER_COUNT 2
73static const int fw_versions[] = {
74 UTIL_FW_VERSION,
75 UTIL_WS2K8_FW_VERSION
76};
77
78static void shutdown_onchannelcallback(void *context);
79static struct hv_util_service util_shutdown = {
80 .util_cb = shutdown_onchannelcallback,
81};
82
83static int hv_timesync_init(struct hv_util_service *srv);
84static void hv_timesync_deinit(void);
85
86static void timesync_onchannelcallback(void *context);
87static struct hv_util_service util_timesynch = {
88 .util_cb = timesync_onchannelcallback,
89 .util_init = hv_timesync_init,
90 .util_deinit = hv_timesync_deinit,
91};
92
93static void heartbeat_onchannelcallback(void *context);
94static struct hv_util_service util_heartbeat = {
95 .util_cb = heartbeat_onchannelcallback,
96};
97
98static struct hv_util_service util_kvp = {
99 .util_cb = hv_kvp_onchannelcallback,
100 .util_init = hv_kvp_init,
101 .util_init_transport = hv_kvp_init_transport,
102 .util_deinit = hv_kvp_deinit,
103};
104
105static struct hv_util_service util_vss = {
106 .util_cb = hv_vss_onchannelcallback,
107 .util_init = hv_vss_init,
108 .util_init_transport = hv_vss_init_transport,
109 .util_deinit = hv_vss_deinit,
110};
111
112static struct hv_util_service util_fcopy = {
113 .util_cb = hv_fcopy_onchannelcallback,
114 .util_init = hv_fcopy_init,
115 .util_deinit = hv_fcopy_deinit,
116};
117
118static void perform_shutdown(struct work_struct *dummy)
119{
120 orderly_poweroff(true);
121}
122
123/*
124 * Perform the shutdown operation in a thread context.
125 */
126static DECLARE_WORK(shutdown_work, perform_shutdown);
127
128static void shutdown_onchannelcallback(void *context)
129{
130 struct vmbus_channel *channel = context;
131 u32 recvlen;
132 u64 requestid;
133 bool execute_shutdown = false;
134 u8 *shut_txf_buf = util_shutdown.recv_buffer;
135
136 struct shutdown_msg_data *shutdown_msg;
137
138 struct icmsg_hdr *icmsghdrp;
139
140 vmbus_recvpacket(channel, shut_txf_buf,
141 PAGE_SIZE, &recvlen, &requestid);
142
143 if (recvlen > 0) {
144 icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
145 sizeof(struct vmbuspipe_hdr)];
146
147 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
148 if (vmbus_prep_negotiate_resp(icmsghdrp, shut_txf_buf,
149 fw_versions, FW_VER_COUNT,
150 sd_versions, SD_VER_COUNT,
151 NULL, &sd_srv_version)) {
152 pr_info("Shutdown IC version %d.%d\n",
153 sd_srv_version >> 16,
154 sd_srv_version & 0xFFFF);
155 }
156 } else {
157 shutdown_msg =
158 (struct shutdown_msg_data *)&shut_txf_buf[
159 sizeof(struct vmbuspipe_hdr) +
160 sizeof(struct icmsg_hdr)];
161
162 switch (shutdown_msg->flags) {
163 case 0:
164 case 1:
165 icmsghdrp->status = HV_S_OK;
166 execute_shutdown = true;
167
168 pr_info("Shutdown request received -"
169 " graceful shutdown initiated\n");
170 break;
171 default:
172 icmsghdrp->status = HV_E_FAIL;
173 execute_shutdown = false;
174
175 pr_info("Shutdown request received -"
176 " Invalid request\n");
177 break;
178 }
179 }
180
181 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
182 | ICMSGHDRFLAG_RESPONSE;
183
184 vmbus_sendpacket(channel, shut_txf_buf,
185 recvlen, requestid,
186 VM_PKT_DATA_INBAND, 0);
187 }
188
189 if (execute_shutdown == true)
190 schedule_work(&shutdown_work);
191}
192
193/*
194 * Set the host time in a process context.
195 */
196static struct work_struct adj_time_work;
197
198/*
199 * The last time sample, received from the host. PTP device responds to
200 * requests by using this data and the current partition-wide time reference
201 * count.
202 */
203static struct {
204 u64 host_time;
205 u64 ref_time;
206 spinlock_t lock;
207} host_ts;
208
209static struct timespec64 hv_get_adj_host_time(void)
210{
211 struct timespec64 ts;
212 u64 newtime, reftime;
213 unsigned long flags;
214
215 spin_lock_irqsave(&host_ts.lock, flags);
216 reftime = hyperv_cs->read(hyperv_cs);
217 newtime = host_ts.host_time + (reftime - host_ts.ref_time);
218 ts = ns_to_timespec64((newtime - WLTIMEDELTA) * 100);
219 spin_unlock_irqrestore(&host_ts.lock, flags);
220
221 return ts;
222}
223
224static void hv_set_host_time(struct work_struct *work)
225{
226 struct timespec64 ts = hv_get_adj_host_time();
227
228 do_settimeofday64(&ts);
229}
230
231/*
232 * Synchronize time with host after reboot, restore, etc.
233 *
234 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
235 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
236 * message after the timesync channel is opened. Since the hv_utils module is
237 * loaded after hv_vmbus, the first message is usually missed. This bit is
238 * considered a hard request to discipline the clock.
239 *
240 * ICTIMESYNCFLAG_SAMPLE bit indicates a time sample from host. This is
241 * typically used as a hint to the guest. The guest is under no obligation
242 * to discipline the clock.
243 */
244static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 adj_flags)
245{
246 unsigned long flags;
247 u64 cur_reftime;
248
249 /*
250 * Save the adjusted time sample from the host and the snapshot
251 * of the current system time.
252 */
253 spin_lock_irqsave(&host_ts.lock, flags);
254
255 cur_reftime = hyperv_cs->read(hyperv_cs);
256 host_ts.host_time = hosttime;
257 host_ts.ref_time = cur_reftime;
258
259 /*
260 * TimeSync v4 messages contain reference time (guest's Hyper-V
261 * clocksource read when the time sample was generated), we can
262 * improve the precision by adding the delta between now and the
263 * time of generation. For older protocols we set
264 * reftime == cur_reftime on call.
265 */
266 host_ts.host_time += (cur_reftime - reftime);
267
268 spin_unlock_irqrestore(&host_ts.lock, flags);
269
270 /* Schedule work to do do_settimeofday64() */
271 if (adj_flags & ICTIMESYNCFLAG_SYNC)
272 schedule_work(&adj_time_work);
273}
274
275/*
276 * Time Sync Channel message handler.
277 */
278static void timesync_onchannelcallback(void *context)
279{
280 struct vmbus_channel *channel = context;
281 u32 recvlen;
282 u64 requestid;
283 struct icmsg_hdr *icmsghdrp;
284 struct ictimesync_data *timedatap;
285 struct ictimesync_ref_data *refdata;
286 u8 *time_txf_buf = util_timesynch.recv_buffer;
287
288 /*
289 * Drain the ring buffer and use the last packet to update
290 * host_ts
291 */
292 while (1) {
293 int ret = vmbus_recvpacket(channel, time_txf_buf,
294 HV_HYP_PAGE_SIZE, &recvlen,
295 &requestid);
296 if (ret) {
297 pr_warn_once("TimeSync IC pkt recv failed (Err: %d)\n",
298 ret);
299 break;
300 }
301
302 if (!recvlen)
303 break;
304
305 icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
306 sizeof(struct vmbuspipe_hdr)];
307
308 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
309 if (vmbus_prep_negotiate_resp(icmsghdrp, time_txf_buf,
310 fw_versions, FW_VER_COUNT,
311 ts_versions, TS_VER_COUNT,
312 NULL, &ts_srv_version)) {
313 pr_info("TimeSync IC version %d.%d\n",
314 ts_srv_version >> 16,
315 ts_srv_version & 0xFFFF);
316 }
317 } else {
318 if (ts_srv_version > TS_VERSION_3) {
319 refdata = (struct ictimesync_ref_data *)
320 &time_txf_buf[
321 sizeof(struct vmbuspipe_hdr) +
322 sizeof(struct icmsg_hdr)];
323
324 adj_guesttime(refdata->parenttime,
325 refdata->vmreferencetime,
326 refdata->flags);
327 } else {
328 timedatap = (struct ictimesync_data *)
329 &time_txf_buf[
330 sizeof(struct vmbuspipe_hdr) +
331 sizeof(struct icmsg_hdr)];
332 adj_guesttime(timedatap->parenttime,
333 hyperv_cs->read(hyperv_cs),
334 timedatap->flags);
335 }
336 }
337
338 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
339 | ICMSGHDRFLAG_RESPONSE;
340
341 vmbus_sendpacket(channel, time_txf_buf,
342 recvlen, requestid,
343 VM_PKT_DATA_INBAND, 0);
344 }
345}
346
347/*
348 * Heartbeat functionality.
349 * Every two seconds, Hyper-V send us a heartbeat request message.
350 * we respond to this message, and Hyper-V knows we are alive.
351 */
352static void heartbeat_onchannelcallback(void *context)
353{
354 struct vmbus_channel *channel = context;
355 u32 recvlen;
356 u64 requestid;
357 struct icmsg_hdr *icmsghdrp;
358 struct heartbeat_msg_data *heartbeat_msg;
359 u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
360
361 while (1) {
362
363 vmbus_recvpacket(channel, hbeat_txf_buf,
364 PAGE_SIZE, &recvlen, &requestid);
365
366 if (!recvlen)
367 break;
368
369 icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
370 sizeof(struct vmbuspipe_hdr)];
371
372 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
373 if (vmbus_prep_negotiate_resp(icmsghdrp,
374 hbeat_txf_buf,
375 fw_versions, FW_VER_COUNT,
376 hb_versions, HB_VER_COUNT,
377 NULL, &hb_srv_version)) {
378
379 pr_info("Heartbeat IC version %d.%d\n",
380 hb_srv_version >> 16,
381 hb_srv_version & 0xFFFF);
382 }
383 } else {
384 heartbeat_msg =
385 (struct heartbeat_msg_data *)&hbeat_txf_buf[
386 sizeof(struct vmbuspipe_hdr) +
387 sizeof(struct icmsg_hdr)];
388
389 heartbeat_msg->seq_num += 1;
390 }
391
392 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
393 | ICMSGHDRFLAG_RESPONSE;
394
395 vmbus_sendpacket(channel, hbeat_txf_buf,
396 recvlen, requestid,
397 VM_PKT_DATA_INBAND, 0);
398 }
399}
400
401static int util_probe(struct hv_device *dev,
402 const struct hv_vmbus_device_id *dev_id)
403{
404 struct hv_util_service *srv =
405 (struct hv_util_service *)dev_id->driver_data;
406 int ret;
407
408 srv->recv_buffer = kmalloc(PAGE_SIZE * 4, GFP_KERNEL);
409 if (!srv->recv_buffer)
410 return -ENOMEM;
411 srv->channel = dev->channel;
412 if (srv->util_init) {
413 ret = srv->util_init(srv);
414 if (ret) {
415 ret = -ENODEV;
416 goto error1;
417 }
418 }
419
420 /*
421 * The set of services managed by the util driver are not performance
422 * critical and do not need batched reading. Furthermore, some services
423 * such as KVP can only handle one message from the host at a time.
424 * Turn off batched reading for all util drivers before we open the
425 * channel.
426 */
427 set_channel_read_mode(dev->channel, HV_CALL_DIRECT);
428
429 hv_set_drvdata(dev, srv);
430
431 ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
432 srv->util_cb, dev->channel);
433 if (ret)
434 goto error;
435
436 if (srv->util_init_transport) {
437 ret = srv->util_init_transport();
438 if (ret) {
439 vmbus_close(dev->channel);
440 goto error;
441 }
442 }
443 return 0;
444
445error:
446 if (srv->util_deinit)
447 srv->util_deinit();
448error1:
449 kfree(srv->recv_buffer);
450 return ret;
451}
452
453static int util_remove(struct hv_device *dev)
454{
455 struct hv_util_service *srv = hv_get_drvdata(dev);
456
457 if (srv->util_deinit)
458 srv->util_deinit();
459 vmbus_close(dev->channel);
460 kfree(srv->recv_buffer);
461
462 return 0;
463}
464
465static const struct hv_vmbus_device_id id_table[] = {
466 /* Shutdown guid */
467 { HV_SHUTDOWN_GUID,
468 .driver_data = (unsigned long)&util_shutdown
469 },
470 /* Time synch guid */
471 { HV_TS_GUID,
472 .driver_data = (unsigned long)&util_timesynch
473 },
474 /* Heartbeat guid */
475 { HV_HEART_BEAT_GUID,
476 .driver_data = (unsigned long)&util_heartbeat
477 },
478 /* KVP guid */
479 { HV_KVP_GUID,
480 .driver_data = (unsigned long)&util_kvp
481 },
482 /* VSS GUID */
483 { HV_VSS_GUID,
484 .driver_data = (unsigned long)&util_vss
485 },
486 /* File copy GUID */
487 { HV_FCOPY_GUID,
488 .driver_data = (unsigned long)&util_fcopy
489 },
490 { },
491};
492
493MODULE_DEVICE_TABLE(vmbus, id_table);
494
495/* The one and only one */
496static struct hv_driver util_drv = {
497 .name = "hv_utils",
498 .id_table = id_table,
499 .probe = util_probe,
500 .remove = util_remove,
501 .driver = {
502 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
503 },
504};
505
506static int hv_ptp_enable(struct ptp_clock_info *info,
507 struct ptp_clock_request *request, int on)
508{
509 return -EOPNOTSUPP;
510}
511
512static int hv_ptp_settime(struct ptp_clock_info *p, const struct timespec64 *ts)
513{
514 return -EOPNOTSUPP;
515}
516
517static int hv_ptp_adjfreq(struct ptp_clock_info *ptp, s32 delta)
518{
519 return -EOPNOTSUPP;
520}
521static int hv_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
522{
523 return -EOPNOTSUPP;
524}
525
526static int hv_ptp_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
527{
528 *ts = hv_get_adj_host_time();
529
530 return 0;
531}
532
533static struct ptp_clock_info ptp_hyperv_info = {
534 .name = "hyperv",
535 .enable = hv_ptp_enable,
536 .adjtime = hv_ptp_adjtime,
537 .adjfreq = hv_ptp_adjfreq,
538 .gettime64 = hv_ptp_gettime,
539 .settime64 = hv_ptp_settime,
540 .owner = THIS_MODULE,
541};
542
543static struct ptp_clock *hv_ptp_clock;
544
545static int hv_timesync_init(struct hv_util_service *srv)
546{
547 /* TimeSync requires Hyper-V clocksource. */
548 if (!hyperv_cs)
549 return -ENODEV;
550
551 spin_lock_init(&host_ts.lock);
552
553 INIT_WORK(&adj_time_work, hv_set_host_time);
554
555 /*
556 * ptp_clock_register() returns NULL when CONFIG_PTP_1588_CLOCK is
557 * disabled but the driver is still useful without the PTP device
558 * as it still handles the ICTIMESYNCFLAG_SYNC case.
559 */
560 hv_ptp_clock = ptp_clock_register(&ptp_hyperv_info, NULL);
561 if (IS_ERR_OR_NULL(hv_ptp_clock)) {
562 pr_err("cannot register PTP clock: %d\n",
563 PTR_ERR_OR_ZERO(hv_ptp_clock));
564 hv_ptp_clock = NULL;
565 }
566
567 return 0;
568}
569
570static void hv_timesync_deinit(void)
571{
572 if (hv_ptp_clock)
573 ptp_clock_unregister(hv_ptp_clock);
574 cancel_work_sync(&adj_time_work);
575}
576
577static int __init init_hyperv_utils(void)
578{
579 pr_info("Registering HyperV Utility Driver\n");
580
581 return vmbus_driver_register(&util_drv);
582}
583
584static void exit_hyperv_utils(void)
585{
586 pr_info("De-Registered HyperV Utility Driver\n");
587
588 vmbus_driver_unregister(&util_drv);
589}
590
591module_init(init_hyperv_utils);
592module_exit(exit_hyperv_utils);
593
594MODULE_DESCRIPTION("Hyper-V Utilities");
595MODULE_LICENSE("GPL");