connection.c 9.9 KB

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