Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / staging / hv / connection.c
blob9e99c044b23fd6b2ea26e379fe70c7a7f04fe369
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/delay.h>
29 #include <linux/mm.h>
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
33 #include "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)
47 int ret = 0;
48 int t;
49 struct vmbus_channel_msginfo *msginfo = NULL;
50 struct vmbus_channel_initiate_contact *msg;
51 unsigned long flags;
53 /* Initialize the vmbus connection */
54 vmbus_connection.conn_state = CONNECTING;
55 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
56 if (!vmbus_connection.work_queue) {
57 ret = -ENOMEM;
58 goto cleanup;
61 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
62 spin_lock_init(&vmbus_connection.channelmsg_lock);
64 INIT_LIST_HEAD(&vmbus_connection.chn_list);
65 spin_lock_init(&vmbus_connection.channel_lock);
68 * Setup the vmbus event connection for channel interrupt
69 * abstraction stuff
71 vmbus_connection.int_page =
72 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
73 if (vmbus_connection.int_page == NULL) {
74 ret = -ENOMEM;
75 goto cleanup;
78 vmbus_connection.recv_int_page = vmbus_connection.int_page;
79 vmbus_connection.send_int_page =
80 (void *)((unsigned long)vmbus_connection.int_page +
81 (PAGE_SIZE >> 1));
84 * Setup the monitor notification facility. The 1st page for
85 * parent->child and the 2nd page for child->parent
87 vmbus_connection.monitor_pages =
88 (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
89 if (vmbus_connection.monitor_pages == NULL) {
90 ret = -ENOMEM;
91 goto cleanup;
94 msginfo = kzalloc(sizeof(*msginfo) +
95 sizeof(struct vmbus_channel_initiate_contact),
96 GFP_KERNEL);
97 if (msginfo == NULL) {
98 ret = -ENOMEM;
99 goto cleanup;
102 init_completion(&msginfo->waitevent);
104 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
106 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
107 msg->vmbus_version_requested = VMBUS_REVISION_NUMBER;
108 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
109 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages);
110 msg->monitor_page2 = virt_to_phys(
111 (void *)((unsigned long)vmbus_connection.monitor_pages +
112 PAGE_SIZE));
115 * Add to list before we send the request since we may
116 * receive the response before returning from this routine
118 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
119 list_add_tail(&msginfo->msglistentry,
120 &vmbus_connection.chn_msg_list);
122 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
124 ret = vmbus_post_msg(msg,
125 sizeof(struct vmbus_channel_initiate_contact));
126 if (ret != 0) {
127 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
128 list_del(&msginfo->msglistentry);
129 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
130 flags);
131 goto cleanup;
134 /* Wait for the connection response */
135 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
136 if (t == 0) {
137 spin_lock_irqsave(&vmbus_connection.channelmsg_lock,
138 flags);
139 list_del(&msginfo->msglistentry);
140 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
141 flags);
142 ret = -ETIMEDOUT;
143 goto cleanup;
146 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
147 list_del(&msginfo->msglistentry);
148 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
150 /* Check if successful */
151 if (msginfo->response.version_response.version_supported) {
152 vmbus_connection.conn_state = CONNECTED;
153 } else {
154 pr_err("Unable to connect, "
155 "Version %d not supported by Hyper-V\n",
156 VMBUS_REVISION_NUMBER);
157 ret = -ECONNREFUSED;
158 goto cleanup;
161 kfree(msginfo);
162 return 0;
164 cleanup:
165 vmbus_connection.conn_state = DISCONNECTED;
167 if (vmbus_connection.work_queue)
168 destroy_workqueue(vmbus_connection.work_queue);
170 if (vmbus_connection.int_page) {
171 free_pages((unsigned long)vmbus_connection.int_page, 0);
172 vmbus_connection.int_page = NULL;
175 if (vmbus_connection.monitor_pages) {
176 free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
177 vmbus_connection.monitor_pages = NULL;
180 kfree(msginfo);
182 return ret;
187 * relid2channel - Get the channel object given its
188 * child relative id (ie channel id)
190 struct vmbus_channel *relid2channel(u32 relid)
192 struct vmbus_channel *channel;
193 struct vmbus_channel *found_channel = NULL;
194 unsigned long flags;
196 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
197 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
198 if (channel->offermsg.child_relid == relid) {
199 found_channel = channel;
200 break;
203 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
205 return found_channel;
209 * process_chn_event - Process a channel event notification
211 static void process_chn_event(u32 relid)
213 struct vmbus_channel *channel;
214 unsigned long flags;
217 * Find the channel based on this relid and invokes the
218 * channel callback to process the event
220 channel = relid2channel(relid);
222 spin_lock_irqsave(&channel->inbound_lock, flags);
223 if (channel && (channel->onchannel_callback != NULL))
224 channel->onchannel_callback(channel->channel_callback_context);
225 else
226 pr_err("channel not found for relid - %u\n", relid);
228 spin_unlock_irqrestore(&channel->inbound_lock, flags);
232 * vmbus_on_event - Handler for events
234 void vmbus_on_event(unsigned long data)
236 u32 dword;
237 u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
238 int bit;
239 u32 relid;
240 u32 *recv_int_page = vmbus_connection.recv_int_page;
242 /* Check events */
243 if (!recv_int_page)
244 return;
245 for (dword = 0; dword < maxdword; dword++) {
246 if (!recv_int_page[dword])
247 continue;
248 for (bit = 0; bit < 32; bit++) {
249 if (sync_test_and_clear_bit(bit,
250 (unsigned long *)&recv_int_page[dword])) {
251 relid = (dword << 5) + bit;
253 if (relid == 0)
255 * Special case - vmbus
256 * channel protocol msg
258 continue;
260 process_chn_event(relid);
267 * vmbus_post_msg - Send a msg on the vmbus's message connection
269 int vmbus_post_msg(void *buffer, size_t buflen)
271 union hv_connection_id conn_id;
272 int ret = 0;
273 int retries = 0;
275 conn_id.asu32 = 0;
276 conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
279 * hv_post_message() can have transient failures because of
280 * insufficient resources. Retry the operation a couple of
281 * times before giving up.
283 while (retries < 3) {
284 ret = hv_post_message(conn_id, 1, buffer, buflen);
285 if (ret != HV_STATUS_INSUFFICIENT_BUFFERS)
286 return ret;
287 retries++;
288 msleep(100);
290 return ret;
294 * vmbus_set_event - Send an event notification to the parent
296 int vmbus_set_event(u32 child_relid)
298 /* Each u32 represents 32 channels */
299 sync_set_bit(child_relid & 31,
300 (unsigned long *)vmbus_connection.send_int_page +
301 (child_relid >> 5));
303 return hv_signal_event();