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 static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo,
67 struct vmbus_channel_initiate_contact *msg;
70 init_completion(&msginfo->waitevent);
72 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
74 memset(msg, 0, sizeof(*msg));
75 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
76 msg->vmbus_version_requested = version;
79 * VMBus protocol 5.0 (VERSION_WIN10_V5) requires that we must use
80 * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
81 * and for subsequent messages, we must use the Message Connection ID
82 * field in the host-returned Version Response Message. And, with
83 * VERSION_WIN10_V5, we don't use msg->interrupt_page, but we tell
84 * the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
87 * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
89 if (version >= VERSION_WIN10_V5) {
90 msg->msg_sint = VMBUS_MESSAGE_SINT;
91 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
93 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
94 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
97 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
98 msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
100 * We want all channel messages to be delivered on CPU 0.
101 * This has been the behavior pre-win8. This is not
102 * perf issue and having all channel messages delivered on CPU 0
104 * For post win8 hosts, we support receiving channel messagges on
105 * all the CPUs. This is needed for kexec to work correctly where
106 * the CPU attempting to connect may not be CPU 0.
108 if (version >= VERSION_WIN8_1) {
110 msg->target_vcpu = hv_cpu_number_to_vp_number(cur_cpu);
111 vmbus_connection.connect_cpu = cur_cpu;
114 msg->target_vcpu = 0;
115 vmbus_connection.connect_cpu = 0;
119 * Add to list before we send the request since we may
120 * receive the response before returning from this routine
122 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
123 list_add_tail(&msginfo->msglistentry,
124 &vmbus_connection.chn_msg_list);
126 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
128 ret = vmbus_post_msg(msg,
129 sizeof(struct vmbus_channel_initiate_contact),
132 trace_vmbus_negotiate_version(msg, ret);
135 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
136 list_del(&msginfo->msglistentry);
137 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
142 /* Wait for the connection response */
143 wait_for_completion(&msginfo->waitevent);
145 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
146 list_del(&msginfo->msglistentry);
147 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
149 /* Check if successful */
150 if (msginfo->response.version_response.version_supported) {
151 vmbus_connection.conn_state = CONNECTED;
153 if (version >= VERSION_WIN10_V5)
154 vmbus_connection.msg_conn_id =
155 msginfo->response.version_response.msg_conn_id;
157 return -ECONNREFUSED;
164 * vmbus_connect - Sends a connect request on the partition service connection
166 int vmbus_connect(void)
169 struct vmbus_channel_msginfo *msginfo = NULL;
172 /* Initialize the vmbus connection */
173 vmbus_connection.conn_state = CONNECTING;
174 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
175 if (!vmbus_connection.work_queue) {
180 vmbus_connection.handle_primary_chan_wq =
181 create_workqueue("hv_pri_chan");
182 if (!vmbus_connection.handle_primary_chan_wq) {
187 vmbus_connection.handle_sub_chan_wq =
188 create_workqueue("hv_sub_chan");
189 if (!vmbus_connection.handle_sub_chan_wq) {
194 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
195 spin_lock_init(&vmbus_connection.channelmsg_lock);
197 INIT_LIST_HEAD(&vmbus_connection.chn_list);
198 mutex_init(&vmbus_connection.channel_mutex);
201 * Setup the vmbus event connection for channel interrupt
204 vmbus_connection.int_page =
205 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
206 if (vmbus_connection.int_page == NULL) {
211 vmbus_connection.recv_int_page = vmbus_connection.int_page;
212 vmbus_connection.send_int_page =
213 (void *)((unsigned long)vmbus_connection.int_page +
217 * Setup the monitor notification facility. The 1st page for
218 * parent->child and the 2nd page for child->parent
220 vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
221 vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
222 if ((vmbus_connection.monitor_pages[0] == NULL) ||
223 (vmbus_connection.monitor_pages[1] == NULL)) {
228 msginfo = kzalloc(sizeof(*msginfo) +
229 sizeof(struct vmbus_channel_initiate_contact),
231 if (msginfo == NULL) {
237 * Negotiate a compatible VMBUS version number with the
238 * host. We start with the highest number we can support
239 * and work our way down until we negotiate a compatible
243 version = VERSION_CURRENT;
246 ret = vmbus_negotiate_version(msginfo, version);
247 if (ret == -ETIMEDOUT)
250 if (vmbus_connection.conn_state == CONNECTED)
253 version = vmbus_get_next_version(version);
254 } while (version != VERSION_INVAL);
256 if (version == VERSION_INVAL)
259 vmbus_proto_version = version;
260 pr_info("Vmbus version:%d.%d\n",
261 version >> 16, version & 0xFFFF);
267 pr_err("Unable to connect to host\n");
269 vmbus_connection.conn_state = DISCONNECTED;
277 void vmbus_disconnect(void)
280 * First send the unload request to the host.
282 vmbus_initiate_unload(false);
284 if (vmbus_connection.handle_sub_chan_wq)
285 destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
287 if (vmbus_connection.handle_primary_chan_wq)
288 destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
290 if (vmbus_connection.work_queue)
291 destroy_workqueue(vmbus_connection.work_queue);
293 if (vmbus_connection.int_page) {
294 free_pages((unsigned long)vmbus_connection.int_page, 0);
295 vmbus_connection.int_page = NULL;
298 free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
299 free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
300 vmbus_connection.monitor_pages[0] = NULL;
301 vmbus_connection.monitor_pages[1] = NULL;
305 * relid2channel - Get the channel object given its
306 * child relative id (ie channel id)
308 struct vmbus_channel *relid2channel(u32 relid)
310 struct vmbus_channel *channel;
311 struct vmbus_channel *found_channel = NULL;
312 struct list_head *cur, *tmp;
313 struct vmbus_channel *cur_sc;
315 BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
317 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
318 if (channel->offermsg.child_relid == relid) {
319 found_channel = channel;
321 } else if (!list_empty(&channel->sc_list)) {
323 * Deal with sub-channels.
325 list_for_each_safe(cur, tmp, &channel->sc_list) {
326 cur_sc = list_entry(cur, struct vmbus_channel,
328 if (cur_sc->offermsg.child_relid == relid) {
329 found_channel = cur_sc;
336 return found_channel;
340 * vmbus_on_event - Process a channel event notification
342 * For batched channels (default) optimize host to guest signaling
344 * 1. While reading the channel, we disable interrupts from host.
345 * 2. Ensure that we process all posted messages from the host
346 * before returning from this callback.
347 * 3. Once we return, enable signaling from the host. Once this
348 * state is set we check to see if additional packets are
349 * available to read. In this case we repeat the process.
350 * If this tasklet has been running for a long time
351 * then reschedule ourselves.
353 void vmbus_on_event(unsigned long data)
355 struct vmbus_channel *channel = (void *) data;
356 unsigned long time_limit = jiffies + 2;
358 trace_vmbus_on_event(channel);
361 void (*callback_fn)(void *);
363 /* A channel once created is persistent even when
364 * there is no driver handling the device. An
365 * unloading driver sets the onchannel_callback to NULL.
367 callback_fn = READ_ONCE(channel->onchannel_callback);
368 if (unlikely(callback_fn == NULL))
371 (*callback_fn)(channel->channel_callback_context);
373 if (channel->callback_mode != HV_CALL_BATCHED)
376 if (likely(hv_end_read(&channel->inbound) == 0))
379 hv_begin_read(&channel->inbound);
380 } while (likely(time_before(jiffies, time_limit)));
382 /* The time limit (2 jiffies) has been reached */
383 tasklet_schedule(&channel->callback_event);
387 * vmbus_post_msg - Send a msg on the vmbus's message connection
389 int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
391 struct vmbus_channel_message_header *hdr;
392 union hv_connection_id conn_id;
398 conn_id.u.id = vmbus_connection.msg_conn_id;
401 * hv_post_message() can have transient failures because of
402 * insufficient resources. Retry the operation a couple of
403 * times before giving up.
405 while (retries < 100) {
406 ret = hv_post_message(conn_id, 1, buffer, buflen);
409 case HV_STATUS_INVALID_CONNECTION_ID:
411 * See vmbus_negotiate_version(): VMBus protocol 5.0
412 * requires that we must use
413 * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
414 * Contact message, but on old hosts that only
415 * support VMBus protocol 4.0 or lower, here we get
416 * HV_STATUS_INVALID_CONNECTION_ID and we should
417 * return an error immediately without retrying.
420 if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
423 * We could get this if we send messages too
428 case HV_STATUS_INSUFFICIENT_MEMORY:
429 case HV_STATUS_INSUFFICIENT_BUFFERS:
432 case HV_STATUS_SUCCESS:
435 pr_err("hv_post_msg() failed; error code:%d\n", ret);
440 if (can_sleep && usec > 1000)
442 else if (usec < MAX_UDELAY_MS * 1000)
454 * vmbus_set_event - Send an event notification to the parent
456 void vmbus_set_event(struct vmbus_channel *channel)
458 u32 child_relid = channel->offermsg.child_relid;
460 if (!channel->is_dedicated_interrupt)
461 vmbus_send_interrupt(child_relid);
463 ++channel->sig_events;
465 hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
467 EXPORT_SYMBOL_GPL(vmbus_set_event);