PageRenderTime 113ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/hv/connection.c

https://github.com/anarsoul/linux-2.6
C | 467 lines | 280 code | 78 blank | 109 comment | 55 complexity | a8bb0f62da779a366cf29d9a8714e1e6 MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (c) 2009, Microsoft Corporation.
  5. *
  6. * Authors:
  7. * Haiyang Zhang <haiyangz@microsoft.com>
  8. * Hank Janssen <hjanssen@microsoft.com>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/wait.h>
  14. #include <linux/delay.h>
  15. #include <linux/mm.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/hyperv.h>
  19. #include <linux/export.h>
  20. #include <asm/mshyperv.h>
  21. #include "hyperv_vmbus.h"
  22. struct vmbus_connection vmbus_connection = {
  23. .conn_state = DISCONNECTED,
  24. .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
  25. };
  26. EXPORT_SYMBOL_GPL(vmbus_connection);
  27. /*
  28. * Negotiated protocol version with the host.
  29. */
  30. __u32 vmbus_proto_version;
  31. EXPORT_SYMBOL_GPL(vmbus_proto_version);
  32. static __u32 vmbus_get_next_version(__u32 current_version)
  33. {
  34. switch (current_version) {
  35. case (VERSION_WIN7):
  36. return VERSION_WS2008;
  37. case (VERSION_WIN8):
  38. return VERSION_WIN7;
  39. case (VERSION_WIN8_1):
  40. return VERSION_WIN8;
  41. case (VERSION_WIN10):
  42. return VERSION_WIN8_1;
  43. case (VERSION_WIN10_V5):
  44. return VERSION_WIN10;
  45. case (VERSION_WS2008):
  46. default:
  47. return VERSION_INVAL;
  48. }
  49. }
  50. static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo,
  51. __u32 version)
  52. {
  53. int ret = 0;
  54. unsigned int cur_cpu;
  55. struct vmbus_channel_initiate_contact *msg;
  56. unsigned long flags;
  57. init_completion(&msginfo->waitevent);
  58. msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
  59. memset(msg, 0, sizeof(*msg));
  60. msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
  61. msg->vmbus_version_requested = version;
  62. /*
  63. * VMBus protocol 5.0 (VERSION_WIN10_V5) requires that we must use
  64. * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
  65. * and for subsequent messages, we must use the Message Connection ID
  66. * field in the host-returned Version Response Message. And, with
  67. * VERSION_WIN10_V5, we don't use msg->interrupt_page, but we tell
  68. * the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
  69. * compatibility.
  70. *
  71. * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
  72. */
  73. if (version >= VERSION_WIN10_V5) {
  74. msg->msg_sint = VMBUS_MESSAGE_SINT;
  75. vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
  76. } else {
  77. msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
  78. vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
  79. }
  80. msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
  81. msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
  82. /*
  83. * We want all channel messages to be delivered on CPU 0.
  84. * This has been the behavior pre-win8. This is not
  85. * perf issue and having all channel messages delivered on CPU 0
  86. * would be ok.
  87. * For post win8 hosts, we support receiving channel messagges on
  88. * all the CPUs. This is needed for kexec to work correctly where
  89. * the CPU attempting to connect may not be CPU 0.
  90. */
  91. if (version >= VERSION_WIN8_1) {
  92. cur_cpu = get_cpu();
  93. msg->target_vcpu = hv_cpu_number_to_vp_number(cur_cpu);
  94. vmbus_connection.connect_cpu = cur_cpu;
  95. put_cpu();
  96. } else {
  97. msg->target_vcpu = 0;
  98. vmbus_connection.connect_cpu = 0;
  99. }
  100. /*
  101. * Add to list before we send the request since we may
  102. * receive the response before returning from this routine
  103. */
  104. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  105. list_add_tail(&msginfo->msglistentry,
  106. &vmbus_connection.chn_msg_list);
  107. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  108. ret = vmbus_post_msg(msg,
  109. sizeof(struct vmbus_channel_initiate_contact),
  110. true);
  111. trace_vmbus_negotiate_version(msg, ret);
  112. if (ret != 0) {
  113. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  114. list_del(&msginfo->msglistentry);
  115. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
  116. flags);
  117. return ret;
  118. }
  119. /* Wait for the connection response */
  120. wait_for_completion(&msginfo->waitevent);
  121. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  122. list_del(&msginfo->msglistentry);
  123. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  124. /* Check if successful */
  125. if (msginfo->response.version_response.version_supported) {
  126. vmbus_connection.conn_state = CONNECTED;
  127. if (version >= VERSION_WIN10_V5)
  128. vmbus_connection.msg_conn_id =
  129. msginfo->response.version_response.msg_conn_id;
  130. } else {
  131. return -ECONNREFUSED;
  132. }
  133. return ret;
  134. }
  135. /*
  136. * vmbus_connect - Sends a connect request on the partition service connection
  137. */
  138. int vmbus_connect(void)
  139. {
  140. int ret = 0;
  141. struct vmbus_channel_msginfo *msginfo = NULL;
  142. __u32 version;
  143. /* Initialize the vmbus connection */
  144. vmbus_connection.conn_state = CONNECTING;
  145. vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
  146. if (!vmbus_connection.work_queue) {
  147. ret = -ENOMEM;
  148. goto cleanup;
  149. }
  150. vmbus_connection.handle_primary_chan_wq =
  151. create_workqueue("hv_pri_chan");
  152. if (!vmbus_connection.handle_primary_chan_wq) {
  153. ret = -ENOMEM;
  154. goto cleanup;
  155. }
  156. vmbus_connection.handle_sub_chan_wq =
  157. create_workqueue("hv_sub_chan");
  158. if (!vmbus_connection.handle_sub_chan_wq) {
  159. ret = -ENOMEM;
  160. goto cleanup;
  161. }
  162. INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
  163. spin_lock_init(&vmbus_connection.channelmsg_lock);
  164. INIT_LIST_HEAD(&vmbus_connection.chn_list);
  165. mutex_init(&vmbus_connection.channel_mutex);
  166. /*
  167. * Setup the vmbus event connection for channel interrupt
  168. * abstraction stuff
  169. */
  170. vmbus_connection.int_page =
  171. (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
  172. if (vmbus_connection.int_page == NULL) {
  173. ret = -ENOMEM;
  174. goto cleanup;
  175. }
  176. vmbus_connection.recv_int_page = vmbus_connection.int_page;
  177. vmbus_connection.send_int_page =
  178. (void *)((unsigned long)vmbus_connection.int_page +
  179. (PAGE_SIZE >> 1));
  180. /*
  181. * Setup the monitor notification facility. The 1st page for
  182. * parent->child and the 2nd page for child->parent
  183. */
  184. vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  185. vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
  186. if ((vmbus_connection.monitor_pages[0] == NULL) ||
  187. (vmbus_connection.monitor_pages[1] == NULL)) {
  188. ret = -ENOMEM;
  189. goto cleanup;
  190. }
  191. msginfo = kzalloc(sizeof(*msginfo) +
  192. sizeof(struct vmbus_channel_initiate_contact),
  193. GFP_KERNEL);
  194. if (msginfo == NULL) {
  195. ret = -ENOMEM;
  196. goto cleanup;
  197. }
  198. /*
  199. * Negotiate a compatible VMBUS version number with the
  200. * host. We start with the highest number we can support
  201. * and work our way down until we negotiate a compatible
  202. * version.
  203. */
  204. version = VERSION_CURRENT;
  205. do {
  206. ret = vmbus_negotiate_version(msginfo, version);
  207. if (ret == -ETIMEDOUT)
  208. goto cleanup;
  209. if (vmbus_connection.conn_state == CONNECTED)
  210. break;
  211. version = vmbus_get_next_version(version);
  212. } while (version != VERSION_INVAL);
  213. if (version == VERSION_INVAL)
  214. goto cleanup;
  215. vmbus_proto_version = version;
  216. pr_info("Vmbus version:%d.%d\n",
  217. version >> 16, version & 0xFFFF);
  218. kfree(msginfo);
  219. return 0;
  220. cleanup:
  221. pr_err("Unable to connect to host\n");
  222. vmbus_connection.conn_state = DISCONNECTED;
  223. vmbus_disconnect();
  224. kfree(msginfo);
  225. return ret;
  226. }
  227. void vmbus_disconnect(void)
  228. {
  229. /*
  230. * First send the unload request to the host.
  231. */
  232. vmbus_initiate_unload(false);
  233. if (vmbus_connection.handle_sub_chan_wq)
  234. destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
  235. if (vmbus_connection.handle_primary_chan_wq)
  236. destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
  237. if (vmbus_connection.work_queue)
  238. destroy_workqueue(vmbus_connection.work_queue);
  239. if (vmbus_connection.int_page) {
  240. free_pages((unsigned long)vmbus_connection.int_page, 0);
  241. vmbus_connection.int_page = NULL;
  242. }
  243. free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
  244. free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
  245. vmbus_connection.monitor_pages[0] = NULL;
  246. vmbus_connection.monitor_pages[1] = NULL;
  247. }
  248. /*
  249. * relid2channel - Get the channel object given its
  250. * child relative id (ie channel id)
  251. */
  252. struct vmbus_channel *relid2channel(u32 relid)
  253. {
  254. struct vmbus_channel *channel;
  255. struct vmbus_channel *found_channel = NULL;
  256. struct list_head *cur, *tmp;
  257. struct vmbus_channel *cur_sc;
  258. BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
  259. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  260. if (channel->offermsg.child_relid == relid) {
  261. found_channel = channel;
  262. break;
  263. } else if (!list_empty(&channel->sc_list)) {
  264. /*
  265. * Deal with sub-channels.
  266. */
  267. list_for_each_safe(cur, tmp, &channel->sc_list) {
  268. cur_sc = list_entry(cur, struct vmbus_channel,
  269. sc_list);
  270. if (cur_sc->offermsg.child_relid == relid) {
  271. found_channel = cur_sc;
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. return found_channel;
  278. }
  279. /*
  280. * vmbus_on_event - Process a channel event notification
  281. *
  282. * For batched channels (default) optimize host to guest signaling
  283. * by ensuring:
  284. * 1. While reading the channel, we disable interrupts from host.
  285. * 2. Ensure that we process all posted messages from the host
  286. * before returning from this callback.
  287. * 3. Once we return, enable signaling from the host. Once this
  288. * state is set we check to see if additional packets are
  289. * available to read. In this case we repeat the process.
  290. * If this tasklet has been running for a long time
  291. * then reschedule ourselves.
  292. */
  293. void vmbus_on_event(unsigned long data)
  294. {
  295. struct vmbus_channel *channel = (void *) data;
  296. unsigned long time_limit = jiffies + 2;
  297. trace_vmbus_on_event(channel);
  298. do {
  299. void (*callback_fn)(void *);
  300. /* A channel once created is persistent even when
  301. * there is no driver handling the device. An
  302. * unloading driver sets the onchannel_callback to NULL.
  303. */
  304. callback_fn = READ_ONCE(channel->onchannel_callback);
  305. if (unlikely(callback_fn == NULL))
  306. return;
  307. (*callback_fn)(channel->channel_callback_context);
  308. if (channel->callback_mode != HV_CALL_BATCHED)
  309. return;
  310. if (likely(hv_end_read(&channel->inbound) == 0))
  311. return;
  312. hv_begin_read(&channel->inbound);
  313. } while (likely(time_before(jiffies, time_limit)));
  314. /* The time limit (2 jiffies) has been reached */
  315. tasklet_schedule(&channel->callback_event);
  316. }
  317. /*
  318. * vmbus_post_msg - Send a msg on the vmbus's message connection
  319. */
  320. int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
  321. {
  322. struct vmbus_channel_message_header *hdr;
  323. union hv_connection_id conn_id;
  324. int ret = 0;
  325. int retries = 0;
  326. u32 usec = 1;
  327. conn_id.asu32 = 0;
  328. conn_id.u.id = vmbus_connection.msg_conn_id;
  329. /*
  330. * hv_post_message() can have transient failures because of
  331. * insufficient resources. Retry the operation a couple of
  332. * times before giving up.
  333. */
  334. while (retries < 100) {
  335. ret = hv_post_message(conn_id, 1, buffer, buflen);
  336. switch (ret) {
  337. case HV_STATUS_INVALID_CONNECTION_ID:
  338. /*
  339. * See vmbus_negotiate_version(): VMBus protocol 5.0
  340. * requires that we must use
  341. * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
  342. * Contact message, but on old hosts that only
  343. * support VMBus protocol 4.0 or lower, here we get
  344. * HV_STATUS_INVALID_CONNECTION_ID and we should
  345. * return an error immediately without retrying.
  346. */
  347. hdr = buffer;
  348. if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
  349. return -EINVAL;
  350. /*
  351. * We could get this if we send messages too
  352. * frequently.
  353. */
  354. ret = -EAGAIN;
  355. break;
  356. case HV_STATUS_INSUFFICIENT_MEMORY:
  357. case HV_STATUS_INSUFFICIENT_BUFFERS:
  358. ret = -ENOBUFS;
  359. break;
  360. case HV_STATUS_SUCCESS:
  361. return ret;
  362. default:
  363. pr_err("hv_post_msg() failed; error code:%d\n", ret);
  364. return -EINVAL;
  365. }
  366. retries++;
  367. if (can_sleep && usec > 1000)
  368. msleep(usec / 1000);
  369. else if (usec < MAX_UDELAY_MS * 1000)
  370. udelay(usec);
  371. else
  372. mdelay(usec / 1000);
  373. if (retries < 22)
  374. usec *= 2;
  375. }
  376. return ret;
  377. }
  378. /*
  379. * vmbus_set_event - Send an event notification to the parent
  380. */
  381. void vmbus_set_event(struct vmbus_channel *channel)
  382. {
  383. u32 child_relid = channel->offermsg.child_relid;
  384. if (!channel->is_dedicated_interrupt)
  385. vmbus_send_interrupt(child_relid);
  386. ++channel->sig_events;
  387. hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
  388. }
  389. EXPORT_SYMBOL_GPL(vmbus_set_event);