hv_util.c 18 KB

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