connection.c 12 KB

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