Drivers: hv: vmbus: Clean up hv_sock channels by force upon suspend
[linux-2.6-block.git] / drivers / hv / connection.c
CommitLineData
3b20eb23 1// SPDX-License-Identifier: GPL-2.0-only
3e7ee490
HJ
2/*
3 *
4 * Copyright (c) 2009, Microsoft Corporation.
5 *
3e7ee490
HJ
6 * Authors:
7 * Haiyang Zhang <haiyangz@microsoft.com>
8 * Hank Janssen <hjanssen@microsoft.com>
3e7ee490 9 */
0a46618d
HJ
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
a0086dc5 12#include <linux/kernel.h>
0c3b7b2f
S
13#include <linux/sched.h>
14#include <linux/wait.h>
5289d3d1 15#include <linux/delay.h>
a0086dc5 16#include <linux/mm.h>
5a0e3ad6 17#include <linux/slab.h>
a0086dc5 18#include <linux/vmalloc.h>
46a97191 19#include <linux/hyperv.h>
37f7278b 20#include <linux/export.h>
fc53662f
VK
21#include <asm/mshyperv.h>
22
0f2a6619 23#include "hyperv_vmbus.h"
3e7ee490 24
3e7ee490 25
da9fcb72
HZ
26struct vmbus_connection vmbus_connection = {
27 .conn_state = DISCONNECTED,
28 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
3e7ee490 29};
95096f2f 30EXPORT_SYMBOL_GPL(vmbus_connection);
3e7ee490 31
37f7278b
S
32/*
33 * Negotiated protocol version with the host.
34 */
35__u32 vmbus_proto_version;
36EXPORT_SYMBOL_GPL(vmbus_proto_version);
37
610071c3
S
38static __u32 vmbus_get_next_version(__u32 current_version)
39{
40 switch (current_version) {
41 case (VERSION_WIN7):
42 return VERSION_WS2008;
43
44 case (VERSION_WIN8):
45 return VERSION_WIN7;
46
03367ef5
S
47 case (VERSION_WIN8_1):
48 return VERSION_WIN8;
49
6c4e5f9c
KM
50 case (VERSION_WIN10):
51 return VERSION_WIN8_1;
52
ae20b254
DC
53 case (VERSION_WIN10_V5):
54 return VERSION_WIN10;
55
610071c3
S
56 case (VERSION_WS2008):
57 default:
58 return VERSION_INVAL;
59 }
60}
61
f53335e3 62int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
610071c3
S
63{
64 int ret = 0;
41e270f6 65 unsigned int cur_cpu;
610071c3
S
66 struct vmbus_channel_initiate_contact *msg;
67 unsigned long flags;
610071c3
S
68
69 init_completion(&msginfo->waitevent);
70
71 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
72
ae20b254 73 memset(msg, 0, sizeof(*msg));
610071c3
S
74 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
75 msg->vmbus_version_requested = version;
ae20b254
DC
76
77 /*
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
84 * compatibility.
85 *
86 * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
87 */
88 if (version >= VERSION_WIN10_V5) {
89 msg->msg_sint = VMBUS_MESSAGE_SINT;
90 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
91 } else {
92 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
93 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
94 }
95
8681db44
GKH
96 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
97 msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
b282e4c0
S
98 /*
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
102 * would be ok.
72686447
AN
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.
b282e4c0 106 */
54a66265 107 if (version >= VERSION_WIN8_1) {
41e270f6
DC
108 cur_cpu = get_cpu();
109 msg->target_vcpu = hv_cpu_number_to_vp_number(cur_cpu);
110 vmbus_connection.connect_cpu = cur_cpu;
111 put_cpu();
54a66265 112 } else {
72686447 113 msg->target_vcpu = 0;
54a66265
S
114 vmbus_connection.connect_cpu = 0;
115 }
610071c3
S
116
117 /*
118 * Add to list before we send the request since we may
119 * receive the response before returning from this routine
120 */
121 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
122 list_add_tail(&msginfo->msglistentry,
123 &vmbus_connection.chn_msg_list);
124
125 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
126
127 ret = vmbus_post_msg(msg,
c0bb0392
VK
128 sizeof(struct vmbus_channel_initiate_contact),
129 true);
034ebf55
VK
130
131 trace_vmbus_negotiate_version(msg, ret);
132
610071c3
S
133 if (ret != 0) {
134 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
135 list_del(&msginfo->msglistentry);
136 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
137 flags);
138 return ret;
139 }
140
141 /* Wait for the connection response */
269f9794 142 wait_for_completion(&msginfo->waitevent);
610071c3
S
143
144 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
145 list_del(&msginfo->msglistentry);
146 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
147
148 /* Check if successful */
149 if (msginfo->response.version_response.version_supported) {
150 vmbus_connection.conn_state = CONNECTED;
ae20b254
DC
151
152 if (version >= VERSION_WIN10_V5)
153 vmbus_connection.msg_conn_id =
154 msginfo->response.version_response.msg_conn_id;
610071c3 155 } else {
610071c3
S
156 return -ECONNREFUSED;
157 }
158
159 return ret;
160}
161
3e189519 162/*
c6977677 163 * vmbus_connect - Sends a connect request on the partition service connection
fd8b85ea 164 */
c6977677 165int vmbus_connect(void)
3e7ee490 166{
fd8b85ea 167 int ret = 0;
15b2f647 168 struct vmbus_channel_msginfo *msginfo = NULL;
610071c3 169 __u32 version;
3e7ee490 170
454f18a9 171 /* Initialize the vmbus connection */
da9fcb72
HZ
172 vmbus_connection.conn_state = CONNECTING;
173 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
174 if (!vmbus_connection.work_queue) {
3a7546d9 175 ret = -ENOMEM;
b0043863 176 goto cleanup;
de65a384 177 }
3e7ee490 178
37c2578c
DC
179 vmbus_connection.handle_primary_chan_wq =
180 create_workqueue("hv_pri_chan");
181 if (!vmbus_connection.handle_primary_chan_wq) {
182 ret = -ENOMEM;
183 goto cleanup;
184 }
185
186 vmbus_connection.handle_sub_chan_wq =
187 create_workqueue("hv_sub_chan");
188 if (!vmbus_connection.handle_sub_chan_wq) {
189 ret = -ENOMEM;
190 goto cleanup;
191 }
192
da9fcb72 193 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
15b2f647 194 spin_lock_init(&vmbus_connection.channelmsg_lock);
3e7ee490 195
da9fcb72 196 INIT_LIST_HEAD(&vmbus_connection.chn_list);
d6f591e3 197 mutex_init(&vmbus_connection.channel_mutex);
3e7ee490 198
454f18a9
BP
199 /*
200 * Setup the vmbus event connection for channel interrupt
201 * abstraction stuff
202 */
df3493e0
S
203 vmbus_connection.int_page =
204 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
da9fcb72 205 if (vmbus_connection.int_page == NULL) {
3a7546d9 206 ret = -ENOMEM;
b0043863 207 goto cleanup;
3e7ee490
HJ
208 }
209
da9fcb72
HZ
210 vmbus_connection.recv_int_page = vmbus_connection.int_page;
211 vmbus_connection.send_int_page =
212 (void *)((unsigned long)vmbus_connection.int_page +
fd8b85ea 213 (PAGE_SIZE >> 1));
3e7ee490 214
fd8b85ea
GKH
215 /*
216 * Setup the monitor notification facility. The 1st page for
217 * parent->child and the 2nd page for child->parent
454f18a9 218 */
8681db44
GKH
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)) {
3a7546d9 223 ret = -ENOMEM;
b0043863 224 goto cleanup;
3e7ee490
HJ
225 }
226
15b2f647 227 msginfo = kzalloc(sizeof(*msginfo) +
fd8b85ea
GKH
228 sizeof(struct vmbus_channel_initiate_contact),
229 GFP_KERNEL);
15b2f647 230 if (msginfo == NULL) {
8cad0af9 231 ret = -ENOMEM;
b0043863 232 goto cleanup;
3e7ee490
HJ
233 }
234
454f18a9 235 /*
610071c3
S
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
239 * version.
454f18a9 240 */
3e7ee490 241
2a5c43a8 242 version = VERSION_CURRENT;
3e7ee490 243
610071c3
S
244 do {
245 ret = vmbus_negotiate_version(msginfo, version);
8bbf9f44 246 if (ret == -ETIMEDOUT)
666b9adc
S
247 goto cleanup;
248
249 if (vmbus_connection.conn_state == CONNECTED)
610071c3 250 break;
3e7ee490 251
610071c3
S
252 version = vmbus_get_next_version(version);
253 } while (version != VERSION_INVAL);
3e7ee490 254
610071c3 255 if (version == VERSION_INVAL)
b0043863 256 goto cleanup;
3e7ee490 257
37f7278b 258 vmbus_proto_version = version;
8de8af7e
S
259 pr_info("Vmbus version:%d.%d\n",
260 version >> 16, version & 0xFFFF);
3bacaf0c 261
15b2f647 262 kfree(msginfo);
3e7ee490
HJ
263 return 0;
264
b0043863 265cleanup:
3bacaf0c 266 pr_err("Unable to connect to host\n");
09a19628 267
da9fcb72 268 vmbus_connection.conn_state = DISCONNECTED;
09a19628
VK
269 vmbus_disconnect();
270
271 kfree(msginfo);
272
273 return ret;
274}
3e7ee490 275
09a19628
VK
276void vmbus_disconnect(void)
277{
2db84eff
S
278 /*
279 * First send the unload request to the host.
280 */
75ff3a8a 281 vmbus_initiate_unload(false);
2db84eff 282
37c2578c
DC
283 if (vmbus_connection.handle_sub_chan_wq)
284 destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
285
286 if (vmbus_connection.handle_primary_chan_wq)
287 destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
288
289 if (vmbus_connection.work_queue)
da9fcb72 290 destroy_workqueue(vmbus_connection.work_queue);
3e7ee490 291
da9fcb72 292 if (vmbus_connection.int_page) {
df3493e0 293 free_pages((unsigned long)vmbus_connection.int_page, 0);
da9fcb72 294 vmbus_connection.int_page = NULL;
3e7ee490
HJ
295 }
296
a100d88d
RK
297 free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
298 free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
8681db44
GKH
299 vmbus_connection.monitor_pages[0] = NULL;
300 vmbus_connection.monitor_pages[1] = NULL;
3e7ee490
HJ
301}
302
3e189519 303/*
c6977677
HZ
304 * relid2channel - Get the channel object given its
305 * child relative id (ie channel id)
fd8b85ea 306 */
d43e2fe7 307struct vmbus_channel *relid2channel(u32 relid)
3e7ee490 308{
aded7165 309 struct vmbus_channel *channel;
15b2f647 310 struct vmbus_channel *found_channel = NULL;
e68d2971
S
311 struct list_head *cur, *tmp;
312 struct vmbus_channel *cur_sc;
3e7ee490 313
85d9aa70
DC
314 BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
315
da9fcb72 316 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
15b2f647
HZ
317 if (channel->offermsg.child_relid == relid) {
318 found_channel = channel;
3e7ee490 319 break;
e68d2971
S
320 } else if (!list_empty(&channel->sc_list)) {
321 /*
322 * Deal with sub-channels.
323 */
324 list_for_each_safe(cur, tmp, &channel->sc_list) {
325 cur_sc = list_entry(cur, struct vmbus_channel,
326 sc_list);
327 if (cur_sc->offermsg.child_relid == relid) {
328 found_channel = cur_sc;
329 break;
330 }
331 }
3e7ee490
HJ
332 }
333 }
3e7ee490 334
15b2f647 335 return found_channel;
3e7ee490
HJ
336}
337
3e189519 338/*
631e63a9 339 * vmbus_on_event - Process a channel event notification
ada6eb11
SH
340 *
341 * For batched channels (default) optimize host to guest signaling
342 * by ensuring:
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.
fd8b85ea 351 */
631e63a9 352void vmbus_on_event(unsigned long data)
3e7ee490 353{
631e63a9 354 struct vmbus_channel *channel = (void *) data;
ada6eb11 355 unsigned long time_limit = jiffies + 2;
3e7ee490 356
991f8f1c
VK
357 trace_vmbus_on_event(channel);
358
ada6eb11
SH
359 do {
360 void (*callback_fn)(void *);
361
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.
f878f3d5 365 */
ada6eb11
SH
366 callback_fn = READ_ONCE(channel->onchannel_callback);
367 if (unlikely(callback_fn == NULL))
368 return;
f878f3d5 369
ada6eb11
SH
370 (*callback_fn)(channel->channel_callback_context);
371
372 if (channel->callback_mode != HV_CALL_BATCHED)
373 return;
374
375 if (likely(hv_end_read(&channel->inbound) == 0))
376 return;
377
378 hv_begin_read(&channel->inbound);
379 } while (likely(time_before(jiffies, time_limit)));
380
381 /* The time limit (2 jiffies) has been reached */
382 tasklet_schedule(&channel->callback_event);
3e7ee490
HJ
383}
384
3e189519 385/*
c6977677 386 * vmbus_post_msg - Send a msg on the vmbus's message connection
fd8b85ea 387 */
c0bb0392 388int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
3e7ee490 389{
ae20b254 390 struct vmbus_channel_message_header *hdr;
15b2f647 391 union hv_connection_id conn_id;
5289d3d1
S
392 int ret = 0;
393 int retries = 0;
8de0d7e9 394 u32 usec = 1;
3e7ee490 395
15b2f647 396 conn_id.asu32 = 0;
ae20b254 397 conn_id.u.id = vmbus_connection.msg_conn_id;
5289d3d1
S
398
399 /*
400 * hv_post_message() can have transient failures because of
401 * insufficient resources. Retry the operation a couple of
402 * times before giving up.
403 */
c0bb0392 404 while (retries < 100) {
fdeebcc6
S
405 ret = hv_post_message(conn_id, 1, buffer, buflen);
406
407 switch (ret) {
89f9f679 408 case HV_STATUS_INVALID_CONNECTION_ID:
ae20b254
DC
409 /*
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.
417 */
89760937 418 hdr = buffer;
ae20b254
DC
419 if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
420 return -EINVAL;
89f9f679
DC
421 /*
422 * We could get this if we send messages too
423 * frequently.
424 */
425 ret = -EAGAIN;
426 break;
427 case HV_STATUS_INSUFFICIENT_MEMORY:
fdeebcc6 428 case HV_STATUS_INSUFFICIENT_BUFFERS:
48f4ccdf 429 ret = -ENOBUFS;
fdeebcc6
S
430 break;
431 case HV_STATUS_SUCCESS:
5289d3d1 432 return ret;
fdeebcc6
S
433 default:
434 pr_err("hv_post_msg() failed; error code:%d\n", ret);
435 return -EINVAL;
436 }
437
5289d3d1 438 retries++;
c0bb0392
VK
439 if (can_sleep && usec > 1000)
440 msleep(usec / 1000);
441 else if (usec < MAX_UDELAY_MS * 1000)
442 udelay(usec);
443 else
444 mdelay(usec / 1000);
445
e917a5e2 446 if (retries < 22)
8de0d7e9 447 usec *= 2;
5289d3d1
S
448 }
449 return ret;
3e7ee490
HJ
450}
451
3e189519 452/*
c6977677 453 * vmbus_set_event - Send an event notification to the parent
fd8b85ea 454 */
1b807e10 455void vmbus_set_event(struct vmbus_channel *channel)
3e7ee490 456{
21c3bef5 457 u32 child_relid = channel->offermsg.child_relid;
7c369f40 458
5c1bec61
SH
459 if (!channel->is_dedicated_interrupt)
460 vmbus_send_interrupt(child_relid);
3be77774 461
6981fbf3
SH
462 ++channel->sig_events;
463
05784171 464 hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
3e7ee490 465}
5cc47247 466EXPORT_SYMBOL_GPL(vmbus_set_event);