2 * Copyright (c) 2009-2012 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/param.h>
30 #include <sys/malloc.h>
31 #include <sys/systm.h>
33 #include <sys/mutex.h>
34 #include <machine/bus.h>
36 #include <vm/vm_param.h>
39 #include "hv_vmbus_priv.h"
44 hv_vmbus_connection hv_vmbus_g_connection =
45 { .connect_state = HV_DISCONNECTED,
46 .next_gpadl_handle = 0xE1E10, };
48 uint32_t hv_vmbus_protocal_version = HV_VMBUS_VERSION_WS2008;
51 hv_vmbus_get_next_version(uint32_t current_ver)
53 switch (current_ver) {
54 case (HV_VMBUS_VERSION_WIN7):
55 return(HV_VMBUS_VERSION_WS2008);
57 case (HV_VMBUS_VERSION_WIN8):
58 return(HV_VMBUS_VERSION_WIN7);
60 case (HV_VMBUS_VERSION_WIN8_1):
61 return(HV_VMBUS_VERSION_WIN8);
63 case (HV_VMBUS_VERSION_WS2008):
65 return(HV_VMBUS_VERSION_INVALID);
70 * Negotiate the highest supported hypervisor version.
73 hv_vmbus_negotiate_version(hv_vmbus_channel_msg_info *msg_info,
77 hv_vmbus_channel_initiate_contact *msg;
79 sema_init(&msg_info->wait_sema, 0, "Msg Info Sema");
80 msg = (hv_vmbus_channel_initiate_contact*) msg_info->msg;
82 msg->header.message_type = HV_CHANNEL_MESSAGE_INITIATED_CONTACT;
83 msg->vmbus_version_requested = version;
85 msg->interrupt_page = hv_get_phys_addr(
86 hv_vmbus_g_connection.interrupt_page);
88 msg->monitor_page_1 = hv_get_phys_addr(
89 hv_vmbus_g_connection.monitor_pages);
93 ((uint8_t *) hv_vmbus_g_connection.monitor_pages
97 * Add to list before we send the request since we may receive the
98 * response before returning from this routine
100 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
103 &hv_vmbus_g_connection.channel_msg_anchor,
107 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
109 ret = hv_vmbus_post_message(
111 sizeof(hv_vmbus_channel_initiate_contact));
114 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
116 &hv_vmbus_g_connection.channel_msg_anchor,
119 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
124 * Wait for the connection response
126 ret = sema_timedwait(&msg_info->wait_sema, 500); /* KYS 5 seconds */
128 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
130 &hv_vmbus_g_connection.channel_msg_anchor,
133 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
136 * Check if successful
138 if (msg_info->response.version_response.version_supported) {
139 hv_vmbus_g_connection.connect_state = HV_CONNECTED;
148 * Send a connect request on the partition service connection
151 hv_vmbus_connect(void) {
154 hv_vmbus_channel_msg_info* msg_info = NULL;
157 * Make sure we are not connecting or connected
159 if (hv_vmbus_g_connection.connect_state != HV_DISCONNECTED) {
164 * Initialize the vmbus connection
166 hv_vmbus_g_connection.connect_state = HV_CONNECTING;
167 hv_vmbus_g_connection.work_queue = hv_work_queue_create("vmbusQ");
168 sema_init(&hv_vmbus_g_connection.control_sema, 1, "control_sema");
170 TAILQ_INIT(&hv_vmbus_g_connection.channel_msg_anchor);
171 mtx_init(&hv_vmbus_g_connection.channel_msg_lock, "vmbus channel msg",
174 TAILQ_INIT(&hv_vmbus_g_connection.channel_anchor);
175 mtx_init(&hv_vmbus_g_connection.channel_lock, "vmbus channel",
179 * Setup the vmbus event connection for channel interrupt abstraction
182 hv_vmbus_g_connection.interrupt_page = contigmalloc(
184 M_NOWAIT | M_ZERO, 0UL,
187 KASSERT(hv_vmbus_g_connection.interrupt_page != NULL,
188 ("Error VMBUS: malloc failed to allocate Channel"
189 " Request Event message!"));
190 if (hv_vmbus_g_connection.interrupt_page == NULL) {
195 hv_vmbus_g_connection.recv_interrupt_page =
196 hv_vmbus_g_connection.interrupt_page;
198 hv_vmbus_g_connection.send_interrupt_page =
199 ((uint8_t *) hv_vmbus_g_connection.interrupt_page +
203 * Set up the monitor notification facility. The 1st page for
204 * parent->child and the 2nd page for child->parent
206 hv_vmbus_g_connection.monitor_pages = contigmalloc(
214 KASSERT(hv_vmbus_g_connection.monitor_pages != NULL,
215 ("Error VMBUS: malloc failed to allocate Monitor Pages!"));
216 if (hv_vmbus_g_connection.monitor_pages == NULL) {
221 msg_info = (hv_vmbus_channel_msg_info*)
222 malloc(sizeof(hv_vmbus_channel_msg_info) +
223 sizeof(hv_vmbus_channel_initiate_contact),
224 M_DEVBUF, M_NOWAIT | M_ZERO);
225 KASSERT(msg_info != NULL,
226 ("Error VMBUS: malloc failed for Initiate Contact message!"));
227 if (msg_info == NULL) {
233 * Find the highest vmbus version number we can support.
235 version = HV_VMBUS_VERSION_CURRENT;
238 ret = hv_vmbus_negotiate_version(msg_info, version);
239 if (ret == EWOULDBLOCK) {
246 if (hv_vmbus_g_connection.connect_state == HV_CONNECTED)
249 version = hv_vmbus_get_next_version(version);
250 } while (version != HV_VMBUS_VERSION_INVALID);
252 hv_vmbus_protocal_version = version;
254 printf("VMBUS: Protocol Version: %d.%d\n",
255 version >> 16, version & 0xFFFF);
257 sema_destroy(&msg_info->wait_sema);
258 free(msg_info, M_DEVBUF);
263 * Cleanup after failure!
267 hv_vmbus_g_connection.connect_state = HV_DISCONNECTED;
269 hv_work_queue_close(hv_vmbus_g_connection.work_queue);
270 sema_destroy(&hv_vmbus_g_connection.control_sema);
271 mtx_destroy(&hv_vmbus_g_connection.channel_lock);
272 mtx_destroy(&hv_vmbus_g_connection.channel_msg_lock);
274 if (hv_vmbus_g_connection.interrupt_page != NULL) {
276 hv_vmbus_g_connection.interrupt_page,
279 hv_vmbus_g_connection.interrupt_page = NULL;
282 if (hv_vmbus_g_connection.monitor_pages != NULL) {
284 hv_vmbus_g_connection.monitor_pages,
287 hv_vmbus_g_connection.monitor_pages = NULL;
291 sema_destroy(&msg_info->wait_sema);
292 free(msg_info, M_DEVBUF);
299 * Send a disconnect request on the partition service connection
302 hv_vmbus_disconnect(void) {
304 hv_vmbus_channel_unload* msg;
306 msg = malloc(sizeof(hv_vmbus_channel_unload),
307 M_DEVBUF, M_NOWAIT | M_ZERO);
309 ("Error VMBUS: malloc failed to allocate Channel Unload Msg!"));
313 msg->message_type = HV_CHANNEL_MESSAGE_UNLOAD;
315 ret = hv_vmbus_post_message(msg, sizeof(hv_vmbus_channel_unload));
318 contigfree(hv_vmbus_g_connection.interrupt_page, PAGE_SIZE, M_DEVBUF);
320 mtx_destroy(&hv_vmbus_g_connection.channel_msg_lock);
322 hv_work_queue_close(hv_vmbus_g_connection.work_queue);
323 sema_destroy(&hv_vmbus_g_connection.control_sema);
325 hv_vmbus_g_connection.connect_state = HV_DISCONNECTED;
333 * Get the channel object given its child relative id (ie channel id)
336 hv_vmbus_get_channel_from_rel_id(uint32_t rel_id) {
338 hv_vmbus_channel* channel;
339 hv_vmbus_channel* foundChannel = NULL;
343 * Consider optimization where relids are stored in a fixed size array
344 * and channels are accessed without the need to take this lock or search
347 mtx_lock(&hv_vmbus_g_connection.channel_lock);
348 TAILQ_FOREACH(channel,
349 &hv_vmbus_g_connection.channel_anchor, list_entry) {
351 if (channel->offer_msg.child_rel_id == rel_id) {
352 foundChannel = channel;
356 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
358 return (foundChannel);
362 * Process a channel event notification
365 VmbusProcessChannelEvent(uint32_t relid)
368 uint32_t bytes_to_read;
369 hv_vmbus_channel* channel;
370 boolean_t is_batched_reading;
373 * Find the channel based on this relid and invokes
374 * the channel callback to process the event
377 channel = hv_vmbus_get_channel_from_rel_id(relid);
379 if (channel == NULL) {
383 * To deal with the race condition where we might
384 * receive a packet while the relevant driver is
385 * being unloaded, dispatch the callback while
386 * holding the channel lock. The unloading driver
387 * will acquire the same channel lock to set the
388 * callback to NULL. This closes the window.
392 * Disable the lock due to newly added WITNESS check in r277723.
393 * Will seek other way to avoid race condition.
396 // mtx_lock(&channel->inbound_lock);
397 if (channel->on_channel_callback != NULL) {
398 arg = channel->channel_callback_context;
399 is_batched_reading = channel->batched_reading;
401 * Optimize host to guest signaling by ensuring:
402 * 1. While reading the channel, we disable interrupts from
404 * 2. Ensure that we process all posted messages from the host
405 * before returning from this callback.
406 * 3. Once we return, enable signaling from the host. Once this
407 * state is set we check to see if additional packets are
408 * available to read. In this case we repeat the process.
411 if (is_batched_reading)
412 hv_ring_buffer_read_begin(&channel->inbound);
414 channel->on_channel_callback(arg);
416 if (is_batched_reading)
418 hv_ring_buffer_read_end(&channel->inbound);
421 } while (is_batched_reading && (bytes_to_read != 0));
423 // mtx_unlock(&channel->inbound_lock);
430 hv_vmbus_on_events(void *arg)
436 uint32_t* recv_interrupt_page = NULL;
439 hv_vmbus_synic_event_flags *event;
440 /* int maxdword = PAGE_SIZE >> 3; */
442 cpu = (int)(long)arg;
443 KASSERT(cpu <= mp_maxid, ("VMBUS: hv_vmbus_on_events: "
444 "cpu out of range!"));
446 if ((hv_vmbus_protocal_version == HV_VMBUS_VERSION_WS2008) ||
447 (hv_vmbus_protocal_version == HV_VMBUS_VERSION_WIN7)) {
448 maxdword = HV_MAX_NUM_CHANNELS_SUPPORTED >> 5;
450 * receive size is 1/2 page and divide that by 4 bytes
452 recv_interrupt_page =
453 hv_vmbus_g_connection.recv_interrupt_page;
456 * On Host with Win8 or above, the event page can be
457 * checked directly to get the id of the channel
458 * that has the pending interrupt.
460 maxdword = HV_EVENT_FLAGS_DWORD_COUNT;
461 page_addr = hv_vmbus_g_context.syn_ic_event_page[cpu];
462 event = (hv_vmbus_synic_event_flags *)
463 page_addr + HV_VMBUS_MESSAGE_SINT;
464 recv_interrupt_page = event->flags32;
470 if (recv_interrupt_page != NULL) {
471 for (dword = 0; dword < maxdword; dword++) {
472 if (recv_interrupt_page[dword]) {
473 for (bit = 0; bit < 32; bit++) {
474 if (synch_test_and_clear_bit(bit,
475 (uint32_t *) &recv_interrupt_page[dword])) {
476 rel_id = (dword << 5) + bit;
480 * vmbus channel protocol msg.
484 VmbusProcessChannelEvent(rel_id);
497 * Send a msg on the vmbus's message connection
499 int hv_vmbus_post_message(void *buffer, size_t bufferLen) {
501 hv_vmbus_connection_id connId;
502 unsigned retries = 0;
504 /* NetScaler delays from previous code were consolidated here */
505 static int delayAmount[] = {100, 100, 100, 500, 500, 5000, 5000, 5000};
507 /* for(each entry in delayAmount) try to post message,
508 * delay a little bit before retrying
511 retries < sizeof(delayAmount)/sizeof(delayAmount[0]); retries++) {
512 connId.as_uint32_t = 0;
513 connId.u.id = HV_VMBUS_MESSAGE_CONNECTION_ID;
514 ret = hv_vmbus_post_msg_via_msg_ipc(connId, 1, buffer, bufferLen);
515 if (ret != HV_STATUS_INSUFFICIENT_BUFFERS)
517 /* TODO: KYS We should use a blocking wait call */
518 DELAY(delayAmount[retries]);
521 KASSERT(ret == 0, ("Error VMBUS: Message Post Failed\n"));
527 * Send an event notification to the parent
530 hv_vmbus_set_event(hv_vmbus_channel *channel) {
532 uint32_t child_rel_id = channel->offer_msg.child_rel_id;
534 /* Each uint32_t represents 32 channels */
536 synch_set_bit(child_rel_id & 31,
537 (((uint32_t *)hv_vmbus_g_connection.send_interrupt_page
538 + (child_rel_id >> 5))));
539 ret = hv_vmbus_signal_event(channel->signal_event_param);