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
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.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
25 #include "include/logging.h"
27 #include "VmbusPrivate.h"
32 struct VMBUS_CONNECTION gVmbusConnection
= {
33 .ConnectState
= Disconnected
,
34 .NextGpadlHandle
= 0xE1E10,
44 Sends a connect request on the partition service connection
51 VMBUS_CHANNEL_MSGINFO
*msgInfo
=NULL
;
52 VMBUS_CHANNEL_INITIATE_CONTACT
*msg
;
57 /* Make sure we are not connecting or connected */
58 if (gVmbusConnection
.ConnectState
!= Disconnected
)
61 /* Initialize the vmbus connection */
62 gVmbusConnection
.ConnectState
= Connecting
;
63 gVmbusConnection
.WorkQueue
= WorkQueueCreate("vmbusQ");
65 INITIALIZE_LIST_HEAD(&gVmbusConnection
.ChannelMsgList
);
66 spin_lock_init(&gVmbusConnection
.channelmsg_lock
);
68 INITIALIZE_LIST_HEAD(&gVmbusConnection
.ChannelList
);
69 spin_lock_init(&gVmbusConnection
.channel_lock
);
72 * Setup the vmbus event connection for channel interrupt
75 gVmbusConnection
.InterruptPage
= PageAlloc(1);
76 if (gVmbusConnection
.InterruptPage
== NULL
)
82 gVmbusConnection
.RecvInterruptPage
= gVmbusConnection
.InterruptPage
;
83 gVmbusConnection
.SendInterruptPage
= (void*)((unsigned long)gVmbusConnection
.InterruptPage
+ (PAGE_SIZE
>> 1));
86 * notification facility. The 1st page for parent->child and
87 * the 2nd page for child->parent
89 gVmbusConnection
.MonitorPages
= PageAlloc(2);
90 if (gVmbusConnection
.MonitorPages
== NULL
)
96 msgInfo
= kzalloc(sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_INITIATE_CONTACT
), GFP_KERNEL
);
103 msgInfo
->WaitEvent
= WaitEventCreate();
104 msg
= (VMBUS_CHANNEL_INITIATE_CONTACT
*)msgInfo
->Msg
;
106 msg
->Header
.MessageType
= ChannelMessageInitiateContact
;
107 msg
->VMBusVersionRequested
= VMBUS_REVISION_NUMBER
;
108 msg
->InterruptPage
= GetPhysicalAddress(gVmbusConnection
.InterruptPage
);
109 msg
->MonitorPage1
= GetPhysicalAddress(gVmbusConnection
.MonitorPages
);
110 msg
->MonitorPage2
= GetPhysicalAddress((void *)((unsigned long)gVmbusConnection
.MonitorPages
+ PAGE_SIZE
));
113 * Add to list before we send the request since we may
114 * receive the response before returning from this routine
116 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
117 INSERT_TAIL_LIST(&gVmbusConnection
.ChannelMsgList
, &msgInfo
->MsgListEntry
);
118 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
120 DPRINT_DBG(VMBUS
, "Vmbus connection - interrupt pfn %llx, monitor1 pfn %llx,, monitor2 pfn %llx",
121 msg
->InterruptPage
, msg
->MonitorPage1
, msg
->MonitorPage2
);
123 DPRINT_DBG(VMBUS
, "Sending channel initiate msg...");
125 ret
= VmbusPostMessage(msg
, sizeof(VMBUS_CHANNEL_INITIATE_CONTACT
));
128 REMOVE_ENTRY_LIST(&msgInfo
->MsgListEntry
);
132 /* Wait for the connection response */
133 WaitEventWait(msgInfo
->WaitEvent
);
135 REMOVE_ENTRY_LIST(&msgInfo
->MsgListEntry
);
137 /* Check if successful */
138 if (msgInfo
->Response
.VersionResponse
.VersionSupported
)
140 DPRINT_INFO(VMBUS
, "Vmbus connected!!");
141 gVmbusConnection
.ConnectState
= Connected
;
146 DPRINT_ERR(VMBUS
, "Vmbus connection failed!!...current version (%d) not supported", VMBUS_REVISION_NUMBER
);
153 WaitEventClose(msgInfo
->WaitEvent
);
161 gVmbusConnection
.ConnectState
= Disconnected
;
163 WorkQueueClose(gVmbusConnection
.WorkQueue
);
165 if (gVmbusConnection
.InterruptPage
)
167 PageFree(gVmbusConnection
.InterruptPage
, 1);
168 gVmbusConnection
.InterruptPage
= NULL
;
171 if (gVmbusConnection
.MonitorPages
)
173 PageFree(gVmbusConnection
.MonitorPages
, 2);
174 gVmbusConnection
.MonitorPages
= NULL
;
179 if (msgInfo
->WaitEvent
)
180 WaitEventClose(msgInfo
->WaitEvent
);
197 Sends a disconnect request on the partition service connection
206 VMBUS_CHANNEL_UNLOAD
*msg
;
210 /* Make sure we are connected */
211 if (gVmbusConnection
.ConnectState
!= Connected
)
214 msg
= kzalloc(sizeof(VMBUS_CHANNEL_UNLOAD
), GFP_KERNEL
);
216 msg
->MessageType
= ChannelMessageUnload
;
218 ret
= VmbusPostMessage(msg
, sizeof(VMBUS_CHANNEL_UNLOAD
));
225 PageFree(gVmbusConnection
.InterruptPage
, 1);
227 /* TODO: iterate thru the msg list and free up */
229 WorkQueueClose(gVmbusConnection
.WorkQueue
);
231 gVmbusConnection
.ConnectState
= Disconnected
;
233 DPRINT_INFO(VMBUS
, "Vmbus disconnected!!");
250 GetChannelFromRelId()
253 Get the channel object given its child relative id (ie channel id)
256 static VMBUS_CHANNEL
*
261 VMBUS_CHANNEL
* channel
;
262 VMBUS_CHANNEL
* foundChannel
=NULL
;
267 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
268 ITERATE_LIST_ENTRIES(anchor
, curr
, &gVmbusConnection
.ChannelList
)
270 channel
= CONTAINING_RECORD(curr
, VMBUS_CHANNEL
, ListEntry
);
272 if (channel
->OfferMsg
.ChildRelId
== relId
)
274 foundChannel
= channel
;
278 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
288 VmbusProcessChannelEvent()
291 Process a channel event notification
295 VmbusProcessChannelEvent(
299 VMBUS_CHANNEL
* channel
;
300 u32 relId
= (u32
)(unsigned long)context
;
305 * Find the channel based on this relid and invokes the
306 * channel callback to process the event
308 channel
= GetChannelFromRelId(relId
);
312 VmbusChannelOnChannelEvent(channel
);
313 /* WorkQueueQueueWorkItem(channel->dataWorkQueue, VmbusChannelOnChannelEvent, (void*)channel); */
317 DPRINT_ERR(VMBUS
, "channel not found for relid - %d.", relId
);
337 /* int maxdword = PAGE_SIZE >> 3; // receive size is 1/2 page and divide that by 4 bytes */
338 int maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
341 u32
* recvInterruptPage
= gVmbusConnection
.RecvInterruptPage
;
342 /* VMBUS_CHANNEL_MESSAGE* receiveMsg; */
347 if (recvInterruptPage
)
349 for (dword
= 0; dword
< maxdword
; dword
++)
351 if (recvInterruptPage
[dword
])
353 for (bit
= 0; bit
< 32; bit
++)
355 if (BitTestAndClear(&recvInterruptPage
[dword
], bit
))
357 relid
= (dword
<< 5) + bit
;
359 DPRINT_DBG(VMBUS
, "event detected for relid - %d", relid
);
361 if (relid
== 0) /* special case - vmbus channel protocol msg */
363 DPRINT_DBG(VMBUS
, "invalid relid - %d", relid
);
368 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
369 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
370 VmbusProcessChannelEvent((void*)(unsigned long)relid
);
388 Send a msg on the vmbus's message connection
398 HV_CONNECTION_ID connId
;
402 connId
.u
.Id
= VMBUS_MESSAGE_CONNECTION_ID
;
418 Send an event notification to the parent
422 VmbusSetEvent(u32 childRelId
)
428 /* Each u32 represents 32 channels */
429 BitSet((u32
*)gVmbusConnection
.SendInterruptPage
+ (childRelId
>> 5), childRelId
& 31);
430 ret
= HvSignalEvent();