connection.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <asm/hyperv.h>
  33. #include "hyperv_vmbus.h"
  34. struct vmbus_connection vmbus_connection = {
  35. .conn_state = DISCONNECTED,
  36. .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
  37. };
  38. /*
  39. * VMBUS version is 32 bit entity broken up into
  40. * two 16 bit quantities: major_number. minor_number.
  41. *
  42. * 0 . 13 (Windows Server 2008)
  43. * 1 . 1 (Windows 7)
  44. * 2 . 4 (Windows 8)
  45. */
  46. #define VERSION_WS2008 ((0 << 16) | (13))
  47. #define VERSION_WIN7 ((1 << 16) | (1))
  48. #define VERSION_WIN8 ((2 << 16) | (4))
  49. #define VERSION_INVAL -1
  50. static __u32 vmbus_get_next_version(__u32 current_version)
  51. {
  52. switch (current_version) {
  53. case (VERSION_WIN7):
  54. return VERSION_WS2008;
  55. case (VERSION_WIN8):
  56. return VERSION_WIN7;
  57. case (VERSION_WS2008):
  58. default:
  59. return VERSION_INVAL;
  60. }
  61. }
  62. static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo,
  63. __u32 version)
  64. {
  65. int ret = 0;
  66. struct vmbus_channel_initiate_contact *msg;
  67. unsigned long flags;
  68. int t;
  69. init_completion(&msginfo->waitevent);
  70. msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
  71. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  72. msg->vmbus_version_requested = version;
  73. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  74. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages);
  75. msg->monitor_page2 = virt_to_phys(
  76. (void *)((unsigned long)vmbus_connection.monitor_pages +
  77. PAGE_SIZE));
  78. /*
  79. * Add to list before we send the request since we may
  80. * receive the response before returning from this routine
  81. */
  82. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  83. list_add_tail(&msginfo->msglistentry,
  84. &vmbus_connection.chn_msg_list);
  85. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  86. ret = vmbus_post_msg(msg,
  87. sizeof(struct vmbus_channel_initiate_contact));
  88. if (ret != 0) {
  89. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  90. list_del(&msginfo->msglistentry);
  91. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  92. flags);
  93. return ret;
  94. }
  95. /* Wait for the connection response */
  96. t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
  97. if (t == 0) {
  98. spin_lock_irqsave(&vmbus_connection.channelmsg_lock,
  99. flags);
  100. list_del(&msginfo->msglistentry);
  101. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  102. flags);
  103. return -ETIMEDOUT;
  104. }
  105. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  106. list_del(&msginfo->msglistentry);
  107. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  108. /* Check if successful */
  109. if (msginfo->response.version_response.version_supported) {
  110. vmbus_connection.conn_state = CONNECTED;
  111. } else {
  112. pr_err("Unable to connect, "
  113. "Version %d not supported by Hyper-V\n",
  114. version);
  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. spin_lock_init(&vmbus_connection.channel_lock);
  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 =
  157. (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
  158. if (vmbus_connection.monitor_pages == NULL) {
  159. ret = -ENOMEM;
  160. goto cleanup;
  161. }
  162. msginfo = kzalloc(sizeof(*msginfo) +
  163. sizeof(struct vmbus_channel_initiate_contact),
  164. GFP_KERNEL);
  165. if (msginfo == NULL) {
  166. ret = -ENOMEM;
  167. goto cleanup;
  168. }
  169. /*
  170. * Negotiate a compatible VMBUS version number with the
  171. * host. We start with the highest number we can support
  172. * and work our way down until we negotiate a compatible
  173. * version.
  174. */
  175. version = VERSION_WS2008;
  176. do {
  177. ret = vmbus_negotiate_version(msginfo, version);
  178. if (ret == 0)
  179. break;
  180. version = vmbus_get_next_version(version);
  181. } while (version != VERSION_INVAL);
  182. if (version == VERSION_INVAL)
  183. goto cleanup;
  184. kfree(msginfo);
  185. return 0;
  186. cleanup:
  187. vmbus_connection.conn_state = DISCONNECTED;
  188. if (vmbus_connection.work_queue)
  189. destroy_workqueue(vmbus_connection.work_queue);
  190. if (vmbus_connection.int_page) {
  191. free_pages((unsigned long)vmbus_connection.int_page, 0);
  192. vmbus_connection.int_page = NULL;
  193. }
  194. if (vmbus_connection.monitor_pages) {
  195. free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
  196. vmbus_connection.monitor_pages = NULL;
  197. }
  198. kfree(msginfo);
  199. return ret;
  200. }
  201. /*
  202. * relid2channel - Get the channel object given its
  203. * child relative id (ie channel id)
  204. */
  205. struct vmbus_channel *relid2channel(u32 relid)
  206. {
  207. struct vmbus_channel *channel;
  208. struct vmbus_channel *found_channel = NULL;
  209. unsigned long flags;
  210. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  211. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  212. if (channel->offermsg.child_relid == relid) {
  213. found_channel = channel;
  214. break;
  215. }
  216. }
  217. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  218. return found_channel;
  219. }
  220. /*
  221. * process_chn_event - Process a channel event notification
  222. */
  223. static void process_chn_event(u32 relid)
  224. {
  225. struct vmbus_channel *channel;
  226. unsigned long flags;
  227. void *arg;
  228. bool read_state;
  229. u32 bytes_to_read;
  230. /*
  231. * Find the channel based on this relid and invokes the
  232. * channel callback to process the event
  233. */
  234. channel = relid2channel(relid);
  235. if (!channel) {
  236. pr_err("channel not found for relid - %u\n", relid);
  237. return;
  238. }
  239. /*
  240. * A channel once created is persistent even when there
  241. * is no driver handling the device. An unloading driver
  242. * sets the onchannel_callback to NULL under the
  243. * protection of the channel inbound_lock. Thus, checking
  244. * and invoking the driver specific callback takes care of
  245. * orderly unloading of the driver.
  246. */
  247. spin_lock_irqsave(&channel->inbound_lock, flags);
  248. if (channel->onchannel_callback != NULL) {
  249. arg = channel->channel_callback_context;
  250. read_state = channel->batched_reading;
  251. /*
  252. * This callback reads the messages sent by the host.
  253. * We can optimize host to guest signaling by ensuring:
  254. * 1. While reading the channel, we disable interrupts from
  255. * host.
  256. * 2. Ensure that we process all posted messages from the host
  257. * before returning from this callback.
  258. * 3. Once we return, enable signaling from the host. Once this
  259. * state is set we check to see if additional packets are
  260. * available to read. In this case we repeat the process.
  261. */
  262. do {
  263. hv_begin_read(&channel->inbound);
  264. channel->onchannel_callback(arg);
  265. bytes_to_read = hv_end_read(&channel->inbound);
  266. } while (read_state && (bytes_to_read != 0));
  267. } else {
  268. pr_err("no channel callback for relid - %u\n", relid);
  269. }
  270. spin_unlock_irqrestore(&channel->inbound_lock, flags);
  271. }
  272. /*
  273. * vmbus_on_event - Handler for events
  274. */
  275. void vmbus_on_event(unsigned long data)
  276. {
  277. u32 dword;
  278. u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
  279. int bit;
  280. u32 relid;
  281. u32 *recv_int_page = vmbus_connection.recv_int_page;
  282. /* Check events */
  283. if (!recv_int_page)
  284. return;
  285. for (dword = 0; dword < maxdword; dword++) {
  286. if (!recv_int_page[dword])
  287. continue;
  288. for (bit = 0; bit < 32; bit++) {
  289. if (sync_test_and_clear_bit(bit,
  290. (unsigned long *)&recv_int_page[dword])) {
  291. relid = (dword << 5) + bit;
  292. if (relid == 0)
  293. /*
  294. * Special case - vmbus
  295. * channel protocol msg
  296. */
  297. continue;
  298. process_chn_event(relid);
  299. }
  300. }
  301. }
  302. }
  303. /*
  304. * vmbus_post_msg - Send a msg on the vmbus's message connection
  305. */
  306. int vmbus_post_msg(void *buffer, size_t buflen)
  307. {
  308. union hv_connection_id conn_id;
  309. int ret = 0;
  310. int retries = 0;
  311. conn_id.asu32 = 0;
  312. conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
  313. /*
  314. * hv_post_message() can have transient failures because of
  315. * insufficient resources. Retry the operation a couple of
  316. * times before giving up.
  317. */
  318. while (retries < 3) {
  319. ret = hv_post_message(conn_id, 1, buffer, buflen);
  320. if (ret != HV_STATUS_INSUFFICIENT_BUFFERS)
  321. return ret;
  322. retries++;
  323. msleep(100);
  324. }
  325. return ret;
  326. }
  327. /*
  328. * vmbus_set_event - Send an event notification to the parent
  329. */
  330. int vmbus_set_event(u32 child_relid)
  331. {
  332. /* Each u32 represents 32 channels */
  333. sync_set_bit(child_relid & 31,
  334. (unsigned long *)vmbus_connection.send_int_page +
  335. (child_relid >> 5));
  336. return hv_signal_event();
  337. }