connection.c 13 KB

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