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