hv_util.c 11 KB

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