blob: a3d8be8d6cfb07af67e0833ab26ce1267bd0ba29 [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Hank Janssen3e7ee492009-07-13 16:02:34 -07002/*
3 *
4 * Copyright (c) 2009, Microsoft Corporation.
5 *
Hank Janssen3e7ee492009-07-13 16:02:34 -07006 * Authors:
7 * Haiyang Zhang <haiyangz@microsoft.com>
8 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -07009 */
Hank Janssen0a466182011-03-29 13:58:47 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070012#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080013#include <linux/sched.h>
14#include <linux/wait.h>
K. Y. Srinivasan5289d3d12011-08-25 09:49:01 -070015#include <linux/delay.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070016#include <linux/mm.h>
Andrea Parri931cccc2019-10-15 13:46:46 +020017#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070019#include <linux/vmalloc.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070020#include <linux/hyperv.h>
K. Y. Srinivasan37f72782012-12-01 06:46:41 -080021#include <linux/export.h>
Tianyu Lanf2f136c2021-10-25 08:21:13 -040022#include <linux/io.h>
23#include <linux/set_memory.h>
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020024#include <asm/mshyperv.h>
25
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070026#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070027
Hank Janssen3e7ee492009-07-13 16:02:34 -070028
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080029struct vmbus_connection vmbus_connection = {
30 .conn_state = DISCONNECTED,
Andrea Parri (Microsoft)8c2d5e02021-04-20 03:43:50 +020031 .unload_event = COMPLETION_INITIALIZER(
32 vmbus_connection.unload_event),
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080033 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
Dexuan Cuib307b382019-09-05 23:01:21 +000034
Vasanthf850a4c2021-02-19 22:43:11 +053035 .ready_for_suspend_event = COMPLETION_INITIALIZER(
Dexuan Cuib307b382019-09-05 23:01:21 +000036 vmbus_connection.ready_for_suspend_event),
Dexuan Cuid8bd2d42019-09-05 23:01:22 +000037 .ready_for_resume_event = COMPLETION_INITIALIZER(
38 vmbus_connection.ready_for_resume_event),
Hank Janssen3e7ee492009-07-13 16:02:34 -070039};
Stephen Hemminger95096f22016-12-03 12:34:40 -080040EXPORT_SYMBOL_GPL(vmbus_connection);
Hank Janssen3e7ee492009-07-13 16:02:34 -070041
Hank Janssen3e189512010-03-04 22:11:00 +000042/*
K. Y. Srinivasan37f72782012-12-01 06:46:41 -080043 * Negotiated protocol version with the host.
44 */
45__u32 vmbus_proto_version;
46EXPORT_SYMBOL_GPL(vmbus_proto_version);
47
Andrea Parribedc61a2019-10-15 13:46:44 +020048/*
49 * Table of VMBus versions listed from newest to oldest.
50 */
51static __u32 vmbus_versions[] = {
Andrea Parri (Microsoft)1df53d22021-04-16 16:34:47 +020052 VERSION_WIN10_V5_3,
Andrea Parri2d4f49b2019-10-15 13:46:45 +020053 VERSION_WIN10_V5_2,
54 VERSION_WIN10_V5_1,
Andrea Parribedc61a2019-10-15 13:46:44 +020055 VERSION_WIN10_V5,
Andrea Parri2d4f49b2019-10-15 13:46:45 +020056 VERSION_WIN10_V4_1,
Andrea Parribedc61a2019-10-15 13:46:44 +020057 VERSION_WIN10,
58 VERSION_WIN8_1,
59 VERSION_WIN8,
60 VERSION_WIN7,
61 VERSION_WS2008
62};
K. Y. Srinivasan610071c2012-12-01 06:46:38 -080063
Andrea Parri931cccc2019-10-15 13:46:46 +020064/*
65 * Maximal VMBus protocol version guests can negotiate. Useful to cap the
66 * VMBus version for testing and debugging purpose.
67 */
Andrea Parri (Microsoft)1df53d22021-04-16 16:34:47 +020068static uint max_version = VERSION_WIN10_V5_3;
Andrea Parri931cccc2019-10-15 13:46:46 +020069
70module_param(max_version, uint, S_IRUGO);
71MODULE_PARM_DESC(max_version,
72 "Maximal VMBus protocol version which can be negotiated");
73
Dexuan Cuif53335e2019-09-05 23:01:19 +000074int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
K. Y. Srinivasan610071c2012-12-01 06:46:38 -080075{
76 int ret = 0;
77 struct vmbus_channel_initiate_contact *msg;
78 unsigned long flags;
K. Y. Srinivasan610071c2012-12-01 06:46:38 -080079
80 init_completion(&msginfo->waitevent);
81
82 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
83
Dexuan Cuiae20b252018-05-12 02:30:33 -070084 memset(msg, 0, sizeof(*msg));
K. Y. Srinivasan610071c2012-12-01 06:46:38 -080085 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
86 msg->vmbus_version_requested = version;
Dexuan Cuiae20b252018-05-12 02:30:33 -070087
88 /*
Andrea Parri2d4f49b2019-10-15 13:46:45 +020089 * VMBus protocol 5.0 (VERSION_WIN10_V5) and higher require that we must
90 * use VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
Dexuan Cuiae20b252018-05-12 02:30:33 -070091 * and for subsequent messages, we must use the Message Connection ID
92 * field in the host-returned Version Response Message. And, with
Andrea Parri2d4f49b2019-10-15 13:46:45 +020093 * VERSION_WIN10_V5 and higher, we don't use msg->interrupt_page, but we
94 * tell the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
Dexuan Cuiae20b252018-05-12 02:30:33 -070095 * compatibility.
96 *
97 * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
98 */
99 if (version >= VERSION_WIN10_V5) {
100 msg->msg_sint = VMBUS_MESSAGE_SINT;
101 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
102 } else {
103 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
104 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
105 }
106
Tianyu Lanf2f136c2021-10-25 08:21:13 -0400107 msg->monitor_page1 = vmbus_connection.monitor_pages_pa[0];
108 msg->monitor_page2 = vmbus_connection.monitor_pages_pa[1];
109
Andrea Parri (Microsoft)8a857c52020-04-06 02:15:04 +0200110 msg->target_vcpu = hv_cpu_number_to_vp_number(VMBUS_CONNECT_CPU);
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800111
112 /*
113 * Add to list before we send the request since we may
114 * receive the response before returning from this routine
115 */
116 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
117 list_add_tail(&msginfo->msglistentry,
118 &vmbus_connection.chn_msg_list);
119
120 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
121
122 ret = vmbus_post_msg(msg,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800123 sizeof(struct vmbus_channel_initiate_contact),
124 true);
Vitaly Kuznetsov034ebf52017-10-29 12:21:13 -0700125
126 trace_vmbus_negotiate_version(msg, ret);
127
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800128 if (ret != 0) {
129 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
130 list_del(&msginfo->msglistentry);
131 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
132 flags);
133 return ret;
134 }
135
136 /* Wait for the connection response */
K. Y. Srinivasan269f97942014-01-16 11:59:58 -0800137 wait_for_completion(&msginfo->waitevent);
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800138
139 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
140 list_del(&msginfo->msglistentry);
141 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
142
143 /* Check if successful */
144 if (msginfo->response.version_response.version_supported) {
145 vmbus_connection.conn_state = CONNECTED;
Dexuan Cuiae20b252018-05-12 02:30:33 -0700146
147 if (version >= VERSION_WIN10_V5)
148 vmbus_connection.msg_conn_id =
149 msginfo->response.version_response.msg_conn_id;
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800150 } else {
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800151 return -ECONNREFUSED;
152 }
153
154 return ret;
155}
156
157/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800158 * vmbus_connect - Sends a connect request on the partition service connection
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700159 */
Haiyang Zhangc6977672011-01-26 12:12:08 -0800160int vmbus_connect(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700161{
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800162 struct vmbus_channel_msginfo *msginfo = NULL;
Andrea Parribedc61a2019-10-15 13:46:44 +0200163 int i, ret = 0;
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800164 __u32 version;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700165
Bill Pemberton454f18a2009-07-27 16:47:24 -0400166 /* Initialize the vmbus connection */
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800167 vmbus_connection.conn_state = CONNECTING;
168 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
169 if (!vmbus_connection.work_queue) {
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -0700170 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700171 goto cleanup;
Bill Pembertonde65a382009-07-29 17:00:09 -0400172 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700173
Dexuan Cui37c25782018-12-03 00:54:35 +0000174 vmbus_connection.handle_primary_chan_wq =
175 create_workqueue("hv_pri_chan");
176 if (!vmbus_connection.handle_primary_chan_wq) {
177 ret = -ENOMEM;
178 goto cleanup;
179 }
180
181 vmbus_connection.handle_sub_chan_wq =
182 create_workqueue("hv_sub_chan");
183 if (!vmbus_connection.handle_sub_chan_wq) {
184 ret = -ENOMEM;
185 goto cleanup;
186 }
187
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800188 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800189 spin_lock_init(&vmbus_connection.channelmsg_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700190
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800191 INIT_LIST_HEAD(&vmbus_connection.chn_list);
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800192 mutex_init(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700193
Bill Pemberton454f18a2009-07-27 16:47:24 -0400194 /*
195 * Setup the vmbus event connection for channel interrupt
196 * abstraction stuff
197 */
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800198 vmbus_connection.int_page =
Himadri Pandya53edce02019-07-30 09:49:44 +0000199 (void *)hv_alloc_hyperv_zeroed_page();
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800200 if (vmbus_connection.int_page == NULL) {
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -0700201 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700202 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700203 }
204
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800205 vmbus_connection.recv_int_page = vmbus_connection.int_page;
206 vmbus_connection.send_int_page =
207 (void *)((unsigned long)vmbus_connection.int_page +
Himadri Pandya53edce02019-07-30 09:49:44 +0000208 (HV_HYP_PAGE_SIZE >> 1));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700210 /*
211 * Setup the monitor notification facility. The 1st page for
212 * parent->child and the 2nd page for child->parent
Bill Pemberton454f18a2009-07-27 16:47:24 -0400213 */
Himadri Pandya53edce02019-07-30 09:49:44 +0000214 vmbus_connection.monitor_pages[0] = (void *)hv_alloc_hyperv_zeroed_page();
215 vmbus_connection.monitor_pages[1] = (void *)hv_alloc_hyperv_zeroed_page();
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -0700216 if ((vmbus_connection.monitor_pages[0] == NULL) ||
217 (vmbus_connection.monitor_pages[1] == NULL)) {
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -0700218 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700219 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700220 }
221
Tianyu Lanf2f136c2021-10-25 08:21:13 -0400222 vmbus_connection.monitor_pages_original[0]
223 = vmbus_connection.monitor_pages[0];
224 vmbus_connection.monitor_pages_original[1]
225 = vmbus_connection.monitor_pages[1];
226 vmbus_connection.monitor_pages_pa[0]
227 = virt_to_phys(vmbus_connection.monitor_pages[0]);
228 vmbus_connection.monitor_pages_pa[1]
229 = virt_to_phys(vmbus_connection.monitor_pages[1]);
230
231 if (hv_is_isolation_supported()) {
232 ret = set_memory_decrypted((unsigned long)
233 vmbus_connection.monitor_pages[0],
234 1);
235 ret |= set_memory_decrypted((unsigned long)
236 vmbus_connection.monitor_pages[1],
237 1);
238 if (ret)
239 goto cleanup;
240
241 /*
242 * Isolation VM with AMD SNP needs to access monitor page via
243 * address space above shared gpa boundary.
244 */
245 if (hv_isolation_type_snp()) {
246 vmbus_connection.monitor_pages_pa[0] +=
247 ms_hyperv.shared_gpa_boundary;
248 vmbus_connection.monitor_pages_pa[1] +=
249 ms_hyperv.shared_gpa_boundary;
250
251 vmbus_connection.monitor_pages[0]
252 = memremap(vmbus_connection.monitor_pages_pa[0],
253 HV_HYP_PAGE_SIZE,
254 MEMREMAP_WB);
255 if (!vmbus_connection.monitor_pages[0]) {
256 ret = -ENOMEM;
257 goto cleanup;
258 }
259
260 vmbus_connection.monitor_pages[1]
261 = memremap(vmbus_connection.monitor_pages_pa[1],
262 HV_HYP_PAGE_SIZE,
263 MEMREMAP_WB);
264 if (!vmbus_connection.monitor_pages[1]) {
265 ret = -ENOMEM;
266 goto cleanup;
267 }
268 }
269
270 /*
271 * Set memory host visibility hvcall smears memory
272 * and so zero monitor pages here.
273 */
274 memset(vmbus_connection.monitor_pages[0], 0x00,
275 HV_HYP_PAGE_SIZE);
276 memset(vmbus_connection.monitor_pages[1], 0x00,
277 HV_HYP_PAGE_SIZE);
278
279 }
280
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800281 msginfo = kzalloc(sizeof(*msginfo) +
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700282 sizeof(struct vmbus_channel_initiate_contact),
283 GFP_KERNEL);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800284 if (msginfo == NULL) {
Bill Pemberton8cad0af2010-05-05 15:27:32 -0400285 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700286 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700287 }
288
Bill Pemberton454f18a2009-07-27 16:47:24 -0400289 /*
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800290 * Negotiate a compatible VMBUS version number with the
291 * host. We start with the highest number we can support
292 * and work our way down until we negotiate a compatible
293 * version.
Bill Pemberton454f18a2009-07-27 16:47:24 -0400294 */
Bill Pemberton53af5452009-09-11 21:46:44 -0400295
Andrea Parribedc61a2019-10-15 13:46:44 +0200296 for (i = 0; ; i++) {
Jiapeng Chong9de66552021-05-25 18:58:41 +0800297 if (i == ARRAY_SIZE(vmbus_versions)) {
298 ret = -EDOM;
Andrea Parribedc61a2019-10-15 13:46:44 +0200299 goto cleanup;
Jiapeng Chong9de66552021-05-25 18:58:41 +0800300 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700301
Andrea Parribedc61a2019-10-15 13:46:44 +0200302 version = vmbus_versions[i];
Andrea Parri931cccc2019-10-15 13:46:46 +0200303 if (version > max_version)
304 continue;
Andrea Parribedc61a2019-10-15 13:46:44 +0200305
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800306 ret = vmbus_negotiate_version(msginfo, version);
K. Y. Srinivasan8bbf9f442013-09-04 15:41:58 -0700307 if (ret == -ETIMEDOUT)
K. Y. Srinivasan666b9ad2013-08-28 14:56:54 -0700308 goto cleanup;
309
310 if (vmbus_connection.conn_state == CONNECTED)
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800311 break;
Andrea Parribedc61a2019-10-15 13:46:44 +0200312 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700313
Andrea Parri (Microsoft)7ef4b2f2021-02-01 15:48:13 +0100314 if (hv_is_isolation_supported() && version < VERSION_WIN10_V5_2) {
315 pr_err("Invalid VMBus version %d.%d (expected >= %d.%d) from the host supporting isolation\n",
316 version >> 16, version & 0xFFFF, VERSION_WIN10_V5_2 >> 16, VERSION_WIN10_V5_2 & 0xFFFF);
317 ret = -EINVAL;
318 goto cleanup;
319 }
320
K. Y. Srinivasan37f72782012-12-01 06:46:41 -0800321 vmbus_proto_version = version;
K. Y. Srinivasan8de8af72017-01-19 11:51:47 -0700322 pr_info("Vmbus version:%d.%d\n",
323 version >> 16, version & 0xFFFF);
K. Y. Srinivasan3bacaf0c2012-12-01 06:46:59 -0800324
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +0200325 vmbus_connection.channels = kcalloc(MAX_CHANNEL_RELIDS,
326 sizeof(struct vmbus_channel *),
327 GFP_KERNEL);
328 if (vmbus_connection.channels == NULL) {
329 ret = -ENOMEM;
330 goto cleanup;
331 }
332
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800333 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700334 return 0;
335
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700336cleanup:
K. Y. Srinivasan3bacaf0c2012-12-01 06:46:59 -0800337 pr_err("Unable to connect to host\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700338
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -0800339 vmbus_connection.conn_state = DISCONNECTED;
340 vmbus_disconnect();
341
342 kfree(msginfo);
343
344 return ret;
345}
346
347void vmbus_disconnect(void)
348{
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700349 /*
350 * First send the unload request to the host.
351 */
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -0800352 vmbus_initiate_unload(false);
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700353
Dexuan Cui37c25782018-12-03 00:54:35 +0000354 if (vmbus_connection.handle_sub_chan_wq)
355 destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
356
357 if (vmbus_connection.handle_primary_chan_wq)
358 destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
359
360 if (vmbus_connection.work_queue)
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800361 destroy_workqueue(vmbus_connection.work_queue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700362
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800363 if (vmbus_connection.int_page) {
Himadri Pandya53edce02019-07-30 09:49:44 +0000364 hv_free_hyperv_page((unsigned long)vmbus_connection.int_page);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800365 vmbus_connection.int_page = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700366 }
367
Tianyu Lanf2f136c2021-10-25 08:21:13 -0400368 if (hv_is_isolation_supported()) {
369 /*
370 * memunmap() checks input address is ioremap address or not
371 * inside. It doesn't unmap any thing in the non-SNP CVM and
372 * so not check CVM type here.
373 */
374 memunmap(vmbus_connection.monitor_pages[0]);
375 memunmap(vmbus_connection.monitor_pages[1]);
376
377 set_memory_encrypted((unsigned long)
378 vmbus_connection.monitor_pages_original[0],
379 1);
380 set_memory_encrypted((unsigned long)
381 vmbus_connection.monitor_pages_original[1],
382 1);
383 }
384
385 hv_free_hyperv_page((unsigned long)
386 vmbus_connection.monitor_pages_original[0]);
387 hv_free_hyperv_page((unsigned long)
388 vmbus_connection.monitor_pages_original[1]);
389 vmbus_connection.monitor_pages_original[0] =
390 vmbus_connection.monitor_pages[0] = NULL;
391 vmbus_connection.monitor_pages_original[1] =
392 vmbus_connection.monitor_pages[1] = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700393}
394
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700395/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800396 * relid2channel - Get the channel object given its
397 * child relative id (ie channel id)
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700398 */
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700399struct vmbus_channel *relid2channel(u32 relid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700400{
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +0200401 if (WARN_ON(relid >= MAX_CHANNEL_RELIDS))
402 return NULL;
403 return READ_ONCE(vmbus_connection.channels[relid]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404}
405
Hank Janssen3e189512010-03-04 22:11:00 +0000406/*
Stephen Hemminger631e63a2017-02-11 23:02:20 -0700407 * vmbus_on_event - Process a channel event notification
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700408 *
409 * For batched channels (default) optimize host to guest signaling
410 * by ensuring:
411 * 1. While reading the channel, we disable interrupts from host.
412 * 2. Ensure that we process all posted messages from the host
413 * before returning from this callback.
414 * 3. Once we return, enable signaling from the host. Once this
415 * state is set we check to see if additional packets are
416 * available to read. In this case we repeat the process.
417 * If this tasklet has been running for a long time
418 * then reschedule ourselves.
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700419 */
Stephen Hemminger631e63a2017-02-11 23:02:20 -0700420void vmbus_on_event(unsigned long data)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700421{
Stephen Hemminger631e63a2017-02-11 23:02:20 -0700422 struct vmbus_channel *channel = (void *) data;
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700423 unsigned long time_limit = jiffies + 2;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424
Vitaly Kuznetsov991f8f12017-10-29 12:21:16 -0700425 trace_vmbus_on_event(channel);
426
Branden Bonabyaf9ca6f2019-10-03 17:01:49 -0400427 hv_debug_delay_test(channel, INTERRUPT_DELAY);
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700428 do {
429 void (*callback_fn)(void *);
Stephen Hemmingerb71e3282017-02-11 23:02:21 -0700430
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700431 /* A channel once created is persistent even when
432 * there is no driver handling the device. An
433 * unloading driver sets the onchannel_callback to NULL.
K. Y. Srinivasanf878f3d2012-12-01 06:46:35 -0800434 */
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700435 callback_fn = READ_ONCE(channel->onchannel_callback);
436 if (unlikely(callback_fn == NULL))
437 return;
K. Y. Srinivasanf878f3d2012-12-01 06:46:35 -0800438
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700439 (*callback_fn)(channel->channel_callback_context);
440
441 if (channel->callback_mode != HV_CALL_BATCHED)
442 return;
443
444 if (likely(hv_end_read(&channel->inbound) == 0))
445 return;
446
447 hv_begin_read(&channel->inbound);
448 } while (likely(time_before(jiffies, time_limit)));
449
450 /* The time limit (2 jiffies) has been reached */
451 tasklet_schedule(&channel->callback_event);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700452}
453
Hank Janssen3e189512010-03-04 22:11:00 +0000454/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800455 * vmbus_post_msg - Send a msg on the vmbus's message connection
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700456 */
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800457int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700458{
Dexuan Cuiae20b252018-05-12 02:30:33 -0700459 struct vmbus_channel_message_header *hdr;
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800460 union hv_connection_id conn_id;
K. Y. Srinivasan5289d3d12011-08-25 09:49:01 -0700461 int ret = 0;
462 int retries = 0;
K. Y. Srinivasan8de0d7e2016-07-01 16:26:36 -0700463 u32 usec = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700464
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800465 conn_id.asu32 = 0;
Dexuan Cuiae20b252018-05-12 02:30:33 -0700466 conn_id.u.id = vmbus_connection.msg_conn_id;
K. Y. Srinivasan5289d3d12011-08-25 09:49:01 -0700467
468 /*
469 * hv_post_message() can have transient failures because of
470 * insufficient resources. Retry the operation a couple of
471 * times before giving up.
472 */
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800473 while (retries < 100) {
K. Y. Srinivasanfdeebcc2014-08-27 16:25:31 -0700474 ret = hv_post_message(conn_id, 1, buffer, buflen);
475
476 switch (ret) {
Dexuan Cui89f9f672015-02-27 11:25:59 -0800477 case HV_STATUS_INVALID_CONNECTION_ID:
478 /*
Dexuan Cuiae20b252018-05-12 02:30:33 -0700479 * See vmbus_negotiate_version(): VMBus protocol 5.0
Andrea Parri2d4f49b2019-10-15 13:46:45 +0200480 * and higher require that we must use
Dexuan Cuiae20b252018-05-12 02:30:33 -0700481 * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
482 * Contact message, but on old hosts that only
483 * support VMBus protocol 4.0 or lower, here we get
484 * HV_STATUS_INVALID_CONNECTION_ID and we should
485 * return an error immediately without retrying.
486 */
Dexuan Cui89760932018-05-15 00:25:01 +0000487 hdr = buffer;
Dexuan Cuiae20b252018-05-12 02:30:33 -0700488 if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
489 return -EINVAL;
490 /*
Dexuan Cui89f9f672015-02-27 11:25:59 -0800491 * We could get this if we send messages too
492 * frequently.
493 */
494 ret = -EAGAIN;
495 break;
496 case HV_STATUS_INSUFFICIENT_MEMORY:
K. Y. Srinivasanfdeebcc2014-08-27 16:25:31 -0700497 case HV_STATUS_INSUFFICIENT_BUFFERS:
K. Y. Srinivasan48f4ccd2017-04-30 16:21:16 -0700498 ret = -ENOBUFS;
K. Y. Srinivasanfdeebcc2014-08-27 16:25:31 -0700499 break;
500 case HV_STATUS_SUCCESS:
K. Y. Srinivasan5289d3d12011-08-25 09:49:01 -0700501 return ret;
K. Y. Srinivasanfdeebcc2014-08-27 16:25:31 -0700502 default:
503 pr_err("hv_post_msg() failed; error code:%d\n", ret);
504 return -EINVAL;
505 }
506
K. Y. Srinivasan5289d3d12011-08-25 09:49:01 -0700507 retries++;
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800508 if (can_sleep && usec > 1000)
509 msleep(usec / 1000);
510 else if (usec < MAX_UDELAY_MS * 1000)
511 udelay(usec);
512 else
513 mdelay(usec / 1000);
514
K. Y. Srinivasane917a5e2017-05-18 10:46:05 -0700515 if (retries < 22)
K. Y. Srinivasan8de0d7e2016-07-01 16:26:36 -0700516 usec *= 2;
K. Y. Srinivasan5289d3d12011-08-25 09:49:01 -0700517 }
518 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700519}
520
Hank Janssen3e189512010-03-04 22:11:00 +0000521/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800522 * vmbus_set_event - Send an event notification to the parent
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700523 */
K. Y. Srinivasan1b807e12015-12-21 15:12:20 -0800524void vmbus_set_event(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700525{
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -0800526 u32 child_relid = channel->offermsg.child_relid;
Bill Pemberton7c369f42009-07-29 17:00:11 -0400527
Stephen Hemminger5c1bec62017-02-05 17:20:31 -0700528 if (!channel->is_dedicated_interrupt)
529 vmbus_send_interrupt(child_relid);
K. Y. Srinivasan3be77772012-12-01 06:46:46 -0800530
Stephen Hemminger6981fbf2017-10-29 11:33:40 -0700531 ++channel->sig_events;
532
Tianyu Lan20c89a52021-10-25 08:21:12 -0400533 if (hv_isolation_type_snp())
534 hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, &channel->sig_event,
535 NULL, sizeof(channel->sig_event));
536 else
537 hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700538}
K. Y. Srinivasan5cc47242016-04-02 17:59:49 -0700539EXPORT_SYMBOL_GPL(vmbus_set_event);