connection.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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_connect - Sends a connect request on the partition service connection
  40. */
  41. int vmbus_connect(void)
  42. {
  43. int ret = 0;
  44. int t;
  45. struct vmbus_channel_msginfo *msginfo = NULL;
  46. struct vmbus_channel_initiate_contact *msg;
  47. unsigned long flags;
  48. /* Initialize the vmbus connection */
  49. vmbus_connection.conn_state = CONNECTING;
  50. vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
  51. if (!vmbus_connection.work_queue) {
  52. ret = -ENOMEM;
  53. goto cleanup;
  54. }
  55. INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
  56. spin_lock_init(&vmbus_connection.channelmsg_lock);
  57. INIT_LIST_HEAD(&vmbus_connection.chn_list);
  58. spin_lock_init(&vmbus_connection.channel_lock);
  59. /*
  60. * Setup the vmbus event connection for channel interrupt
  61. * abstraction stuff
  62. */
  63. vmbus_connection.int_page =
  64. (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
  65. if (vmbus_connection.int_page == NULL) {
  66. ret = -ENOMEM;
  67. goto cleanup;
  68. }
  69. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  70. vmbus_connection.send_int_page =
  71. (void *)((unsigned long)vmbus_connection.int_page +
  72. (PAGE_SIZE >> 1));
  73. /*
  74. * Setup the monitor notification facility. The 1st page for
  75. * parent->child and the 2nd page for child->parent
  76. */
  77. vmbus_connection.monitor_pages =
  78. (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
  79. if (vmbus_connection.monitor_pages == NULL) {
  80. ret = -ENOMEM;
  81. goto cleanup;
  82. }
  83. msginfo = kzalloc(sizeof(*msginfo) +
  84. sizeof(struct vmbus_channel_initiate_contact),
  85. GFP_KERNEL);
  86. if (msginfo == NULL) {
  87. ret = -ENOMEM;
  88. goto cleanup;
  89. }
  90. init_completion(&msginfo->waitevent);
  91. msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
  92. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  93. msg->vmbus_version_requested = VMBUS_REVISION_NUMBER;
  94. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  95. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages);
  96. msg->monitor_page2 = virt_to_phys(
  97. (void *)((unsigned long)vmbus_connection.monitor_pages +
  98. PAGE_SIZE));
  99. /*
  100. * Add to list before we send the request since we may
  101. * receive the response before returning from this routine
  102. */
  103. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  104. list_add_tail(&msginfo->msglistentry,
  105. &vmbus_connection.chn_msg_list);
  106. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  107. ret = vmbus_post_msg(msg,
  108. sizeof(struct vmbus_channel_initiate_contact));
  109. if (ret != 0) {
  110. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  111. list_del(&msginfo->msglistentry);
  112. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  113. flags);
  114. goto cleanup;
  115. }
  116. /* Wait for the connection response */
  117. t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
  118. if (t == 0) {
  119. spin_lock_irqsave(&vmbus_connection.channelmsg_lock,
  120. flags);
  121. list_del(&msginfo->msglistentry);
  122. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  123. flags);
  124. ret = -ETIMEDOUT;
  125. goto cleanup;
  126. }
  127. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  128. list_del(&msginfo->msglistentry);
  129. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  130. /* Check if successful */
  131. if (msginfo->response.version_response.version_supported) {
  132. vmbus_connection.conn_state = CONNECTED;
  133. } else {
  134. pr_err("Unable to connect, "
  135. "Version %d not supported by Hyper-V\n",
  136. VMBUS_REVISION_NUMBER);
  137. ret = -ECONNREFUSED;
  138. goto cleanup;
  139. }
  140. kfree(msginfo);
  141. return 0;
  142. cleanup:
  143. vmbus_connection.conn_state = DISCONNECTED;
  144. if (vmbus_connection.work_queue)
  145. destroy_workqueue(vmbus_connection.work_queue);
  146. if (vmbus_connection.int_page) {
  147. free_pages((unsigned long)vmbus_connection.int_page, 0);
  148. vmbus_connection.int_page = NULL;
  149. }
  150. if (vmbus_connection.monitor_pages) {
  151. free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
  152. vmbus_connection.monitor_pages = NULL;
  153. }
  154. kfree(msginfo);
  155. return ret;
  156. }
  157. /*
  158. * relid2channel - Get the channel object given its
  159. * child relative id (ie channel id)
  160. */
  161. struct vmbus_channel *relid2channel(u32 relid)
  162. {
  163. struct vmbus_channel *channel;
  164. struct vmbus_channel *found_channel = NULL;
  165. unsigned long flags;
  166. spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
  167. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  168. if (channel->offermsg.child_relid == relid) {
  169. found_channel = channel;
  170. break;
  171. }
  172. }
  173. spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
  174. return found_channel;
  175. }
  176. /*
  177. * process_chn_event - Process a channel event notification
  178. */
  179. static void process_chn_event(u32 relid)
  180. {
  181. struct vmbus_channel *channel;
  182. unsigned long flags;
  183. /*
  184. * Find the channel based on this relid and invokes the
  185. * channel callback to process the event
  186. */
  187. channel = relid2channel(relid);
  188. if (!channel) {
  189. pr_err("channel not found for relid - %u\n", relid);
  190. return;
  191. }
  192. /*
  193. * A channel once created is persistent even when there
  194. * is no driver handling the device. An unloading driver
  195. * sets the onchannel_callback to NULL under the
  196. * protection of the channel inbound_lock. Thus, checking
  197. * and invoking the driver specific callback takes care of
  198. * orderly unloading of the driver.
  199. */
  200. spin_lock_irqsave(&channel->inbound_lock, flags);
  201. if (channel->onchannel_callback != NULL)
  202. channel->onchannel_callback(channel->channel_callback_context);
  203. else
  204. pr_err("no channel callback for relid - %u\n", relid);
  205. spin_unlock_irqrestore(&channel->inbound_lock, flags);
  206. }
  207. /*
  208. * vmbus_on_event - Handler for events
  209. */
  210. void vmbus_on_event(unsigned long data)
  211. {
  212. u32 dword;
  213. u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
  214. int bit;
  215. u32 relid;
  216. u32 *recv_int_page = vmbus_connection.recv_int_page;
  217. /* Check events */
  218. if (!recv_int_page)
  219. return;
  220. for (dword = 0; dword < maxdword; dword++) {
  221. if (!recv_int_page[dword])
  222. continue;
  223. for (bit = 0; bit < 32; bit++) {
  224. if (sync_test_and_clear_bit(bit,
  225. (unsigned long *)&recv_int_page[dword])) {
  226. relid = (dword << 5) + bit;
  227. if (relid == 0)
  228. /*
  229. * Special case - vmbus
  230. * channel protocol msg
  231. */
  232. continue;
  233. process_chn_event(relid);
  234. }
  235. }
  236. }
  237. }
  238. /*
  239. * vmbus_post_msg - Send a msg on the vmbus's message connection
  240. */
  241. int vmbus_post_msg(void *buffer, size_t buflen)
  242. {
  243. union hv_connection_id conn_id;
  244. int ret = 0;
  245. int retries = 0;
  246. conn_id.asu32 = 0;
  247. conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
  248. /*
  249. * hv_post_message() can have transient failures because of
  250. * insufficient resources. Retry the operation a couple of
  251. * times before giving up.
  252. */
  253. while (retries < 10) {
  254. ret = hv_post_message(conn_id, 1, buffer, buflen);
  255. switch (ret) {
  256. case HV_STATUS_INSUFFICIENT_BUFFERS:
  257. ret = -ENOMEM;
  258. case -ENOMEM:
  259. break;
  260. case HV_STATUS_SUCCESS:
  261. return ret;
  262. default:
  263. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  264. return -EINVAL;
  265. }
  266. retries++;
  267. msleep(100);
  268. }
  269. return ret;
  270. }
  271. /*
  272. * vmbus_set_event - Send an event notification to the parent
  273. */
  274. int vmbus_set_event(u32 child_relid)
  275. {
  276. /* Each u32 represents 32 channels */
  277. sync_set_bit(child_relid & 31,
  278. (unsigned long *)vmbus_connection.send_int_page +
  279. (child_relid >> 5));
  280. return hv_signal_event();
  281. }