Staging: hv: remove function pointer typedefs from vmbus.h
[safe/jmp/linux-2.6] / drivers / staging / 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 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/vmalloc.h>
26 #include "osd.h"
27 #include "logging.h"
28 #include "VmbusPrivate.h"
29
30
31 struct VMBUS_CONNECTION gVmbusConnection = {
32         .ConnectState           = Disconnected,
33         .NextGpadlHandle        = ATOMIC_INIT(0xE1E10),
34 };
35
36 /**
37  * VmbusConnect - Sends a connect request on the partition service connection
38  */
39 int VmbusConnect(void)
40 {
41         int ret = 0;
42         struct vmbus_channel_msginfo *msgInfo = NULL;
43         struct vmbus_channel_initiate_contact *msg;
44         unsigned long flags;
45
46         DPRINT_ENTER(VMBUS);
47
48         /* Make sure we are not connecting or connected */
49         if (gVmbusConnection.ConnectState != Disconnected)
50                 return -1;
51
52         /* Initialize the vmbus connection */
53         gVmbusConnection.ConnectState = Connecting;
54         gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con");
55         if (!gVmbusConnection.WorkQueue) {
56                 ret = -1;
57                 goto Cleanup;
58         }
59
60         INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
61         spin_lock_init(&gVmbusConnection.channelmsg_lock);
62
63         INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelList);
64         spin_lock_init(&gVmbusConnection.channel_lock);
65
66         /*
67          * Setup the vmbus event connection for channel interrupt
68          * abstraction stuff
69          */
70         gVmbusConnection.InterruptPage = osd_PageAlloc(1);
71         if (gVmbusConnection.InterruptPage == NULL) {
72                 ret = -1;
73                 goto Cleanup;
74         }
75
76         gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
77         gVmbusConnection.SendInterruptPage =
78                 (void *)((unsigned long)gVmbusConnection.InterruptPage +
79                         (PAGE_SIZE >> 1));
80
81         /*
82          * Setup the monitor notification facility. The 1st page for
83          * parent->child and the 2nd page for child->parent
84          */
85         gVmbusConnection.MonitorPages = osd_PageAlloc(2);
86         if (gVmbusConnection.MonitorPages == NULL) {
87                 ret = -1;
88                 goto Cleanup;
89         }
90
91         msgInfo = kzalloc(sizeof(*msgInfo) +
92                           sizeof(struct vmbus_channel_initiate_contact),
93                           GFP_KERNEL);
94         if (msgInfo == NULL) {
95                 ret = -1;
96                 goto Cleanup;
97         }
98
99         msgInfo->WaitEvent = osd_WaitEventCreate();
100         msg = (struct vmbus_channel_initiate_contact *)msgInfo->Msg;
101
102         msg->Header.MessageType = ChannelMessageInitiateContact;
103         msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
104         msg->InterruptPage = virt_to_phys(gVmbusConnection.InterruptPage);
105         msg->MonitorPage1 = virt_to_phys(gVmbusConnection.MonitorPages);
106         msg->MonitorPage2 = virt_to_phys(
107                         (void *)((unsigned long)gVmbusConnection.MonitorPages +
108                                  PAGE_SIZE));
109
110         /*
111          * Add to list before we send the request since we may
112          * receive the response before returning from this routine
113          */
114         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
115         INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList,
116                          &msgInfo->MsgListEntry);
117         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
118
119         DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, "
120                    "monitor1 pfn %llx,, monitor2 pfn %llx",
121                    msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
122
123         DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
124         ret = VmbusPostMessage(msg,
125                                sizeof(struct vmbus_channel_initiate_contact));
126         if (ret != 0) {
127                 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
128                 goto Cleanup;
129         }
130
131         /* Wait for the connection response */
132         osd_WaitEventWait(msgInfo->WaitEvent);
133
134         REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
135
136         /* Check if successful */
137         if (msgInfo->Response.VersionResponse.VersionSupported) {
138                 DPRINT_INFO(VMBUS, "Vmbus connected!!");
139                 gVmbusConnection.ConnectState = Connected;
140
141         } else {
142                 DPRINT_ERR(VMBUS, "Vmbus connection failed!!..."
143                            "current version (%d) not supported",
144                            VMBUS_REVISION_NUMBER);
145                 ret = -1;
146                 goto Cleanup;
147         }
148
149         kfree(msgInfo->WaitEvent);
150         kfree(msgInfo);
151         DPRINT_EXIT(VMBUS);
152
153         return 0;
154
155 Cleanup:
156         gVmbusConnection.ConnectState = Disconnected;
157
158         if (gVmbusConnection.WorkQueue)
159                 destroy_workqueue(gVmbusConnection.WorkQueue);
160
161         if (gVmbusConnection.InterruptPage) {
162                 osd_PageFree(gVmbusConnection.InterruptPage, 1);
163                 gVmbusConnection.InterruptPage = NULL;
164         }
165
166         if (gVmbusConnection.MonitorPages) {
167                 osd_PageFree(gVmbusConnection.MonitorPages, 2);
168                 gVmbusConnection.MonitorPages = NULL;
169         }
170
171         if (msgInfo) {
172                 kfree(msgInfo->WaitEvent);
173                 kfree(msgInfo);
174         }
175
176         DPRINT_EXIT(VMBUS);
177
178         return ret;
179 }
180
181 /**
182  * VmbusDisconnect - Sends a disconnect request on the partition service connection
183  */
184 int VmbusDisconnect(void)
185 {
186         int ret = 0;
187         struct vmbus_channel_message_header *msg;
188
189         DPRINT_ENTER(VMBUS);
190
191         /* Make sure we are connected */
192         if (gVmbusConnection.ConnectState != Connected)
193                 return -1;
194
195         msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL);
196
197         msg->MessageType = ChannelMessageUnload;
198
199         ret = VmbusPostMessage(msg,
200                                sizeof(struct vmbus_channel_message_header));
201         if (ret != 0)
202                 goto Cleanup;
203
204         osd_PageFree(gVmbusConnection.InterruptPage, 1);
205
206         /* TODO: iterate thru the msg list and free up */
207         destroy_workqueue(gVmbusConnection.WorkQueue);
208
209         gVmbusConnection.ConnectState = Disconnected;
210
211         DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
212
213 Cleanup:
214         kfree(msg);
215         DPRINT_EXIT(VMBUS);
216         return ret;
217 }
218
219 /**
220  * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
221  */
222 struct vmbus_channel *GetChannelFromRelId(u32 relId)
223 {
224         struct vmbus_channel *channel;
225         struct vmbus_channel *foundChannel  = NULL;
226         LIST_ENTRY *anchor;
227         LIST_ENTRY *curr;
228         unsigned long flags;
229
230         spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
231         ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList) {
232                 channel = CONTAINING_RECORD(curr, struct vmbus_channel,
233                                             ListEntry);
234
235                 if (channel->OfferMsg.ChildRelId == relId) {
236                         foundChannel = channel;
237                         break;
238                 }
239         }
240         spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
241
242         return foundChannel;
243 }
244
245 /**
246  * VmbusProcessChannelEvent - Process a channel event notification
247  */
248 static void VmbusProcessChannelEvent(void *context)
249 {
250         struct vmbus_channel *channel;
251         u32 relId = (u32)(unsigned long)context;
252
253         ASSERT(relId > 0);
254
255         /*
256          * Find the channel based on this relid and invokes the
257          * channel callback to process the event
258          */
259         channel = GetChannelFromRelId(relId);
260
261         if (channel) {
262                 VmbusChannelOnChannelEvent(channel);
263                 /*
264                  * WorkQueueQueueWorkItem(channel->dataWorkQueue,
265                  *                        VmbusChannelOnChannelEvent,
266                  *                        (void*)channel);
267                  */
268         } else {
269                 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
270         }
271 }
272
273 /**
274  * VmbusOnEvents - Handler for events
275  */
276 void VmbusOnEvents(void)
277 {
278         int dword;
279         int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
280         int bit;
281         int relid;
282         u32 *recvInterruptPage = gVmbusConnection.RecvInterruptPage;
283
284         DPRINT_ENTER(VMBUS);
285
286         /* Check events */
287         if (recvInterruptPage) {
288                 for (dword = 0; dword < maxdword; dword++) {
289                         if (recvInterruptPage[dword]) {
290                                 for (bit = 0; bit < 32; bit++) {
291                                         if (test_and_clear_bit(bit, (unsigned long *)&recvInterruptPage[dword])) {
292                                                 relid = (dword << 5) + bit;
293                                                 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
294
295                                                 if (relid == 0) {
296                                                         /* special case - vmbus channel protocol msg */
297                                                         DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
298                                                         continue;
299                                                 } else {
300                                                         /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
301                                                         /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
302                                                         VmbusProcessChannelEvent((void *)(unsigned long)relid);
303                                                 }
304                                         }
305                                 }
306                         }
307                  }
308         }
309         DPRINT_EXIT(VMBUS);
310
311         return;
312 }
313
314 /**
315  * VmbusPostMessage - Send a msg on the vmbus's message connection
316  */
317 int VmbusPostMessage(void *buffer, size_t bufferLen)
318 {
319         union hv_connection_id connId;
320
321         connId.Asu32 = 0;
322         connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
323         return HvPostMessage(connId, 1, buffer, bufferLen);
324 }
325
326 /**
327  * VmbusSetEvent - Send an event notification to the parent
328  */
329 int VmbusSetEvent(u32 childRelId)
330 {
331         int ret = 0;
332
333         DPRINT_ENTER(VMBUS);
334
335         /* Each u32 represents 32 channels */
336         set_bit(childRelId & 31,
337                 (unsigned long *)gVmbusConnection.SendInterruptPage +
338                 (childRelId >> 5));
339
340         ret = HvSignalEvent();
341
342         DPRINT_EXIT(VMBUS);
343
344         return ret;
345 }