Staging: merge 2.6.39-rc3 into staging-next
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / connection.c
blobb71ed27195231af57f36c96c0632750e29bb988e
1 /*
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
12 * more details.
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.
18 * Authors:
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/mm.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include "hv_api.h"
32 #include "logging.h"
33 #include "vmbus_private.h"
36 struct vmbus_connection vmbus_connection = {
37 .conn_state = DISCONNECTED,
38 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
42 * vmbus_connect - Sends a connect request on the partition service connection
44 int vmbus_connect(void)
46 int ret = 0;
47 struct vmbus_channel_msginfo *msginfo = NULL;
48 struct vmbus_channel_initiate_contact *msg;
49 unsigned long flags;
51 /* Make sure we are not connecting or connected */
52 if (vmbus_connection.conn_state != DISCONNECTED)
53 return -1;
55 /* Initialize the vmbus connection */
56 vmbus_connection.conn_state = CONNECTING;
57 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
58 if (!vmbus_connection.work_queue) {
59 ret = -1;
60 goto Cleanup;
63 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
64 spin_lock_init(&vmbus_connection.channelmsg_lock);
66 INIT_LIST_HEAD(&vmbus_connection.chn_list);
67 spin_lock_init(&vmbus_connection.channel_lock);
70 * Setup the vmbus event connection for channel interrupt
71 * abstraction stuff
73 vmbus_connection.int_page =
74 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
75 if (vmbus_connection.int_page == NULL) {
76 ret = -1;
77 goto Cleanup;
80 vmbus_connection.recv_int_page = vmbus_connection.int_page;
81 vmbus_connection.send_int_page =
82 (void *)((unsigned long)vmbus_connection.int_page +
83 (PAGE_SIZE >> 1));
86 * Setup the monitor notification facility. The 1st page for
87 * parent->child and the 2nd page for child->parent
89 vmbus_connection.monitor_pages =
90 (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
91 if (vmbus_connection.monitor_pages == NULL) {
92 ret = -1;
93 goto Cleanup;
96 msginfo = kzalloc(sizeof(*msginfo) +
97 sizeof(struct vmbus_channel_initiate_contact),
98 GFP_KERNEL);
99 if (msginfo == NULL) {
100 ret = -ENOMEM;
101 goto Cleanup;
104 init_waitqueue_head(&msginfo->waitevent);
106 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
108 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
109 msg->vmbus_version_requested = VMBUS_REVISION_NUMBER;
110 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
111 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages);
112 msg->monitor_page2 = virt_to_phys(
113 (void *)((unsigned long)vmbus_connection.monitor_pages +
114 PAGE_SIZE));
117 * Add to list before we send the request since we may
118 * receive the response before returning from this routine
120 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
121 list_add_tail(&msginfo->msglistentry,
122 &vmbus_connection.chn_msg_list);
124 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
126 ret = vmbus_post_msg(msg,
127 sizeof(struct vmbus_channel_initiate_contact));
128 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 goto Cleanup;
136 /* Wait for the connection response */
137 msginfo->wait_condition = 0;
138 wait_event_timeout(msginfo->waitevent, msginfo->wait_condition,
139 msecs_to_jiffies(1000));
140 if (msginfo->wait_condition == 0) {
141 spin_lock_irqsave(&vmbus_connection.channelmsg_lock,
142 flags);
143 list_del(&msginfo->msglistentry);
144 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
145 flags);
146 ret = -ETIMEDOUT;
147 goto Cleanup;
150 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
151 list_del(&msginfo->msglistentry);
152 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
154 /* Check if successful */
155 if (msginfo->response.version_response.version_supported) {
156 vmbus_connection.conn_state = CONNECTED;
157 } else {
158 pr_err("Unable to connect, "
159 "Version %d not supported by Hyper-V\n",
160 VMBUS_REVISION_NUMBER);
161 ret = -1;
162 goto Cleanup;
165 kfree(msginfo);
166 return 0;
168 Cleanup:
169 vmbus_connection.conn_state = DISCONNECTED;
171 if (vmbus_connection.work_queue)
172 destroy_workqueue(vmbus_connection.work_queue);
174 if (vmbus_connection.int_page) {
175 free_pages((unsigned long)vmbus_connection.int_page, 0);
176 vmbus_connection.int_page = NULL;
179 if (vmbus_connection.monitor_pages) {
180 free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
181 vmbus_connection.monitor_pages = NULL;
184 kfree(msginfo);
186 return ret;
190 * vmbus_disconnect -
191 * Sends a disconnect request on the partition service connection
193 int vmbus_disconnect(void)
195 int ret = 0;
196 struct vmbus_channel_message_header *msg;
198 /* Make sure we are connected */
199 if (vmbus_connection.conn_state != CONNECTED)
200 return -1;
202 msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
203 if (!msg)
204 return -ENOMEM;
206 msg->msgtype = CHANNELMSG_UNLOAD;
208 ret = vmbus_post_msg(msg,
209 sizeof(struct vmbus_channel_message_header));
210 if (ret != 0)
211 goto Cleanup;
213 free_pages((unsigned long)vmbus_connection.int_page, 0);
214 free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
216 /* TODO: iterate thru the msg list and free up */
217 destroy_workqueue(vmbus_connection.work_queue);
219 vmbus_connection.conn_state = DISCONNECTED;
221 pr_info("hv_vmbus disconnected\n");
223 Cleanup:
224 kfree(msg);
225 return ret;
229 * relid2channel - Get the channel object given its
230 * child relative id (ie channel id)
232 struct vmbus_channel *relid2channel(u32 relid)
234 struct vmbus_channel *channel;
235 struct vmbus_channel *found_channel = NULL;
236 unsigned long flags;
238 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
239 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
240 if (channel->offermsg.child_relid == relid) {
241 found_channel = channel;
242 break;
245 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
247 return found_channel;
251 * process_chn_event - Process a channel event notification
253 static void process_chn_event(void *context)
255 struct vmbus_channel *channel;
256 u32 relid = (u32)(unsigned long)context;
258 /* ASSERT(relId > 0); */
261 * Find the channel based on this relid and invokes the
262 * channel callback to process the event
264 channel = relid2channel(relid);
266 if (channel) {
267 vmbus_onchannel_event(channel);
269 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
270 * vmbus_onchannel_event,
271 * (void*)channel);
273 } else {
274 pr_err("channel not found for relid - %d\n", relid);
279 * vmbus_on_event - Handler for events
281 void vmbus_on_event(unsigned long data)
283 int dword;
284 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
285 int bit;
286 int relid;
287 u32 *recv_int_page = vmbus_connection.recv_int_page;
289 /* Check events */
290 if (recv_int_page) {
291 for (dword = 0; dword < maxdword; dword++) {
292 if (recv_int_page[dword]) {
293 for (bit = 0; bit < 32; bit++) {
294 if (sync_test_and_clear_bit(bit,
295 (unsigned long *)
296 &recv_int_page[dword])) {
297 relid = (dword << 5) + bit;
299 if (relid == 0) {
300 /* special case - vmbus channel protocol msg */
301 continue;
302 } else {
303 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
304 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
305 process_chn_event((void *)
306 (unsigned long)relid);
313 return;
317 * vmbus_post_msg - Send a msg on the vmbus's message connection
319 int vmbus_post_msg(void *buffer, size_t buflen)
321 union hv_connection_id conn_id;
323 conn_id.asu32 = 0;
324 conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
325 return hv_post_message(conn_id, 1, buffer, buflen);
329 * vmbus_set_event - Send an event notification to the parent
331 int vmbus_set_event(u32 child_relid)
333 /* Each u32 represents 32 channels */
334 sync_set_bit(child_relid & 31,
335 (unsigned long *)vmbus_connection.send_int_page +
336 (child_relid >> 5));
338 return hv_signal_event();