hv_util.c 15 KB

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