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 "VmbusPrivate.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 msg
= (struct vmbus_channel_initiate_contact
*)msgInfo
->Msg
;
103 msg
->Header
.MessageType
= ChannelMessageInitiateContact
;
104 msg
->VMBusVersionRequested
= VMBUS_REVISION_NUMBER
;
105 msg
->InterruptPage
= virt_to_phys(gVmbusConnection
.InterruptPage
);
106 msg
->MonitorPage1
= virt_to_phys(gVmbusConnection
.MonitorPages
);
107 msg
->MonitorPage2
= virt_to_phys(
108 (void *)((unsigned long)gVmbusConnection
.MonitorPages
+
112 * Add to list before we send the request since we may
113 * receive the response before returning from this routine
115 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
116 list_add_tail(&msgInfo
->MsgListEntry
,
117 &gVmbusConnection
.ChannelMsgList
);
119 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
121 DPRINT_DBG(VMBUS
, "Vmbus connection - interrupt pfn %llx, "
122 "monitor1 pfn %llx,, monitor2 pfn %llx",
123 msg
->InterruptPage
, msg
->MonitorPage1
, msg
->MonitorPage2
);
125 DPRINT_DBG(VMBUS
, "Sending channel initiate msg...");
126 ret
= VmbusPostMessage(msg
,
127 sizeof(struct vmbus_channel_initiate_contact
));
129 list_del(&msgInfo
->MsgListEntry
);
133 /* Wait for the connection response */
134 osd_WaitEventWait(msgInfo
->WaitEvent
);
136 list_del(&msgInfo
->MsgListEntry
);
138 /* Check if successful */
139 if (msgInfo
->Response
.VersionResponse
.VersionSupported
) {
140 DPRINT_INFO(VMBUS
, "Vmbus connected!!");
141 gVmbusConnection
.ConnectState
= Connected
;
144 DPRINT_ERR(VMBUS
, "Vmbus connection failed!!..."
145 "current version (%d) not supported",
146 VMBUS_REVISION_NUMBER
);
151 kfree(msgInfo
->WaitEvent
);
158 gVmbusConnection
.ConnectState
= Disconnected
;
160 if (gVmbusConnection
.WorkQueue
)
161 destroy_workqueue(gVmbusConnection
.WorkQueue
);
163 if (gVmbusConnection
.InterruptPage
) {
164 osd_PageFree(gVmbusConnection
.InterruptPage
, 1);
165 gVmbusConnection
.InterruptPage
= NULL
;
168 if (gVmbusConnection
.MonitorPages
) {
169 osd_PageFree(gVmbusConnection
.MonitorPages
, 2);
170 gVmbusConnection
.MonitorPages
= NULL
;
174 kfree(msgInfo
->WaitEvent
);
184 * VmbusDisconnect - Sends a disconnect request on the partition service connection
186 int VmbusDisconnect(void)
189 struct vmbus_channel_message_header
*msg
;
193 /* Make sure we are connected */
194 if (gVmbusConnection
.ConnectState
!= Connected
)
197 msg
= kzalloc(sizeof(struct vmbus_channel_message_header
), GFP_KERNEL
);
199 msg
->MessageType
= ChannelMessageUnload
;
201 ret
= VmbusPostMessage(msg
,
202 sizeof(struct vmbus_channel_message_header
));
206 osd_PageFree(gVmbusConnection
.InterruptPage
, 1);
208 /* TODO: iterate thru the msg list and free up */
209 destroy_workqueue(gVmbusConnection
.WorkQueue
);
211 gVmbusConnection
.ConnectState
= Disconnected
;
213 DPRINT_INFO(VMBUS
, "Vmbus disconnected!!");
222 * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
224 struct vmbus_channel
*GetChannelFromRelId(u32 relId
)
226 struct vmbus_channel
*channel
;
227 struct vmbus_channel
*foundChannel
= NULL
;
230 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
231 list_for_each_entry(channel
, &gVmbusConnection
.ChannelList
, ListEntry
) {
232 if (channel
->OfferMsg
.ChildRelId
== relId
) {
233 foundChannel
= channel
;
237 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
243 * VmbusProcessChannelEvent - Process a channel event notification
245 static void VmbusProcessChannelEvent(void *context
)
247 struct vmbus_channel
*channel
;
248 u32 relId
= (u32
)(unsigned long)context
;
253 * Find the channel based on this relid and invokes the
254 * channel callback to process the event
256 channel
= GetChannelFromRelId(relId
);
259 VmbusChannelOnChannelEvent(channel
);
261 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
262 * VmbusChannelOnChannelEvent,
266 DPRINT_ERR(VMBUS
, "channel not found for relid - %d.", relId
);
271 * VmbusOnEvents - Handler for events
273 void VmbusOnEvents(void)
276 int maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
279 u32
*recvInterruptPage
= gVmbusConnection
.RecvInterruptPage
;
284 if (recvInterruptPage
) {
285 for (dword
= 0; dword
< maxdword
; dword
++) {
286 if (recvInterruptPage
[dword
]) {
287 for (bit
= 0; bit
< 32; bit
++) {
288 if (test_and_clear_bit(bit
, (unsigned long *)&recvInterruptPage
[dword
])) {
289 relid
= (dword
<< 5) + bit
;
290 DPRINT_DBG(VMBUS
, "event detected for relid - %d", relid
);
293 /* special case - vmbus channel protocol msg */
294 DPRINT_DBG(VMBUS
, "invalid relid - %d", relid
);
297 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
298 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
299 VmbusProcessChannelEvent((void *)(unsigned long)relid
);
312 * VmbusPostMessage - Send a msg on the vmbus's message connection
314 int VmbusPostMessage(void *buffer
, size_t bufferLen
)
316 union hv_connection_id connId
;
319 connId
.u
.Id
= VMBUS_MESSAGE_CONNECTION_ID
;
320 return HvPostMessage(connId
, 1, buffer
, bufferLen
);
324 * VmbusSetEvent - Send an event notification to the parent
326 int VmbusSetEvent(u32 childRelId
)
332 /* Each u32 represents 32 channels */
333 set_bit(childRelId
& 31,
334 (unsigned long *)gVmbusConnection
.SendInterruptPage
+
337 ret
= HvSignalEvent();