hv_util.c 9.7 KB

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