connection.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 "hyperv_vmbus.h"
  35. struct vmbus_connection vmbus_connection = {
  36. .conn_state = DISCONNECTED,
  37. .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
  38. };
  39. /*
  40. * Negotiated protocol version with the host.
  41. */
  42. __u32 vmbus_proto_version;
  43. EXPORT_SYMBOL_GPL(vmbus_proto_version);
  44. static __u32 vmbus_get_next_version(__u32 current_version)
  45. {
  46. switch (current_version) {
  47. case (VERSION_WIN7):
  48. return VERSION_WS2008;
  49. case (VERSION_WIN8):
  50. return VERSION_WIN7;
  51. case (VERSION_WIN8_1):
  52. return VERSION_WIN8;
  53. case (VERSION_WIN10):
  54. return VERSION_WIN8_1;
  55. case (VERSION_WS2008):
  56. default:
  57. return VERSION_INVAL;
  58. }
  59. }
  60. static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo,
  61. __u32 version)
  62. {
  63. int ret = 0;
  64. struct vmbus_channel_initiate_contact *msg;
  65. unsigned long flags;
  66. init_completion(&msginfo->waitevent);
  67. msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
  68. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  69. msg->vmbus_version_requested = version;
  70. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  71. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
  72. msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
  73. /*
  74. * We want all channel messages to be delivered on CPU 0.
  75. * This has been the behavior pre-win8. This is not
  76. * perf issue and having all channel messages delivered on CPU 0
  77. * would be ok.
  78. * For post win8 hosts, we support receiving channel messagges on
  79. * all the CPUs. This is needed for kexec to work correctly where
  80. * the CPU attempting to connect may not be CPU 0.
  81. */
  82. if (version >= VERSION_WIN8_1) {
  83. msg->target_vcpu = hv_context.vp_index[get_cpu()];
  84. put_cpu();
  85. } else {
  86. msg->target_vcpu = 0;
  87. }
  88. /*
  89. * Add to list before we send the request since we may
  90. * receive the response before returning from this routine
  91. */
  92. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  93. list_add_tail(&msginfo->msglistentry,
  94. &vmbus_connection.chn_msg_list);
  95. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  96. ret = vmbus_post_msg(msg,
  97. sizeof(struct vmbus_channel_initiate_contact),
  98. true);
  99. if (ret != 0) {
  100. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  101. list_del(&msginfo->msglistentry);
  102. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  103. flags);
  104. return ret;
  105. }
  106. /* Wait for the connection response */
  107. wait_for_completion(&msginfo->waitevent);
  108. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  109. list_del(&msginfo->msglistentry);
  110. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  111. /* Check if successful */
  112. if (msginfo->response.version_response.version_supported) {
  113. vmbus_connection.conn_state = CONNECTED;
  114. } else {
  115. return -ECONNREFUSED;
  116. }
  117. return ret;
  118. }
  119. /*
  120. * vmbus_connect - Sends a connect request on the partition service connection
  121. */
  122. int vmbus_connect(void)
  123. {
  124. int ret = 0;
  125. struct vmbus_channel_msginfo *msginfo = NULL;
  126. __u32 version;
  127. /* Initialize the vmbus connection */
  128. vmbus_connection.conn_state = CONNECTING;
  129. vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
  130. if (!vmbus_connection.work_queue) {
  131. ret = -ENOMEM;
  132. goto cleanup;
  133. }
  134. INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
  135. spin_lock_init(&vmbus_connection.channelmsg_lock);
  136. INIT_LIST_HEAD(&vmbus_connection.chn_list);
  137. mutex_init(&vmbus_connection.channel_mutex);
  138. /*
  139. * Setup the vmbus event connection for channel interrupt
  140. * abstraction stuff
  141. */
  142. vmbus_connection.int_page =
  143. (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
  144. if (vmbus_connection.int_page == NULL) {
  145. ret = -ENOMEM;
  146. goto cleanup;
  147. }
  148. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  149. vmbus_connection.send_int_page =
  150. (void *)((unsigned long)vmbus_connection.int_page +
  151. (PAGE_SIZE >> 1));
  152. /*
  153. * Setup the monitor notification facility. The 1st page for
  154. * parent->child and the 2nd page for child->parent
  155. */
  156. vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  157. vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  158. if ((vmbus_connection.monitor_pages[0] == NULL) ||
  159. (vmbus_connection.monitor_pages[1] == NULL)) {
  160. ret = -ENOMEM;
  161. goto cleanup;
  162. }
  163. msginfo = kzalloc(sizeof(*msginfo) +
  164. sizeof(struct vmbus_channel_initiate_contact),
  165. GFP_KERNEL);
  166. if (msginfo == NULL) {
  167. ret = -ENOMEM;
  168. goto cleanup;
  169. }
  170. /*
  171. * Negotiate a compatible VMBUS version number with the
  172. * host. We start with the highest number we can support
  173. * and work our way down until we negotiate a compatible
  174. * version.
  175. */
  176. version = VERSION_CURRENT;
  177. do {
  178. ret = vmbus_negotiate_version(msginfo, version);
  179. if (ret == -ETIMEDOUT)
  180. goto cleanup;
  181. if (vmbus_connection.conn_state == CONNECTED)
  182. break;
  183. version = vmbus_get_next_version(version);
  184. } while (version != VERSION_INVAL);
  185. if (version == VERSION_INVAL)
  186. goto cleanup;
  187. vmbus_proto_version = version;
  188. pr_info("Hyper-V Host Build:%d-%d.%d-%d-%d.%d; Vmbus version:%d.%d\n",
  189. host_info_eax, host_info_ebx >> 16,
  190. host_info_ebx & 0xFFFF, host_info_ecx,
  191. host_info_edx >> 24, host_info_edx & 0xFFFFFF,
  192. version >> 16, version & 0xFFFF);
  193. kfree(msginfo);
  194. return 0;
  195. cleanup:
  196. pr_err("Unable to connect to host\n");
  197. vmbus_connection.conn_state = DISCONNECTED;
  198. vmbus_disconnect();
  199. kfree(msginfo);
  200. return ret;
  201. }
  202. void vmbus_disconnect(void)
  203. {
  204. /*
  205. * First send the unload request to the host.
  206. */
  207. vmbus_initiate_unload(false);
  208. if (vmbus_connection.work_queue) {
  209. drain_workqueue(vmbus_connection.work_queue);
  210. destroy_workqueue(vmbus_connection.work_queue);
  211. }
  212. if (vmbus_connection.int_page) {
  213. free_pages((unsigned long)vmbus_connection.int_page, 0);
  214. vmbus_connection.int_page = NULL;
  215. }
  216. free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
  217. free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
  218. vmbus_connection.monitor_pages[0] = NULL;
  219. vmbus_connection.monitor_pages[1] = NULL;
  220. }
  221. /*
  222. * Map the given relid to the corresponding channel based on the
  223. * per-cpu list of channels that have been affinitized to this CPU.
  224. * This will be used in the channel callback path as we can do this
  225. * mapping in a lock-free fashion.
  226. */
  227. static struct vmbus_channel *pcpu_relid2channel(u32 relid)
  228. {
  229. struct vmbus_channel *channel;
  230. struct vmbus_channel *found_channel = NULL;
  231. int cpu = smp_processor_id();
  232. struct list_head *pcpu_head = &hv_context.percpu_list[cpu];
  233. list_for_each_entry(channel, pcpu_head, percpu_list) {
  234. if (channel->offermsg.child_relid == relid) {
  235. found_channel = channel;
  236. break;
  237. }
  238. }
  239. return found_channel;
  240. }
  241. /*
  242. * relid2channel - Get the channel object given its
  243. * child relative id (ie channel id)
  244. */
  245. struct vmbus_channel *relid2channel(u32 relid)
  246. {
  247. struct vmbus_channel *channel;
  248. struct vmbus_channel *found_channel = NULL;
  249. struct list_head *cur, *tmp;
  250. struct vmbus_channel *cur_sc;
  251. BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
  252. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  253. if (channel->offermsg.child_relid == relid) {
  254. found_channel = channel;
  255. break;
  256. } else if (!list_empty(&channel->sc_list)) {
  257. /*
  258. * Deal with sub-channels.
  259. */
  260. list_for_each_safe(cur, tmp, &channel->sc_list) {
  261. cur_sc = list_entry(cur, struct vmbus_channel,
  262. sc_list);
  263. if (cur_sc->offermsg.child_relid == relid) {
  264. found_channel = cur_sc;
  265. break;
  266. }
  267. }
  268. }
  269. }
  270. return found_channel;
  271. }
  272. /*
  273. * process_chn_event - Process a channel event notification
  274. */
  275. static void process_chn_event(u32 relid)
  276. {
  277. struct vmbus_channel *channel;
  278. void *arg;
  279. bool read_state;
  280. u32 bytes_to_read;
  281. /*
  282. * Find the channel based on this relid and invokes the
  283. * channel callback to process the event
  284. */
  285. channel = pcpu_relid2channel(relid);
  286. if (!channel)
  287. return;
  288. /*
  289. * A channel once created is persistent even when there
  290. * is no driver handling the device. An unloading driver
  291. * sets the onchannel_callback to NULL on the same CPU
  292. * as where this interrupt is handled (in an interrupt context).
  293. * Thus, checking and invoking the driver specific callback takes
  294. * care of orderly unloading of the driver.
  295. */
  296. if (channel->onchannel_callback != NULL) {
  297. arg = channel->channel_callback_context;
  298. read_state = channel->batched_reading;
  299. /*
  300. * This callback reads the messages sent by the host.
  301. * We can optimize host to guest signaling by ensuring:
  302. * 1. While reading the channel, we disable interrupts from
  303. * host.
  304. * 2. Ensure that we process all posted messages from the host
  305. * before returning from this callback.
  306. * 3. Once we return, enable signaling from the host. Once this
  307. * state is set we check to see if additional packets are
  308. * available to read. In this case we repeat the process.
  309. */
  310. do {
  311. if (read_state)
  312. hv_begin_read(&channel->inbound);
  313. channel->onchannel_callback(arg);
  314. if (read_state)
  315. bytes_to_read = hv_end_read(&channel->inbound);
  316. else
  317. bytes_to_read = 0;
  318. } while (read_state && (bytes_to_read != 0));
  319. }
  320. }
  321. /*
  322. * vmbus_on_event - Handler for events
  323. */
  324. void vmbus_on_event(unsigned long data)
  325. {
  326. u32 dword;
  327. u32 maxdword;
  328. int bit;
  329. u32 relid;
  330. u32 *recv_int_page = NULL;
  331. void *page_addr;
  332. int cpu = smp_processor_id();
  333. union hv_synic_event_flags *event;
  334. if (vmbus_proto_version < VERSION_WIN8) {
  335. maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
  336. recv_int_page = vmbus_connection.recv_int_page;
  337. } else {
  338. /*
  339. * When the host is win8 and beyond, the event page
  340. * can be directly checked to get the id of the channel
  341. * that has the interrupt pending.
  342. */
  343. maxdword = HV_EVENT_FLAGS_DWORD_COUNT;
  344. page_addr = hv_context.synic_event_page[cpu];
  345. event = (union hv_synic_event_flags *)page_addr +
  346. VMBUS_MESSAGE_SINT;
  347. recv_int_page = event->flags32;
  348. }
  349. /* Check events */
  350. if (!recv_int_page)
  351. return;
  352. for (dword = 0; dword < maxdword; dword++) {
  353. if (!recv_int_page[dword])
  354. continue;
  355. for (bit = 0; bit < 32; bit++) {
  356. if (sync_test_and_clear_bit(bit,
  357. (unsigned long *)&recv_int_page[dword])) {
  358. relid = (dword << 5) + bit;
  359. if (relid == 0)
  360. /*
  361. * Special case - vmbus
  362. * channel protocol msg
  363. */
  364. continue;
  365. process_chn_event(relid);
  366. }
  367. }
  368. }
  369. }
  370. /*
  371. * vmbus_post_msg - Send a msg on the vmbus's message connection
  372. */
  373. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
  374. {
  375. union hv_connection_id conn_id;
  376. int ret = 0;
  377. int retries = 0;
  378. u32 usec = 1;
  379. conn_id.asu32 = 0;
  380. conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
  381. /*
  382. * hv_post_message() can have transient failures because of
  383. * insufficient resources. Retry the operation a couple of
  384. * times before giving up.
  385. */
  386. while (retries < 100) {
  387. ret = hv_post_message(conn_id, 1, buffer, buflen);
  388. switch (ret) {
  389. case HV_STATUS_INVALID_CONNECTION_ID:
  390. /*
  391. * We could get this if we send messages too
  392. * frequently.
  393. */
  394. ret = -EAGAIN;
  395. break;
  396. case HV_STATUS_INSUFFICIENT_MEMORY:
  397. case HV_STATUS_INSUFFICIENT_BUFFERS:
  398. ret = -ENOMEM;
  399. break;
  400. case HV_STATUS_SUCCESS:
  401. return ret;
  402. default:
  403. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  404. return -EINVAL;
  405. }
  406. retries++;
  407. if (can_sleep && usec > 1000)
  408. msleep(usec / 1000);
  409. else if (usec < MAX_UDELAY_MS * 1000)
  410. udelay(usec);
  411. else
  412. mdelay(usec / 1000);
  413. if (usec < 256000)
  414. usec *= 2;
  415. }
  416. return ret;
  417. }
  418. /*
  419. * vmbus_set_event - Send an event notification to the parent
  420. */
  421. void vmbus_set_event(struct vmbus_channel *channel)
  422. {
  423. u32 child_relid = channel->offermsg.child_relid;
  424. if (!channel->is_dedicated_interrupt) {
  425. /* Each u32 represents 32 channels */
  426. sync_set_bit(child_relid & 31,
  427. (unsigned long *)vmbus_connection.send_int_page +
  428. (child_relid >> 5));
  429. }
  430. hv_do_hypercall(HVCALL_SIGNAL_EVENT, channel->sig_event, NULL);
  431. }
  432. EXPORT_SYMBOL_GPL(vmbus_set_event);