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>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
31 #include "vmbus_private.h"
34 struct vmbus_connection vmbus_connection
= {
35 .conn_state
= DISCONNECTED
,
36 .next_gpadl_handle
= ATOMIC_INIT(0xE1E10),
40 * vmbus_connect - Sends a connect request on the partition service connection
42 int vmbus_connect(void)
45 struct vmbus_channel_msginfo
*msginfo
= NULL
;
46 struct vmbus_channel_initiate_contact
*msg
;
49 /* Make sure we are not connecting or connected */
50 if (vmbus_connection
.conn_state
!= DISCONNECTED
)
53 /* Initialize the vmbus connection */
54 vmbus_connection
.conn_state
= CONNECTING
;
55 vmbus_connection
.work_queue
= create_workqueue("hv_vmbus_con");
56 if (!vmbus_connection
.work_queue
) {
61 INIT_LIST_HEAD(&vmbus_connection
.chn_msg_list
);
62 spin_lock_init(&vmbus_connection
.channelmsg_lock
);
64 INIT_LIST_HEAD(&vmbus_connection
.chn_list
);
65 spin_lock_init(&vmbus_connection
.channel_lock
);
68 * Setup the vmbus event connection for channel interrupt
71 vmbus_connection
.int_page
=
72 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
, 0);
73 if (vmbus_connection
.int_page
== NULL
) {
78 vmbus_connection
.recv_int_page
= vmbus_connection
.int_page
;
79 vmbus_connection
.send_int_page
=
80 (void *)((unsigned long)vmbus_connection
.int_page
+
84 * Setup the monitor notification facility. The 1st page for
85 * parent->child and the 2nd page for child->parent
87 vmbus_connection
.monitor_pages
=
88 (void *)__get_free_pages((GFP_KERNEL
|__GFP_ZERO
), 1);
89 if (vmbus_connection
.monitor_pages
== NULL
) {
94 msginfo
= kzalloc(sizeof(*msginfo
) +
95 sizeof(struct vmbus_channel_initiate_contact
),
97 if (msginfo
== NULL
) {
102 init_waitqueue_head(&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
.int_page
);
109 msg
->monitor_page1
= virt_to_phys(vmbus_connection
.monitor_pages
);
110 msg
->monitor_page2
= virt_to_phys(
111 (void *)((unsigned long)vmbus_connection
.monitor_pages
+
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
.chn_msg_list
);
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
= vmbus_post_msg(msg
,
130 sizeof(struct vmbus_channel_initiate_contact
));
132 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
133 list_del(&msginfo
->msglistentry
);
134 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
,
139 /* Wait for the connection response */
140 msginfo
->wait_condition
= 0;
141 wait_event_timeout(msginfo
->waitevent
, msginfo
->wait_condition
,
142 msecs_to_jiffies(1000));
143 if (msginfo
->wait_condition
== 0) {
144 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
,
146 list_del(&msginfo
->msglistentry
);
147 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
,
153 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
154 list_del(&msginfo
->msglistentry
);
155 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
157 /* Check if successful */
158 if (msginfo
->response
.version_response
.version_supported
) {
159 DPRINT_INFO(VMBUS
, "Vmbus connected!!");
160 vmbus_connection
.conn_state
= CONNECTED
;
163 DPRINT_ERR(VMBUS
, "Vmbus connection failed!!..."
164 "current version (%d) not supported",
165 VMBUS_REVISION_NUMBER
);
174 vmbus_connection
.conn_state
= DISCONNECTED
;
176 if (vmbus_connection
.work_queue
)
177 destroy_workqueue(vmbus_connection
.work_queue
);
179 if (vmbus_connection
.int_page
) {
180 free_pages((unsigned long)vmbus_connection
.int_page
, 0);
181 vmbus_connection
.int_page
= NULL
;
184 if (vmbus_connection
.monitor_pages
) {
185 free_pages((unsigned long)vmbus_connection
.monitor_pages
, 1);
186 vmbus_connection
.monitor_pages
= NULL
;
198 * Sends a disconnect request on the partition service connection
200 int vmbus_disconnect(void)
203 struct vmbus_channel_message_header
*msg
;
205 /* Make sure we are connected */
206 if (vmbus_connection
.conn_state
!= CONNECTED
)
209 msg
= kzalloc(sizeof(struct vmbus_channel_message_header
), GFP_KERNEL
);
213 msg
->msgtype
= CHANNELMSG_UNLOAD
;
215 ret
= vmbus_post_msg(msg
,
216 sizeof(struct vmbus_channel_message_header
));
220 free_pages((unsigned long)vmbus_connection
.int_page
, 0);
221 free_pages((unsigned long)vmbus_connection
.monitor_pages
, 1);
223 /* TODO: iterate thru the msg list and free up */
224 destroy_workqueue(vmbus_connection
.work_queue
);
226 vmbus_connection
.conn_state
= DISCONNECTED
;
228 DPRINT_INFO(VMBUS
, "Vmbus disconnected!!");
236 * relid2channel - Get the channel object given its
237 * child relative id (ie channel id)
239 struct vmbus_channel
*relid2channel(u32 relid
)
241 struct vmbus_channel
*channel
;
242 struct vmbus_channel
*found_channel
= NULL
;
245 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
246 list_for_each_entry(channel
, &vmbus_connection
.chn_list
, listentry
) {
247 if (channel
->offermsg
.child_relid
== relid
) {
248 found_channel
= channel
;
252 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
254 return found_channel
;
258 * process_chn_event - Process a channel event notification
260 static void process_chn_event(void *context
)
262 struct vmbus_channel
*channel
;
263 u32 relid
= (u32
)(unsigned long)context
;
265 /* ASSERT(relId > 0); */
268 * Find the channel based on this relid and invokes the
269 * channel callback to process the event
271 channel
= relid2channel(relid
);
274 vmbus_onchannel_event(channel
);
276 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
277 * vmbus_onchannel_event,
281 DPRINT_ERR(VMBUS
, "channel not found for relid - %d.", relid
);
286 * vmbus_on_event - Handler for events
288 void vmbus_on_event(void)
291 int maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
294 u32
*recv_int_page
= vmbus_connection
.recv_int_page
;
298 for (dword
= 0; dword
< maxdword
; dword
++) {
299 if (recv_int_page
[dword
]) {
300 for (bit
= 0; bit
< 32; bit
++) {
301 if (test_and_clear_bit(bit
,
303 &recv_int_page
[dword
])) {
304 relid
= (dword
<< 5) + bit
;
305 DPRINT_DBG(VMBUS
, "event detected for relid - %d", relid
);
308 /* special case - vmbus channel protocol msg */
309 DPRINT_DBG(VMBUS
, "invalid relid - %d", relid
);
312 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
313 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
314 process_chn_event((void *)
315 (unsigned long)relid
);
326 * vmbus_post_msg - Send a msg on the vmbus's message connection
328 int vmbus_post_msg(void *buffer
, size_t buflen
)
330 union hv_connection_id conn_id
;
333 conn_id
.u
.id
= VMBUS_MESSAGE_CONNECTION_ID
;
334 return hv_post_message(conn_id
, 1, buffer
, buflen
);
338 * vmbus_set_event - Send an event notification to the parent
340 int vmbus_set_event(u32 child_relid
)
342 /* Each u32 represents 32 channels */
343 set_bit(child_relid
& 31,
344 (unsigned long *)vmbus_connection
.send_int_page
+
347 return hv_signal_event();