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