blob: 13e164d1453e6fb657e8a2e6ff0e1dc6348a6ab6 [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);
72 gVmbusConnection.ChannelLock = SpinlockCreate();
73
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);
159 SpinlockClose(gVmbusConnection.ChannelLock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700160
161 if (gVmbusConnection.InterruptPage)
162 {
163 PageFree(gVmbusConnection.InterruptPage, 1);
164 gVmbusConnection.InterruptPage = NULL;
165 }
166
167 if (gVmbusConnection.MonitorPages)
168 {
169 PageFree(gVmbusConnection.MonitorPages, 2);
170 gVmbusConnection.MonitorPages = NULL;
171 }
172
173 if (msgInfo)
174 {
175 if (msgInfo->WaitEvent)
176 WaitEventClose(msgInfo->WaitEvent);
177
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700178 kfree(msgInfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700179 }
180
181 DPRINT_EXIT(VMBUS);
182
183 return ret;
184}
185
186
187/*++
188
189Name:
190 VmbusDisconnect()
191
192Description:
193 Sends a disconnect request on the partition service connection
194
195--*/
196int
197VmbusDisconnect(
Greg Kroah-Hartmane20f6832009-07-14 15:07:21 -0700198 void
Hank Janssen3e7ee492009-07-13 16:02:34 -0700199 )
200{
201 int ret=0;
202 VMBUS_CHANNEL_UNLOAD *msg;
203
204 DPRINT_ENTER(VMBUS);
205
206 // Make sure we are connected
207 if (gVmbusConnection.ConnectState != Connected)
208 return -1;
209
Greg Kroah-Hartmane276a3a2009-07-15 12:47:43 -0700210 msg = kzalloc(sizeof(VMBUS_CHANNEL_UNLOAD), GFP_KERNEL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700211
212 msg->MessageType = ChannelMessageUnload;
213
214 ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_UNLOAD));
215
216 if (ret != 0)
217 {
218 goto Cleanup;
219 }
220
221 PageFree(gVmbusConnection.InterruptPage, 1);
222
223 // TODO: iterate thru the msg list and free up
224
Hank Janssen3e7ee492009-07-13 16:02:34 -0700225 WorkQueueClose(gVmbusConnection.WorkQueue);
226
227 gVmbusConnection.ConnectState = Disconnected;
228
229 DPRINT_INFO(VMBUS, "Vmbus disconnected!!");
230
231Cleanup:
232 if (msg)
233 {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700234 kfree(msg);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700235 }
236
237 DPRINT_EXIT(VMBUS);
238
239 return ret;
240}
241
242
243/*++
244
245Name:
246 GetChannelFromRelId()
247
248Description:
249 Get the channel object given its child relative id (ie channel id)
250
251--*/
252VMBUS_CHANNEL*
253GetChannelFromRelId(
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -0700254 u32 relId
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255 )
256{
257 VMBUS_CHANNEL* channel;
258 VMBUS_CHANNEL* foundChannel=NULL;
259 LIST_ENTRY* anchor;
260 LIST_ENTRY* curr;
261
262 SpinlockAcquire(gVmbusConnection.ChannelLock);
263 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 }
273 SpinlockRelease(gVmbusConnection.ChannelLock);
274
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