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