connection.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. *
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16. * Place - Suite 330, Boston, MA 02111-1307 USA.
  17. *
  18. * Authors:
  19. * Haiyang Zhang <haiyangz@microsoft.com>
  20. * Hank Janssen <hjanssen@microsoft.com>
  21. *
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/wait.h>
  27. #include <linux/delay.h>
  28. #include <linux/mm.h>
  29. #include <linux/slab.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/hyperv.h>
  32. #include <linux/export.h>
  33. #include <asm/mshyperv.h>
  34. #include "hyperv_vmbus.h"
  35. struct vmbus_connection vmbus_connection = {
  36. .conn_state = DISCONNECTED,
  37. .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
  38. };
  39. EXPORT_SYMBOL_GPL(vmbus_connection);
  40. /*
  41. * Negotiated protocol version with the host.
  42. */
  43. __u32 vmbus_proto_version;
  44. EXPORT_SYMBOL_GPL(vmbus_proto_version);
  45. static __u32 vmbus_get_next_version(__u32 current_version)
  46. {
  47. switch (current_version) {
  48. case (VERSION_WIN7):
  49. return VERSION_WS2008;
  50. case (VERSION_WIN8):
  51. return VERSION_WIN7;
  52. case (VERSION_WIN8_1):
  53. return VERSION_WIN8;
  54. case (VERSION_WIN10):
  55. return VERSION_WIN8_1;
  56. case (VERSION_WS2008):
  57. default:
  58. return VERSION_INVAL;
  59. }
  60. }
  61. static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo,
  62. __u32 version)
  63. {
  64. int ret = 0;
  65. struct vmbus_channel_initiate_contact *msg;
  66. unsigned long flags;
  67. init_completion(&msginfo->waitevent);
  68. msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
  69. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  70. msg->vmbus_version_requested = version;
  71. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  72. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
  73. msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
  74. /*
  75. * We want all channel messages to be delivered on CPU 0.
  76. * This has been the behavior pre-win8. This is not
  77. * perf issue and having all channel messages delivered on CPU 0
  78. * would be ok.
  79. * For post win8 hosts, we support receiving channel messagges on
  80. * all the CPUs. This is needed for kexec to work correctly where
  81. * the CPU attempting to connect may not be CPU 0.
  82. */
  83. if (version >= VERSION_WIN8_1) {
  84. msg->target_vcpu =
  85. hv_cpu_number_to_vp_number(smp_processor_id());
  86. vmbus_connection.connect_cpu = smp_processor_id();
  87. } else {
  88. msg->target_vcpu = 0;
  89. vmbus_connection.connect_cpu = 0;
  90. }
  91. /*
  92. * Add to list before we send the request since we may
  93. * receive the response before returning from this routine
  94. */
  95. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  96. list_add_tail(&msginfo->msglistentry,
  97. &vmbus_connection.chn_msg_list);
  98. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  99. ret = vmbus_post_msg(msg,
  100. sizeof(struct vmbus_channel_initiate_contact),
  101. true);
  102. trace_vmbus_negotiate_version(msg, ret);
  103. if (ret != 0) {
  104. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  105. list_del(&msginfo->msglistentry);
  106. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  107. flags);
  108. return ret;
  109. }
  110. /* Wait for the connection response */
  111. wait_for_completion(&msginfo->waitevent);
  112. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  113. list_del(&msginfo->msglistentry);
  114. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  115. /* Check if successful */
  116. if (msginfo->response.version_response.version_supported) {
  117. vmbus_connection.conn_state = CONNECTED;
  118. } else {
  119. return -ECONNREFUSED;
  120. }
  121. return ret;
  122. }
  123. /*
  124. * vmbus_connect - Sends a connect request on the partition service connection
  125. */
  126. int vmbus_connect(void)
  127. {
  128. int ret = 0;
  129. struct vmbus_channel_msginfo *msginfo = NULL;
  130. __u32 version;
  131. /* Initialize the vmbus connection */
  132. vmbus_connection.conn_state = CONNECTING;
  133. vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
  134. if (!vmbus_connection.work_queue) {
  135. ret = -ENOMEM;
  136. goto cleanup;
  137. }
  138. INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
  139. spin_lock_init(&vmbus_connection.channelmsg_lock);
  140. INIT_LIST_HEAD(&vmbus_connection.chn_list);
  141. mutex_init(&vmbus_connection.channel_mutex);
  142. /*
  143. * Setup the vmbus event connection for channel interrupt
  144. * abstraction stuff
  145. */
  146. vmbus_connection.int_page =
  147. (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
  148. if (vmbus_connection.int_page == NULL) {
  149. ret = -ENOMEM;
  150. goto cleanup;
  151. }
  152. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  153. vmbus_connection.send_int_page =
  154. (void *)((unsigned long)vmbus_connection.int_page +
  155. (PAGE_SIZE >> 1));
  156. /*
  157. * Setup the monitor notification facility. The 1st page for
  158. * parent->child and the 2nd page for child->parent
  159. */
  160. vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  161. vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  162. if ((vmbus_connection.monitor_pages[0] == NULL) ||
  163. (vmbus_connection.monitor_pages[1] == NULL)) {
  164. ret = -ENOMEM;
  165. goto cleanup;
  166. }
  167. msginfo = kzalloc(sizeof(*msginfo) +
  168. sizeof(struct vmbus_channel_initiate_contact),
  169. GFP_KERNEL);
  170. if (msginfo == NULL) {
  171. ret = -ENOMEM;
  172. goto cleanup;
  173. }
  174. /*
  175. * Negotiate a compatible VMBUS version number with the
  176. * host. We start with the highest number we can support
  177. * and work our way down until we negotiate a compatible
  178. * version.
  179. */
  180. version = VERSION_CURRENT;
  181. do {
  182. ret = vmbus_negotiate_version(msginfo, version);
  183. if (ret == -ETIMEDOUT)
  184. goto cleanup;
  185. if (vmbus_connection.conn_state == CONNECTED)
  186. break;
  187. version = vmbus_get_next_version(version);
  188. } while (version != VERSION_INVAL);
  189. if (version == VERSION_INVAL)
  190. goto cleanup;
  191. vmbus_proto_version = version;
  192. pr_info("Vmbus version:%d.%d\n",
  193. version >> 16, version & 0xFFFF);
  194. kfree(msginfo);
  195. return 0;
  196. cleanup:
  197. pr_err("Unable to connect to host\n");
  198. vmbus_connection.conn_state = DISCONNECTED;
  199. vmbus_disconnect();
  200. kfree(msginfo);
  201. return ret;
  202. }
  203. void vmbus_disconnect(void)
  204. {
  205. /*
  206. * First send the unload request to the host.
  207. */
  208. vmbus_initiate_unload(false);
  209. if (vmbus_connection.work_queue) {
  210. drain_workqueue(vmbus_connection.work_queue);
  211. destroy_workqueue(vmbus_connection.work_queue);
  212. }
  213. if (vmbus_connection.int_page) {
  214. free_pages((unsigned long)vmbus_connection.int_page, 0);
  215. vmbus_connection.int_page = NULL;
  216. }
  217. free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
  218. free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
  219. vmbus_connection.monitor_pages[0] = NULL;
  220. vmbus_connection.monitor_pages[1] = NULL;
  221. }
  222. /*
  223. * relid2channel - Get the channel object given its
  224. * child relative id (ie channel id)
  225. */
  226. struct vmbus_channel *relid2channel(u32 relid)
  227. {
  228. struct vmbus_channel *channel;
  229. struct vmbus_channel *found_channel = NULL;
  230. struct list_head *cur, *tmp;
  231. struct vmbus_channel *cur_sc;
  232. BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
  233. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  234. if (channel->offermsg.child_relid == relid) {
  235. found_channel = channel;
  236. break;
  237. } else if (!list_empty(&channel->sc_list)) {
  238. /*
  239. * Deal with sub-channels.
  240. */
  241. list_for_each_safe(cur, tmp, &channel->sc_list) {
  242. cur_sc = list_entry(cur, struct vmbus_channel,
  243. sc_list);
  244. if (cur_sc->offermsg.child_relid == relid) {
  245. found_channel = cur_sc;
  246. break;
  247. }
  248. }
  249. }
  250. }
  251. return found_channel;
  252. }
  253. /*
  254. * vmbus_on_event - Process a channel event notification
  255. *
  256. * For batched channels (default) optimize host to guest signaling
  257. * by ensuring:
  258. * 1. While reading the channel, we disable interrupts from host.
  259. * 2. Ensure that we process all posted messages from the host
  260. * before returning from this callback.
  261. * 3. Once we return, enable signaling from the host. Once this
  262. * state is set we check to see if additional packets are
  263. * available to read. In this case we repeat the process.
  264. * If this tasklet has been running for a long time
  265. * then reschedule ourselves.
  266. */
  267. void vmbus_on_event(unsigned long data)
  268. {
  269. struct vmbus_channel *channel = (void *) data;
  270. unsigned long time_limit = jiffies + 2;
  271. trace_vmbus_on_event(channel);
  272. do {
  273. void (*callback_fn)(void *);
  274. /* A channel once created is persistent even when
  275. * there is no driver handling the device. An
  276. * unloading driver sets the onchannel_callback to NULL.
  277. */
  278. callback_fn = READ_ONCE(channel->onchannel_callback);
  279. if (unlikely(callback_fn == NULL))
  280. return;
  281. (*callback_fn)(channel->channel_callback_context);
  282. if (channel->callback_mode != HV_CALL_BATCHED)
  283. return;
  284. if (likely(hv_end_read(&channel->inbound) == 0))
  285. return;
  286. hv_begin_read(&channel->inbound);
  287. } while (likely(time_before(jiffies, time_limit)));
  288. /* The time limit (2 jiffies) has been reached */
  289. tasklet_schedule(&channel->callback_event);
  290. }
  291. /*
  292. * vmbus_post_msg - Send a msg on the vmbus's message connection
  293. */
  294. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
  295. {
  296. union hv_connection_id conn_id;
  297. int ret = 0;
  298. int retries = 0;
  299. u32 usec = 1;
  300. conn_id.asu32 = 0;
  301. conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
  302. /*
  303. * hv_post_message() can have transient failures because of
  304. * insufficient resources. Retry the operation a couple of
  305. * times before giving up.
  306. */
  307. while (retries < 100) {
  308. ret = hv_post_message(conn_id, 1, buffer, buflen);
  309. switch (ret) {
  310. case HV_STATUS_INVALID_CONNECTION_ID:
  311. /*
  312. * We could get this if we send messages too
  313. * frequently.
  314. */
  315. ret = -EAGAIN;
  316. break;
  317. case HV_STATUS_INSUFFICIENT_MEMORY:
  318. case HV_STATUS_INSUFFICIENT_BUFFERS:
  319. ret = -ENOBUFS;
  320. break;
  321. case HV_STATUS_SUCCESS:
  322. return ret;
  323. default:
  324. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  325. return -EINVAL;
  326. }
  327. retries++;
  328. if (can_sleep && usec > 1000)
  329. msleep(usec / 1000);
  330. else if (usec < MAX_UDELAY_MS * 1000)
  331. udelay(usec);
  332. else
  333. mdelay(usec / 1000);
  334. if (retries < 22)
  335. usec *= 2;
  336. }
  337. return ret;
  338. }
  339. /*
  340. * vmbus_set_event - Send an event notification to the parent
  341. */
  342. void vmbus_set_event(struct vmbus_channel *channel)
  343. {
  344. u32 child_relid = channel->offermsg.child_relid;
  345. if (!channel->is_dedicated_interrupt)
  346. vmbus_send_interrupt(child_relid);
  347. ++channel->sig_events;
  348. hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
  349. }
  350. EXPORT_SYMBOL_GPL(vmbus_set_event);