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