1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 *
4 * Copyright (c) 2009, Microsoft Corporation.
5 *
6 * Authors:
7 * Haiyang Zhang <haiyangz@microsoft.com>
8 * Hank Janssen <hjanssen@microsoft.com>
9 */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/wait.h>
15 #include <linux/delay.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/hyperv.h>
21 #include <linux/export.h>
22 #include <linux/io.h>
23 #include <linux/set_memory.h>
24 #include <asm/mem_encrypt.h>
25 #include <asm/mshyperv.h>
26
27 #include "hyperv_vmbus.h"
28
29
30 struct vmbus_connection vmbus_connection = {
31 .conn_state = DISCONNECTED,
32 .unload_event = COMPLETION_INITIALIZER(
33 vmbus_connection.unload_event),
34 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
35
36 .ready_for_suspend_event = COMPLETION_INITIALIZER(
37 vmbus_connection.ready_for_suspend_event),
38 .ready_for_resume_event = COMPLETION_INITIALIZER(
39 vmbus_connection.ready_for_resume_event),
40 };
41 EXPORT_SYMBOL_GPL(vmbus_connection);
42
43 /*
44 * Negotiated protocol version with the host.
45 */
46 __u32 vmbus_proto_version;
47 EXPORT_SYMBOL_GPL(vmbus_proto_version);
48
49 /*
50 * Table of VMBus versions listed from newest to oldest.
51 * VERSION_WIN7 and VERSION_WS2008 are no longer supported in
52 * Linux guests and are not listed.
53 */
54 static __u32 vmbus_versions[] = {
55 VERSION_WIN10_V5_3,
56 VERSION_WIN10_V5_2,
57 VERSION_WIN10_V5_1,
58 VERSION_WIN10_V5,
59 VERSION_WIN10_V4_1,
60 VERSION_WIN10,
61 VERSION_WIN8_1,
62 VERSION_WIN8
63 };
64
65 /*
66 * Maximal VMBus protocol version guests can negotiate. Useful to cap the
67 * VMBus version for testing and debugging purpose.
68 */
69 static uint max_version = VERSION_WIN10_V5_3;
70
71 module_param(max_version, uint, S_IRUGO);
72 MODULE_PARM_DESC(max_version,
73 "Maximal VMBus protocol version which can be negotiated");
74
vmbus_negotiate_version(struct vmbus_channel_msginfo * msginfo,u32 version)75 int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
76 {
77 int ret = 0;
78 struct vmbus_channel_initiate_contact *msg;
79 unsigned long flags;
80
81 init_completion(&msginfo->waitevent);
82
83 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
84
85 memset(msg, 0, sizeof(*msg));
86 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
87 msg->vmbus_version_requested = version;
88
89 /*
90 * VMBus protocol 5.0 (VERSION_WIN10_V5) and higher require that we must
91 * use VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
92 * and for subsequent messages, we must use the Message Connection ID
93 * field in the host-returned Version Response Message. And, with
94 * VERSION_WIN10_V5 and higher, we don't use msg->interrupt_page, but we
95 * tell the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
96 * compatibility.
97 *
98 * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
99 */
100 if (version >= VERSION_WIN10_V5) {
101 msg->msg_sint = VMBUS_MESSAGE_SINT;
102 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
103 } else {
104 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
105 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
106 }
107
108 msg->monitor_page1 = vmbus_connection.monitor_pages_pa[0];
109 msg->monitor_page2 = vmbus_connection.monitor_pages_pa[1];
110
111 msg->target_vcpu = hv_cpu_number_to_vp_number(VMBUS_CONNECT_CPU);
112
113 /*
114 * Add to list before we send the request since we may
115 * receive the response before returning from this routine
116 */
117 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
118 list_add_tail(&msginfo->msglistentry,
119 &vmbus_connection.chn_msg_list);
120
121 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
122
123 ret = vmbus_post_msg(msg,
124 sizeof(struct vmbus_channel_initiate_contact),
125 true);
126
127 trace_vmbus_negotiate_version(msg, ret);
128
129 if (ret != 0) {
130 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
131 list_del(&msginfo->msglistentry);
132 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
133 flags);
134 return ret;
135 }
136
137 /* Wait for the connection response */
138 wait_for_completion(&msginfo->waitevent);
139
140 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
141 list_del(&msginfo->msglistentry);
142 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
143
144 /* Check if successful */
145 if (msginfo->response.version_response.version_supported) {
146 vmbus_connection.conn_state = CONNECTED;
147
148 if (version >= VERSION_WIN10_V5)
149 vmbus_connection.msg_conn_id =
150 msginfo->response.version_response.msg_conn_id;
151 } else {
152 return -ECONNREFUSED;
153 }
154
155 return ret;
156 }
157
158 /*
159 * vmbus_connect - Sends a connect request on the partition service connection
160 */
vmbus_connect(void)161 int vmbus_connect(void)
162 {
163 struct vmbus_channel_msginfo *msginfo = NULL;
164 int i, ret = 0;
165 __u32 version;
166
167 /* Initialize the vmbus connection */
168 vmbus_connection.conn_state = CONNECTING;
169 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
170 if (!vmbus_connection.work_queue) {
171 ret = -ENOMEM;
172 goto cleanup;
173 }
174
175 vmbus_connection.rescind_work_queue =
176 create_workqueue("hv_vmbus_rescind");
177 if (!vmbus_connection.rescind_work_queue) {
178 ret = -ENOMEM;
179 goto cleanup;
180 }
181 vmbus_connection.ignore_any_offer_msg = false;
182
183 vmbus_connection.handle_primary_chan_wq =
184 create_workqueue("hv_pri_chan");
185 if (!vmbus_connection.handle_primary_chan_wq) {
186 ret = -ENOMEM;
187 goto cleanup;
188 }
189
190 vmbus_connection.handle_sub_chan_wq =
191 create_workqueue("hv_sub_chan");
192 if (!vmbus_connection.handle_sub_chan_wq) {
193 ret = -ENOMEM;
194 goto cleanup;
195 }
196
197 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
198 spin_lock_init(&vmbus_connection.channelmsg_lock);
199
200 INIT_LIST_HEAD(&vmbus_connection.chn_list);
201 mutex_init(&vmbus_connection.channel_mutex);
202
203 /*
204 * Setup the vmbus event connection for channel interrupt
205 * abstraction stuff
206 */
207 vmbus_connection.int_page =
208 (void *)hv_alloc_hyperv_zeroed_page();
209 if (vmbus_connection.int_page == NULL) {
210 ret = -ENOMEM;
211 goto cleanup;
212 }
213
214 vmbus_connection.recv_int_page = vmbus_connection.int_page;
215 vmbus_connection.send_int_page =
216 (void *)((unsigned long)vmbus_connection.int_page +
217 (HV_HYP_PAGE_SIZE >> 1));
218
219 /*
220 * Setup the monitor notification facility. The 1st page for
221 * parent->child and the 2nd page for child->parent
222 */
223 vmbus_connection.monitor_pages[0] = (void *)hv_alloc_hyperv_zeroed_page();
224 vmbus_connection.monitor_pages[1] = (void *)hv_alloc_hyperv_zeroed_page();
225 if ((vmbus_connection.monitor_pages[0] == NULL) ||
226 (vmbus_connection.monitor_pages[1] == NULL)) {
227 ret = -ENOMEM;
228 goto cleanup;
229 }
230
231 vmbus_connection.monitor_pages_original[0]
232 = vmbus_connection.monitor_pages[0];
233 vmbus_connection.monitor_pages_original[1]
234 = vmbus_connection.monitor_pages[1];
235 vmbus_connection.monitor_pages_pa[0]
236 = virt_to_phys(vmbus_connection.monitor_pages[0]);
237 vmbus_connection.monitor_pages_pa[1]
238 = virt_to_phys(vmbus_connection.monitor_pages[1]);
239
240 if (hv_is_isolation_supported()) {
241 ret = set_memory_decrypted((unsigned long)
242 vmbus_connection.monitor_pages[0],
243 1);
244 ret |= set_memory_decrypted((unsigned long)
245 vmbus_connection.monitor_pages[1],
246 1);
247 if (ret)
248 goto cleanup;
249
250 /*
251 * Isolation VM with AMD SNP needs to access monitor page via
252 * address space above shared gpa boundary.
253 */
254 if (hv_isolation_type_snp()) {
255 vmbus_connection.monitor_pages_pa[0] +=
256 ms_hyperv.shared_gpa_boundary;
257 vmbus_connection.monitor_pages_pa[1] +=
258 ms_hyperv.shared_gpa_boundary;
259
260 vmbus_connection.monitor_pages[0]
261 = memremap(vmbus_connection.monitor_pages_pa[0],
262 HV_HYP_PAGE_SIZE,
263 MEMREMAP_WB);
264 if (!vmbus_connection.monitor_pages[0]) {
265 ret = -ENOMEM;
266 goto cleanup;
267 }
268
269 vmbus_connection.monitor_pages[1]
270 = memremap(vmbus_connection.monitor_pages_pa[1],
271 HV_HYP_PAGE_SIZE,
272 MEMREMAP_WB);
273 if (!vmbus_connection.monitor_pages[1]) {
274 ret = -ENOMEM;
275 goto cleanup;
276 }
277 }
278
279 /*
280 * Set memory host visibility hvcall smears memory
281 * and so zero monitor pages here.
282 */
283 memset(vmbus_connection.monitor_pages[0], 0x00,
284 HV_HYP_PAGE_SIZE);
285 memset(vmbus_connection.monitor_pages[1], 0x00,
286 HV_HYP_PAGE_SIZE);
287
288 }
289
290 msginfo = kzalloc(sizeof(*msginfo) +
291 sizeof(struct vmbus_channel_initiate_contact),
292 GFP_KERNEL);
293 if (msginfo == NULL) {
294 ret = -ENOMEM;
295 goto cleanup;
296 }
297
298 /*
299 * Negotiate a compatible VMBUS version number with the
300 * host. We start with the highest number we can support
301 * and work our way down until we negotiate a compatible
302 * version.
303 */
304
305 for (i = 0; ; i++) {
306 if (i == ARRAY_SIZE(vmbus_versions)) {
307 ret = -EDOM;
308 goto cleanup;
309 }
310
311 version = vmbus_versions[i];
312 if (version > max_version)
313 continue;
314
315 ret = vmbus_negotiate_version(msginfo, version);
316 if (ret == -ETIMEDOUT)
317 goto cleanup;
318
319 if (vmbus_connection.conn_state == CONNECTED)
320 break;
321 }
322
323 if (hv_is_isolation_supported() && version < VERSION_WIN10_V5_2) {
324 pr_err("Invalid VMBus version %d.%d (expected >= %d.%d) from the host supporting isolation\n",
325 version >> 16, version & 0xFFFF, VERSION_WIN10_V5_2 >> 16, VERSION_WIN10_V5_2 & 0xFFFF);
326 ret = -EINVAL;
327 goto cleanup;
328 }
329
330 vmbus_proto_version = version;
331 pr_info("Vmbus version:%d.%d\n",
332 version >> 16, version & 0xFFFF);
333
334 vmbus_connection.channels = kcalloc(MAX_CHANNEL_RELIDS,
335 sizeof(struct vmbus_channel *),
336 GFP_KERNEL);
337 if (vmbus_connection.channels == NULL) {
338 ret = -ENOMEM;
339 goto cleanup;
340 }
341
342 kfree(msginfo);
343 return 0;
344
345 cleanup:
346 pr_err("Unable to connect to host\n");
347
348 vmbus_connection.conn_state = DISCONNECTED;
349 vmbus_disconnect();
350
351 kfree(msginfo);
352
353 return ret;
354 }
355
vmbus_disconnect(void)356 void vmbus_disconnect(void)
357 {
358 /*
359 * First send the unload request to the host.
360 */
361 vmbus_initiate_unload(false);
362
363 if (vmbus_connection.handle_sub_chan_wq)
364 destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
365
366 if (vmbus_connection.handle_primary_chan_wq)
367 destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
368
369 if (vmbus_connection.rescind_work_queue)
370 destroy_workqueue(vmbus_connection.rescind_work_queue);
371
372 if (vmbus_connection.work_queue)
373 destroy_workqueue(vmbus_connection.work_queue);
374
375 if (vmbus_connection.int_page) {
376 hv_free_hyperv_page((unsigned long)vmbus_connection.int_page);
377 vmbus_connection.int_page = NULL;
378 }
379
380 if (hv_is_isolation_supported()) {
381 /*
382 * memunmap() checks input address is ioremap address or not
383 * inside. It doesn't unmap any thing in the non-SNP CVM and
384 * so not check CVM type here.
385 */
386 memunmap(vmbus_connection.monitor_pages[0]);
387 memunmap(vmbus_connection.monitor_pages[1]);
388
389 set_memory_encrypted((unsigned long)
390 vmbus_connection.monitor_pages_original[0],
391 1);
392 set_memory_encrypted((unsigned long)
393 vmbus_connection.monitor_pages_original[1],
394 1);
395 }
396
397 hv_free_hyperv_page((unsigned long)
398 vmbus_connection.monitor_pages_original[0]);
399 hv_free_hyperv_page((unsigned long)
400 vmbus_connection.monitor_pages_original[1]);
401 vmbus_connection.monitor_pages_original[0] =
402 vmbus_connection.monitor_pages[0] = NULL;
403 vmbus_connection.monitor_pages_original[1] =
404 vmbus_connection.monitor_pages[1] = NULL;
405 }
406
407 /*
408 * relid2channel - Get the channel object given its
409 * child relative id (ie channel id)
410 */
relid2channel(u32 relid)411 struct vmbus_channel *relid2channel(u32 relid)
412 {
413 if (vmbus_connection.channels == NULL) {
414 pr_warn_once("relid2channel: relid=%d: No channels mapped!\n", relid);
415 return NULL;
416 }
417 if (WARN_ON(relid >= MAX_CHANNEL_RELIDS))
418 return NULL;
419 return READ_ONCE(vmbus_connection.channels[relid]);
420 }
421
422 /*
423 * vmbus_on_event - Process a channel event notification
424 *
425 * For batched channels (default) optimize host to guest signaling
426 * by ensuring:
427 * 1. While reading the channel, we disable interrupts from host.
428 * 2. Ensure that we process all posted messages from the host
429 * before returning from this callback.
430 * 3. Once we return, enable signaling from the host. Once this
431 * state is set we check to see if additional packets are
432 * available to read. In this case we repeat the process.
433 * If this tasklet has been running for a long time
434 * then reschedule ourselves.
435 */
vmbus_on_event(unsigned long data)436 void vmbus_on_event(unsigned long data)
437 {
438 struct vmbus_channel *channel = (void *) data;
439 void (*callback_fn)(void *context);
440
441 trace_vmbus_on_event(channel);
442
443 hv_debug_delay_test(channel, INTERRUPT_DELAY);
444
445 /* A channel once created is persistent even when
446 * there is no driver handling the device. An
447 * unloading driver sets the onchannel_callback to NULL.
448 */
449 callback_fn = READ_ONCE(channel->onchannel_callback);
450 if (unlikely(!callback_fn))
451 return;
452
453 (*callback_fn)(channel->channel_callback_context);
454
455 if (channel->callback_mode != HV_CALL_BATCHED)
456 return;
457
458 if (likely(hv_end_read(&channel->inbound) == 0))
459 return;
460
461 hv_begin_read(&channel->inbound);
462 tasklet_schedule(&channel->callback_event);
463 }
464
465 /*
466 * vmbus_post_msg - Send a msg on the vmbus's message connection
467 */
vmbus_post_msg(void * buffer,size_t buflen,bool can_sleep)468 int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
469 {
470 struct vmbus_channel_message_header *hdr;
471 union hv_connection_id conn_id;
472 int ret = 0;
473 int retries = 0;
474 u32 usec = 1;
475
476 conn_id.asu32 = 0;
477 conn_id.u.id = vmbus_connection.msg_conn_id;
478
479 /*
480 * hv_post_message() can have transient failures because of
481 * insufficient resources. Retry the operation a couple of
482 * times before giving up.
483 */
484 while (retries < 100) {
485 ret = hv_post_message(conn_id, 1, buffer, buflen);
486
487 switch (ret) {
488 case HV_STATUS_INVALID_CONNECTION_ID:
489 /*
490 * See vmbus_negotiate_version(): VMBus protocol 5.0
491 * and higher require that we must use
492 * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
493 * Contact message, but on old hosts that only
494 * support VMBus protocol 4.0 or lower, here we get
495 * HV_STATUS_INVALID_CONNECTION_ID and we should
496 * return an error immediately without retrying.
497 */
498 hdr = buffer;
499 if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
500 return -EINVAL;
501 /*
502 * We could get this if we send messages too
503 * frequently.
504 */
505 ret = -EAGAIN;
506 break;
507 case HV_STATUS_INSUFFICIENT_MEMORY:
508 case HV_STATUS_INSUFFICIENT_BUFFERS:
509 ret = -ENOBUFS;
510 break;
511 case HV_STATUS_SUCCESS:
512 return ret;
513 default:
514 pr_err("hv_post_msg() failed; error code:%d\n", ret);
515 return -EINVAL;
516 }
517
518 retries++;
519 if (can_sleep && usec > 1000)
520 msleep(usec / 1000);
521 else if (usec < MAX_UDELAY_MS * 1000)
522 udelay(usec);
523 else
524 mdelay(usec / 1000);
525
526 if (retries < 22)
527 usec *= 2;
528 }
529 return ret;
530 }
531
532 /*
533 * vmbus_set_event - Send an event notification to the parent
534 */
vmbus_set_event(struct vmbus_channel * channel)535 void vmbus_set_event(struct vmbus_channel *channel)
536 {
537 u32 child_relid = channel->offermsg.child_relid;
538
539 if (!channel->is_dedicated_interrupt)
540 vmbus_send_interrupt(child_relid);
541
542 ++channel->sig_events;
543
544 if (hv_isolation_type_snp())
545 hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, &channel->sig_event,
546 NULL, sizeof(channel->sig_event));
547 else
548 hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
549 }
550 EXPORT_SYMBOL_GPL(vmbus_set_event);
551