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
= osd_page_alloc(1);
70 if (vmbus_connection
.int_page
== NULL
) {
75 vmbus_connection
.recv_int_page
= vmbus_connection
.int_page
;
76 vmbus_connection
.send_int_page
=
77 (void *)((unsigned long)vmbus_connection
.int_page
+
81 * Setup the monitor notification facility. The 1st page for
82 * parent->child and the 2nd page for child->parent
84 vmbus_connection
.monitor_pages
= osd_page_alloc(2);
85 if (vmbus_connection
.monitor_pages
== 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
.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 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
.conn_state
= 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
.conn_state
= DISCONNECTED
;
161 if (vmbus_connection
.work_queue
)
162 destroy_workqueue(vmbus_connection
.work_queue
);
164 if (vmbus_connection
.int_page
) {
165 osd_page_free(vmbus_connection
.int_page
, 1);
166 vmbus_connection
.int_page
= NULL
;
169 if (vmbus_connection
.monitor_pages
) {
170 osd_page_free(vmbus_connection
.monitor_pages
, 2);
171 vmbus_connection
.monitor_pages
= NULL
;
175 kfree(msginfo
->waitevent
);
184 * Sends a disconnect request on the partition service connection
186 int vmbus_disconnect(void)
189 struct vmbus_channel_message_header
*msg
;
191 /* Make sure we are connected */
192 if (vmbus_connection
.conn_state
!= CONNECTED
)
195 msg
= kzalloc(sizeof(struct vmbus_channel_message_header
), GFP_KERNEL
);
199 msg
->msgtype
= CHANNELMSG_UNLOAD
;
201 ret
= vmbus_post_msg(msg
,
202 sizeof(struct vmbus_channel_message_header
));
206 osd_page_free(vmbus_connection
.int_page
, 1);
208 /* TODO: iterate thru the msg list and free up */
209 destroy_workqueue(vmbus_connection
.work_queue
);
211 vmbus_connection
.conn_state
= DISCONNECTED
;
213 DPRINT_INFO(VMBUS
, "Vmbus disconnected!!");
221 * relid2channel - Get the channel object given its
222 * child relative id (ie channel id)
224 struct vmbus_channel
*relid2channel(u32 relid
)
226 struct vmbus_channel
*channel
;
227 struct vmbus_channel
*found_channel
= NULL
;
230 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
231 list_for_each_entry(channel
, &vmbus_connection
.chn_list
, listentry
) {
232 if (channel
->offermsg
.child_relid
== relid
) {
233 found_channel
= channel
;
237 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
239 return found_channel
;
243 * process_chn_event - Process a channel event notification
245 static void process_chn_event(void *context
)
247 struct vmbus_channel
*channel
;
248 u32 relid
= (u32
)(unsigned long)context
;
250 /* ASSERT(relId > 0); */
253 * Find the channel based on this relid and invokes the
254 * channel callback to process the event
256 channel
= relid2channel(relid
);
259 vmbus_onchannel_event(channel
);
261 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
262 * vmbus_onchannel_event,
266 DPRINT_ERR(VMBUS
, "channel not found for relid - %d.", relid
);
271 * vmbus_on_event - Handler for events
273 void vmbus_on_event(void)
276 int maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
279 u32
*recv_int_page
= vmbus_connection
.recv_int_page
;
283 for (dword
= 0; dword
< maxdword
; dword
++) {
284 if (recv_int_page
[dword
]) {
285 for (bit
= 0; bit
< 32; bit
++) {
286 if (test_and_clear_bit(bit
,
288 &recv_int_page
[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 process_chn_event((void *)
300 (unsigned long)relid
);
311 * vmbus_post_msg - Send a msg on the vmbus's message connection
313 int vmbus_post_msg(void *buffer
, size_t buflen
)
315 union hv_connection_id conn_id
;
318 conn_id
.u
.id
= VMBUS_MESSAGE_CONNECTION_ID
;
319 return hv_post_message(conn_id
, 1, buffer
, buflen
);
323 * vmbus_set_event - Send an event notification to the parent
325 int vmbus_set_event(u32 child_relid
)
327 /* Each u32 represents 32 channels */
328 set_bit(child_relid
& 31,
329 (unsigned long *)vmbus_connection
.send_int_page
+
332 return hv_signal_event();