Staging: hv: Get rid of vmbus_child_dev_add()
[linux-2.6-block.git] / drivers / staging / hv / connection.c
CommitLineData
3e7ee490
HJ
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 */
a0086dc5 23#include <linux/kernel.h>
0c3b7b2f
S
24#include <linux/sched.h>
25#include <linux/wait.h>
a0086dc5 26#include <linux/mm.h>
5a0e3ad6 27#include <linux/slab.h>
a0086dc5 28#include <linux/vmalloc.h>
e3fe0bb6 29#include "hv_api.h"
645954c5 30#include "logging.h"
72daf320 31#include "vmbus_private.h"
3e7ee490 32
3e7ee490 33
da9fcb72
HZ
34struct vmbus_connection vmbus_connection = {
35 .conn_state = DISCONNECTED,
36 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
3e7ee490
HJ
37};
38
3e189519 39/*
c6977677 40 * vmbus_connect - Sends a connect request on the partition service connection
fd8b85ea 41 */
c6977677 42int vmbus_connect(void)
3e7ee490 43{
fd8b85ea 44 int ret = 0;
15b2f647 45 struct vmbus_channel_msginfo *msginfo = NULL;
82250213 46 struct vmbus_channel_initiate_contact *msg;
dd0813b6 47 unsigned long flags;
3e7ee490 48
454f18a9 49 /* Make sure we are not connecting or connected */
da9fcb72 50 if (vmbus_connection.conn_state != DISCONNECTED)
3e7ee490
HJ
51 return -1;
52
454f18a9 53 /* Initialize the vmbus connection */
da9fcb72
HZ
54 vmbus_connection.conn_state = CONNECTING;
55 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
56 if (!vmbus_connection.work_queue) {
de65a384
BP
57 ret = -1;
58 goto Cleanup;
59 }
3e7ee490 60
da9fcb72 61 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
15b2f647 62 spin_lock_init(&vmbus_connection.channelmsg_lock);
3e7ee490 63
da9fcb72 64 INIT_LIST_HEAD(&vmbus_connection.chn_list);
15b2f647 65 spin_lock_init(&vmbus_connection.channel_lock);
3e7ee490 66
454f18a9
BP
67 /*
68 * Setup the vmbus event connection for channel interrupt
69 * abstraction stuff
70 */
df3493e0
S
71 vmbus_connection.int_page =
72 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
da9fcb72 73 if (vmbus_connection.int_page == NULL) {
3e7ee490
HJ
74 ret = -1;
75 goto Cleanup;
76 }
77
da9fcb72
HZ
78 vmbus_connection.recv_int_page = vmbus_connection.int_page;
79 vmbus_connection.send_int_page =
80 (void *)((unsigned long)vmbus_connection.int_page +
fd8b85ea 81 (PAGE_SIZE >> 1));
3e7ee490 82
fd8b85ea
GKH
83 /*
84 * Setup the monitor notification facility. The 1st page for
85 * parent->child and the 2nd page for child->parent
454f18a9 86 */
df3493e0
S
87 vmbus_connection.monitor_pages =
88 (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1);
da9fcb72 89 if (vmbus_connection.monitor_pages == NULL) {
3e7ee490
HJ
90 ret = -1;
91 goto Cleanup;
92 }
93
15b2f647 94 msginfo = kzalloc(sizeof(*msginfo) +
fd8b85ea
GKH
95 sizeof(struct vmbus_channel_initiate_contact),
96 GFP_KERNEL);
15b2f647 97 if (msginfo == NULL) {
8cad0af9 98 ret = -ENOMEM;
3e7ee490
HJ
99 goto Cleanup;
100 }
101
0c3b7b2f 102 init_waitqueue_head(&msginfo->waitevent);
80d11b2a 103
15b2f647 104 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
3e7ee490 105
c50f7fb2
HZ
106 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
107 msg->vmbus_version_requested = VMBUS_REVISION_NUMBER;
da9fcb72
HZ
108 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
109 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages);
c50f7fb2 110 msg->monitor_page2 = virt_to_phys(
da9fcb72 111 (void *)((unsigned long)vmbus_connection.monitor_pages +
fd8b85ea 112 PAGE_SIZE));
3e7ee490 113
454f18a9
BP
114 /*
115 * Add to list before we send the request since we may
116 * receive the response before returning from this routine
117 */
15b2f647
HZ
118 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
119 list_add_tail(&msginfo->msglistentry,
da9fcb72 120 &vmbus_connection.chn_msg_list);
53af545b 121
15b2f647 122 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 123
fd8b85ea
GKH
124 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, "
125 "monitor1 pfn %llx,, monitor2 pfn %llx",
c50f7fb2 126 msg->interrupt_page, msg->monitor_page1, msg->monitor_page2);
3e7ee490
HJ
127
128 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
c6977677 129 ret = vmbus_post_msg(msg,
fd8b85ea
GKH
130 sizeof(struct vmbus_channel_initiate_contact));
131 if (ret != 0) {
0c3b7b2f 132 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
15b2f647 133 list_del(&msginfo->msglistentry);
0c3b7b2f
S
134 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
135 flags);
3e7ee490
HJ
136 goto Cleanup;
137 }
138
454f18a9 139 /* Wait for the connection response */
0c3b7b2f
S
140 msginfo->wait_condition = 0;
141 wait_event_timeout(msginfo->waitevent, msginfo->wait_condition,
142 msecs_to_jiffies(1000));
143 if (msginfo->wait_condition == 0) {
144 spin_lock_irqsave(&vmbus_connection.channelmsg_lock,
145 flags);
146 list_del(&msginfo->msglistentry);
147 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
148 flags);
149 ret = -ETIMEDOUT;
150 goto Cleanup;
151 }
3e7ee490 152
0c3b7b2f 153 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
15b2f647 154 list_del(&msginfo->msglistentry);
0c3b7b2f 155 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 156
454f18a9 157 /* Check if successful */
15b2f647 158 if (msginfo->response.version_response.version_supported) {
3e7ee490 159 DPRINT_INFO(VMBUS, "Vmbus connected!!");
da9fcb72 160 vmbus_connection.conn_state = CONNECTED;
3e7ee490 161
fd8b85ea
GKH
162 } else {
163 DPRINT_ERR(VMBUS, "Vmbus connection failed!!..."
164 "current version (%d) not supported",
165 VMBUS_REVISION_NUMBER);
3e7ee490 166 ret = -1;
3e7ee490
HJ
167 goto Cleanup;
168 }
169
15b2f647 170 kfree(msginfo);
3e7ee490
HJ
171 return 0;
172
173Cleanup:
da9fcb72 174 vmbus_connection.conn_state = DISCONNECTED;
3e7ee490 175
da9fcb72
HZ
176 if (vmbus_connection.work_queue)
177 destroy_workqueue(vmbus_connection.work_queue);
3e7ee490 178
da9fcb72 179 if (vmbus_connection.int_page) {
df3493e0 180 free_pages((unsigned long)vmbus_connection.int_page, 0);
da9fcb72 181 vmbus_connection.int_page = NULL;
3e7ee490
HJ
182 }
183
da9fcb72 184 if (vmbus_connection.monitor_pages) {
df3493e0 185 free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
da9fcb72 186 vmbus_connection.monitor_pages = NULL;
3e7ee490
HJ
187 }
188
15b2f647 189 if (msginfo) {
15b2f647 190 kfree(msginfo);
3e7ee490
HJ
191 }
192
3e7ee490
HJ
193 return ret;
194}
195
3e189519 196/*
c6977677
HZ
197 * vmbus_disconnect -
198 * Sends a disconnect request on the partition service connection
fd8b85ea 199 */
c6977677 200int vmbus_disconnect(void)
3e7ee490 201{
fd8b85ea 202 int ret = 0;
82250213 203 struct vmbus_channel_message_header *msg;
3e7ee490 204
454f18a9 205 /* Make sure we are connected */
da9fcb72 206 if (vmbus_connection.conn_state != CONNECTED)
3e7ee490
HJ
207 return -1;
208
82250213 209 msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
8cad0af9
BP
210 if (!msg)
211 return -ENOMEM;
3e7ee490 212
c50f7fb2 213 msg->msgtype = CHANNELMSG_UNLOAD;
3e7ee490 214
c6977677 215 ret = vmbus_post_msg(msg,
fd8b85ea 216 sizeof(struct vmbus_channel_message_header));
3e7ee490 217 if (ret != 0)
3e7ee490 218 goto Cleanup;
3e7ee490 219
df3493e0
S
220 free_pages((unsigned long)vmbus_connection.int_page, 0);
221 free_pages((unsigned long)vmbus_connection.monitor_pages, 1);
3e7ee490 222
454f18a9 223 /* TODO: iterate thru the msg list and free up */
da9fcb72 224 destroy_workqueue(vmbus_connection.work_queue);
3e7ee490 225
da9fcb72 226 vmbus_connection.conn_state = DISCONNECTED;
3e7ee490
HJ
227
228 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
229
230Cleanup:
fd8b85ea 231 kfree(msg);
3e7ee490
HJ
232 return ret;
233}
234
3e189519 235/*
c6977677
HZ
236 * relid2channel - Get the channel object given its
237 * child relative id (ie channel id)
fd8b85ea 238 */
c6977677 239struct vmbus_channel *relid2channel(u32 relid)
3e7ee490 240{
aded7165 241 struct vmbus_channel *channel;
15b2f647 242 struct vmbus_channel *found_channel = NULL;
0f5e44ca 243 unsigned long flags;
3e7ee490 244
15b2f647 245 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
da9fcb72 246 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
15b2f647
HZ
247 if (channel->offermsg.child_relid == relid) {
248 found_channel = channel;
3e7ee490
HJ
249 break;
250 }
251 }
15b2f647 252 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
3e7ee490 253
15b2f647 254 return found_channel;
3e7ee490
HJ
255}
256
3e189519 257/*
c6977677 258 * process_chn_event - Process a channel event notification
fd8b85ea 259 */
c6977677 260static void process_chn_event(void *context)
3e7ee490 261{
aded7165 262 struct vmbus_channel *channel;
15b2f647 263 u32 relid = (u32)(unsigned long)context;
3e7ee490 264
972b9529 265 /* ASSERT(relId > 0); */
3e7ee490 266
454f18a9
BP
267 /*
268 * Find the channel based on this relid and invokes the
269 * channel callback to process the event
270 */
c6977677 271 channel = relid2channel(relid);
3e7ee490 272
fd8b85ea 273 if (channel) {
fff41b2e 274 vmbus_onchannel_event(channel);
fd8b85ea
GKH
275 /*
276 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
fff41b2e 277 * vmbus_onchannel_event,
0686e4f4 278 * (void*)channel);
fd8b85ea
GKH
279 */
280 } else {
15b2f647 281 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relid);
3e7ee490
HJ
282 }
283}
284
3e189519 285/*
c6977677 286 * vmbus_on_event - Handler for events
fd8b85ea 287 */
6de3d6aa 288void vmbus_on_event(unsigned long data)
3e7ee490
HJ
289{
290 int dword;
3e7ee490
HJ
291 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
292 int bit;
293 int relid;
da9fcb72 294 u32 *recv_int_page = vmbus_connection.recv_int_page;
3e7ee490 295
454f18a9 296 /* Check events */
15b2f647 297 if (recv_int_page) {
fd8b85ea 298 for (dword = 0; dword < maxdword; dword++) {
15b2f647 299 if (recv_int_page[dword]) {
fd8b85ea 300 for (bit = 0; bit < 32; bit++) {
15b2f647
HZ
301 if (test_and_clear_bit(bit,
302 (unsigned long *)
303 &recv_int_page[dword])) {
3e7ee490 304 relid = (dword << 5) + bit;
3e7ee490
HJ
305 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
306
fd8b85ea
GKH
307 if (relid == 0) {
308 /* special case - vmbus channel protocol msg */
3e7ee490 309 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
fd8b85ea
GKH
310 continue;
311 } else {
454f18a9
BP
312 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
313 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
c6977677
HZ
314 process_chn_event((void *)
315 (unsigned long)relid);
3e7ee490
HJ
316 }
317 }
318 }
319 }
320 }
321 }
3e7ee490
HJ
322 return;
323}
324
3e189519 325/*
c6977677 326 * vmbus_post_msg - Send a msg on the vmbus's message connection
fd8b85ea 327 */
c6977677 328int vmbus_post_msg(void *buffer, size_t buflen)
3e7ee490 329{
15b2f647 330 union hv_connection_id conn_id;
3e7ee490 331
15b2f647
HZ
332 conn_id.asu32 = 0;
333 conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID;
334 return hv_post_message(conn_id, 1, buffer, buflen);
3e7ee490
HJ
335}
336
3e189519 337/*
c6977677 338 * vmbus_set_event - Send an event notification to the parent
fd8b85ea 339 */
c6977677 340int vmbus_set_event(u32 child_relid)
3e7ee490 341{
454f18a9 342 /* Each u32 represents 32 channels */
15b2f647 343 set_bit(child_relid & 31,
da9fcb72 344 (unsigned long *)vmbus_connection.send_int_page +
15b2f647 345 (child_relid >> 5));
7c369f40 346
d44890c8 347 return hv_signal_event();
3e7ee490 348}