hv_util.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 struct hv_util_service util_vss = {
  47. .util_cb = hv_vss_onchannelcallback,
  48. .util_init = hv_vss_init,
  49. .util_deinit = hv_vss_deinit,
  50. };
  51. static void perform_shutdown(struct work_struct *dummy)
  52. {
  53. orderly_poweroff(true);
  54. }
  55. /*
  56. * Perform the shutdown operation in a thread context.
  57. */
  58. static DECLARE_WORK(shutdown_work, perform_shutdown);
  59. static void shutdown_onchannelcallback(void *context)
  60. {
  61. struct vmbus_channel *channel = context;
  62. u32 recvlen;
  63. u64 requestid;
  64. u8 execute_shutdown = false;
  65. u8 *shut_txf_buf = util_shutdown.recv_buffer;
  66. struct shutdown_msg_data *shutdown_msg;
  67. struct icmsg_hdr *icmsghdrp;
  68. struct icmsg_negotiate *negop = NULL;
  69. vmbus_recvpacket(channel, shut_txf_buf,
  70. PAGE_SIZE, &recvlen, &requestid);
  71. if (recvlen > 0) {
  72. icmsghdrp = (struct icmsg_hdr *)&shut_txf_buf[
  73. sizeof(struct vmbuspipe_hdr)];
  74. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  75. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  76. shut_txf_buf, MAX_SRV_VER, MAX_SRV_VER);
  77. } else {
  78. shutdown_msg =
  79. (struct shutdown_msg_data *)&shut_txf_buf[
  80. sizeof(struct vmbuspipe_hdr) +
  81. sizeof(struct icmsg_hdr)];
  82. switch (shutdown_msg->flags) {
  83. case 0:
  84. case 1:
  85. icmsghdrp->status = HV_S_OK;
  86. execute_shutdown = true;
  87. pr_info("Shutdown request received -"
  88. " graceful shutdown initiated\n");
  89. break;
  90. default:
  91. icmsghdrp->status = HV_E_FAIL;
  92. execute_shutdown = false;
  93. pr_info("Shutdown request received -"
  94. " Invalid request\n");
  95. break;
  96. }
  97. }
  98. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  99. | ICMSGHDRFLAG_RESPONSE;
  100. vmbus_sendpacket(channel, shut_txf_buf,
  101. recvlen, requestid,
  102. VM_PKT_DATA_INBAND, 0);
  103. }
  104. if (execute_shutdown == true)
  105. schedule_work(&shutdown_work);
  106. }
  107. /*
  108. * Set guest time to host UTC time.
  109. */
  110. static inline void do_adj_guesttime(u64 hosttime)
  111. {
  112. s64 host_tns;
  113. struct timespec host_ts;
  114. host_tns = (hosttime - WLTIMEDELTA) * 100;
  115. host_ts = ns_to_timespec(host_tns);
  116. do_settimeofday(&host_ts);
  117. }
  118. /*
  119. * Set the host time in a process context.
  120. */
  121. struct adj_time_work {
  122. struct work_struct work;
  123. u64 host_time;
  124. };
  125. static void hv_set_host_time(struct work_struct *work)
  126. {
  127. struct adj_time_work *wrk;
  128. wrk = container_of(work, struct adj_time_work, work);
  129. do_adj_guesttime(wrk->host_time);
  130. kfree(wrk);
  131. }
  132. /*
  133. * Synchronize time with host after reboot, restore, etc.
  134. *
  135. * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
  136. * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
  137. * message after the timesync channel is opened. Since the hv_utils module is
  138. * loaded after hv_vmbus, the first message is usually missed. The other
  139. * thing is, systime is automatically set to emulated hardware clock which may
  140. * not be UTC time or in the same time zone. So, to override these effects, we
  141. * use the first 50 time samples for initial system time setting.
  142. */
  143. static inline void adj_guesttime(u64 hosttime, u8 flags)
  144. {
  145. struct adj_time_work *wrk;
  146. static s32 scnt = 50;
  147. wrk = kmalloc(sizeof(struct adj_time_work), GFP_ATOMIC);
  148. if (wrk == NULL)
  149. return;
  150. wrk->host_time = hosttime;
  151. if ((flags & ICTIMESYNCFLAG_SYNC) != 0) {
  152. INIT_WORK(&wrk->work, hv_set_host_time);
  153. schedule_work(&wrk->work);
  154. return;
  155. }
  156. if ((flags & ICTIMESYNCFLAG_SAMPLE) != 0 && scnt > 0) {
  157. scnt--;
  158. INIT_WORK(&wrk->work, hv_set_host_time);
  159. schedule_work(&wrk->work);
  160. } else
  161. kfree(wrk);
  162. }
  163. /*
  164. * Time Sync Channel message handler.
  165. */
  166. static void timesync_onchannelcallback(void *context)
  167. {
  168. struct vmbus_channel *channel = context;
  169. u32 recvlen;
  170. u64 requestid;
  171. struct icmsg_hdr *icmsghdrp;
  172. struct ictimesync_data *timedatap;
  173. u8 *time_txf_buf = util_timesynch.recv_buffer;
  174. vmbus_recvpacket(channel, time_txf_buf,
  175. PAGE_SIZE, &recvlen, &requestid);
  176. if (recvlen > 0) {
  177. icmsghdrp = (struct icmsg_hdr *)&time_txf_buf[
  178. sizeof(struct vmbuspipe_hdr)];
  179. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  180. vmbus_prep_negotiate_resp(icmsghdrp, NULL, time_txf_buf,
  181. MAX_SRV_VER, MAX_SRV_VER);
  182. } else {
  183. timedatap = (struct ictimesync_data *)&time_txf_buf[
  184. sizeof(struct vmbuspipe_hdr) +
  185. sizeof(struct icmsg_hdr)];
  186. adj_guesttime(timedatap->parenttime, timedatap->flags);
  187. }
  188. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  189. | ICMSGHDRFLAG_RESPONSE;
  190. vmbus_sendpacket(channel, time_txf_buf,
  191. recvlen, requestid,
  192. VM_PKT_DATA_INBAND, 0);
  193. }
  194. }
  195. /*
  196. * Heartbeat functionality.
  197. * Every two seconds, Hyper-V send us a heartbeat request message.
  198. * we respond to this message, and Hyper-V knows we are alive.
  199. */
  200. static void heartbeat_onchannelcallback(void *context)
  201. {
  202. struct vmbus_channel *channel = context;
  203. u32 recvlen;
  204. u64 requestid;
  205. struct icmsg_hdr *icmsghdrp;
  206. struct heartbeat_msg_data *heartbeat_msg;
  207. u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
  208. vmbus_recvpacket(channel, hbeat_txf_buf,
  209. PAGE_SIZE, &recvlen, &requestid);
  210. if (recvlen > 0) {
  211. icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
  212. sizeof(struct vmbuspipe_hdr)];
  213. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  214. vmbus_prep_negotiate_resp(icmsghdrp, NULL,
  215. hbeat_txf_buf, MAX_SRV_VER, MAX_SRV_VER);
  216. } else {
  217. heartbeat_msg =
  218. (struct heartbeat_msg_data *)&hbeat_txf_buf[
  219. sizeof(struct vmbuspipe_hdr) +
  220. sizeof(struct icmsg_hdr)];
  221. heartbeat_msg->seq_num += 1;
  222. }
  223. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  224. | ICMSGHDRFLAG_RESPONSE;
  225. vmbus_sendpacket(channel, hbeat_txf_buf,
  226. recvlen, requestid,
  227. VM_PKT_DATA_INBAND, 0);
  228. }
  229. }
  230. static int util_probe(struct hv_device *dev,
  231. const struct hv_vmbus_device_id *dev_id)
  232. {
  233. struct hv_util_service *srv =
  234. (struct hv_util_service *)dev_id->driver_data;
  235. int ret;
  236. srv->recv_buffer = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
  237. if (!srv->recv_buffer)
  238. return -ENOMEM;
  239. if (srv->util_init) {
  240. ret = srv->util_init(srv);
  241. if (ret) {
  242. ret = -ENODEV;
  243. goto error1;
  244. }
  245. }
  246. /*
  247. * The set of services managed by the util driver are not performance
  248. * critical and do not need batched reading. Furthermore, some services
  249. * such as KVP can only handle one message from the host at a time.
  250. * Turn off batched reading for all util drivers before we open the
  251. * channel.
  252. */
  253. set_channel_read_state(dev->channel, false);
  254. ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
  255. srv->util_cb, dev->channel);
  256. if (ret)
  257. goto error;
  258. hv_set_drvdata(dev, srv);
  259. return 0;
  260. error:
  261. if (srv->util_deinit)
  262. srv->util_deinit();
  263. error1:
  264. kfree(srv->recv_buffer);
  265. return ret;
  266. }
  267. static int util_remove(struct hv_device *dev)
  268. {
  269. struct hv_util_service *srv = hv_get_drvdata(dev);
  270. vmbus_close(dev->channel);
  271. if (srv->util_deinit)
  272. srv->util_deinit();
  273. kfree(srv->recv_buffer);
  274. return 0;
  275. }
  276. static const struct hv_vmbus_device_id id_table[] = {
  277. /* Shutdown guid */
  278. { HV_SHUTDOWN_GUID,
  279. .driver_data = (unsigned long)&util_shutdown
  280. },
  281. /* Time synch guid */
  282. { HV_TS_GUID,
  283. .driver_data = (unsigned long)&util_timesynch
  284. },
  285. /* Heartbeat guid */
  286. { HV_HEART_BEAT_GUID,
  287. .driver_data = (unsigned long)&util_heartbeat
  288. },
  289. /* KVP guid */
  290. { HV_KVP_GUID,
  291. .driver_data = (unsigned long)&util_kvp
  292. },
  293. /* VSS GUID */
  294. { HV_VSS_GUID,
  295. .driver_data = (unsigned long)&util_vss
  296. },
  297. { },
  298. };
  299. MODULE_DEVICE_TABLE(vmbus, id_table);
  300. /* The one and only one */
  301. static struct hv_driver util_drv = {
  302. .name = "hv_util",
  303. .id_table = id_table,
  304. .probe = util_probe,
  305. .remove = util_remove,
  306. };
  307. static int __init init_hyperv_utils(void)
  308. {
  309. pr_info("Registering HyperV Utility Driver\n");
  310. return vmbus_driver_register(&util_drv);
  311. }
  312. static void exit_hyperv_utils(void)
  313. {
  314. pr_info("De-Registered HyperV Utility Driver\n");
  315. vmbus_driver_unregister(&util_drv);
  316. }
  317. module_init(init_hyperv_utils);
  318. module_exit(exit_hyperv_utils);
  319. MODULE_DESCRIPTION("Hyper-V Utilities");
  320. MODULE_VERSION(HV_DRV_VERSION);
  321. MODULE_LICENSE("GPL");