connection.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (c) 2009, Microsoft Corporation.
  5. *
  6. * Authors:
  7. * Haiyang Zhang <haiyangz@microsoft.com>
  8. * Hank Janssen <hjanssen@microsoft.com>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/wait.h>
  14. #include <linux/delay.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/hyperv.h>
  20. #include <linux/export.h>
  21. #include <asm/mshyperv.h>
  22. #include "hyperv_vmbus.h"
  23. struct vmbus_connection vmbus_connection = {
  24. .conn_state = DISCONNECTED,
  25. .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
  26. .ready_for_suspend_event= COMPLETION_INITIALIZER(
  27. vmbus_connection.ready_for_suspend_event),
  28. .ready_for_resume_event = COMPLETION_INITIALIZER(
  29. vmbus_connection.ready_for_resume_event),
  30. };
  31. EXPORT_SYMBOL_GPL(vmbus_connection);
  32. /*
  33. * Negotiated protocol version with the host.
  34. */
  35. __u32 vmbus_proto_version;
  36. EXPORT_SYMBOL_GPL(vmbus_proto_version);
  37. /*
  38. * Table of VMBus versions listed from newest to oldest.
  39. */
  40. static __u32 vmbus_versions[] = {
  41. VERSION_WIN10_V5_2,
  42. VERSION_WIN10_V5_1,
  43. VERSION_WIN10_V5,
  44. VERSION_WIN10_V4_1,
  45. VERSION_WIN10,
  46. VERSION_WIN8_1,
  47. VERSION_WIN8,
  48. VERSION_WIN7,
  49. VERSION_WS2008
  50. };
  51. /*
  52. * Maximal VMBus protocol version guests can negotiate. Useful to cap the
  53. * VMBus version for testing and debugging purpose.
  54. */
  55. static uint max_version = VERSION_WIN10_V5_2;
  56. module_param(max_version, uint, S_IRUGO);
  57. MODULE_PARM_DESC(max_version,
  58. "Maximal VMBus protocol version which can be negotiated");
  59. int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
  60. {
  61. int ret = 0;
  62. struct vmbus_channel_initiate_contact *msg;
  63. unsigned long flags;
  64. init_completion(&msginfo->waitevent);
  65. msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
  66. memset(msg, 0, sizeof(*msg));
  67. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  68. msg->vmbus_version_requested = version;
  69. /*
  70. * VMBus protocol 5.0 (VERSION_WIN10_V5) and higher require that we must
  71. * use VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
  72. * and for subsequent messages, we must use the Message Connection ID
  73. * field in the host-returned Version Response Message. And, with
  74. * VERSION_WIN10_V5 and higher, we don't use msg->interrupt_page, but we
  75. * tell the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
  76. * compatibility.
  77. *
  78. * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
  79. */
  80. if (version >= VERSION_WIN10_V5) {
  81. msg->msg_sint = VMBUS_MESSAGE_SINT;
  82. vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
  83. } else {
  84. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  85. vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
  86. }
  87. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
  88. msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
  89. msg->target_vcpu = hv_cpu_number_to_vp_number(VMBUS_CONNECT_CPU);
  90. /*
  91. * Add to list before we send the request since we may
  92. * receive the response before returning from this routine
  93. */
  94. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  95. list_add_tail(&msginfo->msglistentry,
  96. &vmbus_connection.chn_msg_list);
  97. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  98. ret = vmbus_post_msg(msg,
  99. sizeof(struct vmbus_channel_initiate_contact),
  100. true);
  101. trace_vmbus_negotiate_version(msg, ret);
  102. if (ret != 0) {
  103. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  104. list_del(&msginfo->msglistentry);
  105. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  106. flags);
  107. return ret;
  108. }
  109. /* Wait for the connection response */
  110. wait_for_completion(&msginfo->waitevent);
  111. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  112. list_del(&msginfo->msglistentry);
  113. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  114. /* Check if successful */
  115. if (msginfo->response.version_response.version_supported) {
  116. vmbus_connection.conn_state = CONNECTED;
  117. if (version >= VERSION_WIN10_V5)
  118. vmbus_connection.msg_conn_id =
  119. msginfo->response.version_response.msg_conn_id;
  120. } else {
  121. return -ECONNREFUSED;
  122. }
  123. return ret;
  124. }
  125. /*
  126. * vmbus_connect - Sends a connect request on the partition service connection
  127. */
  128. int vmbus_connect(void)
  129. {
  130. struct vmbus_channel_msginfo *msginfo = NULL;
  131. int i, ret = 0;
  132. __u32 version;
  133. /* Initialize the vmbus connection */
  134. vmbus_connection.conn_state = CONNECTING;
  135. vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
  136. if (!vmbus_connection.work_queue) {
  137. ret = -ENOMEM;
  138. goto cleanup;
  139. }
  140. vmbus_connection.handle_primary_chan_wq =
  141. create_workqueue("hv_pri_chan");
  142. if (!vmbus_connection.handle_primary_chan_wq) {
  143. ret = -ENOMEM;
  144. goto cleanup;
  145. }
  146. vmbus_connection.handle_sub_chan_wq =
  147. create_workqueue("hv_sub_chan");
  148. if (!vmbus_connection.handle_sub_chan_wq) {
  149. ret = -ENOMEM;
  150. goto cleanup;
  151. }
  152. INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
  153. spin_lock_init(&vmbus_connection.channelmsg_lock);
  154. INIT_LIST_HEAD(&vmbus_connection.chn_list);
  155. mutex_init(&vmbus_connection.channel_mutex);
  156. /*
  157. * Setup the vmbus event connection for channel interrupt
  158. * abstraction stuff
  159. */
  160. vmbus_connection.int_page =
  161. (void *)hv_alloc_hyperv_zeroed_page();
  162. if (vmbus_connection.int_page == NULL) {
  163. ret = -ENOMEM;
  164. goto cleanup;
  165. }
  166. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  167. vmbus_connection.send_int_page =
  168. (void *)((unsigned long)vmbus_connection.int_page +
  169. (HV_HYP_PAGE_SIZE >> 1));
  170. /*
  171. * Setup the monitor notification facility. The 1st page for
  172. * parent->child and the 2nd page for child->parent
  173. */
  174. vmbus_connection.monitor_pages[0] = (void *)hv_alloc_hyperv_zeroed_page();
  175. vmbus_connection.monitor_pages[1] = (void *)hv_alloc_hyperv_zeroed_page();
  176. if ((vmbus_connection.monitor_pages[0] == NULL) ||
  177. (vmbus_connection.monitor_pages[1] == NULL)) {
  178. ret = -ENOMEM;
  179. goto cleanup;
  180. }
  181. msginfo = kzalloc(sizeof(*msginfo) +
  182. sizeof(struct vmbus_channel_initiate_contact),
  183. GFP_KERNEL);
  184. if (msginfo == NULL) {
  185. ret = -ENOMEM;
  186. goto cleanup;
  187. }
  188. /*
  189. * Negotiate a compatible VMBUS version number with the
  190. * host. We start with the highest number we can support
  191. * and work our way down until we negotiate a compatible
  192. * version.
  193. */
  194. for (i = 0; ; i++) {
  195. if (i == ARRAY_SIZE(vmbus_versions)) {
  196. ret = -EDOM;
  197. goto cleanup;
  198. }
  199. version = vmbus_versions[i];
  200. if (version > max_version)
  201. continue;
  202. ret = vmbus_negotiate_version(msginfo, version);
  203. if (ret == -ETIMEDOUT)
  204. goto cleanup;
  205. if (vmbus_connection.conn_state == CONNECTED)
  206. break;
  207. }
  208. vmbus_proto_version = version;
  209. pr_info("Vmbus version:%d.%d\n",
  210. version >> 16, version & 0xFFFF);
  211. vmbus_connection.channels = kcalloc(MAX_CHANNEL_RELIDS,
  212. sizeof(struct vmbus_channel *),
  213. GFP_KERNEL);
  214. if (vmbus_connection.channels == NULL) {
  215. ret = -ENOMEM;
  216. goto cleanup;
  217. }
  218. kfree(msginfo);
  219. return 0;
  220. cleanup:
  221. pr_err("Unable to connect to host\n");
  222. vmbus_connection.conn_state = DISCONNECTED;
  223. vmbus_disconnect();
  224. kfree(msginfo);
  225. return ret;
  226. }
  227. void vmbus_disconnect(void)
  228. {
  229. /*
  230. * First send the unload request to the host.
  231. */
  232. vmbus_initiate_unload(false);
  233. if (vmbus_connection.handle_sub_chan_wq)
  234. destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
  235. if (vmbus_connection.handle_primary_chan_wq)
  236. destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
  237. if (vmbus_connection.work_queue)
  238. destroy_workqueue(vmbus_connection.work_queue);
  239. if (vmbus_connection.int_page) {
  240. hv_free_hyperv_page((unsigned long)vmbus_connection.int_page);
  241. vmbus_connection.int_page = NULL;
  242. }
  243. hv_free_hyperv_page((unsigned long)vmbus_connection.monitor_pages[0]);
  244. hv_free_hyperv_page((unsigned long)vmbus_connection.monitor_pages[1]);
  245. vmbus_connection.monitor_pages[0] = NULL;
  246. vmbus_connection.monitor_pages[1] = NULL;
  247. }
  248. /*
  249. * relid2channel - Get the channel object given its
  250. * child relative id (ie channel id)
  251. */
  252. struct vmbus_channel *relid2channel(u32 relid)
  253. {
  254. if (WARN_ON(relid >= MAX_CHANNEL_RELIDS))
  255. return NULL;
  256. return READ_ONCE(vmbus_connection.channels[relid]);
  257. }
  258. /*
  259. * vmbus_on_event - Process a channel event notification
  260. *
  261. * For batched channels (default) optimize host to guest signaling
  262. * by ensuring:
  263. * 1. While reading the channel, we disable interrupts from host.
  264. * 2. Ensure that we process all posted messages from the host
  265. * before returning from this callback.
  266. * 3. Once we return, enable signaling from the host. Once this
  267. * state is set we check to see if additional packets are
  268. * available to read. In this case we repeat the process.
  269. * If this tasklet has been running for a long time
  270. * then reschedule ourselves.
  271. */
  272. void vmbus_on_event(unsigned long data)
  273. {
  274. struct vmbus_channel *channel = (void *) data;
  275. unsigned long time_limit = jiffies + 2;
  276. trace_vmbus_on_event(channel);
  277. hv_debug_delay_test(channel, INTERRUPT_DELAY);
  278. do {
  279. void (*callback_fn)(void *);
  280. /* A channel once created is persistent even when
  281. * there is no driver handling the device. An
  282. * unloading driver sets the onchannel_callback to NULL.
  283. */
  284. callback_fn = READ_ONCE(channel->onchannel_callback);
  285. if (unlikely(callback_fn == NULL))
  286. return;
  287. (*callback_fn)(channel->channel_callback_context);
  288. if (channel->callback_mode != HV_CALL_BATCHED)
  289. return;
  290. if (likely(hv_end_read(&channel->inbound) == 0))
  291. return;
  292. hv_begin_read(&channel->inbound);
  293. } while (likely(time_before(jiffies, time_limit)));
  294. /* The time limit (2 jiffies) has been reached */
  295. tasklet_schedule(&channel->callback_event);
  296. }
  297. /*
  298. * vmbus_post_msg - Send a msg on the vmbus's message connection
  299. */
  300. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
  301. {
  302. struct vmbus_channel_message_header *hdr;
  303. union hv_connection_id conn_id;
  304. int ret = 0;
  305. int retries = 0;
  306. u32 usec = 1;
  307. conn_id.asu32 = 0;
  308. conn_id.u.id = vmbus_connection.msg_conn_id;
  309. /*
  310. * hv_post_message() can have transient failures because of
  311. * insufficient resources. Retry the operation a couple of
  312. * times before giving up.
  313. */
  314. while (retries < 100) {
  315. ret = hv_post_message(conn_id, 1, buffer, buflen);
  316. switch (ret) {
  317. case HV_STATUS_INVALID_CONNECTION_ID:
  318. /*
  319. * See vmbus_negotiate_version(): VMBus protocol 5.0
  320. * and higher require that we must use
  321. * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
  322. * Contact message, but on old hosts that only
  323. * support VMBus protocol 4.0 or lower, here we get
  324. * HV_STATUS_INVALID_CONNECTION_ID and we should
  325. * return an error immediately without retrying.
  326. */
  327. hdr = buffer;
  328. if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
  329. return -EINVAL;
  330. /*
  331. * We could get this if we send messages too
  332. * frequently.
  333. */
  334. ret = -EAGAIN;
  335. break;
  336. case HV_STATUS_INSUFFICIENT_MEMORY:
  337. case HV_STATUS_INSUFFICIENT_BUFFERS:
  338. ret = -ENOBUFS;
  339. break;
  340. case HV_STATUS_SUCCESS:
  341. return ret;
  342. default:
  343. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  344. return -EINVAL;
  345. }
  346. retries++;
  347. if (can_sleep && usec > 1000)
  348. msleep(usec / 1000);
  349. else if (usec < MAX_UDELAY_MS * 1000)
  350. udelay(usec);
  351. else
  352. mdelay(usec / 1000);
  353. if (retries < 22)
  354. usec *= 2;
  355. }
  356. return ret;
  357. }
  358. /*
  359. * vmbus_set_event - Send an event notification to the parent
  360. */
  361. void vmbus_set_event(struct vmbus_channel *channel)
  362. {
  363. u32 child_relid = channel->offermsg.child_relid;
  364. if (!channel->is_dedicated_interrupt)
  365. vmbus_send_interrupt(child_relid);
  366. ++channel->sig_events;
  367. hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
  368. }
  369. EXPORT_SYMBOL_GPL(vmbus_set_event);