blob: 835e6039c186789433f54562378f43df8851fabb [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Hank Janssenc88c4e42010-05-04 15:55:05 -07002/*
3 * Copyright (c) 2010, Microsoft Corporation.
4 *
Hank Janssenc88c4e42010-05-04 15:55:05 -07005 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Hank Janssenc88c4e42010-05-04 15:55:05 -070011#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/sysctl.h>
Greg Kroah-Hartman9e629072010-05-04 08:26:23 -070016#include <linux/reboot.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070017#include <linux/hyperv.h>
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -070018#include <linux/clockchips.h>
19#include <linux/ptp_clock_kernel.h>
K. Y. Srinivasan305f7542017-01-19 11:51:52 -070020#include <asm/mshyperv.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070021
K. Y. Srinivasan013254762014-02-16 11:34:30 -080022#include "hyperv_vmbus.h"
K. Y. Srinivasan67413352013-07-02 10:31:30 -070023
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070024#define SD_MAJOR 3
25#define SD_MINOR 0
Dexuan Cui3e9c72052020-01-25 21:49:42 -080026#define SD_MINOR_1 1
Dexuan Cuiffd1d4a42020-01-25 21:49:43 -080027#define SD_MINOR_2 2
Dexuan Cui3e9c72052020-01-25 21:49:42 -080028#define SD_VERSION_3_1 (SD_MAJOR << 16 | SD_MINOR_1)
Dexuan Cuiffd1d4a42020-01-25 21:49:43 -080029#define SD_VERSION_3_2 (SD_MAJOR << 16 | SD_MINOR_2)
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070030#define SD_VERSION (SD_MAJOR << 16 | SD_MINOR)
K. Y. Srinivasan67413352013-07-02 10:31:30 -070031
Alex Ngabeda472016-09-08 05:24:12 -070032#define SD_MAJOR_1 1
33#define SD_VERSION_1 (SD_MAJOR_1 << 16 | SD_MINOR)
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070034
Alex Ng8e1d2602016-09-08 05:24:14 -070035#define TS_MAJOR 4
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070036#define TS_MINOR 0
37#define TS_VERSION (TS_MAJOR << 16 | TS_MINOR)
38
Alex Ngabeda472016-09-08 05:24:12 -070039#define TS_MAJOR_1 1
40#define TS_VERSION_1 (TS_MAJOR_1 << 16 | TS_MINOR)
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070041
Alex Ng8e1d2602016-09-08 05:24:14 -070042#define TS_MAJOR_3 3
43#define TS_VERSION_3 (TS_MAJOR_3 << 16 | TS_MINOR)
44
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070045#define HB_MAJOR 3
Alex Ngabeda472016-09-08 05:24:12 -070046#define HB_MINOR 0
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070047#define HB_VERSION (HB_MAJOR << 16 | HB_MINOR)
48
Alex Ngabeda472016-09-08 05:24:12 -070049#define HB_MAJOR_1 1
50#define HB_VERSION_1 (HB_MAJOR_1 << 16 | HB_MINOR)
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070051
52static int sd_srv_version;
53static int ts_srv_version;
54static int hb_srv_version;
Alex Nga1656452017-01-28 12:37:17 -070055
Dexuan Cuiffd1d4a42020-01-25 21:49:43 -080056#define SD_VER_COUNT 4
Alex Nga1656452017-01-28 12:37:17 -070057static const int sd_versions[] = {
Dexuan Cuiffd1d4a42020-01-25 21:49:43 -080058 SD_VERSION_3_2,
Dexuan Cui3e9c72052020-01-25 21:49:42 -080059 SD_VERSION_3_1,
Alex Nga1656452017-01-28 12:37:17 -070060 SD_VERSION,
61 SD_VERSION_1
62};
63
64#define TS_VER_COUNT 3
65static const int ts_versions[] = {
66 TS_VERSION,
67 TS_VERSION_3,
68 TS_VERSION_1
69};
70
71#define HB_VER_COUNT 2
72static const int hb_versions[] = {
73 HB_VERSION,
74 HB_VERSION_1
75};
76
77#define FW_VER_COUNT 2
78static const int fw_versions[] = {
79 UTIL_FW_VERSION,
80 UTIL_WS2K8_FW_VERSION
81};
K. Y. Srinivasan67413352013-07-02 10:31:30 -070082
Dexuan Cuiffd1d4a42020-01-25 21:49:43 -080083/*
84 * Send the "hibernate" udev event in a thread context.
85 */
86struct hibernate_work_context {
87 struct work_struct work;
88 struct hv_device *dev;
89};
90
91static struct hibernate_work_context hibernate_context;
92static bool hibernation_supported;
93
94static 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
106static 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
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700118static void shutdown_onchannelcallback(void *context);
119static struct hv_util_service util_shutdown = {
120 .util_cb = shutdown_onchannelcallback,
Dexuan Cuiffd1d4a42020-01-25 21:49:43 -0800121 .util_init = hv_shutdown_init,
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700122};
123
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700124static int hv_timesync_init(struct hv_util_service *srv);
Dexuan Cui54e19d32020-01-25 21:49:44 -0800125static int hv_timesync_pre_suspend(void);
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700126static void hv_timesync_deinit(void);
127
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700128static void timesync_onchannelcallback(void *context);
129static struct hv_util_service util_timesynch = {
130 .util_cb = timesync_onchannelcallback,
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700131 .util_init = hv_timesync_init,
Dexuan Cui54e19d32020-01-25 21:49:44 -0800132 .util_pre_suspend = hv_timesync_pre_suspend,
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700133 .util_deinit = hv_timesync_deinit,
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700134};
135
136static void heartbeat_onchannelcallback(void *context);
137static struct hv_util_service util_heartbeat = {
138 .util_cb = heartbeat_onchannelcallback,
139};
140
141static struct hv_util_service util_kvp = {
142 .util_cb = hv_kvp_onchannelcallback,
143 .util_init = hv_kvp_init,
Dexuan Cui54e19d32020-01-25 21:49:44 -0800144 .util_pre_suspend = hv_kvp_pre_suspend,
145 .util_pre_resume = hv_kvp_pre_resume,
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700146 .util_deinit = hv_kvp_deinit,
147};
Hank Janssenc88c4e42010-05-04 15:55:05 -0700148
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700149static struct hv_util_service util_vss = {
150 .util_cb = hv_vss_onchannelcallback,
151 .util_init = hv_vss_init,
Dexuan Cui54e19d32020-01-25 21:49:44 -0800152 .util_pre_suspend = hv_vss_pre_suspend,
153 .util_pre_resume = hv_vss_pre_resume,
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700154 .util_deinit = hv_vss_deinit,
155};
156
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800157static struct hv_util_service util_fcopy = {
158 .util_cb = hv_fcopy_onchannelcallback,
159 .util_init = hv_fcopy_init,
Dexuan Cui54e19d32020-01-25 21:49:44 -0800160 .util_pre_suspend = hv_fcopy_pre_suspend,
161 .util_pre_resume = hv_fcopy_pre_resume,
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800162 .util_deinit = hv_fcopy_deinit,
163};
164
K. Y. Srinivasan3dd6cb42013-01-23 17:42:45 -0800165static void perform_shutdown(struct work_struct *dummy)
166{
167 orderly_poweroff(true);
168}
169
Dexuan Cui3e9c72052020-01-25 21:49:42 -0800170static void perform_restart(struct work_struct *dummy)
171{
172 orderly_reboot();
173}
174
K. Y. Srinivasan3dd6cb42013-01-23 17:42:45 -0800175/*
176 * Perform the shutdown operation in a thread context.
177 */
178static DECLARE_WORK(shutdown_work, perform_shutdown);
179
Dexuan Cui3e9c72052020-01-25 21:49:42 -0800180/*
181 * Perform the restart operation in a thread context.
182 */
183static DECLARE_WORK(restart_work, perform_restart);
184
Greg Kroah-Hartman66109442010-05-04 14:31:18 -0700185static void shutdown_onchannelcallback(void *context)
Hank Janssenc88c4e42010-05-04 15:55:05 -0700186{
187 struct vmbus_channel *channel = context;
Dexuan Cui3e9c72052020-01-25 21:49:42 -0800188 struct work_struct *work = NULL;
Hank Janssen45241e52010-12-13 16:23:36 -0800189 u32 recvlen;
Hank Janssenc88c4e42010-05-04 15:55:05 -0700190 u64 requestid;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700191 u8 *shut_txf_buf = util_shutdown.recv_buffer;
Hank Janssenc88c4e42010-05-04 15:55:05 -0700192
193 struct shutdown_msg_data *shutdown_msg;
194
195 struct icmsg_hdr *icmsghdrp;
Hank Janssenc88c4e42010-05-04 15:55:05 -0700196
Andres Beltran06caa772020-11-09 11:07:04 +0100197 if (vmbus_recvpacket(channel, shut_txf_buf, HV_HYP_PAGE_SIZE, &recvlen, &requestid)) {
198 pr_err_ratelimited("Shutdown request received. Could not read into shut txf buf\n");
199 return;
200 }
Hank Janssenc88c4e42010-05-04 15:55:05 -0700201
Andres Beltran06caa772020-11-09 11:07:04 +0100202 if (!recvlen)
203 return;
Hank Janssenc88c4e42010-05-04 15:55:05 -0700204
Andres Beltran06caa772020-11-09 11:07:04 +0100205 /* Ensure recvlen is big enough to read header data */
206 if (recvlen < ICMSG_HDR) {
207 pr_err_ratelimited("Shutdown request received. Packet length too small: %d\n",
208 recvlen);
209 return;
210 }
Hank Janssenc88c4e42010-05-04 15:55:05 -0700211
Andres Beltran06caa772020-11-09 11:07:04 +0100212 icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[sizeof(struct vmbuspipe_hdr)];
213
214 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
215 if (vmbus_prep_negotiate_resp(icmsghdrp,
216 shut_txf_buf, recvlen,
217 fw_versions, FW_VER_COUNT,
218 sd_versions, SD_VER_COUNT,
219 NULL, &sd_srv_version)) {
220 pr_info("Shutdown IC version %d.%d\n",
221 sd_srv_version >> 16,
222 sd_srv_version & 0xFFFF);
223 }
224 } else if (icmsghdrp->icmsgtype == ICMSGTYPE_SHUTDOWN) {
225 /* Ensure recvlen is big enough to contain shutdown_msg_data struct */
226 if (recvlen < ICMSG_HDR + sizeof(struct shutdown_msg_data)) {
227 pr_err_ratelimited("Invalid shutdown msg data. Packet length too small: %u\n",
228 recvlen);
229 return;
Hank Janssenc88c4e42010-05-04 15:55:05 -0700230 }
231
Andres Beltran06caa772020-11-09 11:07:04 +0100232 shutdown_msg = (struct shutdown_msg_data *)&shut_txf_buf[ICMSG_HDR];
Hank Janssenc88c4e42010-05-04 15:55:05 -0700233
Andres Beltran06caa772020-11-09 11:07:04 +0100234 /*
235 * shutdown_msg->flags can be 0(shut down), 2(reboot),
236 * or 4(hibernate). It may bitwise-OR 1, which means
237 * performing the request by force. Linux always tries
238 * to perform the request by force.
239 */
240 switch (shutdown_msg->flags) {
241 case 0:
242 case 1:
243 icmsghdrp->status = HV_S_OK;
244 work = &shutdown_work;
245 pr_info("Shutdown request received - graceful shutdown initiated\n");
246 break;
247 case 2:
248 case 3:
249 icmsghdrp->status = HV_S_OK;
250 work = &restart_work;
251 pr_info("Restart request received - graceful restart initiated\n");
252 break;
253 case 4:
254 case 5:
255 pr_info("Hibernation request received\n");
256 icmsghdrp->status = hibernation_supported ?
257 HV_S_OK : HV_E_FAIL;
258 if (hibernation_supported)
259 work = &hibernate_context.work;
260 break;
261 default:
262 icmsghdrp->status = HV_E_FAIL;
263 pr_info("Shutdown request received - Invalid request\n");
264 break;
265 }
266 } else {
267 icmsghdrp->status = HV_E_FAIL;
268 pr_err_ratelimited("Shutdown request received. Invalid msg type: %d\n",
269 icmsghdrp->icmsgtype);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700270 }
271
Andres Beltran06caa772020-11-09 11:07:04 +0100272 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
273 | ICMSGHDRFLAG_RESPONSE;
274
275 vmbus_sendpacket(channel, shut_txf_buf,
276 recvlen, requestid,
277 VM_PKT_DATA_INBAND, 0);
278
Dexuan Cui3e9c72052020-01-25 21:49:42 -0800279 if (work)
280 schedule_work(work);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700281}
282
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000283/*
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700284 * Set the host time in a process context.
285 */
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700286static struct work_struct adj_time_work;
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700287
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700288/*
289 * The last time sample, received from the host. PTP device responds to
290 * requests by using this data and the current partition-wide time reference
291 * count.
292 */
293static struct {
294 u64 host_time;
295 u64 ref_time;
296 spinlock_t lock;
297} host_ts;
298
Vineeth Pillai90b125f2020-08-21 15:25:23 +0000299static inline u64 reftime_to_ns(u64 reftime)
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700300{
Vineeth Pillai90b125f2020-08-21 15:25:23 +0000301 return (reftime - WLTIMEDELTA) * 100;
302}
303
304/*
305 * Hard coded threshold for host timesync delay: 600 seconds
306 */
307static const u64 HOST_TIMESYNC_DELAY_THRESH = 600 * (u64)NSEC_PER_SEC;
308
309static int hv_get_adj_host_time(struct timespec64 *ts)
310{
311 u64 newtime, reftime, timediff_adj;
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700312 unsigned long flags;
Vineeth Pillai90b125f2020-08-21 15:25:23 +0000313 int ret = 0;
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700314
315 spin_lock_irqsave(&host_ts.lock, flags);
Andrea Parri0af3e132020-01-09 17:06:49 +0100316 reftime = hv_read_reference_counter();
Vineeth Pillai90b125f2020-08-21 15:25:23 +0000317
318 /*
319 * We need to let the caller know that last update from host
320 * is older than the max allowable threshold. clock_gettime()
321 * and PTP ioctl do not have a documented error that we could
322 * return for this specific case. Use ESTALE to report this.
323 */
324 timediff_adj = reftime - host_ts.ref_time;
325 if (timediff_adj * 100 > HOST_TIMESYNC_DELAY_THRESH) {
326 pr_warn_once("TIMESYNC IC: Stale time stamp, %llu nsecs old\n",
327 (timediff_adj * 100));
328 ret = -ESTALE;
329 }
330
331 newtime = host_ts.host_time + timediff_adj;
332 *ts = ns_to_timespec64(reftime_to_ns(newtime));
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700333 spin_unlock_irqrestore(&host_ts.lock, flags);
334
Vineeth Pillai90b125f2020-08-21 15:25:23 +0000335 return ret;
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700336}
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700337
338static void hv_set_host_time(struct work_struct *work)
339{
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700340
Vineeth Pillai90b125f2020-08-21 15:25:23 +0000341 struct timespec64 ts;
342
343 if (!hv_get_adj_host_time(&ts))
344 do_settimeofday64(&ts);
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700345}
346
347/*
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000348 * Synchronize time with host after reboot, restore, etc.
349 *
350 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
351 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
352 * message after the timesync channel is opened. Since the hv_utils module is
Alex Ng2e338f72016-09-08 05:24:13 -0700353 * loaded after hv_vmbus, the first message is usually missed. This bit is
354 * considered a hard request to discipline the clock.
355 *
356 * ICTIMESYNCFLAG_SAMPLE bit indicates a time sample from host. This is
357 * typically used as a hint to the guest. The guest is under no obligation
358 * to discipline the clock.
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000359 */
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700360static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 adj_flags)
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000361{
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700362 unsigned long flags;
363 u64 cur_reftime;
Haiyang Zhange9ec3602010-05-11 15:11:24 +0000364
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700365 /*
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700366 * Save the adjusted time sample from the host and the snapshot
367 * of the current system time.
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700368 */
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700369 spin_lock_irqsave(&host_ts.lock, flags);
K. Y. Srinivasan95ff7cd2011-08-27 11:31:31 -0700370
Andrea Parri0af3e132020-01-09 17:06:49 +0100371 cur_reftime = hv_read_reference_counter();
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700372 host_ts.host_time = hosttime;
373 host_ts.ref_time = cur_reftime;
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700374
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700375 /*
376 * TimeSync v4 messages contain reference time (guest's Hyper-V
377 * clocksource read when the time sample was generated), we can
378 * improve the precision by adding the delta between now and the
379 * time of generation. For older protocols we set
380 * reftime == cur_reftime on call.
381 */
382 host_ts.host_time += (cur_reftime - reftime);
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700383
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700384 spin_unlock_irqrestore(&host_ts.lock, flags);
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700385
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700386 /* Schedule work to do do_settimeofday64() */
387 if (adj_flags & ICTIMESYNCFLAG_SYNC)
388 schedule_work(&adj_time_work);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000389}
390
391/*
392 * Time Sync Channel message handler.
393 */
394static void timesync_onchannelcallback(void *context)
395{
396 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -0800397 u32 recvlen;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000398 u64 requestid;
399 struct icmsg_hdr *icmsghdrp;
400 struct ictimesync_data *timedatap;
Alex Ng8e1d2602016-09-08 05:24:14 -0700401 struct ictimesync_ref_data *refdata;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700402 u8 *time_txf_buf = util_timesynch.recv_buffer;
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000403
Vineeth Pillaib46b4a82020-08-21 15:28:49 +0000404 /*
405 * Drain the ring buffer and use the last packet to update
406 * host_ts
407 */
408 while (1) {
409 int ret = vmbus_recvpacket(channel, time_txf_buf,
410 HV_HYP_PAGE_SIZE, &recvlen,
411 &requestid);
412 if (ret) {
Andres Beltran06caa772020-11-09 11:07:04 +0100413 pr_err_ratelimited("TimeSync IC pkt recv failed (Err: %d)\n",
414 ret);
Vineeth Pillaib46b4a82020-08-21 15:28:49 +0000415 break;
416 }
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000417
Vineeth Pillaib46b4a82020-08-21 15:28:49 +0000418 if (!recvlen)
419 break;
420
Andres Beltran06caa772020-11-09 11:07:04 +0100421 /* Ensure recvlen is big enough to read header data */
422 if (recvlen < ICMSG_HDR) {
423 pr_err_ratelimited("Timesync request received. Packet length too small: %d\n",
424 recvlen);
425 break;
426 }
427
Hank Janssen45241e52010-12-13 16:23:36 -0800428 icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000429 sizeof(struct vmbuspipe_hdr)];
430
431 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
Andres Beltran06caa772020-11-09 11:07:04 +0100432 if (vmbus_prep_negotiate_resp(icmsghdrp,
433 time_txf_buf, recvlen,
Alex Nga1656452017-01-28 12:37:17 -0700434 fw_versions, FW_VER_COUNT,
435 ts_versions, TS_VER_COUNT,
436 NULL, &ts_srv_version)) {
Alex Ng1274a692017-01-28 12:37:18 -0700437 pr_info("TimeSync IC version %d.%d\n",
Alex Nga1656452017-01-28 12:37:17 -0700438 ts_srv_version >> 16,
439 ts_srv_version & 0xFFFF);
440 }
Andres Beltran06caa772020-11-09 11:07:04 +0100441 } else if (icmsghdrp->icmsgtype == ICMSGTYPE_TIMESYNC) {
Alex Ng8e1d2602016-09-08 05:24:14 -0700442 if (ts_srv_version > TS_VERSION_3) {
Andres Beltran06caa772020-11-09 11:07:04 +0100443 /* Ensure recvlen is big enough to read ictimesync_ref_data */
444 if (recvlen < ICMSG_HDR + sizeof(struct ictimesync_ref_data)) {
445 pr_err_ratelimited("Invalid ictimesync ref data. Length too small: %u\n",
446 recvlen);
447 break;
448 }
449 refdata = (struct ictimesync_ref_data *)&time_txf_buf[ICMSG_HDR];
Alex Ng8e1d2602016-09-08 05:24:14 -0700450
451 adj_guesttime(refdata->parenttime,
452 refdata->vmreferencetime,
453 refdata->flags);
454 } else {
Andres Beltran06caa772020-11-09 11:07:04 +0100455 /* Ensure recvlen is big enough to read ictimesync_data */
456 if (recvlen < ICMSG_HDR + sizeof(struct ictimesync_data)) {
457 pr_err_ratelimited("Invalid ictimesync data. Length too small: %u\n",
458 recvlen);
459 break;
460 }
461 timedatap = (struct ictimesync_data *)&time_txf_buf[ICMSG_HDR];
462
Alex Ng8e1d2602016-09-08 05:24:14 -0700463 adj_guesttime(timedatap->parenttime,
Andrea Parri0af3e132020-01-09 17:06:49 +0100464 hv_read_reference_counter(),
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700465 timedatap->flags);
Alex Ng8e1d2602016-09-08 05:24:14 -0700466 }
Andres Beltran06caa772020-11-09 11:07:04 +0100467 } else {
468 icmsghdrp->status = HV_E_FAIL;
469 pr_err_ratelimited("Timesync request received. Invalid msg type: %d\n",
470 icmsghdrp->icmsgtype);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000471 }
472
473 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
474 | ICMSGHDRFLAG_RESPONSE;
475
Hank Janssen45241e52010-12-13 16:23:36 -0800476 vmbus_sendpacket(channel, time_txf_buf,
Andres Beltran06caa772020-11-09 11:07:04 +0100477 recvlen, requestid,
478 VM_PKT_DATA_INBAND, 0);
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000479 }
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000480}
481
Hank Janssen9153f7b2010-05-15 14:39:58 -0700482/*
483 * Heartbeat functionality.
484 * Every two seconds, Hyper-V send us a heartbeat request message.
485 * we respond to this message, and Hyper-V knows we are alive.
486 */
487static void heartbeat_onchannelcallback(void *context)
488{
489 struct vmbus_channel *channel = context;
Hank Janssen45241e52010-12-13 16:23:36 -0800490 u32 recvlen;
Hank Janssen9153f7b2010-05-15 14:39:58 -0700491 u64 requestid;
492 struct icmsg_hdr *icmsghdrp;
493 struct heartbeat_msg_data *heartbeat_msg;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700494 u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
Hank Janssen9153f7b2010-05-15 14:39:58 -0700495
Long Li407a3ae2016-10-05 16:57:46 -0700496 while (1) {
Hank Janssen9153f7b2010-05-15 14:39:58 -0700497
Andres Beltran06caa772020-11-09 11:07:04 +0100498 if (vmbus_recvpacket(channel, hbeat_txf_buf, HV_HYP_PAGE_SIZE,
499 &recvlen, &requestid)) {
500 pr_err_ratelimited("Heartbeat request received. Could not read into hbeat txf buf\n");
501 return;
502 }
Long Li407a3ae2016-10-05 16:57:46 -0700503
504 if (!recvlen)
505 break;
506
Andres Beltran06caa772020-11-09 11:07:04 +0100507 /* Ensure recvlen is big enough to read header data */
508 if (recvlen < ICMSG_HDR) {
Colin Ian Kingbdb49522021-01-27 23:31:36 +0000509 pr_err_ratelimited("Heartbeat request received. Packet length too small: %d\n",
Andres Beltran06caa772020-11-09 11:07:04 +0100510 recvlen);
511 break;
512 }
513
Hank Janssen45241e52010-12-13 16:23:36 -0800514 icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
Hank Janssen9153f7b2010-05-15 14:39:58 -0700515 sizeof(struct vmbuspipe_hdr)];
516
517 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
Alex Nga1656452017-01-28 12:37:17 -0700518 if (vmbus_prep_negotiate_resp(icmsghdrp,
Andres Beltran06caa772020-11-09 11:07:04 +0100519 hbeat_txf_buf, recvlen,
Alex Nga1656452017-01-28 12:37:17 -0700520 fw_versions, FW_VER_COUNT,
521 hb_versions, HB_VER_COUNT,
522 NULL, &hb_srv_version)) {
523
Alex Ng1274a692017-01-28 12:37:18 -0700524 pr_info("Heartbeat IC version %d.%d\n",
Alex Nga1656452017-01-28 12:37:17 -0700525 hb_srv_version >> 16,
526 hb_srv_version & 0xFFFF);
527 }
Andres Beltran06caa772020-11-09 11:07:04 +0100528 } else if (icmsghdrp->icmsgtype == ICMSGTYPE_HEARTBEAT) {
529 /*
530 * Ensure recvlen is big enough to read seq_num. Reserved area is not
531 * included in the check as the host may not fill it up entirely
532 */
533 if (recvlen < ICMSG_HDR + sizeof(u64)) {
534 pr_err_ratelimited("Invalid heartbeat msg data. Length too small: %u\n",
535 recvlen);
536 break;
537 }
538 heartbeat_msg = (struct heartbeat_msg_data *)&hbeat_txf_buf[ICMSG_HDR];
Hank Janssen9153f7b2010-05-15 14:39:58 -0700539
Hank Janssen9153f7b2010-05-15 14:39:58 -0700540 heartbeat_msg->seq_num += 1;
Andres Beltran06caa772020-11-09 11:07:04 +0100541 } else {
542 icmsghdrp->status = HV_E_FAIL;
543 pr_err_ratelimited("Heartbeat request received. Invalid msg type: %d\n",
544 icmsghdrp->icmsgtype);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700545 }
546
547 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
548 | ICMSGHDRFLAG_RESPONSE;
549
Hank Janssen45241e52010-12-13 16:23:36 -0800550 vmbus_sendpacket(channel, hbeat_txf_buf,
Andres Beltran06caa772020-11-09 11:07:04 +0100551 recvlen, requestid,
552 VM_PKT_DATA_INBAND, 0);
Hank Janssen9153f7b2010-05-15 14:39:58 -0700553 }
Hank Janssen9153f7b2010-05-15 14:39:58 -0700554}
Haiyang Zhang39c4e9c2010-05-05 19:23:46 +0000555
Boqun Feng061dc93e2020-09-16 11:48:16 +0800556#define HV_UTIL_RING_SEND_SIZE VMBUS_RING_SIZE(3 * HV_HYP_PAGE_SIZE)
557#define HV_UTIL_RING_RECV_SIZE VMBUS_RING_SIZE(3 * HV_HYP_PAGE_SIZE)
558
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700559static int util_probe(struct hv_device *dev,
560 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700561{
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700562 struct hv_util_service *srv =
563 (struct hv_util_service *)dev_id->driver_data;
564 int ret;
565
Himadri Pandyab14d7492019-07-25 05:03:14 +0000566 srv->recv_buffer = kmalloc(HV_HYP_PAGE_SIZE * 4, GFP_KERNEL);
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700567 if (!srv->recv_buffer)
568 return -ENOMEM;
K. Y. Srinivasanb9830d12016-02-26 15:13:19 -0800569 srv->channel = dev->channel;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700570 if (srv->util_init) {
571 ret = srv->util_init(srv);
572 if (ret) {
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700573 ret = -ENODEV;
574 goto error1;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700575 }
576 }
577
K. Y. Srinivasan7ae3e032012-12-01 06:46:34 -0800578 /*
579 * The set of services managed by the util driver are not performance
580 * critical and do not need batched reading. Furthermore, some services
581 * such as KVP can only handle one message from the host at a time.
582 * Turn off batched reading for all util drivers before we open the
583 * channel.
584 */
Stephen Hemmingerb71e3282017-02-11 23:02:21 -0700585 set_channel_read_mode(dev->channel, HV_CALL_DIRECT);
K. Y. Srinivasan7ae3e032012-12-01 06:46:34 -0800586
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700587 hv_set_drvdata(dev, srv);
Dexuan Cui18965662015-02-27 11:25:58 -0800588
Boqun Feng061dc93e2020-09-16 11:48:16 +0800589 ret = vmbus_open(dev->channel, HV_UTIL_RING_SEND_SIZE,
590 HV_UTIL_RING_RECV_SIZE, NULL, 0, srv->util_cb,
Himadri Pandya0541a222019-07-25 05:03:15 +0000591 dev->channel);
Dexuan Cui18965662015-02-27 11:25:58 -0800592 if (ret)
593 goto error;
594
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700595 return 0;
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700596
597error:
598 if (srv->util_deinit)
599 srv->util_deinit();
600error1:
601 kfree(srv->recv_buffer);
602 return ret;
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700603}
604
605static int util_remove(struct hv_device *dev)
606{
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700607 struct hv_util_service *srv = hv_get_drvdata(dev);
608
609 if (srv->util_deinit)
610 srv->util_deinit();
K. Y. Srinivasan5380b3832015-02-28 11:18:20 -0800611 vmbus_close(dev->channel);
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700612 kfree(srv->recv_buffer);
613
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700614 return 0;
615}
616
Dexuan Cui54e19d32020-01-25 21:49:44 -0800617/*
618 * When we're in util_suspend(), all the userspace processes have been frozen
619 * (refer to hibernate() -> freeze_processes()). The userspace is thawed only
620 * after the whole resume procedure, including util_resume(), finishes.
621 */
622static int util_suspend(struct hv_device *dev)
623{
624 struct hv_util_service *srv = hv_get_drvdata(dev);
625 int ret = 0;
626
627 if (srv->util_pre_suspend) {
628 ret = srv->util_pre_suspend();
629 if (ret)
630 return ret;
631 }
632
633 vmbus_close(dev->channel);
634
635 return 0;
636}
637
638static int util_resume(struct hv_device *dev)
639{
640 struct hv_util_service *srv = hv_get_drvdata(dev);
641 int ret = 0;
642
643 if (srv->util_pre_resume) {
644 ret = srv->util_pre_resume();
645 if (ret)
646 return ret;
647 }
648
Boqun Feng061dc93e2020-09-16 11:48:16 +0800649 ret = vmbus_open(dev->channel, HV_UTIL_RING_SEND_SIZE,
650 HV_UTIL_RING_RECV_SIZE, NULL, 0, srv->util_cb,
Dexuan Cui54e19d32020-01-25 21:49:44 -0800651 dev->channel);
652 return ret;
653}
654
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700655static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700656 /* Shutdown guid */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -0800657 { HV_SHUTDOWN_GUID,
658 .driver_data = (unsigned long)&util_shutdown
659 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700660 /* Time synch guid */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -0800661 { HV_TS_GUID,
662 .driver_data = (unsigned long)&util_timesynch
663 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700664 /* Heartbeat guid */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -0800665 { HV_HEART_BEAT_GUID,
666 .driver_data = (unsigned long)&util_heartbeat
667 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700668 /* KVP guid */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -0800669 { HV_KVP_GUID,
670 .driver_data = (unsigned long)&util_kvp
671 },
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700672 /* VSS GUID */
673 { HV_VSS_GUID,
674 .driver_data = (unsigned long)&util_vss
675 },
K. Y. Srinivasan013254762014-02-16 11:34:30 -0800676 /* File copy GUID */
677 { HV_FCOPY_GUID,
678 .driver_data = (unsigned long)&util_fcopy
679 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700680 { },
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700681};
682
683MODULE_DEVICE_TABLE(vmbus, id_table);
684
685/* The one and only one */
686static struct hv_driver util_drv = {
Haiyang Zhang5c24ee82018-10-18 05:09:29 +0000687 .name = "hv_utils",
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700688 .id_table = id_table,
689 .probe = util_probe,
690 .remove = util_remove,
Dexuan Cui54e19d32020-01-25 21:49:44 -0800691 .suspend = util_suspend,
692 .resume = util_resume,
Arjan van de Venaf0a5642018-06-05 13:37:49 -0700693 .driver = {
694 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
695 },
K. Y. Srinivasan283f2122011-08-25 09:48:36 -0700696};
697
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700698static int hv_ptp_enable(struct ptp_clock_info *info,
699 struct ptp_clock_request *request, int on)
700{
701 return -EOPNOTSUPP;
702}
703
704static int hv_ptp_settime(struct ptp_clock_info *p, const struct timespec64 *ts)
705{
706 return -EOPNOTSUPP;
707}
708
709static int hv_ptp_adjfreq(struct ptp_clock_info *ptp, s32 delta)
710{
711 return -EOPNOTSUPP;
712}
713static int hv_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
714{
715 return -EOPNOTSUPP;
716}
717
718static int hv_ptp_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
719{
Vineeth Pillai90b125f2020-08-21 15:25:23 +0000720 return hv_get_adj_host_time(ts);
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700721}
722
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700723static struct ptp_clock_info ptp_hyperv_info = {
724 .name = "hyperv",
725 .enable = hv_ptp_enable,
726 .adjtime = hv_ptp_adjtime,
727 .adjfreq = hv_ptp_adjfreq,
728 .gettime64 = hv_ptp_gettime,
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700729 .settime64 = hv_ptp_settime,
730 .owner = THIS_MODULE,
731};
732
733static struct ptp_clock *hv_ptp_clock;
734
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700735static int hv_timesync_init(struct hv_util_service *srv)
736{
Dexuan Cui5a16dfc82017-03-04 18:14:00 -0700737 spin_lock_init(&host_ts.lock);
738
Vitaly Kuznetsov1d106022017-05-18 10:46:04 -0700739 INIT_WORK(&adj_time_work, hv_set_host_time);
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700740
741 /*
742 * ptp_clock_register() returns NULL when CONFIG_PTP_1588_CLOCK is
743 * disabled but the driver is still useful without the PTP device
744 * as it still handles the ICTIMESYNCFLAG_SYNC case.
745 */
746 hv_ptp_clock = ptp_clock_register(&ptp_hyperv_info, NULL);
747 if (IS_ERR_OR_NULL(hv_ptp_clock)) {
YueHaibingc6a86252021-05-14 15:01:16 +0800748 pr_err("cannot register PTP clock: %d\n",
749 PTR_ERR_OR_ZERO(hv_ptp_clock));
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700750 hv_ptp_clock = NULL;
751 }
752
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700753 return 0;
754}
755
Dexuan Cui54e19d32020-01-25 21:49:44 -0800756static void hv_timesync_cancel_work(void)
757{
758 cancel_work_sync(&adj_time_work);
759}
760
761static int hv_timesync_pre_suspend(void)
762{
763 hv_timesync_cancel_work();
764 return 0;
765}
766
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700767static void hv_timesync_deinit(void)
768{
Vitaly Kuznetsov3716a492017-02-04 09:57:14 -0700769 if (hv_ptp_clock)
770 ptp_clock_unregister(hv_ptp_clock);
Dexuan Cui54e19d32020-01-25 21:49:44 -0800771
772 hv_timesync_cancel_work();
Vivek yadav3ba1eb12016-09-09 00:42:30 -0700773}
774
Hank Janssenc88c4e42010-05-04 15:55:05 -0700775static int __init init_hyperv_utils(void)
776{
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700777 pr_info("Registering HyperV Utility Driver\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700778
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700779 return vmbus_driver_register(&util_drv);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700780}
781
782static void exit_hyperv_utils(void)
783{
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700784 pr_info("De-Registered HyperV Utility Driver\n");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700785
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700786 vmbus_driver_unregister(&util_drv);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700787}
788
789module_init(init_hyperv_utils);
790module_exit(exit_hyperv_utils);
791
792MODULE_DESCRIPTION("Hyper-V Utilities");
Hank Janssenc88c4e42010-05-04 15:55:05 -0700793MODULE_LICENSE("GPL");