connection.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_WS2008):
  52. default:
  53. return VERSION_INVAL;
  54. }
  55. }
  56. static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo,
  57. __u32 version)
  58. {
  59. int ret = 0;
  60. struct vmbus_channel_initiate_contact *msg;
  61. unsigned long flags;
  62. init_completion(&msginfo->waitevent);
  63. msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
  64. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  65. msg->vmbus_version_requested = version;
  66. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  67. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
  68. msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
  69. if (version == VERSION_WIN8)
  70. msg->target_vcpu = hv_context.vp_index[smp_processor_id()];
  71. /*
  72. * Add to list before we send the request since we may
  73. * receive the response before returning from this routine
  74. */
  75. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  76. list_add_tail(&msginfo->msglistentry,
  77. &vmbus_connection.chn_msg_list);
  78. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  79. ret = vmbus_post_msg(msg,
  80. sizeof(struct vmbus_channel_initiate_contact));
  81. if (ret != 0) {
  82. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  83. list_del(&msginfo->msglistentry);
  84. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  85. flags);
  86. return ret;
  87. }
  88. /* Wait for the connection response */
  89. wait_for_completion(&msginfo->waitevent);
  90. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  91. list_del(&msginfo->msglistentry);
  92. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  93. /* Check if successful */
  94. if (msginfo->response.version_response.version_supported) {
  95. vmbus_connection.conn_state = CONNECTED;
  96. } else {
  97. return -ECONNREFUSED;
  98. }
  99. return ret;
  100. }
  101. /*
  102. * vmbus_connect - Sends a connect request on the partition service connection
  103. */
  104. int vmbus_connect(void)
  105. {
  106. int ret = 0;
  107. struct vmbus_channel_msginfo *msginfo = NULL;
  108. __u32 version;
  109. /* Initialize the vmbus connection */
  110. vmbus_connection.conn_state = CONNECTING;
  111. vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
  112. if (!vmbus_connection.work_queue) {
  113. ret = -ENOMEM;
  114. goto cleanup;
  115. }
  116. INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
  117. spin_lock_init(&vmbus_connection.channelmsg_lock);
  118. INIT_LIST_HEAD(&vmbus_connection.chn_list);
  119. spin_lock_init(&vmbus_connection.channel_lock);
  120. /*
  121. * Setup the vmbus event connection for channel interrupt
  122. * abstraction stuff
  123. */
  124. vmbus_connection.int_page =
  125. (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
  126. if (vmbus_connection.int_page == NULL) {
  127. ret = -ENOMEM;
  128. goto cleanup;
  129. }
  130. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  131. vmbus_connection.send_int_page =
  132. (void *)((unsigned long)vmbus_connection.int_page +
  133. (PAGE_SIZE >> 1));
  134. /*
  135. * Setup the monitor notification facility. The 1st page for
  136. * parent->child and the 2nd page for child->parent
  137. */
  138. vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  139. vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  140. if ((vmbus_connection.monitor_pages[0] == NULL) ||
  141. (vmbus_connection.monitor_pages[1] == NULL)) {
  142. ret = -ENOMEM;
  143. goto cleanup;
  144. }
  145. msginfo = kzalloc(sizeof(*msginfo) +
  146. sizeof(struct vmbus_channel_initiate_contact),
  147. GFP_KERNEL);
  148. if (msginfo == NULL) {
  149. ret = -ENOMEM;
  150. goto cleanup;
  151. }
  152. /*
  153. * Negotiate a compatible VMBUS version number with the
  154. * host. We start with the highest number we can support
  155. * and work our way down until we negotiate a compatible
  156. * version.
  157. */
  158. version = VERSION_CURRENT;
  159. do {
  160. ret = vmbus_negotiate_version(msginfo, version);
  161. if (ret == -ETIMEDOUT)
  162. goto cleanup;
  163. if (vmbus_connection.conn_state == CONNECTED)
  164. break;
  165. version = vmbus_get_next_version(version);
  166. } while (version != VERSION_INVAL);
  167. if (version == VERSION_INVAL)
  168. goto cleanup;
  169. vmbus_proto_version = version;
  170. pr_info("Hyper-V Host Build:%d-%d.%d-%d-%d.%d; Vmbus version:%d.%d\n",
  171. host_info_eax, host_info_ebx >> 16,
  172. host_info_ebx & 0xFFFF, host_info_ecx,
  173. host_info_edx >> 24, host_info_edx & 0xFFFFFF,
  174. version >> 16, version & 0xFFFF);
  175. kfree(msginfo);
  176. return 0;
  177. cleanup:
  178. pr_err("Unable to connect to host\n");
  179. vmbus_connection.conn_state = DISCONNECTED;
  180. if (vmbus_connection.work_queue)
  181. destroy_workqueue(vmbus_connection.work_queue);
  182. if (vmbus_connection.int_page) {
  183. free_pages((unsigned long)vmbus_connection.int_page, 0);
  184. vmbus_connection.int_page = NULL;
  185. }
  186. free_pages((unsigned long)vmbus_connection.monitor_pages[0], 1);
  187. free_pages((unsigned long)vmbus_connection.monitor_pages[1], 1);
  188. vmbus_connection.monitor_pages[0] = NULL;
  189. vmbus_connection.monitor_pages[1] = NULL;
  190. kfree(msginfo);
  191. return ret;
  192. }
  193. /*
  194. * relid2channel - Get the channel object given its
  195. * child relative id (ie channel id)
  196. */
  197. struct vmbus_channel *relid2channel(u32 relid)
  198. {
  199. struct vmbus_channel *channel;
  200. struct vmbus_channel *found_channel = NULL;
  201. unsigned long flags;
  202. struct list_head *cur, *tmp;
  203. struct vmbus_channel *cur_sc;
  204. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  205. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  206. if (channel->offermsg.child_relid == relid) {
  207. found_channel = channel;
  208. break;
  209. } else if (!list_empty(&channel->sc_list)) {
  210. /*
  211. * Deal with sub-channels.
  212. */
  213. list_for_each_safe(cur, tmp, &channel->sc_list) {
  214. cur_sc = list_entry(cur, struct vmbus_channel,
  215. sc_list);
  216. if (cur_sc->offermsg.child_relid == relid) {
  217. found_channel = cur_sc;
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  224. return found_channel;
  225. }
  226. /*
  227. * process_chn_event - Process a channel event notification
  228. */
  229. static void process_chn_event(u32 relid)
  230. {
  231. struct vmbus_channel *channel;
  232. unsigned long flags;
  233. void *arg;
  234. bool read_state;
  235. u32 bytes_to_read;
  236. /*
  237. * Find the channel based on this relid and invokes the
  238. * channel callback to process the event
  239. */
  240. channel = relid2channel(relid);
  241. if (!channel) {
  242. pr_err("channel not found for relid - %u\n", relid);
  243. return;
  244. }
  245. /*
  246. * A channel once created is persistent even when there
  247. * is no driver handling the device. An unloading driver
  248. * sets the onchannel_callback to NULL under the
  249. * protection of the channel inbound_lock. Thus, checking
  250. * and invoking the driver specific callback takes care of
  251. * orderly unloading of the driver.
  252. */
  253. spin_lock_irqsave(&channel->inbound_lock, flags);
  254. if (channel->onchannel_callback != NULL) {
  255. arg = channel->channel_callback_context;
  256. read_state = channel->batched_reading;
  257. /*
  258. * This callback reads the messages sent by the host.
  259. * We can optimize host to guest signaling by ensuring:
  260. * 1. While reading the channel, we disable interrupts from
  261. * host.
  262. * 2. Ensure that we process all posted messages from the host
  263. * before returning from this callback.
  264. * 3. Once we return, enable signaling from the host. Once this
  265. * state is set we check to see if additional packets are
  266. * available to read. In this case we repeat the process.
  267. */
  268. do {
  269. hv_begin_read(&channel->inbound);
  270. channel->onchannel_callback(arg);
  271. bytes_to_read = hv_end_read(&channel->inbound);
  272. } while (read_state && (bytes_to_read != 0));
  273. } else {
  274. pr_err("no channel callback for relid - %u\n", relid);
  275. }
  276. spin_unlock_irqrestore(&channel->inbound_lock, flags);
  277. }
  278. /*
  279. * vmbus_on_event - Handler for events
  280. */
  281. void vmbus_on_event(unsigned long data)
  282. {
  283. u32 dword;
  284. u32 maxdword;
  285. int bit;
  286. u32 relid;
  287. u32 *recv_int_page = NULL;
  288. void *page_addr;
  289. int cpu = smp_processor_id();
  290. union hv_synic_event_flags *event;
  291. if ((vmbus_proto_version == VERSION_WS2008) ||
  292. (vmbus_proto_version == VERSION_WIN7)) {
  293. maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
  294. recv_int_page = vmbus_connection.recv_int_page;
  295. } else {
  296. /*
  297. * When the host is win8 and beyond, the event page
  298. * can be directly checked to get the id of the channel
  299. * that has the interrupt pending.
  300. */
  301. maxdword = HV_EVENT_FLAGS_DWORD_COUNT;
  302. page_addr = hv_context.synic_event_page[cpu];
  303. event = (union hv_synic_event_flags *)page_addr +
  304. VMBUS_MESSAGE_SINT;
  305. recv_int_page = event->flags32;
  306. }
  307. /* Check events */
  308. if (!recv_int_page)
  309. return;
  310. for (dword = 0; dword < maxdword; dword++) {
  311. if (!recv_int_page[dword])
  312. continue;
  313. for (bit = 0; bit < 32; bit++) {
  314. if (sync_test_and_clear_bit(bit,
  315. (unsigned long *)&recv_int_page[dword])) {
  316. relid = (dword << 5) + bit;
  317. if (relid == 0)
  318. /*
  319. * Special case - vmbus
  320. * channel protocol msg
  321. */
  322. continue;
  323. process_chn_event(relid);
  324. }
  325. }
  326. }
  327. }
  328. /*
  329. * vmbus_post_msg - Send a msg on the vmbus's message connection
  330. */
  331. int vmbus_post_msg(void *buffer, size_t buflen)
  332. {
  333. union hv_connection_id conn_id;
  334. int ret = 0;
  335. int retries = 0;
  336. conn_id.asu32 = 0;
  337. conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
  338. /*
  339. * hv_post_message() can have transient failures because of
  340. * insufficient resources. Retry the operation a couple of
  341. * times before giving up.
  342. */
  343. while (retries < 3) {
  344. ret = hv_post_message(conn_id, 1, buffer, buflen);
  345. if (ret != HV_STATUS_INSUFFICIENT_BUFFERS)
  346. return ret;
  347. retries++;
  348. msleep(100);
  349. }
  350. return ret;
  351. }
  352. /*
  353. * vmbus_set_event - Send an event notification to the parent
  354. */
  355. int vmbus_set_event(struct vmbus_channel *channel)
  356. {
  357. u32 child_relid = channel->offermsg.child_relid;
  358. if (!channel->is_dedicated_interrupt) {
  359. /* Each u32 represents 32 channels */
  360. sync_set_bit(child_relid & 31,
  361. (unsigned long *)vmbus_connection.send_int_page +
  362. (child_relid >> 5));
  363. }
  364. return hv_signal_event(channel->sig_event);
  365. }