block: autoconvert trivial BKL users to private mutex
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / connection.c
blob1f4d6683aaa7e15bd716c25ac26c1eee0e30c092
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 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include "osd.h"
28 #include "logging.h"
29 #include "vmbus_private.h"
32 struct VMBUS_CONNECTION gVmbusConnection = {
33 .ConnectState = Disconnected,
34 .NextGpadlHandle = ATOMIC_INIT(0xE1E10),
38 * VmbusConnect - Sends a connect request on the partition service connection
40 int VmbusConnect(void)
42 int ret = 0;
43 struct vmbus_channel_msginfo *msgInfo = NULL;
44 struct vmbus_channel_initiate_contact *msg;
45 unsigned long flags;
47 /* Make sure we are not connecting or connected */
48 if (gVmbusConnection.ConnectState != Disconnected)
49 return -1;
51 /* Initialize the vmbus connection */
52 gVmbusConnection.ConnectState = Connecting;
53 gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
54 if (!gVmbusConnection.WorkQueue) {
55 ret = -1;
56 goto Cleanup;
59 INIT_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
60 spin_lock_init(&gVmbusConnection.channelmsg_lock);
62 INIT_LIST_HEAD(&gVmbusConnection.ChannelList);
63 spin_lock_init(&gVmbusConnection.channel_lock);
66 * Setup the vmbus event connection for channel interrupt
67 * abstraction stuff
69 gVmbusConnection.InterruptPage = osd_PageAlloc(1);
70 if (gVmbusConnection.InterruptPage == NULL) {
71 ret = -1;
72 goto Cleanup;
75 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
76 gVmbusConnection.SendInterruptPage =
77 (void *)((unsigned long)gVmbusConnection.InterruptPage +
78 (PAGE_SIZE >> 1));
81 * Setup the monitor notification facility. The 1st page for
82 * parent->child and the 2nd page for child->parent
84 gVmbusConnection.MonitorPages = osd_PageAlloc(2);
85 if (gVmbusConnection.MonitorPages == NULL) {
86 ret = -1;
87 goto Cleanup;
90 msgInfo = kzalloc(sizeof(*msgInfo) +
91 sizeof(struct vmbus_channel_initiate_contact),
92 GFP_KERNEL);
93 if (msgInfo == NULL) {
94 ret = -ENOMEM;
95 goto Cleanup;
98 msgInfo->WaitEvent = osd_WaitEventCreate();
99 if (!msgInfo->WaitEvent) {
100 ret = -ENOMEM;
101 goto Cleanup;
104 msg = (struct vmbus_channel_initiate_contact *)msgInfo->Msg;
106 msg->Header.MessageType = ChannelMessageInitiateContact;
107 msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
108 msg->InterruptPage = virt_to_phys(gVmbusConnection.InterruptPage);
109 msg->MonitorPage1 = virt_to_phys(gVmbusConnection.MonitorPages);
110 msg->MonitorPage2 = virt_to_phys(
111 (void *)((unsigned long)gVmbusConnection.MonitorPages +
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(&gVmbusConnection.channelmsg_lock, flags);
119 list_add_tail(&msgInfo->MsgListEntry,
120 &gVmbusConnection.ChannelMsgList);
122 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
124 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, "
125 "monitor1 pfn %llx,, monitor2 pfn %llx",
126 msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
128 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
129 ret = VmbusPostMessage(msg,
130 sizeof(struct vmbus_channel_initiate_contact));
131 if (ret != 0) {
132 list_del(&msgInfo->MsgListEntry);
133 goto Cleanup;
136 /* Wait for the connection response */
137 osd_WaitEventWait(msgInfo->WaitEvent);
139 list_del(&msgInfo->MsgListEntry);
141 /* Check if successful */
142 if (msgInfo->Response.VersionResponse.VersionSupported) {
143 DPRINT_INFO(VMBUS, "Vmbus connected!!");
144 gVmbusConnection.ConnectState = Connected;
146 } else {
147 DPRINT_ERR(VMBUS, "Vmbus connection failed!!..."
148 "current version (%d) not supported",
149 VMBUS_REVISION_NUMBER);
150 ret = -1;
151 goto Cleanup;
154 kfree(msgInfo->WaitEvent);
155 kfree(msgInfo);
156 return 0;
158 Cleanup:
159 gVmbusConnection.ConnectState = Disconnected;
161 if (gVmbusConnection.WorkQueue)
162 destroy_workqueue(gVmbusConnection.WorkQueue);
164 if (gVmbusConnection.InterruptPage) {
165 osd_PageFree(gVmbusConnection.InterruptPage, 1);
166 gVmbusConnection.InterruptPage = NULL;
169 if (gVmbusConnection.MonitorPages) {
170 osd_PageFree(gVmbusConnection.MonitorPages, 2);
171 gVmbusConnection.MonitorPages = NULL;
174 if (msgInfo) {
175 kfree(msgInfo->WaitEvent);
176 kfree(msgInfo);
179 return ret;
183 * VmbusDisconnect - Sends a disconnect request on the partition service connection
185 int VmbusDisconnect(void)
187 int ret = 0;
188 struct vmbus_channel_message_header *msg;
190 /* Make sure we are connected */
191 if (gVmbusConnection.ConnectState != Connected)
192 return -1;
194 msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
195 if (!msg)
196 return -ENOMEM;
198 msg->MessageType = ChannelMessageUnload;
200 ret = VmbusPostMessage(msg,
201 sizeof(struct vmbus_channel_message_header));
202 if (ret != 0)
203 goto Cleanup;
205 osd_PageFree(gVmbusConnection.InterruptPage, 1);
207 /* TODO: iterate thru the msg list and free up */
208 destroy_workqueue(gVmbusConnection.WorkQueue);
210 gVmbusConnection.ConnectState = Disconnected;
212 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
214 Cleanup:
215 kfree(msg);
216 return ret;
220 * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
222 struct vmbus_channel *GetChannelFromRelId(u32 relId)
224 struct vmbus_channel *channel;
225 struct vmbus_channel *foundChannel = NULL;
226 unsigned long flags;
228 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
229 list_for_each_entry(channel, &gVmbusConnection.ChannelList, ListEntry) {
230 if (channel->OfferMsg.ChildRelId == relId) {
231 foundChannel = channel;
232 break;
235 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
237 return foundChannel;
241 * VmbusProcessChannelEvent - Process a channel event notification
243 static void VmbusProcessChannelEvent(void *context)
245 struct vmbus_channel *channel;
246 u32 relId = (u32)(unsigned long)context;
248 /* ASSERT(relId > 0); */
251 * Find the channel based on this relid and invokes the
252 * channel callback to process the event
254 channel = GetChannelFromRelId(relId);
256 if (channel) {
257 VmbusChannelOnChannelEvent(channel);
259 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
260 * VmbusChannelOnChannelEvent,
261 * (void*)channel);
263 } else {
264 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
269 * VmbusOnEvents - Handler for events
271 void VmbusOnEvents(void)
273 int dword;
274 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
275 int bit;
276 int relid;
277 u32 *recvInterruptPage = gVmbusConnection.RecvInterruptPage;
279 /* Check events */
280 if (recvInterruptPage) {
281 for (dword = 0; dword < maxdword; dword++) {
282 if (recvInterruptPage[dword]) {
283 for (bit = 0; bit < 32; bit++) {
284 if (test_and_clear_bit(bit, (unsigned long *)&recvInterruptPage[dword])) {
285 relid = (dword << 5) + bit;
286 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
288 if (relid == 0) {
289 /* special case - vmbus channel protocol msg */
290 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
291 continue;
292 } else {
293 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
294 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
295 VmbusProcessChannelEvent((void *)(unsigned long)relid);
302 return;
306 * VmbusPostMessage - Send a msg on the vmbus's message connection
308 int VmbusPostMessage(void *buffer, size_t bufferLen)
310 union hv_connection_id connId;
312 connId.Asu32 = 0;
313 connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
314 return HvPostMessage(connId, 1, buffer, bufferLen);
318 * VmbusSetEvent - Send an event notification to the parent
320 int VmbusSetEvent(u32 childRelId)
322 /* Each u32 represents 32 channels */
323 set_bit(childRelId & 31,
324 (unsigned long *)gVmbusConnection.SendInterruptPage +
325 (childRelId >> 5));
327 return HvSignalEvent();