]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hv/connection.c
hyper-v: Use fast hypercall for HVCALL_SIGNAL_EVENT
[mirror_ubuntu-bionic-kernel.git] / drivers / hv / connection.c
CommitLineData
3e7ee490
HJ
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 */
0a46618d
HJ
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
a0086dc5 25#include <linux/kernel.h>
0c3b7b2f
S
26#include <linux/sched.h>
27#include <linux/wait.h>
5289d3d1 28#include <linux/delay.h>
a0086dc5 29#include <linux/mm.h>
5a0e3ad6 30#include <linux/slab.h>
a0086dc5 31#include <linux/vmalloc.h>
46a97191 32#include <linux/hyperv.h>
37f7278b 33#include <linux/export.h>
407dd164 34#include <asm/hyperv.h>
fc53662f
VK
35#include <asm/mshyperv.h>
36
0f2a6619 37#include "hyperv_vmbus.h"
3e7ee490 38
3e7ee490 39
da9fcb72
HZ
40struct vmbus_connection vmbus_connection = {
41 .conn_state = DISCONNECTED,
42 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
3e7ee490 43};
95096f2f 44EXPORT_SYMBOL_GPL(vmbus_connection);
3e7ee490 45
37f7278b
S
46/*
47 * Negotiated protocol version with the host.
48 */
49__u32 vmbus_proto_version;
50EXPORT_SYMBOL_GPL(vmbus_proto_version);
51
610071c3
S
52static __u32 vmbus_get_next_version(__u32 current_version)
53{
54 switch (current_version) {
55 case (VERSION_WIN7):
56 return VERSION_WS2008;
57
58 case (VERSION_WIN8):
59 return VERSION_WIN7;
60
03367ef5
S
61 case (VERSION_WIN8_1):
62 return VERSION_WIN8;
63
6c4e5f9c
KM
64 case (VERSION_WIN10):
65 return VERSION_WIN8_1;
66
610071c3
S
67 case (VERSION_WS2008):
68 default:
69 return VERSION_INVAL;
70 }
71}
72
73static int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo,
74 __u32 version)
75{
76 int ret = 0;
77 struct vmbus_channel_initiate_contact *msg;
78 unsigned long flags;
610071c3
S
79
80 init_completion(&msginfo->waitevent);
81
82 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
83
84 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
85 msg->vmbus_version_requested = version;
86 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
8681db44
GKH
87 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
88 msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
b282e4c0
S
89 /*
90 * We want all channel messages to be delivered on CPU 0.
91 * This has been the behavior pre-win8. This is not
92 * perf issue and having all channel messages delivered on CPU 0
93 * would be ok.
72686447
AN
94 * For post win8 hosts, we support receiving channel messagges on
95 * all the CPUs. This is needed for kexec to work correctly where
96 * the CPU attempting to connect may not be CPU 0.
b282e4c0 97 */
54a66265 98 if (version >= VERSION_WIN8_1) {
37cdd991 99 msg->target_vcpu = hv_context.vp_index[smp_processor_id()];
54a66265
S
100 vmbus_connection.connect_cpu = smp_processor_id();
101 } else {
72686447 102 msg->target_vcpu = 0;
54a66265
S
103 vmbus_connection.connect_cpu = 0;
104 }
610071c3
S
105
106 /*
107 * Add to list before we send the request since we may
108 * receive the response before returning from this routine
109 */
110 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
111 list_add_tail(&msginfo->msglistentry,
112 &vmbus_connection.chn_msg_list);
113
114 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
115
116 ret = vmbus_post_msg(msg,
c0bb0392
VK
117 sizeof(struct vmbus_channel_initiate_contact),
118 true);
610071c3
S
119 if (ret != 0) {
120 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
121 list_del(&msginfo->msglistentry);
122 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
123 flags);
124 return ret;
125 }
126
127 /* Wait for the connection response */
269f9794 128 wait_for_completion(&msginfo->waitevent);
610071c3
S
129
130 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
131 list_del(&msginfo->msglistentry);
132 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
133
134 /* Check if successful */
135 if (msginfo->response.version_response.version_supported) {
136 vmbus_connection.conn_state = CONNECTED;
137 } else {
610071c3
S
138 return -ECONNREFUSED;
139 }
140
141 return ret;
142}
143
3e189519 144/*
c6977677 145 * vmbus_connect - Sends a connect request on the partition service connection
fd8b85ea 146 */
c6977677 147int vmbus_connect(void)
3e7ee490 148{
fd8b85ea 149 int ret = 0;
15b2f647 150 struct vmbus_channel_msginfo *msginfo = NULL;
610071c3 151 __u32 version;
3e7ee490 152
454f18a9 153 /* Initialize the vmbus connection */
da9fcb72
HZ
154 vmbus_connection.conn_state = CONNECTING;
155 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
156 if (!vmbus_connection.work_queue) {
3a7546d9 157 ret = -ENOMEM;
b0043863 158 goto cleanup;
de65a384 159 }
3e7ee490 160
da9fcb72 161 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
15b2f647 162 spin_lock_init(&vmbus_connection.channelmsg_lock);
3e7ee490 163
da9fcb72 164 INIT_LIST_HEAD(&vmbus_connection.chn_list);
d6f591e3 165 mutex_init(&vmbus_connection.channel_mutex);
3e7ee490 166
454f18a9
BP
167 /*
168 * Setup the vmbus event connection for channel interrupt
169 * abstraction stuff
170 */
df3493e0
S
171 vmbus_connection.int_page =
172 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
da9fcb72 173 if (vmbus_connection.int_page == NULL) {
3a7546d9 174 ret = -ENOMEM;
b0043863 175 goto cleanup;
3e7ee490
HJ
176 }
177
da9fcb72
HZ
178 vmbus_connection.recv_int_page = vmbus_connection.int_page;
179 vmbus_connection.send_int_page =
180 (void *)((unsigned long)vmbus_connection.int_page +
fd8b85ea 181 (PAGE_SIZE >> 1));
3e7ee490 182
fd8b85ea
GKH
183 /*
184 * Setup the monitor notification facility. The 1st page for
185 * parent->child and the 2nd page for child->parent
454f18a9 186 */
8681db44
GKH
187 vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
188 vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
189 if ((vmbus_connection.monitor_pages[0] == NULL) ||
190 (vmbus_connection.monitor_pages[1] == NULL)) {
3a7546d9 191 ret = -ENOMEM;
b0043863 192 goto cleanup;
3e7ee490
HJ
193 }
194
15b2f647 195 msginfo = kzalloc(sizeof(*msginfo) +
fd8b85ea
GKH
196 sizeof(struct vmbus_channel_initiate_contact),
197 GFP_KERNEL);
15b2f647 198 if (msginfo == NULL) {
8cad0af9 199 ret = -ENOMEM;
b0043863 200 goto cleanup;
3e7ee490
HJ
201 }
202
454f18a9 203 /*
610071c3
S
204 * Negotiate a compatible VMBUS version number with the
205 * host. We start with the highest number we can support
206 * and work our way down until we negotiate a compatible
207 * version.
454f18a9 208 */
3e7ee490 209
2a5c43a8 210 version = VERSION_CURRENT;
3e7ee490 211
610071c3
S
212 do {
213 ret = vmbus_negotiate_version(msginfo, version);
8bbf9f44 214 if (ret == -ETIMEDOUT)
666b9adc
S
215 goto cleanup;
216
217 if (vmbus_connection.conn_state == CONNECTED)
610071c3 218 break;
3e7ee490 219
610071c3
S
220 version = vmbus_get_next_version(version);
221 } while (version != VERSION_INVAL);
3e7ee490 222
610071c3 223 if (version == VERSION_INVAL)
b0043863 224 goto cleanup;
3e7ee490 225
37f7278b 226 vmbus_proto_version = version;
8de8af7e
S
227 pr_info("Vmbus version:%d.%d\n",
228 version >> 16, version & 0xFFFF);
3bacaf0c 229
15b2f647 230 kfree(msginfo);
3e7ee490
HJ
231 return 0;
232
b0043863 233cleanup:
3bacaf0c 234 pr_err("Unable to connect to host\n");
09a19628 235
da9fcb72 236 vmbus_connection.conn_state = DISCONNECTED;
09a19628
VK
237 vmbus_disconnect();
238
239 kfree(msginfo);
240
241 return ret;
242}
3e7ee490 243
09a19628
VK
244void vmbus_disconnect(void)
245{
2db84eff
S
246 /*
247 * First send the unload request to the host.
248 */
75ff3a8a 249 vmbus_initiate_unload(false);
2db84eff 250
09a19628
VK
251 if (vmbus_connection.work_queue) {
252 drain_workqueue(vmbus_connection.work_queue);
da9fcb72 253 destroy_workqueue(vmbus_connection.work_queue);
09a19628 254 }
3e7ee490 255
da9fcb72 256 if (vmbus_connection.int_page) {
df3493e0 257 free_pages((unsigned long)vmbus_connection.int_page, 0);
da9fcb72 258 vmbus_connection.int_page = NULL;
3e7ee490
HJ
259 }
260
a100d88d
RK
261 free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
262 free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
8681db44
GKH
263 vmbus_connection.monitor_pages[0] = NULL;
264 vmbus_connection.monitor_pages[1] = NULL;
3e7ee490
HJ
265}
266
3e189519 267/*
c6977677
HZ
268 * relid2channel - Get the channel object given its
269 * child relative id (ie channel id)
fd8b85ea 270 */
d43e2fe7 271struct vmbus_channel *relid2channel(u32 relid)
3e7ee490 272{
aded7165 273 struct vmbus_channel *channel;
15b2f647 274 struct vmbus_channel *found_channel = NULL;
e68d2971
S
275 struct list_head *cur, *tmp;
276 struct vmbus_channel *cur_sc;
3e7ee490 277
85d9aa70
DC
278 BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
279
da9fcb72 280 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
15b2f647
HZ
281 if (channel->offermsg.child_relid == relid) {
282 found_channel = channel;
3e7ee490 283 break;
e68d2971
S
284 } else if (!list_empty(&channel->sc_list)) {
285 /*
286 * Deal with sub-channels.
287 */
288 list_for_each_safe(cur, tmp, &channel->sc_list) {
289 cur_sc = list_entry(cur, struct vmbus_channel,
290 sc_list);
291 if (cur_sc->offermsg.child_relid == relid) {
292 found_channel = cur_sc;
293 break;
294 }
295 }
3e7ee490
HJ
296 }
297 }
3e7ee490 298
15b2f647 299 return found_channel;
3e7ee490
HJ
300}
301
3e189519 302/*
631e63a9 303 * vmbus_on_event - Process a channel event notification
ada6eb11
SH
304 *
305 * For batched channels (default) optimize host to guest signaling
306 * by ensuring:
307 * 1. While reading the channel, we disable interrupts from host.
308 * 2. Ensure that we process all posted messages from the host
309 * before returning from this callback.
310 * 3. Once we return, enable signaling from the host. Once this
311 * state is set we check to see if additional packets are
312 * available to read. In this case we repeat the process.
313 * If this tasklet has been running for a long time
314 * then reschedule ourselves.
fd8b85ea 315 */
631e63a9 316void vmbus_on_event(unsigned long data)
3e7ee490 317{
631e63a9 318 struct vmbus_channel *channel = (void *) data;
ada6eb11 319 unsigned long time_limit = jiffies + 2;
3e7ee490 320
ada6eb11
SH
321 do {
322 void (*callback_fn)(void *);
323
324 /* A channel once created is persistent even when
325 * there is no driver handling the device. An
326 * unloading driver sets the onchannel_callback to NULL.
f878f3d5 327 */
ada6eb11
SH
328 callback_fn = READ_ONCE(channel->onchannel_callback);
329 if (unlikely(callback_fn == NULL))
330 return;
f878f3d5 331
ada6eb11
SH
332 (*callback_fn)(channel->channel_callback_context);
333
334 if (channel->callback_mode != HV_CALL_BATCHED)
335 return;
336
337 if (likely(hv_end_read(&channel->inbound) == 0))
338 return;
339
340 hv_begin_read(&channel->inbound);
341 } while (likely(time_before(jiffies, time_limit)));
342
343 /* The time limit (2 jiffies) has been reached */
344 tasklet_schedule(&channel->callback_event);
3e7ee490
HJ
345}
346
3e189519 347/*
c6977677 348 * vmbus_post_msg - Send a msg on the vmbus's message connection
fd8b85ea 349 */
c0bb0392 350int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
3e7ee490 351{
15b2f647 352 union hv_connection_id conn_id;
5289d3d1
S
353 int ret = 0;
354 int retries = 0;
8de0d7e9 355 u32 usec = 1;
3e7ee490 356
15b2f647
HZ
357 conn_id.asu32 = 0;
358 conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
5289d3d1
S
359
360 /*
361 * hv_post_message() can have transient failures because of
362 * insufficient resources. Retry the operation a couple of
363 * times before giving up.
364 */
c0bb0392 365 while (retries < 100) {
fdeebcc6
S
366 ret = hv_post_message(conn_id, 1, buffer, buflen);
367
368 switch (ret) {
89f9f679
DC
369 case HV_STATUS_INVALID_CONNECTION_ID:
370 /*
371 * We could get this if we send messages too
372 * frequently.
373 */
374 ret = -EAGAIN;
375 break;
376 case HV_STATUS_INSUFFICIENT_MEMORY:
fdeebcc6 377 case HV_STATUS_INSUFFICIENT_BUFFERS:
48f4ccdf 378 ret = -ENOBUFS;
fdeebcc6
S
379 break;
380 case HV_STATUS_SUCCESS:
5289d3d1 381 return ret;
fdeebcc6
S
382 default:
383 pr_err("hv_post_msg() failed; error code:%d\n", ret);
384 return -EINVAL;
385 }
386
5289d3d1 387 retries++;
c0bb0392
VK
388 if (can_sleep && usec > 1000)
389 msleep(usec / 1000);
390 else if (usec < MAX_UDELAY_MS * 1000)
391 udelay(usec);
392 else
393 mdelay(usec / 1000);
394
e917a5e2 395 if (retries < 22)
8de0d7e9 396 usec *= 2;
5289d3d1
S
397 }
398 return ret;
3e7ee490
HJ
399}
400
3e189519 401/*
c6977677 402 * vmbus_set_event - Send an event notification to the parent
fd8b85ea 403 */
1b807e10 404void vmbus_set_event(struct vmbus_channel *channel)
3e7ee490 405{
21c3bef5 406 u32 child_relid = channel->offermsg.child_relid;
7c369f40 407
5c1bec61
SH
408 if (!channel->is_dedicated_interrupt)
409 vmbus_send_interrupt(child_relid);
3be77774 410
05784171 411 hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
3e7ee490 412}
5cc47247 413EXPORT_SYMBOL_GPL(vmbus_set_event);