Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / hv / connection.c
1 /*
2  *
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
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.
8  *
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.
13  *
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.
17  *
18  * Authors:
19  *   Haiyang Zhang <haiyangz@microsoft.com>
20  *   Hank Janssen  <hjanssen@microsoft.com>
21  *
22  */
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
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>
32 #include <linux/hyperv.h>
33 #include <asm/hyperv.h>
34 #include "hyperv_vmbus.h"
35
36
37 struct vmbus_connection vmbus_connection = {
38         .conn_state             = DISCONNECTED,
39         .next_gpadl_handle      = ATOMIC_INIT(0xE1E10),
40 };
41
42 /*
43  * vmbus_connect - Sends a connect request on the partition service connection
44  */
45 int vmbus_connect(void)
46 {
47         int ret = 0;
48         struct vmbus_channel_msginfo *msginfo = NULL;
49         struct vmbus_channel_initiate_contact *msg;
50         unsigned long flags;
51
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) {
56                 ret = -ENOMEM;
57                 goto cleanup;
58         }
59
60         INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
61         spin_lock_init(&vmbus_connection.channelmsg_lock);
62
63         INIT_LIST_HEAD(&vmbus_connection.chn_list);
64         spin_lock_init(&vmbus_connection.channel_lock);
65
66         /*
67          * Setup the vmbus event connection for channel interrupt
68          * abstraction stuff
69          */
70         vmbus_connection.int_page =
71         (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
72         if (vmbus_connection.int_page == NULL) {
73                 ret = -ENOMEM;
74                 goto cleanup;
75         }
76
77         vmbus_connection.recv_int_page = vmbus_connection.int_page;
78         vmbus_connection.send_int_page =
79                 (void *)((unsigned long)vmbus_connection.int_page +
80                         (PAGE_SIZE >> 1));
81
82         /*
83          * Setup the monitor notification facility. The 1st page for
84          * parent->child and the 2nd page for child->parent
85          */
86         vmbus_connection.monitor_pages =
87         (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
88         if (vmbus_connection.monitor_pages == NULL) {
89                 ret = -ENOMEM;
90                 goto cleanup;
91         }
92
93         msginfo = kzalloc(sizeof(*msginfo) +
94                           sizeof(struct vmbus_channel_initiate_contact),
95                           GFP_KERNEL);
96         if (msginfo == NULL) {
97                 ret = -ENOMEM;
98                 goto cleanup;
99         }
100
101         init_completion(&msginfo->waitevent);
102
103         msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
104
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 +
111                                  PAGE_SIZE));
112
113         /*
114          * Add to list before we send the request since we may
115          * receive the response before returning from this routine
116          */
117         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
118         list_add_tail(&msginfo->msglistentry,
119                       &vmbus_connection.chn_msg_list);
120
121         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
122
123         ret = vmbus_post_msg(msg,
124                                sizeof(struct vmbus_channel_initiate_contact));
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                 goto cleanup;
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         } else {
144                 pr_err("Unable to connect, "
145                         "Version %d not supported by Hyper-V\n",
146                         VMBUS_REVISION_NUMBER);
147                 ret = -ECONNREFUSED;
148                 goto cleanup;
149         }
150
151         kfree(msginfo);
152         return 0;
153
154 cleanup:
155         vmbus_connection.conn_state = DISCONNECTED;
156
157         if (vmbus_connection.work_queue)
158                 destroy_workqueue(vmbus_connection.work_queue);
159
160         if (vmbus_connection.int_page) {
161                 free_pages((unsigned long)vmbus_connection.int_page, 0);
162                 vmbus_connection.int_page = NULL;
163         }
164
165         if (vmbus_connection.monitor_pages) {
166                 free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
167                 vmbus_connection.monitor_pages = NULL;
168         }
169
170         kfree(msginfo);
171
172         return ret;
173 }
174
175
176 /*
177  * relid2channel - Get the channel object given its
178  * child relative id (ie channel id)
179  */
180 struct vmbus_channel *relid2channel(u32 relid)
181 {
182         struct vmbus_channel *channel;
183         struct vmbus_channel *found_channel  = NULL;
184         unsigned long flags;
185
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;
190                         break;
191                 }
192         }
193         spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
194
195         return found_channel;
196 }
197
198 /*
199  * process_chn_event - Process a channel event notification
200  */
201 static void process_chn_event(u32 relid)
202 {
203         struct vmbus_channel *channel;
204         unsigned long flags;
205
206         /*
207          * Find the channel based on this relid and invokes the
208          * channel callback to process the event
209          */
210         channel = relid2channel(relid);
211
212         if (!channel) {
213                 pr_err("channel not found for relid - %u\n", relid);
214                 return;
215         }
216
217         /*
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.
224          */
225
226         spin_lock_irqsave(&channel->inbound_lock, flags);
227         if (channel->onchannel_callback != NULL)
228                 channel->onchannel_callback(channel->channel_callback_context);
229         else
230                 pr_err("no channel callback for relid - %u\n", relid);
231
232         spin_unlock_irqrestore(&channel->inbound_lock, flags);
233 }
234
235 /*
236  * vmbus_on_event - Handler for events
237  */
238 void vmbus_on_event(unsigned long data)
239 {
240         u32 dword;
241         u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
242         int bit;
243         u32 relid;
244         u32 *recv_int_page = vmbus_connection.recv_int_page;
245
246         /* Check events */
247         if (!recv_int_page)
248                 return;
249         for (dword = 0; dword < maxdword; dword++) {
250                 if (!recv_int_page[dword])
251                         continue;
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;
256
257                                 if (relid == 0)
258                                         /*
259                                          * Special case - vmbus
260                                          * channel protocol msg
261                                          */
262                                         continue;
263
264                                 process_chn_event(relid);
265                         }
266                 }
267         }
268 }
269
270 /*
271  * vmbus_post_msg - Send a msg on the vmbus's message connection
272  */
273 int vmbus_post_msg(void *buffer, size_t buflen)
274 {
275         union hv_connection_id conn_id;
276         int ret = 0;
277         int retries = 0;
278
279         conn_id.asu32 = 0;
280         conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
281
282         /*
283          * hv_post_message() can have transient failures because of
284          * insufficient resources. Retry the operation a couple of
285          * times before giving up.
286          */
287         while (retries < 10) {
288                 ret = hv_post_message(conn_id, 1, buffer, buflen);
289
290                 switch (ret) {
291                 case HV_STATUS_INSUFFICIENT_BUFFERS:
292                         ret = -ENOMEM;
293                 case -ENOMEM:
294                         break;
295                 case HV_STATUS_SUCCESS:
296                         return ret;
297                 default:
298                         pr_err("hv_post_msg() failed; error code:%d\n", ret);
299                         return -EINVAL;
300                 }
301
302                 retries++;
303                 msleep(100);
304         }
305         return ret;
306 }
307
308 /*
309  * vmbus_set_event - Send an event notification to the parent
310  */
311 int vmbus_set_event(u32 child_relid)
312 {
313         /* Each u32 represents 32 channels */
314         sync_set_bit(child_relid & 31,
315                 (unsigned long *)vmbus_connection.send_int_page +
316                 (child_relid >> 5));
317
318         return hv_signal_event();
319 }