Staging: hv: move osd.h
[linux-2.6/mini2440.git] / drivers / staging / hv / Connection.c
blob9083f33a4b3dd62c339769916ad697839a1a21d0
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>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/vmalloc.h>
28 #include "osd.h"
29 #include "include/logging.h"
30 #include "VmbusPrivate.h"
32 /* Globals */
35 struct VMBUS_CONNECTION gVmbusConnection = {
36 .ConnectState = Disconnected,
37 .NextGpadlHandle = ATOMIC_INIT(0xE1E10),
41 /*++
43 Name:
44 VmbusConnect()
46 Description:
47 Sends a connect request on the partition service connection
49 --*/
50 int VmbusConnect(void)
52 int ret=0;
53 struct vmbus_channel_msginfo *msgInfo = NULL;
54 VMBUS_CHANNEL_INITIATE_CONTACT *msg;
55 unsigned long flags;
57 DPRINT_ENTER(VMBUS);
59 /* Make sure we are not connecting or connected */
60 if (gVmbusConnection.ConnectState != Disconnected)
61 return -1;
63 /* Initialize the vmbus connection */
64 gVmbusConnection.ConnectState = Connecting;
65 gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
66 if (!gVmbusConnection.WorkQueue)
68 ret = -1;
69 goto Cleanup;
72 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
73 spin_lock_init(&gVmbusConnection.channelmsg_lock);
75 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelList);
76 spin_lock_init(&gVmbusConnection.channel_lock);
79 * Setup the vmbus event connection for channel interrupt
80 * abstraction stuff
82 gVmbusConnection.InterruptPage = osd_PageAlloc(1);
83 if (gVmbusConnection.InterruptPage == NULL)
85 ret = -1;
86 goto Cleanup;
89 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
90 gVmbusConnection.SendInterruptPage = (void*)((unsigned long)gVmbusConnection.InterruptPage + (PAGE_SIZE >> 1));
92 /* Setup the monitor
93 * notification facility. The 1st page for parent->child and
94 * the 2nd page for child->parent
96 gVmbusConnection.MonitorPages = osd_PageAlloc(2);
97 if (gVmbusConnection.MonitorPages == NULL)
99 ret = -1;
100 goto Cleanup;
103 msgInfo = kzalloc(sizeof(*msgInfo) + sizeof(VMBUS_CHANNEL_INITIATE_CONTACT), GFP_KERNEL);
104 if (msgInfo == NULL)
106 ret = -1;
107 goto Cleanup;
110 msgInfo->WaitEvent = osd_WaitEventCreate();
111 msg = (VMBUS_CHANNEL_INITIATE_CONTACT*)msgInfo->Msg;
113 msg->Header.MessageType = ChannelMessageInitiateContact;
114 msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
115 msg->InterruptPage = virt_to_phys(gVmbusConnection.InterruptPage);
116 msg->MonitorPage1 = virt_to_phys(gVmbusConnection.MonitorPages);
117 msg->MonitorPage2 = virt_to_phys((void *)((unsigned long)gVmbusConnection.MonitorPages + PAGE_SIZE));
120 * Add to list before we send the request since we may
121 * receive the response before returning from this routine
123 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
124 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
125 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
127 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, monitor1 pfn %llx,, monitor2 pfn %llx",
128 msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
130 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
132 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_INITIATE_CONTACT));
133 if (ret != 0)
135 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
136 goto Cleanup;
139 /* Wait for the connection response */
140 osd_WaitEventWait(msgInfo->WaitEvent);
142 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
144 /* Check if successful */
145 if (msgInfo->Response.VersionResponse.VersionSupported)
147 DPRINT_INFO(VMBUS, "Vmbus connected!!");
148 gVmbusConnection.ConnectState = Connected;
151 else
153 DPRINT_ERR(VMBUS, "Vmbus connection failed!!...current version (%d) not supported", VMBUS_REVISION_NUMBER);
154 ret = -1;
156 goto Cleanup;
160 kfree(msgInfo->WaitEvent);
161 kfree(msgInfo);
162 DPRINT_EXIT(VMBUS);
164 return 0;
166 Cleanup:
168 gVmbusConnection.ConnectState = Disconnected;
170 if (gVmbusConnection.WorkQueue)
171 destroy_workqueue(gVmbusConnection.WorkQueue);
173 if (gVmbusConnection.InterruptPage)
175 osd_PageFree(gVmbusConnection.InterruptPage, 1);
176 gVmbusConnection.InterruptPage = NULL;
179 if (gVmbusConnection.MonitorPages)
181 osd_PageFree(gVmbusConnection.MonitorPages, 2);
182 gVmbusConnection.MonitorPages = NULL;
185 if (msgInfo)
187 if (msgInfo->WaitEvent)
188 kfree(msgInfo->WaitEvent);
190 kfree(msgInfo);
193 DPRINT_EXIT(VMBUS);
195 return ret;
199 /*++
201 Name:
202 VmbusDisconnect()
204 Description:
205 Sends a disconnect request on the partition service connection
207 --*/
208 int VmbusDisconnect(void)
210 int ret=0;
211 VMBUS_CHANNEL_UNLOAD *msg;
213 DPRINT_ENTER(VMBUS);
215 /* Make sure we are connected */
216 if (gVmbusConnection.ConnectState != Connected)
217 return -1;
219 msg = kzalloc(sizeof(VMBUS_CHANNEL_UNLOAD), GFP_KERNEL);
221 msg->MessageType = ChannelMessageUnload;
223 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_UNLOAD));
225 if (ret != 0)
227 goto Cleanup;
230 osd_PageFree(gVmbusConnection.InterruptPage, 1);
232 /* TODO: iterate thru the msg list and free up */
234 destroy_workqueue(gVmbusConnection.WorkQueue);
236 gVmbusConnection.ConnectState = Disconnected;
238 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
240 Cleanup:
241 if (msg)
243 kfree(msg);
246 DPRINT_EXIT(VMBUS);
248 return ret;
252 /*++
254 Name:
255 GetChannelFromRelId()
257 Description:
258 Get the channel object given its child relative id (ie channel id)
260 --*/
261 struct vmbus_channel *GetChannelFromRelId(u32 relId)
263 struct vmbus_channel *channel;
264 struct vmbus_channel *foundChannel = NULL;
265 LIST_ENTRY* anchor;
266 LIST_ENTRY* curr;
267 unsigned long flags;
269 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
270 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList)
272 channel = CONTAINING_RECORD(curr, struct vmbus_channel, ListEntry);
274 if (channel->OfferMsg.ChildRelId == relId)
276 foundChannel = channel;
277 break;
280 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
282 return foundChannel;
287 /*++
289 Name:
290 VmbusProcessChannelEvent()
292 Description:
293 Process a channel event notification
295 --*/
296 static void
297 VmbusProcessChannelEvent(
298 void * context
301 struct vmbus_channel *channel;
302 u32 relId = (u32)(unsigned long)context;
304 ASSERT(relId > 0);
307 * Find the channel based on this relid and invokes the
308 * channel callback to process the event
310 channel = GetChannelFromRelId(relId);
312 if (channel)
314 VmbusChannelOnChannelEvent(channel);
315 /* WorkQueueQueueWorkItem(channel->dataWorkQueue, VmbusChannelOnChannelEvent, (void*)channel); */
317 else
319 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
324 /*++
326 Name:
327 VmbusOnEvents()
329 Description:
330 Handler for events
332 --*/
333 void VmbusOnEvents(void)
335 int dword;
336 /* int maxdword = PAGE_SIZE >> 3; // receive size is 1/2 page and divide that by 4 bytes */
337 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
338 int bit;
339 int relid;
340 u32* recvInterruptPage = gVmbusConnection.RecvInterruptPage;
341 /* VMBUS_CHANNEL_MESSAGE* receiveMsg; */
343 DPRINT_ENTER(VMBUS);
345 /* Check events */
346 if (recvInterruptPage)
348 for (dword = 0; dword < maxdword; dword++)
350 if (recvInterruptPage[dword])
352 for (bit = 0; bit < 32; bit++)
354 if (test_and_clear_bit(bit, (unsigned long *) &recvInterruptPage[dword]))
356 relid = (dword << 5) + bit;
358 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
360 if (relid == 0) /* special case - vmbus channel protocol msg */
362 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
364 continue; }
365 else
367 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
368 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
369 VmbusProcessChannelEvent((void*)(unsigned long)relid);
376 DPRINT_EXIT(VMBUS);
378 return;
381 /*++
383 Name:
384 VmbusPostMessage()
386 Description:
387 Send a msg on the vmbus's message connection
389 --*/
390 int VmbusPostMessage(void *buffer, size_t bufferLen)
392 int ret=0;
393 HV_CONNECTION_ID connId;
396 connId.Asu32 =0;
397 connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
398 ret = HvPostMessage(
399 connId,
401 buffer,
402 bufferLen);
404 return ret;
407 /*++
409 Name:
410 VmbusSetEvent()
412 Description:
413 Send an event notification to the parent
415 --*/
416 int VmbusSetEvent(u32 childRelId)
418 int ret=0;
420 DPRINT_ENTER(VMBUS);
422 /* Each u32 represents 32 channels */
423 set_bit(childRelId & 31,
424 (unsigned long *) gVmbusConnection.SendInterruptPage + (childRelId >> 5));
426 ret = HvSignalEvent();
428 DPRINT_EXIT(VMBUS);
430 return ret;
433 /* EOF */