connection.c 12 KB

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