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/slab.h>
26 #include <linux/vmalloc.h>
29 #include "vmbus_private.h"
32 struct VMBUS_CONNECTION gVmbusConnection
= {
33 .ConnectState
= Disconnected
,
34 .NextGpadlHandle
= ATOMIC_INIT(0xE1E10),
38 * VmbusConnect - Sends a connect request on the partition service connection
40 int VmbusConnect(void)
43 struct vmbus_channel_msginfo
*msgInfo
= NULL
;
44 struct vmbus_channel_initiate_contact
*msg
;
49 /* Make sure we are not connecting or connected */
50 if (gVmbusConnection
.ConnectState
!= Disconnected
)
53 /* Initialize the vmbus connection */
54 gVmbusConnection
.ConnectState
= Connecting
;
55 gVmbusConnection
.WorkQueue
= create_workqueue("hv_vmbus_con");
56 if (!gVmbusConnection
.WorkQueue
) {
61 INIT_LIST_HEAD(&gVmbusConnection
.ChannelMsgList
);
62 spin_lock_init(&gVmbusConnection
.channelmsg_lock
);
64 INIT_LIST_HEAD(&gVmbusConnection
.ChannelList
);
65 spin_lock_init(&gVmbusConnection
.channel_lock
);
68 * Setup the vmbus event connection for channel interrupt
71 gVmbusConnection
.InterruptPage
= osd_PageAlloc(1);
72 if (gVmbusConnection
.InterruptPage
== NULL
) {
77 gVmbusConnection
.RecvInterruptPage
= gVmbusConnection
.InterruptPage
;
78 gVmbusConnection
.SendInterruptPage
=
79 (void *)((unsigned long)gVmbusConnection
.InterruptPage
+
83 * Setup the monitor notification facility. The 1st page for
84 * parent->child and the 2nd page for child->parent
86 gVmbusConnection
.MonitorPages
= osd_PageAlloc(2);
87 if (gVmbusConnection
.MonitorPages
== NULL
) {
92 msgInfo
= kzalloc(sizeof(*msgInfo
) +
93 sizeof(struct vmbus_channel_initiate_contact
),
95 if (msgInfo
== NULL
) {
100 msgInfo
->WaitEvent
= osd_WaitEventCreate();
101 if (!msgInfo
->WaitEvent
) {
106 msg
= (struct vmbus_channel_initiate_contact
*)msgInfo
->Msg
;
108 msg
->Header
.MessageType
= ChannelMessageInitiateContact
;
109 msg
->VMBusVersionRequested
= VMBUS_REVISION_NUMBER
;
110 msg
->InterruptPage
= virt_to_phys(gVmbusConnection
.InterruptPage
);
111 msg
->MonitorPage1
= virt_to_phys(gVmbusConnection
.MonitorPages
);
112 msg
->MonitorPage2
= virt_to_phys(
113 (void *)((unsigned long)gVmbusConnection
.MonitorPages
+
117 * Add to list before we send the request since we may
118 * receive the response before returning from this routine
120 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
121 list_add_tail(&msgInfo
->MsgListEntry
,
122 &gVmbusConnection
.ChannelMsgList
);
124 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
126 DPRINT_DBG(VMBUS
, "Vmbus connection - interrupt pfn %llx, "
127 "monitor1 pfn %llx,, monitor2 pfn %llx",
128 msg
->InterruptPage
, msg
->MonitorPage1
, msg
->MonitorPage2
);
130 DPRINT_DBG(VMBUS
, "Sending channel initiate msg...");
131 ret
= VmbusPostMessage(msg
,
132 sizeof(struct vmbus_channel_initiate_contact
));
134 list_del(&msgInfo
->MsgListEntry
);
138 /* Wait for the connection response */
139 osd_WaitEventWait(msgInfo
->WaitEvent
);
141 list_del(&msgInfo
->MsgListEntry
);
143 /* Check if successful */
144 if (msgInfo
->Response
.VersionResponse
.VersionSupported
) {
145 DPRINT_INFO(VMBUS
, "Vmbus connected!!");
146 gVmbusConnection
.ConnectState
= Connected
;
149 DPRINT_ERR(VMBUS
, "Vmbus connection failed!!..."
150 "current version (%d) not supported",
151 VMBUS_REVISION_NUMBER
);
156 kfree(msgInfo
->WaitEvent
);
163 gVmbusConnection
.ConnectState
= Disconnected
;
165 if (gVmbusConnection
.WorkQueue
)
166 destroy_workqueue(gVmbusConnection
.WorkQueue
);
168 if (gVmbusConnection
.InterruptPage
) {
169 osd_PageFree(gVmbusConnection
.InterruptPage
, 1);
170 gVmbusConnection
.InterruptPage
= NULL
;
173 if (gVmbusConnection
.MonitorPages
) {
174 osd_PageFree(gVmbusConnection
.MonitorPages
, 2);
175 gVmbusConnection
.MonitorPages
= NULL
;
179 kfree(msgInfo
->WaitEvent
);
189 * VmbusDisconnect - Sends a disconnect request on the partition service connection
191 int VmbusDisconnect(void)
194 struct vmbus_channel_message_header
*msg
;
198 /* Make sure we are connected */
199 if (gVmbusConnection
.ConnectState
!= Connected
)
202 msg
= kzalloc(sizeof(struct vmbus_channel_message_header
), GFP_KERNEL
);
206 msg
->MessageType
= ChannelMessageUnload
;
208 ret
= VmbusPostMessage(msg
,
209 sizeof(struct vmbus_channel_message_header
));
213 osd_PageFree(gVmbusConnection
.InterruptPage
, 1);
215 /* TODO: iterate thru the msg list and free up */
216 destroy_workqueue(gVmbusConnection
.WorkQueue
);
218 gVmbusConnection
.ConnectState
= Disconnected
;
220 DPRINT_INFO(VMBUS
, "Vmbus disconnected!!");
229 * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
231 struct vmbus_channel
*GetChannelFromRelId(u32 relId
)
233 struct vmbus_channel
*channel
;
234 struct vmbus_channel
*foundChannel
= NULL
;
237 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
238 list_for_each_entry(channel
, &gVmbusConnection
.ChannelList
, ListEntry
) {
239 if (channel
->OfferMsg
.ChildRelId
== relId
) {
240 foundChannel
= channel
;
244 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
250 * VmbusProcessChannelEvent - Process a channel event notification
252 static void VmbusProcessChannelEvent(void *context
)
254 struct vmbus_channel
*channel
;
255 u32 relId
= (u32
)(unsigned long)context
;
257 /* ASSERT(relId > 0); */
260 * Find the channel based on this relid and invokes the
261 * channel callback to process the event
263 channel
= GetChannelFromRelId(relId
);
266 VmbusChannelOnChannelEvent(channel
);
268 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
269 * VmbusChannelOnChannelEvent,
273 DPRINT_ERR(VMBUS
, "channel not found for relid - %d.", relId
);
278 * VmbusOnEvents - Handler for events
280 void VmbusOnEvents(void)
283 int maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
286 u32
*recvInterruptPage
= gVmbusConnection
.RecvInterruptPage
;
291 if (recvInterruptPage
) {
292 for (dword
= 0; dword
< maxdword
; dword
++) {
293 if (recvInterruptPage
[dword
]) {
294 for (bit
= 0; bit
< 32; bit
++) {
295 if (test_and_clear_bit(bit
, (unsigned long *)&recvInterruptPage
[dword
])) {
296 relid
= (dword
<< 5) + bit
;
297 DPRINT_DBG(VMBUS
, "event detected for relid - %d", relid
);
300 /* special case - vmbus channel protocol msg */
301 DPRINT_DBG(VMBUS
, "invalid relid - %d", relid
);
304 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
305 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
306 VmbusProcessChannelEvent((void *)(unsigned long)relid
);
319 * VmbusPostMessage - Send a msg on the vmbus's message connection
321 int VmbusPostMessage(void *buffer
, size_t bufferLen
)
323 union hv_connection_id connId
;
326 connId
.u
.Id
= VMBUS_MESSAGE_CONNECTION_ID
;
327 return HvPostMessage(connId
, 1, buffer
, bufferLen
);
331 * VmbusSetEvent - Send an event notification to the parent
333 int VmbusSetEvent(u32 childRelId
)
339 /* Each u32 represents 32 channels */
340 set_bit(childRelId
& 31,
341 (unsigned long *)gVmbusConnection
.SendInterruptPage
+
344 ret
= HvSignalEvent();