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>
23 #include <linux/kernel.h>
25 #include <linux/vmalloc.h>
28 #include "VmbusPrivate.h"
31 struct VMBUS_CONNECTION gVmbusConnection
= {
32 .ConnectState
= Disconnected
,
33 .NextGpadlHandle
= ATOMIC_INIT(0xE1E10),
37 * VmbusConnect - Sends a connect request on the partition service connection
39 int VmbusConnect(void)
42 struct vmbus_channel_msginfo
*msgInfo
= NULL
;
43 struct vmbus_channel_initiate_contact
*msg
;
48 /* Make sure we are not connecting or connected */
49 if (gVmbusConnection
.ConnectState
!= Disconnected
)
52 /* Initialize the vmbus connection */
53 gVmbusConnection
.ConnectState
= Connecting
;
54 gVmbusConnection
.WorkQueue
= create_workqueue("hv_vmbus_con");
55 if (!gVmbusConnection
.WorkQueue
) {
60 INIT_LIST_HEAD(&gVmbusConnection
.ChannelMsgList
);
61 spin_lock_init(&gVmbusConnection
.channelmsg_lock
);
63 INIT_LIST_HEAD(&gVmbusConnection
.ChannelList
);
64 spin_lock_init(&gVmbusConnection
.channel_lock
);
67 * Setup the vmbus event connection for channel interrupt
70 gVmbusConnection
.InterruptPage
= osd_PageAlloc(1);
71 if (gVmbusConnection
.InterruptPage
== NULL
) {
76 gVmbusConnection
.RecvInterruptPage
= gVmbusConnection
.InterruptPage
;
77 gVmbusConnection
.SendInterruptPage
=
78 (void *)((unsigned long)gVmbusConnection
.InterruptPage
+
82 * Setup the monitor notification facility. The 1st page for
83 * parent->child and the 2nd page for child->parent
85 gVmbusConnection
.MonitorPages
= osd_PageAlloc(2);
86 if (gVmbusConnection
.MonitorPages
== NULL
) {
91 msgInfo
= kzalloc(sizeof(*msgInfo
) +
92 sizeof(struct vmbus_channel_initiate_contact
),
94 if (msgInfo
== NULL
) {
99 msgInfo
->WaitEvent
= osd_WaitEventCreate();
100 msg
= (struct vmbus_channel_initiate_contact
*)msgInfo
->Msg
;
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
+
111 * Add to list before we send the request since we may
112 * receive the response before returning from this routine
114 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
115 list_add_tail(&msgInfo
->MsgListEntry
,
116 &gVmbusConnection
.ChannelMsgList
);
118 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
120 DPRINT_DBG(VMBUS
, "Vmbus connection - interrupt pfn %llx, "
121 "monitor1 pfn %llx,, monitor2 pfn %llx",
122 msg
->InterruptPage
, msg
->MonitorPage1
, msg
->MonitorPage2
);
124 DPRINT_DBG(VMBUS
, "Sending channel initiate msg...");
125 ret
= VmbusPostMessage(msg
,
126 sizeof(struct vmbus_channel_initiate_contact
));
128 list_del(&msgInfo
->MsgListEntry
);
132 /* Wait for the connection response */
133 osd_WaitEventWait(msgInfo
->WaitEvent
);
135 list_del(&msgInfo
->MsgListEntry
);
137 /* Check if successful */
138 if (msgInfo
->Response
.VersionResponse
.VersionSupported
) {
139 DPRINT_INFO(VMBUS
, "Vmbus connected!!");
140 gVmbusConnection
.ConnectState
= Connected
;
143 DPRINT_ERR(VMBUS
, "Vmbus connection failed!!..."
144 "current version (%d) not supported",
145 VMBUS_REVISION_NUMBER
);
150 kfree(msgInfo
->WaitEvent
);
157 gVmbusConnection
.ConnectState
= Disconnected
;
159 if (gVmbusConnection
.WorkQueue
)
160 destroy_workqueue(gVmbusConnection
.WorkQueue
);
162 if (gVmbusConnection
.InterruptPage
) {
163 osd_PageFree(gVmbusConnection
.InterruptPage
, 1);
164 gVmbusConnection
.InterruptPage
= NULL
;
167 if (gVmbusConnection
.MonitorPages
) {
168 osd_PageFree(gVmbusConnection
.MonitorPages
, 2);
169 gVmbusConnection
.MonitorPages
= NULL
;
173 kfree(msgInfo
->WaitEvent
);
183 * VmbusDisconnect - Sends a disconnect request on the partition service connection
185 int VmbusDisconnect(void)
188 struct vmbus_channel_message_header
*msg
;
192 /* Make sure we are connected */
193 if (gVmbusConnection
.ConnectState
!= Connected
)
196 msg
= kzalloc(sizeof(struct vmbus_channel_message_header
), GFP_KERNEL
);
198 msg
->MessageType
= ChannelMessageUnload
;
200 ret
= VmbusPostMessage(msg
,
201 sizeof(struct vmbus_channel_message_header
));
205 osd_PageFree(gVmbusConnection
.InterruptPage
, 1);
207 /* TODO: iterate thru the msg list and free up */
208 destroy_workqueue(gVmbusConnection
.WorkQueue
);
210 gVmbusConnection
.ConnectState
= Disconnected
;
212 DPRINT_INFO(VMBUS
, "Vmbus disconnected!!");
221 * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
223 struct vmbus_channel
*GetChannelFromRelId(u32 relId
)
225 struct vmbus_channel
*channel
;
226 struct vmbus_channel
*foundChannel
= NULL
;
229 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
230 list_for_each_entry(channel
, &gVmbusConnection
.ChannelList
, ListEntry
) {
231 if (channel
->OfferMsg
.ChildRelId
== relId
) {
232 foundChannel
= channel
;
236 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
242 * VmbusProcessChannelEvent - Process a channel event notification
244 static void VmbusProcessChannelEvent(void *context
)
246 struct vmbus_channel
*channel
;
247 u32 relId
= (u32
)(unsigned long)context
;
252 * Find the channel based on this relid and invokes the
253 * channel callback to process the event
255 channel
= GetChannelFromRelId(relId
);
258 VmbusChannelOnChannelEvent(channel
);
260 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
261 * VmbusChannelOnChannelEvent,
265 DPRINT_ERR(VMBUS
, "channel not found for relid - %d.", relId
);
270 * VmbusOnEvents - Handler for events
272 void VmbusOnEvents(void)
275 int maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
278 u32
*recvInterruptPage
= gVmbusConnection
.RecvInterruptPage
;
283 if (recvInterruptPage
) {
284 for (dword
= 0; dword
< maxdword
; dword
++) {
285 if (recvInterruptPage
[dword
]) {
286 for (bit
= 0; bit
< 32; bit
++) {
287 if (test_and_clear_bit(bit
, (unsigned long *)&recvInterruptPage
[dword
])) {
288 relid
= (dword
<< 5) + bit
;
289 DPRINT_DBG(VMBUS
, "event detected for relid - %d", relid
);
292 /* special case - vmbus channel protocol msg */
293 DPRINT_DBG(VMBUS
, "invalid relid - %d", relid
);
296 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
297 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
298 VmbusProcessChannelEvent((void *)(unsigned long)relid
);
311 * VmbusPostMessage - Send a msg on the vmbus's message connection
313 int VmbusPostMessage(void *buffer
, size_t bufferLen
)
315 union hv_connection_id connId
;
318 connId
.u
.Id
= VMBUS_MESSAGE_CONNECTION_ID
;
319 return HvPostMessage(connId
, 1, buffer
, bufferLen
);
323 * VmbusSetEvent - Send an event notification to the parent
325 int VmbusSetEvent(u32 childRelId
)
331 /* Each u32 represents 32 channels */
332 set_bit(childRelId
& 31,
333 (unsigned long *)gVmbusConnection
.SendInterruptPage
+
336 ret
= HvSignalEvent();