blob: 78889bdda9318d424b532ccd54eead01019d5c06 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
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
Greg Kroah-Hartman09d50ff2009-07-13 17:09:34 -070025#include "include/logging.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070026
27#include "VmbusPrivate.h"
28
29//
30// Globals
31//
32
33
34VMBUS_CONNECTION gVmbusConnection = {
35 .ConnectState = Disconnected,
36 .NextGpadlHandle = 0xE1E10,
37};
38
39
40/*++
41
42Name:
43 VmbusConnect()
44
45Description:
46 Sends a connect request on the partition service connection
47
48--*/
49int
50VmbusConnect(
51 )
52{
53 int ret=0;
54 VMBUS_CHANNEL_MSGINFO *msgInfo=NULL;
55 VMBUS_CHANNEL_INITIATE_CONTACT *msg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070056 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -070057
58 DPRINT_ENTER(VMBUS);
59
60 // Make sure we are not connecting or connected
61 if (gVmbusConnection.ConnectState != Disconnected)
62 return -1;
63
64 // Initialize the vmbus connection
65 gVmbusConnection.ConnectState = Connecting;
66 gVmbusConnection.WorkQueue = WorkQueueCreate("vmbusQ");
67
68 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelMsgList);
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070069 spin_lock_init(&gVmbusConnection.channelmsg_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070070
71 INITIALIZE_LIST_HEAD(&gVmbusConnection.ChannelList);
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -070072 spin_lock_init(&gVmbusConnection.channel_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070073
74 // Setup the vmbus event connection for channel interrupt abstraction stuff
75 gVmbusConnection.InterruptPage = PageAlloc(1);
76 if (gVmbusConnection.InterruptPage == NULL)
77 {
78 ret = -1;
79 goto Cleanup;
80 }
81
82 gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -070083 gVmbusConnection.SendInterruptPage = (void*)((unsigned long)gVmbusConnection.InterruptPage + (PAGE_SIZE >> 1));
Hank Janssen3e7ee492009-07-13 16:02:34 -070084
85 // Setup the monitor notification facility. The 1st page for parent->child and the 2nd page for child->parent
86 gVmbusConnection.MonitorPages = PageAlloc(2);
87 if (gVmbusConnection.MonitorPages == NULL)
88 {
89 ret = -1;
90 goto Cleanup;
91 }
92
Greg Kroah-Hartmane276a3a2009-07-15 12:47:43 -070093 msgInfo = kzalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_INITIATE_CONTACT), GFP_KERNEL);
Hank Janssen3e7ee492009-07-13 16:02:34 -070094 if (msgInfo == NULL)
95 {
96 ret = -1;
97 goto Cleanup;
98 }
99
100 msgInfo->WaitEvent = WaitEventCreate();
101 msg = (VMBUS_CHANNEL_INITIATE_CONTACT*)msgInfo->Msg;
102
103 msg->Header.MessageType = ChannelMessageInitiateContact;
104 msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER;
105 msg->InterruptPage = GetPhysicalAddress(gVmbusConnection.InterruptPage);
106 msg->MonitorPage1 = GetPhysicalAddress(gVmbusConnection.MonitorPages);
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700107 msg->MonitorPage2 = GetPhysicalAddress((void *)((unsigned long)gVmbusConnection.MonitorPages + PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700108
109 // Add to list before we send the request since we may receive the response
110 // before returning from this routine
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700111 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700112 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700113 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700114
115 DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, monitor1 pfn %llx,, monitor2 pfn %llx",
116 msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2);
117
118 DPRINT_DBG(VMBUS, "Sending channel initiate msg...");
119
120 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_INITIATE_CONTACT));
121 if (ret != 0)
122 {
123 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
124 goto Cleanup;
125 }
126
127 // Wait for the connection response
128 WaitEventWait(msgInfo->WaitEvent);
129
130 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
131
132 // Check if successful
133 if (msgInfo->Response.VersionResponse.VersionSupported)
134 {
135 DPRINT_INFO(VMBUS, "Vmbus connected!!");
136 gVmbusConnection.ConnectState = Connected;
137
138 }
139 else
140 {
141 DPRINT_ERR(VMBUS, "Vmbus connection failed!!...current version (%d) not supported", VMBUS_REVISION_NUMBER);
142 ret = -1;
143
144 goto Cleanup;
145 }
146
147
148 WaitEventClose(msgInfo->WaitEvent);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700149 kfree(msgInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700150 DPRINT_EXIT(VMBUS);
151
152 return 0;
153
154Cleanup:
155
156 gVmbusConnection.ConnectState = Disconnected;
157
158 WorkQueueClose(gVmbusConnection.WorkQueue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700159
160 if (gVmbusConnection.InterruptPage)
161 {
162 PageFree(gVmbusConnection.InterruptPage, 1);
163 gVmbusConnection.InterruptPage = NULL;
164 }
165
166 if (gVmbusConnection.MonitorPages)
167 {
168 PageFree(gVmbusConnection.MonitorPages, 2);
169 gVmbusConnection.MonitorPages = NULL;
170 }
171
172 if (msgInfo)
173 {
174 if (msgInfo->WaitEvent)
175 WaitEventClose(msgInfo->WaitEvent);
176
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700177 kfree(msgInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700178 }
179
180 DPRINT_EXIT(VMBUS);
181
182 return ret;
183}
184
185
186/*++
187
188Name:
189 VmbusDisconnect()
190
191Description:
192 Sends a disconnect request on the partition service connection
193
194--*/
195int
196VmbusDisconnect(
Greg Kroah-Hartmane20f6832009-07-14 15:07:21 -0700197 void
Hank Janssen3e7ee492009-07-13 16:02:34 -0700198 )
199{
200 int ret=0;
201 VMBUS_CHANNEL_UNLOAD *msg;
202
203 DPRINT_ENTER(VMBUS);
204
205 // Make sure we are connected
206 if (gVmbusConnection.ConnectState != Connected)
207 return -1;
208
Greg Kroah-Hartmane276a3a2009-07-15 12:47:43 -0700209 msg = kzalloc(sizeof(VMBUS_CHANNEL_UNLOAD), GFP_KERNEL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700210
211 msg->MessageType = ChannelMessageUnload;
212
213 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_UNLOAD));
214
215 if (ret != 0)
216 {
217 goto Cleanup;
218 }
219
220 PageFree(gVmbusConnection.InterruptPage, 1);
221
222 // TODO: iterate thru the msg list and free up
223
Hank Janssen3e7ee492009-07-13 16:02:34 -0700224 WorkQueueClose(gVmbusConnection.WorkQueue);
225
226 gVmbusConnection.ConnectState = Disconnected;
227
228 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
229
230Cleanup:
231 if (msg)
232 {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700233 kfree(msg);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700234 }
235
236 DPRINT_EXIT(VMBUS);
237
238 return ret;
239}
240
241
242/*++
243
244Name:
245 GetChannelFromRelId()
246
247Description:
248 Get the channel object given its child relative id (ie channel id)
249
250--*/
251VMBUS_CHANNEL*
252GetChannelFromRelId(
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700253 u32 relId
Hank Janssen3e7ee492009-07-13 16:02:34 -0700254 )
255{
256 VMBUS_CHANNEL* channel;
257 VMBUS_CHANNEL* foundChannel=NULL;
258 LIST_ENTRY* anchor;
259 LIST_ENTRY* curr;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700260 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700261
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700262 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700263 ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList)
264 {
265 channel = CONTAINING_RECORD(curr, VMBUS_CHANNEL, ListEntry);
266
267 if (channel->OfferMsg.ChildRelId == relId)
268 {
269 foundChannel = channel;
270 break;
271 }
272 }
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700273 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700274
275 return foundChannel;
276}
277
278
279
280/*++
281
282Name:
283 VmbusProcessChannelEvent()
284
285Description:
286 Process a channel event notification
287
288--*/
289static void
290VmbusProcessChannelEvent(
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700291 void * context
Hank Janssen3e7ee492009-07-13 16:02:34 -0700292 )
293{
294 VMBUS_CHANNEL* channel;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700295 u32 relId = (u32)(unsigned long)context;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700296
297 ASSERT(relId > 0);
298
299 // Find the channel based on this relid and invokes
300 // the channel callback to process the event
301 channel = GetChannelFromRelId(relId);
302
303 if (channel)
304 {
305 VmbusChannelOnChannelEvent(channel);
306 //WorkQueueQueueWorkItem(channel->dataWorkQueue, VmbusChannelOnChannelEvent, (void*)channel);
307 }
308 else
309 {
310 DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId);
311 }
312}
313
314
315/*++
316
317Name:
318 VmbusOnEvents()
319
320Description:
321 Handler for events
322
323--*/
Greg Kroah-Hartmane20f6832009-07-14 15:07:21 -0700324void
Hank Janssen3e7ee492009-07-13 16:02:34 -0700325VmbusOnEvents(
Greg Kroah-Hartmane20f6832009-07-14 15:07:21 -0700326 void
Hank Janssen3e7ee492009-07-13 16:02:34 -0700327 )
328{
329 int dword;
330 //int maxdword = PAGE_SIZE >> 3; // receive size is 1/2 page and divide that by 4 bytes
331 int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5;
332 int bit;
333 int relid;
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700334 u32* recvInterruptPage = gVmbusConnection.RecvInterruptPage;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700335 //VMBUS_CHANNEL_MESSAGE* receiveMsg;
336
337 DPRINT_ENTER(VMBUS);
338
339 // Check events
340 if (recvInterruptPage)
341 {
342 for (dword = 0; dword < maxdword; dword++)
343 {
344 if (recvInterruptPage[dword])
345 {
346 for (bit = 0; bit < 32; bit++)
347 {
348 if (BitTestAndClear(&recvInterruptPage[dword], bit))
349 {
350 relid = (dword << 5) + bit;
351
352 DPRINT_DBG(VMBUS, "event detected for relid - %d", relid);
353
354 if (relid == 0) // special case - vmbus channel protocol msg
355 {
356 DPRINT_DBG(VMBUS, "invalid relid - %d", relid);
357
358 continue; }
359 else
360 {
361 //QueueWorkItem(VmbusProcessEvent, (void*)relid);
362 //ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid);
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700363 VmbusProcessChannelEvent((void*)(unsigned long)relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700364 }
365 }
366 }
367 }
368 }
369 }
370 DPRINT_EXIT(VMBUS);
371
372 return;
373}
374
375/*++
376
377Name:
378 VmbusPostMessage()
379
380Description:
381 Send a msg on the vmbus's message connection
382
383--*/
384int
385VmbusPostMessage(
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700386 void * buffer,
Greg Kroah-Hartman45635d92009-07-14 15:14:20 -0700387 size_t bufferLen
Hank Janssen3e7ee492009-07-13 16:02:34 -0700388 )
389{
390 int ret=0;
391 HV_CONNECTION_ID connId;
392
393
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700394 connId.Asu32 =0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700395 connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID;
396 ret = HvPostMessage(
397 connId,
398 1,
399 buffer,
400 bufferLen);
401
402 return ret;
403}
404
405/*++
406
407Name:
408 VmbusSetEvent()
409
410Description:
411 Send an event notification to the parent
412
413--*/
414int
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700415VmbusSetEvent(u32 childRelId)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700416{
417 int ret=0;
418
419 DPRINT_ENTER(VMBUS);
420
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700421 // Each u32 represents 32 channels
422 BitSet((u32*)gVmbusConnection.SendInterruptPage + (childRelId >> 5), childRelId & 31);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700423 ret = HvSignalEvent();
424
425 DPRINT_EXIT(VMBUS);
426
427 return ret;
428}
429
430// EOF