connection.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/hyperv.h>
  34. #include <asm/mshyperv.h>
  35. #include "hyperv_vmbus.h"
  36. struct vmbus_connection vmbus_connection = {
  37. .conn_state = DISCONNECTED,
  38. .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
  39. };
  40. EXPORT_SYMBOL_GPL(vmbus_connection);
  41. /*
  42. * Negotiated protocol version with the host.
  43. */
  44. __u32 vmbus_proto_version;
  45. EXPORT_SYMBOL_GPL(vmbus_proto_version);
  46. static __u32 vmbus_get_next_version(__u32 current_version)
  47. {
  48. switch (current_version) {
  49. case (VERSION_WIN7):
  50. return VERSION_WS2008;
  51. case (VERSION_WIN8):
  52. return VERSION_WIN7;
  53. case (VERSION_WIN8_1):
  54. return VERSION_WIN8;
  55. case (VERSION_WIN10):
  56. return VERSION_WIN8_1;
  57. case (VERSION_WS2008):
  58. default:
  59. return VERSION_INVAL;
  60. }
  61. }
  62. static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo,
  63. __u32 version)
  64. {
  65. int ret = 0;
  66. unsigned int cur_cpu;
  67. struct vmbus_channel_initiate_contact *msg;
  68. unsigned long flags;
  69. init_completion(&msginfo->waitevent);
  70. msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
  71. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  72. msg->vmbus_version_requested = version;
  73. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  74. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
  75. msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
  76. /*
  77. * We want all channel messages to be delivered on CPU 0.
  78. * This has been the behavior pre-win8. This is not
  79. * perf issue and having all channel messages delivered on CPU 0
  80. * would be ok.
  81. * For post win8 hosts, we support receiving channel messagges on
  82. * all the CPUs. This is needed for kexec to work correctly where
  83. * the CPU attempting to connect may not be CPU 0.
  84. */
  85. if (version >= VERSION_WIN8_1) {
  86. cur_cpu = get_cpu();
  87. msg->target_vcpu = hv_cpu_number_to_vp_number(cur_cpu);
  88. vmbus_connection.connect_cpu = cur_cpu;
  89. put_cpu();
  90. } else {
  91. msg->target_vcpu = 0;
  92. vmbus_connection.connect_cpu = 0;
  93. }
  94. /*
  95. * Add to list before we send the request since we may
  96. * receive the response before returning from this routine
  97. */
  98. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  99. list_add_tail(&msginfo->msglistentry,
  100. &vmbus_connection.chn_msg_list);
  101. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  102. ret = vmbus_post_msg(msg,
  103. sizeof(struct vmbus_channel_initiate_contact),
  104. true);
  105. if (ret != 0) {
  106. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  107. list_del(&msginfo->msglistentry);
  108. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  109. flags);
  110. return ret;
  111. }
  112. /* Wait for the connection response */
  113. wait_for_completion(&msginfo->waitevent);
  114. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  115. list_del(&msginfo->msglistentry);
  116. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  117. /* Check if successful */
  118. if (msginfo->response.version_response.version_supported) {
  119. vmbus_connection.conn_state = CONNECTED;
  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. int ret = 0;
  131. struct vmbus_channel_msginfo *msginfo = NULL;
  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 *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
  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. (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 *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  175. vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  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. version = VERSION_CURRENT;
  195. do {
  196. ret = vmbus_negotiate_version(msginfo, version);
  197. if (ret == -ETIMEDOUT)
  198. goto cleanup;
  199. if (vmbus_connection.conn_state == CONNECTED)
  200. break;
  201. version = vmbus_get_next_version(version);
  202. } while (version != VERSION_INVAL);
  203. if (version == VERSION_INVAL)
  204. goto cleanup;
  205. vmbus_proto_version = version;
  206. pr_info("Vmbus version:%d.%d\n",
  207. version >> 16, version & 0xFFFF);
  208. kfree(msginfo);
  209. return 0;
  210. cleanup:
  211. pr_err("Unable to connect to host\n");
  212. vmbus_connection.conn_state = DISCONNECTED;
  213. vmbus_disconnect();
  214. kfree(msginfo);
  215. return ret;
  216. }
  217. void vmbus_disconnect(void)
  218. {
  219. /*
  220. * First send the unload request to the host.
  221. */
  222. vmbus_initiate_unload(false);
  223. if (vmbus_connection.handle_sub_chan_wq)
  224. destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
  225. if (vmbus_connection.handle_primary_chan_wq)
  226. destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
  227. if (vmbus_connection.work_queue)
  228. destroy_workqueue(vmbus_connection.work_queue);
  229. if (vmbus_connection.int_page) {
  230. free_pages((unsigned long)vmbus_connection.int_page, 0);
  231. vmbus_connection.int_page = NULL;
  232. }
  233. free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
  234. free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
  235. vmbus_connection.monitor_pages[0] = NULL;
  236. vmbus_connection.monitor_pages[1] = NULL;
  237. }
  238. /*
  239. * relid2channel - Get the channel object given its
  240. * child relative id (ie channel id)
  241. */
  242. struct vmbus_channel *relid2channel(u32 relid)
  243. {
  244. struct vmbus_channel *channel;
  245. struct vmbus_channel *found_channel = NULL;
  246. struct list_head *cur, *tmp;
  247. struct vmbus_channel *cur_sc;
  248. BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
  249. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  250. if (channel->offermsg.child_relid == relid) {
  251. found_channel = channel;
  252. break;
  253. } else if (!list_empty(&channel->sc_list)) {
  254. /*
  255. * Deal with sub-channels.
  256. */
  257. list_for_each_safe(cur, tmp, &channel->sc_list) {
  258. cur_sc = list_entry(cur, struct vmbus_channel,
  259. sc_list);
  260. if (cur_sc->offermsg.child_relid == relid) {
  261. found_channel = cur_sc;
  262. break;
  263. }
  264. }
  265. }
  266. }
  267. return found_channel;
  268. }
  269. /*
  270. * vmbus_on_event - Process a channel event notification
  271. *
  272. * For batched channels (default) optimize host to guest signaling
  273. * by ensuring:
  274. * 1. While reading the channel, we disable interrupts from host.
  275. * 2. Ensure that we process all posted messages from the host
  276. * before returning from this callback.
  277. * 3. Once we return, enable signaling from the host. Once this
  278. * state is set we check to see if additional packets are
  279. * available to read. In this case we repeat the process.
  280. * If this tasklet has been running for a long time
  281. * then reschedule ourselves.
  282. */
  283. void vmbus_on_event(unsigned long data)
  284. {
  285. struct vmbus_channel *channel = (void *) data;
  286. unsigned long time_limit = jiffies + 2;
  287. do {
  288. void (*callback_fn)(void *);
  289. /* A channel once created is persistent even when
  290. * there is no driver handling the device. An
  291. * unloading driver sets the onchannel_callback to NULL.
  292. */
  293. callback_fn = READ_ONCE(channel->onchannel_callback);
  294. if (unlikely(callback_fn == NULL))
  295. return;
  296. (*callback_fn)(channel->channel_callback_context);
  297. if (channel->callback_mode != HV_CALL_BATCHED)
  298. return;
  299. if (likely(hv_end_read(&channel->inbound) == 0))
  300. return;
  301. hv_begin_read(&channel->inbound);
  302. } while (likely(time_before(jiffies, time_limit)));
  303. /* The time limit (2 jiffies) has been reached */
  304. tasklet_schedule(&channel->callback_event);
  305. }
  306. /*
  307. * vmbus_post_msg - Send a msg on the vmbus's message connection
  308. */
  309. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
  310. {
  311. union hv_connection_id conn_id;
  312. int ret = 0;
  313. int retries = 0;
  314. u32 usec = 1;
  315. conn_id.asu32 = 0;
  316. conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
  317. /*
  318. * hv_post_message() can have transient failures because of
  319. * insufficient resources. Retry the operation a couple of
  320. * times before giving up.
  321. */
  322. while (retries < 100) {
  323. ret = hv_post_message(conn_id, 1, buffer, buflen);
  324. switch (ret) {
  325. case HV_STATUS_INVALID_CONNECTION_ID:
  326. /*
  327. * We could get this if we send messages too
  328. * frequently.
  329. */
  330. ret = -EAGAIN;
  331. break;
  332. case HV_STATUS_INSUFFICIENT_MEMORY:
  333. case HV_STATUS_INSUFFICIENT_BUFFERS:
  334. ret = -ENOBUFS;
  335. break;
  336. case HV_STATUS_SUCCESS:
  337. return ret;
  338. default:
  339. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  340. return -EINVAL;
  341. }
  342. retries++;
  343. if (can_sleep && usec > 1000)
  344. msleep(usec / 1000);
  345. else if (usec < MAX_UDELAY_MS * 1000)
  346. udelay(usec);
  347. else
  348. mdelay(usec / 1000);
  349. if (retries < 22)
  350. usec *= 2;
  351. }
  352. return ret;
  353. }
  354. /*
  355. * vmbus_set_event - Send an event notification to the parent
  356. */
  357. void vmbus_set_event(struct vmbus_channel *channel)
  358. {
  359. u32 child_relid = channel->offermsg.child_relid;
  360. if (!channel->is_dedicated_interrupt)
  361. vmbus_send_interrupt(child_relid);
  362. hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
  363. }
  364. EXPORT_SYMBOL_GPL(vmbus_set_event);