hv_util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 "hyperv_vmbus.h"
  30. #define SD_MAJOR 3
  31. #define SD_MINOR 0
  32. #define SD_VERSION (SD_MAJOR << 16 | SD_MINOR)
  33. #define SD_MAJOR_1 1
  34. #define SD_VERSION_1 (SD_MAJOR_1 << 16 | SD_MINOR)
  35. #define TS_MAJOR 4
  36. #define TS_MINOR 0
  37. #define TS_VERSION (TS_MAJOR << 16 | TS_MINOR)
  38. #define TS_MAJOR_1 1
  39. #define TS_VERSION_1 (TS_MAJOR_1 << 16 | TS_MINOR)
  40. #define TS_MAJOR_3 3
  41. #define TS_VERSION_3 (TS_MAJOR_3 << 16 | TS_MINOR)
  42. #define HB_MAJOR 3
  43. #define HB_MINOR 0
  44. #define HB_VERSION (HB_MAJOR << 16 | HB_MINOR)
  45. #define HB_MAJOR_1 1
  46. #define HB_VERSION_1 (HB_MAJOR_1 << 16 | HB_MINOR)
  47. static int sd_srv_version;
  48. static int ts_srv_version;
  49. static int hb_srv_version;
  50. static int util_fw_version;
  51. static void shutdown_onchannelcallback(void *context);
  52. static struct hv_util_service util_shutdown = {
  53. .util_cb = shutdown_onchannelcallback,
  54. };
  55. static int hv_timesync_init(struct hv_util_service *srv);
  56. static void hv_timesync_deinit(void);
  57. static void timesync_onchannelcallback(void *context);
  58. static struct hv_util_service util_timesynch = {
  59. .util_cb = timesync_onchannelcallback,
  60. .util_init = hv_timesync_init,
  61. .util_deinit = hv_timesync_deinit,
  62. };
  63. static void heartbeat_onchannelcallback(void *context);
  64. static struct hv_util_service util_heartbeat = {
  65. .util_cb = heartbeat_onchannelcallback,
  66. };
  67. static struct hv_util_service util_kvp = {
  68. .util_cb = hv_kvp_onchannelcallback,
  69. .util_init = hv_kvp_init,
  70. .util_deinit = hv_kvp_deinit,
  71. };
  72. static struct hv_util_service util_vss = {
  73. .util_cb = hv_vss_onchannelcallback,
  74. .util_init = hv_vss_init,
  75. .util_deinit = hv_vss_deinit,
  76. };
  77. static struct hv_util_service util_fcopy = {
  78. .util_cb = hv_fcopy_onchannelcallback,
  79. .util_init = hv_fcopy_init,
  80. .util_deinit = hv_fcopy_deinit,
  81. };
  82. static void perform_shutdown(struct work_struct *dummy)
  83. {
  84. orderly_poweroff(true);
  85. }
  86. /*
  87. * Perform the shutdown operation in a thread context.
  88. */
  89. static DECLARE_WORK(shutdown_work, perform_shutdown);
  90. static void shutdown_onchannelcallback(void *context)
  91. {
  92. struct vmbus_channel *channel = context;
  93. u32 recvlen;
  94. u64 requestid;
  95. bool execute_shutdown = false;
  96. u8 *shut_txf_buf = util_shutdown.recv_buffer;
  97. struct shutdown_msg_data *shutdown_msg;
  98. struct icmsg_hdr *icmsghdrp;
  99. struct icmsg_negotiate *negop = NULL;
  100. vmbus_recvpacket(channel, shut_txf_buf,
  101. PAGE_SIZE, &recvlen, &requestid);
  102. if (recvlen > 0) {
  103. icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
  104. sizeof(struct vmbuspipe_hdr)];
  105. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  106. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  107. shut_txf_buf, util_fw_version,
  108. sd_srv_version);
  109. } else {
  110. shutdown_msg =
  111. (struct shutdown_msg_data *)&shut_txf_buf[
  112. sizeof(struct vmbuspipe_hdr) +
  113. sizeof(struct icmsg_hdr)];
  114. switch (shutdown_msg->flags) {
  115. case 0:
  116. case 1:
  117. icmsghdrp->status = HV_S_OK;
  118. execute_shutdown = true;
  119. pr_info("Shutdown request received -"
  120. " graceful shutdown initiated\n");
  121. break;
  122. default:
  123. icmsghdrp->status = HV_E_FAIL;
  124. execute_shutdown = false;
  125. pr_info("Shutdown request received -"
  126. " Invalid request\n");
  127. break;
  128. }
  129. }
  130. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  131. | ICMSGHDRFLAG_RESPONSE;
  132. vmbus_sendpacket(channel, shut_txf_buf,
  133. recvlen, requestid,
  134. VM_PKT_DATA_INBAND, 0);
  135. }
  136. if (execute_shutdown == true)
  137. schedule_work(&shutdown_work);
  138. }
  139. /*
  140. * Set the host time in a process context.
  141. */
  142. struct adj_time_work {
  143. struct work_struct work;
  144. u64 host_time;
  145. u64 ref_time;
  146. u8 flags;
  147. };
  148. static void hv_set_host_time(struct work_struct *work)
  149. {
  150. struct adj_time_work *wrk;
  151. s64 host_tns;
  152. u64 newtime;
  153. struct timespec host_ts;
  154. wrk = container_of(work, struct adj_time_work, work);
  155. newtime = wrk->host_time;
  156. if (ts_srv_version > TS_VERSION_3) {
  157. /*
  158. * Some latency has been introduced since Hyper-V generated
  159. * its time sample. Take that latency into account before
  160. * using TSC reference time sample from Hyper-V.
  161. *
  162. * This sample is given by TimeSync v4 and above hosts.
  163. */
  164. u64 current_tick;
  165. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  166. newtime += (current_tick - wrk->ref_time);
  167. }
  168. host_tns = (newtime - WLTIMEDELTA) * 100;
  169. host_ts = ns_to_timespec(host_tns);
  170. do_settimeofday(&host_ts);
  171. }
  172. /*
  173. * Synchronize time with host after reboot, restore, etc.
  174. *
  175. * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
  176. * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
  177. * message after the timesync channel is opened. Since the hv_utils module is
  178. * loaded after hv_vmbus, the first message is usually missed. This bit is
  179. * considered a hard request to discipline the clock.
  180. *
  181. * ICTIMESYNCFLAG_SAMPLE bit indicates a time sample from host. This is
  182. * typically used as a hint to the guest. The guest is under no obligation
  183. * to discipline the clock.
  184. */
  185. static struct adj_time_work wrk;
  186. static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 flags)
  187. {
  188. /*
  189. * This check is safe since we are executing in the
  190. * interrupt context and time synch messages arre always
  191. * delivered on the same CPU.
  192. */
  193. if (work_pending(&wrk.work))
  194. return;
  195. wrk.host_time = hosttime;
  196. wrk.ref_time = reftime;
  197. wrk.flags = flags;
  198. if ((flags & (ICTIMESYNCFLAG_SYNC | ICTIMESYNCFLAG_SAMPLE)) != 0) {
  199. schedule_work(&wrk.work);
  200. }
  201. }
  202. /*
  203. * Time Sync Channel message handler.
  204. */
  205. static void timesync_onchannelcallback(void *context)
  206. {
  207. struct vmbus_channel *channel = context;
  208. u32 recvlen;
  209. u64 requestid;
  210. struct icmsg_hdr *icmsghdrp;
  211. struct ictimesync_data *timedatap;
  212. struct ictimesync_ref_data *refdata;
  213. u8 *time_txf_buf = util_timesynch.recv_buffer;
  214. struct icmsg_negotiate *negop = NULL;
  215. vmbus_recvpacket(channel, time_txf_buf,
  216. PAGE_SIZE, &recvlen, &requestid);
  217. if (recvlen > 0) {
  218. icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
  219. sizeof(struct vmbuspipe_hdr)];
  220. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  221. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  222. time_txf_buf,
  223. util_fw_version,
  224. ts_srv_version);
  225. pr_info("Using TimeSync version %d.%d\n",
  226. ts_srv_version >> 16, ts_srv_version & 0xFFFF);
  227. } else {
  228. if (ts_srv_version > TS_VERSION_3) {
  229. refdata = (struct ictimesync_ref_data *)
  230. &time_txf_buf[
  231. sizeof(struct vmbuspipe_hdr) +
  232. sizeof(struct icmsg_hdr)];
  233. adj_guesttime(refdata->parenttime,
  234. refdata->vmreferencetime,
  235. refdata->flags);
  236. } else {
  237. timedatap = (struct ictimesync_data *)
  238. &time_txf_buf[
  239. sizeof(struct vmbuspipe_hdr) +
  240. sizeof(struct icmsg_hdr)];
  241. adj_guesttime(timedatap->parenttime,
  242. 0,
  243. timedatap->flags);
  244. }
  245. }
  246. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  247. | ICMSGHDRFLAG_RESPONSE;
  248. vmbus_sendpacket(channel, time_txf_buf,
  249. recvlen, requestid,
  250. VM_PKT_DATA_INBAND, 0);
  251. }
  252. }
  253. /*
  254. * Heartbeat functionality.
  255. * Every two seconds, Hyper-V send us a heartbeat request message.
  256. * we respond to this message, and Hyper-V knows we are alive.
  257. */
  258. static void heartbeat_onchannelcallback(void *context)
  259. {
  260. struct vmbus_channel *channel = context;
  261. u32 recvlen;
  262. u64 requestid;
  263. struct icmsg_hdr *icmsghdrp;
  264. struct heartbeat_msg_data *heartbeat_msg;
  265. u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
  266. struct icmsg_negotiate *negop = NULL;
  267. while (1) {
  268. vmbus_recvpacket(channel, hbeat_txf_buf,
  269. PAGE_SIZE, &recvlen, &requestid);
  270. if (!recvlen)
  271. break;
  272. icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
  273. sizeof(struct vmbuspipe_hdr)];
  274. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  275. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  276. hbeat_txf_buf, util_fw_version,
  277. hb_srv_version);
  278. } else {
  279. heartbeat_msg =
  280. (struct heartbeat_msg_data *)&hbeat_txf_buf[
  281. sizeof(struct vmbuspipe_hdr) +
  282. sizeof(struct icmsg_hdr)];
  283. heartbeat_msg->seq_num += 1;
  284. }
  285. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  286. | ICMSGHDRFLAG_RESPONSE;
  287. vmbus_sendpacket(channel, hbeat_txf_buf,
  288. recvlen, requestid,
  289. VM_PKT_DATA_INBAND, 0);
  290. }
  291. }
  292. static int util_probe(struct hv_device *dev,
  293. const struct hv_vmbus_device_id *dev_id)
  294. {
  295. struct hv_util_service *srv =
  296. (struct hv_util_service *)dev_id->driver_data;
  297. int ret;
  298. srv->recv_buffer = kmalloc(PAGE_SIZE * 4, GFP_KERNEL);
  299. if (!srv->recv_buffer)
  300. return -ENOMEM;
  301. srv->channel = dev->channel;
  302. if (srv->util_init) {
  303. ret = srv->util_init(srv);
  304. if (ret) {
  305. ret = -ENODEV;
  306. goto error1;
  307. }
  308. }
  309. /*
  310. * The set of services managed by the util driver are not performance
  311. * critical and do not need batched reading. Furthermore, some services
  312. * such as KVP can only handle one message from the host at a time.
  313. * Turn off batched reading for all util drivers before we open the
  314. * channel.
  315. */
  316. set_channel_read_state(dev->channel, false);
  317. hv_set_drvdata(dev, srv);
  318. /*
  319. * Based on the host; initialize the framework and
  320. * service version numbers we will negotiate.
  321. */
  322. switch (vmbus_proto_version) {
  323. case (VERSION_WS2008):
  324. util_fw_version = UTIL_WS2K8_FW_VERSION;
  325. sd_srv_version = SD_VERSION_1;
  326. ts_srv_version = TS_VERSION_1;
  327. hb_srv_version = HB_VERSION_1;
  328. break;
  329. case(VERSION_WIN10):
  330. util_fw_version = UTIL_FW_VERSION;
  331. sd_srv_version = SD_VERSION;
  332. ts_srv_version = TS_VERSION;
  333. hb_srv_version = HB_VERSION;
  334. break;
  335. default:
  336. util_fw_version = UTIL_FW_VERSION;
  337. sd_srv_version = SD_VERSION;
  338. ts_srv_version = TS_VERSION_3;
  339. hb_srv_version = HB_VERSION;
  340. }
  341. ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
  342. srv->util_cb, dev->channel);
  343. if (ret)
  344. goto error;
  345. return 0;
  346. error:
  347. if (srv->util_deinit)
  348. srv->util_deinit();
  349. error1:
  350. kfree(srv->recv_buffer);
  351. return ret;
  352. }
  353. static int util_remove(struct hv_device *dev)
  354. {
  355. struct hv_util_service *srv = hv_get_drvdata(dev);
  356. if (srv->util_deinit)
  357. srv->util_deinit();
  358. vmbus_close(dev->channel);
  359. kfree(srv->recv_buffer);
  360. return 0;
  361. }
  362. static const struct hv_vmbus_device_id id_table[] = {
  363. /* Shutdown guid */
  364. { HV_SHUTDOWN_GUID,
  365. .driver_data = (unsigned long)&util_shutdown
  366. },
  367. /* Time synch guid */
  368. { HV_TS_GUID,
  369. .driver_data = (unsigned long)&util_timesynch
  370. },
  371. /* Heartbeat guid */
  372. { HV_HEART_BEAT_GUID,
  373. .driver_data = (unsigned long)&util_heartbeat
  374. },
  375. /* KVP guid */
  376. { HV_KVP_GUID,
  377. .driver_data = (unsigned long)&util_kvp
  378. },
  379. /* VSS GUID */
  380. { HV_VSS_GUID,
  381. .driver_data = (unsigned long)&util_vss
  382. },
  383. /* File copy GUID */
  384. { HV_FCOPY_GUID,
  385. .driver_data = (unsigned long)&util_fcopy
  386. },
  387. { },
  388. };
  389. MODULE_DEVICE_TABLE(vmbus, id_table);
  390. /* The one and only one */
  391. static struct hv_driver util_drv = {
  392. .name = "hv_util",
  393. .id_table = id_table,
  394. .probe = util_probe,
  395. .remove = util_remove,
  396. };
  397. static int hv_timesync_init(struct hv_util_service *srv)
  398. {
  399. INIT_WORK(&wrk.work, hv_set_host_time);
  400. return 0;
  401. }
  402. static void hv_timesync_deinit(void)
  403. {
  404. cancel_work_sync(&wrk.work);
  405. }
  406. static int __init init_hyperv_utils(void)
  407. {
  408. pr_info("Registering HyperV Utility Driver\n");
  409. return vmbus_driver_register(&util_drv);
  410. }
  411. static void exit_hyperv_utils(void)
  412. {
  413. pr_info("De-Registered HyperV Utility Driver\n");
  414. vmbus_driver_unregister(&util_drv);
  415. }
  416. module_init(init_hyperv_utils);
  417. module_exit(exit_hyperv_utils);
  418. MODULE_DESCRIPTION("Hyper-V Utilities");
  419. MODULE_LICENSE("GPL");