1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2009, Microsoft Corporation.
7 * Haiyang Zhang <haiyangz@microsoft.com>
8 * Hank Janssen <hjanssen@microsoft.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/wait.h>
15 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/hyperv.h>
20 #include <linux/export.h>
21 #include <asm/mshyperv.h>
23 #include "hyperv_vmbus.h"
26 struct vmbus_connection vmbus_connection = {
27 .conn_state = DISCONNECTED,
28 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
30 EXPORT_SYMBOL_GPL(vmbus_connection);
33 * Negotiated protocol version with the host.
35 __u32 vmbus_proto_version;
36 EXPORT_SYMBOL_GPL(vmbus_proto_version);
38 static __u32 vmbus_get_next_version(__u32 current_version)
40 switch (current_version) {
42 return VERSION_WS2008;
47 case (VERSION_WIN8_1):
51 return VERSION_WIN8_1;
53 case (VERSION_WIN10_V5):
56 case (VERSION_WS2008):
62 int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
66 struct vmbus_channel_initiate_contact *msg;
69 init_completion(&msginfo->waitevent);
71 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
73 memset(msg, 0, sizeof(*msg));
74 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
75 msg->vmbus_version_requested = version;
78 * VMBus protocol 5.0 (VERSION_WIN10_V5) requires that we must use
79 * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
80 * and for subsequent messages, we must use the Message Connection ID
81 * field in the host-returned Version Response Message. And, with
82 * VERSION_WIN10_V5, we don't use msg->interrupt_page, but we tell
83 * the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
86 * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
88 if (version >= VERSION_WIN10_V5) {
89 msg->msg_sint = VMBUS_MESSAGE_SINT;
90 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
92 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
93 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
96 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
97 msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
99 * We want all channel messages to be delivered on CPU 0.
100 * This has been the behavior pre-win8. This is not
101 * perf issue and having all channel messages delivered on CPU 0
103 * For post win8 hosts, we support receiving channel messagges on
104 * all the CPUs. This is needed for kexec to work correctly where
105 * the CPU attempting to connect may not be CPU 0.
107 if (version >= VERSION_WIN8_1) {
109 msg->target_vcpu = hv_cpu_number_to_vp_number(cur_cpu);
110 vmbus_connection.connect_cpu = cur_cpu;
113 msg->target_vcpu = 0;
114 vmbus_connection.connect_cpu = 0;
118 * Add to list before we send the request since we may
119 * receive the response before returning from this routine
121 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
122 list_add_tail(&msginfo->msglistentry,
123 &vmbus_connection.chn_msg_list);
125 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
127 ret = vmbus_post_msg(msg,
128 sizeof(struct vmbus_channel_initiate_contact),
131 trace_vmbus_negotiate_version(msg, ret);
134 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
135 list_del(&msginfo->msglistentry);
136 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
141 /* Wait for the connection response */
142 wait_for_completion(&msginfo->waitevent);
144 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
145 list_del(&msginfo->msglistentry);
146 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
148 /* Check if successful */
149 if (msginfo->response.version_response.version_supported) {
150 vmbus_connection.conn_state = CONNECTED;
152 if (version >= VERSION_WIN10_V5)
153 vmbus_connection.msg_conn_id =
154 msginfo->response.version_response.msg_conn_id;
156 return -ECONNREFUSED;
163 * vmbus_connect - Sends a connect request on the partition service connection
165 int vmbus_connect(void)
168 struct vmbus_channel_msginfo *msginfo = NULL;
171 /* Initialize the vmbus connection */
172 vmbus_connection.conn_state = CONNECTING;
173 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
174 if (!vmbus_connection.work_queue) {
179 vmbus_connection.handle_primary_chan_wq =
180 create_workqueue("hv_pri_chan");
181 if (!vmbus_connection.handle_primary_chan_wq) {
186 vmbus_connection.handle_sub_chan_wq =
187 create_workqueue("hv_sub_chan");
188 if (!vmbus_connection.handle_sub_chan_wq) {
193 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
194 spin_lock_init(&vmbus_connection.channelmsg_lock);
196 INIT_LIST_HEAD(&vmbus_connection.chn_list);
197 mutex_init(&vmbus_connection.channel_mutex);
200 * Setup the vmbus event connection for channel interrupt
203 vmbus_connection.int_page =
204 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
205 if (vmbus_connection.int_page == NULL) {
210 vmbus_connection.recv_int_page = vmbus_connection.int_page;
211 vmbus_connection.send_int_page =
212 (void *)((unsigned long)vmbus_connection.int_page +
216 * Setup the monitor notification facility. The 1st page for
217 * parent->child and the 2nd page for child->parent
219 vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
220 vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
221 if ((vmbus_connection.monitor_pages[0] == NULL) ||
222 (vmbus_connection.monitor_pages[1] == NULL)) {
227 msginfo = kzalloc(sizeof(*msginfo) +
228 sizeof(struct vmbus_channel_initiate_contact),
230 if (msginfo == NULL) {
236 * Negotiate a compatible VMBUS version number with the
237 * host. We start with the highest number we can support
238 * and work our way down until we negotiate a compatible
242 version = VERSION_CURRENT;
245 ret = vmbus_negotiate_version(msginfo, version);
246 if (ret == -ETIMEDOUT)
249 if (vmbus_connection.conn_state == CONNECTED)
252 version = vmbus_get_next_version(version);
253 } while (version != VERSION_INVAL);
255 if (version == VERSION_INVAL)
258 vmbus_proto_version = version;
259 pr_info("Vmbus version:%d.%d\n",
260 version >> 16, version & 0xFFFF);
266 pr_err("Unable to connect to host\n");
268 vmbus_connection.conn_state = DISCONNECTED;
276 void vmbus_disconnect(void)
279 * First send the unload request to the host.
281 vmbus_initiate_unload(false);
283 if (vmbus_connection.handle_sub_chan_wq)
284 destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
286 if (vmbus_connection.handle_primary_chan_wq)
287 destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
289 if (vmbus_connection.work_queue)
290 destroy_workqueue(vmbus_connection.work_queue);
292 if (vmbus_connection.int_page) {
293 free_pages((unsigned long)vmbus_connection.int_page, 0);
294 vmbus_connection.int_page = NULL;
297 free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
298 free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
299 vmbus_connection.monitor_pages[0] = NULL;
300 vmbus_connection.monitor_pages[1] = NULL;
304 * relid2channel - Get the channel object given its
305 * child relative id (ie channel id)
307 struct vmbus_channel *relid2channel(u32 relid)
309 struct vmbus_channel *channel;
310 struct vmbus_channel *found_channel = NULL;
311 struct list_head *cur, *tmp;
312 struct vmbus_channel *cur_sc;
314 BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
316 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
317 if (channel->offermsg.child_relid == relid) {
318 found_channel = channel;
320 } else if (!list_empty(&channel->sc_list)) {
322 * Deal with sub-channels.
324 list_for_each_safe(cur, tmp, &channel->sc_list) {
325 cur_sc = list_entry(cur, struct vmbus_channel,
327 if (cur_sc->offermsg.child_relid == relid) {
328 found_channel = cur_sc;
335 return found_channel;
339 * vmbus_on_event - Process a channel event notification
341 * For batched channels (default) optimize host to guest signaling
343 * 1. While reading the channel, we disable interrupts from host.
344 * 2. Ensure that we process all posted messages from the host
345 * before returning from this callback.
346 * 3. Once we return, enable signaling from the host. Once this
347 * state is set we check to see if additional packets are
348 * available to read. In this case we repeat the process.
349 * If this tasklet has been running for a long time
350 * then reschedule ourselves.
352 void vmbus_on_event(unsigned long data)
354 struct vmbus_channel *channel = (void *) data;
355 unsigned long time_limit = jiffies + 2;
357 trace_vmbus_on_event(channel);
360 void (*callback_fn)(void *);
362 /* A channel once created is persistent even when
363 * there is no driver handling the device. An
364 * unloading driver sets the onchannel_callback to NULL.
366 callback_fn = READ_ONCE(channel->onchannel_callback);
367 if (unlikely(callback_fn == NULL))
370 (*callback_fn)(channel->channel_callback_context);
372 if (channel->callback_mode != HV_CALL_BATCHED)
375 if (likely(hv_end_read(&channel->inbound) == 0))
378 hv_begin_read(&channel->inbound);
379 } while (likely(time_before(jiffies, time_limit)));
381 /* The time limit (2 jiffies) has been reached */
382 tasklet_schedule(&channel->callback_event);
386 * vmbus_post_msg - Send a msg on the vmbus's message connection
388 int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
390 struct vmbus_channel_message_header *hdr;
391 union hv_connection_id conn_id;
397 conn_id.u.id = vmbus_connection.msg_conn_id;
400 * hv_post_message() can have transient failures because of
401 * insufficient resources. Retry the operation a couple of
402 * times before giving up.
404 while (retries < 100) {
405 ret = hv_post_message(conn_id, 1, buffer, buflen);
408 case HV_STATUS_INVALID_CONNECTION_ID:
410 * See vmbus_negotiate_version(): VMBus protocol 5.0
411 * requires that we must use
412 * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
413 * Contact message, but on old hosts that only
414 * support VMBus protocol 4.0 or lower, here we get
415 * HV_STATUS_INVALID_CONNECTION_ID and we should
416 * return an error immediately without retrying.
419 if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
422 * We could get this if we send messages too
427 case HV_STATUS_INSUFFICIENT_MEMORY:
428 case HV_STATUS_INSUFFICIENT_BUFFERS:
431 case HV_STATUS_SUCCESS:
434 pr_err("hv_post_msg() failed; error code:%d\n", ret);
439 if (can_sleep && usec > 1000)
441 else if (usec < MAX_UDELAY_MS * 1000)
453 * vmbus_set_event - Send an event notification to the parent
455 void vmbus_set_event(struct vmbus_channel *channel)
457 u32 child_relid = channel->offermsg.child_relid;
459 if (!channel->is_dedicated_interrupt)
460 vmbus_send_interrupt(child_relid);
462 ++channel->sig_events;
464 hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
466 EXPORT_SYMBOL_GPL(vmbus_set_event);