connection.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (c) 2009, Microsoft Corporation.
  5. *
  6. * Authors:
  7. * Haiyang Zhang <[email protected]>
  8. * Hank Janssen <[email protected]>
  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 <linux/io.h>
  22. #include <linux/set_memory.h>
  23. #include <asm/mem_encrypt.h>
  24. #include <asm/mshyperv.h>
  25. #include "hyperv_vmbus.h"
  26. struct vmbus_connection vmbus_connection = {
  27. .conn_state = DISCONNECTED,
  28. .unload_event = COMPLETION_INITIALIZER(
  29. vmbus_connection.unload_event),
  30. .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
  31. .ready_for_suspend_event = COMPLETION_INITIALIZER(
  32. vmbus_connection.ready_for_suspend_event),
  33. .ready_for_resume_event = COMPLETION_INITIALIZER(
  34. vmbus_connection.ready_for_resume_event),
  35. };
  36. EXPORT_SYMBOL_GPL(vmbus_connection);
  37. /*
  38. * Negotiated protocol version with the host.
  39. */
  40. __u32 vmbus_proto_version;
  41. EXPORT_SYMBOL_GPL(vmbus_proto_version);
  42. /*
  43. * Table of VMBus versions listed from newest to oldest.
  44. * VERSION_WIN7 and VERSION_WS2008 are no longer supported in
  45. * Linux guests and are not listed.
  46. */
  47. static __u32 vmbus_versions[] = {
  48. VERSION_WIN10_V5_3,
  49. VERSION_WIN10_V5_2,
  50. VERSION_WIN10_V5_1,
  51. VERSION_WIN10_V5,
  52. VERSION_WIN10_V4_1,
  53. VERSION_WIN10,
  54. VERSION_WIN8_1,
  55. VERSION_WIN8
  56. };
  57. /*
  58. * Maximal VMBus protocol version guests can negotiate. Useful to cap the
  59. * VMBus version for testing and debugging purpose.
  60. */
  61. static uint max_version = VERSION_WIN10_V5_3;
  62. module_param(max_version, uint, S_IRUGO);
  63. MODULE_PARM_DESC(max_version,
  64. "Maximal VMBus protocol version which can be negotiated");
  65. int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
  66. {
  67. int ret = 0;
  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) and higher require that we must
  77. * use 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 and higher, we don't use msg->interrupt_page, but we
  81. * tell 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 = vmbus_connection.monitor_pages_pa[0];
  94. msg->monitor_page2 = vmbus_connection.monitor_pages_pa[1];
  95. msg->target_vcpu = hv_cpu_number_to_vp_number(VMBUS_CONNECT_CPU);
  96. /*
  97. * Add to list before we send the request since we may
  98. * receive the response before returning from this routine
  99. */
  100. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  101. list_add_tail(&msginfo->msglistentry,
  102. &vmbus_connection.chn_msg_list);
  103. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  104. ret = vmbus_post_msg(msg,
  105. sizeof(struct vmbus_channel_initiate_contact),
  106. true);
  107. trace_vmbus_negotiate_version(msg, ret);
  108. if (ret != 0) {
  109. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  110. list_del(&msginfo->msglistentry);
  111. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  112. flags);
  113. return ret;
  114. }
  115. /* Wait for the connection response */
  116. wait_for_completion(&msginfo->waitevent);
  117. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  118. list_del(&msginfo->msglistentry);
  119. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  120. /* Check if successful */
  121. if (msginfo->response.version_response.version_supported) {
  122. vmbus_connection.conn_state = CONNECTED;
  123. if (version >= VERSION_WIN10_V5)
  124. vmbus_connection.msg_conn_id =
  125. msginfo->response.version_response.msg_conn_id;
  126. } else {
  127. return -ECONNREFUSED;
  128. }
  129. return ret;
  130. }
  131. /*
  132. * vmbus_connect - Sends a connect request on the partition service connection
  133. */
  134. int vmbus_connect(void)
  135. {
  136. struct vmbus_channel_msginfo *msginfo = NULL;
  137. int i, ret = 0;
  138. __u32 version;
  139. /* Initialize the vmbus connection */
  140. vmbus_connection.conn_state = CONNECTING;
  141. vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
  142. if (!vmbus_connection.work_queue) {
  143. ret = -ENOMEM;
  144. goto cleanup;
  145. }
  146. vmbus_connection.rescind_work_queue =
  147. create_workqueue("hv_vmbus_rescind");
  148. if (!vmbus_connection.rescind_work_queue) {
  149. ret = -ENOMEM;
  150. goto cleanup;
  151. }
  152. vmbus_connection.ignore_any_offer_msg = false;
  153. vmbus_connection.handle_primary_chan_wq =
  154. create_workqueue("hv_pri_chan");
  155. if (!vmbus_connection.handle_primary_chan_wq) {
  156. ret = -ENOMEM;
  157. goto cleanup;
  158. }
  159. vmbus_connection.handle_sub_chan_wq =
  160. create_workqueue("hv_sub_chan");
  161. if (!vmbus_connection.handle_sub_chan_wq) {
  162. ret = -ENOMEM;
  163. goto cleanup;
  164. }
  165. INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
  166. spin_lock_init(&vmbus_connection.channelmsg_lock);
  167. INIT_LIST_HEAD(&vmbus_connection.chn_list);
  168. mutex_init(&vmbus_connection.channel_mutex);
  169. /*
  170. * Setup the vmbus event connection for channel interrupt
  171. * abstraction stuff
  172. */
  173. vmbus_connection.int_page =
  174. (void *)hv_alloc_hyperv_zeroed_page();
  175. if (vmbus_connection.int_page == NULL) {
  176. ret = -ENOMEM;
  177. goto cleanup;
  178. }
  179. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  180. vmbus_connection.send_int_page =
  181. (void *)((unsigned long)vmbus_connection.int_page +
  182. (HV_HYP_PAGE_SIZE >> 1));
  183. /*
  184. * Setup the monitor notification facility. The 1st page for
  185. * parent->child and the 2nd page for child->parent
  186. */
  187. vmbus_connection.monitor_pages[0] = (void *)hv_alloc_hyperv_zeroed_page();
  188. vmbus_connection.monitor_pages[1] = (void *)hv_alloc_hyperv_zeroed_page();
  189. if ((vmbus_connection.monitor_pages[0] == NULL) ||
  190. (vmbus_connection.monitor_pages[1] == NULL)) {
  191. ret = -ENOMEM;
  192. goto cleanup;
  193. }
  194. vmbus_connection.monitor_pages_original[0]
  195. = vmbus_connection.monitor_pages[0];
  196. vmbus_connection.monitor_pages_original[1]
  197. = vmbus_connection.monitor_pages[1];
  198. vmbus_connection.monitor_pages_pa[0]
  199. = virt_to_phys(vmbus_connection.monitor_pages[0]);
  200. vmbus_connection.monitor_pages_pa[1]
  201. = virt_to_phys(vmbus_connection.monitor_pages[1]);
  202. if (hv_is_isolation_supported()) {
  203. ret = set_memory_decrypted((unsigned long)
  204. vmbus_connection.monitor_pages[0],
  205. 1);
  206. ret |= set_memory_decrypted((unsigned long)
  207. vmbus_connection.monitor_pages[1],
  208. 1);
  209. if (ret)
  210. goto cleanup;
  211. /*
  212. * Isolation VM with AMD SNP needs to access monitor page via
  213. * address space above shared gpa boundary.
  214. */
  215. if (hv_isolation_type_snp()) {
  216. vmbus_connection.monitor_pages_pa[0] +=
  217. ms_hyperv.shared_gpa_boundary;
  218. vmbus_connection.monitor_pages_pa[1] +=
  219. ms_hyperv.shared_gpa_boundary;
  220. vmbus_connection.monitor_pages[0]
  221. = memremap(vmbus_connection.monitor_pages_pa[0],
  222. HV_HYP_PAGE_SIZE,
  223. MEMREMAP_WB);
  224. if (!vmbus_connection.monitor_pages[0]) {
  225. ret = -ENOMEM;
  226. goto cleanup;
  227. }
  228. vmbus_connection.monitor_pages[1]
  229. = memremap(vmbus_connection.monitor_pages_pa[1],
  230. HV_HYP_PAGE_SIZE,
  231. MEMREMAP_WB);
  232. if (!vmbus_connection.monitor_pages[1]) {
  233. ret = -ENOMEM;
  234. goto cleanup;
  235. }
  236. }
  237. /*
  238. * Set memory host visibility hvcall smears memory
  239. * and so zero monitor pages here.
  240. */
  241. memset(vmbus_connection.monitor_pages[0], 0x00,
  242. HV_HYP_PAGE_SIZE);
  243. memset(vmbus_connection.monitor_pages[1], 0x00,
  244. HV_HYP_PAGE_SIZE);
  245. }
  246. msginfo = kzalloc(sizeof(*msginfo) +
  247. sizeof(struct vmbus_channel_initiate_contact),
  248. GFP_KERNEL);
  249. if (msginfo == NULL) {
  250. ret = -ENOMEM;
  251. goto cleanup;
  252. }
  253. /*
  254. * Negotiate a compatible VMBUS version number with the
  255. * host. We start with the highest number we can support
  256. * and work our way down until we negotiate a compatible
  257. * version.
  258. */
  259. for (i = 0; ; i++) {
  260. if (i == ARRAY_SIZE(vmbus_versions)) {
  261. ret = -EDOM;
  262. goto cleanup;
  263. }
  264. version = vmbus_versions[i];
  265. if (version > max_version)
  266. continue;
  267. ret = vmbus_negotiate_version(msginfo, version);
  268. if (ret == -ETIMEDOUT)
  269. goto cleanup;
  270. if (vmbus_connection.conn_state == CONNECTED)
  271. break;
  272. }
  273. if (hv_is_isolation_supported() && version < VERSION_WIN10_V5_2) {
  274. pr_err("Invalid VMBus version %d.%d (expected >= %d.%d) from the host supporting isolation\n",
  275. version >> 16, version & 0xFFFF, VERSION_WIN10_V5_2 >> 16, VERSION_WIN10_V5_2 & 0xFFFF);
  276. ret = -EINVAL;
  277. goto cleanup;
  278. }
  279. vmbus_proto_version = version;
  280. pr_info("Vmbus version:%d.%d\n",
  281. version >> 16, version & 0xFFFF);
  282. vmbus_connection.channels = kcalloc(MAX_CHANNEL_RELIDS,
  283. sizeof(struct vmbus_channel *),
  284. GFP_KERNEL);
  285. if (vmbus_connection.channels == NULL) {
  286. ret = -ENOMEM;
  287. goto cleanup;
  288. }
  289. kfree(msginfo);
  290. return 0;
  291. cleanup:
  292. pr_err("Unable to connect to host\n");
  293. vmbus_connection.conn_state = DISCONNECTED;
  294. vmbus_disconnect();
  295. kfree(msginfo);
  296. return ret;
  297. }
  298. void vmbus_disconnect(void)
  299. {
  300. /*
  301. * First send the unload request to the host.
  302. */
  303. vmbus_initiate_unload(false);
  304. if (vmbus_connection.handle_sub_chan_wq)
  305. destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
  306. if (vmbus_connection.handle_primary_chan_wq)
  307. destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
  308. if (vmbus_connection.rescind_work_queue)
  309. destroy_workqueue(vmbus_connection.rescind_work_queue);
  310. if (vmbus_connection.work_queue)
  311. destroy_workqueue(vmbus_connection.work_queue);
  312. if (vmbus_connection.int_page) {
  313. hv_free_hyperv_page((unsigned long)vmbus_connection.int_page);
  314. vmbus_connection.int_page = NULL;
  315. }
  316. if (hv_is_isolation_supported()) {
  317. /*
  318. * memunmap() checks input address is ioremap address or not
  319. * inside. It doesn't unmap any thing in the non-SNP CVM and
  320. * so not check CVM type here.
  321. */
  322. memunmap(vmbus_connection.monitor_pages[0]);
  323. memunmap(vmbus_connection.monitor_pages[1]);
  324. set_memory_encrypted((unsigned long)
  325. vmbus_connection.monitor_pages_original[0],
  326. 1);
  327. set_memory_encrypted((unsigned long)
  328. vmbus_connection.monitor_pages_original[1],
  329. 1);
  330. }
  331. hv_free_hyperv_page((unsigned long)
  332. vmbus_connection.monitor_pages_original[0]);
  333. hv_free_hyperv_page((unsigned long)
  334. vmbus_connection.monitor_pages_original[1]);
  335. vmbus_connection.monitor_pages_original[0] =
  336. vmbus_connection.monitor_pages[0] = NULL;
  337. vmbus_connection.monitor_pages_original[1] =
  338. vmbus_connection.monitor_pages[1] = NULL;
  339. }
  340. /*
  341. * relid2channel - Get the channel object given its
  342. * child relative id (ie channel id)
  343. */
  344. struct vmbus_channel *relid2channel(u32 relid)
  345. {
  346. if (vmbus_connection.channels == NULL) {
  347. pr_warn_once("relid2channel: relid=%d: No channels mapped!\n", relid);
  348. return NULL;
  349. }
  350. if (WARN_ON(relid >= MAX_CHANNEL_RELIDS))
  351. return NULL;
  352. return READ_ONCE(vmbus_connection.channels[relid]);
  353. }
  354. /*
  355. * vmbus_on_event - Process a channel event notification
  356. *
  357. * For batched channels (default) optimize host to guest signaling
  358. * by ensuring:
  359. * 1. While reading the channel, we disable interrupts from host.
  360. * 2. Ensure that we process all posted messages from the host
  361. * before returning from this callback.
  362. * 3. Once we return, enable signaling from the host. Once this
  363. * state is set we check to see if additional packets are
  364. * available to read. In this case we repeat the process.
  365. * If this tasklet has been running for a long time
  366. * then reschedule ourselves.
  367. */
  368. void vmbus_on_event(unsigned long data)
  369. {
  370. struct vmbus_channel *channel = (void *) data;
  371. void (*callback_fn)(void *context);
  372. trace_vmbus_on_event(channel);
  373. hv_debug_delay_test(channel, INTERRUPT_DELAY);
  374. /* A channel once created is persistent even when
  375. * there is no driver handling the device. An
  376. * unloading driver sets the onchannel_callback to NULL.
  377. */
  378. callback_fn = READ_ONCE(channel->onchannel_callback);
  379. if (unlikely(!callback_fn))
  380. return;
  381. (*callback_fn)(channel->channel_callback_context);
  382. if (channel->callback_mode != HV_CALL_BATCHED)
  383. return;
  384. if (likely(hv_end_read(&channel->inbound) == 0))
  385. return;
  386. hv_begin_read(&channel->inbound);
  387. tasklet_schedule(&channel->callback_event);
  388. }
  389. /*
  390. * vmbus_post_msg - Send a msg on the vmbus's message connection
  391. */
  392. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
  393. {
  394. struct vmbus_channel_message_header *hdr;
  395. union hv_connection_id conn_id;
  396. int ret = 0;
  397. int retries = 0;
  398. u32 usec = 1;
  399. conn_id.asu32 = 0;
  400. conn_id.u.id = vmbus_connection.msg_conn_id;
  401. /*
  402. * hv_post_message() can have transient failures because of
  403. * insufficient resources. Retry the operation a couple of
  404. * times before giving up.
  405. */
  406. while (retries < 100) {
  407. ret = hv_post_message(conn_id, 1, buffer, buflen);
  408. switch (ret) {
  409. case HV_STATUS_INVALID_CONNECTION_ID:
  410. /*
  411. * See vmbus_negotiate_version(): VMBus protocol 5.0
  412. * and higher require that we must use
  413. * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
  414. * Contact message, but on old hosts that only
  415. * support VMBus protocol 4.0 or lower, here we get
  416. * HV_STATUS_INVALID_CONNECTION_ID and we should
  417. * return an error immediately without retrying.
  418. */
  419. hdr = buffer;
  420. if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
  421. return -EINVAL;
  422. /*
  423. * We could get this if we send messages too
  424. * frequently.
  425. */
  426. ret = -EAGAIN;
  427. break;
  428. case HV_STATUS_INSUFFICIENT_MEMORY:
  429. case HV_STATUS_INSUFFICIENT_BUFFERS:
  430. ret = -ENOBUFS;
  431. break;
  432. case HV_STATUS_SUCCESS:
  433. return ret;
  434. default:
  435. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  436. return -EINVAL;
  437. }
  438. retries++;
  439. if (can_sleep && usec > 1000)
  440. msleep(usec / 1000);
  441. else if (usec < MAX_UDELAY_MS * 1000)
  442. udelay(usec);
  443. else
  444. mdelay(usec / 1000);
  445. if (retries < 22)
  446. usec *= 2;
  447. }
  448. return ret;
  449. }
  450. /*
  451. * vmbus_set_event - Send an event notification to the parent
  452. */
  453. void vmbus_set_event(struct vmbus_channel *channel)
  454. {
  455. u32 child_relid = channel->offermsg.child_relid;
  456. if (!channel->is_dedicated_interrupt)
  457. vmbus_send_interrupt(child_relid);
  458. ++channel->sig_events;
  459. if (hv_isolation_type_snp())
  460. hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, &channel->sig_event,
  461. NULL, sizeof(channel->sig_event));
  462. else
  463. hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
  464. }
  465. EXPORT_SYMBOL_GPL(vmbus_set_event);