hv_util.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. static struct work_struct adj_time_work;
  170. /*
  171. * The last time sample, received from the host. PTP device responds to
  172. * requests by using this data and the current partition-wide time reference
  173. * count.
  174. */
  175. static struct {
  176. u64 host_time;
  177. u64 ref_time;
  178. spinlock_t lock;
  179. } host_ts;
  180. static struct timespec64 hv_get_adj_host_time(void)
  181. {
  182. struct timespec64 ts;
  183. u64 newtime, reftime;
  184. unsigned long flags;
  185. spin_lock_irqsave(&host_ts.lock, flags);
  186. reftime = hyperv_cs->read(hyperv_cs);
  187. newtime = host_ts.host_time + (reftime - host_ts.ref_time);
  188. ts = ns_to_timespec64((newtime - WLTIMEDELTA) * 100);
  189. spin_unlock_irqrestore(&host_ts.lock, flags);
  190. return ts;
  191. }
  192. static void hv_set_host_time(struct work_struct *work)
  193. {
  194. struct timespec64 ts = hv_get_adj_host_time();
  195. do_settimeofday64(&ts);
  196. }
  197. /*
  198. * Synchronize time with host after reboot, restore, etc.
  199. *
  200. * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
  201. * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
  202. * message after the timesync channel is opened. Since the hv_utils module is
  203. * loaded after hv_vmbus, the first message is usually missed. This bit is
  204. * considered a hard request to discipline the clock.
  205. *
  206. * ICTIMESYNCFLAG_SAMPLE bit indicates a time sample from host. This is
  207. * typically used as a hint to the guest. The guest is under no obligation
  208. * to discipline the clock.
  209. */
  210. static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 adj_flags)
  211. {
  212. unsigned long flags;
  213. u64 cur_reftime;
  214. /*
  215. * Save the adjusted time sample from the host and the snapshot
  216. * of the current system time.
  217. */
  218. spin_lock_irqsave(&host_ts.lock, flags);
  219. cur_reftime = hyperv_cs->read(hyperv_cs);
  220. host_ts.host_time = hosttime;
  221. host_ts.ref_time = cur_reftime;
  222. /*
  223. * TimeSync v4 messages contain reference time (guest's Hyper-V
  224. * clocksource read when the time sample was generated), we can
  225. * improve the precision by adding the delta between now and the
  226. * time of generation. For older protocols we set
  227. * reftime == cur_reftime on call.
  228. */
  229. host_ts.host_time += (cur_reftime - reftime);
  230. spin_unlock_irqrestore(&host_ts.lock, flags);
  231. /* Schedule work to do do_settimeofday64() */
  232. if (adj_flags & ICTIMESYNCFLAG_SYNC)
  233. schedule_work(&adj_time_work);
  234. }
  235. /*
  236. * Time Sync Channel message handler.
  237. */
  238. static void timesync_onchannelcallback(void *context)
  239. {
  240. struct vmbus_channel *channel = context;
  241. u32 recvlen;
  242. u64 requestid;
  243. struct icmsg_hdr *icmsghdrp;
  244. struct ictimesync_data *timedatap;
  245. struct ictimesync_ref_data *refdata;
  246. u8 *time_txf_buf = util_timesynch.recv_buffer;
  247. vmbus_recvpacket(channel, time_txf_buf,
  248. PAGE_SIZE, &recvlen, &requestid);
  249. if (recvlen > 0) {
  250. icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
  251. sizeof(struct vmbuspipe_hdr)];
  252. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  253. if (vmbus_prep_negotiate_resp(icmsghdrp, time_txf_buf,
  254. fw_versions, FW_VER_COUNT,
  255. ts_versions, TS_VER_COUNT,
  256. NULL, &ts_srv_version)) {
  257. pr_info("TimeSync IC version %d.%d\n",
  258. ts_srv_version >> 16,
  259. ts_srv_version & 0xFFFF);
  260. }
  261. } else {
  262. if (ts_srv_version > TS_VERSION_3) {
  263. refdata = (struct ictimesync_ref_data *)
  264. &time_txf_buf[
  265. sizeof(struct vmbuspipe_hdr) +
  266. sizeof(struct icmsg_hdr)];
  267. adj_guesttime(refdata->parenttime,
  268. refdata->vmreferencetime,
  269. refdata->flags);
  270. } else {
  271. timedatap = (struct ictimesync_data *)
  272. &time_txf_buf[
  273. sizeof(struct vmbuspipe_hdr) +
  274. sizeof(struct icmsg_hdr)];
  275. adj_guesttime(timedatap->parenttime,
  276. hyperv_cs->read(hyperv_cs),
  277. timedatap->flags);
  278. }
  279. }
  280. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  281. | ICMSGHDRFLAG_RESPONSE;
  282. vmbus_sendpacket(channel, time_txf_buf,
  283. recvlen, requestid,
  284. VM_PKT_DATA_INBAND, 0);
  285. }
  286. }
  287. /*
  288. * Heartbeat functionality.
  289. * Every two seconds, Hyper-V send us a heartbeat request message.
  290. * we respond to this message, and Hyper-V knows we are alive.
  291. */
  292. static void heartbeat_onchannelcallback(void *context)
  293. {
  294. struct vmbus_channel *channel = context;
  295. u32 recvlen;
  296. u64 requestid;
  297. struct icmsg_hdr *icmsghdrp;
  298. struct heartbeat_msg_data *heartbeat_msg;
  299. u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
  300. while (1) {
  301. vmbus_recvpacket(channel, hbeat_txf_buf,
  302. PAGE_SIZE, &recvlen, &requestid);
  303. if (!recvlen)
  304. break;
  305. icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
  306. sizeof(struct vmbuspipe_hdr)];
  307. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  308. if (vmbus_prep_negotiate_resp(icmsghdrp,
  309. hbeat_txf_buf,
  310. fw_versions, FW_VER_COUNT,
  311. hb_versions, HB_VER_COUNT,
  312. NULL, &hb_srv_version)) {
  313. pr_info("Heartbeat IC version %d.%d\n",
  314. hb_srv_version >> 16,
  315. hb_srv_version & 0xFFFF);
  316. }
  317. } else {
  318. heartbeat_msg =
  319. (struct heartbeat_msg_data *)&hbeat_txf_buf[
  320. sizeof(struct vmbuspipe_hdr) +
  321. sizeof(struct icmsg_hdr)];
  322. heartbeat_msg->seq_num += 1;
  323. }
  324. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  325. | ICMSGHDRFLAG_RESPONSE;
  326. vmbus_sendpacket(channel, hbeat_txf_buf,
  327. recvlen, requestid,
  328. VM_PKT_DATA_INBAND, 0);
  329. }
  330. }
  331. static int util_probe(struct hv_device *dev,
  332. const struct hv_vmbus_device_id *dev_id)
  333. {
  334. struct hv_util_service *srv =
  335. (struct hv_util_service *)dev_id->driver_data;
  336. int ret;
  337. srv->recv_buffer = kmalloc(PAGE_SIZE * 4, GFP_KERNEL);
  338. if (!srv->recv_buffer)
  339. return -ENOMEM;
  340. srv->channel = dev->channel;
  341. if (srv->util_init) {
  342. ret = srv->util_init(srv);
  343. if (ret) {
  344. ret = -ENODEV;
  345. goto error1;
  346. }
  347. }
  348. /*
  349. * The set of services managed by the util driver are not performance
  350. * critical and do not need batched reading. Furthermore, some services
  351. * such as KVP can only handle one message from the host at a time.
  352. * Turn off batched reading for all util drivers before we open the
  353. * channel.
  354. */
  355. set_channel_read_mode(dev->channel, HV_CALL_DIRECT);
  356. hv_set_drvdata(dev, srv);
  357. ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
  358. srv->util_cb, dev->channel);
  359. if (ret)
  360. goto error;
  361. return 0;
  362. error:
  363. if (srv->util_deinit)
  364. srv->util_deinit();
  365. error1:
  366. kfree(srv->recv_buffer);
  367. return ret;
  368. }
  369. static int util_remove(struct hv_device *dev)
  370. {
  371. struct hv_util_service *srv = hv_get_drvdata(dev);
  372. if (srv->util_deinit)
  373. srv->util_deinit();
  374. vmbus_close(dev->channel);
  375. kfree(srv->recv_buffer);
  376. return 0;
  377. }
  378. static const struct hv_vmbus_device_id id_table[] = {
  379. /* Shutdown guid */
  380. { HV_SHUTDOWN_GUID,
  381. .driver_data = (unsigned long)&util_shutdown
  382. },
  383. /* Time synch guid */
  384. { HV_TS_GUID,
  385. .driver_data = (unsigned long)&util_timesynch
  386. },
  387. /* Heartbeat guid */
  388. { HV_HEART_BEAT_GUID,
  389. .driver_data = (unsigned long)&util_heartbeat
  390. },
  391. /* KVP guid */
  392. { HV_KVP_GUID,
  393. .driver_data = (unsigned long)&util_kvp
  394. },
  395. /* VSS GUID */
  396. { HV_VSS_GUID,
  397. .driver_data = (unsigned long)&util_vss
  398. },
  399. /* File copy GUID */
  400. { HV_FCOPY_GUID,
  401. .driver_data = (unsigned long)&util_fcopy
  402. },
  403. { },
  404. };
  405. MODULE_DEVICE_TABLE(vmbus, id_table);
  406. /* The one and only one */
  407. static struct hv_driver util_drv = {
  408. .name = "hv_util",
  409. .id_table = id_table,
  410. .probe = util_probe,
  411. .remove = util_remove,
  412. };
  413. static int hv_ptp_enable(struct ptp_clock_info *info,
  414. struct ptp_clock_request *request, int on)
  415. {
  416. return -EOPNOTSUPP;
  417. }
  418. static int hv_ptp_settime(struct ptp_clock_info *p, const struct timespec64 *ts)
  419. {
  420. return -EOPNOTSUPP;
  421. }
  422. static int hv_ptp_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  423. {
  424. return -EOPNOTSUPP;
  425. }
  426. static int hv_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  427. {
  428. return -EOPNOTSUPP;
  429. }
  430. static int hv_ptp_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
  431. {
  432. *ts = hv_get_adj_host_time();
  433. return 0;
  434. }
  435. static struct ptp_clock_info ptp_hyperv_info = {
  436. .name = "hyperv",
  437. .enable = hv_ptp_enable,
  438. .adjtime = hv_ptp_adjtime,
  439. .adjfreq = hv_ptp_adjfreq,
  440. .gettime64 = hv_ptp_gettime,
  441. .settime64 = hv_ptp_settime,
  442. .owner = THIS_MODULE,
  443. };
  444. static struct ptp_clock *hv_ptp_clock;
  445. static int hv_timesync_init(struct hv_util_service *srv)
  446. {
  447. /* TimeSync requires Hyper-V clocksource. */
  448. if (!hyperv_cs)
  449. return -ENODEV;
  450. spin_lock_init(&host_ts.lock);
  451. INIT_WORK(&adj_time_work, hv_set_host_time);
  452. /*
  453. * ptp_clock_register() returns NULL when CONFIG_PTP_1588_CLOCK is
  454. * disabled but the driver is still useful without the PTP device
  455. * as it still handles the ICTIMESYNCFLAG_SYNC case.
  456. */
  457. hv_ptp_clock = ptp_clock_register(&ptp_hyperv_info, NULL);
  458. if (IS_ERR_OR_NULL(hv_ptp_clock)) {
  459. pr_err("cannot register PTP clock: %d\n",
  460. PTR_ERR_OR_ZERO(hv_ptp_clock));
  461. hv_ptp_clock = NULL;
  462. }
  463. return 0;
  464. }
  465. static void hv_timesync_deinit(void)
  466. {
  467. if (hv_ptp_clock)
  468. ptp_clock_unregister(hv_ptp_clock);
  469. cancel_work_sync(&adj_time_work);
  470. }
  471. static int __init init_hyperv_utils(void)
  472. {
  473. pr_info("Registering HyperV Utility Driver\n");
  474. return vmbus_driver_register(&util_drv);
  475. }
  476. static void exit_hyperv_utils(void)
  477. {
  478. pr_info("De-Registered HyperV Utility Driver\n");
  479. vmbus_driver_unregister(&util_drv);
  480. }
  481. module_init(init_hyperv_utils);
  482. module_exit(exit_hyperv_utils);
  483. MODULE_DESCRIPTION("Hyper-V Utilities");
  484. MODULE_LICENSE("GPL");