3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/wait.h>
28 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
32 #include <linux/hyperv.h>
33 #include <asm/hyperv.h>
34 #include "hyperv_vmbus.h"
37 struct vmbus_connection vmbus_connection = {
38 .conn_state = DISCONNECTED,
39 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
43 * vmbus_connect - Sends a connect request on the partition service connection
45 int vmbus_connect(void)
48 struct vmbus_channel_msginfo *msginfo = NULL;
49 struct vmbus_channel_initiate_contact *msg;
52 /* Initialize the vmbus connection */
53 vmbus_connection.conn_state = CONNECTING;
54 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
55 if (!vmbus_connection.work_queue) {
60 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
61 spin_lock_init(&vmbus_connection.channelmsg_lock);
63 INIT_LIST_HEAD(&vmbus_connection.chn_list);
64 spin_lock_init(&vmbus_connection.channel_lock);
67 * Setup the vmbus event connection for channel interrupt
70 vmbus_connection.int_page =
71 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
72 if (vmbus_connection.int_page == NULL) {
77 vmbus_connection.recv_int_page = vmbus_connection.int_page;
78 vmbus_connection.send_int_page =
79 (void *)((unsigned long)vmbus_connection.int_page +
83 * Setup the monitor notification facility. The 1st page for
84 * parent->child and the 2nd page for child->parent
86 vmbus_connection.monitor_pages =
87 (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
88 if (vmbus_connection.monitor_pages == NULL) {
93 msginfo = kzalloc(sizeof(*msginfo) +
94 sizeof(struct vmbus_channel_initiate_contact),
96 if (msginfo == NULL) {
101 init_completion(&msginfo->waitevent);
103 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
105 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
106 msg->vmbus_version_requested = VMBUS_REVISION_NUMBER;
107 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
108 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages);
109 msg->monitor_page2 = virt_to_phys(
110 (void *)((unsigned long)vmbus_connection.monitor_pages +
114 * Add to list before we send the request since we may
115 * receive the response before returning from this routine
117 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
118 list_add_tail(&msginfo->msglistentry,
119 &vmbus_connection.chn_msg_list);
121 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
123 ret = vmbus_post_msg(msg,
124 sizeof(struct vmbus_channel_initiate_contact));
126 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
127 list_del(&msginfo->msglistentry);
128 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
133 /* Wait for the connection response */
134 wait_for_completion(&msginfo->waitevent);
136 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
137 list_del(&msginfo->msglistentry);
138 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
140 /* Check if successful */
141 if (msginfo->response.version_response.version_supported) {
142 vmbus_connection.conn_state = CONNECTED;
144 pr_err("Unable to connect, "
145 "Version %d not supported by Hyper-V\n",
146 VMBUS_REVISION_NUMBER);
155 vmbus_connection.conn_state = DISCONNECTED;
157 if (vmbus_connection.work_queue)
158 destroy_workqueue(vmbus_connection.work_queue);
160 if (vmbus_connection.int_page) {
161 free_pages((unsigned long)vmbus_connection.int_page, 0);
162 vmbus_connection.int_page = NULL;
165 if (vmbus_connection.monitor_pages) {
166 free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
167 vmbus_connection.monitor_pages = NULL;
177 * relid2channel - Get the channel object given its
178 * child relative id (ie channel id)
180 struct vmbus_channel *relid2channel(u32 relid)
182 struct vmbus_channel *channel;
183 struct vmbus_channel *found_channel = NULL;
186 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
187 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
188 if (channel->offermsg.child_relid == relid) {
189 found_channel = channel;
193 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
195 return found_channel;
199 * process_chn_event - Process a channel event notification
201 static void process_chn_event(u32 relid)
203 struct vmbus_channel *channel;
207 * Find the channel based on this relid and invokes the
208 * channel callback to process the event
210 channel = relid2channel(relid);
213 pr_err("channel not found for relid - %u\n", relid);
218 * A channel once created is persistent even when there
219 * is no driver handling the device. An unloading driver
220 * sets the onchannel_callback to NULL under the
221 * protection of the channel inbound_lock. Thus, checking
222 * and invoking the driver specific callback takes care of
223 * orderly unloading of the driver.
226 spin_lock_irqsave(&channel->inbound_lock, flags);
227 if (channel->onchannel_callback != NULL)
228 channel->onchannel_callback(channel->channel_callback_context);
230 pr_err("no channel callback for relid - %u\n", relid);
232 spin_unlock_irqrestore(&channel->inbound_lock, flags);
236 * vmbus_on_event - Handler for events
238 void vmbus_on_event(unsigned long data)
241 u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
244 u32 *recv_int_page = vmbus_connection.recv_int_page;
249 for (dword = 0; dword < maxdword; dword++) {
250 if (!recv_int_page[dword])
252 for (bit = 0; bit < 32; bit++) {
253 if (sync_test_and_clear_bit(bit,
254 (unsigned long *)&recv_int_page[dword])) {
255 relid = (dword << 5) + bit;
259 * Special case - vmbus
260 * channel protocol msg
264 process_chn_event(relid);
271 * vmbus_post_msg - Send a msg on the vmbus's message connection
273 int vmbus_post_msg(void *buffer, size_t buflen)
275 union hv_connection_id conn_id;
280 conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
283 * hv_post_message() can have transient failures because of
284 * insufficient resources. Retry the operation a couple of
285 * times before giving up.
287 while (retries < 3) {
288 ret = hv_post_message(conn_id, 1, buffer, buflen);
289 if (ret != HV_STATUS_INSUFFICIENT_BUFFERS)
298 * vmbus_set_event - Send an event notification to the parent
300 int vmbus_set_event(u32 child_relid)
302 /* Each u32 represents 32 channels */
303 sync_set_bit(child_relid & 31,
304 (unsigned long *)vmbus_connection.send_int_page +
307 return hv_signal_event();