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