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 .conn_state
= DISCONNECTED
,
34 .next_gpadl_handle
= ATOMIC_INIT(0xE1E10),
38 * vmbus_connect - Sends a connect request on the partition service connection
40 int vmbus_connect(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
.conn_state
!= DISCONNECTED
)
51 /* Initialize the vmbus connection */
52 vmbus_connection
.conn_state
= CONNECTING
;
53 vmbus_connection
.work_queue
= create_workqueue("hv_vmbus_con");
54 if (!vmbus_connection
.work_queue
) {
59 INIT_LIST_HEAD(&vmbus_connection
.chn_msg_list
);
60 spin_lock_init(&vmbus_connection
.channelmsg_lock
);
62 INIT_LIST_HEAD(&vmbus_connection
.chn_list
);
63 spin_lock_init(&vmbus_connection
.channel_lock
);
66 * Setup the vmbus event connection for channel interrupt
69 vmbus_connection
.int_page
=
70 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
, 0);
71 if (vmbus_connection
.int_page
== NULL
) {
76 vmbus_connection
.recv_int_page
= vmbus_connection
.int_page
;
77 vmbus_connection
.send_int_page
=
78 (void *)((unsigned long)vmbus_connection
.int_page
+
82 * Setup the monitor notification facility. The 1st page for
83 * parent->child and the 2nd page for child->parent
85 vmbus_connection
.monitor_pages
=
86 (void *)__get_free_pages((GFP_KERNEL
|__GFP_ZERO
), 1);
87 if (vmbus_connection
.monitor_pages
== NULL
) {
92 msginfo
= kzalloc(sizeof(*msginfo
) +
93 sizeof(struct vmbus_channel_initiate_contact
),
95 if (msginfo
== NULL
) {
100 msginfo
->waitevent
= osd_waitevent_create();
101 if (!msginfo
->waitevent
) {
106 msg
= (struct vmbus_channel_initiate_contact
*)msginfo
->msg
;
108 msg
->header
.msgtype
= CHANNELMSG_INITIATE_CONTACT
;
109 msg
->vmbus_version_requested
= VMBUS_REVISION_NUMBER
;
110 msg
->interrupt_page
= virt_to_phys(vmbus_connection
.int_page
);
111 msg
->monitor_page1
= virt_to_phys(vmbus_connection
.monitor_pages
);
112 msg
->monitor_page2
= virt_to_phys(
113 (void *)((unsigned long)vmbus_connection
.monitor_pages
+
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(&vmbus_connection
.channelmsg_lock
, flags
);
121 list_add_tail(&msginfo
->msglistentry
,
122 &vmbus_connection
.chn_msg_list
);
124 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
126 DPRINT_DBG(VMBUS
, "Vmbus connection - interrupt pfn %llx, "
127 "monitor1 pfn %llx,, monitor2 pfn %llx",
128 msg
->interrupt_page
, msg
->monitor_page1
, msg
->monitor_page2
);
130 DPRINT_DBG(VMBUS
, "Sending channel initiate msg...");
131 ret
= vmbus_post_msg(msg
,
132 sizeof(struct vmbus_channel_initiate_contact
));
134 list_del(&msginfo
->msglistentry
);
138 /* Wait for the connection response */
139 osd_waitevent_wait(msginfo
->waitevent
);
141 list_del(&msginfo
->msglistentry
);
143 /* Check if successful */
144 if (msginfo
->response
.version_response
.version_supported
) {
145 DPRINT_INFO(VMBUS
, "Vmbus connected!!");
146 vmbus_connection
.conn_state
= CONNECTED
;
149 DPRINT_ERR(VMBUS
, "Vmbus connection failed!!..."
150 "current version (%d) not supported",
151 VMBUS_REVISION_NUMBER
);
156 kfree(msginfo
->waitevent
);
161 vmbus_connection
.conn_state
= DISCONNECTED
;
163 if (vmbus_connection
.work_queue
)
164 destroy_workqueue(vmbus_connection
.work_queue
);
166 if (vmbus_connection
.int_page
) {
167 free_pages((unsigned long)vmbus_connection
.int_page
, 0);
168 vmbus_connection
.int_page
= NULL
;
171 if (vmbus_connection
.monitor_pages
) {
172 free_pages((unsigned long)vmbus_connection
.monitor_pages
, 1);
173 vmbus_connection
.monitor_pages
= NULL
;
177 kfree(msginfo
->waitevent
);
186 * Sends a disconnect request on the partition service connection
188 int vmbus_disconnect(void)
191 struct vmbus_channel_message_header
*msg
;
193 /* Make sure we are connected */
194 if (vmbus_connection
.conn_state
!= CONNECTED
)
197 msg
= kzalloc(sizeof(struct vmbus_channel_message_header
), GFP_KERNEL
);
201 msg
->msgtype
= CHANNELMSG_UNLOAD
;
203 ret
= vmbus_post_msg(msg
,
204 sizeof(struct vmbus_channel_message_header
));
208 free_pages((unsigned long)vmbus_connection
.int_page
, 0);
209 free_pages((unsigned long)vmbus_connection
.monitor_pages
, 1);
211 /* TODO: iterate thru the msg list and free up */
212 destroy_workqueue(vmbus_connection
.work_queue
);
214 vmbus_connection
.conn_state
= DISCONNECTED
;
216 DPRINT_INFO(VMBUS
, "Vmbus disconnected!!");
224 * relid2channel - Get the channel object given its
225 * child relative id (ie channel id)
227 struct vmbus_channel
*relid2channel(u32 relid
)
229 struct vmbus_channel
*channel
;
230 struct vmbus_channel
*found_channel
= NULL
;
233 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
234 list_for_each_entry(channel
, &vmbus_connection
.chn_list
, listentry
) {
235 if (channel
->offermsg
.child_relid
== relid
) {
236 found_channel
= channel
;
240 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
242 return found_channel
;
246 * process_chn_event - Process a channel event notification
248 static void process_chn_event(void *context
)
250 struct vmbus_channel
*channel
;
251 u32 relid
= (u32
)(unsigned long)context
;
253 /* ASSERT(relId > 0); */
256 * Find the channel based on this relid and invokes the
257 * channel callback to process the event
259 channel
= relid2channel(relid
);
262 vmbus_onchannel_event(channel
);
264 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
265 * vmbus_onchannel_event,
269 DPRINT_ERR(VMBUS
, "channel not found for relid - %d.", relid
);
274 * vmbus_on_event - Handler for events
276 void vmbus_on_event(void)
279 int maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
282 u32
*recv_int_page
= vmbus_connection
.recv_int_page
;
286 for (dword
= 0; dword
< maxdword
; dword
++) {
287 if (recv_int_page
[dword
]) {
288 for (bit
= 0; bit
< 32; bit
++) {
289 if (test_and_clear_bit(bit
,
291 &recv_int_page
[dword
])) {
292 relid
= (dword
<< 5) + bit
;
293 DPRINT_DBG(VMBUS
, "event detected for relid - %d", relid
);
296 /* special case - vmbus channel protocol msg */
297 DPRINT_DBG(VMBUS
, "invalid relid - %d", relid
);
300 /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */
301 /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */
302 process_chn_event((void *)
303 (unsigned long)relid
);
314 * vmbus_post_msg - Send a msg on the vmbus's message connection
316 int vmbus_post_msg(void *buffer
, size_t buflen
)
318 union hv_connection_id conn_id
;
321 conn_id
.u
.id
= VMBUS_MESSAGE_CONNECTION_ID
;
322 return hv_post_message(conn_id
, 1, buffer
, buflen
);
326 * vmbus_set_event - Send an event notification to the parent
328 int vmbus_set_event(u32 child_relid
)
330 /* Each u32 represents 32 channels */
331 set_bit(child_relid
& 31,
332 (unsigned long *)vmbus_connection
.send_int_page
+
335 return hv_signal_event();