connection.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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_WIN10_V5):
  57. return VERSION_WIN10;
  58. case (VERSION_WS2008):
  59. default:
  60. return VERSION_INVAL;
  61. }
  62. }
  63. static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo,
  64. __u32 version)
  65. {
  66. int ret = 0;
  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. memset(msg, 0, sizeof(*msg));
  72. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  73. msg->vmbus_version_requested = version;
  74. /*
  75. * VMBus protocol 5.0 (VERSION_WIN10_V5) requires that we must use
  76. * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
  77. * and for subsequent messages, we must use the Message Connection ID
  78. * field in the host-returned Version Response Message. And, with
  79. * VERSION_WIN10_V5, we don't use msg->interrupt_page, but we tell
  80. * the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
  81. * compatibility.
  82. *
  83. * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
  84. */
  85. if (version >= VERSION_WIN10_V5) {
  86. msg->msg_sint = VMBUS_MESSAGE_SINT;
  87. vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
  88. } else {
  89. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  90. vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
  91. }
  92. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
  93. msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
  94. /*
  95. * We want all channel messages to be delivered on CPU 0.
  96. * This has been the behavior pre-win8. This is not
  97. * perf issue and having all channel messages delivered on CPU 0
  98. * would be ok.
  99. * For post win8 hosts, we support receiving channel messagges on
  100. * all the CPUs. This is needed for kexec to work correctly where
  101. * the CPU attempting to connect may not be CPU 0.
  102. */
  103. if (version >= VERSION_WIN8_1) {
  104. msg->target_vcpu =
  105. hv_cpu_number_to_vp_number(smp_processor_id());
  106. vmbus_connection.connect_cpu = smp_processor_id();
  107. } else {
  108. msg->target_vcpu = 0;
  109. vmbus_connection.connect_cpu = 0;
  110. }
  111. /*
  112. * Add to list before we send the request since we may
  113. * receive the response before returning from this routine
  114. */
  115. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  116. list_add_tail(&msginfo->msglistentry,
  117. &vmbus_connection.chn_msg_list);
  118. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  119. ret = vmbus_post_msg(msg,
  120. sizeof(struct vmbus_channel_initiate_contact),
  121. true);
  122. trace_vmbus_negotiate_version(msg, ret);
  123. if (ret != 0) {
  124. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  125. list_del(&msginfo->msglistentry);
  126. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  127. flags);
  128. return ret;
  129. }
  130. /* Wait for the connection response */
  131. wait_for_completion(&msginfo->waitevent);
  132. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  133. list_del(&msginfo->msglistentry);
  134. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  135. /* Check if successful */
  136. if (msginfo->response.version_response.version_supported) {
  137. vmbus_connection.conn_state = CONNECTED;
  138. if (version >= VERSION_WIN10_V5)
  139. vmbus_connection.msg_conn_id =
  140. msginfo->response.version_response.msg_conn_id;
  141. } else {
  142. return -ECONNREFUSED;
  143. }
  144. return ret;
  145. }
  146. /*
  147. * vmbus_connect - Sends a connect request on the partition service connection
  148. */
  149. int vmbus_connect(void)
  150. {
  151. int ret = 0;
  152. struct vmbus_channel_msginfo *msginfo = NULL;
  153. __u32 version;
  154. /* Initialize the vmbus connection */
  155. vmbus_connection.conn_state = CONNECTING;
  156. vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
  157. if (!vmbus_connection.work_queue) {
  158. ret = -ENOMEM;
  159. goto cleanup;
  160. }
  161. INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
  162. spin_lock_init(&vmbus_connection.channelmsg_lock);
  163. INIT_LIST_HEAD(&vmbus_connection.chn_list);
  164. mutex_init(&vmbus_connection.channel_mutex);
  165. /*
  166. * Setup the vmbus event connection for channel interrupt
  167. * abstraction stuff
  168. */
  169. vmbus_connection.int_page =
  170. (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
  171. if (vmbus_connection.int_page == NULL) {
  172. ret = -ENOMEM;
  173. goto cleanup;
  174. }
  175. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  176. vmbus_connection.send_int_page =
  177. (void *)((unsigned long)vmbus_connection.int_page +
  178. (PAGE_SIZE >> 1));
  179. /*
  180. * Setup the monitor notification facility. The 1st page for
  181. * parent->child and the 2nd page for child->parent
  182. */
  183. vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  184. vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  185. if ((vmbus_connection.monitor_pages[0] == NULL) ||
  186. (vmbus_connection.monitor_pages[1] == NULL)) {
  187. ret = -ENOMEM;
  188. goto cleanup;
  189. }
  190. msginfo = kzalloc(sizeof(*msginfo) +
  191. sizeof(struct vmbus_channel_initiate_contact),
  192. GFP_KERNEL);
  193. if (msginfo == NULL) {
  194. ret = -ENOMEM;
  195. goto cleanup;
  196. }
  197. /*
  198. * Negotiate a compatible VMBUS version number with the
  199. * host. We start with the highest number we can support
  200. * and work our way down until we negotiate a compatible
  201. * version.
  202. */
  203. version = VERSION_CURRENT;
  204. do {
  205. ret = vmbus_negotiate_version(msginfo, version);
  206. if (ret == -ETIMEDOUT)
  207. goto cleanup;
  208. if (vmbus_connection.conn_state == CONNECTED)
  209. break;
  210. version = vmbus_get_next_version(version);
  211. } while (version != VERSION_INVAL);
  212. if (version == VERSION_INVAL)
  213. goto cleanup;
  214. vmbus_proto_version = version;
  215. pr_info("Vmbus version:%d.%d\n",
  216. version >> 16, version & 0xFFFF);
  217. kfree(msginfo);
  218. return 0;
  219. cleanup:
  220. pr_err("Unable to connect to host\n");
  221. vmbus_connection.conn_state = DISCONNECTED;
  222. vmbus_disconnect();
  223. kfree(msginfo);
  224. return ret;
  225. }
  226. void vmbus_disconnect(void)
  227. {
  228. /*
  229. * First send the unload request to the host.
  230. */
  231. vmbus_initiate_unload(false);
  232. if (vmbus_connection.work_queue) {
  233. drain_workqueue(vmbus_connection.work_queue);
  234. destroy_workqueue(vmbus_connection.work_queue);
  235. }
  236. if (vmbus_connection.int_page) {
  237. free_pages((unsigned long)vmbus_connection.int_page, 0);
  238. vmbus_connection.int_page = NULL;
  239. }
  240. free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
  241. free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
  242. vmbus_connection.monitor_pages[0] = NULL;
  243. vmbus_connection.monitor_pages[1] = NULL;
  244. }
  245. /*
  246. * relid2channel - Get the channel object given its
  247. * child relative id (ie channel id)
  248. */
  249. struct vmbus_channel *relid2channel(u32 relid)
  250. {
  251. struct vmbus_channel *channel;
  252. struct vmbus_channel *found_channel = NULL;
  253. struct list_head *cur, *tmp;
  254. struct vmbus_channel *cur_sc;
  255. BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
  256. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  257. if (channel->offermsg.child_relid == relid) {
  258. found_channel = channel;
  259. break;
  260. } else if (!list_empty(&channel->sc_list)) {
  261. /*
  262. * Deal with sub-channels.
  263. */
  264. list_for_each_safe(cur, tmp, &channel->sc_list) {
  265. cur_sc = list_entry(cur, struct vmbus_channel,
  266. sc_list);
  267. if (cur_sc->offermsg.child_relid == relid) {
  268. found_channel = cur_sc;
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. return found_channel;
  275. }
  276. /*
  277. * vmbus_on_event - Process a channel event notification
  278. *
  279. * For batched channels (default) optimize host to guest signaling
  280. * by ensuring:
  281. * 1. While reading the channel, we disable interrupts from host.
  282. * 2. Ensure that we process all posted messages from the host
  283. * before returning from this callback.
  284. * 3. Once we return, enable signaling from the host. Once this
  285. * state is set we check to see if additional packets are
  286. * available to read. In this case we repeat the process.
  287. * If this tasklet has been running for a long time
  288. * then reschedule ourselves.
  289. */
  290. void vmbus_on_event(unsigned long data)
  291. {
  292. struct vmbus_channel *channel = (void *) data;
  293. unsigned long time_limit = jiffies + 2;
  294. trace_vmbus_on_event(channel);
  295. do {
  296. void (*callback_fn)(void *);
  297. /* A channel once created is persistent even when
  298. * there is no driver handling the device. An
  299. * unloading driver sets the onchannel_callback to NULL.
  300. */
  301. callback_fn = READ_ONCE(channel->onchannel_callback);
  302. if (unlikely(callback_fn == NULL))
  303. return;
  304. (*callback_fn)(channel->channel_callback_context);
  305. if (channel->callback_mode != HV_CALL_BATCHED)
  306. return;
  307. if (likely(hv_end_read(&channel->inbound) == 0))
  308. return;
  309. hv_begin_read(&channel->inbound);
  310. } while (likely(time_before(jiffies, time_limit)));
  311. /* The time limit (2 jiffies) has been reached */
  312. tasklet_schedule(&channel->callback_event);
  313. }
  314. /*
  315. * vmbus_post_msg - Send a msg on the vmbus's message connection
  316. */
  317. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
  318. {
  319. struct vmbus_channel_message_header *hdr;
  320. union hv_connection_id conn_id;
  321. int ret = 0;
  322. int retries = 0;
  323. u32 usec = 1;
  324. conn_id.asu32 = 0;
  325. conn_id.u.id = vmbus_connection.msg_conn_id;
  326. /*
  327. * hv_post_message() can have transient failures because of
  328. * insufficient resources. Retry the operation a couple of
  329. * times before giving up.
  330. */
  331. while (retries < 100) {
  332. ret = hv_post_message(conn_id, 1, buffer, buflen);
  333. switch (ret) {
  334. case HV_STATUS_INVALID_CONNECTION_ID:
  335. /*
  336. * See vmbus_negotiate_version(): VMBus protocol 5.0
  337. * requires that we must use
  338. * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
  339. * Contact message, but on old hosts that only
  340. * support VMBus protocol 4.0 or lower, here we get
  341. * HV_STATUS_INVALID_CONNECTION_ID and we should
  342. * return an error immediately without retrying.
  343. */
  344. hdr = buffer;
  345. if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
  346. return -EINVAL;
  347. /*
  348. * We could get this if we send messages too
  349. * frequently.
  350. */
  351. ret = -EAGAIN;
  352. break;
  353. case HV_STATUS_INSUFFICIENT_MEMORY:
  354. case HV_STATUS_INSUFFICIENT_BUFFERS:
  355. ret = -ENOBUFS;
  356. break;
  357. case HV_STATUS_SUCCESS:
  358. return ret;
  359. default:
  360. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  361. return -EINVAL;
  362. }
  363. retries++;
  364. if (can_sleep && usec > 1000)
  365. msleep(usec / 1000);
  366. else if (usec < MAX_UDELAY_MS * 1000)
  367. udelay(usec);
  368. else
  369. mdelay(usec / 1000);
  370. if (retries < 22)
  371. usec *= 2;
  372. }
  373. return ret;
  374. }
  375. /*
  376. * vmbus_set_event - Send an event notification to the parent
  377. */
  378. void vmbus_set_event(struct vmbus_channel *channel)
  379. {
  380. u32 child_relid = channel->offermsg.child_relid;
  381. if (!channel->is_dedicated_interrupt)
  382. vmbus_send_interrupt(child_relid);
  383. ++channel->sig_events;
  384. hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
  385. }
  386. EXPORT_SYMBOL_GPL(vmbus_set_event);