hv_util.c 11 KB

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