hv_util.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. static void shutdown_onchannelcallback(void *context);
  30. static struct hv_util_service util_shutdown = {
  31. .util_cb = shutdown_onchannelcallback,
  32. };
  33. static void timesync_onchannelcallback(void *context);
  34. static struct hv_util_service util_timesynch = {
  35. .util_cb = timesync_onchannelcallback,
  36. };
  37. static void heartbeat_onchannelcallback(void *context);
  38. static struct hv_util_service util_heartbeat = {
  39. .util_cb = heartbeat_onchannelcallback,
  40. };
  41. static struct hv_util_service util_kvp = {
  42. .util_cb = hv_kvp_onchannelcallback,
  43. .util_init = hv_kvp_init,
  44. .util_deinit = hv_kvp_deinit,
  45. };
  46. static void shutdown_onchannelcallback(void *context)
  47. {
  48. struct vmbus_channel *channel = context;
  49. u32 recvlen;
  50. u64 requestid;
  51. u8 execute_shutdown = false;
  52. u8 *shut_txf_buf = util_shutdown.recv_buffer;
  53. struct shutdown_msg_data *shutdown_msg;
  54. struct icmsg_hdr *icmsghdrp;
  55. struct icmsg_negotiate *negop = NULL;
  56. vmbus_recvpacket(channel, shut_txf_buf,
  57. PAGE_SIZE, &recvlen, &requestid);
  58. if (recvlen > 0) {
  59. icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
  60. sizeof(struct vmbuspipe_hdr)];
  61. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  62. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  63. shut_txf_buf, MAX_SRV_VER, MAX_SRV_VER);
  64. } else {
  65. shutdown_msg =
  66. (struct shutdown_msg_data *)&shut_txf_buf[
  67. sizeof(struct vmbuspipe_hdr) +
  68. sizeof(struct icmsg_hdr)];
  69. switch (shutdown_msg->flags) {
  70. case 0:
  71. case 1:
  72. icmsghdrp->status = HV_S_OK;
  73. execute_shutdown = true;
  74. pr_info("Shutdown request received -"
  75. " graceful shutdown initiated\n");
  76. break;
  77. default:
  78. icmsghdrp->status = HV_E_FAIL;
  79. execute_shutdown = false;
  80. pr_info("Shutdown request received -"
  81. " Invalid request\n");
  82. break;
  83. }
  84. }
  85. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  86. | ICMSGHDRFLAG_RESPONSE;
  87. vmbus_sendpacket(channel, shut_txf_buf,
  88. recvlen, requestid,
  89. VM_PKT_DATA_INBAND, 0);
  90. }
  91. if (execute_shutdown == true)
  92. orderly_poweroff(true);
  93. }
  94. /*
  95. * Set guest time to host UTC time.
  96. */
  97. static inline void do_adj_guesttime(u64 hosttime)
  98. {
  99. s64 host_tns;
  100. struct timespec host_ts;
  101. host_tns = (hosttime - WLTIMEDELTA) * 100;
  102. host_ts = ns_to_timespec(host_tns);
  103. do_settimeofday(&host_ts);
  104. }
  105. /*
  106. * Set the host time in a process context.
  107. */
  108. struct adj_time_work {
  109. struct work_struct work;
  110. u64 host_time;
  111. };
  112. static void hv_set_host_time(struct work_struct *work)
  113. {
  114. struct adj_time_work *wrk;
  115. wrk = container_of(work, struct adj_time_work, work);
  116. do_adj_guesttime(wrk->host_time);
  117. kfree(wrk);
  118. }
  119. /*
  120. * Synchronize time with host after reboot, restore, etc.
  121. *
  122. * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
  123. * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
  124. * message after the timesync channel is opened. Since the hv_utils module is
  125. * loaded after hv_vmbus, the first message is usually missed. The other
  126. * thing is, systime is automatically set to emulated hardware clock which may
  127. * not be UTC time or in the same time zone. So, to override these effects, we
  128. * use the first 50 time samples for initial system time setting.
  129. */
  130. static inline void adj_guesttime(u64 hosttime, u8 flags)
  131. {
  132. struct adj_time_work *wrk;
  133. static s32 scnt = 50;
  134. wrk = kmalloc(sizeof(struct adj_time_work), GFP_ATOMIC);
  135. if (wrk == NULL)
  136. return;
  137. wrk->host_time = hosttime;
  138. if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
  139. INIT_WORK(&wrk->work, hv_set_host_time);
  140. schedule_work(&wrk->work);
  141. return;
  142. }
  143. if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
  144. scnt--;
  145. INIT_WORK(&wrk->work, hv_set_host_time);
  146. schedule_work(&wrk->work);
  147. } else
  148. kfree(wrk);
  149. }
  150. /*
  151. * Time Sync Channel message handler.
  152. */
  153. static void timesync_onchannelcallback(void *context)
  154. {
  155. struct vmbus_channel *channel = context;
  156. u32 recvlen;
  157. u64 requestid;
  158. struct icmsg_hdr *icmsghdrp;
  159. struct ictimesync_data *timedatap;
  160. u8 *time_txf_buf = util_timesynch.recv_buffer;
  161. vmbus_recvpacket(channel, time_txf_buf,
  162. PAGE_SIZE, &recvlen, &requestid);
  163. if (recvlen > 0) {
  164. icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
  165. sizeof(struct vmbuspipe_hdr)];
  166. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  167. vmbus_prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf,
  168. MAX_SRV_VER, MAX_SRV_VER);
  169. } else {
  170. timedatap = (struct ictimesync_data *)&time_txf_buf[
  171. sizeof(struct vmbuspipe_hdr) +
  172. sizeof(struct icmsg_hdr)];
  173. adj_guesttime(timedatap->parenttime, timedatap->flags);
  174. }
  175. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  176. | ICMSGHDRFLAG_RESPONSE;
  177. vmbus_sendpacket(channel, time_txf_buf,
  178. recvlen, requestid,
  179. VM_PKT_DATA_INBAND, 0);
  180. }
  181. }
  182. /*
  183. * Heartbeat functionality.
  184. * Every two seconds, Hyper-V send us a heartbeat request message.
  185. * we respond to this message, and Hyper-V knows we are alive.
  186. */
  187. static void heartbeat_onchannelcallback(void *context)
  188. {
  189. struct vmbus_channel *channel = context;
  190. u32 recvlen;
  191. u64 requestid;
  192. struct icmsg_hdr *icmsghdrp;
  193. struct heartbeat_msg_data *heartbeat_msg;
  194. u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
  195. vmbus_recvpacket(channel, hbeat_txf_buf,
  196. PAGE_SIZE, &recvlen, &requestid);
  197. if (recvlen > 0) {
  198. icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
  199. sizeof(struct vmbuspipe_hdr)];
  200. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  201. vmbus_prep_negotiate_resp(icmsghdrp, NULL,
  202. hbeat_txf_buf, MAX_SRV_VER, MAX_SRV_VER);
  203. } else {
  204. heartbeat_msg =
  205. (struct heartbeat_msg_data *)&hbeat_txf_buf[
  206. sizeof(struct vmbuspipe_hdr) +
  207. sizeof(struct icmsg_hdr)];
  208. heartbeat_msg->seq_num += 1;
  209. }
  210. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  211. | ICMSGHDRFLAG_RESPONSE;
  212. vmbus_sendpacket(channel, hbeat_txf_buf,
  213. recvlen, requestid,
  214. VM_PKT_DATA_INBAND, 0);
  215. }
  216. }
  217. static int util_probe(struct hv_device *dev,
  218. const struct hv_vmbus_device_id *dev_id)
  219. {
  220. struct hv_util_service *srv =
  221. (struct hv_util_service *)dev_id->driver_data;
  222. int ret;
  223. srv->recv_buffer = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
  224. if (!srv->recv_buffer)
  225. return -ENOMEM;
  226. if (srv->util_init) {
  227. ret = srv->util_init(srv);
  228. if (ret) {
  229. ret = -ENODEV;
  230. goto error1;
  231. }
  232. }
  233. ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
  234. srv->util_cb, dev->channel);
  235. if (ret)
  236. goto error;
  237. hv_set_drvdata(dev, srv);
  238. return 0;
  239. error:
  240. if (srv->util_deinit)
  241. srv->util_deinit();
  242. error1:
  243. kfree(srv->recv_buffer);
  244. return ret;
  245. }
  246. static int util_remove(struct hv_device *dev)
  247. {
  248. struct hv_util_service *srv = hv_get_drvdata(dev);
  249. vmbus_close(dev->channel);
  250. if (srv->util_deinit)
  251. srv->util_deinit();
  252. kfree(srv->recv_buffer);
  253. return 0;
  254. }
  255. static const struct hv_vmbus_device_id id_table[] = {
  256. /* Shutdown guid */
  257. { VMBUS_DEVICE(0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
  258. 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB)
  259. .driver_data = (unsigned long)&util_shutdown },
  260. /* Time synch guid */
  261. { VMBUS_DEVICE(0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
  262. 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf)
  263. .driver_data = (unsigned long)&util_timesynch },
  264. /* Heartbeat guid */
  265. { VMBUS_DEVICE(0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
  266. 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d)
  267. .driver_data = (unsigned long)&util_heartbeat },
  268. /* KVP guid */
  269. { VMBUS_DEVICE(0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
  270. 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6)
  271. .driver_data = (unsigned long)&util_kvp },
  272. { },
  273. };
  274. MODULE_DEVICE_TABLE(vmbus, id_table);
  275. /* The one and only one */
  276. static struct hv_driver util_drv = {
  277. .name = "hv_util",
  278. .id_table = id_table,
  279. .probe = util_probe,
  280. .remove = util_remove,
  281. };
  282. static int __init init_hyperv_utils(void)
  283. {
  284. pr_info("Registering HyperV Utility Driver\n");
  285. return vmbus_driver_register(&util_drv);
  286. }
  287. static void exit_hyperv_utils(void)
  288. {
  289. pr_info("De-Registered HyperV Utility Driver\n");
  290. vmbus_driver_unregister(&util_drv);
  291. }
  292. module_init(init_hyperv_utils);
  293. module_exit(exit_hyperv_utils);
  294. MODULE_DESCRIPTION("Hyper-V Utilities");
  295. MODULE_VERSION(HV_DRV_VERSION);
  296. MODULE_LICENSE("GPL");