PageRenderTime 128ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/hv/hv_util.c

https://gitlab.com/buktemirlnk/mirrors
C | 452 lines | 309 code | 77 blank | 66 comment | 30 complexity | c9f0870ddfabdf318aa90766c67cac54 MD5 | raw file
  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. while (1) {
  239. vmbus_recvpacket(channel, hbeat_txf_buf,
  240. PAGE_SIZE, &recvlen, &requestid);
  241. if (!recvlen)
  242. break;
  243. icmsghdrp = (struct icmsg_hdr *)&hbeat_txf_buf[
  244. sizeof(struct vmbuspipe_hdr)];
  245. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  246. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  247. hbeat_txf_buf, util_fw_version,
  248. hb_srv_version);
  249. } else {
  250. heartbeat_msg =
  251. (struct heartbeat_msg_data *)&hbeat_txf_buf[
  252. sizeof(struct vmbuspipe_hdr) +
  253. sizeof(struct icmsg_hdr)];
  254. heartbeat_msg->seq_num += 1;
  255. }
  256. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  257. | ICMSGHDRFLAG_RESPONSE;
  258. vmbus_sendpacket(channel, hbeat_txf_buf,
  259. recvlen, requestid,
  260. VM_PKT_DATA_INBAND, 0);
  261. }
  262. }
  263. static int util_probe(struct hv_device *dev,
  264. const struct hv_vmbus_device_id *dev_id)
  265. {
  266. struct hv_util_service *srv =
  267. (struct hv_util_service *)dev_id->driver_data;
  268. int ret;
  269. srv->recv_buffer = kmalloc(PAGE_SIZE * 4, GFP_KERNEL);
  270. if (!srv->recv_buffer)
  271. return -ENOMEM;
  272. srv->channel = dev->channel;
  273. if (srv->util_init) {
  274. ret = srv->util_init(srv);
  275. if (ret) {
  276. ret = -ENODEV;
  277. goto error1;
  278. }
  279. }
  280. /*
  281. * The set of services managed by the util driver are not performance
  282. * critical and do not need batched reading. Furthermore, some services
  283. * such as KVP can only handle one message from the host at a time.
  284. * Turn off batched reading for all util drivers before we open the
  285. * channel.
  286. */
  287. set_channel_read_state(dev->channel, false);
  288. hv_set_drvdata(dev, srv);
  289. /*
  290. * Based on the host; initialize the framework and
  291. * service version numbers we will negotiate.
  292. */
  293. switch (vmbus_proto_version) {
  294. case (VERSION_WS2008):
  295. util_fw_version = UTIL_WS2K8_FW_VERSION;
  296. sd_srv_version = SD_WS2008_VERSION;
  297. ts_srv_version = TS_WS2008_VERSION;
  298. hb_srv_version = HB_WS2008_VERSION;
  299. break;
  300. default:
  301. util_fw_version = UTIL_FW_VERSION;
  302. sd_srv_version = SD_VERSION;
  303. ts_srv_version = TS_VERSION;
  304. hb_srv_version = HB_VERSION;
  305. }
  306. ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
  307. srv->util_cb, dev->channel);
  308. if (ret)
  309. goto error;
  310. return 0;
  311. error:
  312. if (srv->util_deinit)
  313. srv->util_deinit();
  314. error1:
  315. kfree(srv->recv_buffer);
  316. return ret;
  317. }
  318. static int util_remove(struct hv_device *dev)
  319. {
  320. struct hv_util_service *srv = hv_get_drvdata(dev);
  321. if (srv->util_deinit)
  322. srv->util_deinit();
  323. vmbus_close(dev->channel);
  324. kfree(srv->recv_buffer);
  325. return 0;
  326. }
  327. static const struct hv_vmbus_device_id id_table[] = {
  328. /* Shutdown guid */
  329. { HV_SHUTDOWN_GUID,
  330. .driver_data = (unsigned long)&util_shutdown
  331. },
  332. /* Time synch guid */
  333. { HV_TS_GUID,
  334. .driver_data = (unsigned long)&util_timesynch
  335. },
  336. /* Heartbeat guid */
  337. { HV_HEART_BEAT_GUID,
  338. .driver_data = (unsigned long)&util_heartbeat
  339. },
  340. /* KVP guid */
  341. { HV_KVP_GUID,
  342. .driver_data = (unsigned long)&util_kvp
  343. },
  344. /* VSS GUID */
  345. { HV_VSS_GUID,
  346. .driver_data = (unsigned long)&util_vss
  347. },
  348. /* File copy GUID */
  349. { HV_FCOPY_GUID,
  350. .driver_data = (unsigned long)&util_fcopy
  351. },
  352. { },
  353. };
  354. MODULE_DEVICE_TABLE(vmbus, id_table);
  355. /* The one and only one */
  356. static struct hv_driver util_drv = {
  357. .name = "hv_util",
  358. .id_table = id_table,
  359. .probe = util_probe,
  360. .remove = util_remove,
  361. };
  362. static int __init init_hyperv_utils(void)
  363. {
  364. pr_info("Registering HyperV Utility Driver\n");
  365. return vmbus_driver_register(&util_drv);
  366. }
  367. static void exit_hyperv_utils(void)
  368. {
  369. pr_info("De-Registered HyperV Utility Driver\n");
  370. vmbus_driver_unregister(&util_drv);
  371. }
  372. module_init(init_hyperv_utils);
  373. module_exit(exit_hyperv_utils);
  374. MODULE_DESCRIPTION("Hyper-V Utilities");
  375. MODULE_LICENSE("GPL");