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 vmbus_connection
= {
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
;
47 /* Make sure we are not connecting or connected */
48 if (vmbus_connection
.ConnectState
!= Disconnected
)
51 /* Initialize the vmbus connection */
52 vmbus_connection
.ConnectState
= Connecting
;
53 vmbus_connection
.WorkQueue
= create_workqueue("hv_vmbus_con");
54 if (!vmbus_connection
.WorkQueue
) {
59 INIT_LIST_HEAD(&vmbus_connection
.ChannelMsgList
);
60 spin_lock_init(&vmbus_connection
.channelmsg_lock
);
62 INIT_LIST_HEAD(&vmbus_connection
.ChannelList
);
63 spin_lock_init(&vmbus_connection
.channel_lock
);
66 * Setup the vmbus event connection for channel interrupt
69 vmbus_connection
.InterruptPage
= osd_page_alloc(1);
70 if (vmbus_connection
.InterruptPage
== NULL
) {
75 vmbus_connection
.RecvInterruptPage
= vmbus_connection
.InterruptPage
;
76 vmbus_connection
.SendInterruptPage
=
77 (void *)((unsigned long)vmbus_connection
.InterruptPage
+
81 * Setup the monitor notification facility. The 1st page for
82 * parent->child and the 2nd page for child->parent
84 vmbus_connection
.MonitorPages
= osd_page_alloc(2);
85 if (vmbus_connection
.MonitorPages
== NULL
) {
90 msginfo
= kzalloc(sizeof(*msginfo
) +
91 sizeof(struct vmbus_channel_initiate_contact
),
93 if (msginfo
== NULL
) {
98 msginfo
->waitevent
= osd_waitevent_create();
99 if (!msginfo
->waitevent
) {
104 msg
= (struct vmbus_channel_initiate_contact
*)msginfo
->msg
;
106 msg
->header
.msgtype
= CHANNELMSG_INITIATE_CONTACT
;
107 msg
->vmbus_version_requested
= VMBUS_REVISION_NUMBER
;
108 msg
->interrupt_page
= virt_to_phys(vmbus_connection
.InterruptPage
);
109 msg
->monitor_page1
= virt_to_phys(vmbus_connection
.MonitorPages
);
110 msg
->monitor_page2
= virt_to_phys(
111 (void *)((unsigned long)vmbus_connection
.MonitorPages
+
115 * Add to list before we send the request since we may
116 * receive the response before returning from this routine
118 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
119 list_add_tail(&msginfo
->msglistentry
,
120 &vmbus_connection
.ChannelMsgList
);
122 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
124 DPRINT_DBG(VMBUS
, "Vmbus connection - interrupt pfn %llx, "
125 "monitor1 pfn %llx,, monitor2 pfn %llx",
126 msg
->interrupt_page
, msg
->monitor_page1
, msg
->monitor_page2
);
128 DPRINT_DBG(VMBUS
, "Sending channel initiate msg...");
129 ret
= VmbusPostMessage(msg
,
130 sizeof(struct vmbus_channel_initiate_contact
));
132 list_del(&msginfo
->msglistentry
);
136 /* Wait for the connection response */
137 osd_waitevent_wait(msginfo
->waitevent
);
139 list_del(&msginfo
->msglistentry
);
141 /* Check if successful */
142 if (msginfo
->response
.version_response
.version_supported
) {
143 DPRINT_INFO(VMBUS
, "Vmbus connected!!");
144 vmbus_connection
.ConnectState
= Connected
;
147 DPRINT_ERR(VMBUS
, "Vmbus connection failed!!..."
148 "current version (%d) not supported",
149 VMBUS_REVISION_NUMBER
);
154 kfree(msginfo
->waitevent
);
159 vmbus_connection
.ConnectState
= Disconnected
;
161 if (vmbus_connection
.WorkQueue
)
162 destroy_workqueue(vmbus_connection
.WorkQueue
);
164 if (vmbus_connection
.InterruptPage
) {
165 osd_page_free(vmbus_connection
.InterruptPage
, 1);
166 vmbus_connection
.InterruptPage
= NULL
;
169 if (vmbus_connection
.MonitorPages
) {
170 osd_page_free(vmbus_connection
.MonitorPages
, 2);
171 vmbus_connection
.MonitorPages
= NULL
;
175 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
;
190 /* Make sure we are connected */
191 if (vmbus_connection
.ConnectState
!= Connected
)
194 msg
= kzalloc(sizeof(struct vmbus_channel_message_header
), GFP_KERNEL
);
198 msg
->msgtype
= CHANNELMSG_UNLOAD
;
200 ret
= VmbusPostMessage(msg
,
201 sizeof(struct vmbus_channel_message_header
));
205 osd_page_free(vmbus_connection
.InterruptPage
, 1);
207 /* TODO: iterate thru the msg list and free up */
208 destroy_workqueue(vmbus_connection
.WorkQueue
);
210 vmbus_connection
.ConnectState
= Disconnected
;
212 DPRINT_INFO(VMBUS
, "Vmbus disconnected!!");
220 * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id)
222 struct vmbus_channel
*GetChannelFromRelId(u32 relid
)
224 struct vmbus_channel
*channel
;
225 struct vmbus_channel
*found_channel
= NULL
;
228 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
229 list_for_each_entry(channel
, &vmbus_connection
.ChannelList
, listentry
) {
230 if (channel
->offermsg
.child_relid
== relid
) {
231 found_channel
= channel
;
235 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
237 return found_channel
;
241 * VmbusProcessChannelEvent - Process a channel event notification
243 static void VmbusProcessChannelEvent(void *context
)
245 struct vmbus_channel
*channel
;
246 u32 relid
= (u32
)(unsigned long)context
;
248 /* ASSERT(relId > 0); */
251 * Find the channel based on this relid and invokes the
252 * channel callback to process the event
254 channel
= GetChannelFromRelId(relid
);
257 vmbus_onchannel_event(channel
);
259 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
260 * vmbus_onchannel_event,
264 DPRINT_ERR(VMBUS
, "channel not found for relid - %d.", relid
);
269 * VmbusOnEvents - Handler for events
271 void VmbusOnEvents(void)
274 int maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
277 u32
*recv_int_page
= vmbus_connection
.RecvInterruptPage
;
281 for (dword
= 0; dword
< maxdword
; dword
++) {
282 if (recv_int_page
[dword
]) {
283 for (bit
= 0; bit
< 32; bit
++) {
284 if (test_and_clear_bit(bit
,
286 &recv_int_page
[dword
])) {
287 relid
= (dword
<< 5) + bit
;
288 DPRINT_DBG(VMBUS
, "event detected for relid - %d", relid
);
291 /* special case - vmbus channel protocol msg */
292 DPRINT_DBG(VMBUS
, "invalid relid - %d", relid
);
295 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
296 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
297 VmbusProcessChannelEvent((void *)(unsigned long)relid
);
308 * VmbusPostMessage - Send a msg on the vmbus's message connection
310 int VmbusPostMessage(void *buffer
, size_t buflen
)
312 union hv_connection_id conn_id
;
315 conn_id
.u
.id
= VMBUS_MESSAGE_CONNECTION_ID
;
316 return hv_post_message(conn_id
, 1, buffer
, buflen
);
320 * VmbusSetEvent - Send an event notification to the parent
322 int VmbusSetEvent(u32 child_relid
)
324 /* Each u32 represents 32 channels */
325 set_bit(child_relid
& 31,
326 (unsigned long *)vmbus_connection
.SendInterruptPage
+
329 return hv_signal_event();